1
0

fs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. * Filesystem library
  3. * Handles all filesystem related work for userBDOS programs 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. * Uses blocking delays for now.
  7. */
  8. // uses stdlib.c
  9. #define FS_INTERRUPT_ADDR 0xC0272D
  10. //CH376 Codes
  11. #define FS_CMD_GET_IC_VER 0x01
  12. #define FS_CMD_SET_BAUDRATE 0x02
  13. #define FS_CMD_ENTER_SLEEP 0x03
  14. #define FS_CMD_SET_USB_SPEED 0x04
  15. #define FS_CMD_RESET_ALL 0x05
  16. #define FS_CMD_CHECK_EXIST 0x06
  17. #define FS_CMD_SET_SD0_INT 0x0b
  18. #define FS_CMD_SET_RETRY 0x0b
  19. #define FS_CMD_GET_FILE_SIZE 0x0c
  20. #define FS_CMD_SET_FILE_SIZE 0x0d
  21. #define FS_CMD_SET_USB_ADDRESS 0x13
  22. #define FS_CMD_SET_USB_MODE 0x15
  23. #define FS_MODE_HOST_0 0x05
  24. #define FS_MODE_HOST_1 0x07
  25. #define FS_MODE_HOST_2 0x06
  26. #define FS_CMD_GET_STATUS 0x22
  27. #define FS_CMD_RD_USB_DATA0 0x27
  28. #define FS_CMD_WR_USB_DATA 0x2c
  29. #define FS_CMD_WR_REQ_DATA 0x2d
  30. #define FS_CMD_WR_OFS_DATA 0x2e
  31. #define FS_CMD_SET_FILE_NAME 0x2f
  32. #define FS_CMD_DISK_CONNECT 0x30
  33. #define FS_CMD_DISK_MOUNT 0x31
  34. #define FS_CMD_FILE_OPEN 0x32
  35. #define FS_CMD_FILE_ENUM_GO 0x33
  36. #define FS_CMD_FILE_CREATE 0x34
  37. #define FS_CMD_FILE_ERASE 0x35
  38. #define FS_CMD_FILE_CLOSE 0x36
  39. #define FS_CMD_DIR_INFO_READ 0x37
  40. #define FS_CMD_DIR_INFO_SAVE 0x38
  41. #define FS_CMD_BYTE_LOCATE 0x39
  42. #define FS_CMD_BYTE_READ 0x3a
  43. #define FS_CMD_BYTE_RD_GO 0x3b
  44. #define FS_CMD_BYTE_WRITE 0x3c
  45. #define FS_CMD_BYTE_WR_GO 0x3d
  46. #define FS_CMD_DISK_CAPACITY 0x3e
  47. #define FS_CMD_DISK_QUERY 0x3f
  48. #define FS_CMD_DIR_CREATE 0x40
  49. #define FS_CMD_SET_ADDRESS 0x45
  50. #define FS_CMD_GET_DESCR 0x46
  51. #define FS_CMD_SET_CONFIG 0x49
  52. #define FS_CMD_AUTO_CONFIG 0x4D
  53. #define FS_CMD_ISSUE_TKN_X 0x4E
  54. #define FS_ANSW_RET_SUCCESS 0x51
  55. #define FS_ANSW_USB_INT_SUCCESS 0x14
  56. #define FS_ANSW_USB_INT_CONNECT 0x15
  57. #define FS_ANSW_USB_INT_DISCONNECT 0x16
  58. #define FS_ANSW_USB_INT_USB_READY 0x18
  59. #define FS_ANSW_USB_INT_DISK_READ 0x1d
  60. #define FS_ANSW_USB_INT_DISK_WRITE 0x1e
  61. #define FS_ANSW_RET_ABORT 0x5F
  62. #define FS_ANSW_USB_INT_DISK_ERR 0x1f
  63. #define FS_ANSW_USB_INT_BUF_OVER 0x17
  64. #define FS_ANSW_ERR_OPEN_DIR 0x41
  65. #define FS_ANSW_ERR_MISS_FILE 0x42
  66. #define FS_ANSW_ERR_FOUND_NAME 0x43
  67. #define FS_ANSW_ERR_DISK_DISCON 0x82
  68. #define FS_ANSW_ERR_LARGE_SECTOR 0x84
  69. #define FS_ANSW_ERR_TYPE_ERROR 0x92
  70. #define FS_ANSW_ERR_BPB_ERROR 0xa1
  71. #define FS_ANSW_ERR_DISK_FULL 0xb1
  72. #define FS_ANSW_ERR_FDT_OVER 0xb2
  73. #define FS_ANSW_ERR_FILE_CLOSE 0xb4
  74. #define FS_ERR_LONGFILENAME 0x01
  75. #define FS_ATTR_READ_ONLY 0x01
  76. #define FS_ATTR_HIDDEN 0x02
  77. #define FS_ATTR_SYSTEM 0x04
  78. #define FS_ATTR_VOLUME_ID 0x08
  79. #define FS_ATTR_DIRECTORY 0x10
  80. #define FS_ATTR_ARCHIVE 0x20
  81. // Sets SPI1_CS low
  82. void FS_spiBeginTransfer()
  83. {
  84. asm(
  85. "; backup regs\n"
  86. "push r1\n"
  87. "push r2\n"
  88. "load32 0xC0272C r2 ; r2 = 0xC0272C\n"
  89. "load 0 r1 ; r1 = 0 (enable)\n"
  90. "write 0 r2 r1 ; write to SPI1_CS\n"
  91. "; restore regs\n"
  92. "pop r2\n"
  93. "pop r1\n"
  94. );
  95. }
  96. // Sets SPI1_CS high
  97. void FS_spiEndTransfer()
  98. {
  99. asm(
  100. "; backup regs\n"
  101. "push r1\n"
  102. "push r2\n"
  103. "load32 0xC0272C r2 ; r2 = 0xC0272C\n"
  104. "load 1 r1 ; r1 = 1 (disable)\n"
  105. "write 0 r2 r1 ; write to SPI1_CS\n"
  106. "; restore regs\n"
  107. "pop r2\n"
  108. "pop r1\n"
  109. );
  110. }
  111. // write dataByte and return read value
  112. // write 0x00 for a read
  113. // Writes byte over SPI1 to CH376
  114. word FS_spiTransfer(word dataByte)
  115. {
  116. word retval = 0;
  117. asm(
  118. "load32 0xC0272B r2 ; r2 = 0xC0272B\n"
  119. "write 0 r2 r4 ; write r4 over SPI1\n"
  120. "read 0 r2 r2 ; read return value\n"
  121. "write -4 r14 r2 ; write to stack to return\n"
  122. );
  123. return retval;
  124. }
  125. // Get status after waiting for an interrupt
  126. word FS_WaitGetStatus()
  127. {
  128. word intValue = 1;
  129. while(intValue)
  130. {
  131. word *i = (word *) FS_INTERRUPT_ADDR;
  132. intValue = *i;
  133. }
  134. FS_spiBeginTransfer();
  135. FS_spiTransfer(FS_CMD_GET_STATUS);
  136. FS_spiEndTransfer();
  137. //delay(1);
  138. FS_spiBeginTransfer();
  139. word retval = FS_spiTransfer(0x00);
  140. FS_spiEndTransfer();
  141. return retval;
  142. }
  143. // Function to send a string (without terminating 0)
  144. void FS_sendString(char* str)
  145. {
  146. char chr = *str; // first character of str
  147. while (chr != 0) // continue until null value
  148. {
  149. FS_spiTransfer(chr);
  150. str++; // go to next character address
  151. chr = *str; // get character from address
  152. }
  153. }
  154. // Function to send data d of size s
  155. void FS_sendData(char* d, word s)
  156. {
  157. char chr = *d; // first byte of data
  158. word i;
  159. for(i = 0; (unsigned int) i < (unsigned int)s; i++) // write s bytes
  160. {
  161. FS_spiTransfer(chr);
  162. d++; // go to next data address
  163. chr = *d; // get data from address
  164. }
  165. }
  166. // Returns file size of currently opened file (32 bits)
  167. word FS_getFileSize()
  168. {
  169. FS_spiBeginTransfer();
  170. FS_spiTransfer(FS_CMD_GET_FILE_SIZE);
  171. FS_spiTransfer(0x68);
  172. word retval = FS_spiTransfer(0);
  173. retval = retval + (FS_spiTransfer(0) << 8);
  174. retval = retval + (FS_spiTransfer(0) << 16);
  175. retval = retval + (FS_spiTransfer(0) << 24);
  176. FS_spiEndTransfer();
  177. return retval;
  178. }
  179. // Sets cursor to position s
  180. // Returns FS_ANSW_USB_INT_SUCCESS on success
  181. word FS_setCursor(word s)
  182. {
  183. FS_spiBeginTransfer();
  184. FS_spiTransfer(FS_CMD_BYTE_LOCATE);
  185. FS_spiTransfer(s);
  186. FS_spiTransfer(s >> 8);
  187. FS_spiTransfer(s >> 16);
  188. FS_spiTransfer(s >> 24);
  189. FS_spiEndTransfer();
  190. return FS_WaitGetStatus();
  191. }
  192. // Reads s bytes into buf
  193. // If bytesToWord is true, four bytes will be stored in one address/word
  194. // Can read 65536 bytes per call
  195. // Returns FS_ANSW_USB_INT_SUCCESS on success
  196. // TODO: this surely can be optimized for speed in some way!
  197. word FS_readFile(char* buf, word s, word bytesToWord)
  198. {
  199. if (s == 0)
  200. return FS_ANSW_USB_INT_SUCCESS;
  201. FS_spiBeginTransfer();
  202. FS_spiTransfer(FS_CMD_BYTE_READ);
  203. FS_spiTransfer(s);
  204. FS_spiTransfer(s >> 8);
  205. FS_spiEndTransfer();
  206. word retval = FS_WaitGetStatus();
  207. // Return on error or EOF
  208. if (retval == FS_ANSW_USB_INT_SUCCESS) // eof
  209. return 0;
  210. if (retval != FS_ANSW_USB_INT_DISK_READ)
  211. return retval;
  212. word bytesRead = 0;
  213. word wordsRead = 0;
  214. word doneReading = 0;
  215. // clear first address
  216. buf[wordsRead] = 0;
  217. // used for shifting bytes to word
  218. word currentByteShift = 24;
  219. while (doneReading == 0)
  220. {
  221. // Read set of bytes (max 255)
  222. FS_spiBeginTransfer();
  223. FS_spiTransfer(FS_CMD_RD_USB_DATA0);
  224. word readLen = FS_spiTransfer(0x00);
  225. word readByte;
  226. word i;
  227. for (i = 0; i < readLen; i++)
  228. {
  229. readByte = FS_spiTransfer(0x00);
  230. // Read 4 bytes into one word, from left to right
  231. if (bytesToWord)
  232. {
  233. readByte = readByte << currentByteShift;
  234. buf[wordsRead] = buf[wordsRead] + readByte;
  235. if (currentByteShift == 0)
  236. {
  237. currentByteShift = 24;
  238. wordsRead++;
  239. buf[wordsRead] = 0;
  240. }
  241. else
  242. {
  243. currentByteShift -= 8;
  244. }
  245. }
  246. else
  247. {
  248. buf[bytesRead] = (char)readByte;
  249. bytesRead = bytesRead + 1;
  250. }
  251. }
  252. FS_spiEndTransfer();
  253. // requesting another set of data
  254. FS_spiBeginTransfer();
  255. FS_spiTransfer(FS_CMD_BYTE_RD_GO);
  256. FS_spiEndTransfer();
  257. retval = FS_WaitGetStatus();
  258. if (retval == FS_ANSW_USB_INT_SUCCESS)
  259. doneReading = 1;
  260. else if (retval == FS_ANSW_USB_INT_DISK_READ)
  261. {
  262. // read another block
  263. }
  264. else
  265. {
  266. uprintln("E: Error while reading data");
  267. return retval;
  268. }
  269. }
  270. return retval;
  271. }
  272. // Writes data d of size s
  273. // Can only write 65536 bytes at a time
  274. // returns FS_ANSW_USB_INT_SUCCESS on success
  275. // TODO: optimize for speed
  276. word FS_writeFile(char* d, word s)
  277. {
  278. if (s == 0)
  279. return FS_ANSW_USB_INT_SUCCESS;
  280. FS_spiBeginTransfer();
  281. FS_spiTransfer(FS_CMD_BYTE_WRITE);
  282. FS_spiTransfer(s);
  283. FS_spiTransfer(s >> 8);
  284. FS_spiEndTransfer();
  285. word retval = FS_WaitGetStatus();
  286. // Return on error
  287. if (retval != FS_ANSW_USB_INT_DISK_WRITE)
  288. return retval;
  289. word bytesWritten = 0;
  290. word doneWriting = 0;
  291. while (doneWriting == 0)
  292. {
  293. // Write set of bytes (max 255)
  294. FS_spiBeginTransfer();
  295. FS_spiTransfer(FS_CMD_WR_REQ_DATA);
  296. word wrLen = FS_spiTransfer(0x00);
  297. FS_sendData(d + bytesWritten, wrLen);
  298. bytesWritten = bytesWritten + wrLen;
  299. FS_spiEndTransfer();
  300. // update file size
  301. FS_spiBeginTransfer();
  302. FS_spiTransfer(FS_CMD_BYTE_WR_GO);
  303. FS_spiEndTransfer();
  304. retval = FS_WaitGetStatus();
  305. if (retval == FS_ANSW_USB_INT_SUCCESS)
  306. doneWriting = 1;
  307. else if (retval == FS_ANSW_USB_INT_DISK_WRITE)
  308. {
  309. // write another block
  310. }
  311. else
  312. {
  313. uprintln("E: Error while writing data");
  314. return retval;
  315. }
  316. }
  317. return retval;
  318. }
  319. // returns status of opening a file/directory
  320. // Usually the successful status codes are:
  321. // FS_ANSW_USB_INT_SUCCESS || FS_ANSW_ERR_OPEN_DIR || FS_ANSW_USB_INT_DISK_READ
  322. word FS_open()
  323. {
  324. FS_spiBeginTransfer();
  325. FS_spiTransfer(FS_CMD_FILE_OPEN);
  326. FS_spiEndTransfer();
  327. return FS_WaitGetStatus();
  328. }
  329. // returns FS_ANSW_USB_INT_SUCCESS on success
  330. word FS_close()
  331. {
  332. FS_spiBeginTransfer();
  333. FS_spiTransfer(FS_CMD_FILE_CLOSE);
  334. FS_spiTransfer(0x01); //0x01 if update filesize, else 0x00
  335. FS_spiEndTransfer();
  336. return FS_WaitGetStatus();
  337. }
  338. // Creates file (recreates if exists)
  339. // New files are 1 byte long
  340. // Have not found a way to set it to 0 bytes,
  341. // since FS_CMD_SET_FILE_SIZE does not work
  342. // Automatically closes file
  343. // returns FS_ANSW_USB_INT_SUCCESS on success
  344. word FS_createFile()
  345. {
  346. FS_spiBeginTransfer();
  347. FS_spiTransfer(FS_CMD_FILE_CREATE);
  348. FS_spiEndTransfer();
  349. word retval = FS_WaitGetStatus();
  350. // Return on error
  351. if (retval != FS_ANSW_USB_INT_SUCCESS)
  352. return retval;
  353. // open and close file
  354. FS_open();
  355. FS_close();
  356. return FS_ANSW_USB_INT_SUCCESS;
  357. }
  358. // Sends single path f
  359. // No processing of f is done, it is directly sent
  360. // This means no error checking on file length!
  361. void FS_sendSinglePath(char* f)
  362. {
  363. // send buf
  364. FS_spiBeginTransfer();
  365. FS_spiTransfer(FS_CMD_SET_FILE_NAME);
  366. FS_sendString(f); // send file name
  367. FS_spiTransfer(0); // close with null
  368. FS_spiEndTransfer();
  369. }
  370. // Sends path f
  371. // Can be relative or absolute file or directory
  372. // REQUIRES CAPITAL LETTERS and must conform the 8.3 filename standard
  373. // Function can handle folders with forward slashes
  374. // returns FS_ANSW_USB_INT_SUCCESS on success
  375. word FS_sendFullPath(char* f)
  376. {
  377. // path to uppercase and replace backslash by slash
  378. word i = 0;
  379. while (f[i] != 0)
  380. {
  381. f[i] = toUpper(f[i]);
  382. if (f[i] == '\\')
  383. f[i] = '/';
  384. i++;
  385. }
  386. word removedSlash = 0; // to restore removed slash
  387. // remove (single) trailing slash if exists
  388. // we reuse i here since it points to the end of the path
  389. if (i > 1) // ignore the root path
  390. {
  391. if (f[i-1] == '/')
  392. {
  393. f[i-1] = 0;
  394. removedSlash = 1;
  395. }
  396. }
  397. // if first char is a /, then we go back to root
  398. if (f[0] == '/')
  399. {
  400. FS_sendSinglePath("/");
  401. FS_open();
  402. }
  403. i = 0;
  404. char buf[16]; // buffer for single folder/file name
  405. word bufi = 0;
  406. while (f[i] != 0)
  407. {
  408. // handle all directories
  409. if (f[i] == 47) // forward slash
  410. {
  411. // return error on opening folders containing a single or double dot
  412. if ((bufi == 1 && buf[0] == '.') || (bufi == 2 && buf[0] == '.' && buf[1] == '.'))
  413. return FS_ANSW_ERR_OPEN_DIR;
  414. // add null to end of buf
  415. buf[bufi] = 0;
  416. // send buf
  417. FS_spiBeginTransfer();
  418. FS_spiTransfer(FS_CMD_SET_FILE_NAME);
  419. FS_sendString(buf); // send folder name
  420. FS_spiTransfer(0); // close with null
  421. FS_spiEndTransfer();
  422. // reset bufi
  423. bufi = 0;
  424. // open folder
  425. word retval = FS_open();
  426. // Return on if failure / folder not found
  427. if (retval != FS_ANSW_USB_INT_SUCCESS && retval != FS_ANSW_ERR_OPEN_DIR)
  428. return retval;
  429. }
  430. else
  431. {
  432. buf[bufi] = f[i];
  433. bufi++;
  434. if (bufi > 13)
  435. return FS_ERR_LONGFILENAME; // exit if folder/file name is too long
  436. }
  437. i++;
  438. }
  439. if (removedSlash) // restore removed slash if there
  440. {
  441. f[i] = '/';
  442. f[i+1] = 0;
  443. }
  444. // handle filename itself (if any)
  445. // add null to end of buf
  446. buf[bufi] = 0;
  447. if (bufi == 0)
  448. return FS_ANSW_USB_INT_SUCCESS; // exit if there is no filename after the folder
  449. // return error on opening folders containing a single or double dot
  450. if ((bufi == 1 && buf[0] == '.') || (bufi == 2 && buf[0] == '.' && buf[1] == '.'))
  451. return FS_ANSW_ERR_OPEN_DIR;
  452. // send buf (last part of string)
  453. FS_spiBeginTransfer();
  454. FS_spiTransfer(FS_CMD_SET_FILE_NAME);
  455. FS_sendString(buf); // send file name
  456. FS_spiTransfer(0); // close with null
  457. FS_spiEndTransfer();
  458. return FS_ANSW_USB_INT_SUCCESS;
  459. }