From b62ec2ba809f093f299111cf263ce51ca4d4a665 Mon Sep 17 00:00:00 2001 From: Amaury Pouly Date: Sat, 19 May 2012 13:53:13 +0200 Subject: [PATCH] imxtools: make sb api work with in-memory files Change-Id: I517244802efb118be214437bbaea2ef811e87aea --- utils/imxtools/sb.c | 56 +++++++++++++++++++++++++++++++-------------- utils/imxtools/sb.h | 5 ++++ 2 files changed, 44 insertions(+), 17 deletions(-) diff --git a/utils/imxtools/sb.c b/utils/imxtools/sb.c index 8e4b98b1a9..db6763c400 100644 --- a/utils/imxtools/sb.c +++ b/utils/imxtools/sb.c @@ -630,32 +630,55 @@ static uint32_t guess_alignment(uint32_t off) struct sb_file_t *sb_read_file(const char *filename, bool raw_mode, void *u, sb_color_printf cprintf, enum sb_error_t *err) { - uint8_t *buf = NULL; + return sb_read_file_ex(filename, 0, -1, raw_mode, u, cprintf, err); +} + +struct sb_file_t *sb_read_file_ex(const char *filename, size_t offset, size_t size, bool raw_mode, void *u, + sb_color_printf cprintf, enum sb_error_t *err) +{ + #define fatal(e, ...) \ + do { if(err) *err = e; \ + cprintf(u, true, GREY, __VA_ARGS__); \ + free(buf); \ + return NULL; } while(0) + + FILE *f = fopen(filename, "rb"); + void *buf = NULL; + if(f == NULL) + fatal(SB_OPEN_ERROR, "Cannot open file for reading\n"); + fseek(f, 0, SEEK_END); + size_t read_size = ftell(f); + fseek(f, offset, SEEK_SET); + if(size != (size_t)-1) + read_size = size; + buf = xmalloc(read_size); + if(fread(buf, read_size, 1, f) != 1) + { + fclose(f); + fatal(SB_READ_ERROR, "Cannot read file\n"); + } + fclose(f); + + struct sb_file_t *ret = sb_read_memory(buf, read_size, raw_mode, u, cprintf, err); + free(buf); + return ret; + + #undef fatal +} + +struct sb_file_t *sb_read_memory(void *buf, size_t filesize, bool raw_mode, void *u, + sb_color_printf cprintf, enum sb_error_t *err) +{ struct sb_file_t *sb_file = NULL; #define printf(c, ...) cprintf(u, false, c, __VA_ARGS__) #define fatal(e, ...) \ do { if(err) *err = e; \ cprintf(u, true, GREY, __VA_ARGS__); \ - free(buf); \ sb_free(sb_file); \ return NULL; } while(0) #define print_hex(c, p, len, nl) \ do { printf(c, ""); print_hex(p, len, nl); } while(0) - - FILE *f = fopen(filename, "rb"); - if(f == NULL) - fatal(SB_OPEN_ERROR, "Cannot open file for reading\n"); - fseek(f, 0, SEEK_END); - long filesize = ftell(f); - fseek(f, 0, SEEK_SET); - buf = xmalloc(filesize); - if(fread(buf, filesize, 1, f) != 1) - { - fclose(f); - fatal(SB_READ_ERROR, "Cannot read file\n"); - } - fclose(f); struct sha_1_params_t sha_1_params; sb_file = xmalloc(sizeof(struct sb_file_t)); @@ -1048,7 +1071,6 @@ struct sb_file_t *sb_read_file(const char *filename, bool raw_mode, void *u, printf(RED, " Failed\n"); fatal(SB_CHECKSUM_ERROR, "File SHA-1 error\n"); } - free(buf); return sb_file; #undef printf diff --git a/utils/imxtools/sb.h b/utils/imxtools/sb.h index e0f25b68a4..74d86f303a 100644 --- a/utils/imxtools/sb.h +++ b/utils/imxtools/sb.h @@ -228,6 +228,11 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename); typedef void (*sb_color_printf)(void *u, bool err, color_t c, const char *f, ...); struct sb_file_t *sb_read_file(const char *filename, bool raw_mode, void *u, sb_color_printf printf, enum sb_error_t *err); +/* use size_t(-1) to use maximum size */ +struct sb_file_t *sb_read_file_ex(const char *filename, size_t offset, size_t size, bool raw_mode, void *u, + sb_color_printf printf, enum sb_error_t *err); +struct sb_file_t *sb_read_memory(void *buffer, size_t size, bool raw_mode, void *u, + sb_color_printf printf, enum sb_error_t *err); void sb_fill_section_name(char name[5], uint32_t identifier); void sb_dump(struct sb_file_t *file, void *u, sb_color_printf printf);