xref: /openbmc/linux/init/initramfs.c (revision 96de2506)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/init.h>
3 #include <linux/fs.h>
4 #include <linux/slab.h>
5 #include <linux/types.h>
6 #include <linux/fcntl.h>
7 #include <linux/delay.h>
8 #include <linux/string.h>
9 #include <linux/dirent.h>
10 #include <linux/syscalls.h>
11 #include <linux/utime.h>
12 #include <linux/file.h>
13 
14 static ssize_t __init xwrite(int fd, const char *p, size_t count)
15 {
16 	ssize_t out = 0;
17 
18 	/* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
19 	while (count) {
20 		ssize_t rv = ksys_write(fd, p, count);
21 
22 		if (rv < 0) {
23 			if (rv == -EINTR || rv == -EAGAIN)
24 				continue;
25 			return out ? out : rv;
26 		} else if (rv == 0)
27 			break;
28 
29 		p += rv;
30 		out += rv;
31 		count -= rv;
32 	}
33 
34 	return out;
35 }
36 
37 static __initdata char *message;
38 static void __init error(char *x)
39 {
40 	if (!message)
41 		message = x;
42 }
43 
44 /* link hash */
45 
46 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
47 
48 static __initdata struct hash {
49 	int ino, minor, major;
50 	umode_t mode;
51 	struct hash *next;
52 	char name[N_ALIGN(PATH_MAX)];
53 } *head[32];
54 
55 static inline int hash(int major, int minor, int ino)
56 {
57 	unsigned long tmp = ino + minor + (major << 3);
58 	tmp += tmp >> 5;
59 	return tmp & 31;
60 }
61 
62 static char __init *find_link(int major, int minor, int ino,
63 			      umode_t mode, char *name)
64 {
65 	struct hash **p, *q;
66 	for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
67 		if ((*p)->ino != ino)
68 			continue;
69 		if ((*p)->minor != minor)
70 			continue;
71 		if ((*p)->major != major)
72 			continue;
73 		if (((*p)->mode ^ mode) & S_IFMT)
74 			continue;
75 		return (*p)->name;
76 	}
77 	q = kmalloc(sizeof(struct hash), GFP_KERNEL);
78 	if (!q)
79 		panic("can't allocate link hash entry");
80 	q->major = major;
81 	q->minor = minor;
82 	q->ino = ino;
83 	q->mode = mode;
84 	strcpy(q->name, name);
85 	q->next = NULL;
86 	*p = q;
87 	return NULL;
88 }
89 
90 static void __init free_hash(void)
91 {
92 	struct hash **p, *q;
93 	for (p = head; p < head + 32; p++) {
94 		while (*p) {
95 			q = *p;
96 			*p = q->next;
97 			kfree(q);
98 		}
99 	}
100 }
101 
102 static long __init do_utime(char *filename, time64_t mtime)
103 {
104 	struct timespec64 t[2];
105 
106 	t[0].tv_sec = mtime;
107 	t[0].tv_nsec = 0;
108 	t[1].tv_sec = mtime;
109 	t[1].tv_nsec = 0;
110 
111 	return do_utimes(AT_FDCWD, filename, t, AT_SYMLINK_NOFOLLOW);
112 }
113 
114 static __initdata LIST_HEAD(dir_list);
115 struct dir_entry {
116 	struct list_head list;
117 	char *name;
118 	time64_t mtime;
119 };
120 
121 static void __init dir_add(const char *name, time64_t mtime)
122 {
123 	struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
124 	if (!de)
125 		panic("can't allocate dir_entry buffer");
126 	INIT_LIST_HEAD(&de->list);
127 	de->name = kstrdup(name, GFP_KERNEL);
128 	de->mtime = mtime;
129 	list_add(&de->list, &dir_list);
130 }
131 
132 static void __init dir_utime(void)
133 {
134 	struct dir_entry *de, *tmp;
135 	list_for_each_entry_safe(de, tmp, &dir_list, list) {
136 		list_del(&de->list);
137 		do_utime(de->name, de->mtime);
138 		kfree(de->name);
139 		kfree(de);
140 	}
141 }
142 
143 static __initdata time64_t mtime;
144 
145 /* cpio header parsing */
146 
147 static __initdata unsigned long ino, major, minor, nlink;
148 static __initdata umode_t mode;
149 static __initdata unsigned long body_len, name_len;
150 static __initdata uid_t uid;
151 static __initdata gid_t gid;
152 static __initdata unsigned rdev;
153 
154 static void __init parse_header(char *s)
155 {
156 	unsigned long parsed[12];
157 	char buf[9];
158 	int i;
159 
160 	buf[8] = '\0';
161 	for (i = 0, s += 6; i < 12; i++, s += 8) {
162 		memcpy(buf, s, 8);
163 		parsed[i] = simple_strtoul(buf, NULL, 16);
164 	}
165 	ino = parsed[0];
166 	mode = parsed[1];
167 	uid = parsed[2];
168 	gid = parsed[3];
169 	nlink = parsed[4];
170 	mtime = parsed[5]; /* breaks in y2106 */
171 	body_len = parsed[6];
172 	major = parsed[7];
173 	minor = parsed[8];
174 	rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
175 	name_len = parsed[11];
176 }
177 
178 /* FSM */
179 
180 static __initdata enum state {
181 	Start,
182 	Collect,
183 	GotHeader,
184 	SkipIt,
185 	GotName,
186 	CopyFile,
187 	GotSymlink,
188 	Reset
189 } state, next_state;
190 
191 static __initdata char *victim;
192 static unsigned long byte_count __initdata;
193 static __initdata loff_t this_header, next_header;
194 
195 static inline void __init eat(unsigned n)
196 {
197 	victim += n;
198 	this_header += n;
199 	byte_count -= n;
200 }
201 
202 static __initdata char *vcollected;
203 static __initdata char *collected;
204 static long remains __initdata;
205 static __initdata char *collect;
206 
207 static void __init read_into(char *buf, unsigned size, enum state next)
208 {
209 	if (byte_count >= size) {
210 		collected = victim;
211 		eat(size);
212 		state = next;
213 	} else {
214 		collect = collected = buf;
215 		remains = size;
216 		next_state = next;
217 		state = Collect;
218 	}
219 }
220 
221 static __initdata char *header_buf, *symlink_buf, *name_buf;
222 
223 static int __init do_start(void)
224 {
225 	read_into(header_buf, 110, GotHeader);
226 	return 0;
227 }
228 
229 static int __init do_collect(void)
230 {
231 	unsigned long n = remains;
232 	if (byte_count < n)
233 		n = byte_count;
234 	memcpy(collect, victim, n);
235 	eat(n);
236 	collect += n;
237 	if ((remains -= n) != 0)
238 		return 1;
239 	state = next_state;
240 	return 0;
241 }
242 
243 static int __init do_header(void)
244 {
245 	if (memcmp(collected, "070707", 6)==0) {
246 		error("incorrect cpio method used: use -H newc option");
247 		return 1;
248 	}
249 	if (memcmp(collected, "070701", 6)) {
250 		error("no cpio magic");
251 		return 1;
252 	}
253 	parse_header(collected);
254 	next_header = this_header + N_ALIGN(name_len) + body_len;
255 	next_header = (next_header + 3) & ~3;
256 	state = SkipIt;
257 	if (name_len <= 0 || name_len > PATH_MAX)
258 		return 0;
259 	if (S_ISLNK(mode)) {
260 		if (body_len > PATH_MAX)
261 			return 0;
262 		collect = collected = symlink_buf;
263 		remains = N_ALIGN(name_len) + body_len;
264 		next_state = GotSymlink;
265 		state = Collect;
266 		return 0;
267 	}
268 	if (S_ISREG(mode) || !body_len)
269 		read_into(name_buf, N_ALIGN(name_len), GotName);
270 	return 0;
271 }
272 
273 static int __init do_skip(void)
274 {
275 	if (this_header + byte_count < next_header) {
276 		eat(byte_count);
277 		return 1;
278 	} else {
279 		eat(next_header - this_header);
280 		state = next_state;
281 		return 0;
282 	}
283 }
284 
285 static int __init do_reset(void)
286 {
287 	while (byte_count && *victim == '\0')
288 		eat(1);
289 	if (byte_count && (this_header & 3))
290 		error("broken padding");
291 	return 1;
292 }
293 
294 static int __init maybe_link(void)
295 {
296 	if (nlink >= 2) {
297 		char *old = find_link(major, minor, ino, mode, collected);
298 		if (old)
299 			return (ksys_link(old, collected) < 0) ? -1 : 1;
300 	}
301 	return 0;
302 }
303 
304 static void __init clean_path(char *path, umode_t fmode)
305 {
306 	struct kstat st;
307 
308 	if (!vfs_lstat(path, &st) && (st.mode ^ fmode) & S_IFMT) {
309 		if (S_ISDIR(st.mode))
310 			ksys_rmdir(path);
311 		else
312 			ksys_unlink(path);
313 	}
314 }
315 
316 static __initdata int wfd;
317 
318 static int __init do_name(void)
319 {
320 	state = SkipIt;
321 	next_state = Reset;
322 	if (strcmp(collected, "TRAILER!!!") == 0) {
323 		free_hash();
324 		return 0;
325 	}
326 	clean_path(collected, mode);
327 	if (S_ISREG(mode)) {
328 		int ml = maybe_link();
329 		if (ml >= 0) {
330 			int openflags = O_WRONLY|O_CREAT;
331 			if (ml != 1)
332 				openflags |= O_TRUNC;
333 			wfd = ksys_open(collected, openflags, mode);
334 
335 			if (wfd >= 0) {
336 				ksys_fchown(wfd, uid, gid);
337 				ksys_fchmod(wfd, mode);
338 				if (body_len)
339 					ksys_ftruncate(wfd, body_len);
340 				vcollected = kstrdup(collected, GFP_KERNEL);
341 				state = CopyFile;
342 			}
343 		}
344 	} else if (S_ISDIR(mode)) {
345 		ksys_mkdir(collected, mode);
346 		ksys_chown(collected, uid, gid);
347 		ksys_chmod(collected, mode);
348 		dir_add(collected, mtime);
349 	} else if (S_ISBLK(mode) || S_ISCHR(mode) ||
350 		   S_ISFIFO(mode) || S_ISSOCK(mode)) {
351 		if (maybe_link() == 0) {
352 			ksys_mknod(collected, mode, rdev);
353 			ksys_chown(collected, uid, gid);
354 			ksys_chmod(collected, mode);
355 			do_utime(collected, mtime);
356 		}
357 	}
358 	return 0;
359 }
360 
361 static int __init do_copy(void)
362 {
363 	if (byte_count >= body_len) {
364 		if (xwrite(wfd, victim, body_len) != body_len)
365 			error("write error");
366 		ksys_close(wfd);
367 		do_utime(vcollected, mtime);
368 		kfree(vcollected);
369 		eat(body_len);
370 		state = SkipIt;
371 		return 0;
372 	} else {
373 		if (xwrite(wfd, victim, byte_count) != byte_count)
374 			error("write error");
375 		body_len -= byte_count;
376 		eat(byte_count);
377 		return 1;
378 	}
379 }
380 
381 static int __init do_symlink(void)
382 {
383 	collected[N_ALIGN(name_len) + body_len] = '\0';
384 	clean_path(collected, 0);
385 	ksys_symlink(collected + N_ALIGN(name_len), collected);
386 	ksys_lchown(collected, uid, gid);
387 	do_utime(collected, mtime);
388 	state = SkipIt;
389 	next_state = Reset;
390 	return 0;
391 }
392 
393 static __initdata int (*actions[])(void) = {
394 	[Start]		= do_start,
395 	[Collect]	= do_collect,
396 	[GotHeader]	= do_header,
397 	[SkipIt]	= do_skip,
398 	[GotName]	= do_name,
399 	[CopyFile]	= do_copy,
400 	[GotSymlink]	= do_symlink,
401 	[Reset]		= do_reset,
402 };
403 
404 static long __init write_buffer(char *buf, unsigned long len)
405 {
406 	byte_count = len;
407 	victim = buf;
408 
409 	while (!actions[state]())
410 		;
411 	return len - byte_count;
412 }
413 
414 static long __init flush_buffer(void *bufv, unsigned long len)
415 {
416 	char *buf = (char *) bufv;
417 	long written;
418 	long origLen = len;
419 	if (message)
420 		return -1;
421 	while ((written = write_buffer(buf, len)) < len && !message) {
422 		char c = buf[written];
423 		if (c == '0') {
424 			buf += written;
425 			len -= written;
426 			state = Start;
427 		} else if (c == 0) {
428 			buf += written;
429 			len -= written;
430 			state = Reset;
431 		} else
432 			error("junk in compressed archive");
433 	}
434 	return origLen;
435 }
436 
437 static unsigned long my_inptr; /* index of next byte to be processed in inbuf */
438 
439 #include <linux/decompress/generic.h>
440 
441 static char * __init unpack_to_rootfs(char *buf, unsigned long len)
442 {
443 	long written;
444 	decompress_fn decompress;
445 	const char *compress_name;
446 	static __initdata char msg_buf[64];
447 
448 	header_buf = kmalloc(110, GFP_KERNEL);
449 	symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
450 	name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
451 
452 	if (!header_buf || !symlink_buf || !name_buf)
453 		panic("can't allocate buffers");
454 
455 	state = Start;
456 	this_header = 0;
457 	message = NULL;
458 	while (!message && len) {
459 		loff_t saved_offset = this_header;
460 		if (*buf == '0' && !(this_header & 3)) {
461 			state = Start;
462 			written = write_buffer(buf, len);
463 			buf += written;
464 			len -= written;
465 			continue;
466 		}
467 		if (!*buf) {
468 			buf++;
469 			len--;
470 			this_header++;
471 			continue;
472 		}
473 		this_header = 0;
474 		decompress = decompress_method(buf, len, &compress_name);
475 		pr_debug("Detected %s compressed data\n", compress_name);
476 		if (decompress) {
477 			int res = decompress(buf, len, NULL, flush_buffer, NULL,
478 				   &my_inptr, error);
479 			if (res)
480 				error("decompressor failed");
481 		} else if (compress_name) {
482 			if (!message) {
483 				snprintf(msg_buf, sizeof msg_buf,
484 					 "compression method %s not configured",
485 					 compress_name);
486 				message = msg_buf;
487 			}
488 		} else
489 			error("junk in compressed archive");
490 		if (state != Reset)
491 			error("junk in compressed archive");
492 		this_header = saved_offset + my_inptr;
493 		buf += my_inptr;
494 		len -= my_inptr;
495 	}
496 	dir_utime();
497 	kfree(name_buf);
498 	kfree(symlink_buf);
499 	kfree(header_buf);
500 	return message;
501 }
502 
503 static int __initdata do_retain_initrd;
504 
505 static int __init retain_initrd_param(char *str)
506 {
507 	if (*str)
508 		return 0;
509 	do_retain_initrd = 1;
510 	return 1;
511 }
512 __setup("retain_initrd", retain_initrd_param);
513 
514 extern char __initramfs_start[];
515 extern unsigned long __initramfs_size;
516 #include <linux/initrd.h>
517 #include <linux/kexec.h>
518 
519 static void __init free_initrd(void)
520 {
521 #ifdef CONFIG_KEXEC_CORE
522 	unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
523 	unsigned long crashk_end   = (unsigned long)__va(crashk_res.end);
524 #endif
525 	if (do_retain_initrd)
526 		goto skip;
527 
528 #ifdef CONFIG_KEXEC_CORE
529 	/*
530 	 * If the initrd region is overlapped with crashkernel reserved region,
531 	 * free only memory that is not part of crashkernel region.
532 	 */
533 	if (initrd_start < crashk_end && initrd_end > crashk_start) {
534 		/*
535 		 * Initialize initrd memory region since the kexec boot does
536 		 * not do.
537 		 */
538 		memset((void *)initrd_start, 0, initrd_end - initrd_start);
539 		if (initrd_start < crashk_start)
540 			free_initrd_mem(initrd_start, crashk_start);
541 		if (initrd_end > crashk_end)
542 			free_initrd_mem(crashk_end, initrd_end);
543 	} else
544 #endif
545 		free_initrd_mem(initrd_start, initrd_end);
546 skip:
547 	initrd_start = 0;
548 	initrd_end = 0;
549 }
550 
551 #ifdef CONFIG_BLK_DEV_RAM
552 #define BUF_SIZE 1024
553 static void __init clean_rootfs(void)
554 {
555 	int fd;
556 	void *buf;
557 	struct linux_dirent64 *dirp;
558 	int num;
559 
560 	fd = ksys_open("/", O_RDONLY, 0);
561 	WARN_ON(fd < 0);
562 	if (fd < 0)
563 		return;
564 	buf = kzalloc(BUF_SIZE, GFP_KERNEL);
565 	WARN_ON(!buf);
566 	if (!buf) {
567 		ksys_close(fd);
568 		return;
569 	}
570 
571 	dirp = buf;
572 	num = ksys_getdents64(fd, dirp, BUF_SIZE);
573 	while (num > 0) {
574 		while (num > 0) {
575 			struct kstat st;
576 			int ret;
577 
578 			ret = vfs_lstat(dirp->d_name, &st);
579 			WARN_ON_ONCE(ret);
580 			if (!ret) {
581 				if (S_ISDIR(st.mode))
582 					ksys_rmdir(dirp->d_name);
583 				else
584 					ksys_unlink(dirp->d_name);
585 			}
586 
587 			num -= dirp->d_reclen;
588 			dirp = (void *)dirp + dirp->d_reclen;
589 		}
590 		dirp = buf;
591 		memset(buf, 0, BUF_SIZE);
592 		num = ksys_getdents64(fd, dirp, BUF_SIZE);
593 	}
594 
595 	ksys_close(fd);
596 	kfree(buf);
597 }
598 #endif
599 
600 static int __init populate_rootfs(void)
601 {
602 	/* Load the built in initramfs */
603 	char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
604 	if (err)
605 		panic("%s", err); /* Failed to decompress INTERNAL initramfs */
606 	/* If available load the bootloader supplied initrd */
607 	if (initrd_start && !IS_ENABLED(CONFIG_INITRAMFS_FORCE)) {
608 #ifdef CONFIG_BLK_DEV_RAM
609 		int fd;
610 		printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
611 		err = unpack_to_rootfs((char *)initrd_start,
612 			initrd_end - initrd_start);
613 		if (!err) {
614 			free_initrd();
615 			goto done;
616 		} else {
617 			clean_rootfs();
618 			unpack_to_rootfs(__initramfs_start, __initramfs_size);
619 		}
620 		printk(KERN_INFO "rootfs image is not initramfs (%s)"
621 				"; looks like an initrd\n", err);
622 		fd = ksys_open("/initrd.image",
623 			      O_WRONLY|O_CREAT, 0700);
624 		if (fd >= 0) {
625 			ssize_t written = xwrite(fd, (char *)initrd_start,
626 						initrd_end - initrd_start);
627 
628 			if (written != initrd_end - initrd_start)
629 				pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
630 				       written, initrd_end - initrd_start);
631 
632 			ksys_close(fd);
633 			free_initrd();
634 		}
635 	done:
636 		/* empty statement */;
637 #else
638 		printk(KERN_INFO "Unpacking initramfs...\n");
639 		err = unpack_to_rootfs((char *)initrd_start,
640 			initrd_end - initrd_start);
641 		if (err)
642 			printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
643 		free_initrd();
644 #endif
645 	}
646 	flush_delayed_fput();
647 	/*
648 	 * Try loading default modules from initramfs.  This gives
649 	 * us a chance to load before device_initcalls.
650 	 */
651 	load_default_modules();
652 
653 	return 0;
654 }
655 rootfs_initcall(populate_rootfs);
656