added stdio.h to includes for DBG compilation
[gpxe.git] / contrib / bin2intelhex / bin2intelhex.c
blob75b88c1576495cc8e8b0ba5e4055e77519bb245d
1 /* name : bin2intelhex.c
2 * from : Jean Marc Lacroix <jeanmarc.lacroix@free.fr>
3 * date : 06/12/1997.
4 * abstract : Y have rewrite this program from ????? with some modifications
5 * to add :
6 * - the Intel specification.
7 * - correct a bug because my prom programmer don't understand the
8 * initial format. Y suspect a bug in the calcul of the lrc
9 * in the original program.
10 * - correct the format of printf . In the original program, it was
11 * %x, and it is in fact %X, because in the Intel Format, all the
12 * char are in upper case.
13 * - correct the lrc calculation.
14 * usage:
15 *-------
16 * this program read the standard input and put to the standard output
17 * the result of the conversion.
18 * an example of use :
19 * cat my_bin | bin2intelhex > my_bin.hex or.....
20 * bin2intelhex < my_bin > my_bin.hex
25 * $Id$
26 * $Log$
27 * Revision 1.1 2005/05/17 16:45:06 mcb30
28 * Initial revision
30 * Revision 1.9 1997/12/14 05:14:54 install
31 * - some documentation....
35 #include <stdio.h>
36 #include <unistd.h>
38 /* Intel Hex format specifications
40 The 8-bit Intel Hex File Format is a printable ASCII format consisting of one
41 or more data records followed by an end of file record. Each
42 record consists of one line of information. Data records may appear in any
43 order. Address and data values are represented as 2 or 4 hexadecimal
44 digit values.
46 Record Format
47 :LLAAAARRDDDD......DDDDCC
51 AAAA
55 Length field. Number of data bytes.
56 Address field. Address of first byte.
57 Record type field. 00 for data and 01 for end of record.
58 Data field.
59 Checksum field. One's complement of length, address, record type and data
60 fields modulo 256.
61 CC = LL + AAAA + RR + all DD = 0
63 Example:
64 :06010000010203040506E4
65 :00000001FF
67 The first line in the above example Intel Hex file is a data record addressed
68 at location 100H with data values 1 to 6. The second line is the end
69 of file record, so that the LL field is 0
74 typedef unsigned char t_u8;
75 typedef unsigned short t_u16;
77 * the choice for the total length (16) of a line, but the specification
78 * can support an another value
80 #define LL_MAX_LINE 16
81 typedef struct
83 t_u8 intel_lg_data;
84 t_u16 intel_adr;
85 t_u8 intel_type;
86 t_u8 intel_data [LL_MAX_LINE];
87 t_u8 intel_lrc;
88 } t_one_line;
89 #define INTEL_DATA_TYPE 0
90 #define EXIT_OK 0
91 int main (const int argc, const char ** const argv)
93 t_one_line line;
95 * init for the adress, please note that it is assume that the program begin at 0
97 line.intel_adr = 0;
98 line.intel_type = INTEL_DATA_TYPE;
100 * read the data on the standard input
102 while ((line.intel_lg_data = read (0, &line.intel_data [0] ,LL_MAX_LINE )) > 0)
104 t_u8 i;
106 * and now for this line, calculate the lrc.
108 line.intel_lrc = line.intel_lg_data;
109 line.intel_lrc += ((line.intel_adr >> 8) & 0xff);
110 line.intel_lrc += (line.intel_adr &0xff);
111 line.intel_lrc += line.intel_type;
113 * the structure is ready, print it to stdout in the
114 * right format
116 (void) printf (":%02X%04X%02X",
117 line.intel_lg_data,
118 line.intel_adr,
119 line.intel_type);
121 * edit all the data read
123 for (i=0; i<line.intel_lg_data; i++)
125 (void) printf ("%02X",
126 (line.intel_data [i] & 0xff));
128 * add to the lrc the data print
130 line.intel_lrc +=line.intel_data [i];
133 * edit the value of the lrc and new line for the next
135 (void) printf ("%02X\n",
136 (0x100 - line.intel_lrc) & 0xff);
138 * prepare the new adress for the next line
140 line.intel_adr+=line.intel_lg_data;
143 * print the last line with a length of 0 data, so that the lrc is easy to
144 * calculate (ff+01 =0)
146 printf (":00000001FF\n");
147 exit (EXIT_OK);