initramfs.c 14 KB

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