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; 133e35c4c64SArnd Bergmann time64_t mtime; 134*fcb7aeddSDavid Disseldorp char name[]; 135889d51a1SNye Liu }; 136889d51a1SNye Liu 137e35c4c64SArnd Bergmann static void __init dir_add(const char *name, time64_t mtime) 138889d51a1SNye Liu { 139*fcb7aeddSDavid Disseldorp size_t nlen = strlen(name) + 1; 140*fcb7aeddSDavid Disseldorp struct dir_entry *de; 141*fcb7aeddSDavid Disseldorp 142*fcb7aeddSDavid Disseldorp de = kmalloc(sizeof(struct dir_entry) + nlen, GFP_KERNEL); 143889d51a1SNye Liu if (!de) 144dd23e809SFlorian Fainelli panic_show_mem("can't allocate dir_entry buffer"); 145889d51a1SNye Liu INIT_LIST_HEAD(&de->list); 146*fcb7aeddSDavid Disseldorp strscpy(de->name, name, nlen); 147889d51a1SNye Liu de->mtime = mtime; 148889d51a1SNye Liu list_add(&de->list, &dir_list); 149889d51a1SNye Liu } 150889d51a1SNye Liu 151889d51a1SNye Liu static void __init dir_utime(void) 152889d51a1SNye Liu { 153889d51a1SNye Liu struct dir_entry *de, *tmp; 154889d51a1SNye Liu list_for_each_entry_safe(de, tmp, &dir_list, list) { 155889d51a1SNye Liu list_del(&de->list); 156889d51a1SNye Liu do_utime(de->name, de->mtime); 157889d51a1SNye Liu kfree(de); 158889d51a1SNye Liu } 159889d51a1SNye Liu } 160889d51a1SNye Liu 161e35c4c64SArnd Bergmann static __initdata time64_t mtime; 162889d51a1SNye Liu 1631da177e4SLinus Torvalds /* cpio header parsing */ 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds static __initdata unsigned long ino, major, minor, nlink; 166685dd2d5SAl Viro static __initdata umode_t mode; 1671da177e4SLinus Torvalds static __initdata unsigned long body_len, name_len; 1681da177e4SLinus Torvalds static __initdata uid_t uid; 1691da177e4SLinus Torvalds static __initdata gid_t gid; 1701da177e4SLinus Torvalds static __initdata unsigned rdev; 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds static void __init parse_header(char *s) 1731da177e4SLinus Torvalds { 1741da177e4SLinus Torvalds unsigned long parsed[12]; 1751da177e4SLinus Torvalds char buf[9]; 1761da177e4SLinus Torvalds int i; 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds buf[8] = '\0'; 1791da177e4SLinus Torvalds for (i = 0, s += 6; i < 12; i++, s += 8) { 1801da177e4SLinus Torvalds memcpy(buf, s, 8); 1811da177e4SLinus Torvalds parsed[i] = simple_strtoul(buf, NULL, 16); 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds ino = parsed[0]; 1841da177e4SLinus Torvalds mode = parsed[1]; 1851da177e4SLinus Torvalds uid = parsed[2]; 1861da177e4SLinus Torvalds gid = parsed[3]; 1871da177e4SLinus Torvalds nlink = parsed[4]; 188e35c4c64SArnd Bergmann mtime = parsed[5]; /* breaks in y2106 */ 1891da177e4SLinus Torvalds body_len = parsed[6]; 1901da177e4SLinus Torvalds major = parsed[7]; 1911da177e4SLinus Torvalds minor = parsed[8]; 1921da177e4SLinus Torvalds rdev = new_encode_dev(MKDEV(parsed[9], parsed[10])); 1931da177e4SLinus Torvalds name_len = parsed[11]; 1941da177e4SLinus Torvalds } 1951da177e4SLinus Torvalds 1961da177e4SLinus Torvalds /* FSM */ 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds static __initdata enum state { 1991da177e4SLinus Torvalds Start, 2001da177e4SLinus Torvalds Collect, 2011da177e4SLinus Torvalds GotHeader, 2021da177e4SLinus Torvalds SkipIt, 2031da177e4SLinus Torvalds GotName, 2041da177e4SLinus Torvalds CopyFile, 2051da177e4SLinus Torvalds GotSymlink, 2061da177e4SLinus Torvalds Reset 2071da177e4SLinus Torvalds } state, next_state; 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds static __initdata char *victim; 210c34d85acSMark Rustad static unsigned long byte_count __initdata; 2111da177e4SLinus Torvalds static __initdata loff_t this_header, next_header; 2121da177e4SLinus Torvalds 213b0a5ab93SAl Viro static inline void __init eat(unsigned n) 2141da177e4SLinus Torvalds { 2151da177e4SLinus Torvalds victim += n; 2161da177e4SLinus Torvalds this_header += n; 217c34d85acSMark Rustad byte_count -= n; 2181da177e4SLinus Torvalds } 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds static __initdata char *collected; 221d97b07c5SYinghai Lu static long remains __initdata; 2221da177e4SLinus Torvalds static __initdata char *collect; 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds static void __init read_into(char *buf, unsigned size, enum state next) 2251da177e4SLinus Torvalds { 226c34d85acSMark Rustad if (byte_count >= size) { 2271da177e4SLinus Torvalds collected = victim; 2281da177e4SLinus Torvalds eat(size); 2291da177e4SLinus Torvalds state = next; 2301da177e4SLinus Torvalds } else { 2311da177e4SLinus Torvalds collect = collected = buf; 2321da177e4SLinus Torvalds remains = size; 2331da177e4SLinus Torvalds next_state = next; 2341da177e4SLinus Torvalds state = Collect; 2351da177e4SLinus Torvalds } 2361da177e4SLinus Torvalds } 2371da177e4SLinus Torvalds 2381da177e4SLinus Torvalds static __initdata char *header_buf, *symlink_buf, *name_buf; 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds static int __init do_start(void) 2411da177e4SLinus Torvalds { 2421da177e4SLinus Torvalds read_into(header_buf, 110, GotHeader); 2431da177e4SLinus Torvalds return 0; 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds static int __init do_collect(void) 2471da177e4SLinus Torvalds { 248d97b07c5SYinghai Lu unsigned long n = remains; 249c34d85acSMark Rustad if (byte_count < n) 250c34d85acSMark Rustad n = byte_count; 2511da177e4SLinus Torvalds memcpy(collect, victim, n); 2521da177e4SLinus Torvalds eat(n); 2531da177e4SLinus Torvalds collect += n; 2541da177e4SLinus Torvalds if ((remains -= n) != 0) 2551da177e4SLinus Torvalds return 1; 2561da177e4SLinus Torvalds state = next_state; 2571da177e4SLinus Torvalds return 0; 2581da177e4SLinus Torvalds } 2591da177e4SLinus Torvalds 2601da177e4SLinus Torvalds static int __init do_header(void) 2611da177e4SLinus Torvalds { 2621da177e4SLinus Torvalds if (memcmp(collected, "070701", 6)) { 263da028e4cSDavid Disseldorp if (memcmp(collected, "070707", 6) == 0) 264da028e4cSDavid Disseldorp error("incorrect cpio method used: use -H newc option"); 265da028e4cSDavid Disseldorp else 2661da177e4SLinus Torvalds error("no cpio magic"); 2671da177e4SLinus Torvalds return 1; 2681da177e4SLinus Torvalds } 2691da177e4SLinus Torvalds parse_header(collected); 2701da177e4SLinus Torvalds next_header = this_header + N_ALIGN(name_len) + body_len; 2711da177e4SLinus Torvalds next_header = (next_header + 3) & ~3; 2721da177e4SLinus Torvalds state = SkipIt; 2731da177e4SLinus Torvalds if (name_len <= 0 || name_len > PATH_MAX) 2741da177e4SLinus Torvalds return 0; 2751da177e4SLinus Torvalds if (S_ISLNK(mode)) { 2761da177e4SLinus Torvalds if (body_len > PATH_MAX) 2771da177e4SLinus Torvalds return 0; 2781da177e4SLinus Torvalds collect = collected = symlink_buf; 2791da177e4SLinus Torvalds remains = N_ALIGN(name_len) + body_len; 2801da177e4SLinus Torvalds next_state = GotSymlink; 2811da177e4SLinus Torvalds state = Collect; 2821da177e4SLinus Torvalds return 0; 2831da177e4SLinus Torvalds } 2841da177e4SLinus Torvalds if (S_ISREG(mode) || !body_len) 2851da177e4SLinus Torvalds read_into(name_buf, N_ALIGN(name_len), GotName); 2861da177e4SLinus Torvalds return 0; 2871da177e4SLinus Torvalds } 2881da177e4SLinus Torvalds 2891da177e4SLinus Torvalds static int __init do_skip(void) 2901da177e4SLinus Torvalds { 291c34d85acSMark Rustad if (this_header + byte_count < next_header) { 292c34d85acSMark Rustad eat(byte_count); 2931da177e4SLinus Torvalds return 1; 2941da177e4SLinus Torvalds } else { 2951da177e4SLinus Torvalds eat(next_header - this_header); 2961da177e4SLinus Torvalds state = next_state; 2971da177e4SLinus Torvalds return 0; 2981da177e4SLinus Torvalds } 2991da177e4SLinus Torvalds } 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds static int __init do_reset(void) 3021da177e4SLinus Torvalds { 303c34d85acSMark Rustad while (byte_count && *victim == '\0') 3041da177e4SLinus Torvalds eat(1); 305c34d85acSMark Rustad if (byte_count && (this_header & 3)) 3061da177e4SLinus Torvalds error("broken padding"); 3071da177e4SLinus Torvalds return 1; 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds 310c34d85acSMark Rustad static void __init clean_path(char *path, umode_t fmode) 3112139a7fbSH. Peter Anvin { 312046aa126SArnd Bergmann struct kstat st; 3132139a7fbSH. Peter Anvin 3147b81ce7cSBarret Rhoden if (!init_stat(path, &st, AT_SYMLINK_NOFOLLOW) && 315716308a5SChristoph Hellwig (st.mode ^ fmode) & S_IFMT) { 316046aa126SArnd Bergmann if (S_ISDIR(st.mode)) 31720cce026SChristoph Hellwig init_rmdir(path); 3182139a7fbSH. Peter Anvin else 3198fb9f73eSChristoph Hellwig init_unlink(path); 3202139a7fbSH. Peter Anvin } 3212139a7fbSH. Peter Anvin } 3222139a7fbSH. Peter Anvin 3237c0950d4SLi Zhijian static int __init maybe_link(void) 3247c0950d4SLi Zhijian { 3257c0950d4SLi Zhijian if (nlink >= 2) { 3267c0950d4SLi Zhijian char *old = find_link(major, minor, ino, mode, collected); 3277c0950d4SLi Zhijian if (old) { 3287c0950d4SLi Zhijian clean_path(collected, 0); 329812931d6SChristoph Hellwig return (init_link(old, collected) < 0) ? -1 : 1; 3307c0950d4SLi Zhijian } 3317c0950d4SLi Zhijian } 3327c0950d4SLi Zhijian return 0; 3337c0950d4SLi Zhijian } 3347c0950d4SLi Zhijian 335bf6419e4SChristoph Hellwig static __initdata struct file *wfile; 336bf6419e4SChristoph Hellwig static __initdata loff_t wfile_pos; 3371da177e4SLinus Torvalds 3381da177e4SLinus Torvalds static int __init do_name(void) 3391da177e4SLinus Torvalds { 3401da177e4SLinus Torvalds state = SkipIt; 3411da177e4SLinus Torvalds next_state = Reset; 3421da177e4SLinus Torvalds if (strcmp(collected, "TRAILER!!!") == 0) { 3431da177e4SLinus Torvalds free_hash(); 3441da177e4SLinus Torvalds return 0; 3451da177e4SLinus Torvalds } 3462139a7fbSH. Peter Anvin clean_path(collected, mode); 3471da177e4SLinus Torvalds if (S_ISREG(mode)) { 3482139a7fbSH. Peter Anvin int ml = maybe_link(); 3492139a7fbSH. Peter Anvin if (ml >= 0) { 3502139a7fbSH. Peter Anvin int openflags = O_WRONLY|O_CREAT; 3512139a7fbSH. Peter Anvin if (ml != 1) 3522139a7fbSH. Peter Anvin openflags |= O_TRUNC; 353bf6419e4SChristoph Hellwig wfile = filp_open(collected, openflags, mode); 354bf6419e4SChristoph Hellwig if (IS_ERR(wfile)) 355bf6419e4SChristoph Hellwig return 0; 356bf6419e4SChristoph Hellwig wfile_pos = 0; 3572139a7fbSH. Peter Anvin 358bf6419e4SChristoph Hellwig vfs_fchown(wfile, uid, gid); 359bf6419e4SChristoph Hellwig vfs_fchmod(wfile, mode); 360d20d5a74SRandy Robertson if (body_len) 361bf6419e4SChristoph Hellwig vfs_truncate(&wfile->f_path, body_len); 3621da177e4SLinus Torvalds state = CopyFile; 3631da177e4SLinus Torvalds } 3641da177e4SLinus Torvalds } else if (S_ISDIR(mode)) { 36583ff98c3SChristoph Hellwig init_mkdir(collected, mode); 366b873498fSChristoph Hellwig init_chown(collected, uid, gid, 0); 3671097742eSChristoph Hellwig init_chmod(collected, mode); 368889d51a1SNye Liu dir_add(collected, mtime); 3691da177e4SLinus Torvalds } else if (S_ISBLK(mode) || S_ISCHR(mode) || 3701da177e4SLinus Torvalds S_ISFIFO(mode) || S_ISSOCK(mode)) { 3711da177e4SLinus Torvalds if (maybe_link() == 0) { 3725fee64fcSChristoph Hellwig init_mknod(collected, mode, rdev); 373b873498fSChristoph Hellwig init_chown(collected, uid, gid, 0); 3741097742eSChristoph Hellwig init_chmod(collected, mode); 375889d51a1SNye Liu do_utime(collected, mtime); 3761da177e4SLinus Torvalds } 3771da177e4SLinus Torvalds } 3781da177e4SLinus Torvalds return 0; 3791da177e4SLinus Torvalds } 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds static int __init do_copy(void) 3821da177e4SLinus Torvalds { 383c34d85acSMark Rustad if (byte_count >= body_len) { 38438b08223SChristoph Hellwig struct timespec64 t[2] = { }; 385bf6419e4SChristoph Hellwig if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len) 3869687fd91SDavid Engraf error("write error"); 38738b08223SChristoph Hellwig 38838b08223SChristoph Hellwig t[0].tv_sec = mtime; 38938b08223SChristoph Hellwig t[1].tv_sec = mtime; 39038b08223SChristoph Hellwig vfs_utimes(&wfile->f_path, t); 39138b08223SChristoph Hellwig 392bf6419e4SChristoph Hellwig fput(wfile); 3931da177e4SLinus Torvalds eat(body_len); 3941da177e4SLinus Torvalds state = SkipIt; 3951da177e4SLinus Torvalds return 0; 3961da177e4SLinus Torvalds } else { 397bf6419e4SChristoph Hellwig if (xwrite(wfile, victim, byte_count, &wfile_pos) != byte_count) 3989687fd91SDavid Engraf error("write error"); 399c34d85acSMark Rustad body_len -= byte_count; 400c34d85acSMark Rustad eat(byte_count); 4011da177e4SLinus Torvalds return 1; 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds static int __init do_symlink(void) 4061da177e4SLinus Torvalds { 4071da177e4SLinus Torvalds collected[N_ALIGN(name_len) + body_len] = '\0'; 4082139a7fbSH. Peter Anvin clean_path(collected, 0); 409cd3acb6aSChristoph Hellwig init_symlink(collected + N_ALIGN(name_len), collected); 410b873498fSChristoph Hellwig init_chown(collected, uid, gid, AT_SYMLINK_NOFOLLOW); 411889d51a1SNye Liu do_utime(collected, mtime); 4121da177e4SLinus Torvalds state = SkipIt; 4131da177e4SLinus Torvalds next_state = Reset; 4141da177e4SLinus Torvalds return 0; 4151da177e4SLinus Torvalds } 4161da177e4SLinus Torvalds 4171da177e4SLinus Torvalds static __initdata int (*actions[])(void) = { 4181da177e4SLinus Torvalds [Start] = do_start, 4191da177e4SLinus Torvalds [Collect] = do_collect, 4201da177e4SLinus Torvalds [GotHeader] = do_header, 4211da177e4SLinus Torvalds [SkipIt] = do_skip, 4221da177e4SLinus Torvalds [GotName] = do_name, 4231da177e4SLinus Torvalds [CopyFile] = do_copy, 4241da177e4SLinus Torvalds [GotSymlink] = do_symlink, 4251da177e4SLinus Torvalds [Reset] = do_reset, 4261da177e4SLinus Torvalds }; 4271da177e4SLinus Torvalds 428d97b07c5SYinghai Lu static long __init write_buffer(char *buf, unsigned long len) 4291da177e4SLinus Torvalds { 430c34d85acSMark Rustad byte_count = len; 4311da177e4SLinus Torvalds victim = buf; 4321da177e4SLinus Torvalds 4331da177e4SLinus Torvalds while (!actions[state]()) 4341da177e4SLinus Torvalds ; 435c34d85acSMark Rustad return len - byte_count; 4361da177e4SLinus Torvalds } 4371da177e4SLinus Torvalds 438d97b07c5SYinghai Lu static long __init flush_buffer(void *bufv, unsigned long len) 4391da177e4SLinus Torvalds { 44030d65dbfSAlain Knaff char *buf = (char *) bufv; 441d97b07c5SYinghai Lu long written; 442d97b07c5SYinghai Lu long origLen = len; 4431da177e4SLinus Torvalds if (message) 44430d65dbfSAlain Knaff return -1; 4451da177e4SLinus Torvalds while ((written = write_buffer(buf, len)) < len && !message) { 4461da177e4SLinus Torvalds char c = buf[written]; 4471da177e4SLinus Torvalds if (c == '0') { 4481da177e4SLinus Torvalds buf += written; 4491da177e4SLinus Torvalds len -= written; 4501da177e4SLinus Torvalds state = Start; 4511da177e4SLinus Torvalds } else if (c == 0) { 4521da177e4SLinus Torvalds buf += written; 4531da177e4SLinus Torvalds len -= written; 4541da177e4SLinus Torvalds state = Reset; 4551da177e4SLinus Torvalds } else 456e5eed351SDavid Engraf error("junk within compressed archive"); 4571da177e4SLinus Torvalds } 45830d65dbfSAlain Knaff return origLen; 4591da177e4SLinus Torvalds } 4601da177e4SLinus Torvalds 461d97b07c5SYinghai Lu static unsigned long my_inptr; /* index of next byte to be processed in inbuf */ 4621da177e4SLinus Torvalds 463889c92d2SH. Peter Anvin #include <linux/decompress/generic.h> 4641da177e4SLinus Torvalds 465d97b07c5SYinghai Lu static char * __init unpack_to_rootfs(char *buf, unsigned long len) 4661da177e4SLinus Torvalds { 467d97b07c5SYinghai Lu long written; 468889c92d2SH. Peter Anvin decompress_fn decompress; 46923a22d57SH. Peter Anvin const char *compress_name; 47023a22d57SH. Peter Anvin static __initdata char msg_buf[64]; 471889c92d2SH. Peter Anvin 4723265e66bSThomas Petazzoni header_buf = kmalloc(110, GFP_KERNEL); 4733265e66bSThomas Petazzoni symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL); 4743265e66bSThomas Petazzoni name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL); 47530d65dbfSAlain Knaff 47630d65dbfSAlain Knaff if (!header_buf || !symlink_buf || !name_buf) 477dd23e809SFlorian Fainelli panic_show_mem("can't allocate buffers"); 47830d65dbfSAlain Knaff 4791da177e4SLinus Torvalds state = Start; 4801da177e4SLinus Torvalds this_header = 0; 4811da177e4SLinus Torvalds message = NULL; 4821da177e4SLinus Torvalds while (!message && len) { 4831da177e4SLinus Torvalds loff_t saved_offset = this_header; 4841da177e4SLinus Torvalds if (*buf == '0' && !(this_header & 3)) { 4851da177e4SLinus Torvalds state = Start; 4861da177e4SLinus Torvalds written = write_buffer(buf, len); 4871da177e4SLinus Torvalds buf += written; 4881da177e4SLinus Torvalds len -= written; 4891da177e4SLinus Torvalds continue; 4901da177e4SLinus Torvalds } 4911da177e4SLinus Torvalds if (!*buf) { 4921da177e4SLinus Torvalds buf++; 4931da177e4SLinus Torvalds len--; 4941da177e4SLinus Torvalds this_header++; 4951da177e4SLinus Torvalds continue; 4961da177e4SLinus Torvalds } 4971da177e4SLinus Torvalds this_header = 0; 49823a22d57SH. Peter Anvin decompress = decompress_method(buf, len, &compress_name); 4996aa7a29aSDaniel M. Weeks pr_debug("Detected %s compressed data\n", compress_name); 50054291362SPhillip Lougher if (decompress) { 501d97b07c5SYinghai Lu int res = decompress(buf, len, NULL, flush_buffer, NULL, 502889c92d2SH. Peter Anvin &my_inptr, error); 50354291362SPhillip Lougher if (res) 50454291362SPhillip Lougher error("decompressor failed"); 50554291362SPhillip Lougher } else if (compress_name) { 50623a22d57SH. Peter Anvin if (!message) { 50723a22d57SH. Peter Anvin snprintf(msg_buf, sizeof msg_buf, 50823a22d57SH. Peter Anvin "compression method %s not configured", 50923a22d57SH. Peter Anvin compress_name); 51023a22d57SH. Peter Anvin message = msg_buf; 51123a22d57SH. Peter Anvin } 512df37bd15SPhillip Lougher } else 513e5eed351SDavid Engraf error("invalid magic at start of compressed archive"); 5141da177e4SLinus Torvalds if (state != Reset) 515e5eed351SDavid Engraf error("junk at the end of compressed archive"); 51630d65dbfSAlain Knaff this_header = saved_offset + my_inptr; 51730d65dbfSAlain Knaff buf += my_inptr; 51830d65dbfSAlain Knaff len -= my_inptr; 5191da177e4SLinus Torvalds } 520889d51a1SNye Liu dir_utime(); 5213265e66bSThomas Petazzoni kfree(name_buf); 5223265e66bSThomas Petazzoni kfree(symlink_buf); 5233265e66bSThomas Petazzoni kfree(header_buf); 5241da177e4SLinus Torvalds return message; 5251da177e4SLinus Torvalds } 5261da177e4SLinus Torvalds 5270a7b35cbSMichael Neuling static int __initdata do_retain_initrd; 5280a7b35cbSMichael Neuling 5290a7b35cbSMichael Neuling static int __init retain_initrd_param(char *str) 5300a7b35cbSMichael Neuling { 5310a7b35cbSMichael Neuling if (*str) 5320a7b35cbSMichael Neuling return 0; 5330a7b35cbSMichael Neuling do_retain_initrd = 1; 5340a7b35cbSMichael Neuling return 1; 5350a7b35cbSMichael Neuling } 5360a7b35cbSMichael Neuling __setup("retain_initrd", retain_initrd_param); 5370a7b35cbSMichael Neuling 538d8ae8a37SChristoph Hellwig #ifdef CONFIG_ARCH_HAS_KEEPINITRD 539d8ae8a37SChristoph Hellwig static int __init keepinitrd_setup(char *__unused) 540d8ae8a37SChristoph Hellwig { 541d8ae8a37SChristoph Hellwig do_retain_initrd = 1; 542d8ae8a37SChristoph Hellwig return 1; 543d8ae8a37SChristoph Hellwig } 544d8ae8a37SChristoph Hellwig __setup("keepinitrd", keepinitrd_setup); 545d8ae8a37SChristoph Hellwig #endif 546d8ae8a37SChristoph Hellwig 547e7cb072eSRasmus Villemoes static bool __initdata initramfs_async = true; 548e7cb072eSRasmus Villemoes static int __init initramfs_async_setup(char *str) 549e7cb072eSRasmus Villemoes { 550e7cb072eSRasmus Villemoes strtobool(str, &initramfs_async); 551e7cb072eSRasmus Villemoes return 1; 552e7cb072eSRasmus Villemoes } 553e7cb072eSRasmus Villemoes __setup("initramfs_async=", initramfs_async_setup); 554e7cb072eSRasmus Villemoes 555ffe8018cSHendrik Brueckner extern char __initramfs_start[]; 556ffe8018cSHendrik Brueckner extern unsigned long __initramfs_size; 5571da177e4SLinus Torvalds #include <linux/initrd.h> 5589c15e852SHaren Myneni #include <linux/kexec.h> 5590f3d2bd5SJan Beulich 560c72160feSKefeng Wang void __init reserve_initrd_mem(void) 561c72160feSKefeng Wang { 562c72160feSKefeng Wang phys_addr_t start; 563c72160feSKefeng Wang unsigned long size; 564c72160feSKefeng Wang 565c72160feSKefeng Wang /* Ignore the virtul address computed during device tree parsing */ 566c72160feSKefeng Wang initrd_start = initrd_end = 0; 567c72160feSKefeng Wang 568c72160feSKefeng Wang if (!phys_initrd_size) 569c72160feSKefeng Wang return; 570c72160feSKefeng Wang /* 571c72160feSKefeng Wang * Round the memory region to page boundaries as per free_initrd_mem() 572c72160feSKefeng Wang * This allows us to detect whether the pages overlapping the initrd 573c72160feSKefeng Wang * are in use, but more importantly, reserves the entire set of pages 574c72160feSKefeng Wang * as we don't want these pages allocated for other purposes. 575c72160feSKefeng Wang */ 576c72160feSKefeng Wang start = round_down(phys_initrd_start, PAGE_SIZE); 577c72160feSKefeng Wang size = phys_initrd_size + (phys_initrd_start - start); 578c72160feSKefeng Wang size = round_up(size, PAGE_SIZE); 579c72160feSKefeng Wang 580c72160feSKefeng Wang if (!memblock_is_region_memory(start, size)) { 581c72160feSKefeng Wang pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region", 582c72160feSKefeng Wang (u64)start, size); 583c72160feSKefeng Wang goto disable; 584c72160feSKefeng Wang } 585c72160feSKefeng Wang 586c72160feSKefeng Wang if (memblock_is_region_reserved(start, size)) { 587c72160feSKefeng Wang pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region\n", 588c72160feSKefeng Wang (u64)start, size); 589c72160feSKefeng Wang goto disable; 590c72160feSKefeng Wang } 591c72160feSKefeng Wang 592c72160feSKefeng Wang memblock_reserve(start, size); 593c72160feSKefeng Wang /* Now convert initrd to virtual addresses */ 594c72160feSKefeng Wang initrd_start = (unsigned long)__va(phys_initrd_start); 595c72160feSKefeng Wang initrd_end = initrd_start + phys_initrd_size; 596c72160feSKefeng Wang initrd_below_start_ok = 1; 597c72160feSKefeng Wang 598c72160feSKefeng Wang return; 599c72160feSKefeng Wang disable: 600c72160feSKefeng Wang pr_cont(" - disabling initrd\n"); 601c72160feSKefeng Wang initrd_start = 0; 602c72160feSKefeng Wang initrd_end = 0; 603c72160feSKefeng Wang } 604c72160feSKefeng Wang 60555d5b7ddSArnd Bergmann void __weak __init free_initrd_mem(unsigned long start, unsigned long end) 6064afd58e1SChristoph Hellwig { 607899ee4afSMike Rapoport #ifdef CONFIG_ARCH_KEEP_MEMBLOCK 608899ee4afSMike Rapoport unsigned long aligned_start = ALIGN_DOWN(start, PAGE_SIZE); 609899ee4afSMike Rapoport unsigned long aligned_end = ALIGN(end, PAGE_SIZE); 610899ee4afSMike Rapoport 6114421cca0SMike Rapoport memblock_free((void *)aligned_start, aligned_end - aligned_start); 612899ee4afSMike Rapoport #endif 613899ee4afSMike Rapoport 614f94f7434SChristoph Hellwig free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM, 615f94f7434SChristoph Hellwig "initrd"); 6164afd58e1SChristoph Hellwig } 6174afd58e1SChristoph Hellwig 6182965faa5SDave Young #ifdef CONFIG_KEXEC_CORE 619e99332e7SLinus Torvalds static bool __init kexec_free_initrd(void) 62023091e28SChristoph Hellwig { 6219c15e852SHaren Myneni unsigned long crashk_start = (unsigned long)__va(crashk_res.start); 6229c15e852SHaren Myneni unsigned long crashk_end = (unsigned long)__va(crashk_res.end); 6239c15e852SHaren Myneni 6249c15e852SHaren Myneni /* 6259c15e852SHaren Myneni * If the initrd region is overlapped with crashkernel reserved region, 6269c15e852SHaren Myneni * free only memory that is not part of crashkernel region. 6279c15e852SHaren Myneni */ 62823091e28SChristoph Hellwig if (initrd_start >= crashk_end || initrd_end <= crashk_start) 62923091e28SChristoph Hellwig return false; 63023091e28SChristoph Hellwig 6319c15e852SHaren Myneni /* 63223091e28SChristoph Hellwig * Initialize initrd memory region since the kexec boot does not do. 6339c15e852SHaren Myneni */ 6349c15e852SHaren Myneni memset((void *)initrd_start, 0, initrd_end - initrd_start); 6359c15e852SHaren Myneni if (initrd_start < crashk_start) 6369c15e852SHaren Myneni free_initrd_mem(initrd_start, crashk_start); 6379c15e852SHaren Myneni if (initrd_end > crashk_end) 6389c15e852SHaren Myneni free_initrd_mem(crashk_end, initrd_end); 63923091e28SChristoph Hellwig return true; 6400f3d2bd5SJan Beulich } 64123091e28SChristoph Hellwig #else 64223091e28SChristoph Hellwig static inline bool kexec_free_initrd(void) 64323091e28SChristoph Hellwig { 64423091e28SChristoph Hellwig return false; 64523091e28SChristoph Hellwig } 64623091e28SChristoph Hellwig #endif /* CONFIG_KEXEC_CORE */ 6470f3d2bd5SJan Beulich 648a841c673SAndrew Morton #ifdef CONFIG_BLK_DEV_RAM 6494ada1e81SGeert Uytterhoeven static void __init populate_initrd_image(char *err) 6507c184ecdSChristoph Hellwig { 6517c184ecdSChristoph Hellwig ssize_t written; 652bf6419e4SChristoph Hellwig struct file *file; 653bf6419e4SChristoph Hellwig loff_t pos = 0; 6547c184ecdSChristoph Hellwig 6557c184ecdSChristoph Hellwig unpack_to_rootfs(__initramfs_start, __initramfs_size); 6567c184ecdSChristoph Hellwig 6577c184ecdSChristoph Hellwig printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n", 6587c184ecdSChristoph Hellwig err); 659bf6419e4SChristoph Hellwig file = filp_open("/initrd.image", O_WRONLY | O_CREAT, 0700); 660bf6419e4SChristoph Hellwig if (IS_ERR(file)) 6617c184ecdSChristoph Hellwig return; 6627c184ecdSChristoph Hellwig 663bf6419e4SChristoph Hellwig written = xwrite(file, (char *)initrd_start, initrd_end - initrd_start, 664bf6419e4SChristoph Hellwig &pos); 6657c184ecdSChristoph Hellwig if (written != initrd_end - initrd_start) 6667c184ecdSChristoph Hellwig pr_err("/initrd.image: incomplete write (%zd != %ld)\n", 6677c184ecdSChristoph Hellwig written, initrd_end - initrd_start); 668bf6419e4SChristoph Hellwig fput(file); 6697c184ecdSChristoph Hellwig } 6707c184ecdSChristoph Hellwig #endif /* CONFIG_BLK_DEV_RAM */ 6717c184ecdSChristoph Hellwig 672e7cb072eSRasmus Villemoes static void __init do_populate_rootfs(void *unused, async_cookie_t cookie) 6731da177e4SLinus Torvalds { 67417a9be31SStafford Horne /* Load the built in initramfs */ 675ffe8018cSHendrik Brueckner char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size); 6761da177e4SLinus Torvalds if (err) 677dd23e809SFlorian Fainelli panic_show_mem("%s", err); /* Failed to decompress INTERNAL initramfs */ 678afef7889SChristoph Hellwig 679afef7889SChristoph Hellwig if (!initrd_start || IS_ENABLED(CONFIG_INITRAMFS_FORCE)) 680bb813f4cSTejun Heo goto done; 68154c7a891SChristoph Hellwig 682afef7889SChristoph Hellwig if (IS_ENABLED(CONFIG_BLK_DEV_RAM)) 683afef7889SChristoph Hellwig printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n"); 684afef7889SChristoph Hellwig else 685afef7889SChristoph Hellwig printk(KERN_INFO "Unpacking initramfs...\n"); 686afef7889SChristoph Hellwig 687afef7889SChristoph Hellwig err = unpack_to_rootfs((char *)initrd_start, initrd_end - initrd_start); 688afef7889SChristoph Hellwig if (err) { 6899ab6b718SChristoph Hellwig #ifdef CONFIG_BLK_DEV_RAM 6907c184ecdSChristoph Hellwig populate_initrd_image(err); 6919ab6b718SChristoph Hellwig #else 6929ab6b718SChristoph Hellwig printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err); 6939ab6b718SChristoph Hellwig #endif 69417a9be31SStafford Horne } 69523091e28SChristoph Hellwig 696afef7889SChristoph Hellwig done: 69723091e28SChristoph Hellwig /* 69823091e28SChristoph Hellwig * If the initrd region is overlapped with crashkernel reserved region, 69923091e28SChristoph Hellwig * free only memory that is not part of crashkernel region. 70023091e28SChristoph Hellwig */ 7015d59aa8fSSteven Price if (!do_retain_initrd && initrd_start && !kexec_free_initrd()) 70223091e28SChristoph Hellwig free_initrd_mem(initrd_start, initrd_end); 70323091e28SChristoph Hellwig initrd_start = 0; 70423091e28SChristoph Hellwig initrd_end = 0; 70523091e28SChristoph Hellwig 70608865514SLokesh Vutla flush_delayed_fput(); 707e7cb072eSRasmus Villemoes } 708e7cb072eSRasmus Villemoes 709e7cb072eSRasmus Villemoes static ASYNC_DOMAIN_EXCLUSIVE(initramfs_domain); 710e7cb072eSRasmus Villemoes static async_cookie_t initramfs_cookie; 711e7cb072eSRasmus Villemoes 712e7cb072eSRasmus Villemoes void wait_for_initramfs(void) 713e7cb072eSRasmus Villemoes { 714e7cb072eSRasmus Villemoes if (!initramfs_cookie) { 715e7cb072eSRasmus Villemoes /* 716e7cb072eSRasmus Villemoes * Something before rootfs_initcall wants to access 717e7cb072eSRasmus Villemoes * the filesystem/initramfs. Probably a bug. Make a 718e7cb072eSRasmus Villemoes * note, avoid deadlocking the machine, and let the 719e7cb072eSRasmus Villemoes * caller's access fail as it used to. 720e7cb072eSRasmus Villemoes */ 721e7cb072eSRasmus Villemoes pr_warn_once("wait_for_initramfs() called before rootfs_initcalls\n"); 722e7cb072eSRasmus Villemoes return; 723e7cb072eSRasmus Villemoes } 724e7cb072eSRasmus Villemoes async_synchronize_cookie_domain(initramfs_cookie + 1, &initramfs_domain); 725e7cb072eSRasmus Villemoes } 726e7cb072eSRasmus Villemoes EXPORT_SYMBOL_GPL(wait_for_initramfs); 727e7cb072eSRasmus Villemoes 728e7cb072eSRasmus Villemoes static int __init populate_rootfs(void) 729e7cb072eSRasmus Villemoes { 730e7cb072eSRasmus Villemoes initramfs_cookie = async_schedule_domain(do_populate_rootfs, NULL, 731e7cb072eSRasmus Villemoes &initramfs_domain); 732b234ed6dSRasmus Villemoes usermodehelper_enable(); 733e7cb072eSRasmus Villemoes if (!initramfs_async) 734e7cb072eSRasmus Villemoes wait_for_initramfs(); 7358d610dd5SLinus Torvalds return 0; 7361da177e4SLinus Torvalds } 7378d610dd5SLinus Torvalds rootfs_initcall(populate_rootfs); 738