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