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>
9 #include <linux/utime.h>
11 static __initdata
char *message
;
12 static void __init
error(char *x
)
20 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
22 static __initdata
struct hash
{
23 int ino
, minor
, major
;
26 char name
[N_ALIGN(PATH_MAX
)];
29 static inline int hash(int major
, int minor
, int ino
)
31 unsigned long tmp
= ino
+ minor
+ (major
<< 3);
36 static char __init
*find_link(int major
, int minor
, int ino
,
37 mode_t mode
, char *name
)
40 for (p
= head
+ hash(major
, minor
, ino
); *p
; p
= &(*p
)->next
) {
43 if ((*p
)->minor
!= minor
)
45 if ((*p
)->major
!= major
)
47 if (((*p
)->mode
^ mode
) & S_IFMT
)
51 q
= kmalloc(sizeof(struct hash
), GFP_KERNEL
);
53 panic("can't allocate link hash entry");
58 strcpy(q
->name
, name
);
64 static void __init
free_hash(void)
67 for (p
= head
; p
< head
+ 32; p
++) {
76 static long __init
do_utime(char __user
*filename
, time_t mtime
)
85 return do_utimes(AT_FDCWD
, filename
, t
, AT_SYMLINK_NOFOLLOW
);
88 static __initdata
LIST_HEAD(dir_list
);
90 struct list_head list
;
95 static void __init
dir_add(const char *name
, time_t mtime
)
97 struct dir_entry
*de
= kmalloc(sizeof(struct dir_entry
), GFP_KERNEL
);
99 panic("can't allocate dir_entry buffer");
100 INIT_LIST_HEAD(&de
->list
);
101 de
->name
= kstrdup(name
, GFP_KERNEL
);
103 list_add(&de
->list
, &dir_list
);
106 static void __init
dir_utime(void)
108 struct dir_entry
*de
, *tmp
;
109 list_for_each_entry_safe(de
, tmp
, &dir_list
, list
) {
111 do_utime(de
->name
, de
->mtime
);
117 static __initdata
time_t mtime
;
119 /* cpio header parsing */
121 static __initdata
unsigned long ino
, major
, minor
, nlink
;
122 static __initdata mode_t mode
;
123 static __initdata
unsigned long body_len
, name_len
;
124 static __initdata uid_t uid
;
125 static __initdata gid_t gid
;
126 static __initdata
unsigned rdev
;
128 static void __init
parse_header(char *s
)
130 unsigned long parsed
[12];
135 for (i
= 0, s
+= 6; i
< 12; i
++, s
+= 8) {
137 parsed
[i
] = simple_strtoul(buf
, NULL
, 16);
145 body_len
= parsed
[6];
148 rdev
= new_encode_dev(MKDEV(parsed
[9], parsed
[10]));
149 name_len
= parsed
[11];
154 static __initdata
enum state
{
165 static __initdata
char *victim
;
166 static __initdata
unsigned count
;
167 static __initdata loff_t this_header
, next_header
;
169 static __initdata
int dry_run
;
171 static inline void __init
eat(unsigned n
)
178 static __initdata
char *vcollected
;
179 static __initdata
char *collected
;
180 static __initdata
int remains
;
181 static __initdata
char *collect
;
183 static void __init
read_into(char *buf
, unsigned size
, enum state next
)
190 collect
= collected
= buf
;
197 static __initdata
char *header_buf
, *symlink_buf
, *name_buf
;
199 static int __init
do_start(void)
201 read_into(header_buf
, 110, GotHeader
);
205 static int __init
do_collect(void)
207 unsigned n
= remains
;
210 memcpy(collect
, victim
, n
);
213 if ((remains
-= n
) != 0)
219 static int __init
do_header(void)
221 if (memcmp(collected
, "070707", 6)==0) {
222 error("incorrect cpio method used: use -H newc option");
225 if (memcmp(collected
, "070701", 6)) {
226 error("no cpio magic");
229 parse_header(collected
);
230 next_header
= this_header
+ N_ALIGN(name_len
) + body_len
;
231 next_header
= (next_header
+ 3) & ~3;
233 read_into(name_buf
, N_ALIGN(name_len
), GotName
);
237 if (name_len
<= 0 || name_len
> PATH_MAX
)
240 if (body_len
> PATH_MAX
)
242 collect
= collected
= symlink_buf
;
243 remains
= N_ALIGN(name_len
) + body_len
;
244 next_state
= GotSymlink
;
248 if (S_ISREG(mode
) || !body_len
)
249 read_into(name_buf
, N_ALIGN(name_len
), GotName
);
253 static int __init
do_skip(void)
255 if (this_header
+ count
< next_header
) {
259 eat(next_header
- this_header
);
265 static int __init
do_reset(void)
267 while(count
&& *victim
== '\0')
269 if (count
&& (this_header
& 3))
270 error("broken padding");
274 static int __init
maybe_link(void)
277 char *old
= find_link(major
, minor
, ino
, mode
, collected
);
279 return (sys_link(old
, collected
) < 0) ? -1 : 1;
284 static void __init
clean_path(char *path
, mode_t mode
)
288 if (!sys_newlstat(path
, &st
) && (st
.st_mode
^mode
) & S_IFMT
) {
289 if (S_ISDIR(st
.st_mode
))
296 static __initdata
int wfd
;
298 static int __init
do_name(void)
302 if (strcmp(collected
, "TRAILER!!!") == 0) {
308 clean_path(collected
, mode
);
310 int ml
= maybe_link();
312 int openflags
= O_WRONLY
|O_CREAT
;
314 openflags
|= O_TRUNC
;
315 wfd
= sys_open(collected
, openflags
, mode
);
318 sys_fchown(wfd
, uid
, gid
);
319 sys_fchmod(wfd
, mode
);
320 sys_ftruncate(wfd
, body_len
);
321 vcollected
= kstrdup(collected
, GFP_KERNEL
);
325 } else if (S_ISDIR(mode
)) {
326 sys_mkdir(collected
, mode
);
327 sys_chown(collected
, uid
, gid
);
328 sys_chmod(collected
, mode
);
329 dir_add(collected
, mtime
);
330 } else if (S_ISBLK(mode
) || S_ISCHR(mode
) ||
331 S_ISFIFO(mode
) || S_ISSOCK(mode
)) {
332 if (maybe_link() == 0) {
333 sys_mknod(collected
, mode
, rdev
);
334 sys_chown(collected
, uid
, gid
);
335 sys_chmod(collected
, mode
);
336 do_utime(collected
, mtime
);
342 static int __init
do_copy(void)
344 if (count
>= body_len
) {
345 sys_write(wfd
, victim
, body_len
);
347 do_utime(vcollected
, mtime
);
353 sys_write(wfd
, victim
, count
);
360 static int __init
do_symlink(void)
362 collected
[N_ALIGN(name_len
) + body_len
] = '\0';
363 clean_path(collected
, 0);
364 sys_symlink(collected
+ N_ALIGN(name_len
), collected
);
365 sys_lchown(collected
, uid
, gid
);
366 do_utime(collected
, mtime
);
372 static __initdata
int (*actions
[])(void) = {
374 [Collect
] = do_collect
,
375 [GotHeader
] = do_header
,
378 [CopyFile
] = do_copy
,
379 [GotSymlink
] = do_symlink
,
383 static int __init
write_buffer(char *buf
, unsigned len
)
388 while (!actions
[state
]())
393 static void __init
flush_buffer(char *buf
, unsigned len
)
398 while ((written
= write_buffer(buf
, len
)) < len
&& !message
) {
399 char c
= buf
[written
];
409 error("junk in compressed archive");
417 #define OF(args) args
420 #define memzero(s, n) memset ((s), 0, (n))
423 typedef unsigned char uch
;
424 typedef unsigned short ush
;
425 typedef unsigned long ulg
;
427 #define WSIZE 0x8000 /* window size--must be a power of two, and */
428 /* at least 32K for zip's deflate method */
433 static unsigned insize
; /* valid bytes in inbuf */
434 static unsigned inptr
; /* index of next byte to be processed in inbuf */
435 static unsigned outcnt
; /* bytes in output buffer */
436 static long bytes_out
;
438 #define get_byte() (inptr < insize ? inbuf[inptr++] : -1)
440 /* Diagnostic functions (stubbed out) */
441 #define Assert(cond,msg)
448 #define STATIC static
451 static void __init
flush_window(void);
452 static void __init
error(char *m
);
454 #define NO_INFLATE_MALLOC
456 #include "../lib/inflate.c"
458 /* ===========================================================================
459 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
460 * (Used for the decompressed data only.)
462 static void __init
flush_window(void)
464 ulg c
= crc
; /* temporary variable */
468 flush_buffer(window
, outcnt
);
470 for (n
= 0; n
< outcnt
; n
++) {
472 c
= crc_32_tab
[((int)c
^ ch
) & 0xff] ^ (c
>> 8);
475 bytes_out
+= (ulg
)outcnt
;
479 static char * __init
unpack_to_rootfs(char *buf
, unsigned len
, int check_only
)
482 dry_run
= check_only
;
483 header_buf
= kmalloc(110, GFP_KERNEL
);
484 symlink_buf
= kmalloc(PATH_MAX
+ N_ALIGN(PATH_MAX
) + 1, GFP_KERNEL
);
485 name_buf
= kmalloc(N_ALIGN(PATH_MAX
), GFP_KERNEL
);
486 window
= kmalloc(WSIZE
, GFP_KERNEL
);
487 if (!window
|| !header_buf
|| !symlink_buf
|| !name_buf
)
488 panic("can't allocate buffers");
492 while (!message
&& len
) {
493 loff_t saved_offset
= this_header
;
494 if (*buf
== '0' && !(this_header
& 3)) {
496 written
= write_buffer(buf
, len
);
511 outcnt
= 0; /* bytes in output buffer */
513 crc
= (ulg
)0xffffffffL
; /* shift register contents */
517 error("junk in gzipped archive");
518 this_header
= saved_offset
+ inptr
;
530 static int __initdata do_retain_initrd
;
532 static int __init
retain_initrd_param(char *str
)
536 do_retain_initrd
= 1;
539 __setup("retain_initrd", retain_initrd_param
);
541 extern char __initramfs_start
[], __initramfs_end
[];
542 #include <linux/initrd.h>
543 #include <linux/kexec.h>
545 static void __init
free_initrd(void)
548 unsigned long crashk_start
= (unsigned long)__va(crashk_res
.start
);
549 unsigned long crashk_end
= (unsigned long)__va(crashk_res
.end
);
551 if (do_retain_initrd
)
556 * If the initrd region is overlapped with crashkernel reserved region,
557 * free only memory that is not part of crashkernel region.
559 if (initrd_start
< crashk_end
&& initrd_end
> crashk_start
) {
561 * Initialize initrd memory region since the kexec boot does
564 memset((void *)initrd_start
, 0, initrd_end
- initrd_start
);
565 if (initrd_start
< crashk_start
)
566 free_initrd_mem(initrd_start
, crashk_start
);
567 if (initrd_end
> crashk_end
)
568 free_initrd_mem(crashk_end
, initrd_end
);
571 free_initrd_mem(initrd_start
, initrd_end
);
577 static int __init
populate_rootfs(void)
579 char *err
= unpack_to_rootfs(__initramfs_start
,
580 __initramfs_end
- __initramfs_start
, 0);
584 #ifdef CONFIG_BLK_DEV_RAM
586 printk(KERN_INFO
"checking if image is initramfs...");
587 err
= unpack_to_rootfs((char *)initrd_start
,
588 initrd_end
- initrd_start
, 1);
591 unpack_to_rootfs((char *)initrd_start
,
592 initrd_end
- initrd_start
, 0);
596 printk("it isn't (%s); looks like an initrd\n", err
);
597 fd
= sys_open("/initrd.image", O_WRONLY
|O_CREAT
, 0700);
599 sys_write(fd
, (char *)initrd_start
,
600 initrd_end
- initrd_start
);
605 printk(KERN_INFO
"Unpacking initramfs...");
606 err
= unpack_to_rootfs((char *)initrd_start
,
607 initrd_end
- initrd_start
, 0);
616 rootfs_initcall(populate_rootfs
);