1 // SPDX-License-Identifier: GPL-2.0
15 * Original work by Jeff Garzik
17 * External file lists, symlink, pipe and fifo support by Thayne Harbaugh
18 * Hard link support by Luciano Rocha
22 #define str(s) xstr(s)
23 #define MIN(a, b) ((a) < (b) ? (a) : (b))
25 static unsigned int offset;
26 static unsigned int ino = 721;
27 static time_t default_mtime;
31 int (*handler)(const char *line);
34 static void push_string(const char *name)
36 unsigned int name_len = strlen(name) + 1;
43 static void push_pad (void)
51 static void push_rest(const char *name)
53 unsigned int name_len = strlen(name) + 1;
60 tmp_ofs = name_len + 110;
68 static void push_hdr(const char *s)
74 static void cpio_trailer(void)
77 const char name[] = "TRAILER!!!";
79 sprintf(s, "%s%08X%08X%08lX%08lX%08X%08lX"
80 "%08X%08X%08X%08X%08X%08X%08X",
93 (unsigned)strlen(name)+1, /* namesize */
98 while (offset % 512) {
104 static int cpio_mkslink(const char *name, const char *target,
105 unsigned int mode, uid_t uid, gid_t gid)
111 sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
112 "%08X%08X%08X%08X%08X%08X%08X",
113 "070701", /* magic */
115 S_IFLNK | mode, /* mode */
116 (long) uid, /* uid */
117 (long) gid, /* gid */
119 (long) default_mtime, /* mtime */
120 (unsigned)strlen(target)+1, /* filesize */
125 (unsigned)strlen(name) + 1,/* namesize */
135 static int cpio_mkslink_line(const char *line)
137 char name[PATH_MAX + 1];
138 char target[PATH_MAX + 1];
144 if (5 != sscanf(line, "%" str(PATH_MAX) "s %" str(PATH_MAX) "s %o %d %d", name, target, &mode, &uid, &gid)) {
145 fprintf(stderr, "Unrecognized dir format '%s'", line);
148 rc = cpio_mkslink(name, target, mode, uid, gid);
153 static int cpio_mkgeneric(const char *name, unsigned int mode,
154 uid_t uid, gid_t gid)
160 sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
161 "%08X%08X%08X%08X%08X%08X%08X",
162 "070701", /* magic */
165 (long) uid, /* uid */
166 (long) gid, /* gid */
168 (long) default_mtime, /* mtime */
174 (unsigned)strlen(name) + 1,/* namesize */
187 struct generic_type {
192 static const struct generic_type generic_type_table[] = {
207 static int cpio_mkgeneric_line(const char *line, enum generic_types gt)
209 char name[PATH_MAX + 1];
215 if (4 != sscanf(line, "%" str(PATH_MAX) "s %o %d %d", name, &mode, &uid, &gid)) {
216 fprintf(stderr, "Unrecognized %s format '%s'",
217 line, generic_type_table[gt].type);
220 mode |= generic_type_table[gt].mode;
221 rc = cpio_mkgeneric(name, mode, uid, gid);
226 static int cpio_mkdir_line(const char *line)
228 return cpio_mkgeneric_line(line, GT_DIR);
231 static int cpio_mkpipe_line(const char *line)
233 return cpio_mkgeneric_line(line, GT_PIPE);
236 static int cpio_mksock_line(const char *line)
238 return cpio_mkgeneric_line(line, GT_SOCK);
241 static int cpio_mknod(const char *name, unsigned int mode,
242 uid_t uid, gid_t gid, char dev_type,
243 unsigned int maj, unsigned int min)
254 sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
255 "%08X%08X%08X%08X%08X%08X%08X",
256 "070701", /* magic */
259 (long) uid, /* uid */
260 (long) gid, /* gid */
262 (long) default_mtime, /* mtime */
268 (unsigned)strlen(name) + 1,/* namesize */
275 static int cpio_mknod_line(const char *line)
277 char name[PATH_MAX + 1];
286 if (7 != sscanf(line, "%" str(PATH_MAX) "s %o %d %d %c %u %u",
287 name, &mode, &uid, &gid, &dev_type, &maj, &min)) {
288 fprintf(stderr, "Unrecognized nod format '%s'", line);
291 rc = cpio_mknod(name, mode, uid, gid, dev_type, maj, min);
296 static int cpio_mkfile(const char *name, const char *location,
297 unsigned int mode, uid_t uid, gid_t gid,
311 file = open (location, O_RDONLY);
313 fprintf (stderr, "File %s could not be opened for reading\n", location);
317 retval = fstat(file, &buf);
319 fprintf(stderr, "File %s could not be stat()'ed\n", location);
323 if (buf.st_mtime > 0xffffffff) {
324 fprintf(stderr, "%s: Timestamp exceeds maximum cpio timestamp, clipping.\n",
326 buf.st_mtime = 0xffffffff;
329 if (buf.st_size > 0xffffffff) {
330 fprintf(stderr, "%s: Size exceeds maximum cpio file size\n",
336 for (i = 1; i <= nlinks; i++) {
337 /* data goes on last link */
343 namesize = strlen(name) + 1;
344 sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX"
345 "%08lX%08X%08X%08X%08X%08X%08X",
346 "070701", /* magic */
349 (long) uid, /* uid */
350 (long) gid, /* gid */
352 (long) buf.st_mtime, /* mtime */
358 namesize, /* namesize */
365 unsigned char filebuf[65536];
367 size_t this_size = MIN(size, sizeof(filebuf));
369 this_read = read(file, filebuf, this_size);
370 if (this_read <= 0 || this_read > this_size) {
371 fprintf(stderr, "Can not read %s file\n", location);
375 if (fwrite(filebuf, this_read, 1, stdout) != 1) {
376 fprintf(stderr, "writing filebuf failed\n");
395 static char *cpio_replace_env(char *new_location)
397 char expanded[PATH_MAX + 1];
398 char *start, *end, *var;
400 while ((start = strstr(new_location, "${")) &&
401 (end = strchr(start + 2, '}'))) {
403 var = getenv(start + 2);
404 snprintf(expanded, sizeof expanded, "%s%s%s",
405 new_location, var ? var : "", end + 1);
406 strcpy(new_location, expanded);
412 static int cpio_mkfile_line(const char *line)
414 char name[PATH_MAX + 1];
415 char *dname = NULL; /* malloc'ed buffer for hard links */
416 char location[PATH_MAX + 1];
421 int end = 0, dname_len = 0;
424 if (5 > sscanf(line, "%" str(PATH_MAX) "s %" str(PATH_MAX)
426 name, location, &mode, &uid, &gid, &end)) {
427 fprintf(stderr, "Unrecognized file format '%s'", line);
430 if (end && isgraph(line[end])) {
434 dname = malloc(strlen(line));
436 fprintf (stderr, "out of memory (%d)\n", dname_len);
440 dname_len = strlen(name) + 1;
441 memcpy(dname, name, dname_len);
445 if (sscanf(line + end, "%" str(PATH_MAX) "s %n",
448 len = strlen(name) + 1;
449 memcpy(dname + dname_len, name, len);
453 } while (isgraph(line[end]));
457 rc = cpio_mkfile(dname, cpio_replace_env(location),
458 mode, uid, gid, nlinks);
460 if (dname_len) free(dname);
464 static void usage(const char *prog)
466 fprintf(stderr, "Usage:\n"
467 "\t%s [-t <timestamp>] <cpio_list>\n"
469 "<cpio_list> is a file containing newline separated entries that\n"
470 "describe the files to be included in the initramfs archive:\n"
473 "file <name> <location> <mode> <uid> <gid> [<hard links>]\n"
474 "dir <name> <mode> <uid> <gid>\n"
475 "nod <name> <mode> <uid> <gid> <dev_type> <maj> <min>\n"
476 "slink <name> <target> <mode> <uid> <gid>\n"
477 "pipe <name> <mode> <uid> <gid>\n"
478 "sock <name> <mode> <uid> <gid>\n"
480 "<name> name of the file/dir/nod/etc in the archive\n"
481 "<location> location of the file in the current filesystem\n"
482 " expands shell variables quoted with ${}\n"
483 "<target> link target\n"
484 "<mode> mode/permissions of the file\n"
485 "<uid> user id (0=root)\n"
486 "<gid> group id (0=root)\n"
487 "<dev_type> device type (b=block, c=character)\n"
488 "<maj> major number of nod\n"
489 "<min> minor number of nod\n"
490 "<hard links> space separated list of other links to file\n"
493 "# A simple initramfs\n"
494 "dir /dev 0755 0 0\n"
495 "nod /dev/console 0600 0 0 c 5 1\n"
496 "dir /root 0700 0 0\n"
497 "dir /sbin 0755 0 0\n"
498 "file /sbin/kinit /usr/src/klibc/kinit/kinit 0755 0 0\n"
500 "<timestamp> is time in seconds since Epoch that will be used\n"
501 "as mtime for symlinks, special files and directories. The default\n"
502 "is to use the current time for these entries.\n",
506 static const struct file_handler file_handler_table[] = {
509 .handler = cpio_mkfile_line,
512 .handler = cpio_mknod_line,
515 .handler = cpio_mkdir_line,
518 .handler = cpio_mkslink_line,
521 .handler = cpio_mkpipe_line,
524 .handler = cpio_mksock_line,
531 #define LINE_SIZE (2 * PATH_MAX + 50)
533 int main (int argc, char *argv[])
536 char line[LINE_SIZE];
540 const char *filename;
542 default_mtime = time(NULL);
544 int opt = getopt(argc, argv, "t:h");
551 default_mtime = strtol(optarg, &invalid, 10);
552 if (!*optarg || *invalid) {
553 fprintf(stderr, "Invalid timestamp: %s\n",
562 exit(opt == 'h' ? 0 : 1);
567 * Timestamps after 2106-02-07 06:28:15 UTC have an ascii hex time_t
568 * representation that exceeds 8 chars and breaks the cpio header
571 if (default_mtime > 0xffffffff) {
572 fprintf(stderr, "ERROR: Timestamp too large for cpio format\n");
576 if (argc - optind != 1) {
580 filename = argv[optind];
581 if (!strcmp(filename, "-"))
583 else if (!(cpio_list = fopen(filename, "r"))) {
584 fprintf(stderr, "ERROR: unable to open '%s': %s\n\n",
585 filename, strerror(errno));
590 while (fgets(line, LINE_SIZE, cpio_list)) {
592 size_t slen = strlen(line);
597 /* comment - skip to next line */
601 if (! (type = strtok(line, " \t"))) {
603 "ERROR: incorrect format, could not locate file type line %d: '%s'\n",
614 if (slen == strlen(type)) {
615 /* must be an empty line */
619 if (! (args = strtok(NULL, "\n"))) {
621 "ERROR: incorrect format, newline required line %d: '%s'\n",
626 for (type_idx = 0; file_handler_table[type_idx].type; type_idx++) {
628 if (! strcmp(line, file_handler_table[type_idx].type)) {
629 if ((rc = file_handler_table[type_idx].handler(args))) {
631 fprintf(stderr, " line %d\n", line_nr);
637 if (NULL == file_handler_table[type_idx].type) {
638 fprintf(stderr, "unknown file type line %d: '%s'\n",