initramfs.c 13 KB

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