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