USB: serial: iuu_phoenix: fix DMA from stack
[linux/fpc-iii.git] / init / initramfs.c
blob981f286c1d16ab57dff10cb10099f573b62f07b9
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
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/file.h>
23 static ssize_t __init xwrite(int fd, const char *p, size_t count)
25 ssize_t out = 0;
27 /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
28 while (count) {
29 ssize_t rv = sys_write(fd, p, count);
31 if (rv < 0) {
32 if (rv == -EINTR || rv == -EAGAIN)
33 continue;
34 return out ? out : rv;
35 } else if (rv == 0)
36 break;
38 p += rv;
39 out += rv;
40 count -= rv;
43 return out;
46 static __initdata char *message;
47 static void __init error(char *x)
49 if (!message)
50 message = x;
53 /* link hash */
55 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
57 static __initdata struct hash {
58 int ino, minor, major;
59 umode_t mode;
60 struct hash *next;
61 char name[N_ALIGN(PATH_MAX)];
62 } *head[32];
64 static inline int hash(int major, int minor, int ino)
66 unsigned long tmp = ino + minor + (major << 3);
67 tmp += tmp >> 5;
68 return tmp & 31;
71 static char __init *find_link(int major, int minor, int ino,
72 umode_t mode, char *name)
74 struct hash **p, *q;
75 for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
76 if ((*p)->ino != ino)
77 continue;
78 if ((*p)->minor != minor)
79 continue;
80 if ((*p)->major != major)
81 continue;
82 if (((*p)->mode ^ mode) & S_IFMT)
83 continue;
84 return (*p)->name;
86 q = kmalloc(sizeof(struct hash), GFP_KERNEL);
87 if (!q)
88 panic("can't allocate link hash entry");
89 q->major = major;
90 q->minor = minor;
91 q->ino = ino;
92 q->mode = mode;
93 strcpy(q->name, name);
94 q->next = NULL;
95 *p = q;
96 return NULL;
99 static void __init free_hash(void)
101 struct hash **p, *q;
102 for (p = head; p < head + 32; p++) {
103 while (*p) {
104 q = *p;
105 *p = q->next;
106 kfree(q);
111 static long __init do_utime(char *filename, time_t mtime)
113 struct timespec t[2];
115 t[0].tv_sec = mtime;
116 t[0].tv_nsec = 0;
117 t[1].tv_sec = mtime;
118 t[1].tv_nsec = 0;
120 return do_utimes(AT_FDCWD, filename, t, AT_SYMLINK_NOFOLLOW);
123 static __initdata LIST_HEAD(dir_list);
124 struct dir_entry {
125 struct list_head list;
126 char *name;
127 time_t mtime;
130 static void __init dir_add(const char *name, time_t mtime)
132 struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
133 if (!de)
134 panic("can't allocate dir_entry buffer");
135 INIT_LIST_HEAD(&de->list);
136 de->name = kstrdup(name, GFP_KERNEL);
137 de->mtime = mtime;
138 list_add(&de->list, &dir_list);
141 static void __init dir_utime(void)
143 struct dir_entry *de, *tmp;
144 list_for_each_entry_safe(de, tmp, &dir_list, list) {
145 list_del(&de->list);
146 do_utime(de->name, de->mtime);
147 kfree(de->name);
148 kfree(de);
152 static __initdata time_t mtime;
154 /* cpio header parsing */
156 static __initdata unsigned long ino, major, minor, nlink;
157 static __initdata umode_t mode;
158 static __initdata unsigned long body_len, name_len;
159 static __initdata uid_t uid;
160 static __initdata gid_t gid;
161 static __initdata unsigned rdev;
163 static void __init parse_header(char *s)
165 unsigned long parsed[12];
166 char buf[9];
167 int i;
169 buf[8] = '\0';
170 for (i = 0, s += 6; i < 12; i++, s += 8) {
171 memcpy(buf, s, 8);
172 parsed[i] = simple_strtoul(buf, NULL, 16);
174 ino = parsed[0];
175 mode = parsed[1];
176 uid = parsed[2];
177 gid = parsed[3];
178 nlink = parsed[4];
179 mtime = parsed[5];
180 body_len = parsed[6];
181 major = parsed[7];
182 minor = parsed[8];
183 rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
184 name_len = parsed[11];
187 /* FSM */
189 static __initdata enum state {
190 Start,
191 Collect,
192 GotHeader,
193 SkipIt,
194 GotName,
195 CopyFile,
196 GotSymlink,
197 Reset
198 } state, next_state;
200 static __initdata char *victim;
201 static unsigned long byte_count __initdata;
202 static __initdata loff_t this_header, next_header;
204 static inline void __init eat(unsigned n)
206 victim += n;
207 this_header += n;
208 byte_count -= n;
211 static __initdata char *vcollected;
212 static __initdata char *collected;
213 static long remains __initdata;
214 static __initdata char *collect;
216 static void __init read_into(char *buf, unsigned size, enum state next)
218 if (byte_count >= size) {
219 collected = victim;
220 eat(size);
221 state = next;
222 } else {
223 collect = collected = buf;
224 remains = size;
225 next_state = next;
226 state = Collect;
230 static __initdata char *header_buf, *symlink_buf, *name_buf;
232 static int __init do_start(void)
234 read_into(header_buf, 110, GotHeader);
235 return 0;
238 static int __init do_collect(void)
240 unsigned long n = remains;
241 if (byte_count < n)
242 n = byte_count;
243 memcpy(collect, victim, n);
244 eat(n);
245 collect += n;
246 if ((remains -= n) != 0)
247 return 1;
248 state = next_state;
249 return 0;
252 static int __init do_header(void)
254 if (memcmp(collected, "070707", 6)==0) {
255 error("incorrect cpio method used: use -H newc option");
256 return 1;
258 if (memcmp(collected, "070701", 6)) {
259 error("no cpio magic");
260 return 1;
262 parse_header(collected);
263 next_header = this_header + N_ALIGN(name_len) + body_len;
264 next_header = (next_header + 3) & ~3;
265 state = SkipIt;
266 if (name_len <= 0 || name_len > PATH_MAX)
267 return 0;
268 if (S_ISLNK(mode)) {
269 if (body_len > PATH_MAX)
270 return 0;
271 collect = collected = symlink_buf;
272 remains = N_ALIGN(name_len) + body_len;
273 next_state = GotSymlink;
274 state = Collect;
275 return 0;
277 if (S_ISREG(mode) || !body_len)
278 read_into(name_buf, N_ALIGN(name_len), GotName);
279 return 0;
282 static int __init do_skip(void)
284 if (this_header + byte_count < next_header) {
285 eat(byte_count);
286 return 1;
287 } else {
288 eat(next_header - this_header);
289 state = next_state;
290 return 0;
294 static int __init do_reset(void)
296 while (byte_count && *victim == '\0')
297 eat(1);
298 if (byte_count && (this_header & 3))
299 error("broken padding");
300 return 1;
303 static int __init maybe_link(void)
305 if (nlink >= 2) {
306 char *old = find_link(major, minor, ino, mode, collected);
307 if (old)
308 return (sys_link(old, collected) < 0) ? -1 : 1;
310 return 0;
313 static void __init clean_path(char *path, umode_t fmode)
315 struct stat st;
317 if (!sys_newlstat(path, &st) && (st.st_mode ^ fmode) & S_IFMT) {
318 if (S_ISDIR(st.st_mode))
319 sys_rmdir(path);
320 else
321 sys_unlink(path);
325 static __initdata int wfd;
327 static int __init do_name(void)
329 state = SkipIt;
330 next_state = Reset;
331 if (strcmp(collected, "TRAILER!!!") == 0) {
332 free_hash();
333 return 0;
335 clean_path(collected, mode);
336 if (S_ISREG(mode)) {
337 int ml = maybe_link();
338 if (ml >= 0) {
339 int openflags = O_WRONLY|O_CREAT;
340 if (ml != 1)
341 openflags |= O_TRUNC;
342 wfd = sys_open(collected, openflags, mode);
344 if (wfd >= 0) {
345 sys_fchown(wfd, uid, gid);
346 sys_fchmod(wfd, mode);
347 if (body_len)
348 sys_ftruncate(wfd, body_len);
349 vcollected = kstrdup(collected, GFP_KERNEL);
350 state = CopyFile;
353 } else if (S_ISDIR(mode)) {
354 sys_mkdir(collected, mode);
355 sys_chown(collected, uid, gid);
356 sys_chmod(collected, mode);
357 dir_add(collected, mtime);
358 } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
359 S_ISFIFO(mode) || S_ISSOCK(mode)) {
360 if (maybe_link() == 0) {
361 sys_mknod(collected, mode, rdev);
362 sys_chown(collected, uid, gid);
363 sys_chmod(collected, mode);
364 do_utime(collected, mtime);
367 return 0;
370 static int __init do_copy(void)
372 if (byte_count >= body_len) {
373 if (xwrite(wfd, victim, body_len) != body_len)
374 error("write error");
375 sys_close(wfd);
376 do_utime(vcollected, mtime);
377 kfree(vcollected);
378 eat(body_len);
379 state = SkipIt;
380 return 0;
381 } else {
382 if (xwrite(wfd, victim, byte_count) != byte_count)
383 error("write error");
384 body_len -= byte_count;
385 eat(byte_count);
386 return 1;
390 static int __init do_symlink(void)
392 collected[N_ALIGN(name_len) + body_len] = '\0';
393 clean_path(collected, 0);
394 sys_symlink(collected + N_ALIGN(name_len), collected);
395 sys_lchown(collected, uid, gid);
396 do_utime(collected, mtime);
397 state = SkipIt;
398 next_state = Reset;
399 return 0;
402 static __initdata int (*actions[])(void) = {
403 [Start] = do_start,
404 [Collect] = do_collect,
405 [GotHeader] = do_header,
406 [SkipIt] = do_skip,
407 [GotName] = do_name,
408 [CopyFile] = do_copy,
409 [GotSymlink] = do_symlink,
410 [Reset] = do_reset,
413 static long __init write_buffer(char *buf, unsigned long len)
415 byte_count = len;
416 victim = buf;
418 while (!actions[state]())
420 return len - byte_count;
423 static long __init flush_buffer(void *bufv, unsigned long len)
425 char *buf = (char *) bufv;
426 long written;
427 long origLen = len;
428 if (message)
429 return -1;
430 while ((written = write_buffer(buf, len)) < len && !message) {
431 char c = buf[written];
432 if (c == '0') {
433 buf += written;
434 len -= written;
435 state = Start;
436 } else if (c == 0) {
437 buf += written;
438 len -= written;
439 state = Reset;
440 } else
441 error("junk in compressed archive");
443 return origLen;
446 static unsigned long my_inptr; /* index of next byte to be processed in inbuf */
448 #include <linux/decompress/generic.h>
450 static char * __init unpack_to_rootfs(char *buf, unsigned long len)
452 long written;
453 decompress_fn decompress;
454 const char *compress_name;
455 static __initdata char msg_buf[64];
457 header_buf = kmalloc(110, GFP_KERNEL);
458 symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
459 name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
461 if (!header_buf || !symlink_buf || !name_buf)
462 panic("can't allocate buffers");
464 state = Start;
465 this_header = 0;
466 message = NULL;
467 while (!message && len) {
468 loff_t saved_offset = this_header;
469 if (*buf == '0' && !(this_header & 3)) {
470 state = Start;
471 written = write_buffer(buf, len);
472 buf += written;
473 len -= written;
474 continue;
476 if (!*buf) {
477 buf++;
478 len--;
479 this_header++;
480 continue;
482 this_header = 0;
483 decompress = decompress_method(buf, len, &compress_name);
484 pr_debug("Detected %s compressed data\n", compress_name);
485 if (decompress) {
486 int res = decompress(buf, len, NULL, flush_buffer, NULL,
487 &my_inptr, error);
488 if (res)
489 error("decompressor failed");
490 } else if (compress_name) {
491 if (!message) {
492 snprintf(msg_buf, sizeof msg_buf,
493 "compression method %s not configured",
494 compress_name);
495 message = msg_buf;
497 } else
498 error("junk in compressed archive");
499 if (state != Reset)
500 error("junk in compressed archive");
501 this_header = saved_offset + my_inptr;
502 buf += my_inptr;
503 len -= my_inptr;
505 dir_utime();
506 kfree(name_buf);
507 kfree(symlink_buf);
508 kfree(header_buf);
509 return message;
512 static int __initdata do_retain_initrd;
514 static int __init retain_initrd_param(char *str)
516 if (*str)
517 return 0;
518 do_retain_initrd = 1;
519 return 1;
521 __setup("retain_initrd", retain_initrd_param);
523 extern char __initramfs_start[];
524 extern unsigned long __initramfs_size;
525 #include <linux/initrd.h>
526 #include <linux/kexec.h>
528 static void __init free_initrd(void)
530 #ifdef CONFIG_KEXEC_CORE
531 unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
532 unsigned long crashk_end = (unsigned long)__va(crashk_res.end);
533 #endif
534 if (do_retain_initrd)
535 goto skip;
537 #ifdef CONFIG_KEXEC_CORE
539 * If the initrd region is overlapped with crashkernel reserved region,
540 * free only memory that is not part of crashkernel region.
542 if (initrd_start < crashk_end && initrd_end > crashk_start) {
544 * Initialize initrd memory region since the kexec boot does
545 * not do.
547 memset((void *)initrd_start, 0, initrd_end - initrd_start);
548 if (initrd_start < crashk_start)
549 free_initrd_mem(initrd_start, crashk_start);
550 if (initrd_end > crashk_end)
551 free_initrd_mem(crashk_end, initrd_end);
552 } else
553 #endif
554 free_initrd_mem(initrd_start, initrd_end);
555 skip:
556 initrd_start = 0;
557 initrd_end = 0;
560 #ifdef CONFIG_BLK_DEV_RAM
561 #define BUF_SIZE 1024
562 static void __init clean_rootfs(void)
564 int fd;
565 void *buf;
566 struct linux_dirent64 *dirp;
567 int num;
569 fd = sys_open("/", O_RDONLY, 0);
570 WARN_ON(fd < 0);
571 if (fd < 0)
572 return;
573 buf = kzalloc(BUF_SIZE, GFP_KERNEL);
574 WARN_ON(!buf);
575 if (!buf) {
576 sys_close(fd);
577 return;
580 dirp = buf;
581 num = sys_getdents64(fd, dirp, BUF_SIZE);
582 while (num > 0) {
583 while (num > 0) {
584 struct stat st;
585 int ret;
587 ret = sys_newlstat(dirp->d_name, &st);
588 WARN_ON_ONCE(ret);
589 if (!ret) {
590 if (S_ISDIR(st.st_mode))
591 sys_rmdir(dirp->d_name);
592 else
593 sys_unlink(dirp->d_name);
596 num -= dirp->d_reclen;
597 dirp = (void *)dirp + dirp->d_reclen;
599 dirp = buf;
600 memset(buf, 0, BUF_SIZE);
601 num = sys_getdents64(fd, dirp, BUF_SIZE);
604 sys_close(fd);
605 kfree(buf);
607 #endif
609 static int __init populate_rootfs(void)
611 char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
612 if (err)
613 panic("%s", err); /* Failed to decompress INTERNAL initramfs */
614 if (initrd_start) {
615 #ifdef CONFIG_BLK_DEV_RAM
616 int fd;
617 printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
618 err = unpack_to_rootfs((char *)initrd_start,
619 initrd_end - initrd_start);
620 if (!err) {
621 free_initrd();
622 goto done;
623 } else {
624 clean_rootfs();
625 unpack_to_rootfs(__initramfs_start, __initramfs_size);
627 printk(KERN_INFO "rootfs image is not initramfs (%s)"
628 "; looks like an initrd\n", err);
629 fd = sys_open("/initrd.image",
630 O_WRONLY|O_CREAT, 0700);
631 if (fd >= 0) {
632 ssize_t written = xwrite(fd, (char *)initrd_start,
633 initrd_end - initrd_start);
635 if (written != initrd_end - initrd_start)
636 pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
637 written, initrd_end - initrd_start);
639 sys_close(fd);
640 free_initrd();
642 done:
643 #else
644 printk(KERN_INFO "Unpacking initramfs...\n");
645 err = unpack_to_rootfs((char *)initrd_start,
646 initrd_end - initrd_start);
647 if (err)
648 printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
649 free_initrd();
650 #endif
651 flush_delayed_fput();
653 * Try loading default modules from initramfs. This gives
654 * us a chance to load before device_initcalls.
656 load_default_modules();
658 return 0;
660 rootfs_initcall(populate_rootfs);