Remove tailing spaces

This commit is contained in:
Andrey Gusakov 2021-11-01 00:22:51 +03:00
parent ed82a06749
commit 7abc40af31
1 changed files with 61 additions and 61 deletions

122
hex2dfu.c
View File

@ -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); unsigned int crc32(unsigned int crc, const void *buf, size_t size);
//efab5b0739a834bac702aeb5cd08ffe227908faaae501f910e7e07d8d41fbb06 //efab5b0739a834bac702aeb5cd08ffe227908faaae501f910e7e07d8d41fbb06
int main (int argc, char **argv) { int main (int argc, char **argv) {
int i, c, vid =0x0483, pid = 0xdf11, ver = 0xffff; int i, c, vid =0x0483, pid = 0xdf11, ver = 0xffff;
char *tar0_lab = NULL, *out_fn = NULL; char *tar0_lab = NULL, *out_fn = NULL;
@ -39,28 +39,28 @@ int main (int argc, char **argv) {
unsigned char json_output = 0; unsigned char json_output = 0;
char *in_hex[MAX_IN_HEX]; char *in_hex[MAX_IN_HEX];
int image_cnt = 0; int image_cnt = 0;
#ifdef ED25519_SUPPORT #ifdef ED25519_SUPPORT
unsigned char hash_buf[64]; unsigned char hash_buf[64];
sha512_context hash; sha512_context hash;
unsigned char *ed25519_secret = NULL; unsigned char *ed25519_secret = NULL;
unsigned char *ed25519_public = NULL, ed25519_public_add = 0; 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 #endif
unsigned int tar_start_address[MAX_IN_HEX]; unsigned int tar_start_address[MAX_IN_HEX];
int tar_len[MAX_IN_HEX]; int tar_len[MAX_IN_HEX];
unsigned char *tar_buf[MAX_IN_HEX]; unsigned char *tar_buf[MAX_IN_HEX];
int total_len = 0; int total_len = 0;
unsigned char *dfu; unsigned char *dfu;
int dfu_len; int dfu_len;
unsigned int crc = 0, tmp, add_crc32 = 0; unsigned int crc = 0, tmp, add_crc32 = 0;
opterr = 0; opterr = 0;
while ((c = getopt (argc, argv, "hv:p:d:i:l:o:c:S:P:eJ")) != -1) { while ((c = getopt (argc, argv, "hv:p:d:i:l:o:c:S:P:eJ")) != -1) {
switch (c) { switch (c) {
case 'J': case 'J':
json_output = 1; json_output = 1;
break; break;
case 'i': //target input file names case 'i': //target input file names
@ -85,26 +85,26 @@ int main (int argc, char **argv) {
case 'S': //ED25519 secret (signing key), hex case 'S': //ED25519 secret (signing key), hex
#ifndef ED25519_SUPPORT #ifndef ED25519_SUPPORT
fprintf (stderr, "Code signing not supported!\n"); fprintf (stderr, "Code signing not supported!\n");
return 1; return 1;
#else #else
ed25519_secret = optarg; ed25519_secret = optarg;
#endif #endif
break; break;
case 'P': //ED25519 publisher public, hex case 'P': //ED25519 publisher public, hex
#ifndef ED25519_SUPPORT #ifndef ED25519_SUPPORT
fprintf (stderr, "Code signing not supported!\n"); fprintf (stderr, "Code signing not supported!\n");
return 1; return 1;
#else #else
ed25519_public = optarg; ed25519_public = optarg;
#endif #endif
break; break;
case 'e': case 'e':
#ifndef ED25519_SUPPORT #ifndef ED25519_SUPPORT
fprintf (stderr, "Code signing not supported!\n"); fprintf (stderr, "Code signing not supported!\n");
return 1; return 1;
#else #else
ed25519_public_add = 1; ed25519_public_add = 1;
#endif #endif
break; break;
case 'o': //output file name case 'o': //output file name
out_fn = optarg; out_fn = optarg;
@ -119,7 +119,7 @@ int main (int argc, char **argv) {
break; break;
} }
} }
if (image_cnt == 0) { if (image_cnt == 0) {
perror ("No input file(s) specifed.\n"); perror ("No input file(s) specifed.\n");
return 0; return 0;
@ -130,16 +130,16 @@ int main (int argc, char **argv) {
return 0; return 0;
} }
#ifdef ED25519_SUPPORT #ifdef ED25519_SUPPORT
if (ed25519_secret) { if (ed25519_secret) {
c = hex2bin(ed25519_secret, ed25519_secret, strlen(ed25519_secret)); c = hex2bin(ed25519_secret, ed25519_secret, strlen(ed25519_secret));
if (c != 32) { if (c != 32) {
perror ("ED25519 'secret' have to be 32bytes long.\n"); perror ("ED25519 'secret' have to be 32bytes long.\n");
return 0; return 0;
} }
ed25519_create_keypair(public_key, private_key, ed25519_secret); ed25519_create_keypair(public_key, private_key, ed25519_secret);
if (ed25519_public) { if (ed25519_public) {
c = hex2bin(public_key_publisher, ed25519_public, strlen(ed25519_public)); c = hex2bin(public_key_publisher, ed25519_public, strlen(ed25519_public));
if (c != 32) { if (c != 32) {
@ -148,12 +148,12 @@ int main (int argc, char **argv) {
} }
} else { } else {
memmove(public_key_publisher, public_key, 32); memmove(public_key_publisher, public_key, 32);
} }
} }
#endif #endif
for (i = 0; i < image_cnt; i++) { for (i = 0; i < image_cnt; i++) {
inFile = fopen(in_hex[i], "r"); inFile = fopen(in_hex[i], "r");
if (inFile == NULL) { if (inFile == NULL) {
fprintf(stderr, "Failed to open %s\n", in_hex[i]); fprintf(stderr, "Failed to open %s\n", in_hex[i]);
return 0; 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_len[i] - 256))) { //-c request CRC32 placement at given address
add_crc32 -= tar_start_address[i]; 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 + 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 + 5] = tar_len[i]>>8 & 0xFF;
tar_buf[i][add_crc32 + 6] = tar_len[i]>>16 & 0xFF; tar_buf[i][add_crc32 + 6] = tar_len[i]>>16 & 0xFF;
tar_buf[i][add_crc32 + 7] = tar_len[i]>>24 & 0xFF; tar_buf[i][add_crc32 + 7] = tar_len[i]>>24 & 0xFF;
#ifdef ED25519_SUPPORT #ifdef ED25519_SUPPORT
if (ed25519_secret) { 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);
sha512_update(&hash, tar_buf[i] + add_crc32 + 256, tar_len[i] - (add_crc32 + 256)); 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); 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, signature, 64);
memmove(tar_buf[i] + add_crc32 + 0x10 + 64, public_key, 32); memmove(tar_buf[i] + add_crc32 + 0x10 + 64, public_key, 32);
if (ed25519_public_add) { if (ed25519_public_add) {
memmove(tar_buf[i] + add_crc32 + 0x10 + 64 + 32, public_key_publisher, 32); 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(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 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 + 0] = crc>>0 & 0xFF; //CRC placement (little endian)
tar_buf[i][add_crc32 + 1] = crc>>8 & 0xFF; tar_buf[i][add_crc32 + 1] = crc>>8 & 0xFF;
tar_buf[i][add_crc32 + 2] = crc>>16 & 0xFF; tar_buf[i][add_crc32 + 2] = crc>>16 & 0xFF;
tar_buf[i][add_crc32 + 3] = crc>>24 & 0xFF; tar_buf[i][add_crc32 + 3] = crc>>24 & 0xFF;
} }
} }
@ -216,13 +216,13 @@ int main (int argc, char **argv) {
//DFU prefix //DFU prefix
memmove(dfu, "DfuSe", 5); //szSignature memmove(dfu, "DfuSe", 5); //szSignature
dfu[5] = 0x01; //bVersion dfu[5] = 0x01; //bVersion
tmp = dfu_len - 0x10; tmp = dfu_len - 0x10;
dfu[6] = (unsigned char)(tmp & 0xFF); //DFUImageSize (except Prefix!) dfu[6] = (unsigned char)(tmp & 0xFF); //DFUImageSize (except Prefix!)
dfu[7] = (unsigned char)(tmp>>8 & 0xFF); dfu[7] = (unsigned char)(tmp>>8 & 0xFF);
dfu[8] = (unsigned char)(tmp>>16 & 0xFF); dfu[8] = (unsigned char)(tmp>>16 & 0xFF);
dfu[9] = (unsigned char)(tmp>>24 & 0xFF); dfu[9] = (unsigned char)(tmp>>24 & 0xFF);
dfu[10] = 1; //bTargets dfu[10] = 1; //bTargets
//DFU Image //DFU Image
//Target prefix //Target prefix
c = 11; 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 memmove(dfu + c + 4, tar0_lab, (strlen(tar0_lab) > 254) ? 254 : strlen(tar0_lab)); //szTargetName
} else { } else {
dfu[c] = 0x01; //place default target name 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; c += 259;
tmp = (8 * image_cnt) + total_len; //ImageElement length (8+bin_data) 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] >> 0) & 0xFF; //dwElementAddress
dfu[c++] = (tar_start_address[i] >> 8) & 0xFF; dfu[c++] = (tar_start_address[i] >> 8) & 0xFF;
dfu[c++] = (tar_start_address[i] >> 16) & 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] >> 0) & 0xFF; //dwElementSize
dfu[c++] = (tar_len[i] >> 8) & 0xFF; dfu[c++] = (tar_len[i] >> 8) & 0xFF;
dfu[c++] = (tar_len[i] >> 16) & 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++] = (ver >> 8) & 0xFF;
dfu[c++] = (pid >> 0) & 0xFF; //idProductLo 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 >> 0) & 0xFF; //idVendorLo
dfu[c++] = (vid >> 8) & 0xFF; dfu[c++] = (vid >> 8) & 0xFF;
dfu[c++] = 0x1A; //bcdDFULo dfu[c++] = 0x1A; //bcdDFULo
dfu[c++] = 0x01; dfu[c++] = 0x01;
dfu[c++] = 'U'; //ucDfuSignature dfu[c++] = 'U'; //ucDfuSignature
@ -283,7 +283,7 @@ int main (int argc, char **argv) {
dfu[c++] = (crc >> 0) & 0xFF; //dwCRC dfu[c++] = (crc >> 0) & 0xFF; //dwCRC
dfu[c++] = (crc >> 8) & 0xFF; dfu[c++] = (crc >> 8) & 0xFF;
dfu[c++] = (crc >> 16) & 0xFF; dfu[c++] = (crc >> 16) & 0xFF;
dfu[c++] = (crc >> 24) & 0xFF; dfu[c++] = (crc >> 24) & 0xFF;
//write DFU to file //write DFU to file
outFile = fopen (out_fn, "wb"); outFile = fopen (out_fn, "wb");
@ -304,13 +304,13 @@ int main (int argc, char **argv) {
printf("%02x", (unsigned char)hash_buf[c]); printf("%02x", (unsigned char)hash_buf[c]);
} }
printf("\""); printf("\"");
printf(",\"signature_pubkey\":\""); printf(",\"signature_pubkey\":\"");
for(c=0; c<32; c++) { for(c=0; c<32; c++) {
printf("%02x", (unsigned char)public_key[c]); printf("%02x", (unsigned char)public_key[c]);
} }
printf("\""); printf("\"");
printf(",\"signature\":\""); printf(",\"signature\":\"");
for(c=0; c<64; c++) { for(c=0; c<64; c++) {
printf("%02x", (unsigned char)signature[c]); printf("%02x", (unsigned char)signature[c]);
@ -324,7 +324,7 @@ int main (int argc, char **argv) {
} }
printf("\""); printf("\"");
} }
printf(",\"crc32\":\"0x%08x\"", crc); printf(",\"crc32\":\"0x%08x\"", crc);
} }
#endif #endif
printf("}\r\n"); printf("}\r\n");
@ -337,30 +337,30 @@ int main (int argc, char **argv) {
} }
#ifdef ED25519_SUPPORT #ifdef ED25519_SUPPORT
if (ed25519_secret) { if (ed25519_secret) {
printf("SHA512: "); printf("SHA512: ");
for(c=0; c<64; c++) { for(c=0; c<64; c++) {
printf("%02x", (unsigned char)hash_buf[c]); printf("%02x", (unsigned char)hash_buf[c]);
} }
printf("\r\n"); printf("\r\n");
printf("Signing PublicKey: "); printf("Signing PublicKey: ");
for(c=0; c<32; c++) { for(c=0; c<32; c++) {
printf("%02x", (unsigned char)public_key[c]); printf("%02x", (unsigned char)public_key[c]);
} }
printf("\r\n"); printf("\r\n");
printf("Signature: "); printf("Signature: ");
for(c=0; c<64; c++) { for(c=0; c<64; c++) {
printf("%02x", (unsigned char)signature[c]); printf("%02x", (unsigned char)signature[c]);
} }
printf("\r\n"); printf("\r\n");
if (ed25519_public_add) { if (ed25519_public_add) {
printf("Publisher PublicKey: "); printf("Publisher PublicKey: ");
for(c=0; c<32; c++) { for(c=0; c<32; c++) {
printf("%02x", (unsigned char)public_key_publisher[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); 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"); printf("Done.\r\n");
return 0; 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 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 int lines = 0, total = 0, oneline_len, elar = 0, pos, cnt;
unsigned char oneline [512], raw[256], start_set = 0, *dst = NULL; unsigned char oneline [512], raw[256], start_set = 0, *dst = NULL;
*dst_len = 1024*128; *dst_len = 1024*128;
dst = malloc(*dst_len); //allocate 129kB of memory for bin data buffer dst = malloc(*dst_len); //allocate 129kB of memory for bin data buffer
if (dst == NULL) { if (dst == NULL) {
*dst_len = -2; *dst_len = -2;
return NULL; return NULL;
} }
*start_address = 0; *start_address = 0;
while ( fgets (oneline, sizeof(oneline), inFile) != NULL ) { while ( fgets (oneline, sizeof(oneline), inFile) != NULL ) {
if (oneline[0] == ':') { //is valid record? 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) { if (dst_new == NULL) {
*dst_len = -2; //allocation error - exit *dst_len = -2; //allocation error - exit
free(dst); free(dst);
return NULL; return NULL;
} else { } else {
*dst_len += 8192; *dst_len += 8192;
dst = dst_new; //allocation succesed - copy new pointer 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) { int ihex2bin(FILE * outFile, FILE *inFile) {
unsigned int total = 0, oneline_len, elar = 0, start = 0xFFFFFFFF, pos, cnt; unsigned int total = 0, oneline_len, elar = 0, start = 0xFFFFFFFF, pos, cnt;
unsigned char oneline [512], raw[256]; unsigned char oneline [512], raw[256];
while ( fgets (oneline , sizeof(oneline) , inFile) != NULL ) { while ( fgets (oneline , sizeof(oneline) , inFile) != NULL ) {
//puts (oneline); //puts (oneline);
if (oneline[0] == ':') { 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 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; elar = (unsigned int)raw[4]<<24 | (unsigned int) raw[5]<<16;
if (start == 0xFFFFFFFF) { if (start == 0xFFFFFFFF) {
start = elar; start = elar;
} }
} else } else
if ((raw[0] == 0) && (raw[1] == 0) && (raw[2] == 0) && (raw[3] == 1)) { //:00000001FF if ((raw[0] == 0) && (raw[1] == 0) && (raw[2] == 0) && (raw[3] == 1)) { //:00000001FF