initramfs.c 14 KB

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