initramfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/init.h>
  3. #include <linux/fs.h>
  4. #include <linux/slab.h>
  5. #include <linux/types.h>
  6. #include <linux/fcntl.h>
  7. #include <linux/delay.h>
  8. #include <linux/string.h>
  9. #include <linux/dirent.h>
  10. #include <linux/syscalls.h>
  11. #include <linux/utime.h>
  12. #include <linux/file.h>
  13. #include <linux/memblock.h>
  14. #include <linux/namei.h>
  15. #include <linux/init_syscalls.h>
  16. static ssize_t __init xwrite(struct file *file, const char *p, size_t count,
  17. loff_t *pos)
  18. {
  19. ssize_t out = 0;
  20. /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
  21. while (count) {
  22. ssize_t rv = kernel_write(file, p, count, pos);
  23. if (rv < 0) {
  24. if (rv == -EINTR || rv == -EAGAIN)
  25. continue;
  26. return out ? out : rv;
  27. } else if (rv == 0)
  28. break;
  29. p += rv;
  30. out += rv;
  31. count -= rv;
  32. }
  33. return out;
  34. }
  35. static __initdata char *message;
  36. static void __init error(char *x)
  37. {
  38. if (!message)
  39. message = x;
  40. }
  41. /* link hash */
  42. #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
  43. static __initdata struct hash {
  44. int ino, minor, major;
  45. umode_t mode;
  46. struct hash *next;
  47. char name[N_ALIGN(PATH_MAX)];
  48. } *head[32];
  49. static inline int hash(int major, int minor, int ino)
  50. {
  51. unsigned long tmp = ino + minor + (major << 3);
  52. tmp += tmp >> 5;
  53. return tmp & 31;
  54. }
  55. static char __init *find_link(int major, int minor, int ino,
  56. umode_t mode, char *name)
  57. {
  58. struct hash **p, *q;
  59. for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
  60. if ((*p)->ino != ino)
  61. continue;
  62. if ((*p)->minor != minor)
  63. continue;
  64. if ((*p)->major != major)
  65. continue;
  66. if (((*p)->mode ^ mode) & S_IFMT)
  67. continue;
  68. return (*p)->name;
  69. }
  70. q = kmalloc(sizeof(struct hash), GFP_KERNEL);
  71. if (!q)
  72. panic("can't allocate link hash entry");
  73. q->major = major;
  74. q->minor = minor;
  75. q->ino = ino;
  76. q->mode = mode;
  77. strcpy(q->name, name);
  78. q->next = NULL;
  79. *p = q;
  80. return NULL;
  81. }
  82. static void __init free_hash(void)
  83. {
  84. struct hash **p, *q;
  85. for (p = head; p < head + 32; p++) {
  86. while (*p) {
  87. q = *p;
  88. *p = q->next;
  89. kfree(q);
  90. }
  91. }
  92. }
  93. static long __init do_utime(char *filename, time64_t mtime)
  94. {
  95. struct timespec64 t[2];
  96. t[0].tv_sec = mtime;
  97. t[0].tv_nsec = 0;
  98. t[1].tv_sec = mtime;
  99. t[1].tv_nsec = 0;
  100. return init_utimes(filename, t);
  101. }
  102. static __initdata LIST_HEAD(dir_list);
  103. struct dir_entry {
  104. struct list_head list;
  105. char *name;
  106. time64_t mtime;
  107. };
  108. static void __init dir_add(const char *name, time64_t mtime)
  109. {
  110. struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
  111. if (!de)
  112. panic("can't allocate dir_entry buffer");
  113. INIT_LIST_HEAD(&de->list);
  114. de->name = kstrdup(name, GFP_KERNEL);
  115. de->mtime = mtime;
  116. list_add(&de->list, &dir_list);
  117. }
  118. static void __init dir_utime(void)
  119. {
  120. struct dir_entry *de, *tmp;
  121. list_for_each_entry_safe(de, tmp, &dir_list, list) {
  122. list_del(&de->list);
  123. do_utime(de->name, de->mtime);
  124. kfree(de->name);
  125. kfree(de);
  126. }
  127. }
  128. static __initdata time64_t mtime;
  129. /* cpio header parsing */
  130. static __initdata unsigned long ino, major, minor, nlink;
  131. static __initdata umode_t mode;
  132. static __initdata unsigned long body_len, name_len;
  133. static __initdata uid_t uid;
  134. static __initdata gid_t gid;
  135. static __initdata unsigned rdev;
  136. static void __init parse_header(char *s)
  137. {
  138. unsigned long parsed[12];
  139. char buf[9];
  140. int i;
  141. buf[8] = '\0';
  142. for (i = 0, s += 6; i < 12; i++, s += 8) {
  143. memcpy(buf, s, 8);
  144. parsed[i] = simple_strtoul(buf, NULL, 16);
  145. }
  146. ino = parsed[0];
  147. mode = parsed[1];
  148. uid = parsed[2];
  149. gid = parsed[3];
  150. nlink = parsed[4];
  151. mtime = parsed[5]; /* breaks in y2106 */
  152. body_len = parsed[6];
  153. major = parsed[7];
  154. minor = parsed[8];
  155. rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
  156. name_len = parsed[11];
  157. }
  158. /* FSM */
  159. static __initdata enum state {
  160. Start,
  161. Collect,
  162. GotHeader,
  163. SkipIt,
  164. GotName,
  165. CopyFile,
  166. GotSymlink,
  167. Reset
  168. } state, next_state;
  169. static __initdata char *victim;
  170. static unsigned long byte_count __initdata;
  171. static __initdata loff_t this_header, next_header;
  172. static inline void __init eat(unsigned n)
  173. {
  174. victim += n;
  175. this_header += n;
  176. byte_count -= n;
  177. }
  178. static __initdata char *collected;
  179. static long remains __initdata;
  180. static __initdata char *collect;
  181. static void __init read_into(char *buf, unsigned size, enum state next)
  182. {
  183. if (byte_count >= size) {
  184. collected = victim;
  185. eat(size);
  186. state = next;
  187. } else {
  188. collect = collected = buf;
  189. remains = size;
  190. next_state = next;
  191. state = Collect;
  192. }
  193. }
  194. static __initdata char *header_buf, *symlink_buf, *name_buf;
  195. static int __init do_start(void)
  196. {
  197. read_into(header_buf, 110, GotHeader);
  198. return 0;
  199. }
  200. static int __init do_collect(void)
  201. {
  202. unsigned long n = remains;
  203. if (byte_count < n)
  204. n = byte_count;
  205. memcpy(collect, victim, n);
  206. eat(n);
  207. collect += n;
  208. if ((remains -= n) != 0)
  209. return 1;
  210. state = next_state;
  211. return 0;
  212. }
  213. static int __init do_header(void)
  214. {
  215. if (memcmp(collected, "070707", 6)==0) {
  216. error("incorrect cpio method used: use -H newc option");
  217. return 1;
  218. }
  219. if (memcmp(collected, "070701", 6)) {
  220. error("no cpio magic");
  221. return 1;
  222. }
  223. parse_header(collected);
  224. next_header = this_header + N_ALIGN(name_len) + body_len;
  225. next_header = (next_header + 3) & ~3;
  226. state = SkipIt;
  227. if (name_len <= 0 || name_len > PATH_MAX)
  228. return 0;
  229. if (S_ISLNK(mode)) {
  230. if (body_len > PATH_MAX)
  231. return 0;
  232. collect = collected = symlink_buf;
  233. remains = N_ALIGN(name_len) + body_len;
  234. next_state = GotSymlink;
  235. state = Collect;
  236. return 0;
  237. }
  238. if (S_ISREG(mode) || !body_len)
  239. read_into(name_buf, N_ALIGN(name_len), GotName);
  240. return 0;
  241. }
  242. static int __init do_skip(void)
  243. {
  244. if (this_header + byte_count < next_header) {
  245. eat(byte_count);
  246. return 1;
  247. } else {
  248. eat(next_header - this_header);
  249. state = next_state;
  250. return 0;
  251. }
  252. }
  253. static int __init do_reset(void)
  254. {
  255. while (byte_count && *victim == '\0')
  256. eat(1);
  257. if (byte_count && (this_header & 3))
  258. error("broken padding");
  259. return 1;
  260. }
  261. static void __init clean_path(char *path, umode_t fmode)
  262. {
  263. struct kstat st;
  264. if (!init_stat(path, &st, AT_SYMLINK_NOFOLLOW) &&
  265. (st.mode ^ fmode) & S_IFMT) {
  266. if (S_ISDIR(st.mode))
  267. init_rmdir(path);
  268. else
  269. init_unlink(path);
  270. }
  271. }
  272. static int __init maybe_link(void)
  273. {
  274. if (nlink >= 2) {
  275. char *old = find_link(major, minor, ino, mode, collected);
  276. if (old) {
  277. clean_path(collected, 0);
  278. return (init_link(old, collected) < 0) ? -1 : 1;
  279. }
  280. }
  281. return 0;
  282. }
  283. static __initdata struct file *wfile;
  284. static __initdata loff_t wfile_pos;
  285. static int __init do_name(void)
  286. {
  287. state = SkipIt;
  288. next_state = Reset;
  289. if (strcmp(collected, "TRAILER!!!") == 0) {
  290. free_hash();
  291. return 0;
  292. }
  293. clean_path(collected, mode);
  294. if (S_ISREG(mode)) {
  295. int ml = maybe_link();
  296. if (ml >= 0) {
  297. int openflags = O_WRONLY|O_CREAT;
  298. if (ml != 1)
  299. openflags |= O_TRUNC;
  300. wfile = filp_open(collected, openflags, mode);
  301. if (IS_ERR(wfile))
  302. return 0;
  303. wfile_pos = 0;
  304. vfs_fchown(wfile, uid, gid);
  305. vfs_fchmod(wfile, mode);
  306. if (body_len)
  307. vfs_truncate(&wfile->f_path, body_len);
  308. state = CopyFile;
  309. }
  310. } else if (S_ISDIR(mode)) {
  311. init_mkdir(collected, mode);
  312. init_chown(collected, uid, gid, 0);
  313. init_chmod(collected, mode);
  314. dir_add(collected, mtime);
  315. } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
  316. S_ISFIFO(mode) || S_ISSOCK(mode)) {
  317. if (maybe_link() == 0) {
  318. init_mknod(collected, mode, rdev);
  319. init_chown(collected, uid, gid, 0);
  320. init_chmod(collected, mode);
  321. do_utime(collected, mtime);
  322. }
  323. }
  324. return 0;
  325. }
  326. static int __init do_copy(void)
  327. {
  328. if (byte_count >= body_len) {
  329. struct timespec64 t[2] = { };
  330. if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len)
  331. error("write error");
  332. t[0].tv_sec = mtime;
  333. t[1].tv_sec = mtime;
  334. vfs_utimes(&wfile->f_path, t);
  335. fput(wfile);
  336. eat(body_len);
  337. state = SkipIt;
  338. return 0;
  339. } else {
  340. if (xwrite(wfile, victim, byte_count, &wfile_pos) != byte_count)
  341. error("write error");
  342. body_len -= byte_count;
  343. eat(byte_count);
  344. return 1;
  345. }
  346. }
  347. static int __init do_symlink(void)
  348. {
  349. collected[N_ALIGN(name_len) + body_len] = '\0';
  350. clean_path(collected, 0);
  351. init_symlink(collected + N_ALIGN(name_len), collected);
  352. init_chown(collected, uid, gid, AT_SYMLINK_NOFOLLOW);
  353. do_utime(collected, mtime);
  354. state = SkipIt;
  355. next_state = Reset;
  356. return 0;
  357. }
  358. static __initdata int (*actions[])(void) = {
  359. [Start] = do_start,
  360. [Collect] = do_collect,
  361. [GotHeader] = do_header,
  362. [SkipIt] = do_skip,
  363. [GotName] = do_name,
  364. [CopyFile] = do_copy,
  365. [GotSymlink] = do_symlink,
  366. [Reset] = do_reset,
  367. };
  368. static long __init write_buffer(char *buf, unsigned long len)
  369. {
  370. byte_count = len;
  371. victim = buf;
  372. while (!actions[state]())
  373. ;
  374. return len - byte_count;
  375. }
  376. static long __init flush_buffer(void *bufv, unsigned long len)
  377. {
  378. char *buf = (char *) bufv;
  379. long written;
  380. long origLen = len;
  381. if (message)
  382. return -1;
  383. while ((written = write_buffer(buf, len)) < len && !message) {
  384. char c = buf[written];
  385. if (c == '0') {
  386. buf += written;
  387. len -= written;
  388. state = Start;
  389. } else if (c == 0) {
  390. buf += written;
  391. len -= written;
  392. state = Reset;
  393. } else
  394. error("junk within compressed archive");
  395. }
  396. return origLen;
  397. }
  398. static unsigned long my_inptr; /* index of next byte to be processed in inbuf */
  399. #include <linux/decompress/generic.h>
  400. static char * __init unpack_to_rootfs(char *buf, unsigned long len)
  401. {
  402. long written;
  403. decompress_fn decompress;
  404. const char *compress_name;
  405. static __initdata char msg_buf[64];
  406. header_buf = kmalloc(110, GFP_KERNEL);
  407. symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
  408. name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
  409. if (!header_buf || !symlink_buf || !name_buf)
  410. panic("can't allocate buffers");
  411. state = Start;
  412. this_header = 0;
  413. message = NULL;
  414. while (!message && len) {
  415. loff_t saved_offset = this_header;
  416. if (*buf == '0' && !(this_header & 3)) {
  417. state = Start;
  418. written = write_buffer(buf, len);
  419. buf += written;
  420. len -= written;
  421. continue;
  422. }
  423. if (!*buf) {
  424. buf++;
  425. len--;
  426. this_header++;
  427. continue;
  428. }
  429. this_header = 0;
  430. decompress = decompress_method(buf, len, &compress_name);
  431. pr_debug("Detected %s compressed data\n", compress_name);
  432. if (decompress) {
  433. int res = decompress(buf, len, NULL, flush_buffer, NULL,
  434. &my_inptr, error);
  435. if (res)
  436. error("decompressor failed");
  437. } else if (compress_name) {
  438. if (!message) {
  439. snprintf(msg_buf, sizeof msg_buf,
  440. "compression method %s not configured",
  441. compress_name);
  442. message = msg_buf;
  443. }
  444. } else
  445. error("invalid magic at start of compressed archive");
  446. if (state != Reset)
  447. error("junk at the end of compressed archive");
  448. this_header = saved_offset + my_inptr;
  449. buf += my_inptr;
  450. len -= my_inptr;
  451. }
  452. dir_utime();
  453. kfree(name_buf);
  454. kfree(symlink_buf);
  455. kfree(header_buf);
  456. return message;
  457. }
  458. static int __initdata do_retain_initrd;
  459. static int __init retain_initrd_param(char *str)
  460. {
  461. if (*str)
  462. return 0;
  463. do_retain_initrd = 1;
  464. return 1;
  465. }
  466. __setup("retain_initrd", retain_initrd_param);
  467. #ifdef CONFIG_ARCH_HAS_KEEPINITRD
  468. static int __init keepinitrd_setup(char *__unused)
  469. {
  470. do_retain_initrd = 1;
  471. return 1;
  472. }
  473. __setup("keepinitrd", keepinitrd_setup);
  474. #endif
  475. extern char __initramfs_start[];
  476. extern unsigned long __initramfs_size;
  477. #include <linux/initrd.h>
  478. #include <linux/kexec.h>
  479. void __weak __init free_initrd_mem(unsigned long start, unsigned long end)
  480. {
  481. #ifdef CONFIG_ARCH_KEEP_MEMBLOCK
  482. unsigned long aligned_start = ALIGN_DOWN(start, PAGE_SIZE);
  483. unsigned long aligned_end = ALIGN(end, PAGE_SIZE);
  484. memblock_free(__pa(aligned_start), aligned_end - aligned_start);
  485. #endif
  486. free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM,
  487. "initrd");
  488. }
  489. #ifdef CONFIG_KEXEC_CORE
  490. static bool __init kexec_free_initrd(void)
  491. {
  492. unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
  493. unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
  494. /*
  495. * If the initrd region is overlapped with crashkernel reserved region,
  496. * free only memory that is not part of crashkernel region.
  497. */
  498. if (initrd_start >= crashk_end || initrd_end <= crashk_start)
  499. return false;
  500. /*
  501. * Initialize initrd memory region since the kexec boot does not do.
  502. */
  503. memset((void *)initrd_start, 0, initrd_end - initrd_start);
  504. if (initrd_start < crashk_start)
  505. free_initrd_mem(initrd_start, crashk_start);
  506. if (initrd_end > crashk_end)
  507. free_initrd_mem(crashk_end, initrd_end);
  508. return true;
  509. }
  510. #else
  511. static inline bool kexec_free_initrd(void)
  512. {
  513. return false;
  514. }
  515. #endif /* CONFIG_KEXEC_CORE */
  516. #ifdef CONFIG_BLK_DEV_RAM
  517. static void __init populate_initrd_image(char *err)
  518. {
  519. ssize_t written;
  520. struct file *file;
  521. loff_t pos = 0;
  522. unpack_to_rootfs(__initramfs_start, __initramfs_size);
  523. printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n",
  524. err);
  525. file = filp_open("/initrd.image", O_WRONLY | O_CREAT, 0700);
  526. if (IS_ERR(file))
  527. return;
  528. written = xwrite(file, (char *)initrd_start, initrd_end - initrd_start,
  529. &pos);
  530. if (written != initrd_end - initrd_start)
  531. pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
  532. written, initrd_end - initrd_start);
  533. fput(file);
  534. }
  535. #endif /* CONFIG_BLK_DEV_RAM */
  536. static int __init populate_rootfs(void)
  537. {
  538. /* Load the built in initramfs */
  539. char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
  540. if (err)
  541. panic("%s", err); /* Failed to decompress INTERNAL initramfs */
  542. if (!initrd_start || IS_ENABLED(CONFIG_INITRAMFS_FORCE))
  543. goto done;
  544. if (IS_ENABLED(CONFIG_BLK_DEV_RAM))
  545. printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
  546. else
  547. printk(KERN_INFO "Unpacking initramfs...\n");
  548. err = unpack_to_rootfs((char *)initrd_start, initrd_end - initrd_start);
  549. if (err) {
  550. #ifdef CONFIG_BLK_DEV_RAM
  551. populate_initrd_image(err);
  552. #else
  553. printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
  554. #endif
  555. }
  556. done:
  557. /*
  558. * If the initrd region is overlapped with crashkernel reserved region,
  559. * free only memory that is not part of crashkernel region.
  560. */
  561. if (!do_retain_initrd && initrd_start && !kexec_free_initrd())
  562. free_initrd_mem(initrd_start, initrd_end);
  563. initrd_start = 0;
  564. initrd_end = 0;
  565. flush_delayed_fput();
  566. return 0;
  567. }
  568. rootfs_initcall(populate_rootfs);