2 * Many of the syscalls used in this file expect some of the arguments
3 * to be __user pointers not __kernel pointers. To limit the sparse
4 * noise, turn off sparse checking for this file.
8 #warning "Sparse checking disabled for this file"
11 #include <linux/init.h>
13 #include <linux/slab.h>
14 #include <linux/types.h>
15 #include <linux/fcntl.h>
16 #include <linux/delay.h>
17 #include <linux/string.h>
18 #include <linux/dirent.h>
19 #include <linux/syscalls.h>
20 #include <linux/utime.h>
21 #include <linux/file.h>
23 static ssize_t __init
xwrite(int fd
, const char *p
, size_t count
)
27 /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
29 ssize_t rv
= sys_write(fd
, p
, count
);
32 if (rv
== -EINTR
|| rv
== -EAGAIN
)
34 return out
? out
: rv
;
46 static __initdata
char *message
;
47 static void __init
error(char *x
)
55 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
57 static __initdata
struct hash
{
58 int ino
, minor
, major
;
61 char name
[N_ALIGN(PATH_MAX
)];
64 static inline int hash(int major
, int minor
, int ino
)
66 unsigned long tmp
= ino
+ minor
+ (major
<< 3);
71 static char __init
*find_link(int major
, int minor
, int ino
,
72 umode_t mode
, char *name
)
75 for (p
= head
+ hash(major
, minor
, ino
); *p
; p
= &(*p
)->next
) {
78 if ((*p
)->minor
!= minor
)
80 if ((*p
)->major
!= major
)
82 if (((*p
)->mode
^ mode
) & S_IFMT
)
86 q
= kmalloc(sizeof(struct hash
), GFP_KERNEL
);
88 panic("can't allocate link hash entry");
93 strcpy(q
->name
, name
);
99 static void __init
free_hash(void)
102 for (p
= head
; p
< head
+ 32; p
++) {
111 static long __init
do_utime(char *filename
, time_t mtime
)
113 struct timespec t
[2];
120 return do_utimes(AT_FDCWD
, filename
, t
, AT_SYMLINK_NOFOLLOW
);
123 static __initdata
LIST_HEAD(dir_list
);
125 struct list_head list
;
130 static void __init
dir_add(const char *name
, time_t mtime
)
132 struct dir_entry
*de
= kmalloc(sizeof(struct dir_entry
), GFP_KERNEL
);
134 panic("can't allocate dir_entry buffer");
135 INIT_LIST_HEAD(&de
->list
);
136 de
->name
= kstrdup(name
, GFP_KERNEL
);
138 list_add(&de
->list
, &dir_list
);
141 static void __init
dir_utime(void)
143 struct dir_entry
*de
, *tmp
;
144 list_for_each_entry_safe(de
, tmp
, &dir_list
, list
) {
146 do_utime(de
->name
, de
->mtime
);
152 static __initdata
time_t mtime
;
154 /* cpio header parsing */
156 static __initdata
unsigned long ino
, major
, minor
, nlink
;
157 static __initdata umode_t mode
;
158 static __initdata
unsigned long body_len
, name_len
;
159 static __initdata uid_t uid
;
160 static __initdata gid_t gid
;
161 static __initdata
unsigned rdev
;
163 static void __init
parse_header(char *s
)
165 unsigned long parsed
[12];
170 for (i
= 0, s
+= 6; i
< 12; i
++, s
+= 8) {
172 parsed
[i
] = simple_strtoul(buf
, NULL
, 16);
180 body_len
= parsed
[6];
183 rdev
= new_encode_dev(MKDEV(parsed
[9], parsed
[10]));
184 name_len
= parsed
[11];
189 static __initdata
enum state
{
200 static __initdata
char *victim
;
201 static unsigned long byte_count __initdata
;
202 static __initdata loff_t this_header
, next_header
;
204 static inline void __init
eat(unsigned n
)
211 static __initdata
char *vcollected
;
212 static __initdata
char *collected
;
213 static long remains __initdata
;
214 static __initdata
char *collect
;
216 static void __init
read_into(char *buf
, unsigned size
, enum state next
)
218 if (byte_count
>= size
) {
223 collect
= collected
= buf
;
230 static __initdata
char *header_buf
, *symlink_buf
, *name_buf
;
232 static int __init
do_start(void)
234 read_into(header_buf
, 110, GotHeader
);
238 static int __init
do_collect(void)
240 unsigned long n
= remains
;
243 memcpy(collect
, victim
, n
);
246 if ((remains
-= n
) != 0)
252 static int __init
do_header(void)
254 if (memcmp(collected
, "070707", 6)==0) {
255 error("incorrect cpio method used: use -H newc option");
258 if (memcmp(collected
, "070701", 6)) {
259 error("no cpio magic");
262 parse_header(collected
);
263 next_header
= this_header
+ N_ALIGN(name_len
) + body_len
;
264 next_header
= (next_header
+ 3) & ~3;
266 if (name_len
<= 0 || name_len
> PATH_MAX
)
269 if (body_len
> PATH_MAX
)
271 collect
= collected
= symlink_buf
;
272 remains
= N_ALIGN(name_len
) + body_len
;
273 next_state
= GotSymlink
;
277 if (S_ISREG(mode
) || !body_len
)
278 read_into(name_buf
, N_ALIGN(name_len
), GotName
);
282 static int __init
do_skip(void)
284 if (this_header
+ byte_count
< next_header
) {
288 eat(next_header
- this_header
);
294 static int __init
do_reset(void)
296 while (byte_count
&& *victim
== '\0')
298 if (byte_count
&& (this_header
& 3))
299 error("broken padding");
303 static int __init
maybe_link(void)
306 char *old
= find_link(major
, minor
, ino
, mode
, collected
);
308 return (sys_link(old
, collected
) < 0) ? -1 : 1;
313 static void __init
clean_path(char *path
, umode_t fmode
)
317 if (!vfs_lstat(path
, &st
) && (st
.mode
^ fmode
) & S_IFMT
) {
318 if (S_ISDIR(st
.mode
))
325 static __initdata
int wfd
;
327 static int __init
do_name(void)
331 if (strcmp(collected
, "TRAILER!!!") == 0) {
335 clean_path(collected
, mode
);
337 int ml
= maybe_link();
339 int openflags
= O_WRONLY
|O_CREAT
;
341 openflags
|= O_TRUNC
;
342 wfd
= sys_open(collected
, openflags
, mode
);
345 sys_fchown(wfd
, uid
, gid
);
346 sys_fchmod(wfd
, mode
);
348 sys_ftruncate(wfd
, body_len
);
349 vcollected
= kstrdup(collected
, GFP_KERNEL
);
353 } else if (S_ISDIR(mode
)) {
354 sys_mkdir(collected
, mode
);
355 sys_chown(collected
, uid
, gid
);
356 sys_chmod(collected
, mode
);
357 dir_add(collected
, mtime
);
358 } else if (S_ISBLK(mode
) || S_ISCHR(mode
) ||
359 S_ISFIFO(mode
) || S_ISSOCK(mode
)) {
360 if (maybe_link() == 0) {
361 sys_mknod(collected
, mode
, rdev
);
362 sys_chown(collected
, uid
, gid
);
363 sys_chmod(collected
, mode
);
364 do_utime(collected
, mtime
);
370 static int __init
do_copy(void)
372 if (byte_count
>= body_len
) {
373 if (xwrite(wfd
, victim
, body_len
) != body_len
)
374 error("write error");
376 do_utime(vcollected
, mtime
);
382 if (xwrite(wfd
, victim
, byte_count
) != byte_count
)
383 error("write error");
384 body_len
-= byte_count
;
390 static int __init
do_symlink(void)
392 collected
[N_ALIGN(name_len
) + body_len
] = '\0';
393 clean_path(collected
, 0);
394 sys_symlink(collected
+ N_ALIGN(name_len
), collected
);
395 sys_lchown(collected
, uid
, gid
);
396 do_utime(collected
, mtime
);
402 static __initdata
int (*actions
[])(void) = {
404 [Collect
] = do_collect
,
405 [GotHeader
] = do_header
,
408 [CopyFile
] = do_copy
,
409 [GotSymlink
] = do_symlink
,
413 static long __init
write_buffer(char *buf
, unsigned long len
)
418 while (!actions
[state
]())
420 return len
- byte_count
;
423 static long __init
flush_buffer(void *bufv
, unsigned long len
)
425 char *buf
= (char *) bufv
;
430 while ((written
= write_buffer(buf
, len
)) < len
&& !message
) {
431 char c
= buf
[written
];
441 error("junk in compressed archive");
446 static unsigned long my_inptr
; /* index of next byte to be processed in inbuf */
448 #include <linux/decompress/generic.h>
450 static char * __init
unpack_to_rootfs(char *buf
, unsigned long len
)
453 decompress_fn decompress
;
454 const char *compress_name
;
455 static __initdata
char msg_buf
[64];
457 header_buf
= kmalloc(110, GFP_KERNEL
);
458 symlink_buf
= kmalloc(PATH_MAX
+ N_ALIGN(PATH_MAX
) + 1, GFP_KERNEL
);
459 name_buf
= kmalloc(N_ALIGN(PATH_MAX
), GFP_KERNEL
);
461 if (!header_buf
|| !symlink_buf
|| !name_buf
)
462 panic("can't allocate buffers");
467 while (!message
&& len
) {
468 loff_t saved_offset
= this_header
;
469 if (*buf
== '0' && !(this_header
& 3)) {
471 written
= write_buffer(buf
, len
);
483 decompress
= decompress_method(buf
, len
, &compress_name
);
484 pr_debug("Detected %s compressed data\n", compress_name
);
486 int res
= decompress(buf
, len
, NULL
, flush_buffer
, NULL
,
489 error("decompressor failed");
490 } else if (compress_name
) {
492 snprintf(msg_buf
, sizeof msg_buf
,
493 "compression method %s not configured",
498 error("junk in compressed archive");
500 error("junk in compressed archive");
501 this_header
= saved_offset
+ my_inptr
;
512 static int __initdata do_retain_initrd
;
514 static int __init
retain_initrd_param(char *str
)
518 do_retain_initrd
= 1;
521 __setup("retain_initrd", retain_initrd_param
);
523 extern char __initramfs_start
[];
524 extern unsigned long __initramfs_size
;
525 #include <linux/initrd.h>
526 #include <linux/kexec.h>
528 static void __init
free_initrd(void)
530 #ifdef CONFIG_KEXEC_CORE
531 unsigned long crashk_start
= (unsigned long)__va(crashk_res
.start
);
532 unsigned long crashk_end
= (unsigned long)__va(crashk_res
.end
);
534 if (do_retain_initrd
)
537 #ifdef CONFIG_KEXEC_CORE
539 * If the initrd region is overlapped with crashkernel reserved region,
540 * free only memory that is not part of crashkernel region.
542 if (initrd_start
< crashk_end
&& initrd_end
> crashk_start
) {
544 * Initialize initrd memory region since the kexec boot does
547 memset((void *)initrd_start
, 0, initrd_end
- initrd_start
);
548 if (initrd_start
< crashk_start
)
549 free_initrd_mem(initrd_start
, crashk_start
);
550 if (initrd_end
> crashk_end
)
551 free_initrd_mem(crashk_end
, initrd_end
);
554 free_initrd_mem(initrd_start
, initrd_end
);
560 #ifdef CONFIG_BLK_DEV_RAM
561 #define BUF_SIZE 1024
562 static void __init
clean_rootfs(void)
566 struct linux_dirent64
*dirp
;
569 fd
= sys_open("/", O_RDONLY
, 0);
573 buf
= kzalloc(BUF_SIZE
, GFP_KERNEL
);
581 num
= sys_getdents64(fd
, dirp
, BUF_SIZE
);
587 ret
= vfs_lstat(dirp
->d_name
, &st
);
590 if (S_ISDIR(st
.mode
))
591 sys_rmdir(dirp
->d_name
);
593 sys_unlink(dirp
->d_name
);
596 num
-= dirp
->d_reclen
;
597 dirp
= (void *)dirp
+ dirp
->d_reclen
;
600 memset(buf
, 0, BUF_SIZE
);
601 num
= sys_getdents64(fd
, dirp
, BUF_SIZE
);
609 static int __init
populate_rootfs(void)
611 /* Load the built in initramfs */
612 char *err
= unpack_to_rootfs(__initramfs_start
, __initramfs_size
);
614 panic("%s", err
); /* Failed to decompress INTERNAL initramfs */
615 /* If available load the bootloader supplied initrd */
616 if (initrd_start
&& !IS_ENABLED(CONFIG_INITRAMFS_FORCE
)) {
617 #ifdef CONFIG_BLK_DEV_RAM
619 printk(KERN_INFO
"Trying to unpack rootfs image as initramfs...\n");
620 err
= unpack_to_rootfs((char *)initrd_start
,
621 initrd_end
- initrd_start
);
627 unpack_to_rootfs(__initramfs_start
, __initramfs_size
);
629 printk(KERN_INFO
"rootfs image is not initramfs (%s)"
630 "; looks like an initrd\n", err
);
631 fd
= sys_open("/initrd.image",
632 O_WRONLY
|O_CREAT
, 0700);
634 ssize_t written
= xwrite(fd
, (char *)initrd_start
,
635 initrd_end
- initrd_start
);
637 if (written
!= initrd_end
- initrd_start
)
638 pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
639 written
, initrd_end
- initrd_start
);
645 /* empty statement */;
647 printk(KERN_INFO
"Unpacking initramfs...\n");
648 err
= unpack_to_rootfs((char *)initrd_start
,
649 initrd_end
- initrd_start
);
651 printk(KERN_EMERG
"Initramfs unpacking failed: %s\n", err
);
655 flush_delayed_fput();
657 * Try loading default modules from initramfs. This gives
658 * us a chance to load before device_initcalls.
660 load_default_modules();
664 rootfs_initcall(populate_rootfs
);