mirror of https://github.com/rusefi/hex2dfu.git
Merge pull request #1 from dron0gus/support-multi-input
Support multi input
This commit is contained in:
commit
357c04c780
186
hex2dfu.c
186
hex2dfu.c
|
@ -2,8 +2,11 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define MAX_IN_HEX 16
|
||||
|
||||
#define TARGET_NAME_ENCEDO "EncedoKey"
|
||||
// uncomment to add support for digital code signature using ED25519
|
||||
//#define ED25519_SUPPORT
|
||||
|
@ -31,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];
|
||||
|
@ -43,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;
|
||||
|
@ -57,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;
|
||||
|
@ -113,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;
|
||||
}
|
||||
|
||||
|
@ -145,45 +152,68 @@ 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);
|
||||
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_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(tar0_buf+add_crc32+0x10, signature, 64);
|
||||
memmove(tar0_buf+add_crc32+0x10+64, public_key, 32);
|
||||
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(tar0_buf+add_crc32+0x10+64+32, public_key_publisher, 32);
|
||||
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;
|
||||
//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) {
|
||||
//DFU Suffix
|
||||
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;
|
||||
|
@ -193,47 +223,56 @@ int main (int argc, char **argv) {
|
|||
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
|
||||
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;
|
||||
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;
|
||||
|
||||
//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[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 & 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++] = (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
|
||||
|
@ -241,11 +280,10 @@ int main (int argc, char **argv) {
|
|||
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;
|
||||
|
||||
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");
|
||||
|
@ -255,9 +293,10 @@ int main (int argc, char **argv) {
|
|||
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);
|
||||
/* 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\":\"");
|
||||
|
@ -288,11 +327,14 @@ int main (int argc, char **argv) {
|
|||
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);
|
||||
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: ");
|
||||
|
@ -323,12 +365,9 @@ int main (int argc, char **argv) {
|
|||
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");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -352,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;
|
||||
|
||||
|
|
Loading…
Reference in New Issue