summaryrefslogtreecommitdiff
path: root/kernel/src/apic/rsdp.c
blob: c12b63f6965908d3e1ed9d70a4d8f73ef6a422ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <rsdp.h>
#include <heap.h>
#include <paging.h>
#include <libk/string.h>
#include <libk/stdio.h>

uint64_t *find_rsdp()
{
	map_addr(0x0, 0x0, FLAG_PRESENT);
	const char *rsdp_cs = "RSD PTR ";
	for (uint64_t i = 0x10; i < 0x100000; i += 0x10) {
		char *x = (char *)i;
		uint8_t ind = 1;
		for (size_t j = 0; j < strlen(rsdp_cs); j++) {
			if (rsdp_cs[j] != x[j]) {
				ind = 0;
				break;
			}
		}
		if (ind) {
			return (uint64_t *)i;
		}
	}
	return NULL;
}

void list_sys_tables(void)
{
	uint64_t *rsdp = find_rsdp();

	if (rsdp == NULL) {
		printf("RSDP NOT FOUND\n");
		return;
	}

	struct RSDP_descriptor *rsdp_desc = (struct RSDP_descriptor *)rsdp;
	map_addr(rsdp_desc->RsdtAddress, rsdp_desc->RsdtAddress, FLAG_PRESENT);

	struct ACPI_header *rsdt =
		(struct ACPI_header *)kalloc(sizeof(struct ACPI_header));
	memcpy(rsdt, (uint64_t *)(uint64_t)rsdp_desc->RsdtAddress,
	       sizeof(struct ACPI_header));

	uint32_t entries =
		(rsdt->Length - (uint32_t)sizeof(struct ACPI_header)) / 4;

	for (size_t i = 0; i < entries; i++) {
		uint32_t na_addr = (uint32_t)rsdp_desc->RsdtAddress +
				   (uint32_t)sizeof(struct ACPI_header) +
				   (uint32_t)i * 4;
		uint32_t addr;
		memcpy(&addr, (uint64_t *)(uint64_t)na_addr, 4);

		struct ACPI_header *t = (struct ACPI_header *)kalloc(
			sizeof(struct ACPI_header));
		memcpy(t, (uint64_t *)(uint64_t)addr,
		       sizeof(struct ACPI_header));

		for (size_t j = 0; j < 4; j++) {
			printf("%c", t->Signature[j]);
		}
		printf(", ");

		kfree(t);
	}
	printf("\n");

	kfree(rsdt);
}

uint64_t *find_sys_table_addr(const char *signature)
{
	uint64_t *rsdp = find_rsdp();

	if (rsdp == NULL) {
		printf("RSDP NOT FOUND\n");
		return NULL;
	}

	struct RSDP_descriptor *rsdp_desc = (struct RSDP_descriptor *)rsdp;
	map_addr(rsdp_desc->RsdtAddress, rsdp_desc->RsdtAddress, FLAG_PRESENT);

	struct ACPI_header *rsdt =
		(struct ACPI_header *)kalloc(sizeof(struct ACPI_header));
	memcpy(rsdt, (uint64_t *)(uint64_t)rsdp_desc->RsdtAddress,
	       sizeof(struct ACPI_header));

	uint32_t entries =
		(rsdt->Length - (uint32_t)sizeof(struct ACPI_header)) / 4;

	for (size_t i = 0; i < entries; i++) {
		uint32_t na_addr = (uint32_t)rsdp_desc->RsdtAddress +
				   (uint32_t)sizeof(struct ACPI_header) +
				   (uint32_t)i * 4;
		uint32_t addr;
		memcpy(&addr, (uint64_t *)(uint64_t)na_addr, 4);

		struct ACPI_header *t = (struct ACPI_header *)kalloc(
			sizeof(struct ACPI_header));
		memcpy(t, (uint64_t *)(uint64_t)addr,
		       sizeof(struct ACPI_header));

		int ind = 1;
		for (size_t j = 0; j < 4; j++) {
			if (t->Signature[j] != signature[j])
				ind = 0;
		}
		if (ind) {
			kfree(t);
			kfree(rsdt);
			return (uint64_t *)(uint64_t)addr;
		}

		kfree(t);
	}

	kfree(rsdt);
	return NULL;
}