1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/unistd.h>
3 #include <linux/kernel.h>
4 #include <linux/fs.h>
5 #include <linux/minix_fs.h>
6 #include <linux/romfs_fs.h>
7 #include <linux/initrd.h>
8 #include <linux/sched.h>
9 #include <linux/freezer.h>
10 #include <linux/kmod.h>
11 #include <uapi/linux/mount.h>
12 
13 #include "do_mounts.h"
14 
15 unsigned long initrd_start, initrd_end;
16 int initrd_below_start_ok;
17 static unsigned int real_root_dev;	/* do_proc_dointvec cannot handle kdev_t */
18 static int __initdata mount_initrd = 1;
19 
20 phys_addr_t phys_initrd_start __initdata;
21 unsigned long phys_initrd_size __initdata;
22 
23 #ifdef CONFIG_SYSCTL
24 static struct ctl_table kern_do_mounts_initrd_table[] = {
25 	{
26 		.procname       = "real-root-dev",
27 		.data           = &real_root_dev,
28 		.maxlen         = sizeof(int),
29 		.mode           = 0644,
30 		.proc_handler   = proc_dointvec,
31 	},
32 	{ }
33 };
34 
kernel_do_mounts_initrd_sysctls_init(void)35 static __init int kernel_do_mounts_initrd_sysctls_init(void)
36 {
37 	register_sysctl_init("kernel", kern_do_mounts_initrd_table);
38 	return 0;
39 }
40 late_initcall(kernel_do_mounts_initrd_sysctls_init);
41 #endif /* CONFIG_SYSCTL */
42 
no_initrd(char * str)43 static int __init no_initrd(char *str)
44 {
45 	mount_initrd = 0;
46 	return 1;
47 }
48 
49 __setup("noinitrd", no_initrd);
50 
early_initrdmem(char * p)51 static int __init early_initrdmem(char *p)
52 {
53 	phys_addr_t start;
54 	unsigned long size;
55 	char *endp;
56 
57 	start = memparse(p, &endp);
58 	if (*endp == ',') {
59 		size = memparse(endp + 1, NULL);
60 
61 		phys_initrd_start = start;
62 		phys_initrd_size = size;
63 	}
64 	return 0;
65 }
66 early_param("initrdmem", early_initrdmem);
67 
early_initrd(char * p)68 static int __init early_initrd(char *p)
69 {
70 	return early_initrdmem(p);
71 }
72 early_param("initrd", early_initrd);
73 
init_linuxrc(struct subprocess_info * info,struct cred * new)74 static int __init init_linuxrc(struct subprocess_info *info, struct cred *new)
75 {
76 	ksys_unshare(CLONE_FS | CLONE_FILES);
77 	console_on_rootfs();
78 	/* move initrd over / and chdir/chroot in initrd root */
79 	init_chdir("/root");
80 	init_mount(".", "/", NULL, MS_MOVE, NULL);
81 	init_chroot(".");
82 	ksys_setsid();
83 	return 0;
84 }
85 
handle_initrd(void)86 static void __init handle_initrd(void)
87 {
88 	struct subprocess_info *info;
89 	static char *argv[] = { "linuxrc", NULL, };
90 	extern char *envp_init[];
91 	int error;
92 
93 	pr_warn("using deprecated initrd support, will be removed in 2021.\n");
94 
95 	real_root_dev = new_encode_dev(ROOT_DEV);
96 	create_dev("/dev/root.old", Root_RAM0);
97 	/* mount initrd on rootfs' /root */
98 	mount_block_root("/dev/root.old", root_mountflags & ~MS_RDONLY);
99 	init_mkdir("/old", 0700);
100 	init_chdir("/old");
101 
102 	info = call_usermodehelper_setup("/linuxrc", argv, envp_init,
103 					 GFP_KERNEL, init_linuxrc, NULL, NULL);
104 	if (!info)
105 		return;
106 	call_usermodehelper_exec(info, UMH_WAIT_PROC|UMH_FREEZABLE);
107 
108 	/* move initrd to rootfs' /old */
109 	init_mount("..", ".", NULL, MS_MOVE, NULL);
110 	/* switch root and cwd back to / of rootfs */
111 	init_chroot("..");
112 
113 	if (new_decode_dev(real_root_dev) == Root_RAM0) {
114 		init_chdir("/old");
115 		return;
116 	}
117 
118 	init_chdir("/");
119 	ROOT_DEV = new_decode_dev(real_root_dev);
120 	mount_root();
121 
122 	printk(KERN_NOTICE "Trying to move old root to /initrd ... ");
123 	error = init_mount("/old", "/root/initrd", NULL, MS_MOVE, NULL);
124 	if (!error)
125 		printk("okay\n");
126 	else {
127 		if (error == -ENOENT)
128 			printk("/initrd does not exist. Ignored.\n");
129 		else
130 			printk("failed\n");
131 		printk(KERN_NOTICE "Unmounting old root\n");
132 		init_umount("/old", MNT_DETACH);
133 	}
134 }
135 
initrd_load(void)136 bool __init initrd_load(void)
137 {
138 	if (mount_initrd) {
139 		create_dev("/dev/ram", Root_RAM0);
140 		/*
141 		 * Load the initrd data into /dev/ram0. Execute it as initrd
142 		 * unless /dev/ram0 is supposed to be our actual root device,
143 		 * in that case the ram disk is just set up here, and gets
144 		 * mounted in the normal path.
145 		 */
146 		if (rd_load_image("/initrd.image") && ROOT_DEV != Root_RAM0) {
147 			init_unlink("/initrd.image");
148 			handle_initrd();
149 			return true;
150 		}
151 	}
152 	init_unlink("/initrd.image");
153 	return false;
154 }
155