diff --git a/usr/src/sp1/common/bpt/bpt.c b/usr/src/sp1/common/bpt/bpt.c index cc57466..9f99b71 100644 --- a/usr/src/sp1/common/bpt/bpt.c +++ b/usr/src/sp1/common/bpt/bpt.c @@ -36,6 +36,20 @@ bpt_get_protovar(struct bpt_protovar *res) return ops.get_protovar(res); } +status_t +bpt_mem_entry_i(size_t index, struct mem_entry *res) +{ + if (res == NULL) { + return STATUS_INVALID_PARAM; + } + + if (ops.mem_entry_i == NULL) { + return STATUS_IO_ERROR; + } + + return ops.mem_entry_i(index, res); +} + status_t bpt_init(void) { diff --git a/usr/src/sp1/common/bpt/bpt_limine.c b/usr/src/sp1/common/bpt/bpt_limine.c index fb195bb..82e407a 100644 --- a/usr/src/sp1/common/bpt/bpt_limine.c +++ b/usr/src/sp1/common/bpt/bpt_limine.c @@ -13,6 +13,13 @@ #include #include +/* Memory map request */ +static struct limine_memmap_response *memmap_resp = NULL; +static volatile struct limine_memmap_request memmap_req = { + .id = LIMINE_MEMMAP_REQUEST, + .revision = 0 +}; + /* HHDM request */ static struct limine_hhdm_response *hhdm_resp = NULL; static volatile struct limine_hhdm_request hhdm_req = { @@ -36,6 +43,34 @@ limine_get_protovar(struct bpt_protovar *res) return STATUS_SUCCESS; } +/* + * Obtain a memory map entry by index + * + * @index: Index of memory map entry to obtain + * @res: Result is written here + */ +static status_t +limine_mem_entry_i(size_t index, struct mem_entry *res) +{ + struct limine_memmap_entry *entry; + + if (res == NULL) { + return STATUS_INVALID_PARAM; + } + + /* Don't overflow */ + if (index >= memmap_resp->entry_count) { + return STATUS_NOT_FOUND; + } + + /* 1:1 */ + entry = memmap_resp->entries[index]; + res->base = entry->base; + res->length = entry->length; + res->type = entry->type; + return STATUS_SUCCESS; +} + status_t bpt_init_limine(struct bpt_ops *ops) { @@ -44,6 +79,9 @@ bpt_init_limine(struct bpt_ops *ops) } hhdm_resp = hhdm_req.response; + memmap_resp = memmap_req.response; + ops->get_protovar = limine_get_protovar; + ops->mem_entry_i = limine_mem_entry_i; return STATUS_SUCCESS; } diff --git a/usr/src/sp1/head/os/bpt.h b/usr/src/sp1/head/os/bpt.h index ebc5981..43bbc86 100644 --- a/usr/src/sp1/head/os/bpt.h +++ b/usr/src/sp1/head/os/bpt.h @@ -15,6 +15,34 @@ #include #include +/* + * Memory map entry types + */ +typedef enum { + MEMORY_USABLE, + MEMORY_RESERVED, + MEMORY_ACPI_RECLAIM, + MEMORY_ACPI_NVS, + MEMORY_BAD, + MEMORY_BOOTLOADER, + MEMORY_KERNEL, + MEMORY_FRAMEBUFFER, + MEMORY_ACPI_TABLES +} mem_type_t; + +/* + * Represents a memory map entry + * + * @base: Entry base address + * @length: Entry length + * @type: Entry type + */ +struct mem_entry { + uintptr_t base; + size_t length; + mem_type_t type; +}; + /* * The protovar stores a list of information passed * by the bootloader. @@ -30,6 +58,7 @@ struct bpt_protovar { */ struct bpt_ops { status_t(*get_protovar)(struct bpt_protovar *res); + status_t(*mem_entry_i)(size_t index, struct mem_entry *res); }; /* @@ -39,6 +68,13 @@ struct bpt_ops { */ status_t bpt_get_protovar(struct bpt_protovar *res); +/* + * Obtain a memory map entry by index + * + * Returns STATUS_NOT_FOUND on failure + */ +status_t bpt_mem_entry_i(size_t index, struct mem_entry *res); + /* * Initialize the boot protocol translation layer */