2009-06-17 18:28:50 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* binfmt/libnxflat/libnxflat_read.c
|
|
|
|
*
|
2024-09-09 09:12:59 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-03 14:47:23 +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
|
2009-06-17 18:28:50 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-06-17 18:28:50 +02:00
|
|
|
*
|
2021-02-03 14:47:23 +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.
|
2009-06-17 18:28:50 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2009-12-15 18:06:59 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdint.h>
|
2009-06-17 18:28:50 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <nxflat.h>
|
|
|
|
#include <debug.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
2017-10-11 20:13:41 +02:00
|
|
|
|
|
|
|
#include <nuttx/fs/fs.h>
|
2012-10-24 22:19:44 +02:00
|
|
|
#include <nuttx/binfmt/nxflat.h>
|
2009-06-17 18:28:50 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
2015-04-08 16:29:03 +02:00
|
|
|
* Pre-processor Definitions
|
2009-06-17 18:28:50 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2009-06-25 02:05:11 +02:00
|
|
|
#undef NXFLAT_DUMP_READDATA /* Define to dump all file data read */
|
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Constant Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2009-06-25 02:05:11 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_dumpreaddata
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#if defined(NXFLAT_DUMP_READDATA)
|
2015-10-09 03:20:17 +02:00
|
|
|
static inline void nxflat_dumpreaddata(FAR char *buffer, int buflen)
|
2009-06-25 02:05:11 +02:00
|
|
|
{
|
2015-10-09 03:20:17 +02:00
|
|
|
FAR uint32_t *buf32 = (FAR uint32_t *)buffer;
|
2009-06-25 02:05:11 +02:00
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
for (i = 0; i < buflen; i += 32)
|
|
|
|
{
|
2014-10-08 23:34:51 +02:00
|
|
|
syslog(LOG_DEBUG, "%04x:", i);
|
2009-12-15 18:06:59 +01:00
|
|
|
for (j = 0; j < 32; j += sizeof(uint32_t))
|
2009-06-25 02:05:11 +02:00
|
|
|
{
|
2014-10-08 23:34:51 +02:00
|
|
|
syslog(LOG_DEBUG, " %08x", *buf32++);
|
2009-06-25 02:05:11 +02:00
|
|
|
}
|
2014-10-08 23:34:51 +02:00
|
|
|
|
|
|
|
syslog(LOG_DEBUG, "\n");
|
2009-06-25 02:05:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define nxflat_dumpreaddata(b,n)
|
|
|
|
#endif
|
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: nxflat_read
|
2009-06-23 00:12:19 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read 'readsize' bytes from the object file at 'offset'
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0 (OK) is returned on success and a negated errno is returned on
|
|
|
|
* failure.
|
|
|
|
*
|
2009-06-17 18:28:50 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2024-08-25 01:21:12 +02:00
|
|
|
int nxflat_read(FAR struct nxflat_loadinfo_s *loadinfo, FAR char *buffer,
|
2017-10-11 20:13:41 +02:00
|
|
|
int readsize, int offset)
|
2009-06-17 18:28:50 +02:00
|
|
|
{
|
2024-08-25 01:21:12 +02:00
|
|
|
ssize_t nbytes; /* Number of bytes read */
|
|
|
|
off_t rpos; /* Position returned by lseek */
|
|
|
|
FAR char *bufptr; /* Next buffer location to read into */
|
|
|
|
int bytesleft; /* Number of bytes of .data left to read */
|
|
|
|
int bytesread; /* Total number of bytes read */
|
2009-06-17 18:28:50 +02:00
|
|
|
|
2016-06-11 19:59:51 +02:00
|
|
|
binfo("Read %d bytes from offset %d\n", readsize, offset);
|
2009-06-25 02:05:11 +02:00
|
|
|
|
2009-06-17 18:28:50 +02:00
|
|
|
/* Seek to the position in the object file where the initialized
|
|
|
|
* data is saved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bytesread = 0;
|
|
|
|
bufptr = buffer;
|
|
|
|
bytesleft = readsize;
|
|
|
|
do
|
|
|
|
{
|
2021-02-15 18:15:08 +01:00
|
|
|
rpos = file_seek(&loadinfo->file, offset, SEEK_SET);
|
2009-06-17 18:28:50 +02:00
|
|
|
if (rpos != offset)
|
|
|
|
{
|
2020-05-02 16:30:58 +02:00
|
|
|
berr("Failed to seek to position %d: %d\n", offset, (int)rpos);
|
|
|
|
return rpos;
|
2009-06-17 18:28:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the file data at offset into the user buffer */
|
|
|
|
|
2021-02-15 18:15:08 +01:00
|
|
|
nbytes = file_read(&loadinfo->file, bufptr, bytesleft);
|
2020-01-11 17:10:49 +01:00
|
|
|
if (nbytes < 0)
|
|
|
|
{
|
|
|
|
if (nbytes != -EINTR)
|
|
|
|
{
|
|
|
|
berr("Read from offset %d failed: %d\n",
|
|
|
|
offset, (int)nbytes);
|
|
|
|
return nbytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (nbytes == 0)
|
|
|
|
{
|
|
|
|
berr("Unexpected end of file\n");
|
|
|
|
return -ENODATA;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bytesread += nbytes;
|
|
|
|
bytesleft -= nbytes;
|
|
|
|
bufptr += nbytes;
|
|
|
|
offset += nbytes;
|
|
|
|
}
|
2009-06-17 18:28:50 +02:00
|
|
|
}
|
|
|
|
while (bytesread < readsize);
|
2009-06-25 02:05:11 +02:00
|
|
|
|
|
|
|
nxflat_dumpreaddata(buffer, readsize);
|
2009-06-17 18:28:50 +02:00
|
|
|
return OK;
|
|
|
|
}
|