1 #include <linux/init.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>
10 static __initdata char *message;
11 static void __init error(char *x)
19 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
21 static __initdata struct hash {
22 int ino, minor, major;
25 char name[N_ALIGN(PATH_MAX)];
28 static inline int hash(int major, int minor, int ino)
30 unsigned long tmp = ino + minor + (major << 3);
35 static char __init *find_link(int major, int minor, int ino,
36 mode_t mode, char *name)
39 for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
42 if ((*p)->minor != minor)
44 if ((*p)->major != major)
46 if (((*p)->mode ^ mode) & S_IFMT)
50 q = kmalloc(sizeof(struct hash), GFP_KERNEL);
52 panic("can't allocate link hash entry");
57 strcpy(q->name, name);
63 static void __init free_hash(void)
66 for (p = head; p < head + 32; p++) {
75 /* cpio header parsing */
77 static __initdata unsigned long ino, major, minor, nlink;
78 static __initdata mode_t mode;
79 static __initdata unsigned long body_len, name_len;
80 static __initdata uid_t uid;
81 static __initdata gid_t gid;
82 static __initdata unsigned rdev;
84 static void __init parse_header(char *s)
86 unsigned long parsed[12];
91 for (i = 0, s += 6; i < 12; i++, s += 8) {
93 parsed[i] = simple_strtoul(buf, NULL, 16);
100 body_len = parsed[6];
103 rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
104 name_len = parsed[11];
109 static __initdata enum state {
120 static __initdata char *victim;
121 static __initdata unsigned count;
122 static __initdata loff_t this_header, next_header;
124 static __initdata int dry_run;
126 static inline void __init eat(unsigned n)
133 static __initdata char *collected;
134 static __initdata int remains;
135 static __initdata char *collect;
137 static void __init read_into(char *buf, unsigned size, enum state next)
144 collect = collected = buf;
151 static __initdata char *header_buf, *symlink_buf, *name_buf;
153 static int __init do_start(void)
155 read_into(header_buf, 110, GotHeader);
159 static int __init do_collect(void)
161 unsigned n = remains;
164 memcpy(collect, victim, n);
167 if ((remains -= n) != 0)
173 static int __init do_header(void)
175 if (memcmp(collected, "070707", 6)==0) {
176 error("incorrect cpio method used: use -H newc option");
179 if (memcmp(collected, "070701", 6)) {
180 error("no cpio magic");
183 parse_header(collected);
184 next_header = this_header + N_ALIGN(name_len) + body_len;
185 next_header = (next_header + 3) & ~3;
187 read_into(name_buf, N_ALIGN(name_len), GotName);
191 if (name_len <= 0 || name_len > PATH_MAX)
194 if (body_len > PATH_MAX)
196 collect = collected = symlink_buf;
197 remains = N_ALIGN(name_len) + body_len;
198 next_state = GotSymlink;
202 if (S_ISREG(mode) || !body_len)
203 read_into(name_buf, N_ALIGN(name_len), GotName);
207 static int __init do_skip(void)
209 if (this_header + count < next_header) {
213 eat(next_header - this_header);
219 static int __init do_reset(void)
221 while(count && *victim == '\0')
223 if (count && (this_header & 3))
224 error("broken padding");
228 static int __init maybe_link(void)
231 char *old = find_link(major, minor, ino, mode, collected);
233 return (sys_link(old, collected) < 0) ? -1 : 1;
238 static void __init clean_path(char *path, mode_t mode)
242 if (!sys_newlstat(path, &st) && (st.st_mode^mode) & S_IFMT) {
243 if (S_ISDIR(st.st_mode))
250 static __initdata int wfd;
252 static int __init do_name(void)
256 if (strcmp(collected, "TRAILER!!!") == 0) {
262 clean_path(collected, mode);
264 int ml = maybe_link();
266 int openflags = O_WRONLY|O_CREAT;
268 openflags |= O_TRUNC;
269 wfd = sys_open(collected, openflags, mode);
272 sys_fchown(wfd, uid, gid);
273 sys_fchmod(wfd, mode);
277 } else if (S_ISDIR(mode)) {
278 sys_mkdir(collected, mode);
279 sys_chown(collected, uid, gid);
280 sys_chmod(collected, mode);
281 } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
282 S_ISFIFO(mode) || S_ISSOCK(mode)) {
283 if (maybe_link() == 0) {
284 sys_mknod(collected, mode, rdev);
285 sys_chown(collected, uid, gid);
286 sys_chmod(collected, mode);
292 static int __init do_copy(void)
294 if (count >= body_len) {
295 sys_write(wfd, victim, body_len);
301 sys_write(wfd, victim, count);
308 static int __init do_symlink(void)
310 collected[N_ALIGN(name_len) + body_len] = '\0';
311 clean_path(collected, 0);
312 sys_symlink(collected + N_ALIGN(name_len), collected);
313 sys_lchown(collected, uid, gid);
319 static __initdata int (*actions[])(void) = {
321 [Collect] = do_collect,
322 [GotHeader] = do_header,
325 [CopyFile] = do_copy,
326 [GotSymlink] = do_symlink,
330 static int __init write_buffer(char *buf, unsigned len)
335 while (!actions[state]())
340 static void __init flush_buffer(char *buf, unsigned len)
345 while ((written = write_buffer(buf, len)) < len && !message) {
346 char c = buf[written];
356 error("junk in compressed archive");
364 #define OF(args) args
367 #define memzero(s, n) memset ((s), 0, (n))
370 typedef unsigned char uch;
371 typedef unsigned short ush;
372 typedef unsigned long ulg;
374 #define WSIZE 0x8000 /* window size--must be a power of two, and */
375 /* at least 32K for zip's deflate method */
380 static unsigned insize; /* valid bytes in inbuf */
381 static unsigned inptr; /* index of next byte to be processed in inbuf */
382 static unsigned outcnt; /* bytes in output buffer */
383 static long bytes_out;
385 #define get_byte() (inptr < insize ? inbuf[inptr++] : -1)
387 /* Diagnostic functions (stubbed out) */
388 #define Assert(cond,msg)
395 #define STATIC static
398 static void __init flush_window(void);
399 static void __init error(char *m);
401 #define NO_INFLATE_MALLOC
403 #include "../lib/inflate.c"
405 /* ===========================================================================
406 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
407 * (Used for the decompressed data only.)
409 static void __init flush_window(void)
411 ulg c = crc; /* temporary variable */
415 flush_buffer(window, outcnt);
417 for (n = 0; n < outcnt; n++) {
419 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
422 bytes_out += (ulg)outcnt;
426 static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
429 dry_run = check_only;
430 header_buf = kmalloc(110, GFP_KERNEL);
431 symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
432 name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
433 window = kmalloc(WSIZE, GFP_KERNEL);
434 if (!window || !header_buf || !symlink_buf || !name_buf)
435 panic("can't allocate buffers");
439 while (!message && len) {
440 loff_t saved_offset = this_header;
441 if (*buf == '0' && !(this_header & 3)) {
443 written = write_buffer(buf, len);
458 outcnt = 0; /* bytes in output buffer */
460 crc = (ulg)0xffffffffL; /* shift register contents */
464 error("junk in gzipped archive");
465 this_header = saved_offset + inptr;
476 static int __initdata do_retain_initrd;
478 static int __init retain_initrd_param(char *str)
482 do_retain_initrd = 1;
485 __setup("retain_initrd", retain_initrd_param);
487 extern char __initramfs_start[], __initramfs_end[];
488 #include <linux/initrd.h>
489 #include <linux/kexec.h>
491 static void __init free_initrd(void)
494 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
495 unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
497 if (do_retain_initrd)
502 * If the initrd region is overlapped with crashkernel reserved region,
503 * free only memory that is not part of crashkernel region.
505 if (initrd_start < crashk_end && initrd_end > crashk_start) {
507 * Initialize initrd memory region since the kexec boot does
510 memset((void *)initrd_start, 0, initrd_end - initrd_start);
511 if (initrd_start < crashk_start)
512 free_initrd_mem(initrd_start, crashk_start);
513 if (initrd_end > crashk_end)
514 free_initrd_mem(crashk_end, initrd_end);
517 free_initrd_mem(initrd_start, initrd_end);
523 static int __init populate_rootfs(void)
525 char *err = unpack_to_rootfs(__initramfs_start,
526 __initramfs_end - __initramfs_start, 0);
530 #ifdef CONFIG_BLK_DEV_RAM
532 printk(KERN_INFO "checking if image is initramfs...");
533 err = unpack_to_rootfs((char *)initrd_start,
534 initrd_end - initrd_start, 1);
537 unpack_to_rootfs((char *)initrd_start,
538 initrd_end - initrd_start, 0);
542 printk("it isn't (%s); looks like an initrd\n", err);
543 fd = sys_open("/initrd.image", O_WRONLY|O_CREAT, 0700);
545 sys_write(fd, (char *)initrd_start,
546 initrd_end - initrd_start);
551 printk(KERN_INFO "Unpacking initramfs...");
552 err = unpack_to_rootfs((char *)initrd_start,
553 initrd_end - initrd_start, 0);
562 rootfs_initcall(populate_rootfs);