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.
8 #warning "Sparse checking disabled for this file"
11 #include <linux/init.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>
23 static __initdata char *message;
24 static void __init error(char *x)
32 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
34 static __initdata struct hash {
35 int ino, minor, major;
38 char name[N_ALIGN(PATH_MAX)];
41 static inline int hash(int major, int minor, int ino)
43 unsigned long tmp = ino + minor + (major << 3);
48 static char __init *find_link(int major, int minor, int ino,
49 umode_t mode, char *name)
52 for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
55 if ((*p)->minor != minor)
57 if ((*p)->major != major)
59 if (((*p)->mode ^ mode) & S_IFMT)
63 q = kmalloc(sizeof(struct hash), GFP_KERNEL);
65 panic("can't allocate link hash entry");
70 strcpy(q->name, name);
76 static void __init free_hash(void)
79 for (p = head; p < head + 32; p++) {
88 static long __init do_utime(char *filename, time_t mtime)
97 return do_utimes(AT_FDCWD, filename, t, AT_SYMLINK_NOFOLLOW);
100 static __initdata LIST_HEAD(dir_list);
102 struct list_head list;
107 static void __init dir_add(const char *name, time_t mtime)
109 struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
111 panic("can't allocate dir_entry buffer");
112 INIT_LIST_HEAD(&de->list);
113 de->name = kstrdup(name, GFP_KERNEL);
115 list_add(&de->list, &dir_list);
118 static void __init dir_utime(void)
120 struct dir_entry *de, *tmp;
121 list_for_each_entry_safe(de, tmp, &dir_list, list) {
123 do_utime(de->name, de->mtime);
129 static __initdata time_t mtime;
131 /* cpio header parsing */
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;
140 static void __init parse_header(char *s)
142 unsigned long parsed[12];
147 for (i = 0, s += 6; i < 12; i++, s += 8) {
149 parsed[i] = simple_strtoul(buf, NULL, 16);
157 body_len = parsed[6];
160 rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
161 name_len = parsed[11];
166 static __initdata enum state {
177 static __initdata char *victim;
178 static __initdata unsigned count;
179 static __initdata loff_t this_header, next_header;
181 static inline void __init eat(unsigned n)
188 static __initdata char *vcollected;
189 static __initdata char *collected;
190 static __initdata int remains;
191 static __initdata char *collect;
193 static void __init read_into(char *buf, unsigned size, enum state next)
200 collect = collected = buf;
207 static __initdata char *header_buf, *symlink_buf, *name_buf;
209 static int __init do_start(void)
211 read_into(header_buf, 110, GotHeader);
215 static int __init do_collect(void)
217 unsigned n = remains;
220 memcpy(collect, victim, n);
223 if ((remains -= n) != 0)
229 static int __init do_header(void)
231 if (memcmp(collected, "070707", 6)==0) {
232 error("incorrect cpio method used: use -H newc option");
235 if (memcmp(collected, "070701", 6)) {
236 error("no cpio magic");
239 parse_header(collected);
240 next_header = this_header + N_ALIGN(name_len) + body_len;
241 next_header = (next_header + 3) & ~3;
243 if (name_len <= 0 || name_len > PATH_MAX)
246 if (body_len > PATH_MAX)
248 collect = collected = symlink_buf;
249 remains = N_ALIGN(name_len) + body_len;
250 next_state = GotSymlink;
254 if (S_ISREG(mode) || !body_len)
255 read_into(name_buf, N_ALIGN(name_len), GotName);
259 static int __init do_skip(void)
261 if (this_header + count < next_header) {
265 eat(next_header - this_header);
271 static int __init do_reset(void)
273 while(count && *victim == '\0')
275 if (count && (this_header & 3))
276 error("broken padding");
280 static int __init maybe_link(void)
283 char *old = find_link(major, minor, ino, mode, collected);
285 return (sys_link(old, collected) < 0) ? -1 : 1;
290 static void __init clean_path(char *path, umode_t mode)
294 if (!sys_newlstat(path, &st) && (st.st_mode^mode) & S_IFMT) {
295 if (S_ISDIR(st.st_mode))
302 static __initdata int wfd;
304 static int __init do_name(void)
308 if (strcmp(collected, "TRAILER!!!") == 0) {
312 clean_path(collected, mode);
314 int ml = maybe_link();
316 int openflags = O_WRONLY|O_CREAT;
318 openflags |= O_TRUNC;
319 wfd = sys_open(collected, openflags, mode);
322 sys_fchown(wfd, uid, gid);
323 sys_fchmod(wfd, mode);
325 sys_ftruncate(wfd, body_len);
326 vcollected = kstrdup(collected, GFP_KERNEL);
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);
347 static int __init do_copy(void)
349 if (count >= body_len) {
350 sys_write(wfd, victim, body_len);
352 do_utime(vcollected, mtime);
358 sys_write(wfd, victim, count);
365 static int __init do_symlink(void)
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);
377 static __initdata int (*actions[])(void) = {
379 [Collect] = do_collect,
380 [GotHeader] = do_header,
383 [CopyFile] = do_copy,
384 [GotSymlink] = do_symlink,
388 static int __init write_buffer(char *buf, unsigned len)
393 while (!actions[state]())
398 static int __init flush_buffer(void *bufv, unsigned len)
400 char *buf = (char *) bufv;
405 while ((written = write_buffer(buf, len)) < len && !message) {
406 char c = buf[written];
416 error("junk in compressed archive");
421 static unsigned my_inptr; /* index of next byte to be processed in inbuf */
423 #include <linux/decompress/generic.h>
425 static char * __init unpack_to_rootfs(char *buf, unsigned len)
428 decompress_fn decompress;
429 const char *compress_name;
430 static __initdata char msg_buf[64];
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);
436 if (!header_buf || !symlink_buf || !name_buf)
437 panic("can't allocate buffers");
442 while (!message && len) {
443 loff_t saved_offset = this_header;
444 if (*buf == '0' && !(this_header & 3)) {
446 written = write_buffer(buf, len);
458 decompress = decompress_method(buf, len, &compress_name);
460 res = decompress(buf, len, NULL, flush_buffer, NULL,
463 error("decompressor failed");
464 } else if (compress_name) {
466 snprintf(msg_buf, sizeof msg_buf,
467 "compression method %s not configured",
472 error("junk in compressed archive");
474 error("junk in compressed archive");
475 this_header = saved_offset + my_inptr;
486 static int __initdata do_retain_initrd;
488 static int __init retain_initrd_param(char *str)
492 do_retain_initrd = 1;
495 __setup("retain_initrd", retain_initrd_param);
497 extern char __initramfs_start[];
498 extern unsigned long __initramfs_size;
499 #include <linux/initrd.h>
500 #include <linux/kexec.h>
502 static void __init free_initrd(void)
505 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
506 unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
508 if (do_retain_initrd)
513 * If the initrd region is overlapped with crashkernel reserved region,
514 * free only memory that is not part of crashkernel region.
516 if (initrd_start < crashk_end && initrd_end > crashk_start) {
518 * Initialize initrd memory region since the kexec boot does
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);
528 free_initrd_mem(initrd_start, initrd_end);
534 #ifdef CONFIG_BLK_DEV_RAM
535 #define BUF_SIZE 1024
536 static void __init clean_rootfs(void)
540 struct linux_dirent64 *dirp;
543 fd = sys_open("/", O_RDONLY, 0);
547 buf = kzalloc(BUF_SIZE, GFP_KERNEL);
555 num = sys_getdents64(fd, dirp, BUF_SIZE);
561 ret = sys_newlstat(dirp->d_name, &st);
564 if (S_ISDIR(st.st_mode))
565 sys_rmdir(dirp->d_name);
567 sys_unlink(dirp->d_name);
570 num -= dirp->d_reclen;
571 dirp = (void *)dirp + dirp->d_reclen;
574 memset(buf, 0, BUF_SIZE);
575 num = sys_getdents64(fd, dirp, BUF_SIZE);
583 static int __initdata do_skip_initramfs;
585 static int __init skip_initramfs_param(char *str)
589 do_skip_initramfs = 1;
592 __setup("skip_initramfs", skip_initramfs_param);
594 static int __init populate_rootfs(void)
598 if (do_skip_initramfs)
599 return default_rootfs();
601 err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
603 panic(err); /* Failed to decompress INTERNAL initramfs */
605 #ifdef CONFIG_BLK_DEV_RAM
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);
615 unpack_to_rootfs(__initramfs_start, __initramfs_size);
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);
622 sys_write(fd, (char *)initrd_start,
623 initrd_end - initrd_start);
629 printk(KERN_INFO "Unpacking initramfs...\n");
630 err = unpack_to_rootfs((char *)initrd_start,
631 initrd_end - initrd_start);
633 printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
637 * Try loading default modules from initramfs. This gives
638 * us a chance to load before device_initcalls.
640 load_default_modules();
644 rootfs_initcall(populate_rootfs);