e626a75840dbbaa91fe61f65a33027d5067ae7b9
[linux-2.6.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/syscalls.h>
9
10 static __initdata char *message;
11 static void __init error(char *x)
12 {
13         if (!message)
14                 message = x;
15 }
16
17 static void __init *malloc(size_t size)
18 {
19         return kmalloc(size, GFP_KERNEL);
20 }
21
22 static void __init free(void *where)
23 {
24         kfree(where);
25 }
26
27 /* link hash */
28
29 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
30
31 static __initdata struct hash {
32         int ino, minor, major;
33         mode_t mode;
34         struct hash *next;
35         char name[N_ALIGN(PATH_MAX)];
36 } *head[32];
37
38 static inline int hash(int major, int minor, int ino)
39 {
40         unsigned long tmp = ino + minor + (major << 3);
41         tmp += tmp >> 5;
42         return tmp & 31;
43 }
44
45 static char __init *find_link(int major, int minor, int ino,
46                               mode_t mode, char *name)
47 {
48         struct hash **p, *q;
49         for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
50                 if ((*p)->ino != ino)
51                         continue;
52                 if ((*p)->minor != minor)
53                         continue;
54                 if ((*p)->major != major)
55                         continue;
56                 if (((*p)->mode ^ mode) & S_IFMT)
57                         continue;
58                 return (*p)->name;
59         }
60         q = (struct hash *)malloc(sizeof(struct hash));
61         if (!q)
62                 panic("can't allocate link hash entry");
63         q->major = major;
64         q->minor = minor;
65         q->ino = ino;
66         q->mode = mode;
67         strcpy(q->name, name);
68         q->next = NULL;
69         *p = q;
70         return NULL;
71 }
72
73 static void __init free_hash(void)
74 {
75         struct hash **p, *q;
76         for (p = head; p < head + 32; p++) {
77                 while (*p) {
78                         q = *p;
79                         *p = q->next;
80                         free(q);
81                 }
82         }
83 }
84
85 /* cpio header parsing */
86
87 static __initdata unsigned long ino, major, minor, nlink;
88 static __initdata mode_t mode;
89 static __initdata unsigned long body_len, name_len;
90 static __initdata uid_t uid;
91 static __initdata gid_t gid;
92 static __initdata unsigned rdev;
93
94 static void __init parse_header(char *s)
95 {
96         unsigned long parsed[12];
97         char buf[9];
98         int i;
99
100         buf[8] = '\0';
101         for (i = 0, s += 6; i < 12; i++, s += 8) {
102                 memcpy(buf, s, 8);
103                 parsed[i] = simple_strtoul(buf, NULL, 16);
104         }
105         ino = parsed[0];
106         mode = parsed[1];
107         uid = parsed[2];
108         gid = parsed[3];
109         nlink = parsed[4];
110         body_len = parsed[6];
111         major = parsed[7];
112         minor = parsed[8];
113         rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
114         name_len = parsed[11];
115 }
116
117 /* FSM */
118
119 static __initdata enum state {
120         Start,
121         Collect,
122         GotHeader,
123         SkipIt,
124         GotName,
125         CopyFile,
126         GotSymlink,
127         Reset
128 } state, next_state;
129
130 static __initdata char *victim;
131 static __initdata unsigned count;
132 static __initdata loff_t this_header, next_header;
133
134 static __initdata int dry_run;
135
136 static inline void eat(unsigned n)
137 {
138         victim += n;
139         this_header += n;
140         count -= n;
141 }
142
143 static __initdata char *collected;
144 static __initdata int remains;
145 static __initdata char *collect;
146
147 static void __init read_into(char *buf, unsigned size, enum state next)
148 {
149         if (count >= size) {
150                 collected = victim;
151                 eat(size);
152                 state = next;
153         } else {
154                 collect = collected = buf;
155                 remains = size;
156                 next_state = next;
157                 state = Collect;
158         }
159 }
160
161 static __initdata char *header_buf, *symlink_buf, *name_buf;
162
163 static int __init do_start(void)
164 {
165         read_into(header_buf, 110, GotHeader);
166         return 0;
167 }
168
169 static int __init do_collect(void)
170 {
171         unsigned n = remains;
172         if (count < n)
173                 n = count;
174         memcpy(collect, victim, n);
175         eat(n);
176         collect += n;
177         if ((remains -= n) != 0)
178                 return 1;
179         state = next_state;
180         return 0;
181 }
182
183 static int __init do_header(void)
184 {
185         if (memcmp(collected, "070707", 6)==0) {
186                 error("incorrect cpio method used: use -H newc option");
187                 return 1;
188         }
189         if (memcmp(collected, "070701", 6)) {
190                 error("no cpio magic");
191                 return 1;
192         }
193         parse_header(collected);
194         next_header = this_header + N_ALIGN(name_len) + body_len;
195         next_header = (next_header + 3) & ~3;
196         if (dry_run) {
197                 read_into(name_buf, N_ALIGN(name_len), GotName);
198                 return 0;
199         }
200         state = SkipIt;
201         if (name_len <= 0 || name_len > PATH_MAX)
202                 return 0;
203         if (S_ISLNK(mode)) {
204                 if (body_len > PATH_MAX)
205                         return 0;
206                 collect = collected = symlink_buf;
207                 remains = N_ALIGN(name_len) + body_len;
208                 next_state = GotSymlink;
209                 state = Collect;
210                 return 0;
211         }
212         if (S_ISREG(mode) || !body_len)
213                 read_into(name_buf, N_ALIGN(name_len), GotName);
214         return 0;
215 }
216
217 static int __init do_skip(void)
218 {
219         if (this_header + count < next_header) {
220                 eat(count);
221                 return 1;
222         } else {
223                 eat(next_header - this_header);
224                 state = next_state;
225                 return 0;
226         }
227 }
228
229 static int __init do_reset(void)
230 {
231         while(count && *victim == '\0')
232                 eat(1);
233         if (count && (this_header & 3))
234                 error("broken padding");
235         return 1;
236 }
237
238 static int __init maybe_link(void)
239 {
240         if (nlink >= 2) {
241                 char *old = find_link(major, minor, ino, mode, collected);
242                 if (old)
243                         return (sys_link(old, collected) < 0) ? -1 : 1;
244         }
245         return 0;
246 }
247
248 static void __init clean_path(char *path, mode_t mode)
249 {
250         struct stat st;
251
252         if (!sys_newlstat(path, &st) && (st.st_mode^mode) & S_IFMT) {
253                 if (S_ISDIR(st.st_mode))
254                         sys_rmdir(path);
255                 else
256                         sys_unlink(path);
257         }
258 }
259
260 static __initdata int wfd;
261
262 static int __init do_name(void)
263 {
264         state = SkipIt;
265         next_state = Reset;
266         if (strcmp(collected, "TRAILER!!!") == 0) {
267                 free_hash();
268                 return 0;
269         }
270         if (dry_run)
271                 return 0;
272         clean_path(collected, mode);
273         if (S_ISREG(mode)) {
274                 int ml = maybe_link();
275                 if (ml >= 0) {
276                         int openflags = O_WRONLY|O_CREAT;
277                         if (ml != 1)
278                                 openflags |= O_TRUNC;
279                         wfd = sys_open(collected, openflags, mode);
280
281                         if (wfd >= 0) {
282                                 sys_fchown(wfd, uid, gid);
283                                 sys_fchmod(wfd, mode);
284                                 state = CopyFile;
285                         }
286                 }
287         } else if (S_ISDIR(mode)) {
288                 sys_mkdir(collected, mode);
289                 sys_chown(collected, uid, gid);
290                 sys_chmod(collected, mode);
291         } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
292                    S_ISFIFO(mode) || S_ISSOCK(mode)) {
293                 sys_unlink(collected);
294                 if (maybe_link() == 0) {
295                         sys_mknod(collected, mode, rdev);
296                         sys_chown(collected, uid, gid);
297                         sys_chmod(collected, mode);
298                 }
299         }
300         return 0;
301 }
302
303 static int __init do_copy(void)
304 {
305         if (count >= body_len) {
306                 sys_write(wfd, victim, body_len);
307                 sys_close(wfd);
308                 eat(body_len);
309                 state = SkipIt;
310                 return 0;
311         } else {
312                 sys_write(wfd, victim, count);
313                 body_len -= count;
314                 eat(count);
315                 return 1;
316         }
317 }
318
319 static int __init do_symlink(void)
320 {
321         collected[N_ALIGN(name_len) + body_len] = '\0';
322         clean_path(collected, 0);
323         sys_symlink(collected + N_ALIGN(name_len), collected);
324         sys_lchown(collected, uid, gid);
325         state = SkipIt;
326         next_state = Reset;
327         return 0;
328 }
329
330 static __initdata int (*actions[])(void) = {
331         [Start]         = do_start,
332         [Collect]       = do_collect,
333         [GotHeader]     = do_header,
334         [SkipIt]        = do_skip,
335         [GotName]       = do_name,
336         [CopyFile]      = do_copy,
337         [GotSymlink]    = do_symlink,
338         [Reset]         = do_reset,
339 };
340
341 static int __init write_buffer(char *buf, unsigned len)
342 {
343         count = len;
344         victim = buf;
345
346         while (!actions[state]())
347                 ;
348         return len - count;
349 }
350
351 static void __init flush_buffer(char *buf, unsigned len)
352 {
353         int written;
354         if (message)
355                 return;
356         while ((written = write_buffer(buf, len)) < len && !message) {
357                 char c = buf[written];
358                 if (c == '0') {
359                         buf += written;
360                         len -= written;
361                         state = Start;
362                 } else if (c == 0) {
363                         buf += written;
364                         len -= written;
365                         state = Reset;
366                 } else
367                         error("junk in compressed archive");
368         }
369 }
370
371 /*
372  * gzip declarations
373  */
374
375 #define OF(args)  args
376
377 #ifndef memzero
378 #define memzero(s, n)     memset ((s), 0, (n))
379 #endif
380
381 typedef unsigned char  uch;
382 typedef unsigned short ush;
383 typedef unsigned long  ulg;
384
385 #define WSIZE 0x8000    /* window size--must be a power of two, and */
386                         /*  at least 32K for zip's deflate method */
387
388 static uch *inbuf;
389 static uch *window;
390
391 static unsigned insize;  /* valid bytes in inbuf */
392 static unsigned inptr;   /* index of next byte to be processed in inbuf */
393 static unsigned outcnt;  /* bytes in output buffer */
394 static long bytes_out;
395
396 #define get_byte()  (inptr < insize ? inbuf[inptr++] : -1)
397                 
398 /* Diagnostic functions (stubbed out) */
399 #define Assert(cond,msg)
400 #define Trace(x)
401 #define Tracev(x)
402 #define Tracevv(x)
403 #define Tracec(c,x)
404 #define Tracecv(c,x)
405
406 #define STATIC static
407 #define INIT __init
408
409 static void __init flush_window(void);
410 static void __init error(char *m);
411 static void __init gzip_mark(void **);
412 static void __init gzip_release(void **);
413
414 #include "../lib/inflate.c"
415
416 static void __init gzip_mark(void **ptr)
417 {
418 }
419
420 static void __init gzip_release(void **ptr)
421 {
422 }
423
424 /* ===========================================================================
425  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
426  * (Used for the decompressed data only.)
427  */
428 static void __init flush_window(void)
429 {
430         ulg c = crc;         /* temporary variable */
431         unsigned n;
432         uch *in, ch;
433
434         flush_buffer(window, outcnt);
435         in = window;
436         for (n = 0; n < outcnt; n++) {
437                 ch = *in++;
438                 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
439         }
440         crc = c;
441         bytes_out += (ulg)outcnt;
442         outcnt = 0;
443 }
444
445 static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
446 {
447         int written;
448         dry_run = check_only;
449         header_buf = malloc(110);
450         symlink_buf = malloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1);
451         name_buf = malloc(N_ALIGN(PATH_MAX));
452         window = malloc(WSIZE);
453         if (!window || !header_buf || !symlink_buf || !name_buf)
454                 panic("can't allocate buffers");
455         state = Start;
456         this_header = 0;
457         message = NULL;
458         while (!message && len) {
459                 loff_t saved_offset = this_header;
460                 if (*buf == '0' && !(this_header & 3)) {
461                         state = Start;
462                         written = write_buffer(buf, len);
463                         buf += written;
464                         len -= written;
465                         continue;
466                 }
467                 if (!*buf) {
468                         buf++;
469                         len--;
470                         this_header++;
471                         continue;
472                 }
473                 this_header = 0;
474                 insize = len;
475                 inbuf = buf;
476                 inptr = 0;
477                 outcnt = 0;             /* bytes in output buffer */
478                 bytes_out = 0;
479                 crc = (ulg)0xffffffffL; /* shift register contents */
480                 makecrc();
481                 gunzip();
482                 if (state != Reset)
483                         error("junk in gzipped archive");
484                 this_header = saved_offset + inptr;
485                 buf += inptr;
486                 len -= inptr;
487         }
488         free(window);
489         free(name_buf);
490         free(symlink_buf);
491         free(header_buf);
492         return message;
493 }
494
495 extern char __initramfs_start[], __initramfs_end[];
496 #ifdef CONFIG_BLK_DEV_INITRD
497 #include <linux/initrd.h>
498 #include <linux/kexec.h>
499
500 static void __init free_initrd(void)
501 {
502 #ifdef CONFIG_KEXEC
503         unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
504         unsigned long crashk_end   = (unsigned long)__va(crashk_res.end);
505
506         /*
507          * If the initrd region is overlapped with crashkernel reserved region,
508          * free only memory that is not part of crashkernel region.
509          */
510         if (initrd_start < crashk_end && initrd_end > crashk_start) {
511                 /*
512                  * Initialize initrd memory region since the kexec boot does
513                  * not do.
514                  */
515                 memset((void *)initrd_start, 0, initrd_end - initrd_start);
516                 if (initrd_start < crashk_start)
517                         free_initrd_mem(initrd_start, crashk_start);
518                 if (initrd_end > crashk_end)
519                         free_initrd_mem(crashk_end, initrd_end);
520         } else
521 #endif
522                 free_initrd_mem(initrd_start, initrd_end);
523
524         initrd_start = 0;
525         initrd_end = 0;
526 }
527
528 #endif
529
530 static int __init populate_rootfs(void)
531 {
532         char *err = unpack_to_rootfs(__initramfs_start,
533                          __initramfs_end - __initramfs_start, 0);
534         if (err)
535                 panic(err);
536 #ifdef CONFIG_BLK_DEV_INITRD
537         if (initrd_start) {
538 #ifdef CONFIG_BLK_DEV_RAM
539                 int fd;
540                 printk(KERN_INFO "checking if image is initramfs...");
541                 err = unpack_to_rootfs((char *)initrd_start,
542                         initrd_end - initrd_start, 1);
543                 if (!err) {
544                         printk(" it is\n");
545                         unpack_to_rootfs((char *)initrd_start,
546                                 initrd_end - initrd_start, 0);
547                         free_initrd();
548                         return 0;
549                 }
550                 printk("it isn't (%s); looks like an initrd\n", err);
551                 fd = sys_open("/initrd.image", O_WRONLY|O_CREAT, 0700);
552                 if (fd >= 0) {
553                         sys_write(fd, (char *)initrd_start,
554                                         initrd_end - initrd_start);
555                         sys_close(fd);
556                         free_initrd();
557                 }
558 #else
559                 printk(KERN_INFO "Unpacking initramfs...");
560                 err = unpack_to_rootfs((char *)initrd_start,
561                         initrd_end - initrd_start, 0);
562                 if (err)
563                         panic(err);
564                 printk(" done\n");
565                 free_initrd();
566 #endif
567         }
568 #endif
569         return 0;
570 }
571 rootfs_initcall(populate_rootfs);