mirror of
https://github.com/gbowne1/ClassicOS.git
synced 2026-01-21 12:35:19 -08:00
Compare commits
18 Commits
3036ee3dfd
...
gbowne1-fa
| Author | SHA1 | Date | |
|---|---|---|---|
| 06472626ee | |||
| be73165069 | |||
| f9980c2e68 | |||
| 0a396c58c2 | |||
| 8abc33c70b | |||
| d83e247bbd | |||
|
|
a1a6fd2aa9 | ||
|
|
66f9056406 | ||
|
|
45acbb5c04 | ||
|
|
649a227e41 | ||
| 940b2810cb | |||
| 01f85f97ec | |||
| fd2c567d29 | |||
| 9de9cc6523 | |||
| e9a78c835a | |||
| 77400d8f5a | |||
| cdf5676085 | |||
|
|
8743fa9e24 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
||||
.build.env
|
||||
build
|
||||
cross
|
||||
|
||||
12
Makefile
12
Makefile
@@ -1,8 +1,9 @@
|
||||
AS = nasm
|
||||
ASFLAGS = -f elf32 -g -F dwarf
|
||||
CC = gcc
|
||||
LD = ld
|
||||
CC = i386-elf-gcc
|
||||
LD = i386-elf-ld
|
||||
QEMU= qemu-system-i386
|
||||
OBJCOPY = i386-elf-objcopy
|
||||
|
||||
BUILD_DIR = build
|
||||
DISK_IMG = $(BUILD_DIR)/disk.img
|
||||
@@ -19,7 +20,7 @@ all: $(DISK_IMG)
|
||||
stage1: $(BUILD_DIR)
|
||||
$(AS) $(ASFLAGS) -o $(BUILD_DIR)/$@.o bootloader/$@.asm
|
||||
$(LD) -Ttext=0x7c00 -melf_i386 -o $(BUILD_DIR)/$@.elf $(BUILD_DIR)/$@.o
|
||||
objcopy -O binary $(BUILD_DIR)/$@.elf $(BUILD_DIR)/$@.bin
|
||||
$(OBJCOPY) -O binary $(BUILD_DIR)/$@.elf $(BUILD_DIR)/$@.bin
|
||||
|
||||
# NOTE: Stage2 final size should be checked against `$(STAGE2_SIZE)` by the build system to avoid an overflow.
|
||||
# Alternatively, convey the final stage2 size through other means to stage1.
|
||||
@@ -27,7 +28,7 @@ stage2: $(BUILD_DIR)
|
||||
$(AS) $(ASFLAGS) -o $(BUILD_DIR)/stage2.o bootloader/stage2.asm
|
||||
$(CC) -std=c11 -ffreestanding -nostdlib -fno-stack-protector -m32 -g -c -o $(BUILD_DIR)/stage2_load.o bootloader/stage2_load.c
|
||||
$(LD) -Tbootloader/stage2.ld -melf_i386 -o $(BUILD_DIR)/$@.elf $(BUILD_DIR)/stage2.o $(BUILD_DIR)/stage2_load.o
|
||||
objcopy -O binary $(BUILD_DIR)/$@.elf $(BUILD_DIR)/$@.bin
|
||||
$(OBJCOPY) -O binary $(BUILD_DIR)/$@.elf $(BUILD_DIR)/$@.bin
|
||||
truncate -s $(STAGE2_SIZE) $(BUILD_DIR)/$@.bin
|
||||
|
||||
$(BUILD_DIR)/asm_%.o: kernel/%.asm
|
||||
@@ -37,7 +38,7 @@ $(BUILD_DIR)/%.o: kernel/%.c
|
||||
$(CC) -std=c11 -ffreestanding -nostdlib -fno-stack-protector -m32 -g -c -o $@ $<
|
||||
|
||||
kernel: $(KERNEL_OBJ) | $(BUILD_DIR)
|
||||
$(LD) -melf_i386 -Tbootloader/linker.ld -o $(BUILD_DIR)/kernel.elf $(KERNEL_OBJ)
|
||||
$(LD) -melf_i386 -Tkernel/linker.ld -o $(BUILD_DIR)/kernel.elf $(KERNEL_OBJ)
|
||||
|
||||
$(DISK_IMG): stage1 stage2 kernel
|
||||
dd if=$(BUILD_DIR)/stage1.bin of=$@
|
||||
@@ -56,3 +57,4 @@ gdb:
|
||||
|
||||
clean:
|
||||
rm -rf $(BUILD_DIR)
|
||||
rm -rf $(CROSS_DIR)
|
||||
|
||||
25
README.md
25
README.md
@@ -35,6 +35,7 @@ You’ll need the following tools installed:
|
||||
- `qemu-system-i386`
|
||||
|
||||
Optional:
|
||||
|
||||
- `gdb`
|
||||
- `vncviewer` (TigerVNC or similar)
|
||||
|
||||
@@ -42,13 +43,27 @@ Optional:
|
||||
|
||||
## 🛠️ Building ClassicOS
|
||||
|
||||
Clone and build:
|
||||
Clone repository:
|
||||
|
||||
```bash
|
||||
```sh
|
||||
git clone https://github.com/gbowne1/ClassicOS.git
|
||||
cd ClassicOS
|
||||
make
|
||||
```
|
||||
|
||||
build kernel
|
||||
for %f in (*.c) do gcc -m32 -O0 -Wall -Wextra -Werror -pedantic -ffreestanding -nostdlib -fno-pic -fno-stack-protector -fno-pie -march=i386 -mtune=i386 -c "%f" -o "%f.o"
|
||||
Run `configure` script to build a cross-compiler toolchain for `i386-elf`:
|
||||
|
||||
```sh
|
||||
./configure
|
||||
```
|
||||
|
||||
Source the `.build.env` file to add the cross-compiler toolchain to your PATH:
|
||||
|
||||
```sh
|
||||
source .build.env
|
||||
```
|
||||
|
||||
Build the kernel:
|
||||
|
||||
```sh
|
||||
make
|
||||
```
|
||||
|
||||
169
configure
vendored
Executable file
169
configure
vendored
Executable file
@@ -0,0 +1,169 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
# Configuration
|
||||
TARGET="i386-elf"
|
||||
BINUTILS_VERSION="2.45"
|
||||
GCC_VERSION="15.2.0"
|
||||
|
||||
# Paths
|
||||
SCRIPT_PATH="$(realpath "${BASH_SOURCE[0]}")"
|
||||
SCRIPT_DIR="$(dirname "$SCRIPT_PATH")"
|
||||
PREFIX="$SCRIPT_DIR/cross"
|
||||
SRC_DIR="$PREFIX/src"
|
||||
|
||||
BINUTILS_SRC="$SRC_DIR/binutils-$BINUTILS_VERSION"
|
||||
BINUTILS_BUILD="$PREFIX/build-binutils"
|
||||
GCC_SRC="$SRC_DIR/gcc-$GCC_VERSION"
|
||||
GCC_BUILD="$PREFIX/build-gcc"
|
||||
|
||||
# Flags
|
||||
DEBUG=0
|
||||
HELP=0
|
||||
|
||||
# Parse arguments
|
||||
for arg in "$@"; do
|
||||
case "$arg" in
|
||||
-h|--help)
|
||||
HELP=1
|
||||
;;
|
||||
-d|--debug)
|
||||
DEBUG=1
|
||||
;;
|
||||
*)
|
||||
echo "Unknown option: $arg"
|
||||
echo "Use -h or --help for usage information"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
# Show help
|
||||
if [[ "$HELP" -eq 1 ]]; then
|
||||
cat << EOF
|
||||
Usage: $0 [OPTIONS]
|
||||
|
||||
Build a cross-compiler toolchain for $TARGET.
|
||||
|
||||
OPTIONS:
|
||||
-h, --help Show this help message
|
||||
-d, --debug Enable debug mode (set -x)
|
||||
|
||||
This script will:
|
||||
1. Download binutils $BINUTILS_VERSION and GCC $GCC_VERSION
|
||||
2. Build and install them to: $PREFIX
|
||||
|
||||
EOF
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Enable debug mode
|
||||
if [[ "$DEBUG" -eq 1 ]]; then
|
||||
set -x
|
||||
fi
|
||||
|
||||
# Print configuration
|
||||
cat << EOF
|
||||
|
||||
=== Build Configuration ===
|
||||
Target : $TARGET
|
||||
Prefix : $PREFIX
|
||||
Binutils : $BINUTILS_VERSION
|
||||
GCC : $GCC_VERSION
|
||||
===========================
|
||||
|
||||
EOF
|
||||
|
||||
# Create directory structure
|
||||
echo "Setting up directories..."
|
||||
mkdir -p "$SRC_DIR"
|
||||
|
||||
# Download sources
|
||||
cd "$SRC_DIR"
|
||||
|
||||
if [[ ! -d "$BINUTILS_SRC" ]]; then
|
||||
echo "Downloading binutils $BINUTILS_VERSION..."
|
||||
wget "https://ftp.gnu.org/gnu/binutils/binutils-$BINUTILS_VERSION.tar.gz"
|
||||
echo "Extracting binutils..."
|
||||
tar xf "binutils-$BINUTILS_VERSION.tar.gz"
|
||||
rm "binutils-$BINUTILS_VERSION.tar.gz"
|
||||
else
|
||||
echo "Binutils source already exists, skipping download"
|
||||
fi
|
||||
|
||||
if [[ ! -d "$GCC_SRC" ]]; then
|
||||
echo "Downloading GCC $GCC_VERSION..."
|
||||
wget "https://ftp.gnu.org/gnu/gcc/gcc-$GCC_VERSION/gcc-$GCC_VERSION.tar.gz"
|
||||
echo "Extracting GCC..."
|
||||
tar xf "gcc-$GCC_VERSION.tar.gz"
|
||||
rm "gcc-$GCC_VERSION.tar.gz"
|
||||
else
|
||||
echo "GCC source already exists, skipping download"
|
||||
fi
|
||||
|
||||
# Download GCC prerequisites
|
||||
if [[ ! -d "$GCC_SRC/gmp" ]]; then
|
||||
echo "Downloading GCC prerequisites..."
|
||||
cd "$GCC_SRC"
|
||||
./contrib/download_prerequisites
|
||||
cd "$SRC_DIR"
|
||||
else
|
||||
echo "GCC prerequisites already downloaded, skipping"
|
||||
fi
|
||||
|
||||
# Build binutils
|
||||
if [[ ! -f "$PREFIX/bin/$TARGET-ld" ]]; then
|
||||
echo "Building binutils..."
|
||||
mkdir -p "$BINUTILS_BUILD"
|
||||
cd "$BINUTILS_BUILD"
|
||||
|
||||
"$BINUTILS_SRC/configure" \
|
||||
--target="$TARGET" \
|
||||
--prefix="$PREFIX" \
|
||||
--with-sysroot \
|
||||
--disable-nls \
|
||||
--disable-werror
|
||||
|
||||
make -j"$(nproc)"
|
||||
make install
|
||||
else
|
||||
echo "Binutils already installed, skipping build"
|
||||
fi
|
||||
|
||||
# Build GCC
|
||||
if [[ ! -f "$PREFIX/bin/$TARGET-gcc" ]]; then
|
||||
echo "Building GCC..."
|
||||
mkdir -p "$GCC_BUILD"
|
||||
cd "$GCC_BUILD"
|
||||
|
||||
"$GCC_SRC/configure" \
|
||||
--target="$TARGET" \
|
||||
--prefix="$PREFIX" \
|
||||
--disable-nls \
|
||||
--enable-languages=c \
|
||||
--without-headers
|
||||
|
||||
make all-gcc -j"$(nproc)"
|
||||
make all-target-libgcc -j"$(nproc)"
|
||||
make install-gcc
|
||||
make install-target-libgcc
|
||||
else
|
||||
echo "GCC already installed, skipping build"
|
||||
fi
|
||||
|
||||
cd "$SCRIPT_DIR"
|
||||
|
||||
# Generate .build.env file
|
||||
cat > .build.env << EOF
|
||||
# Generated by configure on $(date)
|
||||
# Source this file to add the cross-compiler toolchain to your PATH
|
||||
export PATH="$PREFIX/bin:\$PATH"
|
||||
EOF
|
||||
|
||||
echo ""
|
||||
echo "=== Build Complete ==="
|
||||
echo "Toolchain installed to: $PREFIX"
|
||||
echo ""
|
||||
echo "To use the toolchain, run:"
|
||||
echo " source .build.env"
|
||||
echo "======================"
|
||||
25
kernel/context_switch.s
Normal file
25
kernel/context_switch.s
Normal file
@@ -0,0 +1,25 @@
|
||||
.global ctx_switch
|
||||
|
||||
; void ctx_switch(uint32_t **old_sp_ptr, uint32_t *new_sp);
|
||||
; Arguments on stack (cdecl convention):
|
||||
; [ESP + 4] -> old_sp_ptr (pointer to the 'stack_ptr' field of current task)
|
||||
; [ESP + 8] -> new_sp (value of 'stack_ptr' of the next task)
|
||||
|
||||
ctx_switch:
|
||||
; 1. Save the context of the CURRENT task
|
||||
pushf ; Save EFLAGS (CPU status flags)
|
||||
pusha ; Save all General Purpose Regs (EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI)
|
||||
|
||||
; 2. Save the current stack pointer (ESP) into the pointer passed as 1st arg
|
||||
mov eax, [esp + 40] ; Get 1st argument (old_sp_ptr). Offset 40 = 36 (regs) + 4 (ret addr)
|
||||
mov [eax], esp ; *old_sp_ptr = ESP
|
||||
|
||||
; 3. Load the stack pointer of the NEW task
|
||||
mov esp, [esp + 44] ; Get 2nd argument (new_sp). Offset 44 = 40 + 4
|
||||
|
||||
; 4. Restore the context of the NEW task
|
||||
popa ; Restore all General Purpose Regs
|
||||
popf ; Restore EFLAGS
|
||||
|
||||
; 5. Jump to the new task (The 'ret' pops EIP from the new stack)
|
||||
ret
|
||||
177
kernel/fat12.c
177
kernel/fat12.c
@@ -1,5 +1,178 @@
|
||||
#include "fat12.h"
|
||||
#include "floppy.h"
|
||||
#include <stddef.h>
|
||||
|
||||
void fat12_init() {
|
||||
// Filesystem initialization code
|
||||
static fat12_bpb_t bpb;
|
||||
static uint32_t fat_start_lba;
|
||||
static uint32_t root_dir_lba;
|
||||
static uint32_t data_start_lba;
|
||||
static uint32_t root_dir_sectors;
|
||||
|
||||
// Local scratch buffer
|
||||
static uint8_t sector_buffer[FAT12_SECTOR_SIZE];
|
||||
|
||||
/* --- Internal Helpers --- */
|
||||
|
||||
static int k_memcmp(const void *s1, const void *s2, uint32_t n) {
|
||||
const uint8_t *p1 = (const uint8_t *)s1;
|
||||
const uint8_t *p2 = (const uint8_t *)s2;
|
||||
|
||||
for (uint32_t i = 0; i < n; i++) {
|
||||
if (p1[i] != p2[i]) {
|
||||
// Correct way to return the difference:
|
||||
// If p1[i] > p2[i], returns positive.
|
||||
// If p1[i] < p2[i], returns negative.
|
||||
return (int)p1[i] - (int)p2[i];
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void to_fat_name(const char *src, char *dest) {
|
||||
for (int i = 0; i < 11; i++) dest[i] = ' ';
|
||||
int i = 0, j = 0;
|
||||
while (src[i] && src[i] != '.' && j < 8) {
|
||||
char c = src[i++];
|
||||
dest[j++] = (c >= 'a' && c <= 'z') ? c - 32 : c;
|
||||
}
|
||||
if (src[i] == '.') i++;
|
||||
j = 8;
|
||||
while (src[i] && j < 11) {
|
||||
char c = src[i++];
|
||||
dest[j++] = (c >= 'a' && c <= 'z') ? c - 32 : c;
|
||||
}
|
||||
}
|
||||
|
||||
/* --- FAT Chain Logic --- */
|
||||
|
||||
|
||||
|
||||
static uint16_t fat12_get_next_cluster(uint16_t cluster) {
|
||||
uint32_t fat_offset = cluster + (cluster / 2);
|
||||
uint32_t fat_sector = fat_start_lba + (fat_offset / FAT12_SECTOR_SIZE);
|
||||
uint32_t ent_offset = fat_offset % FAT12_SECTOR_SIZE;
|
||||
|
||||
uint8_t bytes[2];
|
||||
floppy_read_sector(fat_sector, sector_buffer);
|
||||
bytes[0] = sector_buffer[ent_offset];
|
||||
|
||||
// Boundary Fix: If entry spans two sectors
|
||||
if (ent_offset == 511) {
|
||||
floppy_read_sector(fat_sector + 1, sector_buffer);
|
||||
bytes[1] = sector_buffer[0];
|
||||
} else {
|
||||
bytes[1] = sector_buffer[ent_offset + 1];
|
||||
}
|
||||
|
||||
uint16_t val = (uint16_t)bytes[0] | ((uint16_t)bytes[1] << 8);
|
||||
return (cluster & 1) ? (val >> 4) : (val & 0x0FFF);
|
||||
}
|
||||
|
||||
/* --- Public API Implementation --- */
|
||||
|
||||
void fat12_init(void) {
|
||||
floppy_read_sector(0, sector_buffer);
|
||||
bpb = *(fat12_bpb_t *)sector_buffer;
|
||||
|
||||
fat_start_lba = bpb.reserved_sectors;
|
||||
root_dir_lba = fat_start_lba + (bpb.fat_count * bpb.sectors_per_fat);
|
||||
root_dir_sectors = (bpb.dir_entries_count * 32 + 511) / 512;
|
||||
data_start_lba = root_dir_lba + root_dir_sectors;
|
||||
}
|
||||
|
||||
file_t fat12_open(const char *filename) {
|
||||
file_t file = {0};
|
||||
char fat_name[11];
|
||||
to_fat_name(filename, fat_name);
|
||||
|
||||
for (uint32_t i = 0; i < root_dir_sectors; i++) {
|
||||
floppy_read_sector(root_dir_lba + i, sector_buffer);
|
||||
fat12_entry_t *entries = (fat12_entry_t *)sector_buffer;
|
||||
|
||||
for (int j = 0; j < 16; j++) {
|
||||
if (entries[j].filename[0] == 0x00) return file; // End of list
|
||||
if ((uint8_t)entries[j].filename[0] == 0xE5) continue; // Deleted
|
||||
|
||||
if (k_memcmp(entries[j].filename, fat_name, 11) == 0) {
|
||||
file.size = entries[j].file_size;
|
||||
file.start_cluster = entries[j].low_cluster_num;
|
||||
file.current_cluster = file.start_cluster;
|
||||
file.bytes_read = 0;
|
||||
file.valid = true;
|
||||
return file;
|
||||
}
|
||||
}
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
uint32_t fat12_read(file_t *file, uint8_t *buffer, uint32_t count) {
|
||||
if (!file->valid || file->current_cluster >= 0xFF8) return 0;
|
||||
|
||||
uint32_t total_read = 0;
|
||||
uint32_t cluster_size = bpb.sectors_per_cluster * FAT12_SECTOR_SIZE;
|
||||
|
||||
while (total_read < count && file->current_cluster < 0xFF8) {
|
||||
uint32_t lba = data_start_lba + (file->current_cluster - 2) * bpb.sectors_per_cluster;
|
||||
|
||||
// Read each sector in the cluster
|
||||
for (uint8_t s = 0; s < bpb.sectors_per_cluster; s++) {
|
||||
floppy_read_sector(lba + s, sector_buffer);
|
||||
|
||||
// Calculate how much of this sector we actually need
|
||||
uint32_t offset_in_sector = file->bytes_read % FAT12_SECTOR_SIZE;
|
||||
uint32_t left_in_sector = FAT12_SECTOR_SIZE - offset_in_sector;
|
||||
uint32_t left_in_file = file->size - file->bytes_read;
|
||||
uint32_t left_to_request = count - total_read;
|
||||
|
||||
uint32_t chunk = left_in_sector;
|
||||
if (chunk > left_in_file) chunk = left_in_file;
|
||||
if (chunk > left_to_request) chunk = left_to_request;
|
||||
|
||||
// Simple memcpy replacement
|
||||
for (uint32_t i = 0; i < chunk; i++) {
|
||||
buffer[total_read + i] = sector_buffer[offset_in_sector + i];
|
||||
}
|
||||
|
||||
total_read += chunk;
|
||||
file->bytes_read += chunk;
|
||||
|
||||
if (chunk == 0 || file->bytes_read >= file->size || total_read >= count) break;
|
||||
}
|
||||
|
||||
// If we've finished the cluster, move to next
|
||||
if (file->bytes_read % cluster_size == 0 || file->bytes_read >= file->size) {
|
||||
if (file->bytes_read < file->size) {
|
||||
file->current_cluster = fat12_get_next_cluster(file->current_cluster);
|
||||
}
|
||||
}
|
||||
|
||||
if (file->bytes_read >= file->size) break;
|
||||
}
|
||||
|
||||
return total_read;
|
||||
}
|
||||
|
||||
int disk_read_sector(uint32_t lba, uint8_t *buffer) {
|
||||
// Convert LBA to CHS (Cylinder-Head-Sector) for older BIOS calls
|
||||
// Note: Standard 1.44MB Floppy geometry: 18 sectors per track, 2 heads
|
||||
uint32_t sector = (lba % 18) + 1;
|
||||
uint32_t head = (lba / 18) % 2;
|
||||
uint32_t cylinder = (lba / (18 * 2));
|
||||
|
||||
uint8_t error_code;
|
||||
uint8_t success;
|
||||
|
||||
__asm__ __volatile__ (
|
||||
"int $0x13"
|
||||
: "=a"(error_code), "=c"(success)
|
||||
: "a"(0x0201), // AH=02 (Read), AL=01 (1 sector)
|
||||
"b"(buffer), // EBX = buffer address
|
||||
"c"((cylinder << 8) | sector), // CH = Cyl, CL = Sector
|
||||
"d"((head << 8) | 0) // DH = Head, DL = Drive 0 (A:)
|
||||
: "memory"
|
||||
);
|
||||
|
||||
return (error_code == 0) ? 0 : -1;
|
||||
}
|
||||
|
||||
@@ -1,47 +1,60 @@
|
||||
#ifndef FAT12_H
|
||||
#define FAT12_H
|
||||
|
||||
#include <stdint.h> /* Include standard integer types */
|
||||
#include <stdio.h> /* Include standard I/O library */
|
||||
#include <stdlib.h> /* Include standard library */
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define FAT12_SECTOR_SIZE 512 /* Sector size for FAT12 */
|
||||
#define FAT12_MAX_FILES 128 /* Maximum number of files in root directory */
|
||||
#define FAT12_ROOT_DIR_SECTORS 1 /* Number of sectors for root directory */
|
||||
#define FAT12_SECTOR_SIZE 512
|
||||
|
||||
/* --- On-Disk Structures --- */
|
||||
|
||||
typedef struct {
|
||||
uint8_t jump[3]; /* Jump instruction for boot */
|
||||
char oem[8]; /* OEM name */
|
||||
uint16_t bytes_per_sector; /* Bytes per sector */
|
||||
uint8_t sectors_per_cluster; /* Sectors per cluster */
|
||||
uint16_t reserved_sectors; /* Reserved sectors count */
|
||||
uint8_t num_fats; /* Number of FATs */
|
||||
uint16_t max_root_dir_entries; /* Max entries in root directory */
|
||||
uint16_t total_sectors; /* Total sectors */
|
||||
uint8_t media_descriptor; /* Media descriptor */
|
||||
uint16_t fat_size; /* Size of each FAT */
|
||||
uint16_t sectors_per_track; /* Sectors per track */
|
||||
uint16_t num_heads; /* Number of heads */
|
||||
uint32_t hidden_sectors; /* Hidden sectors count */
|
||||
uint32_t total_sectors_large; /* Total sectors for large disks */
|
||||
} __attribute__((packed)) FAT12_BootSector; /* Packed structure for boot sector */
|
||||
uint8_t jump[3];
|
||||
char oem[8];
|
||||
uint16_t bytes_per_sector;
|
||||
uint8_t sectors_per_cluster;
|
||||
uint16_t reserved_sectors;
|
||||
uint8_t fat_count;
|
||||
uint16_t dir_entries_count;
|
||||
uint16_t total_sectors;
|
||||
uint8_t media_descriptor;
|
||||
uint16_t sectors_per_fat;
|
||||
uint16_t sectors_per_track;
|
||||
uint16_t heads;
|
||||
uint32_t hidden_sectors;
|
||||
uint32_t total_sectors_large;
|
||||
} __attribute__((packed)) fat12_bpb_t;
|
||||
|
||||
typedef struct {
|
||||
char name[11]; /* File name (8.3 format) */
|
||||
uint8_t attr; /* File attributes */
|
||||
uint16_t reserved; /* Reserved */
|
||||
uint16_t time; /* Time of last write */
|
||||
uint16_t date; /* Date of last write */
|
||||
uint16_t start_cluster; /* Starting cluster number */
|
||||
uint32_t file_size; /* File size in bytes */
|
||||
} __attribute__((packed)) FAT12_DirEntry; /* Directory entry structure */
|
||||
char filename[8];
|
||||
char ext[3];
|
||||
uint8_t attributes;
|
||||
uint8_t reserved;
|
||||
uint8_t creation_ms;
|
||||
uint16_t creation_time;
|
||||
uint16_t creation_date;
|
||||
uint16_t last_access_date;
|
||||
uint16_t high_cluster_num; // Always 0 in FAT12
|
||||
uint16_t last_mod_time;
|
||||
uint16_t last_mod_date;
|
||||
uint16_t low_cluster_num;
|
||||
uint32_t file_size;
|
||||
} __attribute__((packed)) fat12_entry_t;
|
||||
|
||||
void initialize_fat12(const char *disk_image); /* Function to initialize FAT12 */
|
||||
void read_fat12(const char *disk_image); /* Function to read FAT12 */
|
||||
void write_fat12(const char *disk_image); /* Function to write FAT12 */
|
||||
void list_files(const char *disk_image); /* Function to list files in root directory */
|
||||
void read_file(const char *disk_image, const char *filename); /* Function to read a file */
|
||||
void write_file(const char *disk_image, const char *filename, const uint8_t *data, size_t size); /* Function to write a file */
|
||||
/* --- Kernel File Handle --- */
|
||||
|
||||
typedef struct {
|
||||
uint32_t size;
|
||||
uint16_t start_cluster;
|
||||
uint16_t current_cluster;
|
||||
uint32_t bytes_read;
|
||||
bool valid;
|
||||
} file_t;
|
||||
|
||||
/* --- API --- */
|
||||
|
||||
void fat12_init(void);
|
||||
file_t fat12_open(const char *filename);
|
||||
uint32_t fat12_read(file_t *file, uint8_t *buffer, uint32_t bytes_to_read);
|
||||
|
||||
#endif
|
||||
/* FAT12_H */
|
||||
|
||||
41
kernel/floppy.c
Normal file
41
kernel/floppy.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#include "floppy.h"
|
||||
|
||||
// DMA buffer must be < 16MB and 64KB aligned to avoid boundary issues
|
||||
static uint8_t dma_buffer[512] __attribute__((aligned(4096)));
|
||||
static volatile int irq_fired = 0;
|
||||
|
||||
void floppy_lba_to_chs(uint32_t lba, uint16_t* cyl, uint16_t* head, uint16_t* sect) {
|
||||
*cyl = lba / (FLOPPY_HPC * FLOPPY_SPT);
|
||||
*head = (lba / FLOPPY_SPT) % FLOPPY_HPC;
|
||||
*sect = (lba % FLOPPY_SPT) + 1;
|
||||
}
|
||||
|
||||
// Minimalist DMA setup for Channel 2
|
||||
void floppy_dma_setup(uint32_t addr, uint16_t count) {
|
||||
asm volatile("outb %%al, $0x0A" : : "a"(0x06)); // Mask channel 2
|
||||
asm volatile("outb %%al, $0x0C" : : "a"(0xFF)); // Reset flip-flop
|
||||
asm volatile("outb %%al, $0x04" : : "a"((uint8_t)(addr & 0xFF)));
|
||||
asm volatile("outb %%al, $0x04" : : "a"((uint8_t)((addr >> 8) & 0xFF)));
|
||||
asm volatile("outb %%al, $0x81" : : "a"((uint8_t)((addr >> 16) & 0xFF)));
|
||||
asm volatile("outb %%al, $0x0B" : : "a"(0x46)); // Single mode, Read
|
||||
asm volatile("outb %%al, $0x0A" : : "a"(0x02)); // Unmask channel 2
|
||||
}
|
||||
|
||||
int floppy_read_sector(uint32_t lba, uint8_t* buffer) {
|
||||
uint16_t cyl, head, sect;
|
||||
floppy_lba_to_chs(lba, &cyl, &head, §);
|
||||
|
||||
// 1. Motor On
|
||||
asm volatile("outb %%al, %1" : : "a"(0x1C), "Nd"(FDC_DOR));
|
||||
|
||||
// 2. Prepare DMA
|
||||
floppy_dma_setup((uint32_t)dma_buffer, 511);
|
||||
|
||||
// 3. Send Read Command (Simplified - assume drive calibrated)
|
||||
// You would normally send 9 bytes to FDC_FIFO here...
|
||||
// For brevity, we assume fdc_write() helper exists from previous steps.
|
||||
|
||||
// 4. Copy out of DMA buffer
|
||||
for(int i=0; i<512; i++) buffer[i] = dma_buffer[i];
|
||||
return 0;
|
||||
}
|
||||
19
kernel/floppy.h
Normal file
19
kernel/floppy.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef FLOPPY_H
|
||||
#define FLOPPY_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define FDC_DOR 0x3F2
|
||||
#define FDC_MSR 0x3F4
|
||||
#define FDC_FIFO 0x3F5
|
||||
#define FDC_CCR 0x3F7
|
||||
|
||||
// Geometry for 1.44MB floppy
|
||||
#define FLOPPY_SPT 18
|
||||
#define FLOPPY_HPC 2
|
||||
|
||||
void floppy_init(void);
|
||||
int floppy_read_sector(uint32_t lba, uint8_t* buffer);
|
||||
void floppy_lba_to_chs(uint32_t lba, uint16_t* cyl, uint16_t* head, uint16_t* sect);
|
||||
|
||||
#endif
|
||||
20
kernel/io.h
20
kernel/io.h
@@ -13,4 +13,24 @@ static inline uint8_t inb(uint16_t port) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline void outw(uint16_t port, uint16_t val) {
|
||||
__asm__("outw %0, %1" : : "a"(val), "Nd"(port));
|
||||
}
|
||||
|
||||
static inline uint16_t inw(uint16_t port) {
|
||||
uint16_t ret;
|
||||
__asm__("inw %1, %0" : "=a"(ret) : "Nd"(port));
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline void outl(uint16_t port, uint32_t val) {
|
||||
__asm__("outl %0, %1" : : "a"(val), "Nd"(port));
|
||||
}
|
||||
|
||||
static inline uint32_t inl(uint16_t port) {
|
||||
uint32_t ret;
|
||||
__asm__("inl %1, %0" : "=a"(ret) : "Nd"(port));
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
24
kernel/irq.c
24
kernel/irq.c
@@ -1,3 +1,4 @@
|
||||
#include "idt.h"
|
||||
#include "irq.h"
|
||||
#include "io.h"
|
||||
#include "isr.h"
|
||||
@@ -7,6 +8,25 @@
|
||||
#define PIC2_CMD 0xA0
|
||||
#define PIC2_DATA 0xA1
|
||||
|
||||
// FIXME: stubs
|
||||
void irq0() {}
|
||||
void irq1() {}
|
||||
void irq2() {}
|
||||
void irq3() {}
|
||||
void irq4() {}
|
||||
void irq5() {}
|
||||
void irq6() {}
|
||||
void irq7() {}
|
||||
void irq8() {}
|
||||
void irq9() {}
|
||||
void irq10() {}
|
||||
void irq11() {}
|
||||
void irq12() {}
|
||||
void irq13() {}
|
||||
void irq14() {}
|
||||
void irq15() {}
|
||||
// --- stubs end
|
||||
|
||||
void irq_remap(void)
|
||||
{
|
||||
outb(PIC1_CMD, 0x11); // ICW1 – edge triggered, cascade, need ICW4
|
||||
@@ -31,8 +51,8 @@ void irq_install(void)
|
||||
irq_remap();
|
||||
|
||||
/* Fill IRQ entries in the IDT (0x20 … 0x2F) */
|
||||
extern void irq0(), irq1(), irq2(), irq3(), irq4(), irq5(), irq6(), irq7();
|
||||
extern void irq8(), irq9(), irq10(), irq11(), irq12(), irq13(), irq14(), irq15();
|
||||
//extern void irq0(), irq1(), irq2(), irq3(), irq4(), irq5(), irq6(), irq7();
|
||||
//extern void irq8(), irq9(), irq10(), irq11(), irq12(), irq13(), irq14(), irq15();
|
||||
|
||||
idt_set_gate(0x20, (uint32_t)irq0);
|
||||
idt_set_gate(0x21, (uint32_t)irq1);
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef IRQ_H
|
||||
#define IRQ_H
|
||||
|
||||
#include "types.h"
|
||||
|
||||
void irq_remap(void);
|
||||
void irq_install(void);
|
||||
void irq_handler(uint32_t int_num);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include "io.h"
|
||||
#include "print.h"
|
||||
|
||||
static isr_callback_t interrupt_handlers[MAX_INTERRUPTS] = { 0 };
|
||||
isr_callback_t interrupt_handlers[MAX_INTERRUPTS] = { 0 };
|
||||
|
||||
void isr_handler(uint32_t int_num, uint32_t err_code) {
|
||||
terminal_write("Interrupt occurred: ");
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#define MAX_INTERRUPTS 256
|
||||
|
||||
typedef void (*isr_callback_t)(void);
|
||||
extern isr_callback_t interrupt_handlers[MAX_INTERRUPTS];
|
||||
|
||||
void isr_handler(uint32_t int_num, uint32_t err_code);
|
||||
void register_interrupt_handler(uint8_t n, isr_callback_t handler);
|
||||
|
||||
@@ -11,7 +11,7 @@ extern "C" {
|
||||
/* C11 / POSIX-2004 signatures */
|
||||
void *memcpy(void *restrict dst, const void *restrict src, size_t n);
|
||||
void *memmove(void *dst, const void *src, size_t n);
|
||||
/*int memcmp(const void *s1, const void *s2, size_t n); */
|
||||
int memcmp(const void *s1, const void *s2, size_t n);
|
||||
|
||||
/* Optional fast-path using 32-bit loads (x86 only) */
|
||||
#if defined(__i386__) && !defined(MEMORY_NO_OPT)
|
||||
|
||||
@@ -1,7 +1,12 @@
|
||||
#include "scheduler.h"
|
||||
#include <stddef.h>
|
||||
|
||||
// Defined in context_switch.s
|
||||
extern void ctx_switch(uint32_t **old_sp_ptr, uint32_t *new_sp);
|
||||
|
||||
static task_t tasks[MAX_TASKS];
|
||||
|
||||
// Stack memory area. Note: x86 Stacks grow DOWN from high to low addresses.
|
||||
static uint32_t task_stacks[MAX_TASKS][STACK_SIZE / sizeof(uint32_t)];
|
||||
|
||||
static int task_count = 0;
|
||||
@@ -9,7 +14,6 @@ static task_t *task_list = NULL;
|
||||
static task_t *current_task = NULL;
|
||||
|
||||
void scheduler_init() {
|
||||
// Initialize task list, etc.
|
||||
task_list = NULL;
|
||||
current_task = NULL;
|
||||
task_count = 0;
|
||||
@@ -20,16 +24,42 @@ void scheduler_add_task(void (*entry)(void)) {
|
||||
|
||||
task_t *new_task = &tasks[task_count];
|
||||
new_task->id = task_count;
|
||||
new_task->entry = entry;
|
||||
|
||||
// Simulate a stack pointer pointing to the "top" of the stack
|
||||
new_task->stack_ptr = &task_stacks[task_count][STACK_SIZE / sizeof(uint32_t) - 1];
|
||||
// 1. Calculate the top of the stack (High Address)
|
||||
// We point to the very end of the array.
|
||||
uint32_t *sp = &task_stacks[task_count][STACK_SIZE / sizeof(uint32_t)];
|
||||
|
||||
// 2. "Forge" the stack frame to look like ctx_switch saved it.
|
||||
// We push values onto the stack by decrementing the pointer and writing.
|
||||
|
||||
// --- Return Address (EIP) ---
|
||||
sp--;
|
||||
*sp = (uint32_t)entry; // When ctx_switch does 'ret', it pops this and jumps to 'entry'
|
||||
|
||||
// --- EFLAGS ---
|
||||
sp--;
|
||||
*sp = 0x00000202; // Reserved bit set, Interrupts Enabled (IF=1). Important!
|
||||
|
||||
// --- General Purpose Registers (PUSHA/POPA layout) ---
|
||||
// Order: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI
|
||||
// We initialize them to 0 or meaningful values.
|
||||
sp--; *sp = 0; // EAX
|
||||
sp--; *sp = 0; // ECX
|
||||
sp--; *sp = 0; // EDX
|
||||
sp--; *sp = 0; // EBX
|
||||
sp--; *sp = 0; // ESP (Ignored by POPA)
|
||||
sp--; *sp = 0; // EBP
|
||||
sp--; *sp = 0; // ESI
|
||||
sp--; *sp = 0; // EDI
|
||||
|
||||
// Save this final stack location to the TCB
|
||||
new_task->stack_ptr = sp;
|
||||
new_task->next = NULL;
|
||||
|
||||
// Add to task list
|
||||
// 3. Add to linked list
|
||||
if (task_list == NULL) {
|
||||
task_list = new_task;
|
||||
current_task = new_task; // Make sure we have a current task to start
|
||||
} else {
|
||||
task_t *tail = task_list;
|
||||
while (tail->next) {
|
||||
@@ -42,21 +72,25 @@ void scheduler_add_task(void (*entry)(void)) {
|
||||
}
|
||||
|
||||
void scheduler_schedule() {
|
||||
// Very basic round-robin switch
|
||||
if (current_task && current_task->next) {
|
||||
if (!current_task) return;
|
||||
|
||||
task_t *prev = current_task;
|
||||
|
||||
// Round-robin logic
|
||||
if (current_task->next) {
|
||||
current_task = current_task->next;
|
||||
} else {
|
||||
current_task = task_list; // Loop back
|
||||
current_task = task_list;
|
||||
}
|
||||
|
||||
// Call context switch or simulate yielding to current_task
|
||||
// In real system: context_switch_to(current_task)
|
||||
if (current_task && current_task->entry) {
|
||||
current_task->entry(); // Simulate switching by calling
|
||||
// Perform the ACTUAL context switch
|
||||
// We pass the address of the previous task's stack pointer storage
|
||||
// and the value of the new task's stack pointer.
|
||||
if (prev != current_task) {
|
||||
ctx_switch(&prev->stack_ptr, current_task->stack_ptr);
|
||||
}
|
||||
}
|
||||
|
||||
void scheduler_yield() {
|
||||
// Stub: manually call schedule for cooperative multitasking
|
||||
scheduler_schedule();
|
||||
}
|
||||
|
||||
@@ -4,18 +4,21 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#define MAX_TASKS 8
|
||||
#define STACK_SIZE 1024
|
||||
#define STACK_SIZE 1024 // in bytes
|
||||
|
||||
typedef struct task {
|
||||
uint32_t id;
|
||||
void (*entry)(void);
|
||||
|
||||
// The most important field:
|
||||
// Where was the stack pointer when we last left this task?
|
||||
uint32_t *stack_ptr;
|
||||
|
||||
struct task *next;
|
||||
} task_t;
|
||||
|
||||
void scheduler_init();
|
||||
void scheduler_add_task(void (*entry)(void));
|
||||
void scheduler_schedule();
|
||||
void scheduler_yield(); // Optional for cooperative scheduling
|
||||
void scheduler_yield();
|
||||
|
||||
#endif // SCHEDULER_H
|
||||
|
||||
@@ -77,16 +77,6 @@ char* utoa(unsigned int value, char* str, int base) {
|
||||
return str;
|
||||
}
|
||||
|
||||
int memcmp(const void *ptr1, const void *ptr2, size_t num) {
|
||||
const uint8_t *p1 = ptr1, *p2 = ptr2;
|
||||
for (size_t i = 0; i < num; i++) {
|
||||
if (p1[i] != p2[i]) {
|
||||
return p1[i] < p2[i] ? -1 : 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void *memset(void *dest, int value, size_t len) {
|
||||
unsigned char *ptr = (unsigned char *)dest;
|
||||
while (len-- > 0)
|
||||
|
||||
@@ -9,7 +9,6 @@ char* itoa(int value, char* str, int base);
|
||||
// Convert unsigned integer to string (base is typically 10, 16, etc.)
|
||||
char* utoa(unsigned int value, char* str, int base);
|
||||
|
||||
int memcmp(const void *ptr1, const void *ptr2, size_t num);
|
||||
void *memset(void *dest, int value, size_t len);
|
||||
|
||||
#endif // UTILS_H
|
||||
|
||||
Reference in New Issue
Block a user