WKern
Loading...
Searching...
No Matches
printconts.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 <mem/kmem.h>
22#include <types/nums.h>
23#include <utils/util.h>
24
40void Fileconts(const char *filename, const char *ext) {
41 u32 entries_per_sector = fat16.bytes_per_sector / 32;
42 u32 root_dir_sectors =
43 ((fat16.root_entry_count * 32) + (fat16.bytes_per_sector - 1)) /
44 fat16.bytes_per_sector;
45
46 u8 sector[512];
47
48 for (u32 i = 0; i < root_dir_sectors; i++) {
49 AtaReadSector(fat16.root_dir_start_lba + i, sector);
50 for (u32 j = 0; j < entries_per_sector; j++) {
51 // Padding entries to be exactly 8 and 3, and upercased (blame msft
52 // for FAT16)
53 u8 *entry = &sector[j * 32];
54 char name_pad[8];
55 char ext_pad[3];
56 Padname(filename, name_pad, 8);
57 Padname(ext, ext_pad, 3);
58
59 if (entry[0] != 0x00 &&
60 entry[0] != 0xE5 && // ChecK if entry is not deleted & exists
61 Kmemcmp(entry + 0x00, name_pad, 8) == 0 && // Compare name
62 Kmemcmp(entry + 0x08, ext_pad, 3) == 0) { // Compare ext
63 u16 clust = entry[0x1A] | (entry[0x1B] << 8);
64 if (clust < 2) {
65 Kprintf("Invalid Cluster\n");
66 return;
67 }
68 u32 lba = fat16.data_start_lba +
69 ((clust - 2) * fat16.sectors_per_cluster);
70
71 u8 data[512] = {0};
72 AtaReadSector(lba, data); // Read data in the FAT16 Sector
73 for (int i = 0; i < 512; i++) {
74 if (data[i] == 0x1A) {
75 break; // EOF
76 }
77 if (data[i] >= 0x20 && data[i] <= 0x7E) {
78 Kputchar(data[i]); // printable ASCII
79 } else {
80 Kputchar('.'); // non-printable gets a dot
81 }
82 }
83 Kputchar('\n');
84 }
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
int Kmemcmp(const void *a, const void *b, size_t n)
Compare memory.
Definition memutil.c:59
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
void Fileconts(const char *filename, const char *ext)
Display the contents of a file in the FAT16 filesystem.
Definition printconts.c:40