hex2mem: Like hex2bin, but writes Intel HEX files directly to memory

This commit is contained in:
Gregory Nutt 2014-06-16 10:34:50 -06:00
parent 896d1d3f83
commit 36595a49cb
4 changed files with 346 additions and 15 deletions

View File

@ -12,7 +12,7 @@ config SYSTEM_HEX2BIN
if SYSTEM_HEX2BIN if SYSTEM_HEX2BIN
config SYSTEM_HEX2BIN_BUILTIN config SYSTEM_HEX2BIN_BUILTIN
bool "NSH Built-In" bool "NSH hex2bin Built-In"
default n default n
depends on NSH_BUILTIN_APPS depends on NSH_BUILTIN_APPS
---help--- ---help---
@ -23,13 +23,13 @@ config SYSTEM_HEX2BIN_BUILTIN
if SYSTEM_HEX2BIN_BUILTIN if SYSTEM_HEX2BIN_BUILTIN
config SYSTEM_HEX2BIN_STACKSIZE config SYSTEM_HEX2BIN_STACKSIZE
int "Program stack size" int "hex2bin stack size"
default 1536 default 1536
---help--- ---help---
The size of stack allocated for the hex2bin task. The size of stack allocated for the hex2bin task.
config SYSTEM_HEX2BIN_PRIORITY config SYSTEM_HEX2BIN_PRIORITY
int "Program priority" int "hex2bin priority"
default 100 default 100
---help--- ---help---
The priority of the hex2bin task. The priority of the hex2bin task.
@ -47,6 +47,58 @@ config SYSTEM_HEX2BIN_ENDPADDR
The default value of the end (plus 1) address argument. Saves typing. The default value of the end (plus 1) address argument. Saves typing.
config SYSTEM_HEX2BIN_SWAP config SYSTEM_HEX2BIN_SWAP
int "Swap bytes"
default 0
range 0 2
---help---
The default value of the swap argument. (0) No swap, (1) swap bytes
in 16-bit values, or (2) swap bytes in 32-bit values.
config SYSTEM_HEX2BIN_USAGE
bool "hex2bin usage"
default y
---help---
You can save a little FLASH memory by suppressing usage
instructions.
endif # SYSTEM_HEX2BIN_BUILTIN
config SYSTEM_HEX2MEM_BUILTIN
bool "NSH hex2mem Built-In"
default n
depends on NSH_BUILTIN_APPS
---help---
By default, a flexible hex2bin library function built. An NSH
builtin function can also be generated to copy Intel HEX files
to memory.
if SYSTEM_HEX2MEM_BUILTIN
config SYSTEM_HEX2MEM_STACKSIZE
int "hex2mem stack size"
default 1536
---help---
The size of stack allocated for the hex2bin task.
config SYSTEM_HEX2MEM_PRIORITY
int "hex2mem priority"
default 100
---help---
The priority of the hex2bin task.
config SYSTEM_HEX2MEM_BASEADDR
hex "Binary base address"
default 0x00000000
---help---
The default value of the base address argument. Saves typing.
config SYSTEM_HEX2MEM_ENDPADDR
hex "Binary base address"
default 0x00000000
---help---
The default value of the end (plus 1) address argument. Saves typing.
config SYSTEM_HEX2MEM_SWAP
int "Binary base address" int "Binary base address"
default 0 default 0
range 0 2 range 0 2
@ -54,7 +106,14 @@ config SYSTEM_HEX2BIN_SWAP
The default value of the swap argument. (0) No swap, (1) swap bytes The default value of the swap argument. (0) No swap, (1) swap bytes
in 16-bit values, or (2) swap tbytes in 32-bit values. in 16-bit values, or (2) swap tbytes in 32-bit values.
endif # SYSTEM_HEX2BIN_BUILTIN config SYSTEM_HEX2MEM_USAGE
bool "hex2mem usage"
default y
---help---
You can save a little FLASH memory by suppressing usage
instructions.
endif # SYSTEM_HEX2MEM_BUILTIN
config SYSTEM_HEX2BIN_DEBUG config SYSTEM_HEX2BIN_DEBUG
bool "Hex2bin detailed error output" bool "Hex2bin detailed error output"

View File

