bchlib_read.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /****************************************************************************
  2. * drivers/bch/bchlib_read.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <sys/types.h>
  25. #include <stdint.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <assert.h>
  29. #include <debug.h>
  30. #include "bch.h"
  31. /****************************************************************************
  32. * Private Types
  33. ****************************************************************************/
  34. /****************************************************************************
  35. * Private Function Prototypes
  36. ****************************************************************************/
  37. /****************************************************************************
  38. * Private Data
  39. ****************************************************************************/
  40. /****************************************************************************
  41. * Private Functions
  42. ****************************************************************************/
  43. /****************************************************************************
  44. * Public Functions
  45. ****************************************************************************/
  46. /****************************************************************************
  47. * Name: bchlib_read
  48. *
  49. * Description:
  50. * Read from the block device set-up by bchlib_setup as if it were a
  51. * character device.
  52. *
  53. ****************************************************************************/
  54. ssize_t bchlib_read(FAR void *handle, FAR char *buffer, size_t offset,
  55. size_t len)
  56. {
  57. FAR struct bchlib_s *bch = (FAR struct bchlib_s *)handle;
  58. size_t nsectors;
  59. size_t sector;
  60. uint16_t sectoffset;
  61. size_t nbytes;
  62. size_t bytesread;
  63. int ret;
  64. /* Get rid of this special case right away */
  65. if (len < 1)
  66. {
  67. return 0;
  68. }
  69. /* Convert the file position into a sector number an offset. */
  70. sector = offset / bch->sectsize;
  71. sectoffset = offset - sector * bch->sectsize;
  72. if (sector >= bch->nsectors)
  73. {
  74. /* Return end-of-file */
  75. return 0;
  76. }
  77. /* Read the initial partial sector */
  78. bytesread = 0;
  79. if (sectoffset > 0)
  80. {
  81. /* Read the sector into the sector buffer */
  82. ret = bchlib_readsector(bch, sector);
  83. if (ret < 0)
  84. {
  85. return ret;
  86. }
  87. /* Copy the tail end of the sector to the user buffer */
  88. if (sectoffset + len > bch->sectsize)
  89. {
  90. nbytes = bch->sectsize - sectoffset;
  91. }
  92. else
  93. {
  94. nbytes = len;
  95. }
  96. memcpy(buffer, &bch->buffer[sectoffset], nbytes);
  97. /* Adjust pointers and counts */
  98. sector++;
  99. if (sector >= bch->nsectors)
  100. {
  101. return nbytes;
  102. }
  103. bytesread = nbytes;
  104. buffer += nbytes;
  105. len -= nbytes;
  106. }
  107. /* Then read all of the full sectors following the partial sector directly
  108. * into the user buffer.
  109. */
  110. if (len >= bch->sectsize)
  111. {
  112. nsectors = len / bch->sectsize;
  113. if (sector + nsectors > bch->nsectors)
  114. {
  115. nsectors = bch->nsectors - sector;
  116. }
  117. ret = bch->inode->u.i_bops->read(bch->inode, (FAR uint8_t *)buffer,
  118. sector, nsectors);
  119. if (ret < 0)
  120. {
  121. ferr("ERROR: Read failed: %d\n", ret);
  122. return ret;
  123. }
  124. /* Adjust pointers and counts */
  125. sector += nsectors;
  126. nbytes = nsectors * bch->sectsize;
  127. bytesread += nbytes;
  128. if (sector >= bch->nsectors)
  129. {
  130. return bytesread;
  131. }
  132. buffer += nbytes;
  133. len -= nbytes;
  134. }
  135. /* Then read any partial final sector */
  136. if (len > 0)
  137. {
  138. /* Read the sector into the sector buffer */
  139. ret = bchlib_readsector(bch, sector);
  140. if (ret < 0)
  141. {
  142. return ret;
  143. }
  144. /* Copy the head end of the sector to the user buffer */
  145. memcpy(buffer, bch->buffer, len);
  146. /* Adjust counts */
  147. bytesread += len;
  148. }
  149. return bytesread;
  150. }