1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0 21da177e4SLinus Torvalds #include <linux/init.h> 31da177e4SLinus Torvalds #include <linux/fs.h> 41da177e4SLinus Torvalds #include <linux/slab.h> 51da177e4SLinus Torvalds #include <linux/types.h> 61da177e4SLinus Torvalds #include <linux/fcntl.h> 71da177e4SLinus Torvalds #include <linux/delay.h> 81da177e4SLinus Torvalds #include <linux/string.h> 9df52092fSLi, Shaohua #include <linux/dirent.h> 101da177e4SLinus Torvalds #include <linux/syscalls.h> 11889d51a1SNye Liu #include <linux/utime.h> 1208865514SLokesh Vutla #include <linux/file.h> 13899ee4afSMike Rapoport #include <linux/memblock.h> 14b2a74d5fSChristoph Hellwig #include <linux/namei.h> 158fb9f73eSChristoph Hellwig #include <linux/init_syscalls.h> 161da177e4SLinus Torvalds 17bf6419e4SChristoph Hellwig static ssize_t __init xwrite(struct file *file, const char *p, size_t count, 18bf6419e4SChristoph Hellwig loff_t *pos) 1938747439SYinghai Lu { 2038747439SYinghai Lu ssize_t out = 0; 2138747439SYinghai Lu 2238747439SYinghai Lu /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */ 2338747439SYinghai Lu while (count) { 24bf6419e4SChristoph Hellwig ssize_t rv = kernel_write(file, p, count, pos); 2538747439SYinghai Lu 2638747439SYinghai Lu if (rv < 0) { 2738747439SYinghai Lu if (rv == -EINTR || rv == -EAGAIN) 2838747439SYinghai Lu continue; 2938747439SYinghai Lu return out ? out : rv; 3038747439SYinghai Lu } else if (rv == 0) 3138747439SYinghai Lu break; 3238747439SYinghai Lu 3338747439SYinghai Lu p += rv; 3438747439SYinghai Lu out += rv; 3538747439SYinghai Lu count -= rv; 3638747439SYinghai Lu } 3738747439SYinghai Lu 3838747439SYinghai Lu return out; 3938747439SYinghai Lu } 4038747439SYinghai Lu 411da177e4SLinus Torvalds static __initdata char *message; 421da177e4SLinus Torvalds static void __init error(char *x) 431da177e4SLinus Torvalds { 441da177e4SLinus Torvalds if (!message) 451da177e4SLinus Torvalds message = x; 461da177e4SLinus Torvalds } 471da177e4SLinus Torvalds 481da177e4SLinus Torvalds /* link hash */ 491da177e4SLinus Torvalds 506a050da4SMark Huang #define N_ALIGN(len) ((((len) + 1) & ~3) + 2) 516a050da4SMark Huang 521da177e4SLinus Torvalds static __initdata struct hash { 531da177e4SLinus Torvalds int ino, minor, major; 54685dd2d5SAl Viro umode_t mode; 551da177e4SLinus Torvalds struct hash *next; 566a050da4SMark Huang char name[N_ALIGN(PATH_MAX)]; 571da177e4SLinus Torvalds } *head[32]; 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds static inline int hash(int major, int minor, int ino) 601da177e4SLinus Torvalds { 611da177e4SLinus Torvalds unsigned long tmp = ino + minor + (major << 3); 621da177e4SLinus Torvalds tmp += tmp >> 5; 631da177e4SLinus Torvalds return tmp & 31; 641da177e4SLinus Torvalds } 651da177e4SLinus Torvalds 662139a7fbSH. Peter Anvin static char __init *find_link(int major, int minor, int ino, 67685dd2d5SAl Viro umode_t mode, char *name) 681da177e4SLinus Torvalds { 691da177e4SLinus Torvalds struct hash **p, *q; 701da177e4SLinus Torvalds for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) { 711da177e4SLinus Torvalds if ((*p)->ino != ino) 721da177e4SLinus Torvalds continue; 731da177e4SLinus Torvalds if ((*p)->minor != minor) 741da177e4SLinus Torvalds continue; 751da177e4SLinus Torvalds if ((*p)->major != major) 761da177e4SLinus Torvalds continue; 772139a7fbSH. Peter Anvin if (((*p)->mode ^ mode) & S_IFMT) 782139a7fbSH. Peter Anvin continue; 791da177e4SLinus Torvalds return (*p)->name; 801da177e4SLinus Torvalds } 813265e66bSThomas Petazzoni q = kmalloc(sizeof(struct hash), GFP_KERNEL); 821da177e4SLinus Torvalds if (!q) 831da177e4SLinus Torvalds panic("can't allocate link hash entry"); 841da177e4SLinus Torvalds q->major = major; 852139a7fbSH. Peter Anvin q->minor = minor; 862139a7fbSH. Peter Anvin q->ino = ino; 872139a7fbSH. Peter Anvin q->mode = mode; 886a050da4SMark Huang strcpy(q->name, name); 891da177e4SLinus Torvalds q->next = NULL; 901da177e4SLinus Torvalds *p = q; 911da177e4SLinus Torvalds return NULL; 921da177e4SLinus Torvalds } 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds static void __init free_hash(void) 951da177e4SLinus Torvalds { 961da177e4SLinus Torvalds struct hash **p, *q; 971da177e4SLinus Torvalds for (p = head; p < head + 32; p++) { 981da177e4SLinus Torvalds while (*p) { 991da177e4SLinus Torvalds q = *p; 1001da177e4SLinus Torvalds *p = q->next; 1013265e66bSThomas Petazzoni kfree(q); 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds } 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds 106e35c4c64SArnd Bergmann static long __init do_utime(char *filename, time64_t mtime) 107889d51a1SNye Liu { 108aaed2dd8SDeepa Dinamani struct timespec64 t[2]; 109889d51a1SNye Liu 110889d51a1SNye Liu t[0].tv_sec = mtime; 111889d51a1SNye Liu t[0].tv_nsec = 0; 112889d51a1SNye Liu t[1].tv_sec = mtime; 113889d51a1SNye Liu t[1].tv_nsec = 0; 114889d51a1SNye Liu 115889d51a1SNye Liu return do_utimes(AT_FDCWD, filename, t, AT_SYMLINK_NOFOLLOW); 116889d51a1SNye Liu } 117889d51a1SNye Liu 118889d51a1SNye Liu static __initdata LIST_HEAD(dir_list); 119889d51a1SNye Liu struct dir_entry { 120889d51a1SNye Liu struct list_head list; 121889d51a1SNye Liu char *name; 122e35c4c64SArnd Bergmann time64_t mtime; 123889d51a1SNye Liu }; 124889d51a1SNye Liu 125e35c4c64SArnd Bergmann static void __init dir_add(const char *name, time64_t mtime) 126889d51a1SNye Liu { 127889d51a1SNye Liu struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL); 128889d51a1SNye Liu if (!de) 129889d51a1SNye Liu panic("can't allocate dir_entry buffer"); 130889d51a1SNye Liu INIT_LIST_HEAD(&de->list); 131889d51a1SNye Liu de->name = kstrdup(name, GFP_KERNEL); 132889d51a1SNye Liu de->mtime = mtime; 133889d51a1SNye Liu list_add(&de->list, &dir_list); 134889d51a1SNye Liu } 135889d51a1SNye Liu 136889d51a1SNye Liu static void __init dir_utime(void) 137889d51a1SNye Liu { 138889d51a1SNye Liu struct dir_entry *de, *tmp; 139889d51a1SNye Liu list_for_each_entry_safe(de, tmp, &dir_list, list) { 140889d51a1SNye Liu list_del(&de->list); 141889d51a1SNye Liu do_utime(de->name, de->mtime); 142889d51a1SNye Liu kfree(de->name); 143889d51a1SNye Liu kfree(de); 144889d51a1SNye Liu } 145889d51a1SNye Liu } 146889d51a1SNye Liu 147e35c4c64SArnd Bergmann static __initdata time64_t mtime; 148889d51a1SNye Liu 1491da177e4SLinus Torvalds /* cpio header parsing */ 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds static __initdata unsigned long ino, major, minor, nlink; 152685dd2d5SAl Viro static __initdata umode_t mode; 1531da177e4SLinus Torvalds static __initdata unsigned long body_len, name_len; 1541da177e4SLinus Torvalds static __initdata uid_t uid; 1551da177e4SLinus Torvalds static __initdata gid_t gid; 1561da177e4SLinus Torvalds static __initdata unsigned rdev; 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds static void __init parse_header(char *s) 1591da177e4SLinus Torvalds { 1601da177e4SLinus Torvalds unsigned long parsed[12]; 1611da177e4SLinus Torvalds char buf[9]; 1621da177e4SLinus Torvalds int i; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds buf[8] = '\0'; 1651da177e4SLinus Torvalds for (i = 0, s += 6; i < 12; i++, s += 8) { 1661da177e4SLinus Torvalds memcpy(buf, s, 8); 1671da177e4SLinus Torvalds parsed[i] = simple_strtoul(buf, NULL, 16); 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds ino = parsed[0]; 1701da177e4SLinus Torvalds mode = parsed[1]; 1711da177e4SLinus Torvalds uid = parsed[2]; 1721da177e4SLinus Torvalds gid = parsed[3]; 1731da177e4SLinus Torvalds nlink = parsed[4]; 174e35c4c64SArnd Bergmann mtime = parsed[5]; /* breaks in y2106 */ 1751da177e4SLinus Torvalds body_len = parsed[6]; 1761da177e4SLinus Torvalds major = parsed[7]; 1771da177e4SLinus Torvalds minor = parsed[8]; 1781da177e4SLinus Torvalds rdev = new_encode_dev(MKDEV(parsed[9], parsed[10])); 1791da177e4SLinus Torvalds name_len = parsed[11]; 1801da177e4SLinus Torvalds } 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds /* FSM */ 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds static __initdata enum state { 1851da177e4SLinus Torvalds Start, 1861da177e4SLinus Torvalds Collect, 1871da177e4SLinus Torvalds GotHeader, 1881da177e4SLinus Torvalds SkipIt, 1891da177e4SLinus Torvalds GotName, 1901da177e4SLinus Torvalds CopyFile, 1911da177e4SLinus Torvalds GotSymlink, 1921da177e4SLinus Torvalds Reset 1931da177e4SLinus Torvalds } state, next_state; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds static __initdata char *victim; 196c34d85acSMark Rustad static unsigned long byte_count __initdata; 1971da177e4SLinus Torvalds static __initdata loff_t this_header, next_header; 1981da177e4SLinus Torvalds 199b0a5ab93SAl Viro static inline void __init eat(unsigned n) 2001da177e4SLinus Torvalds { 2011da177e4SLinus Torvalds victim += n; 2021da177e4SLinus Torvalds this_header += n; 203c34d85acSMark Rustad byte_count -= n; 2041da177e4SLinus Torvalds } 2051da177e4SLinus Torvalds 2061da177e4SLinus Torvalds static __initdata char *collected; 207d97b07c5SYinghai Lu static long remains __initdata; 2081da177e4SLinus Torvalds static __initdata char *collect; 2091da177e4SLinus Torvalds 2101da177e4SLinus Torvalds static void __init read_into(char *buf, unsigned size, enum state next) 2111da177e4SLinus Torvalds { 212c34d85acSMark Rustad if (byte_count >= size) { 2131da177e4SLinus Torvalds collected = victim; 2141da177e4SLinus Torvalds eat(size); 2151da177e4SLinus Torvalds state = next; 2161da177e4SLinus Torvalds } else { 2171da177e4SLinus Torvalds collect = collected = buf; 2181da177e4SLinus Torvalds remains = size; 2191da177e4SLinus Torvalds next_state = next; 2201da177e4SLinus Torvalds state = Collect; 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds } 2231da177e4SLinus Torvalds 2241da177e4SLinus Torvalds static __initdata char *header_buf, *symlink_buf, *name_buf; 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds static int __init do_start(void) 2271da177e4SLinus Torvalds { 2281da177e4SLinus Torvalds read_into(header_buf, 110, GotHeader); 2291da177e4SLinus Torvalds return 0; 2301da177e4SLinus Torvalds } 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds static int __init do_collect(void) 2331da177e4SLinus Torvalds { 234d97b07c5SYinghai Lu unsigned long n = remains; 235c34d85acSMark Rustad if (byte_count < n) 236c34d85acSMark Rustad n = byte_count; 2371da177e4SLinus Torvalds memcpy(collect, victim, n); 2381da177e4SLinus Torvalds eat(n); 2391da177e4SLinus Torvalds collect += n; 2401da177e4SLinus Torvalds if ((remains -= n) != 0) 2411da177e4SLinus Torvalds return 1; 2421da177e4SLinus Torvalds state = next_state; 2431da177e4SLinus Torvalds return 0; 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds static int __init do_header(void) 2471da177e4SLinus Torvalds { 2482e591bbcSArjan van de Ven if (memcmp(collected, "070707", 6)==0) { 2492e591bbcSArjan van de Ven error("incorrect cpio method used: use -H newc option"); 2502e591bbcSArjan van de Ven return 1; 2512e591bbcSArjan van de Ven } 2521da177e4SLinus Torvalds if (memcmp(collected, "070701", 6)) { 2531da177e4SLinus Torvalds error("no cpio magic"); 2541da177e4SLinus Torvalds return 1; 2551da177e4SLinus Torvalds } 2561da177e4SLinus Torvalds parse_header(collected); 2571da177e4SLinus Torvalds next_header = this_header + N_ALIGN(name_len) + body_len; 2581da177e4SLinus Torvalds next_header = (next_header + 3) & ~3; 2591da177e4SLinus Torvalds state = SkipIt; 2601da177e4SLinus Torvalds if (name_len <= 0 || name_len > PATH_MAX) 2611da177e4SLinus Torvalds return 0; 2621da177e4SLinus Torvalds if (S_ISLNK(mode)) { 2631da177e4SLinus Torvalds if (body_len > PATH_MAX) 2641da177e4SLinus Torvalds return 0; 2651da177e4SLinus Torvalds collect = collected = symlink_buf; 2661da177e4SLinus Torvalds remains = N_ALIGN(name_len) + body_len; 2671da177e4SLinus Torvalds next_state = GotSymlink; 2681da177e4SLinus Torvalds state = Collect; 2691da177e4SLinus Torvalds return 0; 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds if (S_ISREG(mode) || !body_len) 2721da177e4SLinus Torvalds read_into(name_buf, N_ALIGN(name_len), GotName); 2731da177e4SLinus Torvalds return 0; 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds static int __init do_skip(void) 2771da177e4SLinus Torvalds { 278c34d85acSMark Rustad if (this_header + byte_count < next_header) { 279c34d85acSMark Rustad eat(byte_count); 2801da177e4SLinus Torvalds return 1; 2811da177e4SLinus Torvalds } else { 2821da177e4SLinus Torvalds eat(next_header - this_header); 2831da177e4SLinus Torvalds state = next_state; 2841da177e4SLinus Torvalds return 0; 2851da177e4SLinus Torvalds } 2861da177e4SLinus Torvalds } 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds static int __init do_reset(void) 2891da177e4SLinus Torvalds { 290c34d85acSMark Rustad while (byte_count && *victim == '\0') 2911da177e4SLinus Torvalds eat(1); 292c34d85acSMark Rustad if (byte_count && (this_header & 3)) 2931da177e4SLinus Torvalds error("broken padding"); 2941da177e4SLinus Torvalds return 1; 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds 297c34d85acSMark Rustad static void __init clean_path(char *path, umode_t fmode) 2982139a7fbSH. Peter Anvin { 299046aa126SArnd Bergmann struct kstat st; 3002139a7fbSH. Peter Anvin 301046aa126SArnd Bergmann if (!vfs_lstat(path, &st) && (st.mode ^ fmode) & S_IFMT) { 302046aa126SArnd Bergmann if (S_ISDIR(st.mode)) 303*20cce026SChristoph Hellwig init_rmdir(path); 3042139a7fbSH. Peter Anvin else 3058fb9f73eSChristoph Hellwig init_unlink(path); 3062139a7fbSH. Peter Anvin } 3072139a7fbSH. Peter Anvin } 3082139a7fbSH. Peter Anvin 3097c0950d4SLi Zhijian static int __init maybe_link(void) 3107c0950d4SLi Zhijian { 3117c0950d4SLi Zhijian if (nlink >= 2) { 3127c0950d4SLi Zhijian char *old = find_link(major, minor, ino, mode, collected); 3137c0950d4SLi Zhijian if (old) { 3147c0950d4SLi Zhijian clean_path(collected, 0); 3157c0950d4SLi Zhijian return (ksys_link(old, collected) < 0) ? -1 : 1; 3167c0950d4SLi Zhijian } 3177c0950d4SLi Zhijian } 3187c0950d4SLi Zhijian return 0; 3197c0950d4SLi Zhijian } 3207c0950d4SLi Zhijian 321bf6419e4SChristoph Hellwig static __initdata struct file *wfile; 322bf6419e4SChristoph Hellwig static __initdata loff_t wfile_pos; 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds static int __init do_name(void) 3251da177e4SLinus Torvalds { 3261da177e4SLinus Torvalds state = SkipIt; 3271da177e4SLinus Torvalds next_state = Reset; 3281da177e4SLinus Torvalds if (strcmp(collected, "TRAILER!!!") == 0) { 3291da177e4SLinus Torvalds free_hash(); 3301da177e4SLinus Torvalds return 0; 3311da177e4SLinus Torvalds } 3322139a7fbSH. Peter Anvin clean_path(collected, mode); 3331da177e4SLinus Torvalds if (S_ISREG(mode)) { 3342139a7fbSH. Peter Anvin int ml = maybe_link(); 3352139a7fbSH. Peter Anvin if (ml >= 0) { 3362139a7fbSH. Peter Anvin int openflags = O_WRONLY|O_CREAT; 3372139a7fbSH. Peter Anvin if (ml != 1) 3382139a7fbSH. Peter Anvin openflags |= O_TRUNC; 339bf6419e4SChristoph Hellwig wfile = filp_open(collected, openflags, mode); 340bf6419e4SChristoph Hellwig if (IS_ERR(wfile)) 341bf6419e4SChristoph Hellwig return 0; 342bf6419e4SChristoph Hellwig wfile_pos = 0; 3432139a7fbSH. Peter Anvin 344bf6419e4SChristoph Hellwig vfs_fchown(wfile, uid, gid); 345bf6419e4SChristoph Hellwig vfs_fchmod(wfile, mode); 346d20d5a74SRandy Robertson if (body_len) 347bf6419e4SChristoph Hellwig vfs_truncate(&wfile->f_path, body_len); 3481da177e4SLinus Torvalds state = CopyFile; 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds } else if (S_ISDIR(mode)) { 3510101db7aSDominik Brodowski ksys_mkdir(collected, mode); 35255731b3cSDominik Brodowski ksys_chown(collected, uid, gid); 35303450e27SDominik Brodowski ksys_chmod(collected, mode); 354889d51a1SNye Liu dir_add(collected, mtime); 3551da177e4SLinus Torvalds } else if (S_ISBLK(mode) || S_ISCHR(mode) || 3561da177e4SLinus Torvalds S_ISFIFO(mode) || S_ISSOCK(mode)) { 3571da177e4SLinus Torvalds if (maybe_link() == 0) { 35887c4e192SDominik Brodowski ksys_mknod(collected, mode, rdev); 35955731b3cSDominik Brodowski ksys_chown(collected, uid, gid); 36003450e27SDominik Brodowski ksys_chmod(collected, mode); 361889d51a1SNye Liu do_utime(collected, mtime); 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds } 3641da177e4SLinus Torvalds return 0; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds static int __init do_copy(void) 3681da177e4SLinus Torvalds { 369c34d85acSMark Rustad if (byte_count >= body_len) { 37038b08223SChristoph Hellwig struct timespec64 t[2] = { }; 371bf6419e4SChristoph Hellwig if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len) 3729687fd91SDavid Engraf error("write error"); 37338b08223SChristoph Hellwig 37438b08223SChristoph Hellwig t[0].tv_sec = mtime; 37538b08223SChristoph Hellwig t[1].tv_sec = mtime; 37638b08223SChristoph Hellwig vfs_utimes(&wfile->f_path, t); 37738b08223SChristoph Hellwig 378bf6419e4SChristoph Hellwig fput(wfile); 3791da177e4SLinus Torvalds eat(body_len); 3801da177e4SLinus Torvalds state = SkipIt; 3811da177e4SLinus Torvalds return 0; 3821da177e4SLinus Torvalds } else { 383bf6419e4SChristoph Hellwig if (xwrite(wfile, victim, byte_count, &wfile_pos) != byte_count) 3849687fd91SDavid Engraf error("write error"); 385c34d85acSMark Rustad body_len -= byte_count; 386c34d85acSMark Rustad eat(byte_count); 3871da177e4SLinus Torvalds return 1; 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds } 3901da177e4SLinus Torvalds 3911da177e4SLinus Torvalds static int __init do_symlink(void) 3921da177e4SLinus Torvalds { 3931da177e4SLinus Torvalds collected[N_ALIGN(name_len) + body_len] = '\0'; 3942139a7fbSH. Peter Anvin clean_path(collected, 0); 395b724e846SDominik Brodowski ksys_symlink(collected + N_ALIGN(name_len), collected); 39655731b3cSDominik Brodowski ksys_lchown(collected, uid, gid); 397889d51a1SNye Liu do_utime(collected, mtime); 3981da177e4SLinus Torvalds state = SkipIt; 3991da177e4SLinus Torvalds next_state = Reset; 4001da177e4SLinus Torvalds return 0; 4011da177e4SLinus Torvalds } 4021da177e4SLinus Torvalds 4031da177e4SLinus Torvalds static __initdata int (*actions[])(void) = { 4041da177e4SLinus Torvalds [Start] = do_start, 4051da177e4SLinus Torvalds [Collect] = do_collect, 4061da177e4SLinus Torvalds [GotHeader] = do_header, 4071da177e4SLinus Torvalds [SkipIt] = do_skip, 4081da177e4SLinus Torvalds [GotName] = do_name, 4091da177e4SLinus Torvalds [CopyFile] = do_copy, 4101da177e4SLinus Torvalds [GotSymlink] = do_symlink, 4111da177e4SLinus Torvalds [Reset] = do_reset, 4121da177e4SLinus Torvalds }; 4131da177e4SLinus Torvalds 414d97b07c5SYinghai Lu static long __init write_buffer(char *buf, unsigned long len) 4151da177e4SLinus Torvalds { 416c34d85acSMark Rustad byte_count = len; 4171da177e4SLinus Torvalds victim = buf; 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds while (!actions[state]()) 4201da177e4SLinus Torvalds ; 421c34d85acSMark Rustad return len - byte_count; 4221da177e4SLinus Torvalds } 4231da177e4SLinus Torvalds 424d97b07c5SYinghai Lu static long __init flush_buffer(void *bufv, unsigned long len) 4251da177e4SLinus Torvalds { 42630d65dbfSAlain Knaff char *buf = (char *) bufv; 427d97b07c5SYinghai Lu long written; 428d97b07c5SYinghai Lu long origLen = len; 4291da177e4SLinus Torvalds if (message) 43030d65dbfSAlain Knaff return -1; 4311da177e4SLinus Torvalds while ((written = write_buffer(buf, len)) < len && !message) { 4321da177e4SLinus Torvalds char c = buf[written]; 4331da177e4SLinus Torvalds if (c == '0') { 4341da177e4SLinus Torvalds buf += written; 4351da177e4SLinus Torvalds len -= written; 4361da177e4SLinus Torvalds state = Start; 4371da177e4SLinus Torvalds } else if (c == 0) { 4381da177e4SLinus Torvalds buf += written; 4391da177e4SLinus Torvalds len -= written; 4401da177e4SLinus Torvalds state = Reset; 4411da177e4SLinus Torvalds } else 442e5eed351SDavid Engraf error("junk within compressed archive"); 4431da177e4SLinus Torvalds } 44430d65dbfSAlain Knaff return origLen; 4451da177e4SLinus Torvalds } 4461da177e4SLinus Torvalds 447d97b07c5SYinghai Lu static unsigned long my_inptr; /* index of next byte to be processed in inbuf */ 4481da177e4SLinus Torvalds 449889c92d2SH. Peter Anvin #include <linux/decompress/generic.h> 4501da177e4SLinus Torvalds 451d97b07c5SYinghai Lu static char * __init unpack_to_rootfs(char *buf, unsigned long len) 4521da177e4SLinus Torvalds { 453d97b07c5SYinghai Lu long written; 454889c92d2SH. Peter Anvin decompress_fn decompress; 45523a22d57SH. Peter Anvin const char *compress_name; 45623a22d57SH. Peter Anvin static __initdata char msg_buf[64]; 457889c92d2SH. Peter Anvin 4583265e66bSThomas Petazzoni header_buf = kmalloc(110, GFP_KERNEL); 4593265e66bSThomas Petazzoni symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL); 4603265e66bSThomas Petazzoni name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL); 46130d65dbfSAlain Knaff 46230d65dbfSAlain Knaff if (!header_buf || !symlink_buf || !name_buf) 4631da177e4SLinus Torvalds panic("can't allocate buffers"); 46430d65dbfSAlain Knaff 4651da177e4SLinus Torvalds state = Start; 4661da177e4SLinus Torvalds this_header = 0; 4671da177e4SLinus Torvalds message = NULL; 4681da177e4SLinus Torvalds while (!message && len) { 4691da177e4SLinus Torvalds loff_t saved_offset = this_header; 4701da177e4SLinus Torvalds if (*buf == '0' && !(this_header & 3)) { 4711da177e4SLinus Torvalds state = Start; 4721da177e4SLinus Torvalds written = write_buffer(buf, len); 4731da177e4SLinus Torvalds buf += written; 4741da177e4SLinus Torvalds len -= written; 4751da177e4SLinus Torvalds continue; 4761da177e4SLinus Torvalds } 4771da177e4SLinus Torvalds if (!*buf) { 4781da177e4SLinus Torvalds buf++; 4791da177e4SLinus Torvalds len--; 4801da177e4SLinus Torvalds this_header++; 4811da177e4SLinus Torvalds continue; 4821da177e4SLinus Torvalds } 4831da177e4SLinus Torvalds this_header = 0; 48423a22d57SH. Peter Anvin decompress = decompress_method(buf, len, &compress_name); 4856aa7a29aSDaniel M. Weeks pr_debug("Detected %s compressed data\n", compress_name); 48654291362SPhillip Lougher if (decompress) { 487d97b07c5SYinghai Lu int res = decompress(buf, len, NULL, flush_buffer, NULL, 488889c92d2SH. Peter Anvin &my_inptr, error); 48954291362SPhillip Lougher if (res) 49054291362SPhillip Lougher error("decompressor failed"); 49154291362SPhillip Lougher } else if (compress_name) { 49223a22d57SH. Peter Anvin if (!message) { 49323a22d57SH. Peter Anvin snprintf(msg_buf, sizeof msg_buf, 49423a22d57SH. Peter Anvin "compression method %s not configured", 49523a22d57SH. Peter Anvin compress_name); 49623a22d57SH. Peter Anvin message = msg_buf; 49723a22d57SH. Peter Anvin } 498df37bd15SPhillip Lougher } else 499e5eed351SDavid Engraf error("invalid magic at start of compressed archive"); 5001da177e4SLinus Torvalds if (state != Reset) 501e5eed351SDavid Engraf error("junk at the end of compressed archive"); 50230d65dbfSAlain Knaff this_header = saved_offset + my_inptr; 50330d65dbfSAlain Knaff buf += my_inptr; 50430d65dbfSAlain Knaff len -= my_inptr; 5051da177e4SLinus Torvalds } 506889d51a1SNye Liu dir_utime(); 5073265e66bSThomas Petazzoni kfree(name_buf); 5083265e66bSThomas Petazzoni kfree(symlink_buf); 5093265e66bSThomas Petazzoni kfree(header_buf); 5101da177e4SLinus Torvalds return message; 5111da177e4SLinus Torvalds } 5121da177e4SLinus Torvalds 5130a7b35cbSMichael Neuling static int __initdata do_retain_initrd; 5140a7b35cbSMichael Neuling 5150a7b35cbSMichael Neuling static int __init retain_initrd_param(char *str) 5160a7b35cbSMichael Neuling { 5170a7b35cbSMichael Neuling if (*str) 5180a7b35cbSMichael Neuling return 0; 5190a7b35cbSMichael Neuling do_retain_initrd = 1; 5200a7b35cbSMichael Neuling return 1; 5210a7b35cbSMichael Neuling } 5220a7b35cbSMichael Neuling __setup("retain_initrd", retain_initrd_param); 5230a7b35cbSMichael Neuling 524d8ae8a37SChristoph Hellwig #ifdef CONFIG_ARCH_HAS_KEEPINITRD 525d8ae8a37SChristoph Hellwig static int __init keepinitrd_setup(char *__unused) 526d8ae8a37SChristoph Hellwig { 527d8ae8a37SChristoph Hellwig do_retain_initrd = 1; 528d8ae8a37SChristoph Hellwig return 1; 529d8ae8a37SChristoph Hellwig } 530d8ae8a37SChristoph Hellwig __setup("keepinitrd", keepinitrd_setup); 531d8ae8a37SChristoph Hellwig #endif 532d8ae8a37SChristoph Hellwig 533ffe8018cSHendrik Brueckner extern char __initramfs_start[]; 534ffe8018cSHendrik Brueckner extern unsigned long __initramfs_size; 5351da177e4SLinus Torvalds #include <linux/initrd.h> 5369c15e852SHaren Myneni #include <linux/kexec.h> 5370f3d2bd5SJan Beulich 5384afd58e1SChristoph Hellwig void __weak free_initrd_mem(unsigned long start, unsigned long end) 5394afd58e1SChristoph Hellwig { 540899ee4afSMike Rapoport #ifdef CONFIG_ARCH_KEEP_MEMBLOCK 541899ee4afSMike Rapoport unsigned long aligned_start = ALIGN_DOWN(start, PAGE_SIZE); 542899ee4afSMike Rapoport unsigned long aligned_end = ALIGN(end, PAGE_SIZE); 543899ee4afSMike Rapoport 544899ee4afSMike Rapoport memblock_free(__pa(aligned_start), aligned_end - aligned_start); 545899ee4afSMike Rapoport #endif 546899ee4afSMike Rapoport 547f94f7434SChristoph Hellwig free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM, 548f94f7434SChristoph Hellwig "initrd"); 5494afd58e1SChristoph Hellwig } 5504afd58e1SChristoph Hellwig 5512965faa5SDave Young #ifdef CONFIG_KEXEC_CORE 552e99332e7SLinus Torvalds static bool __init kexec_free_initrd(void) 55323091e28SChristoph Hellwig { 5549c15e852SHaren Myneni unsigned long crashk_start = (unsigned long)__va(crashk_res.start); 5559c15e852SHaren Myneni unsigned long crashk_end = (unsigned long)__va(crashk_res.end); 5569c15e852SHaren Myneni 5579c15e852SHaren Myneni /* 5589c15e852SHaren Myneni * If the initrd region is overlapped with crashkernel reserved region, 5599c15e852SHaren Myneni * free only memory that is not part of crashkernel region. 5609c15e852SHaren Myneni */ 56123091e28SChristoph Hellwig if (initrd_start >= crashk_end || initrd_end <= crashk_start) 56223091e28SChristoph Hellwig return false; 56323091e28SChristoph Hellwig 5649c15e852SHaren Myneni /* 56523091e28SChristoph Hellwig * Initialize initrd memory region since the kexec boot does not do. 5669c15e852SHaren Myneni */ 5679c15e852SHaren Myneni memset((void *)initrd_start, 0, initrd_end - initrd_start); 5689c15e852SHaren Myneni if (initrd_start < crashk_start) 5699c15e852SHaren Myneni free_initrd_mem(initrd_start, crashk_start); 5709c15e852SHaren Myneni if (initrd_end > crashk_end) 5719c15e852SHaren Myneni free_initrd_mem(crashk_end, initrd_end); 57223091e28SChristoph Hellwig return true; 5730f3d2bd5SJan Beulich } 57423091e28SChristoph Hellwig #else 57523091e28SChristoph Hellwig static inline bool kexec_free_initrd(void) 57623091e28SChristoph Hellwig { 57723091e28SChristoph Hellwig return false; 57823091e28SChristoph Hellwig } 57923091e28SChristoph Hellwig #endif /* CONFIG_KEXEC_CORE */ 5800f3d2bd5SJan Beulich 581a841c673SAndrew Morton #ifdef CONFIG_BLK_DEV_RAM 5824ada1e81SGeert Uytterhoeven static void __init populate_initrd_image(char *err) 5837c184ecdSChristoph Hellwig { 5847c184ecdSChristoph Hellwig ssize_t written; 585bf6419e4SChristoph Hellwig struct file *file; 586bf6419e4SChristoph Hellwig loff_t pos = 0; 5877c184ecdSChristoph Hellwig 5887c184ecdSChristoph Hellwig unpack_to_rootfs(__initramfs_start, __initramfs_size); 5897c184ecdSChristoph Hellwig 5907c184ecdSChristoph Hellwig printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n", 5917c184ecdSChristoph Hellwig err); 592bf6419e4SChristoph Hellwig file = filp_open("/initrd.image", O_WRONLY | O_CREAT, 0700); 593bf6419e4SChristoph Hellwig if (IS_ERR(file)) 5947c184ecdSChristoph Hellwig return; 5957c184ecdSChristoph Hellwig 596bf6419e4SChristoph Hellwig written = xwrite(file, (char *)initrd_start, initrd_end - initrd_start, 597bf6419e4SChristoph Hellwig &pos); 5987c184ecdSChristoph Hellwig if (written != initrd_end - initrd_start) 5997c184ecdSChristoph Hellwig pr_err("/initrd.image: incomplete write (%zd != %ld)\n", 6007c184ecdSChristoph Hellwig written, initrd_end - initrd_start); 601bf6419e4SChristoph Hellwig fput(file); 6027c184ecdSChristoph Hellwig } 6037c184ecdSChristoph Hellwig #endif /* CONFIG_BLK_DEV_RAM */ 6047c184ecdSChristoph Hellwig 6059a9e0d68SLinus Torvalds static int __init populate_rootfs(void) 6061da177e4SLinus Torvalds { 60717a9be31SStafford Horne /* Load the built in initramfs */ 608ffe8018cSHendrik Brueckner char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size); 6091da177e4SLinus Torvalds if (err) 610499a4584STetsuo Handa panic("%s", err); /* Failed to decompress INTERNAL initramfs */ 611afef7889SChristoph Hellwig 612afef7889SChristoph Hellwig if (!initrd_start || IS_ENABLED(CONFIG_INITRAMFS_FORCE)) 613bb813f4cSTejun Heo goto done; 61454c7a891SChristoph Hellwig 615afef7889SChristoph Hellwig if (IS_ENABLED(CONFIG_BLK_DEV_RAM)) 616afef7889SChristoph Hellwig printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n"); 617afef7889SChristoph Hellwig else 618afef7889SChristoph Hellwig printk(KERN_INFO "Unpacking initramfs...\n"); 619afef7889SChristoph Hellwig 620afef7889SChristoph Hellwig err = unpack_to_rootfs((char *)initrd_start, initrd_end - initrd_start); 621afef7889SChristoph Hellwig if (err) { 6229ab6b718SChristoph Hellwig #ifdef CONFIG_BLK_DEV_RAM 6237c184ecdSChristoph Hellwig populate_initrd_image(err); 6249ab6b718SChristoph Hellwig #else 6259ab6b718SChristoph Hellwig printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err); 6269ab6b718SChristoph Hellwig #endif 62717a9be31SStafford Horne } 62823091e28SChristoph Hellwig 629afef7889SChristoph Hellwig done: 63023091e28SChristoph Hellwig /* 63123091e28SChristoph Hellwig * If the initrd region is overlapped with crashkernel reserved region, 63223091e28SChristoph Hellwig * free only memory that is not part of crashkernel region. 63323091e28SChristoph Hellwig */ 6345d59aa8fSSteven Price if (!do_retain_initrd && initrd_start && !kexec_free_initrd()) 63523091e28SChristoph Hellwig free_initrd_mem(initrd_start, initrd_end); 63623091e28SChristoph Hellwig initrd_start = 0; 63723091e28SChristoph Hellwig initrd_end = 0; 63823091e28SChristoph Hellwig 63908865514SLokesh Vutla flush_delayed_fput(); 6408d610dd5SLinus Torvalds return 0; 6411da177e4SLinus Torvalds } 6428d610dd5SLinus Torvalds rootfs_initcall(populate_rootfs); 643