e6400_flash_unlock.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2023 Nicholas Chin
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. * IN THE SOFTWARE.
  21. */
  22. #include <sys/io.h>
  23. #include <sys/mman.h>
  24. #include <err.h>
  25. #include <errno.h>
  26. #include <fcntl.h>
  27. #include <stdint.h>
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. enum
  31. EC_FDO_CMD {
  32. QUERY = 0,
  33. SET_OVERRIDE = 2,
  34. UNSET_OVERRIDE = 3
  35. };
  36. int get_fdo_status(void);
  37. void ec_fdo_command(enum EC_FDO_CMD arg);
  38. void write_ec_reg(uint8_t index, uint8_t data);
  39. void send_ec_cmd(uint8_t cmd);
  40. void wait_ec(void);
  41. int get_gbl_smi_en(void);
  42. int set_gbl_smi_en(int enable);
  43. /* uint8_t read_ec_reg(uint8_t index); */
  44. #define EC_INDEX 0x910
  45. #define EC_DATA 0x911
  46. #define PMBASE 0x1000
  47. #define SMI_EN_REG (PMBASE + 0x30)
  48. /* Assume this is the same on all vendor BIOS versions */
  49. #define RCBA 0xfed18000
  50. #define RCBA_MMIO_LEN 0x4000
  51. #define SPIBAR 0x3800
  52. #define HSFS_REG 0x04
  53. volatile uint8_t *rcba_mmio;
  54. int
  55. main(int argc, char *argv[])
  56. {
  57. int devmemfd; (void)argc; (void)argv;
  58. if ((ioperm(EC_INDEX, 2, 1) == -1) || (ioperm(SMI_EN_REG, 4, 1) == -1))
  59. err(errno, "Could not access IO ports");
  60. if ((devmemfd = open("/dev/mem", O_RDONLY)) == -1)
  61. err(errno, "/dev/mem");
  62. /* FDO pin-strap status bit is in RCBA mmio space */
  63. rcba_mmio = mmap(0, RCBA_MMIO_LEN, PROT_READ, MAP_SHARED, devmemfd,
  64. RCBA);
  65. if (rcba_mmio == MAP_FAILED)
  66. err(errno, "Could not map RCBA");
  67. if (get_fdo_status() == 1) {
  68. ec_fdo_command(SET_OVERRIDE);
  69. printf("Flash Descriptor Override enabled. Shut down now. The "
  70. "EC will auto-boot the system and set the override.\n"
  71. "Upon boot, re-run this utility to unlock flash.\n");
  72. } else if (get_gbl_smi_en()){
  73. set_gbl_smi_en(0);
  74. printf("SMIs disabled. Internal flashing should work now.\n"
  75. "After flashing, re-run this utility to enable SMIs.\n"
  76. "(shutdown is buggy when SMIs are disabled)\n");
  77. } else {
  78. set_gbl_smi_en(1);
  79. printf("SMIs enabled, you can now shutdown the system.\n");
  80. }
  81. return errno;
  82. }
  83. int
  84. get_fdo_status(void)
  85. {
  86. return (*(uint16_t*)(rcba_mmio + SPIBAR + HSFS_REG) >> 13) & 1;
  87. }
  88. /*
  89. * arg:
  90. * 0 = Query EC FDO status - TODO
  91. * 2 = Enable FDO for next boot
  92. * 3 = Disable FDO for next boot - TODO
  93. */
  94. void
  95. ec_fdo_command(enum EC_FDO_CMD arg)
  96. {
  97. write_ec_reg(0x12, arg);
  98. send_ec_cmd(0xb8);
  99. }
  100. void
  101. write_ec_reg(uint8_t index, uint8_t data)
  102. {
  103. outb(index, EC_INDEX);
  104. outb(data, EC_DATA);
  105. }
  106. void
  107. send_ec_cmd(uint8_t cmd)
  108. {
  109. outb(0, EC_INDEX);
  110. outb(cmd, EC_DATA);
  111. wait_ec();
  112. }
  113. void
  114. wait_ec(void)
  115. {
  116. uint8_t busy;
  117. do {
  118. outb(0, EC_INDEX);
  119. busy = inb(EC_DATA);
  120. } while (busy);
  121. }
  122. int
  123. get_gbl_smi_en(void)
  124. {
  125. return inl(SMI_EN_REG) & 1;
  126. }
  127. int
  128. set_gbl_smi_en(int enable)
  129. {
  130. uint32_t smi_en = inl(SMI_EN_REG);
  131. if (enable) {
  132. smi_en |= 1;
  133. } else {
  134. smi_en &= ~1;
  135. }
  136. outl(smi_en, SMI_EN_REG);
  137. return (get_gbl_smi_en() == enable);
  138. }
  139. /*
  140. uint8_t
  141. read_ec_reg(uint8_t index)
  142. {
  143. outb(index, EC_INDEX);
  144. return inb(EC_DATA);
  145. }
  146. */