Skip to content

Commit 9575d74

Browse files
Minor bug fixes and removal of unnecessary whitespace.
1 parent bd46e28 commit 9575d74

4 files changed

Lines changed: 14 additions & 76 deletions

File tree

FAT12/fat12.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ void fat12_list_root() {
5858
name[k] = entry[j].filename[k];
5959
}
6060
name[11] = '\0';
61-
62-
vga_print_scr(name);
63-
vga_print_scr("\n");
61+
vga_print_scr_nw(name);
6462
}
6563
}
6664
}
@@ -70,30 +68,25 @@ int fat12_read_file(const char* filename, void* buffer, uint32_t size) {
7068

7169
for (uint32_t i = 0; i < fs.root_dir_size; i++) {
7270
read_sector(fs.root_dir_lba + i, sector);
73-
7471
fat12_dir_entry_t* entry = (fat12_dir_entry_t*)sector;
7572

7673
for (int j = 0; j < 16; j++) {
7774
if (entry[j].filename[0] == 0x00)
7875
return -1;
7976

8077
if (strncmp(entry[j].filename, filename, 11) == 0) {
81-
8278
uint16_t cluster = entry[j].first_cluster;
8379
uint32_t read_bytes = 0;
8480

8581
while (cluster < FAT12_EOC && read_bytes < size) {
86-
8782
uint32_t lba = fs.data_start_lba + (cluster - 2) * fs.bs.sectors_per_cluster;
8883
read_sector(lba, buffer + read_bytes);
8984
read_bytes += fs.bs.sectors_per_cluster * fs.bs.bytes_per_sector;
9085
cluster = fat12_get_next_cluster(cluster);
9186
}
92-
9387
return read_bytes;
9488
}
9589
}
9690
}
97-
9891
return -1;
9992
}

