xref: /openbmc/linux/init/initramfs.c (revision 800c24dc)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/init.h>
3 #include <linux/async.h>
4 #include <linux/fs.h>
5 #include <linux/slab.h>
6 #include <linux/types.h>
7 #include <linux/fcntl.h>
8 #include <linux/delay.h>
9 #include <linux/string.h>
10 #include <linux/dirent.h>
11 #include <linux/syscalls.h>
12 #include <linux/utime.h>
13 #include <linux/file.h>
14 #include <linux/memblock.h>
15 #include <linux/mm.h>
16 #include <linux/namei.h>
17 #include <linux/init_syscalls.h>
18 #include <linux/umh.h>
19 
20 static __initdata bool csum_present;
21 static __initdata u32 io_csum;
22 
23 static ssize_t __init xwrite(struct file *file, const unsigned char *p,
24 		size_t count, loff_t *pos)
25 {
26 	ssize_t out = 0;
27 
28 	/* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
29 	while (count) {
30 		ssize_t rv = kernel_write(file, p, count, pos);
31 
32 		if (rv < 0) {
33 			if (rv == -EINTR || rv == -EAGAIN)
34 				continue;
35 			return out ? out : rv;
36 		} else if (rv == 0)
37 			break;
38 
39 		if (csum_present) {
40 			ssize_t i;
41 
42 			for (i = 0; i < rv; i++)
43 				io_csum += p[i];
44 		}
45 
46 		p += rv;
47 		out += rv;
48 		count -= rv;
49 	}
50 
51 	return out;
52 }
53 
54 static __initdata char *message;
55 static void __init error(char *x)
56 {
57 	if (!message)
58 		message = x;
59 }
60 
61 static void panic_show_mem(const char *fmt, ...)
62 {
63 	va_list args;
64 
65 	show_mem(0, NULL);
66 	va_start(args, fmt);
67 	panic(fmt, args);
68 	va_end(args);
69 }
70 
71 /* link hash */
72 
73 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
74 
75 static __initdata struct hash {
76 	int ino, minor, major;
77 	umode_t mode;
78 	struct hash *next;
79 	char name[N_ALIGN(PATH_MAX)];
80 } *head[32];
81 
82 static inline int hash(int major, int minor, int ino)
83 {
84 	unsigned long tmp = ino + minor + (major << 3);
85 	tmp += tmp >> 5;
86 	return tmp & 31;
87 }
88 
89 static char __init *find_link(int major, int minor, int ino,
90 			      umode_t mode, char *name)
91 {
92 	struct hash **p, *q;
93 	for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
94 		if ((*p)->ino != ino)
95 			continue;
96 		if ((*p)->minor != minor)
97 			continue;
98 		if ((*p)->major != major)
99 			continue;
100 		if (((*p)->mode ^ mode) & S_IFMT)
101 			continue;
102 		return (*p)->name;
103 	}
104 	q = kmalloc(sizeof(struct hash), GFP_KERNEL);
105 	if (!q)
106 		panic_show_mem("can't allocate link hash entry");
107 	q->major = major;
108 	q->minor = minor;
109 	q->ino = ino;
110 	q->mode = mode;
111 	strcpy(q->name, name);
112 	q->next = NULL;
113 	*p = q;
114 	return NULL;
115 }
116 
117 static void __init free_hash(void)
118 {
119 	struct hash **p, *q;
120 	for (p = head; p < head + 32; p++) {
121 		while (*p) {
122 			q = *p;
123 			*p = q->next;
124 			kfree(q);
125 		}
126 	}
127 }
128 
129 #ifdef CONFIG_INITRAMFS_PRESERVE_MTIME
130 static void __init do_utime(char *filename, time64_t mtime)
131 {
132 	struct timespec64 t[2] = { { .tv_sec = mtime }, { .tv_sec = mtime } };
133 	init_utimes(filename, t);
134 }
135 
136 static void __init do_utime_path(const struct path *path, time64_t mtime)
137 {
138 	struct timespec64 t[2] = { { .tv_sec = mtime }, { .tv_sec = mtime } };
139 	vfs_utimes(path, t);
140 }
141 
142 static __initdata LIST_HEAD(dir_list);
143 struct dir_entry {
144 	struct list_head list;
145 	time64_t mtime;
146 	char name[];
147 };
148 
149 static void __init dir_add(const char *name, time64_t mtime)
150 {
151 	size_t nlen = strlen(name) + 1;
152 	struct dir_entry *de;
153 
154 	de = kmalloc(sizeof(struct dir_entry) + nlen, GFP_KERNEL);
155 	if (!de)
156 		panic_show_mem("can't allocate dir_entry buffer");
157 	INIT_LIST_HEAD(&de->list);
158 	strscpy(de->name, name, nlen);
159 	de->mtime = mtime;
160 	list_add(&de->list, &dir_list);
161 }
162 
163 static void __init dir_utime(void)
164 {
165 	struct dir_entry *de, *tmp;
166 	list_for_each_entry_safe(de, tmp, &dir_list, list) {
167 		list_del(&de->list);
168 		do_utime(de->name, de->mtime);
169 		kfree(de);
170 	}
171 }
172 #else
173 static void __init do_utime(char *filename, time64_t mtime) {}
174 static void __init do_utime_path(const struct path *path, time64_t mtime) {}
175 static void __init dir_add(const char *name, time64_t mtime) {}
176 static void __init dir_utime(void) {}
177 #endif
178 
179 static __initdata time64_t mtime;
180 
181 /* cpio header parsing */
182 
183 static __initdata unsigned long ino, major, minor, nlink;
184 static __initdata umode_t mode;
185 static __initdata unsigned long body_len, name_len;
186 static __initdata uid_t uid;
187 static __initdata gid_t gid;
188 static __initdata unsigned rdev;
189 static __initdata u32 hdr_csum;
190 
191 static void __init parse_header(char *s)
192 {
193 	unsigned long parsed[13];
194 	char buf[9];
195 	int i;
196 
197 	buf[8] = '\0';
198 	for (i = 0, s += 6; i < 13; i++, s += 8) {
199 		memcpy(buf, s, 8);
200 		parsed[i] = simple_strtoul(buf, NULL, 16);
201 	}
202 	ino = parsed[0];
203 	mode = parsed[1];
204 	uid = parsed[2];
205 	gid = parsed[3];
206 	nlink = parsed[4];
207 	mtime = parsed[5]; /* breaks in y2106 */
208 	body_len = parsed[6];
209 	major = parsed[7];
210 	minor = parsed[8];
211 	rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
212 	name_len = parsed[11];
213 	hdr_csum = parsed[12];
214 }
215 
216 /* FSM */
217 
218 static __initdata enum state {
219 	Start,
220 	Collect,
221 	GotHeader,
222 	SkipIt,
223 	GotName,
224 	CopyFile,
225 	GotSymlink,
226 	Reset
227 } state, next_state;
228 
229 static __initdata char *victim;
230 static unsigned long byte_count __initdata;
231 static __initdata loff_t this_header, next_header;
232 
233 static inline void __init eat(unsigned n)
234 {
235 	victim += n;
236 	this_header += n;
237 	byte_count -= n;
238 }
239 
240 static __initdata char *collected;
241 static long remains __initdata;
242 static __initdata char *collect;
243 
244 static void __init read_into(char *buf, unsigned size, enum state next)
245 {
246 	if (byte_count >= size) {
247 		collected = victim;
248 		eat(size);
249 		state = next;
250 	} else {
251 		collect = collected = buf;
252 		remains = size;
253 		next_state = next;
254 		state = Collect;
255 	}
256 }
257 
258 static __initdata char *header_buf, *symlink_buf, *name_buf;
259 
260 static int __init do_start(void)
261 {
262 	read_into(header_buf, 110, GotHeader);
263 	return 0;
264 }
265 
266 static int __init do_collect(void)
267 {
268 	unsigned long n = remains;
269 	if (byte_count < n)
270 		n = byte_count;
271 	memcpy(collect, victim, n);
272 	eat(n);
273 	collect += n;
274 	if ((remains -= n) != 0)
275 		return 1;
276 	state = next_state;
277 	return 0;
278 }
279 
280 static int __init do_header(void)
281 {
282 	if (!memcmp(collected, "070701", 6)) {
283 		csum_present = false;
284 	} else if (!memcmp(collected, "070702", 6)) {
285 		csum_present = true;
286 	} else {
287 		if (memcmp(collected, "070707", 6) == 0)
288 			error("incorrect cpio method used: use -H newc option");
289 		else
290 			error("no cpio magic");
291 		return 1;
292 	}
293 	parse_header(collected);
294 	next_header = this_header + N_ALIGN(name_len) + body_len;
295 	next_header = (next_header + 3) & ~3;
296 	state = SkipIt;
297 	if (name_len <= 0 || name_len > PATH_MAX)
298 		return 0;
299 	if (S_ISLNK(mode)) {
300 		if (body_len > PATH_MAX)
301 			return 0;
302 		collect = collected = symlink_buf;
303 		remains = N_ALIGN(name_len) + body_len;
304 		next_state = GotSymlink;
305 		state = Collect;
306 		return 0;
307 	}
308 	if (S_ISREG(mode) || !body_len)
309 		read_into(name_buf, N_ALIGN(name_len), GotName);
310 	return 0;
311 }
312 
313 static int __init do_skip(void)
314 {
315 	if (this_header + byte_count < next_header) {
316 		eat(byte_count);
317 		return 1;
318 	} else {
319 		eat(next_header - this_header);
320 		state = next_state;
321 		return 0;
322 	}
323 }
324 
325 static int __init do_reset(void)
326 {
327 	while (byte_count && *victim == '\0')
328 		eat(1);
329 	if (byte_count && (this_header & 3))
330 		error("broken padding");
331 	return 1;
332 }
333 
334 static void __init clean_path(char *path, umode_t fmode)
335 {
336 	struct kstat st;
337 
338 	if (!init_stat(path, &st, AT_SYMLINK_NOFOLLOW) &&
339 	    (st.mode ^ fmode) & S_IFMT) {
340 		if (S_ISDIR(st.mode))
341 			init_rmdir(path);
342 		else
343 			init_unlink(path);
344 	}
345 }
346 
347 static int __init maybe_link(void)
348 {
349 	if (nlink >= 2) {
350 		char *old = find_link(major, minor, ino, mode, collected);
351 		if (old) {
352 			clean_path(collected, 0);
353 			return (init_link(old, collected) < 0) ? -1 : 1;
354 		}
355 	}
356 	return 0;
357 }
358 
359 static __initdata struct file *wfile;
360 static __initdata loff_t wfile_pos;
361 
362 static int __init do_name(void)
363 {
364 	state = SkipIt;
365 	next_state = Reset;
366 	if (strcmp(collected, "TRAILER!!!") == 0) {
367 		free_hash();
368 		return 0;
369 	}
370 	clean_path(collected, mode);
371 	if (S_ISREG(mode)) {
372 		int ml = maybe_link();
373 		if (ml >= 0) {
374 			int openflags = O_WRONLY|O_CREAT;
375 			if (ml != 1)
376 				openflags |= O_TRUNC;
377 			wfile = filp_open(collected, openflags, mode);
378 			if (IS_ERR(wfile))
379 				return 0;
380 			wfile_pos = 0;
381 			io_csum = 0;
382 
383 			vfs_fchown(wfile, uid, gid);
384 			vfs_fchmod(wfile, mode);
385 			if (body_len)
386 				vfs_truncate(&wfile->f_path, body_len);
387 			state = CopyFile;
388 		}
389 	} else if (S_ISDIR(mode)) {
390 		init_mkdir(collected, mode);
391 		init_chown(collected, uid, gid, 0);
392 		init_chmod(collected, mode);
393 		dir_add(collected, mtime);
394 	} else if (S_ISBLK(mode) || S_ISCHR(mode) ||
395 		   S_ISFIFO(mode) || S_ISSOCK(mode)) {
396 		if (maybe_link() == 0) {
397 			init_mknod(collected, mode, rdev);
398 			init_chown(collected, uid, gid, 0);
399 			init_chmod(collected, mode);
400 			do_utime(collected, mtime);
401 		}
402 	}
403 	return 0;
404 }
405 
406 static int __init do_copy(void)
407 {
408 	if (byte_count >= body_len) {
409 		if (xwrite(wfile, victim, body_len, &wfile_pos) != body_len)
410 			error("write error");
411 
412 		do_utime_path(&wfile->f_path, mtime);
413 		fput(wfile);
414 		if (csum_present && io_csum != hdr_csum)
415 			error("bad data checksum");
416 		eat(body_len);
417 		state = SkipIt;
418 		return 0;
419 	} else {
420 		if (xwrite(wfile, victim, byte_count, &wfile_pos) != byte_count)
421 			error("write error");
422 		body_len -= byte_count;
423 		eat(byte_count);
424 		return 1;
425 	}
426 }
427 
428 static int __init do_symlink(void)
429 {
430 	collected[N_ALIGN(name_len) + body_len] = '\0';
431 	clean_path(collected, 0);
432 	init_symlink(collected + N_ALIGN(name_len), collected);
433 	init_chown(collected, uid, gid, AT_SYMLINK_NOFOLLOW);
434 	do_utime(collected, mtime);
435 	state = SkipIt;
436 	next_state = Reset;
437 	return 0;
438 }
439 
440 static __initdata int (*actions[])(void) = {
441 	[Start]		= do_start,
442 	[Collect]	= do_collect,
443 	[GotHeader]	= do_header,
444 	[SkipIt]	= do_skip,
445 	[GotName]	= do_name,
446 	[CopyFile]	= do_copy,
447 	[GotSymlink]	= do_symlink,
448 	[Reset]		= do_reset,
449 };
450 
451 static long __init write_buffer(char *buf, unsigned long len)
452 {
453 	byte_count = len;
454 	victim = buf;
455 
456 	while (!actions[state]())
457 		;
458 	return len - byte_count;
459 }
460 
461 static long __init flush_buffer(void *bufv, unsigned long len)
462 {
463 	char *buf = (char *) bufv;
464 	long written;
465 	long origLen = len;
466 	if (message)
467 		return -1;
468 	while ((written = write_buffer(buf, len)) < len && !message) {
469 		char c = buf[written];
470 		if (c == '0') {
471 			buf += written;
472 			len -= written;
473 			state = Start;
474 		} else if (c == 0) {
475 			buf += written;
476 			len -= written;
477 			state = Reset;
478 		} else
479 			error("junk within compressed archive");
480 	}
481 	return origLen;
482 }
483 
484 static unsigned long my_inptr; /* index of next byte to be processed in inbuf */
485 
486 #include <linux/decompress/generic.h>
487 
488 static char * __init unpack_to_rootfs(char *buf, unsigned long len)
489 {
490 	long written;
491 	decompress_fn decompress;
492 	const char *compress_name;
493 	static __initdata char msg_buf[64];
494 
495 	header_buf = kmalloc(110, GFP_KERNEL);
496 	symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
497 	name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
498 
499 	if (!header_buf || !symlink_buf || !name_buf)
500 		panic_show_mem("can't allocate buffers");
501 
502 	state = Start;
503 	this_header = 0;
504 	message = NULL;
505 	while (!message && len) {
506 		loff_t saved_offset = this_header;
507 		if (*buf == '0' && !(this_header & 3)) {
508 			state = Start;
509 			written = write_buffer(buf, len);
510 			buf += written;
511 			len -= written;
512 			continue;
513 		}
514 		if (!*buf) {
515 			buf++;
516 			len--;
517 			this_header++;
518 			continue;
519 		}
520 		this_header = 0;
521 		decompress = decompress_method(buf, len, &compress_name);
522 		pr_debug("Detected %s compressed data\n", compress_name);
523 		if (decompress) {
524 			int res = decompress(buf, len, NULL, flush_buffer, NULL,
525 				   &my_inptr, error);
526 			if (res)
527 				error("decompressor failed");
528 		} else if (compress_name) {
529 			if (!message) {
530 				snprintf(msg_buf, sizeof msg_buf,
531 					 "compression method %s not configured",
532 					 compress_name);
533 				message = msg_buf;
534 			}
535 		} else
536 			error("invalid magic at start of compressed archive");
537 		if (state != Reset)
538 			error("junk at the end of compressed archive");
539 		this_header = saved_offset + my_inptr;
540 		buf += my_inptr;
541 		len -= my_inptr;
542 	}
543 	dir_utime();
544 	kfree(name_buf);
545 	kfree(symlink_buf);
546 	kfree(header_buf);
547 	return message;
548 }
549 
550 static int __initdata do_retain_initrd;
551 
552 static int __init retain_initrd_param(char *str)
553 {
554 	if (*str)
555 		return 0;
556 	do_retain_initrd = 1;
557 	return 1;
558 }
559 __setup("retain_initrd", retain_initrd_param);
560 
561 #ifdef CONFIG_ARCH_HAS_KEEPINITRD
562 static int __init keepinitrd_setup(char *__unused)
563 {
564 	do_retain_initrd = 1;
565 	return 1;
566 }
567 __setup("keepinitrd", keepinitrd_setup);
568 #endif
569 
570 static bool __initdata initramfs_async = true;
571 static int __init initramfs_async_setup(char *str)
572 {
573 	strtobool(str, &initramfs_async);
574 	return 1;
575 }
576 __setup("initramfs_async=", initramfs_async_setup);
577 
578 extern char __initramfs_start[];
579 extern unsigned long __initramfs_size;
580 #include <linux/initrd.h>
581 #include <linux/kexec.h>
582 
583 void __init reserve_initrd_mem(void)
584 {
585 	phys_addr_t start;
586 	unsigned long size;
587 
588 	/* Ignore the virtul address computed during device tree parsing */
589 	initrd_start = initrd_end = 0;
590 
591 	if (!phys_initrd_size)
592 		return;
593 	/*
594 	 * Round the memory region to page boundaries as per free_initrd_mem()
595 	 * This allows us to detect whether the pages overlapping the initrd
596 	 * are in use, but more importantly, reserves the entire set of pages
597 	 * as we don't want these pages allocated for other purposes.
598 	 */
599 	start = round_down(phys_initrd_start, PAGE_SIZE);
600 	size = phys_initrd_size + (phys_initrd_start - start);
601 	size = round_up(size, PAGE_SIZE);
602 
603 	if (!memblock_is_region_memory(start, size)) {
604 		pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region",
605 		       (u64)start, size);
606 		goto disable;
607 	}
608 
609 	if (memblock_is_region_reserved(start, size)) {
610 		pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region\n",
611 		       (u64)start, size);
612 		goto disable;
613 	}
614 
615 	memblock_reserve(start, size);
616 	/* Now convert initrd to virtual addresses */
617 	initrd_start = (unsigned long)__va(phys_initrd_start);
618 	initrd_end = initrd_start + phys_initrd_size;
619 	initrd_below_start_ok = 1;
620 
621 	return;
622 disable:
623 	pr_cont(" - disabling initrd\n");
624 	initrd_start = 0;
625 	initrd_end = 0;
626 }
627 
628 void __weak __init free_initrd_mem(unsigned long start, unsigned long end)
629 {
630 #ifdef CONFIG_ARCH_KEEP_MEMBLOCK
631 	unsigned long aligned_start = ALIGN_DOWN(start, PAGE_SIZE);
632 	unsigned long aligned_end = ALIGN(end, PAGE_SIZE);
633 
634 	memblock_free((void *)aligned_start, aligned_end - aligned_start);
635 #endif
636 
637 	free_reserved_area((void *)start, (void *)end, POISON_FREE_INITMEM,
638 			"initrd");
639 }
640 
641 #ifdef CONFIG_KEXEC_CORE
642 static bool __init kexec_free_initrd(void)
643 {
644 	unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
645 	unsigned long crashk_end   = (unsigned long)__va(crashk_res.end);
646 
647 	/*
648 	 * If the initrd region is overlapped with crashkernel reserved region,
649 	 * free only memory that is not part of crashkernel region.
650 	 */
651 	if (initrd_start >= crashk_end || initrd_end <= crashk_start)
652 		return false;
653 
654 	/*
655 	 * Initialize initrd memory region since the kexec boot does not do.
656 	 */
657 	memset((void *)initrd_start, 0, initrd_end - initrd_start);
658 	if (initrd_start < crashk_start)
659 		free_initrd_mem(initrd_start, crashk_start);
660 	if (initrd_end > crashk_end)
661 		free_initrd_mem(crashk_end, initrd_end);
662 	return true;
663 }
664 #else
665 static inline bool kexec_free_initrd(void)
666 {
667 	return false;
668 }
669 #endif /* CONFIG_KEXEC_CORE */
670 
671 #ifdef CONFIG_BLK_DEV_RAM
672 static void __init populate_initrd_image(char *err)
673 {
674 	ssize_t written;
675 	struct file *file;
676 	loff_t pos = 0;
677 
678 	unpack_to_rootfs(__initramfs_start, __initramfs_size);
679 
680 	printk(KERN_INFO "rootfs image is not initramfs (%s); looks like an initrd\n",
681 			err);
682 	file = filp_open("/initrd.image", O_WRONLY | O_CREAT, 0700);
683 	if (IS_ERR(file))
684 		return;
685 
686 	written = xwrite(file, (char *)initrd_start, initrd_end - initrd_start,
687 			&pos);
688 	if (written != initrd_end - initrd_start)
689 		pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
690 		       written, initrd_end - initrd_start);
691 	fput(file);
692 }
693 #endif /* CONFIG_BLK_DEV_RAM */
694 
695 static void __init do_populate_rootfs(void *unused, async_cookie_t cookie)
696 {
697 	/* Load the built in initramfs */
698 	char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
699 	if (err)
700 		panic_show_mem("%s", err); /* Failed to decompress INTERNAL initramfs */
701 
702 	if (!initrd_start || IS_ENABLED(CONFIG_INITRAMFS_FORCE))
703 		goto done;
704 
705 	if (IS_ENABLED(CONFIG_BLK_DEV_RAM))
706 		printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
707 	else
708 		printk(KERN_INFO "Unpacking initramfs...\n");
709 
710 	err = unpack_to_rootfs((char *)initrd_start, initrd_end - initrd_start);
711 	if (err) {
712 #ifdef CONFIG_BLK_DEV_RAM
713 		populate_initrd_image(err);
714 #else
715 		printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
716 #endif
717 	}
718 
719 done:
720 	/*
721 	 * If the initrd region is overlapped with crashkernel reserved region,
722 	 * free only memory that is not part of crashkernel region.
723 	 */
724 	if (!do_retain_initrd && initrd_start && !kexec_free_initrd())
725 		free_initrd_mem(initrd_start, initrd_end);
726 	initrd_start = 0;
727 	initrd_end = 0;
728 
729 	flush_delayed_fput();
730 }
731 
732 static ASYNC_DOMAIN_EXCLUSIVE(initramfs_domain);
733 static async_cookie_t initramfs_cookie;
734 
735 void wait_for_initramfs(void)
736 {
737 	if (!initramfs_cookie) {
738 		/*
739 		 * Something before rootfs_initcall wants to access
740 		 * the filesystem/initramfs. Probably a bug. Make a
741 		 * note, avoid deadlocking the machine, and let the
742 		 * caller's access fail as it used to.
743 		 */
744 		pr_warn_once("wait_for_initramfs() called before rootfs_initcalls\n");
745 		return;
746 	}
747 	async_synchronize_cookie_domain(initramfs_cookie + 1, &initramfs_domain);
748 }
749 EXPORT_SYMBOL_GPL(wait_for_initramfs);
750 
751 static int __init populate_rootfs(void)
752 {
753 	initramfs_cookie = async_schedule_domain(do_populate_rootfs, NULL,
754 						 &initramfs_domain);
755 	usermodehelper_enable();
756 	if (!initramfs_async)
757 		wait_for_initramfs();
758 	return 0;
759 }
760 rootfs_initcall(populate_rootfs);
761