Skip to content

Commit b3e52ef

Browse files
Added forceforeful executable to code flags. ALSO FIXED FAT16 NOT ITS PERFECT!
1 parent 975996d commit b3e52ef

File tree

8 files changed

+91
-79
lines changed

8 files changed

+91
-79
lines changed

disk.img

0 Bytes
Binary file not shown.

source/includes/tss.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ extern uint8_t kernel_stack[0x4000];
5959
* @brief Initialize TSS.
6060
*
6161
*/
62-
void tss_init();
62+
void kernel_tss_init();
6363

6464
/**
6565
* @brief Load the TSS.

source/kernel/C/filesystems/fat16.c

Lines changed: 61 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -222,20 +222,21 @@ int fat16_find_in_dir(
222222
{
223223
uint8_t buf[512];
224224

225-
debug_printf("[fat16] find_in_dir cluster=%u\n", dir_cluster);
226-
227-
/* ROOT DIR */
228-
if (dir_cluster == FAT16_ROOT_CLUSTER) {
229-
uint32_t lba = fs->root_dir_start;
230-
uint32_t sectors = fs->root_dir_sectors;
231-
232-
for (uint32_t s = 0; s < sectors; s++) {
233-
ahci_read_sector(fs->portno, lba + s, buf, 1);
225+
/* ---------- ROOT DIR ---------- */
226+
if (dir_cluster == 0) {
227+
for (uint32_t s = 0; s < fs->root_dir_sectors; s++) {
228+
ahci_read_sector(fs->portno, fs->root_dir_start + s, buf, 1);
234229
fat16_dir_entry_t* e = (fat16_dir_entry_t*)buf;
235230

236231
for (int i = 0; i < DIR_ENTRIES_PER_SECTOR; i++) {
237-
if (e[i].name[0] == 0x00) return FAT_ERR_NOT_FOUND;
238-
if (e[i].name[0] == 0xE5) continue;
232+
if (e[i].name[0] == 0x00)
233+
return FAT_ERR_NOT_FOUND;
234+
if (e[i].name[0] == 0xE5)
235+
continue;
236+
if ((e[i].attr & 0x0F) == 0x0F)
237+
continue;
238+
if (e[i].attr & 0x08)
239+
continue;
239240

240241
if (fat16_name_eq(e[i].name, name)) {
241242
*out = e[i];
@@ -246,7 +247,7 @@ int fat16_find_in_dir(
246247
return FAT_ERR_NOT_FOUND;
247248
}
248249

249-
/* NORMAL DIRECTORY */
250+
/* ---------- SUBDIRECTORY ---------- */
250251
uint16_t cluster = dir_cluster;
251252

252253
while (cluster >= 2 && cluster < FAT16_EOC) {
@@ -257,8 +258,14 @@ int fat16_find_in_dir(
257258
fat16_dir_entry_t* e = (fat16_dir_entry_t*)buf;
258259

259260
for (int i = 0; i < DIR_ENTRIES_PER_SECTOR; i++) {
260-
if (e[i].name[0] == 0x00) return FAT_ERR_NOT_FOUND;
261-
if (e[i].name[0] == 0xE5) continue;
261+
if (e[i].name[0] == 0x00)
262+
return FAT_ERR_NOT_FOUND;
263+
if (e[i].name[0] == 0xE5)
264+
continue;
265+
if ((e[i].attr & 0x0F) == 0x0F)
266+
continue;
267+
if (e[i].attr & 0x08)
268+
continue;
262269

263270
if (fat16_name_eq(e[i].name, name)) {
264271
*out = e[i];
@@ -267,14 +274,13 @@ int fat16_find_in_dir(
267274
}
268275
}
269276

270-
uint16_t next = fat16_read_fat_fs(fs, cluster);
271-
if (next == cluster) break;
272-
cluster = next;
277+
cluster = fat16_read_fat_fs(fs, cluster);
273278
}
274279

275280
return FAT_ERR_NOT_FOUND;
276281
}
277282

283+
278284
void fat16_list_dir_cluster(fat16_fs_t* fs, uint16_t start_cluster) {
279285
uint8_t buf[512];
280286
uint16_t cluster = start_cluster;
@@ -943,10 +949,11 @@ int fat16_mkdir(fat16_fs_t* fs, uint16_t parent_cluster, const char* name) {
943949
return FAT_OK;
944950
}
945951

946-
static int fat16_dir_is_empty(fat16_fs_t* fs, uint16_t cluster) {
952+
static int fat16_dir_is_empty(fat16_fs_t* fs, uint16_t cluster)
953+
{
947954
uint8_t buf[512];
948955

949-
while (cluster < FAT16_EOC) {
956+
while (cluster >= 2 && cluster < FAT16_EOC) {
950957
uint32_t lba = fat16_cluster_lba(fs, cluster);
951958

952959
for (uint32_t s = 0; s < fs->bs.sectors_per_cluster; s++) {
@@ -955,22 +962,23 @@ static int fat16_dir_is_empty(fat16_fs_t* fs, uint16_t cluster) {
955962

956963
for (int i = 0; i < DIR_ENTRIES_PER_SECTOR; i++) {
957964
if (e[i].name[0] == 0x00)
958-
return FAT_ERR_NOT_EMPTY;
965+
return FAT_OK; // end → empty
959966

960967
if (e[i].name[0] == 0xE5)
961968
continue;
962969

963-
if (e[i].name[0] == '.' && (e[i].name[1] == ' ' || e[i].name[1] == '.'))
970+
if (fat16_name_eq(e[i].name, ".") ||
971+
fat16_name_eq(e[i].name, ".."))
964972
continue;
965-
966-
return FAT_OK; // found real entry
973+
974+
return FAT_ERR_NOT_EMPTY;
967975
}
968976
}
969977

970978
cluster = fat16_read_fat_fs(fs, cluster);
971979
}
972980

973-
return FAT_ERR_NOT_EMPTY;
981+
return FAT_OK;
974982
}
975983

976984
int fat16_create_path(fat16_fs_t* fs,
@@ -1161,6 +1169,9 @@ int fat16_ls(fat16_fs_t* fs, const char* path) {
11611169
for (int i = 0; i < DIR_ENTRIES_PER_SECTOR; i++) {
11621170
if (e[i].name[0] == 0x00) return FAT_OK;
11631171
if (e[i].name[0] == 0xE5) continue;
1172+
if ((e[i].attr & 0x0F) == 0x0F) continue;
1173+
if (e[i].attr & 0x08) continue;
1174+
11641175

11651176
char name[13];
11661177
fat16_unformat_name(&e[i], name);
@@ -1190,6 +1201,9 @@ int fat16_ls(fat16_fs_t* fs, const char* path) {
11901201
for (int i = 0; i < DIR_ENTRIES_PER_SECTOR; i++) {
11911202
if (e[i].name[0] == 0x00) return FAT_OK;
11921203
if (e[i].name[0] == 0xE5) continue;
1204+
if ((e[i].attr & 0x0F) == 0x0F) continue;
1205+
if (e[i].attr & 0x08) continue;
1206+
11931207

11941208
char name[13];
11951209
fat16_unformat_name(&e[i], name);
@@ -1217,63 +1231,56 @@ int fat16_cd(fat16_fs_t* fs, const char* path, uint16_t* pwd_cluster) {
12171231
return FAT_OK;
12181232
}
12191233

1220-
12211234
int fat16_resolve_path(
12221235
fat16_fs_t* fs,
12231236
const char* path,
1224-
uint16_t pwd_cluster, // current working directory cluster
1225-
uint16_t* out_cluster // result cluster
1237+
uint16_t pwd_cluster,
1238+
uint16_t* out_cluster
12261239
) {
12271240
char part[13];
1228-
uint16_t current_cluster;
1229-
1241+
uint16_t current = (path[0] == '/') ? 0 : pwd_cluster;
12301242
const char* p = path;
1231-
while (*p == '/') p++;
12321243

1233-
// absolute path starts from root
1234-
if (path[0] == '/') {
1235-
current_cluster = 0;
1236-
} else {
1237-
// relative path starts from pwd
1238-
current_cluster = pwd_cluster;
1239-
}
1244+
while (*p == '/') p++;
12401245

12411246
while (*p) {
12421247
int len = 0;
12431248
while (p[len] && p[len] != '/') len++;
12441249

1245-
if (len >= 13) len = 12;
12461250
memcpy(part, p, len);
12471251
part[len] = 0;
12481252

1249-
// handle special cases
12501253
if (strcmp(part, ".") == 0) {
1251-
// stay in current cluster
1252-
} else if (strcmp(part, "..") == 0) {
1253-
fat16_dir_entry_t entry;
1254-
if (fat16_find_in_dir(fs, current_cluster, "..", &entry) != 0) {
1255-
current_cluster = 0; // fallback to root if parent not found
1254+
/* no-op */
1255+
}
1256+
else if (strcmp(part, "..") == 0) {
1257+
if (current == 0) {
1258+
/* stay at root */
12561259
} else {
1257-
current_cluster = entry.first_cluster;
1260+
fat16_dir_entry_t e;
1261+
if (fat16_find_in_dir(fs, current, "..", &e) == 0)
1262+
current = e.first_cluster;
1263+
else
1264+
current = 0;
12581265
}
1259-
} else {
1260-
fat16_dir_entry_t entry;
1261-
if (fat16_find_in_dir(fs, current_cluster, part, &entry) != 0)
1262-
return FAT_ERR_NOT_FOUND; // path component not found
1263-
if (!(entry.attr & 0x10))
1264-
return FAT_ERR_NOT_FOUND; // must be directory
1265-
current_cluster = entry.first_cluster;
1266+
}
1267+
else {
1268+
fat16_dir_entry_t e;
1269+
if (fat16_find_in_dir(fs, current, part, &e) != 0)
1270+
return FAT_ERR_NOT_FOUND;
1271+
if (!(e.attr & 0x10))
1272+
return FAT_ERR_NOT_FOUND;
1273+
current = e.first_cluster;
12661274
}
12671275

12681276
p += len;
12691277
while (*p == '/') p++;
12701278
}
12711279

1272-
*out_cluster = current_cluster;
1280+
*out_cluster = current;
12731281
return FAT_OK;
12741282
}
12751283

1276-
12771284
int fat16_ls_cwd(fat16_fs_t* fs, const char* path) {
12781285
if (!path || path[0] == '\0') {
12791286
path = fs->cwd_path;

source/kernel/C/filesystems/vfs.c

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@ int vfs_rm_recursive(const char* path)
306306
return 0;
307307
}
308308

309-
int vfs_cd(const char* path) {
309+
int vfs_cd(const char* path)
310+
{
310311
if (!path || !*path) {
311312
printf("cd: path is null or undefined");
312313
return -1;
@@ -316,35 +317,39 @@ int vfs_cd(const char* path) {
316317
vfs_normalize_path(path, norm);
317318

318319
vfs_mount_res_t res;
319-
if (vfs_resolve_mount(norm, &res) != 0) {
320+
if (vfs_resolve_mount(norm, &res) != 0)
320321
return -1;
322+
323+
if (res.mnt->type != FS_FAT16) {
324+
printf("cd: unknown filesystem");
325+
return -2;
321326
}
322327

323-
if (res.mnt->type == FS_FAT16) {
324-
fat16_fs_t* fs = (fat16_fs_t*)res.mnt->fs;
328+
fat16_fs_t* fs = (fat16_fs_t*)res.mnt->fs;
325329

326-
uint16_t new_cluster;
327-
uint16_t base_cluster = vfs_cwd_cluster;
330+
uint16_t new_cluster = FAT16_ROOT_CLUSTER;
328331

329-
// handle root specially
330-
if (*res.rel_path == '\0') {
331-
new_cluster = FAT16_ROOT_CLUSTER;
332-
} else {
333-
int ret = fat16_cd(fs, res.rel_path, &vfs_cwd_cluster);
332+
if (*res.rel_path) {
333+
fat16_dir_entry_t e;
334+
if (fat16_find_path(fs, res.rel_path, &e) != FAT_OK)
335+
return -3;
334336

335-
if (ret != FAT_OK)
336-
return -4;
337-
338-
}
339-
strncpy(vfs_cwd, norm, sizeof(vfs_cwd));
340-
debug_printf("[vfs] cd : %s cluster=%u\n", norm, new_cluster);
341-
return 0;
337+
if (!(e.attr & 0x10))
338+
return -4;
339+
340+
new_cluster = e.first_cluster;
342341
}
343342

344-
printf("cd: unknown filesystem");
345-
return -3;
343+
/* COMMIT CWD */
344+
vfs_cwd_cluster = new_cluster;
345+
strncpy(vfs_cwd, norm, sizeof(vfs_cwd));
346+
vfs_cwd[sizeof(vfs_cwd) - 1] = 0;
347+
348+
debug_printf("[vfs] cd -> %s (cluster=%u)\n", vfs_cwd, vfs_cwd_cluster);
349+
return 0;
346350
}
347351

352+
348353
int vfs_create_path(const char* path, uint8_t attr) {
349354
if (!path || !*path) {
350355
printf("create_path: path is null or undefined");

source/kernel/C/gdt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void setup_gdt() {
4646
info("User data descriptor has been set", __FILE__);
4747

4848
/* TSS Setup */
49-
tss_init();
49+
kernel_tss_init();
5050

5151
gdtp.limit = sizeof(gdt) - 1;
5252
gdtp.base = (uint64_t)&gdt;

source/kernel/C/kernel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ void main(void) {
239239

240240
info("Welcome to FrostWing Operating System!", "(https://github.com/Frost-Wing)");
241241

242-
enter_userland();
242+
// enter_userland();
243243
sh_exec();
244244
}
245245

source/kernel/C/tss.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ __attribute__((aligned(16)))
2020
uint8_t kernel_stack[0x4000]; // 16 KB kernel stack
2121

2222
// Initialize TSS
23-
void tss_init(void) {
23+
void kernel_tss_init(void) {
2424
memset(&tss, 0, sizeof(tss));
2525

2626
tss.rsp0 = (uint64_t)(kernel_stack + sizeof(kernel_stack));

source/kernel/C/userland.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ void map_user_code_physical(uint64_t code_phys, uint64_t code_size) {
1818
uint64_t mapped = 0;
1919

2020
while (mapped < code_size) {
21-
map_user_page(USER_CODE_VADDR + mapped, phys_page, USER_CODE_FLAGS);
21+
map_user_page(USER_CODE_VADDR + mapped, phys_page, USER_CODE_FLAGS & ~PAGE_NX);
2222

2323
if (mapped == 0 && page_offset != 0)
2424
mapped += PAGE_SIZE - page_offset;

0 commit comments

Comments
 (0)