xref: /openbmc/qemu/hw/i386/e820_memory_layout.c (revision 7f6c3d1a)
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 struct e820_table e820_reserve;
15 struct e820_entry *e820_table;
16 
17 int e820_add_entry(uint64_t address, uint64_t length, uint32_t type)
18 {
19     int index = le32_to_cpu(e820_reserve.count);
20     struct e820_entry *entry;
21 
22     if (type != E820_RAM) {
23         /* old FW_CFG_E820_TABLE entry -- reservations only */
24         if (index >= E820_NR_ENTRIES) {
25             return -EBUSY;
26         }
27         entry = &e820_reserve.entry[index++];
28 
29         entry->address = cpu_to_le64(address);
30         entry->length = cpu_to_le64(length);
31         entry->type = cpu_to_le32(type);
32 
33         e820_reserve.count = cpu_to_le32(index);
34     }
35 
36     /* new "etc/e820" file -- include ram too */
37     e820_table = g_renew(struct e820_entry, e820_table, e820_entries + 1);
38     e820_table[e820_entries].address = cpu_to_le64(address);
39     e820_table[e820_entries].length = cpu_to_le64(length);
40     e820_table[e820_entries].type = cpu_to_le32(type);
41     e820_entries++;
42 
43     return e820_entries;
44 }
45 
46 int e820_get_num_entries(void)
47 {
48     return e820_entries;
49 }
50 
51 bool e820_get_entry(int idx, uint32_t type, uint64_t *address, uint64_t *length)
52 {
53     if (idx < e820_entries && e820_table[idx].type == cpu_to_le32(type)) {
54         *address = le64_to_cpu(e820_table[idx].address);
55         *length = le64_to_cpu(e820_table[idx].length);
56         return true;
57     }
58     return false;
59 }
60