2010-11-28 10:53:01 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Bertrik Sikken
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* .sb file parser and chunk extractor
|
|
|
|
*
|
|
|
|
* Based on amsinfo, which is
|
|
|
|
* Copyright © 2008 Rafaël Carré <rafael.carre@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _ISOC99_SOURCE /* snprintf() */
|
2013-08-21 20:20:53 +00:00
|
|
|
#define _POSIX_C_SOURCE 200809L /* for strdup */
|
2010-11-28 10:53:01 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <time.h>
|
2011-10-29 14:22:17 +00:00
|
|
|
#include <stdarg.h>
|
2011-06-09 09:39:21 +00:00
|
|
|
#include <strings.h>
|
2011-10-29 14:22:17 +00:00
|
|
|
#include <getopt.h>
|
2010-12-01 16:17:11 +00:00
|
|
|
|
2010-11-29 14:15:06 +00:00
|
|
|
#include "crypto.h"
|
2010-12-01 16:17:11 +00:00
|
|
|
#include "elf.h"
|
2011-04-16 18:22:37 +00:00
|
|
|
#include "sb.h"
|
2012-11-26 22:54:44 +00:00
|
|
|
#include "sb1.h"
|
2011-10-29 14:22:17 +00:00
|
|
|
#include "misc.h"
|
2013-08-21 22:58:19 +00:00
|
|
|
#include "dbparser.h"
|
2010-11-28 10:53:01 +00:00
|
|
|
|
|
|
|
/* all blocks are sized as a multiple of 0x1ff */
|
|
|
|
#define PAD_TO_BOUNDARY(x) (((x) + 0x1ff) & ~0x1ff)
|
|
|
|
|
|
|
|
/* globals */
|
|
|
|
|
2012-11-27 23:17:15 +00:00
|
|
|
static char *g_out_prefix;
|
|
|
|
static bool g_elf_simplify = true;
|
2010-11-28 20:56:09 +00:00
|
|
|
|
2013-08-21 22:58:19 +00:00
|
|
|
static void extract_elf_section(struct elf_params_t *elf, int count, uint32_t id,
|
|
|
|
struct cmd_file_t *cmd_file, struct cmd_section_t *section, bool is_call, uint32_t arg)
|
2010-12-01 16:17:11 +00:00
|
|
|
{
|
2011-11-01 11:26:16 +00:00
|
|
|
char name[5];
|
2013-08-21 22:58:19 +00:00
|
|
|
char fileid[16];
|
2011-11-01 11:26:16 +00:00
|
|
|
char *filename = xmalloc(strlen(g_out_prefix) + 32);
|
|
|
|
sb_fill_section_name(name, id);
|
2013-08-21 22:58:19 +00:00
|
|
|
sb_fill_section_name(fileid, id);
|
|
|
|
sprintf(fileid + strlen(fileid), "%d", count);
|
2011-11-01 11:26:16 +00:00
|
|
|
sprintf(filename, "%s%s.%d.elf", g_out_prefix, name, count);
|
2013-08-21 22:58:19 +00:00
|
|
|
db_add_source(cmd_file, fileid, filename + strlen(g_out_prefix));
|
|
|
|
db_add_inst_id(section, CMD_LOAD, fileid, 0);
|
|
|
|
if(elf_get_start_addr(elf, NULL))
|
|
|
|
db_add_inst_id(section, is_call ? CMD_CALL : CMD_JUMP, fileid, arg);
|
|
|
|
|
2011-11-01 11:26:16 +00:00
|
|
|
if(g_debug)
|
|
|
|
printf("Write boot section %s to %s\n", name, filename);
|
2013-06-15 20:19:52 +00:00
|
|
|
|
2010-12-01 16:17:11 +00:00
|
|
|
FILE *fd = fopen(filename, "wb");
|
|
|
|
free(filename);
|
2013-06-15 20:19:52 +00:00
|
|
|
|
2010-12-01 16:17:11 +00:00
|
|
|
if(fd == NULL)
|
2012-11-27 23:17:15 +00:00
|
|
|
return;
|
|
|
|
if(g_elf_simplify)
|
|
|
|
elf_simplify(elf);
|
2013-08-21 21:02:49 +00:00
|
|
|
elf_write_file(elf, elf_std_write, generic_std_printf, fd);
|
2010-12-01 16:17:11 +00:00
|
|
|
fclose(fd);
|
|
|
|
}
|
|
|
|
|
2013-08-21 22:58:19 +00:00
|
|
|
static void extract_sb_section(struct sb_section_t *sec, struct cmd_file_t *cmd_file)
|
2011-11-01 11:26:16 +00:00
|
|
|
{
|
2013-08-21 22:58:19 +00:00
|
|
|
struct cmd_section_t *db_sec = db_add_section(cmd_file, sec->identifier, sec->is_data);
|
|
|
|
db_add_int_opt(&db_sec->opt_list, "alignment", sec->alignment);
|
|
|
|
db_add_int_opt(&db_sec->opt_list, "cleartext", sec->is_cleartext);
|
2013-08-22 12:39:46 +00:00
|
|
|
db_add_int_opt(&db_sec->opt_list, "sectionFlags", sec->other_flags);
|
2013-08-21 22:58:19 +00:00
|
|
|
|
2011-11-01 11:26:16 +00:00
|
|
|
if(sec->is_data)
|
|
|
|
{
|
|
|
|
char sec_name[5];
|
|
|
|
char *filename = xmalloc(strlen(g_out_prefix) + 32);
|
|
|
|
sb_fill_section_name(sec_name, sec->identifier);
|
|
|
|
sprintf(filename, "%s%s.bin", g_out_prefix, sec_name);
|
2013-08-21 22:58:19 +00:00
|
|
|
db_add_source(cmd_file, sec_name, filename + strlen(g_out_prefix));
|
|
|
|
db_sec->source_id = strdup(sec_name);
|
|
|
|
|
2011-11-01 11:26:16 +00:00
|
|
|
FILE *fd = fopen(filename, "wb");
|
|
|
|
if(fd == NULL)
|
|
|
|
bugp("Cannot open %s for writing\n", filename);
|
|
|
|
if(g_debug)
|
|
|
|
printf("Write data section %s to %s\n", sec_name, filename);
|
|
|
|
free(filename);
|
2013-06-15 20:19:52 +00:00
|
|
|
|
2011-11-01 11:26:16 +00:00
|
|
|
for(int j = 0; j < sec->nr_insts; j++)
|
|
|
|
{
|
2017-01-01 18:46:01 +00:00
|
|
|
if(sec->insts[j].inst != SB_INST_DATA)
|
|
|
|
bug("Internal errror: should be a data section\n");
|
2011-11-01 11:26:16 +00:00
|
|
|
fwrite(sec->insts[j].data, sec->insts[j].size, 1, fd);
|
|
|
|
}
|
|
|
|
fclose(fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int elf_count = 0;
|
|
|
|
struct elf_params_t elf;
|
|
|
|
elf_init(&elf);
|
|
|
|
|
2013-07-30 15:12:56 +00:00
|
|
|
int bss_idx = 0, text_idx = 0;
|
|
|
|
char secname[32];
|
2011-11-01 11:26:16 +00:00
|
|
|
for(int i = 0; i < sec->nr_insts; i++)
|
|
|
|
{
|
|
|
|
struct sb_inst_t *inst = &sec->insts[i];
|
|
|
|
switch(inst->inst)
|
|
|
|
{
|
|
|
|
case SB_INST_LOAD:
|
2013-08-22 13:05:13 +00:00
|
|
|
sprintf(secname, ".text%d", text_idx++);
|
2013-07-30 15:12:56 +00:00
|
|
|
elf_add_load_section(&elf, inst->addr, inst->size, inst->data, secname);
|
2011-11-01 11:26:16 +00:00
|
|
|
break;
|
|
|
|
case SB_INST_FILL:
|
2013-07-30 15:12:56 +00:00
|
|
|
sprintf(secname, ".bss%d", bss_idx++);
|
|
|
|
elf_add_fill_section(&elf, inst->addr, inst->size, inst->pattern, secname);
|
2011-11-01 11:26:16 +00:00
|
|
|
break;
|
|
|
|
case SB_INST_CALL:
|
|
|
|
case SB_INST_JUMP:
|
|
|
|
elf_set_start_addr(&elf, inst->addr);
|
2013-08-21 22:58:19 +00:00
|
|
|
extract_elf_section(&elf, elf_count++, sec->identifier, cmd_file, db_sec,
|
|
|
|
inst->inst == SB_INST_CALL, inst->argument);
|
2011-11-01 11:26:16 +00:00
|
|
|
elf_release(&elf);
|
|
|
|
elf_init(&elf);
|
2013-07-30 15:12:56 +00:00
|
|
|
bss_idx = text_idx = 0;
|
2011-11-01 11:26:16 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* ignore mode and nop */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!elf_is_empty(&elf))
|
2013-08-21 22:58:19 +00:00
|
|
|
extract_elf_section(&elf, elf_count, sec->identifier, cmd_file, db_sec, false, 0);
|
2011-11-01 11:26:16 +00:00
|
|
|
elf_release(&elf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void extract_sb_file(struct sb_file_t *file)
|
|
|
|
{
|
2013-08-21 22:58:19 +00:00
|
|
|
char buffer[64];
|
|
|
|
struct cmd_file_t *cmd_file = xmalloc(sizeof(struct cmd_file_t));
|
|
|
|
memset(cmd_file, 0, sizeof(struct cmd_file_t));
|
|
|
|
db_generate_sb_version(&file->product_ver, buffer, sizeof(buffer));
|
|
|
|
db_add_str_opt(&cmd_file->opt_list, "productVersion", buffer);
|
|
|
|
db_generate_sb_version(&file->component_ver, buffer, sizeof(buffer));
|
|
|
|
db_add_str_opt(&cmd_file->opt_list, "componentVersion", buffer);
|
|
|
|
db_add_int_opt(&cmd_file->opt_list, "driveTag", file->drive_tag);
|
|
|
|
db_add_int_opt(&cmd_file->opt_list, "flags", file->flags);
|
|
|
|
db_add_int_opt(&cmd_file->opt_list, "timestampLow", file->timestamp & 0xffffffff);
|
|
|
|
db_add_int_opt(&cmd_file->opt_list, "timestampHigh", file->timestamp >> 32);
|
|
|
|
db_add_int_opt(&cmd_file->opt_list, "sbMinorVersion", file->minor_version);
|
|
|
|
|
2011-11-01 11:26:16 +00:00
|
|
|
for(int i = 0; i < file->nr_sections; i++)
|
2013-08-21 22:58:19 +00:00
|
|
|
extract_sb_section(&file->sections[i], cmd_file);
|
|
|
|
|
|
|
|
char *filename = xmalloc(strlen(g_out_prefix) + 32);
|
|
|
|
sprintf(filename, "%smake.db", g_out_prefix);
|
|
|
|
if(g_debug)
|
|
|
|
printf("Write command file to %s\n", filename);
|
|
|
|
db_generate_file(cmd_file, filename, NULL, generic_std_printf);
|
|
|
|
db_free(cmd_file);
|
2017-01-01 18:46:01 +00:00
|
|
|
free(filename);
|
2011-11-01 11:26:16 +00:00
|
|
|
}
|
|
|
|
|
2012-11-27 21:38:48 +00:00
|
|
|
static void extract_elf(struct elf_params_t *elf, int count)
|
|
|
|
{
|
|
|
|
char *filename = xmalloc(strlen(g_out_prefix) + 32);
|
2013-08-21 22:58:19 +00:00
|
|
|
sprintf(filename, "%s%d.elf", g_out_prefix, count);
|
2012-11-27 21:38:48 +00:00
|
|
|
if(g_debug)
|
|
|
|
printf("Write boot content to %s\n", filename);
|
|
|
|
|
|
|
|
FILE *fd = fopen(filename, "wb");
|
|
|
|
free(filename);
|
|
|
|
|
|
|
|
if(fd == NULL)
|
2012-11-27 23:17:15 +00:00
|
|
|
return;
|
|
|
|
if(g_elf_simplify)
|
|
|
|
elf_simplify(elf);
|
2013-08-21 20:20:53 +00:00
|
|
|
elf_write_file(elf, elf_std_write, generic_std_printf, fd);
|
2012-11-27 21:38:48 +00:00
|
|
|
fclose(fd);
|
|
|
|
}
|
|
|
|
|
2012-11-26 22:54:44 +00:00
|
|
|
static void extract_sb1_file(struct sb1_file_t *file)
|
|
|
|
{
|
2012-11-27 21:38:48 +00:00
|
|
|
int elf_count = 0;
|
|
|
|
struct elf_params_t elf;
|
|
|
|
elf_init(&elf);
|
|
|
|
|
2013-07-30 15:12:56 +00:00
|
|
|
int bss_idx = 0, text_idx = 0;
|
|
|
|
char secname[32];
|
2012-11-27 21:38:48 +00:00
|
|
|
for(int i = 0; i < file->nr_insts; i++)
|
|
|
|
{
|
|
|
|
struct sb1_inst_t *inst = &file->insts[i];
|
|
|
|
switch(inst->cmd)
|
|
|
|
{
|
|
|
|
case SB1_INST_LOAD:
|
2013-07-30 15:12:56 +00:00
|
|
|
sprintf(secname, ".text%d", text_idx++);
|
|
|
|
elf_add_load_section(&elf, inst->addr, inst->size, inst->data, secname);
|
2012-11-27 21:38:48 +00:00
|
|
|
break;
|
|
|
|
case SB1_INST_FILL:
|
2013-07-30 15:12:56 +00:00
|
|
|
sprintf(secname, ".bss%d", bss_idx++);
|
|
|
|
elf_add_fill_section(&elf, inst->addr, inst->size, inst->pattern, secname);
|
2012-11-27 21:38:48 +00:00
|
|
|
break;
|
|
|
|
case SB1_INST_CALL:
|
|
|
|
case SB1_INST_JUMP:
|
|
|
|
elf_set_start_addr(&elf, inst->addr);
|
|
|
|
extract_elf(&elf, elf_count++);
|
|
|
|
elf_release(&elf);
|
|
|
|
elf_init(&elf);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* ignore mode and nop */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!elf_is_empty(&elf))
|
|
|
|
extract_elf(&elf, elf_count);
|
|
|
|
elf_release(&elf);
|
2012-11-26 22:54:44 +00:00
|
|
|
}
|
|
|
|
|
2011-11-01 11:23:43 +00:00
|
|
|
static void usage(void)
|
2010-11-28 10:53:01 +00:00
|
|
|
{
|
2011-10-29 14:22:17 +00:00
|
|
|
printf("Usage: sbtoelf [options] sb-file\n");
|
|
|
|
printf("Options:\n");
|
2017-01-01 18:46:01 +00:00
|
|
|
printf(" -h/--help Display this message\n");
|
2013-06-17 11:58:59 +00:00
|
|
|
printf(" -o <prefix> Enable output and set prefix\n");
|
|
|
|
printf(" -d/--debug Enable debug output*\n");
|
|
|
|
printf(" -k <file> Add key file\n");
|
|
|
|
printf(" -z Add zero key\n");
|
|
|
|
printf(" -r Use raw command mode\n");
|
2017-01-01 18:46:01 +00:00
|
|
|
printf(" -a/--add-key <key> Add single key\n");
|
2013-06-17 11:58:59 +00:00
|
|
|
printf(" -n/--no-color Disable output colors\n");
|
|
|
|
printf(" -l/--loopback <file> Produce sb file out of extracted description*\n");
|
|
|
|
printf(" -f/--force Force reading even without a key*\n");
|
|
|
|
printf(" -1/--v1 Force to read file as a version 1 file\n");
|
|
|
|
printf(" -2/--v2 Force to read file as a version 2 file\n");
|
|
|
|
printf(" -s/--no-simpl Prevent elf files from being simplified*\n");
|
|
|
|
printf(" -x Use default sb1 key\n");
|
|
|
|
printf(" -b Brute force key\n");
|
2014-09-07 10:12:09 +00:00
|
|
|
printf(" --ignore-sha1 Ignore SHA-1 mismatch*\n");
|
2011-11-01 11:23:43 +00:00
|
|
|
printf("Options marked with a * are for debug purpose only\n");
|
2011-10-29 14:22:17 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2010-11-28 20:56:09 +00:00
|
|
|
|
2011-10-29 14:22:17 +00:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2014-09-07 10:12:09 +00:00
|
|
|
unsigned flags = 0;
|
2011-11-01 11:23:43 +00:00
|
|
|
const char *loopback = NULL;
|
2012-11-26 22:54:44 +00:00
|
|
|
bool force_sb1 = false;
|
|
|
|
bool force_sb2 = false;
|
2013-02-16 19:47:07 +00:00
|
|
|
bool brute_force = false;
|
2012-11-26 22:54:44 +00:00
|
|
|
|
2011-10-29 14:22:17 +00:00
|
|
|
while(1)
|
|
|
|
{
|
|
|
|
static struct option long_options[] =
|
|
|
|
{
|
2017-01-01 18:46:01 +00:00
|
|
|
{"help", no_argument, 0, 'h'},
|
2011-10-29 14:22:17 +00:00
|
|
|
{"debug", no_argument, 0, 'd'},
|
2011-10-29 17:04:20 +00:00
|
|
|
{"add-key", required_argument, 0, 'a'},
|
2011-11-01 11:23:24 +00:00
|
|
|
{"no-color", no_argument, 0, 'n'},
|
2011-11-01 11:23:43 +00:00
|
|
|
{"loopback", required_argument, 0, 'l'},
|
2012-11-26 22:54:44 +00:00
|
|
|
{"force", no_argument, 0, 'f'},
|
|
|
|
{"v1", no_argument, 0, '1'},
|
|
|
|
{"v2", no_argument, 0, '2'},
|
2012-11-27 23:17:15 +00:00
|
|
|
{"no-simpl", no_argument, 0, 's'},
|
2014-09-07 10:12:09 +00:00
|
|
|
{"ignore-sha1", no_argument, 0, 254},
|
2011-10-29 14:22:17 +00:00
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2017-01-01 18:46:01 +00:00
|
|
|
int c = getopt_long(argc, argv, "hdo:k:zra:nl:f12xsb", long_options, NULL);
|
2011-10-29 14:22:17 +00:00
|
|
|
if(c == -1)
|
|
|
|
break;
|
|
|
|
switch(c)
|
|
|
|
{
|
|
|
|
case -1:
|
|
|
|
break;
|
2011-11-01 11:23:43 +00:00
|
|
|
case 'l':
|
|
|
|
if(loopback)
|
|
|
|
bug("Only one loopback file can be specified !\n");
|
|
|
|
loopback = optarg;
|
|
|
|
break;
|
2011-11-01 11:23:24 +00:00
|
|
|
case 'n':
|
|
|
|
enable_color(false);
|
|
|
|
break;
|
2011-10-29 14:22:17 +00:00
|
|
|
case 'd':
|
|
|
|
g_debug = true;
|
|
|
|
break;
|
2017-01-01 18:46:01 +00:00
|
|
|
case 'h':
|
2011-10-29 14:22:17 +00:00
|
|
|
usage();
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
g_out_prefix = optarg;
|
|
|
|
break;
|
2012-06-27 12:50:39 +00:00
|
|
|
case 'f':
|
|
|
|
g_force = true;
|
|
|
|
break;
|
2011-10-29 14:22:17 +00:00
|
|
|
case 'k':
|
|
|
|
{
|
2012-02-19 17:36:57 +00:00
|
|
|
if(!add_keys_from_file(optarg))
|
|
|
|
bug("Cannot add keys from %s\n", optarg);
|
2011-10-29 14:22:17 +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-10-29 14:22:17 +00:00
|
|
|
add_keys(&g_zero_key, 1);
|
|
|
|
break;
|
2013-07-17 22:27:12 +00:00
|
|
|
}
|
2012-11-26 22:54:44 +00:00
|
|
|
case 'x':
|
2012-12-16 00:53:20 +00:00
|
|
|
{
|
|
|
|
struct crypto_key_t key;
|
|
|
|
sb1_get_default_key(&key);
|
|
|
|
add_keys(&key, 1);
|
2012-11-26 22:54:44 +00:00
|
|
|
break;
|
2012-12-16 00:53:20 +00:00
|
|
|
}
|
2011-10-29 22:27:53 +00:00
|
|
|
case 'r':
|
2014-09-07 10:12:09 +00:00
|
|
|
flags |= SB_RAW_MODE;
|
2011-10-29 22:27:53 +00:00
|
|
|
break;
|
2011-10-29 17:04:20 +00:00
|
|
|
case 'a':
|
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))
|
2012-06-27 12:50:39 +00:00
|
|
|
bug("Invalid key specified as argument\n");
|
2011-10-29 17:04:20 +00:00
|
|
|
if(*s != 0)
|
2012-06-27 12:50:39 +00:00
|
|
|
bug("Trailing characters after key specified as argument\n");
|
2011-10-29 14:22:17 +00:00
|
|
|
add_keys(&key, 1);
|
|
|
|
break;
|
|
|
|
}
|
2012-11-26 22:54:44 +00:00
|
|
|
case '1':
|
|
|
|
force_sb1 = true;
|
|
|
|
break;
|
|
|
|
case '2':
|
|
|
|
force_sb2 = true;
|
|
|
|
break;
|
2012-11-27 23:17:15 +00:00
|
|
|
case 's':
|
|
|
|
g_elf_simplify = false;
|
|
|
|
break;
|
2013-02-16 19:47:07 +00:00
|
|
|
case 'b':
|
|
|
|
brute_force = true;
|
|
|
|
break;
|
2014-09-07 10:12:09 +00:00
|
|
|
case 254:
|
|
|
|
flags |= SB_IGNORE_SHA1;
|
|
|
|
break;
|
2011-10-29 14:22:17 +00:00
|
|
|
default:
|
2013-07-17 22:27:12 +00:00
|
|
|
bug("Internal error: unknown option '%c'\n", c);
|
2011-10-29 14:22:17 +00:00
|
|
|
}
|
|
|
|
}
|
2010-11-28 20:56:09 +00:00
|
|
|
|
2012-11-26 22:54:44 +00:00
|
|
|
if(force_sb1 && force_sb2)
|
|
|
|
bug("You cannot force both version 1 and 2\n");
|
|
|
|
|
2011-10-29 14:22:17 +00:00
|
|
|
if(argc - optind != 1)
|
2011-10-29 17:04:20 +00:00
|
|
|
{
|
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
}
|
2010-11-28 10:53:01 +00:00
|
|
|
|
2011-11-01 11:23:43 +00:00
|
|
|
const char *sb_filename = argv[optind];
|
2010-11-28 10:53:01 +00:00
|
|
|
|
2012-11-26 22:54:44 +00:00
|
|
|
enum sb_version_guess_t ver = guess_sb_version(sb_filename);
|
2013-07-17 22:27:12 +00:00
|
|
|
if(ver == SB_VERSION_ERR)
|
|
|
|
{
|
|
|
|
printf("Cannot open/read SB file: %m\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2012-11-26 22:54:44 +00:00
|
|
|
|
|
|
|
if(force_sb2 || ver == SB_VERSION_2)
|
2011-11-06 01:49:13 +00:00
|
|
|
{
|
2012-11-26 22:54:44 +00:00
|
|
|
enum sb_error_t err;
|
2014-09-07 10:12:09 +00:00
|
|
|
struct sb_file_t *file = sb_read_file(sb_filename, flags, NULL, generic_std_printf, &err);
|
2012-11-26 22:54:44 +00:00
|
|
|
if(file == NULL)
|
|
|
|
{
|
|
|
|
color(OFF);
|
|
|
|
printf("SB read failed: %d\n", err);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-11-06 01:49:13 +00:00
|
|
|
color(OFF);
|
2012-11-26 22:54:44 +00:00
|
|
|
if(g_out_prefix)
|
|
|
|
extract_sb_file(file);
|
|
|
|
if(g_debug)
|
|
|
|
{
|
|
|
|
color(GREY);
|
|
|
|
printf("[Debug output]\n");
|
2013-08-21 20:20:53 +00:00
|
|
|
sb_dump(file, NULL, generic_std_printf);
|
2012-11-26 22:54:44 +00:00
|
|
|
}
|
|
|
|
if(loopback)
|
2013-08-21 20:20:53 +00:00
|
|
|
sb_write_file(file, loopback, 0, generic_std_printf);
|
2012-11-26 22:54:44 +00:00
|
|
|
sb_free(file);
|
2011-11-06 01:49:13 +00:00
|
|
|
}
|
2012-11-26 22:54:44 +00:00
|
|
|
else if(force_sb1 || ver == SB_VERSION_1)
|
2011-11-01 11:23:43 +00:00
|
|
|
{
|
2013-02-16 19:47:07 +00:00
|
|
|
if(brute_force)
|
|
|
|
{
|
|
|
|
struct crypto_key_t key;
|
|
|
|
enum sb1_error_t err;
|
2013-08-21 20:20:53 +00:00
|
|
|
if(!sb1_brute_force(sb_filename, NULL, generic_std_printf, &err, &key))
|
2013-02-16 19:47:07 +00:00
|
|
|
{
|
|
|
|
color(OFF);
|
|
|
|
printf("Brute force failed: %d\n", err);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
color(RED);
|
|
|
|
printf("Key found:");
|
|
|
|
color(YELLOW);
|
|
|
|
for(int i = 0; i < 32; i++)
|
|
|
|
printf(" %08x", key.u.xor_key[i / 16].k[i % 16]);
|
|
|
|
color(OFF);
|
|
|
|
printf("\n");
|
|
|
|
color(RED);
|
|
|
|
printf("Key: ");
|
|
|
|
color(YELLOW);
|
|
|
|
for(int i = 0; i < 128; i++)
|
|
|
|
printf("%02x", key.u.xor_key[i / 64].key[i % 64]);
|
|
|
|
color(OFF);
|
|
|
|
printf("\n");
|
|
|
|
add_keys(&key, 1);
|
|
|
|
}
|
|
|
|
|
2012-11-26 22:54:44 +00:00
|
|
|
enum sb1_error_t err;
|
2013-08-21 20:20:53 +00:00
|
|
|
struct sb1_file_t *file = sb1_read_file(sb_filename, NULL, generic_std_printf, &err);
|
2012-11-26 22:54:44 +00:00
|
|
|
if(file == NULL)
|
|
|
|
{
|
|
|
|
color(OFF);
|
|
|
|
printf("SB read failed: %d\n", err);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
color(OFF);
|
|
|
|
if(g_out_prefix)
|
|
|
|
extract_sb1_file(file);
|
|
|
|
if(g_debug)
|
|
|
|
{
|
|
|
|
color(GREY);
|
|
|
|
printf("[Debug output]\n");
|
2013-08-21 20:20:53 +00:00
|
|
|
sb1_dump(file, NULL, generic_std_printf);
|
2012-11-26 22:54:44 +00:00
|
|
|
}
|
|
|
|
if(loopback)
|
|
|
|
sb1_write_file(file, loopback);
|
2013-06-15 20:19:52 +00:00
|
|
|
|
2012-11-26 22:54:44 +00:00
|
|
|
sb1_free(file);
|
2011-11-01 11:23:43 +00:00
|
|
|
}
|
2012-11-26 22:54:44 +00:00
|
|
|
else
|
2011-11-01 11:23:43 +00:00
|
|
|
{
|
2012-11-26 22:54:44 +00:00
|
|
|
color(OFF);
|
|
|
|
printf("Cannot guess file type, are you sure it's a valid image ?\n");
|
|
|
|
return 1;
|
2011-11-01 11:23:43 +00:00
|
|
|
}
|
2011-11-06 01:49:13 +00:00
|
|
|
clear_keys();
|
2013-06-15 20:19:52 +00:00
|
|
|
|
2010-11-28 20:56:09 +00:00
|
|
|
return 0;
|
2010-11-28 10:53:01 +00:00
|
|
|
}
|