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
10static __initdata char *message;
11static void __init error(char *x)
12{
13 if (!message)
14 message = x;
15}
16
17static void __init *malloc(size_t size)
18{
19 return kmalloc(size, GFP_KERNEL);
20}
21
22static void __init free(void *where)
23{
24 kfree(where);
25}
26
27
28
29static struct hash {
30 int ino, minor, major;
31 struct hash *next;
32 char *name;
33} *head[32];
34
35static inline int hash(int major, int minor, int ino)
36{
37 unsigned long tmp = ino + minor + (major << 3);
38 tmp += tmp >> 5;
39 return tmp & 31;
40}
41
42static char __init *find_link(int major, int minor, int ino, char *name)
43{
44 struct hash **p, *q;
45 for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
46 if ((*p)->ino != ino)
47 continue;
48 if ((*p)->minor != minor)
49 continue;
50 if ((*p)->major != major)
51 continue;
52 return (*p)->name;
53 }
54 q = (struct hash *)malloc(sizeof(struct hash));
55 if (!q)
56 panic("can't allocate link hash entry");
57 q->ino = ino;
58 q->minor = minor;
59 q->major = major;
60 q->name = name;
61 q->next = NULL;
62 *p = q;
63 return NULL;
64}
65
66static void __init free_hash(void)
67{
68 struct hash **p, *q;
69 for (p = head; p < head + 32; p++) {
70 while (*p) {
71 q = *p;
72 *p = q->next;
73 free(q);
74 }
75 }
76}
77
78
79
80static __initdata unsigned long ino, major, minor, nlink;
81static __initdata mode_t mode;
82static __initdata unsigned long body_len, name_len;
83static __initdata uid_t uid;
84static __initdata gid_t gid;
85static __initdata unsigned rdev;
86
87static void __init parse_header(char *s)
88{
89 unsigned long parsed[12];
90 char buf[9];
91 int i;
92
93 buf[8] = '\0';
94 for (i = 0, s += 6; i < 12; i++, s += 8) {
95 memcpy(buf, s, 8);
96 parsed[i] = simple_strtoul(buf, NULL, 16);
97 }
98 ino = parsed[0];
99 mode = parsed[1];
100 uid = parsed[2];
101 gid = parsed[3];
102 nlink = parsed[4];
103 body_len = parsed[6];
104 major = parsed[7];
105 minor = parsed[8];
106 rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
107 name_len = parsed[11];
108}
109
110
111
112static __initdata enum state {
113 Start,
114 Collect,
115 GotHeader,
116 SkipIt,
117 GotName,
118 CopyFile,
119 GotSymlink,
120 Reset
121} state, next_state;
122
123static __initdata char *victim;
124static __initdata unsigned count;
125static __initdata loff_t this_header, next_header;
126
127static __initdata int dry_run;
128
129static inline void eat(unsigned n)
130{
131 victim += n;
132 this_header += n;
133 count -= n;
134}
135
136#define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
137
138static __initdata char *collected;
139static __initdata int remains;
140static __initdata char *collect;
141
142static 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
156static __initdata char *header_buf, *symlink_buf, *name_buf;
157
158static int __init do_start(void)
159{
160 read_into(header_buf, 110, GotHeader);
161 return 0;
162}
163
164static 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
178static 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
208static 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
220static 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
229static 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
239static __initdata int wfd;
240
241static int __init do_name(void)
242{
243 state = SkipIt;
244 next_state = Start;
245 if (strcmp(collected, "TRAILER!!!") == 0) {
246 free_hash();
247 next_state = Reset;
248 return 0;
249 }
250 if (dry_run)
251 return 0;
252 if (S_ISREG(mode)) {
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 if (maybe_link() == 0) {
268 sys_mknod(collected, mode, rdev);
269 sys_chown(collected, uid, gid);
270 sys_chmod(collected, mode);
271 }
272 }
273 return 0;
274}
275
276static int __init do_copy(void)
277{
278 if (count >= body_len) {
279 sys_write(wfd, victim, body_len);
280 sys_close(wfd);
281 eat(body_len);
282 state = SkipIt;
283 return 0;
284 } else {
285 sys_write(wfd, victim, count);
286 body_len -= count;
287 eat(count);
288 return 1;
289 }
290}
291
292static int __init do_symlink(void)
293{
294 collected[N_ALIGN(name_len) + body_len] = '\0';
295 sys_symlink(collected + N_ALIGN(name_len), collected);
296 sys_lchown(collected, uid, gid);
297 state = SkipIt;
298 next_state = Start;
299 return 0;
300}
301
302static __initdata int (*actions[])(void) = {
303 [Start] = do_start,
304 [Collect] = do_collect,
305 [GotHeader] = do_header,
306 [SkipIt] = do_skip,
307 [GotName] = do_name,
308 [CopyFile] = do_copy,
309 [GotSymlink] = do_symlink,
310 [Reset] = do_reset,
311};
312
313static int __init write_buffer(char *buf, unsigned len)
314{
315 count = len;
316 victim = buf;
317
318 while (!actions[state]())
319 ;
320 return len - count;
321}
322
323static void __init flush_buffer(char *buf, unsigned len)
324{
325 int written;
326 if (message)
327 return;
328 while ((written = write_buffer(buf, len)) < len && !message) {
329 char c = buf[written];
330 if (c == '0') {
331 buf += written;
332 len -= written;
333 state = Start;
334 } else
335 error("junk in compressed archive");
336 }
337}
338
339
340
341
342
343#define OF(args) args
344
345#ifndef memzero
346#define memzero(s, n) memset ((s), 0, (n))
347#endif
348
349typedef unsigned char uch;
350typedef unsigned short ush;
351typedef unsigned long ulg;
352
353#define WSIZE 0x8000
354
355
356static uch *inbuf;
357static uch *window;
358
359static unsigned insize;
360static unsigned inptr;
361static unsigned outcnt;
362static long bytes_out;
363
364#define get_byte() (inptr < insize ? inbuf[inptr++] : -1)
365
366
367#define Assert(cond,msg)
368#define Trace(x)
369#define Tracev(x)
370#define Tracevv(x)
371#define Tracec(c,x)
372#define Tracecv(c,x)
373
374#define STATIC static
375
376static void flush_window(void);
377static void error(char *m);
378static void gzip_mark(void **);
379static void gzip_release(void **);
380
381#include "../lib/inflate.c"
382
383static void __init gzip_mark(void **ptr)
384{
385}
386
387static void __init gzip_release(void **ptr)
388{
389}
390
391
392
393
394
395static void __init flush_window(void)
396{
397 ulg c = crc;
398 unsigned n;
399 uch *in, ch;
400
401 flush_buffer(window, outcnt);
402 in = window;
403 for (n = 0; n < outcnt; n++) {
404 ch = *in++;
405 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
406 }
407 crc = c;
408 bytes_out += (ulg)outcnt;
409 outcnt = 0;
410}
411
412char * __init unpack_to_rootfs(char *buf, unsigned len, int check_only)
413{
414 int written;
415 dry_run = check_only;
416 header_buf = malloc(110);
417 symlink_buf = malloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1);
418 name_buf = malloc(N_ALIGN(PATH_MAX));
419 window = malloc(WSIZE);
420 if (!window || !header_buf || !symlink_buf || !name_buf)
421 panic("can't allocate buffers");
422 state = Start;
423 this_header = 0;
424 message = NULL;
425 while (!message && len) {
426 loff_t saved_offset = this_header;
427 if (*buf == '0' && !(this_header & 3)) {
428 state = Start;
429 written = write_buffer(buf, len);
430 buf += written;
431 len -= written;
432 continue;
433 }
434 if (!*buf) {
435 buf++;
436 len--;
437 this_header++;
438 continue;
439 }
440 this_header = 0;
441 insize = len;
442 inbuf = buf;
443 inptr = 0;
444 outcnt = 0;
445 bytes_out = 0;
446 crc = (ulg)0xffffffffL;
447 makecrc();
448 if (gunzip())
449 message = "ungzip failed";
450 if (state != Reset)
451 error("junk in gzipped archive");
452 this_header = saved_offset + inptr;
453 buf += inptr;
454 len -= inptr;
455 }
456 free(window);
457 free(name_buf);
458 free(symlink_buf);
459 free(header_buf);
460 return message;
461}
462
463extern char __initramfs_start, __initramfs_end;
464#ifdef CONFIG_BLK_DEV_INITRD
465#include <linux/initrd.h>
466#endif
467
468void __init populate_rootfs(void)
469{
470 char *err = unpack_to_rootfs(&__initramfs_start,
471 &__initramfs_end - &__initramfs_start, 0);
472 if (err)
473 panic(err);
474#ifdef CONFIG_BLK_DEV_INITRD
475 if (initrd_start) {
476 int fd;
477 printk(KERN_INFO "checking if image is initramfs...");
478 err = unpack_to_rootfs((char *)initrd_start,
479 initrd_end - initrd_start, 1);
480 if (!err) {
481 printk(" it is\n");
482 unpack_to_rootfs((char *)initrd_start,
483 initrd_end - initrd_start, 0);
484 free_initrd_mem(initrd_start, initrd_end);
485 return;
486 }
487 printk("it isn't (%s); looks like an initrd\n", err);
488 fd = sys_open("/initrd.image", O_WRONLY|O_CREAT, 700);
489 if (fd >= 0) {
490 sys_write(fd, (char *)initrd_start,
491 initrd_end - initrd_start);
492 sys_close(fd);
493 free_initrd_mem(initrd_start, initrd_end);
494 }
495 }
496#endif
497}
498