1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/init.h>
4 #include <linux/slab.h>
5 #include <linux/types.h>
6 #include <linux/fcntl.h>
7 #include <linux/delay.h>
8 #include <linux/string.h>
9 #include <linux/dirent.h>
10 #include <linux/syscalls.h>
11 #include <linux/utime.h>
12 #include <linux/file.h>
13 #include <linux/memblock.h>
15 static ssize_t __init
xwrite(int fd
, const char *p
, size_t count
)
19 /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
21 ssize_t rv
= ksys_write(fd
, p
, count
);
24 if (rv
== -EINTR
|| rv
== -EAGAIN
)
26 return out
? out
: rv
;
38 static __initdata
char *message
;
39 static void __init
error(char *x
)
47 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
49 static __initdata
struct hash
{
50 int ino
, minor
, major
;
53 char name
[N_ALIGN(PATH_MAX
)];
56 static inline int hash(int major
, int minor
, int ino
)
58 unsigned long tmp
= ino
+ minor
+ (major
<< 3);
63 static char __init
*find_link(int major
, int minor
, int ino
,
64 umode_t mode
, char *name
)
67 for (p
= head
+ hash(major
, minor
, ino
); *p
; p
= &(*p
)->next
) {
70 if ((*p
)->minor
!= minor
)
72 if ((*p
)->major
!= major
)
74 if (((*p
)->mode
^ mode
) & S_IFMT
)
78 q
= kmalloc(sizeof(struct hash
), GFP_KERNEL
);
80 panic("can't allocate link hash entry");
85 strcpy(q
->name
, name
);
91 static void __init
free_hash(void)
94 for (p
= head
; p
< head
+ 32; p
++) {
103 static long __init
do_utime(char *filename
, time64_t mtime
)
105 struct timespec64 t
[2];
112 return do_utimes(AT_FDCWD
, filename
, t
, AT_SYMLINK_NOFOLLOW
);
115 static __initdata
LIST_HEAD(dir_list
);
117 struct list_head list
;
122 static void __init
dir_add(const char *name
, time64_t mtime
)
124 struct dir_entry
*de
= kmalloc(sizeof(struct dir_entry
), GFP_KERNEL
);
126 panic("can't allocate dir_entry buffer");
127 INIT_LIST_HEAD(&de
->list
);
128 de
->name
= kstrdup(name
, GFP_KERNEL
);
130 list_add(&de
->list
, &dir_list
);
133 static void __init
dir_utime(void)
135 struct dir_entry
*de
, *tmp
;
136 list_for_each_entry_safe(de
, tmp
, &dir_list
, list
) {
138 do_utime(de
->name
, de
->mtime
);
144 static __initdata time64_t mtime
;
146 /* cpio header parsing */
148 static __initdata
unsigned long ino
, major
, minor
, nlink
;
149 static __initdata umode_t mode
;
150 static __initdata
unsigned long body_len
, name_len
;
151 static __initdata uid_t uid
;
152 static __initdata gid_t gid
;
153 static __initdata
unsigned rdev
;
155 static void __init
parse_header(char *s
)
157 unsigned long parsed
[12];
162 for (i
= 0, s
+= 6; i
< 12; i
++, s
+= 8) {
164 parsed
[i
] = simple_strtoul(buf
, NULL
, 16);
171 mtime
= parsed
[5]; /* breaks in y2106 */
172 body_len
= parsed
[6];
175 rdev
= new_encode_dev(MKDEV(parsed
[9], parsed
[10]));
176 name_len
= parsed
[11];
181 static __initdata
enum state
{
192 static __initdata
char *victim
;
193 static unsigned long byte_count __initdata
;
194 static __initdata loff_t this_header
, next_header
;
196 static inline void __init
eat(unsigned n
)
203 static __initdata
char *vcollected
;
204 static __initdata
char *collected
;
205 static long remains __initdata
;
206 static __initdata
char *collect
;
208 static void __init
read_into(char *buf
, unsigned size
, enum state next
)
210 if (byte_count
>= size
) {
215 collect
= collected
= buf
;
222 static __initdata
char *header_buf
, *symlink_buf
, *name_buf
;
224 static int __init
do_start(void)
226 read_into(header_buf
, 110, GotHeader
);
230 static int __init
do_collect(void)
232 unsigned long n
= remains
;
235 memcpy(collect
, victim
, n
);
238 if ((remains
-= n
) != 0)
244 static int __init
do_header(void)
246 if (memcmp(collected
, "070707", 6)==0) {
247 error("incorrect cpio method used: use -H newc option");
250 if (memcmp(collected
, "070701", 6)) {
251 error("no cpio magic");
254 parse_header(collected
);
255 next_header
= this_header
+ N_ALIGN(name_len
) + body_len
;
256 next_header
= (next_header
+ 3) & ~3;
258 if (name_len
<= 0 || name_len
> PATH_MAX
)
261 if (body_len
> PATH_MAX
)
263 collect
= collected
= symlink_buf
;
264 remains
= N_ALIGN(name_len
) + body_len
;
265 next_state
= GotSymlink
;
269 if (S_ISREG(mode
) || !body_len
)
270 read_into(name_buf
, N_ALIGN(name_len
), GotName
);
274 static int __init
do_skip(void)
276 if (this_header
+ byte_count
< next_header
) {
280 eat(next_header
- this_header
);
286 static int __init
do_reset(void)
288 while (byte_count
&& *victim
== '\0')
290 if (byte_count
&& (this_header
& 3))
291 error("broken padding");
295 static void __init
clean_path(char *path
, umode_t fmode
)
299 if (!vfs_lstat(path
, &st
) && (st
.mode
^ fmode
) & S_IFMT
) {
300 if (S_ISDIR(st
.mode
))
307 static int __init
maybe_link(void)
310 char *old
= find_link(major
, minor
, ino
, mode
, collected
);
312 clean_path(collected
, 0);
313 return (ksys_link(old
, collected
) < 0) ? -1 : 1;
319 static __initdata
int wfd
;
321 static int __init
do_name(void)
325 if (strcmp(collected
, "TRAILER!!!") == 0) {
329 clean_path(collected
, mode
);
331 int ml
= maybe_link();
333 int openflags
= O_WRONLY
|O_CREAT
;
335 openflags
|= O_TRUNC
;
336 wfd
= ksys_open(collected
, openflags
, mode
);
339 ksys_fchown(wfd
, uid
, gid
);
340 ksys_fchmod(wfd
, mode
);
342 ksys_ftruncate(wfd
, body_len
);
343 vcollected
= kstrdup(collected
, GFP_KERNEL
);
347 } else if (S_ISDIR(mode
)) {
348 ksys_mkdir(collected
, mode
);
349 ksys_chown(collected
, uid
, gid
);
350 ksys_chmod(collected
, mode
);
351 dir_add(collected
, mtime
);
352 } else if (S_ISBLK(mode
) || S_ISCHR(mode
) ||
353 S_ISFIFO(mode
) || S_ISSOCK(mode
)) {
354 if (maybe_link() == 0) {
355 ksys_mknod(collected
, mode
, rdev
);
356 ksys_chown(collected
, uid
, gid
);
357 ksys_chmod(collected
, mode
);
358 do_utime(collected
, mtime
);
364 static int __init
do_copy(void)
366 if (byte_count
>= body_len
) {
367 if (xwrite(wfd
, victim
, body_len
) != body_len
)
368 error("write error");
370 do_utime(vcollected
, mtime
);
376 if (xwrite(wfd
, victim
, byte_count
) != byte_count
)
377 error("write error");
378 body_len
-= byte_count
;
384 static int __init
do_symlink(void)
386 collected
[N_ALIGN(name_len
) + body_len
] = '\0';
387 clean_path(collected
, 0);
388 ksys_symlink(collected
+ N_ALIGN(name_len
), collected
);
389 ksys_lchown(collected
, uid
, gid
);
390 do_utime(collected
, mtime
);
396 static __initdata
int (*actions
[])(void) = {
398 [Collect
] = do_collect
,
399 [GotHeader
] = do_header
,
402 [CopyFile
] = do_copy
,
403 [GotSymlink
] = do_symlink
,
407 static long __init
write_buffer(char *buf
, unsigned long len
)
412 while (!actions
[state
]())
414 return len
- byte_count
;
417 static long __init
flush_buffer(void *bufv
, unsigned long len
)
419 char *buf
= (char *) bufv
;
424 while ((written
= write_buffer(buf
, len
)) < len
&& !message
) {
425 char c
= buf
[written
];
435 error("junk within compressed archive");
440 static unsigned long my_inptr
; /* index of next byte to be processed in inbuf */
442 #include <linux/decompress/generic.h>
444 static char * __init
unpack_to_rootfs(char *buf
, unsigned long len
)
447 decompress_fn decompress
;
448 const char *compress_name
;
449 static __initdata
char msg_buf
[64];
451 header_buf
= kmalloc(110, GFP_KERNEL
);
452 symlink_buf
= kmalloc(PATH_MAX
+ N_ALIGN(PATH_MAX
) + 1, GFP_KERNEL
);
453 name_buf
= kmalloc(N_ALIGN(PATH_MAX
), GFP_KERNEL
);
455 if (!header_buf
|| !symlink_buf
|| !name_buf
)
456 panic("can't allocate buffers");
461 while (!message
&& len
) {
462 loff_t saved_offset
= this_header
;
463 if (*buf
== '0' && !(this_header
& 3)) {
465 written
= write_buffer(buf
, len
);
477 decompress
= decompress_method(buf
, len
, &compress_name
);
478 pr_debug("Detected %s compressed data\n", compress_name
);
480 int res
= decompress(buf
, len
, NULL
, flush_buffer
, NULL
,
483 error("decompressor failed");
484 } else if (compress_name
) {
486 snprintf(msg_buf
, sizeof msg_buf
,
487 "compression method %s not configured",
492 error("invalid magic at start of compressed archive");
494 error("junk at the end of compressed archive");
495 this_header
= saved_offset
+ my_inptr
;
506 static int __initdata do_retain_initrd
;
508 static int __init
retain_initrd_param(char *str
)
512 do_retain_initrd
= 1;
515 __setup("retain_initrd", retain_initrd_param
);
517 #ifdef CONFIG_ARCH_HAS_KEEPINITRD
518 static int __init
keepinitrd_setup(char *__unused
)
520 do_retain_initrd
= 1;
523 __setup("keepinitrd", keepinitrd_setup
);
526 extern char __initramfs_start
[];
527 extern unsigned long __initramfs_size
;
528 #include <linux/initrd.h>
529 #include <linux/kexec.h>
531 void __weak
free_initrd_mem(unsigned long start
, unsigned long end
)
533 #ifdef CONFIG_ARCH_KEEP_MEMBLOCK
534 unsigned long aligned_start
= ALIGN_DOWN(start
, PAGE_SIZE
);
535 unsigned long aligned_end
= ALIGN(end
, PAGE_SIZE
);
537 memblock_free(__pa(aligned_start
), aligned_end
- aligned_start
);
540 free_reserved_area((void *)start
, (void *)end
, POISON_FREE_INITMEM
,
544 #ifdef CONFIG_KEXEC_CORE
545 static bool kexec_free_initrd(void)
547 unsigned long crashk_start
= (unsigned long)__va(crashk_res
.start
);
548 unsigned long crashk_end
= (unsigned long)__va(crashk_res
.end
);
551 * If the initrd region is overlapped with crashkernel reserved region,
552 * free only memory that is not part of crashkernel region.
554 if (initrd_start
>= crashk_end
|| initrd_end
<= crashk_start
)
558 * Initialize initrd memory region since the kexec boot does not do.
560 memset((void *)initrd_start
, 0, initrd_end
- initrd_start
);
561 if (initrd_start
< crashk_start
)
562 free_initrd_mem(initrd_start
, crashk_start
);
563 if (initrd_end
> crashk_end
)
564 free_initrd_mem(crashk_end
, initrd_end
);
568 static inline bool kexec_free_initrd(void)
572 #endif /* CONFIG_KEXEC_CORE */
574 #ifdef CONFIG_BLK_DEV_RAM
575 #define BUF_SIZE 1024
576 static void __init
clean_rootfs(void)
580 struct linux_dirent64
*dirp
;
583 fd
= ksys_open("/", O_RDONLY
, 0);
587 buf
= kzalloc(BUF_SIZE
, GFP_KERNEL
);
595 num
= ksys_getdents64(fd
, dirp
, BUF_SIZE
);
601 ret
= vfs_lstat(dirp
->d_name
, &st
);
604 if (S_ISDIR(st
.mode
))
605 ksys_rmdir(dirp
->d_name
);
607 ksys_unlink(dirp
->d_name
);
610 num
-= dirp
->d_reclen
;
611 dirp
= (void *)dirp
+ dirp
->d_reclen
;
614 memset(buf
, 0, BUF_SIZE
);
615 num
= ksys_getdents64(fd
, dirp
, BUF_SIZE
);
622 static inline void clean_rootfs(void)
625 #endif /* CONFIG_BLK_DEV_RAM */
627 #ifdef CONFIG_BLK_DEV_RAM
628 static void __init
populate_initrd_image(char *err
)
633 unpack_to_rootfs(__initramfs_start
, __initramfs_size
);
635 printk(KERN_INFO
"rootfs image is not initramfs (%s); looks like an initrd\n",
637 fd
= ksys_open("/initrd.image", O_WRONLY
| O_CREAT
, 0700);
641 written
= xwrite(fd
, (char *)initrd_start
, initrd_end
- initrd_start
);
642 if (written
!= initrd_end
- initrd_start
)
643 pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
644 written
, initrd_end
- initrd_start
);
648 static void __init
populate_initrd_image(char *err
)
650 printk(KERN_EMERG
"Initramfs unpacking failed: %s\n", err
);
652 #endif /* CONFIG_BLK_DEV_RAM */
654 static int __init
populate_rootfs(void)
656 /* Load the built in initramfs */
657 char *err
= unpack_to_rootfs(__initramfs_start
, __initramfs_size
);
659 panic("%s", err
); /* Failed to decompress INTERNAL initramfs */
661 if (!initrd_start
|| IS_ENABLED(CONFIG_INITRAMFS_FORCE
))
664 if (IS_ENABLED(CONFIG_BLK_DEV_RAM
))
665 printk(KERN_INFO
"Trying to unpack rootfs image as initramfs...\n");
667 printk(KERN_INFO
"Unpacking initramfs...\n");
669 err
= unpack_to_rootfs((char *)initrd_start
, initrd_end
- initrd_start
);
672 populate_initrd_image(err
);
677 * If the initrd region is overlapped with crashkernel reserved region,
678 * free only memory that is not part of crashkernel region.
680 if (!do_retain_initrd
&& initrd_start
&& !kexec_free_initrd())
681 free_initrd_mem(initrd_start
, initrd_end
);
685 flush_delayed_fput();
688 rootfs_initcall(populate_rootfs
);