phat12.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (C) 2005 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. */
  32. /*!
  33. * \file fs/phat12.c
  34. * \brief PHAT12 specific routines.
  35. *
  36. * \verbatim
  37. *
  38. * $Log$
  39. * Revision 1.5 2008/08/11 06:59:42 haraldkipp
  40. * BSD types replaced by stdint types (feature request #1282721).
  41. *
  42. * Revision 1.4 2006/02/23 15:45:21 haraldkipp
  43. * PHAT file system now supports configurable number of sector buffers.
  44. * This dramatically increased write rates of no-name cards.
  45. * AVR compile errors corrected.
  46. *
  47. * Revision 1.3 2006/01/23 19:52:10 haraldkipp
  48. * Added required typecasts before left shift.
  49. *
  50. * Revision 1.2 2006/01/22 17:43:46 haraldkipp
  51. * Bugfix. Deleting files sometimes corrupted a volume.
  52. *
  53. * Revision 1.1 2006/01/05 16:31:09 haraldkipp
  54. * First check-in.
  55. *
  56. *
  57. * \endverbatim
  58. */
  59. #include <errno.h>
  60. #include <fs/phatfs.h>
  61. #include <fs/phatvol.h>
  62. #include <fs/phatio.h>
  63. /*!
  64. * \addtogroup xgPhat12
  65. */
  66. /*@{*/
  67. /*!
  68. * \brief Calculate table location of a specified cluster.
  69. *
  70. * \param vol Mounted volume.
  71. * \param clust Cluster number of the entry to locate.
  72. * \param tabnum Number of the table.
  73. * \param sect Pointer to the variable that receives the sector of the
  74. * table entry.
  75. * \param pos Pointer to the variable that receives position within
  76. * the sector.
  77. */
  78. static void PhatTableLoc(PHATVOL * vol, uint32_t clust, int tabnum, uint32_t * sect, uint32_t * pos)
  79. {
  80. uint32_t tabpos = clust + (clust / 2);
  81. *sect = vol->vol_tab_sect[tabnum] + tabpos / vol->vol_sectsz;
  82. *pos = tabpos % vol->vol_sectsz;
  83. }
  84. /*!
  85. * \brief Get link value of a specified cluster.
  86. *
  87. * \param dev Specifies the file system device.
  88. * \param clust Get the link of this cluster.
  89. * \param link Pointer to a variable which will receive the link.
  90. *
  91. * \return 0 on success or -1 on failure.
  92. */
  93. int Phat12GetClusterLink(NUTDEVICE * dev, uint32_t clust, uint32_t * link)
  94. {
  95. uint32_t sect;
  96. uint32_t pos;
  97. int sbn;
  98. PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
  99. /* Do not seek beyond the end of the chain. */
  100. if (clust >= (PHATEOC & PHAT12CMASK)) {
  101. return -1;
  102. }
  103. /* Load the sector that contains the table entry. */
  104. PhatTableLoc(vol, clust, 0, &sect, &pos);
  105. if ((sbn = PhatSectorLoad(dev, sect)) < 0) {
  106. return -1;
  107. }
  108. /* Read the link value. Be aware, that entries may cross sector boundaries. */
  109. *link = vol->vol_buf[sbn].sect_data[pos++];
  110. if (pos >= vol->vol_sectsz) {
  111. PhatSectorBufferRelease(dev, sbn);
  112. if ((sbn = PhatSectorLoad(dev, sect + 1)) < 0) {
  113. return -1;
  114. }
  115. pos = 0;
  116. }
  117. *link += (uint32_t)(vol->vol_buf[sbn].sect_data[pos]) << 8;
  118. PhatSectorBufferRelease(dev, sbn);
  119. /* Adjust the 12 bit position within the 16 bit result. */
  120. if (clust & 1) {
  121. *link >>= 4;
  122. }
  123. *link &= PHAT12CMASK;
  124. return 0;
  125. }
  126. /*!
  127. * \brief Set link value of a specified cluster.
  128. *
  129. * \param dev Specifies the file system device.
  130. * \param clust This cluster will be linked.
  131. * \param link Link to this cluster.
  132. *
  133. * \return 0 on success or -1 on failure.
  134. */
  135. int Phat12SetClusterLink(NUTDEVICE * dev, uint32_t clust, uint32_t link)
  136. {
  137. int tabnum;
  138. uint32_t sect;
  139. uint32_t pos;
  140. uint32_t tval;
  141. int sbn;
  142. PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
  143. for (tabnum = 0; tabnum < 2 && vol->vol_tab_sect[tabnum]; tabnum++) {
  144. /* Load the sector containing the table entry to set. */
  145. PhatTableLoc(vol, clust, tabnum, &sect, &pos);
  146. if ((sbn = PhatSectorLoad(dev, sect)) < 0) {
  147. return -1;
  148. }
  149. /* The new value is 12 bit only. Thus we must load the
  150. * old value and keep the upper or lower 4 bit part. */
  151. tval = vol->vol_buf[sbn].sect_data[pos];
  152. if (pos + 1 < vol->vol_sectsz) {
  153. tval += (uint32_t)(vol->vol_buf[sbn].sect_data[pos + 1]) << 8;
  154. } else {
  155. PhatSectorBufferRelease(dev, sbn);
  156. if ((sbn = PhatSectorLoad(dev, sect + 1)) < 0) {
  157. return -1;
  158. }
  159. tval += (uint32_t)(vol->vol_buf[sbn].sect_data[0]) << 8;
  160. }
  161. link &= PHAT12CMASK;
  162. if (clust & 1) {
  163. tval &= 0x000F;
  164. link <<= 4;
  165. } else {
  166. tval &= 0xF000;
  167. }
  168. tval |= link;
  169. /* Save the modified entry. Again maintain sector boundaries. */
  170. if (pos + 1 < vol->vol_sectsz) {
  171. vol->vol_buf[sbn].sect_data[pos + 1] = (uint8_t) (tval >> 8);
  172. } else {
  173. vol->vol_buf[sbn].sect_data[0] = (uint8_t) (tval >> 8);
  174. vol->vol_buf[sbn].sect_dirty = 1;
  175. PhatSectorBufferRelease(dev, sbn);
  176. if ((sbn = PhatSectorLoad(dev, sect)) < 0) {
  177. return -1;
  178. }
  179. }
  180. vol->vol_buf[sbn].sect_data[pos] = (uint8_t) tval;
  181. vol->vol_buf[sbn].sect_dirty = 1;
  182. PhatSectorBufferRelease(dev, sbn);
  183. }
  184. return 0;
  185. }
  186. /*!
  187. * \brief Release a cluster chain.
  188. *
  189. * \param dev Specifies the file system device.
  190. * \param first First cluster of the chain to release.
  191. *
  192. * \return 0 on success or -1 on failure.
  193. */
  194. int Phat12ReleaseChain(NUTDEVICE * dev, uint32_t first)
  195. {
  196. uint32_t next;
  197. PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
  198. while (first < (PHATEOC & PHAT12CMASK)) {
  199. if (Phat12GetClusterLink(dev, first, &next)) {
  200. /* Read error. */
  201. return -1;
  202. }
  203. if (next < 2) {
  204. /* Incomplete chain, should not happen. */
  205. break;
  206. }
  207. if (Phat12SetClusterLink(dev, first, 0)) {
  208. /* Write error. */
  209. return -1;
  210. }
  211. vol->vol_numfree++;
  212. first = next;
  213. }
  214. return 0;
  215. }
  216. /*@}*/