blob: 32fb049d18f9b40f95d000eb60e70bb7a87aa65c [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#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>
Al Virof7f4f4d2013-12-10 16:54:28 -05007#include <uapi/linux/cramfs_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/initrd.h>
9#include <linux/string.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include "do_mounts.h"
Phillip Lougherb8fed872009-01-05 08:46:28 +000013#include "../fs/squashfs/squashfs_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Alain Knaff30d65db2009-01-04 22:46:17 +010015#include <linux/decompress/generic.h>
16
Alain Knaff30d65db2009-01-04 22:46:17 +010017
Linus Torvalds1da177e2005-04-16 15:20:36 -070018int __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
Alain Knaff30d65db2009-01-04 22:46:17 +010036static int __init crd_load(int in_fd, int out_fd, decompress_fn deco);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
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:
H. Peter Anvinb172fd82009-01-04 14:42:52 -080045 * minix
46 * ext2
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 * romfs
48 * cramfs
Phillip Lougherb8fed872009-01-05 08:46:28 +000049 * squashfs
H. Peter Anvinb172fd82009-01-04 14:42:52 -080050 * gzip
P J P1bf49dd2013-11-12 15:11:44 -080051 * bzip2
52 * lzma
53 * xz
54 * lzo
55 * lz4
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 */
H. Peter Anvinb172fd82009-01-04 14:42:52 -080057static int __init
Alain Knaff30d65db2009-01-04 22:46:17 +010058identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
60 const int size = 512;
61 struct minix_super_block *minixsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct romfs_super_block *romfsb;
63 struct cramfs_super *cramfsb;
Phillip Lougherb8fed872009-01-05 08:46:28 +000064 struct squashfs_super_block *squashfsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 int nblocks = -1;
66 unsigned char *buf;
H. Peter Anvin889c92d2009-01-08 15:14:17 -080067 const char *compress_name;
Al Viro39429c52012-03-23 16:36:45 -040068 unsigned long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 buf = kmalloc(size, GFP_KERNEL);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -070071 if (!buf)
Davidlohr Buesoea611b262011-03-22 16:34:49 -070072 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 minixsb = (struct minix_super_block *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 romfsb = (struct romfs_super_block *) buf;
76 cramfsb = (struct cramfs_super *) buf;
Phillip Lougherb8fed872009-01-05 08:46:28 +000077 squashfsb = (struct squashfs_super_block *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 memset(buf, 0xe5, size);
79
80 /*
H. Peter Anvinb172fd82009-01-04 14:42:52 -080081 * Read block 0 to test for compressed kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
Dominik Brodowski76847e42018-03-13 21:51:17 +010083 ksys_lseek(fd, start_block * BLOCK_SIZE, 0);
Dominik Brodowski3ce4a7b2018-03-13 21:56:26 +010084 ksys_read(fd, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
H. Peter Anvin889c92d2009-01-08 15:14:17 -080086 *decompressor = decompress_method(buf, size, &compress_name);
H. Peter Anvin23a22d52009-01-12 14:24:04 -080087 if (compress_name) {
H. Peter Anvin889c92d2009-01-08 15:14:17 -080088 printk(KERN_NOTICE "RAMDISK: %s image found at block %d\n",
89 compress_name, start_block);
H. Peter Anvin23a22d52009-01-12 14:24:04 -080090 if (!*decompressor)
H. Peter Anvin73310a12009-01-14 11:28:35 -080091 printk(KERN_EMERG
92 "RAMDISK: %s decompressor not configured!\n",
H. Peter Anvin23a22d52009-01-12 14:24:04 -080093 compress_name);
H. Peter Anvin889c92d2009-01-08 15:14:17 -080094 nblocks = 0;
95 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
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
Phillip Lougherb8fed872009-01-05 08:46:28 +0000116 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 /*
Neil Armstrongf919b922011-11-02 13:37:47 -0700127 * Read 512 bytes further to check if cramfs is padded
128 */
Dominik Brodowski76847e42018-03-13 21:51:17 +0100129 ksys_lseek(fd, start_block * BLOCK_SIZE + 0x200, 0);
Dominik Brodowski3ce4a7b2018-03-13 21:56:26 +0100130 ksys_read(fd, buf, size);
Neil Armstrongf919b922011-11-02 13:37:47 -0700131
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
140 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 * Read block 1 to test for minix and ext2 superblock
142 */
Dominik Brodowski76847e42018-03-13 21:51:17 +0100143 ksys_lseek(fd, (start_block+1) * BLOCK_SIZE, 0);
Dominik Brodowski3ce4a7b2018-03-13 21:56:26 +0100144 ksys_read(fd, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
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 */
Al Viro39429c52012-03-23 16:36:45 -0400157 n = ext2_image_size(buf);
158 if (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 printk(KERN_NOTICE
160 "RAMDISK: ext2 filesystem found at block %d\n",
161 start_block);
Al Viro39429c52012-03-23 16:36:45 -0400162 nblocks = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 goto done;
164 }
165
166 printk(KERN_NOTICE
167 "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
168 start_block);
H. Peter Anvinb172fd82009-01-04 14:42:52 -0800169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170done:
Dominik Brodowski76847e42018-03-13 21:51:17 +0100171 ksys_lseek(fd, start_block * BLOCK_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 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;
Alain Knaff30d65db2009-01-04 22:46:17 +0100184 decompress_fn decompressor = NULL;
Stephen Rothwellbc584502012-03-15 18:19:08 +0000185#if !defined(CONFIG_S390)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 char rotator[4] = { '|' , '/' , '-' , '\\' };
187#endif
188
Dominik Brodowskibae217e2018-03-11 11:34:56 +0100189 out_fd = ksys_open("/dev/ram", O_RDWR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (out_fd < 0)
191 goto out;
192
Dominik Brodowskibae217e2018-03-11 11:34:56 +0100193 in_fd = ksys_open(from, O_RDONLY, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (in_fd < 0)
195 goto noclose_input;
196
Alain Knaff30d65db2009-01-04 22:46:17 +0100197 nblocks = identify_ramdisk_image(in_fd, rd_image_start, &decompressor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (nblocks < 0)
199 goto done;
200
201 if (nblocks == 0) {
Alain Knaff30d65db2009-01-04 22:46:17 +0100202 if (crd_load(in_fd, out_fd, decompressor) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 goto successful_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 */
Dominik Brodowskicbb60b92018-03-13 21:43:59 +0100211 if (ksys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 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 }
H. Peter Anvinb172fd82009-01-04 14:42:52 -0800221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 /*
223 * OK, time to copy in the data
224 */
Dominik Brodowskicbb60b92018-03-13 21:43:59 +0100225 if (ksys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 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);
Harvey Harrisond613c3e2008-04-28 14:13:14 -0700239 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 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)) {
Aaro Koskinen1a6a05a2018-04-10 16:34:34 -0700248 pr_cont("done disk #%d.\n", disk++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 rotate = 0;
Dominik Brodowski2ca2a09d62018-03-11 11:34:55 +0100250 if (ksys_close(in_fd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 printk("Error closing the disk.\n");
252 goto noclose_input;
253 }
254 change_floppy("disk #%d", disk);
Dominik Brodowskibae217e2018-03-11 11:34:56 +0100255 in_fd = ksys_open(from, O_RDONLY, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (in_fd < 0) {
257 printk("Error opening disk.\n");
258 goto noclose_input;
259 }
260 printk("Loading disk #%d... ", disk);
261 }
Dominik Brodowski3ce4a7b2018-03-13 21:56:26 +0100262 ksys_read(in_fd, buf, BLOCK_SIZE);
Dominik Brodowskie7a3e8b2018-03-11 11:34:41 +0100263 ksys_write(out_fd, buf, BLOCK_SIZE);
Stephen Rothwellbc584502012-03-15 18:19:08 +0000264#if !defined(CONFIG_S390)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 if (!(i % 16)) {
Nicolas Schichan18594e92016-11-24 13:38:04 +0100266 pr_cont("%c\b", rotator[rotate & 0x3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 rotate++;
268 }
269#endif
270 }
Aaro Koskinen1a6a05a2018-04-10 16:34:34 -0700271 pr_cont("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273successful_load:
274 res = 1;
275done:
Dominik Brodowski2ca2a09d62018-03-11 11:34:55 +0100276 ksys_close(in_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277noclose_input:
Dominik Brodowski2ca2a09d62018-03-11 11:34:55 +0100278 ksys_close(out_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279out:
280 kfree(buf);
Dominik Brodowski0f32ab82018-03-11 11:34:47 +0100281 ksys_unlink("/dev/ram");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 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");
Greg Kroah-Hartmanbdaf8522005-06-20 21:15:16 -0700289 create_dev("/dev/root", ROOT_DEV);
290 create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return rd_load_image("/dev/root");
292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294static int exit_code;
Alain Knaff30d65db2009-01-04 22:46:17 +0100295static int decompress_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static int crd_infd, crd_outfd;
297
Yinghai Lud97b07c2014-08-08 14:23:14 -0700298static long __init compr_fill(void *buf, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Dominik Brodowski3ce4a7b2018-03-13 21:56:26 +0100300 long r = ksys_read(crd_infd, buf, len);
Alain Knaff30d65db2009-01-04 22:46:17 +0100301 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Yinghai Lud97b07c2014-08-08 14:23:14 -0700308static long __init compr_flush(void *window, unsigned long outcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Dominik Brodowskie7a3e8b2018-03-11 11:34:41 +0100310 long written = ksys_write(crd_outfd, window, outcnt);
Alain Knaff30d65db2009-01-04 22:46:17 +0100311 if (written != outcnt) {
312 if (decompress_error == 0)
313 printk(KERN_ERR
Yinghai Lud97b07c2014-08-08 14:23:14 -0700314 "RAMDISK: incomplete write (%ld != %ld)\n",
Alain Knaff30d65db2009-01-04 22:46:17 +0100315 written, outcnt);
316 decompress_error = 1;
317 return -1;
318 }
319 return outcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
322static void __init error(char *x)
323{
324 printk(KERN_ERR "%s\n", x);
325 exit_code = 1;
Alain Knaff30d65db2009-01-04 22:46:17 +0100326 decompress_error = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
Alain Knaff30d65db2009-01-04 22:46:17 +0100329static int __init crd_load(int in_fd, int out_fd, decompress_fn deco)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 crd_infd = in_fd;
333 crd_outfd = out_fd;
P J Pdf3ef3a2013-11-12 15:10:20 -0800334
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
Alain Knaff30d65db2009-01-04 22:46:17 +0100341 result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
342 if (decompress_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 result = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return result;
345}