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