msc_disk.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2019 Ha Thach (tinyusb.org)
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. *
  24. */
  25. #include "bsp/board.h"
  26. #include "tusb.h"
  27. #if CFG_TUD_MSC
  28. // whether host does safe-eject
  29. static bool ejected = false;
  30. // Some MCU doesn't have enough 8KB SRAM to store the whole disk
  31. // We will use Flash as read-only disk with board that has
  32. // CFG_EXAMPLE_MSC_READONLY defined
  33. #define README_CONTENTS \
  34. "This is the password vault utilities directory.\r\n\r\nWork in progress\r\n"
  35. enum
  36. {
  37. DISK_BLOCK_NUM = 16, // 8KB is the smallest size that windows allow to mount
  38. DISK_BLOCK_SIZE = 512
  39. };
  40. #ifdef CFG_EXAMPLE_MSC_READONLY
  41. const
  42. #endif
  43. uint8_t msc_disk[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] =
  44. {
  45. //------------- Block0: Boot Sector -------------//
  46. // byte_per_sector = DISK_BLOCK_SIZE; fat12_sector_num_16 = DISK_BLOCK_NUM;
  47. // sector_per_cluster = 1; reserved_sectors = 1;
  48. // fat_num = 1; fat12_root_entry_num = 16;
  49. // sector_per_fat = 1; sector_per_track = 1; head_num = 1; hidden_sectors = 0;
  50. // drive_number = 0x80; media_type = 0xf8; extended_boot_signature = 0x29;
  51. // filesystem_type = "FAT12 "; volume_serial_number = 0x1234; volume_label = "TinyUSB MSC";
  52. // FAT magic code at offset 510-511
  53. {
  54. 0xEB, 0x3C, 0x90, 0x4D, 0x53, 0x44, 0x4F, 0x53, 0x35, 0x2E, 0x30, 0x00, 0x02, 0x01, 0x01, 0x00,
  55. 0x01, 0x10, 0x00, 0x10, 0x00, 0xF8, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  56. 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x29, 0x34, 0x12, 0x00, 0x00, 'V' , 'a' , 'u' , 'l' , 't' ,
  57. ' ' , 'T' , 'o' , 'o' , 'l' , 's' , 0x46, 0x41, 0x54, 0x31, 0x32, 0x20, 0x20, 0x20, 0x00, 0x00,
  58. // Zero up to 2 last bytes of FAT magic code
  59. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  63. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  64. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  66. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  68. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  69. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  70. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  71. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  72. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  73. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  74. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  75. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  76. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  77. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  78. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  79. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  80. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  81. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  82. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  83. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  84. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  85. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  86. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xAA
  87. },
  88. //------------- Block1: FAT12 Table -------------//
  89. {
  90. 0xF8, 0xFF, 0xFF, 0xFF, 0x0F // // first 2 entries must be F8FF, third entry is cluster end of readme file
  91. },
  92. //------------- Block2: Root Directory -------------//
  93. {
  94. // first entry is volume label
  95. 'V' , 'a' , 'u' , 'l' , 't' , ' ' , 'T' , 'o' , 'o' , 'l' , 's' , 0x08, 0x00, 0x00, 0x00, 0x00,
  96. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4F, 0x6D, 0x65, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  97. // second entry is readme file
  98. 'R' , 'E' , 'A' , 'D' , 'M' , 'E' , ' ' , ' ' , 'T' , 'X' , 'T' , 0x20, 0x00, 0xC6, 0x52, 0x6D,
  99. 0x65, 0x43, 0x65, 0x43, 0x00, 0x00, 0x88, 0x6D, 0x65, 0x43, 0x02, 0x00,
  100. sizeof(README_CONTENTS)-1, 0x00, 0x00, 0x00 // readme's files size (4 Bytes)
  101. },
  102. //------------- Block3: Readme Content -------------//
  103. README_CONTENTS
  104. };
  105. // Invoked when received SCSI_CMD_INQUIRY
  106. // Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively
  107. void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4])
  108. {
  109. (void) lun;
  110. const char vid[] = "DLX";
  111. const char pid[] = "Vault tools";
  112. const char rev[] = "0.1a";
  113. memcpy(vendor_id , vid, strlen(vid));
  114. memcpy(product_id , pid, strlen(pid));
  115. memcpy(product_rev, rev, strlen(rev));
  116. }
  117. // Invoked when received Test Unit Ready command.
  118. // return true allowing host to read/write this LUN e.g SD card inserted
  119. bool tud_msc_test_unit_ready_cb(uint8_t lun)
  120. {
  121. (void) lun;
  122. // RAM disk is ready until ejected
  123. if (ejected) {
  124. tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00);
  125. return false;
  126. }
  127. return true;
  128. }
  129. // Invoked when received SCSI_CMD_READ_CAPACITY_10 and SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size
  130. // Application update block count and block size
  131. void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size)
  132. {
  133. (void) lun;
  134. *block_count = DISK_BLOCK_NUM;
  135. *block_size = DISK_BLOCK_SIZE;
  136. }
  137. // Invoked when received Start Stop Unit command
  138. // - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
  139. // - Start = 1 : active mode, if load_eject = 1 : load disk storage
  140. bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject)
  141. {
  142. (void) lun;
  143. (void) power_condition;
  144. if ( load_eject )
  145. {
  146. if (start)
  147. {
  148. // load disk storage
  149. }else
  150. {
  151. // unload disk storage
  152. ejected = true;
  153. }
  154. }
  155. return true;
  156. }
  157. // Callback invoked when received READ10 command.
  158. // Copy disk's data to buffer (up to bufsize) and return number of copied bytes.
  159. int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize)
  160. {
  161. (void) lun;
  162. // out of ramdisk
  163. if ( lba >= DISK_BLOCK_NUM ) return -1;
  164. uint8_t const* addr = msc_disk[lba] + offset;
  165. memcpy(buffer, addr, bufsize);
  166. return bufsize;
  167. }
  168. bool tud_msc_is_writable_cb (uint8_t lun)
  169. {
  170. (void) lun;
  171. /* Read only. */
  172. return false;
  173. }
  174. // Callback invoked when received WRITE10 command.
  175. // Process data in buffer to disk's storage and return number of written bytes
  176. int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize)
  177. {
  178. (void) lun;
  179. // out of ramdisk
  180. if ( lba >= DISK_BLOCK_NUM )
  181. return -1;
  182. /* Read only. Silently fail. */
  183. (void) lba; (void) offset; (void) buffer;
  184. return bufsize;
  185. }
  186. // Callback invoked when received an SCSI command not in built-in list below
  187. // - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
  188. // - READ10 and WRITE10 has their own callbacks
  189. int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize)
  190. {
  191. // read10 & write10 has their own callback and MUST not be handled here
  192. void const* response = NULL;
  193. int32_t resplen = 0;
  194. // most scsi handled is input
  195. bool in_xfer = true;
  196. switch (scsi_cmd[0])
  197. {
  198. case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
  199. // Host is about to read/write etc ... better not to disconnect disk
  200. resplen = 0;
  201. break;
  202. default:
  203. // Set Sense = Invalid Command Operation
  204. tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
  205. // negative means error -> tinyusb could stall and/or response with failed status
  206. resplen = -1;
  207. break;
  208. }
  209. // return resplen must not larger than bufsize
  210. if ( resplen > bufsize ) resplen = bufsize;
  211. if ( response && (resplen > 0) )
  212. {
  213. if(in_xfer)
  214. {
  215. memcpy(buffer, response, resplen);
  216. }else
  217. {
  218. // SCSI output
  219. }
  220. }
  221. return resplen;
  222. }
  223. #endif