Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Al Viro | f7f4f4d | 2013-12-10 16:54:28 -0500 | [diff] [blame] | 7 | #include <uapi/linux/cramfs_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/initrd.h> |
| 9 | #include <linux/string.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
| 12 | #include "do_mounts.h" |
Phillip Lougher | b8fed87 | 2009-01-05 08:46:28 +0000 | [diff] [blame] | 13 | #include "../fs/squashfs/squashfs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 15 | #include <linux/decompress/generic.h> |
| 16 | |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | static int __init prompt_ramdisk(char *str) |
| 19 | { |
Christoph Hellwig | c837699 | 2020-06-04 10:23:14 +0200 | [diff] [blame] | 20 | pr_warn("ignoring the deprecated prompt_ramdisk= option\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 34 | static int __init crd_load(int in_fd, int out_fd, decompress_fn deco); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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: |
H. Peter Anvin | b172fd8 | 2009-01-04 14:42:52 -0800 | [diff] [blame] | 43 | * minix |
| 44 | * ext2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | * romfs |
| 46 | * cramfs |
Phillip Lougher | b8fed87 | 2009-01-05 08:46:28 +0000 | [diff] [blame] | 47 | * squashfs |
H. Peter Anvin | b172fd8 | 2009-01-04 14:42:52 -0800 | [diff] [blame] | 48 | * gzip |
P J P | 1bf49dd | 2013-11-12 15:11:44 -0800 | [diff] [blame] | 49 | * bzip2 |
| 50 | * lzma |
| 51 | * xz |
| 52 | * lzo |
| 53 | * lz4 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | */ |
H. Peter Anvin | b172fd8 | 2009-01-04 14:42:52 -0800 | [diff] [blame] | 55 | static int __init |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 56 | identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | { |
| 58 | const int size = 512; |
| 59 | struct minix_super_block *minixsb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | struct romfs_super_block *romfsb; |
| 61 | struct cramfs_super *cramfsb; |
Phillip Lougher | b8fed87 | 2009-01-05 08:46:28 +0000 | [diff] [blame] | 62 | struct squashfs_super_block *squashfsb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | int nblocks = -1; |
| 64 | unsigned char *buf; |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 65 | const char *compress_name; |
Al Viro | 39429c5 | 2012-03-23 16:36:45 -0400 | [diff] [blame] | 66 | unsigned long n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | buf = kmalloc(size, GFP_KERNEL); |
Stephen Hemminger | c80544d | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 69 | if (!buf) |
Davidlohr Bueso | ea611b26 | 2011-03-22 16:34:49 -0700 | [diff] [blame] | 70 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | minixsb = (struct minix_super_block *) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | romfsb = (struct romfs_super_block *) buf; |
| 74 | cramfsb = (struct cramfs_super *) buf; |
Phillip Lougher | b8fed87 | 2009-01-05 08:46:28 +0000 | [diff] [blame] | 75 | squashfsb = (struct squashfs_super_block *) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | memset(buf, 0xe5, size); |
| 77 | |
| 78 | /* |
H. Peter Anvin | b172fd8 | 2009-01-04 14:42:52 -0800 | [diff] [blame] | 79 | * Read block 0 to test for compressed kernel |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | */ |
Dominik Brodowski | 76847e4 | 2018-03-13 21:51:17 +0100 | [diff] [blame] | 81 | ksys_lseek(fd, start_block * BLOCK_SIZE, 0); |
Dominik Brodowski | 3ce4a7b | 2018-03-13 21:56:26 +0100 | [diff] [blame] | 82 | ksys_read(fd, buf, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 84 | *decompressor = decompress_method(buf, size, &compress_name); |
H. Peter Anvin | 23a22d5 | 2009-01-12 14:24:04 -0800 | [diff] [blame] | 85 | if (compress_name) { |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 86 | printk(KERN_NOTICE "RAMDISK: %s image found at block %d\n", |
| 87 | compress_name, start_block); |
H. Peter Anvin | 23a22d5 | 2009-01-12 14:24:04 -0800 | [diff] [blame] | 88 | if (!*decompressor) |
H. Peter Anvin | 73310a1 | 2009-01-14 11:28:35 -0800 | [diff] [blame] | 89 | printk(KERN_EMERG |
| 90 | "RAMDISK: %s decompressor not configured!\n", |
H. Peter Anvin | 23a22d5 | 2009-01-12 14:24:04 -0800 | [diff] [blame] | 91 | compress_name); |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 92 | nblocks = 0; |
| 93 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Phillip Lougher | b8fed87 | 2009-01-05 08:46:28 +0000 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | /* |
Neil Armstrong | f919b92 | 2011-11-02 13:37:47 -0700 | [diff] [blame] | 125 | * Read 512 bytes further to check if cramfs is padded |
| 126 | */ |
Dominik Brodowski | 76847e4 | 2018-03-13 21:51:17 +0100 | [diff] [blame] | 127 | ksys_lseek(fd, start_block * BLOCK_SIZE + 0x200, 0); |
Dominik Brodowski | 3ce4a7b | 2018-03-13 21:56:26 +0100 | [diff] [blame] | 128 | ksys_read(fd, buf, size); |
Neil Armstrong | f919b92 | 2011-11-02 13:37:47 -0700 | [diff] [blame] | 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 | |
| 138 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | * Read block 1 to test for minix and ext2 superblock |
| 140 | */ |
Dominik Brodowski | 76847e4 | 2018-03-13 21:51:17 +0100 | [diff] [blame] | 141 | ksys_lseek(fd, (start_block+1) * BLOCK_SIZE, 0); |
Dominik Brodowski | 3ce4a7b | 2018-03-13 21:56:26 +0100 | [diff] [blame] | 142 | ksys_read(fd, buf, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Al Viro | 39429c5 | 2012-03-23 16:36:45 -0400 | [diff] [blame] | 155 | n = ext2_image_size(buf); |
| 156 | if (n) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | printk(KERN_NOTICE |
| 158 | "RAMDISK: ext2 filesystem found at block %d\n", |
| 159 | start_block); |
Al Viro | 39429c5 | 2012-03-23 16:36:45 -0400 | [diff] [blame] | 160 | nblocks = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
H. Peter Anvin | b172fd8 | 2009-01-04 14:42:52 -0800 | [diff] [blame] | 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | done: |
Dominik Brodowski | 76847e4 | 2018-03-13 21:51:17 +0100 | [diff] [blame] | 169 | ksys_lseek(fd, start_block * BLOCK_SIZE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Christoph Hellwig | c837699 | 2020-06-04 10:23:14 +0200 | [diff] [blame] | 179 | int nblocks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | char *buf = NULL; |
| 181 | unsigned short rotate = 0; |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 182 | decompress_fn decompressor = NULL; |
Stephen Rothwell | bc58450 | 2012-03-15 18:19:08 +0000 | [diff] [blame] | 183 | #if !defined(CONFIG_S390) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | char rotator[4] = { '|' , '/' , '-' , '\\' }; |
| 185 | #endif |
| 186 | |
Dominik Brodowski | bae217e | 2018-03-11 11:34:56 +0100 | [diff] [blame] | 187 | out_fd = ksys_open("/dev/ram", O_RDWR, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | if (out_fd < 0) |
| 189 | goto out; |
| 190 | |
Dominik Brodowski | bae217e | 2018-03-11 11:34:56 +0100 | [diff] [blame] | 191 | in_fd = ksys_open(from, O_RDONLY, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | if (in_fd < 0) |
| 193 | goto noclose_input; |
| 194 | |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 195 | nblocks = identify_ramdisk_image(in_fd, rd_image_start, &decompressor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | if (nblocks < 0) |
| 197 | goto done; |
| 198 | |
| 199 | if (nblocks == 0) { |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 200 | if (crd_load(in_fd, out_fd, decompressor) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | goto successful_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | */ |
Dominik Brodowski | cbb60b9 | 2018-03-13 21:43:59 +0100 | [diff] [blame] | 209 | if (ksys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
H. Peter Anvin | b172fd8 | 2009-01-04 14:42:52 -0800 | [diff] [blame] | 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | /* |
| 221 | * OK, time to copy in the data |
| 222 | */ |
Dominik Brodowski | cbb60b9 | 2018-03-13 21:43:59 +0100 | [diff] [blame] | 223 | if (ksys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Harvey Harrison | d613c3e | 2008-04-28 14:13:14 -0700 | [diff] [blame] | 237 | if (!buf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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" : ""); |
Christoph Hellwig | c837699 | 2020-06-04 10:23:14 +0200 | [diff] [blame] | 244 | for (i = 0; i < nblocks; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | if (i && (i % devblocks == 0)) { |
Christoph Hellwig | c837699 | 2020-06-04 10:23:14 +0200 | [diff] [blame] | 246 | pr_cont("done disk #1.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | rotate = 0; |
Dominik Brodowski | 2ca2a09d6 | 2018-03-11 11:34:55 +0100 | [diff] [blame] | 248 | if (ksys_close(in_fd)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | printk("Error closing the disk.\n"); |
| 250 | goto noclose_input; |
| 251 | } |
Christoph Hellwig | c837699 | 2020-06-04 10:23:14 +0200 | [diff] [blame] | 252 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
Dominik Brodowski | 3ce4a7b | 2018-03-13 21:56:26 +0100 | [diff] [blame] | 254 | ksys_read(in_fd, buf, BLOCK_SIZE); |
Dominik Brodowski | e7a3e8b | 2018-03-11 11:34:41 +0100 | [diff] [blame] | 255 | ksys_write(out_fd, buf, BLOCK_SIZE); |
Stephen Rothwell | bc58450 | 2012-03-15 18:19:08 +0000 | [diff] [blame] | 256 | #if !defined(CONFIG_S390) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | if (!(i % 16)) { |
Nicolas Schichan | 18594e9 | 2016-11-24 13:38:04 +0100 | [diff] [blame] | 258 | pr_cont("%c\b", rotator[rotate & 0x3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | rotate++; |
| 260 | } |
| 261 | #endif |
| 262 | } |
Aaro Koskinen | 1a6a05a | 2018-04-10 16:34:34 -0700 | [diff] [blame] | 263 | pr_cont("done.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
| 265 | successful_load: |
| 266 | res = 1; |
| 267 | done: |
Dominik Brodowski | 2ca2a09d6 | 2018-03-11 11:34:55 +0100 | [diff] [blame] | 268 | ksys_close(in_fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | noclose_input: |
Dominik Brodowski | 2ca2a09d6 | 2018-03-11 11:34:55 +0100 | [diff] [blame] | 270 | ksys_close(out_fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | out: |
| 272 | kfree(buf); |
Dominik Brodowski | 0f32ab8 | 2018-03-11 11:34:47 +0100 | [diff] [blame] | 273 | ksys_unlink("/dev/ram"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | return res; |
| 275 | } |
| 276 | |
| 277 | int __init rd_load_disk(int n) |
| 278 | { |
Greg Kroah-Hartman | bdaf852 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 279 | create_dev("/dev/root", ROOT_DEV); |
| 280 | create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | return rd_load_image("/dev/root"); |
| 282 | } |
| 283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | static int exit_code; |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 285 | static int decompress_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | static int crd_infd, crd_outfd; |
| 287 | |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 288 | static long __init compr_fill(void *buf, unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | { |
Dominik Brodowski | 3ce4a7b | 2018-03-13 21:56:26 +0100 | [diff] [blame] | 290 | long r = ksys_read(crd_infd, buf, len); |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 298 | static long __init compr_flush(void *window, unsigned long outcnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
Dominik Brodowski | e7a3e8b | 2018-03-11 11:34:41 +0100 | [diff] [blame] | 300 | long written = ksys_write(crd_outfd, window, outcnt); |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 301 | if (written != outcnt) { |
| 302 | if (decompress_error == 0) |
| 303 | printk(KERN_ERR |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 304 | "RAMDISK: incomplete write (%ld != %ld)\n", |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 305 | written, outcnt); |
| 306 | decompress_error = 1; |
| 307 | return -1; |
| 308 | } |
| 309 | return outcnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static void __init error(char *x) |
| 313 | { |
| 314 | printk(KERN_ERR "%s\n", x); |
| 315 | exit_code = 1; |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 316 | decompress_error = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 319 | static int __init crd_load(int in_fd, int out_fd, decompress_fn deco) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | { |
| 321 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | crd_infd = in_fd; |
| 323 | crd_outfd = out_fd; |
P J P | df3ef3a | 2013-11-12 15:10:20 -0800 | [diff] [blame] | 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 | |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 331 | result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error); |
| 332 | if (decompress_error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | result = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | return result; |
| 335 | } |