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
This commit is contained in:
Amaury Pouly 2012-02-19 18:36:57 +01:00
parent 2d7a4e9dfa
commit c483905b92
6 changed files with 50 additions and 14 deletions

View file

@ -460,13 +460,21 @@ struct cmd_file_t *db_parse_file(const char *file)
size_t size; size_t size;
FILE *f = fopen(file, "r"); FILE *f = fopen(file, "r");
if(f == NULL) if(f == NULL)
bugp("Cannot open file '%s'", file); {
if(g_debug)
perror("Cannot open db file");
return NULL;
}
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
size = ftell(f); size = ftell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
char *buf = xmalloc(size); char *buf = xmalloc(size);
if(fread(buf, size, 1, f) != 1) 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); fclose(f);
if(g_debug) if(g_debug)

View file

@ -380,7 +380,8 @@ int main(int argc, char **argv)
break; break;
case 'k': case 'k':
{ {
add_keys_from_file(optarg); if(!add_keys_from_file(optarg))
bug("Cannot keys from %s\n", optarg);
break; break;
} }
case 'z': case 'z':

View file

@ -157,18 +157,27 @@ void clear_keys()
g_key_array = NULL; g_key_array = NULL;
} }
void add_keys_from_file(const char *key_file) bool add_keys_from_file(const char *key_file)
{ {
int size; int size;
FILE *fd = fopen(key_file, "r"); FILE *fd = fopen(key_file, "r");
if(fd == NULL) if(fd == NULL)
bug("opening key file failed"); {
if(g_debug)
perror("cannot open key file");
return false;
}
fseek(fd, 0, SEEK_END); fseek(fd, 0, SEEK_END);
size = ftell(fd); size = ftell(fd);
fseek(fd, 0, SEEK_SET); fseek(fd, 0, SEEK_SET);
char *buf = xmalloc(size + 1); char *buf = xmalloc(size + 1);
if(fread(buf, 1, size, fd) != (size_t)size) 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; buf[size] = 0;
fclose(fd); fclose(fd);
@ -180,7 +189,11 @@ void add_keys_from_file(const char *key_file)
struct crypto_key_t k; struct crypto_key_t k;
/* parse key */ /* parse key */
if(!parse_key(&p, &k)) if(!parse_key(&p, &k))
bug("invalid key file"); {
if(g_debug)
printf("invalid key file\n");
return false;
}
if(g_debug) if(g_debug)
{ {
printf("Add key: "); printf("Add key: ");
@ -189,7 +202,11 @@ void add_keys_from_file(const char *key_file)
add_keys(&k, 1); add_keys(&k, 1);
/* request at least one space character before next key, or end of file */ /* request at least one space character before next key, or end of file */
if(*p != 0 && !isspace(*p)) if(*p != 0 && !isspace(*p))
bug("invalid key file"); {
if(g_debug)
printf("invalid key file\n");
return false;
}
/* skip whitespace */ /* skip whitespace */
while(isspace(*p)) while(isspace(*p))
p++; p++;
@ -197,6 +214,7 @@ void add_keys_from_file(const char *key_file)
break; break;
} }
free(buf); free(buf);
return true;
} }
void print_hex(byte *data, int len, bool newline) void print_hex(byte *data, int len, bool newline)

View file

@ -46,7 +46,7 @@ int convxdigit(char digit, byte *val);
void print_hex(byte *data, int len, bool newline); void print_hex(byte *data, int len, bool newline);
void add_keys(key_array_t ka, int kac); void add_keys(key_array_t ka, int kac);
bool parse_key(char **str, struct crypto_key_t *key); 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 print_key(struct crypto_key_t *key, bool newline);
void clear_keys(); void clear_keys();

View file

@ -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; sec->sec_size += ROUND_UP(inst->size, BLOCK_SIZE) / BLOCK_SIZE;
} }
else 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. /* 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 * 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: case SB_INST_NOP:
break; break;
default: default:
bug("die\n"); if(g_debug)
printf("die on invalid inst %d\n", inst->inst);
} }
cmd->hdr.checksum = instruction_checksum(&cmd->hdr); 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); write(final_sig, 32);
if(buf_p - buf != sb_hdr.image_size * BLOCK_SIZE) 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"); FILE *fd = fopen(filename, "wb");
if(fd == NULL) 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; struct crypto_key_t k;
char *env = getenv("SB_REAL_KEY"); char *env = getenv("SB_REAL_KEY");
if(!parse_key(&env, &k) || *env) 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); memcpy(real_key, k.u.key, 16);
} }

View file

@ -233,7 +233,8 @@ int main(int argc, char **argv)
break; break;
case 'k': case 'k':
{ {
add_keys_from_file(optarg); if(!add_keys_from_file(optarg))
bug("Cannot add keys from %s\n", optarg);
break; break;
} }
case 'z': case 'z':