brfs.c 42 KB

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