fs.c 29 KB

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