2 * rdf2ihx.c - convert an RDOFF object file to Intel Hex format.
3 * This is based on rdf2bin.
4 * Note that this program only writes 16-bit HEX.
20 /* This function writes a single n-byte data record to of. Maximum value
22 static int write_data_record(FILE * of
, int ofs
, int nbytes
,
26 unsigned int checksum
;
29 fprintf(of
, ":%02X%04X00", nbytes
, ofs
);
31 for (i
= 0; i
< nbytes
; i
++) {
32 fprintf(of
, "%02X", data
[i
]);
36 checksum
= checksum
+ /* current checksum */
37 nbytes
+ /* RECLEN (one byte) */
38 ((iofs
>> 8) & 0xff) + /* high byte of load offset */
39 (iofs
& 0xff); /* low byte of load offset */
40 checksum
= ~checksum
+ 1;
41 fprintf(of
, "%02X\n", checksum
& 0xff);
45 int main(int argc
, char **argv
)
52 int pad
[2], segn
, ofs
, i
;
54 unsigned int checksum
;
58 puts("Usage: rdf2ihx [-o relocation-origin] [-p segment-alignment] " "input-file output-file");
65 if (strcmp(*argv
, "-o") == 0) {
67 origin
= readnum(*argv
, &err
);
69 fprintf(stderr
, "rdf2ihx: invalid parameter: %s\n", *argv
);
72 } else if (strcmp(*argv
, "-p") == 0) {
74 align
= readnum(*argv
, &err
);
76 fprintf(stderr
, "rdf2ihx: invalid parameter: %s\n", *argv
);
84 puts("rdf2ihx: required parameter missing");
90 rdfperror("rdf2ihx", *argv
);
93 printf("relocating %s: origin=%"PRIx32
", align=%d\n", *argv
, origin
, align
);
96 m
->datarel
= origin
+ m
->f
.seg
[0].length
;
97 if (m
->datarel
% align
!= 0) {
98 pad
[0] = align
- (m
->datarel
% align
);
104 m
->bssrel
= m
->datarel
+ m
->f
.seg
[1].length
;
105 if (m
->bssrel
% align
!= 0) {
106 pad
[1] = align
- (m
->bssrel
% align
);
112 printf("code: %08"PRIx32
"\ndata: %08"PRIx32
"\nbss: %08"PRIx32
"\n",
113 m
->textrel
, m
->datarel
, m
->bssrel
);
119 of
= fopen(*argv
, "w");
121 fprintf(stderr
, "rdf2ihx: could not open output file %s\n", *argv
);
125 padding
= malloc(align
);
127 fprintf(stderr
, "rdf2ihx: out of memory\n");
131 /* write extended segment address record */
132 fprintf(of
, ":02000002"); /* Record mark, reclen, load offset & rectyp
133 fields for ext. seg. address record */
134 segaddr
= ((origin
>> 16) & 0xffff); /* segment address */
135 fprintf(of
, "%04X", (unsigned int)(segaddr
& 0xffff));
136 checksum
= 0x02 + /* reclen */
137 0x0000 + /* Load Offset */
139 (segaddr
& 0xff) + /* USBA low */
140 ((segaddr
>> 8) & 0xff); /* USBA high */
141 checksum
= ~checksum
+ 1; /* two's-complement the checksum */
142 fprintf(of
, "%02X\n", checksum
& 0xff);
144 /* See if there's a '_main' symbol in the symbol table */
145 if ((s
= symtabFind(m
->symtab
, "_main")) == NULL
) {
147 ("No _main symbol found, no start segment address record added\n");
149 printf("_main symbol found at %04x:%04x\n", s
->segment
,
150 (unsigned int)(s
->offset
& 0xffff));
151 /* Create a start segment address record for the _main symbol. */
152 segaddr
= ((s
->segment
& 0xffff) << 16) + ((s
->offset
) & 0xffff);
153 fprintf(of
, ":04000003"); /* Record mark, reclen, load offset & rectyp
154 fields for start seg. addr. record */
155 fprintf(of
, "%08"PRIX32
"", segaddr
); /* CS/IP field */
156 checksum
= 0x04 + /* reclen */
157 0x0000 + /* load offset */
159 (segaddr
& 0xff) + /* low-low byte of segaddr */
160 ((segaddr
>> 8) & 0xff) + /* low-high byte of segaddr */
161 ((segaddr
>> 16) & 0xff) + /* high-low byte of segaddr */
162 ((segaddr
>> 24) & 0xff); /* high-high byte of segaddr */
163 checksum
= ~checksum
+ 1; /* two's complement */
164 fprintf(of
, "%02X\n", checksum
& 0xff);
167 /* Now it's time to write data records from the code and data segments in.
168 This current version doesn't check for segment overflow; proper behavior
169 should be to output a segment address record for the code and data
170 segments. Something to do. */
174 for (segn
= 0; segn
< 2; segn
++) {
177 if (m
->f
.seg
[segn
].length
== 0)
179 for (i
= 0; i
+ 15 < m
->f
.seg
[segn
].length
; i
+= 16) {
180 ofs
= write_data_record(of
, ofs
, 16, &segbin
[segn
][i
]);
182 if ((mod
= m
->f
.seg
[segn
].length
& 0x000f) != 0) {
183 adr
= m
->f
.seg
[segn
].length
& 0xfff0;
184 ofs
= write_data_record(of
, ofs
, mod
, &segbin
[segn
][adr
]);
187 /* output an end of file record */
188 fprintf(of
, ":00000001FF\n");