From c483905b9244646e89bc36940da7ea5a65e37392 Mon Sep 17 00:00:00 2001 From: Amaury Pouly Date: Sun, 19 Feb 2012 18:36:57 +0100 Subject: imxtools: remove most calls to bug/bugp from core library. It should not exit() anymore on error except on malloc failure. Resource leaks on errors (especially I/O) are quite likely though. Change-Id: I6fcf72fb08fc683468b390d0b8745d31ca982b48 --- utils/imxtools/dbparser.c | 12 ++++++++++-- utils/imxtools/elftosb.c | 3 ++- utils/imxtools/misc.c | 28 +++++++++++++++++++++++----- utils/imxtools/misc.h | 2 +- utils/imxtools/sb.c | 16 ++++++++++++---- utils/imxtools/sbtoelf.c | 3 ++- 6 files changed, 50 insertions(+), 14 deletions(-) diff --git a/utils/imxtools/dbparser.c b/utils/imxtools/dbparser.c index b2027e5ad7..89a63b3767 100644 --- a/utils/imxtools/dbparser.c +++ b/utils/imxtools/dbparser.c @@ -460,13 +460,21 @@ struct cmd_file_t *db_parse_file(const char *file) size_t size; FILE *f = fopen(file, "r"); if(f == NULL) - bugp("Cannot open file '%s'", file); + { + if(g_debug) + perror("Cannot open db file"); + return NULL; + } fseek(f, 0, SEEK_END); size = ftell(f); fseek(f, 0, SEEK_SET); char *buf = xmalloc(size); if(fread(buf, size, 1, f) != 1) - bugp("Cannot read file '%s'", file); + { + if(g_debug) + perror("Cannot read db file"); + return NULL; + } fclose(f); if(g_debug) diff --git a/utils/imxtools/elftosb.c b/utils/imxtools/elftosb.c index 5bd247f27e..43bcf34849 100644 --- a/utils/imxtools/elftosb.c +++ b/utils/imxtools/elftosb.c @@ -380,7 +380,8 @@ int main(int argc, char **argv) break; case 'k': { - add_keys_from_file(optarg); + if(!add_keys_from_file(optarg)) + bug("Cannot keys from %s\n", optarg); break; } case 'z': diff --git a/utils/imxtools/misc.c b/utils/imxtools/misc.c index abbffbd5ae..a7cc096059 100644 --- a/utils/imxtools/misc.c +++ b/utils/imxtools/misc.c @@ -157,18 +157,27 @@ void clear_keys() g_key_array = NULL; } -void add_keys_from_file(const char *key_file) +bool add_keys_from_file(const char *key_file) { int size; FILE *fd = fopen(key_file, "r"); if(fd == NULL) - bug("opening key file failed"); + { + if(g_debug) + perror("cannot open key file"); + return false; + } fseek(fd, 0, SEEK_END); size = ftell(fd); fseek(fd, 0, SEEK_SET); char *buf = xmalloc(size + 1); if(fread(buf, 1, size, fd) != (size_t)size) - bug("reading key file"); + { + if(g_debug) + perror("Cannot read key file"); + fclose(fd); + return false; + } buf[size] = 0; fclose(fd); @@ -180,7 +189,11 @@ void add_keys_from_file(const char *key_file) struct crypto_key_t k; /* parse key */ if(!parse_key(&p, &k)) - bug("invalid key file"); + { + if(g_debug) + printf("invalid key file\n"); + return false; + } if(g_debug) { printf("Add key: "); @@ -189,7 +202,11 @@ void add_keys_from_file(const char *key_file) add_keys(&k, 1); /* request at least one space character before next key, or end of file */ if(*p != 0 && !isspace(*p)) - bug("invalid key file"); + { + if(g_debug) + printf("invalid key file\n"); + return false; + } /* skip whitespace */ while(isspace(*p)) p++; @@ -197,6 +214,7 @@ void add_keys_from_file(const char *key_file) break; } free(buf); + return true; } void print_hex(byte *data, int len, bool newline) diff --git a/utils/imxtools/misc.h b/utils/imxtools/misc.h index 7ff9ce545a..4df9bbe957 100644 --- a/utils/imxtools/misc.h +++ b/utils/imxtools/misc.h @@ -46,7 +46,7 @@ int convxdigit(char digit, byte *val); void print_hex(byte *data, int len, bool newline); void add_keys(key_array_t ka, int kac); bool parse_key(char **str, struct crypto_key_t *key); -void add_keys_from_file(const char *key_file); +bool add_keys_from_file(const char *key_file); void print_key(struct crypto_key_t *key, bool newline); void clear_keys(); diff --git a/utils/imxtools/sb.c b/utils/imxtools/sb.c index 5e636e99fd..b140df3eb3 100644 --- a/utils/imxtools/sb.c +++ b/utils/imxtools/sb.c @@ -122,7 +122,10 @@ static void compute_sb_offsets(struct sb_file_t *sb) sec->sec_size += ROUND_UP(inst->size, BLOCK_SIZE) / BLOCK_SIZE; } else - bug("die on inst %d\n", inst->inst); + { + if(g_debug) + printf("die on inst %d\n", inst->inst); + } } /* we need to make sure next section starts on the right alignment. * Since each section starts with a boot tag, we thus need to ensure @@ -299,7 +302,8 @@ void produce_sb_instruction(struct sb_inst_t *inst, case SB_INST_NOP: break; default: - bug("die\n"); + if(g_debug) + printf("die on invalid inst %d\n", inst->inst); } cmd->hdr.checksum = instruction_checksum(&cmd->hdr); } @@ -438,7 +442,11 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename) write(final_sig, 32); if(buf_p - buf != sb_hdr.image_size * BLOCK_SIZE) - bug("SB image buffer was not entirely filled !"); + { + if(g_debug) + printf("SB image buffer was not entirely filled !"); + return SB_ERROR; + } FILE *fd = fopen(filename, "wb"); if(fd == NULL) @@ -833,7 +841,7 @@ struct sb_file_t *sb_read_file(const char *filename, bool raw_mode, void *u, struct crypto_key_t k; char *env = getenv("SB_REAL_KEY"); if(!parse_key(&env, &k) || *env) - bug("Invalid SB_REAL_KEY\n"); + fatal(SB_ERROR, "Invalid SB_REAL_KEY\n"); memcpy(real_key, k.u.key, 16); } diff --git a/utils/imxtools/sbtoelf.c b/utils/imxtools/sbtoelf.c index fda70b1180..c1d1e9aa34 100644 --- a/utils/imxtools/sbtoelf.c +++ b/utils/imxtools/sbtoelf.c @@ -233,7 +233,8 @@ int main(int argc, char **argv) break; case 'k': { - add_keys_from_file(optarg); + if(!add_keys_from_file(optarg)) + bug("Cannot add keys from %s\n", optarg); break; } case 'z': -- cgit v1.2.3