mmap.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2003,2004,2005,2007,2008 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/machine/memory.h>
  19. #include <grub/ieee1275/ieee1275.h>
  20. #include <grub/types.h>
  21. #include <grub/misc.h>
  22. GRUB_EXPORT(grub_machine_mmap_iterate);
  23. grub_err_t
  24. grub_machine_mmap_iterate (int (*hook) (grub_uint64_t, grub_uint64_t,
  25. grub_uint32_t, void *closure),
  26. void *closure)
  27. {
  28. grub_ieee1275_phandle_t root;
  29. grub_ieee1275_phandle_t memory;
  30. grub_uint32_t available[32];
  31. grub_ssize_t available_size;
  32. grub_uint32_t address_cells = 1;
  33. grub_uint32_t size_cells = 1;
  34. char copyright[128];
  35. int i;
  36. /* Determine the format of each entry in `available'. */
  37. grub_ieee1275_finddevice ("/", &root);
  38. grub_ieee1275_get_integer_property (root, "#address-cells", &address_cells,
  39. sizeof address_cells, 0);
  40. grub_ieee1275_get_integer_property (root, "#size-cells", &size_cells,
  41. sizeof size_cells, 0);
  42. grub_ieee1275_get_property (root, "copyright", copyright,
  43. sizeof copyright, 0);
  44. if (size_cells > address_cells)
  45. address_cells = size_cells;
  46. /* Apple ppc g4, g5 /memory/available[] uses 32bit offset and size,
  47. including for >4GB fitted RAM,
  48. and does not use root cell sizes. Maybe some other ieee1275 Apple
  49. uses 64bit offset and size? */
  50. if (grub_strstr (copyright, "Apple"))
  51. address_cells = size_cells;
  52. /* Load `/memory/available'. */
  53. if (grub_ieee1275_finddevice ("/memory", &memory))
  54. return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
  55. "couldn't find /memory node");
  56. if (grub_ieee1275_get_integer_property (memory, "available", available,
  57. sizeof available, &available_size))
  58. return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
  59. "couldn't examine /memory/available property");
  60. /* Decode each entry and call `hook'. */
  61. i = 0;
  62. available_size /= sizeof (grub_uint32_t);
  63. while (i < available_size)
  64. {
  65. grub_uint64_t address;
  66. grub_uint64_t size;
  67. address = available[i++];
  68. if (address_cells == 2)
  69. address = (address << 32) | available[i++];
  70. size = available[i++];
  71. if (size_cells == 2)
  72. size = (size << 32) | available[i++];
  73. if (hook (address, size, GRUB_MACHINE_MEMORY_AVAILABLE, closure))
  74. break;
  75. }
  76. return grub_errno;
  77. }