From 284d5c8d51405388ce7bfe7dce692c57eaaaa392 Mon Sep 17 00:00:00 2001 From: Andrey Gusakov Date: Sun, 31 Oct 2021 22:58:49 +0300 Subject: [PATCH 1/3] Fix compilation issues --- hex2dfu.c | 1 + 1 file changed, 1 insertion(+) diff --git a/hex2dfu.c b/hex2dfu.c index 5955433..dbb39bc 100644 --- a/hex2dfu.c +++ b/hex2dfu.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #define TARGET_NAME_ENCEDO "EncedoKey" From ed82a0674947395ff7f4765a81d50cd81a0dfc70 Mon Sep 17 00:00:00 2001 From: Andrey Gusakov Date: Mon, 1 Nov 2021 00:20:30 +0300 Subject: [PATCH 2/3] Support multiple input hex files --- hex2dfu.c | 415 +++++++++++++++++++++++++++++------------------------- 1 file changed, 226 insertions(+), 189 deletions(-) diff --git a/hex2dfu.c b/hex2dfu.c index dbb39bc..29f0d0a 100644 --- a/hex2dfu.c +++ b/hex2dfu.c @@ -5,6 +5,8 @@ #include #include +#define MAX_IN_HEX 16 + #define TARGET_NAME_ENCEDO "EncedoKey" // uncomment to add support for digital code signature using ED25519 //#define ED25519_SUPPORT @@ -32,9 +34,11 @@ unsigned int crc32(unsigned int crc, const void *buf, size_t size); //efab5b0739a834bac702aeb5cd08ffe227908faaae501f910e7e07d8d41fbb06 int main (int argc, char **argv) { int i, c, vid =0x0483, pid = 0xdf11, ver = 0xffff; - char *tar0 = NULL, *tar0_lab = NULL, *out_fn = NULL; + char *tar0_lab = NULL, *out_fn = NULL; FILE *inFile, *outFile; unsigned char json_output = 0; + char *in_hex[MAX_IN_HEX]; + int image_cnt = 0; #ifdef ED25519_SUPPORT unsigned char hash_buf[64]; @@ -44,9 +48,10 @@ int main (int argc, char **argv) { unsigned char public_key[32], private_key[64], signature[64], public_key_publisher[32]; #endif - unsigned int tar0_start_address; - int tar0_len; - unsigned char *tar0_buf; + unsigned int tar_start_address[MAX_IN_HEX]; + int tar_len[MAX_IN_HEX]; + unsigned char *tar_buf[MAX_IN_HEX]; + int total_len = 0; unsigned char *dfu; int dfu_len; @@ -58,8 +63,9 @@ int main (int argc, char **argv) { case 'J': json_output = 1; break; - case 'i': //target0 input file name - tar0 = optarg; + case 'i': //target input file names + in_hex[image_cnt] = optarg; + image_cnt++; break; case 'l': //target0 label tar0_lab = optarg; @@ -114,8 +120,8 @@ int main (int argc, char **argv) { } } - if (!tar0) { - perror ("No input file specifed.\n"); + if (image_cnt == 0) { + perror ("No input file(s) specifed.\n"); return 0; } @@ -130,7 +136,7 @@ int main (int argc, char **argv) { if (c != 32) { perror ("ED25519 'secret' have to be 32bytes long.\n"); return 0; - } + } ed25519_create_keypair(public_key, private_key, ed25519_secret); @@ -146,190 +152,222 @@ int main (int argc, char **argv) { } #endif - inFile = fopen ( tar0, "r"); - tar0_buf = ihex2bin_buf(&tar0_start_address, &tar0_len, inFile); + for (i = 0; i < image_cnt; i++) { + inFile = fopen(in_hex[i], "r"); + if (inFile == NULL) { + fprintf(stderr, "Failed to open %s\n", in_hex[i]); + return 0; + } + tar_buf[i] = ihex2bin_buf(&tar_start_address[i], &tar_len[i], inFile); + fclose (inFile); - fclose (inFile); - if (tar0_buf && (tar0_len > 0)) { - if ((add_crc32>0) && (add_crc32 < (tar0_start_address+tar0_len-256))) { //-c request CRC32 placement at given address - add_crc32 -= tar0_start_address; - tar0_buf[add_crc32 + 4] = tar0_len>>0 & 0xFF; //binary code length first(little endian) - tar0_buf[add_crc32 + 5] = tar0_len>>8 & 0xFF; - tar0_buf[add_crc32 + 6] = tar0_len>>16 & 0xFF; - tar0_buf[add_crc32 + 7] = tar0_len>>24 & 0xFF; + if ((tar_buf[i] == NULL) || (tar_len[i] < 0)) { + fprintf(stderr, "Failed to parse %s\n", in_hex[i]); + return 0; + } + + total_len += tar_len[i]; + + if ((add_crc32 > 0) && + (add_crc32 < (tar_start_address[i] + tar_len[i] - 256))) { //-c request CRC32 placement at given address + add_crc32 -= tar_start_address[i]; + tar_buf[i][add_crc32 + 4] = tar_len[i]>>0 & 0xFF; //binary code length first(little endian) + tar_buf[i][add_crc32 + 5] = tar_len[i]>>8 & 0xFF; + tar_buf[i][add_crc32 + 6] = tar_len[i]>>16 & 0xFF; + tar_buf[i][add_crc32 + 7] = tar_len[i]>>24 & 0xFF; #ifdef ED25519_SUPPORT - if (ed25519_secret) { - sha512_init(&hash); - sha512_update(&hash, tar0_buf, add_crc32); - sha512_update(&hash, tar0_buf+add_crc32+256, tar0_len-(add_crc32+256)); - sha512_final(&hash, hash_buf); - - ed25519_sign(signature, hash_buf, 64, public_key, private_key); - memmove(tar0_buf+add_crc32+0x10, signature, 64); - memmove(tar0_buf+add_crc32+0x10+64, public_key, 32); - - if (ed25519_public_add) { - memmove(tar0_buf+add_crc32+0x10+64+32, public_key_publisher, 32); - } - } + if (ed25519_secret) { + sha512_init(&hash); + sha512_update(&hash, tar_buf[i], add_crc32); + sha512_update(&hash, tar_buf[i] + add_crc32 + 256, tar_len[i] - (add_crc32 + 256)); + sha512_final(&hash, hash_buf); + + ed25519_sign(signature, hash_buf, 64, public_key, private_key); + memmove(tar_buf[i] + add_crc32 + 0x10, signature, 64); + memmove(tar_buf[i] + add_crc32 + 0x10 + 64, public_key, 32); + + if (ed25519_public_add) { + memmove(tar_buf[i] + add_crc32 + 0x10 + 64 + 32, public_key_publisher, 32); + } + } #endif - crc = crc32(0, tar0_buf, add_crc32); //calc CRC upto placement address - crc = crc32(crc, tar0_buf+add_crc32+4, tar0_len-(add_crc32+4)); //calc the rest of - starting from placement+4 up to end - tar0_buf[add_crc32] = crc>>0 & 0xFF; //CRC placement (little endian) - tar0_buf[add_crc32 + 1] = crc>>8 & 0xFF; - tar0_buf[add_crc32 + 2] = crc>>16 & 0xFF; - tar0_buf[add_crc32 + 3] = crc>>24 & 0xFF; + crc = crc32(0, tar_buf[i], add_crc32); //calc CRC upto placement address + crc = crc32(crc, tar_buf[i] + add_crc32 + 4, tar_len[i] - (add_crc32 + 4)); //calc the rest of - starting from placement+4 up to end + tar_buf[i][add_crc32 + 0] = crc>>0 & 0xFF; //CRC placement (little endian) + tar_buf[i][add_crc32 + 1] = crc>>8 & 0xFF; + tar_buf[i][add_crc32 + 2] = crc>>16 & 0xFF; + tar_buf[i][add_crc32 + 3] = crc>>24 & 0xFF; } - - dfu_len = 11 + 274 + 8+ tar0_len + 16; - dfu = calloc(1, dfu_len); - if (dfu) { - //DFU Suffix - memmove(dfu, "DfuSe", 5); //szSignature - dfu[5] = 0x01; //bVersion - tmp = dfu_len - 0x10; - dfu[6] = (unsigned char)(tmp & 0xFF); //DFUImageSize (except Prefix!) - dfu[7] = (unsigned char)(tmp>>8 & 0xFF); - dfu[8] = (unsigned char)(tmp>>16 & 0xFF); - dfu[9] = (unsigned char)(tmp>>24 & 0xFF); - dfu[10] = 1; //bTargets - - - //DFU Image - c = 11; - memmove(dfu+c, "Target", 6); //szSignature 'Target' - c += 6; - dfu[c++] = 0x00; //bAlternateSettings //to check - if (tar0_lab) { - dfu[c] = 0x01; //bTargetNamed - memmove(dfu+c+4, tar0_lab, strlen(tar0_lab)>254?254:strlen(tar0_lab)); //szTargetName - } else { - dfu[c] = 0x01; //place default target name - memmove(dfu+c+4, TARGET_NAME_ENCEDO, strlen(TARGET_NAME_ENCEDO)); - } - c += 259; - tmp = 8 + tar0_len; //ImageElement length (8+bin_data) - dfu[c++] = tmp & 0xFF; //dwTargetSize - dfu[c++] = tmp>>8 & 0xFF; - dfu[c++] = tmp>>16 & 0xFF; - dfu[c++] = tmp>>24 & 0xFF; - dfu[c] = 0x01; //dwNbElements - c += 4; - - //Image Element - dfu[c++] = tar0_start_address & 0xFF; //dwElementAddress - dfu[c++] = tar0_start_address>>8 & 0xFF; - dfu[c++] = tar0_start_address>>16 & 0xFF; - dfu[c++] = tar0_start_address>>24 & 0xFF; - dfu[c++] = tar0_len & 0xFF; //dwElementSize - dfu[c++] = tar0_len>>8 & 0xFF; - dfu[c++] = tar0_len>>16 & 0xFF; - dfu[c++] = tar0_len>>24 & 0xFF; - memmove(dfu+c, tar0_buf, tar0_len); - - //DFU Suffix - c = dfu_len - 16; - dfu[c++] = ver & 0xFF; //bcdDeviceLo - dfu[c++] = ver>>8 & 0xFF; - dfu[c++] = pid & 0xFF; //idProductLo - dfu[c++] = pid>>8 & 0xFF; - dfu[c++] = vid & 0xFF; //idVendorLo - dfu[c++] = vid>>8 & 0xFF; - dfu[c++] = 0x1A; //bcdDFULo - dfu[c++] = 0x01; - dfu[c++] = 'U'; //ucDfuSignature - dfu[c++] = 'F'; - dfu[c++] = 'D'; - dfu[c++] = 16; //bLength - crc = 0xFFFFFFFF & -crc32(0, dfu, c) - 1 ; - dfu[c++] = crc>>0 & 0xFF; //dwCRC - dfu[c++] = crc>>8 & 0xFF; - dfu[c++] = crc>>16 & 0xFF; - dfu[c++] = crc>>24 & 0xFF; - - - //write DFU to file - outFile = fopen (out_fn, "wb"); - c = fwrite (dfu, dfu_len, 1, outFile); - fclose(outFile); - if (c != 1) { - printf ("error: write to output file\n"); - } - if (json_output) { - printf("{\"code_address\":\"0x%08x\"", tar0_start_address); - printf(",\"code_length\":\"0x%08x\"", tar0_len); - printf(",\"meta_address\":\"0x%08x\"", add_crc32+tar0_start_address); -#ifdef ED25519_SUPPORT - if (ed25519_secret) { - printf(",\"sha512\":\""); - for(c=0; c<64; c++) { - printf("%02x", (unsigned char)hash_buf[c]); - } - printf("\""); - - printf(",\"signature_pubkey\":\""); - for(c=0; c<32; c++) { - printf("%02x", (unsigned char)public_key[c]); - } - printf("\""); - - printf(",\"signature\":\""); - for(c=0; c<64; c++) { - printf("%02x", (unsigned char)signature[c]); - } - printf("\""); - - if (ed25519_public_add) { - printf(",\"publisher_pubkey\":\""); - for(c=0; c<32; c++) { - printf("%02x", (unsigned char)public_key_publisher[c]); - } - printf("\""); - } - printf(",\"crc32\":\"0x%08x\"", crc); - } -#endif - - printf("}\r\n"); - } else { - printf("Data Start Address: 0x%08x\r\n", tar0_start_address); - printf("Data Length: %ub\r\n", tar0_len); -#ifdef ED25519_SUPPORT - if (ed25519_secret) { - printf("SHA512: "); - for(c=0; c<64; c++) { - printf("%02x", (unsigned char)hash_buf[c]); - } - printf("\r\n"); - - printf("Signing PublicKey: "); - for(c=0; c<32; c++) { - printf("%02x", (unsigned char)public_key[c]); - } - printf("\r\n"); - - printf("Signature: "); - for(c=0; c<64; c++) { - printf("%02x", (unsigned char)signature[c]); - } - printf("\r\n"); - - if (ed25519_public_add) { - printf("Publisher PublicKey: "); - for(c=0; c<32; c++) { - printf("%02x", (unsigned char)public_key_publisher[c]); - } - printf("\r\n"); - } - printf("CRC32 data: 0x%08x @0x%08x\r\n", crc, add_crc32+tar0_start_address); - } -#endif - printf("Done.\r\n"); - } - } - } else { - printf ("error: processing input file\n"); } + + //Total dfu len + dfu_len = + 11 + //Prefix + 274 + //Target prefix + (8 * image_cnt) + //Image header + total_len + //Images len + 16; //Suffix + dfu = calloc(1, dfu_len); + if (dfu == NULL) { + perror("Failed to aloocate out buffer\n"); + return 0; + } + + //DFU prefix + memmove(dfu, "DfuSe", 5); //szSignature + dfu[5] = 0x01; //bVersion + tmp = dfu_len - 0x10; + dfu[6] = (unsigned char)(tmp & 0xFF); //DFUImageSize (except Prefix!) + dfu[7] = (unsigned char)(tmp>>8 & 0xFF); + dfu[8] = (unsigned char)(tmp>>16 & 0xFF); + dfu[9] = (unsigned char)(tmp>>24 & 0xFF); + dfu[10] = 1; //bTargets + + //DFU Image + //Target prefix + c = 11; + memmove(dfu + c, "Target", 6); //szSignature 'Target' + c += 6; + dfu[c++] = 0x00; //bAlternateSettings //to check + if (tar0_lab) { + dfu[c] = 0x01; //bTargetNamed + memmove(dfu + c + 4, tar0_lab, (strlen(tar0_lab) > 254) ? 254 : strlen(tar0_lab)); //szTargetName + } else { + dfu[c] = 0x01; //place default target name + memmove(dfu + c + 4, TARGET_NAME_ENCEDO, strlen(TARGET_NAME_ENCEDO)); + } + c += 259; + tmp = (8 * image_cnt) + total_len; //ImageElement length (8+bin_data) + dfu[c++] = (tmp >> 0) & 0xFF; //dwTargetSize + dfu[c++] = (tmp >> 8) & 0xFF; + dfu[c++] = (tmp >> 16) & 0xFF; + dfu[c++] = (tmp >> 24) & 0xFF; + + dfu[c++] = (image_cnt >> 0) & 0xFF; //dwNbElements + dfu[c++] = (image_cnt >> 8) & 0xFF; + dfu[c++] = (image_cnt >> 16) & 0xFF; + dfu[c++] = (image_cnt >> 24) & 0xFF; + + //Image Elements + for (i = 0; i < image_cnt; i++) { + dfu[c++] = (tar_start_address[i] >> 0) & 0xFF; //dwElementAddress + dfu[c++] = (tar_start_address[i] >> 8) & 0xFF; + dfu[c++] = (tar_start_address[i] >> 16) & 0xFF; + dfu[c++] = (tar_start_address[i] >> 24) & 0xFF; + dfu[c++] = (tar_len[i] >> 0) & 0xFF; //dwElementSize + dfu[c++] = (tar_len[i] >> 8) & 0xFF; + dfu[c++] = (tar_len[i] >> 16) & 0xFF; + dfu[c++] = (tar_len[i] >> 24) & 0xFF; + + memmove(dfu + c, tar_buf[i], tar_len[i]); + c += tar_len[i]; + } + + //DFU Suffix + c = dfu_len - 16; + dfu[c++] = (ver >> 0) & 0xFF; //bcdDeviceLo + dfu[c++] = (ver >> 8) & 0xFF; + + dfu[c++] = (pid >> 0) & 0xFF; //idProductLo + dfu[c++] = (pid >> 8) & 0xFF; + + dfu[c++] = (vid >> 0) & 0xFF; //idVendorLo + dfu[c++] = (vid >> 8) & 0xFF; + dfu[c++] = 0x1A; //bcdDFULo + dfu[c++] = 0x01; + dfu[c++] = 'U'; //ucDfuSignature + dfu[c++] = 'F'; + dfu[c++] = 'D'; + dfu[c++] = 16; //bLength + crc = 0xFFFFFFFF & - crc32(0, dfu, c) - 1; + dfu[c++] = (crc >> 0) & 0xFF; //dwCRC + dfu[c++] = (crc >> 8) & 0xFF; + dfu[c++] = (crc >> 16) & 0xFF; + dfu[c++] = (crc >> 24) & 0xFF; + + //write DFU to file + outFile = fopen (out_fn, "wb"); + c = fwrite (dfu, dfu_len, 1, outFile); + fclose(outFile); + if (c != 1) { + printf ("error: write to output file\n"); + } + if (json_output) { + /* TODO: for each input file? */ + printf("{\"code_address\":\"0x%08x\"", tar_start_address[0]); + printf(",\"code_length\":\"0x%08x\"", tar_len[0]); + printf(",\"meta_address\":\"0x%08x\"", add_crc32 + tar_start_address[0]); +#ifdef ED25519_SUPPORT + if (ed25519_secret) { + printf(",\"sha512\":\""); + for(c=0; c<64; c++) { + printf("%02x", (unsigned char)hash_buf[c]); + } + printf("\""); + + printf(",\"signature_pubkey\":\""); + for(c=0; c<32; c++) { + printf("%02x", (unsigned char)public_key[c]); + } + printf("\""); + + printf(",\"signature\":\""); + for(c=0; c<64; c++) { + printf("%02x", (unsigned char)signature[c]); + } + printf("\""); + + if (ed25519_public_add) { + printf(",\"publisher_pubkey\":\""); + for(c=0; c<32; c++) { + printf("%02x", (unsigned char)public_key_publisher[c]); + } + printf("\""); + } + printf(",\"crc32\":\"0x%08x\"", crc); + } +#endif + printf("}\r\n"); + } else { + printf("Total elements: %d\n", image_cnt); + for (i = 0; i < image_cnt; i++) { + printf(" Element %d:\r\n", i); + printf(" Data Start: 0x%08x\r\n", tar_start_address[i]); + printf(" Data Length: %u bytes\r\n", tar_len[i]); + } +#ifdef ED25519_SUPPORT + if (ed25519_secret) { + printf("SHA512: "); + for(c=0; c<64; c++) { + printf("%02x", (unsigned char)hash_buf[c]); + } + printf("\r\n"); + + printf("Signing PublicKey: "); + for(c=0; c<32; c++) { + printf("%02x", (unsigned char)public_key[c]); + } + printf("\r\n"); + + printf("Signature: "); + for(c=0; c<64; c++) { + printf("%02x", (unsigned char)signature[c]); + } + printf("\r\n"); + + if (ed25519_public_add) { + printf("Publisher PublicKey: "); + for(c=0; c<32; c++) { + printf("%02x", (unsigned char)public_key_publisher[c]); + } + printf("\r\n"); + } + printf("CRC32 data: 0x%08x @0x%08x\r\n", crc, add_crc32+tar0_start_address); + } +#endif + } + + printf("Done.\r\n"); return 0; } @@ -353,7 +391,6 @@ void print_help(void) { printf("Example: hex2dfu -i infile.hex -i outfile.dfu\r\n"); } - int hex2bin(unsigned char *obuf, const char *ibuf, int len) { unsigned char c, c2; From 7abc40af31fc97f98959ef34f2f682979527460d Mon Sep 17 00:00:00 2001 From: Andrey Gusakov Date: Mon, 1 Nov 2021 00:22:51 +0300 Subject: [PATCH 3/3] Remove tailing spaces --- hex2dfu.c | 122 +++++++++++++++++++++++++++--------------------------- 1 file changed, 61 insertions(+), 61 deletions(-) diff --git a/hex2dfu.c b/hex2dfu.c index 29f0d0a..434565f 100644 --- a/hex2dfu.c +++ b/hex2dfu.c @@ -31,7 +31,7 @@ unsigned char *ihex2bin_buf(unsigned int *start_address, int *dst_len, FILE *inF unsigned int crc32(unsigned int crc, const void *buf, size_t size); -//efab5b0739a834bac702aeb5cd08ffe227908faaae501f910e7e07d8d41fbb06 +//efab5b0739a834bac702aeb5cd08ffe227908faaae501f910e7e07d8d41fbb06 int main (int argc, char **argv) { int i, c, vid =0x0483, pid = 0xdf11, ver = 0xffff; char *tar0_lab = NULL, *out_fn = NULL; @@ -39,28 +39,28 @@ int main (int argc, char **argv) { unsigned char json_output = 0; char *in_hex[MAX_IN_HEX]; int image_cnt = 0; - -#ifdef ED25519_SUPPORT + +#ifdef ED25519_SUPPORT unsigned char hash_buf[64]; sha512_context hash; unsigned char *ed25519_secret = NULL; unsigned char *ed25519_public = NULL, ed25519_public_add = 0; - unsigned char public_key[32], private_key[64], signature[64], public_key_publisher[32]; + unsigned char public_key[32], private_key[64], signature[64], public_key_publisher[32]; #endif unsigned int tar_start_address[MAX_IN_HEX]; int tar_len[MAX_IN_HEX]; unsigned char *tar_buf[MAX_IN_HEX]; int total_len = 0; - + unsigned char *dfu; int dfu_len; unsigned int crc = 0, tmp, add_crc32 = 0; - + opterr = 0; while ((c = getopt (argc, argv, "hv:p:d:i:l:o:c:S:P:eJ")) != -1) { switch (c) { - case 'J': + case 'J': json_output = 1; break; case 'i': //target input file names @@ -85,26 +85,26 @@ int main (int argc, char **argv) { case 'S': //ED25519 secret (signing key), hex #ifndef ED25519_SUPPORT fprintf (stderr, "Code signing not supported!\n"); - return 1; + return 1; #else - ed25519_secret = optarg; -#endif + ed25519_secret = optarg; +#endif break; case 'P': //ED25519 publisher public, hex #ifndef ED25519_SUPPORT fprintf (stderr, "Code signing not supported!\n"); - return 1; + return 1; #else - ed25519_public = optarg; -#endif + ed25519_public = optarg; +#endif break; - case 'e': + case 'e': #ifndef ED25519_SUPPORT fprintf (stderr, "Code signing not supported!\n"); - return 1; + return 1; #else - ed25519_public_add = 1; -#endif + ed25519_public_add = 1; +#endif break; case 'o': //output file name out_fn = optarg; @@ -119,7 +119,7 @@ int main (int argc, char **argv) { break; } } - + if (image_cnt == 0) { perror ("No input file(s) specifed.\n"); return 0; @@ -130,16 +130,16 @@ int main (int argc, char **argv) { return 0; } -#ifdef ED25519_SUPPORT +#ifdef ED25519_SUPPORT if (ed25519_secret) { c = hex2bin(ed25519_secret, ed25519_secret, strlen(ed25519_secret)); if (c != 32) { perror ("ED25519 'secret' have to be 32bytes long.\n"); return 0; } - + ed25519_create_keypair(public_key, private_key, ed25519_secret); - + if (ed25519_public) { c = hex2bin(public_key_publisher, ed25519_public, strlen(ed25519_public)); if (c != 32) { @@ -148,12 +148,12 @@ int main (int argc, char **argv) { } } else { memmove(public_key_publisher, public_key, 32); - } + } } #endif - + for (i = 0; i < image_cnt; i++) { - inFile = fopen(in_hex[i], "r"); + inFile = fopen(in_hex[i], "r"); if (inFile == NULL) { fprintf(stderr, "Failed to open %s\n", in_hex[i]); return 0; @@ -172,30 +172,30 @@ int main (int argc, char **argv) { (add_crc32 < (tar_start_address[i] + tar_len[i] - 256))) { //-c request CRC32 placement at given address add_crc32 -= tar_start_address[i]; tar_buf[i][add_crc32 + 4] = tar_len[i]>>0 & 0xFF; //binary code length first(little endian) - tar_buf[i][add_crc32 + 5] = tar_len[i]>>8 & 0xFF; - tar_buf[i][add_crc32 + 6] = tar_len[i]>>16 & 0xFF; + tar_buf[i][add_crc32 + 5] = tar_len[i]>>8 & 0xFF; + tar_buf[i][add_crc32 + 6] = tar_len[i]>>16 & 0xFF; tar_buf[i][add_crc32 + 7] = tar_len[i]>>24 & 0xFF; -#ifdef ED25519_SUPPORT +#ifdef ED25519_SUPPORT if (ed25519_secret) { - sha512_init(&hash); + sha512_init(&hash); sha512_update(&hash, tar_buf[i], add_crc32); sha512_update(&hash, tar_buf[i] + add_crc32 + 256, tar_len[i] - (add_crc32 + 256)); - sha512_final(&hash, hash_buf); - + sha512_final(&hash, hash_buf); + ed25519_sign(signature, hash_buf, 64, public_key, private_key); memmove(tar_buf[i] + add_crc32 + 0x10, signature, 64); memmove(tar_buf[i] + add_crc32 + 0x10 + 64, public_key, 32); - + if (ed25519_public_add) { memmove(tar_buf[i] + add_crc32 + 0x10 + 64 + 32, public_key_publisher, 32); } - } -#endif + } +#endif crc = crc32(0, tar_buf[i], add_crc32); //calc CRC upto placement address crc = crc32(crc, tar_buf[i] + add_crc32 + 4, tar_len[i] - (add_crc32 + 4)); //calc the rest of - starting from placement+4 up to end tar_buf[i][add_crc32 + 0] = crc>>0 & 0xFF; //CRC placement (little endian) - tar_buf[i][add_crc32 + 1] = crc>>8 & 0xFF; - tar_buf[i][add_crc32 + 2] = crc>>16 & 0xFF; + tar_buf[i][add_crc32 + 1] = crc>>8 & 0xFF; + tar_buf[i][add_crc32 + 2] = crc>>16 & 0xFF; tar_buf[i][add_crc32 + 3] = crc>>24 & 0xFF; } } @@ -216,13 +216,13 @@ int main (int argc, char **argv) { //DFU prefix memmove(dfu, "DfuSe", 5); //szSignature dfu[5] = 0x01; //bVersion - tmp = dfu_len - 0x10; + tmp = dfu_len - 0x10; dfu[6] = (unsigned char)(tmp & 0xFF); //DFUImageSize (except Prefix!) dfu[7] = (unsigned char)(tmp>>8 & 0xFF); dfu[8] = (unsigned char)(tmp>>16 & 0xFF); dfu[9] = (unsigned char)(tmp>>24 & 0xFF); dfu[10] = 1; //bTargets - + //DFU Image //Target prefix c = 11; @@ -234,7 +234,7 @@ int main (int argc, char **argv) { memmove(dfu + c + 4, tar0_lab, (strlen(tar0_lab) > 254) ? 254 : strlen(tar0_lab)); //szTargetName } else { dfu[c] = 0x01; //place default target name - memmove(dfu + c + 4, TARGET_NAME_ENCEDO, strlen(TARGET_NAME_ENCEDO)); + memmove(dfu + c + 4, TARGET_NAME_ENCEDO, strlen(TARGET_NAME_ENCEDO)); } c += 259; tmp = (8 * image_cnt) + total_len; //ImageElement length (8+bin_data) @@ -253,7 +253,7 @@ int main (int argc, char **argv) { dfu[c++] = (tar_start_address[i] >> 0) & 0xFF; //dwElementAddress dfu[c++] = (tar_start_address[i] >> 8) & 0xFF; dfu[c++] = (tar_start_address[i] >> 16) & 0xFF; - dfu[c++] = (tar_start_address[i] >> 24) & 0xFF; + dfu[c++] = (tar_start_address[i] >> 24) & 0xFF; dfu[c++] = (tar_len[i] >> 0) & 0xFF; //dwElementSize dfu[c++] = (tar_len[i] >> 8) & 0xFF; dfu[c++] = (tar_len[i] >> 16) & 0xFF; @@ -269,10 +269,10 @@ int main (int argc, char **argv) { dfu[c++] = (ver >> 8) & 0xFF; dfu[c++] = (pid >> 0) & 0xFF; //idProductLo - dfu[c++] = (pid >> 8) & 0xFF; + dfu[c++] = (pid >> 8) & 0xFF; dfu[c++] = (vid >> 0) & 0xFF; //idVendorLo - dfu[c++] = (vid >> 8) & 0xFF; + dfu[c++] = (vid >> 8) & 0xFF; dfu[c++] = 0x1A; //bcdDFULo dfu[c++] = 0x01; dfu[c++] = 'U'; //ucDfuSignature @@ -283,7 +283,7 @@ int main (int argc, char **argv) { dfu[c++] = (crc >> 0) & 0xFF; //dwCRC dfu[c++] = (crc >> 8) & 0xFF; dfu[c++] = (crc >> 16) & 0xFF; - dfu[c++] = (crc >> 24) & 0xFF; + dfu[c++] = (crc >> 24) & 0xFF; //write DFU to file outFile = fopen (out_fn, "wb"); @@ -304,13 +304,13 @@ int main (int argc, char **argv) { printf("%02x", (unsigned char)hash_buf[c]); } printf("\""); - + printf(",\"signature_pubkey\":\""); for(c=0; c<32; c++) { printf("%02x", (unsigned char)public_key[c]); } printf("\""); - + printf(",\"signature\":\""); for(c=0; c<64; c++) { printf("%02x", (unsigned char)signature[c]); @@ -324,7 +324,7 @@ int main (int argc, char **argv) { } printf("\""); } - printf(",\"crc32\":\"0x%08x\"", crc); + printf(",\"crc32\":\"0x%08x\"", crc); } #endif printf("}\r\n"); @@ -337,30 +337,30 @@ int main (int argc, char **argv) { } #ifdef ED25519_SUPPORT if (ed25519_secret) { - printf("SHA512: "); + printf("SHA512: "); for(c=0; c<64; c++) { printf("%02x", (unsigned char)hash_buf[c]); } - printf("\r\n"); - - printf("Signing PublicKey: "); + printf("\r\n"); + + printf("Signing PublicKey: "); for(c=0; c<32; c++) { printf("%02x", (unsigned char)public_key[c]); } printf("\r\n"); - - printf("Signature: "); + + printf("Signature: "); for(c=0; c<64; c++) { printf("%02x", (unsigned char)signature[c]); } - printf("\r\n"); + printf("\r\n"); if (ed25519_public_add) { - printf("Publisher PublicKey: "); + printf("Publisher PublicKey: "); for(c=0; c<32; c++) { printf("%02x", (unsigned char)public_key_publisher[c]); } - printf("\r\n"); + printf("\r\n"); } printf("CRC32 data: 0x%08x @0x%08x\r\n", crc, add_crc32+tar0_start_address); } @@ -368,7 +368,7 @@ int main (int argc, char **argv) { } printf("Done.\r\n"); - + return 0; } @@ -434,14 +434,14 @@ int check_checksum(unsigned char *inbuf, int len) { unsigned char *ihex2bin_buf(unsigned int *start_address, int *dst_len, FILE *inFile) { unsigned int lines = 0, total = 0, oneline_len, elar = 0, pos, cnt; unsigned char oneline [512], raw[256], start_set = 0, *dst = NULL; - + *dst_len = 1024*128; dst = malloc(*dst_len); //allocate 129kB of memory for bin data buffer if (dst == NULL) { - *dst_len = -2; + *dst_len = -2; return NULL; } - + *start_address = 0; while ( fgets (oneline, sizeof(oneline), inFile) != NULL ) { if (oneline[0] == ':') { //is valid record? @@ -468,7 +468,7 @@ unsigned char *ihex2bin_buf(unsigned int *start_address, int *dst_len, FILE *inF if (dst_new == NULL) { *dst_len = -2; //allocation error - exit free(dst); - return NULL; + return NULL; } else { *dst_len += 8192; dst = dst_new; //allocation succesed - copy new pointer @@ -497,7 +497,7 @@ unsigned char *ihex2bin_buf(unsigned int *start_address, int *dst_len, FILE *inF int ihex2bin(FILE * outFile, FILE *inFile) { unsigned int total = 0, oneline_len, elar = 0, start = 0xFFFFFFFF, pos, cnt; unsigned char oneline [512], raw[256]; - + while ( fgets (oneline , sizeof(oneline) , inFile) != NULL ) { //puts (oneline); if (oneline[0] == ':') { @@ -507,7 +507,7 @@ int ihex2bin(FILE * outFile, FILE *inFile) { if ((raw[0] == 2) && (raw[1] == 0) && (raw[2] == 0) && (raw[3] == 4)) { //:020000040803EF elar = (unsigned int)raw[4]<<24 | (unsigned int) raw[5]<<16; if (start == 0xFFFFFFFF) { - start = elar; + start = elar; } } else if ((raw[0] == 0) && (raw[1] == 0) && (raw[2] == 0) && (raw[3] == 1)) { //:00000001FF