@ -46,6 +46,10 @@ ifeq ($(CONFIG_SYSTEM_HEX2BIN_BUILTIN),y)
CSRCS += hex2bin_main.c CSRCS += hex2bin_main.c
endif endif
ifeq ($(CONFIG_SYSTEM_HEX2MEM_BUILTIN),y)
CSRCS += hex2mem_main.c
endif
AOBJS = $(ASRCS:.S=$(OBJEXT)) AOBJS = $(ASRCS:.S=$(OBJEXT))
COBJS = $(CSRCS:.c=$(OBJEXT)) COBJS = $(CSRCS:.c=$(OBJEXT))
@ -68,8 +72,14 @@ VPATH =
CONFIG_SYSTEM_HEX2BIN_STACKSIZE ?= 1536 CONFIG_SYSTEM_HEX2BIN_STACKSIZE ?= 1536
CONFIG_SYSTEM_HEX2BIN_PRIORITY ?= 100 CONFIG_SYSTEM_HEX2BIN_PRIORITY ?= 100
STACKSIZE = $(CONFIG_SYSTEM_HEX2BIN_STACKSIZE) HEX2BIN_STACKSIZE = $(CONFIG_SYSTEM_HEX2BIN_STACKSIZE)
PRIORITY = $(CONFIG_SYSTEM_HEX2BIN_PRIORITY) HEX2BIN_PRIORITY = $(CONFIG_SYSTEM_HEX2BIN_PRIORITY)
CONFIG_SYSTEM_HEX2MEM_STACKSIZE ?= 1536
CONFIG_SYSTEM_HEX2MEM_PRIORITY ?= 100
HEX2MEM_STACKSIZE = $(CONFIG_SYSTEM_HEX2MEM_STACKSIZE)
HEX2MEM_PRIORITY = $(CONFIG_SYSTEM_HEX2MEM_PRIORITY)
# Build targets # Build targets
@ -86,17 +96,28 @@ $(COBJS): %$(OBJEXT): %.c
$(call ARCHIVE, $(BIN), $(OBJS)) $(call ARCHIVE, $(BIN), $(OBJS))
$(Q) touch .built $(Q) touch .built
# Register application # Register application(s)
ifeq ($(CONFIG_SYSTEM_HEX2BIN_BUILTIN),y) ifeq ($(CONFIG_SYSTEM_HEX2BIN_BUILTIN),y)
$(BUILTIN_REGISTRY)$(DELIM)hex2bin.bdat: $(DEPCONFIG) Makefile $(BUILTIN_REGISTRY)$(DELIM)hex2bin.bdat: $(DEPCONFIG) Makefile
$(call REGISTER,"hex2bin",$(PRIORITY),$(STACKSIZE),hex2bin_main) $(call REGISTER,"hex2bin",$(HEX2BIN_PRIORITY),$(HEX2BIN_STACKSIZE),hex2bin_main)
context: $(BUILTIN_REGISTRY)$(DELIM)hex2bin.bdat rhex2bin: $(BUILTIN_REGISTRY)$(DELIM)hex2bin.bdat
else else
context: rhex2bin:
endif endif
ifeq ($(CONFIG_SYSTEM_HEX2MEM_BUILTIN),y)
$(BUILTIN_REGISTRY)$(DELIM)hex2mem.bdat: $(DEPCONFIG) Makefile
$(call REGISTER,"hex2mem",$(HEX2MEM_PRIORITY),$(HEX2MEM_STACKSIZE),hex2mem_main)
rhex2mem: $(BUILTIN_REGISTRY)$(DELIM)hex2mem.bdat
else
rhex2mem:
endif
context: rhex2bin rhex2mem
.depend: Makefile $(SRCS) .depend: Makefile $(SRCS)
$(Q) $(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep $(Q) $(MKDEP) $(ROOTDEPPATH) "$(CC)" -- $(CFLAGS) -- $(SRCS) >Make.dep
$(Q) touch $@ $(Q) touch $@

View File

@ -65,11 +65,12 @@
* Name: show_usage * Name: show_usage
****************************************************************************/ ****************************************************************************/
void show_usage(FAR const char *progname, int exitcode) static void show_usage(FAR const char *progname, int exitcode)
{ {
#ifdef CONFIG_SYSTEM_HEX2BIN_USAGE
fprintf(stderr, "Usage:\n"); fprintf(stderr, "Usage:\n");
fprintf(stderr, "\t%s [OPTIONS] <hexfile> <binfile>\n"); fprintf(stderr, "\t%s [OPTIONS] <hexfile> <binfile>\n", progname);
fprintf(stderr, "\t%s -h\n"); fprintf(stderr, "\t%s -h\n", progname);
fprintf(stderr, "Where:\n"); fprintf(stderr, "Where:\n");
fprintf(stderr, "\t<hexfile>:\n"); fprintf(stderr, "\t<hexfile>:\n");
fprintf(stderr, "\t\tThe file containing the Intel HEX data to be converted.\n"); fprintf(stderr, "\t\tThe file containing the Intel HEX data to be converted.\n");
@ -86,13 +87,15 @@ void show_usage(FAR const char *progname, int exitcode)
CONFIG_SYSTEM_HEX2BIN_BASEADDR); CONFIG_SYSTEM_HEX2BIN_BASEADDR);
fprintf(stderr, "\t-e <end address>\n"); fprintf(stderr, "\t-e <end address>\n");
fprintf(stderr, "\t\tSets the maximum address (plus 1) of the binary output.\n"); fprintf(stderr, "\t\tSets the maximum address (plus 1) of the binary output.\n");
fprintf(stderr, "\t\tThis value is used to only for error checking. Default:\n"); fprintf(stderr, "\t\tThis value is used to only for error checking. The value\n");
fprintf(stderr, "\t\t0x%08x\n", CONFIG_SYSTEM_HEX2BIN_ENDPADDR); fprintf(stderr, "\t\tzero disables error checking. Default: 0x%08x\n",
CONFIG_SYSTEM_HEX2BIN_ENDPADDR);
fprintf(stderr, "\t\tno error checking\n"); fprintf(stderr, "\t\tno error checking\n");
fprintf(stderr, "\t-w <swap code>\n"); fprintf(stderr, "\t-w <swap code>\n");
fprintf(stderr, "\t\t(0) No swap, (1) swap bytes in 16-bit values, or (3) swap\n"); fprintf(stderr, "\t\t(0) No swap, (1) swap bytes in 16-bit values, or (3) swap\n");
fprintf(stderr, "\t\tbytes in 32-bit values. Default: %d\n", fprintf(stderr, "\t\tbytes in 32-bit values. Default: %d\n",
CONFIG_SYSTEM_HEX2BIN_SWAP); CONFIG_SYSTEM_HEX2BIN_SWAP);
#endif
exit(exitcode); exit(exitcode);
} }

