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>
14 #include <linux/namei.h>
15 #include <linux/init_syscalls.h>
17 static ssize_t __init xwrite(struct file *file, const char *p, size_t count,
22 /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
24 ssize_t rv = kernel_write(file, p, count, pos);
27 if (rv == -EINTR || rv == -EAGAIN)
29 return out ? out : rv;
41 static __initdata char *message;
42 static void __init error(char *x)
50 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
52 static __initdata struct hash {
53 int ino, minor, major;
56 char name[N_ALIGN(PATH_MAX)];
59 static inline int hash(int major, int minor, int ino)
61 unsigned long tmp = ino + minor + (major << 3);
66 static char __init *find_link(int major, int minor, int ino,
67 umode_t mode, char *name)
70 for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
73 if ((*p)->minor != minor)
75 if ((*p)->major != major)
77 if (((*p)->mode ^ mode) & S_IFMT)
81 q = kmalloc(sizeof(struct hash), GFP_KERNEL);
83 panic("can't allocate link hash entry");
88 strcpy(q->name, name);
94 static void __init free_hash(void)
97 for (p = head; p < head + 32; p++) {
106 static long __init do_utime(char *filename, time64_t mtime)
108 struct timespec64 t[2];
114 return init_utimes(filename, t);
117 static __initdata LIST_HEAD(dir_list);
119 struct list_head list;
124 static void __init dir_add(const char *name, time64_t mtime)
126 struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
128 panic("can't allocate dir_entry buffer");
129 INIT_LIST_HEAD(&de->list);
130 de->name = kstrdup(name, GFP_KERNEL);
132 list_add(&de->list, &dir_list);
135 static void __init dir_utime(void)
137 struct dir_entry *de, *tmp;
138 list_for_each_entry_safe(de, tmp, &dir_list, list) {
140 do_utime(de->name, de->mtime);
146 static __initdata time64_t mtime;
148 /* cpio header parsing */
150 static __initdata unsigned long ino, major, minor, nlink;
151 static __initdata umode_t mode;
152 static __initdata unsigned long body_len, name_len;
153 static __initdata uid_t uid;
154 static __initdata gid_t gid;
155 static __initdata unsigned rdev;
157 static void __init parse_header(char *s)
159 unsigned long parsed[12];
164 for (i = 0, s += 6; i < 12; i++, s += 8) {
166 parsed[i] = simple_strtoul(buf, NULL, 16);
173 mtime = parsed[5]; /* breaks in y2106 */
174 body_len = parsed[6];
177 rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
178 name_len = parsed[11];
183 static __initdata enum state {
194 static __initdata char *victim;
195 static unsigned long byte_count __initdata;
196 static __initdata loff_t this_header, next_header;
198 static inline void __init eat(unsigned n)
205 static __initdata char *collected;
206 static long remains __initdata;
207 static __initdata char *collect;
209 static void __init read_into(char *buf, unsigned size, enum state next)
211 if (byte_count >= size) {
216 collect = collected = buf;
223 static __initdata char *header_buf, *symlink_buf, *name_buf;
225 static int __init do_start(void)
227 read_into(header_buf, 110, GotHeader);
231 static int __init do_collect(void)
233 unsigned long n = remains;
236 memcpy(collect, victim, n);
239 if ((remains -= n) != 0)
245 static int __init do_header(void)
247 if (memcmp(collected, "070707", 6)==0) {
248 error("incorrect cpio method used: use -H newc option");
251 if (memcmp(collected, "070701", 6)) {
252 error("no cpio magic");
255 parse_header(collected);
256 next_header = this_header + N_ALIGN(name_len) + body_len;
257 next_header = (next_header + 3) & ~3;
259 if (name_len <= 0 || name_len > PATH_MAX)
262 if (body_len > PATH_MAX)
264 collect = collected = symlink_buf;
265 remains = N_ALIGN(name_len) + body_len;
266 next_state = GotSymlink;
270 if (S_ISREG(mode) || !body_len)
271 read_into(name_buf, N_ALIGN(name_len), GotName);
275 static int __init do_skip(void)
277 if (this_header + byte_count < next_header) {
281 eat(next_header - this_header);
287 static int __init do_reset(void)
289 while (byte_count && *victim == '\0')
291 if (byte_count && (this_header & 3))
292 error("broken padding");
296 static void __init clean_path(char *path, umode_t fmode)
300 if (init_stat(path, &st, AT_SYMLINK_NOFOLLOW) &&
301 (st.mode ^ fmode) & S_IFMT) {
302 if (S_ISDIR(st.mode))
309 static int __init maybe_link(void)
312 char *old = find_link(major, minor, ino, mode, collected);
314 clean_path(collected, 0);
315 return (init_link(old, collected) < 0) ? -1 : 1;
321 static __initdata struct file *wfile;
322 static __initdata loff_t wfile_pos;
324 static int __init do_name(void)
328 if (strcmp(collected, "TRAILER!!!") == 0) {
332 clean_path(collected, mode);
334 int ml = maybe_link();
336 int openflags = O_WRONLY|O_CREAT;
338 openflags |= O_TRUNC;
339 wfile = filp_open(collected, openflags, mode);
344 vfs_fchown(wfile, uid, gid);
345 vfs_fchmod(wfile, mode);
347 vfs_truncate(&wfile->f_path, body_len);
350 } else if (S_ISDIR(mode)) {
351 init_mkdir(collected, mode);
352 init_chown(collected, uid, gid, 0);
353 init_chmod(collected, mode);
354 dir_add(collected, mtime);
355 } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
356 S_ISFIFO(mode) || S_ISSOCK(mode)) {
357 if (maybe_link() == 0) {
358 init_mknod(collected, mode, rdev);
359 init_chown(collected, uid, gid, 0);
360 init_chmod(collected, mode);
361 do_utime(collected, mtime);
367 static int __init do_copy(void)
369 if (byte_count >= body_len) {
370 struct timespec64 t[2] = { };
371 if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len)
372 error("write error");
376 vfs_utimes(&wfile->f_path, t);
383 if (xwrite(wfile, victim, byte_count, &wfile_pos) != byte_count)
384 error("write error");
385 body_len -= byte_count;
391 static int __init do_symlink(void)
393 collected[N_ALIGN(name_len) + body_len] = '\0';
394 clean_path(collected, 0);
395 init_symlink(collected + N_ALIGN(name_len), collected);
396 init_chown(collected, uid, gid, AT_SYMLINK_NOFOLLOW);
397 do_utime(collected, mtime);
403 static __initdata int (*actions[])(void) = {
405 [Collect] = do_collect,
406 [GotHeader] = do_header,
409 [CopyFile] = do_copy,
410 [GotSymlink] = do_symlink,
414 static long __init write_buffer(char *buf, unsigned long len)
419 while (!actions[state]())
421 return len - byte_count;
424 static long __init flush_buffer(void *bufv, unsigned long len)
426 char *buf = (char *) bufv;
431 while ((written = write_buffer(buf, len)) < len && !message) {
432 char c = buf[written];
442 error("junk within compressed archive");
447 static unsigned long my_inptr; /* index of next byte to be processed in inbuf */
449 #include <linux/decompress/generic.h>
451 static char * __init unpack_to_rootfs(char *buf, unsigned long len)
454 decompress_fn decompress;
455 const char *compress_name;
456 static __initdata char msg_buf[64];
458 header_buf = kmalloc(110, GFP_KERNEL);
459 symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
460 name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
462 if (!header_buf || !symlink_buf || !name_buf)
463 panic("can't allocate buffers");
468 while (!message && len) {
469 loff_t saved_offset = this_header;
470 if (*buf == '0' && !(this_header & 3)) {
472 written = write_buffer(buf, len);
484 decompress = decompress_method(buf, len, &compress_name);
485 pr_debug("Detected %s compressed data\n", compress_name);
487 int res = decompress(buf, len, NULL, flush_buffer, NULL,
490 error("decompressor failed");
491 } else if (compress_name) {
493 snprintf(msg_buf, sizeof msg_buf,
494 "compression method %s not configured",
499 error("invalid magic at start of compressed archive");
501 error("junk at the end of compressed archive");
502 this_header = saved_offset + my_inptr;
513 static int __initdata do_retain_initrd;
515 static int __init retain_initrd_param(char *str)
519 do_retain_initrd = 1;
522 __setup("retain_initrd", retain_initrd_param);
524 #ifdef CONFIG_ARCH_HAS_KEEPINITRD
525 static int __init keepinitrd_setup(char *__unused)
527 do_retain_initrd = 1;
530 __setup("keepinitrd", keepinitrd_setup);
533 extern char __initramfs_start[];
534 extern unsigned long __initramfs_size;
535 #include <linux/initrd.h>
536 #include <linux/kexec.h>
538 void __weak free_initrd_mem(unsigned long start, unsigned long end)
540 #ifdef CONFIG_ARCH_KEEP_MEMBLOCK
541 unsigned long aligned_start = ALIGN_DOWN(start, PAGE_SIZE);
542 unsigned long aligned_end = ALIGN(end, PAGE_SIZE);
544 memblock_free(__pa(aligned_start), aligned_end - aligned_start);
547 free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM,
551 #ifdef CONFIG_KEXEC_CORE
552 static bool __init kexec_free_initrd(void)
554 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
555 unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
558 * If the initrd region is overlapped with crashkernel reserved region,
559 * free only memory that is not part of crashkernel region.
561 if (initrd_start >= crashk_end || initrd_end <= crashk_start)
565 * Initialize initrd memory region since the kexec boot does not do.
567 memset((void *)initrd_start, 0, initrd_end - initrd_start);
568 if (initrd_start < crashk_start)
569 free_initrd_mem(initrd_start, crashk_start);
570 if (initrd_end > crashk_end)
571 free_initrd_mem(crashk_end, initrd_end);
575 static inline bool kexec_free_initrd(void)
579 #endif /* CONFIG_KEXEC_CORE */
581 #ifdef CONFIG_BLK_DEV_RAM
582 static void __init populate_initrd_image(char *err)
588 unpack_to_rootfs(__initramfs_start, __initramfs_size);
590 printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n",
592 file = filp_open("/initrd.image", O_WRONLY | O_CREAT, 0700);
596 written = xwrite(file, (char *)initrd_start, initrd_end - initrd_start,
598 if (written != initrd_end - initrd_start)
599 pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
600 written, initrd_end - initrd_start);
603 #endif /* CONFIG_BLK_DEV_RAM */
605 static int __init populate_rootfs(void)
607 /* Load the built in initramfs */
608 char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
610 panic("%s", err); /* Failed to decompress INTERNAL initramfs */
612 if (!initrd_start || IS_ENABLED(CONFIG_INITRAMFS_FORCE))
615 if (IS_ENABLED(CONFIG_BLK_DEV_RAM))
616 printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
618 printk(KERN_INFO "Unpacking initramfs...\n");
620 err = unpack_to_rootfs((char *)initrd_start, initrd_end - initrd_start);
622 #ifdef CONFIG_BLK_DEV_RAM
623 populate_initrd_image(err);
625 printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
631 * If the initrd region is overlapped with crashkernel reserved region,
632 * free only memory that is not part of crashkernel region.
634 if (!do_retain_initrd && initrd_start && !kexec_free_initrd())
635 free_initrd_mem(initrd_start, initrd_end);
639 flush_delayed_fput();
642 rootfs_initcall(populate_rootfs);