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