initramfs.c 14 KB

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