phat16.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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/phat16.c
  34. * \brief PHAT16 specific routines.
  35. *
  36. * \verbatim
  37. *
  38. * $Log$
  39. * Revision 1.6 2008/08/11 06:59:42 haraldkipp
  40. * BSD types replaced by stdint types (feature request #1282721).
  41. *
  42. * Revision 1.5 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.4 2006/01/23 19:52:10 haraldkipp
  48. * Added required typecasts before left shift.
  49. *
  50. * Revision 1.3 2006/01/23 17:33:47 haraldkipp
  51. * Avoid memory alignment errors.
  52. *
  53. * Revision 1.2 2006/01/22 17:43:46 haraldkipp
  54. * Bugfix. Deleting files sometimes corrupted a volume.
  55. *
  56. * Revision 1.1 2006/01/05 16:31:17 haraldkipp
  57. * First check-in.
  58. *
  59. *
  60. * \endverbatim
  61. */
  62. #include <errno.h>
  63. #include <fs/phatfs.h>
  64. #include <fs/phatvol.h>
  65. #include <fs/phatio.h>
  66. /*!
  67. * \addtogroup xgPhat16
  68. */
  69. /*@{*/
  70. /*!
  71. * \brief Calculate table location of a specified cluster.
  72. *
  73. * \param vol Mounted volume.
  74. * \param clust Cluster number of the entry to locate.
  75. * \param tabnum Number of the table.
  76. * \param sect Pointer to the variable that receives the sector of the
  77. * table entry.
  78. * \param pos Pointer to the variable that receives position within
  79. * the sector.
  80. */
  81. static void PhatTableLoc(PHATVOL * vol, uint32_t clust, int tabnum, uint32_t * sect, uint32_t * pos)
  82. {
  83. uint32_t tabpos = clust * 2;
  84. *sect = vol->vol_tab_sect[tabnum] + tabpos / vol->vol_sectsz;
  85. *pos = tabpos % vol->vol_sectsz;
  86. }
  87. /*!
  88. * \brief Get link value of a specified cluster.
  89. *
  90. * \param dev Specifies the file system device.
  91. * \param clust Get the link of this cluster.
  92. * \param link Pointer to a variable which will receive the link.
  93. *
  94. * \return 0 on success or -1 on failure.
  95. */
  96. int Phat16GetClusterLink(NUTDEVICE * dev, uint32_t clust, uint32_t * link)
  97. {
  98. uint32_t sect;
  99. uint32_t pos;
  100. int sbn;
  101. PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
  102. /* Do not seek beyond the end of the chain. */
  103. if (clust >= (PHATEOC & PHAT16CMASK)) {
  104. return -1;
  105. }
  106. /* Load the sector that contains the table entry. */
  107. PhatTableLoc(vol, clust, 0, &sect, &pos);
  108. if ((sbn = PhatSectorLoad(dev, sect)) < 0) {
  109. return -1;
  110. }
  111. /* Get the 16 bit link value. */
  112. *link = vol->vol_buf[sbn].sect_data[pos];
  113. *link += (uint32_t)(vol->vol_buf[sbn].sect_data[pos + 1]) << 8;
  114. PhatSectorBufferRelease(dev, sbn);
  115. return 0;
  116. }
  117. /*!
  118. * \brief Set link value of a specified cluster.
  119. *
  120. * \param dev Specifies the file system device.
  121. * \param clust This cluster will be linked.
  122. * \param link Link to this cluster.
  123. *
  124. * \return 0 on success or -1 on failure.
  125. */
  126. int Phat16SetClusterLink(NUTDEVICE * dev, uint32_t clust, uint32_t link)
  127. {
  128. int tabnum;
  129. uint32_t sect;
  130. uint32_t pos;
  131. int sbn;
  132. PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
  133. for (tabnum = 0; tabnum < 2 && vol->vol_tab_sect[tabnum]; tabnum++) {
  134. PhatTableLoc(vol, clust, tabnum, &sect, &pos);
  135. if ((sbn = PhatSectorLoad(dev, sect)) < 0) {
  136. return -1;
  137. }
  138. vol->vol_buf[sbn].sect_data[pos] = (uint8_t) link;
  139. vol->vol_buf[sbn].sect_data[pos + 1] = (uint8_t) (link >> 8);
  140. vol->vol_buf[sbn].sect_dirty = 1;
  141. PhatSectorBufferRelease(dev, sbn);
  142. }
  143. return 0;
  144. }
  145. /*!
  146. * \brief Release a cluster chain.
  147. *
  148. * \param dev Specifies the file system device.
  149. * \param first First cluster of the chain to release.
  150. *
  151. * \return 0 on success or -1 on failure.
  152. */
  153. int Phat16ReleaseChain(NUTDEVICE * dev, uint32_t first)
  154. {
  155. uint32_t next;
  156. PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
  157. while (first < (PHATEOC & PHAT16CMASK)) {
  158. if (Phat16GetClusterLink(dev, first, &next)) {
  159. /* Read error. */
  160. return -1;
  161. }
  162. if (next < 2) {
  163. /* Incomplete chain, should not happen. */
  164. break;
  165. }
  166. if (Phat16SetClusterLink(dev, first, 0)) {
  167. /* Write error. */
  168. return -1;
  169. }
  170. vol->vol_numfree++;
  171. first = next;
  172. }
  173. return 0;
  174. }
  175. /*@}*/