initramfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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, "070701", 6)) {
  162. error("no cpio magic");
  163. return 1;
  164. }
  165. parse_header(collected);
  166. next_header = this_header + N_ALIGN(name_len) + body_len;
  167. next_header = (next_header + 3) & ~3;
  168. if (dry_run) {
  169. read_into(name_buf, N_ALIGN(name_len), GotName);
  170. return 0;
  171. }
  172. state = SkipIt;
  173. if (name_len <= 0 || name_len > PATH_MAX)
  174. return 0;
  175. if (S_ISLNK(mode)) {
  176. if (body_len > PATH_MAX)
  177. return 0;
  178. collect = collected = symlink_buf;
  179. remains = N_ALIGN(name_len) + body_len;
  180. next_state = GotSymlink;
  181. state = Collect;
  182. return 0;
  183. }
  184. if (S_ISREG(mode) || !body_len)
  185. read_into(name_buf, N_ALIGN(name_len), GotName);
  186. return 0;
  187. }
  188. static int __init do_skip(void)
  189. {
  190. if (this_header + count < next_header) {
  191. eat(count);
  192. return 1;
  193. } else {
  194. eat(next_header - this_header);
  195. state = next_state;
  196. return 0;
  197. }
  198. }
  199. static int __init do_reset(void)
  200. {
  201. while(count && *victim == '\0')
  202. eat(1);
  203. if (count && (this_header & 3))
  204. error("broken padding");
  205. return 1;
  206. }
  207. static int __init maybe_link(void)
  208. {
  209. if (nlink >= 2) {
  210. char *old = find_link(major, minor, ino, mode, collected);
  211. if (old)
  212. return (sys_link(old, collected) < 0) ? -1 : 1;
  213. }
  214. return 0;
  215. }
  216. static void __init clean_path(char *path, mode_t mode)
  217. {
  218. struct stat st;
  219. if (!sys_newlstat(path, &st) && (st.st_mode^mode) & S_IFMT) {
  220. if (S_ISDIR(st.st_mode))
  221. sys_rmdir(path);
  222. else
  223. sys_unlink(path);
  224. }
  225. }
  226. static __initdata int wfd;
  227. static int __init do_name(void)
  228. {
  229. state = SkipIt;
  230. next_state = Reset;
  231. if (strcmp(collected, "TRAILER!!!") == 0) {
  232. free_hash();
  233. return 0;
  234. }
  235. if (dry_run)
  236. return 0;
  237. clean_path(collected, mode);
  238. if (S_ISREG(mode)) {
  239. int ml = maybe_link();
  240. if (ml >= 0) {
  241. int openflags = O_WRONLY|O_CREAT;
  242. if (ml != 1)
  243. openflags |= O_TRUNC;
  244. wfd = sys_open(collected, openflags, mode);
  245. if (wfd >= 0) {
  246. sys_fchown(wfd, uid, gid);
  247. sys_fchmod(wfd, mode);
  248. state = CopyFile;
  249. }
  250. }
  251. } else if (S_ISDIR(mode)) {
  252. sys_mkdir(collected, mode);
  253. sys_chown(collected, uid, gid);
  254. sys_chmod(collected, mode);
  255. } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
  256. S_ISFIFO(mode) || S_ISSOCK(mode)) {
  257. if (maybe_link() == 0) {
  258. sys_mknod(collected, mode, rdev);
  259. sys_chown(collected, uid, gid);
  260. sys_chmod(collected, mode);
  261. }
  262. }
  263. return 0;
  264. }
  265. static int __init do_copy(void)
  266. {
  267. if (count >= body_len) {
  268. sys_write(wfd, victim, body_len);
  269. sys_close(wfd);
  270. eat(body_len);
  271. state = SkipIt;
  272. return 0;
  273. } else {
  274. sys_write(wfd, victim, count);
  275. body_len -= count;
  276. eat(count);
  277. return 1;
  278. }
  279. }
  280. static int __init do_symlink(void)
  281. {
  282. collected[N_ALIGN(name_len) + body_len] = '\0';
  283. clean_path(collected, 0);
  284. sys_symlink(collected + N_ALIGN(name_len), collected);
  285. sys_lchown(collected, uid, gid);
  286. state = SkipIt;
  287. next_state = Reset;
  288. return 0;
  289. }
  290. static __initdata int (*actions[])(void) = {
  291. [Start] = do_start,
  292. [Collect] = do_collect,
  293. [GotHeader] = do_header,
  294. [SkipIt] = do_skip,
  295. [GotName] = do_name,
  296. [CopyFile] = do_copy,
  297. [GotSymlink] = do_symlink,
  298. [Reset] = do_reset,
  299. };
  300. static int __init write_buffer(char *buf, unsigned len)
  301. {
  302. count = len;
  303. victim = buf;
  304. while (!actions[state]())
  305. ;
  306. return len - count;
  307. }
  308. static void __init flush_buffer(char *buf, unsigned len)
  309. {
  310. int written;
  311. if (message)
  312. return;
  313. while ((written = write_buffer(buf, len)) < len && !message) {
  314. char c = buf[written];
  315. if (c == '0') {
  316. buf += written;
  317. len -= written;
  318. state = Start;
  319. } else if (c == 0) {
  320. buf += written;
  321. len -= written;
  322. state = Reset;
  323. } else
  324. error("junk in compressed archive");
  325. }
  326. }
  327. /*
  328. * gzip declarations
  329. */
  330. #define OF(args) args
  331. #ifndef memzero
  332. #define memzero(s, n) memset ((s), 0, (n))
  333. #endif
  334. typedef unsigned char uch;
  335. typedef unsigned short ush;
  336. typedef unsigned long ulg;
  337. #define WSIZE 0x8000 /* window size--must be a power of two, and */
  338. /* at least 32K for zip's deflate method */
  339. static uch *inbuf;
  340. static uch *window;
  341. static unsigned insize; /* valid bytes in inbuf */
  342. static unsigned inptr; /* index of next byte to be processed in inbuf */
  343. static unsigned outcnt; /* bytes in output buffer */
  344. static long bytes_out;
  345. #define get_byte() (inptr < insize ? inbuf[inptr++] : -1)
  346. /* Diagnostic functions (stubbed out) */
  347. #define Assert(cond,msg)
  348. #define Trace(x)
  349. #define Tracev(x)
  350. #define Tracevv(x)
  351. #define Tracec(c,x)
  352. #define Tracecv(c,x)
  353. #define STATIC static
  354. #define INIT __init
  355. static void __init flush_window(void);
  356. static void __init error(char *m);
  357. static void __init gzip_mark(void **);
  358. static void __init gzip_release(void **);
  359. #include "../lib/inflate.c"
  360. static void __init gzip_mark(void **ptr)
  361. {
  362. }
  363. static void __init gzip_release(void **ptr)
  364. {
  365. }
  366. /* ===========================================================================
  367. * Write the output window window[0..outcnt-1] and update crc and bytes_out.
  368. * (Used for the decompressed data only.)
  369. */
  370. static void __init flush_window(void)
  371. {
  372. ulg c = crc; /* temporary variable */
  373. unsigned n;
  374. uch *in, ch;
  375. flush_buffer(window, outcnt);
  376. in = window;
  377. for (n = 0; n < outcnt; n++) {
  378. ch = *in++;
  379. c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
  380. }
  381. crc = c;
  382. bytes_out += (ulg)outcnt;
  383. outcnt = 0;
  384. }
  385. static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
  386. {
  387. int written;
  388. dry_run = check_only;
  389. header_buf = malloc(110);
  390. symlink_buf = malloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1);
  391. name_buf = malloc(N_ALIGN(PATH_MAX));
  392. window = malloc(WSIZE);
  393. if (!window || !header_buf || !symlink_buf || !name_buf)
  394. panic("can't allocate buffers");
  395. state = Start;
  396. this_header = 0;
  397. message = NULL;
  398. while (!message && len) {
  399. loff_t saved_offset = this_header;
  400. if (*buf == '0' && !(this_header & 3)) {
  401. state = Start;
  402. written = write_buffer(buf, len);
  403. buf += written;
  404. len -= written;
  405. continue;
  406. }
  407. if (!*buf) {
  408. buf++;
  409. len--;
  410. this_header++;
  411. continue;
  412. }
  413. this_header = 0;
  414. insize = len;
  415. inbuf = buf;
  416. inptr = 0;
  417. outcnt = 0; /* bytes in output buffer */
  418. bytes_out = 0;
  419. crc = (ulg)0xffffffffL; /* shift register contents */
  420. makecrc();
  421. gunzip();
  422. if (state != Reset)
  423. error("junk in gzipped archive");
  424. this_header = saved_offset + inptr;
  425. buf += inptr;
  426. len -= inptr;
  427. }
  428. free(window);
  429. free(name_buf);
  430. free(symlink_buf);
  431. free(header_buf);
  432. return message;
  433. }
  434. extern char __initramfs_start[], __initramfs_end[];
  435. #ifdef CONFIG_BLK_DEV_INITRD
  436. #include <linux/initrd.h>
  437. #include <linux/kexec.h>
  438. static void __init free_initrd(void)
  439. {
  440. #ifdef CONFIG_KEXEC
  441. unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
  442. unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
  443. /*
  444. * If the initrd region is overlapped with crashkernel reserved region,
  445. * free only memory that is not part of crashkernel region.
  446. */
  447. if (initrd_start < crashk_end && initrd_end > crashk_start) {
  448. /*
  449. * Initialize initrd memory region since the kexec boot does
  450. * not do.
  451. */
  452. memset((void *)initrd_start, 0, initrd_end - initrd_start);
  453. if (initrd_start < crashk_start)
  454. free_initrd_mem(initrd_start, crashk_start);
  455. if (initrd_end > crashk_end)
  456. free_initrd_mem(crashk_end, initrd_end);
  457. } else
  458. #endif
  459. free_initrd_mem(initrd_start, initrd_end);
  460. initrd_start = 0;
  461. initrd_end = 0;
  462. }
  463. #endif
  464. void __init populate_rootfs(void)
  465. {
  466. char *err = unpack_to_rootfs(__initramfs_start,
  467. __initramfs_end - __initramfs_start, 0);
  468. if (err)
  469. panic(err);
  470. #ifdef CONFIG_BLK_DEV_INITRD
  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;
  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. #endif
  503. }