WKern
Loading...
Searching...
No Matches
write.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
41void Writefile(const char *filename, const char *ext, const char *data,
42 u32 size) {
43 Kprintf("Writing");
44 u32 entries_per_sector = fat16.bytes_per_sector / 32;
45 u32 root_dir_sectors =
46 ((fat16.root_entry_count * 32) + (fat16.bytes_per_sector - 1)) /
47 fat16.bytes_per_sector;
48
49 u8 sector[512];
50
51 for (u32 i = 0; i < root_dir_sectors; i++) {
52 AtaReadSector(fat16.root_dir_start_lba + i, sector);
53 Kprintf("ChecKing for file\n");
54 for (u32 j = 0; j < entries_per_sector; j++) {
55 u8 *entry = &sector[j * 32];
56
57 char name_pad[8];
58 char ext_pad[3];
59 Padname(filename, name_pad, 8);
60 Padname(ext, ext_pad, 3);
61
62 if (entry[0] != 0x00 && entry[0] != 0xE5 &&
63 Kmemcmp(entry + 0x00, name_pad, 8) == 0 &&
64 Kmemcmp(entry + 0x08, ext_pad, 3) == 0) {
65 u16 clust = entry[0x1A] | (entry[0x1B] << 8);
66 Kprintf("Found File\n");
67 if (clust < 2) {
68 Kprintf("Invalid Cluster\n");
69 return;
70 }
71
72 u32 data_sector = fat16.data_start_lba +
73 ((clust - 2) * fat16.sectors_per_cluster);
74
75 u8 writebuf[512] = {0};
76 Kmemcpy(writebuf, data,
77 size < 512 ? size
78 : 512); // memcpy data into buffer to write
79 AtaWriteSector(data_sector, writebuf); // write data to sector
80
81 Kprintf("Size: %x\n", size);
82 entry[0x1C] = size & 0xFF; // Update entries
83 entry[0x1D] = (size >> 8) & 0xFF;
84 entry[0x1E] = (size >> 16) & 0xFF;
85 entry[0x1F] = (size >> 24) & 0xFF;
86
87 AtaWriteSector(fat16.root_dir_start_lba + i,
88 sector); // Write updated entries
89
90 Kprintf("Wrote to file");
91 return;
92 }
93 }
94 }
95}
void AtaWriteSector(u32 lba, const u8 *buffer)
Write a single 512-byte sector to the ATA device using LBA addressing.
Definition ATA.c:100
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
int Kmemcmp(const void *a, const void *b, size_t n)
Compare memory.
Definition memutil.c:59
void * Kmemcpy(void *dest, const void *src, unsigned int count)
Copy memory.
Definition memutil.c:38
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 Writefile(const char *filename, const char *ext, const char *data, u32 size)
Write data to an existing file in the FAT16 filesystem.
Definition write.c:41