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