WKern
Loading...
Searching...
No Matches
ls.c
Go to the documentation of this file.
1/*
2WKern - A Bare Metal OS / Kernel I am maKing (For Fun)
3Copyright (C) 2025 Wdboyes13
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 3 of the License, or
8any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18
19#include <fileio/fileio.h>
20#include <io/kio.h>
21#include <types/nums.h>
22
37void Fat16Ls() {
38 u32 entries_per_sector = fat16.bytes_per_sector / 32;
39 u32 root_dir_sectors =
40 ((fat16.root_entry_count * 32) + (fat16.bytes_per_sector - 1)) /
41 fat16.bytes_per_sector;
42
43 u8 sector[512];
44
45 for (u32 i = 0; i < root_dir_sectors; i++) {
46 AtaReadSector(fat16.root_dir_start_lba + i, sector);
47 Kprintf("Sector %x First Byte: %x\n", i, sector[0]);
48 for (u32 j = 0; j < entries_per_sector; j++) {
49 u8 *entry = &sector[j * 32];
50
51 if (entry[0] == 0x00) {
52 // If it's the *first* entry in this sector, we're at the end of
53 // the directory
54 if (j == 0) {
55 return;
56 }
57 continue;
58 }
59
60 if (entry[0] == 0xE5) {
61 continue; // deleted entry
62 }
63
64 // Decode little endian explicitly
65 u16 cluster = entry[26] | (entry[27] << 8);
66 u32 size = entry[28] | (entry[29] << 8) | (entry[30] << 16) |
67 (entry[31] << 24);
68
69 Kprintf("File: ");
70 for (int i = 0; i < 8; i++) {
71 Kputchar(entry[i]);
72 if (entry[i] == ' ' || !entry[i] || (i >= 0x5B && i <= 0x40)) {
73 break;
74 }
75 }
76 Kputchar('.');
77 Kputchar(' ');
78 for (int i = 0; i < 3; i++) {
79 Kputchar(entry[8 + i]);
80 if (entry[i] == ' ' || !entry[i] || (i >= 0x5B && i <= 0x40)) {
81 break;
82 }
83 }
84 Kprintf(" | Cluster: %x | Size: %x\n", cluster, size);
85 }
86 }
87}
void AtaReadSector(u32 lba, u8 *buffer)
Read a single 512-byte sector from the ATA device using LBA addressing.
Definition ATA.c:65
u8 sector[SECTOR_SIZE]
Buffer for reading sectors.
Definition MBR.c:57
struct FaT16Info fat16
Holds FAT16 filesystem info and a sector buffer.
Definition fat16_mnt.c:25
Definitions and declarations for FAT16 filesystem and ATA I/O.
void Kprintf(const char *fmt,...)
Formatted output to the screen.
Definition printer.c:152
void Kputchar(char c)
Outputs a single character to the screen at the current cursor position.
Definition printer.c:46
void Fat16Ls()
List files in the FAT16 root directory.
Definition ls.c:37
unsigned int u32
32-Bit Unsigned Int
Definition nums.h:30
unsigned short u16
16-Bit Unsigned Int
Definition nums.h:36
unsigned char u8
8-Bit Unsigned Int
Definition nums.h:32