relocator.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010 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/relocator.h>
  19. #include <grub/relocator_private.h>
  20. #include <grub/memory.h>
  21. #include <grub/ieee1275/ieee1275.h>
  22. /* Helper for grub_relocator_firmware_get_max_events. */
  23. static int
  24. count (grub_uint64_t addr __attribute__ ((unused)),
  25. grub_uint64_t len __attribute__ ((unused)),
  26. grub_memory_type_t type __attribute__ ((unused)), void *data)
  27. {
  28. int *counter = data;
  29. (*counter)++;
  30. return 0;
  31. }
  32. unsigned
  33. grub_relocator_firmware_get_max_events (void)
  34. {
  35. int counter = 0;
  36. grub_machine_mmap_iterate (count, &counter);
  37. return 2 * counter;
  38. }
  39. /* Context for grub_relocator_firmware_fill_events. */
  40. struct grub_relocator_firmware_fill_events_ctx
  41. {
  42. struct grub_relocator_mmap_event *events;
  43. int counter;
  44. };
  45. /* Helper for grub_relocator_firmware_fill_events. */
  46. static int
  47. grub_relocator_firmware_fill_events_iter (grub_uint64_t addr,
  48. grub_uint64_t len,
  49. grub_memory_type_t type, void *data)
  50. {
  51. struct grub_relocator_firmware_fill_events_ctx *ctx = data;
  52. if (type != GRUB_MEMORY_AVAILABLE)
  53. return 0;
  54. if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_PRE1_5M_CLAIM))
  55. {
  56. if (addr + len <= 0x180000)
  57. return 0;
  58. if (addr < 0x180000)
  59. {
  60. len = addr + len - 0x180000;
  61. addr = 0x180000;
  62. }
  63. }
  64. ctx->events[ctx->counter].type = REG_FIRMWARE_START;
  65. ctx->events[ctx->counter].pos = addr;
  66. ctx->counter++;
  67. ctx->events[ctx->counter].type = REG_FIRMWARE_END;
  68. ctx->events[ctx->counter].pos = addr + len;
  69. ctx->counter++;
  70. return 0;
  71. }
  72. unsigned
  73. grub_relocator_firmware_fill_events (struct grub_relocator_mmap_event *events)
  74. {
  75. struct grub_relocator_firmware_fill_events_ctx ctx = {
  76. .events = events,
  77. .counter = 0
  78. };
  79. grub_machine_mmap_iterate (grub_relocator_firmware_fill_events_iter, &ctx);
  80. return ctx.counter;
  81. }
  82. int
  83. grub_relocator_firmware_alloc_region (grub_addr_t start, grub_size_t size)
  84. {
  85. grub_err_t err;
  86. err = grub_claimmap (start, size);
  87. grub_errno = 0;
  88. return (err == 0);
  89. }
  90. void
  91. grub_relocator_firmware_free_region (grub_addr_t start, grub_size_t size)
  92. {
  93. grub_ieee1275_release (start, size);
  94. }