push
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
#ifndef ATA_H
|
||||
#define ATA_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define ATA_PRIMARY_IO 0x1F0
|
||||
#define ATA_PRIMARY_CTRL 0x3F6
|
||||
#define ATA_SECONDARY_IO 0x170
|
||||
#define ATA_SECONDARY_CTRL 0x376
|
||||
|
||||
#define ATA_REG_DATA 0x00
|
||||
#define ATA_REG_ERROR 0x01
|
||||
#define ATA_REG_FEATURES 0x01
|
||||
#define ATA_REG_SECCOUNT 0x02
|
||||
#define ATA_REG_LBA_LO 0x03
|
||||
#define ATA_REG_LBA_MID 0x04
|
||||
#define ATA_REG_LBA_HI 0x05
|
||||
#define ATA_REG_DRIVE 0x06
|
||||
#define ATA_REG_STATUS 0x07
|
||||
#define ATA_REG_COMMAND 0x07
|
||||
|
||||
#define ATA_REG_ALT_STATUS 0x00
|
||||
#define ATA_REG_DEV_CTRL 0x00
|
||||
|
||||
#define ATA_SR_BSY 0x80
|
||||
#define ATA_SR_DRDY 0x40
|
||||
#define ATA_SR_DF 0x20
|
||||
#define ATA_SR_DSC 0x10
|
||||
#define ATA_SR_DRQ 0x08
|
||||
#define ATA_SR_CORR 0x04
|
||||
#define ATA_SR_IDX 0x02
|
||||
#define ATA_SR_ERR 0x01
|
||||
|
||||
#define ATA_CMD_READ_PIO 0x20
|
||||
#define ATA_CMD_READ_PIO_EXT 0x24
|
||||
#define ATA_CMD_WRITE_PIO 0x30
|
||||
#define ATA_CMD_WRITE_PIO_EXT 0x34
|
||||
#define ATA_CMD_CACHE_FLUSH 0xE7
|
||||
#define ATA_CMD_CACHE_FLUSH_EXT 0xEA
|
||||
#define ATA_CMD_IDENTIFY 0xEC
|
||||
#define ATA_CMD_IDENTIFY_PACKET 0xA1
|
||||
|
||||
#define ATA_DRIVE_MASTER 0xA0
|
||||
#define ATA_DRIVE_SLAVE 0xB0
|
||||
|
||||
#define ATA_LBA_BIT 0x40
|
||||
|
||||
#define ATA_MAX_DRIVES 4
|
||||
|
||||
#define ATA_SECTOR_SIZE 512
|
||||
|
||||
typedef struct {
|
||||
bool present;
|
||||
bool is_atapi;
|
||||
bool lba48;
|
||||
uint16_t io_base;
|
||||
uint16_t ctrl_base;
|
||||
uint8_t drive_select;
|
||||
uint8_t irq;
|
||||
uint64_t sectors;
|
||||
uint64_t size_bytes;
|
||||
char model[41];
|
||||
char serial[21];
|
||||
char firmware[9];
|
||||
uint16_t identify[256];
|
||||
} ata_drive_t;
|
||||
|
||||
void ata_init(void);
|
||||
ata_drive_t *ata_get_drive(int index);
|
||||
int ata_get_drive_count(void);
|
||||
int ata_read_sectors(ata_drive_t *drive, uint64_t lba, uint32_t count, void *buffer);
|
||||
int ata_write_sectors(ata_drive_t *drive, uint64_t lba, uint32_t count, const void *buffer);
|
||||
int ata_flush(ata_drive_t *drive);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,38 @@
|
||||
#ifndef BLKDEV_H
|
||||
#define BLKDEV_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define BLKDEV_MAX 8
|
||||
#define BLKDEV_NAME_MAX 32
|
||||
#define BLKDEV_SECTOR_SIZE 512
|
||||
|
||||
typedef struct blkdev blkdev_t;
|
||||
|
||||
typedef struct blkdev_ops {
|
||||
int (*read_sectors) (blkdev_t *dev, uint64_t lba, uint32_t count, void *buf);
|
||||
int (*write_sectors)(blkdev_t *dev, uint64_t lba, uint32_t count, const void *buf);
|
||||
int (*flush) (blkdev_t *dev);
|
||||
} blkdev_ops_t;
|
||||
|
||||
struct blkdev {
|
||||
char name[BLKDEV_NAME_MAX];
|
||||
bool present;
|
||||
uint64_t sector_count;
|
||||
uint64_t size_bytes;
|
||||
uint32_t sector_size;
|
||||
const blkdev_ops_t *ops;
|
||||
void *priv;
|
||||
};
|
||||
|
||||
int blkdev_register(blkdev_t *dev);
|
||||
blkdev_t *blkdev_get_by_name(const char *name);
|
||||
blkdev_t *blkdev_get(int index);
|
||||
int blkdev_count(void);
|
||||
void blkdev_init(void);
|
||||
int blkdev_read(blkdev_t *dev, uint64_t offset, void *buf, size_t len);
|
||||
int blkdev_write(blkdev_t *dev, uint64_t offset, const void *buf, size_t len);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
#ifndef DISK_H
|
||||
#define DISK_H
|
||||
|
||||
#include "../drivers/blkdev.h"
|
||||
|
||||
void disk_init(void);
|
||||
int disk_mount(const char *devname, const char *path);
|
||||
int disk_umount(const char *path);
|
||||
int disk_format(const char *devname, const char *label);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,52 @@
|
||||
#ifndef PARTITION_H
|
||||
#define PARTITION_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "blkdev.h"
|
||||
|
||||
#define MBR_SIGNATURE 0xAA55
|
||||
#define MBR_SIGNATURE_OFF 510
|
||||
#define MBR_PARTITION_OFF 446
|
||||
#define MBR_MAX_PARTITIONS 4
|
||||
|
||||
#define MBR_TYPE_EMPTY 0x00
|
||||
#define MBR_TYPE_FAT12 0x01
|
||||
#define MBR_TYPE_FAT16_S 0x04
|
||||
#define MBR_TYPE_EXTENDED 0x05
|
||||
#define MBR_TYPE_FAT16 0x06
|
||||
#define MBR_TYPE_FAT32_CHS 0x0B
|
||||
#define MBR_TYPE_FAT32_LBA 0x0C
|
||||
#define MBR_TYPE_FAT16_LBA 0x0E
|
||||
#define MBR_TYPE_LINUX 0x83
|
||||
#define MBR_TYPE_ESP 0xEF
|
||||
|
||||
typedef struct __attribute__((packed)) {
|
||||
uint8_t boot_flag;
|
||||
uint8_t chs_start[3];
|
||||
uint8_t type;
|
||||
uint8_t chs_end[3];
|
||||
uint32_t lba_start;
|
||||
uint32_t sector_count;
|
||||
} mbr_partition_t;
|
||||
|
||||
typedef struct __attribute__((packed)) {
|
||||
uint8_t bootstrap[440];
|
||||
uint32_t disk_signature;
|
||||
uint16_t reserved;
|
||||
mbr_partition_t partitions[4];
|
||||
uint16_t signature;
|
||||
} mbr_t;
|
||||
|
||||
_Static_assert(sizeof(mbr_t) == 512, "mbr size must be 512");
|
||||
|
||||
int partition_scan(blkdev_t *disk);
|
||||
|
||||
int partition_write_mbr(blkdev_t *disk, const mbr_partition_t parts[4],
|
||||
uint32_t disk_signature);
|
||||
|
||||
int partition_read_mbr(blkdev_t *disk, mbr_t *out);
|
||||
|
||||
blkdev_t *partition_get(const char *name);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,91 @@
|
||||
#ifndef PS2_H
|
||||
#define PS2_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#define PS2_DATA_PORT 0x60
|
||||
#define PS2_STATUS_PORT 0x64
|
||||
#define PS2_CMD_PORT 0x64
|
||||
|
||||
#define PS2_STATUS_OUTPUT_FULL (1 << 0)
|
||||
#define PS2_STATUS_INPUT_FULL (1 << 1)
|
||||
|
||||
#define PS2_CMD_READ_CONFIG 0x20
|
||||
#define PS2_CMD_WRITE_CONFIG 0x60
|
||||
#define PS2_CMD_DISABLE_PORT2 0xA7
|
||||
#define PS2_CMD_ENABLE_PORT2 0xA8
|
||||
#define PS2_CMD_TEST_PORT2 0xA9
|
||||
#define PS2_CMD_SELF_TEST 0xAA
|
||||
#define PS2_CMD_TEST_PORT1 0xAB
|
||||
#define PS2_CMD_DISABLE_PORT1 0xAD
|
||||
#define PS2_CMD_ENABLE_PORT1 0xAE
|
||||
#define PS2_CMD_WRITE_PORT2 0xD4
|
||||
|
||||
#define PS2_CFG_PORT1_IRQ (1 << 0)
|
||||
#define PS2_CFG_PORT2_IRQ (1 << 1)
|
||||
#define PS2_CFG_PORT1_XLAT (1 << 6)
|
||||
|
||||
#define PS2_KEY_RELEASE_BIT 0x80
|
||||
|
||||
#define SC_LSHIFT 0x2A
|
||||
#define SC_RSHIFT 0x36
|
||||
#define SC_CAPS 0x3A
|
||||
#define SC_LCTRL 0x1D
|
||||
#define SC_LALT 0x38
|
||||
#define SC_ESCAPE 0x01
|
||||
#define SC_F1 0x3B
|
||||
#define SC_F2 0x3C
|
||||
#define SC_F3 0x3D
|
||||
#define SC_F4 0x3E
|
||||
#define SC_F5 0x3F
|
||||
#define SC_F6 0x40
|
||||
#define SC_F7 0x41
|
||||
#define SC_F8 0x42
|
||||
#define SC_F9 0x43
|
||||
#define SC_F10 0x44
|
||||
#define SC_F11 0x57
|
||||
#define SC_F12 0x58
|
||||
|
||||
#define MOUSE_BTN_LEFT (1 << 0)
|
||||
#define MOUSE_BTN_RIGHT (1 << 1)
|
||||
#define MOUSE_BTN_MIDDLE (1 << 2)
|
||||
#define MOUSE_X_SIGN (1 << 4)
|
||||
#define MOUSE_Y_SIGN (1 << 5)
|
||||
#define MOUSE_X_OVERFLOW (1 << 6)
|
||||
#define MOUSE_Y_OVERFLOW (1 << 7)
|
||||
|
||||
typedef enum {
|
||||
MOUSE_SCROLL_NONE = 0,
|
||||
MOUSE_SCROLL_UP,
|
||||
MOUSE_SCROLL_DOWN,
|
||||
} mouse_scroll_t;
|
||||
|
||||
typedef struct {
|
||||
int32_t x, y;
|
||||
bool btn_left, btn_right, btn_middle;
|
||||
mouse_scroll_t scroll;
|
||||
} mouse_state_t;
|
||||
|
||||
typedef struct {
|
||||
bool shift, caps_lock, ctrl, alt;
|
||||
} kb_state_t;
|
||||
|
||||
#define KB_BUF_SIZE 64
|
||||
|
||||
typedef struct {
|
||||
char buf[KB_BUF_SIZE];
|
||||
uint8_t head, tail;
|
||||
} kb_buf_t;
|
||||
|
||||
bool ps2_init(void);
|
||||
const kb_state_t* ps2_kb_get_state(void);
|
||||
const mouse_state_t* ps2_mouse_get_state(void);
|
||||
bool kb_buf_empty(void);
|
||||
char kb_buf_getc(void);
|
||||
bool kb_buf_try_getc(char *out);
|
||||
bool kb_buf_has_ctrlc(void);
|
||||
void kb_buf_consume_ctrlc(void);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef TIMER_H
|
||||
#define TIMER_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
bool timer_init(void);
|
||||
|
||||
uint64_t timer_get_ticks(void);
|
||||
|
||||
void timer_sleep_ms(uint64_t milliseconds);
|
||||
void timer_sleep_us(uint64_t microseconds);
|
||||
void timer_sleep_ns(uint64_t nanoseconds);
|
||||
|
||||
extern volatile uint32_t g_ctrlc_pending;
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user