fx_user_driver.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /***************************************************************************
  2. * Copyright (c) 2024 Microsoft Corporation
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the MIT License which is available at
  6. * https://opensource.org/licenses/MIT.
  7. *
  8. * SPDX-License-Identifier: MIT
  9. **************************************************************************/
  10. /**************************************************************************/
  11. /**************************************************************************/
  12. /** */
  13. /** FileX Component */
  14. /** */
  15. /** RAM Disk Driver */
  16. /** */
  17. /**************************************************************************/
  18. /**************************************************************************/
  19. /* Include necessary system files. */
  20. #include <rtthread.h>
  21. #include <rtdevice.h>
  22. #include "fx_api.h"
  23. #define DRV_DEBUG
  24. #define LOG_TAG "drv.filex"
  25. #include <drv_log.h>
  26. /* The RAM driver relies on the fx_media_format call to be made prior to
  27. the fx_media_open call. The following call will format the default
  28. 32KB RAM drive, with a sector size of 128 bytes per sector.
  29. fx_media_format(&ram_disk,
  30. _fx_ram_driver, // Driver entry
  31. ram_disk_memory, // RAM disk memory pointer
  32. media_memory, // Media buffer pointer
  33. sizeof(media_memory), // Media buffer size
  34. "MY_RAM_DISK", // Volume Name
  35. 1, // Number of FATs
  36. 32, // Directory Entries
  37. 0, // Hidden sectors
  38. 256, // Total sectors
  39. 128, // Sector size
  40. 1, // Sectors per cluster
  41. 1, // Heads
  42. 1); // Sectors per track
  43. */
  44. VOID _fx_user_driver(FX_MEDIA *media_ptr);
  45. /**************************************************************************/
  46. /* */
  47. /* FUNCTION RELEASE */
  48. /* */
  49. /* _fx_ram_driver PORTABLE C */
  50. /* 6.1.5 */
  51. /* AUTHOR */
  52. /* */
  53. /* William E. Lamie, Microsoft Corporation */
  54. /* */
  55. /* DESCRIPTION */
  56. /* */
  57. /* This function is the entry point to the generic RAM disk driver */
  58. /* that is delivered with all versions of FileX. The format of the */
  59. /* RAM disk is easily modified by calling fx_media_format prior */
  60. /* to opening the media. */
  61. /* */
  62. /* This driver also serves as a template for developing FileX drivers */
  63. /* for actual devices. Simply replace the read/write sector logic with */
  64. /* calls to read/write from the appropriate physical device */
  65. /* */
  66. /* FileX RAM/FLASH structures look like the following: */
  67. /* */
  68. /* Physical Sector Contents */
  69. /* */
  70. /* 0 Boot record */
  71. /* 1 FAT Area Start */
  72. /* +FAT Sectors Root Directory Start */
  73. /* +Directory Sectors Data Sector Start */
  74. /* */
  75. /* */
  76. /* INPUT */
  77. /* */
  78. /* media_ptr Media control block pointer */
  79. /* */
  80. /* OUTPUT */
  81. /* */
  82. /* None */
  83. /* */
  84. /* CALLS */
  85. /* */
  86. /* _fx_utility_memory_copy Copy sector memory */
  87. /* _fx_utility_16_unsigned_read Read 16-bit unsigned */
  88. /* */
  89. /* CALLED BY */
  90. /* */
  91. /* FileX System Functions */
  92. /* */
  93. /* RELEASE HISTORY */
  94. /* */
  95. /* DATE NAME DESCRIPTION */
  96. /* */
  97. /* 05-19-2020 William E. Lamie Initial Version 6.0 */
  98. /* 09-30-2020 William E. Lamie Modified comment(s), */
  99. /* resulting in version 6.1 */
  100. /* 03-02-2021 William E. Lamie Modified comment(s), */
  101. /* resulting in version 6.1.5 */
  102. /* */
  103. /**************************************************************************/
  104. VOID _fx_user_driver(FX_MEDIA *media_ptr)
  105. {
  106. UCHAR *source_buffer;
  107. UCHAR *destination_buffer;
  108. UINT bytes_per_sector;
  109. /* There are several useful/important pieces of information contained in
  110. the media structure, some of which are supplied by FileX and others
  111. are for the driver to setup. The following is a summary of the
  112. necessary FX_MEDIA structure members:
  113. FX_MEDIA Member Meaning
  114. fx_media_driver_request FileX request type. Valid requests from
  115. FileX are as follows:
  116. FX_DRIVER_READ
  117. FX_DRIVER_WRITE
  118. FX_DRIVER_FLUSH
  119. FX_DRIVER_ABORT
  120. FX_DRIVER_INIT
  121. FX_DRIVER_BOOT_READ
  122. FX_DRIVER_RELEASE_SECTORS
  123. FX_DRIVER_BOOT_WRITE
  124. FX_DRIVER_UNINIT
  125. fx_media_driver_status This value is RETURNED by the driver.
  126. If the operation is successful, this
  127. field should be set to FX_SUCCESS for
  128. before returning. Otherwise, if an
  129. error occurred, this field should be
  130. set to FX_IO_ERROR.
  131. fx_media_driver_buffer Pointer to buffer to read or write
  132. sector data. This is supplied by
  133. FileX.
  134. fx_media_driver_logical_sector Logical sector FileX is requesting.
  135. fx_media_driver_sectors Number of sectors FileX is requesting.
  136. The following is a summary of the optional FX_MEDIA structure members:
  137. FX_MEDIA Member Meaning
  138. fx_media_driver_info Pointer to any additional information
  139. or memory. This is optional for the
  140. driver use and is setup from the
  141. fx_media_open call. The RAM disk uses
  142. this pointer for the RAM disk memory
  143. itself.
  144. fx_media_driver_write_protect The DRIVER sets this to FX_TRUE when
  145. media is write protected. This is
  146. typically done in initialization,
  147. but can be done anytime.
  148. fx_media_driver_free_sector_update The DRIVER sets this to FX_TRUE when
  149. it needs to know when clusters are
  150. released. This is important for FLASH
  151. wear-leveling drivers.
  152. fx_media_driver_system_write FileX sets this flag to FX_TRUE if the
  153. sector being written is a system sector,
  154. e.g., a boot, FAT, or directory sector.
  155. The driver may choose to use this to
  156. initiate error recovery logic for greater
  157. fault tolerance.
  158. fx_media_driver_data_sector_read FileX sets this flag to FX_TRUE if the
  159. sector(s) being read are file data sectors,
  160. i.e., NOT system sectors.
  161. fx_media_driver_sector_type FileX sets this variable to the specific
  162. type of sector being read or written. The
  163. following sector types are identified:
  164. FX_UNKNOWN_SECTOR
  165. FX_BOOT_SECTOR
  166. FX_FAT_SECTOR
  167. FX_DIRECTORY_SECTOR
  168. FX_DATA_SECTOR
  169. */
  170. /* Process the driver request specified in the media control block. */
  171. switch (media_ptr -> fx_media_driver_request)
  172. {
  173. case FX_DRIVER_READ:
  174. {
  175. /* Calculate the RAM disk sector offset. Note the RAM disk memory is pointed to by
  176. the fx_media_driver_info pointer, which is supplied by the application in the
  177. call to fx_media_open. */
  178. source_buffer = ((UCHAR *)media_ptr -> fx_media_driver_info) +
  179. ((media_ptr -> fx_media_driver_logical_sector +
  180. media_ptr -> fx_media_hidden_sectors));
  181. LOG_D("read from device");
  182. LOG_D("logical_sector:%d, hidden_sectors:%d, sectors:%d",
  183. media_ptr -> fx_media_driver_logical_sector,
  184. media_ptr -> fx_media_hidden_sectors,
  185. media_ptr -> fx_media_driver_sectors);
  186. LOG_D("source_buffer:%p, destination_buffer:%p", source_buffer, media_ptr -> fx_media_driver_buffer);
  187. /* Copy the RAM sector into the destination. */
  188. if(rt_device_read(media_ptr ->rt_blk_device,
  189. (rt_off_t)source_buffer,
  190. media_ptr -> fx_media_driver_buffer,
  191. media_ptr -> fx_media_driver_sectors)!=media_ptr -> fx_media_driver_sectors)
  192. {
  193. media_ptr -> fx_media_driver_status = FX_IO_ERROR;
  194. while(1);
  195. break;
  196. }
  197. /* Successful driver request. */
  198. media_ptr -> fx_media_driver_status = FX_SUCCESS;
  199. break;
  200. }
  201. case FX_DRIVER_WRITE:
  202. {
  203. /* Calculate the RAM disk sector offset. Note the RAM disk memory is pointed to by
  204. the fx_media_driver_info pointer, which is supplied by the application in the
  205. call to fx_media_open. */
  206. destination_buffer = ((UCHAR *)media_ptr -> fx_media_driver_info) +
  207. ((media_ptr -> fx_media_driver_logical_sector +
  208. media_ptr -> fx_media_hidden_sectors));
  209. /* Copy the source to the RAM sector. */
  210. LOG_D("write to device");
  211. LOG_D("logical_sector:%d, hidden_sectors:%d, sectors:%d",
  212. media_ptr -> fx_media_driver_logical_sector,
  213. media_ptr -> fx_media_hidden_sectors,
  214. media_ptr -> fx_media_driver_sectors);
  215. LOG_D("source_buffer:%p, destination_buffer:%p, bytes_per_sector:%d", media_ptr -> fx_media_driver_buffer, destination_buffer, media_ptr -> fx_media_bytes_per_sector);
  216. if(rt_device_write(media_ptr ->rt_blk_device,
  217. (rt_off_t)destination_buffer,
  218. media_ptr -> fx_media_driver_buffer,
  219. media_ptr -> fx_media_driver_sectors)!=media_ptr -> fx_media_driver_sectors)
  220. {
  221. media_ptr -> fx_media_driver_status = FX_IO_ERROR;
  222. while(1);
  223. break;
  224. }
  225. /* Successful driver request. */
  226. media_ptr -> fx_media_driver_status = FX_SUCCESS;
  227. break;
  228. }
  229. case FX_DRIVER_FLUSH:
  230. {
  231. /* Return driver success. */
  232. media_ptr -> fx_media_driver_status = FX_SUCCESS;
  233. break;
  234. }
  235. case FX_DRIVER_ABORT:
  236. {
  237. /* Return driver success. */
  238. media_ptr -> fx_media_driver_status = FX_SUCCESS;
  239. break;
  240. }
  241. case FX_DRIVER_INIT:
  242. {
  243. /* FLASH drivers are responsible for setting several fields in the
  244. media structure, as follows:
  245. media_ptr -> fx_media_driver_free_sector_update
  246. media_ptr -> fx_media_driver_write_protect
  247. The fx_media_driver_free_sector_update flag is used to instruct
  248. FileX to inform the driver whenever sectors are not being used.
  249. This is especially useful for FLASH managers so they don't have
  250. maintain mapping for sectors no longer in use.
  251. The fx_media_driver_write_protect flag can be set anytime by the
  252. driver to indicate the media is not writable. Write attempts made
  253. when this flag is set are returned as errors. */
  254. /* Perform basic initialization here... since the boot record is going
  255. to be read subsequently and again for volume name requests. */
  256. /*find device in rtthread*/
  257. rt_device_t device = rt_device_find("filesystem");
  258. if (device == RT_NULL)
  259. {
  260. /* Error, the device is not found */
  261. media_ptr -> fx_media_driver_status = FX_MEDIA_NOT_OPEN;
  262. while(1);
  263. return;
  264. }
  265. /*register to fal device */
  266. media_ptr ->rt_blk_device = device;
  267. /*open device*/
  268. if(rt_device_open(media_ptr ->rt_blk_device, RT_DEVICE_OFLAG_RDWR)!=RT_EOK)
  269. {
  270. media_ptr -> fx_media_driver_status = FX_MEDIA_NOT_OPEN;
  271. while(1);
  272. return;
  273. }
  274. /* Successful driver request. */
  275. media_ptr -> fx_media_driver_status = FX_SUCCESS;
  276. break;
  277. }
  278. case FX_DRIVER_UNINIT:
  279. {
  280. /* There is nothing to do in this case for the RAM driver. For actual
  281. devices some shutdown processing may be necessary. */
  282. //rt_device_t device = rt_device_find("filesystem");
  283. /*close device*/
  284. rt_device_close(media_ptr ->rt_blk_device);
  285. /* Successful driver request. */
  286. media_ptr -> fx_media_driver_status = FX_SUCCESS;
  287. break;
  288. }
  289. case FX_DRIVER_BOOT_READ:
  290. {
  291. /* Read the boot record and return to the caller. */
  292. /* Calculate the RAM disk boot sector offset, which is at the very beginning of
  293. the RAM disk. Note the RAM disk memory is pointed to by the
  294. fx_media_driver_info pointer, which is supplied by the application in the
  295. call to fx_media_open. */
  296. source_buffer = (UCHAR *)media_ptr -> fx_media_driver_info;
  297. /* For RAM driver, determine if the boot record is valid. */
  298. if ((source_buffer[0] != (UCHAR)0xEB) ||
  299. ((source_buffer[1] != (UCHAR)0x34) &&
  300. (source_buffer[1] != (UCHAR)0x76)) ||
  301. (source_buffer[2] != (UCHAR)0x90))
  302. {
  303. /* Invalid boot record, return an error! */
  304. media_ptr -> fx_media_driver_status = FX_MEDIA_INVALID;
  305. return;
  306. }
  307. // /* For RAM disk only, pickup the bytes per sector. */
  308. // bytes_per_sector = _fx_utility_16_unsigned_read(&source_buffer[FX_BYTES_SECTOR]);
  309. // /* Ensure this is less than the media memory size. */
  310. // if (bytes_per_sector > media_ptr -> fx_media_memory_size)
  311. // {
  312. // media_ptr -> fx_media_driver_status = FX_BUFFER_ERROR;
  313. // break;
  314. // }
  315. /* Copy the RAM boot sector into the destination. */
  316. // _fx_utility_memory_copy(source_buffer, media_ptr -> fx_media_driver_buffer,
  317. // bytes_per_sector);
  318. LOG_D("read boot record from device");
  319. LOG_D("source_buffer:%p, destination_buffer:%p, bytes_per_sector:%d", source_buffer, media_ptr -> fx_media_driver_buffer, media_ptr -> fx_media_bytes_per_sector);
  320. if(rt_device_read(media_ptr ->rt_blk_device,
  321. (rt_off_t)source_buffer,
  322. media_ptr -> fx_media_driver_buffer,
  323. 1)!=1)
  324. {
  325. media_ptr -> fx_media_driver_status = FX_IO_ERROR;
  326. while(1);
  327. break;
  328. }
  329. /* Successful driver request. */
  330. media_ptr -> fx_media_driver_status = FX_SUCCESS;
  331. break;
  332. }
  333. case FX_DRIVER_BOOT_WRITE:
  334. {
  335. /* Write the boot record and return to the caller. */
  336. /* Calculate the RAM disk boot sector offset, which is at the very beginning of the
  337. RAM disk. Note the RAM disk memory is pointed to by the fx_media_driver_info
  338. pointer, which is supplied by the application in the call to fx_media_open. */
  339. destination_buffer = (UCHAR *)media_ptr -> fx_media_driver_info;
  340. /* Copy the RAM boot sector into the destination. */
  341. // _fx_utility_memory_copy(media_ptr -> fx_media_driver_buffer, destination_buffer,
  342. // media_ptr -> fx_media_bytes_per_sector);
  343. LOG_D("write boot record to device");
  344. LOG_D("source_buffer:%p, destination_buffer:%p, bytes_per_sector:%d", media_ptr -> fx_media_driver_buffer, destination_buffer, media_ptr -> fx_media_bytes_per_sector);
  345. rt_device_write(media_ptr ->rt_blk_device,
  346. (rt_off_t)destination_buffer,
  347. media_ptr -> fx_media_driver_buffer,
  348. 1);
  349. /* Successful driver request. */
  350. media_ptr -> fx_media_driver_status = FX_SUCCESS;
  351. break;
  352. }
  353. default:
  354. {
  355. /* Invalid driver request. */
  356. media_ptr -> fx_media_driver_status = FX_IO_ERROR;
  357. while(1);
  358. break;
  359. }
  360. }
  361. }
  362. FX_MEDIA user_disk;
  363. FX_FILE my_file;
  364. unsigned char media_memory[4096];
  365. int rt_cp_filex_init(void)
  366. {
  367. extern void _fx_user_driver(FX_MEDIA *media_ptr);
  368. UINT status;
  369. ULONG actual;
  370. CHAR local_buffer[30];
  371. /* Format the RAM disk - the memory for the RAM disk was setup in
  372. tx_application_define above. */
  373. UINT state = fx_media_format(&user_disk,
  374. _fx_user_driver, // Driver entry
  375. 0, // RAM disk memory pointer
  376. media_memory, // Media buffer pointer
  377. sizeof(media_memory), // Media buffer size
  378. "MY_USER_DISK", // Volume Name
  379. 1, // Number of FATs
  380. 32, // Directory Entries
  381. 0, // Hidden sectors
  382. 64, // Total sectors
  383. 4096, // Sector size
  384. 8, // Sectors per cluster
  385. 1, // Heads
  386. 1); // Sectors per track
  387. if(state != FX_SUCCESS)
  388. {
  389. LOG_D("fx_media_format failed(0x%x)", state);
  390. return -1;
  391. }
  392. LOG_D("fx_media_format success");
  393. return 0;
  394. }
  395. INIT_APP_EXPORT(rt_cp_filex_init);