xref: /linux/init/initramfs.c (revision da028e4c4b0279eb49f80220d8f7cc62b4a57ccb)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds #include <linux/init.h>
3e7cb072eSRasmus Villemoes #include <linux/async.h>
41da177e4SLinus Torvalds #include <linux/fs.h>
51da177e4SLinus Torvalds #include <linux/slab.h>
61da177e4SLinus Torvalds #include <linux/types.h>
71da177e4SLinus Torvalds #include <linux/fcntl.h>
81da177e4SLinus Torvalds #include <linux/delay.h>
91da177e4SLinus Torvalds #include <linux/string.h>
10df52092fSLi, Shaohua #include <linux/dirent.h>
111da177e4SLinus Torvalds #include <linux/syscalls.h>
12889d51a1SNye Liu #include <linux/utime.h>
1308865514SLokesh Vutla #include <linux/file.h>
14899ee4afSMike Rapoport #include <linux/memblock.h>
15dd23e809SFlorian Fainelli #include <linux/mm.h>
16b2a74d5fSChristoph Hellwig #include <linux/namei.h>
178fb9f73eSChristoph Hellwig #include <linux/init_syscalls.h>
18b234ed6dSRasmus Villemoes #include <linux/umh.h>
191da177e4SLinus Torvalds 
20bf6419e4SChristoph Hellwig static ssize_t __init xwrite(struct file *file, const char *p, size_t count,
21bf6419e4SChristoph Hellwig 		loff_t *pos)
2238747439SYinghai Lu {
2338747439SYinghai Lu 	ssize_t out = 0;
2438747439SYinghai Lu 
2538747439SYinghai Lu 	/* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
2638747439SYinghai Lu 	while (count) {
27bf6419e4SChristoph Hellwig 		ssize_t rv = kernel_write(file, p, count, pos);
2838747439SYinghai Lu 
2938747439SYinghai Lu 		if (rv < 0) {
3038747439SYinghai Lu 			if (rv == -EINTR || rv == -EAGAIN)
3138747439SYinghai Lu 				continue;
3238747439SYinghai Lu 			return out ? out : rv;
3338747439SYinghai Lu 		} else if (rv == 0)
3438747439SYinghai Lu 			break;
3538747439SYinghai Lu 
3638747439SYinghai Lu 		p += rv;
3738747439SYinghai Lu 		out += rv;
3838747439SYinghai Lu 		count -= rv;
3938747439SYinghai Lu 	}
4038747439SYinghai Lu 
4138747439SYinghai Lu 	return out;
4238747439SYinghai Lu }
4338747439SYinghai Lu 
441da177e4SLinus Torvalds static __initdata char *message;
451da177e4SLinus Torvalds static void __init error(char *x)
461da177e4SLinus Torvalds {
471da177e4SLinus Torvalds 	if (!message)
481da177e4SLinus Torvalds 		message = x;
491da177e4SLinus Torvalds }
501da177e4SLinus Torvalds 
51dd23e809SFlorian Fainelli static void panic_show_mem(const char *fmt, ...)
52dd23e809SFlorian Fainelli {
53dd23e809SFlorian Fainelli 	va_list args;
54dd23e809SFlorian Fainelli 
55dd23e809SFlorian Fainelli 	show_mem(0, NULL);
56dd23e809SFlorian Fainelli 	va_start(args, fmt);
57dd23e809SFlorian Fainelli 	panic(fmt, args);
58dd23e809SFlorian Fainelli 	va_end(args);
59dd23e809SFlorian Fainelli }
60dd23e809SFlorian Fainelli 
611da177e4SLinus Torvalds /* link hash */
621da177e4SLinus Torvalds 
636a050da4SMark Huang #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
646a050da4SMark Huang 
651da177e4SLinus Torvalds static __initdata struct hash {
661da177e4SLinus Torvalds 	int ino, minor, major;
67685dd2d5SAl Viro 	umode_t mode;
681da177e4SLinus Torvalds 	struct hash *next;
696a050da4SMark Huang 	char name[N_ALIGN(PATH_MAX)];
701da177e4SLinus Torvalds } *head[32];
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds static inline int hash(int major, int minor, int ino)
731da177e4SLinus Torvalds {
741da177e4SLinus Torvalds 	unsigned long tmp = ino + minor + (major << 3);
751da177e4SLinus Torvalds 	tmp += tmp >> 5;
761da177e4SLinus Torvalds 	return tmp & 31;
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds 
792139a7fbSH. Peter Anvin static char __init *find_link(int major, int minor, int ino,
80685dd2d5SAl Viro 			      umode_t mode, char *name)
811da177e4SLinus Torvalds {
821da177e4SLinus Torvalds 	struct hash **p, *q;
831da177e4SLinus Torvalds 	for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
841da177e4SLinus Torvalds 		if ((*p)->ino != ino)
851da177e4SLinus Torvalds 			continue;
861da177e4SLinus Torvalds 		if ((*p)->minor != minor)
871da177e4SLinus Torvalds 			continue;
881da177e4SLinus Torvalds 		if ((*p)->major != major)
891da177e4SLinus Torvalds 			continue;
902139a7fbSH. Peter Anvin 		if (((*p)->mode ^ mode) & S_IFMT)
912139a7fbSH. Peter Anvin 			continue;
921da177e4SLinus Torvalds 		return (*p)->name;
931da177e4SLinus Torvalds 	}
943265e66bSThomas Petazzoni 	q = kmalloc(sizeof(struct hash), GFP_KERNEL);
951da177e4SLinus Torvalds 	if (!q)
96dd23e809SFlorian Fainelli 		panic_show_mem("can't allocate link hash entry");
971da177e4SLinus Torvalds 	q->major = major;
982139a7fbSH. Peter Anvin 	q->minor = minor;
992139a7fbSH. Peter Anvin 	q->ino = ino;
1002139a7fbSH. Peter Anvin 	q->mode = mode;
1016a050da4SMark Huang 	strcpy(q->name, name);
1021da177e4SLinus Torvalds 	q->next = NULL;
1031da177e4SLinus Torvalds 	*p = q;
1041da177e4SLinus Torvalds 	return NULL;
1051da177e4SLinus Torvalds }
1061da177e4SLinus Torvalds 
1071da177e4SLinus Torvalds static void __init free_hash(void)
1081da177e4SLinus Torvalds {
1091da177e4SLinus Torvalds 	struct hash **p, *q;
1101da177e4SLinus Torvalds 	for (p = head; p < head + 32; p++) {
1111da177e4SLinus Torvalds 		while (*p) {
1121da177e4SLinus Torvalds 			q = *p;
1131da177e4SLinus Torvalds 			*p = q->next;
1143265e66bSThomas Petazzoni 			kfree(q);
1151da177e4SLinus Torvalds 		}
1161da177e4SLinus Torvalds 	}
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
119e35c4c64SArnd Bergmann static long __init do_utime(char *filename, time64_t mtime)
120889d51a1SNye Liu {
121aaed2dd8SDeepa Dinamani 	struct timespec64 t[2];
122889d51a1SNye Liu 
123889d51a1SNye Liu 	t[0].tv_sec = mtime;
124889d51a1SNye Liu 	t[0].tv_nsec = 0;
125889d51a1SNye Liu 	t[1].tv_sec = mtime;
126889d51a1SNye Liu 	t[1].tv_nsec = 0;
127235e5793SChristoph Hellwig 	return init_utimes(filename, t);
128889d51a1SNye Liu }
129889d51a1SNye Liu 
130889d51a1SNye Liu static __initdata LIST_HEAD(dir_list);
131889d51a1SNye Liu struct dir_entry {
132889d51a1SNye Liu 	struct list_head list;
133889d51a1SNye Liu 	char *name;
134e35c4c64SArnd Bergmann 	time64_t mtime;
135889d51a1SNye Liu };
136889d51a1SNye Liu 
137e35c4c64SArnd Bergmann static void __init dir_add(const char *name, time64_t mtime)
138889d51a1SNye Liu {
139889d51a1SNye Liu 	struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
140889d51a1SNye Liu 	if (!de)
141dd23e809SFlorian Fainelli 		panic_show_mem("can't allocate dir_entry buffer");
142889d51a1SNye Liu 	INIT_LIST_HEAD(&de->list);
143889d51a1SNye Liu 	de->name = kstrdup(name, GFP_KERNEL);
144889d51a1SNye Liu 	de->mtime = mtime;
145889d51a1SNye Liu 	list_add(&de->list, &dir_list);
146889d51a1SNye Liu }
147889d51a1SNye Liu 
148889d51a1SNye Liu static void __init dir_utime(void)
149889d51a1SNye Liu {
150889d51a1SNye Liu 	struct dir_entry *de, *tmp;
151889d51a1SNye Liu 	list_for_each_entry_safe(de, tmp, &dir_list, list) {
152889d51a1SNye Liu 		list_del(&de->list);
153889d51a1SNye Liu 		do_utime(de->name, de->mtime);
154889d51a1SNye Liu 		kfree(de->name);
155889d51a1SNye Liu 		kfree(de);
156889d51a1SNye Liu 	}
157889d51a1SNye Liu }
158889d51a1SNye Liu 
159e35c4c64SArnd Bergmann static __initdata time64_t mtime;
160889d51a1SNye Liu 
1611da177e4SLinus Torvalds /* cpio header parsing */
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds static __initdata unsigned long ino, major, minor, nlink;
164685dd2d5SAl Viro static __initdata umode_t mode;
1651da177e4SLinus Torvalds static __initdata unsigned long body_len, name_len;
1661da177e4SLinus Torvalds static __initdata uid_t uid;
1671da177e4SLinus Torvalds static __initdata gid_t gid;
1681da177e4SLinus Torvalds static __initdata unsigned rdev;
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds static void __init parse_header(char *s)
1711da177e4SLinus Torvalds {
1721da177e4SLinus Torvalds 	unsigned long parsed[12];
1731da177e4SLinus Torvalds 	char buf[9];
1741da177e4SLinus Torvalds 	int i;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	buf[8] = '\0';
1771da177e4SLinus Torvalds 	for (i = 0, s += 6; i < 12; i++, s += 8) {
1781da177e4SLinus Torvalds 		memcpy(buf, s, 8);
1791da177e4SLinus Torvalds 		parsed[i] = simple_strtoul(buf, NULL, 16);
1801da177e4SLinus Torvalds 	}
1811da177e4SLinus Torvalds 	ino = parsed[0];
1821da177e4SLinus Torvalds 	mode = parsed[1];
1831da177e4SLinus Torvalds 	uid = parsed[2];
1841da177e4SLinus Torvalds 	gid = parsed[3];
1851da177e4SLinus Torvalds 	nlink = parsed[4];
186e35c4c64SArnd Bergmann 	mtime = parsed[5]; /* breaks in y2106 */
1871da177e4SLinus Torvalds 	body_len = parsed[6];
1881da177e4SLinus Torvalds 	major = parsed[7];
1891da177e4SLinus Torvalds 	minor = parsed[8];
1901da177e4SLinus Torvalds 	rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
1911da177e4SLinus Torvalds 	name_len = parsed[11];
1921da177e4SLinus Torvalds }
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds /* FSM */
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds static __initdata enum state {
1971da177e4SLinus Torvalds 	Start,
1981da177e4SLinus Torvalds 	Collect,
1991da177e4SLinus Torvalds 	GotHeader,
2001da177e4SLinus Torvalds 	SkipIt,
2011da177e4SLinus Torvalds 	GotName,
2021da177e4SLinus Torvalds 	CopyFile,
2031da177e4SLinus Torvalds 	GotSymlink,
2041da177e4SLinus Torvalds 	Reset
2051da177e4SLinus Torvalds } state, next_state;
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds static __initdata char *victim;
208c34d85acSMark Rustad static unsigned long byte_count __initdata;
2091da177e4SLinus Torvalds static __initdata loff_t this_header, next_header;
2101da177e4SLinus Torvalds 
211b0a5ab93SAl Viro static inline void __init eat(unsigned n)
2121da177e4SLinus Torvalds {
2131da177e4SLinus Torvalds 	victim += n;
2141da177e4SLinus Torvalds 	this_header += n;
215c34d85acSMark Rustad 	byte_count -= n;
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds static __initdata char *collected;
219d97b07c5SYinghai Lu static long remains __initdata;
2201da177e4SLinus Torvalds static __initdata char *collect;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds static void __init read_into(char *buf, unsigned size, enum state next)
2231da177e4SLinus Torvalds {
224c34d85acSMark Rustad 	if (byte_count >= size) {
2251da177e4SLinus Torvalds 		collected = victim;
2261da177e4SLinus Torvalds 		eat(size);
2271da177e4SLinus Torvalds 		state = next;
2281da177e4SLinus Torvalds 	} else {
2291da177e4SLinus Torvalds 		collect = collected = buf;
2301da177e4SLinus Torvalds 		remains = size;
2311da177e4SLinus Torvalds 		next_state = next;
2321da177e4SLinus Torvalds 		state = Collect;
2331da177e4SLinus Torvalds 	}
2341da177e4SLinus Torvalds }
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds static __initdata char *header_buf, *symlink_buf, *name_buf;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds static int __init do_start(void)
2391da177e4SLinus Torvalds {
2401da177e4SLinus Torvalds 	read_into(header_buf, 110, GotHeader);
2411da177e4SLinus Torvalds 	return 0;
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds 
2441da177e4SLinus Torvalds static int __init do_collect(void)
2451da177e4SLinus Torvalds {
246d97b07c5SYinghai Lu 	unsigned long n = remains;
247c34d85acSMark Rustad 	if (byte_count < n)
248c34d85acSMark Rustad 		n = byte_count;
2491da177e4SLinus Torvalds 	memcpy(collect, victim, n);
2501da177e4SLinus Torvalds 	eat(n);
2511da177e4SLinus Torvalds 	collect += n;
2521da177e4SLinus Torvalds 	if ((remains -= n) != 0)
2531da177e4SLinus Torvalds 		return 1;
2541da177e4SLinus Torvalds 	state = next_state;
2551da177e4SLinus Torvalds 	return 0;
2561da177e4SLinus Torvalds }
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds static int __init do_header(void)
2591da177e4SLinus Torvalds {
2601da177e4SLinus Torvalds 	if (memcmp(collected, "070701", 6)) {
261*da028e4cSDavid Disseldorp 		if (memcmp(collected, "070707", 6) == 0)
262*da028e4cSDavid Disseldorp 			error("incorrect cpio method used: use -H newc option");
263*da028e4cSDavid Disseldorp 		else
2641da177e4SLinus Torvalds 			error("no cpio magic");
2651da177e4SLinus Torvalds 		return 1;
2661da177e4SLinus Torvalds 	}
2671da177e4SLinus Torvalds 	parse_header(collected);
2681da177e4SLinus Torvalds 	next_header = this_header + N_ALIGN(name_len) + body_len;
2691da177e4SLinus Torvalds 	next_header = (next_header + 3) & ~3;
2701da177e4SLinus Torvalds 	state = SkipIt;
2711da177e4SLinus Torvalds 	if (name_len <= 0 || name_len > PATH_MAX)
2721da177e4SLinus Torvalds 		return 0;
2731da177e4SLinus Torvalds 	if (S_ISLNK(mode)) {
2741da177e4SLinus Torvalds 		if (body_len > PATH_MAX)
2751da177e4SLinus Torvalds 			return 0;
2761da177e4SLinus Torvalds 		collect = collected = symlink_buf;
2771da177e4SLinus Torvalds 		remains = N_ALIGN(name_len) + body_len;
2781da177e4SLinus Torvalds 		next_state = GotSymlink;
2791da177e4SLinus Torvalds 		state = Collect;
2801da177e4SLinus Torvalds 		return 0;
2811da177e4SLinus Torvalds 	}
2821da177e4SLinus Torvalds 	if (S_ISREG(mode) || !body_len)
2831da177e4SLinus Torvalds 		read_into(name_buf, N_ALIGN(name_len), GotName);
2841da177e4SLinus Torvalds 	return 0;
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds static int __init do_skip(void)
2881da177e4SLinus Torvalds {
289c34d85acSMark Rustad 	if (this_header + byte_count < next_header) {
290c34d85acSMark Rustad 		eat(byte_count);
2911da177e4SLinus Torvalds 		return 1;
2921da177e4SLinus Torvalds 	} else {
2931da177e4SLinus Torvalds 		eat(next_header - this_header);
2941da177e4SLinus Torvalds 		state = next_state;
2951da177e4SLinus Torvalds 		return 0;
2961da177e4SLinus Torvalds 	}
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds static int __init do_reset(void)
3001da177e4SLinus Torvalds {
301c34d85acSMark Rustad 	while (byte_count && *victim == '\0')
3021da177e4SLinus Torvalds 		eat(1);
303c34d85acSMark Rustad 	if (byte_count && (this_header & 3))
3041da177e4SLinus Torvalds 		error("broken padding");
3051da177e4SLinus Torvalds 	return 1;
3061da177e4SLinus Torvalds }
3071da177e4SLinus Torvalds 
308c34d85acSMark Rustad static void __init clean_path(char *path, umode_t fmode)
3092139a7fbSH. Peter Anvin {
310046aa126SArnd Bergmann 	struct kstat st;
3112139a7fbSH. Peter Anvin 
3127b81ce7cSBarret Rhoden 	if (!init_stat(path, &st, AT_SYMLINK_NOFOLLOW) &&
313716308a5SChristoph Hellwig 	    (st.mode ^ fmode) & S_IFMT) {
314046aa126SArnd Bergmann 		if (S_ISDIR(st.mode))
31520cce026SChristoph Hellwig 			init_rmdir(path);
3162139a7fbSH. Peter Anvin 		else
3178fb9f73eSChristoph Hellwig 			init_unlink(path);
3182139a7fbSH. Peter Anvin 	}
3192139a7fbSH. Peter Anvin }
3202139a7fbSH. Peter Anvin 
3217c0950d4SLi Zhijian static int __init maybe_link(void)
3227c0950d4SLi Zhijian {
3237c0950d4SLi Zhijian 	if (nlink >= 2) {
3247c0950d4SLi Zhijian 		char *old = find_link(major, minor, ino, mode, collected);
3257c0950d4SLi Zhijian 		if (old) {
3267c0950d4SLi Zhijian 			clean_path(collected, 0);
327812931d6SChristoph Hellwig 			return (init_link(old, collected) < 0) ? -1 : 1;
3287c0950d4SLi Zhijian 		}
3297c0950d4SLi Zhijian 	}
3307c0950d4SLi Zhijian 	return 0;
3317c0950d4SLi Zhijian }
3327c0950d4SLi Zhijian 
333bf6419e4SChristoph Hellwig static __initdata struct file *wfile;
334bf6419e4SChristoph Hellwig static __initdata loff_t wfile_pos;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds static int __init do_name(void)
3371da177e4SLinus Torvalds {
3381da177e4SLinus Torvalds 	state = SkipIt;
3391da177e4SLinus Torvalds 	next_state = Reset;
3401da177e4SLinus Torvalds 	if (strcmp(collected, "TRAILER!!!") == 0) {
3411da177e4SLinus Torvalds 		free_hash();
3421da177e4SLinus Torvalds 		return 0;
3431da177e4SLinus Torvalds 	}
3442139a7fbSH. Peter Anvin 	clean_path(collected, mode);
3451da177e4SLinus Torvalds 	if (S_ISREG(mode)) {
3462139a7fbSH. Peter Anvin 		int ml = maybe_link();
3472139a7fbSH. Peter Anvin 		if (ml >= 0) {
3482139a7fbSH. Peter Anvin 			int openflags = O_WRONLY|O_CREAT;
3492139a7fbSH. Peter Anvin 			if (ml != 1)
3502139a7fbSH. Peter Anvin 				openflags |= O_TRUNC;
351bf6419e4SChristoph Hellwig 			wfile = filp_open(collected, openflags, mode);
352bf6419e4SChristoph Hellwig 			if (IS_ERR(wfile))
353bf6419e4SChristoph Hellwig 				return 0;
354bf6419e4SChristoph Hellwig 			wfile_pos = 0;
3552139a7fbSH. Peter Anvin 
356bf6419e4SChristoph Hellwig 			vfs_fchown(wfile, uid, gid);
357bf6419e4SChristoph Hellwig 			vfs_fchmod(wfile, mode);
358d20d5a74SRandy Robertson 			if (body_len)
359bf6419e4SChristoph Hellwig 				vfs_truncate(&wfile->f_path, body_len);
3601da177e4SLinus Torvalds 			state = CopyFile;
3611da177e4SLinus Torvalds 		}
3621da177e4SLinus Torvalds 	} else if (S_ISDIR(mode)) {
36383ff98c3SChristoph Hellwig 		init_mkdir(collected, mode);
364b873498fSChristoph Hellwig 		init_chown(collected, uid, gid, 0);
3651097742eSChristoph Hellwig 		init_chmod(collected, mode);
366889d51a1SNye Liu 		dir_add(collected, mtime);
3671da177e4SLinus Torvalds 	} else if (S_ISBLK(mode) || S_ISCHR(mode) ||
3681da177e4SLinus Torvalds 		   S_ISFIFO(mode) || S_ISSOCK(mode)) {
3691da177e4SLinus Torvalds 		if (maybe_link() == 0) {
3705fee64fcSChristoph Hellwig 			init_mknod(collected, mode, rdev);
371b873498fSChristoph Hellwig 			init_chown(collected, uid, gid, 0);
3721097742eSChristoph Hellwig 			init_chmod(collected, mode);
373889d51a1SNye Liu 			do_utime(collected, mtime);
3741da177e4SLinus Torvalds 		}
3751da177e4SLinus Torvalds 	}
3761da177e4SLinus Torvalds 	return 0;
3771da177e4SLinus Torvalds }
3781da177e4SLinus Torvalds 
3791da177e4SLinus Torvalds static int __init do_copy(void)
3801da177e4SLinus Torvalds {
381c34d85acSMark Rustad 	if (byte_count >= body_len) {
38238b08223SChristoph Hellwig 		struct timespec64 t[2] = { };
383bf6419e4SChristoph Hellwig 		if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len)
3849687fd91SDavid Engraf 			error("write error");
38538b08223SChristoph Hellwig 
38638b08223SChristoph Hellwig 		t[0].tv_sec = mtime;
38738b08223SChristoph Hellwig 		t[1].tv_sec = mtime;
38838b08223SChristoph Hellwig 		vfs_utimes(&wfile->f_path, t);
38938b08223SChristoph Hellwig 
390bf6419e4SChristoph Hellwig 		fput(wfile);
3911da177e4SLinus Torvalds 		eat(body_len);
3921da177e4SLinus Torvalds 		state = SkipIt;
3931da177e4SLinus Torvalds 		return 0;
3941da177e4SLinus Torvalds 	} else {
395bf6419e4SChristoph Hellwig 		if (xwrite(wfile, victim, byte_count, &wfile_pos) != byte_count)
3969687fd91SDavid Engraf 			error("write error");
397c34d85acSMark Rustad 		body_len -= byte_count;
398c34d85acSMark Rustad 		eat(byte_count);
3991da177e4SLinus Torvalds 		return 1;
4001da177e4SLinus Torvalds 	}
4011da177e4SLinus Torvalds }
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds static int __init do_symlink(void)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	collected[N_ALIGN(name_len) + body_len] = '\0';
4062139a7fbSH. Peter Anvin 	clean_path(collected, 0);
407cd3acb6aSChristoph Hellwig 	init_symlink(collected + N_ALIGN(name_len), collected);
408b873498fSChristoph Hellwig 	init_chown(collected, uid, gid, AT_SYMLINK_NOFOLLOW);
409889d51a1SNye Liu 	do_utime(collected, mtime);
4101da177e4SLinus Torvalds 	state = SkipIt;
4111da177e4SLinus Torvalds 	next_state = Reset;
4121da177e4SLinus Torvalds 	return 0;
4131da177e4SLinus Torvalds }
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds static __initdata int (*actions[])(void) = {
4161da177e4SLinus Torvalds 	[Start]		= do_start,
4171da177e4SLinus Torvalds 	[Collect]	= do_collect,
4181da177e4SLinus Torvalds 	[GotHeader]	= do_header,
4191da177e4SLinus Torvalds 	[SkipIt]	= do_skip,
4201da177e4SLinus Torvalds 	[GotName]	= do_name,
4211da177e4SLinus Torvalds 	[CopyFile]	= do_copy,
4221da177e4SLinus Torvalds 	[GotSymlink]	= do_symlink,
4231da177e4SLinus Torvalds 	[Reset]		= do_reset,
4241da177e4SLinus Torvalds };
4251da177e4SLinus Torvalds 
426d97b07c5SYinghai Lu static long __init write_buffer(char *buf, unsigned long len)
4271da177e4SLinus Torvalds {
428c34d85acSMark Rustad 	byte_count = len;
4291da177e4SLinus Torvalds 	victim = buf;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	while (!actions[state]())
4321da177e4SLinus Torvalds 		;
433c34d85acSMark Rustad 	return len - byte_count;
4341da177e4SLinus Torvalds }
4351da177e4SLinus Torvalds 
436d97b07c5SYinghai Lu static long __init flush_buffer(void *bufv, unsigned long len)
4371da177e4SLinus Torvalds {
43830d65dbfSAlain Knaff 	char *buf = (char *) bufv;
439d97b07c5SYinghai Lu 	long written;
440d97b07c5SYinghai Lu 	long origLen = len;
4411da177e4SLinus Torvalds 	if (message)
44230d65dbfSAlain Knaff 		return -1;
4431da177e4SLinus Torvalds 	while ((written = write_buffer(buf, len)) < len && !message) {
4441da177e4SLinus Torvalds 		char c = buf[written];
4451da177e4SLinus Torvalds 		if (c == '0') {
4461da177e4SLinus Torvalds 			buf += written;
4471da177e4SLinus Torvalds 			len -= written;
4481da177e4SLinus Torvalds 			state = Start;
4491da177e4SLinus Torvalds 		} else if (c == 0) {
4501da177e4SLinus Torvalds 			buf += written;
4511da177e4SLinus Torvalds 			len -= written;
4521da177e4SLinus Torvalds 			state = Reset;
4531da177e4SLinus Torvalds 		} else
454e5eed351SDavid Engraf 			error("junk within compressed archive");
4551da177e4SLinus Torvalds 	}
45630d65dbfSAlain Knaff 	return origLen;
4571da177e4SLinus Torvalds }
4581da177e4SLinus Torvalds 
459d97b07c5SYinghai Lu static unsigned long my_inptr; /* index of next byte to be processed in inbuf */
4601da177e4SLinus Torvalds 
461889c92d2SH. Peter Anvin #include <linux/decompress/generic.h>
4621da177e4SLinus Torvalds 
463d97b07c5SYinghai Lu static char * __init unpack_to_rootfs(char *buf, unsigned long len)
4641da177e4SLinus Torvalds {
465d97b07c5SYinghai Lu 	long written;
466889c92d2SH. Peter Anvin 	decompress_fn decompress;
46723a22d57SH. Peter Anvin 	const char *compress_name;
46823a22d57SH. Peter Anvin 	static __initdata char msg_buf[64];
469889c92d2SH. Peter Anvin 
4703265e66bSThomas Petazzoni 	header_buf = kmalloc(110, GFP_KERNEL);
4713265e66bSThomas Petazzoni 	symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
4723265e66bSThomas Petazzoni 	name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
47330d65dbfSAlain Knaff 
47430d65dbfSAlain Knaff 	if (!header_buf || !symlink_buf || !name_buf)
475dd23e809SFlorian Fainelli 		panic_show_mem("can't allocate buffers");
47630d65dbfSAlain Knaff 
4771da177e4SLinus Torvalds 	state = Start;
4781da177e4SLinus Torvalds 	this_header = 0;
4791da177e4SLinus Torvalds 	message = NULL;
4801da177e4SLinus Torvalds 	while (!message && len) {
4811da177e4SLinus Torvalds 		loff_t saved_offset = this_header;
4821da177e4SLinus Torvalds 		if (*buf == '0' && !(this_header & 3)) {
4831da177e4SLinus Torvalds 			state = Start;
4841da177e4SLinus Torvalds 			written = write_buffer(buf, len);
4851da177e4SLinus Torvalds 			buf += written;
4861da177e4SLinus Torvalds 			len -= written;
4871da177e4SLinus Torvalds 			continue;
4881da177e4SLinus Torvalds 		}
4891da177e4SLinus Torvalds 		if (!*buf) {
4901da177e4SLinus Torvalds 			buf++;
4911da177e4SLinus Torvalds 			len--;
4921da177e4SLinus Torvalds 			this_header++;
4931da177e4SLinus Torvalds 			continue;
4941da177e4SLinus Torvalds 		}
4951da177e4SLinus Torvalds 		this_header = 0;
49623a22d57SH. Peter Anvin 		decompress = decompress_method(buf, len, &compress_name);
4976aa7a29aSDaniel M. Weeks 		pr_debug("Detected %s compressed data\n", compress_name);
49854291362SPhillip Lougher 		if (decompress) {
499d97b07c5SYinghai Lu 			int res = decompress(buf, len, NULL, flush_buffer, NULL,
500889c92d2SH. Peter Anvin 				   &my_inptr, error);
50154291362SPhillip Lougher 			if (res)
50254291362SPhillip Lougher 				error("decompressor failed");
50354291362SPhillip Lougher 		} else if (compress_name) {
50423a22d57SH. Peter Anvin 			if (!message) {
50523a22d57SH. Peter Anvin 				snprintf(msg_buf, sizeof msg_buf,
50623a22d57SH. Peter Anvin 					 "compression method %s not configured",
50723a22d57SH. Peter Anvin 					 compress_name);
50823a22d57SH. Peter Anvin 				message = msg_buf;
50923a22d57SH. Peter Anvin 			}
510df37bd15SPhillip Lougher 		} else
511e5eed351SDavid Engraf 			error("invalid magic at start of compressed archive");
5121da177e4SLinus Torvalds 		if (state != Reset)
513e5eed351SDavid Engraf 			error("junk at the end of compressed archive");
51430d65dbfSAlain Knaff 		this_header = saved_offset + my_inptr;
51530d65dbfSAlain Knaff 		buf += my_inptr;
51630d65dbfSAlain Knaff 		len -= my_inptr;
5171da177e4SLinus Torvalds 	}
518889d51a1SNye Liu 	dir_utime();
5193265e66bSThomas Petazzoni 	kfree(name_buf);
5203265e66bSThomas Petazzoni 	kfree(symlink_buf);
5213265e66bSThomas Petazzoni 	kfree(header_buf);
5221da177e4SLinus Torvalds 	return message;
5231da177e4SLinus Torvalds }
5241da177e4SLinus Torvalds 
5250a7b35cbSMichael Neuling static int __initdata do_retain_initrd;
5260a7b35cbSMichael Neuling 
5270a7b35cbSMichael Neuling static int __init retain_initrd_param(char *str)
5280a7b35cbSMichael Neuling {
5290a7b35cbSMichael Neuling 	if (*str)
5300a7b35cbSMichael Neuling 		return 0;
5310a7b35cbSMichael Neuling 	do_retain_initrd = 1;
5320a7b35cbSMichael Neuling 	return 1;
5330a7b35cbSMichael Neuling }
5340a7b35cbSMichael Neuling __setup("retain_initrd", retain_initrd_param);
5350a7b35cbSMichael Neuling 
536d8ae8a37SChristoph Hellwig #ifdef CONFIG_ARCH_HAS_KEEPINITRD
537d8ae8a37SChristoph Hellwig static int __init keepinitrd_setup(char *__unused)
538d8ae8a37SChristoph Hellwig {
539d8ae8a37SChristoph Hellwig 	do_retain_initrd = 1;
540d8ae8a37SChristoph Hellwig 	return 1;
541d8ae8a37SChristoph Hellwig }
542d8ae8a37SChristoph Hellwig __setup("keepinitrd", keepinitrd_setup);
543d8ae8a37SChristoph Hellwig #endif
544d8ae8a37SChristoph Hellwig 
545e7cb072eSRasmus Villemoes static bool __initdata initramfs_async = true;
546e7cb072eSRasmus Villemoes static int __init initramfs_async_setup(char *str)
547e7cb072eSRasmus Villemoes {
548e7cb072eSRasmus Villemoes 	strtobool(str, &initramfs_async);
549e7cb072eSRasmus Villemoes 	return 1;
550e7cb072eSRasmus Villemoes }
551e7cb072eSRasmus Villemoes __setup("initramfs_async=", initramfs_async_setup);
552e7cb072eSRasmus Villemoes 
553ffe8018cSHendrik Brueckner extern char __initramfs_start[];
554ffe8018cSHendrik Brueckner extern unsigned long __initramfs_size;
5551da177e4SLinus Torvalds #include <linux/initrd.h>
5569c15e852SHaren Myneni #include <linux/kexec.h>
5570f3d2bd5SJan Beulich 
558c72160feSKefeng Wang void __init reserve_initrd_mem(void)
559c72160feSKefeng Wang {
560c72160feSKefeng Wang 	phys_addr_t start;
561c72160feSKefeng Wang 	unsigned long size;
562c72160feSKefeng Wang 
563c72160feSKefeng Wang 	/* Ignore the virtul address computed during device tree parsing */
564c72160feSKefeng Wang 	initrd_start = initrd_end = 0;
565c72160feSKefeng Wang 
566c72160feSKefeng Wang 	if (!phys_initrd_size)
567c72160feSKefeng Wang 		return;
568c72160feSKefeng Wang 	/*
569c72160feSKefeng Wang 	 * Round the memory region to page boundaries as per free_initrd_mem()
570c72160feSKefeng Wang 	 * This allows us to detect whether the pages overlapping the initrd
571c72160feSKefeng Wang 	 * are in use, but more importantly, reserves the entire set of pages
572c72160feSKefeng Wang 	 * as we don't want these pages allocated for other purposes.
573c72160feSKefeng Wang 	 */
574c72160feSKefeng Wang 	start = round_down(phys_initrd_start, PAGE_SIZE);
575c72160feSKefeng Wang 	size = phys_initrd_size + (phys_initrd_start - start);
576c72160feSKefeng Wang 	size = round_up(size, PAGE_SIZE);
577c72160feSKefeng Wang 
578c72160feSKefeng Wang 	if (!memblock_is_region_memory(start, size)) {
579c72160feSKefeng Wang 		pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region",
580c72160feSKefeng Wang 		       (u64)start, size);
581c72160feSKefeng Wang 		goto disable;
582c72160feSKefeng Wang 	}
583c72160feSKefeng Wang 
584c72160feSKefeng Wang 	if (memblock_is_region_reserved(start, size)) {
585c72160feSKefeng Wang 		pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region\n",
586c72160feSKefeng Wang 		       (u64)start, size);
587c72160feSKefeng Wang 		goto disable;
588c72160feSKefeng Wang 	}
589c72160feSKefeng Wang 
590c72160feSKefeng Wang 	memblock_reserve(start, size);
591c72160feSKefeng Wang 	/* Now convert initrd to virtual addresses */
592c72160feSKefeng Wang 	initrd_start = (unsigned long)__va(phys_initrd_start);
593c72160feSKefeng Wang 	initrd_end = initrd_start + phys_initrd_size;
594c72160feSKefeng Wang 	initrd_below_start_ok = 1;
595c72160feSKefeng Wang 
596c72160feSKefeng Wang 	return;
597c72160feSKefeng Wang disable:
598c72160feSKefeng Wang 	pr_cont(" - disabling initrd\n");
599c72160feSKefeng Wang 	initrd_start = 0;
600c72160feSKefeng Wang 	initrd_end = 0;
601c72160feSKefeng Wang }
602c72160feSKefeng Wang 
60355d5b7ddSArnd Bergmann void __weak __init free_initrd_mem(unsigned long start, unsigned long end)
6044afd58e1SChristoph Hellwig {
605899ee4afSMike Rapoport #ifdef CONFIG_ARCH_KEEP_MEMBLOCK
606899ee4afSMike Rapoport 	unsigned long aligned_start = ALIGN_DOWN(start, PAGE_SIZE);
607899ee4afSMike Rapoport 	unsigned long aligned_end = ALIGN(end, PAGE_SIZE);
608899ee4afSMike Rapoport 
6094421cca0SMike Rapoport 	memblock_free((void *)aligned_start, aligned_end - aligned_start);
610899ee4afSMike Rapoport #endif
611899ee4afSMike Rapoport 
612f94f7434SChristoph Hellwig 	free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM,
613f94f7434SChristoph Hellwig 			"initrd");
6144afd58e1SChristoph Hellwig }
6154afd58e1SChristoph Hellwig 
6162965faa5SDave Young #ifdef CONFIG_KEXEC_CORE
617e99332e7SLinus Torvalds static bool __init kexec_free_initrd(void)
61823091e28SChristoph Hellwig {
6199c15e852SHaren Myneni 	unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
6209c15e852SHaren Myneni 	unsigned long crashk_end   = (unsigned long)__va(crashk_res.end);
6219c15e852SHaren Myneni 
6229c15e852SHaren Myneni 	/*
6239c15e852SHaren Myneni 	 * If the initrd region is overlapped with crashkernel reserved region,
6249c15e852SHaren Myneni 	 * free only memory that is not part of crashkernel region.
6259c15e852SHaren Myneni 	 */
62623091e28SChristoph Hellwig 	if (initrd_start >= crashk_end || initrd_end <= crashk_start)
62723091e28SChristoph Hellwig 		return false;
62823091e28SChristoph Hellwig 
6299c15e852SHaren Myneni 	/*
63023091e28SChristoph Hellwig 	 * Initialize initrd memory region since the kexec boot does not do.
6319c15e852SHaren Myneni 	 */
6329c15e852SHaren Myneni 	memset((void *)initrd_start, 0, initrd_end - initrd_start);
6339c15e852SHaren Myneni 	if (initrd_start < crashk_start)
6349c15e852SHaren Myneni 		free_initrd_mem(initrd_start, crashk_start);
6359c15e852SHaren Myneni 	if (initrd_end > crashk_end)
6369c15e852SHaren Myneni 		free_initrd_mem(crashk_end, initrd_end);
63723091e28SChristoph Hellwig 	return true;
6380f3d2bd5SJan Beulich }
63923091e28SChristoph Hellwig #else
64023091e28SChristoph Hellwig static inline bool kexec_free_initrd(void)
64123091e28SChristoph Hellwig {
64223091e28SChristoph Hellwig 	return false;
64323091e28SChristoph Hellwig }
64423091e28SChristoph Hellwig #endif /* CONFIG_KEXEC_CORE */
6450f3d2bd5SJan Beulich 
646a841c673SAndrew Morton #ifdef CONFIG_BLK_DEV_RAM
6474ada1e81SGeert Uytterhoeven static void __init populate_initrd_image(char *err)
6487c184ecdSChristoph Hellwig {
6497c184ecdSChristoph Hellwig 	ssize_t written;
650bf6419e4SChristoph Hellwig 	struct file *file;
651bf6419e4SChristoph Hellwig 	loff_t pos = 0;
6527c184ecdSChristoph Hellwig 
6537c184ecdSChristoph Hellwig 	unpack_to_rootfs(__initramfs_start, __initramfs_size);
6547c184ecdSChristoph Hellwig 
6557c184ecdSChristoph Hellwig 	printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n",
6567c184ecdSChristoph Hellwig 			err);
657bf6419e4SChristoph Hellwig 	file = filp_open("/initrd.image", O_WRONLY | O_CREAT, 0700);
658bf6419e4SChristoph Hellwig 	if (IS_ERR(file))
6597c184ecdSChristoph Hellwig 		return;
6607c184ecdSChristoph Hellwig 
661bf6419e4SChristoph Hellwig 	written = xwrite(file, (char *)initrd_start, initrd_end - initrd_start,
662bf6419e4SChristoph Hellwig 			&pos);
6637c184ecdSChristoph Hellwig 	if (written != initrd_end - initrd_start)
6647c184ecdSChristoph Hellwig 		pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
6657c184ecdSChristoph Hellwig 		       written, initrd_end - initrd_start);
666bf6419e4SChristoph Hellwig 	fput(file);
6677c184ecdSChristoph Hellwig }
6687c184ecdSChristoph Hellwig #endif /* CONFIG_BLK_DEV_RAM */
6697c184ecdSChristoph Hellwig 
670e7cb072eSRasmus Villemoes static void __init do_populate_rootfs(void *unused, async_cookie_t cookie)
6711da177e4SLinus Torvalds {
67217a9be31SStafford Horne 	/* Load the built in initramfs */
673ffe8018cSHendrik Brueckner 	char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
6741da177e4SLinus Torvalds 	if (err)
675dd23e809SFlorian Fainelli 		panic_show_mem("%s", err); /* Failed to decompress INTERNAL initramfs */
676afef7889SChristoph Hellwig 
677afef7889SChristoph Hellwig 	if (!initrd_start || IS_ENABLED(CONFIG_INITRAMFS_FORCE))
678bb813f4cSTejun Heo 		goto done;
67954c7a891SChristoph Hellwig 
680afef7889SChristoph Hellwig 	if (IS_ENABLED(CONFIG_BLK_DEV_RAM))
681afef7889SChristoph Hellwig 		printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
682afef7889SChristoph Hellwig 	else
683afef7889SChristoph Hellwig 		printk(KERN_INFO "Unpacking initramfs...\n");
684afef7889SChristoph Hellwig 
685afef7889SChristoph Hellwig 	err = unpack_to_rootfs((char *)initrd_start, initrd_end - initrd_start);
686afef7889SChristoph Hellwig 	if (err) {
6879ab6b718SChristoph Hellwig #ifdef CONFIG_BLK_DEV_RAM
6887c184ecdSChristoph Hellwig 		populate_initrd_image(err);
6899ab6b718SChristoph Hellwig #else
6909ab6b718SChristoph Hellwig 		printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
6919ab6b718SChristoph Hellwig #endif
69217a9be31SStafford Horne 	}
69323091e28SChristoph Hellwig 
694afef7889SChristoph Hellwig done:
69523091e28SChristoph Hellwig 	/*
69623091e28SChristoph Hellwig 	 * If the initrd region is overlapped with crashkernel reserved region,
69723091e28SChristoph Hellwig 	 * free only memory that is not part of crashkernel region.
69823091e28SChristoph Hellwig 	 */
6995d59aa8fSSteven Price 	if (!do_retain_initrd && initrd_start && !kexec_free_initrd())
70023091e28SChristoph Hellwig 		free_initrd_mem(initrd_start, initrd_end);
70123091e28SChristoph Hellwig 	initrd_start = 0;
70223091e28SChristoph Hellwig 	initrd_end = 0;
70323091e28SChristoph Hellwig 
70408865514SLokesh Vutla 	flush_delayed_fput();
705e7cb072eSRasmus Villemoes }
706e7cb072eSRasmus Villemoes 
707e7cb072eSRasmus Villemoes static ASYNC_DOMAIN_EXCLUSIVE(initramfs_domain);
708e7cb072eSRasmus Villemoes static async_cookie_t initramfs_cookie;
709e7cb072eSRasmus Villemoes 
710e7cb072eSRasmus Villemoes void wait_for_initramfs(void)
711e7cb072eSRasmus Villemoes {
712e7cb072eSRasmus Villemoes 	if (!initramfs_cookie) {
713e7cb072eSRasmus Villemoes 		/*
714e7cb072eSRasmus Villemoes 		 * Something before rootfs_initcall wants to access
715e7cb072eSRasmus Villemoes 		 * the filesystem/initramfs. Probably a bug. Make a
716e7cb072eSRasmus Villemoes 		 * note, avoid deadlocking the machine, and let the
717e7cb072eSRasmus Villemoes 		 * caller's access fail as it used to.
718e7cb072eSRasmus Villemoes 		 */
719e7cb072eSRasmus Villemoes 		pr_warn_once("wait_for_initramfs() called before rootfs_initcalls\n");
720e7cb072eSRasmus Villemoes 		return;
721e7cb072eSRasmus Villemoes 	}
722e7cb072eSRasmus Villemoes 	async_synchronize_cookie_domain(initramfs_cookie + 1, &initramfs_domain);
723e7cb072eSRasmus Villemoes }
724e7cb072eSRasmus Villemoes EXPORT_SYMBOL_GPL(wait_for_initramfs);
725e7cb072eSRasmus Villemoes 
726e7cb072eSRasmus Villemoes static int __init populate_rootfs(void)
727e7cb072eSRasmus Villemoes {
728e7cb072eSRasmus Villemoes 	initramfs_cookie = async_schedule_domain(do_populate_rootfs, NULL,
729e7cb072eSRasmus Villemoes 						 &initramfs_domain);
730b234ed6dSRasmus Villemoes 	usermodehelper_enable();
731e7cb072eSRasmus Villemoes 	if (!initramfs_async)
732e7cb072eSRasmus Villemoes 		wait_for_initramfs();
7338d610dd5SLinus Torvalds 	return 0;
7341da177e4SLinus Torvalds }
7358d610dd5SLinus Torvalds rootfs_initcall(populate_rootfs);
736