FrostWing
A lightweight raw-control operating system.
Loading...
Searching...
No Matches
ahci.h
Go to the documentation of this file.
10
11#ifndef AHCI_H
12#define AHCI_H
13
14#include <basics.h>
15#include <graphics.h>
16
17#define AHCI_PORT_DET_PRESENT 3
18#define AHCI_PORT_IPM_ACTIVE 1
19#define AHCI_PORT_CMD_ST (1 << 0)
20#define AHCI_PORT_CMD_FRE (1 << 4)
21#define AHCI_PORT_CMD_FR (1 << 14)
22#define AHCI_PORT_CMD_CR (1 << 15)
23#define READ_DMA_EXT 0x25
24#define SECTOR_SIZE 512
25
26#define ATA_CMD_WRITE_DMA_EXT 0x35
27#define ATA_CMD_IDENTIFY 0xEC
28
29#define MAX_PARTITIONS 128
30#define MAX_BLOCK_DEVICES 64
31
32#define AHCI_MAX_PRDT 16 // 8,16 OR 32
33#define PRDT_MAX_BYTES (4 * 1024 * 1024) // 4 KiB
34
35
39#define sata_disk 0x00000101
40#define satapi_disk 0xEB140101
41#define semb_disk 0xC33C0101
42#define port_multiplier 0x96690101
43
49typedef struct __attribute__((packed)) {
50 /* first 16-bit flags */
51 uint16_t flags; /* contains CFL(5), A, W, P, R, B, C, rsv0, PMP(4) */
52 uint16_t prdtl; /* PRDT length (entries) */
53 uint32_t prdbc; /* PRDT byte count transferred (updated by HBA) */
54 uint32_t ctba; /* Command table descriptor base address (lower 32) */
55 uint32_t ctbau; /* Command table descriptor base address upper 32 */
56 uint32_t rsv[4];
57} ahci_cmd_header_t;
58
59#define AHCI_CMD_HDR_CFL_MASK 0x001F /* bits 0-4 */
60#define AHCI_CMD_HDR_A_BIT 0x0020
61#define AHCI_CMD_HDR_W_BIT 0x0040
62#define AHCI_CMD_HDR_P_BIT 0x0080
63#define AHCI_CMD_HDR_R_BIT 0x0100
64#define AHCI_CMD_HDR_B_BIT 0x0200
65#define AHCI_CMD_HDR_C_BIT 0x0400
66#define AHCI_CMD_HDR_PMP_MASK 0xF000 /* bits 12-15 */
67
71 typedef struct __attribute__((packed)) {
72 uint32_t dba; /* Data base address (lower 32) */
73 uint32_t dbau; /* Data base address upper 32 (if S64A supported) */
74 uint32_t reserved;
75 uint32_t dbc; /* DW3: byte count and flags (use macros) */
76} prdt_entry_t;
77
78
82 typedef struct __attribute__((packed)) {
83 uint8_t cfis[64];
84 uint8_t acmd[16];
85 uint8_t reserved[48];
86 prdt_entry_t prdt[AHCI_MAX_PRDT]; /* flexible / variable sized in allocation */
87} ahci_cmd_table_t;
88
89typedef struct {
90 ahci_cmd_header_t* cmd_list;
91 ahci_cmd_table_t* cmd_tables[32];
92 void* fis;
94
95static ahci_port_mem_t port_mem[32];
96
97
101 typedef volatile struct {
102 uint32_t clb; /* 0x00 Command list base address (lower 32) */
103 uint32_t clbu; /* 0x04 Command list base address upper 32 */
104 uint32_t fb; /* 0x08 FIS base address (lower 32) */
105 uint32_t fbu; /* 0x0C FIS base address upper 32 */
106 uint32_t is; /* 0x10 Interrupt status */
107 uint32_t ie; /* 0x14 Interrupt enable */
108 uint32_t cmd; /* 0x18 Command and status */
109 uint32_t rsv0; /* 0x1C Reserved */
110 uint32_t tfd; /* 0x20 Task file data */
111 uint32_t sig; /* 0x24 Signature */
112 uint32_t ssts; /* 0x28 SATA status (SStatus) */
113 uint32_t sctl; /* 0x2C SATA control (SControl) */
114 uint32_t serr; /* 0x30 SATA error (SError) */
115 uint32_t sact; /* 0x34 SATA active (SActive) */
116 uint32_t ci; /* 0x38 Command issue */
117 uint32_t sntf; /* 0x3C SATA notification (SNotification) */
118 uint32_t fbs; /* 0x40 FIS-based switching */
119 uint32_t rsv1[11]; /* 0x44 - 0x6F reserved */
120 uint32_t vendor[4]; /* 0x70 - 0x7F vendor specific */
122
123typedef volatile struct {
124 uint32_t cap; /* 0x00 Host capabilities (CAP) */
125 uint32_t ghc; /* 0x04 Global host control (GHC) */
126 uint32_t is; /* 0x08 Interrupt status */
127 uint32_t pi; /* 0x0C Ports implemented */
128 uint32_t vs; /* 0x10 Version */
129 uint32_t ccc_ctl; /* 0x14 Command completion coalescing control */
130 uint32_t ccc_pts; /* 0x18 Command completion ports */
131 uint32_t em_loc; /* 0x1C Enclosure management location */
132 uint32_t em_ctl; /* 0x20 Enclosure management control */
133 uint32_t cap2; /* 0x24 Host capabilities extended */
134 uint32_t bohc; /* 0x28 BIOS/OS handoff control and status */
135 uint8_t rsv[0xA0 - 0x2C]; /* reserved bytes up to vendor area */
136 uint8_t vendor[0x100 - 0xA0];/* vendor specific area */
137 /* 0x100 - start of port control registers (ports[0] starts here) */
138 ahci_port_t ports[32]; /* array of port structures (0x100.. ) */
140
141typedef struct {
142 uint64_t total_sectors;
143 int present;
144 int logical_device;
146
147typedef enum {
148 BLOCK_DEVICE_AHCI = 0,
149 BLOCK_DEVICE_NVME
150} block_device_type_t;
151
152typedef struct {
153 block_device_type_t type;
154 int present;
155 int backend_index;
156 uint32_t sector_size;
157 uint64_t total_sectors;
158 char name[32];
160
162
163typedef enum {
164 FS_UNKNOWN = 0,
165
166 // FAT family
167 FS_FAT12,
168 FS_FAT16,
169 FS_FAT32,
170 FS_EXFAT,
171
172 // Linux / Unix
173 FS_EXT2,
174 FS_EXT3,
175 FS_EXT4,
176 FS_XFS,
177 FS_BTRFS,
178
179 // Other common FS
180 FS_NTFS,
181 FS_ISO9660,
182 FS_UDF,
183
184 // OS / Custom
185 FS_PROC,
186 FS_DEV
188
189
190typedef enum {
191 PART_TABLE_MBR,
192 PART_TABLE_GPT
193} partition_table_type_t;
194
195typedef struct {
196 partition_table_type_t table_type;
197
198 // Common geometry
199 int64 lba_start;
200 int64 lba_end;
201 int64 sector_count;
202 int64 ahci_port;
203
204 // Bootable?
205 bool bootable;
206
207 // Filesystem info
208 partition_fs_type_t fs_type;
209
210 // Original on-disk identifiers (optional but useful)
211 union {
212 uint8_t mbr_type; // raw MBR partition type
213 uint8_t gpt_type_guid[16]; // raw GPT type GUID
214 };
215
216 char name[64]; // UTF-8, converted from GPT UTF-16 if needed
218
219typedef struct mount_entry {
220 char* mount_point;
221 char* part_name;
223 void* fs;
224} mount_entry_t;
225
226// END
227
228extern ahci_disk_info_t ahci_disks[32];
229
230extern general_partition_t ahci_partitions[MAX_PARTITIONS];
231extern mount_entry_t mounted_partitions[MAX_PARTITIONS];
232extern block_device_info_t block_devices[MAX_BLOCK_DEVICES];
233extern int general_partition_count;
234extern int mounted_partition_count;
235extern int block_device_count;
236
241
248void handle_sata_disk(int portno);
249void handle_satapi_disk(int portno);
250void ahci_init_port(int portno);
251int ahci_read_sector(int portno, uint64_t lba, void* buffer, uint32_t count);
252int ahci_write_sector(int portno, uint64_t lba, void* buffer, uint32_t count);
253int ahci_identify(int portno, void* buffer);
254
255int block_register_device(
256 block_device_type_t type,
257 int backend_index,
258 uint64_t total_sectors,
259 uint32_t sector_size,
260 const char* name
261);
262block_device_info_t* block_get_device(int device_id);
263const char* block_get_device_name(int device_id);
264int block_read_sector(int device_id, uint64_t lba, void* buffer, uint32_t count);
265int block_write_sector(int device_id, uint64_t lba, void* buffer, uint32_t count);
266
267general_partition_t* add_general_partition(
268 partition_table_type_t table_type,
269 int64 lba_start,
270 int64 lba_end,
271 int64 sector_count,
272 int64 ahci_port,
273 bool bootable,
274 partition_fs_type_t fs_type,
275 cstring name,
276 uint8_t mbr_type,
277 const uint8_t* gpt_guid // must be 16 bytes
278);
279
280general_partition_t* search_general_partition(cstring partition_name);
281
282mount_entry_t* add_mount(const char* mount_point, const char* part_name, partition_fs_type_t type, void* fs_ptr);
283mount_entry_t* find_mount_by_point(const char* mount_point);
284
285#endif // AHCI_H
struct acpi_gas __attribute__
Physical Region Descriptor Table (PRDT) entry.
Definition ahci.h:71
partition_fs_type_t
GENERAL PARITION LAYOUT BEGIN.
Definition ahci.h:163
void detect_ahci_devices(ahci_hba_mem_t *ahci_ctrl)
Probes and detects all AHCI devices connected to the controller.
ahci_hba_mem_t * global_ahci_ctrl
Global AHCI controller pointer.
This is a basic header files with FrostWing specific short forms and basically a good for life header...
Contains all the print functions.
AHCI port registers and command header pointer.
Definition ahci.h:101
Definition ahci.h:219