WKern
Loading...
Searching...
No Matches
mkfile.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 Mkfile(const char *filename, const char *ext) {
42 int free = -1;
43 int sector_to_write = -1;
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 u16 clust = 0;
51
52 // Find free root directory entry
53 for (u32 i = 0; i < root_dir_sectors; i++) {
54 AtaReadSector(fat16.root_dir_start_lba + i, sector);
55 for (u32 j = 0; j < entries_per_sector; j++) {
56 u8 *entry = &sector[j * 32];
57 if (entry[0] == 0x00 || entry[0] == 0xE5) {
58 free = j;
59 sector_to_write = i;
60 goto found_free_entry; // breaK out of both loops ASAP
61 }
62 }
63 }
64
65found_free_entry:
66 if (free == -1 || sector_to_write == -1) {
67 Kprintf("No free root directory entry found!\n");
68 return;
69 }
70
71 // Find free cluster
72 for (u16 c = 2; c < fat16.total_cluster; c++) {
73 u32 fat_offset = c * 2;
74 u32 fat_sector = fat16.fat_start_lba + (fat_offset / 512);
75 u32 fat_index = fat_offset % 512;
76
77 u8 fatbuf[512];
78 AtaReadSector(fat_sector, fatbuf);
79
80 if (fatbuf[fat_index] == 0x00 && fatbuf[fat_index + 1] == 0x00) {
81 clust = c;
82 fatbuf[fat_index] = 0xFF;
83 fatbuf[fat_index + 1] = 0xFF;
84 AtaWriteSector(fat_sector, fatbuf);
85 break;
86 }
87 }
88
89 if (clust == 0) {
90 Kprintf("No free cluster found!\n");
91 return;
92 }
93
94 // Now write the root directory entry
95 AtaReadSector(fat16.root_dir_start_lba + sector_to_write,
96 sector); // Re-read sector for safety
97 u8 *entry = &sector[free * 32];
98
99 // Clear entry bytes (optional but good practice)
100 for (int i = 0; i < 32; i++) {
101 entry[i] = 0x00;
102 }
103
104 char name_pad[8];
105 char ext_pad[3];
106 Padname(filename, name_pad, 8);
107 Padname(ext, ext_pad, 3);
108 Kmemcpy(entry + 0x00, name_pad, 8);
109 Kmemcpy(entry + 0x08, ext_pad, 3);
110 entry[0x0B] = 0x20; // Attribute: Archive
111
112 // Time and Date (optional, set 0 here)
113 entry[0x0E] = 0;
114 entry[0x0F] = 0;
115 entry[0x10] = 0;
116 entry[0x11] = 0;
117
118 // First cluster (little endian)
119 entry[0x1A] = clust & 0xFF;
120 entry[0x1B] = (clust >> 8) & 0xFF;
121
122 // File size = 0 for new file
123 entry[0x1C] = 0;
124 entry[0x1D] = 0;
125 entry[0x1E] = 0;
126 entry[0x1F] = 0;
127
128 AtaWriteSector(fat16.root_dir_start_lba + sector_to_write, sector);
129 Kprintf("File entry written successfully.\n");
130}
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
void * Kmemcpy(void *dest, const void *src, unsigned int count)
Copy memory.
Definition memutil.c:38
void Mkfile(const char *filename, const char *ext)
Create a new empty file entry in the FAT16 root directory.
Definition mkfile.c:41
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