initramfs.c 10 KB

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