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