-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex2carray
executable file
·118 lines (103 loc) · 3.11 KB
/
hex2carray
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
#!/bin/perl
use strict;
use warnings;
use diagnostics;
##
# Script to parse an Intel Hex file and convert it to an array of bytes
# (plus some other variables).
#
# !!!!! BIG FAT WARNING !!!!!
#
# This will not work on noncontiguous data. It doesn't even check. It assumes
# the first data address is the start and then stuffs all the data into an array,
# in order, keeping count of how many bytes there are. That's it.
#
# !!!!! END BIG FAT WARNING !!!!!
##
# Parse a hex line and return the string to be written to the file.
#
# @param lineno line number
# @param line line to be parsed
# @param length_total the current total length
#
# @return the current length
# @return a string to be printed to the output file.
sub parse_line($$$)
{
my ($lineno, $line, $length_total) = @_;
my $output;
my @chars = split('', $line);
if ($chars[0] ne ":") {
print("Error: Line ${lineno} does not start with :\n");
}
else {
# We need the length as a number to correctly parse the data.
my $length = hex("0x${chars[1]}${chars[2]}");
$length_total += $length;
# Only print address on for first line, and we declare our data array.
if ($lineno == 1) {
$output .= "const uint16_t firmwareStartAddress = 0x${chars[3]}${chars[4]}${chars[5]}${chars[6]};\n";
$output .= "const byte firmwareData[] = {\n";
}
# We skip the next 2 characters (type)
if ($length > 0) {
# proper indenting.
$output .= " ";
for (my $data_index = 0; $data_index < $length; ++$data_index) {
$output .= "0x${chars[$data_index*2+9]}${chars[$data_index*2+10]},";
}
}
else {
# last line, end the array.
$output .= " };";
}
# add a newline at the end.
$output .= "\n";
}
return ($length_total, $output);
}
##
# Print usage
#
sub print_usage()
{
print("Usage:\n");
print(" ${0} <input> <output>\n");
}
##
# C programmer writing perl compatibility shim
sub main($@)
{
my ($argc, @argv) = @_;
if ($argc != 2) {
print("Error - incorrect number of arguments.\n");
print_usage();
}
else {
my $infile = shift(@ARGV);
my $outfile = shift(@ARGV);
if (!defined(open(INFH, "<", $infile))) {
printf("Error opening file ${infile}: $!\n");
}
else {
if (!defined(open(OUTFH, ">", $outfile))) {
printf("Error opening file ${outfile}: $!\n");
close(INFH);
}
else {
my $lineno = 0;
my $length_total = 0;
my $print_lines;
while(my $line = <INFH>) {
++$lineno;
($length_total, $print_lines) = parse_line($lineno, $line, $length_total);
print(OUTFH $print_lines);
}
print(OUTFH "const size_t firmwareLengthBytes = ${length_total};\n");
close(OUTFH);
close(INFH);
}
}
}
}
main(scalar(@ARGV), @ARGV);