1 // SPDX-License-Identifier: GPL-2.0
3 * Many of the syscalls used in this file expect some of the arguments
4 * to be __user pointers not __kernel pointers. To limit the sparse
5 * noise, turn off sparse checking for this file.
9 #warning "Sparse checking disabled for this file"
12 #include <linux/init.h>
14 #include <linux/slab.h>
15 #include <linux/types.h>
16 #include <linux/fcntl.h>
17 #include <linux/delay.h>
18 #include <linux/string.h>
19 #include <linux/dirent.h>
20 #include <linux/syscalls.h>
21 #include <linux/utime.h>
22 #include <linux/file.h>
23 #include <linux/initramfs.h>
25 static ssize_t __init
xwrite(int fd
, const char *p
, size_t count
)
29 /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
31 ssize_t rv
= sys_write(fd
, p
, count
);
34 if (rv
== -EINTR
|| rv
== -EAGAIN
)
36 return out
? out
: rv
;
48 static __initdata
char *message
;
49 static void __init
error(char *x
)
57 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
59 static __initdata
struct hash
{
60 int ino
, minor
, major
;
63 char name
[N_ALIGN(PATH_MAX
)];
66 static inline int hash(int major
, int minor
, int ino
)
68 unsigned long tmp
= ino
+ minor
+ (major
<< 3);
73 static char __init
*find_link(int major
, int minor
, int ino
,
74 umode_t mode
, char *name
)
77 for (p
= head
+ hash(major
, minor
, ino
); *p
; p
= &(*p
)->next
) {
80 if ((*p
)->minor
!= minor
)
82 if ((*p
)->major
!= major
)
84 if (((*p
)->mode
^ mode
) & S_IFMT
)
88 q
= kmalloc(sizeof(struct hash
), GFP_KERNEL
);
90 panic("can't allocate link hash entry");
95 strcpy(q
->name
, name
);
101 static void __init
free_hash(void)
104 for (p
= head
; p
< head
+ 32; p
++) {
113 static long __init
do_utime(char *filename
, time_t mtime
)
115 struct timespec64 t
[2];
122 return do_utimes(AT_FDCWD
, filename
, t
, AT_SYMLINK_NOFOLLOW
);
125 static __initdata
LIST_HEAD(dir_list
);
127 struct list_head list
;
132 static void __init
dir_add(const char *name
, time_t mtime
)
134 struct dir_entry
*de
= kmalloc(sizeof(struct dir_entry
), GFP_KERNEL
);
136 panic("can't allocate dir_entry buffer");
137 INIT_LIST_HEAD(&de
->list
);
138 de
->name
= kstrdup(name
, GFP_KERNEL
);
140 list_add(&de
->list
, &dir_list
);
143 static void __init
dir_utime(void)
145 struct dir_entry
*de
, *tmp
;
146 list_for_each_entry_safe(de
, tmp
, &dir_list
, list
) {
148 do_utime(de
->name
, de
->mtime
);
154 static __initdata
time_t mtime
;
156 /* cpio header parsing */
158 static __initdata
unsigned long ino
, major
, minor
, nlink
;
159 static __initdata umode_t mode
;
160 static __initdata
unsigned long body_len
, name_len
;
161 static __initdata uid_t uid
;
162 static __initdata gid_t gid
;
163 static __initdata
unsigned rdev
;
165 static void __init
parse_header(char *s
)
167 unsigned long parsed
[12];
172 for (i
= 0, s
+= 6; i
< 12; i
++, s
+= 8) {
174 parsed
[i
] = simple_strtoul(buf
, NULL
, 16);
182 body_len
= parsed
[6];
185 rdev
= new_encode_dev(MKDEV(parsed
[9], parsed
[10]));
186 name_len
= parsed
[11];
191 static __initdata
enum state
{
202 static __initdata
char *victim
;
203 static unsigned long byte_count __initdata
;
204 static __initdata loff_t this_header
, next_header
;
206 static inline void __init
eat(unsigned n
)
213 static __initdata
char *vcollected
;
214 static __initdata
char *collected
;
215 static long remains __initdata
;
216 static __initdata
char *collect
;
218 static void __init
read_into(char *buf
, unsigned size
, enum state next
)
220 if (byte_count
>= size
) {
225 collect
= collected
= buf
;
232 static __initdata
char *header_buf
, *symlink_buf
, *name_buf
;
234 static int __init
do_start(void)
236 read_into(header_buf
, 110, GotHeader
);
240 static int __init
do_collect(void)
242 unsigned long n
= remains
;
245 memcpy(collect
, victim
, n
);
248 if ((remains
-= n
) != 0)
254 static int __init
do_header(void)
256 if (memcmp(collected
, "070707", 6)==0) {
257 error("incorrect cpio method used: use -H newc option");
260 if (memcmp(collected
, "070701", 6)) {
261 error("no cpio magic");
264 parse_header(collected
);
265 next_header
= this_header
+ N_ALIGN(name_len
) + body_len
;
266 next_header
= (next_header
+ 3) & ~3;
268 if (name_len
<= 0 || name_len
> PATH_MAX
)
271 if (body_len
> PATH_MAX
)
273 collect
= collected
= symlink_buf
;
274 remains
= N_ALIGN(name_len
) + body_len
;
275 next_state
= GotSymlink
;
279 if (S_ISREG(mode
) || !body_len
)
280 read_into(name_buf
, N_ALIGN(name_len
), GotName
);
284 static int __init
do_skip(void)
286 if (this_header
+ byte_count
< next_header
) {
290 eat(next_header
- this_header
);
296 static int __init
do_reset(void)
298 while (byte_count
&& *victim
== '\0')
300 if (byte_count
&& (this_header
& 3))
301 error("broken padding");
305 static int __init
maybe_link(void)
308 char *old
= find_link(major
, minor
, ino
, mode
, collected
);
310 return (sys_link(old
, collected
) < 0) ? -1 : 1;
315 static void __init
clean_path(char *path
, umode_t fmode
)
319 if (!vfs_lstat(path
, &st
) && (st
.mode
^ fmode
) & S_IFMT
) {
320 if (S_ISDIR(st
.mode
))
327 static __initdata
int wfd
;
329 static int __init
do_name(void)
333 if (strcmp(collected
, "TRAILER!!!") == 0) {
337 clean_path(collected
, mode
);
339 int ml
= maybe_link();
341 int openflags
= O_WRONLY
|O_CREAT
;
343 openflags
|= O_TRUNC
;
344 wfd
= sys_open(collected
, openflags
, mode
);
347 sys_fchown(wfd
, uid
, gid
);
348 sys_fchmod(wfd
, mode
);
350 sys_ftruncate(wfd
, body_len
);
351 vcollected
= kstrdup(collected
, GFP_KERNEL
);
355 } else if (S_ISDIR(mode
)) {
356 sys_mkdir(collected
, mode
);
357 sys_chown(collected
, uid
, gid
);
358 sys_chmod(collected
, mode
);
359 dir_add(collected
, mtime
);
360 } else if (S_ISBLK(mode
) || S_ISCHR(mode
) ||
361 S_ISFIFO(mode
) || S_ISSOCK(mode
)) {
362 if (maybe_link() == 0) {
363 sys_mknod(collected
, mode
, rdev
);
364 sys_chown(collected
, uid
, gid
);
365 sys_chmod(collected
, mode
);
366 do_utime(collected
, mtime
);
372 static int __init
do_copy(void)
374 if (byte_count
>= body_len
) {
375 if (xwrite(wfd
, victim
, body_len
) != body_len
)
376 error("write error");
378 do_utime(vcollected
, mtime
);
384 if (xwrite(wfd
, victim
, byte_count
) != byte_count
)
385 error("write error");
386 body_len
-= byte_count
;
392 static int __init
do_symlink(void)
394 collected
[N_ALIGN(name_len
) + body_len
] = '\0';
395 clean_path(collected
, 0);
396 sys_symlink(collected
+ N_ALIGN(name_len
), collected
);
397 sys_lchown(collected
, uid
, gid
);
398 do_utime(collected
, mtime
);
404 static __initdata
int (*actions
[])(void) = {
406 [Collect
] = do_collect
,
407 [GotHeader
] = do_header
,
410 [CopyFile
] = do_copy
,
411 [GotSymlink
] = do_symlink
,
415 static long __init
write_buffer(char *buf
, unsigned long len
)
420 while (!actions
[state
]())
422 return len
- byte_count
;
425 static long __init
flush_buffer(void *bufv
, unsigned long len
)
427 char *buf
= (char *) bufv
;
432 while ((written
= write_buffer(buf
, len
)) < len
&& !message
) {
433 char c
= buf
[written
];
443 error("junk in compressed archive");
448 static unsigned long my_inptr
; /* index of next byte to be processed in inbuf */
450 #include <linux/decompress/generic.h>
452 static char * __init
unpack_to_rootfs(char *buf
, unsigned long len
)
455 decompress_fn decompress
;
456 const char *compress_name
;
457 static __initdata
char msg_buf
[64];
459 header_buf
= kmalloc(110, GFP_KERNEL
);
460 symlink_buf
= kmalloc(PATH_MAX
+ N_ALIGN(PATH_MAX
) + 1, GFP_KERNEL
);
461 name_buf
= kmalloc(N_ALIGN(PATH_MAX
), GFP_KERNEL
);
463 if (!header_buf
|| !symlink_buf
|| !name_buf
)
464 panic("can't allocate buffers");
469 while (!message
&& len
) {
470 loff_t saved_offset
= this_header
;
471 if (*buf
== '0' && !(this_header
& 3)) {
473 written
= write_buffer(buf
, len
);
485 decompress
= decompress_method(buf
, len
, &compress_name
);
486 pr_debug("Detected %s compressed data\n", compress_name
);
488 int res
= decompress(buf
, len
, NULL
, flush_buffer
, NULL
,
491 error("decompressor failed");
492 } else if (compress_name
) {
494 snprintf(msg_buf
, sizeof msg_buf
,
495 "compression method %s not configured",
500 error("junk in compressed archive");
502 error("junk in compressed archive");
503 this_header
= saved_offset
+ my_inptr
;
514 static int __initdata do_retain_initrd
;
516 static int __init
retain_initrd_param(char *str
)
520 do_retain_initrd
= 1;
523 __setup("retain_initrd", retain_initrd_param
);
525 extern char __initramfs_start
[];
526 extern unsigned long __initramfs_size
;
527 #include <linux/initrd.h>
528 #include <linux/kexec.h>
530 static void __init
free_initrd(void)
532 #ifdef CONFIG_KEXEC_CORE
533 unsigned long crashk_start
= (unsigned long)__va(crashk_res
.start
);
534 unsigned long crashk_end
= (unsigned long)__va(crashk_res
.end
);
536 if (do_retain_initrd
)
539 #ifdef CONFIG_KEXEC_CORE
541 * If the initrd region is overlapped with crashkernel reserved region,
542 * free only memory that is not part of crashkernel region.
544 if (initrd_start
< crashk_end
&& initrd_end
> crashk_start
) {
546 * Initialize initrd memory region since the kexec boot does
549 memset((void *)initrd_start
, 0, initrd_end
- initrd_start
);
550 if (initrd_start
< crashk_start
)
551 free_initrd_mem(initrd_start
, crashk_start
);
552 if (initrd_end
> crashk_end
)
553 free_initrd_mem(crashk_end
, initrd_end
);
556 free_initrd_mem(initrd_start
, initrd_end
);
562 #ifdef CONFIG_BLK_DEV_RAM
563 #define BUF_SIZE 1024
564 static void __init
clean_rootfs(void)
568 struct linux_dirent64
*dirp
;
571 fd
= sys_open("/", O_RDONLY
, 0);
575 buf
= kzalloc(BUF_SIZE
, GFP_KERNEL
);
583 num
= sys_getdents64(fd
, dirp
, BUF_SIZE
);
589 ret
= vfs_lstat(dirp
->d_name
, &st
);
592 if (S_ISDIR(st
.mode
))
593 sys_rmdir(dirp
->d_name
);
595 sys_unlink(dirp
->d_name
);
598 num
-= dirp
->d_reclen
;
599 dirp
= (void *)dirp
+ dirp
->d_reclen
;
602 memset(buf
, 0, BUF_SIZE
);
603 num
= sys_getdents64(fd
, dirp
, BUF_SIZE
);
611 static int __initdata do_skip_initramfs
;
613 static int __init
skip_initramfs_param(char *str
)
617 do_skip_initramfs
= 1;
620 __setup("skip_initramfs", skip_initramfs_param
);
622 static int __init
populate_rootfs(void)
626 if (do_skip_initramfs
) {
629 return default_rootfs();
632 /* Load the built in initramfs */
633 err
= unpack_to_rootfs(__initramfs_start
, __initramfs_size
);
635 panic("%s", err
); /* Failed to decompress INTERNAL initramfs */
636 /* If available load the bootloader supplied initrd */
637 if (initrd_start
&& !IS_ENABLED(CONFIG_INITRAMFS_FORCE
)) {
638 #ifdef CONFIG_BLK_DEV_RAM
640 printk(KERN_INFO
"Trying to unpack rootfs image as initramfs...\n");
641 err
= unpack_to_rootfs((char *)initrd_start
,
642 initrd_end
- initrd_start
);
648 unpack_to_rootfs(__initramfs_start
, __initramfs_size
);
650 printk(KERN_INFO
"rootfs image is not initramfs (%s)"
651 "; looks like an initrd\n", err
);
652 fd
= sys_open("/initrd.image",
653 O_WRONLY
|O_CREAT
, 0700);
655 ssize_t written
= xwrite(fd
, (char *)initrd_start
,
656 initrd_end
- initrd_start
);
658 if (written
!= initrd_end
- initrd_start
)
659 pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
660 written
, initrd_end
- initrd_start
);
666 /* empty statement */;
668 printk(KERN_INFO
"Unpacking initramfs...\n");
669 err
= unpack_to_rootfs((char *)initrd_start
,
670 initrd_end
- initrd_start
);
672 printk(KERN_EMERG
"Initramfs unpacking failed: %s\n", err
);
676 flush_delayed_fput();
678 * Try loading default modules from initramfs. This gives
679 * us a chance to load before device_initcalls.
681 load_default_modules();
685 rootfs_initcall(populate_rootfs
);