2 Commits

Author SHA1 Message Date
09b2b8cd11 Create ata.c 2026-01-18 17:48:39 -08:00
4079d18a45 Create ata.h
Add base ATA PIO mode driver so that filesystems like fat16 fat32 work.
2026-01-18 17:46:26 -08:00
6 changed files with 156 additions and 75 deletions

91
kernel/ata.c Normal file
View File

@@ -0,0 +1,91 @@
#include "ata.h"
#include "io.h"
#include "print.h"
#define ATA_TIMEOUT 100000
static inline void ata_delay(void) {
/* 400ns delay by reading alternate status */
inb(ATA_PRIMARY_CTRL);
inb(ATA_PRIMARY_CTRL);
inb(ATA_PRIMARY_CTRL);
inb(ATA_PRIMARY_CTRL);
}
static bool ata_wait(uint8_t mask) {
for (int i = 0; i < ATA_TIMEOUT; i++) {
uint8_t status = inb(ATA_PRIMARY_IO + ATA_REG_STATUS);
if (!(status & ATA_SR_BSY) && (status & mask))
return true;
}
return false;
}
bool ata_init(void) {
outb(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, 0xA0); // master
ata_delay();
outb(ATA_PRIMARY_IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY);
ata_delay();
uint8_t status = inb(ATA_PRIMARY_IO + ATA_REG_STATUS);
if (status == 0)
return false; // no drive
if (!ata_wait(ATA_SR_DRQ))
return false;
uint16_t identify[256];
for (int i = 0; i < 256; i++)
identify[i] = inw(ATA_PRIMARY_IO);
print_string("[ATA] Primary master detected\n");
return true;
}
bool ata_read_sector(uint32_t lba, uint8_t* buffer) {
if (!buffer) return false;
outb(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, 0xE0 | ((lba >> 24) & 0x0F));
outb(ATA_PRIMARY_IO + ATA_REG_SECCOUNT0, 1);
outb(ATA_PRIMARY_IO + ATA_REG_LBA0, (uint8_t)(lba));
outb(ATA_PRIMARY_IO + ATA_REG_LBA1, (uint8_t)(lba >> 8));
outb(ATA_PRIMARY_IO + ATA_REG_LBA2, (uint8_t)(lba >> 16));
outb(ATA_PRIMARY_IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO);
if (!ata_wait(ATA_SR_DRQ))
return false;
for (int i = 0; i < 256; i++) {
uint16_t data = inw(ATA_PRIMARY_IO);
buffer[i * 2] = data & 0xFF;
buffer[i * 2 + 1] = data >> 8;
}
ata_delay();
return true;
}
bool ata_write_sector(uint32_t lba, const uint8_t* buffer) {
if (!buffer) return false;
outb(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, 0xE0 | ((lba >> 24) & 0x0F));
outb(ATA_PRIMARY_IO + ATA_REG_SECCOUNT0, 1);
outb(ATA_PRIMARY_IO + ATA_REG_LBA0, (uint8_t)(lba));
outb(ATA_PRIMARY_IO + ATA_REG_LBA1, (uint8_t)(lba >> 8));
outb(ATA_PRIMARY_IO + ATA_REG_LBA2, (uint8_t)(lba >> 16));
outb(ATA_PRIMARY_IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO);
if (!ata_wait(ATA_SR_DRQ))
return false;
for (int i = 0; i < 256; i++) {
uint16_t word =
buffer[i * 2] |
(buffer[i * 2 + 1] << 8);
outw(ATA_PRIMARY_IO, word);
}
ata_delay();
return true;
}

43
kernel/ata.h Normal file
View File

@@ -0,0 +1,43 @@
#ifndef ATA_H
#define ATA_H
#include <stdint.h>
#include <stdbool.h>
/* ATA I/O ports */
#define ATA_PRIMARY_IO 0x1F0
#define ATA_PRIMARY_CTRL 0x3F6
/* ATA registers */
#define ATA_REG_DATA 0x00
#define ATA_REG_ERROR 0x01
#define ATA_REG_FEATURES 0x01
#define ATA_REG_SECCOUNT0 0x02
#define ATA_REG_LBA0 0x03
#define ATA_REG_LBA1 0x04
#define ATA_REG_LBA2 0x05
#define ATA_REG_HDDEVSEL 0x06
#define ATA_REG_COMMAND 0x07
#define ATA_REG_STATUS 0x07
/* ATA commands */
#define ATA_CMD_READ_PIO 0x20
#define ATA_CMD_WRITE_PIO 0x30
#define ATA_CMD_IDENTIFY 0xEC
/* Status flags */
#define ATA_SR_BSY 0x80
#define ATA_SR_DRDY 0x40
#define ATA_SR_DRQ 0x08
#define ATA_SR_ERR 0x01
/* Drive select */
#define ATA_MASTER 0x00
#define ATA_SLAVE 0x10
/* Public API */
bool ata_init(void);
bool ata_read_sector(uint32_t lba, uint8_t* buffer);
bool ata_write_sector(uint32_t lba, const uint8_t* buffer);
#endif

View File

