2 * Copyright (c) 2010,2011 Kai Wang
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/param.h>
39 ELFTC_VCSID("$Id: ascii.c 3757 2019-06-28 01:15:28Z emaste $");
41 static void append_data(struct section *s, const void *buf, size_t sz);
42 static char hex_digit(uint8_t n);
43 static int hex_value(int x);
44 static void finalize_data_section(struct section *s);
45 static int ishexdigit(int x);
46 static int ihex_read(const char *line, char *type, uint64_t *addr,
47 uint64_t *num, uint8_t *data, size_t *sz);
48 static void ihex_write(int ofd, int type, uint64_t addr, uint64_t num,
49 const void *buf, size_t sz);
50 static void ihex_write_00(int ofd, uint64_t addr, const void *buf, size_t sz);
51 static void ihex_write_01(int ofd);
52 static void ihex_write_04(int ofd, uint16_t addr);
53 static void ihex_write_05(int ofd, uint64_t e_entry);
54 static struct section *new_data_section(struct elfcopy *ecp, int sec_index,
55 uint64_t off, uint64_t addr);
56 static int read_num(const char *line, int *len, uint64_t *num, size_t sz,
58 static int srec_read(const char *line, char *type, uint64_t *addr,
59 uint8_t *data, size_t *sz);
60 static void srec_write(int ofd, char type, uint64_t addr, const void *buf,
62 static void srec_write_symtab(int ofd, const char *ofn, Elf *e, Elf_Scn *scn,
64 static void srec_write_S0(int ofd, const char *ofn);
65 static void srec_write_Sd(int ofd, char dr, uint64_t addr, const void *buf,
66 size_t sz, size_t rlen);
67 static void srec_write_Se(int ofd, uint64_t e_entry, int forceS3);
68 static void write_num(char *line, int *len, uint64_t num, size_t sz,
71 #define _LINE_BUFSZ 1024
72 #define _DATA_BUFSZ 256
75 * Convert ELF object to S-Record.
78 create_srec(struct elfcopy *ecp, int ifd, int ofd, const char *ofn)
90 if ((e = elf_begin(ifd, ELF_C_READ, NULL)) == NULL)
91 errx(EXIT_FAILURE, "elf_begin() failed: %s",
94 /* Output a symbol table for `symbolsrec' target. */
95 if (!strncmp(ecp->otgt, "symbolsrec", strlen("symbolsrec"))) {
97 while ((scn = elf_nextscn(e, scn)) != NULL) {
98 if (gelf_getshdr(scn, &sh) == NULL) {
99 warnx("gelf_getshdr failed: %s",
104 if (sh.sh_type != SHT_SYMTAB)
106 srec_write_symtab(ofd, ofn, e, scn, &sh);
111 if (ecp->flags & SREC_FORCE_S3)
115 * Find maximum address size in the first iteration.
119 while ((scn = elf_nextscn(e, scn)) != NULL) {
120 if (gelf_getshdr(scn, &sh) == NULL) {
121 warnx("gelf_getshdr failed: %s",
126 if ((sh.sh_flags & SHF_ALLOC) == 0 ||
127 sh.sh_type == SHT_NOBITS ||
130 if ((uint64_t) sh.sh_addr > max_addr)
131 max_addr = sh.sh_addr;
133 elferr = elf_errno();
135 warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
137 if (max_addr <= 0xFFFF)
139 else if (max_addr <= 0xFFFFFF)
145 if (ecp->flags & SREC_FORCE_LEN) {
146 addr_sz = dr - '0' + 1;
147 if (ecp->srec_len < 1)
149 else if (ecp->srec_len + addr_sz + 1 > 255)
150 rlen = 255 - (addr_sz + 1);
152 rlen = ecp->srec_len;
156 /* Generate S0 record which contains the output filename. */
157 srec_write_S0(ofd, ofn);
159 /* Generate S{1,2,3} data records for section data. */
161 while ((scn = elf_nextscn(e, scn)) != NULL) {
162 if (gelf_getshdr(scn, &sh) == NULL) {
163 warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
167 if ((sh.sh_flags & SHF_ALLOC) == 0 ||
168 sh.sh_type == SHT_NOBITS ||
171 if (sh.sh_addr > 0xFFFFFFFF) {
172 warnx("address space too big for S-Record file");
176 if ((d = elf_getdata(scn, NULL)) == NULL) {
177 elferr = elf_errno();
179 warnx("elf_getdata failed: %s", elf_errmsg(-1));
182 if (d->d_buf == NULL || d->d_size == 0)
184 srec_write_Sd(ofd, dr, sh.sh_addr, d->d_buf, d->d_size, rlen);
186 elferr = elf_errno();
188 warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
190 /* Generate S{7,8,9} end of block record. */
191 if (gelf_getehdr(e, &eh) == NULL)
192 errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
194 srec_write_Se(ofd, eh.e_entry, ecp->flags & SREC_FORCE_S3);
198 create_elf_from_srec(struct elfcopy *ecp, int ifd)
200 char line[_LINE_BUFSZ], name[_LINE_BUFSZ];
201 uint8_t data[_DATA_BUFSZ];
203 struct section *s, *shtab;
205 uint64_t addr, entry, off, sec_addr;
208 int _ifd, first, sec_index, in_symtab, symtab_created;
212 if ((_ifd = dup(ifd)) < 0)
213 err(EXIT_FAILURE, "dup failed");
214 if ((ifp = fdopen(_ifd, "r")) == NULL)
215 err(EXIT_FAILURE, "fdopen failed");
217 /* Create EHDR for output .o file. */
218 if (gelf_newehdr(ecp->eout, ecp->oec) == NULL)
219 errx(EXIT_FAILURE, "gelf_newehdr failed: %s",
221 if (gelf_getehdr(ecp->eout, &oeh) == NULL)
222 errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
225 /* Initialise e_ident fields. */
226 oeh.e_ident[EI_CLASS] = ecp->oec;
227 oeh.e_ident[EI_DATA] = ecp->oed;
229 * TODO: Set OSABI according to the OS platform where elfcopy(1)
230 * was build. (probably)
232 oeh.e_ident[EI_OSABI] = ELFOSABI_NONE;
233 oeh.e_machine = ecp->oem;
237 ecp->flags |= RELOCATABLE;
239 /* Create .shstrtab section */
241 ecp->shstrtab->off = 0;
243 /* Data sections are inserted after EHDR. */
244 off = gelf_fsize(ecp->eout, ELF_T_EHDR, 1, EV_CURRENT);
246 errx(EXIT_FAILURE, "gelf_fsize() failed: %s", elf_errmsg(-1));
248 /* Create data sections. */
252 sec_addr = entry = 0;
253 while (fgets(line, _LINE_BUFSZ, ifp) != NULL) {
255 if (line[0] == '\r' || line[0] == '\n')
257 if (line[0] == '$' && line[1] == '$') {
258 ecp->flags |= SYMTAB_EXIST;
259 while ((rlt = fgets(line, _LINE_BUFSZ, ifp)) != NULL) {
260 if (line[0] == '$' && line[1] == '$')
267 if (line[0] != 'S' || line[1] < '0' || line[1] > '9') {
268 warnx("Invalid srec record");
271 if (srec_read(line, &type, &addr, data, &sz) < 0) {
272 warnx("Invalid srec record or mismatched checksum");
281 if (first || sec_addr != addr) {
283 finalize_data_section(s);
284 s = new_data_section(ecp, sec_index, off,
287 warnx("new_data_section failed");
294 append_data(s, data, sz);
308 finalize_data_section(s);
310 warn("fgets failed");
312 /* Insert .shstrtab after data sections. */
313 if ((ecp->shstrtab->os = elf_newscn(ecp->eout)) == NULL)
314 errx(EXIT_FAILURE, "elf_newscn failed: %s",
316 insert_to_sec_list(ecp, ecp->shstrtab, 1);
318 /* Insert section header table here. */
319 shtab = insert_shtab(ecp, 1);
322 * Rescan and create symbol table if we found '$$' section in
327 if (ecp->flags & SYMTAB_EXIST) {
328 if (fseek(ifp, 0, SEEK_SET) < 0) {
329 warn("fseek failed");
330 ecp->flags &= ~SYMTAB_EXIST;
333 while (fgets(line, _LINE_BUFSZ, ifp) != NULL) {
335 if (line[0] == '$' && line[1] == '$') {
339 if (sscanf(line, "%s $%jx", name,
341 warnx("Invalid symbolsrec record");
344 if (!symtab_created) {
345 create_external_symtab(ecp);
348 add_to_symtab(ecp, name, st_value, 0, SHN_ABS,
349 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, 1);
351 if (line[0] == '$' && line[1] == '$') {
358 warn("fgets failed");
359 if (symtab_created) {
360 finalize_external_symtab(ecp);
361 create_symtab_data(ecp);
362 /* Count in .symtab and .strtab section headers. */
363 shtab->sz += gelf_fsize(ecp->eout, ELF_T_SHDR, 2, EV_CURRENT);
365 ecp->flags &= ~SYMTAB_EXIST;
370 /* Set entry point. */
374 * Write the underlying ehdr. Note that it should be called
375 * before elf_setshstrndx() since it will overwrite e->e_shstrndx.
377 if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
378 errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
381 /* Update sh_name pointer for each section header entry. */
384 /* Renew oeh to get the updated e_shstrndx. */
385 if (gelf_getehdr(ecp->eout, &oeh) == NULL)
386 errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
389 /* Resync section offsets. */
390 resync_sections(ecp);
392 /* Store SHDR offset in EHDR. */
393 oeh.e_shoff = shtab->off;
395 /* Update ehdr since we modified e_shoff. */
396 if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
397 errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
400 /* Write out the output elf object. */
401 if (elf_update(ecp->eout, ELF_C_WRITE) < 0)
402 errx(EXIT_FAILURE, "elf_update() failed: %s",
405 /* Release allocated resource. */
410 create_ihex(int ifd, int ofd)
418 uint16_t addr_hi, old_addr_hi;
420 if ((e = elf_begin(ifd, ELF_C_READ, NULL)) == NULL)
421 errx(EXIT_FAILURE, "elf_begin() failed: %s",
426 while ((scn = elf_nextscn(e, scn)) != NULL) {
427 if (gelf_getshdr(scn, &sh) == NULL) {
428 warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
432 if ((sh.sh_flags & SHF_ALLOC) == 0 ||
433 sh.sh_type == SHT_NOBITS ||
436 if (sh.sh_addr > 0xFFFFFFFF) {
437 warnx("address space too big for Intel Hex file");
441 if ((d = elf_getdata(scn, NULL)) == NULL) {
442 elferr = elf_errno();
444 warnx("elf_getdata failed: %s", elf_errmsg(-1));
447 if (d->d_buf == NULL || d->d_size == 0)
449 addr_hi = (sh.sh_addr >> 16) & 0xFFFF;
450 if (addr_hi > 0 && addr_hi != old_addr_hi) {
451 /* Write 04 record if addr_hi is new. */
452 old_addr_hi = addr_hi;
453 ihex_write_04(ofd, addr_hi);
455 ihex_write_00(ofd, sh.sh_addr, d->d_buf, d->d_size);
457 elferr = elf_errno();
459 warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
461 if (gelf_getehdr(e, &eh) == NULL)
462 errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
464 ihex_write_05(ofd, eh.e_entry);
469 create_elf_from_ihex(struct elfcopy *ecp, int ifd)
471 char line[_LINE_BUFSZ];
472 uint8_t data[_DATA_BUFSZ];
474 struct section *s, *shtab;
476 uint64_t addr, addr_base, entry, num, off, rec_addr, sec_addr;
478 int _ifd, first, sec_index;
481 if ((_ifd = dup(ifd)) < 0)
482 err(EXIT_FAILURE, "dup failed");
483 if ((ifp = fdopen(_ifd, "r")) == NULL)
484 err(EXIT_FAILURE, "fdopen failed");
486 /* Create EHDR for output .o file. */
487 if (gelf_newehdr(ecp->eout, ecp->oec) == NULL)
488 errx(EXIT_FAILURE, "gelf_newehdr failed: %s",
490 if (gelf_getehdr(ecp->eout, &oeh) == NULL)
491 errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
494 /* Initialise e_ident fields. */
495 oeh.e_ident[EI_CLASS] = ecp->oec;
496 oeh.e_ident[EI_DATA] = ecp->oed;
498 * TODO: Set OSABI according to the OS platform where elfcopy(1)
499 * was build. (probably)
501 oeh.e_ident[EI_OSABI] = ELFOSABI_NONE;
502 oeh.e_machine = ecp->oem;
506 ecp->flags |= RELOCATABLE;
508 /* Create .shstrtab section */
510 ecp->shstrtab->off = 0;
512 /* Data sections are inserted after EHDR. */
513 off = gelf_fsize(ecp->eout, ELF_T_EHDR, 1, EV_CURRENT);
515 errx(EXIT_FAILURE, "gelf_fsize() failed: %s", elf_errmsg(-1));
517 /* Create data sections. */
521 addr_base = rec_addr = sec_addr = entry = 0;
522 while (fgets(line, _LINE_BUFSZ, ifp) != NULL) {
523 if (line[0] == '\r' || line[0] == '\n')
525 if (line[0] != ':') {
526 warnx("Invalid ihex record");
529 if (ihex_read(line, &type, &addr, &num, data, &sz) < 0) {
530 warnx("Invalid ihex record or mismatched checksum");
538 rec_addr = addr_base + addr;
539 if (first || sec_addr != rec_addr) {
541 finalize_data_section(s);
542 s = new_data_section(ecp, sec_index, off,
545 warnx("new_data_section failed");
552 append_data(s, data, sz);
557 /* End of file record. */
560 /* Extended segment address record. */
561 addr_base = addr << 4;
564 /* Start segment address record (CS:IP). Ignored. */
567 /* Extended linear address record. */
568 addr_base = num << 16;
571 /* Start linear address record. */
580 finalize_data_section(s);
582 warn("fgets failed");
585 /* Insert .shstrtab after data sections. */
586 if ((ecp->shstrtab->os = elf_newscn(ecp->eout)) == NULL)
587 errx(EXIT_FAILURE, "elf_newscn failed: %s",
589 insert_to_sec_list(ecp, ecp->shstrtab, 1);
591 /* Insert section header table here. */
592 shtab = insert_shtab(ecp, 1);
594 /* Set entry point. */
598 * Write the underlying ehdr. Note that it should be called
599 * before elf_setshstrndx() since it will overwrite e->e_shstrndx.
601 if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
602 errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
605 /* Update sh_name pointer for each section header entry. */
608 /* Renew oeh to get the updated e_shstrndx. */
609 if (gelf_getehdr(ecp->eout, &oeh) == NULL)
610 errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
613 /* Resync section offsets. */
614 resync_sections(ecp);
616 /* Store SHDR offset in EHDR. */
617 oeh.e_shoff = shtab->off;
619 /* Update ehdr since we modified e_shoff. */
620 if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
621 errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
624 /* Write out the output elf object. */
625 if (elf_update(ecp->eout, ELF_C_WRITE) < 0)
626 errx(EXIT_FAILURE, "elf_update() failed: %s",
629 /* Release allocated resource. */
633 #define _SEC_NAMESZ 64
634 #define _SEC_INIT_CAP 1024
636 static struct section *
637 new_data_section(struct elfcopy *ecp, int sec_index, uint64_t off,
642 if ((name = malloc(_SEC_NAMESZ)) == NULL)
643 errx(EXIT_FAILURE, "malloc failed");
644 snprintf(name, _SEC_NAMESZ, ".sec%d", sec_index);
646 return (create_external_section(ecp, name, name, NULL, 0, off,
647 SHT_PROGBITS, ELF_T_BYTE, SHF_ALLOC | SHF_WRITE, 1, addr, 0));
651 finalize_data_section(struct section *s)
655 if ((od = elf_newdata(s->os)) == NULL)
656 errx(EXIT_FAILURE, "elf_newdata() failed: %s",
658 od->d_align = s->align;
662 od->d_version = EV_CURRENT;
666 append_data(struct section *s, const void *buf, size_t sz)
670 if (s->buf == NULL) {
672 s->cap = _SEC_INIT_CAP;
673 if ((s->buf = malloc(s->cap)) == NULL)
674 err(EXIT_FAILURE, "malloc failed");
677 while (sz + s->sz > s->cap) {
679 if ((s->buf = realloc(s->buf, s->cap)) == NULL)
680 err(EXIT_FAILURE, "realloc failed");
684 memcpy(&p[s->sz], buf, sz);
689 srec_read(const char *line, char *type, uint64_t *addr, uint8_t *data,
692 uint64_t count, _checksum, num;
694 int checksum, i, len;
698 if (read_num(line, &len, &count, 1, &checksum) < 0)
720 if (read_num(line, &len, addr, addr_sz, &checksum) < 0)
723 count -= addr_sz + 1;
724 if (*type >= '0' && *type <= '3') {
725 for (i = 0; (uint64_t) i < count; i++) {
726 if (read_num(line, &len, &num, 1, &checksum) < 0)
728 data[i] = (uint8_t) num;
734 if (read_num(line, &len, &_checksum, 1, NULL) < 0)
737 if ((int) _checksum != (~checksum & 0xFF))
744 srec_write_symtab(int ofd, const char *ofn, Elf *e, Elf_Scn *scn, GElf_Shdr *sh)
746 char line[_LINE_BUFSZ];
753 #define _WRITE_LINE do { \
754 if (write(ofd, line, strlen(line)) != (ssize_t) strlen(line)) \
755 errx(EXIT_FAILURE, "write failed"); \
760 if ((d = elf_getdata(scn, NULL)) == NULL) {
761 elferr = elf_errno();
763 warnx("elf_getdata failed: %s",
767 if (d->d_buf == NULL || d->d_size == 0)
770 snprintf(line, sizeof(line), "$$ %s\r\n", ofn);
772 sc = d->d_size / sh->sh_entsize;
773 for (i = 1; (size_t) i < sc; i++) {
774 if (gelf_getsym(d, i, &sym) != &sym) {
775 warnx("gelf_getsym failed: %s", elf_errmsg(-1));
778 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION ||
779 GELF_ST_TYPE(sym.st_info) == STT_FILE)
781 if ((name = elf_strptr(e, sh->sh_link, sym.st_name)) == NULL) {
782 warnx("elf_strptr failed: %s", elf_errmsg(-1));
785 snprintf(line, sizeof(line), " %s $%jx\r\n", name,
786 (uintmax_t) sym.st_value);
789 snprintf(line, sizeof(line), "$$ \r\n");
796 srec_write_S0(int ofd, const char *ofn)
799 srec_write(ofd, '0', 0, ofn, strlen(ofn));
803 srec_write_Sd(int ofd, char dr, uint64_t addr, const void *buf, size_t sz,
806 const uint8_t *p, *pe;
810 while (pe - p >= (int) rlen) {
811 srec_write(ofd, dr, addr, p, rlen);
816 srec_write(ofd, dr, addr, p, pe - p);
820 srec_write_Se(int ofd, uint64_t e_entry, int forceS3)
824 if (e_entry > 0xFFFFFFFF) {
825 warnx("address space too big for S-Record file");
832 if (e_entry <= 0xFFFF)
834 else if (e_entry <= 0xFFFFFF)
840 srec_write(ofd, er, e_entry, NULL, 0);
844 srec_write(int ofd, char type, uint64_t addr, const void *buf, size_t sz)
846 char line[_LINE_BUFSZ];
847 const uint8_t *p, *pe;
848 int len, addr_sz, checksum;
850 if (type == '0' || type == '1' || type == '5' || type == '9')
852 else if (type == '2' || type == '8')
861 write_num(line, &len, addr_sz + sz + 1, 1, &checksum);
862 write_num(line, &len, addr, addr_sz, &checksum);
863 for (p = buf, pe = p + sz; p < pe; p++)
864 write_num(line, &len, *p, 1, &checksum);
865 write_num(line, &len, ~checksum & 0xFF, 1, NULL);
868 if (write(ofd, line, len) != (ssize_t) len)
869 err(EXIT_FAILURE, "write failed");
873 ihex_write_00(int ofd, uint64_t addr, const void *buf, size_t sz)
875 uint16_t addr_hi, old_addr_hi;
876 const uint8_t *p, *pe;
878 old_addr_hi = (addr >> 16) & 0xFFFF;
881 while (pe - p >= 16) {
882 ihex_write(ofd, 0, addr, 0, p, 16);
885 addr_hi = (addr >> 16) & 0xFFFF;
886 if (addr_hi != old_addr_hi) {
887 old_addr_hi = addr_hi;
888 ihex_write_04(ofd, addr_hi);
892 ihex_write(ofd, 0, addr, 0, p, pe - p);
896 ihex_read(const char *line, char *type, uint64_t *addr, uint64_t *num,
897 uint8_t *data, size_t *sz)
899 uint64_t count, _checksum;
900 int checksum, i, len;
905 if (read_num(line, &len, &count, 1, &checksum) < 0)
907 if (read_num(line, &len, addr, 2, &checksum) < 0)
909 if (line[len++] != '0')
912 checksum += *type - '0';
915 for (i = 0; (uint64_t) i < count; i++) {
916 if (read_num(line, &len, num, 1, &checksum) < 0)
918 data[i] = (uint8_t) *num;
930 if (read_num(line, &len, num, 2, &checksum) < 0)
937 if (read_num(line, &len, num, 4, &checksum) < 0)
944 if (read_num(line, &len, &_checksum, 1, &checksum) < 0)
947 if ((checksum & 0xFF) != 0) {
955 ihex_write_01(int ofd)
958 ihex_write(ofd, 1, 0, 0, NULL, 0);
962 ihex_write_04(int ofd, uint16_t addr)
965 ihex_write(ofd, 4, 0, addr, NULL, 2);
969 ihex_write_05(int ofd, uint64_t e_entry)
972 if (e_entry > 0xFFFFFFFF) {
973 warnx("address space too big for Intel Hex file");
977 ihex_write(ofd, 5, 0, e_entry, NULL, 4);
981 ihex_write(int ofd, int type, uint64_t addr, uint64_t num, const void *buf,
984 char line[_LINE_BUFSZ];
985 const uint8_t *p, *pe;
989 errx(EXIT_FAILURE, "Internal: ihex_write() sz too big");
993 write_num(line, &len, sz, 1, &checksum);
994 write_num(line, &len, addr, 2, &checksum);
995 write_num(line, &len, type, 1, &checksum);
998 for (p = buf, pe = p + sz; p < pe; p++)
999 write_num(line, &len, *p, 1, &checksum);
1001 write_num(line, &len, num, sz, &checksum);
1003 write_num(line, &len, (~checksum + 1) & 0xFF, 1, NULL);
1006 if (write(ofd, line, len) != (ssize_t) len)
1007 err(EXIT_FAILURE, "write failed");
1011 read_num(const char *line, int *len, uint64_t *num, size_t sz, int *checksum)
1016 for (; sz > 0; sz--) {
1017 if (!ishexdigit(line[*len]) || !ishexdigit(line[*len + 1]))
1019 b = (hex_value(line[*len]) << 4) | hex_value(line[*len + 1]);
1020 *num = (*num << 8) | b;
1022 if (checksum != NULL)
1023 *checksum = (*checksum + b) & 0xFF;
1030 write_num(char *line, int *len, uint64_t num, size_t sz, int *checksum)
1034 for (; sz > 0; sz--) {
1035 b = (num >> ((sz - 1) * 8)) & 0xFF;
1036 line[*len] = hex_digit((b >> 4) & 0xF);
1037 line[*len + 1] = hex_digit(b & 0xF);
1039 if (checksum != NULL)
1040 *checksum = (*checksum + b) & 0xFF;
1045 hex_digit(uint8_t n)
1048 return ((n < 10) ? '0' + n : 'A' + (n - 10));
1057 else if (x >= 'a' && x <= 'f')
1058 return (x - 'a' + 10);
1060 return (x - 'A' + 10);
1069 if ((x >= 'a' && x <= 'f') || (x >= 'A' && x <= 'F'))