initramfs.c 13 KB

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