OpenRTX/scripts/bin2c.awk
Niccolò Izzo 67a45cb26d Automatic sa8x8-fw flashing and patching
Now the 1.4.1 release of sa8x8-fw is downloaded at every build of the
ttwr21 target, it is converted from s37 to bin to c header file and is
included in the OpenRTX firmware image.
OpenRTX, at runtime detects the band of the radio and patches the
firmware with the appropriate identification string.

TG-553
2025-10-16 19:17:35 +02:00

70 lines
2.3 KiB
Awk

BEGIN {
BINMODE = "r"
infile = ARGV[1]
ARGV[1] = ""
if(length(infile)==0) {
print "\n" \
"bin2c - (C) 2026 by Niccolò Izzo\n" \
" License: WTFPL (wikipedia.org/wiki/WTFPL)\n" \
"\n" \
"usage: gawk -b -f this_file input_binary_file\n" \
"\n" \
"Convert the binary data of a file into a hex form.\n"
exit(3)
}
####################################################################################################
# From "The GNU Awk User's Guide" paragraph: "Translating Between Characters and Numbers" (adapted)
####################################################################################################
for(i=0; i<256; i++)
_ord_[sprintf("%c", i)] = i
RS="a sequence that is SURELY not found in any input file, \x00 not even in this file"
getline < infile # read the whole input file into $0
len = length($0)
bytes_x_line = 12
outfile = infile
gsub(getext(infile), ".h", outfile)
arr_name = basename(infile)
gsub("-", "_", arr_name)
# Print header
printf("unsigned char %s[] = {\n", arr_name) > outfile
for(i=0; i<len; i+=bytes_x_line) {
printf(" ") > outfile
# printf("0x%08x ", i)
for(j=i; j<i+bytes_x_line; j++) {
if(j==len)
break
# +1 is needed because the 1st char in a string is at position 1
printf(" 0x%02x,", _ord_[substr($0, j+1, 1)]) > outfile
# _ord_(char) converts a character to its ASCII code
# same as 'ord(char)' in Python, or Asc(c) in Excel/VBA.
}
print "" > outfile
}
printf("};\n\n") > outfile
printf("unsigned int %s_len = %d;\n", arr_name, len) > outfile
# Find offset of character to patch (UHF -> VHF)
patch_offset = index($0, "SA868S-UHF") + 6
printf("unsigned int %s_patch_offset = %d;\n", arr_name, patch_offset) > outfile
}
# Extract basename of file
function basename(filename, a) {
# Get file name
split(filename, a, "/")
filename = a[length(a)]
# Get without extension
split(filename, a, ".")
return a[1]
}
# Extract file extension
function getext(filename, a) {
split(filename, a, ".")
return "." a[length(a)]
}