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