xref: /openbmc/qemu/hw/i386/e820_memory_layout.c (revision b9c0a2e0)
1 /*
2  * QEMU BIOS e820 routines
3  *
4  * Copyright (c) 2003-2004 Fabrice Bellard
5  *
6  * SPDX-License-Identifier: MIT
7  */
8 
9 #include "qemu/osdep.h"
10 #include "qemu/bswap.h"
11 #include "e820_memory_layout.h"
12 
13 static size_t e820_entries;
14 static struct e820_entry *e820_table;
15 static gboolean e820_done;
16 
17 void e820_add_entry(uint64_t address, uint64_t length, uint32_t type)
18 {
19     assert(!e820_done);
20 
21     /* new "etc/e820" file -- include ram and reserved entries */
22     e820_table = g_renew(struct e820_entry, e820_table, e820_entries + 1);
23     e820_table[e820_entries].address = cpu_to_le64(address);
24     e820_table[e820_entries].length = cpu_to_le64(length);
25     e820_table[e820_entries].type = cpu_to_le32(type);
26     e820_entries++;
27 }
28 
29 int e820_get_table(struct e820_entry **table)
30 {
31     e820_done = true;
32 
33     if (table) {
34         *table = e820_table;
35     }
36 
37     return e820_entries;
38 }
39 
40 bool e820_get_entry(int idx, uint32_t type, uint64_t *address, uint64_t *length)
41 {
42     if (idx < e820_entries && e820_table[idx].type == cpu_to_le32(type)) {
43         *address = le64_to_cpu(e820_table[idx].address);
44         *length = le64_to_cpu(e820_table[idx].length);
45         return true;
46     }
47     return false;
48 }
49