Skip to content

Commit 5c164a9

Browse files
The OFS file system, developed specifically for OpenKernel, has been added.
1 parent 82a6898 commit 5c164a9

2 files changed

Lines changed: 156 additions & 0 deletions

File tree

OFS/ofs.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
}

OFS/ofs.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef OFS_H
2+
#define OFS_H
3+
4+
#include "../SystemLib/Std/types.h"
5+
6+
#define OFS_NAME_MAX_LENGTH 32
7+
#define OFS_MAX_FILES 128
8+
9+
typedef struct {
10+
char signature[4];
11+
uint32_t total_sectors;
12+
uint32_t file_table_start;
13+
uint32_t data_start;
14+
uint32_t version;
15+
} ofs_superblock_t;
16+
17+
typedef struct {
18+
char name[OFS_NAME_MAX_LENGTH];
19+
uint32_t start_sector;
20+
uint32_t size_bytes;
21+
uint8_t used;
22+
uint8_t flags;
23+
uint8_t reserved;
24+
} ofs_file_t;
25+
26+
typedef struct {
27+
ofs_superblock_t superb;
28+
ofs_file_t file_table[OFS_MAX_FILES];
29+
} ofs_t;
30+
31+
extern ofs_t ofs;
32+
33+
void ofs_init();
34+
void ofs_create_file(const char* name, uint32_t size);
35+
void ofs_read_file(const char* name, uint8_t* buffer);
36+
void ofs_write_file(const char* name, const uint8_t* data, uint32_t size);
37+
void ofs_delete_file(const char* name);
38+
void ofs_list_files();
39+
40+
#endif

0 commit comments

Comments
 (0)