filetypes.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /* sd2snes - SD card based universal cartridge for the SNES
  2. Copyright (C) 2009-2010 Maximilian Rehkopf <otakon@gmx.net>
  3. AVR firmware portion
  4. Inspired by and based on code from sd2iec, written by Ingo Korb et al.
  5. See sdcard.c|h, config.h.
  6. FAT file system access based on code by ChaN, Jim Brain, Ingo Korb,
  7. see ff.c|h.
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; version 2 of the License only.
  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. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. filetypes.c: directory scanning and file type detection
  19. */
  20. #include <string.h>
  21. #include "config.h"
  22. #include "uart.h"
  23. #include "filetypes.h"
  24. #include "ff.h"
  25. #include "smc.h"
  26. #include "fileops.h"
  27. #include "crc.h"
  28. #include "memory.h"
  29. #include "led.h"
  30. #include "sort.h"
  31. uint16_t scan_flat(const char* path) {
  32. DIR dir;
  33. FRESULT res;
  34. FILINFO fno;
  35. fno.lfname = NULL;
  36. res = f_opendir(&dir, (TCHAR*)path);
  37. uint16_t numentries = 0;
  38. if (res == FR_OK) {
  39. for (;;) {
  40. res = f_readdir(&dir, &fno);
  41. if(res != FR_OK || fno.fname[0] == 0)break;
  42. numentries++;
  43. }
  44. }
  45. return numentries;
  46. }
  47. uint32_t scan_dir(char* path, FILINFO* fno_param, char mkdb, uint32_t this_dir_tgt) {
  48. DIR dir;
  49. FILINFO fno;
  50. FRESULT res;
  51. uint8_t len;
  52. TCHAR* fn;
  53. static unsigned char depth = 0;
  54. static uint32_t crc, fncrc;
  55. static uint32_t db_tgt;
  56. static uint32_t next_subdir_tgt;
  57. static uint32_t parent_tgt;
  58. static uint32_t dir_end = 0;
  59. /* static uint8_t was_empty = 0;*/
  60. static uint16_t num_files_total = 0;
  61. static uint16_t num_dirs_total = 0;
  62. uint32_t dir_tgt;
  63. uint32_t switched_dir_tgt = 0;
  64. uint16_t numentries;
  65. uint32_t dirsize;
  66. uint8_t pass = 0;
  67. char buf[7];
  68. char *size_units[3] = {" ", "k", "M"};
  69. uint32_t entry_fsize;
  70. uint8_t entry_unit_idx;
  71. uint16_t entrycnt;
  72. dir_tgt = this_dir_tgt;
  73. if(depth==0) {
  74. crc = 0;
  75. db_tgt = SRAM_DB_ADDR+0x10;
  76. dir_tgt = SRAM_DIR_ADDR;
  77. next_subdir_tgt = SRAM_DIR_ADDR;
  78. this_dir_tgt = SRAM_DIR_ADDR;
  79. parent_tgt = 0;
  80. printf("root dir @%lx\n", dir_tgt);
  81. }
  82. fno.lfsize = 255;
  83. fno.lfname = (TCHAR*)file_lfn;
  84. numentries=0;
  85. for(pass = 0; pass < (mkdb ? 2 : 1); pass++) {
  86. if(pass) {
  87. dirsize = 4*(numentries);
  88. if(((next_subdir_tgt + dirsize + 8) & 0xff0000) > (next_subdir_tgt & 0xff0000)) {
  89. printf("switchdir! old=%lX ", next_subdir_tgt + dirsize + 4);
  90. next_subdir_tgt &= 0xffff0000;
  91. next_subdir_tgt += 0x00010004;
  92. printf("new=%lx\n", next_subdir_tgt);
  93. dir_tgt &= 0xffff0000;
  94. dir_tgt += 0x00010004;
  95. }
  96. switched_dir_tgt = dir_tgt;
  97. next_subdir_tgt += dirsize + 4;
  98. if(parent_tgt) next_subdir_tgt += 4;
  99. if(next_subdir_tgt > dir_end) {
  100. dir_end = next_subdir_tgt;
  101. }
  102. DBG_FS printf("path=%s depth=%d ptr=%lx entries=%d parent=%lx next subdir @%lx\n", path, depth, db_tgt, numentries, parent_tgt, next_subdir_tgt);
  103. if(mkdb) {
  104. num_dirs_total++;
  105. // printf("d=%d Saving %lx to Address %lx [end]\n", depth, 0L, next_subdir_tgt - 4);
  106. sram_writelong(0L, next_subdir_tgt - 4);
  107. }
  108. }
  109. if(fno_param) {
  110. res = dir_open_by_filinfo(&dir, fno_param);
  111. } else {
  112. res = f_opendir(&dir, path);
  113. }
  114. if (res == FR_OK) {
  115. if(pass && parent_tgt && mkdb) {
  116. /* write backlink to parent dir
  117. switch to next bank if record does not fit in current bank */
  118. if((db_tgt&0xffff) > ((0x10000-(sizeof(next_subdir_tgt)+sizeof(len)+4))&0xffff)) {
  119. printf("switch! old=%lx ", db_tgt);
  120. db_tgt &= 0xffff0000;
  121. db_tgt += 0x00010000;
  122. printf("new=%lx\n", db_tgt);
  123. }
  124. // printf("writing link to parent, %lx to address %lx [../]\n", parent_tgt-SRAM_MENU_ADDR, db_tgt);
  125. sram_writelong((parent_tgt-SRAM_MENU_ADDR), db_tgt);
  126. sram_writebyte(0, db_tgt+sizeof(next_subdir_tgt));
  127. sram_writeblock("../\0", db_tgt+sizeof(next_subdir_tgt)+sizeof(len), 4);
  128. sram_writelong((db_tgt-SRAM_MENU_ADDR)|((uint32_t)0x81<<24), dir_tgt);
  129. db_tgt += sizeof(next_subdir_tgt)+sizeof(len)+4;
  130. dir_tgt += 4;
  131. }
  132. len = strlen((char*)path);
  133. /* scan at most DIR_FILE_MAX entries per directory */
  134. for(entrycnt=0; entrycnt < DIR_FILE_MAX; entrycnt++) {
  135. // toggle_read_led();
  136. res = f_readdir(&dir, &fno);
  137. if (res != FR_OK || fno.fname[0] == 0) {
  138. if(pass) {
  139. /* if(!numentries) was_empty=1;*/
  140. }
  141. break;
  142. }
  143. fn = *fno.lfname ? fno.lfname : fno.fname;
  144. if ((*fn == '.') || !(strncasecmp(fn, SYS_DIR_NAME, sizeof(SYS_DIR_NAME)))) continue;
  145. if (fno.fattrib & AM_DIR) {
  146. depth++;
  147. if(depth < FS_MAX_DEPTH) {
  148. numentries++;
  149. if(pass && mkdb) {
  150. path[len]='/';
  151. strncpy(path+len+1, (char*)fn, sizeof(fs_path)-len);
  152. uint16_t pathlen = 0;
  153. uint32_t old_db_tgt = 0;
  154. if(mkdb) {
  155. pathlen = strlen(path);
  156. DBG_FS printf("d=%d Saving %lx to Address %lx [dir]\n", depth, db_tgt, dir_tgt);
  157. /* save element:
  158. - path name
  159. - pointer to sub dir structure */
  160. if((db_tgt&0xffff) > ((0x10000-(sizeof(next_subdir_tgt) + sizeof(len) + pathlen + 2))&0xffff)) {
  161. printf("switch! old=%lx ", db_tgt);
  162. db_tgt &= 0xffff0000;
  163. db_tgt += 0x00010000;
  164. printf("new=%lx\n", db_tgt);
  165. }
  166. /* write element pointer to current dir structure */
  167. sram_writelong((db_tgt-SRAM_MENU_ADDR)|((uint32_t)0x80<<24), dir_tgt);
  168. /* save element:
  169. - path name
  170. - pointer to sub dir structure
  171. moved below */
  172. old_db_tgt = db_tgt;
  173. db_tgt += sizeof(next_subdir_tgt) + sizeof(len) + pathlen + 2;
  174. }
  175. parent_tgt = this_dir_tgt;
  176. /* scan subdir before writing current dir element to account for bank switches */
  177. uint32_t corrected_subdir_tgt = scan_dir(path, &fno, mkdb, next_subdir_tgt);
  178. if(mkdb) {
  179. DBG_FS printf(" Saving dir descriptor to %lx tgt=%lx, path=%s\n", old_db_tgt, corrected_subdir_tgt, path);
  180. sram_writelong((corrected_subdir_tgt-SRAM_MENU_ADDR), old_db_tgt);
  181. sram_writebyte(len+1, old_db_tgt+sizeof(next_subdir_tgt));
  182. sram_writeblock(path, old_db_tgt+sizeof(next_subdir_tgt)+sizeof(len), pathlen);
  183. sram_writeblock("/\0", old_db_tgt + sizeof(next_subdir_tgt) + sizeof(len) + pathlen, 2);
  184. }
  185. dir_tgt += 4;
  186. /* was_empty = 0;*/
  187. } else if(!mkdb) {
  188. path[len]='/';
  189. strncpy(path+len+1, (char*)fn, sizeof(fs_path)-len);
  190. scan_dir(path, &fno, mkdb, next_subdir_tgt);
  191. }
  192. }
  193. depth--;
  194. path[len]=0;
  195. } else {
  196. SNES_FTYPE type = determine_filetype((char*)fn);
  197. if(type != TYPE_UNKNOWN) {
  198. numentries++;
  199. if(pass) {
  200. if(mkdb) {
  201. num_files_total++;
  202. /* snes_romprops_t romprops; */
  203. path[len]='/';
  204. strncpy(path+len+1, (char*)fn, sizeof(fs_path)-len);
  205. uint16_t pathlen = strlen(path);
  206. switch(type) {
  207. case TYPE_IPS:
  208. case TYPE_SMC:
  209. case TYPE_SPC:
  210. /* write element pointer to current dir structure */
  211. DBG_FS printf("d=%d Saving %lX to Address %lX [file %s]\n", depth, db_tgt, dir_tgt, path);
  212. if((db_tgt&0xffff) > ((0x10000-(sizeof(len) + pathlen + sizeof(buf)-1 + 1))&0xffff)) {
  213. printf("switch! old=%lx ", db_tgt);
  214. db_tgt &= 0xffff0000;
  215. db_tgt += 0x00010000;
  216. printf("new=%lx\n", db_tgt);
  217. }
  218. sram_writelong((db_tgt-SRAM_MENU_ADDR) | ((uint32_t)type << 24), dir_tgt);
  219. dir_tgt += 4;
  220. /* save element:
  221. - index of last slash character
  222. - file name
  223. - file size */
  224. /* sram_writeblock((uint8_t*)&romprops, db_tgt, sizeof(romprops)); */
  225. entry_fsize = fno.fsize;
  226. entry_unit_idx = 0;
  227. while(entry_fsize > 9999) {
  228. entry_fsize >>= 10;
  229. entry_unit_idx++;
  230. }
  231. snprintf(buf, sizeof(buf), "% 5ld", entry_fsize);
  232. strncat(buf, size_units[entry_unit_idx], 1);
  233. sram_writeblock(buf, db_tgt, sizeof(buf)-1);
  234. sram_writebyte(len+1, db_tgt + sizeof(buf)-1);
  235. sram_writeblock(path, db_tgt + sizeof(len) + sizeof(buf)-1, pathlen + 1);
  236. // sram_writelong(fno.fsize, db_tgt + sizeof(len) + pathlen + 1);
  237. db_tgt += sizeof(len) + pathlen + sizeof(buf)-1 + 1;
  238. break;
  239. case TYPE_UNKNOWN:
  240. default:
  241. break;
  242. }
  243. path[len] = 0;
  244. /* printf("%s ", path);
  245. _delay_ms(30); */
  246. }
  247. } else {
  248. TCHAR* fn2 = fn;
  249. fncrc = 0;
  250. while(*fn2 != 0) {
  251. fncrc += crc_xmodem_update(fncrc, *((unsigned char*)fn2++));
  252. }
  253. crc += fncrc;
  254. }
  255. }
  256. }
  257. }
  258. } else uart_putc(0x30+res);
  259. }
  260. DBG_FS printf("db_tgt=%lx dir_end=%lx\n", db_tgt, dir_end);
  261. sram_writelong(db_tgt, SRAM_DB_ADDR+4);
  262. sram_writelong(dir_end, SRAM_DB_ADDR+8);
  263. sram_writeshort(num_files_total, SRAM_DB_ADDR+12);
  264. sram_writeshort(num_dirs_total, SRAM_DB_ADDR+14);
  265. if(depth==0) return crc;
  266. else return switched_dir_tgt;
  267. return was_empty; // tricky!
  268. }
  269. SNES_FTYPE determine_filetype(char* filename) {
  270. char* ext = strrchr(filename, '.');
  271. if(ext == NULL)
  272. return TYPE_UNKNOWN;
  273. if( (!strcasecmp(ext+1, "SMC"))
  274. ||(!strcasecmp(ext+1, "SFC"))
  275. ||(!strcasecmp(ext+1, "FIG"))
  276. ||(!strcasecmp(ext+1, "BS"))
  277. ) {
  278. return TYPE_SMC;
  279. }
  280. /* if( (!strcasecmp(ext+1, "IPS"))
  281. ||(!strcasecmp(ext+1, "UPS"))
  282. ) {
  283. return TYPE_IPS;
  284. }*/
  285. if(!strcasecmp(ext+1, "SPC")) {
  286. return TYPE_SPC;
  287. }
  288. return TYPE_UNKNOWN;
  289. }
  290. FRESULT get_db_id(uint32_t* id) {
  291. file_open((uint8_t*)"/sd2snes/sd2snes.db", FA_READ);
  292. if(file_res == FR_OK) {
  293. file_readblock(id, 0, 4);
  294. /* XXX */// *id=0xdead;
  295. file_close();
  296. } else {
  297. *id=0xdeadbeef;
  298. }
  299. return file_res;
  300. }
  301. int get_num_dirent(uint32_t addr) {
  302. int result = 0;
  303. while(sram_readlong(addr+result*4)) {
  304. result++;
  305. }
  306. return result;
  307. }
  308. void sort_all_dir(uint32_t endaddr) {
  309. uint32_t entries = 0;
  310. uint32_t current_base = SRAM_DIR_ADDR;
  311. while(current_base<(endaddr)) {
  312. while(sram_readlong(current_base+entries*4)) {
  313. entries++;
  314. }
  315. printf("sorting dir @%lx, entries: %ld\n", current_base, entries);
  316. sort_dir(current_base, entries);
  317. current_base += 4*entries + 4;
  318. entries = 0;
  319. }
  320. }