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