1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds #include <linux/init.h> 3e7cb072eSRasmus Villemoes #include <linux/async.h> 41da177e4SLinus Torvalds #include <linux/fs.h> 51da177e4SLinus Torvalds #include <linux/slab.h> 61da177e4SLinus Torvalds #include <linux/types.h> 71da177e4SLinus Torvalds #include <linux/fcntl.h> 81da177e4SLinus Torvalds #include <linux/delay.h> 91da177e4SLinus Torvalds #include <linux/string.h> 10df52092fSLi, Shaohua #include <linux/dirent.h> 111da177e4SLinus Torvalds #include <linux/syscalls.h> 12889d51a1SNye Liu #include <linux/utime.h> 1308865514SLokesh Vutla #include <linux/file.h> 14899ee4afSMike Rapoport #include <linux/memblock.h> 15dd23e809SFlorian Fainelli #include <linux/mm.h> 16b2a74d5fSChristoph Hellwig #include <linux/namei.h> 178fb9f73eSChristoph Hellwig #include <linux/init_syscalls.h> 18b234ed6dSRasmus Villemoes #include <linux/umh.h> 191da177e4SLinus Torvalds 20bf6419e4SChristoph Hellwig static ssize_t __init xwrite(struct file *file, const char *p, size_t count, 21bf6419e4SChristoph Hellwig loff_t *pos) 2238747439SYinghai Lu { 2338747439SYinghai Lu ssize_t out = 0; 2438747439SYinghai Lu 2538747439SYinghai Lu /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */ 2638747439SYinghai Lu while (count) { 27bf6419e4SChristoph Hellwig ssize_t rv = kernel_write(file, p, count, pos); 2838747439SYinghai Lu 2938747439SYinghai Lu if (rv < 0) { 3038747439SYinghai Lu if (rv == -EINTR || rv == -EAGAIN) 3138747439SYinghai Lu continue; 3238747439SYinghai Lu return out ? out : rv; 3338747439SYinghai Lu } else if (rv == 0) 3438747439SYinghai Lu break; 3538747439SYinghai Lu 3638747439SYinghai Lu p += rv; 3738747439SYinghai Lu out += rv; 3838747439SYinghai Lu count -= rv; 3938747439SYinghai Lu } 4038747439SYinghai Lu 4138747439SYinghai Lu return out; 4238747439SYinghai Lu } 4338747439SYinghai Lu 441da177e4SLinus Torvalds static __initdata char *message; 451da177e4SLinus Torvalds static void __init error(char *x) 461da177e4SLinus Torvalds { 471da177e4SLinus Torvalds if (!message) 481da177e4SLinus Torvalds message = x; 491da177e4SLinus Torvalds } 501da177e4SLinus Torvalds 51dd23e809SFlorian Fainelli static void panic_show_mem(const char *fmt, ...) 52dd23e809SFlorian Fainelli { 53dd23e809SFlorian Fainelli va_list args; 54dd23e809SFlorian Fainelli 55dd23e809SFlorian Fainelli show_mem(0, NULL); 56dd23e809SFlorian Fainelli va_start(args, fmt); 57dd23e809SFlorian Fainelli panic(fmt, args); 58dd23e809SFlorian Fainelli va_end(args); 59dd23e809SFlorian Fainelli } 60dd23e809SFlorian Fainelli 611da177e4SLinus Torvalds /* link hash */ 621da177e4SLinus Torvalds 636a050da4SMark Huang #define N_ALIGN(len) ((((len) + 1) & ~3) + 2) 646a050da4SMark Huang 651da177e4SLinus Torvalds static __initdata struct hash { 661da177e4SLinus Torvalds int ino, minor, major; 67685dd2d5SAl Viro umode_t mode; 681da177e4SLinus Torvalds struct hash *next; 696a050da4SMark Huang char name[N_ALIGN(PATH_MAX)]; 701da177e4SLinus Torvalds } *head[32]; 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds static inline int hash(int major, int minor, int ino) 731da177e4SLinus Torvalds { 741da177e4SLinus Torvalds unsigned long tmp = ino + minor + (major << 3); 751da177e4SLinus Torvalds tmp += tmp >> 5; 761da177e4SLinus Torvalds return tmp & 31; 771da177e4SLinus Torvalds } 781da177e4SLinus Torvalds 792139a7fbSH. Peter Anvin static char __init *find_link(int major, int minor, int ino, 80685dd2d5SAl Viro umode_t mode, char *name) 811da177e4SLinus Torvalds { 821da177e4SLinus Torvalds struct hash **p, *q; 831da177e4SLinus Torvalds for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) { 841da177e4SLinus Torvalds if ((*p)->ino != ino) 851da177e4SLinus Torvalds continue; 861da177e4SLinus Torvalds if ((*p)->minor != minor) 871da177e4SLinus Torvalds continue; 881da177e4SLinus Torvalds if ((*p)->major != major) 891da177e4SLinus Torvalds continue; 902139a7fbSH. Peter Anvin if (((*p)->mode ^ mode) & S_IFMT) 912139a7fbSH. Peter Anvin continue; 921da177e4SLinus Torvalds return (*p)->name; 931da177e4SLinus Torvalds } 943265e66bSThomas Petazzoni q = kmalloc(sizeof(struct hash), GFP_KERNEL); 951da177e4SLinus Torvalds if (!q) 96dd23e809SFlorian Fainelli panic_show_mem("can't allocate link hash entry"); 971da177e4SLinus Torvalds q->major = major; 982139a7fbSH. Peter Anvin q->minor = minor; 992139a7fbSH. Peter Anvin q->ino = ino; 1002139a7fbSH. Peter Anvin q->mode = mode; 1016a050da4SMark Huang strcpy(q->name, name); 1021da177e4SLinus Torvalds q->next = NULL; 1031da177e4SLinus Torvalds *p = q; 1041da177e4SLinus Torvalds return NULL; 1051da177e4SLinus Torvalds } 1061da177e4SLinus Torvalds 1071da177e4SLinus Torvalds static void __init free_hash(void) 1081da177e4SLinus Torvalds { 1091da177e4SLinus Torvalds struct hash **p, *q; 1101da177e4SLinus Torvalds for (p = head; p < head + 32; p++) { 1111da177e4SLinus Torvalds while (*p) { 1121da177e4SLinus Torvalds q = *p; 1131da177e4SLinus Torvalds *p = q->next; 1143265e66bSThomas Petazzoni kfree(q); 1151da177e4SLinus Torvalds } 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds } 1181da177e4SLinus Torvalds 119e35c4c64SArnd Bergmann static long __init do_utime(char *filename, time64_t mtime) 120889d51a1SNye Liu { 121aaed2dd8SDeepa Dinamani struct timespec64 t[2]; 122889d51a1SNye Liu 123889d51a1SNye Liu t[0].tv_sec = mtime; 124889d51a1SNye Liu t[0].tv_nsec = 0; 125889d51a1SNye Liu t[1].tv_sec = mtime; 126889d51a1SNye Liu t[1].tv_nsec = 0; 127235e5793SChristoph Hellwig return init_utimes(filename, t); 128889d51a1SNye Liu } 129889d51a1SNye Liu 130889d51a1SNye Liu static __initdata LIST_HEAD(dir_list); 131889d51a1SNye Liu struct dir_entry { 132889d51a1SNye Liu struct list_head list; 133889d51a1SNye Liu char *name; 134e35c4c64SArnd Bergmann time64_t mtime; 135889d51a1SNye Liu }; 136889d51a1SNye Liu 137e35c4c64SArnd Bergmann static void __init dir_add(const char *name, time64_t mtime) 138889d51a1SNye Liu { 139889d51a1SNye Liu struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL); 140889d51a1SNye Liu if (!de) 141dd23e809SFlorian Fainelli panic_show_mem("can't allocate dir_entry buffer"); 142889d51a1SNye Liu INIT_LIST_HEAD(&de->list); 143889d51a1SNye Liu de->name = kstrdup(name, GFP_KERNEL); 144889d51a1SNye Liu de->mtime = mtime; 145889d51a1SNye Liu list_add(&de->list, &dir_list); 146889d51a1SNye Liu } 147889d51a1SNye Liu 148889d51a1SNye Liu static void __init dir_utime(void) 149889d51a1SNye Liu { 150889d51a1SNye Liu struct dir_entry *de, *tmp; 151889d51a1SNye Liu list_for_each_entry_safe(de, tmp, &dir_list, list) { 152889d51a1SNye Liu list_del(&de->list); 153889d51a1SNye Liu do_utime(de->name, de->mtime); 154889d51a1SNye Liu kfree(de->name); 155889d51a1SNye Liu kfree(de); 156889d51a1SNye Liu } 157889d51a1SNye Liu } 158889d51a1SNye Liu 159e35c4c64SArnd Bergmann static __initdata time64_t mtime; 160889d51a1SNye Liu 1611da177e4SLinus Torvalds /* cpio header parsing */ 1621da177e4SLinus Torvalds 1631da177e4SLinus Torvalds static __initdata unsigned long ino, major, minor, nlink; 164685dd2d5SAl Viro static __initdata umode_t mode; 1651da177e4SLinus Torvalds static __initdata unsigned long body_len, name_len; 1661da177e4SLinus Torvalds static __initdata uid_t uid; 1671da177e4SLinus Torvalds static __initdata gid_t gid; 1681da177e4SLinus Torvalds static __initdata unsigned rdev; 1691da177e4SLinus Torvalds 1701da177e4SLinus Torvalds static void __init parse_header(char *s) 1711da177e4SLinus Torvalds { 1721da177e4SLinus Torvalds unsigned long parsed[12]; 1731da177e4SLinus Torvalds char buf[9]; 1741da177e4SLinus Torvalds int i; 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds buf[8] = '\0'; 1771da177e4SLinus Torvalds for (i = 0, s += 6; i < 12; i++, s += 8) { 1781da177e4SLinus Torvalds memcpy(buf, s, 8); 1791da177e4SLinus Torvalds parsed[i] = simple_strtoul(buf, NULL, 16); 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds ino = parsed[0]; 1821da177e4SLinus Torvalds mode = parsed[1]; 1831da177e4SLinus Torvalds uid = parsed[2]; 1841da177e4SLinus Torvalds gid = parsed[3]; 1851da177e4SLinus Torvalds nlink = parsed[4]; 186e35c4c64SArnd Bergmann mtime = parsed[5]; /* breaks in y2106 */ 1871da177e4SLinus Torvalds body_len = parsed[6]; 1881da177e4SLinus Torvalds major = parsed[7]; 1891da177e4SLinus Torvalds minor = parsed[8]; 1901da177e4SLinus Torvalds rdev = new_encode_dev(MKDEV(parsed[9], parsed[10])); 1911da177e4SLinus Torvalds name_len = parsed[11]; 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds /* FSM */ 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds static __initdata enum state { 1971da177e4SLinus Torvalds Start, 1981da177e4SLinus Torvalds Collect, 1991da177e4SLinus Torvalds GotHeader, 2001da177e4SLinus Torvalds SkipIt, 2011da177e4SLinus Torvalds GotName, 2021da177e4SLinus Torvalds CopyFile, 2031da177e4SLinus Torvalds GotSymlink, 2041da177e4SLinus Torvalds Reset 2051da177e4SLinus Torvalds } state, next_state; 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds static __initdata char *victim; 208c34d85acSMark Rustad static unsigned long byte_count __initdata; 2091da177e4SLinus Torvalds static __initdata loff_t this_header, next_header; 2101da177e4SLinus Torvalds 211b0a5ab93SAl Viro static inline void __init eat(unsigned n) 2121da177e4SLinus Torvalds { 2131da177e4SLinus Torvalds victim += n; 2141da177e4SLinus Torvalds this_header += n; 215c34d85acSMark Rustad byte_count -= n; 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds static __initdata char *collected; 219d97b07c5SYinghai Lu static long remains __initdata; 2201da177e4SLinus Torvalds static __initdata char *collect; 2211da177e4SLinus Torvalds 2221da177e4SLinus Torvalds static void __init read_into(char *buf, unsigned size, enum state next) 2231da177e4SLinus Torvalds { 224c34d85acSMark Rustad if (byte_count >= size) { 2251da177e4SLinus Torvalds collected = victim; 2261da177e4SLinus Torvalds eat(size); 2271da177e4SLinus Torvalds state = next; 2281da177e4SLinus Torvalds } else { 2291da177e4SLinus Torvalds collect = collected = buf; 2301da177e4SLinus Torvalds remains = size; 2311da177e4SLinus Torvalds next_state = next; 2321da177e4SLinus Torvalds state = Collect; 2331da177e4SLinus Torvalds } 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds static __initdata char *header_buf, *symlink_buf, *name_buf; 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds static int __init do_start(void) 2391da177e4SLinus Torvalds { 2401da177e4SLinus Torvalds read_into(header_buf, 110, GotHeader); 2411da177e4SLinus Torvalds return 0; 2421da177e4SLinus Torvalds } 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds static int __init do_collect(void) 2451da177e4SLinus Torvalds { 246d97b07c5SYinghai Lu unsigned long n = remains; 247c34d85acSMark Rustad if (byte_count < n) 248c34d85acSMark Rustad n = byte_count; 2491da177e4SLinus Torvalds memcpy(collect, victim, n); 2501da177e4SLinus Torvalds eat(n); 2511da177e4SLinus Torvalds collect += n; 2521da177e4SLinus Torvalds if ((remains -= n) != 0) 2531da177e4SLinus Torvalds return 1; 2541da177e4SLinus Torvalds state = next_state; 2551da177e4SLinus Torvalds return 0; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds static int __init do_header(void) 2591da177e4SLinus Torvalds { 2602e591bbcSArjan van de Ven if (memcmp(collected, "070707", 6)==0) { 2612e591bbcSArjan van de Ven error("incorrect cpio method used: use -H newc option"); 2622e591bbcSArjan van de Ven return 1; 2632e591bbcSArjan van de Ven } 2641da177e4SLinus Torvalds if (memcmp(collected, "070701", 6)) { 2651da177e4SLinus Torvalds error("no cpio magic"); 2661da177e4SLinus Torvalds return 1; 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds parse_header(collected); 2691da177e4SLinus Torvalds next_header = this_header + N_ALIGN(name_len) + body_len; 2701da177e4SLinus Torvalds next_header = (next_header + 3) & ~3; 2711da177e4SLinus Torvalds state = SkipIt; 2721da177e4SLinus Torvalds if (name_len <= 0 || name_len > PATH_MAX) 2731da177e4SLinus Torvalds return 0; 2741da177e4SLinus Torvalds if (S_ISLNK(mode)) { 2751da177e4SLinus Torvalds if (body_len > PATH_MAX) 2761da177e4SLinus Torvalds return 0; 2771da177e4SLinus Torvalds collect = collected = symlink_buf; 2781da177e4SLinus Torvalds remains = N_ALIGN(name_len) + body_len; 2791da177e4SLinus Torvalds next_state = GotSymlink; 2801da177e4SLinus Torvalds state = Collect; 2811da177e4SLinus Torvalds return 0; 2821da177e4SLinus Torvalds } 2831da177e4SLinus Torvalds if (S_ISREG(mode) || !body_len) 2841da177e4SLinus Torvalds read_into(name_buf, N_ALIGN(name_len), GotName); 2851da177e4SLinus Torvalds return 0; 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds static int __init do_skip(void) 2891da177e4SLinus Torvalds { 290c34d85acSMark Rustad if (this_header + byte_count < next_header) { 291c34d85acSMark Rustad eat(byte_count); 2921da177e4SLinus Torvalds return 1; 2931da177e4SLinus Torvalds } else { 2941da177e4SLinus Torvalds eat(next_header - this_header); 2951da177e4SLinus Torvalds state = next_state; 2961da177e4SLinus Torvalds return 0; 2971da177e4SLinus Torvalds } 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds 3001da177e4SLinus Torvalds static int __init do_reset(void) 3011da177e4SLinus Torvalds { 302c34d85acSMark Rustad while (byte_count && *victim == '\0') 3031da177e4SLinus Torvalds eat(1); 304c34d85acSMark Rustad if (byte_count && (this_header & 3)) 3051da177e4SLinus Torvalds error("broken padding"); 3061da177e4SLinus Torvalds return 1; 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds 309c34d85acSMark Rustad static void __init clean_path(char *path, umode_t fmode) 3102139a7fbSH. Peter Anvin { 311046aa126SArnd Bergmann struct kstat st; 3122139a7fbSH. Peter Anvin 3137b81ce7cSBarret Rhoden if (!init_stat(path, &st, AT_SYMLINK_NOFOLLOW) && 314716308a5SChristoph Hellwig (st.mode ^ fmode) & S_IFMT) { 315046aa126SArnd Bergmann if (S_ISDIR(st.mode)) 31620cce026SChristoph Hellwig init_rmdir(path); 3172139a7fbSH. Peter Anvin else 3188fb9f73eSChristoph Hellwig init_unlink(path); 3192139a7fbSH. Peter Anvin } 3202139a7fbSH. Peter Anvin } 3212139a7fbSH. Peter Anvin 3227c0950d4SLi Zhijian static int __init maybe_link(void) 3237c0950d4SLi Zhijian { 3247c0950d4SLi Zhijian if (nlink >= 2) { 3257c0950d4SLi Zhijian char *old = find_link(major, minor, ino, mode, collected); 3267c0950d4SLi Zhijian if (old) { 3277c0950d4SLi Zhijian clean_path(collected, 0); 328812931d6SChristoph Hellwig return (init_link(old, collected) < 0) ? -1 : 1; 3297c0950d4SLi Zhijian } 3307c0950d4SLi Zhijian } 3317c0950d4SLi Zhijian return 0; 3327c0950d4SLi Zhijian } 3337c0950d4SLi Zhijian 334bf6419e4SChristoph Hellwig static __initdata struct file *wfile; 335bf6419e4SChristoph Hellwig static __initdata loff_t wfile_pos; 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds static int __init do_name(void) 3381da177e4SLinus Torvalds { 3391da177e4SLinus Torvalds state = SkipIt; 3401da177e4SLinus Torvalds next_state = Reset; 3411da177e4SLinus Torvalds if (strcmp(collected, "TRAILER!!!") == 0) { 3421da177e4SLinus Torvalds free_hash(); 3431da177e4SLinus Torvalds return 0; 3441da177e4SLinus Torvalds } 3452139a7fbSH. Peter Anvin clean_path(collected, mode); 3461da177e4SLinus Torvalds if (S_ISREG(mode)) { 3472139a7fbSH. Peter Anvin int ml = maybe_link(); 3482139a7fbSH. Peter Anvin if (ml >= 0) { 3492139a7fbSH. Peter Anvin int openflags = O_WRONLY|O_CREAT; 3502139a7fbSH. Peter Anvin if (ml != 1) 3512139a7fbSH. Peter Anvin openflags |= O_TRUNC; 352bf6419e4SChristoph Hellwig wfile = filp_open(collected, openflags, mode); 353bf6419e4SChristoph Hellwig if (IS_ERR(wfile)) 354bf6419e4SChristoph Hellwig return 0; 355bf6419e4SChristoph Hellwig wfile_pos = 0; 3562139a7fbSH. Peter Anvin 357bf6419e4SChristoph Hellwig vfs_fchown(wfile, uid, gid); 358bf6419e4SChristoph Hellwig vfs_fchmod(wfile, mode); 359d20d5a74SRandy Robertson if (body_len) 360bf6419e4SChristoph Hellwig vfs_truncate(&wfile->f_path, body_len); 3611da177e4SLinus Torvalds state = CopyFile; 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds } else if (S_ISDIR(mode)) { 36483ff98c3SChristoph Hellwig init_mkdir(collected, mode); 365b873498fSChristoph Hellwig init_chown(collected, uid, gid, 0); 3661097742eSChristoph Hellwig init_chmod(collected, mode); 367889d51a1SNye Liu dir_add(collected, mtime); 3681da177e4SLinus Torvalds } else if (S_ISBLK(mode) || S_ISCHR(mode) || 3691da177e4SLinus Torvalds S_ISFIFO(mode) || S_ISSOCK(mode)) { 3701da177e4SLinus Torvalds if (maybe_link() == 0) { 3715fee64fcSChristoph Hellwig init_mknod(collected, mode, rdev); 372b873498fSChristoph Hellwig init_chown(collected, uid, gid, 0); 3731097742eSChristoph Hellwig init_chmod(collected, mode); 374889d51a1SNye Liu do_utime(collected, mtime); 3751da177e4SLinus Torvalds } 3761da177e4SLinus Torvalds } 3771da177e4SLinus Torvalds return 0; 3781da177e4SLinus Torvalds } 3791da177e4SLinus Torvalds 3801da177e4SLinus Torvalds static int __init do_copy(void) 3811da177e4SLinus Torvalds { 382c34d85acSMark Rustad if (byte_count >= body_len) { 38338b08223SChristoph Hellwig struct timespec64 t[2] = { }; 384bf6419e4SChristoph Hellwig if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len) 3859687fd91SDavid Engraf error("write error"); 38638b08223SChristoph Hellwig 38738b08223SChristoph Hellwig t[0].tv_sec = mtime; 38838b08223SChristoph Hellwig t[1].tv_sec = mtime; 38938b08223SChristoph Hellwig vfs_utimes(&wfile->f_path, t); 39038b08223SChristoph Hellwig 391bf6419e4SChristoph Hellwig fput(wfile); 3921da177e4SLinus Torvalds eat(body_len); 3931da177e4SLinus Torvalds state = SkipIt; 3941da177e4SLinus Torvalds return 0; 3951da177e4SLinus Torvalds } else { 396bf6419e4SChristoph Hellwig if (xwrite(wfile, victim, byte_count, &wfile_pos) != byte_count) 3979687fd91SDavid Engraf error("write error"); 398c34d85acSMark Rustad body_len -= byte_count; 399c34d85acSMark Rustad eat(byte_count); 4001da177e4SLinus Torvalds return 1; 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds 4041da177e4SLinus Torvalds static int __init do_symlink(void) 4051da177e4SLinus Torvalds { 4061da177e4SLinus Torvalds collected[N_ALIGN(name_len) + body_len] = '\0'; 4072139a7fbSH. Peter Anvin clean_path(collected, 0); 408cd3acb6aSChristoph Hellwig init_symlink(collected + N_ALIGN(name_len), collected); 409b873498fSChristoph Hellwig init_chown(collected, uid, gid, AT_SYMLINK_NOFOLLOW); 410889d51a1SNye Liu do_utime(collected, mtime); 4111da177e4SLinus Torvalds state = SkipIt; 4121da177e4SLinus Torvalds next_state = Reset; 4131da177e4SLinus Torvalds return 0; 4141da177e4SLinus Torvalds } 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds static __initdata int (*actions[])(void) = { 4171da177e4SLinus Torvalds [Start] = do_start, 4181da177e4SLinus Torvalds [Collect] = do_collect, 4191da177e4SLinus Torvalds [GotHeader] = do_header, 4201da177e4SLinus Torvalds [SkipIt] = do_skip, 4211da177e4SLinus Torvalds [GotName] = do_name, 4221da177e4SLinus Torvalds [CopyFile] = do_copy, 4231da177e4SLinus Torvalds [GotSymlink] = do_symlink, 4241da177e4SLinus Torvalds [Reset] = do_reset, 4251da177e4SLinus Torvalds }; 4261da177e4SLinus Torvalds 427d97b07c5SYinghai Lu static long __init write_buffer(char *buf, unsigned long len) 4281da177e4SLinus Torvalds { 429c34d85acSMark Rustad byte_count = len; 4301da177e4SLinus Torvalds victim = buf; 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds while (!actions[state]()) 4331da177e4SLinus Torvalds ; 434c34d85acSMark Rustad return len - byte_count; 4351da177e4SLinus Torvalds } 4361da177e4SLinus Torvalds 437d97b07c5SYinghai Lu static long __init flush_buffer(void *bufv, unsigned long len) 4381da177e4SLinus Torvalds { 43930d65dbfSAlain Knaff char *buf = (char *) bufv; 440d97b07c5SYinghai Lu long written; 441d97b07c5SYinghai Lu long origLen = len; 4421da177e4SLinus Torvalds if (message) 44330d65dbfSAlain Knaff return -1; 4441da177e4SLinus Torvalds while ((written = write_buffer(buf, len)) < len && !message) { 4451da177e4SLinus Torvalds char c = buf[written]; 4461da177e4SLinus Torvalds if (c == '0') { 4471da177e4SLinus Torvalds buf += written; 4481da177e4SLinus Torvalds len -= written; 4491da177e4SLinus Torvalds state = Start; 4501da177e4SLinus Torvalds } else if (c == 0) { 4511da177e4SLinus Torvalds buf += written; 4521da177e4SLinus Torvalds len -= written; 4531da177e4SLinus Torvalds state = Reset; 4541da177e4SLinus Torvalds } else 455e5eed351SDavid Engraf error("junk within compressed archive"); 4561da177e4SLinus Torvalds } 45730d65dbfSAlain Knaff return origLen; 4581da177e4SLinus Torvalds } 4591da177e4SLinus Torvalds 460d97b07c5SYinghai Lu static unsigned long my_inptr; /* index of next byte to be processed in inbuf */ 4611da177e4SLinus Torvalds 462889c92d2SH. Peter Anvin #include <linux/decompress/generic.h> 4631da177e4SLinus Torvalds 464d97b07c5SYinghai Lu static char * __init unpack_to_rootfs(char *buf, unsigned long len) 4651da177e4SLinus Torvalds { 466d97b07c5SYinghai Lu long written; 467889c92d2SH. Peter Anvin decompress_fn decompress; 46823a22d57SH. Peter Anvin const char *compress_name; 46923a22d57SH. Peter Anvin static __initdata char msg_buf[64]; 470889c92d2SH. Peter Anvin 4713265e66bSThomas Petazzoni header_buf = kmalloc(110, GFP_KERNEL); 4723265e66bSThomas Petazzoni symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL); 4733265e66bSThomas Petazzoni name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL); 47430d65dbfSAlain Knaff 47530d65dbfSAlain Knaff if (!header_buf || !symlink_buf || !name_buf) 476dd23e809SFlorian Fainelli panic_show_mem("can't allocate buffers"); 47730d65dbfSAlain Knaff 4781da177e4SLinus Torvalds state = Start; 4791da177e4SLinus Torvalds this_header = 0; 4801da177e4SLinus Torvalds message = NULL; 4811da177e4SLinus Torvalds while (!message && len) { 4821da177e4SLinus Torvalds loff_t saved_offset = this_header; 4831da177e4SLinus Torvalds if (*buf == '0' && !(this_header & 3)) { 4841da177e4SLinus Torvalds state = Start; 4851da177e4SLinus Torvalds written = write_buffer(buf, len); 4861da177e4SLinus Torvalds buf += written; 4871da177e4SLinus Torvalds len -= written; 4881da177e4SLinus Torvalds continue; 4891da177e4SLinus Torvalds } 4901da177e4SLinus Torvalds if (!*buf) { 4911da177e4SLinus Torvalds buf++; 4921da177e4SLinus Torvalds len--; 4931da177e4SLinus Torvalds this_header++; 4941da177e4SLinus Torvalds continue; 4951da177e4SLinus Torvalds } 4961da177e4SLinus Torvalds this_header = 0; 49723a22d57SH. Peter Anvin decompress = decompress_method(buf, len, &compress_name); 4986aa7a29aSDaniel M. Weeks pr_debug("Detected %s compressed data\n", compress_name); 49954291362SPhillip Lougher if (decompress) { 500d97b07c5SYinghai Lu int res = decompress(buf, len, NULL, flush_buffer, NULL, 501889c92d2SH. Peter Anvin &my_inptr, error); 50254291362SPhillip Lougher if (res) 50354291362SPhillip Lougher error("decompressor failed"); 50454291362SPhillip Lougher } else if (compress_name) { 50523a22d57SH. Peter Anvin if (!message) { 50623a22d57SH. Peter Anvin snprintf(msg_buf, sizeof msg_buf, 50723a22d57SH. Peter Anvin "compression method %s not configured", 50823a22d57SH. Peter Anvin compress_name); 50923a22d57SH. Peter Anvin message = msg_buf; 51023a22d57SH. Peter Anvin } 511df37bd15SPhillip Lougher } else 512e5eed351SDavid Engraf error("invalid magic at start of compressed archive"); 5131da177e4SLinus Torvalds if (state != Reset) 514e5eed351SDavid Engraf error("junk at the end of compressed archive"); 51530d65dbfSAlain Knaff this_header = saved_offset + my_inptr; 51630d65dbfSAlain Knaff buf += my_inptr; 51730d65dbfSAlain Knaff len -= my_inptr; 5181da177e4SLinus Torvalds } 519889d51a1SNye Liu dir_utime(); 5203265e66bSThomas Petazzoni kfree(name_buf); 5213265e66bSThomas Petazzoni kfree(symlink_buf); 5223265e66bSThomas Petazzoni kfree(header_buf); 5231da177e4SLinus Torvalds return message; 5241da177e4SLinus Torvalds } 5251da177e4SLinus Torvalds 5260a7b35cbSMichael Neuling static int __initdata do_retain_initrd; 5270a7b35cbSMichael Neuling 5280a7b35cbSMichael Neuling static int __init retain_initrd_param(char *str) 5290a7b35cbSMichael Neuling { 5300a7b35cbSMichael Neuling if (*str) 5310a7b35cbSMichael Neuling return 0; 5320a7b35cbSMichael Neuling do_retain_initrd = 1; 5330a7b35cbSMichael Neuling return 1; 5340a7b35cbSMichael Neuling } 5350a7b35cbSMichael Neuling __setup("retain_initrd", retain_initrd_param); 5360a7b35cbSMichael Neuling 537d8ae8a37SChristoph Hellwig #ifdef CONFIG_ARCH_HAS_KEEPINITRD 538d8ae8a37SChristoph Hellwig static int __init keepinitrd_setup(char *__unused) 539d8ae8a37SChristoph Hellwig { 540d8ae8a37SChristoph Hellwig do_retain_initrd = 1; 541d8ae8a37SChristoph Hellwig return 1; 542d8ae8a37SChristoph Hellwig } 543d8ae8a37SChristoph Hellwig __setup("keepinitrd", keepinitrd_setup); 544d8ae8a37SChristoph Hellwig #endif 545d8ae8a37SChristoph Hellwig 546e7cb072eSRasmus Villemoes static bool __initdata initramfs_async = true; 547e7cb072eSRasmus Villemoes static int __init initramfs_async_setup(char *str) 548e7cb072eSRasmus Villemoes { 549e7cb072eSRasmus Villemoes strtobool(str, &initramfs_async); 550e7cb072eSRasmus Villemoes return 1; 551e7cb072eSRasmus Villemoes } 552e7cb072eSRasmus Villemoes __setup("initramfs_async=", initramfs_async_setup); 553e7cb072eSRasmus Villemoes 554ffe8018cSHendrik Brueckner extern char __initramfs_start[]; 555ffe8018cSHendrik Brueckner extern unsigned long __initramfs_size; 5561da177e4SLinus Torvalds #include <linux/initrd.h> 5579c15e852SHaren Myneni #include <linux/kexec.h> 5580f3d2bd5SJan Beulich 559c72160feSKefeng Wang void __init reserve_initrd_mem(void) 560c72160feSKefeng Wang { 561c72160feSKefeng Wang phys_addr_t start; 562c72160feSKefeng Wang unsigned long size; 563c72160feSKefeng Wang 564c72160feSKefeng Wang /* Ignore the virtul address computed during device tree parsing */ 565c72160feSKefeng Wang initrd_start = initrd_end = 0; 566c72160feSKefeng Wang 567c72160feSKefeng Wang if (!phys_initrd_size) 568c72160feSKefeng Wang return; 569c72160feSKefeng Wang /* 570c72160feSKefeng Wang * Round the memory region to page boundaries as per free_initrd_mem() 571c72160feSKefeng Wang * This allows us to detect whether the pages overlapping the initrd 572c72160feSKefeng Wang * are in use, but more importantly, reserves the entire set of pages 573c72160feSKefeng Wang * as we don't want these pages allocated for other purposes. 574c72160feSKefeng Wang */ 575c72160feSKefeng Wang start = round_down(phys_initrd_start, PAGE_SIZE); 576c72160feSKefeng Wang size = phys_initrd_size + (phys_initrd_start - start); 577c72160feSKefeng Wang size = round_up(size, PAGE_SIZE); 578c72160feSKefeng Wang 579c72160feSKefeng Wang if (!memblock_is_region_memory(start, size)) { 580c72160feSKefeng Wang pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region", 581c72160feSKefeng Wang (u64)start, size); 582c72160feSKefeng Wang goto disable; 583c72160feSKefeng Wang } 584c72160feSKefeng Wang 585c72160feSKefeng Wang if (memblock_is_region_reserved(start, size)) { 586c72160feSKefeng Wang pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region\n", 587c72160feSKefeng Wang (u64)start, size); 588c72160feSKefeng Wang goto disable; 589c72160feSKefeng Wang } 590c72160feSKefeng Wang 591c72160feSKefeng Wang memblock_reserve(start, size); 592c72160feSKefeng Wang /* Now convert initrd to virtual addresses */ 593c72160feSKefeng Wang initrd_start = (unsigned long)__va(phys_initrd_start); 594c72160feSKefeng Wang initrd_end = initrd_start + phys_initrd_size; 595c72160feSKefeng Wang initrd_below_start_ok = 1; 596c72160feSKefeng Wang 597c72160feSKefeng Wang return; 598c72160feSKefeng Wang disable: 599c72160feSKefeng Wang pr_cont(" - disabling initrd\n"); 600c72160feSKefeng Wang initrd_start = 0; 601c72160feSKefeng Wang initrd_end = 0; 602c72160feSKefeng Wang } 603c72160feSKefeng Wang 60455d5b7ddSArnd Bergmann void __weak __init free_initrd_mem(unsigned long start, unsigned long end) 6054afd58e1SChristoph Hellwig { 606899ee4afSMike Rapoport #ifdef CONFIG_ARCH_KEEP_MEMBLOCK 607899ee4afSMike Rapoport unsigned long aligned_start = ALIGN_DOWN(start, PAGE_SIZE); 608899ee4afSMike Rapoport unsigned long aligned_end = ALIGN(end, PAGE_SIZE); 609899ee4afSMike Rapoport 610*4421cca0SMike Rapoport memblock_free((void *)aligned_start, aligned_end - aligned_start); 611899ee4afSMike Rapoport #endif 612899ee4afSMike Rapoport 613f94f7434SChristoph Hellwig free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM, 614f94f7434SChristoph Hellwig "initrd"); 6154afd58e1SChristoph Hellwig } 6164afd58e1SChristoph Hellwig 6172965faa5SDave Young #ifdef CONFIG_KEXEC_CORE 618e99332e7SLinus Torvalds static bool __init kexec_free_initrd(void) 61923091e28SChristoph Hellwig { 6209c15e852SHaren Myneni unsigned long crashk_start = (unsigned long)__va(crashk_res.start); 6219c15e852SHaren Myneni unsigned long crashk_end = (unsigned long)__va(crashk_res.end); 6229c15e852SHaren Myneni 6239c15e852SHaren Myneni /* 6249c15e852SHaren Myneni * If the initrd region is overlapped with crashkernel reserved region, 6259c15e852SHaren Myneni * free only memory that is not part of crashkernel region. 6269c15e852SHaren Myneni */ 62723091e28SChristoph Hellwig if (initrd_start >= crashk_end || initrd_end <= crashk_start) 62823091e28SChristoph Hellwig return false; 62923091e28SChristoph Hellwig 6309c15e852SHaren Myneni /* 63123091e28SChristoph Hellwig * Initialize initrd memory region since the kexec boot does not do. 6329c15e852SHaren Myneni */ 6339c15e852SHaren Myneni memset((void *)initrd_start, 0, initrd_end - initrd_start); 6349c15e852SHaren Myneni if (initrd_start < crashk_start) 6359c15e852SHaren Myneni free_initrd_mem(initrd_start, crashk_start); 6369c15e852SHaren Myneni if (initrd_end > crashk_end) 6379c15e852SHaren Myneni free_initrd_mem(crashk_end, initrd_end); 63823091e28SChristoph Hellwig return true; 6390f3d2bd5SJan Beulich } 64023091e28SChristoph Hellwig #else 64123091e28SChristoph Hellwig static inline bool kexec_free_initrd(void) 64223091e28SChristoph Hellwig { 64323091e28SChristoph Hellwig return false; 64423091e28SChristoph Hellwig } 64523091e28SChristoph Hellwig #endif /* CONFIG_KEXEC_CORE */ 6460f3d2bd5SJan Beulich 647a841c673SAndrew Morton #ifdef CONFIG_BLK_DEV_RAM 6484ada1e81SGeert Uytterhoeven static void __init populate_initrd_image(char *err) 6497c184ecdSChristoph Hellwig { 6507c184ecdSChristoph Hellwig ssize_t written; 651bf6419e4SChristoph Hellwig struct file *file; 652bf6419e4SChristoph Hellwig loff_t pos = 0; 6537c184ecdSChristoph Hellwig 6547c184ecdSChristoph Hellwig unpack_to_rootfs(__initramfs_start, __initramfs_size); 6557c184ecdSChristoph Hellwig 6567c184ecdSChristoph Hellwig printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n", 6577c184ecdSChristoph Hellwig err); 658bf6419e4SChristoph Hellwig file = filp_open("/initrd.image", O_WRONLY | O_CREAT, 0700); 659bf6419e4SChristoph Hellwig if (IS_ERR(file)) 6607c184ecdSChristoph Hellwig return; 6617c184ecdSChristoph Hellwig 662bf6419e4SChristoph Hellwig written = xwrite(file, (char *)initrd_start, initrd_end - initrd_start, 663bf6419e4SChristoph Hellwig &pos); 6647c184ecdSChristoph Hellwig if (written != initrd_end - initrd_start) 6657c184ecdSChristoph Hellwig pr_err("/initrd.image: incomplete write (%zd != %ld)\n", 6667c184ecdSChristoph Hellwig written, initrd_end - initrd_start); 667bf6419e4SChristoph Hellwig fput(file); 6687c184ecdSChristoph Hellwig } 6697c184ecdSChristoph Hellwig #endif /* CONFIG_BLK_DEV_RAM */ 6707c184ecdSChristoph Hellwig 671e7cb072eSRasmus Villemoes static void __init do_populate_rootfs(void *unused, async_cookie_t cookie) 6721da177e4SLinus Torvalds { 67317a9be31SStafford Horne /* Load the built in initramfs */ 674ffe8018cSHendrik Brueckner char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size); 6751da177e4SLinus Torvalds if (err) 676dd23e809SFlorian Fainelli panic_show_mem("%s", err); /* Failed to decompress INTERNAL initramfs */ 677afef7889SChristoph Hellwig 678afef7889SChristoph Hellwig if (!initrd_start || IS_ENABLED(CONFIG_INITRAMFS_FORCE)) 679bb813f4cSTejun Heo goto done; 68054c7a891SChristoph Hellwig 681afef7889SChristoph Hellwig if (IS_ENABLED(CONFIG_BLK_DEV_RAM)) 682afef7889SChristoph Hellwig printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n"); 683afef7889SChristoph Hellwig else 684afef7889SChristoph Hellwig printk(KERN_INFO "Unpacking initramfs...\n"); 685afef7889SChristoph Hellwig 686afef7889SChristoph Hellwig err = unpack_to_rootfs((char *)initrd_start, initrd_end - initrd_start); 687afef7889SChristoph Hellwig if (err) { 6889ab6b718SChristoph Hellwig #ifdef CONFIG_BLK_DEV_RAM 6897c184ecdSChristoph Hellwig populate_initrd_image(err); 6909ab6b718SChristoph Hellwig #else 6919ab6b718SChristoph Hellwig printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err); 6929ab6b718SChristoph Hellwig #endif 69317a9be31SStafford Horne } 69423091e28SChristoph Hellwig 695afef7889SChristoph Hellwig done: 69623091e28SChristoph Hellwig /* 69723091e28SChristoph Hellwig * If the initrd region is overlapped with crashkernel reserved region, 69823091e28SChristoph Hellwig * free only memory that is not part of crashkernel region. 69923091e28SChristoph Hellwig */ 7005d59aa8fSSteven Price if (!do_retain_initrd && initrd_start && !kexec_free_initrd()) 70123091e28SChristoph Hellwig free_initrd_mem(initrd_start, initrd_end); 70223091e28SChristoph Hellwig initrd_start = 0; 70323091e28SChristoph Hellwig initrd_end = 0; 70423091e28SChristoph Hellwig 70508865514SLokesh Vutla flush_delayed_fput(); 706e7cb072eSRasmus Villemoes } 707e7cb072eSRasmus Villemoes 708e7cb072eSRasmus Villemoes static ASYNC_DOMAIN_EXCLUSIVE(initramfs_domain); 709e7cb072eSRasmus Villemoes static async_cookie_t initramfs_cookie; 710e7cb072eSRasmus Villemoes 711e7cb072eSRasmus Villemoes void wait_for_initramfs(void) 712e7cb072eSRasmus Villemoes { 713e7cb072eSRasmus Villemoes if (!initramfs_cookie) { 714e7cb072eSRasmus Villemoes /* 715e7cb072eSRasmus Villemoes * Something before rootfs_initcall wants to access 716e7cb072eSRasmus Villemoes * the filesystem/initramfs. Probably a bug. Make a 717e7cb072eSRasmus Villemoes * note, avoid deadlocking the machine, and let the 718e7cb072eSRasmus Villemoes * caller's access fail as it used to. 719e7cb072eSRasmus Villemoes */ 720e7cb072eSRasmus Villemoes pr_warn_once("wait_for_initramfs() called before rootfs_initcalls\n"); 721e7cb072eSRasmus Villemoes return; 722e7cb072eSRasmus Villemoes } 723e7cb072eSRasmus Villemoes async_synchronize_cookie_domain(initramfs_cookie + 1, &initramfs_domain); 724e7cb072eSRasmus Villemoes } 725e7cb072eSRasmus Villemoes EXPORT_SYMBOL_GPL(wait_for_initramfs); 726e7cb072eSRasmus Villemoes 727e7cb072eSRasmus Villemoes static int __init populate_rootfs(void) 728e7cb072eSRasmus Villemoes { 729e7cb072eSRasmus Villemoes initramfs_cookie = async_schedule_domain(do_populate_rootfs, NULL, 730e7cb072eSRasmus Villemoes &initramfs_domain); 731b234ed6dSRasmus Villemoes usermodehelper_enable(); 732e7cb072eSRasmus Villemoes if (!initramfs_async) 733e7cb072eSRasmus Villemoes wait_for_initramfs(); 7348d610dd5SLinus Torvalds return 0; 7351da177e4SLinus Torvalds } 7368d610dd5SLinus Torvalds rootfs_initcall(populate_rootfs); 737