blob: 0a5c3ebee61da7be964d367a96028b14db8b6d6d [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 -070018static int __init prompt_ramdisk(char *str)
19{
Christoph Hellwigc8376992020-06-04 10:23:14 +020020 pr_warn("ignoring the deprecated prompt_ramdisk= option\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 return 1;
22}
23__setup("prompt_ramdisk=", prompt_ramdisk);
24
25int __initdata rd_image_start; /* starting block # of image */
26
27static 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
Alain Knaff30d65db2009-01-04 22:46:17 +010034static int __init crd_load(int in_fd, int out_fd, decompress_fn deco);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
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:
H. Peter Anvinb172fd82009-01-04 14:42:52 -080043 * minix
44 * ext2
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 * romfs
46 * cramfs
Phillip Lougherb8fed872009-01-05 08:46:28 +000047 * squashfs
H. Peter Anvinb172fd82009-01-04 14:42:52 -080048 * gzip
P J P1bf49dd2013-11-12 15:11:44 -080049 * bzip2
50 * lzma
51 * xz
52 * lzo
53 * lz4
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 */
H. Peter Anvinb172fd82009-01-04 14:42:52 -080055static int __init
Alain Knaff30d65db2009-01-04 22:46:17 +010056identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
58 const int size = 512;
59 struct minix_super_block *minixsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 struct romfs_super_block *romfsb;
61 struct cramfs_super *cramfsb;
Phillip Lougherb8fed872009-01-05 08:46:28 +000062 struct squashfs_super_block *squashfsb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 int nblocks = -1;
64 unsigned char *buf;
H. Peter Anvin889c92d2009-01-08 15:14:17 -080065 const char *compress_name;
Al Viro39429c52012-03-23 16:36:45 -040066 unsigned long n;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 buf = kmalloc(size, GFP_KERNEL);
Stephen Hemmingerc80544d2007-10-18 03:07:05 -070069 if (!buf)
Davidlohr Buesoea611b262011-03-22 16:34:49 -070070 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72 minixsb = (struct minix_super_block *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 romfsb = (struct romfs_super_block *) buf;
74 cramfsb = (struct cramfs_super *) buf;
Phillip Lougherb8fed872009-01-05 08:46:28 +000075 squashfsb = (struct squashfs_super_block *) buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 memset(buf, 0xe5, size);
77
78 /*
H. Peter Anvinb172fd82009-01-04 14:42:52 -080079 * Read block 0 to test for compressed kernel
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 */
Dominik Brodowski76847e42018-03-13 21:51:17 +010081 ksys_lseek(fd, start_block * BLOCK_SIZE, 0);
Dominik Brodowski3ce4a7b2018-03-13 21:56:26 +010082 ksys_read(fd, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
H. Peter Anvin889c92d2009-01-08 15:14:17 -080084 *decompressor = decompress_method(buf, size, &compress_name);
H. Peter Anvin23a22d52009-01-12 14:24:04 -080085 if (compress_name) {
H. Peter Anvin889c92d2009-01-08 15:14:17 -080086 printk(KERN_NOTICE "RAMDISK: %s image found at block %d\n",
87 compress_name, start_block);
H. Peter Anvin23a22d52009-01-12 14:24:04 -080088 if (!*decompressor)
H. Peter Anvin73310a12009-01-14 11:28:35 -080089 printk(KERN_EMERG
90 "RAMDISK: %s decompressor not configured!\n",
H. Peter Anvin23a22d52009-01-12 14:24:04 -080091 compress_name);
H. Peter Anvin889c92d2009-01-08 15:14:17 -080092 nblocks = 0;
93 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
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
Phillip Lougherb8fed872009-01-05 08:46:28 +0000114 /* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /*
Neil Armstrongf919b922011-11-02 13:37:47 -0700125 * Read 512 bytes further to check if cramfs is padded
126 */
Dominik Brodowski76847e42018-03-13 21:51:17 +0100127 ksys_lseek(fd, start_block * BLOCK_SIZE + 0x200, 0);
Dominik Brodowski3ce4a7b2018-03-13 21:56:26 +0100128 ksys_read(fd, buf, size);
Neil Armstrongf919b922011-11-02 13:37:47 -0700129
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
138 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 * Read block 1 to test for minix and ext2 superblock
140 */
Dominik Brodowski76847e42018-03-13 21:51:17 +0100141 ksys_lseek(fd, (start_block+1) * BLOCK_SIZE, 0);
Dominik Brodowski3ce4a7b2018-03-13 21:56:26 +0100142 ksys_read(fd, buf, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
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 */
Al Viro39429c52012-03-23 16:36:45 -0400155 n = ext2_image_size(buf);
156 if (n) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 printk(KERN_NOTICE
158 "RAMDISK: ext2 filesystem found at block %d\n",
159 start_block);
Al Viro39429c52012-03-23 16:36:45 -0400160 nblocks = n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 goto done;
162 }
163
164 printk(KERN_NOTICE
165 "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
166 start_block);
H. Peter Anvinb172fd82009-01-04 14:42:52 -0800167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168done:
Dominik Brodowski76847e42018-03-13 21:51:17 +0100169 ksys_lseek(fd, start_block * BLOCK_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 kfree(buf);
171 return nblocks;
172}
173
174int __init rd_load_image(char *from)
175{
176 int res = 0;
177 int in_fd, out_fd;
178 unsigned long rd_blocks, devblocks;
Christoph Hellwigc8376992020-06-04 10:23:14 +0200179 int nblocks, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 char *buf = NULL;
181 unsigned short rotate = 0;
Alain Knaff30d65db2009-01-04 22:46:17 +0100182 decompress_fn decompressor = NULL;
Stephen Rothwellbc584502012-03-15 18:19:08 +0000183#if !defined(CONFIG_S390)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 char rotator[4] = { '|' , '/' , '-' , '\\' };
185#endif
186
Dominik Brodowskibae217e2018-03-11 11:34:56 +0100187 out_fd = ksys_open("/dev/ram", O_RDWR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 if (out_fd < 0)
189 goto out;
190
Dominik Brodowskibae217e2018-03-11 11:34:56 +0100191 in_fd = ksys_open(from, O_RDONLY, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (in_fd < 0)
193 goto noclose_input;
194
Alain Knaff30d65db2009-01-04 22:46:17 +0100195 nblocks = identify_ramdisk_image(in_fd, rd_image_start, &decompressor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (nblocks < 0)
197 goto done;
198
199 if (nblocks == 0) {
Alain Knaff30d65db2009-01-04 22:46:17 +0100200 if (crd_load(in_fd, out_fd, decompressor) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 goto successful_load;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 */
Dominik Brodowskicbb60b92018-03-13 21:43:59 +0100209 if (ksys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 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 }
H. Peter Anvinb172fd82009-01-04 14:42:52 -0800219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 /*
221 * OK, time to copy in the data
222 */
Dominik Brodowskicbb60b92018-03-13 21:43:59 +0100223 if (ksys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 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);
Harvey Harrisond613c3e2008-04-28 14:13:14 -0700237 if (!buf) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 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" : "");
Christoph Hellwigc8376992020-06-04 10:23:14 +0200244 for (i = 0; i < nblocks; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if (i && (i % devblocks == 0)) {
Christoph Hellwigc8376992020-06-04 10:23:14 +0200246 pr_cont("done disk #1.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 rotate = 0;
Dominik Brodowski2ca2a09d62018-03-11 11:34:55 +0100248 if (ksys_close(in_fd)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 printk("Error closing the disk.\n");
250 goto noclose_input;
251 }
Christoph Hellwigc8376992020-06-04 10:23:14 +0200252 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
Dominik Brodowski3ce4a7b2018-03-13 21:56:26 +0100254 ksys_read(in_fd, buf, BLOCK_SIZE);
Dominik Brodowskie7a3e8b2018-03-11 11:34:41 +0100255 ksys_write(out_fd, buf, BLOCK_SIZE);
Stephen Rothwellbc584502012-03-15 18:19:08 +0000256#if !defined(CONFIG_S390)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (!(i % 16)) {
Nicolas Schichan18594e92016-11-24 13:38:04 +0100258 pr_cont("%c\b", rotator[rotate & 0x3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 rotate++;
260 }
261#endif
262 }
Aaro Koskinen1a6a05a2018-04-10 16:34:34 -0700263 pr_cont("done.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265successful_load:
266 res = 1;
267done:
Dominik Brodowski2ca2a09d62018-03-11 11:34:55 +0100268 ksys_close(in_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269noclose_input:
Dominik Brodowski2ca2a09d62018-03-11 11:34:55 +0100270 ksys_close(out_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271out:
272 kfree(buf);
Dominik Brodowski0f32ab82018-03-11 11:34:47 +0100273 ksys_unlink("/dev/ram");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return res;
275}
276
277int __init rd_load_disk(int n)
278{
Greg Kroah-Hartmanbdaf8522005-06-20 21:15:16 -0700279 create_dev("/dev/root", ROOT_DEV);
280 create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return rd_load_image("/dev/root");
282}
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284static int exit_code;
Alain Knaff30d65db2009-01-04 22:46:17 +0100285static int decompress_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static int crd_infd, crd_outfd;
287
Yinghai Lud97b07c2014-08-08 14:23:14 -0700288static long __init compr_fill(void *buf, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Dominik Brodowski3ce4a7b2018-03-13 21:56:26 +0100290 long r = ksys_read(crd_infd, buf, len);
Alain Knaff30d65db2009-01-04 22:46:17 +0100291 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Yinghai Lud97b07c2014-08-08 14:23:14 -0700298static long __init compr_flush(void *window, unsigned long outcnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
Dominik Brodowskie7a3e8b2018-03-11 11:34:41 +0100300 long written = ksys_write(crd_outfd, window, outcnt);
Alain Knaff30d65db2009-01-04 22:46:17 +0100301 if (written != outcnt) {
302 if (decompress_error == 0)
303 printk(KERN_ERR
Yinghai Lud97b07c2014-08-08 14:23:14 -0700304 "RAMDISK: incomplete write (%ld != %ld)\n",
Alain Knaff30d65db2009-01-04 22:46:17 +0100305 written, outcnt);
306 decompress_error = 1;
307 return -1;
308 }
309 return outcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
312static void __init error(char *x)
313{
314 printk(KERN_ERR "%s\n", x);
315 exit_code = 1;
Alain Knaff30d65db2009-01-04 22:46:17 +0100316 decompress_error = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Alain Knaff30d65db2009-01-04 22:46:17 +0100319static int __init crd_load(int in_fd, int out_fd, decompress_fn deco)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
321 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 crd_infd = in_fd;
323 crd_outfd = out_fd;
P J Pdf3ef3a2013-11-12 15:10:20 -0800324
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
Alain Knaff30d65db2009-01-04 22:46:17 +0100331 result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error);
332 if (decompress_error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 result = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return result;
335}