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> 14f3296f80SChristophe JAILLET #include <linux/kstrtox.h> 15899ee4afSMike Rapoport #include <linux/memblock.h> 16dd23e809SFlorian Fainelli #include <linux/mm.h> 17b2a74d5fSChristoph Hellwig #include <linux/namei.h> 188fb9f73eSChristoph Hellwig #include <linux/init_syscalls.h> 19b234ed6dSRasmus Villemoes #include <linux/umh.h> 201da177e4SLinus Torvalds 21386dc41cSChristian Brauner #include "do_mounts.h" 22386dc41cSChristian Brauner 23800c24dcSDavid Disseldorp static __initdata bool csum_present; 24800c24dcSDavid Disseldorp static __initdata u32 io_csum; 25800c24dcSDavid Disseldorp 26800c24dcSDavid Disseldorp static ssize_t __init xwrite(struct file *file, const unsigned char *p, 27800c24dcSDavid Disseldorp size_t count, loff_t *pos) 2838747439SYinghai Lu { 2938747439SYinghai Lu ssize_t out = 0; 3038747439SYinghai Lu 3138747439SYinghai Lu /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */ 3238747439SYinghai Lu while (count) { 33bf6419e4SChristoph Hellwig ssize_t rv = kernel_write(file, p, count, pos); 3438747439SYinghai Lu 3538747439SYinghai Lu if (rv < 0) { 3638747439SYinghai Lu if (rv == -EINTR || rv == -EAGAIN) 3738747439SYinghai Lu continue; 3838747439SYinghai Lu return out ? out : rv; 3938747439SYinghai Lu } else if (rv == 0) 4038747439SYinghai Lu break; 4138747439SYinghai Lu 42800c24dcSDavid Disseldorp if (csum_present) { 43800c24dcSDavid Disseldorp ssize_t i; 44800c24dcSDavid Disseldorp 45800c24dcSDavid Disseldorp for (i = 0; i < rv; i++) 46800c24dcSDavid Disseldorp io_csum += p[i]; 47800c24dcSDavid Disseldorp } 48800c24dcSDavid Disseldorp 4938747439SYinghai Lu p += rv; 5038747439SYinghai Lu out += rv; 5138747439SYinghai Lu count -= rv; 5238747439SYinghai Lu } 5338747439SYinghai Lu 5438747439SYinghai Lu return out; 5538747439SYinghai Lu } 5638747439SYinghai Lu 571da177e4SLinus Torvalds static __initdata char *message; 581da177e4SLinus Torvalds static void __init error(char *x) 591da177e4SLinus Torvalds { 601da177e4SLinus Torvalds if (!message) 611da177e4SLinus Torvalds message = x; 621da177e4SLinus Torvalds } 631da177e4SLinus Torvalds 64735faf92SBenjamin Gray #define panic_show_mem(fmt, ...) \ 65527ed4f7SKefeng Wang ({ show_mem(); panic(fmt, ##__VA_ARGS__); }) 66dd23e809SFlorian Fainelli 671da177e4SLinus Torvalds /* link hash */ 681da177e4SLinus Torvalds 696a050da4SMark Huang #define N_ALIGN(len) ((((len) + 1) & ~3) + 2) 706a050da4SMark Huang 711da177e4SLinus Torvalds static __initdata struct hash { 721da177e4SLinus Torvalds int ino, minor, major; 73685dd2d5SAl Viro umode_t mode; 741da177e4SLinus Torvalds struct hash *next; 756a050da4SMark Huang char name[N_ALIGN(PATH_MAX)]; 761da177e4SLinus Torvalds } *head[32]; 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds static inline int hash(int major, int minor, int ino) 791da177e4SLinus Torvalds { 801da177e4SLinus Torvalds unsigned long tmp = ino + minor + (major << 3); 811da177e4SLinus Torvalds tmp += tmp >> 5; 821da177e4SLinus Torvalds return tmp & 31; 831da177e4SLinus Torvalds } 841da177e4SLinus Torvalds 852139a7fbSH. Peter Anvin static char __init *find_link(int major, int minor, int ino, 86685dd2d5SAl Viro umode_t mode, char *name) 871da177e4SLinus Torvalds { 881da177e4SLinus Torvalds struct hash **p, *q; 891da177e4SLinus Torvalds for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) { 901da177e4SLinus Torvalds if ((*p)->ino != ino) 911da177e4SLinus Torvalds continue; 921da177e4SLinus Torvalds if ((*p)->minor != minor) 931da177e4SLinus Torvalds continue; 941da177e4SLinus Torvalds if ((*p)->major != major) 951da177e4SLinus Torvalds continue; 962139a7fbSH. Peter Anvin if (((*p)->mode ^ mode) & S_IFMT) 972139a7fbSH. Peter Anvin continue; 981da177e4SLinus Torvalds return (*p)->name; 991da177e4SLinus Torvalds } 1003265e66bSThomas Petazzoni q = kmalloc(sizeof(struct hash), GFP_KERNEL); 1011da177e4SLinus Torvalds if (!q) 102dd23e809SFlorian Fainelli panic_show_mem("can't allocate link hash entry"); 1031da177e4SLinus Torvalds q->major = major; 1042139a7fbSH. Peter Anvin q->minor = minor; 1052139a7fbSH. Peter Anvin q->ino = ino; 1062139a7fbSH. Peter Anvin q->mode = mode; 1076a050da4SMark Huang strcpy(q->name, name); 1081da177e4SLinus Torvalds q->next = NULL; 1091da177e4SLinus Torvalds *p = q; 1101da177e4SLinus Torvalds return NULL; 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds 1131da177e4SLinus Torvalds static void __init free_hash(void) 1141da177e4SLinus Torvalds { 1151da177e4SLinus Torvalds struct hash **p, *q; 1161da177e4SLinus Torvalds for (p = head; p < head + 32; p++) { 1171da177e4SLinus Torvalds while (*p) { 1181da177e4SLinus Torvalds q = *p; 1191da177e4SLinus Torvalds *p = q->next; 1203265e66bSThomas Petazzoni kfree(q); 1211da177e4SLinus Torvalds } 1221da177e4SLinus Torvalds } 1231da177e4SLinus Torvalds } 1241da177e4SLinus Torvalds 1251274aea1SDavid Disseldorp #ifdef CONFIG_INITRAMFS_PRESERVE_MTIME 1261274aea1SDavid Disseldorp static void __init do_utime(char *filename, time64_t mtime) 127889d51a1SNye Liu { 1281274aea1SDavid Disseldorp struct timespec64 t[2] = { { .tv_sec = mtime }, { .tv_sec = mtime } }; 1291274aea1SDavid Disseldorp init_utimes(filename, t); 1301274aea1SDavid Disseldorp } 131889d51a1SNye Liu 1321274aea1SDavid Disseldorp static void __init do_utime_path(const struct path *path, time64_t mtime) 1331274aea1SDavid Disseldorp { 1341274aea1SDavid Disseldorp struct timespec64 t[2] = { { .tv_sec = mtime }, { .tv_sec = mtime } }; 1351274aea1SDavid Disseldorp vfs_utimes(path, t); 136889d51a1SNye Liu } 137889d51a1SNye Liu 138889d51a1SNye Liu static __initdata LIST_HEAD(dir_list); 139889d51a1SNye Liu struct dir_entry { 140889d51a1SNye Liu struct list_head list; 141e35c4c64SArnd Bergmann time64_t mtime; 142fcb7aeddSDavid Disseldorp char name[]; 143889d51a1SNye Liu }; 144889d51a1SNye Liu 145e35c4c64SArnd Bergmann static void __init dir_add(const char *name, time64_t mtime) 146889d51a1SNye Liu { 147fcb7aeddSDavid Disseldorp size_t nlen = strlen(name) + 1; 148fcb7aeddSDavid Disseldorp struct dir_entry *de; 149fcb7aeddSDavid Disseldorp 150fcb7aeddSDavid Disseldorp de = kmalloc(sizeof(struct dir_entry) + nlen, GFP_KERNEL); 151889d51a1SNye Liu if (!de) 152dd23e809SFlorian Fainelli panic_show_mem("can't allocate dir_entry buffer"); 153889d51a1SNye Liu INIT_LIST_HEAD(&de->list); 154fcb7aeddSDavid Disseldorp strscpy(de->name, name, nlen); 155889d51a1SNye Liu de->mtime = mtime; 156889d51a1SNye Liu list_add(&de->list, &dir_list); 157889d51a1SNye Liu } 158889d51a1SNye Liu 159889d51a1SNye Liu static void __init dir_utime(void) 160889d51a1SNye Liu { 161889d51a1SNye Liu struct dir_entry *de, *tmp; 162889d51a1SNye Liu list_for_each_entry_safe(de, tmp, &dir_list, list) { 163889d51a1SNye Liu list_del(&de->list); 164889d51a1SNye Liu do_utime(de->name, de->mtime); 165889d51a1SNye Liu kfree(de); 166889d51a1SNye Liu } 167889d51a1SNye Liu } 1681274aea1SDavid Disseldorp #else 1691274aea1SDavid Disseldorp static void __init do_utime(char *filename, time64_t mtime) {} 1701274aea1SDavid Disseldorp static void __init do_utime_path(const struct path *path, time64_t mtime) {} 1711274aea1SDavid Disseldorp static void __init dir_add(const char *name, time64_t mtime) {} 1721274aea1SDavid Disseldorp static void __init dir_utime(void) {} 1731274aea1SDavid Disseldorp #endif 174889d51a1SNye Liu 175e35c4c64SArnd Bergmann static __initdata time64_t mtime; 176889d51a1SNye Liu 1771da177e4SLinus Torvalds /* cpio header parsing */ 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds static __initdata unsigned long ino, major, minor, nlink; 180685dd2d5SAl Viro static __initdata umode_t mode; 1811da177e4SLinus Torvalds static __initdata unsigned long body_len, name_len; 1821da177e4SLinus Torvalds static __initdata uid_t uid; 1831da177e4SLinus Torvalds static __initdata gid_t gid; 1841da177e4SLinus Torvalds static __initdata unsigned rdev; 185800c24dcSDavid Disseldorp static __initdata u32 hdr_csum; 1861da177e4SLinus Torvalds 1871da177e4SLinus Torvalds static void __init parse_header(char *s) 1881da177e4SLinus Torvalds { 189800c24dcSDavid Disseldorp unsigned long parsed[13]; 1901da177e4SLinus Torvalds char buf[9]; 1911da177e4SLinus Torvalds int i; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds buf[8] = '\0'; 194800c24dcSDavid Disseldorp for (i = 0, s += 6; i < 13; i++, s += 8) { 1951da177e4SLinus Torvalds memcpy(buf, s, 8); 1961da177e4SLinus Torvalds parsed[i] = simple_strtoul(buf, NULL, 16); 1971da177e4SLinus Torvalds } 1981da177e4SLinus Torvalds ino = parsed[0]; 1991da177e4SLinus Torvalds mode = parsed[1]; 2001da177e4SLinus Torvalds uid = parsed[2]; 2011da177e4SLinus Torvalds gid = parsed[3]; 2021da177e4SLinus Torvalds nlink = parsed[4]; 203e35c4c64SArnd Bergmann mtime = parsed[5]; /* breaks in y2106 */ 2041da177e4SLinus Torvalds body_len = parsed[6]; 2051da177e4SLinus Torvalds major = parsed[7]; 2061da177e4SLinus Torvalds minor = parsed[8]; 2071da177e4SLinus Torvalds rdev = new_encode_dev(MKDEV(parsed[9], parsed[10])); 2081da177e4SLinus Torvalds name_len = parsed[11]; 209800c24dcSDavid Disseldorp hdr_csum = parsed[12]; 2101da177e4SLinus Torvalds } 2111da177e4SLinus Torvalds 2121da177e4SLinus Torvalds /* FSM */ 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds static __initdata enum state { 2151da177e4SLinus Torvalds Start, 2161da177e4SLinus Torvalds Collect, 2171da177e4SLinus Torvalds GotHeader, 2181da177e4SLinus Torvalds SkipIt, 2191da177e4SLinus Torvalds GotName, 2201da177e4SLinus Torvalds CopyFile, 2211da177e4SLinus Torvalds GotSymlink, 2221da177e4SLinus Torvalds Reset 2231da177e4SLinus Torvalds } state, next_state; 2241da177e4SLinus Torvalds 2251da177e4SLinus Torvalds static __initdata char *victim; 226c34d85acSMark Rustad static unsigned long byte_count __initdata; 2271da177e4SLinus Torvalds static __initdata loff_t this_header, next_header; 2281da177e4SLinus Torvalds 229b0a5ab93SAl Viro static inline void __init eat(unsigned n) 2301da177e4SLinus Torvalds { 2311da177e4SLinus Torvalds victim += n; 2321da177e4SLinus Torvalds this_header += n; 233c34d85acSMark Rustad byte_count -= n; 2341da177e4SLinus Torvalds } 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds static __initdata char *collected; 237d97b07c5SYinghai Lu static long remains __initdata; 2381da177e4SLinus Torvalds static __initdata char *collect; 2391da177e4SLinus Torvalds 2401da177e4SLinus Torvalds static void __init read_into(char *buf, unsigned size, enum state next) 2411da177e4SLinus Torvalds { 242c34d85acSMark Rustad if (byte_count >= size) { 2431da177e4SLinus Torvalds collected = victim; 2441da177e4SLinus Torvalds eat(size); 2451da177e4SLinus Torvalds state = next; 2461da177e4SLinus Torvalds } else { 2471da177e4SLinus Torvalds collect = collected = buf; 2481da177e4SLinus Torvalds remains = size; 2491da177e4SLinus Torvalds next_state = next; 2501da177e4SLinus Torvalds state = Collect; 2511da177e4SLinus Torvalds } 2521da177e4SLinus Torvalds } 2531da177e4SLinus Torvalds 2541da177e4SLinus Torvalds static __initdata char *header_buf, *symlink_buf, *name_buf; 2551da177e4SLinus Torvalds 2561da177e4SLinus Torvalds static int __init do_start(void) 2571da177e4SLinus Torvalds { 2581da177e4SLinus Torvalds read_into(header_buf, 110, GotHeader); 2591da177e4SLinus Torvalds return 0; 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds static int __init do_collect(void) 2631da177e4SLinus Torvalds { 264d97b07c5SYinghai Lu unsigned long n = remains; 265c34d85acSMark Rustad if (byte_count < n) 266c34d85acSMark Rustad n = byte_count; 2671da177e4SLinus Torvalds memcpy(collect, victim, n); 2681da177e4SLinus Torvalds eat(n); 2691da177e4SLinus Torvalds collect += n; 2701da177e4SLinus Torvalds if ((remains -= n) != 0) 2711da177e4SLinus Torvalds return 1; 2721da177e4SLinus Torvalds state = next_state; 2731da177e4SLinus Torvalds return 0; 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds static int __init do_header(void) 2771da177e4SLinus Torvalds { 278800c24dcSDavid Disseldorp if (!memcmp(collected, "070701", 6)) { 279800c24dcSDavid Disseldorp csum_present = false; 280800c24dcSDavid Disseldorp } else if (!memcmp(collected, "070702", 6)) { 281800c24dcSDavid Disseldorp csum_present = true; 282800c24dcSDavid Disseldorp } else { 283da028e4cSDavid Disseldorp if (memcmp(collected, "070707", 6) == 0) 284da028e4cSDavid Disseldorp error("incorrect cpio method used: use -H newc option"); 285da028e4cSDavid Disseldorp else 2861da177e4SLinus Torvalds error("no cpio magic"); 2871da177e4SLinus Torvalds return 1; 2881da177e4SLinus Torvalds } 2891da177e4SLinus Torvalds parse_header(collected); 2901da177e4SLinus Torvalds next_header = this_header + N_ALIGN(name_len) + body_len; 2911da177e4SLinus Torvalds next_header = (next_header + 3) & ~3; 2921da177e4SLinus Torvalds state = SkipIt; 2931da177e4SLinus Torvalds if (name_len <= 0 || name_len > PATH_MAX) 2941da177e4SLinus Torvalds return 0; 2951da177e4SLinus Torvalds if (S_ISLNK(mode)) { 2961da177e4SLinus Torvalds if (body_len > PATH_MAX) 2971da177e4SLinus Torvalds return 0; 2981da177e4SLinus Torvalds collect = collected = symlink_buf; 2991da177e4SLinus Torvalds remains = N_ALIGN(name_len) + body_len; 3001da177e4SLinus Torvalds next_state = GotSymlink; 3011da177e4SLinus Torvalds state = Collect; 3021da177e4SLinus Torvalds return 0; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds if (S_ISREG(mode) || !body_len) 3051da177e4SLinus Torvalds read_into(name_buf, N_ALIGN(name_len), GotName); 3061da177e4SLinus Torvalds return 0; 3071da177e4SLinus Torvalds } 3081da177e4SLinus Torvalds 3091da177e4SLinus Torvalds static int __init do_skip(void) 3101da177e4SLinus Torvalds { 311c34d85acSMark Rustad if (this_header + byte_count < next_header) { 312c34d85acSMark Rustad eat(byte_count); 3131da177e4SLinus Torvalds return 1; 3141da177e4SLinus Torvalds } else { 3151da177e4SLinus Torvalds eat(next_header - this_header); 3161da177e4SLinus Torvalds state = next_state; 3171da177e4SLinus Torvalds return 0; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds } 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds static int __init do_reset(void) 3221da177e4SLinus Torvalds { 323c34d85acSMark Rustad while (byte_count && *victim == '\0') 3241da177e4SLinus Torvalds eat(1); 325c34d85acSMark Rustad if (byte_count && (this_header & 3)) 3261da177e4SLinus Torvalds error("broken padding"); 3271da177e4SLinus Torvalds return 1; 3281da177e4SLinus Torvalds } 3291da177e4SLinus Torvalds 330c34d85acSMark Rustad static void __init clean_path(char *path, umode_t fmode) 3312139a7fbSH. Peter Anvin { 332046aa126SArnd Bergmann struct kstat st; 3332139a7fbSH. Peter Anvin 3347b81ce7cSBarret Rhoden if (!init_stat(path, &st, AT_SYMLINK_NOFOLLOW) && 335716308a5SChristoph Hellwig (st.mode ^ fmode) & S_IFMT) { 336046aa126SArnd Bergmann if (S_ISDIR(st.mode)) 33720cce026SChristoph Hellwig init_rmdir(path); 3382139a7fbSH. Peter Anvin else 3398fb9f73eSChristoph Hellwig init_unlink(path); 3402139a7fbSH. Peter Anvin } 3412139a7fbSH. Peter Anvin } 3422139a7fbSH. Peter Anvin 3437c0950d4SLi Zhijian static int __init maybe_link(void) 3447c0950d4SLi Zhijian { 3457c0950d4SLi Zhijian if (nlink >= 2) { 3467c0950d4SLi Zhijian char *old = find_link(major, minor, ino, mode, collected); 3477c0950d4SLi Zhijian if (old) { 3487c0950d4SLi Zhijian clean_path(collected, 0); 349812931d6SChristoph Hellwig return (init_link(old, collected) < 0) ? -1 : 1; 3507c0950d4SLi Zhijian } 3517c0950d4SLi Zhijian } 3527c0950d4SLi Zhijian return 0; 3537c0950d4SLi Zhijian } 3547c0950d4SLi Zhijian 355bf6419e4SChristoph Hellwig static __initdata struct file *wfile; 356bf6419e4SChristoph Hellwig static __initdata loff_t wfile_pos; 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds static int __init do_name(void) 3591da177e4SLinus Torvalds { 3601da177e4SLinus Torvalds state = SkipIt; 3611da177e4SLinus Torvalds next_state = Reset; 3621da177e4SLinus Torvalds if (strcmp(collected, "TRAILER!!!") == 0) { 3631da177e4SLinus Torvalds free_hash(); 3641da177e4SLinus Torvalds return 0; 3651da177e4SLinus Torvalds } 3662139a7fbSH. Peter Anvin clean_path(collected, mode); 3671da177e4SLinus Torvalds if (S_ISREG(mode)) { 3682139a7fbSH. Peter Anvin int ml = maybe_link(); 3692139a7fbSH. Peter Anvin if (ml >= 0) { 370*8434f9aaSJohn Sperbeck int openflags = O_WRONLY|O_CREAT|O_LARGEFILE; 3712139a7fbSH. Peter Anvin if (ml != 1) 3722139a7fbSH. Peter Anvin openflags |= O_TRUNC; 373bf6419e4SChristoph Hellwig wfile = filp_open(collected, openflags, mode); 374bf6419e4SChristoph Hellwig if (IS_ERR(wfile)) 375bf6419e4SChristoph Hellwig return 0; 376bf6419e4SChristoph Hellwig wfile_pos = 0; 377800c24dcSDavid Disseldorp io_csum = 0; 3782139a7fbSH. Peter Anvin 379bf6419e4SChristoph Hellwig vfs_fchown(wfile, uid, gid); 380bf6419e4SChristoph Hellwig vfs_fchmod(wfile, mode); 381d20d5a74SRandy Robertson if (body_len) 382bf6419e4SChristoph Hellwig vfs_truncate(&wfile->f_path, body_len); 3831da177e4SLinus Torvalds state = CopyFile; 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds } else if (S_ISDIR(mode)) { 38683ff98c3SChristoph Hellwig init_mkdir(collected, mode); 387b873498fSChristoph Hellwig init_chown(collected, uid, gid, 0); 3881097742eSChristoph Hellwig init_chmod(collected, mode); 389889d51a1SNye Liu dir_add(collected, mtime); 3901da177e4SLinus Torvalds } else if (S_ISBLK(mode) || S_ISCHR(mode) || 3911da177e4SLinus Torvalds S_ISFIFO(mode) || S_ISSOCK(mode)) { 3921da177e4SLinus Torvalds if (maybe_link() == 0) { 3935fee64fcSChristoph Hellwig init_mknod(collected, mode, rdev); 394b873498fSChristoph Hellwig init_chown(collected, uid, gid, 0); 3951097742eSChristoph Hellwig init_chmod(collected, mode); 396889d51a1SNye Liu do_utime(collected, mtime); 3971da177e4SLinus Torvalds } 3981da177e4SLinus Torvalds } 3991da177e4SLinus Torvalds return 0; 4001da177e4SLinus Torvalds } 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds static int __init do_copy(void) 4031da177e4SLinus Torvalds { 404c34d85acSMark Rustad if (byte_count >= body_len) { 405bf6419e4SChristoph Hellwig if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len) 4069687fd91SDavid Engraf error("write error"); 40738b08223SChristoph Hellwig 4081274aea1SDavid Disseldorp do_utime_path(&wfile->f_path, mtime); 409bf6419e4SChristoph Hellwig fput(wfile); 410800c24dcSDavid Disseldorp if (csum_present && io_csum != hdr_csum) 411800c24dcSDavid Disseldorp error("bad data checksum"); 4121da177e4SLinus Torvalds eat(body_len); 4131da177e4SLinus Torvalds state = SkipIt; 4141da177e4SLinus Torvalds return 0; 4151da177e4SLinus Torvalds } else { 416bf6419e4SChristoph Hellwig if (xwrite(wfile, victim, byte_count, &wfile_pos) != byte_count) 4179687fd91SDavid Engraf error("write error"); 418c34d85acSMark Rustad body_len -= byte_count; 419c34d85acSMark Rustad eat(byte_count); 4201da177e4SLinus Torvalds return 1; 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds 4241da177e4SLinus Torvalds static int __init do_symlink(void) 4251da177e4SLinus Torvalds { 4261da177e4SLinus Torvalds collected[N_ALIGN(name_len) + body_len] = '\0'; 4272139a7fbSH. Peter Anvin clean_path(collected, 0); 428cd3acb6aSChristoph Hellwig init_symlink(collected + N_ALIGN(name_len), collected); 429b873498fSChristoph Hellwig init_chown(collected, uid, gid, AT_SYMLINK_NOFOLLOW); 430889d51a1SNye Liu do_utime(collected, mtime); 4311da177e4SLinus Torvalds state = SkipIt; 4321da177e4SLinus Torvalds next_state = Reset; 4331da177e4SLinus Torvalds return 0; 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds static __initdata int (*actions[])(void) = { 4371da177e4SLinus Torvalds [Start] = do_start, 4381da177e4SLinus Torvalds [Collect] = do_collect, 4391da177e4SLinus Torvalds [GotHeader] = do_header, 4401da177e4SLinus Torvalds [SkipIt] = do_skip, 4411da177e4SLinus Torvalds [GotName] = do_name, 4421da177e4SLinus Torvalds [CopyFile] = do_copy, 4431da177e4SLinus Torvalds [GotSymlink] = do_symlink, 4441da177e4SLinus Torvalds [Reset] = do_reset, 4451da177e4SLinus Torvalds }; 4461da177e4SLinus Torvalds 447d97b07c5SYinghai Lu static long __init write_buffer(char *buf, unsigned long len) 4481da177e4SLinus Torvalds { 449c34d85acSMark Rustad byte_count = len; 4501da177e4SLinus Torvalds victim = buf; 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds while (!actions[state]()) 4531da177e4SLinus Torvalds ; 454c34d85acSMark Rustad return len - byte_count; 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds 457d97b07c5SYinghai Lu static long __init flush_buffer(void *bufv, unsigned long len) 4581da177e4SLinus Torvalds { 4594197530bSXU pengfei char *buf = bufv; 460d97b07c5SYinghai Lu long written; 461d97b07c5SYinghai Lu long origLen = len; 4621da177e4SLinus Torvalds if (message) 46330d65dbfSAlain Knaff return -1; 4641da177e4SLinus Torvalds while ((written = write_buffer(buf, len)) < len && !message) { 4651da177e4SLinus Torvalds char c = buf[written]; 4661da177e4SLinus Torvalds if (c == '0') { 4671da177e4SLinus Torvalds buf += written; 4681da177e4SLinus Torvalds len -= written; 4691da177e4SLinus Torvalds state = Start; 4701da177e4SLinus Torvalds } else if (c == 0) { 4711da177e4SLinus Torvalds buf += written; 4721da177e4SLinus Torvalds len -= written; 4731da177e4SLinus Torvalds state = Reset; 4741da177e4SLinus Torvalds } else 475e5eed351SDavid Engraf error("junk within compressed archive"); 4761da177e4SLinus Torvalds } 47730d65dbfSAlain Knaff return origLen; 4781da177e4SLinus Torvalds } 4791da177e4SLinus Torvalds 480199cda13Swuchi static unsigned long my_inptr __initdata; /* index of next byte to be processed in inbuf */ 4811da177e4SLinus Torvalds 482889c92d2SH. Peter Anvin #include <linux/decompress/generic.h> 4831da177e4SLinus Torvalds 484d97b07c5SYinghai Lu static char * __init unpack_to_rootfs(char *buf, unsigned long len) 4851da177e4SLinus Torvalds { 486d97b07c5SYinghai Lu long written; 487889c92d2SH. Peter Anvin decompress_fn decompress; 48823a22d57SH. Peter Anvin const char *compress_name; 48923a22d57SH. Peter Anvin static __initdata char msg_buf[64]; 490889c92d2SH. Peter Anvin 4913265e66bSThomas Petazzoni header_buf = kmalloc(110, GFP_KERNEL); 4923265e66bSThomas Petazzoni symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL); 4933265e66bSThomas Petazzoni name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL); 49430d65dbfSAlain Knaff 49530d65dbfSAlain Knaff if (!header_buf || !symlink_buf || !name_buf) 496dd23e809SFlorian Fainelli panic_show_mem("can't allocate buffers"); 49730d65dbfSAlain Knaff 4981da177e4SLinus Torvalds state = Start; 4991da177e4SLinus Torvalds this_header = 0; 5001da177e4SLinus Torvalds message = NULL; 5011da177e4SLinus Torvalds while (!message && len) { 5021da177e4SLinus Torvalds loff_t saved_offset = this_header; 5031da177e4SLinus Torvalds if (*buf == '0' && !(this_header & 3)) { 5041da177e4SLinus Torvalds state = Start; 5051da177e4SLinus Torvalds written = write_buffer(buf, len); 5061da177e4SLinus Torvalds buf += written; 5071da177e4SLinus Torvalds len -= written; 5081da177e4SLinus Torvalds continue; 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds if (!*buf) { 5111da177e4SLinus Torvalds buf++; 5121da177e4SLinus Torvalds len--; 5131da177e4SLinus Torvalds this_header++; 5141da177e4SLinus Torvalds continue; 5151da177e4SLinus Torvalds } 5161da177e4SLinus Torvalds this_header = 0; 51723a22d57SH. Peter Anvin decompress = decompress_method(buf, len, &compress_name); 5186aa7a29aSDaniel M. Weeks pr_debug("Detected %s compressed data\n", compress_name); 51954291362SPhillip Lougher if (decompress) { 520d97b07c5SYinghai Lu int res = decompress(buf, len, NULL, flush_buffer, NULL, 521889c92d2SH. Peter Anvin &my_inptr, error); 52254291362SPhillip Lougher if (res) 52354291362SPhillip Lougher error("decompressor failed"); 52454291362SPhillip Lougher } else if (compress_name) { 52523a22d57SH. Peter Anvin if (!message) { 52623a22d57SH. Peter Anvin snprintf(msg_buf, sizeof msg_buf, 52723a22d57SH. Peter Anvin "compression method %s not configured", 52823a22d57SH. Peter Anvin compress_name); 52923a22d57SH. Peter Anvin message = msg_buf; 53023a22d57SH. Peter Anvin } 531df37bd15SPhillip Lougher } else 532e5eed351SDavid Engraf error("invalid magic at start of compressed archive"); 5331da177e4SLinus Torvalds if (state != Reset) 534e5eed351SDavid Engraf error("junk at the end of compressed archive"); 53530d65dbfSAlain Knaff this_header = saved_offset + my_inptr; 53630d65dbfSAlain Knaff buf += my_inptr; 53730d65dbfSAlain Knaff len -= my_inptr; 5381da177e4SLinus Torvalds } 539889d51a1SNye Liu dir_utime(); 5403265e66bSThomas Petazzoni kfree(name_buf); 5413265e66bSThomas Petazzoni kfree(symlink_buf); 5423265e66bSThomas Petazzoni kfree(header_buf); 5431da177e4SLinus Torvalds return message; 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds 5460a7b35cbSMichael Neuling static int __initdata do_retain_initrd; 5470a7b35cbSMichael Neuling 5480a7b35cbSMichael Neuling static int __init retain_initrd_param(char *str) 5490a7b35cbSMichael Neuling { 5500a7b35cbSMichael Neuling if (*str) 5510a7b35cbSMichael Neuling return 0; 5520a7b35cbSMichael Neuling do_retain_initrd = 1; 5530a7b35cbSMichael Neuling return 1; 5540a7b35cbSMichael Neuling } 5550a7b35cbSMichael Neuling __setup("retain_initrd", retain_initrd_param); 5560a7b35cbSMichael Neuling 557d8ae8a37SChristoph Hellwig #ifdef CONFIG_ARCH_HAS_KEEPINITRD 558d8ae8a37SChristoph Hellwig static int __init keepinitrd_setup(char *__unused) 559d8ae8a37SChristoph Hellwig { 560d8ae8a37SChristoph Hellwig do_retain_initrd = 1; 561d8ae8a37SChristoph Hellwig return 1; 562d8ae8a37SChristoph Hellwig } 563d8ae8a37SChristoph Hellwig __setup("keepinitrd", keepinitrd_setup); 564d8ae8a37SChristoph Hellwig #endif 565d8ae8a37SChristoph Hellwig 566e7cb072eSRasmus Villemoes static bool __initdata initramfs_async = true; 567e7cb072eSRasmus Villemoes static int __init initramfs_async_setup(char *str) 568e7cb072eSRasmus Villemoes { 569f3296f80SChristophe JAILLET return kstrtobool(str, &initramfs_async) == 0; 570e7cb072eSRasmus Villemoes } 571e7cb072eSRasmus Villemoes __setup("initramfs_async=", initramfs_async_setup); 572e7cb072eSRasmus Villemoes 573ffe8018cSHendrik Brueckner extern char __initramfs_start[]; 574ffe8018cSHendrik Brueckner extern unsigned long __initramfs_size; 5751da177e4SLinus Torvalds #include <linux/initrd.h> 5769c15e852SHaren Myneni #include <linux/kexec.h> 5770f3d2bd5SJan Beulich 5782678fd2fSAlexander Graf static ssize_t raw_read(struct file *file, struct kobject *kobj, 5792678fd2fSAlexander Graf struct bin_attribute *attr, char *buf, 5802678fd2fSAlexander Graf loff_t pos, size_t count) 5812678fd2fSAlexander Graf { 5822678fd2fSAlexander Graf memcpy(buf, attr->private + pos, count); 5832678fd2fSAlexander Graf return count; 5842678fd2fSAlexander Graf } 5852678fd2fSAlexander Graf 5862678fd2fSAlexander Graf static BIN_ATTR(initrd, 0440, raw_read, NULL, 0); 5872678fd2fSAlexander Graf 588c72160feSKefeng Wang void __init reserve_initrd_mem(void) 589c72160feSKefeng Wang { 590c72160feSKefeng Wang phys_addr_t start; 591c72160feSKefeng Wang unsigned long size; 592c72160feSKefeng Wang 593c72160feSKefeng Wang /* Ignore the virtul address computed during device tree parsing */ 594c72160feSKefeng Wang initrd_start = initrd_end = 0; 595c72160feSKefeng Wang 596c72160feSKefeng Wang if (!phys_initrd_size) 597c72160feSKefeng Wang return; 598c72160feSKefeng Wang /* 599c72160feSKefeng Wang * Round the memory region to page boundaries as per free_initrd_mem() 600c72160feSKefeng Wang * This allows us to detect whether the pages overlapping the initrd 601c72160feSKefeng Wang * are in use, but more importantly, reserves the entire set of pages 602c72160feSKefeng Wang * as we don't want these pages allocated for other purposes. 603c72160feSKefeng Wang */ 604c72160feSKefeng Wang start = round_down(phys_initrd_start, PAGE_SIZE); 605c72160feSKefeng Wang size = phys_initrd_size + (phys_initrd_start - start); 606c72160feSKefeng Wang size = round_up(size, PAGE_SIZE); 607c72160feSKefeng Wang 608c72160feSKefeng Wang if (!memblock_is_region_memory(start, size)) { 609c72160feSKefeng Wang pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region", 610c72160feSKefeng Wang (u64)start, size); 611c72160feSKefeng Wang goto disable; 612c72160feSKefeng Wang } 613c72160feSKefeng Wang 614c72160feSKefeng Wang if (memblock_is_region_reserved(start, size)) { 615c72160feSKefeng Wang pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region\n", 616c72160feSKefeng Wang (u64)start, size); 617c72160feSKefeng Wang goto disable; 618c72160feSKefeng Wang } 619c72160feSKefeng Wang 620c72160feSKefeng Wang memblock_reserve(start, size); 621c72160feSKefeng Wang /* Now convert initrd to virtual addresses */ 622c72160feSKefeng Wang initrd_start = (unsigned long)__va(phys_initrd_start); 623c72160feSKefeng Wang initrd_end = initrd_start + phys_initrd_size; 624c72160feSKefeng Wang initrd_below_start_ok = 1; 625c72160feSKefeng Wang 626c72160feSKefeng Wang return; 627c72160feSKefeng Wang disable: 628c72160feSKefeng Wang pr_cont(" - disabling initrd\n"); 629c72160feSKefeng Wang initrd_start = 0; 630c72160feSKefeng Wang initrd_end = 0; 631c72160feSKefeng Wang } 632c72160feSKefeng Wang 63355d5b7ddSArnd Bergmann void __weak __init free_initrd_mem(unsigned long start, unsigned long end) 6344afd58e1SChristoph Hellwig { 635899ee4afSMike Rapoport #ifdef CONFIG_ARCH_KEEP_MEMBLOCK 636899ee4afSMike Rapoport unsigned long aligned_start = ALIGN_DOWN(start, PAGE_SIZE); 637899ee4afSMike Rapoport unsigned long aligned_end = ALIGN(end, PAGE_SIZE); 638899ee4afSMike Rapoport 6394421cca0SMike Rapoport memblock_free((void *)aligned_start, aligned_end - aligned_start); 640899ee4afSMike Rapoport #endif 641899ee4afSMike Rapoport 642f94f7434SChristoph Hellwig free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM, 643f94f7434SChristoph Hellwig "initrd"); 6444afd58e1SChristoph Hellwig } 6454afd58e1SChristoph Hellwig 64602aff848SBaoquan He #ifdef CONFIG_CRASH_RESERVE 647e99332e7SLinus Torvalds static bool __init kexec_free_initrd(void) 64823091e28SChristoph Hellwig { 6499c15e852SHaren Myneni unsigned long crashk_start = (unsigned long)__va(crashk_res.start); 6509c15e852SHaren Myneni unsigned long crashk_end = (unsigned long)__va(crashk_res.end); 6519c15e852SHaren Myneni 6529c15e852SHaren Myneni /* 6539c15e852SHaren Myneni * If the initrd region is overlapped with crashkernel reserved region, 6549c15e852SHaren Myneni * free only memory that is not part of crashkernel region. 6559c15e852SHaren Myneni */ 65623091e28SChristoph Hellwig if (initrd_start >= crashk_end || initrd_end <= crashk_start) 65723091e28SChristoph Hellwig return false; 65823091e28SChristoph Hellwig 6599c15e852SHaren Myneni /* 66023091e28SChristoph Hellwig * Initialize initrd memory region since the kexec boot does not do. 6619c15e852SHaren Myneni */ 6629c15e852SHaren Myneni memset((void *)initrd_start, 0, initrd_end - initrd_start); 6639c15e852SHaren Myneni if (initrd_start < crashk_start) 6649c15e852SHaren Myneni free_initrd_mem(initrd_start, crashk_start); 6659c15e852SHaren Myneni if (initrd_end > crashk_end) 6669c15e852SHaren Myneni free_initrd_mem(crashk_end, initrd_end); 66723091e28SChristoph Hellwig return true; 6680f3d2bd5SJan Beulich } 66923091e28SChristoph Hellwig #else 67023091e28SChristoph Hellwig static inline bool kexec_free_initrd(void) 67123091e28SChristoph Hellwig { 67223091e28SChristoph Hellwig return false; 67323091e28SChristoph Hellwig } 67423091e28SChristoph Hellwig #endif /* CONFIG_KEXEC_CORE */ 6750f3d2bd5SJan Beulich 676a841c673SAndrew Morton #ifdef CONFIG_BLK_DEV_RAM 6774ada1e81SGeert Uytterhoeven static void __init populate_initrd_image(char *err) 6787c184ecdSChristoph Hellwig { 6797c184ecdSChristoph Hellwig ssize_t written; 680bf6419e4SChristoph Hellwig struct file *file; 681bf6419e4SChristoph Hellwig loff_t pos = 0; 6827c184ecdSChristoph Hellwig 6837c184ecdSChristoph Hellwig printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n", 6847c184ecdSChristoph Hellwig err); 6854624b346SJohn Sperbeck file = filp_open("/initrd.image", O_WRONLY|O_CREAT|O_LARGEFILE, 0700); 686bf6419e4SChristoph Hellwig if (IS_ERR(file)) 6877c184ecdSChristoph Hellwig return; 6887c184ecdSChristoph Hellwig 689bf6419e4SChristoph Hellwig written = xwrite(file, (char *)initrd_start, initrd_end - initrd_start, 690bf6419e4SChristoph Hellwig &pos); 6917c184ecdSChristoph Hellwig if (written != initrd_end - initrd_start) 6927c184ecdSChristoph Hellwig pr_err("/initrd.image: incomplete write (%zd != %ld)\n", 6937c184ecdSChristoph Hellwig written, initrd_end - initrd_start); 694bf6419e4SChristoph Hellwig fput(file); 6957c184ecdSChristoph Hellwig } 6967c184ecdSChristoph Hellwig #endif /* CONFIG_BLK_DEV_RAM */ 6977c184ecdSChristoph Hellwig 698e7cb072eSRasmus Villemoes static void __init do_populate_rootfs(void *unused, async_cookie_t cookie) 6991da177e4SLinus Torvalds { 70017a9be31SStafford Horne /* Load the built in initramfs */ 701ffe8018cSHendrik Brueckner char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size); 7021da177e4SLinus Torvalds if (err) 703dd23e809SFlorian Fainelli panic_show_mem("%s", err); /* Failed to decompress INTERNAL initramfs */ 704afef7889SChristoph Hellwig 705afef7889SChristoph Hellwig if (!initrd_start || IS_ENABLED(CONFIG_INITRAMFS_FORCE)) 706bb813f4cSTejun Heo goto done; 70754c7a891SChristoph Hellwig 708afef7889SChristoph Hellwig if (IS_ENABLED(CONFIG_BLK_DEV_RAM)) 709afef7889SChristoph Hellwig printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n"); 710afef7889SChristoph Hellwig else 711afef7889SChristoph Hellwig printk(KERN_INFO "Unpacking initramfs...\n"); 712afef7889SChristoph Hellwig 713afef7889SChristoph Hellwig err = unpack_to_rootfs((char *)initrd_start, initrd_end - initrd_start); 714afef7889SChristoph Hellwig if (err) { 7159ab6b718SChristoph Hellwig #ifdef CONFIG_BLK_DEV_RAM 7167c184ecdSChristoph Hellwig populate_initrd_image(err); 7179ab6b718SChristoph Hellwig #else 7189ab6b718SChristoph Hellwig printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err); 7199ab6b718SChristoph Hellwig #endif 72017a9be31SStafford Horne } 72123091e28SChristoph Hellwig 722afef7889SChristoph Hellwig done: 72323091e28SChristoph Hellwig /* 72423091e28SChristoph Hellwig * If the initrd region is overlapped with crashkernel reserved region, 72523091e28SChristoph Hellwig * free only memory that is not part of crashkernel region. 72623091e28SChristoph Hellwig */ 7272678fd2fSAlexander Graf if (!do_retain_initrd && initrd_start && !kexec_free_initrd()) { 72823091e28SChristoph Hellwig free_initrd_mem(initrd_start, initrd_end); 7292678fd2fSAlexander Graf } else if (do_retain_initrd && initrd_start) { 7302678fd2fSAlexander Graf bin_attr_initrd.size = initrd_end - initrd_start; 7312678fd2fSAlexander Graf bin_attr_initrd.private = (void *)initrd_start; 7322678fd2fSAlexander Graf if (sysfs_create_bin_file(firmware_kobj, &bin_attr_initrd)) 7332678fd2fSAlexander Graf pr_err("Failed to create initrd sysfs file"); 7342678fd2fSAlexander Graf } 73523091e28SChristoph Hellwig initrd_start = 0; 73623091e28SChristoph Hellwig initrd_end = 0; 73723091e28SChristoph Hellwig 738386dc41cSChristian Brauner init_flush_fput(); 739e7cb072eSRasmus Villemoes } 740e7cb072eSRasmus Villemoes 741e7cb072eSRasmus Villemoes static ASYNC_DOMAIN_EXCLUSIVE(initramfs_domain); 742e7cb072eSRasmus Villemoes static async_cookie_t initramfs_cookie; 743e7cb072eSRasmus Villemoes 744e7cb072eSRasmus Villemoes void wait_for_initramfs(void) 745e7cb072eSRasmus Villemoes { 746e7cb072eSRasmus Villemoes if (!initramfs_cookie) { 747e7cb072eSRasmus Villemoes /* 748e7cb072eSRasmus Villemoes * Something before rootfs_initcall wants to access 749e7cb072eSRasmus Villemoes * the filesystem/initramfs. Probably a bug. Make a 750e7cb072eSRasmus Villemoes * note, avoid deadlocking the machine, and let the 751e7cb072eSRasmus Villemoes * caller's access fail as it used to. 752e7cb072eSRasmus Villemoes */ 753e7cb072eSRasmus Villemoes pr_warn_once("wait_for_initramfs() called before rootfs_initcalls\n"); 754e7cb072eSRasmus Villemoes return; 755e7cb072eSRasmus Villemoes } 756e7cb072eSRasmus Villemoes async_synchronize_cookie_domain(initramfs_cookie + 1, &initramfs_domain); 757e7cb072eSRasmus Villemoes } 758e7cb072eSRasmus Villemoes EXPORT_SYMBOL_GPL(wait_for_initramfs); 759e7cb072eSRasmus Villemoes 760e7cb072eSRasmus Villemoes static int __init populate_rootfs(void) 761e7cb072eSRasmus Villemoes { 762e7cb072eSRasmus Villemoes initramfs_cookie = async_schedule_domain(do_populate_rootfs, NULL, 763e7cb072eSRasmus Villemoes &initramfs_domain); 764b234ed6dSRasmus Villemoes usermodehelper_enable(); 765e7cb072eSRasmus Villemoes if (!initramfs_async) 766e7cb072eSRasmus Villemoes wait_for_initramfs(); 7678d610dd5SLinus Torvalds return 0; 7681da177e4SLinus Torvalds } 7698d610dd5SLinus Torvalds rootfs_initcall(populate_rootfs); 770