initramfs.c 12 KB

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