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>
14 static ssize_t __init
xwrite(int fd
, const char *p
, size_t count
)
18 /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
20 ssize_t rv
= ksys_write(fd
, p
, count
);
23 if (rv
== -EINTR
|| rv
== -EAGAIN
)
25 return out
? out
: rv
;
37 static __initdata
char *message
;
38 static void __init
error(char *x
)
46 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
48 static __initdata
struct hash
{
49 int ino
, minor
, major
;
52 char name
[N_ALIGN(PATH_MAX
)];
55 static inline int hash(int major
, int minor
, int ino
)
57 unsigned long tmp
= ino
+ minor
+ (major
<< 3);
62 static char __init
*find_link(int major
, int minor
, int ino
,
63 umode_t mode
, char *name
)
66 for (p
= head
+ hash(major
, minor
, ino
); *p
; p
= &(*p
)->next
) {
69 if ((*p
)->minor
!= minor
)
71 if ((*p
)->major
!= major
)
73 if (((*p
)->mode
^ mode
) & S_IFMT
)
77 q
= kmalloc(sizeof(struct hash
), GFP_KERNEL
);
79 panic("can't allocate link hash entry");
84 strcpy(q
->name
, name
);
90 static void __init
free_hash(void)
93 for (p
= head
; p
< head
+ 32; p
++) {
102 static long __init
do_utime(char *filename
, time64_t mtime
)
104 struct timespec64 t
[2];
111 return do_utimes(AT_FDCWD
, filename
, t
, AT_SYMLINK_NOFOLLOW
);
114 static __initdata
LIST_HEAD(dir_list
);
116 struct list_head list
;
121 static void __init
dir_add(const char *name
, time64_t mtime
)
123 struct dir_entry
*de
= kmalloc(sizeof(struct dir_entry
), GFP_KERNEL
);
125 panic("can't allocate dir_entry buffer");
126 INIT_LIST_HEAD(&de
->list
);
127 de
->name
= kstrdup(name
, GFP_KERNEL
);
129 list_add(&de
->list
, &dir_list
);
132 static void __init
dir_utime(void)
134 struct dir_entry
*de
, *tmp
;
135 list_for_each_entry_safe(de
, tmp
, &dir_list
, list
) {
137 do_utime(de
->name
, de
->mtime
);
143 static __initdata time64_t mtime
;
145 /* cpio header parsing */
147 static __initdata
unsigned long ino
, major
, minor
, nlink
;
148 static __initdata umode_t mode
;
149 static __initdata
unsigned long body_len
, name_len
;
150 static __initdata uid_t uid
;
151 static __initdata gid_t gid
;
152 static __initdata
unsigned rdev
;
154 static void __init
parse_header(char *s
)
156 unsigned long parsed
[12];
161 for (i
= 0, s
+= 6; i
< 12; i
++, s
+= 8) {
163 parsed
[i
] = simple_strtoul(buf
, NULL
, 16);
170 mtime
= parsed
[5]; /* breaks in y2106 */
171 body_len
= parsed
[6];
174 rdev
= new_encode_dev(MKDEV(parsed
[9], parsed
[10]));
175 name_len
= parsed
[11];
180 static __initdata
enum state
{
191 static __initdata
char *victim
;
192 static unsigned long byte_count __initdata
;
193 static __initdata loff_t this_header
, next_header
;
195 static inline void __init
eat(unsigned n
)
202 static __initdata
char *vcollected
;
203 static __initdata
char *collected
;
204 static long remains __initdata
;
205 static __initdata
char *collect
;
207 static void __init
read_into(char *buf
, unsigned size
, enum state next
)
209 if (byte_count
>= size
) {
214 collect
= collected
= buf
;
221 static __initdata
char *header_buf
, *symlink_buf
, *name_buf
;
223 static int __init
do_start(void)
225 read_into(header_buf
, 110, GotHeader
);
229 static int __init
do_collect(void)
231 unsigned long n
= remains
;
234 memcpy(collect
, victim
, n
);
237 if ((remains
-= n
) != 0)
243 static int __init
do_header(void)
245 if (memcmp(collected
, "070707", 6)==0) {
246 error("incorrect cpio method used: use -H newc option");
249 if (memcmp(collected
, "070701", 6)) {
250 error("no cpio magic");
253 parse_header(collected
);
254 next_header
= this_header
+ N_ALIGN(name_len
) + body_len
;
255 next_header
= (next_header
+ 3) & ~3;
257 if (name_len
<= 0 || name_len
> PATH_MAX
)
260 if (body_len
> PATH_MAX
)
262 collect
= collected
= symlink_buf
;
263 remains
= N_ALIGN(name_len
) + body_len
;
264 next_state
= GotSymlink
;
268 if (S_ISREG(mode
) || !body_len
)
269 read_into(name_buf
, N_ALIGN(name_len
), GotName
);
273 static int __init
do_skip(void)
275 if (this_header
+ byte_count
< next_header
) {
279 eat(next_header
- this_header
);
285 static int __init
do_reset(void)
287 while (byte_count
&& *victim
== '\0')
289 if (byte_count
&& (this_header
& 3))
290 error("broken padding");
294 static void __init
clean_path(char *path
, umode_t fmode
)
298 if (!vfs_lstat(path
, &st
) && (st
.mode
^ fmode
) & S_IFMT
) {
299 if (S_ISDIR(st
.mode
))
306 static int __init
maybe_link(void)
309 char *old
= find_link(major
, minor
, ino
, mode
, collected
);
311 clean_path(collected
, 0);
312 return (ksys_link(old
, collected
) < 0) ? -1 : 1;
318 static __initdata
int wfd
;
320 static int __init
do_name(void)
324 if (strcmp(collected
, "TRAILER!!!") == 0) {
328 clean_path(collected
, mode
);
330 int ml
= maybe_link();
332 int openflags
= O_WRONLY
|O_CREAT
;
334 openflags
|= O_TRUNC
;
335 wfd
= ksys_open(collected
, openflags
, mode
);
338 ksys_fchown(wfd
, uid
, gid
);
339 ksys_fchmod(wfd
, mode
);
341 ksys_ftruncate(wfd
, body_len
);
342 vcollected
= kstrdup(collected
, GFP_KERNEL
);
346 } else if (S_ISDIR(mode
)) {
347 ksys_mkdir(collected
, mode
);
348 ksys_chown(collected
, uid
, gid
);
349 ksys_chmod(collected
, mode
);
350 dir_add(collected
, mtime
);
351 } else if (S_ISBLK(mode
) || S_ISCHR(mode
) ||
352 S_ISFIFO(mode
) || S_ISSOCK(mode
)) {
353 if (maybe_link() == 0) {
354 ksys_mknod(collected
, mode
, rdev
);
355 ksys_chown(collected
, uid
, gid
);
356 ksys_chmod(collected
, mode
);
357 do_utime(collected
, mtime
);
363 static int __init
do_copy(void)
365 if (byte_count
>= body_len
) {
366 if (xwrite(wfd
, victim
, body_len
) != body_len
)
367 error("write error");
369 do_utime(vcollected
, mtime
);
375 if (xwrite(wfd
, victim
, byte_count
) != byte_count
)
376 error("write error");
377 body_len
-= byte_count
;
383 static int __init
do_symlink(void)
385 collected
[N_ALIGN(name_len
) + body_len
] = '\0';
386 clean_path(collected
, 0);
387 ksys_symlink(collected
+ N_ALIGN(name_len
), collected
);
388 ksys_lchown(collected
, uid
, gid
);
389 do_utime(collected
, mtime
);
395 static __initdata
int (*actions
[])(void) = {
397 [Collect
] = do_collect
,
398 [GotHeader
] = do_header
,
401 [CopyFile
] = do_copy
,
402 [GotSymlink
] = do_symlink
,
406 static long __init
write_buffer(char *buf
, unsigned long len
)
411 while (!actions
[state
]())
413 return len
- byte_count
;
416 static long __init
flush_buffer(void *bufv
, unsigned long len
)
418 char *buf
= (char *) bufv
;
423 while ((written
= write_buffer(buf
, len
)) < len
&& !message
) {
424 char c
= buf
[written
];
434 error("junk within compressed archive");
439 static unsigned long my_inptr
; /* index of next byte to be processed in inbuf */
441 #include <linux/decompress/generic.h>
443 static char * __init
unpack_to_rootfs(char *buf
, unsigned long len
)
446 decompress_fn decompress
;
447 const char *compress_name
;
448 static __initdata
char msg_buf
[64];
450 header_buf
= kmalloc(110, GFP_KERNEL
);
451 symlink_buf
= kmalloc(PATH_MAX
+ N_ALIGN(PATH_MAX
) + 1, GFP_KERNEL
);
452 name_buf
= kmalloc(N_ALIGN(PATH_MAX
), GFP_KERNEL
);
454 if (!header_buf
|| !symlink_buf
|| !name_buf
)
455 panic("can't allocate buffers");
460 while (!message
&& len
) {
461 loff_t saved_offset
= this_header
;
462 if (*buf
== '0' && !(this_header
& 3)) {
464 written
= write_buffer(buf
, len
);
476 decompress
= decompress_method(buf
, len
, &compress_name
);
477 pr_debug("Detected %s compressed data\n", compress_name
);
479 int res
= decompress(buf
, len
, NULL
, flush_buffer
, NULL
,
482 error("decompressor failed");
483 } else if (compress_name
) {
485 snprintf(msg_buf
, sizeof msg_buf
,
486 "compression method %s not configured",
491 error("invalid magic at start of compressed archive");
493 error("junk at the end of compressed archive");
494 this_header
= saved_offset
+ my_inptr
;
505 static int __initdata do_retain_initrd
;
507 static int __init
retain_initrd_param(char *str
)
511 do_retain_initrd
= 1;
514 __setup("retain_initrd", retain_initrd_param
);
516 extern char __initramfs_start
[];
517 extern unsigned long __initramfs_size
;
518 #include <linux/initrd.h>
519 #include <linux/kexec.h>
521 static void __init
free_initrd(void)
523 #ifdef CONFIG_KEXEC_CORE
524 unsigned long crashk_start
= (unsigned long)__va(crashk_res
.start
);
525 unsigned long crashk_end
= (unsigned long)__va(crashk_res
.end
);
527 if (do_retain_initrd
)
530 #ifdef CONFIG_KEXEC_CORE
532 * If the initrd region is overlapped with crashkernel reserved region,
533 * free only memory that is not part of crashkernel region.
535 if (initrd_start
< crashk_end
&& initrd_end
> crashk_start
) {
537 * Initialize initrd memory region since the kexec boot does
540 memset((void *)initrd_start
, 0, initrd_end
- initrd_start
);
541 if (initrd_start
< crashk_start
)
542 free_initrd_mem(initrd_start
, crashk_start
);
543 if (initrd_end
> crashk_end
)
544 free_initrd_mem(crashk_end
, initrd_end
);
547 free_initrd_mem(initrd_start
, initrd_end
);
553 #ifdef CONFIG_BLK_DEV_RAM
554 #define BUF_SIZE 1024
555 static void __init
clean_rootfs(void)
559 struct linux_dirent64
*dirp
;
562 fd
= ksys_open("/", O_RDONLY
, 0);
566 buf
= kzalloc(BUF_SIZE
, GFP_KERNEL
);
574 num
= ksys_getdents64(fd
, dirp
, BUF_SIZE
);
580 ret
= vfs_lstat(dirp
->d_name
, &st
);
583 if (S_ISDIR(st
.mode
))
584 ksys_rmdir(dirp
->d_name
);
586 ksys_unlink(dirp
->d_name
);
589 num
-= dirp
->d_reclen
;
590 dirp
= (void *)dirp
+ dirp
->d_reclen
;
593 memset(buf
, 0, BUF_SIZE
);
594 num
= ksys_getdents64(fd
, dirp
, BUF_SIZE
);
602 static int __init
populate_rootfs(void)
604 /* Load the built in initramfs */
605 char *err
= unpack_to_rootfs(__initramfs_start
, __initramfs_size
);
607 panic("%s", err
); /* Failed to decompress INTERNAL initramfs */
608 /* If available load the bootloader supplied initrd */
609 if (initrd_start
&& !IS_ENABLED(CONFIG_INITRAMFS_FORCE
)) {
610 #ifdef CONFIG_BLK_DEV_RAM
612 printk(KERN_INFO
"Trying to unpack rootfs image as initramfs...\n");
613 err
= unpack_to_rootfs((char *)initrd_start
,
614 initrd_end
- initrd_start
);
619 unpack_to_rootfs(__initramfs_start
, __initramfs_size
);
621 printk(KERN_INFO
"rootfs image is not initramfs (%s)"
622 "; looks like an initrd\n", err
);
623 fd
= ksys_open("/initrd.image",
624 O_WRONLY
|O_CREAT
, 0700);
626 ssize_t written
= xwrite(fd
, (char *)initrd_start
,
627 initrd_end
- initrd_start
);
629 if (written
!= initrd_end
- initrd_start
)
630 pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
631 written
, initrd_end
- initrd_start
);
636 /* empty statement */;
638 printk(KERN_INFO
"Unpacking initramfs...\n");
639 err
= unpack_to_rootfs((char *)initrd_start
,
640 initrd_end
- initrd_start
);
642 printk(KERN_EMERG
"Initramfs unpacking failed: %s\n", err
);
646 flush_delayed_fput();
649 rootfs_initcall(populate_rootfs
);