mmap.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/memory.h>
  19. #include <grub/ieee1275/ieee1275.h>
  20. #include <grub/types.h>
  21. grub_err_t
  22. grub_machine_mmap_iterate (grub_memory_hook_t hook, void *hook_data)
  23. {
  24. grub_ieee1275_phandle_t root;
  25. grub_ieee1275_phandle_t memory;
  26. grub_uint32_t available[128];
  27. grub_ssize_t available_size;
  28. grub_uint32_t address_cells = 1;
  29. grub_uint32_t size_cells = 1;
  30. int i;
  31. /* Determine the format of each entry in `available'. */
  32. grub_ieee1275_finddevice ("/", &root);
  33. grub_ieee1275_get_integer_property (root, "#address-cells", &address_cells,
  34. sizeof address_cells, 0);
  35. grub_ieee1275_get_integer_property (root, "#size-cells", &size_cells,
  36. sizeof size_cells, 0);
  37. if (size_cells > address_cells)
  38. address_cells = size_cells;
  39. /* Load `/memory/available'. */
  40. if (grub_ieee1275_finddevice ("/memory", &memory))
  41. return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
  42. "couldn't find /memory node");
  43. if (grub_ieee1275_get_integer_property (memory, "available", available,
  44. sizeof available, &available_size))
  45. return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
  46. "couldn't examine /memory/available property");
  47. if (available_size < 0 || (grub_size_t) available_size > sizeof (available))
  48. return grub_error (GRUB_ERR_UNKNOWN_DEVICE,
  49. "/memory response buffer exceeded");
  50. if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_BROKEN_ADDRESS_CELLS))
  51. {
  52. address_cells = 1;
  53. size_cells = 1;
  54. }
  55. /* Decode each entry and call `hook'. */
  56. i = 0;
  57. available_size /= sizeof (grub_uint32_t);
  58. while (i < available_size)
  59. {
  60. grub_uint64_t address;
  61. grub_uint64_t size;
  62. address = available[i++];
  63. if (address_cells == 2)
  64. address = (address << 32) | available[i++];
  65. size = available[i++];
  66. if (size_cells == 2)
  67. size = (size << 32) | available[i++];
  68. if (hook (address, size, GRUB_MEMORY_AVAILABLE, hook_data))
  69. break;
  70. }
  71. return grub_errno;
  72. }