Fork of the vendor (Boundary Devices) u-boot for Reform 2, with minor tweaks. The goal is to migrate to mainstream u-boot or barebox ASAP. The main impediment so far is the 4GB RAM config.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 
 
 
 

373 lines
9.7 KiB

  1. /*************************************************************************
  2. | COPYRIGHT (c) 2000 BY ABATRON AG
  3. |*************************************************************************
  4. |
  5. | PROJECT NAME: Linux Image to S-record Conversion Utility
  6. | FILENAME : img2srec.c
  7. |
  8. | COMPILER : GCC
  9. |
  10. | TARGET OS : LINUX / UNIX
  11. | TARGET HW : -
  12. |
  13. | PROGRAMMER : Abatron / RD
  14. | CREATION : 07.07.00
  15. |
  16. |*************************************************************************
  17. |
  18. | DESCRIPTION :
  19. |
  20. | Utility to convert a Linux Boot Image to S-record:
  21. | ==================================================
  22. |
  23. | This command line utility can be used to convert a Linux boot image
  24. | (zimage.initrd) to S-Record format used for flash programming.
  25. | This conversion takes care of the special sections "IMAGE" and INITRD".
  26. |
  27. | img2srec [-o offset] image > image.srec
  28. |
  29. |
  30. | Build the utility:
  31. | ==================
  32. |
  33. | To build the utility use GCC as follows:
  34. |
  35. | gcc img2srec.c -o img2srec
  36. |
  37. |
  38. |*************************************************************************
  39. |
  40. |
  41. | UPDATES :
  42. |
  43. | DATE NAME CHANGES
  44. | -----------------------------------------------------------
  45. | Latest update
  46. |
  47. | 07.07.00 aba Initial release
  48. |
  49. |*************************************************************************/
  50. /*************************************************************************
  51. | INCLUDES
  52. |*************************************************************************/
  53. #include "os_support.h"
  54. #include <stdbool.h>
  55. #include <stddef.h>
  56. #include <stdio.h>
  57. #include <stdlib.h>
  58. #include <ctype.h>
  59. #include <string.h>
  60. #include <elf.h>
  61. #include <unistd.h>
  62. #include <errno.h>
  63. /*************************************************************************
  64. | FUNCTIONS
  65. |*************************************************************************/
  66. static char* ExtractHex (uint32_t* value, char* getPtr)
  67. {
  68. uint32_t num;
  69. uint32_t digit;
  70. uint8_t c;
  71. while (*getPtr == ' ') getPtr++;
  72. num = 0;
  73. for (;;) {
  74. c = *getPtr;
  75. if ((c >= '0') && (c <= '9')) digit = (uint32_t)(c - '0');
  76. else if ((c >= 'A') && (c <= 'F')) digit = (uint32_t)(c - 'A' + 10);
  77. else if ((c >= 'a') && (c <= 'f')) digit = (uint32_t)(c - 'a' + 10);
  78. else break;
  79. num <<= 4;
  80. num += digit;
  81. getPtr++;
  82. } /* for */
  83. *value = num;
  84. return getPtr;
  85. } /* ExtractHex */
  86. static char* ExtractDecimal (uint32_t* value, char* getPtr)
  87. {
  88. uint32_t num;
  89. uint32_t digit;
  90. uint8_t c;
  91. while (*getPtr == ' ') getPtr++;
  92. num = 0;
  93. for (;;) {
  94. c = *getPtr;
  95. if ((c >= '0') && (c <= '9')) digit = (uint32_t)(c - '0');
  96. else break;
  97. num *= 10;
  98. num += digit;
  99. getPtr++;
  100. } /* for */
  101. *value = num;
  102. return getPtr;
  103. } /* ExtractDecimal */
  104. static void ExtractNumber (uint32_t* value, char* getPtr)
  105. {
  106. bool neg = false;;
  107. while (*getPtr == ' ') getPtr++;
  108. if (*getPtr == '-') {
  109. neg = true;
  110. getPtr++;
  111. } /* if */
  112. if ((*getPtr == '0') && ((*(getPtr+1) == 'x') || (*(getPtr+1) == 'X'))) {
  113. getPtr +=2;
  114. (void)ExtractHex(value, getPtr);
  115. } /* if */
  116. else {
  117. (void)ExtractDecimal(value, getPtr);
  118. } /* else */
  119. if (neg) *value = -(*value);
  120. } /* ExtractNumber */
  121. static uint8_t* ExtractWord(uint16_t* value, uint8_t* buffer)
  122. {
  123. uint16_t x;
  124. x = (uint16_t)*buffer++;
  125. x = (x<<8) + (uint16_t)*buffer++;
  126. *value = x;
  127. return buffer;
  128. } /* ExtractWord */
  129. static uint8_t* ExtractLong(uint32_t* value, uint8_t* buffer)
  130. {
  131. uint32_t x;
  132. x = (uint32_t)*buffer++;
  133. x = (x<<8) + (uint32_t)*buffer++;
  134. x = (x<<8) + (uint32_t)*buffer++;
  135. x = (x<<8) + (uint32_t)*buffer++;
  136. *value = x;
  137. return buffer;
  138. } /* ExtractLong */
  139. static uint8_t* ExtractBlock(uint16_t count, uint8_t* data, uint8_t* buffer)
  140. {
  141. while (count--) *data++ = *buffer++;
  142. return buffer;
  143. } /* ExtractBlock */
  144. static char* WriteHex(char* pa, uint8_t value, uint16_t* pCheckSum)
  145. {
  146. uint16_t temp;
  147. static char ByteToHex[] = "0123456789ABCDEF";
  148. *pCheckSum += value;
  149. temp = value / 16;
  150. *pa++ = ByteToHex[temp];
  151. temp = value % 16;
  152. *pa++ = ByteToHex[temp];
  153. return pa;
  154. }
  155. static char* BuildSRecord(char* pa, uint16_t sType, uint32_t addr,
  156. const uint8_t* data, int nCount)
  157. {
  158. uint16_t addrLen;
  159. uint16_t sRLen;
  160. uint16_t checkSum;
  161. uint16_t i;
  162. switch (sType) {
  163. case 0:
  164. case 1:
  165. case 9:
  166. addrLen = 2;
  167. break;
  168. case 2:
  169. case 8:
  170. addrLen = 3;
  171. break;
  172. case 3:
  173. case 7:
  174. addrLen = 4;
  175. break;
  176. default:
  177. return pa;
  178. } /* switch */
  179. *pa++ = 'S';
  180. *pa++ = (char)(sType + '0');
  181. sRLen = addrLen + nCount + 1;
  182. checkSum = 0;
  183. pa = WriteHex(pa, (uint8_t)sRLen, &checkSum);
  184. /* Write address field */
  185. for (i = 1; i <= addrLen; i++) {
  186. pa = WriteHex(pa, (uint8_t)(addr >> (8 * (addrLen - i))), &checkSum);
  187. } /* for */
  188. /* Write code/data fields */
  189. for (i = 0; i < nCount; i++) {
  190. pa = WriteHex(pa, *data++, &checkSum);
  191. } /* for */
  192. /* Write checksum field */
  193. checkSum = ~checkSum;
  194. pa = WriteHex(pa, (uint8_t)checkSum, &checkSum);
  195. *pa++ = '\0';
  196. return pa;
  197. }
  198. static void ConvertELF(char* fileName, uint32_t loadOffset)
  199. {
  200. FILE* file;
  201. int i;
  202. int rxCount;
  203. uint8_t rxBlock[1024];
  204. uint32_t loadSize;
  205. uint32_t firstAddr;
  206. uint32_t loadAddr;
  207. uint32_t loadDiff = 0;
  208. Elf32_Ehdr elfHeader;
  209. Elf32_Shdr sectHeader[32];
  210. uint8_t* getPtr;
  211. char srecLine[128];
  212. char *hdr_name;
  213. /* open file */
  214. if ((file = fopen(fileName,"rb")) == NULL) {
  215. fprintf (stderr, "Can't open %s: %s\n", fileName, strerror(errno));
  216. return;
  217. } /* if */
  218. /* read ELF header */
  219. rxCount = fread(rxBlock, 1, sizeof elfHeader, file);
  220. getPtr = ExtractBlock(sizeof elfHeader.e_ident, elfHeader.e_ident, rxBlock);
  221. getPtr = ExtractWord(&elfHeader.e_type, getPtr);
  222. getPtr = ExtractWord(&elfHeader.e_machine, getPtr);
  223. getPtr = ExtractLong((uint32_t *)&elfHeader.e_version, getPtr);
  224. getPtr = ExtractLong((uint32_t *)&elfHeader.e_entry, getPtr);
  225. getPtr = ExtractLong((uint32_t *)&elfHeader.e_phoff, getPtr);
  226. getPtr = ExtractLong((uint32_t *)&elfHeader.e_shoff, getPtr);
  227. getPtr = ExtractLong((uint32_t *)&elfHeader.e_flags, getPtr);
  228. getPtr = ExtractWord(&elfHeader.e_ehsize, getPtr);
  229. getPtr = ExtractWord(&elfHeader.e_phentsize, getPtr);
  230. getPtr = ExtractWord(&elfHeader.e_phnum, getPtr);
  231. getPtr = ExtractWord(&elfHeader.e_shentsize, getPtr);
  232. getPtr = ExtractWord(&elfHeader.e_shnum, getPtr);
  233. getPtr = ExtractWord(&elfHeader.e_shstrndx, getPtr);
  234. if ( (rxCount != sizeof elfHeader)
  235. || (elfHeader.e_ident[0] != ELFMAG0)
  236. || (elfHeader.e_ident[1] != ELFMAG1)
  237. || (elfHeader.e_ident[2] != ELFMAG2)
  238. || (elfHeader.e_ident[3] != ELFMAG3)
  239. || (elfHeader.e_type != ET_EXEC)
  240. ) {
  241. fclose(file);
  242. fprintf (stderr, "*** illegal file format\n");
  243. return;
  244. } /* if */
  245. /* read all section headers */
  246. fseek(file, elfHeader.e_shoff, SEEK_SET);
  247. for (i = 0; i < elfHeader.e_shnum; i++) {
  248. rxCount = fread(rxBlock, 1, sizeof sectHeader[0], file);
  249. getPtr = ExtractLong((uint32_t *)&sectHeader[i].sh_name, rxBlock);
  250. getPtr = ExtractLong((uint32_t *)&sectHeader[i].sh_type, getPtr);
  251. getPtr = ExtractLong((uint32_t *)&sectHeader[i].sh_flags, getPtr);
  252. getPtr = ExtractLong((uint32_t *)&sectHeader[i].sh_addr, getPtr);
  253. getPtr = ExtractLong((uint32_t *)&sectHeader[i].sh_offset, getPtr);
  254. getPtr = ExtractLong((uint32_t *)&sectHeader[i].sh_size, getPtr);
  255. getPtr = ExtractLong((uint32_t *)&sectHeader[i].sh_link, getPtr);
  256. getPtr = ExtractLong((uint32_t *)&sectHeader[i].sh_info, getPtr);
  257. getPtr = ExtractLong((uint32_t *)&sectHeader[i].sh_addralign, getPtr);
  258. getPtr = ExtractLong((uint32_t *)&sectHeader[i].sh_entsize, getPtr);
  259. if (rxCount != sizeof sectHeader[0]) {
  260. fclose(file);
  261. fprintf (stderr, "*** illegal file format\n");
  262. return;
  263. } /* if */
  264. } /* for */
  265. if ((hdr_name = strrchr(fileName, '/')) == NULL) {
  266. hdr_name = fileName;
  267. } else {
  268. ++hdr_name;
  269. }
  270. /* write start record */
  271. (void)BuildSRecord(srecLine, 0, 0, (uint8_t *)hdr_name, strlen(hdr_name));
  272. printf("%s\r\n",srecLine);
  273. /* write data records */
  274. firstAddr = ~0;
  275. loadAddr = 0;
  276. for (i = 0; i < elfHeader.e_shnum; i++) {
  277. if ( (sectHeader[i].sh_type == SHT_PROGBITS)
  278. && (sectHeader[i].sh_size != 0)
  279. ) {
  280. loadSize = sectHeader[i].sh_size;
  281. if (sectHeader[i].sh_flags != 0) {
  282. loadAddr = sectHeader[i].sh_addr;
  283. loadDiff = loadAddr - sectHeader[i].sh_offset;
  284. } /* if */
  285. else {
  286. loadAddr = sectHeader[i].sh_offset + loadDiff;
  287. } /* else */
  288. if (loadAddr < firstAddr)
  289. firstAddr = loadAddr;
  290. /* build s-records */
  291. loadSize = sectHeader[i].sh_size;
  292. fseek(file, sectHeader[i].sh_offset, SEEK_SET);
  293. while (loadSize) {
  294. rxCount = fread(rxBlock, 1, (loadSize > 32) ? 32 : loadSize, file);
  295. if (rxCount < 0) {
  296. fclose(file);
  297. fprintf (stderr, "*** illegal file format\n");
  298. return;
  299. } /* if */
  300. (void)BuildSRecord(srecLine, 3, loadAddr + loadOffset, rxBlock, rxCount);
  301. loadSize -= rxCount;
  302. loadAddr += rxCount;
  303. printf("%s\r\n",srecLine);
  304. } /* while */
  305. } /* if */
  306. } /* for */
  307. /* add end record */
  308. (void)BuildSRecord(srecLine, 7, firstAddr + loadOffset, 0, 0);
  309. printf("%s\r\n",srecLine);
  310. fclose(file);
  311. } /* ConvertELF */
  312. /*************************************************************************
  313. | MAIN
  314. |*************************************************************************/
  315. int main( int argc, char *argv[ ])
  316. {
  317. uint32_t offset;
  318. if (argc == 2) {
  319. ConvertELF(argv[1], 0);
  320. } /* if */
  321. else if ((argc == 4) && (strcmp(argv[1], "-o") == 0)) {
  322. ExtractNumber(&offset, argv[2]);
  323. ConvertELF(argv[3], offset);
  324. } /* if */
  325. else {
  326. fprintf (stderr, "Usage: img2srec [-o offset] <image>\n");
  327. } /* if */
  328. return 0;
  329. } /* main */