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