init.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2023 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/env.h>
  19. #include <grub/kernel.h>
  20. #include <grub/misc.h>
  21. #include <grub/mm.h>
  22. #include <grub/time.h>
  23. #include <grub/efi/efi.h>
  24. #include <grub/loader.h>
  25. #define EFI_TIMER_PERIOD_MILLISECONDS(ms) ((grub_uint64_t)(ms * 10000))
  26. static grub_uint64_t tmr;
  27. static grub_efi_event_t tmr_evt;
  28. static grub_uint64_t
  29. grub_efi_get_time_ms (void)
  30. {
  31. return tmr;
  32. }
  33. static void
  34. grub_loongson_increment_timer (grub_efi_event_t event __attribute__ ((unused)),
  35. void *context __attribute__ ((unused)))
  36. {
  37. tmr += 10;
  38. }
  39. void
  40. grub_machine_init (void)
  41. {
  42. grub_efi_boot_services_t *b;
  43. grub_efi_init ();
  44. b = grub_efi_system_table->boot_services;
  45. b->create_event (GRUB_EFI_EVT_TIMER | GRUB_EFI_EVT_NOTIFY_SIGNAL,
  46. GRUB_EFI_TPL_CALLBACK, grub_loongson_increment_timer, NULL, &tmr_evt);
  47. b->set_timer (tmr_evt, GRUB_EFI_TIMER_PERIODIC, EFI_TIMER_PERIOD_MILLISECONDS(10));
  48. grub_install_get_time_ms (grub_efi_get_time_ms);
  49. }
  50. void
  51. grub_machine_fini (int flags)
  52. {
  53. grub_efi_boot_services_t *b;
  54. if (!(flags & GRUB_LOADER_FLAG_NORETURN))
  55. return;
  56. b = grub_efi_system_table->boot_services;
  57. b->set_timer (tmr_evt, GRUB_EFI_TIMER_CANCEL, 0);
  58. b->close_event (tmr_evt);
  59. grub_efi_fini ();
  60. if (!(flags & GRUB_LOADER_FLAG_EFI_KEEP_ALLOCATED_MEMORY))
  61. grub_efi_memory_fini ();
  62. }