initramfs.c 14 KB

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