Merge to Fedora kernel-2.6.18-1.2224_FC5-vs2.0.2.2-rc4 patched with stable patch...
[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, "070701", 6)) {
186                 error("no cpio magic");
187                 return 1;
188         }
189         parse_header(collected);
190         next_header = this_header + N_ALIGN(name_len) + body_len;
191         next_header = (next_header + 3) & ~3;
192         if (dry_run) {
193                 read_into(name_buf, N_ALIGN(name_len), GotName);
194                 return 0;
195         }
196         state = SkipIt;
197         if (name_len <= 0 || name_len > PATH_MAX)
198                 return 0;
199         if (S_ISLNK(mode)) {
200                 if (body_len > PATH_MAX)
201                         return 0;
202                 collect = collected = symlink_buf;
203                 remains = N_ALIGN(name_len) + body_len;
204                 next_state = GotSymlink;
205                 state = Collect;
206                 return 0;
207         }
208         if (S_ISREG(mode) || !body_len)
209                 read_into(name_buf, N_ALIGN(name_len), GotName);
210         return 0;
211 }
212
213 static int __init do_skip(void)
214 {
215         if (this_header + count < next_header) {
216                 eat(count);
217                 return 1;
218         } else {
219                 eat(next_header - this_header);
220                 state = next_state;
221                 return 0;
222         }
223 }
224
225 static int __init do_reset(void)
226 {
227         while(count && *victim == '\0')
228                 eat(1);
229         if (count && (this_header & 3))
230                 error("broken padding");
231         return 1;
232 }
233
234 static int __init maybe_link(void)
235 {
236         if (nlink >= 2) {
237                 char *old = find_link(major, minor, ino, mode, collected);
238                 if (old)
239                         return (sys_link(old, collected) < 0) ? -1 : 1;
240         }
241         return 0;
242 }
243
244 static void __init clean_path(char *path, mode_t mode)
245 {
246         struct stat st;
247
248         if (!sys_newlstat(path, &st) && (st.st_mode^mode) & S_IFMT) {
249                 if (S_ISDIR(st.st_mode))
250                         sys_rmdir(path);
251                 else
252                         sys_unlink(path);
253         }
254 }
255
256 static __initdata int wfd;
257
258 static int __init do_name(void)
259 {
260         state = SkipIt;
261         next_state = Reset;
262         if (strcmp(collected, "TRAILER!!!") == 0) {
263                 free_hash();
264                 return 0;
265         }
266         if (dry_run)
267                 return 0;
268         clean_path(collected, mode);
269         if (S_ISREG(mode)) {
270                 int ml = maybe_link();
271                 if (ml >= 0) {
272                         int openflags = O_WRONLY|O_CREAT;
273                         if (ml != 1)
274                                 openflags |= O_TRUNC;
275                         wfd = sys_open(collected, openflags, mode);
276
277                         if (wfd >= 0) {
278                                 sys_fchown(wfd, uid, gid);
279                                 sys_fchmod(wfd, mode);
280                                 state = CopyFile;
281                         }
282                 }
283         } else if (S_ISDIR(mode)) {
284                 sys_mkdir(collected, mode);
285                 sys_chown(collected, uid, gid);
286                 sys_chmod(collected, mode);
287         } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
288                    S_ISFIFO(mode) || S_ISSOCK(mode)) {
289                 sys_unlink(collected);
290                 if (maybe_link() == 0) {
291                         sys_mknod(collected, mode, rdev);
292                         sys_chown(collected, uid, gid);
293                         sys_chmod(collected, mode);
294                 }
295         }
296         return 0;
297 }
298
299 static int __init do_copy(void)
300 {
301         if (count >= body_len) {
302                 sys_write(wfd, victim, body_len);
303                 sys_close(wfd);
304                 eat(body_len);
305                 state = SkipIt;
306                 return 0;
307         } else {
308                 sys_write(wfd, victim, count);
309                 body_len -= count;
310                 eat(count);
311                 return 1;
312         }
313 }
314
315 static int __init do_symlink(void)
316 {
317         collected[N_ALIGN(name_len) + body_len] = '\0';
318         clean_path(collected, 0);
319         sys_symlink(collected + N_ALIGN(name_len), collected);
320         sys_lchown(collected, uid, gid);
321         state = SkipIt;
322         next_state = Reset;
323         return 0;
324 }
325
326 static __initdata int (*actions[])(void) = {
327         [Start]         = do_start,
328         [Collect]       = do_collect,
329         [GotHeader]     = do_header,
330         [SkipIt]        = do_skip,
331         [GotName]       = do_name,
332         [CopyFile]      = do_copy,
333         [GotSymlink]    = do_symlink,
334         [Reset]         = do_reset,
335 };
336
337 static int __init write_buffer(char *buf, unsigned len)
338 {
339         count = len;
340         victim = buf;
341
342         while (!actions[state]())
343                 ;
344         return len - count;
345 }
346
347 static void __init flush_buffer(char *buf, unsigned len)
348 {
349         int written;
350         if (message)
351                 return;
352         while ((written = write_buffer(buf, len)) < len && !message) {
353                 char c = buf[written];
354                 if (c == '0') {
355                         buf += written;
356                         len -= written;
357                         state = Start;
358                 } else if (c == 0) {
359                         buf += written;
360                         len -= written;
361                         state = Reset;
362                 } else
363                         error("junk in compressed archive");
364         }
365 }
366
367 /*
368  * gzip declarations
369  */
370
371 #define OF(args)  args
372
373 #ifndef memzero
374 #define memzero(s, n)     memset ((s), 0, (n))
375 #endif
376
377 typedef unsigned char  uch;
378 typedef unsigned short ush;
379 typedef unsigned long  ulg;
380
381 #define WSIZE 0x8000    /* window size--must be a power of two, and */
382                         /*  at least 32K for zip's deflate method */
383
384 static uch *inbuf;
385 static uch *window;
386
387 static unsigned insize;  /* valid bytes in inbuf */
388 static unsigned inptr;   /* index of next byte to be processed in inbuf */
389 static unsigned outcnt;  /* bytes in output buffer */
390 static long bytes_out;
391
392 #define get_byte()  (inptr < insize ? inbuf[inptr++] : -1)
393                 
394 /* Diagnostic functions (stubbed out) */
395 #define Assert(cond,msg)
396 #define Trace(x)
397 #define Tracev(x)
398 #define Tracevv(x)
399 #define Tracec(c,x)
400 #define Tracecv(c,x)
401
402 #define STATIC static
403 #define INIT __init
404
405 static void __init flush_window(void);
406 static void __init error(char *m);
407 static void __init gzip_mark(void **);
408 static void __init gzip_release(void **);
409
410 #include "../lib/inflate.c"
411
412 static void __init gzip_mark(void **ptr)
413 {
414 }
415
416 static void __init gzip_release(void **ptr)
417 {
418 }
419
420 /* ===========================================================================
421  * Write the output window window[0..outcnt-1] and update crc and bytes_out.
422  * (Used for the decompressed data only.)
423  */
424 static void __init flush_window(void)
425 {
426         ulg c = crc;         /* temporary variable */
427         unsigned n;
428         uch *in, ch;
429
430         flush_buffer(window, outcnt);
431         in = window;
432         for (n = 0; n < outcnt; n++) {
433                 ch = *in++;
434                 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
435         }
436         crc = c;
437         bytes_out += (ulg)outcnt;
438         outcnt = 0;
439 }
440
441 static char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
442 {
443         int written;
444         dry_run = check_only;
445         header_buf = malloc(110);
446         symlink_buf = malloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1);
447         name_buf = malloc(N_ALIGN(PATH_MAX));
448         window = malloc(WSIZE);
449         if (!window || !header_buf || !symlink_buf || !name_buf)
450                 panic("can't allocate buffers");
451         state = Start;
452         this_header = 0;
453         message = NULL;
454         while (!message && len) {
455                 loff_t saved_offset = this_header;
456                 if (*buf == '0' && !(this_header & 3)) {
457                         state = Start;
458                         written = write_buffer(buf, len);
459                         buf += written;
460                         len -= written;
461                         continue;
462                 }
463                 if (!*buf) {
464                         buf++;
465                         len--;
466                         this_header++;
467                         continue;
468                 }
469                 this_header = 0;
470                 insize = len;
471                 inbuf = buf;
472                 inptr = 0;
473                 outcnt = 0;             /* bytes in output buffer */
474                 bytes_out = 0;
475                 crc = (ulg)0xffffffffL; /* shift register contents */
476                 makecrc();
477                 gunzip();
478                 if (state != Reset)
479                         error("junk in gzipped archive");
480                 this_header = saved_offset + inptr;
481                 buf += inptr;
482                 len -= inptr;
483         }
484         free(window);
485         free(name_buf);
486         free(symlink_buf);
487         free(header_buf);
488         return message;
489 }
490
491 extern char __initramfs_start[], __initramfs_end[];
492 #ifdef CONFIG_BLK_DEV_INITRD
493 #include <linux/initrd.h>
494 #include <linux/kexec.h>
495
496 static void __init free_initrd(void)
497 {
498 #ifdef CONFIG_KEXEC
499         unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
500         unsigned long crashk_end   = (unsigned long)__va(crashk_res.end);
501
502         /*
503          * If the initrd region is overlapped with crashkernel reserved region,
504          * free only memory that is not part of crashkernel region.
505          */
506         if (initrd_start < crashk_end && initrd_end > crashk_start) {
507                 /*
508                  * Initialize initrd memory region since the kexec boot does
509                  * not do.
510                  */
511                 memset((void *)initrd_start, 0, initrd_end - initrd_start);
512                 if (initrd_start < crashk_start)
513                         free_initrd_mem(initrd_start, crashk_start);
514                 if (initrd_end > crashk_end)
515                         free_initrd_mem(crashk_end, initrd_end);
516         } else
517 #endif
518                 free_initrd_mem(initrd_start, initrd_end);
519
520         initrd_start = 0;
521         initrd_end = 0;
522 }
523
524 #endif
525
526 void __init populate_rootfs(void)
527 {
528         char *err = unpack_to_rootfs(__initramfs_start,
529                          __initramfs_end - __initramfs_start, 0);
530         if (err)
531                 panic(err);
532 #ifdef CONFIG_BLK_DEV_INITRD
533         if (initrd_start) {
534 #ifdef CONFIG_BLK_DEV_RAM
535                 int fd;
536                 printk(KERN_INFO "checking if image is initramfs...");
537                 err = unpack_to_rootfs((char *)initrd_start,
538                         initrd_end - initrd_start, 1);
539                 if (!err) {
540                         printk(" it is\n");
541                         unpack_to_rootfs((char *)initrd_start,
542                                 initrd_end - initrd_start, 0);
543                         free_initrd();
544                         return;
545                 }
546                 printk("it isn't (%s); looks like an initrd\n", err);
547                 fd = sys_open("/initrd.image", O_WRONLY|O_CREAT, 0700);
548                 if (fd >= 0) {
549                         sys_write(fd, (char *)initrd_start,
550                                         initrd_end - initrd_start);
551                         sys_close(fd);
552                         free_initrd();
553                 }
554 #else
555                 printk(KERN_INFO "Unpacking initramfs...");
556                 err = unpack_to_rootfs((char *)initrd_start,
557                         initrd_end - initrd_start, 0);
558                 if (err)
559                         panic(err);
560                 printk(" done\n");
561                 free_initrd();
562 #endif
563         }
564 #endif
565 }