forked from gicking/stm8gal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhexfile.c
1442 lines (1151 loc) · 49.2 KB
/
hexfile.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
\file hexfile.c
\author G. Icking-Konert
\date 2018-12-14
\version 0.2
\brief implementation of routines for HEX, S19 and table files
implementation of routines for importing and exporting Motorola S19 and Intel HEX files,
as well as plain ASCII tables.
(format descriptions under http://en.wikipedia.org/wiki/SREC_(file_format) or
http://www.keil.com/support/docs/1584.htm).
*/
// include files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdint.h>
#include <inttypes.h>
#include <ctype.h>
#include "hexfile.h"
#include "main.h"
#include "misc.h"
/**
\fn char *get_line(char **buf, char *line)
\param[in] buf pointer to read from (is updated)
\param[out] line pointer to line read (has to be large anough)
read line (until LF, CR, or EOF) from RAM buffer and advance buffer pointer.
memory for line has to be allocated externally
*/
char *get_line(char **buf, char *line) {
char *p = line;
// copy line
while ((**buf!=10) && (**buf!=13) && (**buf!=0)) {
*line = **buf;
line++;
(*buf)++;
}
// skip CR + LF in buffer
while ((**buf==10) || (**buf==13))
(*buf)++;
// terminate line
*line = '\0';
// check if data was copied
if (p == line)
return(NULL);
else
return(p);
} // get_line
/**
\fn void load_file(const char *filename, char *fileBuf, uint64_t *lenFileBuf, uint8_t verbose)
\param[in] filename name of file to read
\param[out] fileBuf memory buffer containing file content
\param[out] lenFileBuf size of data [B] read from file
\param[in] verbose verbosity level (0=MUTE, 1=SILENT, 2=INFORM, 3=CHATTY)
read file from file to memory buffer. Don't interpret (is done in separate routine)
*/
void load_file(const char *filename, char *fileBuf, uint64_t *lenFileBuf, uint8_t verbose) {
FILE *fp;
// strip path from filename for readability
#if defined(WIN32) || defined(WIN64)
const char *shortname = strrchr(filename, '\\');
#else
const char *shortname = strrchr(filename, '/');
#endif
if (!shortname)
shortname = filename;
else
shortname++;
// print message
if (verbose >= SILENT)
printf(" load '%s' ... ", shortname);
fflush(stdout);
// open file to read
if (!(fp = fopen(filename, "rb")))
Error("Failed to open file %s", filename);
// get filesize
fseek(fp, 0, SEEK_END);
(*lenFileBuf) = ftell(fp);
fseek(fp, 0, SEEK_SET);
// check file size vs. buffer
if ((*lenFileBuf) > LENFILEBUF)
Error("File %s exceeded buffer size (%ld vs %ld)", (*lenFileBuf), LENFILEBUF);
// init memory image to zero
memset(fileBuf, 0, LENFILEBUF * sizeof(*fileBuf));
// read file to buffer
fread(fileBuf, (*lenFileBuf), 1, fp);
// close file again
fclose(fp);
// print message
if ((verbose == SILENT) || (verbose == INFORM)){
printf("done\n");
}
else if (verbose == CHATTY) {
if ((*lenFileBuf)>1024*1024)
printf("done (%1.1fMB)\n", (float) (*lenFileBuf)/1024.0/1024.0);
else if ((*lenFileBuf)>1024)
printf("done (%1.1fkB)\n", (float) (*lenFileBuf)/1024.0);
else if ((*lenFileBuf)>0)
printf("done (%dB)\n", (int) (*lenFileBuf));
else
printf("done, no data read\n");
}
fflush(stdout);
} // load_file
/**
\fn void convert_s19(char *fileBuf, uint64_t lenFileBuf, uint16_t *imageBuf, uint8_t verbose)
\param[in] fileBuf memory buffer to read from
\param[in] lenFileBuf length of memory buffer
\param[out] imageBuf RAM image of file. HB!=0 indicates content
\param[in] verbose verbosity level (0=MUTE, 1=SILENT, 2=INFORM, 3=CHATTY)
convert memory buffer containing s19 hexfile to memory image. For description of
Motorola S19 file format see http://en.wikipedia.org/wiki/SREC_(file_format)
*/
void convert_s19(char *fileBuf, uint64_t lenFileBuf, uint16_t *imageBuf, uint8_t verbose) {
char line[1000], tmp[1000], *p;
uint64_t linecount, idx;
uint8_t type, len, chkRead, chkCalc;
uint64_t addr, addrStart, addrStop, numData;
int val, i;
// print message
if (verbose == INFORM)
printf(" convert S19 ... ");
else if (verbose == CHATTY)
printf(" convert Motorola S19 file ... ");
fflush(stdout);
//////
// import data to memory with syntax check
//////
p = fileBuf;
linecount = 0;
numData = 0;
addrStart = 0xFFFFFFFFFFFFFFFF;
addrStop = 0x0000000000000000;
while ((uint64_t) (p-fileBuf) < lenFileBuf) {
// get next line. On EOF terminate
if (!get_line(&p, line))
break;
// increase line counter
linecount++;
chkCalc = 0x00;
// check 1st char (must be 'S')
if (line[0] != 'S')
Error("Line %u in Motorola S-record file: line does not start with 'S'", linecount);
// record type
type = line[1]-48;
// skip if line contains no data, i.e. line doesn't start with S1, S2 or S3
if ((type != 1) && (type != 2) && (type != 3))
continue;
// record length (address + data + checksum)
sprintf(tmp,"0x00");
strncpy(tmp+2, line+2, 2);
sscanf(tmp, "%x", &val);
len = val;
chkCalc += val; // increase checksum
// address (S1=16bit, S2=24bit, S3=32bit)
addr = 0;
for (i=0; i<type+1; i++) {
sprintf(tmp,"0x00");
tmp[2] = line[4+(i*2)];
tmp[3] = line[5+(i*2)];
sscanf(tmp, "%x", &val);
addr *= (uint64_t) 256;
addr += (uint64_t) val;
chkCalc += (uint8_t) val;
}
// check for buffer overflow
if (addr > (uint64_t) (LENIMAGEBUF-1L))
Error("Line %u in Motorola S-record file: buffer address exceeded (%dMB vs %dMB)", linecount, (int) (addr/1024L/1024L), (int) (LENIMAGEBUF/1024L/1024L));
// read record data
idx=6+(type*2); // start at position 8, 10, or 12, depending on record type
len=len-1-(1+type); // substract chk and address length
for (i=0; i<len; i++) {
sprintf(tmp,"0x00");
strncpy(tmp+2, line+idx, 2); // get next 2 chars as string
sscanf(tmp, "%x", &val); // interpret as hex data
imageBuf[addr+i] = ((uint16_t) val) | 0xFF00; // store data byte in buffer and set high byte for "defined"
numData++; // increade byte counter
chkCalc += (uint8_t) val; // increase checksum
idx+=2; // advance 2 chars in line
}
// for printout store min/max address in file
if (addr < addrStart) addrStart = addr;
if (addr+len-1 > addrStop) addrStop = addr+len-1;
// checksum
sprintf(tmp,"0x00");
strncpy(tmp+2, line+idx, 2);
sscanf(tmp, "%x", &val);
chkRead = (uint8_t) val;
// assert checksum (0xFF xor (sum over all except record type)
chkCalc ^= 0xFF; // invert checksum
if (chkCalc != chkRead)
Error("Line %u in Motorola S-record file: checksum error (0x%02x vs. 0x%02x)", linecount, chkRead, chkCalc);
} // while !EOF
// print message
if (verbose == INFORM) {
printf("done\n");
}
else if (verbose == CHATTY) {
if (numData>1024*1024)
printf("done (%1.1fMB in 0x%" PRIx64 " - 0x%" PRIx64 ")\n", (float) numData/1024.0/1024.0, addrStart, addrStop);
else if (numData>1024)
printf("done (%1.1fkB in 0x%" PRIx64 " - 0x%" PRIx64 ")\n", (float) numData/1024.0, addrStart, addrStop);
else if (numData>0)
printf("done (%dB in 0x%" PRIx64 " - 0x%" PRIx64 ")\n", (int) numData, addrStart, addrStop);
else
printf("done, no data\n");
}
fflush(stdout);
} // convert_s19
/**
\fn void convert_ihx(char *fileBuf, uint64_t lenFileBuf, uint16_t *imageBuf, uint8_t verbose)
\param[in] fileBuf memory buffer to read from
\param[in] lenFileBuf length of memory buffer
\param[out] imageBuf RAM image of file. HB!=0 indicates content
\param[in] verbose verbosity level (0=MUTE, 1=SILENT, 2=INFORM, 3=CHATTY)
convert memory buffer containing intel hexfile to memory buffer. For description of
Intel hex file format see http://en.wikipedia.org/wiki/Intel_HEX
*/
void convert_ihx(char *fileBuf, uint64_t lenFileBuf, uint16_t *imageBuf, uint8_t verbose) {
char line[1000], tmp[1000], *p;
uint64_t linecount, idx;
uint8_t type, len, chkRead, chkCalc;
uint64_t addr, addrStart, addrStop, numData;
uint64_t addrOffset, addrJumpStart;
int val, i;
// avoid compiler warning (variable not yet used). See https://stackoverflow.com/questions/3599160/unused-parameter-warnings-in-c
(void) (addrJumpStart);
// print message
if (verbose == INFORM)
printf(" convert IHX ... ");
else if (verbose == CHATTY)
printf(" convert Intel HEX file ... ");
fflush(stdout);
//////
// import data to memory with syntax check
//////
p = fileBuf;
linecount = 0;
numData = 0;
addrStart = 0xFFFFFFFFFFFFFFFF;
addrStop = 0x0000000000000000;
addrOffset = 0x0000000000000000;
while ((uint64_t) (p-fileBuf) < lenFileBuf) {
// get next line. On EOF terminate
if (!get_line(&p, line))
break;
// increase line counter
linecount++;
chkCalc = 0x00;
// check 1st char (must be ':')
if (line[0] != ':')
Error("Line %u in Intel hex file: line does not start with ':'", linecount);
// record length (address + data + checksum)
sprintf(tmp,"0x00");
strncpy(tmp+2, line+1, 2);
sscanf(tmp, "%x", &val);
len = val;
chkCalc += len; // increase checksum
// 16b address
addr = 0;
sprintf(tmp,"0x0000");
strncpy(tmp+2, line+3, 4);
sscanf(tmp, "%x", &val);
chkCalc += (uint8_t) (val >> 8);
chkCalc += (uint8_t) val;
addr = (uint64_t) (val + addrOffset); // add offset for >64kB addresses
// record type
sprintf(tmp,"0x00");
strncpy(tmp+2, line+7, 2);
sscanf(tmp, "%x", &val);
type = val;
chkCalc += type; // increase checksum
// record contains data
if (type==0) {
// check for buffer overflow
if (addr > (uint64_t) (LENIMAGEBUF-1L))
Error("Line %u in Intel hex file: buffer size exceeded (%dMB vs %dMB)", linecount, (int) (addr/1024L/1024L), (int) (LENIMAGEBUF/1024L/1024L));
// for printout store min/max address in file
if (addr < addrStart) addrStart = addr;
if (addr > addrStop) addrStop = addr;
// get data
idx = 9; // start at index 9
for (i=0; i<len; i++) {
sprintf(tmp,"0x00");
strncpy(tmp+2, line+idx, 2); // get next 2 chars as string
sscanf(tmp, "%x", &val); // interpret as hex data
imageBuf[addr+i] = val | 0xFF00; // store data byte in buffer and set high byte for "defined"
numData++; // increade byte counter
chkCalc += val; // increase checksum
idx+=2; // advance 2 chars in line
}
} // type==0
// EOF indicator
else if (type==1)
continue;
// extended segment addresses not yet supported
else if (type==2)
Error("Line %u in Intel hex file: extended segment address type 2 not supported", linecount);
// start segment address (only relevant for 80x86 processor, ignore here)
else if (type==3)
continue;
// extended address (=upper 16b of address for following data records)
else if (type==4) {
idx = 13; // start at index 13
sprintf(tmp,"0x0000");
strncpy(tmp+2, line+9, 4); // get next 4 chars as string
sscanf(tmp, "%x", &val); // interpret as hex data
chkCalc += (uint8_t) (val >> 8);
chkCalc += (uint8_t) val;
addrOffset = ((uint64_t) val) << 16;
} // type==4
// start linear address records. Can be ignored, see http://www.keil.com/support/docs/1584/
else if (type==5)
continue;
// unsupported record type -> error
else
Error("Line %u in Intel hex file: unsupported type %d", linecount, type);
// checksum
sprintf(tmp,"0x00");
strncpy(tmp+2, line+idx, 2);
sscanf(tmp, "%x", &val);
chkRead = val;
// assert checksum (0xFF xor (sum over all except record type))
chkCalc = 255 - chkCalc + 1; // calculate 2-complement
if (chkCalc != chkRead)
Error("Line %u in Intel hex file: checksum error (read 0x%02x, calc 0x%02x)", linecount, chkRead, chkCalc);
} // while !EOF
// print message
if (verbose == INFORM) {
printf("done\n");
}
else if (verbose == CHATTY) {
if (numData>1024*1024)
printf("done (%1.1fMB in 0x%" PRIx64 " - 0x%" PRIx64 ")\n", (float) numData/1024.0/1024.0, addrStart, addrStop);
else if (numData>1024)
printf("done (%1.1fkB in 0x%" PRIx64 " - 0x%" PRIx64 ")\n", (float) numData/1024.0, addrStart, addrStop);
else if (numData>0)
printf("done (%dB in 0x%" PRIx64 " - 0x%" PRIx64 ")\n", (int) numData, addrStart, addrStop);
else
printf("done, no data\n");
}
fflush(stdout);
} // convert_ihx
/**
\fn void convert_txt(char *fileBuf, uint64_t lenFileBuf, uint16_t *imageBuf, uint8_t verbose)
\param[in] fileBuf memory buffer to read from
\param[in] lenFileBuf length of memory buffer
\param[out] imageBuf RAM image of file. HB!=0 indicates content
\param[in] verbose verbosity level (0=MUTE, 1=SILENT, 2=INFORM, 3=CHATTY)
convert memory buffer containing plain table (address / value) to memory buffer.
Address and value may be decimal (plain numberst) or hexadecimal (starting with '0x').
Lines starting with '#' are ignored. No syntax check is performed.
*/
void convert_txt(char *fileBuf, uint64_t lenFileBuf, uint16_t *imageBuf, uint8_t verbose) {
char line[1000], *p;
uint64_t linecount;
char sAddr[1000], sValue[1000];
uint64_t addr, addrStart, addrStop, numData;
int val, i;
// print message
if (verbose == INFORM)
printf(" convert table ... ");
else if (verbose == CHATTY)
printf(" convert ASCII table file ... ");
fflush(stdout);
//////
// import data to memory with syntax check
//////
p = fileBuf;
linecount = 0;
numData = 0;
addrStart = 0xFFFFFFFFFFFFFFFF;
addrStop = 0x0000000000000000;
while ((uint64_t) (p-fileBuf) < lenFileBuf) {
// get next line. On EOF terminate
if (!get_line(&p, line))
break;
// increase line counter
linecount++;
// if line starts with '#' ignore as comment
if (line[0] == '#')
continue;
// get address and value as string
sscanf(line, "%s %s", sAddr, sValue);
//////////
// extract address
//////////
// address string is in hex format (starts with '0x')
if ((sAddr[0] == '0') && ((sAddr[1] == 'x') || (sAddr[1] == 'X'))) {
// check for valid characters 0-9, A-F
for (i=2; i<strlen(sAddr); i++) {
if (!isxdigit(sAddr[i]))
Error("Line %u in table file: hex address '%s' contains invalid character ('%c')", linecount, sAddr, sAddr[i]);
}
// get address
sscanf(sAddr, "%" SCNx64, &addr);
} // address is in hex format
// address string is in decimal format
else {
// check for valid characters 0-9
for (i=0; i<strlen(sAddr); i++) {
if (!isdigit(sAddr[i]))
Error("Line %u in table file: dec address '%s' contains invalid character ('%c')", linecount, sAddr, sAddr[i]);
}
// get address
sscanf(sAddr, "%" SCNu64, &addr);
} // extract address
//////////
// extract value
//////////
// value string is in hex format (starts with '0x')
if ((sValue[0] == '0') && ((sValue[1] == 'x') || (sValue[1] == 'X'))) {
// check for valid characters 0-9, A-F
for (i=2; i<strlen(sValue); i++) {
if (!isxdigit(sValue[i]))
Error("Line %u in table file: hex value '%s' contains invalid character ('%c')", linecount, sValue, sValue[i]);
}
// get address
sscanf(sValue, "%x", &val);
} // address is in hex format
// address string is in decimal format
else {
// check for valid characters 0-9
for (i=0; i<strlen(sValue); i++) {
if (!isdigit(sValue[i]))
Error("Line %u in table file: dec value '%s' contains invalid character ('%c')", linecount, sValue, sValue[i]);
}
// get address
sscanf(sValue, "%d", &val);
} // extract value
// check for buffer overflow
if (addr > (uint64_t) (LENIMAGEBUF-1L))
Error("Line %u in table file: buffer size exceeded (%dMB vs %dMB)", linecount, (int) (addr/1024L/1024L), (int) (LENIMAGEBUF/1024L/1024L));
// for printout store min/max address in file
if (addr < addrStart) addrStart = addr;
if (addr > addrStop) addrStop = addr;
// store data byte in buffer and set high byte
imageBuf[addr] = (uint16_t) val | 0xFF00;
numData++;
} // while !EOF
// print message
if (verbose == INFORM) {
printf("done\n");
}
else if (verbose == CHATTY) {
if (numData>1024*1024)
printf("done (%1.1fMB in 0x%" PRIx64 " - 0x%" PRIx64 ")\n", (float) numData/1024.0/1024.0, addrStart, addrStop);
else if (numData>1024)
printf("done (%1.1fkB in 0x%" PRIx64 " - 0x%" PRIx64 ")\n", (float) numData/1024.0, addrStart, addrStop);
else if (numData>0)
printf("done (%dB in 0x%" PRIx64 " - 0x%" PRIx64 ")\n", (int) numData, addrStart, addrStop);
else
printf("done, no data\n");
}
fflush(stdout);
} // convert_txt
/**
\fn void convert_bin(char *fileBuf, uint64_t lenFileBuf, uint64_t addrStart, uint16_t *imageBuf, uint8_t verbose)
\param[in] fileBuf memory buffer to read from
\param[in] lenFileBuf length of memory buffer
\param[in] addrStart address offset for binary import
\param[out] imageBuf RAM image of file. HB!=0 indicates content
\param[in] verbose verbosity level (0=MUTE, 1=SILENT, 2=INFORM, 3=CHATTY)
convert memory buffer containing binary data to memory image. Binary data contains no absolute addresses,
just data. Therefor a starting address must also be provided.
*/
void convert_bin(char *fileBuf, uint64_t lenFileBuf, uint64_t addrStart, uint16_t *imageBuf, uint8_t verbose) {
uint64_t addrStop, numData;
uint64_t i;
// print message
if (verbose == INFORM)
printf(" convert binary ... ");
else if (verbose == CHATTY)
printf(" convert binary data ... ");
fflush(stdout);
// calculate number of bytes and last address
numData = lenFileBuf;
addrStop = addrStart + numData;
// check for buffer overflow
if (addrStop > (uint64_t) (LENIMAGEBUF-1L))
Error("Binary file conversion: buffer size exceeded (%dMB vs %dMB)", (int) (addrStop/1024L/1024L), (int) (LENIMAGEBUF/1024L/1024L));
// copy data and mark as set (HB=0xFF)
for (i=0; i<numData; i++) {
imageBuf[addrStart+i] = ((uint16_t) fileBuf[i]) | 0xFF00;
}
// print message
if (verbose == INFORM) {
printf("done\n");
}
else if (verbose == CHATTY) {
if (numData>1024*1024)
printf("done (%1.1fMB in 0x%" PRIx64 " - 0x%" PRIx64 ")\n", (float) numData/1024.0/1024.0, addrStart, addrStop);
else if (numData>1024)
printf("done (%1.1fkB in 0x%" PRIx64 " - 0x%" PRIx64 ")\n", (float) numData/1024.0, addrStart, addrStop);
else if (numData>0)
printf("done (%dB in 0x%" PRIx64 " - 0x%" PRIx64 ")\n", (int) numData, addrStart, addrStop);
else
printf("done, no data\n");
}
fflush(stdout);
} // convert_bin
/**
\fn void get_image_size(uint16_t *imageBuf, uint64_t scanStart, uint64_t scanStop, uint64_t *addrStart, uint64_t *addrStop, uint64_t *numData)
\param[in] imageBuf memory image containing data. HB!=0 indicates content
\param[in] scanStart start address for scan
\param[in] scanStop end address for scan
\param[out] addrStart first address containing data (HB!=0x00)
\param[out] addrStop last address containing data (HB!=0x00)
\param[out] numData number of data bytes in image (HB!=0x00)
Get fist and last address and number of bytes in memory image. Defined data is indicated by HB!=0x00
*/
void get_image_size(uint16_t *imageBuf, uint64_t scanStart, uint64_t scanStop, uint64_t *addrStart, uint64_t *addrStop, uint64_t *numData) {
uint64_t addr;
// simple checks of scan window
if (scanStart > scanStop)
Error("scan start address 0x%" PRIx64 " higher than end address 0x%" PRIx64, scanStart, scanStop);
if (scanStart > LENIMAGEBUF)
Error("scan start address 0x%" PRIx64 " exceeds buffer size 0x%" PRIx64, scanStart, LENIMAGEBUF);
if (scanStop > LENIMAGEBUF)
Error("scan end address 0x%" PRIx64 " exceeds buffer size 0x%" PRIx64, scanStop, LENIMAGEBUF);
// loop though image and check for defined data (HB!=0x00)
*addrStart = 0xFFFFFFFFFFFFFFFF;
*addrStop = 0x0000000000000000;
*numData = 0;
for (addr=scanStart; addr<=scanStop; addr++) {
// entry contains data (HB!=0x00)
if (imageBuf[addr] & 0xFF00) {
if (addr < *addrStart) *addrStart = addr;
if (addr > *addrStop) *addrStop = addr;
(*numData)++;
}
} // loop over image
} // get_image_size
/**
\fn void fill_image(uint16_t *imageBuf, uint64_t addrStart, uint64_t addrStop, uint8_t value, uint8_t verbose)
\param imageBuf memory image containing data. HB!=0 indicates content
\param[in] addrStart starting address of filling window
\param[in] addrStop topmost address of filling window
\param[in] value value to write
\param[in] verbose verbosity level (0=MUTE, 1=SILENT, 2=INFORM, 3=CHATTY)
Fill memory image in specified window with specified value and set status to "defined" (HB=0xFF)
*/
void fill_image(uint16_t *imageBuf, uint64_t addrStart, uint64_t addrStop, uint8_t value, uint8_t verbose) {
uint64_t addr, numFilled;
// print message
if (verbose == INFORM)
printf(" fill image ... ");
else if (verbose == CHATTY)
printf(" fill memory image ... ");
fflush(stdout);
// simple checks of scan window
if (addrStart > addrStop)
Error("start address 0x%" PRIx64 " higher than end address 0x%" PRIx64, addrStart, addrStop);
if (addrStart > (uint64_t) LENIMAGEBUF)
Error("start address 0x%" PRIx64 " exceeds buffer size 0x%" PRIx64, addrStart, LENIMAGEBUF);
if (addrStop > (uint64_t) LENIMAGEBUF)
Error("end address 0x%" PRIx64 " exceeds buffer size 0x%" PRIx64, addrStop, LENIMAGEBUF);
// loop over memory image and clear all data outside specified clipping window
numFilled = 0;
for (addr = addrStart; addr <= addrStop; addr++) {
numFilled++; // count filled bytes for output below
imageBuf[addr] = ((uint16_t) value) | 0xFF00; // HB=0x00 indicates data undefined, LB contains data
}
// print message
if (verbose == INFORM) {
printf("done\n");
}
else if (verbose == CHATTY) {
if (numFilled>1024*1024)
printf("done, filled %1.1fMB with 0x%02x within 0x%" PRIx64 " - 0x%" PRIx64 "\n", (float) numFilled/1024.0/1024.0, value, addrStart, addrStop);
else if (numFilled>1024)
printf("done, filled %1.1fkB with 0x%02x within 0x%" PRIx64 " - 0x%" PRIx64 "\n", (float) numFilled/1024.0, value, addrStart, addrStop);
else if (numFilled>0)
printf("done, filled %dB with 0x%02x within 0x%" PRIx64 " - 0x%" PRIx64 "\n", (int) numFilled, value, addrStart, addrStop);
else
printf("done, no data filled\n");
}
fflush(stdout);
} // fill_image
/**
\fn void clip_image(uint16_t *imageBuf, uint64_t addrStart, uint64_t addrStop, uint8_t verbose)
\param imageBuf memory image containing data. HB!=0 indicates content
\param[in] addrStart starting address of clipping window
\param[in] addrStop topmost address of clipping window
\param[in] verbose verbosity level (0=MUTE, 1=SILENT, 2=INFORM, 3=CHATTY)
Clip memory image to specified window, i.e. reset all data outside specified window to "undefined" (HB=0x00)
*/
void clip_image(uint16_t *imageBuf, uint64_t addrStart, uint64_t addrStop, uint8_t verbose) {
uint64_t addr, numCleared;
// print message
if (verbose == INFORM)
printf(" clip image ... ");
else if (verbose == CHATTY)
printf(" clip memory image ... ");
fflush(stdout);
// simple checks of scan window
if (addrStart > addrStop)
Error("start address 0x%" PRIx64 " higher than end address 0x%" PRIx64, addrStart, addrStop);
if (addrStart > (uint64_t) LENIMAGEBUF)
Error("start address 0x%" PRIx64 " exceeds buffer size 0x%" PRIx64, addrStart, LENIMAGEBUF);
if (addrStop > (uint64_t) LENIMAGEBUF)
Error("end address 0x%" PRIx64 " exceeds buffer size 0x%" PRIx64, addrStop, LENIMAGEBUF);
// loop over memory image and clear all data outside specified clipping window
numCleared = 0;
for (addr = 0; addr < (uint64_t)LENIMAGEBUF; addr++) {
if ((addr < addrStart) || (addr > addrStop)) {
if (imageBuf[addr] & 0xFF00)
numCleared++; // count deleted bytes for output below
imageBuf[addr] = 0x0000; // HB=0x00 indicates data undefined, LB contains data
}
}
// print message
if (verbose == INFORM) {
printf("done\n");
}
else if (verbose == CHATTY) {
if (numCleared>1024*1024)
printf("done, clipped %1.1fMB outside 0x%" PRIx64 " - 0x%" PRIx64 "\n", (float) numCleared/1024.0/1024.0, addrStart, addrStop);
else if (numCleared>1024)
printf("done, clipped %1.1fkB outside 0x%" PRIx64 " - 0x%" PRIx64 "\n", (float) numCleared/1024.0, addrStart, addrStop);
else if (numCleared>0)
printf("done, clipped %" PRId64 "B outside 0x%" PRIx64 " - 0x%" PRIx64 "\n", numCleared, addrStart, addrStop);
else
printf("done, no data cleared\n");
}
fflush(stdout);
} // clip_image
/**
\fn void cut_image(uint16_t *imageBuf, uint64_t addrStart, uint64_t addrStop, uint8_t verbose)
\param imageBuf memory image containing data. HB!=0 indicates content
\param[in] addrStart starting address of section to clear
\param[in] addrStop topmost address of section to clear
\param[in] verbose verbosity level (0=MUTE, 1=SILENT, 2=INFORM, 3=CHATTY)
Cut data range from memory image, i.e. reset all data inside specified window to "undefined" (HB=0x00)
*/
void cut_image(uint16_t *imageBuf, uint64_t addrStart, uint64_t addrStop, uint8_t verbose) {
uint64_t addr, numCleared;
// print message
if (verbose == INFORM)
printf(" clear image ... ");
else if (verbose == CHATTY)
printf(" clear memory image ... ");
fflush(stdout);
// simple checks of scan window
if (addrStart > addrStop)
Error("start address 0x%" PRIx64 " higher than end address 0x%" PRIx64, addrStart, addrStop);
if (addrStart > (uint64_t) LENIMAGEBUF)
Error("start address 0x%" PRIx64 " exceeds buffer size 0x%" PRIx64, addrStart, LENIMAGEBUF);
if (addrStop > (uint64_t) LENIMAGEBUF)
Error("end address 0x%" PRIx64 " exceeds buffer size 0x" PRIx64, addrStop, LENIMAGEBUF);
// loop over memory image and clear all data inside specified window
numCleared = 0;
for (addr=0; addr<(uint64_t)LENIMAGEBUF; addr++) {
if ((addr >= addrStart) && (addr <= addrStop)) {
if (imageBuf[addr] & 0xFF00)
numCleared++; // count deleted bytes for output below
imageBuf[addr] = 0x0000; // HB=0x00 indicates data undefined, LB contains data
}
}
// print message
if (verbose == INFORM) {
printf("done\n");
}
else if (verbose == CHATTY) {
if (numCleared>1024*1024)
printf("done, cut %1.1fMB within 0x%" PRIx64 " - 0x%" PRIx64 "\n", (float) numCleared/1024.0/1024.0, addrStart, addrStop);
else if (numCleared>1024)
printf("done, cut %1.1fkB within 0x%" PRIx64 " - 0x%" PRIx64 "\n", (float) numCleared/1024.0, addrStart, addrStop);
else if (numCleared>0)
printf("done, cut %" PRId64 "B within 0x%" PRIx64 " - 0x%" PRIx64 "\n", numCleared, addrStart, addrStop);
else
printf("done, no data cut\n");
}
fflush(stdout);
} // cut_image
/**
\fn void copy_image(uint16_t *imageBuf, uint64_t sourceStart, uint64_t sourceStop, uint64_t destinationStart, uint8_t verbose)
\param imageBuf memory image containing data. HB!=0 indicates content
\param[in] sourceStart starting address to copy from
\param[in] sourceStart last address to copy from
\param[in] destinationStart starting address to copy to
\param[in] verbose verbosity level (0=MUTE, 1=SILENT, 2=INFORM, 3=CHATTY)
Copy data section within image to new address. Data at old address is maintained (if sections don't overlap).
*/
void copy_image(uint16_t *imageBuf, uint64_t sourceStart, uint64_t sourceStop, uint64_t destinationStart, uint8_t verbose) {
uint64_t numCopied, i;
// print message
if (verbose == INFORM)
printf(" copy data ... ");
else if (verbose == CHATTY)
printf(" copy image data ... ");
fflush(stdout);
// simple checks of scan window
if (sourceStart > sourceStop)
Error("source start address 0x%" PRIx64 " higher than end address 0x%" PRIx64, sourceStart, sourceStop);
if (sourceStart > (uint64_t) LENIMAGEBUF)
Error("source start address 0x%" PRIx64 " exceeds buffer size 0x%" PRIx64, sourceStart, LENIMAGEBUF);
if (sourceStop > (uint64_t) LENIMAGEBUF)
Error("source end address 0x%" PRIx64 " exceeds buffer size 0x%" PRIx64, sourceStop, LENIMAGEBUF);
if (destinationStart > (uint64_t) LENIMAGEBUF)
Error("destination start address 0x%" PRIx64 " exceeds buffer size 0x%" PRIx64, destinationStart, LENIMAGEBUF);
if (destinationStart+(sourceStop-sourceStart+1) > (uint64_t) LENIMAGEBUF)
Error("destination end address 0x%" PRIx64 " exceeds buffer size 0x" PRIx64, destinationStart+(sourceStop-sourceStart+1), LENIMAGEBUF);
// get number of data to copy (HB!=0x00)
numCopied = 0;
for (i=sourceStart; i<=sourceStop; i++) {
if (imageBuf[i] & 0xFF00)
numCopied++;
}
// copy data within image
memcpy((void*) &(imageBuf[destinationStart]), (void*) &(imageBuf[sourceStart]), (sourceStop-sourceStart+1)*sizeof(*imageBuf));
// print message
if (verbose == INFORM) {
printf("done\n");
}
else if (verbose == CHATTY) {
if (numCopied>1024*1024)
printf("done, copied %1.1fMB from 0x%" PRIx64 " - 0x%" PRIx64 " to 0x%" PRIx64 "\n", (float) numCopied/1024.0/1024.0, sourceStart, sourceStop, destinationStart);
else if (numCopied>1024)
printf("done, copied %1.1fkB from 0x%" PRIx64 " - 0x%" PRIx64 " to 0x%" PRIx64 "\n", (float) numCopied/1024.0, sourceStart, sourceStop, destinationStart);
else if (numCopied>0)
printf("done, copied %" PRId64 "B from 0x%" PRIx64 " - 0x%" PRIx64 " to 0x%" PRIx64 "\n", numCopied, sourceStart, sourceStop, destinationStart);
else
printf("done, no data copied\n");
}
fflush(stdout);
} // copy_image
/**
\fn void move_image(uint16_t *imageBuf, uint64_t sourceStart, uint64_t sourceStop, uint64_t destinationStart, uint8_t verbose)
\param imageBuf memory image containing data. HB!=0 indicates content
\param[in] sourceStart starting address to move from
\param[in] sourceStart last address to move from
\param[in] destinationStart starting address to move to
\param[in] verbose verbosity level (0=MUTE, 1=SILENT, 2=INFORM, 3=CHATTY)
Move data section within image to new address. Data at old address is cleared.
*/
void move_image(uint16_t *imageBuf, uint64_t sourceStart, uint64_t sourceStop, uint64_t destinationStart, uint8_t verbose) {
uint64_t numMoved, i;
uint16_t *tmpImageBuf; // temporary buffer
// print message
if (verbose == INFORM)
printf(" move data ... ");
else if (verbose == CHATTY)
printf(" move image data ... ");
fflush(stdout);
// simple checks of scan window
if (sourceStart > sourceStop)
Error("source start address 0x%" PRIx64 " higher than end address 0x%" PRIx64, sourceStart, sourceStop);
if (sourceStart > (uint64_t) LENIMAGEBUF)
Error("source start address 0x%" PRIx64 " exceeds buffer size 0x%" PRIx64, sourceStart, LENIMAGEBUF);
if (sourceStop > (uint64_t) LENIMAGEBUF)
Error("source end address 0x%" PRIx64 " exceeds buffer size 0x%" PRIx64, sourceStop, LENIMAGEBUF);
if (destinationStart > (uint64_t) LENIMAGEBUF)
Error("destination start address 0x%" PRIx64 " exceeds buffer size 0x%" PRIx64, destinationStart, LENIMAGEBUF);
if (destinationStart+(sourceStop-sourceStart+1) > (uint64_t) LENIMAGEBUF)
Error("destination end address 0x%" PRIx64 " exceeds buffer size 0x" PRIx64, destinationStart+(sourceStop-sourceStart+1), LENIMAGEBUF);
// get number of data to move (HB!=0x00)
numMoved = 0;
for (i=sourceStart; i<=sourceStop; i++) {
if (imageBuf[i] & 0xFF00)
numMoved++;
}
// allocate temporary buffer (required for overlapping windows
if (!(tmpImageBuf = malloc(LENIMAGEBUF * sizeof(*tmpImageBuf))))
Error("Cannot allocate image buffer, try reducing LENIMAGEBUF");
// copy data from image to temporary buffer
memcpy((void*) &(tmpImageBuf[sourceStart]), (void*) &(imageBuf[sourceStart]), (sourceStop-sourceStart+1)*sizeof(*imageBuf));
// remove old data from image
cut_image(imageBuf, sourceStart, sourceStop, MUTE);
// copy data from temporary buffer to image
memcpy((void*) &(imageBuf[destinationStart]), (void*) &(tmpImageBuf[sourceStart]), (sourceStop-sourceStart+1)*sizeof(*imageBuf));
// release temporary buffer again
free(tmpImageBuf);
// print message
if (verbose == INFORM) {
printf("done\n");
}
else if (verbose == CHATTY) {
if (numMoved>1024*1024)
printf("done, moved %1.1fkB from 0x%" PRIx64 " - 0x%" PRIx64 " to 0x%" PRIx64 "\n", (float) numMoved/1024.0/1024.0, sourceStart, sourceStop, destinationStart);
else if (numMoved>1024)
printf("done, moved %1.1fkB from 0x%" PRIx64 " - 0x%" PRIx64 " to 0x%" PRIx64 "\n", (float) numMoved/1024.0, sourceStart, sourceStop, destinationStart);
else if (numMoved>0)
printf("done, moved %" PRId64 "B from 0x%" PRIx64 " - 0x%" PRIx64 " to 0x%" PRIx64 "\n", numMoved, sourceStart, sourceStop, destinationStart);
else
printf("done, no data moved\n");
}
fflush(stdout);