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;
28 int (*handler
)(const char *line
);
31 static void push_string(const char *name
)
33 unsigned int name_len
= strlen(name
) + 1;
40 static void push_pad (void)
48 static void push_rest(const char *name
)
50 unsigned int name_len
= strlen(name
) + 1;
57 tmp_ofs
= name_len
+ 110;
65 static void push_hdr(const char *s
)
71 static void cpio_trailer(void)
74 const char name
[] = "TRAILER!!!";
76 sprintf(s
, "%s%08X%08X%08lX%08lX%08X%08lX"
77 "%08X%08X%08X%08X%08X%08X%08X",
90 (unsigned)strlen(name
)+1, /* namesize */
95 while (offset
% 512) {
101 static int cpio_mkslink(const char *name
, const char *target
,
102 unsigned int mode
, uid_t uid
, gid_t gid
)
105 time_t mtime
= time(NULL
);
107 sprintf(s
,"%s%08X%08X%08lX%08lX%08X%08lX"
108 "%08X%08X%08X%08X%08X%08X%08X",
109 "070701", /* magic */
111 S_IFLNK
| mode
, /* mode */
112 (long) uid
, /* uid */
113 (long) gid
, /* gid */
115 (long) mtime
, /* mtime */
116 (unsigned)strlen(target
)+1, /* filesize */
121 (unsigned)strlen(name
) + 1,/* namesize */
131 static int cpio_mkslink_line(const char *line
)
133 char name
[PATH_MAX
+ 1];
134 char target
[PATH_MAX
+ 1];
140 if (5 != sscanf(line
, "%" str(PATH_MAX
) "s %" str(PATH_MAX
) "s %o %d %d", name
, target
, &mode
, &uid
, &gid
)) {
141 fprintf(stderr
, "Unrecognized dir format '%s'", line
);
144 rc
= cpio_mkslink(name
, target
, mode
, uid
, gid
);
149 static int cpio_mkgeneric(const char *name
, unsigned int mode
,
150 uid_t uid
, gid_t gid
)
153 time_t mtime
= time(NULL
);
155 sprintf(s
,"%s%08X%08X%08lX%08lX%08X%08lX"
156 "%08X%08X%08X%08X%08X%08X%08X",
157 "070701", /* magic */
160 (long) uid
, /* uid */
161 (long) gid
, /* gid */
163 (long) mtime
, /* mtime */
169 (unsigned)strlen(name
) + 1,/* namesize */
182 struct generic_type
{
187 static struct generic_type generic_type_table
[] = {
202 static int cpio_mkgeneric_line(const char *line
, enum generic_types gt
)
204 char name
[PATH_MAX
+ 1];
210 if (4 != sscanf(line
, "%" str(PATH_MAX
) "s %o %d %d", name
, &mode
, &uid
, &gid
)) {
211 fprintf(stderr
, "Unrecognized %s format '%s'",
212 line
, generic_type_table
[gt
].type
);
215 mode
|= generic_type_table
[gt
].mode
;
216 rc
= cpio_mkgeneric(name
, mode
, uid
, gid
);
221 static int cpio_mkdir_line(const char *line
)
223 return cpio_mkgeneric_line(line
, GT_DIR
);
226 static int cpio_mkpipe_line(const char *line
)
228 return cpio_mkgeneric_line(line
, GT_PIPE
);
231 static int cpio_mksock_line(const char *line
)
233 return cpio_mkgeneric_line(line
, GT_SOCK
);
236 static int cpio_mknod(const char *name
, unsigned int mode
,
237 uid_t uid
, gid_t gid
, char dev_type
,
238 unsigned int maj
, unsigned int min
)
241 time_t mtime
= time(NULL
);
248 sprintf(s
,"%s%08X%08X%08lX%08lX%08X%08lX"
249 "%08X%08X%08X%08X%08X%08X%08X",
250 "070701", /* magic */
253 (long) uid
, /* uid */
254 (long) gid
, /* gid */
256 (long) mtime
, /* mtime */
262 (unsigned)strlen(name
) + 1,/* namesize */
269 static int cpio_mknod_line(const char *line
)
271 char name
[PATH_MAX
+ 1];
280 if (7 != sscanf(line
, "%" str(PATH_MAX
) "s %o %d %d %c %u %u",
281 name
, &mode
, &uid
, &gid
, &dev_type
, &maj
, &min
)) {
282 fprintf(stderr
, "Unrecognized nod format '%s'", line
);
285 rc
= cpio_mknod(name
, mode
, uid
, gid
, dev_type
, maj
, min
);
290 static int cpio_mkfile(const char *name
, const char *location
,
291 unsigned int mode
, uid_t uid
, gid_t gid
,
295 char *filebuf
= NULL
;
306 retval
= stat (location
, &buf
);
308 fprintf (stderr
, "File %s could not be located\n", location
);
312 file
= open (location
, O_RDONLY
);
314 fprintf (stderr
, "File %s could not be opened for reading\n", location
);
318 filebuf
= malloc(buf
.st_size
);
320 fprintf (stderr
, "out of memory\n");
324 retval
= read (file
, filebuf
, buf
.st_size
);
326 fprintf (stderr
, "Can not read %s file\n", location
);
331 for (i
= 1; i
<= nlinks
; i
++) {
332 /* data goes on last link */
333 if (i
== nlinks
) size
= buf
.st_size
;
335 namesize
= strlen(name
) + 1;
336 sprintf(s
,"%s%08X%08X%08lX%08lX%08X%08lX"
337 "%08lX%08X%08X%08X%08X%08X%08X",
338 "070701", /* magic */
341 (long) uid
, /* uid */
342 (long) gid
, /* gid */
344 (long) buf
.st_mtime
, /* mtime */
350 namesize
, /* namesize */
357 fwrite(filebuf
, size
, 1, stdout
);
368 if (filebuf
) free(filebuf
);
369 if (file
>= 0) close(file
);
373 static char *cpio_replace_env(char *new_location
)
375 char expanded
[PATH_MAX
+ 1];
376 char env_var
[PATH_MAX
+ 1];
380 for (start
= NULL
; (start
= strstr(new_location
, "${")); ) {
381 end
= strchr(start
, '}');
383 *env_var
= *expanded
= '\0';
384 strncat(env_var
, start
+ 2, end
- start
- 2);
385 strncat(expanded
, new_location
, start
- new_location
);
386 strncat(expanded
, getenv(env_var
), PATH_MAX
);
387 strncat(expanded
, end
+ 1, PATH_MAX
);
388 strncpy(new_location
, expanded
, PATH_MAX
);
397 static int cpio_mkfile_line(const char *line
)
399 char name
[PATH_MAX
+ 1];
400 char *dname
= NULL
; /* malloc'ed buffer for hard links */
401 char location
[PATH_MAX
+ 1];
406 int end
= 0, dname_len
= 0;
409 if (5 > sscanf(line
, "%" str(PATH_MAX
) "s %" str(PATH_MAX
)
411 name
, location
, &mode
, &uid
, &gid
, &end
)) {
412 fprintf(stderr
, "Unrecognized file format '%s'", line
);
415 if (end
&& isgraph(line
[end
])) {
419 dname
= malloc(strlen(line
));
421 fprintf (stderr
, "out of memory (%d)\n", dname_len
);
425 dname_len
= strlen(name
) + 1;
426 memcpy(dname
, name
, dname_len
);
430 if (sscanf(line
+ end
, "%" str(PATH_MAX
) "s %n",
433 len
= strlen(name
) + 1;
434 memcpy(dname
+ dname_len
, name
, len
);
438 } while (isgraph(line
[end
]));
442 rc
= cpio_mkfile(dname
, cpio_replace_env(location
),
443 mode
, uid
, gid
, nlinks
);
445 if (dname_len
) free(dname
);
449 void usage(const char *prog
)
451 fprintf(stderr
, "Usage:\n"
454 "<cpio_list> is a file containing newline separated entries that\n"
455 "describe the files to be included in the initramfs archive:\n"
458 "file <name> <location> <mode> <uid> <gid> [<hard links>]\n"
459 "dir <name> <mode> <uid> <gid>\n"
460 "nod <name> <mode> <uid> <gid> <dev_type> <maj> <min>\n"
461 "slink <name> <target> <mode> <uid> <gid>\n"
462 "pipe <name> <mode> <uid> <gid>\n"
463 "sock <name> <mode> <uid> <gid>\n"
465 "<name> name of the file/dir/nod/etc in the archive\n"
466 "<location> location of the file in the current filesystem\n"
467 " expands shell variables quoted with ${}\n"
468 "<target> link target\n"
469 "<mode> mode/permissions of the file\n"
470 "<uid> user id (0=root)\n"
471 "<gid> group id (0=root)\n"
472 "<dev_type> device type (b=block, c=character)\n"
473 "<maj> major number of nod\n"
474 "<min> minor number of nod\n"
475 "<hard links> space separated list of other links to file\n"
478 "# A simple initramfs\n"
479 "dir /dev 0755 0 0\n"
480 "nod /dev/console 0600 0 0 c 5 1\n"
481 "dir /root 0700 0 0\n"
482 "dir /sbin 0755 0 0\n"
483 "file /sbin/kinit /usr/src/klibc/kinit/kinit 0755 0 0\n",
487 struct file_handler file_handler_table
[] = {
490 .handler
= cpio_mkfile_line
,
493 .handler
= cpio_mknod_line
,
496 .handler
= cpio_mkdir_line
,
499 .handler
= cpio_mkslink_line
,
502 .handler
= cpio_mkpipe_line
,
505 .handler
= cpio_mksock_line
,
512 #define LINE_SIZE (2 * PATH_MAX + 50)
514 int main (int argc
, char *argv
[])
517 char line
[LINE_SIZE
];
527 if (!strcmp(argv
[1], "-"))
529 else if (! (cpio_list
= fopen(argv
[1], "r"))) {
530 fprintf(stderr
, "ERROR: unable to open '%s': %s\n\n",
531 argv
[1], strerror(errno
));
536 while (fgets(line
, LINE_SIZE
, cpio_list
)) {
538 size_t slen
= strlen(line
);
543 /* comment - skip to next line */
547 if (! (type
= strtok(line
, " \t"))) {
549 "ERROR: incorrect format, could not locate file type line %d: '%s'\n",
560 if (slen
== strlen(type
)) {
561 /* must be an empty line */
565 if (! (args
= strtok(NULL
, "\n"))) {
567 "ERROR: incorrect format, newline required line %d: '%s'\n",
572 for (type_idx
= 0; file_handler_table
[type_idx
].type
; type_idx
++) {
574 if (! strcmp(line
, file_handler_table
[type_idx
].type
)) {
575 if ((rc
= file_handler_table
[type_idx
].handler(args
))) {
577 fprintf(stderr
, " line %d\n", line_nr
);
583 if (NULL
== file_handler_table
[type_idx
].type
) {
584 fprintf(stderr
, "unknown file type line %d: '%s'\n",