forked from len0rd/rockbox
sbtools: implement more complicated integer expression in db files, implement data sections, section attributes, file options, rework command line attributes
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30562 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
parent
d20f803af0
commit
66dce4b730
3 changed files with 593 additions and 243 deletions
|
|
@ -22,6 +22,7 @@
|
|||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "dbparser.h"
|
||||
|
||||
typedef uint8_t byte;
|
||||
|
|
@ -45,6 +46,10 @@ enum lexem_type_t
|
|||
LEX_LBRACE,
|
||||
LEX_RBRACE,
|
||||
LEX_RANGLE,
|
||||
LEX_OR,
|
||||
LEX_LSHIFT,
|
||||
LEX_COLON,
|
||||
LEX_LE,
|
||||
LEX_EOF
|
||||
};
|
||||
|
||||
|
|
@ -274,6 +279,8 @@ static void next_lexem(struct context_t *ctx, struct lexem_t *lexem)
|
|||
}
|
||||
if(eof(ctx)) ret_simple(LEX_EOF, 0);
|
||||
char c = cur_char(ctx);
|
||||
bool nv = next_valid(ctx, 1);
|
||||
char nc = nv ? next_char(ctx, 1) : 0;
|
||||
if(c == '(') ret_simple(LEX_LPAREN, 1);
|
||||
if(c == ')') ret_simple(LEX_RPAREN, 1);
|
||||
if(c == '{') ret_simple(LEX_LBRACE, 1);
|
||||
|
|
@ -281,6 +288,10 @@ static void next_lexem(struct context_t *ctx, struct lexem_t *lexem)
|
|||
if(c == '>') ret_simple(LEX_RANGLE, 1);
|
||||
if(c == '=') ret_simple(LEX_EQUAL, 1);
|
||||
if(c == ';') ret_simple(LEX_SEMICOLON, 1);
|
||||
if(c == ',') ret_simple(LEX_COLON, 1);
|
||||
if(c == '|') ret_simple(LEX_OR, 1);
|
||||
if(c == '<' && nv && nc == '<') ret_simple(LEX_LSHIFT, 2);
|
||||
if(c == '<' && nv && nc == '=') ret_simple(LEX_LE, 2);
|
||||
if(c == '"') return parse_string(ctx, lexem);
|
||||
if(c == '\'') return parse_ascii_number(ctx, lexem);
|
||||
if(isdigit(c)) return parse_number(ctx, lexem);
|
||||
|
|
@ -304,6 +315,8 @@ static void log_lexem(struct lexem_t *lexem)
|
|||
case LEX_SEMICOLON: printf(";"); break;
|
||||
case LEX_NUMBER: printf("num(%d)", lexem->num); break;
|
||||
case LEX_STRING: printf("str(%s)", lexem->str); break;
|
||||
case LEX_OR: printf("|"); break;
|
||||
case LEX_LSHIFT: printf("<<"); break;
|
||||
default: printf("<unk>");
|
||||
}
|
||||
}
|
||||
|
|
@ -321,11 +334,15 @@ struct cmd_source_t *db_find_source_by_id(struct cmd_file_t *cmd_file, const cha
|
|||
return NULL;
|
||||
}
|
||||
|
||||
static void generate_default_version(struct sb_version_t *ver)
|
||||
struct cmd_option_t *db_find_option_by_id(struct cmd_option_t *opt, const char *name)
|
||||
{
|
||||
ver->major = 0x999;
|
||||
ver->minor = 0x999;
|
||||
ver->revision = 0x999;
|
||||
while(opt)
|
||||
{
|
||||
if(strcmp(opt->name, name) == 0)
|
||||
return opt;
|
||||
opt = opt->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#define INVALID_SB_SUBVERSION 0xffff
|
||||
|
|
@ -375,6 +392,68 @@ bool db_parse_sb_version(struct sb_version_t *ver, char *str)
|
|||
do { fprintf(stderr, "%s:%d: ", lexem.file, lexem.line); \
|
||||
fprintf(stderr, __VA_ARGS__); exit(2); } while(0)
|
||||
|
||||
struct lex_ctx_t
|
||||
{
|
||||
struct context_t ctx;
|
||||
struct lexem_t lexem;
|
||||
};
|
||||
|
||||
static inline void next(struct lex_ctx_t *ctx)
|
||||
{
|
||||
next_lexem(&ctx->ctx, &ctx->lexem);
|
||||
}
|
||||
|
||||
static uint32_t parse_term_expr(struct lex_ctx_t *ctx, struct cmd_option_t *const_list)
|
||||
{
|
||||
uint32_t ret = 0;
|
||||
if(ctx->lexem.type == LEX_NUMBER)
|
||||
ret = ctx->lexem.num;
|
||||
else if(ctx->lexem.type == LEX_IDENTIFIER)
|
||||
{
|
||||
struct cmd_option_t *c = db_find_option_by_id(const_list, ctx->lexem.str);
|
||||
if(c == NULL)
|
||||
parse_error(ctx->lexem, "Undefined reference to constant '%s'\n", ctx->lexem.str);
|
||||
if(c->is_string)
|
||||
parse_error(ctx->lexem, "Internal error: constant '%s' is not an integer\n", ctx->lexem.str);
|
||||
ret = c->val;
|
||||
}
|
||||
else
|
||||
parse_error(ctx->lexem, "Number or constant identifier expected\n");
|
||||
next(ctx);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static uint32_t parse_shift_expr(struct lex_ctx_t *ctx, struct cmd_option_t *const_list)
|
||||
{
|
||||
uint32_t v = parse_term_expr(ctx, const_list);
|
||||
while(ctx->lexem.type == LEX_LSHIFT)
|
||||
{
|
||||
next(ctx);
|
||||
v <<= parse_term_expr(ctx, const_list);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
static uint32_t parse_or_expr(struct lex_ctx_t *ctx, struct cmd_option_t *const_list)
|
||||
{
|
||||
uint32_t v = parse_shift_expr(ctx, const_list);
|
||||
while(ctx->lexem.type == LEX_OR)
|
||||
{
|
||||
next(ctx);
|
||||
v |= parse_shift_expr(ctx, const_list);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
static uint32_t parse_intexpr(struct lex_ctx_t *ctx, struct cmd_option_t *const_list)
|
||||
{
|
||||
return parse_or_expr(ctx, const_list);
|
||||
}
|
||||
|
||||
#define NR_INITIAL_CONSTANTS 4
|
||||
static char *init_const_name[NR_INITIAL_CONSTANTS] = {"true", "false", "yes", "no"};
|
||||
static uint32_t init_const_value[NR_INITIAL_CONSTANTS] = {1, 0, 1, 0};
|
||||
|
||||
struct cmd_file_t *db_parse_file(const char *file)
|
||||
{
|
||||
size_t size;
|
||||
|
|
@ -394,19 +473,58 @@ struct cmd_file_t *db_parse_file(const char *file)
|
|||
struct cmd_file_t *cmd_file = xmalloc(sizeof(struct cmd_file_t));
|
||||
memset(cmd_file, 0, sizeof(struct cmd_file_t));
|
||||
|
||||
generate_default_version(&cmd_file->product_ver);
|
||||
generate_default_version(&cmd_file->component_ver);
|
||||
/* add initial constants */
|
||||
for(int i = 0; i < NR_INITIAL_CONSTANTS; i++)
|
||||
{
|
||||
struct cmd_option_t *opt = xmalloc(sizeof(struct cmd_option_t));
|
||||
memset(opt, 0, sizeof(struct cmd_option_t));
|
||||
opt->name = init_const_name[i];
|
||||
opt->is_string = false;
|
||||
opt->val = init_const_value[i];
|
||||
opt->next = cmd_file->constant_list;
|
||||
cmd_file->constant_list = opt;
|
||||
}
|
||||
|
||||
struct lexem_t lexem;
|
||||
struct context_t ctx;
|
||||
ctx.file = file;
|
||||
ctx.line = 1;
|
||||
ctx.begin = buf;
|
||||
ctx.ptr = buf;
|
||||
ctx.end = buf + size;
|
||||
#define next() next_lexem(&ctx, &lexem)
|
||||
struct lex_ctx_t lctx;
|
||||
lctx.ctx.file = file;
|
||||
lctx.ctx.line = 1;
|
||||
lctx.ctx.begin = buf;
|
||||
lctx.ctx.ptr = buf;
|
||||
lctx.ctx.end = buf + size;
|
||||
#define next() next(&lctx)
|
||||
#define lexem lctx.lexem
|
||||
/* init lexer */
|
||||
next();
|
||||
/* constants ? */
|
||||
if(lexem.type == LEX_IDENTIFIER && !strcmp(lexem.str, "constants"))
|
||||
{
|
||||
next();
|
||||
if(lexem.type != LEX_LBRACE)
|
||||
parse_error(lexem, "'{' expected after 'constants'\n");
|
||||
|
||||
while(true)
|
||||
{
|
||||
struct cmd_option_t *opt = xmalloc(sizeof(struct cmd_option_t));
|
||||
memset(opt, 0, sizeof(struct cmd_option_t));
|
||||
next();
|
||||
if(lexem.type == LEX_RBRACE)
|
||||
break;
|
||||
if(lexem.type != LEX_IDENTIFIER)
|
||||
parse_error(lexem, "Identifier expected in constants\n");
|
||||
opt->name = lexem.str;
|
||||
next();
|
||||
if(lexem.type != LEX_EQUAL)
|
||||
parse_error(lexem, "'=' expected after identifier\n");
|
||||
next();
|
||||
opt->is_string = false;
|
||||
opt->val = parse_intexpr(&lctx, cmd_file->constant_list);
|
||||
opt->next = cmd_file->constant_list;
|
||||
cmd_file->constant_list = opt;
|
||||
if(lexem.type != LEX_SEMICOLON)
|
||||
parse_error(lexem, "';' expected after string\n");
|
||||
}
|
||||
next();
|
||||
}
|
||||
/* options ? */
|
||||
if(lexem.type == LEX_IDENTIFIER && !strcmp(lexem.str, "options"))
|
||||
{
|
||||
|
|
@ -416,31 +534,31 @@ struct cmd_file_t *db_parse_file(const char *file)
|
|||
|
||||
while(true)
|
||||
{
|
||||
struct cmd_option_t *opt = xmalloc(sizeof(struct cmd_option_t));
|
||||
memset(opt, 0, sizeof(struct cmd_option_t));
|
||||
next();
|
||||
if(lexem.type == LEX_RBRACE)
|
||||
break;
|
||||
if(lexem.type != LEX_IDENTIFIER)
|
||||
parse_error(lexem, "Identifier expected in options\n");
|
||||
char *opt = lexem.str;
|
||||
opt->name = lexem.str;
|
||||
next();
|
||||
if(lexem.type != LEX_EQUAL)
|
||||
parse_error(lexem, "'=' expected after identifier\n");
|
||||
next();
|
||||
if(!strcmp(opt, "productVersion") || !strcmp(opt, "componentVersion"))
|
||||
if(lexem.type == LEX_STRING)
|
||||
{
|
||||
if(lexem.type != LEX_STRING)
|
||||
parse_error(lexem, "String expected after '='\n");
|
||||
bool ret;
|
||||
if(!strcmp(opt, "productVersion"))
|
||||
ret = db_parse_sb_version(&cmd_file->product_ver, lexem.str);
|
||||
else
|
||||
ret = db_parse_sb_version(&cmd_file->component_ver, lexem.str);
|
||||
if(!ret)
|
||||
parse_error(lexem, "Invalid product/component version");
|
||||
opt->is_string = true;
|
||||
opt->str = lexem.str;
|
||||
next();
|
||||
}
|
||||
else
|
||||
parse_error(lexem, "Unknown option '%s'\n", opt);
|
||||
next();
|
||||
{
|
||||
opt->is_string = false;
|
||||
opt->val = parse_intexpr(&lctx, cmd_file->constant_list);
|
||||
}
|
||||
opt->next = cmd_file->opt_list;
|
||||
cmd_file->opt_list = opt;
|
||||
if(lexem.type != LEX_SEMICOLON)
|
||||
parse_error(lexem, "';' expected after string\n");
|
||||
}
|
||||
|
|
@ -460,7 +578,6 @@ struct cmd_file_t *db_parse_file(const char *file)
|
|||
break;
|
||||
struct cmd_source_t *src = xmalloc(sizeof(struct cmd_source_t));
|
||||
memset(src, 0, sizeof(struct cmd_source_t));
|
||||
src->next = cmd_file->source_list;
|
||||
if(lexem.type != LEX_IDENTIFIER)
|
||||
parse_error(lexem, "identifier expected in sources\n");
|
||||
src->identifier = lexem.str;
|
||||
|
|
@ -468,16 +585,34 @@ struct cmd_file_t *db_parse_file(const char *file)
|
|||
if(lexem.type != LEX_EQUAL)
|
||||
parse_error(lexem, "'=' expected after identifier\n");
|
||||
next();
|
||||
if(lexem.type != LEX_STRING)
|
||||
parse_error(lexem, "String expected after '='\n");
|
||||
src->filename = lexem.str;
|
||||
next();
|
||||
if(lexem.type == LEX_STRING)
|
||||
{
|
||||
src->is_extern = false;
|
||||
src->filename = lexem.str;
|
||||
next();
|
||||
}
|
||||
else if(lexem.type == LEX_IDENTIFIER && !strcmp(lexem.str, "extern"))
|
||||
{
|
||||
src->is_extern = true;
|
||||
src->filename = "<extern>";
|
||||
next();
|
||||
if(lexem.type != LEX_LPAREN)
|
||||
parse_error(lexem, "'(' expected after 'extern'\n");
|
||||
next();
|
||||
src->extern_nr = parse_intexpr(&lctx, cmd_file->constant_list);
|
||||
if(lexem.type != LEX_RPAREN)
|
||||
parse_error(lexem, "')' expected\n");
|
||||
next();
|
||||
}
|
||||
else
|
||||
parse_error(lexem, "String or 'extern' expected after '='\n");
|
||||
if(lexem.type != LEX_SEMICOLON)
|
||||
parse_error(lexem, "';' expected after string\n");
|
||||
parse_error(lexem, "';' expected\n");
|
||||
if(db_find_source_by_id(cmd_file, src->identifier) != NULL)
|
||||
parse_error(lexem, "Duplicate source identifier\n");
|
||||
/* type filled later */
|
||||
src->type = CMD_SRC_UNK;
|
||||
src->next = cmd_file->source_list;
|
||||
cmd_file->source_list = src;
|
||||
}
|
||||
|
||||
|
|
@ -497,117 +632,143 @@ struct cmd_file_t *db_parse_file(const char *file)
|
|||
if(lexem.type != LEX_LPAREN)
|
||||
parse_error(lexem, "'(' expected after 'section'\n");
|
||||
next();
|
||||
/* can be a number or a 4 character long string */
|
||||
if(lexem.type == LEX_NUMBER)
|
||||
/* can be any number */
|
||||
sec->identifier = parse_intexpr(&lctx, cmd_file->constant_list);
|
||||
/* options ? */
|
||||
if(lexem.type == LEX_SEMICOLON)
|
||||
{
|
||||
sec->identifier = lexem.num;
|
||||
do
|
||||
{
|
||||
next();
|
||||
struct cmd_option_t *opt = xmalloc(sizeof(struct cmd_option_t));
|
||||
memset(opt, 0, sizeof(struct cmd_option_t));
|
||||
if(lexem.type != LEX_IDENTIFIER)
|
||||
parse_error(lexem, "Identifier expected for section option\n");
|
||||
opt->name = lexem.str;
|
||||
next();
|
||||
if(lexem.type != LEX_EQUAL)
|
||||
parse_error(lexem, "'=' expected after option identifier\n");
|
||||
next();
|
||||
if(lexem.type == LEX_STRING)
|
||||
{
|
||||
opt->is_string = true;
|
||||
opt->str = lexem.str;
|
||||
next();
|
||||
}
|
||||
else
|
||||
{
|
||||
opt->is_string = false;
|
||||
opt->val = parse_intexpr(&lctx, cmd_file->constant_list);
|
||||
}
|
||||
opt->next = sec->opt_list;
|
||||
sec->opt_list = opt;
|
||||
}while(lexem.type == LEX_COLON);
|
||||
}
|
||||
else
|
||||
parse_error(lexem, "Number expected as section identifier\n");
|
||||
|
||||
next();
|
||||
if(lexem.type != LEX_RPAREN)
|
||||
parse_error(lexem, "')' expected after section identifier\n");
|
||||
next();
|
||||
if(lexem.type != LEX_LBRACE)
|
||||
parse_error(lexem, "'{' expected after section directive\n");
|
||||
/* commands */
|
||||
while(true)
|
||||
if(lexem.type == LEX_LBRACE)
|
||||
{
|
||||
struct cmd_inst_t *inst = xmalloc(sizeof(struct cmd_inst_t));
|
||||
memset(inst, 0, sizeof(struct cmd_inst_t));
|
||||
next();
|
||||
if(lexem.type == LEX_RBRACE)
|
||||
break;
|
||||
if(lexem.type != LEX_IDENTIFIER)
|
||||
parse_error(lexem, "Instruction expected in section\n");
|
||||
if(strcmp(lexem.str, "load") == 0)
|
||||
inst->type = CMD_LOAD;
|
||||
else if(strcmp(lexem.str, "call") == 0)
|
||||
inst->type = CMD_CALL;
|
||||
else if(strcmp(lexem.str, "jump") == 0)
|
||||
inst->type = CMD_JUMP;
|
||||
else if(strcmp(lexem.str, "mode") == 0)
|
||||
inst->type = CMD_MODE;
|
||||
else
|
||||
parse_error(lexem, "Instruction expected in section\n");
|
||||
next();
|
||||
|
||||
if(inst->type == CMD_LOAD)
|
||||
sec->is_data = false;
|
||||
/* commands */
|
||||
while(true)
|
||||
{
|
||||
if(lexem.type != LEX_IDENTIFIER)
|
||||
parse_error(lexem, "Identifier expected after instruction\n");
|
||||
inst->identifier = lexem.str;
|
||||
if(db_find_source_by_id(cmd_file, inst->identifier) == NULL)
|
||||
parse_error(lexem, "Undefined reference to source '%s'\n", inst->identifier);
|
||||
struct cmd_inst_t *inst = xmalloc(sizeof(struct cmd_inst_t));
|
||||
memset(inst, 0, sizeof(struct cmd_inst_t));
|
||||
next();
|
||||
if(lexem.type == LEX_RANGLE)
|
||||
{
|
||||
// load at
|
||||
inst->type = CMD_LOAD_AT;
|
||||
next();
|
||||
if(lexem.type != LEX_NUMBER)
|
||||
parse_error(lexem, "Number expected for loading address\n");
|
||||
inst->addr = lexem.num;
|
||||
next();
|
||||
}
|
||||
if(lexem.type != LEX_SEMICOLON)
|
||||
parse_error(lexem, "';' expected after command\n");
|
||||
}
|
||||
else if(inst->type == CMD_CALL || inst->type == CMD_JUMP)
|
||||
{
|
||||
if(lexem.type == LEX_IDENTIFIER)
|
||||
if(lexem.type == LEX_RBRACE)
|
||||
break;
|
||||
if(lexem.type != LEX_IDENTIFIER)
|
||||
parse_error(lexem, "Instruction expected in section\n");
|
||||
if(strcmp(lexem.str, "load") == 0)
|
||||
inst->type = CMD_LOAD;
|
||||
else if(strcmp(lexem.str, "call") == 0)
|
||||
inst->type = CMD_CALL;
|
||||
else if(strcmp(lexem.str, "jump") == 0)
|
||||
inst->type = CMD_JUMP;
|
||||
else if(strcmp(lexem.str, "mode") == 0)
|
||||
inst->type = CMD_MODE;
|
||||
else
|
||||
parse_error(lexem, "Instruction expected in section\n");
|
||||
next();
|
||||
|
||||
if(inst->type == CMD_LOAD)
|
||||
{
|
||||
if(lexem.type != LEX_IDENTIFIER)
|
||||
parse_error(lexem, "Identifier expected after instruction\n");
|
||||
inst->identifier = lexem.str;
|
||||
if(db_find_source_by_id(cmd_file, inst->identifier) == NULL)
|
||||
parse_error(lexem, "Undefined reference to source '%s'\n", inst->identifier);
|
||||
next();
|
||||
if(lexem.type == LEX_RANGLE)
|
||||
{
|
||||
// load at
|
||||
inst->type = CMD_LOAD_AT;
|
||||
next();
|
||||
inst->addr = parse_intexpr(&lctx, cmd_file->constant_list);
|
||||
}
|
||||
if(lexem.type != LEX_SEMICOLON)
|
||||
parse_error(lexem, "';' expected after command\n");
|
||||
}
|
||||
else if(lexem.type == LEX_NUMBER)
|
||||
else if(inst->type == CMD_CALL || inst->type == CMD_JUMP)
|
||||
{
|
||||
inst->type = (inst->type == CMD_CALL) ? CMD_CALL_AT : CMD_JUMP_AT;
|
||||
inst->addr = lexem.num;
|
||||
next();
|
||||
if(lexem.type == LEX_IDENTIFIER)
|
||||
{
|
||||
inst->identifier = lexem.str;
|
||||
if(db_find_source_by_id(cmd_file, inst->identifier) == NULL)
|
||||
parse_error(lexem, "Undefined reference to source '%s'\n", inst->identifier);
|
||||
next();
|
||||
}
|
||||
else
|
||||
{
|
||||
inst->type = (inst->type == CMD_CALL) ? CMD_CALL_AT : CMD_JUMP_AT;
|
||||
inst->addr = parse_intexpr(&lctx, cmd_file->constant_list);
|
||||
}
|
||||
|
||||
if(lexem.type == LEX_LPAREN)
|
||||
{
|
||||
next();
|
||||
inst->argument = parse_intexpr(&lctx, cmd_file->constant_list);
|
||||
if(lexem.type != LEX_RPAREN)
|
||||
parse_error(lexem, "Expected closing brace\n");
|
||||
next();
|
||||
}
|
||||
if(lexem.type != LEX_SEMICOLON)
|
||||
parse_error(lexem, "';' expected after command\n");
|
||||
}
|
||||
else if(inst->type == CMD_MODE)
|
||||
{
|
||||
inst->argument = parse_intexpr(&lctx, cmd_file->constant_list);
|
||||
if(lexem.type != LEX_SEMICOLON)
|
||||
parse_error(lexem, "Expected ';' after command\n");
|
||||
}
|
||||
else
|
||||
parse_error(lexem, "Identifier or number expected after jump/load\n");
|
||||
|
||||
if(lexem.type == LEX_LPAREN)
|
||||
parse_error(lexem, "Internal error");
|
||||
if(end_list == NULL)
|
||||
{
|
||||
next();
|
||||
if(lexem.type != LEX_NUMBER)
|
||||
parse_error(lexem, "Expected numeral expression after (\n");
|
||||
inst->argument = lexem.num;
|
||||
next();
|
||||
if(lexem.type != LEX_RPAREN)
|
||||
parse_error(lexem, "Expected closing brace\n");
|
||||
next();
|
||||
sec->inst_list = inst;
|
||||
end_list = inst;
|
||||
}
|
||||
else
|
||||
{
|
||||
end_list->next = inst;
|
||||
end_list = inst;
|
||||
}
|
||||
if(lexem.type != LEX_SEMICOLON)
|
||||
parse_error(lexem, "Expected ';' after command\n");
|
||||
}
|
||||
else if(inst->type == CMD_MODE)
|
||||
{
|
||||
if(lexem.type != LEX_NUMBER)
|
||||
parse_error(lexem, "Number expected after 'mode'\n");
|
||||
inst->argument = lexem.num;
|
||||
next();
|
||||
if(lexem.type != LEX_SEMICOLON)
|
||||
parse_error(lexem, "Expected ';' after command\n");
|
||||
}
|
||||
else
|
||||
parse_error(lexem, "Internal error");
|
||||
if(end_list == NULL)
|
||||
{
|
||||
sec->inst_list = inst;
|
||||
end_list = inst;
|
||||
}
|
||||
else
|
||||
{
|
||||
end_list->next = inst;
|
||||
end_list = inst;
|
||||
}
|
||||
}
|
||||
else if(lexem.type == LEX_LE)
|
||||
{
|
||||
sec->is_data = true;
|
||||
next();
|
||||
if(lexem.type != LEX_IDENTIFIER)
|
||||
parse_error(lexem, "Identifier expected after '<='\n");
|
||||
sec->source_id = lexem.str;
|
||||
next();
|
||||
if(lexem.type != LEX_SEMICOLON)
|
||||
parse_error(lexem, "';' expected after identifier\n");
|
||||
}
|
||||
else
|
||||
parse_error(lexem, "'{' or '<=' expected after section directive\n");
|
||||
|
||||
if(end_sec == NULL)
|
||||
{
|
||||
|
|
@ -620,7 +781,13 @@ struct cmd_file_t *db_parse_file(const char *file)
|
|||
end_sec = sec;
|
||||
}
|
||||
}
|
||||
#undef lexem
|
||||
#undef next
|
||||
|
||||
return cmd_file;
|
||||
}
|
||||
|
||||
void db_generate_default_sb_version(struct sb_version_t *ver)
|
||||
{
|
||||
ver->major = ver->minor = ver->revision = 0x999;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -43,7 +43,11 @@ struct bin_param_t
|
|||
struct cmd_source_t
|
||||
{
|
||||
char *identifier;
|
||||
bool is_extern;
|
||||
// <union>
|
||||
int extern_nr;
|
||||
char *filename;
|
||||
// </union>
|
||||
struct cmd_source_t *next;
|
||||
/* for later use */
|
||||
enum cmd_source_type_t type;
|
||||
|
|
@ -72,23 +76,41 @@ struct cmd_inst_t
|
|||
struct cmd_inst_t *next;
|
||||
};
|
||||
|
||||
struct cmd_option_t
|
||||
{
|
||||
char *name;
|
||||
bool is_string;
|
||||
/* <union> */
|
||||
uint32_t val;
|
||||
char *str;
|
||||
/* </union> */
|
||||
struct cmd_option_t *next;
|
||||
};
|
||||
|
||||
struct cmd_section_t
|
||||
{
|
||||
uint32_t identifier;
|
||||
struct cmd_inst_t *inst_list;
|
||||
bool is_data;
|
||||
// <union>
|
||||
struct cmd_inst_t *inst_list;
|
||||
char *source_id;
|
||||
// </union>
|
||||
struct cmd_section_t *next;
|
||||
struct cmd_option_t *opt_list;
|
||||
};
|
||||
|
||||
struct cmd_file_t
|
||||
{
|
||||
struct sb_version_t product_ver;
|
||||
struct sb_version_t component_ver;
|
||||
struct cmd_option_t *opt_list;
|
||||
struct cmd_option_t *constant_list; /* constant all always integers */
|
||||
struct cmd_source_t *source_list;
|
||||
struct cmd_section_t *section_list;
|
||||
};
|
||||
|
||||
struct cmd_source_t *db_find_source_by_id(struct cmd_file_t *cmd_file, const char *id);
|
||||
struct cmd_option_t *db_find_option_by_id(struct cmd_option_t *opt, const char *name);
|
||||
bool db_parse_sb_version(struct sb_version_t *ver, char *str);
|
||||
void db_generate_default_sb_version(struct sb_version_t *ver);
|
||||
struct cmd_file_t *db_parse_file(const char *file);
|
||||
|
||||
#endif /* __DBPARSER__ */
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@
|
|||
#include <time.h>
|
||||
#include <stdarg.h>
|
||||
#include <strings.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#include "crypto.h"
|
||||
#include "elf.h"
|
||||
|
|
@ -46,6 +47,8 @@
|
|||
#define bugp(a) do { perror("ERROR: "a); exit(1); } while(0)
|
||||
|
||||
bool g_debug = false;
|
||||
char **g_extern;
|
||||
int g_extern_count;
|
||||
|
||||
#define ROUND_UP(val, round) ((((val) + (round) - 1) / (round)) * (round))
|
||||
|
||||
|
|
@ -107,6 +110,16 @@ typedef byte (*key_array_t)[16];
|
|||
int g_nr_keys;
|
||||
key_array_t g_key_array;
|
||||
|
||||
static void add_keys(key_array_t ka, int kac)
|
||||
{
|
||||
key_array_t new_ka = xmalloc((g_nr_keys + kac) * 16);
|
||||
memcpy(new_ka, g_key_array, g_nr_keys * 16);
|
||||
memcpy(new_ka + g_nr_keys, ka, kac * 16);
|
||||
free(g_key_array);
|
||||
g_key_array = new_ka;
|
||||
g_nr_keys += kac;
|
||||
}
|
||||
|
||||
static key_array_t read_keys(const char *key_file, int *num_keys)
|
||||
{
|
||||
int size;
|
||||
|
|
@ -168,6 +181,8 @@ static key_array_t read_keys(const char *key_file, int *num_keys)
|
|||
* command file to sb conversion
|
||||
*/
|
||||
|
||||
#define SB_INST_DATA 0xff
|
||||
|
||||
struct sb_inst_t
|
||||
{
|
||||
uint8_t inst; /* SB_INST_* */
|
||||
|
|
@ -186,6 +201,9 @@ struct sb_inst_t
|
|||
struct sb_section_t
|
||||
{
|
||||
uint32_t identifier;
|
||||
bool is_data;
|
||||
bool is_cleartext;
|
||||
// data sections are handled as a single SB_INST_DATA virtual instruction
|
||||
int nr_insts;
|
||||
struct sb_inst_t *insts;
|
||||
/* for production use */
|
||||
|
|
@ -221,6 +239,16 @@ static void elf_printf(void *user, bool error, const char *fmt, ...)
|
|||
va_end(args);
|
||||
}
|
||||
|
||||
static void resolve_extern(struct cmd_source_t *src)
|
||||
{
|
||||
if(!src->is_extern)
|
||||
return;
|
||||
src->is_extern = false;
|
||||
if(src->extern_nr < 0 || src->extern_nr >= g_extern_count)
|
||||
bug("There aren't enough file on command file to resolve extern(%d)\n", src->extern_nr);
|
||||
src->filename = g_extern[src->extern_nr];
|
||||
}
|
||||
|
||||
static void load_elf_by_id(struct cmd_file_t *cmd_file, const char *id)
|
||||
{
|
||||
struct cmd_source_t *src = db_find_source_by_id(cmd_file, id);
|
||||
|
|
@ -231,6 +259,9 @@ static void load_elf_by_id(struct cmd_file_t *cmd_file, const char *id)
|
|||
return;
|
||||
if(src->type != CMD_SRC_UNK)
|
||||
bug("source '%s' seen both as elf and binary file\n", id);
|
||||
/* resolve potential extern file */
|
||||
resolve_extern(src);
|
||||
/* load it */
|
||||
src->type = CMD_SRC_ELF;
|
||||
int fd = open(src->filename, O_RDONLY);
|
||||
if(fd < 0)
|
||||
|
|
@ -255,6 +286,9 @@ static void load_bin_by_id(struct cmd_file_t *cmd_file, const char *id)
|
|||
return;
|
||||
if(src->type != CMD_SRC_UNK)
|
||||
bug("source '%s' seen both as elf and binary file\n", id);
|
||||
/* resolve potential extern file */
|
||||
resolve_extern(src);
|
||||
/* load it */
|
||||
src->type = CMD_SRC_BIN;
|
||||
int fd = open(src->filename, O_RDONLY);
|
||||
if(fd < 0)
|
||||
|
|
@ -274,8 +308,8 @@ static struct sb_file_t *apply_cmd_file(struct cmd_file_t *cmd_file)
|
|||
struct sb_file_t *sb = xmalloc(sizeof(struct sb_file_t));
|
||||
memset(sb, 0, sizeof(struct sb_file_t));
|
||||
|
||||
sb->product_ver = cmd_file->product_ver;
|
||||
sb->component_ver = cmd_file->component_ver;
|
||||
db_generate_default_sb_version(&sb->product_ver);
|
||||
db_generate_default_sb_version(&sb->component_ver);
|
||||
|
||||
if(g_debug)
|
||||
printf("Applying command file...\n");
|
||||
|
|
@ -295,103 +329,136 @@ static struct sb_file_t *apply_cmd_file(struct cmd_file_t *cmd_file)
|
|||
{
|
||||
struct sb_section_t *sec = &sb->sections[i];
|
||||
sec->identifier = csec->identifier;
|
||||
/* count instructions */
|
||||
struct cmd_inst_t *cinst = csec->inst_list;
|
||||
while(cinst)
|
||||
{
|
||||
if(cinst->type == CMD_LOAD)
|
||||
{
|
||||
load_elf_by_id(cmd_file, cinst->identifier);
|
||||
struct elf_params_t *elf = &db_find_source_by_id(cmd_file, cinst->identifier)->elf;
|
||||
sec->nr_insts += elf_get_nr_sections(elf);
|
||||
}
|
||||
else if(cinst->type == CMD_JUMP || cinst->type == CMD_CALL)
|
||||
{
|
||||
load_elf_by_id(cmd_file, cinst->identifier);
|
||||
struct elf_params_t *elf = &db_find_source_by_id(cmd_file, cinst->identifier)->elf;
|
||||
if(!elf_get_start_addr(elf, NULL))
|
||||
bug("cannot jump/call '%s' because it has no starting point !\n", cinst->identifier);
|
||||
sec->nr_insts++;
|
||||
}
|
||||
else if(cinst->type == CMD_CALL_AT || cinst->type == CMD_JUMP_AT)
|
||||
{
|
||||
sec->nr_insts++;
|
||||
}
|
||||
else if(cinst->type == CMD_LOAD_AT)
|
||||
{
|
||||
load_bin_by_id(cmd_file, cinst->identifier);
|
||||
sec->nr_insts++;
|
||||
}
|
||||
else if(cinst->type == CMD_MODE)
|
||||
{
|
||||
sec->nr_insts++;
|
||||
}
|
||||
else
|
||||
bug("die\n");
|
||||
|
||||
cinst = cinst->next;
|
||||
}
|
||||
|
||||
sec->insts = xmalloc(sec->nr_insts * sizeof(struct sb_inst_t));
|
||||
memset(sec->insts, 0, sec->nr_insts * sizeof(struct sb_inst_t));
|
||||
/* flatten */
|
||||
int idx = 0;
|
||||
cinst = csec->inst_list;
|
||||
while(cinst)
|
||||
/* options */
|
||||
do
|
||||
{
|
||||
if(cinst->type == CMD_LOAD)
|
||||
struct cmd_option_t *opt = db_find_option_by_id(csec->opt_list, "cleartext");
|
||||
if(opt != NULL)
|
||||
{
|
||||
struct elf_params_t *elf = &db_find_source_by_id(cmd_file, cinst->identifier)->elf;
|
||||
struct elf_section_t *esec = elf->first_section;
|
||||
while(esec)
|
||||
if(opt->is_string)
|
||||
bug("Cleartext section attribute must be an integer\n");
|
||||
if(opt->val != 0 && opt->val != 1)
|
||||
bug("Cleartext section attribute must be 0 or 1\n");
|
||||
sec->is_cleartext = opt->val;
|
||||
}
|
||||
}while(0);
|
||||
|
||||
if(csec->is_data)
|
||||
{
|
||||
sec->is_data = true;
|
||||
sec->nr_insts = 1;
|
||||
sec->insts = xmalloc(sec->nr_insts * sizeof(struct sb_inst_t));
|
||||
memset(sec->insts, 0, sec->nr_insts * sizeof(struct sb_inst_t));
|
||||
|
||||
load_bin_by_id(cmd_file, csec->source_id);
|
||||
struct bin_param_t *bin = &db_find_source_by_id(cmd_file, csec->source_id)->bin;
|
||||
|
||||
sec->insts[0].inst = SB_INST_DATA;
|
||||
sec->insts[0].size = bin->size;
|
||||
sec->insts[0].data = bin->data;
|
||||
}
|
||||
else
|
||||
{
|
||||
sec->is_data = false;
|
||||
/* count instructions and loads things */
|
||||
struct cmd_inst_t *cinst = csec->inst_list;
|
||||
while(cinst)
|
||||
{
|
||||
if(cinst->type == CMD_LOAD)
|
||||
{
|
||||
if(esec->type == EST_LOAD)
|
||||
{
|
||||
sec->insts[idx].inst = SB_INST_LOAD;
|
||||
sec->insts[idx].addr = esec->addr;
|
||||
sec->insts[idx].size = esec->size;
|
||||
sec->insts[idx++].data = esec->section;
|
||||
}
|
||||
else if(esec->type == EST_FILL)
|
||||
{
|
||||
sec->insts[idx].inst = SB_INST_FILL;
|
||||
sec->insts[idx].addr = esec->addr;
|
||||
sec->insts[idx].size = esec->size;
|
||||
sec->insts[idx++].pattern = esec->pattern;
|
||||
}
|
||||
esec = esec->next;
|
||||
load_elf_by_id(cmd_file, cinst->identifier);
|
||||
struct elf_params_t *elf = &db_find_source_by_id(cmd_file, cinst->identifier)->elf;
|
||||
sec->nr_insts += elf_get_nr_sections(elf);
|
||||
}
|
||||
else if(cinst->type == CMD_JUMP || cinst->type == CMD_CALL)
|
||||
{
|
||||
load_elf_by_id(cmd_file, cinst->identifier);
|
||||
struct elf_params_t *elf = &db_find_source_by_id(cmd_file, cinst->identifier)->elf;
|
||||
if(!elf_get_start_addr(elf, NULL))
|
||||
bug("cannot jump/call '%s' because it has no starting point !\n", cinst->identifier);
|
||||
sec->nr_insts++;
|
||||
}
|
||||
else if(cinst->type == CMD_CALL_AT || cinst->type == CMD_JUMP_AT)
|
||||
{
|
||||
sec->nr_insts++;
|
||||
}
|
||||
else if(cinst->type == CMD_LOAD_AT)
|
||||
{
|
||||
load_bin_by_id(cmd_file, cinst->identifier);
|
||||
sec->nr_insts++;
|
||||
}
|
||||
else if(cinst->type == CMD_MODE)
|
||||
{
|
||||
sec->nr_insts++;
|
||||
}
|
||||
else
|
||||
bug("die\n");
|
||||
|
||||
cinst = cinst->next;
|
||||
}
|
||||
else if(cinst->type == CMD_JUMP || cinst->type == CMD_CALL)
|
||||
|
||||
sec->insts = xmalloc(sec->nr_insts * sizeof(struct sb_inst_t));
|
||||
memset(sec->insts, 0, sec->nr_insts * sizeof(struct sb_inst_t));
|
||||
/* flatten */
|
||||
int idx = 0;
|
||||
cinst = csec->inst_list;
|
||||
while(cinst)
|
||||
{
|
||||
struct elf_params_t *elf = &db_find_source_by_id(cmd_file, cinst->identifier)->elf;
|
||||
sec->insts[idx].argument = cinst->argument;
|
||||
sec->insts[idx].inst = (cinst->type == CMD_JUMP) ? SB_INST_JUMP : SB_INST_CALL;
|
||||
sec->insts[idx++].addr = elf->start_addr;
|
||||
if(cinst->type == CMD_LOAD)
|
||||
{
|
||||
struct elf_params_t *elf = &db_find_source_by_id(cmd_file, cinst->identifier)->elf;
|
||||
struct elf_section_t *esec = elf->first_section;
|
||||
while(esec)
|
||||
{
|
||||
if(esec->type == EST_LOAD)
|
||||
{
|
||||
sec->insts[idx].inst = SB_INST_LOAD;
|
||||
sec->insts[idx].addr = esec->addr;
|
||||
sec->insts[idx].size = esec->size;
|
||||
sec->insts[idx++].data = esec->section;
|
||||
}
|
||||
else if(esec->type == EST_FILL)
|
||||
{
|
||||
sec->insts[idx].inst = SB_INST_FILL;
|
||||
sec->insts[idx].addr = esec->addr;
|
||||
sec->insts[idx].size = esec->size;
|
||||
sec->insts[idx++].pattern = esec->pattern;
|
||||
}
|
||||
esec = esec->next;
|
||||
}
|
||||
}
|
||||
else if(cinst->type == CMD_JUMP || cinst->type == CMD_CALL)
|
||||
{
|
||||
struct elf_params_t *elf = &db_find_source_by_id(cmd_file, cinst->identifier)->elf;
|
||||
sec->insts[idx].argument = cinst->argument;
|
||||
sec->insts[idx].inst = (cinst->type == CMD_JUMP) ? SB_INST_JUMP : SB_INST_CALL;
|
||||
sec->insts[idx++].addr = elf->start_addr;
|
||||
}
|
||||
else if(cinst->type == CMD_JUMP_AT || cinst->type == CMD_CALL_AT)
|
||||
{
|
||||
sec->insts[idx].argument = cinst->argument;
|
||||
sec->insts[idx].inst = (cinst->type == CMD_JUMP_AT) ? SB_INST_JUMP : SB_INST_CALL;
|
||||
sec->insts[idx++].addr = cinst->addr;
|
||||
}
|
||||
else if(cinst->type == CMD_LOAD_AT)
|
||||
{
|
||||
struct bin_param_t *bin = &db_find_source_by_id(cmd_file, cinst->identifier)->bin;
|
||||
sec->insts[idx].inst = SB_INST_LOAD;
|
||||
sec->insts[idx].addr = cinst->addr;
|
||||
sec->insts[idx].data = bin->data;
|
||||
sec->insts[idx++].size = bin->size;
|
||||
}
|
||||
else if(cinst->type == CMD_MODE)
|
||||
{
|
||||
sec->insts[idx].inst = SB_INST_MODE;
|
||||
sec->insts[idx++].addr = cinst->argument;
|
||||
}
|
||||
else
|
||||
bug("die\n");
|
||||
|
||||
cinst = cinst->next;
|
||||
}
|
||||
else if(cinst->type == CMD_JUMP_AT || cinst->type == CMD_CALL_AT)
|
||||
{
|
||||
sec->insts[idx].argument = cinst->argument;
|
||||
sec->insts[idx].inst = (cinst->type == CMD_JUMP_AT) ? SB_INST_JUMP : SB_INST_CALL;
|
||||
sec->insts[idx++].addr = cinst->addr;
|
||||
}
|
||||
else if(cinst->type == CMD_LOAD_AT)
|
||||
{
|
||||
struct bin_param_t *bin = &db_find_source_by_id(cmd_file, cinst->identifier)->bin;
|
||||
sec->insts[idx].inst = SB_INST_LOAD;
|
||||
sec->insts[idx].addr = cinst->addr;
|
||||
sec->insts[idx].data = bin->data;
|
||||
sec->insts[idx++].size = bin->size;
|
||||
}
|
||||
else if(cinst->type == CMD_MODE)
|
||||
{
|
||||
sec->insts[idx].inst = SB_INST_MODE;
|
||||
sec->insts[idx++].addr = cinst->argument;
|
||||
}
|
||||
else
|
||||
bug("die\n");
|
||||
|
||||
cinst = cinst->next;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -399,7 +466,7 @@ static struct sb_file_t *apply_cmd_file(struct cmd_file_t *cmd_file)
|
|||
}
|
||||
|
||||
/**
|
||||
* Sb file production
|
||||
* SB file production
|
||||
*/
|
||||
|
||||
static void fill_gaps(struct sb_file_t *sb)
|
||||
|
|
@ -436,6 +503,16 @@ static void compute_sb_offsets(struct sb_file_t *sb)
|
|||
sb->image_size += sizeof(struct sb_instruction_tag_t) / BLOCK_SIZE;
|
||||
|
||||
struct sb_section_t *sec = &sb->sections[i];
|
||||
|
||||
if(g_debug)
|
||||
{
|
||||
printf("%s section 0x%08x", sec->is_data ? "Data" : "Boot",
|
||||
sec->identifier);
|
||||
if(sec->is_cleartext)
|
||||
printf(" (cleartext)");
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
sec->file_offset = sb->image_size;
|
||||
for(int j = 0; j < sec->nr_insts; j++)
|
||||
{
|
||||
|
|
@ -443,7 +520,7 @@ static void compute_sb_offsets(struct sb_file_t *sb)
|
|||
if(inst->inst == SB_INST_CALL || inst->inst == SB_INST_JUMP)
|
||||
{
|
||||
if(g_debug)
|
||||
printf("%s | addr=0x%08x | arg=0x%08x\n",
|
||||
printf(" %s | addr=0x%08x | arg=0x%08x\n",
|
||||
inst->inst == SB_INST_CALL ? "CALL" : "JUMP", inst->addr, inst->argument);
|
||||
sb->image_size += sizeof(struct sb_instruction_call_t) / BLOCK_SIZE;
|
||||
sec->sec_size += sizeof(struct sb_instruction_call_t) / BLOCK_SIZE;
|
||||
|
|
@ -451,7 +528,7 @@ static void compute_sb_offsets(struct sb_file_t *sb)
|
|||
else if(inst->inst == SB_INST_FILL)
|
||||
{
|
||||
if(g_debug)
|
||||
printf("FILL | addr=0x%08x | len=0x%08x | pattern=0x%08x\n",
|
||||
printf(" FILL | addr=0x%08x | len=0x%08x | pattern=0x%08x\n",
|
||||
inst->addr, inst->size, inst->pattern);
|
||||
sb->image_size += sizeof(struct sb_instruction_fill_t) / BLOCK_SIZE;
|
||||
sec->sec_size += sizeof(struct sb_instruction_fill_t) / BLOCK_SIZE;
|
||||
|
|
@ -459,7 +536,7 @@ static void compute_sb_offsets(struct sb_file_t *sb)
|
|||
else if(inst->inst == SB_INST_LOAD)
|
||||
{
|
||||
if(g_debug)
|
||||
printf("LOAD | addr=0x%08x | len=0x%08x\n", inst->addr, inst->size);
|
||||
printf(" LOAD | addr=0x%08x | len=0x%08x\n", inst->addr, inst->size);
|
||||
/* load header */
|
||||
sb->image_size += sizeof(struct sb_instruction_load_t) / BLOCK_SIZE;
|
||||
sec->sec_size += sizeof(struct sb_instruction_load_t) / BLOCK_SIZE;
|
||||
|
|
@ -470,10 +547,17 @@ static void compute_sb_offsets(struct sb_file_t *sb)
|
|||
else if(inst->inst == SB_INST_MODE)
|
||||
{
|
||||
if(g_debug)
|
||||
printf("MODE | mod=0x%08x", inst->addr);
|
||||
printf(" MODE | mod=0x%08x", inst->addr);
|
||||
sb->image_size += sizeof(struct sb_instruction_mode_t) / BLOCK_SIZE;
|
||||
sec->sec_size += sizeof(struct sb_instruction_mode_t) / BLOCK_SIZE;
|
||||
}
|
||||
else if(inst->inst == SB_INST_DATA)
|
||||
{
|
||||
if(g_debug)
|
||||
printf(" DATA | size=0x%08x\n", inst->size);
|
||||
sb->image_size += ROUND_UP(inst->size, BLOCK_SIZE) / BLOCK_SIZE;
|
||||
sec->sec_size += ROUND_UP(inst->size, BLOCK_SIZE) / BLOCK_SIZE;
|
||||
}
|
||||
else
|
||||
bug("die on inst %d\n", inst->inst);
|
||||
}
|
||||
|
|
@ -544,7 +628,8 @@ static void produce_sb_section_header(struct sb_section_t *sec,
|
|||
sec_hdr->identifier = sec->identifier;
|
||||
sec_hdr->offset = sec->file_offset;
|
||||
sec_hdr->size = sec->sec_size;
|
||||
sec_hdr->flags = SECTION_BOOTABLE;
|
||||
sec_hdr->flags = (sec->is_data ? 0 : SECTION_BOOTABLE)
|
||||
| (sec->is_cleartext ? SECTION_CLEARTEXT : 0);
|
||||
}
|
||||
|
||||
static uint8_t instruction_checksum(struct sb_instruction_header_t *hdr)
|
||||
|
|
@ -563,7 +648,8 @@ static void produce_section_tag_cmd(struct sb_section_t *sec,
|
|||
tag->hdr.flags = is_last ? SB_INST_LAST_TAG : 0;
|
||||
tag->identifier = sec->identifier;
|
||||
tag->len = sec->sec_size;
|
||||
tag->flags = SECTION_BOOTABLE;
|
||||
tag->flags = (sec->is_data ? 0 : SECTION_BOOTABLE)
|
||||
| (sec->is_cleartext ? SECTION_CLEARTEXT : 0);
|
||||
tag->hdr.checksum = instruction_checksum(&tag->hdr);
|
||||
}
|
||||
|
||||
|
|
@ -671,21 +757,24 @@ static void produce_sb_file(struct sb_file_t *sb, const char *filename)
|
|||
{
|
||||
struct sb_inst_t *inst = &sb->sections[i].insts[j];
|
||||
/* command */
|
||||
struct sb_instruction_common_t cmd;
|
||||
produce_sb_instruction(inst, &cmd);
|
||||
if(g_nr_keys > 0)
|
||||
cbc_mac((byte *)&cmd, (byte *)&cmd, sizeof(cmd) / BLOCK_SIZE,
|
||||
real_key, cur_cbc_mac, &cur_cbc_mac, 1);
|
||||
sha_1_update(&file_sha1, (byte *)&cmd, sizeof(cmd));
|
||||
write(fd, &cmd, sizeof(cmd));
|
||||
if(inst->inst != SB_INST_DATA)
|
||||
{
|
||||
struct sb_instruction_common_t cmd;
|
||||
produce_sb_instruction(inst, &cmd);
|
||||
if(g_nr_keys > 0 && !sb->sections[i].is_cleartext)
|
||||
cbc_mac((byte *)&cmd, (byte *)&cmd, sizeof(cmd) / BLOCK_SIZE,
|
||||
real_key, cur_cbc_mac, &cur_cbc_mac, 1);
|
||||
sha_1_update(&file_sha1, (byte *)&cmd, sizeof(cmd));
|
||||
write(fd, &cmd, sizeof(cmd));
|
||||
}
|
||||
/* data */
|
||||
if(inst->inst == SB_INST_LOAD)
|
||||
if(inst->inst == SB_INST_LOAD || inst->inst == SB_INST_DATA)
|
||||
{
|
||||
uint32_t sz = inst->size + inst->padding_size;
|
||||
byte *data = xmalloc(sz);
|
||||
memcpy(data, inst->data, inst->size);
|
||||
memcpy(data + inst->size, inst->padding, inst->padding_size);
|
||||
if(g_nr_keys > 0)
|
||||
if(g_nr_keys > 0 && !sb->sections[i].is_cleartext)
|
||||
cbc_mac(data, data, sz / BLOCK_SIZE,
|
||||
real_key, cur_cbc_mac, &cur_cbc_mac, 1);
|
||||
sha_1_update(&file_sha1, data, sz);
|
||||
|
|
@ -706,22 +795,94 @@ static void produce_sb_file(struct sb_file_t *sb, const char *filename)
|
|||
close(fd);
|
||||
}
|
||||
|
||||
int main(int argc, const char **argv)
|
||||
void usage(void)
|
||||
{
|
||||
if(argc != 4)
|
||||
printf("Usage: elftosb [options | file]...\n");
|
||||
printf("Options:\n");
|
||||
printf(" -?/--help:\t\tDisplay this message\n");
|
||||
printf(" -o <file>\tSet output file\n");
|
||||
printf(" -c <file>\tSet command file\n");
|
||||
printf(" -d/--debug\tEnable debug output\n");
|
||||
printf(" -k <file>\t\tAdd key file\n");
|
||||
printf(" -z\t\tAdd zero key\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static byte g_zero_key[16] = {0};
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
char *cmd_filename = NULL;
|
||||
char *output_filename = NULL;
|
||||
|
||||
while(1)
|
||||
{
|
||||
printf("Usage: %s <cmd file> <key file> <out file>\n",*argv);
|
||||
printf("To enable debug mode, set environement variable SB_DEBUG to YES\n");
|
||||
return 1;
|
||||
static struct option long_options[] =
|
||||
{
|
||||
{"help", no_argument, 0, '?'},
|
||||
{"debug", no_argument, 0, 'd'},
|
||||
{0, 0, 0, 0}
|
||||
};
|
||||
|
||||
int c = getopt_long(argc, argv, "?do:c:k:z", long_options, NULL);
|
||||
if(c == -1)
|
||||
break;
|
||||
switch(c)
|
||||
{
|
||||
case 'd':
|
||||
g_debug = true;
|
||||
break;
|
||||
case '?':
|
||||
usage();
|
||||
break;
|
||||
case 'o':
|
||||
output_filename = optarg;
|
||||
break;
|
||||
case 'c':
|
||||
cmd_filename = optarg;
|
||||
break;
|
||||
case 'k':
|
||||
{
|
||||
int kac;
|
||||
key_array_t ka = read_keys(optarg, &kac);
|
||||
add_keys(ka, kac);
|
||||
break;
|
||||
}
|
||||
case 'z':
|
||||
{
|
||||
add_keys(&g_zero_key, 1);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
if(strcasecmp(s_getenv("SB_DEBUG"), "YES") == 0)
|
||||
g_debug = true;
|
||||
if(!cmd_filename)
|
||||
bug("You must specify a command file\n");
|
||||
if(!output_filename)
|
||||
bug("You must specify an output file\n");
|
||||
|
||||
g_key_array = read_keys(argv[2], &g_nr_keys);
|
||||
struct cmd_file_t *cmd_file = db_parse_file(argv[1]);
|
||||
g_extern = &argv[optind];
|
||||
g_extern_count = argc - optind;
|
||||
|
||||
if(g_debug)
|
||||
{
|
||||
printf("key: %d\n", g_nr_keys);
|
||||
for(int i = 0; i < g_nr_keys; i++)
|
||||
{
|
||||
for(int j = 0; j < 16; j++)
|
||||
printf(" %02x", g_key_array[i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
for(int i = 0; i < g_extern_count; i++)
|
||||
printf("extern(%d)=%s\n", i, g_extern[i]);
|
||||
}
|
||||
|
||||
struct cmd_file_t *cmd_file = db_parse_file(cmd_filename);
|
||||
struct sb_file_t *sb_file = apply_cmd_file(cmd_file);
|
||||
produce_sb_file(sb_file, argv[3]);
|
||||
produce_sb_file(sb_file, output_filename);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue