Toolbox.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /**
  2. * Copyright (C) 2023 Eric Helgeson
  3. * Copyright (C) 2024 Rabbit Hole Computing
  4. * Copyright (C) 2025 Niels Martin Hansen
  5. *
  6. * This file is originally part of BlueSCSI adopted for ZuluSCSI
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. **/
  21. #include "Toolbox.h"
  22. #include "ZuluSCSI_disk.h"
  23. #include "ZuluSCSI_cdrom.h"
  24. #include "ZuluSCSI_log.h"
  25. #include <minIni.h>
  26. #include <SdFat.h>
  27. extern "C" {
  28. #include <toolbox.h>
  29. #include <scsi2sd_time.h>
  30. #include <sd.h>
  31. #include <mode.h>
  32. }
  33. const uint8_t MAX_FILE_LISTING_FILES = 100;
  34. extern "C" int8_t scsiToolboxEnabled()
  35. {
  36. static int8_t enabled = -1;
  37. if (enabled == -1)
  38. {
  39. enabled = ini_getbool("SCSI", "EnableToolbox", 0, CONFIGFILE);
  40. logmsg("Toolbox enabled = ", enabled);
  41. }
  42. return enabled == 1;
  43. }
  44. static bool toolboxFilenameValid(const char* name, bool isCD = false)
  45. {
  46. if(strlen(name) == 0)
  47. {
  48. dbgmsg("toolbox: Ignoring filename empty file name");
  49. return false;
  50. }
  51. if (isCD)
  52. {
  53. return scsiDiskFilenameValid(name);
  54. }
  55. return true;
  56. }
  57. static void doCountFiles(const char * dir_name, bool isCD = false)
  58. {
  59. FsFile dir;
  60. FsFile file;
  61. char name[MAX_FILE_PATH] = {0};
  62. dir.open(dir_name);
  63. dir.rewindDirectory();
  64. uint8_t file_count = 0;
  65. while (file.openNext(&dir, O_RDONLY))
  66. {
  67. if(file.getError() > 0)
  68. {
  69. file.close();
  70. break;
  71. }
  72. bool isDir = file.isDirectory();
  73. size_t len = file.getName(name, MAX_FILE_PATH);
  74. file.close();
  75. if (isCD && isDir)
  76. continue;
  77. // truncate filename the same way listing does, before validating name
  78. if (len > MAX_MAC_PATH)
  79. name[MAX_MAC_PATH] = 0x0;
  80. dbgmsg("TOOLBOX COUNT FILES: truncated filename is '", name, "'");
  81. // only count valid files.
  82. if(toolboxFilenameValid(name, isCD))
  83. {
  84. file_count = file_count + 1;
  85. if(file_count > MAX_FILE_LISTING_FILES) {
  86. scsiDev.status = CHECK_CONDITION;
  87. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  88. scsiDev.target->sense.asc = OPEN_RETRO_SCSI_TOO_MANY_FILES;
  89. scsiDev.phase = STATUS;
  90. dir.close();
  91. return;
  92. }
  93. }
  94. }
  95. scsiDev.data[0] = file_count;
  96. scsiDev.dataLen = sizeof(file_count);
  97. scsiDev.phase = DATA_IN;
  98. }
  99. static void onListFiles(const char * dir_name, bool isCD = false) {
  100. FsFile dir;
  101. FsFile file;
  102. const size_t ENTRY_SIZE = 40;
  103. memset(scsiDev.data, 0, ENTRY_SIZE * (MAX_FILE_LISTING_FILES + 1));
  104. char name[MAX_FILE_PATH] = {0};
  105. uint8_t index = 0;
  106. uint8_t file_entry[ENTRY_SIZE] = {0};
  107. dir.open(dir_name);
  108. dir.rewindDirectory();
  109. while (file.openNext(&dir, O_RDONLY))
  110. {
  111. memset(name, 0, sizeof(name));
  112. // get base information
  113. uint8_t isDir = file.isDirectory() ? 0x00 : 0x01;
  114. size_t len = file.getName(name, MAX_FILE_PATH);
  115. uint64_t size = file.fileSize();
  116. file.close();
  117. // truncate filename to fit in destination buffer
  118. if (len > MAX_MAC_PATH)
  119. name[MAX_MAC_PATH] = 0x0;
  120. dbgmsg("TOOLBOX LIST FILES: truncated filename is '", name, "'");
  121. // validate file is allowed for this listing
  122. if (!toolboxFilenameValid(name, isCD))
  123. continue;
  124. if (isCD && isDir == 0x00)
  125. continue;
  126. // fill output buffer
  127. file_entry[0] = index;
  128. file_entry[1] = isDir;
  129. for(int i = 0; i < MAX_MAC_PATH + 1 ; i++) {
  130. file_entry[i + 2] = name[i]; // bytes 2 - 34
  131. }
  132. file_entry[35] = 0; //(size >> 32) & 0xff;
  133. file_entry[36] = (size >> 24) & 0xff;
  134. file_entry[37] = (size >> 16) & 0xff;
  135. file_entry[38] = (size >> 8) & 0xff;
  136. file_entry[39] = (size) & 0xff;
  137. // send to SCSI output buffer
  138. memcpy(&(scsiDev.data[ENTRY_SIZE * index]), file_entry, ENTRY_SIZE);
  139. // increment index
  140. index = index + 1;
  141. if (index >= MAX_FILE_LISTING_FILES) break;
  142. }
  143. dir.close();
  144. scsiDev.dataLen = ENTRY_SIZE * index;
  145. scsiDev.phase = DATA_IN;
  146. dbgmsg("TOOLBOX LIST FILES: returning ", index, " files for size ", scsiDev.dataLen);
  147. }
  148. static FsFile get_file_from_index(uint8_t index, const char * dir_name, bool isCD = false)
  149. {
  150. FsFile dir;
  151. FsFile file_test;
  152. char name[MAX_FILE_PATH] = {0};
  153. dir.open(dir_name);
  154. dir.rewindDirectory(); // Back to the top
  155. int count = 0;
  156. while (file_test.openNext(&dir, O_RDONLY))
  157. {
  158. // If error there is no next file to open.
  159. if(file_test.getError() > 0) {
  160. file_test.close();
  161. break;
  162. }
  163. // no directories in CD image listing
  164. if (isCD && file_test.isDirectory())
  165. {
  166. file_test.close();
  167. continue;
  168. }
  169. // truncate filename the same way listing does, before validating name
  170. size_t len = file_test.getName(name, MAX_FILE_PATH);
  171. if (len > MAX_MAC_PATH)
  172. name[MAX_MAC_PATH] = 0x0;
  173. // validate filename
  174. if(!toolboxFilenameValid(name, isCD))
  175. {
  176. file_test.close();
  177. continue;
  178. }
  179. // found file?
  180. if (count == index)
  181. {
  182. dir.close();
  183. return file_test;
  184. }
  185. else
  186. {
  187. file_test.close();
  188. }
  189. count++;
  190. }
  191. file_test.close();
  192. dir.close();
  193. return file_test;
  194. }
  195. // Devices that are active on this SCSI device.
  196. static void onListDevices()
  197. {
  198. for (int i = 0; i < NUM_SCSIID; i++)
  199. {
  200. const S2S_TargetCfg* cfg = s2s_getConfigById(i);
  201. if (cfg && (cfg->scsiId & S2S_CFG_TARGET_ENABLED))
  202. {
  203. scsiDev.data[i] = (int)cfg->deviceType; // 2 == cd
  204. }
  205. else
  206. {
  207. scsiDev.data[i] = 0xFF; // not enabled target.
  208. }
  209. }
  210. scsiDev.dataLen = NUM_SCSIID;
  211. }
  212. static void onSetNextCD(const char * img_dir)
  213. {
  214. char name[MAX_FILE_PATH] = {0};
  215. char full_path[MAX_FILE_PATH * 2] = {0};
  216. uint8_t file_index = scsiDev.cdb[1];
  217. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  218. FsFile next_cd = get_file_from_index(file_index, img_dir, true);
  219. next_cd.getName(name, sizeof(name));
  220. next_cd.close();
  221. snprintf(full_path, (MAX_FILE_PATH * 2), "%s/%s", img_dir, name);
  222. switchNextImage(img, full_path);
  223. }
  224. FsFile gFile; // global so we can keep it open while transfering.
  225. void onGetFile10(char * dir_name) {
  226. uint8_t index = scsiDev.cdb[1];
  227. uint32_t offset = ((uint32_t)scsiDev.cdb[2] << 24) | ((uint32_t)scsiDev.cdb[3] << 16) | ((uint32_t)scsiDev.cdb[4] << 8) | scsiDev.cdb[5];
  228. if (offset == 0) // first time, open the file.
  229. {
  230. gFile = get_file_from_index(index, dir_name);
  231. if(!gFile.isDirectory() && !gFile.isReadable())
  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. return;
  238. }
  239. }
  240. uint32_t file_total = gFile.size();
  241. memset(scsiDev.data, 0, 4096);
  242. gFile.seekSet(offset * 4096);
  243. int bytes_read = gFile.read(scsiDev.data, 4096);
  244. if(offset * 4096 >= file_total) // transfer done, close.
  245. {
  246. gFile.close();
  247. }
  248. scsiDev.dataLen = bytes_read;
  249. scsiDev.phase = DATA_IN;
  250. }
  251. /*
  252. Prepares a file for receving. The file name is null terminated in the scsi data.
  253. */
  254. static void onSendFilePrep(char * dir_name)
  255. {
  256. char file_name[32+1];
  257. scsiEnterPhase(DATA_OUT);
  258. scsiRead(static_cast<uint8_t *>(static_cast<void *>(file_name)), 32+1, NULL);
  259. file_name[32] = '\0';
  260. dbgmsg("TOOLBOX OPEN FILE FOR WRITE: '", file_name, "'");
  261. SD.chdir(dir_name);
  262. gFile.open(file_name, FILE_WRITE);
  263. SD.chdir("/");
  264. if(gFile.isOpen() && gFile.isWritable())
  265. {
  266. gFile.rewind();
  267. gFile.sync();
  268. // do i need to manually set phase to status here?
  269. return;
  270. } else {
  271. gFile.close();
  272. scsiDev.status = CHECK_CONDITION;
  273. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  274. //SCSI_ASC_INVALID_FIELD_IN_CDB
  275. scsiDev.phase = STATUS;
  276. }
  277. }
  278. static void onSendFileEnd(void)
  279. {
  280. gFile.sync();
  281. gFile.close();
  282. scsiDev.phase = STATUS;
  283. }
  284. static void onSendFile10(void)
  285. {
  286. if(!gFile.isOpen() || !gFile.isWritable())
  287. {
  288. scsiDev.status = CHECK_CONDITION;
  289. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  290. //SCSI_ASC_INVALID_FIELD_IN_CDB
  291. scsiDev.phase = STATUS;
  292. }
  293. // Number of bytes sent this request, 1..512.
  294. uint16_t bytes_sent = ((uint16_t)scsiDev.cdb[1] << 8) | scsiDev.cdb[2];
  295. // 512 byte offset of where to put these bytes.
  296. uint32_t offset = ((uint32_t)scsiDev.cdb[3] << 16) | ((uint32_t)scsiDev.cdb[4] << 8) | scsiDev.cdb[5];
  297. const uint16_t BUFSIZE = 512;
  298. uint8_t buf[BUFSIZE];
  299. // Do not allow buffer overrun
  300. if (bytes_sent > BUFSIZE)
  301. {
  302. dbgmsg("TOOLBOX SEND FILE 10 ILLEGAL DATA SIZE");
  303. gFile.close();
  304. scsiDev.status = CHECK_CONDITION;
  305. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  306. }
  307. scsiEnterPhase(DATA_OUT);
  308. scsiRead(buf, bytes_sent, NULL);
  309. gFile.seekCur(offset * 512);
  310. gFile.write(buf, bytes_sent);
  311. if(gFile.getWriteError())
  312. {
  313. gFile.clearWriteError();
  314. gFile.close();
  315. scsiDev.status = CHECK_CONDITION;
  316. scsiDev.target->sense.code = ILLEGAL_REQUEST;
  317. }
  318. //scsiDev.phase = STATUS;
  319. }
  320. static void onToggleDebug()
  321. {
  322. if(scsiDev.cdb[1] == 0) // 0 == Set Debug, 1 == Get Debug State
  323. {
  324. g_log_debug = scsiDev.cdb[2];
  325. logmsg("Set debug logs to: ", g_log_debug);
  326. scsiDev.phase = STATUS;
  327. }
  328. else
  329. {
  330. logmsg("Debug currently set to: ", g_log_debug);
  331. scsiDev.data[0] = g_log_debug ? 0x1 : 0x0;
  332. scsiDev.dataLen = 1;
  333. scsiDev.phase = DATA_IN;
  334. }
  335. }
  336. static int getToolBoxSharedDir(char * dir_name)
  337. {
  338. return ini_gets("SCSI", "ToolBoxSharedDir", "/shared", dir_name, MAX_FILE_PATH, CONFIGFILE);
  339. }
  340. extern "C" int scsiToolboxCommand()
  341. {
  342. int commandHandled = 1;
  343. image_config_t &img = *(image_config_t*)scsiDev.target->cfg;
  344. uint8_t command = scsiDev.cdb[0];
  345. if (unlikely(command == TOOLBOX_COUNT_FILES))
  346. {
  347. char img_dir[MAX_FILE_PATH];
  348. dbgmsg("TOOLBOX_COUNT_FILES");
  349. getToolBoxSharedDir(img_dir);
  350. doCountFiles(img_dir);
  351. }
  352. else if (unlikely(command == TOOLBOX_LIST_FILES))
  353. {
  354. char img_dir[MAX_FILE_PATH];
  355. dbgmsg("TOOLBOX_LIST_FILES");
  356. getToolBoxSharedDir(img_dir);
  357. onListFiles(img_dir);
  358. }
  359. else if (unlikely(command == TOOLBOX_GET_FILE))
  360. {
  361. char img_dir[MAX_FILE_PATH];
  362. dbgmsg("TOOLBOX_GET_FILE");
  363. getToolBoxSharedDir(img_dir);
  364. onGetFile10(img_dir);
  365. }
  366. else if (unlikely(command == TOOLBOX_SEND_FILE_PREP))
  367. {
  368. char img_dir[MAX_FILE_PATH];
  369. dbgmsg("TOOLBOX_SEND_FILE_PREP");
  370. getToolBoxSharedDir(img_dir);
  371. onSendFilePrep(img_dir);
  372. }
  373. else if (unlikely(command == TOOLBOX_SEND_FILE_10))
  374. {
  375. dbgmsg("TOOLBOX_SEND_FILE_10");
  376. onSendFile10();
  377. }
  378. else if (unlikely(command == TOOLBOX_SEND_FILE_END))
  379. {
  380. dbgmsg("TOOLBOX_SEND_FILE_END");
  381. onSendFileEnd();
  382. }
  383. else if(unlikely(command == TOOLBOX_TOGGLE_DEBUG))
  384. {
  385. dbgmsg("TOOLBOX_TOGGLE_DEBUG");
  386. onToggleDebug();
  387. }
  388. else if(unlikely(command == TOOLBOX_LIST_CDS))
  389. {
  390. char img_dir[4];
  391. dbgmsg("TOOLBOX_LIST_CDS");
  392. snprintf(img_dir, sizeof(img_dir), CD_IMG_DIR, (int)img.scsiId & S2S_CFG_TARGET_ID_BITS);
  393. onListFiles(img_dir, true);
  394. }
  395. else if(unlikely(command == TOOLBOX_SET_NEXT_CD))
  396. {
  397. char img_dir[4];
  398. dbgmsg("TOOLBOX_SET_NEXT_CD");
  399. snprintf(img_dir, sizeof(img_dir), CD_IMG_DIR, (int)img.scsiId & S2S_CFG_TARGET_ID_BITS);
  400. onSetNextCD(img_dir);
  401. }
  402. else if(unlikely(command == TOOLBOX_LIST_DEVICES))
  403. {
  404. dbgmsg("TOOLBOX_LIST_DEVICES");
  405. onListDevices();
  406. }
  407. else if (unlikely(command == TOOLBOX_COUNT_CDS))
  408. {
  409. char img_dir[4];
  410. dbgmsg("TOOLBOX_COUNT_CDS");
  411. snprintf(img_dir, sizeof(img_dir), CD_IMG_DIR, (int)img.scsiId & S2S_CFG_TARGET_ID_BITS);
  412. doCountFiles(img_dir, true);
  413. }
  414. else
  415. {
  416. commandHandled = 0;
  417. }
  418. return commandHandled;
  419. }