wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 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 | /************************************************************************* |
| 52 | | INCLUDES |
| 53 | |*************************************************************************/ |
| 54 | |
Mike Frysinger | 4ad8e9f | 2009-07-02 19:23:25 -0400 | [diff] [blame] | 55 | #include "os_support.h" |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 56 | #include <stdbool.h> |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 57 | #include <stddef.h> |
| 58 | #include <stdio.h> |
| 59 | #include <stdlib.h> |
| 60 | #include <ctype.h> |
| 61 | #include <string.h> |
| 62 | #include <elf.h> |
| 63 | #include <unistd.h> |
| 64 | #include <errno.h> |
| 65 | |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 66 | /************************************************************************* |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 67 | | FUNCTIONS |
| 68 | |*************************************************************************/ |
| 69 | |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 70 | static char* ExtractHex (uint32_t* value, char* getPtr) |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 71 | { |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 72 | uint32_t num; |
| 73 | uint32_t digit; |
| 74 | uint8_t c; |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 75 | |
| 76 | while (*getPtr == ' ') getPtr++; |
| 77 | num = 0; |
| 78 | for (;;) { |
| 79 | c = *getPtr; |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 80 | if ((c >= '0') && (c <= '9')) digit = (uint32_t)(c - '0'); |
| 81 | else if ((c >= 'A') && (c <= 'F')) digit = (uint32_t)(c - 'A' + 10); |
| 82 | else if ((c >= 'a') && (c <= 'f')) digit = (uint32_t)(c - 'a' + 10); |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 83 | else break; |
| 84 | num <<= 4; |
| 85 | num += digit; |
| 86 | getPtr++; |
| 87 | } /* for */ |
| 88 | *value = num; |
| 89 | return getPtr; |
| 90 | } /* ExtractHex */ |
| 91 | |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 92 | static char* ExtractDecimal (uint32_t* value, char* getPtr) |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 93 | { |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 94 | uint32_t num; |
| 95 | uint32_t digit; |
| 96 | uint8_t c; |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 97 | |
| 98 | while (*getPtr == ' ') getPtr++; |
| 99 | num = 0; |
| 100 | for (;;) { |
| 101 | c = *getPtr; |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 102 | if ((c >= '0') && (c <= '9')) digit = (uint32_t)(c - '0'); |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 103 | else break; |
| 104 | num *= 10; |
| 105 | num += digit; |
| 106 | getPtr++; |
| 107 | } /* for */ |
| 108 | *value = num; |
| 109 | return getPtr; |
| 110 | } /* ExtractDecimal */ |
| 111 | |
| 112 | |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 113 | static void ExtractNumber (uint32_t* value, char* getPtr) |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 114 | { |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 115 | bool neg = false;; |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 116 | |
| 117 | while (*getPtr == ' ') getPtr++; |
| 118 | if (*getPtr == '-') { |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 119 | neg = true; |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 120 | getPtr++; |
| 121 | } /* if */ |
| 122 | if ((*getPtr == '0') && ((*(getPtr+1) == 'x') || (*(getPtr+1) == 'X'))) { |
| 123 | getPtr +=2; |
| 124 | (void)ExtractHex(value, getPtr); |
| 125 | } /* if */ |
| 126 | else { |
| 127 | (void)ExtractDecimal(value, getPtr); |
| 128 | } /* else */ |
| 129 | if (neg) *value = -(*value); |
| 130 | } /* ExtractNumber */ |
| 131 | |
| 132 | |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 133 | static uint8_t* ExtractWord(uint16_t* value, uint8_t* buffer) |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 134 | { |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 135 | uint16_t x; |
| 136 | x = (uint16_t)*buffer++; |
| 137 | x = (x<<8) + (uint16_t)*buffer++; |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 138 | *value = x; |
| 139 | return buffer; |
| 140 | } /* ExtractWord */ |
| 141 | |
| 142 | |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 143 | static uint8_t* ExtractLong(uint32_t* value, uint8_t* buffer) |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 144 | { |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 145 | uint32_t x; |
| 146 | x = (uint32_t)*buffer++; |
| 147 | x = (x<<8) + (uint32_t)*buffer++; |
| 148 | x = (x<<8) + (uint32_t)*buffer++; |
| 149 | x = (x<<8) + (uint32_t)*buffer++; |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 150 | *value = x; |
| 151 | return buffer; |
| 152 | } /* ExtractLong */ |
| 153 | |
| 154 | |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 155 | static uint8_t* ExtractBlock(uint16_t count, uint8_t* data, uint8_t* buffer) |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 156 | { |
| 157 | while (count--) *data++ = *buffer++; |
| 158 | return buffer; |
| 159 | } /* ExtractBlock */ |
| 160 | |
| 161 | |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 162 | static char* WriteHex(char* pa, uint8_t value, uint16_t* pCheckSum) |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 163 | { |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 164 | uint16_t temp; |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 165 | |
| 166 | static char ByteToHex[] = "0123456789ABCDEF"; |
| 167 | |
| 168 | *pCheckSum += value; |
| 169 | temp = value / 16; |
| 170 | *pa++ = ByteToHex[temp]; |
| 171 | temp = value % 16; |
| 172 | *pa++ = ByteToHex[temp]; |
| 173 | return pa; |
| 174 | } |
| 175 | |
| 176 | |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 177 | static char* BuildSRecord(char* pa, uint16_t sType, uint32_t addr, |
| 178 | const uint8_t* data, int nCount) |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 179 | { |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 180 | uint16_t addrLen; |
| 181 | uint16_t sRLen; |
| 182 | uint16_t checkSum; |
| 183 | uint16_t i; |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 184 | |
| 185 | switch (sType) { |
| 186 | case 0: |
| 187 | case 1: |
| 188 | case 9: |
| 189 | addrLen = 2; |
| 190 | break; |
| 191 | case 2: |
| 192 | case 8: |
| 193 | addrLen = 3; |
| 194 | break; |
| 195 | case 3: |
| 196 | case 7: |
| 197 | addrLen = 4; |
| 198 | break; |
| 199 | default: |
| 200 | return pa; |
| 201 | } /* switch */ |
| 202 | |
| 203 | *pa++ = 'S'; |
| 204 | *pa++ = (char)(sType + '0'); |
| 205 | sRLen = addrLen + nCount + 1; |
| 206 | checkSum = 0; |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 207 | pa = WriteHex(pa, (uint8_t)sRLen, &checkSum); |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 208 | |
| 209 | /* Write address field */ |
| 210 | for (i = 1; i <= addrLen; i++) { |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 211 | pa = WriteHex(pa, (uint8_t)(addr >> (8 * (addrLen - i))), &checkSum); |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 212 | } /* for */ |
| 213 | |
| 214 | /* Write code/data fields */ |
| 215 | for (i = 0; i < nCount; i++) { |
| 216 | pa = WriteHex(pa, *data++, &checkSum); |
| 217 | } /* for */ |
| 218 | |
| 219 | /* Write checksum field */ |
| 220 | checkSum = ~checkSum; |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 221 | pa = WriteHex(pa, (uint8_t)checkSum, &checkSum); |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 222 | *pa++ = '\0'; |
| 223 | return pa; |
| 224 | } |
| 225 | |
| 226 | |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 227 | static void ConvertELF(char* fileName, uint32_t loadOffset) |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 228 | { |
| 229 | FILE* file; |
| 230 | int i; |
| 231 | int rxCount; |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 232 | uint8_t rxBlock[1024]; |
| 233 | uint32_t loadSize; |
| 234 | uint32_t firstAddr; |
| 235 | uint32_t loadAddr; |
| 236 | uint32_t loadDiff = 0; |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 237 | Elf32_Ehdr elfHeader; |
| 238 | Elf32_Shdr sectHeader[32]; |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 239 | uint8_t* getPtr; |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 240 | char srecLine[128]; |
| 241 | char *hdr_name; |
| 242 | |
| 243 | |
| 244 | /* open file */ |
| 245 | if ((file = fopen(fileName,"rb")) == NULL) { |
| 246 | fprintf (stderr, "Can't open %s: %s\n", fileName, strerror(errno)); |
| 247 | return; |
| 248 | } /* if */ |
| 249 | |
| 250 | /* read ELF header */ |
| 251 | rxCount = fread(rxBlock, 1, sizeof elfHeader, file); |
| 252 | getPtr = ExtractBlock(sizeof elfHeader.e_ident, elfHeader.e_ident, rxBlock); |
| 253 | getPtr = ExtractWord(&elfHeader.e_type, getPtr); |
| 254 | getPtr = ExtractWord(&elfHeader.e_machine, getPtr); |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 255 | getPtr = ExtractLong((uint32_t *)&elfHeader.e_version, getPtr); |
| 256 | getPtr = ExtractLong((uint32_t *)&elfHeader.e_entry, getPtr); |
| 257 | getPtr = ExtractLong((uint32_t *)&elfHeader.e_phoff, getPtr); |
| 258 | getPtr = ExtractLong((uint32_t *)&elfHeader.e_shoff, getPtr); |
| 259 | getPtr = ExtractLong((uint32_t *)&elfHeader.e_flags, getPtr); |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 260 | getPtr = ExtractWord(&elfHeader.e_ehsize, getPtr); |
| 261 | getPtr = ExtractWord(&elfHeader.e_phentsize, getPtr); |
| 262 | getPtr = ExtractWord(&elfHeader.e_phnum, getPtr); |
| 263 | getPtr = ExtractWord(&elfHeader.e_shentsize, getPtr); |
| 264 | getPtr = ExtractWord(&elfHeader.e_shnum, getPtr); |
| 265 | getPtr = ExtractWord(&elfHeader.e_shstrndx, getPtr); |
| 266 | if ( (rxCount != sizeof elfHeader) |
| 267 | || (elfHeader.e_ident[0] != ELFMAG0) |
| 268 | || (elfHeader.e_ident[1] != ELFMAG1) |
| 269 | || (elfHeader.e_ident[2] != ELFMAG2) |
| 270 | || (elfHeader.e_ident[3] != ELFMAG3) |
| 271 | || (elfHeader.e_type != ET_EXEC) |
| 272 | ) { |
| 273 | fclose(file); |
| 274 | fprintf (stderr, "*** illegal file format\n"); |
| 275 | return; |
| 276 | } /* if */ |
| 277 | |
| 278 | /* read all section headers */ |
| 279 | fseek(file, elfHeader.e_shoff, SEEK_SET); |
| 280 | for (i = 0; i < elfHeader.e_shnum; i++) { |
| 281 | rxCount = fread(rxBlock, 1, sizeof sectHeader[0], file); |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 282 | getPtr = ExtractLong((uint32_t *)§Header[i].sh_name, rxBlock); |
| 283 | getPtr = ExtractLong((uint32_t *)§Header[i].sh_type, getPtr); |
| 284 | getPtr = ExtractLong((uint32_t *)§Header[i].sh_flags, getPtr); |
| 285 | getPtr = ExtractLong((uint32_t *)§Header[i].sh_addr, getPtr); |
| 286 | getPtr = ExtractLong((uint32_t *)§Header[i].sh_offset, getPtr); |
| 287 | getPtr = ExtractLong((uint32_t *)§Header[i].sh_size, getPtr); |
| 288 | getPtr = ExtractLong((uint32_t *)§Header[i].sh_link, getPtr); |
| 289 | getPtr = ExtractLong((uint32_t *)§Header[i].sh_info, getPtr); |
| 290 | getPtr = ExtractLong((uint32_t *)§Header[i].sh_addralign, getPtr); |
| 291 | getPtr = ExtractLong((uint32_t *)§Header[i].sh_entsize, getPtr); |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 292 | if (rxCount != sizeof sectHeader[0]) { |
| 293 | fclose(file); |
| 294 | fprintf (stderr, "*** illegal file format\n"); |
| 295 | return; |
| 296 | } /* if */ |
| 297 | } /* for */ |
| 298 | |
| 299 | if ((hdr_name = strrchr(fileName, '/')) == NULL) { |
| 300 | hdr_name = fileName; |
| 301 | } else { |
| 302 | ++hdr_name; |
| 303 | } |
| 304 | /* write start record */ |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 305 | (void)BuildSRecord(srecLine, 0, 0, (uint8_t *)hdr_name, strlen(hdr_name)); |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 306 | printf("%s\r\n",srecLine); |
| 307 | |
| 308 | /* write data records */ |
| 309 | firstAddr = ~0; |
| 310 | loadAddr = 0; |
| 311 | for (i = 0; i < elfHeader.e_shnum; i++) { |
| 312 | if ( (sectHeader[i].sh_type == SHT_PROGBITS) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 313 | && (sectHeader[i].sh_size != 0) |
| 314 | ) { |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 315 | loadSize = sectHeader[i].sh_size; |
| 316 | if (sectHeader[i].sh_flags != 0) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 317 | loadAddr = sectHeader[i].sh_addr; |
| 318 | loadDiff = loadAddr - sectHeader[i].sh_offset; |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 319 | } /* if */ |
| 320 | else { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 321 | loadAddr = sectHeader[i].sh_offset + loadDiff; |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 322 | } /* else */ |
| 323 | |
| 324 | if (loadAddr < firstAddr) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 325 | firstAddr = loadAddr; |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 326 | |
| 327 | /* build s-records */ |
| 328 | loadSize = sectHeader[i].sh_size; |
| 329 | fseek(file, sectHeader[i].sh_offset, SEEK_SET); |
| 330 | while (loadSize) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 331 | rxCount = fread(rxBlock, 1, (loadSize > 32) ? 32 : loadSize, file); |
| 332 | if (rxCount < 0) { |
| 333 | fclose(file); |
| 334 | fprintf (stderr, "*** illegal file format\n"); |
| 335 | return; |
| 336 | } /* if */ |
| 337 | (void)BuildSRecord(srecLine, 3, loadAddr + loadOffset, rxBlock, rxCount); |
| 338 | loadSize -= rxCount; |
| 339 | loadAddr += rxCount; |
| 340 | printf("%s\r\n",srecLine); |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 341 | } /* while */ |
| 342 | } /* if */ |
| 343 | } /* for */ |
| 344 | |
| 345 | /* add end record */ |
| 346 | (void)BuildSRecord(srecLine, 7, firstAddr + loadOffset, 0, 0); |
| 347 | printf("%s\r\n",srecLine); |
| 348 | fclose(file); |
| 349 | } /* ConvertELF */ |
| 350 | |
| 351 | |
| 352 | /************************************************************************* |
| 353 | | MAIN |
| 354 | |*************************************************************************/ |
| 355 | |
| 356 | int main( int argc, char *argv[ ]) |
| 357 | { |
Mike Frysinger | 3bf11b6 | 2009-11-04 16:13:19 -0500 | [diff] [blame] | 358 | uint32_t offset; |
wdenk | 89752b9 | 2001-10-23 11:55:05 +0000 | [diff] [blame] | 359 | |
| 360 | if (argc == 2) { |
| 361 | ConvertELF(argv[1], 0); |
| 362 | } /* if */ |
| 363 | else if ((argc == 4) && (strcmp(argv[1], "-o") == 0)) { |
| 364 | ExtractNumber(&offset, argv[2]); |
| 365 | ConvertELF(argv[3], offset); |
| 366 | } /* if */ |
| 367 | else { |
| 368 | fprintf (stderr, "Usage: img2srec [-o offset] <image>\n"); |
| 369 | } /* if */ |
| 370 | |
| 371 | return 0; |
| 372 | } /* main */ |