phatutil.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * Copyright (C) 2005-2006 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/phatutil.c
  34. * \brief PHAT File System.
  35. *
  36. * \verbatim
  37. *
  38. * $Log$
  39. * Revision 1.7 2009/02/13 14:52:05 haraldkipp
  40. * Include memdebug.h for heap management debugging support.
  41. *
  42. * Revision 1.6 2008/08/11 06:59:42 haraldkipp
  43. * BSD types replaced by stdint types (feature request #1282721).
  44. *
  45. * Revision 1.5 2006/10/08 16:48:09 haraldkipp
  46. * Documentation fixed
  47. *
  48. * Revision 1.4 2006/06/18 16:39:46 haraldkipp
  49. * File modification date changed from GMT to local time for Windows
  50. * compatibility.
  51. * No need to set errno after malloc failed.
  52. * Support for long filenames (VFAT) added.
  53. * Fixed positioning bug, which caused several problems like limiting
  54. * directories to one cluster.
  55. *
  56. * Revision 1.3 2006/03/02 19:59:56 haraldkipp
  57. * ICCARM insists on a (void *) typecast for the second parameter of memcpy().
  58. *
  59. * Revision 1.2 2006/02/23 15:45:22 haraldkipp
  60. * PHAT file system now supports configurable number of sector buffers.
  61. * This dramatically increased write rates of no-name cards.
  62. * AVR compile errors corrected.
  63. *
  64. * Revision 1.1 2006/01/05 16:31:50 haraldkipp
  65. * First check-in.
  66. *
  67. *
  68. * \endverbatim
  69. */
  70. #include <fs/phatfs.h>
  71. #include <fs/phatvol.h>
  72. #include <fs/phatdir.h>
  73. #include <fs/phatutil.h>
  74. #include <stdlib.h>
  75. #include <string.h>
  76. #include <time.h>
  77. #include <ctype.h>
  78. #include <errno.h>
  79. #include <memdebug.h>
  80. #if 0
  81. /* Use for local debugging. */
  82. #define NUTDEBUG
  83. #include <stdio.h>
  84. #include <fs/phatdbg.h>
  85. #endif
  86. /*!
  87. * \addtogroup xgPhatUtil
  88. */
  89. /*@{*/
  90. /*!
  91. * \brief Create a DOS timestamp of the current time and date.
  92. *
  93. * \param dostim Receives the time of day part.
  94. * \param dosdat Receives the calendar date part.
  95. */
  96. void GetDosTimeStamp(uint16_t * dostim, uint16_t * dosdat)
  97. {
  98. time_t now;
  99. struct _tm *gmt;
  100. time(&now);
  101. gmt = localtime(&now);
  102. if (dosdat) {
  103. *dosdat = (uint16_t) (gmt->tm_mday | ((gmt->tm_mon + 1) << 5) | ((gmt->tm_year - 80) << 9));
  104. }
  105. if (dostim) {
  106. *dostim = (uint16_t) ((gmt->tm_sec / 2) | (gmt->tm_min << 5) | (gmt->tm_hour << 11));
  107. }
  108. }
  109. /*!
  110. * \brief Convert filename to a directory entry name.
  111. *
  112. * \param src Original name in the format 'name.ext'.
  113. * \param dst Converted name in the format 'name ext'.
  114. *
  115. * \return 0 on success, 1 if the name contains wildcards or
  116. * -1 if the name is malformed.
  117. */
  118. int MakePhatName(const char *src, uint8_t * dst)
  119. {
  120. int rc = 0;
  121. int i;
  122. /* Fill destination with spaces. */
  123. memset(dst, ' ', 11);
  124. /* Skip leading dots. */
  125. for (i = 0; src[i] == '.'; i++);
  126. /* Handle the special entries dot and double dot. */
  127. if (src[i] == 0 && (i == 1 || i == 2)) {
  128. while (i--) {
  129. *dst++ = '.';
  130. }
  131. return 0;
  132. }
  133. /* Convert the special token of removed entries. */
  134. if (*src == (char)PHAT_REM_DIRENT) {
  135. dst[0] = PHAT_REM_NAMENT;
  136. src++;
  137. } else {
  138. dst[0] = toupper((unsigned char)*src);
  139. src++;
  140. }
  141. /* Copy the rest of the base name upto the dot, 8 characters max. */
  142. for (i = 1; i < 8 && *src && *src != '.'; i++, src++) {
  143. dst[i] = toupper((unsigned char)*src);
  144. }
  145. /* More characters? */
  146. if (*src) {
  147. /* If we didn't reach a dot, then the base name is too long. */
  148. if (*src != '.') {
  149. return -1;
  150. }
  151. /* Skip the dot and copy the extension. */
  152. src++;
  153. for (i = 8; i < 11 && *src; i++, src++) {
  154. dst[i] = toupper((unsigned char)*src);
  155. }
  156. /* If more characters are available, then the extension is too long. */
  157. if (*src) {
  158. return -1;
  159. }
  160. }
  161. /* Finally check our result. */
  162. for (i = 0; i < 11; i++) {
  163. /* Reject control codes. */
  164. if (dst[i] < ' ') {
  165. return -1;
  166. }
  167. /* Reject illegal characters. */
  168. if (strchr("\"+,./:;<=>[\\]^", dst[i])) {
  169. return -1;
  170. }
  171. /* Wildcard found. */
  172. if (dst[i] == '?') {
  173. rc = 1;
  174. } else if (dst[i] == '*') {
  175. rc = 1;
  176. /* Stars in the name will fill the remaining name or
  177. extension part with '?'. */
  178. if (i < 8) {
  179. memset(&dst[i], '?', 8 - i);
  180. } else {
  181. memset(&dst[i], '?', 11 - i);
  182. }
  183. }
  184. }
  185. return rc;
  186. }
  187. /*!
  188. * \brief Convert a directory entry name to a visible file name.
  189. *
  190. * \param src Original name in the format 'name ext'.
  191. * \param dst Converted name in the format 'name.ext'.
  192. *
  193. * \return 0 on success, 1 if the name contains wildcards or
  194. * -1 if the name is malformed.
  195. */
  196. void MakeVisibleName(const uint8_t * src, char *dst)
  197. {
  198. int i;
  199. /* Replace the 0x05 cludge. */
  200. if (src[0] == PHAT_REM_NAMENT) {
  201. *dst++ = PHAT_REM_DIRENT;
  202. } else {
  203. *dst++ = src[0];
  204. }
  205. /* Copy the base name part up to the first space. */
  206. for (i = 1; i < 8 && src[i] != ' '; i++) {
  207. *dst++ = src[i];
  208. }
  209. /* Add the extension up to the first space. */
  210. for (i = 8; i < 11 && src[i] != ' '; i++) {
  211. if (i == 8) {
  212. *dst++ = '.';
  213. }
  214. *dst++ = src[i];
  215. }
  216. *dst = 0;
  217. }
  218. /*!
  219. * \brief Chop off the last component of a path.
  220. *
  221. * \param path Full path.
  222. * \param comp Points to a pointer which will be set to the last component
  223. * within the full path.
  224. *
  225. * \return Pointer to a newly allocated string containing the parent path,
  226. * or NULL in case of an error. The allocated buffer must be
  227. * released by the caller.
  228. */
  229. char *GetParentPath(const char *path, const char **comp)
  230. {
  231. char *parent;
  232. int len;
  233. if ((*comp = strrchr(path, '/')) == NULL) {
  234. errno = EINVAL;
  235. return NULL;
  236. }
  237. (*comp)++;
  238. len = strlen(path) - strlen(*comp);
  239. if (len < 2) {
  240. len = 2;
  241. }
  242. if ((parent = malloc(len)) == NULL) {
  243. return NULL;
  244. }
  245. memcpy(parent, (void *)path, len - 1);
  246. parent[len - 1] = 0;
  247. return parent;
  248. }
  249. /*!
  250. * \brief Test for PHAT12/PHAT16 root directory.
  251. *
  252. * \param ndp Handle of an opened file to check.
  253. *
  254. * \return 0 if the file is not a fixed root directory.
  255. */
  256. int IsFixedRootDir(NUTFILE * ndp)
  257. {
  258. NUTDEVICE *dev = ndp->nf_dev;
  259. PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
  260. PHATFILE *fcb;
  261. /* PHAT32 root directories are expandable. */
  262. if (vol->vol_type == 32) {
  263. return 0;
  264. }
  265. /* Root directory cluster number is 0 per definition. */
  266. fcb = ndp->nf_fcb;
  267. if (fcb->f_de_sect || fcb->f_dirent.dent_clusthi || fcb->f_dirent.dent_clust) {
  268. return 0;
  269. }
  270. return 1;
  271. }
  272. /*!
  273. * \brief Set file pointer back to zero.
  274. *
  275. * \param fcb Specifies the file control block.
  276. */
  277. void PhatFilePosRewind(PHATFILE * fcb)
  278. {
  279. /* Reset current pointer into the file. */
  280. fcb->f_pos = 0;
  281. /* Reset current cluster to the first cluster. */
  282. fcb->f_clust = fcb->f_dirent.dent_clusthi;
  283. fcb->f_clust <<= 16;
  284. fcb->f_clust += fcb->f_dirent.dent_clust;
  285. /* Reset position (sector number) within the cluster. */
  286. fcb->f_clust_pos = 0;
  287. /* Reset current position into the current sector. */
  288. fcb->f_sect_pos = 0;
  289. }
  290. /*!
  291. * \brief Move file pointer to a specified position.
  292. *
  293. * Moving beyond the current file size is not supported.
  294. *
  295. * \param nfp File descriptor.
  296. * \param pos Requested file position.
  297. *
  298. * \return 0 on success, -1 otherwise. In the latter case the position
  299. * is unspecified.
  300. */
  301. int PhatFilePosSet(NUTFILE * nfp, uint32_t pos)
  302. {
  303. uint32_t dist;
  304. uint32_t step;
  305. uint32_t clust;
  306. PHATFILE *fcb = nfp->nf_fcb;
  307. NUTDEVICE *dev = nfp->nf_dev;
  308. PHATVOL *vol = (PHATVOL *) dev->dev_dcb;
  309. /* Simple case, rewind the file. */
  310. if (pos == 0) {
  311. PhatFilePosRewind(fcb);
  312. return 0;
  313. }
  314. /* We do not support seeking beyond the file size plus one. */
  315. if ((fcb->f_dirent.dent_attr & PHAT_FATTR_DIR) == 0 && pos > fcb->f_dirent.dent_fsize) {
  316. return -1;
  317. }
  318. /*
  319. * If the requested position is in front of the currect position,
  320. * then we start from the beginning.
  321. * TODO: Should check if we still are in the current cluster.
  322. */
  323. if (pos < fcb->f_pos) {
  324. PhatFilePosRewind(fcb);
  325. dist = pos;
  326. } else {
  327. dist = pos - fcb->f_pos;
  328. }
  329. for (;;) {
  330. if (fcb->f_sect_pos >= vol->vol_sectsz) {
  331. if (IsFixedRootDir(nfp)) {
  332. if (fcb->f_clust_pos + 1 >= vol->vol_rootsz) {
  333. /* End of root directory, abort reading. */
  334. break;
  335. }
  336. fcb->f_clust_pos++;
  337. }
  338. else {
  339. /*
  340. * We reached the end of the current sector. Move to the
  341. * next sector of the current cluster.
  342. */
  343. if (fcb->f_clust_pos + 1 >= vol->vol_clustsz) {
  344. /*
  345. * We reached the end of the current cluster. Move to
  346. * the next cluster.
  347. */
  348. if (vol->vol_type == 32) {
  349. if (Phat32GetClusterLink(dev, fcb->f_clust, &clust)) {
  350. break;
  351. }
  352. if (clust >= (PHATEOC & PHAT32CMASK)) {
  353. break;
  354. }
  355. } else if (vol->vol_type == 16) {
  356. if (Phat16GetClusterLink(dev, fcb->f_clust, &clust)) {
  357. break;
  358. }
  359. if (clust >= (PHATEOC & PHAT16CMASK)) {
  360. break;
  361. }
  362. } else if (Phat12GetClusterLink(dev, fcb->f_clust, &clust)) {
  363. break;
  364. }
  365. else if (clust >= (PHATEOC & PHAT12CMASK)) {
  366. break;
  367. }
  368. fcb->f_clust_pos = 0;
  369. fcb->f_clust_prv = fcb->f_clust;
  370. fcb->f_clust = clust;
  371. } else {
  372. fcb->f_clust_pos++;
  373. }
  374. }
  375. fcb->f_sect_pos = 0;
  376. }
  377. if (dist == 0) {
  378. break;
  379. }
  380. /* Calculate the number of bytes available in the current sector. */
  381. step = vol->vol_sectsz - fcb->f_sect_pos;
  382. if (step > dist) {
  383. step = dist;
  384. }
  385. fcb->f_sect_pos += step;
  386. fcb->f_pos += step;
  387. dist -= step;
  388. }
  389. return fcb->f_pos == pos ? 0 : -1;
  390. }
  391. /*@}*/