1
0

fs.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  1. /*
  2. * Filesystem library
  3. * Handles all filesystem related work for BDOS using CH376.
  4. * Uses only bottom USB port (SPI1 CH376),
  5. * allowing top USB top to be used for other USB devices (like HID).
  6. */
  7. // uses stdlib.c
  8. #define FS_INTERRUPT_ADDR 0xC0272D
  9. //CH376 Codes
  10. #define FS_CMD_GET_IC_VER 0x01
  11. #define FS_CMD_SET_BAUDRATE 0x02
  12. #define FS_CMD_ENTER_SLEEP 0x03
  13. #define FS_CMD_SET_USB_SPEED 0x04
  14. #define FS_CMD_RESET_ALL 0x05
  15. #define FS_CMD_CHECK_EXIST 0x06
  16. #define FS_CMD_SET_SD0_INT 0x0b
  17. #define FS_CMD_SET_RETRY 0x0b
  18. #define FS_CMD_GET_FILE_SIZE 0x0c
  19. #define FS_CMD_SET_FILE_SIZE 0x0d
  20. #define FS_CMD_SET_USB_ADDRESS 0x13
  21. #define FS_CMD_SET_USB_MODE 0x15
  22. #define FS_MODE_HOST_0 0x05
  23. #define FS_MODE_HOST_1 0x07
  24. #define FS_MODE_HOST_2 0x06
  25. #define FS_CMD_GET_STATUS 0x22
  26. #define FS_CMD_RD_USB_DATA0 0x27
  27. #define FS_CMD_WR_USB_DATA 0x2c
  28. #define FS_CMD_WR_REQ_DATA 0x2d
  29. #define FS_CMD_WR_OFS_DATA 0x2e
  30. #define FS_CMD_SET_FILE_NAME 0x2f
  31. #define FS_CMD_DISK_CONNECT 0x30
  32. #define FS_CMD_DISK_MOUNT 0x31
  33. #define FS_CMD_FILE_OPEN 0x32
  34. #define FS_CMD_FILE_ENUM_GO 0x33
  35. #define FS_CMD_FILE_CREATE 0x34
  36. #define FS_CMD_FILE_ERASE 0x35
  37. #define FS_CMD_FILE_CLOSE 0x36
  38. #define FS_CMD_DIR_INFO_READ 0x37
  39. #define FS_CMD_DIR_INFO_SAVE 0x38
  40. #define FS_CMD_BYTE_LOCATE 0x39
  41. #define FS_CMD_BYTE_READ 0x3a
  42. #define FS_CMD_BYTE_RD_GO 0x3b
  43. #define FS_CMD_BYTE_WRITE 0x3c
  44. #define FS_CMD_BYTE_WR_GO 0x3d
  45. #define FS_CMD_DISK_CAPACITY 0x3e
  46. #define FS_CMD_DISK_QUERY 0x3f
  47. #define FS_CMD_DIR_CREATE 0x40
  48. #define FS_CMD_SET_ADDRESS 0x45
  49. #define FS_CMD_GET_DESCR 0x46
  50. #define FS_CMD_SET_CONFIG 0x49
  51. #define FS_CMD_AUTO_CONFIG 0x4D
  52. #define FS_CMD_ISSUE_TKN_X 0x4E
  53. #define FS_ANSW_RET_SUCCESS 0x51
  54. #define FS_ANSW_USB_INT_SUCCESS 0x14
  55. #define FS_ANSW_USB_INT_CONNECT 0x15
  56. #define FS_ANSW_USB_INT_DISCONNECT 0x16
  57. #define FS_ANSW_USB_INT_USB_READY 0x18
  58. #define FS_ANSW_USB_INT_DISK_READ 0x1d
  59. #define FS_ANSW_USB_INT_DISK_WRITE 0x1e
  60. #define FS_ANSW_RET_ABORT 0x5F
  61. #define FS_ANSW_USB_INT_DISK_ERR 0x1f
  62. #define FS_ANSW_USB_INT_BUF_OVER 0x17
  63. #define FS_ANSW_ERR_OPEN_DIR 0x41
  64. #define FS_ANSW_ERR_MISS_FILE 0x42
  65. #define FS_ANSW_ERR_FOUND_NAME 0x43
  66. #define FS_ANSW_ERR_DISK_DISCON 0x82
  67. #define FS_ANSW_ERR_LARGE_SECTOR 0x84
  68. #define FS_ANSW_ERR_TYPE_ERROR 0x92
  69. #define FS_ANSW_ERR_BPB_ERROR 0xa1
  70. #define FS_ANSW_ERR_DISK_FULL 0xb1
  71. #define FS_ANSW_ERR_FDT_OVER 0xb2
  72. #define FS_ANSW_ERR_FILE_CLOSE 0xb4
  73. #define FS_ERR_LONGFILENAME 0x01
  74. #define FS_ATTR_READ_ONLY 0x01
  75. #define FS_ATTR_HIDDEN 0x02
  76. #define FS_ATTR_SYSTEM 0x04
  77. #define FS_ATTR_VOLUME_ID 0x08
  78. #define FS_ATTR_DIRECTORY 0x10
  79. #define FS_ATTR_ARCHIVE 0x20
  80. #define FS_LDIR_FNAME_SIZE 16
  81. // 2d array for filename list and 1d array for filesize list used in listdir
  82. char (*fsLdirFnames)[FS_LDIR_FNAME_SIZE] = (char (*)[FS_LDIR_FNAME_SIZE]) FS_LDIR_FNAME_ADDR;
  83. word *fsLdirFsizes= (word *) FS_LDIR_FSIZE_ADDR;
  84. word fsLdirEntries = 0; // index for lists
  85. // Workaround for defines in ASM
  86. void FS_asmDefines()
  87. {
  88. asm(
  89. "define FS_SPI1_CS_ADDR = 0xC0272C ; address of SPI1_CS\n"
  90. "define FS_SPI1_ADDR = 0xC0272B ; address of SPI1\n"
  91. );
  92. }
  93. // Sets SPI1_CS low
  94. void FS_spiBeginTransfer()
  95. {
  96. asm(
  97. "; backup regs\n"
  98. "push r1\n"
  99. "push r2\n"
  100. "load32 FS_SPI1_CS_ADDR r2 ; r2 = FS_SPI1_CS_ADDR\n"
  101. "load 0 r1 ; r1 = 0 (enable)\n"
  102. "write 0 r2 r1 ; write to SPI1_CS\n"
  103. "; restore regs\n"
  104. "pop r2\n"
  105. "pop r1\n"
  106. );
  107. }
  108. // Sets SPI1_CS high
  109. void FS_spiEndTransfer()
  110. {
  111. asm(
  112. "; backup regs\n"
  113. "push r1\n"
  114. "push r2\n"
  115. "load32 FS_SPI1_CS_ADDR r2 ; r2 = FS_SPI1_CS_ADDR\n"
  116. "load 1 r1 ; r1 = 1 (disable)\n"
  117. "write 0 r2 r1 ; write to SPI1_CS\n"
  118. "; restore regs\n"
  119. "pop r2\n"
  120. "pop r1\n"
  121. );
  122. }
  123. // Write dataByte over SPI1 and return read value
  124. // write 0x00 for a read
  125. word FS_spiTransfer(word dataByte)
  126. {
  127. word retval = 0;
  128. asm(
  129. "load32 FS_SPI1_ADDR r2 ; r2 = FS_SPI1_ADDR\n"
  130. "write 0 r2 r4 ; write r4 over SPI1\n"
  131. "read 0 r2 r2 ; read return value\n"
  132. "write -4 r14 r2 ; write to stack to return\n"
  133. );
  134. return retval;
  135. }
  136. // Get status after waiting for an interrupt
  137. word FS_WaitGetStatus()
  138. {
  139. word intValue = 1;
  140. while(intValue)
  141. {
  142. word *i = (word *) FS_INTERRUPT_ADDR;
  143. intValue = *i;
  144. }
  145. FS_spiBeginTransfer();
  146. FS_spiTransfer(FS_CMD_GET_STATUS);
  147. word retval = FS_spiTransfer(0x00);
  148. FS_spiEndTransfer();
  149. return retval;
  150. }
  151. // Get status without using interrupts
  152. word FS_noWaitGetStatus()
  153. {
  154. FS_spiBeginTransfer();
  155. FS_spiTransfer(FS_CMD_GET_STATUS);
  156. word retval = FS_spiTransfer(0x00);
  157. FS_spiEndTransfer();
  158. return retval;
  159. }
  160. // Function to send a string (without terminating 0)
  161. void FS_sendString(char* str)
  162. {
  163. char chr = *str; // first character of str
  164. while (chr != 0) // continue until null value
  165. {
  166. FS_spiTransfer(chr);
  167. str++; // go to next character address
  168. chr = *str; // get character from address
  169. }
  170. }
  171. // Function to send data d of size s
  172. void FS_sendData(char* d, word s)
  173. {
  174. char chr = *d; // first byte of data
  175. // write s bytes
  176. word i;
  177. for(i = 0; (unsigned int) i < (unsigned int)s; i++)
  178. {
  179. FS_spiTransfer(chr);
  180. d++; // go to next data address
  181. chr = *d; // get data from address
  182. }
  183. }
  184. // Returns IC version of CH376 chip
  185. // good test to know if the communication with chip works
  186. word FS_getICver()
  187. {
  188. FS_spiBeginTransfer();
  189. FS_spiTransfer(FS_CMD_GET_IC_VER);
  190. word icVer = FS_spiTransfer(0x00);
  191. FS_spiEndTransfer();
  192. return icVer;
  193. }
  194. // Sets USB mode to mode, returns status code
  195. // which should be FS_ANSW_RET_SUCCESS when successful
  196. // probably does not need all three delays though!
  197. word FS_setUSBmode(word mode)
  198. {
  199. FS_spiBeginTransfer();
  200. FS_spiTransfer(FS_CMD_SET_USB_MODE);
  201. FS_spiEndTransfer();
  202. delay(1);
  203. FS_spiBeginTransfer();
  204. FS_spiTransfer(mode);
  205. FS_spiEndTransfer();
  206. delay(1);
  207. FS_spiBeginTransfer();
  208. word status = FS_spiTransfer(0x00);
  209. FS_spiEndTransfer();
  210. delay(1);
  211. return status;
  212. }
  213. // Resets and intitializes CH376
  214. // returns FS_ANSW_RET_SUCCESS on success
  215. word FS_init()
  216. {
  217. FS_asmDefines(); // prevent deletion of function by optimizer
  218. FS_spiEndTransfer(); // start with cs high
  219. delay(10);
  220. // reset
  221. FS_spiBeginTransfer();
  222. FS_spiTransfer(FS_CMD_RESET_ALL);
  223. FS_spiEndTransfer();
  224. delay(100); // wait after reset
  225. // USB mode 0
  226. return FS_setUSBmode(FS_MODE_HOST_0);
  227. }
  228. // Connects a drive:
  229. // - waits for drive connection,
  230. // - sets usb host mode
  231. // - waits for drive to be ready
  232. // - mounts drive
  233. // - also initializes current path to /
  234. // returns FS_ANSW_USB_INT_SUCCESS on success
  235. word FS_connectDrive()
  236. {
  237. // wait forever until an USB device is connected
  238. while(FS_WaitGetStatus() != FS_ANSW_USB_INT_CONNECT);
  239. // USB mode 1
  240. word retval = FS_setUSBmode(FS_MODE_HOST_1);
  241. // return on error
  242. if (retval != FS_ANSW_RET_SUCCESS)
  243. {
  244. return retval;
  245. }
  246. // USB mode 2
  247. retval = FS_setUSBmode(FS_MODE_HOST_2);
  248. // return on error
  249. if (retval != FS_ANSW_RET_SUCCESS)
  250. {
  251. return retval;
  252. }
  253. // need to check again for device connection after changing USB mode
  254. while(FS_WaitGetStatus() != FS_ANSW_USB_INT_CONNECT);
  255. // connect to drive
  256. FS_spiBeginTransfer();
  257. FS_spiTransfer(FS_CMD_DISK_CONNECT);
  258. FS_spiEndTransfer();
  259. retval = FS_WaitGetStatus();
  260. // return on error
  261. if (retval != FS_ANSW_USB_INT_SUCCESS)
  262. {
  263. return retval;
  264. }
  265. FS_spiBeginTransfer();
  266. FS_spiTransfer(FS_CMD_DISK_MOUNT);
  267. FS_spiEndTransfer();
  268. // reset path variable to /
  269. SHELL_path[0] = '/';
  270. SHELL_path[1] = 0; // terminate string
  271. // also reset path backup
  272. strcpy(SHELL_pathBackup, SHELL_path);
  273. return FS_WaitGetStatus();
  274. }
  275. // Returns file size of currently opened file (32 bits)
  276. word FS_getFileSize()
  277. {
  278. FS_spiBeginTransfer();
  279. FS_spiTransfer(FS_CMD_GET_FILE_SIZE);
  280. FS_spiTransfer(0x68);
  281. word retval = FS_spiTransfer(0);
  282. retval = retval + (FS_spiTransfer(0) << 8);
  283. retval = retval + (FS_spiTransfer(0) << 16);
  284. retval = retval + (FS_spiTransfer(0) << 24);
  285. FS_spiEndTransfer();
  286. return retval;
  287. }
  288. // Sets cursor to position s
  289. // returns FS_ANSW_USB_INT_SUCCESS on success
  290. word FS_setCursor(word s)
  291. {
  292. FS_spiBeginTransfer();
  293. FS_spiTransfer(FS_CMD_BYTE_LOCATE);
  294. FS_spiTransfer(s);
  295. FS_spiTransfer((unsigned) s >> 8);
  296. FS_spiTransfer((unsigned) s >> 16);
  297. FS_spiTransfer((unsigned) s >> 24);
  298. FS_spiEndTransfer();
  299. return FS_WaitGetStatus();
  300. }
  301. // Reads s bytes into buf
  302. // if bytesToWord is true, four bytes will be stored in one address/word
  303. // can read 65536 bytes per call
  304. // returns FS_ANSW_USB_INT_SUCCESS on success
  305. // TODO: this surely can be optimized for speed in some way! (hint: assembly)
  306. word FS_readFile(char* buf, word s, word bytesToWord)
  307. {
  308. if (s == 0)
  309. {
  310. return FS_ANSW_USB_INT_SUCCESS;
  311. }
  312. FS_spiBeginTransfer();
  313. FS_spiTransfer(FS_CMD_BYTE_READ);
  314. FS_spiTransfer(s);
  315. FS_spiTransfer((unsigned) s >> 8);
  316. FS_spiEndTransfer();
  317. word retval = FS_WaitGetStatus();
  318. // return on error
  319. if (retval != FS_ANSW_USB_INT_DISK_READ)
  320. {
  321. return retval;
  322. }
  323. word bytesRead = 0;
  324. word wordsRead = 0;
  325. word doneReading = 0;
  326. // clear first address
  327. buf[wordsRead] = 0;
  328. // used for shifting bytes to word
  329. word currentByteShift = 24;
  330. while (doneReading == 0)
  331. {
  332. // read set of bytes (max 255)
  333. FS_spiBeginTransfer();
  334. FS_spiTransfer(FS_CMD_RD_USB_DATA0);
  335. word readLen = FS_spiTransfer(0x00);
  336. word readByte;
  337. word i;
  338. for (i = 0; i < readLen; i++)
  339. {
  340. readByte = FS_spiTransfer(0x00);
  341. // read 4 bytes into one word, from left to right
  342. if (bytesToWord)
  343. {
  344. readByte = readByte << currentByteShift;
  345. buf[wordsRead] = buf[wordsRead] + readByte;
  346. if (currentByteShift == 0)
  347. {
  348. currentByteShift = 24;
  349. wordsRead++;
  350. buf[wordsRead] = 0;
  351. }
  352. else
  353. {
  354. currentByteShift -= 8;
  355. }
  356. }
  357. else
  358. {
  359. buf[bytesRead] = (char)readByte;
  360. bytesRead = bytesRead + 1;
  361. }
  362. }
  363. FS_spiEndTransfer();
  364. // requesting another set of data
  365. FS_spiBeginTransfer();
  366. FS_spiTransfer(FS_CMD_BYTE_RD_GO);
  367. FS_spiEndTransfer();
  368. retval = FS_WaitGetStatus();
  369. if (retval == FS_ANSW_USB_INT_SUCCESS)
  370. {
  371. doneReading = 1;
  372. }
  373. else if (retval == FS_ANSW_USB_INT_DISK_READ)
  374. {
  375. // read another block
  376. }
  377. else
  378. {
  379. GFX_PrintConsole("E: Error while reading data\n");
  380. return retval;
  381. }
  382. }
  383. return retval;
  384. }
  385. // Writes data d of size s
  386. // can only write 65536 bytes at a time
  387. // returns FS_ANSW_USB_INT_SUCCESS on success
  388. // TODO: optimize for speed
  389. word FS_writeFile(char* d, word s)
  390. {
  391. if (s == 0)
  392. {
  393. return FS_ANSW_USB_INT_SUCCESS;
  394. }
  395. FS_spiBeginTransfer();
  396. FS_spiTransfer(FS_CMD_BYTE_WRITE);
  397. FS_spiTransfer(s);
  398. FS_spiTransfer((unsigned) s >> 8);
  399. FS_spiEndTransfer();
  400. word retval = FS_WaitGetStatus();
  401. // return on error
  402. if (retval != FS_ANSW_USB_INT_DISK_WRITE)
  403. {
  404. return retval;
  405. }
  406. word bytesWritten = 0;
  407. word doneWriting = 0;
  408. while (doneWriting == 0)
  409. {
  410. // write set of bytes (max 255)
  411. FS_spiBeginTransfer();
  412. FS_spiTransfer(FS_CMD_WR_REQ_DATA);
  413. word wrLen = FS_spiTransfer(0x00);
  414. FS_sendData(d + bytesWritten, wrLen);
  415. bytesWritten = bytesWritten + wrLen;
  416. FS_spiEndTransfer();
  417. // update file size
  418. FS_spiBeginTransfer();
  419. FS_spiTransfer(FS_CMD_BYTE_WR_GO);
  420. FS_spiEndTransfer();
  421. retval = FS_WaitGetStatus();
  422. if (retval == FS_ANSW_USB_INT_SUCCESS)
  423. {
  424. doneWriting = 1;
  425. }
  426. else if (retval == FS_ANSW_USB_INT_DISK_WRITE)
  427. {
  428. // write another block
  429. }
  430. else
  431. {
  432. GFX_PrintConsole("E: Error while writing data\n");
  433. return retval;
  434. }
  435. }
  436. return retval;
  437. }
  438. // Returns status of opening a file/directory
  439. // usually the successful status codes are:
  440. // FS_ANSW_USB_INT_SUCCESS || FS_ANSW_ERR_OPEN_DIR || FS_ANSW_USB_INT_DISK_READ
  441. word FS_open()
  442. {
  443. FS_spiBeginTransfer();
  444. FS_spiTransfer(FS_CMD_FILE_OPEN);
  445. FS_spiEndTransfer();
  446. return FS_WaitGetStatus();
  447. }
  448. // Returns FS_ANSW_USB_INT_SUCCESS on success
  449. word FS_delete()
  450. {
  451. FS_spiBeginTransfer();
  452. FS_spiTransfer(FS_CMD_FILE_ERASE);
  453. FS_spiEndTransfer();
  454. return FS_WaitGetStatus();
  455. }
  456. // Returns FS_ANSW_USB_INT_SUCCESS on success
  457. word FS_close()
  458. {
  459. FS_spiBeginTransfer();
  460. FS_spiTransfer(FS_CMD_FILE_CLOSE);
  461. FS_spiTransfer(0x01); //0x01 if update filesize, else 0x00
  462. FS_spiEndTransfer();
  463. return FS_WaitGetStatus();
  464. }
  465. // Returns FS_ANSW_USB_INT_SUCCESS on success
  466. word FS_createDir()
  467. {
  468. FS_spiBeginTransfer();
  469. FS_spiTransfer(FS_CMD_DIR_CREATE);
  470. FS_spiEndTransfer();
  471. return FS_WaitGetStatus();
  472. }
  473. // Creates file (recreates if exists)
  474. // new files are 1 byte long
  475. // have not found a way to set it to 0 bytes,
  476. // since FS_CMD_SET_FILE_SIZE does not work
  477. // automatically closes file
  478. // returns FS_ANSW_USB_INT_SUCCESS on success
  479. word FS_createFile()
  480. {
  481. FS_spiBeginTransfer();
  482. FS_spiTransfer(FS_CMD_FILE_CREATE);
  483. FS_spiEndTransfer();
  484. word retval = FS_WaitGetStatus();
  485. // Return on error
  486. if (retval != FS_ANSW_USB_INT_SUCCESS)
  487. {
  488. return retval;
  489. }
  490. // open and close file
  491. FS_open();
  492. FS_close();
  493. return FS_ANSW_USB_INT_SUCCESS;
  494. }
  495. // Sends single path f
  496. // no processing of f is done, it is directly sent
  497. // this means no error checking on file length!
  498. void FS_sendSinglePath(char* f)
  499. {
  500. FS_spiBeginTransfer();
  501. FS_spiTransfer(FS_CMD_SET_FILE_NAME);
  502. FS_sendString(f); // send file name
  503. FS_spiTransfer(0); // close with null
  504. FS_spiEndTransfer();
  505. }
  506. // Sends path f
  507. // can be relative or absolute file or directory
  508. // REQUIRES CAPITAL LETTERS and must conform the 8.3 filename standard
  509. // function can handle folders with forward slashes
  510. // returns FS_ANSW_USB_INT_SUCCESS on success
  511. word FS_sendFullPath(char* f)
  512. {
  513. // path to uppercase and replace backslash by slash
  514. word i = 0;
  515. while (f[i] != 0)
  516. {
  517. f[i] = toUpper(f[i]);
  518. if (f[i] == '\\')
  519. {
  520. f[i] = '/';
  521. }
  522. i++;
  523. }
  524. word removedSlash = 0; // to restore removed slash
  525. // remove (single) trailing slash if exists
  526. // we reuse i here since it points to the end of the path
  527. if (i > 1) // ignore the root path
  528. {
  529. if (f[i-1] == '/')
  530. {
  531. f[i-1] = 0;
  532. removedSlash = 1;
  533. }
  534. }
  535. // if first char is a /, then we go back to root
  536. if (f[0] == '/')
  537. {
  538. FS_sendSinglePath("/");
  539. FS_open();
  540. }
  541. i = 0;
  542. char buf[16]; // buffer for single folder/file name
  543. word bufi = 0;
  544. while (f[i] != 0)
  545. {
  546. // handle all directories
  547. if (f[i] == 47) // forward slash
  548. {
  549. // return error on opening folders containing a single or double dot
  550. if ((bufi == 1 && buf[0] == '.') || (bufi == 2 && buf[0] == '.' && buf[1] == '.'))
  551. {
  552. return FS_ANSW_ERR_OPEN_DIR;
  553. }
  554. // add null to end of buf
  555. buf[bufi] = 0;
  556. // send buf
  557. FS_spiBeginTransfer();
  558. FS_spiTransfer(FS_CMD_SET_FILE_NAME);
  559. FS_sendString(buf); // send folder name
  560. FS_spiTransfer(0); // close with null
  561. FS_spiEndTransfer();
  562. // reset bufi
  563. bufi = 0;
  564. // open folder
  565. word retval = FS_open();
  566. // return on if failure / folder not found
  567. if (retval != FS_ANSW_USB_INT_SUCCESS && retval != FS_ANSW_ERR_OPEN_DIR)
  568. {
  569. return retval;
  570. }
  571. }
  572. else
  573. {
  574. buf[bufi] = f[i];
  575. bufi++;
  576. if (bufi > 13)
  577. {
  578. return FS_ERR_LONGFILENAME; // exit if folder/file name is too long
  579. }
  580. }
  581. i++;
  582. }
  583. /*
  584. if (removedSlash) // restore removed slash if there
  585. {
  586. f[i] = '/';
  587. f[i+1] = 0;
  588. }*/
  589. // handle filename itself (if any)
  590. // add null to end of buf
  591. buf[bufi] = 0;
  592. if (bufi == 0)
  593. {
  594. return FS_ANSW_USB_INT_SUCCESS; // exit if there is no filename after the folder
  595. }
  596. // return error on opening folders containing a single or double dot
  597. if ((bufi == 1 && buf[0] == '.') || (bufi == 2 && buf[0] == '.' && buf[1] == '.'))
  598. {
  599. return FS_ANSW_ERR_OPEN_DIR;
  600. }
  601. // send buf (last part of string)
  602. FS_spiBeginTransfer();
  603. FS_spiTransfer(FS_CMD_SET_FILE_NAME);
  604. FS_sendString(buf); // send file name
  605. FS_spiTransfer(0); // close with null
  606. FS_spiEndTransfer();
  607. return FS_ANSW_USB_INT_SUCCESS;
  608. }
  609. // Parses and prints a string (useful for name and extension) after removing trailing spaces
  610. // len should be <= 8 chars
  611. // does not add a new line at the end.
  612. // returns length of written string
  613. word FS_parseFATstring(char* fatBuffer, word len, char* b)
  614. {
  615. if (len > 8)
  616. {
  617. GFX_PrintConsole("FATstring: Len argument > 8\n");
  618. return 0;
  619. }
  620. word retval = 0;
  621. word bufLen = 0;
  622. // buffer of parsed string
  623. char nameBuf[9];
  624. nameBuf[len] = 0;
  625. // loop backwards until a non-space character is found
  626. // then, write string to nameBuf from there, keeping spaces in filename if any
  627. word foundChar = 0;
  628. word i;
  629. for (i = 0; i < len; i++)
  630. {
  631. if (!foundChar)
  632. {
  633. if (fatBuffer[len-1-i] == ' ')
  634. {
  635. nameBuf[len-1-i] = 0; // set null until a non-space char is found
  636. }
  637. else
  638. {
  639. foundChar = 1;
  640. retval = len-i;
  641. nameBuf[len-1-i] = fatBuffer[len-1-i]; // write the non-space char
  642. }
  643. }
  644. else
  645. {
  646. nameBuf[len-1-i] = fatBuffer[len-1-i]; // copy char
  647. }
  648. }
  649. // write to buffer
  650. i = 0;
  651. while (nameBuf[i] != 0)
  652. {
  653. b[bufLen] = nameBuf[i];
  654. bufLen++;
  655. i++;
  656. }
  657. return retval;
  658. }
  659. // Parses and writes name.extension and filesize on one line
  660. // ignores lines with '.' and ".." as filename
  661. void FS_parseFATdata(word datalen, char* fatBuffer)
  662. {
  663. if (datalen != 32)
  664. {
  665. memcpy(fsLdirFnames[fsLdirEntries], "", 1);
  666. fsLdirFsizes[fsLdirEntries] = 0;
  667. GFX_PrintConsole("Unexpected FAT table length\n");
  668. return;
  669. }
  670. // ignore '.'
  671. if (memcmp(fatBuffer, ". ", 11))
  672. {
  673. memcpy(fsLdirFnames[fsLdirEntries], ".", 2);
  674. fsLdirFsizes[fsLdirEntries] = -1;
  675. return;
  676. }
  677. // ignore ".."
  678. if (memcmp(fatBuffer, ".. ", 11))
  679. {
  680. memcpy(fsLdirFnames[fsLdirEntries], "..", 3);
  681. fsLdirFsizes[fsLdirEntries] = -1;
  682. return;
  683. }
  684. // parse filename
  685. word fnameLen = FS_parseFATstring(fatBuffer, 8, fsLdirFnames[fsLdirEntries]);
  686. // add '.' and parse extension
  687. if (fatBuffer[8] != ' ' || fatBuffer[9] != ' ' || fatBuffer[10] != ' ')
  688. {
  689. fsLdirFnames[fsLdirEntries][fnameLen] = '.';
  690. fnameLen++;
  691. fnameLen += FS_parseFATstring(fatBuffer+8, 3, fsLdirFnames[fsLdirEntries] + fnameLen);
  692. }
  693. // check if dir
  694. word attributes = fatBuffer[11];
  695. word isDir = 0;
  696. if (attributes & 0x10)
  697. {
  698. isDir = 1;
  699. }
  700. fsLdirFnames[fsLdirEntries][fnameLen] = 0; // terminate
  701. // stop if dir
  702. if (isDir)
  703. {
  704. fsLdirFsizes[fsLdirEntries] = -1;
  705. return;
  706. }
  707. // filesize
  708. word fileSize = 0;
  709. fileSize += fatBuffer[28];
  710. fileSize += (fatBuffer[29] << 8);
  711. fileSize += (fatBuffer[30] << 16);
  712. fileSize += (fatBuffer[31] << 24);
  713. fsLdirFsizes[fsLdirEntries] = fileSize;
  714. }
  715. // Reads FAT data for single entry
  716. // FAT data is parsed by FS_parseFatData()
  717. void FS_readFATdata()
  718. {
  719. FS_spiBeginTransfer();
  720. FS_spiTransfer(FS_CMD_RD_USB_DATA0);
  721. word datalen = FS_spiTransfer(0x0);
  722. char fatbuf[32];
  723. word i;
  724. for (i = 0; i < datalen; i++)
  725. {
  726. fatbuf[i] = FS_spiTransfer(0x00);
  727. }
  728. FS_parseFATdata(datalen, fatbuf);
  729. FS_spiEndTransfer();
  730. }
  731. // Lists directory of full path f
  732. // f needs to start with / and not end with /
  733. // returns FS_ANSW_USB_INT_SUCCESS if successful
  734. // uses fsLdirX variables to sort all entries
  735. // writes parsed result to address b
  736. // result is terminated with a \0
  737. word FS_listDir(char* f, char* b)
  738. {
  739. b[0] = 0; // initialize output string
  740. word retval = FS_sendFullPath(f);
  741. // return on failure
  742. if (retval != FS_ANSW_USB_INT_SUCCESS)
  743. {
  744. return retval;
  745. }
  746. retval = FS_open();
  747. // return on failure
  748. if (retval != FS_ANSW_USB_INT_SUCCESS && retval != FS_ANSW_ERR_OPEN_DIR)
  749. {
  750. return retval;
  751. }
  752. FS_sendSinglePath("*");
  753. retval = FS_open();
  754. // return on failure
  755. if (retval != FS_ANSW_USB_INT_DISK_READ)
  756. {
  757. return retval;
  758. }
  759. fsLdirEntries = 0; // reset
  760. while (retval == FS_ANSW_USB_INT_DISK_READ)
  761. {
  762. FS_readFATdata();
  763. fsLdirEntries++;
  764. FS_spiBeginTransfer();
  765. FS_spiTransfer(FS_CMD_FILE_ENUM_GO);
  766. FS_spiEndTransfer();
  767. retval = FS_WaitGetStatus();
  768. }
  769. // print lists
  770. word i, j, tmpFsize;
  771. char tmpFname[FS_LDIR_FNAME_SIZE];
  772. char fsizeBuf[12]; // max 4.29B which fits in 12b + term
  773. // sort ascending on filesize to get folders on top
  774. for (i = 0; i < fsLdirEntries; ++i)
  775. {
  776. for (j = i + 1; j < fsLdirEntries; ++j)
  777. {
  778. if (fsLdirFsizes[i] > fsLdirFsizes[j]) // signed comparison
  779. {
  780. tmpFsize = fsLdirFsizes[i];
  781. strcpy(tmpFname, fsLdirFnames[i]);
  782. fsLdirFsizes[i] = fsLdirFsizes[j];
  783. strcpy(fsLdirFnames[i], fsLdirFnames[j]);
  784. fsLdirFsizes[j] = tmpFsize;
  785. strcpy(fsLdirFnames[j], tmpFname);
  786. }
  787. }
  788. }
  789. // sort ascending on filename without mixing files and folders
  790. for (i = 0; i < fsLdirEntries; ++i)
  791. {
  792. for (j = i + 1; j < fsLdirEntries; ++j)
  793. {
  794. if ((fsLdirFsizes[i] >= 0 && fsLdirFsizes[j] >= 0) || (fsLdirFsizes[i] < 0 && fsLdirFsizes[j] < 0)) // do not mix
  795. {
  796. if (strcmp(fsLdirFnames[i], fsLdirFnames[j]) > 0) // signed comparison
  797. {
  798. tmpFsize = fsLdirFsizes[i];
  799. strcpy(tmpFname, fsLdirFnames[i]);
  800. fsLdirFsizes[i] = fsLdirFsizes[j];
  801. strcpy(fsLdirFnames[i], fsLdirFnames[j]);
  802. fsLdirFsizes[j] = tmpFsize;
  803. strcpy(fsLdirFnames[j], tmpFname);
  804. }
  805. }
  806. }
  807. }
  808. // write printable string to b
  809. for (i = 0; i < fsLdirEntries; i++)
  810. {
  811. // skip "." and ".."
  812. if (strcmp(fsLdirFnames[i], ".") && strcmp(fsLdirFnames[i], ".."))
  813. {
  814. // print filename.ext
  815. strcat(b, fsLdirFnames[i]);
  816. // skip filesize if directory
  817. if (fsLdirFsizes[i] >= 0)
  818. {
  819. // append with spaces until a width of 16 is reached
  820. word fnameLen = strlen(fsLdirFnames[i]);
  821. while (fnameLen < 16)
  822. {
  823. strcat(b, " ");
  824. fnameLen++;
  825. }
  826. itoa(fsLdirFsizes[i], fsizeBuf);
  827. strcat(b, fsizeBuf);
  828. }
  829. strcat(b, "\n");
  830. }
  831. }
  832. return FS_ANSW_USB_INT_SUCCESS;
  833. }
  834. // Returns FS_ANSW_USB_INT_SUCCESS on successful change of dir
  835. // will return error FS_ANSW_ERR_FILE_CLOSE if dir is a file
  836. word FS_changeDir(char* f)
  837. {
  838. // special case for root, since FS_open() returns as if it opened a file
  839. if (f[0] == '/' && f[1] == 0)
  840. {
  841. FS_sendSinglePath("/");
  842. FS_open();
  843. return FS_ANSW_USB_INT_SUCCESS;
  844. }
  845. word retval = FS_sendFullPath(f);
  846. // return on failure
  847. if (retval != FS_ANSW_USB_INT_SUCCESS)
  848. {
  849. return retval;
  850. }
  851. retval = FS_open();
  852. // return sucess on open dir
  853. if (retval == FS_ANSW_ERR_OPEN_DIR)
  854. {
  855. return FS_ANSW_USB_INT_SUCCESS;
  856. }
  857. // close and return on opening file
  858. if (retval == FS_ANSW_USB_INT_SUCCESS)
  859. {
  860. FS_close();
  861. return FS_ANSW_ERR_FILE_CLOSE;
  862. }
  863. else // otherwise return error code
  864. {
  865. return retval;
  866. }
  867. }
  868. // Goes up a directory by removing the last directory from the global path
  869. // does nothing if already at root
  870. // assumes no trailing / in path
  871. void FS_goUpDirectory()
  872. {
  873. // do nothing if at root
  874. if (SHELL_path[0] == '/' && SHELL_path[1] == 0)
  875. {
  876. return;
  877. }
  878. word i = 0;
  879. word lastSlash = 0;
  880. // loop through path
  881. while (SHELL_path[i] != 0)
  882. {
  883. // save location of lastest /
  884. if (SHELL_path[i] == '/')
  885. {
  886. lastSlash = i;
  887. }
  888. i++;
  889. }
  890. // set location of last slash to end of string
  891. SHELL_path[lastSlash] = 0;
  892. // rix removal of root directory /
  893. if (SHELL_path[0] != '/')
  894. {
  895. SHELL_path[0] = '/';
  896. SHELL_path[1] = 0; // terminate string
  897. }
  898. }
  899. // Appends relative path f to current path
  900. // reverts when invalid resulting path
  901. // returns FS_ANSW_USB_INT_SUCCESS on success
  902. word FS_setRelativePath(char* f)
  903. {
  904. // get length of currentPath
  905. word currentPathLength = 0;
  906. while (SHELL_path[currentPathLength] != 0)
  907. {
  908. currentPathLength++;
  909. }
  910. // append current path with /
  911. // only when not at root
  912. if (currentPathLength == 1)
  913. {
  914. currentPathLength -= 1;
  915. }
  916. else
  917. {
  918. SHELL_path[currentPathLength] = '/';
  919. }
  920. // append relative path
  921. word currentPathIndex = currentPathLength + 1;
  922. word relativePathIndex = 0;
  923. while (f[relativePathIndex] != 0)
  924. {
  925. SHELL_path[currentPathIndex] = f[relativePathIndex];
  926. currentPathIndex++;
  927. relativePathIndex++;
  928. }
  929. // terminate new current path
  930. SHELL_path[currentPathIndex] = 0;
  931. // test new path and revert if failure
  932. word retval = FS_changeDir(SHELL_path);
  933. if (retval != FS_ANSW_USB_INT_SUCCESS)
  934. {
  935. // in case of being at root path
  936. if (currentPathLength == 0)
  937. {
  938. SHELL_path[0] = '/';
  939. SHELL_path[1] = 0;
  940. }
  941. else
  942. {
  943. SHELL_path[currentPathLength] = 0; // set terminator back to original place
  944. }
  945. }
  946. return retval;
  947. }
  948. // Changes current path based on f
  949. // if f is relative, then the path is added
  950. // if f is absolute, then the path will be replaced by f
  951. // if f is "..", then the last directory will be removed from f
  952. // if f is empty, then nothing will be done and FS_ANSW_USB_INT_SUCCESS is returned
  953. // TODO: remove trailing / if any
  954. // TODO: make sure that path will be < max_pathlength characters
  955. // returns FS_ANSW_USB_INT_SUCCESS for valid paths
  956. word FS_changePath(char* f)
  957. {
  958. if (f[0] == 0)
  959. {
  960. return FS_ANSW_USB_INT_SUCCESS;
  961. }
  962. // if f == "..", go up a directory
  963. if (f[0] == '.' && f[1] == '.' && f[2] == 0)
  964. {
  965. FS_goUpDirectory();
  966. return FS_ANSW_USB_INT_SUCCESS;
  967. }
  968. // if absolute path
  969. else if (f[0] == '/')
  970. {
  971. // if absolute path is valid
  972. word retval = FS_changeDir(f);
  973. if (retval == FS_ANSW_USB_INT_SUCCESS)
  974. {
  975. // copy f to SHELL_path
  976. strcpy(SHELL_path, f);
  977. }
  978. return retval;
  979. }
  980. // if relative path
  981. else
  982. {
  983. return FS_setRelativePath(f);
  984. }
  985. }
  986. // Calculates full path given arg f.
  987. // f can be relative or absolute.
  988. // full path is written to SHELL_path
  989. // no checks are done to verify if the path is valid
  990. // so backup of SHELL_path is advised
  991. void FS_getFullPath(char* f)
  992. {
  993. if (f[0] == 0)
  994. {
  995. return;
  996. }
  997. // absolutee path
  998. if (f[0] == '/')
  999. {
  1000. strcpy(SHELL_path, f); // copy f to SHELL_path
  1001. }
  1002. else
  1003. {
  1004. strcat(SHELL_path, "/"); // append / to SHELL_path
  1005. strcat(SHELL_path, f); // append f to SHELL_path
  1006. }
  1007. }