1+ #include "ofs.h"
2+ #include "../Drivers/Ata/ata.h"
3+ #include "../SystemLib/Std/std.h"
4+ #include "../SystemLib/Std/types.h"
5+ #include "../SystemLib/Memory/mem.h"
6+ #include "../Drivers/Vga/vga.h"
7+
8+ ofs_t ofs ;
9+
10+ #define SECTOR_SIZE 512
11+ #define FTABLE_SECTORS 8
12+
13+ void ofs_init () {
14+ ata_sys_disk_read (0 , (uint8_t * )& ofs .superb );
15+
16+ if (memcmp (ofs .superb .signature , "OFS1" , 4 ) != 0 ) {
17+ memset (& ofs , 0 , sizeof (ofs ));
18+
19+ ofs .superb .signature [0 ] = 'O' ;
20+ ofs .superb .signature [1 ] = 'F' ;
21+ ofs .superb .signature [2 ] = 'S' ;
22+ ofs .superb .signature [3 ] = '1' ;
23+
24+ ofs .superb .version = 1 ;
25+ ofs .superb .total_sectors = 0 ;
26+ ofs .superb .file_table_start = 1 ;
27+ ofs .superb .data_start = 1 + FTABLE_SECTORS ;
28+
29+ ata_sys_disk_write (0 , (uint8_t * )& ofs .superb );
30+
31+ memset (ofs .file_table , 0 , sizeof (ofs .file_table ));
32+
33+ for (int i = 0 ; i < FTABLE_SECTORS ; i ++ ) {
34+ ata_sys_disk_write (1 + i , (uint8_t * )(ofs .file_table + (i * SECTOR_SIZE )));
35+ }
36+ }
37+ for (int i = 0 ; i < FTABLE_SECTORS ; i ++ ) {
38+ ata_sys_disk_read (1 + i , (uint8_t * )(ofs .file_table + (i * SECTOR_SIZE )));
39+ }
40+ }
41+
42+ void ofs_create_file (const char * name , uint32_t size ) {
43+
44+ for (int i = 0 ; i < OFS_MAX_FILES ; i ++ ) {
45+
46+ if (!ofs .file_table [i ].used ) {
47+
48+ memset (ofs .file_table [i ].name , 0 , OFS_NAME_MAX_LENGTH );
49+ strncpy (ofs .file_table [i ].name , name , OFS_NAME_MAX_LENGTH - 1 );
50+
51+ ofs .file_table [i ].size_bytes = size ;
52+ ofs .file_table [i ].used = 1 ;
53+ ofs .file_table [i ].flags = 0 ;
54+
55+ ofs .file_table [i ].start_sector =
56+ ofs .superb .data_start + (i * 8 );
57+
58+ return ;
59+ }
60+ }
61+ }
62+
63+ void ofs_read_file (const char * name , uint8_t * buffer ) {
64+ for (int i = 0 ; i < OFS_MAX_FILES ; i ++ ) {
65+ if (ofs .file_table [i ].used &&
66+ strncmp (ofs .file_table [i ].name , name , OFS_NAME_MAX_LENGTH ) == 0 ) {
67+
68+ uint32_t sectors_to_read =
69+ (ofs .file_table [i ].size_bytes + SECTOR_SIZE - 1 ) / SECTOR_SIZE ;
70+
71+ for (uint32_t s = 0 ; s < sectors_to_read ; s ++ ) {
72+ ata_sys_disk_read (ofs .file_table [i ].start_sector + s ,
73+ buffer + (s * SECTOR_SIZE ));
74+ }
75+ return ;
76+ }
77+ }
78+ }
79+
80+ void ofs_write_file (const char * name , const uint8_t * data , uint32_t size ) {
81+
82+ for (int i = 0 ; i < OFS_MAX_FILES ; i ++ ) {
83+
84+ if (ofs .file_table [i ].used && strncmp (ofs .file_table [i ].name , name , OFS_NAME_MAX_LENGTH ) == 0 ) {
85+
86+ ofs .file_table [i ].size_bytes = size ;
87+
88+ uint32_t sectors =
89+ (size + SECTOR_SIZE - 1 ) / SECTOR_SIZE ;
90+
91+ for (uint32_t s = 0 ; s < sectors ; s ++ ) {
92+ ata_sys_disk_write (ofs .file_table [i ].start_sector + s , data + (s * SECTOR_SIZE ));
93+ }
94+ return ;
95+ }
96+ }
97+ }
98+
99+ void ofs_delete_file (const char * name ) {
100+ for (int i = 0 ; i < OFS_MAX_FILES ; i ++ ) {
101+ if (ofs .file_table [i ].used && strncmp (ofs .file_table [i ].name , name , OFS_NAME_MAX_LENGTH ) == 0 ) {
102+ ofs .file_table [i ].used = 0 ;
103+ return ;
104+ }
105+ }
106+ }
107+
108+ void ofs_list_files () {
109+ for (int i = 0 ; i < OFS_MAX_FILES ; i ++ ) {
110+ if (ofs .file_table [i ].used ) {
111+ vga_print_scr ("FILE: " );
112+ vga_print_scr (ofs .file_table [i ].name );
113+ vga_print_scr ("\n" );
114+ }
115+ }
116+ }
0 commit comments