initramfs.c 18 KB

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