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 | int __initdata rd_prompt = 1;/* 1 = prompt for RAM disk, 0 = don't prompt */ |
| 19 | |
| 20 | static 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 | |
| 27 | int __initdata rd_image_start; /* starting block # of image */ |
| 28 | |
| 29 | static 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 Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 36 | 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] | 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: |
H. Peter Anvin | b172fd8 | 2009-01-04 14:42:52 -0800 | [diff] [blame] | 45 | * minix |
| 46 | * ext2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | * romfs |
| 48 | * cramfs |
Phillip Lougher | b8fed87 | 2009-01-05 08:46:28 +0000 | [diff] [blame] | 49 | * squashfs |
H. Peter Anvin | b172fd8 | 2009-01-04 14:42:52 -0800 | [diff] [blame] | 50 | * gzip |
P J P | 1bf49dd | 2013-11-12 15:11:44 -0800 | [diff] [blame] | 51 | * bzip2 |
| 52 | * lzma |
| 53 | * xz |
| 54 | * lzo |
| 55 | * lz4 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | */ |
H. Peter Anvin | b172fd8 | 2009-01-04 14:42:52 -0800 | [diff] [blame] | 57 | static int __init |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 58 | identify_ramdisk_image(int fd, int start_block, decompress_fn *decompressor) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | { |
| 60 | const int size = 512; |
| 61 | struct minix_super_block *minixsb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | struct romfs_super_block *romfsb; |
| 63 | struct cramfs_super *cramfsb; |
Phillip Lougher | b8fed87 | 2009-01-05 08:46:28 +0000 | [diff] [blame] | 64 | struct squashfs_super_block *squashfsb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | int nblocks = -1; |
| 66 | unsigned char *buf; |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 67 | const char *compress_name; |
Al Viro | 39429c5 | 2012-03-23 16:36:45 -0400 | [diff] [blame] | 68 | unsigned long n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
| 70 | buf = kmalloc(size, GFP_KERNEL); |
Stephen Hemminger | c80544d | 2007-10-18 03:07:05 -0700 | [diff] [blame] | 71 | if (!buf) |
Davidlohr Bueso | ea611b26 | 2011-03-22 16:34:49 -0700 | [diff] [blame] | 72 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
| 74 | minixsb = (struct minix_super_block *) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | romfsb = (struct romfs_super_block *) buf; |
| 76 | cramfsb = (struct cramfs_super *) buf; |
Phillip Lougher | b8fed87 | 2009-01-05 08:46:28 +0000 | [diff] [blame] | 77 | squashfsb = (struct squashfs_super_block *) buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | memset(buf, 0xe5, size); |
| 79 | |
| 80 | /* |
H. Peter Anvin | b172fd8 | 2009-01-04 14:42:52 -0800 | [diff] [blame] | 81 | * Read block 0 to test for compressed kernel |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | */ |
Dominik Brodowski | 76847e4 | 2018-03-13 21:51:17 +0100 | [diff] [blame] | 83 | ksys_lseek(fd, start_block * BLOCK_SIZE, 0); |
Dominik Brodowski | 3ce4a7b | 2018-03-13 21:56:26 +0100 | [diff] [blame] | 84 | ksys_read(fd, buf, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 86 | *decompressor = decompress_method(buf, size, &compress_name); |
H. Peter Anvin | 23a22d5 | 2009-01-12 14:24:04 -0800 | [diff] [blame] | 87 | if (compress_name) { |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 88 | printk(KERN_NOTICE "RAMDISK: %s image found at block %d\n", |
| 89 | compress_name, start_block); |
H. Peter Anvin | 23a22d5 | 2009-01-12 14:24:04 -0800 | [diff] [blame] | 90 | if (!*decompressor) |
H. Peter Anvin | 73310a1 | 2009-01-14 11:28:35 -0800 | [diff] [blame] | 91 | printk(KERN_EMERG |
| 92 | "RAMDISK: %s decompressor not configured!\n", |
H. Peter Anvin | 23a22d5 | 2009-01-12 14:24:04 -0800 | [diff] [blame] | 93 | compress_name); |
H. Peter Anvin | 889c92d | 2009-01-08 15:14:17 -0800 | [diff] [blame] | 94 | nblocks = 0; |
| 95 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Phillip Lougher | b8fed87 | 2009-01-05 08:46:28 +0000 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | /* |
Neil Armstrong | f919b92 | 2011-11-02 13:37:47 -0700 | [diff] [blame] | 127 | * Read 512 bytes further to check if cramfs is padded |
| 128 | */ |
Dominik Brodowski | 76847e4 | 2018-03-13 21:51:17 +0100 | [diff] [blame] | 129 | ksys_lseek(fd, start_block * BLOCK_SIZE + 0x200, 0); |
Dominik Brodowski | 3ce4a7b | 2018-03-13 21:56:26 +0100 | [diff] [blame] | 130 | ksys_read(fd, buf, size); |
Neil Armstrong | f919b92 | 2011-11-02 13:37:47 -0700 | [diff] [blame] | 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 | |
| 140 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | * Read block 1 to test for minix and ext2 superblock |
| 142 | */ |
Dominik Brodowski | 76847e4 | 2018-03-13 21:51:17 +0100 | [diff] [blame] | 143 | ksys_lseek(fd, (start_block+1) * BLOCK_SIZE, 0); |
Dominik Brodowski | 3ce4a7b | 2018-03-13 21:56:26 +0100 | [diff] [blame] | 144 | ksys_read(fd, buf, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Al Viro | 39429c5 | 2012-03-23 16:36:45 -0400 | [diff] [blame] | 157 | n = ext2_image_size(buf); |
| 158 | if (n) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | printk(KERN_NOTICE |
| 160 | "RAMDISK: ext2 filesystem found at block %d\n", |
| 161 | start_block); |
Al Viro | 39429c5 | 2012-03-23 16:36:45 -0400 | [diff] [blame] | 162 | nblocks = n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
H. Peter Anvin | b172fd8 | 2009-01-04 14:42:52 -0800 | [diff] [blame] | 169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | done: |
Dominik Brodowski | 76847e4 | 2018-03-13 21:51:17 +0100 | [diff] [blame] | 171 | ksys_lseek(fd, start_block * BLOCK_SIZE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | kfree(buf); |
| 173 | return nblocks; |
| 174 | } |
| 175 | |
| 176 | int __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 Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 184 | decompress_fn decompressor = NULL; |
Stephen Rothwell | bc58450 | 2012-03-15 18:19:08 +0000 | [diff] [blame] | 185 | #if !defined(CONFIG_S390) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | char rotator[4] = { '|' , '/' , '-' , '\\' }; |
| 187 | #endif |
| 188 | |
Dominik Brodowski | bae217e | 2018-03-11 11:34:56 +0100 | [diff] [blame] | 189 | out_fd = ksys_open("/dev/ram", O_RDWR, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | if (out_fd < 0) |
| 191 | goto out; |
| 192 | |
Dominik Brodowski | bae217e | 2018-03-11 11:34:56 +0100 | [diff] [blame] | 193 | in_fd = ksys_open(from, O_RDONLY, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | if (in_fd < 0) |
| 195 | goto noclose_input; |
| 196 | |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 197 | nblocks = identify_ramdisk_image(in_fd, rd_image_start, &decompressor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | if (nblocks < 0) |
| 199 | goto done; |
| 200 | |
| 201 | if (nblocks == 0) { |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 202 | if (crd_load(in_fd, out_fd, decompressor) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | goto successful_load; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | */ |
Dominik Brodowski | cbb60b9 | 2018-03-13 21:43:59 +0100 | [diff] [blame] | 211 | if (ksys_ioctl(out_fd, BLKGETSIZE, (unsigned long)&rd_blocks) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
H. Peter Anvin | b172fd8 | 2009-01-04 14:42:52 -0800 | [diff] [blame] | 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | /* |
| 223 | * OK, time to copy in the data |
| 224 | */ |
Dominik Brodowski | cbb60b9 | 2018-03-13 21:43:59 +0100 | [diff] [blame] | 225 | if (ksys_ioctl(in_fd, BLKGETSIZE, (unsigned long)&devblocks) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Harvey Harrison | d613c3e | 2008-04-28 14:13:14 -0700 | [diff] [blame] | 239 | if (!buf) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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)) { |
Aaro Koskinen | 1a6a05a | 2018-04-10 16:34:34 -0700 | [diff] [blame] | 248 | pr_cont("done disk #%d.\n", disk++); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | rotate = 0; |
Dominik Brodowski | 2ca2a09d6 | 2018-03-11 11:34:55 +0100 | [diff] [blame] | 250 | if (ksys_close(in_fd)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | printk("Error closing the disk.\n"); |
| 252 | goto noclose_input; |
| 253 | } |
| 254 | change_floppy("disk #%d", disk); |
Dominik Brodowski | bae217e | 2018-03-11 11:34:56 +0100 | [diff] [blame] | 255 | in_fd = ksys_open(from, O_RDONLY, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | if (in_fd < 0) { |
| 257 | printk("Error opening disk.\n"); |
| 258 | goto noclose_input; |
| 259 | } |
| 260 | printk("Loading disk #%d... ", disk); |
| 261 | } |
Dominik Brodowski | 3ce4a7b | 2018-03-13 21:56:26 +0100 | [diff] [blame] | 262 | ksys_read(in_fd, buf, BLOCK_SIZE); |
Dominik Brodowski | e7a3e8b | 2018-03-11 11:34:41 +0100 | [diff] [blame] | 263 | ksys_write(out_fd, buf, BLOCK_SIZE); |
Stephen Rothwell | bc58450 | 2012-03-15 18:19:08 +0000 | [diff] [blame] | 264 | #if !defined(CONFIG_S390) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | if (!(i % 16)) { |
Nicolas Schichan | 18594e9 | 2016-11-24 13:38:04 +0100 | [diff] [blame] | 266 | pr_cont("%c\b", rotator[rotate & 0x3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | rotate++; |
| 268 | } |
| 269 | #endif |
| 270 | } |
Aaro Koskinen | 1a6a05a | 2018-04-10 16:34:34 -0700 | [diff] [blame] | 271 | pr_cont("done.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
| 273 | successful_load: |
| 274 | res = 1; |
| 275 | done: |
Dominik Brodowski | 2ca2a09d6 | 2018-03-11 11:34:55 +0100 | [diff] [blame] | 276 | ksys_close(in_fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | noclose_input: |
Dominik Brodowski | 2ca2a09d6 | 2018-03-11 11:34:55 +0100 | [diff] [blame] | 278 | ksys_close(out_fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | out: |
| 280 | kfree(buf); |
Dominik Brodowski | 0f32ab8 | 2018-03-11 11:34:47 +0100 | [diff] [blame] | 281 | ksys_unlink("/dev/ram"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | return res; |
| 283 | } |
| 284 | |
| 285 | int __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-Hartman | bdaf852 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 289 | create_dev("/dev/root", ROOT_DEV); |
| 290 | create_dev("/dev/ram", MKDEV(RAMDISK_MAJOR, n)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | return rd_load_image("/dev/root"); |
| 292 | } |
| 293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | static int exit_code; |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 295 | static int decompress_error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | static int crd_infd, crd_outfd; |
| 297 | |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 298 | static long __init compr_fill(void *buf, unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
Dominik Brodowski | 3ce4a7b | 2018-03-13 21:56:26 +0100 | [diff] [blame] | 300 | long r = ksys_read(crd_infd, buf, len); |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 308 | static long __init compr_flush(void *window, unsigned long outcnt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | { |
Dominik Brodowski | e7a3e8b | 2018-03-11 11:34:41 +0100 | [diff] [blame] | 310 | long written = ksys_write(crd_outfd, window, outcnt); |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 311 | if (written != outcnt) { |
| 312 | if (decompress_error == 0) |
| 313 | printk(KERN_ERR |
Yinghai Lu | d97b07c | 2014-08-08 14:23:14 -0700 | [diff] [blame] | 314 | "RAMDISK: incomplete write (%ld != %ld)\n", |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 315 | written, outcnt); |
| 316 | decompress_error = 1; |
| 317 | return -1; |
| 318 | } |
| 319 | return outcnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | static void __init error(char *x) |
| 323 | { |
| 324 | printk(KERN_ERR "%s\n", x); |
| 325 | exit_code = 1; |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 326 | decompress_error = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 329 | 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] | 330 | { |
| 331 | int result; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | crd_infd = in_fd; |
| 333 | crd_outfd = out_fd; |
P J P | df3ef3a | 2013-11-12 15:10:20 -0800 | [diff] [blame] | 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 | |
Alain Knaff | 30d65db | 2009-01-04 22:46:17 +0100 | [diff] [blame] | 341 | result = deco(NULL, 0, compr_fill, compr_flush, NULL, NULL, error); |
| 342 | if (decompress_error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | result = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | return result; |
| 345 | } |