From ed82a0674947395ff7f4765a81d50cd81a0dfc70 Mon Sep 17 00:00:00 2001 From: Andrey Gusakov Date: Mon, 1 Nov 2021 00:20:30 +0300 Subject: [PATCH] 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;