filetypes.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. {
  33. DIR dir;
  34. FRESULT res;
  35. FILINFO fno;
  36. fno.lfname = NULL;
  37. res = f_opendir( &dir, ( TCHAR * )path );
  38. uint16_t numentries = 0;
  39. if ( res == FR_OK )
  40. {
  41. for ( ;; )
  42. {
  43. res = f_readdir( &dir, &fno );
  44. if ( res != FR_OK || fno.fname[0] == 0 )
  45. {
  46. break;
  47. }
  48. numentries++;
  49. }
  50. }
  51. return numentries;
  52. }
  53. uint32_t scan_dir( char *path, FILINFO *fno_param, char mkdb, uint32_t this_dir_tgt )
  54. {
  55. DIR dir;
  56. FILINFO fno;
  57. FRESULT res;
  58. uint8_t len;
  59. TCHAR *fn;
  60. static unsigned char depth = 0;
  61. static uint32_t crc, fncrc;
  62. static uint32_t db_tgt;
  63. static uint32_t next_subdir_tgt;
  64. static uint32_t parent_tgt;
  65. static uint32_t dir_end = 0;
  66. static uint8_t was_empty = 0;
  67. static uint16_t num_files_total = 0;
  68. static uint16_t num_dirs_total = 0;
  69. uint32_t dir_tgt;
  70. uint32_t switched_dir_tgt = 0;
  71. uint16_t numentries;
  72. uint32_t dirsize;
  73. uint8_t pass = 0;
  74. char buf[7];
  75. char *size_units[3] = {" ", "k", "M"};
  76. uint32_t entry_fsize;
  77. uint8_t entry_unit_idx;
  78. uint16_t entrycnt;
  79. dir_tgt = this_dir_tgt;
  80. if ( depth == 0 )
  81. {
  82. crc = 0;
  83. db_tgt = SRAM_DB_ADDR + 0x10;
  84. dir_tgt = SRAM_DIR_ADDR;
  85. next_subdir_tgt = SRAM_DIR_ADDR;
  86. this_dir_tgt = SRAM_DIR_ADDR;
  87. parent_tgt = 0;
  88. printf( "root dir @%lx\n", dir_tgt );
  89. }
  90. fno.lfsize = 255;
  91. fno.lfname = ( TCHAR * )file_lfn;
  92. numentries = 0;
  93. for ( pass = 0; pass < ( mkdb ? 2 : 1 ); pass++ )
  94. {
  95. if ( pass )
  96. {
  97. dirsize = 4 * ( numentries );
  98. if ( ( ( next_subdir_tgt + dirsize + 8 ) & 0xff0000 ) > ( next_subdir_tgt & 0xff0000 ) )
  99. {
  100. printf( "switchdir! old=%lX ", next_subdir_tgt + dirsize + 4 );
  101. next_subdir_tgt &= 0xffff0000;
  102. next_subdir_tgt += 0x00010004;
  103. printf( "new=%lx\n", next_subdir_tgt );
  104. dir_tgt &= 0xffff0000;
  105. dir_tgt += 0x00010004;
  106. }
  107. switched_dir_tgt = dir_tgt;
  108. next_subdir_tgt += dirsize + 4;
  109. if ( parent_tgt )
  110. {
  111. next_subdir_tgt += 4;
  112. }
  113. if ( next_subdir_tgt > dir_end )
  114. {
  115. dir_end = next_subdir_tgt;
  116. }
  117. DBG_FS printf( "path=%s depth=%d ptr=%lx entries=%d parent=%lx next subdir @%lx\n", path, depth, db_tgt, numentries,
  118. parent_tgt, next_subdir_tgt );
  119. if ( mkdb )
  120. {
  121. num_dirs_total++;
  122. // printf("d=%d Saving %lx to Address %lx [end]\n", depth, 0L, next_subdir_tgt - 4);
  123. sram_writelong( 0L, next_subdir_tgt - 4 );
  124. }
  125. }
  126. if ( fno_param )
  127. {
  128. res = dir_open_by_filinfo( &dir, fno_param );
  129. }
  130. else
  131. {
  132. res = f_opendir( &dir, path );
  133. }
  134. if ( res == FR_OK )
  135. {
  136. if ( pass && parent_tgt && mkdb )
  137. {
  138. /* write backlink to parent dir
  139. switch to next bank if record does not fit in current bank */
  140. if ( ( db_tgt & 0xffff ) > ( ( 0x10000 - ( sizeof( next_subdir_tgt ) + sizeof( len ) + 4 ) ) & 0xffff ) )
  141. {
  142. printf( "switch! old=%lx ", db_tgt );
  143. db_tgt &= 0xffff0000;
  144. db_tgt += 0x00010000;
  145. printf( "new=%lx\n", db_tgt );
  146. }
  147. // printf("writing link to parent, %lx to address %lx [../]\n", parent_tgt-SRAM_MENU_ADDR, db_tgt);
  148. sram_writelong( ( parent_tgt - SRAM_MENU_ADDR ), db_tgt );
  149. sram_writebyte( 0, db_tgt + sizeof( next_subdir_tgt ) );
  150. sram_writeblock( "../\0", db_tgt + sizeof( next_subdir_tgt ) + sizeof( len ), 4 );
  151. sram_writelong( ( db_tgt - SRAM_MENU_ADDR ) | ( ( uint32_t )0x81 << 24 ), dir_tgt );
  152. db_tgt += sizeof( next_subdir_tgt ) + sizeof( len ) + 4;
  153. dir_tgt += 4;
  154. }
  155. len = strlen( ( char * )path );
  156. /* scan at most DIR_FILE_MAX entries per directory */
  157. for ( entrycnt = 0; entrycnt < DIR_FILE_MAX; entrycnt++ )
  158. {
  159. // toggle_read_led();
  160. res = f_readdir( &dir, &fno );
  161. if ( res != FR_OK || fno.fname[0] == 0 )
  162. {
  163. if ( pass )
  164. {
  165. /* if(!numentries) was_empty=1;*/
  166. }
  167. break;
  168. }
  169. fn = *fno.lfname ? fno.lfname : fno.fname;
  170. if ( ( *fn == '.' ) || !( strncasecmp( fn, SYS_DIR_NAME, sizeof( SYS_DIR_NAME ) ) ) )
  171. {
  172. continue;
  173. }
  174. if ( fno.fattrib & AM_DIR )
  175. {
  176. depth++;
  177. if ( depth < FS_MAX_DEPTH )
  178. {
  179. numentries++;
  180. if ( pass && mkdb )
  181. {
  182. path[len] = '/';
  183. strncpy( path + len + 1, ( char * )fn, sizeof( fs_path ) - len );
  184. uint16_t pathlen = 0;
  185. uint32_t old_db_tgt = 0;
  186. if ( mkdb )
  187. {
  188. pathlen = strlen( path );
  189. DBG_FS printf( "d=%d Saving %lx to Address %lx [dir]\n", depth, db_tgt, dir_tgt );
  190. /* save element:
  191. - path name
  192. - pointer to sub dir structure */
  193. if ( ( db_tgt & 0xffff ) > ( ( 0x10000 - ( sizeof( next_subdir_tgt ) + sizeof( len ) + pathlen + 2 ) ) & 0xffff ) )
  194. {
  195. printf( "switch! old=%lx ", db_tgt );
  196. db_tgt &= 0xffff0000;
  197. db_tgt += 0x00010000;
  198. printf( "new=%lx\n", db_tgt );
  199. }
  200. /* write element pointer to current dir structure */
  201. sram_writelong( ( db_tgt - SRAM_MENU_ADDR ) | ( ( uint32_t )0x80 << 24 ), dir_tgt );
  202. /* save element:
  203. - path name
  204. - pointer to sub dir structure
  205. moved below */
  206. old_db_tgt = db_tgt;
  207. db_tgt += sizeof( next_subdir_tgt ) + sizeof( len ) + pathlen + 2;
  208. }
  209. parent_tgt = this_dir_tgt;
  210. /* scan subdir before writing current dir element to account for bank switches */
  211. uint32_t corrected_subdir_tgt = scan_dir( path, &fno, mkdb, next_subdir_tgt );
  212. if ( mkdb )
  213. {
  214. DBG_FS printf( " Saving dir descriptor to %lx tgt=%lx, path=%s\n", old_db_tgt, corrected_subdir_tgt, path );
  215. sram_writelong( ( corrected_subdir_tgt - SRAM_MENU_ADDR ), old_db_tgt );
  216. sram_writebyte( len + 1, old_db_tgt + sizeof( next_subdir_tgt ) );
  217. sram_writeblock( path, old_db_tgt + sizeof( next_subdir_tgt ) + sizeof( len ), pathlen );
  218. sram_writeblock( "/\0", old_db_tgt + sizeof( next_subdir_tgt ) + sizeof( len ) + pathlen, 2 );
  219. }
  220. dir_tgt += 4;
  221. /* was_empty = 0;*/
  222. }
  223. else if ( !mkdb )
  224. {
  225. path[len] = '/';
  226. strncpy( path + len + 1, ( char * )fn, sizeof( fs_path ) - len );
  227. scan_dir( path, &fno, mkdb, next_subdir_tgt );
  228. }
  229. }
  230. depth--;
  231. path[len] = 0;
  232. }
  233. else
  234. {
  235. SNES_FTYPE type = determine_filetype( ( char * )fn );
  236. if ( type != TYPE_UNKNOWN )
  237. {
  238. numentries++;
  239. if ( pass )
  240. {
  241. if ( mkdb )
  242. {
  243. num_files_total++;
  244. /* snes_romprops_t romprops; */
  245. path[len] = '/';
  246. strncpy( path + len + 1, ( char * )fn, sizeof( fs_path ) - len );
  247. uint16_t pathlen = strlen( path );
  248. switch ( type )
  249. {
  250. case TYPE_IPS:
  251. case TYPE_SMC:
  252. case TYPE_SPC:
  253. /* write element pointer to current dir structure */
  254. DBG_FS printf( "d=%d Saving %lX to Address %lX [file %s]\n", depth, db_tgt, dir_tgt, path );
  255. if ( ( db_tgt & 0xffff ) > ( ( 0x10000 - ( sizeof( len ) + pathlen + sizeof( buf ) - 1 + 1 ) ) & 0xffff ) )
  256. {
  257. printf( "switch! old=%lx ", db_tgt );
  258. db_tgt &= 0xffff0000;
  259. db_tgt += 0x00010000;
  260. printf( "new=%lx\n", db_tgt );
  261. }
  262. sram_writelong( ( db_tgt - SRAM_MENU_ADDR ) | ( ( uint32_t )type << 24 ), dir_tgt );
  263. dir_tgt += 4;
  264. /* save element:
  265. - index of last slash character
  266. - file name
  267. - file size */
  268. /* sram_writeblock((uint8_t*)&romprops, db_tgt, sizeof(romprops)); */
  269. entry_fsize = fno.fsize;
  270. entry_unit_idx = 0;
  271. while ( entry_fsize > 9999 )
  272. {
  273. entry_fsize >>= 10;
  274. entry_unit_idx++;
  275. }
  276. snprintf( buf, sizeof( buf ), "% 5ld", entry_fsize );
  277. strncat( buf, size_units[entry_unit_idx], 1 );
  278. sram_writeblock( buf, db_tgt, sizeof( buf ) - 1 );
  279. sram_writebyte( len + 1, db_tgt + sizeof( buf ) - 1 );
  280. sram_writeblock( path, db_tgt + sizeof( len ) + sizeof( buf ) - 1, pathlen + 1 );
  281. // sram_writelong(fno.fsize, db_tgt + sizeof(len) + pathlen + 1);
  282. db_tgt += sizeof( len ) + pathlen + sizeof( buf ) - 1 + 1;
  283. break;
  284. case TYPE_UNKNOWN:
  285. default:
  286. break;
  287. }
  288. path[len] = 0;
  289. /* printf("%s ", path);
  290. _delay_ms(30); */
  291. }
  292. }
  293. else
  294. {
  295. TCHAR *fn2 = fn;
  296. fncrc = 0;
  297. while ( *fn2 != 0 )
  298. {
  299. fncrc += crc_xmodem_update( fncrc, *( ( unsigned char * )fn2++ ) );
  300. }
  301. crc += fncrc;
  302. }
  303. }
  304. }
  305. }
  306. }
  307. else
  308. {
  309. uart_putc( 0x30 + res );
  310. }
  311. }
  312. DBG_FS printf( "db_tgt=%lx dir_end=%lx\n", db_tgt, dir_end );
  313. sram_writelong( db_tgt, SRAM_DB_ADDR + 4 );
  314. sram_writelong( dir_end, SRAM_DB_ADDR + 8 );
  315. sram_writeshort( num_files_total, SRAM_DB_ADDR + 12 );
  316. sram_writeshort( num_dirs_total, SRAM_DB_ADDR + 14 );
  317. if ( depth == 0 )
  318. {
  319. return crc;
  320. }
  321. else
  322. {
  323. return switched_dir_tgt;
  324. }
  325. return was_empty; // tricky!
  326. }
  327. SNES_FTYPE determine_filetype( char *filename )
  328. {
  329. char *ext = strrchr( filename, '.' );
  330. if ( ext == NULL )
  331. {
  332. return TYPE_UNKNOWN;
  333. }
  334. if ( ( !strcasecmp( ext + 1, "SMC" ) )
  335. || ( !strcasecmp( ext + 1, "SFC" ) )
  336. || ( !strcasecmp( ext + 1, "FIG" ) )
  337. || ( !strcasecmp( ext + 1, "BS" ) )
  338. )
  339. {
  340. return TYPE_SMC;
  341. }
  342. /* if( (!strcasecmp(ext+1, "IPS"))
  343. ||(!strcasecmp(ext+1, "UPS"))
  344. ) {
  345. return TYPE_IPS;
  346. }*/
  347. if ( !strcasecmp( ext + 1, "SPC" ) )
  348. {
  349. return TYPE_SPC;
  350. }
  351. return TYPE_UNKNOWN;
  352. }
  353. FRESULT get_db_id( uint32_t *id )
  354. {
  355. file_open( ( uint8_t * )"/sd2snes/sd2snes.db", FA_READ );
  356. if ( file_res == FR_OK )
  357. {
  358. file_readblock( id, 0, 4 );
  359. /* XXX */// *id=0xdead;
  360. file_close();
  361. }
  362. else
  363. {
  364. *id = 0xdeadbeef;
  365. }
  366. return file_res;
  367. }
  368. int get_num_dirent( uint32_t addr )
  369. {
  370. int result = 0;
  371. while ( sram_readlong( addr + result * 4 ) )
  372. {
  373. result++;
  374. }
  375. return result;
  376. }
  377. void sort_all_dir( uint32_t endaddr )
  378. {
  379. uint32_t entries = 0;
  380. uint32_t current_base = SRAM_DIR_ADDR;
  381. while ( current_base < ( endaddr ) )
  382. {
  383. while ( sram_readlong( current_base + entries * 4 ) )
  384. {
  385. entries++;
  386. }
  387. printf( "sorting dir @%lx, entries: %ld\n", current_base, entries );
  388. sort_dir( current_base, entries );
  389. current_base += 4 * entries + 4;
  390. entries = 0;
  391. }
  392. }