config.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. // Copyright (C) 2014 Michael McMaster <michael@codesrc.com>
  2. //
  3. // This file is part of SCSI2SD.
  4. //
  5. // SCSI2SD is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // SCSI2SD is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with SCSI2SD. If not, see <http://www.gnu.org/licenses/>.
  17. #include "config.h"
  18. #include "led.h"
  19. #include "scsi.h"
  20. #include "scsiPhy.h"
  21. #include "sd.h"
  22. #include "disk.h"
  23. #include "bootloader.h"
  24. #include "bsp.h"
  25. #include "spinlock.h"
  26. #include "../../include/scsi2sd.h"
  27. #include "../../include/hidpacket.h"
  28. #include "usb_device/usb_device.h"
  29. #include "usb_device/usbd_hid.h"
  30. #include "usb_device/usbd_composite.h"
  31. #include "bsp_driver_sd.h"
  32. #include <string.h>
  33. static const uint16_t FIRMWARE_VERSION = 0x0632;
  34. // Optional static config
  35. extern uint8_t* __fixed_config;
  36. // 1 flash row
  37. static const uint8_t DEFAULT_CONFIG[128] =
  38. {
  39. 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x3F, 0x00,
  40. 0x00, 0x02, 0x3F, 0x00, 0xFF, 0x00, 0x20, 0x63, 0x6F, 0x64, 0x65, 0x73,
  41. 0x72, 0x63, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x53,
  42. 0x43, 0x53, 0x49, 0x32, 0x53, 0x44, 0x20, 0x31, 0x2E, 0x30, 0x31, 0x32,
  43. 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
  44. 0x37, 0x38, 0x00, 0x00
  45. };
  46. static uint8_t s2s_cfg[S2S_CFG_SIZE] S2S_DMA_ALIGN;
  47. static uint8_t configDmaBuf[512] S2S_DMA_ALIGN; // For SD card writes.
  48. enum USB_STATE
  49. {
  50. USB_IDLE,
  51. USB_DATA_SENT
  52. };
  53. static int usbInEpState;
  54. static void s2s_debugTimer();
  55. // Debug timer to log via USB.
  56. // Timer 6 & 7 is a simple counter with no external IO supported.
  57. static s2s_lock_t usbDevLock = s2s_lock_init;
  58. TIM_HandleTypeDef htim7;
  59. static int debugTimerStarted = 0;
  60. void TIM7_IRQHandler()
  61. {
  62. HAL_TIM_IRQHandler(&htim7);
  63. }
  64. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  65. {
  66. if (s2s_spin_trylock(&usbDevLock)) {
  67. s2s_debugTimer();
  68. s2s_spin_unlock(&usbDevLock);
  69. }
  70. }
  71. void s2s_configInit(S2S_BoardCfg* config)
  72. {
  73. usbInEpState = USB_IDLE;
  74. if (memcmp(__fixed_config, "BCFG", 4) == 0)
  75. {
  76. // Use hardcoded config
  77. memcpy(s2s_cfg, __fixed_config, S2S_CFG_SIZE);
  78. memcpy(config, s2s_cfg, sizeof(S2S_BoardCfg));
  79. }
  80. else if ((blockDev.state & DISK_PRESENT) && sdDev.capacity)
  81. {
  82. int cfgSectors = (S2S_CFG_SIZE + 511) / 512;
  83. BSP_SD_ReadBlocks_DMA(
  84. (uint32_t*) &s2s_cfg[0],
  85. (sdDev.capacity - cfgSectors) * 512ll,
  86. 512,
  87. cfgSectors);
  88. memcpy(config, s2s_cfg, sizeof(S2S_BoardCfg));
  89. if (memcmp(config->magic, "BCFG", 4))
  90. {
  91. // Invalid SD card config, use default.
  92. memset(&s2s_cfg[0], 0, S2S_CFG_SIZE);
  93. memcpy(config, s2s_cfg, sizeof(S2S_BoardCfg));
  94. memcpy(config->magic, "BCFG", 4);
  95. config->selectionDelay = 255; // auto
  96. config->flags6 = S2S_CFG_ENABLE_TERMINATOR;
  97. memcpy(
  98. &s2s_cfg[0] + sizeof(S2S_BoardCfg),
  99. DEFAULT_CONFIG,
  100. sizeof(S2S_TargetCfg));
  101. }
  102. }
  103. else
  104. {
  105. // No SD card, use existing config if valid
  106. if (memcmp(config->magic, "BCFG", 4))
  107. {
  108. // Not valid, use empty config with no disks.
  109. memset(&s2s_cfg[0], 0, S2S_CFG_SIZE);
  110. memcpy(config, s2s_cfg, sizeof(S2S_BoardCfg));
  111. config->selectionDelay = 255; // auto
  112. config->flags6 = S2S_CFG_ENABLE_TERMINATOR;
  113. }
  114. }
  115. }
  116. static void debugInit(void)
  117. {
  118. if (debugTimerStarted == 1) return;
  119. debugTimerStarted = 1;
  120. // 10ms debug timer to capture logs over USB
  121. __TIM7_CLK_ENABLE();
  122. htim7.Instance = TIM7;
  123. htim7.Init.Prescaler = 10800 - 1; // 16bit. 108MHz down to 10KHz
  124. htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
  125. htim7.Init.Period = 100 - 1; // 16bit. 10KHz down to 10ms.
  126. htim7.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  127. HAL_TIM_Base_Init(&htim7);
  128. HAL_TIM_Base_Start_IT(&htim7);
  129. HAL_NVIC_SetPriority(TIM7_IRQn, 10, 0);
  130. HAL_NVIC_EnableIRQ(TIM7_IRQn);
  131. }
  132. static void
  133. pingCommand()
  134. {
  135. uint8_t response[] =
  136. {
  137. S2S_CFG_STATUS_GOOD
  138. };
  139. hidPacket_send(response, sizeof(response));
  140. }
  141. static void
  142. sdInfoCommand()
  143. {
  144. uint8_t response[sizeof(sdDev.csd) + sizeof(sdDev.cid)];
  145. memcpy(response, sdDev.csd, sizeof(sdDev.csd));
  146. memcpy(response + sizeof(sdDev.csd), sdDev.cid, sizeof(sdDev.cid));
  147. hidPacket_send(response, sizeof(response));
  148. }
  149. static void
  150. scsiTestCommand()
  151. {
  152. int resultCode = scsiSelfTest();
  153. uint8_t response[] =
  154. {
  155. resultCode == 0 ? S2S_CFG_STATUS_GOOD : S2S_CFG_STATUS_ERR,
  156. resultCode
  157. };
  158. hidPacket_send(response, sizeof(response));
  159. }
  160. static void
  161. scsiDevInfoCommand()
  162. {
  163. uint8_t response[] =
  164. {
  165. FIRMWARE_VERSION >> 8,
  166. FIRMWARE_VERSION & 0xff,
  167. sdDev.capacity >> 24,
  168. sdDev.capacity >> 16,
  169. sdDev.capacity >> 8,
  170. sdDev.capacity,
  171. 1 // useSdConfig, always true for V6.
  172. };
  173. hidPacket_send(response, sizeof(response));
  174. }
  175. static void
  176. debugCommand()
  177. {
  178. uint8_t response[32];
  179. memcpy(&response, &scsiDev.cdb, 12);
  180. response[12] = scsiDev.msgIn;
  181. response[13] = scsiDev.msgOut;
  182. response[14] = scsiDev.lastStatus;
  183. response[15] = scsiDev.lastSense;
  184. response[16] = scsiDev.phase;
  185. response[17] = *SCSI_STS_SCSI;
  186. response[18] = scsiDev.target != NULL ? scsiDev.target->syncOffset : 0;
  187. response[19] = scsiDev.target != NULL ? scsiDev.target->syncPeriod : 0;
  188. response[20] = scsiDev.minSyncPeriod;
  189. response[21] = scsiDev.rstCount;
  190. response[22] = scsiDev.selCount;
  191. response[23] = scsiDev.msgCount;
  192. response[24] = scsiDev.cmdCount;
  193. response[25] = scsiDev.watchdogTick;
  194. response[26] = blockDev.state;
  195. response[27] = scsiDev.lastSenseASC >> 8;
  196. response[28] = scsiDev.lastSenseASC;
  197. response[29] = *SCSI_STS_DBX & 0xff; // What we've read
  198. response[30] = *SCSI_STS_SELECTED;
  199. response[31] = *SCSI_STS_DBX >> 8; // What we're writing
  200. hidPacket_send(response, sizeof(response));
  201. }
  202. static void
  203. sdWriteCommand(const uint8_t* cmd, size_t cmdSize)
  204. {
  205. if (cmdSize < 517)
  206. {
  207. return; // ignore.
  208. }
  209. uint32_t lba =
  210. (((uint32_t)cmd[1]) << 24) |
  211. (((uint32_t)cmd[2]) << 16) |
  212. (((uint32_t)cmd[3]) << 8) |
  213. ((uint32_t)cmd[4]);
  214. memcpy(configDmaBuf, &cmd[5], 512);
  215. BSP_SD_WriteBlocks_DMA((uint32_t*) configDmaBuf, lba * 512ll, 512, 1);
  216. uint8_t response[] =
  217. {
  218. S2S_CFG_STATUS_GOOD
  219. };
  220. hidPacket_send(response, sizeof(response));
  221. }
  222. static void
  223. sdReadCommand(const uint8_t* cmd, size_t cmdSize)
  224. {
  225. if (cmdSize < 5)
  226. {
  227. return; // ignore.
  228. }
  229. uint32_t lba =
  230. (((uint32_t)cmd[1]) << 24) |
  231. (((uint32_t)cmd[2]) << 16) |
  232. (((uint32_t)cmd[3]) << 8) |
  233. ((uint32_t)cmd[4]);
  234. BSP_SD_ReadBlocks_DMA((uint32_t*) configDmaBuf, lba * 512ll, 512, 1);
  235. hidPacket_send(configDmaBuf, 512);
  236. }
  237. static void
  238. processCommand(const uint8_t* cmd, size_t cmdSize)
  239. {
  240. switch (cmd[0])
  241. {
  242. case S2S_CMD_PING:
  243. pingCommand();
  244. break;
  245. case S2S_CMD_REBOOT:
  246. s2s_enterBootloader();
  247. break;
  248. case S2S_CMD_SDINFO:
  249. sdInfoCommand();
  250. break;
  251. case S2S_CMD_SCSITEST:
  252. scsiTestCommand();
  253. break;
  254. case S2S_CMD_DEVINFO:
  255. scsiDevInfoCommand();
  256. break;
  257. case S2S_CMD_SD_WRITE:
  258. sdWriteCommand(cmd, cmdSize);
  259. break;
  260. case S2S_CMD_SD_READ:
  261. sdReadCommand(cmd, cmdSize);
  262. break;
  263. case S2S_CMD_DEBUG:
  264. if (debugTimerStarted == 0) {
  265. debugInit();
  266. }
  267. debugCommand();
  268. break;
  269. case S2S_CMD_NONE: // invalid
  270. default:
  271. break;
  272. }
  273. }
  274. void s2s_configPoll()
  275. {
  276. s2s_spin_lock(&usbDevLock);
  277. if (!USBD_Composite_IsConfigured(&hUsbDeviceFS))
  278. {
  279. usbInEpState = USB_IDLE;
  280. goto out;
  281. }
  282. if (USBD_HID_IsReportReady(&hUsbDeviceFS))
  283. {
  284. s2s_ledOn();
  285. // The host sent us some data!
  286. uint8_t hidBuffer[USBHID_LEN];
  287. int byteCount = USBD_HID_GetReport(&hUsbDeviceFS, hidBuffer, sizeof(hidBuffer));
  288. hidPacket_recv(hidBuffer, byteCount);
  289. size_t cmdSize;
  290. const uint8_t* cmd = hidPacket_getPacket(&cmdSize);
  291. if (cmd && (cmdSize > 0))
  292. {
  293. processCommand(cmd, cmdSize);
  294. }
  295. s2s_ledOff();
  296. }
  297. switch (usbInEpState)
  298. {
  299. case USB_IDLE:
  300. {
  301. uint8_t hidBuffer[USBHID_LEN];
  302. const uint8_t* nextChunk = hidPacket_getHIDBytes(hidBuffer);
  303. if (nextChunk)
  304. {
  305. USBD_HID_SendReport (&hUsbDeviceFS, nextChunk, sizeof(hidBuffer));
  306. usbInEpState = USB_DATA_SENT;
  307. }
  308. }
  309. break;
  310. case USB_DATA_SENT:
  311. if (!USBD_HID_IsBusy(&hUsbDeviceFS))
  312. {
  313. // Data accepted.
  314. usbInEpState = USB_IDLE;
  315. }
  316. break;
  317. }
  318. out:
  319. s2s_spin_unlock(&usbDevLock);
  320. }
  321. void s2s_debugTimer()
  322. {
  323. if (!USBD_Composite_IsConfigured(&hUsbDeviceFS))
  324. {
  325. usbInEpState = USB_IDLE;
  326. return;
  327. }
  328. if (USBD_HID_IsReportReady(&hUsbDeviceFS))
  329. {
  330. uint8_t hidBuffer[USBHID_LEN];
  331. int byteCount = USBD_HID_GetReport(&hUsbDeviceFS, hidBuffer, sizeof(hidBuffer));
  332. hidPacket_recv(hidBuffer, byteCount);
  333. size_t cmdSize;
  334. const uint8_t* cmd = hidPacket_peekPacket(&cmdSize);
  335. // This is called from an ISR, only process simple commands.
  336. if (cmd && (cmdSize > 0))
  337. {
  338. if (cmd[0] == S2S_CMD_DEBUG)
  339. {
  340. hidPacket_getPacket(&cmdSize);
  341. debugCommand();
  342. }
  343. else if (cmd[0] == S2S_CMD_PING)
  344. {
  345. hidPacket_getPacket(&cmdSize);
  346. pingCommand();
  347. }
  348. }
  349. }
  350. switch (usbInEpState)
  351. {
  352. case USB_IDLE:
  353. {
  354. uint8_t hidBuffer[USBHID_LEN];
  355. const uint8_t* nextChunk = hidPacket_getHIDBytes(hidBuffer);
  356. if (nextChunk)
  357. {
  358. USBD_HID_SendReport (&hUsbDeviceFS, nextChunk, sizeof(hidBuffer));
  359. usbInEpState = USB_DATA_SENT;
  360. }
  361. }
  362. break;
  363. case USB_DATA_SENT:
  364. if (!USBD_HID_IsBusy(&hUsbDeviceFS))
  365. {
  366. // Data accepted.
  367. usbInEpState = USB_IDLE;
  368. }
  369. break;
  370. }
  371. }
  372. // Public method for storing MODE SELECT results.
  373. void s2s_configSave(int scsiId, uint16_t bytesPerSector)
  374. {
  375. S2S_TargetCfg* cfg = (S2S_TargetCfg*) s2s_getConfigById(scsiId);
  376. cfg->bytesPerSector = bytesPerSector;
  377. BSP_SD_WriteBlocks_DMA(
  378. (uint32_t*) &s2s_cfg[0],
  379. (sdDev.capacity - S2S_CFG_SIZE) * 512ll,
  380. 512,
  381. (S2S_CFG_SIZE + 511) / 512);
  382. }
  383. const S2S_TargetCfg* s2s_getConfigByIndex(int i)
  384. {
  385. return (const S2S_TargetCfg*)
  386. (s2s_cfg + sizeof(S2S_BoardCfg) + (i * sizeof(S2S_TargetCfg)));
  387. }
  388. const S2S_TargetCfg* s2s_getConfigById(int scsiId)
  389. {
  390. int i;
  391. for (i = 0; i < S2S_MAX_TARGETS; ++i)
  392. {
  393. const S2S_TargetCfg* tgt = s2s_getConfigByIndex(i);
  394. if ((tgt->scsiId & S2S_CFG_TARGET_ID_BITS) == scsiId)
  395. {
  396. return tgt;
  397. }
  398. }
  399. return NULL;
  400. }