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 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
31 static __initdata
struct hash
{
32 int ino
, minor
, major
;
35 char name
[N_ALIGN(PATH_MAX
)];
38 static inline int hash(int major
, int minor
, int ino
)
40 unsigned long tmp
= ino
+ minor
+ (major
<< 3);
45 static char __init
*find_link(int major
, int minor
, int ino
,
46 mode_t mode
, char *name
)
49 for (p
= head
+ hash(major
, minor
, ino
); *p
; p
= &(*p
)->next
) {
52 if ((*p
)->minor
!= minor
)
54 if ((*p
)->major
!= major
)
56 if (((*p
)->mode
^ mode
) & S_IFMT
)
60 q
= (struct hash
*)malloc(sizeof(struct hash
));
62 panic("can't allocate link hash entry");
67 strcpy(q
->name
, name
);
73 static void __init
free_hash(void)
76 for (p
= head
; p
< head
+ 32; p
++) {
85 /* cpio header parsing */
87 static __initdata
unsigned long ino
, major
, minor
, nlink
;
88 static __initdata mode_t mode
;
89 static __initdata
unsigned long body_len
, name_len
;
90 static __initdata uid_t uid
;
91 static __initdata gid_t gid
;
92 static __initdata
unsigned rdev
;
94 static void __init
parse_header(char *s
)
96 unsigned long parsed
[12];
101 for (i
= 0, s
+= 6; i
< 12; i
++, s
+= 8) {
103 parsed
[i
] = simple_strtoul(buf
, NULL
, 16);
110 body_len
= parsed
[6];
113 rdev
= new_encode_dev(MKDEV(parsed
[9], parsed
[10]));
114 name_len
= parsed
[11];
119 static __initdata
enum state
{
130 static __initdata
char *victim
;
131 static __initdata
unsigned count
;
132 static __initdata loff_t this_header
, next_header
;
134 static __initdata
int dry_run
;
136 static inline void eat(unsigned n
)
143 static __initdata
char *collected
;
144 static __initdata
int remains
;
145 static __initdata
char *collect
;
147 static void __init
read_into(char *buf
, unsigned size
, enum state next
)
154 collect
= collected
= buf
;
161 static __initdata
char *header_buf
, *symlink_buf
, *name_buf
;
163 static int __init
do_start(void)
165 read_into(header_buf
, 110, GotHeader
);
169 static int __init
do_collect(void)
171 unsigned n
= remains
;
174 memcpy(collect
, victim
, n
);
177 if ((remains
-= n
) != 0)
183 static int __init
do_header(void)
185 if (memcmp(collected
, "070707", 6)==0) {
186 error("incorrect cpio method used: use -H newc option");
189 if (memcmp(collected
, "070701", 6)) {
190 error("no cpio magic");
193 parse_header(collected
);
194 next_header
= this_header
+ N_ALIGN(name_len
) + body_len
;
195 next_header
= (next_header
+ 3) & ~3;
197 read_into(name_buf
, N_ALIGN(name_len
), GotName
);
201 if (name_len
<= 0 || name_len
> PATH_MAX
)
204 if (body_len
> PATH_MAX
)
206 collect
= collected
= symlink_buf
;
207 remains
= N_ALIGN(name_len
) + body_len
;
208 next_state
= GotSymlink
;
212 if (S_ISREG(mode
) || !body_len
)
213 read_into(name_buf
, N_ALIGN(name_len
), GotName
);
217 static int __init
do_skip(void)
219 if (this_header
+ count
< next_header
) {
223 eat(next_header
- this_header
);
229 static int __init
do_reset(void)
231 while(count
&& *victim
== '\0')
233 if (count
&& (this_header
& 3))
234 error("broken padding");
238 static int __init
maybe_link(void)
241 char *old
= find_link(major
, minor
, ino
, mode
, collected
);
243 return (sys_link(old
, collected
) < 0) ? -1 : 1;
248 static void __init
clean_path(char *path
, mode_t mode
)
252 if (!sys_newlstat(path
, &st
) && (st
.st_mode
^mode
) & S_IFMT
) {
253 if (S_ISDIR(st
.st_mode
))
260 static __initdata
int wfd
;
262 static int __init
do_name(void)
266 if (strcmp(collected
, "TRAILER!!!") == 0) {
272 clean_path(collected
, mode
);
274 int ml
= maybe_link();
276 int openflags
= O_WRONLY
|O_CREAT
;
278 openflags
|= O_TRUNC
;
279 wfd
= sys_open(collected
, openflags
, mode
);
282 sys_fchown(wfd
, uid
, gid
);
283 sys_fchmod(wfd
, mode
);
287 } else if (S_ISDIR(mode
)) {
288 sys_mkdir(collected
, mode
);
289 sys_chown(collected
, uid
, gid
);
290 sys_chmod(collected
, mode
);
291 } else if (S_ISBLK(mode
) || S_ISCHR(mode
) ||
292 S_ISFIFO(mode
) || S_ISSOCK(mode
)) {
293 if (maybe_link() == 0) {
294 sys_mknod(collected
, mode
, rdev
);
295 sys_chown(collected
, uid
, gid
);
296 sys_chmod(collected
, mode
);
302 static int __init
do_copy(void)
304 if (count
>= body_len
) {
305 sys_write(wfd
, victim
, body_len
);
311 sys_write(wfd
, victim
, count
);
318 static int __init
do_symlink(void)
320 collected
[N_ALIGN(name_len
) + body_len
] = '\0';
321 clean_path(collected
, 0);
322 sys_symlink(collected
+ N_ALIGN(name_len
), collected
);
323 sys_lchown(collected
, uid
, gid
);
329 static __initdata
int (*actions
[])(void) = {
331 [Collect
] = do_collect
,
332 [GotHeader
] = do_header
,
335 [CopyFile
] = do_copy
,
336 [GotSymlink
] = do_symlink
,
340 static int __init
write_buffer(char *buf
, unsigned len
)
345 while (!actions
[state
]())
350 static void __init
flush_buffer(char *buf
, unsigned len
)
355 while ((written
= write_buffer(buf
, len
)) < len
&& !message
) {
356 char c
= buf
[written
];
366 error("junk in compressed archive");
374 #define OF(args) args
377 #define memzero(s, n) memset ((s), 0, (n))
380 typedef unsigned char uch
;
381 typedef unsigned short ush
;
382 typedef unsigned long ulg
;
384 #define WSIZE 0x8000 /* window size--must be a power of two, and */
385 /* at least 32K for zip's deflate method */
390 static unsigned insize
; /* valid bytes in inbuf */
391 static unsigned inptr
; /* index of next byte to be processed in inbuf */
392 static unsigned outcnt
; /* bytes in output buffer */
393 static long bytes_out
;
395 #define get_byte() (inptr < insize ? inbuf[inptr++] : -1)
397 /* Diagnostic functions (stubbed out) */
398 #define Assert(cond,msg)
405 #define STATIC static
408 static void __init
flush_window(void);
409 static void __init
error(char *m
);
410 static void __init
gzip_mark(void **);
411 static void __init
gzip_release(void **);
413 #include "../lib/inflate.c"
415 static void __init
gzip_mark(void **ptr
)
419 static void __init
gzip_release(void **ptr
)
423 /* ===========================================================================
424 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
425 * (Used for the decompressed data only.)
427 static void __init
flush_window(void)
429 ulg c
= crc
; /* temporary variable */
433 flush_buffer(window
, outcnt
);
435 for (n
= 0; n
< outcnt
; n
++) {
437 c
= crc_32_tab
[((int)c
^ ch
) & 0xff] ^ (c
>> 8);
440 bytes_out
+= (ulg
)outcnt
;
444 static char * __init
unpack_to_rootfs(char *buf
, unsigned len
, int check_only
)
447 dry_run
= check_only
;
448 header_buf
= malloc(110);
449 symlink_buf
= malloc(PATH_MAX
+ N_ALIGN(PATH_MAX
) + 1);
450 name_buf
= malloc(N_ALIGN(PATH_MAX
));
451 window
= malloc(WSIZE
);
452 if (!window
|| !header_buf
|| !symlink_buf
|| !name_buf
)
453 panic("can't allocate buffers");
457 while (!message
&& len
) {
458 loff_t saved_offset
= this_header
;
459 if (*buf
== '0' && !(this_header
& 3)) {
461 written
= write_buffer(buf
, len
);
476 outcnt
= 0; /* bytes in output buffer */
478 crc
= (ulg
)0xffffffffL
; /* shift register contents */
482 error("junk in gzipped archive");
483 this_header
= saved_offset
+ inptr
;
494 extern char __initramfs_start
[], __initramfs_end
[];
495 #ifdef CONFIG_BLK_DEV_INITRD
496 #include <linux/initrd.h>
497 #include <linux/kexec.h>
499 static void __init
free_initrd(void)
502 unsigned long crashk_start
= (unsigned long)__va(crashk_res
.start
);
503 unsigned long crashk_end
= (unsigned long)__va(crashk_res
.end
);
506 * If the initrd region is overlapped with crashkernel reserved region,
507 * free only memory that is not part of crashkernel region.
509 if (initrd_start
< crashk_end
&& initrd_end
> crashk_start
) {
511 * Initialize initrd memory region since the kexec boot does
514 memset((void *)initrd_start
, 0, initrd_end
- initrd_start
);
515 if (initrd_start
< crashk_start
)
516 free_initrd_mem(initrd_start
, crashk_start
);
517 if (initrd_end
> crashk_end
)
518 free_initrd_mem(crashk_end
, initrd_end
);
521 free_initrd_mem(initrd_start
, initrd_end
);
529 void __init
populate_rootfs(void)
531 char *err
= unpack_to_rootfs(__initramfs_start
,
532 __initramfs_end
- __initramfs_start
, 0);
535 #ifdef CONFIG_BLK_DEV_INITRD
537 #ifdef CONFIG_BLK_DEV_RAM
539 printk(KERN_INFO
"checking if image is initramfs...");
540 err
= unpack_to_rootfs((char *)initrd_start
,
541 initrd_end
- initrd_start
, 1);
544 unpack_to_rootfs((char *)initrd_start
,
545 initrd_end
- initrd_start
, 0);
549 printk("it isn't (%s); looks like an initrd\n", err
);
550 fd
= sys_open("/initrd.image", O_WRONLY
|O_CREAT
, 0700);
552 sys_write(fd
, (char *)initrd_start
,
553 initrd_end
- initrd_start
);
558 printk(KERN_INFO
"Unpacking initramfs...");
559 err
= unpack_to_rootfs((char *)initrd_start
,
560 initrd_end
- initrd_start
, 0);