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> 1868d85f0aSEric W. Biederman #include <linux/task_work.h> 19b234ed6dSRasmus Villemoes #include <linux/umh.h> 201da177e4SLinus Torvalds 21800c24dcSDavid Disseldorp static __initdata bool csum_present; 22800c24dcSDavid Disseldorp static __initdata u32 io_csum; 23800c24dcSDavid Disseldorp 24800c24dcSDavid Disseldorp static ssize_t __init xwrite(struct file *file, const unsigned char *p, 25800c24dcSDavid Disseldorp size_t count, loff_t *pos) 2638747439SYinghai Lu { 2738747439SYinghai Lu ssize_t out = 0; 2838747439SYinghai Lu 2938747439SYinghai Lu /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */ 3038747439SYinghai Lu while (count) { 31bf6419e4SChristoph Hellwig ssize_t rv = kernel_write(file, p, count, pos); 3238747439SYinghai Lu 3338747439SYinghai Lu if (rv < 0) { 3438747439SYinghai Lu if (rv == -EINTR || rv == -EAGAIN) 3538747439SYinghai Lu continue; 3638747439SYinghai Lu return out ? out : rv; 3738747439SYinghai Lu } else if (rv == 0) 3838747439SYinghai Lu break; 3938747439SYinghai Lu 40800c24dcSDavid Disseldorp if (csum_present) { 41800c24dcSDavid Disseldorp ssize_t i; 42800c24dcSDavid Disseldorp 43800c24dcSDavid Disseldorp for (i = 0; i < rv; i++) 44800c24dcSDavid Disseldorp io_csum += p[i]; 45800c24dcSDavid Disseldorp } 46800c24dcSDavid Disseldorp 4738747439SYinghai Lu p += rv; 4838747439SYinghai Lu out += rv; 4938747439SYinghai Lu count -= rv; 5038747439SYinghai Lu } 5138747439SYinghai Lu 5238747439SYinghai Lu return out; 5338747439SYinghai Lu } 5438747439SYinghai Lu 551da177e4SLinus Torvalds static __initdata char *message; 561da177e4SLinus Torvalds static void __init error(char *x) 571da177e4SLinus Torvalds { 581da177e4SLinus Torvalds if (!message) 591da177e4SLinus Torvalds message = x; 601da177e4SLinus Torvalds } 611da177e4SLinus Torvalds 62dd23e809SFlorian Fainelli static void panic_show_mem(const char *fmt, ...) 63dd23e809SFlorian Fainelli { 64dd23e809SFlorian Fainelli va_list args; 65dd23e809SFlorian Fainelli 66dd23e809SFlorian Fainelli show_mem(0, NULL); 67dd23e809SFlorian Fainelli va_start(args, fmt); 68dd23e809SFlorian Fainelli panic(fmt, args); 69dd23e809SFlorian Fainelli va_end(args); 70dd23e809SFlorian Fainelli } 71dd23e809SFlorian Fainelli 721da177e4SLinus Torvalds /* link hash */ 731da177e4SLinus Torvalds 746a050da4SMark Huang #define N_ALIGN(len) ((((len) + 1) & ~3) + 2) 756a050da4SMark Huang 761da177e4SLinus Torvalds static __initdata struct hash { 771da177e4SLinus Torvalds int ino, minor, major; 78685dd2d5SAl Viro umode_t mode; 791da177e4SLinus Torvalds struct hash *next; 806a050da4SMark Huang char name[N_ALIGN(PATH_MAX)]; 811da177e4SLinus Torvalds } *head[32]; 821da177e4SLinus Torvalds 831da177e4SLinus Torvalds static inline int hash(int major, int minor, int ino) 841da177e4SLinus Torvalds { 851da177e4SLinus Torvalds unsigned long tmp = ino + minor + (major << 3); 861da177e4SLinus Torvalds tmp += tmp >> 5; 871da177e4SLinus Torvalds return tmp & 31; 881da177e4SLinus Torvalds } 891da177e4SLinus Torvalds 902139a7fbSH. Peter Anvin static char __init *find_link(int major, int minor, int ino, 91685dd2d5SAl Viro umode_t mode, char *name) 921da177e4SLinus Torvalds { 931da177e4SLinus Torvalds struct hash **p, *q; 941da177e4SLinus Torvalds for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) { 951da177e4SLinus Torvalds if ((*p)->ino != ino) 961da177e4SLinus Torvalds continue; 971da177e4SLinus Torvalds if ((*p)->minor != minor) 981da177e4SLinus Torvalds continue; 991da177e4SLinus Torvalds if ((*p)->major != major) 1001da177e4SLinus Torvalds continue; 1012139a7fbSH. Peter Anvin if (((*p)->mode ^ mode) & S_IFMT) 1022139a7fbSH. Peter Anvin continue; 1031da177e4SLinus Torvalds return (*p)->name; 1041da177e4SLinus Torvalds } 1053265e66bSThomas Petazzoni q = kmalloc(sizeof(struct hash), GFP_KERNEL); 1061da177e4SLinus Torvalds if (!q) 107dd23e809SFlorian Fainelli panic_show_mem("can't allocate link hash entry"); 1081da177e4SLinus Torvalds q->major = major; 1092139a7fbSH. Peter Anvin q->minor = minor; 1102139a7fbSH. Peter Anvin q->ino = ino; 1112139a7fbSH. Peter Anvin q->mode = mode; 1126a050da4SMark Huang strcpy(q->name, name); 1131da177e4SLinus Torvalds q->next = NULL; 1141da177e4SLinus Torvalds *p = q; 1151da177e4SLinus Torvalds return NULL; 1161da177e4SLinus Torvalds } 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds static void __init free_hash(void) 1191da177e4SLinus Torvalds { 1201da177e4SLinus Torvalds struct hash **p, *q; 1211da177e4SLinus Torvalds for (p = head; p < head + 32; p++) { 1221da177e4SLinus Torvalds while (*p) { 1231da177e4SLinus Torvalds q = *p; 1241da177e4SLinus Torvalds *p = q->next; 1253265e66bSThomas Petazzoni kfree(q); 1261da177e4SLinus Torvalds } 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 1301274aea1SDavid Disseldorp #ifdef CONFIG_INITRAMFS_PRESERVE_MTIME 1311274aea1SDavid Disseldorp static void __init do_utime(char *filename, time64_t mtime) 132889d51a1SNye Liu { 1331274aea1SDavid Disseldorp struct timespec64 t[2] = { { .tv_sec = mtime }, { .tv_sec = mtime } }; 1341274aea1SDavid Disseldorp init_utimes(filename, t); 1351274aea1SDavid Disseldorp } 136889d51a1SNye Liu 1371274aea1SDavid Disseldorp static void __init do_utime_path(const struct path *path, time64_t mtime) 1381274aea1SDavid Disseldorp { 1391274aea1SDavid Disseldorp struct timespec64 t[2] = { { .tv_sec = mtime }, { .tv_sec = mtime } }; 1401274aea1SDavid Disseldorp vfs_utimes(path, t); 141889d51a1SNye Liu } 142889d51a1SNye Liu 143889d51a1SNye Liu static __initdata LIST_HEAD(dir_list); 144889d51a1SNye Liu struct dir_entry { 145889d51a1SNye Liu struct list_head list; 146e35c4c64SArnd Bergmann time64_t mtime; 147fcb7aeddSDavid Disseldorp char name[]; 148889d51a1SNye Liu }; 149889d51a1SNye Liu 150e35c4c64SArnd Bergmann static void __init dir_add(const char *name, time64_t mtime) 151889d51a1SNye Liu { 152fcb7aeddSDavid Disseldorp size_t nlen = strlen(name) + 1; 153fcb7aeddSDavid Disseldorp struct dir_entry *de; 154fcb7aeddSDavid Disseldorp 155fcb7aeddSDavid Disseldorp de = kmalloc(sizeof(struct dir_entry) + nlen, GFP_KERNEL); 156889d51a1SNye Liu if (!de) 157dd23e809SFlorian Fainelli panic_show_mem("can't allocate dir_entry buffer"); 158889d51a1SNye Liu INIT_LIST_HEAD(&de->list); 159fcb7aeddSDavid Disseldorp strscpy(de->name, name, nlen); 160889d51a1SNye Liu de->mtime = mtime; 161889d51a1SNye Liu list_add(&de->list, &dir_list); 162889d51a1SNye Liu } 163889d51a1SNye Liu 164889d51a1SNye Liu static void __init dir_utime(void) 165889d51a1SNye Liu { 166889d51a1SNye Liu struct dir_entry *de, *tmp; 167889d51a1SNye Liu list_for_each_entry_safe(de, tmp, &dir_list, list) { 168889d51a1SNye Liu list_del(&de->list); 169889d51a1SNye Liu do_utime(de->name, de->mtime); 170889d51a1SNye Liu kfree(de); 171889d51a1SNye Liu } 172889d51a1SNye Liu } 1731274aea1SDavid Disseldorp #else 1741274aea1SDavid Disseldorp static void __init do_utime(char *filename, time64_t mtime) {} 1751274aea1SDavid Disseldorp static void __init do_utime_path(const struct path *path, time64_t mtime) {} 1761274aea1SDavid Disseldorp static void __init dir_add(const char *name, time64_t mtime) {} 1771274aea1SDavid Disseldorp static void __init dir_utime(void) {} 1781274aea1SDavid Disseldorp #endif 179889d51a1SNye Liu 180e35c4c64SArnd Bergmann static __initdata time64_t mtime; 181889d51a1SNye Liu 1821da177e4SLinus Torvalds /* cpio header parsing */ 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds static __initdata unsigned long ino, major, minor, nlink; 185685dd2d5SAl Viro static __initdata umode_t mode; 1861da177e4SLinus Torvalds static __initdata unsigned long body_len, name_len; 1871da177e4SLinus Torvalds static __initdata uid_t uid; 1881da177e4SLinus Torvalds static __initdata gid_t gid; 1891da177e4SLinus Torvalds static __initdata unsigned rdev; 190800c24dcSDavid Disseldorp static __initdata u32 hdr_csum; 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds static void __init parse_header(char *s) 1931da177e4SLinus Torvalds { 194800c24dcSDavid Disseldorp unsigned long parsed[13]; 1951da177e4SLinus Torvalds char buf[9]; 1961da177e4SLinus Torvalds int i; 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds buf[8] = '\0'; 199800c24dcSDavid Disseldorp for (i = 0, s += 6; i < 13; i++, s += 8) { 2001da177e4SLinus Torvalds memcpy(buf, s, 8); 2011da177e4SLinus Torvalds parsed[i] = simple_strtoul(buf, NULL, 16); 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds ino = parsed[0]; 2041da177e4SLinus Torvalds mode = parsed[1]; 2051da177e4SLinus Torvalds uid = parsed[2]; 2061da177e4SLinus Torvalds gid = parsed[3]; 2071da177e4SLinus Torvalds nlink = parsed[4]; 208e35c4c64SArnd Bergmann mtime = parsed[5]; /* breaks in y2106 */ 2091da177e4SLinus Torvalds body_len = parsed[6]; 2101da177e4SLinus Torvalds major = parsed[7]; 2111da177e4SLinus Torvalds minor = parsed[8]; 2121da177e4SLinus Torvalds rdev = new_encode_dev(MKDEV(parsed[9], parsed[10])); 2131da177e4SLinus Torvalds name_len = parsed[11]; 214800c24dcSDavid Disseldorp hdr_csum = parsed[12]; 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds 2171da177e4SLinus Torvalds /* FSM */ 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds static __initdata enum state { 2201da177e4SLinus Torvalds Start, 2211da177e4SLinus Torvalds Collect, 2221da177e4SLinus Torvalds GotHeader, 2231da177e4SLinus Torvalds SkipIt, 2241da177e4SLinus Torvalds GotName, 2251da177e4SLinus Torvalds CopyFile, 2261da177e4SLinus Torvalds GotSymlink, 2271da177e4SLinus Torvalds Reset 2281da177e4SLinus Torvalds } state, next_state; 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds static __initdata char *victim; 231c34d85acSMark Rustad static unsigned long byte_count __initdata; 2321da177e4SLinus Torvalds static __initdata loff_t this_header, next_header; 2331da177e4SLinus Torvalds 234b0a5ab93SAl Viro static inline void __init eat(unsigned n) 2351da177e4SLinus Torvalds { 2361da177e4SLinus Torvalds victim += n; 2371da177e4SLinus Torvalds this_header += n; 238c34d85acSMark Rustad byte_count -= n; 2391da177e4SLinus Torvalds } 2401da177e4SLinus Torvalds 2411da177e4SLinus Torvalds static __initdata char *collected; 242d97b07c5SYinghai Lu static long remains __initdata; 2431da177e4SLinus Torvalds static __initdata char *collect; 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds static void __init read_into(char *buf, unsigned size, enum state next) 2461da177e4SLinus Torvalds { 247c34d85acSMark Rustad if (byte_count >= size) { 2481da177e4SLinus Torvalds collected = victim; 2491da177e4SLinus Torvalds eat(size); 2501da177e4SLinus Torvalds state = next; 2511da177e4SLinus Torvalds } else { 2521da177e4SLinus Torvalds collect = collected = buf; 2531da177e4SLinus Torvalds remains = size; 2541da177e4SLinus Torvalds next_state = next; 2551da177e4SLinus Torvalds state = Collect; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds static __initdata char *header_buf, *symlink_buf, *name_buf; 2601da177e4SLinus Torvalds 2611da177e4SLinus Torvalds static int __init do_start(void) 2621da177e4SLinus Torvalds { 2631da177e4SLinus Torvalds read_into(header_buf, 110, GotHeader); 2641da177e4SLinus Torvalds return 0; 2651da177e4SLinus Torvalds } 2661da177e4SLinus Torvalds 2671da177e4SLinus Torvalds static int __init do_collect(void) 2681da177e4SLinus Torvalds { 269d97b07c5SYinghai Lu unsigned long n = remains; 270c34d85acSMark Rustad if (byte_count < n) 271c34d85acSMark Rustad n = byte_count; 2721da177e4SLinus Torvalds memcpy(collect, victim, n); 2731da177e4SLinus Torvalds eat(n); 2741da177e4SLinus Torvalds collect += n; 2751da177e4SLinus Torvalds if ((remains -= n) != 0) 2761da177e4SLinus Torvalds return 1; 2771da177e4SLinus Torvalds state = next_state; 2781da177e4SLinus Torvalds return 0; 2791da177e4SLinus Torvalds } 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds static int __init do_header(void) 2821da177e4SLinus Torvalds { 283800c24dcSDavid Disseldorp if (!memcmp(collected, "070701", 6)) { 284800c24dcSDavid Disseldorp csum_present = false; 285800c24dcSDavid Disseldorp } else if (!memcmp(collected, "070702", 6)) { 286800c24dcSDavid Disseldorp csum_present = true; 287800c24dcSDavid Disseldorp } else { 288da028e4cSDavid Disseldorp if (memcmp(collected, "070707", 6) == 0) 289da028e4cSDavid Disseldorp error("incorrect cpio method used: use -H newc option"); 290da028e4cSDavid Disseldorp else 2911da177e4SLinus Torvalds error("no cpio magic"); 2921da177e4SLinus Torvalds return 1; 2931da177e4SLinus Torvalds } 2941da177e4SLinus Torvalds parse_header(collected); 2951da177e4SLinus Torvalds next_header = this_header + N_ALIGN(name_len) + body_len; 2961da177e4SLinus Torvalds next_header = (next_header + 3) & ~3; 2971da177e4SLinus Torvalds state = SkipIt; 2981da177e4SLinus Torvalds if (name_len <= 0 || name_len > PATH_MAX) 2991da177e4SLinus Torvalds return 0; 3001da177e4SLinus Torvalds if (S_ISLNK(mode)) { 3011da177e4SLinus Torvalds if (body_len > PATH_MAX) 3021da177e4SLinus Torvalds return 0; 3031da177e4SLinus Torvalds collect = collected = symlink_buf; 3041da177e4SLinus Torvalds remains = N_ALIGN(name_len) + body_len; 3051da177e4SLinus Torvalds next_state = GotSymlink; 3061da177e4SLinus Torvalds state = Collect; 3071da177e4SLinus Torvalds return 0; 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds if (S_ISREG(mode) || !body_len) 3101da177e4SLinus Torvalds read_into(name_buf, N_ALIGN(name_len), GotName); 3111da177e4SLinus Torvalds return 0; 3121da177e4SLinus Torvalds } 3131da177e4SLinus Torvalds 3141da177e4SLinus Torvalds static int __init do_skip(void) 3151da177e4SLinus Torvalds { 316c34d85acSMark Rustad if (this_header + byte_count < next_header) { 317c34d85acSMark Rustad eat(byte_count); 3181da177e4SLinus Torvalds return 1; 3191da177e4SLinus Torvalds } else { 3201da177e4SLinus Torvalds eat(next_header - this_header); 3211da177e4SLinus Torvalds state = next_state; 3221da177e4SLinus Torvalds return 0; 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds 3261da177e4SLinus Torvalds static int __init do_reset(void) 3271da177e4SLinus Torvalds { 328c34d85acSMark Rustad while (byte_count && *victim == '\0') 3291da177e4SLinus Torvalds eat(1); 330c34d85acSMark Rustad if (byte_count && (this_header & 3)) 3311da177e4SLinus Torvalds error("broken padding"); 3321da177e4SLinus Torvalds return 1; 3331da177e4SLinus Torvalds } 3341da177e4SLinus Torvalds 335c34d85acSMark Rustad static void __init clean_path(char *path, umode_t fmode) 3362139a7fbSH. Peter Anvin { 337046aa126SArnd Bergmann struct kstat st; 3382139a7fbSH. Peter Anvin 3397b81ce7cSBarret Rhoden if (!init_stat(path, &st, AT_SYMLINK_NOFOLLOW) && 340716308a5SChristoph Hellwig (st.mode ^ fmode) & S_IFMT) { 341046aa126SArnd Bergmann if (S_ISDIR(st.mode)) 34220cce026SChristoph Hellwig init_rmdir(path); 3432139a7fbSH. Peter Anvin else 3448fb9f73eSChristoph Hellwig init_unlink(path); 3452139a7fbSH. Peter Anvin } 3462139a7fbSH. Peter Anvin } 3472139a7fbSH. Peter Anvin 3487c0950d4SLi Zhijian static int __init maybe_link(void) 3497c0950d4SLi Zhijian { 3507c0950d4SLi Zhijian if (nlink >= 2) { 3517c0950d4SLi Zhijian char *old = find_link(major, minor, ino, mode, collected); 3527c0950d4SLi Zhijian if (old) { 3537c0950d4SLi Zhijian clean_path(collected, 0); 354812931d6SChristoph Hellwig return (init_link(old, collected) < 0) ? -1 : 1; 3557c0950d4SLi Zhijian } 3567c0950d4SLi Zhijian } 3577c0950d4SLi Zhijian return 0; 3587c0950d4SLi Zhijian } 3597c0950d4SLi Zhijian 360bf6419e4SChristoph Hellwig static __initdata struct file *wfile; 361bf6419e4SChristoph Hellwig static __initdata loff_t wfile_pos; 3621da177e4SLinus Torvalds 3631da177e4SLinus Torvalds static int __init do_name(void) 3641da177e4SLinus Torvalds { 3651da177e4SLinus Torvalds state = SkipIt; 3661da177e4SLinus Torvalds next_state = Reset; 3671da177e4SLinus Torvalds if (strcmp(collected, "TRAILER!!!") == 0) { 3681da177e4SLinus Torvalds free_hash(); 3691da177e4SLinus Torvalds return 0; 3701da177e4SLinus Torvalds } 3712139a7fbSH. Peter Anvin clean_path(collected, mode); 3721da177e4SLinus Torvalds if (S_ISREG(mode)) { 3732139a7fbSH. Peter Anvin int ml = maybe_link(); 3742139a7fbSH. Peter Anvin if (ml >= 0) { 3752139a7fbSH. Peter Anvin int openflags = O_WRONLY|O_CREAT; 3762139a7fbSH. Peter Anvin if (ml != 1) 3772139a7fbSH. Peter Anvin openflags |= O_TRUNC; 378bf6419e4SChristoph Hellwig wfile = filp_open(collected, openflags, mode); 379bf6419e4SChristoph Hellwig if (IS_ERR(wfile)) 380bf6419e4SChristoph Hellwig return 0; 381bf6419e4SChristoph Hellwig wfile_pos = 0; 382800c24dcSDavid Disseldorp io_csum = 0; 3832139a7fbSH. Peter Anvin 384bf6419e4SChristoph Hellwig vfs_fchown(wfile, uid, gid); 385bf6419e4SChristoph Hellwig vfs_fchmod(wfile, mode); 386d20d5a74SRandy Robertson if (body_len) 387bf6419e4SChristoph Hellwig vfs_truncate(&wfile->f_path, body_len); 3881da177e4SLinus Torvalds state = CopyFile; 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds } else if (S_ISDIR(mode)) { 39183ff98c3SChristoph Hellwig init_mkdir(collected, mode); 392b873498fSChristoph Hellwig init_chown(collected, uid, gid, 0); 3931097742eSChristoph Hellwig init_chmod(collected, mode); 394889d51a1SNye Liu dir_add(collected, mtime); 3951da177e4SLinus Torvalds } else if (S_ISBLK(mode) || S_ISCHR(mode) || 3961da177e4SLinus Torvalds S_ISFIFO(mode) || S_ISSOCK(mode)) { 3971da177e4SLinus Torvalds if (maybe_link() == 0) { 3985fee64fcSChristoph Hellwig init_mknod(collected, mode, rdev); 399b873498fSChristoph Hellwig init_chown(collected, uid, gid, 0); 4001097742eSChristoph Hellwig init_chmod(collected, mode); 401889d51a1SNye Liu do_utime(collected, mtime); 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds return 0; 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds 4071da177e4SLinus Torvalds static int __init do_copy(void) 4081da177e4SLinus Torvalds { 409c34d85acSMark Rustad if (byte_count >= body_len) { 410bf6419e4SChristoph Hellwig if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len) 4119687fd91SDavid Engraf error("write error"); 41238b08223SChristoph Hellwig 4131274aea1SDavid Disseldorp do_utime_path(&wfile->f_path, mtime); 414bf6419e4SChristoph Hellwig fput(wfile); 415800c24dcSDavid Disseldorp if (csum_present && io_csum != hdr_csum) 416800c24dcSDavid Disseldorp error("bad data checksum"); 4171da177e4SLinus Torvalds eat(body_len); 4181da177e4SLinus Torvalds state = SkipIt; 4191da177e4SLinus Torvalds return 0; 4201da177e4SLinus Torvalds } else { 421bf6419e4SChristoph Hellwig if (xwrite(wfile, victim, byte_count, &wfile_pos) != byte_count) 4229687fd91SDavid Engraf error("write error"); 423c34d85acSMark Rustad body_len -= byte_count; 424c34d85acSMark Rustad eat(byte_count); 4251da177e4SLinus Torvalds return 1; 4261da177e4SLinus Torvalds } 4271da177e4SLinus Torvalds } 4281da177e4SLinus Torvalds 4291da177e4SLinus Torvalds static int __init do_symlink(void) 4301da177e4SLinus Torvalds { 4311da177e4SLinus Torvalds collected[N_ALIGN(name_len) + body_len] = '\0'; 4322139a7fbSH. Peter Anvin clean_path(collected, 0); 433cd3acb6aSChristoph Hellwig init_symlink(collected + N_ALIGN(name_len), collected); 434b873498fSChristoph Hellwig init_chown(collected, uid, gid, AT_SYMLINK_NOFOLLOW); 435889d51a1SNye Liu do_utime(collected, mtime); 4361da177e4SLinus Torvalds state = SkipIt; 4371da177e4SLinus Torvalds next_state = Reset; 4381da177e4SLinus Torvalds return 0; 4391da177e4SLinus Torvalds } 4401da177e4SLinus Torvalds 4411da177e4SLinus Torvalds static __initdata int (*actions[])(void) = { 4421da177e4SLinus Torvalds [Start] = do_start, 4431da177e4SLinus Torvalds [Collect] = do_collect, 4441da177e4SLinus Torvalds [GotHeader] = do_header, 4451da177e4SLinus Torvalds [SkipIt] = do_skip, 4461da177e4SLinus Torvalds [GotName] = do_name, 4471da177e4SLinus Torvalds [CopyFile] = do_copy, 4481da177e4SLinus Torvalds [GotSymlink] = do_symlink, 4491da177e4SLinus Torvalds [Reset] = do_reset, 4501da177e4SLinus Torvalds }; 4511da177e4SLinus Torvalds 452d97b07c5SYinghai Lu static long __init write_buffer(char *buf, unsigned long len) 4531da177e4SLinus Torvalds { 454c34d85acSMark Rustad byte_count = len; 4551da177e4SLinus Torvalds victim = buf; 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds while (!actions[state]()) 4581da177e4SLinus Torvalds ; 459c34d85acSMark Rustad return len - byte_count; 4601da177e4SLinus Torvalds } 4611da177e4SLinus Torvalds 462d97b07c5SYinghai Lu static long __init flush_buffer(void *bufv, unsigned long len) 4631da177e4SLinus Torvalds { 464*4197530bSXU pengfei char *buf = bufv; 465d97b07c5SYinghai Lu long written; 466d97b07c5SYinghai Lu long origLen = len; 4671da177e4SLinus Torvalds if (message) 46830d65dbfSAlain Knaff return -1; 4691da177e4SLinus Torvalds while ((written = write_buffer(buf, len)) < len && !message) { 4701da177e4SLinus Torvalds char c = buf[written]; 4711da177e4SLinus Torvalds if (c == '0') { 4721da177e4SLinus Torvalds buf += written; 4731da177e4SLinus Torvalds len -= written; 4741da177e4SLinus Torvalds state = Start; 4751da177e4SLinus Torvalds } else if (c == 0) { 4761da177e4SLinus Torvalds buf += written; 4771da177e4SLinus Torvalds len -= written; 4781da177e4SLinus Torvalds state = Reset; 4791da177e4SLinus Torvalds } else 480e5eed351SDavid Engraf error("junk within compressed archive"); 4811da177e4SLinus Torvalds } 48230d65dbfSAlain Knaff return origLen; 4831da177e4SLinus Torvalds } 4841da177e4SLinus Torvalds 485199cda13Swuchi static unsigned long my_inptr __initdata; /* index of next byte to be processed in inbuf */ 4861da177e4SLinus Torvalds 487889c92d2SH. Peter Anvin #include <linux/decompress/generic.h> 4881da177e4SLinus Torvalds 489d97b07c5SYinghai Lu static char * __init unpack_to_rootfs(char *buf, unsigned long len) 4901da177e4SLinus Torvalds { 491d97b07c5SYinghai Lu long written; 492889c92d2SH. Peter Anvin decompress_fn decompress; 49323a22d57SH. Peter Anvin const char *compress_name; 49423a22d57SH. Peter Anvin static __initdata char msg_buf[64]; 495889c92d2SH. Peter Anvin 4963265e66bSThomas Petazzoni header_buf = kmalloc(110, GFP_KERNEL); 4973265e66bSThomas Petazzoni symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL); 4983265e66bSThomas Petazzoni name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL); 49930d65dbfSAlain Knaff 50030d65dbfSAlain Knaff if (!header_buf || !symlink_buf || !name_buf) 501dd23e809SFlorian Fainelli panic_show_mem("can't allocate buffers"); 50230d65dbfSAlain Knaff 5031da177e4SLinus Torvalds state = Start; 5041da177e4SLinus Torvalds this_header = 0; 5051da177e4SLinus Torvalds message = NULL; 5061da177e4SLinus Torvalds while (!message && len) { 5071da177e4SLinus Torvalds loff_t saved_offset = this_header; 5081da177e4SLinus Torvalds if (*buf == '0' && !(this_header & 3)) { 5091da177e4SLinus Torvalds state = Start; 5101da177e4SLinus Torvalds written = write_buffer(buf, len); 5111da177e4SLinus Torvalds buf += written; 5121da177e4SLinus Torvalds len -= written; 5131da177e4SLinus Torvalds continue; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds if (!*buf) { 5161da177e4SLinus Torvalds buf++; 5171da177e4SLinus Torvalds len--; 5181da177e4SLinus Torvalds this_header++; 5191da177e4SLinus Torvalds continue; 5201da177e4SLinus Torvalds } 5211da177e4SLinus Torvalds this_header = 0; 52223a22d57SH. Peter Anvin decompress = decompress_method(buf, len, &compress_name); 5236aa7a29aSDaniel M. Weeks pr_debug("Detected %s compressed data\n", compress_name); 52454291362SPhillip Lougher if (decompress) { 525d97b07c5SYinghai Lu int res = decompress(buf, len, NULL, flush_buffer, NULL, 526889c92d2SH. Peter Anvin &my_inptr, error); 52754291362SPhillip Lougher if (res) 52854291362SPhillip Lougher error("decompressor failed"); 52954291362SPhillip Lougher } else if (compress_name) { 53023a22d57SH. Peter Anvin if (!message) { 53123a22d57SH. Peter Anvin snprintf(msg_buf, sizeof msg_buf, 53223a22d57SH. Peter Anvin "compression method %s not configured", 53323a22d57SH. Peter Anvin compress_name); 53423a22d57SH. Peter Anvin message = msg_buf; 53523a22d57SH. Peter Anvin } 536df37bd15SPhillip Lougher } else 537e5eed351SDavid Engraf error("invalid magic at start of compressed archive"); 5381da177e4SLinus Torvalds if (state != Reset) 539e5eed351SDavid Engraf error("junk at the end of compressed archive"); 54030d65dbfSAlain Knaff this_header = saved_offset + my_inptr; 54130d65dbfSAlain Knaff buf += my_inptr; 54230d65dbfSAlain Knaff len -= my_inptr; 5431da177e4SLinus Torvalds } 544889d51a1SNye Liu dir_utime(); 5453265e66bSThomas Petazzoni kfree(name_buf); 5463265e66bSThomas Petazzoni kfree(symlink_buf); 5473265e66bSThomas Petazzoni kfree(header_buf); 5481da177e4SLinus Torvalds return message; 5491da177e4SLinus Torvalds } 5501da177e4SLinus Torvalds 5510a7b35cbSMichael Neuling static int __initdata do_retain_initrd; 5520a7b35cbSMichael Neuling 5530a7b35cbSMichael Neuling static int __init retain_initrd_param(char *str) 5540a7b35cbSMichael Neuling { 5550a7b35cbSMichael Neuling if (*str) 5560a7b35cbSMichael Neuling return 0; 5570a7b35cbSMichael Neuling do_retain_initrd = 1; 5580a7b35cbSMichael Neuling return 1; 5590a7b35cbSMichael Neuling } 5600a7b35cbSMichael Neuling __setup("retain_initrd", retain_initrd_param); 5610a7b35cbSMichael Neuling 562d8ae8a37SChristoph Hellwig #ifdef CONFIG_ARCH_HAS_KEEPINITRD 563d8ae8a37SChristoph Hellwig static int __init keepinitrd_setup(char *__unused) 564d8ae8a37SChristoph Hellwig { 565d8ae8a37SChristoph Hellwig do_retain_initrd = 1; 566d8ae8a37SChristoph Hellwig return 1; 567d8ae8a37SChristoph Hellwig } 568d8ae8a37SChristoph Hellwig __setup("keepinitrd", keepinitrd_setup); 569d8ae8a37SChristoph Hellwig #endif 570d8ae8a37SChristoph Hellwig 571e7cb072eSRasmus Villemoes static bool __initdata initramfs_async = true; 572e7cb072eSRasmus Villemoes static int __init initramfs_async_setup(char *str) 573e7cb072eSRasmus Villemoes { 574e7cb072eSRasmus Villemoes strtobool(str, &initramfs_async); 575e7cb072eSRasmus Villemoes return 1; 576e7cb072eSRasmus Villemoes } 577e7cb072eSRasmus Villemoes __setup("initramfs_async=", initramfs_async_setup); 578e7cb072eSRasmus Villemoes 579ffe8018cSHendrik Brueckner extern char __initramfs_start[]; 580ffe8018cSHendrik Brueckner extern unsigned long __initramfs_size; 5811da177e4SLinus Torvalds #include <linux/initrd.h> 5829c15e852SHaren Myneni #include <linux/kexec.h> 5830f3d2bd5SJan Beulich 584c72160feSKefeng Wang void __init reserve_initrd_mem(void) 585c72160feSKefeng Wang { 586c72160feSKefeng Wang phys_addr_t start; 587c72160feSKefeng Wang unsigned long size; 588c72160feSKefeng Wang 589c72160feSKefeng Wang /* Ignore the virtul address computed during device tree parsing */ 590c72160feSKefeng Wang initrd_start = initrd_end = 0; 591c72160feSKefeng Wang 592c72160feSKefeng Wang if (!phys_initrd_size) 593c72160feSKefeng Wang return; 594c72160feSKefeng Wang /* 595c72160feSKefeng Wang * Round the memory region to page boundaries as per free_initrd_mem() 596c72160feSKefeng Wang * This allows us to detect whether the pages overlapping the initrd 597c72160feSKefeng Wang * are in use, but more importantly, reserves the entire set of pages 598c72160feSKefeng Wang * as we don't want these pages allocated for other purposes. 599c72160feSKefeng Wang */ 600c72160feSKefeng Wang start = round_down(phys_initrd_start, PAGE_SIZE); 601c72160feSKefeng Wang size = phys_initrd_size + (phys_initrd_start - start); 602c72160feSKefeng Wang size = round_up(size, PAGE_SIZE); 603c72160feSKefeng Wang 604c72160feSKefeng Wang if (!memblock_is_region_memory(start, size)) { 605c72160feSKefeng Wang pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region", 606c72160feSKefeng Wang (u64)start, size); 607c72160feSKefeng Wang goto disable; 608c72160feSKefeng Wang } 609c72160feSKefeng Wang 610c72160feSKefeng Wang if (memblock_is_region_reserved(start, size)) { 611c72160feSKefeng Wang pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region\n", 612c72160feSKefeng Wang (u64)start, size); 613c72160feSKefeng Wang goto disable; 614c72160feSKefeng Wang } 615c72160feSKefeng Wang 616c72160feSKefeng Wang memblock_reserve(start, size); 617c72160feSKefeng Wang /* Now convert initrd to virtual addresses */ 618c72160feSKefeng Wang initrd_start = (unsigned long)__va(phys_initrd_start); 619c72160feSKefeng Wang initrd_end = initrd_start + phys_initrd_size; 620c72160feSKefeng Wang initrd_below_start_ok = 1; 621c72160feSKefeng Wang 622c72160feSKefeng Wang return; 623c72160feSKefeng Wang disable: 624c72160feSKefeng Wang pr_cont(" - disabling initrd\n"); 625c72160feSKefeng Wang initrd_start = 0; 626c72160feSKefeng Wang initrd_end = 0; 627c72160feSKefeng Wang } 628c72160feSKefeng Wang 62955d5b7ddSArnd Bergmann void __weak __init free_initrd_mem(unsigned long start, unsigned long end) 6304afd58e1SChristoph Hellwig { 631899ee4afSMike Rapoport #ifdef CONFIG_ARCH_KEEP_MEMBLOCK 632899ee4afSMike Rapoport unsigned long aligned_start = ALIGN_DOWN(start, PAGE_SIZE); 633899ee4afSMike Rapoport unsigned long aligned_end = ALIGN(end, PAGE_SIZE); 634899ee4afSMike Rapoport 6354421cca0SMike Rapoport memblock_free((void *)aligned_start, aligned_end - aligned_start); 636899ee4afSMike Rapoport #endif 637899ee4afSMike Rapoport 638f94f7434SChristoph Hellwig free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM, 639f94f7434SChristoph Hellwig "initrd"); 6404afd58e1SChristoph Hellwig } 6414afd58e1SChristoph Hellwig 6422965faa5SDave Young #ifdef CONFIG_KEXEC_CORE 643e99332e7SLinus Torvalds static bool __init kexec_free_initrd(void) 64423091e28SChristoph Hellwig { 6459c15e852SHaren Myneni unsigned long crashk_start = (unsigned long)__va(crashk_res.start); 6469c15e852SHaren Myneni unsigned long crashk_end = (unsigned long)__va(crashk_res.end); 6479c15e852SHaren Myneni 6489c15e852SHaren Myneni /* 6499c15e852SHaren Myneni * If the initrd region is overlapped with crashkernel reserved region, 6509c15e852SHaren Myneni * free only memory that is not part of crashkernel region. 6519c15e852SHaren Myneni */ 65223091e28SChristoph Hellwig if (initrd_start >= crashk_end || initrd_end <= crashk_start) 65323091e28SChristoph Hellwig return false; 65423091e28SChristoph Hellwig 6559c15e852SHaren Myneni /* 65623091e28SChristoph Hellwig * Initialize initrd memory region since the kexec boot does not do. 6579c15e852SHaren Myneni */ 6589c15e852SHaren Myneni memset((void *)initrd_start, 0, initrd_end - initrd_start); 6599c15e852SHaren Myneni if (initrd_start < crashk_start) 6609c15e852SHaren Myneni free_initrd_mem(initrd_start, crashk_start); 6619c15e852SHaren Myneni if (initrd_end > crashk_end) 6629c15e852SHaren Myneni free_initrd_mem(crashk_end, initrd_end); 66323091e28SChristoph Hellwig return true; 6640f3d2bd5SJan Beulich } 66523091e28SChristoph Hellwig #else 66623091e28SChristoph Hellwig static inline bool kexec_free_initrd(void) 66723091e28SChristoph Hellwig { 66823091e28SChristoph Hellwig return false; 66923091e28SChristoph Hellwig } 67023091e28SChristoph Hellwig #endif /* CONFIG_KEXEC_CORE */ 6710f3d2bd5SJan Beulich 672a841c673SAndrew Morton #ifdef CONFIG_BLK_DEV_RAM 6734ada1e81SGeert Uytterhoeven static void __init populate_initrd_image(char *err) 6747c184ecdSChristoph Hellwig { 6757c184ecdSChristoph Hellwig ssize_t written; 676bf6419e4SChristoph Hellwig struct file *file; 677bf6419e4SChristoph Hellwig loff_t pos = 0; 6787c184ecdSChristoph Hellwig 6797c184ecdSChristoph Hellwig unpack_to_rootfs(__initramfs_start, __initramfs_size); 6807c184ecdSChristoph Hellwig 6817c184ecdSChristoph Hellwig printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n", 6827c184ecdSChristoph Hellwig err); 683bf6419e4SChristoph Hellwig file = filp_open("/initrd.image", O_WRONLY | O_CREAT, 0700); 684bf6419e4SChristoph Hellwig if (IS_ERR(file)) 6857c184ecdSChristoph Hellwig return; 6867c184ecdSChristoph Hellwig 687bf6419e4SChristoph Hellwig written = xwrite(file, (char *)initrd_start, initrd_end - initrd_start, 688bf6419e4SChristoph Hellwig &pos); 6897c184ecdSChristoph Hellwig if (written != initrd_end - initrd_start) 6907c184ecdSChristoph Hellwig pr_err("/initrd.image: incomplete write (%zd != %ld)\n", 6917c184ecdSChristoph Hellwig written, initrd_end - initrd_start); 692bf6419e4SChristoph Hellwig fput(file); 6937c184ecdSChristoph Hellwig } 6947c184ecdSChristoph Hellwig #endif /* CONFIG_BLK_DEV_RAM */ 6957c184ecdSChristoph Hellwig 696e7cb072eSRasmus Villemoes static void __init do_populate_rootfs(void *unused, async_cookie_t cookie) 6971da177e4SLinus Torvalds { 69817a9be31SStafford Horne /* Load the built in initramfs */ 699ffe8018cSHendrik Brueckner char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size); 7001da177e4SLinus Torvalds if (err) 701dd23e809SFlorian Fainelli panic_show_mem("%s", err); /* Failed to decompress INTERNAL initramfs */ 702afef7889SChristoph Hellwig 703afef7889SChristoph Hellwig if (!initrd_start || IS_ENABLED(CONFIG_INITRAMFS_FORCE)) 704bb813f4cSTejun Heo goto done; 70554c7a891SChristoph Hellwig 706afef7889SChristoph Hellwig if (IS_ENABLED(CONFIG_BLK_DEV_RAM)) 707afef7889SChristoph Hellwig printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n"); 708afef7889SChristoph Hellwig else 709afef7889SChristoph Hellwig printk(KERN_INFO "Unpacking initramfs...\n"); 710afef7889SChristoph Hellwig 711afef7889SChristoph Hellwig err = unpack_to_rootfs((char *)initrd_start, initrd_end - initrd_start); 712afef7889SChristoph Hellwig if (err) { 7139ab6b718SChristoph Hellwig #ifdef CONFIG_BLK_DEV_RAM 7147c184ecdSChristoph Hellwig populate_initrd_image(err); 7159ab6b718SChristoph Hellwig #else 7169ab6b718SChristoph Hellwig printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err); 7179ab6b718SChristoph Hellwig #endif 71817a9be31SStafford Horne } 71923091e28SChristoph Hellwig 720afef7889SChristoph Hellwig done: 72123091e28SChristoph Hellwig /* 72223091e28SChristoph Hellwig * If the initrd region is overlapped with crashkernel reserved region, 72323091e28SChristoph Hellwig * free only memory that is not part of crashkernel region. 72423091e28SChristoph Hellwig */ 7255d59aa8fSSteven Price if (!do_retain_initrd && initrd_start && !kexec_free_initrd()) 72623091e28SChristoph Hellwig free_initrd_mem(initrd_start, initrd_end); 72723091e28SChristoph Hellwig initrd_start = 0; 72823091e28SChristoph Hellwig initrd_end = 0; 72923091e28SChristoph Hellwig 73008865514SLokesh Vutla flush_delayed_fput(); 73168d85f0aSEric W. Biederman task_work_run(); 732e7cb072eSRasmus Villemoes } 733e7cb072eSRasmus Villemoes 734e7cb072eSRasmus Villemoes static ASYNC_DOMAIN_EXCLUSIVE(initramfs_domain); 735e7cb072eSRasmus Villemoes static async_cookie_t initramfs_cookie; 736e7cb072eSRasmus Villemoes 737e7cb072eSRasmus Villemoes void wait_for_initramfs(void) 738e7cb072eSRasmus Villemoes { 739e7cb072eSRasmus Villemoes if (!initramfs_cookie) { 740e7cb072eSRasmus Villemoes /* 741e7cb072eSRasmus Villemoes * Something before rootfs_initcall wants to access 742e7cb072eSRasmus Villemoes * the filesystem/initramfs. Probably a bug. Make a 743e7cb072eSRasmus Villemoes * note, avoid deadlocking the machine, and let the 744e7cb072eSRasmus Villemoes * caller's access fail as it used to. 745e7cb072eSRasmus Villemoes */ 746e7cb072eSRasmus Villemoes pr_warn_once("wait_for_initramfs() called before rootfs_initcalls\n"); 747e7cb072eSRasmus Villemoes return; 748e7cb072eSRasmus Villemoes } 749e7cb072eSRasmus Villemoes async_synchronize_cookie_domain(initramfs_cookie + 1, &initramfs_domain); 750e7cb072eSRasmus Villemoes } 751e7cb072eSRasmus Villemoes EXPORT_SYMBOL_GPL(wait_for_initramfs); 752e7cb072eSRasmus Villemoes 753e7cb072eSRasmus Villemoes static int __init populate_rootfs(void) 754e7cb072eSRasmus Villemoes { 755e7cb072eSRasmus Villemoes initramfs_cookie = async_schedule_domain(do_populate_rootfs, NULL, 756e7cb072eSRasmus Villemoes &initramfs_domain); 757b234ed6dSRasmus Villemoes usermodehelper_enable(); 758e7cb072eSRasmus Villemoes if (!initramfs_async) 759e7cb072eSRasmus Villemoes wait_for_initramfs(); 7608d610dd5SLinus Torvalds return 0; 7611da177e4SLinus Torvalds } 7628d610dd5SLinus Torvalds rootfs_initcall(populate_rootfs); 763