msc_disk.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. // Some MCU doesn't have enough 8KB SRAM to store the whole disk
  29. // We will use Flash as read-only disk with board that has
  30. // CFG_EXAMPLE_MSC_READONLY defined
  31. #define README_CONTENTS \
  32. "This is tinyusb's MassStorage Class demo.\r\n\r\n\
  33. If you find any bugs or get any questions, feel free to file an\r\n\
  34. issue at github.com/hathach/tinyusb"
  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, 'T' , 'i' , 'n' , 'y' , 'U' ,
  57. 'S' , 'B' , ' ' , 'M' , 'S' , 'C' , 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. 'T' , 'i' , 'n' , 'y' , 'U' , 'S' , 'B' , ' ' , 'M' , 'S' , 'C' , 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[] = "TinyUSB";
  111. const char pid[] = "Mass Storage";
  112. const char rev[] = "1.0";
  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. return true; // RAM disk is always ready
  123. }
  124. // Invoked when received SCSI_CMD_READ_CAPACITY_10 and SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size
  125. // Application update block count and block size
  126. void tud_msc_capacity_cb(uint8_t lun, uint32_t* block_count, uint16_t* block_size)
  127. {
  128. (void) lun;
  129. *block_count = DISK_BLOCK_NUM;
  130. *block_size = DISK_BLOCK_SIZE;
  131. }
  132. // Invoked when received Start Stop Unit command
  133. // - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
  134. // - Start = 1 : active mode, if load_eject = 1 : load disk storage
  135. bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject)
  136. {
  137. (void) lun;
  138. (void) power_condition;
  139. if ( load_eject )
  140. {
  141. if (start)
  142. {
  143. // load disk storage
  144. }else
  145. {
  146. // unload disk storage
  147. }
  148. }
  149. return true;
  150. }
  151. // Callback invoked when received READ10 command.
  152. // Copy disk's data to buffer (up to bufsize) and return number of copied bytes.
  153. int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void* buffer, uint32_t bufsize)
  154. {
  155. (void) lun;
  156. // out of ramdisk
  157. if ( lba >= DISK_BLOCK_NUM ) return -1;
  158. uint8_t const* addr = msc_disk[lba] + offset;
  159. memcpy(buffer, addr, bufsize);
  160. return bufsize;
  161. }
  162. // Callback invoked when received WRITE10 command.
  163. // Process data in buffer to disk's storage and return number of written bytes
  164. int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t* buffer, uint32_t bufsize)
  165. {
  166. (void) lun;
  167. // out of ramdisk
  168. if ( lba >= DISK_BLOCK_NUM ) return -1;
  169. #ifndef CFG_EXAMPLE_MSC_READONLY
  170. uint8_t* addr = msc_disk[lba] + offset;
  171. memcpy(addr, buffer, bufsize);
  172. #else
  173. (void) lba; (void) offset; (void) buffer;
  174. #endif
  175. return bufsize;
  176. }
  177. // Callback invoked when received an SCSI command not in built-in list below
  178. // - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
  179. // - READ10 and WRITE10 has their own callbacks
  180. int32_t tud_msc_scsi_cb (uint8_t lun, uint8_t const scsi_cmd[16], void* buffer, uint16_t bufsize)
  181. {
  182. // read10 & write10 has their own callback and MUST not be handled here
  183. void const* response = NULL;
  184. int32_t resplen = 0;
  185. // most scsi handled is input
  186. bool in_xfer = true;
  187. switch (scsi_cmd[0])
  188. {
  189. case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
  190. // Host is about to read/write etc ... better not to disconnect disk
  191. resplen = 0;
  192. break;
  193. default:
  194. // Set Sense = Invalid Command Operation
  195. tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
  196. // negative means error -> tinyusb could stall and/or response with failed status
  197. resplen = -1;
  198. break;
  199. }
  200. // return resplen must not larger than bufsize
  201. if ( resplen > bufsize ) resplen = bufsize;
  202. if ( response && (resplen > 0) )
  203. {
  204. if(in_xfer)
  205. {
  206. memcpy(buffer, response, resplen);
  207. }else
  208. {
  209. // SCSI output
  210. }
  211. }
  212. return resplen;
  213. }
  214. #endif