You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bin2intelhex.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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
  21. */
  22. /*
  23. * $Id$
  24. * $Log$
  25. * Revision 1.1 2005/05/17 16:45:06 mcb30
  26. * Initial revision
  27. *
  28. * Revision 1.9 1997/12/14 05:14:54 install
  29. * - some documentation....
  30. *
  31. */
  32. #include <stdio.h>
  33. #include <unistd.h>
  34. /* Intel Hex format specifications
  35. The 8-bit Intel Hex File Format is a printable ASCII format consisting of one
  36. or more data records followed by an end of file record. Each
  37. record consists of one line of information. Data records may appear in any
  38. order. Address and data values are represented as 2 or 4 hexadecimal
  39. digit values.
  40. Record Format
  41. :LLAAAARRDDDD......DDDDCC
  42. LL
  43. AAAA
  44. RR
  45. DD
  46. CC
  47. Length field. Number of data bytes.
  48. Address field. Address of first byte.
  49. Record type field. 00 for data and 01 for end of record.
  50. Data field.
  51. Checksum field. One's complement of length, address, record type and data
  52. fields modulo 256.
  53. CC = LL + AAAA + RR + all DD = 0
  54. Example:
  55. :06010000010203040506E4
  56. :00000001FF
  57. The first line in the above example Intel Hex file is a data record addressed
  58. at location 100H with data values 1 to 6. The second line is the end
  59. of file record, so that the LL field is 0
  60. */
  61. typedef unsigned char t_u8;
  62. typedef unsigned short t_u16;
  63. /*
  64. * the choice for the total length (16) of a line, but the specification
  65. * can support an another value
  66. */
  67. #define LL_MAX_LINE 16
  68. typedef struct
  69. {
  70. t_u8 intel_lg_data;
  71. t_u16 intel_adr;
  72. t_u8 intel_type;
  73. t_u8 intel_data [LL_MAX_LINE];
  74. t_u8 intel_lrc;
  75. } t_one_line;
  76. #define INTEL_DATA_TYPE 0
  77. #define EXIT_OK 0
  78. int main (const int argc, const char ** const argv)
  79. {
  80. t_one_line line;
  81. /*
  82. * init for the adress, please note that it is assume that the program begin at 0
  83. */
  84. line.intel_adr = 0;
  85. line.intel_type = INTEL_DATA_TYPE;
  86. /*
  87. * read the data on the standard input
  88. */
  89. while ((line.intel_lg_data = read (0, &line.intel_data [0] ,LL_MAX_LINE )) > 0)
  90. {
  91. t_u8 i;
  92. /*
  93. * and now for this line, calculate the lrc.
  94. */
  95. line.intel_lrc = line.intel_lg_data;
  96. line.intel_lrc += ((line.intel_adr >> 8) & 0xff);
  97. line.intel_lrc += (line.intel_adr &0xff);
  98. line.intel_lrc += line.intel_type;
  99. /*
  100. * the structure is ready, print it to stdout in the
  101. * right format
  102. */
  103. (void) printf (":%02X%04X%02X",
  104. line.intel_lg_data,
  105. line.intel_adr,
  106. line.intel_type);
  107. /*
  108. * edit all the data read
  109. */
  110. for (i=0; i<line.intel_lg_data; i++)
  111. {
  112. (void) printf ("%02X",
  113. (line.intel_data [i] & 0xff));
  114. /*
  115. * add to the lrc the data print
  116. */
  117. line.intel_lrc +=line.intel_data [i];
  118. }
  119. /*
  120. * edit the value of the lrc and new line for the next
  121. */
  122. (void) printf ("%02X\n",
  123. (0x100 - line.intel_lrc) & 0xff);
  124. /*
  125. * prepare the new adress for the next line
  126. */
  127. line.intel_adr+=line.intel_lg_data;
  128. }
  129. /*
  130. * print the last line with a length of 0 data, so that the lrc is easy to
  131. * calculate (ff+01 =0)
  132. */
  133. printf (":00000001FF\n");
  134. exit (EXIT_OK);
  135. }