LocalFileSystem.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "platform/LocalFileSystem.h"
  17. #if DEVICE_LOCALFILESYSTEM
  18. #include "platform/mbed_semihost_api.h"
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <errno.h>
  22. namespace mbed {
  23. /* Extension to FINFO type defined in RTL.h (in Keil RL) - adds 'create time'. */
  24. typedef struct {
  25. unsigned char hr; /* Hours [0..23] */
  26. unsigned char min; /* Minutes [0..59] */
  27. unsigned char sec; /* Seconds [0..59] */
  28. unsigned char day; /* Day [1..31] */
  29. unsigned char mon; /* Month [1..12] */
  30. unsigned short year; /* Year [1980..2107] */
  31. } FTIME;
  32. typedef struct { /* File Search info record */
  33. char name[32]; /* File name */
  34. long size; /* File size in bytes */
  35. int fileID; /* System File Identification */
  36. FTIME create_time; /* Date & time file was created */
  37. FTIME write_time; /* Date & time of last write */
  38. } XFINFO;
  39. #define RESERVED_FOR_USER_APPLICATIONS (0x100) /* 0x100 - 0x1ff */
  40. #define USR_XFFIND (RESERVED_FOR_USER_APPLICATIONS + 0)
  41. static int xffind(const char *pattern, XFINFO *info)
  42. {
  43. unsigned param[4];
  44. param[0] = (unsigned long)pattern;
  45. param[1] = (unsigned long)strlen(pattern);
  46. param[2] = (unsigned long)info;
  47. param[3] = (unsigned long)sizeof(XFINFO);
  48. return __semihost(USR_XFFIND, param);
  49. }
  50. #define OPEN_R 0
  51. #define OPEN_B 1
  52. #define OPEN_PLUS 2
  53. #define OPEN_W 4
  54. #define OPEN_A 8
  55. #define OPEN_INVALID -1
  56. int posix_to_semihost_open_flags(int flags)
  57. {
  58. /* POSIX flags -> semihosting open mode */
  59. int openmode;
  60. if (flags & O_RDWR) {
  61. /* a plus mode */
  62. openmode = OPEN_PLUS;
  63. if (flags & O_APPEND) {
  64. openmode |= OPEN_A;
  65. } else if (flags & O_TRUNC) {
  66. openmode |= OPEN_W;
  67. } else {
  68. openmode |= OPEN_R;
  69. }
  70. } else if (flags & O_WRONLY) {
  71. /* write or append */
  72. if (flags & O_APPEND) {
  73. openmode = OPEN_A;
  74. } else {
  75. openmode = OPEN_W;
  76. }
  77. } else if (flags == O_RDONLY) {
  78. /* read mode */
  79. openmode = OPEN_R;
  80. } else {
  81. /* invalid flags */
  82. openmode = OPEN_INVALID;
  83. }
  84. return openmode;
  85. }
  86. FILEHANDLE local_file_open(const char *name, int flags)
  87. {
  88. int openmode = posix_to_semihost_open_flags(flags);
  89. if (openmode == OPEN_INVALID) {
  90. return (FILEHANDLE)NULL;
  91. }
  92. FILEHANDLE fh = semihost_open(name, openmode);
  93. if (fh == -1) {
  94. return (FILEHANDLE)NULL;
  95. }
  96. return fh;
  97. }
  98. LocalFileHandle::LocalFileHandle(FILEHANDLE fh) : _fh(fh), pos(0)
  99. {
  100. // No lock needed in constructor
  101. }
  102. int LocalFileHandle::close()
  103. {
  104. int retval = semihost_close(_fh);
  105. delete this;
  106. return retval;
  107. }
  108. ssize_t LocalFileHandle::write(const void *buffer, size_t length)
  109. {
  110. lock();
  111. ssize_t n = semihost_write(_fh, (const unsigned char *)buffer, length, 0); // number of characters not written
  112. n = length - n; // number of characters written
  113. pos += n;
  114. unlock();
  115. return n;
  116. }
  117. ssize_t LocalFileHandle::read(void *buffer, size_t length)
  118. {
  119. lock();
  120. ssize_t n = semihost_read(_fh, (unsigned char *)buffer, length, 0); // number of characters not read
  121. n = length - n; // number of characters read
  122. pos += n;
  123. unlock();
  124. return n;
  125. }
  126. int LocalFileHandle::isatty()
  127. {
  128. lock();
  129. int ret = semihost_istty(_fh);
  130. unlock();
  131. return ret;
  132. }
  133. off_t LocalFileHandle::seek(off_t position, int whence)
  134. {
  135. lock();
  136. if (whence == SEEK_CUR) {
  137. position += pos;
  138. } else if (whence == SEEK_END) {
  139. position += semihost_flen(_fh);
  140. } /* otherwise SEEK_SET, so position is fine */
  141. /* Always seems to return -1, so just ignore for now. */
  142. semihost_seek(_fh, position);
  143. pos = position;
  144. unlock();
  145. return position;
  146. }
  147. int LocalFileHandle::sync()
  148. {
  149. lock();
  150. int ret = semihost_ensure(_fh);
  151. unlock();
  152. return ret;
  153. }
  154. off_t LocalFileHandle::size()
  155. {
  156. lock();
  157. off_t off = semihost_flen(_fh);
  158. unlock();
  159. return off;
  160. }
  161. void LocalFileHandle::lock()
  162. {
  163. _mutex.lock();
  164. }
  165. void LocalFileHandle::unlock()
  166. {
  167. _mutex.unlock();
  168. }
  169. class LocalDirHandle : public DirHandle {
  170. public:
  171. XFINFO info;
  172. LocalDirHandle() : info()
  173. {
  174. }
  175. virtual int close()
  176. {
  177. // No lock can be used in destructor
  178. delete this;
  179. return 0;
  180. }
  181. virtual int read(struct dirent *ent)
  182. {
  183. lock();
  184. if (xffind("*", &info) != 0) {
  185. unlock();
  186. return 0;
  187. }
  188. memcpy(ent->d_name, info.name, sizeof(info.name));
  189. unlock();
  190. return 1;
  191. }
  192. virtual void rewind()
  193. {
  194. lock();
  195. info.fileID = 0;
  196. unlock();
  197. }
  198. virtual off_t tell()
  199. {
  200. lock();
  201. int fileId = info.fileID;
  202. unlock();
  203. return fileId;
  204. }
  205. virtual void seek(off_t offset)
  206. {
  207. lock();
  208. info.fileID = offset;
  209. unlock();
  210. }
  211. protected:
  212. PlatformMutex _mutex;
  213. virtual void lock()
  214. {
  215. _mutex.lock();
  216. }
  217. virtual void unlock()
  218. {
  219. _mutex.unlock();
  220. }
  221. };
  222. int LocalFileSystem::open(FileHandle **file, const char *name, int flags)
  223. {
  224. // No global state modified so function is thread safe
  225. /* reject filenames with / in them */
  226. for (const char *tmp = name; *tmp; tmp++) {
  227. if (*tmp == '/') {
  228. return -EINVAL;
  229. }
  230. }
  231. int openmode = posix_to_semihost_open_flags(flags);
  232. if (openmode == OPEN_INVALID) {
  233. return -EINVAL;
  234. }
  235. FILEHANDLE fh = semihost_open(name, openmode);
  236. if (fh == -1) {
  237. return -EIO;
  238. }
  239. *file = new LocalFileHandle(fh);
  240. return 0;
  241. }
  242. int LocalFileSystem::remove(const char *filename)
  243. {
  244. // No global state modified so function is thread safe
  245. return semihost_remove(filename);
  246. }
  247. int LocalFileSystem::open(DirHandle **dir, const char *name)
  248. {
  249. // No global state modified so function is thread safe
  250. *dir = new LocalDirHandle();
  251. return 0;
  252. }
  253. } // namespace mbed
  254. #endif