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
21 #define str(s) xstr(s)
23 static unsigned int offset
;
24 static unsigned int ino
= 721;
25 static time_t default_mtime
;
29 int (*handler
)(const char *line
);
32 static void push_string(const char *name
)
34 unsigned int name_len
= strlen(name
) + 1;
41 static void push_pad (void)
49 static void push_rest(const char *name
)
51 unsigned int name_len
= strlen(name
) + 1;
58 tmp_ofs
= name_len
+ 110;
66 static void push_hdr(const char *s
)
72 static void cpio_trailer(void)
75 const char name
[] = "TRAILER!!!";
77 sprintf(s
, "%s%08X%08X%08lX%08lX%08X%08lX"
78 "%08X%08X%08X%08X%08X%08X%08X",
91 (unsigned)strlen(name
)+1, /* namesize */
96 while (offset
% 512) {
102 static int cpio_mkslink(const char *name
, const char *target
,
103 unsigned int mode
, uid_t uid
, gid_t gid
)
109 sprintf(s
,"%s%08X%08X%08lX%08lX%08X%08lX"
110 "%08X%08X%08X%08X%08X%08X%08X",
111 "070701", /* magic */
113 S_IFLNK
| mode
, /* mode */
114 (long) uid
, /* uid */
115 (long) gid
, /* gid */
117 (long) default_mtime
, /* mtime */
118 (unsigned)strlen(target
)+1, /* filesize */
123 (unsigned)strlen(name
) + 1,/* namesize */
133 static int cpio_mkslink_line(const char *line
)
135 char name
[PATH_MAX
+ 1];
136 char target
[PATH_MAX
+ 1];
142 if (5 != sscanf(line
, "%" str(PATH_MAX
) "s %" str(PATH_MAX
) "s %o %d %d", name
, target
, &mode
, &uid
, &gid
)) {
143 fprintf(stderr
, "Unrecognized dir format '%s'", line
);
146 rc
= cpio_mkslink(name
, target
, mode
, uid
, gid
);
151 static int cpio_mkgeneric(const char *name
, unsigned int mode
,
152 uid_t uid
, gid_t gid
)
158 sprintf(s
,"%s%08X%08X%08lX%08lX%08X%08lX"
159 "%08X%08X%08X%08X%08X%08X%08X",
160 "070701", /* magic */
163 (long) uid
, /* uid */
164 (long) gid
, /* gid */
166 (long) default_mtime
, /* mtime */
172 (unsigned)strlen(name
) + 1,/* namesize */
185 struct generic_type
{
190 static struct generic_type generic_type_table
[] = {
205 static int cpio_mkgeneric_line(const char *line
, enum generic_types gt
)
207 char name
[PATH_MAX
+ 1];
213 if (4 != sscanf(line
, "%" str(PATH_MAX
) "s %o %d %d", name
, &mode
, &uid
, &gid
)) {
214 fprintf(stderr
, "Unrecognized %s format '%s'",
215 line
, generic_type_table
[gt
].type
);
218 mode
|= generic_type_table
[gt
].mode
;
219 rc
= cpio_mkgeneric(name
, mode
, uid
, gid
);
224 static int cpio_mkdir_line(const char *line
)
226 return cpio_mkgeneric_line(line
, GT_DIR
);
229 static int cpio_mkpipe_line(const char *line
)
231 return cpio_mkgeneric_line(line
, GT_PIPE
);
234 static int cpio_mksock_line(const char *line
)
236 return cpio_mkgeneric_line(line
, GT_SOCK
);
239 static int cpio_mknod(const char *name
, unsigned int mode
,
240 uid_t uid
, gid_t gid
, char dev_type
,
241 unsigned int maj
, unsigned int min
)
252 sprintf(s
,"%s%08X%08X%08lX%08lX%08X%08lX"
253 "%08X%08X%08X%08X%08X%08X%08X",
254 "070701", /* magic */
257 (long) uid
, /* uid */
258 (long) gid
, /* gid */
260 (long) default_mtime
, /* mtime */
266 (unsigned)strlen(name
) + 1,/* namesize */
273 static int cpio_mknod_line(const char *line
)
275 char name
[PATH_MAX
+ 1];
284 if (7 != sscanf(line
, "%" str(PATH_MAX
) "s %o %d %d %c %u %u",
285 name
, &mode
, &uid
, &gid
, &dev_type
, &maj
, &min
)) {
286 fprintf(stderr
, "Unrecognized nod format '%s'", line
);
289 rc
= cpio_mknod(name
, mode
, uid
, gid
, dev_type
, maj
, min
);
294 static int cpio_mkfile(const char *name
, const char *location
,
295 unsigned int mode
, uid_t uid
, gid_t gid
,
299 char *filebuf
= NULL
;
310 file
= open (location
, O_RDONLY
);
312 fprintf (stderr
, "File %s could not be opened for reading\n", location
);
316 retval
= fstat(file
, &buf
);
318 fprintf(stderr
, "File %s could not be stat()'ed\n", location
);
322 filebuf
= malloc(buf
.st_size
);
324 fprintf (stderr
, "out of memory\n");
328 retval
= read (file
, filebuf
, buf
.st_size
);
330 fprintf (stderr
, "Can not read %s file\n", location
);
335 for (i
= 1; i
<= nlinks
; i
++) {
336 /* data goes on last link */
337 if (i
== nlinks
) size
= buf
.st_size
;
341 namesize
= strlen(name
) + 1;
342 sprintf(s
,"%s%08X%08X%08lX%08lX%08X%08lX"
343 "%08lX%08X%08X%08X%08X%08X%08X",
344 "070701", /* magic */
347 (long) uid
, /* uid */
348 (long) gid
, /* gid */
350 (long) buf
.st_mtime
, /* mtime */
356 namesize
, /* namesize */
363 if (fwrite(filebuf
, size
, 1, stdout
) != 1) {
364 fprintf(stderr
, "writing filebuf failed\n");
377 if (filebuf
) free(filebuf
);
378 if (file
>= 0) close(file
);
382 static char *cpio_replace_env(char *new_location
)
384 char expanded
[PATH_MAX
+ 1];
385 char env_var
[PATH_MAX
+ 1];
389 for (start
= NULL
; (start
= strstr(new_location
, "${")); ) {
390 end
= strchr(start
, '}');
392 *env_var
= *expanded
= '\0';
393 strncat(env_var
, start
+ 2, end
- start
- 2);
394 strncat(expanded
, new_location
, start
- new_location
);
395 strncat(expanded
, getenv(env_var
),
396 PATH_MAX
- strlen(expanded
));
397 strncat(expanded
, end
+ 1,
398 PATH_MAX
- strlen(expanded
));
399 strncpy(new_location
, expanded
, PATH_MAX
);
400 new_location
[PATH_MAX
] = 0;
409 static int cpio_mkfile_line(const char *line
)
411 char name
[PATH_MAX
+ 1];
412 char *dname
= NULL
; /* malloc'ed buffer for hard links */
413 char location
[PATH_MAX
+ 1];
418 int end
= 0, dname_len
= 0;
421 if (5 > sscanf(line
, "%" str(PATH_MAX
) "s %" str(PATH_MAX
)
423 name
, location
, &mode
, &uid
, &gid
, &end
)) {
424 fprintf(stderr
, "Unrecognized file format '%s'", line
);
427 if (end
&& isgraph(line
[end
])) {
431 dname
= malloc(strlen(line
));
433 fprintf (stderr
, "out of memory (%d)\n", dname_len
);
437 dname_len
= strlen(name
) + 1;
438 memcpy(dname
, name
, dname_len
);
442 if (sscanf(line
+ end
, "%" str(PATH_MAX
) "s %n",
445 len
= strlen(name
) + 1;
446 memcpy(dname
+ dname_len
, name
, len
);
450 } while (isgraph(line
[end
]));
454 rc
= cpio_mkfile(dname
, cpio_replace_env(location
),
455 mode
, uid
, gid
, nlinks
);
457 if (dname_len
) free(dname
);
461 static void usage(const char *prog
)
463 fprintf(stderr
, "Usage:\n"
464 "\t%s [-t <timestamp>] <cpio_list>\n"
466 "<cpio_list> is a file containing newline separated entries that\n"
467 "describe the files to be included in the initramfs archive:\n"
470 "file <name> <location> <mode> <uid> <gid> [<hard links>]\n"
471 "dir <name> <mode> <uid> <gid>\n"
472 "nod <name> <mode> <uid> <gid> <dev_type> <maj> <min>\n"
473 "slink <name> <target> <mode> <uid> <gid>\n"
474 "pipe <name> <mode> <uid> <gid>\n"
475 "sock <name> <mode> <uid> <gid>\n"
477 "<name> name of the file/dir/nod/etc in the archive\n"
478 "<location> location of the file in the current filesystem\n"
479 " expands shell variables quoted with ${}\n"
480 "<target> link target\n"
481 "<mode> mode/permissions of the file\n"
482 "<uid> user id (0=root)\n"
483 "<gid> group id (0=root)\n"
484 "<dev_type> device type (b=block, c=character)\n"
485 "<maj> major number of nod\n"
486 "<min> minor number of nod\n"
487 "<hard links> space separated list of other links to file\n"
490 "# A simple initramfs\n"
491 "dir /dev 0755 0 0\n"
492 "nod /dev/console 0600 0 0 c 5 1\n"
493 "dir /root 0700 0 0\n"
494 "dir /sbin 0755 0 0\n"
495 "file /sbin/kinit /usr/src/klibc/kinit/kinit 0755 0 0\n"
497 "<timestamp> is time in seconds since Epoch that will be used\n"
498 "as mtime for symlinks, special files and directories. The default\n"
499 "is to use the current time for these entries.\n",
503 struct file_handler file_handler_table
[] = {
506 .handler
= cpio_mkfile_line
,
509 .handler
= cpio_mknod_line
,
512 .handler
= cpio_mkdir_line
,
515 .handler
= cpio_mkslink_line
,
518 .handler
= cpio_mkpipe_line
,
521 .handler
= cpio_mksock_line
,
528 #define LINE_SIZE (2 * PATH_MAX + 50)
530 int main (int argc
, char *argv
[])
533 char line
[LINE_SIZE
];
537 const char *filename
;
539 default_mtime
= time(NULL
);
541 int opt
= getopt(argc
, argv
, "t:h");
548 default_mtime
= strtol(optarg
, &invalid
, 10);
549 if (!*optarg
|| *invalid
) {
550 fprintf(stderr
, "Invalid timestamp: %s\n",
559 exit(opt
== 'h' ? 0 : 1);
563 if (argc
- optind
!= 1) {
567 filename
= argv
[optind
];
568 if (!strcmp(filename
, "-"))
570 else if (!(cpio_list
= fopen(filename
, "r"))) {
571 fprintf(stderr
, "ERROR: unable to open '%s': %s\n\n",
572 filename
, strerror(errno
));
577 while (fgets(line
, LINE_SIZE
, cpio_list
)) {
579 size_t slen
= strlen(line
);
584 /* comment - skip to next line */
588 if (! (type
= strtok(line
, " \t"))) {
590 "ERROR: incorrect format, could not locate file type line %d: '%s'\n",
601 if (slen
== strlen(type
)) {
602 /* must be an empty line */
606 if (! (args
= strtok(NULL
, "\n"))) {
608 "ERROR: incorrect format, newline required line %d: '%s'\n",
613 for (type_idx
= 0; file_handler_table
[type_idx
].type
; type_idx
++) {
615 if (! strcmp(line
, file_handler_table
[type_idx
].type
)) {
616 if ((rc
= file_handler_table
[type_idx
].handler(args
))) {
618 fprintf(stderr
, " line %d\n", line_nr
);
624 if (NULL
== file_handler_table
[type_idx
].type
) {
625 fprintf(stderr
, "unknown file type line %d: '%s'\n",