wrmsr.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* wrmsr.c - Write CPU model-specific registers. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2019 Free Software Foundation, Inc.
  5. * Based on gcc/gcc/config/i386/driver-i386.c
  6. *
  7. * GRUB is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * GRUB is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <grub/dl.h>
  21. #include <grub/misc.h>
  22. #include <grub/mm.h>
  23. #include <grub/env.h>
  24. #include <grub/command.h>
  25. #include <grub/extcmd.h>
  26. #include <grub/lockdown.h>
  27. #include <grub/i18n.h>
  28. #include <grub/i386/cpuid.h>
  29. #include <grub/i386/wrmsr.h>
  30. GRUB_MOD_LICENSE("GPLv3+");
  31. static grub_command_t cmd_write;
  32. static grub_err_t
  33. grub_cmd_msr_write (grub_command_t cmd __attribute__ ((unused)), int argc, char **argv)
  34. {
  35. grub_uint32_t manufacturer[3], max_cpuid, a, b, c, features, addr;
  36. grub_uint64_t value;
  37. const char *ptr;
  38. /*
  39. * The CPUID instruction should be used to determine whether MSRs
  40. * are supported. (CPUID.01H:EDX[5] = 1)
  41. */
  42. if (!grub_cpu_is_cpuid_supported ())
  43. return grub_error (GRUB_ERR_BUG, N_("unsupported instruction"));
  44. grub_cpuid (0, max_cpuid, manufacturer[0], manufacturer[2], manufacturer[1]);
  45. if (max_cpuid < 1)
  46. return grub_error (GRUB_ERR_BUG, N_("unsupported instruction"));
  47. grub_cpuid (1, a, b, c, features);
  48. if (!(features & (1 << 5)))
  49. return grub_error (GRUB_ERR_BUG, N_("unsupported instruction"));
  50. if (argc != 2)
  51. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("two arguments expected"));
  52. grub_errno = GRUB_ERR_NONE;
  53. ptr = argv[0];
  54. addr = grub_strtoul (ptr, &ptr, 0);
  55. if (grub_errno != GRUB_ERR_NONE)
  56. return grub_errno;
  57. if (*ptr != '\0')
  58. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument"));
  59. ptr = argv[1];
  60. value = grub_strtoull (ptr, &ptr, 0);
  61. if (grub_errno != GRUB_ERR_NONE)
  62. return grub_errno;
  63. if (*ptr != '\0')
  64. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("invalid argument"));
  65. grub_msr_write (addr, value);
  66. return GRUB_ERR_NONE;
  67. }
  68. GRUB_MOD_INIT(wrmsr)
  69. {
  70. cmd_write = grub_register_command_lockdown ("wrmsr", grub_cmd_msr_write, N_("ADDR VALUE"),
  71. N_("Write a value to a CPU model specific register."));
  72. }
  73. GRUB_MOD_FINI(wrmsr)
  74. {
  75. grub_unregister_command (cmd_write);
  76. }