@@ -1,80 +1,36 @@
#include <string.h>
#include "display.h" #include "display.h"
#include "io.h" #include "io.h" // Include your I/O header for port access
#include "vga.h" #include "vga.h"
// Initialize the display // Initialize the display
void init_display(void) { void init_display(void) {
// Initialize the VGA driver. This typically sets up the 80x25 text mode, // Initialize VGA settings, if necessary
// clears the screen, and sets the cursor. // This could involve setting up the VGA mode, etc.
vga_init(); set_display_mode(0x13); // Example: Set to 320x200 256-color mode
} }
// Enumerate connected displays // Enumerate connected displays
void enumerate_displays(void) { void enumerate_displays(void) {
// This function is often a complex operation in a real driver. // This is a simplified example. Actual enumeration may require
// In this simplified kernel/VGA text mode environment, we use printf // reading from specific VGA registers or using BIOS interrupts.
// to output a message and rely on the fact that VGA is present.
// Clear the display before printing a message // For demonstration, we will just print a message
vga_clear(vga_entry_color(VGA_COLOR_LIGHT_GREY, VGA_COLOR_BLACK)); // In a real driver, you would check the VGA registers
// to determine connected displays.
// Output a simplified enumeration message clear_display();
vga_printf("Display: Standard VGA Text Mode (80x25) Detected.\n"); // Here you would typically read from VGA registers to find connected displays
// For example, using inb() to read from VGA ports
// In a real driver, you would use inb() and outb() with specific VGA ports
// to read information (e.g., from the CRTC registers 0x3D4/0x3D5)
// to check for display presence or configuration.
} }
// Set the display mode // Set the display mode
// NOTE: Setting arbitrary VGA modes (like 0x13 for 320x200) is very complex
// and requires writing hundreds of register values, often done via BIOS in
// real mode. Since we are in protected mode and have a simple text driver,
// this function is kept simple or treated as a placeholder for full mode changes.
void set_display_mode(uint8_t mode) { void set_display_mode(uint8_t mode) {
// Check if the requested mode is a known mode (e.g., VGA Text Mode 3) // Set the VGA mode by writing to the appropriate registers
// For this example, we simply acknowledge the call. outb(VGA_PORT, mode); // Example function to write to a port
// A true mode set would involve complex register sequencing.
// The provided vga.c is a Text Mode driver, so a graphical mode set
// like 0x13 (320x200 256-color) would break the existing vga_printf functionality.
// A simplified text-mode-specific response:
if (mode == 0x03) { // Mode 3 is standard 80x25 text mode
vga_printf("Display mode set to 80x25 Text Mode (Mode 0x03).\n");
vga_init(); // Re-initialize the text mode
} else {
// Simple I/O example based on the original structure (Caution: Incomplete for full mode set)
outb(VGA_PORT, mode); // Example function to write to a port
vga_printf("Attempting to set display mode to 0x%x. (Warning: May break current display)\n", mode);
}
} }
// Clear the display // Clear the display
void clear_display(void) { void clear_display(void) {
// Use the VGA driver's clear function, typically clearing to black on light grey // Clear the display by filling it with a color
// or black on black. We'll use the black on light grey from vga_init for consistency. // This is a placeholder for actual clearing logic
vga_clear(vga_entry_color(VGA_COLOR_BLACK, VGA_COLOR_LIGHT_GREY)); // You would typically write to video memory here
// Reset cursor to 0, 0
vga_set_cursor_position(0, 0);
}
// Helper function to write a string
void display_write_string(const char* str) {
// Use the VGA driver's string writing function
vga_write_string(str, strlen(str));
}
// Helper function to print a formatted string
void display_printf(const char* format, ...) {
// Use the VGA driver's printf function
va_list args;
va_start(args, format);
// The vga_printf function already handles the va_list internally,
// so we can just call it directly.
vga_printf(format, args);
va_end(args);
} }

View File

@@ -2,21 +2,13 @@
#define DISPLAY_H #define DISPLAY_H
#include <stdint.h> #include <stdint.h>
#include "vga.h" // Include VGA functions
#define VGA_PORT 0x3C0 // Base port for VGA (Often used for general control, though 0x3D4/0x3D5 are used for cursor) #define VGA_PORT 0x3C0 // Base port for VGA
// Function prototypes // Function prototypes
void init_display(void); void init_display(void);
void enumerate_displays(void); void enumerate_displays(void);
void set_display_mode(uint8_t mode); // In this context, modes are typically BIOS or VESA modes, which are complex. void set_display_mode(uint8_t mode);
// We'll treat this as a placeholder/simple mode call.
void clear_display(void); void clear_display(void);
// New function to write a string using the VGA driver
void display_write_string(const char* str);
// New function to print a formatted string using the VGA driver
void display_printf(const char* format, ...);
#endif // DISPLAY_H #endif // DISPLAY_H

View File

@@ -1,9 +1,9 @@
#include "vga.h"
#include <stddef.h> #include <stddef.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
#include "string_utils.h" #include "string_utils.h"
#include "vga.h"
void outb(uint16_t port, uint8_t value) { void outb(uint16_t port, uint8_t value) {
__asm__ volatile("outb %0, %1" : : "a"(value), "Nd"(port)); __asm__ volatile("outb %0, %1" : : "a"(value), "Nd"(port));
@@ -134,7 +134,7 @@ void vga_printf(const char* format, ...) {
va_end(args); va_end(args);
// Now you can use the buffer with vga_write_string // Now you can use the buffer with vga_write_string
vga_write_string(buffer, strlen(buffer)); // Use my_strlen instead of strlen vga_write_string(buffer, my_strlen(buffer)); // Use my_strlen instead of strlen
} }
void vga_init(void) { void vga_init(void) {

View File

@@ -35,7 +35,6 @@ typedef enum {
// Function prototypes // Function prototypes
uint8_t vga_entry_color(vga_color fg, vga_color bg); uint8_t vga_entry_color(vga_color fg, vga_color bg);
uint16_t vga_entry(unsigned char uc, uint8_t color); uint16_t vga_entry(unsigned char uc, uint8_t color);
void vga_init(void);
void vga_put_entry_at(char c, uint8_t color, size_t x, size_t y); void vga_put_entry_at(char c, uint8_t color, size_t x, size_t y);
void vga_clear(uint8_t color); void vga_clear(uint8_t color);