initramfs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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 int __init maybe_link(void)
  259. {
  260. if (nlink >= 2) {
  261. char *old = find_link(major, minor, ino, mode, collected);
  262. if (old)
  263. return (ksys_link(old, collected) < 0) ? -1 : 1;
  264. }
  265. return 0;
  266. }
  267. static void __init clean_path(char *path, umode_t fmode)
  268. {
  269. struct kstat st;
  270. if (!vfs_lstat(path, &st) && (st.mode ^ fmode) & S_IFMT) {
  271. if (S_ISDIR(st.mode))
  272. ksys_rmdir(path);
  273. else
  274. ksys_unlink(path);
  275. }
  276. }
  277. static __initdata int wfd;
  278. static int __init do_name(void)
  279. {
  280. state = SkipIt;
  281. next_state = Reset;
  282. if (strcmp(collected, "TRAILER!!!") == 0) {
  283. free_hash();
  284. return 0;
  285. }
  286. clean_path(collected, mode);
  287. if (S_ISREG(mode)) {
  288. int ml = maybe_link();
  289. if (ml >= 0) {
  290. int openflags = O_WRONLY|O_CREAT;
  291. if (ml != 1)
  292. openflags |= O_TRUNC;
  293. wfd = ksys_open(collected, openflags, mode);
  294. if (wfd >= 0) {
  295. ksys_fchown(wfd, uid, gid);
  296. ksys_fchmod(wfd, mode);
  297. if (body_len)
  298. ksys_ftruncate(wfd, body_len);
  299. vcollected = kstrdup(collected, GFP_KERNEL);
  300. state = CopyFile;
  301. }
  302. }
  303. } else if (S_ISDIR(mode)) {
  304. ksys_mkdir(collected, mode);
  305. ksys_chown(collected, uid, gid);
  306. ksys_chmod(collected, mode);
  307. dir_add(collected, mtime);
  308. } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
  309. S_ISFIFO(mode) || S_ISSOCK(mode)) {
  310. if (maybe_link() == 0) {
  311. ksys_mknod(collected, mode, rdev);
  312. ksys_chown(collected, uid, gid);
  313. ksys_chmod(collected, mode);
  314. do_utime(collected, mtime);
  315. }
  316. }
  317. return 0;
  318. }
  319. static int __init do_copy(void)
  320. {
  321. if (byte_count >= body_len) {
  322. if (xwrite(wfd, victim, body_len) != body_len)
  323. error("write error");
  324. ksys_close(wfd);
  325. do_utime(vcollected, mtime);
  326. kfree(vcollected);
  327. eat(body_len);
  328. state = SkipIt;
  329. return 0;
  330. } else {
  331. if (xwrite(wfd, victim, byte_count) != byte_count)
  332. error("write error");
  333. body_len -= byte_count;
  334. eat(byte_count);
  335. return 1;
  336. }
  337. }
  338. static int __init do_symlink(void)
  339. {
  340. collected[N_ALIGN(name_len) + body_len] = '\0';
  341. clean_path(collected, 0);
  342. ksys_symlink(collected + N_ALIGN(name_len), collected);
  343. ksys_lchown(collected, uid, gid);
  344. do_utime(collected, mtime);
  345. state = SkipIt;
  346. next_state = Reset;
  347. return 0;
  348. }
  349. static __initdata int (*actions[])(void) = {
  350. [Start] = do_start,
  351. [Collect] = do_collect,
  352. [GotHeader] = do_header,
  353. [SkipIt] = do_skip,
  354. [GotName] = do_name,
  355. [CopyFile] = do_copy,
  356. [GotSymlink] = do_symlink,
  357. [Reset] = do_reset,
  358. };
  359. static long __init write_buffer(char *buf, unsigned long len)
  360. {
  361. byte_count = len;
  362. victim = buf;
  363. while (!actions[state]())
  364. ;
  365. return len - byte_count;
  366. }
  367. static long __init flush_buffer(void *bufv, unsigned long len)
  368. {
  369. char *buf = (char *) bufv;
  370. long written;
  371. long origLen = len;
  372. if (message)
  373. return -1;
  374. while ((written = write_buffer(buf, len)) < len && !message) {
  375. char c = buf[written];
  376. if (c == '0') {
  377. buf += written;
  378. len -= written;
  379. state = Start;
  380. } else if (c == 0) {
  381. buf += written;
  382. len -= written;
  383. state = Reset;
  384. } else
  385. error("junk in compressed archive");
  386. }
  387. return origLen;
  388. }
  389. static unsigned long my_inptr; /* index of next byte to be processed in inbuf */
  390. #include <linux/decompress/generic.h>
  391. static char * __init unpack_to_rootfs(char *buf, unsigned long len)
  392. {
  393. long written;
  394. decompress_fn decompress;
  395. const char *compress_name;
  396. static __initdata char msg_buf[64];
  397. header_buf = kmalloc(110, GFP_KERNEL);
  398. symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
  399. name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
  400. if (!header_buf || !symlink_buf || !name_buf)
  401. panic("can't allocate buffers");
  402. state = Start;
  403. this_header = 0;
  404. message = NULL;
  405. while (!message && len) {
  406. loff_t saved_offset = this_header;
  407. if (*buf == '0' && !(this_header & 3)) {
  408. state = Start;
  409. written = write_buffer(buf, len);
  410. buf += written;
  411. len -= written;
  412. continue;
  413. }
  414. if (!*buf) {
  415. buf++;
  416. len--;
  417. this_header++;
  418. continue;
  419. }
  420. this_header = 0;
  421. decompress = decompress_method(buf, len, &compress_name);
  422. pr_debug("Detected %s compressed data\n", compress_name);
  423. if (decompress) {
  424. int res = decompress(buf, len, NULL, flush_buffer, NULL,
  425. &my_inptr, error);
  426. if (res)
  427. error("decompressor failed");
  428. } else if (compress_name) {
  429. if (!message) {
  430. snprintf(msg_buf, sizeof msg_buf,
  431. "compression method %s not configured",
  432. compress_name);
  433. message = msg_buf;
  434. }
  435. } else
  436. error("junk in compressed archive");
  437. if (state != Reset)
  438. error("junk in compressed archive");
  439. this_header = saved_offset + my_inptr;
  440. buf += my_inptr;
  441. len -= my_inptr;
  442. }
  443. dir_utime();
  444. kfree(name_buf);
  445. kfree(symlink_buf);
  446. kfree(header_buf);
  447. return message;
  448. }
  449. static int __initdata do_retain_initrd;
  450. static int __init retain_initrd_param(char *str)
  451. {
  452. if (*str)
  453. return 0;
  454. do_retain_initrd = 1;
  455. return 1;
  456. }
  457. __setup("retain_initrd", retain_initrd_param);
  458. extern char __initramfs_start[];
  459. extern unsigned long __initramfs_size;
  460. #include <linux/initrd.h>
  461. #include <linux/kexec.h>
  462. static void __init free_initrd(void)
  463. {
  464. #ifdef CONFIG_KEXEC_CORE
  465. unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
  466. unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
  467. #endif
  468. if (do_retain_initrd)
  469. goto skip;
  470. #ifdef CONFIG_KEXEC_CORE
  471. /*
  472. * If the initrd region is overlapped with crashkernel reserved region,
  473. * free only memory that is not part of crashkernel region.
  474. */
  475. if (initrd_start < crashk_end && initrd_end > crashk_start) {
  476. /*
  477. * Initialize initrd memory region since the kexec boot does
  478. * not do.
  479. */
  480. memset((void *)initrd_start, 0, initrd_end - initrd_start);
  481. if (initrd_start < crashk_start)
  482. free_initrd_mem(initrd_start, crashk_start);
  483. if (initrd_end > crashk_end)
  484. free_initrd_mem(crashk_end, initrd_end);
  485. } else
  486. #endif
  487. free_initrd_mem(initrd_start, initrd_end);
  488. skip:
  489. initrd_start = 0;
  490. initrd_end = 0;
  491. }
  492. #ifdef CONFIG_BLK_DEV_RAM
  493. #define BUF_SIZE 1024
  494. static void __init clean_rootfs(void)
  495. {
  496. int fd;
  497. void *buf;
  498. struct linux_dirent64 *dirp;
  499. int num;
  500. fd = ksys_open("/", O_RDONLY, 0);
  501. WARN_ON(fd < 0);
  502. if (fd < 0)
  503. return;
  504. buf = kzalloc(BUF_SIZE, GFP_KERNEL);
  505. WARN_ON(!buf);
  506. if (!buf) {
  507. ksys_close(fd);
  508. return;
  509. }
  510. dirp = buf;
  511. num = ksys_getdents64(fd, dirp, BUF_SIZE);
  512. while (num > 0) {
  513. while (num > 0) {
  514. struct kstat st;
  515. int ret;
  516. ret = vfs_lstat(dirp->d_name, &st);
  517. WARN_ON_ONCE(ret);
  518. if (!ret) {
  519. if (S_ISDIR(st.mode))
  520. ksys_rmdir(dirp->d_name);
  521. else
  522. ksys_unlink(dirp->d_name);
  523. }
  524. num -= dirp->d_reclen;
  525. dirp = (void *)dirp + dirp->d_reclen;
  526. }
  527. dirp = buf;
  528. memset(buf, 0, BUF_SIZE);
  529. num = ksys_getdents64(fd, dirp, BUF_SIZE);
  530. }
  531. ksys_close(fd);
  532. kfree(buf);
  533. }
  534. #endif
  535. static int __init populate_rootfs(void)
  536. {
  537. /* Load the built in initramfs */
  538. char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
  539. if (err)
  540. panic("%s", err); /* Failed to decompress INTERNAL initramfs */
  541. /* If available load the bootloader supplied initrd */
  542. if (initrd_start && !IS_ENABLED(CONFIG_INITRAMFS_FORCE)) {
  543. #ifdef CONFIG_BLK_DEV_RAM
  544. int fd;
  545. printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
  546. err = unpack_to_rootfs((char *)initrd_start,
  547. initrd_end - initrd_start);
  548. if (!err) {
  549. free_initrd();
  550. goto done;
  551. } else {
  552. clean_rootfs();
  553. unpack_to_rootfs(__initramfs_start, __initramfs_size);
  554. }
  555. printk(KERN_INFO "rootfs image is not initramfs (%s)"
  556. "; looks like an initrd\n", err);
  557. fd = ksys_open("/initrd.image",
  558. O_WRONLY|O_CREAT, 0700);
  559. if (fd >= 0) {
  560. ssize_t written = xwrite(fd, (char *)initrd_start,
  561. initrd_end - initrd_start);
  562. if (written != initrd_end - initrd_start)
  563. pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
  564. written, initrd_end - initrd_start);
  565. ksys_close(fd);
  566. free_initrd();
  567. }
  568. done:
  569. /* empty statement */;
  570. #else
  571. printk(KERN_INFO "Unpacking initramfs...\n");
  572. err = unpack_to_rootfs((char *)initrd_start,
  573. initrd_end - initrd_start);
  574. if (err)
  575. printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
  576. free_initrd();
  577. #endif
  578. }
  579. flush_delayed_fput();
  580. /*
  581. * Try loading default modules from initramfs. This gives
  582. * us a chance to load before device_initcalls.
  583. */
  584. load_default_modules();
  585. return 0;
  586. }
  587. rootfs_initcall(populate_rootfs);