feat: probe more device info
[lunaix-os.git] / lunaix-os / hal / ahci / utils.c
1 #include <hal/ahci/hba.h>
2 #include <hal/ahci/utils.h>
3 #include <klibc/string.h>
4
5 #define IDDEV_OFFMAXLBA 60
6 #define IDDEV_OFFMAXLBA_EXT 230
7 #define IDDEV_OFFLSECSIZE 117
8 #define IDDEV_OFFWWN 108
9 #define IDDEV_OFFSERIALNUM 10
10 #define IDDEV_OFFMODELNUM 27
11 #define IDDEV_OFFADDSUPPORT 69
12 #define IDDEV_OFFALIGN 209
13 #define IDDEV_OFFLPP 106
14 #define IDDEV_OFFCAPABILITIES 49
15
16 void
17 ahci_parse_dev_info(struct hba_device* dev_info, uint16_t* data)
18 {
19     dev_info->max_lba = *((uint32_t*)(data + IDDEV_OFFMAXLBA));
20     dev_info->block_size = *((uint32_t*)(data + IDDEV_OFFLSECSIZE));
21     dev_info->cbd_size = (*data & 0x3) ? 16 : 12;
22     dev_info->wwn = *(uint64_t*)(data + IDDEV_OFFWWN);
23     dev_info->block_per_sec = 1 << (*(data + IDDEV_OFFLPP) & 0xf);
24     dev_info->alignment_offset = *(data + IDDEV_OFFALIGN) & 0x3fff;
25     dev_info->capabilities = *((uint32_t*)(data + IDDEV_OFFCAPABILITIES));
26
27     if (!dev_info->block_size) {
28         dev_info->block_size = 512;
29     }
30
31     if ((*(data + IDDEV_OFFADDSUPPORT) & 0x8)) {
32         dev_info->max_lba = *((uint64_t*)(data + IDDEV_OFFMAXLBA_EXT));
33         dev_info->flags |= HBA_DEV_FEXTLBA;
34     }
35
36     ahci_parsestr(&dev_info->serial_num, data + IDDEV_OFFSERIALNUM, 10);
37     ahci_parsestr(&dev_info->model, data + IDDEV_OFFMODELNUM, 20);
38 }
39
40 void
41 ahci_parsestr(char* str, uint16_t* reg_start, int size_word)
42 {
43     int j = 0;
44     for (int i = 0; i < size_word; i++, j += 2) {
45         uint16_t reg = *(reg_start + i);
46         str[j] = (char)(reg >> 8);
47         str[j + 1] = (char)(reg & 0xff);
48     }
49     str[j - 1] = '\0';
50 }