Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | #include <linux/init.h> |
e7cb072e | 3 | #include <linux/async.h> |
66ac1a4d | 4 | #include <linux/export.h> |
1da177e4 LT |
5 | #include <linux/fs.h> |
6 | #include <linux/slab.h> | |
7 | #include <linux/types.h> | |
8 | #include <linux/fcntl.h> | |
9 | #include <linux/delay.h> | |
10 | #include <linux/string.h> | |
df52092f | 11 | #include <linux/dirent.h> |
1da177e4 | 12 | #include <linux/syscalls.h> |
889d51a1 | 13 | #include <linux/utime.h> |
08865514 | 14 | #include <linux/file.h> |
f3296f80 | 15 | #include <linux/kstrtox.h> |
899ee4af | 16 | #include <linux/memblock.h> |
dd23e809 | 17 | #include <linux/mm.h> |
b2a74d5f | 18 | #include <linux/namei.h> |
8fb9f73e | 19 | #include <linux/init_syscalls.h> |
b234ed6d | 20 | #include <linux/umh.h> |
2fea0c26 | 21 | #include <linux/security.h> |
1da177e4 | 22 | |
386dc41c | 23 | #include "do_mounts.h" |
5f469c4f | 24 | #include "initramfs_internal.h" |
386dc41c | 25 | |
800c24dc DD |
26 | static __initdata bool csum_present; |
27 | static __initdata u32 io_csum; | |
28 | ||
29 | static ssize_t __init xwrite(struct file *file, const unsigned char *p, | |
30 | size_t count, loff_t *pos) | |
38747439 YL |
31 | { |
32 | ssize_t out = 0; | |
33 | ||
34 | /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */ | |
35 | while (count) { | |
bf6419e4 | 36 | ssize_t rv = kernel_write(file, p, count, pos); |
38747439 YL |
37 | |
38 | if (rv < 0) { | |
39 | if (rv == -EINTR || rv == -EAGAIN) | |
40 | continue; | |
41 | return out ? out : rv; | |
42 | } else if (rv == 0) | |
43 | break; | |
44 | ||
800c24dc DD |
45 | if (csum_present) { |
46 | ssize_t i; | |
47 | ||
48 | for (i = 0; i < rv; i++) | |
49 | io_csum += p[i]; | |
50 | } | |
51 | ||
38747439 YL |
52 | p += rv; |
53 | out += rv; | |
54 | count -= rv; | |
55 | } | |
56 | ||
57 | return out; | |
58 | } | |
59 | ||
1da177e4 LT |
60 | static __initdata char *message; |
61 | static void __init error(char *x) | |
62 | { | |
63 | if (!message) | |
64 | message = x; | |
65 | } | |
66 | ||
735faf92 | 67 | #define panic_show_mem(fmt, ...) \ |
527ed4f7 | 68 | ({ show_mem(); panic(fmt, ##__VA_ARGS__); }) |
dd23e809 | 69 | |
1da177e4 LT |
70 | /* link hash */ |
71 | ||
6a050da4 MH |
72 | #define N_ALIGN(len) ((((len) + 1) & ~3) + 2) |
73 | ||
1da177e4 LT |
74 | static __initdata struct hash { |
75 | int ino, minor, major; | |
685dd2d5 | 76 | umode_t mode; |
1da177e4 | 77 | struct hash *next; |
6a050da4 | 78 | char name[N_ALIGN(PATH_MAX)]; |
1da177e4 | 79 | } *head[32]; |
225034cd | 80 | static __initdata bool hardlink_seen; |
1da177e4 LT |
81 | |
82 | static inline int hash(int major, int minor, int ino) | |
83 | { | |
84 | unsigned long tmp = ino + minor + (major << 3); | |
85 | tmp += tmp >> 5; | |
86 | return tmp & 31; | |
87 | } | |
88 | ||
2139a7fb | 89 | static char __init *find_link(int major, int minor, int ino, |
685dd2d5 | 90 | umode_t mode, char *name) |
1da177e4 LT |
91 | { |
92 | struct hash **p, *q; | |
93 | for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) { | |
94 | if ((*p)->ino != ino) | |
95 | continue; | |
96 | if ((*p)->minor != minor) | |
97 | continue; | |
98 | if ((*p)->major != major) | |
99 | continue; | |
2139a7fb PA |
100 | if (((*p)->mode ^ mode) & S_IFMT) |
101 | continue; | |
1da177e4 LT |
102 | return (*p)->name; |
103 | } | |
3265e66b | 104 | q = kmalloc(sizeof(struct hash), GFP_KERNEL); |
1da177e4 | 105 | if (!q) |
dd23e809 | 106 | panic_show_mem("can't allocate link hash entry"); |
1da177e4 | 107 | q->major = major; |
2139a7fb PA |
108 | q->minor = minor; |
109 | q->ino = ino; | |
110 | q->mode = mode; | |
6a050da4 | 111 | strcpy(q->name, name); |
1da177e4 LT |
112 | q->next = NULL; |
113 | *p = q; | |
225034cd | 114 | hardlink_seen = true; |
1da177e4 LT |
115 | return NULL; |
116 | } | |
117 | ||
118 | static void __init free_hash(void) | |
119 | { | |
120 | struct hash **p, *q; | |
225034cd | 121 | for (p = head; hardlink_seen && p < head + 32; p++) { |
1da177e4 LT |
122 | while (*p) { |
123 | q = *p; | |
124 | *p = q->next; | |
3265e66b | 125 | kfree(q); |
1da177e4 LT |
126 | } |
127 | } | |
225034cd | 128 | hardlink_seen = false; |
1da177e4 LT |
129 | } |
130 | ||
1274aea1 DD |
131 | #ifdef CONFIG_INITRAMFS_PRESERVE_MTIME |
132 | static void __init do_utime(char *filename, time64_t mtime) | |
889d51a1 | 133 | { |
1274aea1 DD |
134 | struct timespec64 t[2] = { { .tv_sec = mtime }, { .tv_sec = mtime } }; |
135 | init_utimes(filename, t); | |
136 | } | |
889d51a1 | 137 | |
1274aea1 DD |
138 | static void __init do_utime_path(const struct path *path, time64_t mtime) |
139 | { | |
140 | struct timespec64 t[2] = { { .tv_sec = mtime }, { .tv_sec = mtime } }; | |
141 | vfs_utimes(path, t); | |
889d51a1 NL |
142 | } |
143 | ||
144 | static __initdata LIST_HEAD(dir_list); | |
145 | struct dir_entry { | |
146 | struct list_head list; | |
e35c4c64 | 147 | time64_t mtime; |
fcb7aedd | 148 | char name[]; |
889d51a1 NL |
149 | }; |
150 | ||
43094e10 | 151 | static void __init dir_add(const char *name, size_t nlen, time64_t mtime) |
889d51a1 | 152 | { |
fcb7aedd DD |
153 | struct dir_entry *de; |
154 | ||
155 | de = kmalloc(sizeof(struct dir_entry) + nlen, GFP_KERNEL); | |
889d51a1 | 156 | if (!de) |
dd23e809 | 157 | panic_show_mem("can't allocate dir_entry buffer"); |
889d51a1 | 158 | INIT_LIST_HEAD(&de->list); |
fcb7aedd | 159 | strscpy(de->name, name, nlen); |
889d51a1 NL |
160 | de->mtime = mtime; |
161 | list_add(&de->list, &dir_list); | |
162 | } | |
163 | ||
164 | static void __init dir_utime(void) | |
165 | { | |
166 | struct dir_entry *de, *tmp; | |
167 | list_for_each_entry_safe(de, tmp, &dir_list, list) { | |
168 | list_del(&de->list); | |
169 | do_utime(de->name, de->mtime); | |
889d51a1 NL |
170 | kfree(de); |
171 | } | |
172 | } | |
1274aea1 DD |
173 | #else |
174 | static void __init do_utime(char *filename, time64_t mtime) {} | |
175 | static void __init do_utime_path(const struct path *path, time64_t mtime) {} | |
43094e10 | 176 | static void __init dir_add(const char *name, size_t nlen, time64_t mtime) {} |
1274aea1 DD |
177 | static void __init dir_utime(void) {} |
178 | #endif | |
889d51a1 | 179 | |
e35c4c64 | 180 | static __initdata time64_t mtime; |
889d51a1 | 181 | |
1da177e4 LT |
182 | /* cpio header parsing */ |
183 | ||
184 | static __initdata unsigned long ino, major, minor, nlink; | |
685dd2d5 | 185 | static __initdata umode_t mode; |
1da177e4 LT |
186 | static __initdata unsigned long body_len, name_len; |
187 | static __initdata uid_t uid; | |
188 | static __initdata gid_t gid; | |
189 | static __initdata unsigned rdev; | |
800c24dc | 190 | static __initdata u32 hdr_csum; |
1da177e4 LT |
191 | |
192 | static void __init parse_header(char *s) | |
193 | { | |
800c24dc | 194 | unsigned long parsed[13]; |
1da177e4 LT |
195 | int i; |
196 | ||
a8a3bc2e DD |
197 | for (i = 0, s += 6; i < 13; i++, s += 8) |
198 | parsed[i] = simple_strntoul(s, NULL, 16, 8); | |
199 | ||
1da177e4 LT |
200 | ino = parsed[0]; |
201 | mode = parsed[1]; | |
202 | uid = parsed[2]; | |
203 | gid = parsed[3]; | |
204 | nlink = parsed[4]; | |
e35c4c64 | 205 | mtime = parsed[5]; /* breaks in y2106 */ |
1da177e4 LT |
206 | body_len = parsed[6]; |
207 | major = parsed[7]; | |
208 | minor = parsed[8]; | |
209 | rdev = new_encode_dev(MKDEV(parsed[9], parsed[10])); | |
210 | name_len = parsed[11]; | |
800c24dc | 211 | hdr_csum = parsed[12]; |
1da177e4 LT |
212 | } |
213 | ||
214 | /* FSM */ | |
215 | ||
216 | static __initdata enum state { | |
217 | Start, | |
218 | Collect, | |
219 | GotHeader, | |
220 | SkipIt, | |
221 | GotName, | |
222 | CopyFile, | |
223 | GotSymlink, | |
224 | Reset | |
225 | } state, next_state; | |
226 | ||
227 | static __initdata char *victim; | |
c34d85ac | 228 | static unsigned long byte_count __initdata; |
1da177e4 LT |
229 | static __initdata loff_t this_header, next_header; |
230 | ||
b0a5ab93 | 231 | static inline void __init eat(unsigned n) |
1da177e4 LT |
232 | { |
233 | victim += n; | |
234 | this_header += n; | |
c34d85ac | 235 | byte_count -= n; |
1da177e4 LT |
236 | } |
237 | ||
1da177e4 | 238 | static __initdata char *collected; |
d97b07c5 | 239 | static long remains __initdata; |
1da177e4 LT |
240 | static __initdata char *collect; |
241 | ||
242 | static void __init read_into(char *buf, unsigned size, enum state next) | |
243 | { | |
c34d85ac | 244 | if (byte_count >= size) { |
1da177e4 LT |
245 | collected = victim; |
246 | eat(size); | |
247 | state = next; | |
248 | } else { | |
249 | collect = collected = buf; | |
250 | remains = size; | |
251 | next_state = next; | |
252 | state = Collect; | |
253 | } | |
254 | } | |
255 | ||
256 | static __initdata char *header_buf, *symlink_buf, *name_buf; | |
257 | ||
258 | static int __init do_start(void) | |
259 | { | |
5f469c4f | 260 | read_into(header_buf, CPIO_HDRLEN, GotHeader); |
1da177e4 LT |
261 | return 0; |
262 | } | |
263 | ||
264 | static int __init do_collect(void) | |
265 | { | |
d97b07c5 | 266 | unsigned long n = remains; |
c34d85ac MR |
267 | if (byte_count < n) |
268 | n = byte_count; | |
1da177e4 LT |
269 | memcpy(collect, victim, n); |
270 | eat(n); | |
271 | collect += n; | |
272 | if ((remains -= n) != 0) | |
273 | return 1; | |
274 | state = next_state; | |
275 | return 0; | |
276 | } | |
277 | ||
278 | static int __init do_header(void) | |
279 | { | |
800c24dc DD |
280 | if (!memcmp(collected, "070701", 6)) { |
281 | csum_present = false; | |
282 | } else if (!memcmp(collected, "070702", 6)) { | |
283 | csum_present = true; | |
284 | } else { | |
da028e4c DD |
285 | if (memcmp(collected, "070707", 6) == 0) |
286 | error("incorrect cpio method used: use -H newc option"); | |
287 | else | |
288 | error("no cpio magic"); | |
1da177e4 LT |
289 | return 1; |
290 | } | |
291 | parse_header(collected); | |
292 | next_header = this_header + N_ALIGN(name_len) + body_len; | |
293 | next_header = (next_header + 3) & ~3; | |
1da177e4 LT |
294 | state = SkipIt; |
295 | if (name_len <= 0 || name_len > PATH_MAX) | |
296 | return 0; | |
297 | if (S_ISLNK(mode)) { | |
298 | if (body_len > PATH_MAX) | |
299 | return 0; | |
300 | collect = collected = symlink_buf; | |
301 | remains = N_ALIGN(name_len) + body_len; | |
302 | next_state = GotSymlink; | |
303 | state = Collect; | |
304 | return 0; | |
305 | } | |
306 | if (S_ISREG(mode) || !body_len) | |
307 | read_into(name_buf, N_ALIGN(name_len), GotName); | |
308 | return 0; | |
309 | } | |
310 | ||
311 | static int __init do_skip(void) | |
312 | { | |
c34d85ac MR |
313 | if (this_header + byte_count < next_header) { |
314 | eat(byte_count); | |
1da177e4 LT |
315 | return 1; |
316 | } else { | |
317 | eat(next_header - this_header); | |
318 | state = next_state; | |
319 | return 0; | |
320 | } | |
321 | } | |
322 | ||
323 | static int __init do_reset(void) | |
324 | { | |
c34d85ac | 325 | while (byte_count && *victim == '\0') |
1da177e4 | 326 | eat(1); |
c34d85ac | 327 | if (byte_count && (this_header & 3)) |
1da177e4 LT |
328 | error("broken padding"); |
329 | return 1; | |
330 | } | |
331 | ||
c34d85ac | 332 | static void __init clean_path(char *path, umode_t fmode) |
2139a7fb | 333 | { |
046aa126 | 334 | struct kstat st; |
2139a7fb | 335 | |
7b81ce7c | 336 | if (!init_stat(path, &st, AT_SYMLINK_NOFOLLOW) && |
716308a5 | 337 | (st.mode ^ fmode) & S_IFMT) { |
046aa126 | 338 | if (S_ISDIR(st.mode)) |
20cce026 | 339 | init_rmdir(path); |
2139a7fb | 340 | else |
8fb9f73e | 341 | init_unlink(path); |
2139a7fb PA |
342 | } |
343 | } | |
344 | ||
7c0950d4 LZ |
345 | static int __init maybe_link(void) |
346 | { | |
347 | if (nlink >= 2) { | |
348 | char *old = find_link(major, minor, ino, mode, collected); | |
349 | if (old) { | |
350 | clean_path(collected, 0); | |
812931d6 | 351 | return (init_link(old, collected) < 0) ? -1 : 1; |
7c0950d4 LZ |
352 | } |
353 | } | |
354 | return 0; | |
355 | } | |
356 | ||
bf6419e4 CH |
357 | static __initdata struct file *wfile; |
358 | static __initdata loff_t wfile_pos; | |
1da177e4 LT |
359 | |
360 | static int __init do_name(void) | |
361 | { | |
362 | state = SkipIt; | |
363 | next_state = Reset; | |
e017671f DD |
364 | |
365 | /* name_len > 0 && name_len <= PATH_MAX checked in do_header */ | |
366 | if (collected[name_len - 1] != '\0') { | |
367 | pr_err("initramfs name without nulterm: %.*s\n", | |
368 | (int)name_len, collected); | |
369 | error("malformed archive"); | |
370 | return 1; | |
371 | } | |
372 | ||
1da177e4 LT |
373 | if (strcmp(collected, "TRAILER!!!") == 0) { |
374 | free_hash(); | |
375 | return 0; | |
376 | } | |
2139a7fb | 377 | clean_path(collected, mode); |
1da177e4 | 378 | if (S_ISREG(mode)) { |
2139a7fb PA |
379 | int ml = maybe_link(); |
380 | if (ml >= 0) { | |
8434f9aa | 381 | int openflags = O_WRONLY|O_CREAT|O_LARGEFILE; |
2139a7fb PA |
382 | if (ml != 1) |
383 | openflags |= O_TRUNC; | |
bf6419e4 CH |
384 | wfile = filp_open(collected, openflags, mode); |
385 | if (IS_ERR(wfile)) | |
386 | return 0; | |
387 | wfile_pos = 0; | |
800c24dc | 388 | io_csum = 0; |
bf6419e4 CH |
389 | |
390 | vfs_fchown(wfile, uid, gid); | |
391 | vfs_fchmod(wfile, mode); | |
392 | if (body_len) | |
393 | vfs_truncate(&wfile->f_path, body_len); | |
bf6419e4 | 394 | state = CopyFile; |
1da177e4 LT |
395 | } |
396 | } else if (S_ISDIR(mode)) { | |
83ff98c3 | 397 | init_mkdir(collected, mode); |
b873498f | 398 | init_chown(collected, uid, gid, 0); |
1097742e | 399 | init_chmod(collected, mode); |
43094e10 | 400 | dir_add(collected, name_len, mtime); |
1da177e4 LT |
401 | } else if (S_ISBLK(mode) || S_ISCHR(mode) || |
402 | S_ISFIFO(mode) || S_ISSOCK(mode)) { | |
403 | if (maybe_link() == 0) { | |
5fee64fc | 404 | init_mknod(collected, mode, rdev); |
b873498f | 405 | init_chown(collected, uid, gid, 0); |
1097742e | 406 | init_chmod(collected, mode); |
889d51a1 | 407 | do_utime(collected, mtime); |
1da177e4 LT |
408 | } |
409 | } | |
410 | return 0; | |
411 | } | |
412 | ||
413 | static int __init do_copy(void) | |
414 | { | |
c34d85ac | 415 | if (byte_count >= body_len) { |
bf6419e4 | 416 | if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len) |
9687fd91 | 417 | error("write error"); |
38b08223 | 418 | |
1274aea1 | 419 | do_utime_path(&wfile->f_path, mtime); |
bf6419e4 | 420 | fput(wfile); |
800c24dc DD |
421 | if (csum_present && io_csum != hdr_csum) |
422 | error("bad data checksum"); | |
1da177e4 LT |
423 | eat(body_len); |
424 | state = SkipIt; | |
425 | return 0; | |
426 | } else { | |
bf6419e4 | 427 | if (xwrite(wfile, victim, byte_count, &wfile_pos) != byte_count) |
9687fd91 | 428 | error("write error"); |
c34d85ac MR |
429 | body_len -= byte_count; |
430 | eat(byte_count); | |
1da177e4 LT |
431 | return 1; |
432 | } | |
433 | } | |
434 | ||
435 | static int __init do_symlink(void) | |
436 | { | |
e017671f DD |
437 | if (collected[name_len - 1] != '\0') { |
438 | pr_err("initramfs symlink without nulterm: %.*s\n", | |
439 | (int)name_len, collected); | |
440 | error("malformed archive"); | |
441 | return 1; | |
442 | } | |
1da177e4 | 443 | collected[N_ALIGN(name_len) + body_len] = '\0'; |
2139a7fb | 444 | clean_path(collected, 0); |
cd3acb6a | 445 | init_symlink(collected + N_ALIGN(name_len), collected); |
b873498f | 446 | init_chown(collected, uid, gid, AT_SYMLINK_NOFOLLOW); |
889d51a1 | 447 | do_utime(collected, mtime); |
1da177e4 LT |
448 | state = SkipIt; |
449 | next_state = Reset; | |
450 | return 0; | |
451 | } | |
452 | ||
453 | static __initdata int (*actions[])(void) = { | |
454 | [Start] = do_start, | |
455 | [Collect] = do_collect, | |
456 | [GotHeader] = do_header, | |
457 | [SkipIt] = do_skip, | |
458 | [GotName] = do_name, | |
459 | [CopyFile] = do_copy, | |
460 | [GotSymlink] = do_symlink, | |
461 | [Reset] = do_reset, | |
462 | }; | |
463 | ||
d97b07c5 | 464 | static long __init write_buffer(char *buf, unsigned long len) |
1da177e4 | 465 | { |
c34d85ac | 466 | byte_count = len; |
1da177e4 LT |
467 | victim = buf; |
468 | ||
469 | while (!actions[state]()) | |
470 | ; | |
c34d85ac | 471 | return len - byte_count; |
1da177e4 LT |
472 | } |
473 | ||
d97b07c5 | 474 | static long __init flush_buffer(void *bufv, unsigned long len) |
1da177e4 | 475 | { |
4197530b | 476 | char *buf = bufv; |
d97b07c5 YL |
477 | long written; |
478 | long origLen = len; | |
1da177e4 | 479 | if (message) |
30d65dbf | 480 | return -1; |
1da177e4 LT |
481 | while ((written = write_buffer(buf, len)) < len && !message) { |
482 | char c = buf[written]; | |
483 | if (c == '0') { | |
484 | buf += written; | |
485 | len -= written; | |
486 | state = Start; | |
487 | } else if (c == 0) { | |
488 | buf += written; | |
489 | len -= written; | |
490 | state = Reset; | |
491 | } else | |
e5eed351 | 492 | error("junk within compressed archive"); |
1da177e4 | 493 | } |
30d65dbf | 494 | return origLen; |
1da177e4 LT |
495 | } |
496 | ||
199cda13 | 497 | static unsigned long my_inptr __initdata; /* index of next byte to be processed in inbuf */ |
1da177e4 | 498 | |
889c92d2 | 499 | #include <linux/decompress/generic.h> |
1da177e4 | 500 | |
5f469c4f DD |
501 | /** |
502 | * unpack_to_rootfs - decompress and extract an initramfs archive | |
503 | * @buf: input initramfs archive to extract | |
504 | * @len: length of initramfs data to process | |
505 | * | |
506 | * Returns: NULL for success or an error message string | |
507 | * | |
508 | * This symbol shouldn't be used externally. It's available for unit tests. | |
509 | */ | |
510 | char * __init unpack_to_rootfs(char *buf, unsigned long len) | |
1da177e4 | 511 | { |
d97b07c5 | 512 | long written; |
889c92d2 | 513 | decompress_fn decompress; |
23a22d57 | 514 | const char *compress_name; |
7be37c94 DD |
515 | struct { |
516 | char header[CPIO_HDRLEN]; | |
517 | char symlink[PATH_MAX + N_ALIGN(PATH_MAX) + 1]; | |
518 | char name[N_ALIGN(PATH_MAX)]; | |
519 | } *bufs = kmalloc(sizeof(*bufs), GFP_KERNEL); | |
889c92d2 | 520 | |
7be37c94 | 521 | if (!bufs) |
dd23e809 | 522 | panic_show_mem("can't allocate buffers"); |
30d65dbf | 523 | |
7be37c94 DD |
524 | header_buf = bufs->header; |
525 | symlink_buf = bufs->symlink; | |
526 | name_buf = bufs->name; | |
527 | ||
1da177e4 LT |
528 | state = Start; |
529 | this_header = 0; | |
530 | message = NULL; | |
531 | while (!message && len) { | |
532 | loff_t saved_offset = this_header; | |
533 | if (*buf == '0' && !(this_header & 3)) { | |
534 | state = Start; | |
535 | written = write_buffer(buf, len); | |
536 | buf += written; | |
537 | len -= written; | |
538 | continue; | |
539 | } | |
540 | if (!*buf) { | |
541 | buf++; | |
542 | len--; | |
543 | this_header++; | |
544 | continue; | |
545 | } | |
546 | this_header = 0; | |
23a22d57 | 547 | decompress = decompress_method(buf, len, &compress_name); |
6aa7a29a | 548 | pr_debug("Detected %s compressed data\n", compress_name); |
54291362 | 549 | if (decompress) { |
d97b07c5 | 550 | int res = decompress(buf, len, NULL, flush_buffer, NULL, |
889c92d2 | 551 | &my_inptr, error); |
54291362 PL |
552 | if (res) |
553 | error("decompressor failed"); | |
554 | } else if (compress_name) { | |
7a329ed2 DD |
555 | pr_err("compression method %s not configured\n", |
556 | compress_name); | |
557 | error("decompressor failed"); | |
df37bd15 | 558 | } else |
e5eed351 | 559 | error("invalid magic at start of compressed archive"); |
1da177e4 | 560 | if (state != Reset) |
e5eed351 | 561 | error("junk at the end of compressed archive"); |
30d65dbf AK |
562 | this_header = saved_offset + my_inptr; |
563 | buf += my_inptr; | |
564 | len -= my_inptr; | |
1da177e4 | 565 | } |
889d51a1 | 566 | dir_utime(); |
225034cd DD |
567 | /* free any hardlink state collected without optional TRAILER!!! */ |
568 | free_hash(); | |
7be37c94 | 569 | kfree(bufs); |
1da177e4 LT |
570 | return message; |
571 | } | |
572 | ||
0a7b35cb MN |
573 | static int __initdata do_retain_initrd; |
574 | ||
575 | static int __init retain_initrd_param(char *str) | |
576 | { | |
577 | if (*str) | |
578 | return 0; | |
579 | do_retain_initrd = 1; | |
580 | return 1; | |
581 | } | |
582 | __setup("retain_initrd", retain_initrd_param); | |
583 | ||
d8ae8a37 CH |
584 | #ifdef CONFIG_ARCH_HAS_KEEPINITRD |
585 | static int __init keepinitrd_setup(char *__unused) | |
586 | { | |
587 | do_retain_initrd = 1; | |
588 | return 1; | |
589 | } | |
590 | __setup("keepinitrd", keepinitrd_setup); | |
591 | #endif | |
592 | ||
e7cb072e RV |
593 | static bool __initdata initramfs_async = true; |
594 | static int __init initramfs_async_setup(char *str) | |
595 | { | |
f3296f80 | 596 | return kstrtobool(str, &initramfs_async) == 0; |
e7cb072e RV |
597 | } |
598 | __setup("initramfs_async=", initramfs_async_setup); | |
599 | ||
ffe8018c HB |
600 | extern char __initramfs_start[]; |
601 | extern unsigned long __initramfs_size; | |
1da177e4 | 602 | #include <linux/initrd.h> |
9c15e852 | 603 | #include <linux/kexec.h> |
0f3d2bd5 | 604 | |
66bc1a17 | 605 | static BIN_ATTR(initrd, 0440, sysfs_bin_attr_simple_read, NULL, 0); |
2678fd2f | 606 | |
c72160fe KW |
607 | void __init reserve_initrd_mem(void) |
608 | { | |
609 | phys_addr_t start; | |
610 | unsigned long size; | |
611 | ||
612 | /* Ignore the virtul address computed during device tree parsing */ | |
613 | initrd_start = initrd_end = 0; | |
614 | ||
615 | if (!phys_initrd_size) | |
616 | return; | |
617 | /* | |
618 | * Round the memory region to page boundaries as per free_initrd_mem() | |
619 | * This allows us to detect whether the pages overlapping the initrd | |
620 | * are in use, but more importantly, reserves the entire set of pages | |
621 | * as we don't want these pages allocated for other purposes. | |
622 | */ | |
623 | start = round_down(phys_initrd_start, PAGE_SIZE); | |
624 | size = phys_initrd_size + (phys_initrd_start - start); | |
625 | size = round_up(size, PAGE_SIZE); | |
626 | ||
627 | if (!memblock_is_region_memory(start, size)) { | |
628 | pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region", | |
629 | (u64)start, size); | |
630 | goto disable; | |
631 | } | |
632 | ||
633 | if (memblock_is_region_reserved(start, size)) { | |
634 | pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region\n", | |
635 | (u64)start, size); | |
636 | goto disable; | |
637 | } | |
638 | ||
639 | memblock_reserve(start, size); | |
640 | /* Now convert initrd to virtual addresses */ | |
641 | initrd_start = (unsigned long)__va(phys_initrd_start); | |
642 | initrd_end = initrd_start + phys_initrd_size; | |
643 | initrd_below_start_ok = 1; | |
644 | ||
645 | return; | |
646 | disable: | |
647 | pr_cont(" - disabling initrd\n"); | |
648 | initrd_start = 0; | |
649 | initrd_end = 0; | |
650 | } | |
651 | ||
55d5b7dd | 652 | void __weak __init free_initrd_mem(unsigned long start, unsigned long end) |
4afd58e1 | 653 | { |
899ee4af MR |
654 | #ifdef CONFIG_ARCH_KEEP_MEMBLOCK |
655 | unsigned long aligned_start = ALIGN_DOWN(start, PAGE_SIZE); | |
656 | unsigned long aligned_end = ALIGN(end, PAGE_SIZE); | |
657 | ||
4421cca0 | 658 | memblock_free((void *)aligned_start, aligned_end - aligned_start); |
899ee4af MR |
659 | #endif |
660 | ||
f94f7434 CH |
661 | free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM, |
662 | "initrd"); | |
4afd58e1 CH |
663 | } |
664 | ||
02aff848 | 665 | #ifdef CONFIG_CRASH_RESERVE |
e99332e7 | 666 | static bool __init kexec_free_initrd(void) |
23091e28 | 667 | { |
9c15e852 HM |
668 | unsigned long crashk_start = (unsigned long)__va(crashk_res.start); |
669 | unsigned long crashk_end = (unsigned long)__va(crashk_res.end); | |
670 | ||
671 | /* | |
672 | * If the initrd region is overlapped with crashkernel reserved region, | |
673 | * free only memory that is not part of crashkernel region. | |
674 | */ | |
23091e28 CH |
675 | if (initrd_start >= crashk_end || initrd_end <= crashk_start) |
676 | return false; | |
677 | ||
678 | /* | |
679 | * Initialize initrd memory region since the kexec boot does not do. | |
680 | */ | |
681 | memset((void *)initrd_start, 0, initrd_end - initrd_start); | |
682 | if (initrd_start < crashk_start) | |
683 | free_initrd_mem(initrd_start, crashk_start); | |
684 | if (initrd_end > crashk_end) | |
685 | free_initrd_mem(crashk_end, initrd_end); | |
686 | return true; | |
0f3d2bd5 | 687 | } |
23091e28 CH |
688 | #else |
689 | static inline bool kexec_free_initrd(void) | |
690 | { | |
691 | return false; | |
692 | } | |
693 | #endif /* CONFIG_KEXEC_CORE */ | |
0f3d2bd5 | 694 | |
a841c673 | 695 | #ifdef CONFIG_BLK_DEV_RAM |
4ada1e81 | 696 | static void __init populate_initrd_image(char *err) |
7c184ecd CH |
697 | { |
698 | ssize_t written; | |
bf6419e4 CH |
699 | struct file *file; |
700 | loff_t pos = 0; | |
7c184ecd | 701 | |
7c184ecd CH |
702 | printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n", |
703 | err); | |
4624b346 | 704 | file = filp_open("/initrd.image", O_WRONLY|O_CREAT|O_LARGEFILE, 0700); |
bf6419e4 | 705 | if (IS_ERR(file)) |
7c184ecd CH |
706 | return; |
707 | ||
bf6419e4 CH |
708 | written = xwrite(file, (char *)initrd_start, initrd_end - initrd_start, |
709 | &pos); | |
7c184ecd CH |
710 | if (written != initrd_end - initrd_start) |
711 | pr_err("/initrd.image: incomplete write (%zd != %ld)\n", | |
712 | written, initrd_end - initrd_start); | |
bf6419e4 | 713 | fput(file); |
7c184ecd CH |
714 | } |
715 | #endif /* CONFIG_BLK_DEV_RAM */ | |
716 | ||
e7cb072e | 717 | static void __init do_populate_rootfs(void *unused, async_cookie_t cookie) |
1da177e4 | 718 | { |
17a9be31 | 719 | /* Load the built in initramfs */ |
ffe8018c | 720 | char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size); |
1da177e4 | 721 | if (err) |
dd23e809 | 722 | panic_show_mem("%s", err); /* Failed to decompress INTERNAL initramfs */ |
afef7889 CH |
723 | |
724 | if (!initrd_start || IS_ENABLED(CONFIG_INITRAMFS_FORCE)) | |
725 | goto done; | |
726 | ||
727 | if (IS_ENABLED(CONFIG_BLK_DEV_RAM)) | |
a1e6b6c1 | 728 | printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n"); |
afef7889 CH |
729 | else |
730 | printk(KERN_INFO "Unpacking initramfs...\n"); | |
54c7a891 | 731 | |
afef7889 CH |
732 | err = unpack_to_rootfs((char *)initrd_start, initrd_end - initrd_start); |
733 | if (err) { | |
9ab6b718 | 734 | #ifdef CONFIG_BLK_DEV_RAM |
7c184ecd | 735 | populate_initrd_image(err); |
9ab6b718 CH |
736 | #else |
737 | printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err); | |
738 | #endif | |
1da177e4 | 739 | } |
23091e28 | 740 | |
afef7889 | 741 | done: |
2fea0c26 FW |
742 | security_initramfs_populated(); |
743 | ||
23091e28 CH |
744 | /* |
745 | * If the initrd region is overlapped with crashkernel reserved region, | |
746 | * free only memory that is not part of crashkernel region. | |
747 | */ | |
2678fd2f | 748 | if (!do_retain_initrd && initrd_start && !kexec_free_initrd()) { |
23091e28 | 749 | free_initrd_mem(initrd_start, initrd_end); |
2678fd2f AG |
750 | } else if (do_retain_initrd && initrd_start) { |
751 | bin_attr_initrd.size = initrd_end - initrd_start; | |
752 | bin_attr_initrd.private = (void *)initrd_start; | |
753 | if (sysfs_create_bin_file(firmware_kobj, &bin_attr_initrd)) | |
754 | pr_err("Failed to create initrd sysfs file"); | |
755 | } | |
23091e28 CH |
756 | initrd_start = 0; |
757 | initrd_end = 0; | |
758 | ||
386dc41c | 759 | init_flush_fput(); |
e7cb072e RV |
760 | } |
761 | ||
762 | static ASYNC_DOMAIN_EXCLUSIVE(initramfs_domain); | |
763 | static async_cookie_t initramfs_cookie; | |
764 | ||
765 | void wait_for_initramfs(void) | |
766 | { | |
767 | if (!initramfs_cookie) { | |
768 | /* | |
769 | * Something before rootfs_initcall wants to access | |
770 | * the filesystem/initramfs. Probably a bug. Make a | |
771 | * note, avoid deadlocking the machine, and let the | |
772 | * caller's access fail as it used to. | |
773 | */ | |
774 | pr_warn_once("wait_for_initramfs() called before rootfs_initcalls\n"); | |
775 | return; | |
776 | } | |
777 | async_synchronize_cookie_domain(initramfs_cookie + 1, &initramfs_domain); | |
778 | } | |
779 | EXPORT_SYMBOL_GPL(wait_for_initramfs); | |
780 | ||
781 | static int __init populate_rootfs(void) | |
782 | { | |
783 | initramfs_cookie = async_schedule_domain(do_populate_rootfs, NULL, | |
784 | &initramfs_domain); | |
b234ed6d | 785 | usermodehelper_enable(); |
e7cb072e RV |
786 | if (!initramfs_async) |
787 | wait_for_initramfs(); | |
8d610dd5 | 788 | return 0; |
1da177e4 | 789 | } |
8d610dd5 | 790 | rootfs_initcall(populate_rootfs); |