1 #define __KERNEL_SYSCALLS__
2 #include <linux/init.h>
4 #include <linux/slab.h>
5 #include <linux/types.h>
6 #include <linux/fcntl.h>
7 #include <linux/unistd.h>
8 #include <linux/delay.h>
10 static void __init error(char *x)
12 panic("populate_root: %s\n", x);
15 static void __init *malloc(int size)
17 return kmalloc(size, GFP_KERNEL);
20 static void __init free(void *where)
25 asmlinkage long sys_mkdir(char *name, int mode);
26 asmlinkage long sys_mknod(char *name, int mode, dev_t dev);
27 asmlinkage long sys_symlink(char *old, char *new);
28 asmlinkage long sys_link(char *old, char *new);
29 asmlinkage long sys_write(int fd, void *buf, ssize_t size);
30 asmlinkage long sys_chown(char *name, uid_t uid, gid_t gid);
31 asmlinkage long sys_lchown(char *name, uid_t uid, gid_t gid);
32 asmlinkage long sys_fchown(int fd, uid_t uid, gid_t gid);
33 asmlinkage long sys_chmod(char *name, mode_t mode);
34 asmlinkage long sys_fchmod(int fd, mode_t mode);
39 int ino, minor, major;
44 static inline int hash(int major, int minor, int ino)
46 unsigned long tmp = ino + minor + (major << 3);
51 static char __init *find_link(int major, int minor, int ino, char *name)
54 for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
57 if ((*p)->minor != minor)
59 if ((*p)->major != major)
63 q = (struct hash *)malloc(sizeof(struct hash));
65 error("can't allocate link hash entry");
75 static void __init free_hash(void)
78 for (p = head; p < head + 32; p++) {
87 /* cpio header parsing */
89 static __initdata unsigned long ino, major, minor, nlink;
90 static __initdata mode_t mode;
91 static __initdata unsigned long body_len, name_len;
92 static __initdata uid_t uid;
93 static __initdata gid_t gid;
94 static __initdata dev_t rdev;
96 static void __init parse_header(char *s)
98 unsigned long parsed[12];
103 for (i = 0, s += 6; i < 12; i++, s += 8) {
105 parsed[i] = simple_strtoul(buf, NULL, 16);
112 body_len = parsed[6];
115 rdev = MKDEV(parsed[9], parsed[10]);
116 name_len = parsed[11];
134 loff_t this_header, next_header;
136 static inline void eat(unsigned n)
143 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
145 static __initdata char *collected;
146 static __initdata int remains;
147 static __initdata char *collect;
149 static void __init read_into(char *buf, unsigned size, enum state next)
156 collect = collected = buf;
163 static __initdata char *header_buf, *symlink_buf, *name_buf;
165 static int __init do_start(void)
167 read_into(header_buf, 110, GotHeader);
171 static int __init do_collect(void)
173 unsigned n = remains;
176 memcpy(collect, victim, n);
185 static int __init do_header(void)
187 parse_header(collected);
188 next_header = this_header + N_ALIGN(name_len) + body_len;
189 next_header = (next_header + 3) & ~3;
190 if (name_len <= 0 || name_len > PATH_MAX)
192 else if (S_ISLNK(mode)) {
193 if (body_len > PATH_MAX)
196 collect = collected = symlink_buf;
197 remains = N_ALIGN(name_len) + body_len;
198 next_state = GotSymlink;
201 } else if (body_len && !S_ISREG(mode))
204 read_into(name_buf, N_ALIGN(name_len), GotName);
208 static int __init do_skip(void)
210 if (this_header + count <= next_header) {
214 eat(next_header - this_header);
220 static int __init do_reset(void)
222 while(count && *victim == '\0')
224 if (count && (this_header & 3))
225 error("broken padding");
229 static int __init maybe_link(void)
232 char *old = find_link(major, minor, ino, collected);
234 return (sys_link(old, collected) < 0) ? -1 : 1;
239 static __initdata int wfd;
241 static int __init do_name(void)
245 if (strcmp(collected, "TRAILER!!!") == 0) {
250 printk(KERN_INFO "-> %s\n", collected);
252 if (maybe_link() >= 0) {
253 wfd = sys_open(collected, O_WRONLY|O_CREAT, mode);
255 sys_fchown(wfd, uid, gid);
256 sys_fchmod(wfd, mode);
260 } else if (S_ISDIR(mode)) {
261 sys_mkdir(collected, mode);
262 sys_chown(collected, uid, gid);
263 } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
264 S_ISFIFO(mode) || S_ISSOCK(mode)) {
265 if (maybe_link() == 0) {
266 sys_mknod(collected, mode, rdev);
267 sys_chown(collected, uid, gid);
270 panic("populate_root: bogus mode: %o\n", mode);
274 static int __init do_copy(void)
276 if (count >= body_len) {
277 sys_write(wfd, victim, body_len);
283 sys_write(wfd, victim, count);
290 static int __init do_symlink(void)
292 collected[N_ALIGN(name_len) + body_len] = '\0';
293 sys_symlink(collected + N_ALIGN(name_len), collected);
294 sys_lchown(collected, uid, gid);
300 static __initdata int (*actions[])(void) = {
302 [Collect] do_collect,
303 [GotHeader] do_header,
307 [GotSymlink] do_symlink,
311 static int __init write_buffer(char *buf, unsigned len)
316 while (!actions[state]())
321 static void __init flush_buffer(char *buf, unsigned len)
324 while ((written = write_buffer(buf, len)) < len) {
325 char c = buf[written];
332 error("junk in compressed archive");
340 #define OF(args) args
343 #define memzero(s, n) memset ((s), 0, (n))
346 typedef unsigned char uch;
347 typedef unsigned short ush;
348 typedef unsigned long ulg;
350 #define WSIZE 0x8000 /* window size--must be a power of two, and */
351 /* at least 32K for zip's deflate method */
356 static unsigned insize; /* valid bytes in inbuf */
357 static unsigned inptr; /* index of next byte to be processed in inbuf */
358 static unsigned outcnt; /* bytes in output buffer */
359 static long bytes_out;
361 #define get_byte() (inptr < insize ? inbuf[inptr++] : -1)
363 /* Diagnostic functions (stubbed out) */
364 #define Assert(cond,msg)
371 #define STATIC static
373 static void flush_window(void);
374 static void error(char *m);
375 static void gzip_mark(void **);
376 static void gzip_release(void **);
378 #include "../lib/inflate.c"
380 static void __init gzip_mark(void **ptr)
384 static void __init gzip_release(void **ptr)
388 /* ===========================================================================
389 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
390 * (Used for the decompressed data only.)
392 static void __init flush_window(void)
394 ulg c = crc; /* temporary variable */
398 flush_buffer(window, outcnt);
400 for (n = 0; n < outcnt; n++) {
402 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
405 bytes_out += (ulg)outcnt;
409 static void __init unpack_to_rootfs(char *buf, unsigned len)
412 header_buf = malloc(110);
413 symlink_buf = malloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1);
414 name_buf = malloc(N_ALIGN(PATH_MAX));
415 window = malloc(WSIZE);
416 if (!window || !header_buf || !symlink_buf || !name_buf)
417 error("can't allocate buffers");
421 loff_t saved_offset = this_header;
422 if (*buf == '0' && !(this_header & 3)) {
424 written = write_buffer(buf, len);
438 outcnt = 0; /* bytes in output buffer */
440 crc = (ulg)0xffffffffL; /* shift register contents */
443 error("ungzip failed");
445 error("junk in gzipped archive");
446 this_header = saved_offset + inptr;
456 extern unsigned long __initramfs_start, __initramfs_end;
458 void __init populate_rootfs(void)
460 unpack_to_rootfs((void *) &__initramfs_start,
461 &__initramfs_end - &__initramfs_start);