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 3177 2015-03-30 18:19:41Z 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 recrod. */
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) {
254 if (line[0] == '\r' || line[0] == '\n')
256 if (line[0] == '$' && line[1] == '$') {
257 ecp->flags |= SYMTAB_EXIST;
258 while ((rlt = fgets(line, _LINE_BUFSZ, ifp)) != NULL) {
259 if (line[0] == '$' && line[1] == '$')
266 if (line[0] != 'S' || line[1] < '0' || line[1] > '9') {
267 warnx("Invalid srec record");
270 if (srec_read(line, &type, &addr, data, &sz) < 0) {
271 warnx("Invalid srec record or mismatched checksum");
280 if (first || sec_addr != addr) {
282 finalize_data_section(s);
283 s = new_data_section(ecp, sec_index, off,
286 warnx("new_data_section failed");
293 append_data(s, data, sz);
307 finalize_data_section(s);
309 warn("fgets failed");
311 /* Insert .shstrtab after data sections. */
312 if ((ecp->shstrtab->os = elf_newscn(ecp->eout)) == NULL)
313 errx(EXIT_FAILURE, "elf_newscn failed: %s",
315 insert_to_sec_list(ecp, ecp->shstrtab, 1);
317 /* Insert section header table here. */
318 shtab = insert_shtab(ecp, 1);
321 * Rescan and create symbol table if we found '$$' section in
326 if (ecp->flags & SYMTAB_EXIST) {
327 if (fseek(ifp, 0, SEEK_SET) < 0) {
328 warn("fseek failed");
329 ecp->flags &= ~SYMTAB_EXIST;
332 while (fgets(line, _LINE_BUFSZ, ifp) != NULL) {
334 if (line[0] == '$' && line[1] == '$') {
338 if (sscanf(line, "%s $%jx", name,
340 warnx("Invalid symbolsrec record");
343 if (!symtab_created) {
344 create_external_symtab(ecp);
347 add_to_symtab(ecp, name, st_value, 0, SHN_ABS,
348 ELF32_ST_INFO(STB_GLOBAL, STT_NOTYPE), 0, 1);
350 if (line[0] == '$' && line[1] == '$') {
357 warn("fgets failed");
358 if (symtab_created) {
359 finalize_external_symtab(ecp);
360 create_symtab_data(ecp);
361 /* Count in .symtab and .strtab section headers. */
362 shtab->sz += gelf_fsize(ecp->eout, ELF_T_SHDR, 2, EV_CURRENT);
364 ecp->flags &= ~SYMTAB_EXIST;
369 /* Set entry point. */
373 * Write the underlying ehdr. Note that it should be called
374 * before elf_setshstrndx() since it will overwrite e->e_shstrndx.
376 if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
377 errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
380 /* Generate section name string table (.shstrtab). */
383 /* Update sh_name pointer for each section header entry. */
386 /* Renew oeh to get the updated e_shstrndx. */
387 if (gelf_getehdr(ecp->eout, &oeh) == NULL)
388 errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
391 /* Resync section offsets. */
392 resync_sections(ecp);
394 /* Store SHDR offset in EHDR. */
395 oeh.e_shoff = shtab->off;
397 /* Update ehdr since we modified e_shoff. */
398 if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
399 errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
402 /* Write out the output elf object. */
403 if (elf_update(ecp->eout, ELF_C_WRITE) < 0)
404 errx(EXIT_FAILURE, "elf_update() failed: %s",
407 /* Release allocated resource. */
412 create_ihex(int ifd, int ofd)
420 uint16_t addr_hi, old_addr_hi;
422 if ((e = elf_begin(ifd, ELF_C_READ, NULL)) == NULL)
423 errx(EXIT_FAILURE, "elf_begin() failed: %s",
428 while ((scn = elf_nextscn(e, scn)) != NULL) {
429 if (gelf_getshdr(scn, &sh) == NULL) {
430 warnx("gelf_getshdr failed: %s", elf_errmsg(-1));
434 if ((sh.sh_flags & SHF_ALLOC) == 0 ||
435 sh.sh_type == SHT_NOBITS ||
438 if (sh.sh_addr > 0xFFFFFFFF) {
439 warnx("address space too big for Intel Hex file");
443 if ((d = elf_getdata(scn, NULL)) == NULL) {
444 elferr = elf_errno();
446 warnx("elf_getdata failed: %s", elf_errmsg(-1));
449 if (d->d_buf == NULL || d->d_size == 0)
451 addr_hi = (sh.sh_addr >> 16) & 0xFFFF;
452 if (addr_hi > 0 && addr_hi != old_addr_hi) {
453 /* Write 04 record if addr_hi is new. */
454 old_addr_hi = addr_hi;
455 ihex_write_04(ofd, addr_hi);
457 ihex_write_00(ofd, sh.sh_addr, d->d_buf, d->d_size);
459 elferr = elf_errno();
461 warnx("elf_nextscn failed: %s", elf_errmsg(elferr));
463 if (gelf_getehdr(e, &eh) == NULL)
464 errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
466 ihex_write_05(ofd, eh.e_entry);
471 create_elf_from_ihex(struct elfcopy *ecp, int ifd)
473 char line[_LINE_BUFSZ];
474 uint8_t data[_DATA_BUFSZ];
476 struct section *s, *shtab;
478 uint64_t addr, addr_base, entry, num, off, rec_addr, sec_addr;
480 int _ifd, first, sec_index;
483 if ((_ifd = dup(ifd)) < 0)
484 err(EXIT_FAILURE, "dup failed");
485 if ((ifp = fdopen(_ifd, "r")) == NULL)
486 err(EXIT_FAILURE, "fdopen failed");
488 /* Create EHDR for output .o file. */
489 if (gelf_newehdr(ecp->eout, ecp->oec) == NULL)
490 errx(EXIT_FAILURE, "gelf_newehdr failed: %s",
492 if (gelf_getehdr(ecp->eout, &oeh) == NULL)
493 errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
496 /* Initialise e_ident fields. */
497 oeh.e_ident[EI_CLASS] = ecp->oec;
498 oeh.e_ident[EI_DATA] = ecp->oed;
500 * TODO: Set OSABI according to the OS platform where elfcopy(1)
501 * was build. (probably)
503 oeh.e_ident[EI_OSABI] = ELFOSABI_NONE;
504 oeh.e_machine = ecp->oem;
508 ecp->flags |= RELOCATABLE;
510 /* Create .shstrtab section */
512 ecp->shstrtab->off = 0;
514 /* Data sections are inserted after EHDR. */
515 off = gelf_fsize(ecp->eout, ELF_T_EHDR, 1, EV_CURRENT);
517 errx(EXIT_FAILURE, "gelf_fsize() failed: %s", elf_errmsg(-1));
519 /* Create data sections. */
523 addr_base = rec_addr = sec_addr = entry = 0;
524 while (fgets(line, _LINE_BUFSZ, ifp) != NULL) {
525 if (line[0] == '\r' || line[0] == '\n')
527 if (line[0] != ':') {
528 warnx("Invalid ihex record");
531 if (ihex_read(line, &type, &addr, &num, data, &sz) < 0) {
532 warnx("Invalid ihex record or mismatched checksum");
540 rec_addr = addr_base + addr;
541 if (first || sec_addr != rec_addr) {
543 finalize_data_section(s);
544 s = new_data_section(ecp, sec_index, off,
547 warnx("new_data_section failed");
554 append_data(s, data, sz);
559 /* End of file record. */
562 /* Extended segment address record. */
563 addr_base = addr << 4;
566 /* Start segment address record (CS:IP). Ignored. */
569 /* Extended linear address record. */
570 addr_base = num << 16;
573 /* Start linear address record. */
582 finalize_data_section(s);
584 warn("fgets failed");
587 /* Insert .shstrtab after data sections. */
588 if ((ecp->shstrtab->os = elf_newscn(ecp->eout)) == NULL)
589 errx(EXIT_FAILURE, "elf_newscn failed: %s",
591 insert_to_sec_list(ecp, ecp->shstrtab, 1);
593 /* Insert section header table here. */
594 shtab = insert_shtab(ecp, 1);
596 /* Set entry point. */
600 * Write the underlying ehdr. Note that it should be called
601 * before elf_setshstrndx() since it will overwrite e->e_shstrndx.
603 if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
604 errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
607 /* Generate section name string table (.shstrtab). */
610 /* Update sh_name pointer for each section header entry. */
613 /* Renew oeh to get the updated e_shstrndx. */
614 if (gelf_getehdr(ecp->eout, &oeh) == NULL)
615 errx(EXIT_FAILURE, "gelf_getehdr() failed: %s",
618 /* Resync section offsets. */
619 resync_sections(ecp);
621 /* Store SHDR offset in EHDR. */
622 oeh.e_shoff = shtab->off;
624 /* Update ehdr since we modified e_shoff. */
625 if (gelf_update_ehdr(ecp->eout, &oeh) == 0)
626 errx(EXIT_FAILURE, "gelf_update_ehdr() failed: %s",
629 /* Write out the output elf object. */
630 if (elf_update(ecp->eout, ELF_C_WRITE) < 0)
631 errx(EXIT_FAILURE, "elf_update() failed: %s",
634 /* Release allocated resource. */
638 #define _SEC_NAMESZ 64
639 #define _SEC_INIT_CAP 1024
641 static struct section *
642 new_data_section(struct elfcopy *ecp, int sec_index, uint64_t off,
647 if ((name = malloc(_SEC_NAMESZ)) == NULL)
648 errx(EXIT_FAILURE, "malloc failed");
649 snprintf(name, _SEC_NAMESZ, ".sec%d", sec_index);
651 return (create_external_section(ecp, name, name, NULL, 0, off,
652 SHT_PROGBITS, ELF_T_BYTE, SHF_ALLOC | SHF_WRITE, 1, addr, 0));
656 finalize_data_section(struct section *s)
660 if ((od = elf_newdata(s->os)) == NULL)
661 errx(EXIT_FAILURE, "elf_newdata() failed: %s",
663 od->d_align = s->align;
667 od->d_version = EV_CURRENT;
671 append_data(struct section *s, const void *buf, size_t sz)
675 if (s->buf == NULL) {
677 s->cap = _SEC_INIT_CAP;
678 if ((s->buf = malloc(s->cap)) == NULL)
679 err(EXIT_FAILURE, "malloc failed");
682 while (sz + s->sz > s->cap) {
684 if ((s->buf = realloc(s->buf, s->cap)) == NULL)
685 err(EXIT_FAILURE, "realloc failed");
689 memcpy(&p[s->sz], buf, sz);
694 srec_read(const char *line, char *type, uint64_t *addr, uint8_t *data,
697 uint64_t count, _checksum, num;
699 int checksum, i, len;
703 if (read_num(line, &len, &count, 1, &checksum) < 0)
725 if (read_num(line, &len, addr, addr_sz, &checksum) < 0)
728 count -= addr_sz + 1;
729 if (*type >= '0' && *type <= '3') {
730 for (i = 0; (uint64_t) i < count; i++) {
731 if (read_num(line, &len, &num, 1, &checksum) < 0)
733 data[i] = (uint8_t) num;
739 if (read_num(line, &len, &_checksum, 1, NULL) < 0)
742 if ((int) _checksum != (~checksum & 0xFF))
749 srec_write_symtab(int ofd, const char *ofn, Elf *e, Elf_Scn *scn, GElf_Shdr *sh)
751 char line[_LINE_BUFSZ];
758 #define _WRITE_LINE do { \
759 if (write(ofd, line, strlen(line)) != (ssize_t) strlen(line)) \
760 errx(EXIT_FAILURE, "write failed"); \
765 if ((d = elf_getdata(scn, NULL)) == NULL) {
766 elferr = elf_errno();
768 warnx("elf_getdata failed: %s",
772 if (d->d_buf == NULL || d->d_size == 0)
775 snprintf(line, sizeof(line), "$$ %s\r\n", ofn);
777 sc = d->d_size / sh->sh_entsize;
778 for (i = 1; (size_t) i < sc; i++) {
779 if (gelf_getsym(d, i, &sym) != &sym) {
780 warnx("gelf_getsym failed: %s", elf_errmsg(-1));
783 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION ||
784 GELF_ST_TYPE(sym.st_info) == STT_FILE)
786 if ((name = elf_strptr(e, sh->sh_link, sym.st_name)) == NULL) {
787 warnx("elf_strptr failed: %s", elf_errmsg(-1));
790 snprintf(line, sizeof(line), " %s $%jx\r\n", name,
791 (uintmax_t) sym.st_value);
794 snprintf(line, sizeof(line), "$$ \r\n");
801 srec_write_S0(int ofd, const char *ofn)
804 srec_write(ofd, '0', 0, ofn, strlen(ofn));
808 srec_write_Sd(int ofd, char dr, uint64_t addr, const void *buf, size_t sz,
811 const uint8_t *p, *pe;
815 while (pe - p >= (int) rlen) {
816 srec_write(ofd, dr, addr, p, rlen);
821 srec_write(ofd, dr, addr, p, pe - p);
825 srec_write_Se(int ofd, uint64_t e_entry, int forceS3)
829 if (e_entry > 0xFFFFFFFF) {
830 warnx("address space too big for S-Record file");
837 if (e_entry <= 0xFFFF)
839 else if (e_entry <= 0xFFFFFF)
845 srec_write(ofd, er, e_entry, NULL, 0);
849 srec_write(int ofd, char type, uint64_t addr, const void *buf, size_t sz)
851 char line[_LINE_BUFSZ];
852 const uint8_t *p, *pe;
853 int len, addr_sz, checksum;
855 if (type == '0' || type == '1' || type == '5' || type == '9')
857 else if (type == '2' || type == '8')
866 write_num(line, &len, addr_sz + sz + 1, 1, &checksum);
867 write_num(line, &len, addr, addr_sz, &checksum);
868 for (p = buf, pe = p + sz; p < pe; p++)
869 write_num(line, &len, *p, 1, &checksum);
870 write_num(line, &len, ~checksum & 0xFF, 1, NULL);
873 if (write(ofd, line, len) != (ssize_t) len)
874 err(EXIT_FAILURE, "write failed");
878 ihex_write_00(int ofd, uint64_t addr, const void *buf, size_t sz)
880 uint16_t addr_hi, old_addr_hi;
881 const uint8_t *p, *pe;
883 old_addr_hi = (addr >> 16) & 0xFFFF;
886 while (pe - p >= 16) {
887 ihex_write(ofd, 0, addr, 0, p, 16);
890 addr_hi = (addr >> 16) & 0xFFFF;
891 if (addr_hi != old_addr_hi) {
892 old_addr_hi = addr_hi;
893 ihex_write_04(ofd, addr_hi);
897 ihex_write(ofd, 0, addr, 0, p, pe - p);
901 ihex_read(const char *line, char *type, uint64_t *addr, uint64_t *num,
902 uint8_t *data, size_t *sz)
904 uint64_t count, _checksum;
905 int checksum, i, len;
910 if (read_num(line, &len, &count, 1, &checksum) < 0)
912 if (read_num(line, &len, addr, 2, &checksum) < 0)
914 if (line[len++] != '0')
917 checksum += *type - '0';
920 for (i = 0; (uint64_t) i < count; i++) {
921 if (read_num(line, &len, num, 1, &checksum) < 0)
923 data[i] = (uint8_t) *num;
935 if (read_num(line, &len, num, 2, &checksum) < 0)
942 if (read_num(line, &len, num, 4, &checksum) < 0)
949 if (read_num(line, &len, &_checksum, 1, &checksum) < 0)
952 if ((checksum & 0xFF) != 0) {
960 ihex_write_01(int ofd)
963 ihex_write(ofd, 1, 0, 0, NULL, 0);
967 ihex_write_04(int ofd, uint16_t addr)
970 ihex_write(ofd, 4, 0, addr, NULL, 2);
974 ihex_write_05(int ofd, uint64_t e_entry)
977 if (e_entry > 0xFFFFFFFF) {
978 warnx("address space too big for Intel Hex file");
982 ihex_write(ofd, 5, 0, e_entry, NULL, 4);
986 ihex_write(int ofd, int type, uint64_t addr, uint64_t num, const void *buf,
989 char line[_LINE_BUFSZ];
990 const uint8_t *p, *pe;
994 errx(EXIT_FAILURE, "Internal: ihex_write() sz too big");
998 write_num(line, &len, sz, 1, &checksum);
999 write_num(line, &len, addr, 2, &checksum);
1000 write_num(line, &len, type, 1, &checksum);
1003 for (p = buf, pe = p + sz; p < pe; p++)
1004 write_num(line, &len, *p, 1, &checksum);
1006 write_num(line, &len, num, sz, &checksum);
1008 write_num(line, &len, (~checksum + 1) & 0xFF, 1, NULL);
1011 if (write(ofd, line, len) != (ssize_t) len)
1012 err(EXIT_FAILURE, "write failed");
1016 read_num(const char *line, int *len, uint64_t *num, size_t sz, int *checksum)
1021 for (; sz > 0; sz--) {
1022 if (!ishexdigit(line[*len]) || !ishexdigit(line[*len + 1]))
1024 b = (hex_value(line[*len]) << 4) | hex_value(line[*len + 1]);
1025 *num = (*num << 8) | b;
1027 if (checksum != NULL)
1028 *checksum = (*checksum + b) & 0xFF;
1035 write_num(char *line, int *len, uint64_t num, size_t sz, int *checksum)
1039 for (; sz > 0; sz--) {
1040 b = (num >> ((sz - 1) * 8)) & 0xFF;
1041 line[*len] = hex_digit((b >> 4) & 0xF);
1042 line[*len + 1] = hex_digit(b & 0xF);
1044 if (checksum != NULL)
1045 *checksum = (*checksum + b) & 0xFF;
1050 hex_digit(uint8_t n)
1053 return ((n < 10) ? '0' + n : 'A' + (n - 10));
1062 else if (x >= 'a' && x <= 'f')
1063 return (x - 'a' + 10);
1065 return (x - 'A' + 10);
1074 if ((x >= 'a' && x <= 'f') || (x >= 'A' && x <= 'F'))