libnxflat_read.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /****************************************************************************
  2. * binfmt/libnxflat/libnxflat_read.c
  3. *
  4. * Copyright (C) 2009, 2020 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/types.h>
  40. #include <stdint.h>
  41. #include <unistd.h>
  42. #include <string.h>
  43. #include <nxflat.h>
  44. #include <debug.h>
  45. #include <errno.h>
  46. #include <arpa/inet.h>
  47. #include <nuttx/fs/fs.h>
  48. #include <nuttx/binfmt/nxflat.h>
  49. /****************************************************************************
  50. * Pre-processor Definitions
  51. ****************************************************************************/
  52. #undef NXFLAT_DUMP_READDATA /* Define to dump all file data read */
  53. /****************************************************************************
  54. * Private Constant Data
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Private Functions
  58. ****************************************************************************/
  59. /****************************************************************************
  60. * Name: nxflat_dumpreaddata
  61. ****************************************************************************/
  62. #if defined(NXFLAT_DUMP_READDATA)
  63. static inline void nxflat_dumpreaddata(FAR char *buffer, int buflen)
  64. {
  65. FAR uint32_t *buf32 = (FAR uint32_t *)buffer;
  66. int i;
  67. int j;
  68. for (i = 0; i < buflen; i += 32)
  69. {
  70. syslog(LOG_DEBUG, "%04x:", i);
  71. for (j = 0; j < 32; j += sizeof(uint32_t))
  72. {
  73. syslog(LOG_DEBUG, " %08x", *buf32++);
  74. }
  75. syslog(LOG_DEBUG, "\n");
  76. }
  77. }
  78. #else
  79. # define nxflat_dumpreaddata(b,n)
  80. #endif
  81. /****************************************************************************
  82. * Public Functions
  83. ****************************************************************************/
  84. /****************************************************************************
  85. * Name: nxflat_read
  86. *
  87. * Description:
  88. * Read 'readsize' bytes from the object file at 'offset'
  89. *
  90. * Returned Value:
  91. * 0 (OK) is returned on success and a negated errno is returned on
  92. * failure.
  93. *
  94. ****************************************************************************/
  95. int nxflat_read(struct nxflat_loadinfo_s *loadinfo, char *buffer,
  96. int readsize, int offset)
  97. {
  98. ssize_t nbytes; /* Number of bytes read */
  99. off_t rpos; /* Position returned by lseek */
  100. char *bufptr; /* Next buffer location to read into */
  101. int bytesleft; /* Number of bytes of .data left to read */
  102. int bytesread; /* Total number of bytes read */
  103. binfo("Read %d bytes from offset %d\n", readsize, offset);
  104. /* Seek to the position in the object file where the initialized
  105. * data is saved.
  106. */
  107. bytesread = 0;
  108. bufptr = buffer;
  109. bytesleft = readsize;
  110. do
  111. {
  112. rpos = lseek(loadinfo->filfd, offset, SEEK_SET);
  113. if (rpos != offset)
  114. {
  115. int errval = get_errno();
  116. berr("Failed to seek to position %d: %d\n", offset, errval);
  117. return -errval;
  118. }
  119. /* Read the file data at offset into the user buffer */
  120. nbytes = nx_read(loadinfo->filfd, bufptr, bytesleft);
  121. if (nbytes < 0)
  122. {
  123. if (nbytes != -EINTR)
  124. {
  125. berr("Read from offset %d failed: %d\n",
  126. offset, (int)nbytes);
  127. return nbytes;
  128. }
  129. }
  130. else if (nbytes == 0)
  131. {
  132. berr("Unexpected end of file\n");
  133. return -ENODATA;
  134. }
  135. else
  136. {
  137. bytesread += nbytes;
  138. bytesleft -= nbytes;
  139. bufptr += nbytes;
  140. offset += nbytes;
  141. }
  142. }
  143. while (bytesread < readsize);
  144. nxflat_dumpreaddata(buffer, readsize);
  145. return OK;
  146. }