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 #include <linux/namei.h>
16 #include <linux/init_syscalls.h>
18 static ssize_t __init xwrite(struct file *file, const char *p, size_t count,
23 /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
25 ssize_t rv = kernel_write(file, p, count, pos);
28 if (rv == -EINTR || rv == -EAGAIN)
30 return out ? out : rv;
42 static __initdata char *message;
43 static void __init error(char *x)
49 static void panic_show_mem(const char *fmt, ...)
61 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
63 static __initdata struct hash {
64 int ino, minor, major;
67 char name[N_ALIGN(PATH_MAX)];
70 static inline int hash(int major, int minor, int ino)
72 unsigned long tmp = ino + minor + (major << 3);
77 static char __init *find_link(int major, int minor, int ino,
78 umode_t mode, char *name)
81 for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
84 if ((*p)->minor != minor)
86 if ((*p)->major != major)
88 if (((*p)->mode ^ mode) & S_IFMT)
92 q = kmalloc(sizeof(struct hash), GFP_KERNEL);
94 panic_show_mem("can't allocate link hash entry");
99 strcpy(q->name, name);
105 static void __init free_hash(void)
108 for (p = head; p < head + 32; p++) {
117 static long __init do_utime(char *filename, time64_t mtime)
119 struct timespec64 t[2];
125 return init_utimes(filename, t);
128 static __initdata LIST_HEAD(dir_list);
130 struct list_head list;
135 static void __init dir_add(const char *name, time64_t mtime)
137 struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
139 panic_show_mem("can't allocate dir_entry buffer");
140 INIT_LIST_HEAD(&de->list);
141 de->name = kstrdup(name, GFP_KERNEL);
143 list_add(&de->list, &dir_list);
146 static void __init dir_utime(void)
148 struct dir_entry *de, *tmp;
149 list_for_each_entry_safe(de, tmp, &dir_list, list) {
151 do_utime(de->name, de->mtime);
157 static __initdata time64_t mtime;
159 /* cpio header parsing */
161 static __initdata unsigned long ino, major, minor, nlink;
162 static __initdata umode_t mode;
163 static __initdata unsigned long body_len, name_len;
164 static __initdata uid_t uid;
165 static __initdata gid_t gid;
166 static __initdata unsigned rdev;
168 static void __init parse_header(char *s)
170 unsigned long parsed[12];
175 for (i = 0, s += 6; i < 12; i++, s += 8) {
177 parsed[i] = simple_strtoul(buf, NULL, 16);
184 mtime = parsed[5]; /* breaks in y2106 */
185 body_len = parsed[6];
188 rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
189 name_len = parsed[11];
194 static __initdata enum state {
205 static __initdata char *victim;
206 static unsigned long byte_count __initdata;
207 static __initdata loff_t this_header, next_header;
209 static inline void __init eat(unsigned n)
216 static __initdata char *collected;
217 static long remains __initdata;
218 static __initdata char *collect;
220 static void __init read_into(char *buf, unsigned size, enum state next)
222 if (byte_count >= size) {
227 collect = collected = buf;
234 static __initdata char *header_buf, *symlink_buf, *name_buf;
236 static int __init do_start(void)
238 read_into(header_buf, 110, GotHeader);
242 static int __init do_collect(void)
244 unsigned long n = remains;
247 memcpy(collect, victim, n);
250 if ((remains -= n) != 0)
256 static int __init do_header(void)
258 if (memcmp(collected, "070707", 6)==0) {
259 error("incorrect cpio method used: use -H newc option");
262 if (memcmp(collected, "070701", 6)) {
263 error("no cpio magic");
266 parse_header(collected);
267 next_header = this_header + N_ALIGN(name_len) + body_len;
268 next_header = (next_header + 3) & ~3;
270 if (name_len <= 0 || name_len > PATH_MAX)
273 if (body_len > PATH_MAX)
275 collect = collected = symlink_buf;
276 remains = N_ALIGN(name_len) + body_len;
277 next_state = GotSymlink;
281 if (S_ISREG(mode) || !body_len)
282 read_into(name_buf, N_ALIGN(name_len), GotName);
286 static int __init do_skip(void)
288 if (this_header + byte_count < next_header) {
292 eat(next_header - this_header);
298 static int __init do_reset(void)
300 while (byte_count && *victim == '\0')
302 if (byte_count && (this_header & 3))
303 error("broken padding");
307 static void __init clean_path(char *path, umode_t fmode)
311 if (!init_stat(path, &st, AT_SYMLINK_NOFOLLOW) &&
312 (st.mode ^ fmode) & S_IFMT) {
313 if (S_ISDIR(st.mode))
320 static int __init maybe_link(void)
323 char *old = find_link(major, minor, ino, mode, collected);
325 clean_path(collected, 0);
326 return (init_link(old, collected) < 0) ? -1 : 1;
332 static __initdata struct file *wfile;
333 static __initdata loff_t wfile_pos;
335 static int __init do_name(void)
339 if (strcmp(collected, "TRAILER!!!") == 0) {
343 clean_path(collected, mode);
345 int ml = maybe_link();
347 int openflags = O_WRONLY|O_CREAT;
349 openflags |= O_TRUNC;
350 wfile = filp_open(collected, openflags, mode);
355 vfs_fchown(wfile, uid, gid);
356 vfs_fchmod(wfile, mode);
358 vfs_truncate(&wfile->f_path, body_len);
361 } else if (S_ISDIR(mode)) {
362 init_mkdir(collected, mode);
363 init_chown(collected, uid, gid, 0);
364 init_chmod(collected, mode);
365 dir_add(collected, mtime);
366 } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
367 S_ISFIFO(mode) || S_ISSOCK(mode)) {
368 if (maybe_link() == 0) {
369 init_mknod(collected, mode, rdev);
370 init_chown(collected, uid, gid, 0);
371 init_chmod(collected, mode);
372 do_utime(collected, mtime);
378 static int __init do_copy(void)
380 if (byte_count >= body_len) {
381 struct timespec64 t[2] = { };
382 if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len)
383 error("write error");
387 vfs_utimes(&wfile->f_path, t);
394 if (xwrite(wfile, victim, byte_count, &wfile_pos) != byte_count)
395 error("write error");
396 body_len -= byte_count;
402 static int __init do_symlink(void)
404 collected[N_ALIGN(name_len) + body_len] = '\0';
405 clean_path(collected, 0);
406 init_symlink(collected + N_ALIGN(name_len), collected);
407 init_chown(collected, uid, gid, AT_SYMLINK_NOFOLLOW);
408 do_utime(collected, mtime);
414 static __initdata int (*actions[])(void) = {
416 [Collect] = do_collect,
417 [GotHeader] = do_header,
420 [CopyFile] = do_copy,
421 [GotSymlink] = do_symlink,
425 static long __init write_buffer(char *buf, unsigned long len)
430 while (!actions[state]())
432 return len - byte_count;
435 static long __init flush_buffer(void *bufv, unsigned long len)
437 char *buf = (char *) bufv;
442 while ((written = write_buffer(buf, len)) < len && !message) {
443 char c = buf[written];
453 error("junk within compressed archive");
458 static unsigned long my_inptr; /* index of next byte to be processed in inbuf */
460 #include <linux/decompress/generic.h>
462 static char * __init unpack_to_rootfs(char *buf, unsigned long len)
465 decompress_fn decompress;
466 const char *compress_name;
467 static __initdata char msg_buf[64];
469 header_buf = kmalloc(110, GFP_KERNEL);
470 symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
471 name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
473 if (!header_buf || !symlink_buf || !name_buf)
474 panic_show_mem("can't allocate buffers");
479 while (!message && len) {
480 loff_t saved_offset = this_header;
481 if (*buf == '0' && !(this_header & 3)) {
483 written = write_buffer(buf, len);
495 decompress = decompress_method(buf, len, &compress_name);
496 pr_debug("Detected %s compressed data\n", compress_name);
498 int res = decompress(buf, len, NULL, flush_buffer, NULL,
501 error("decompressor failed");
502 } else if (compress_name) {
504 snprintf(msg_buf, sizeof msg_buf,
505 "compression method %s not configured",
510 error("invalid magic at start of compressed archive");
512 error("junk at the end of compressed archive");
513 this_header = saved_offset + my_inptr;
524 static int __initdata do_retain_initrd;
526 static int __init retain_initrd_param(char *str)
530 do_retain_initrd = 1;
533 __setup("retain_initrd", retain_initrd_param);
535 #ifdef CONFIG_ARCH_HAS_KEEPINITRD
536 static int __init keepinitrd_setup(char *__unused)
538 do_retain_initrd = 1;
541 __setup("keepinitrd", keepinitrd_setup);
544 extern char __initramfs_start[];
545 extern unsigned long __initramfs_size;
546 #include <linux/initrd.h>
547 #include <linux/kexec.h>
549 void __init reserve_initrd_mem(void)
554 /* Ignore the virtul address computed during device tree parsing */
555 initrd_start = initrd_end = 0;
557 if (!phys_initrd_size)
560 * Round the memory region to page boundaries as per free_initrd_mem()
561 * This allows us to detect whether the pages overlapping the initrd
562 * are in use, but more importantly, reserves the entire set of pages
563 * as we don't want these pages allocated for other purposes.
565 start = round_down(phys_initrd_start, PAGE_SIZE);
566 size = phys_initrd_size + (phys_initrd_start - start);
567 size = round_up(size, PAGE_SIZE);
569 if (!memblock_is_region_memory(start, size)) {
570 pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region",
575 if (memblock_is_region_reserved(start, size)) {
576 pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region\n",
581 memblock_reserve(start, size);
582 /* Now convert initrd to virtual addresses */
583 initrd_start = (unsigned long)__va(phys_initrd_start);
584 initrd_end = initrd_start + phys_initrd_size;
585 initrd_below_start_ok = 1;
589 pr_cont(" - disabling initrd\n");
594 void __weak __init free_initrd_mem(unsigned long start, unsigned long end)
596 #ifdef CONFIG_ARCH_KEEP_MEMBLOCK
597 unsigned long aligned_start = ALIGN_DOWN(start, PAGE_SIZE);
598 unsigned long aligned_end = ALIGN(end, PAGE_SIZE);
600 memblock_free(__pa(aligned_start), aligned_end - aligned_start);
603 free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM,
607 #ifdef CONFIG_KEXEC_CORE
608 static bool __init kexec_free_initrd(void)
610 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
611 unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
614 * If the initrd region is overlapped with crashkernel reserved region,
615 * free only memory that is not part of crashkernel region.
617 if (initrd_start >= crashk_end || initrd_end <= crashk_start)
621 * Initialize initrd memory region since the kexec boot does not do.
623 memset((void *)initrd_start, 0, initrd_end - initrd_start);
624 if (initrd_start < crashk_start)
625 free_initrd_mem(initrd_start, crashk_start);
626 if (initrd_end > crashk_end)
627 free_initrd_mem(crashk_end, initrd_end);
631 static inline bool kexec_free_initrd(void)
635 #endif /* CONFIG_KEXEC_CORE */
637 #ifdef CONFIG_BLK_DEV_RAM
638 static void __init populate_initrd_image(char *err)
644 unpack_to_rootfs(__initramfs_start, __initramfs_size);
646 printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n",
648 file = filp_open("/initrd.image", O_WRONLY | O_CREAT, 0700);
652 written = xwrite(file, (char *)initrd_start, initrd_end - initrd_start,
654 if (written != initrd_end - initrd_start)
655 pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
656 written, initrd_end - initrd_start);
659 #endif /* CONFIG_BLK_DEV_RAM */
661 static int __init populate_rootfs(void)
663 /* Load the built in initramfs */
664 char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
666 panic_show_mem("%s", err); /* Failed to decompress INTERNAL initramfs */
668 if (!initrd_start || IS_ENABLED(CONFIG_INITRAMFS_FORCE))
671 if (IS_ENABLED(CONFIG_BLK_DEV_RAM))
672 printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
674 printk(KERN_INFO "Unpacking initramfs...\n");
676 err = unpack_to_rootfs((char *)initrd_start, initrd_end - initrd_start);
678 #ifdef CONFIG_BLK_DEV_RAM
679 populate_initrd_image(err);
681 printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
687 * If the initrd region is overlapped with crashkernel reserved region,
688 * free only memory that is not part of crashkernel region.
690 if (!do_retain_initrd && initrd_start && !kexec_free_initrd())
691 free_initrd_mem(initrd_start, initrd_end);
695 flush_delayed_fput();
698 rootfs_initcall(populate_rootfs);