blob: 7868a6039fb4519c8a3dc175d7f8454113e2fc01 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
H Hartley Sweetenc67e5382012-05-31 16:26:10 -07002/*
3 * Many of the syscalls used in this file expect some of the arguments
4 * to be __user pointers not __kernel pointers. To limit the sparse
5 * noise, turn off sparse checking for this file.
6 */
7#ifdef __CHECKER__
8#undef __CHECKER__
9#warning "Sparse checking disabled for this file"
10#endif
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/unistd.h>
13#include <linux/kernel.h>
14#include <linux/fs.h>
15#include <linux/minix_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/romfs_fs.h>
17#include <linux/initrd.h>
18#include <linux/sched.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080019#include <linux/freezer.h>
Al Viroba4df282012-10-02 15:29:10 -040020#include <linux/kmod.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include "do_mounts.h"
23
24unsigned long initrd_start, initrd_end;
25int initrd_below_start_ok;
26unsigned int real_root_dev; /* do_proc_dointvec cannot handle kdev_t */
Linus Torvalds1da177e2005-04-16 15:20:36 -070027static int __initdata mount_initrd = 1;
28
29static int __init no_initrd(char *str)
30{
31 mount_initrd = 0;
32 return 1;
33}
34
35__setup("noinitrd", no_initrd);
36
Al Viroba4df282012-10-02 15:29:10 -040037static int init_linuxrc(struct subprocess_info *info, struct cred *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Al Viroba4df282012-10-02 15:29:10 -040039 sys_unshare(CLONE_FS | CLONE_FILES);
Al Viro43b16822013-01-19 13:29:54 -050040 /* stdin/stdout/stderr for /linuxrc */
41 sys_open("/dev/console", O_RDWR, 0);
42 sys_dup(0);
43 sys_dup(0);
Al Viroba4df282012-10-02 15:29:10 -040044 /* move initrd over / and chdir/chroot in initrd root */
45 sys_chdir("/root");
Dominik Brodowski312db1a2018-03-11 11:34:39 +010046 ksys_mount(".", "/", NULL, MS_MOVE, NULL);
Al Viroba4df282012-10-02 15:29:10 -040047 sys_chroot(".");
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 sys_setsid();
Al Viroba4df282012-10-02 15:29:10 -040049 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
52static void __init handle_initrd(void)
53{
Lucas De Marchi907ed132013-04-30 15:28:07 -070054 struct subprocess_info *info;
Al Viroba4df282012-10-02 15:29:10 -040055 static char *argv[] = { "linuxrc", NULL, };
56 extern char *envp_init[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59 real_root_dev = new_encode_dev(ROOT_DEV);
Greg Kroah-Hartmanbdaf8522005-06-20 21:15:16 -070060 create_dev("/dev/root.old", Root_RAM0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 /* mount initrd on rootfs' /root */
62 mount_block_root("/dev/root.old", root_mountflags & ~MS_RDONLY);
63 sys_mkdir("/old", 0700);
Al Viroba4df282012-10-02 15:29:10 -040064 sys_chdir("/old");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Tejun Heobb813f42013-01-18 14:05:56 -080066 /* try loading default modules from initrd */
67 load_default_modules();
68
Rafael J. Wysocki8baabde2007-11-21 02:50:17 +010069 /*
70 * In case that a resume from disk is carried out by linuxrc or one of
71 * its children, we need to tell the freezer not to wait for us.
72 */
73 current->flags |= PF_FREEZER_SKIP;
74
Lucas De Marchi907ed132013-04-30 15:28:07 -070075 info = call_usermodehelper_setup("/linuxrc", argv, envp_init,
76 GFP_KERNEL, init_linuxrc, NULL, NULL);
77 if (!info)
78 return;
79 call_usermodehelper_exec(info, UMH_WAIT_PROC);
Rafael J. Wysocki8baabde2007-11-21 02:50:17 +010080
81 current->flags &= ~PF_FREEZER_SKIP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 /* move initrd to rootfs' /old */
Dominik Brodowski312db1a2018-03-11 11:34:39 +010084 ksys_mount("..", ".", NULL, MS_MOVE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 /* switch root and cwd back to / of rootfs */
Al Viroba4df282012-10-02 15:29:10 -040086 sys_chroot("..");
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 if (new_decode_dev(real_root_dev) == Root_RAM0) {
89 sys_chdir("/old");
90 return;
91 }
92
Al Viroba4df282012-10-02 15:29:10 -040093 sys_chdir("/");
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 ROOT_DEV = new_decode_dev(real_root_dev);
95 mount_root();
96
97 printk(KERN_NOTICE "Trying to move old root to /initrd ... ");
Dominik Brodowski312db1a2018-03-11 11:34:39 +010098 error = ksys_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 if (!error)
100 printk("okay\n");
101 else {
102 int fd = sys_open("/dev/root.old", O_RDWR, 0);
Jay Lanf220ab22005-06-30 02:59:03 -0700103 if (error == -ENOENT)
104 printk("/initrd does not exist. Ignored.\n");
105 else
106 printk("failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 printk(KERN_NOTICE "Unmounting old root\n");
108 sys_umount("/old", MNT_DETACH);
109 printk(KERN_NOTICE "Trying to free ramdisk memory ... ");
110 if (fd < 0) {
111 error = fd;
112 } else {
113 error = sys_ioctl(fd, BLKFLSBUF, 0);
114 sys_close(fd);
115 }
116 printk(!error ? "okay\n" : "failed\n");
117 }
118}
119
Yaowei Baif057f3b2016-01-20 14:59:29 -0800120bool __init initrd_load(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
122 if (mount_initrd) {
Greg Kroah-Hartmanbdaf8522005-06-20 21:15:16 -0700123 create_dev("/dev/ram", Root_RAM0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /*
125 * Load the initrd data into /dev/ram0. Execute it as initrd
126 * unless /dev/ram0 is supposed to be our actual root device,
127 * in that case the ram disk is just set up here, and gets
128 * mounted in the normal path.
129 */
130 if (rd_load_image("/initrd.image") && ROOT_DEV != Root_RAM0) {
131 sys_unlink("/initrd.image");
132 handle_initrd();
Yaowei Baif057f3b2016-01-20 14:59:29 -0800133 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135 }
136 sys_unlink("/initrd.image");
Yaowei Baif057f3b2016-01-20 14:59:29 -0800137 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}