summaryrefslogtreecommitdiff
path: root/kernel/src/apic/rsdp.c
blob: ebecd6636f56814a8950670af10d70a4da93154b (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
#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;
}

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;
}