initramfs.c 13 KB

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