FAT16/fat16.c

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,18 @@ static void read_sector(uint32_t lba, void* buffer) {
1111

1212
void fat16_init(uint32_t boot_lba) {
1313
read_sector(boot_lba, &fs.bs);
14-
1514
fs.fat_start_lba = boot_lba + fs.bs.reserved_sectors;
16-
17-
fs.root_dir_lba =
18-
fs.fat_start_lba +
19-
(fs.bs.fat_count * fs.bs.sectors_per_fat);
20-
21-
fs.root_dir_sectors =
22-
((fs.bs.root_entry_count * 32) +
23-
(fs.bs.bytes_per_sector - 1)) / fs.bs.bytes_per_sector;
24-
15+
fs.root_dir_lba = fs.fat_start_lba + (fs.bs.fat_count * fs.bs.sectors_per_fat);
16+
fs.root_dir_sectors = ((fs.bs.root_entry_count * 32) + (fs.bs.bytes_per_sector - 1)) / fs.bs.bytes_per_sector;
2517
fs.data_start_lba = fs.root_dir_lba + fs.root_dir_sectors;
2618
}
2719

2820
uint16_t fat16_get_next_cluster(uint16_t cluster) {
2921
uint32_t fat_offset = cluster * 2;
3022
uint32_t fat_sector = fs.fat_start_lba + (fat_offset / fs.bs.bytes_per_sector);
3123
uint32_t ent_offset = fat_offset % fs.bs.bytes_per_sector;
32-
3324
uint8_t sector[512];
3425
read_sector(fat_sector, sector);
35-
3626
return *(uint16_t*)&sector[ent_offset];
3727
}
3828

@@ -41,11 +31,9 @@ void fat16_list_root() {
4131

4232
for (uint32_t i = 0; i < fs.root_dir_sectors; i++) {
4333
read_sector(fs.root_dir_lba + i, sector);
44-
4534
fat16_dir_entry_t* entry = (fat16_dir_entry_t*)sector;
4635

4736
for (int j = 0; j < 16; j++) {
48-
4937
if (entry[j].filename[0] == 0x00)
5038
return;
5139

@@ -59,46 +47,35 @@ void fat16_list_root() {
5947
name[k++] = entry[j].filename[i];
6048
}
6149
name[k] = '\0';
62-
vga_print_scr(name);
63-
vga_print_scr("\n");
50+
vga_print_scr_nw(name);
6451
}
6552
}
6653
}
6754

6855
int fat16_read_file(const char* filename, void* buffer, uint32_t size) {
6956
uint8_t sector[512];
70-
7157
for (uint32_t i = 0; i < fs.root_dir_sectors; i++) {
7258
read_sector(fs.root_dir_lba + i, sector);
73-
7459
fat16_dir_entry_t* entry = (fat16_dir_entry_t*)sector;
7560

7661
for (int j = 0; j < 16; j++) {
77-
7862
if (entry[j].filename[0] == 0x00)
7963
return -1;
8064

8165
if (strncmp(entry[j].filename, filename, 11) == 0) {
82-
8366
uint16_t cluster = entry[j].first_cluster;
8467
uint32_t offset = 0;
8568

8669
while (cluster < FAT16_EOC && offset < size) {
8770

88-
uint32_t lba = fs.data_start_lba +
89-
(cluster - 2) * fs.bs.sectors_per_cluster;
90-
71+
uint32_t lba = fs.data_start_lba + (cluster - 2) * fs.bs.sectors_per_cluster;
9172
read_sector(lba, buffer + offset);
92-
9373
offset += fs.bs.sectors_per_cluster * fs.bs.bytes_per_sector;
94-
9574
cluster = fat16_get_next_cluster(cluster);
9675
}
97-
9876
return offset;
9977
}
10078
}
10179
}
102-
10380
return -1;
10481
}

FAT32/fat32.c

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@ static void read_sector(uint32_t lba, void* buffer) {
1111

1212
void fat32_init(uint32_t boot_lba) {
1313
read_sector(boot_lba, &fs.bs);
14-
1514
fs.fat_start_lba = boot_lba + fs.bs.reserved_sectors;
16-
17-
fs.data_start_lba =
18-
fs.fat_start_lba +
19-
(fs.bs.fat_count * fs.bs.sectors_per_fat);
20-
15+
fs.data_start_lba = fs.fat_start_lba + (fs.bs.fat_count * fs.bs.sectors_per_fat);
2116
fs.root_cluster = fs.bs.root_cluster;
2217
}
2318

@@ -35,22 +30,17 @@ uint32_t fat32_get_next_cluster(uint32_t cluster) {
3530

3631
void fat32_list_dir(uint32_t cluster) {
3732
uint8_t sector[512];
38-
3933
uint32_t current = cluster;
4034

4135
while (current < FAT32_EOC) {
4236

43-
uint32_t lba = fs.data_start_lba +
44-
(current - 2) * fs.bs.sectors_per_cluster;
37+
uint32_t lba = fs.data_start_lba + (current - 2) * fs.bs.sectors_per_cluster;
4538

4639
for (uint8_t i = 0; i < fs.bs.sectors_per_cluster; i++) {
47-
4840
read_sector(lba + i, sector);
49-
5041
fat32_dir_entry_t* entry = (fat32_dir_entry_t*)sector;
5142

5243
for (int j = 0; j < 16; j++) {
53-
5444
if (entry[j].filename[0] == 0x00)
5545
return;
5646

@@ -74,65 +64,43 @@ void fat32_list_dir(uint32_t cluster) {
7464
} else {
7565
vga_print_scr(" ");
7666
}
77-
78-
79-
vga_print_scr(name);
80-
vga_print_scr("\n");
67+
vga_print_scr_nw(name);
8168
}
8269
}
83-
8470
current = fat32_get_next_cluster(current);
8571
}
8672
}
8773

8874
int fat32_read_file(const char* filename, void* buffer, uint32_t size) {
8975
uint8_t sector[512];
90-
9176
uint32_t cluster = fs.root_cluster;
9277

9378
while (cluster < FAT32_EOC) {
94-
95-
uint32_t lba = fs.data_start_lba +
96-
(cluster - 2) * fs.bs.sectors_per_cluster;
79+
uint32_t lba = fs.data_start_lba + (cluster - 2) * fs.bs.sectors_per_cluster;
9780

9881
for (uint8_t i = 0; i < fs.bs.sectors_per_cluster; i++) {
99-
10082
read_sector(lba + i, sector);
101-
10283
fat32_dir_entry_t* entry = (fat32_dir_entry_t*)sector;
10384

10485
for (int j = 0; j < 16; j++) {
105-
10686
if (entry[j].filename[0] == 0x00)
10787
return -1;
10888

10989
if (strncmp(entry[j].filename, filename, 11) == 0) {
110-
111-
uint32_t file_cluster =
112-
((uint32_t)entry[j].first_cluster_high << 16) |
113-
entry[j].first_cluster_low;
114-
90+
uint32_t file_cluster = ((uint32_t)entry[j].first_cluster_high << 16) | entry[j].first_cluster_low;
11591
uint32_t offset = 0;
11692

11793
while (file_cluster < FAT32_EOC && offset < size) {
118-
119-
uint32_t file_lba = fs.data_start_lba +
120-
(file_cluster - 2) * fs.bs.sectors_per_cluster;
121-
94+
uint32_t file_lba = fs.data_start_lba + (file_cluster - 2) * fs.bs.sectors_per_cluster;
12295
read_sector(file_lba, buffer + offset);
123-
12496
offset += fs.bs.sectors_per_cluster * fs.bs.bytes_per_sector;
125-
12697
file_cluster = fat32_get_next_cluster(file_cluster);
12798
}
128-
12999
return offset;
130100
}
131101
}
132102
}
133-
134103
cluster = fat32_get_next_cluster(cluster);
135104
}
136-
137105
return -1;
138106
}

VFS/vfs.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ void vfs_init(fs_t fsname) {
1818
set_fs(OFS);
1919
break;
2020
case FAT12:
21-
fat12_init();
21+
fat12_init(0);
2222
set_fs(FAT12);
2323
break;
2424
case FAT16:
25-
fat16_init();
25+
fat16_init(0);
2626
set_fs(FAT16);
2727
break;
2828
case FAT32:
29-
fat32_init();
29+
fat32_init(0);
3030
set_fs(FAT32);
3131
break;
3232
}

0 commit comments

Comments
 (0)