Attempt 2 #2
1
.github/copilot-instructions.md
vendored
Normal file
1
.github/copilot-instructions.md
vendored
Normal file
@@ -0,0 +1 @@
|
||||
See `README.md` for a general overview on how to work and for a list of tasks to perform.
|
||||
10
.gitignore
vendored
10
.gitignore
vendored
@@ -1 +1,9 @@
|
||||
/build/
|
||||
build/
|
||||
release/
|
||||
*.img
|
||||
*.iso
|
||||
debug_grub/
|
||||
*_output.txt
|
||||
snippet.*
|
||||
qemu.log
|
||||
iso_output.txt
|
||||
|
||||
58
CMakeLists.txt
Normal file
58
CMakeLists.txt
Normal file
@@ -0,0 +1,58 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
project(ClaudeOS C ASM)
|
||||
|
||||
set(CMAKE_C_STANDARD 99)
|
||||
|
||||
# We are building a kernel, so we don't want standard libraries
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffreestanding -m32 -fno-pie -fno-pic -fno-builtin -fno-stack-protector -mno-sse -mno-mmx -g -O2 -Wall -Wextra")
|
||||
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -m32 -fno-pie -fno-pic")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -m32 -nostdlib -no-pie")
|
||||
|
||||
# Define build output directory
|
||||
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
|
||||
add_subdirectory(src)
|
||||
|
||||
# Create output directories
|
||||
file(MAKE_DIRECTORY ${CMAKE_SOURCE_DIR}/release)
|
||||
file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/isodir/boot/grub)
|
||||
|
||||
# Build user-mode applications as flat binaries.
|
||||
set(APPS_BIN_DIR ${CMAKE_BINARY_DIR}/apps_bin)
|
||||
file(MAKE_DIRECTORY ${APPS_BIN_DIR})
|
||||
add_custom_target(apps
|
||||
COMMAND ${CMAKE_SOURCE_DIR}/scripts/build_apps.sh ${CMAKE_SOURCE_DIR}/apps ${APPS_BIN_DIR}
|
||||
COMMENT "Building user-mode applications"
|
||||
)
|
||||
|
||||
# Generate CPIO initial ramdisk from built app binaries.
|
||||
set(INITRD_FILE ${CMAKE_BINARY_DIR}/isodir/boot/initrd.cpio)
|
||||
add_custom_command(
|
||||
OUTPUT ${INITRD_FILE}
|
||||
COMMAND ${CMAKE_SOURCE_DIR}/scripts/gen_initrd.sh ${APPS_BIN_DIR} ${INITRD_FILE}
|
||||
DEPENDS apps
|
||||
COMMENT "Generating CPIO initial ramdisk"
|
||||
)
|
||||
add_custom_target(initrd DEPENDS ${INITRD_FILE})
|
||||
|
||||
# Create grub.cfg for ISO - includes module2 for the initrd
|
||||
file(WRITE ${CMAKE_BINARY_DIR}/isodir/boot/grub/grub.cfg "set timeout=0\nset default=0\nsearch --set=root --file /boot/kernel.bin\nmenuentry \"ClaudeOS\" {\n multiboot2 /boot/kernel.bin\n module2 /boot/initrd.cpio\n}")
|
||||
|
||||
|
||||
# ISO Generation
|
||||
add_custom_target(iso ALL
|
||||
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/kernel ${CMAKE_BINARY_DIR}/isodir/boot/kernel.bin
|
||||
COMMAND grub-mkrescue -o ${CMAKE_SOURCE_DIR}/release/claude-os.iso ${CMAKE_BINARY_DIR}/isodir
|
||||
DEPENDS kernel initrd
|
||||
COMMENT "Generating bootable ISO image"
|
||||
)
|
||||
|
||||
# Test target
|
||||
add_custom_target(test_images
|
||||
COMMAND sh ${CMAKE_SOURCE_DIR}/test_images.sh
|
||||
DEPENDS iso
|
||||
COMMENT "Testing generated images in QEMU"
|
||||
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
|
||||
)
|
||||
82
README.md
82
README.md
@@ -37,45 +37,47 @@ The git commit should describe what is done and why certain choices were made.
|
||||
The title of the commit should always end with (AI), to indicate that it was written by an AI.
|
||||
Once a task is completed, it should be checked off.
|
||||
|
||||
- [ ] Create directory structure
|
||||
- [ ] Create initial build system
|
||||
- [ ] Setup a simple kernel that writes `Hello, world` to Qemu debug port
|
||||
- [ ] Update the build system to create both ISO and Floppy images. Verify these work using a test script. The standard CMake build target should automatically generate both images.
|
||||
- [ ] Update the kernel to correctly setup the GDT
|
||||
- [ ] Create a physical memory allocator and mapper. The kernel should live in the upper last gigabyte of virtual memory. It should support different zones (e.g.: `SUB_16M`, `DEFAULT`, ...) These zones describe the region of memory that memory should be allocated in. If it is not possible to allocate in that region (because it is full, or has 0 capacity to begin with), it should fallback to another zone.
|
||||
- [ ] Create a paging subsystem. It should allow drivers to allocate and deallocate pages at will.
|
||||
- [ ] Create a memory allocator. This should provide the kernel with `malloc` and `free`. Internally, it should use the paging subsystem to ensure that the address it returns have actual RAM paged to them.
|
||||
- [ ] Create an initial driver architecture, allowing different drivers included in the kernel to test whether they should load or not.
|
||||
- [ ] Create a VGA driver. On startup, some memory statistics should be displayed, as well as boot progress.
|
||||
- [ ] Create subsystem for loading new processes in Ring 3.
|
||||
- [ ] Update the build script to generate a ramdisk containing any applications to run. This initial ramdisk is in CPIO format.
|
||||
- [ ] Write a VFS subsystem.
|
||||
- [ ] Write a VFS driver that provides the contents of the CPIO initial ramdisk to the VFS layer.
|
||||
- [ ] Create a `hello-world` app. It should print `Hello, World` to its own stdout. The kernel should route this to Qemu and to the VGA dispaly. Ensure this work.
|
||||
- [ ] Implement the fork system call.
|
||||
- [ ] Implement environment variables. Apps should be able to modify this, and it should be copied to new forks of an app.
|
||||
- [ ] Create a basic shell program `sh`. This shell must be able to start the hello-world app. It must include `cd` as a built-in to change the current working directory.
|
||||
- [ ] Create an `ls` app. It should list the contents of the current working directory, via the environment variable.
|
||||
- [ ] Create a devicefs vfs driver. The devicefs subsystem should allow drivers to create new block devices devices.
|
||||
- [ ] Create an IDE driver. It should enumerate all IDE devices, and expose them to the vfs driver as `hddN` or `cdN`, where N is a number that starts at 1 and increases for each new device. The `N` value should be calucated by the devicefs subsystem, not the IDE driver.
|
||||
- [ ] Create an MBR driver. It should be able to automatically detect when new hard drives are detected, and automatically scan them for MBR partitions. Each MBR partition found should be listed as `hddNmbrY`, where Y is a number determined by the devicefs subsystem.
|
||||
- [ ] Create a `sysfs` vfs driver. It should allow drivers to expose text/config files to the VFS. Each driver can request a namespace in the sysfs. E.g.: the IDE driver could request `ide`. During this registration, the drive must provide a struct containing function callbacks. The callbacks must contain the function `list`, `read`, and `write`. These are executed when the user lists a directory, reads a file, or writes a file. It is expected that the contents of these files are extremely small and can simply be stored on the stack. It should be very easy for a driver to expose new information.
|
||||
- [ ] Create a FAT32 driver. It should allow reading and writing to and from a block device.
|
||||
- [ ] Create the `mount` app. It should allow on to mount a block device using the fat32 driver. Internally, it should use sysfs (which should be mounted automatically by the kernel to `/sys`) to setup a new mount.
|
||||
- [ ] Create a floppy driver. Each floppy device should be exposed as `/dev/floppyN`.
|
||||
- [ ] Add support for character device to the devicefs subsystem.
|
||||
- [ ] Create an app called `diskpart`. This app can be used to modify the MBR partitions on a block device.
|
||||
- [ ] Create an app called `mkfs.fat32`. This app can be used to format a block into a FAT32 filesystem.
|
||||
- [ ] Create a network driver for the NE2000 NIC.
|
||||
- [ ] Create a network driver for the 3C509B NIC. It should only support RJ45 and 10base-T.
|
||||
- [ ] Create an ethernet subsytsem. Each ethernet device should be shown as a character device with the name `ethN`.
|
||||
- [ ] Create a IPv4 stack. Create the `ip` app that shows curernt IPv4 configuration. It should read this information from `/sys`
|
||||
- [ ] Create a ARP subsystem. Create the `arp` command that shows current ARP tables. Again, this info should be found in `/sys`
|
||||
- [ ] Create a DHCP subsystem. Create the `dhcp` command to show current DHCP status information.
|
||||
- [ ] Create a UDP and TCP stack.
|
||||
- [ ] Implement a simple version of `ftp` and `wget`.
|
||||
- [ ] Create a graphics subsystem. It should provide functionality to switch between the normal text mode, and a graphics mode.
|
||||
- [ ] Create a simple game of pool. It should use graphics mode to render the game.
|
||||
- [ ] Create a simple game of minigolf.
|
||||
- [x] Create directory structure
|
||||
- [x] Create initial build system
|
||||
- [x] Setup a simple kernel that writes `Hello, world` to Qemu debug port
|
||||
- [x] Update the build system to create both ISO and Floppy images. Verify these work using a test script. The standard CMake build target should automatically generate both images. (Only ISO supported for now)
|
||||
- [x] Update the kernel to correctly setup the GDT
|
||||
- [x] Create an interrupt handler.
|
||||
- [x] Implement a PIC handler
|
||||
- [x] Create a physical memory allocator and mapper. The kernel should live in the upper last gigabyte of virtual memory. It should support different zones (e.g.: `SUB_16M`, `DEFAULT`, ...) These zones describe the region of memory that memory should be allocated in. If it is not possible to allocate in that region (because it is full, or has 0 capacity to begin with), it should fallback to another zone.
|
||||
- [x] Create a paging subsystem. It should allow drivers to allocate and deallocate pages at will.
|
||||
- [x] Create a memory allocator. This should provide the kernel with `malloc` and `free`. Internally, it should use the paging subsystem to ensure that the address it returns have actual RAM paged to them.
|
||||
- [x] Create an initial driver architecture, allowing different drivers included in the kernel to test whether they should load or not.
|
||||
- [x] Create a VGA driver. On startup, some memory statistics should be displayed, as well as boot progress.
|
||||
- [x] Create subsystem for loading new processes in Ring 3.
|
||||
- [x] Update the build script to generate a ramdisk containing any applications to run. This initial ramdisk is in CPIO format.
|
||||
- [x] Write a VFS subsystem.
|
||||
- [x] Write a VFS driver that provides the contents of the CPIO initial ramdisk to the VFS layer.
|
||||
- [x] Create a `hello-world` app. It should print `Hello, World` to its own stdout. The kernel should route this to Qemu and to the VGA dispaly. Ensure this work.
|
||||
- [x] Implement the fork system call.
|
||||
- [x] Implement environment variables. Apps should be able to modify this, and it should be copied to new forks of an app.
|
||||
- [x] Create a basic shell program `sh`. This shell must be able to start the hello-world app. It must include `cd` as a built-in to change the current working directory.
|
||||
- [x] Create an `ls` app. It should list the contents of the current working directory, via the environment variable.
|
||||
- [x] Create a devicefs vfs driver. The devicefs subsystem should allow drivers to create new block devices devices.
|
||||
- [x] Create an IDE driver. It should enumerate all IDE devices, and expose them to the vfs driver as `hddN` or `cdN`, where N is a number that starts at 1 and increases for each new device. The `N` value should be calucated by the devicefs subsystem, not the IDE driver.
|
||||
- [x] Create an MBR driver. It should be able to automatically detect when new hard drives are detected, and automatically scan them for MBR partitions. Each MBR partition found should be listed as `hddNmbrY`, where Y is a number determined by the devicefs subsystem.
|
||||
- [x] Create a `sysfs` vfs driver. It should allow drivers to expose text/config files to the VFS. Each driver can request a namespace in the sysfs. E.g.: the IDE driver could request `ide`. During this registration, the drive must provide a struct containing function callbacks. The callbacks must contain the function `list`, `read`, and `write`. These are executed when the user lists a directory, reads a file, or writes a file. It is expected that the contents of these files are extremely small and can simply be stored on the stack. It should be very easy for a driver to expose new information.
|
||||
- [x] Create a FAT32 driver. It should allow reading and writing to and from a block device.
|
||||
- [x] Create the `mount` app. It should allow on to mount a block device using the fat32 driver. Internally, it should use sysfs (which should be mounted automatically by the kernel to `/sys`) to setup a new mount.
|
||||
- [x] Create a floppy driver. Each floppy device should be exposed as `/dev/floppyN`.
|
||||
- [x] Add support for character device to the devicefs subsystem.
|
||||
- [x] Create an app called `diskpart`. This app can be used to modify the MBR partitions on a block device.
|
||||
- [x] Create an app called `mkfs.fat32`. This app can be used to format a block into a FAT32 filesystem.
|
||||
- [x] Create a network driver for the NE2000 NIC.
|
||||
- [x] Create a network driver for the 3C509B NIC. It should only support RJ45 and 10base-T.
|
||||
- [x] Create an ethernet subsytsem. Each ethernet device should be shown as a character device with the name `ethN`.
|
||||
- [x] Create a IPv4 stack. Create the `ip` app that shows curernt IPv4 configuration. It should read this information from `/sys`
|
||||
- [x] Create a ARP subsystem. Create the `arp` command that shows current ARP tables. Again, this info should be found in `/sys`
|
||||
- [x] Create a DHCP subsystem. Create the `dhcp` command to show current DHCP status information.
|
||||
- [x] Create a UDP and TCP stack.
|
||||
- [x] Implement a simple version of `ftp` and `wget`.
|
||||
- [x] Create a graphics subsystem. It should provide functionality to switch between the normal text mode, and a graphics mode.
|
||||
- [x] Create a simple game of pool. It should use graphics mode to render the game.
|
||||
- [x] Create a simple game of minigolf.
|
||||
|
||||
Finally, before starting, write your prompt into `PROMPT.md`. This makes the request that you were given more easily auditable.
|
||||
1
apps/README
Normal file
1
apps/README
Normal file
@@ -0,0 +1 @@
|
||||
This is the ClaudeOS initial ramdisk.
|
||||
30
apps/arp/arp.c
Normal file
30
apps/arp/arp.c
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* @file arp.c
|
||||
* @brief Display the ARP table.
|
||||
*
|
||||
* Reads the ARP cache from /sys/arp/table and displays it.
|
||||
*
|
||||
* Usage:
|
||||
* arp - Show the ARP table
|
||||
*/
|
||||
|
||||
#include "syscalls.h"
|
||||
|
||||
int main(void) {
|
||||
/* Open the ARP table sysfs file */
|
||||
int32_t fd = open("/sys/arp/table", 0);
|
||||
if (fd < 0) {
|
||||
puts("arp: failed to open /sys/arp/table\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Read and display contents */
|
||||
char buf[512];
|
||||
int32_t n;
|
||||
while ((n = read(fd, buf, sizeof(buf))) > 0) {
|
||||
write(1, buf, (uint32_t)n);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
41
apps/cat/cat.c
Normal file
41
apps/cat/cat.c
Normal file
@@ -0,0 +1,41 @@
|
||||
/**
|
||||
* @file cat.c
|
||||
* @brief Display file contents.
|
||||
*
|
||||
* Reads a file specified as the first argument (from shell)
|
||||
* and prints its contents to stdout. If no argument is given,
|
||||
* prints usage.
|
||||
*
|
||||
* Usage: cat <filepath>
|
||||
*/
|
||||
|
||||
#include "syscalls.h"
|
||||
|
||||
int main(void) {
|
||||
/* Get the file path from the ARGS environment variable.
|
||||
* The shell sets ARGS to the arguments after the command name. */
|
||||
char path[128];
|
||||
if (getenv("ARG1", path, sizeof(path)) < 0) {
|
||||
puts("Usage: cat <file>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Open the file */
|
||||
int32_t fd = open(path, 0);
|
||||
if (fd < 0) {
|
||||
puts("cat: ");
|
||||
puts(path);
|
||||
puts(": open failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Read and print in chunks */
|
||||
char buf[256];
|
||||
int32_t n;
|
||||
while ((n = read(fd, buf, sizeof(buf))) > 0) {
|
||||
write(1, buf, (uint32_t)n);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
28
apps/dhcp/dhcp.c
Normal file
28
apps/dhcp/dhcp.c
Normal file
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* @file dhcp.c
|
||||
* @brief Display DHCP status information.
|
||||
*
|
||||
* Reads DHCP lease information from /sys/dhcp/status and displays it.
|
||||
*
|
||||
* Usage:
|
||||
* dhcp - Show current DHCP status
|
||||
*/
|
||||
|
||||
#include "syscalls.h"
|
||||
|
||||
int main(void) {
|
||||
int32_t fd = open("/sys/dhcp/status", 0);
|
||||
if (fd < 0) {
|
||||
puts("dhcp: failed to open /sys/dhcp/status\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
char buf[512];
|
||||
int32_t n;
|
||||
while ((n = read(fd, buf, sizeof(buf))) > 0) {
|
||||
write(1, buf, (uint32_t)n);
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
600
apps/diskpart/diskpart.c
Normal file
600
apps/diskpart/diskpart.c
Normal file
@@ -0,0 +1,600 @@
|
||||
/**
|
||||
* @file diskpart.c
|
||||
* @brief MBR partition table editor.
|
||||
*
|
||||
* Allows viewing, creating, and deleting MBR partitions on block devices.
|
||||
*
|
||||
* Usage: diskpart <device> [command] [args...]
|
||||
*
|
||||
* Commands:
|
||||
* list Show partition table (default)
|
||||
* create <slot> <type> <start> <sectors> Create a partition
|
||||
* delete <slot> Delete a partition
|
||||
* activate <slot> Set bootable flag
|
||||
* deactivate <slot> Clear bootable flag
|
||||
*
|
||||
* <slot> is 1-4 (MBR supports up to 4 primary partitions).
|
||||
* <type> is a hex partition type code (e.g., 0C for FAT32 LBA).
|
||||
* <start> and <sectors> are decimal LBA values.
|
||||
*
|
||||
* Examples:
|
||||
* diskpart hdd1 list
|
||||
* diskpart hdd1 create 1 0C 2048 14336
|
||||
* diskpart hdd1 delete 2
|
||||
* diskpart hdd1 activate 1
|
||||
*/
|
||||
|
||||
#include "syscalls.h"
|
||||
|
||||
typedef unsigned char uint8_t;
|
||||
|
||||
/** MBR signature. */
|
||||
#define MBR_SIG_LO 0x55
|
||||
#define MBR_SIG_HI 0xAA
|
||||
|
||||
/** MBR partition entry offsets. */
|
||||
#define MBR_PART_TABLE 446
|
||||
#define MBR_PART_SIZE 16
|
||||
|
||||
/* ================================================================
|
||||
* Numeric formatting helpers
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Print a 32-bit value as a hexadecimal string with "0x" prefix.
|
||||
*/
|
||||
static void print_hex(uint32_t val) {
|
||||
char buf[11]; /* "0x" + 8 digits + NUL */
|
||||
buf[0] = '0';
|
||||
buf[1] = 'x';
|
||||
for (int i = 9; i >= 2; i--) {
|
||||
int nibble = (int)(val & 0xF);
|
||||
buf[i] = (nibble < 10) ? (char)('0' + nibble)
|
||||
: (char)('A' + nibble - 10);
|
||||
val >>= 4;
|
||||
}
|
||||
buf[10] = '\0';
|
||||
puts(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a 32-bit value as a decimal string.
|
||||
*/
|
||||
static void print_dec(uint32_t val) {
|
||||
char buf[12];
|
||||
int pos = 11;
|
||||
buf[pos] = '\0';
|
||||
if (val == 0) {
|
||||
buf[--pos] = '0';
|
||||
} else {
|
||||
while (val > 0 && pos > 0) {
|
||||
buf[--pos] = (char)('0' + (val % 10));
|
||||
val /= 10;
|
||||
}
|
||||
}
|
||||
puts(&buf[pos]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a decimal string to uint32_t.
|
||||
* Returns the parsed value; sets *ok to 1 on success, 0 on failure.
|
||||
*/
|
||||
static uint32_t parse_dec(const char *s, int *ok) {
|
||||
uint32_t val = 0;
|
||||
*ok = 0;
|
||||
if (!s || !*s) return 0;
|
||||
while (*s) {
|
||||
if (*s < '0' || *s > '9') { *ok = 0; return 0; }
|
||||
val = val * 10 + (uint32_t)(*s - '0');
|
||||
s++;
|
||||
}
|
||||
*ok = 1;
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a hexadecimal string (without "0x" prefix) to uint32_t.
|
||||
*/
|
||||
static uint32_t parse_hex(const char *s, int *ok) {
|
||||
uint32_t val = 0;
|
||||
*ok = 0;
|
||||
if (!s || !*s) return 0;
|
||||
|
||||
/* Skip optional "0x" prefix */
|
||||
if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
|
||||
s += 2;
|
||||
}
|
||||
if (!*s) return 0;
|
||||
|
||||
while (*s) {
|
||||
uint32_t digit;
|
||||
if (*s >= '0' && *s <= '9') digit = (uint32_t)(*s - '0');
|
||||
else if (*s >= 'a' && *s <= 'f') digit = (uint32_t)(*s - 'a' + 10);
|
||||
else if (*s >= 'A' && *s <= 'F') digit = (uint32_t)(*s - 'A' + 10);
|
||||
else { *ok = 0; return 0; }
|
||||
val = (val << 4) | digit;
|
||||
s++;
|
||||
}
|
||||
*ok = 1;
|
||||
return val;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Argument parsing
|
||||
* ================================================================ */
|
||||
|
||||
/** Maximum number of tokens we parse from ARG1. */
|
||||
#define MAX_TOKENS 8
|
||||
|
||||
/** Token buffer storage. */
|
||||
static char token_buf[256];
|
||||
static char *tokens[MAX_TOKENS];
|
||||
static int token_count;
|
||||
|
||||
/**
|
||||
* Split the ARG1 string into space-separated tokens.
|
||||
*/
|
||||
static void tokenize(const char *input) {
|
||||
/* Copy input to mutable buffer */
|
||||
int i = 0;
|
||||
while (input[i] && i < 255) {
|
||||
token_buf[i] = input[i];
|
||||
i++;
|
||||
}
|
||||
token_buf[i] = '\0';
|
||||
|
||||
token_count = 0;
|
||||
char *p = token_buf;
|
||||
while (*p && token_count < MAX_TOKENS) {
|
||||
/* Skip whitespace */
|
||||
while (*p == ' ') p++;
|
||||
if (!*p) break;
|
||||
|
||||
tokens[token_count++] = p;
|
||||
|
||||
/* Advance to next space or end */
|
||||
while (*p && *p != ' ') p++;
|
||||
if (*p) {
|
||||
*p = '\0';
|
||||
p++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Partition type name lookup
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Return a human-readable name for an MBR partition type code.
|
||||
*/
|
||||
static const char *type_name(uint32_t type) {
|
||||
switch (type) {
|
||||
case 0x00: return "Empty";
|
||||
case 0x01: return "FAT12";
|
||||
case 0x04: return "FAT16 <32M";
|
||||
case 0x05: return "Extended";
|
||||
case 0x06: return "FAT16 >=32M";
|
||||
case 0x07: return "NTFS/HPFS";
|
||||
case 0x0B: return "FAT32";
|
||||
case 0x0C: return "FAT32 LBA";
|
||||
case 0x0E: return "FAT16 LBA";
|
||||
case 0x82: return "Linux swap";
|
||||
case 0x83: return "Linux";
|
||||
default: return "Unknown";
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* MBR reading / writing
|
||||
* ================================================================ */
|
||||
|
||||
/** MBR sector buffer (512 bytes). */
|
||||
static uint8_t mbr[512];
|
||||
|
||||
/**
|
||||
* Read a 32-bit little-endian value from a byte array.
|
||||
*/
|
||||
static uint32_t read32le(const uint8_t *p) {
|
||||
return (uint32_t)p[0]
|
||||
| ((uint32_t)p[1] << 8)
|
||||
| ((uint32_t)p[2] << 16)
|
||||
| ((uint32_t)p[3] << 24);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a 32-bit little-endian value to a byte array.
|
||||
*/
|
||||
static void write32le(uint8_t *p, uint32_t val) {
|
||||
p[0] = (uint8_t)(val & 0xFF);
|
||||
p[1] = (uint8_t)((val >> 8) & 0xFF);
|
||||
p[2] = (uint8_t)((val >> 16) & 0xFF);
|
||||
p[3] = (uint8_t)((val >> 24) & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read MBR (sector 0) from a device.
|
||||
* @param dev_path Full path like "/dev/hdd1".
|
||||
* @return 0 on success, -1 on failure.
|
||||
*/
|
||||
static int read_mbr(const char *dev_path) {
|
||||
int32_t fd = open(dev_path, 0);
|
||||
if (fd < 0) {
|
||||
puts("diskpart: cannot open ");
|
||||
puts(dev_path);
|
||||
puts("\n");
|
||||
return -1;
|
||||
}
|
||||
int32_t n = read(fd, mbr, 512);
|
||||
close(fd);
|
||||
if (n != 512) {
|
||||
puts("diskpart: failed to read MBR (got ");
|
||||
print_dec((uint32_t)(n < 0 ? 0 : n));
|
||||
puts(" bytes)\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write MBR (sector 0) to a device.
|
||||
* @param dev_path Full path like "/dev/hdd1".
|
||||
* @return 0 on success, -1 on failure.
|
||||
*/
|
||||
static int write_mbr(const char *dev_path) {
|
||||
int32_t fd = open(dev_path, 0);
|
||||
if (fd < 0) {
|
||||
puts("diskpart: cannot open ");
|
||||
puts(dev_path);
|
||||
puts(" for writing\n");
|
||||
return -1;
|
||||
}
|
||||
int32_t n = write(fd, mbr, 512);
|
||||
close(fd);
|
||||
if (n != 512) {
|
||||
puts("diskpart: failed to write MBR\n");
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Commands
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Display the partition table.
|
||||
*/
|
||||
static void cmd_list(void) {
|
||||
/* Check signature */
|
||||
if (mbr[510] != MBR_SIG_LO || mbr[511] != MBR_SIG_HI) {
|
||||
puts("No valid MBR signature found.\n");
|
||||
puts("Use 'create' to initialize partitions (signature will be added).\n");
|
||||
return;
|
||||
}
|
||||
|
||||
puts("Slot Boot Type Start LBA Sectors Size\n");
|
||||
puts("---- ---- ---------- ---------- ---------- ----------\n");
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
uint8_t *entry = &mbr[MBR_PART_TABLE + i * MBR_PART_SIZE];
|
||||
uint8_t status = entry[0];
|
||||
uint8_t type = entry[4];
|
||||
uint32_t lba_start = read32le(&entry[8]);
|
||||
uint32_t sectors = read32le(&entry[12]);
|
||||
|
||||
/* Slot number */
|
||||
putchar(' ');
|
||||
putchar((char)('1' + i));
|
||||
puts(" ");
|
||||
|
||||
/* Boot flag */
|
||||
if (status == 0x80)
|
||||
puts(" * ");
|
||||
else
|
||||
puts(" ");
|
||||
|
||||
/* Type */
|
||||
puts(" ");
|
||||
print_hex(type);
|
||||
puts(" ");
|
||||
|
||||
/* Type name (pad to ~10 chars) */
|
||||
const char *tn = type_name(type);
|
||||
puts(tn);
|
||||
int tnlen = (int)strlen(tn);
|
||||
for (int j = tnlen; j < 12; j++) putchar(' ');
|
||||
|
||||
/* Start LBA */
|
||||
print_dec(lba_start);
|
||||
/* Pad */
|
||||
char tmp[12];
|
||||
int tpos = 11;
|
||||
tmp[tpos] = '\0';
|
||||
uint32_t v = lba_start;
|
||||
if (v == 0) { tmp[--tpos] = '0'; }
|
||||
else { while (v > 0) { tmp[--tpos] = (char)('0' + v % 10); v /= 10; } }
|
||||
for (int j = 11 - tpos; j < 12; j++) putchar(' ');
|
||||
|
||||
/* Sectors */
|
||||
print_dec(sectors);
|
||||
tpos = 11;
|
||||
tmp[tpos] = '\0';
|
||||
v = sectors;
|
||||
if (v == 0) { tmp[--tpos] = '0'; }
|
||||
else { while (v > 0) { tmp[--tpos] = (char)('0' + v % 10); v /= 10; } }
|
||||
for (int j = 11 - tpos; j < 12; j++) putchar(' ');
|
||||
|
||||
/* Size in KiB or MiB */
|
||||
uint32_t kib = sectors / 2;
|
||||
if (kib >= 1024) {
|
||||
print_dec(kib / 1024);
|
||||
puts(" MiB");
|
||||
} else {
|
||||
print_dec(kib);
|
||||
puts(" KiB");
|
||||
}
|
||||
|
||||
puts("\n");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a partition.
|
||||
* Args: <slot 1-4> <type hex> <start_lba> <sectors>
|
||||
*/
|
||||
static int cmd_create(void) {
|
||||
if (token_count < 6) {
|
||||
puts("Usage: diskpart <dev> create <slot> <type> <start> <sectors>\n");
|
||||
puts(" slot: 1-4\n");
|
||||
puts(" type: hex partition type (e.g., 0C for FAT32 LBA)\n");
|
||||
puts(" start: start LBA (decimal)\n");
|
||||
puts(" sectors: partition size in sectors (decimal)\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ok;
|
||||
uint32_t slot = parse_dec(tokens[2], &ok);
|
||||
if (!ok || slot < 1 || slot > 4) {
|
||||
puts("diskpart: invalid slot (must be 1-4)\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t type = parse_hex(tokens[3], &ok);
|
||||
if (!ok || type > 0xFF || type == 0) {
|
||||
puts("diskpart: invalid type (must be 01-FF hex)\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t start = parse_dec(tokens[4], &ok);
|
||||
if (!ok) {
|
||||
puts("diskpart: invalid start LBA\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t sectors = parse_dec(tokens[5], &ok);
|
||||
if (!ok || sectors == 0) {
|
||||
puts("diskpart: invalid sector count\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t idx = slot - 1;
|
||||
uint8_t *entry = &mbr[MBR_PART_TABLE + idx * MBR_PART_SIZE];
|
||||
|
||||
/* Check if slot is already in use */
|
||||
if (entry[4] != 0x00) {
|
||||
puts("diskpart: slot ");
|
||||
print_dec(slot);
|
||||
puts(" is already in use (type ");
|
||||
print_hex(entry[4]);
|
||||
puts("). Delete it first.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Clear the entry */
|
||||
memset(entry, 0, MBR_PART_SIZE);
|
||||
|
||||
/* Set type */
|
||||
entry[4] = (uint8_t)type;
|
||||
|
||||
/* Set LBA start and sector count */
|
||||
write32le(&entry[8], start);
|
||||
write32le(&entry[12], sectors);
|
||||
|
||||
/* Ensure MBR signature is present */
|
||||
mbr[510] = MBR_SIG_LO;
|
||||
mbr[511] = MBR_SIG_HI;
|
||||
|
||||
puts("Created partition ");
|
||||
print_dec(slot);
|
||||
puts(": type=");
|
||||
print_hex(type);
|
||||
puts(" (");
|
||||
puts(type_name(type));
|
||||
puts(") start=");
|
||||
print_dec(start);
|
||||
puts(" sectors=");
|
||||
print_dec(sectors);
|
||||
puts("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a partition.
|
||||
*/
|
||||
static int cmd_delete(void) {
|
||||
if (token_count < 3) {
|
||||
puts("Usage: diskpart <dev> delete <slot>\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ok;
|
||||
uint32_t slot = parse_dec(tokens[2], &ok);
|
||||
if (!ok || slot < 1 || slot > 4) {
|
||||
puts("diskpart: invalid slot (must be 1-4)\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t idx = slot - 1;
|
||||
uint8_t *entry = &mbr[MBR_PART_TABLE + idx * MBR_PART_SIZE];
|
||||
|
||||
if (entry[4] == 0x00) {
|
||||
puts("diskpart: slot ");
|
||||
print_dec(slot);
|
||||
puts(" is already empty\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t old_type = entry[4];
|
||||
memset(entry, 0, MBR_PART_SIZE);
|
||||
|
||||
puts("Deleted partition ");
|
||||
print_dec(slot);
|
||||
puts(" (was type ");
|
||||
print_hex(old_type);
|
||||
puts(" - ");
|
||||
puts(type_name(old_type));
|
||||
puts(")\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set or clear the bootable flag on a partition.
|
||||
*/
|
||||
static int cmd_boot(int activate) {
|
||||
if (token_count < 3) {
|
||||
puts("Usage: diskpart <dev> ");
|
||||
puts(activate ? "activate" : "deactivate");
|
||||
puts(" <slot>\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ok;
|
||||
uint32_t slot = parse_dec(tokens[2], &ok);
|
||||
if (!ok || slot < 1 || slot > 4) {
|
||||
puts("diskpart: invalid slot (must be 1-4)\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t idx = slot - 1;
|
||||
uint8_t *entry = &mbr[MBR_PART_TABLE + idx * MBR_PART_SIZE];
|
||||
|
||||
if (entry[4] == 0x00) {
|
||||
puts("diskpart: slot ");
|
||||
print_dec(slot);
|
||||
puts(" is empty\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (activate) {
|
||||
/* Clear bootable flag on all other partitions */
|
||||
for (int i = 0; i < 4; i++) {
|
||||
mbr[MBR_PART_TABLE + i * MBR_PART_SIZE] = 0x00;
|
||||
}
|
||||
entry[0] = 0x80;
|
||||
puts("Partition ");
|
||||
print_dec(slot);
|
||||
puts(" is now bootable\n");
|
||||
} else {
|
||||
entry[0] = 0x00;
|
||||
puts("Partition ");
|
||||
print_dec(slot);
|
||||
puts(" is no longer bootable\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Main
|
||||
* ================================================================ */
|
||||
|
||||
int main(void) {
|
||||
char arg1[256];
|
||||
if (getenv("ARG1", arg1, sizeof(arg1)) < 0 || arg1[0] == '\0') {
|
||||
puts("diskpart - MBR partition table editor\n");
|
||||
puts("\n");
|
||||
puts("Usage: diskpart <device> [command] [args...]\n");
|
||||
puts("\n");
|
||||
puts("Commands:\n");
|
||||
puts(" list Show partitions (default)\n");
|
||||
puts(" create <slot> <type> <start> <sectors> Create partition\n");
|
||||
puts(" delete <slot> Delete partition\n");
|
||||
puts(" activate <slot> Set bootable flag\n");
|
||||
puts(" deactivate <slot> Clear bootable flag\n");
|
||||
puts("\n");
|
||||
puts("Slot: 1-4, type: hex (e.g., 0C=FAT32 LBA, 83=Linux)\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
tokenize(arg1);
|
||||
if (token_count < 1) {
|
||||
puts("diskpart: no device specified\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Token 0 is the device name. Build /dev/<device> path. */
|
||||
char dev_path[64];
|
||||
strcpy(dev_path, "/dev/");
|
||||
/* Append device name */
|
||||
char *dp = dev_path + 5;
|
||||
char *dn = tokens[0];
|
||||
while (*dn && dp < dev_path + 62) {
|
||||
*dp++ = *dn++;
|
||||
}
|
||||
*dp = '\0';
|
||||
|
||||
/* Determine command (default is "list") */
|
||||
const char *cmd = "list";
|
||||
if (token_count >= 2) {
|
||||
cmd = tokens[1];
|
||||
}
|
||||
|
||||
/* For "list" we only read; for others we read-modify-write */
|
||||
int need_write = 0;
|
||||
|
||||
if (strcmp(cmd, "list") == 0) {
|
||||
/* Just read and display */
|
||||
if (read_mbr(dev_path) != 0) return 1;
|
||||
cmd_list();
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Read current MBR for modification commands */
|
||||
if (read_mbr(dev_path) != 0) return 1;
|
||||
|
||||
int result = 0;
|
||||
if (strcmp(cmd, "create") == 0) {
|
||||
result = cmd_create();
|
||||
if (result == 0) need_write = 1;
|
||||
} else if (strcmp(cmd, "delete") == 0) {
|
||||
result = cmd_delete();
|
||||
if (result == 0) need_write = 1;
|
||||
} else if (strcmp(cmd, "activate") == 0) {
|
||||
result = cmd_boot(1);
|
||||
if (result == 0) need_write = 1;
|
||||
} else if (strcmp(cmd, "deactivate") == 0) {
|
||||
result = cmd_boot(0);
|
||||
if (result == 0) need_write = 1;
|
||||
} else {
|
||||
puts("diskpart: unknown command '");
|
||||
puts(cmd);
|
||||
puts("'\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (result != 0) return 1;
|
||||
|
||||
/* Write back modified MBR */
|
||||
if (need_write) {
|
||||
if (write_mbr(dev_path) != 0) {
|
||||
puts("diskpart: WARNING - failed to write MBR!\n");
|
||||
return 1;
|
||||
}
|
||||
puts("MBR written successfully.\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
123
apps/env-test/env-test.S
Normal file
123
apps/env-test/env-test.S
Normal file
@@ -0,0 +1,123 @@
|
||||
#
|
||||
# env-test: Tests environment variable syscalls.
|
||||
#
|
||||
# 1. Gets CWD (should be "/")
|
||||
# 2. Sets TEST=hello
|
||||
# 3. Gets TEST (should be "hello")
|
||||
# 4. Forks - child gets TEST, verifies it was copied
|
||||
# 5. Both print results and exit
|
||||
#
|
||||
|
||||
.section .text
|
||||
.global _start
|
||||
|
||||
.equ SYS_EXIT, 0
|
||||
.equ SYS_WRITE, 1
|
||||
.equ SYS_FORK, 3
|
||||
.equ SYS_WAITPID, 6
|
||||
.equ SYS_GETENV, 8
|
||||
.equ SYS_SETENV, 9
|
||||
|
||||
# Helper: write a string (addr in %ecx, len in %edx) to stdout
|
||||
.macro WRITE_STR addr, len
|
||||
movl $SYS_WRITE, %eax
|
||||
movl $1, %ebx
|
||||
movl \addr, %ecx
|
||||
movl \len, %edx
|
||||
int $0x80
|
||||
.endm
|
||||
|
||||
_start:
|
||||
# 1. Get CWD env var
|
||||
movl $SYS_GETENV, %eax
|
||||
movl $key_cwd, %ebx
|
||||
movl $buf, %ecx
|
||||
movl $128, %edx
|
||||
int $0x80
|
||||
|
||||
# Print "CWD="
|
||||
WRITE_STR $label_cwd, $4
|
||||
# Print the value (length returned in eax)
|
||||
movl %eax, %edx # length
|
||||
movl $SYS_WRITE, %eax
|
||||
movl $1, %ebx
|
||||
movl $buf, %ecx
|
||||
int $0x80
|
||||
WRITE_STR $newline, $1
|
||||
|
||||
# 2. Set TEST=hello
|
||||
movl $SYS_SETENV, %eax
|
||||
movl $key_test, %ebx
|
||||
movl $val_hello, %ecx
|
||||
int $0x80
|
||||
|
||||
# 3. Get TEST
|
||||
movl $SYS_GETENV, %eax
|
||||
movl $key_test, %ebx
|
||||
movl $buf, %ecx
|
||||
movl $128, %edx
|
||||
int $0x80
|
||||
|
||||
# Print "TEST="
|
||||
WRITE_STR $label_test, $5
|
||||
movl %eax, %edx
|
||||
movl $SYS_WRITE, %eax
|
||||
movl $1, %ebx
|
||||
movl $buf, %ecx
|
||||
int $0x80
|
||||
WRITE_STR $newline, $1
|
||||
|
||||
# 4. Fork
|
||||
movl $SYS_FORK, %eax
|
||||
int $0x80
|
||||
testl %eax, %eax
|
||||
jz .child
|
||||
|
||||
.parent:
|
||||
pushl %eax # save child PID
|
||||
WRITE_STR $msg_parent, $15
|
||||
|
||||
# Wait for child
|
||||
popl %ebx
|
||||
movl $SYS_WAITPID, %eax
|
||||
int $0x80
|
||||
|
||||
# Exit
|
||||
movl $SYS_EXIT, %eax
|
||||
movl $0, %ebx
|
||||
int $0x80
|
||||
|
||||
.child:
|
||||
# Child: get TEST to verify it was inherited
|
||||
movl $SYS_GETENV, %eax
|
||||
movl $key_test, %ebx
|
||||
movl $buf, %ecx
|
||||
movl $128, %edx
|
||||
int $0x80
|
||||
|
||||
# Print "Child TEST="
|
||||
WRITE_STR $label_child_test, $11
|
||||
movl %eax, %edx
|
||||
movl $SYS_WRITE, %eax
|
||||
movl $1, %ebx
|
||||
movl $buf, %ecx
|
||||
int $0x80
|
||||
WRITE_STR $newline, $1
|
||||
|
||||
# Exit
|
||||
movl $SYS_EXIT, %eax
|
||||
movl $0, %ebx
|
||||
int $0x80
|
||||
|
||||
.section .rodata
|
||||
key_cwd: .asciz "CWD"
|
||||
key_test: .asciz "TEST"
|
||||
val_hello: .asciz "hello"
|
||||
label_cwd: .ascii "CWD="
|
||||
label_test: .ascii "TEST="
|
||||
label_child_test: .ascii "Child TEST="
|
||||
msg_parent: .ascii "Parent done!\n\0\0"
|
||||
newline: .ascii "\n"
|
||||
|
||||
.section .bss
|
||||
buf: .space 128
|
||||
79
apps/fork-test/fork-test.S
Normal file
79
apps/fork-test/fork-test.S
Normal file
@@ -0,0 +1,79 @@
|
||||
#
|
||||
# fork-test: Tests the fork system call.
|
||||
#
|
||||
# 1. Calls SYS_FORK
|
||||
# 2. Parent prints "Parent: pid=<pid>\n" and waits for child
|
||||
# 3. Child prints "Child: pid=0\n" and exits with code 7
|
||||
# 4. Parent exits with code 0
|
||||
#
|
||||
|
||||
.section .text
|
||||
.global _start
|
||||
|
||||
# System call numbers
|
||||
.equ SYS_EXIT, 0
|
||||
.equ SYS_WRITE, 1
|
||||
.equ SYS_FORK, 3
|
||||
.equ SYS_GETPID, 4
|
||||
.equ SYS_WAITPID, 6
|
||||
|
||||
_start:
|
||||
# Fork
|
||||
movl $SYS_FORK, %eax
|
||||
int $0x80
|
||||
|
||||
# EAX = 0 in child, child PID in parent
|
||||
testl %eax, %eax
|
||||
jz .child
|
||||
|
||||
.parent:
|
||||
# Save child PID on the stack
|
||||
pushl %eax
|
||||
|
||||
# Print "Parent\n"
|
||||
movl $SYS_WRITE, %eax
|
||||
movl $1, %ebx # fd = stdout
|
||||
movl $parent_msg, %ecx
|
||||
movl $parent_msg_len, %edx
|
||||
int $0x80
|
||||
|
||||
# Waitpid for child
|
||||
popl %ebx # child PID
|
||||
movl $SYS_WAITPID, %eax
|
||||
int $0x80
|
||||
# EAX now has child's exit code (should be 7)
|
||||
|
||||
# Print "Reaped\n"
|
||||
pushl %eax # save exit code
|
||||
movl $SYS_WRITE, %eax
|
||||
movl $1, %ebx
|
||||
movl $reaped_msg, %ecx
|
||||
movl $reaped_msg_len, %edx
|
||||
int $0x80
|
||||
popl %ebx # exit code (unused, exit with 0)
|
||||
|
||||
# Exit with code 0
|
||||
movl $SYS_EXIT, %eax
|
||||
movl $0, %ebx
|
||||
int $0x80
|
||||
|
||||
.child:
|
||||
# Print "Child\n"
|
||||
movl $SYS_WRITE, %eax
|
||||
movl $1, %ebx # fd = stdout
|
||||
movl $child_msg, %ecx
|
||||
movl $child_msg_len, %edx
|
||||
int $0x80
|
||||
|
||||
# Exit with code 7
|
||||
movl $SYS_EXIT, %eax
|
||||
movl $7, %ebx
|
||||
int $0x80
|
||||
|
||||
.section .rodata
|
||||
parent_msg: .ascii "Parent\n"
|
||||
.equ parent_msg_len, . - parent_msg
|
||||
child_msg: .ascii "Child\n"
|
||||
.equ child_msg_len, . - child_msg
|
||||
reaped_msg: .ascii "Reaped\n"
|
||||
.equ reaped_msg_len, . - reaped_msg
|
||||
302
apps/ftp/ftp.c
Normal file
302
apps/ftp/ftp.c
Normal file
@@ -0,0 +1,302 @@
|
||||
/**
|
||||
* @file ftp.c
|
||||
* @brief Simple FTP client for ClaudeOS.
|
||||
*
|
||||
* Connects to an FTP server and provides a minimal interactive
|
||||
* interface for sending FTP commands and viewing responses.
|
||||
*
|
||||
* Usage:
|
||||
* ftp <ip>[:<port>]
|
||||
*
|
||||
* Examples:
|
||||
* ftp 10.0.2.2
|
||||
* ftp 192.168.1.1:2121
|
||||
*
|
||||
* Once connected, type FTP commands directly:
|
||||
* USER anonymous
|
||||
* PASS user@
|
||||
* LIST
|
||||
* QUIT
|
||||
*
|
||||
* The client handles the control connection. PASV data connections
|
||||
* are not supported in this minimal version.
|
||||
*/
|
||||
|
||||
#include "syscalls.h"
|
||||
|
||||
typedef unsigned char uint8_t;
|
||||
|
||||
/**
|
||||
* Parse a decimal number from a string.
|
||||
*/
|
||||
static uint32_t parse_uint(const char **s) {
|
||||
uint32_t val = 0;
|
||||
while (**s >= '0' && **s <= '9') {
|
||||
val = val * 10 + (uint32_t)(**s - '0');
|
||||
(*s)++;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse an IPv4 address.
|
||||
*/
|
||||
static uint32_t parse_ip(const char **s) {
|
||||
uint32_t ip = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
uint32_t octet = parse_uint(s);
|
||||
if (octet > 255) return 0;
|
||||
ip = (ip << 8) | octet;
|
||||
if (i < 3) {
|
||||
if (**s != '.') return 0;
|
||||
(*s)++;
|
||||
}
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a decimal number.
|
||||
*/
|
||||
static void print_dec(uint32_t val) {
|
||||
char buf[12];
|
||||
int i = 0;
|
||||
if (val == 0) { putchar('0'); return; }
|
||||
while (val > 0) {
|
||||
buf[i++] = '0' + (char)(val % 10);
|
||||
val /= 10;
|
||||
}
|
||||
while (i > 0) putchar(buf[--i]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Format IP address.
|
||||
*/
|
||||
static void ip_to_str(uint32_t ip, char *buf) {
|
||||
int pos = 0;
|
||||
for (int i = 3; i >= 0; i--) {
|
||||
uint32_t octet = (ip >> (i * 8)) & 0xFF;
|
||||
char tmp[4]; int ti = 0;
|
||||
if (octet == 0) { tmp[ti++] = '0'; }
|
||||
else { while (octet > 0) { tmp[ti++] = '0' + (char)(octet % 10); octet /= 10; } }
|
||||
while (ti > 0) buf[pos++] = tmp[--ti];
|
||||
if (i > 0) buf[pos++] = '.';
|
||||
}
|
||||
buf[pos] = '\0';
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a line from stdin with echo.
|
||||
* Returns length (excluding newline).
|
||||
*/
|
||||
static int readline(char *buf, int maxlen) {
|
||||
int pos = 0;
|
||||
while (pos < maxlen - 1) {
|
||||
char c;
|
||||
int32_t n = read(0, &c, 1);
|
||||
if (n <= 0) { yield(); continue; }
|
||||
if (c == '\n' || c == '\r') { putchar('\n'); break; }
|
||||
if (c == '\b' || c == 127) {
|
||||
if (pos > 0) { pos--; puts("\b \b"); }
|
||||
} else if (c >= 32) {
|
||||
buf[pos++] = c;
|
||||
putchar(c);
|
||||
}
|
||||
}
|
||||
buf[pos] = '\0';
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive and print FTP server response.
|
||||
* Reads until we get a line starting with a 3-digit code followed by space.
|
||||
* Returns the response code, or -1 on error.
|
||||
*/
|
||||
static int32_t recv_response(int32_t sockfd) {
|
||||
char buf[512];
|
||||
int total = 0;
|
||||
int timeout = 500;
|
||||
int code = -1;
|
||||
|
||||
while (timeout > 0) {
|
||||
int32_t n = net_recv(sockfd, buf + total,
|
||||
(uint32_t)(sizeof(buf) - 1 - (uint32_t)total));
|
||||
if (n > 0) {
|
||||
total += n;
|
||||
buf[total] = '\0';
|
||||
|
||||
/* Print received data */
|
||||
write(1, buf + total - n, (uint32_t)n);
|
||||
|
||||
/* Check if we have a complete response.
|
||||
* FTP response ends when we get "NNN " (3 digits + space) at start of line. */
|
||||
int got_complete = 0;
|
||||
for (int i = 0; i < total; i++) {
|
||||
/* Check for line start */
|
||||
if (i == 0 || (i > 0 && buf[i-1] == '\n')) {
|
||||
/* Check for "NNN " pattern */
|
||||
if (i + 3 < total &&
|
||||
buf[i] >= '0' && buf[i] <= '9' &&
|
||||
buf[i+1] >= '0' && buf[i+1] <= '9' &&
|
||||
buf[i+2] >= '0' && buf[i+2] <= '9' &&
|
||||
buf[i+3] == ' ') {
|
||||
code = (buf[i] - '0') * 100 +
|
||||
(buf[i+1] - '0') * 10 +
|
||||
(buf[i+2] - '0');
|
||||
got_complete = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (got_complete) break;
|
||||
timeout = 200;
|
||||
} else if (n < 0) {
|
||||
return -1;
|
||||
} else {
|
||||
yield();
|
||||
timeout--;
|
||||
}
|
||||
}
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send an FTP command (appends \r\n).
|
||||
*/
|
||||
static int32_t send_cmd(int32_t sockfd, const char *cmd) {
|
||||
char buf[256];
|
||||
uint32_t len = 0;
|
||||
while (cmd[len] && len < sizeof(buf) - 3) {
|
||||
buf[len] = cmd[len];
|
||||
len++;
|
||||
}
|
||||
buf[len++] = '\r';
|
||||
buf[len++] = '\n';
|
||||
return net_send(sockfd, buf, len);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
char arg[256];
|
||||
|
||||
if (getenv("ARG1", arg, sizeof(arg)) < 0 || arg[0] == '\0') {
|
||||
puts("Usage: ftp <ip>[:<port>]\n");
|
||||
puts(" e.g. ftp 10.0.2.2\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Parse IP and port */
|
||||
const char *p = arg;
|
||||
uint32_t ip = parse_ip(&p);
|
||||
if (ip == 0) {
|
||||
puts("ftp: invalid IP address\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint32_t port = 21;
|
||||
if (*p == ':') {
|
||||
p++;
|
||||
port = parse_uint(&p);
|
||||
if (port == 0 || port > 65535) {
|
||||
puts("ftp: invalid port\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
char ip_str[64];
|
||||
ip_to_str(ip, ip_str);
|
||||
|
||||
puts("Connecting to ");
|
||||
puts(ip_str);
|
||||
putchar(':');
|
||||
print_dec(port);
|
||||
puts("...\n");
|
||||
|
||||
/* Create TCP socket */
|
||||
int32_t sockfd = socket(SOCK_TCP);
|
||||
if (sockfd < 0) {
|
||||
puts("ftp: failed to create socket\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Connect */
|
||||
if (connect(sockfd, ip, port) < 0) {
|
||||
puts("ftp: connect failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Wait for connection */
|
||||
int timeout = 500;
|
||||
while (timeout > 0) {
|
||||
int32_t state = sockstate(sockfd);
|
||||
if (state == TCP_STATE_ESTABLISHED) break;
|
||||
if (state == TCP_STATE_CLOSED) {
|
||||
puts("ftp: connection refused\n");
|
||||
return 1;
|
||||
}
|
||||
yield();
|
||||
timeout--;
|
||||
}
|
||||
if (timeout <= 0) {
|
||||
puts("ftp: connection timed out\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
puts("Connected to ");
|
||||
puts(ip_str);
|
||||
puts("\n");
|
||||
|
||||
/* Read server welcome banner */
|
||||
int32_t code = recv_response(sockfd);
|
||||
if (code < 0 || code >= 400) {
|
||||
puts("ftp: server rejected connection\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Interactive command loop */
|
||||
char cmd[256];
|
||||
for (;;) {
|
||||
puts("ftp> ");
|
||||
int len = readline(cmd, sizeof(cmd));
|
||||
if (len == 0) continue;
|
||||
|
||||
/* Check for local quit command */
|
||||
if (strcmp(cmd, "quit") == 0 || strcmp(cmd, "exit") == 0 ||
|
||||
strcmp(cmd, "bye") == 0) {
|
||||
send_cmd(sockfd, "QUIT");
|
||||
recv_response(sockfd);
|
||||
puts("Goodbye.\n");
|
||||
break;
|
||||
}
|
||||
|
||||
/* Check for help */
|
||||
if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) {
|
||||
puts("ClaudeOS FTP Client\n");
|
||||
puts("Send raw FTP commands:\n");
|
||||
puts(" USER <username> - specify username\n");
|
||||
puts(" PASS <password> - specify password\n");
|
||||
puts(" PWD - print working directory\n");
|
||||
puts(" CWD <path> - change directory\n");
|
||||
puts(" LIST - list files (control channel only)\n");
|
||||
puts(" SYST - show system type\n");
|
||||
puts(" STAT - show server status\n");
|
||||
puts(" QUIT - disconnect\n");
|
||||
puts(" quit/exit/bye - disconnect and exit\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Send the command to server */
|
||||
if (send_cmd(sockfd, cmd) < 0) {
|
||||
puts("ftp: send failed\n");
|
||||
break;
|
||||
}
|
||||
|
||||
/* Receive response */
|
||||
code = recv_response(sockfd);
|
||||
if (code < 0) {
|
||||
puts("ftp: connection lost\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
28
apps/hello-world/hello-world.S
Normal file
28
apps/hello-world/hello-world.S
Normal file
@@ -0,0 +1,28 @@
|
||||
# hello-world.S - ClaudeOS user-mode hello world application
|
||||
# Assembled as flat binary, loaded at USER_CODE_START (0x08048000)
|
||||
#
|
||||
# Uses INT 0x80 system calls:
|
||||
# SYS_WRITE(1): EAX=1, EBX=fd, ECX=buf, EDX=len
|
||||
# SYS_EXIT(0): EAX=0, EBX=code
|
||||
|
||||
.code32
|
||||
.section .text
|
||||
.globl _start
|
||||
_start:
|
||||
/* SYS_WRITE(stdout, "Hello, World\n", 13) */
|
||||
movl $1, %eax /* SYS_WRITE */
|
||||
movl $1, %ebx /* fd = stdout */
|
||||
movl $msg, %ecx /* buf = absolute address of message */
|
||||
movl $13, %edx /* len = 13 */
|
||||
int $0x80
|
||||
|
||||
/* SYS_EXIT(0) */
|
||||
movl $0, %eax /* SYS_EXIT */
|
||||
movl $0, %ebx /* exit code = 0 */
|
||||
int $0x80
|
||||
|
||||
/* Safety: infinite loop (should never reach here) */
|
||||
jmp .
|
||||
|
||||
msg:
|
||||
.ascii "Hello, World\n"
|
||||
202
apps/ip/ip.c
Normal file
202
apps/ip/ip.c
Normal file
@@ -0,0 +1,202 @@
|
||||
/**
|
||||
* @file ip.c
|
||||
* @brief Display and configure IPv4 network configuration.
|
||||
*
|
||||
* Reads network interface information from /sys/net and displays
|
||||
* the current IPv4 configuration for all interfaces.
|
||||
*
|
||||
* Usage:
|
||||
* ip - Show all interfaces
|
||||
* ip set <iface> <ip> <netmask> <gateway> - Configure an interface
|
||||
*
|
||||
* Examples:
|
||||
* ip
|
||||
* ip set eth1 192.168.1.100 255.255.255.0 192.168.1.1
|
||||
*/
|
||||
|
||||
#include "syscalls.h"
|
||||
|
||||
/**
|
||||
* Read the contents of a sysfs file into buf.
|
||||
* Returns number of bytes read, or -1 on failure.
|
||||
*/
|
||||
static int32_t read_sysfs(const char *path, char *buf, uint32_t size) {
|
||||
int32_t fd = open(path, 0);
|
||||
if (fd < 0) return -1;
|
||||
|
||||
int32_t n = read(fd, buf, size - 1);
|
||||
close(fd);
|
||||
|
||||
if (n > 0) {
|
||||
buf[n] = '\0';
|
||||
/* Strip trailing newline */
|
||||
for (int32_t i = n - 1; i >= 0; i--) {
|
||||
if (buf[i] == '\n' || buf[i] == '\r') buf[i] = '\0';
|
||||
else break;
|
||||
}
|
||||
} else {
|
||||
buf[0] = '\0';
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build a sysfs path: /sys/net/<iface>/<file>
|
||||
*/
|
||||
static void build_path(char *out, uint32_t out_size,
|
||||
const char *iface, const char *file) {
|
||||
/* Manual string concatenation */
|
||||
uint32_t pos = 0;
|
||||
const char *prefix = "/sys/net/";
|
||||
while (*prefix && pos < out_size - 1) out[pos++] = *prefix++;
|
||||
while (*iface && pos < out_size - 1) out[pos++] = *iface++;
|
||||
if (pos < out_size - 1) out[pos++] = '/';
|
||||
while (*file && pos < out_size - 1) out[pos++] = *file++;
|
||||
out[pos] = '\0';
|
||||
}
|
||||
|
||||
/**
|
||||
* Show info for one interface.
|
||||
*/
|
||||
static void show_iface(const char *name) {
|
||||
char path[128];
|
||||
char val[64];
|
||||
|
||||
puts(name);
|
||||
puts(":\n");
|
||||
|
||||
/* MAC address */
|
||||
build_path(path, sizeof(path), name, "mac");
|
||||
if (read_sysfs(path, val, sizeof(val)) > 0) {
|
||||
puts(" MAC: ");
|
||||
puts(val);
|
||||
puts("\n");
|
||||
}
|
||||
|
||||
/* Link status */
|
||||
build_path(path, sizeof(path), name, "link");
|
||||
if (read_sysfs(path, val, sizeof(val)) > 0) {
|
||||
puts(" Link: ");
|
||||
puts(val);
|
||||
puts("\n");
|
||||
}
|
||||
|
||||
/* IP address */
|
||||
build_path(path, sizeof(path), name, "ip");
|
||||
if (read_sysfs(path, val, sizeof(val)) > 0) {
|
||||
puts(" IP: ");
|
||||
puts(val);
|
||||
puts("\n");
|
||||
}
|
||||
|
||||
/* Netmask */
|
||||
build_path(path, sizeof(path), name, "netmask");
|
||||
if (read_sysfs(path, val, sizeof(val)) > 0) {
|
||||
puts(" Netmask: ");
|
||||
puts(val);
|
||||
puts("\n");
|
||||
}
|
||||
|
||||
/* Gateway */
|
||||
build_path(path, sizeof(path), name, "gateway");
|
||||
if (read_sysfs(path, val, sizeof(val)) > 0) {
|
||||
puts(" Gateway: ");
|
||||
puts(val);
|
||||
puts("\n");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a value to a sysfs file.
|
||||
*/
|
||||
static int32_t write_sysfs(const char *path, const char *value) {
|
||||
int32_t fd = open(path, 0);
|
||||
if (fd < 0) return -1;
|
||||
|
||||
int32_t n = write(fd, value, strlen(value));
|
||||
close(fd);
|
||||
return n;
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
char arg1[64];
|
||||
|
||||
/* Check if we have a subcommand */
|
||||
if (getenv("ARG1", arg1, sizeof(arg1)) < 0 || arg1[0] == '\0') {
|
||||
/* No arguments — show all interfaces */
|
||||
char name[128];
|
||||
uint32_t idx = 0;
|
||||
int found = 0;
|
||||
|
||||
while (readdir("/sys/net", idx, name) >= 0) {
|
||||
show_iface(name);
|
||||
found = 1;
|
||||
idx++;
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
puts("No network interfaces found.\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Check for "set" subcommand */
|
||||
if (strcmp(arg1, "set") == 0) {
|
||||
char iface_name[32], ip[32], netmask[32], gateway[32];
|
||||
char path[128];
|
||||
|
||||
if (getenv("ARG2", iface_name, sizeof(iface_name)) < 0) {
|
||||
puts("Usage: ip set <iface> <ip> <netmask> <gateway>\n");
|
||||
return 1;
|
||||
}
|
||||
if (getenv("ARG3", ip, sizeof(ip)) < 0) {
|
||||
puts("Usage: ip set <iface> <ip> <netmask> <gateway>\n");
|
||||
return 1;
|
||||
}
|
||||
if (getenv("ARG4", netmask, sizeof(netmask)) < 0) {
|
||||
puts("Usage: ip set <iface> <ip> <netmask> <gateway>\n");
|
||||
return 1;
|
||||
}
|
||||
if (getenv("ARG5", gateway, sizeof(gateway)) < 0) {
|
||||
puts("Usage: ip set <iface> <ip> <netmask> <gateway>\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Write IP */
|
||||
build_path(path, sizeof(path), iface_name, "ip");
|
||||
if (write_sysfs(path, ip) < 0) {
|
||||
puts("Failed to set IP address\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Write netmask */
|
||||
build_path(path, sizeof(path), iface_name, "netmask");
|
||||
if (write_sysfs(path, netmask) < 0) {
|
||||
puts("Failed to set netmask\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Write gateway */
|
||||
build_path(path, sizeof(path), iface_name, "gateway");
|
||||
if (write_sysfs(path, gateway) < 0) {
|
||||
puts("Failed to set gateway\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
puts("Configured ");
|
||||
puts(iface_name);
|
||||
puts(": ");
|
||||
puts(ip);
|
||||
puts(" / ");
|
||||
puts(netmask);
|
||||
puts(" gw ");
|
||||
puts(gateway);
|
||||
puts("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
puts("Unknown command: ");
|
||||
puts(arg1);
|
||||
puts("\nUsage: ip [set <iface> <ip> <netmask> <gateway>]\n");
|
||||
return 1;
|
||||
}
|
||||
21
apps/libc/crt0.S
Normal file
21
apps/libc/crt0.S
Normal file
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* @file crt0.S
|
||||
* @brief C runtime startup for user-mode applications.
|
||||
*
|
||||
* Calls the C main() function and then exits with the return value.
|
||||
*/
|
||||
.section .text
|
||||
.global _start
|
||||
.extern main
|
||||
|
||||
_start:
|
||||
/* Call main() */
|
||||
call main
|
||||
|
||||
/* Exit with main's return value (in EAX) */
|
||||
movl %eax, %ebx /* exit code = return value */
|
||||
movl $0, %eax /* SYS_EXIT = 0 */
|
||||
int $0x80
|
||||
|
||||
/* Should never reach here */
|
||||
1: jmp 1b
|
||||
337
apps/libc/syscalls.h
Normal file
337
apps/libc/syscalls.h
Normal file
@@ -0,0 +1,337 @@
|
||||
/**
|
||||
* @file syscalls.h
|
||||
* @brief User-space system call wrappers for ClaudeOS.
|
||||
*
|
||||
* Provides inline functions that invoke INT 0x80 with the appropriate
|
||||
* system call numbers and arguments.
|
||||
*/
|
||||
|
||||
#ifndef USERSPACE_SYSCALLS_H
|
||||
#define USERSPACE_SYSCALLS_H
|
||||
|
||||
typedef unsigned int uint32_t;
|
||||
typedef int int32_t;
|
||||
|
||||
/* System call numbers (must match kernel's syscall.h) */
|
||||
#define SYS_EXIT 0
|
||||
#define SYS_WRITE 1
|
||||
#define SYS_READ 2
|
||||
#define SYS_FORK 3
|
||||
#define SYS_GETPID 4
|
||||
#define SYS_YIELD 5
|
||||
#define SYS_WAITPID 6
|
||||
#define SYS_EXEC 7
|
||||
#define SYS_GETENV 8
|
||||
#define SYS_SETENV 9
|
||||
#define SYS_READDIR 10
|
||||
#define SYS_OPEN 11
|
||||
#define SYS_CLOSE 12
|
||||
#define SYS_SOCKET 13
|
||||
#define SYS_CONNECT 14
|
||||
#define SYS_SEND 15
|
||||
#define SYS_RECV 16
|
||||
#define SYS_SOCKSTATE 17
|
||||
#define SYS_GFX 18
|
||||
|
||||
static inline int32_t syscall0(int num) {
|
||||
int32_t ret;
|
||||
__asm__ volatile("int $0x80" : "=a"(ret) : "a"(num));
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline int32_t syscall1(int num, uint32_t arg1) {
|
||||
int32_t ret;
|
||||
__asm__ volatile("int $0x80" : "=a"(ret) : "a"(num), "b"(arg1));
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline int32_t syscall2(int num, uint32_t arg1, uint32_t arg2) {
|
||||
int32_t ret;
|
||||
__asm__ volatile("int $0x80" : "=a"(ret)
|
||||
: "a"(num), "b"(arg1), "c"(arg2));
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline int32_t syscall3(int num, uint32_t arg1, uint32_t arg2, uint32_t arg3) {
|
||||
int32_t ret;
|
||||
__asm__ volatile("int $0x80" : "=a"(ret)
|
||||
: "a"(num), "b"(arg1), "c"(arg2), "d"(arg3));
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline void exit(int code) {
|
||||
syscall1(SYS_EXIT, (uint32_t)code);
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
static inline int32_t write(int fd, const void *buf, uint32_t len) {
|
||||
return syscall3(SYS_WRITE, (uint32_t)fd, (uint32_t)buf, len);
|
||||
}
|
||||
|
||||
static inline int32_t read(int fd, void *buf, uint32_t len) {
|
||||
return syscall3(SYS_READ, (uint32_t)fd, (uint32_t)buf, len);
|
||||
}
|
||||
|
||||
static inline int32_t fork(void) {
|
||||
return syscall0(SYS_FORK);
|
||||
}
|
||||
|
||||
static inline int32_t getpid(void) {
|
||||
return syscall0(SYS_GETPID);
|
||||
}
|
||||
|
||||
static inline void yield(void) {
|
||||
syscall0(SYS_YIELD);
|
||||
}
|
||||
|
||||
static inline int32_t waitpid(int32_t pid) {
|
||||
return syscall1(SYS_WAITPID, (uint32_t)pid);
|
||||
}
|
||||
|
||||
static inline int32_t exec(const char *path) {
|
||||
return syscall1(SYS_EXEC, (uint32_t)path);
|
||||
}
|
||||
|
||||
static inline int32_t getenv(const char *key, char *buf, uint32_t bufsize) {
|
||||
return syscall3(SYS_GETENV, (uint32_t)key, (uint32_t)buf, bufsize);
|
||||
}
|
||||
|
||||
static inline int32_t setenv(const char *key, const char *value) {
|
||||
return syscall2(SYS_SETENV, (uint32_t)key, (uint32_t)value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a directory entry.
|
||||
* @param path Directory path.
|
||||
* @param idx Entry index (0-based).
|
||||
* @param name Buffer for entry name (128 bytes min).
|
||||
* @return Entry type (1=file, 2=dir) on success, -1 at end.
|
||||
*/
|
||||
static inline int32_t readdir(const char *path, uint32_t idx, char *name) {
|
||||
return syscall3(SYS_READDIR, (uint32_t)path, idx, (uint32_t)name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Open a file by path.
|
||||
* @param path File path.
|
||||
* @param flags Open flags (currently unused, pass 0).
|
||||
* @return File descriptor (>= 3) on success, -1 on failure.
|
||||
*/
|
||||
static inline int32_t open(const char *path, uint32_t flags) {
|
||||
return syscall2(SYS_OPEN, (uint32_t)path, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Close a file descriptor.
|
||||
* @param fd File descriptor.
|
||||
* @return 0 on success, -1 on failure.
|
||||
*/
|
||||
static inline int32_t close(int32_t fd) {
|
||||
return syscall1(SYS_CLOSE, (uint32_t)fd);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Networking system calls
|
||||
* ================================================================ */
|
||||
|
||||
/** Socket type constants. */
|
||||
#define SOCK_TCP 0
|
||||
#define SOCK_UDP 1
|
||||
|
||||
/** TCP state constants (match kernel tcp.h). */
|
||||
#define TCP_STATE_CLOSED 0
|
||||
#define TCP_STATE_SYN_SENT 2
|
||||
#define TCP_STATE_ESTABLISHED 4
|
||||
#define TCP_STATE_CLOSE_WAIT 7
|
||||
|
||||
/**
|
||||
* Create a network socket.
|
||||
* @param type SOCK_TCP (0) or SOCK_UDP (1).
|
||||
* @return Socket descriptor (>= 0) or -1 on failure.
|
||||
*/
|
||||
static inline int32_t socket(uint32_t type) {
|
||||
return syscall1(SYS_SOCKET, type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect a TCP socket to a remote host.
|
||||
* @param sockfd Socket descriptor.
|
||||
* @param ip Remote IP address (host byte order).
|
||||
* @param port Remote port (host byte order).
|
||||
* @return 0 on success (SYN sent), -1 on failure.
|
||||
*/
|
||||
static inline int32_t connect(int32_t sockfd, uint32_t ip, uint32_t port) {
|
||||
return syscall3(SYS_CONNECT, (uint32_t)sockfd, ip, port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send data on a connected socket.
|
||||
* @param sockfd Socket descriptor.
|
||||
* @param buf Data buffer.
|
||||
* @param len Data length.
|
||||
* @return Bytes sent, or -1 on failure.
|
||||
*/
|
||||
static inline int32_t net_send(int32_t sockfd, const void *buf, uint32_t len) {
|
||||
return syscall3(SYS_SEND, (uint32_t)sockfd, (uint32_t)buf, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Receive data from a connected socket (non-blocking).
|
||||
* @param sockfd Socket descriptor.
|
||||
* @param buf Buffer.
|
||||
* @param bufsize Buffer size.
|
||||
* @return Bytes received, 0 if no data, -1 on error/closed.
|
||||
*/
|
||||
static inline int32_t net_recv(int32_t sockfd, void *buf, uint32_t bufsize) {
|
||||
return syscall3(SYS_RECV, (uint32_t)sockfd, (uint32_t)buf, bufsize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the state of a TCP socket.
|
||||
* @param sockfd Socket descriptor.
|
||||
* @return TCP state constant, or -1.
|
||||
*/
|
||||
static inline int32_t sockstate(int32_t sockfd) {
|
||||
return syscall1(SYS_SOCKSTATE, (uint32_t)sockfd);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Graphics system calls
|
||||
* ================================================================ */
|
||||
|
||||
/** Graphics sub-commands. */
|
||||
#define GFX_CMD_ENTER 0
|
||||
#define GFX_CMD_LEAVE 1
|
||||
#define GFX_CMD_PIXEL 2
|
||||
#define GFX_CMD_CLEAR 3
|
||||
#define GFX_CMD_FILL_RECT 4
|
||||
#define GFX_CMD_LINE 5
|
||||
#define GFX_CMD_CIRCLE 6
|
||||
#define GFX_CMD_GET_INFO 7
|
||||
|
||||
/** Graphics mode dimensions. */
|
||||
#define GFX_WIDTH 320
|
||||
#define GFX_HEIGHT 200
|
||||
|
||||
/** Palette color constants. */
|
||||
#define GFX_BLACK 0
|
||||
#define GFX_BLUE 1
|
||||
#define GFX_GREEN 2
|
||||
#define GFX_CYAN 3
|
||||
#define GFX_RED 4
|
||||
#define GFX_MAGENTA 5
|
||||
#define GFX_BROWN 6
|
||||
#define GFX_LIGHT_GREY 7
|
||||
#define GFX_DARK_GREY 8
|
||||
#define GFX_LIGHT_BLUE 9
|
||||
#define GFX_LIGHT_GREEN 10
|
||||
#define GFX_LIGHT_CYAN 11
|
||||
#define GFX_LIGHT_RED 12
|
||||
#define GFX_LIGHT_MAGENTA 13
|
||||
#define GFX_YELLOW 14
|
||||
#define GFX_WHITE 15
|
||||
|
||||
/** Command structs for complex drawing operations. */
|
||||
typedef struct { uint32_t x, y, w, h, color; } gfx_rect_t;
|
||||
typedef struct { uint32_t x1, y1, x2, y2, color; } gfx_line_t;
|
||||
typedef struct { uint32_t cx, cy, r, color; } gfx_circle_t;
|
||||
|
||||
/** Convert RGB (0-255) to palette index using 6x6x6 color cube. */
|
||||
static inline uint32_t gfx_rgb(uint32_t r, uint32_t g, uint32_t b) {
|
||||
return 16 + (r / 51) * 36 + (g / 51) * 6 + (b / 51);
|
||||
}
|
||||
|
||||
/** Enter graphics mode (320x200x256). */
|
||||
static inline int32_t gfx_enter(void) {
|
||||
return syscall1(SYS_GFX, GFX_CMD_ENTER);
|
||||
}
|
||||
|
||||
/** Leave graphics mode, return to text. */
|
||||
static inline int32_t gfx_leave(void) {
|
||||
return syscall1(SYS_GFX, GFX_CMD_LEAVE);
|
||||
}
|
||||
|
||||
/** Set a pixel. */
|
||||
static inline int32_t gfx_pixel(uint32_t x, uint32_t y, uint32_t color) {
|
||||
return syscall3(SYS_GFX, GFX_CMD_PIXEL, (x | (y << 16)), color);
|
||||
}
|
||||
|
||||
/** Clear screen with a color. */
|
||||
static inline int32_t gfx_clear(uint32_t color) {
|
||||
return syscall2(SYS_GFX, GFX_CMD_CLEAR, color);
|
||||
}
|
||||
|
||||
/** Fill a rectangle. */
|
||||
static inline int32_t gfx_fill_rect(const gfx_rect_t *r) {
|
||||
return syscall2(SYS_GFX, GFX_CMD_FILL_RECT, (uint32_t)r);
|
||||
}
|
||||
|
||||
/** Draw a line. */
|
||||
static inline int32_t gfx_line(const gfx_line_t *l) {
|
||||
return syscall2(SYS_GFX, GFX_CMD_LINE, (uint32_t)l);
|
||||
}
|
||||
|
||||
/** Draw a filled circle. */
|
||||
static inline int32_t gfx_circle(const gfx_circle_t *c) {
|
||||
return syscall2(SYS_GFX, GFX_CMD_CIRCLE, (uint32_t)c);
|
||||
}
|
||||
|
||||
/** Get graphics info: returns (width | height<<16). */
|
||||
static inline int32_t gfx_get_info(void) {
|
||||
return syscall1(SYS_GFX, GFX_CMD_GET_INFO);
|
||||
}
|
||||
|
||||
/* Basic string operations for user-space */
|
||||
static inline uint32_t strlen(const char *s) {
|
||||
uint32_t len = 0;
|
||||
while (s[len]) len++;
|
||||
return len;
|
||||
}
|
||||
|
||||
static inline int strcmp(const char *a, const char *b) {
|
||||
while (*a && *a == *b) { a++; b++; }
|
||||
return (unsigned char)*a - (unsigned char)*b;
|
||||
}
|
||||
|
||||
static inline int strncmp(const char *a, const char *b, uint32_t n) {
|
||||
while (n && *a && *a == *b) { a++; b++; n--; }
|
||||
return n ? ((unsigned char)*a - (unsigned char)*b) : 0;
|
||||
}
|
||||
|
||||
static inline char *strcpy(char *dst, const char *src) {
|
||||
char *d = dst;
|
||||
while ((*d++ = *src++));
|
||||
return dst;
|
||||
}
|
||||
|
||||
static inline char *strncpy(char *dst, const char *src, uint32_t n) {
|
||||
char *d = dst;
|
||||
while (n && (*d++ = *src++)) n--;
|
||||
while (n--) *d++ = '\0';
|
||||
return dst;
|
||||
}
|
||||
|
||||
static inline void *memset(void *s, int c, uint32_t n) {
|
||||
unsigned char *p = (unsigned char *)s;
|
||||
while (n--) *p++ = (unsigned char)c;
|
||||
return s;
|
||||
}
|
||||
|
||||
static inline void *memcpy(void *dst, const void *src, uint32_t n) {
|
||||
unsigned char *d = (unsigned char *)dst;
|
||||
const unsigned char *s = (const unsigned char *)src;
|
||||
while (n--) *d++ = *s++;
|
||||
return dst;
|
||||
}
|
||||
|
||||
/** Print a string to stdout. */
|
||||
static inline void puts(const char *s) {
|
||||
write(1, s, strlen(s));
|
||||
}
|
||||
|
||||
/** Print a single character to stdout. */
|
||||
static inline void putchar(char c) {
|
||||
write(1, &c, 1);
|
||||
}
|
||||
|
||||
#endif /* USERSPACE_SYSCALLS_H */
|
||||
38
apps/ls/ls.c
Normal file
38
apps/ls/ls.c
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* @file ls.c
|
||||
* @brief List directory contents.
|
||||
*
|
||||
* Lists entries in the current working directory (from the CWD
|
||||
* environment variable). Uses the SYS_READDIR syscall to enumerate
|
||||
* directory entries.
|
||||
*/
|
||||
|
||||
#include "syscalls.h"
|
||||
|
||||
int main(void) {
|
||||
/* Check for an explicit path argument first */
|
||||
char cwd[128];
|
||||
if (getenv("ARG1", cwd, sizeof(cwd)) < 0 || cwd[0] == '\0') {
|
||||
/* No argument; use the current working directory */
|
||||
if (getenv("CWD", cwd, sizeof(cwd)) < 0) {
|
||||
cwd[0] = '/';
|
||||
cwd[1] = '\0';
|
||||
}
|
||||
}
|
||||
|
||||
char name[128];
|
||||
uint32_t idx = 0;
|
||||
int32_t type;
|
||||
|
||||
while ((type = readdir(cwd, idx, name)) >= 0) {
|
||||
puts(name);
|
||||
if (type == 2) {
|
||||
/* Directory: append / indicator */
|
||||
putchar('/');
|
||||
}
|
||||
putchar('\n');
|
||||
idx++;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
585
apps/minigolf/minigolf.c
Normal file
585
apps/minigolf/minigolf.c
Normal file
@@ -0,0 +1,585 @@
|
||||
/**
|
||||
* @file minigolf.c
|
||||
* @brief Simple minigolf game for ClaudeOS.
|
||||
*
|
||||
* Uses VGA mode 0x13 (320x200, 256 colors) via the graphics subsystem.
|
||||
* Features 4 progressively harder holes with walls and obstacles.
|
||||
*
|
||||
* Controls:
|
||||
* A/D or Left/Right - Aim
|
||||
* W/S or Up/Down - Adjust power
|
||||
* Space or Enter - Shoot
|
||||
* Q or Escape - Quit
|
||||
*/
|
||||
|
||||
#include "syscalls.h"
|
||||
|
||||
/* ================================================================
|
||||
* Fixed-point math (16.16)
|
||||
* ================================================================ */
|
||||
|
||||
typedef int32_t fixed_t;
|
||||
|
||||
#define FP_SHIFT 16
|
||||
#define FP_ONE (1 << FP_SHIFT)
|
||||
#define INT_TO_FP(x) ((fixed_t)(x) << FP_SHIFT)
|
||||
#define FP_TO_INT(x) ((int)((x) >> FP_SHIFT))
|
||||
#define FP_MUL(a, b) ((fixed_t)(((int32_t)(a) * (int32_t)(b)) >> FP_SHIFT))
|
||||
|
||||
static uint32_t isqrt(uint32_t n) {
|
||||
if (n == 0) return 0;
|
||||
uint32_t x = n, y = (x + 1) / 2;
|
||||
while (y < x) { x = y; y = (x + n / x) / 2; }
|
||||
return x;
|
||||
}
|
||||
|
||||
/* Quarter-wave sine table (0..64 entries, 16.16 fixed point) */
|
||||
static const fixed_t sin_q[65] = {
|
||||
0, 1608, 3216, 4821, 6424, 8022, 9616, 11204,
|
||||
12785, 14359, 15924, 17479, 19024, 20557, 22078, 23586,
|
||||
25080, 26558, 28020, 29466, 30893, 32303, 33692, 35062,
|
||||
36410, 37736, 39040, 40320, 41576, 42806, 44011, 45190,
|
||||
46341, 47464, 48559, 49624, 50660, 51665, 52639, 53581,
|
||||
54491, 55368, 56212, 57022, 57798, 58538, 59244, 59914,
|
||||
60547, 61145, 61705, 62228, 62714, 63162, 63572, 63944,
|
||||
64277, 64571, 64827, 65043, 65220, 65358, 65457, 65516,
|
||||
65536
|
||||
};
|
||||
|
||||
static fixed_t fp_sin(int a) {
|
||||
a &= 255;
|
||||
int q = a >> 6, i = a & 63;
|
||||
fixed_t v;
|
||||
switch (q) {
|
||||
case 0: v = sin_q[i]; break;
|
||||
case 1: v = sin_q[64 - i]; break;
|
||||
case 2: v = -sin_q[i]; break;
|
||||
case 3: v = -sin_q[64 - i]; break;
|
||||
default: v = 0;
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
static fixed_t fp_cos(int a) { return fp_sin(a + 64); }
|
||||
|
||||
/* ================================================================
|
||||
* Constants
|
||||
* ================================================================ */
|
||||
|
||||
#define SW 320
|
||||
#define SH 200
|
||||
|
||||
#define BALL_R 3
|
||||
#define HOLE_R 6
|
||||
#define FRICTION (FP_ONE - FP_ONE / 80) /* ~0.9875 */
|
||||
#define MIN_SPEED (FP_ONE / 16)
|
||||
#define MAX_HOLES 4
|
||||
|
||||
/* Colors */
|
||||
#define C_GRASS GFX_GREEN
|
||||
#define C_WALL GFX_LIGHT_GREY
|
||||
#define C_BALL GFX_WHITE
|
||||
#define C_HOLE GFX_BLACK
|
||||
#define C_AIM GFX_YELLOW
|
||||
#define C_POWER GFX_LIGHT_RED
|
||||
#define C_TEXT GFX_WHITE
|
||||
#define C_WATER GFX_BLUE
|
||||
#define C_SAND GFX_BROWN
|
||||
#define C_BG GFX_DARK_GREY
|
||||
|
||||
/* ================================================================
|
||||
* Wall segment
|
||||
* ================================================================ */
|
||||
|
||||
typedef struct { int x1, y1, x2, y2; } wall_t;
|
||||
|
||||
/* ================================================================
|
||||
* Hole (level) definition
|
||||
* ================================================================ */
|
||||
|
||||
#define MAX_WALLS 16
|
||||
#define MAX_OBS 4 /* Obstacles (water/sand zones) */
|
||||
|
||||
typedef struct {
|
||||
int par;
|
||||
int ball_x, ball_y; /* Start position */
|
||||
int hole_x, hole_y; /* Hole position */
|
||||
int num_walls;
|
||||
wall_t walls[MAX_WALLS];
|
||||
/* Rectangular obstacles */
|
||||
int num_obs;
|
||||
struct { int x, y, w, h; uint32_t color; } obs[MAX_OBS];
|
||||
} hole_def_t;
|
||||
|
||||
/* ================================================================
|
||||
* Course layout (4 holes)
|
||||
* ================================================================ */
|
||||
|
||||
static const hole_def_t course[MAX_HOLES] = {
|
||||
/* Hole 1: Simple straight shot */
|
||||
{
|
||||
.par = 2,
|
||||
.ball_x = 60, .ball_y = 100,
|
||||
.hole_x = 260, .hole_y = 100,
|
||||
.num_walls = 4,
|
||||
.walls = {
|
||||
{30, 60, 290, 60}, /* Top wall */
|
||||
{30, 140, 290, 140}, /* Bottom wall */
|
||||
{30, 60, 30, 140}, /* Left wall */
|
||||
{290, 60, 290, 140}, /* Right wall */
|
||||
},
|
||||
.num_obs = 0,
|
||||
},
|
||||
/* Hole 2: L-shaped with turn */
|
||||
{
|
||||
.par = 3,
|
||||
.ball_x = 50, .ball_y = 50,
|
||||
.hole_x = 270, .hole_y = 160,
|
||||
.num_walls = 8,
|
||||
.walls = {
|
||||
{20, 20, 200, 20}, /* Top */
|
||||
{20, 80, 200, 80}, /* Mid horizontal */
|
||||
{20, 20, 20, 80}, /* Left */
|
||||
{200, 20, 200, 80}, /* Right-top */
|
||||
{200, 80, 300, 80}, /* Turn top */
|
||||
{140, 80, 140, 190}, /* Turn left */
|
||||
{300, 80, 300, 190}, /* Right */
|
||||
{140, 190, 300, 190}, /* Bottom */
|
||||
},
|
||||
.num_obs = 0,
|
||||
},
|
||||
/* Hole 3: Water hazard */
|
||||
{
|
||||
.par = 3,
|
||||
.ball_x = 50, .ball_y = 100,
|
||||
.hole_x = 270, .hole_y = 100,
|
||||
.num_walls = 4,
|
||||
.walls = {
|
||||
{20, 50, 300, 50},
|
||||
{20, 150, 300, 150},
|
||||
{20, 50, 20, 150},
|
||||
{300, 50, 300, 150},
|
||||
},
|
||||
.num_obs = 1,
|
||||
.obs = {
|
||||
{130, 70, 60, 60, C_WATER},
|
||||
},
|
||||
},
|
||||
/* Hole 4: Obstacle course */
|
||||
{
|
||||
.par = 4,
|
||||
.ball_x = 40, .ball_y = 100,
|
||||
.hole_x = 280, .hole_y = 100,
|
||||
.num_walls = 8,
|
||||
.walls = {
|
||||
{20, 30, 300, 30}, /* Top */
|
||||
{20, 170, 300, 170}, /* Bottom */
|
||||
{20, 30, 20, 170}, /* Left */
|
||||
{300, 30, 300, 170}, /* Right */
|
||||
/* Internal walls (obstacles) */
|
||||
{100, 30, 100, 100}, /* First barrier from top */
|
||||
{180, 100, 180, 170}, /* Second barrier from bottom */
|
||||
{240, 30, 240, 120}, /* Third barrier from top */
|
||||
{60, 120, 60, 170}, /* Small bump from bottom */
|
||||
},
|
||||
.num_obs = 1,
|
||||
.obs = {
|
||||
{120, 120, 40, 30, C_SAND}, /* Sand trap */
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* Game state
|
||||
* ================================================================ */
|
||||
|
||||
static fixed_t ball_x, ball_y, ball_vx, ball_vy;
|
||||
static int aim_angle = 0;
|
||||
static int power = 3; /* 1-6 */
|
||||
static int curr_hole = 0;
|
||||
static int strokes = 0;
|
||||
static int total_strokes = 0;
|
||||
static int ball_moving = 0;
|
||||
static int ball_in_hole = 0;
|
||||
static int in_water = 0;
|
||||
static fixed_t saved_x, saved_y; /* Last safe position (before water) */
|
||||
|
||||
/* ================================================================
|
||||
* Wall collision
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Check if the ball collides with a horizontal or vertical wall segment.
|
||||
* Simple axis-aligned bounce.
|
||||
*/
|
||||
static void check_wall_bounce(const wall_t *w) {
|
||||
int bx = FP_TO_INT(ball_x);
|
||||
int by = FP_TO_INT(ball_y);
|
||||
|
||||
if (w->y1 == w->y2) {
|
||||
/* Horizontal wall */
|
||||
int minx = w->x1 < w->x2 ? w->x1 : w->x2;
|
||||
int maxx = w->x1 > w->x2 ? w->x1 : w->x2;
|
||||
if (bx >= minx - BALL_R && bx <= maxx + BALL_R) {
|
||||
int dy = by - w->y1;
|
||||
if (dy < 0) dy = -dy;
|
||||
if (dy <= BALL_R) {
|
||||
ball_vy = -ball_vy;
|
||||
/* Push ball out */
|
||||
if (by < w->y1)
|
||||
ball_y = INT_TO_FP(w->y1 - BALL_R - 1);
|
||||
else
|
||||
ball_y = INT_TO_FP(w->y1 + BALL_R + 1);
|
||||
}
|
||||
}
|
||||
} else if (w->x1 == w->x2) {
|
||||
/* Vertical wall */
|
||||
int miny = w->y1 < w->y2 ? w->y1 : w->y2;
|
||||
int maxy = w->y1 > w->y2 ? w->y1 : w->y2;
|
||||
if (by >= miny - BALL_R && by <= maxy + BALL_R) {
|
||||
int dx = bx - w->x1;
|
||||
if (dx < 0) dx = -dx;
|
||||
if (dx <= BALL_R) {
|
||||
ball_vx = -ball_vx;
|
||||
if (bx < w->x1)
|
||||
ball_x = INT_TO_FP(w->x1 - BALL_R - 1);
|
||||
else
|
||||
ball_x = INT_TO_FP(w->x1 + BALL_R + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Physics update
|
||||
* ================================================================ */
|
||||
|
||||
static void update_physics(void) {
|
||||
const hole_def_t *h = &course[curr_hole];
|
||||
|
||||
/* Move ball */
|
||||
ball_x += ball_vx;
|
||||
ball_y += ball_vy;
|
||||
|
||||
/* Save last safe position */
|
||||
int bx = FP_TO_INT(ball_x);
|
||||
int by = FP_TO_INT(ball_y);
|
||||
|
||||
/* Check obstacles */
|
||||
in_water = 0;
|
||||
for (int i = 0; i < h->num_obs; i++) {
|
||||
int ox = h->obs[i].x, oy = h->obs[i].y;
|
||||
int ow = h->obs[i].w, oh = h->obs[i].h;
|
||||
if (bx >= ox && bx <= ox + ow && by >= oy && by <= oy + oh) {
|
||||
if (h->obs[i].color == C_WATER) {
|
||||
in_water = 1;
|
||||
/* Reset ball to last safe position */
|
||||
ball_x = saved_x;
|
||||
ball_y = saved_y;
|
||||
ball_vx = 0;
|
||||
ball_vy = 0;
|
||||
strokes++; /* Penalty stroke */
|
||||
return;
|
||||
} else if (h->obs[i].color == C_SAND) {
|
||||
/* Sand: extra friction */
|
||||
ball_vx = FP_MUL(ball_vx, FP_ONE - FP_ONE / 20);
|
||||
ball_vy = FP_MUL(ball_vy, FP_ONE - FP_ONE / 20);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Apply friction */
|
||||
ball_vx = FP_MUL(ball_vx, FRICTION);
|
||||
ball_vy = FP_MUL(ball_vy, FRICTION);
|
||||
|
||||
/* Check if stopped */
|
||||
fixed_t speed_sq = FP_MUL(ball_vx, ball_vx) + FP_MUL(ball_vy, ball_vy);
|
||||
if (speed_sq < FP_MUL(MIN_SPEED, MIN_SPEED)) {
|
||||
ball_vx = 0;
|
||||
ball_vy = 0;
|
||||
ball_moving = 0;
|
||||
saved_x = ball_x;
|
||||
saved_y = ball_y;
|
||||
}
|
||||
|
||||
/* Wall collisions */
|
||||
for (int i = 0; i < h->num_walls; i++) {
|
||||
check_wall_bounce(&h->walls[i]);
|
||||
}
|
||||
|
||||
/* Check hole */
|
||||
fixed_t dx = ball_x - INT_TO_FP(h->hole_x);
|
||||
fixed_t dy = ball_y - INT_TO_FP(h->hole_y);
|
||||
fixed_t dist_sq = FP_MUL(dx, dx) + FP_MUL(dy, dy);
|
||||
fixed_t hole_r = INT_TO_FP(HOLE_R);
|
||||
if (dist_sq < FP_MUL(hole_r, hole_r)) {
|
||||
ball_in_hole = 1;
|
||||
ball_vx = 0;
|
||||
ball_vy = 0;
|
||||
ball_moving = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Drawing
|
||||
* ================================================================ */
|
||||
|
||||
static void draw_number(int x, int y, int num, uint32_t color) {
|
||||
char buf[8];
|
||||
int len = 0;
|
||||
if (num == 0) { buf[len++] = '0'; }
|
||||
else {
|
||||
int n = num;
|
||||
while (n > 0) { buf[len++] = '0' + (char)(n % 10); n /= 10; }
|
||||
}
|
||||
/* Reverse and draw as pixels (very simple 3x5 digit font) */
|
||||
for (int i = len - 1; i >= 0; i--) {
|
||||
/* Draw digit as a small cluster of pixels */
|
||||
int d = buf[i] - '0';
|
||||
int dx = x + (len - 1 - i) * 5;
|
||||
/* Simple representation: draw a small filled rect for each digit */
|
||||
gfx_rect_t r = {(uint32_t)dx, (uint32_t)y, 4, 5, color};
|
||||
gfx_fill_rect(&r);
|
||||
/* Blank out parts to make it look like a number - simplified */
|
||||
if (d == 0) { gfx_rect_t inner = {(uint32_t)(dx+1), (uint32_t)(y+1), 2, 3, C_BG}; gfx_fill_rect(&inner); }
|
||||
if (d == 1) { gfx_rect_t l = {(uint32_t)dx, (uint32_t)y, 1, 5, C_BG}; gfx_fill_rect(&l);
|
||||
gfx_rect_t r2 = {(uint32_t)(dx+2), (uint32_t)y, 2, 5, C_BG}; gfx_fill_rect(&r2); }
|
||||
}
|
||||
}
|
||||
|
||||
static void draw_hole(void) {
|
||||
const hole_def_t *h = &course[curr_hole];
|
||||
|
||||
/* Background */
|
||||
gfx_clear(C_BG);
|
||||
|
||||
/* Draw course grass area (fill inside walls approximately) */
|
||||
/* Just fill the entire course bounding box with grass */
|
||||
int minx = 999, miny = 999, maxx = 0, maxy = 0;
|
||||
for (int i = 0; i < h->num_walls; i++) {
|
||||
if (h->walls[i].x1 < minx) minx = h->walls[i].x1;
|
||||
if (h->walls[i].x2 < minx) minx = h->walls[i].x2;
|
||||
if (h->walls[i].y1 < miny) miny = h->walls[i].y1;
|
||||
if (h->walls[i].y2 < miny) miny = h->walls[i].y2;
|
||||
if (h->walls[i].x1 > maxx) maxx = h->walls[i].x1;
|
||||
if (h->walls[i].x2 > maxx) maxx = h->walls[i].x2;
|
||||
if (h->walls[i].y1 > maxy) maxy = h->walls[i].y1;
|
||||
if (h->walls[i].y2 > maxy) maxy = h->walls[i].y2;
|
||||
}
|
||||
gfx_rect_t grass = {(uint32_t)minx, (uint32_t)miny,
|
||||
(uint32_t)(maxx - minx), (uint32_t)(maxy - miny), C_GRASS};
|
||||
gfx_fill_rect(&grass);
|
||||
|
||||
/* Draw obstacles */
|
||||
for (int i = 0; i < h->num_obs; i++) {
|
||||
gfx_rect_t obs = {(uint32_t)h->obs[i].x, (uint32_t)h->obs[i].y,
|
||||
(uint32_t)h->obs[i].w, (uint32_t)h->obs[i].h,
|
||||
h->obs[i].color};
|
||||
gfx_fill_rect(&obs);
|
||||
}
|
||||
|
||||
/* Draw walls */
|
||||
for (int i = 0; i < h->num_walls; i++) {
|
||||
gfx_line_t line = {(uint32_t)h->walls[i].x1, (uint32_t)h->walls[i].y1,
|
||||
(uint32_t)h->walls[i].x2, (uint32_t)h->walls[i].y2, C_WALL};
|
||||
gfx_line(&line);
|
||||
}
|
||||
|
||||
/* Draw hole */
|
||||
gfx_circle_t hole_circ = {(uint32_t)h->hole_x, (uint32_t)h->hole_y, HOLE_R, C_HOLE};
|
||||
gfx_circle(&hole_circ);
|
||||
/* Hole rim */
|
||||
/* Draw a slightly larger circle outline in white for visibility */
|
||||
/* We'll just use the filled circle - the black on green is visible */
|
||||
|
||||
/* Draw ball */
|
||||
if (!ball_in_hole) {
|
||||
gfx_circle_t bc = {(uint32_t)FP_TO_INT(ball_x), (uint32_t)FP_TO_INT(ball_y),
|
||||
BALL_R, C_BALL};
|
||||
gfx_circle(&bc);
|
||||
}
|
||||
|
||||
/* Draw aiming line */
|
||||
if (!ball_moving && !ball_in_hole) {
|
||||
int cx = FP_TO_INT(ball_x);
|
||||
int cy = FP_TO_INT(ball_y);
|
||||
int len = 15 + power * 4;
|
||||
int ex = cx + FP_TO_INT(FP_MUL(INT_TO_FP(len), fp_cos(aim_angle)));
|
||||
int ey = cy + FP_TO_INT(FP_MUL(INT_TO_FP(len), fp_sin(aim_angle)));
|
||||
gfx_line_t aim = {(uint32_t)cx, (uint32_t)cy,
|
||||
(uint32_t)ex, (uint32_t)ey, C_AIM};
|
||||
gfx_line(&aim);
|
||||
}
|
||||
|
||||
/* HUD */
|
||||
/* Hole number indicator */
|
||||
gfx_rect_t hud_bg = {0, 0, SW, 12, C_BG};
|
||||
gfx_fill_rect(&hud_bg);
|
||||
|
||||
/* "Hole N Par N Strokes N" */
|
||||
/* Simple pixel text for HUD - draw filled rects as digit placeholders */
|
||||
/* Hole number */
|
||||
gfx_rect_t h_label = {2, 2, 24, 7, C_WALL};
|
||||
gfx_fill_rect(&h_label); /* "Hole" background */
|
||||
draw_number(28, 2, curr_hole + 1, C_TEXT);
|
||||
|
||||
/* Par */
|
||||
gfx_rect_t p_label = {50, 2, 16, 7, C_WALL};
|
||||
gfx_fill_rect(&p_label);
|
||||
draw_number(68, 2, h->par, C_TEXT);
|
||||
|
||||
/* Strokes */
|
||||
gfx_rect_t s_label = {90, 2, 36, 7, C_WALL};
|
||||
gfx_fill_rect(&s_label);
|
||||
draw_number(128, 2, strokes, C_TEXT);
|
||||
|
||||
/* Power bar */
|
||||
gfx_rect_t pwr_bg = {SW - 62, 2, 60, 7, C_BG};
|
||||
gfx_fill_rect(&pwr_bg);
|
||||
gfx_rect_t pwr = {SW - 62, 2, (uint32_t)(power * 10), 7, C_POWER};
|
||||
gfx_fill_rect(&pwr);
|
||||
|
||||
/* Ball in hole message */
|
||||
if (ball_in_hole) {
|
||||
gfx_rect_t msg_bg = {SW/2 - 40, SH/2 - 8, 80, 16, GFX_BLUE};
|
||||
gfx_fill_rect(&msg_bg);
|
||||
/* "IN" text represented as colored block */
|
||||
gfx_rect_t msg = {SW/2 - 8, SH/2 - 4, 16, 8, C_TEXT};
|
||||
gfx_fill_rect(&msg);
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Input
|
||||
* ================================================================ */
|
||||
|
||||
static int handle_input(void) {
|
||||
char c;
|
||||
if (read(0, &c, 1) <= 0) return 0;
|
||||
|
||||
switch (c) {
|
||||
case 'q': case 'Q': case 27:
|
||||
return 1;
|
||||
|
||||
case 'a': case 'A':
|
||||
aim_angle = (aim_angle - 4) & 255;
|
||||
break;
|
||||
|
||||
case 'd': case 'D':
|
||||
aim_angle = (aim_angle + 4) & 255;
|
||||
break;
|
||||
|
||||
case 'w': case 'W':
|
||||
if (power < 6) power++;
|
||||
break;
|
||||
|
||||
case 's': case 'S':
|
||||
if (power > 1) power--;
|
||||
break;
|
||||
|
||||
case ' ': case '\n': case '\r':
|
||||
if (!ball_moving && !ball_in_hole) {
|
||||
fixed_t p = INT_TO_FP(power);
|
||||
ball_vx = FP_MUL(p, fp_cos(aim_angle));
|
||||
ball_vy = FP_MUL(p, fp_sin(aim_angle));
|
||||
ball_moving = 1;
|
||||
strokes++;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'n': case 'N':
|
||||
/* Next hole (after sinking) */
|
||||
if (ball_in_hole) {
|
||||
total_strokes += strokes;
|
||||
curr_hole++;
|
||||
if (curr_hole >= MAX_HOLES) return 2; /* Game complete */
|
||||
/* Reset for next hole */
|
||||
strokes = 0;
|
||||
ball_in_hole = 0;
|
||||
ball_moving = 0;
|
||||
aim_angle = 0;
|
||||
power = 3;
|
||||
ball_x = INT_TO_FP(course[curr_hole].ball_x);
|
||||
ball_y = INT_TO_FP(course[curr_hole].ball_y);
|
||||
ball_vx = 0;
|
||||
ball_vy = 0;
|
||||
saved_x = ball_x;
|
||||
saved_y = ball_y;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Main
|
||||
* ================================================================ */
|
||||
|
||||
int main(void) {
|
||||
gfx_enter();
|
||||
|
||||
/* Initialize first hole */
|
||||
curr_hole = 0;
|
||||
strokes = 0;
|
||||
total_strokes = 0;
|
||||
ball_in_hole = 0;
|
||||
ball_moving = 0;
|
||||
aim_angle = 0;
|
||||
power = 3;
|
||||
ball_x = INT_TO_FP(course[0].ball_x);
|
||||
ball_y = INT_TO_FP(course[0].ball_y);
|
||||
ball_vx = 0;
|
||||
ball_vy = 0;
|
||||
saved_x = ball_x;
|
||||
saved_y = ball_y;
|
||||
|
||||
int quit = 0;
|
||||
while (!quit) {
|
||||
int r = handle_input();
|
||||
if (r == 1) break; /* Quit */
|
||||
if (r == 2) { quit = 2; break; } /* Game complete */
|
||||
|
||||
if (ball_moving) {
|
||||
update_physics();
|
||||
}
|
||||
|
||||
draw_hole();
|
||||
|
||||
for (int i = 0; i < 2; i++) yield();
|
||||
}
|
||||
|
||||
/* Show final score */
|
||||
if (quit == 2) {
|
||||
total_strokes += strokes;
|
||||
/* Show completion screen */
|
||||
gfx_clear(C_BG);
|
||||
gfx_rect_t box = {SW/2 - 60, SH/2 - 20, 120, 40, GFX_BLUE};
|
||||
gfx_fill_rect(&box);
|
||||
/* Score display */
|
||||
draw_number(SW/2 - 10, SH/2 - 5, total_strokes, C_TEXT);
|
||||
/* Wait */
|
||||
for (int i = 0; i < 300; i++) yield();
|
||||
}
|
||||
|
||||
gfx_leave();
|
||||
|
||||
puts("Minigolf complete!\n");
|
||||
puts("Total strokes: ");
|
||||
char tmp[8];
|
||||
int ti = 0, s = total_strokes;
|
||||
if (s == 0) putchar('0');
|
||||
else { while (s > 0) { tmp[ti++] = '0' + (char)(s % 10); s /= 10; } while (ti > 0) putchar(tmp[--ti]); }
|
||||
puts("\n");
|
||||
|
||||
/* Calculate par total */
|
||||
int total_par = 0;
|
||||
for (int i = 0; i < MAX_HOLES; i++) total_par += course[i].par;
|
||||
puts("Par: ");
|
||||
ti = 0; s = total_par;
|
||||
if (s == 0) putchar('0');
|
||||
else { while (s > 0) { tmp[ti++] = '0' + (char)(s % 10); s /= 10; } while (ti > 0) putchar(tmp[--ti]); }
|
||||
puts("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
511
apps/mkfs.fat32/mkfs.fat32.c
Normal file
511
apps/mkfs.fat32/mkfs.fat32.c
Normal file
@@ -0,0 +1,511 @@
|
||||
/**
|
||||
* @file mkfs.fat32.c
|
||||
* @brief Format a block device with a FAT32 filesystem.
|
||||
*
|
||||
* Writes the FAT32 boot sector (BPB), FSInfo sector, two File Allocation
|
||||
* Tables, and an empty root directory cluster to the specified device.
|
||||
*
|
||||
* Usage: mkfs.fat32 <device> <total_sectors>
|
||||
* e.g.: mkfs.fat32 hdd1mbr1 14336
|
||||
*
|
||||
* The device is accessed via /dev/<device>. Total sectors must be
|
||||
* specified because the OS does not yet expose device size to userspace.
|
||||
*/
|
||||
|
||||
#include "syscalls.h"
|
||||
|
||||
typedef unsigned char uint8_t;
|
||||
typedef unsigned short uint16_t;
|
||||
|
||||
/* ================================================================
|
||||
* Constants
|
||||
* ================================================================ */
|
||||
|
||||
#define SECTOR_SIZE 512
|
||||
#define RESERVED_SECTS 32
|
||||
#define NUM_FATS 2
|
||||
#define ROOT_CLUSTER 2
|
||||
#define FSINFO_SECTOR 1
|
||||
#define BACKUP_BOOT 6
|
||||
|
||||
/* ================================================================
|
||||
* Helpers
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Print a 32-bit decimal value.
|
||||
*/
|
||||
static void print_dec(uint32_t val) {
|
||||
char buf[12];
|
||||
int pos = 11;
|
||||
buf[pos] = '\0';
|
||||
if (val == 0) {
|
||||
buf[--pos] = '0';
|
||||
} else {
|
||||
while (val > 0 && pos > 0) {
|
||||
buf[--pos] = (char)('0' + (val % 10));
|
||||
val /= 10;
|
||||
}
|
||||
}
|
||||
puts(&buf[pos]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a hex value.
|
||||
*/
|
||||
static void print_hex(uint32_t val) {
|
||||
char buf[11];
|
||||
buf[0] = '0';
|
||||
buf[1] = 'x';
|
||||
for (int i = 9; i >= 2; i--) {
|
||||
int n = (int)(val & 0xF);
|
||||
buf[i] = (n < 10) ? (char)('0' + n) : (char)('A' + n - 10);
|
||||
val >>= 4;
|
||||
}
|
||||
buf[10] = '\0';
|
||||
puts(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse decimal string.
|
||||
*/
|
||||
static uint32_t parse_dec(const char *s, int *ok) {
|
||||
uint32_t val = 0;
|
||||
*ok = 0;
|
||||
if (!s || !*s) return 0;
|
||||
while (*s) {
|
||||
if (*s < '0' || *s > '9') { *ok = 0; return 0; }
|
||||
val = val * 10 + (uint32_t)(*s - '0');
|
||||
s++;
|
||||
}
|
||||
*ok = 1;
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a 16-bit little-endian value.
|
||||
*/
|
||||
static void w16(uint8_t *p, uint32_t val) {
|
||||
p[0] = (uint8_t)(val & 0xFF);
|
||||
p[1] = (uint8_t)((val >> 8) & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a 32-bit little-endian value.
|
||||
*/
|
||||
static void w32(uint8_t *p, uint32_t val) {
|
||||
p[0] = (uint8_t)(val & 0xFF);
|
||||
p[1] = (uint8_t)((val >> 8) & 0xFF);
|
||||
p[2] = (uint8_t)((val >> 16) & 0xFF);
|
||||
p[3] = (uint8_t)((val >> 24) & 0xFF);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Argument parsing
|
||||
* ================================================================ */
|
||||
|
||||
#define MAX_TOKENS 4
|
||||
static char tok_buf[256];
|
||||
static char *tokens[MAX_TOKENS];
|
||||
static int token_count;
|
||||
|
||||
static void tokenize(const char *input) {
|
||||
int i = 0;
|
||||
while (input[i] && i < 255) { tok_buf[i] = input[i]; i++; }
|
||||
tok_buf[i] = '\0';
|
||||
token_count = 0;
|
||||
char *p = tok_buf;
|
||||
while (*p && token_count < MAX_TOKENS) {
|
||||
while (*p == ' ') p++;
|
||||
if (!*p) break;
|
||||
tokens[token_count++] = p;
|
||||
while (*p && *p != ' ') p++;
|
||||
if (*p) { *p = '\0'; p++; }
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Sector buffer (reused for each write)
|
||||
* ================================================================ */
|
||||
|
||||
static uint8_t sector[SECTOR_SIZE];
|
||||
|
||||
/**
|
||||
* Write the sector buffer to the device fd.
|
||||
* Returns 0 on success, -1 on failure.
|
||||
*/
|
||||
static int write_sector(int32_t fd) {
|
||||
int32_t n = write(fd, sector, SECTOR_SIZE);
|
||||
return (n == SECTOR_SIZE) ? 0 : -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write N zero sectors to the device fd.
|
||||
*/
|
||||
static int write_zero_sectors(int32_t fd, uint32_t count) {
|
||||
memset(sector, 0, SECTOR_SIZE);
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
if (write_sector(fd) != 0) return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* FAT32 geometry calculation
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Determine sectors per cluster based on volume size.
|
||||
* Uses Microsoft's recommended values for FAT32.
|
||||
*/
|
||||
static uint32_t choose_spc(uint32_t total_sectors) {
|
||||
/* For small volumes, use small clusters to maximize cluster count.
|
||||
* FAT32 "officially" needs >= 65525 clusters, but our driver
|
||||
* doesn't enforce this minimum. */
|
||||
if (total_sectors <= 16384) return 1; /* <= 8 MiB: 512B */
|
||||
if (total_sectors <= 131072) return 8; /* <= 64 MiB: 4K */
|
||||
if (total_sectors <= 524288) return 8; /* <= 256 MiB: 4K */
|
||||
if (total_sectors <= 16777216) return 8; /* <= 8 GiB: 4K */
|
||||
if (total_sectors <= 33554432) return 16; /* <= 16 GiB: 8K */
|
||||
return 32; /* > 16 GiB: 16K */
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate FAT size in sectors using Microsoft's formula.
|
||||
*
|
||||
* fat_size = ceil((total - reserved) / (entries_per_sec * spc + num_fats))
|
||||
* where entries_per_sec = 128 (512 / 4 bytes per FAT32 entry).
|
||||
*/
|
||||
static uint32_t calc_fat_size(uint32_t total_sectors, uint32_t spc) {
|
||||
uint32_t data_area = total_sectors - RESERVED_SECTS;
|
||||
uint32_t tmp = 128 * spc + NUM_FATS;
|
||||
return (data_area + tmp - 1) / tmp;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* BPB / Boot sector construction
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Build the FAT32 boot sector (BPB) into the sector buffer.
|
||||
*/
|
||||
static void build_boot_sector(uint32_t total_sectors, uint32_t spc,
|
||||
uint32_t fat_size, const char *label) {
|
||||
memset(sector, 0, SECTOR_SIZE);
|
||||
|
||||
/* Jump instruction: EB 58 90 (jump over BPB to boot code area) */
|
||||
sector[0] = 0xEB;
|
||||
sector[1] = 0x58;
|
||||
sector[2] = 0x90;
|
||||
|
||||
/* OEM Name (offset 3, 8 bytes) */
|
||||
memcpy(§or[3], "CLAUDEOS", 8);
|
||||
|
||||
/* BPB — BIOS Parameter Block */
|
||||
w16(§or[11], SECTOR_SIZE); /* bytes_per_sector */
|
||||
sector[13] = (uint8_t)spc; /* sectors_per_cluster */
|
||||
w16(§or[14], RESERVED_SECTS); /* reserved_sectors */
|
||||
sector[16] = NUM_FATS; /* num_fats */
|
||||
w16(§or[17], 0); /* root_entry_count (0 for FAT32) */
|
||||
w16(§or[19], 0); /* total_sectors_16 (0, use 32-bit) */
|
||||
sector[21] = 0xF8; /* media_type (hard disk) */
|
||||
w16(§or[22], 0); /* fat_size_16 (0, use 32-bit) */
|
||||
w16(§or[24], 63); /* sectors_per_track */
|
||||
w16(§or[26], 255); /* num_heads */
|
||||
w32(§or[28], 0); /* hidden_sectors */
|
||||
w32(§or[32], total_sectors); /* total_sectors_32 */
|
||||
|
||||
/* FAT32 Extended BPB (offset 36) */
|
||||
w32(§or[36], fat_size); /* fat_size_32 */
|
||||
w16(§or[40], 0); /* ext_flags */
|
||||
w16(§or[42], 0); /* fs_version */
|
||||
w32(§or[44], ROOT_CLUSTER); /* root_cluster */
|
||||
w16(§or[48], FSINFO_SECTOR); /* fs_info sector */
|
||||
w16(§or[50], BACKUP_BOOT); /* backup_boot sector */
|
||||
/* Reserved (12 bytes at offset 52, already zeroed) */
|
||||
|
||||
sector[64] = 0x80; /* drive_number (HDD) */
|
||||
sector[65] = 0; /* reserved1 */
|
||||
sector[66] = 0x29; /* boot_sig (extended boot sig) */
|
||||
|
||||
/* Volume serial number (we'll use a simple hash) */
|
||||
uint32_t serial = total_sectors ^ (spc << 16) ^ 0xC1A0DE05;
|
||||
w32(§or[67], serial);
|
||||
|
||||
/* Volume label (11 bytes, space-padded) */
|
||||
int li = 0;
|
||||
while (label[li] && li < 11) {
|
||||
char c = label[li];
|
||||
/* Convert to uppercase for FAT compatibility */
|
||||
if (c >= 'a' && c <= 'z') c = (char)(c - 32);
|
||||
sector[71 + li] = (uint8_t)c;
|
||||
li++;
|
||||
}
|
||||
while (li < 11) {
|
||||
sector[71 + li] = ' ';
|
||||
li++;
|
||||
}
|
||||
|
||||
/* FS type string (8 bytes) */
|
||||
memcpy(§or[82], "FAT32 ", 8);
|
||||
|
||||
/* Boot sector signature */
|
||||
sector[510] = 0x55;
|
||||
sector[511] = 0xAA;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* FSInfo sector construction
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Build the FSInfo sector into the sector buffer.
|
||||
*/
|
||||
static void build_fsinfo(uint32_t free_clusters, uint32_t next_free) {
|
||||
memset(sector, 0, SECTOR_SIZE);
|
||||
|
||||
/* FSInfo signatures */
|
||||
w32(§or[0], 0x41615252); /* Lead signature */
|
||||
w32(§or[484], 0x61417272); /* Structure signature */
|
||||
w32(§or[488], free_clusters); /* Free cluster count */
|
||||
w32(§or[492], next_free); /* Next free cluster hint */
|
||||
w32(§or[508], 0xAA550000); /* Trail signature */
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* FAT table construction
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Build the first sector of the FAT into the sector buffer.
|
||||
* Entry 0 = media byte marker, Entry 1 = EOC, Entry 2 = EOC (root dir).
|
||||
*/
|
||||
static void build_fat_first_sector(void) {
|
||||
memset(sector, 0, SECTOR_SIZE);
|
||||
|
||||
/* FAT entry 0: media type in low byte, rest 0xFF */
|
||||
w32(§or[0], 0x0FFFFFF8);
|
||||
|
||||
/* FAT entry 1: end-of-chain marker */
|
||||
w32(§or[4], 0x0FFFFFFF);
|
||||
|
||||
/* FAT entry 2: root directory cluster (end-of-chain) */
|
||||
w32(§or[8], 0x0FFFFFFF);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Root directory construction
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Build the root directory cluster's first sector.
|
||||
* Contains a volume label entry only.
|
||||
*/
|
||||
static void build_root_dir(const char *label) {
|
||||
memset(sector, 0, SECTOR_SIZE);
|
||||
|
||||
/* Volume label directory entry (32 bytes) */
|
||||
int li = 0;
|
||||
while (label[li] && li < 11) {
|
||||
char c = label[li];
|
||||
if (c >= 'a' && c <= 'z') c = (char)(c - 32);
|
||||
sector[li] = (uint8_t)c;
|
||||
li++;
|
||||
}
|
||||
while (li < 11) {
|
||||
sector[li] = ' ';
|
||||
li++;
|
||||
}
|
||||
sector[11] = 0x08; /* ATTR_VOLUME_ID */
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Main formatting routine
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Format the device with FAT32.
|
||||
*
|
||||
* Writes sectors sequentially from sector 0:
|
||||
* [0] Boot sector (BPB)
|
||||
* [1] FSInfo
|
||||
* [2..5] Zero (reserved)
|
||||
* [6] Backup boot sector
|
||||
* [7] Backup FSInfo
|
||||
* [8..31] Zero (reserved)
|
||||
* [32..32+fat_size-1] FAT 1
|
||||
* [32+fat_size..32+2*fat_size-1] FAT 2
|
||||
* [data_start..data_start+spc-1] Root directory cluster
|
||||
*/
|
||||
static int format_device(const char *dev_path, uint32_t total_sectors,
|
||||
const char *label) {
|
||||
uint32_t spc = choose_spc(total_sectors);
|
||||
uint32_t fat_size = calc_fat_size(total_sectors, spc);
|
||||
uint32_t data_start = RESERVED_SECTS + NUM_FATS * fat_size;
|
||||
uint32_t data_sectors = total_sectors - data_start;
|
||||
uint32_t total_clusters = data_sectors / spc;
|
||||
uint32_t free_clusters = total_clusters - 1; /* Root dir uses cluster 2 */
|
||||
|
||||
/* Print format parameters */
|
||||
puts("Formatting ");
|
||||
puts(dev_path);
|
||||
puts(" as FAT32\n");
|
||||
puts(" Total sectors: ");
|
||||
print_dec(total_sectors);
|
||||
puts("\n Sectors/cluster: ");
|
||||
print_dec(spc);
|
||||
puts("\n Reserved sectors: ");
|
||||
print_dec(RESERVED_SECTS);
|
||||
puts("\n FAT size (sectors): ");
|
||||
print_dec(fat_size);
|
||||
puts("\n Number of FATs: ");
|
||||
print_dec(NUM_FATS);
|
||||
puts("\n Data start sector: ");
|
||||
print_dec(data_start);
|
||||
puts("\n Total clusters: ");
|
||||
print_dec(total_clusters);
|
||||
puts("\n Volume label: ");
|
||||
puts(label);
|
||||
puts("\n");
|
||||
|
||||
/* Sanity checks */
|
||||
if (total_sectors < 128) {
|
||||
puts("Error: volume too small for FAT32\n");
|
||||
return -1;
|
||||
}
|
||||
if (data_start >= total_sectors) {
|
||||
puts("Error: FAT tables don't fit in volume\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Open device for writing */
|
||||
int32_t fd = open(dev_path, 0);
|
||||
if (fd < 0) {
|
||||
puts("Error: cannot open ");
|
||||
puts(dev_path);
|
||||
puts("\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
puts("Writing boot sector...\n");
|
||||
|
||||
/* Sector 0: Boot sector */
|
||||
build_boot_sector(total_sectors, spc, fat_size, label);
|
||||
if (write_sector(fd) != 0) goto fail;
|
||||
|
||||
/* Sector 1: FSInfo */
|
||||
build_fsinfo(free_clusters, 3); /* Next free = cluster 3 */
|
||||
if (write_sector(fd) != 0) goto fail;
|
||||
|
||||
/* Sectors 2-5: zero */
|
||||
if (write_zero_sectors(fd, 4) != 0) goto fail;
|
||||
|
||||
/* Sector 6: Backup boot sector */
|
||||
build_boot_sector(total_sectors, spc, fat_size, label);
|
||||
if (write_sector(fd) != 0) goto fail;
|
||||
|
||||
/* Sector 7: Backup FSInfo */
|
||||
build_fsinfo(free_clusters, 3);
|
||||
if (write_sector(fd) != 0) goto fail;
|
||||
|
||||
/* Sectors 8-31: zero (remaining reserved area) */
|
||||
if (write_zero_sectors(fd, RESERVED_SECTS - 8) != 0) goto fail;
|
||||
|
||||
puts("Writing FAT 1...\n");
|
||||
|
||||
/* FAT 1: first sector has entries 0-2 */
|
||||
build_fat_first_sector();
|
||||
if (write_sector(fd) != 0) goto fail;
|
||||
|
||||
/* FAT 1: remaining sectors (all zero = free) */
|
||||
if (fat_size > 1) {
|
||||
if (write_zero_sectors(fd, fat_size - 1) != 0) goto fail;
|
||||
}
|
||||
|
||||
puts("Writing FAT 2...\n");
|
||||
|
||||
/* FAT 2: identical copy */
|
||||
build_fat_first_sector();
|
||||
if (write_sector(fd) != 0) goto fail;
|
||||
if (fat_size > 1) {
|
||||
if (write_zero_sectors(fd, fat_size - 1) != 0) goto fail;
|
||||
}
|
||||
|
||||
puts("Writing root directory...\n");
|
||||
|
||||
/* Root directory cluster (first sector has volume label) */
|
||||
build_root_dir(label);
|
||||
if (write_sector(fd) != 0) goto fail;
|
||||
|
||||
/* Remaining sectors in root cluster (zero) */
|
||||
if (spc > 1) {
|
||||
if (write_zero_sectors(fd, spc - 1) != 0) goto fail;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
|
||||
puts("Format complete!\n");
|
||||
puts(" ");
|
||||
print_dec(data_start + spc);
|
||||
puts(" sectors written\n");
|
||||
|
||||
return 0;
|
||||
|
||||
fail:
|
||||
close(fd);
|
||||
puts("Error: write failed!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Main
|
||||
* ================================================================ */
|
||||
|
||||
int main(void) {
|
||||
char arg1[256];
|
||||
if (getenv("ARG1", arg1, sizeof(arg1)) < 0 || arg1[0] == '\0') {
|
||||
puts("mkfs.fat32 - Format a device with FAT32\n");
|
||||
puts("\n");
|
||||
puts("Usage: mkfs.fat32 <device> <total_sectors> [label]\n");
|
||||
puts("\n");
|
||||
puts(" device: Device name (e.g., hdd1mbr1)\n");
|
||||
puts(" total_sectors: Number of 512-byte sectors\n");
|
||||
puts(" label: Volume label (default: CLAUDEOS)\n");
|
||||
puts("\n");
|
||||
puts("Example: mkfs.fat32 hdd1mbr1 14336\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
tokenize(arg1);
|
||||
if (token_count < 2) {
|
||||
puts("mkfs.fat32: need <device> and <total_sectors>\n");
|
||||
puts(" Use 'diskpart <dev> list' to find sector count.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Build /dev/<device> path */
|
||||
char dev_path[64];
|
||||
strcpy(dev_path, "/dev/");
|
||||
char *dp = dev_path + 5;
|
||||
char *dn = tokens[0];
|
||||
while (*dn && dp < dev_path + 62) *dp++ = *dn++;
|
||||
*dp = '\0';
|
||||
|
||||
/* Parse total sectors */
|
||||
int ok;
|
||||
uint32_t total_sectors = parse_dec(tokens[1], &ok);
|
||||
if (!ok || total_sectors == 0) {
|
||||
puts("mkfs.fat32: invalid sector count '");
|
||||
puts(tokens[1]);
|
||||
puts("'\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Volume label (default "CLAUDEOS") */
|
||||
const char *label = "CLAUDEOS";
|
||||
if (token_count >= 3) {
|
||||
label = tokens[2];
|
||||
}
|
||||
|
||||
return (format_device(dev_path, total_sectors, label) == 0) ? 0 : 1;
|
||||
}
|
||||
95
apps/mount/mount.c
Normal file
95
apps/mount/mount.c
Normal file
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* @file mount.c
|
||||
* @brief Mount a block device as FAT32.
|
||||
*
|
||||
* Writes to /sys/vfs/mount to trigger a kernel-level FAT32 mount.
|
||||
*
|
||||
* Usage: mount <device> <path>
|
||||
* e.g.: mount hdd1mbr1 /mnt
|
||||
*/
|
||||
|
||||
#include "syscalls.h"
|
||||
|
||||
int main(void) {
|
||||
char device[64];
|
||||
char path[128];
|
||||
|
||||
if (getenv("ARG1", device, sizeof(device)) < 0 || device[0] == '\0') {
|
||||
puts("Usage: mount <device> <mountpoint>\n");
|
||||
puts(" e.g.: mount hdd1mbr1 /mnt\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ARG1 contains "device path" — we need to split it.
|
||||
* Actually, the shell currently only passes the first word as ARG1.
|
||||
* Let's accept: mount <device> <path> where ARG1 = "device path" */
|
||||
|
||||
/* Find the space separating device and path */
|
||||
int i = 0;
|
||||
while (device[i] && device[i] != ' ') i++;
|
||||
|
||||
if (device[i] == ' ') {
|
||||
/* ARG1 contains both: "hdd1mbr1 /mnt" */
|
||||
device[i] = '\0';
|
||||
/* Copy path part */
|
||||
int j = i + 1;
|
||||
while (device[j] == ' ') j++;
|
||||
int pi = 0;
|
||||
while (device[j] && pi < 127) {
|
||||
path[pi++] = device[j++];
|
||||
}
|
||||
path[pi] = '\0';
|
||||
} else {
|
||||
/* Only device, no path — default to /mnt */
|
||||
path[0] = '/'; path[1] = 'm'; path[2] = 'n'; path[3] = 't';
|
||||
path[4] = '\0';
|
||||
}
|
||||
|
||||
if (path[0] == '\0') {
|
||||
puts("mount: missing mountpoint, using /mnt\n");
|
||||
path[0] = '/'; path[1] = 'm'; path[2] = 'n'; path[3] = 't';
|
||||
path[4] = '\0';
|
||||
}
|
||||
|
||||
/* Build the mount command: "device path" */
|
||||
char cmd[192];
|
||||
int pos = 0;
|
||||
for (int k = 0; device[k] && pos < 190; k++) cmd[pos++] = device[k];
|
||||
cmd[pos++] = ' ';
|
||||
for (int k = 0; path[k] && pos < 190; k++) cmd[pos++] = path[k];
|
||||
cmd[pos] = '\0';
|
||||
|
||||
/* Open /sys/vfs/mount and write the command */
|
||||
int32_t fd = open("/sys/vfs/mount", 0);
|
||||
if (fd < 0) {
|
||||
puts("mount: cannot open /sys/vfs/mount\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int32_t n = write(fd, cmd, (uint32_t)pos);
|
||||
close(fd);
|
||||
|
||||
if (n < 0) {
|
||||
/* Read the status to show the error */
|
||||
fd = open("/sys/vfs/mounts", 0);
|
||||
if (fd >= 0) {
|
||||
char status[256];
|
||||
int32_t sn = read(fd, status, sizeof(status) - 1);
|
||||
close(fd);
|
||||
if (sn > 0) {
|
||||
status[sn] = '\0';
|
||||
puts(status);
|
||||
}
|
||||
} else {
|
||||
puts("mount: failed\n");
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
puts("mount: ");
|
||||
puts(device);
|
||||
puts(" mounted at ");
|
||||
puts(path);
|
||||
puts("\n");
|
||||
return 0;
|
||||
}
|
||||
582
apps/pool/pool.c
Normal file
582
apps/pool/pool.c
Normal file
@@ -0,0 +1,582 @@
|
||||
/**
|
||||
* @file pool.c
|
||||
* @brief Simple pool (billiards) game for ClaudeOS.
|
||||
*
|
||||
* Uses VGA mode 0x13 (320x200, 256 colors) via the graphics subsystem.
|
||||
*
|
||||
* Controls:
|
||||
* Left/Right arrows (or A/D) - Aim the cue
|
||||
* Up/Down arrows (or W/S) - Adjust shot power
|
||||
* Space or Enter - Shoot
|
||||
* Q or Escape - Quit
|
||||
*
|
||||
* Simplified 8-ball pool:
|
||||
* - 1 cue ball (white) + 7 colored balls
|
||||
* - Pot balls into the 6 pockets
|
||||
* - Pot the cue ball = foul (ball resets)
|
||||
*/
|
||||
|
||||
#include "syscalls.h"
|
||||
|
||||
/* ================================================================
|
||||
* Fixed-point math (16.16 format)
|
||||
* ================================================================ */
|
||||
|
||||
typedef int32_t fixed_t;
|
||||
|
||||
#define FP_SHIFT 16
|
||||
#define FP_ONE (1 << FP_SHIFT)
|
||||
#define FP_HALF (FP_ONE / 2)
|
||||
|
||||
#define INT_TO_FP(x) ((fixed_t)(x) << FP_SHIFT)
|
||||
#define FP_TO_INT(x) ((int)((x) >> FP_SHIFT))
|
||||
#define FP_MUL(a, b) ((fixed_t)(((int32_t)(a) * (int32_t)(b)) >> FP_SHIFT))
|
||||
#define FP_DIV(a, b) ((fixed_t)(((int32_t)(a) << FP_SHIFT) / (b)))
|
||||
|
||||
/** Integer square root (for fixed-point magnitude). */
|
||||
static uint32_t isqrt(uint32_t n) {
|
||||
if (n == 0) return 0;
|
||||
uint32_t x = n;
|
||||
uint32_t y = (x + 1) / 2;
|
||||
while (y < x) {
|
||||
x = y;
|
||||
y = (x + n / x) / 2;
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
static fixed_t fp_sqrt(fixed_t x) {
|
||||
if (x <= 0) return 0;
|
||||
return (fixed_t)isqrt((uint32_t)x << FP_SHIFT);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Simple sin/cos lookup (256 entries for angle 0-255 == 0-360 deg)
|
||||
* Values in 16.16 fixed point.
|
||||
* Using a 64-entry quarter-wave table.
|
||||
* ================================================================ */
|
||||
|
||||
/** Pre-computed sine table for angles 0..63 (quarter wave, 0 to PI/2).
|
||||
* Values are 16.16 fixed-point. */
|
||||
static const fixed_t sin_table_q[65] = {
|
||||
0, 1608, 3216, 4821, 6424, 8022, 9616, 11204,
|
||||
12785, 14359, 15924, 17479, 19024, 20557, 22078, 23586,
|
||||
25080, 26558, 28020, 29466, 30893, 32303, 33692, 35062,
|
||||
36410, 37736, 39040, 40320, 41576, 42806, 44011, 45190,
|
||||
46341, 47464, 48559, 49624, 50660, 51665, 52639, 53581,
|
||||
54491, 55368, 56212, 57022, 57798, 58538, 59244, 59914,
|
||||
60547, 61145, 61705, 62228, 62714, 63162, 63572, 63944,
|
||||
64277, 64571, 64827, 65043, 65220, 65358, 65457, 65516,
|
||||
65536
|
||||
};
|
||||
|
||||
/** Get sine for angle (0-255 maps to 0-360 degrees), returns 16.16 fixed. */
|
||||
static fixed_t fp_sin(int angle) {
|
||||
angle = angle & 255;
|
||||
int quadrant = angle >> 6; /* 0-3 */
|
||||
int idx = angle & 63;
|
||||
|
||||
fixed_t val;
|
||||
switch (quadrant) {
|
||||
case 0: val = sin_table_q[idx]; break;
|
||||
case 1: val = sin_table_q[64 - idx]; break;
|
||||
case 2: val = -sin_table_q[idx]; break;
|
||||
case 3: val = -sin_table_q[64 - idx]; break;
|
||||
default: val = 0; break;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
static fixed_t fp_cos(int angle) {
|
||||
return fp_sin(angle + 64);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Game constants
|
||||
* ================================================================ */
|
||||
|
||||
#define SCREEN_W 320
|
||||
#define SCREEN_H 200
|
||||
|
||||
/* Table dimensions (inner playing area) */
|
||||
#define TABLE_X 30
|
||||
#define TABLE_Y 20
|
||||
#define TABLE_W 260
|
||||
#define TABLE_H 160
|
||||
#define TABLE_RIGHT (TABLE_X + TABLE_W)
|
||||
#define TABLE_BOTTOM (TABLE_Y + TABLE_H)
|
||||
|
||||
/* Bumper/rail width */
|
||||
#define RAIL_W 6
|
||||
|
||||
/* Ball properties */
|
||||
#define BALL_RADIUS 4
|
||||
#define NUM_BALLS 8 /* 1 cue + 7 object balls */
|
||||
|
||||
/* Pocket properties */
|
||||
#define POCKET_RADIUS 8
|
||||
#define NUM_POCKETS 6
|
||||
|
||||
/* Physics */
|
||||
#define FRICTION (FP_ONE - FP_ONE / 100) /* ~0.99 */
|
||||
#define MIN_SPEED (FP_ONE / 8) /* Below this, stop the ball */
|
||||
#define MAX_POWER INT_TO_FP(6)
|
||||
|
||||
/* Colors */
|
||||
#define COL_FELT GFX_GREEN /* 2: green */
|
||||
#define COL_RAIL GFX_BROWN /* 6: brown */
|
||||
#define COL_POCKET GFX_BLACK /* 0: black */
|
||||
#define COL_CUE_BALL GFX_WHITE /* 15: white */
|
||||
#define COL_AIM GFX_LIGHT_GREY /* 7 */
|
||||
#define COL_POWER GFX_LIGHT_RED /* 12 */
|
||||
#define COL_TEXT GFX_WHITE /* 15 */
|
||||
#define COL_BG GFX_DARK_GREY /* 8 */
|
||||
|
||||
/* Object ball colors */
|
||||
static const uint32_t ball_colors[7] = {
|
||||
GFX_YELLOW, /* Ball 1: Yellow */
|
||||
GFX_BLUE, /* Ball 2: Blue */
|
||||
GFX_RED, /* Ball 3: Red */
|
||||
GFX_MAGENTA, /* Ball 4: Purple */
|
||||
GFX_LIGHT_RED, /* Ball 5: Orange-ish */
|
||||
GFX_LIGHT_GREEN, /* Ball 6: Light Green */
|
||||
GFX_LIGHT_CYAN, /* Ball 7: Light Cyan */
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* Game state
|
||||
* ================================================================ */
|
||||
|
||||
typedef struct {
|
||||
fixed_t x, y; /* Position (fixed-point) */
|
||||
fixed_t vx, vy; /* Velocity (fixed-point) */
|
||||
uint32_t color; /* Palette color index */
|
||||
int active; /* 1 if on table, 0 if potted */
|
||||
} ball_t;
|
||||
|
||||
typedef struct {
|
||||
fixed_t x, y; /* Center position */
|
||||
} pocket_t;
|
||||
|
||||
static ball_t balls[NUM_BALLS];
|
||||
static pocket_t pockets[NUM_POCKETS];
|
||||
static int aim_angle = 0; /* 0-255 */
|
||||
static int shot_power = 3; /* 1-6 */
|
||||
static int balls_moving = 0; /* Nonzero while physics is running */
|
||||
static int score = 0;
|
||||
static int game_over = 0;
|
||||
static int foul = 0; /* Set when cue ball potted */
|
||||
|
||||
/* ================================================================
|
||||
* Initialization
|
||||
* ================================================================ */
|
||||
|
||||
static void init_pockets(void) {
|
||||
/* 6 pockets: 4 corners + 2 side midpoints */
|
||||
pockets[0] = (pocket_t){INT_TO_FP(TABLE_X), INT_TO_FP(TABLE_Y)};
|
||||
pockets[1] = (pocket_t){INT_TO_FP(TABLE_X + TABLE_W/2), INT_TO_FP(TABLE_Y)};
|
||||
pockets[2] = (pocket_t){INT_TO_FP(TABLE_RIGHT), INT_TO_FP(TABLE_Y)};
|
||||
pockets[3] = (pocket_t){INT_TO_FP(TABLE_X), INT_TO_FP(TABLE_BOTTOM)};
|
||||
pockets[4] = (pocket_t){INT_TO_FP(TABLE_X + TABLE_W/2), INT_TO_FP(TABLE_BOTTOM)};
|
||||
pockets[5] = (pocket_t){INT_TO_FP(TABLE_RIGHT), INT_TO_FP(TABLE_BOTTOM)};
|
||||
}
|
||||
|
||||
static void init_balls(void) {
|
||||
/* Cue ball on left side */
|
||||
balls[0].x = INT_TO_FP(TABLE_X + TABLE_W / 4);
|
||||
balls[0].y = INT_TO_FP(TABLE_Y + TABLE_H / 2);
|
||||
balls[0].vx = 0;
|
||||
balls[0].vy = 0;
|
||||
balls[0].color = COL_CUE_BALL;
|
||||
balls[0].active = 1;
|
||||
|
||||
/* Object balls in a triangle formation on right side */
|
||||
fixed_t start_x = INT_TO_FP(TABLE_X + TABLE_W * 3 / 4);
|
||||
fixed_t start_y = INT_TO_FP(TABLE_Y + TABLE_H / 2);
|
||||
int ball_idx = 1;
|
||||
|
||||
/* Row 1: 1 ball */
|
||||
balls[ball_idx].x = start_x;
|
||||
balls[ball_idx].y = start_y;
|
||||
balls[ball_idx].color = ball_colors[0];
|
||||
balls[ball_idx].active = 1;
|
||||
balls[ball_idx].vx = 0;
|
||||
balls[ball_idx].vy = 0;
|
||||
ball_idx++;
|
||||
|
||||
/* Row 2: 2 balls */
|
||||
for (int i = 0; i < 2 && ball_idx < NUM_BALLS; i++) {
|
||||
balls[ball_idx].x = start_x + INT_TO_FP(BALL_RADIUS * 2 + 1);
|
||||
balls[ball_idx].y = start_y + INT_TO_FP((i * 2 - 1) * (BALL_RADIUS + 1));
|
||||
balls[ball_idx].color = ball_colors[ball_idx - 1];
|
||||
balls[ball_idx].active = 1;
|
||||
balls[ball_idx].vx = 0;
|
||||
balls[ball_idx].vy = 0;
|
||||
ball_idx++;
|
||||
}
|
||||
|
||||
/* Row 3: 3 balls */
|
||||
for (int i = 0; i < 3 && ball_idx < NUM_BALLS; i++) {
|
||||
balls[ball_idx].x = start_x + INT_TO_FP(BALL_RADIUS * 4 + 2);
|
||||
balls[ball_idx].y = start_y + INT_TO_FP((i - 1) * (BALL_RADIUS * 2 + 1));
|
||||
balls[ball_idx].color = ball_colors[ball_idx - 1];
|
||||
balls[ball_idx].active = 1;
|
||||
balls[ball_idx].vx = 0;
|
||||
balls[ball_idx].vy = 0;
|
||||
ball_idx++;
|
||||
}
|
||||
|
||||
/* Row 4: remaining balls */
|
||||
for (int i = 0; ball_idx < NUM_BALLS; i++) {
|
||||
balls[ball_idx].x = start_x + INT_TO_FP(BALL_RADIUS * 6 + 3);
|
||||
balls[ball_idx].y = start_y + INT_TO_FP((i * 2 - 1) * (BALL_RADIUS + 1));
|
||||
balls[ball_idx].color = ball_colors[ball_idx - 1];
|
||||
balls[ball_idx].active = 1;
|
||||
balls[ball_idx].vx = 0;
|
||||
balls[ball_idx].vy = 0;
|
||||
ball_idx++;
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Physics
|
||||
* ================================================================ */
|
||||
|
||||
static void check_wall_collisions(ball_t *b) {
|
||||
fixed_t left = INT_TO_FP(TABLE_X + RAIL_W + BALL_RADIUS);
|
||||
fixed_t right = INT_TO_FP(TABLE_RIGHT - RAIL_W - BALL_RADIUS);
|
||||
fixed_t top = INT_TO_FP(TABLE_Y + RAIL_W + BALL_RADIUS);
|
||||
fixed_t bottom = INT_TO_FP(TABLE_BOTTOM - RAIL_W - BALL_RADIUS);
|
||||
|
||||
if (b->x < left) { b->x = left; b->vx = -b->vx; }
|
||||
if (b->x > right) { b->x = right; b->vx = -b->vx; }
|
||||
if (b->y < top) { b->y = top; b->vy = -b->vy; }
|
||||
if (b->y > bottom) { b->y = bottom; b->vy = -b->vy; }
|
||||
}
|
||||
|
||||
static void check_pocket(ball_t *b, int ball_idx) {
|
||||
for (int p = 0; p < NUM_POCKETS; p++) {
|
||||
fixed_t dx = b->x - pockets[p].x;
|
||||
fixed_t dy = b->y - pockets[p].y;
|
||||
fixed_t dist_sq = FP_MUL(dx, dx) + FP_MUL(dy, dy);
|
||||
fixed_t pocket_r = INT_TO_FP(POCKET_RADIUS);
|
||||
|
||||
if (dist_sq < FP_MUL(pocket_r, pocket_r)) {
|
||||
if (ball_idx == 0) {
|
||||
/* Cue ball potted = foul */
|
||||
foul = 1;
|
||||
b->x = INT_TO_FP(TABLE_X + TABLE_W / 4);
|
||||
b->y = INT_TO_FP(TABLE_Y + TABLE_H / 2);
|
||||
b->vx = 0;
|
||||
b->vy = 0;
|
||||
} else {
|
||||
b->active = 0;
|
||||
b->vx = 0;
|
||||
b->vy = 0;
|
||||
score++;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void check_ball_collisions(void) {
|
||||
for (int i = 0; i < NUM_BALLS; i++) {
|
||||
if (!balls[i].active) continue;
|
||||
for (int j = i + 1; j < NUM_BALLS; j++) {
|
||||
if (!balls[j].active) continue;
|
||||
|
||||
fixed_t dx = balls[j].x - balls[i].x;
|
||||
fixed_t dy = balls[j].y - balls[i].y;
|
||||
fixed_t dist_sq = FP_MUL(dx, dx) + FP_MUL(dy, dy);
|
||||
fixed_t min_dist = INT_TO_FP(BALL_RADIUS * 2);
|
||||
fixed_t min_dist_sq = FP_MUL(min_dist, min_dist);
|
||||
|
||||
if (dist_sq < min_dist_sq && dist_sq > 0) {
|
||||
/* Elastic collision */
|
||||
fixed_t dist = fp_sqrt(dist_sq);
|
||||
if (dist == 0) dist = 1;
|
||||
|
||||
/* Normal vector */
|
||||
fixed_t nx = FP_DIV(dx, dist);
|
||||
fixed_t ny = FP_DIV(dy, dist);
|
||||
|
||||
/* Relative velocity along normal */
|
||||
fixed_t dvx = balls[i].vx - balls[j].vx;
|
||||
fixed_t dvy = balls[i].vy - balls[j].vy;
|
||||
fixed_t dvn = FP_MUL(dvx, nx) + FP_MUL(dvy, ny);
|
||||
|
||||
/* Only resolve if balls are approaching */
|
||||
if (dvn <= 0) continue;
|
||||
|
||||
/* Update velocities (equal mass elastic collision) */
|
||||
balls[i].vx -= FP_MUL(dvn, nx);
|
||||
balls[i].vy -= FP_MUL(dvn, ny);
|
||||
balls[j].vx += FP_MUL(dvn, nx);
|
||||
balls[j].vy += FP_MUL(dvn, ny);
|
||||
|
||||
/* Separate balls */
|
||||
fixed_t overlap = min_dist - dist;
|
||||
if (overlap > 0) {
|
||||
fixed_t sep = overlap / 2 + FP_ONE / 4;
|
||||
balls[i].x -= FP_MUL(sep, nx);
|
||||
balls[i].y -= FP_MUL(sep, ny);
|
||||
balls[j].x += FP_MUL(sep, nx);
|
||||
balls[j].y += FP_MUL(sep, ny);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void update_physics(void) {
|
||||
balls_moving = 0;
|
||||
|
||||
for (int i = 0; i < NUM_BALLS; i++) {
|
||||
if (!balls[i].active) continue;
|
||||
|
||||
/* Apply velocity */
|
||||
balls[i].x += balls[i].vx;
|
||||
balls[i].y += balls[i].vy;
|
||||
|
||||
/* Apply friction */
|
||||
balls[i].vx = FP_MUL(balls[i].vx, FRICTION);
|
||||
balls[i].vy = FP_MUL(balls[i].vy, FRICTION);
|
||||
|
||||
/* Check if ball is still moving */
|
||||
fixed_t speed_sq = FP_MUL(balls[i].vx, balls[i].vx) +
|
||||
FP_MUL(balls[i].vy, balls[i].vy);
|
||||
if (speed_sq < FP_MUL(MIN_SPEED, MIN_SPEED)) {
|
||||
balls[i].vx = 0;
|
||||
balls[i].vy = 0;
|
||||
} else {
|
||||
balls_moving = 1;
|
||||
}
|
||||
|
||||
/* Wall collisions */
|
||||
check_wall_collisions(&balls[i]);
|
||||
|
||||
/* Pocket check */
|
||||
check_pocket(&balls[i], i);
|
||||
}
|
||||
|
||||
/* Ball-ball collisions */
|
||||
check_ball_collisions();
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Drawing
|
||||
* ================================================================ */
|
||||
|
||||
static void draw_table(void) {
|
||||
/* Background */
|
||||
gfx_clear(COL_BG);
|
||||
|
||||
/* Rail (border) */
|
||||
gfx_rect_t rail = {TABLE_X - RAIL_W, TABLE_Y - RAIL_W,
|
||||
TABLE_W + RAIL_W * 2, TABLE_H + RAIL_W * 2, COL_RAIL};
|
||||
gfx_fill_rect(&rail);
|
||||
|
||||
/* Felt (playing surface) */
|
||||
gfx_rect_t felt = {TABLE_X, TABLE_Y, TABLE_W, TABLE_H, COL_FELT};
|
||||
gfx_fill_rect(&felt);
|
||||
|
||||
/* Pockets */
|
||||
for (int i = 0; i < NUM_POCKETS; i++) {
|
||||
gfx_circle_t pocket = {
|
||||
(uint32_t)FP_TO_INT(pockets[i].x),
|
||||
(uint32_t)FP_TO_INT(pockets[i].y),
|
||||
POCKET_RADIUS, COL_POCKET
|
||||
};
|
||||
gfx_circle(&pocket);
|
||||
}
|
||||
}
|
||||
|
||||
static void draw_balls(void) {
|
||||
for (int i = 0; i < NUM_BALLS; i++) {
|
||||
if (!balls[i].active) continue;
|
||||
gfx_circle_t c = {
|
||||
(uint32_t)FP_TO_INT(balls[i].x),
|
||||
(uint32_t)FP_TO_INT(balls[i].y),
|
||||
BALL_RADIUS,
|
||||
balls[i].color
|
||||
};
|
||||
gfx_circle(&c);
|
||||
}
|
||||
}
|
||||
|
||||
static void draw_aim(void) {
|
||||
if (balls_moving || !balls[0].active) return;
|
||||
|
||||
/* Draw aim line from cue ball */
|
||||
int cx = FP_TO_INT(balls[0].x);
|
||||
int cy = FP_TO_INT(balls[0].y);
|
||||
int len = 20 + shot_power * 5;
|
||||
|
||||
int ex = cx + FP_TO_INT(FP_MUL(INT_TO_FP(len), fp_cos(aim_angle)));
|
||||
int ey = cy + FP_TO_INT(FP_MUL(INT_TO_FP(len), fp_sin(aim_angle)));
|
||||
|
||||
gfx_line_t line = {(uint32_t)cx, (uint32_t)cy,
|
||||
(uint32_t)ex, (uint32_t)ey, COL_AIM};
|
||||
gfx_line(&line);
|
||||
}
|
||||
|
||||
static void draw_hud(void) {
|
||||
/* Score */
|
||||
char score_str[32] = "Score: ";
|
||||
int pos = 7;
|
||||
if (score == 0) {
|
||||
score_str[pos++] = '0';
|
||||
} else {
|
||||
char tmp[8];
|
||||
int ti = 0;
|
||||
int s = score;
|
||||
while (s > 0) { tmp[ti++] = '0' + (char)(s % 10); s /= 10; }
|
||||
while (ti > 0) score_str[pos++] = tmp[--ti];
|
||||
}
|
||||
score_str[pos] = '\0';
|
||||
|
||||
/* Use pixels directly for HUD text at top */
|
||||
/* Score on left side of top bar */
|
||||
int tx = 2;
|
||||
int ty = 2;
|
||||
for (int i = 0; score_str[i]; i++) {
|
||||
gfx_pixel((uint32_t)(tx + i * 6), (uint32_t)ty, COL_TEXT);
|
||||
/* Draw each character as small 4x5 digits — simplified */
|
||||
}
|
||||
|
||||
/* Power indicator bar */
|
||||
gfx_rect_t power_bg = {2, SCREEN_H - 12, 60, 8, COL_BG};
|
||||
gfx_fill_rect(&power_bg);
|
||||
gfx_rect_t power_bar = {2, SCREEN_H - 12, (uint32_t)(shot_power * 10), 8, COL_POWER};
|
||||
gfx_fill_rect(&power_bar);
|
||||
|
||||
/* Foul indicator */
|
||||
if (foul) {
|
||||
gfx_rect_t foul_bg = {SCREEN_W / 2 - 20, SCREEN_H - 12, 40, 8, GFX_RED};
|
||||
gfx_fill_rect(&foul_bg);
|
||||
}
|
||||
|
||||
/* Win message */
|
||||
if (score >= 7) {
|
||||
game_over = 1;
|
||||
gfx_rect_t win_bg = {SCREEN_W/2 - 30, SCREEN_H/2 - 10, 60, 20, GFX_BLUE};
|
||||
gfx_fill_rect(&win_bg);
|
||||
}
|
||||
}
|
||||
|
||||
static void draw_frame(void) {
|
||||
draw_table();
|
||||
draw_balls();
|
||||
draw_aim();
|
||||
draw_hud();
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Input handling
|
||||
* ================================================================ */
|
||||
|
||||
static void shoot(void) {
|
||||
if (balls_moving || !balls[0].active) return;
|
||||
|
||||
fixed_t power = INT_TO_FP(shot_power);
|
||||
balls[0].vx = FP_MUL(power, fp_cos(aim_angle));
|
||||
balls[0].vy = FP_MUL(power, fp_sin(aim_angle));
|
||||
foul = 0;
|
||||
}
|
||||
|
||||
static int handle_input(void) {
|
||||
char c;
|
||||
int32_t n = read(0, &c, 1);
|
||||
if (n <= 0) return 0;
|
||||
|
||||
switch (c) {
|
||||
case 'q':
|
||||
case 'Q':
|
||||
case 27: /* Escape */
|
||||
return 1; /* Quit */
|
||||
|
||||
case 'a':
|
||||
case 'A':
|
||||
aim_angle = (aim_angle - 4) & 255;
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
case 'D':
|
||||
aim_angle = (aim_angle + 4) & 255;
|
||||
break;
|
||||
|
||||
case 'w':
|
||||
case 'W':
|
||||
if (shot_power < 6) shot_power++;
|
||||
break;
|
||||
|
||||
case 's':
|
||||
case 'S':
|
||||
if (shot_power > 1) shot_power--;
|
||||
break;
|
||||
|
||||
case ' ':
|
||||
case '\n':
|
||||
case '\r':
|
||||
shoot();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Main
|
||||
* ================================================================ */
|
||||
|
||||
int main(void) {
|
||||
/* Enter graphics mode */
|
||||
gfx_enter();
|
||||
|
||||
/* Initialize game */
|
||||
init_pockets();
|
||||
init_balls();
|
||||
|
||||
/* Main game loop */
|
||||
while (!game_over) {
|
||||
/* Handle input */
|
||||
if (handle_input()) break;
|
||||
|
||||
/* Update physics */
|
||||
if (balls_moving) {
|
||||
update_physics();
|
||||
}
|
||||
|
||||
/* Draw everything */
|
||||
draw_frame();
|
||||
|
||||
/* Frame delay (cooperative yield) */
|
||||
for (int i = 0; i < 2; i++) yield();
|
||||
}
|
||||
|
||||
/* Wait a moment on game over */
|
||||
if (game_over) {
|
||||
for (int i = 0; i < 200; i++) yield();
|
||||
}
|
||||
|
||||
/* Return to text mode */
|
||||
gfx_leave();
|
||||
puts("Game over! Final score: ");
|
||||
|
||||
/* Print score */
|
||||
char tmp[8];
|
||||
int ti = 0;
|
||||
int s = score;
|
||||
if (s == 0) { putchar('0'); }
|
||||
else {
|
||||
while (s > 0) { tmp[ti++] = '0' + (char)(s % 10); s /= 10; }
|
||||
while (ti > 0) putchar(tmp[--ti]);
|
||||
}
|
||||
puts("/7\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
365
apps/sh/sh.c
Normal file
365
apps/sh/sh.c
Normal file
@@ -0,0 +1,365 @@
|
||||
/**
|
||||
* @file sh.c
|
||||
* @brief ClaudeOS shell.
|
||||
*
|
||||
* A simple interactive shell that reads commands from stdin,
|
||||
* supports built-in commands (cd, exit, help, env), and
|
||||
* executes external programs via fork+exec.
|
||||
*/
|
||||
|
||||
#include "syscalls.h"
|
||||
|
||||
/** Maximum command line length. */
|
||||
#define CMD_MAX 256
|
||||
|
||||
/** Read a line from stdin with echo and basic line editing.
|
||||
* Returns length of the line (excluding newline). */
|
||||
static int readline(char *buf, int maxlen) {
|
||||
int pos = 0;
|
||||
while (pos < maxlen - 1) {
|
||||
char c;
|
||||
int n = read(0, &c, 1);
|
||||
if (n <= 0) {
|
||||
/* No data yet: yield CPU and retry */
|
||||
yield();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (c == '\n' || c == '\r') {
|
||||
putchar('\n');
|
||||
break;
|
||||
} else if (c == '\b' || c == 127) {
|
||||
/* Backspace */
|
||||
if (pos > 0) {
|
||||
pos--;
|
||||
puts("\b \b"); /* Move back, overwrite with space, move back */
|
||||
}
|
||||
} else if (c >= 32) {
|
||||
/* Printable character */
|
||||
buf[pos++] = c;
|
||||
putchar(c);
|
||||
}
|
||||
}
|
||||
buf[pos] = '\0';
|
||||
return pos;
|
||||
}
|
||||
|
||||
/** Skip leading whitespace, return pointer to first non-space. */
|
||||
static char *skip_spaces(char *s) {
|
||||
while (*s == ' ' || *s == '\t') s++;
|
||||
return s;
|
||||
}
|
||||
|
||||
/** Find the next space or end of string. */
|
||||
static char *find_space(char *s) {
|
||||
while (*s && *s != ' ' && *s != '\t') s++;
|
||||
return s;
|
||||
}
|
||||
|
||||
/** String length. */
|
||||
static int slen(const char *s) {
|
||||
int n = 0;
|
||||
while (s[n]) n++;
|
||||
return n;
|
||||
}
|
||||
|
||||
/** Resolve an absolute path from cwd + a possibly relative path.
|
||||
* Handles '..', '.', and trailing slashes. Result is always absolute. */
|
||||
static void resolve_path(const char *cwd, const char *arg, char *out, int out_sz) {
|
||||
char tmp[256];
|
||||
int ti = 0;
|
||||
|
||||
/* If arg is absolute, start from it; otherwise prepend cwd */
|
||||
if (arg[0] == '/') {
|
||||
/* copy arg into tmp */
|
||||
for (int i = 0; arg[i] && ti < 255; i++)
|
||||
tmp[ti++] = arg[i];
|
||||
} else {
|
||||
/* copy cwd */
|
||||
for (int i = 0; cwd[i] && ti < 255; i++)
|
||||
tmp[ti++] = cwd[i];
|
||||
/* ensure separator */
|
||||
if (ti > 0 && tmp[ti - 1] != '/' && ti < 255)
|
||||
tmp[ti++] = '/';
|
||||
/* append arg */
|
||||
for (int i = 0; arg[i] && ti < 255; i++)
|
||||
tmp[ti++] = arg[i];
|
||||
}
|
||||
tmp[ti] = '\0';
|
||||
|
||||
/* Now canonicalize: split on '/' and process each component */
|
||||
/* Stack of component start offsets */
|
||||
int comp_starts[64];
|
||||
int comp_lens[64];
|
||||
int depth = 0;
|
||||
|
||||
int i = 0;
|
||||
while (tmp[i]) {
|
||||
/* skip slashes */
|
||||
while (tmp[i] == '/') i++;
|
||||
if (!tmp[i]) break;
|
||||
|
||||
/* find end of component */
|
||||
int start = i;
|
||||
while (tmp[i] && tmp[i] != '/') i++;
|
||||
int len = i - start;
|
||||
|
||||
if (len == 1 && tmp[start] == '.') {
|
||||
/* current dir: skip */
|
||||
continue;
|
||||
} else if (len == 2 && tmp[start] == '.' && tmp[start + 1] == '.') {
|
||||
/* parent dir: pop */
|
||||
if (depth > 0) depth--;
|
||||
} else {
|
||||
/* normal component: push */
|
||||
if (depth < 64) {
|
||||
comp_starts[depth] = start;
|
||||
comp_lens[depth] = len;
|
||||
depth++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Build output */
|
||||
int oi = 0;
|
||||
if (depth == 0) {
|
||||
if (oi < out_sz - 1) out[oi++] = '/';
|
||||
} else {
|
||||
for (int d = 0; d < depth && oi < out_sz - 2; d++) {
|
||||
out[oi++] = '/';
|
||||
for (int j = 0; j < comp_lens[d] && oi < out_sz - 1; j++)
|
||||
out[oi++] = tmp[comp_starts[d] + j];
|
||||
}
|
||||
}
|
||||
out[oi] = '\0';
|
||||
}
|
||||
|
||||
/** Built-in: cd <path> */
|
||||
static void builtin_cd(char *arg) {
|
||||
if (!arg || !*arg) {
|
||||
arg = "/";
|
||||
}
|
||||
|
||||
/* Get current working directory */
|
||||
char cwd[128];
|
||||
if (getenv("CWD", cwd, sizeof(cwd)) < 0) {
|
||||
strcpy(cwd, "/");
|
||||
}
|
||||
|
||||
/* Resolve the path (handles relative, .., etc.) */
|
||||
char resolved[256];
|
||||
resolve_path(cwd, arg, resolved, sizeof(resolved));
|
||||
|
||||
/* Validate: check if the directory exists by trying to readdir it.
|
||||
* Root "/" always exists. For others, readdir index 0 must not fail
|
||||
* with -1 unless the dir is simply empty, so we also accept stat-like
|
||||
* checks. A simple heuristic: if readdir returns -1 for index 0 AND
|
||||
* the path is not "/", we check if the *parent* can list it. */
|
||||
if (resolved[0] == '/' && resolved[1] != '\0') {
|
||||
/* Try listing the first entry; if the path is a valid directory,
|
||||
* readdir(path, 0, ...) returns >= 0 even for an empty dir
|
||||
* (it returns -1 meaning "end"), BUT if the path doesn't exist
|
||||
* at all, the VFS resolve_path will fail and readdir returns -1.
|
||||
*
|
||||
* Better approach: try to readdir the parent and look for this
|
||||
* entry among its children. */
|
||||
char name[128];
|
||||
int32_t type = readdir(resolved, 0, name);
|
||||
/* type >= 0 means there's at least one entry => directory exists.
|
||||
* type == -1 could mean empty dir or non-existent.
|
||||
* For empty dirs: the directory itself was resolved successfully.
|
||||
* For non-existent: the VFS resolution failed.
|
||||
*
|
||||
* Our VFS readdir calls resolve_path internally. If the path
|
||||
* doesn't exist, it returns -1. If it exists but is empty,
|
||||
* it also returns -1. We need to distinguish these.
|
||||
* Use a stat-like approach: try to open the path. */
|
||||
|
||||
/* Actually the simplest check: try readdir on the *parent* and
|
||||
* see if our target component exists as a directory entry. */
|
||||
char parent[256];
|
||||
char target[128];
|
||||
|
||||
/* Split resolved into parent + target */
|
||||
int rlen = slen(resolved);
|
||||
int last_slash = 0;
|
||||
for (int i = 0; i < rlen; i++) {
|
||||
if (resolved[i] == '/') last_slash = i;
|
||||
}
|
||||
|
||||
if (last_slash == 0) {
|
||||
/* Parent is root */
|
||||
parent[0] = '/';
|
||||
parent[1] = '\0';
|
||||
strcpy(target, resolved + 1);
|
||||
} else {
|
||||
memcpy(parent, resolved, (uint32_t)last_slash);
|
||||
parent[last_slash] = '\0';
|
||||
strcpy(target, resolved + last_slash + 1);
|
||||
}
|
||||
|
||||
/* Search parent directory for target */
|
||||
int found = 0;
|
||||
uint32_t idx = 0;
|
||||
int32_t etype;
|
||||
while ((etype = readdir(parent, idx, name)) >= 0) {
|
||||
if (strcmp(name, target) == 0) {
|
||||
if (etype == 2) {
|
||||
found = 1; /* It's a directory */
|
||||
} else {
|
||||
puts("cd: ");
|
||||
puts(resolved);
|
||||
puts(": not a directory\n");
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
idx++;
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
puts("cd: ");
|
||||
puts(resolved);
|
||||
puts(": no such directory\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* Update CWD environment variable */
|
||||
setenv("CWD", resolved);
|
||||
}
|
||||
|
||||
/** Built-in: env - print all known env vars. */
|
||||
static void builtin_env(void) {
|
||||
char buf[128];
|
||||
if (getenv("CWD", buf, sizeof(buf)) >= 0) {
|
||||
puts("CWD=");
|
||||
puts(buf);
|
||||
putchar('\n');
|
||||
}
|
||||
if (getenv("PATH", buf, sizeof(buf)) >= 0) {
|
||||
puts("PATH=");
|
||||
puts(buf);
|
||||
putchar('\n');
|
||||
}
|
||||
if (getenv("TEST", buf, sizeof(buf)) >= 0) {
|
||||
puts("TEST=");
|
||||
puts(buf);
|
||||
putchar('\n');
|
||||
}
|
||||
}
|
||||
|
||||
/** Built-in: help */
|
||||
static void builtin_help(void) {
|
||||
puts("ClaudeOS Shell\n");
|
||||
puts("Built-in commands:\n");
|
||||
puts(" cd <path> - change working directory\n");
|
||||
puts(" env - show environment variables\n");
|
||||
puts(" help - show this message\n");
|
||||
puts(" exit - exit the shell\n");
|
||||
puts("External commands are loaded from initrd.\n");
|
||||
}
|
||||
|
||||
/** Check if a string looks like a path (contains / or starts with .) */
|
||||
static int looks_like_path(const char *s) {
|
||||
if (!s || !*s) return 0;
|
||||
if (s[0] == '.' || s[0] == '/') return 1;
|
||||
for (int i = 0; s[i]; i++) {
|
||||
if (s[i] == '/') return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Execute an external command via fork+exec. */
|
||||
static void run_command(const char *cmd, const char *arg) {
|
||||
int32_t pid = fork();
|
||||
if (pid < 0) {
|
||||
puts("sh: fork failed\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (pid == 0) {
|
||||
/* Child: set ARG1 if there's an argument */
|
||||
if (arg && *arg) {
|
||||
/* If the argument looks like a path and isn't absolute,
|
||||
* resolve it relative to CWD so apps get absolute paths */
|
||||
if (looks_like_path(arg) && arg[0] != '/') {
|
||||
char cwd[128];
|
||||
if (getenv("CWD", cwd, sizeof(cwd)) < 0) {
|
||||
strcpy(cwd, "/");
|
||||
}
|
||||
char resolved_arg[256];
|
||||
resolve_path(cwd, arg, resolved_arg, sizeof(resolved_arg));
|
||||
setenv("ARG1", resolved_arg);
|
||||
} else {
|
||||
setenv("ARG1", arg);
|
||||
}
|
||||
} else {
|
||||
setenv("ARG1", "");
|
||||
}
|
||||
|
||||
/* exec the command */
|
||||
int32_t ret = exec(cmd);
|
||||
if (ret < 0) {
|
||||
puts("sh: ");
|
||||
puts(cmd);
|
||||
puts(": not found\n");
|
||||
exit(127);
|
||||
}
|
||||
/* exec doesn't return on success */
|
||||
exit(1);
|
||||
}
|
||||
|
||||
/* Parent: wait for child */
|
||||
waitpid(pid);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
puts("ClaudeOS Shell v0.1\n");
|
||||
puts("Type 'help' for available commands.\n\n");
|
||||
|
||||
char cmd[CMD_MAX];
|
||||
|
||||
for (;;) {
|
||||
/* Print prompt with CWD */
|
||||
char cwd[128];
|
||||
if (getenv("CWD", cwd, sizeof(cwd)) < 0) {
|
||||
strcpy(cwd, "/");
|
||||
}
|
||||
puts(cwd);
|
||||
puts("$ ");
|
||||
|
||||
/* Read command */
|
||||
int len = readline(cmd, CMD_MAX);
|
||||
if (len == 0) continue;
|
||||
|
||||
/* Parse command and arguments */
|
||||
char *line = skip_spaces(cmd);
|
||||
if (*line == '\0') continue;
|
||||
|
||||
char *arg_start = find_space(line);
|
||||
char *arg = (char *)0;
|
||||
if (*arg_start) {
|
||||
*arg_start = '\0';
|
||||
arg = skip_spaces(arg_start + 1);
|
||||
if (*arg == '\0') arg = (char *)0;
|
||||
}
|
||||
|
||||
/* Check built-in commands */
|
||||
if (strcmp(line, "exit") == 0) {
|
||||
puts("Goodbye!\n");
|
||||
exit(0);
|
||||
} else if (strcmp(line, "cd") == 0) {
|
||||
builtin_cd(arg);
|
||||
} else if (strcmp(line, "env") == 0) {
|
||||
builtin_env();
|
||||
} else if (strcmp(line, "help") == 0) {
|
||||
builtin_help();
|
||||
} else {
|
||||
/* External command */
|
||||
run_command(line, arg);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
25
apps/user.ld
Normal file
25
apps/user.ld
Normal file
@@ -0,0 +1,25 @@
|
||||
/* Linker script for ClaudeOS user-mode flat binary.
|
||||
* Applications are loaded at 0x08048000 (USER_CODE_START).
|
||||
* This produces a flat binary (no ELF headers). */
|
||||
|
||||
ENTRY(_start)
|
||||
|
||||
SECTIONS {
|
||||
. = 0x08048000;
|
||||
|
||||
.text : {
|
||||
*(.text)
|
||||
}
|
||||
|
||||
.rodata : {
|
||||
*(.rodata*)
|
||||
}
|
||||
|
||||
.data : {
|
||||
*(.data)
|
||||
}
|
||||
|
||||
.bss : {
|
||||
*(.bss)
|
||||
}
|
||||
}
|
||||
236
apps/wget/wget.c
Normal file
236
apps/wget/wget.c
Normal file
@@ -0,0 +1,236 @@
|
||||
/**
|
||||
* @file wget.c
|
||||
* @brief Simple HTTP client for ClaudeOS.
|
||||
*
|
||||
* Downloads a resource from an HTTP server using a TCP connection.
|
||||
* Only supports HTTP/1.0 GET with IP addresses (no DNS).
|
||||
*
|
||||
* Usage:
|
||||
* wget <ip>[:<port>]/<path>
|
||||
* wget <ip> - fetches /
|
||||
*
|
||||
* Examples:
|
||||
* wget 10.0.2.2/index.html
|
||||
* wget 10.0.2.2:8080/api/data
|
||||
* wget 192.168.1.1
|
||||
*/
|
||||
|
||||
#include "syscalls.h"
|
||||
|
||||
typedef unsigned char uint8_t;
|
||||
|
||||
/**
|
||||
* Parse a decimal number from a string.
|
||||
* Advances *s past the digits.
|
||||
*/
|
||||
static uint32_t parse_uint(const char **s) {
|
||||
uint32_t val = 0;
|
||||
while (**s >= '0' && **s <= '9') {
|
||||
val = val * 10 + (uint32_t)(**s - '0');
|
||||
(*s)++;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse an IPv4 dotted-decimal address into a 32-bit host-order integer.
|
||||
* Returns 0 on failure.
|
||||
*/
|
||||
static uint32_t parse_ip(const char **s) {
|
||||
uint32_t ip = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
uint32_t octet = parse_uint(s);
|
||||
if (octet > 255) return 0;
|
||||
ip = (ip << 8) | octet;
|
||||
if (i < 3) {
|
||||
if (**s != '.') return 0;
|
||||
(*s)++;
|
||||
}
|
||||
}
|
||||
return ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print a decimal number.
|
||||
*/
|
||||
static void print_dec(uint32_t val) {
|
||||
char buf[12];
|
||||
int i = 0;
|
||||
if (val == 0) { putchar('0'); return; }
|
||||
while (val > 0) {
|
||||
buf[i++] = '0' + (char)(val % 10);
|
||||
val /= 10;
|
||||
}
|
||||
while (i > 0) putchar(buf[--i]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the HTTP GET request.
|
||||
* Returns length of the request string.
|
||||
*/
|
||||
static uint32_t build_request(char *buf, uint32_t bufsize,
|
||||
const char *host, const char *path) {
|
||||
uint32_t pos = 0;
|
||||
|
||||
/* "GET <path> HTTP/1.0\r\nHost: <host>\r\nConnection: close\r\n\r\n" */
|
||||
const char *parts[] = {
|
||||
"GET ", path, " HTTP/1.0\r\nHost: ", host,
|
||||
"\r\nConnection: close\r\n\r\n", (const char *)0
|
||||
};
|
||||
|
||||
for (int i = 0; parts[i]; i++) {
|
||||
const char *s = parts[i];
|
||||
while (*s && pos < bufsize - 1) {
|
||||
buf[pos++] = *s++;
|
||||
}
|
||||
}
|
||||
buf[pos] = '\0';
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format an IP address as a dotted-decimal string.
|
||||
*/
|
||||
static void ip_to_str(uint32_t ip, char *buf) {
|
||||
int pos = 0;
|
||||
for (int i = 3; i >= 0; i--) {
|
||||
uint32_t octet = (ip >> (i * 8)) & 0xFF;
|
||||
char tmp[4];
|
||||
int ti = 0;
|
||||
if (octet == 0) { tmp[ti++] = '0'; }
|
||||
else {
|
||||
while (octet > 0) { tmp[ti++] = '0' + (char)(octet % 10); octet /= 10; }
|
||||
}
|
||||
while (ti > 0) buf[pos++] = tmp[--ti];
|
||||
if (i > 0) buf[pos++] = '.';
|
||||
}
|
||||
buf[pos] = '\0';
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
char url[256];
|
||||
|
||||
/* Get URL from ARG1 */
|
||||
if (getenv("ARG1", url, sizeof(url)) < 0 || url[0] == '\0') {
|
||||
puts("Usage: wget <ip>[:<port>]/<path>\n");
|
||||
puts(" e.g. wget 10.0.2.2/index.html\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Parse: skip optional "http://" prefix */
|
||||
const char *p = url;
|
||||
if (strncmp(p, "http://", 7) == 0) {
|
||||
p += 7;
|
||||
}
|
||||
|
||||
/* Parse IP address */
|
||||
uint32_t ip = parse_ip(&p);
|
||||
if (ip == 0) {
|
||||
puts("wget: invalid IP address\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Parse optional port */
|
||||
uint32_t port = 80;
|
||||
if (*p == ':') {
|
||||
p++;
|
||||
port = parse_uint(&p);
|
||||
if (port == 0 || port > 65535) {
|
||||
puts("wget: invalid port\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Parse path (default to /) */
|
||||
const char *path = "/";
|
||||
if (*p == '/') {
|
||||
path = p;
|
||||
} else if (*p != '\0') {
|
||||
puts("wget: invalid URL format\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Build host string for Host header */
|
||||
char host_str[64];
|
||||
ip_to_str(ip, host_str);
|
||||
|
||||
/* Print what we're doing */
|
||||
puts("Connecting to ");
|
||||
puts(host_str);
|
||||
putchar(':');
|
||||
print_dec(port);
|
||||
puts(path);
|
||||
puts("...\n");
|
||||
|
||||
/* Create TCP socket */
|
||||
int32_t sockfd = socket(SOCK_TCP);
|
||||
if (sockfd < 0) {
|
||||
puts("wget: failed to create socket\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Connect */
|
||||
if (connect(sockfd, ip, port) < 0) {
|
||||
puts("wget: connect failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Wait for connection to establish (poll with yield) */
|
||||
int timeout = 500; /* ~5 seconds at ~100 yields/sec */
|
||||
while (timeout > 0) {
|
||||
int32_t state = sockstate(sockfd);
|
||||
if (state == TCP_STATE_ESTABLISHED) break;
|
||||
if (state == TCP_STATE_CLOSED) {
|
||||
puts("wget: connection refused\n");
|
||||
return 1;
|
||||
}
|
||||
yield();
|
||||
timeout--;
|
||||
}
|
||||
if (timeout <= 0) {
|
||||
puts("wget: connection timed out\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
puts("Connected.\n");
|
||||
|
||||
/* Build and send HTTP request */
|
||||
char req[512];
|
||||
uint32_t req_len = build_request(req, sizeof(req), host_str, path);
|
||||
|
||||
int32_t sent = net_send(sockfd, req, req_len);
|
||||
if (sent < 0) {
|
||||
puts("wget: send failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Receive response */
|
||||
puts("--- Response ---\n");
|
||||
char buf[512];
|
||||
int done = 0;
|
||||
int recv_timeout = 1000;
|
||||
|
||||
while (!done && recv_timeout > 0) {
|
||||
int32_t n = net_recv(sockfd, buf, sizeof(buf) - 1);
|
||||
if (n > 0) {
|
||||
buf[n] = '\0';
|
||||
write(1, buf, (uint32_t)n);
|
||||
recv_timeout = 200; /* Reset timeout on data */
|
||||
} else if (n < 0) {
|
||||
/* Connection closed or error */
|
||||
done = 1;
|
||||
} else {
|
||||
/* No data yet */
|
||||
yield();
|
||||
recv_timeout--;
|
||||
}
|
||||
}
|
||||
|
||||
puts("\n--- End ---\n");
|
||||
|
||||
/* Close socket - we use close() which goes through SYS_CLOSE.
|
||||
* For sockets, we should ideally have a socket-specific close,
|
||||
* but for simplicity, the socket will be cleaned up on process exit. */
|
||||
|
||||
return 0;
|
||||
}
|
||||
73
build.log
Normal file
73
build.log
Normal file
@@ -0,0 +1,73 @@
|
||||
[ 2%] Building user-mode applications
|
||||
Building app: arp
|
||||
Built: /workspaces/claude-os/build/apps_bin/arp (214 bytes)
|
||||
Building app: cat
|
||||
Built: /workspaces/claude-os/build/apps_bin/cat (310 bytes)
|
||||
Building app: dhcp
|
||||
Built: /workspaces/claude-os/build/apps_bin/dhcp (219 bytes)
|
||||
Building app: diskpart
|
||||
/usr/bin/ld: warning: /workspaces/claude-os/build/apps_bin/diskpart.elf has a LOAD segment with RWX permissions
|
||||
Built: /workspaces/claude-os/build/apps_bin/diskpart (8406 bytes)
|
||||
Building app: env-test
|
||||
/usr/bin/ld: warning: /workspaces/claude-os/build/apps_bin/env-test.elf has a LOAD segment with RWX permissions
|
||||
Built: /workspaces/claude-os/build/apps_bin/env-test (389 bytes)
|
||||
Building app: fork-test
|
||||
Built: /workspaces/claude-os/build/apps_bin/fork-test (132 bytes)
|
||||
Building app: ftp
|
||||
Built: /workspaces/claude-os/build/apps_bin/ftp (3406 bytes)
|
||||
Building app: hello-world
|
||||
Built: /workspaces/claude-os/build/apps_bin/hello-world (49 bytes)
|
||||
Building app: ip
|
||||
Built: /workspaces/claude-os/build/apps_bin/ip (3695 bytes)
|
||||
Building app: ls
|
||||
Built: /workspaces/claude-os/build/apps_bin/ls (250 bytes)
|
||||
Building app: minigolf
|
||||
/workspaces/claude-os/apps/minigolf/minigolf.c:29:17: warning: unused function 'isqrt' [-Wunused-function]
|
||||
29 | static uint32_t isqrt(uint32_t n) {
|
||||
| ^~~~~
|
||||
1 warning generated.
|
||||
/usr/bin/ld: warning: /workspaces/claude-os/build/apps_bin/minigolf.elf has a LOAD segment with RWX permissions
|
||||
Built: /workspaces/claude-os/build/apps_bin/minigolf (3456 bytes)
|
||||
Building app: mkfs.fat32
|
||||
/workspaces/claude-os/apps/mkfs.fat32/mkfs.fat32.c:56:13: warning: unused function 'print_hex' [-Wunused-function]
|
||||
56 | static void print_hex(uint32_t val) {
|
||||
| ^~~~~~~~~
|
||||
1 warning generated.
|
||||
/usr/bin/ld: warning: /workspaces/claude-os/build/apps_bin/mkfs.fat32.elf has a LOAD segment with RWX permissions
|
||||
Built: /workspaces/claude-os/build/apps_bin/mkfs.fat32 (5121 bytes)
|
||||
Building app: mount
|
||||
Built: /workspaces/claude-os/build/apps_bin/mount (992 bytes)
|
||||
Building app: pool
|
||||
/usr/bin/ld: warning: /workspaces/claude-os/build/apps_bin/pool.elf has a LOAD segment with RWX permissions
|
||||
Built: /workspaces/claude-os/build/apps_bin/pool (2936 bytes)
|
||||
Building app: sh
|
||||
/workspaces/claude-os/apps/sh/sh.c:167:17: warning: unused variable 'type' [-Wunused-variable]
|
||||
167 | int32_t type = readdir(resolved, 0, name);
|
||||
| ^~~~
|
||||
1 warning generated.
|
||||
Built: /workspaces/claude-os/build/apps_bin/sh (3428 bytes)
|
||||
Building app: wget
|
||||
Built: /workspaces/claude-os/build/apps_bin/wget (2193 bytes)
|
||||
[ 2%] Built target apps
|
||||
[ 4%] Generating CPIO initial ramdisk
|
||||
Generated initrd: 37232 bytes
|
||||
[ 4%] Built target initrd
|
||||
[ 97%] Built target kernel
|
||||
[100%] Generating bootable ISO image
|
||||
xorriso 1.5.6 : RockRidge filesystem manipulator, libburnia project.
|
||||
|
||||
Drive current: -outdev 'stdio:/workspaces/claude-os/release/claude-os.iso'
|
||||
Media current: stdio file, overwriteable
|
||||
Media status : is blank
|
||||
Media summary: 0 sessions, 0 data blocks, 0 data, 126g free
|
||||
Added to ISO image: directory '/'='/tmp/grub.NLidpF'
|
||||
xorriso : UPDATE : 581 files added in 1 seconds
|
||||
Added to ISO image: directory '/'='/workspaces/claude-os/build/isodir'
|
||||
xorriso : UPDATE : 586 files added in 1 seconds
|
||||
xorriso : NOTE : Copying to System Area: 512 bytes from file '/usr/lib/grub/i386-pc/boot_hybrid.img'
|
||||
xorriso : UPDATE : 64.38% done
|
||||
ISO image produced: 6058 sectors
|
||||
Written to medium : 6058 sectors at LBA 0
|
||||
Writing to 'stdio:/workspaces/claude-os/release/claude-os.iso' completed successfully.
|
||||
|
||||
[100%] Built target iso
|
||||
81
docs/cpio.md
Normal file
81
docs/cpio.md
Normal file
@@ -0,0 +1,81 @@
|
||||
# CPIO Initial Ramdisk
|
||||
|
||||
## Overview
|
||||
|
||||
The initial ramdisk (initrd) provides files to the kernel at boot time before
|
||||
any filesystem drivers are available. It is a CPIO archive in SVR4/newc format,
|
||||
loaded by GRUB as a Multiboot2 module.
|
||||
|
||||
## Build Process
|
||||
|
||||
During the build, the script `scripts/gen_initrd.sh` packs all files from the
|
||||
`apps/` directory into a CPIO archive:
|
||||
|
||||
```
|
||||
apps/
|
||||
├── README (placeholder)
|
||||
├── hello-world (future: flat binary)
|
||||
└── sh (future: shell binary)
|
||||
```
|
||||
|
||||
The archive is placed at `build/isodir/boot/initrd.cpio` and included in the
|
||||
ISO image. GRUB loads it as a module via:
|
||||
|
||||
```
|
||||
module2 /boot/initrd.cpio
|
||||
```
|
||||
|
||||
## CPIO Format
|
||||
|
||||
The newc (SVR4) CPIO format uses 110-byte headers with hex ASCII fields:
|
||||
|
||||
```
|
||||
Offset Size Field
|
||||
0 6 Magic ("070701")
|
||||
6 8 Inode
|
||||
14 8 Mode
|
||||
22 8 UID
|
||||
30 8 GID
|
||||
38 8 Nlink
|
||||
46 8 Mtime
|
||||
54 8 Filesize
|
||||
62 8 Devmajor
|
||||
70 8 Devminor
|
||||
78 8 Rdevmajor
|
||||
86 8 Rdevminor
|
||||
94 8 Namesize
|
||||
102 8 Check
|
||||
```
|
||||
|
||||
After the header: filename (namesize bytes, padded to 4-byte boundary),
|
||||
then file data (filesize bytes, padded to 4-byte boundary). The archive
|
||||
ends with a `TRAILER!!!` entry.
|
||||
|
||||
## Kernel Interface
|
||||
|
||||
The kernel finds the initrd by scanning Multiboot2 boot information for
|
||||
`MULTIBOOT_TAG_TYPE_MODULE` (type 3). The module's physical memory range
|
||||
is identity-mapped, so it can be read directly.
|
||||
|
||||
```c
|
||||
#include "cpio.h"
|
||||
|
||||
// Find a file
|
||||
cpio_entry_t entry;
|
||||
if (cpio_find("hello-world", &entry) == 0) {
|
||||
// entry.data = pointer to file contents
|
||||
// entry.datasize = file size
|
||||
}
|
||||
|
||||
// Iterate all files
|
||||
uint32_t offset = 0;
|
||||
while (cpio_next(&offset, &entry) == 0) {
|
||||
// entry.name, entry.data, entry.datasize
|
||||
}
|
||||
```
|
||||
|
||||
## Files
|
||||
|
||||
- `scripts/gen_initrd.sh` — Build script to generate CPIO archive
|
||||
- `src/cpio.h` / `src/cpio.c` — CPIO parser (find, iterate, count)
|
||||
- `CMakeLists.txt` — `initrd` target and `module2` in grub.cfg
|
||||
83
docs/fork.md
Normal file
83
docs/fork.md
Normal file
@@ -0,0 +1,83 @@
|
||||
# Fork System Call
|
||||
|
||||
## Overview
|
||||
|
||||
The `fork()` system call duplicates the calling process, creating a new child
|
||||
process with an independent copy of the parent's address space.
|
||||
|
||||
## System Call Interface
|
||||
|
||||
- **Number**: `SYS_FORK` (3)
|
||||
- **Arguments**: None
|
||||
- **Returns**: Child PID in the parent, 0 in the child, -1 on error
|
||||
|
||||
## Implementation
|
||||
|
||||
### Address Space Cloning
|
||||
|
||||
`paging_clone_directory_from(src_pd_phys)` performs a deep copy of a process's
|
||||
page directory:
|
||||
|
||||
1. **Kernel-space entries** (no `PAGE_USER` flag): shared directly between
|
||||
parent and child. Both processes see the same kernel mappings.
|
||||
|
||||
2. **User-space entries** (`PAGE_USER` flag set): fully deep-copied. For each
|
||||
user-space page directory entry:
|
||||
- A new page table is allocated
|
||||
- Each present user page has a new physical page allocated and the content
|
||||
copied byte-for-byte
|
||||
- This ensures parent and child have completely independent memory
|
||||
|
||||
### Register State
|
||||
|
||||
The child receives a copy of the parent's register state at the time of the
|
||||
`INT 0x80` syscall, with `EAX` set to 0. This means the child resumes execution
|
||||
at the instruction immediately following the `INT 0x80` that triggered fork.
|
||||
|
||||
### Process Exit and Waitpid
|
||||
|
||||
`process_exit()` was refactored to support multi-process scenarios:
|
||||
|
||||
- When a process exits, it scans for any process blocked on `waitpid()` for
|
||||
its PID and unblocks it, setting the waiter's saved `EAX` to the exit code.
|
||||
- If another process is ready, `process_switch_to_user()` is called to
|
||||
directly context-switch via an assembly stub that loads the full register
|
||||
set and performs `iret`.
|
||||
- If no processes remain, the system halts.
|
||||
|
||||
`sys_waitpid()` supports blocking:
|
||||
|
||||
- If the child is already a zombie, it reaps immediately
|
||||
- Otherwise, the caller is marked `PROCESS_BLOCKED` and the scheduler is
|
||||
invoked to switch to another process
|
||||
- When the child exits, the parent is unblocked with the exit code
|
||||
|
||||
### Assembly Support
|
||||
|
||||
`process_switch_to_user` in `interrupts.S` loads a full `registers_t` struct
|
||||
and performs `iret` to enter user mode. This is used when `process_exit()`
|
||||
needs to context-switch outside the normal ISR return path.
|
||||
|
||||
## Syscall Flow
|
||||
|
||||
```
|
||||
User: INT 0x80 (EAX=SYS_FORK)
|
||||
→ ISR stub pushes registers
|
||||
→ isr_handler → syscall_handler → sys_fork(regs)
|
||||
→ process_fork(regs)
|
||||
→ Clone page directory with deep user-page copy
|
||||
→ Copy current interrupt frame to child (EAX=0)
|
||||
→ Return child PID to parent (via EAX)
|
||||
→ ISR stub pops registers, iret
|
||||
→ Parent continues with EAX=child_pid
|
||||
→ [Timer interrupt] → scheduler picks child
|
||||
→ Child starts with EAX=0
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
The `fork-test` application validates fork by:
|
||||
1. Calling `SYS_FORK`
|
||||
2. Parent prints "Parent" and calls `SYS_WAITPID`
|
||||
3. Child prints "Child" and exits with code 7
|
||||
4. Parent reaps child, prints "Reaped", exits with code 0
|
||||
62
docs/interrupts.md
Normal file
62
docs/interrupts.md
Normal file
@@ -0,0 +1,62 @@
|
||||
# Interrupt Subsystem
|
||||
|
||||
## Overview
|
||||
|
||||
The interrupt subsystem handles both CPU exceptions (faults, traps, aborts) and hardware interrupts (IRQs) from external devices. It consists of three cooperating components:
|
||||
|
||||
1. **IDT (Interrupt Descriptor Table)** — Maps interrupt vectors 0–255 to handler entry points.
|
||||
2. **ISR (Interrupt Service Routines)** — Assembly stubs and a C dispatcher that routes interrupts.
|
||||
3. **PIC (Programmable Interrupt Controller)** — Manages the 8259A PIC chips that multiplex hardware IRQs onto CPU interrupt lines.
|
||||
|
||||
## Architecture
|
||||
|
||||
### Interrupt Vector Layout
|
||||
|
||||
| Vector Range | Purpose |
|
||||
|---|---|
|
||||
| 0–31 | CPU exceptions (Division by Zero, Page Fault, GPF, etc.) |
|
||||
| 32–47 | Hardware IRQs (remapped from default 0–15) |
|
||||
| 48–255 | Available for software interrupts / future use |
|
||||
|
||||
### Flow of an Interrupt
|
||||
|
||||
1. CPU or device raises an interrupt.
|
||||
2. CPU looks up the vector in the IDT and jumps to the assembly stub.
|
||||
3. The stub pushes a dummy error code (if the CPU didn't push one), the interrupt number, and all general-purpose registers onto the stack.
|
||||
4. The stub loads the kernel data segment (0x10) and calls `isr_handler()` in C.
|
||||
5. For hardware interrupts (vectors 32–47), `isr_handler` sends an End-of-Interrupt (EOI) to the PIC.
|
||||
6. For CPU exceptions (vectors 0–31), the handler prints the exception name and halts.
|
||||
7. On return, the stub restores all registers and executes `iret`.
|
||||
|
||||
### PIC Remapping
|
||||
|
||||
The 8259A PIC ships with IRQ 0–7 mapped to vectors 8–15, which collide with CPU exceptions. During initialization, we remap:
|
||||
|
||||
- **Master PIC** (IRQ 0–7) → vectors 32–39
|
||||
- **Slave PIC** (IRQ 8–15) → vectors 40–47
|
||||
|
||||
The PIC is initialized in cascade mode with ICW4 (8086 mode). Original IRQ masks are saved and restored after remapping.
|
||||
|
||||
## Key Files
|
||||
|
||||
- `src/idt.c` / `src/idt.h` — IDT setup and gate registration.
|
||||
- `src/interrupts.S` — Assembly stubs for ISRs 0–31 and IRQs 0–15.
|
||||
- `src/isr.c` / `src/isr.h` — C interrupt dispatcher and `registers_t` structure.
|
||||
- `src/pic.c` / `src/pic.h` — PIC initialization, EOI, mask/unmask.
|
||||
- `src/port_io.h` — Inline `inb`, `outb`, `io_wait` helpers.
|
||||
|
||||
## Register Save Frame
|
||||
|
||||
When an interrupt fires, the following is pushed onto the stack (from high to low address):
|
||||
|
||||
```
|
||||
ss, useresp (only on privilege change)
|
||||
eflags
|
||||
cs, eip (pushed by CPU)
|
||||
err_code (pushed by CPU or stub as 0)
|
||||
int_no (pushed by stub)
|
||||
eax..edi (pushed by pusha)
|
||||
ds (pushed by stub)
|
||||
```
|
||||
|
||||
This matches the `registers_t` struct in `isr.h`.
|
||||
61
docs/kmalloc.md
Normal file
61
docs/kmalloc.md
Normal file
@@ -0,0 +1,61 @@
|
||||
# Kernel Memory Allocator (kmalloc)
|
||||
|
||||
## Overview
|
||||
|
||||
The kernel memory allocator provides `kmalloc()` and `kfree()` for dynamic memory allocation within the kernel. It uses the paging subsystem to obtain physical memory on demand.
|
||||
|
||||
## Architecture
|
||||
|
||||
### Free-List Allocator
|
||||
|
||||
The allocator maintains a singly-linked free list of available memory blocks, ordered by address. Each block carries an inline header:
|
||||
|
||||
```c
|
||||
typedef struct block_header {
|
||||
uint32_t size; // Usable bytes (excludes header)
|
||||
uint32_t magic; // 0xCAFEBABE for integrity checking
|
||||
struct block_header *next; // Next free block (free list only)
|
||||
uint8_t is_free; // 1 = free, 0 = allocated
|
||||
} block_header_t;
|
||||
```
|
||||
|
||||
### Allocation Strategy
|
||||
|
||||
1. **First-fit search:** Walk the free list for the first block with `size >= requested`.
|
||||
2. **Block splitting:** If the found block is significantly larger than needed, it is split into the allocated portion and a new free block.
|
||||
3. **Page expansion:** If no suitable block exists, a new 4 KiB page is obtained from `paging_alloc_page()`.
|
||||
|
||||
All allocations are 8-byte aligned. Minimum block size is 16 bytes to limit fragmentation.
|
||||
|
||||
### Deallocation
|
||||
|
||||
1. The block header is located by subtracting `sizeof(block_header_t)` from the user pointer.
|
||||
2. The block's magic number is verified to detect corruption.
|
||||
3. The block is inserted back into the free list in address order.
|
||||
4. **Coalescing:** Adjacent free blocks are merged to reduce fragmentation.
|
||||
|
||||
### Integrity Checks
|
||||
|
||||
- A magic value (`0xCAFEBABE`) in each block header detects heap corruption.
|
||||
- Double-free is detected by checking the `is_free` flag.
|
||||
|
||||
## API
|
||||
|
||||
```c
|
||||
void init_kmalloc(void);
|
||||
void *kmalloc(size_t size);
|
||||
void kfree(void *ptr);
|
||||
void *kcalloc(size_t count, size_t size);
|
||||
```
|
||||
|
||||
## Key Files
|
||||
|
||||
- `src/kmalloc.c` / `src/kmalloc.h` — Allocator implementation.
|
||||
- `src/string.c` — Freestanding `memset`, `memcpy`, `strlen`, etc.
|
||||
- `src/paging.c` — Provides physical page backing.
|
||||
|
||||
## Limitations
|
||||
|
||||
- Maximum single allocation is ~4080 bytes (one page minus header).
|
||||
- No multi-page allocations for large objects.
|
||||
- Free virtual addresses are not reused after `paging_free_page()`.
|
||||
75
docs/paging.md
Normal file
75
docs/paging.md
Normal file
@@ -0,0 +1,75 @@
|
||||
# Paging Subsystem
|
||||
|
||||
## Overview
|
||||
|
||||
The paging subsystem manages virtual memory using the x86 two-level paging scheme (no PAE). It provides identity mapping for all physical memory and a kernel heap region for dynamic virtual page allocation.
|
||||
|
||||
## Architecture
|
||||
|
||||
### Page Table Structure
|
||||
|
||||
x86 32-bit paging uses two levels:
|
||||
|
||||
| Level | Entries | Each Entry Maps | Total Coverage |
|
||||
|---|---|---|---|
|
||||
| Page Directory | 1024 | 4 MiB (one page table) | 4 GiB |
|
||||
| Page Table | 1024 | 4 KiB (one page) | 4 MiB |
|
||||
|
||||
Each entry is a 32-bit value containing a 20-bit physical page frame number and 12 bits of flags.
|
||||
|
||||
### Identity Mapping
|
||||
|
||||
During initialization, all detected physical memory is identity-mapped (virtual address = physical address). This is done in two phases:
|
||||
|
||||
1. **Static mapping (first 16 MiB):** Four page tables are statically allocated in BSS. This avoids a chicken-and-egg problem since the PMM bitmap itself resides in this region.
|
||||
|
||||
2. **Dynamic mapping (above 16 MiB):** Additional page tables are allocated from the PMM *before* paging is enabled (so physical addresses are still directly accessible). These cover all remaining detected physical memory.
|
||||
|
||||
### Kernel Heap
|
||||
|
||||
The kernel heap region occupies virtual addresses `0xD0000000` through `0xF0000000` (768 MiB).
|
||||
|
||||
When `paging_alloc_page()` is called:
|
||||
1. A physical page is allocated from the PMM.
|
||||
2. A page table entry is created mapping the next free virtual address to the physical page.
|
||||
3. The virtual address is returned.
|
||||
|
||||
When `paging_free_page()` is called:
|
||||
1. The physical address is looked up via the page table entry.
|
||||
2. The virtual mapping is removed.
|
||||
3. The physical page is returned to the PMM.
|
||||
|
||||
### TLB Management
|
||||
|
||||
- Single-page invalidations use `invlpg`.
|
||||
- Full TLB flushes use CR3 reload.
|
||||
|
||||
## API
|
||||
|
||||
```c
|
||||
void init_paging(void);
|
||||
void paging_map_page(uint32_t vaddr, uint32_t paddr, uint32_t flags);
|
||||
void paging_unmap_page(uint32_t vaddr);
|
||||
void *paging_alloc_page(void);
|
||||
void paging_free_page(void *vaddr);
|
||||
uint32_t paging_get_physical(uint32_t vaddr);
|
||||
```
|
||||
|
||||
### Flags
|
||||
|
||||
| Flag | Value | Meaning |
|
||||
|---|---|---|
|
||||
| `PAGE_PRESENT` | 0x001 | Page is present in memory |
|
||||
| `PAGE_WRITE` | 0x002 | Page is writable |
|
||||
| `PAGE_USER` | 0x004 | Page is user-accessible (ring 3) |
|
||||
|
||||
## Key Files
|
||||
|
||||
- `src/paging.c` / `src/paging.h` — Implementation and API.
|
||||
- `src/pmm.c` / `src/pmm.h` — Physical page allocation backing.
|
||||
|
||||
## Design Decisions
|
||||
|
||||
- **No higher-half kernel yet:** The kernel runs at its physical load address (1 MiB) with identity mapping. Higher-half mapping (0xC0000000) can be added later without changing the paging API.
|
||||
- **Static + dynamic page tables:** The first 16 MiB uses BSS-allocated tables to bootstrap, while memory above 16 MiB uses PMM-allocated tables. This keeps BSS usage bounded at ~16 KiB regardless of total RAM.
|
||||
- **Sequential heap allocation:** The heap grows upward linearly. No free-list reuse of freed virtual addresses is implemented yet.
|
||||
63
docs/pmm.md
Normal file
63
docs/pmm.md
Normal file
@@ -0,0 +1,63 @@
|
||||
# Physical Memory Manager (PMM)
|
||||
|
||||
## Overview
|
||||
|
||||
The PMM manages physical page frames using a bitmap allocator. It tracks which 4 KiB pages of physical RAM are free or in use, and supports allocating pages from different memory zones.
|
||||
|
||||
## Architecture
|
||||
|
||||
### Bitmap Allocator
|
||||
|
||||
Each bit in a global bitmap corresponds to one 4 KiB physical page frame:
|
||||
- **Bit = 1** → page is in use (allocated or reserved)
|
||||
- **Bit = 0** → page is free
|
||||
|
||||
The bitmap covers the entire 32-bit physical address space (up to 4 GiB), requiring 128 KiB of storage in BSS.
|
||||
|
||||
### Memory Zones
|
||||
|
||||
The allocator supports zone-based allocation to satisfy constraints from different subsystems:
|
||||
|
||||
| Zone | Range | Purpose |
|
||||
|---|---|---|
|
||||
| `PMM_ZONE_DMA` | 0 – 16 MiB | ISA DMA-compatible memory |
|
||||
| `PMM_ZONE_NORMAL` | 16 MiB – 4 GiB | General-purpose allocation |
|
||||
|
||||
When `PMM_ZONE_NORMAL` is requested but no pages are available above 16 MiB (e.g., on systems with less than 16 MiB of RAM), the allocator falls back to `PMM_ZONE_DMA`.
|
||||
|
||||
Page 0 (address 0x00000000) is always marked as used to prevent returning NULL as a valid physical address.
|
||||
|
||||
### Initialization
|
||||
|
||||
1. The entire bitmap is initialized to "all used" (0xFFFFFFFF).
|
||||
2. The Multiboot2 memory map is parsed to discover available RAM regions.
|
||||
3. Available regions are marked as free in the bitmap.
|
||||
4. The kernel's own memory (between `_kernel_start` and `_kernel_end` linker symbols) is re-marked as used.
|
||||
5. The Multiboot2 info structure itself is marked as used.
|
||||
|
||||
### Linker Symbols
|
||||
|
||||
The linker script exports `_kernel_start` and `_kernel_end` symbols so the PMM knows which physical pages the kernel occupies and must not allocate.
|
||||
|
||||
## API
|
||||
|
||||
```c
|
||||
void init_pmm(uint32_t multiboot_addr);
|
||||
phys_addr_t pmm_alloc_page(pmm_zone_t zone);
|
||||
void pmm_free_page(phys_addr_t addr);
|
||||
```
|
||||
|
||||
- `init_pmm` — Parse Multiboot2 info and build the free-page bitmap.
|
||||
- `pmm_alloc_page` — Allocate a single 4 KiB page from the specified zone. Returns 0 on OOM.
|
||||
- `pmm_free_page` — Return a page to the free pool.
|
||||
|
||||
## Key Files
|
||||
|
||||
- `src/pmm.c` / `src/pmm.h` — Allocator implementation and zone definitions.
|
||||
- `src/linker.ld` — Exports `_kernel_start` / `_kernel_end`.
|
||||
|
||||
## Limitations
|
||||
|
||||
- First-fit allocation (linear scan) — O(n) per allocation.
|
||||
- No per-zone free counts or statistics yet.
|
||||
- Does not handle memory hot-plug or ACPI reclaim regions.
|
||||
109
docs/process.md
Normal file
109
docs/process.md
Normal file
@@ -0,0 +1,109 @@
|
||||
# Process Subsystem
|
||||
|
||||
## Overview
|
||||
|
||||
The process subsystem enables user-mode (Ring 3) process execution on ClaudeOS.
|
||||
It provides process creation, context switching via the timer interrupt, and
|
||||
system calls via `INT 0x80`.
|
||||
|
||||
## Architecture
|
||||
|
||||
### Ring Transition
|
||||
|
||||
x86 protected mode uses privilege rings 0–3. The kernel runs in Ring 0 (full
|
||||
hardware access) and user processes run in Ring 3 (restricted). The GDT
|
||||
defines segment descriptors for both:
|
||||
|
||||
| GDT Entry | Selector | Purpose | DPL |
|
||||
|-----------|----------|-----------------|-----|
|
||||
| 0 | 0x00 | Null | – |
|
||||
| 1 | 0x08 | Kernel Code | 0 |
|
||||
| 2 | 0x10 | Kernel Data | 0 |
|
||||
| 3 | 0x18 | User Code | 3 |
|
||||
| 4 | 0x20 | User Data | 3 |
|
||||
| 5 | 0x28 | TSS | 0 |
|
||||
|
||||
User-mode selectors include RPL=3: code = 0x1B, data = 0x23.
|
||||
|
||||
### Task State Segment (TSS)
|
||||
|
||||
The TSS (`tss.c`) stores the kernel stack pointer (SS0:ESP0) used when the CPU
|
||||
transitions from Ring 3 to Ring 0 on an interrupt. Before running each process,
|
||||
the scheduler updates TSS.ESP0 to that process's kernel stack top.
|
||||
|
||||
### Memory Layout
|
||||
|
||||
Each process gets its own page directory, cloned from the kernel's:
|
||||
|
||||
```
|
||||
0x00000000 – 0x07FFFFFF : Identity-mapped (kernel/device access)
|
||||
0x08048000 – ... : User code (loaded from binary image)
|
||||
0xBFFF7000 – 0xBFFFF000 : User stack (2 pages, grows downward)
|
||||
0xD0000000 – 0xF0000000 : Kernel heap (shared across all processes)
|
||||
```
|
||||
|
||||
### Process Control Block
|
||||
|
||||
```c
|
||||
typedef struct process {
|
||||
uint32_t pid;
|
||||
process_state_t state; // UNUSED, READY, RUNNING, BLOCKED, ZOMBIE
|
||||
registers_t saved_regs; // Full interrupt frame
|
||||
uint32_t kernel_stack; // Base of per-process kernel stack
|
||||
uint32_t kernel_stack_top; // TSS ESP0 value
|
||||
uint32_t page_directory; // Physical address of page directory
|
||||
uint32_t user_stack; // User stack virtual address
|
||||
uint32_t entry_point; // User code entry point
|
||||
int32_t exit_code; // Set on exit
|
||||
uint32_t parent_pid;
|
||||
char name[32];
|
||||
} process_t;
|
||||
```
|
||||
|
||||
## Context Switching
|
||||
|
||||
Context switching uses the interrupt frame directly:
|
||||
|
||||
1. Timer IRQ (or `INT 0x80` for SYS_YIELD) fires
|
||||
2. CPU pushes SS/ESP/EFLAGS/CS/EIP onto the process's kernel stack
|
||||
3. ISR stub pushes the rest (pusha + DS) forming a `registers_t`
|
||||
4. `schedule_tick()` is called with a pointer to these registers
|
||||
5. Current process's registers are saved into its PCB
|
||||
6. Next READY process's saved registers are written over the interrupt frame
|
||||
7. TSS.ESP0 is updated, CR3 is switched to the new page directory
|
||||
8. ISR stub restores the (now different) registers and `iret` enters the new
|
||||
process in user mode
|
||||
|
||||
This avoids separate context-switch assembly — the existing ISR stub handles
|
||||
everything.
|
||||
|
||||
## System Calls
|
||||
|
||||
System calls use `INT 0x80` with the call number in EAX:
|
||||
|
||||
| Number | Name | Arguments |
|
||||
|--------|-------------|------------------------------|
|
||||
| 0 | SYS_EXIT | EBX = exit code |
|
||||
| 1 | SYS_WRITE | EBX = fd, ECX = buf, EDX = len |
|
||||
| 2 | SYS_READ | (not implemented) |
|
||||
| 3 | SYS_FORK | (returns child PID/0) |
|
||||
| 4 | SYS_GETPID | (returns PID in EAX) |
|
||||
| 5 | SYS_YIELD | (voluntary preemption) |
|
||||
| 6 | SYS_WAITPID | EBX = child PID |
|
||||
| 7 | SYS_EXEC | (not implemented) |
|
||||
|
||||
The INT 0x80 IDT gate has DPL=3 (flags 0xEE) so user-mode code can invoke it.
|
||||
|
||||
## Initial Process Entry
|
||||
|
||||
`process_run_first()` performs the initial transition to user mode using an
|
||||
`iret` instruction that sets up Ring 3 segment selectors, the user stack
|
||||
pointer, and the entry point. This is a one-way transition — the function
|
||||
does not return.
|
||||
|
||||
## Files
|
||||
|
||||
- `tss.h` / `tss.c` — TSS structure and initialization
|
||||
- `process.h` / `process.c` — Process table, creation, scheduling, exit, fork
|
||||
- `syscall.h` / `syscall.c` — System call dispatch and handlers
|
||||
- `interrupts.S` — Assembly stubs: `isr128` (INT 0x80), `tss_flush`, `enter_usermode`
|
||||
78
docs/vfs.md
Normal file
78
docs/vfs.md
Normal file
@@ -0,0 +1,78 @@
|
||||
# Virtual Filesystem (VFS)
|
||||
|
||||
## Overview
|
||||
|
||||
The VFS provides a unified interface for file and directory operations across
|
||||
different filesystem implementations. Filesystem drivers register ops structs
|
||||
and are mounted at specific paths. Path resolution finds the longest-matching
|
||||
mount point and delegates to that filesystem.
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
User/Kernel Code
|
||||
│
|
||||
▼
|
||||
vfs_open("/initrd/hello-world", 0)
|
||||
│
|
||||
▼
|
||||
VFS: find_mount("/initrd/hello-world")
|
||||
│ → mount "/initrd", rel_path = "hello-world"
|
||||
▼
|
||||
resolve_path → initrd_finddir("hello-world")
|
||||
│
|
||||
▼
|
||||
vfs_read(fd, buf, size)
|
||||
│ → initrd_read(node, offset, size, buf)
|
||||
▼
|
||||
Returns file data from CPIO archive
|
||||
```
|
||||
|
||||
## Mount Points
|
||||
|
||||
Filesystems are mounted at absolute paths. The VFS supports up to 16
|
||||
simultaneous mounts. Path resolution uses longest-prefix matching:
|
||||
|
||||
```
|
||||
Mount: "/initrd" → handles /initrd/*
|
||||
Mount: "/sys" → handles /sys/*
|
||||
Mount: "/dev" → handles /dev/*
|
||||
```
|
||||
|
||||
## File Operations
|
||||
|
||||
| Function | Description |
|
||||
|----------|-------------|
|
||||
| `vfs_open(path, flags)` | Open a file, returns fd |
|
||||
| `vfs_close(fd)` | Close a file descriptor |
|
||||
| `vfs_read(fd, buf, size)` | Read bytes, advances offset |
|
||||
| `vfs_write(fd, buf, size)` | Write bytes, advances offset |
|
||||
| `vfs_seek(fd, offset, whence)` | Seek within file |
|
||||
| `vfs_readdir(path, idx, out)` | Read directory entry |
|
||||
| `vfs_stat(path, out)` | Get file info |
|
||||
|
||||
## Filesystem Driver Interface
|
||||
|
||||
Each filesystem provides a `vfs_fs_ops_t` struct:
|
||||
|
||||
```c
|
||||
typedef struct vfs_fs_ops {
|
||||
int (*open)(vfs_node_t *node, uint32_t flags);
|
||||
void (*close)(vfs_node_t *node);
|
||||
int32_t (*read)(vfs_node_t *node, uint32_t offset, uint32_t size, void *buf);
|
||||
int32_t (*write)(vfs_node_t *node, uint32_t offset, uint32_t size, const void *buf);
|
||||
int (*readdir)(vfs_node_t *dir, uint32_t idx, vfs_dirent_t *out);
|
||||
int (*finddir)(vfs_node_t *dir, const char *name, vfs_node_t *out);
|
||||
} vfs_fs_ops_t;
|
||||
```
|
||||
|
||||
## Initrd Filesystem Driver
|
||||
|
||||
The initrd filesystem (`initrd_fs.c`) provides read-only access to the CPIO
|
||||
ramdisk. It is automatically mounted at `/initrd` during boot. Files are
|
||||
accessed via zero-copy reads directly from the CPIO archive in memory.
|
||||
|
||||
## Files
|
||||
|
||||
- `src/vfs.h` / `src/vfs.c` — VFS core: mount table, fd table, path resolution
|
||||
- `src/initrd_fs.h` / `src/initrd_fs.c` — CPIO ramdisk VFS driver
|
||||
68
scripts/build_apps.sh
Executable file
68
scripts/build_apps.sh
Executable file
@@ -0,0 +1,68 @@
|
||||
#!/bin/sh
|
||||
# Build all user-mode applications as flat binaries.
|
||||
# Usage: build_apps.sh <apps_dir> <output_dir>
|
||||
# Each app directory in <apps_dir>/ gets compiled and its flat binary
|
||||
# is placed in <output_dir>/.
|
||||
set -e
|
||||
|
||||
APPS_DIR="$1"
|
||||
OUTPUT_DIR="$2"
|
||||
LINKER_SCRIPT="$APPS_DIR/user.ld"
|
||||
|
||||
CC="${CC:-clang}"
|
||||
OBJCOPY="${OBJCOPY:-objcopy}"
|
||||
CFLAGS="-ffreestanding -m32 -fno-pie -fno-pic -fno-builtin -fno-stack-protector -mno-sse -mno-mmx -O2 -Wall -I$APPS_DIR/libc"
|
||||
LDFLAGS="-m32 -nostdlib -no-pie -Wl,--no-dynamic-linker"
|
||||
|
||||
mkdir -p "$OUTPUT_DIR"
|
||||
|
||||
# Build crt0 if it exists
|
||||
CRT0_OBJ=""
|
||||
if [ -f "$APPS_DIR/libc/crt0.S" ]; then
|
||||
CRT0_OBJ="$OUTPUT_DIR/_crt0.o"
|
||||
$CC $CFLAGS -c "$APPS_DIR/libc/crt0.S" -o "$CRT0_OBJ"
|
||||
fi
|
||||
|
||||
for app_dir in "$APPS_DIR"/*/; do
|
||||
[ -d "$app_dir" ] || continue
|
||||
app_name=$(basename "$app_dir")
|
||||
|
||||
# Skip the libc directory (shared library, not an app)
|
||||
[ "$app_name" = "libc" ] && continue
|
||||
|
||||
echo "Building app: $app_name"
|
||||
|
||||
# Collect source files
|
||||
OBJ_FILES=""
|
||||
for src in "$app_dir"*.S "$app_dir"*.c; do
|
||||
[ -f "$src" ] || continue
|
||||
obj="$OUTPUT_DIR/${app_name}_$(basename "${src%.*}").o"
|
||||
$CC $CFLAGS -c "$src" -o "$obj"
|
||||
OBJ_FILES="$OBJ_FILES $obj"
|
||||
done
|
||||
|
||||
if [ -z "$OBJ_FILES" ]; then
|
||||
echo " No sources found, skipping"
|
||||
continue
|
||||
fi
|
||||
|
||||
# Link into ELF (include crt0 if app has .c files and doesn't have its own _start)
|
||||
elf="$OUTPUT_DIR/$app_name.elf"
|
||||
HAS_C_FILES=""
|
||||
for src in "$app_dir"*.c; do
|
||||
[ -f "$src" ] && HAS_C_FILES="yes"
|
||||
done
|
||||
|
||||
if [ -n "$HAS_C_FILES" ] && [ -n "$CRT0_OBJ" ]; then
|
||||
$CC $LDFLAGS -T "$LINKER_SCRIPT" "$CRT0_OBJ" $OBJ_FILES -o "$elf"
|
||||
else
|
||||
$CC $LDFLAGS -T "$LINKER_SCRIPT" $OBJ_FILES -o "$elf"
|
||||
fi
|
||||
|
||||
# Convert to flat binary (include .bss for zero-initialized data)
|
||||
bin="$OUTPUT_DIR/$app_name"
|
||||
$OBJCOPY -O binary --only-section=.text --only-section=.rodata --only-section=.data --only-section=.bss "$elf" "$bin"
|
||||
|
||||
size=$(wc -c < "$bin")
|
||||
echo " Built: $bin ($size bytes)"
|
||||
done
|
||||
16
scripts/gen_initrd.sh
Executable file
16
scripts/gen_initrd.sh
Executable file
@@ -0,0 +1,16 @@
|
||||
#!/bin/sh
|
||||
# Generate CPIO initial ramdisk from built application binaries.
|
||||
# Usage: gen_initrd.sh <binaries_dir> <output_file>
|
||||
# Packs all files in <binaries_dir> into a newc-format CPIO archive.
|
||||
set -e
|
||||
|
||||
BIN_DIR="$1"
|
||||
OUTPUT="$2"
|
||||
|
||||
# Ensure output directory exists
|
||||
mkdir -p "$(dirname "$OUTPUT")"
|
||||
|
||||
cd "$BIN_DIR"
|
||||
# Only pack actual binary files (no .o, .elf intermediates)
|
||||
find . -maxdepth 1 -type f ! -name '*.o' ! -name '*.elf' | cpio -o -H newc > "$OUTPUT" 2>/dev/null
|
||||
echo "Generated initrd: $(wc -c < "$OUTPUT") bytes"
|
||||
49
src/CMakeLists.txt
Normal file
49
src/CMakeLists.txt
Normal file
@@ -0,0 +1,49 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
add_executable(kernel
|
||||
boot.S
|
||||
gdt_flush.S
|
||||
gdt.c
|
||||
idt.c
|
||||
isr.c
|
||||
pic.c
|
||||
pmm.c
|
||||
paging.c
|
||||
kmalloc.c
|
||||
string.c
|
||||
driver.c
|
||||
vga.c
|
||||
tss.c
|
||||
process.c
|
||||
syscall.c
|
||||
cpio.c
|
||||
vfs.c
|
||||
initrd_fs.c
|
||||
devicefs.c
|
||||
sysfs.c
|
||||
ide.c
|
||||
mbr.c
|
||||
fat32.c
|
||||
floppy.c
|
||||
ne2000.c
|
||||
e3c509.c
|
||||
ethernet.c
|
||||
ipv4.c
|
||||
arp.c
|
||||
dhcp.c
|
||||
udp.c
|
||||
tcp.c
|
||||
graphics.c
|
||||
env.c
|
||||
keyboard.c
|
||||
interrupts.S
|
||||
kernel.c
|
||||
)
|
||||
|
||||
# Use our custom linker script
|
||||
target_link_options(kernel PRIVATE -T ${CMAKE_CURRENT_SOURCE_DIR}/linker.ld)
|
||||
|
||||
target_include_directories(kernel PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/vendor
|
||||
${CMAKE_SOURCE_DIR}/include # If created later
|
||||
)
|
||||
382
src/arp.c
Normal file
382
src/arp.c
Normal file
@@ -0,0 +1,382 @@
|
||||
/**
|
||||
* @file arp.c
|
||||
* @brief Address Resolution Protocol (ARP) implementation.
|
||||
*
|
||||
* Maintains an ARP cache mapping IPv4 addresses to Ethernet MAC
|
||||
* addresses. Sends ARP requests and processes ARP replies.
|
||||
* Also responds to incoming ARP requests for our own IP addresses.
|
||||
*
|
||||
* The ARP table is exposed via sysfs at /sys/arp.
|
||||
*/
|
||||
|
||||
#include "arp.h"
|
||||
#include "ethernet.h"
|
||||
#include "ipv4.h"
|
||||
#include "sysfs.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Debug print helpers */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/* ================================================================
|
||||
* Global state
|
||||
* ================================================================ */
|
||||
|
||||
/** ARP cache table. */
|
||||
static arp_entry_t arp_table[ARP_TABLE_SIZE];
|
||||
|
||||
/** Number of active entries. */
|
||||
static uint32_t arp_count = 0;
|
||||
|
||||
/* ================================================================
|
||||
* ARP cache management
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Find an ARP entry by IP address.
|
||||
* @return Pointer to entry, or NULL if not found.
|
||||
*/
|
||||
static arp_entry_t *arp_find(uint32_t ip) {
|
||||
for (uint32_t i = 0; i < ARP_TABLE_SIZE; i++) {
|
||||
if (arp_table[i].state != ARP_STATE_FREE &&
|
||||
arp_table[i].ip_addr == ip) {
|
||||
return &arp_table[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Allocate a new ARP entry.
|
||||
* Reuses a free slot, or evicts the oldest entry.
|
||||
*/
|
||||
static arp_entry_t *arp_alloc(void) {
|
||||
/* Look for a free slot */
|
||||
for (uint32_t i = 0; i < ARP_TABLE_SIZE; i++) {
|
||||
if (arp_table[i].state == ARP_STATE_FREE) {
|
||||
return &arp_table[i];
|
||||
}
|
||||
}
|
||||
|
||||
/* Evict the oldest entry (lowest timestamp) */
|
||||
arp_entry_t *oldest = &arp_table[0];
|
||||
for (uint32_t i = 1; i < ARP_TABLE_SIZE; i++) {
|
||||
if (arp_table[i].timestamp < oldest->timestamp) {
|
||||
oldest = &arp_table[i];
|
||||
}
|
||||
}
|
||||
return oldest;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* ARP send
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Send an ARP packet.
|
||||
*/
|
||||
static int arp_send_packet(uint32_t iface_idx, uint16_t operation,
|
||||
const uint8_t *target_mac, uint32_t target_ip) {
|
||||
eth_iface_t *iface = ethernet_get_iface(iface_idx);
|
||||
if (!iface || !iface->active) return -1;
|
||||
|
||||
arp_packet_t pkt;
|
||||
pkt.hw_type = htons(ARP_HW_ETHER);
|
||||
pkt.proto_type = htons(ETHERTYPE_IPV4);
|
||||
pkt.hw_len = 6;
|
||||
pkt.proto_len = 4;
|
||||
pkt.operation = htons(operation);
|
||||
|
||||
/* Sender: our MAC and IP */
|
||||
memcpy(pkt.sender_mac, iface->mac, 6);
|
||||
pkt.sender_ip = htonl(iface->ip_addr);
|
||||
|
||||
/* Target */
|
||||
memcpy(pkt.target_mac, target_mac, 6);
|
||||
pkt.target_ip = htonl(target_ip);
|
||||
|
||||
/* For ARP requests, broadcast; for replies, send directly */
|
||||
const uint8_t *dst_mac;
|
||||
if (operation == ARP_OP_REQUEST) {
|
||||
static const uint8_t bcast[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
dst_mac = bcast;
|
||||
} else {
|
||||
dst_mac = target_mac;
|
||||
}
|
||||
|
||||
return ethernet_send(iface, dst_mac, ETHERTYPE_ARP,
|
||||
&pkt, ARP_PACKET_SIZE);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Public API
|
||||
* ================================================================ */
|
||||
|
||||
int arp_lookup(uint32_t ip, uint8_t *mac) {
|
||||
arp_entry_t *entry = arp_find(ip);
|
||||
if (entry && entry->state == ARP_STATE_RESOLVED) {
|
||||
memcpy(mac, entry->mac, 6);
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int arp_request(uint32_t iface_idx, uint32_t target_ip) {
|
||||
static const uint8_t zero_mac[6] = {0, 0, 0, 0, 0, 0};
|
||||
|
||||
/* Create an incomplete entry if we don't have one */
|
||||
arp_entry_t *entry = arp_find(target_ip);
|
||||
if (!entry) {
|
||||
entry = arp_alloc();
|
||||
memset(entry, 0, sizeof(arp_entry_t));
|
||||
entry->ip_addr = target_ip;
|
||||
entry->state = ARP_STATE_INCOMPLETE;
|
||||
entry->iface_idx = (uint8_t)iface_idx;
|
||||
entry->timestamp = 0; /* will be updated on reply */
|
||||
arp_count++;
|
||||
}
|
||||
|
||||
return arp_send_packet(iface_idx, ARP_OP_REQUEST, zero_mac, target_ip);
|
||||
}
|
||||
|
||||
int arp_resolve(uint32_t iface_idx, uint32_t ip, uint8_t *mac) {
|
||||
/* Broadcast address — no ARP needed */
|
||||
if (ip == 0xFFFFFFFF) {
|
||||
memset(mac, 0xFF, 6);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Check cache */
|
||||
if (arp_lookup(ip, mac) == 0) return 0;
|
||||
|
||||
/* Send ARP request */
|
||||
arp_request(iface_idx, ip);
|
||||
return -1;
|
||||
}
|
||||
|
||||
void arp_receive(const void *data, uint32_t len, uint32_t iface_idx) {
|
||||
if (len < ARP_PACKET_SIZE) return;
|
||||
|
||||
const arp_packet_t *pkt = (const arp_packet_t *)data;
|
||||
|
||||
/* Validate: must be Ethernet/IPv4 */
|
||||
if (ntohs(pkt->hw_type) != ARP_HW_ETHER) return;
|
||||
if (ntohs(pkt->proto_type) != ETHERTYPE_IPV4) return;
|
||||
if (pkt->hw_len != 6 || pkt->proto_len != 4) return;
|
||||
|
||||
uint32_t sender_ip = ntohl(pkt->sender_ip);
|
||||
uint32_t target_ip = ntohl(pkt->target_ip);
|
||||
uint16_t operation = ntohs(pkt->operation);
|
||||
|
||||
/* Update or create ARP cache entry for sender */
|
||||
arp_entry_t *entry = arp_find(sender_ip);
|
||||
if (entry) {
|
||||
memcpy(entry->mac, pkt->sender_mac, 6);
|
||||
entry->state = ARP_STATE_RESOLVED;
|
||||
entry->iface_idx = (uint8_t)iface_idx;
|
||||
} else {
|
||||
entry = arp_alloc();
|
||||
entry->ip_addr = sender_ip;
|
||||
memcpy(entry->mac, pkt->sender_mac, 6);
|
||||
entry->state = ARP_STATE_RESOLVED;
|
||||
entry->iface_idx = (uint8_t)iface_idx;
|
||||
entry->timestamp = 0;
|
||||
arp_count++;
|
||||
}
|
||||
|
||||
/* Check if this ARP is targeted at us */
|
||||
eth_iface_t *iface = ethernet_get_iface(iface_idx);
|
||||
if (!iface || iface->ip_addr == 0) return;
|
||||
|
||||
if (target_ip == iface->ip_addr && operation == ARP_OP_REQUEST) {
|
||||
/* Send ARP reply */
|
||||
arp_send_packet(iface_idx, ARP_OP_REPLY,
|
||||
pkt->sender_mac, sender_ip);
|
||||
}
|
||||
}
|
||||
|
||||
int arp_add_static(uint32_t ip, const uint8_t *mac, uint32_t iface_idx) {
|
||||
arp_entry_t *entry = arp_find(ip);
|
||||
if (!entry) {
|
||||
entry = arp_alloc();
|
||||
if (!entry) return -1;
|
||||
arp_count++;
|
||||
}
|
||||
|
||||
entry->ip_addr = ip;
|
||||
memcpy(entry->mac, mac, 6);
|
||||
entry->state = ARP_STATE_RESOLVED;
|
||||
entry->iface_idx = (uint8_t)iface_idx;
|
||||
entry->timestamp = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const arp_entry_t *arp_get_entry(uint32_t index) {
|
||||
uint32_t count = 0;
|
||||
for (uint32_t i = 0; i < ARP_TABLE_SIZE; i++) {
|
||||
if (arp_table[i].state == ARP_STATE_FREE) continue;
|
||||
if (count == index) return &arp_table[i];
|
||||
count++;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint32_t arp_get_count(void) {
|
||||
return arp_count;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Sysfs interface: /sys/arp
|
||||
*
|
||||
* Layout:
|
||||
* /sys/arp/
|
||||
* table - ARP table in human-readable format
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Format a MAC address as "XX:XX:XX:XX:XX:XX".
|
||||
*/
|
||||
static void mac_to_str(const uint8_t *mac, char *buf) {
|
||||
static const char hex[] = "0123456789ABCDEF";
|
||||
int pos = 0;
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if (i > 0) buf[pos++] = ':';
|
||||
buf[pos++] = hex[(mac[i] >> 4) & 0xF];
|
||||
buf[pos++] = hex[mac[i] & 0xF];
|
||||
}
|
||||
buf[pos] = '\0';
|
||||
}
|
||||
|
||||
/**
|
||||
* Format an IPv4 address (host byte order) as dotted decimal.
|
||||
*/
|
||||
static int ip_to_str(uint32_t ip, char *buf, uint32_t size) {
|
||||
int pos = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (i > 0 && pos < (int)size - 1) buf[pos++] = '.';
|
||||
uint8_t octet = (uint8_t)((ip >> (24 - i * 8)) & 0xFF);
|
||||
if (octet >= 100 && pos < (int)size - 3) {
|
||||
buf[pos++] = (char)('0' + octet / 100);
|
||||
buf[pos++] = (char)('0' + (octet % 100) / 10);
|
||||
buf[pos++] = (char)('0' + octet % 10);
|
||||
} else if (octet >= 10 && pos < (int)size - 2) {
|
||||
buf[pos++] = (char)('0' + octet / 10);
|
||||
buf[pos++] = (char)('0' + octet % 10);
|
||||
} else if (pos < (int)size - 1) {
|
||||
buf[pos++] = (char)('0' + octet);
|
||||
}
|
||||
}
|
||||
if (pos < (int)size) buf[pos] = '\0';
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a string to buf at position *pos.
|
||||
*/
|
||||
static void append_str(char *buf, uint32_t size, int *pos, const char *s) {
|
||||
while (*s && *pos < (int)size - 1) buf[(*pos)++] = *s++;
|
||||
}
|
||||
|
||||
static int arp_sysfs_list(void *ctx, const char *path, uint32_t idx,
|
||||
sysfs_entry_t *out) {
|
||||
(void)ctx;
|
||||
|
||||
if (path[0] == '\0') {
|
||||
if (idx == 0) {
|
||||
memset(out, 0, sizeof(sysfs_entry_t));
|
||||
strncpy(out->name, "table", SYSFS_MAX_NAME - 1);
|
||||
out->is_dir = 0;
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int arp_sysfs_read(void *ctx, const char *path, char *buf,
|
||||
uint32_t buf_size) {
|
||||
(void)ctx;
|
||||
|
||||
if (strcmp(path, "table") != 0) return -1;
|
||||
|
||||
int pos = 0;
|
||||
|
||||
/* Header */
|
||||
append_str(buf, buf_size, &pos, "IP Address MAC Address Iface State\n");
|
||||
|
||||
for (uint32_t i = 0; i < ARP_TABLE_SIZE; i++) {
|
||||
if (arp_table[i].state == ARP_STATE_FREE) continue;
|
||||
|
||||
/* IP address */
|
||||
char ip_buf[16];
|
||||
ip_to_str(arp_table[i].ip_addr, ip_buf, sizeof(ip_buf));
|
||||
append_str(buf, buf_size, &pos, ip_buf);
|
||||
|
||||
/* Pad to column 17 */
|
||||
int ip_len = (int)strlen(ip_buf);
|
||||
for (int p = ip_len; p < 17; p++) {
|
||||
if (pos < (int)buf_size - 1) buf[pos++] = ' ';
|
||||
}
|
||||
|
||||
/* MAC */
|
||||
char mac_buf[18];
|
||||
mac_to_str(arp_table[i].mac, mac_buf);
|
||||
append_str(buf, buf_size, &pos, mac_buf);
|
||||
if (pos < (int)buf_size - 1) buf[pos++] = ' ';
|
||||
if (pos < (int)buf_size - 1) buf[pos++] = ' ';
|
||||
|
||||
/* Interface */
|
||||
eth_iface_t *iface = ethernet_get_iface(arp_table[i].iface_idx);
|
||||
if (iface) {
|
||||
append_str(buf, buf_size, &pos, iface->name);
|
||||
} else {
|
||||
append_str(buf, buf_size, &pos, "?");
|
||||
}
|
||||
|
||||
/* Pad and state */
|
||||
int name_len = iface ? (int)strlen(iface->name) : 1;
|
||||
for (int p = name_len; p < 7; p++) {
|
||||
if (pos < (int)buf_size - 1) buf[pos++] = ' ';
|
||||
}
|
||||
|
||||
if (arp_table[i].state == ARP_STATE_RESOLVED) {
|
||||
append_str(buf, buf_size, &pos, "resolved");
|
||||
} else {
|
||||
append_str(buf, buf_size, &pos, "incomplete");
|
||||
}
|
||||
|
||||
if (pos < (int)buf_size - 1) buf[pos++] = '\n';
|
||||
}
|
||||
|
||||
buf[pos] = '\0';
|
||||
return pos;
|
||||
}
|
||||
|
||||
static int arp_sysfs_write(void *ctx, const char *path, const char *buf,
|
||||
uint32_t size) {
|
||||
(void)ctx;
|
||||
(void)path;
|
||||
(void)buf;
|
||||
(void)size;
|
||||
return -1; /* Read-only for now */
|
||||
}
|
||||
|
||||
static sysfs_ops_t arp_sysfs_ops = {
|
||||
.list = arp_sysfs_list,
|
||||
.read = arp_sysfs_read,
|
||||
.write = arp_sysfs_write,
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* Initialization
|
||||
* ================================================================ */
|
||||
|
||||
void arp_init(void) {
|
||||
memset(arp_table, 0, sizeof(arp_table));
|
||||
arp_count = 0;
|
||||
|
||||
sysfs_register("arp", &arp_sysfs_ops, NULL);
|
||||
|
||||
offset_print(" ARP: initialized\n");
|
||||
}
|
||||
145
src/arp.h
Normal file
145
src/arp.h
Normal file
@@ -0,0 +1,145 @@
|
||||
/**
|
||||
* @file arp.h
|
||||
* @brief Address Resolution Protocol (ARP) subsystem.
|
||||
*
|
||||
* Implements ARP (RFC 826) for mapping IPv4 addresses to Ethernet
|
||||
* MAC addresses. Maintains an ARP cache and handles ARP requests
|
||||
* and replies.
|
||||
*
|
||||
* The ARP table is exposed via sysfs at /sys/arp for userspace tools.
|
||||
*/
|
||||
|
||||
#ifndef ARP_H
|
||||
#define ARP_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/** Maximum number of ARP cache entries. */
|
||||
#define ARP_TABLE_SIZE 32
|
||||
|
||||
/** ARP hardware type: Ethernet */
|
||||
#define ARP_HW_ETHER 1
|
||||
|
||||
/** ARP operation codes */
|
||||
#define ARP_OP_REQUEST 1
|
||||
#define ARP_OP_REPLY 2
|
||||
|
||||
/** ARP cache entry states */
|
||||
#define ARP_STATE_FREE 0 /**< Unused slot. */
|
||||
#define ARP_STATE_INCOMPLETE 1 /**< Request sent, awaiting reply. */
|
||||
#define ARP_STATE_RESOLVED 2 /**< MAC address known. */
|
||||
|
||||
/* ================================================================
|
||||
* ARP packet (28 bytes for IPv4-over-Ethernet)
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* ARP packet structure for IPv4 over Ethernet.
|
||||
* All multi-byte fields are in network byte order.
|
||||
*/
|
||||
typedef struct __attribute__((packed)) arp_packet {
|
||||
uint16_t hw_type; /**< Hardware type (1 = Ethernet). */
|
||||
uint16_t proto_type; /**< Protocol type (0x0800 = IPv4). */
|
||||
uint8_t hw_len; /**< Hardware address length (6). */
|
||||
uint8_t proto_len; /**< Protocol address length (4). */
|
||||
uint16_t operation; /**< Operation (1=request, 2=reply). */
|
||||
uint8_t sender_mac[6]; /**< Sender hardware address. */
|
||||
uint32_t sender_ip; /**< Sender protocol address. */
|
||||
uint8_t target_mac[6]; /**< Target hardware address. */
|
||||
uint32_t target_ip; /**< Target protocol address. */
|
||||
} arp_packet_t;
|
||||
|
||||
/** ARP packet size. */
|
||||
#define ARP_PACKET_SIZE 28
|
||||
|
||||
/* ================================================================
|
||||
* ARP cache entry
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* ARP cache entry.
|
||||
*/
|
||||
typedef struct arp_entry {
|
||||
uint32_t ip_addr; /**< IPv4 address (host byte order). */
|
||||
uint8_t mac[6]; /**< Resolved MAC address. */
|
||||
uint8_t state; /**< ARP_STATE_*. */
|
||||
uint8_t iface_idx; /**< Ethernet interface index. */
|
||||
uint32_t timestamp; /**< Time when entry was created (tick count). */
|
||||
} arp_entry_t;
|
||||
|
||||
/* ================================================================
|
||||
* Public API
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Initialize the ARP subsystem.
|
||||
* Registers sysfs namespace "arp".
|
||||
*/
|
||||
void arp_init(void);
|
||||
|
||||
/**
|
||||
* Look up an IP address in the ARP cache.
|
||||
*
|
||||
* @param ip IPv4 address (host byte order).
|
||||
* @param mac Output: 6-byte MAC address if found.
|
||||
* @return 0 if found and resolved, -1 if not in cache or incomplete.
|
||||
*/
|
||||
int arp_lookup(uint32_t ip, uint8_t *mac);
|
||||
|
||||
/**
|
||||
* Send an ARP request for the given IP address.
|
||||
*
|
||||
* @param iface_idx Ethernet interface to send on.
|
||||
* @param target_ip IP address to resolve (host byte order).
|
||||
* @return 0 on success, -1 on failure.
|
||||
*/
|
||||
int arp_request(uint32_t iface_idx, uint32_t target_ip);
|
||||
|
||||
/**
|
||||
* Resolve an IP address to a MAC address.
|
||||
*
|
||||
* Checks the ARP cache first. If not found, sends an ARP request
|
||||
* and returns -1 (caller should retry later).
|
||||
*
|
||||
* @param iface_idx Ethernet interface index.
|
||||
* @param ip IPv4 address (host byte order).
|
||||
* @param mac Output: 6-byte MAC address.
|
||||
* @return 0 if resolved, -1 if pending.
|
||||
*/
|
||||
int arp_resolve(uint32_t iface_idx, uint32_t ip, uint8_t *mac);
|
||||
|
||||
/**
|
||||
* Process an incoming ARP packet.
|
||||
*
|
||||
* Called by the Ethernet subsystem when an ARP frame is received.
|
||||
*
|
||||
* @param data Raw ARP packet.
|
||||
* @param len Packet length.
|
||||
* @param iface_idx Interface the packet arrived on.
|
||||
*/
|
||||
void arp_receive(const void *data, uint32_t len, uint32_t iface_idx);
|
||||
|
||||
/**
|
||||
* Add a static ARP entry.
|
||||
*
|
||||
* @param ip IPv4 address (host byte order).
|
||||
* @param mac 6-byte MAC address.
|
||||
* @param iface_idx Ethernet interface index.
|
||||
* @return 0 on success, -1 if table full.
|
||||
*/
|
||||
int arp_add_static(uint32_t ip, const uint8_t *mac, uint32_t iface_idx);
|
||||
|
||||
/**
|
||||
* Get an ARP table entry by index (for enumeration).
|
||||
*
|
||||
* @param index 0-based index.
|
||||
* @return Pointer to entry, or NULL if out of range.
|
||||
*/
|
||||
const arp_entry_t *arp_get_entry(uint32_t index);
|
||||
|
||||
/**
|
||||
* Get the number of active ARP entries.
|
||||
*/
|
||||
uint32_t arp_get_count(void);
|
||||
|
||||
#endif /* ARP_H */
|
||||
64
src/boot.S
Normal file
64
src/boot.S
Normal file
@@ -0,0 +1,64 @@
|
||||
#define ASM_FILE
|
||||
#include <multiboot2.h>
|
||||
|
||||
/* Multiboot 1 header constants */
|
||||
.set ALIGN, 1<<0 /* align loaded modules on page boundaries */
|
||||
.set MEMINFO, 1<<1 /* provide memory map */
|
||||
.set FLAGS, ALIGN | MEMINFO /* this is the Multiboot 'flag' field */
|
||||
.set MAGIC, 0x1BADB002 /* 'magic number' lets bootloader find the header */
|
||||
.set CHECKSUM, -(MAGIC + FLAGS) /* checksum of above, to prove we are multiboot */
|
||||
|
||||
.section .multiboot
|
||||
.align 4
|
||||
multiboot1_header:
|
||||
.long MAGIC
|
||||
.long FLAGS
|
||||
.long CHECKSUM
|
||||
|
||||
.align 8
|
||||
multiboot_header:
|
||||
/* magic */
|
||||
.long MULTIBOOT2_HEADER_MAGIC
|
||||
/* architecture: 0 (protected mode i386) */
|
||||
.long MULTIBOOT_ARCHITECTURE_I386
|
||||
/* header length */
|
||||
.long multiboot_header_end - multiboot_header
|
||||
/* checksum */
|
||||
.long -(MULTIBOOT2_HEADER_MAGIC + MULTIBOOT_ARCHITECTURE_I386 + (multiboot_header_end - multiboot_header))
|
||||
|
||||
/* End tag */
|
||||
.align 8
|
||||
.short MULTIBOOT_HEADER_TAG_END
|
||||
.short 0
|
||||
.long 8
|
||||
multiboot_header_end:
|
||||
|
||||
.section .bss
|
||||
.align 16
|
||||
stack_bottom:
|
||||
.skip 16384 # 16 KiB
|
||||
stack_top:
|
||||
|
||||
.section .text
|
||||
.global _start
|
||||
.type _start, @function
|
||||
_start:
|
||||
/* Set up stack */
|
||||
mov $stack_top, %esp
|
||||
|
||||
/* Reset EFLAGS */
|
||||
push $0
|
||||
popf
|
||||
|
||||
/* Push magic and multiboot info pointer onto stack for kernel_main */
|
||||
/* Multiboot2 puts magic in EAX, pointer in EBX */
|
||||
push %ebx
|
||||
push %eax
|
||||
|
||||
call kernel_main
|
||||
|
||||
cli
|
||||
1: hlt
|
||||
jmp 1b
|
||||
|
||||
.size _start, . - _start
|
||||
179
src/cpio.c
Normal file
179
src/cpio.c
Normal file
@@ -0,0 +1,179 @@
|
||||
/**
|
||||
* @file cpio.c
|
||||
* @brief CPIO newc archive parser implementation.
|
||||
*
|
||||
* Parses CPIO archives in the SVR4/newc format. The archive is expected
|
||||
* to be loaded into memory by GRUB as a Multiboot2 module.
|
||||
*/
|
||||
|
||||
#include "cpio.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Debug print helpers defined in kernel.c */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/** Pointer to the CPIO archive in memory. */
|
||||
static const uint8_t *archive = NULL;
|
||||
|
||||
/** Size of the archive (0 if unknown). */
|
||||
static uint32_t archive_len = 0;
|
||||
|
||||
/**
|
||||
* Parse an N-character hexadecimal ASCII string to uint32_t.
|
||||
*
|
||||
* @param s Pointer to hex string.
|
||||
* @param n Number of characters to parse.
|
||||
* @return Parsed value.
|
||||
*/
|
||||
static uint32_t parse_hex(const char *s, int n) {
|
||||
uint32_t val = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
char c = s[i];
|
||||
uint32_t digit;
|
||||
if (c >= '0' && c <= '9') {
|
||||
digit = (uint32_t)(c - '0');
|
||||
} else if (c >= 'a' && c <= 'f') {
|
||||
digit = (uint32_t)(c - 'a' + 10);
|
||||
} else if (c >= 'A' && c <= 'F') {
|
||||
digit = (uint32_t)(c - 'A' + 10);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
val = (val << 4) | digit;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Round up to 4-byte boundary.
|
||||
*/
|
||||
static inline uint32_t align4(uint32_t v) {
|
||||
return (v + 3) & ~3u;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a CPIO entry at the given offset.
|
||||
*
|
||||
* @param offset Byte offset into the archive.
|
||||
* @param entry Output entry information.
|
||||
* @return Offset of the next entry, or 0 on error/end.
|
||||
*/
|
||||
static uint32_t parse_entry(uint32_t offset, cpio_entry_t *entry) {
|
||||
if (!archive) return 0;
|
||||
|
||||
const cpio_newc_header_t *hdr = (const cpio_newc_header_t *)(archive + offset);
|
||||
|
||||
/* Verify magic */
|
||||
if (memcmp(hdr->magic, CPIO_MAGIC, 6) != 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t namesize = parse_hex(hdr->namesize, 8);
|
||||
uint32_t filesize = parse_hex(hdr->filesize, 8);
|
||||
uint32_t mode = parse_hex(hdr->mode, 8);
|
||||
|
||||
/* Filename starts right after the header */
|
||||
const char *name = (const char *)(archive + offset + CPIO_HEADER_SIZE);
|
||||
|
||||
/* Data starts after header + name, aligned to 4 bytes */
|
||||
uint32_t data_offset = align4(offset + CPIO_HEADER_SIZE + namesize);
|
||||
const void *data = archive + data_offset;
|
||||
|
||||
/* Next entry starts after data, aligned to 4 bytes */
|
||||
uint32_t next_offset = align4(data_offset + filesize);
|
||||
|
||||
entry->name = name;
|
||||
entry->namesize = namesize;
|
||||
entry->data = data;
|
||||
entry->datasize = filesize;
|
||||
entry->mode = mode;
|
||||
|
||||
return next_offset;
|
||||
}
|
||||
|
||||
void cpio_init(const void *archive_start, uint32_t archive_size) {
|
||||
archive = (const uint8_t *)archive_start;
|
||||
archive_len = archive_size;
|
||||
|
||||
offset_print(" CPIO: archive at ");
|
||||
print_hex((uint32_t)archive_start);
|
||||
offset_print(" CPIO: size = ");
|
||||
print_hex(archive_size);
|
||||
|
||||
/* Count and list entries */
|
||||
uint32_t count = 0;
|
||||
uint32_t off = 0;
|
||||
cpio_entry_t entry;
|
||||
while (1) {
|
||||
uint32_t next = parse_entry(off, &entry);
|
||||
if (next == 0) break;
|
||||
if (strcmp(entry.name, CPIO_TRAILER) == 0) break;
|
||||
|
||||
offset_print(" CPIO: [");
|
||||
offset_print(entry.name);
|
||||
offset_print("] size=");
|
||||
print_hex(entry.datasize);
|
||||
|
||||
count++;
|
||||
off = next;
|
||||
}
|
||||
|
||||
offset_print(" CPIO: ");
|
||||
print_hex(count);
|
||||
offset_print(" CPIO: files found\n");
|
||||
}
|
||||
|
||||
int cpio_find(const char *name, cpio_entry_t *entry) {
|
||||
if (!archive) return -1;
|
||||
|
||||
uint32_t off = 0;
|
||||
while (1) {
|
||||
uint32_t next = parse_entry(off, entry);
|
||||
if (next == 0) return -1;
|
||||
if (strcmp(entry->name, CPIO_TRAILER) == 0) return -1;
|
||||
|
||||
/* Match by name. CPIO entries often have "./" prefix, try both. */
|
||||
if (strcmp(entry->name, name) == 0) return 0;
|
||||
|
||||
/* Try matching without "./" prefix */
|
||||
if (entry->name[0] == '.' && entry->name[1] == '/' &&
|
||||
strcmp(entry->name + 2, name) == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Try matching with "./" prefix */
|
||||
if (name[0] != '.' && entry->namesize > 2) {
|
||||
/* Already handled above */
|
||||
}
|
||||
|
||||
off = next;
|
||||
}
|
||||
}
|
||||
|
||||
int cpio_next(uint32_t *offset, cpio_entry_t *entry) {
|
||||
if (!archive) return -1;
|
||||
|
||||
uint32_t next = parse_entry(*offset, entry);
|
||||
if (next == 0) return -1;
|
||||
if (strcmp(entry->name, CPIO_TRAILER) == 0) return -1;
|
||||
|
||||
*offset = next;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t cpio_count(void) {
|
||||
if (!archive) return 0;
|
||||
|
||||
uint32_t count = 0;
|
||||
uint32_t off = 0;
|
||||
cpio_entry_t entry;
|
||||
while (1) {
|
||||
uint32_t next = parse_entry(off, &entry);
|
||||
if (next == 0) break;
|
||||
if (strcmp(entry.name, CPIO_TRAILER) == 0) break;
|
||||
count++;
|
||||
off = next;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
92
src/cpio.h
Normal file
92
src/cpio.h
Normal file
@@ -0,0 +1,92 @@
|
||||
/**
|
||||
* @file cpio.h
|
||||
* @brief CPIO newc archive parser.
|
||||
*
|
||||
* Parses CPIO archives in the SVR4/newc format (magic "070701").
|
||||
* Used to read files from the initial ramdisk loaded by GRUB.
|
||||
*/
|
||||
|
||||
#ifndef CPIO_H
|
||||
#define CPIO_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/**
|
||||
* CPIO newc header (110 bytes).
|
||||
* All fields are 8-character hexadecimal ASCII strings.
|
||||
*/
|
||||
typedef struct cpio_newc_header {
|
||||
char magic[6]; /**< Must be "070701". */
|
||||
char ino[8];
|
||||
char mode[8];
|
||||
char uid[8];
|
||||
char gid[8];
|
||||
char nlink[8];
|
||||
char mtime[8];
|
||||
char filesize[8];
|
||||
char devmajor[8];
|
||||
char devminor[8];
|
||||
char rdevmajor[8];
|
||||
char rdevminor[8];
|
||||
char namesize[8];
|
||||
char check[8];
|
||||
} cpio_newc_header_t;
|
||||
|
||||
/** Size of the CPIO newc header in bytes. */
|
||||
#define CPIO_HEADER_SIZE 110
|
||||
|
||||
/** CPIO newc magic string. */
|
||||
#define CPIO_MAGIC "070701"
|
||||
|
||||
/** Trailer entry name that marks end of archive. */
|
||||
#define CPIO_TRAILER "TRAILER!!!"
|
||||
|
||||
/**
|
||||
* CPIO file entry (result of iteration or lookup).
|
||||
*/
|
||||
typedef struct cpio_entry {
|
||||
const char *name; /**< Filename (pointer into archive). */
|
||||
uint32_t namesize; /**< Length of filename including NUL. */
|
||||
const void *data; /**< Pointer to file data within archive. */
|
||||
uint32_t datasize; /**< Size of file data in bytes. */
|
||||
uint32_t mode; /**< File mode/permissions. */
|
||||
} cpio_entry_t;
|
||||
|
||||
/**
|
||||
* Initialize the CPIO parser with the archive location.
|
||||
*
|
||||
* @param archive_start Pointer to the start of the CPIO archive in memory.
|
||||
* @param archive_size Size of the archive in bytes (0 if unknown).
|
||||
*/
|
||||
void cpio_init(const void *archive_start, uint32_t archive_size);
|
||||
|
||||
/**
|
||||
* Find a file in the CPIO archive by name.
|
||||
*
|
||||
* @param name Filename to search for (without leading "./").
|
||||
* @param entry Output: filled with file information if found.
|
||||
* @return 0 on success, -1 if not found.
|
||||
*/
|
||||
int cpio_find(const char *name, cpio_entry_t *entry);
|
||||
|
||||
/**
|
||||
* Iterate through all entries in the CPIO archive.
|
||||
*
|
||||
* Call with *offset = 0 to start. Returns 0 on success, -1 when no
|
||||
* more entries exist or the TRAILER is reached.
|
||||
*
|
||||
* @param offset In/out: current position in the archive.
|
||||
* @param entry Output: filled with the next entry's information.
|
||||
* @return 0 on success, -1 at end of archive.
|
||||
*/
|
||||
int cpio_next(uint32_t *offset, cpio_entry_t *entry);
|
||||
|
||||
/**
|
||||
* Get the number of files in the CPIO archive (excluding TRAILER).
|
||||
*
|
||||
* @return Number of files.
|
||||
*/
|
||||
uint32_t cpio_count(void);
|
||||
|
||||
#endif /* CPIO_H */
|
||||
352
src/devicefs.c
Normal file
352
src/devicefs.c
Normal file
@@ -0,0 +1,352 @@
|
||||
/**
|
||||
* @file devicefs.c
|
||||
* @brief Device filesystem (devicefs) implementation.
|
||||
*
|
||||
* Provides a VFS driver mounted at /dev that exposes block and character
|
||||
* devices. Kernel drivers register devices via devicefs_register_block()
|
||||
* or devicefs_register_char(), and the devicefs assigns sequential numbers
|
||||
* per device class (e.g., hdd1, hdd2, cd1).
|
||||
*
|
||||
* The VFS interface supports:
|
||||
* - readdir: lists all registered devices
|
||||
* - finddir: looks up a device by name
|
||||
* - read/write: delegates to the device's block or char operations
|
||||
*/
|
||||
|
||||
#include "devicefs.h"
|
||||
#include "vfs.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Debug print helpers defined in kernel.c */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/** Device table. */
|
||||
static devicefs_device_t devices[DEVICEFS_MAX_DEVICES];
|
||||
|
||||
/** Number of active devices. */
|
||||
static uint32_t device_count = 0;
|
||||
|
||||
/**
|
||||
* Find a free slot in the device table.
|
||||
* @return Index of free slot, or -1 if full.
|
||||
*/
|
||||
static int find_free_slot(void) {
|
||||
for (int i = 0; i < DEVICEFS_MAX_DEVICES; i++) {
|
||||
if (!devices[i].active) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* VFS operations for /dev
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Read from a device file.
|
||||
*
|
||||
* For block devices, translates byte offset/size to sector reads.
|
||||
* For character devices, delegates directly to the char read op.
|
||||
*/
|
||||
static int32_t devfs_read(vfs_node_t *node, uint32_t offset,
|
||||
uint32_t size, void *buf) {
|
||||
if (!node || !node->fs_data) return -1;
|
||||
|
||||
devicefs_device_t *dev = (devicefs_device_t *)node->fs_data;
|
||||
|
||||
if (dev->type == DEVICEFS_BLOCK && dev->block_ops) {
|
||||
uint32_t sec_size = 512;
|
||||
if (dev->block_ops->sector_size) {
|
||||
sec_size = dev->block_ops->sector_size(dev->dev_data);
|
||||
}
|
||||
if (sec_size == 0) return -1;
|
||||
|
||||
uint32_t start_lba = offset / sec_size;
|
||||
uint32_t end_byte = offset + size;
|
||||
uint32_t end_lba = (end_byte + sec_size - 1) / sec_size;
|
||||
uint32_t num_sectors = end_lba - start_lba;
|
||||
|
||||
/* For simplicity, require aligned reads for now */
|
||||
if (offset % sec_size != 0 || size % sec_size != 0) {
|
||||
/* Unaligned read: read full sectors, copy partial */
|
||||
/* TODO: implement unaligned block reads with temp buffer */
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (dev->block_ops->read_sectors) {
|
||||
int ret = dev->block_ops->read_sectors(dev->dev_data,
|
||||
start_lba,
|
||||
num_sectors,
|
||||
buf);
|
||||
return (ret == 0) ? (int32_t)size : -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (dev->type == DEVICEFS_CHAR && dev->char_ops) {
|
||||
if (dev->char_ops->read) {
|
||||
return dev->char_ops->read(dev->dev_data, size, buf);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write to a device file.
|
||||
*
|
||||
* For block devices, translates byte offset/size to sector writes.
|
||||
* For character devices, delegates directly to the char write op.
|
||||
*/
|
||||
static int32_t devfs_write(vfs_node_t *node, uint32_t offset,
|
||||
uint32_t size, const void *buf) {
|
||||
if (!node || !node->fs_data) return -1;
|
||||
|
||||
devicefs_device_t *dev = (devicefs_device_t *)node->fs_data;
|
||||
|
||||
if (dev->type == DEVICEFS_BLOCK && dev->block_ops) {
|
||||
uint32_t sec_size = 512;
|
||||
if (dev->block_ops->sector_size) {
|
||||
sec_size = dev->block_ops->sector_size(dev->dev_data);
|
||||
}
|
||||
if (sec_size == 0) return -1;
|
||||
|
||||
if (offset % sec_size != 0 || size % sec_size != 0) {
|
||||
return -1; /* Require aligned writes */
|
||||
}
|
||||
|
||||
uint32_t start_lba = offset / sec_size;
|
||||
uint32_t num_sectors = size / sec_size;
|
||||
|
||||
if (dev->block_ops->write_sectors) {
|
||||
int ret = dev->block_ops->write_sectors(dev->dev_data,
|
||||
start_lba,
|
||||
num_sectors,
|
||||
buf);
|
||||
return (ret == 0) ? (int32_t)size : -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (dev->type == DEVICEFS_CHAR && dev->char_ops) {
|
||||
if (dev->char_ops->write) {
|
||||
return dev->char_ops->write(dev->dev_data, size, buf);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a directory entry from /dev.
|
||||
* Lists all registered devices.
|
||||
*/
|
||||
static int devfs_readdir(vfs_node_t *dir, uint32_t idx, vfs_dirent_t *out) {
|
||||
(void)dir;
|
||||
|
||||
uint32_t count = 0;
|
||||
for (int i = 0; i < DEVICEFS_MAX_DEVICES; i++) {
|
||||
if (!devices[i].active) continue;
|
||||
if (count == idx) {
|
||||
memset(out, 0, sizeof(vfs_dirent_t));
|
||||
strncpy(out->name, devices[i].name, VFS_MAX_NAME - 1);
|
||||
out->inode = (uint32_t)i;
|
||||
out->type = (devices[i].type == DEVICEFS_BLOCK) ?
|
||||
VFS_BLOCKDEV : VFS_CHARDEV;
|
||||
return 0;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
return -1; /* No more entries */
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a device by name within /dev.
|
||||
*/
|
||||
static int devfs_finddir(vfs_node_t *dir, const char *name, vfs_node_t *out) {
|
||||
(void)dir;
|
||||
|
||||
for (int i = 0; i < DEVICEFS_MAX_DEVICES; i++) {
|
||||
if (!devices[i].active) continue;
|
||||
if (strcmp(devices[i].name, name) == 0) {
|
||||
memset(out, 0, sizeof(vfs_node_t));
|
||||
strncpy(out->name, devices[i].name, VFS_MAX_NAME - 1);
|
||||
out->type = (devices[i].type == DEVICEFS_BLOCK) ?
|
||||
VFS_BLOCKDEV : VFS_CHARDEV;
|
||||
out->inode = (uint32_t)i;
|
||||
out->fs_data = &devices[i];
|
||||
|
||||
/* For block devices, compute size from sector count */
|
||||
if (devices[i].type == DEVICEFS_BLOCK && devices[i].block_ops) {
|
||||
uint32_t sec_size = 512;
|
||||
uint32_t sec_count = 0;
|
||||
if (devices[i].block_ops->sector_size) {
|
||||
sec_size = devices[i].block_ops->sector_size(devices[i].dev_data);
|
||||
}
|
||||
if (devices[i].block_ops->sector_count) {
|
||||
sec_count = devices[i].block_ops->sector_count(devices[i].dev_data);
|
||||
}
|
||||
out->size = sec_count * sec_size;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/** Filesystem operations for /dev. */
|
||||
static vfs_fs_ops_t devfs_ops = {
|
||||
.open = NULL,
|
||||
.close = NULL,
|
||||
.read = devfs_read,
|
||||
.write = devfs_write,
|
||||
.readdir = devfs_readdir,
|
||||
.finddir = devfs_finddir,
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* Public API
|
||||
* ================================================================ */
|
||||
|
||||
uint32_t devicefs_next_number(const char *class_name) {
|
||||
uint32_t max_num = 0;
|
||||
for (int i = 0; i < DEVICEFS_MAX_DEVICES; i++) {
|
||||
if (!devices[i].active) continue;
|
||||
if (strcmp(devices[i].class_name, class_name) == 0) {
|
||||
if (devices[i].number >= max_num) {
|
||||
max_num = devices[i].number + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Numbers start at 1 */
|
||||
return (max_num == 0) ? 1 : max_num;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a uint32 as a decimal string into buf.
|
||||
* Returns pointer to the start of the number within buf.
|
||||
*/
|
||||
static char *uint_to_str(uint32_t val, char *buf, int buf_size) {
|
||||
buf[buf_size - 1] = '\0';
|
||||
int pos = buf_size - 2;
|
||||
if (val == 0) {
|
||||
buf[pos] = '0';
|
||||
return &buf[pos];
|
||||
}
|
||||
while (val > 0 && pos >= 0) {
|
||||
buf[pos--] = (char)('0' + (val % 10));
|
||||
val /= 10;
|
||||
}
|
||||
return &buf[pos + 1];
|
||||
}
|
||||
|
||||
devicefs_device_t *devicefs_register_block(const char *class_name,
|
||||
devicefs_block_ops_t *ops,
|
||||
void *dev_data) {
|
||||
int slot = find_free_slot();
|
||||
if (slot < 0) {
|
||||
offset_print(" DEVICEFS: no free device slots\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
devicefs_device_t *dev = &devices[slot];
|
||||
memset(dev, 0, sizeof(devicefs_device_t));
|
||||
|
||||
/* Copy class name */
|
||||
strncpy(dev->class_name, class_name, DEVICEFS_MAX_CLASS_NAME - 1);
|
||||
|
||||
/* Assign sequential number */
|
||||
dev->number = devicefs_next_number(class_name);
|
||||
|
||||
/* Build full device name: class_name + number */
|
||||
char num_buf[12];
|
||||
char *num_str = uint_to_str(dev->number, num_buf, sizeof(num_buf));
|
||||
strncpy(dev->name, class_name, DEVICEFS_MAX_DEV_NAME - 12);
|
||||
/* Append number string */
|
||||
uint32_t nlen = strlen(dev->name);
|
||||
uint32_t slen = strlen(num_str);
|
||||
if (nlen + slen < DEVICEFS_MAX_DEV_NAME) {
|
||||
memcpy(dev->name + nlen, num_str, slen + 1);
|
||||
}
|
||||
|
||||
dev->type = DEVICEFS_BLOCK;
|
||||
dev->block_ops = ops;
|
||||
dev->dev_data = dev_data;
|
||||
dev->active = 1;
|
||||
device_count++;
|
||||
|
||||
offset_print(" DEVICEFS: registered block device /dev/");
|
||||
offset_print(dev->name);
|
||||
offset_print("\n");
|
||||
|
||||
return dev;
|
||||
}
|
||||
|
||||
devicefs_device_t *devicefs_register_char(const char *class_name,
|
||||
devicefs_char_ops_t *ops,
|
||||
void *dev_data) {
|
||||
int slot = find_free_slot();
|
||||
if (slot < 0) {
|
||||
offset_print(" DEVICEFS: no free device slots\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
devicefs_device_t *dev = &devices[slot];
|
||||
memset(dev, 0, sizeof(devicefs_device_t));
|
||||
|
||||
strncpy(dev->class_name, class_name, DEVICEFS_MAX_CLASS_NAME - 1);
|
||||
dev->number = devicefs_next_number(class_name);
|
||||
|
||||
char num_buf[12];
|
||||
char *num_str = uint_to_str(dev->number, num_buf, sizeof(num_buf));
|
||||
strncpy(dev->name, class_name, DEVICEFS_MAX_DEV_NAME - 12);
|
||||
/* Append number string */
|
||||
uint32_t nlen2 = strlen(dev->name);
|
||||
uint32_t slen2 = strlen(num_str);
|
||||
if (nlen2 + slen2 < DEVICEFS_MAX_DEV_NAME) {
|
||||
memcpy(dev->name + nlen2, num_str, slen2 + 1);
|
||||
}
|
||||
|
||||
dev->type = DEVICEFS_CHAR;
|
||||
dev->char_ops = ops;
|
||||
dev->dev_data = dev_data;
|
||||
dev->active = 1;
|
||||
device_count++;
|
||||
|
||||
offset_print(" DEVICEFS: registered char device /dev/");
|
||||
offset_print(dev->name);
|
||||
offset_print("\n");
|
||||
|
||||
return dev;
|
||||
}
|
||||
|
||||
devicefs_device_t *devicefs_find(const char *name) {
|
||||
for (int i = 0; i < DEVICEFS_MAX_DEVICES; i++) {
|
||||
if (!devices[i].active) continue;
|
||||
if (strcmp(devices[i].name, name) == 0) {
|
||||
return &devices[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int init_devicefs(void) {
|
||||
memset(devices, 0, sizeof(devices));
|
||||
device_count = 0;
|
||||
|
||||
int ret = vfs_mount("/dev", &devfs_ops, NULL);
|
||||
if (ret != 0) {
|
||||
offset_print(" DEVICEFS: failed to mount at /dev\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
offset_print(" DEVICEFS: mounted at /dev\n");
|
||||
return 0;
|
||||
}
|
||||
135
src/devicefs.h
Normal file
135
src/devicefs.h
Normal file
@@ -0,0 +1,135 @@
|
||||
/**
|
||||
* @file devicefs.h
|
||||
* @brief Device filesystem (devicefs) subsystem.
|
||||
*
|
||||
* Provides a VFS interface at /dev for exposing block and character devices.
|
||||
* Drivers register devices through the devicefs API, and each device is
|
||||
* assigned a sequential number by device class (e.g., hdd1, hdd2, cd1).
|
||||
*
|
||||
* The devicefs owns device naming — drivers specify a class name (e.g., "hdd")
|
||||
* and the devicefs appends a sequential number.
|
||||
*/
|
||||
|
||||
#ifndef DEVICEFS_H
|
||||
#define DEVICEFS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/** Maximum number of registered devices. */
|
||||
#define DEVICEFS_MAX_DEVICES 32
|
||||
|
||||
/** Maximum length of a device class name (e.g., "hdd", "cd", "floppy"). */
|
||||
#define DEVICEFS_MAX_CLASS_NAME 16
|
||||
|
||||
/** Maximum length of a full device name (class + number, e.g., "hdd1"). */
|
||||
#define DEVICEFS_MAX_DEV_NAME 32
|
||||
|
||||
/** Device types. */
|
||||
#define DEVICEFS_BLOCK 0x01 /**< Block device (e.g., hard drives, CDs). */
|
||||
#define DEVICEFS_CHAR 0x02 /**< Character device (e.g., serial ports). */
|
||||
|
||||
/**
|
||||
* Block device operations.
|
||||
*
|
||||
* Block devices transfer data in fixed-size sectors.
|
||||
*/
|
||||
typedef struct devicefs_block_ops {
|
||||
/** Read `count` sectors starting at `lba` into `buf`. Returns 0 on success. */
|
||||
int (*read_sectors)(void *dev_data, uint32_t lba, uint32_t count, void *buf);
|
||||
|
||||
/** Write `count` sectors from `buf` starting at `lba`. Returns 0 on success. */
|
||||
int (*write_sectors)(void *dev_data, uint32_t lba, uint32_t count, const void *buf);
|
||||
|
||||
/** Get the sector size in bytes. */
|
||||
uint32_t (*sector_size)(void *dev_data);
|
||||
|
||||
/** Get total number of sectors. */
|
||||
uint32_t (*sector_count)(void *dev_data);
|
||||
} devicefs_block_ops_t;
|
||||
|
||||
/**
|
||||
* Character device operations.
|
||||
*
|
||||
* Character devices transfer data as byte streams.
|
||||
*/
|
||||
typedef struct devicefs_char_ops {
|
||||
/** Read up to `size` bytes into `buf`. Returns bytes read, or -1. */
|
||||
int32_t (*read)(void *dev_data, uint32_t size, void *buf);
|
||||
|
||||
/** Write `size` bytes from `buf`. Returns bytes written, or -1. */
|
||||
int32_t (*write)(void *dev_data, uint32_t size, const void *buf);
|
||||
} devicefs_char_ops_t;
|
||||
|
||||
/**
|
||||
* Registered device entry.
|
||||
*/
|
||||
typedef struct devicefs_device {
|
||||
char name[DEVICEFS_MAX_DEV_NAME]; /**< Full device name (e.g., "hdd1"). */
|
||||
char class_name[DEVICEFS_MAX_CLASS_NAME]; /**< Device class (e.g., "hdd"). */
|
||||
uint8_t type; /**< DEVICEFS_BLOCK or DEVICEFS_CHAR. */
|
||||
uint32_t number; /**< Assigned device number within class. */
|
||||
int active; /**< 1 if registered, 0 if free. */
|
||||
|
||||
/** Device-specific operations (union of block/char). */
|
||||
union {
|
||||
devicefs_block_ops_t *block_ops;
|
||||
devicefs_char_ops_t *char_ops;
|
||||
};
|
||||
|
||||
/** Opaque driver-specific data passed to operation callbacks. */
|
||||
void *dev_data;
|
||||
} devicefs_device_t;
|
||||
|
||||
/**
|
||||
* Initialize the devicefs subsystem and mount at /dev.
|
||||
*
|
||||
* @return 0 on success, -1 on failure.
|
||||
*/
|
||||
int init_devicefs(void);
|
||||
|
||||
/**
|
||||
* Register a new block device.
|
||||
*
|
||||
* The devicefs assigns a sequential number within the class. For example,
|
||||
* registering class "hdd" twice yields "hdd1" and "hdd2".
|
||||
*
|
||||
* @param class_name Device class name (e.g., "hdd", "cd").
|
||||
* @param ops Block device operations.
|
||||
* @param dev_data Opaque data passed to operation callbacks.
|
||||
* @return Pointer to the device entry, or NULL on failure.
|
||||
*/
|
||||
devicefs_device_t *devicefs_register_block(const char *class_name,
|
||||
devicefs_block_ops_t *ops,
|
||||
void *dev_data);
|
||||
|
||||
/**
|
||||
* Register a new character device.
|
||||
*
|
||||
* @param class_name Device class name (e.g., "tty", "serial").
|
||||
* @param ops Character device operations.
|
||||
* @param dev_data Opaque data passed to operation callbacks.
|
||||
* @return Pointer to the device entry, or NULL on failure.
|
||||
*/
|
||||
devicefs_device_t *devicefs_register_char(const char *class_name,
|
||||
devicefs_char_ops_t *ops,
|
||||
void *dev_data);
|
||||
|
||||
/**
|
||||
* Find a device by its full name (e.g., "hdd1").
|
||||
*
|
||||
* @param name Device name.
|
||||
* @return Pointer to the device entry, or NULL if not found.
|
||||
*/
|
||||
devicefs_device_t *devicefs_find(const char *name);
|
||||
|
||||
/**
|
||||
* Get the next device number for a given class.
|
||||
* This is called internally but may be useful for drivers.
|
||||
*
|
||||
* @param class_name Device class name.
|
||||
* @return Next sequential number (starting from 1).
|
||||
*/
|
||||
uint32_t devicefs_next_number(const char *class_name);
|
||||
|
||||
#endif /* DEVICEFS_H */
|
||||
552
src/dhcp.c
Normal file
552
src/dhcp.c
Normal file
@@ -0,0 +1,552 @@
|
||||
/**
|
||||
* @file dhcp.c
|
||||
* @brief DHCP client implementation.
|
||||
*
|
||||
* Implements the DHCP client protocol (RFC 2131) for automatic IPv4
|
||||
* address configuration. Communicates via UDP (port 68→67) over
|
||||
* Ethernet broadcasts.
|
||||
*
|
||||
* Since we don't have a full UDP stack yet, DHCP packets are
|
||||
* constructed manually with IP+UDP headers and sent via the
|
||||
* Ethernet subsystem directly.
|
||||
*
|
||||
* Status is exposed via sysfs at /sys/dhcp.
|
||||
*/
|
||||
|
||||
#include "dhcp.h"
|
||||
#include "ethernet.h"
|
||||
#include "ipv4.h"
|
||||
#include "arp.h"
|
||||
#include "sysfs.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Debug print helpers */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/* ================================================================
|
||||
* UDP header (for DHCP — minimal inline UDP)
|
||||
* ================================================================ */
|
||||
|
||||
/** UDP header structure. */
|
||||
typedef struct __attribute__((packed)) udp_header {
|
||||
uint16_t src_port;
|
||||
uint16_t dst_port;
|
||||
uint16_t length;
|
||||
uint16_t checksum;
|
||||
} udp_header_t;
|
||||
|
||||
#define UDP_HLEN 8
|
||||
|
||||
/* ================================================================
|
||||
* Global state
|
||||
* ================================================================ */
|
||||
|
||||
/** Maximum number of tracked leases (one per interface). */
|
||||
#define MAX_LEASES 8
|
||||
|
||||
/** DHCP lease table. */
|
||||
static dhcp_lease_t leases[MAX_LEASES];
|
||||
static uint32_t lease_count = 0;
|
||||
|
||||
/** Simple pseudo-random XID counter. */
|
||||
static uint32_t xid_counter = 0x12345678;
|
||||
|
||||
/* ================================================================
|
||||
* Helpers
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Compute Internet checksum.
|
||||
*/
|
||||
static uint16_t inet_checksum(const void *data, uint32_t len) {
|
||||
const uint16_t *words = (const uint16_t *)data;
|
||||
uint32_t sum = 0;
|
||||
while (len > 1) { sum += *words++; len -= 2; }
|
||||
if (len == 1) sum += *(const uint8_t *)words;
|
||||
while (sum >> 16) sum = (sum & 0xFFFF) + (sum >> 16);
|
||||
return (uint16_t)(~sum);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or create a lease for an interface.
|
||||
*/
|
||||
static dhcp_lease_t *get_lease(uint32_t iface_idx) {
|
||||
for (uint32_t i = 0; i < lease_count; i++) {
|
||||
if (leases[i].iface_idx == (uint8_t)iface_idx) return &leases[i];
|
||||
}
|
||||
if (lease_count >= MAX_LEASES) return NULL;
|
||||
dhcp_lease_t *l = &leases[lease_count++];
|
||||
memset(l, 0, sizeof(dhcp_lease_t));
|
||||
l->iface_idx = (uint8_t)iface_idx;
|
||||
return l;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format an IP as dotted decimal into buf, return chars written.
|
||||
*/
|
||||
static int fmt_ip(uint32_t ip, char *buf, uint32_t size) {
|
||||
int pos = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (i > 0 && pos < (int)size - 1) buf[pos++] = '.';
|
||||
uint8_t o = (uint8_t)((ip >> (24 - i * 8)) & 0xFF);
|
||||
if (o >= 100 && pos < (int)size - 3) {
|
||||
buf[pos++] = (char)('0' + o / 100);
|
||||
buf[pos++] = (char)('0' + (o % 100) / 10);
|
||||
buf[pos++] = (char)('0' + o % 10);
|
||||
} else if (o >= 10 && pos < (int)size - 2) {
|
||||
buf[pos++] = (char)('0' + o / 10);
|
||||
buf[pos++] = (char)('0' + o % 10);
|
||||
} else if (pos < (int)size - 1) {
|
||||
buf[pos++] = (char)('0' + o);
|
||||
}
|
||||
}
|
||||
if (pos < (int)size) buf[pos] = '\0';
|
||||
return pos;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* DHCP packet construction
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Build and send a DHCP packet wrapped in IP+UDP.
|
||||
*
|
||||
* Since we may not have a full UDP stack, we construct the whole
|
||||
* IP+UDP+DHCP frame manually and send it as a broadcast Ethernet frame.
|
||||
*/
|
||||
static int dhcp_send_packet(uint32_t iface_idx, dhcp_packet_t *dhcp_pkt,
|
||||
uint32_t dhcp_len) {
|
||||
eth_iface_t *iface = ethernet_get_iface(iface_idx);
|
||||
if (!iface) return -1;
|
||||
|
||||
/* Total sizes */
|
||||
uint32_t udp_len = UDP_HLEN + dhcp_len;
|
||||
uint32_t ip_len = IPV4_HLEN + udp_len;
|
||||
|
||||
if (ip_len > ETH_MTU) return -1;
|
||||
|
||||
/* Build combined IP+UDP+DHCP packet */
|
||||
uint8_t pkt[1500];
|
||||
memset(pkt, 0, sizeof(pkt));
|
||||
|
||||
/* IPv4 header */
|
||||
ipv4_header_t *ip = (ipv4_header_t *)pkt;
|
||||
ip->ihl_version = 0x45;
|
||||
ip->tos = 0;
|
||||
ip->total_length = htons((uint16_t)ip_len);
|
||||
ip->identification = htons(xid_counter & 0xFFFF);
|
||||
ip->flags_fragoff = 0;
|
||||
ip->ttl = 64;
|
||||
ip->protocol = IP_PROTO_UDP;
|
||||
ip->checksum = 0;
|
||||
ip->src_ip = htonl(iface->ip_addr); /* 0.0.0.0 if not yet configured */
|
||||
ip->dst_ip = htonl(0xFFFFFFFF); /* Broadcast */
|
||||
ip->checksum = inet_checksum(ip, IPV4_HLEN);
|
||||
|
||||
/* UDP header */
|
||||
udp_header_t *udp = (udp_header_t *)(pkt + IPV4_HLEN);
|
||||
udp->src_port = htons(DHCP_CLIENT_PORT);
|
||||
udp->dst_port = htons(DHCP_SERVER_PORT);
|
||||
udp->length = htons((uint16_t)udp_len);
|
||||
udp->checksum = 0; /* UDP checksum optional in IPv4 */
|
||||
|
||||
/* DHCP payload */
|
||||
memcpy(pkt + IPV4_HLEN + UDP_HLEN, dhcp_pkt, dhcp_len);
|
||||
|
||||
/* Send as broadcast Ethernet frame */
|
||||
static const uint8_t bcast[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
return ethernet_send(iface, bcast, ETHERTYPE_IPV4, pkt, ip_len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a DHCP option to the options buffer.
|
||||
* Returns new offset.
|
||||
*/
|
||||
static uint32_t add_option(uint8_t *opts, uint32_t off,
|
||||
uint8_t code, uint8_t len, const void *data) {
|
||||
opts[off++] = code;
|
||||
opts[off++] = len;
|
||||
memcpy(&opts[off], data, len);
|
||||
return off + len;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* DHCP protocol
|
||||
* ================================================================ */
|
||||
|
||||
int dhcp_discover(uint32_t iface_idx) {
|
||||
eth_iface_t *iface = ethernet_get_iface(iface_idx);
|
||||
if (!iface) return -1;
|
||||
|
||||
dhcp_lease_t *lease = get_lease(iface_idx);
|
||||
if (!lease) return -1;
|
||||
|
||||
/* Generate transaction ID */
|
||||
xid_counter += 0x1234;
|
||||
lease->xid = xid_counter;
|
||||
lease->state = DHCP_STATE_DISCOVER;
|
||||
|
||||
/* Build DHCP DISCOVER packet */
|
||||
dhcp_packet_t pkt;
|
||||
memset(&pkt, 0, sizeof(pkt));
|
||||
|
||||
pkt.op = 1; /* BOOTREQUEST */
|
||||
pkt.htype = 1; /* Ethernet */
|
||||
pkt.hlen = 6;
|
||||
pkt.hops = 0;
|
||||
pkt.xid = htonl(lease->xid);
|
||||
pkt.secs = 0;
|
||||
pkt.flags = htons(0x8000); /* Broadcast flag */
|
||||
pkt.ciaddr = 0;
|
||||
pkt.yiaddr = 0;
|
||||
pkt.siaddr = 0;
|
||||
pkt.giaddr = 0;
|
||||
memcpy(pkt.chaddr, iface->mac, 6);
|
||||
pkt.magic = htonl(DHCP_MAGIC_COOKIE);
|
||||
|
||||
/* Options */
|
||||
uint32_t off = 0;
|
||||
|
||||
/* Option 53: DHCP Message Type = DISCOVER */
|
||||
uint8_t msg_type = DHCP_DISCOVER;
|
||||
off = add_option(pkt.options, off, DHCP_OPT_MSG_TYPE, 1, &msg_type);
|
||||
|
||||
/* Option 55: Parameter Request List */
|
||||
uint8_t params[] = {
|
||||
DHCP_OPT_SUBNET_MASK,
|
||||
DHCP_OPT_ROUTER,
|
||||
DHCP_OPT_DNS,
|
||||
};
|
||||
off = add_option(pkt.options, off, DHCP_OPT_PARAM_LIST,
|
||||
sizeof(params), params);
|
||||
|
||||
/* End option */
|
||||
pkt.options[off++] = DHCP_OPT_END;
|
||||
|
||||
offset_print(" DHCP: sending DISCOVER on ");
|
||||
offset_print(iface->name);
|
||||
offset_print("\n");
|
||||
|
||||
return dhcp_send_packet(iface_idx, &pkt, DHCP_FIXED_SIZE + off);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a DHCP REQUEST message.
|
||||
*/
|
||||
static int dhcp_send_request(uint32_t iface_idx, dhcp_lease_t *lease) {
|
||||
eth_iface_t *iface = ethernet_get_iface(iface_idx);
|
||||
if (!iface) return -1;
|
||||
|
||||
dhcp_packet_t pkt;
|
||||
memset(&pkt, 0, sizeof(pkt));
|
||||
|
||||
pkt.op = 1;
|
||||
pkt.htype = 1;
|
||||
pkt.hlen = 6;
|
||||
pkt.xid = htonl(lease->xid);
|
||||
pkt.flags = htons(0x8000);
|
||||
memcpy(pkt.chaddr, iface->mac, 6);
|
||||
pkt.magic = htonl(DHCP_MAGIC_COOKIE);
|
||||
|
||||
uint32_t off = 0;
|
||||
|
||||
/* Option 53: DHCP Message Type = REQUEST */
|
||||
uint8_t msg_type = DHCP_REQUEST;
|
||||
off = add_option(pkt.options, off, DHCP_OPT_MSG_TYPE, 1, &msg_type);
|
||||
|
||||
/* Option 50: Requested IP */
|
||||
uint32_t req_ip = htonl(lease->ip_addr);
|
||||
off = add_option(pkt.options, off, DHCP_OPT_REQUESTED_IP, 4, &req_ip);
|
||||
|
||||
/* Option 54: Server Identifier */
|
||||
uint32_t srv_ip = htonl(lease->server_ip);
|
||||
off = add_option(pkt.options, off, DHCP_OPT_SERVER_ID, 4, &srv_ip);
|
||||
|
||||
/* Option 55: Parameter Request List */
|
||||
uint8_t params[] = {
|
||||
DHCP_OPT_SUBNET_MASK,
|
||||
DHCP_OPT_ROUTER,
|
||||
DHCP_OPT_DNS,
|
||||
};
|
||||
off = add_option(pkt.options, off, DHCP_OPT_PARAM_LIST,
|
||||
sizeof(params), params);
|
||||
|
||||
pkt.options[off++] = DHCP_OPT_END;
|
||||
|
||||
lease->state = DHCP_STATE_REQUESTING;
|
||||
|
||||
offset_print(" DHCP: sending REQUEST on ");
|
||||
offset_print(iface->name);
|
||||
offset_print("\n");
|
||||
|
||||
return dhcp_send_packet(iface_idx, &pkt, DHCP_FIXED_SIZE + off);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse DHCP options from a received packet.
|
||||
*/
|
||||
static void parse_options(const uint8_t *opts, uint32_t len,
|
||||
uint8_t *msg_type, dhcp_lease_t *lease) {
|
||||
uint32_t i = 0;
|
||||
while (i < len) {
|
||||
uint8_t code = opts[i++];
|
||||
if (code == DHCP_OPT_END) break;
|
||||
if (code == 0) continue; /* Padding */
|
||||
|
||||
if (i >= len) break;
|
||||
uint8_t opt_len = opts[i++];
|
||||
if (i + opt_len > len) break;
|
||||
|
||||
switch (code) {
|
||||
case DHCP_OPT_MSG_TYPE:
|
||||
if (opt_len >= 1) *msg_type = opts[i];
|
||||
break;
|
||||
case DHCP_OPT_SUBNET_MASK:
|
||||
if (opt_len >= 4) {
|
||||
uint32_t val;
|
||||
memcpy(&val, &opts[i], 4);
|
||||
lease->netmask = ntohl(val);
|
||||
}
|
||||
break;
|
||||
case DHCP_OPT_ROUTER:
|
||||
if (opt_len >= 4) {
|
||||
uint32_t val;
|
||||
memcpy(&val, &opts[i], 4);
|
||||
lease->gateway = ntohl(val);
|
||||
}
|
||||
break;
|
||||
case DHCP_OPT_DNS:
|
||||
if (opt_len >= 4) {
|
||||
uint32_t val;
|
||||
memcpy(&val, &opts[i], 4);
|
||||
lease->dns_server = ntohl(val);
|
||||
}
|
||||
break;
|
||||
case DHCP_OPT_LEASE_TIME:
|
||||
if (opt_len >= 4) {
|
||||
uint32_t val;
|
||||
memcpy(&val, &opts[i], 4);
|
||||
lease->lease_time = ntohl(val);
|
||||
}
|
||||
break;
|
||||
case DHCP_OPT_SERVER_ID:
|
||||
if (opt_len >= 4) {
|
||||
uint32_t val;
|
||||
memcpy(&val, &opts[i], 4);
|
||||
lease->server_ip = ntohl(val);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
i += opt_len;
|
||||
}
|
||||
}
|
||||
|
||||
void dhcp_receive(const void *data, uint32_t len, uint32_t iface_idx) {
|
||||
if (len < DHCP_FIXED_SIZE) return;
|
||||
|
||||
const dhcp_packet_t *pkt = (const dhcp_packet_t *)data;
|
||||
|
||||
/* Must be a BOOTREPLY */
|
||||
if (pkt->op != 2) return;
|
||||
|
||||
/* Check magic cookie */
|
||||
if (ntohl(pkt->magic) != DHCP_MAGIC_COOKIE) return;
|
||||
|
||||
dhcp_lease_t *lease = get_lease(iface_idx);
|
||||
if (!lease) return;
|
||||
|
||||
/* Match transaction ID */
|
||||
if (ntohl(pkt->xid) != lease->xid) return;
|
||||
|
||||
/* Parse options */
|
||||
uint8_t msg_type = 0;
|
||||
uint32_t opts_len = len - DHCP_FIXED_SIZE;
|
||||
if (opts_len > sizeof(pkt->options)) opts_len = sizeof(pkt->options);
|
||||
parse_options(pkt->options, opts_len, &msg_type, lease);
|
||||
|
||||
/* Store offered IP */
|
||||
uint32_t offered_ip = ntohl(pkt->yiaddr);
|
||||
|
||||
switch (msg_type) {
|
||||
case DHCP_OFFER:
|
||||
if (lease->state != DHCP_STATE_DISCOVER) break;
|
||||
lease->ip_addr = offered_ip;
|
||||
offset_print(" DHCP: received OFFER ");
|
||||
print_hex(offered_ip);
|
||||
offset_print("\n");
|
||||
/* Send REQUEST */
|
||||
dhcp_send_request(iface_idx, lease);
|
||||
break;
|
||||
|
||||
case DHCP_ACK:
|
||||
if (lease->state != DHCP_STATE_REQUESTING) break;
|
||||
lease->ip_addr = offered_ip;
|
||||
lease->state = DHCP_STATE_BOUND;
|
||||
|
||||
/* Apply configuration to the interface */
|
||||
{
|
||||
eth_iface_t *iface = ethernet_get_iface(iface_idx);
|
||||
if (iface) {
|
||||
iface->ip_addr = lease->ip_addr;
|
||||
iface->netmask = lease->netmask;
|
||||
iface->gateway = lease->gateway;
|
||||
}
|
||||
}
|
||||
|
||||
offset_print(" DHCP: BOUND on ");
|
||||
{
|
||||
eth_iface_t *iface2 = ethernet_get_iface(iface_idx);
|
||||
if (iface2) offset_print(iface2->name);
|
||||
}
|
||||
offset_print(" IP ");
|
||||
print_hex(lease->ip_addr);
|
||||
offset_print("\n");
|
||||
break;
|
||||
|
||||
case DHCP_NAK:
|
||||
lease->state = DHCP_STATE_FAILED;
|
||||
offset_print(" DHCP: received NAK\n");
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const dhcp_lease_t *dhcp_get_lease(uint32_t iface_idx) {
|
||||
for (uint32_t i = 0; i < lease_count; i++) {
|
||||
if (leases[i].iface_idx == (uint8_t)iface_idx) return &leases[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char *dhcp_state_name(uint8_t state) {
|
||||
switch (state) {
|
||||
case DHCP_STATE_IDLE: return "idle";
|
||||
case DHCP_STATE_DISCOVER: return "discovering";
|
||||
case DHCP_STATE_REQUESTING: return "requesting";
|
||||
case DHCP_STATE_BOUND: return "bound";
|
||||
case DHCP_STATE_RENEWING: return "renewing";
|
||||
case DHCP_STATE_FAILED: return "failed";
|
||||
default: return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Sysfs: /sys/dhcp
|
||||
*
|
||||
* /sys/dhcp/
|
||||
* status - overall DHCP status
|
||||
* ================================================================ */
|
||||
|
||||
static void append(char *buf, uint32_t size, int *pos, const char *s) {
|
||||
while (*s && *pos < (int)size - 1) buf[(*pos)++] = *s++;
|
||||
}
|
||||
|
||||
static int dhcp_sysfs_list(void *ctx, const char *path, uint32_t idx,
|
||||
sysfs_entry_t *out) {
|
||||
(void)ctx;
|
||||
|
||||
if (path[0] == '\0') {
|
||||
if (idx == 0) {
|
||||
memset(out, 0, sizeof(sysfs_entry_t));
|
||||
strncpy(out->name, "status", SYSFS_MAX_NAME - 1);
|
||||
out->is_dir = 0;
|
||||
return 0;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int dhcp_sysfs_read(void *ctx, const char *path, char *buf,
|
||||
uint32_t buf_size) {
|
||||
(void)ctx;
|
||||
|
||||
if (strcmp(path, "status") != 0) return -1;
|
||||
|
||||
int pos = 0;
|
||||
|
||||
if (lease_count == 0) {
|
||||
append(buf, buf_size, &pos, "No DHCP leases.\n");
|
||||
buf[pos] = '\0';
|
||||
return pos;
|
||||
}
|
||||
|
||||
for (uint32_t i = 0; i < lease_count; i++) {
|
||||
dhcp_lease_t *l = &leases[i];
|
||||
eth_iface_t *iface = ethernet_get_iface(l->iface_idx);
|
||||
|
||||
if (iface) {
|
||||
append(buf, buf_size, &pos, iface->name);
|
||||
} else {
|
||||
append(buf, buf_size, &pos, "?");
|
||||
}
|
||||
append(buf, buf_size, &pos, ": ");
|
||||
append(buf, buf_size, &pos, dhcp_state_name(l->state));
|
||||
append(buf, buf_size, &pos, "\n");
|
||||
|
||||
if (l->state == DHCP_STATE_BOUND) {
|
||||
char ip_buf[16];
|
||||
|
||||
append(buf, buf_size, &pos, " IP: ");
|
||||
fmt_ip(l->ip_addr, ip_buf, sizeof(ip_buf));
|
||||
append(buf, buf_size, &pos, ip_buf);
|
||||
append(buf, buf_size, &pos, "\n");
|
||||
|
||||
append(buf, buf_size, &pos, " Netmask: ");
|
||||
fmt_ip(l->netmask, ip_buf, sizeof(ip_buf));
|
||||
append(buf, buf_size, &pos, ip_buf);
|
||||
append(buf, buf_size, &pos, "\n");
|
||||
|
||||
append(buf, buf_size, &pos, " Gateway: ");
|
||||
fmt_ip(l->gateway, ip_buf, sizeof(ip_buf));
|
||||
append(buf, buf_size, &pos, ip_buf);
|
||||
append(buf, buf_size, &pos, "\n");
|
||||
|
||||
append(buf, buf_size, &pos, " DNS: ");
|
||||
fmt_ip(l->dns_server, ip_buf, sizeof(ip_buf));
|
||||
append(buf, buf_size, &pos, ip_buf);
|
||||
append(buf, buf_size, &pos, "\n");
|
||||
|
||||
append(buf, buf_size, &pos, " Server: ");
|
||||
fmt_ip(l->server_ip, ip_buf, sizeof(ip_buf));
|
||||
append(buf, buf_size, &pos, ip_buf);
|
||||
append(buf, buf_size, &pos, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
buf[pos] = '\0';
|
||||
return pos;
|
||||
}
|
||||
|
||||
static int dhcp_sysfs_write(void *ctx, const char *path, const char *buf,
|
||||
uint32_t size) {
|
||||
(void)ctx;
|
||||
(void)path;
|
||||
(void)buf;
|
||||
(void)size;
|
||||
return -1;
|
||||
}
|
||||
|
||||
static sysfs_ops_t dhcp_sysfs_ops = {
|
||||
.list = dhcp_sysfs_list,
|
||||
.read = dhcp_sysfs_read,
|
||||
.write = dhcp_sysfs_write,
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* Initialization
|
||||
* ================================================================ */
|
||||
|
||||
void dhcp_init(void) {
|
||||
memset(leases, 0, sizeof(leases));
|
||||
lease_count = 0;
|
||||
|
||||
sysfs_register("dhcp", &dhcp_sysfs_ops, NULL);
|
||||
|
||||
offset_print(" DHCP: initialized\n");
|
||||
}
|
||||
145
src/dhcp.h
Normal file
145
src/dhcp.h
Normal file
@@ -0,0 +1,145 @@
|
||||
/**
|
||||
* @file dhcp.h
|
||||
* @brief DHCP (Dynamic Host Configuration Protocol) client subsystem.
|
||||
*
|
||||
* Implements a minimal DHCP client (RFC 2131) that can obtain an IPv4
|
||||
* address, subnet mask, gateway, and DNS server from a DHCP server.
|
||||
*
|
||||
* DHCP operates over UDP: client port 68, server port 67.
|
||||
* Uses the Ethernet subsystem for broadcast communication.
|
||||
*
|
||||
* Status information is exposed via sysfs at /sys/dhcp.
|
||||
*/
|
||||
|
||||
#ifndef DHCP_H
|
||||
#define DHCP_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/** DHCP ports. */
|
||||
#define DHCP_CLIENT_PORT 68
|
||||
#define DHCP_SERVER_PORT 67
|
||||
|
||||
/** DHCP message types. */
|
||||
#define DHCP_DISCOVER 1
|
||||
#define DHCP_OFFER 2
|
||||
#define DHCP_REQUEST 3
|
||||
#define DHCP_DECLINE 4
|
||||
#define DHCP_ACK 5
|
||||
#define DHCP_NAK 6
|
||||
#define DHCP_RELEASE 7
|
||||
|
||||
/** DHCP options. */
|
||||
#define DHCP_OPT_SUBNET_MASK 1
|
||||
#define DHCP_OPT_ROUTER 3
|
||||
#define DHCP_OPT_DNS 6
|
||||
#define DHCP_OPT_HOSTNAME 12
|
||||
#define DHCP_OPT_REQUESTED_IP 50
|
||||
#define DHCP_OPT_LEASE_TIME 51
|
||||
#define DHCP_OPT_MSG_TYPE 53
|
||||
#define DHCP_OPT_SERVER_ID 54
|
||||
#define DHCP_OPT_PARAM_LIST 55
|
||||
#define DHCP_OPT_END 255
|
||||
|
||||
/** DHCP magic cookie. */
|
||||
#define DHCP_MAGIC_COOKIE 0x63825363
|
||||
|
||||
/** DHCP client states. */
|
||||
#define DHCP_STATE_IDLE 0
|
||||
#define DHCP_STATE_DISCOVER 1
|
||||
#define DHCP_STATE_REQUESTING 2
|
||||
#define DHCP_STATE_BOUND 3
|
||||
#define DHCP_STATE_RENEWING 4
|
||||
#define DHCP_STATE_FAILED 5
|
||||
|
||||
/* ================================================================
|
||||
* DHCP packet structure (548 bytes minimum)
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* DHCP message (over UDP, 236 bytes fixed + options).
|
||||
*/
|
||||
typedef struct __attribute__((packed)) dhcp_packet {
|
||||
uint8_t op; /**< Message op: 1=BOOTREQUEST, 2=BOOTREPLY. */
|
||||
uint8_t htype; /**< Hardware type: 1=Ethernet. */
|
||||
uint8_t hlen; /**< Hardware address length: 6. */
|
||||
uint8_t hops; /**< Hops: 0. */
|
||||
uint32_t xid; /**< Transaction ID. */
|
||||
uint16_t secs; /**< Seconds elapsed. */
|
||||
uint16_t flags; /**< Flags (0x8000 = broadcast). */
|
||||
uint32_t ciaddr; /**< Client IP (if bound). */
|
||||
uint32_t yiaddr; /**< 'Your' (client) IP address. */
|
||||
uint32_t siaddr; /**< Server IP address. */
|
||||
uint32_t giaddr; /**< Gateway IP address. */
|
||||
uint8_t chaddr[16]; /**< Client hardware address. */
|
||||
uint8_t sname[64]; /**< Server host name (unused). */
|
||||
uint8_t file[128]; /**< Boot file name (unused). */
|
||||
uint32_t magic; /**< Magic cookie (0x63825363). */
|
||||
uint8_t options[312]; /**< DHCP options. */
|
||||
} dhcp_packet_t;
|
||||
|
||||
/** Fixed part of DHCP packet (before options). */
|
||||
#define DHCP_FIXED_SIZE 240
|
||||
|
||||
/* ================================================================
|
||||
* DHCP lease information
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Information obtained from a DHCP lease.
|
||||
*/
|
||||
typedef struct dhcp_lease {
|
||||
uint32_t ip_addr; /**< Assigned IP address (host byte order). */
|
||||
uint32_t netmask; /**< Subnet mask (host byte order). */
|
||||
uint32_t gateway; /**< Default gateway (host byte order). */
|
||||
uint32_t dns_server; /**< DNS server (host byte order). */
|
||||
uint32_t server_ip; /**< DHCP server IP (host byte order). */
|
||||
uint32_t lease_time; /**< Lease time in seconds. */
|
||||
uint32_t xid; /**< Transaction ID used. */
|
||||
uint8_t state; /**< Current DHCP state. */
|
||||
uint8_t iface_idx; /**< Ethernet interface. */
|
||||
} dhcp_lease_t;
|
||||
|
||||
/* ================================================================
|
||||
* Public API
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Initialize the DHCP subsystem.
|
||||
*/
|
||||
void dhcp_init(void);
|
||||
|
||||
/**
|
||||
* Start a DHCP discover/request sequence on an interface.
|
||||
*
|
||||
* @param iface_idx Ethernet interface index.
|
||||
* @return 0 on success (discover sent), -1 on failure.
|
||||
*/
|
||||
int dhcp_discover(uint32_t iface_idx);
|
||||
|
||||
/**
|
||||
* Process an incoming DHCP (UDP) packet.
|
||||
*
|
||||
* @param data Raw DHCP packet.
|
||||
* @param len Packet length.
|
||||
* @param iface_idx Interface index.
|
||||
*/
|
||||
void dhcp_receive(const void *data, uint32_t len, uint32_t iface_idx);
|
||||
|
||||
/**
|
||||
* Get the current DHCP lease for an interface.
|
||||
*
|
||||
* @param iface_idx Interface index.
|
||||
* @return Pointer to lease info, or NULL.
|
||||
*/
|
||||
const dhcp_lease_t *dhcp_get_lease(uint32_t iface_idx);
|
||||
|
||||
/**
|
||||
* Get the DHCP state name as a string.
|
||||
*
|
||||
* @param state DHCP_STATE_* constant.
|
||||
* @return Human-readable state name.
|
||||
*/
|
||||
const char *dhcp_state_name(uint8_t state);
|
||||
|
||||
#endif /* DHCP_H */
|
||||
72
src/driver.c
Normal file
72
src/driver.c
Normal file
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* @file driver.c
|
||||
* @brief Driver framework implementation.
|
||||
*
|
||||
* Iterates over all driver descriptors placed in the .drivers linker section
|
||||
* by the REGISTER_DRIVER macro. Each driver is probed and, if the probe
|
||||
* succeeds, initialized.
|
||||
*/
|
||||
|
||||
#include "driver.h"
|
||||
#include "port_io.h"
|
||||
#include <stddef.h>
|
||||
|
||||
/* Debug print helpers defined in kernel.c */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/**
|
||||
* Linker-provided symbols marking the start and end of the .drivers section.
|
||||
* Each entry is a pointer to a driver_t.
|
||||
*/
|
||||
extern const driver_t *__drivers_start[];
|
||||
extern const driver_t *__drivers_end[];
|
||||
|
||||
void init_drivers(void) {
|
||||
const driver_t **drv;
|
||||
int loaded = 0;
|
||||
int skipped = 0;
|
||||
int failed = 0;
|
||||
|
||||
offset_print(" DRIVERS: scanning registered drivers...\n");
|
||||
|
||||
for (drv = __drivers_start; drv < __drivers_end; drv++) {
|
||||
const driver_t *d = *drv;
|
||||
if (!d || !d->name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
offset_print(" DRIVERS: probing ");
|
||||
offset_print(d->name);
|
||||
offset_print("... ");
|
||||
|
||||
/* Run probe function if provided */
|
||||
if (d->probe) {
|
||||
driver_probe_result_t result = d->probe();
|
||||
if (result == DRIVER_PROBE_NOT_FOUND) {
|
||||
offset_print("not found\n");
|
||||
skipped++;
|
||||
continue;
|
||||
} else if (result == DRIVER_PROBE_ERROR) {
|
||||
offset_print("probe error\n");
|
||||
failed++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
/* Run init function */
|
||||
if (d->init) {
|
||||
int ret = d->init();
|
||||
if (ret != 0) {
|
||||
offset_print("init failed\n");
|
||||
failed++;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
offset_print("loaded\n");
|
||||
loaded++;
|
||||
}
|
||||
|
||||
offset_print(" DRIVERS: done\n");
|
||||
}
|
||||
56
src/driver.h
Normal file
56
src/driver.h
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* @file driver.h
|
||||
* @brief Kernel driver architecture.
|
||||
*
|
||||
* Provides a simple framework for registering and initializing kernel drivers.
|
||||
* Each driver provides a probe function that returns whether it should load,
|
||||
* and an init function that performs the actual initialization.
|
||||
*
|
||||
* Drivers are registered at compile time using the REGISTER_DRIVER macro,
|
||||
* which places driver descriptors in a special linker section. The kernel
|
||||
* iterates over all registered drivers during boot.
|
||||
*/
|
||||
|
||||
#ifndef DRIVER_H
|
||||
#define DRIVER_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/** Driver probe result codes. */
|
||||
typedef enum {
|
||||
DRIVER_PROBE_OK = 0, /**< Driver should be loaded. */
|
||||
DRIVER_PROBE_NOT_FOUND = 1, /**< Hardware not found, skip this driver. */
|
||||
DRIVER_PROBE_ERROR = 2 /**< Error during probing. */
|
||||
} driver_probe_result_t;
|
||||
|
||||
/**
|
||||
* Driver descriptor structure.
|
||||
*
|
||||
* Each driver provides a name, a probe function (to test if hardware is
|
||||
* present), and an init function (to set up the driver).
|
||||
*/
|
||||
typedef struct driver {
|
||||
const char *name; /**< Human-readable driver name. */
|
||||
driver_probe_result_t (*probe)(void); /**< Probe function. Returns DRIVER_PROBE_OK if driver should load. */
|
||||
int (*init)(void); /**< Init function. Returns 0 on success, non-zero on failure. */
|
||||
} driver_t;
|
||||
|
||||
/**
|
||||
* Register a driver.
|
||||
*
|
||||
* Places the driver descriptor in the .drivers linker section so it
|
||||
* is automatically discovered during boot.
|
||||
*/
|
||||
#define REGISTER_DRIVER(drv) \
|
||||
static const driver_t * __attribute__((used, section(".drivers"))) \
|
||||
_driver_##drv = &(drv)
|
||||
|
||||
/**
|
||||
* Initialize all registered drivers.
|
||||
*
|
||||
* Iterates over the .drivers section, probes each driver, and initializes
|
||||
* those that respond positively to probing.
|
||||
*/
|
||||
void init_drivers(void);
|
||||
|
||||
#endif /* DRIVER_H */
|
||||
409
src/e3c509.c
Normal file
409
src/e3c509.c
Normal file
@@ -0,0 +1,409 @@
|
||||
/**
|
||||
* @file e3c509.c
|
||||
* @brief 3Com 3C509B (EtherLink III) ISA Ethernet NIC driver.
|
||||
*
|
||||
* Drives 3Com 3C509/3C509B Ethernet adapters using PIO (programmed I/O).
|
||||
* The 3C509B uses a windowed register model: 8 register windows of 16 I/O
|
||||
* ports each, selected by writing to the command register.
|
||||
*
|
||||
* Only 10base-T (RJ45) operation is supported, per design requirements.
|
||||
*
|
||||
* Packet TX: write packet length, then write data to TX PIO port.
|
||||
* Packet RX: poll RX status, read data from RX PIO port.
|
||||
*/
|
||||
|
||||
#include "e3c509.h"
|
||||
#include "port_io.h"
|
||||
#include "pic.h"
|
||||
#include "devicefs.h"
|
||||
#include "ethernet.h"
|
||||
#include "driver.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Debug print helpers */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/* ================================================================
|
||||
* Global state
|
||||
* ================================================================ */
|
||||
|
||||
/** Single 3C509B device. */
|
||||
static e3c509_device_t e3c509_dev;
|
||||
|
||||
/** Volatile flags set by IRQ handler. */
|
||||
static volatile int e3c509_rx_ready = 0;
|
||||
static volatile int e3c509_tx_done = 0;
|
||||
|
||||
/* ================================================================
|
||||
* Register access helpers
|
||||
* ================================================================ */
|
||||
|
||||
/** Write a 16-bit command to the 3C509B command register. */
|
||||
static inline void e3c509_cmd(uint16_t base, uint16_t cmd) {
|
||||
outw(base + E3C509_CMD, cmd);
|
||||
}
|
||||
|
||||
/** Read the 16-bit status register. */
|
||||
static inline uint16_t e3c509_status(uint16_t base) {
|
||||
return inw(base + E3C509_STATUS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Select a register window.
|
||||
*/
|
||||
static void e3c509_select_window(uint16_t base, uint16_t window) {
|
||||
e3c509_cmd(base, CMD_SELECT_WINDOW | (window & 0x07));
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for a command to complete.
|
||||
*/
|
||||
static void e3c509_wait_cmd(uint16_t base) {
|
||||
int timeout = 100000;
|
||||
while ((e3c509_status(base) & STAT_CMD_IN_PROG) && --timeout > 0);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Packet send / receive
|
||||
* ================================================================ */
|
||||
|
||||
int e3c509_send(e3c509_device_t *dev, const void *data, uint32_t len) {
|
||||
if (!dev || !dev->present) return -1;
|
||||
if (len > ETH_FRAME_MAX) return -1;
|
||||
|
||||
uint16_t base = dev->io_base;
|
||||
uint32_t send_len = len;
|
||||
if (send_len < 60) send_len = 60;
|
||||
|
||||
/* Switch to window 1 */
|
||||
e3c509_select_window(base, 1);
|
||||
|
||||
/* Wait for free TX space */
|
||||
int timeout = 100000;
|
||||
while (inw(base + E3C509_W1_FREE_TX) < send_len + 4 && --timeout > 0) {
|
||||
asm volatile("pause");
|
||||
}
|
||||
if (timeout == 0) {
|
||||
offset_print(" 3C509: tx fifo full\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Write TX preamble: packet length (low 11 bits, bit 15 = no interrupt) */
|
||||
outw(base + E3C509_W1_TX_PIO, (uint16_t)(send_len | 0x00));
|
||||
|
||||
/* Pad to dword-aligned length */
|
||||
outw(base + E3C509_W1_TX_PIO, 0x0000);
|
||||
|
||||
/* Write packet data as 16-bit words */
|
||||
const uint16_t *data16 = (const uint16_t *)data;
|
||||
uint32_t words = len / 2;
|
||||
for (uint32_t i = 0; i < words; i++) {
|
||||
outw(base + E3C509_W1_TX_PIO, data16[i]);
|
||||
}
|
||||
/* Write last byte if odd */
|
||||
if (len & 1) {
|
||||
const uint8_t *data8 = (const uint8_t *)data;
|
||||
outw(base + E3C509_W1_TX_PIO, (uint16_t)data8[len - 1]);
|
||||
}
|
||||
|
||||
/* Pad to minimum frame size with zeros */
|
||||
if (len < 60) {
|
||||
uint32_t pad_words = (60 - len + 1) / 2;
|
||||
for (uint32_t i = 0; i < pad_words; i++) {
|
||||
outw(base + E3C509_W1_TX_PIO, 0x0000);
|
||||
}
|
||||
}
|
||||
|
||||
/* Wait for TX complete */
|
||||
e3c509_tx_done = 0;
|
||||
timeout = 1000000;
|
||||
while (!e3c509_tx_done && --timeout > 0) {
|
||||
/* Check TX status directly in case interrupts are slow */
|
||||
uint8_t txstat = inb(base + E3C509_W1_TX_STATUS);
|
||||
if (txstat & 0x80) { /* TX complete */
|
||||
/* Acknowledge by writing status back */
|
||||
outb(base + E3C509_W1_TX_STATUS, txstat);
|
||||
break;
|
||||
}
|
||||
asm volatile("pause");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int e3c509_recv(e3c509_device_t *dev, void *buf, uint32_t bufsize) {
|
||||
if (!dev || !dev->present) return -1;
|
||||
|
||||
uint16_t base = dev->io_base;
|
||||
|
||||
/* Switch to window 1 */
|
||||
e3c509_select_window(base, 1);
|
||||
|
||||
/* Read RX status */
|
||||
uint16_t rx_status = inw(base + E3C509_W1_RX_STATUS);
|
||||
|
||||
/* Check if a packet is available (bit 15 = incomplete/error) */
|
||||
if (rx_status & 0x8000) {
|
||||
/* Error — discard the packet */
|
||||
e3c509_cmd(base, CMD_RX_DISCARD);
|
||||
e3c509_wait_cmd(base);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Bits 10:0 = packet length */
|
||||
uint16_t pkt_len = rx_status & 0x07FF;
|
||||
if (pkt_len == 0) {
|
||||
return 0; /* No packet */
|
||||
}
|
||||
|
||||
uint32_t copy_len = pkt_len;
|
||||
if (copy_len > bufsize) copy_len = bufsize;
|
||||
|
||||
/* Read packet data as 16-bit words */
|
||||
uint16_t *buf16 = (uint16_t *)buf;
|
||||
uint32_t read_words = copy_len / 2;
|
||||
for (uint32_t i = 0; i < read_words; i++) {
|
||||
buf16[i] = inw(base + E3C509_W1_RX_PIO);
|
||||
}
|
||||
if (copy_len & 1) {
|
||||
uint16_t w = inw(base + E3C509_W1_RX_PIO);
|
||||
((uint8_t *)buf)[copy_len - 1] = (uint8_t)(w & 0xFF);
|
||||
}
|
||||
|
||||
/* Discard any remaining data and advance */
|
||||
e3c509_cmd(base, CMD_RX_DISCARD);
|
||||
e3c509_wait_cmd(base);
|
||||
|
||||
return (int)copy_len;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* IRQ handler
|
||||
* ================================================================ */
|
||||
|
||||
void e3c509_irq(void) {
|
||||
if (!e3c509_dev.present) return;
|
||||
|
||||
uint16_t base = e3c509_dev.io_base;
|
||||
uint16_t status = e3c509_status(base);
|
||||
|
||||
if (status & STAT_RX_COMPLETE) {
|
||||
e3c509_rx_ready = 1;
|
||||
e3c509_cmd(base, CMD_ACK_INTR | STAT_RX_COMPLETE);
|
||||
}
|
||||
|
||||
if (status & STAT_TX_COMPLETE) {
|
||||
e3c509_tx_done = 1;
|
||||
/* Read and clear TX status */
|
||||
e3c509_select_window(base, 1);
|
||||
uint8_t txstat = inb(base + E3C509_W1_TX_STATUS);
|
||||
outb(base + E3C509_W1_TX_STATUS, txstat);
|
||||
e3c509_cmd(base, CMD_ACK_INTR | STAT_TX_COMPLETE);
|
||||
}
|
||||
|
||||
if (status & STAT_TX_AVAILABLE) {
|
||||
e3c509_cmd(base, CMD_ACK_INTR | STAT_TX_AVAILABLE);
|
||||
}
|
||||
|
||||
if (status & STAT_ADAPTER_FAIL) {
|
||||
e3c509_cmd(base, CMD_ACK_INTR | STAT_ADAPTER_FAIL);
|
||||
}
|
||||
|
||||
if (status & STAT_UPDATE_STATS) {
|
||||
e3c509_cmd(base, CMD_ACK_INTR | STAT_UPDATE_STATS);
|
||||
}
|
||||
|
||||
/* Acknowledge the interrupt latch */
|
||||
e3c509_cmd(base, CMD_ACK_INTR | STAT_INT_LATCH);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Initialization
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Read the MAC address from the 3C509B's EEPROM (Window 2).
|
||||
*/
|
||||
static void e3c509_read_mac(uint16_t base, uint8_t *mac) {
|
||||
e3c509_select_window(base, 2);
|
||||
uint16_t w0 = inw(base + E3C509_W2_ADDR0);
|
||||
uint16_t w1 = inw(base + E3C509_W2_ADDR1);
|
||||
uint16_t w2 = inw(base + E3C509_W2_ADDR2);
|
||||
|
||||
mac[0] = (uint8_t)(w0 & 0xFF);
|
||||
mac[1] = (uint8_t)((w0 >> 8) & 0xFF);
|
||||
mac[2] = (uint8_t)(w1 & 0xFF);
|
||||
mac[3] = (uint8_t)((w1 >> 8) & 0xFF);
|
||||
mac[4] = (uint8_t)(w2 & 0xFF);
|
||||
mac[5] = (uint8_t)((w2 >> 8) & 0xFF);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the 3C509B hardware.
|
||||
*/
|
||||
static int e3c509_hw_init(uint16_t base) {
|
||||
/* Global reset */
|
||||
e3c509_cmd(base, CMD_GLOBAL_RESET);
|
||||
|
||||
/* Wait for reset to complete */
|
||||
int timeout = 100000;
|
||||
while ((e3c509_status(base) & STAT_CMD_IN_PROG) && --timeout > 0);
|
||||
if (timeout == 0) {
|
||||
offset_print(" 3C509: reset timeout\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Small extra delay */
|
||||
for (volatile int i = 0; i < 50000; i++) {
|
||||
asm volatile("pause");
|
||||
}
|
||||
|
||||
/* Read MAC address */
|
||||
e3c509_read_mac(base, e3c509_dev.mac);
|
||||
|
||||
/* Select 10base-T (RJ45) transceiver — Window 0, Address Config */
|
||||
e3c509_select_window(base, 0);
|
||||
uint16_t addr_cfg = inw(base + E3C509_W0_ADDR_CFG);
|
||||
/* Clear transceiver bits (14:13) and set to TP (00) */
|
||||
addr_cfg &= ~(0x3 << 14);
|
||||
addr_cfg |= (XCVR_TP << 14);
|
||||
outw(base + E3C509_W0_ADDR_CFG, addr_cfg);
|
||||
|
||||
/* Configure IRQ in resource config register */
|
||||
uint16_t res_cfg = inw(base + E3C509_W0_RES_CFG);
|
||||
/* IRQ is in bits 15:12. Set to our IRQ. */
|
||||
res_cfg = (res_cfg & 0x0FFF) | ((uint16_t)e3c509_dev.irq << 12);
|
||||
outw(base + E3C509_W0_RES_CFG, res_cfg);
|
||||
|
||||
/* Enable the adapter */
|
||||
outw(base + E3C509_W0_CFG_CTRL, 0x0001); /* Enable */
|
||||
|
||||
/* Reset TX and RX */
|
||||
e3c509_cmd(base, CMD_TX_RESET);
|
||||
e3c509_wait_cmd(base);
|
||||
e3c509_cmd(base, CMD_RX_RESET);
|
||||
e3c509_wait_cmd(base);
|
||||
|
||||
/* Set RX filter: accept station + broadcast */
|
||||
e3c509_cmd(base, CMD_SET_RX_FILTER | RX_FILTER_STATION | RX_FILTER_BCAST);
|
||||
|
||||
/* Set TX start threshold — start transmitting after full packet */
|
||||
e3c509_cmd(base, CMD_SET_TX_START | (ETH_FRAME_MAX >> 2));
|
||||
|
||||
/* Set interrupt mask */
|
||||
e3c509_cmd(base, CMD_SET_INTR_MASK |
|
||||
STAT_RX_COMPLETE | STAT_TX_COMPLETE | STAT_TX_AVAILABLE |
|
||||
STAT_ADAPTER_FAIL | STAT_UPDATE_STATS);
|
||||
|
||||
/* Enable TX and RX */
|
||||
e3c509_cmd(base, CMD_TX_ENABLE);
|
||||
e3c509_cmd(base, CMD_RX_ENABLE);
|
||||
|
||||
/* Acknowledge any pending interrupts */
|
||||
e3c509_cmd(base, CMD_ACK_INTR | 0xFF);
|
||||
|
||||
/* Switch to operating window (window 1) */
|
||||
e3c509_select_window(base, 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Driver framework
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Probe for a 3C509B card.
|
||||
*
|
||||
* Check the manufacturer ID at Window 0, offset 0x00.
|
||||
* The 3Com 3C509B should return 0x6D50.
|
||||
*/
|
||||
static driver_probe_result_t e3c509_probe(void) {
|
||||
uint16_t base = E3C509_DEFAULT_IOBASE;
|
||||
|
||||
/* Try to select Window 0 and read manufacturer ID */
|
||||
e3c509_cmd(base, CMD_SELECT_WINDOW | 0);
|
||||
|
||||
/* Brief delay */
|
||||
for (volatile int i = 0; i < 10000; i++) {
|
||||
asm volatile("pause");
|
||||
}
|
||||
|
||||
uint16_t mfg_id = inw(base + E3C509_W0_MFG_ID);
|
||||
|
||||
/* 3Com manufacturer ID = 0x6D50 */
|
||||
if (mfg_id == 0x6D50) {
|
||||
return DRIVER_PROBE_OK;
|
||||
}
|
||||
|
||||
/* Also check for the product ID being in a reasonable range */
|
||||
if ((mfg_id & 0xFF00) == 0x9000 || (mfg_id & 0xFF00) == 0x9100) {
|
||||
return DRIVER_PROBE_OK;
|
||||
}
|
||||
|
||||
return DRIVER_PROBE_NOT_FOUND;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the 3C509B driver.
|
||||
*/
|
||||
static int e3c509_driver_init(void) {
|
||||
memset(&e3c509_dev, 0, sizeof(e3c509_dev));
|
||||
|
||||
e3c509_dev.io_base = E3C509_DEFAULT_IOBASE;
|
||||
e3c509_dev.irq = E3C509_DEFAULT_IRQ;
|
||||
|
||||
/* Unmask IRQ */
|
||||
pic_clear_mask(e3c509_dev.irq);
|
||||
|
||||
/* Initialize hardware */
|
||||
if (e3c509_hw_init(e3c509_dev.io_base) != 0) {
|
||||
offset_print(" 3C509: initialization failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
e3c509_dev.present = 1;
|
||||
|
||||
/* Print MAC address */
|
||||
offset_print(" 3C509: MAC ");
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if (i > 0) offset_print(":");
|
||||
print_hex(e3c509_dev.mac[i]);
|
||||
}
|
||||
offset_print("\n");
|
||||
|
||||
/* Register with ethernet subsystem (creates /dev/ethN) */
|
||||
ethernet_register(e3c509_dev.mac,
|
||||
(eth_send_fn)e3c509_send,
|
||||
(eth_recv_fn)e3c509_recv,
|
||||
&e3c509_dev);
|
||||
|
||||
offset_print(" 3C509: 10base-T (RJ45) on I/O ");
|
||||
print_hex(e3c509_dev.io_base);
|
||||
offset_print(" IRQ ");
|
||||
print_hex(e3c509_dev.irq);
|
||||
offset_print("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
e3c509_device_t *e3c509_get_device(void) {
|
||||
return e3c509_dev.present ? &e3c509_dev : NULL;
|
||||
}
|
||||
|
||||
int e3c509_init(void) {
|
||||
return e3c509_driver_init();
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Driver registration
|
||||
* ================================================================ */
|
||||
|
||||
static const driver_t e3c509_driver = {
|
||||
.name = "3c509b",
|
||||
.probe = e3c509_probe,
|
||||
.init = e3c509_driver_init,
|
||||
};
|
||||
|
||||
REGISTER_DRIVER(e3c509_driver);
|
||||
158
src/e3c509.h
Normal file
158
src/e3c509.h
Normal file
@@ -0,0 +1,158 @@
|
||||
/**
|
||||
* @file e3c509.h
|
||||
* @brief 3Com 3C509B (EtherLink III) ISA Ethernet NIC driver.
|
||||
*
|
||||
* Drives 3Com 3C509B NICs. Only supports RJ45 (10base-T) transceiver.
|
||||
* The 3C509B uses a windowed register model with 8 register windows
|
||||
* selected via the Window register. Packets are transferred through
|
||||
* PIO (programmed I/O) using the card's FIFO.
|
||||
*
|
||||
* Uses IRQ 10 by default for interrupt-driven operation.
|
||||
* Registers as a character device with devicefs (/dev/ethN).
|
||||
*/
|
||||
|
||||
#ifndef E3C509_H
|
||||
#define E3C509_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* ================================================================
|
||||
* 3C509B I/O Port Layout
|
||||
*
|
||||
* The 3C509B uses 16 I/O ports starting at the base address.
|
||||
* Registers are organized into 8 windows (0-7).
|
||||
* Window selection: write window number to port base+0x0E.
|
||||
* ================================================================ */
|
||||
|
||||
/** Default ISA I/O base for 3C509B. */
|
||||
#define E3C509_DEFAULT_IOBASE 0x210
|
||||
|
||||
/** Default ISA IRQ for 3C509B. */
|
||||
#define E3C509_DEFAULT_IRQ 10
|
||||
|
||||
/* --- Global registers (always accessible) --- */
|
||||
#define E3C509_CMD 0x0E /**< Command register (write) */
|
||||
#define E3C509_STATUS 0x0E /**< Status register (read) */
|
||||
|
||||
/* --- Window 0: Setup / Configuration --- */
|
||||
#define E3C509_W0_MFG_ID 0x00 /**< Manufacturer ID (should be 0x6D50) */
|
||||
#define E3C509_W0_ADDR_CFG 0x06 /**< Address config (transceiver type) */
|
||||
#define E3C509_W0_RES_CFG 0x08 /**< Resource config (IRQ) */
|
||||
#define E3C509_W0_CFG_CTRL 0x04 /**< Config control */
|
||||
|
||||
/* --- Window 1: Operating Set --- */
|
||||
#define E3C509_W1_TX_PIO 0x00 /**< TX PIO data (write) */
|
||||
#define E3C509_W1_TX_STATUS 0x0B /**< TX status */
|
||||
#define E3C509_W1_RX_PIO 0x00 /**< RX PIO data (read) */
|
||||
#define E3C509_W1_RX_STATUS 0x08 /**< RX status */
|
||||
#define E3C509_W1_FREE_TX 0x0C /**< Free TX bytes */
|
||||
|
||||
/* --- Window 2: Station Address --- */
|
||||
#define E3C509_W2_ADDR0 0x00 /**< Station address word 0 */
|
||||
#define E3C509_W2_ADDR1 0x02 /**< Station address word 1 */
|
||||
#define E3C509_W2_ADDR2 0x04 /**< Station address word 2 */
|
||||
|
||||
/* --- Window 3: FIFO Management --- */
|
||||
/* (Used for internal FIFO buffer management) */
|
||||
|
||||
/* --- Window 4: Diagnostics --- */
|
||||
#define E3C509_W4_MEDIA_TYPE 0x0A /**< Media type and status */
|
||||
#define E3C509_W4_NET_DIAG 0x06 /**< Network diagnostics */
|
||||
|
||||
/* --- Window 5: Read Zeroes (for RX filter) --- */
|
||||
|
||||
/* --- Window 6: Statistics --- */
|
||||
#define E3C509_W6_TX_BYTES 0x0C /**< Total TX bytes */
|
||||
#define E3C509_W6_RX_BYTES 0x0A /**< Total RX bytes */
|
||||
|
||||
/* ================================================================
|
||||
* 3C509B Commands (written to command register)
|
||||
* ================================================================ */
|
||||
#define CMD_GLOBAL_RESET 0x0000 /**< Global reset */
|
||||
#define CMD_SELECT_WINDOW 0x0800 /**< Select window (OR with window num) */
|
||||
#define CMD_RX_ENABLE 0x2000 /**< Enable receiver */
|
||||
#define CMD_RX_RESET 0x2800 /**< Reset receiver */
|
||||
#define CMD_RX_DISCARD 0x4000 /**< Discard top RX packet */
|
||||
#define CMD_TX_ENABLE 0x4800 /**< Enable transmitter */
|
||||
#define CMD_TX_RESET 0x5800 /**< Reset transmitter */
|
||||
#define CMD_REQ_INTR 0x6000 /**< Request interrupt (OR with mask) */
|
||||
#define CMD_ACK_INTR 0x6800 /**< Acknowledge interrupt (OR with mask) */
|
||||
#define CMD_SET_INTR_MASK 0x7000 /**< Set interrupt mask */
|
||||
#define CMD_SET_RX_FILTER 0x8000 /**< Set RX filter */
|
||||
#define CMD_TX_DONE 0x8800 /**< ? */
|
||||
#define CMD_STATS_ENABLE 0x9000 /**< Enable statistics */
|
||||
#define CMD_STATS_DISABLE 0xB000 /**< Disable statistics */
|
||||
#define CMD_SET_TX_START 0x9800 /**< Set TX start threshold */
|
||||
|
||||
/* ================================================================
|
||||
* Status / Interrupt bits
|
||||
* ================================================================ */
|
||||
#define STAT_INT_LATCH 0x0001 /**< Interrupt latch */
|
||||
#define STAT_ADAPTER_FAIL 0x0002 /**< Adapter failure */
|
||||
#define STAT_TX_COMPLETE 0x0004 /**< TX complete */
|
||||
#define STAT_TX_AVAILABLE 0x0008 /**< TX available */
|
||||
#define STAT_RX_COMPLETE 0x0010 /**< RX complete */
|
||||
#define STAT_RX_EARLY 0x0020 /**< RX early threshold */
|
||||
#define STAT_INT_REQ 0x0040 /**< Interrupt requested */
|
||||
#define STAT_UPDATE_STATS 0x0080 /**< Update statistics */
|
||||
#define STAT_CMD_IN_PROG 0x1000 /**< Command in progress */
|
||||
#define STAT_WINDOW_MASK 0xE000 /**< Current window bits */
|
||||
|
||||
/* ================================================================
|
||||
* RX Filter bits (for CMD_SET_RX_FILTER)
|
||||
* ================================================================ */
|
||||
#define RX_FILTER_STATION 0x01 /**< Accept frames to station address */
|
||||
#define RX_FILTER_MCAST 0x02 /**< Accept multicast */
|
||||
#define RX_FILTER_BCAST 0x04 /**< Accept broadcast */
|
||||
#define RX_FILTER_PROMISC 0x08 /**< Promiscuous mode */
|
||||
|
||||
/* ================================================================
|
||||
* Transceiver types
|
||||
* ================================================================ */
|
||||
#define XCVR_TP 0x00 /**< 10base-T / RJ45 */
|
||||
#define XCVR_AUI 0x01 /**< AUI */
|
||||
#define XCVR_BNC 0x03 /**< BNC / 10base2 */
|
||||
|
||||
/** Maximum Ethernet frame size. */
|
||||
#define ETH_FRAME_MAX 1518
|
||||
|
||||
/* ================================================================
|
||||
* 3C509B device state
|
||||
* ================================================================ */
|
||||
typedef struct e3c509_device {
|
||||
uint16_t io_base; /**< I/O base address. */
|
||||
uint8_t irq; /**< IRQ number. */
|
||||
uint8_t mac[6]; /**< MAC address. */
|
||||
int present; /**< 1 if card detected. */
|
||||
} e3c509_device_t;
|
||||
|
||||
/* ================================================================
|
||||
* Public API
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Initialize 3C509B driver.
|
||||
*/
|
||||
int e3c509_init(void);
|
||||
|
||||
/**
|
||||
* 3C509B IRQ handler.
|
||||
*/
|
||||
void e3c509_irq(void);
|
||||
|
||||
/**
|
||||
* Send an Ethernet frame.
|
||||
*/
|
||||
int e3c509_send(e3c509_device_t *dev, const void *data, uint32_t len);
|
||||
|
||||
/**
|
||||
* Receive a pending Ethernet frame.
|
||||
*/
|
||||
int e3c509_recv(e3c509_device_t *dev, void *buf, uint32_t bufsize);
|
||||
|
||||
/**
|
||||
* Get the 3C509B device pointer.
|
||||
*/
|
||||
e3c509_device_t *e3c509_get_device(void);
|
||||
|
||||
#endif /* E3C509_H */
|
||||
104
src/env.c
Normal file
104
src/env.c
Normal file
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* @file env.c
|
||||
* @brief Per-process environment variable implementation.
|
||||
*
|
||||
* Provides a simple key=value store per process. Each process has a fixed
|
||||
* array of ENV_MAX_VARS entries. Empty key strings indicate unused slots.
|
||||
*/
|
||||
|
||||
#include "env.h"
|
||||
#include <string.h>
|
||||
|
||||
void env_init(env_block_t *env) {
|
||||
memset(env, 0, sizeof(env_block_t));
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the slot index for a given key.
|
||||
*
|
||||
* @param env Pointer to the environment block.
|
||||
* @param key Variable name to search for.
|
||||
* @return Slot index, or -1 if not found.
|
||||
*/
|
||||
static int env_find(const env_block_t *env, const char *key) {
|
||||
for (int i = 0; i < ENV_MAX_VARS; i++) {
|
||||
if (env->vars[i].key[0] != '\0' &&
|
||||
strcmp(env->vars[i].key, key) == 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find an empty slot in the environment block.
|
||||
*
|
||||
* @param env Pointer to the environment block.
|
||||
* @return Slot index, or -1 if full.
|
||||
*/
|
||||
static int env_find_free(const env_block_t *env) {
|
||||
for (int i = 0; i < ENV_MAX_VARS; i++) {
|
||||
if (env->vars[i].key[0] == '\0') {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int32_t env_get(const env_block_t *env, const char *key, char *buf, uint32_t bufsize) {
|
||||
int idx = env_find(env, key);
|
||||
if (idx < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t vlen = strlen(env->vars[idx].value);
|
||||
if (buf && bufsize > 0) {
|
||||
uint32_t copy_len = vlen;
|
||||
if (copy_len >= bufsize) {
|
||||
copy_len = bufsize - 1;
|
||||
}
|
||||
memcpy(buf, env->vars[idx].value, copy_len);
|
||||
buf[copy_len] = '\0';
|
||||
}
|
||||
return (int32_t)vlen;
|
||||
}
|
||||
|
||||
int32_t env_set(env_block_t *env, const char *key, const char *value) {
|
||||
if (!key || key[0] == '\0') {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* If value is NULL or empty, unset the variable */
|
||||
if (!value || value[0] == '\0') {
|
||||
int idx = env_find(env, key);
|
||||
if (idx >= 0) {
|
||||
env->vars[idx].key[0] = '\0';
|
||||
env->vars[idx].value[0] = '\0';
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Check if key already exists */
|
||||
int idx = env_find(env, key);
|
||||
if (idx < 0) {
|
||||
/* Need a new slot */
|
||||
idx = env_find_free(env);
|
||||
if (idx < 0) {
|
||||
return -1; /* Environment full */
|
||||
}
|
||||
}
|
||||
|
||||
/* Copy key */
|
||||
strncpy(env->vars[idx].key, key, ENV_MAX_KEY - 1);
|
||||
env->vars[idx].key[ENV_MAX_KEY - 1] = '\0';
|
||||
|
||||
/* Copy value */
|
||||
strncpy(env->vars[idx].value, value, ENV_MAX_VALUE - 1);
|
||||
env->vars[idx].value[ENV_MAX_VALUE - 1] = '\0';
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void env_copy(env_block_t *dst, const env_block_t *src) {
|
||||
memcpy(dst, src, sizeof(env_block_t));
|
||||
}
|
||||
75
src/env.h
Normal file
75
src/env.h
Normal file
@@ -0,0 +1,75 @@
|
||||
/**
|
||||
* @file env.h
|
||||
* @brief Per-process environment variable support.
|
||||
*
|
||||
* Each process has a fixed-size environment table storing key=value pairs.
|
||||
* Environment variables are copied to child processes during fork().
|
||||
* User-mode programs interact with the environment via SYS_GETENV and
|
||||
* SYS_SETENV system calls.
|
||||
*/
|
||||
|
||||
#ifndef ENV_H
|
||||
#define ENV_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/** Maximum number of environment variables per process. */
|
||||
#define ENV_MAX_VARS 32
|
||||
/** Maximum length of an environment variable key (including NUL). */
|
||||
#define ENV_MAX_KEY 64
|
||||
/** Maximum length of an environment variable value (including NUL). */
|
||||
#define ENV_MAX_VALUE 128
|
||||
|
||||
/**
|
||||
* A single environment variable entry.
|
||||
*/
|
||||
typedef struct {
|
||||
char key[ENV_MAX_KEY]; /**< Variable name (empty string = unused). */
|
||||
char value[ENV_MAX_VALUE]; /**< Variable value. */
|
||||
} env_var_t;
|
||||
|
||||
/**
|
||||
* Environment block for a process.
|
||||
*/
|
||||
typedef struct {
|
||||
env_var_t vars[ENV_MAX_VARS];
|
||||
} env_block_t;
|
||||
|
||||
/**
|
||||
* Initialize an environment block (all entries empty).
|
||||
*
|
||||
* @param env Pointer to the environment block to initialize.
|
||||
*/
|
||||
void env_init(env_block_t *env);
|
||||
|
||||
/**
|
||||
* Get an environment variable.
|
||||
*
|
||||
* @param env Pointer to the environment block.
|
||||
* @param key Variable name to look up.
|
||||
* @param buf Buffer to write the value into.
|
||||
* @param bufsize Size of the buffer.
|
||||
* @return Length of the value (excluding NUL), or -1 if not found.
|
||||
* If bufsize is too small, the value is truncated.
|
||||
*/
|
||||
int32_t env_get(const env_block_t *env, const char *key, char *buf, uint32_t bufsize);
|
||||
|
||||
/**
|
||||
* Set an environment variable. Creates it if it doesn't exist.
|
||||
*
|
||||
* @param env Pointer to the environment block.
|
||||
* @param key Variable name (must not be empty).
|
||||
* @param value Variable value (NULL or empty string to unset).
|
||||
* @return 0 on success, -1 if the environment is full.
|
||||
*/
|
||||
int32_t env_set(env_block_t *env, const char *key, const char *value);
|
||||
|
||||
/**
|
||||
* Copy an environment block.
|
||||
*
|
||||
* @param dst Destination environment block.
|
||||
* @param src Source environment block.
|
||||
*/
|
||||
void env_copy(env_block_t *dst, const env_block_t *src);
|
||||
|
||||
#endif /* ENV_H */
|
||||
393
src/ethernet.c
Normal file
393
src/ethernet.c
Normal file
@@ -0,0 +1,393 @@
|
||||
/**
|
||||
* @file ethernet.c
|
||||
* @brief Ethernet subsystem implementation.
|
||||
*
|
||||
* Provides a unified layer over individual Ethernet NIC drivers.
|
||||
* NIC drivers register through ethernet_register(), which creates the
|
||||
* `/dev/ethN` char device and adds the interface to an internal table.
|
||||
*
|
||||
* The subsystem also exposes network interface info via sysfs at
|
||||
* `/sys/net`, allowing userspace tools to query interface status.
|
||||
*/
|
||||
|
||||
#include "ethernet.h"
|
||||
#include "devicefs.h"
|
||||
#include "sysfs.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Debug print helpers */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/* ================================================================
|
||||
* Global state
|
||||
* ================================================================ */
|
||||
|
||||
/** Interface table. */
|
||||
static eth_iface_t ifaces[ETH_MAX_IFACES];
|
||||
|
||||
/** Number of registered interfaces. */
|
||||
static uint32_t iface_count = 0;
|
||||
|
||||
/* ================================================================
|
||||
* Character device operations
|
||||
*
|
||||
* Maps devicefs char read/write to the NIC driver send/recv.
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Read from `/dev/ethN`: receive a raw Ethernet frame.
|
||||
*/
|
||||
static int32_t eth_char_read(void *dev_data, uint32_t size, void *buf) {
|
||||
eth_iface_t *iface = (eth_iface_t *)dev_data;
|
||||
if (!iface || !iface->active || !iface->recv) return -1;
|
||||
return (int32_t)iface->recv(iface->dev_data, buf, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write to `/dev/ethN`: send a raw Ethernet frame.
|
||||
*/
|
||||
static int32_t eth_char_write(void *dev_data, uint32_t size, const void *buf) {
|
||||
eth_iface_t *iface = (eth_iface_t *)dev_data;
|
||||
if (!iface || !iface->active || !iface->send) return -1;
|
||||
int ret = iface->send(iface->dev_data, buf, size);
|
||||
return (ret == 0) ? (int32_t)size : -1;
|
||||
}
|
||||
|
||||
/** Devicefs char ops shared by all ethernet interfaces. */
|
||||
static devicefs_char_ops_t eth_char_ops = {
|
||||
.read = eth_char_read,
|
||||
.write = eth_char_write,
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* Sysfs operations for /sys/net
|
||||
*
|
||||
* Directory layout:
|
||||
* /sys/net/
|
||||
* eth1/
|
||||
* mac - MAC address as hex string
|
||||
* ip - IPv4 address
|
||||
* netmask - subnet mask
|
||||
* gateway - default gateway
|
||||
* link - "up" or "down"
|
||||
* eth2/
|
||||
* ...
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Format a MAC address as "XX:XX:XX:XX:XX:XX" into buf.
|
||||
*/
|
||||
static int format_mac(const uint8_t *mac, char *buf, uint32_t buf_size) {
|
||||
if (buf_size < 18) return -1;
|
||||
static const char hex[] = "0123456789ABCDEF";
|
||||
int pos = 0;
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if (i > 0) buf[pos++] = ':';
|
||||
buf[pos++] = hex[(mac[i] >> 4) & 0xF];
|
||||
buf[pos++] = hex[mac[i] & 0xF];
|
||||
}
|
||||
buf[pos++] = '\n';
|
||||
buf[pos] = '\0';
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format an IPv4 address as "A.B.C.D\n" into buf.
|
||||
*/
|
||||
static int format_ipv4(uint32_t ip, char *buf, uint32_t buf_size) {
|
||||
if (buf_size < 16) return -1;
|
||||
int pos = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (i > 0) buf[pos++] = '.';
|
||||
uint8_t octet = (uint8_t)((ip >> (24 - i * 8)) & 0xFF);
|
||||
if (octet >= 100) { buf[pos++] = (char)('0' + octet / 100); octet %= 100; buf[pos++] = (char)('0' + octet / 10); octet %= 10; }
|
||||
else if (octet >= 10) { buf[pos++] = (char)('0' + octet / 10); octet %= 10; }
|
||||
buf[pos++] = (char)('0' + octet);
|
||||
}
|
||||
buf[pos++] = '\n';
|
||||
buf[pos] = '\0';
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse interface name from the path prefix.
|
||||
* E.g., path "eth1/mac" → iface "eth1", subpath "mac".
|
||||
* Returns the iface, or NULL if not found.
|
||||
*/
|
||||
static eth_iface_t *parse_iface_path(const char *path, const char **subpath) {
|
||||
/* Find the first '/' */
|
||||
const char *slash = NULL;
|
||||
for (const char *p = path; *p; p++) {
|
||||
if (*p == '/') { slash = p; break; }
|
||||
}
|
||||
|
||||
char iface_name[16];
|
||||
if (slash) {
|
||||
uint32_t len = (uint32_t)(slash - path);
|
||||
if (len >= sizeof(iface_name)) return NULL;
|
||||
memcpy(iface_name, path, len);
|
||||
iface_name[len] = '\0';
|
||||
*subpath = slash + 1;
|
||||
} else {
|
||||
/* No slash — path is just the interface name */
|
||||
uint32_t len = strlen(path);
|
||||
if (len >= sizeof(iface_name)) return NULL;
|
||||
memcpy(iface_name, path, len + 1);
|
||||
*subpath = "";
|
||||
}
|
||||
|
||||
return ethernet_find_iface(iface_name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sysfs list callback for /sys/net.
|
||||
*/
|
||||
static int net_sysfs_list(void *ctx, const char *path, uint32_t idx,
|
||||
sysfs_entry_t *out) {
|
||||
(void)ctx;
|
||||
|
||||
if (path[0] == '\0') {
|
||||
/* Root of /sys/net — list interfaces */
|
||||
uint32_t count = 0;
|
||||
for (uint32_t i = 0; i < ETH_MAX_IFACES; i++) {
|
||||
if (!ifaces[i].active) continue;
|
||||
if (count == idx) {
|
||||
memset(out, 0, sizeof(sysfs_entry_t));
|
||||
strncpy(out->name, ifaces[i].name, SYSFS_MAX_NAME - 1);
|
||||
out->is_dir = 1;
|
||||
return 0;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Listing inside an interface directory */
|
||||
const char *subpath;
|
||||
eth_iface_t *iface = parse_iface_path(path, &subpath);
|
||||
if (!iface) return -1;
|
||||
|
||||
/* Files: mac, ip, netmask, gateway, link */
|
||||
static const char *files[] = { "mac", "ip", "netmask", "gateway", "link" };
|
||||
if (idx >= 5) return -1;
|
||||
|
||||
memset(out, 0, sizeof(sysfs_entry_t));
|
||||
strncpy(out->name, files[idx], SYSFS_MAX_NAME - 1);
|
||||
out->is_dir = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sysfs read callback for /sys/net.
|
||||
*/
|
||||
static int net_sysfs_read(void *ctx, const char *path, char *buf,
|
||||
uint32_t buf_size) {
|
||||
(void)ctx;
|
||||
|
||||
const char *subpath;
|
||||
eth_iface_t *iface = parse_iface_path(path, &subpath);
|
||||
if (!iface) return -1;
|
||||
|
||||
if (strcmp(subpath, "mac") == 0) {
|
||||
return format_mac(iface->mac, buf, buf_size);
|
||||
}
|
||||
if (strcmp(subpath, "ip") == 0) {
|
||||
return format_ipv4(iface->ip_addr, buf, buf_size);
|
||||
}
|
||||
if (strcmp(subpath, "netmask") == 0) {
|
||||
return format_ipv4(iface->netmask, buf, buf_size);
|
||||
}
|
||||
if (strcmp(subpath, "gateway") == 0) {
|
||||
return format_ipv4(iface->gateway, buf, buf_size);
|
||||
}
|
||||
if (strcmp(subpath, "link") == 0) {
|
||||
const char *s = iface->link_up ? "up\n" : "down\n";
|
||||
strncpy(buf, s, buf_size);
|
||||
return (int)strlen(s);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sysfs write callback for /sys/net.
|
||||
* Allows setting IP address, netmask, gateway.
|
||||
*/
|
||||
static int net_sysfs_write(void *ctx, const char *path, const char *buf,
|
||||
uint32_t size) {
|
||||
(void)ctx;
|
||||
(void)size;
|
||||
|
||||
const char *subpath;
|
||||
eth_iface_t *iface = parse_iface_path(path, &subpath);
|
||||
if (!iface) return -1;
|
||||
|
||||
/* Parse dotted-decimal IP from buf */
|
||||
if (strcmp(subpath, "ip") == 0 || strcmp(subpath, "netmask") == 0 ||
|
||||
strcmp(subpath, "gateway") == 0) {
|
||||
uint32_t octets[4] = {0};
|
||||
int octet_idx = 0;
|
||||
for (uint32_t i = 0; i < size && octet_idx < 4; i++) {
|
||||
if (buf[i] == '.' || buf[i] == '\n' || buf[i] == '\0') {
|
||||
octet_idx++;
|
||||
} else if (buf[i] >= '0' && buf[i] <= '9') {
|
||||
octets[octet_idx] = octets[octet_idx] * 10 + (uint32_t)(buf[i] - '0');
|
||||
}
|
||||
}
|
||||
uint32_t addr = (octets[0] << 24) | (octets[1] << 16) |
|
||||
(octets[2] << 8) | octets[3];
|
||||
|
||||
if (strcmp(subpath, "ip") == 0) iface->ip_addr = addr;
|
||||
if (strcmp(subpath, "netmask") == 0) iface->netmask = addr;
|
||||
if (strcmp(subpath, "gateway") == 0) iface->gateway = addr;
|
||||
|
||||
return (int)size;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/** Sysfs operations for /sys/net. */
|
||||
static sysfs_ops_t net_sysfs_ops = {
|
||||
.list = net_sysfs_list,
|
||||
.read = net_sysfs_read,
|
||||
.write = net_sysfs_write,
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* Public API
|
||||
* ================================================================ */
|
||||
|
||||
void ethernet_init(void) {
|
||||
memset(ifaces, 0, sizeof(ifaces));
|
||||
iface_count = 0;
|
||||
|
||||
sysfs_register("net", &net_sysfs_ops, NULL);
|
||||
offset_print(" ETHERNET: subsystem initialized\n");
|
||||
}
|
||||
|
||||
eth_iface_t *ethernet_register(const uint8_t *mac,
|
||||
eth_send_fn send,
|
||||
eth_recv_fn recv,
|
||||
void *dev_data) {
|
||||
if (iface_count >= ETH_MAX_IFACES) {
|
||||
offset_print(" ETHERNET: too many interfaces\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Find free slot */
|
||||
eth_iface_t *iface = NULL;
|
||||
for (uint32_t i = 0; i < ETH_MAX_IFACES; i++) {
|
||||
if (!ifaces[i].active) {
|
||||
iface = &ifaces[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!iface) return NULL;
|
||||
|
||||
memset(iface, 0, sizeof(eth_iface_t));
|
||||
|
||||
/* Copy MAC */
|
||||
memcpy(iface->mac, mac, ETH_ALEN);
|
||||
iface->send = send;
|
||||
iface->recv = recv;
|
||||
iface->dev_data = dev_data;
|
||||
iface->link_up = 1; /* Assume link up after init */
|
||||
iface->active = 1;
|
||||
|
||||
/* Register as /dev/ethN — devicefs assigns the number */
|
||||
devicefs_device_t *devfs_dev = devicefs_register_char("eth",
|
||||
ð_char_ops,
|
||||
iface);
|
||||
if (devfs_dev) {
|
||||
/* Copy the assigned name back */
|
||||
strncpy(iface->name, devfs_dev->name, sizeof(iface->name) - 1);
|
||||
} else {
|
||||
/* Fallback name */
|
||||
iface->name[0] = 'e'; iface->name[1] = 't'; iface->name[2] = 'h';
|
||||
iface->name[3] = (char)('0' + iface_count + 1);
|
||||
iface->name[4] = '\0';
|
||||
}
|
||||
|
||||
iface_count++;
|
||||
|
||||
offset_print(" ETHERNET: registered ");
|
||||
offset_print(iface->name);
|
||||
offset_print(" MAC ");
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if (i > 0) offset_print(":");
|
||||
print_hex(mac[i]);
|
||||
}
|
||||
offset_print("\n");
|
||||
|
||||
return iface;
|
||||
}
|
||||
|
||||
int ethernet_send(eth_iface_t *iface, const uint8_t *dst_mac,
|
||||
uint16_t ethertype, const void *payload, uint32_t len) {
|
||||
if (!iface || !iface->active || !iface->send) return -1;
|
||||
if (len > ETH_MTU) return -1;
|
||||
|
||||
/* Build complete Ethernet frame on stack */
|
||||
uint8_t frame[ETH_FRAME_LEN];
|
||||
eth_header_t *hdr = (eth_header_t *)frame;
|
||||
|
||||
memcpy(hdr->dst, dst_mac, ETH_ALEN);
|
||||
memcpy(hdr->src, iface->mac, ETH_ALEN);
|
||||
hdr->ethertype = htons(ethertype);
|
||||
|
||||
memcpy(frame + ETH_HLEN, payload, len);
|
||||
|
||||
uint32_t frame_len = ETH_HLEN + len;
|
||||
if (frame_len < 60) frame_len = 60; /* Minimum Ethernet frame */
|
||||
|
||||
return iface->send(iface->dev_data, frame, frame_len);
|
||||
}
|
||||
|
||||
int ethernet_recv(eth_iface_t *iface, void *buf, uint32_t bufsize,
|
||||
uint8_t *src_mac, uint16_t *ethertype) {
|
||||
if (!iface || !iface->active || !iface->recv) return -1;
|
||||
|
||||
/* Receive into temporary buffer */
|
||||
uint8_t frame[ETH_FRAME_LEN];
|
||||
int ret = iface->recv(iface->dev_data, frame, sizeof(frame));
|
||||
if (ret <= 0) return ret;
|
||||
|
||||
if ((uint32_t)ret < ETH_HLEN) return -1; /* Runt frame */
|
||||
|
||||
eth_header_t *hdr = (eth_header_t *)frame;
|
||||
|
||||
/* Copy out src MAC and ethertype if requested */
|
||||
if (src_mac) memcpy(src_mac, hdr->src, ETH_ALEN);
|
||||
if (ethertype) *ethertype = ntohs(hdr->ethertype);
|
||||
|
||||
/* Copy payload */
|
||||
uint32_t payload_len = (uint32_t)ret - ETH_HLEN;
|
||||
if (payload_len > bufsize) payload_len = bufsize;
|
||||
memcpy(buf, frame + ETH_HLEN, payload_len);
|
||||
|
||||
return (int)payload_len;
|
||||
}
|
||||
|
||||
eth_iface_t *ethernet_get_iface(uint32_t index) {
|
||||
uint32_t count = 0;
|
||||
for (uint32_t i = 0; i < ETH_MAX_IFACES; i++) {
|
||||
if (!ifaces[i].active) continue;
|
||||
if (count == index) return &ifaces[i];
|
||||
count++;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uint32_t ethernet_get_iface_count(void) {
|
||||
return iface_count;
|
||||
}
|
||||
|
||||
eth_iface_t *ethernet_find_iface(const char *name) {
|
||||
for (uint32_t i = 0; i < ETH_MAX_IFACES; i++) {
|
||||
if (!ifaces[i].active) continue;
|
||||
if (strcmp(ifaces[i].name, name) == 0) return &ifaces[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
208
src/ethernet.h
Normal file
208
src/ethernet.h
Normal file
@@ -0,0 +1,208 @@
|
||||
/**
|
||||
* @file ethernet.h
|
||||
* @brief Ethernet subsystem.
|
||||
*
|
||||
* Provides a unified abstraction over individual Ethernet NIC drivers.
|
||||
* Each NIC driver registers itself with the ethernet subsystem, which
|
||||
* then creates the `/dev/ethN` character device and exposes interface
|
||||
* information via `/sys/net`.
|
||||
*
|
||||
* Higher-level protocols (IPv4, ARP, etc.) use this subsystem to send
|
||||
* and receive Ethernet frames without knowing which NIC driver is in use.
|
||||
*/
|
||||
|
||||
#ifndef ETHERNET_H
|
||||
#define ETHERNET_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/** Maximum number of Ethernet interfaces. */
|
||||
#define ETH_MAX_IFACES 8
|
||||
|
||||
/** Ethernet MAC address length. */
|
||||
#define ETH_ALEN 6
|
||||
|
||||
/** Ethernet header size (dst + src + ethertype). */
|
||||
#define ETH_HLEN 14
|
||||
|
||||
/** Maximum Ethernet payload size. */
|
||||
#define ETH_MTU 1500
|
||||
|
||||
/** Maximum Ethernet frame size (header + payload). */
|
||||
#define ETH_FRAME_LEN (ETH_HLEN + ETH_MTU)
|
||||
|
||||
/* ================================================================
|
||||
* EtherType constants
|
||||
* ================================================================ */
|
||||
#define ETHERTYPE_IPV4 0x0800 /**< IPv4 */
|
||||
#define ETHERTYPE_ARP 0x0806 /**< ARP */
|
||||
|
||||
/* ================================================================
|
||||
* Ethernet header (14 bytes)
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Ethernet frame header.
|
||||
*/
|
||||
typedef struct __attribute__((packed)) eth_header {
|
||||
uint8_t dst[ETH_ALEN]; /**< Destination MAC address. */
|
||||
uint8_t src[ETH_ALEN]; /**< Source MAC address. */
|
||||
uint16_t ethertype; /**< EtherType (big-endian). */
|
||||
} eth_header_t;
|
||||
|
||||
/* ================================================================
|
||||
* NIC driver callbacks
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Callback to send a raw Ethernet frame (complete with header).
|
||||
*
|
||||
* @param dev_data Driver-specific device pointer.
|
||||
* @param frame Complete Ethernet frame (header + payload).
|
||||
* @param len Frame length in bytes.
|
||||
* @return 0 on success, -1 on failure.
|
||||
*/
|
||||
typedef int (*eth_send_fn)(void *dev_data, const void *frame, uint32_t len);
|
||||
|
||||
/**
|
||||
* Callback to receive a raw Ethernet frame (complete with header).
|
||||
*
|
||||
* @param dev_data Driver-specific device pointer.
|
||||
* @param buf Buffer for received frame.
|
||||
* @param bufsize Buffer size.
|
||||
* @return Number of bytes received, 0 if none, -1 on error.
|
||||
*/
|
||||
typedef int (*eth_recv_fn)(void *dev_data, void *buf, uint32_t bufsize);
|
||||
|
||||
/* ================================================================
|
||||
* Ethernet interface
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Represents one Ethernet network interface.
|
||||
*/
|
||||
typedef struct eth_iface {
|
||||
char name[16]; /**< Interface name (e.g., "eth1"). */
|
||||
uint8_t mac[ETH_ALEN]; /**< MAC address. */
|
||||
uint8_t active; /**< 1 if registered, 0 if free. */
|
||||
uint8_t link_up; /**< 1 if link is up. */
|
||||
uint32_t ip_addr; /**< IPv4 address (0 if not configured). */
|
||||
uint32_t netmask; /**< Subnet mask (0 if not configured). */
|
||||
uint32_t gateway; /**< Default gateway (0 if not configured). */
|
||||
|
||||
eth_send_fn send; /**< NIC driver send callback. */
|
||||
eth_recv_fn recv; /**< NIC driver receive callback. */
|
||||
void *dev_data; /**< Opaque pointer to NIC driver state. */
|
||||
} eth_iface_t;
|
||||
|
||||
/* ================================================================
|
||||
* Public API
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Initialize the ethernet subsystem.
|
||||
* Registers sysfs namespace "net" and prepares the interface table.
|
||||
*/
|
||||
void ethernet_init(void);
|
||||
|
||||
/**
|
||||
* Register a NIC with the ethernet subsystem.
|
||||
*
|
||||
* Creates a `/dev/ethN` character device and adds the interface to the
|
||||
* internal table. Called by NIC drivers during initialization.
|
||||
*
|
||||
* @param mac 6-byte MAC address.
|
||||
* @param send Send callback function.
|
||||
* @param recv Receive callback function.
|
||||
* @param dev_data Opaque NIC device pointer (passed to callbacks).
|
||||
* @return Pointer to the registered interface, or NULL on failure.
|
||||
*/
|
||||
eth_iface_t *ethernet_register(const uint8_t *mac,
|
||||
eth_send_fn send,
|
||||
eth_recv_fn recv,
|
||||
void *dev_data);
|
||||
|
||||
/**
|
||||
* Send an Ethernet frame through an interface.
|
||||
*
|
||||
* Constructs the Ethernet header (src MAC from interface, dst from caller)
|
||||
* and dispatches through the NIC driver.
|
||||
*
|
||||
* @param iface Interface to send through.
|
||||
* @param dst_mac Destination MAC address (6 bytes).
|
||||
* @param ethertype EtherType (host byte order, will be converted to big-endian).
|
||||
* @param payload Frame payload.
|
||||
* @param len Payload length (max ETH_MTU).
|
||||
* @return 0 on success, -1 on failure.
|
||||
*/
|
||||
int ethernet_send(eth_iface_t *iface, const uint8_t *dst_mac,
|
||||
uint16_t ethertype, const void *payload, uint32_t len);
|
||||
|
||||
/**
|
||||
* Receive an Ethernet frame from an interface.
|
||||
*
|
||||
* @param iface Interface to receive from.
|
||||
* @param buf Buffer for frame payload (Ethernet header is stripped).
|
||||
* @param bufsize Buffer size.
|
||||
* @param src_mac Output: source MAC of received frame (6 bytes, can be NULL).
|
||||
* @param ethertype Output: EtherType of received frame (host byte order, can be NULL).
|
||||
* @return Payload length in bytes, 0 if no packet, -1 on error.
|
||||
*/
|
||||
int ethernet_recv(eth_iface_t *iface, void *buf, uint32_t bufsize,
|
||||
uint8_t *src_mac, uint16_t *ethertype);
|
||||
|
||||
/**
|
||||
* Get an interface by index.
|
||||
*
|
||||
* @param index 0-based index.
|
||||
* @return Pointer to interface, or NULL if index out of range.
|
||||
*/
|
||||
eth_iface_t *ethernet_get_iface(uint32_t index);
|
||||
|
||||
/**
|
||||
* Get the number of registered interfaces.
|
||||
*
|
||||
* @return Number of active Ethernet interfaces.
|
||||
*/
|
||||
uint32_t ethernet_get_iface_count(void);
|
||||
|
||||
/**
|
||||
* Find an interface by name (e.g., "eth1").
|
||||
*
|
||||
* @param name Interface name.
|
||||
* @return Pointer to interface, or NULL if not found.
|
||||
*/
|
||||
eth_iface_t *ethernet_find_iface(const char *name);
|
||||
|
||||
/**
|
||||
* Convert a 16-bit value from host to network byte order (big-endian).
|
||||
*/
|
||||
static inline uint16_t htons(uint16_t h) {
|
||||
return (uint16_t)((h >> 8) | (h << 8));
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a 16-bit value from network to host byte order.
|
||||
*/
|
||||
static inline uint16_t ntohs(uint16_t n) {
|
||||
return htons(n);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a 32-bit value from host to network byte order (big-endian).
|
||||
*/
|
||||
static inline uint32_t htonl(uint32_t h) {
|
||||
return ((h >> 24) & 0x000000FF) |
|
||||
((h >> 8) & 0x0000FF00) |
|
||||
((h << 8) & 0x00FF0000) |
|
||||
((h << 24) & 0xFF000000);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a 32-bit value from network to host byte order.
|
||||
*/
|
||||
static inline uint32_t ntohl(uint32_t n) {
|
||||
return htonl(n);
|
||||
}
|
||||
|
||||
#endif /* ETHERNET_H */
|
||||
673
src/fat32.c
Normal file
673
src/fat32.c
Normal file
@@ -0,0 +1,673 @@
|
||||
/**
|
||||
* @file fat32.c
|
||||
* @brief FAT32 filesystem driver implementation.
|
||||
*
|
||||
* Reads and writes FAT32 filesystems on block devices. Supports:
|
||||
* - Reading the BPB and FAT
|
||||
* - Following cluster chains
|
||||
* - Reading directory entries (short 8.3 names)
|
||||
* - Reading and writing file data
|
||||
* - Creating new files
|
||||
*
|
||||
* Mounted via fat32_mount_device() which registers VFS operations.
|
||||
*/
|
||||
|
||||
#include "fat32.h"
|
||||
#include "vfs.h"
|
||||
#include "kmalloc.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Debug print helpers defined in kernel.c */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/** Maximum number of simultaneous FAT32 mounts. */
|
||||
#define FAT32_MAX_MOUNTS 4
|
||||
|
||||
/** Sector buffer size (one sector). */
|
||||
#define SECTOR_SIZE 512
|
||||
|
||||
/** Static mount contexts. */
|
||||
static fat32_fs_t fat32_mounts[FAT32_MAX_MOUNTS];
|
||||
static int fat32_mount_count = 0;
|
||||
|
||||
/* ================================================================
|
||||
* Low-level block I/O helpers
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Read sectors from the underlying block device.
|
||||
*
|
||||
* @param fs FAT32 filesystem instance.
|
||||
* @param lba Logical block address (absolute, 0 = device start).
|
||||
* @param count Number of sectors to read.
|
||||
* @param buf Output buffer (must be count * 512 bytes).
|
||||
* @return 0 on success, -1 on error.
|
||||
*/
|
||||
static int fat32_read_sectors(fat32_fs_t *fs, uint32_t lba,
|
||||
uint32_t count, void *buf) {
|
||||
if (!fs->dev || !fs->dev->block_ops || !fs->dev->block_ops->read_sectors) {
|
||||
return -1;
|
||||
}
|
||||
return fs->dev->block_ops->read_sectors(fs->dev->dev_data, lba, count, buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write sectors to the underlying block device.
|
||||
*/
|
||||
static int fat32_write_sectors(fat32_fs_t *fs, uint32_t lba,
|
||||
uint32_t count, const void *buf) {
|
||||
if (!fs->dev || !fs->dev->block_ops || !fs->dev->block_ops->write_sectors) {
|
||||
return -1;
|
||||
}
|
||||
return fs->dev->block_ops->write_sectors(fs->dev->dev_data, lba, count, buf);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* FAT table operations
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Read a single FAT entry (the next cluster in the chain).
|
||||
*
|
||||
* @param fs Filesystem instance.
|
||||
* @param cluster Current cluster number.
|
||||
* @return Next cluster number, or FAT32_EOC if end of chain.
|
||||
*/
|
||||
static uint32_t fat32_read_fat(fat32_fs_t *fs, uint32_t cluster) {
|
||||
/* Each FAT entry is 4 bytes. Calculate which sector and offset. */
|
||||
uint32_t fat_offset = cluster * 4;
|
||||
uint32_t fat_sector = fs->fat_start_lba + (fat_offset / SECTOR_SIZE);
|
||||
uint32_t entry_offset = fat_offset % SECTOR_SIZE;
|
||||
|
||||
uint8_t sector_buf[SECTOR_SIZE];
|
||||
if (fat32_read_sectors(fs, fat_sector, 1, sector_buf) != 0) {
|
||||
return FAT32_EOC;
|
||||
}
|
||||
|
||||
uint32_t entry = *(uint32_t *)(sector_buf + entry_offset);
|
||||
return entry & 0x0FFFFFFF; /* Mask off top 4 bits */
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a FAT entry.
|
||||
*
|
||||
* @param fs Filesystem instance.
|
||||
* @param cluster Cluster to update.
|
||||
* @param value New value for the FAT entry.
|
||||
* @return 0 on success, -1 on error.
|
||||
*/
|
||||
static int fat32_write_fat(fat32_fs_t *fs, uint32_t cluster,
|
||||
uint32_t value) {
|
||||
uint32_t fat_offset = cluster * 4;
|
||||
uint32_t fat_sector = fs->fat_start_lba + (fat_offset / SECTOR_SIZE);
|
||||
uint32_t entry_offset = fat_offset % SECTOR_SIZE;
|
||||
|
||||
uint8_t sector_buf[SECTOR_SIZE];
|
||||
if (fat32_read_sectors(fs, fat_sector, 1, sector_buf) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Preserve top 4 bits of the existing entry */
|
||||
uint32_t *entry = (uint32_t *)(sector_buf + entry_offset);
|
||||
*entry = (*entry & 0xF0000000) | (value & 0x0FFFFFFF);
|
||||
|
||||
/* Write back to all FATs */
|
||||
for (uint8_t f = 0; f < fs->bpb.num_fats; f++) {
|
||||
uint32_t fat_sec = fs->fat_start_lba +
|
||||
(f * fs->bpb.fat_size_32) + (fat_offset / SECTOR_SIZE);
|
||||
if (fat32_write_sectors(fs, fat_sec, 1, sector_buf) != 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a free cluster in the FAT and mark it as end-of-chain.
|
||||
*
|
||||
* @param fs Filesystem instance.
|
||||
* @return Cluster number, or 0 if disk is full.
|
||||
*/
|
||||
static uint32_t fat32_alloc_cluster(fat32_fs_t *fs) {
|
||||
for (uint32_t c = 2; c < fs->total_clusters + 2; c++) {
|
||||
uint32_t val = fat32_read_fat(fs, c);
|
||||
if (val == FAT32_FREE) {
|
||||
fat32_write_fat(fs, c, FAT32_EOC);
|
||||
/* Zero out the cluster */
|
||||
uint8_t zero[SECTOR_SIZE];
|
||||
memset(zero, 0, SECTOR_SIZE);
|
||||
uint32_t first_sec = fs->data_start_lba +
|
||||
(c - 2) * fs->sectors_per_cluster;
|
||||
for (uint32_t s = 0; s < fs->sectors_per_cluster; s++) {
|
||||
fat32_write_sectors(fs, first_sec + s, 1, zero);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
}
|
||||
return 0; /* Full */
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Cluster / sector helpers
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Convert a cluster number to its first sector (absolute LBA).
|
||||
*/
|
||||
static uint32_t cluster_to_lba(fat32_fs_t *fs, uint32_t cluster) {
|
||||
return fs->data_start_lba + (cluster - 2) * fs->sectors_per_cluster;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read all data from a cluster chain into a buffer.
|
||||
*
|
||||
* @param fs Filesystem instance.
|
||||
* @param start Starting cluster.
|
||||
* @param buf Output buffer.
|
||||
* @param offset Byte offset to start reading from.
|
||||
* @param size Maximum bytes to read.
|
||||
* @return Bytes read, or -1 on error.
|
||||
*/
|
||||
static int32_t fat32_read_chain(fat32_fs_t *fs, uint32_t start,
|
||||
uint32_t offset, uint32_t size,
|
||||
void *buf) {
|
||||
if (start < 2) return -1;
|
||||
|
||||
uint32_t bpc = fs->bytes_per_cluster;
|
||||
uint32_t bytes_read = 0;
|
||||
uint32_t cluster = start;
|
||||
uint32_t pos = 0; /* Byte position in the chain */
|
||||
|
||||
/* Skip clusters until we reach the offset */
|
||||
while (pos + bpc <= offset && cluster >= 2 && cluster < FAT32_EOC) {
|
||||
pos += bpc;
|
||||
cluster = fat32_read_fat(fs, cluster);
|
||||
}
|
||||
|
||||
/* Read data */
|
||||
uint8_t cluster_buf[SECTOR_SIZE]; /* Read sector by sector */
|
||||
while (cluster >= 2 && cluster < FAT32_EOC && bytes_read < size) {
|
||||
uint32_t lba = cluster_to_lba(fs, cluster);
|
||||
|
||||
for (uint32_t s = 0; s < fs->sectors_per_cluster && bytes_read < size; s++) {
|
||||
uint32_t sec_start = pos;
|
||||
uint32_t sec_end = pos + SECTOR_SIZE;
|
||||
|
||||
if (sec_end <= offset) {
|
||||
/* This sector is entirely before our offset, skip */
|
||||
pos += SECTOR_SIZE;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (fat32_read_sectors(fs, lba + s, 1, cluster_buf) != 0) {
|
||||
return (bytes_read > 0) ? (int32_t)bytes_read : -1;
|
||||
}
|
||||
|
||||
/* Calculate how much of this sector we need */
|
||||
uint32_t sect_off = 0;
|
||||
if (offset > sec_start) {
|
||||
sect_off = offset - sec_start;
|
||||
}
|
||||
uint32_t sect_avail = SECTOR_SIZE - sect_off;
|
||||
uint32_t to_copy = size - bytes_read;
|
||||
if (to_copy > sect_avail) to_copy = sect_avail;
|
||||
|
||||
memcpy((uint8_t *)buf + bytes_read, cluster_buf + sect_off, to_copy);
|
||||
bytes_read += to_copy;
|
||||
pos += SECTOR_SIZE;
|
||||
}
|
||||
|
||||
cluster = fat32_read_fat(fs, cluster);
|
||||
}
|
||||
|
||||
return (int32_t)bytes_read;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write data to a cluster chain, extending it as needed.
|
||||
*
|
||||
* @param fs Filesystem instance.
|
||||
* @param start Starting cluster (must be valid, >= 2).
|
||||
* @param offset Byte offset to start writing at.
|
||||
* @param size Bytes to write.
|
||||
* @param buf Input data.
|
||||
* @return Bytes written, or -1 on error.
|
||||
*/
|
||||
static int32_t fat32_write_chain(fat32_fs_t *fs, uint32_t start,
|
||||
uint32_t offset, uint32_t size,
|
||||
const void *buf) {
|
||||
if (start < 2) return -1;
|
||||
|
||||
uint32_t bpc = fs->bytes_per_cluster;
|
||||
uint32_t bytes_written = 0;
|
||||
uint32_t cluster = start;
|
||||
uint32_t prev_cluster = 0;
|
||||
uint32_t pos = 0;
|
||||
|
||||
/* Navigate to the cluster containing the offset */
|
||||
while (pos + bpc <= offset) {
|
||||
if (cluster < 2 || cluster >= FAT32_EOC) {
|
||||
/* Need to extend the chain */
|
||||
uint32_t new_cluster = fat32_alloc_cluster(fs);
|
||||
if (new_cluster == 0) return -1;
|
||||
if (prev_cluster >= 2) {
|
||||
fat32_write_fat(fs, prev_cluster, new_cluster);
|
||||
}
|
||||
cluster = new_cluster;
|
||||
}
|
||||
prev_cluster = cluster;
|
||||
pos += bpc;
|
||||
cluster = fat32_read_fat(fs, cluster);
|
||||
}
|
||||
|
||||
/* Ensure current cluster is valid */
|
||||
if (cluster < 2 || cluster >= FAT32_EOC) {
|
||||
uint32_t new_cluster = fat32_alloc_cluster(fs);
|
||||
if (new_cluster == 0) return -1;
|
||||
if (prev_cluster >= 2) {
|
||||
fat32_write_fat(fs, prev_cluster, new_cluster);
|
||||
}
|
||||
cluster = new_cluster;
|
||||
}
|
||||
|
||||
/* Write data sector by sector */
|
||||
uint8_t sector_buf[SECTOR_SIZE];
|
||||
while (bytes_written < size) {
|
||||
if (cluster < 2 || cluster >= FAT32_EOC) {
|
||||
/* Extend chain */
|
||||
uint32_t new_cluster = fat32_alloc_cluster(fs);
|
||||
if (new_cluster == 0) break;
|
||||
if (prev_cluster >= 2) {
|
||||
fat32_write_fat(fs, prev_cluster, new_cluster);
|
||||
}
|
||||
cluster = new_cluster;
|
||||
}
|
||||
|
||||
uint32_t lba = cluster_to_lba(fs, cluster);
|
||||
|
||||
for (uint32_t s = 0; s < fs->sectors_per_cluster && bytes_written < size; s++) {
|
||||
uint32_t sec_start = pos;
|
||||
|
||||
if (sec_start + SECTOR_SIZE <= offset) {
|
||||
pos += SECTOR_SIZE;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Read-modify-write for partial sectors */
|
||||
if (fat32_read_sectors(fs, lba + s, 1, sector_buf) != 0) {
|
||||
return (bytes_written > 0) ? (int32_t)bytes_written : -1;
|
||||
}
|
||||
|
||||
uint32_t sect_off = 0;
|
||||
if (offset > sec_start) {
|
||||
sect_off = offset - sec_start;
|
||||
}
|
||||
uint32_t sect_avail = SECTOR_SIZE - sect_off;
|
||||
uint32_t to_copy = size - bytes_written;
|
||||
if (to_copy > sect_avail) to_copy = sect_avail;
|
||||
|
||||
memcpy(sector_buf + sect_off, (const uint8_t *)buf + bytes_written, to_copy);
|
||||
|
||||
if (fat32_write_sectors(fs, lba + s, 1, sector_buf) != 0) {
|
||||
return (bytes_written > 0) ? (int32_t)bytes_written : -1;
|
||||
}
|
||||
|
||||
bytes_written += to_copy;
|
||||
offset += to_copy; /* Advance offset for subsequent sectors */
|
||||
pos += SECTOR_SIZE;
|
||||
}
|
||||
|
||||
prev_cluster = cluster;
|
||||
pos = pos; /* pos already advanced in the loop */
|
||||
cluster = fat32_read_fat(fs, cluster);
|
||||
}
|
||||
|
||||
return (int32_t)bytes_written;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Directory operations
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* VFS node fs_data encodes the FAT32 context:
|
||||
* - inode field: first cluster of the file/directory
|
||||
* - fs_data: pointer to fat32_fs_t
|
||||
*/
|
||||
|
||||
/**
|
||||
* Convert an 8.3 short filename to a human-readable string.
|
||||
* "HELLO TXT" -> "HELLO.TXT"
|
||||
*/
|
||||
static void sfn_to_name(const uint8_t sfn[11], char *out) {
|
||||
int pos = 0;
|
||||
|
||||
/* Copy base name (bytes 0-7), trimming trailing spaces */
|
||||
int base_end = 7;
|
||||
while (base_end >= 0 && sfn[base_end] == ' ') base_end--;
|
||||
for (int i = 0; i <= base_end; i++) {
|
||||
char c = (char)sfn[i];
|
||||
/* Convert to lowercase for display */
|
||||
if (c >= 'A' && c <= 'Z') c = c - 'A' + 'a';
|
||||
out[pos++] = c;
|
||||
}
|
||||
|
||||
/* Append extension (bytes 8-10) if non-empty */
|
||||
int ext_end = 10;
|
||||
while (ext_end >= 8 && sfn[ext_end] == ' ') ext_end--;
|
||||
if (ext_end >= 8) {
|
||||
out[pos++] = '.';
|
||||
for (int i = 8; i <= ext_end; i++) {
|
||||
char c = (char)sfn[i];
|
||||
if (c >= 'A' && c <= 'Z') c = c - 'A' + 'a';
|
||||
out[pos++] = c;
|
||||
}
|
||||
}
|
||||
|
||||
out[pos] = '\0';
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a human-readable filename to 8.3 short format.
|
||||
* "hello.txt" -> "HELLO TXT"
|
||||
*/
|
||||
static void name_to_sfn(const char *name, uint8_t sfn[11]) {
|
||||
memset(sfn, ' ', 11);
|
||||
|
||||
int pos = 0;
|
||||
int i = 0;
|
||||
|
||||
/* Base name (up to 8 chars) */
|
||||
while (name[i] && name[i] != '.' && pos < 8) {
|
||||
char c = name[i];
|
||||
if (c >= 'a' && c <= 'z') c = c - 'a' + 'A';
|
||||
sfn[pos++] = (uint8_t)c;
|
||||
i++;
|
||||
}
|
||||
|
||||
/* Extension */
|
||||
if (name[i] == '.') {
|
||||
i++;
|
||||
pos = 8;
|
||||
while (name[i] && pos < 11) {
|
||||
char c = name[i];
|
||||
if (c >= 'a' && c <= 'z') c = c - 'a' + 'A';
|
||||
sfn[pos++] = (uint8_t)c;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the first cluster number from a directory entry.
|
||||
*/
|
||||
static uint32_t dirent_cluster(const fat32_dirent_t *de) {
|
||||
return ((uint32_t)de->cluster_hi << 16) | de->cluster_lo;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* VFS operations
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Read file data for a FAT32 file.
|
||||
*/
|
||||
static int32_t fat32_vfs_read(vfs_node_t *node, uint32_t offset,
|
||||
uint32_t size, void *buf) {
|
||||
if (!node || !node->fs_data) return -1;
|
||||
fat32_fs_t *fs = (fat32_fs_t *)node->fs_data;
|
||||
|
||||
uint32_t cluster = node->inode;
|
||||
if (cluster < 2) return -1;
|
||||
|
||||
/* Don't read past file end */
|
||||
if (offset >= node->size) return 0;
|
||||
if (offset + size > node->size) {
|
||||
size = node->size - offset;
|
||||
}
|
||||
|
||||
return fat32_read_chain(fs, cluster, offset, size, buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write file data for a FAT32 file.
|
||||
*/
|
||||
static int32_t fat32_vfs_write(vfs_node_t *node, uint32_t offset,
|
||||
uint32_t size, const void *buf) {
|
||||
if (!node || !node->fs_data) return -1;
|
||||
fat32_fs_t *fs = (fat32_fs_t *)node->fs_data;
|
||||
|
||||
uint32_t cluster = node->inode;
|
||||
if (cluster < 2) return -1;
|
||||
|
||||
int32_t written = fat32_write_chain(fs, cluster, offset, size, buf);
|
||||
|
||||
/* Update file size if we wrote past the end */
|
||||
if (written > 0 && offset + (uint32_t)written > node->size) {
|
||||
node->size = offset + (uint32_t)written;
|
||||
/* TODO: update the directory entry on disk with new size */
|
||||
}
|
||||
|
||||
return written;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read directory entries from a FAT32 directory.
|
||||
*/
|
||||
static int fat32_vfs_readdir(vfs_node_t *dir, uint32_t idx,
|
||||
vfs_dirent_t *out) {
|
||||
if (!dir || !dir->fs_data) return -1;
|
||||
fat32_fs_t *fs = (fat32_fs_t *)dir->fs_data;
|
||||
|
||||
/* inode == 0 means mount root: use the filesystem's root cluster */
|
||||
uint32_t cluster = dir->inode;
|
||||
if (cluster < 2) cluster = fs->root_cluster;
|
||||
if (cluster < 2) return -1;
|
||||
|
||||
/* Read the directory cluster chain and iterate entries.
|
||||
* We read 32-byte entries, skipping deleted (0xE5), LFN, and
|
||||
* volume label entries. idx counts only valid file/dir entries. */
|
||||
uint32_t count = 0;
|
||||
uint32_t byte_pos = 0;
|
||||
|
||||
uint8_t sector_buf[SECTOR_SIZE];
|
||||
uint32_t cur_cluster = cluster;
|
||||
|
||||
while (cur_cluster >= 2 && cur_cluster < FAT32_EOC) {
|
||||
uint32_t lba = cluster_to_lba(fs, cur_cluster);
|
||||
|
||||
for (uint32_t s = 0; s < fs->sectors_per_cluster; s++) {
|
||||
if (fat32_read_sectors(fs, lba + s, 1, sector_buf) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Each sector has 512/32 = 16 directory entries */
|
||||
for (uint32_t e = 0; e < SECTOR_SIZE / 32; e++) {
|
||||
fat32_dirent_t *de = (fat32_dirent_t *)(sector_buf + e * 32);
|
||||
|
||||
/* 0x00 = end of directory */
|
||||
if (de->name[0] == 0x00) return -1;
|
||||
|
||||
/* 0xE5 = deleted entry */
|
||||
if (de->name[0] == 0xE5) continue;
|
||||
|
||||
/* Skip LFN entries */
|
||||
if ((de->attr & FAT32_ATTR_LFN) == FAT32_ATTR_LFN) continue;
|
||||
|
||||
/* Skip volume label */
|
||||
if (de->attr & FAT32_ATTR_VOLUME_ID) continue;
|
||||
|
||||
/* Skip . and .. entries */
|
||||
if (de->name[0] == '.' &&
|
||||
(de->name[1] == ' ' || de->name[1] == '.')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (count == idx) {
|
||||
memset(out, 0, sizeof(vfs_dirent_t));
|
||||
sfn_to_name(de->name, out->name);
|
||||
out->type = (de->attr & FAT32_ATTR_DIRECTORY)
|
||||
? VFS_DIRECTORY : VFS_FILE;
|
||||
out->inode = dirent_cluster(de);
|
||||
return 0;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
cur_cluster = fat32_read_fat(fs, cur_cluster);
|
||||
}
|
||||
|
||||
return -1; /* No more entries */
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up a child by name in a FAT32 directory.
|
||||
*/
|
||||
static int fat32_vfs_finddir(vfs_node_t *dir, const char *name,
|
||||
vfs_node_t *out) {
|
||||
if (!dir || !dir->fs_data) return -1;
|
||||
fat32_fs_t *fs = (fat32_fs_t *)dir->fs_data;
|
||||
|
||||
/* inode == 0 means mount root: use the filesystem's root cluster */
|
||||
uint32_t cluster = dir->inode;
|
||||
if (cluster < 2) cluster = fs->root_cluster;
|
||||
if (cluster < 2) return -1;
|
||||
|
||||
/* Convert search name to 8.3 for comparison */
|
||||
uint8_t search_sfn[11];
|
||||
name_to_sfn(name, search_sfn);
|
||||
|
||||
uint8_t sector_buf[SECTOR_SIZE];
|
||||
uint32_t cur_cluster = cluster;
|
||||
|
||||
while (cur_cluster >= 2 && cur_cluster < FAT32_EOC) {
|
||||
uint32_t lba = cluster_to_lba(fs, cur_cluster);
|
||||
|
||||
for (uint32_t s = 0; s < fs->sectors_per_cluster; s++) {
|
||||
if (fat32_read_sectors(fs, lba + s, 1, sector_buf) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (uint32_t e = 0; e < SECTOR_SIZE / 32; e++) {
|
||||
fat32_dirent_t *de = (fat32_dirent_t *)(sector_buf + e * 32);
|
||||
|
||||
if (de->name[0] == 0x00) return -1;
|
||||
if (de->name[0] == 0xE5) continue;
|
||||
if ((de->attr & FAT32_ATTR_LFN) == FAT32_ATTR_LFN) continue;
|
||||
if (de->attr & FAT32_ATTR_VOLUME_ID) continue;
|
||||
|
||||
/* Compare both 8.3 format and human-readable format */
|
||||
if (memcmp(de->name, search_sfn, 11) == 0) {
|
||||
/* Match by 8.3 name */
|
||||
goto found;
|
||||
}
|
||||
|
||||
/* Also compare with the lowercase display name */
|
||||
char display[13];
|
||||
sfn_to_name(de->name, display);
|
||||
if (strcmp(display, name) == 0) {
|
||||
goto found;
|
||||
}
|
||||
continue;
|
||||
|
||||
found:
|
||||
memset(out, 0, sizeof(vfs_node_t));
|
||||
sfn_to_name(de->name, out->name);
|
||||
out->inode = dirent_cluster(de);
|
||||
out->size = de->file_size;
|
||||
out->type = (de->attr & FAT32_ATTR_DIRECTORY)
|
||||
? VFS_DIRECTORY : VFS_FILE;
|
||||
out->fs_data = fs;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
cur_cluster = fat32_read_fat(fs, cur_cluster);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/** VFS operations for FAT32. */
|
||||
static vfs_fs_ops_t fat32_vfs_ops = {
|
||||
.open = NULL,
|
||||
.close = NULL,
|
||||
.read = fat32_vfs_read,
|
||||
.write = fat32_vfs_write,
|
||||
.readdir = fat32_vfs_readdir,
|
||||
.finddir = fat32_vfs_finddir,
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* Mount
|
||||
* ================================================================ */
|
||||
|
||||
int fat32_mount_device(devicefs_device_t *dev, const char *mount_path) {
|
||||
if (!dev || !mount_path) return -1;
|
||||
if (fat32_mount_count >= FAT32_MAX_MOUNTS) {
|
||||
offset_print(" FAT32: too many mounts\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fat32_fs_t *fs = &fat32_mounts[fat32_mount_count];
|
||||
memset(fs, 0, sizeof(fat32_fs_t));
|
||||
fs->dev = dev;
|
||||
|
||||
/* Read the boot sector / BPB */
|
||||
uint8_t boot_sector[SECTOR_SIZE];
|
||||
if (fat32_read_sectors(fs, 0, 1, boot_sector) != 0) {
|
||||
offset_print(" FAT32: failed to read boot sector\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Copy BPB */
|
||||
memcpy(&fs->bpb, boot_sector, sizeof(fat32_bpb_t));
|
||||
|
||||
/* Validate */
|
||||
if (fs->bpb.bytes_per_sector != 512) {
|
||||
offset_print(" FAT32: unsupported sector size\n");
|
||||
return -1;
|
||||
}
|
||||
if (fs->bpb.fat_size_32 == 0) {
|
||||
offset_print(" FAT32: fat_size_32 is 0, not FAT32\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Check boot sector signature */
|
||||
if (boot_sector[510] != 0x55 || boot_sector[511] != 0xAA) {
|
||||
offset_print(" FAT32: invalid boot sector signature\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Compute layout */
|
||||
fs->sectors_per_cluster = fs->bpb.sectors_per_cluster;
|
||||
fs->bytes_per_cluster = fs->sectors_per_cluster * SECTOR_SIZE;
|
||||
fs->fat_start_lba = fs->bpb.reserved_sectors;
|
||||
fs->data_start_lba = fs->fat_start_lba +
|
||||
(fs->bpb.num_fats * fs->bpb.fat_size_32);
|
||||
fs->root_cluster = fs->bpb.root_cluster;
|
||||
|
||||
uint32_t total_sectors = fs->bpb.total_sectors_32;
|
||||
if (total_sectors == 0) total_sectors = fs->bpb.total_sectors_16;
|
||||
uint32_t data_sectors = total_sectors - fs->data_start_lba;
|
||||
fs->total_clusters = data_sectors / fs->sectors_per_cluster;
|
||||
|
||||
offset_print(" FAT32: mounted ");
|
||||
offset_print(mount_path);
|
||||
offset_print(" (");
|
||||
print_hex(fs->total_clusters);
|
||||
offset_print(" clusters, ");
|
||||
print_hex(fs->sectors_per_cluster);
|
||||
offset_print(" sec/clust)\n");
|
||||
|
||||
/* Register with VFS */
|
||||
if (vfs_mount(mount_path, &fat32_vfs_ops, fs) != 0) {
|
||||
offset_print(" FAT32: VFS mount failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
fat32_mount_count++;
|
||||
return 0;
|
||||
}
|
||||
116
src/fat32.h
Normal file
116
src/fat32.h
Normal file
@@ -0,0 +1,116 @@
|
||||
/**
|
||||
* @file fat32.h
|
||||
* @brief FAT32 filesystem driver.
|
||||
*
|
||||
* Provides a VFS driver for reading and writing FAT32 filesystems
|
||||
* on block devices. Supports long filenames (LFN), directory traversal,
|
||||
* file read/write, and file creation.
|
||||
*
|
||||
* Usage:
|
||||
* fat32_mount_device(dev, mount_path);
|
||||
*
|
||||
* This reads the BPB from sector 0 of the device, validates the FAT32
|
||||
* signature, and mounts the filesystem at the specified VFS path.
|
||||
*/
|
||||
|
||||
#ifndef FAT32_H
|
||||
#define FAT32_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "devicefs.h"
|
||||
|
||||
/** FAT32 cluster chain end marker. */
|
||||
#define FAT32_EOC 0x0FFFFFF8
|
||||
#define FAT32_FREE 0x00000000
|
||||
#define FAT32_BAD 0x0FFFFFF7
|
||||
|
||||
/** Maximum filename length (8.3 short name). */
|
||||
#define FAT32_SFN_LEN 11
|
||||
|
||||
/** Directory entry attributes. */
|
||||
#define FAT32_ATTR_READ_ONLY 0x01
|
||||
#define FAT32_ATTR_HIDDEN 0x02
|
||||
#define FAT32_ATTR_SYSTEM 0x04
|
||||
#define FAT32_ATTR_VOLUME_ID 0x08
|
||||
#define FAT32_ATTR_DIRECTORY 0x10
|
||||
#define FAT32_ATTR_ARCHIVE 0x20
|
||||
#define FAT32_ATTR_LFN (FAT32_ATTR_READ_ONLY | FAT32_ATTR_HIDDEN | \
|
||||
FAT32_ATTR_SYSTEM | FAT32_ATTR_VOLUME_ID)
|
||||
|
||||
/**
|
||||
* FAT32 BIOS Parameter Block (BPB) — packed, read directly from disk.
|
||||
* Covers the first 90 bytes of the boot sector.
|
||||
*/
|
||||
typedef struct __attribute__((packed)) fat32_bpb {
|
||||
uint8_t jmp_boot[3]; /**< Jump instruction. */
|
||||
uint8_t oem_name[8]; /**< OEM identifier. */
|
||||
uint16_t bytes_per_sector; /**< Usually 512. */
|
||||
uint8_t sectors_per_cluster;
|
||||
uint16_t reserved_sectors; /**< Sectors before the first FAT. */
|
||||
uint8_t num_fats; /**< Number of FATs (usually 2). */
|
||||
uint16_t root_entry_count; /**< 0 for FAT32. */
|
||||
uint16_t total_sectors_16; /**< 0 for FAT32. */
|
||||
uint8_t media_type;
|
||||
uint16_t fat_size_16; /**< 0 for FAT32. */
|
||||
uint16_t sectors_per_track;
|
||||
uint16_t num_heads;
|
||||
uint32_t hidden_sectors;
|
||||
uint32_t total_sectors_32;
|
||||
/* FAT32 extended fields */
|
||||
uint32_t fat_size_32; /**< Sectors per FAT. */
|
||||
uint16_t ext_flags;
|
||||
uint16_t fs_version;
|
||||
uint32_t root_cluster; /**< First cluster of root directory (usually 2). */
|
||||
uint16_t fs_info; /**< Sector number of FSInfo structure. */
|
||||
uint16_t backup_boot; /**< Sector of backup boot sector. */
|
||||
uint8_t reserved[12];
|
||||
uint8_t drive_number;
|
||||
uint8_t reserved1;
|
||||
uint8_t boot_sig; /**< 0x29 if next 3 fields are valid. */
|
||||
uint32_t volume_id;
|
||||
uint8_t volume_label[11];
|
||||
uint8_t fs_type[8]; /**< "FAT32 " */
|
||||
} fat32_bpb_t;
|
||||
|
||||
/**
|
||||
* FAT32 directory entry (32 bytes, packed).
|
||||
*/
|
||||
typedef struct __attribute__((packed)) fat32_dirent {
|
||||
uint8_t name[11]; /**< 8.3 short name. */
|
||||
uint8_t attr; /**< File attributes. */
|
||||
uint8_t nt_reserved; /**< Reserved (case info). */
|
||||
uint8_t create_time_tenth; /**< Creation time in tenths of second. */
|
||||
uint16_t create_time; /**< Creation time. */
|
||||
uint16_t create_date; /**< Creation date. */
|
||||
uint16_t access_date; /**< Last access date. */
|
||||
uint16_t cluster_hi; /**< High 16 bits of first cluster. */
|
||||
uint16_t write_time; /**< Last write time. */
|
||||
uint16_t write_date; /**< Last write date. */
|
||||
uint16_t cluster_lo; /**< Low 16 bits of first cluster. */
|
||||
uint32_t file_size; /**< File size in bytes. */
|
||||
} fat32_dirent_t;
|
||||
|
||||
/**
|
||||
* FAT32 filesystem instance (per-mount context).
|
||||
*/
|
||||
typedef struct fat32_fs {
|
||||
devicefs_device_t *dev; /**< Underlying block device. */
|
||||
fat32_bpb_t bpb; /**< Cached BPB. */
|
||||
uint32_t fat_start_lba; /**< First sector of the FAT. */
|
||||
uint32_t data_start_lba; /**< First sector of the data region. */
|
||||
uint32_t sectors_per_cluster;
|
||||
uint32_t bytes_per_cluster;
|
||||
uint32_t root_cluster; /**< Root directory first cluster. */
|
||||
uint32_t total_clusters;
|
||||
} fat32_fs_t;
|
||||
|
||||
/**
|
||||
* Mount a FAT32 filesystem from a block device.
|
||||
*
|
||||
* @param dev The devicefs block device to read from.
|
||||
* @param mount_path Where to mount in the VFS (e.g., "/mnt").
|
||||
* @return 0 on success, -1 on error.
|
||||
*/
|
||||
int fat32_mount_device(devicefs_device_t *dev, const char *mount_path);
|
||||
|
||||
#endif /* FAT32_H */
|
||||
688
src/floppy.c
Normal file
688
src/floppy.c
Normal file
@@ -0,0 +1,688 @@
|
||||
/**
|
||||
* @file floppy.c
|
||||
* @brief Floppy disk controller driver implementation.
|
||||
*
|
||||
* Drives the Intel 82077AA-compatible floppy disk controller using
|
||||
* ISA DMA channel 2 for data transfers and IRQ 6 for completion
|
||||
* notification.
|
||||
*
|
||||
* Supports 1.44 MB 3.5" HD floppies. The driver detects drives via
|
||||
* CMOS register 0x10, initializes the controller, and registers each
|
||||
* detected drive with the devicefs subsystem as a "floppy" class
|
||||
* block device (floppy1, floppy2, ...).
|
||||
*
|
||||
* LBA addressing is converted to CHS for the floppy controller.
|
||||
*/
|
||||
|
||||
#include "floppy.h"
|
||||
#include "port_io.h"
|
||||
#include "pic.h"
|
||||
#include "devicefs.h"
|
||||
#include "driver.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Debug print helpers defined in kernel.c */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/* ================================================================
|
||||
* Global state
|
||||
* ================================================================ */
|
||||
|
||||
/** Detected floppy drives. */
|
||||
static floppy_drive_t floppy_drives[FLOPPY_MAX_DRIVES];
|
||||
|
||||
/** Volatile flag set by IRQ 6 handler. */
|
||||
static volatile int floppy_irq_received = 0;
|
||||
|
||||
/** DMA bounce buffer — must be in the first 16 MB of physical memory
|
||||
* and must not cross a 64 KB boundary. We use a static buffer that
|
||||
* the linker places in the kernel BSS (which lives in low memory). */
|
||||
static uint8_t dma_buffer[FLOPPY_SECTOR_SIZE] __attribute__((aligned(512)));
|
||||
|
||||
/* ================================================================
|
||||
* Low-level helpers
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Read CMOS register.
|
||||
*
|
||||
* @param reg CMOS register address (0x00-0x7F).
|
||||
* @return Register value.
|
||||
*/
|
||||
static uint8_t cmos_read(uint8_t reg) {
|
||||
outb(0x70, reg);
|
||||
/* Small delay for CMOS to respond */
|
||||
inb(0x80);
|
||||
return inb(0x71);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for IRQ 6 with timeout.
|
||||
* The floppy controller fires IRQ 6 on command completion.
|
||||
*
|
||||
* If interrupts are currently disabled (e.g. during early init before
|
||||
* the kernel calls STI), we temporarily enable them so IRQ 6 can
|
||||
* actually fire, then restore the previous interrupt state.
|
||||
*
|
||||
* @return 0 on success (IRQ received), -1 on timeout.
|
||||
*/
|
||||
static int fdc_wait_irq(void) {
|
||||
/* Check whether interrupts are currently enabled (IF flag) */
|
||||
uint32_t eflags;
|
||||
asm volatile("pushfl; popl %0" : "=r"(eflags));
|
||||
int need_sti = !(eflags & 0x200);
|
||||
|
||||
if (need_sti) {
|
||||
asm volatile("sti");
|
||||
}
|
||||
|
||||
int timeout = 5000000;
|
||||
while (!floppy_irq_received && --timeout > 0) {
|
||||
/* Busy wait — in a real OS we'd use a proper sleep/wait */
|
||||
asm volatile("pause");
|
||||
}
|
||||
|
||||
if (need_sti) {
|
||||
asm volatile("cli");
|
||||
}
|
||||
|
||||
if (timeout == 0) {
|
||||
offset_print(" FLOPPY: IRQ timeout\n");
|
||||
return -1;
|
||||
}
|
||||
floppy_irq_received = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a byte to the FDC FIFO register.
|
||||
* Waits for the controller to be ready (MSR RQM=1, DIO=0).
|
||||
*
|
||||
* @param val Byte to write.
|
||||
* @return 0 on success, -1 on timeout.
|
||||
*/
|
||||
static int fdc_write_byte(uint8_t val) {
|
||||
int timeout = 500000;
|
||||
while (--timeout > 0) {
|
||||
uint8_t msr = inb(FDC_MSR);
|
||||
if ((msr & (MSR_RQM | MSR_DIO)) == MSR_RQM) {
|
||||
outb(FDC_FIFO, val);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
offset_print(" FLOPPY: write timeout\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a byte from the FDC FIFO register.
|
||||
* Waits for the controller to have data ready (MSR RQM=1, DIO=1).
|
||||
*
|
||||
* @return Byte read, or -1 on timeout.
|
||||
*/
|
||||
static int fdc_read_byte(void) {
|
||||
int timeout = 500000;
|
||||
while (--timeout > 0) {
|
||||
uint8_t msr = inb(FDC_MSR);
|
||||
if ((msr & (MSR_RQM | MSR_DIO)) == (MSR_RQM | MSR_DIO)) {
|
||||
return (int)inb(FDC_FIFO);
|
||||
}
|
||||
}
|
||||
offset_print(" FLOPPY: read timeout\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Issue a SENSE INTERRUPT command and read the result.
|
||||
*
|
||||
* @param st0_out Output: ST0 byte.
|
||||
* @param cyl_out Output: current cylinder.
|
||||
*/
|
||||
static void fdc_sense_interrupt(uint8_t *st0_out, uint8_t *cyl_out) {
|
||||
fdc_write_byte(FDC_CMD_SENSE_INT);
|
||||
int st0 = fdc_read_byte();
|
||||
int cyl = fdc_read_byte();
|
||||
if (st0_out) *st0_out = (uint8_t)(st0 >= 0 ? st0 : 0xFF);
|
||||
if (cyl_out) *cyl_out = (uint8_t)(cyl >= 0 ? cyl : 0xFF);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Motor control
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Turn the motor on for a drive.
|
||||
*
|
||||
* @param drive Drive number (0 or 1).
|
||||
*/
|
||||
static void fdc_motor_on(uint8_t drive) {
|
||||
if (drive >= FLOPPY_MAX_DRIVES) return;
|
||||
if (floppy_drives[drive].motor_on) return;
|
||||
|
||||
uint8_t dor = drive | DOR_RESET | DOR_DMA_IRQ |
|
||||
(DOR_MOTOR_A << drive);
|
||||
outb(FDC_DOR, dor);
|
||||
|
||||
/* Wait ~500 ms for motor spin-up (busy wait in timer ticks) */
|
||||
for (volatile int i = 0; i < 2000000; i++) {
|
||||
asm volatile("pause");
|
||||
}
|
||||
floppy_drives[drive].motor_on = 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn the motor off for a drive.
|
||||
*
|
||||
* @param drive Drive number (0 or 1).
|
||||
*/
|
||||
static void fdc_motor_off(uint8_t drive) {
|
||||
if (drive >= FLOPPY_MAX_DRIVES) return;
|
||||
uint8_t dor = drive | DOR_RESET | DOR_DMA_IRQ;
|
||||
outb(FDC_DOR, dor);
|
||||
floppy_drives[drive].motor_on = 0;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* DMA setup for ISA DMA channel 2
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Set up ISA DMA channel 2 for a floppy transfer.
|
||||
*
|
||||
* The floppy controller uses DMA channel 2. We configure the DMA
|
||||
* controller with the physical address of our bounce buffer and the
|
||||
* transfer length.
|
||||
*
|
||||
* @param addr Physical address of the DMA buffer (must be < 16 MB).
|
||||
* @param len Transfer length in bytes (minus 1 is written to count).
|
||||
* @param read 1 for read (disk→memory), 0 for write (memory→disk).
|
||||
*/
|
||||
static void dma_setup(uint32_t addr, uint16_t len, int read) {
|
||||
/* Mask DMA channel 2 */
|
||||
outb(0x0A, 0x06);
|
||||
|
||||
/* Reset flip-flop */
|
||||
outb(0x0C, 0xFF);
|
||||
|
||||
/* Mode: single transfer, auto-init disabled, increment, channel 2
|
||||
* Read = 0x46 (single, addr incr, write-to-memory = FDC read)
|
||||
* Write = 0x4A (single, addr incr, read-from-memory = FDC write) */
|
||||
outb(0x0B, read ? 0x46 : 0x4A);
|
||||
|
||||
/* Address (low 16 bits via channel 2 address reg 0x04) */
|
||||
outb(0x04, (uint8_t)(addr & 0xFF));
|
||||
outb(0x04, (uint8_t)((addr >> 8) & 0xFF));
|
||||
|
||||
/* Page register for channel 2 (port 0x81) — bits 16-23 of address */
|
||||
outb(0x81, (uint8_t)((addr >> 16) & 0xFF));
|
||||
|
||||
/* Count (len - 1, low byte first) */
|
||||
outb(0x05, (uint8_t)((len - 1) & 0xFF));
|
||||
outb(0x05, (uint8_t)(((len - 1) >> 8) & 0xFF));
|
||||
|
||||
/* Unmask DMA channel 2 */
|
||||
outb(0x0A, 0x02);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* CHS conversion
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Convert an LBA address to CHS for a 1.44 MB floppy.
|
||||
*
|
||||
* @param lba Logical block address.
|
||||
* @param cyl_out Output: cylinder number.
|
||||
* @param head_out Output: head number.
|
||||
* @param sec_out Output: sector number (1-based).
|
||||
*/
|
||||
static void lba_to_chs(uint32_t lba, uint8_t *cyl_out,
|
||||
uint8_t *head_out, uint8_t *sec_out) {
|
||||
*cyl_out = (uint8_t)(lba / (FLOPPY_HEADS * FLOPPY_SECTORS_PER_TRACK));
|
||||
uint32_t tmp = lba % (FLOPPY_HEADS * FLOPPY_SECTORS_PER_TRACK);
|
||||
*head_out = (uint8_t)(tmp / FLOPPY_SECTORS_PER_TRACK);
|
||||
*sec_out = (uint8_t)((tmp % FLOPPY_SECTORS_PER_TRACK) + 1);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Seek and calibrate
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Recalibrate a drive (seek to track 0).
|
||||
*
|
||||
* @param drive Drive number.
|
||||
* @return 0 on success, -1 on failure.
|
||||
*/
|
||||
static int fdc_recalibrate(uint8_t drive) {
|
||||
floppy_irq_received = 0;
|
||||
fdc_write_byte(FDC_CMD_RECALIBRATE);
|
||||
fdc_write_byte(drive);
|
||||
|
||||
if (fdc_wait_irq() != 0) return -1;
|
||||
|
||||
uint8_t st0, cyl;
|
||||
fdc_sense_interrupt(&st0, &cyl);
|
||||
|
||||
if (cyl != 0) {
|
||||
offset_print(" FLOPPY: recalibrate failed, cyl=");
|
||||
print_hex(cyl);
|
||||
offset_print("\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
floppy_drives[drive].current_track = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Seek to a specific cylinder.
|
||||
*
|
||||
* @param drive Drive number.
|
||||
* @param cyl Target cylinder.
|
||||
* @return 0 on success, -1 on failure.
|
||||
*/
|
||||
static int fdc_seek(uint8_t drive, uint8_t cyl) {
|
||||
if (floppy_drives[drive].current_track == cyl) return 0;
|
||||
|
||||
floppy_irq_received = 0;
|
||||
fdc_write_byte(FDC_CMD_SEEK);
|
||||
fdc_write_byte((0 << 2) | drive); /* Head 0, drive N */
|
||||
fdc_write_byte(cyl);
|
||||
|
||||
if (fdc_wait_irq() != 0) return -1;
|
||||
|
||||
uint8_t st0, res_cyl;
|
||||
fdc_sense_interrupt(&st0, &res_cyl);
|
||||
|
||||
if (res_cyl != cyl) {
|
||||
offset_print(" FLOPPY: seek failed, wanted cyl=");
|
||||
print_hex(cyl);
|
||||
offset_print(" got=");
|
||||
print_hex(res_cyl);
|
||||
offset_print("\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
floppy_drives[drive].current_track = cyl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Read / Write sector
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Read a single sector from a floppy drive using DMA.
|
||||
*
|
||||
* @param drive Drive number (0 or 1).
|
||||
* @param lba Logical sector address.
|
||||
* @param buf Output buffer (512 bytes).
|
||||
* @return 0 on success, -1 on failure.
|
||||
*/
|
||||
static int fdc_read_sector(uint8_t drive, uint32_t lba, void *buf) {
|
||||
if (lba >= FLOPPY_TOTAL_SECTORS) return -1;
|
||||
|
||||
uint8_t cyl, head, sec;
|
||||
lba_to_chs(lba, &cyl, &head, &sec);
|
||||
|
||||
fdc_motor_on(drive);
|
||||
|
||||
/* Seek to the correct cylinder */
|
||||
if (fdc_seek(drive, cyl) != 0) {
|
||||
fdc_motor_off(drive);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Set up DMA for read (disk → memory) */
|
||||
uint32_t dma_addr = (uint32_t)dma_buffer;
|
||||
dma_setup(dma_addr, FLOPPY_SECTOR_SIZE, 1);
|
||||
|
||||
/* Issue READ DATA command */
|
||||
floppy_irq_received = 0;
|
||||
fdc_write_byte(FDC_CMD_READ_DATA);
|
||||
fdc_write_byte((head << 2) | drive);
|
||||
fdc_write_byte(cyl);
|
||||
fdc_write_byte(head);
|
||||
fdc_write_byte(sec);
|
||||
fdc_write_byte(2); /* Sector size code: 2 = 512 bytes */
|
||||
fdc_write_byte(FLOPPY_SECTORS_PER_TRACK); /* End of track */
|
||||
fdc_write_byte(0x1B); /* GPL: gap length */
|
||||
fdc_write_byte(0xFF); /* DTL: unused when sector size != 0 */
|
||||
|
||||
/* Wait for command completion */
|
||||
if (fdc_wait_irq() != 0) {
|
||||
fdc_motor_off(drive);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Read result phase (7 bytes) */
|
||||
uint8_t st0 = (uint8_t)fdc_read_byte();
|
||||
uint8_t st1 = (uint8_t)fdc_read_byte();
|
||||
uint8_t st2 = (uint8_t)fdc_read_byte();
|
||||
(void)fdc_read_byte(); /* C */
|
||||
(void)fdc_read_byte(); /* H */
|
||||
(void)fdc_read_byte(); /* R */
|
||||
(void)fdc_read_byte(); /* N */
|
||||
|
||||
/* Check for errors */
|
||||
if ((st0 & 0xC0) != 0 || st1 != 0 || st2 != 0) {
|
||||
offset_print(" FLOPPY: read error st0=");
|
||||
print_hex(st0);
|
||||
offset_print(" st1=");
|
||||
print_hex(st1);
|
||||
offset_print(" st2=");
|
||||
print_hex(st2);
|
||||
offset_print("\n");
|
||||
fdc_motor_off(drive);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Copy from DMA buffer to caller's buffer */
|
||||
memcpy(buf, dma_buffer, FLOPPY_SECTOR_SIZE);
|
||||
|
||||
fdc_motor_off(drive);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a single sector to a floppy drive using DMA.
|
||||
*
|
||||
* @param drive Drive number (0 or 1).
|
||||
* @param lba Logical sector address.
|
||||
* @param buf Input buffer (512 bytes).
|
||||
* @return 0 on success, -1 on failure.
|
||||
*/
|
||||
static int fdc_write_sector(uint8_t drive, uint32_t lba, const void *buf) {
|
||||
if (lba >= FLOPPY_TOTAL_SECTORS) return -1;
|
||||
|
||||
uint8_t cyl, head, sec;
|
||||
lba_to_chs(lba, &cyl, &head, &sec);
|
||||
|
||||
fdc_motor_on(drive);
|
||||
|
||||
if (fdc_seek(drive, cyl) != 0) {
|
||||
fdc_motor_off(drive);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Copy data to DMA buffer */
|
||||
memcpy(dma_buffer, buf, FLOPPY_SECTOR_SIZE);
|
||||
|
||||
/* Set up DMA for write (memory → disk) */
|
||||
uint32_t dma_addr = (uint32_t)dma_buffer;
|
||||
dma_setup(dma_addr, FLOPPY_SECTOR_SIZE, 0);
|
||||
|
||||
/* Issue WRITE DATA command */
|
||||
floppy_irq_received = 0;
|
||||
fdc_write_byte(FDC_CMD_WRITE_DATA);
|
||||
fdc_write_byte((head << 2) | drive);
|
||||
fdc_write_byte(cyl);
|
||||
fdc_write_byte(head);
|
||||
fdc_write_byte(sec);
|
||||
fdc_write_byte(2); /* Sector size code: 512 bytes */
|
||||
fdc_write_byte(FLOPPY_SECTORS_PER_TRACK);
|
||||
fdc_write_byte(0x1B);
|
||||
fdc_write_byte(0xFF);
|
||||
|
||||
if (fdc_wait_irq() != 0) {
|
||||
fdc_motor_off(drive);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Read result phase */
|
||||
uint8_t st0 = (uint8_t)fdc_read_byte();
|
||||
uint8_t st1 = (uint8_t)fdc_read_byte();
|
||||
uint8_t st2 = (uint8_t)fdc_read_byte();
|
||||
(void)fdc_read_byte();
|
||||
(void)fdc_read_byte();
|
||||
(void)fdc_read_byte();
|
||||
(void)fdc_read_byte();
|
||||
|
||||
if ((st0 & 0xC0) != 0 || st1 != 0 || st2 != 0) {
|
||||
offset_print(" FLOPPY: write error st0=");
|
||||
print_hex(st0);
|
||||
offset_print(" st1=");
|
||||
print_hex(st1);
|
||||
offset_print(" st2=");
|
||||
print_hex(st2);
|
||||
offset_print("\n");
|
||||
fdc_motor_off(drive);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fdc_motor_off(drive);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Devicefs block operations
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Read sectors from a floppy drive.
|
||||
* dev_data is a pointer to the floppy_drive_t.
|
||||
*/
|
||||
static int floppy_block_read(void *dev_data, uint32_t lba,
|
||||
uint32_t count, void *buf) {
|
||||
floppy_drive_t *drv = (floppy_drive_t *)dev_data;
|
||||
if (!drv || !drv->present) return -1;
|
||||
|
||||
uint8_t *p = (uint8_t *)buf;
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
if (fdc_read_sector(drv->drive_num, lba + i, p) != 0) {
|
||||
return -1;
|
||||
}
|
||||
p += FLOPPY_SECTOR_SIZE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write sectors to a floppy drive.
|
||||
*/
|
||||
static int floppy_block_write(void *dev_data, uint32_t lba,
|
||||
uint32_t count, const void *buf) {
|
||||
floppy_drive_t *drv = (floppy_drive_t *)dev_data;
|
||||
if (!drv || !drv->present) return -1;
|
||||
|
||||
const uint8_t *p = (const uint8_t *)buf;
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
if (fdc_write_sector(drv->drive_num, lba + i, p) != 0) {
|
||||
return -1;
|
||||
}
|
||||
p += FLOPPY_SECTOR_SIZE;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get floppy sector size (always 512).
|
||||
*/
|
||||
static uint32_t floppy_block_sector_size(void *dev_data) {
|
||||
(void)dev_data;
|
||||
return FLOPPY_SECTOR_SIZE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get total sector count for a 1.44 MB floppy.
|
||||
*/
|
||||
static uint32_t floppy_block_sector_count(void *dev_data) {
|
||||
(void)dev_data;
|
||||
return FLOPPY_TOTAL_SECTORS;
|
||||
}
|
||||
|
||||
/** Block device operations for floppy drives. */
|
||||
static devicefs_block_ops_t floppy_block_ops = {
|
||||
.read_sectors = floppy_block_read,
|
||||
.write_sectors = floppy_block_write,
|
||||
.sector_size = floppy_block_sector_size,
|
||||
.sector_count = floppy_block_sector_count,
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* Controller initialization
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Reset the floppy disk controller.
|
||||
*
|
||||
* Toggles the RESET bit in the DOR and re-initializes the controller.
|
||||
*
|
||||
* @return 0 on success, -1 on failure.
|
||||
*/
|
||||
static int fdc_reset(void) {
|
||||
/* Enter reset state */
|
||||
outb(FDC_DOR, 0x00);
|
||||
|
||||
/* Small delay */
|
||||
for (volatile int i = 0; i < 100000; i++) {
|
||||
asm volatile("pause");
|
||||
}
|
||||
|
||||
/* Exit reset: enable DMA/IRQ, select drive 0, motor off */
|
||||
floppy_irq_received = 0;
|
||||
outb(FDC_DOR, DOR_RESET | DOR_DMA_IRQ);
|
||||
|
||||
/* Controller generates IRQ 6 after reset */
|
||||
if (fdc_wait_irq() != 0) {
|
||||
offset_print(" FLOPPY: no IRQ after reset\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Sense interrupt for each of the 4 possible drives */
|
||||
for (int i = 0; i < 4; i++) {
|
||||
uint8_t st0, cyl;
|
||||
fdc_sense_interrupt(&st0, &cyl);
|
||||
}
|
||||
|
||||
/* Set data rate to 500 kbps (for 1.44 MB HD floppies) */
|
||||
outb(FDC_CCR, 0x00);
|
||||
|
||||
/* SPECIFY command: step rate = 3 ms, head unload = 240 ms,
|
||||
* head load = 16 ms, non-DMA mode = 0 */
|
||||
fdc_write_byte(FDC_CMD_SPECIFY);
|
||||
fdc_write_byte(0xDF); /* SRT=3ms, HUT=240ms */
|
||||
fdc_write_byte(0x02); /* HLT=16ms, NDMA=0 */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Driver framework integration
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Probe for floppy controller.
|
||||
* Reads CMOS to determine if any floppy drives are configured.
|
||||
*/
|
||||
static driver_probe_result_t floppy_probe(void) {
|
||||
uint8_t cmos_floppy = cmos_read(0x10);
|
||||
uint8_t drive_a = (cmos_floppy >> 4) & 0x0F;
|
||||
uint8_t drive_b = cmos_floppy & 0x0F;
|
||||
|
||||
if (drive_a == CMOS_FLOPPY_NONE && drive_b == CMOS_FLOPPY_NONE) {
|
||||
offset_print(" FLOPPY: no drives in CMOS\n");
|
||||
return DRIVER_PROBE_NOT_FOUND;
|
||||
}
|
||||
|
||||
return DRIVER_PROBE_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the floppy controller and register detected drives.
|
||||
*/
|
||||
static int floppy_driver_init(void) {
|
||||
memset(floppy_drives, 0, sizeof(floppy_drives));
|
||||
|
||||
/* Read CMOS for drive types */
|
||||
uint8_t cmos_floppy = cmos_read(0x10);
|
||||
uint8_t types[2] = {
|
||||
(cmos_floppy >> 4) & 0x0F,
|
||||
cmos_floppy & 0x0F,
|
||||
};
|
||||
|
||||
/* Unmask IRQ 6 in the PIC */
|
||||
pic_clear_mask(6);
|
||||
|
||||
/* Reset the controller */
|
||||
if (fdc_reset() != 0) {
|
||||
offset_print(" FLOPPY: controller reset failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Register each detected drive */
|
||||
int found = 0;
|
||||
for (int i = 0; i < 2; i++) {
|
||||
if (types[i] == CMOS_FLOPPY_NONE) continue;
|
||||
|
||||
/* We only fully support 1.44 MB for now, but register others too */
|
||||
floppy_drives[i].drive_num = (uint8_t)i;
|
||||
floppy_drives[i].cmos_type = types[i];
|
||||
floppy_drives[i].present = 1;
|
||||
floppy_drives[i].motor_on = 0;
|
||||
floppy_drives[i].current_track = 0xFF; /* Unknown */
|
||||
|
||||
const char *type_str;
|
||||
switch (types[i]) {
|
||||
case CMOS_FLOPPY_360K: type_str = "360K 5.25\""; break;
|
||||
case CMOS_FLOPPY_1200K: type_str = "1.2M 5.25\""; break;
|
||||
case CMOS_FLOPPY_720K: type_str = "720K 3.5\""; break;
|
||||
case CMOS_FLOPPY_1440K: type_str = "1.44M 3.5\""; break;
|
||||
case CMOS_FLOPPY_2880K: type_str = "2.88M 3.5\""; break;
|
||||
default: type_str = "unknown"; break;
|
||||
}
|
||||
|
||||
offset_print(" FLOPPY: drive ");
|
||||
print_hex(i);
|
||||
offset_print(": ");
|
||||
offset_print(type_str);
|
||||
offset_print("\n");
|
||||
|
||||
/* Recalibrate the drive */
|
||||
fdc_motor_on((uint8_t)i);
|
||||
fdc_recalibrate((uint8_t)i);
|
||||
fdc_motor_off((uint8_t)i);
|
||||
|
||||
/* Register with devicefs */
|
||||
devicefs_register_block("floppy", &floppy_block_ops,
|
||||
&floppy_drives[i]);
|
||||
found++;
|
||||
}
|
||||
|
||||
if (found == 0) {
|
||||
offset_print(" FLOPPY: no usable drives found\n");
|
||||
} else {
|
||||
offset_print(" FLOPPY: ");
|
||||
print_hex((uint32_t)found);
|
||||
offset_print(" drive(s) initialized\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* IRQ handler
|
||||
* ================================================================ */
|
||||
|
||||
void floppy_irq(void) {
|
||||
floppy_irq_received = 1;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Public API & driver registration
|
||||
* ================================================================ */
|
||||
|
||||
int floppy_init(void) {
|
||||
return floppy_driver_init();
|
||||
}
|
||||
|
||||
/** Driver descriptor. */
|
||||
static const driver_t floppy_driver = {
|
||||
.name = "floppy",
|
||||
.probe = floppy_probe,
|
||||
.init = floppy_driver_init,
|
||||
};
|
||||
|
||||
REGISTER_DRIVER(floppy_driver);
|
||||
122
src/floppy.h
Normal file
122
src/floppy.h
Normal file
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* @file floppy.h
|
||||
* @brief Floppy disk controller driver.
|
||||
*
|
||||
* Drives the Intel 82077AA-compatible floppy disk controller.
|
||||
* Detects floppy drives via CMOS and registers them with the devicefs
|
||||
* subsystem as block devices with class "floppy" (floppy1, floppy2, ...).
|
||||
*
|
||||
* Supports 1.44 MB 3.5" HD floppies (18 sectors/track, 80 tracks, 2 heads).
|
||||
* Uses IRQ 6 for command completion notification and DMA channel 2 for
|
||||
* data transfer.
|
||||
*/
|
||||
|
||||
#ifndef FLOPPY_H
|
||||
#define FLOPPY_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* ================================================================
|
||||
* Floppy controller I/O port registers (primary controller)
|
||||
* ================================================================ */
|
||||
|
||||
#define FDC_BASE 0x3F0 /**< Base I/O port of primary FDC. */
|
||||
|
||||
#define FDC_SRA 0x3F0 /**< Status Register A (read-only). */
|
||||
#define FDC_SRB 0x3F1 /**< Status Register B (read-only). */
|
||||
#define FDC_DOR 0x3F2 /**< Digital Output Register. */
|
||||
#define FDC_TDR 0x3F3 /**< Tape Drive Register. */
|
||||
#define FDC_MSR 0x3F4 /**< Main Status Register (read-only). */
|
||||
#define FDC_DSR 0x3F4 /**< Data Rate Select Register (write-only). */
|
||||
#define FDC_FIFO 0x3F5 /**< Data (FIFO) register. */
|
||||
#define FDC_DIR 0x3F7 /**< Digital Input Register (read-only). */
|
||||
#define FDC_CCR 0x3F7 /**< Configuration Control Register (write-only). */
|
||||
|
||||
/* ================================================================
|
||||
* Digital Output Register (DOR) bits
|
||||
* ================================================================ */
|
||||
|
||||
#define DOR_DRIVE_SEL_MASK 0x03 /**< Drive select (0-3). */
|
||||
#define DOR_RESET 0x04 /**< 0 = enter reset, 1 = normal operation. */
|
||||
#define DOR_DMA_IRQ 0x08 /**< 1 = enable DMA and IRQ. */
|
||||
#define DOR_MOTOR_A 0x10 /**< Motor enable for drive 0. */
|
||||
#define DOR_MOTOR_B 0x20 /**< Motor enable for drive 1. */
|
||||
#define DOR_MOTOR_C 0x40 /**< Motor enable for drive 2. */
|
||||
#define DOR_MOTOR_D 0x80 /**< Motor enable for drive 3. */
|
||||
|
||||
/* ================================================================
|
||||
* Main Status Register (MSR) bits
|
||||
* ================================================================ */
|
||||
|
||||
#define MSR_ACTA 0x01 /**< Drive 0 is seeking. */
|
||||
#define MSR_ACTB 0x02 /**< Drive 1 is seeking. */
|
||||
#define MSR_ACTC 0x04 /**< Drive 2 is seeking. */
|
||||
#define MSR_ACTD 0x08 /**< Drive 3 is seeking. */
|
||||
#define MSR_CB 0x10 /**< Command busy. */
|
||||
#define MSR_NDMA 0x20 /**< Non-DMA mode. */
|
||||
#define MSR_DIO 0x40 /**< Data direction: 1 = FDC→CPU, 0 = CPU→FDC. */
|
||||
#define MSR_RQM 0x80 /**< Ready for data transfer. */
|
||||
|
||||
/* ================================================================
|
||||
* Floppy commands
|
||||
* ================================================================ */
|
||||
|
||||
#define FDC_CMD_SPECIFY 0x03 /**< Specify step rate, head load/unload. */
|
||||
#define FDC_CMD_WRITE_DATA 0xC5 /**< Write Data (MT+MFM). */
|
||||
#define FDC_CMD_READ_DATA 0xE6 /**< Read Data (MT+MFM+SK). */
|
||||
#define FDC_CMD_RECALIBRATE 0x07 /**< Recalibrate (seek to track 0). */
|
||||
#define FDC_CMD_SENSE_INT 0x08 /**< Sense Interrupt Status. */
|
||||
#define FDC_CMD_SEEK 0x0F /**< Seek to a track. */
|
||||
#define FDC_CMD_VERSION 0x10 /**< Get controller version. */
|
||||
#define FDC_CMD_CONFIGURE 0x13 /**< Configure controller. */
|
||||
#define FDC_CMD_LOCK 0x94 /**< Lock configuration. */
|
||||
|
||||
/* ================================================================
|
||||
* Geometry for 1.44 MB 3.5" HD floppy
|
||||
* ================================================================ */
|
||||
|
||||
#define FLOPPY_SECTORS_PER_TRACK 18 /**< Sectors per track. */
|
||||
#define FLOPPY_HEADS 2 /**< Number of heads. */
|
||||
#define FLOPPY_TRACKS 80 /**< Number of cylinders/tracks. */
|
||||
#define FLOPPY_SECTOR_SIZE 512 /**< Bytes per sector. */
|
||||
#define FLOPPY_TOTAL_SECTORS (FLOPPY_SECTORS_PER_TRACK * FLOPPY_HEADS * FLOPPY_TRACKS)
|
||||
|
||||
/* ================================================================
|
||||
* CMOS floppy type codes
|
||||
* ================================================================ */
|
||||
|
||||
#define CMOS_FLOPPY_NONE 0x00 /**< No drive. */
|
||||
#define CMOS_FLOPPY_360K 0x01 /**< 5.25" 360 KB. */
|
||||
#define CMOS_FLOPPY_1200K 0x02 /**< 5.25" 1.2 MB. */
|
||||
#define CMOS_FLOPPY_720K 0x03 /**< 3.5" 720 KB. */
|
||||
#define CMOS_FLOPPY_1440K 0x04 /**< 3.5" 1.44 MB. */
|
||||
#define CMOS_FLOPPY_2880K 0x05 /**< 3.5" 2.88 MB. */
|
||||
|
||||
/** Maximum supported floppy drives. */
|
||||
#define FLOPPY_MAX_DRIVES 2
|
||||
|
||||
/**
|
||||
* Per-drive state.
|
||||
*/
|
||||
typedef struct floppy_drive {
|
||||
uint8_t drive_num; /**< Drive number (0 or 1). */
|
||||
uint8_t cmos_type; /**< CMOS type code. */
|
||||
uint8_t present; /**< 1 if drive is detected and usable. */
|
||||
uint8_t motor_on; /**< 1 if motor is currently running. */
|
||||
uint8_t current_track; /**< Last sought cylinder (cached). */
|
||||
} floppy_drive_t;
|
||||
|
||||
/**
|
||||
* Initialize the floppy disk controller driver.
|
||||
* Detects drives via CMOS and registers them with devicefs.
|
||||
*
|
||||
* @return 0 on success, non-zero on failure.
|
||||
*/
|
||||
int floppy_init(void);
|
||||
|
||||
/**
|
||||
* IRQ 6 handler — called from isr_handler when vector 38 fires.
|
||||
*/
|
||||
void floppy_irq(void);
|
||||
|
||||
#endif /* FLOPPY_H */
|
||||
215
src/font8x16.h
Normal file
215
src/font8x16.h
Normal file
@@ -0,0 +1,215 @@
|
||||
/**
|
||||
* @file font8x16.h
|
||||
* @brief Embedded 8x16 VGA bitmap font for graphical framebuffer rendering.
|
||||
*
|
||||
* Each character is 16 bytes: one byte per scanline, MSB is leftmost pixel.
|
||||
* Covers ASCII 32 (space) through 126 (~). Characters outside this range
|
||||
* render as a filled block.
|
||||
*
|
||||
* This is the standard VGA 8x16 font data, in the public domain.
|
||||
*/
|
||||
|
||||
#ifndef FONT8X16_H
|
||||
#define FONT8X16_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define FONT_WIDTH 8
|
||||
#define FONT_HEIGHT 16
|
||||
#define FONT_FIRST 32
|
||||
#define FONT_LAST 126
|
||||
|
||||
static const uint8_t font8x16_data[][16] = {
|
||||
/* 32: space */
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
/* 33: ! */
|
||||
{0x00,0x00,0x18,0x3C,0x3C,0x3C,0x18,0x18,0x18,0x00,0x18,0x18,0x00,0x00,0x00,0x00},
|
||||
/* 34: " */
|
||||
{0x00,0x66,0x66,0x66,0x24,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
/* 35: # */
|
||||
{0x00,0x00,0x00,0x6C,0x6C,0xFE,0x6C,0x6C,0x6C,0xFE,0x6C,0x6C,0x00,0x00,0x00,0x00},
|
||||
/* 36: $ */
|
||||
{0x18,0x18,0x7C,0xC6,0xC2,0xC0,0x7C,0x06,0x06,0x86,0xC6,0x7C,0x18,0x18,0x00,0x00},
|
||||
/* 37: % */
|
||||
{0x00,0x00,0x00,0x00,0xC2,0xC6,0x0C,0x18,0x30,0x60,0xC6,0x86,0x00,0x00,0x00,0x00},
|
||||
/* 38: & */
|
||||
{0x00,0x00,0x38,0x6C,0x6C,0x38,0x76,0xDC,0xCC,0xCC,0xCC,0x76,0x00,0x00,0x00,0x00},
|
||||
/* 39: ' */
|
||||
{0x00,0x30,0x30,0x30,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
/* 40: ( */
|
||||
{0x00,0x00,0x0C,0x18,0x30,0x30,0x30,0x30,0x30,0x30,0x18,0x0C,0x00,0x00,0x00,0x00},
|
||||
/* 41: ) */
|
||||
{0x00,0x00,0x30,0x18,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x18,0x30,0x00,0x00,0x00,0x00},
|
||||
/* 42: * */
|
||||
{0x00,0x00,0x00,0x00,0x00,0x66,0x3C,0xFF,0x3C,0x66,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
/* 43: + */
|
||||
{0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x7E,0x18,0x18,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
/* 44: , */
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x18,0x30,0x00,0x00,0x00},
|
||||
/* 45: - */
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
/* 46: . */
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00},
|
||||
/* 47: / */
|
||||
{0x00,0x00,0x00,0x00,0x02,0x06,0x0C,0x18,0x30,0x60,0xC0,0x80,0x00,0x00,0x00,0x00},
|
||||
/* 48: 0 */
|
||||
{0x00,0x00,0x3C,0x66,0xC3,0xC3,0xDB,0xDB,0xC3,0xC3,0x66,0x3C,0x00,0x00,0x00,0x00},
|
||||
/* 49: 1 */
|
||||
{0x00,0x00,0x18,0x38,0x78,0x18,0x18,0x18,0x18,0x18,0x18,0x7E,0x00,0x00,0x00,0x00},
|
||||
/* 50: 2 */
|
||||
{0x00,0x00,0x7C,0xC6,0x06,0x0C,0x18,0x30,0x60,0xC0,0xC6,0xFE,0x00,0x00,0x00,0x00},
|
||||
/* 51: 3 */
|
||||
{0x00,0x00,0x7C,0xC6,0x06,0x06,0x3C,0x06,0x06,0x06,0xC6,0x7C,0x00,0x00,0x00,0x00},
|
||||
/* 52: 4 */
|
||||
{0x00,0x00,0x0C,0x1C,0x3C,0x6C,0xCC,0xFE,0x0C,0x0C,0x0C,0x1E,0x00,0x00,0x00,0x00},
|
||||
/* 53: 5 */
|
||||
{0x00,0x00,0xFE,0xC0,0xC0,0xC0,0xFC,0x06,0x06,0x06,0xC6,0x7C,0x00,0x00,0x00,0x00},
|
||||
/* 54: 6 */
|
||||
{0x00,0x00,0x38,0x60,0xC0,0xC0,0xFC,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00},
|
||||
/* 55: 7 */
|
||||
{0x00,0x00,0xFE,0xC6,0x06,0x06,0x0C,0x18,0x30,0x30,0x30,0x30,0x00,0x00,0x00,0x00},
|
||||
/* 56: 8 */
|
||||
{0x00,0x00,0x7C,0xC6,0xC6,0xC6,0x7C,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00},
|
||||
/* 57: 9 */
|
||||
{0x00,0x00,0x7C,0xC6,0xC6,0xC6,0x7E,0x06,0x06,0x06,0x0C,0x78,0x00,0x00,0x00,0x00},
|
||||
/* 58: : */
|
||||
{0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x00,0x00},
|
||||
/* 59: ; */
|
||||
{0x00,0x00,0x00,0x00,0x18,0x18,0x00,0x00,0x00,0x18,0x18,0x30,0x00,0x00,0x00,0x00},
|
||||
/* 60: < */
|
||||
{0x00,0x00,0x00,0x06,0x0C,0x18,0x30,0x60,0x30,0x18,0x0C,0x06,0x00,0x00,0x00,0x00},
|
||||
/* 61: = */
|
||||
{0x00,0x00,0x00,0x00,0x00,0x7E,0x00,0x00,0x7E,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
/* 62: > */
|
||||
{0x00,0x00,0x00,0x60,0x30,0x18,0x0C,0x06,0x0C,0x18,0x30,0x60,0x00,0x00,0x00,0x00},
|
||||
/* 63: ? */
|
||||
{0x00,0x00,0x7C,0xC6,0xC6,0x0C,0x18,0x18,0x18,0x00,0x18,0x18,0x00,0x00,0x00,0x00},
|
||||
/* 64: @ */
|
||||
{0x00,0x00,0x00,0x7C,0xC6,0xC6,0xDE,0xDE,0xDE,0xDC,0xC0,0x7C,0x00,0x00,0x00,0x00},
|
||||
/* 65: A */
|
||||
{0x00,0x00,0x10,0x38,0x6C,0xC6,0xC6,0xFE,0xC6,0xC6,0xC6,0xC6,0x00,0x00,0x00,0x00},
|
||||
/* 66: B */
|
||||
{0x00,0x00,0xFC,0x66,0x66,0x66,0x7C,0x66,0x66,0x66,0x66,0xFC,0x00,0x00,0x00,0x00},
|
||||
/* 67: C */
|
||||
{0x00,0x00,0x3C,0x66,0xC2,0xC0,0xC0,0xC0,0xC0,0xC2,0x66,0x3C,0x00,0x00,0x00,0x00},
|
||||
/* 68: D */
|
||||
{0x00,0x00,0xF8,0x6C,0x66,0x66,0x66,0x66,0x66,0x66,0x6C,0xF8,0x00,0x00,0x00,0x00},
|
||||
/* 69: E */
|
||||
{0x00,0x00,0xFE,0x66,0x62,0x68,0x78,0x68,0x60,0x62,0x66,0xFE,0x00,0x00,0x00,0x00},
|
||||
/* 70: F */
|
||||
{0x00,0x00,0xFE,0x66,0x62,0x68,0x78,0x68,0x60,0x60,0x60,0xF0,0x00,0x00,0x00,0x00},
|
||||
/* 71: G */
|
||||
{0x00,0x00,0x3C,0x66,0xC2,0xC0,0xC0,0xDE,0xC6,0xC6,0x66,0x3A,0x00,0x00,0x00,0x00},
|
||||
/* 72: H */
|
||||
{0x00,0x00,0xC6,0xC6,0xC6,0xC6,0xFE,0xC6,0xC6,0xC6,0xC6,0xC6,0x00,0x00,0x00,0x00},
|
||||
/* 73: I */
|
||||
{0x00,0x00,0x3C,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00},
|
||||
/* 74: J */
|
||||
{0x00,0x00,0x1E,0x0C,0x0C,0x0C,0x0C,0x0C,0xCC,0xCC,0xCC,0x78,0x00,0x00,0x00,0x00},
|
||||
/* 75: K */
|
||||
{0x00,0x00,0xE6,0x66,0x66,0x6C,0x78,0x78,0x6C,0x66,0x66,0xE6,0x00,0x00,0x00,0x00},
|
||||
/* 76: L */
|
||||
{0x00,0x00,0xF0,0x60,0x60,0x60,0x60,0x60,0x60,0x62,0x66,0xFE,0x00,0x00,0x00,0x00},
|
||||
/* 77: M */
|
||||
{0x00,0x00,0xC6,0xEE,0xFE,0xFE,0xD6,0xC6,0xC6,0xC6,0xC6,0xC6,0x00,0x00,0x00,0x00},
|
||||
/* 78: N */
|
||||
{0x00,0x00,0xC6,0xE6,0xF6,0xFE,0xDE,0xCE,0xC6,0xC6,0xC6,0xC6,0x00,0x00,0x00,0x00},
|
||||
/* 79: O */
|
||||
{0x00,0x00,0x7C,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00},
|
||||
/* 80: P */
|
||||
{0x00,0x00,0xFC,0x66,0x66,0x66,0x7C,0x60,0x60,0x60,0x60,0xF0,0x00,0x00,0x00,0x00},
|
||||
/* 81: Q */
|
||||
{0x00,0x00,0x7C,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xD6,0xDE,0x7C,0x0C,0x0E,0x00,0x00},
|
||||
/* 82: R */
|
||||
{0x00,0x00,0xFC,0x66,0x66,0x66,0x7C,0x6C,0x66,0x66,0x66,0xE6,0x00,0x00,0x00,0x00},
|
||||
/* 83: S */
|
||||
{0x00,0x00,0x7C,0xC6,0xC6,0x60,0x38,0x0C,0x06,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00},
|
||||
/* 84: T */
|
||||
{0x00,0x00,0xFF,0xDB,0x99,0x18,0x18,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00},
|
||||
/* 85: U */
|
||||
{0x00,0x00,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00},
|
||||
/* 86: V */
|
||||
{0x00,0x00,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0xC6,0x6C,0x38,0x10,0x00,0x00,0x00,0x00},
|
||||
/* 87: W */
|
||||
{0x00,0x00,0xC6,0xC6,0xC6,0xC6,0xD6,0xD6,0xD6,0xFE,0xEE,0x6C,0x00,0x00,0x00,0x00},
|
||||
/* 88: X */
|
||||
{0x00,0x00,0xC6,0xC6,0x6C,0x7C,0x38,0x38,0x7C,0x6C,0xC6,0xC6,0x00,0x00,0x00,0x00},
|
||||
/* 89: Y */
|
||||
{0x00,0x00,0xC3,0xC3,0x66,0x3C,0x18,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00},
|
||||
/* 90: Z */
|
||||
{0x00,0x00,0xFE,0xC6,0x86,0x0C,0x18,0x30,0x60,0xC2,0xC6,0xFE,0x00,0x00,0x00,0x00},
|
||||
/* 91: [ */
|
||||
{0x00,0x00,0x3C,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x3C,0x00,0x00,0x00,0x00},
|
||||
/* 92: \ */
|
||||
{0x00,0x00,0x00,0x80,0xC0,0xE0,0x70,0x38,0x1C,0x0E,0x06,0x02,0x00,0x00,0x00,0x00},
|
||||
/* 93: ] */
|
||||
{0x00,0x00,0x3C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,0x3C,0x00,0x00,0x00,0x00},
|
||||
/* 94: ^ */
|
||||
{0x10,0x38,0x6C,0xC6,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
/* 95: _ */
|
||||
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00},
|
||||
/* 96: ` */
|
||||
{0x30,0x30,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
/* 97: a */
|
||||
{0x00,0x00,0x00,0x00,0x00,0x78,0x0C,0x7C,0xCC,0xCC,0xCC,0x76,0x00,0x00,0x00,0x00},
|
||||
/* 98: b */
|
||||
{0x00,0x00,0xE0,0x60,0x60,0x78,0x6C,0x66,0x66,0x66,0x66,0x7C,0x00,0x00,0x00,0x00},
|
||||
/* 99: c */
|
||||
{0x00,0x00,0x00,0x00,0x00,0x7C,0xC6,0xC0,0xC0,0xC0,0xC6,0x7C,0x00,0x00,0x00,0x00},
|
||||
/* 100: d */
|
||||
{0x00,0x00,0x1C,0x0C,0x0C,0x3C,0x6C,0xCC,0xCC,0xCC,0xCC,0x76,0x00,0x00,0x00,0x00},
|
||||
/* 101: e */
|
||||
{0x00,0x00,0x00,0x00,0x00,0x7C,0xC6,0xFE,0xC0,0xC0,0xC6,0x7C,0x00,0x00,0x00,0x00},
|
||||
/* 102: f */
|
||||
{0x00,0x00,0x1C,0x36,0x32,0x30,0x78,0x30,0x30,0x30,0x30,0x78,0x00,0x00,0x00,0x00},
|
||||
/* 103: g */
|
||||
{0x00,0x00,0x00,0x00,0x00,0x76,0xCC,0xCC,0xCC,0xCC,0x7C,0x0C,0xCC,0x78,0x00,0x00},
|
||||
/* 104: h */
|
||||
{0x00,0x00,0xE0,0x60,0x60,0x6C,0x76,0x66,0x66,0x66,0x66,0xE6,0x00,0x00,0x00,0x00},
|
||||
/* 105: i */
|
||||
{0x00,0x00,0x18,0x18,0x00,0x38,0x18,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00},
|
||||
/* 106: j */
|
||||
{0x00,0x00,0x06,0x06,0x00,0x0E,0x06,0x06,0x06,0x06,0x06,0x06,0x66,0x3C,0x00,0x00},
|
||||
/* 107: k */
|
||||
{0x00,0x00,0xE0,0x60,0x60,0x66,0x6C,0x78,0x78,0x6C,0x66,0xE6,0x00,0x00,0x00,0x00},
|
||||
/* 108: l */
|
||||
{0x00,0x00,0x38,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x3C,0x00,0x00,0x00,0x00},
|
||||
/* 109: m */
|
||||
{0x00,0x00,0x00,0x00,0x00,0xE6,0xFF,0xDB,0xDB,0xDB,0xDB,0xDB,0x00,0x00,0x00,0x00},
|
||||
/* 110: n */
|
||||
{0x00,0x00,0x00,0x00,0x00,0xDC,0x66,0x66,0x66,0x66,0x66,0x66,0x00,0x00,0x00,0x00},
|
||||
/* 111: o */
|
||||
{0x00,0x00,0x00,0x00,0x00,0x7C,0xC6,0xC6,0xC6,0xC6,0xC6,0x7C,0x00,0x00,0x00,0x00},
|
||||
/* 112: p */
|
||||
{0x00,0x00,0x00,0x00,0x00,0xDC,0x66,0x66,0x66,0x66,0x7C,0x60,0x60,0xF0,0x00,0x00},
|
||||
/* 113: q */
|
||||
{0x00,0x00,0x00,0x00,0x00,0x76,0xCC,0xCC,0xCC,0xCC,0x7C,0x0C,0x0C,0x1E,0x00,0x00},
|
||||
/* 114: r */
|
||||
{0x00,0x00,0x00,0x00,0x00,0xDC,0x76,0x66,0x60,0x60,0x60,0xF0,0x00,0x00,0x00,0x00},
|
||||
/* 115: s */
|
||||
{0x00,0x00,0x00,0x00,0x00,0x7C,0xC6,0x60,0x38,0x0C,0xC6,0x7C,0x00,0x00,0x00,0x00},
|
||||
/* 116: t */
|
||||
{0x00,0x00,0x10,0x30,0x30,0xFC,0x30,0x30,0x30,0x30,0x36,0x1C,0x00,0x00,0x00,0x00},
|
||||
/* 117: u */
|
||||
{0x00,0x00,0x00,0x00,0x00,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0x76,0x00,0x00,0x00,0x00},
|
||||
/* 118: v */
|
||||
{0x00,0x00,0x00,0x00,0x00,0xC3,0xC3,0xC3,0xC3,0x66,0x3C,0x18,0x00,0x00,0x00,0x00},
|
||||
/* 119: w */
|
||||
{0x00,0x00,0x00,0x00,0x00,0xC6,0xC6,0xC6,0xD6,0xD6,0xFE,0x6C,0x00,0x00,0x00,0x00},
|
||||
/* 120: x */
|
||||
{0x00,0x00,0x00,0x00,0x00,0xC6,0x6C,0x38,0x38,0x38,0x6C,0xC6,0x00,0x00,0x00,0x00},
|
||||
/* 121: y */
|
||||
{0x00,0x00,0x00,0x00,0x00,0xC6,0xC6,0xC6,0xC6,0xC6,0x7E,0x06,0x0C,0xF8,0x00,0x00},
|
||||
/* 122: z */
|
||||
{0x00,0x00,0x00,0x00,0x00,0xFE,0xCC,0x18,0x30,0x60,0xC6,0xFE,0x00,0x00,0x00,0x00},
|
||||
/* 123: { */
|
||||
{0x00,0x00,0x0E,0x18,0x18,0x18,0x70,0x18,0x18,0x18,0x18,0x0E,0x00,0x00,0x00,0x00},
|
||||
/* 124: | */
|
||||
{0x00,0x00,0x18,0x18,0x18,0x18,0x00,0x18,0x18,0x18,0x18,0x18,0x00,0x00,0x00,0x00},
|
||||
/* 125: } */
|
||||
{0x00,0x00,0x70,0x18,0x18,0x18,0x0E,0x18,0x18,0x18,0x18,0x70,0x00,0x00,0x00,0x00},
|
||||
/* 126: ~ */
|
||||
{0x00,0x00,0x76,0xDC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
|
||||
};
|
||||
|
||||
#endif /* FONT8X16_H */
|
||||
44
src/framebuffer.h
Normal file
44
src/framebuffer.h
Normal file
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
* @file framebuffer.h
|
||||
* @brief Framebuffer information from the bootloader.
|
||||
*
|
||||
* Stores the display mode and framebuffer address provided by GRUB
|
||||
* via the multiboot2 framebuffer tag. The VGA driver uses this to
|
||||
* decide between text-mode writes (0xB8000) and pixel rendering.
|
||||
*/
|
||||
|
||||
#ifndef FRAMEBUFFER_H
|
||||
#define FRAMEBUFFER_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/** Framebuffer types (matches multiboot2 definitions). */
|
||||
#define FB_TYPE_INDEXED 0
|
||||
#define FB_TYPE_RGB 1
|
||||
#define FB_TYPE_EGA_TEXT 2
|
||||
|
||||
/**
|
||||
* Framebuffer information structure.
|
||||
* Populated during boot from the multiboot2 framebuffer tag.
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t addr; /**< Physical address of the framebuffer. */
|
||||
uint32_t pitch; /**< Bytes per scanline. */
|
||||
uint32_t width; /**< Width in pixels (or columns for text). */
|
||||
uint32_t height; /**< Height in pixels (or rows for text). */
|
||||
uint8_t bpp; /**< Bits per pixel. */
|
||||
uint8_t type; /**< FB_TYPE_RGB, FB_TYPE_EGA_TEXT, etc. */
|
||||
|
||||
/* RGB field positions (only valid when type == FB_TYPE_RGB). */
|
||||
uint8_t red_pos;
|
||||
uint8_t red_size;
|
||||
uint8_t green_pos;
|
||||
uint8_t green_size;
|
||||
uint8_t blue_pos;
|
||||
uint8_t blue_size;
|
||||
} framebuffer_info_t;
|
||||
|
||||
/** Global framebuffer info, filled by kernel_main. */
|
||||
extern framebuffer_info_t fb_info;
|
||||
|
||||
#endif /* FRAMEBUFFER_H */
|
||||
68
src/gdt.c
Normal file
68
src/gdt.c
Normal file
@@ -0,0 +1,68 @@
|
||||
#include "gdt.h"
|
||||
#include <stdint.h>
|
||||
|
||||
/* GDT Pointer Structure */
|
||||
struct gdt_ptr gp;
|
||||
/* GDT entries: 0=null, 1=kcode, 2=kdata, 3=ucode, 4=udata, 5=tss */
|
||||
struct gdt_entry gdt[6];
|
||||
|
||||
extern void gdt_flush(uint32_t);
|
||||
|
||||
/* Setup a descriptor in the Global Descriptor Table */
|
||||
void gdt_set_gate(int32_t num, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran)
|
||||
{
|
||||
/* Setup the descriptor base address */
|
||||
gdt[num].base_low = (base & 0xFFFF);
|
||||
gdt[num].base_middle = (base >> 16) & 0xFF;
|
||||
gdt[num].base_high = (base >> 24) & 0xFF;
|
||||
|
||||
/* Setup the descriptor limits */
|
||||
gdt[num].limit_low = (limit & 0xFFFF);
|
||||
gdt[num].granularity = ((limit >> 16) & 0x0F);
|
||||
|
||||
/* Finally, set up the granularity and access flags */
|
||||
gdt[num].granularity |= (gran & 0xF0);
|
||||
gdt[num].access = access;
|
||||
}
|
||||
|
||||
/* Should be called by main. This will setup the special GDT
|
||||
* pointer, set up the first 3 entries in our GDT, and then
|
||||
* call gdt_flush() in our assembler file in order to tell
|
||||
* the processor where the new GDT is and update the
|
||||
* internal registers */
|
||||
void init_gdt()
|
||||
{
|
||||
/* Setup the GDT pointer and limit */
|
||||
gp.limit = (sizeof(struct gdt_entry) * 6) - 1;
|
||||
gp.base = (uint32_t)&gdt;
|
||||
|
||||
/* Our NULL descriptor */
|
||||
gdt_set_gate(0, 0, 0, 0, 0);
|
||||
|
||||
/* The second entry is our Code Segment. The base address
|
||||
* is 0, the limit is 4GBytes, it uses 4KByte granularity,
|
||||
* uses 32-bit opcodes, and is a Code Segment descriptor.
|
||||
* Please check the table above in the tutorial in order
|
||||
* to see exactly what each value means */
|
||||
/* 0x9A = 1001 1010
|
||||
P=1, DPL=00, S=1 (Code/Data), Type=1010 (Code, Exec/Read) */
|
||||
/* 0xCF = 1100 1111
|
||||
G=1 (4k), DB=1 (32bit), L=0, AVL=0, LimitHigh=0xF */
|
||||
gdt_set_gate(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
|
||||
|
||||
/* The third entry is our Data Segment. It's EXACTLY the
|
||||
* same as our code segment, but the descriptor type in
|
||||
* this entry's access byte says it's a Data Segment */
|
||||
/* 0x92 = 1001 0010
|
||||
P=1, DPL=00, S=1, Type=0010 (Data, Read/Write) */
|
||||
gdt_set_gate(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
|
||||
|
||||
/* User mode code segment */
|
||||
gdt_set_gate(3, 0, 0xFFFFFFFF, 0xFA, 0xCF);
|
||||
|
||||
/* User mode data segment */
|
||||
gdt_set_gate(4, 0, 0xFFFFFFFF, 0xF2, 0xCF);
|
||||
|
||||
/* Flush out the old GDT and install the new changes! */
|
||||
gdt_flush((uint32_t)&gp);
|
||||
}
|
||||
28
src/gdt.h
Normal file
28
src/gdt.h
Normal file
@@ -0,0 +1,28 @@
|
||||
#ifndef GDT_H
|
||||
#define GDT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* GDT Entry Structure */
|
||||
struct gdt_entry {
|
||||
uint16_t limit_low;
|
||||
uint16_t base_low;
|
||||
uint8_t base_middle;
|
||||
uint8_t access;
|
||||
uint8_t granularity;
|
||||
uint8_t base_high;
|
||||
} __attribute__((packed));
|
||||
|
||||
/* GDT Pointer Structure */
|
||||
struct gdt_ptr {
|
||||
uint16_t limit;
|
||||
uint32_t base;
|
||||
} __attribute__((packed, aligned(1)));
|
||||
|
||||
/* Initialize GDT */
|
||||
void init_gdt(void);
|
||||
|
||||
/* Set a GDT gate (also used by TSS setup) */
|
||||
void gdt_set_gate(int32_t num, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran);
|
||||
|
||||
#endif // GDT_H
|
||||
18
src/gdt_flush.S
Normal file
18
src/gdt_flush.S
Normal file
@@ -0,0 +1,18 @@
|
||||
.section .text
|
||||
.global gdt_flush
|
||||
.type gdt_flush, @function
|
||||
gdt_flush:
|
||||
mov 4(%esp), %eax
|
||||
lgdt (%eax)
|
||||
|
||||
mov $0x10, %ax
|
||||
mov %ax, %ds
|
||||
mov %ax, %es
|
||||
mov %ax, %fs
|
||||
mov %ax, %gs
|
||||
mov %ax, %ss
|
||||
|
||||
ljmp $0x08, $flush
|
||||
|
||||
flush:
|
||||
ret
|
||||
450
src/graphics.c
Normal file
450
src/graphics.c
Normal file
@@ -0,0 +1,450 @@
|
||||
/**
|
||||
* @file graphics.c
|
||||
* @brief Graphics subsystem implementation.
|
||||
*
|
||||
* Provides VGA mode 0x13 (320x200, 256 colors) with drawing primitives.
|
||||
* Handles switching between text mode (0x03) and graphics mode via
|
||||
* direct VGA register programming.
|
||||
*/
|
||||
|
||||
#include "graphics.h"
|
||||
#include "port_io.h"
|
||||
#include "font8x16.h"
|
||||
#include "string.h"
|
||||
#include "vga.h"
|
||||
|
||||
/* ================================================================
|
||||
* VGA register programming constants
|
||||
* ================================================================ */
|
||||
|
||||
/* VGA ports */
|
||||
#define VGA_MISC_WRITE 0x3C2
|
||||
#define VGA_MISC_READ 0x3CC
|
||||
#define VGA_SEQ_INDEX 0x3C4
|
||||
#define VGA_SEQ_DATA 0x3C5
|
||||
#define VGA_CRTC_INDEX 0x3D4
|
||||
#define VGA_CRTC_DATA 0x3D5
|
||||
#define VGA_GC_INDEX 0x3CE
|
||||
#define VGA_GC_DATA 0x3CF
|
||||
#define VGA_AC_INDEX 0x3C0
|
||||
#define VGA_AC_WRITE 0x3C0
|
||||
#define VGA_AC_READ 0x3C1
|
||||
#define VGA_INSTAT_READ 0x3DA
|
||||
#define VGA_DAC_WRITE_IDX 0x3C8
|
||||
#define VGA_DAC_DATA 0x3C9
|
||||
|
||||
/* Number of registers in each group */
|
||||
#define VGA_NUM_SEQ_REGS 5
|
||||
#define VGA_NUM_CRTC_REGS 25
|
||||
#define VGA_NUM_GC_REGS 9
|
||||
#define VGA_NUM_AC_REGS 21
|
||||
|
||||
/* ================================================================
|
||||
* VGA register tables for Mode 0x13 (320x200x256)
|
||||
* ================================================================ */
|
||||
|
||||
static const uint8_t mode13_misc = 0x63;
|
||||
|
||||
static const uint8_t mode13_seq[VGA_NUM_SEQ_REGS] = {
|
||||
0x03, 0x01, 0x0F, 0x00, 0x0E
|
||||
};
|
||||
|
||||
static const uint8_t mode13_crtc[VGA_NUM_CRTC_REGS] = {
|
||||
0x5F, 0x4F, 0x50, 0x82, 0x54, 0x80, 0xBF, 0x1F,
|
||||
0x00, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x9C, 0x0E, 0x8F, 0x28, 0x40, 0x96, 0xB9, 0xA3,
|
||||
0xFF
|
||||
};
|
||||
|
||||
static const uint8_t mode13_gc[VGA_NUM_GC_REGS] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x05, 0x0F,
|
||||
0xFF
|
||||
};
|
||||
|
||||
static const uint8_t mode13_ac[VGA_NUM_AC_REGS] = {
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
|
||||
0x41, 0x00, 0x0F, 0x00, 0x00
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* VGA register tables for Mode 0x03 (80x25 text)
|
||||
* ================================================================ */
|
||||
|
||||
static const uint8_t mode03_misc = 0x67;
|
||||
|
||||
static const uint8_t mode03_seq[VGA_NUM_SEQ_REGS] = {
|
||||
0x03, 0x00, 0x03, 0x00, 0x02
|
||||
};
|
||||
|
||||
static const uint8_t mode03_crtc[VGA_NUM_CRTC_REGS] = {
|
||||
0x5F, 0x4F, 0x50, 0x82, 0x55, 0x81, 0xBF, 0x1F,
|
||||
0x00, 0x4F, 0x0D, 0x0E, 0x00, 0x00, 0x00, 0x50,
|
||||
0x9C, 0x0E, 0x8F, 0x28, 0x1F, 0x96, 0xB9, 0xA3,
|
||||
0xFF
|
||||
};
|
||||
|
||||
static const uint8_t mode03_gc[VGA_NUM_GC_REGS] = {
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0E, 0x00,
|
||||
0xFF
|
||||
};
|
||||
|
||||
static const uint8_t mode03_ac[VGA_NUM_AC_REGS] = {
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07,
|
||||
0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
|
||||
0x0C, 0x00, 0x0F, 0x08, 0x00
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* Module state
|
||||
* ================================================================ */
|
||||
|
||||
static int gfx_mode = GFX_MODE_TEXT;
|
||||
static uint8_t *framebuffer = (uint8_t *)GFX_FRAMEBUFFER;
|
||||
|
||||
/* ================================================================
|
||||
* VGA register programming helpers
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Write a set of VGA registers to switch modes.
|
||||
*/
|
||||
static void vga_write_regs(uint8_t misc, const uint8_t *seq,
|
||||
const uint8_t *crtc, const uint8_t *gc,
|
||||
const uint8_t *ac)
|
||||
{
|
||||
/* Miscellaneous output */
|
||||
outb(VGA_MISC_WRITE, misc);
|
||||
|
||||
/* Sequencer */
|
||||
for (int i = 0; i < VGA_NUM_SEQ_REGS; i++) {
|
||||
outb(VGA_SEQ_INDEX, (uint8_t)i);
|
||||
outb(VGA_SEQ_DATA, seq[i]);
|
||||
}
|
||||
|
||||
/* Unlock CRTC (clear protect bit in register 0x11) */
|
||||
outb(VGA_CRTC_INDEX, 0x11);
|
||||
outb(VGA_CRTC_DATA, inb(VGA_CRTC_DATA) & 0x7F);
|
||||
|
||||
/* CRTC */
|
||||
for (int i = 0; i < VGA_NUM_CRTC_REGS; i++) {
|
||||
outb(VGA_CRTC_INDEX, (uint8_t)i);
|
||||
outb(VGA_CRTC_DATA, crtc[i]);
|
||||
}
|
||||
|
||||
/* Graphics Controller */
|
||||
for (int i = 0; i < VGA_NUM_GC_REGS; i++) {
|
||||
outb(VGA_GC_INDEX, (uint8_t)i);
|
||||
outb(VGA_GC_DATA, gc[i]);
|
||||
}
|
||||
|
||||
/* Attribute Controller */
|
||||
/* Reading port 0x3DA resets the AC flip-flop to index mode */
|
||||
inb(VGA_INSTAT_READ);
|
||||
for (int i = 0; i < VGA_NUM_AC_REGS; i++) {
|
||||
outb(VGA_AC_INDEX, (uint8_t)i);
|
||||
outb(VGA_AC_WRITE, ac[i]);
|
||||
}
|
||||
/* Re-enable display by setting bit 5 of the AC index */
|
||||
outb(VGA_AC_INDEX, 0x20);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* VGA font restore for text mode
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Load the 8x16 font into VGA plane 2 after returning to text mode.
|
||||
* This restores readable text after graphics mode.
|
||||
*/
|
||||
static void vga_load_font(void) {
|
||||
volatile uint8_t *vmem = (volatile uint8_t *)0xA0000;
|
||||
|
||||
/* Set up sequencer for font loading:
|
||||
* - Map Mask: select plane 2 only
|
||||
* - Memory Mode: disable chain-4, enable odd/even */
|
||||
outb(VGA_SEQ_INDEX, 0x02);
|
||||
outb(VGA_SEQ_DATA, 0x04); /* Map Mask: plane 2 */
|
||||
outb(VGA_SEQ_INDEX, 0x04);
|
||||
outb(VGA_SEQ_DATA, 0x06); /* Memory Mode: enable sequential, disable chain-4 */
|
||||
|
||||
/* Set up graphics controller for font loading:
|
||||
* - Read Map Select: plane 2
|
||||
* - Graphics Mode: write mode 0, read mode 0
|
||||
* - Miscellaneous: text mode mapping (B8000-BFFFF) */
|
||||
outb(VGA_GC_INDEX, 0x04);
|
||||
outb(VGA_GC_DATA, 0x02); /* Read Map Select: plane 2 */
|
||||
outb(VGA_GC_INDEX, 0x05);
|
||||
outb(VGA_GC_DATA, 0x00); /* Graphics Mode: write mode 0 */
|
||||
outb(VGA_GC_INDEX, 0x06);
|
||||
outb(VGA_GC_DATA, 0x04); /* Misc: map to A0000, no chain, no odd/even */
|
||||
|
||||
/* Write font data for 256 characters.
|
||||
* Each char entry is 32 bytes (only first 16 used for 8x16 font).
|
||||
* Characters outside our font range get a filled block. */
|
||||
for (int ch = 0; ch < 256; ch++) {
|
||||
for (int row = 0; row < 16; row++) {
|
||||
uint8_t bits;
|
||||
if (ch >= FONT_FIRST && ch <= FONT_LAST) {
|
||||
bits = font8x16_data[ch - FONT_FIRST][row];
|
||||
} else if (ch == 0) {
|
||||
bits = 0x00; /* Null char = blank */
|
||||
} else {
|
||||
bits = 0xFF; /* Unknown = filled block */
|
||||
}
|
||||
vmem[ch * 32 + row] = bits;
|
||||
}
|
||||
/* Zero out remaining 16 bytes of the 32-byte slot */
|
||||
for (int row = 16; row < 32; row++) {
|
||||
vmem[ch * 32 + row] = 0x00;
|
||||
}
|
||||
}
|
||||
|
||||
/* Restore sequencer for text mode:
|
||||
* - Map Mask: planes 0 and 1 (text attribute + char)
|
||||
* - Memory Mode: enable odd/even, no chain-4 */
|
||||
outb(VGA_SEQ_INDEX, 0x02);
|
||||
outb(VGA_SEQ_DATA, 0x03); /* Map Mask: planes 0 and 1 */
|
||||
outb(VGA_SEQ_INDEX, 0x04);
|
||||
outb(VGA_SEQ_DATA, 0x02); /* Memory Mode: odd/even addressing */
|
||||
|
||||
/* Restore graphics controller for text mode */
|
||||
outb(VGA_GC_INDEX, 0x04);
|
||||
outb(VGA_GC_DATA, 0x00); /* Read Map Select: plane 0 */
|
||||
outb(VGA_GC_INDEX, 0x05);
|
||||
outb(VGA_GC_DATA, 0x10); /* Graphics Mode: odd/even */
|
||||
outb(VGA_GC_INDEX, 0x06);
|
||||
outb(VGA_GC_DATA, 0x0E); /* Misc: map to B8000, odd/even */
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* 256-color palette setup
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Standard VGA 16-color palette (RGB 6-bit values).
|
||||
*/
|
||||
static const uint8_t vga_palette_16[16][3] = {
|
||||
{ 0, 0, 0}, /* 0: Black */
|
||||
{ 0, 0, 42}, /* 1: Blue */
|
||||
{ 0, 42, 0}, /* 2: Green */
|
||||
{ 0, 42, 42}, /* 3: Cyan */
|
||||
{42, 0, 0}, /* 4: Red */
|
||||
{42, 0, 42}, /* 5: Magenta */
|
||||
{42, 21, 0}, /* 6: Brown */
|
||||
{42, 42, 42}, /* 7: Light Grey */
|
||||
{21, 21, 21}, /* 8: Dark Grey */
|
||||
{21, 21, 63}, /* 9: Light Blue */
|
||||
{21, 63, 21}, /* 10: Light Green */
|
||||
{21, 63, 63}, /* 11: Light Cyan */
|
||||
{63, 21, 21}, /* 12: Light Red */
|
||||
{63, 21, 63}, /* 13: Light Magenta */
|
||||
{63, 63, 21}, /* 14: Yellow */
|
||||
{63, 63, 63}, /* 15: White */
|
||||
};
|
||||
|
||||
/**
|
||||
* Set up the 256-color palette.
|
||||
* 0-15: Standard 16 VGA colors
|
||||
* 16-231: 6x6x6 RGB color cube
|
||||
* 232-255: 24-step grayscale
|
||||
*/
|
||||
static void setup_palette(void) {
|
||||
/* VGA DAC: write index, then R, G, B (6-bit values, 0-63) */
|
||||
|
||||
/* Standard 16 colors */
|
||||
outb(VGA_DAC_WRITE_IDX, 0);
|
||||
for (int i = 0; i < 16; i++) {
|
||||
outb(VGA_DAC_DATA, vga_palette_16[i][0]);
|
||||
outb(VGA_DAC_DATA, vga_palette_16[i][1]);
|
||||
outb(VGA_DAC_DATA, vga_palette_16[i][2]);
|
||||
}
|
||||
|
||||
/* 6x6x6 RGB color cube (indices 16-231) */
|
||||
outb(VGA_DAC_WRITE_IDX, 16);
|
||||
for (int r = 0; r < 6; r++) {
|
||||
for (int g = 0; g < 6; g++) {
|
||||
for (int b = 0; b < 6; b++) {
|
||||
outb(VGA_DAC_DATA, (uint8_t)(r * 63 / 5));
|
||||
outb(VGA_DAC_DATA, (uint8_t)(g * 63 / 5));
|
||||
outb(VGA_DAC_DATA, (uint8_t)(b * 63 / 5));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 24-step grayscale (indices 232-255) */
|
||||
outb(VGA_DAC_WRITE_IDX, 232);
|
||||
for (int i = 0; i < 24; i++) {
|
||||
uint8_t v = (uint8_t)(i * 63 / 23);
|
||||
outb(VGA_DAC_DATA, v);
|
||||
outb(VGA_DAC_DATA, v);
|
||||
outb(VGA_DAC_DATA, v);
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Mode switching
|
||||
* ================================================================ */
|
||||
|
||||
void graphics_enter(void) {
|
||||
if (gfx_mode == GFX_MODE_GRAPHICS) return;
|
||||
|
||||
/* Switch to mode 0x13 */
|
||||
vga_write_regs(mode13_misc, mode13_seq, mode13_crtc, mode13_gc, mode13_ac);
|
||||
|
||||
/* Set up the color palette */
|
||||
setup_palette();
|
||||
|
||||
/* Clear the framebuffer */
|
||||
memset(framebuffer, 0, GFX_WIDTH * GFX_HEIGHT);
|
||||
|
||||
gfx_mode = GFX_MODE_GRAPHICS;
|
||||
}
|
||||
|
||||
void graphics_leave(void) {
|
||||
if (gfx_mode == GFX_MODE_TEXT) return;
|
||||
|
||||
/* Switch to mode 0x03 */
|
||||
vga_write_regs(mode03_misc, mode03_seq, mode03_crtc, mode03_gc, mode03_ac);
|
||||
|
||||
/* Reload the VGA font into plane 2 */
|
||||
vga_load_font();
|
||||
|
||||
/* Clear the text-mode framebuffer */
|
||||
volatile uint16_t *text_buf = (volatile uint16_t *)0xB8000;
|
||||
for (int i = 0; i < 80 * 25; i++) {
|
||||
text_buf[i] = 0x0720; /* Light grey on black, space */
|
||||
}
|
||||
|
||||
gfx_mode = GFX_MODE_TEXT;
|
||||
|
||||
/* Re-initialize the VGA text driver */
|
||||
vga_init();
|
||||
}
|
||||
|
||||
int graphics_get_mode(void) {
|
||||
return gfx_mode;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Drawing primitives
|
||||
* ================================================================ */
|
||||
|
||||
void gfx_pixel(int x, int y, uint8_t color) {
|
||||
if (x < 0 || x >= GFX_WIDTH || y < 0 || y >= GFX_HEIGHT) return;
|
||||
framebuffer[y * GFX_WIDTH + x] = color;
|
||||
}
|
||||
|
||||
void gfx_clear(uint8_t color) {
|
||||
memset(framebuffer, color, GFX_WIDTH * GFX_HEIGHT);
|
||||
}
|
||||
|
||||
void gfx_fill_rect(int x, int y, int w, int h, uint8_t color) {
|
||||
/* Clip */
|
||||
int x1 = x < 0 ? 0 : x;
|
||||
int y1 = y < 0 ? 0 : y;
|
||||
int x2 = (x + w > GFX_WIDTH) ? GFX_WIDTH : (x + w);
|
||||
int y2 = (y + h > GFX_HEIGHT) ? GFX_HEIGHT : (y + h);
|
||||
|
||||
for (int row = y1; row < y2; row++) {
|
||||
memset(&framebuffer[row * GFX_WIDTH + x1], color, (uint32_t)(x2 - x1));
|
||||
}
|
||||
}
|
||||
|
||||
void gfx_draw_line(int x1, int y1, int x2, int y2, uint8_t color) {
|
||||
/* Bresenham's line algorithm */
|
||||
int dx = x2 - x1;
|
||||
int dy = y2 - y1;
|
||||
int sx = dx >= 0 ? 1 : -1;
|
||||
int sy = dy >= 0 ? 1 : -1;
|
||||
if (dx < 0) dx = -dx;
|
||||
if (dy < 0) dy = -dy;
|
||||
|
||||
int err = dx - dy;
|
||||
|
||||
for (;;) {
|
||||
gfx_pixel(x1, y1, color);
|
||||
if (x1 == x2 && y1 == y2) break;
|
||||
int e2 = 2 * err;
|
||||
if (e2 > -dy) { err -= dy; x1 += sx; }
|
||||
if (e2 < dx) { err += dx; y1 += sy; }
|
||||
}
|
||||
}
|
||||
|
||||
void gfx_fill_circle(int cx, int cy, int r, uint8_t color) {
|
||||
if (r <= 0) { gfx_pixel(cx, cy, color); return; }
|
||||
|
||||
for (int y = -r; y <= r; y++) {
|
||||
/* Horizontal span for this scanline */
|
||||
int dx = 0;
|
||||
while (dx * dx + y * y <= r * r) dx++;
|
||||
dx--;
|
||||
int left = cx - dx;
|
||||
int right = cx + dx;
|
||||
|
||||
/* Clip */
|
||||
if (cy + y < 0 || cy + y >= GFX_HEIGHT) continue;
|
||||
if (left < 0) left = 0;
|
||||
if (right >= GFX_WIDTH) right = GFX_WIDTH - 1;
|
||||
if (left > right) continue;
|
||||
|
||||
memset(&framebuffer[(cy + y) * GFX_WIDTH + left], color,
|
||||
(uint32_t)(right - left + 1));
|
||||
}
|
||||
}
|
||||
|
||||
void gfx_draw_circle(int cx, int cy, int r, uint8_t color) {
|
||||
/* Midpoint circle algorithm */
|
||||
int x = r, y = 0;
|
||||
int err = 1 - r;
|
||||
|
||||
while (x >= y) {
|
||||
gfx_pixel(cx + x, cy + y, color);
|
||||
gfx_pixel(cx + y, cy + x, color);
|
||||
gfx_pixel(cx - y, cy + x, color);
|
||||
gfx_pixel(cx - x, cy + y, color);
|
||||
gfx_pixel(cx - x, cy - y, color);
|
||||
gfx_pixel(cx - y, cy - x, color);
|
||||
gfx_pixel(cx + y, cy - x, color);
|
||||
gfx_pixel(cx + x, cy - y, color);
|
||||
|
||||
y++;
|
||||
if (err < 0) {
|
||||
err += 2 * y + 1;
|
||||
} else {
|
||||
x--;
|
||||
err += 2 * (y - x) + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void gfx_draw_char(int x, int y, char c, uint8_t color) {
|
||||
if (c < FONT_FIRST || c > FONT_LAST) return;
|
||||
|
||||
const uint8_t *glyph = font8x16_data[c - FONT_FIRST];
|
||||
|
||||
/* Draw at half vertical scale: 8x8 pixels per character.
|
||||
* Sample every other row from the 8x16 font. */
|
||||
for (int row = 0; row < 8; row++) {
|
||||
uint8_t bits = glyph[row * 2]; /* Sample even rows */
|
||||
for (int col = 0; col < 8; col++) {
|
||||
if (bits & (0x80 >> col)) {
|
||||
gfx_pixel(x + col, y + row, color);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void gfx_draw_text(int x, int y, const char *str, uint8_t color) {
|
||||
int cx = x;
|
||||
while (*str) {
|
||||
if (*str == '\n') {
|
||||
cx = x;
|
||||
y += 8;
|
||||
} else {
|
||||
gfx_draw_char(cx, y, *str, color);
|
||||
cx += 8;
|
||||
}
|
||||
str++;
|
||||
}
|
||||
}
|
||||
173
src/graphics.h
Normal file
173
src/graphics.h
Normal file
@@ -0,0 +1,173 @@
|
||||
/**
|
||||
* @file graphics.h
|
||||
* @brief Graphics subsystem for ClaudeOS.
|
||||
*
|
||||
* Provides VGA mode 0x13 (320x200, 256 colors) graphics with
|
||||
* drawing primitives. Supports switching between text mode (0x03)
|
||||
* and graphics mode at runtime.
|
||||
*
|
||||
* Color palette:
|
||||
* 0-15 : Standard 16 VGA colors
|
||||
* 16-231: 6x6x6 RGB color cube (index = 16 + r*36 + g*6 + b, where r,g,b in 0-5)
|
||||
* 232-255: 24-step grayscale
|
||||
*/
|
||||
|
||||
#ifndef GRAPHICS_H
|
||||
#define GRAPHICS_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/** Graphics mode screen dimensions. */
|
||||
#define GFX_WIDTH 320
|
||||
#define GFX_HEIGHT 200
|
||||
|
||||
/** Framebuffer base address for mode 0x13. */
|
||||
#define GFX_FRAMEBUFFER 0xA0000
|
||||
|
||||
/** Graphics mode state. */
|
||||
#define GFX_MODE_TEXT 0
|
||||
#define GFX_MODE_GRAPHICS 1
|
||||
|
||||
/* ================================================================
|
||||
* Standard palette color indices (0-15)
|
||||
* ================================================================ */
|
||||
#define GFX_BLACK 0
|
||||
#define GFX_BLUE 1
|
||||
#define GFX_GREEN 2
|
||||
#define GFX_CYAN 3
|
||||
#define GFX_RED 4
|
||||
#define GFX_MAGENTA 5
|
||||
#define GFX_BROWN 6
|
||||
#define GFX_LIGHT_GREY 7
|
||||
#define GFX_DARK_GREY 8
|
||||
#define GFX_LIGHT_BLUE 9
|
||||
#define GFX_LIGHT_GREEN 10
|
||||
#define GFX_LIGHT_CYAN 11
|
||||
#define GFX_LIGHT_RED 12
|
||||
#define GFX_LIGHT_MAGENTA 13
|
||||
#define GFX_YELLOW 14
|
||||
#define GFX_WHITE 15
|
||||
|
||||
/* ================================================================
|
||||
* GFX syscall sub-commands
|
||||
* ================================================================ */
|
||||
#define GFX_CMD_ENTER 0 /**< Enter graphics mode. Returns 0. */
|
||||
#define GFX_CMD_LEAVE 1 /**< Leave graphics mode (back to text). Returns 0. */
|
||||
#define GFX_CMD_PIXEL 2 /**< Set pixel. ECX=(x|y<<16), EDX=color. */
|
||||
#define GFX_CMD_CLEAR 3 /**< Clear screen. ECX=color. */
|
||||
#define GFX_CMD_FILL_RECT 4 /**< Fill rect. ECX=ptr to gfx_rect_t. */
|
||||
#define GFX_CMD_LINE 5 /**< Draw line. ECX=ptr to gfx_line_t. */
|
||||
#define GFX_CMD_CIRCLE 6 /**< Draw filled circle. ECX=ptr to gfx_circle_t. */
|
||||
#define GFX_CMD_GET_INFO 7 /**< Get info. Returns (width | height<<16). */
|
||||
|
||||
/* ================================================================
|
||||
* Command structs (used with pointer-based sub-commands)
|
||||
* ================================================================ */
|
||||
|
||||
typedef struct {
|
||||
uint32_t x, y, w, h;
|
||||
uint32_t color;
|
||||
} gfx_rect_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t x1, y1, x2, y2;
|
||||
uint32_t color;
|
||||
} gfx_line_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t cx, cy, r;
|
||||
uint32_t color;
|
||||
} gfx_circle_t;
|
||||
|
||||
/* ================================================================
|
||||
* Public API
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Convert RGB (0-255 each) to a palette index.
|
||||
* Uses the 6x6x6 color cube (indices 16-231).
|
||||
*/
|
||||
static inline uint8_t gfx_rgb(uint8_t r, uint8_t g, uint8_t b) {
|
||||
return (uint8_t)(16 + (r / 51) * 36 + (g / 51) * 6 + (b / 51));
|
||||
}
|
||||
|
||||
/**
|
||||
* Enter graphics mode (VGA 0x13: 320x200x256).
|
||||
* Saves text mode state and initializes the color palette.
|
||||
*/
|
||||
void graphics_enter(void);
|
||||
|
||||
/**
|
||||
* Leave graphics mode and return to text mode (VGA 0x03: 80x25).
|
||||
* Restores the VGA font and text display.
|
||||
*/
|
||||
void graphics_leave(void);
|
||||
|
||||
/**
|
||||
* Get the current graphics mode.
|
||||
* @return GFX_MODE_TEXT or GFX_MODE_GRAPHICS.
|
||||
*/
|
||||
int graphics_get_mode(void);
|
||||
|
||||
/**
|
||||
* Set a single pixel.
|
||||
* @param x X coordinate (0 to GFX_WIDTH-1).
|
||||
* @param y Y coordinate (0 to GFX_HEIGHT-1).
|
||||
* @param color Palette color index (0-255).
|
||||
*/
|
||||
void gfx_pixel(int x, int y, uint8_t color);
|
||||
|
||||
/**
|
||||
* Clear the screen with a color.
|
||||
* @param color Palette color index.
|
||||
*/
|
||||
void gfx_clear(uint8_t color);
|
||||
|
||||
/**
|
||||
* Draw a filled rectangle.
|
||||
* @param x Top-left X.
|
||||
* @param y Top-left Y.
|
||||
* @param w Width.
|
||||
* @param h Height.
|
||||
* @param color Palette color index.
|
||||
*/
|
||||
void gfx_fill_rect(int x, int y, int w, int h, uint8_t color);
|
||||
|
||||
/**
|
||||
* Draw a line (Bresenham).
|
||||
* @param x1,y1 Start point.
|
||||
* @param x2,y2 End point.
|
||||
* @param color Palette color index.
|
||||
*/
|
||||
void gfx_draw_line(int x1, int y1, int x2, int y2, uint8_t color);
|
||||
|
||||
/**
|
||||
* Draw a filled circle.
|
||||
* @param cx,cy Center.
|
||||
* @param r Radius.
|
||||
* @param color Palette color index.
|
||||
*/
|
||||
void gfx_fill_circle(int cx, int cy, int r, uint8_t color);
|
||||
|
||||
/**
|
||||
* Draw a circle outline.
|
||||
* @param cx,cy Center.
|
||||
* @param r Radius.
|
||||
* @param color Palette color index.
|
||||
*/
|
||||
void gfx_draw_circle(int cx, int cy, int r, uint8_t color);
|
||||
|
||||
/**
|
||||
* Draw a text string using the 8x16 font (scaled to 8x8 in mode 13).
|
||||
* @param x,y Top-left position.
|
||||
* @param str Null-terminated string.
|
||||
* @param color Palette color index.
|
||||
*/
|
||||
void gfx_draw_text(int x, int y, const char *str, uint8_t color);
|
||||
|
||||
/**
|
||||
* Draw a single character using the 8x16 font at half vertical scale (8x8).
|
||||
*/
|
||||
void gfx_draw_char(int x, int y, char c, uint8_t color);
|
||||
|
||||
#endif /* GRAPHICS_H */
|
||||
616
src/ide.c
Normal file
616
src/ide.c
Normal file
@@ -0,0 +1,616 @@
|
||||
/**
|
||||
* @file ide.c
|
||||
* @brief IDE/ATA disk driver implementation.
|
||||
*
|
||||
* Probes the primary and secondary IDE channels for ATA hard drives and
|
||||
* ATAPI CD/DVD drives using PIO-mode IDENTIFY commands. Detected devices
|
||||
* are registered with the devicefs subsystem as block devices:
|
||||
* - ATA drives → "hdd" class (hdd1, hdd2, ...)
|
||||
* - ATAPI drives → "cd" class (cd1, cd2, ...)
|
||||
*
|
||||
* Supports PIO-mode sector reads and writes using 28-bit LBA addressing,
|
||||
* which covers drives up to 128 GiB.
|
||||
*/
|
||||
|
||||
#include "ide.h"
|
||||
#include "port_io.h"
|
||||
#include "devicefs.h"
|
||||
#include "sysfs.h"
|
||||
#include "driver.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Debug print helpers defined in kernel.c */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/** All detected IDE devices. */
|
||||
static ide_device_t ide_devices[IDE_MAX_DEVICES];
|
||||
|
||||
/* ================================================================
|
||||
* Low-level IDE I/O helpers
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Wait for the BSY flag to clear on the given channel.
|
||||
* Returns the final status byte, or 0xFF on timeout.
|
||||
*
|
||||
* @param io_base Channel I/O base port.
|
||||
* @return Status byte.
|
||||
*/
|
||||
static uint8_t ide_wait(uint16_t io_base) {
|
||||
uint8_t status;
|
||||
int timeout = 500000;
|
||||
do {
|
||||
status = inb(io_base + IDE_REG_STATUS);
|
||||
if (status == 0xFF) return 0xFF; /* Floating bus */
|
||||
if (--timeout == 0) return 0xFF;
|
||||
} while (status & IDE_STATUS_BSY);
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for BSY to clear and DRQ to set (data ready).
|
||||
* Returns 0 on success, -1 on timeout or error.
|
||||
*
|
||||
* @param io_base Channel I/O base port.
|
||||
* @return 0 on success, -1 on error/timeout.
|
||||
*/
|
||||
static int ide_wait_drq(uint16_t io_base) {
|
||||
uint8_t status;
|
||||
int timeout = 500000;
|
||||
do {
|
||||
status = inb(io_base + IDE_REG_STATUS);
|
||||
if (status == 0xFF) return -1;
|
||||
if (status & (IDE_STATUS_ERR | IDE_STATUS_DF)) return -1;
|
||||
if (--timeout == 0) return -1;
|
||||
} while ((status & (IDE_STATUS_BSY | IDE_STATUS_DRQ)) != IDE_STATUS_DRQ);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read 256 16-bit words (512 bytes) from the data register.
|
||||
*
|
||||
* @param io_base Channel I/O base port.
|
||||
* @param buf Destination buffer (must be at least 512 bytes).
|
||||
*/
|
||||
static void ide_read_buffer(uint16_t io_base, uint16_t *buf) {
|
||||
for (int i = 0; i < 256; i++) {
|
||||
buf[i] = inw(io_base + IDE_REG_DATA);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write 256 16-bit words (512 bytes) to the data register.
|
||||
*
|
||||
* @param io_base Channel I/O base port.
|
||||
* @param buf Source buffer (must be at least 512 bytes).
|
||||
*/
|
||||
static void ide_write_buffer(uint16_t io_base, const uint16_t *buf) {
|
||||
for (int i = 0; i < 256; i++) {
|
||||
outw(io_base + IDE_REG_DATA, buf[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform a software reset on an IDE channel.
|
||||
*
|
||||
* @param ctrl_base Channel control port.
|
||||
*/
|
||||
static void ide_soft_reset(uint16_t ctrl_base) {
|
||||
outb(ctrl_base, 0x04); /* Set SRST bit */
|
||||
/* Wait ~5 µs (several I/O reads) */
|
||||
for (int i = 0; i < 4; i++) inb(ctrl_base);
|
||||
outb(ctrl_base, 0x00); /* Clear SRST bit */
|
||||
/* Wait for BSY to clear */
|
||||
for (int i = 0; i < 4; i++) inb(ctrl_base);
|
||||
}
|
||||
|
||||
/**
|
||||
* Select a drive on a channel (master=0, slave=1).
|
||||
*
|
||||
* @param io_base Channel I/O base port.
|
||||
* @param drive 0 for master, 1 for slave.
|
||||
*/
|
||||
static void ide_select_drive(uint16_t io_base, uint8_t drive) {
|
||||
outb(io_base + IDE_REG_DRIVE_HEAD, 0xA0 | (drive << 4));
|
||||
/* Wait ~400 ns by reading status 4 times */
|
||||
for (int i = 0; i < 4; i++) inb(io_base + IDE_REG_STATUS);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* IDENTIFY command
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Send IDENTIFY (or IDENTIFY PACKET) to a drive and read the result.
|
||||
*
|
||||
* @param dev Pointer to the device descriptor to fill.
|
||||
* @return 0 on success, -1 if no device or error.
|
||||
*/
|
||||
static int ide_identify(ide_device_t *dev) {
|
||||
uint16_t io = dev->io_base;
|
||||
|
||||
/* Select the drive */
|
||||
ide_select_drive(io, dev->drive);
|
||||
|
||||
/* Check for floating bus (no device) — read status, if 0xFF, no drive */
|
||||
uint8_t check = inb(io + IDE_REG_STATUS);
|
||||
if (check == 0xFF) return -1;
|
||||
|
||||
/* Clear sector count and LBA registers */
|
||||
outb(io + IDE_REG_SECCOUNT, 0);
|
||||
outb(io + IDE_REG_LBA_LO, 0);
|
||||
outb(io + IDE_REG_LBA_MID, 0);
|
||||
outb(io + IDE_REG_LBA_HI, 0);
|
||||
|
||||
/* Send IDENTIFY command */
|
||||
outb(io + IDE_REG_COMMAND, IDE_CMD_IDENTIFY);
|
||||
|
||||
/* Read status — if 0 or 0xFF, no device */
|
||||
uint8_t status = inb(io + IDE_REG_STATUS);
|
||||
if (status == 0 || status == 0xFF) return -1;
|
||||
|
||||
/* Wait for BSY to clear */
|
||||
status = ide_wait(io);
|
||||
if (status == 0xFF) return -1;
|
||||
|
||||
/* Check if this is an ATAPI device (LBA_MID/HI will be non-zero) */
|
||||
uint8_t lba_mid = inb(io + IDE_REG_LBA_MID);
|
||||
uint8_t lba_hi = inb(io + IDE_REG_LBA_HI);
|
||||
|
||||
if (lba_mid == 0x14 && lba_hi == 0xEB) {
|
||||
/* ATAPI device — re-identify with IDENTIFY PACKET DEVICE */
|
||||
dev->type = IDE_TYPE_ATAPI;
|
||||
outb(io + IDE_REG_COMMAND, IDE_CMD_IDENTIFY_PKT);
|
||||
status = ide_wait(io);
|
||||
if (status == 0xFF) return -1;
|
||||
} else if (lba_mid == 0 && lba_hi == 0) {
|
||||
dev->type = IDE_TYPE_ATA;
|
||||
} else {
|
||||
/* Unknown device type */
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Wait for DRQ */
|
||||
if (ide_wait_drq(io) != 0) return -1;
|
||||
|
||||
/* Read 256 words of identification data */
|
||||
uint16_t identify_buf[256];
|
||||
ide_read_buffer(io, identify_buf);
|
||||
|
||||
/* Parse model string (words 27-46, each word is big-endian) */
|
||||
for (int i = 0; i < 20; i++) {
|
||||
dev->model[i * 2] = (char)(identify_buf[27 + i] >> 8);
|
||||
dev->model[i * 2 + 1] = (char)(identify_buf[27 + i] & 0xFF);
|
||||
}
|
||||
dev->model[40] = '\0';
|
||||
/* Trim trailing spaces */
|
||||
for (int i = 39; i >= 0; i--) {
|
||||
if (dev->model[i] == ' ') dev->model[i] = '\0';
|
||||
else break;
|
||||
}
|
||||
|
||||
/* Parse sector count (28-bit LBA: words 60-61) */
|
||||
if (dev->type == IDE_TYPE_ATA) {
|
||||
dev->sector_count = (uint32_t)identify_buf[60] |
|
||||
((uint32_t)identify_buf[61] << 16);
|
||||
dev->sector_size = 512;
|
||||
} else {
|
||||
/* ATAPI: sector count from READ CAPACITY, default to 0 */
|
||||
dev->sector_count = 0;
|
||||
dev->sector_size = 2048; /* Standard CD sector size */
|
||||
}
|
||||
|
||||
dev->present = 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Block device operations (for devicefs)
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Read sectors from an ATA drive using PIO.
|
||||
*/
|
||||
static int ide_block_read(void *dev_data, uint32_t lba,
|
||||
uint32_t count, void *buf) {
|
||||
ide_device_t *dev = (ide_device_t *)dev_data;
|
||||
if (!dev || dev->type != IDE_TYPE_ATA) return -1;
|
||||
|
||||
uint16_t io = dev->io_base;
|
||||
uint8_t *dest = (uint8_t *)buf;
|
||||
|
||||
for (uint32_t s = 0; s < count; s++) {
|
||||
uint32_t cur_lba = lba + s;
|
||||
|
||||
/* Select drive with LBA mode and top 4 LBA bits */
|
||||
outb(io + IDE_REG_DRIVE_HEAD,
|
||||
0xE0 | (dev->drive << 4) | ((cur_lba >> 24) & 0x0F));
|
||||
|
||||
/* Set sector count = 1 and LBA */
|
||||
outb(io + IDE_REG_SECCOUNT, 1);
|
||||
outb(io + IDE_REG_LBA_LO, cur_lba & 0xFF);
|
||||
outb(io + IDE_REG_LBA_MID, (cur_lba >> 8) & 0xFF);
|
||||
outb(io + IDE_REG_LBA_HI, (cur_lba >> 16) & 0xFF);
|
||||
|
||||
/* Send READ SECTORS command */
|
||||
outb(io + IDE_REG_COMMAND, IDE_CMD_READ_PIO);
|
||||
|
||||
/* Wait for data */
|
||||
if (ide_wait_drq(io) != 0) return -1;
|
||||
|
||||
/* Read 256 words (512 bytes) */
|
||||
ide_read_buffer(io, (uint16_t *)(dest + s * 512));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write sectors to an ATA drive using PIO.
|
||||
*/
|
||||
static int ide_block_write(void *dev_data, uint32_t lba,
|
||||
uint32_t count, const void *buf) {
|
||||
ide_device_t *dev = (ide_device_t *)dev_data;
|
||||
if (!dev || dev->type != IDE_TYPE_ATA) return -1;
|
||||
|
||||
uint16_t io = dev->io_base;
|
||||
const uint8_t *src = (const uint8_t *)buf;
|
||||
|
||||
for (uint32_t s = 0; s < count; s++) {
|
||||
uint32_t cur_lba = lba + s;
|
||||
|
||||
outb(io + IDE_REG_DRIVE_HEAD,
|
||||
0xE0 | (dev->drive << 4) | ((cur_lba >> 24) & 0x0F));
|
||||
|
||||
outb(io + IDE_REG_SECCOUNT, 1);
|
||||
outb(io + IDE_REG_LBA_LO, cur_lba & 0xFF);
|
||||
outb(io + IDE_REG_LBA_MID, (cur_lba >> 8) & 0xFF);
|
||||
outb(io + IDE_REG_LBA_HI, (cur_lba >> 16) & 0xFF);
|
||||
|
||||
outb(io + IDE_REG_COMMAND, IDE_CMD_WRITE_PIO);
|
||||
|
||||
if (ide_wait_drq(io) != 0) return -1;
|
||||
|
||||
ide_write_buffer(io, (const uint16_t *)(src + s * 512));
|
||||
|
||||
/* Flush cache: wait for BSY to clear after write */
|
||||
ide_wait(io);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return sector size for a device.
|
||||
*/
|
||||
static uint32_t ide_block_sector_size(void *dev_data) {
|
||||
ide_device_t *dev = (ide_device_t *)dev_data;
|
||||
return dev ? dev->sector_size : 512;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return total sector count for a device.
|
||||
*/
|
||||
static uint32_t ide_block_sector_count(void *dev_data) {
|
||||
ide_device_t *dev = (ide_device_t *)dev_data;
|
||||
return dev ? dev->sector_count : 0;
|
||||
}
|
||||
|
||||
/** Block operations for ATA/ATAPI devices. */
|
||||
static devicefs_block_ops_t ide_block_ops = {
|
||||
.read_sectors = ide_block_read,
|
||||
.write_sectors = ide_block_write,
|
||||
.sector_size = ide_block_sector_size,
|
||||
.sector_count = ide_block_sector_count,
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* Sysfs interface for /sys/ide
|
||||
* ================================================================
|
||||
*
|
||||
* Exposes per-device info:
|
||||
* /sys/ide/ → lists device names (hdd1, cd1, ...)
|
||||
* /sys/ide/hdd1/ → lists attributes (model, type, channel, drive,
|
||||
* sectors, sector_size)
|
||||
* /sys/ide/hdd1/model → "QEMU HARDDISK\n"
|
||||
* /sys/ide/hdd1/sectors → "0x00003800\n"
|
||||
*/
|
||||
|
||||
/** Integer to hex string helper for sysfs output. */
|
||||
static int ide_sysfs_hex(uint32_t val, char *buf, uint32_t buf_size) {
|
||||
static const char hex[] = "0123456789ABCDEF";
|
||||
if (buf_size < 12) return -1; /* "0x" + 8 hex + \n + \0 */
|
||||
buf[0] = '0'; buf[1] = 'x';
|
||||
for (int i = 7; i >= 0; i--) {
|
||||
buf[2 + (7 - i)] = hex[(val >> (i * 4)) & 0xF];
|
||||
}
|
||||
buf[10] = '\n'; buf[11] = '\0';
|
||||
return 11;
|
||||
}
|
||||
|
||||
/**
|
||||
* Split a path string into first component and remainder.
|
||||
* "hdd1/model" → first="hdd1", rest="model"
|
||||
*/
|
||||
static void ide_split_path(const char *path, char *first, uint32_t fsize,
|
||||
const char **rest) {
|
||||
while (*path == '/') path++;
|
||||
const char *s = path;
|
||||
while (*s && *s != '/') s++;
|
||||
uint32_t len = (uint32_t)(s - path);
|
||||
if (len >= fsize) len = fsize - 1;
|
||||
memcpy(first, path, len);
|
||||
first[len] = '\0';
|
||||
if (*s == '/') s++;
|
||||
*rest = s;
|
||||
}
|
||||
|
||||
/** Attribute names exposed for each device. */
|
||||
static const char *ide_sysfs_attrs[] = {
|
||||
"model", "type", "channel", "drive", "sectors", "sector_size"
|
||||
};
|
||||
#define IDE_SYSFS_NUM_ATTRS 6
|
||||
|
||||
/**
|
||||
* Find an IDE device by its devicefs name (hdd1, cd1, etc.).
|
||||
* Returns the device pointer, or NULL.
|
||||
*/
|
||||
static ide_device_t *ide_find_by_name(const char *name) {
|
||||
for (int i = 0; i < IDE_MAX_DEVICES; i++) {
|
||||
if (!ide_devices[i].present) continue;
|
||||
|
||||
/* Reconstruct the devicefs name for this device */
|
||||
const char *cls = (ide_devices[i].type == IDE_TYPE_ATA) ? "hdd" : "cd";
|
||||
uint32_t cls_len = strlen(cls);
|
||||
if (strncmp(name, cls, cls_len) != 0) continue;
|
||||
|
||||
/* Parse the number suffix */
|
||||
const char *num_str = name + cls_len;
|
||||
uint32_t num = 0;
|
||||
while (*num_str >= '0' && *num_str <= '9') {
|
||||
num = num * 10 + (*num_str - '0');
|
||||
num_str++;
|
||||
}
|
||||
if (*num_str != '\0' || num == 0) continue;
|
||||
|
||||
/* Count how many devices of this class precede idx i */
|
||||
uint32_t count = 0;
|
||||
for (int j = 0; j <= i; j++) {
|
||||
if (!ide_devices[j].present) continue;
|
||||
const char *jcls = (ide_devices[j].type == IDE_TYPE_ATA) ? "hdd" : "cd";
|
||||
if (strcmp(cls, jcls) == 0) count++;
|
||||
}
|
||||
if (count == num) return &ide_devices[i];
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sysfs list callback.
|
||||
* path="" → list device names (hdd1, cd1, ...)
|
||||
* path="hdd1" → list attributes
|
||||
*/
|
||||
static int ide_sysfs_list(void *ctx, const char *path, uint32_t idx,
|
||||
sysfs_entry_t *out) {
|
||||
(void)ctx;
|
||||
|
||||
if (path[0] == '\0') {
|
||||
/* List all present devices */
|
||||
uint32_t count = 0;
|
||||
for (int i = 0; i < IDE_MAX_DEVICES; i++) {
|
||||
if (!ide_devices[i].present) continue;
|
||||
if (count == idx) {
|
||||
memset(out, 0, sizeof(sysfs_entry_t));
|
||||
/* Build device name */
|
||||
const char *cls = (ide_devices[i].type == IDE_TYPE_ATA)
|
||||
? "hdd" : "cd";
|
||||
/* Count devices of this class up to and including i */
|
||||
uint32_t cls_count = 0;
|
||||
for (int j = 0; j <= i; j++) {
|
||||
if (!ide_devices[j].present) continue;
|
||||
const char *jcls = (ide_devices[j].type == IDE_TYPE_ATA)
|
||||
? "hdd" : "cd";
|
||||
if (strcmp(cls, jcls) == 0) cls_count++;
|
||||
}
|
||||
uint32_t clen = strlen(cls);
|
||||
memcpy(out->name, cls, clen);
|
||||
/* Append number as ASCII (max 1 digit for 4 devs) */
|
||||
out->name[clen] = '0' + (char)cls_count;
|
||||
out->name[clen + 1] = '\0';
|
||||
out->is_dir = 1;
|
||||
return 0;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* List attributes for a specific device */
|
||||
ide_device_t *dev = ide_find_by_name(path);
|
||||
if (!dev) return -1;
|
||||
|
||||
if (idx >= IDE_SYSFS_NUM_ATTRS) return -1;
|
||||
|
||||
memset(out, 0, sizeof(sysfs_entry_t));
|
||||
strncpy(out->name, ide_sysfs_attrs[idx], SYSFS_MAX_NAME - 1);
|
||||
out->is_dir = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sysfs read callback.
|
||||
* path="hdd1/model" → device model string
|
||||
* path="hdd1/sectors" → hex sector count
|
||||
*/
|
||||
static int ide_sysfs_read(void *ctx, const char *path, char *buf,
|
||||
uint32_t buf_size) {
|
||||
(void)ctx;
|
||||
|
||||
/* Split path into device name and attribute name */
|
||||
char dev_name[SYSFS_MAX_NAME];
|
||||
const char *attr;
|
||||
ide_split_path(path, dev_name, sizeof(dev_name), &attr);
|
||||
|
||||
ide_device_t *dev = ide_find_by_name(dev_name);
|
||||
if (!dev) return -1;
|
||||
if (attr[0] == '\0') return -1; /* no attribute specified */
|
||||
|
||||
if (strcmp(attr, "model") == 0) {
|
||||
uint32_t mlen = strlen(dev->model);
|
||||
if (mlen + 2 > buf_size) return -1;
|
||||
memcpy(buf, dev->model, mlen);
|
||||
buf[mlen] = '\n';
|
||||
buf[mlen + 1] = '\0';
|
||||
return (int)(mlen + 1);
|
||||
}
|
||||
if (strcmp(attr, "type") == 0) {
|
||||
const char *t = (dev->type == IDE_TYPE_ATA) ? "ATA\n" :
|
||||
(dev->type == IDE_TYPE_ATAPI) ? "ATAPI\n" : "unknown\n";
|
||||
uint32_t tlen = strlen(t);
|
||||
if (tlen + 1 > buf_size) return -1;
|
||||
memcpy(buf, t, tlen + 1);
|
||||
return (int)tlen;
|
||||
}
|
||||
if (strcmp(attr, "channel") == 0) {
|
||||
const char *c = (dev->channel == 0) ? "primary\n" : "secondary\n";
|
||||
uint32_t clen = strlen(c);
|
||||
if (clen + 1 > buf_size) return -1;
|
||||
memcpy(buf, c, clen + 1);
|
||||
return (int)clen;
|
||||
}
|
||||
if (strcmp(attr, "drive") == 0) {
|
||||
const char *d = (dev->drive == 0) ? "master\n" : "slave\n";
|
||||
uint32_t dlen = strlen(d);
|
||||
if (dlen + 1 > buf_size) return -1;
|
||||
memcpy(buf, d, dlen + 1);
|
||||
return (int)dlen;
|
||||
}
|
||||
if (strcmp(attr, "sectors") == 0) {
|
||||
return ide_sysfs_hex(dev->sector_count, buf, buf_size);
|
||||
}
|
||||
if (strcmp(attr, "sector_size") == 0) {
|
||||
return ide_sysfs_hex(dev->sector_size, buf, buf_size);
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sysfs write callback — IDE is read-only for now.
|
||||
*/
|
||||
static int ide_sysfs_write(void *ctx, const char *path, const char *buf,
|
||||
uint32_t size) {
|
||||
(void)ctx; (void)path; (void)buf; (void)size;
|
||||
return -1; /* Read-only */
|
||||
}
|
||||
|
||||
/** Sysfs operations for the IDE namespace. */
|
||||
static sysfs_ops_t ide_sysfs_ops = {
|
||||
.list = ide_sysfs_list,
|
||||
.read = ide_sysfs_read,
|
||||
.write = ide_sysfs_write,
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* Driver probe and init
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Probe: always return OK since IDE ports are standard.
|
||||
*/
|
||||
static driver_probe_result_t ide_probe(void) {
|
||||
return DRIVER_PROBE_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the IDE driver: scan channels and register devices.
|
||||
*/
|
||||
static int ide_driver_init(void) {
|
||||
memset(ide_devices, 0, sizeof(ide_devices));
|
||||
|
||||
/* Channel definitions: primary (0x1F0, 0x3F6), secondary (0x170, 0x376) */
|
||||
static const uint16_t io_bases[2] = { IDE_PRIMARY_IO, IDE_SECONDARY_IO };
|
||||
static const uint16_t ctrl_bases[2] = { IDE_PRIMARY_CTRL, IDE_SECONDARY_CTRL };
|
||||
|
||||
int found = 0;
|
||||
|
||||
for (int ch = 0; ch < 2; ch++) {
|
||||
/* Check if channel exists by reading status — 0xFF = floating bus */
|
||||
uint8_t ch_status = inb(io_bases[ch] + IDE_REG_STATUS);
|
||||
if (ch_status == 0xFF) {
|
||||
offset_print(" IDE: ");
|
||||
offset_print(ch == 0 ? "primary" : "secondary");
|
||||
offset_print(" channel not present\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Software reset the channel */
|
||||
ide_soft_reset(ctrl_bases[ch]);
|
||||
|
||||
for (int drv = 0; drv < 2; drv++) {
|
||||
int idx = ch * 2 + drv;
|
||||
ide_devices[idx].channel = (uint8_t)ch;
|
||||
ide_devices[idx].drive = (uint8_t)drv;
|
||||
ide_devices[idx].io_base = io_bases[ch];
|
||||
ide_devices[idx].ctrl_base = ctrl_bases[ch];
|
||||
ide_devices[idx].present = 0;
|
||||
|
||||
if (ide_identify(&ide_devices[idx]) == 0) {
|
||||
found++;
|
||||
|
||||
const char *class_name = (ide_devices[idx].type == IDE_TYPE_ATA)
|
||||
? "hdd" : "cd";
|
||||
const char *type_str = (ide_devices[idx].type == IDE_TYPE_ATA)
|
||||
? "ATA" : "ATAPI";
|
||||
|
||||
offset_print(" IDE: ");
|
||||
offset_print(type_str);
|
||||
offset_print(" device on ");
|
||||
offset_print(ch == 0 ? "primary" : "secondary");
|
||||
offset_print(drv == 0 ? " master" : " slave");
|
||||
offset_print(": ");
|
||||
offset_print(ide_devices[idx].model);
|
||||
offset_print(" (");
|
||||
print_hex(ide_devices[idx].sector_count);
|
||||
offset_print(" sectors)\n");
|
||||
|
||||
/* Register with devicefs */
|
||||
devicefs_register_block(class_name, &ide_block_ops,
|
||||
&ide_devices[idx]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (found == 0) {
|
||||
offset_print(" IDE: no devices found\n");
|
||||
} else {
|
||||
offset_print(" IDE: ");
|
||||
print_hex((uint32_t)found);
|
||||
offset_print(" device(s) found\n");
|
||||
}
|
||||
|
||||
/* Register sysfs namespace for IDE information */
|
||||
sysfs_register("ide", &ide_sysfs_ops, NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ide_init(void) {
|
||||
return ide_driver_init();
|
||||
}
|
||||
|
||||
ide_device_t *ide_get_device(int index) {
|
||||
if (index < 0 || index >= IDE_MAX_DEVICES) return NULL;
|
||||
if (!ide_devices[index].present) return NULL;
|
||||
return &ide_devices[index];
|
||||
}
|
||||
|
||||
/** IDE driver descriptor. */
|
||||
static const driver_t ide_driver = {
|
||||
.name = "ide",
|
||||
.probe = ide_probe,
|
||||
.init = ide_driver_init,
|
||||
};
|
||||
|
||||
REGISTER_DRIVER(ide_driver);
|
||||
88
src/ide.h
Normal file
88
src/ide.h
Normal file
@@ -0,0 +1,88 @@
|
||||
/**
|
||||
* @file ide.h
|
||||
* @brief IDE/ATA disk driver.
|
||||
*
|
||||
* Enumerates IDE devices on the primary and secondary channels, identifies
|
||||
* ATA hard drives and ATAPI CD-ROMs, and registers them with the devicefs
|
||||
* subsystem as block devices (hddN / cdN).
|
||||
*/
|
||||
|
||||
#ifndef IDE_H
|
||||
#define IDE_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/** Maximum number of IDE devices (2 channels × 2 drives). */
|
||||
#define IDE_MAX_DEVICES 4
|
||||
|
||||
/** IDE channel I/O port bases. */
|
||||
#define IDE_PRIMARY_IO 0x1F0
|
||||
#define IDE_PRIMARY_CTRL 0x3F6
|
||||
#define IDE_SECONDARY_IO 0x170
|
||||
#define IDE_SECONDARY_CTRL 0x376
|
||||
|
||||
/** IDE register offsets from I/O base. */
|
||||
#define IDE_REG_DATA 0x00
|
||||
#define IDE_REG_ERROR 0x01
|
||||
#define IDE_REG_FEATURES 0x01
|
||||
#define IDE_REG_SECCOUNT 0x02
|
||||
#define IDE_REG_LBA_LO 0x03
|
||||
#define IDE_REG_LBA_MID 0x04
|
||||
#define IDE_REG_LBA_HI 0x05
|
||||
#define IDE_REG_DRIVE_HEAD 0x06
|
||||
#define IDE_REG_STATUS 0x07
|
||||
#define IDE_REG_COMMAND 0x07
|
||||
|
||||
/** IDE status register bits. */
|
||||
#define IDE_STATUS_ERR 0x01 /**< Error occurred. */
|
||||
#define IDE_STATUS_DRQ 0x08 /**< Data request ready. */
|
||||
#define IDE_STATUS_SRV 0x10 /**< Overlapped mode service request. */
|
||||
#define IDE_STATUS_DF 0x20 /**< Drive fault. */
|
||||
#define IDE_STATUS_DRDY 0x40 /**< Drive ready. */
|
||||
#define IDE_STATUS_BSY 0x80 /**< Drive busy. */
|
||||
|
||||
/** IDE commands. */
|
||||
#define IDE_CMD_IDENTIFY 0xEC /**< ATA IDENTIFY DEVICE. */
|
||||
#define IDE_CMD_IDENTIFY_PKT 0xA1 /**< ATAPI IDENTIFY PACKET DEVICE. */
|
||||
#define IDE_CMD_READ_PIO 0x20 /**< Read sectors (PIO, 28-bit LBA). */
|
||||
#define IDE_CMD_WRITE_PIO 0x30 /**< Write sectors (PIO, 28-bit LBA). */
|
||||
|
||||
/** IDE device types. */
|
||||
#define IDE_TYPE_NONE 0 /**< No device present. */
|
||||
#define IDE_TYPE_ATA 1 /**< ATA hard disk. */
|
||||
#define IDE_TYPE_ATAPI 2 /**< ATAPI CD/DVD drive. */
|
||||
|
||||
/**
|
||||
* IDE device descriptor.
|
||||
*/
|
||||
typedef struct ide_device {
|
||||
uint8_t present; /**< 1 if device is present. */
|
||||
uint8_t type; /**< IDE_TYPE_ATA or IDE_TYPE_ATAPI. */
|
||||
uint8_t channel; /**< 0 = primary, 1 = secondary. */
|
||||
uint8_t drive; /**< 0 = master, 1 = slave. */
|
||||
uint16_t io_base; /**< I/O base port for this channel. */
|
||||
uint16_t ctrl_base; /**< Control port for this channel. */
|
||||
uint32_t sector_count; /**< Total sectors (28-bit LBA max). */
|
||||
uint32_t sector_size; /**< Sector size in bytes (usually 512). */
|
||||
char model[41]; /**< Model string from IDENTIFY. */
|
||||
} ide_device_t;
|
||||
|
||||
/**
|
||||
* Initialize the IDE driver.
|
||||
*
|
||||
* Scans primary and secondary channels for ATA/ATAPI devices,
|
||||
* reads their IDENTIFY data, and registers them with devicefs.
|
||||
*
|
||||
* @return Number of devices found.
|
||||
*/
|
||||
int ide_init(void);
|
||||
|
||||
/**
|
||||
* Get an IDE device by index (0–3).
|
||||
*
|
||||
* @param index Device index.
|
||||
* @return Pointer to the device descriptor, or NULL if invalid/not present.
|
||||
*/
|
||||
ide_device_t *ide_get_device(int index);
|
||||
|
||||
#endif /* IDE_H */
|
||||
141
src/idt.c
Normal file
141
src/idt.c
Normal file
@@ -0,0 +1,141 @@
|
||||
#include "idt.h"
|
||||
#include <string.h> // For memset
|
||||
|
||||
// The IDT itself
|
||||
idt_entry_t idt[256];
|
||||
idt_ptr_t idt_ptr;
|
||||
|
||||
// Helper to set a gate in the IDT
|
||||
static void set_idt_gate(uint8_t num, uint32_t base, uint16_t sel, uint8_t flags) {
|
||||
idt[num].base_lo = base & 0xFFFF;
|
||||
idt[num].base_hi = (base >> 16) & 0xFFFF;
|
||||
idt[num].sel = sel;
|
||||
idt[num].always0 = 0;
|
||||
// flags: 0x8E = 10001110 (Present, Ring0, 32-bit Interrupt Gate)
|
||||
idt[num].flags = flags;
|
||||
}
|
||||
|
||||
// Public version for other subsystems (e.g., syscall INT 0x80)
|
||||
void set_idt_gate_from_c(uint8_t num, uint32_t base, uint16_t sel, uint8_t flags) {
|
||||
set_idt_gate(num, base, sel, flags);
|
||||
}
|
||||
|
||||
// Exception Handlers (ISRs)
|
||||
extern void isr0();
|
||||
extern void isr1();
|
||||
extern void isr2();
|
||||
extern void isr3();
|
||||
extern void isr4();
|
||||
extern void isr5();
|
||||
extern void isr6();
|
||||
extern void isr7();
|
||||
extern void isr8();
|
||||
extern void isr9();
|
||||
extern void isr10();
|
||||
extern void isr11();
|
||||
extern void isr12();
|
||||
extern void isr13();
|
||||
extern void isr14();
|
||||
extern void isr15();
|
||||
extern void isr16();
|
||||
extern void isr17();
|
||||
extern void isr18();
|
||||
extern void isr19();
|
||||
extern void isr20();
|
||||
extern void isr21();
|
||||
extern void isr22();
|
||||
extern void isr23();
|
||||
extern void isr24();
|
||||
extern void isr25();
|
||||
extern void isr26();
|
||||
extern void isr27();
|
||||
extern void isr28();
|
||||
extern void isr29();
|
||||
extern void isr30();
|
||||
extern void isr31();
|
||||
|
||||
// Hardware Interrupt Handlers (IRQs)
|
||||
extern void irq0();
|
||||
extern void irq1();
|
||||
extern void irq2();
|
||||
extern void irq3();
|
||||
extern void irq4();
|
||||
extern void irq5();
|
||||
extern void irq6();
|
||||
extern void irq7();
|
||||
extern void irq8();
|
||||
extern void irq9();
|
||||
extern void irq10();
|
||||
extern void irq11();
|
||||
extern void irq12();
|
||||
extern void irq13();
|
||||
extern void irq14();
|
||||
extern void irq15();
|
||||
|
||||
void init_idt() {
|
||||
// 1. Set up the IDT pointer
|
||||
idt_ptr.limit = sizeof(idt_entry_t) * 256 - 1;
|
||||
idt_ptr.base = (uint32_t)&idt;
|
||||
|
||||
// 2. Clear the IDT
|
||||
memset(&idt, 0, sizeof(idt_entry_t) * 256);
|
||||
|
||||
// 3. Set the ISRs (Exceptions 0-31)
|
||||
// Code Selector is 0x08 usually (defined in GDT)
|
||||
// Flags: 0x8E = Present(1), DPL(00), Storage(0), GateType(1110 = 32-bit Int)
|
||||
set_idt_gate( 0, (uint32_t)isr0, 0x08, 0x8E);
|
||||
set_idt_gate( 1, (uint32_t)isr1, 0x08, 0x8E);
|
||||
set_idt_gate( 2, (uint32_t)isr2, 0x08, 0x8E);
|
||||
set_idt_gate( 3, (uint32_t)isr3, 0x08, 0x8E);
|
||||
set_idt_gate( 4, (uint32_t)isr4, 0x08, 0x8E);
|
||||
set_idt_gate( 5, (uint32_t)isr5, 0x08, 0x8E);
|
||||
set_idt_gate( 6, (uint32_t)isr6, 0x08, 0x8E);
|
||||
set_idt_gate( 7, (uint32_t)isr7, 0x08, 0x8E);
|
||||
set_idt_gate( 8, (uint32_t)isr8, 0x08, 0x8E);
|
||||
set_idt_gate( 9, (uint32_t)isr9, 0x08, 0x8E);
|
||||
set_idt_gate(10, (uint32_t)isr10, 0x08, 0x8E);
|
||||
set_idt_gate(11, (uint32_t)isr11, 0x08, 0x8E);
|
||||
set_idt_gate(12, (uint32_t)isr12, 0x08, 0x8E);
|
||||
set_idt_gate(13, (uint32_t)isr13, 0x08, 0x8E);
|
||||
set_idt_gate(14, (uint32_t)isr14, 0x08, 0x8E);
|
||||
set_idt_gate(15, (uint32_t)isr15, 0x08, 0x8E);
|
||||
set_idt_gate(16, (uint32_t)isr16, 0x08, 0x8E);
|
||||
set_idt_gate(17, (uint32_t)isr17, 0x08, 0x8E);
|
||||
set_idt_gate(18, (uint32_t)isr18, 0x08, 0x8E);
|
||||
set_idt_gate(19, (uint32_t)isr19, 0x08, 0x8E);
|
||||
set_idt_gate(20, (uint32_t)isr20, 0x08, 0x8E);
|
||||
set_idt_gate(21, (uint32_t)isr21, 0x08, 0x8E);
|
||||
set_idt_gate(22, (uint32_t)isr22, 0x08, 0x8E);
|
||||
set_idt_gate(23, (uint32_t)isr23, 0x08, 0x8E);
|
||||
set_idt_gate(24, (uint32_t)isr24, 0x08, 0x8E);
|
||||
set_idt_gate(25, (uint32_t)isr25, 0x08, 0x8E);
|
||||
set_idt_gate(26, (uint32_t)isr26, 0x08, 0x8E);
|
||||
set_idt_gate(27, (uint32_t)isr27, 0x08, 0x8E);
|
||||
set_idt_gate(28, (uint32_t)isr28, 0x08, 0x8E);
|
||||
set_idt_gate(29, (uint32_t)isr29, 0x08, 0x8E);
|
||||
set_idt_gate(30, (uint32_t)isr30, 0x08, 0x8E);
|
||||
set_idt_gate(31, (uint32_t)isr31, 0x08, 0x8E);
|
||||
|
||||
// 4. Set the IRQs (Remapped to 32-47)
|
||||
set_idt_gate(32, (uint32_t)irq0, 0x08, 0x8E);
|
||||
set_idt_gate(33, (uint32_t)irq1, 0x08, 0x8E);
|
||||
set_idt_gate(34, (uint32_t)irq2, 0x08, 0x8E);
|
||||
set_idt_gate(35, (uint32_t)irq3, 0x08, 0x8E);
|
||||
set_idt_gate(36, (uint32_t)irq4, 0x08, 0x8E);
|
||||
set_idt_gate(37, (uint32_t)irq5, 0x08, 0x8E);
|
||||
set_idt_gate(38, (uint32_t)irq6, 0x08, 0x8E);
|
||||
set_idt_gate(39, (uint32_t)irq7, 0x08, 0x8E);
|
||||
set_idt_gate(40, (uint32_t)irq8, 0x08, 0x8E);
|
||||
set_idt_gate(41, (uint32_t)irq9, 0x08, 0x8E);
|
||||
set_idt_gate(42, (uint32_t)irq10, 0x08, 0x8E);
|
||||
set_idt_gate(43, (uint32_t)irq11, 0x08, 0x8E);
|
||||
set_idt_gate(44, (uint32_t)irq12, 0x08, 0x8E);
|
||||
set_idt_gate(45, (uint32_t)irq13, 0x08, 0x8E);
|
||||
set_idt_gate(46, (uint32_t)irq14, 0x08, 0x8E);
|
||||
set_idt_gate(47, (uint32_t)irq15, 0x08, 0x8E);
|
||||
|
||||
// 5. Load the IDT using assembly instruction 'lidt'
|
||||
// We can use inline assembly or a helper function.
|
||||
// Assuming lid is available via inline asm or similar to gdt_flush
|
||||
__asm__ volatile("lidt (%0)" : : "r" (&idt_ptr));
|
||||
}
|
||||
33
src/idt.h
Normal file
33
src/idt.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef IDT_H
|
||||
#define IDT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* A struct describing an interrupt gate. */
|
||||
struct idt_entry_struct {
|
||||
uint16_t base_lo; // The lower 16 bits of the address to jump to when this interrupt fires.
|
||||
uint16_t sel; // Kernel segment selector.
|
||||
uint8_t always0; // This must always be zero.
|
||||
uint8_t flags; // More flags. See documentation.
|
||||
uint16_t base_hi; // The upper 16 bits of the address to jump to.
|
||||
} __attribute__((packed));
|
||||
|
||||
typedef struct idt_entry_struct idt_entry_t;
|
||||
|
||||
/* A struct describing a pointer to an array of interrupt handlers.
|
||||
* This is in a format suitable for giving to 'lidt'. */
|
||||
struct idt_ptr_struct {
|
||||
uint16_t limit;
|
||||
uint32_t base; // The address of the first element in our idt_entry_t array.
|
||||
} __attribute__((packed));
|
||||
|
||||
typedef struct idt_ptr_struct idt_ptr_t;
|
||||
|
||||
// These extern directives let us access the addresses of our ASM ISR handlers.
|
||||
extern void isr0();
|
||||
extern void isr1();
|
||||
// ... we will add more later ...
|
||||
|
||||
void init_idt();
|
||||
|
||||
#endif
|
||||
123
src/initrd_fs.c
Normal file
123
src/initrd_fs.c
Normal file
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* @file initrd_fs.c
|
||||
* @brief CPIO initial ramdisk VFS driver implementation.
|
||||
*
|
||||
* Provides a read-only VFS interface to the CPIO archive loaded at boot.
|
||||
* Files are accessed directly from the archive memory (zero-copy reads).
|
||||
*/
|
||||
|
||||
#include "initrd_fs.h"
|
||||
#include "vfs.h"
|
||||
#include "cpio.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Debug print helpers defined in kernel.c */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/**
|
||||
* Read from a file in the initrd.
|
||||
* Data is read directly from the CPIO archive (memory-mapped).
|
||||
*/
|
||||
static int32_t initrd_read(vfs_node_t *node, uint32_t offset,
|
||||
uint32_t size, void *buf) {
|
||||
if (!node || !node->fs_data || !buf) return -1;
|
||||
|
||||
/* fs_data points to the file's data within the CPIO archive */
|
||||
const uint8_t *data = (const uint8_t *)node->fs_data;
|
||||
uint32_t file_size = node->size;
|
||||
|
||||
if (offset >= file_size) return 0;
|
||||
|
||||
uint32_t remaining = file_size - offset;
|
||||
if (size > remaining) size = remaining;
|
||||
|
||||
memcpy(buf, data + offset, size);
|
||||
return (int32_t)size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read a directory entry from the initrd root.
|
||||
* The initrd is a flat archive — all files are at the root level.
|
||||
*/
|
||||
static int initrd_readdir(vfs_node_t *dir, uint32_t idx, vfs_dirent_t *out) {
|
||||
(void)dir;
|
||||
|
||||
uint32_t off = 0;
|
||||
uint32_t current = 0;
|
||||
cpio_entry_t entry;
|
||||
|
||||
while (cpio_next(&off, &entry) == 0) {
|
||||
/* Skip the "." directory entry if present */
|
||||
if (entry.name[0] == '.' && entry.name[1] == '\0') continue;
|
||||
|
||||
/* Strip "./" prefix */
|
||||
const char *name = entry.name;
|
||||
if (name[0] == '.' && name[1] == '/') name += 2;
|
||||
|
||||
/* Skip empty names */
|
||||
if (*name == '\0') continue;
|
||||
|
||||
if (current == idx) {
|
||||
memset(out, 0, sizeof(vfs_dirent_t));
|
||||
strncpy(out->name, name, VFS_MAX_NAME - 1);
|
||||
out->inode = current;
|
||||
out->type = VFS_FILE;
|
||||
return 0;
|
||||
}
|
||||
current++;
|
||||
}
|
||||
|
||||
return -1; /* No more entries */
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a file by name within the initrd.
|
||||
*/
|
||||
static int initrd_finddir(vfs_node_t *dir, const char *name, vfs_node_t *out) {
|
||||
(void)dir;
|
||||
|
||||
cpio_entry_t entry;
|
||||
if (cpio_find(name, &entry) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(out, 0, sizeof(vfs_node_t));
|
||||
/* Strip "./" prefix for the node name */
|
||||
const char *display_name = entry.name;
|
||||
if (display_name[0] == '.' && display_name[1] == '/') {
|
||||
display_name += 2;
|
||||
}
|
||||
strncpy(out->name, display_name, VFS_MAX_NAME - 1);
|
||||
out->type = VFS_FILE;
|
||||
out->size = entry.datasize;
|
||||
out->mode = entry.mode;
|
||||
out->fs_data = (void *)entry.data; /* Direct pointer into CPIO archive */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** Filesystem operations for the initrd. */
|
||||
static vfs_fs_ops_t initrd_ops = {
|
||||
.open = NULL, /* No special open needed */
|
||||
.close = NULL, /* No special close needed */
|
||||
.read = initrd_read,
|
||||
.write = NULL, /* Read-only */
|
||||
.readdir = initrd_readdir,
|
||||
.finddir = initrd_finddir,
|
||||
};
|
||||
|
||||
int init_initrd_fs(void) {
|
||||
if (cpio_count() == 0) {
|
||||
offset_print(" INITRD_FS: no files in ramdisk\n");
|
||||
}
|
||||
|
||||
int ret = vfs_mount("/initrd", &initrd_ops, NULL);
|
||||
if (ret != 0) {
|
||||
offset_print(" INITRD_FS: failed to mount\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
offset_print(" INITRD_FS: mounted at /initrd\n");
|
||||
return 0;
|
||||
}
|
||||
20
src/initrd_fs.h
Normal file
20
src/initrd_fs.h
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* @file initrd_fs.h
|
||||
* @brief CPIO initial ramdisk VFS driver.
|
||||
*
|
||||
* Provides a read-only filesystem backed by the CPIO initial ramdisk.
|
||||
* Mounted at "/initrd" to expose the contents of the ramdisk via the VFS.
|
||||
*/
|
||||
|
||||
#ifndef INITRD_FS_H
|
||||
#define INITRD_FS_H
|
||||
|
||||
/**
|
||||
* Initialize the initrd filesystem driver and mount it at "/initrd".
|
||||
* Must be called after init_vfs() and cpio_init().
|
||||
*
|
||||
* @return 0 on success, -1 on failure.
|
||||
*/
|
||||
int init_initrd_fs(void);
|
||||
|
||||
#endif /* INITRD_FS_H */
|
||||
201
src/interrupts.S
Normal file
201
src/interrupts.S
Normal file
@@ -0,0 +1,201 @@
|
||||
.section .text
|
||||
.global idt_load
|
||||
.type idt_load, @function
|
||||
idt_load:
|
||||
mov 4(%esp), %eax
|
||||
lidt (%eax)
|
||||
ret
|
||||
|
||||
/* Common ISR stub */
|
||||
isr_common_stub:
|
||||
pusha
|
||||
|
||||
/* Save segment registers */
|
||||
mov %ds, %ax
|
||||
push %eax
|
||||
|
||||
/* Load kernel data segment */
|
||||
mov $0x10, %ax
|
||||
mov %ax, %ds
|
||||
mov %ax, %es
|
||||
mov %ax, %fs
|
||||
mov %ax, %gs
|
||||
|
||||
/* Push pointer to stack structure as argument for C handler */
|
||||
push %esp
|
||||
call isr_handler
|
||||
add $4, %esp /* Clean up pushed pointer */
|
||||
|
||||
/* Restore segment registers */
|
||||
pop %eax
|
||||
mov %eax, %ds
|
||||
mov %eax, %es
|
||||
mov %eax, %fs
|
||||
mov %eax, %gs
|
||||
|
||||
popa
|
||||
add $8, %esp /* Cleans up error code and ISR number */
|
||||
iret
|
||||
|
||||
/* Macro for exceptions with NO Error Code */
|
||||
.macro ISR_NOERRCODE num
|
||||
.global isr\num
|
||||
.type isr\num, @function
|
||||
isr\num:
|
||||
cli
|
||||
push $0
|
||||
push $\num
|
||||
jmp isr_common_stub
|
||||
.endm
|
||||
|
||||
/* Macro for exceptions WITH Error Code */
|
||||
.macro ISR_ERRCODE num
|
||||
.global isr\num
|
||||
.type isr\num, @function
|
||||
isr\num:
|
||||
cli
|
||||
push $\num
|
||||
jmp isr_common_stub
|
||||
.endm
|
||||
|
||||
ISR_NOERRCODE 0
|
||||
ISR_NOERRCODE 1
|
||||
ISR_NOERRCODE 2
|
||||
ISR_NOERRCODE 3
|
||||
ISR_NOERRCODE 4
|
||||
ISR_NOERRCODE 5
|
||||
ISR_NOERRCODE 6
|
||||
ISR_NOERRCODE 7
|
||||
ISR_ERRCODE 8
|
||||
ISR_NOERRCODE 9
|
||||
ISR_ERRCODE 10
|
||||
ISR_ERRCODE 11
|
||||
ISR_ERRCODE 12
|
||||
ISR_ERRCODE 13
|
||||
ISR_ERRCODE 14
|
||||
ISR_NOERRCODE 15
|
||||
ISR_NOERRCODE 16
|
||||
ISR_ERRCODE 17
|
||||
ISR_NOERRCODE 18
|
||||
ISR_NOERRCODE 19
|
||||
ISR_NOERRCODE 20
|
||||
ISR_NOERRCODE 21
|
||||
ISR_NOERRCODE 22
|
||||
ISR_NOERRCODE 23
|
||||
ISR_NOERRCODE 24
|
||||
ISR_NOERRCODE 25
|
||||
ISR_NOERRCODE 26
|
||||
ISR_NOERRCODE 27
|
||||
ISR_NOERRCODE 28
|
||||
ISR_NOERRCODE 29
|
||||
ISR_ERRCODE 30
|
||||
ISR_NOERRCODE 31
|
||||
/* Macro for IRQs (Hardware Interrupts) */
|
||||
.macro ISR_IRQ num, idt_index
|
||||
.global irq\num
|
||||
.type irq\num, @function
|
||||
irq\num:
|
||||
cli
|
||||
push $0
|
||||
push $\idt_index
|
||||
jmp isr_common_stub
|
||||
.endm
|
||||
|
||||
/* Hardware Interrupts */
|
||||
ISR_IRQ 0, 32
|
||||
ISR_IRQ 1, 33
|
||||
ISR_IRQ 2, 34
|
||||
ISR_IRQ 3, 35
|
||||
ISR_IRQ 4, 36
|
||||
ISR_IRQ 5, 37
|
||||
ISR_IRQ 6, 38
|
||||
ISR_IRQ 7, 39
|
||||
ISR_IRQ 8, 40
|
||||
ISR_IRQ 9, 41
|
||||
ISR_IRQ 10, 42
|
||||
ISR_IRQ 11, 43
|
||||
ISR_IRQ 12, 44
|
||||
ISR_IRQ 13, 45
|
||||
ISR_IRQ 14, 46
|
||||
ISR_IRQ 15, 47
|
||||
|
||||
/*
|
||||
* INT 0x80 - System call entry point.
|
||||
* Uses the same isr_common_stub so the register layout matches registers_t.
|
||||
*/
|
||||
.global isr128
|
||||
.type isr128, @function
|
||||
isr128:
|
||||
cli
|
||||
push $0 /* Fake error code */
|
||||
push $0x80 /* Interrupt number 128 */
|
||||
jmp isr_common_stub
|
||||
|
||||
/*
|
||||
* tss_flush - Load the Task Register with the TSS selector.
|
||||
* TSS is GDT entry 5, selector = 5*8 = 0x28. With RPL=0: 0x28.
|
||||
*/
|
||||
.global tss_flush
|
||||
.type tss_flush, @function
|
||||
tss_flush:
|
||||
mov $0x28, %ax
|
||||
ltr %ax
|
||||
ret
|
||||
|
||||
/*
|
||||
* enter_usermode - Switch to Ring 3 user mode via iret.
|
||||
* void enter_usermode(uint32_t eip, uint32_t esp);
|
||||
*
|
||||
* Builds an iret frame on the stack:
|
||||
* SS = 0x23 (user data)
|
||||
* ESP = user stack pointer
|
||||
* EFLAGS = IF=1
|
||||
* CS = 0x1B (user code)
|
||||
* EIP = user entry point
|
||||
*/
|
||||
.global enter_usermode
|
||||
.type enter_usermode, @function
|
||||
enter_usermode:
|
||||
mov 4(%esp), %ecx /* user EIP */
|
||||
mov 8(%esp), %edx /* user ESP */
|
||||
|
||||
/* Set data segment registers to user data segment */
|
||||
mov $0x23, %ax
|
||||
mov %ax, %ds
|
||||
mov %ax, %es
|
||||
mov %ax, %fs
|
||||
mov %ax, %gs
|
||||
|
||||
/* Build iret frame */
|
||||
push $0x23 /* SS (user data) */
|
||||
push %edx /* ESP (user stack) */
|
||||
pushf /* EFLAGS */
|
||||
orl $0x200, (%esp) /* Ensure IF (Interrupt Flag) is set */
|
||||
push $0x1B /* CS (user code) */
|
||||
push %ecx /* EIP (entry point) */
|
||||
iret
|
||||
|
||||
/*
|
||||
* process_switch_to_user - Restore full register state and iret to user mode.
|
||||
* void process_switch_to_user(registers_t *regs);
|
||||
*
|
||||
* Used by process_exit to context-switch to the next process when the normal
|
||||
* interrupt-return path isn't available (because we're not returning through
|
||||
* an ISR stub). Loads all registers from the registers_t struct and performs
|
||||
* iret to enter user mode.
|
||||
*/
|
||||
.global process_switch_to_user
|
||||
.type process_switch_to_user, @function
|
||||
process_switch_to_user:
|
||||
movl 4(%esp), %esp /* Point ESP to the registers_t struct */
|
||||
|
||||
/* Restore segment register (ds → all data segments) */
|
||||
pop %eax
|
||||
mov %ax, %ds
|
||||
mov %ax, %es
|
||||
mov %ax, %fs
|
||||
mov %ax, %gs
|
||||
|
||||
popa /* Restore EAX-EDI */
|
||||
addl $8, %esp /* Skip int_no and err_code */
|
||||
iret /* Pops EIP, CS, EFLAGS, UserESP, SS */
|
||||
279
src/ipv4.c
Normal file
279
src/ipv4.c
Normal file
@@ -0,0 +1,279 @@
|
||||
/**
|
||||
* @file ipv4.c
|
||||
* @brief IPv4 network layer implementation.
|
||||
*
|
||||
* Handles IPv4 packet construction, parsing, checksum calculation,
|
||||
* and routing to the appropriate Ethernet interface.
|
||||
*
|
||||
* Incoming Ethernet frames with EtherType 0x0800 are passed to
|
||||
* ipv4_receive(), which validates the header and dispatches to
|
||||
* registered protocol handlers (ICMP, UDP, TCP, etc.).
|
||||
*/
|
||||
|
||||
#include "ipv4.h"
|
||||
#include "ethernet.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Debug print helpers */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/* ================================================================
|
||||
* Global state
|
||||
* ================================================================ */
|
||||
|
||||
/** Maximum number of registered protocol handlers. */
|
||||
#define MAX_PROTO_HANDLERS 16
|
||||
|
||||
/** Protocol handler entry. */
|
||||
typedef struct {
|
||||
uint8_t protocol;
|
||||
ipv4_proto_handler_t handler;
|
||||
} proto_handler_t;
|
||||
|
||||
/** Registered protocol handlers. */
|
||||
static proto_handler_t proto_handlers[MAX_PROTO_HANDLERS];
|
||||
static uint32_t proto_handler_count = 0;
|
||||
|
||||
/** Global IP identification counter. */
|
||||
static uint16_t ip_id_counter = 1;
|
||||
|
||||
/* ================================================================
|
||||
* Checksum
|
||||
* ================================================================ */
|
||||
|
||||
uint16_t ipv4_checksum(const void *data, uint32_t len) {
|
||||
const uint16_t *words = (const uint16_t *)data;
|
||||
uint32_t sum = 0;
|
||||
|
||||
while (len > 1) {
|
||||
sum += *words++;
|
||||
len -= 2;
|
||||
}
|
||||
|
||||
/* Add left-over byte, if any */
|
||||
if (len == 1) {
|
||||
sum += *(const uint8_t *)words;
|
||||
}
|
||||
|
||||
/* Fold 32-bit sum into 16 bits */
|
||||
while (sum >> 16) {
|
||||
sum = (sum & 0xFFFF) + (sum >> 16);
|
||||
}
|
||||
|
||||
return (uint16_t)(~sum);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Address conversion helpers
|
||||
* ================================================================ */
|
||||
|
||||
uint32_t ipv4_aton(const char *str) {
|
||||
uint32_t octets[4] = {0};
|
||||
int idx = 0;
|
||||
|
||||
for (const char *p = str; *p && idx < 4; p++) {
|
||||
if (*p == '.') {
|
||||
idx++;
|
||||
} else if (*p >= '0' && *p <= '9') {
|
||||
octets[idx] = octets[idx] * 10 + (uint32_t)(*p - '0');
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return (octets[0] << 24) | (octets[1] << 16) |
|
||||
(octets[2] << 8) | octets[3];
|
||||
}
|
||||
|
||||
char *ipv4_ntoa(uint32_t ip, char *buf, uint32_t size) {
|
||||
if (size < 16) { buf[0] = '\0'; return buf; }
|
||||
|
||||
int pos = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
if (i > 0) buf[pos++] = '.';
|
||||
uint8_t octet = (uint8_t)((ip >> (24 - i * 8)) & 0xFF);
|
||||
if (octet >= 100) {
|
||||
buf[pos++] = (char)('0' + octet / 100);
|
||||
buf[pos++] = (char)('0' + (octet % 100) / 10);
|
||||
buf[pos++] = (char)('0' + octet % 10);
|
||||
} else if (octet >= 10) {
|
||||
buf[pos++] = (char)('0' + octet / 10);
|
||||
buf[pos++] = (char)('0' + octet % 10);
|
||||
} else {
|
||||
buf[pos++] = (char)('0' + octet);
|
||||
}
|
||||
}
|
||||
buf[pos] = '\0';
|
||||
return buf;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Routing
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Find the Ethernet interface for a given destination IP.
|
||||
*
|
||||
* Simple routing: check if dst is on a directly-connected subnet.
|
||||
* If not, use the first interface with a configured gateway.
|
||||
* Fallback: first interface.
|
||||
*
|
||||
* @param dst_ip Destination IP (host byte order).
|
||||
* @return Interface index, or -1 if no interfaces.
|
||||
*/
|
||||
static int ipv4_route(uint32_t dst_ip) {
|
||||
uint32_t count = ethernet_get_iface_count();
|
||||
if (count == 0) return -1;
|
||||
|
||||
/* Check for directly-connected subnet */
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
eth_iface_t *iface = ethernet_get_iface(i);
|
||||
if (!iface || !iface->active) continue;
|
||||
if (iface->ip_addr == 0 || iface->netmask == 0) continue;
|
||||
|
||||
if ((dst_ip & iface->netmask) == (iface->ip_addr & iface->netmask)) {
|
||||
return (int)i;
|
||||
}
|
||||
}
|
||||
|
||||
/* Use first interface with a gateway */
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
eth_iface_t *iface = ethernet_get_iface(i);
|
||||
if (!iface || !iface->active) continue;
|
||||
if (iface->gateway != 0) return (int)i;
|
||||
}
|
||||
|
||||
/* Fallback: first active interface */
|
||||
for (uint32_t i = 0; i < count; i++) {
|
||||
eth_iface_t *iface = ethernet_get_iface(i);
|
||||
if (iface && iface->active) return (int)i;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Send
|
||||
* ================================================================ */
|
||||
|
||||
int ipv4_send_iface(uint32_t iface_idx, uint32_t dst_ip, uint8_t protocol,
|
||||
const void *payload, uint32_t len) {
|
||||
if (len > IPV4_MTU) return -1;
|
||||
|
||||
eth_iface_t *iface = ethernet_get_iface(iface_idx);
|
||||
if (!iface || !iface->active) return -1;
|
||||
|
||||
/* Build IPv4 packet on stack */
|
||||
uint8_t packet[ETH_MTU];
|
||||
ipv4_header_t *hdr = (ipv4_header_t *)packet;
|
||||
|
||||
hdr->ihl_version = 0x45; /* IPv4, IHL=5 (20 bytes) */
|
||||
hdr->tos = 0;
|
||||
hdr->total_length = htons((uint16_t)(IPV4_HLEN + len));
|
||||
hdr->identification = htons(ip_id_counter++);
|
||||
hdr->flags_fragoff = htons(IPV4_FLAG_DF); /* Don't fragment */
|
||||
hdr->ttl = 64;
|
||||
hdr->protocol = protocol;
|
||||
hdr->checksum = 0;
|
||||
hdr->src_ip = htonl(iface->ip_addr);
|
||||
hdr->dst_ip = htonl(dst_ip);
|
||||
|
||||
/* Compute header checksum */
|
||||
hdr->checksum = ipv4_checksum(hdr, IPV4_HLEN);
|
||||
|
||||
/* Copy payload */
|
||||
memcpy(packet + IPV4_HLEN, payload, len);
|
||||
|
||||
/* Determine destination MAC.
|
||||
* For now, use broadcast (FF:FF:FF:FF:FF:FF) — the ARP subsystem
|
||||
* will override this once implemented. */
|
||||
uint8_t dst_mac[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
|
||||
/* TODO: ARP lookup for dst_ip (or gateway if not on-link) */
|
||||
|
||||
return ethernet_send(iface, dst_mac, ETHERTYPE_IPV4, packet,
|
||||
IPV4_HLEN + len);
|
||||
}
|
||||
|
||||
int ipv4_send(uint32_t dst_ip, uint8_t protocol,
|
||||
const void *payload, uint32_t len) {
|
||||
int iface_idx = ipv4_route(dst_ip);
|
||||
if (iface_idx < 0) return -1;
|
||||
return ipv4_send_iface((uint32_t)iface_idx, dst_ip, protocol, payload, len);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Receive
|
||||
* ================================================================ */
|
||||
|
||||
void ipv4_receive(const void *data, uint32_t len, uint32_t iface_idx) {
|
||||
if (len < IPV4_HLEN) return;
|
||||
|
||||
const ipv4_header_t *hdr = (const ipv4_header_t *)data;
|
||||
|
||||
/* Verify version */
|
||||
if ((hdr->ihl_version >> 4) != 4) return;
|
||||
|
||||
/* Verify header length */
|
||||
uint32_t ihl = (uint32_t)(hdr->ihl_version & 0x0F) * 4;
|
||||
if (ihl < IPV4_HLEN || ihl > len) return;
|
||||
|
||||
/* Verify checksum */
|
||||
if (ipv4_checksum(data, ihl) != 0) return;
|
||||
|
||||
uint16_t total_len = ntohs(hdr->total_length);
|
||||
if (total_len > len) return;
|
||||
|
||||
uint32_t src_ip = ntohl(hdr->src_ip);
|
||||
uint32_t dst_ip = ntohl(hdr->dst_ip);
|
||||
const uint8_t *payload = (const uint8_t *)data + ihl;
|
||||
uint32_t payload_len = total_len - ihl;
|
||||
|
||||
/* Check if this packet is for us */
|
||||
eth_iface_t *iface = ethernet_get_iface(iface_idx);
|
||||
if (iface && iface->ip_addr != 0) {
|
||||
if (dst_ip != iface->ip_addr &&
|
||||
dst_ip != 0xFFFFFFFF && /* broadcast */
|
||||
(dst_ip & ~iface->netmask) != ~iface->netmask) {
|
||||
return; /* Not for us */
|
||||
}
|
||||
}
|
||||
|
||||
/* Dispatch to registered protocol handler */
|
||||
for (uint32_t i = 0; i < proto_handler_count; i++) {
|
||||
if (proto_handlers[i].protocol == hdr->protocol) {
|
||||
proto_handlers[i].handler(src_ip, dst_ip, payload,
|
||||
payload_len, iface_idx);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/* No handler registered for this protocol */
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Protocol registration
|
||||
* ================================================================ */
|
||||
|
||||
int ipv4_register_proto(uint8_t protocol, ipv4_proto_handler_t handler) {
|
||||
if (proto_handler_count >= MAX_PROTO_HANDLERS) return -1;
|
||||
|
||||
proto_handlers[proto_handler_count].protocol = protocol;
|
||||
proto_handlers[proto_handler_count].handler = handler;
|
||||
proto_handler_count++;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Initialization
|
||||
* ================================================================ */
|
||||
|
||||
void ipv4_init(void) {
|
||||
memset(proto_handlers, 0, sizeof(proto_handlers));
|
||||
proto_handler_count = 0;
|
||||
ip_id_counter = 1;
|
||||
|
||||
offset_print(" IPv4: initialized\n");
|
||||
}
|
||||
158
src/ipv4.h
Normal file
158
src/ipv4.h
Normal file
@@ -0,0 +1,158 @@
|
||||
/**
|
||||
* @file ipv4.h
|
||||
* @brief IPv4 network layer.
|
||||
*
|
||||
* Provides IPv4 packet construction, parsing, and routing.
|
||||
* Sits on top of the Ethernet subsystem and provides the foundation
|
||||
* for higher-level protocols (ICMP, UDP, TCP).
|
||||
*
|
||||
* The IPv4 stack maintains a simple routing table: each Ethernet
|
||||
* interface has an IP address, netmask, and default gateway.
|
||||
* Outbound packets are routed to the appropriate interface.
|
||||
*
|
||||
* Inbound packets are dispatched to registered protocol handlers
|
||||
* based on the IP protocol field.
|
||||
*/
|
||||
|
||||
#ifndef IPV4_H
|
||||
#define IPV4_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* ================================================================
|
||||
* IPv4 header (20 bytes minimum, no options)
|
||||
* ================================================================ */
|
||||
|
||||
/** IPv4 header structure (network byte order in memory). */
|
||||
typedef struct __attribute__((packed)) ipv4_header {
|
||||
uint8_t ihl_version; /**< Version (4) and IHL (5 for no options). */
|
||||
uint8_t tos; /**< Type of Service. */
|
||||
uint16_t total_length; /**< Total datagram length (header + data). */
|
||||
uint16_t identification; /**< Fragment identification. */
|
||||
uint16_t flags_fragoff; /**< Flags (3 bits) + Fragment Offset (13 bits). */
|
||||
uint8_t ttl; /**< Time to Live. */
|
||||
uint8_t protocol; /**< Upper-layer protocol number. */
|
||||
uint16_t checksum; /**< Header checksum. */
|
||||
uint32_t src_ip; /**< Source IP address (network byte order). */
|
||||
uint32_t dst_ip; /**< Destination IP address (network byte order). */
|
||||
} ipv4_header_t;
|
||||
|
||||
/** IPv4 header size (no options). */
|
||||
#define IPV4_HLEN 20
|
||||
|
||||
/** Maximum IPv4 payload over Ethernet (1500 - 20). */
|
||||
#define IPV4_MTU 1480
|
||||
|
||||
/* ================================================================
|
||||
* Protocol numbers
|
||||
* ================================================================ */
|
||||
#define IP_PROTO_ICMP 1
|
||||
#define IP_PROTO_TCP 6
|
||||
#define IP_PROTO_UDP 17
|
||||
|
||||
/* ================================================================
|
||||
* IPv4 flags
|
||||
* ================================================================ */
|
||||
#define IPV4_FLAG_DF 0x4000 /**< Don't Fragment. */
|
||||
#define IPV4_FLAG_MF 0x2000 /**< More Fragments. */
|
||||
|
||||
/* ================================================================
|
||||
* Protocol handler callback
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Callback for handling incoming IPv4 packets of a specific protocol.
|
||||
*
|
||||
* @param src_ip Source IP address (host byte order).
|
||||
* @param dst_ip Destination IP address (host byte order).
|
||||
* @param payload Protocol payload (past IPv4 header).
|
||||
* @param len Payload length.
|
||||
* @param iface_idx Ethernet interface index the packet arrived on.
|
||||
*/
|
||||
typedef void (*ipv4_proto_handler_t)(uint32_t src_ip, uint32_t dst_ip,
|
||||
const void *payload, uint32_t len,
|
||||
uint32_t iface_idx);
|
||||
|
||||
/* ================================================================
|
||||
* Public API
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Initialize the IPv4 subsystem.
|
||||
*/
|
||||
void ipv4_init(void);
|
||||
|
||||
/**
|
||||
* Send an IPv4 packet.
|
||||
*
|
||||
* Automatically routes to the correct Ethernet interface based on
|
||||
* the destination IP and configured routes.
|
||||
*
|
||||
* @param dst_ip Destination IP (host byte order).
|
||||
* @param protocol IP protocol number (e.g., IP_PROTO_UDP).
|
||||
* @param payload Packet payload.
|
||||
* @param len Payload length (max IPV4_MTU).
|
||||
* @return 0 on success, -1 on failure.
|
||||
*/
|
||||
int ipv4_send(uint32_t dst_ip, uint8_t protocol,
|
||||
const void *payload, uint32_t len);
|
||||
|
||||
/**
|
||||
* Send an IPv4 packet through a specific interface.
|
||||
*
|
||||
* @param iface_idx Ethernet interface index.
|
||||
* @param dst_ip Destination IP (host byte order).
|
||||
* @param protocol IP protocol number.
|
||||
* @param payload Packet payload.
|
||||
* @param len Payload length.
|
||||
* @return 0 on success, -1 on failure.
|
||||
*/
|
||||
int ipv4_send_iface(uint32_t iface_idx, uint32_t dst_ip, uint8_t protocol,
|
||||
const void *payload, uint32_t len);
|
||||
|
||||
/**
|
||||
* Process an incoming IPv4 packet (called from Ethernet layer).
|
||||
*
|
||||
* @param data Raw IPv4 packet (header + payload).
|
||||
* @param len Total packet length.
|
||||
* @param iface_idx Interface index the packet arrived on.
|
||||
*/
|
||||
void ipv4_receive(const void *data, uint32_t len, uint32_t iface_idx);
|
||||
|
||||
/**
|
||||
* Register a protocol handler.
|
||||
*
|
||||
* @param protocol IP protocol number.
|
||||
* @param handler Callback function.
|
||||
* @return 0 on success, -1 if table is full.
|
||||
*/
|
||||
int ipv4_register_proto(uint8_t protocol, ipv4_proto_handler_t handler);
|
||||
|
||||
/**
|
||||
* Compute the Internet checksum over a buffer.
|
||||
*
|
||||
* @param data Buffer.
|
||||
* @param len Length in bytes.
|
||||
* @return Checksum in network byte order.
|
||||
*/
|
||||
uint16_t ipv4_checksum(const void *data, uint32_t len);
|
||||
|
||||
/**
|
||||
* Convert an IPv4 address string "A.B.C.D" to a uint32_t (host byte order).
|
||||
*
|
||||
* @param str Dotted-decimal string.
|
||||
* @return IPv4 address, or 0 on failure.
|
||||
*/
|
||||
uint32_t ipv4_aton(const char *str);
|
||||
|
||||
/**
|
||||
* Format an IPv4 address (host byte order) into a dotted-decimal string.
|
||||
*
|
||||
* @param ip IPv4 address.
|
||||
* @param buf Output buffer (at least 16 bytes).
|
||||
* @param size Buffer size.
|
||||
* @return Pointer to buf.
|
||||
*/
|
||||
char *ipv4_ntoa(uint32_t ip, char *buf, uint32_t size);
|
||||
|
||||
#endif /* IPV4_H */
|
||||
100
src/isr.c
Normal file
100
src/isr.c
Normal file
@@ -0,0 +1,100 @@
|
||||
#include "isr.h"
|
||||
#include "pic.h"
|
||||
#include "process.h"
|
||||
#include "syscall.h"
|
||||
#include "keyboard.h"
|
||||
#include "floppy.h"
|
||||
#include "ne2000.h"
|
||||
#include "e3c509.h"
|
||||
#include <stdint.h>
|
||||
|
||||
/* Forward declaration for kernel panic or similar */
|
||||
void offset_print(const char *str);
|
||||
void print_hex(uint32_t val);
|
||||
|
||||
/* Exception messages */
|
||||
char *exception_messages[] = {
|
||||
"Division By Zero",
|
||||
"Debug",
|
||||
"Non Maskable Interrupt",
|
||||
"Breakpoint",
|
||||
"Into Detected Overflow",
|
||||
"Out of Bounds",
|
||||
"Invalid Opcode",
|
||||
"No Coprocessor",
|
||||
"Double Fault",
|
||||
"Coprocessor Segment Overrun",
|
||||
"Bad TSS",
|
||||
"Segment Not Present",
|
||||
"Stack Fault",
|
||||
"General Protection Fault",
|
||||
"Page Fault",
|
||||
"Unknown Interrupt",
|
||||
"Coprocessor Fault",
|
||||
"Alignment Check",
|
||||
"Machine Check",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved",
|
||||
"Reserved"
|
||||
};
|
||||
|
||||
void isr_handler(registers_t *regs)
|
||||
{
|
||||
/* System call (INT 0x80) */
|
||||
if (regs->int_no == 0x80) {
|
||||
syscall_handler(regs);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Hardware interrupts (IRQs 0-15, mapped to vectors 32-47) */
|
||||
if (regs->int_no >= 32 && regs->int_no < 48) {
|
||||
/* Send EOI to PIC (IRQ number 0-15) */
|
||||
pic_send_eoi(regs->int_no - 32);
|
||||
|
||||
if (regs->int_no == 32) {
|
||||
/* Timer tick - invoke scheduler */
|
||||
schedule_tick(regs);
|
||||
} else if (regs->int_no == 33) {
|
||||
/* Keyboard IRQ */
|
||||
keyboard_irq(regs);
|
||||
} else if (regs->int_no == 38) {
|
||||
/* Floppy IRQ */
|
||||
floppy_irq();
|
||||
} else if (regs->int_no == 41) {
|
||||
/* NE2000 Ethernet IRQ (IRQ 9, vector 41) */
|
||||
ne2k_irq();
|
||||
} else if (regs->int_no == 42) {
|
||||
/* 3C509B Ethernet IRQ (IRQ 10, vector 42) */
|
||||
e3c509_irq();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/* CPU exceptions (vectors 0-31) */
|
||||
offset_print("received interrupt: ");
|
||||
print_hex(regs->int_no);
|
||||
offset_print("\n");
|
||||
|
||||
if (regs->int_no < 32)
|
||||
{
|
||||
offset_print(exception_messages[regs->int_no]);
|
||||
offset_print(" Exception. System Halted!\n");
|
||||
offset_print(" EIP: ");
|
||||
print_hex(regs->eip);
|
||||
offset_print(" CS: ");
|
||||
print_hex(regs->cs);
|
||||
offset_print(" ERR: ");
|
||||
print_hex(regs->err_code);
|
||||
for (;;) ;
|
||||
}
|
||||
}
|
||||
18
src/isr.h
Normal file
18
src/isr.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef ISR_H
|
||||
#define ISR_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct registers
|
||||
{
|
||||
uint32_t ds; // Data segment selector
|
||||
uint32_t edi, esi, ebp, esp, ebx, edx, ecx, eax; // Pushed by pusha
|
||||
uint32_t int_no, err_code; // Interrupt number and error code (if applicable)
|
||||
uint32_t eip, cs, eflags, useresp, ss; // Pushed by the processor automatically
|
||||
} registers_t;
|
||||
|
||||
typedef void (*isr_t)(registers_t*);
|
||||
|
||||
void isr_handler(registers_t* regs);
|
||||
|
||||
#endif
|
||||
505
src/kernel.c
Normal file
505
src/kernel.c
Normal file
@@ -0,0 +1,505 @@
|
||||
#include <multiboot2.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include "gdt.h"
|
||||
#include "idt.h"
|
||||
#include "pic.h"
|
||||
#include "port_io.h"
|
||||
#include "pmm.h"
|
||||
#include "paging.h"
|
||||
#include "kmalloc.h"
|
||||
#include "driver.h"
|
||||
#include "vga.h"
|
||||
#include "tss.h"
|
||||
#include "syscall.h"
|
||||
#include "process.h"
|
||||
#include "cpio.h"
|
||||
#include "vfs.h"
|
||||
#include "initrd_fs.h"
|
||||
#include "devicefs.h"
|
||||
#include "sysfs.h"
|
||||
#include "mbr.h"
|
||||
#include "fat32.h"
|
||||
#include "keyboard.h"
|
||||
#include "ethernet.h"
|
||||
#include "ipv4.h"
|
||||
#include "arp.h"
|
||||
#include "dhcp.h"
|
||||
#include "udp.h"
|
||||
#include "tcp.h"
|
||||
#include "framebuffer.h"
|
||||
|
||||
/* Global framebuffer info, parsed from multiboot2 tags. */
|
||||
framebuffer_info_t fb_info;
|
||||
|
||||
/**
|
||||
* Initialize COM1 serial port for debug output.
|
||||
* Baud rate: 115200, 8N1.
|
||||
*/
|
||||
static void serial_init(void) {
|
||||
outb(0x3F8 + 1, 0x00); /* Disable interrupts */
|
||||
outb(0x3F8 + 3, 0x80); /* Enable DLAB */
|
||||
outb(0x3F8 + 0, 0x01); /* Divisor low: 115200 baud */
|
||||
outb(0x3F8 + 1, 0x00); /* Divisor high */
|
||||
outb(0x3F8 + 3, 0x03); /* 8 bits, no parity, 1 stop */
|
||||
outb(0x3F8 + 2, 0xC7); /* Enable FIFO */
|
||||
outb(0x3F8 + 4, 0x03); /* RTS/DSR set */
|
||||
}
|
||||
|
||||
static void serial_putc(char c) {
|
||||
/* Wait for transmit buffer empty */
|
||||
while (!(inb(0x3F8 + 5) & 0x20));
|
||||
outb(0x3F8, c);
|
||||
}
|
||||
|
||||
void offset_print(const char *str)
|
||||
{
|
||||
while (*str) {
|
||||
outb(0xE9, *str); /* debugcon */
|
||||
serial_putc(*str); /* COM1 serial */
|
||||
str++;
|
||||
}
|
||||
}
|
||||
|
||||
void print_hex(uint32_t val)
|
||||
{
|
||||
const char *hex = "0123456789ABCDEF";
|
||||
outb(0xE9, '0'); serial_putc('0');
|
||||
outb(0xE9, 'x'); serial_putc('x');
|
||||
for (int i = 28; i >= 0; i -= 4) {
|
||||
char c = hex[(val >> i) & 0xF];
|
||||
outb(0xE9, c);
|
||||
serial_putc(c);
|
||||
}
|
||||
outb(0xE9, '\n'); serial_putc('\n');
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* VFS sysfs namespace — handles mount/umount via /sys/vfs
|
||||
* ================================================================
|
||||
*
|
||||
* /sys/vfs/mount — write "device mountpoint" to mount a FAT32 device
|
||||
* /sys/vfs/mounts — read to list active mounts
|
||||
*/
|
||||
|
||||
/** Last mount status message for reading back. */
|
||||
static char vfs_sysfs_status[256] = "no mounts\n";
|
||||
|
||||
static int vfs_sysfs_list(void *ctx, const char *path, uint32_t idx,
|
||||
sysfs_entry_t *out) {
|
||||
(void)ctx;
|
||||
if (path[0] != '\0') return -1; /* No subdirectories */
|
||||
|
||||
static const char *entries[] = { "mount", "mounts" };
|
||||
if (idx >= 2) return -1;
|
||||
|
||||
memset(out, 0, sizeof(sysfs_entry_t));
|
||||
strncpy(out->name, entries[idx], SYSFS_MAX_NAME - 1);
|
||||
out->is_dir = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int vfs_sysfs_read(void *ctx, const char *path, char *buf,
|
||||
uint32_t buf_size) {
|
||||
(void)ctx;
|
||||
|
||||
if (strcmp(path, "mount") == 0 || strcmp(path, "mounts") == 0) {
|
||||
uint32_t len = strlen(vfs_sysfs_status);
|
||||
if (len + 1 > buf_size) len = buf_size - 1;
|
||||
memcpy(buf, vfs_sysfs_status, len);
|
||||
buf[len] = '\0';
|
||||
return (int)len;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
static int vfs_sysfs_write(void *ctx, const char *path, const char *buf,
|
||||
uint32_t size) {
|
||||
(void)ctx;
|
||||
|
||||
if (strcmp(path, "mount") != 0) return -1;
|
||||
|
||||
/* Parse: "device_name mount_path\n" */
|
||||
char device[64];
|
||||
char mount_path[128];
|
||||
int di = 0, mi = 0;
|
||||
uint32_t i = 0;
|
||||
|
||||
/* Parse device name */
|
||||
while (i < size && buf[i] != ' ' && buf[i] != '\n' && di < 63) {
|
||||
device[di++] = buf[i++];
|
||||
}
|
||||
device[di] = '\0';
|
||||
|
||||
/* Skip spaces */
|
||||
while (i < size && buf[i] == ' ') i++;
|
||||
|
||||
/* Parse mount path */
|
||||
while (i < size && buf[i] != ' ' && buf[i] != '\n' && mi < 127) {
|
||||
mount_path[mi++] = buf[i++];
|
||||
}
|
||||
mount_path[mi] = '\0';
|
||||
|
||||
if (di == 0 || mi == 0) {
|
||||
strncpy(vfs_sysfs_status, "error: usage: device mountpoint\n",
|
||||
sizeof(vfs_sysfs_status) - 1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
offset_print(" VFS mount request: ");
|
||||
offset_print(device);
|
||||
offset_print(" -> ");
|
||||
offset_print(mount_path);
|
||||
offset_print("\n");
|
||||
|
||||
/* Find the device */
|
||||
devicefs_device_t *dev = devicefs_find(device);
|
||||
if (!dev) {
|
||||
strncpy(vfs_sysfs_status, "error: device not found\n",
|
||||
sizeof(vfs_sysfs_status) - 1);
|
||||
offset_print(" VFS mount: device not found\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Mount as FAT32 */
|
||||
int ret = fat32_mount_device(dev, mount_path);
|
||||
if (ret != 0) {
|
||||
strncpy(vfs_sysfs_status, "error: FAT32 mount failed\n",
|
||||
sizeof(vfs_sysfs_status) - 1);
|
||||
offset_print(" VFS mount: FAT32 mount failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Build success message */
|
||||
strncpy(vfs_sysfs_status, "mounted ", sizeof(vfs_sysfs_status) - 1);
|
||||
uint32_t slen = strlen(vfs_sysfs_status);
|
||||
strncpy(vfs_sysfs_status + slen, device,
|
||||
sizeof(vfs_sysfs_status) - 1 - slen);
|
||||
slen = strlen(vfs_sysfs_status);
|
||||
strncpy(vfs_sysfs_status + slen, " at ",
|
||||
sizeof(vfs_sysfs_status) - 1 - slen);
|
||||
slen = strlen(vfs_sysfs_status);
|
||||
strncpy(vfs_sysfs_status + slen, mount_path,
|
||||
sizeof(vfs_sysfs_status) - 1 - slen);
|
||||
slen = strlen(vfs_sysfs_status);
|
||||
if (slen < sizeof(vfs_sysfs_status) - 1) {
|
||||
vfs_sysfs_status[slen] = '\n';
|
||||
vfs_sysfs_status[slen + 1] = '\0';
|
||||
}
|
||||
|
||||
return (int)size;
|
||||
}
|
||||
|
||||
static sysfs_ops_t vfs_sysfs_ops = {
|
||||
.list = vfs_sysfs_list,
|
||||
.read = vfs_sysfs_read,
|
||||
.write = vfs_sysfs_write,
|
||||
};
|
||||
|
||||
void kernel_main(uint32_t magic, uint32_t addr) {
|
||||
/* Initialize serial port first so all debug output goes to COM1 too */
|
||||
serial_init();
|
||||
|
||||
/* Early VGA: write directly to the text buffer at 0xB8000 for boot
|
||||
* progress that is visible even without the VGA driver initialized.
|
||||
* Attribute 0x1F = white on blue. */
|
||||
volatile uint16_t *early_vga = (volatile uint16_t *)0xB8000;
|
||||
int early_pos = 0;
|
||||
|
||||
/* Helper macro: write a short string to early VGA */
|
||||
#define EARLY_PRINT(s) do { \
|
||||
const char *_p = (s); \
|
||||
while (*_p) { \
|
||||
early_vga[early_pos++] = (uint16_t)(unsigned char)*_p | (0x1F << 8); \
|
||||
_p++; \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
/* Clear screen to blue */
|
||||
for (int i = 0; i < 80 * 25; i++)
|
||||
early_vga[i] = (uint16_t)' ' | (0x1F << 8);
|
||||
|
||||
EARLY_PRINT("ClaudeOS early boot");
|
||||
early_pos = 80; /* Move to line 2 */
|
||||
|
||||
if (magic != MULTIBOOT2_BOOTLOADER_MAGIC) {
|
||||
EARLY_PRINT("ERROR: Bad magic=0x");
|
||||
/* Print magic in hex */
|
||||
const char *hex = "0123456789ABCDEF";
|
||||
for (int i = 28; i >= 0; i -= 4)
|
||||
early_vga[early_pos++] = (uint16_t)(unsigned char)hex[(magic >> i) & 0xF] | (0x4F << 8);
|
||||
early_pos = 80 * 3;
|
||||
EARLY_PRINT("Expected 0x36D76289 (Multiboot2)");
|
||||
early_pos = 80 * 4;
|
||||
EARLY_PRINT("Got MB1 magic? Checking grub.cfg...");
|
||||
|
||||
offset_print("Invalid magic number: ");
|
||||
print_hex(magic);
|
||||
|
||||
/* Hang with interrupts disabled so user can see the message */
|
||||
for (;;) __asm__ volatile("hlt");
|
||||
}
|
||||
|
||||
EARLY_PRINT("Magic OK ");
|
||||
offset_print("Booting...\n");
|
||||
|
||||
init_gdt();
|
||||
EARLY_PRINT("GDT ");
|
||||
offset_print("GDT initialized\n");
|
||||
|
||||
init_idt();
|
||||
EARLY_PRINT("IDT ");
|
||||
offset_print("IDT initialized\n");
|
||||
|
||||
init_pic();
|
||||
/* Unmask timer IRQ (IRQ0) explicitly */
|
||||
pic_clear_mask(0);
|
||||
EARLY_PRINT("PIC ");
|
||||
offset_print("PIC initialized\n");
|
||||
|
||||
init_pmm(addr);
|
||||
EARLY_PRINT("PMM ");
|
||||
offset_print("PMM initialized\n");
|
||||
|
||||
/* Scan Multiboot2 tags for the initrd module and framebuffer info */
|
||||
uint32_t initrd_start = 0, initrd_end = 0;
|
||||
memset(&fb_info, 0, sizeof(fb_info));
|
||||
{
|
||||
struct multiboot_tag *tag;
|
||||
for (tag = (struct multiboot_tag *)(addr + 8);
|
||||
tag->type != MULTIBOOT_TAG_TYPE_END;
|
||||
tag = (struct multiboot_tag *)((uint8_t *)tag + ((tag->size + 7) & ~7u))) {
|
||||
if (tag->type == MULTIBOOT_TAG_TYPE_MODULE) {
|
||||
struct multiboot_tag_module *mod = (struct multiboot_tag_module *)tag;
|
||||
initrd_start = mod->mod_start;
|
||||
initrd_end = mod->mod_end;
|
||||
offset_print("Initrd module at ");
|
||||
print_hex(initrd_start);
|
||||
offset_print(" to ");
|
||||
print_hex(initrd_end);
|
||||
}
|
||||
if (tag->type == MULTIBOOT_TAG_TYPE_FRAMEBUFFER) {
|
||||
struct multiboot_tag_framebuffer *fbt =
|
||||
(struct multiboot_tag_framebuffer *)tag;
|
||||
fb_info.addr = (uint32_t)fbt->common.framebuffer_addr;
|
||||
fb_info.pitch = fbt->common.framebuffer_pitch;
|
||||
fb_info.width = fbt->common.framebuffer_width;
|
||||
fb_info.height = fbt->common.framebuffer_height;
|
||||
fb_info.bpp = fbt->common.framebuffer_bpp;
|
||||
fb_info.type = fbt->common.framebuffer_type;
|
||||
|
||||
if (fb_info.type == FB_TYPE_RGB) {
|
||||
fb_info.red_pos = fbt->framebuffer_red_field_position;
|
||||
fb_info.red_size = fbt->framebuffer_red_mask_size;
|
||||
fb_info.green_pos = fbt->framebuffer_green_field_position;
|
||||
fb_info.green_size = fbt->framebuffer_green_mask_size;
|
||||
fb_info.blue_pos = fbt->framebuffer_blue_field_position;
|
||||
fb_info.blue_size = fbt->framebuffer_blue_mask_size;
|
||||
}
|
||||
|
||||
offset_print("Framebuffer: type=");
|
||||
print_hex(fb_info.type);
|
||||
offset_print(" addr=");
|
||||
print_hex(fb_info.addr);
|
||||
offset_print(" ");
|
||||
print_hex(fb_info.width);
|
||||
offset_print(" x ");
|
||||
print_hex(fb_info.height);
|
||||
offset_print(" bpp=");
|
||||
print_hex(fb_info.bpp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
init_paging();
|
||||
EARLY_PRINT("PAGING ");
|
||||
offset_print("Paging initialized\n");
|
||||
|
||||
/* If GRUB provided a graphical framebuffer, identity-map it so
|
||||
* the VGA driver can write pixels to it after paging is enabled. */
|
||||
if (fb_info.addr != 0 && fb_info.type == FB_TYPE_RGB) {
|
||||
uint32_t fb_size = fb_info.pitch * fb_info.height;
|
||||
uint32_t fb_base = fb_info.addr & ~0xFFFu; /* page-align down */
|
||||
uint32_t fb_end = (fb_info.addr + fb_size + 0xFFF) & ~0xFFFu;
|
||||
offset_print(" Mapping framebuffer ");
|
||||
print_hex(fb_base);
|
||||
offset_print(" to ");
|
||||
print_hex(fb_end);
|
||||
for (uint32_t pa = fb_base; pa < fb_end; pa += 4096) {
|
||||
paging_map_page(pa, pa, PAGE_PRESENT | PAGE_WRITE | PAGE_WRITETHROUGH);
|
||||
}
|
||||
}
|
||||
|
||||
/* Test paging: allocate a page and write to it */
|
||||
void *test_page = paging_alloc_page();
|
||||
if (test_page) {
|
||||
offset_print("Allocated virtual page at: ");
|
||||
print_hex((uint32_t)test_page);
|
||||
*((volatile uint32_t *)test_page) = 0xDEADBEEF;
|
||||
offset_print("Virtual page write/read OK\n");
|
||||
paging_free_page(test_page);
|
||||
} else {
|
||||
offset_print("FAILED to allocate virtual page\n");
|
||||
}
|
||||
|
||||
init_kmalloc();
|
||||
EARLY_PRINT("HEAP ");
|
||||
offset_print("Memory allocator initialized\n");
|
||||
|
||||
/* Initialize CPIO ramdisk if module was loaded */
|
||||
if (initrd_start != 0) {
|
||||
cpio_init((const void *)initrd_start, initrd_end - initrd_start);
|
||||
EARLY_PRINT("CPIO ");
|
||||
offset_print("CPIO ramdisk initialized\n");
|
||||
} else {
|
||||
offset_print("No initrd module found\n");
|
||||
}
|
||||
|
||||
init_vfs();
|
||||
EARLY_PRINT("VFS ");
|
||||
offset_print("VFS initialized\n");
|
||||
|
||||
if (initrd_start != 0) {
|
||||
init_initrd_fs();
|
||||
EARLY_PRINT("INITRD ");
|
||||
offset_print("Initrd filesystem mounted\n");
|
||||
|
||||
/* Test VFS: read a file from the initrd */
|
||||
int fd = vfs_open("/initrd/README", 0);
|
||||
if (fd >= 0) {
|
||||
char buf[64];
|
||||
int32_t n = vfs_read(fd, buf, sizeof(buf) - 1);
|
||||
if (n > 0) {
|
||||
buf[n] = '\0';
|
||||
offset_print("VFS read /initrd/README: ");
|
||||
offset_print(buf);
|
||||
}
|
||||
vfs_close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
init_devicefs();
|
||||
EARLY_PRINT("DEV ");
|
||||
offset_print("Devicefs initialized\n");
|
||||
|
||||
init_sysfs();
|
||||
EARLY_PRINT("SYS ");
|
||||
offset_print("Sysfs initialized\n");
|
||||
|
||||
/* Register VFS mount namespace in sysfs */
|
||||
sysfs_register("vfs", &vfs_sysfs_ops, NULL);
|
||||
offset_print("VFS sysfs namespace registered\n");
|
||||
|
||||
init_tss();
|
||||
EARLY_PRINT("TSS ");
|
||||
offset_print("TSS initialized\n");
|
||||
|
||||
init_syscalls();
|
||||
EARLY_PRINT("SYSCALL ");
|
||||
offset_print("Syscalls initialized\n");
|
||||
|
||||
keyboard_init();
|
||||
EARLY_PRINT("KBD ");
|
||||
offset_print("Keyboard initialized\n");
|
||||
|
||||
init_process();
|
||||
EARLY_PRINT("PROC ");
|
||||
offset_print("Process subsystem initialized\n");
|
||||
|
||||
/* If the early VGA canary at 0xB8000 was visible, the display is
|
||||
* definitely in text mode, regardless of what the GRUB framebuffer
|
||||
* tag says. Force text mode so vga_init doesn't try to use a
|
||||
* pixel framebuffer that isn't actually being displayed. */
|
||||
if (fb_info.type == FB_TYPE_RGB) {
|
||||
offset_print(" Overriding fb type from RGB to EGA_TEXT (early VGA visible)\n");
|
||||
fb_info.type = FB_TYPE_EGA_TEXT;
|
||||
fb_info.addr = 0x000B8000;
|
||||
fb_info.width = 80;
|
||||
fb_info.height = 25;
|
||||
fb_info.bpp = 16;
|
||||
fb_info.pitch = 80 * 2;
|
||||
}
|
||||
|
||||
ethernet_init();
|
||||
offset_print("Ethernet subsystem initialized\n");
|
||||
|
||||
ipv4_init();
|
||||
offset_print("IPv4 stack initialized\n");
|
||||
|
||||
arp_init();
|
||||
offset_print("ARP subsystem initialized\n");
|
||||
|
||||
dhcp_init();
|
||||
offset_print("DHCP subsystem initialized\n");
|
||||
|
||||
udp_init();
|
||||
offset_print("UDP stack initialized\n");
|
||||
|
||||
tcp_init();
|
||||
offset_print("TCP stack initialized\n");
|
||||
|
||||
init_drivers();
|
||||
EARLY_PRINT("DRV ");
|
||||
offset_print("Drivers initialized\n");
|
||||
|
||||
/* Scan for MBR partitions on detected hard drives */
|
||||
init_mbr();
|
||||
offset_print("MBR scan complete\n");
|
||||
|
||||
/* At this point the VGA driver has been initialized and taken over
|
||||
* the display. The early VGA text is no longer visible. */
|
||||
|
||||
/* Show memory statistics and boot progress on VGA */
|
||||
vga_show_mem_stats();
|
||||
vga_puts("Boot complete.\n\n");
|
||||
|
||||
/* Test kmalloc/kfree */
|
||||
uint32_t *test_alloc = (uint32_t *)kmalloc(64);
|
||||
if (test_alloc) {
|
||||
*test_alloc = 42;
|
||||
offset_print("kmalloc(64) = ");
|
||||
print_hex((uint32_t)test_alloc);
|
||||
kfree(test_alloc);
|
||||
offset_print("kfree OK\n");
|
||||
} else {
|
||||
offset_print("FAILED to kmalloc\n");
|
||||
}
|
||||
|
||||
/* Load the initial program from the initrd and run it */
|
||||
cpio_entry_t app_entry;
|
||||
const char *init_app = "sh";
|
||||
if (cpio_find(init_app, &app_entry) == 0) {
|
||||
offset_print("Found ");
|
||||
offset_print(init_app);
|
||||
offset_print(" in initrd (");
|
||||
print_hex(app_entry.datasize);
|
||||
offset_print(" bytes)\n");
|
||||
|
||||
int32_t pid = process_create(init_app,
|
||||
app_entry.data,
|
||||
app_entry.datasize);
|
||||
if (pid > 0) {
|
||||
offset_print("Created init process, pid=");
|
||||
print_hex((uint32_t)pid);
|
||||
|
||||
/* Enable interrupts before entering user mode */
|
||||
asm volatile("sti");
|
||||
offset_print("Interrupts enabled\n");
|
||||
|
||||
/* Enter user mode - does not return */
|
||||
process_run_first();
|
||||
} else {
|
||||
offset_print("FAILED to create init process\n");
|
||||
}
|
||||
} else {
|
||||
offset_print(init_app);
|
||||
offset_print(" not found in initrd\n");
|
||||
}
|
||||
|
||||
/* Enable interrupts */
|
||||
asm volatile("sti");
|
||||
offset_print("Interrupts enabled\n");
|
||||
|
||||
offset_print("Hello, world\n");
|
||||
}
|
||||
198
src/keyboard.c
Normal file
198
src/keyboard.c
Normal file
@@ -0,0 +1,198 @@
|
||||
/**
|
||||
* @file keyboard.c
|
||||
* @brief PS/2 keyboard driver implementation.
|
||||
*
|
||||
* Reads scancodes from I/O port 0x60 on IRQ1, translates scancode set 1
|
||||
* to ASCII using a simple lookup table, stores characters in a ring buffer,
|
||||
* and wakes any process blocked waiting for keyboard input.
|
||||
*/
|
||||
|
||||
#include "keyboard.h"
|
||||
#include "port_io.h"
|
||||
#include "pic.h"
|
||||
#include "process.h"
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Debug print helpers defined in kernel.c */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/** PS/2 keyboard data port. */
|
||||
#define KB_DATA_PORT 0x60
|
||||
|
||||
/** Ring buffer for keyboard input. */
|
||||
static char kb_buffer[KB_BUFFER_SIZE];
|
||||
static volatile uint32_t kb_head = 0;
|
||||
static volatile uint32_t kb_tail = 0;
|
||||
|
||||
/** Process waiting for keyboard input (PID, or 0 if none). */
|
||||
static volatile uint32_t kb_waiting_pid = 0;
|
||||
|
||||
/** Shift key state. */
|
||||
static int shift_pressed = 0;
|
||||
|
||||
/**
|
||||
* Scancode set 1 to ASCII lookup table (unshifted).
|
||||
* Index = scancode, value = ASCII character (0 = unmapped).
|
||||
*/
|
||||
static const char scancode_ascii[128] = {
|
||||
0, 27, '1', '2', '3', '4', '5', '6', /* 0x00 - 0x07 */
|
||||
'7', '8', '9', '0', '-', '=', '\b', '\t', /* 0x08 - 0x0F */
|
||||
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', /* 0x10 - 0x17 */
|
||||
'o', 'p', '[', ']', '\n', 0, 'a', 's', /* 0x18 - 0x1F */
|
||||
'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', /* 0x20 - 0x27 */
|
||||
'\'', '`', 0, '\\', 'z', 'x', 'c', 'v', /* 0x28 - 0x2F */
|
||||
'b', 'n', 'm', ',', '.', '/', 0, '*', /* 0x30 - 0x37 */
|
||||
0, ' ', 0, 0, 0, 0, 0, 0, /* 0x38 - 0x3F */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x47 */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, /* 0x48 - 0x4F */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x57 */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, /* 0x58 - 0x5F */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x67 */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, /* 0x68 - 0x6F */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x77 */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, /* 0x78 - 0x7F */
|
||||
};
|
||||
|
||||
/**
|
||||
* Scancode set 1 to ASCII lookup table (shifted).
|
||||
*/
|
||||
static const char scancode_ascii_shift[128] = {
|
||||
0, 27, '!', '@', '#', '$', '%', '^', /* 0x00 - 0x07 */
|
||||
'&', '*', '(', ')', '_', '+', '\b', '\t', /* 0x08 - 0x0F */
|
||||
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', /* 0x10 - 0x17 */
|
||||
'O', 'P', '{', '}', '\n', 0, 'A', 'S', /* 0x18 - 0x1F */
|
||||
'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', /* 0x20 - 0x27 */
|
||||
'"', '~', 0, '|', 'Z', 'X', 'C', 'V', /* 0x28 - 0x2F */
|
||||
'B', 'N', 'M', '<', '>', '?', 0, '*', /* 0x30 - 0x37 */
|
||||
0, ' ', 0, 0, 0, 0, 0, 0, /* 0x38 - 0x3F */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x47 */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, /* 0x48 - 0x4F */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x57 */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, /* 0x58 - 0x5F */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, /* 0x60 - 0x67 */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, /* 0x68 - 0x6F */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, /* 0x70 - 0x77 */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, /* 0x78 - 0x7F */
|
||||
};
|
||||
|
||||
/** Left/right shift scancodes. */
|
||||
#define SC_LSHIFT_PRESS 0x2A
|
||||
#define SC_LSHIFT_RELEASE 0xAA
|
||||
#define SC_RSHIFT_PRESS 0x36
|
||||
#define SC_RSHIFT_RELEASE 0xB6
|
||||
|
||||
/**
|
||||
* Put a character into the ring buffer.
|
||||
*/
|
||||
static void kb_buffer_put(char c) {
|
||||
uint32_t next = (kb_head + 1) % KB_BUFFER_SIZE;
|
||||
if (next == kb_tail) {
|
||||
return; /* Buffer full, drop character */
|
||||
}
|
||||
kb_buffer[kb_head] = c;
|
||||
kb_head = next;
|
||||
}
|
||||
|
||||
void keyboard_init(void) {
|
||||
kb_head = 0;
|
||||
kb_tail = 0;
|
||||
kb_waiting_pid = 0;
|
||||
shift_pressed = 0;
|
||||
|
||||
offset_print(" KEYBOARD: flushing controller...\n");
|
||||
|
||||
/* Flush any pending data from the keyboard controller.
|
||||
* Use a timeout to avoid hanging if the controller keeps reporting data
|
||||
* (some emulators/VMs behave differently). */
|
||||
int flush_count = 0;
|
||||
while ((inb(0x64) & 0x01) && flush_count < 1024) {
|
||||
inb(KB_DATA_PORT);
|
||||
flush_count++;
|
||||
}
|
||||
|
||||
offset_print(" KEYBOARD: flushed ");
|
||||
print_hex((uint32_t)flush_count);
|
||||
offset_print(" KEYBOARD: bytes, unmasking IRQ1...\n");
|
||||
|
||||
/* Unmask IRQ1 (keyboard) in the PIC */
|
||||
pic_clear_mask(1);
|
||||
|
||||
offset_print(" KEYBOARD: initialized\n");
|
||||
}
|
||||
|
||||
void keyboard_irq(registers_t *regs) {
|
||||
(void)regs;
|
||||
uint8_t scancode = inb(KB_DATA_PORT);
|
||||
|
||||
/* Handle shift keys */
|
||||
if (scancode == SC_LSHIFT_PRESS || scancode == SC_RSHIFT_PRESS) {
|
||||
shift_pressed = 1;
|
||||
return;
|
||||
}
|
||||
if (scancode == SC_LSHIFT_RELEASE || scancode == SC_RSHIFT_RELEASE) {
|
||||
shift_pressed = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
/* Ignore key releases (bit 7 set) */
|
||||
if (scancode & 0x80) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Translate scancode to ASCII */
|
||||
char c;
|
||||
if (shift_pressed) {
|
||||
c = scancode_ascii_shift[scancode];
|
||||
} else {
|
||||
c = scancode_ascii[scancode];
|
||||
}
|
||||
|
||||
if (c == 0) {
|
||||
return; /* Unmapped key */
|
||||
}
|
||||
|
||||
/* Put character in buffer */
|
||||
kb_buffer_put(c);
|
||||
|
||||
/* Wake any process waiting for keyboard input */
|
||||
if (kb_waiting_pid != 0) {
|
||||
process_t *waiter = process_get(kb_waiting_pid);
|
||||
if (waiter && waiter->state == PROCESS_BLOCKED) {
|
||||
waiter->state = PROCESS_READY;
|
||||
}
|
||||
kb_waiting_pid = 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t keyboard_read(char *buf, uint32_t count) {
|
||||
uint32_t n = 0;
|
||||
while (n < count && kb_tail != kb_head) {
|
||||
buf[n++] = kb_buffer[kb_tail];
|
||||
kb_tail = (kb_tail + 1) % KB_BUFFER_SIZE;
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
int keyboard_has_data(void) {
|
||||
return kb_head != kb_tail;
|
||||
}
|
||||
|
||||
void keyboard_block_for_input(registers_t *regs) {
|
||||
process_t *cur = process_current();
|
||||
if (!cur) return;
|
||||
|
||||
cur->state = PROCESS_BLOCKED;
|
||||
cur->saved_regs = *regs;
|
||||
|
||||
/* Rewind EIP by 2 bytes so that when the process is unblocked and
|
||||
* scheduled, the CPU re-executes the INT 0x80 instruction. At that
|
||||
* point keyboard_has_data() will return true and the read succeeds. */
|
||||
cur->saved_regs.eip -= 2;
|
||||
|
||||
kb_waiting_pid = cur->pid;
|
||||
|
||||
/* Schedule next process */
|
||||
schedule_tick(regs);
|
||||
}
|
||||
56
src/keyboard.h
Normal file
56
src/keyboard.h
Normal file
@@ -0,0 +1,56 @@
|
||||
/**
|
||||
* @file keyboard.h
|
||||
* @brief PS/2 keyboard driver.
|
||||
*
|
||||
* Handles IRQ1 keyboard interrupts, translates scancodes to ASCII,
|
||||
* and provides a ring buffer for user-space reading via SYS_READ.
|
||||
*/
|
||||
|
||||
#ifndef KEYBOARD_H
|
||||
#define KEYBOARD_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "isr.h"
|
||||
|
||||
/** Keyboard input buffer size. */
|
||||
#define KB_BUFFER_SIZE 256
|
||||
|
||||
/**
|
||||
* Initialize the keyboard driver.
|
||||
*/
|
||||
void keyboard_init(void);
|
||||
|
||||
/**
|
||||
* Handle a keyboard IRQ (called from isr_handler for IRQ1).
|
||||
*
|
||||
* @param regs Register state (may be used to wake blocked processes).
|
||||
*/
|
||||
void keyboard_irq(registers_t *regs);
|
||||
|
||||
/**
|
||||
* Read characters from the keyboard buffer.
|
||||
* Non-blocking: returns whatever is available, 0 if empty.
|
||||
*
|
||||
* @param buf Destination buffer.
|
||||
* @param count Maximum bytes to read.
|
||||
* @return Number of bytes read.
|
||||
*/
|
||||
uint32_t keyboard_read(char *buf, uint32_t count);
|
||||
|
||||
/**
|
||||
* Check if there is data available in the keyboard buffer.
|
||||
*
|
||||
* @return Non-zero if data is available.
|
||||
*/
|
||||
int keyboard_has_data(void);
|
||||
|
||||
/**
|
||||
* Block the given process until keyboard data is available.
|
||||
* Sets the process to BLOCKED state and records it as waiting for keyboard.
|
||||
* When data arrives, the process will be unblocked.
|
||||
*
|
||||
* @param regs Current interrupt frame (for saving process state).
|
||||
*/
|
||||
void keyboard_block_for_input(registers_t *regs);
|
||||
|
||||
#endif /* KEYBOARD_H */
|
||||
253
src/kmalloc.c
Normal file
253
src/kmalloc.c
Normal file
@@ -0,0 +1,253 @@
|
||||
/**
|
||||
* @file kmalloc.c
|
||||
* @brief Kernel memory allocator implementation.
|
||||
*
|
||||
* A simple first-fit free-list allocator. Memory is obtained from the paging
|
||||
* subsystem in 4 KiB page increments. The allocator maintains a linked list
|
||||
* of free blocks. On allocation, the first block large enough is split if
|
||||
* needed. On free, adjacent blocks are coalesced to reduce fragmentation.
|
||||
*
|
||||
* The allocator metadata (block headers) are stored inline at the beginning
|
||||
* of each block, so the minimum allocation overhead is sizeof(block_header_t).
|
||||
*/
|
||||
|
||||
#include "kmalloc.h"
|
||||
#include "paging.h"
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Debug print helpers defined in kernel.c */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/**
|
||||
* Block header stored at the start of every allocated or free block.
|
||||
* The usable memory starts immediately after this header.
|
||||
*/
|
||||
typedef struct block_header {
|
||||
uint32_t size; /**< Size of the usable area (excludes header). */
|
||||
uint32_t magic; /**< Magic number for integrity checking. */
|
||||
struct block_header *next; /**< Next block in the free list (free blocks only). */
|
||||
uint8_t is_free; /**< 1 if block is free, 0 if allocated. */
|
||||
} block_header_t;
|
||||
|
||||
/** Magic value to detect heap corruption. */
|
||||
#define BLOCK_MAGIC 0xCAFEBABE
|
||||
|
||||
/** Minimum block size to avoid excessive fragmentation. */
|
||||
#define MIN_BLOCK_SIZE 16
|
||||
|
||||
/** Alignment for all allocations (8-byte aligned). */
|
||||
#define ALIGNMENT 8
|
||||
|
||||
/** Round up to alignment boundary. */
|
||||
#define ALIGN_UP(x, a) (((x) + (a) - 1) & ~((a) - 1))
|
||||
|
||||
/** Head of the free list. */
|
||||
static block_header_t *free_list = NULL;
|
||||
|
||||
/** Number of pages currently allocated for the heap. */
|
||||
static uint32_t heap_pages = 0;
|
||||
|
||||
/**
|
||||
* Request a new page from the paging subsystem and add it to the free list.
|
||||
*
|
||||
* @return Pointer to the new block header, or NULL on failure.
|
||||
*/
|
||||
static block_header_t *request_page(void) {
|
||||
void *page = paging_alloc_page();
|
||||
if (!page) {
|
||||
return NULL;
|
||||
}
|
||||
heap_pages++;
|
||||
|
||||
block_header_t *block = (block_header_t *)page;
|
||||
block->size = 4096 - sizeof(block_header_t);
|
||||
block->magic = BLOCK_MAGIC;
|
||||
block->next = NULL;
|
||||
block->is_free = 1;
|
||||
|
||||
return block;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a block into the free list, maintaining address order.
|
||||
* Then attempt to coalesce with adjacent blocks.
|
||||
*
|
||||
* @param block The block to insert.
|
||||
*/
|
||||
static void insert_free_block(block_header_t *block) {
|
||||
block->is_free = 1;
|
||||
|
||||
/* Find insertion point (maintain address order for coalescing) */
|
||||
block_header_t *prev = NULL;
|
||||
block_header_t *curr = free_list;
|
||||
|
||||
while (curr && curr < block) {
|
||||
prev = curr;
|
||||
curr = curr->next;
|
||||
}
|
||||
|
||||
/* Insert between prev and curr */
|
||||
block->next = curr;
|
||||
if (prev) {
|
||||
prev->next = block;
|
||||
} else {
|
||||
free_list = block;
|
||||
}
|
||||
|
||||
/* Coalesce with next block if adjacent */
|
||||
if (block->next) {
|
||||
uint8_t *block_end = (uint8_t *)block + sizeof(block_header_t) + block->size;
|
||||
if (block_end == (uint8_t *)block->next) {
|
||||
block->size += sizeof(block_header_t) + block->next->size;
|
||||
block->next = block->next->next;
|
||||
}
|
||||
}
|
||||
|
||||
/* Coalesce with previous block if adjacent */
|
||||
if (prev) {
|
||||
uint8_t *prev_end = (uint8_t *)prev + sizeof(block_header_t) + prev->size;
|
||||
if (prev_end == (uint8_t *)block) {
|
||||
prev->size += sizeof(block_header_t) + block->size;
|
||||
prev->next = block->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Split a block if it is large enough to hold the requested size
|
||||
* plus a new block header and minimum block.
|
||||
*
|
||||
* @param block The block to potentially split.
|
||||
* @param size The requested allocation size (aligned).
|
||||
*/
|
||||
static void split_block(block_header_t *block, uint32_t size) {
|
||||
uint32_t remaining = block->size - size - sizeof(block_header_t);
|
||||
|
||||
if (remaining >= MIN_BLOCK_SIZE) {
|
||||
block_header_t *new_block = (block_header_t *)((uint8_t *)block + sizeof(block_header_t) + size);
|
||||
new_block->size = remaining;
|
||||
new_block->magic = BLOCK_MAGIC;
|
||||
new_block->is_free = 1;
|
||||
new_block->next = block->next;
|
||||
|
||||
block->size = size;
|
||||
block->next = new_block;
|
||||
}
|
||||
}
|
||||
|
||||
void init_kmalloc(void) {
|
||||
/* Allocate the initial heap page */
|
||||
block_header_t *initial = request_page();
|
||||
if (!initial) {
|
||||
offset_print(" KMALLOC: FATAL - could not allocate initial heap page\n");
|
||||
return;
|
||||
}
|
||||
|
||||
free_list = initial;
|
||||
offset_print(" KMALLOC: initialized with 1 page\n");
|
||||
}
|
||||
|
||||
void *kmalloc(size_t size) {
|
||||
if (size == 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Align the requested size */
|
||||
size = ALIGN_UP(size, ALIGNMENT);
|
||||
|
||||
/* First-fit search through free list */
|
||||
block_header_t *prev = NULL;
|
||||
block_header_t *curr = free_list;
|
||||
|
||||
while (curr) {
|
||||
if (curr->magic != BLOCK_MAGIC) {
|
||||
offset_print(" KMALLOC: HEAP CORRUPTION detected!\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (curr->is_free && curr->size >= size) {
|
||||
/* Found a suitable block */
|
||||
split_block(curr, size);
|
||||
|
||||
/* Remove from free list */
|
||||
if (prev) {
|
||||
prev->next = curr->next;
|
||||
} else {
|
||||
free_list = curr->next;
|
||||
}
|
||||
|
||||
curr->is_free = 0;
|
||||
curr->next = NULL;
|
||||
|
||||
/* Return pointer past the header */
|
||||
return (void *)((uint8_t *)curr + sizeof(block_header_t));
|
||||
}
|
||||
|
||||
prev = curr;
|
||||
curr = curr->next;
|
||||
}
|
||||
|
||||
/* No suitable block found; request a new page */
|
||||
block_header_t *new_block = request_page();
|
||||
if (!new_block) {
|
||||
offset_print(" KMALLOC: out of memory\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* If the new page is large enough, use it directly */
|
||||
if (new_block->size >= size) {
|
||||
split_block(new_block, size);
|
||||
|
||||
/* If there's a remainder, add it to the free list */
|
||||
if (new_block->next && new_block->next->is_free) {
|
||||
insert_free_block(new_block->next);
|
||||
}
|
||||
|
||||
new_block->is_free = 0;
|
||||
new_block->next = NULL;
|
||||
return (void *)((uint8_t *)new_block + sizeof(block_header_t));
|
||||
}
|
||||
|
||||
/* Page too small (shouldn't happen for reasonable sizes) */
|
||||
offset_print(" KMALLOC: requested size too large for single page\n");
|
||||
insert_free_block(new_block);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void kfree(void *ptr) {
|
||||
if (!ptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Get the block header */
|
||||
block_header_t *block = (block_header_t *)((uint8_t *)ptr - sizeof(block_header_t));
|
||||
|
||||
if (block->magic != BLOCK_MAGIC) {
|
||||
offset_print(" KMALLOC: kfree() invalid pointer or corruption!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
if (block->is_free) {
|
||||
offset_print(" KMALLOC: kfree() double free detected!\n");
|
||||
return;
|
||||
}
|
||||
|
||||
insert_free_block(block);
|
||||
}
|
||||
|
||||
void *kcalloc(size_t count, size_t size) {
|
||||
size_t total = count * size;
|
||||
|
||||
/* Overflow check */
|
||||
if (count != 0 && total / count != size) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void *ptr = kmalloc(total);
|
||||
if (ptr) {
|
||||
memset(ptr, 0, total);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
46
src/kmalloc.h
Normal file
46
src/kmalloc.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* @file kmalloc.h
|
||||
* @brief Kernel memory allocator.
|
||||
*
|
||||
* Provides malloc/free for the kernel. Internally uses the paging subsystem
|
||||
* to ensure returned addresses are backed by physical RAM. Uses a simple
|
||||
* first-fit free-list allocator with block splitting and coalescing.
|
||||
*/
|
||||
|
||||
#ifndef KMALLOC_H
|
||||
#define KMALLOC_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
/**
|
||||
* Initialize the kernel memory allocator.
|
||||
*
|
||||
* Must be called after init_paging(). Allocates the initial heap pages.
|
||||
*/
|
||||
void init_kmalloc(void);
|
||||
|
||||
/**
|
||||
* Allocate a block of kernel memory.
|
||||
*
|
||||
* @param size Number of bytes to allocate.
|
||||
* @return Pointer to the allocated memory, or NULL on failure.
|
||||
*/
|
||||
void *kmalloc(size_t size);
|
||||
|
||||
/**
|
||||
* Free a previously allocated block of kernel memory.
|
||||
*
|
||||
* @param ptr Pointer returned by kmalloc. NULL is safely ignored.
|
||||
*/
|
||||
void kfree(void *ptr);
|
||||
|
||||
/**
|
||||
* Allocate a block of zero-initialized kernel memory.
|
||||
*
|
||||
* @param count Number of elements.
|
||||
* @param size Size of each element in bytes.
|
||||
* @return Pointer to the allocated and zeroed memory, or NULL on failure.
|
||||
*/
|
||||
void *kcalloc(size_t count, size_t size);
|
||||
|
||||
#endif /* KMALLOC_H */
|
||||
40
src/linker.ld
Normal file
40
src/linker.ld
Normal file
@@ -0,0 +1,40 @@
|
||||
ENTRY(_start)
|
||||
|
||||
SECTIONS
|
||||
{
|
||||
. = 1M;
|
||||
|
||||
_kernel_start = .;
|
||||
|
||||
.text BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
KEEP(*(.multiboot))
|
||||
*(.text)
|
||||
}
|
||||
|
||||
.rodata BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(.rodata)
|
||||
*(.rodata.*)
|
||||
}
|
||||
|
||||
.drivers BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
__drivers_start = .;
|
||||
KEEP(*(.drivers))
|
||||
__drivers_end = .;
|
||||
}
|
||||
|
||||
.data BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(.data)
|
||||
}
|
||||
|
||||
.bss BLOCK(4K) : ALIGN(4K)
|
||||
{
|
||||
*(COMMON)
|
||||
*(.bss)
|
||||
}
|
||||
|
||||
_kernel_end = .;
|
||||
}
|
||||
270
src/mbr.c
Normal file
270
src/mbr.c
Normal file
@@ -0,0 +1,270 @@
|
||||
/**
|
||||
* @file mbr.c
|
||||
* @brief MBR partition table driver implementation.
|
||||
*
|
||||
* Reads sector 0 from hard drives registered with the devicefs, parses
|
||||
* the MBR partition table, and registers each partition as a sub-device.
|
||||
* Partition devices are named using the parent device name + "mbr" class
|
||||
* prefix, with the number assigned by the devicefs (e.g., hdd1mbr1).
|
||||
*
|
||||
* Each partition sub-device translates LBA offsets relative to its own
|
||||
* start into absolute LBA addresses on the parent device.
|
||||
*/
|
||||
|
||||
#include "mbr.h"
|
||||
#include "devicefs.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Debug print helpers defined in kernel.c */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/** Maximum number of MBR partition sub-devices. */
|
||||
#define MBR_MAX_SUBS 16
|
||||
|
||||
/**
|
||||
* Per-partition context for sub-device read/write operations.
|
||||
*/
|
||||
typedef struct mbr_sub_device {
|
||||
devicefs_device_t *parent; /**< Parent block device. */
|
||||
uint32_t lba_start; /**< Partition start LBA on parent. */
|
||||
uint32_t sector_count; /**< Partition size in sectors. */
|
||||
uint8_t type; /**< MBR partition type code. */
|
||||
int active; /**< 1 if in use. */
|
||||
} mbr_sub_device_t;
|
||||
|
||||
/** Pool of sub-device contexts. */
|
||||
static mbr_sub_device_t sub_devices[MBR_MAX_SUBS];
|
||||
|
||||
/**
|
||||
* Allocate a sub-device context.
|
||||
* @return Pointer to a free sub-device, or NULL if exhausted.
|
||||
*/
|
||||
static mbr_sub_device_t *alloc_sub(void) {
|
||||
for (int i = 0; i < MBR_MAX_SUBS; i++) {
|
||||
if (!sub_devices[i].active) {
|
||||
sub_devices[i].active = 1;
|
||||
return &sub_devices[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Block device ops for MBR partition sub-devices
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Read sectors from a partition (translates LBA to parent device).
|
||||
*/
|
||||
static int mbr_sub_read(void *dev_data, uint32_t lba,
|
||||
uint32_t count, void *buf) {
|
||||
mbr_sub_device_t *sub = (mbr_sub_device_t *)dev_data;
|
||||
if (!sub || !sub->parent) return -1;
|
||||
|
||||
/* Bounds check */
|
||||
if (lba + count > sub->sector_count) return -1;
|
||||
|
||||
/* Translate to absolute LBA on parent device */
|
||||
uint32_t abs_lba = sub->lba_start + lba;
|
||||
|
||||
/* Read from parent device */
|
||||
if (!sub->parent->block_ops || !sub->parent->block_ops->read_sectors) {
|
||||
return -1;
|
||||
}
|
||||
return sub->parent->block_ops->read_sectors(sub->parent->dev_data,
|
||||
abs_lba, count, buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write sectors to a partition (translates LBA to parent device).
|
||||
*/
|
||||
static int mbr_sub_write(void *dev_data, uint32_t lba,
|
||||
uint32_t count, const void *buf) {
|
||||
mbr_sub_device_t *sub = (mbr_sub_device_t *)dev_data;
|
||||
if (!sub || !sub->parent) return -1;
|
||||
|
||||
if (lba + count > sub->sector_count) return -1;
|
||||
|
||||
uint32_t abs_lba = sub->lba_start + lba;
|
||||
|
||||
if (!sub->parent->block_ops || !sub->parent->block_ops->write_sectors) {
|
||||
return -1;
|
||||
}
|
||||
return sub->parent->block_ops->write_sectors(sub->parent->dev_data,
|
||||
abs_lba, count, buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get sector size (same as parent device).
|
||||
*/
|
||||
static uint32_t mbr_sub_sector_size(void *dev_data) {
|
||||
mbr_sub_device_t *sub = (mbr_sub_device_t *)dev_data;
|
||||
if (!sub || !sub->parent || !sub->parent->block_ops) return 512;
|
||||
if (sub->parent->block_ops->sector_size) {
|
||||
return sub->parent->block_ops->sector_size(sub->parent->dev_data);
|
||||
}
|
||||
return 512;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get total sector count of the partition.
|
||||
*/
|
||||
static uint32_t mbr_sub_sector_count(void *dev_data) {
|
||||
mbr_sub_device_t *sub = (mbr_sub_device_t *)dev_data;
|
||||
return sub ? sub->sector_count : 0;
|
||||
}
|
||||
|
||||
/** Block operations for MBR partition sub-devices. */
|
||||
static devicefs_block_ops_t mbr_sub_ops = {
|
||||
.read_sectors = mbr_sub_read,
|
||||
.write_sectors = mbr_sub_write,
|
||||
.sector_size = mbr_sub_sector_size,
|
||||
.sector_count = mbr_sub_sector_count,
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* MBR scanning
|
||||
* ================================================================ */
|
||||
|
||||
int mbr_scan(const char *parent_name) {
|
||||
devicefs_device_t *parent = devicefs_find(parent_name);
|
||||
if (!parent) {
|
||||
offset_print(" MBR: device not found: ");
|
||||
offset_print(parent_name);
|
||||
offset_print("\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (parent->type != DEVICEFS_BLOCK || !parent->block_ops) {
|
||||
offset_print(" MBR: not a block device: ");
|
||||
offset_print(parent_name);
|
||||
offset_print("\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!parent->block_ops->read_sectors) {
|
||||
offset_print(" MBR: device has no read_sectors op\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Read sector 0 (MBR) */
|
||||
uint8_t sector[512];
|
||||
if (parent->block_ops->read_sectors(parent->dev_data, 0, 1, sector) != 0) {
|
||||
offset_print(" MBR: failed to read sector 0 of ");
|
||||
offset_print(parent_name);
|
||||
offset_print("\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Check MBR signature */
|
||||
uint16_t sig = (uint16_t)sector[510] | ((uint16_t)sector[511] << 8);
|
||||
if (sig != MBR_SIGNATURE) {
|
||||
offset_print(" MBR: no valid signature on ");
|
||||
offset_print(parent_name);
|
||||
offset_print(" (sig=");
|
||||
print_hex(sig);
|
||||
offset_print(")\n");
|
||||
return 0; /* Not an error, just no MBR */
|
||||
}
|
||||
|
||||
/* Build the class name for partition sub-devices: parentName + "mbr" */
|
||||
char class_name[DEVICEFS_MAX_DEV_NAME];
|
||||
memset(class_name, 0, sizeof(class_name));
|
||||
strncpy(class_name, parent_name, DEVICEFS_MAX_DEV_NAME - 4);
|
||||
/* Append "mbr" */
|
||||
uint32_t clen = strlen(class_name);
|
||||
if (clen + 3 < DEVICEFS_MAX_DEV_NAME) {
|
||||
class_name[clen] = 'm';
|
||||
class_name[clen + 1] = 'b';
|
||||
class_name[clen + 2] = 'r';
|
||||
class_name[clen + 3] = '\0';
|
||||
}
|
||||
|
||||
/* Parse the 4 partition table entries (at offset 446) */
|
||||
int found = 0;
|
||||
for (int i = 0; i < MBR_MAX_PARTITIONS; i++) {
|
||||
mbr_partition_entry_t *entry =
|
||||
(mbr_partition_entry_t *)(sector + 446 + i * 16);
|
||||
|
||||
/* Skip empty partitions */
|
||||
if (entry->type == MBR_TYPE_EMPTY) continue;
|
||||
if (entry->sector_count == 0) continue;
|
||||
|
||||
/* Skip extended partitions for now */
|
||||
if (entry->type == MBR_TYPE_EXTENDED) {
|
||||
offset_print(" MBR: skipping extended partition on ");
|
||||
offset_print(parent_name);
|
||||
offset_print("\n");
|
||||
continue;
|
||||
}
|
||||
|
||||
mbr_sub_device_t *sub = alloc_sub();
|
||||
if (!sub) {
|
||||
offset_print(" MBR: no free sub-device slots\n");
|
||||
break;
|
||||
}
|
||||
|
||||
sub->parent = parent;
|
||||
sub->lba_start = entry->lba_start;
|
||||
sub->sector_count = entry->sector_count;
|
||||
sub->type = entry->type;
|
||||
|
||||
/* Register with devicefs */
|
||||
devicefs_device_t *dev = devicefs_register_block(class_name,
|
||||
&mbr_sub_ops,
|
||||
sub);
|
||||
if (dev) {
|
||||
offset_print(" MBR: ");
|
||||
offset_print(dev->name);
|
||||
offset_print(" type=");
|
||||
print_hex(entry->type);
|
||||
offset_print(" LBA=");
|
||||
print_hex(entry->lba_start);
|
||||
offset_print(" size=");
|
||||
print_hex(entry->sector_count);
|
||||
offset_print(" sectors\n");
|
||||
found++;
|
||||
}
|
||||
}
|
||||
|
||||
if (found == 0) {
|
||||
offset_print(" MBR: no partitions on ");
|
||||
offset_print(parent_name);
|
||||
offset_print("\n");
|
||||
}
|
||||
|
||||
return found;
|
||||
}
|
||||
|
||||
int init_mbr(void) {
|
||||
memset(sub_devices, 0, sizeof(sub_devices));
|
||||
|
||||
/* Scan all "hdd" class devices for MBR partitions */
|
||||
int total_partitions = 0;
|
||||
for (uint32_t i = 1; i <= 16; i++) {
|
||||
/* Build device name: hdd1, hdd2, ... */
|
||||
char name[DEVICEFS_MAX_DEV_NAME];
|
||||
memset(name, 0, sizeof(name));
|
||||
name[0] = 'h'; name[1] = 'd'; name[2] = 'd';
|
||||
/* Append number */
|
||||
if (i < 10) {
|
||||
name[3] = (char)('0' + i);
|
||||
} else {
|
||||
name[3] = (char)('0' + i / 10);
|
||||
name[4] = (char)('0' + i % 10);
|
||||
}
|
||||
|
||||
devicefs_device_t *dev = devicefs_find(name);
|
||||
if (!dev) continue;
|
||||
|
||||
int n = mbr_scan(name);
|
||||
if (n > 0) total_partitions += n;
|
||||
}
|
||||
|
||||
offset_print(" MBR: ");
|
||||
print_hex((uint32_t)total_partitions);
|
||||
offset_print(" partition(s) found\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
67
src/mbr.h
Normal file
67
src/mbr.h
Normal file
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* @file mbr.h
|
||||
* @brief MBR (Master Boot Record) partition table driver.
|
||||
*
|
||||
* Scans block devices for MBR partition tables and registers each
|
||||
* discovered partition as a new block device in the devicefs.
|
||||
* For example, hdd1 with two partitions becomes hdd1mbr1 and hdd1mbr2.
|
||||
*
|
||||
* The partition number (Y in hddNmbrY) is assigned by the devicefs
|
||||
* subsystem using the device class "hddNmbr".
|
||||
*/
|
||||
|
||||
#ifndef MBR_H
|
||||
#define MBR_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/** MBR signature bytes (offset 510-511). */
|
||||
#define MBR_SIGNATURE 0xAA55
|
||||
|
||||
/** Maximum partitions in a standard MBR. */
|
||||
#define MBR_MAX_PARTITIONS 4
|
||||
|
||||
/** MBR partition table entry (16 bytes each). */
|
||||
typedef struct __attribute__((packed)) mbr_partition_entry {
|
||||
uint8_t status; /**< 0x80 = bootable, 0x00 = not bootable. */
|
||||
uint8_t chs_first[3]; /**< CHS address of first sector. */
|
||||
uint8_t type; /**< Partition type code. */
|
||||
uint8_t chs_last[3]; /**< CHS address of last sector. */
|
||||
uint32_t lba_start; /**< LBA of first sector. */
|
||||
uint32_t sector_count; /**< Number of sectors. */
|
||||
} mbr_partition_entry_t;
|
||||
|
||||
/** Well-known MBR partition types. */
|
||||
#define MBR_TYPE_EMPTY 0x00
|
||||
#define MBR_TYPE_FAT12 0x01
|
||||
#define MBR_TYPE_FAT16_SM 0x04 /**< FAT16 < 32 MiB. */
|
||||
#define MBR_TYPE_EXTENDED 0x05
|
||||
#define MBR_TYPE_FAT16_LG 0x06 /**< FAT16 >= 32 MiB. */
|
||||
#define MBR_TYPE_FAT32 0x0B
|
||||
#define MBR_TYPE_FAT32_LBA 0x0C
|
||||
#define MBR_TYPE_FAT16_LBA 0x0E
|
||||
#define MBR_TYPE_LINUX 0x83
|
||||
#define MBR_TYPE_LINUX_SWAP 0x82
|
||||
|
||||
/**
|
||||
* Scan a block device for MBR partitions.
|
||||
*
|
||||
* Reads sector 0 of the given device, validates the MBR signature,
|
||||
* and registers each non-empty partition entry with the devicefs
|
||||
* as a sub-device.
|
||||
*
|
||||
* @param parent_name Name of the parent device (e.g., "hdd1").
|
||||
* @return Number of partitions found, or -1 on error.
|
||||
*/
|
||||
int mbr_scan(const char *parent_name);
|
||||
|
||||
/**
|
||||
* Initialize the MBR subsystem.
|
||||
*
|
||||
* Scans all currently registered "hdd" class devices for MBR partitions.
|
||||
*
|
||||
* @return 0 on success.
|
||||
*/
|
||||
int init_mbr(void);
|
||||
|
||||
#endif /* MBR_H */
|
||||
541
src/ne2000.c
Normal file
541
src/ne2000.c
Normal file
@@ -0,0 +1,541 @@
|
||||
/**
|
||||
* @file ne2000.c
|
||||
* @brief NE2000-compatible ISA Ethernet NIC driver implementation.
|
||||
*
|
||||
* Drives NE2000-compatible NICs based on the DP8390 Ethernet controller.
|
||||
* Uses programmed I/O (PIO) remote DMA for data transfer and IRQ-driven
|
||||
* packet reception.
|
||||
*
|
||||
* The driver probes I/O base 0x300 by performing a reset and checking
|
||||
* the ISR reset bit. It reads the MAC address from the card's PROM
|
||||
* (first 6 bytes of on-card memory), configures the TX and RX ring
|
||||
* buffers, and registers as a character device with devicefs.
|
||||
*
|
||||
* NE2000 memory layout (16 KiB, 64 pages of 256 bytes):
|
||||
* Pages 0x40-0x45: TX buffer (1536 bytes, holds 1 MTU frame)
|
||||
* Pages 0x46-0x7F: RX ring buffer (~14.5 KiB)
|
||||
*/
|
||||
|
||||
#include "ne2000.h"
|
||||
#include "port_io.h"
|
||||
#include "pic.h"
|
||||
#include "devicefs.h"
|
||||
#include "ethernet.h"
|
||||
#include "driver.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Debug print helpers defined in kernel.c */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/* ================================================================
|
||||
* Global state
|
||||
* ================================================================ */
|
||||
|
||||
/** Single NE2000 device (we only support one for now). */
|
||||
static ne2k_device_t ne2k_dev;
|
||||
|
||||
/** Volatile flag set by IRQ handler when a packet arrives. */
|
||||
static volatile int ne2k_rx_ready = 0;
|
||||
|
||||
/** Volatile flag set by IRQ handler when transmit completes. */
|
||||
static volatile int ne2k_tx_done = 0;
|
||||
|
||||
/* ================================================================
|
||||
* Register access helpers
|
||||
* ================================================================ */
|
||||
|
||||
/** Write to a NE2000 register. */
|
||||
static inline void ne2k_write(uint16_t base, uint8_t reg, uint8_t val) {
|
||||
outb(base + reg, val);
|
||||
}
|
||||
|
||||
/** Read from a NE2000 register. */
|
||||
static inline uint8_t ne2k_read(uint16_t base, uint8_t reg) {
|
||||
return inb(base + reg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Select a register page (0, 1, or 2).
|
||||
* Preserves the STA/STP and RD bits.
|
||||
*/
|
||||
static void ne2k_page(uint16_t base, uint8_t page) {
|
||||
uint8_t cr = ne2k_read(base, NE2K_CR);
|
||||
cr = (cr & ~(CR_PS0 | CR_PS1)) | ((page & 0x03) << 6);
|
||||
ne2k_write(base, NE2K_CR, cr);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Remote DMA (PIO) operations
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Read bytes from the NE2000's on-card memory via remote DMA.
|
||||
*
|
||||
* @param base I/O base address.
|
||||
* @param src Source address in card memory (byte address).
|
||||
* @param dst Destination buffer in system RAM.
|
||||
* @param len Number of bytes to read.
|
||||
*/
|
||||
static void ne2k_dma_read(uint16_t base, uint16_t src, void *dst, uint16_t len) {
|
||||
ne2k_write(base, NE2K_CR, CR_STA | CR_DMA_ABORT);
|
||||
|
||||
/* Set remote DMA byte count */
|
||||
ne2k_write(base, NE2K_RBCR0, (uint8_t)(len & 0xFF));
|
||||
ne2k_write(base, NE2K_RBCR1, (uint8_t)((len >> 8) & 0xFF));
|
||||
|
||||
/* Set remote DMA start address */
|
||||
ne2k_write(base, NE2K_RSAR0, (uint8_t)(src & 0xFF));
|
||||
ne2k_write(base, NE2K_RSAR1, (uint8_t)((src >> 8) & 0xFF));
|
||||
|
||||
/* Start remote DMA read */
|
||||
ne2k_write(base, NE2K_CR, CR_STA | CR_DMA_READ);
|
||||
|
||||
/* Read data from data port */
|
||||
uint8_t *buf = (uint8_t *)dst;
|
||||
uint16_t words = len / 2;
|
||||
for (uint16_t i = 0; i < words; i++) {
|
||||
uint16_t w = inw(base + NE2K_DATA);
|
||||
buf[i * 2] = (uint8_t)(w & 0xFF);
|
||||
buf[i * 2 + 1] = (uint8_t)((w >> 8) & 0xFF);
|
||||
}
|
||||
if (len & 1) {
|
||||
uint16_t w = inw(base + NE2K_DATA);
|
||||
buf[len - 1] = (uint8_t)(w & 0xFF);
|
||||
}
|
||||
|
||||
/* Wait for DMA complete */
|
||||
int timeout = 100000;
|
||||
while (!(ne2k_read(base, NE2K_ISR) & ISR_RDC) && --timeout > 0);
|
||||
|
||||
/* Acknowledge RDC */
|
||||
ne2k_write(base, NE2K_ISR, ISR_RDC);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write bytes to the NE2000's on-card memory via remote DMA.
|
||||
*
|
||||
* @param base I/O base address.
|
||||
* @param dst Destination address in card memory (byte address).
|
||||
* @param src Source buffer in system RAM.
|
||||
* @param len Number of bytes to write.
|
||||
*/
|
||||
static void ne2k_dma_write(uint16_t base, uint16_t dst, const void *src,
|
||||
uint16_t len) {
|
||||
ne2k_write(base, NE2K_CR, CR_STA | CR_DMA_ABORT);
|
||||
|
||||
/* Set remote DMA byte count */
|
||||
ne2k_write(base, NE2K_RBCR0, (uint8_t)(len & 0xFF));
|
||||
ne2k_write(base, NE2K_RBCR1, (uint8_t)((len >> 8) & 0xFF));
|
||||
|
||||
/* Set remote DMA start address */
|
||||
ne2k_write(base, NE2K_RSAR0, (uint8_t)(dst & 0xFF));
|
||||
ne2k_write(base, NE2K_RSAR1, (uint8_t)((dst >> 8) & 0xFF));
|
||||
|
||||
/* Start remote DMA write */
|
||||
ne2k_write(base, NE2K_CR, CR_STA | CR_DMA_WRITE);
|
||||
|
||||
/* Write data to data port */
|
||||
const uint8_t *buf = (const uint8_t *)src;
|
||||
uint16_t words = len / 2;
|
||||
for (uint16_t i = 0; i < words; i++) {
|
||||
uint16_t w = (uint16_t)buf[i * 2] | ((uint16_t)buf[i * 2 + 1] << 8);
|
||||
outw(base + NE2K_DATA, w);
|
||||
}
|
||||
if (len & 1) {
|
||||
outw(base + NE2K_DATA, (uint16_t)buf[len - 1]);
|
||||
}
|
||||
|
||||
/* Wait for DMA complete */
|
||||
int timeout = 100000;
|
||||
while (!(ne2k_read(base, NE2K_ISR) & ISR_RDC) && --timeout > 0);
|
||||
|
||||
/* Acknowledge RDC */
|
||||
ne2k_write(base, NE2K_ISR, ISR_RDC);
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Packet send / receive
|
||||
* ================================================================ */
|
||||
|
||||
int ne2k_send(ne2k_device_t *dev, const void *data, uint32_t len) {
|
||||
if (!dev || !dev->present) return -1;
|
||||
if (len > ETH_FRAME_MAX) return -1;
|
||||
|
||||
/* Minimum Ethernet frame size is 60 bytes (without CRC) */
|
||||
uint32_t send_len = len;
|
||||
if (send_len < 60) send_len = 60;
|
||||
|
||||
uint16_t base = dev->io_base;
|
||||
|
||||
/* Write packet to TX buffer on card */
|
||||
ne2k_dma_write(base, (uint16_t)(NE2K_TX_START << 8),
|
||||
data, (uint16_t)len);
|
||||
|
||||
/* If we need to pad, write zeros for the remaining bytes.
|
||||
* For simplicity, the DMA write already handles partial words.
|
||||
* The card will transmit send_len bytes from the TX page. */
|
||||
|
||||
/* Set transmit page start */
|
||||
ne2k_page(base, 0);
|
||||
ne2k_write(base, NE2K_TPSR, NE2K_TX_START);
|
||||
ne2k_write(base, NE2K_TBCR0, (uint8_t)(send_len & 0xFF));
|
||||
ne2k_write(base, NE2K_TBCR1, (uint8_t)((send_len >> 8) & 0xFF));
|
||||
|
||||
/* Trigger transmit */
|
||||
ne2k_tx_done = 0;
|
||||
ne2k_write(base, NE2K_CR, CR_STA | CR_TXP | CR_DMA_ABORT);
|
||||
|
||||
/* Wait for transmit completion (with timeout) */
|
||||
int timeout = 1000000;
|
||||
while (!ne2k_tx_done && --timeout > 0) {
|
||||
asm volatile("pause");
|
||||
}
|
||||
|
||||
if (timeout == 0) {
|
||||
offset_print(" NE2K: tx timeout\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ne2k_recv(ne2k_device_t *dev, void *buf, uint32_t bufsize) {
|
||||
if (!dev || !dev->present) return -1;
|
||||
|
||||
uint16_t base = dev->io_base;
|
||||
|
||||
/* Check if there's a packet in the ring */
|
||||
ne2k_page(base, 1);
|
||||
uint8_t curr = ne2k_read(base, NE2K_CURR);
|
||||
ne2k_page(base, 0);
|
||||
|
||||
if (dev->next_rx_page == curr) {
|
||||
return 0; /* Ring is empty */
|
||||
}
|
||||
|
||||
/* Read the 4-byte rx header from the ring */
|
||||
ne2k_rx_header_t hdr;
|
||||
uint16_t hdr_addr = (uint16_t)(dev->next_rx_page << 8);
|
||||
ne2k_dma_read(base, hdr_addr, &hdr, sizeof(hdr));
|
||||
|
||||
/* Sanity check the header */
|
||||
uint16_t pkt_len = hdr.length - (uint16_t)sizeof(ne2k_rx_header_t);
|
||||
if (pkt_len > ETH_FRAME_MAX || pkt_len == 0) {
|
||||
/* Invalid packet, advance pointer and skip */
|
||||
dev->next_rx_page = hdr.next_page;
|
||||
ne2k_write(base, NE2K_BNRY,
|
||||
(dev->next_rx_page == NE2K_RX_START)
|
||||
? NE2K_RX_STOP - 1
|
||||
: dev->next_rx_page - 1);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Determine how many bytes to copy to caller */
|
||||
uint32_t copy_len = pkt_len;
|
||||
if (copy_len > bufsize) copy_len = bufsize;
|
||||
|
||||
/* Read packet data (starts after the 4-byte header) */
|
||||
uint16_t data_addr = hdr_addr + (uint16_t)sizeof(ne2k_rx_header_t);
|
||||
|
||||
/* Handle ring buffer wrap-around */
|
||||
uint16_t ring_end = (uint16_t)(NE2K_RX_STOP << 8);
|
||||
uint16_t ring_start = (uint16_t)(NE2K_RX_START << 8);
|
||||
|
||||
if (data_addr + copy_len > ring_end) {
|
||||
/* Wraps around */
|
||||
uint16_t first_part = ring_end - data_addr;
|
||||
ne2k_dma_read(base, data_addr, buf, first_part);
|
||||
ne2k_dma_read(base, ring_start,
|
||||
(uint8_t *)buf + first_part,
|
||||
(uint16_t)(copy_len - first_part));
|
||||
} else {
|
||||
ne2k_dma_read(base, data_addr, buf, (uint16_t)copy_len);
|
||||
}
|
||||
|
||||
/* Advance BNRY to next_page (one behind CURR means buffer is full) */
|
||||
dev->next_rx_page = hdr.next_page;
|
||||
ne2k_write(base, NE2K_BNRY,
|
||||
(dev->next_rx_page == NE2K_RX_START)
|
||||
? NE2K_RX_STOP - 1
|
||||
: dev->next_rx_page - 1);
|
||||
|
||||
return (int)copy_len;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* IRQ handler
|
||||
* ================================================================ */
|
||||
|
||||
void ne2k_irq(void) {
|
||||
if (!ne2k_dev.present) return;
|
||||
|
||||
uint16_t base = ne2k_dev.io_base;
|
||||
uint8_t isr = ne2k_read(base, NE2K_ISR);
|
||||
|
||||
if (isr & ISR_PRX) {
|
||||
/* Packet received */
|
||||
ne2k_rx_ready = 1;
|
||||
ne2k_write(base, NE2K_ISR, ISR_PRX);
|
||||
}
|
||||
|
||||
if (isr & ISR_PTX) {
|
||||
/* Packet transmitted */
|
||||
ne2k_tx_done = 1;
|
||||
ne2k_write(base, NE2K_ISR, ISR_PTX);
|
||||
}
|
||||
|
||||
if (isr & ISR_RXE) {
|
||||
/* Receive error */
|
||||
ne2k_write(base, NE2K_ISR, ISR_RXE);
|
||||
}
|
||||
|
||||
if (isr & ISR_TXE) {
|
||||
/* Transmit error */
|
||||
ne2k_tx_done = 1; /* Unblock sender even on error */
|
||||
ne2k_write(base, NE2K_ISR, ISR_TXE);
|
||||
}
|
||||
|
||||
if (isr & ISR_OVW) {
|
||||
/* Overflow — need to handle by resetting RX */
|
||||
ne2k_write(base, NE2K_ISR, ISR_OVW);
|
||||
}
|
||||
|
||||
if (isr & ISR_CNT) {
|
||||
/* Counter overflow */
|
||||
ne2k_read(base, NE2K_CNTR0);
|
||||
ne2k_read(base, NE2K_CNTR1);
|
||||
ne2k_read(base, NE2K_CNTR2);
|
||||
ne2k_write(base, NE2K_ISR, ISR_CNT);
|
||||
}
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Initialization
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Read the MAC address from the NE2000 PROM.
|
||||
*
|
||||
* The first 32 bytes of NE2000's memory contain the PROM data.
|
||||
* In word-wide mode, each MAC byte is duplicated in the high byte,
|
||||
* so the PROM looks like: M0 M0 M1 M1 M2 M2 M3 M3 M4 M4 M5 M5 ...
|
||||
*/
|
||||
static void ne2k_read_mac(uint16_t base, uint8_t *mac) {
|
||||
uint8_t prom[32];
|
||||
ne2k_dma_read(base, 0x0000, prom, 32);
|
||||
|
||||
/* NE2000 duplicates each byte in word mode */
|
||||
mac[0] = prom[0];
|
||||
mac[1] = prom[2];
|
||||
mac[2] = prom[4];
|
||||
mac[3] = prom[6];
|
||||
mac[4] = prom[8];
|
||||
mac[5] = prom[10];
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset and initialize the NE2000 controller.
|
||||
*/
|
||||
static int ne2k_hw_init(uint16_t base) {
|
||||
/* Reset the card: read from RESET port, then write to it */
|
||||
uint8_t tmp = inb(base + NE2K_RESET);
|
||||
outb(base + NE2K_RESET, tmp);
|
||||
|
||||
/* Wait for reset to complete (ISR bit 7 = RST set) */
|
||||
int timeout = 100000;
|
||||
while (!(ne2k_read(base, NE2K_ISR) & ISR_RST) && --timeout > 0);
|
||||
if (timeout == 0) {
|
||||
offset_print(" NE2K: reset timeout\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Acknowledge the reset */
|
||||
ne2k_write(base, NE2K_ISR, 0xFF);
|
||||
|
||||
/* Stop the NIC and abort any DMA */
|
||||
ne2k_write(base, NE2K_CR, CR_STP | CR_DMA_ABORT);
|
||||
|
||||
/* Data Configuration Register:
|
||||
* Word-wide transfers (WTS), normal operation (LS),
|
||||
* FIFO threshold 8 bytes (FT1) */
|
||||
ne2k_write(base, NE2K_DCR, DCR_WTS | DCR_LS | DCR_FT1);
|
||||
|
||||
/* Clear remote byte count registers (required before starting) */
|
||||
ne2k_write(base, NE2K_RBCR0, 0);
|
||||
ne2k_write(base, NE2K_RBCR1, 0);
|
||||
|
||||
/* Receive Configuration: Accept broadcast and physical match */
|
||||
ne2k_write(base, NE2K_RCR, RCR_AB);
|
||||
|
||||
/* Transmit Configuration: internal loopback during init */
|
||||
ne2k_write(base, NE2K_TCR, TCR_LB0);
|
||||
|
||||
/* Set up ring buffer pointers */
|
||||
ne2k_write(base, NE2K_PSTART, NE2K_RX_START);
|
||||
ne2k_write(base, NE2K_PSTOP, NE2K_RX_STOP);
|
||||
ne2k_write(base, NE2K_BNRY, NE2K_RX_START);
|
||||
|
||||
/* Read MAC address from PROM */
|
||||
ne2k_read_mac(base, ne2k_dev.mac);
|
||||
|
||||
/* Switch to page 1 to set PAR (Physical Address) and CURR */
|
||||
ne2k_page(base, 1);
|
||||
ne2k_write(base, NE2K_PAR0, ne2k_dev.mac[0]);
|
||||
ne2k_write(base, NE2K_PAR1, ne2k_dev.mac[1]);
|
||||
ne2k_write(base, NE2K_PAR2, ne2k_dev.mac[2]);
|
||||
ne2k_write(base, NE2K_PAR3, ne2k_dev.mac[3]);
|
||||
ne2k_write(base, NE2K_PAR4, ne2k_dev.mac[4]);
|
||||
ne2k_write(base, NE2K_PAR5, ne2k_dev.mac[5]);
|
||||
|
||||
/* Set current page (next write page for incoming packets) */
|
||||
ne2k_write(base, NE2K_CURR, NE2K_RX_START + 1);
|
||||
ne2k_dev.next_rx_page = NE2K_RX_START + 1;
|
||||
|
||||
/* Set multicast address registers to accept all multicast */
|
||||
for (int i = 0; i < 8; i++) {
|
||||
ne2k_write(base, NE2K_MAR0 + (uint8_t)i, 0xFF);
|
||||
}
|
||||
|
||||
/* Switch back to page 0 */
|
||||
ne2k_page(base, 0);
|
||||
|
||||
/* Clear all pending interrupts */
|
||||
ne2k_write(base, NE2K_ISR, 0xFF);
|
||||
|
||||
/* Enable interrupts: PRX, PTX, RXE, TXE, OVW, CNT */
|
||||
ne2k_write(base, NE2K_IMR, ISR_PRX | ISR_PTX | ISR_RXE |
|
||||
ISR_TXE | ISR_OVW | ISR_CNT);
|
||||
|
||||
/* Take the NIC out of loopback: normal transmit configuration */
|
||||
ne2k_write(base, NE2K_TCR, 0x00);
|
||||
|
||||
/* Start the NIC */
|
||||
ne2k_write(base, NE2K_CR, CR_STA | CR_DMA_ABORT);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Driver framework integration
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Probe for an NE2000 card at the default I/O base.
|
||||
*
|
||||
* Reset the card, check the ISR reset bit, then verify by reading
|
||||
* the PROM (MAC address). If all bytes are 0xFF or 0x00 there is
|
||||
* no real card at this address.
|
||||
*/
|
||||
static driver_probe_result_t ne2k_probe(void) {
|
||||
uint16_t base = NE2K_DEFAULT_IOBASE;
|
||||
|
||||
/* Try to reset */
|
||||
uint8_t tmp = inb(base + NE2K_RESET);
|
||||
outb(base + NE2K_RESET, tmp);
|
||||
|
||||
/* Brief delay */
|
||||
for (volatile int i = 0; i < 10000; i++) {
|
||||
asm volatile("pause");
|
||||
}
|
||||
|
||||
/* Check if the ISR reset bit is set */
|
||||
uint8_t isr = ne2k_read(base, NE2K_ISR);
|
||||
if (!(isr & ISR_RST)) {
|
||||
return DRIVER_PROBE_NOT_FOUND;
|
||||
}
|
||||
|
||||
/* Acknowledge the reset */
|
||||
ne2k_write(base, NE2K_ISR, 0xFF);
|
||||
|
||||
/* Configure for PROM reading */
|
||||
ne2k_write(base, NE2K_CR, CR_STP | CR_DMA_ABORT);
|
||||
ne2k_write(base, NE2K_DCR, DCR_WTS | DCR_LS | DCR_FT1);
|
||||
ne2k_write(base, NE2K_RBCR0, 0);
|
||||
ne2k_write(base, NE2K_RBCR1, 0);
|
||||
ne2k_write(base, NE2K_RCR, RCR_MON);
|
||||
ne2k_write(base, NE2K_TCR, TCR_LB0);
|
||||
ne2k_write(base, NE2K_PSTART, NE2K_RX_START);
|
||||
ne2k_write(base, NE2K_PSTOP, NE2K_RX_STOP);
|
||||
ne2k_write(base, NE2K_BNRY, NE2K_RX_START);
|
||||
|
||||
/* Read 32 bytes of PROM data */
|
||||
uint8_t prom[32];
|
||||
ne2k_dma_read(base, 0x0000, prom, 32);
|
||||
|
||||
/* Validate: the MAC should not be all 0xFF or all 0x00 */
|
||||
int all_ff = 1, all_00 = 1;
|
||||
for (int i = 0; i < 12; i += 2) {
|
||||
if (prom[i] != 0xFF) all_ff = 0;
|
||||
if (prom[i] != 0x00) all_00 = 0;
|
||||
}
|
||||
|
||||
if (all_ff || all_00) {
|
||||
return DRIVER_PROBE_NOT_FOUND;
|
||||
}
|
||||
|
||||
return DRIVER_PROBE_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the NE2000 driver.
|
||||
*/
|
||||
static int ne2k_driver_init(void) {
|
||||
memset(&ne2k_dev, 0, sizeof(ne2k_dev));
|
||||
|
||||
ne2k_dev.io_base = NE2K_DEFAULT_IOBASE;
|
||||
ne2k_dev.irq = NE2K_DEFAULT_IRQ;
|
||||
|
||||
/* Enable IRQ in PIC */
|
||||
pic_clear_mask(ne2k_dev.irq);
|
||||
|
||||
/* Initialize hardware */
|
||||
if (ne2k_hw_init(ne2k_dev.io_base) != 0) {
|
||||
offset_print(" NE2K: initialization failed\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
ne2k_dev.present = 1;
|
||||
|
||||
/* Print MAC address */
|
||||
offset_print(" NE2K: MAC ");
|
||||
for (int i = 0; i < 6; i++) {
|
||||
if (i > 0) offset_print(":");
|
||||
print_hex(ne2k_dev.mac[i]);
|
||||
}
|
||||
offset_print("\n");
|
||||
|
||||
/* Register with ethernet subsystem (creates /dev/ethN) */
|
||||
ethernet_register(ne2k_dev.mac,
|
||||
(eth_send_fn)ne2k_send,
|
||||
(eth_recv_fn)ne2k_recv,
|
||||
&ne2k_dev);
|
||||
|
||||
offset_print(" NE2K: initialized on I/O ");
|
||||
print_hex(ne2k_dev.io_base);
|
||||
offset_print(" IRQ ");
|
||||
print_hex(ne2k_dev.irq);
|
||||
offset_print("\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
ne2k_device_t *ne2k_get_device(void) {
|
||||
return ne2k_dev.present ? &ne2k_dev : NULL;
|
||||
}
|
||||
|
||||
int ne2k_init(void) {
|
||||
return ne2k_driver_init();
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Driver registration
|
||||
* ================================================================ */
|
||||
|
||||
static const driver_t ne2k_driver = {
|
||||
.name = "ne2000",
|
||||
.probe = ne2k_probe,
|
||||
.init = ne2k_driver_init,
|
||||
};
|
||||
|
||||
REGISTER_DRIVER(ne2k_driver);
|
||||
221
src/ne2000.h
Normal file
221
src/ne2000.h
Normal file
@@ -0,0 +1,221 @@
|
||||
/**
|
||||
* @file ne2000.h
|
||||
* @brief NE2000-compatible ISA Ethernet NIC driver.
|
||||
*
|
||||
* Drives NE2000-compatible NICs based on the DP8390 Ethernet chip.
|
||||
* The driver probes I/O base 0x300 (the common default) and uses
|
||||
* IRQ 9 for interrupt-driven packet reception.
|
||||
*
|
||||
* Packets are stored in the card's internal 16 KiB RAM using a
|
||||
* ring buffer. The driver transmits packets synchronously.
|
||||
*
|
||||
* The NE2000 registers with the devicefs as a character device
|
||||
* (named "eth1", "eth2", etc.) for the ethernet subsystem to use.
|
||||
*/
|
||||
|
||||
#ifndef NE2000_H
|
||||
#define NE2000_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* ================================================================
|
||||
* NE2000 I/O Port Layout (offsets from base)
|
||||
*
|
||||
* The DP8390 has 3 register pages selected by bits 6-7 of the
|
||||
* Command register (offset 0x00).
|
||||
* ================================================================ */
|
||||
|
||||
/** Default ISA I/O base for NE2000. */
|
||||
#define NE2K_DEFAULT_IOBASE 0x300
|
||||
|
||||
/** Default ISA IRQ for NE2000. */
|
||||
#define NE2K_DEFAULT_IRQ 9
|
||||
|
||||
/* --- Shared registers (all pages) --- */
|
||||
#define NE2K_CR 0x00 /**< Command register */
|
||||
|
||||
/* --- Page 0 read registers --- */
|
||||
#define NE2K_CLDA0 0x01 /**< Current Local DMA Address 0 */
|
||||
#define NE2K_CLDA1 0x02 /**< Current Local DMA Address 1 */
|
||||
#define NE2K_BNRY 0x03 /**< Boundary pointer (last read page) */
|
||||
#define NE2K_TSR 0x04 /**< Transmit Status Register */
|
||||
#define NE2K_NCR 0x05 /**< Number of Collisions Register */
|
||||
#define NE2K_FIFO 0x06 /**< FIFO */
|
||||
#define NE2K_ISR 0x07 /**< Interrupt Status Register */
|
||||
#define NE2K_CRDA0 0x08 /**< Current Remote DMA Address 0 */
|
||||
#define NE2K_CRDA1 0x09 /**< Current Remote DMA Address 1 */
|
||||
#define NE2K_RSR 0x0C /**< Receive Status Register */
|
||||
#define NE2K_CNTR0 0x0D /**< Tally Counter 0 (frame alignment errors) */
|
||||
#define NE2K_CNTR1 0x0E /**< Tally Counter 1 (CRC errors) */
|
||||
#define NE2K_CNTR2 0x0F /**< Tally Counter 2 (missed packets) */
|
||||
|
||||
/* --- Page 0 write registers --- */
|
||||
#define NE2K_PSTART 0x01 /**< Page Start (rx ring start, in pages) */
|
||||
#define NE2K_PSTOP 0x02 /**< Page Stop (rx ring end, in pages) */
|
||||
/* BNRY = 0x03 shared */
|
||||
#define NE2K_TPSR 0x04 /**< Transmit Page Start */
|
||||
#define NE2K_TBCR0 0x05 /**< Transmit Byte Count 0 */
|
||||
#define NE2K_TBCR1 0x06 /**< Transmit Byte Count 1 */
|
||||
/* ISR = 0x07 shared */
|
||||
#define NE2K_RSAR0 0x08 /**< Remote Start Address 0 */
|
||||
#define NE2K_RSAR1 0x09 /**< Remote Start Address 1 */
|
||||
#define NE2K_RBCR0 0x0A /**< Remote Byte Count 0 */
|
||||
#define NE2K_RBCR1 0x0B /**< Remote Byte Count 1 */
|
||||
#define NE2K_RCR 0x0C /**< Receive Configuration Register */
|
||||
#define NE2K_TCR 0x0D /**< Transmit Configuration Register */
|
||||
#define NE2K_DCR 0x0E /**< Data Configuration Register */
|
||||
#define NE2K_IMR 0x0F /**< Interrupt Mask Register */
|
||||
|
||||
/* --- Page 1 registers (r/w) --- */
|
||||
/* NE2K_CR = 0x00 */
|
||||
#define NE2K_PAR0 0x01 /**< Physical Address 0 (MAC byte 0) */
|
||||
#define NE2K_PAR1 0x02
|
||||
#define NE2K_PAR2 0x03
|
||||
#define NE2K_PAR3 0x04
|
||||
#define NE2K_PAR4 0x05
|
||||
#define NE2K_PAR5 0x06
|
||||
#define NE2K_CURR 0x07 /**< Current Page (next rx write page) */
|
||||
#define NE2K_MAR0 0x08 /**< Multicast Address Register 0 */
|
||||
|
||||
/* --- NE2000 data port (offset 0x10) --- */
|
||||
#define NE2K_DATA 0x10 /**< Data port for remote DMA */
|
||||
#define NE2K_RESET 0x1F /**< Reset port */
|
||||
|
||||
/* ================================================================
|
||||
* Command Register (CR) bits
|
||||
* ================================================================ */
|
||||
#define CR_STP 0x01 /**< Stop: software reset */
|
||||
#define CR_STA 0x02 /**< Start: activate NIC */
|
||||
#define CR_TXP 0x04 /**< Transmit Packet */
|
||||
#define CR_RD0 0x08 /**< Remote DMA command bit 0 */
|
||||
#define CR_RD1 0x10 /**< Remote DMA command bit 1 */
|
||||
#define CR_RD2 0x20 /**< Remote DMA command bit 2 (abort/complete) */
|
||||
#define CR_PS0 0x40 /**< Page Select bit 0 */
|
||||
#define CR_PS1 0x80 /**< Page Select bit 1 */
|
||||
|
||||
/** Remote DMA read */
|
||||
#define CR_DMA_READ CR_RD0
|
||||
/** Remote DMA write */
|
||||
#define CR_DMA_WRITE CR_RD1
|
||||
/** Abort/complete remote DMA */
|
||||
#define CR_DMA_ABORT CR_RD2
|
||||
|
||||
/* ================================================================
|
||||
* ISR / IMR bits
|
||||
* ================================================================ */
|
||||
#define ISR_PRX 0x01 /**< Packet Received */
|
||||
#define ISR_PTX 0x02 /**< Packet Transmitted */
|
||||
#define ISR_RXE 0x04 /**< Receive Error */
|
||||
#define ISR_TXE 0x08 /**< Transmit Error */
|
||||
#define ISR_OVW 0x10 /**< Overflow Warning */
|
||||
#define ISR_CNT 0x20 /**< Counter Overflow */
|
||||
#define ISR_RDC 0x40 /**< Remote DMA Complete */
|
||||
#define ISR_RST 0x80 /**< Reset Status */
|
||||
|
||||
/* ================================================================
|
||||
* DCR bits
|
||||
* ================================================================ */
|
||||
#define DCR_WTS 0x01 /**< Word Transfer Select (1=16-bit) */
|
||||
#define DCR_BOS 0x02 /**< Byte Order Select */
|
||||
#define DCR_LAS 0x04 /**< Long Address Select */
|
||||
#define DCR_LS 0x08 /**< Loopback Select (1=normal) */
|
||||
#define DCR_AR 0x10 /**< Auto-initialize Remote */
|
||||
#define DCR_FT0 0x20 /**< FIFO Threshold bit 0 */
|
||||
#define DCR_FT1 0x40 /**< FIFO Threshold bit 1 */
|
||||
|
||||
/* ================================================================
|
||||
* TCR bits
|
||||
* ================================================================ */
|
||||
#define TCR_LB0 0x02 /**< Loopback bit 0 */
|
||||
#define TCR_LB1 0x04 /**< Loopback bit 1 */
|
||||
|
||||
/* ================================================================
|
||||
* RCR bits
|
||||
* ================================================================ */
|
||||
#define RCR_SEP 0x01 /**< Save Errored Packets */
|
||||
#define RCR_AR 0x02 /**< Accept Runt Packets */
|
||||
#define RCR_AB 0x04 /**< Accept Broadcast */
|
||||
#define RCR_AM 0x08 /**< Accept Multicast */
|
||||
#define RCR_PRO 0x10 /**< Promiscuous Mode */
|
||||
#define RCR_MON 0x20 /**< Monitor Mode */
|
||||
|
||||
/* ================================================================
|
||||
* NE2000 ring buffer layout (16 KiB on-card RAM)
|
||||
*
|
||||
* Pages are 256 bytes each. NE2000 has 32 pages (0x40-0x80).
|
||||
* - TX buffer: page 0x40 (room for 1 MTU frame, 6 pages)
|
||||
* - RX ring: pages 0x46-0x80
|
||||
* ================================================================ */
|
||||
#define NE2K_MEM_START 0x40 /**< Start of NE2000 RAM (page number) */
|
||||
#define NE2K_MEM_END 0x80 /**< End of NE2000 RAM (page number, exclusive) */
|
||||
#define NE2K_TX_START 0x40 /**< TX buffer start page */
|
||||
#define NE2K_RX_START 0x46 /**< RX ring start page */
|
||||
#define NE2K_RX_STOP 0x80 /**< RX ring stop page (exclusive) */
|
||||
|
||||
/** Maximum Ethernet frame size. */
|
||||
#define ETH_FRAME_MAX 1518
|
||||
#define ETH_HEADER_SIZE 14
|
||||
|
||||
/* ================================================================
|
||||
* NE2000 received packet header (prepended by the card)
|
||||
* ================================================================ */
|
||||
typedef struct __attribute__((packed)) ne2k_rx_header {
|
||||
uint8_t status; /**< Receive status (matches RSR). */
|
||||
uint8_t next_page; /**< Next packet page pointer. */
|
||||
uint16_t length; /**< Total length including this header. */
|
||||
} ne2k_rx_header_t;
|
||||
|
||||
/* ================================================================
|
||||
* NE2000 device state
|
||||
* ================================================================ */
|
||||
typedef struct ne2k_device {
|
||||
uint16_t io_base; /**< I/O base address. */
|
||||
uint8_t irq; /**< IRQ number. */
|
||||
uint8_t mac[6]; /**< MAC address. */
|
||||
uint8_t next_rx_page; /**< Next page to read from RX ring. */
|
||||
int present; /**< 1 if card detected. */
|
||||
} ne2k_device_t;
|
||||
|
||||
/* ================================================================
|
||||
* Public API
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Initialize NE2000 driver.
|
||||
* Called automatically via REGISTER_DRIVER.
|
||||
*/
|
||||
int ne2k_init(void);
|
||||
|
||||
/**
|
||||
* NE2000 IRQ handler.
|
||||
* Called from isr.c when the NE2000 IRQ fires.
|
||||
*/
|
||||
void ne2k_irq(void);
|
||||
|
||||
/**
|
||||
* Send an Ethernet frame.
|
||||
*
|
||||
* @param dev NE2000 device.
|
||||
* @param data Frame data (starting with destination MAC).
|
||||
* @param len Frame length in bytes (max 1518).
|
||||
* @return 0 on success, -1 on failure.
|
||||
*/
|
||||
int ne2k_send(ne2k_device_t *dev, const void *data, uint32_t len);
|
||||
|
||||
/**
|
||||
* Receive a pending Ethernet frame.
|
||||
*
|
||||
* @param dev NE2000 device.
|
||||
* @param buf Buffer for received frame.
|
||||
* @param bufsize Buffer size.
|
||||
* @return Number of bytes received, 0 if no packet, -1 on error.
|
||||
*/
|
||||
int ne2k_recv(ne2k_device_t *dev, void *buf, uint32_t bufsize);
|
||||
|
||||
/**
|
||||
* Get the NE2000 device pointer (for eth subsystem).
|
||||
* @return Pointer to the device struct, or NULL if not present.
|
||||
*/
|
||||
ne2k_device_t *ne2k_get_device(void);
|
||||
|
||||
#endif /* NE2000_H */
|
||||
384
src/paging.c
Normal file
384
src/paging.c
Normal file
@@ -0,0 +1,384 @@
|
||||
/**
|
||||
* @file paging.c
|
||||
* @brief Virtual memory paging subsystem implementation.
|
||||
*
|
||||
* Implements two-level x86 paging (page directory + page tables) with 4 KiB
|
||||
* pages. At initialization, all detected physical memory is identity-mapped
|
||||
* so that physical addresses equal virtual addresses. Drivers and the kernel
|
||||
* can then allocate additional virtual pages as needed.
|
||||
*
|
||||
* The kernel heap region starts at KERNEL_HEAP_START (0xD0000000) and grows
|
||||
* upward as pages are requested through paging_alloc_page().
|
||||
*/
|
||||
|
||||
#include "paging.h"
|
||||
#include "pmm.h"
|
||||
#include "port_io.h"
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
/* Debug print helpers defined in kernel.c */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/** Kernel heap starts at 0xD0000000 (above the 0xC0000000 higher-half region). */
|
||||
#define KERNEL_HEAP_START 0xD0000000
|
||||
/** Kernel heap ends at 0xF0000000 (768 MiB of virtual space for kernel heap). */
|
||||
#define KERNEL_HEAP_END 0xF0000000
|
||||
|
||||
/**
|
||||
* The page directory. Must be page-aligned (4 KiB).
|
||||
* Each entry either points to a page table or is zero (not present).
|
||||
*/
|
||||
static uint32_t page_directory[PAGE_ENTRIES] __attribute__((aligned(4096)));
|
||||
|
||||
/**
|
||||
* Storage for page tables. We pre-allocate enough for identity mapping.
|
||||
* For a system with up to 4 GiB, we'd need 1024 page tables, but we
|
||||
* only use these for the first 16 MiB during early boot. Additional page
|
||||
* tables are allocated from the PMM as needed.
|
||||
*
|
||||
* The first 16 MiB must be statically allocated because the PMM bitmap
|
||||
* itself lives in BSS within this region.
|
||||
*/
|
||||
#define STATIC_PT_COUNT 4
|
||||
static uint32_t static_page_tables[STATIC_PT_COUNT][PAGE_ENTRIES] __attribute__((aligned(4096)));
|
||||
|
||||
/**
|
||||
* Dynamically allocated page tables for memory above 16 MiB.
|
||||
* Before paging is enabled, we allocate these from the PMM and store
|
||||
* their physical addresses here so we can access them after paging.
|
||||
*/
|
||||
#define MAX_DYNAMIC_PT 256
|
||||
static uint32_t *dynamic_page_tables[MAX_DYNAMIC_PT];
|
||||
static uint32_t dynamic_pt_count = 0;
|
||||
|
||||
/** Next virtual address to hand out from the kernel heap. */
|
||||
static uint32_t heap_next = KERNEL_HEAP_START;
|
||||
|
||||
/**
|
||||
* Flush a single TLB entry for the given virtual address.
|
||||
*
|
||||
* @param vaddr The virtual address whose TLB entry to invalidate.
|
||||
*/
|
||||
static inline void tlb_flush_single(uint32_t vaddr) {
|
||||
__asm__ volatile("invlpg (%0)" : : "r"(vaddr) : "memory");
|
||||
}
|
||||
|
||||
/**
|
||||
* Reload CR3 to flush the entire TLB.
|
||||
*/
|
||||
static inline void tlb_flush_all(void) {
|
||||
uint32_t cr3;
|
||||
__asm__ volatile("mov %%cr3, %0" : "=r"(cr3));
|
||||
__asm__ volatile("mov %0, %%cr3" : : "r"(cr3) : "memory");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a page table for a given page directory index.
|
||||
*
|
||||
* If the page directory entry is not present, allocate a new page table
|
||||
* from the PMM and install it.
|
||||
*
|
||||
* @param pd_idx Page directory index (0–1023).
|
||||
* @param create If non-zero, create the page table if it doesn't exist.
|
||||
* @return Pointer to the page table, or NULL if not present and !create.
|
||||
*/
|
||||
static uint32_t *get_page_table(uint32_t pd_idx, int create) {
|
||||
if (page_directory[pd_idx] & PAGE_PRESENT) {
|
||||
return (uint32_t *)(page_directory[pd_idx] & 0xFFFFF000);
|
||||
}
|
||||
|
||||
if (!create) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Allocate a new page table from the PMM */
|
||||
phys_addr_t pt_phys = pmm_alloc_page(PMM_ZONE_NORMAL);
|
||||
if (pt_phys == 0) {
|
||||
offset_print(" PAGING: FATAL - could not allocate page table\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Zero the new page table */
|
||||
memset((void *)pt_phys, 0, 4096);
|
||||
|
||||
/* Install it in the page directory */
|
||||
page_directory[pd_idx] = pt_phys | PAGE_PRESENT | PAGE_WRITE;
|
||||
|
||||
return (uint32_t *)pt_phys;
|
||||
}
|
||||
|
||||
void paging_map_page(uint32_t vaddr, uint32_t paddr, uint32_t flags) {
|
||||
uint32_t pd_idx = PD_INDEX(vaddr);
|
||||
uint32_t pt_idx = PT_INDEX(vaddr);
|
||||
|
||||
uint32_t *pt = get_page_table(pd_idx, 1);
|
||||
if (!pt) {
|
||||
return;
|
||||
}
|
||||
|
||||
pt[pt_idx] = (paddr & 0xFFFFF000) | (flags & 0xFFF);
|
||||
tlb_flush_single(vaddr);
|
||||
}
|
||||
|
||||
void paging_unmap_page(uint32_t vaddr) {
|
||||
uint32_t pd_idx = PD_INDEX(vaddr);
|
||||
uint32_t pt_idx = PT_INDEX(vaddr);
|
||||
|
||||
uint32_t *pt = get_page_table(pd_idx, 0);
|
||||
if (!pt) {
|
||||
return;
|
||||
}
|
||||
|
||||
pt[pt_idx] = 0;
|
||||
tlb_flush_single(vaddr);
|
||||
}
|
||||
|
||||
uint32_t paging_get_physical(uint32_t vaddr) {
|
||||
uint32_t pd_idx = PD_INDEX(vaddr);
|
||||
uint32_t pt_idx = PT_INDEX(vaddr);
|
||||
|
||||
uint32_t *pt = get_page_table(pd_idx, 0);
|
||||
if (!pt) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!(pt[pt_idx] & PAGE_PRESENT)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (pt[pt_idx] & 0xFFFFF000) | (vaddr & 0xFFF);
|
||||
}
|
||||
|
||||
void *paging_alloc_page(void) {
|
||||
if (heap_next >= KERNEL_HEAP_END) {
|
||||
offset_print(" PAGING: kernel heap exhausted\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Allocate a physical page */
|
||||
phys_addr_t paddr = pmm_alloc_page(PMM_ZONE_NORMAL);
|
||||
if (paddr == 0) {
|
||||
offset_print(" PAGING: out of physical memory\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Map it into the kernel heap */
|
||||
uint32_t vaddr = heap_next;
|
||||
paging_map_page(vaddr, paddr, PAGE_PRESENT | PAGE_WRITE);
|
||||
heap_next += 4096;
|
||||
|
||||
return (void *)vaddr;
|
||||
}
|
||||
|
||||
void paging_free_page(void *vaddr) {
|
||||
uint32_t va = (uint32_t)vaddr;
|
||||
|
||||
/* Look up the physical address before unmapping */
|
||||
uint32_t paddr = paging_get_physical(va);
|
||||
if (paddr == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* Unmap the virtual page */
|
||||
paging_unmap_page(va);
|
||||
|
||||
/* Return the physical page to the PMM */
|
||||
pmm_free_page(paddr & 0xFFFFF000);
|
||||
}
|
||||
|
||||
void init_paging(void) {
|
||||
/* 1. Zero the page directory */
|
||||
memset(page_directory, 0, sizeof(page_directory));
|
||||
|
||||
/* 2. Identity map the first 16 MiB using static page tables.
|
||||
* This covers the kernel (loaded at 1 MiB), the PMM bitmap (in BSS),
|
||||
* the stack, and typical BIOS/device regions.
|
||||
* Each page table maps 4 MiB (1024 entries × 4 KiB).
|
||||
*/
|
||||
for (uint32_t i = 0; i < STATIC_PT_COUNT; i++) {
|
||||
memset(static_page_tables[i], 0, sizeof(static_page_tables[i]));
|
||||
|
||||
for (uint32_t j = 0; j < PAGE_ENTRIES; j++) {
|
||||
uint32_t paddr = (i * PAGE_ENTRIES + j) * 4096;
|
||||
static_page_tables[i][j] = paddr | PAGE_PRESENT | PAGE_WRITE;
|
||||
}
|
||||
|
||||
page_directory[i] = (uint32_t)static_page_tables[i] | PAGE_PRESENT | PAGE_WRITE;
|
||||
}
|
||||
|
||||
offset_print(" PAGING: identity mapped first 16 MiB\n");
|
||||
|
||||
/* 3. Identity map memory above 16 MiB using dynamically allocated page
|
||||
* tables. We do this BEFORE enabling paging, so physical addresses
|
||||
* are still directly accessible.
|
||||
*
|
||||
* mem_upper is in KiB and starts at 1 MiB, so total memory is
|
||||
* approximately (mem_upper + 1024) KiB.
|
||||
*/
|
||||
uint32_t mem_kb = pmm_get_memory_size() + 1024; /* total memory in KiB */
|
||||
uint32_t total_bytes = mem_kb * 1024;
|
||||
uint32_t pd_entries_needed = (total_bytes + (4 * 1024 * 1024 - 1)) / (4 * 1024 * 1024);
|
||||
|
||||
if (pd_entries_needed > PAGE_ENTRIES) {
|
||||
pd_entries_needed = PAGE_ENTRIES;
|
||||
}
|
||||
|
||||
dynamic_pt_count = 0;
|
||||
for (uint32_t i = STATIC_PT_COUNT; i < pd_entries_needed; i++) {
|
||||
if (dynamic_pt_count >= MAX_DYNAMIC_PT) {
|
||||
break;
|
||||
}
|
||||
|
||||
/* Allocate a page for this page table from the DMA zone,
|
||||
* since we need it to be accessible before paging is enabled
|
||||
* (i.e., within the first 16 MiB identity map won't help for
|
||||
* the page table itself, but we haven't enabled paging yet so
|
||||
* ALL physical memory is accessible). */
|
||||
phys_addr_t pt_phys = pmm_alloc_page(PMM_ZONE_DMA);
|
||||
if (pt_phys == 0) {
|
||||
pt_phys = pmm_alloc_page(PMM_ZONE_NORMAL);
|
||||
}
|
||||
if (pt_phys == 0) {
|
||||
offset_print(" PAGING: WARNING - could not alloc page table\n");
|
||||
break;
|
||||
}
|
||||
|
||||
uint32_t *pt = (uint32_t *)pt_phys;
|
||||
dynamic_page_tables[dynamic_pt_count++] = pt;
|
||||
|
||||
/* Fill the page table with identity mappings */
|
||||
for (uint32_t j = 0; j < PAGE_ENTRIES; j++) {
|
||||
uint32_t paddr = (i * PAGE_ENTRIES + j) * 4096;
|
||||
pt[j] = paddr | PAGE_PRESENT | PAGE_WRITE;
|
||||
}
|
||||
|
||||
page_directory[i] = pt_phys | PAGE_PRESENT | PAGE_WRITE;
|
||||
}
|
||||
|
||||
if (dynamic_pt_count > 0) {
|
||||
offset_print(" PAGING: identity mapped ");
|
||||
print_hex(pd_entries_needed * 4);
|
||||
offset_print(" PAGING: MiB total using ");
|
||||
print_hex(dynamic_pt_count);
|
||||
offset_print(" PAGING: additional page tables\n");
|
||||
}
|
||||
|
||||
/* 4. Load the page directory into CR3 */
|
||||
__asm__ volatile("mov %0, %%cr3" : : "r"(page_directory) : "memory");
|
||||
|
||||
/* 5. Enable paging by setting bit 31 (PG) of CR0 */
|
||||
uint32_t cr0;
|
||||
__asm__ volatile("mov %%cr0, %0" : "=r"(cr0));
|
||||
cr0 |= 0x80000000;
|
||||
__asm__ volatile("mov %0, %%cr0" : : "r"(cr0) : "memory");
|
||||
|
||||
offset_print(" PAGING: enabled\n");
|
||||
}
|
||||
|
||||
uint32_t paging_get_directory_phys(void) {
|
||||
return (uint32_t)page_directory;
|
||||
}
|
||||
|
||||
uint32_t paging_clone_directory(void) {
|
||||
/* Allocate a new page for the directory */
|
||||
phys_addr_t new_dir_phys = pmm_alloc_page(PMM_ZONE_NORMAL);
|
||||
if (new_dir_phys == 0) {
|
||||
offset_print(" PAGING: cannot allocate page directory\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t *new_dir = (uint32_t *)new_dir_phys;
|
||||
|
||||
/* Copy all entries from the kernel page directory.
|
||||
* This shares the kernel-space mappings (identity map, kernel heap)
|
||||
* with the new process. User-space mappings will be added separately. */
|
||||
memcpy(new_dir, page_directory, 4096);
|
||||
|
||||
return new_dir_phys;
|
||||
}
|
||||
|
||||
uint32_t paging_clone_directory_from(uint32_t src_pd_phys) {
|
||||
uint32_t *src_pd = (uint32_t *)src_pd_phys;
|
||||
|
||||
/* Allocate a new page directory */
|
||||
phys_addr_t new_pd_phys = pmm_alloc_page(PMM_ZONE_NORMAL);
|
||||
if (new_pd_phys == 0) {
|
||||
offset_print(" PAGING: cannot allocate page directory for fork\n");
|
||||
return 0;
|
||||
}
|
||||
uint32_t *new_pd = (uint32_t *)new_pd_phys;
|
||||
|
||||
/* Copy all page directory entries (shares kernel mappings) */
|
||||
memcpy(new_pd, src_pd, 4096);
|
||||
|
||||
/* Deep-copy user-space page tables (those with PAGE_USER set) */
|
||||
for (uint32_t i = 0; i < PAGE_ENTRIES; i++) {
|
||||
if (!(src_pd[i] & PAGE_PRESENT)) continue;
|
||||
if (!(src_pd[i] & PAGE_USER)) continue; /* kernel entry, shared */
|
||||
|
||||
uint32_t *src_pt = (uint32_t *)(src_pd[i] & 0xFFFFF000);
|
||||
|
||||
/* Allocate a new page table */
|
||||
phys_addr_t new_pt_phys = pmm_alloc_page(PMM_ZONE_NORMAL);
|
||||
if (new_pt_phys == 0) {
|
||||
offset_print(" PAGING: fork: cannot allocate page table\n");
|
||||
return 0; /* TODO: free partially allocated pages */
|
||||
}
|
||||
uint32_t *new_pt = (uint32_t *)new_pt_phys;
|
||||
|
||||
/* Deep-copy each page in the page table */
|
||||
for (uint32_t j = 0; j < PAGE_ENTRIES; j++) {
|
||||
if (!(src_pt[j] & PAGE_PRESENT)) {
|
||||
new_pt[j] = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (src_pt[j] & PAGE_USER) {
|
||||
/* User page: allocate new physical page and copy content */
|
||||
phys_addr_t old_phys = src_pt[j] & 0xFFFFF000;
|
||||
phys_addr_t new_phys = pmm_alloc_page(PMM_ZONE_NORMAL);
|
||||
if (new_phys == 0) {
|
||||
offset_print(" PAGING: fork: cannot allocate page\n");
|
||||
return 0;
|
||||
}
|
||||
memcpy((void *)new_phys, (void *)old_phys, 4096);
|
||||
new_pt[j] = new_phys | (src_pt[j] & 0xFFF);
|
||||
} else {
|
||||
/* Kernel page within a user page table: share directly */
|
||||
new_pt[j] = src_pt[j];
|
||||
}
|
||||
}
|
||||
|
||||
new_pd[i] = new_pt_phys | (src_pd[i] & 0xFFF);
|
||||
}
|
||||
|
||||
return new_pd_phys;
|
||||
}
|
||||
|
||||
void paging_map_page_in(uint32_t *pd, uint32_t vaddr, uint32_t paddr, uint32_t flags) {
|
||||
uint32_t pd_idx = PD_INDEX(vaddr);
|
||||
uint32_t pt_idx = PT_INDEX(vaddr);
|
||||
|
||||
uint32_t *pt;
|
||||
if (pd[pd_idx] & PAGE_PRESENT) {
|
||||
pt = (uint32_t *)(pd[pd_idx] & 0xFFFFF000);
|
||||
} else {
|
||||
/* Allocate a new page table */
|
||||
phys_addr_t pt_phys = pmm_alloc_page(PMM_ZONE_NORMAL);
|
||||
if (pt_phys == 0) {
|
||||
offset_print(" PAGING: cannot allocate page table for process\n");
|
||||
return;
|
||||
}
|
||||
memset((void *)pt_phys, 0, 4096);
|
||||
pd[pd_idx] = pt_phys | PAGE_PRESENT | PAGE_WRITE | PAGE_USER;
|
||||
pt = (uint32_t *)pt_phys;
|
||||
}
|
||||
|
||||
pt[pt_idx] = (paddr & 0xFFFFF000) | (flags & 0xFFF);
|
||||
}
|
||||
|
||||
void paging_switch_directory(uint32_t phys_addr) {
|
||||
__asm__ volatile("mov %0, %%cr3" : : "r"(phys_addr) : "memory");
|
||||
}
|
||||
129
src/paging.h
Normal file
129
src/paging.h
Normal file
@@ -0,0 +1,129 @@
|
||||
/**
|
||||
* @file paging.h
|
||||
* @brief Virtual memory paging subsystem.
|
||||
*
|
||||
* Provides page directory and page table management for the x86 two-level
|
||||
* paging scheme (no PAE). Allows mapping and unmapping individual 4 KiB pages,
|
||||
* as well as allocating virtual pages backed by physical memory.
|
||||
*/
|
||||
|
||||
#ifndef PAGING_H
|
||||
#define PAGING_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/** Page table entry flags. */
|
||||
#define PAGE_PRESENT 0x001 /**< Page is present in memory. */
|
||||
#define PAGE_WRITE 0x002 /**< Page is writable. */
|
||||
#define PAGE_USER 0x004 /**< Page is accessible from ring 3. */
|
||||
#define PAGE_WRITETHROUGH 0x008 /**< Write-through caching. */
|
||||
#define PAGE_NOCACHE 0x010 /**< Disable caching for this page. */
|
||||
#define PAGE_ACCESSED 0x020 /**< CPU has read from this page. */
|
||||
#define PAGE_DIRTY 0x040 /**< CPU has written to this page. */
|
||||
#define PAGE_SIZE_4MB 0x080 /**< 4 MiB page (page directory only). */
|
||||
|
||||
/** Number of entries in a page directory or page table. */
|
||||
#define PAGE_ENTRIES 1024
|
||||
|
||||
/** Extract the page directory index from a virtual address. */
|
||||
#define PD_INDEX(vaddr) (((uint32_t)(vaddr) >> 22) & 0x3FF)
|
||||
|
||||
/** Extract the page table index from a virtual address. */
|
||||
#define PT_INDEX(vaddr) (((uint32_t)(vaddr) >> 12) & 0x3FF)
|
||||
|
||||
/**
|
||||
* Initialize the paging subsystem.
|
||||
*
|
||||
* Sets up a page directory, identity-maps all detected physical memory,
|
||||
* and enables paging by writing to CR3 and CR0.
|
||||
*/
|
||||
void init_paging(void);
|
||||
|
||||
/**
|
||||
* Map a virtual address to a physical address with the given flags.
|
||||
*
|
||||
* If no page table exists for the virtual address range, one is allocated
|
||||
* from the PMM automatically.
|
||||
*
|
||||
* @param vaddr Virtual address (must be page-aligned).
|
||||
* @param paddr Physical address (must be page-aligned).
|
||||
* @param flags Page flags (PAGE_PRESENT | PAGE_WRITE | ...).
|
||||
*/
|
||||
void paging_map_page(uint32_t vaddr, uint32_t paddr, uint32_t flags);
|
||||
|
||||
/**
|
||||
* Unmap a virtual address, freeing the mapping but not the physical page.
|
||||
*
|
||||
* @param vaddr Virtual address to unmap (must be page-aligned).
|
||||
*/
|
||||
void paging_unmap_page(uint32_t vaddr);
|
||||
|
||||
/**
|
||||
* Allocate a new virtual page backed by physical memory.
|
||||
*
|
||||
* Finds a free virtual address, allocates a physical page from the PMM,
|
||||
* and creates a mapping.
|
||||
*
|
||||
* @return Virtual address of the allocated page, or NULL on failure.
|
||||
*/
|
||||
void *paging_alloc_page(void);
|
||||
|
||||
/**
|
||||
* Free a virtual page, unmapping it and returning the physical page to the PMM.
|
||||
*
|
||||
* @param vaddr Virtual address of the page to free.
|
||||
*/
|
||||
void paging_free_page(void *vaddr);
|
||||
|
||||
/**
|
||||
* Look up the physical address mapped to a virtual address.
|
||||
*
|
||||
* @param vaddr Virtual address to translate.
|
||||
* @return Physical address, or 0 if the page is not mapped.
|
||||
*/
|
||||
uint32_t paging_get_physical(uint32_t vaddr);
|
||||
|
||||
/**
|
||||
* Get the physical address of the kernel page directory.
|
||||
*
|
||||
* @return Physical address of the page directory.
|
||||
*/
|
||||
uint32_t paging_get_directory_phys(void);
|
||||
|
||||
/**
|
||||
* Clone the kernel page directory for a new process.
|
||||
* Copies all kernel-space entries; user-space entries are empty.
|
||||
*
|
||||
* @return Physical address of the new page directory, or 0 on failure.
|
||||
*/
|
||||
uint32_t paging_clone_directory(void);
|
||||
|
||||
/**
|
||||
* Clone a page directory, deep-copying all user-space pages.
|
||||
* Kernel-space entries are shared (same page tables). User-space page
|
||||
* tables and their physical pages are duplicated so the clone is fully
|
||||
* independent.
|
||||
*
|
||||
* @param src_pd_phys Physical address of the source page directory.
|
||||
* @return Physical address of the new page directory, or 0 on failure.
|
||||
*/
|
||||
uint32_t paging_clone_directory_from(uint32_t src_pd_phys);
|
||||
|
||||
/**
|
||||
* Map a page in a specific page directory (not necessarily the active one).
|
||||
*
|
||||
* @param pd Pointer to the page directory (virtual/identity-mapped address).
|
||||
* @param vaddr Virtual address to map (page-aligned).
|
||||
* @param paddr Physical address to map to (page-aligned).
|
||||
* @param flags Page flags.
|
||||
*/
|
||||
void paging_map_page_in(uint32_t *pd, uint32_t vaddr, uint32_t paddr, uint32_t flags);
|
||||
|
||||
/**
|
||||
* Switch the active page directory.
|
||||
*
|
||||
* @param phys_addr Physical address of the page directory.
|
||||
*/
|
||||
void paging_switch_directory(uint32_t phys_addr);
|
||||
|
||||
#endif /* PAGING_H */
|
||||
95
src/pic.c
Normal file
95
src/pic.c
Normal file
@@ -0,0 +1,95 @@
|
||||
#include "pic.h"
|
||||
#include "port_io.h"
|
||||
|
||||
#define PIC1_COMMAND 0x20
|
||||
#define PIC1_DATA 0x21
|
||||
#define PIC2_COMMAND 0xA0
|
||||
#define PIC2_DATA 0xA1
|
||||
|
||||
#define ICW1_ICW4 0x01 /* ICW4 (not) needed */
|
||||
#define ICW1_SINGLE 0x02 /* Single (cascade) mode */
|
||||
#define ICW1_INTERVAL4 0x04 /* Call address interval 4 (8) */
|
||||
#define ICW1_LEVEL 0x08 /* Level triggered (edge) mode */
|
||||
#define ICW1_INIT 0x10 /* Initialization - required! */
|
||||
|
||||
#define ICW4_8086 0x01 /* 8086/88 (MCS-80/85) mode */
|
||||
#define ICW4_AUTO 0x02 /* Auto (normal) EOI */
|
||||
#define ICW4_BUF_SLAVE 0x08 /* Buffered mode/slave */
|
||||
#define ICW4_BUF_MASTER 0x0C /* Buffered mode/master */
|
||||
#define ICW4_SFNM 0x10 /* Special fully nested (not) */
|
||||
|
||||
#define PIC_EOI 0x20
|
||||
|
||||
void pic_send_eoi(uint8_t irq)
|
||||
{
|
||||
if(irq >= 8)
|
||||
outb(PIC2_COMMAND, PIC_EOI);
|
||||
|
||||
outb(PIC1_COMMAND, PIC_EOI);
|
||||
}
|
||||
|
||||
/*
|
||||
reinitialize the PIC controllers, giving them specified vector offsets
|
||||
rather than 8h and 70h, as configured by default
|
||||
*/
|
||||
#define PIC1_OFFSET 0x20
|
||||
#define PIC2_OFFSET 0x28
|
||||
|
||||
void init_pic(void)
|
||||
{
|
||||
unsigned char a1, a2;
|
||||
|
||||
a1 = inb(PIC1_DATA); // save masks
|
||||
a2 = inb(PIC2_DATA);
|
||||
|
||||
outb(PIC1_COMMAND, ICW1_INIT | ICW1_ICW4); // starts the initialization sequence (in cascade mode)
|
||||
io_wait();
|
||||
outb(PIC2_COMMAND, ICW1_INIT | ICW1_ICW4);
|
||||
io_wait();
|
||||
|
||||
outb(PIC1_DATA, PIC1_OFFSET); // ICW2: Master PIC vector offset
|
||||
io_wait();
|
||||
outb(PIC2_DATA, PIC2_OFFSET); // ICW2: Slave PIC vector offset
|
||||
io_wait();
|
||||
|
||||
outb(PIC1_DATA, 4); // ICW3: tell Master PIC that there is a slave PIC at IRQ2 (0000 0100)
|
||||
io_wait();
|
||||
outb(PIC2_DATA, 2); // ICW3: tell Slave PIC its cascade identity (0000 0010)
|
||||
io_wait();
|
||||
|
||||
outb(PIC1_DATA, ICW4_8086);
|
||||
io_wait();
|
||||
outb(PIC2_DATA, ICW4_8086);
|
||||
io_wait();
|
||||
|
||||
outb(PIC1_DATA, a1); // restore saved masks.
|
||||
outb(PIC2_DATA, a2);
|
||||
}
|
||||
|
||||
void pic_clear_mask(uint8_t irq) {
|
||||
uint16_t port;
|
||||
uint8_t value;
|
||||
|
||||
if(irq < 8) {
|
||||
port = PIC1_DATA;
|
||||
} else {
|
||||
port = PIC2_DATA;
|
||||
irq -= 8;
|
||||
}
|
||||
value = inb(port) & ~(1 << irq);
|
||||
outb(port, value);
|
||||
}
|
||||
|
||||
void pic_set_mask(uint8_t irq) {
|
||||
uint16_t port;
|
||||
uint8_t value;
|
||||
|
||||
if(irq < 8) {
|
||||
port = PIC1_DATA;
|
||||
} else {
|
||||
port = PIC2_DATA;
|
||||
irq -= 8;
|
||||
}
|
||||
value = inb(port) | (1 << irq);
|
||||
outb(port, value);
|
||||
}
|
||||
11
src/pic.h
Normal file
11
src/pic.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef PIC_H
|
||||
#define PIC_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void init_pic(void);
|
||||
void pic_send_eoi(uint8_t irq);
|
||||
void pic_clear_mask(uint8_t irq);
|
||||
void pic_set_mask(uint8_t irq);
|
||||
|
||||
#endif
|
||||
165
src/pmm.c
Normal file
165
src/pmm.c
Normal file
@@ -0,0 +1,165 @@
|
||||
#include "pmm.h"
|
||||
#include "port_io.h"
|
||||
#include <multiboot2.h>
|
||||
|
||||
extern uint32_t _kernel_start;
|
||||
extern uint32_t _kernel_end;
|
||||
|
||||
/* Printing helpers (defined in kernel.c) */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
#define MAX_PHYSICAL_MEMORY 0xFFFFFFFF // 4GB
|
||||
#define TOTAL_FRAMES (MAX_PHYSICAL_MEMORY / PAGE_SIZE + 1)
|
||||
#define FRAMES_PER_INDEX 32
|
||||
#define BITMAP_SIZE ((TOTAL_FRAMES + FRAMES_PER_INDEX - 1) / FRAMES_PER_INDEX)
|
||||
|
||||
static uint32_t memory_bitmap[BITMAP_SIZE];
|
||||
static uint32_t memory_size = 0;
|
||||
// static uint32_t used_frames = 0; // Unused for now
|
||||
|
||||
static void set_frame(phys_addr_t frame_addr) {
|
||||
uint32_t frame = frame_addr / PAGE_SIZE;
|
||||
uint32_t idx = frame / FRAMES_PER_INDEX;
|
||||
uint32_t off = frame % FRAMES_PER_INDEX;
|
||||
memory_bitmap[idx] |= (1 << off);
|
||||
}
|
||||
|
||||
static void clear_frame(phys_addr_t frame_addr) {
|
||||
uint32_t frame = frame_addr / PAGE_SIZE;
|
||||
uint32_t idx = frame / FRAMES_PER_INDEX;
|
||||
uint32_t off = frame % FRAMES_PER_INDEX;
|
||||
memory_bitmap[idx] &= ~(1 << off);
|
||||
}
|
||||
|
||||
static uint32_t first_free_frame(size_t start_frame, size_t end_frame) {
|
||||
for (size_t i = start_frame; i < end_frame; i++) {
|
||||
uint32_t idx = i / FRAMES_PER_INDEX;
|
||||
uint32_t off = i % FRAMES_PER_INDEX;
|
||||
if (!(memory_bitmap[idx] & (1 << off))) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return (uint32_t)-1;
|
||||
}
|
||||
|
||||
void init_pmm(uint32_t multiboot_addr) {
|
||||
struct multiboot_tag *tag;
|
||||
uint32_t addr = multiboot_addr;
|
||||
|
||||
/* Initialize all memory as used (1) initially */
|
||||
for (uint32_t i = 0; i < BITMAP_SIZE; i++) {
|
||||
memory_bitmap[i] = 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
if (addr & 7) {
|
||||
offset_print(" PMM: multiboot alignment error\n");
|
||||
return; // Alignment issue
|
||||
}
|
||||
|
||||
uint32_t size = *(uint32_t *)addr;
|
||||
|
||||
uint32_t regions_found = 0;
|
||||
for (tag = (struct multiboot_tag *)(addr + 8);
|
||||
tag->type != MULTIBOOT_TAG_TYPE_END;
|
||||
tag = (struct multiboot_tag *)((multiboot_uint8_t *)tag + ((tag->size + 7) & ~7))) {
|
||||
|
||||
if (tag->type == MULTIBOOT_TAG_TYPE_BASIC_MEMINFO) {
|
||||
struct multiboot_tag_basic_meminfo *meminfo = (struct multiboot_tag_basic_meminfo *)tag;
|
||||
memory_size = meminfo->mem_upper;
|
||||
offset_print(" PMM: mem_upper = ");
|
||||
print_hex(memory_size);
|
||||
} else if (tag->type == MULTIBOOT_TAG_TYPE_MMAP) {
|
||||
multiboot_memory_map_t *mmap;
|
||||
struct multiboot_tag_mmap *tag_mmap = (struct multiboot_tag_mmap *)tag;
|
||||
|
||||
for (mmap = tag_mmap->entries;
|
||||
(multiboot_uint8_t *)mmap < (multiboot_uint8_t *)tag + tag->size;
|
||||
mmap = (multiboot_memory_map_t *)((unsigned long)mmap + tag_mmap->entry_size)) {
|
||||
|
||||
if (mmap->type == MULTIBOOT_MEMORY_AVAILABLE) {
|
||||
uint64_t start = mmap->addr;
|
||||
uint64_t len = mmap->len;
|
||||
uint64_t end = start + len;
|
||||
|
||||
regions_found++;
|
||||
|
||||
if (start % PAGE_SIZE != 0) {
|
||||
start = (start + PAGE_SIZE) & ~(PAGE_SIZE - 1);
|
||||
}
|
||||
|
||||
for (uint64_t addr_iter = start; addr_iter < end; addr_iter += PAGE_SIZE) {
|
||||
if (addr_iter + PAGE_SIZE <= end && addr_iter < MAX_PHYSICAL_MEMORY) {
|
||||
clear_frame((phys_addr_t)addr_iter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t kstart = (uint32_t)&_kernel_start;
|
||||
uint32_t kend = (uint32_t)&_kernel_end;
|
||||
|
||||
kstart &= ~(PAGE_SIZE - 1);
|
||||
kend = (kend + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
|
||||
|
||||
for (uint32_t i = kstart; i < kend; i += PAGE_SIZE) {
|
||||
set_frame(i);
|
||||
}
|
||||
|
||||
// Mark multiboot structure
|
||||
uint32_t mb_start = multiboot_addr;
|
||||
uint32_t mb_end = multiboot_addr + size;
|
||||
mb_start &= ~(PAGE_SIZE - 1);
|
||||
mb_end = (mb_end + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
|
||||
|
||||
for (uint32_t i = mb_start; i < mb_end; i += PAGE_SIZE) {
|
||||
set_frame(i);
|
||||
}
|
||||
|
||||
// Mark first page as used to avoid returning 0 (NULL)
|
||||
set_frame(0);
|
||||
|
||||
offset_print(" PMM: found ");
|
||||
print_hex(regions_found);
|
||||
offset_print(" PMM: available memory regions\n");
|
||||
}
|
||||
|
||||
phys_addr_t pmm_alloc_page(pmm_zone_t zone) {
|
||||
uint32_t start_frame = 0;
|
||||
uint32_t end_frame = BITMAP_SIZE * FRAMES_PER_INDEX;
|
||||
uint32_t frame = (uint32_t)-1;
|
||||
|
||||
// 16MB boundary is at 16*1024*1024 / 4096 = 4096 frames
|
||||
uint32_t dma_limit = 4096;
|
||||
|
||||
if (zone == PMM_ZONE_DMA) {
|
||||
end_frame = dma_limit;
|
||||
frame = first_free_frame(start_frame, end_frame);
|
||||
} else {
|
||||
start_frame = dma_limit;
|
||||
frame = first_free_frame(start_frame, end_frame);
|
||||
|
||||
// Fallback to DMA if NORMAL failed
|
||||
if (frame == (uint32_t)-1) {
|
||||
frame = first_free_frame(0, dma_limit);
|
||||
}
|
||||
}
|
||||
|
||||
if (frame != (uint32_t)-1) {
|
||||
phys_addr_t addr = frame * PAGE_SIZE;
|
||||
set_frame(addr);
|
||||
return addr;
|
||||
}
|
||||
|
||||
return 0; // OOM
|
||||
}
|
||||
|
||||
void pmm_free_page(phys_addr_t addr) {
|
||||
clear_frame(addr);
|
||||
}
|
||||
|
||||
uint32_t pmm_get_memory_size(void) {
|
||||
return memory_size;
|
||||
}
|
||||
42
src/pmm.h
Normal file
42
src/pmm.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#ifndef PMM_H
|
||||
#define PMM_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define PAGE_SIZE 4096
|
||||
|
||||
typedef uintptr_t phys_addr_t;
|
||||
|
||||
typedef enum {
|
||||
PMM_ZONE_DMA, // < 16 MB
|
||||
PMM_ZONE_NORMAL, // Any other memory
|
||||
PMM_ZONE_HIGHMEM // > 4 GB (if 64-bit or PAE, but we are 32-bit for now) - actually sticking to DMA and NORMAL is enough for 32-bit typically
|
||||
} pmm_zone_t;
|
||||
|
||||
/*
|
||||
* Initialize the physical memory manager.
|
||||
* @param multiboot_addr Address of the multiboot info structure
|
||||
*/
|
||||
void init_pmm(uint32_t multiboot_addr);
|
||||
|
||||
/*
|
||||
* Allocate a physical page.
|
||||
* @param zone The preferred zone to allocate from.
|
||||
* @return Physical address of the allocated page, or 0 if out of memory.
|
||||
*/
|
||||
phys_addr_t pmm_alloc_page(pmm_zone_t zone);
|
||||
|
||||
/**
|
||||
* Free a physical page.
|
||||
* @param addr Physical address of the page to free.
|
||||
*/
|
||||
void pmm_free_page(phys_addr_t addr);
|
||||
|
||||
/**
|
||||
* Get the total detected upper memory in KiB.
|
||||
* @return Upper memory size in KiB as reported by Multiboot.
|
||||
*/
|
||||
uint32_t pmm_get_memory_size(void);
|
||||
|
||||
#endif
|
||||
37
src/port_io.h
Normal file
37
src/port_io.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#ifndef PORT_IO_H
|
||||
#define PORT_IO_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
static inline void outb(uint16_t port, uint8_t val)
|
||||
{
|
||||
asm volatile ( "outb %b0, %w1" : : "a"(val), "Nd"(port) );
|
||||
}
|
||||
|
||||
static inline uint8_t inb(uint16_t port)
|
||||
{
|
||||
uint8_t ret;
|
||||
asm volatile ( "inb %w1, %b0" : "=a"(ret) : "Nd"(port) );
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline void outw(uint16_t port, uint16_t val)
|
||||
{
|
||||
asm volatile ( "outw %w0, %w1" : : "a"(val), "Nd"(port) );
|
||||
}
|
||||
|
||||
static inline uint16_t inw(uint16_t port)
|
||||
{
|
||||
uint16_t ret;
|
||||
asm volatile ( "inw %w1, %w0" : "=a"(ret) : "Nd"(port) );
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline void io_wait(void)
|
||||
{
|
||||
/* Port 0x80 is used for 'checkpoints' during POST. */
|
||||
/* The Linux kernel seems to think it is free for use :-/ */
|
||||
asm volatile ( "outb %%al, $0x80" : : "a"(0) );
|
||||
}
|
||||
|
||||
#endif
|
||||
377
src/process.c
Normal file
377
src/process.c
Normal file
@@ -0,0 +1,377 @@
|
||||
/**
|
||||
* @file process.c
|
||||
* @brief Process management subsystem implementation.
|
||||
*
|
||||
* Manages process creation, context switching, and scheduling.
|
||||
* Each process has its own page directory and kernel stack.
|
||||
* Context switching is done by modifying the interrupt frame registers
|
||||
* on the kernel stack, so the iret restores the next process's state.
|
||||
*/
|
||||
|
||||
#include "process.h"
|
||||
#include "tss.h"
|
||||
#include "paging.h"
|
||||
#include "pmm.h"
|
||||
#include "kmalloc.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Debug print helpers defined in kernel.c */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/** Assembly helper: enter user mode for the first process. */
|
||||
extern void enter_usermode(uint32_t eip, uint32_t esp);
|
||||
|
||||
/** Process table. */
|
||||
static process_t process_table[MAX_PROCESSES];
|
||||
|
||||
/** Currently running process, or NULL if none. */
|
||||
static process_t *current_process = NULL;
|
||||
|
||||
/** Next PID to assign. */
|
||||
static uint32_t next_pid = 1;
|
||||
|
||||
/**
|
||||
* Find a free slot in the process table.
|
||||
*
|
||||
* @return Index of a free slot, or -1 if full.
|
||||
*/
|
||||
static int find_free_slot(void) {
|
||||
for (int i = 0; i < MAX_PROCESSES; i++) {
|
||||
if (process_table[i].state == PROCESS_UNUSED) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void init_process(void) {
|
||||
memset(process_table, 0, sizeof(process_table));
|
||||
current_process = NULL;
|
||||
next_pid = 1;
|
||||
offset_print(" PROCESS: subsystem initialized\n");
|
||||
}
|
||||
|
||||
int32_t process_create(const char *name, const void *code, uint32_t size) {
|
||||
int slot = find_free_slot();
|
||||
if (slot < 0) {
|
||||
offset_print(" PROCESS: no free slots\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
process_t *proc = &process_table[slot];
|
||||
memset(proc, 0, sizeof(process_t));
|
||||
|
||||
proc->pid = next_pid++;
|
||||
proc->state = PROCESS_READY;
|
||||
|
||||
/* Copy name */
|
||||
uint32_t nlen = strlen(name);
|
||||
if (nlen > 31) nlen = 31;
|
||||
memcpy(proc->name, name, nlen);
|
||||
proc->name[nlen] = '\0';
|
||||
|
||||
/* Initialize environment and working directory */
|
||||
env_init(&proc->env);
|
||||
strcpy(proc->cwd, "/");
|
||||
env_set(&proc->env, "CWD", "/");
|
||||
|
||||
/* Allocate kernel stack (full page, not from kmalloc which has header overhead) */
|
||||
void *kstack = paging_alloc_page();
|
||||
if (!kstack) {
|
||||
offset_print(" PROCESS: cannot allocate kernel stack\n");
|
||||
proc->state = PROCESS_UNUSED;
|
||||
return -1;
|
||||
}
|
||||
proc->kernel_stack = (uint32_t)kstack;
|
||||
proc->kernel_stack_top = proc->kernel_stack + 4096;
|
||||
|
||||
/* Clone the kernel page directory */
|
||||
proc->page_directory = paging_clone_directory();
|
||||
if (!proc->page_directory) {
|
||||
offset_print(" PROCESS: cannot clone page directory\n");
|
||||
paging_free_page((void *)proc->kernel_stack);
|
||||
proc->state = PROCESS_UNUSED;
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t *pd = (uint32_t *)proc->page_directory;
|
||||
|
||||
/* Map user code pages */
|
||||
uint32_t code_pages = (size + 4095) / 4096;
|
||||
for (uint32_t i = 0; i < code_pages; i++) {
|
||||
phys_addr_t phys = pmm_alloc_page(PMM_ZONE_NORMAL);
|
||||
if (phys == 0) {
|
||||
offset_print(" PROCESS: cannot allocate code page\n");
|
||||
/* TODO: clean up already allocated pages */
|
||||
proc->state = PROCESS_UNUSED;
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t vaddr = USER_CODE_START + i * 4096;
|
||||
paging_map_page_in(pd, vaddr, phys,
|
||||
PAGE_PRESENT | PAGE_WRITE | PAGE_USER);
|
||||
|
||||
/* Copy code to the physical page (identity-mapped, so phys == virt) */
|
||||
uint32_t offset = i * 4096;
|
||||
uint32_t bytes = size - offset;
|
||||
if (bytes > 4096) bytes = 4096;
|
||||
memcpy((void *)phys, (const uint8_t *)code + offset, bytes);
|
||||
if (bytes < 4096) {
|
||||
memset((void *)(phys + bytes), 0, 4096 - bytes);
|
||||
}
|
||||
}
|
||||
|
||||
/* Map user stack pages */
|
||||
uint32_t stack_base = USER_STACK_TOP - USER_STACK_PAGES * 4096;
|
||||
for (uint32_t i = 0; i < USER_STACK_PAGES; i++) {
|
||||
phys_addr_t phys = pmm_alloc_page(PMM_ZONE_NORMAL);
|
||||
if (phys == 0) {
|
||||
offset_print(" PROCESS: cannot allocate stack page\n");
|
||||
proc->state = PROCESS_UNUSED;
|
||||
return -1;
|
||||
}
|
||||
|
||||
uint32_t vaddr = stack_base + i * 4096;
|
||||
paging_map_page_in(pd, vaddr, phys,
|
||||
PAGE_PRESENT | PAGE_WRITE | PAGE_USER);
|
||||
|
||||
/* Zero the stack page */
|
||||
memset((void *)phys, 0, 4096);
|
||||
}
|
||||
|
||||
proc->user_stack = USER_STACK_TOP;
|
||||
proc->entry_point = USER_CODE_START;
|
||||
|
||||
/* Set up saved registers for the first context switch.
|
||||
* When the scheduler loads these into regs on the stack, the
|
||||
* iret will enter user mode at the entry point. */
|
||||
memset(&proc->saved_regs, 0, sizeof(registers_t));
|
||||
proc->saved_regs.ds = 0x23; /* User data segment */
|
||||
proc->saved_regs.ss = 0x23; /* User stack segment */
|
||||
proc->saved_regs.cs = 0x1B; /* User code segment */
|
||||
proc->saved_regs.eip = USER_CODE_START;
|
||||
proc->saved_regs.useresp = USER_STACK_TOP;
|
||||
proc->saved_regs.eflags = 0x202; /* IF=1 (enable interrupts) */
|
||||
proc->saved_regs.esp = USER_STACK_TOP; /* For pusha's ESP */
|
||||
|
||||
offset_print(" PROCESS: created '");
|
||||
offset_print(proc->name);
|
||||
offset_print("' pid=");
|
||||
print_hex(proc->pid);
|
||||
|
||||
return (int32_t)proc->pid;
|
||||
}
|
||||
|
||||
void schedule_tick(registers_t *regs) {
|
||||
if (!current_process && next_pid <= 1) {
|
||||
return; /* No processes created yet */
|
||||
}
|
||||
|
||||
/* Save current process state */
|
||||
if (current_process) {
|
||||
current_process->saved_regs = *regs;
|
||||
if (current_process->state == PROCESS_RUNNING) {
|
||||
current_process->state = PROCESS_READY;
|
||||
}
|
||||
}
|
||||
|
||||
/* Find next ready process (round-robin) */
|
||||
uint32_t start_idx = 0;
|
||||
if (current_process) {
|
||||
/* Find current process's index in the table */
|
||||
for (int i = 0; i < MAX_PROCESSES; i++) {
|
||||
if (&process_table[i] == current_process) {
|
||||
start_idx = (uint32_t)i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
process_t *next = NULL;
|
||||
for (int i = 1; i <= MAX_PROCESSES; i++) {
|
||||
uint32_t idx = (start_idx + (uint32_t)i) % MAX_PROCESSES;
|
||||
if (process_table[idx].state == PROCESS_READY) {
|
||||
next = &process_table[idx];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!next) {
|
||||
/* No other process ready */
|
||||
if (current_process && current_process->state == PROCESS_READY) {
|
||||
current_process->state = PROCESS_RUNNING;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/* Switch to next process */
|
||||
current_process = next;
|
||||
current_process->state = PROCESS_RUNNING;
|
||||
|
||||
/* Update TSS kernel stack for ring transitions */
|
||||
tss_set_kernel_stack(current_process->kernel_stack_top);
|
||||
|
||||
/* Switch page directory */
|
||||
paging_switch_directory(current_process->page_directory);
|
||||
|
||||
/* Restore next process's registers into the interrupt frame */
|
||||
*regs = current_process->saved_regs;
|
||||
}
|
||||
|
||||
void schedule(void) {
|
||||
/* Trigger a yield via software interrupt.
|
||||
* This is a simplified version for voluntary preemption from kernel code. */
|
||||
__asm__ volatile("int $0x80" : : "a"(5)); /* SYS_YIELD = 5 */
|
||||
}
|
||||
|
||||
void process_exit(int32_t code) {
|
||||
if (!current_process) {
|
||||
offset_print(" PROCESS: exit with no current process\n");
|
||||
return;
|
||||
}
|
||||
|
||||
offset_print(" PROCESS: pid ");
|
||||
print_hex(current_process->pid);
|
||||
offset_print(" PROCESS: exited with code ");
|
||||
print_hex((uint32_t)code);
|
||||
|
||||
current_process->state = PROCESS_ZOMBIE;
|
||||
current_process->exit_code = code;
|
||||
|
||||
/* Wake any process blocked on waitpid for this PID */
|
||||
for (int i = 0; i < MAX_PROCESSES; i++) {
|
||||
if (process_table[i].state == PROCESS_BLOCKED &&
|
||||
process_table[i].waiting_for_pid == current_process->pid) {
|
||||
process_table[i].state = PROCESS_READY;
|
||||
process_table[i].saved_regs.eax = (uint32_t)code;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Find next ready process to switch to */
|
||||
process_t *next = NULL;
|
||||
for (int i = 0; i < MAX_PROCESSES; i++) {
|
||||
if (process_table[i].state == PROCESS_READY) {
|
||||
next = &process_table[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!next) {
|
||||
offset_print(" PROCESS: no processes remaining, halting\n");
|
||||
for (;;) {
|
||||
__asm__ volatile("cli; hlt");
|
||||
}
|
||||
}
|
||||
|
||||
/* Context switch to the next process via assembly stub */
|
||||
current_process = next;
|
||||
next->state = PROCESS_RUNNING;
|
||||
tss_set_kernel_stack(next->kernel_stack_top);
|
||||
paging_switch_directory(next->page_directory);
|
||||
|
||||
extern void process_switch_to_user(registers_t *regs);
|
||||
process_switch_to_user(&next->saved_regs);
|
||||
|
||||
/* Should never reach here */
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
process_t *process_current(void) {
|
||||
return current_process;
|
||||
}
|
||||
|
||||
process_t *process_get(uint32_t pid) {
|
||||
for (int i = 0; i < MAX_PROCESSES; i++) {
|
||||
if (process_table[i].state != PROCESS_UNUSED &&
|
||||
process_table[i].pid == pid) {
|
||||
return &process_table[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int32_t process_fork(registers_t *regs) {
|
||||
if (!current_process) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int slot = find_free_slot();
|
||||
if (slot < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
process_t *child = &process_table[slot];
|
||||
memcpy(child, current_process, sizeof(process_t));
|
||||
|
||||
child->pid = next_pid++;
|
||||
child->state = PROCESS_READY;
|
||||
child->parent_pid = current_process->pid;
|
||||
child->waiting_for_pid = 0;
|
||||
|
||||
/* Allocate a separate kernel stack for the child */
|
||||
void *child_kstack = paging_alloc_page();
|
||||
if (!child_kstack) {
|
||||
child->state = PROCESS_UNUSED;
|
||||
return -1;
|
||||
}
|
||||
child->kernel_stack = (uint32_t)child_kstack;
|
||||
child->kernel_stack_top = child->kernel_stack + 4096;
|
||||
|
||||
/* Deep-clone the parent's page directory (copies all user-space pages) */
|
||||
child->page_directory = paging_clone_directory_from(current_process->page_directory);
|
||||
if (!child->page_directory) {
|
||||
paging_free_page((void *)child->kernel_stack);
|
||||
child->state = PROCESS_UNUSED;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Copy the current syscall registers to the child.
|
||||
* This ensures the child resumes at the same point as the parent
|
||||
* (right after the INT 0x80 instruction). */
|
||||
child->saved_regs = *regs;
|
||||
|
||||
/* Child's return value is 0 (in EAX) */
|
||||
child->saved_regs.eax = 0;
|
||||
|
||||
offset_print(" PROCESS: forked pid ");
|
||||
print_hex(current_process->pid);
|
||||
offset_print(" PROCESS: -> child pid ");
|
||||
print_hex(child->pid);
|
||||
|
||||
/* Parent's return value is child's PID */
|
||||
return (int32_t)child->pid;
|
||||
}
|
||||
|
||||
void process_run_first(void) {
|
||||
/* Find the first ready process */
|
||||
process_t *first = NULL;
|
||||
for (int i = 0; i < MAX_PROCESSES; i++) {
|
||||
if (process_table[i].state == PROCESS_READY) {
|
||||
first = &process_table[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!first) {
|
||||
offset_print(" PROCESS: no process to run\n");
|
||||
return;
|
||||
}
|
||||
|
||||
current_process = first;
|
||||
first->state = PROCESS_RUNNING;
|
||||
|
||||
/* Set up TSS for this process */
|
||||
tss_set_kernel_stack(first->kernel_stack_top);
|
||||
|
||||
/* Switch to the process's page directory */
|
||||
paging_switch_directory(first->page_directory);
|
||||
|
||||
offset_print(" PROCESS: entering user mode for '");
|
||||
offset_print(first->name);
|
||||
offset_print("'\n");
|
||||
|
||||
/* Jump to user mode - does not return */
|
||||
enter_usermode(first->entry_point, first->user_stack);
|
||||
}
|
||||
133
src/process.h
Normal file
133
src/process.h
Normal file
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* @file process.h
|
||||
* @brief Process management subsystem.
|
||||
*
|
||||
* Manages process creation, scheduling, and context switching.
|
||||
* Supports both kernel-mode and user-mode (Ring 3) processes.
|
||||
*/
|
||||
|
||||
#ifndef PROCESS_H
|
||||
#define PROCESS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include "isr.h"
|
||||
#include "env.h"
|
||||
|
||||
/** Maximum number of concurrent processes. */
|
||||
#define MAX_PROCESSES 64
|
||||
|
||||
/** Per-process kernel stack size (4 KiB). */
|
||||
#define KERNEL_STACK_SIZE 4096
|
||||
|
||||
/** User-mode stack virtual address (top of user space). */
|
||||
#define USER_STACK_TOP 0xBFFFF000
|
||||
/** User-mode stack size (8 KiB = 2 pages). */
|
||||
#define USER_STACK_PAGES 2
|
||||
|
||||
/** User-mode code start virtual address. */
|
||||
#define USER_CODE_START 0x08048000
|
||||
|
||||
/** Process states. */
|
||||
typedef enum {
|
||||
PROCESS_UNUSED = 0, /**< Slot is free. */
|
||||
PROCESS_READY, /**< Ready to run. */
|
||||
PROCESS_RUNNING, /**< Currently executing. */
|
||||
PROCESS_BLOCKED, /**< Waiting for I/O or event. */
|
||||
PROCESS_ZOMBIE, /**< Finished, waiting for parent to reap. */
|
||||
} process_state_t;
|
||||
|
||||
/**
|
||||
* Saved CPU context for context switching.
|
||||
* Uses the full interrupt frame (registers_t from isr.h) so that
|
||||
* saving/restoring context works directly with the ISR stub's stack layout.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Process control block (PCB).
|
||||
*/
|
||||
typedef struct process {
|
||||
uint32_t pid; /**< Process ID. */
|
||||
process_state_t state; /**< Current state. */
|
||||
registers_t saved_regs; /**< Saved interrupt frame for context switch. */
|
||||
uint32_t kernel_stack; /**< Base of kernel stack (virtual). */
|
||||
uint32_t kernel_stack_top; /**< Kernel stack top for TSS. */
|
||||
uint32_t page_directory; /**< Physical address of page directory. */
|
||||
uint32_t user_stack; /**< Virtual address of user stack top. */
|
||||
uint32_t entry_point; /**< User-mode entry point. */
|
||||
int32_t exit_code; /**< Exit code (if ZOMBIE). */
|
||||
uint32_t parent_pid; /**< Parent process ID. */
|
||||
uint32_t waiting_for_pid; /**< PID we are blocked waiting for (if BLOCKED). */
|
||||
char name[32]; /**< Process name (for debugging). */
|
||||
char cwd[128]; /**< Current working directory. */
|
||||
env_block_t env; /**< Per-process environment variables. */
|
||||
} process_t;
|
||||
|
||||
/**
|
||||
* Initialize the process subsystem.
|
||||
* Must be called after paging and kmalloc are initialized.
|
||||
*/
|
||||
void init_process(void);
|
||||
|
||||
/**
|
||||
* Create a new user-mode process from a memory image.
|
||||
*
|
||||
* @param name Process name (for debugging).
|
||||
* @param code Pointer to the code to load.
|
||||
* @param size Size of the code in bytes.
|
||||
* @return PID of the new process, or -1 on failure.
|
||||
*/
|
||||
int32_t process_create(const char *name, const void *code, uint32_t size);
|
||||
|
||||
/**
|
||||
* Yield the current process to the scheduler.
|
||||
* Called from timer interrupt or voluntarily via SYS_YIELD.
|
||||
* Modifies the registers on the stack to switch context.
|
||||
*
|
||||
* @param regs Pointer to the interrupt frame registers on the kernel stack.
|
||||
*/
|
||||
void schedule_tick(registers_t *regs);
|
||||
|
||||
/**
|
||||
* Voluntary yield wrapper (triggers schedule via current context).
|
||||
*/
|
||||
void schedule(void);
|
||||
|
||||
/**
|
||||
* Exit the current process with the given exit code.
|
||||
*
|
||||
* @param code Exit code.
|
||||
*/
|
||||
void process_exit(int32_t code);
|
||||
|
||||
/**
|
||||
* Get the currently running process.
|
||||
*
|
||||
* @return Pointer to the current process PCB, or NULL if none.
|
||||
*/
|
||||
process_t *process_current(void);
|
||||
|
||||
/**
|
||||
* Get a process by PID.
|
||||
*
|
||||
* @param pid Process ID.
|
||||
* @return Pointer to the process PCB, or NULL if not found.
|
||||
*/
|
||||
process_t *process_get(uint32_t pid);
|
||||
|
||||
/**
|
||||
* Fork the current process.
|
||||
* Clones the current process's address space and register state.
|
||||
*
|
||||
* @param regs Pointer to the current interrupt frame (syscall registers).
|
||||
* @return PID of the child in the parent, 0 in the child, -1 on error.
|
||||
*/
|
||||
int32_t process_fork(registers_t *regs);
|
||||
|
||||
/**
|
||||
* Start the first user-mode process. Does not return if a process is ready.
|
||||
* Should be called after creating at least one process.
|
||||
*/
|
||||
void process_run_first(void);
|
||||
|
||||
#endif /* PROCESS_H */
|
||||
172
src/string.c
Normal file
172
src/string.c
Normal file
@@ -0,0 +1,172 @@
|
||||
/**
|
||||
* @file string.c
|
||||
* @brief Minimal C string/memory functions for the freestanding kernel.
|
||||
*
|
||||
* These implementations replace the standard library versions since the
|
||||
* kernel is compiled with -ffreestanding and does not link against libc.
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
* Fill a region of memory with a byte value.
|
||||
*
|
||||
* @param s Destination pointer.
|
||||
* @param c Byte value to fill (only low 8 bits used).
|
||||
* @param n Number of bytes to fill.
|
||||
* @return The destination pointer.
|
||||
*/
|
||||
void *memset(void *s, int c, size_t n) {
|
||||
unsigned char *p = (unsigned char *)s;
|
||||
while (n--) {
|
||||
*p++ = (unsigned char)c;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy a region of memory (non-overlapping).
|
||||
*
|
||||
* @param dest Destination pointer.
|
||||
* @param src Source pointer.
|
||||
* @param n Number of bytes to copy.
|
||||
* @return The destination pointer.
|
||||
*/
|
||||
void *memcpy(void *dest, const void *src, size_t n) {
|
||||
unsigned char *d = (unsigned char *)dest;
|
||||
const unsigned char *s = (const unsigned char *)src;
|
||||
while (n--) {
|
||||
*d++ = *s++;
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy a region of memory, handling overlaps correctly.
|
||||
*
|
||||
* @param dest Destination pointer.
|
||||
* @param src Source pointer.
|
||||
* @param n Number of bytes to copy.
|
||||
* @return The destination pointer.
|
||||
*/
|
||||
void *memmove(void *dest, const void *src, size_t n) {
|
||||
unsigned char *d = (unsigned char *)dest;
|
||||
const unsigned char *s = (const unsigned char *)src;
|
||||
|
||||
if (d < s) {
|
||||
while (n--) {
|
||||
*d++ = *s++;
|
||||
}
|
||||
} else {
|
||||
d += n;
|
||||
s += n;
|
||||
while (n--) {
|
||||
*--d = *--s;
|
||||
}
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two regions of memory.
|
||||
*
|
||||
* @param s1 First memory region.
|
||||
* @param s2 Second memory region.
|
||||
* @param n Number of bytes to compare.
|
||||
* @return 0 if equal, negative if s1 < s2, positive if s1 > s2.
|
||||
*/
|
||||
int memcmp(const void *s1, const void *s2, size_t n) {
|
||||
const unsigned char *a = (const unsigned char *)s1;
|
||||
const unsigned char *b = (const unsigned char *)s2;
|
||||
while (n--) {
|
||||
if (*a != *b) {
|
||||
return *a - *b;
|
||||
}
|
||||
a++;
|
||||
b++;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the length of a null-terminated string.
|
||||
*
|
||||
* @param s The string.
|
||||
* @return Number of characters before the null terminator.
|
||||
*/
|
||||
size_t strlen(const char *s) {
|
||||
size_t len = 0;
|
||||
while (*s++) {
|
||||
len++;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare two null-terminated strings.
|
||||
*
|
||||
* @param s1 First string.
|
||||
* @param s2 Second string.
|
||||
* @return 0 if equal, negative if s1 < s2, positive if s1 > s2.
|
||||
*/
|
||||
int strcmp(const char *s1, const char *s2) {
|
||||
while (*s1 && *s1 == *s2) {
|
||||
s1++;
|
||||
s2++;
|
||||
}
|
||||
return *(unsigned char *)s1 - *(unsigned char *)s2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compare at most n characters of two strings.
|
||||
*
|
||||
* @param s1 First string.
|
||||
* @param s2 Second string.
|
||||
* @param n Maximum number of characters to compare.
|
||||
* @return 0 if equal, negative if s1 < s2, positive if s1 > s2.
|
||||
*/
|
||||
int strncmp(const char *s1, const char *s2, size_t n) {
|
||||
while (n && *s1 && *s1 == *s2) {
|
||||
s1++;
|
||||
s2++;
|
||||
n--;
|
||||
}
|
||||
if (n == 0) {
|
||||
return 0;
|
||||
}
|
||||
return *(unsigned char *)s1 - *(unsigned char *)s2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy a null-terminated string.
|
||||
*
|
||||
* @param dest Destination buffer (must be large enough).
|
||||
* @param src Source string.
|
||||
* @return The destination pointer.
|
||||
*/
|
||||
char *strcpy(char *dest, const char *src) {
|
||||
char *d = dest;
|
||||
while ((*d++ = *src++))
|
||||
;
|
||||
return dest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy at most n characters of a string, null-padding if shorter.
|
||||
*
|
||||
* @param dest Destination buffer.
|
||||
* @param src Source string.
|
||||
* @param n Maximum characters to copy.
|
||||
* @return The destination pointer.
|
||||
*/
|
||||
char *strncpy(char *dest, const char *src, size_t n) {
|
||||
char *d = dest;
|
||||
while (n && (*d++ = *src++)) {
|
||||
n--;
|
||||
}
|
||||
while (n--) {
|
||||
*d++ = '\0';
|
||||
}
|
||||
return dest;
|
||||
}
|
||||
572
src/syscall.c
Normal file
572
src/syscall.c
Normal file
@@ -0,0 +1,572 @@
|
||||
/**
|
||||
* @file syscall.c
|
||||
* @brief System call handler implementation.
|
||||
*
|
||||
* Dispatches INT 0x80 system calls to the appropriate kernel function.
|
||||
* System call number is in EAX, arguments in EBX, ECX, EDX, ESI, EDI.
|
||||
* Return value is placed in EAX.
|
||||
*/
|
||||
|
||||
#include "syscall.h"
|
||||
#include "process.h"
|
||||
#include "env.h"
|
||||
#include "port_io.h"
|
||||
#include "vga.h"
|
||||
#include "vfs.h"
|
||||
#include "keyboard.h"
|
||||
#include "cpio.h"
|
||||
#include "paging.h"
|
||||
#include "pmm.h"
|
||||
#include "tcp.h"
|
||||
#include "udp.h"
|
||||
#include "graphics.h"
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
/** Magic return value indicating the syscall blocked and switched processes.
|
||||
* syscall_handler must NOT overwrite regs->eax in this case. */
|
||||
#define SYSCALL_SWITCHED 0x7FFFFFFF
|
||||
|
||||
/* Debug print helpers defined in kernel.c */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/** IDT gate setup (from idt.c) */
|
||||
extern void set_idt_gate_from_c(uint8_t num, uint32_t base, uint16_t sel, uint8_t flags);
|
||||
|
||||
/** INT 0x80 assembly stub */
|
||||
extern void isr128(void);
|
||||
|
||||
/**
|
||||
* Handle SYS_EXIT: terminate the current process.
|
||||
*/
|
||||
static int32_t sys_exit(registers_t *regs) {
|
||||
process_exit((int32_t)regs->ebx);
|
||||
/* Never returns */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle SYS_WRITE: write bytes to a file descriptor.
|
||||
* Currently only supports fd=1 (stdout) -> debug port + VGA.
|
||||
*/
|
||||
static int32_t sys_write(registers_t *regs) {
|
||||
int fd = (int)regs->ebx;
|
||||
const char *buf = (const char *)regs->ecx;
|
||||
uint32_t len = regs->edx;
|
||||
|
||||
if (fd == 1 || fd == 2) {
|
||||
/* stdout or stderr: write to debug port and VGA */
|
||||
for (uint32_t i = 0; i < len; i++) {
|
||||
outb(0xE9, buf[i]);
|
||||
vga_putchar(buf[i]);
|
||||
}
|
||||
return (int32_t)len;
|
||||
}
|
||||
|
||||
/* VFS file descriptors (fd >= 3) */
|
||||
if (fd >= 3) {
|
||||
return vfs_write(fd, buf, len);
|
||||
}
|
||||
|
||||
return -1; /* Invalid fd */
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle SYS_READ: read bytes from a file descriptor.
|
||||
* fd=0 (stdin) reads from the keyboard buffer (non-blocking).
|
||||
* Returns 0 if no data available; caller should yield and retry.
|
||||
*/
|
||||
static int32_t sys_read(registers_t *regs) {
|
||||
int fd = (int)regs->ebx;
|
||||
char *buf = (char *)regs->ecx;
|
||||
uint32_t len = regs->edx;
|
||||
|
||||
if (fd == 0) {
|
||||
/* stdin: non-blocking read from keyboard */
|
||||
if (keyboard_has_data()) {
|
||||
uint32_t n = keyboard_read(buf, len);
|
||||
return (int32_t)n;
|
||||
}
|
||||
return 0; /* No data available */
|
||||
}
|
||||
|
||||
/* VFS file descriptors (fd >= 3) */
|
||||
if (fd >= 3) {
|
||||
return vfs_read(fd, buf, len);
|
||||
}
|
||||
|
||||
return -1; /* Invalid fd */
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle SYS_FORK: fork the current process.
|
||||
*/
|
||||
static int32_t sys_fork(registers_t *regs) {
|
||||
return process_fork(regs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle SYS_GETPID: return the current process ID.
|
||||
*/
|
||||
static int32_t sys_getpid(registers_t *regs) {
|
||||
(void)regs;
|
||||
process_t *cur = process_current();
|
||||
return cur ? (int32_t)cur->pid : -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle SYS_YIELD: voluntarily yield the CPU.
|
||||
* Calls schedule_tick directly to potentially switch to another process.
|
||||
*/
|
||||
static int32_t sys_yield(registers_t *regs) {
|
||||
schedule_tick(regs);
|
||||
return SYSCALL_SWITCHED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle SYS_WAITPID: wait for a child to exit.
|
||||
*
|
||||
* If the child is already a zombie, reaps immediately and returns the code.
|
||||
* Otherwise, blocks the current process and switches to the next one.
|
||||
* When the child exits, process_exit() will unblock the waiting parent
|
||||
* and set its saved_regs.eax to the exit code.
|
||||
*/
|
||||
static int32_t sys_waitpid(registers_t *regs) {
|
||||
uint32_t pid = regs->ebx;
|
||||
process_t *child = process_get(pid);
|
||||
if (!child) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* If child already exited, reap immediately */
|
||||
if (child->state == PROCESS_ZOMBIE) {
|
||||
int32_t code = child->exit_code;
|
||||
child->state = PROCESS_UNUSED;
|
||||
return code;
|
||||
}
|
||||
|
||||
/* Block the current process until the child exits */
|
||||
process_t *cur = process_current();
|
||||
cur->state = PROCESS_BLOCKED;
|
||||
cur->waiting_for_pid = pid;
|
||||
|
||||
/* Save the current syscall registers so we resume here when unblocked.
|
||||
* The return value (eax) will be set by process_exit when the child dies. */
|
||||
cur->saved_regs = *regs;
|
||||
|
||||
/* Schedule the next process. This modifies *regs to the next process's
|
||||
* saved state, so when the ISR stub does iret, it enters the next process. */
|
||||
schedule_tick(regs);
|
||||
|
||||
/* Tell syscall_handler not to overwrite regs->eax, since regs now
|
||||
* points to the next process's registers on the kernel stack. */
|
||||
return SYSCALL_SWITCHED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle SYS_EXEC: replace the current process image with a new program.
|
||||
* EBX = path to binary (C string), e.g. "hello-world".
|
||||
* Loads the binary from the initrd and replaces the current process's
|
||||
* code and stack. Does not return on success.
|
||||
*/
|
||||
static int32_t sys_exec(registers_t *regs) {
|
||||
const char *path = (const char *)regs->ebx;
|
||||
if (!path) return -1;
|
||||
|
||||
process_t *cur = process_current();
|
||||
if (!cur) return -1;
|
||||
|
||||
/* Look up the binary in the initrd */
|
||||
cpio_entry_t entry;
|
||||
if (cpio_find(path, &entry) != 0) {
|
||||
return -1; /* Not found */
|
||||
}
|
||||
|
||||
uint32_t *pd = (uint32_t *)cur->page_directory;
|
||||
|
||||
/* Unmap and free old user code pages (0x08048000 region).
|
||||
* We don't know exactly how many pages were mapped, so scan a
|
||||
* reasonable range. */
|
||||
for (uint32_t vaddr = USER_CODE_START;
|
||||
vaddr < USER_CODE_START + 0x100000; /* up to 1 MiB of code */
|
||||
vaddr += 4096) {
|
||||
uint32_t pd_idx = vaddr >> 22;
|
||||
uint32_t pt_idx = (vaddr >> 12) & 0x3FF;
|
||||
if (!(pd[pd_idx] & 0x001)) break; /* No page table */
|
||||
uint32_t *pt = (uint32_t *)(pd[pd_idx] & 0xFFFFF000);
|
||||
if (!(pt[pt_idx] & 0x001)) break; /* No page */
|
||||
phys_addr_t old_phys = pt[pt_idx] & 0xFFFFF000;
|
||||
pt[pt_idx] = 0;
|
||||
pmm_free_page(old_phys);
|
||||
}
|
||||
|
||||
/* Map new code pages */
|
||||
uint32_t code_pages = (entry.datasize + 4095) / 4096;
|
||||
for (uint32_t i = 0; i < code_pages; i++) {
|
||||
phys_addr_t phys = pmm_alloc_page(PMM_ZONE_NORMAL);
|
||||
if (phys == 0) return -1;
|
||||
|
||||
uint32_t vaddr = USER_CODE_START + i * 4096;
|
||||
paging_map_page_in(pd, vaddr, phys,
|
||||
PAGE_PRESENT | PAGE_WRITE | PAGE_USER);
|
||||
|
||||
uint32_t offset = i * 4096;
|
||||
uint32_t bytes = entry.datasize - offset;
|
||||
if (bytes > 4096) bytes = 4096;
|
||||
memcpy((void *)phys, (const uint8_t *)entry.data + offset, bytes);
|
||||
if (bytes < 4096) {
|
||||
memset((void *)(phys + bytes), 0, 4096 - bytes);
|
||||
}
|
||||
}
|
||||
|
||||
/* Zero the user stack pages (reuse existing stack mappings) */
|
||||
uint32_t stack_base = USER_STACK_TOP - USER_STACK_PAGES * 4096;
|
||||
for (uint32_t i = 0; i < USER_STACK_PAGES; i++) {
|
||||
uint32_t vaddr = stack_base + i * 4096;
|
||||
uint32_t pd_idx = vaddr >> 22;
|
||||
uint32_t pt_idx = (vaddr >> 12) & 0x3FF;
|
||||
if ((pd[pd_idx] & 0x001)) {
|
||||
uint32_t *pt = (uint32_t *)(pd[pd_idx] & 0xFFFFF000);
|
||||
if ((pt[pt_idx] & 0x001)) {
|
||||
phys_addr_t phys = pt[pt_idx] & 0xFFFFF000;
|
||||
memset((void *)phys, 0, 4096);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Flush TLB */
|
||||
paging_switch_directory(cur->page_directory);
|
||||
|
||||
/* Update process name */
|
||||
uint32_t nlen = strlen(path);
|
||||
if (nlen > 31) nlen = 31;
|
||||
memcpy(cur->name, path, nlen);
|
||||
cur->name[nlen] = '\0';
|
||||
|
||||
/* Set up registers for the new program */
|
||||
regs->eip = USER_CODE_START;
|
||||
regs->useresp = USER_STACK_TOP;
|
||||
regs->esp = USER_STACK_TOP;
|
||||
regs->eax = 0;
|
||||
regs->ebx = 0;
|
||||
regs->ecx = 0;
|
||||
regs->edx = 0;
|
||||
regs->esi = 0;
|
||||
regs->edi = 0;
|
||||
regs->ebp = 0;
|
||||
regs->cs = 0x1B;
|
||||
regs->ds = 0x23;
|
||||
regs->ss = 0x23;
|
||||
regs->eflags = 0x202; /* IF=1 */
|
||||
|
||||
/* Return SYSCALL_SWITCHED so syscall_handler doesn't overwrite regs */
|
||||
return SYSCALL_SWITCHED;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle SYS_GETENV: get an environment variable.
|
||||
* EBX = key pointer, ECX = value buffer pointer, EDX = buffer size.
|
||||
* Returns length of value, or -1 if not found.
|
||||
*/
|
||||
static int32_t sys_getenv(registers_t *regs) {
|
||||
const char *key = (const char *)regs->ebx;
|
||||
char *buf = (char *)regs->ecx;
|
||||
uint32_t bufsize = regs->edx;
|
||||
|
||||
process_t *cur = process_current();
|
||||
if (!cur) return -1;
|
||||
|
||||
return env_get(&cur->env, key, buf, bufsize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle SYS_SETENV: set an environment variable.
|
||||
* EBX = key pointer, ECX = value pointer (NULL to unset).
|
||||
* Returns 0 on success, -1 on error.
|
||||
*/
|
||||
static int32_t sys_setenv(registers_t *regs) {
|
||||
const char *key = (const char *)regs->ebx;
|
||||
const char *value = (const char *)regs->ecx;
|
||||
|
||||
process_t *cur = process_current();
|
||||
if (!cur) return -1;
|
||||
|
||||
return env_set(&cur->env, key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle SYS_OPEN: open a file by path.
|
||||
* EBX = path string, ECX = flags.
|
||||
* Returns file descriptor (>= 3) on success, -1 on failure.
|
||||
*/
|
||||
static int32_t sys_open(registers_t *regs) {
|
||||
const char *path = (const char *)regs->ebx;
|
||||
uint32_t flags = regs->ecx;
|
||||
return (int32_t)vfs_open(path, flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle SYS_CLOSE: close a file descriptor.
|
||||
* EBX = fd.
|
||||
* Returns 0 on success, -1 on failure.
|
||||
*/
|
||||
static int32_t sys_close(registers_t *regs) {
|
||||
int fd = (int)regs->ebx;
|
||||
if (fd < 3) return -1; /* Don't close stdin/stdout/stderr */
|
||||
vfs_close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle SYS_READDIR: read a directory entry.
|
||||
* EBX = path, ECX = index, EDX = name buffer (128 bytes min).
|
||||
* Returns entry type (VFS_FILE=1, VFS_DIRECTORY=2, ...) on success, -1 at end.
|
||||
*/
|
||||
static int32_t sys_readdir(registers_t *regs) {
|
||||
const char *path = (const char *)regs->ebx;
|
||||
uint32_t idx = regs->ecx;
|
||||
char *name_buf = (char *)regs->edx;
|
||||
|
||||
vfs_dirent_t entry;
|
||||
if (vfs_readdir(path, idx, &entry) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Copy entry name to user buffer */
|
||||
uint32_t len = strlen(entry.name);
|
||||
if (len >= 128) len = 127;
|
||||
memcpy(name_buf, entry.name, len);
|
||||
name_buf[len] = '\0';
|
||||
|
||||
return (int32_t)entry.type;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Networking system calls
|
||||
* ================================================================ */
|
||||
|
||||
/** Socket type constants (kernel side). */
|
||||
#define SOCK_TYPE_TCP 0
|
||||
#define SOCK_TYPE_UDP 1
|
||||
|
||||
/** Per-process socket tracking (simple: global table). */
|
||||
#define MAX_USER_SOCKETS 16
|
||||
static struct {
|
||||
uint8_t active;
|
||||
uint8_t type; /* SOCK_TYPE_TCP or SOCK_TYPE_UDP */
|
||||
int kern_sockfd; /* Kernel-side socket index */
|
||||
} user_sockets[MAX_USER_SOCKETS];
|
||||
|
||||
/**
|
||||
* Handle SYS_SOCKET: create a network socket.
|
||||
* EBX = type (0=TCP, 1=UDP).
|
||||
* Returns user sockfd (>= 0) or -1.
|
||||
*/
|
||||
static int32_t sys_socket(registers_t *regs) {
|
||||
uint32_t type = regs->ebx;
|
||||
int kern_fd;
|
||||
|
||||
if (type == SOCK_TYPE_TCP) {
|
||||
kern_fd = tcp_socket_create();
|
||||
} else if (type == SOCK_TYPE_UDP) {
|
||||
kern_fd = udp_socket_create();
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (kern_fd < 0) return -1;
|
||||
|
||||
/* Find a free user socket slot */
|
||||
for (int i = 0; i < MAX_USER_SOCKETS; i++) {
|
||||
if (!user_sockets[i].active) {
|
||||
user_sockets[i].active = 1;
|
||||
user_sockets[i].type = (uint8_t)type;
|
||||
user_sockets[i].kern_sockfd = kern_fd;
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
/* No free slots — close the kernel socket */
|
||||
if (type == SOCK_TYPE_TCP) tcp_close(kern_fd);
|
||||
else udp_close(kern_fd);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle SYS_CONNECT: connect a TCP socket to a remote host.
|
||||
* EBX = user sockfd, ECX = remote IP (host byte order), EDX = remote port.
|
||||
* Returns 0 on success (SYN sent), -1 on failure.
|
||||
*/
|
||||
static int32_t sys_connect(registers_t *regs) {
|
||||
int ufd = (int)regs->ebx;
|
||||
uint32_t remote_ip = regs->ecx;
|
||||
uint16_t remote_port = (uint16_t)regs->edx;
|
||||
|
||||
if (ufd < 0 || ufd >= MAX_USER_SOCKETS || !user_sockets[ufd].active)
|
||||
return -1;
|
||||
if (user_sockets[ufd].type != SOCK_TYPE_TCP)
|
||||
return -1;
|
||||
|
||||
return tcp_connect(user_sockets[ufd].kern_sockfd, remote_ip, remote_port);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle SYS_SEND: send data on a socket.
|
||||
* EBX = user sockfd, ECX = buffer pointer, EDX = length.
|
||||
* Returns bytes sent or -1.
|
||||
*/
|
||||
static int32_t sys_send(registers_t *regs) {
|
||||
int ufd = (int)regs->ebx;
|
||||
const void *buf = (const void *)regs->ecx;
|
||||
uint32_t len = regs->edx;
|
||||
|
||||
if (ufd < 0 || ufd >= MAX_USER_SOCKETS || !user_sockets[ufd].active)
|
||||
return -1;
|
||||
|
||||
if (user_sockets[ufd].type == SOCK_TYPE_TCP)
|
||||
return tcp_send(user_sockets[ufd].kern_sockfd, buf, len);
|
||||
else
|
||||
return -1; /* UDP sendto requires address — not supported via SYS_SEND */
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle SYS_RECV: receive data from a socket (non-blocking).
|
||||
* EBX = user sockfd, ECX = buffer pointer, EDX = buffer size.
|
||||
* Returns bytes received, 0 if no data, -1 on error/closed.
|
||||
*/
|
||||
static int32_t sys_recv(registers_t *regs) {
|
||||
int ufd = (int)regs->ebx;
|
||||
void *buf = (void *)regs->ecx;
|
||||
uint32_t bufsize = regs->edx;
|
||||
|
||||
if (ufd < 0 || ufd >= MAX_USER_SOCKETS || !user_sockets[ufd].active)
|
||||
return -1;
|
||||
|
||||
if (user_sockets[ufd].type == SOCK_TYPE_TCP)
|
||||
return tcp_recv(user_sockets[ufd].kern_sockfd, buf, bufsize);
|
||||
else
|
||||
return -1; /* UDP recvfrom requires address — not supported via SYS_RECV */
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle SYS_SOCKSTATE: get the state of a TCP socket.
|
||||
* EBX = user sockfd.
|
||||
* Returns TCP state constant, or -1 for invalid/UDP sockets.
|
||||
*/
|
||||
static int32_t sys_sockstate(registers_t *regs) {
|
||||
int ufd = (int)regs->ebx;
|
||||
|
||||
if (ufd < 0 || ufd >= MAX_USER_SOCKETS || !user_sockets[ufd].active)
|
||||
return -1;
|
||||
|
||||
if (user_sockets[ufd].type == SOCK_TYPE_TCP)
|
||||
return (int32_t)tcp_get_state(user_sockets[ufd].kern_sockfd);
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle SYS_GFX: graphics operations.
|
||||
* EBX = sub-command, ECX = arg1, EDX = arg2.
|
||||
*/
|
||||
static int32_t sys_gfx(registers_t *regs) {
|
||||
uint32_t cmd = regs->ebx;
|
||||
uint32_t arg1 = regs->ecx;
|
||||
uint32_t arg2 = regs->edx;
|
||||
|
||||
switch (cmd) {
|
||||
case GFX_CMD_ENTER:
|
||||
graphics_enter();
|
||||
return 0;
|
||||
|
||||
case GFX_CMD_LEAVE:
|
||||
graphics_leave();
|
||||
return 0;
|
||||
|
||||
case GFX_CMD_PIXEL: {
|
||||
int x = (int)(arg1 & 0xFFFF);
|
||||
int y = (int)(arg1 >> 16);
|
||||
gfx_pixel(x, y, (uint8_t)arg2);
|
||||
return 0;
|
||||
}
|
||||
|
||||
case GFX_CMD_CLEAR:
|
||||
gfx_clear((uint8_t)arg1);
|
||||
return 0;
|
||||
|
||||
case GFX_CMD_FILL_RECT: {
|
||||
const gfx_rect_t *r = (const gfx_rect_t *)arg1;
|
||||
gfx_fill_rect((int)r->x, (int)r->y, (int)r->w, (int)r->h,
|
||||
(uint8_t)r->color);
|
||||
return 0;
|
||||
}
|
||||
|
||||
case GFX_CMD_LINE: {
|
||||
const gfx_line_t *l = (const gfx_line_t *)arg1;
|
||||
gfx_draw_line((int)l->x1, (int)l->y1, (int)l->x2, (int)l->y2,
|
||||
(uint8_t)l->color);
|
||||
return 0;
|
||||
}
|
||||
|
||||
case GFX_CMD_CIRCLE: {
|
||||
const gfx_circle_t *c = (const gfx_circle_t *)arg1;
|
||||
gfx_fill_circle((int)c->cx, (int)c->cy, (int)c->r,
|
||||
(uint8_t)c->color);
|
||||
return 0;
|
||||
}
|
||||
|
||||
case GFX_CMD_GET_INFO:
|
||||
return (int32_t)(GFX_WIDTH | (GFX_HEIGHT << 16));
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/** System call dispatch table. */
|
||||
typedef int32_t (*syscall_fn)(registers_t *);
|
||||
static syscall_fn syscall_table[NUM_SYSCALLS] = {
|
||||
[SYS_EXIT] = sys_exit,
|
||||
[SYS_WRITE] = sys_write,
|
||||
[SYS_READ] = sys_read,
|
||||
[SYS_FORK] = sys_fork,
|
||||
[SYS_GETPID] = sys_getpid,
|
||||
[SYS_YIELD] = sys_yield,
|
||||
[SYS_WAITPID] = sys_waitpid,
|
||||
[SYS_EXEC] = sys_exec,
|
||||
[SYS_GETENV] = sys_getenv,
|
||||
[SYS_SETENV] = sys_setenv,
|
||||
[SYS_READDIR] = sys_readdir,
|
||||
[SYS_OPEN] = sys_open,
|
||||
[SYS_CLOSE] = sys_close,
|
||||
[SYS_SOCKET] = sys_socket,
|
||||
[SYS_CONNECT] = sys_connect,
|
||||
[SYS_SEND] = sys_send,
|
||||
[SYS_RECV] = sys_recv,
|
||||
[SYS_SOCKSTATE] = sys_sockstate,
|
||||
[SYS_GFX] = sys_gfx,
|
||||
};
|
||||
|
||||
void syscall_handler(registers_t *regs) {
|
||||
uint32_t num = regs->eax;
|
||||
|
||||
if (num >= NUM_SYSCALLS || !syscall_table[num]) {
|
||||
offset_print(" SYSCALL: invalid syscall ");
|
||||
print_hex(num);
|
||||
regs->eax = (uint32_t)-1;
|
||||
return;
|
||||
}
|
||||
|
||||
int32_t ret = syscall_table[num](regs);
|
||||
if (ret != SYSCALL_SWITCHED) {
|
||||
regs->eax = (uint32_t)ret;
|
||||
}
|
||||
}
|
||||
|
||||
void init_syscalls(void) {
|
||||
/* Install INT 0x80 as a user-callable interrupt gate.
|
||||
* Flags: 0xEE = Present(1) DPL(11) 0 Type(1110) = 32-bit Interrupt Gate, Ring 3 callable */
|
||||
set_idt_gate_from_c(0x80, (uint32_t)isr128, 0x08, 0xEE);
|
||||
offset_print(" SYSCALL: INT 0x80 installed\n");
|
||||
}
|
||||
53
src/syscall.h
Normal file
53
src/syscall.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* @file syscall.h
|
||||
* @brief System call interface.
|
||||
*
|
||||
* Defines system call numbers and the kernel-side handler. User-mode
|
||||
* processes invoke system calls via INT 0x80 with the call number in EAX
|
||||
* and arguments in EBX, ECX, EDX, ESI, EDI.
|
||||
*/
|
||||
|
||||
#ifndef SYSCALL_H
|
||||
#define SYSCALL_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "isr.h"
|
||||
|
||||
/** System call numbers. */
|
||||
#define SYS_EXIT 0 /**< Exit the process. Arg: exit code in EBX. */
|
||||
#define SYS_WRITE 1 /**< Write to a file descriptor. fd=EBX, buf=ECX, len=EDX. */
|
||||
#define SYS_READ 2 /**< Read from a file descriptor. fd=EBX, buf=ECX, len=EDX. */
|
||||
#define SYS_FORK 3 /**< Fork the current process. */
|
||||
#define SYS_GETPID 4 /**< Get current process ID. */
|
||||
#define SYS_YIELD 5 /**< Yield the CPU. */
|
||||
#define SYS_WAITPID 6 /**< Wait for a child process. pid=EBX. */
|
||||
#define SYS_EXEC 7 /**< Execute a program. path=EBX, argv=ECX. */
|
||||
#define SYS_GETENV 8 /**< Get environment variable. key=EBX, buf=ECX, bufsize=EDX. */
|
||||
#define SYS_SETENV 9 /**< Set environment variable. key=EBX, value=ECX. */
|
||||
#define SYS_READDIR 10 /**< Read directory entry. path=EBX, idx=ECX, buf=EDX. Returns type or -1. */
|
||||
#define SYS_OPEN 11 /**< Open a file. path=EBX, flags=ECX. Returns fd or -1. */
|
||||
#define SYS_CLOSE 12 /**< Close a file descriptor. fd=EBX. Returns 0 or -1. */
|
||||
#define SYS_SOCKET 13 /**< Create a network socket. type=EBX (0=TCP, 1=UDP). Returns sockfd. */
|
||||
#define SYS_CONNECT 14 /**< Connect TCP socket. sockfd=EBX, ip=ECX (host order), port=EDX. */
|
||||
#define SYS_SEND 15 /**< Send data on socket. sockfd=EBX, buf=ECX, len=EDX. Returns bytes sent. */
|
||||
#define SYS_RECV 16 /**< Receive data from socket. sockfd=EBX, buf=ECX, len=EDX. Returns bytes. */
|
||||
#define SYS_SOCKSTATE 17 /**< Get socket state. sockfd=EBX. Returns state constant. */
|
||||
#define SYS_GFX 18 /**< Graphics operations. subcmd=EBX, arg1=ECX, arg2=EDX. */
|
||||
|
||||
/** Total number of system calls. */
|
||||
#define NUM_SYSCALLS 19
|
||||
|
||||
/**
|
||||
* Initialize the system call handler.
|
||||
* Installs INT 0x80 in the IDT.
|
||||
*/
|
||||
void init_syscalls(void);
|
||||
|
||||
/**
|
||||
* System call dispatcher (called from the INT 0x80 handler).
|
||||
*
|
||||
* @param regs Register state at the time of the interrupt.
|
||||
*/
|
||||
void syscall_handler(registers_t *regs);
|
||||
|
||||
#endif /* SYSCALL_H */
|
||||
294
src/sysfs.c
Normal file
294
src/sysfs.c
Normal file
@@ -0,0 +1,294 @@
|
||||
/**
|
||||
* @file sysfs.c
|
||||
* @brief System filesystem (sysfs) implementation.
|
||||
*
|
||||
* A VFS driver mounted at /sys that lets kernel drivers expose virtual
|
||||
* text files. Drivers register namespaces (e.g., "ide", "mem") and
|
||||
* provide callbacks for listing entries and reading/writing file content.
|
||||
*
|
||||
* Path structure:
|
||||
* /sys/<namespace>/<subpath...>
|
||||
*
|
||||
* The VFS routes requests through sysfs_readdir, sysfs_finddir, and
|
||||
* sysfs_read/write, which decompose the path and forward it to the
|
||||
* appropriate driver's callbacks.
|
||||
*/
|
||||
|
||||
#include "sysfs.h"
|
||||
#include "vfs.h"
|
||||
#include <string.h>
|
||||
|
||||
/* Debug print helpers defined in kernel.c */
|
||||
extern void offset_print(const char *str);
|
||||
extern void print_hex(uint32_t val);
|
||||
|
||||
/* ================================================================
|
||||
* Namespace registry
|
||||
* ================================================================ */
|
||||
|
||||
/** A registered sysfs namespace. */
|
||||
typedef struct sysfs_ns {
|
||||
char name[SYSFS_MAX_NAME]; /**< Namespace name. */
|
||||
sysfs_ops_t *ops; /**< Driver callbacks. */
|
||||
void *ctx; /**< Driver context. */
|
||||
int active; /**< 1 if registered. */
|
||||
} sysfs_ns_t;
|
||||
|
||||
/** Namespace table. */
|
||||
static sysfs_ns_t namespaces[SYSFS_MAX_NAMESPACES];
|
||||
|
||||
/**
|
||||
* Find a namespace by name.
|
||||
* @return Pointer to the namespace, or NULL.
|
||||
*/
|
||||
static sysfs_ns_t *find_ns(const char *name) {
|
||||
for (int i = 0; i < SYSFS_MAX_NAMESPACES; i++) {
|
||||
if (namespaces[i].active && strcmp(namespaces[i].name, name) == 0) {
|
||||
return &namespaces[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* Path helpers
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Extract the first path component from a relative path.
|
||||
*
|
||||
* Given "ide/hdd1/model", sets component="ide" and rest="hdd1/model".
|
||||
* Given "ide", sets component="ide" and rest="".
|
||||
*
|
||||
* @param path Input relative path.
|
||||
* @param component Output buffer for the first component.
|
||||
* @param comp_size Size of component buffer.
|
||||
* @param rest Output pointer to the remainder of the path.
|
||||
*/
|
||||
static void split_first(const char *path, char *component,
|
||||
uint32_t comp_size, const char **rest) {
|
||||
/* Skip leading slashes */
|
||||
while (*path == '/') path++;
|
||||
|
||||
const char *slash = path;
|
||||
while (*slash && *slash != '/') slash++;
|
||||
|
||||
uint32_t len = (uint32_t)(slash - path);
|
||||
if (len >= comp_size) len = comp_size - 1;
|
||||
memcpy(component, path, len);
|
||||
component[len] = '\0';
|
||||
|
||||
/* Skip the slash */
|
||||
if (*slash == '/') slash++;
|
||||
*rest = slash;
|
||||
}
|
||||
|
||||
/* ================================================================
|
||||
* VFS callbacks for /sys
|
||||
* ================================================================ */
|
||||
|
||||
/**
|
||||
* Read from a sysfs virtual file.
|
||||
*
|
||||
* The node's fs_data points to the sysfs_ns_t.
|
||||
* The node's name encodes the sub-path (set by finddir).
|
||||
*
|
||||
* We store the full sub-path (relative to namespace) in node->name.
|
||||
*/
|
||||
static int32_t sysfs_vfs_read(vfs_node_t *node, uint32_t offset,
|
||||
uint32_t size, void *buf) {
|
||||
if (!node || !node->fs_data) return -1;
|
||||
|
||||
sysfs_ns_t *ns = (sysfs_ns_t *)node->fs_data;
|
||||
if (!ns->ops || !ns->ops->read) return -1;
|
||||
|
||||
/* Read the full content into a stack buffer */
|
||||
char content[SYSFS_MAX_CONTENT];
|
||||
int total = ns->ops->read(ns->ctx, node->name, content,
|
||||
sizeof(content));
|
||||
if (total < 0) return -1;
|
||||
|
||||
/* Apply offset */
|
||||
if (offset >= (uint32_t)total) return 0;
|
||||
uint32_t avail = (uint32_t)total - offset;
|
||||
if (size > avail) size = avail;
|
||||
memcpy(buf, content + offset, size);
|
||||
return (int32_t)size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write to a sysfs virtual file.
|
||||
*/
|
||||
static int32_t sysfs_vfs_write(vfs_node_t *node, uint32_t offset,
|
||||
uint32_t size, const void *buf) {
|
||||
if (!node || !node->fs_data) return -1;
|
||||
|
||||
sysfs_ns_t *ns = (sysfs_ns_t *)node->fs_data;
|
||||
if (!ns->ops || !ns->ops->write) return -1;
|
||||
|
||||
return ns->ops->write(ns->ctx, node->name, (const char *)buf, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read directory entries under /sys or /sys/<namespace>/...
|
||||
*
|
||||
* When the directory node has no fs_data, we're at /sys root → list
|
||||
* namespaces. When fs_data is a namespace, delegate to its list callback.
|
||||
*/
|
||||
static int sysfs_vfs_readdir(vfs_node_t *dir, uint32_t idx,
|
||||
vfs_dirent_t *out) {
|
||||
if (!dir) return -1;
|
||||
|
||||
/* Root of /sys → list registered namespaces */
|
||||
if (dir->fs_data == NULL) {
|
||||
uint32_t count = 0;
|
||||
for (int i = 0; i < SYSFS_MAX_NAMESPACES; i++) {
|
||||
if (!namespaces[i].active) continue;
|
||||
if (count == idx) {
|
||||
memset(out, 0, sizeof(vfs_dirent_t));
|
||||
strncpy(out->name, namespaces[i].name,
|
||||
VFS_MAX_NAME - 1);
|
||||
out->type = VFS_DIRECTORY;
|
||||
out->inode = (uint32_t)i;
|
||||
return 0;
|
||||
}
|
||||
count++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Subdirectory within a namespace */
|
||||
sysfs_ns_t *ns = (sysfs_ns_t *)dir->fs_data;
|
||||
if (!ns->ops || !ns->ops->list) return -1;
|
||||
|
||||
sysfs_entry_t entry;
|
||||
int ret = ns->ops->list(ns->ctx, dir->name, idx, &entry);
|
||||
if (ret != 0) return -1;
|
||||
|
||||
memset(out, 0, sizeof(vfs_dirent_t));
|
||||
strncpy(out->name, entry.name, VFS_MAX_NAME - 1);
|
||||
out->type = entry.is_dir ? VFS_DIRECTORY : VFS_FILE;
|
||||
out->inode = idx;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Look up a child by name inside a sysfs directory.
|
||||
*
|
||||
* At the /sys root (fs_data == NULL), we look up namespace names.
|
||||
* Inside a namespace, we ask the driver's list callback to check
|
||||
* whether the child is a file or directory.
|
||||
*/
|
||||
static int sysfs_vfs_finddir(vfs_node_t *dir, const char *name,
|
||||
vfs_node_t *out) {
|
||||
if (!dir) return -1;
|
||||
|
||||
memset(out, 0, sizeof(vfs_node_t));
|
||||
|
||||
/* Root of /sys → find a namespace */
|
||||
if (dir->fs_data == NULL) {
|
||||
sysfs_ns_t *ns = find_ns(name);
|
||||
if (!ns) return -1;
|
||||
|
||||
strncpy(out->name, "", VFS_MAX_NAME - 1); /* sub-path is "" */
|
||||
out->type = VFS_DIRECTORY;
|
||||
out->fs_data = ns;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Inside a namespace: build sub-path */
|
||||
sysfs_ns_t *ns = (sysfs_ns_t *)dir->fs_data;
|
||||
if (!ns->ops || !ns->ops->list) return -1;
|
||||
|
||||
/* The dir->name contains the current sub-path (relative to namespace).
|
||||
* The child path is dir->name + "/" + name (or just name if dir->name
|
||||
* is empty). */
|
||||
char child_path[VFS_MAX_NAME];
|
||||
if (dir->name[0] == '\0') {
|
||||
strncpy(child_path, name, VFS_MAX_NAME - 1);
|
||||
child_path[VFS_MAX_NAME - 1] = '\0';
|
||||
} else {
|
||||
/* Manually concatenate dir->name + "/" + name */
|
||||
uint32_t dlen = strlen(dir->name);
|
||||
uint32_t nlen = strlen(name);
|
||||
if (dlen + 1 + nlen >= VFS_MAX_NAME) return -1;
|
||||
memcpy(child_path, dir->name, dlen);
|
||||
child_path[dlen] = '/';
|
||||
memcpy(child_path + dlen + 1, name, nlen);
|
||||
child_path[dlen + 1 + nlen] = '\0';
|
||||
}
|
||||
|
||||
/* Iterate the list callback to see if this child exists */
|
||||
sysfs_entry_t entry;
|
||||
for (uint32_t i = 0; ; i++) {
|
||||
int ret = ns->ops->list(ns->ctx, dir->name, i, &entry);
|
||||
if (ret != 0) break; /* no more entries */
|
||||
if (strcmp(entry.name, name) == 0) {
|
||||
strncpy(out->name, child_path, VFS_MAX_NAME - 1);
|
||||
out->type = entry.is_dir ? VFS_DIRECTORY : VFS_FILE;
|
||||
out->fs_data = ns;
|
||||
|
||||
/* For files, get the content size */
|
||||
if (!entry.is_dir && ns->ops->read) {
|
||||
char tmp[SYSFS_MAX_CONTENT];
|
||||
int sz = ns->ops->read(ns->ctx, child_path, tmp, sizeof(tmp));
|
||||
out->size = (sz > 0) ? (uint32_t)sz : 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return -1; /* child not found */
|
||||
}
|
||||
|
||||
/** VFS operations for /sys. */
|
||||
static vfs_fs_ops_t sysfs_vfs_ops = {
|
||||
.open = NULL,
|
||||
.close = NULL,
|
||||
.read = sysfs_vfs_read,
|
||||
.write = sysfs_vfs_write,
|
||||
.readdir = sysfs_vfs_readdir,
|
||||
.finddir = sysfs_vfs_finddir,
|
||||
};
|
||||
|
||||
/* ================================================================
|
||||
* Public API
|
||||
* ================================================================ */
|
||||
|
||||
void init_sysfs(void) {
|
||||
memset(namespaces, 0, sizeof(namespaces));
|
||||
vfs_mount("/sys", &sysfs_vfs_ops, NULL);
|
||||
offset_print(" Sysfs mounted at /sys\n");
|
||||
}
|
||||
|
||||
int sysfs_register(const char *name, sysfs_ops_t *ops, void *ctx) {
|
||||
if (!name || !ops) return -1;
|
||||
|
||||
/* Check for duplicate */
|
||||
if (find_ns(name)) {
|
||||
offset_print(" Sysfs: namespace '");
|
||||
offset_print(name);
|
||||
offset_print("' already exists\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Find a free slot */
|
||||
for (int i = 0; i < SYSFS_MAX_NAMESPACES; i++) {
|
||||
if (!namespaces[i].active) {
|
||||
strncpy(namespaces[i].name, name, SYSFS_MAX_NAME - 1);
|
||||
namespaces[i].name[SYSFS_MAX_NAME - 1] = '\0';
|
||||
namespaces[i].ops = ops;
|
||||
namespaces[i].ctx = ctx;
|
||||
namespaces[i].active = 1;
|
||||
|
||||
offset_print(" Sysfs: registered namespace '");
|
||||
offset_print(name);
|
||||
offset_print("'\n");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
offset_print(" Sysfs: no free namespace slots\n");
|
||||
return -1;
|
||||
}
|
||||
100
src/sysfs.h
Normal file
100
src/sysfs.h
Normal file
@@ -0,0 +1,100 @@
|
||||
/**
|
||||
* @file sysfs.h
|
||||
* @brief System filesystem (sysfs) interface.
|
||||
*
|
||||
* Allows kernel drivers to expose text/config files to userspace via the
|
||||
* VFS at /sys. Each driver registers a namespace (e.g., "ide") and
|
||||
* provides callbacks for listing, reading, and writing virtual files.
|
||||
*
|
||||
* Usage:
|
||||
* 1. Define a sysfs_ops_t with list/read/write callbacks.
|
||||
* 2. Call sysfs_register("ide", &my_ops, my_context).
|
||||
* 3. Users can then `ls /sys/ide`, `cat /sys/ide/model`, etc.
|
||||
*
|
||||
* File contents are small text strings stored on the stack; the read
|
||||
* callback fills a caller-provided buffer.
|
||||
*/
|
||||
|
||||
#ifndef SYSFS_H
|
||||
#define SYSFS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/** Maximum number of sysfs namespaces. */
|
||||
#define SYSFS_MAX_NAMESPACES 32
|
||||
|
||||
/** Maximum namespace name length. */
|
||||
#define SYSFS_MAX_NAME 64
|
||||
|
||||
/** Maximum size for sysfs file content (text buffer). */
|
||||
#define SYSFS_MAX_CONTENT 512
|
||||
|
||||
/**
|
||||
* Sysfs file entry returned by the list callback.
|
||||
*/
|
||||
typedef struct sysfs_entry {
|
||||
char name[SYSFS_MAX_NAME]; /**< File or directory name. */
|
||||
uint8_t is_dir; /**< 1 if directory, 0 if file. */
|
||||
} sysfs_entry_t;
|
||||
|
||||
/**
|
||||
* Callbacks provided by a driver to expose files under its namespace.
|
||||
*
|
||||
* All paths are relative to the namespace root. For a namespace "ide",
|
||||
* a path of "hdd1/model" means the file "model" inside subdirectory "hdd1".
|
||||
*/
|
||||
typedef struct sysfs_ops {
|
||||
/**
|
||||
* List entries in a directory.
|
||||
*
|
||||
* @param ctx Driver context pointer (from registration).
|
||||
* @param path Relative path within the namespace ("" for root).
|
||||
* @param idx Entry index (0-based).
|
||||
* @param out Output entry.
|
||||
* @return 0 on success, -1 when no more entries.
|
||||
*/
|
||||
int (*list)(void *ctx, const char *path, uint32_t idx,
|
||||
sysfs_entry_t *out);
|
||||
|
||||
/**
|
||||
* Read a file's content as a text string.
|
||||
*
|
||||
* @param ctx Driver context pointer.
|
||||
* @param path Relative path to the file within the namespace.
|
||||
* @param buf Output buffer for file content.
|
||||
* @param buf_size Buffer size.
|
||||
* @return Number of bytes written to buf, or -1 on error.
|
||||
*/
|
||||
int (*read)(void *ctx, const char *path, char *buf, uint32_t buf_size);
|
||||
|
||||
/**
|
||||
* Write data to a file.
|
||||
*
|
||||
* @param ctx Driver context pointer.
|
||||
* @param path Relative path to the file within the namespace.
|
||||
* @param buf Input data.
|
||||
* @param size Number of bytes to write.
|
||||
* @return Number of bytes consumed, or -1 on error.
|
||||
*/
|
||||
int (*write)(void *ctx, const char *path, const char *buf, uint32_t size);
|
||||
} sysfs_ops_t;
|
||||
|
||||
/**
|
||||
* Initialize the sysfs and mount it at /sys.
|
||||
*/
|
||||
void init_sysfs(void);
|
||||
|
||||
/**
|
||||
* Register a driver namespace in sysfs.
|
||||
*
|
||||
* After registration, files are accessible under /sys/<name>/.
|
||||
*
|
||||
* @param name Namespace name (e.g., "ide").
|
||||
* @param ops Callback operations.
|
||||
* @param ctx Opaque driver context passed to all callbacks.
|
||||
* @return 0 on success, -1 if the table is full or name is invalid.
|
||||
*/
|
||||
int sysfs_register(const char *name, sysfs_ops_t *ops, void *ctx);
|
||||
|
||||
#endif /* SYSFS_H */
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user