View File

@ -0,0 +1,248 @@
/****************************************************************************
* apps/system/hex2mem_main.c
*
* Copyright (C) 2014 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <nuttx/streams.h>
#include <apps/hex2bin.h>
#ifdef CONFIG_SYSTEM_HEX2MEM_BUILTIN
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Types
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: show_usage
****************************************************************************/
static void show_usage(FAR const char *progname, int exitcode)
{
#ifdef CONFIG_SYSTEM_HEX2MEM_USAGE
fprintf(stderr, "Usage:\n");
fprintf(stderr, "\t%s [OPTIONS] <hexfile>\n", progname);
fprintf(stderr, "\t%s -h\n", progname);
fprintf(stderr, "Where:\n");
fprintf(stderr, "\t<hexfile>:\n");
fprintf(stderr, "\t\tThe file containing the Intel HEX data to be converted.\n");
fprintf(stderr, "\t-h:\n");
fprintf(stderr, "\t\tPrints this message and exits\n");
fprintf(stderr, "And [OPTIONS] include:\n");
fprintf(stderr, "\t-s <start address>\n");
fprintf(stderr, "\t\tSets the start memory address for the binary output. Hex\n");
fprintf(stderr, "\t\tdata is written to memory relative to this address. Default:\n");
fprintf(stderr, "\t\t0x%08x\n", CONFIG_SYSTEM_HEX2MEM_BASEADDR);
fprintf(stderr, "\t-e <end address>\n");
fprintf(stderr, "\t\tSets the maximum memory address (plus 1). This value is\n");
fprintf(stderr, "\t\tused to assure that the program does not write past the end\n");
fprintf(stderr, "\t\tof memory. The value zero disables error checking.\n");
fprintf(stderr, "\t\tDefault: 0x%08x\n", CONFIG_SYSTEM_HEX2MEM_ENDPADDR);
fprintf(stderr, "\t-w <swap code>\n");
fprintf(stderr, "\t\t(0) No swap, (1) swap bytes in 16-bit values, or (3) swap\n");
fprintf(stderr, "\t\tbytes in 32-bit values. Default: %d\n",
CONFIG_SYSTEM_HEX2MEM_SWAP);
#endif
exit(exitcode);
}
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name:
****************************************************************************/
/****************************************************************************
* Name: hex2mem_main
*
* Description:
* Main entry point when hex2mem is built as an NSH built-in task.
*
* Input Parameters:
* Standard task inputs
*
* Returned Value
* EXIT_SUCESS on success; EXIT_FAILURE on failure
*
****************************************************************************/
int hex2mem_main(int argc, char **argv)
{
struct lib_stdinstream_s stdinstream;
struct lib_memsostream_s memoutstream;
FAR const char *hexfile;
FAR char *endptr;
FAR FILE *instream;
unsigned long baseaddr;
unsigned long endpaddr;
unsigned long swap;
int option;
int ret;
/* Parse the command line options */
baseaddr = CONFIG_SYSTEM_HEX2MEM_BASEADDR;
endpaddr = CONFIG_SYSTEM_HEX2MEM_ENDPADDR;
swap = CONFIG_SYSTEM_HEX2MEM_SWAP;
while ((option = getopt(argc, argv, ":hs:e:w:")) != ERROR)
{
switch (option)
{
case 'h':
show_usage(argv[0], EXIT_SUCCESS);
break;
case 's':
baseaddr = strtoul(optarg, &endptr, 16);
if (endptr == optarg)
{
fprintf(stderr, "ERROR: Invalid argument to the -s option\n");
show_usage(argv[0], EXIT_FAILURE);
}
break;
case 'e':
endpaddr = strtoul(optarg, &endptr, 16);
if (endptr == optarg)
{
fprintf(stderr, "ERROR: Invalid argument to the -e option\n");
show_usage(argv[0], EXIT_FAILURE);
}
break;
case 'w':
swap = strtoul(optarg, &endptr, 16);
if (endptr == optarg || swap > (unsigned long)HEX2BIN_SWAP32)
{
fprintf(stderr, "ERROR: Invalid argument to the -w option\n");
show_usage(argv[0], EXIT_FAILURE);
}
break;
case ':':
fprintf(stderr, "ERROR: Missing required argument\n");
show_usage(argv[0], EXIT_FAILURE);
break;
default:
case '?':
fprintf(stderr, "ERROR: Unrecognized option\n");
show_usage(argv[0], EXIT_FAILURE);
break;
}
}
/* There should be two final parameters remaining on the command line */
if (optind >= argc)
{
printf("ERROR: Missing required <hexfile> argument\n");
show_usage(argv[0], EXIT_FAILURE);
}
hexfile = argv[optind];
optind++;
if (optind < argc)
{
printf("ERROR: Garbage at end of command line\n");
show_usage(argv[0], EXIT_FAILURE);
}
/* Check memory addresses */
if (endpaddr <= baseaddr)
{
printf("ERROR: Memory end (+1) address must be AFTER memory base address\n");
show_usage(argv[0], EXIT_FAILURE);
}
/* Open the HEX file for reading */
instream = fopen(hexfile, "r");
if (instream == NULL)
{
int errcode = errno;
DEBUGASSERT(errcode > 0);
fprintf(stderr, "ERROR: Failed to open \"%s\" for reading: %d\n",
hexfile, errcode);
return -errcode;
}
/* Wrap the FILE stream as standard streams; wrap the memory as a memory
* stream.
*/
lib_stdinstream(&stdinstream, instream);
lib_memsostream(&memoutstream, (FAR char *)baseaddr, (int)(endpaddr - baseaddr));
/* And do the deed */
ret = hex2bin(&stdinstream.public, &memoutstream.public,
(uint32_t)baseaddr, (uint32_t)endpaddr,
(enum hex2bin_swap_e)swap);
if (ret < 0)
{
fprintf(stderr, "ERROR: Failed to convert \"%s\" to binary: %d\n",
ret);
}
/* Clean up and return */
fclose(instream);
return ret < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
#endif /* CONFIG_SYSTEM_HEX2MEM_BUILTIN */