Used filesys.h in kernel

This commit is contained in:
2024-05-26 14:13:46 +02:00
parent dd3b5c000f
commit 01323c5874
2 changed files with 93 additions and 150 deletions
+9 -4
View File
@@ -16,13 +16,17 @@ typedef struct {
char* filetable = (char*)FILETABLE_LOC; // Pretty sure we don't have to care about the bootloader at 0x7c00 char* filetable = (char*)FILETABLE_LOC; // Pretty sure we don't have to care about the bootloader at 0x7c00
fileEntry_t* findFile(char* filename) { // TODO: Don't modify the filename string. (maybe make a copy??) fileEntry_t* findFile(const char* filename) {
char* filetype = strchr(filename, '.'); if (strlen(filename) > 14) { return NULL; } // Filename too long
static char filenameBuf[16];
strcpy(filenameBuf, filename);
char* filetype = strchr(filenameBuf, '.');
*filetype++ = '\0'; // Split filename and filetype by a NULL *filetype++ = '\0'; // Split filename and filetype by a NULL
char* ft = filetable; char* ft = filetable;
while (*ft != '\0') { while (*ft != '\0') {
if (strcmp(filename, ft) != 0) { // Filename doesn't match if (strcmp(filenameBuf, ft) != 0) { // Filename doesn't match
ft += 16; // Goto next entry ft += 16; // Goto next entry
continue; continue;
} }
@@ -58,7 +62,8 @@ void writeFile(char* filename, void* buffer) { // TODO: Add length so we don't w
file = (fileEntry_t*)filetable; file = (fileEntry_t*)filetable;
while (*(char*)file != '\0') { file++; } // Find end of the filetable while (*(char*)file != '\0') { file++; } // Find end of the filetable
// strchr(filename, '.'); TODO: We don't have to do this, because findFile() modifies the string like this *strchr(filename, '.') = '\0';
char* filetype = filename + strlen(filename); // + 1? char* filetype = filename + strlen(filename); // + 1?
fileEntry_t* previous = file - 1; fileEntry_t* previous = file - 1;
+84 -146
View File
@@ -1,34 +1,35 @@
#include <memory/physical.h>
#include <disk/disk.h>
#include <screen/text.h>
#include <interrupt/idt.h> #include <interrupt/idt.h>
#include <interrupt/exceptions.h> #include <interrupt/exceptions.h>
#include <interrupt/syscall.h> #include <interrupt/syscall.h>
#include <interrupt/pic.h> #include <interrupt/pic.h>
#include <ports/io.h>
#include <string.h>
#include <screen/graphics.h>
#include <keyboard/keyboard.h>
#include <interrupt/pit.h> #include <interrupt/pit.h>
#include <interrupt/rtc.h> #include <interrupt/rtc.h>
#include <time.h> #include <keyboard/keyboard.h>
#include <sound/pcspk.h>
#include <sound/notes.h> #include <memory/physical.h>
#include <memory/virtual.h> #include <memory/virtual.h>
#include <memory/addresses.h> #include <memory/addresses.h>
#include <memory/malloc.h> #include <memory/malloc.h>
#include <disk/filesys.h>
#include <disk/disk.h>
#include <screen/text.h>
#include <screen/graphics.h>
#include <sound/pcspk.h>
#include <sound/notes.h>
#include <ports/io.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <time.h>
#define PROMPT "#> " #define PROMPT "#> "
char* filetable = (char*)FILETABLE_LOC; // Pretty sure we don't have to care about the bootloader at 0x7c00
// Function declarations // Function declarations
void printFiletable(char* ft);
void printPhysicalMemmap(); void printPhysicalMemmap();
__attribute__ ((section("entry"))) __attribute__ ((section("entry")))
void main() { void main() {
diskRead(5, 1, filetable);
// Interrupt setup // Interrupt setup
setupIdt(); setupIdt();
setupExceptions(); setupExceptions();
@@ -71,12 +72,10 @@ void main() {
deinitMemoryRegion(gfxMode->physicalBasePtr, fbSize * BLOCK_SIZE); deinitMemoryRegion(gfxMode->physicalBasePtr, fbSize * BLOCK_SIZE);
// Screen setup // Screen setup
diskRead(46, 4, font); // TODO: Find font in filetable readFile("term16n.fnt", font);
clear(); clear();
print("kern.\n\n"); print("kern.\n\n");
diskRead(5, 1, filetable); // Read the filetable and store it in memory
// Run Interactive Shell Program // Run Interactive Shell Program
// TODO: Make it in another file // TODO: Make it in another file
@@ -269,147 +268,86 @@ void main() {
print("\nTests Finished!\n"); print("\nTests Finished!\n");
} else { } else {
char* ft = filetable; fileEntry_t* file = findFile(input);
uint8_t found = 0;
if (file == NULL) {
while (*ft != '\0') {
char filename[15] = { 0 };
uint8_t offset = 0;
for (uint8_t i = 0; i < 14; i++) {
if (i == 9) {
filename[offset++] = '.';
continue;
}
if (*ft != '\0') { filename[offset++] = *ft; }
ft++;
}
if (strcmp(input, filename) == 0) {
ft++; // RESERVED
uint8_t sector = *ft++;
uint8_t size = *ft++;
found++; // Set found to 1
char* fileType = strchr(filename, '.') + 1;
uint32_t neededPages = (size * 512) / PAGE_SIZE;
if ((size * 512) % PAGE_SIZE > 0) { neededPages++; }
char* entryPoint = (char*)0x400000; // Right after first 4MB
for (uint32_t i = 0; i < neededPages; i++) {
ptEntry_t page = 0;
uint32_t physicalAddress = (uint32_t)allocPage(&page);
if (mapPage((void*)physicalAddress, (void*)(entryPoint + i * PAGE_SIZE)) != 0) {
fgColor = RED;
print("\nNot enough memory to allocate!\n");
fgColor = FG_COLOR;
break;
}
}
diskRead(sector, size, entryPoint);
if (strcmp(fileType, "bin") == 0) {
// Reset malloc
mallocListHead = NULL;
mallocVirtualAddr = (uint32_t)entryPoint + neededPages * PAGE_SIZE;
mallocPhysicalAddr = 0;
mallocPages = 0;
((void(*)(void))entryPoint)(); // Call program
for (uint32_t i = 0, virtualAddr = mallocVirtualAddr; i < mallocPages; i++, virtualAddr += PAGE_SIZE) {
ptEntry_t* page = getPage(virtualAddr);
if (PAGE_PHYS_ADDRESS(page) && TEST_ATTR(page, PTE_PRESENT)) {
freePage(page);
unmapPage((void*)virtualAddr);
flushTlbEntry(virtualAddr);
}
}
mallocListHead = NULL;
mallocVirtualAddr = 0;
mallocPhysicalAddr = 0;
mallocPages = 0;
} else if (strcmp(fileType, "tab") == 0) {
printFiletable(entryPoint);
} else if (strcmp(fileType, "fnt") == 0) {
print("Loading ");
print(filename);
print("...\n");
memcpy(entryPoint, font, size * 512);
print("Font loaded\n");
} else {
for (size_t i = 0; i < size * 512; i++) {
if (entryPoint[i] != '\0') {
putc(entryPoint[i]);
}
}
putc('\n');
}
for (uint32_t i = 0, virtualAddr = (uint32_t)entryPoint; i < neededPages; i++, virtualAddr += PAGE_SIZE) {
ptEntry_t* page = getPage(virtualAddr);
if (PAGE_PHYS_ADDRESS(page) && TEST_ATTR(page, PTE_PRESENT)) { // Check if the page is used
freePage(page);
unmapPage((void*)virtualAddr);
flushTlbEntry(virtualAddr); // Invalidate unused page
}
}
break;
}
ft += 3;
}
if (!found) {
print("Command not found: "); print("Command not found: ");
print(input); print(input);
putc('\n'); putc('\n');
}
}
}
}
void printFiletable(char* ft) {
while (*ft != '\0') {
char filename[15] = { 0 };
uint8_t offset = 0;
for (uint8_t i = 0; i < 14; i++) {
if (i == 9) {
filename[offset++] = '.';
continue; continue;
} }
if (*ft != '\0') { filename[offset++] = *ft; }
ft++;
}
ft++; // RESERVED
uint8_t sector = *ft++; uint32_t neededPages = (file->length * 512) / PAGE_SIZE;
uint8_t size = *ft++; if ((file->length * 512) % PAGE_SIZE > 0) { neededPages++; }
if (sector < 10) putc('0'); char* entryPoint = (char*)0x400000; // Right after first 4MB
if (sector == 0) putc('0'); for (uint32_t i = 0; i < neededPages; i++) {
else print(itoa(sector, 10)); ptEntry_t page = 0;
print(": "); uint32_t physicalAddress = (uint32_t)allocPage(&page);
print(filename);
if (mapPage((void*)physicalAddress, (void*)(entryPoint + i * PAGE_SIZE)) != 0) {
fgColor = RED;
print("\nNot enough memory to allocate!\n");
fgColor = FG_COLOR;
break;
}
}
for (uint8_t i = 14 - strlen(filename); i > 0; i--) { readFile(input, entryPoint);
putc(' ');
}
print(" | "); if (strcmp(file->filetype, "bin") == 0) {
if (size >> 1) { // Reset malloc
print(itoa(size >> 1, 10)); mallocListHead = NULL;
if (size & 0x01) { print(".5"); } mallocVirtualAddr = (uint32_t)entryPoint + neededPages * PAGE_SIZE;
print("kB\n"); mallocPhysicalAddr = 0;
} else { mallocPages = 0;
print(itoa(size*512, 10));
print("B\n"); ((void(*)(void))entryPoint)(); // Call program
for (uint32_t i = 0, virtualAddr = mallocVirtualAddr; i < mallocPages; i++, virtualAddr += PAGE_SIZE) {
ptEntry_t* page = getPage(virtualAddr);
if (PAGE_PHYS_ADDRESS(page) && TEST_ATTR(page, PTE_PRESENT)) {
freePage(page);
unmapPage((void*)virtualAddr);
flushTlbEntry(virtualAddr);
}
}
mallocListHead = NULL;
mallocVirtualAddr = 0;
mallocPhysicalAddr = 0;
mallocPages = 0;
} else if (strcmp(file->filetype, "tab") == 0) {
printFiletable(entryPoint);
} else if (strcmp(file->filetype, "fnt") == 0) {
print("Loading ");
print(file->filename);
print("...\n");
memcpy(entryPoint, font, file->length * 512);
print("Font loaded\n");
} else {
for (size_t i = 0; i < file->length * 512; i++) {
if (entryPoint[i] != '\0') {
putc(entryPoint[i]);
}
}
putc('\n');
}
for (uint32_t i = 0, virtualAddr = (uint32_t)entryPoint; i < neededPages; i++, virtualAddr += PAGE_SIZE) {
ptEntry_t* page = getPage(virtualAddr);
if (PAGE_PHYS_ADDRESS(page) && TEST_ATTR(page, PTE_PRESENT)) { // Check if the page is used
freePage(page);
unmapPage((void*)virtualAddr);
flushTlbEntry(virtualAddr); // Invalidate unused page
}
}
} }
} }
outw(0x604, 0x2000); // QEMU - Quit emulator
while (1); // Just hang
} }
void printPhysicalMemmap() { void printPhysicalMemmap() {