summaryrefslogtreecommitdiffstats
path: root/utils/imxtools
diff options
context:
space:
mode:
authorAmaury Pouly <amaury.pouly@gmail.com>2013-08-06 19:30:05 +0200
committerAmaury Pouly <amaury.pouly@gmail.com>2013-08-06 21:24:38 +0200
commitc0aba07f1a4221e0aee10f7fddc47dde29e5282e (patch)
tree6c19e8b1b61ebbb620efdadd01370aeb0d1ea6ec /utils/imxtools
parent97459def3c34d338d3ced9bd2b55f8902573aa19 (diff)
downloadrockbox-c0aba07f1a4221e0aee10f7fddc47dde29e5282e.tar.gz
rockbox-c0aba07f1a4221e0aee10f7fddc47dde29e5282e.tar.bz2
rockbox-c0aba07f1a4221e0aee10f7fddc47dde29e5282e.zip
sbtools: fix output printing
The code used printf instead of the provided printf functions, resulting in strange output. Change-Id: I2c7c2531d8d54ecdea97e8c189d18d351320ca7d
Diffstat (limited to 'utils/imxtools')
-rw-r--r--utils/imxtools/sbtools/misc.c46
-rw-r--r--utils/imxtools/sbtools/misc.h10
-rw-r--r--utils/imxtools/sbtools/sb.c29
3 files changed, 60 insertions, 25 deletions
diff --git a/utils/imxtools/sbtools/misc.c b/utils/imxtools/sbtools/misc.c
index 71b3cb600b..58e9609909 100644
--- a/utils/imxtools/sbtools/misc.c
+++ b/utils/imxtools/sbtools/misc.c
@@ -22,6 +22,7 @@
#include <stdio.h>
#include <time.h>
#include <ctype.h>
+#include <stdarg.h>
#include "misc.h"
bool g_debug = false;
@@ -193,6 +194,15 @@ void clear_keys()
g_key_array = NULL;
}
+void misc_std_printf(void *user, const char *fmt, ...)
+{
+ (void) user;
+ va_list args;
+ va_start(args, fmt);
+ vprintf(fmt, args);
+ va_end(args);
+}
+
bool add_keys_from_file(const char *key_file)
{
int size;
@@ -233,7 +243,7 @@ bool add_keys_from_file(const char *key_file)
if(g_debug)
{
printf("Add key: ");
- print_key(&k, true);
+ print_key(NULL, misc_std_printf, &k, true);
}
add_keys(&k, 1);
/* request at least one space character before next key, or end of file */
@@ -253,45 +263,45 @@ bool add_keys_from_file(const char *key_file)
return true;
}
-void print_hex(byte *data, int len, bool newline)
+void print_hex(void *user, misc_printf_t printf, byte *data, int len, bool newline)
{
for(int i = 0; i < len; i++)
- printf("%02X ", data[i]);
+ printf(user, "%02X ", data[i]);
if(newline)
- printf("\n");
+ printf(user, "\n");
}
-void print_key(struct crypto_key_t *key, bool newline)
+void print_key(void *user, misc_printf_t printf, struct crypto_key_t *key, bool newline)
{
switch(key->method)
{
case CRYPTO_KEY:
- print_hex(key->u.key, 16, false);
+ print_hex(user, printf, key->u.key, 16, false);
break;
case CRYPTO_USBOTP:
- printf("USB-OTP(%04x:%04x)", key->u.vid_pid >> 16, key->u.vid_pid & 0xffff);
+ printf(user, "USB-OTP(%04x:%04x)", key->u.vid_pid >> 16, key->u.vid_pid & 0xffff);
break;
case CRYPTO_NONE:
- printf("none");
+ printf(user, "none");
break;
case CRYPTO_XOR_KEY:
- print_hex(&key->u.xor_key[0].key[0], 64, false);
- print_hex(&key->u.xor_key[1].key[0], 64, false);
+ print_hex(user, printf, &key->u.xor_key[0].key[0], 64, false);
+ print_hex(user, printf, &key->u.xor_key[1].key[0], 64, false);
break;
default:
- printf("unknown");
+ printf(user, "unknown");
}
if(newline)
- printf("\n");
+ printf(user, "\n");
}
-char OFF[] = { 0x1b, 0x5b, 0x31, 0x3b, '0', '0', 0x6d, '\0' };
+const char OFF[] = { 0x1b, 0x5b, 0x31, 0x3b, '0', '0', 0x6d, '\0' };
-char GREY[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '0', 0x6d, '\0' };
-char RED[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '1', 0x6d, '\0' };
-char GREEN[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '2', 0x6d, '\0' };
-char YELLOW[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '3', 0x6d, '\0' };
-char BLUE[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '4', 0x6d, '\0' };
+const char GREY[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '0', 0x6d, '\0' };
+const char RED[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '1', 0x6d, '\0' };
+const char GREEN[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '2', 0x6d, '\0' };
+const char YELLOW[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '3', 0x6d, '\0' };
+const char BLUE[] = { 0x1b, 0x5b, 0x31, 0x3b, '3', '4', 0x6d, '\0' };
static bool g_color_enable = true;
diff --git a/utils/imxtools/sbtools/misc.h b/utils/imxtools/sbtools/misc.h
index 8429e84faf..624cd5a9c0 100644
--- a/utils/imxtools/sbtools/misc.h
+++ b/utils/imxtools/sbtools/misc.h
@@ -43,6 +43,10 @@ typedef struct crypto_key_t *key_array_t;
int g_nr_keys;
key_array_t g_key_array;
+typedef void (*misc_printf_t)(void *user, const char *fmt, ...);
+
+void misc_std_printf(void *user, const char *fmt, ...);
+
void *memdup(const void *p, size_t len);
void *augment_array(void *arr, size_t elem_sz, size_t cnt, void *aug, size_t aug_cnt);
void augment_array_ex(void **arr, size_t elem_sz, int *cnt, int *capacity,
@@ -50,14 +54,14 @@ void augment_array_ex(void **arr, size_t elem_sz, int *cnt, int *capacity,
void generate_random_data(void *buf, size_t sz);
void *xmalloc(size_t s);
int convxdigit(char digit, byte *val);
-void print_hex(byte *data, int len, bool newline);
+void print_hex(void *user, misc_printf_t printf, 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);
bool add_keys_from_file(const char *key_file);
-void print_key(struct crypto_key_t *key, bool newline);
+void print_key(void *user, misc_printf_t printf, struct crypto_key_t *key, bool newline);
void clear_keys();
-typedef char color_t[];
+typedef const char color_t[];
extern color_t OFF, GREY, RED, GREEN, YELLOW, BLUE;
void color(color_t c);
diff --git a/utils/imxtools/sbtools/sb.c b/utils/imxtools/sbtools/sb.c
index df3622940f..218ea5a6f7 100644
--- a/utils/imxtools/sbtools/sb.c
+++ b/utils/imxtools/sbtools/sb.c
@@ -674,6 +674,25 @@ struct sb_file_t *sb_read_file_ex(const char *filename, size_t offset, size_t si
#undef fatal
}
+struct printer_t
+{
+ void *user;
+ sb_color_printf cprintf;
+ const char *color;
+ bool error;
+};
+
+static void sb_printer(void *user, const char *fmt, ...)
+{
+ struct printer_t *p = user;
+ va_list args;
+ va_start(args, fmt);
+ char buffer[1024];
+ vsnprintf(buffer, sizeof(buffer), fmt, args);
+ p->cprintf(p->user, p->error, p->color, "%s", buffer);
+ va_end(args);
+}
+
struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, bool raw_mode, void *u,
sb_color_printf cprintf, enum sb_error_t *err)
{
@@ -686,8 +705,9 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, bool raw_mode, voi
cprintf(u, true, GREY, __VA_ARGS__); \
sb_free(sb_file); \
return NULL; } while(0)
+ struct printer_t printer = {.user = u, .cprintf = cprintf, .color = OFF, .error = false };
#define print_hex(c, p, len, nl) \
- do { printf(c, ""); print_hex(p, len, nl); } while(0)
+ do { printer.color = c; print_hex(&printer, sb_printer, p, len, nl); } while(0)
struct sha_1_params_t sha_1_params;
sb_file = xmalloc(sizeof(struct sb_file_t));
@@ -798,8 +818,8 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, bool raw_mode, voi
{
printf(RED, " Key %d\n", i),
printf(GREEN, " Key: ");
- printf(YELLOW, "");
- print_key(&g_key_array[i], true);
+ printer.color = YELLOW;
+ print_key(&printer, sb_printer, &g_key_array[i], true);
printf(GREEN, " CBC-MAC: ");
/* check it */
byte zero[16];
@@ -1137,8 +1157,9 @@ void sb_free(struct sb_file_t *file)
void sb_dump(struct sb_file_t *file, void *u, sb_color_printf cprintf)
{
#define printf(c, ...) cprintf(u, false, c, __VA_ARGS__)
+ struct printer_t printer = {.user = u, .cprintf = cprintf, .color = OFF, .error = false };
#define print_hex(c, p, len, nl) \
- do { printf(c, ""); print_hex(p, len, nl); } while(0)
+ do { printer.color = c; print_hex(&printer, sb_printer, p, len, nl); } while(0)
#define TREE RED
#define HEADER GREEN