1
0

brfs.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. // Bart's RAM File System (BRFS)
  2. /*
  3. Implementing in BDOS on PCB v3:
  4. - BDOS only uses ~340 pages of 256 bytes -> 90000 bytes
  5. - SPI flash has 65536 pages of 256 bytes -> 16777216 bytes
  6. - With current verilog implementation, only 32MiB of RAM is addressable, so BRFS should be used somewhere in the first 32MiB
  7. - With current BDOS memory map, BRFS should be placed in the first 8MiB available as BDOS Program Code
  8. - Lets use the last 4MiB of this space for BRFS (0x100000 - 0x200000)
  9. */
  10. #define BRFS_SUPPORTED_VERSION 1
  11. #define BRFS_RAM_STORAGE_ADDR 0x100000 // From 4th MiB
  12. // Addresses in SPI Flash
  13. #define SPIFLASH_MEMMAP_ADDR 0x800000
  14. // Note that each section should be in a different 4KiB sector in SPI Flash
  15. #define BRFS_SPIFLASH_SUPERBLOCK_ADDR 0xDF000 // One sector before FAT
  16. #define BRFS_SPIFLASH_FAT_ADDR 0xE0000 // Can be 32768 words (128KiB) for 32MiB of 256word blocks
  17. #define BRFS_SPIFLASH_BLOCK_ADDR 0x100000 // From first MiB
  18. //#define MAX_PATH_LENGTH 127 // Set by BDOS
  19. #define MAX_OPEN_FILES 16 // Can be set higher, but 4 is good for testing
  20. // Length of structs, should not be changed
  21. #define SUPERBLOCK_SIZE 16
  22. #define DIR_ENTRY_SIZE 8
  23. #define BRFS_MAX_BLOCKS 65536 // 64KiB
  24. word brfs_changed_blocks[BRFS_MAX_BLOCKS >> 5]; // Bitmap of changed blocks, each block has 1 bit
  25. // 16 words long
  26. struct brfs_superblock
  27. {
  28. word total_blocks;
  29. word words_per_block;
  30. word label[10]; // 1 char per word
  31. word brfs_version;
  32. word reserved[3];
  33. };
  34. // 8 words long
  35. struct brfs_dir_entry
  36. {
  37. word filename[4]; // 4 chars per word
  38. word modify_date; // TBD when RTC added to FPGC
  39. word flags; // 32 flags, from right to left: directory, hidden
  40. word fat_idx; // idx of first FAT block
  41. word filesize; // file size in words, not bytes
  42. };
  43. word *brfs_ram_storage = (word*) BRFS_RAM_STORAGE_ADDR; // RAM storage of file system
  44. // Variables for open files
  45. word brfs_cursors[MAX_OPEN_FILES]; // Cursor position offset from start of file
  46. word brfs_file_pointers[MAX_OPEN_FILES]; // FAT idx of open file
  47. struct brfs_dir_entry* brfs_dir_entry_pointers[MAX_OPEN_FILES]; // Pointer to dir entry of open file
  48. /**
  49. * Create a hexdump like dump of a section of memory
  50. * addr: address of the section
  51. * len: length of the section in words
  52. * linesize: number of words per line to print
  53. */
  54. void brfs_dump_section(word* addr, word len, word linesize)
  55. {
  56. char buf[16];
  57. word i;
  58. for (i = 0; i < len; i++)
  59. {
  60. itoah(addr[i], buf);
  61. if (strlen(buf+2) == 1)
  62. uprintc('0');
  63. uprint(buf+2);
  64. uprintc(' ');
  65. // newline every linesize words
  66. // also print last linesize words as chars if alphanum
  67. if (i != 0 && MATH_modU(i+1, linesize) == 0)
  68. {
  69. uprint(" ");
  70. word j;
  71. for (j = i - (linesize-1); j < i+1; j++)
  72. {
  73. if (isalnum(addr[j]) || addr[j] == ' ')
  74. uprintc(addr[j]);
  75. else
  76. uprintc('.');
  77. }
  78. uprintc('\n');
  79. }
  80. }
  81. }
  82. /**
  83. * Create a raw filesystem dump over UART
  84. * fatsize: size of the FAT table in words
  85. * datasize: size of the data section in words
  86. */
  87. void brfs_dump(word fatsize, word datasize)
  88. {
  89. // Superblock dump
  90. uprintln("Superblock:");
  91. brfs_dump_section(brfs_ram_storage, SUPERBLOCK_SIZE, 16);
  92. // FAT dump
  93. uprintln("\nFAT:");
  94. brfs_dump_section(brfs_ram_storage+SUPERBLOCK_SIZE, fatsize, 16);
  95. // Datablock dump
  96. uprintln("\nData:");
  97. brfs_dump_section(brfs_ram_storage+SUPERBLOCK_SIZE+fatsize, datasize, 32);
  98. uprintln("\nOpen files:");
  99. word i;
  100. for (i = 0; i < MAX_OPEN_FILES; i++)
  101. {
  102. uprint("FP");
  103. uprintDec(i+1);
  104. uprint(":");
  105. uprint(" FAT idx: ");
  106. uprintDec(brfs_file_pointers[i]);
  107. uprint(" Cursor: ");
  108. uprintDec(brfs_cursors[i]);
  109. uprint(" Size: ");
  110. uprintDec(brfs_dir_entry_pointers[i] ? brfs_dir_entry_pointers[i]->filesize : 0);
  111. uprintc('\n');
  112. }
  113. }
  114. /**
  115. * Return the FAT index of a directory, or -1 if not found
  116. * dir_path: full path of the directory
  117. */
  118. word brfs_get_fat_idx_of_dir(char* dir_path)
  119. {
  120. // Check length of path
  121. if (strlen(dir_path) > MAX_PATH_LENGTH)
  122. {
  123. uprintln("Path too long!");
  124. return -1;
  125. }
  126. // Start with root directory
  127. word current_dir_fat_idx = 0;
  128. // Check if root directory is requested
  129. if (strcmp(dir_path, "/") == 0)
  130. {
  131. return current_dir_fat_idx;
  132. }
  133. // Copy dir_path, size + 1 for null terminator
  134. // Since strtok modifies the string
  135. char dir_path_copy[MAX_PATH_LENGTH+1];
  136. strcpy(dir_path_copy, dir_path);
  137. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  138. word* brfs_data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  139. word dir_entries_max = superblock->words_per_block / sizeof(struct brfs_dir_entry);
  140. // Split path by '/' and traverse directories
  141. char* token = strtok(dir_path_copy, "/");
  142. while (token != (word*)-1)
  143. {
  144. // Find token in current directory
  145. word* dir_addr = brfs_data_block_addr + (current_dir_fat_idx * superblock->words_per_block);
  146. word found_dir = 0; // Keep track if token is found in current directory
  147. word i;
  148. for (i = 0; i < dir_entries_max; i++)
  149. {
  150. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  151. if (dir_entry->filename[0] != 0)
  152. {
  153. char decompressed_filename[17];
  154. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  155. // Also check for directory flag
  156. if (strcmp(decompressed_filename, token) == 0 && dir_entry->flags == 1)
  157. {
  158. // Found token in current directory
  159. // Set current directory to token's FAT index
  160. current_dir_fat_idx = dir_entry->fat_idx;
  161. found_dir = 1;
  162. break;
  163. }
  164. }
  165. }
  166. // If token not found in current directory, return -1
  167. if (!found_dir)
  168. {
  169. uprint("Directory ");
  170. uprint(dir_path);
  171. uprintln(" not found!");
  172. return -1;
  173. }
  174. token = strtok((word*)-1, "/");
  175. }
  176. return current_dir_fat_idx;
  177. }
  178. /**
  179. * Given the address of the FAT table and the number of blocks, find the next free block
  180. * Returns -1 if no free block is found
  181. * fat_addr: address of the FAT table
  182. * blocks: number of blocks in the FAT table
  183. */
  184. word brfs_find_next_free_block(word* fat_addr, word blocks)
  185. {
  186. word i = 0;
  187. word* fat_ptr = fat_addr;
  188. while (i < blocks)
  189. {
  190. if (*fat_ptr == 0)
  191. {
  192. return i;
  193. }
  194. fat_ptr++;
  195. i++;
  196. }
  197. return -1;
  198. }
  199. /**
  200. * Given the address of a directory data block and the maximum number of entries, find the next free directory entry
  201. * Returns -1 if no free entry is found
  202. * dir_addr: address of the directory data block (not the FAT idx)
  203. * dir_entries_max: maximum number of entries in the directory
  204. */
  205. word brfs_find_next_free_dir_entry(word* dir_addr, word dir_entries_max)
  206. {
  207. word i = 0;
  208. word* dir_ptr = dir_addr;
  209. while (i < dir_entries_max)
  210. {
  211. if (*dir_ptr == 0)
  212. {
  213. return i;
  214. }
  215. dir_ptr += sizeof(struct brfs_dir_entry);
  216. i++;
  217. }
  218. return -1;
  219. }
  220. /**
  221. * Create a single directory entry
  222. * dir_entry: pointer to the directory entry to be created
  223. * filename: name of the file, max 16 chars and uncompressed
  224. * fat_idx: index of the first FAT block of the file/directory
  225. * filesize: size of the file in words
  226. * flags: flags of the file/directory
  227. */
  228. void brfs_create_single_dir_entry(struct brfs_dir_entry* dir_entry, char* filename, word fat_idx, word filesize, word flags)
  229. {
  230. // Initialize to 0
  231. memset((char*)dir_entry, 0, sizeof(*dir_entry));
  232. // Set filename
  233. char compressed_filename[4] = {0,0,0,0};
  234. strcompress(compressed_filename, filename);
  235. memcpy((char*)&(dir_entry->filename), compressed_filename, sizeof(compressed_filename));
  236. // Set other fields
  237. dir_entry->fat_idx = fat_idx;
  238. dir_entry->flags = flags;
  239. dir_entry->filesize = filesize;
  240. }
  241. /**
  242. * Initialize a directory with . and .. entries
  243. * dir_addr: address of the directory data block
  244. * dir_entries_max: maximum number of entries in the directory
  245. * dir_fat_idx: index of the FAT block of the directory
  246. * parent_fat_idx: index of the FAT block of the parent directory
  247. */
  248. void brfs_init_directory(word* dir_addr, word dir_entries_max, word dir_fat_idx, word parent_fat_idx)
  249. {
  250. // Get block size from superblock
  251. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  252. word block_size = superblock->words_per_block;
  253. // Set data block of dir_fat_idx to 0
  254. memset(dir_addr, 0, block_size);
  255. // Create . entry
  256. struct brfs_dir_entry dir_entry;
  257. brfs_create_single_dir_entry(&dir_entry, ".", dir_fat_idx, dir_entries_max*sizeof(struct brfs_dir_entry), 1);
  258. // Copy to first data entry
  259. memcpy(dir_addr, (char*)&dir_entry, sizeof(dir_entry));
  260. // Create .. entry
  261. brfs_create_single_dir_entry(&dir_entry, "..", parent_fat_idx, dir_entries_max*sizeof(struct brfs_dir_entry), 1);
  262. // Copy to second data entry
  263. memcpy(dir_addr+sizeof(dir_entry), (char*)&dir_entry, sizeof(dir_entry));
  264. // Set FAT table
  265. brfs_ram_storage[SUPERBLOCK_SIZE + dir_fat_idx] = -1;
  266. // Set changed block
  267. brfs_changed_blocks[dir_fat_idx >> 5] |= (1 << (dir_fat_idx & 31));
  268. }
  269. /**
  270. * Format the ram storage as a BRFS filesystem
  271. * Also writes the superblock to SPI Flash
  272. * blocks: number of blocks in the filesystem
  273. * words_per_block: number of bytes per block
  274. * label: label of the filesystem
  275. * full_format: if 1, initialize data section to 0
  276. */
  277. void brfs_format(word blocks, word words_per_block, char* label, word full_format)
  278. {
  279. // Create a superblock
  280. struct brfs_superblock superblock;
  281. // Initialize to 0
  282. memset((char*)&superblock, 0, sizeof(superblock));
  283. // Set values of superblock
  284. superblock.total_blocks = blocks;
  285. superblock.words_per_block = words_per_block;
  286. strcpy((char*)&superblock.label, label);
  287. superblock.brfs_version = BRFS_SUPPORTED_VERSION;
  288. // Copy superblock to head of ram addr
  289. memcpy(brfs_ram_storage, (char*)&superblock, sizeof(superblock));
  290. // Create FAT
  291. memset(brfs_ram_storage + SUPERBLOCK_SIZE, 0, blocks);
  292. // Create Data section
  293. if (full_format)
  294. {
  295. memset(brfs_ram_storage + SUPERBLOCK_SIZE + blocks, 0, blocks * words_per_block);
  296. }
  297. // Initialize root dir
  298. word dir_entries_max = words_per_block / sizeof(struct brfs_dir_entry);
  299. brfs_init_directory(brfs_ram_storage + SUPERBLOCK_SIZE + blocks, dir_entries_max, 0, 0);
  300. // Clear open files and cursors
  301. memset(brfs_file_pointers, 0, sizeof(brfs_file_pointers));
  302. memset(brfs_cursors, 0, sizeof(brfs_cursors));
  303. // Set all dir entry pointers to 0
  304. word i;
  305. for (i = 0; i < MAX_OPEN_FILES; i++)
  306. {
  307. brfs_dir_entry_pointers[i] = 0;
  308. }
  309. // For all blocks that have just been formatted, set changed block
  310. word j;
  311. for (j = 0; j < blocks; j++)
  312. {
  313. brfs_changed_blocks[j >> 5] |= (1 << (j & 31));
  314. }
  315. // Write superblock to SPI Flash
  316. spiflash_sector_erase(BRFS_SPIFLASH_SUPERBLOCK_ADDR);
  317. spiflash_write_page_in_words((char*)&superblock, BRFS_SPIFLASH_SUPERBLOCK_ADDR, sizeof(superblock));
  318. }
  319. /**
  320. * Create a new directory in the directory of parent_dir_path
  321. * Returns 1 on success, 0 on error
  322. * parent_dir_path: full path of the parent directory
  323. * dirname: name of the new directory
  324. */
  325. word brfs_create_directory(char* parent_dir_path, char* dirname)
  326. {
  327. // Check length of dirname
  328. if (strlen(dirname) >= 16)
  329. {
  330. uprintln("Directory name too long!");
  331. return 0;
  332. }
  333. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  334. word* brfs_data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  335. // Find first free FAT block
  336. word next_free_block = brfs_find_next_free_block(brfs_ram_storage + SUPERBLOCK_SIZE, superblock->total_blocks);
  337. if (next_free_block == -1)
  338. {
  339. uprintln("No free blocks left!");
  340. return 0;
  341. }
  342. // Find data block address of parent directory path
  343. word parent_dir_fat_idx = brfs_get_fat_idx_of_dir(parent_dir_path);
  344. if (parent_dir_fat_idx == -1)
  345. {
  346. uprint("Parent directory ");
  347. uprint(parent_dir_path);
  348. uprintln(" not found!");
  349. return 0;
  350. }
  351. // Check if file or folder already exists
  352. word* parent_dir_addr = brfs_data_block_addr + (parent_dir_fat_idx * superblock->words_per_block);
  353. word dir_entries_max = superblock->words_per_block / sizeof(struct brfs_dir_entry);
  354. word i;
  355. for (i = 0; i < dir_entries_max; i++)
  356. {
  357. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (parent_dir_addr + (i * sizeof(struct brfs_dir_entry)));
  358. if (dir_entry->filename[0] != 0)
  359. {
  360. char decompressed_filename[17];
  361. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  362. if (strcmp(decompressed_filename, dirname) == 0)
  363. {
  364. uprint(dirname);
  365. uprintln(" already exists!");
  366. return 0;
  367. }
  368. }
  369. }
  370. // Find first free dir entry
  371. word next_free_dir_entry = brfs_find_next_free_dir_entry(
  372. brfs_data_block_addr + (parent_dir_fat_idx * superblock->words_per_block),
  373. superblock->words_per_block / sizeof(struct brfs_dir_entry)
  374. );
  375. if (next_free_dir_entry == -1)
  376. {
  377. uprintln("No free dir entries left!");
  378. return 0;
  379. }
  380. // Create dir entry
  381. struct brfs_dir_entry new_entry;
  382. brfs_create_single_dir_entry(&new_entry, dirname, next_free_block, dir_entries_max*sizeof(struct brfs_dir_entry), 1);
  383. // Copy dir entry to first free dir entry
  384. memcpy(
  385. brfs_data_block_addr + (parent_dir_fat_idx * superblock->words_per_block) + (next_free_dir_entry * sizeof(struct brfs_dir_entry)),
  386. (char*)&new_entry,
  387. sizeof(new_entry)
  388. );
  389. // Initialize directory
  390. brfs_init_directory(
  391. brfs_data_block_addr + (next_free_block * superblock->words_per_block),
  392. dir_entries_max,
  393. next_free_block,
  394. parent_dir_fat_idx
  395. );
  396. // Update changed block
  397. brfs_changed_blocks[next_free_block >> 5] |= (1 << (next_free_block & 31));
  398. brfs_changed_blocks[parent_dir_fat_idx >> 5] |= (1 << (parent_dir_fat_idx & 31));
  399. return 1;
  400. }
  401. /**
  402. * Create a new file in the directory of parent_dir_path
  403. * Returns 1 on success, 0 on error
  404. * parent_dir_path: full path of the parent directory
  405. * filename: name of the new file
  406. */
  407. word brfs_create_file(char* parent_dir_path, char* filename)
  408. {
  409. // Check length of filename
  410. if (strlen(filename) >= 16)
  411. {
  412. uprintln("Filename too long!");
  413. return 0;
  414. }
  415. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  416. word* brfs_data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  417. // Find first free FAT block
  418. word next_free_block = brfs_find_next_free_block(brfs_ram_storage + SUPERBLOCK_SIZE, superblock->total_blocks);
  419. if (next_free_block == -1)
  420. {
  421. uprintln("No free blocks left!");
  422. return 0;
  423. }
  424. // Find data block address of parent directory path
  425. word parent_dir_fat_idx = brfs_get_fat_idx_of_dir(parent_dir_path);
  426. if (parent_dir_fat_idx == -1)
  427. {
  428. uprint("Parent directory ");
  429. uprint(parent_dir_path);
  430. uprintln(" not found!");
  431. return 0;
  432. }
  433. // Check if file or folder already exists
  434. word* parent_dir_addr = brfs_data_block_addr + (parent_dir_fat_idx * superblock->words_per_block);
  435. word dir_entries_max = superblock->words_per_block / sizeof(struct brfs_dir_entry);
  436. word i;
  437. for (i = 0; i < dir_entries_max; i++)
  438. {
  439. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (parent_dir_addr + (i * sizeof(struct brfs_dir_entry)));
  440. if (dir_entry->filename[0] != 0)
  441. {
  442. char decompressed_filename[17];
  443. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  444. if (strcmp(decompressed_filename, filename) == 0)
  445. {
  446. uprint(filename);
  447. uprintln(" already exists!");
  448. return 0;
  449. }
  450. }
  451. }
  452. // Find first free dir entry
  453. word next_free_dir_entry = brfs_find_next_free_dir_entry(
  454. brfs_data_block_addr + (parent_dir_fat_idx * superblock->words_per_block),
  455. superblock->words_per_block / sizeof(struct brfs_dir_entry)
  456. );
  457. if (next_free_dir_entry == -1)
  458. {
  459. uprintln("No free dir entries left!");
  460. return 0;
  461. }
  462. // Create file entry
  463. struct brfs_dir_entry new_entry;
  464. brfs_create_single_dir_entry(&new_entry, filename, next_free_block, 0, 0);
  465. // Copy dir entry to first free dir entry
  466. memcpy(
  467. brfs_data_block_addr + (parent_dir_fat_idx * superblock->words_per_block) + (next_free_dir_entry * sizeof(struct brfs_dir_entry)),
  468. (char*)&new_entry,
  469. sizeof(new_entry)
  470. );
  471. // Initialize file by setting data to 0
  472. memset(
  473. brfs_data_block_addr + (next_free_block * superblock->words_per_block),
  474. 0,
  475. superblock->words_per_block
  476. );
  477. // Update FAT
  478. brfs_ram_storage[SUPERBLOCK_SIZE + next_free_block] = -1;
  479. // Update changed block
  480. brfs_changed_blocks[next_free_block >> 5] |= (1 << (next_free_block & 31));
  481. brfs_changed_blocks[parent_dir_fat_idx >> 5] |= (1 << (parent_dir_fat_idx & 31));
  482. return 1;
  483. }
  484. /**
  485. * Reads all directory entries of a directory into a buffer
  486. * dir_path: full path of the directory
  487. * buffer: buffer to store the directory entries
  488. * Returns the number of entries read, or -1 on error
  489. */
  490. word brfs_read_directory(char* dir_path, struct brfs_dir_entry* buffer)
  491. {
  492. // Find data block address of parent directory path
  493. word dir_fat_idx = brfs_get_fat_idx_of_dir(dir_path);
  494. if (dir_fat_idx == -1)
  495. {
  496. uprint("Parent directory ");
  497. uprint(dir_path);
  498. uprintln(" not found!");
  499. return -1;
  500. }
  501. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  502. word* dir_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks + (dir_fat_idx * superblock->words_per_block);
  503. word dir_entries_max = superblock->words_per_block / sizeof(struct brfs_dir_entry);
  504. word entries_read = 0;
  505. word i;
  506. for (i = 0; i < dir_entries_max; i++)
  507. {
  508. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  509. if (dir_entry->filename[0] != 0)
  510. {
  511. memcpy(buffer, dir_entry, sizeof(struct brfs_dir_entry));
  512. buffer++;
  513. entries_read++;
  514. }
  515. }
  516. return entries_read;
  517. }
  518. /**
  519. * Open a file for reading and writing
  520. * Returns the file pointer (FAT idx of file), or -1 on error
  521. * file_path: full path of the file
  522. */
  523. word brfs_open_file(char* file_path)
  524. {
  525. // Split filename from path using basename and dirname
  526. char dirname_output[MAX_PATH_LENGTH];
  527. char* file_path_basename = basename(file_path);
  528. char* file_path_dirname = dirname(dirname_output, file_path);
  529. // Find data block address of parent directory path
  530. word dir_fat_idx = brfs_get_fat_idx_of_dir(file_path_dirname);
  531. if (dir_fat_idx == -1)
  532. {
  533. uprint("Parent directory ");
  534. uprint(file_path_dirname);
  535. uprintln(" not found!");
  536. return -1;
  537. }
  538. // Find file in directory
  539. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  540. word* dir_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks + (dir_fat_idx * superblock->words_per_block);
  541. word dir_entries_max = superblock->words_per_block / sizeof(struct brfs_dir_entry);
  542. word i;
  543. for (i = 0; i < dir_entries_max; i++)
  544. {
  545. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  546. if (dir_entry->filename[0] != 0)
  547. {
  548. char decompressed_filename[17];
  549. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  550. // Also check for directory flag to be 0
  551. if (strcmp(decompressed_filename, file_path_basename) == 0 && dir_entry->flags == 0)
  552. {
  553. // Found file
  554. // Check if file is already open
  555. word j;
  556. for (j = 0; j < MAX_OPEN_FILES; j++)
  557. {
  558. if (brfs_file_pointers[j] == dir_entry->fat_idx)
  559. {
  560. uprint("File ");
  561. uprint(file_path_basename);
  562. uprintln(" already open!");
  563. return -1;
  564. }
  565. }
  566. // Find first free file pointer
  567. word next_free_file_pointer = -1;
  568. for (j = 0; j < MAX_OPEN_FILES; j++)
  569. {
  570. if (brfs_file_pointers[j] == 0)
  571. {
  572. next_free_file_pointer = j;
  573. break;
  574. }
  575. }
  576. if (next_free_file_pointer == -1)
  577. {
  578. uprintln("All files already opened!");
  579. return -1;
  580. }
  581. // Open file
  582. brfs_file_pointers[next_free_file_pointer] = dir_entry->fat_idx;
  583. brfs_cursors[next_free_file_pointer] = 0;
  584. brfs_dir_entry_pointers[next_free_file_pointer] = dir_entry;
  585. return brfs_file_pointers[next_free_file_pointer];
  586. }
  587. }
  588. }
  589. uprint("File ");
  590. uprint(file_path_basename);
  591. uprintln(" not found!");
  592. return -1;
  593. }
  594. /**
  595. * Close an opened file
  596. * Returns 1 on success, 0 on error
  597. * file_pointer: file pointer returned by brfs_open_file
  598. */
  599. word brfs_close_file(word file_pointer)
  600. {
  601. // Find file pointer
  602. word i;
  603. for (i = 0; i < MAX_OPEN_FILES; i++)
  604. {
  605. if (brfs_file_pointers[i] == file_pointer)
  606. {
  607. // Close file
  608. brfs_file_pointers[i] = 0;
  609. brfs_cursors[i] = 0;
  610. brfs_dir_entry_pointers[i] = 0;
  611. return 1;
  612. }
  613. }
  614. uprintln("File not found!");
  615. return 0;
  616. }
  617. /**
  618. * Delete a file by removing all FAT blocks and the directory entry
  619. * Deletes a directory only if it is empty
  620. * Returns 1 on success, 0 on error
  621. * file_path: full path of the file
  622. */
  623. word brfs_delete(char* file_path)
  624. {
  625. // Split filename from path using basename and dirname
  626. char dirname_output[MAX_PATH_LENGTH];
  627. char* file_path_basename = basename(file_path);
  628. char* file_path_dirname = dirname(dirname_output, file_path);
  629. // Find data block address of parent directory path
  630. word dir_fat_idx = brfs_get_fat_idx_of_dir(file_path_dirname);
  631. if (dir_fat_idx == -1)
  632. {
  633. uprint("Parent directory ");
  634. uprint(file_path_dirname);
  635. uprintln(" not found!");
  636. return 0;
  637. }
  638. // Find file in directory
  639. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  640. word* dir_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks + (dir_fat_idx * superblock->words_per_block);
  641. word dir_entries_max = superblock->words_per_block / sizeof(struct brfs_dir_entry);
  642. word i;
  643. for (i = 0; i < dir_entries_max; i++)
  644. {
  645. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  646. if (dir_entry->filename[0] != 0)
  647. {
  648. char decompressed_filename[17];
  649. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  650. if (strcmp(decompressed_filename, file_path_basename) == 0)
  651. {
  652. if ((dir_entry->flags & 0x01) == 1)
  653. {
  654. // Check if directory is empty
  655. struct brfs_dir_entry buffer[128]; // 128 to be safe
  656. word num_entries = brfs_read_directory(file_path, buffer);
  657. if (num_entries > 2)
  658. {
  659. uprint("Directory ");
  660. uprint(file_path_basename);
  661. uprintln(" is not empty!");
  662. return 0;
  663. }
  664. }
  665. // Check if file is already open
  666. word j;
  667. for (j = 0; j < MAX_OPEN_FILES; j++)
  668. {
  669. if (brfs_file_pointers[j] == dir_entry->fat_idx)
  670. {
  671. uprint("File ");
  672. uprint(file_path_basename);
  673. uprintln(" is open!");
  674. return 0;
  675. }
  676. }
  677. // Delete fat blocks
  678. word current_fat_idx = dir_entry->fat_idx;
  679. word next_fat_idx;
  680. while (current_fat_idx != -1)
  681. {
  682. next_fat_idx = brfs_ram_storage[SUPERBLOCK_SIZE + current_fat_idx];
  683. brfs_ram_storage[SUPERBLOCK_SIZE + current_fat_idx] = 0;
  684. brfs_changed_blocks[current_fat_idx >> 5] |= (1 << (current_fat_idx & 31));
  685. current_fat_idx = next_fat_idx;
  686. }
  687. // Delete entry
  688. memset((char*)dir_entry, 0, sizeof(struct brfs_dir_entry));
  689. // Update changed block
  690. brfs_changed_blocks[dir_fat_idx >> 5] |= (1 << (dir_fat_idx & 31));
  691. return 1;
  692. }
  693. }
  694. }
  695. uprint("File ");
  696. uprint(file_path_basename);
  697. uprintln(" not found!");
  698. return 0;
  699. }
  700. /**
  701. * Set the cursor of an opened file
  702. * Returns 1 on success, 0 on error
  703. * file_pointer: file pointer returned by brfs_open_file
  704. * cursor: new cursor position in words
  705. */
  706. word brfs_set_cursor(word file_pointer, word cursor)
  707. {
  708. if (file_pointer == 0)
  709. {
  710. uprintln("File not open!");
  711. return 0;
  712. }
  713. // Find file pointer
  714. word i;
  715. for (i = 0; i < MAX_OPEN_FILES; i++)
  716. {
  717. if (brfs_file_pointers[i] == file_pointer)
  718. {
  719. // Set cursor
  720. if (cursor < 0 || cursor > brfs_dir_entry_pointers[i]->filesize)
  721. {
  722. cursor = brfs_dir_entry_pointers[i]->filesize;
  723. }
  724. brfs_cursors[i] = cursor;
  725. return 1;
  726. }
  727. }
  728. uprintln("File not found!");
  729. return 0;
  730. }
  731. /**
  732. * Get the cursor of an opened file
  733. * Returns the cursor position in words, or -1 on error
  734. * file_pointer: file pointer returned by brfs_open_file
  735. */
  736. word brfs_get_cursor(word file_pointer)
  737. {
  738. if (file_pointer == 0)
  739. {
  740. uprintln("File not open!");
  741. return -1;
  742. }
  743. // Find file pointer
  744. word i;
  745. for (i = 0; i < MAX_OPEN_FILES; i++)
  746. {
  747. if (brfs_file_pointers[i] == file_pointer)
  748. {
  749. // Get cursor
  750. return brfs_cursors[i];
  751. }
  752. }
  753. uprintln("File not found!");
  754. return -1;
  755. }
  756. /**
  757. * Get the FAT index of a file at the cursor
  758. * Returns the FAT index, or 0 on error
  759. * file_pointer: file pointer returned by brfs_open_file
  760. * cursor: cursor position of opened file
  761. */
  762. word brfs_get_fat_idx_at_cursor(word file_pointer, word cursor)
  763. {
  764. if (file_pointer == 0)
  765. {
  766. uprintln("File not open!");
  767. return 0;
  768. }
  769. // Get FAT index of file at cursor
  770. word current_fat_idx = file_pointer;
  771. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  772. // Loop through FAT until cursor is reached
  773. while (cursor >= superblock->words_per_block)
  774. {
  775. current_fat_idx = brfs_ram_storage[SUPERBLOCK_SIZE + current_fat_idx];
  776. if (current_fat_idx == -1)
  777. {
  778. uprintln("Cursor is out of bounds!");
  779. return 0;
  780. }
  781. cursor -= superblock->words_per_block;
  782. }
  783. return current_fat_idx;
  784. }
  785. /**
  786. * Read a file from the cursor position
  787. * Returns 1 on success, or 0 on error
  788. * file_pointer: file pointer returned by brfs_open_file
  789. * buffer: buffer to read the file into
  790. * length: number of words to read
  791. */
  792. word brfs_read(word file_pointer, word* buffer, word length)
  793. {
  794. if (file_pointer == 0)
  795. {
  796. uprintln("File not open!");
  797. return 0;
  798. }
  799. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  800. word* data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  801. // Find file pointer
  802. word i;
  803. for (i = 0; i < MAX_OPEN_FILES; i++)
  804. {
  805. if (brfs_file_pointers[i] == file_pointer)
  806. {
  807. if (length < 0)
  808. {
  809. uprintln("Length cannot be negative!");
  810. return 0;
  811. }
  812. // Trunctate length to file size - cursor
  813. if (length > brfs_dir_entry_pointers[i]->filesize - brfs_cursors[i])
  814. {
  815. length = brfs_dir_entry_pointers[i]->filesize - brfs_cursors[i];
  816. }
  817. // Get FAT index of file at cursor
  818. word current_fat_idx = brfs_get_fat_idx_at_cursor(file_pointer, brfs_cursors[i]);
  819. if (current_fat_idx == 0)
  820. {
  821. uprintln("Error getting FAT index at cursor!");
  822. return 0;
  823. }
  824. // Loop:
  825. // - calculate words until end of block (or up to length)
  826. // - read words until end of block (or up to length)
  827. // - decrease length by words read
  828. // - get next block from FAT
  829. // - repeat until length is 0
  830. while (length > 0)
  831. {
  832. word cursor_in_block = MATH_modU(brfs_cursors[i], superblock->words_per_block);
  833. word words_until_end_of_block = superblock->words_per_block - cursor_in_block;
  834. word words_to_read = words_until_end_of_block > length ? length : words_until_end_of_block;
  835. // Copy words to buffer
  836. memcpy(buffer, data_block_addr + (current_fat_idx * superblock->words_per_block) + cursor_in_block, words_to_read);
  837. // Update cursor and length
  838. brfs_cursors[i] += words_to_read;
  839. length -= words_to_read;
  840. buffer += words_to_read;
  841. // Get next block from FAT
  842. current_fat_idx = brfs_ram_storage[SUPERBLOCK_SIZE + current_fat_idx];
  843. if (current_fat_idx == -1 && length > 0)
  844. {
  845. uprintln("There is no next block in the file!");
  846. return 0;
  847. }
  848. }
  849. return 1;
  850. }
  851. }
  852. uprintln("File not found!");
  853. return 0;
  854. }
  855. /**
  856. * Write a file from the cursor position
  857. * Returns 1 on success, or 0 on error
  858. * file_pointer: file pointer returned by brfs_open_file
  859. * buffer: buffer to write to the file
  860. * length: number of words to write
  861. */
  862. word brfs_write(word file_pointer, word* buffer, word length)
  863. {
  864. if (file_pointer == 0)
  865. {
  866. uprintln("File not open!");
  867. return 0;
  868. }
  869. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  870. word* data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  871. // Find file pointer
  872. word i;
  873. for (i = 0; i < MAX_OPEN_FILES; i++)
  874. {
  875. if (brfs_file_pointers[i] == file_pointer)
  876. {
  877. if (length < 0)
  878. {
  879. uprintln("Length cannot be negative!");
  880. return 0;
  881. }
  882. // Get FAT index of file at cursor
  883. word current_fat_idx = brfs_get_fat_idx_at_cursor(file_pointer, brfs_cursors[i]);
  884. if (current_fat_idx == 0)
  885. {
  886. uprintln("Error getting FAT index at cursor!");
  887. return 0;
  888. }
  889. // Loop:
  890. // - calculate words until end of block (or up to length)
  891. // - write words until end of block (or up to length)
  892. // - decrease length by words written
  893. // - get next block from FAT, or find next free block if end of block
  894. // - if next block is needed, update FAT
  895. // - repeat until length is 0
  896. while (length > 0)
  897. {
  898. word cursor_in_block = MATH_modU(brfs_cursors[i], superblock->words_per_block);
  899. word words_until_end_of_block = superblock->words_per_block - cursor_in_block;
  900. word words_to_write = words_until_end_of_block > length ? length : words_until_end_of_block;
  901. // Copy words to buffer
  902. memcpy(data_block_addr + (current_fat_idx * superblock->words_per_block) + cursor_in_block, buffer, words_to_write);
  903. // Update changed block
  904. brfs_changed_blocks[current_fat_idx >> 5] |= (1 << (current_fat_idx & 31));
  905. // Update cursor and length
  906. brfs_cursors[i] += words_to_write;
  907. length -= words_to_write;
  908. buffer += words_to_write;
  909. // Get next block from FAT, or find next free block if end of block
  910. if (words_until_end_of_block == words_to_write)
  911. {
  912. word next_fat_idx = brfs_ram_storage[SUPERBLOCK_SIZE + current_fat_idx];
  913. // Check if next block is already allocated
  914. if (next_fat_idx != -1)
  915. {
  916. current_fat_idx = next_fat_idx;
  917. }
  918. else
  919. {
  920. // Find next free block
  921. word next_free_block = brfs_find_next_free_block(brfs_ram_storage + SUPERBLOCK_SIZE, superblock->total_blocks);
  922. if (next_free_block == -1)
  923. {
  924. uprintln("No free blocks left!");
  925. return 0;
  926. }
  927. // Update FAT
  928. brfs_ram_storage[SUPERBLOCK_SIZE + current_fat_idx] = next_free_block;
  929. // Go to next block
  930. current_fat_idx = next_free_block;
  931. // Set next block to -1 to indicate end of file
  932. brfs_ram_storage[SUPERBLOCK_SIZE + current_fat_idx] = -1;
  933. // Update changed block
  934. brfs_changed_blocks[current_fat_idx >> 5] |= (1 << (current_fat_idx & 31));
  935. }
  936. }
  937. }
  938. // Update file size in dir entry if we wrote past the current size
  939. if (brfs_cursors[i] > brfs_dir_entry_pointers[i]->filesize)
  940. {
  941. brfs_dir_entry_pointers[i]->filesize = brfs_cursors[i];
  942. }
  943. return 1;
  944. }
  945. }
  946. uprintln("File not found!");
  947. return 0;
  948. }
  949. /**
  950. * Stat a file or directory
  951. * Returns the directory entry, or -1 on error
  952. */
  953. struct brfs_dir_entry* brfs_stat(char* file_path)
  954. {
  955. // Remove all trailing slashes
  956. while (strlen(file_path) > 1 && file_path[strlen(file_path)-1] == '/')
  957. {
  958. file_path[strlen(file_path)-1] = 0;
  959. }
  960. // Split filename from path using basename and dirname
  961. char dirname_output[MAX_PATH_LENGTH];
  962. char* file_path_basename = basename(file_path);
  963. char* file_path_dirname = dirname(dirname_output, file_path);
  964. // Find data block address of parent directory path
  965. word dir_fat_idx = brfs_get_fat_idx_of_dir(file_path_dirname);
  966. if (dir_fat_idx == -1)
  967. {
  968. uprint("Parent directory ");
  969. uprint(file_path_dirname);
  970. uprintln(" not found!");
  971. return (struct brfs_dir_entry*)-1;
  972. }
  973. // Find file in directory
  974. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  975. word* dir_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks + (dir_fat_idx * superblock->words_per_block);
  976. word dir_entries_max = superblock->words_per_block / sizeof(struct brfs_dir_entry);
  977. word i;
  978. for (i = 0; i < dir_entries_max; i++)
  979. {
  980. struct brfs_dir_entry* dir_entry = (struct brfs_dir_entry*) (dir_addr + (i * sizeof(struct brfs_dir_entry)));
  981. if (dir_entry->filename[0] != 0)
  982. {
  983. char decompressed_filename[17];
  984. strdecompress(decompressed_filename, (char*)&(dir_entry->filename));
  985. // Also check for directory flag to be 0
  986. if (strcmp(decompressed_filename, file_path_basename) == 0)
  987. {
  988. return dir_entry;
  989. }
  990. }
  991. }
  992. uprint("File or directory ");
  993. uprint(file_path_basename);
  994. uprintln(" not found!");
  995. return (struct brfs_dir_entry*)-1;
  996. }
  997. /**
  998. * Write the FAT table to SPI flash by performing three steps:
  999. * 1. Check which FAT entries have changed
  1000. * 2. Erase the 4KiB sectors that contain these FAT entries
  1001. * 3. Write each changed FAT entry to flash by using 16 page writes per sector
  1002. */
  1003. void brfs_write_fat_to_flash()
  1004. {
  1005. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  1006. word* data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  1007. // 1 sector = 4KiB = 1024 words = 1024 FAT entries
  1008. // 1 word contains 32 flags for changed blocks/FAT entries
  1009. // 1024/32 = 32 words in the changed_blocks array per sector
  1010. uprintln("---Writing FAT to SPI Flash---");
  1011. // Loop over brfs_changed_blocks in 32 word parts
  1012. // Assumes length of brfs_changed_blocks is a multiple of 32
  1013. word i;
  1014. for (i = 0; i < sizeof(brfs_changed_blocks); i+=32)
  1015. {
  1016. // Check if any value within brfs_changed_blocks[i:i+32] is not 0
  1017. word j;
  1018. word changed = 0;
  1019. for (j = 0; j < 32; j++)
  1020. {
  1021. if (brfs_changed_blocks[i+j] != 0)
  1022. {
  1023. changed = 1;
  1024. break;
  1025. }
  1026. }
  1027. if (changed)
  1028. {
  1029. // Erase sector
  1030. word addr = BRFS_SPIFLASH_FAT_ADDR; // Workaround because of large static number
  1031. addr += (i >> 5) * 4096; // Sector idx * bytes per sector
  1032. spiflash_sector_erase(addr);
  1033. uprint("Erased sector ");
  1034. uprintDec(i >> 5);
  1035. uprint(" at address ");
  1036. uprintHex(addr);
  1037. uprintln("");
  1038. // Write sector by writing 16 pages k of 64 words
  1039. // Does not check for boundaries of actual FAT table size,
  1040. // so it can write garbage if block size is not a multiple of 1024
  1041. word k;
  1042. for (k = 0; k < 1024; k+=64)
  1043. {
  1044. addr = BRFS_SPIFLASH_FAT_ADDR; // Workaround because of large static number
  1045. addr += (i >> 5) * 4096; // Sector idx * bytes per sector
  1046. addr += k << 2; // 64 words * 4 bytes per word
  1047. word* fat_addr_ram = brfs_ram_storage + SUPERBLOCK_SIZE + (i << 5) + k;
  1048. spiflash_write_page_in_words(fat_addr_ram, addr, 64);
  1049. uprint("Wrote FAT entries ");
  1050. uprintDec((i << 5) + k);
  1051. uprint(":");
  1052. uprintDec((i << 5) + k + 63);
  1053. uprint(" from RAM addr ");
  1054. uprintHex((word)fat_addr_ram);
  1055. uprint(" to SPI Flash addr ");
  1056. uprintHex(addr);
  1057. uprintln("");
  1058. }
  1059. }
  1060. }
  1061. uprintln("---Finished writing FAT to SPI Flash---");
  1062. }
  1063. /**
  1064. * Write a sector (4KiB) to SPI flash
  1065. * sector_idx: index of the sector
  1066. */
  1067. void brfs_write_sector_to_flash(word sector_idx)
  1068. {
  1069. word spi_addr = BRFS_SPIFLASH_BLOCK_ADDR; // Workaround because of large static number
  1070. spi_addr += sector_idx * 4096; // Sector idx * bytes per sector
  1071. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  1072. word* data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  1073. word brfs_sector_addr = data_block_addr + sector_idx * (4096 >> 2); // Divided by 4 because of word size
  1074. // Write sector by writing 16 pages k of 64 words
  1075. // Does not check for boundaries of actual FAT table size,
  1076. // so it can write garbage if block size is not a multiple of 1024
  1077. word k;
  1078. for (k = 0; k < 1024; k+=64)
  1079. {
  1080. spiflash_write_page_in_words(brfs_sector_addr + k, spi_addr + (k << 2), 64);
  1081. uprint("Wrote sector ");
  1082. uprintDec(sector_idx);
  1083. uprint(":");
  1084. uprintDec(sector_idx + 15);
  1085. uprint(" from RAM addr ");
  1086. uprintHex((word)(brfs_sector_addr + k));
  1087. uprint(" to SPI Flash addr ");
  1088. uprintHex(spi_addr + (k << 2));
  1089. uprintln("");
  1090. }
  1091. }
  1092. /**
  1093. * Write the data blocks to SPI flash by performing three steps:
  1094. * 1. Check which blocks have changed
  1095. * 2. Erase the 4KiB sectors that contain these blocks
  1096. * 3. Write each erased sector with the new block data by using 16 page writes per sector
  1097. */
  1098. void brfs_write_blocks_to_flash()
  1099. {
  1100. // Loop over all blocks
  1101. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  1102. word* data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  1103. // Check if block size is <= 4KiB
  1104. if (superblock->words_per_block > 1024)
  1105. {
  1106. uprintln("Error: block size should be <= 4KiB");
  1107. return;
  1108. }
  1109. // Check if block size is a multiple of 64
  1110. if (superblock->words_per_block & 63)
  1111. {
  1112. uprintln("Error: block size should be a multiple of 64");
  1113. return;
  1114. }
  1115. uprintln("---Writing blocks to SPI Flash---");
  1116. word blocks_per_sector = MATH_divU(4096, superblock->words_per_block * 4);
  1117. uprint("Blocks per sector: ");
  1118. uprintDec(blocks_per_sector);
  1119. uprintln("");
  1120. // Erase 4KiB sectors that contain changed blocks
  1121. // This code is written such that it only erases each sector once, even if multiple blocks in the sector have changed
  1122. word i;
  1123. word sector_to_erase = -1;
  1124. for (i = 0; i < superblock->total_blocks; i++)
  1125. {
  1126. if (brfs_changed_blocks[i >> 5] & (1 << (i & 31)))
  1127. {
  1128. if (sector_to_erase == -1)
  1129. {
  1130. sector_to_erase = MATH_divU(i, blocks_per_sector);
  1131. }
  1132. else if (sector_to_erase != MATH_divU(i, blocks_per_sector))
  1133. {
  1134. word addr = BRFS_SPIFLASH_BLOCK_ADDR; // Workaround because of large static number
  1135. addr += sector_to_erase * 4096;
  1136. spiflash_sector_erase(addr);
  1137. uprint("Erased sector ");
  1138. uprintDec(sector_to_erase);
  1139. uprint(" at address ");
  1140. uprintHex(addr);
  1141. uprintln("");
  1142. brfs_write_sector_to_flash(sector_to_erase);
  1143. sector_to_erase = MATH_divU(i, blocks_per_sector);
  1144. }
  1145. }
  1146. }
  1147. if (sector_to_erase != -1)
  1148. {
  1149. word addr = BRFS_SPIFLASH_BLOCK_ADDR; // Workaround because of large static number
  1150. addr += sector_to_erase * 4096;
  1151. spiflash_sector_erase(addr);
  1152. uprint("Erased sector ");
  1153. uprintDec(sector_to_erase);
  1154. uprint(" at address ");
  1155. uprintHex(addr);
  1156. uprintln("");
  1157. brfs_write_sector_to_flash(sector_to_erase);
  1158. }
  1159. uprintln("---Finished writing blocks to SPI Flash---");
  1160. }
  1161. /**
  1162. * Write the FAT and data blocks to SPI flash
  1163. * Superblock should already be written to flash during format
  1164. */
  1165. void brfs_write_to_flash()
  1166. {
  1167. brfs_write_fat_to_flash();
  1168. brfs_write_blocks_to_flash();
  1169. // Reset changed blocks
  1170. memset(brfs_changed_blocks, 0, sizeof(brfs_changed_blocks));
  1171. }
  1172. /**
  1173. * Checks if given superblock is valid
  1174. * Returns 1 if valid, 0 if invalid
  1175. */
  1176. word brfs_superblock_is_valid(struct brfs_superblock* superblock)
  1177. {
  1178. // Check if brfs version is correct
  1179. if (superblock->brfs_version != BRFS_SUPPORTED_VERSION)
  1180. {
  1181. uprint("BRFS version ");
  1182. uprintDec(superblock->brfs_version);
  1183. uprint(" is not supported by this implementation (");
  1184. uprintDec(BRFS_SUPPORTED_VERSION);
  1185. uprintln(")!");
  1186. return 0;
  1187. }
  1188. // Check if total blocks is > 0 and a multiple of 64
  1189. if (superblock->total_blocks == 0 || superblock->total_blocks & 63)
  1190. {
  1191. uprintln("Error: total blocks should be > 0 and a multiple of 64");
  1192. return 0;
  1193. }
  1194. // Check if block size is > 0
  1195. if (superblock->words_per_block == 0)
  1196. {
  1197. uprintln("Error: block size should be > 0");
  1198. return 0;
  1199. }
  1200. // Check if words per block is > 0 and <= 2048
  1201. if (superblock->words_per_block == 0 || superblock->words_per_block > 2048)
  1202. {
  1203. uprintln("Error: words per block should be > 0 and <= 2048");
  1204. return 0;
  1205. }
  1206. return 1;
  1207. }
  1208. /**
  1209. * Read the superblock, FAT and data blocks from SPI flash
  1210. * Reads in QSPI mode and returns to SPI mode after reading
  1211. * Returns 1 on success, or 0 on error
  1212. */
  1213. word brfs_read_from_flash()
  1214. {
  1215. // Set QSPI memory mapped mode
  1216. spiflash_qspi();
  1217. // Read superblock from flash
  1218. char* spi_flash_read_addr = (char*) SPIFLASH_MEMMAP_ADDR + (BRFS_SPIFLASH_SUPERBLOCK_ADDR >> 2);
  1219. memcpy(brfs_ram_storage, spi_flash_read_addr, SUPERBLOCK_SIZE);
  1220. // Perform validity checks on superblock
  1221. struct brfs_superblock* superblock = (struct brfs_superblock*) brfs_ram_storage;
  1222. if (!brfs_superblock_is_valid(superblock))
  1223. {
  1224. uprintln("Error: superblock is not valid!");
  1225. spiflash_init(); // Return to SPI mode
  1226. return 0;
  1227. }
  1228. // Read FAT from flash
  1229. spi_flash_read_addr = (char*) SPIFLASH_MEMMAP_ADDR + (BRFS_SPIFLASH_FAT_ADDR >> 2);
  1230. memcpy(brfs_ram_storage + SUPERBLOCK_SIZE, spi_flash_read_addr, superblock->total_blocks);
  1231. // Read data blocks from flash
  1232. word* data_block_addr = brfs_ram_storage + SUPERBLOCK_SIZE + superblock->total_blocks;
  1233. spi_flash_read_addr = (char*) SPIFLASH_MEMMAP_ADDR + (BRFS_SPIFLASH_BLOCK_ADDR >> 2);
  1234. word read_length = superblock->total_blocks * superblock->words_per_block;
  1235. // Check if read_length is a multiple of 16
  1236. if (read_length & 15)
  1237. {
  1238. // Read without progress bar
  1239. memcpy(data_block_addr, spi_flash_read_addr, read_length);
  1240. spiflash_init(); // Return to SPI mode
  1241. return 1;
  1242. }
  1243. // Print progress bar
  1244. GFX_disable_cursor = 1;
  1245. GFX_PrintConsole("Loading blocks: ");
  1246. // Print emtpy block character
  1247. word i;
  1248. for (i = 0; i < 16; i++)
  1249. {
  1250. GFX_PrintcConsole(176);
  1251. }
  1252. // Set cursor back to start of progress bar
  1253. GFX_cursor -= 16;
  1254. // Split in 16 parts
  1255. word read_length_per_part = read_length >> 4;
  1256. for (i = 0; i < 16; i++)
  1257. {
  1258. memcpy(data_block_addr + (i * read_length_per_part), spi_flash_read_addr + (i * read_length_per_part), read_length_per_part);
  1259. GFX_PrintcConsole(219); // Print full block character
  1260. }
  1261. GFX_disable_cursor = 0;
  1262. GFX_PrintcConsole('\n');
  1263. spiflash_init(); // Return to SPI mode
  1264. return 1;
  1265. }