1 #include <linux/init.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/dirent.h>
9 #include <linux/syscalls.h>
10 #include <linux/utime.h>
11 #include <linux/async.h>
13 static __initdata char *message;
14 static void __init error(char *x)
22 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
24 static __initdata struct hash {
25 int ino, minor, major;
28 char name[N_ALIGN(PATH_MAX)];
31 static inline int hash(int major, int minor, int ino)
33 unsigned long tmp = ino + minor + (major << 3);
38 static char __init *find_link(int major, int minor, int ino,
39 mode_t mode, char *name)
42 for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
45 if ((*p)->minor != minor)
47 if ((*p)->major != major)
49 if (((*p)->mode ^ mode) & S_IFMT)
53 q = kmalloc(sizeof(struct hash), GFP_KERNEL);
55 panic("can't allocate link hash entry");
60 strcpy(q->name, name);
66 static void __init free_hash(void)
69 for (p = head; p < head + 32; p++) {
78 static long __init do_utime(char __user *filename, time_t mtime)
87 return do_utimes(AT_FDCWD, filename, t, AT_SYMLINK_NOFOLLOW);
90 static __initdata LIST_HEAD(dir_list);
92 struct list_head list;
97 static void __init dir_add(const char *name, time_t mtime)
99 struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
101 panic("can't allocate dir_entry buffer");
102 INIT_LIST_HEAD(&de->list);
103 de->name = kstrdup(name, GFP_KERNEL);
105 list_add(&de->list, &dir_list);
108 static void __init dir_utime(void)
110 struct dir_entry *de, *tmp;
111 list_for_each_entry_safe(de, tmp, &dir_list, list) {
113 do_utime(de->name, de->mtime);
119 static __initdata time_t mtime;
121 /* cpio header parsing */
123 static __initdata unsigned long ino, major, minor, nlink;
124 static __initdata mode_t mode;
125 static __initdata unsigned long body_len, name_len;
126 static __initdata uid_t uid;
127 static __initdata gid_t gid;
128 static __initdata unsigned rdev;
130 static void __init parse_header(char *s)
132 unsigned long parsed[12];
137 for (i = 0, s += 6; i < 12; i++, s += 8) {
139 parsed[i] = simple_strtoul(buf, NULL, 16);
147 body_len = parsed[6];
150 rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
151 name_len = parsed[11];
156 static __initdata enum state {
167 static __initdata char *victim;
168 static __initdata unsigned count;
169 static __initdata loff_t this_header, next_header;
171 static inline void __init eat(unsigned 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)
190 collect = collected = buf;
197 static __initdata char *header_buf, *symlink_buf, *name_buf;
199 static int __init do_start(void)
201 read_into(header_buf, 110, GotHeader);
205 static int __init do_collect(void)
207 unsigned n = remains;
210 memcpy(collect, victim, n);
213 if ((remains -= n) != 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");
225 if (memcmp(collected, "070701", 6)) {
226 error("no cpio magic");
229 parse_header(collected);
230 next_header = this_header + N_ALIGN(name_len) + body_len;
231 next_header = (next_header + 3) & ~3;
233 if (name_len <= 0 || name_len > PATH_MAX)
236 if (body_len > PATH_MAX)
238 collect = collected = symlink_buf;
239 remains = N_ALIGN(name_len) + body_len;
240 next_state = GotSymlink;
244 if (S_ISREG(mode) || !body_len)
245 read_into(name_buf, N_ALIGN(name_len), GotName);
249 static int __init do_skip(void)
251 if (this_header + count < next_header) {
255 eat(next_header - this_header);
261 static int __init do_reset(void)
263 while(count && *victim == '\0')
265 if (count && (this_header & 3))
266 error("broken padding");
270 static int __init maybe_link(void)
273 char *old = find_link(major, minor, ino, mode, collected);
275 return (sys_link(old, collected) < 0) ? -1 : 1;
280 static void __init clean_path(char *path, mode_t mode)
284 if (!sys_newlstat(path, &st) && (st.st_mode^mode) & S_IFMT) {
285 if (S_ISDIR(st.st_mode))
292 static __initdata int wfd;
294 static int __init do_name(void)
298 if (strcmp(collected, "TRAILER!!!") == 0) {
302 clean_path(collected, mode);
304 int ml = maybe_link();
306 int openflags = O_WRONLY|O_CREAT;
308 openflags |= O_TRUNC;
309 wfd = sys_open(collected, openflags, mode);
312 sys_fchown(wfd, uid, gid);
313 sys_fchmod(wfd, mode);
315 sys_ftruncate(wfd, body_len);
316 vcollected = kstrdup(collected, GFP_KERNEL);
320 } else if (S_ISDIR(mode)) {
321 sys_mkdir(collected, mode);
322 sys_chown(collected, uid, gid);
323 sys_chmod(collected, mode);
324 dir_add(collected, mtime);
325 } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
326 S_ISFIFO(mode) || S_ISSOCK(mode)) {
327 if (maybe_link() == 0) {
328 sys_mknod(collected, mode, rdev);
329 sys_chown(collected, uid, gid);
330 sys_chmod(collected, mode);
331 do_utime(collected, mtime);
337 static int __init do_copy(void)
339 if (count >= body_len) {
340 sys_write(wfd, victim, body_len);
342 do_utime(vcollected, mtime);
348 sys_write(wfd, victim, count);
355 static int __init do_symlink(void)
357 collected[N_ALIGN(name_len) + body_len] = '\0';
358 clean_path(collected, 0);
359 sys_symlink(collected + N_ALIGN(name_len), collected);
360 sys_lchown(collected, uid, gid);
361 do_utime(collected, mtime);
367 static __initdata int (*actions[])(void) = {
369 [Collect] = do_collect,
370 [GotHeader] = do_header,
373 [CopyFile] = do_copy,
374 [GotSymlink] = do_symlink,
378 static int __init write_buffer(char *buf, unsigned len)
383 while (!actions[state]())
388 static int __init flush_buffer(void *bufv, unsigned len)
390 char *buf = (char *) bufv;
395 while ((written = write_buffer(buf, len)) < len && !message) {
396 char c = buf[written];
406 error("junk in compressed archive");
411 static unsigned my_inptr; /* index of next byte to be processed in inbuf */
413 #include <linux/decompress/generic.h>
415 static char * __init unpack_to_rootfs(char *buf, unsigned len)
418 decompress_fn decompress;
419 const char *compress_name;
420 static __initdata char msg_buf[64];
422 header_buf = kmalloc(110, GFP_KERNEL);
423 symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
424 name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
426 if (!header_buf || !symlink_buf || !name_buf)
427 panic("can't allocate buffers");
432 while (!message && len) {
433 loff_t saved_offset = this_header;
434 if (*buf == '0' && !(this_header & 3)) {
436 written = write_buffer(buf, len);
448 decompress = decompress_method(buf, len, &compress_name);
450 res = decompress(buf, len, NULL, flush_buffer, NULL,
453 error("decompressor failed");
454 } else if (compress_name) {
456 snprintf(msg_buf, sizeof msg_buf,
457 "compression method %s not configured",
462 error("junk in compressed archive");
464 error("junk in compressed archive");
465 this_header = saved_offset + my_inptr;
476 static int __initdata do_retain_initrd;
478 static int __init retain_initrd_param(char *str)
482 do_retain_initrd = 1;
485 __setup("retain_initrd", retain_initrd_param);
487 extern char __initramfs_start[];
488 extern unsigned long __initramfs_size;
489 #include <linux/initrd.h>
490 #include <linux/kexec.h>
492 static void __init free_initrd(void)
495 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
496 unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
498 if (do_retain_initrd)
503 * If the initrd region is overlapped with crashkernel reserved region,
504 * free only memory that is not part of crashkernel region.
506 if (initrd_start < crashk_end && initrd_end > crashk_start) {
508 * Initialize initrd memory region since the kexec boot does
511 memset((void *)initrd_start, 0, initrd_end - initrd_start);
512 if (initrd_start < crashk_start)
513 free_initrd_mem(initrd_start, crashk_start);
514 if (initrd_end > crashk_end)
515 free_initrd_mem(crashk_end, initrd_end);
518 free_initrd_mem(initrd_start, initrd_end);
524 #ifdef CONFIG_BLK_DEV_RAM
525 #define BUF_SIZE 1024
526 static void __init clean_rootfs(void)
530 struct linux_dirent64 *dirp;
533 fd = sys_open((const char __user __force *) "/", O_RDONLY, 0);
537 buf = kzalloc(BUF_SIZE, GFP_KERNEL);
545 num = sys_getdents64(fd, dirp, BUF_SIZE);
551 ret = sys_newlstat(dirp->d_name, &st);
554 if (S_ISDIR(st.st_mode))
555 sys_rmdir(dirp->d_name);
557 sys_unlink(dirp->d_name);
560 num -= dirp->d_reclen;
561 dirp = (void *)dirp + dirp->d_reclen;
564 memset(buf, 0, BUF_SIZE);
565 num = sys_getdents64(fd, dirp, BUF_SIZE);
573 LIST_HEAD(populate_rootfs_domain);
575 void populate_rootfs_wait(void)
577 async_synchronize_full_domain(&populate_rootfs_domain);
579 EXPORT_SYMBOL(populate_rootfs_wait);
581 static void __init async_populate_rootfs(void)
583 char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
585 panic(err); /* Failed to decompress INTERNAL initramfs */
587 #ifdef CONFIG_BLK_DEV_RAM
589 printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
590 err = unpack_to_rootfs((char *)initrd_start,
591 initrd_end - initrd_start);
597 unpack_to_rootfs(__initramfs_start, __initramfs_size);
599 printk(KERN_INFO "rootfs image is not initramfs (%s)"
600 "; looks like an initrd\n", err);
601 fd = sys_open((const char __user __force *) "/initrd.image",
602 O_WRONLY|O_CREAT, 0700);
604 sys_write(fd, (char *)initrd_start,
605 initrd_end - initrd_start);
610 printk(KERN_INFO "Unpacking initramfs...\n");
611 err = unpack_to_rootfs((char *)initrd_start,
612 initrd_end - initrd_start);
614 printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
621 static int __initdata rootfs_populated;
623 static int __init populate_rootfs_early(void)
625 if (num_online_cpus() > 1) {
626 rootfs_populated = 1;
627 async_schedule_domain(async_populate_rootfs, NULL,
628 &populate_rootfs_domain);
631 static int __init populate_rootfs(void)
633 if (!rootfs_populated)
634 async_schedule_domain(async_populate_rootfs, NULL,
635 &populate_rootfs_domain);
638 earlyrootfs_initcall(populate_rootfs_early);
639 rootfs_initcall(populate_rootfs);