Xilinx: ARM: Frame buffer driver release from Xylon (4th July)
[linux-2.6-xlnx.git] / usr / gen_init_cpio.c
blob148124626ff602d87fff6f044057353c9140a41c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <time.h>
8 #include <fcntl.h>
9 #include <errno.h>
10 #include <ctype.h>
11 #include <limits.h>
14 * Original work by Jeff Garzik
16 * External file lists, symlink, pipe and fifo support by Thayne Harbaugh
17 * Hard link support by Luciano Rocha
20 #ifdef __CYGWIN32__
21 #undef PATH_MAX
22 #define PATH_MAX 259
23 #endif
25 #define xstr(s) #s
26 #define str(s) xstr(s)
28 static unsigned int offset;
29 static unsigned int ino = 721;
30 static time_t default_mtime;
32 struct file_handler {
33 const char *type;
34 int (*handler)(const char *line);
37 static void push_string(const char *name)
39 unsigned int name_len = strlen(name) + 1;
41 fputs(name, stdout);
42 putchar(0);
43 offset += name_len;
46 static void push_pad (void)
48 while (offset & 3) {
49 putchar(0);
50 offset++;
54 static void push_rest(const char *name)
56 unsigned int name_len = strlen(name) + 1;
57 unsigned int tmp_ofs;
59 fputs(name, stdout);
60 putchar(0);
61 offset += name_len;
63 tmp_ofs = name_len + 110;
64 while (tmp_ofs & 3) {
65 putchar(0);
66 offset++;
67 tmp_ofs++;
71 static void push_hdr(const char *s)
73 fputs(s, stdout);
74 offset += 110;
77 static void cpio_trailer(void)
79 char s[256];
80 const char name[] = "TRAILER!!!";
82 sprintf(s, "%s%08X%08X%08lX%08lX%08X%08lX"
83 "%08X%08X%08X%08X%08X%08X%08X",
84 "070701", /* magic */
85 0, /* ino */
86 0, /* mode */
87 (long) 0, /* uid */
88 (long) 0, /* gid */
89 1, /* nlink */
90 (long) 0, /* mtime */
91 0, /* filesize */
92 0, /* major */
93 0, /* minor */
94 0, /* rmajor */
95 0, /* rminor */
96 (unsigned)strlen(name)+1, /* namesize */
97 0); /* chksum */
98 push_hdr(s);
99 push_rest(name);
101 while (offset % 512) {
102 putchar(0);
103 offset++;
107 static int cpio_mkslink(const char *name, const char *target,
108 unsigned int mode, uid_t uid, gid_t gid)
110 char s[256];
112 if (name[0] == '/')
113 name++;
114 sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
115 "%08X%08X%08X%08X%08X%08X%08X",
116 "070701", /* magic */
117 ino++, /* ino */
118 S_IFLNK | mode, /* mode */
119 (long) uid, /* uid */
120 (long) gid, /* gid */
121 1, /* nlink */
122 (long) default_mtime, /* mtime */
123 (unsigned)strlen(target)+1, /* filesize */
124 3, /* major */
125 1, /* minor */
126 0, /* rmajor */
127 0, /* rminor */
128 (unsigned)strlen(name) + 1,/* namesize */
129 0); /* chksum */
130 push_hdr(s);
131 push_string(name);
132 push_pad();
133 push_string(target);
134 push_pad();
135 return 0;
138 static int cpio_mkslink_line(const char *line)
140 char name[PATH_MAX + 1];
141 char target[PATH_MAX + 1];
142 unsigned int mode;
143 int uid;
144 int gid;
145 int rc = -1;
147 if (5 != sscanf(line, "%" str(PATH_MAX) "s %" str(PATH_MAX) "s %o %d %d", name, target, &mode, &uid, &gid)) {
148 fprintf(stderr, "Unrecognized dir format '%s'", line);
149 goto fail;
151 rc = cpio_mkslink(name, target, mode, uid, gid);
152 fail:
153 return rc;
156 static int cpio_mkgeneric(const char *name, unsigned int mode,
157 uid_t uid, gid_t gid)
159 char s[256];
161 if (name[0] == '/')
162 name++;
163 sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
164 "%08X%08X%08X%08X%08X%08X%08X",
165 "070701", /* magic */
166 ino++, /* ino */
167 mode, /* mode */
168 (long) uid, /* uid */
169 (long) gid, /* gid */
170 2, /* nlink */
171 (long) default_mtime, /* mtime */
172 0, /* filesize */
173 3, /* major */
174 1, /* minor */
175 0, /* rmajor */
176 0, /* rminor */
177 (unsigned)strlen(name) + 1,/* namesize */
178 0); /* chksum */
179 push_hdr(s);
180 push_rest(name);
181 return 0;
184 enum generic_types {
185 GT_DIR,
186 GT_PIPE,
187 GT_SOCK
190 struct generic_type {
191 const char *type;
192 mode_t mode;
195 static struct generic_type generic_type_table[] = {
196 [GT_DIR] = {
197 .type = "dir",
198 .mode = S_IFDIR
200 [GT_PIPE] = {
201 .type = "pipe",
202 .mode = S_IFIFO
204 [GT_SOCK] = {
205 .type = "sock",
206 .mode = S_IFSOCK
210 static int cpio_mkgeneric_line(const char *line, enum generic_types gt)
212 char name[PATH_MAX + 1];
213 unsigned int mode;
214 int uid;
215 int gid;
216 int rc = -1;
218 if (4 != sscanf(line, "%" str(PATH_MAX) "s %o %d %d", name, &mode, &uid, &gid)) {
219 fprintf(stderr, "Unrecognized %s format '%s'",
220 line, generic_type_table[gt].type);
221 goto fail;
223 mode |= generic_type_table[gt].mode;
224 rc = cpio_mkgeneric(name, mode, uid, gid);
225 fail:
226 return rc;
229 static int cpio_mkdir_line(const char *line)
231 return cpio_mkgeneric_line(line, GT_DIR);
234 static int cpio_mkpipe_line(const char *line)
236 return cpio_mkgeneric_line(line, GT_PIPE);
239 static int cpio_mksock_line(const char *line)
241 return cpio_mkgeneric_line(line, GT_SOCK);
244 static int cpio_mknod(const char *name, unsigned int mode,
245 uid_t uid, gid_t gid, char dev_type,
246 unsigned int maj, unsigned int min)
248 char s[256];
250 if (dev_type == 'b')
251 mode |= S_IFBLK;
252 else
253 mode |= S_IFCHR;
255 if (name[0] == '/')
256 name++;
257 sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
258 "%08X%08X%08X%08X%08X%08X%08X",
259 "070701", /* magic */
260 ino++, /* ino */
261 mode, /* mode */
262 (long) uid, /* uid */
263 (long) gid, /* gid */
264 1, /* nlink */
265 (long) default_mtime, /* mtime */
266 0, /* filesize */
267 3, /* major */
268 1, /* minor */
269 maj, /* rmajor */
270 min, /* rminor */
271 (unsigned)strlen(name) + 1,/* namesize */
272 0); /* chksum */
273 push_hdr(s);
274 push_rest(name);
275 return 0;
278 static int cpio_mknod_line(const char *line)
280 char name[PATH_MAX + 1];
281 unsigned int mode;
282 int uid;
283 int gid;
284 char dev_type;
285 unsigned int maj;
286 unsigned int min;
287 int rc = -1;
289 if (7 != sscanf(line, "%" str(PATH_MAX) "s %o %d %d %c %u %u",
290 name, &mode, &uid, &gid, &dev_type, &maj, &min)) {
291 fprintf(stderr, "Unrecognized nod format '%s'", line);
292 goto fail;
294 rc = cpio_mknod(name, mode, uid, gid, dev_type, maj, min);
295 fail:
296 return rc;
299 static int cpio_mkfile(const char *name, const char *location,
300 unsigned int mode, uid_t uid, gid_t gid,
301 unsigned int nlinks)
303 char s[256];
304 char *filebuf = NULL;
305 struct stat buf;
306 long size;
307 int file = -1;
308 int retval;
309 int rc = -1;
310 int namesize;
311 int i;
313 mode |= S_IFREG;
315 #ifdef __CYGWIN32__
316 file = open (location, O_RDONLY | O_BINARY);
317 #else
318 file = open (location, O_RDONLY);
319 #endif
320 if (file < 0) {
321 fprintf (stderr, "File %s could not be opened for reading\n", location);
322 goto error;
325 retval = fstat(file, &buf);
326 if (retval) {
327 fprintf(stderr, "File %s could not be stat()'ed\n", location);
328 goto error;
331 filebuf = malloc(buf.st_size);
332 if (!filebuf) {
333 fprintf (stderr, "out of memory\n");
334 goto error;
337 retval = read (file, filebuf, buf.st_size);
338 if (retval < 0) {
339 fprintf (stderr, "Can not read %s file\n", location);
340 goto error;
343 size = 0;
344 for (i = 1; i <= nlinks; i++) {
345 /* data goes on last link */
346 if (i == nlinks) size = buf.st_size;
348 if (name[0] == '/')
349 name++;
350 namesize = strlen(name) + 1;
351 sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
352 "%08lX%08X%08X%08X%08X%08X%08X",
353 "070701", /* magic */
354 ino, /* ino */
355 mode, /* mode */
356 (long) uid, /* uid */
357 (long) gid, /* gid */
358 nlinks, /* nlink */
359 (long) buf.st_mtime, /* mtime */
360 size, /* filesize */
361 3, /* major */
362 1, /* minor */
363 0, /* rmajor */
364 0, /* rminor */
365 namesize, /* namesize */
366 0); /* chksum */
367 push_hdr(s);
368 push_string(name);
369 push_pad();
371 if (size) {
372 if (fwrite(filebuf, size, 1, stdout) != 1) {
373 fprintf(stderr, "writing filebuf failed\n");
374 goto error;
376 offset += size;
377 push_pad();
380 name += namesize;
382 ino++;
383 rc = 0;
385 error:
386 if (filebuf) free(filebuf);
387 if (file >= 0) close(file);
388 return rc;
391 static char *cpio_replace_env(char *new_location)
393 char expanded[PATH_MAX + 1];
394 char env_var[PATH_MAX + 1];
395 char *start;
396 char *end;
398 for (start = NULL; (start = strstr(new_location, "${")); ) {
399 end = strchr(start, '}');
400 if (start < end) {
401 *env_var = *expanded = '\0';
402 strncat(env_var, start + 2, end - start - 2);
403 strncat(expanded, new_location, start - new_location);
404 strncat(expanded, getenv(env_var), PATH_MAX);
405 strncat(expanded, end + 1, PATH_MAX);
406 strncpy(new_location, expanded, PATH_MAX);
407 } else
408 break;
411 return new_location;
415 static int cpio_mkfile_line(const char *line)
417 char name[PATH_MAX + 1];
418 char *dname = NULL; /* malloc'ed buffer for hard links */
419 char location[PATH_MAX + 1];
420 unsigned int mode;
421 int uid;
422 int gid;
423 int nlinks = 1;
424 int end = 0, dname_len = 0;
425 int rc = -1;
427 if (5 > sscanf(line, "%" str(PATH_MAX) "s %" str(PATH_MAX)
428 "s %o %d %d %n",
429 name, location, &mode, &uid, &gid, &end)) {
430 fprintf(stderr, "Unrecognized file format '%s'", line);
431 goto fail;
433 if (end && isgraph(line[end])) {
434 int len;
435 int nend;
437 dname = malloc(strlen(line));
438 if (!dname) {
439 fprintf (stderr, "out of memory (%d)\n", dname_len);
440 goto fail;
443 dname_len = strlen(name) + 1;
444 memcpy(dname, name, dname_len);
446 do {
447 nend = 0;
448 if (sscanf(line + end, "%" str(PATH_MAX) "s %n",
449 name, &nend) < 1)
450 break;
451 len = strlen(name) + 1;
452 memcpy(dname + dname_len, name, len);
453 dname_len += len;
454 nlinks++;
455 end += nend;
456 } while (isgraph(line[end]));
457 } else {
458 dname = name;
460 rc = cpio_mkfile(dname, cpio_replace_env(location),
461 mode, uid, gid, nlinks);
462 fail:
463 if (dname_len) free(dname);
464 return rc;
467 static void usage(const char *prog)
469 fprintf(stderr, "Usage:\n"
470 "\t%s [-t <timestamp>] <cpio_list>\n"
471 "\n"
472 "<cpio_list> is a file containing newline separated entries that\n"
473 "describe the files to be included in the initramfs archive:\n"
474 "\n"
475 "# a comment\n"
476 "file <name> <location> <mode> <uid> <gid> [<hard links>]\n"
477 "dir <name> <mode> <uid> <gid>\n"
478 "nod <name> <mode> <uid> <gid> <dev_type> <maj> <min>\n"
479 "slink <name> <target> <mode> <uid> <gid>\n"
480 "pipe <name> <mode> <uid> <gid>\n"
481 "sock <name> <mode> <uid> <gid>\n"
482 "\n"
483 "<name> name of the file/dir/nod/etc in the archive\n"
484 "<location> location of the file in the current filesystem\n"
485 " expands shell variables quoted with ${}\n"
486 "<target> link target\n"
487 "<mode> mode/permissions of the file\n"
488 "<uid> user id (0=root)\n"
489 "<gid> group id (0=root)\n"
490 "<dev_type> device type (b=block, c=character)\n"
491 "<maj> major number of nod\n"
492 "<min> minor number of nod\n"
493 "<hard links> space separated list of other links to file\n"
494 "\n"
495 "example:\n"
496 "# A simple initramfs\n"
497 "dir /dev 0755 0 0\n"
498 "nod /dev/console 0600 0 0 c 5 1\n"
499 "dir /root 0700 0 0\n"
500 "dir /sbin 0755 0 0\n"
501 "file /sbin/kinit /usr/src/klibc/kinit/kinit 0755 0 0\n"
502 "\n"
503 "<timestamp> is time in seconds since Epoch that will be used\n"
504 "as mtime for symlinks, special files and directories. The default\n"
505 "is to use the current time for these entries.\n",
506 prog);
509 struct file_handler file_handler_table[] = {
511 .type = "file",
512 .handler = cpio_mkfile_line,
513 }, {
514 .type = "nod",
515 .handler = cpio_mknod_line,
516 }, {
517 .type = "dir",
518 .handler = cpio_mkdir_line,
519 }, {
520 .type = "slink",
521 .handler = cpio_mkslink_line,
522 }, {
523 .type = "pipe",
524 .handler = cpio_mkpipe_line,
525 }, {
526 .type = "sock",
527 .handler = cpio_mksock_line,
528 }, {
529 .type = NULL,
530 .handler = NULL,
534 #define LINE_SIZE (2 * PATH_MAX + 50)
536 int main (int argc, char *argv[])
538 FILE *cpio_list;
539 char line[LINE_SIZE];
540 char *args, *type;
541 int ec = 0;
542 int line_nr = 0;
543 const char *filename;
545 default_mtime = time(NULL);
546 while (1) {
547 int opt = getopt(argc, argv, "t:h");
548 char *invalid;
550 if (opt == -1)
551 break;
552 switch (opt) {
553 case 't':
554 default_mtime = strtol(optarg, &invalid, 10);
555 if (!*optarg || *invalid) {
556 fprintf(stderr, "Invalid timestamp: %s\n",
557 optarg);
558 usage(argv[0]);
559 exit(1);
561 break;
562 case 'h':
563 case '?':
564 usage(argv[0]);
565 exit(opt == 'h' ? 0 : 1);
569 if (argc - optind != 1) {
570 usage(argv[0]);
571 exit(1);
573 filename = argv[optind];
574 if (!strcmp(filename, "-"))
575 cpio_list = stdin;
576 else if (!(cpio_list = fopen(filename, "r"))) {
577 fprintf(stderr, "ERROR: unable to open '%s': %s\n\n",
578 filename, strerror(errno));
579 usage(argv[0]);
580 exit(1);
583 while (fgets(line, LINE_SIZE, cpio_list)) {
584 int type_idx;
585 size_t slen = strlen(line);
587 line_nr++;
589 if ('#' == *line) {
590 /* comment - skip to next line */
591 continue;
594 if (! (type = strtok(line, " \t"))) {
595 fprintf(stderr,
596 "ERROR: incorrect format, could not locate file type line %d: '%s'\n",
597 line_nr, line);
598 ec = -1;
599 break;
602 if ('\n' == *type) {
603 /* a blank line */
604 continue;
607 if (slen == strlen(type)) {
608 /* must be an empty line */
609 continue;
612 if (! (args = strtok(NULL, "\n"))) {
613 fprintf(stderr,
614 "ERROR: incorrect format, newline required line %d: '%s'\n",
615 line_nr, line);
616 ec = -1;
619 for (type_idx = 0; file_handler_table[type_idx].type; type_idx++) {
620 int rc;
621 if (! strcmp(line, file_handler_table[type_idx].type)) {
622 if ((rc = file_handler_table[type_idx].handler(args))) {
623 ec = rc;
624 fprintf(stderr, " line %d\n", line_nr);
626 break;
630 if (NULL == file_handler_table[type_idx].type) {
631 fprintf(stderr, "unknown file type line %d: '%s'\n",
632 line_nr, line);
635 if (ec == 0)
636 cpio_trailer();
638 exit(ec);