2011-04-17 01:43:52 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Amaury Pouly
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2013-06-15 20:19:52 +00:00
|
|
|
|
2011-04-17 01:43:52 +00:00
|
|
|
#define _ISOC99_SOURCE
|
2011-11-06 01:49:13 +00:00
|
|
|
#define _POSIX_C_SOURCE 200809L /* for strdup */
|
2011-04-17 01:43:52 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <time.h>
|
2011-04-17 15:49:58 +00:00
|
|
|
#include <stdarg.h>
|
2011-06-09 09:39:21 +00:00
|
|
|
#include <strings.h>
|
2011-09-16 22:27:16 +00:00
|
|
|
#include <getopt.h>
|
2011-04-17 01:43:52 +00:00
|
|
|
|
|
|
|
#include "crypto.h"
|
|
|
|
#include "elf.h"
|
|
|
|
#include "sb.h"
|
2011-09-15 16:10:31 +00:00
|
|
|
#include "dbparser.h"
|
2011-10-29 14:22:17 +00:00
|
|
|
#include "misc.h"
|
2011-04-17 01:43:52 +00:00
|
|
|
|
2011-09-16 22:27:16 +00:00
|
|
|
char **g_extern;
|
|
|
|
int g_extern_count;
|
2011-04-17 15:49:58 +00:00
|
|
|
|
2011-04-17 22:30:09 +00:00
|
|
|
#define ROUND_UP(val, round) ((((val) + (round) - 1) / (round)) * (round))
|
|
|
|
|
2011-10-29 14:22:17 +00:00
|
|
|
#define crypto_cbc(...) \
|
|
|
|
do { int ret = crypto_cbc(__VA_ARGS__); \
|
|
|
|
if(ret != CRYPTO_ERROR_SUCCESS) \
|
|
|
|
bug("crypto_cbc error: %d\n", ret); \
|
|
|
|
}while(0)
|
2011-04-17 01:43:52 +00:00
|
|
|
|
2011-04-17 15:49:58 +00:00
|
|
|
/**
|
|
|
|
* command file to sb conversion
|
|
|
|
*/
|
|
|
|
|
2011-09-16 22:27:16 +00:00
|
|
|
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);
|
2011-11-06 01:49:13 +00:00
|
|
|
/* first free the old src->filename content */
|
|
|
|
free(src->filename);
|
|
|
|
src->filename = strdup(g_extern[src->extern_nr]);
|
2011-09-16 22:27:16 +00:00
|
|
|
}
|
|
|
|
|
2011-04-17 15:49:58 +00:00
|
|
|
static void load_elf_by_id(struct cmd_file_t *cmd_file, const char *id)
|
|
|
|
{
|
2011-09-15 16:10:31 +00:00
|
|
|
struct cmd_source_t *src = db_find_source_by_id(cmd_file, id);
|
2011-04-17 15:49:58 +00:00
|
|
|
if(src == NULL)
|
|
|
|
bug("undefined reference to source '%s'\n", id);
|
|
|
|
/* avoid reloading */
|
2011-07-04 21:55:56 +00:00
|
|
|
if(src->type == CMD_SRC_ELF && src->loaded)
|
2011-04-17 15:49:58 +00:00
|
|
|
return;
|
2011-07-02 02:12:01 +00:00
|
|
|
if(src->type != CMD_SRC_UNK)
|
2011-07-22 15:45:08 +00:00
|
|
|
bug("source '%s' seen both as elf and binary file\n", id);
|
2011-09-16 22:27:16 +00:00
|
|
|
/* resolve potential extern file */
|
|
|
|
resolve_extern(src);
|
|
|
|
/* load it */
|
2011-07-02 02:12:01 +00:00
|
|
|
src->type = CMD_SRC_ELF;
|
2011-10-29 14:22:17 +00:00
|
|
|
FILE *fd = fopen(src->filename, "rb");
|
|
|
|
if(fd == NULL)
|
2011-04-17 15:49:58 +00:00
|
|
|
bug("cannot open '%s' (id '%s')\n", src->filename, id);
|
2011-05-13 12:13:02 +00:00
|
|
|
if(g_debug)
|
|
|
|
printf("Loading ELF file '%s'...\n", src->filename);
|
2011-04-17 15:49:58 +00:00
|
|
|
elf_init(&src->elf);
|
2013-08-21 20:20:53 +00:00
|
|
|
src->loaded = elf_read_file(&src->elf, elf_std_read, generic_std_printf, fd);
|
2011-10-29 14:22:17 +00:00
|
|
|
fclose(fd);
|
2011-07-04 21:55:56 +00:00
|
|
|
if(!src->loaded)
|
2011-04-17 15:49:58 +00:00
|
|
|
bug("error loading elf file '%s' (id '%s')\n", src->filename, id);
|
|
|
|
}
|
|
|
|
|
2011-07-02 02:12:01 +00:00
|
|
|
static void load_bin_by_id(struct cmd_file_t *cmd_file, const char *id)
|
|
|
|
{
|
2011-09-15 16:10:31 +00:00
|
|
|
struct cmd_source_t *src = db_find_source_by_id(cmd_file, id);
|
2011-07-02 02:12:01 +00:00
|
|
|
if(src == NULL)
|
|
|
|
bug("undefined reference to source '%s'\n", id);
|
|
|
|
/* avoid reloading */
|
2011-07-04 21:55:56 +00:00
|
|
|
if(src->type == CMD_SRC_BIN && src->loaded)
|
2011-07-02 02:12:01 +00:00
|
|
|
return;
|
|
|
|
if(src->type != CMD_SRC_UNK)
|
2011-07-22 15:45:08 +00:00
|
|
|
bug("source '%s' seen both as elf and binary file\n", id);
|
2011-09-16 22:27:16 +00:00
|
|
|
/* resolve potential extern file */
|
|
|
|
resolve_extern(src);
|
|
|
|
/* load it */
|
2011-07-02 02:12:01 +00:00
|
|
|
src->type = CMD_SRC_BIN;
|
2011-10-29 14:22:17 +00:00
|
|
|
FILE *fd = fopen(src->filename, "rb");
|
|
|
|
if(fd == NULL)
|
2011-07-02 02:12:01 +00:00
|
|
|
bug("cannot open '%s' (id '%s')\n", src->filename, id);
|
|
|
|
if(g_debug)
|
|
|
|
printf("Loading BIN file '%s'...\n", src->filename);
|
2011-10-29 14:22:17 +00:00
|
|
|
fseek(fd, 0, SEEK_END);
|
|
|
|
src->bin.size = ftell(fd);
|
|
|
|
fseek(fd, 0, SEEK_SET);
|
2011-07-02 02:12:01 +00:00
|
|
|
src->bin.data = xmalloc(src->bin.size);
|
2011-10-29 14:22:17 +00:00
|
|
|
fread(src->bin.data, 1, src->bin.size, fd);
|
|
|
|
fclose(fd);
|
2011-07-04 21:55:56 +00:00
|
|
|
src->loaded = true;
|
2011-07-02 02:12:01 +00:00
|
|
|
}
|
|
|
|
|
2013-08-22 12:39:46 +00:00
|
|
|
static const char *get_str_opt(struct cmd_option_t *opt_list, const char *id, const char *dflt)
|
|
|
|
{
|
|
|
|
struct cmd_option_t *opt = db_find_option_by_id(opt_list, id);
|
|
|
|
if(!opt)
|
|
|
|
return dflt;
|
|
|
|
if(!opt->is_string)
|
|
|
|
bug("'%s' option must be a string\n", id);
|
|
|
|
return opt->str;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t get_int_opt(struct cmd_option_t *opt_list, const char *id, uint32_t dflt)
|
|
|
|
{
|
|
|
|
struct cmd_option_t *opt = db_find_option_by_id(opt_list, id);
|
|
|
|
if(!opt)
|
|
|
|
return dflt;
|
|
|
|
if(opt->is_string)
|
|
|
|
bug("'%s' option must be an integer\n", id);
|
|
|
|
return opt->val;
|
|
|
|
}
|
|
|
|
|
2011-04-17 15:49:58 +00:00
|
|
|
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));
|
2013-08-22 12:39:46 +00:00
|
|
|
sb_build_default_image(sb);
|
|
|
|
|
|
|
|
if(db_find_option_by_id(cmd_file->opt_list, "componentVersion") &&
|
|
|
|
!db_parse_sb_version(&sb->component_ver, get_str_opt(cmd_file->opt_list, "componentVersion", "")))
|
|
|
|
bug("Invalid 'componentVersion' format\n");
|
|
|
|
if(db_find_option_by_id(cmd_file->opt_list, "productVersion") &&
|
|
|
|
!db_parse_sb_version(&sb->product_ver, get_str_opt(cmd_file->opt_list, "productVersion", "")))
|
|
|
|
bug("Invalid 'productVersion' format\n");
|
|
|
|
if(db_find_option_by_id(cmd_file->opt_list, "sbMinorVersion"))
|
|
|
|
sb->minor_version = get_int_opt(cmd_file->opt_list, "sbMinorVersion", 0);
|
|
|
|
if(db_find_option_by_id(cmd_file->opt_list, "flags"))
|
|
|
|
sb->flags = get_int_opt(cmd_file->opt_list, "flags", 0);
|
|
|
|
if(db_find_option_by_id(cmd_file->opt_list, "driveTag"))
|
|
|
|
sb->drive_tag = get_int_opt(cmd_file->opt_list, "driveTag", 0);
|
|
|
|
if(db_find_option_by_id(cmd_file->opt_list, "timestampLow"))
|
|
|
|
{
|
|
|
|
if(!db_find_option_by_id(cmd_file->opt_list, "timestampHigh"))
|
|
|
|
bug("Option 'timestampLow' and 'timestampHigh' must both specified\n");
|
|
|
|
sb->timestamp = (uint64_t)get_int_opt(cmd_file->opt_list, "timestampHigh", 0) << 32 |
|
|
|
|
get_int_opt(cmd_file->opt_list, "timestampLow", 0);
|
|
|
|
}
|
2013-06-15 20:19:52 +00:00
|
|
|
|
2011-05-13 12:13:02 +00:00
|
|
|
if(g_debug)
|
|
|
|
printf("Applying command file...\n");
|
2011-04-17 15:49:58 +00:00
|
|
|
/* count sections */
|
|
|
|
struct cmd_section_t *csec = cmd_file->section_list;
|
|
|
|
while(csec)
|
|
|
|
{
|
|
|
|
sb->nr_sections++;
|
|
|
|
csec = csec->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
sb->sections = xmalloc(sb->nr_sections * sizeof(struct sb_section_t));
|
|
|
|
memset(sb->sections, 0, sb->nr_sections * sizeof(struct sb_section_t));
|
|
|
|
/* flatten sections */
|
|
|
|
csec = cmd_file->section_list;
|
|
|
|
for(int i = 0; i < sb->nr_sections; i++, csec = csec->next)
|
|
|
|
{
|
|
|
|
struct sb_section_t *sec = &sb->sections[i];
|
|
|
|
sec->identifier = csec->identifier;
|
2011-09-16 22:27:16 +00:00
|
|
|
|
|
|
|
/* options */
|
|
|
|
do
|
2011-04-17 15:49:58 +00:00
|
|
|
{
|
2011-10-23 17:43:52 +00:00
|
|
|
/* cleartext */
|
2013-08-22 12:39:46 +00:00
|
|
|
sec->is_cleartext = get_int_opt(csec->opt_list, "cleartext", false);
|
2011-10-23 17:43:52 +00:00
|
|
|
/* alignment */
|
2013-08-22 12:39:46 +00:00
|
|
|
sec->alignment = get_int_opt(csec->opt_list, "alignment", BLOCK_SIZE);
|
|
|
|
// alignement cannot be lower than block size
|
|
|
|
if((sec->alignment & (sec->alignment - 1)) != 0)
|
|
|
|
bug("Alignment section attribute must be a power of two\n");
|
|
|
|
if(sec->alignment < BLOCK_SIZE)
|
2011-10-23 17:43:52 +00:00
|
|
|
sec->alignment = BLOCK_SIZE;
|
2013-08-22 12:39:46 +00:00
|
|
|
/* other flags */
|
|
|
|
sec->other_flags = get_int_opt(csec->opt_list, "sectionFlags", 0) & ~SECTION_STD_MASK;
|
2011-09-16 22:27:16 +00:00
|
|
|
}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;
|
2011-11-06 01:49:13 +00:00
|
|
|
sec->insts[0].data = memdup(bin->data, bin->size);
|
2011-09-16 22:27:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sec->is_data = false;
|
|
|
|
/* count instructions and loads things */
|
|
|
|
struct cmd_inst_t *cinst = csec->inst_list;
|
|
|
|
while(cinst)
|
2011-07-22 15:45:08 +00:00
|
|
|
{
|
2011-09-16 22:27:16 +00:00
|
|
|
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");
|
2013-06-15 20:19:52 +00:00
|
|
|
|
2011-09-16 22:27:16 +00:00
|
|
|
cinst = cinst->next;
|
2011-07-22 15:45:08 +00:00
|
|
|
}
|
2011-04-17 15:49:58 +00:00
|
|
|
|
2011-09-16 22:27:16 +00:00
|
|
|
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)
|
2011-04-17 15:49:58 +00:00
|
|
|
{
|
2011-09-16 22:27:16 +00:00
|
|
|
if(cinst->type == CMD_LOAD)
|
2011-04-17 15:49:58 +00:00
|
|
|
{
|
2011-09-16 22:27:16 +00:00
|
|
|
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)
|
2011-04-17 15:49:58 +00:00
|
|
|
{
|
2011-09-16 22:27:16 +00:00
|
|
|
if(esec->type == EST_LOAD)
|
|
|
|
{
|
|
|
|
sec->insts[idx].inst = SB_INST_LOAD;
|
|
|
|
sec->insts[idx].addr = esec->addr;
|
|
|
|
sec->insts[idx].size = esec->size;
|
2011-11-06 01:49:13 +00:00
|
|
|
sec->insts[idx++].data = memdup(esec->section, esec->size);
|
2011-09-16 22:27:16 +00:00
|
|
|
}
|
|
|
|
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;
|
2011-04-17 15:49:58 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-16 22:27:16 +00:00
|
|
|
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;
|
2011-11-06 01:49:13 +00:00
|
|
|
sec->insts[idx].data = memdup(bin->data, bin->size);
|
2011-09-16 22:27:16 +00:00
|
|
|
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");
|
2013-06-15 20:19:52 +00:00
|
|
|
|
2011-09-16 22:27:16 +00:00
|
|
|
cinst = cinst->next;
|
2011-04-17 15:49:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sb;
|
|
|
|
}
|
|
|
|
|
2011-11-06 13:30:13 +00:00
|
|
|
static void usage(void)
|
2011-09-16 22:27:16 +00:00
|
|
|
{
|
|
|
|
printf("Usage: elftosb [options | file]...\n");
|
|
|
|
printf("Options:\n");
|
2017-01-01 18:46:01 +00:00
|
|
|
printf(" -h/--help\tDisplay this message\n");
|
2011-09-16 22:27:16 +00:00
|
|
|
printf(" -o <file>\tSet output file\n");
|
|
|
|
printf(" -c <file>\tSet command file\n");
|
|
|
|
printf(" -d/--debug\tEnable debug output\n");
|
2011-10-23 17:43:52 +00:00
|
|
|
printf(" -k <file>\tAdd key file\n");
|
2011-09-16 22:27:16 +00:00
|
|
|
printf(" -z\t\tAdd zero key\n");
|
2017-01-01 18:46:01 +00:00
|
|
|
printf(" --add-key <key>\tAdd single key\n");
|
2011-10-29 17:04:20 +00:00
|
|
|
printf(" --real-key <key>\tOverride real key\n");
|
|
|
|
printf(" --crypto-iv <iv>\tOverride crypto IV\n");
|
2011-09-16 22:27:16 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
2011-04-17 01:43:52 +00:00
|
|
|
{
|
2011-09-16 22:27:16 +00:00
|
|
|
char *cmd_filename = NULL;
|
|
|
|
char *output_filename = NULL;
|
2011-10-29 17:04:20 +00:00
|
|
|
struct crypto_key_t real_key;
|
|
|
|
struct crypto_key_t crypto_iv;
|
2017-01-01 18:46:01 +00:00
|
|
|
memset(&real_key, 0, sizeof(real_key));
|
|
|
|
memset(&crypto_iv, 0, sizeof(crypto_iv));
|
2011-10-29 17:04:20 +00:00
|
|
|
real_key.method = CRYPTO_NONE;
|
|
|
|
crypto_iv.method = CRYPTO_NONE;
|
2013-06-15 20:19:52 +00:00
|
|
|
|
2017-01-01 18:46:01 +00:00
|
|
|
if(argc == 1)
|
|
|
|
usage();
|
|
|
|
|
2011-09-16 22:27:16 +00:00
|
|
|
while(1)
|
2011-04-17 18:37:30 +00:00
|
|
|
{
|
2011-09-16 22:27:16 +00:00
|
|
|
static struct option long_options[] =
|
|
|
|
{
|
2017-01-01 18:46:01 +00:00
|
|
|
{"help", no_argument, 0, 'h'},
|
2011-09-16 22:27:16 +00:00
|
|
|
{"debug", no_argument, 0, 'd'},
|
2011-10-29 17:04:20 +00:00
|
|
|
{"add-key", required_argument, 0, 'a'},
|
|
|
|
{"real-key", required_argument, 0, 'r'},
|
|
|
|
{"crypto-iv", required_argument, 0, 'i'},
|
2011-09-16 22:27:16 +00:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2017-01-01 18:46:01 +00:00
|
|
|
int c = getopt_long(argc, argv, "hdo:c:k:za:", long_options, NULL);
|
2011-09-16 22:27:16 +00:00
|
|
|
if(c == -1)
|
|
|
|
break;
|
|
|
|
switch(c)
|
|
|
|
{
|
|
|
|
case 'd':
|
|
|
|
g_debug = true;
|
|
|
|
break;
|
2017-01-01 18:46:01 +00:00
|
|
|
case 'h':
|
2011-09-16 22:27:16 +00:00
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
output_filename = optarg;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
cmd_filename = optarg;
|
|
|
|
break;
|
|
|
|
case 'k':
|
|
|
|
{
|
2012-02-19 17:36:57 +00:00
|
|
|
if(!add_keys_from_file(optarg))
|
|
|
|
bug("Cannot keys from %s\n", optarg);
|
2011-09-16 22:27:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'z':
|
|
|
|
{
|
2013-07-17 22:27:12 +00:00
|
|
|
struct crypto_key_t g_zero_key;
|
|
|
|
sb_get_zero_key(&g_zero_key);
|
2011-09-16 22:27:16 +00:00
|
|
|
add_keys(&g_zero_key, 1);
|
|
|
|
break;
|
|
|
|
}
|
2011-10-29 17:04:20 +00:00
|
|
|
case 'a':
|
|
|
|
case 'r':
|
|
|
|
case 'i':
|
2011-10-29 14:22:17 +00:00
|
|
|
{
|
|
|
|
struct crypto_key_t key;
|
2011-10-29 17:04:20 +00:00
|
|
|
char *s = optarg;
|
|
|
|
if(!parse_key(&s, &key))
|
|
|
|
bug("Invalid key/iv specified as argument");
|
|
|
|
if(*s != 0)
|
|
|
|
bug("Trailing characters after key/iv specified as argument");
|
|
|
|
if(c == 'r')
|
|
|
|
memcpy(&real_key, &key, sizeof(key));
|
|
|
|
else if(c == 'i')
|
|
|
|
memcpy(&crypto_iv, &key, sizeof(key));
|
|
|
|
else
|
|
|
|
add_keys(&key, 1);
|
2011-10-29 14:22:17 +00:00
|
|
|
break;
|
|
|
|
}
|
2011-09-16 22:27:16 +00:00
|
|
|
default:
|
2013-07-17 22:27:12 +00:00
|
|
|
bug("Internal error: unknown option '%c'\n", c);
|
2011-09-16 22:27:16 +00:00
|
|
|
}
|
2011-04-17 18:37:30 +00:00
|
|
|
}
|
|
|
|
|
2011-09-16 22:27:16 +00:00
|
|
|
if(!cmd_filename)
|
|
|
|
bug("You must specify a command file\n");
|
|
|
|
if(!output_filename)
|
|
|
|
bug("You must specify an output file\n");
|
|
|
|
|
|
|
|
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++)
|
|
|
|
{
|
2011-10-29 14:22:17 +00:00
|
|
|
printf(" ");
|
2013-08-10 19:37:27 +00:00
|
|
|
print_key(NULL, misc_std_printf, &g_key_array[i], true);
|
2011-09-16 22:27:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for(int i = 0; i < g_extern_count; i++)
|
|
|
|
printf("extern(%d)=%s\n", i, g_extern[i]);
|
|
|
|
}
|
2011-04-17 01:43:52 +00:00
|
|
|
|
2011-09-16 22:27:16 +00:00
|
|
|
struct cmd_file_t *cmd_file = db_parse_file(cmd_filename);
|
2013-12-06 10:37:49 +00:00
|
|
|
if(cmd_file == NULL)
|
|
|
|
bug("Error parsing command file\n");
|
2011-04-17 15:49:58 +00:00
|
|
|
struct sb_file_t *sb_file = apply_cmd_file(cmd_file);
|
2011-11-06 01:49:13 +00:00
|
|
|
db_free(cmd_file);
|
2011-10-29 17:04:20 +00:00
|
|
|
|
|
|
|
if(real_key.method == CRYPTO_KEY)
|
2011-11-06 01:49:13 +00:00
|
|
|
{
|
|
|
|
sb_file->override_real_key = true;
|
|
|
|
memcpy(sb_file->real_key, real_key.u.key, 16);
|
|
|
|
}
|
2011-10-29 17:04:20 +00:00
|
|
|
if(crypto_iv.method == CRYPTO_KEY)
|
2011-11-06 01:49:13 +00:00
|
|
|
{
|
|
|
|
sb_file->override_crypto_iv = true;
|
|
|
|
memcpy(sb_file->crypto_iv, crypto_iv.u.key, 16);
|
|
|
|
}
|
2011-11-01 11:23:43 +00:00
|
|
|
|
2013-08-21 20:20:53 +00:00
|
|
|
sb_write_file(sb_file, output_filename, 0, generic_std_printf);
|
2011-11-06 01:49:13 +00:00
|
|
|
sb_free(sb_file);
|
|
|
|
clear_keys();
|
2013-06-15 20:19:52 +00:00
|
|
|
|
2011-04-17 01:43:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|