1212#include <heap.h>
1313
1414ahci_hba_mem_t * global_ahci_ctrl ;
15+ ahci_disk_info_t ahci_disks [32 ];
1516
1617void detect_ahci_devices (ahci_hba_mem_t * ahci_ctrl ) {
1718 global_ahci_ctrl = ahci_ctrl ;
@@ -33,6 +34,8 @@ void detect_ahci_devices(ahci_hba_mem_t* ahci_ctrl) {
3334
3435 uint32_t sig = port -> sig ;
3536
37+ printf ("port->sig = 0x%x" , port -> sig );
38+
3639 switch (sig ) {
3740 case sata_disk :
3841 printf ("[AHCI] SATA Disk detected on port %d" , i );
@@ -58,17 +61,28 @@ void detect_ahci_devices(ahci_hba_mem_t* ahci_ctrl) {
5861void handle_sata_disk (int portno ) {
5962 ahci_init_port (portno );
6063
61- uint8_t * buf = kmalloc_aligned (512 , 512 );
62- memset (buf , 0 , 512 );
64+ int16 * id = kmalloc_aligned (512 , 4096 );
65+
66+ if (!id ) {
67+ error ("[AHCI] Allocation failed!" , __FILE__ );
68+ return ;
69+ }
6370
64- if (ahci_read_sector (portno , 0 , buf , 1 ) != 0 ) {
65- printf ("[AHCI] Read LBA failed on port %d\n " , portno );
71+ if (ahci_identify (portno , id ) != 0 ) {
72+ printf ("[AHCI] IDENTIFY failed on port %d" , portno );
6673 return ;
6774 }
6875
69- for (int i = 0 ; i < 32 ; i ++ ) printfnoln ("%c" , buf [i ]);
76+ uint64_t sectors =
77+ ((uint64_t )id [103 ] << 48 ) |
78+ ((uint64_t )id [102 ] << 32 ) |
79+ ((uint64_t )id [101 ] << 16 ) |
80+ ((uint64_t )id [100 ]);
81+
82+ ahci_disks [portno ].total_sectors = sectors ;
83+ ahci_disks [portno ].present = 1 ;
7084
71- print ( "\n" );
85+ check_mbr ( portno );
7286
7387 // char* msg = "FROSTWING WAS HERE";
7488 // memset(buf, 0, 512);
@@ -77,13 +91,13 @@ void handle_sata_disk(int portno) {
7791 // uint64_t write_lba = 2; // or 10 for testing
7892
7993 // if (ahci_write_sector(portno, 0, buf, 1) != 0) {
80- // printf("[AHCI] Write LBA failed\n ");
94+ // printf("[AHCI] Write LBA failed");
8195 // return;
8296 // }
8397
8498 // memset(buf, 0, 512);
8599 // if (ahci_read_sector(portno, 0, buf, 1) != 0) {
86- // printf("[AHCI] Read LBA failed\n ");
100+ // printf("[AHCI] Read LBA failed");
87101 // return;
88102 // }
89103
@@ -240,3 +254,54 @@ int ahci_write_sector(int portno, uint64_t lba, void* buffer, uint32_t count) {
240254 port -> is = 0xFFFFFFFF ;
241255 return 0 ;
242256}
257+
258+ int ahci_identify (int portno , void * buffer ) {
259+ ahci_port_t * port = & global_ahci_ctrl -> ports [portno ];
260+ ahci_port_mem_t * mem = & port_mem [portno ];
261+
262+ // Wait until port is ready
263+ while (port -> tfd & (0x80 | 0x08 )); // BSY | DRQ
264+
265+ int slot = ahci_find_slot (port );
266+ if (slot == -1 ) return -1 ;
267+
268+ ahci_cmd_header_t * hdr = & mem -> cmd_list [slot ];
269+ memset (hdr , 0 , sizeof (* hdr ));
270+ hdr -> flags = 5 ;
271+ hdr -> prdtl = 1 ; // one PRDT entry
272+ hdr -> ctba = (uint32_t )(uintptr_t )mem -> cmd_tables [slot ];
273+ hdr -> ctbau = 0 ;
274+
275+
276+ ahci_cmd_table_t * tbl = mem -> cmd_tables [slot ];
277+ memset (tbl -> cfis , 0 , 64 );
278+ memset (tbl -> prdt , 0 , sizeof (tbl -> prdt ));
279+
280+
281+ tbl -> prdt [0 ].dba = (uint32_t )(uintptr_t )buffer ;
282+ tbl -> prdt [0 ].dbau = 0 ;
283+ tbl -> prdt [0 ].dbc = (512 - 1 ) | (1 << 31 ); // 512 bytes, IOC
284+
285+ uint8_t * cfis = tbl -> cfis ;
286+ cfis [0 ] = 0x27 ; // Host to device
287+ cfis [1 ] = 1 << 7 ; // Command
288+ cfis [2 ] = ATA_CMD_IDENTIFY ; // IDENTIFY DEVICE
289+ cfis [7 ] = 0 ; // Features = 0
290+ cfis [8 ] = 0 ; cfis [9 ] = 0 ; cfis [10 ] = 0 ; // LBA = 0
291+
292+ port -> serr = 0xFFFFFFFF ;
293+ port -> is = 0xFFFFFFFF ;
294+
295+ port -> ci = 1 << slot ;
296+
297+ // Wait for completion
298+ while (port -> ci & (1 << slot )) {
299+ if (port -> tfd & (0x01 | 0x20 )) { // ERR | DF
300+ port -> is = 0xFFFFFFFF ;
301+ return -2 ;
302+ }
303+ }
304+
305+ port -> is = 0xFFFFFFFF ;
306+ return 0 ;
307+ }
0 commit comments