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 int __init maybe_link(void)
297 char *old = find_link(major, minor, ino, mode, collected);
299 return (ksys_link(old, collected) < 0) ? -1 : 1;
304 static void __init clean_path(char *path, umode_t fmode)
308 if (!vfs_lstat(path, &st) && (st.mode ^ fmode) & S_IFMT) {
309 if (S_ISDIR(st.mode))
316 static __initdata int wfd;
318 static int __init do_name(void)
322 if (strcmp(collected, "TRAILER!!!") == 0) {
326 clean_path(collected, mode);
328 int ml = maybe_link();
330 int openflags = O_WRONLY|O_CREAT;
332 openflags |= O_TRUNC;
333 wfd = ksys_open(collected, openflags, mode);
336 ksys_fchown(wfd, uid, gid);
337 ksys_fchmod(wfd, mode);
339 ksys_ftruncate(wfd, body_len);
340 vcollected = kstrdup(collected, GFP_KERNEL);
344 } else if (S_ISDIR(mode)) {
345 ksys_mkdir(collected, mode);
346 ksys_chown(collected, uid, gid);
347 ksys_chmod(collected, mode);
348 dir_add(collected, mtime);
349 } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
350 S_ISFIFO(mode) || S_ISSOCK(mode)) {
351 if (maybe_link() == 0) {
352 ksys_mknod(collected, mode, rdev);
353 ksys_chown(collected, uid, gid);
354 ksys_chmod(collected, mode);
355 do_utime(collected, mtime);
361 static int __init do_copy(void)
363 if (byte_count >= body_len) {
364 if (xwrite(wfd, victim, body_len) != body_len)
365 error("write error");
367 do_utime(vcollected, mtime);
373 if (xwrite(wfd, victim, byte_count) != byte_count)
374 error("write error");
375 body_len -= byte_count;
381 static int __init do_symlink(void)
383 collected[N_ALIGN(name_len) + body_len] = '\0';
384 clean_path(collected, 0);
385 ksys_symlink(collected + N_ALIGN(name_len), collected);
386 ksys_lchown(collected, uid, gid);
387 do_utime(collected, mtime);
393 static __initdata int (*actions[])(void) = {
395 [Collect] = do_collect,
396 [GotHeader] = do_header,
399 [CopyFile] = do_copy,
400 [GotSymlink] = do_symlink,
404 static long __init write_buffer(char *buf, unsigned long len)
409 while (!actions[state]())
411 return len - byte_count;
414 static long __init flush_buffer(void *bufv, unsigned long len)
416 char *buf = (char *) bufv;
421 while ((written = write_buffer(buf, len)) < len && !message) {
422 char c = buf[written];
432 error("junk in compressed archive");
437 static unsigned long my_inptr; /* index of next byte to be processed in inbuf */
439 #include <linux/decompress/generic.h>
441 static char * __init unpack_to_rootfs(char *buf, unsigned long len)
444 decompress_fn decompress;
445 const char *compress_name;
446 static __initdata char msg_buf[64];
448 header_buf = kmalloc(110, GFP_KERNEL);
449 symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
450 name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
452 if (!header_buf || !symlink_buf || !name_buf)
453 panic("can't allocate buffers");
458 while (!message && len) {
459 loff_t saved_offset = this_header;
460 if (*buf == '0' && !(this_header & 3)) {
462 written = write_buffer(buf, len);
474 decompress = decompress_method(buf, len, &compress_name);
475 pr_debug("Detected %s compressed data\n", compress_name);
477 int res = decompress(buf, len, NULL, flush_buffer, NULL,
480 error("decompressor failed");
481 } else if (compress_name) {
483 snprintf(msg_buf, sizeof msg_buf,
484 "compression method %s not configured",
489 error("junk in compressed archive");
491 error("junk in compressed archive");
492 this_header = saved_offset + my_inptr;
503 static int __initdata do_retain_initrd;
505 static int __init retain_initrd_param(char *str)
509 do_retain_initrd = 1;
512 __setup("retain_initrd", retain_initrd_param);
514 extern char __initramfs_start[];
515 extern unsigned long __initramfs_size;
516 #include <linux/initrd.h>
517 #include <linux/kexec.h>
519 static void __init free_initrd(void)
521 #ifdef CONFIG_KEXEC_CORE
522 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
523 unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
525 if (do_retain_initrd)
528 #ifdef CONFIG_KEXEC_CORE
530 * If the initrd region is overlapped with crashkernel reserved region,
531 * free only memory that is not part of crashkernel region.
533 if (initrd_start < crashk_end && initrd_end > crashk_start) {
535 * Initialize initrd memory region since the kexec boot does
538 memset((void *)initrd_start, 0, initrd_end - initrd_start);
539 if (initrd_start < crashk_start)
540 free_initrd_mem(initrd_start, crashk_start);
541 if (initrd_end > crashk_end)
542 free_initrd_mem(crashk_end, initrd_end);
545 free_initrd_mem(initrd_start, initrd_end);
551 #ifdef CONFIG_BLK_DEV_RAM
552 #define BUF_SIZE 1024
553 static void __init clean_rootfs(void)
557 struct linux_dirent64 *dirp;
560 fd = ksys_open("/", O_RDONLY, 0);
564 buf = kzalloc(BUF_SIZE, GFP_KERNEL);
572 num = ksys_getdents64(fd, dirp, BUF_SIZE);
578 ret = vfs_lstat(dirp->d_name, &st);
581 if (S_ISDIR(st.mode))
582 ksys_rmdir(dirp->d_name);
584 ksys_unlink(dirp->d_name);
587 num -= dirp->d_reclen;
588 dirp = (void *)dirp + dirp->d_reclen;
591 memset(buf, 0, BUF_SIZE);
592 num = ksys_getdents64(fd, dirp, BUF_SIZE);
600 static int __init populate_rootfs(void)
602 /* Load the built in initramfs */
603 char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
605 panic("%s", err); /* Failed to decompress INTERNAL initramfs */
606 /* If available load the bootloader supplied initrd */
607 if (initrd_start && !IS_ENABLED(CONFIG_INITRAMFS_FORCE)) {
608 #ifdef CONFIG_BLK_DEV_RAM
610 printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
611 err = unpack_to_rootfs((char *)initrd_start,
612 initrd_end - initrd_start);
618 unpack_to_rootfs(__initramfs_start, __initramfs_size);
620 printk(KERN_INFO "rootfs image is not initramfs (%s)"
621 "; looks like an initrd\n", err);
622 fd = ksys_open("/initrd.image",
623 O_WRONLY|O_CREAT, 0700);
625 ssize_t written = xwrite(fd, (char *)initrd_start,
626 initrd_end - initrd_start);
628 if (written != initrd_end - initrd_start)
629 pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
630 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();
648 * Try loading default modules from initramfs. This gives
649 * us a chance to load before device_initcalls.
651 load_default_modules();
655 rootfs_initcall(populate_rootfs);