summaryrefslogtreecommitdiffstats
path: root/contrib/bin2intelhex/bin2intelhex.c
blob: 75b88c1576495cc8e8b0ba5e4055e77519bb245d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* name : bin2intelhex.c
 * from : Jean Marc Lacroix <jeanmarc.lacroix@free.fr>
 * date : 06/12/1997.
 * abstract : Y have rewrite this program from ????? with some modifications
 * to add :
 * - the Intel specification.
 * - correct a bug because my prom programmer don't understand the
 * initial format. Y suspect a bug in the calcul of the lrc
 * in the original program.
 * - correct the format of printf . In the original program, it was
 *   %x, and it is in fact %X, because in the Intel Format, all the
 * char are in upper case.
 * - correct the lrc calculation.
 * usage:
 *-------
 * this program read the standard input and put to the standard output
 * the result of the conversion.
 * an example of use :
 * cat my_bin | bin2intelhex > my_bin.hex or.....
 * bin2intelhex < my_bin > my_bin.hex
 */


/*
 * $Id$
 * $Log$
 * Revision 1.1  2005/05/17 16:45:06  mcb30
 * Initial revision
 *
 * Revision 1.9  1997/12/14 05:14:54  install
 * - some documentation....
 *
 */

#include <stdio.h>
#include <unistd.h>

/* Intel Hex format specifications

The 8-bit Intel Hex File Format is a printable ASCII format consisting of one
 or more data records followed by an end of file record. Each
record consists of one line of information. Data records may appear in any
 order. Address and data values are represented as 2 or 4 hexadecimal
digit values. 

Record Format 
:LLAAAARRDDDD......DDDDCC 


LL
AAAA
RR
DD
CC
Length field. Number of data bytes.
Address field. Address of first byte.
Record type field. 00 for data and 01 for end of record.
Data field.
Checksum field. One's complement of length, address, record type and data
 fields modulo 256. 
CC = LL + AAAA + RR + all DD = 0

Example: 
:06010000010203040506E4 
:00000001FF 

The first line in the above example Intel Hex file is a data record addressed
 at location 100H with data values 1 to 6. The second line is the end
of file record, so that the LL field is 0

*/


typedef unsigned char t_u8;
typedef unsigned short t_u16;
/*
 * the choice for the total length (16) of a line, but the specification
 * can support an another value
 */
#define LL_MAX_LINE 16
typedef struct 
{ 
  t_u8 intel_lg_data;
  t_u16 intel_adr;
  t_u8 intel_type;
  t_u8 intel_data [LL_MAX_LINE];
  t_u8 intel_lrc;
} t_one_line;
#define INTEL_DATA_TYPE 0
#define EXIT_OK 0
int main (const int argc, const char ** const argv)
{
  t_one_line line;
  /*
   * init for the adress, please note that it is assume that the program begin at 0
   */
  line.intel_adr = 0;
  line.intel_type = INTEL_DATA_TYPE;
  /*
   * read the data on the standard input
   */
  while ((line.intel_lg_data = read (0, &line.intel_data [0] ,LL_MAX_LINE )) > 0) 
    {
      t_u8 i; 
      /*
       * and now for this line, calculate the lrc.
       */
      line.intel_lrc = line.intel_lg_data;
      line.intel_lrc += ((line.intel_adr >> 8) & 0xff);
      line.intel_lrc += (line.intel_adr &0xff);
      line.intel_lrc += line.intel_type;
      /*
       * the structure is ready, print it to stdout in the
       * right format
       */
      (void) printf (":%02X%04X%02X",
		     line.intel_lg_data,
		     line.intel_adr,
		     line.intel_type);
      /*
       * edit all the data read
       */
      for (i=0; i<line.intel_lg_data; i++)
	{
	  (void) printf ("%02X",
			 (line.intel_data [i] & 0xff));	  
	  /*
	   * add to the lrc the data print
	   */
	  line.intel_lrc +=line.intel_data [i];
	}
      /*
       * edit the value of the lrc and new line for the next
       */
      (void) printf ("%02X\n",
			 (0x100 - line.intel_lrc) & 0xff);
      /* 
       * prepare the new adress for the next line
       */
      line.intel_adr+=line.intel_lg_data;     
    }
  /*
   * print the last line with a length of 0 data, so that the lrc is easy to
   * calculate (ff+01 =0)
   */
  printf (":00000001FF\n");
  exit (EXIT_OK); 
}