Skip to content

Commit 59890f7

Browse files
dwmw2David Woodhouse
authored andcommitted
ihex: Add support for long records to ihex2fw.c
Some drivers could do with using records like Intel HEX, but with each record being larger than 256 bytes. This has been possible in the binary representation (struct ihex_binrec) in the kernel since the beginning -- at least of the the current version of history. But we haven't been able to represent that in the .HEX files which get converted to .fw files. This adds a '-w' option to ihex2fw to make it interpret the first _two_ bytes of each line as the record length, instead of only one byte. And adds makefile rules for %.H16->%.fw which use that. Signed-off-by: David Woodhouse <dwmw2@infradead.org>
1 parent 8bd6b22 commit 59890f7

2 files changed

Lines changed: 47 additions & 19 deletions

File tree

‎firmware/Makefile‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ quiet_cmd_ihex = IHEX $@
3535
quiet_cmd_ihex2fw = IHEX2FW $@
3636
cmd_ihex2fw = $(objtree)/$(obj)/ihex2fw $< $@
3737

38+
quiet_cmd_h16tofw = H16TOFW $@
39+
cmd_h16tofw = $(objtree)/$(obj)/ihex2fw -w $< $@
40+
3841
quiet_cmd_fwbin = MK_FW $@
3942
cmd_fwbin = FWNAME="$(patsubst firmware/%.gen.S,%,$@)"; \
4043
FWSTR="$(subst /,_,$(subst .,_,$(subst -,_,$(patsubst \
@@ -99,6 +102,10 @@ $(obj)/%: $(obj)/%.ihex | $(objtree)/$(obj)/$$(dir %)
99102
$(obj)/%.fw: $(obj)/%.HEX $(obj)/ihex2fw | $(objtree)/$(obj)/$$(dir %)
100103
$(call cmd,ihex2fw)
101104

105+
# .H16 is our own modified form of Intel HEX, with 16-bit length for records.
106+
$(obj)/%.fw: $(obj)/%.H16 $(obj)/ihex2fw | $(objtree)/$(obj)/$$(dir %)
107+
$(call cmd,h16tofw)
108+
102109
$(firmware-dirs):
103110
$(call cmd,mkdir)
104111

‎firmware/ihex2fw.c‎

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
#include <string.h>
2121
#include <unistd.h>
2222
#include <stdlib.h>
23+
#define _GNU_SOURCE
24+
#include <getopt.h>
25+
2326

2427
struct ihex_binrec {
2528
struct ihex_binrec *next; /* not part of the real data structure */
@@ -51,34 +54,49 @@ static void file_record(struct ihex_binrec *record);
5154
static int output_records(int outfd);
5255

5356
static int sort_records = 0;
57+
static int wide_records = 0;
58+
59+
int usage(void)
60+
{
61+
fprintf(stderr, "ihex2fw: Convert ihex files into binary "
62+
"representation for use by Linux kernel\n");
63+
fprintf(stderr, "usage: ihex2fw [<options>] <src.HEX> <dst.fw>\n");
64+
fprintf(stderr, " -w: wide records (16-bit length)\n");
65+
fprintf(stderr, " -s: sort records by address\n");
66+
return 1;
67+
}
5468

5569
int main(int argc, char **argv)
5670
{
5771
int infd, outfd;
5872
struct stat st;
5973
uint8_t *data;
74+
int opt;
6075

61-
if (argc == 4 && !strcmp(argv[1], "-s")) {
62-
sort_records = 1;
63-
argc--;
64-
argv++;
65-
}
66-
if (argc != 3) {
67-
usage:
68-
fprintf(stderr, "ihex2fw: Convert ihex files into binary "
69-
"representation for use by Linux kernel\n");
70-
fprintf(stderr, "usage: ihex2fw [-s] <src.HEX> <dst.fw>\n");
71-
fprintf(stderr, " -s: sort records by address\n");
72-
return 1;
76+
while ((opt = getopt(argc, argv, "ws")) != -1) {
77+
switch (opt) {
78+
case 'w':
79+
wide_records = 1;
80+
break;
81+
case 's':
82+
sort_records = 1;
83+
break;
84+
default:
85+
return usage();
86+
}
7387
}
74-
if (!strcmp(argv[1], "-"))
88+
89+
if (optind + 2 != argc)
90+
return usage();
91+
92+
if (!strcmp(argv[optind], "-"))
7593
infd = 0;
7694
else
77-
infd = open(argv[1], O_RDONLY);
95+
infd = open(argv[optind], O_RDONLY);
7896
if (infd == -1) {
7997
fprintf(stderr, "Failed to open source file: %s",
8098
strerror(errno));
81-
goto usage;
99+
return usage();
82100
}
83101
if (fstat(infd, &st)) {
84102
perror("stat");
@@ -90,14 +108,14 @@ int main(int argc, char **argv)
90108
return 1;
91109
}
92110

93-
if (!strcmp(argv[2], "-"))
111+
if (!strcmp(argv[optind+1], "-"))
94112
outfd = 1;
95113
else
96-
outfd = open(argv[2], O_TRUNC|O_CREAT|O_WRONLY, 0644);
114+
outfd = open(argv[optind+1], O_TRUNC|O_CREAT|O_WRONLY, 0644);
97115
if (outfd == -1) {
98116
fprintf(stderr, "Failed to open destination file: %s",
99117
strerror(errno));
100-
goto usage;
118+
return usage();
101119
}
102120
if (process_ihex(data, st.st_size))
103121
return 1;
@@ -130,7 +148,10 @@ static int process_ihex(uint8_t *data, ssize_t size)
130148
}
131149

132150
len = hex(data + i, &crc); i += 2;
133-
151+
if (wide_records) {
152+
len <<= 8;
153+
len += hex(data + i, &crc); i += 2;
154+
}
134155
record = malloc((sizeof (*record) + len + 3) & ~3);
135156
if (!record) {
136157
fprintf(stderr, "out of memory for records\n");

0 commit comments

Comments
 (0)