feat: Intel/AMD AVX & MSR sensors, Gigabit PCIe NICs, AHCI SATA, FAT32, and ogit in Ring 3
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
#pragma once
|
||||
|
||||
#include "stdint.h"
|
||||
#include "stdbool.h"
|
||||
#include "stddef.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define SATA_SIG_ATA 0x00000101 // SATA drive
|
||||
#define SATA_SIG_ATAPI 0xEB140101 // SATAPI drive
|
||||
#define SATA_SIG_SEMB 0xC33C0101 // Enclosure management bridge
|
||||
#define SATA_SIG_PM 0x96690101 // Port multiplier
|
||||
|
||||
#define AHCI_DEV_NULL 0
|
||||
#define AHCI_DEV_SATA 1
|
||||
#define AHCI_DEV_SEMB 2
|
||||
#define AHCI_DEV_PM 3
|
||||
#define AHCI_DEV_SATAPI 4
|
||||
|
||||
#define HBA_PORT_IPM_ACTIVE 1
|
||||
#define HBA_PORT_DET_PRESENT 3
|
||||
|
||||
// Register FIS - Host to Device
|
||||
#define FIS_TYPE_REG_H2D 0x27
|
||||
#define ATA_CMD_READ_DMA_EXT 0x25
|
||||
#define ATA_CMD_WRITE_DMA_EXT 0x35
|
||||
|
||||
typedef volatile struct tagHBA_PORT {
|
||||
uint32_t clb; // 0x00, Command list base address, 1K-byte aligned
|
||||
uint32_t clbu; // 0x04, Command list base address upper 32 bits
|
||||
uint32_t fb; // 0x08, FIS base address, 256-byte aligned
|
||||
uint32_t fbu; // 0x0C, FIS base address upper 32 bits
|
||||
uint32_t is; // 0x10, Interrupt status
|
||||
uint32_t ie; // 0x14, Interrupt enable
|
||||
uint32_t cmd; // 0x18, Command and status
|
||||
uint32_t rsv0; // 0x1C, Reserved
|
||||
uint32_t tfd; // 0x20, Task file data
|
||||
uint32_t sig; // 0x24, Signature
|
||||
uint32_t ssts; // 0x28, SATA status (SCR0:SStatus)
|
||||
uint32_t sctl; // 0x2C, SATA control (SCR2:SControl)
|
||||
uint32_t serr; // 0x30, SATA error (SCR1:SError)
|
||||
uint32_t sact; // 0x34, SATA active (SCR3:SActive)
|
||||
uint32_t ci; // 0x38, Command issue
|
||||
uint32_t sntf; // 0x3C, SATA notification (SCR4:SNotification)
|
||||
uint32_t fbs; // 0x40, FIS-based switch control
|
||||
uint32_t rsv1[11]; // 0x44 ~ 0x6F, Reserved
|
||||
uint32_t vendor[4]; // 0x70 ~ 0x7F, vendor specific
|
||||
} HBA_PORT;
|
||||
|
||||
typedef volatile struct tagHBA_MEM {
|
||||
uint32_t cap; // 0x00 - 0x03, Host capability
|
||||
uint32_t ghc; // 0x04 - 0x07, Global host control
|
||||
uint32_t is; // 0x08 - 0x0B, Interrupt status
|
||||
uint32_t pi; // 0x0C - 0x0F, Port implemented
|
||||
uint32_t vs; // 0x10 - 0x13, Version
|
||||
uint32_t ccc_ctl; // 0x14 - 0x17, Command completion coalescing control
|
||||
uint32_t ccc_pts; // 0x18 - 0x1B, Command completion coalescing ports
|
||||
uint32_t em_loc; // 0x1C - 0x1F, Enclosure management location
|
||||
uint32_t em_ctl; // 0x20 - 0x23, Enclosure management control
|
||||
uint32_t cap2; // 0x24 - 0x27, Host capabilities extended
|
||||
uint32_t bohc; // 0x28 - 0x2B, BIOS/OS handoff control and status
|
||||
uint8_t rsv[0xA0-0x2C];
|
||||
uint8_t vendor[0x100-0xA0];
|
||||
HBA_PORT ports[32]; // 0x100 ~ 0x10FF, Port control registers
|
||||
} HBA_MEM;
|
||||
|
||||
typedef struct tagHBA_CMD_HEADER {
|
||||
uint8_t cfl:5; // Command FIS length in DWORDS, 2 ~ 16
|
||||
uint8_t a:1; // ATAPI
|
||||
uint8_t w:1; // Write, 1: H2D, 0: D2H
|
||||
uint8_t p:1; // Prefetchable
|
||||
uint8_t r:1; // Reset
|
||||
uint8_t b:1; // BIST
|
||||
uint8_t c:1; // Clear busy upon R_OK
|
||||
uint8_t rsv0:1; // Reserved
|
||||
uint8_t pmp:4; // Port multiplier port
|
||||
uint16_t prdtl; // Physical region descriptor table length in entries
|
||||
volatile uint32_t prdbc; // Physical region descriptor byte count transferred
|
||||
uint32_t ctba; // Command table descriptor base address
|
||||
uint32_t ctbau; // Command table descriptor base address upper 32 bits
|
||||
uint32_t rsv1[4]; // Reserved
|
||||
} HBA_CMD_HEADER;
|
||||
|
||||
typedef struct tagHBA_PRDT_ENTRY {
|
||||
uint32_t dba; // Data base address
|
||||
uint32_t dbau; // Data base address upper 32 bits
|
||||
uint32_t rsv0; // Reserved
|
||||
uint32_t dbc:22; // Byte count, 4M max
|
||||
uint32_t rsv1:9; // Reserved
|
||||
uint32_t i:1; // Interrupt on completion
|
||||
} HBA_PRDT_ENTRY;
|
||||
|
||||
typedef struct tagHBA_CMD_TABLE {
|
||||
uint8_t cfis[64]; // Command FIS
|
||||
uint8_t acmd[16]; // ATAPI command, 12 or 16 bytes
|
||||
uint8_t rsv[48]; // Reserved
|
||||
HBA_PRDT_ENTRY prdt_entry[1]; // Physical region descriptor table entries, 0 ~ 65535
|
||||
} HBA_CMD_TABLE;
|
||||
|
||||
typedef struct tagFIS_REG_H2D {
|
||||
uint8_t fis_type; // FIS_TYPE_REG_H2D
|
||||
uint8_t pmport:4; // Port multiplier
|
||||
uint8_t rsv0:3; // Reserved
|
||||
uint8_t c:1; // 1: Command, 0: Control
|
||||
uint8_t command; // Command register
|
||||
uint8_t featurel; // Feature register, 7:0
|
||||
uint8_t lba0; // LBA low register, 7:0
|
||||
uint8_t lba1; // LBA mid register, 15:8
|
||||
uint8_t lba2; // LBA high register, 23:16
|
||||
uint8_t device; // Device register
|
||||
uint8_t lba3; // LBA register, 31:24
|
||||
uint8_t lba4; // LBA register, 39:32
|
||||
uint8_t lba5; // LBA register, 47:40
|
||||
uint8_t featureh; // Feature register, 15:8
|
||||
uint8_t countl; // Count register, 7:0
|
||||
uint8_t counth; // Count register, 15:8
|
||||
uint8_t icc; // Isochronous command completion
|
||||
uint8_t control; // Control register
|
||||
uint8_t rsv1[4]; // Reserved
|
||||
} FIS_REG_H2D;
|
||||
|
||||
typedef struct ahci_device {
|
||||
uint8_t bus;
|
||||
uint8_t slot;
|
||||
uint8_t func;
|
||||
uint64_t abar_phys;
|
||||
HBA_MEM *hba_mem;
|
||||
int active_port;
|
||||
bool initialized;
|
||||
uint64_t sector_count;
|
||||
} ahci_device_t;
|
||||
|
||||
extern ahci_device_t g_ahci;
|
||||
|
||||
int ahci_detect_and_init(void);
|
||||
int ahci_read_sectors(uint64_t lba, uint32_t count, void *buf);
|
||||
int ahci_write_sectors(uint64_t lba, uint32_t count, const void *buf);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,107 @@
|
||||
#pragma once
|
||||
|
||||
#include "stdint.h"
|
||||
#include "stdbool.h"
|
||||
#include "stddef.h"
|
||||
#include "pci.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define E1000_NUM_RX_DESC 16
|
||||
#define E1000_NUM_TX_DESC 16
|
||||
#define E1000_RX_BUF_SIZE 2048
|
||||
#define E1000_TX_BUF_SIZE 2048
|
||||
|
||||
// E1000 MMIO Register Offsets
|
||||
#define E1000_REG_CTRL 0x0000
|
||||
#define E1000_REG_STATUS 0x0008
|
||||
#define E1000_REG_EEPROM 0x0014
|
||||
#define E1000_REG_ICR 0x00C0
|
||||
#define E1000_REG_IMS 0x00D0
|
||||
#define E1000_REG_RCTL 0x0100
|
||||
#define E1000_REG_RDBAL 0x2800
|
||||
#define E1000_REG_RDBAH 0x2804
|
||||
#define E1000_REG_RDLEN 0x2808
|
||||
#define E1000_REG_RDH 0x2810
|
||||
#define E1000_REG_RDT 0x2818
|
||||
#define E1000_REG_TCTL 0x0400
|
||||
#define E1000_REG_TDBAL 0x3800
|
||||
#define E1000_REG_TDBAH 0x3804
|
||||
#define E1000_REG_TDLEN 0x3808
|
||||
#define E1000_REG_TDH 0x3810
|
||||
#define E1000_REG_TDT 0x3818
|
||||
#define E1000_REG_RAL 0x5400
|
||||
#define E1000_REG_RAH 0x5404
|
||||
|
||||
// RCTL bits
|
||||
#define E1000_RCTL_EN (1 << 1)
|
||||
#define E1000_RCTL_SBP (1 << 2)
|
||||
#define E1000_RCTL_UPE (1 << 3)
|
||||
#define E1000_RCTL_MPE (1 << 4)
|
||||
#define E1000_RCTL_BAM (1 << 15)
|
||||
#define E1000_RCTL_BSIZE_2K (0 << 16)
|
||||
#define E1000_RCTL_SECRC (1 << 26)
|
||||
|
||||
// TCTL bits
|
||||
#define E1000_TCTL_EN (1 << 1)
|
||||
#define E1000_TCTL_PSP (1 << 3)
|
||||
#define E1000_TCTL_CT_SHIFT 4
|
||||
#define E1000_TCTL_COLD_SHIFT 12
|
||||
|
||||
// TX Command bits
|
||||
#define E1000_TXD_CMD_EOP (1 << 0)
|
||||
#define E1000_TXD_CMD_IFCS (1 << 1)
|
||||
#define E1000_TXD_CMD_RS (1 << 3)
|
||||
|
||||
// RX Status bits
|
||||
#define E1000_RXD_STAT_DD (1 << 0)
|
||||
#define E1000_RXD_STAT_EOP (1 << 1)
|
||||
|
||||
// E1000 RX Descriptor
|
||||
typedef struct __attribute__((packed)) e1000_rx_desc {
|
||||
uint64_t addr;
|
||||
uint16_t length;
|
||||
uint16_t checksum;
|
||||
uint8_t status;
|
||||
uint8_t errors;
|
||||
uint16_t special;
|
||||
} e1000_rx_desc_t;
|
||||
|
||||
// E1000 TX Descriptor
|
||||
typedef struct __attribute__((packed)) e1000_tx_desc {
|
||||
uint64_t addr;
|
||||
uint16_t length;
|
||||
uint8_t cso;
|
||||
uint8_t cmd;
|
||||
uint8_t status;
|
||||
uint8_t css;
|
||||
uint16_t special;
|
||||
} e1000_tx_desc_t;
|
||||
|
||||
typedef struct e1000_device {
|
||||
uint8_t bus;
|
||||
uint8_t slot;
|
||||
uint8_t func;
|
||||
uint16_t device_id;
|
||||
uint64_t mmio_base;
|
||||
uint8_t mac[6];
|
||||
uint32_t rx_cur;
|
||||
uint32_t tx_cur;
|
||||
uint64_t rx_packets;
|
||||
uint64_t rx_bytes;
|
||||
uint64_t tx_packets;
|
||||
uint64_t tx_bytes;
|
||||
bool initialized;
|
||||
} e1000_device_t;
|
||||
|
||||
extern e1000_device_t g_e1000;
|
||||
|
||||
int e1000_detect_and_init(void);
|
||||
int e1000_send_packet(const void *packet, size_t length);
|
||||
int e1000_receive_packet(void *buffer, size_t max_length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,75 @@
|
||||
#pragma once
|
||||
|
||||
#include "stdint.h"
|
||||
#include "stdbool.h"
|
||||
#include "stddef.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct __attribute__((packed)) fat32_bpb {
|
||||
uint8_t jmp[3];
|
||||
char oem[8];
|
||||
uint16_t bytes_per_sector;
|
||||
uint8_t sectors_per_cluster;
|
||||
uint16_t reserved_sectors;
|
||||
uint8_t num_fats;
|
||||
uint16_t root_entry_count;
|
||||
uint16_t total_sectors_16;
|
||||
uint8_t media_type;
|
||||
uint16_t fat_size_16;
|
||||
uint16_t sectors_per_track;
|
||||
uint16_t num_heads;
|
||||
uint32_t hidden_sectors;
|
||||
uint32_t total_sectors_32;
|
||||
uint32_t fat_size_32;
|
||||
uint16_t ext_flags;
|
||||
uint16_t fs_version;
|
||||
uint32_t root_cluster;
|
||||
uint16_t fs_info;
|
||||
uint16_t backup_boot_sector;
|
||||
uint8_t reserved[12];
|
||||
uint8_t drive_number;
|
||||
uint8_t reserved1;
|
||||
uint8_t boot_signature;
|
||||
uint32_t volume_id;
|
||||
char volume_label[11];
|
||||
char fs_type[8];
|
||||
} fat32_bpb_t;
|
||||
|
||||
typedef struct __attribute__((packed)) fat32_dir_entry {
|
||||
char name[11];
|
||||
uint8_t attr;
|
||||
uint8_t nt_reserved;
|
||||
uint8_t creation_time_tenth;
|
||||
uint16_t creation_time;
|
||||
uint16_t creation_date;
|
||||
uint16_t last_access_date;
|
||||
uint16_t first_cluster_high;
|
||||
uint16_t write_time;
|
||||
uint16_t write_date;
|
||||
uint16_t first_cluster_low;
|
||||
uint32_t file_size;
|
||||
} fat32_dir_entry_t;
|
||||
|
||||
typedef struct fat32_fs {
|
||||
uint32_t partition_lba;
|
||||
uint32_t fat_start_lba;
|
||||
uint32_t cluster_start_lba;
|
||||
uint32_t sectors_per_cluster;
|
||||
uint32_t bytes_per_cluster;
|
||||
uint32_t root_cluster;
|
||||
bool mounted;
|
||||
} fat32_fs_t;
|
||||
|
||||
extern fat32_fs_t g_fat32;
|
||||
|
||||
int fat32_mount(void);
|
||||
int fat32_list(const char *dir_path, char *out_buf, size_t buf_size);
|
||||
int fat32_read_file(const char *path, void *buffer, size_t max_len);
|
||||
int fat32_write_file(const char *path, const void *buffer, size_t len);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include "stdint.h"
|
||||
#include "stdbool.h"
|
||||
#include "stddef.h"
|
||||
#include "pci.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct r8169_device {
|
||||
uint8_t bus;
|
||||
uint8_t slot;
|
||||
uint8_t func;
|
||||
uint16_t device_id;
|
||||
uint16_t io_base;
|
||||
uint8_t mac[6];
|
||||
uint64_t rx_packets;
|
||||
uint64_t rx_bytes;
|
||||
uint64_t tx_packets;
|
||||
uint64_t tx_bytes;
|
||||
bool initialized;
|
||||
} r8169_device_t;
|
||||
|
||||
extern r8169_device_t g_r8169;
|
||||
|
||||
int r8169_detect_and_init(void);
|
||||
int r8169_send_packet(const void *packet, size_t length);
|
||||
int r8169_receive_packet(void *buffer, size_t max_length);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user