2016-06-27 19:22:38 +02:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/hex2bin/hex2mem.c
|
2016-06-27 19:22:38 +02:00
|
|
|
*
|
2020-03-09 21:28:20 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2016-06-27 19:22:38 +02:00
|
|
|
*
|
2020-03-09 21:28:20 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-06-27 19:22:38 +02:00
|
|
|
*
|
2020-03-09 21:28:20 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2016-06-27 19:22:38 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <hex2bin.h>
|
|
|
|
|
|
|
|
#include <nuttx/streams.h>
|
|
|
|
|
|
|
|
#ifdef CONFIG_LIB_HEX2BIN
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-02-01 19:17:03 +01:00
|
|
|
* Name: hex2mem
|
2016-06-27 19:22:38 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read the Intel HEX ASCII data provided on the file descriptor 'fd' and
|
|
|
|
* write the binary to memory.
|
|
|
|
*
|
|
|
|
* If, for example, fd is zero (stdin), then the HEX ASCII data would be
|
|
|
|
* taken from the console and written to memory.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* fd - The file descriptor from which Intel HEX data will be
|
|
|
|
* received.
|
|
|
|
* baseaddr - The base address of the memory region stream.
|
|
|
|
* endpaddr - The end address (plus 1) of the memory region.
|
|
|
|
* swap - Controls byte ordering. See enum hex2bin_swap_e for
|
|
|
|
* description of the values.
|
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Returned Value:
|
2016-06-27 19:22:38 +02:00
|
|
|
* Zero (OK) is returned on success; a negated errno value is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int hex2mem(int fd, uint32_t baseaddr, uint32_t endpaddr,
|
|
|
|
enum hex2bin_swap_e swap)
|
|
|
|
{
|
|
|
|
struct lib_rawinstream_s rawinstream;
|
|
|
|
struct lib_memsostream_s memoutstream;
|
|
|
|
|
|
|
|
/* Check memory addresses */
|
|
|
|
|
|
|
|
DEBUGASSERT(fd >= 0 && endpaddr > baseaddr);
|
|
|
|
|
|
|
|
/* Wrap the file descriptor as raw stream; wrap the memory as a memory
|
|
|
|
* stream.
|
|
|
|
*/
|
|
|
|
|
|
|
|
lib_rawinstream(&rawinstream, fd);
|
|
|
|
lib_memsostream(&memoutstream, (FAR char *)baseaddr,
|
|
|
|
(int)(endpaddr - baseaddr));
|
|
|
|
|
|
|
|
/* And do the deed */
|
|
|
|
|
|
|
|
return hex2bin(&rawinstream.public, &memoutstream.public,
|
|
|
|
(uint32_t)baseaddr, (uint32_t)endpaddr,
|
|
|
|
(enum hex2bin_swap_e)swap);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* CONFIG_LIB_HEX2BIN */
|