]>
git.ipfire.org Git - people/ms/u-boot.git/blob - tools/img2srec.c
1 /*************************************************************************
2 | COPYRIGHT (c) 2000 BY ABATRON AG
3 |*************************************************************************
5 | PROJECT NAME: Linux Image to S-record Conversion Utility
6 | FILENAME : img2srec.c
10 | TARGET OS : LINUX / UNIX
13 | PROGRAMMER : Abatron / RD
16 |*************************************************************************
20 | Utility to convert a Linux Boot Image to S-record:
21 | ==================================================
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".
27 | img2srec [-o offset] image > image.srec
33 | To build the utility use GCC as follows:
35 | gcc img2srec.c -o img2srec
38 |*************************************************************************
44 | -----------------------------------------------------------
47 | 07.07.00 aba Initial release
49 |*************************************************************************/
51 /*************************************************************************
53 |*************************************************************************/
55 #include "os_support.h"
66 /*************************************************************************
68 |*************************************************************************/
70 static char* ExtractHex (uint32_t* value
, char* getPtr
)
76 while (*getPtr
== ' ') getPtr
++;
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);
92 static char* ExtractDecimal (uint32_t* value
, char* getPtr
)
98 while (*getPtr
== ' ') getPtr
++;
102 if ((c
>= '0') && (c
<= '9')) digit
= (uint32_t)(c
- '0');
110 } /* ExtractDecimal */
113 static void ExtractNumber (uint32_t* value
, char* getPtr
)
117 while (*getPtr
== ' ') getPtr
++;
118 if (*getPtr
== '-') {
122 if ((*getPtr
== '0') && ((*(getPtr
+1) == 'x') || (*(getPtr
+1) == 'X'))) {
124 (void)ExtractHex(value
, getPtr
);
127 (void)ExtractDecimal(value
, getPtr
);
129 if (neg
) *value
= -(*value
);
130 } /* ExtractNumber */
133 static uint8_t* ExtractWord(uint16_t* value
, uint8_t* buffer
)
136 x
= (uint16_t)*buffer
++;
137 x
= (x
<<8) + (uint16_t)*buffer
++;
143 static uint8_t* ExtractLong(uint32_t* value
, uint8_t* buffer
)
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
++;
155 static uint8_t* ExtractBlock(uint16_t count
, uint8_t* data
, uint8_t* buffer
)
157 while (count
--) *data
++ = *buffer
++;
162 static char* WriteHex(char* pa
, uint8_t value
, uint16_t* pCheckSum
)
166 static char ByteToHex
[] = "0123456789ABCDEF";
170 *pa
++ = ByteToHex
[temp
];
172 *pa
++ = ByteToHex
[temp
];
177 static char* BuildSRecord(char* pa
, uint16_t sType
, uint32_t addr
,
178 const uint8_t* data
, int nCount
)
204 *pa
++ = (char)(sType
+ '0');
205 sRLen
= addrLen
+ nCount
+ 1;
207 pa
= WriteHex(pa
, (uint8_t)sRLen
, &checkSum
);
209 /* Write address field */
210 for (i
= 1; i
<= addrLen
; i
++) {
211 pa
= WriteHex(pa
, (uint8_t)(addr
>> (8 * (addrLen
- i
))), &checkSum
);
214 /* Write code/data fields */
215 for (i
= 0; i
< nCount
; i
++) {
216 pa
= WriteHex(pa
, *data
++, &checkSum
);
219 /* Write checksum field */
220 checkSum
= ~checkSum
;
221 pa
= WriteHex(pa
, (uint8_t)checkSum
, &checkSum
);
227 static void ConvertELF(char* fileName
, uint32_t loadOffset
)
232 uint8_t rxBlock
[1024];
236 uint32_t loadDiff
= 0;
237 Elf32_Ehdr elfHeader
;
238 Elf32_Shdr sectHeader
[32];
245 if ((file
= fopen(fileName
,"rb")) == NULL
) {
246 fprintf (stderr
, "Can't open %s: %s\n", fileName
, strerror(errno
));
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
);
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
);
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
)
274 fprintf (stderr
, "*** illegal file format\n");
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
);
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
);
292 if (rxCount
!= sizeof sectHeader
[0]) {
294 fprintf (stderr
, "*** illegal file format\n");
299 if ((hdr_name
= strrchr(fileName
, '/')) == NULL
) {
304 /* write start record */
305 (void)BuildSRecord(srecLine
, 0, 0, (uint8_t *)hdr_name
, strlen(hdr_name
));
306 printf("%s\r\n",srecLine
);
308 /* write data records */
311 for (i
= 0; i
< elfHeader
.e_shnum
; i
++) {
312 if ( (sectHeader
[i
].sh_type
== SHT_PROGBITS
)
313 && (sectHeader
[i
].sh_size
!= 0)
315 loadSize
= sectHeader
[i
].sh_size
;
316 if (sectHeader
[i
].sh_flags
!= 0) {
317 loadAddr
= sectHeader
[i
].sh_addr
;
318 loadDiff
= loadAddr
- sectHeader
[i
].sh_offset
;
321 loadAddr
= sectHeader
[i
].sh_offset
+ loadDiff
;
324 if (loadAddr
< firstAddr
)
325 firstAddr
= loadAddr
;
327 /* build s-records */
328 loadSize
= sectHeader
[i
].sh_size
;
329 fseek(file
, sectHeader
[i
].sh_offset
, SEEK_SET
);
331 rxCount
= fread(rxBlock
, 1, (loadSize
> 32) ? 32 : loadSize
, file
);
334 fprintf (stderr
, "*** illegal file format\n");
337 (void)BuildSRecord(srecLine
, 3, loadAddr
+ loadOffset
, rxBlock
, rxCount
);
340 printf("%s\r\n",srecLine
);
346 (void)BuildSRecord(srecLine
, 7, firstAddr
+ loadOffset
, 0, 0);
347 printf("%s\r\n",srecLine
);
352 /*************************************************************************
354 |*************************************************************************/
356 int main( int argc
, char *argv
[ ])
361 ConvertELF(argv
[1], 0);
363 else if ((argc
== 4) && (strcmp(argv
[1], "-o") == 0)) {
364 ExtractNumber(&offset
, argv
[2]);
365 ConvertELF(argv
[3], offset
);
368 fprintf (stderr
, "Usage: img2srec [-o offset] <image>\n");