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