Created
June 11, 2020 15:40
-
-
Save akarpenko/fe702ff73a28374b24e70be18de5b46b to your computer and use it in GitHub Desktop.
Patch for sbtools to add parsesb tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/utils/imxtools/sbtools/Makefile b/utils/imxtools/sbtools/Makefile | |
index e6d064b2a3..af51fabf13 100644 | |
--- a/utils/imxtools/sbtools/Makefile | |
+++ b/utils/imxtools/sbtools/Makefile | |
@@ -3,9 +3,11 @@ CC=gcc | |
CXX=g++ | |
LD=g++ | |
CFLAGS=-O3 -g -std=c99 -Wall `pkg-config --cflags libusb-1.0` $(DEFINES) | |
-CXXFLAGS=-O3 -g -Wall `pkg-config --cflags libcrypto++` $(DEFINES) | |
-LDFLAGS=`pkg-config --libs libusb-1.0` `pkg-config --libs libcrypto++` | |
-BINS=elftosb sbtoelf sbloader rsrctool elftosb1 | |
+#CXXFLAGS=-O3 -g -Wall `pkg-config --cflags libcrypto++` $(DEFINES) | |
+#LDFLAGS=`pkg-config --libs libusb-1.0` `pkg-config --libs libcrypto++` | |
+CXXFLAGS=-O3 -g -Wall $(DEFINES) | |
+LDFLAGS=`pkg-config --libs libusb-1.0` -lcryptopp | |
+BINS=elftosb sbtoelf sbloader rsrctool elftosb1 parsesb | |
all: $(BINS) | |
@@ -30,6 +32,9 @@ sbloader: sbloader.o | |
rsrctool: rsrctool.o rsrc.o misc.o | |
$(LD) -o $@ $^ $(LDFLAGS) | |
+parsesb: parsesb.o crc.o crypto.o elf.o dbparser.o misc.o sb.o | |
+ $(LD) -o $@ $^ $(LDFLAGS) | |
+ | |
clean: | |
rm -fr *.o | |
diff --git a/utils/imxtools/sbtools/sb.c b/utils/imxtools/sbtools/sb.c | |
index cbeacf9c3f..5a74500714 100644 | |
--- a/utils/imxtools/sbtools/sb.c | |
+++ b/utils/imxtools/sbtools/sb.c | |
@@ -513,8 +513,8 @@ enum sb_error_t sb_write_file(struct sb_file_t *sb, const char *filename, void * | |
#undef printf | |
} | |
-static struct sb_section_t *read_section(bool data_sec, uint32_t id, byte *buf, | |
- int size, const char *indent, void *u, generic_printf_t cprintf, enum sb_error_t *err) | |
+struct sb_section_t *read_section(bool data_sec, uint32_t id, byte *buf, | |
+ int size, bool fix_crc, const char *indent, void *u, generic_printf_t cprintf, enum sb_error_t *err) | |
{ | |
#define printf(c, ...) cprintf(u, false, c, __VA_ARGS__) | |
#define fatal(e, ...) \ | |
@@ -554,7 +554,17 @@ static struct sb_section_t *read_section(bool data_sec, uint32_t id, byte *buf, | |
printf(OFF, "%s", indent); | |
uint8_t checksum = instruction_checksum(hdr); | |
if(checksum != hdr->checksum) | |
- fatal(SB_CHECKSUM_ERROR, "Bad instruction checksum\n"); | |
+ if (fix_crc) | |
+ { | |
+ printf(RED, " Failed (checksum=0x%02x)\n", checksum); | |
+ uint8_t new_instruction_checksum = instruction_checksum(hdr); | |
+ hdr->checksum = new_instruction_checksum; | |
+ printf(RED, " Fixed\n"); | |
+ } | |
+ else | |
+ { | |
+ fatal(SB_CHECKSUM_ERROR, "Bad instruction checksum\n"); | |
+ } | |
if(hdr->flags != 0) | |
{ | |
printf(GREY, "["); | |
@@ -583,7 +593,18 @@ static struct sb_section_t *read_section(bool data_sec, uint32_t id, byte *buf, | |
else | |
{ | |
printf(RED, " Failed (crc=0x%08x)\n", computed_crc); | |
- fatal(SB_CHECKSUM_ERROR, "Instruction data crc error\n"); | |
+ if (fix_crc) | |
+ { | |
+ // update data crc | |
+ load->crc = computed_crc; | |
+ // now also need to update instruction checksum | |
+ uint8_t new_instruction_checksum = instruction_checksum(hdr); | |
+ hdr->checksum = new_instruction_checksum; | |
+ printf(RED, " Fixed\n"); | |
+ } | |
+ else{ | |
+ fatal(SB_CHECKSUM_ERROR, "Instruction data crc error\n"); | |
+ } | |
} | |
pos += load->len + sizeof(struct sb_instruction_load_t); | |
@@ -618,6 +639,8 @@ static struct sb_section_t *read_section(bool data_sec, uint32_t id, byte *buf, | |
else | |
printf(RED, "JUMP"); | |
printf(OFF, " | "); | |
+ printf(GREY, "flags=0x%04x", call->hdr.flags); | |
+ printf(OFF, " | "); | |
printf(BLUE, "addr=0x%08x", call->addr); | |
printf(OFF, " | "); | |
printf(GREEN, "arg=0x%08x\n", call->arg); | |
@@ -1015,7 +1038,7 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo | |
memcpy(sec, buf + pos, size); | |
struct sb_section_t *s = read_section(data_sec, sec_hdr->identifier, | |
- sec, size, " ", u, cprintf, out_err); | |
+ sec, size, false, " ", u, cprintf, out_err); | |
free(sec); | |
if(s) | |
{ | |
@@ -1113,7 +1136,7 @@ struct sb_file_t *sb_read_memory(void *_buf, size_t filesize, unsigned flags, vo | |
memcpy(sec, buf + pos, size); | |
struct sb_section_t *s = read_section(data_sec, tag->identifier, | |
- sec, size, " ", u, cprintf, out_err); | |
+ sec, size, false, " ", u, cprintf, out_err); | |
free(sec); | |
if(s) | |
{ | |
diff --git a/utils/imxtools/sbtools/sb.h b/utils/imxtools/sbtools/sb.h | |
index 62fe4464fb..bd8891e795 100644 | |
--- a/utils/imxtools/sbtools/sb.h | |
+++ b/utils/imxtools/sbtools/sb.h | |
@@ -258,4 +258,7 @@ void sb_free_section(struct sb_section_t file); | |
void sb_free(struct sb_file_t *file); | |
void sb_get_zero_key(struct crypto_key_t *key); | |
+struct sb_section_t *read_section(bool data_sec, uint32_t id, byte *buf, | |
+ int size, bool fix_crc, const char *indent, void *u, generic_printf_t cprintf, enum sb_error_t *err); | |
+ | |
#endif /* __SB_H__ */ | |
diff --git a/utils/imxtools/sbtools/parsesb.c b/utils/imxtools/sbtools/parsesb.c | |
new file mode 100644 | |
index 0000000000..a6ccba497b | |
--- /dev/null | |
+++ b/utils/imxtools/sbtools/parsesb.c | |
@@ -0,0 +1,61 @@ | |
+#define _ISOC99_SOURCE | |
+#define _POSIX_C_SOURCE 200809L /* for strdup */ | |
+#include <stdio.h> | |
+#include <errno.h> | |
+#include <stdlib.h> | |
+#include <string.h> | |
+#include <ctype.h> | |
+#include <time.h> | |
+#include <stdarg.h> | |
+#include <strings.h> | |
+#include <getopt.h> | |
+ | |
+#include "crypto.h" | |
+#include "elf.h" | |
+#include "sb.h" | |
+#include "dbparser.h" | |
+#include "misc.h" | |
+#include "sb.h" | |
+ | |
+void parse_sb(const char *file) { | |
+ enum sb_error_t err; | |
+ size_t size; | |
+ FILE *f = fopen(file, "r"); | |
+ if(f == NULL) { | |
+ if(g_debug) | |
+ perror("Cannot open input file"); | |
+ return; | |
+ } | |
+ fseek(f, 0, SEEK_END); | |
+ size = ftell(f); | |
+ fseek(f, 0, SEEK_SET); | |
+ byte *buf = xmalloc(size); | |
+ if(fread(buf, size, 1, f) != 1) { | |
+ if(g_debug) | |
+ perror("Cannot read inputfile"); | |
+ return; | |
+ } | |
+ fclose(f); | |
+ | |
+ read_section(false, 0, | |
+ buf, size, true /* fix_crc */, "", NULL, generic_std_printf, &err); | |
+ | |
+ f = fopen(file, "w"); | |
+ fwrite(buf, size, 1, f); | |
+ fclose(f); | |
+ | |
+ free(buf); | |
+} | |
+ | |
+int main(int argc, char **argv) { | |
+ g_debug = true; | |
+ | |
+ if (argc < 2) { | |
+ printf("Please specify unencrypted section file\n"); | |
+ return 1; | |
+ } | |
+ char *input_filename = argv[1]; | |
+ parse_sb(input_filename); | |
+ | |
+ return 0; | |
+} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment