msc_disk.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 tinyusb's MassStorage Class demo.\r\n\r\n\
  35. If you find any bugs or get any questions, feel free to file an\r\n\
  36. issue at github.com/hathach/tinyusb"
  37. enum
  38. {
  39. DISK_BLOCK_NUM = 16, // 8KB is the smallest size that windows allow to mount
  40. DISK_BLOCK_SIZE = 512
  41. };
  42. #ifdef CFG_EXAMPLE_MSC_READONLY
  43. const
  44. #endif
  45. uint8_t msc_disk[DISK_BLOCK_NUM][DISK_BLOCK_SIZE] =
  46. {
  47. //------------- Block0: Boot Sector -------------//
  48. // byte_per_sector = DISK_BLOCK_SIZE; fat12_sector_num_16 = DISK_BLOCK_NUM;
  49. // sector_per_cluster = 1; reserved_sectors = 1;
  50. // fat_num = 1; fat12_root_entry_num = 16;
  51. // sector_per_fat = 1; sector_per_track = 1; head_num = 1; hidden_sectors = 0;
  52. // drive_number = 0x80; media_type = 0xf8; extended_boot_signature = 0x29;
  53. // filesystem_type = "FAT12 "; volume_serial_number = 0x1234; volume_label = "TinyUSB MSC";
  54. // FAT magic code at offset 510-511
  55. {
  56. 0xEB, 0x3C, 0x90, 0x4D, 0x53, 0x44, 0x4F, 0x53, 0x35, 0x2E, 0x30, 0x00, 0x02, 0x01, 0x01, 0x00,
  57. 0x01, 0x10, 0x00, 0x10, 0x00, 0xF8, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  58. 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x29, 0x34, 0x12, 0x00, 0x00, 'T' , 'i' , 'n' , 'y' , 'U' ,
  59. 'S' , 'B' , ' ' , 'M' , 'S' , 'C' , 0x46, 0x41, 0x54, 0x31, 0x32, 0x20, 0x20, 0x20, 0x00, 0x00,
  60. // Zero up to 2 last bytes of FAT magic code
  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, 0x00, 0x00,
  87. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  88. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, 0xAA
  89. },
  90. //------------- Block1: FAT12 Table -------------//
  91. {
  92. 0xF8, 0xFF, 0xFF, 0xFF, 0x0F // // first 2 entries must be F8FF, third entry is cluster end of readme file
  93. },
  94. //------------- Block2: Root Directory -------------//
  95. {
  96. // first entry is volume label
  97. 'T' , 'i' , 'n' , 'y' , 'U' , 'S' , 'B' , ' ' , 'M' , 'S' , 'C' , 0x08, 0x00, 0x00, 0x00, 0x00,
  98. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4F, 0x6D, 0x65, 0x43, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  99. // second entry is readme file
  100. 'R' , 'E' , 'A' , 'D' , 'M' , 'E' , ' ' , ' ' , 'T' , 'X' , 'T' , 0x20, 0x00, 0xC6, 0x52, 0x6D,
  101. 0x65, 0x43, 0x65, 0x43, 0x00, 0x00, 0x88, 0x6D, 0x65, 0x43, 0x02, 0x00,
  102. sizeof(README_CONTENTS)-1, 0x00, 0x00, 0x00 // readme's files size (4 Bytes)
  103. },
  104. //------------- Block3: Readme Content -------------//
  105. README_CONTENTS
  106. };
  107. // Invoked when received SCSI_CMD_INQUIRY
  108. // Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively
  109. void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4])
  110. {
  111. (void) lun;
  112. const char vid[] = "TinyUSB";
  113. const char pid[] = "Mass Storage";
  114. const char rev[] = "1.0";
  115. memcpy(vendor_id , vid, strlen(vid));
  116. memcpy(product_id , pid, strlen(pid));
  117. memcpy(product_rev, rev, strlen(rev));
  118. }
  119. // Invoked when received Test Unit Ready command.
  120. // return true allowing host to read/write this LUN e.g SD card inserted
  121. bool tud_msc_test_unit_ready_cb(uint8_t lun)
  122. {
  123. (void) lun;
  124. // RAM disk is ready until ejected
  125. if (ejected) {
  126. tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00);
  127. return false;
  128. }
  129. return true;
  130. }
  131. // Invoked when received SCSI_CMD_READ_CAPACITY_10 and SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size
  132. // Application update block count and block size
  133. void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size)
  134. {
  135. (void) lun;
  136. *block_count = DISK_BLOCK_NUM;
  137. *block_size = DISK_BLOCK_SIZE;
  138. }
  139. // Invoked when received Start Stop Unit command
  140. // - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
  141. // - Start = 1 : active mode, if load_eject = 1 : load disk storage
  142. bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject)
  143. {
  144. (void) lun;
  145. (void) power_condition;
  146. if ( load_eject )
  147. {
  148. if (start)
  149. {
  150. // load disk storage
  151. }else
  152. {
  153. // unload disk storage
  154. ejected = true;
  155. }
  156. }
  157. return true;
  158. }
  159. // Callback invoked when received READ10 command.
  160. // Copy disk's data to buffer (up to bufsize) and return number of copied bytes.
  161. int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize)
  162. {
  163. (void) lun;
  164. uint8_t const* addr = msc_disk[lba] + offset;
  165. memcpy(buffer, addr, bufsize);
  166. return bufsize;
  167. }
  168. // Callback invoked when received WRITE10 command.
  169. // Process data in buffer to disk's storage and return number of written bytes
  170. int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize)
  171. {
  172. (void) lun;
  173. #ifndef CFG_EXAMPLE_MSC_READONLY
  174. uint8_t* addr = msc_disk[lba] + offset;
  175. memcpy(addr, buffer, bufsize);
  176. #else
  177. (void) lba; (void) offset; (void) buffer;
  178. #endif
  179. return bufsize;
  180. }
  181. // Callback invoked when received an SCSI command not in built-in list below
  182. // - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
  183. // - READ10 and WRITE10 has their own callbacks
  184. int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize)
  185. {
  186. // read10 & write10 has their own callback and MUST not be handled here
  187. void const* response = NULL;
  188. uint16_t resplen = 0;
  189. // most scsi handled is input
  190. bool in_xfer = true;
  191. switch (scsi_cmd[0])
  192. {
  193. case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
  194. // Host is about to read/write etc ... better not to disconnect disk
  195. resplen = 0;
  196. break;
  197. default:
  198. // Set Sense = Invalid Command Operation
  199. tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
  200. // negative means error -> tinyusb could stall and/or response with failed status
  201. resplen = -1;
  202. break;
  203. }
  204. // return resplen must not larger than bufsize
  205. if ( resplen > bufsize ) resplen = bufsize;
  206. if ( response && (resplen > 0) )
  207. {
  208. if(in_xfer)
  209. {
  210. memcpy(buffer, response, resplen);
  211. }else
  212. {
  213. // SCSI output
  214. }
  215. }
  216. return resplen;
  217. }
  218. #endif