initramfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  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. sys_ftruncate(wfd, body_len);
  284. vcollected = kstrdup(collected, GFP_KERNEL);
  285. state = CopyFile;
  286. }
  287. }
  288. } else if (S_ISDIR(mode)) {
  289. sys_mkdir(collected, mode);
  290. sys_chown(collected, uid, gid);
  291. sys_chmod(collected, mode);
  292. dir_add(collected, mtime);
  293. } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
  294. S_ISFIFO(mode) || S_ISSOCK(mode)) {
  295. if (maybe_link() == 0) {
  296. sys_mknod(collected, mode, rdev);
  297. sys_chown(collected, uid, gid);
  298. sys_chmod(collected, mode);
  299. do_utime(collected, mtime);
  300. }
  301. }
  302. return 0;
  303. }
  304. static int __init do_copy(void)
  305. {
  306. if (count >= body_len) {
  307. sys_write(wfd, victim, body_len);
  308. sys_close(wfd);
  309. do_utime(vcollected, mtime);
  310. kfree(vcollected);
  311. eat(body_len);
  312. state = SkipIt;
  313. return 0;
  314. } else {
  315. sys_write(wfd, victim, count);
  316. body_len -= count;
  317. eat(count);
  318. return 1;
  319. }
  320. }
  321. static int __init do_symlink(void)
  322. {
  323. collected[N_ALIGN(name_len) + body_len] = '\0';
  324. clean_path(collected, 0);
  325. sys_symlink(collected + N_ALIGN(name_len), collected);
  326. sys_lchown(collected, uid, gid);
  327. do_utime(collected, mtime);
  328. state = SkipIt;
  329. next_state = Reset;
  330. return 0;
  331. }
  332. static __initdata int (*actions[])(void) = {
  333. [Start] = do_start,
  334. [Collect] = do_collect,
  335. [GotHeader] = do_header,
  336. [SkipIt] = do_skip,
  337. [GotName] = do_name,
  338. [CopyFile] = do_copy,
  339. [GotSymlink] = do_symlink,
  340. [Reset] = do_reset,
  341. };
  342. static int __init write_buffer(char *buf, unsigned len)
  343. {
  344. count = len;
  345. victim = buf;
  346. while (!actions[state]())
  347. ;
  348. return len - count;
  349. }
  350. static void __init flush_buffer(char *buf, unsigned len)
  351. {
  352. int written;
  353. if (message)
  354. return;
  355. while ((written = write_buffer(buf, len)) < len && !message) {
  356. char c = buf[written];
  357. if (c == '0') {
  358. buf += written;
  359. len -= written;
  360. state = Start;
  361. } else if (c == 0) {
  362. buf += written;
  363. len -= written;
  364. state = Reset;
  365. } else
  366. error("junk in compressed archive");
  367. }
  368. }
  369. /*
  370. * gzip declarations
  371. */
  372. #define OF(args) args
  373. #ifndef memzero
  374. #define memzero(s, n) memset ((s), 0, (n))
  375. #endif
  376. typedef unsigned char uch;
  377. typedef unsigned short ush;
  378. typedef unsigned long ulg;
  379. #define WSIZE 0x8000 /* window size--must be a power of two, and */
  380. /* at least 32K for zip's deflate method */
  381. static uch *inbuf;
  382. static uch *window;
  383. static unsigned insize; /* valid bytes in inbuf */
  384. static unsigned inptr; /* index of next byte to be processed in inbuf */
  385. static unsigned outcnt; /* bytes in output buffer */
  386. static long bytes_out;
  387. #define get_byte() (inptr < insize ? inbuf[inptr++] : -1)
  388. /* Diagnostic functions (stubbed out) */
  389. #define Assert(cond,msg)
  390. #define Trace(x)
  391. #define Tracev(x)
  392. #define Tracevv(x)
  393. #define Tracec(c,x)
  394. #define Tracecv(c,x)
  395. #define STATIC static
  396. #define INIT __init
  397. static void __init flush_window(void);
  398. static void __init error(char *m);
  399. #define NO_INFLATE_MALLOC
  400. #include "../lib/inflate.c"
  401. /* ===========================================================================
  402. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  403. * (Used for the decompressed data only.)
  404. */
  405. static void __init flush_window(void)
  406. {
  407. ulg c = crc; /* temporary variable */
  408. unsigned n;
  409. uch *in, ch;
  410. flush_buffer(window, outcnt);
  411. in = window;
  412. for (n = 0; n < outcnt; n++) {
  413. ch = *in++;
  414. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  415. }
  416. crc = c;
  417. bytes_out += (ulg)outcnt;
  418. outcnt = 0;
  419. }
  420. static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
  421. {
  422. int written;
  423. dry_run = check_only;
  424. header_buf = kmalloc(110, GFP_KERNEL);
  425. symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
  426. name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
  427. window = kmalloc(WSIZE, GFP_KERNEL);
  428. if (!window || !header_buf || !symlink_buf || !name_buf)
  429. panic("can't allocate buffers");
  430. state = Start;
  431. this_header = 0;
  432. message = NULL;
  433. while (!message && len) {
  434. loff_t saved_offset = this_header;
  435. if (*buf == '0' && !(this_header & 3)) {
  436. state = Start;
  437. written = write_buffer(buf, len);
  438. buf += written;
  439. len -= written;
  440. continue;
  441. }
  442. if (!*buf) {
  443. buf++;
  444. len--;
  445. this_header++;
  446. continue;
  447. }
  448. this_header = 0;
  449. insize = len;
  450. inbuf = buf;
  451. inptr = 0;
  452. outcnt = 0; /* bytes in output buffer */
  453. bytes_out = 0;
  454. crc = (ulg)0xffffffffL; /* shift register contents */
  455. makecrc();
  456. gunzip();
  457. if (state != Reset)
  458. error("junk in gzipped archive");
  459. this_header = saved_offset + inptr;
  460. buf += inptr;
  461. len -= inptr;
  462. }
  463. dir_utime();
  464. kfree(window);
  465. kfree(name_buf);
  466. kfree(symlink_buf);
  467. kfree(header_buf);
  468. return message;
  469. }
  470. static int __initdata do_retain_initrd;
  471. static int __init retain_initrd_param(char *str)
  472. {
  473. if (*str)
  474. return 0;
  475. do_retain_initrd = 1;
  476. return 1;
  477. }
  478. __setup("retain_initrd", retain_initrd_param);
  479. extern char __initramfs_start[], __initramfs_end[];
  480. #include <linux/initrd.h>
  481. #include <linux/kexec.h>
  482. static void __init free_initrd(void)
  483. {
  484. #ifdef CONFIG_KEXEC
  485. unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
  486. unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
  487. #endif
  488. if (do_retain_initrd)
  489. goto skip;
  490. #ifdef CONFIG_KEXEC
  491. /*
  492. * If the initrd region is overlapped with crashkernel reserved region,
  493. * free only memory that is not part of crashkernel region.
  494. */
  495. if (initrd_start < crashk_end && initrd_end > crashk_start) {
  496. /*
  497. * Initialize initrd memory region since the kexec boot does
  498. * not do.
  499. */
  500. memset((void *)initrd_start, 0, initrd_end - initrd_start);
  501. if (initrd_start < crashk_start)
  502. free_initrd_mem(initrd_start, crashk_start);
  503. if (initrd_end > crashk_end)
  504. free_initrd_mem(crashk_end, initrd_end);
  505. } else
  506. #endif
  507. free_initrd_mem(initrd_start, initrd_end);
  508. skip:
  509. initrd_start = 0;
  510. initrd_end = 0;
  511. }
  512. static int __init populate_rootfs(void)
  513. {
  514. char *err = unpack_to_rootfs(__initramfs_start,
  515. __initramfs_end - __initramfs_start, 0);
  516. if (err)
  517. panic(err);
  518. if (initrd_start) {
  519. #ifdef CONFIG_BLK_DEV_RAM
  520. int fd;
  521. printk(KERN_INFO "checking if image is initramfs...");
  522. err = unpack_to_rootfs((char *)initrd_start,
  523. initrd_end - initrd_start, 1);
  524. if (!err) {
  525. printk(" it is\n");
  526. unpack_to_rootfs((char *)initrd_start,
  527. initrd_end - initrd_start, 0);
  528. free_initrd();
  529. return 0;
  530. }
  531. printk("it isn't (%s); looks like an initrd\n", err);
  532. fd = sys_open("/initrd.image", O_WRONLY|O_CREAT, 0700);
  533. if (fd >= 0) {
  534. sys_write(fd, (char *)initrd_start,
  535. initrd_end - initrd_start);
  536. sys_close(fd);
  537. free_initrd();
  538. }
  539. #else
  540. printk(KERN_INFO "Unpacking initramfs...");
  541. err = unpack_to_rootfs((char *)initrd_start,
  542. initrd_end - initrd_start, 0);
  543. if (err)
  544. panic(err);
  545. printk(" done\n");
  546. free_initrd();
  547. #endif
  548. }
  549. return 0;
  550. }
  551. rootfs_initcall(populate_rootfs);