initramfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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. static __initdata char *message;
  10. static void __init error(char *x)
  11. {
  12. if (!message)
  13. message = x;
  14. }
  15. /* link hash */
  16. #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
  17. static __initdata struct hash {
  18. int ino, minor, major;
  19. mode_t mode;
  20. struct hash *next;
  21. char name[N_ALIGN(PATH_MAX)];
  22. } *head[32];
  23. static inline int hash(int major, int minor, int ino)
  24. {
  25. unsigned long tmp = ino + minor + (major << 3);
  26. tmp += tmp >> 5;
  27. return tmp & 31;
  28. }
  29. static char __init *find_link(int major, int minor, int ino,
  30. mode_t mode, char *name)
  31. {
  32. struct hash **p, *q;
  33. for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
  34. if ((*p)->ino != ino)
  35. continue;
  36. if ((*p)->minor != minor)
  37. continue;
  38. if ((*p)->major != major)
  39. continue;
  40. if (((*p)->mode ^ mode) & S_IFMT)
  41. continue;
  42. return (*p)->name;
  43. }
  44. q = kmalloc(sizeof(struct hash), GFP_KERNEL);
  45. if (!q)
  46. panic("can't allocate link hash entry");
  47. q->major = major;
  48. q->minor = minor;
  49. q->ino = ino;
  50. q->mode = mode;
  51. strcpy(q->name, name);
  52. q->next = NULL;
  53. *p = q;
  54. return NULL;
  55. }
  56. static void __init free_hash(void)
  57. {
  58. struct hash **p, *q;
  59. for (p = head; p < head + 32; p++) {
  60. while (*p) {
  61. q = *p;
  62. *p = q->next;
  63. kfree(q);
  64. }
  65. }
  66. }
  67. /* cpio header parsing */
  68. static __initdata unsigned long ino, major, minor, nlink;
  69. static __initdata mode_t mode;
  70. static __initdata unsigned long body_len, name_len;
  71. static __initdata uid_t uid;
  72. static __initdata gid_t gid;
  73. static __initdata unsigned rdev;
  74. static void __init parse_header(char *s)
  75. {
  76. unsigned long parsed[12];
  77. char buf[9];
  78. int i;
  79. buf[8] = '\0';
  80. for (i = 0, s += 6; i < 12; i++, s += 8) {
  81. memcpy(buf, s, 8);
  82. parsed[i] = simple_strtoul(buf, NULL, 16);
  83. }
  84. ino = parsed[0];
  85. mode = parsed[1];
  86. uid = parsed[2];
  87. gid = parsed[3];
  88. nlink = parsed[4];
  89. body_len = parsed[6];
  90. major = parsed[7];
  91. minor = parsed[8];
  92. rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
  93. name_len = parsed[11];
  94. }
  95. /* FSM */
  96. static __initdata enum state {
  97. Start,
  98. Collect,
  99. GotHeader,
  100. SkipIt,
  101. GotName,
  102. CopyFile,
  103. GotSymlink,
  104. Reset
  105. } state, next_state;
  106. static __initdata char *victim;
  107. static __initdata unsigned count;
  108. static __initdata loff_t this_header, next_header;
  109. static __initdata int dry_run;
  110. static inline void __init eat(unsigned n)
  111. {
  112. victim += n;
  113. this_header += n;
  114. count -= n;
  115. }
  116. static __initdata char *collected;
  117. static __initdata int remains;
  118. static __initdata char *collect;
  119. static void __init read_into(char *buf, unsigned size, enum state next)
  120. {
  121. if (count >= size) {
  122. collected = victim;
  123. eat(size);
  124. state = next;
  125. } else {
  126. collect = collected = buf;
  127. remains = size;
  128. next_state = next;
  129. state = Collect;
  130. }
  131. }
  132. static __initdata char *header_buf, *symlink_buf, *name_buf;
  133. static int __init do_start(void)
  134. {
  135. read_into(header_buf, 110, GotHeader);
  136. return 0;
  137. }
  138. static int __init do_collect(void)
  139. {
  140. unsigned n = remains;
  141. if (count < n)
  142. n = count;
  143. memcpy(collect, victim, n);
  144. eat(n);
  145. collect += n;
  146. if ((remains -= n) != 0)
  147. return 1;
  148. state = next_state;
  149. return 0;
  150. }
  151. static int __init do_header(void)
  152. {
  153. if (memcmp(collected, "070707", 6)==0) {
  154. error("incorrect cpio method used: use -H newc option");
  155. return 1;
  156. }
  157. if (memcmp(collected, "070701", 6)) {
  158. error("no cpio magic");
  159. return 1;
  160. }
  161. parse_header(collected);
  162. next_header = this_header + N_ALIGN(name_len) + body_len;
  163. next_header = (next_header + 3) & ~3;
  164. if (dry_run) {
  165. read_into(name_buf, N_ALIGN(name_len), GotName);
  166. return 0;
  167. }
  168. state = SkipIt;
  169. if (name_len <= 0 || name_len > PATH_MAX)
  170. return 0;
  171. if (S_ISLNK(mode)) {
  172. if (body_len > PATH_MAX)
  173. return 0;
  174. collect = collected = symlink_buf;
  175. remains = N_ALIGN(name_len) + body_len;
  176. next_state = GotSymlink;
  177. state = Collect;
  178. return 0;
  179. }
  180. if (S_ISREG(mode) || !body_len)
  181. read_into(name_buf, N_ALIGN(name_len), GotName);
  182. return 0;
  183. }
  184. static int __init do_skip(void)
  185. {
  186. if (this_header + count < next_header) {
  187. eat(count);
  188. return 1;
  189. } else {
  190. eat(next_header - this_header);
  191. state = next_state;
  192. return 0;
  193. }
  194. }
  195. static int __init do_reset(void)
  196. {
  197. while(count && *victim == '\0')
  198. eat(1);
  199. if (count && (this_header & 3))
  200. error("broken padding");
  201. return 1;
  202. }
  203. static int __init maybe_link(void)
  204. {
  205. if (nlink >= 2) {
  206. char *old = find_link(major, minor, ino, mode, collected);
  207. if (old)
  208. return (sys_link(old, collected) < 0) ? -1 : 1;
  209. }
  210. return 0;
  211. }
  212. static void __init clean_path(char *path, mode_t mode)
  213. {
  214. struct stat st;
  215. if (!sys_newlstat(path, &st) && (st.st_mode^mode) & S_IFMT) {
  216. if (S_ISDIR(st.st_mode))
  217. sys_rmdir(path);
  218. else
  219. sys_unlink(path);
  220. }
  221. }
  222. static __initdata int wfd;
  223. static int __init do_name(void)
  224. {
  225. state = SkipIt;
  226. next_state = Reset;
  227. if (strcmp(collected, "TRAILER!!!") == 0) {
  228. free_hash();
  229. return 0;
  230. }
  231. if (dry_run)
  232. return 0;
  233. clean_path(collected, mode);
  234. if (S_ISREG(mode)) {
  235. int ml = maybe_link();
  236. if (ml >= 0) {
  237. int openflags = O_WRONLY|O_CREAT;
  238. if (ml != 1)
  239. openflags |= O_TRUNC;
  240. wfd = sys_open(collected, openflags, mode);
  241. if (wfd >= 0) {
  242. sys_fchown(wfd, uid, gid);
  243. sys_fchmod(wfd, mode);
  244. state = CopyFile;
  245. }
  246. }
  247. } else if (S_ISDIR(mode)) {
  248. sys_mkdir(collected, mode);
  249. sys_chown(collected, uid, gid);
  250. sys_chmod(collected, mode);
  251. } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
  252. S_ISFIFO(mode) || S_ISSOCK(mode)) {
  253. if (maybe_link() == 0) {
  254. sys_mknod(collected, mode, rdev);
  255. sys_chown(collected, uid, gid);
  256. sys_chmod(collected, mode);
  257. }
  258. }
  259. return 0;
  260. }
  261. static int __init do_copy(void)
  262. {
  263. if (count >= body_len) {
  264. sys_write(wfd, victim, body_len);
  265. sys_close(wfd);
  266. eat(body_len);
  267. state = SkipIt;
  268. return 0;
  269. } else {
  270. sys_write(wfd, victim, count);
  271. body_len -= count;
  272. eat(count);
  273. return 1;
  274. }
  275. }
  276. static int __init do_symlink(void)
  277. {
  278. collected[N_ALIGN(name_len) + body_len] = '\0';
  279. clean_path(collected, 0);
  280. sys_symlink(collected + N_ALIGN(name_len), collected);
  281. sys_lchown(collected, uid, gid);
  282. state = SkipIt;
  283. next_state = Reset;
  284. return 0;
  285. }
  286. static __initdata int (*actions[])(void) = {
  287. [Start] = do_start,
  288. [Collect] = do_collect,
  289. [GotHeader] = do_header,
  290. [SkipIt] = do_skip,
  291. [GotName] = do_name,
  292. [CopyFile] = do_copy,
  293. [GotSymlink] = do_symlink,
  294. [Reset] = do_reset,
  295. };
  296. static int __init write_buffer(char *buf, unsigned len)
  297. {
  298. count = len;
  299. victim = buf;
  300. while (!actions[state]())
  301. ;
  302. return len - count;
  303. }
  304. static void __init flush_buffer(char *buf, unsigned len)
  305. {
  306. int written;
  307. if (message)
  308. return;
  309. while ((written = write_buffer(buf, len)) < len && !message) {
  310. char c = buf[written];
  311. if (c == '0') {
  312. buf += written;
  313. len -= written;
  314. state = Start;
  315. } else if (c == 0) {
  316. buf += written;
  317. len -= written;
  318. state = Reset;
  319. } else
  320. error("junk in compressed archive");
  321. }
  322. }
  323. /*
  324. * gzip declarations
  325. */
  326. #define OF(args) args
  327. #ifndef memzero
  328. #define memzero(s, n) memset ((s), 0, (n))
  329. #endif
  330. typedef unsigned char uch;
  331. typedef unsigned short ush;
  332. typedef unsigned long ulg;
  333. #define WSIZE 0x8000 /* window size--must be a power of two, and */
  334. /* at least 32K for zip's deflate method */
  335. static uch *inbuf;
  336. static uch *window;
  337. static unsigned insize; /* valid bytes in inbuf */
  338. static unsigned inptr; /* index of next byte to be processed in inbuf */
  339. static unsigned outcnt; /* bytes in output buffer */
  340. static long bytes_out;
  341. #define get_byte() (inptr < insize ? inbuf[inptr++] : -1)
  342. /* Diagnostic functions (stubbed out) */
  343. #define Assert(cond,msg)
  344. #define Trace(x)
  345. #define Tracev(x)
  346. #define Tracevv(x)
  347. #define Tracec(c,x)
  348. #define Tracecv(c,x)
  349. #define STATIC static
  350. #define INIT __init
  351. static void __init flush_window(void);
  352. static void __init error(char *m);
  353. #define NO_INFLATE_MALLOC
  354. #include "../lib/inflate.c"
  355. /* ===========================================================================
  356. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  357. * (Used for the decompressed data only.)
  358. */
  359. static void __init flush_window(void)
  360. {
  361. ulg c = crc; /* temporary variable */
  362. unsigned n;
  363. uch *in, ch;
  364. flush_buffer(window, outcnt);
  365. in = window;
  366. for (n = 0; n < outcnt; n++) {
  367. ch = *in++;
  368. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  369. }
  370. crc = c;
  371. bytes_out += (ulg)outcnt;
  372. outcnt = 0;
  373. }
  374. static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
  375. {
  376. int written;
  377. dry_run = check_only;
  378. header_buf = kmalloc(110, GFP_KERNEL);
  379. symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
  380. name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
  381. window = kmalloc(WSIZE, GFP_KERNEL);
  382. if (!window || !header_buf || !symlink_buf || !name_buf)
  383. panic("can't allocate buffers");
  384. state = Start;
  385. this_header = 0;
  386. message = NULL;
  387. while (!message && len) {
  388. loff_t saved_offset = this_header;
  389. if (*buf == '0' && !(this_header & 3)) {
  390. state = Start;
  391. written = write_buffer(buf, len);
  392. buf += written;
  393. len -= written;
  394. continue;
  395. }
  396. if (!*buf) {
  397. buf++;
  398. len--;
  399. this_header++;
  400. continue;
  401. }
  402. this_header = 0;
  403. insize = len;
  404. inbuf = buf;
  405. inptr = 0;
  406. outcnt = 0; /* bytes in output buffer */
  407. bytes_out = 0;
  408. crc = (ulg)0xffffffffL; /* shift register contents */
  409. makecrc();
  410. gunzip();
  411. if (state != Reset)
  412. error("junk in gzipped archive");
  413. this_header = saved_offset + inptr;
  414. buf += inptr;
  415. len -= inptr;
  416. }
  417. kfree(window);
  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. static int __init populate_rootfs(void)
  466. {
  467. char *err = unpack_to_rootfs(__initramfs_start,
  468. __initramfs_end - __initramfs_start, 0);
  469. if (err)
  470. panic(err);
  471. if (initrd_start) {
  472. #ifdef CONFIG_BLK_DEV_RAM
  473. int fd;
  474. printk(KERN_INFO "checking if image is initramfs...");
  475. err = unpack_to_rootfs((char *)initrd_start,
  476. initrd_end - initrd_start, 1);
  477. if (!err) {
  478. printk(" it is\n");
  479. unpack_to_rootfs((char *)initrd_start,
  480. initrd_end - initrd_start, 0);
  481. free_initrd();
  482. return 0;
  483. }
  484. printk("it isn't (%s); looks like an initrd\n", err);
  485. fd = sys_open("/initrd.image", O_WRONLY|O_CREAT, 0700);
  486. if (fd >= 0) {
  487. sys_write(fd, (char *)initrd_start,
  488. initrd_end - initrd_start);
  489. sys_close(fd);
  490. free_initrd();
  491. }
  492. #else
  493. printk(KERN_INFO "Unpacking initramfs...");
  494. err = unpack_to_rootfs((char *)initrd_start,
  495. initrd_end - initrd_start, 0);
  496. if (err)
  497. panic(err);
  498. printk(" done\n");
  499. free_initrd();
  500. #endif
  501. }
  502. return 0;
  503. }
  504. rootfs_initcall(populate_rootfs);