]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - init/do_mounts_rd.c
Merge tag 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma
[thirdparty/kernel/stable.git] / init / do_mounts_rd.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1da177e4
LT
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>
f7f4f4dd 7#include <uapi/linux/cramfs_fs.h>
1da177e4
LT
8#include <linux/initrd.h>
9#include <linux/string.h>
5a0e3ad6 10#include <linux/slab.h>
1da177e4
LT
11
12#include "do_mounts.h"
b8fed87d 13#include "../fs/squashfs/squashfs_fs.h"
1da177e4 14
30d65dbf
AK
15#include <linux/decompress/generic.h>
16
30d65dbf 17
1da177e4
LT
18int __initdata rd_prompt = 1;/* 1 = prompt for RAM disk, 0 = don't prompt */
19
20static int __init prompt_ramdisk(char *str)
21{
22 rd_prompt = simple_strtol(str,NULL,0) & 1;
23 return 1;
24}
25__setup("prompt_ramdisk=", prompt_ramdisk);
26
27int __initdata rd_image_start; /* starting block # of image */
28
29static 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
30d65dbf 36static int __init crd_load(int in_fd, int out_fd, decompress_fn deco);
1da177e4
LT
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:
b172fd88
PA
45 * minix
46 * ext2
1da177e4
LT
47 * romfs
48 * cramfs
b8fed87d 49 * squashfs
b172fd88 50 * gzip
1bf49dd4 51 * bzip2
52 * lzma
53 * xz
54 * lzo
55 * lz4
1da177e4 56 */
b172fd88 57static int __init
30d65dbf 58identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
1da177e4
LT
59{
60 const int size = 512;
61 struct minix_super_block *minixsb;
1da177e4
LT
62 struct romfs_super_block *romfsb;
63 struct cramfs_super *cramfsb;
b8fed87d 64 struct squashfs_super_block *squashfsb;
1da177e4
LT
65 int nblocks = -1;
66 unsigned char *buf;
889c92d2 67 const char *compress_name;
39429c5e 68 unsigned long n;
1da177e4
LT
69
70 buf = kmalloc(size, GFP_KERNEL);
c80544dc 71 if (!buf)
ea611b26 72 return -ENOMEM;
1da177e4
LT
73
74 minixsb = (struct minix_super_block *) buf;
1da177e4
LT
75 romfsb = (struct romfs_super_block *) buf;
76 cramfsb = (struct cramfs_super *) buf;
b8fed87d 77 squashfsb = (struct squashfs_super_block *) buf;
1da177e4
LT
78 memset(buf, 0xe5, size);
79
80 /*
b172fd88 81 * Read block 0 to test for compressed kernel
1da177e4 82 */
76847e43 83 ksys_lseek(fd, start_block * BLOCK_SIZE, 0);
3ce4a7bf 84 ksys_read(fd, buf, size);
1da177e4 85
889c92d2 86 *decompressor = decompress_method(buf, size, &compress_name);
23a22d57 87 if (compress_name) {
889c92d2
PA
88 printk(KERN_NOTICE "RAMDISK: %s image found at block %d\n",
89 compress_name, start_block);
23a22d57 90 if (!*decompressor)
73310a16
PA
91 printk(KERN_EMERG
92 "RAMDISK: %s decompressor not configured!\n",
23a22d57 93 compress_name);
889c92d2
PA
94 nblocks = 0;
95 goto done;
1da177e4
LT
96 }
97
98 /* romfs is at block zero too */
99 if (romfsb->word0 == ROMSB_WORD0 &&
100 romfsb->word1 == ROMSB_WORD1) {
101 printk(KERN_NOTICE
102 "RAMDISK: romfs filesystem found at block %d\n",
103 start_block);
104 nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
105 goto done;
106 }
107
108 if (cramfsb->magic == CRAMFS_MAGIC) {
109 printk(KERN_NOTICE
110 "RAMDISK: cramfs filesystem found at block %d\n",
111 start_block);
112 nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
113 goto done;
114 }
115
b8fed87d
PL
116 /* squashfs is at block zero too */
117 if (le32_to_cpu(squashfsb->s_magic) == SQUASHFS_MAGIC) {
118 printk(KERN_NOTICE
119 "RAMDISK: squashfs filesystem found at block %d\n",
120 start_block);
121 nblocks = (le64_to_cpu(squashfsb->bytes_used) + BLOCK_SIZE - 1)
122 >> BLOCK_SIZE_BITS;
123 goto done;
124 }
125
f919b923
NA
126 /*
127 * Read 512 bytes further to check if cramfs is padded
128 */
76847e43 129 ksys_lseek(fd, start_block * BLOCK_SIZE + 0x200, 0);
3ce4a7bf 130 ksys_read(fd, buf, size);
f919b923
NA
131
132 if (cramfsb->magic == CRAMFS_MAGIC) {
133 printk(KERN_NOTICE
134 "RAMDISK: cramfs filesystem found at block %d\n",
135 start_block);
136 nblocks = (cramfsb->size + BLOCK_SIZE - 1) >> BLOCK_SIZE_BITS;
137 goto done;
138 }
139
1da177e4
LT
140 /*
141 * Read block 1 to test for minix and ext2 superblock
142 */
76847e43 143 ksys_lseek(fd, (start_block+1) * BLOCK_SIZE, 0);
3ce4a7bf 144 ksys_read(fd, buf, size);
1da177e4
LT
145
146 /* Try minix */
147 if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
148 minixsb->s_magic == MINIX_SUPER_MAGIC2) {
149 printk(KERN_NOTICE
150 "RAMDISK: Minix filesystem found at block %d\n",
151 start_block);
152 nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
153 goto done;
154 }
155
156 /* Try ext2 */
39429c5e
AV
157 n = ext2_image_size(buf);
158 if (n) {
1da177e4
LT
159 printk(KERN_NOTICE
160 "RAMDISK: ext2 filesystem found at block %d\n",
161 start_block);
39429c5e 162 nblocks = n;
1da177e4
LT
163 goto done;
164 }
165
166 printk(KERN_NOTICE
167 "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
168 start_block);
b172fd88 169
1da177e4 170done:
76847e43 171 ksys_lseek(fd, start_block * BLOCK_SIZE, 0);
1da177e4
LT
172 kfree(buf);
173 return nblocks;
174}
175
176int __init rd_load_image(char *from)
177{
178 int res = 0;
179 int in_fd, out_fd;
180 unsigned long rd_blocks, devblocks;
181 int nblocks, i, disk;
182 char *buf = NULL;
183 unsigned short rotate = 0;
30d65dbf 184 decompress_fn decompressor = NULL;
bc58450b 185#if !defined(CONFIG_S390)
1da177e4
LT
186 char rotator[4] = { '|' , '/' , '-' , '\\' };
187#endif
188
bae217ea 189 out_fd = ksys_open("/dev/ram", O_RDWR, 0);
1da177e4
LT
190 if (out_fd < 0)
191 goto out;
192
bae217ea 193 in_fd = ksys_open(from, O_RDONLY, 0);
1da177e4
LT
194 if (in_fd < 0)
195 goto noclose_input;
196
30d65dbf 197 nblocks = identify_ramdisk_image(in_fd, rd_image_start, &decompressor);
1da177e4
LT
198 if (nblocks < 0)
199 goto done;
200
201 if (nblocks == 0) {
30d65dbf 202 if (crd_load(in_fd, out_fd, decompressor) == 0)
1da177e4 203 goto successful_load;
1da177e4
LT
204 goto done;
205 }
206
207 /*
208 * NOTE NOTE: nblocks is not actually blocks but
209 * the number of kibibytes of data to load into a ramdisk.
1da177e4 210 */
cbb60b92 211 if (ksys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0)
1da177e4
LT
212 rd_blocks = 0;
213 else
214 rd_blocks >>= 1;
215
216 if (nblocks > rd_blocks) {
217 printk("RAMDISK: image too big! (%dKiB/%ldKiB)\n",
218 nblocks, rd_blocks);
219 goto done;
220 }
b172fd88 221
1da177e4
LT
222 /*
223 * OK, time to copy in the data
224 */
cbb60b92 225 if (ksys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0)
1da177e4
LT
226 devblocks = 0;
227 else
228 devblocks >>= 1;
229
230 if (strcmp(from, "/initrd.image") == 0)
231 devblocks = nblocks;
232
233 if (devblocks == 0) {
234 printk(KERN_ERR "RAMDISK: could not determine device size\n");
235 goto done;
236 }
237
238 buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
d613c3e2 239 if (!buf) {
1da177e4
LT
240 printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
241 goto done;
242 }
243
244 printk(KERN_NOTICE "RAMDISK: Loading %dKiB [%ld disk%s] into ram disk... ",
245 nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
246 for (i = 0, disk = 1; i < nblocks; i++) {
247 if (i && (i % devblocks == 0)) {
1a6a05a4 248 pr_cont("done disk #%d.\n", disk++);
1da177e4 249 rotate = 0;
2ca2a09d 250 if (ksys_close(in_fd)) {
1da177e4
LT
251 printk("Error closing the disk.\n");
252 goto noclose_input;
253 }
254 change_floppy("disk #%d", disk);
bae217ea 255 in_fd = ksys_open(from, O_RDONLY, 0);
1da177e4
LT
256 if (in_fd < 0) {
257 printk("Error opening disk.\n");
258 goto noclose_input;
259 }
260 printk("Loading disk #%d... ", disk);
261 }
3ce4a7bf 262 ksys_read(in_fd, buf, BLOCK_SIZE);
e7a3e8b2 263 ksys_write(out_fd, buf, BLOCK_SIZE);
bc58450b 264#if !defined(CONFIG_S390)
1da177e4 265 if (!(i % 16)) {
18594e9b 266 pr_cont("%c\b", rotator[rotate & 0x3]);
1da177e4
LT
267 rotate++;
268 }
269#endif
270 }
1a6a05a4 271 pr_cont("done.\n");
1da177e4
LT
272
273successful_load:
274 res = 1;
275done:
2ca2a09d 276 ksys_close(in_fd);
1da177e4 277noclose_input:
2ca2a09d 278 ksys_close(out_fd);
1da177e4
LT
279out:
280 kfree(buf);
0f32ab8c 281 ksys_unlink("/dev/ram");
1da177e4
LT
282 return res;
283}
284
285int __init rd_load_disk(int n)
286{
287 if (rd_prompt)
288 change_floppy("root floppy disk to be loaded into RAM disk");
bdaf8529
GKH
289 create_dev("/dev/root", ROOT_DEV);
290 create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n));
1da177e4
LT
291 return rd_load_image("/dev/root");
292}
293
1da177e4 294static int exit_code;
30d65dbf 295static int decompress_error;
1da177e4
LT
296static int crd_infd, crd_outfd;
297
d97b07c5 298static long __init compr_fill(void *buf, unsigned long len)
1da177e4 299{
3ce4a7bf 300 long r = ksys_read(crd_infd, buf, len);
30d65dbf
AK
301 if (r < 0)
302 printk(KERN_ERR "RAMDISK: error while reading compressed data");
303 else if (r == 0)
304 printk(KERN_ERR "RAMDISK: EOF while reading compressed data");
305 return r;
1da177e4
LT
306}
307
d97b07c5 308static long __init compr_flush(void *window, unsigned long outcnt)
1da177e4 309{
e7a3e8b2 310 long written = ksys_write(crd_outfd, window, outcnt);
30d65dbf
AK
311 if (written != outcnt) {
312 if (decompress_error == 0)
313 printk(KERN_ERR
d97b07c5 314 "RAMDISK: incomplete write (%ld != %ld)\n",
30d65dbf
AK
315 written, outcnt);
316 decompress_error = 1;
317 return -1;
318 }
319 return outcnt;
1da177e4
LT
320}
321
322static void __init error(char *x)
323{
324 printk(KERN_ERR "%s\n", x);
325 exit_code = 1;
30d65dbf 326 decompress_error = 1;
1da177e4
LT
327}
328
30d65dbf 329static int __init crd_load(int in_fd, int out_fd, decompress_fn deco)
1da177e4
LT
330{
331 int result;
1da177e4
LT
332 crd_infd = in_fd;
333 crd_outfd = out_fd;
df3ef3af 334
335 if (!deco) {
336 pr_emerg("Invalid ramdisk decompression routine. "
337 "Select appropriate config option.\n");
338 panic("Could not decompress initial ramdisk image.");
339 }
340
30d65dbf
AK
341 result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
342 if (decompress_error)
1da177e4 343 result = 1;
1da177e4
LT
344 return result;
345}