diff options
Diffstat (limited to 'usr/gen_init_cpio.c')
| -rw-r--r-- | usr/gen_init_cpio.c | 377 |
1 files changed, 263 insertions, 114 deletions
diff --git a/usr/gen_init_cpio.c b/usr/gen_init_cpio.c index af8c925e93eb..b7296edc6626 100644 --- a/usr/gen_init_cpio.c +++ b/usr/gen_init_cpio.c @@ -1,5 +1,9 @@ +// SPDX-License-Identifier: GPL-2.0 +#define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> +#include <stdint.h> +#include <stdbool.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> @@ -19,64 +23,75 @@ #define xstr(s) #s #define str(s) xstr(s) +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define CPIO_HDR_LEN 110 +#define CPIO_TRAILER "TRAILER!!!" +#define padlen(_off, _align) (((_align) - ((_off) & ((_align) - 1))) % (_align)) +/* zero-padding the filename field for data alignment is limited by PATH_MAX */ +static char padding[PATH_MAX]; static unsigned int offset; static unsigned int ino = 721; static time_t default_mtime; +static bool do_file_mtime; +static bool do_csum = false; +static int outfd = STDOUT_FILENO; +static unsigned int dalign; struct file_handler { const char *type; int (*handler)(const char *line); }; -static void push_string(const char *name) +static int push_buf(const char *name, size_t name_len) { - unsigned int name_len = strlen(name) + 1; + ssize_t len; + + len = write(outfd, name, name_len); + if (len != name_len) + return -1; - fputs(name, stdout); - putchar(0); offset += name_len; + return 0; } -static void push_pad (void) +static int push_pad(size_t padlen) { - while (offset & 3) { - putchar(0); - offset++; - } -} + ssize_t len = 0; -static void push_rest(const char *name) -{ - unsigned int name_len = strlen(name) + 1; - unsigned int tmp_ofs; + if (!padlen) + return 0; - fputs(name, stdout); - putchar(0); - offset += name_len; + if (padlen < sizeof(padding)) + len = write(outfd, padding, padlen); + if (len != padlen) + return -1; - tmp_ofs = name_len + 110; - while (tmp_ofs & 3) { - putchar(0); - offset++; - tmp_ofs++; - } + offset += padlen; + return 0; } -static void push_hdr(const char *s) +static int push_rest(const char *name, size_t name_len) { - fputs(s, stdout); - offset += 110; + ssize_t len; + + len = write(outfd, name, name_len); + if (len != name_len) + return -1; + + offset += name_len; + + return push_pad(padlen(name_len + CPIO_HDR_LEN, 4)); } -static void cpio_trailer(void) +static int cpio_trailer(void) { - char s[256]; - const char name[] = "TRAILER!!!"; + int len; + unsigned int namesize = sizeof(CPIO_TRAILER); - sprintf(s, "%s%08X%08X%08lX%08lX%08X%08lX" + len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX" "%08X%08X%08X%08X%08X%08X%08X", - "070701", /* magic */ + do_csum ? "070702" : "070701", /* magic */ 0, /* ino */ 0, /* mode */ (long) 0, /* uid */ @@ -88,46 +103,58 @@ static void cpio_trailer(void) 0, /* minor */ 0, /* rmajor */ 0, /* rminor */ - (unsigned)strlen(name)+1, /* namesize */ + namesize, /* namesize */ 0); /* chksum */ - push_hdr(s); - push_rest(name); + offset += len; - while (offset % 512) { - putchar(0); - offset++; - } + if (len != CPIO_HDR_LEN || + push_rest(CPIO_TRAILER, namesize) < 0 || + push_pad(padlen(offset, 512)) < 0) + return -1; + + if (fsync(outfd) < 0 && errno != EINVAL) + return -1; + + return 0; } static int cpio_mkslink(const char *name, const char *target, unsigned int mode, uid_t uid, gid_t gid) { - char s[256]; + int len; + unsigned int namesize, targetsize = strlen(target) + 1; if (name[0] == '/') name++; - sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX" + namesize = strlen(name) + 1; + + len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX" "%08X%08X%08X%08X%08X%08X%08X", - "070701", /* magic */ + do_csum ? "070702" : "070701", /* magic */ ino++, /* ino */ S_IFLNK | mode, /* mode */ (long) uid, /* uid */ (long) gid, /* gid */ 1, /* nlink */ (long) default_mtime, /* mtime */ - (unsigned)strlen(target)+1, /* filesize */ + targetsize, /* filesize */ 3, /* major */ 1, /* minor */ 0, /* rmajor */ 0, /* rminor */ - (unsigned)strlen(name) + 1,/* namesize */ + namesize, /* namesize */ 0); /* chksum */ - push_hdr(s); - push_string(name); - push_pad(); - push_string(target); - push_pad(); + offset += len; + + if (len != CPIO_HDR_LEN || + push_buf(name, namesize) < 0 || + push_pad(padlen(offset, 4)) < 0 || + push_buf(target, targetsize) < 0 || + push_pad(padlen(offset, 4)) < 0) + return -1; + return 0; + } static int cpio_mkslink_line(const char *line) @@ -151,13 +178,16 @@ static int cpio_mkslink_line(const char *line) static int cpio_mkgeneric(const char *name, unsigned int mode, uid_t uid, gid_t gid) { - char s[256]; + int len; + unsigned int namesize; if (name[0] == '/') name++; - sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX" + namesize = strlen(name) + 1; + + len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX" "%08X%08X%08X%08X%08X%08X%08X", - "070701", /* magic */ + do_csum ? "070702" : "070701", /* magic */ ino++, /* ino */ mode, /* mode */ (long) uid, /* uid */ @@ -169,10 +199,14 @@ static int cpio_mkgeneric(const char *name, unsigned int mode, 1, /* minor */ 0, /* rmajor */ 0, /* rminor */ - (unsigned)strlen(name) + 1,/* namesize */ + namesize, /* namesize */ 0); /* chksum */ - push_hdr(s); - push_rest(name); + offset += len; + + if (len != CPIO_HDR_LEN || + push_rest(name, namesize) < 0) + return -1; + return 0; } @@ -187,7 +221,7 @@ struct generic_type { mode_t mode; }; -static struct generic_type generic_type_table[] = { +static const struct generic_type generic_type_table[] = { [GT_DIR] = { .type = "dir", .mode = S_IFDIR @@ -240,7 +274,8 @@ static int cpio_mknod(const char *name, unsigned int mode, uid_t uid, gid_t gid, char dev_type, unsigned int maj, unsigned int min) { - char s[256]; + int len; + unsigned int namesize; if (dev_type == 'b') mode |= S_IFBLK; @@ -249,9 +284,11 @@ static int cpio_mknod(const char *name, unsigned int mode, if (name[0] == '/') name++; - sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX" + namesize = strlen(name) + 1; + + len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX" "%08X%08X%08X%08X%08X%08X%08X", - "070701", /* magic */ + do_csum ? "070702" : "070701", /* magic */ ino++, /* ino */ mode, /* mode */ (long) uid, /* uid */ @@ -263,10 +300,14 @@ static int cpio_mknod(const char *name, unsigned int mode, 1, /* minor */ maj, /* rmajor */ min, /* rminor */ - (unsigned)strlen(name) + 1,/* namesize */ + namesize, /* namesize */ 0); /* chksum */ - push_hdr(s); - push_rest(name); + offset += len; + + if (len != CPIO_HDR_LEN || + push_rest(name, namesize) < 0) + return -1; + return 0; } @@ -291,19 +332,42 @@ static int cpio_mknod_line(const char *line) return rc; } +static int cpio_mkfile_csum(int fd, unsigned long size, uint32_t *csum) +{ + while (size) { + unsigned char filebuf[65536]; + ssize_t this_read; + size_t i, this_size = MIN(size, sizeof(filebuf)); + + this_read = read(fd, filebuf, this_size); + if (this_read <= 0 || this_read > this_size) + return -1; + + for (i = 0; i < this_read; i++) + *csum += filebuf[i]; + + size -= this_read; + } + /* seek back to the start for data segment I/O */ + if (lseek(fd, 0, SEEK_SET) < 0) + return -1; + + return 0; +} + static int cpio_mkfile(const char *name, const char *location, unsigned int mode, uid_t uid, gid_t gid, unsigned int nlinks) { - char s[256]; - char *filebuf = NULL; struct stat buf; - long size; - int file = -1; - int retval; + unsigned long size; + int file, retval, len; int rc = -1; - int namesize; + time_t mtime; + int namesize, namepadlen; unsigned int i; + uint32_t csum = 0; + ssize_t this_read; mode |= S_IFREG; @@ -319,87 +383,133 @@ static int cpio_mkfile(const char *name, const char *location, goto error; } - filebuf = malloc(buf.st_size); - if (!filebuf) { - fprintf (stderr, "out of memory\n"); + if (do_file_mtime) { + mtime = default_mtime; + } else { + mtime = buf.st_mtime; + if (mtime > 0xffffffff) { + fprintf(stderr, "%s: Timestamp exceeds maximum cpio timestamp, clipping.\n", + location); + mtime = 0xffffffff; + } + + if (mtime < 0) { + fprintf(stderr, "%s: Timestamp negative, clipping.\n", + location); + mtime = 0; + } + } + + if (buf.st_size > 0xffffffff) { + fprintf(stderr, "%s: Size exceeds maximum cpio file size\n", + location); goto error; } - retval = read (file, filebuf, buf.st_size); - if (retval < 0) { - fprintf (stderr, "Can not read %s file\n", location); + if (do_csum && cpio_mkfile_csum(file, buf.st_size, &csum) < 0) { + fprintf(stderr, "Failed to checksum file %s\n", location); goto error; } size = 0; + namepadlen = 0; for (i = 1; i <= nlinks; i++) { - /* data goes on last link */ - if (i == nlinks) size = buf.st_size; - if (name[0] == '/') name++; namesize = strlen(name) + 1; - sprintf(s,"%s%08X%08X%08lX%08lX%08X%08lX" + + /* data goes on last link, after any alignment padding */ + if (i == nlinks) + size = buf.st_size; + + if (dalign && size > dalign) { + namepadlen = padlen(offset + CPIO_HDR_LEN + namesize, + dalign); + if (namesize + namepadlen > PATH_MAX) { + fprintf(stderr, + "%s: best-effort alignment %u missed\n", + name, dalign); + namepadlen = 0; + } + } + + len = dprintf(outfd, "%s%08X%08X%08lX%08lX%08X%08lX" "%08lX%08X%08X%08X%08X%08X%08X", - "070701", /* magic */ + do_csum ? "070702" : "070701", /* magic */ ino, /* ino */ mode, /* mode */ (long) uid, /* uid */ (long) gid, /* gid */ nlinks, /* nlink */ - (long) buf.st_mtime, /* mtime */ + (long) mtime, /* mtime */ size, /* filesize */ 3, /* major */ 1, /* minor */ 0, /* rmajor */ 0, /* rminor */ - namesize, /* namesize */ - 0); /* chksum */ - push_hdr(s); - push_string(name); - push_pad(); + namesize + namepadlen, /* namesize */ + size ? csum : 0); /* chksum */ + offset += len; + + if (len != CPIO_HDR_LEN || + push_buf(name, namesize) < 0 || + push_pad(namepadlen ? namepadlen : padlen(offset, 4)) < 0) + goto error; if (size) { - if (fwrite(filebuf, size, 1, stdout) != 1) { + this_read = copy_file_range(file, NULL, outfd, NULL, size, 0); + if (this_read > 0) { + if (this_read > size) + goto error; + offset += this_read; + size -= this_read; + } + /* short or failed copy falls back to read/write... */ + } + + while (size) { + unsigned char filebuf[65536]; + size_t this_size = MIN(size, sizeof(filebuf)); + + this_read = read(file, filebuf, this_size); + if (this_read <= 0 || this_read > this_size) { + fprintf(stderr, "Can not read %s file\n", location); + goto error; + } + + if (write(outfd, filebuf, this_read) != this_read) { fprintf(stderr, "writing filebuf failed\n"); goto error; } - offset += size; - push_pad(); + offset += this_read; + size -= this_read; } + if (push_pad(padlen(offset, 4)) < 0) + goto error; name += namesize; } ino++; rc = 0; - + error: - if (filebuf) free(filebuf); - if (file >= 0) close(file); + if (file >= 0) + close(file); return rc; } static char *cpio_replace_env(char *new_location) { char expanded[PATH_MAX + 1]; - char env_var[PATH_MAX + 1]; - char *start; - char *end; - - for (start = NULL; (start = strstr(new_location, "${")); ) { - end = strchr(start, '}'); - if (start < end) { - *env_var = *expanded = '\0'; - strncat(env_var, start + 2, end - start - 2); - strncat(expanded, new_location, start - new_location); - strncat(expanded, getenv(env_var), - PATH_MAX - strlen(expanded)); - strncat(expanded, end + 1, - PATH_MAX - strlen(expanded)); - strncpy(new_location, expanded, PATH_MAX); - new_location[PATH_MAX] = 0; - } else - break; + char *start, *end, *var; + + while ((start = strstr(new_location, "${")) && + (end = strchr(start + 2, '}'))) { + *start = *end = 0; + var = getenv(start + 2); + snprintf(expanded, sizeof expanded, "%s%s%s", + new_location, var ? var : "", end + 1); + strcpy(new_location, expanded); } return new_location; @@ -460,7 +570,7 @@ static int cpio_mkfile_line(const char *line) static void usage(const char *prog) { fprintf(stderr, "Usage:\n" - "\t%s [-t <timestamp>] <cpio_list>\n" + "\t%s [-t <timestamp>] [-c] [-o <output_file>] [-a <data_align>] <cpio_list>\n" "\n" "<cpio_list> is a file containing newline separated entries that\n" "describe the files to be included in the initramfs archive:\n" @@ -494,12 +604,18 @@ static void usage(const char *prog) "file /sbin/kinit /usr/src/klibc/kinit/kinit 0755 0 0\n" "\n" "<timestamp> is time in seconds since Epoch that will be used\n" - "as mtime for symlinks, special files and directories. The default\n" - "is to use the current time for these entries.\n", + "as mtime for symlinks, directories, regular and special files.\n" + "The default is to use the current time for all files, but\n" + "preserve modification time for regular files.\n" + "-c: calculate and store 32-bit checksums for file data.\n" + "<output_file>: write cpio to this file instead of stdout\n" + "<data_align>: attempt to align file data by zero-padding the\n" + "filename field up to data_align. Must be a multiple of 4.\n" + "Alignment is best-effort; PATH_MAX limits filename padding.\n", prog); } -struct file_handler file_handler_table[] = { +static const struct file_handler file_handler_table[] = { { .type = "file", .handler = cpio_mkfile_line, @@ -537,7 +653,7 @@ int main (int argc, char *argv[]) default_mtime = time(NULL); while (1) { - int opt = getopt(argc, argv, "t:h"); + int opt = getopt(argc, argv, "t:cho:a:"); char *invalid; if (opt == -1) @@ -551,6 +667,29 @@ int main (int argc, char *argv[]) usage(argv[0]); exit(1); } + do_file_mtime = true; + break; + case 'c': + do_csum = true; + break; + case 'o': + outfd = open(optarg, + O_WRONLY | O_CREAT | O_LARGEFILE | O_TRUNC, + 0600); + if (outfd < 0) { + fprintf(stderr, "failed to open %s\n", optarg); + usage(argv[0]); + exit(1); + } + break; + case 'a': + dalign = strtoul(optarg, &invalid, 10); + if (!*optarg || *invalid || (dalign & 3)) { + fprintf(stderr, "Invalid data_align: %s\n", + optarg); + usage(argv[0]); + exit(1); + } break; case 'h': case '?': @@ -559,6 +698,16 @@ int main (int argc, char *argv[]) } } + /* + * Timestamps after 2106-02-07 06:28:15 UTC have an ascii hex time_t + * representation that exceeds 8 chars and breaks the cpio header + * specification. Negative timestamps similarly exceed 8 chars. + */ + if (default_mtime > 0xffffffff || default_mtime < 0) { + fprintf(stderr, "ERROR: Timestamp out of range for cpio format\n"); + exit(1); + } + if (argc - optind != 1) { usage(argv[0]); exit(1); @@ -626,7 +775,7 @@ int main (int argc, char *argv[]) } } if (ec == 0) - cpio_trailer(); + ec = cpio_trailer(); exit(ec); } |
