e820_memory_layout.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * QEMU BIOS e820 routines
  3. *
  4. * Copyright (c) 2003-2004 Fabrice Bellard
  5. *
  6. * SPDX-License-Identifier: MIT
  7. */
  8. #include "qemu/osdep.h"
  9. #include "qemu/bswap.h"
  10. #include "e820_memory_layout.h"
  11. static size_t e820_entries;
  12. struct e820_table e820_reserve;
  13. struct e820_entry *e820_table;
  14. int e820_add_entry(uint64_t address, uint64_t length, uint32_t type)
  15. {
  16. int index = le32_to_cpu(e820_reserve.count);
  17. struct e820_entry *entry;
  18. if (type != E820_RAM) {
  19. /* old FW_CFG_E820_TABLE entry -- reservations only */
  20. if (index >= E820_NR_ENTRIES) {
  21. return -EBUSY;
  22. }
  23. entry = &e820_reserve.entry[index++];
  24. entry->address = cpu_to_le64(address);
  25. entry->length = cpu_to_le64(length);
  26. entry->type = cpu_to_le32(type);
  27. e820_reserve.count = cpu_to_le32(index);
  28. }
  29. /* new "etc/e820" file -- include ram too */
  30. e820_table = g_renew(struct e820_entry, e820_table, e820_entries + 1);
  31. e820_table[e820_entries].address = cpu_to_le64(address);
  32. e820_table[e820_entries].length = cpu_to_le64(length);
  33. e820_table[e820_entries].type = cpu_to_le32(type);
  34. e820_entries++;
  35. return e820_entries;
  36. }
  37. int e820_get_num_entries(void)
  38. {
  39. return e820_entries;
  40. }
  41. bool e820_get_entry(int idx, uint32_t type, uint64_t *address, uint64_t *length)
  42. {
  43. if (idx < e820_entries && e820_table[idx].type == cpu_to_le32(type)) {
  44. *address = le64_to_cpu(e820_table[idx].address);
  45. *length = le64_to_cpu(e820_table[idx].length);
  46. return true;
  47. }
  48. return false;
  49. }