BlueSCSI_Toolbox.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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, 32 + 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. continue;
  119. if (count == index)
  120. {
  121. dir.close();
  122. return file_test;
  123. }
  124. else
  125. {
  126. file_test.close();
  127. }
  128. count++;
  129. }
  130. file_test.close();
  131. dir.close();
  132. return file_test;
  133. }
  134. // Devices that are active on this SCSI device.
  135. void onListDevices()
  136. {
  137. for (int i = 0; i < NUM_SCSIID; i++)
  138. {
  139. const S2S_TargetCfg* cfg = s2s_getConfigById(i);
  140. // id, type
  141. if (cfg && (cfg->scsiId & S2S_CFG_TARGET_ENABLED))
  142. {
  143. scsiDev.data[i] = (int)cfg->deviceType; // 2 == cd
  144. }
  145. else
  146. {
  147. scsiDev.data[i] = 255; // not enabled target.
  148. }
  149. }
  150. scsiDev.dataLen = 16;
  151. }
  152. void onSetNextCD()
  153. {
  154. char name[MAX_FILE_PATH];
  155. char full_path[MAX_FILE_PATH * 2];
  156. uint8_t file_index = scsiDev.cdb[1];
  157. uint8_t cd_scsi_id = scsiDev.cdb[2];
  158. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  159. File next_cd = get_file_from_index(file_index, CD_IMG_DIR);
  160. next_cd.getName(name, sizeof(name));
  161. next_cd.close();
  162. snprintf(full_path, (MAX_FILE_PATH * 2), "%s/%s", CD_IMG_DIR, name);
  163. cdromSwitch(img, full_path);
  164. }
  165. File gFile; // global so we can keep it open while transfering.
  166. void onGetFile10(void) {
  167. uint8_t index = scsiDev.cdb[1];
  168. uint32_t offset = ((uint32_t)scsiDev.cdb[2] << 24) | ((uint32_t)scsiDev.cdb[3] << 16) | ((uint32_t)scsiDev.cdb[4] << 8) | scsiDev.cdb[5];
  169. if (offset == 0) // first time, open the file.
  170. {
  171. gFile = get_file_from_index(index, "/shared");
  172. if(!gFile.isDirectory() && !gFile.isReadable())
  173. {
  174. scsiDev.status = CHECK_CONDITION;
  175. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  176. //SCSI_ASC_INVALID_FIELD_IN_CDB
  177. scsiDev.phase = STATUS;
  178. return;
  179. }
  180. }
  181. uint32_t file_total = gFile.size();
  182. memset(scsiDev.data, 0, 4096);
  183. gFile.seekSet(offset * 4096);
  184. int bytes_read = gFile.read(scsiDev.data, 4096);
  185. if(offset * 4096 >= file_total) // transfer done, close.
  186. {
  187. gFile.close();
  188. }
  189. scsiDev.dataLen = bytes_read;
  190. scsiDev.phase = DATA_IN;
  191. }
  192. /*
  193. Prepares a file for receving. The file name is null terminated in the scsi data.
  194. */
  195. File receveFile;
  196. void onSendFilePrep(void)
  197. {
  198. char file_name[32+1];
  199. memset(file_name, '\0', 32+1);
  200. scsiEnterPhase(DATA_OUT);
  201. for (int i = 0; i < 32+1; ++i)
  202. {
  203. file_name[i] = scsiReadByte();
  204. }
  205. SD.chdir("/shared");
  206. receveFile.open(file_name, FILE_WRITE);
  207. SD.chdir("/");
  208. if(receveFile.isOpen() && receveFile.isWritable())
  209. {
  210. receveFile.rewind();
  211. receveFile.sync();
  212. // do i need to manually set phase to status here?
  213. return;
  214. } else {
  215. receveFile.close();
  216. scsiDev.status = CHECK_CONDITION;
  217. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  218. //SCSI_ASC_INVALID_FIELD_IN_CDB
  219. scsiDev.phase = STATUS;
  220. }
  221. }
  222. void onSendFileEnd(void)
  223. {
  224. receveFile.sync();
  225. receveFile.close();
  226. scsiDev.phase = STATUS;
  227. }
  228. void onSendFile10(void)
  229. {
  230. if(!receveFile.isOpen() || !receveFile.isWritable())
  231. {
  232. scsiDev.status = CHECK_CONDITION;
  233. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  234. //SCSI_ASC_INVALID_FIELD_IN_CDB
  235. scsiDev.phase = STATUS;
  236. }
  237. // Number of bytes sent this request, 1..512.
  238. uint16_t bytes_sent = ((uint16_t)scsiDev.cdb[1] << 8) | scsiDev.cdb[2];
  239. // 512 byte offset of where to put these bytes.
  240. uint32_t offset = ((uint32_t)scsiDev.cdb[3] << 16) | ((uint32_t)scsiDev.cdb[4] << 8) | scsiDev.cdb[5];
  241. uint16_t buf_size = 512;
  242. uint8_t buf[512];
  243. // Check if last block of file, and not the only bock in file.
  244. if(bytes_sent < buf_size)
  245. {
  246. buf_size = bytes_sent;
  247. }
  248. scsiEnterPhase(DATA_OUT);
  249. scsiRead(buf, bytes_sent, NULL);
  250. receveFile.seekCur(offset * 512);
  251. receveFile.write(buf, buf_size);
  252. if(receveFile.getWriteError())
  253. {
  254. receveFile.clearWriteError();
  255. receveFile.close();
  256. scsiDev.status = CHECK_CONDITION;
  257. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  258. }
  259. //scsiDev.phase = STATUS;
  260. }
  261. void onToggleDebug()
  262. {
  263. if(g_log_debug)
  264. {
  265. debuglog("Turning Debug logs off.");
  266. }
  267. else
  268. {
  269. log("Turning Debug logs on.");
  270. }
  271. g_log_debug = !g_log_debug;
  272. scsiDev.phase = STATUS;
  273. }
  274. extern "C" int scsiBlueSCSIToolboxCommand()
  275. {
  276. int commandHandled = 1;
  277. uint8_t command = scsiDev.cdb[0];
  278. if (unlikely(command == BLUESCSI_TOOLBOX_COUNT_FILES))
  279. {
  280. doCountFiles("/shared");
  281. }
  282. else if (unlikely(command == BLUESCSI_TOOLBOX_LIST_FILES))
  283. {
  284. // TODO: Allow user to set dir name via ini
  285. onListFiles("/shared");
  286. }
  287. else if (unlikely(command == BLUESCSI_TOOLBOX_GET_FILE))
  288. {
  289. onGetFile10();
  290. }
  291. else if (unlikely(command == BLUESCSI_TOOLBOX_SEND_FILE_PREP))
  292. {
  293. onSendFilePrep();
  294. }
  295. else if (unlikely(command == BLUESCSI_TOOLBOX_SEND_FILE_10))
  296. {
  297. onSendFile10();
  298. }
  299. else if (unlikely(command == BLUESCSI_TOOLBOX_SEND_FILE_END))
  300. {
  301. onSendFileEnd();
  302. }
  303. else if(unlikely(command == BLUESCSI_TOOLBOX_TOGGLE_DEBUG))
  304. {
  305. onToggleDebug();
  306. }
  307. else if(unlikely(command == BLUESCSI_TOOLBOX_LIST_CDS))
  308. {
  309. onListFiles("/CDImages");
  310. }
  311. else if(unlikely(command == BLUESCSI_TOOLBOX_SET_NEXT_CD))
  312. {
  313. onSetNextCD();
  314. }
  315. else if(unlikely(command == BLUESCSI_TOOLBOX_LIST_DEVICES))
  316. {
  317. onListDevices();
  318. }
  319. else if (unlikely(command == BLUESCSI_TOOLBOX_COUNT_CDS))
  320. {
  321. // TODO: Allow user to set dir name via ini
  322. doCountFiles("/CDImages");
  323. }
  324. else
  325. {
  326. commandHandled = 0;
  327. }
  328. return commandHandled;
  329. }