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>
24 static ssize_t __init xwrite(int fd, const char *p, size_t count)
28 /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
30 ssize_t rv = sys_write(fd, p, count);
33 if (rv == -EINTR || rv == -EAGAIN)
35 return out ? out : rv;
47 static __initdata char *message;
48 static void __init error(char *x)
56 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
58 static __initdata struct hash {
59 int ino, minor, major;
62 char name[N_ALIGN(PATH_MAX)];
65 static inline int hash(int major, int minor, int ino)
67 unsigned long tmp = ino + minor + (major << 3);
72 static char __init *find_link(int major, int minor, int ino,
73 umode_t mode, char *name)
76 for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
79 if ((*p)->minor != minor)
81 if ((*p)->major != major)
83 if (((*p)->mode ^ mode) & S_IFMT)
87 q = kmalloc(sizeof(struct hash), GFP_KERNEL);
89 panic("can't allocate link hash entry");
94 strcpy(q->name, name);
100 static void __init free_hash(void)
103 for (p = head; p < head + 32; p++) {
112 static long __init do_utime(char *filename, time64_t mtime)
114 struct timespec64 t[2];
121 return do_utimes(AT_FDCWD, filename, t, AT_SYMLINK_NOFOLLOW);
124 static __initdata LIST_HEAD(dir_list);
126 struct list_head list;
131 static void __init dir_add(const char *name, time64_t mtime)
133 struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
135 panic("can't allocate dir_entry buffer");
136 INIT_LIST_HEAD(&de->list);
137 de->name = kstrdup(name, GFP_KERNEL);
139 list_add(&de->list, &dir_list);
142 static void __init dir_utime(void)
144 struct dir_entry *de, *tmp;
145 list_for_each_entry_safe(de, tmp, &dir_list, list) {
147 do_utime(de->name, de->mtime);
153 static __initdata time64_t mtime;
155 /* cpio header parsing */
157 static __initdata unsigned long ino, major, minor, nlink;
158 static __initdata umode_t mode;
159 static __initdata unsigned long body_len, name_len;
160 static __initdata uid_t uid;
161 static __initdata gid_t gid;
162 static __initdata unsigned rdev;
164 static void __init parse_header(char *s)
166 unsigned long parsed[12];
171 for (i = 0, s += 6; i < 12; i++, s += 8) {
173 parsed[i] = simple_strtoul(buf, NULL, 16);
180 mtime = parsed[5]; /* breaks in y2106 */
181 body_len = parsed[6];
184 rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
185 name_len = parsed[11];
190 static __initdata enum state {
201 static __initdata char *victim;
202 static unsigned long byte_count __initdata;
203 static __initdata loff_t this_header, next_header;
205 static inline void __init eat(unsigned n)
212 static __initdata char *vcollected;
213 static __initdata char *collected;
214 static long remains __initdata;
215 static __initdata char *collect;
217 static void __init read_into(char *buf, unsigned size, enum state next)
219 if (byte_count >= size) {
224 collect = collected = buf;
231 static __initdata char *header_buf, *symlink_buf, *name_buf;
233 static int __init do_start(void)
235 read_into(header_buf, 110, GotHeader);
239 static int __init do_collect(void)
241 unsigned long n = remains;
244 memcpy(collect, victim, n);
247 if ((remains -= n) != 0)
253 static int __init do_header(void)
255 if (memcmp(collected, "070707", 6)==0) {
256 error("incorrect cpio method used: use -H newc option");
259 if (memcmp(collected, "070701", 6)) {
260 error("no cpio magic");
263 parse_header(collected);
264 next_header = this_header + N_ALIGN(name_len) + body_len;
265 next_header = (next_header + 3) & ~3;
267 if (name_len <= 0 || name_len > PATH_MAX)
270 if (body_len > PATH_MAX)
272 collect = collected = symlink_buf;
273 remains = N_ALIGN(name_len) + body_len;
274 next_state = GotSymlink;
278 if (S_ISREG(mode) || !body_len)
279 read_into(name_buf, N_ALIGN(name_len), GotName);
283 static int __init do_skip(void)
285 if (this_header + byte_count < next_header) {
289 eat(next_header - this_header);
295 static int __init do_reset(void)
297 while (byte_count && *victim == '\0')
299 if (byte_count && (this_header & 3))
300 error("broken padding");
304 static int __init maybe_link(void)
307 char *old = find_link(major, minor, ino, mode, collected);
309 return (sys_link(old, collected) < 0) ? -1 : 1;
314 static void __init clean_path(char *path, umode_t fmode)
318 if (!vfs_lstat(path, &st) && (st.mode ^ fmode) & S_IFMT) {
319 if (S_ISDIR(st.mode))
326 static __initdata int wfd;
328 static int __init do_name(void)
332 if (strcmp(collected, "TRAILER!!!") == 0) {
336 clean_path(collected, mode);
338 int ml = maybe_link();
340 int openflags = O_WRONLY|O_CREAT;
342 openflags |= O_TRUNC;
343 wfd = sys_open(collected, openflags, mode);
346 sys_fchown(wfd, uid, gid);
347 sys_fchmod(wfd, mode);
349 sys_ftruncate(wfd, body_len);
350 vcollected = kstrdup(collected, GFP_KERNEL);
354 } else if (S_ISDIR(mode)) {
355 sys_mkdir(collected, mode);
356 sys_chown(collected, uid, gid);
357 sys_chmod(collected, mode);
358 dir_add(collected, mtime);
359 } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
360 S_ISFIFO(mode) || S_ISSOCK(mode)) {
361 if (maybe_link() == 0) {
362 sys_mknod(collected, mode, rdev);
363 sys_chown(collected, uid, gid);
364 sys_chmod(collected, mode);
365 do_utime(collected, mtime);
371 static int __init do_copy(void)
373 if (byte_count >= body_len) {
374 if (xwrite(wfd, victim, body_len) != body_len)
375 error("write error");
377 do_utime(vcollected, mtime);
383 if (xwrite(wfd, victim, byte_count) != 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 sys_symlink(collected + N_ALIGN(name_len), collected);
396 sys_lchown(collected, uid, gid);
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 in 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("junk in compressed archive");
501 error("junk in 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 extern char __initramfs_start[];
525 extern unsigned long __initramfs_size;
526 #include <linux/initrd.h>
527 #include <linux/kexec.h>
529 static void __init free_initrd(void)
531 #ifdef CONFIG_KEXEC_CORE
532 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
533 unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
535 if (do_retain_initrd)
538 #ifdef CONFIG_KEXEC_CORE
540 * If the initrd region is overlapped with crashkernel reserved region,
541 * free only memory that is not part of crashkernel region.
543 if (initrd_start < crashk_end && initrd_end > crashk_start) {
545 * Initialize initrd memory region since the kexec boot does
548 memset((void *)initrd_start, 0, initrd_end - initrd_start);
549 if (initrd_start < crashk_start)
550 free_initrd_mem(initrd_start, crashk_start);
551 if (initrd_end > crashk_end)
552 free_initrd_mem(crashk_end, initrd_end);
555 free_initrd_mem(initrd_start, initrd_end);
561 #ifdef CONFIG_BLK_DEV_RAM
562 #define BUF_SIZE 1024
563 static void __init clean_rootfs(void)
567 struct linux_dirent64 *dirp;
570 fd = sys_open("/", O_RDONLY, 0);
574 buf = kzalloc(BUF_SIZE, GFP_KERNEL);
582 num = sys_getdents64(fd, dirp, BUF_SIZE);
588 ret = vfs_lstat(dirp->d_name, &st);
591 if (S_ISDIR(st.mode))
592 sys_rmdir(dirp->d_name);
594 sys_unlink(dirp->d_name);
597 num -= dirp->d_reclen;
598 dirp = (void *)dirp + dirp->d_reclen;
601 memset(buf, 0, BUF_SIZE);
602 num = sys_getdents64(fd, dirp, BUF_SIZE);
610 static int __init populate_rootfs(void)
612 /* Load the built in initramfs */
613 char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
615 panic("%s", err); /* Failed to decompress INTERNAL initramfs */
616 /* If available load the bootloader supplied initrd */
617 if (initrd_start && !IS_ENABLED(CONFIG_INITRAMFS_FORCE)) {
618 #ifdef CONFIG_BLK_DEV_RAM
620 printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
621 err = unpack_to_rootfs((char *)initrd_start,
622 initrd_end - initrd_start);
628 unpack_to_rootfs(__initramfs_start, __initramfs_size);
630 printk(KERN_INFO "rootfs image is not initramfs (%s)"
631 "; looks like an initrd\n", err);
632 fd = sys_open("/initrd.image",
633 O_WRONLY|O_CREAT, 0700);
635 ssize_t written = xwrite(fd, (char *)initrd_start,
636 initrd_end - initrd_start);
638 if (written != initrd_end - initrd_start)
639 pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
640 written, initrd_end - initrd_start);
646 /* empty statement */;
648 printk(KERN_INFO "Unpacking initramfs...\n");
649 err = unpack_to_rootfs((char *)initrd_start,
650 initrd_end - initrd_start);
652 printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
656 flush_delayed_fput();
658 * Try loading default modules from initramfs. This gives
659 * us a chance to load before device_initcalls.
661 load_default_modules();
665 rootfs_initcall(populate_rootfs);