diff --git a/image.c b/image.c index 6f43405..64c31ad 100644 --- a/image.c +++ b/image.c @@ -34,51 +34,6 @@ #define DATA_DIR_CERT_TABLE 4 -struct image *image_load(const char *filename) -{ - struct stat statbuf; - struct image *image; - int rc; - - image = talloc(NULL, struct image); - if (!image) { - perror("talloc(image)"); - return NULL; - } - - image->fd = open(filename, O_RDONLY); - if (image->fd < 0) { - perror("open"); - goto err; - } - - rc = fstat(image->fd, &statbuf); - if (rc) { - perror("fstat"); - goto err; - } - - image->size = statbuf.st_size; - - image->buf = talloc_size(image, image->size); - if (!image->buf) { - perror("talloc(buf)"); - goto err; - } - - if (!read_all(image->fd, image->buf, image->size)) { - perror("read_all"); - fprintf(stderr, "error reading input file\n"); - goto err; - } - - lseek(image->fd, 0, SEEK_SET); - return image; -err: - talloc_free(image); - return NULL; -} - /** * The PE/COFF headers export struct fields as arrays of chars. So, define * a couple of accessor functions that allow fields to be deferenced as their @@ -105,7 +60,7 @@ static uint16_t __pehdr_u16(char field[]) #define pehdr_u32(f) __pehdr_u32(f + BUILD_ASSERT_OR_ZERO(sizeof(f) == 4)) #define pehdr_u16(f) __pehdr_u16(f + BUILD_ASSERT_OR_ZERO(sizeof(f) == 2)) -int image_pecoff_parse(struct image *image) +static int image_pecoff_parse(struct image *image) { char nt_sig[] = {'P', 'E', 0, 0}; size_t size = image->size; @@ -184,6 +139,56 @@ int image_pecoff_parse(struct image *image) return 0; } +struct image *image_load(const char *filename) +{ + struct stat statbuf; + struct image *image; + int rc; + + image = talloc(NULL, struct image); + if (!image) { + perror("talloc(image)"); + return NULL; + } + + image->fd = open(filename, O_RDONLY); + if (image->fd < 0) { + perror("open"); + goto err; + } + + rc = fstat(image->fd, &statbuf); + if (rc) { + perror("fstat"); + goto err; + } + + image->size = statbuf.st_size; + + image->buf = talloc_size(image, image->size); + if (!image->buf) { + perror("talloc(buf)"); + goto err; + } + + if (!read_all(image->fd, image->buf, image->size)) { + perror("read_all"); + fprintf(stderr, "error reading input file\n"); + goto err; + } + + lseek(image->fd, 0, SEEK_SET); + + rc = image_pecoff_parse(image); + if (rc) + goto err; + + return image; +err: + talloc_free(image); + return NULL; +} + static int align_up(int size, int align) { return (size + align - 1) & ~(align - 1); diff --git a/image.h b/image.h index 4bbbf31..8df0d9d 100644 --- a/image.h +++ b/image.h @@ -77,7 +77,6 @@ struct cert_table_header { struct image *image_load(const char *filename); -int image_pecoff_parse(struct image *image); int image_find_regions(struct image *image); int image_hash_sha256(struct image *image, uint8_t digest[]); int image_write(struct image *image, const char *filename); diff --git a/sbattach.c b/sbattach.c index 7cce654..b2c217c 100644 --- a/sbattach.c +++ b/sbattach.c @@ -214,8 +214,6 @@ int main(int argc, char **argv) return EXIT_FAILURE; } - image_pecoff_parse(image); - rc = 0; if (action == ACTION_ATTACH) diff --git a/sbsign.c b/sbsign.c index 884772b..24bc759 100644 --- a/sbsign.c +++ b/sbsign.c @@ -166,8 +166,6 @@ int main(int argc, char **argv) talloc_steal(ctx, ctx->image); - image_pecoff_parse(ctx->image); - image_find_regions(ctx->image); ERR_load_crypto_strings(); diff --git a/sbverify.c b/sbverify.c index cb8f9b4..e472b1e 100644 --- a/sbverify.c +++ b/sbverify.c @@ -239,7 +239,6 @@ int main(int argc, char **argv) return EXIT_FAILURE; } - image_pecoff_parse(image); image_find_regions(image); if (detached_sig_filename)