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