bchlib_read.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /****************************************************************************
  2. * drivers/bch/bchlib_read.c
  3. *
  4. * Copyright (C) 2008-2009 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 <string.h>
  42. #include <errno.h>
  43. #include <assert.h>
  44. #include <debug.h>
  45. #include <nuttx/fs/fs.h>
  46. #include "bch_internal.h"
  47. /****************************************************************************
  48. * Private Types
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Private Function Prototypes
  52. ****************************************************************************/
  53. /****************************************************************************
  54. * Private Data
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Private Functions
  58. ****************************************************************************/
  59. /****************************************************************************
  60. * Public Functions
  61. ****************************************************************************/
  62. /****************************************************************************
  63. * Name: bchlib_read
  64. *
  65. * Description:
  66. * Read from the block device set-up by bchlib_setup as if it were a character
  67. * device.
  68. *
  69. ****************************************************************************/
  70. ssize_t bchlib_read(FAR void *handle, FAR char *buffer, size_t offset, size_t len)
  71. {
  72. FAR struct bchlib_s *bch = (FAR struct bchlib_s *)handle;
  73. size_t nsectors;
  74. size_t sector;
  75. uint16_t sectoffset;
  76. size_t nbytes;
  77. size_t bytesread;
  78. int ret;
  79. /* Get rid of this special case right away */
  80. if (len < 1)
  81. {
  82. return 0;
  83. }
  84. /* Convert the file position into a sector number an offset. */
  85. sector = offset / bch->sectsize;
  86. sectoffset = offset - sector * bch->sectsize;
  87. if (sector >= bch->nsectors)
  88. {
  89. /* Return end-of-file */
  90. return 0;
  91. }
  92. /* Read the initial partial sector */
  93. bytesread = 0;
  94. if (sectoffset > 0)
  95. {
  96. /* Read the sector into the sector buffer */
  97. bchlib_readsector(bch, sector);
  98. /* Copy the tail end of the sector to the user buffer */
  99. if (sectoffset + len > bch->sectsize)
  100. {
  101. nbytes = bch->sectsize - sectoffset;
  102. }
  103. else
  104. {
  105. nbytes = len;
  106. }
  107. memcpy(buffer, &bch->buffer[sectoffset], nbytes);
  108. /* Adjust pointers and counts */
  109. sectoffset = 0;
  110. sector++;
  111. if (sector >= bch->nsectors)
  112. {
  113. return nbytes;
  114. }
  115. bytesread = nbytes;
  116. buffer += nbytes;
  117. len -= nbytes;
  118. }
  119. /* Then read all of the full sectors following the partial sector directly
  120. * into the user buffer.
  121. */
  122. if (len >= bch->sectsize )
  123. {
  124. nsectors = len / bch->sectsize;
  125. if (sector + nsectors > bch->nsectors)
  126. {
  127. nsectors = bch->nsectors - sector;
  128. }
  129. ret = bch->inode->u.i_bops->read(bch->inode, (FAR uint8_t *)buffer,
  130. sector, nsectors);
  131. if (ret < 0)
  132. {
  133. fdbg("Read failed: %d\n");
  134. return ret;
  135. }
  136. /* Adjust pointers and counts */
  137. sectoffset = 0;
  138. sector += nsectors;
  139. nbytes = nsectors * bch->sectsize;
  140. bytesread += nbytes;
  141. if (sector >= bch->nsectors)
  142. {
  143. return bytesread;
  144. }
  145. buffer += nbytes;
  146. len -= nbytes;
  147. }
  148. /* Then read any partial final sector */
  149. if (len > 0)
  150. {
  151. /* Read the sector into the sector buffer */
  152. bchlib_readsector(bch, sector);
  153. /* Copy the head end of the sector to the user buffer */
  154. memcpy(buffer, bch->buffer, len);
  155. /* Adjust counts */
  156. bytesread += len;
  157. }
  158. return bytesread;
  159. }