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