/kern_oII/init/initramfs.c

http://omnia2droid.googlecode.com/ · C · 608 lines · 537 code · 60 blank · 11 comment · 99 complexity · 7e9bc5b2457440ee332603eb3940d925 MD5 · raw file

  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;
  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. decompress(buf, len, NULL, flush_buffer, NULL,
  402. &my_inptr, error);
  403. else if (compress_name) {
  404. if (!message) {
  405. snprintf(msg_buf, sizeof msg_buf,
  406. "compression method %s not configured",
  407. compress_name);
  408. message = msg_buf;
  409. }
  410. }
  411. if (state != Reset)
  412. error("junk in compressed archive");
  413. this_header = saved_offset + my_inptr;
  414. buf += my_inptr;
  415. len -= my_inptr;
  416. }
  417. dir_utime();
  418. kfree(name_buf);
  419. kfree(symlink_buf);
  420. kfree(header_buf);
  421. return message;
  422. }
  423. static int __initdata do_retain_initrd;
  424. static int __init retain_initrd_param(char *str)
  425. {
  426. if (*str)
  427. return 0;
  428. do_retain_initrd = 1;
  429. return 1;
  430. }
  431. __setup("retain_initrd", retain_initrd_param);
  432. extern char __initramfs_start[], __initramfs_end[];
  433. #include <linux/initrd.h>
  434. #include <linux/kexec.h>
  435. static void __init free_initrd(void)
  436. {
  437. #ifdef CONFIG_KEXEC
  438. unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
  439. unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
  440. #endif
  441. if (do_retain_initrd)
  442. goto skip;
  443. #ifdef CONFIG_KEXEC
  444. /*
  445. * If the initrd region is overlapped with crashkernel reserved region,
  446. * free only memory that is not part of crashkernel region.
  447. */
  448. if (initrd_start < crashk_end && initrd_end > crashk_start) {
  449. /*
  450. * Initialize initrd memory region since the kexec boot does
  451. * not do.
  452. */
  453. memset((void *)initrd_start, 0, initrd_end - initrd_start);
  454. if (initrd_start < crashk_start)
  455. free_initrd_mem(initrd_start, crashk_start);
  456. if (initrd_end > crashk_end)
  457. free_initrd_mem(crashk_end, initrd_end);
  458. } else
  459. #endif
  460. free_initrd_mem(initrd_start, initrd_end);
  461. skip:
  462. initrd_start = 0;
  463. initrd_end = 0;
  464. }
  465. #ifdef CONFIG_BLK_DEV_RAM
  466. #define BUF_SIZE 1024
  467. static void __init clean_rootfs(void)
  468. {
  469. int fd;
  470. void *buf;
  471. struct linux_dirent64 *dirp;
  472. int count;
  473. fd = sys_open("/", O_RDONLY, 0);
  474. WARN_ON(fd < 0);
  475. if (fd < 0)
  476. return;
  477. buf = kzalloc(BUF_SIZE, GFP_KERNEL);
  478. WARN_ON(!buf);
  479. if (!buf) {
  480. sys_close(fd);
  481. return;
  482. }
  483. dirp = buf;
  484. count = sys_getdents64(fd, dirp, BUF_SIZE);
  485. while (count > 0) {
  486. while (count > 0) {
  487. struct stat st;
  488. int ret;
  489. ret = sys_newlstat(dirp->d_name, &st);
  490. WARN_ON_ONCE(ret);
  491. if (!ret) {
  492. if (S_ISDIR(st.st_mode))
  493. sys_rmdir(dirp->d_name);
  494. else
  495. sys_unlink(dirp->d_name);
  496. }
  497. count -= dirp->d_reclen;
  498. dirp = (void *)dirp + dirp->d_reclen;
  499. }
  500. dirp = buf;
  501. memset(buf, 0, BUF_SIZE);
  502. count = sys_getdents64(fd, dirp, BUF_SIZE);
  503. }
  504. sys_close(fd);
  505. kfree(buf);
  506. }
  507. #endif
  508. static int __init populate_rootfs(void)
  509. {
  510. char *err = unpack_to_rootfs(__initramfs_start,
  511. __initramfs_end - __initramfs_start);
  512. if (err)
  513. panic(err); /* Failed to decompress INTERNAL initramfs */
  514. if (initrd_start) {
  515. #ifdef CONFIG_BLK_DEV_RAM
  516. int fd;
  517. printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
  518. err = unpack_to_rootfs((char *)initrd_start,
  519. initrd_end - initrd_start);
  520. if (!err) {
  521. free_initrd();
  522. return 0;
  523. } else {
  524. clean_rootfs();
  525. unpack_to_rootfs(__initramfs_start,
  526. __initramfs_end - __initramfs_start);
  527. }
  528. printk(KERN_INFO "rootfs image is not initramfs (%s)"
  529. "; looks like an initrd\n", err);
  530. fd = sys_open("/initrd.image", O_WRONLY|O_CREAT, 0700);
  531. if (fd >= 0) {
  532. sys_write(fd, (char *)initrd_start,
  533. initrd_end - initrd_start);
  534. sys_close(fd);
  535. free_initrd();
  536. }
  537. #else
  538. printk(KERN_INFO "Unpacking initramfs...\n");
  539. err = unpack_to_rootfs((char *)initrd_start,
  540. initrd_end - initrd_start);
  541. if (err)
  542. printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
  543. free_initrd();
  544. #endif
  545. }
  546. return 0;
  547. }
  548. rootfs_initcall(populate_rootfs);