BlueSCSI_Toolbox.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /**
  2. * Copyright (C) 2023 Eric Helgeson
  3. *
  4. * This file is part of BlueSCSI
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. **/
  19. #include "BlueSCSI_Toolbox.h"
  20. #include "BlueSCSI_disk.h"
  21. #include "BlueSCSI_cdrom.h"
  22. #include "BlueSCSI_log.h"
  23. #include <minIni.h>
  24. #include <SdFat.h>
  25. extern "C" {
  26. #include <scsi2sd_time.h>
  27. #include <sd.h>
  28. #include <mode.h>
  29. }
  30. static void doCountFiles(const char * dir_name)
  31. {
  32. File dir;
  33. File file;
  34. char name[ MAX_MAC_PATH + 1];
  35. dir.open(dir_name);
  36. dir.rewindDirectory();
  37. uint8_t file_count = 0;
  38. while (file.openNext(&dir, O_RDONLY))
  39. {
  40. if(file.getError() > 0)
  41. {
  42. file.close();
  43. break;
  44. }
  45. file.getName(name, MAX_MAC_PATH + 1);
  46. file.close();
  47. // only count valid files.
  48. if(scsiDiskFilenameValid(name))
  49. {
  50. file_count = file_count + 1;
  51. if(file_count > 100) {
  52. scsiDev.status = CHECK_CONDITION;
  53. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  54. scsiDev.target->sense.asc = OPEN_RETRO_SCSI_TOO_MANY_FILES;
  55. scsiDev.phase = STATUS;
  56. dir.close();
  57. return;
  58. }
  59. }
  60. }
  61. scsiDev.data[0] = file_count;
  62. scsiDev.dataLen = sizeof(file_count);
  63. scsiDev.phase = DATA_IN;
  64. }
  65. void onListFiles(const char * dir_name) {
  66. File dir;
  67. File file;
  68. memset(scsiDev.data, 0, 4096);
  69. int ENTRY_SIZE = 40;
  70. char name[MAX_MAC_PATH + 1];
  71. dir.open(dir_name);
  72. dir.rewindDirectory();
  73. uint8_t index = 0;
  74. byte file_entry[40] = {0};
  75. while (file.openNext(&dir, O_RDONLY))
  76. {
  77. uint8_t isDir = file.isDirectory() ? 0x00 : 0x01;
  78. file.getName(name, MAX_MAC_PATH + 1);
  79. uint64_t size = file.fileSize();
  80. file.close();
  81. if(!scsiDiskFilenameValid(name))
  82. continue;
  83. file_entry[0] = index;
  84. file_entry[1] = isDir;
  85. int c = 0; // Index of char in name[]
  86. for(int i = 2; i < (MAX_MAC_PATH + 1 + 2); i++) { // bytes 2 - 34
  87. file_entry[i] = name[c++];
  88. }
  89. file_entry[35] = 0; //(size >> 32) & 0xff;
  90. file_entry[36] = (size >> 24) & 0xff;
  91. file_entry[37] = (size >> 16) & 0xff;
  92. file_entry[38] = (size >> 8) & 0xff;
  93. file_entry[39] = (size) & 0xff;
  94. memcpy(&(scsiDev.data[ENTRY_SIZE * index]), file_entry, ENTRY_SIZE);
  95. index = index + 1;
  96. }
  97. dir.close();
  98. scsiDev.dataLen = 4096;
  99. scsiDev.phase = DATA_IN;
  100. }
  101. File get_file_from_index(uint8_t index, const char * dir_name)
  102. {
  103. File dir;
  104. FsFile file_test;
  105. char name[MAX_MAC_PATH + 1];
  106. dir.open(dir_name);
  107. dir.rewindDirectory(); // Back to the top
  108. int count = 0;
  109. while (file_test.openNext(&dir, O_RDONLY))
  110. {
  111. // If error there is no next file to open.
  112. if(file_test.getError() > 0) {
  113. file_test.close();
  114. break;
  115. }
  116. file_test.getName(name, MAX_MAC_PATH + 1);
  117. if(!scsiDiskFilenameValid(name))
  118. {
  119. file_test.close();
  120. continue;
  121. }
  122. if (count == index)
  123. {
  124. dir.close();
  125. return file_test;
  126. }
  127. else
  128. {
  129. file_test.close();
  130. }
  131. count++;
  132. }
  133. file_test.close();
  134. dir.close();
  135. return file_test;
  136. }
  137. // Devices that are active on this SCSI device.
  138. void onListDevices()
  139. {
  140. for (int i = 0; i < NUM_SCSIID; i++)
  141. {
  142. const S2S_TargetCfg* cfg = s2s_getConfigById(i);
  143. if (cfg && (cfg->scsiId & S2S_CFG_TARGET_ENABLED))
  144. {
  145. scsiDev.data[i] = (int)cfg->deviceType; // 2 == cd
  146. }
  147. else
  148. {
  149. scsiDev.data[i] = 0xFF; // not enabled target.
  150. }
  151. }
  152. scsiDev.dataLen = NUM_SCSIID;
  153. }
  154. void onSetNextCD()
  155. {
  156. char name[MAX_FILE_PATH];
  157. char full_path[MAX_FILE_PATH * 2];
  158. uint8_t file_index = scsiDev.cdb[1];
  159. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  160. File next_cd = get_file_from_index(file_index, CD_IMG_DIR);
  161. next_cd.getName(name, sizeof(name));
  162. next_cd.close();
  163. snprintf(full_path, (MAX_FILE_PATH * 2), "%s/%s", CD_IMG_DIR, name);
  164. cdromSwitch(img, full_path);
  165. }
  166. File gFile; // global so we can keep it open while transfering.
  167. void onGetFile10(void) {
  168. uint8_t index = scsiDev.cdb[1];
  169. uint32_t offset = ((uint32_t)scsiDev.cdb[2] << 24) | ((uint32_t)scsiDev.cdb[3] << 16) | ((uint32_t)scsiDev.cdb[4] << 8) | scsiDev.cdb[5];
  170. if (offset == 0) // first time, open the file.
  171. {
  172. gFile = get_file_from_index(index, "/shared");
  173. if(!gFile.isDirectory() && !gFile.isReadable())
  174. {
  175. scsiDev.status = CHECK_CONDITION;
  176. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  177. //SCSI_ASC_INVALID_FIELD_IN_CDB
  178. scsiDev.phase = STATUS;
  179. return;
  180. }
  181. }
  182. uint32_t file_total = gFile.size();
  183. memset(scsiDev.data, 0, 4096);
  184. gFile.seekSet(offset * 4096);
  185. int bytes_read = gFile.read(scsiDev.data, 4096);
  186. if(offset * 4096 >= file_total) // transfer done, close.
  187. {
  188. gFile.close();
  189. }
  190. scsiDev.dataLen = bytes_read;
  191. scsiDev.phase = DATA_IN;
  192. }
  193. /*
  194. Prepares a file for receving. The file name is null terminated in the scsi data.
  195. */
  196. File receveFile;
  197. void onSendFilePrep(void)
  198. {
  199. char file_name[32+1];
  200. memset(file_name, '\0', 32+1);
  201. scsiEnterPhase(DATA_OUT);
  202. for (int i = 0; i < 32+1; ++i)
  203. {
  204. file_name[i] = scsiReadByte();
  205. }
  206. SD.chdir("/shared");
  207. receveFile.open(file_name, FILE_WRITE);
  208. SD.chdir("/");
  209. if(receveFile.isOpen() && receveFile.isWritable())
  210. {
  211. receveFile.rewind();
  212. receveFile.sync();
  213. // do i need to manually set phase to status here?
  214. return;
  215. } else {
  216. receveFile.close();
  217. scsiDev.status = CHECK_CONDITION;
  218. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  219. //SCSI_ASC_INVALID_FIELD_IN_CDB
  220. scsiDev.phase = STATUS;
  221. }
  222. }
  223. void onSendFileEnd(void)
  224. {
  225. receveFile.sync();
  226. receveFile.close();
  227. scsiDev.phase = STATUS;
  228. }
  229. void onSendFile10(void)
  230. {
  231. if(!receveFile.isOpen() || !receveFile.isWritable())
  232. {
  233. scsiDev.status = CHECK_CONDITION;
  234. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  235. //SCSI_ASC_INVALID_FIELD_IN_CDB
  236. scsiDev.phase = STATUS;
  237. }
  238. // Number of bytes sent this request, 1..512.
  239. uint16_t bytes_sent = ((uint16_t)scsiDev.cdb[1] << 8) | scsiDev.cdb[2];
  240. // 512 byte offset of where to put these bytes.
  241. uint32_t offset = ((uint32_t)scsiDev.cdb[3] << 16) | ((uint32_t)scsiDev.cdb[4] << 8) | scsiDev.cdb[5];
  242. uint16_t buf_size = 512;
  243. uint8_t buf[512];
  244. // Check if last block of file, and not the only bock in file.
  245. if(bytes_sent < buf_size)
  246. {
  247. buf_size = bytes_sent;
  248. }
  249. scsiEnterPhase(DATA_OUT);
  250. scsiRead(buf, bytes_sent, NULL);
  251. receveFile.seekCur(offset * 512);
  252. receveFile.write(buf, buf_size);
  253. if(receveFile.getWriteError())
  254. {
  255. receveFile.clearWriteError();
  256. receveFile.close();
  257. scsiDev.status = CHECK_CONDITION;
  258. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  259. }
  260. //scsiDev.phase = STATUS;
  261. }
  262. void onToggleDebug()
  263. {
  264. if(g_log_debug)
  265. {
  266. debuglog("Turning Debug logs off.");
  267. }
  268. else
  269. {
  270. log("Turning Debug logs on.");
  271. }
  272. g_log_debug = !g_log_debug;
  273. scsiDev.phase = STATUS;
  274. }
  275. extern "C" int scsiBlueSCSIToolboxCommand()
  276. {
  277. int commandHandled = 1;
  278. uint8_t command = scsiDev.cdb[0];
  279. if (unlikely(command == BLUESCSI_TOOLBOX_COUNT_FILES))
  280. {
  281. doCountFiles("/shared");
  282. }
  283. else if (unlikely(command == BLUESCSI_TOOLBOX_LIST_FILES))
  284. {
  285. // TODO: Allow user to set dir name via ini
  286. onListFiles("/shared");
  287. }
  288. else if (unlikely(command == BLUESCSI_TOOLBOX_GET_FILE))
  289. {
  290. onGetFile10();
  291. }
  292. else if (unlikely(command == BLUESCSI_TOOLBOX_SEND_FILE_PREP))
  293. {
  294. onSendFilePrep();
  295. }
  296. else if (unlikely(command == BLUESCSI_TOOLBOX_SEND_FILE_10))
  297. {
  298. onSendFile10();
  299. }
  300. else if (unlikely(command == BLUESCSI_TOOLBOX_SEND_FILE_END))
  301. {
  302. onSendFileEnd();
  303. }
  304. else if(unlikely(command == BLUESCSI_TOOLBOX_TOGGLE_DEBUG))
  305. {
  306. onToggleDebug();
  307. }
  308. else if(unlikely(command == BLUESCSI_TOOLBOX_LIST_CDS))
  309. {
  310. onListFiles("/CDImages");
  311. }
  312. else if(unlikely(command == BLUESCSI_TOOLBOX_SET_NEXT_CD))
  313. {
  314. onSetNextCD();
  315. }
  316. else if(unlikely(command == BLUESCSI_TOOLBOX_LIST_DEVICES))
  317. {
  318. onListDevices();
  319. }
  320. else if (unlikely(command == BLUESCSI_TOOLBOX_COUNT_CDS))
  321. {
  322. // TODO: Allow user to set dir name via ini
  323. doCountFiles("/CDImages");
  324. }
  325. else
  326. {
  327. commandHandled = 0;
  328. }
  329. return commandHandled;
  330. }