bzip2/lzma: config and initramfs support for bzip2/lzma decompression
[linux-2.6/linux-2.6-openrd.git] / init / initramfs.c
blob40bd4fb9578851fb782f9f41633ed721cd25d519
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>
11 static __initdata char *message;
12 static void __init error(char *x)
14 if (!message)
15 message = x;
18 /* link hash */
20 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
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];
29 static inline int hash(int major, int minor, int ino)
31 unsigned long tmp = ino + minor + (major << 3);
32 tmp += tmp >> 5;
33 return tmp & 31;
36 static char __init *find_link(int major, int minor, int ino,
37 mode_t mode, char *name)
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;
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;
64 static void __init free_hash(void)
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);
76 static long __init do_utime(char __user *filename, time_t mtime)
78 struct timespec t[2];
80 t[0].tv_sec = mtime;
81 t[0].tv_nsec = 0;
82 t[1].tv_sec = mtime;
83 t[1].tv_nsec = 0;
85 return do_utimes(AT_FDCWD, filename, t, AT_SYMLINK_NOFOLLOW);
88 static __initdata LIST_HEAD(dir_list);
89 struct dir_entry {
90 struct list_head list;
91 char *name;
92 time_t mtime;
95 static void __init dir_add(const char *name, time_t mtime)
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);
106 static void __init dir_utime(void)
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);
117 static __initdata time_t mtime;
119 /* cpio header parsing */
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;
128 static void __init parse_header(char *s)
130 unsigned long parsed[12];
131 char buf[9];
132 int i;
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);
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];
152 /* FSM */
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;
165 static __initdata char *victim;
166 static __initdata unsigned count;
167 static __initdata loff_t this_header, next_header;
169 static __initdata int dry_run;
171 static inline void __init eat(unsigned n)
173 victim += n;
174 this_header += n;
175 count -= n;
178 static __initdata char *vcollected;
179 static __initdata char *collected;
180 static __initdata int remains;
181 static __initdata char *collect;
183 static void __init read_into(char *buf, unsigned size, enum state next)
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;
197 static __initdata char *header_buf, *symlink_buf, *name_buf;
199 static int __init do_start(void)
201 read_into(header_buf, 110, GotHeader);
202 return 0;
205 static int __init do_collect(void)
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;
219 static int __init do_header(void)
221 if (memcmp(collected, "070707", 6)==0) {
222 error("incorrect cpio method used: use -H newc option");
223 return 1;
225 if (memcmp(collected, "070701", 6)) {
226 error("no cpio magic");
227 return 1;
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;
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;
248 if (S_ISREG(mode) || !body_len)
249 read_into(name_buf, N_ALIGN(name_len), GotName);
250 return 0;
253 static int __init do_skip(void)
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;
265 static int __init do_reset(void)
267 while(count && *victim == '\0')
268 eat(1);
269 if (count && (this_header & 3))
270 error("broken padding");
271 return 1;
274 static int __init maybe_link(void)
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;
281 return 0;
284 static void __init clean_path(char *path, mode_t mode)
286 struct stat st;
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);
296 static __initdata int wfd;
298 static int __init do_name(void)
300 state = SkipIt;
301 next_state = Reset;
302 if (strcmp(collected, "TRAILER!!!") == 0) {
303 free_hash();
304 return 0;
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);
317 if (wfd >= 0) {
318 sys_fchown(wfd, uid, gid);
319 sys_fchmod(wfd, mode);
320 vcollected = kstrdup(collected, GFP_KERNEL);
321 state = CopyFile;
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);
338 return 0;
341 static int __init do_copy(void)
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;
359 static int __init do_symlink(void)
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;
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,
382 static int __init write_buffer(char *buf, unsigned len)
384 count = len;
385 victim = buf;
387 while (!actions[state]())
389 return len - count;
393 static int __init flush_buffer(void *bufv, unsigned len)
395 char *buf = (char *) bufv;
396 int written;
397 int origLen = len;
398 if (message)
399 return -1;
400 while ((written = write_buffer(buf, len)) < len && !message) {
401 char c = buf[written];
402 if (c == '0') {
403 buf += written;
404 len -= written;
405 state = Start;
406 } else if (c == 0) {
407 buf += written;
408 len -= written;
409 state = Reset;
410 } else
411 error("junk in compressed archive");
413 return origLen;
416 static unsigned my_inptr; /* index of next byte to be processed in inbuf */
418 #include <linux/decompress/bunzip2.h>
419 #include <linux/decompress/unlzma.h>
420 #include <linux/decompress/inflate.h>
422 static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
424 int written;
425 dry_run = check_only;
426 header_buf = kmalloc(110, GFP_KERNEL);
427 symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
428 name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
430 if (!header_buf || !symlink_buf || !name_buf)
431 panic("can't allocate buffers");
433 state = Start;
434 this_header = 0;
435 message = NULL;
436 while (!message && len) {
437 loff_t saved_offset = this_header;
438 if (*buf == '0' && !(this_header & 3)) {
439 state = Start;
440 written = write_buffer(buf, len);
441 buf += written;
442 len -= written;
443 continue;
445 if (!*buf) {
446 buf++;
447 len--;
448 this_header++;
449 continue;
451 this_header = 0;
452 if (!gunzip(buf, len, NULL, flush_buffer, NULL,
453 &my_inptr, error) &&
454 message == NULL)
455 goto ok;
457 #ifdef CONFIG_RD_BZIP2
458 message = NULL; /* Zero out message, or else cpio will
459 think an error has already occured */
460 if (!bunzip2(buf, len, NULL, flush_buffer, NULL,
461 &my_inptr, error) &&
462 message == NULL) {
463 goto ok;
465 #endif
467 #ifdef CONFIG_RD_LZMA
468 message = NULL; /* Zero out message, or else cpio will
469 think an error has already occured */
470 if (!unlzma(buf, len, NULL, flush_buffer, NULL,
471 &my_inptr, error) &&
472 message == NULL) {
473 goto ok;
475 #endif
477 if (state != Reset)
478 error("junk in compressed archive");
479 this_header = saved_offset + my_inptr;
480 buf += my_inptr;
481 len -= my_inptr;
483 dir_utime();
484 kfree(name_buf);
485 kfree(symlink_buf);
486 kfree(header_buf);
487 return message;
490 static int __initdata do_retain_initrd;
492 static int __init retain_initrd_param(char *str)
494 if (*str)
495 return 0;
496 do_retain_initrd = 1;
497 return 1;
499 __setup("retain_initrd", retain_initrd_param);
501 extern char __initramfs_start[], __initramfs_end[];
502 #include <linux/initrd.h>
503 #include <linux/kexec.h>
505 static void __init free_initrd(void)
507 #ifdef CONFIG_KEXEC
508 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
509 unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
510 #endif
511 if (do_retain_initrd)
512 goto skip;
514 #ifdef CONFIG_KEXEC
516 * If the initrd region is overlapped with crashkernel reserved region,
517 * free only memory that is not part of crashkernel region.
519 if (initrd_start < crashk_end && initrd_end > crashk_start) {
521 * Initialize initrd memory region since the kexec boot does
522 * not do.
524 memset((void *)initrd_start, 0, initrd_end - initrd_start);
525 if (initrd_start < crashk_start)
526 free_initrd_mem(initrd_start, crashk_start);
527 if (initrd_end > crashk_end)
528 free_initrd_mem(crashk_end, initrd_end);
529 } else
530 #endif
531 free_initrd_mem(initrd_start, initrd_end);
532 skip:
533 initrd_start = 0;
534 initrd_end = 0;
537 static int __init populate_rootfs(void)
539 char *err = unpack_to_rootfs(__initramfs_start,
540 __initramfs_end - __initramfs_start, 0);
541 if (err)
542 panic(err);
543 if (initrd_start) {
544 #ifdef CONFIG_BLK_DEV_RAM
545 int fd;
546 printk(KERN_INFO "checking if image is initramfs...");
547 err = unpack_to_rootfs((char *)initrd_start,
548 initrd_end - initrd_start, 1);
549 if (!err) {
550 printk(" it is\n");
551 unpack_to_rootfs((char *)initrd_start,
552 initrd_end - initrd_start, 0);
553 free_initrd();
554 return 0;
556 printk("it isn't (%s); looks like an initrd\n", err);
557 fd = sys_open("/initrd.image", O_WRONLY|O_CREAT, 0700);
558 if (fd >= 0) {
559 sys_write(fd, (char *)initrd_start,
560 initrd_end - initrd_start);
561 sys_close(fd);
562 free_initrd();
564 #else
565 printk(KERN_INFO "Unpacking initramfs...");
566 err = unpack_to_rootfs((char *)initrd_start,
567 initrd_end - initrd_start, 0);
568 if (err)
569 panic(err);
570 printk(" done\n");
571 free_initrd();
572 #endif
574 return 0;
576 rootfs_initcall(populate_rootfs);