e6400_flash_unlock.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. #define EC_INDEX 0x910
  44. #define EC_DATA 0x911
  45. #define PMBASE 0x1000
  46. #define SMI_EN_REG (PMBASE + 0x30)
  47. /* Assume this is the same on all vendor BIOS versions */
  48. #define RCBA 0xfed18000
  49. #define RCBA_MMIO_LEN 0x4000
  50. #define SPIBAR 0x3800
  51. #define HSFS_REG 0x04
  52. volatile uint8_t *rcba_mmio;
  53. int
  54. main(int argc, char *argv[])
  55. {
  56. int devmemfd; (void)argc; (void)argv;
  57. if ((ioperm(EC_INDEX, 2, 1) == -1) || (ioperm(SMI_EN_REG, 4, 1) == -1))
  58. err(errno, "Could not access IO ports");
  59. if ((devmemfd = open("/dev/mem", O_RDONLY)) == -1)
  60. err(errno, "/dev/mem");
  61. /* FDO pin-strap status bit is in RCBA mmio space */
  62. rcba_mmio = mmap(0, RCBA_MMIO_LEN, PROT_READ, MAP_SHARED, devmemfd,
  63. RCBA);
  64. if (rcba_mmio == MAP_FAILED)
  65. err(errno, "Could not map RCBA");
  66. if (get_fdo_status() == 1) {
  67. ec_fdo_command(SET_OVERRIDE);
  68. printf("Flash Descriptor Override enabled. Shut down now. The "
  69. "EC will auto-boot the system and set the override.\n"
  70. "Upon boot, re-run this utility to unlock flash.\n");
  71. } else if (get_gbl_smi_en()){
  72. set_gbl_smi_en(0);
  73. printf("SMIs disabled. Internal flashing should work now.\n"
  74. "After flashing, re-run this utility to enable SMIs.\n"
  75. "(shutdown is buggy when SMIs are disabled)\n");
  76. } else {
  77. set_gbl_smi_en(1);
  78. printf("SMIs enabled, you can now shutdown the system.\n");
  79. }
  80. return errno;
  81. }
  82. int
  83. get_fdo_status(void)
  84. {
  85. return (*(uint16_t*)(rcba_mmio + SPIBAR + HSFS_REG) >> 13) & 1;
  86. }
  87. /*
  88. * arg:
  89. * 0 = Query EC FDO status - TODO
  90. * 2 = Enable FDO for next boot
  91. * 3 = Disable FDO for next boot - TODO
  92. */
  93. void
  94. ec_fdo_command(enum EC_FDO_CMD arg)
  95. {
  96. write_ec_reg(0x12, arg);
  97. send_ec_cmd(0xb8);
  98. }
  99. void
  100. write_ec_reg(uint8_t index, uint8_t data)
  101. {
  102. outb(index, EC_INDEX);
  103. outb(data, EC_DATA);
  104. }
  105. void
  106. send_ec_cmd(uint8_t cmd)
  107. {
  108. outb(0, EC_INDEX);
  109. outb(cmd, EC_DATA);
  110. wait_ec();
  111. }
  112. void
  113. wait_ec(void)
  114. {
  115. uint8_t busy;
  116. do {
  117. outb(0, EC_INDEX);
  118. busy = inb(EC_DATA);
  119. } while (busy);
  120. }
  121. int
  122. get_gbl_smi_en(void)
  123. {
  124. return inl(SMI_EN_REG) & 1;
  125. }
  126. int
  127. set_gbl_smi_en(int enable)
  128. {
  129. uint32_t smi_en = inl(SMI_EN_REG);
  130. if (enable) {
  131. smi_en |= 1;
  132. } else {
  133. smi_en &= ~1;
  134. }
  135. outl(smi_en, SMI_EN_REG);
  136. return (get_gbl_smi_en() == enable);
  137. }