diff options
| author | David Woodhouse <dwmw2@infradead.org> | 2008-05-31 08:07:18 -0400 |
|---|---|---|
| committer | David Woodhouse <David.Woodhouse@intel.com> | 2008-07-10 09:47:41 -0400 |
| commit | 8bd6b2229bf98761465020467ec33547d05bff46 (patch) | |
| tree | 7254aa39f6cede847ec67377f17cf58a04f02c7e /firmware | |
| parent | f1485f3deb89e6ae10c4d34662ec9e692855ab5d (diff) | |
ihex: add ihex2fw tool for converting HEX files into firmware images
Not the straight conversion to binary which objcopy can do for us, but
actually representing each record with its original {addr, length},
because some drivers need that information preserved.
Fix up 'firmware_install' to be able to build $(hostprogs-y) too.
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
Diffstat (limited to 'firmware')
| -rw-r--r-- | firmware/Makefile | 14 | ||||
| -rw-r--r-- | firmware/ihex2fw.c | 247 |
2 files changed, 261 insertions, 0 deletions
diff --git a/firmware/Makefile b/firmware/Makefile index 3742feeb066f..9e780a331e10 100644 --- a/firmware/Makefile +++ b/firmware/Makefile | |||
| @@ -32,6 +32,9 @@ quiet_cmd_mkdir = MKDIR $(patsubst $(objtree)/%,%,$@) | |||
| 32 | quiet_cmd_ihex = IHEX $@ | 32 | quiet_cmd_ihex = IHEX $@ |
| 33 | cmd_ihex = $(OBJCOPY) -Iihex -Obinary $< $@ | 33 | cmd_ihex = $(OBJCOPY) -Iihex -Obinary $< $@ |
| 34 | 34 | ||
| 35 | quiet_cmd_ihex2fw = IHEX2FW $@ | ||
| 36 | cmd_ihex2fw = $(objtree)/$(obj)/ihex2fw $< $@ | ||
| 37 | |||
| 35 | quiet_cmd_fwbin = MK_FW $@ | 38 | quiet_cmd_fwbin = MK_FW $@ |
| 36 | cmd_fwbin = FWNAME="$(patsubst firmware/%.gen.S,%,$@)"; \ | 39 | cmd_fwbin = FWNAME="$(patsubst firmware/%.gen.S,%,$@)"; \ |
| 37 | FWSTR="$(subst /,_,$(subst .,_,$(subst -,_,$(patsubst \ | 40 | FWSTR="$(subst /,_,$(subst .,_,$(subst -,_,$(patsubst \ |
| @@ -84,9 +87,18 @@ $(patsubst %,$(obj)/%.gen.S, $(fw-external-y)): %: $(wordsize_deps) \ | |||
| 84 | $(patsubst %,$(obj)/%.gen.o, $(fw-shipped-y)): %.gen.o: % | 87 | $(patsubst %,$(obj)/%.gen.o, $(fw-shipped-y)): %.gen.o: % |
| 85 | $(patsubst %,$(obj)/%.gen.o, $(fw-external-y)): $(obj)/%.gen.o: $(fwdir)/% | 88 | $(patsubst %,$(obj)/%.gen.o, $(fw-external-y)): $(obj)/%.gen.o: $(fwdir)/% |
| 86 | 89 | ||
| 90 | # .ihex is used just as a simple way to hold binary files in a source tree | ||
| 91 | # where binaries are frowned upon. They are directly converted with objcopy. | ||
| 87 | $(obj)/%: $(obj)/%.ihex | $(objtree)/$(obj)/$$(dir %) | 92 | $(obj)/%: $(obj)/%.ihex | $(objtree)/$(obj)/$$(dir %) |
| 88 | $(call cmd,ihex) | 93 | $(call cmd,ihex) |
| 89 | 94 | ||
| 95 | # .HEX is also Intel HEX, but where the offset and length in each record | ||
| 96 | # is actually meaningful, because the firmware has to be loaded in a certain | ||
| 97 | # order rather than as a single binary blob. Thus, we convert them into our | ||
| 98 | # more compact binary representation of ihex records (<linux/ihex.h>) | ||
| 99 | $(obj)/%.fw: $(obj)/%.HEX $(obj)/ihex2fw | $(objtree)/$(obj)/$$(dir %) | ||
| 100 | $(call cmd,ihex2fw) | ||
| 101 | |||
| 90 | $(firmware-dirs): | 102 | $(firmware-dirs): |
| 91 | $(call cmd,mkdir) | 103 | $(call cmd,mkdir) |
| 92 | 104 | ||
| @@ -101,3 +113,5 @@ targets := $(fw-shipped-) $(patsubst $(obj)/%,%, \ | |||
| 101 | # Without this, built-in.o won't be created when it's empty, and the | 113 | # Without this, built-in.o won't be created when it's empty, and the |
| 102 | # final vmlinux link will fail. | 114 | # final vmlinux link will fail. |
| 103 | obj-n := dummy | 115 | obj-n := dummy |
| 116 | |||
| 117 | hostprogs-y := ihex2fw | ||
diff --git a/firmware/ihex2fw.c b/firmware/ihex2fw.c new file mode 100644 index 000000000000..9e77cd2f7152 --- /dev/null +++ b/firmware/ihex2fw.c | |||
| @@ -0,0 +1,247 @@ | |||
| 1 | /* | ||
| 2 | * Parser/loader for IHEX formatted data. | ||
| 3 | * | ||
| 4 | * Copyright © 2008 David Woodhouse <dwmw2@infradead.org> | ||
| 5 | * Copyright © 2005 Jan Harkes <jaharkes@cs.cmu.edu> | ||
| 6 | * | ||
| 7 | * This program is free software; you can redistribute it and/or modify | ||
| 8 | * it under the terms of the GNU General Public License version 2 as | ||
| 9 | * published by the Free Software Foundation. | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include <stdint.h> | ||
| 13 | #include <arpa/inet.h> | ||
| 14 | #include <stdio.h> | ||
| 15 | #include <errno.h> | ||
| 16 | #include <sys/types.h> | ||
| 17 | #include <sys/stat.h> | ||
| 18 | #include <sys/mman.h> | ||
| 19 | #include <fcntl.h> | ||
| 20 | #include <string.h> | ||
| 21 | #include <unistd.h> | ||
| 22 | #include <stdlib.h> | ||
| 23 | |||
| 24 | struct ihex_binrec { | ||
| 25 | struct ihex_binrec *next; /* not part of the real data structure */ | ||
| 26 | uint32_t addr; | ||
| 27 | uint16_t len; | ||
| 28 | uint8_t data[]; | ||
| 29 | }; | ||
| 30 | |||
| 31 | /** | ||
| 32 | * nybble/hex are little helpers to parse hexadecimal numbers to a byte value | ||
| 33 | **/ | ||
| 34 | static uint8_t nybble(const uint8_t n) | ||
| 35 | { | ||
| 36 | if (n >= '0' && n <= '9') return n - '0'; | ||
| 37 | else if (n >= 'A' && n <= 'F') return n - ('A' - 10); | ||
| 38 | else if (n >= 'a' && n <= 'f') return n - ('a' - 10); | ||
| 39 | return 0; | ||
| 40 | } | ||
| 41 | |||
| 42 | static uint8_t hex(const uint8_t *data, uint8_t *crc) | ||
| 43 | { | ||
| 44 | uint8_t val = (nybble(data[0]) << 4) | nybble(data[1]); | ||
| 45 | *crc += val; | ||
| 46 | return val; | ||
| 47 | } | ||
| 48 | |||
| 49 | static int process_ihex(uint8_t *data, ssize_t size); | ||
| 50 | static void file_record(struct ihex_binrec *record); | ||
| 51 | static int output_records(int outfd); | ||
| 52 | |||
| 53 | static int sort_records = 0; | ||
| 54 | |||
| 55 | int main(int argc, char **argv) | ||
| 56 | { | ||
| 57 | int infd, outfd; | ||
| 58 | struct stat st; | ||
| 59 | uint8_t *data; | ||
| 60 | |||
| 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; | ||
| 73 | } | ||
| 74 | if (!strcmp(argv[1], "-")) | ||
| 75 | infd = 0; | ||
| 76 | else | ||
| 77 | infd = open(argv[1], O_RDONLY); | ||
| 78 | if (infd == -1) { | ||
| 79 | fprintf(stderr, "Failed to open source file: %s", | ||
| 80 | strerror(errno)); | ||
| 81 | goto usage; | ||
| 82 | } | ||
| 83 | if (fstat(infd, &st)) { | ||
| 84 | perror("stat"); | ||
| 85 | return 1; | ||
| 86 | } | ||
| 87 | data = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, infd, 0); | ||
| 88 | if (data == MAP_FAILED) { | ||
| 89 | perror("mmap"); | ||
| 90 | return 1; | ||
| 91 | } | ||
| 92 | |||
| 93 | if (!strcmp(argv[2], "-")) | ||
| 94 | outfd = 1; | ||
| 95 | else | ||
| 96 | outfd = open(argv[2], O_TRUNC|O_CREAT|O_WRONLY, 0644); | ||
| 97 | if (outfd == -1) { | ||
| 98 | fprintf(stderr, "Failed to open destination file: %s", | ||
| 99 | strerror(errno)); | ||
| 100 | goto usage; | ||
| 101 | } | ||
| 102 | if (process_ihex(data, st.st_size)) | ||
| 103 | return 1; | ||
| 104 | |||
| 105 | output_records(outfd); | ||
| 106 | return 0; | ||
| 107 | } | ||
| 108 | |||
| 109 | static int process_ihex(uint8_t *data, ssize_t size) | ||
| 110 | { | ||
| 111 | struct ihex_binrec *record; | ||
| 112 | uint32_t offset = 0; | ||
| 113 | uint8_t type, crc = 0, crcbyte = 0; | ||
| 114 | int i, j; | ||
| 115 | int line = 1; | ||
| 116 | int len; | ||
| 117 | |||
| 118 | i = 0; | ||
| 119 | next_record: | ||
| 120 | /* search for the start of record character */ | ||
| 121 | while (i < size) { | ||
| 122 | if (data[i] == '\n') line++; | ||
| 123 | if (data[i++] == ':') break; | ||
| 124 | } | ||
| 125 | |||
| 126 | /* Minimum record length would be about 10 characters */ | ||
| 127 | if (i + 10 > size) { | ||
| 128 | fprintf(stderr, "Can't find valid record at line %d\n", line); | ||
| 129 | return -EINVAL; | ||
| 130 | } | ||
| 131 | |||
| 132 | len = hex(data + i, &crc); i += 2; | ||
| 133 | |||
| 134 | record = malloc((sizeof (*record) + len + 3) & ~3); | ||
| 135 | if (!record) { | ||
| 136 | fprintf(stderr, "out of memory for records\n"); | ||
| 137 | return -ENOMEM; | ||
| 138 | } | ||
| 139 | memset(record, 0, (sizeof(*record) + len + 3) & ~3); | ||
| 140 | record->len = len; | ||
| 141 | |||
| 142 | /* now check if we have enough data to read everything */ | ||
| 143 | if (i + 8 + (record->len * 2) > size) { | ||
| 144 | fprintf(stderr, "Not enough data to read complete record at line %d\n", | ||
| 145 | line); | ||
| 146 | return -EINVAL; | ||
| 147 | } | ||
| 148 | |||
| 149 | record->addr = hex(data + i, &crc) << 8; i += 2; | ||
| 150 | record->addr |= hex(data + i, &crc); i += 2; | ||
| 151 | type = hex(data + i, &crc); i += 2; | ||
| 152 | |||
| 153 | for (j = 0; j < record->len; j++, i += 2) | ||
| 154 | record->data[j] = hex(data + i, &crc); | ||
| 155 | |||
| 156 | /* check CRC */ | ||
| 157 | |||
