Toolbox.cpp 13 KB

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