cmostest.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2009 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/dl.h>
  19. #include <grub/command.h>
  20. #include <grub/misc.h>
  21. #include <grub/cmos.h>
  22. GRUB_MOD_LICENSE ("GPLv3+");
  23. static grub_err_t
  24. parse_args (int argc, char *argv[], int *byte, int *bit)
  25. {
  26. char *rest;
  27. if (argc != 1)
  28. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Address required.");
  29. *byte = grub_strtoul (argv[0], &rest, 0);
  30. if (*rest != ':')
  31. return grub_error (GRUB_ERR_BAD_ARGUMENT, "Address required.");
  32. *bit = grub_strtoul (rest + 1, 0, 0);
  33. return GRUB_ERR_NONE;
  34. }
  35. static grub_err_t
  36. grub_cmd_cmostest (struct grub_command *cmd __attribute__ ((unused)),
  37. int argc, char *argv[])
  38. {
  39. int byte, bit;
  40. grub_err_t err;
  41. err = parse_args (argc, argv, &byte, &bit);
  42. if (err)
  43. return err;
  44. if (grub_cmos_read (byte) & (1 << bit))
  45. return GRUB_ERR_NONE;
  46. return grub_error (GRUB_ERR_TEST_FAILURE, "false");
  47. }
  48. static grub_err_t
  49. grub_cmd_cmosclean (struct grub_command *cmd __attribute__ ((unused)),
  50. int argc, char *argv[])
  51. {
  52. int byte, bit;
  53. grub_err_t err;
  54. err = parse_args (argc, argv, &byte, &bit);
  55. if (err)
  56. return err;
  57. grub_cmos_write (byte, grub_cmos_read (byte) & (~(1 << bit)));
  58. return GRUB_ERR_NONE;
  59. }
  60. static grub_command_t cmd, cmd_clean;
  61. GRUB_MOD_INIT(cmostest)
  62. {
  63. cmd = grub_register_command ("cmostest", grub_cmd_cmostest,
  64. "cmostest BYTE:BIT",
  65. "Test bit at BYTE:BIT in CMOS.");
  66. cmd_clean = grub_register_command ("cmosclean", grub_cmd_cmosclean,
  67. "cmosclean BYTE:BIT",
  68. "Clean bit at BYTE:BIT in CMOS.");
  69. }
  70. GRUB_MOD_FINI(cmostest)
  71. {
  72. grub_unregister_command (cmd);
  73. grub_unregister_command (cmd_clean);
  74. }