xref: /linux/init/do_mounts_rd.c (revision 37a93dd5c49b5fda807fd204edf2547c3493319c)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/fs.h>
4 #include <linux/minix_fs.h>
5 #include <linux/ext2_fs.h>
6 #include <linux/romfs_fs.h>
7 #include <uapi/linux/cramfs_fs.h>
8 #include <linux/initrd.h>
9 #include <linux/string.h>
10 #include <linux/string_choices.h>
11 #include <linux/slab.h>
12 
13 #include "do_mounts.h"
14 #include "../fs/squashfs/squashfs_fs.h"
15 
16 #include <linux/decompress/generic.h>
17 
18 static struct file *in_file, *out_file;
19 static loff_t in_pos, out_pos;
20 
21 int __initdata rd_image_start;		/* starting block # of image */
22 
23 static int __init ramdisk_start_setup(char *str)
24 {
25 	pr_warn("ramdisk_start= option is deprecated and will be removed soon\n");
26 	return kstrtoint(str, 0, &rd_image_start) == 0;
27 }
28 __setup("ramdisk_start=", ramdisk_start_setup);
29 
30 static int __init crd_load(decompress_fn deco);
31 
32 /*
33  * This routine tries to find a RAM disk image to load, and returns the
34  * number of blocks to read for a non-compressed image, 0 if the image
35  * is a compressed image, and -1 if an image with the right magic
36  * numbers could not be found.
37  *
38  * We currently check for the following magic numbers:
39  *	minix
40  *	ext2
41  *	romfs
42  *	cramfs
43  *	squashfs
44  *	gzip
45  *	bzip2
46  *	lzma
47  *	xz
48  *	lzo
49  *	lz4
50  */
51 static int __init
52 identify_ramdisk_image(struct file *file, loff_t pos,
53 		decompress_fn *decompressor)
54 {
55 	const int size = 512;
56 	struct minix_super_block *minixsb;
57 	struct romfs_super_block *romfsb;
58 	struct cramfs_super *cramfsb;
59 	struct squashfs_super_block *squashfsb;
60 	int nblocks = -1;
61 	unsigned char *buf;
62 	const char *compress_name;
63 	unsigned long n;
64 	int start_block = rd_image_start;
65 
66 	buf = kmalloc(size, GFP_KERNEL);
67 	if (!buf)
68 		return -ENOMEM;
69 
70 	minixsb = (struct minix_super_block *) buf;
71 	romfsb = (struct romfs_super_block *) buf;
72 	cramfsb = (struct cramfs_super *) buf;
73 	squashfsb = (struct squashfs_super_block *) buf;
74 	memset(buf, 0xe5, size);
75 
76 	/*
77 	 * Read block 0 to test for compressed kernel
78 	 */
79 	pos = start_block * BLOCK_SIZE;
80 	kernel_read(file, buf, size, &pos);
81 
82 	*decompressor = decompress_method(buf, size, &compress_name);
83 	if (compress_name) {
84 		printk(KERN_NOTICE "RAMDISK: %s image found at block %d\n",
85 		       compress_name, start_block);
86 		if (!*decompressor)
87 			printk(KERN_EMERG
88 			       "RAMDISK: %s decompressor not configured!\n",
89 			       compress_name);
90 		nblocks = 0;
91 		goto done;
92 	}
93 
94 	/* romfs is at block zero too */
95 	if (romfsb->word0 == ROMSB_WORD0 &&
96 	    romfsb->word1 == ROMSB_WORD1) {
97 		printk(KERN_NOTICE
98 		       "RAMDISK: romfs filesystem found at block %d\n",
99 		       start_block);
100 		nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
101 		goto done;
102 	}
103 
104 	if (cramfsb->magic == CRAMFS_MAGIC) {
105 		printk(KERN_NOTICE
106 		       "RAMDISK: cramfs filesystem found at block %d\n",
107 		       start_block);
108 		nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
109 		goto done;
110 	}
111 
112 	/* squashfs is at block zero too */
113 	if (le32_to_cpu(squashfsb->s_magic) == SQUASHFS_MAGIC) {
114 		printk(KERN_NOTICE
115 		       "RAMDISK: squashfs filesystem found at block %d\n",
116 		       start_block);
117 		nblocks = (le64_to_cpu(squashfsb->bytes_used) + BLOCK_SIZE - 1)
118 			 >> BLOCK_SIZE_BITS;
119 		goto done;
120 	}
121 
122 	/*
123 	 * Read 512 bytes further to check if cramfs is padded
124 	 */
125 	pos = start_block * BLOCK_SIZE + 0x200;
126 	kernel_read(file, buf, size, &pos);
127 
128 	if (cramfsb->magic == CRAMFS_MAGIC) {
129 		printk(KERN_NOTICE
130 		       "RAMDISK: cramfs filesystem found at block %d\n",
131 		       start_block);
132 		nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
133 		goto done;
134 	}
135 
136 	/*
137 	 * Read block 1 to test for minix and ext2 superblock
138 	 */
139 	pos = (start_block + 1) * BLOCK_SIZE;
140 	kernel_read(file, buf, size, &pos);
141 
142 	/* Try minix */
143 	if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
144 	    minixsb->s_magic == MINIX_SUPER_MAGIC2) {
145 		printk(KERN_NOTICE
146 		       "RAMDISK: Minix filesystem found at block %d\n",
147 		       start_block);
148 		nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
149 		goto done;
150 	}
151 
152 	/* Try ext2 */
153 	n = ext2_image_size(buf);
154 	if (n) {
155 		printk(KERN_NOTICE
156 		       "RAMDISK: ext2 filesystem found at block %d\n",
157 		       start_block);
158 		nblocks = n;
159 		goto done;
160 	}
161 
162 	printk(KERN_NOTICE
163 	       "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
164 	       start_block);
165 
166 done:
167 	kfree(buf);
168 	return nblocks;
169 }
170 
171 static unsigned long nr_blocks(struct file *file)
172 {
173 	struct inode *inode = file->f_mapping->host;
174 
175 	if (!S_ISBLK(inode->i_mode))
176 		return 0;
177 	return i_size_read(inode) >> 10;
178 }
179 
180 int __init rd_load_image(void)
181 {
182 	int res = 0;
183 	unsigned long rd_blocks, devblocks, nr_disks;
184 	int nblocks, i;
185 	char *buf = NULL;
186 	unsigned short rotate = 0;
187 	decompress_fn decompressor = NULL;
188 	char rotator[4] = { '|' , '/' , '-' , '\\' };
189 
190 	out_file = filp_open("/dev/ram", O_RDWR, 0);
191 	if (IS_ERR(out_file))
192 		goto out;
193 
194 	in_file = filp_open("/initrd.image", O_RDONLY, 0);
195 	if (IS_ERR(in_file))
196 		goto noclose_input;
197 
198 	in_pos = rd_image_start * BLOCK_SIZE;
199 	nblocks = identify_ramdisk_image(in_file, in_pos, &decompressor);
200 	if (nblocks < 0)
201 		goto done;
202 
203 	if (nblocks == 0) {
204 		if (crd_load(decompressor) == 0)
205 			goto successful_load;
206 		goto done;
207 	}
208 
209 	/*
210 	 * NOTE NOTE: nblocks is not actually blocks but
211 	 * the number of kibibytes of data to load into a ramdisk.
212 	 */
213 	rd_blocks = nr_blocks(out_file);
214 	if (nblocks > rd_blocks) {
215 		printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
216 		       nblocks, rd_blocks);
217 		goto done;
218 	}
219 
220 	/*
221 	 * OK, time to copy in the data
222 	 */
223 	devblocks = nblocks;
224 
225 	if (devblocks == 0) {
226 		printk(KERN_ERR "RAMDISK: could not determine device size\n");
227 		goto done;
228 	}
229 
230 	buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
231 	if (!buf) {
232 		printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
233 		goto done;
234 	}
235 
236 	nr_disks = (nblocks - 1) / devblocks + 1;
237 	pr_notice("RAMDISK: Loading %dKiB [%ld disk%s] into ram disk... ",
238 		  nblocks, nr_disks, str_plural(nr_disks));
239 	for (i = 0; i < nblocks; i++) {
240 		if (i && (i % devblocks == 0)) {
241 			pr_cont("done disk #1.\n");
242 			rotate = 0;
243 			fput(in_file);
244 			break;
245 		}
246 		kernel_read(in_file, buf, BLOCK_SIZE, &in_pos);
247 		kernel_write(out_file, buf, BLOCK_SIZE, &out_pos);
248 		if (!IS_ENABLED(CONFIG_S390) && !(i % 16)) {
249 			pr_cont("%c\b", rotator[rotate & 0x3]);
250 			rotate++;
251 		}
252 	}
253 	pr_cont("done.\n");
254 
255 successful_load:
256 	res = 1;
257 done:
258 	fput(in_file);
259 noclose_input:
260 	fput(out_file);
261 out:
262 	kfree(buf);
263 	init_unlink("/dev/ram");
264 	return res;
265 }
266 
267 static int exit_code;
268 static int decompress_error;
269 
270 static long __init compr_fill(void *buf, unsigned long len)
271 {
272 	long r = kernel_read(in_file, buf, len, &in_pos);
273 	if (r < 0)
274 		printk(KERN_ERR "RAMDISK: error while reading compressed data");
275 	else if (r == 0)
276 		printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
277 	return r;
278 }
279 
280 static long __init compr_flush(void *window, unsigned long outcnt)
281 {
282 	long written = kernel_write(out_file, window, outcnt, &out_pos);
283 	if (written != outcnt) {
284 		if (decompress_error == 0)
285 			printk(KERN_ERR
286 			       "RAMDISK: incomplete write (%ld != %ld)\n",
287 			       written, outcnt);
288 		decompress_error = 1;
289 		return -1;
290 	}
291 	return outcnt;
292 }
293 
294 static void __init error(char *x)
295 {
296 	printk(KERN_ERR "%s\n", x);
297 	exit_code = 1;
298 	decompress_error = 1;
299 }
300 
301 static int __init crd_load(decompress_fn deco)
302 {
303 	int result;
304 
305 	if (!deco) {
306 		pr_emerg("Invalid ramdisk decompression routine.  "
307 			 "Select appropriate config option.\n");
308 		panic("Could not decompress initial ramdisk image.");
309 	}
310 
311 	result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
312 	if (decompress_error)
313 		result = 1;
314 	return result;
315 }
316