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)
17 static void __init *malloc(size_t size)
19 return kmalloc(size, GFP_KERNEL);
22 static void __init free(void *where)
29 static __initdata struct hash {
30 int ino, minor, major;
35 static inline int hash(int major, int minor, int ino)
37 unsigned long tmp = ino + minor + (major << 3);
42 static char __init *find_link(int major, int minor, int ino, char *name)
45 for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
48 if ((*p)->minor != minor)
50 if ((*p)->major != major)
54 q = (struct hash *)malloc(sizeof(struct hash));
56 panic("can't allocate link hash entry");
66 static void __init free_hash(void)
69 for (p = head; p < head + 32; p++) {
78 /* cpio header parsing */
80 static __initdata unsigned long ino, major, minor, nlink;
81 static __initdata mode_t mode;
82 static __initdata unsigned long body_len, name_len;
83 static __initdata uid_t uid;
84 static __initdata gid_t gid;
85 static __initdata unsigned rdev;
87 static void __init parse_header(char *s)
89 unsigned long parsed[12];
94 for (i = 0, s += 6; i < 12; i++, s += 8) {
96 parsed[i] = simple_strtoul(buf, NULL, 16);
103 body_len = parsed[6];
106 rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
107 name_len = parsed[11];
112 static __initdata enum state {
123 static __initdata char *victim;
124 static __initdata unsigned count;
125 static __initdata loff_t this_header, next_header;
127 static __initdata int dry_run;
129 static inline void eat(unsigned n)
136 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
138 static __initdata char *collected;
139 static __initdata int remains;
140 static __initdata char *collect;
142 static void __init read_into(char *buf, unsigned size, enum state next)
149 collect = collected = buf;
156 static __initdata char *header_buf, *symlink_buf, *name_buf;
158 static int __init do_start(void)
160 read_into(header_buf, 110, GotHeader);
164 static int __init do_collect(void)
166 unsigned n = remains;
169 memcpy(collect, victim, n);
172 if ((remains -= n) != 0)
178 static int __init do_header(void)
180 if (memcmp(collected, "070701", 6)) {
181 error("no cpio magic");
184 parse_header(collected);
185 next_header = this_header + N_ALIGN(name_len) + body_len;
186 next_header = (next_header + 3) & ~3;
188 read_into(name_buf, N_ALIGN(name_len), GotName);
192 if (name_len <= 0 || name_len > PATH_MAX)
195 if (body_len > PATH_MAX)
197 collect = collected = symlink_buf;
198 remains = N_ALIGN(name_len) + body_len;
199 next_state = GotSymlink;
203 if (S_ISREG(mode) || !body_len)
204 read_into(name_buf, N_ALIGN(name_len), GotName);
208 static int __init do_skip(void)
210 if (this_header + count < next_header) {
214 eat(next_header - this_header);
220 static int __init do_reset(void)
222 while(count && *victim == '\0')
224 if (count && (this_header & 3))
225 error("broken padding");
229 static int __init maybe_link(void)
232 char *old = find_link(major, minor, ino, collected);
234 return (sys_link(old, collected) < 0) ? -1 : 1;
239 static __initdata int wfd;
241 static int __init do_name(void)
245 if (strcmp(collected, "TRAILER!!!") == 0) {
252 sys_unlink(collected);
253 if (maybe_link() >= 0) {
254 wfd = sys_open(collected, O_WRONLY|O_CREAT, mode);
256 sys_fchown(wfd, uid, gid);
257 sys_fchmod(wfd, mode);
261 } else if (S_ISDIR(mode)) {
262 sys_mkdir(collected, mode);
263 sys_chown(collected, uid, gid);
264 sys_chmod(collected, mode);
265 } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
266 S_ISFIFO(mode) || S_ISSOCK(mode)) {
267 sys_unlink(collected);
268 if (maybe_link() == 0) {
269 sys_mknod(collected, mode, rdev);
270 sys_chown(collected, uid, gid);
271 sys_chmod(collected, mode);
277 static int __init do_copy(void)
279 if (count >= body_len) {
280 sys_write(wfd, victim, body_len);
286 sys_write(wfd, victim, count);
293 static int __init do_symlink(void)
295 collected[N_ALIGN(name_len) + body_len] = '\0';
296 sys_unlink(collected);
297 sys_symlink(collected + N_ALIGN(name_len), collected);
298 sys_lchown(collected, uid, gid);
304 static __initdata int (*actions[])(void) = {
306 [Collect] = do_collect,
307 [GotHeader] = do_header,
310 [CopyFile] = do_copy,
311 [GotSymlink] = do_symlink,
315 static int __init write_buffer(char *buf, unsigned len)
320 while (!actions[state]())
325 static void __init flush_buffer(char *buf, unsigned len)
330 while ((written = write_buffer(buf, len)) < len && !message) {
331 char c = buf[written];
341 error("junk in compressed archive");
349 #define OF(args) args
352 #define memzero(s, n) memset ((s), 0, (n))
355 typedef unsigned char uch;
356 typedef unsigned short ush;
357 typedef unsigned long ulg;
359 #define WSIZE 0x8000 /* window size--must be a power of two, and */
360 /* at least 32K for zip's deflate method */
365 static unsigned insize; /* valid bytes in inbuf */
366 static unsigned inptr; /* index of next byte to be processed in inbuf */
367 static unsigned outcnt; /* bytes in output buffer */
368 static long bytes_out;
370 #define get_byte() (inptr < insize ? inbuf[inptr++] : -1)
372 /* Diagnostic functions (stubbed out) */
373 #define Assert(cond,msg)
380 #define STATIC static
383 static void __init flush_window(void);
384 static void __init error(char *m);
385 static void __init gzip_mark(void **);
386 static void __init gzip_release(void **);
388 #include "../lib/inflate.c"
390 static void __init gzip_mark(void **ptr)
394 static void __init gzip_release(void **ptr)
398 /* ===========================================================================
399 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
400 * (Used for the decompressed data only.)
402 static void __init flush_window(void)
404 ulg c = crc; /* temporary variable */
408 flush_buffer(window, outcnt);
410 for (n = 0; n < outcnt; n++) {
412 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
415 bytes_out += (ulg)outcnt;
419 static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
422 dry_run = check_only;
423 header_buf = malloc(110);
424 symlink_buf = malloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1);
425 name_buf = malloc(N_ALIGN(PATH_MAX));
426 window = malloc(WSIZE);
427 if (!window || !header_buf || !symlink_buf || !name_buf)
428 panic("can't allocate buffers");
432 while (!message && len) {
433 loff_t saved_offset = this_header;
434 if (*buf == '0' && !(this_header & 3)) {
436 written = write_buffer(buf, len);
451 outcnt = 0; /* bytes in output buffer */
453 crc = (ulg)0xffffffffL; /* shift register contents */
457 error("junk in gzipped archive");
458 this_header = saved_offset + inptr;
469 extern char __initramfs_start[], __initramfs_end[];
470 #ifdef CONFIG_BLK_DEV_INITRD
471 #include <linux/initrd.h>
472 #include <linux/kexec.h>
474 static void __init free_initrd(void)
477 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
478 unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
481 * If the initrd region is overlapped with crashkernel reserved region,
482 * free only memory that is not part of crashkernel region.
484 if (initrd_start < crashk_end && initrd_end > crashk_start) {
486 * Initialize initrd memory region since the kexec boot does
489 memset((void *)initrd_start, 0, initrd_end - initrd_start);
490 if (initrd_start < crashk_start)
491 free_initrd_mem(initrd_start, crashk_start);
492 if (initrd_end > crashk_end)
493 free_initrd_mem(crashk_end, initrd_end);
496 free_initrd_mem(initrd_start, initrd_end);
504 void __init populate_rootfs(void)
506 char *err = unpack_to_rootfs(__initramfs_start,
507 __initramfs_end - __initramfs_start, 0);
510 #ifdef CONFIG_BLK_DEV_INITRD
513 printk(KERN_INFO "checking if image is initramfs...");
514 err = unpack_to_rootfs((char *)initrd_start,
515 initrd_end - initrd_start, 1);
518 unpack_to_rootfs((char *)initrd_start,
519 initrd_end - initrd_start, 0);
523 printk("it isn't (%s); looks like an initrd\n", err);
524 fd = sys_open("/initrd.image", O_WRONLY|O_CREAT, 700);
526 sys_write(fd, (char *)initrd_start,
527 initrd_end - initrd_start);