diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/.gitignore | 1 | ||||
-rw-r--r-- | tools/fit_image.c | 34 | ||||
-rw-r--r-- | tools/img2srec.c | 164 | ||||
-rw-r--r-- | tools/mingw_support.c | 157 | ||||
-rw-r--r-- | tools/mingw_support.h | 2 | ||||
-rw-r--r-- | tools/mkimage.c | 58 |
6 files changed, 256 insertions, 160 deletions
diff --git a/tools/.gitignore b/tools/.gitignore index 03f54ef..cb067a4 100644 --- a/tools/.gitignore +++ b/tools/.gitignore @@ -8,3 +8,4 @@ /ncp /ubsha1 /inca-swap-bytes +/*.exe diff --git a/tools/fit_image.c b/tools/fit_image.c index d1e612f..ef9ffee 100644 --- a/tools/fit_image.c +++ b/tools/fit_image.c @@ -155,38 +155,6 @@ static int fit_handle_file (struct mkimage_params *params) return (EXIT_SUCCESS); } -static void fit_set_header (void *ptr, struct stat *sbuf, int ifd, - struct mkimage_params *params) -{ - uint32_t checksum; - - image_header_t * hdr = (image_header_t *)ptr; - - checksum = crc32 (0, - (const unsigned char *)(ptr + - sizeof(image_header_t)), - sbuf->st_size - sizeof(image_header_t)); - - /* Build new header */ - image_set_magic (hdr, IH_MAGIC); - image_set_time (hdr, sbuf->st_mtime); - image_set_size (hdr, sbuf->st_size - sizeof(image_header_t)); - image_set_load (hdr, params->addr); - image_set_ep (hdr, params->ep); - image_set_dcrc (hdr, checksum); - image_set_os (hdr, params->os); - image_set_arch (hdr, params->arch); - image_set_type (hdr, params->type); - image_set_comp (hdr, params->comp); - - image_set_name (hdr, params->imagename); - - checksum = crc32 (0, (const unsigned char *)hdr, - sizeof(image_header_t)); - - image_set_hcrc (hdr, checksum); -} - static int fit_check_params (struct mkimage_params *params) { return ((params->dflag && (params->fflag || params->lflag)) || @@ -202,7 +170,7 @@ static struct image_type_params fitimage_params = { .print_header = fit_print_contents, .check_image_type = fit_check_image_types, .fflag_handle = fit_handle_file, - .set_header = fit_set_header, + .set_header = NULL, /* FIT images use DTB header */ .check_params = fit_check_params, }; diff --git a/tools/img2srec.c b/tools/img2srec.c index f10379f..ec76964 100644 --- a/tools/img2srec.c +++ b/tools/img2srec.c @@ -53,6 +53,7 @@ |*************************************************************************/ #include "os_support.h" +#include <stdbool.h> #include <stddef.h> #include <stdio.h> #include <stdlib.h> @@ -63,63 +64,22 @@ #include <errno.h> /************************************************************************* -| DEFINES -|*************************************************************************/ - -#define FALSE 0 -#define TRUE 1 - -/************************************************************************* -| MACROS -|*************************************************************************/ - -/************************************************************************* -| TYPEDEFS -|*************************************************************************/ - -typedef uint8_t CHAR; -typedef uint8_t BYTE; -typedef uint16_t WORD; -typedef uint32_t DWORD; -typedef int BOOL; - -/************************************************************************* -| LOCALS -|*************************************************************************/ - -/************************************************************************* -| PROTOTYPES -|*************************************************************************/ - -static char *ExtractHex(DWORD *value, char *getPtr); -static char *ExtractDecimal(DWORD *value, char *getPtr); -static void ExtractNumber(DWORD *value, char *getPtr); -static BYTE *ExtractWord(WORD *value, BYTE *buffer); -static BYTE *ExtractLong(DWORD *value, BYTE *buffer); -static BYTE *ExtractBlock(WORD count, BYTE *data, BYTE *buffer); -static char *WriteHex(char *pa, BYTE value, WORD *pCheckSum); -static char *BuildSRecord(char *pa, WORD sType, DWORD addr, - const BYTE *data, int nCount); -static void ConvertELF(char *fileName, DWORD loadOffset); -int main(int argc, char *argv[]); - -/************************************************************************* | FUNCTIONS |*************************************************************************/ -static char* ExtractHex (DWORD* value, char* getPtr) +static char* ExtractHex (uint32_t* value, char* getPtr) { - DWORD num; - DWORD digit; - BYTE c; + uint32_t num; + uint32_t digit; + uint8_t c; while (*getPtr == ' ') getPtr++; num = 0; for (;;) { c = *getPtr; - if ((c >= '0') && (c <= '9')) digit = (DWORD)(c - '0'); - else if ((c >= 'A') && (c <= 'F')) digit = (DWORD)(c - 'A' + 10); - else if ((c >= 'a') && (c <= 'f')) digit = (DWORD)(c - 'a' + 10); + if ((c >= '0') && (c <= '9')) digit = (uint32_t)(c - '0'); + else if ((c >= 'A') && (c <= 'F')) digit = (uint32_t)(c - 'A' + 10); + else if ((c >= 'a') && (c <= 'f')) digit = (uint32_t)(c - 'a' + 10); else break; num <<= 4; num += digit; @@ -129,17 +89,17 @@ static char* ExtractHex (DWORD* value, char* getPtr) return getPtr; } /* ExtractHex */ -static char* ExtractDecimal (DWORD* value, char* getPtr) +static char* ExtractDecimal (uint32_t* value, char* getPtr) { - DWORD num; - DWORD digit; - BYTE c; + uint32_t num; + uint32_t digit; + uint8_t c; while (*getPtr == ' ') getPtr++; num = 0; for (;;) { c = *getPtr; - if ((c >= '0') && (c <= '9')) digit = (DWORD)(c - '0'); + if ((c >= '0') && (c <= '9')) digit = (uint32_t)(c - '0'); else break; num *= 10; num += digit; @@ -150,13 +110,13 @@ static char* ExtractDecimal (DWORD* value, char* getPtr) } /* ExtractDecimal */ -static void ExtractNumber (DWORD* value, char* getPtr) +static void ExtractNumber (uint32_t* value, char* getPtr) { - BOOL neg = FALSE;; + bool neg = false;; while (*getPtr == ' ') getPtr++; if (*getPtr == '-') { - neg = TRUE; + neg = true; getPtr++; } /* if */ if ((*getPtr == '0') && ((*(getPtr+1) == 'x') || (*(getPtr+1) == 'X'))) { @@ -170,38 +130,38 @@ static void ExtractNumber (DWORD* value, char* getPtr) } /* ExtractNumber */ -static BYTE* ExtractWord(WORD* value, BYTE* buffer) +static uint8_t* ExtractWord(uint16_t* value, uint8_t* buffer) { - WORD x; - x = (WORD)*buffer++; - x = (x<<8) + (WORD)*buffer++; + uint16_t x; + x = (uint16_t)*buffer++; + x = (x<<8) + (uint16_t)*buffer++; *value = x; return buffer; } /* ExtractWord */ -static BYTE* ExtractLong(DWORD* value, BYTE* buffer) +static uint8_t* ExtractLong(uint32_t* value, uint8_t* buffer) { - DWORD x; - x = (DWORD)*buffer++; - x = (x<<8) + (DWORD)*buffer++; - x = (x<<8) + (DWORD)*buffer++; - x = (x<<8) + (DWORD)*buffer++; + uint32_t x; + x = (uint32_t)*buffer++; + x = (x<<8) + (uint32_t)*buffer++; + x = (x<<8) + (uint32_t)*buffer++; + x = (x<<8) + (uint32_t)*buffer++; *value = x; return buffer; } /* ExtractLong */ -static BYTE* ExtractBlock(WORD count, BYTE* data, BYTE* buffer) +static uint8_t* ExtractBlock(uint16_t count, uint8_t* data, uint8_t* buffer) { while (count--) *data++ = *buffer++; return buffer; } /* ExtractBlock */ -static char* WriteHex(char* pa, BYTE value, WORD* pCheckSum) +static char* WriteHex(char* pa, uint8_t value, uint16_t* pCheckSum) { - WORD temp; + uint16_t temp; static char ByteToHex[] = "0123456789ABCDEF"; @@ -214,13 +174,13 @@ static char* WriteHex(char* pa, BYTE value, WORD* pCheckSum) } -static char* BuildSRecord(char* pa, WORD sType, DWORD addr, - const BYTE* data, int nCount) +static char* BuildSRecord(char* pa, uint16_t sType, uint32_t addr, + const uint8_t* data, int nCount) { - WORD addrLen; - WORD sRLen; - WORD checkSum; - WORD i; + uint16_t addrLen; + uint16_t sRLen; + uint16_t checkSum; + uint16_t i; switch (sType) { case 0: @@ -244,11 +204,11 @@ static char* BuildSRecord(char* pa, WORD sType, DWORD addr, *pa++ = (char)(sType + '0'); sRLen = addrLen + nCount + 1; checkSum = 0; - pa = WriteHex(pa, (BYTE)sRLen, &checkSum); + pa = WriteHex(pa, (uint8_t)sRLen, &checkSum); /* Write address field */ for (i = 1; i <= addrLen; i++) { - pa = WriteHex(pa, (BYTE)(addr >> (8 * (addrLen - i))), &checkSum); + pa = WriteHex(pa, (uint8_t)(addr >> (8 * (addrLen - i))), &checkSum); } /* for */ /* Write code/data fields */ @@ -258,25 +218,25 @@ static char* BuildSRecord(char* pa, WORD sType, DWORD addr, /* Write checksum field */ checkSum = ~checkSum; - pa = WriteHex(pa, (BYTE)checkSum, &checkSum); + pa = WriteHex(pa, (uint8_t)checkSum, &checkSum); *pa++ = '\0'; return pa; } -static void ConvertELF(char* fileName, DWORD loadOffset) +static void ConvertELF(char* fileName, uint32_t loadOffset) { FILE* file; int i; int rxCount; - BYTE rxBlock[1024]; - DWORD loadSize; - DWORD firstAddr; - DWORD loadAddr; - DWORD loadDiff = 0; + uint8_t rxBlock[1024]; + uint32_t loadSize; + uint32_t firstAddr; + uint32_t loadAddr; + uint32_t loadDiff = 0; Elf32_Ehdr elfHeader; Elf32_Shdr sectHeader[32]; - BYTE* getPtr; + uint8_t* getPtr; char srecLine[128]; char *hdr_name; @@ -292,11 +252,11 @@ static void ConvertELF(char* fileName, DWORD loadOffset) getPtr = ExtractBlock(sizeof elfHeader.e_ident, elfHeader.e_ident, rxBlock); getPtr = ExtractWord(&elfHeader.e_type, getPtr); getPtr = ExtractWord(&elfHeader.e_machine, getPtr); - getPtr = ExtractLong((DWORD *)&elfHeader.e_version, getPtr); - getPtr = ExtractLong((DWORD *)&elfHeader.e_entry, getPtr); - getPtr = ExtractLong((DWORD *)&elfHeader.e_phoff, getPtr); - getPtr = ExtractLong((DWORD *)&elfHeader.e_shoff, getPtr); - getPtr = ExtractLong((DWORD *)&elfHeader.e_flags, getPtr); + getPtr = ExtractLong((uint32_t *)&elfHeader.e_version, getPtr); + getPtr = ExtractLong((uint32_t *)&elfHeader.e_entry, getPtr); + getPtr = ExtractLong((uint32_t *)&elfHeader.e_phoff, getPtr); + getPtr = ExtractLong((uint32_t *)&elfHeader.e_shoff, getPtr); + getPtr = ExtractLong((uint32_t *)&elfHeader.e_flags, getPtr); getPtr = ExtractWord(&elfHeader.e_ehsize, getPtr); getPtr = ExtractWord(&elfHeader.e_phentsize, getPtr); getPtr = ExtractWord(&elfHeader.e_phnum, getPtr); @@ -319,16 +279,16 @@ static void ConvertELF(char* fileName, DWORD loadOffset) fseek(file, elfHeader.e_shoff, SEEK_SET); for (i = 0; i < elfHeader.e_shnum; i++) { rxCount = fread(rxBlock, 1, sizeof sectHeader[0], file); - getPtr = ExtractLong((DWORD *)§Header[i].sh_name, rxBlock); - getPtr = ExtractLong((DWORD *)§Header[i].sh_type, getPtr); - getPtr = ExtractLong((DWORD *)§Header[i].sh_flags, getPtr); - getPtr = ExtractLong((DWORD *)§Header[i].sh_addr, getPtr); - getPtr = ExtractLong((DWORD *)§Header[i].sh_offset, getPtr); - getPtr = ExtractLong((DWORD *)§Header[i].sh_size, getPtr); - getPtr = ExtractLong((DWORD *)§Header[i].sh_link, getPtr); - getPtr = ExtractLong((DWORD *)§Header[i].sh_info, getPtr); - getPtr = ExtractLong((DWORD *)§Header[i].sh_addralign, getPtr); - getPtr = ExtractLong((DWORD *)§Header[i].sh_entsize, getPtr); + getPtr = ExtractLong((uint32_t *)§Header[i].sh_name, rxBlock); + getPtr = ExtractLong((uint32_t *)§Header[i].sh_type, getPtr); + getPtr = ExtractLong((uint32_t *)§Header[i].sh_flags, getPtr); + getPtr = ExtractLong((uint32_t *)§Header[i].sh_addr, getPtr); + getPtr = ExtractLong((uint32_t *)§Header[i].sh_offset, getPtr); + getPtr = ExtractLong((uint32_t *)§Header[i].sh_size, getPtr); + getPtr = ExtractLong((uint32_t *)§Header[i].sh_link, getPtr); + getPtr = ExtractLong((uint32_t *)§Header[i].sh_info, getPtr); + getPtr = ExtractLong((uint32_t *)§Header[i].sh_addralign, getPtr); + getPtr = ExtractLong((uint32_t *)§Header[i].sh_entsize, getPtr); if (rxCount != sizeof sectHeader[0]) { fclose(file); fprintf (stderr, "*** illegal file format\n"); @@ -342,7 +302,7 @@ static void ConvertELF(char* fileName, DWORD loadOffset) ++hdr_name; } /* write start record */ - (void)BuildSRecord(srecLine, 0, 0, (BYTE *)hdr_name, strlen(hdr_name)); + (void)BuildSRecord(srecLine, 0, 0, (uint8_t *)hdr_name, strlen(hdr_name)); printf("%s\r\n",srecLine); /* write data records */ @@ -395,7 +355,7 @@ static void ConvertELF(char* fileName, DWORD loadOffset) int main( int argc, char *argv[ ]) { - DWORD offset; + uint32_t offset; if (argc == 2) { ConvertELF(argv[1], 0); diff --git a/tools/mingw_support.c b/tools/mingw_support.c index 67cd6e1..6379710 100644 --- a/tools/mingw_support.c +++ b/tools/mingw_support.c @@ -24,7 +24,9 @@ #include "mingw_support.h" #include <stdio.h> #include <stdint.h> +#include <string.h> #include <errno.h> +#include <assert.h> #include <io.h> int fsync(int fd) @@ -77,3 +79,158 @@ int munmap(void *addr, size_t len) return 0; } + +/* Reentrant string tokenizer. Generic version. + Copyright (C) 1991,1996-1999,2001,2004,2007 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Parse S into tokens separated by characters in DELIM. + If S is NULL, the saved pointer in SAVE_PTR is used as + the next starting point. For example: + char s[] = "-abc-=-def"; + char *sp; + x = strtok_r(s, "-", &sp); // x = "abc", sp = "=-def" + x = strtok_r(NULL, "-=", &sp); // x = "def", sp = NULL + x = strtok_r(NULL, "=", &sp); // x = NULL + // s = "abc\0-def\0" +*/ +char *strtok_r(char *s, const char *delim, char **save_ptr) +{ + char *token; + + if (s == NULL) + s = *save_ptr; + + /* Scan leading delimiters. */ + s += strspn(s, delim); + if (*s == '\0') { + *save_ptr = s; + return NULL; + } + + /* Find the end of the token. */ + token = s; + s = strpbrk (token, delim); + if (s == NULL) { + /* This token finishes the string. */ + *save_ptr = memchr(token, '\0', strlen(token)); + } else { + /* Terminate the token and make *SAVE_PTR point past it. */ + *s = '\0'; + *save_ptr = s + 1; + } + return token; +} + +/* getline.c -- Replacement for GNU C library function getline + +Copyright (C) 1993, 1996, 2001, 2002 Free Software Foundation, Inc. + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License as +published by the Free Software Foundation; either version 2 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ + +/* Written by Jan Brittenson, bson@gnu.ai.mit.edu. */ + +/* Always add at least this many bytes when extending the buffer. */ +#define MIN_CHUNK 64 + +/* Read up to (and including) a TERMINATOR from STREAM into *LINEPTR + + OFFSET (and null-terminate it). *LINEPTR is a pointer returned from + malloc (or NULL), pointing to *N characters of space. It is realloc'd + as necessary. Return the number of characters read (not including the + null terminator), or -1 on error or EOF. + NOTE: There is another getstr() function declared in <curses.h>. */ +static int getstr(char **lineptr, size_t *n, FILE *stream, + char terminator, size_t offset) +{ + int nchars_avail; /* Allocated but unused chars in *LINEPTR. */ + char *read_pos; /* Where we're reading into *LINEPTR. */ + int ret; + + if (!lineptr || !n || !stream) + return -1; + + if (!*lineptr) { + *n = MIN_CHUNK; + *lineptr = malloc(*n); + if (!*lineptr) + return -1; + } + + nchars_avail = *n - offset; + read_pos = *lineptr + offset; + + for (;;) { + register int c = getc(stream); + + /* We always want at least one char left in the buffer, since we + always (unless we get an error while reading the first char) + NUL-terminate the line buffer. */ + + assert(*n - nchars_avail == read_pos - *lineptr); + if (nchars_avail < 2) { + if (*n > MIN_CHUNK) + *n *= 2; + else + *n += MIN_CHUNK; + + nchars_avail = *n + *lineptr - read_pos; + *lineptr = realloc(*lineptr, *n); + if (!*lineptr) + return -1; + read_pos = *n - nchars_avail + *lineptr; + assert(*n - nchars_avail == read_pos - *lineptr); + } + + if (c == EOF || ferror (stream)) { + /* Return partial line, if any. */ + if (read_pos == *lineptr) + return -1; + else + break; + } + + *read_pos++ = c; + nchars_avail--; + + if (c == terminator) + /* Return the line. */ + break; + } + + /* Done - NUL terminate and return the number of chars read. */ + *read_pos = '\0'; + + ret = read_pos - (*lineptr + offset); + return ret; +} + +int getline (char **lineptr, size_t *n, FILE *stream) +{ + return getstr(lineptr, n, stream, '\n', 0); +} diff --git a/tools/mingw_support.h b/tools/mingw_support.h index 9e45e64..2793674 100644 --- a/tools/mingw_support.h +++ b/tools/mingw_support.h @@ -44,5 +44,7 @@ typedef ULONG ulong; int fsync(int fd); void *mmap(void *, size_t, int, int, int, int); int munmap(void *, size_t); +char *strtok_r(char *s, const char *delim, char **save_ptr); +int getline(char **lineptr, size_t *n, FILE *stream); #endif /* __MINGW_SUPPORT_H_ */ diff --git a/tools/mkimage.c b/tools/mkimage.c index ab6ea32..6826eae 100644 --- a/tools/mkimage.c +++ b/tools/mkimage.c @@ -229,8 +229,15 @@ main (int argc, char **argv) case 'f': if (--argc <= 0) usage (); + params.type = IH_TYPE_FLATDT; params.datafile = *++argv; params.fflag = 1; + + /* + * The flattened image tree (FIT) format + * requires a flattened device tree image type + */ + params.type = IH_TYPE_FLATDT; goto NXTARG; case 'n': if (--argc <= 0) @@ -292,23 +299,35 @@ NXTARG: ; params.imagefile = *argv; - if (!params.fflag){ - if (params.lflag) { - ifd = open (params.imagefile, O_RDONLY|O_BINARY); - } else { - ifd = open (params.imagefile, - O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666); - } + if (params.fflag){ + if (tparams->fflag_handle) + /* + * in some cases, some additional processing needs + * to be done if fflag is defined + * + * For ex. fit_handle_file for Fit file support + */ + retval = tparams->fflag_handle(¶ms); - if (ifd < 0) { - fprintf (stderr, "%s: Can't open %s: %s\n", - params.cmdname, params.imagefile, - strerror(errno)); - exit (EXIT_FAILURE); - } + if (retval != EXIT_SUCCESS) + exit (retval); } - if (params.lflag) { + if (params.lflag || params.fflag) { + ifd = open (params.imagefile, O_RDONLY|O_BINARY); + } else { + ifd = open (params.imagefile, + O_RDWR|O_CREAT|O_TRUNC|O_BINARY, 0666); + } + + if (ifd < 0) { + fprintf (stderr, "%s: Can't open %s: %s\n", + params.cmdname, params.imagefile, + strerror(errno)); + exit (EXIT_FAILURE); + } + + if (params.lflag || params.fflag) { /* * list header information of existing image */ @@ -346,17 +365,6 @@ NXTARG: ; (void) close (ifd); exit (retval); - } else if (params.fflag) { - if (tparams->fflag_handle) - /* - * in some cases, some additional processing needs - * to be done if fflag is defined - * - * For ex. fit_handle_file for Fit file support - */ - retval = tparams->fflag_handle(¶ms); - - exit (retval); } /* |