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