image: always parse image regions

Rather than only calling image_find_regions when we want to sign or
verify image, call it when the image is loaded. We'll want to use the
parse data later, which will require it to be present on all instances
of an image.

Signed-off-by: Jeremy Kerr <jeremy.kerr@canonical.com>
This commit is contained in:
Jeremy Kerr 2012-08-13 13:47:44 +08:00
parent 36a14ed978
commit 8a55df5e96
4 changed files with 30 additions and 31 deletions

56
image.c
View file

@ -227,31 +227,6 @@ static int image_pecoff_parse(struct image *image)
return 0; return 0;
} }
struct image *image_load(const char *filename)
{
struct image *image;
int rc;
image = talloc(NULL, struct image);
if (!image) {
perror("talloc(image)");
return NULL;
}
rc = fileio_read_file(image, filename, &image->buf, &image->size);
if (rc)
goto err;
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) static int align_up(int size, int align)
{ {
return (size + align - 1) & ~(align - 1); return (size + align - 1) & ~(align - 1);
@ -274,7 +249,7 @@ static void set_region_from_range(struct region *region, void *start, void *end)
region->size = end - start; region->size = end - start;
} }
int image_find_regions(struct image *image) static int image_find_regions(struct image *image)
{ {
struct region *regions; struct region *regions;
void *buf = image->buf; void *buf = image->buf;
@ -391,6 +366,35 @@ int image_find_regions(struct image *image)
return 0; return 0;
} }
struct image *image_load(const char *filename)
{
struct image *image;
int rc;
image = talloc(NULL, struct image);
if (!image) {
perror("talloc(image)");
return NULL;
}
rc = fileio_read_file(image, filename, &image->buf, &image->size);
if (rc)
goto err;
rc = image_pecoff_parse(image);
if (rc)
goto err;
rc = image_find_regions(image);
if (rc)
goto err;
return image;
err:
talloc_free(image);
return NULL;
}
int image_hash_sha256(struct image *image, uint8_t digest[]) int image_hash_sha256(struct image *image, uint8_t digest[])
{ {
struct region *region; struct region *region;

View file

@ -98,7 +98,6 @@ struct cert_table_header {
struct image *image_load(const char *filename); struct image *image_load(const char *filename);
int image_find_regions(struct image *image);
int image_hash_sha256(struct image *image, uint8_t digest[]); int image_hash_sha256(struct image *image, uint8_t digest[]);
int image_add_signature(struct image *, void *sig, int size); int image_add_signature(struct image *, void *sig, int size);
void image_remove_signature(struct image *image); void image_remove_signature(struct image *image);

View file

@ -180,8 +180,6 @@ int main(int argc, char **argv)
talloc_steal(ctx, ctx->image); talloc_steal(ctx, ctx->image);
image_find_regions(ctx->image);
ERR_load_crypto_strings(); ERR_load_crypto_strings();
OpenSSL_add_all_digests(); OpenSSL_add_all_digests();
OpenSSL_add_all_ciphers(); OpenSSL_add_all_ciphers();

View file

@ -198,8 +198,6 @@ int main(int argc, char **argv)
return EXIT_FAILURE; return EXIT_FAILURE;
} }
image_find_regions(image);
if (detached_sig_filename) if (detached_sig_filename)
rc = load_detached_signature_data(image, detached_sig_filename, rc = load_detached_signature_data(image, detached_sig_filename,
&sig_buf, &sig_size); &sig_buf, &sig_size);