initramfs.c 18 KB

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