^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/dirent.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/utime.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/namei.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/initramfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/init_syscalls.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) static ssize_t __init xwrite(struct file *file, const char *p, size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) ssize_t out = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) while (count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) ssize_t rv = kernel_write(file, p, count, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (rv < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) if (rv == -EINTR || rv == -EAGAIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) return out ? out : rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) } else if (rv == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) p += rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) out += rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) count -= rv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static __initdata char *message;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static void __init error(char *x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (!message)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) message = x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /* link hash */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static __initdata struct hash {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) int ino, minor, major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) umode_t mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct hash *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) char name[N_ALIGN(PATH_MAX)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) } *head[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static inline int hash(int major, int minor, int ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) unsigned long tmp = ino + minor + (major << 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) tmp += tmp >> 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return tmp & 31;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static char __init *find_link(int major, int minor, int ino,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) umode_t mode, char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct hash **p, *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) if ((*p)->ino != ino)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if ((*p)->minor != minor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if ((*p)->major != major)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) if (((*p)->mode ^ mode) & S_IFMT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) return (*p)->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) q = kmalloc(sizeof(struct hash), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (!q)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) panic("can't allocate link hash entry");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) q->major = major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) q->minor = minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) q->ino = ino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) q->mode = mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) strcpy(q->name, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) q->next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) *p = q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) static void __init free_hash(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct hash **p, *q;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) for (p = head; p < head + 32; p++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) while (*p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) q = *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) *p = q->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) kfree(q);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) static long __init do_utime(char *filename, time64_t mtime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) struct timespec64 t[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) t[0].tv_sec = mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) t[0].tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) t[1].tv_sec = mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) t[1].tv_nsec = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) return init_utimes(filename, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static __initdata LIST_HEAD(dir_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct dir_entry {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) time64_t mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static void __init dir_add(const char *name, time64_t mtime)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (!de)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) panic("can't allocate dir_entry buffer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) INIT_LIST_HEAD(&de->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) de->name = kstrdup(name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) de->mtime = mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) list_add(&de->list, &dir_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) static void __init dir_utime(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct dir_entry *de, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) list_for_each_entry_safe(de, tmp, &dir_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) list_del(&de->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) do_utime(de->name, de->mtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) kfree(de->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) kfree(de);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static __initdata time64_t mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) /* cpio header parsing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) static __initdata unsigned long ino, major, minor, nlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) static __initdata umode_t mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static __initdata unsigned long body_len, name_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) static __initdata uid_t uid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static __initdata gid_t gid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) static __initdata unsigned rdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) static void __init parse_header(char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) unsigned long parsed[12];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) char buf[9];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) buf[8] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) for (i = 0, s += 6; i < 12; i++, s += 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) memcpy(buf, s, 8);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) parsed[i] = simple_strtoul(buf, NULL, 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) ino = parsed[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) mode = parsed[1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) uid = parsed[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) gid = parsed[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) nlink = parsed[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) mtime = parsed[5]; /* breaks in y2106 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) body_len = parsed[6];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) major = parsed[7];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) minor = parsed[8];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) name_len = parsed[11];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /* FSM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) static __initdata enum state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) Start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) Collect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) GotHeader,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) SkipIt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) GotName,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) CopyFile,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) GotSymlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) Reset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) } state, next_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) static __initdata char *victim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static unsigned long byte_count __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static __initdata loff_t this_header, next_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) static inline void __init eat(unsigned n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) victim += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) this_header += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) byte_count -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static __initdata char *collected;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) static long remains __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static __initdata char *collect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static void __init read_into(char *buf, unsigned size, enum state next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (byte_count >= size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) collected = victim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) eat(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) state = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) collect = collected = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) remains = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) next_state = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) state = Collect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) static __initdata char *header_buf, *symlink_buf, *name_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) static int __init do_start(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) read_into(header_buf, 110, GotHeader);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) static int __init do_collect(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) unsigned long n = remains;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) if (byte_count < n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) n = byte_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) memcpy(collect, victim, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) eat(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) collect += n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) if ((remains -= n) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) state = next_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static int __init do_header(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (memcmp(collected, "070707", 6)==0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) error("incorrect cpio method used: use -H newc option");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) if (memcmp(collected, "070701", 6)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) error("no cpio magic");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) parse_header(collected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) next_header = this_header + N_ALIGN(name_len) + body_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) next_header = (next_header + 3) & ~3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) state = SkipIt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (name_len <= 0 || name_len > PATH_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (S_ISLNK(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (body_len > PATH_MAX)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) collect = collected = symlink_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) remains = N_ALIGN(name_len) + body_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) next_state = GotSymlink;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) state = Collect;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (S_ISREG(mode) || !body_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) read_into(name_buf, N_ALIGN(name_len), GotName);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static int __init do_skip(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (this_header + byte_count < next_header) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) eat(byte_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) eat(next_header - this_header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) state = next_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) static int __init do_reset(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) while (byte_count && *victim == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) eat(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (byte_count && (this_header & 3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) error("broken padding");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) static void __init clean_path(char *path, umode_t fmode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) struct kstat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (!init_stat(path, &st, AT_SYMLINK_NOFOLLOW) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) (st.mode ^ fmode) & S_IFMT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (S_ISDIR(st.mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) init_rmdir(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) init_unlink(path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) static int __init maybe_link(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) if (nlink >= 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) char *old = find_link(major, minor, ino, mode, collected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) if (old) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) clean_path(collected, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return (init_link(old, collected) < 0) ? -1 : 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static __initdata struct file *wfile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) static __initdata loff_t wfile_pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) static int __init do_name(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) state = SkipIt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) next_state = Reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (strcmp(collected, "TRAILER!!!") == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) free_hash();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) clean_path(collected, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (S_ISREG(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) int ml = maybe_link();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) if (ml >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) int openflags = O_WRONLY|O_CREAT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (ml != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) openflags |= O_TRUNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) wfile = filp_open(collected, openflags, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) if (IS_ERR(wfile))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) wfile_pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) vfs_fchown(wfile, uid, gid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) vfs_fchmod(wfile, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (body_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) vfs_truncate(&wfile->f_path, body_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) state = CopyFile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) } else if (S_ISDIR(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) init_mkdir(collected, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) init_chown(collected, uid, gid, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) init_chmod(collected, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) dir_add(collected, mtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) S_ISFIFO(mode) || S_ISSOCK(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (maybe_link() == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) init_mknod(collected, mode, rdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) init_chown(collected, uid, gid, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) init_chmod(collected, mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) do_utime(collected, mtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) static int __init do_copy(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (byte_count >= body_len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) struct timespec64 t[2] = { };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) error("write error");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) t[0].tv_sec = mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) t[1].tv_sec = mtime;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) vfs_utimes(&wfile->f_path, t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) fput(wfile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) eat(body_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) state = SkipIt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) if (xwrite(wfile, victim, byte_count, &wfile_pos) != byte_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) error("write error");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) body_len -= byte_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) eat(byte_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) static int __init do_symlink(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) collected[N_ALIGN(name_len) + body_len] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) clean_path(collected, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) init_symlink(collected + N_ALIGN(name_len), collected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) init_chown(collected, uid, gid, AT_SYMLINK_NOFOLLOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) do_utime(collected, mtime);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) state = SkipIt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) next_state = Reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) static __initdata int (*actions[])(void) = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) [Start] = do_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) [Collect] = do_collect,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) [GotHeader] = do_header,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) [SkipIt] = do_skip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) [GotName] = do_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) [CopyFile] = do_copy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) [GotSymlink] = do_symlink,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) [Reset] = do_reset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) static long __init write_buffer(char *buf, unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) byte_count = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) victim = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) while (!actions[state]())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return len - byte_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) static long __init flush_buffer(void *bufv, unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) char *buf = (char *) bufv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) long written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) long origLen = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) if (message)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) while ((written = write_buffer(buf, len)) < len && !message) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) char c = buf[written];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) if (c == '0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) buf += written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) len -= written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) state = Start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) } else if (c == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) buf += written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) len -= written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) state = Reset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) error("junk within compressed archive");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) return origLen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) static unsigned long my_inptr; /* index of next byte to be processed in inbuf */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) #include <linux/decompress/generic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) static char * __init unpack_to_rootfs(char *buf, unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) long written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) decompress_fn decompress;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) const char *compress_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) static __initdata char msg_buf[64];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) header_buf = kmalloc(110, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) if (!header_buf || !symlink_buf || !name_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) panic("can't allocate buffers");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) state = Start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) this_header = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) message = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) #if defined(CONFIG_ROCKCHIP_THUNDER_BOOT) && defined(CONFIG_ROCKCHIP_HW_DECOMPRESS) && defined(CONFIG_INITRD_ASYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) wait_initrd_hw_decom_done();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) while (!message && len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) loff_t saved_offset = this_header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) if (*buf == '0' && !(this_header & 3)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) state = Start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) written = write_buffer(buf, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) buf += written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) len -= written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) if (!*buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) buf++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) len--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) this_header++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) this_header = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) decompress = decompress_method(buf, len, &compress_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) pr_debug("Detected %s compressed data\n", compress_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) if (decompress) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) int res = decompress(buf, len, NULL, flush_buffer, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) &my_inptr, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) error("decompressor failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) } else if (compress_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (!message) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) snprintf(msg_buf, sizeof msg_buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) "compression method %s not configured",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) compress_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) message = msg_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) error("invalid magic at start of compressed archive");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) if (state != Reset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) error("junk at the end of compressed archive");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) this_header = saved_offset + my_inptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) buf += my_inptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) len -= my_inptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) dir_utime();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) kfree(name_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) kfree(symlink_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) kfree(header_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) return message;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) static int __initdata do_retain_initrd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) static int __init retain_initrd_param(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (*str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) do_retain_initrd = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) __setup("retain_initrd", retain_initrd_param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) #ifdef CONFIG_ARCH_HAS_KEEPINITRD
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) static int __init keepinitrd_setup(char *__unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) do_retain_initrd = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) __setup("keepinitrd", keepinitrd_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) extern char __initramfs_start[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) extern unsigned long __initramfs_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) #include <linux/initrd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) #include <linux/kexec.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) void __weak __init free_initrd_mem(unsigned long start, unsigned long end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) #ifdef CONFIG_ARCH_KEEP_MEMBLOCK
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) unsigned long aligned_start = ALIGN_DOWN(start, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) unsigned long aligned_end = ALIGN(end, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) memblock_free(__pa(aligned_start), aligned_end - aligned_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) "initrd");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) #ifdef CONFIG_KEXEC_CORE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) static bool __init kexec_free_initrd(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) * If the initrd region is overlapped with crashkernel reserved region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) * free only memory that is not part of crashkernel region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) if (initrd_start >= crashk_end || initrd_end <= crashk_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) * Initialize initrd memory region since the kexec boot does not do.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) memset((void *)initrd_start, 0, initrd_end - initrd_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) if (initrd_start < crashk_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) free_initrd_mem(initrd_start, crashk_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) if (initrd_end > crashk_end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) free_initrd_mem(crashk_end, initrd_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) static inline bool kexec_free_initrd(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) #endif /* CONFIG_KEXEC_CORE */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) #ifdef CONFIG_BLK_DEV_RAM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) static void __init populate_initrd_image(char *err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) ssize_t written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) struct file *file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) loff_t pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) unpack_to_rootfs(__initramfs_start, __initramfs_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) file = filp_open("/initrd.image", O_WRONLY | O_CREAT, 0700);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) if (IS_ERR(file))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) written = xwrite(file, (char *)initrd_start, initrd_end - initrd_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) &pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) if (written != initrd_end - initrd_start)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) written, initrd_end - initrd_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) fput(file);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) #endif /* CONFIG_BLK_DEV_RAM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) static int __init populate_rootfs(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) /* Load the built in initramfs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) panic("%s", err); /* Failed to decompress INTERNAL initramfs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) if (!initrd_start || IS_ENABLED(CONFIG_INITRAMFS_FORCE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) if (IS_ENABLED(CONFIG_BLK_DEV_RAM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) printk(KERN_INFO "Unpacking initramfs...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) err = unpack_to_rootfs((char *)initrd_start, initrd_end - initrd_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) #ifdef CONFIG_BLK_DEV_RAM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) populate_initrd_image(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) * If the initrd region is overlapped with crashkernel reserved region,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) * free only memory that is not part of crashkernel region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) if (!do_retain_initrd && initrd_start && !kexec_free_initrd())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) free_initrd_mem(initrd_start, initrd_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) initrd_start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) initrd_end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) flush_delayed_fput();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) #if IS_BUILTIN(CONFIG_INITRD_ASYNC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) #include <linux/kthread.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) #include <linux/async.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) static void __init unpack_rootfs_async(void *unused, async_cookie_t cookie)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) populate_rootfs();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) static int __init populate_rootfs_async(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) async_schedule(unpack_rootfs_async, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) pure_initcall(populate_rootfs_async);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) rootfs_initcall(populate_rootfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) #endif