1 /*******************************************************************************
2 this code is protected by the GNU affero GPLv3
3 author:Sylvain BERTRAND <sylvain.bertrand AT gmail dot com>
4 *******************************************************************************/
6 we use libraries based on C posix runtime, then we need this to fetch
7 environment variables from the C posix runtime
11 #include <ulinux/compiler_types.h>
12 #include <ulinux/sysc.h>
13 #include <ulinux/types.h>
14 #include <ulinux/error.h>
15 #include <ulinux/fs.h>
16 #include <ulinux/signal/signal.h>
17 #include <ulinux/utils/mem.h>
18 #include <ulinux/utils/ascii/string/string.h>
22 #include "ulinux_namespace.h"
29 static void sigs_setup(void)
34 OUT(PRE
"setting up signals...\n");
35 r
=rt_sigprocmask(SIG_BLOCK
,&mask
,0,sizeof(mask
));
37 OUT("ERROR:unable to block all signals (except KILL and STOP)\n");
43 static void sysfs_mount(void)
45 OUT(PRE
"mounting sysfs...\n");
46 l r
=mount(0,"/sys","sysfs",MS_NOATIME
|MS_NODIRATIME
,0);
48 OUT("ERROR(%ld):unable to mount sysfs\n",r
);
53 static void sysfs_umount(void)
55 OUT(PRE
"umounting sysfs...\n");
58 OUT("ERROR(%ld):unable to umount sysfs\n",r
);
63 static void proc_mount(void)
65 OUT(PRE
"mounting proc...\n");
66 l r
=mount(0,"/proc","proc",MS_NOATIME
|MS_NODIRATIME
,0);
68 OUT("ERROR(%ld):unable to mount proc\n",r
);
73 static void proc_umount(void)
75 OUT(PRE
"umounting proc...\n");
78 OUT("ERROR(%ld):unable to umount proc\n",r
);
83 static void devtmpfs_mount(void)
85 OUT(PRE
"mounting devtmpfs...\n");
86 l r
=mount(0,"/dev","devtmpfs",MS_NOATIME
|MS_NODIRATIME
,0);
88 OUT("ERROR(%ld):unable to mount devtmpfs\n",r
);
93 static void devtmpfs_umount(void)
95 OUT(PRE
"umounting devtmpfs...\n");
98 OUT("ERROR(%ld):unable to umount devtmpfs\n",r
);
103 static void newroot_mount(void)
105 OUT(PRE
"mounting root device %s on /newroot, root file system is %s...\n",
106 root_dev_path
,root_fs_type
);
107 l r
=mount(root_dev_path
,"/newroot",root_fs_type
,MS_NOATIME
|MS_NODIRATIME
,0);
109 OUT("ERROR(%ld):unable to mount root\n",r
);
114 static void newroot_chdir()
116 OUT(PRE
"chdir to /newroot...");
117 l r
=chdir("/newroot");
119 OUT("ERROR(%ld):unable to chdir to /newroot\n",r
);
125 static void newroot_move(void)
127 OUT(PRE
"mount moving . (/newroot) to / ...");
128 l r
=mount(".","/",0,MS_MOVE
,0);
130 OUT("ERROR(%ld):unable to mount move . (/newroot) to /\n",r
);
136 static void newroot_chroot(void)
138 OUT(PRE
"chroot to . (/newroot)...");
141 OUT("ERROR(%ld):unable to chroot to . (/newroot)\n",r
);
147 /*this parameter *must* be around*/
148 #define PARAM_NAME "cinitramfs_root_uuid"
149 static void root_uuid_get(i argc
, c
**argv
)
153 /*we should use libc getenv to be friendly with the C runtime*/
154 root_uuid
=(u8
*)getenv(PARAM_NAME
);
155 if(root_uuid
) return;
157 /*it's not in the environment, then could be in argv, see
158 Documentation/kernel-parameters.txt*/
160 if(argc
==1) goto fatal_err
;
164 if(strncmp(argv
[arg
],PARAM_NAME
,sizeof(PARAM_NAME
)-1)==0) break;
165 if(++arg
==argc
) goto fatal_err
;
167 root_uuid
=(u8
*)argv
[arg
];
168 loop
if(*root_uuid
++==(u8
)'=') break;/*lookup for the key/value separator*/
172 OUT(PRE
"ERROR:unable to find " PARAM_NAME
"\n");
177 static void command_line_parse(i argc
, c
**argv
)
179 root_uuid_get(argc
, argv
);
183 fds 0,1 and 2 are rw /dev/console, see linux/init/main.
184 Since we use standard C libs, be nice with the C runtime and use main instead of
187 i
main(i argc
,c
**argv
)
192 static u8 dprintf_buf
[DPRINTF_BUF_SZ
];
193 g_dprintf_buf
=dprintf_buf
;
196 command_line_parse(argc
,argv
);
202 modules_probe_static();
203 modules_probe_drivers();
204 r0
=uevents_process();
205 if(r0
==ROOT_NOT_FOUND
){
206 OUT(PRE
"terminating cinitramfs since no proper root was found\n");
209 /*from here, we located our root*/
211 modules_probe_name(root_fs_type
);
223 r1
=execve("/bin/init");
224 if(ISERR(r1
)){OUT("ERROR(%ld):unable to execve /bin/init\n",r1
);}