1 /* rdf2ihx: convert an RDOFF object file to Intel Hex format. This is based
2 on rdf2bin. Note that this program only writes 16-bit HEX. */
16 /* This function writes a single n-byte data record to of. Maximum value
18 static int write_data_record(FILE *of
, int ofs
, int nbytes
,
22 unsigned int checksum
;
25 fprintf(of
, ":%02X%04X00", nbytes
, ofs
);
27 for (i
=0; i
<nbytes
; i
++) {
28 fprintf(of
, "%02X", data
[i
]);
32 checksum
= checksum
+ /* current checksum */
33 nbytes
+ /* RECLEN (one byte) */
34 ((iofs
>> 8) & 0xff) + /* high byte of load offset */
35 (iofs
& 0xff); /* low byte of load offset */
36 checksum
= ~checksum
+ 1;
37 fprintf(of
, "%02X\n", checksum
&0xff);
41 int main(int argc
, char **argv
)
47 unsigned char *segbin
[2];
48 int pad
[2], segn
, ofs
, i
;
50 unsigned int checksum
;
54 puts("Usage: rdf2ihx [-o relocation-origin] [-p segment-alignment] "
55 "input-file output-file");
62 if (strcmp(*argv
,"-o") == 0) {
64 origin
= readnum(*argv
, &tmp
);
66 fprintf(stderr
,"rdf2ihx: invalid parameter: %s\n",*argv
);
69 } else if (strcmp(*argv
,"-p") == 0) {
71 align
= readnum(*argv
, &tmp
);
73 fprintf(stderr
,"rdf2ihx: invalid parameter: %s\n",*argv
);
81 puts("rdf2bin: required parameter missing");
87 rdfperror("rdf2bin",*argv
);
90 printf("relocating %s: origin=%lx, align=%d\n",*argv
, origin
, align
);
93 m
->datarel
= origin
+ m
->f
.seg
[0].length
;
94 if (m
->datarel
% align
!= 0) {
95 pad
[0] = align
- (m
->datarel
% align
);
101 m
->bssrel
= m
->datarel
+ m
->f
.seg
[1].length
;
102 if (m
->bssrel
% align
!= 0) {
103 pad
[1] = align
- (m
->bssrel
% align
);
109 printf("code: %08lx\ndata: %08lx\nbss: %08lx\n",
110 m
->textrel
, m
->datarel
, m
->bssrel
);
116 of
= fopen(*argv
,"w");
118 fprintf(stderr
,"rdf2ihx: could not open output file %s\n",*argv
);
122 padding
= malloc(align
);
124 fprintf(stderr
,"rdf2ihx: out of memory\n");
128 /* write extended segment address record */
129 fprintf(of
, ":02000002"); /* Record mark, reclen, load offset & rectyp
130 fields for ext. seg. address record */
131 segaddr
= ((origin
>> 16) & 0xffff); /* segment address */
132 fprintf(of
, "%04X", (unsigned int)(segaddr
& 0xffff));
133 checksum
= 0x02 + /* reclen */
134 0x0000 + /* Load Offset */
136 (segaddr
& 0xff) + /* USBA low */
137 ((segaddr
>> 8) & 0xff); /* USBA high */
138 checksum
= ~checksum
+ 1; /* two's-complement the checksum */
139 fprintf(of
, "%02X\n", checksum
& 0xff);
141 /* See if there's a '_main' symbol in the symbol table */
142 if ((s
=symtabFind(m
->symtab
, "_main")) == NULL
) {
143 printf("No _main symbol found, no start segment address record added\n");
145 printf("_main symbol found at %04x:%04x\n", s
->segment
,
146 (unsigned int)(s
->offset
& 0xffff));
147 /* Create a start segment address record for the _main symbol. */
148 segaddr
= ((s
->segment
& 0xffff) << 16) + ((s
->offset
) & 0xffff);
149 fprintf(of
, ":04000003"); /* Record mark, reclen, load offset & rectyp
150 fields for start seg. addr. record */
151 fprintf(of
, "%08lX", segaddr
); /* CS/IP field */
152 checksum
= 0x04 + /* reclen */
153 0x0000 + /* load offset */
155 (segaddr
& 0xff) + /* low-low byte of segaddr */
156 ((segaddr
>> 8) & 0xff) + /* low-high byte of segaddr */
157 ((segaddr
>> 16) & 0xff) + /* high-low byte of segaddr */
158 ((segaddr
>> 24) & 0xff); /* high-high byte of segaddr */
159 checksum
= ~checksum
+ 1; /* two's complement */
160 fprintf(of
, "%02X\n", checksum
& 0xff);
163 /* Now it's time to write data records from the code and data segments in.
164 This current version doesn't check for segment overflow; proper behavior
165 should be to output a segment address record for the code and data
166 segments. Something to do. */
170 for (segn
=0; segn
<2; segn
++) {
173 if (m
->f
.seg
[segn
].length
== 0)
175 for (i
=0; i
+15<m
->f
.seg
[segn
].length
; i
+=16) {
176 ofs
= write_data_record(of
, ofs
, 16, &segbin
[segn
][i
]);
178 if ((mod
=m
->f
.seg
[segn
].length
& 0x000f) != 0) {
179 adr
= m
->f
.seg
[segn
].length
& 0xfff0;
180 ofs
= write_data_record(of
, ofs
, mod
, &segbin
[segn
][adr
]);
183 /* output an end of file record */
184 fprintf(of
, ":00000001FF\n");