initramfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. if (body_len)
  278. sys_ftruncate(wfd, body_len);
  279. vcollected = kstrdup(collected, GFP_KERNEL);
  280. state = CopyFile;
  281. }
  282. }
  283. } else if (S_ISDIR(mode)) {
  284. sys_mkdir(collected, mode);
  285. sys_chown(collected, uid, gid);
  286. sys_chmod(collected, mode);
  287. dir_add(collected, mtime);
  288. } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
  289. S_ISFIFO(mode) || S_ISSOCK(mode)) {
  290. if (maybe_link() == 0) {
  291. sys_mknod(collected, mode, rdev);
  292. sys_chown(collected, uid, gid);
  293. sys_chmod(collected, mode);
  294. do_utime(collected, mtime);
  295. }
  296. }
  297. return 0;
  298. }
  299. static int __init do_copy(void)
  300. {
  301. if (count >= body_len) {
  302. sys_write(wfd, victim, body_len);
  303. sys_close(wfd);
  304. do_utime(vcollected, mtime);
  305. kfree(vcollected);
  306. eat(body_len);
  307. state = SkipIt;
  308. return 0;
  309. } else {
  310. sys_write(wfd, victim, count);
  311. body_len -= count;
  312. eat(count);
  313. return 1;
  314. }
  315. }
  316. static int __init do_symlink(void)
  317. {
  318. collected[N_ALIGN(name_len) + body_len] = '\0';
  319. clean_path(collected, 0);
  320. sys_symlink(collected + N_ALIGN(name_len), collected);
  321. sys_lchown(collected, uid, gid);
  322. do_utime(collected, mtime);
  323. state = SkipIt;
  324. next_state = Reset;
  325. return 0;
  326. }
  327. static __initdata int (*actions[])(void) = {
  328. [Start] = do_start,
  329. [Collect] = do_collect,
  330. [GotHeader] = do_header,
  331. [SkipIt] = do_skip,
  332. [GotName] = do_name,
  333. [CopyFile] = do_copy,
  334. [GotSymlink] = do_symlink,
  335. [Reset] = do_reset,
  336. };
  337. static int __init write_buffer(char *buf, unsigned len)
  338. {
  339. count = len;
  340. victim = buf;
  341. while (!actions[state]())
  342. ;
  343. return len - count;
  344. }
  345. static int __init flush_buffer(void *bufv, unsigned len)
  346. {
  347. char *buf = (char *) bufv;
  348. int written;
  349. int origLen = len;
  350. if (message)
  351. return -1;
  352. while ((written = write_buffer(buf, len)) < len && !message) {
  353. char c = buf[written];
  354. if (c == '0') {
  355. buf += written;
  356. len -= written;
  357. state = Start;
  358. } else if (c == 0) {
  359. buf += written;
  360. len -= written;
  361. state = Reset;
  362. } else
  363. error("junk in compressed archive");
  364. }
  365. return origLen;
  366. }
  367. static unsigned my_inptr; /* index of next byte to be processed in inbuf */
  368. #include <linux/decompress/generic.h>
  369. static char * __init unpack_to_rootfs(char *buf, unsigned len)
  370. {
  371. int written, res;
  372. decompress_fn decompress;
  373. const char *compress_name;
  374. static __initdata char msg_buf[64];
  375. header_buf = kmalloc(110, GFP_KERNEL);
  376. symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
  377. name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
  378. if (!header_buf || !symlink_buf || !name_buf)
  379. panic("can't allocate buffers");
  380. state = Start;
  381. this_header = 0;
  382. message = NULL;
  383. while (!message && len) {
  384. loff_t saved_offset = this_header;
  385. if (*buf == '0' && !(this_header & 3)) {
  386. state = Start;
  387. written = write_buffer(buf, len);
  388. buf += written;
  389. len -= written;
  390. continue;
  391. }
  392. if (!*buf) {
  393. buf++;
  394. len--;
  395. this_header++;
  396. continue;
  397. }
  398. this_header = 0;
  399. decompress = decompress_method(buf, len, &compress_name);
  400. if (decompress) {
  401. res = decompress(buf, len, NULL, flush_buffer, NULL,
  402. &my_inptr, error);
  403. if (res)
  404. error("decompressor failed");
  405. } else if (compress_name) {
  406. if (!message) {
  407. snprintf(msg_buf, sizeof msg_buf,
  408. "compression method %s not configured",
  409. compress_name);
  410. message = msg_buf;
  411. }
  412. } else
  413. error("junk in compressed archive");
  414. if (state != Reset)
  415. error("junk in compressed archive");
  416. this_header = saved_offset + my_inptr;
  417. buf += my_inptr;
  418. len -= my_inptr;
  419. }
  420. dir_utime();
  421. kfree(name_buf);
  422. kfree(symlink_buf);
  423. kfree(header_buf);
  424. return message;
  425. }
  426. static int __initdata do_retain_initrd;
  427. static int __init retain_initrd_param(char *str)
  428. {
  429. if (*str)
  430. return 0;
  431. do_retain_initrd = 1;
  432. return 1;
  433. }
  434. __setup("retain_initrd", retain_initrd_param);
  435. extern char __initramfs_start[];
  436. extern unsigned long __initramfs_size;
  437. #include <linux/initrd.h>
  438. #include <linux/kexec.h>
  439. static void __init free_initrd(void)
  440. {
  441. #ifdef CONFIG_KEXEC
  442. unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
  443. unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
  444. #endif
  445. if (do_retain_initrd)
  446. goto skip;
  447. #ifdef CONFIG_KEXEC
  448. /*
  449. * If the initrd region is overlapped with crashkernel reserved region,
  450. * free only memory that is not part of crashkernel region.
  451. */
  452. if (initrd_start < crashk_end && initrd_end > crashk_start) {
  453. /*
  454. * Initialize initrd memory region since the kexec boot does
  455. * not do.
  456. */
  457. memset((void *)initrd_start, 0, initrd_end - initrd_start);
  458. if (initrd_start < crashk_start)
  459. free_initrd_mem(initrd_start, crashk_start);
  460. if (initrd_end > crashk_end)
  461. free_initrd_mem(crashk_end, initrd_end);
  462. } else
  463. #endif
  464. free_initrd_mem(initrd_start, initrd_end);
  465. skip:
  466. initrd_start = 0;
  467. initrd_end = 0;
  468. }
  469. #ifdef CONFIG_BLK_DEV_RAM
  470. #define BUF_SIZE 1024
  471. static void __init clean_rootfs(void)
  472. {
  473. int fd;
  474. void *buf;
  475. struct linux_dirent64 *dirp;
  476. int num;
  477. fd = sys_open((const char __user __force *) "/", O_RDONLY, 0);
  478. WARN_ON(fd < 0);
  479. if (fd < 0)
  480. return;
  481. buf = kzalloc(BUF_SIZE, GFP_KERNEL);
  482. WARN_ON(!buf);
  483. if (!buf) {
  484. sys_close(fd);
  485. return;
  486. }
  487. dirp = buf;
  488. num = sys_getdents64(fd, dirp, BUF_SIZE);
  489. while (num > 0) {
  490. while (num > 0) {
  491. struct stat st;
  492. int ret;
  493. ret = sys_newlstat(dirp->d_name, &st);
  494. WARN_ON_ONCE(ret);
  495. if (!ret) {
  496. if (S_ISDIR(st.st_mode))
  497. sys_rmdir(dirp->d_name);
  498. else
  499. sys_unlink(dirp->d_name);
  500. }
  501. num -= dirp->d_reclen;
  502. dirp = (void *)dirp + dirp->d_reclen;
  503. }
  504. dirp = buf;
  505. memset(buf, 0, BUF_SIZE);
  506. num = sys_getdents64(fd, dirp, BUF_SIZE);
  507. }
  508. sys_close(fd);
  509. kfree(buf);
  510. }
  511. #endif
  512. static int __init populate_rootfs(void)
  513. {
  514. char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
  515. if (err)
  516. panic(err); /* Failed to decompress INTERNAL initramfs */
  517. if (initrd_start) {
  518. #ifdef CONFIG_BLK_DEV_RAM
  519. int fd;
  520. printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
  521. err = unpack_to_rootfs((char *)initrd_start,
  522. initrd_end - initrd_start);
  523. if (!err) {
  524. free_initrd();
  525. return 0;
  526. } else {
  527. clean_rootfs();
  528. unpack_to_rootfs(__initramfs_start, __initramfs_size);
  529. }
  530. printk(KERN_INFO "rootfs image is not initramfs (%s)"
  531. "; looks like an initrd\n", err);
  532. fd = sys_open((const char __user __force *) "/initrd.image",
  533. O_WRONLY|O_CREAT, 0700);
  534. if (fd >= 0) {
  535. sys_write(fd, (char *)initrd_start,
  536. initrd_end - initrd_start);
  537. sys_close(fd);
  538. free_initrd();
  539. }
  540. #else
  541. printk(KERN_INFO "Unpacking initramfs...\n");
  542. err = unpack_to_rootfs((char *)initrd_start,
  543. initrd_end - initrd_start);
  544. if (err)
  545. printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
  546. free_initrd();
  547. #endif
  548. }
  549. return 0;
  550. }
  551. rootfs_initcall(populate_rootfs);