ich9deblob.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * ich9deblob.c
  3. * This file is part of the ich9deblob utility from the libreboot project
  4. *
  5. * Purpose: disable and remove the ME from ich9m/gm45 systems in coreboot.
  6. *
  7. * Copyright (C) 2014 Steve Shenton <sgsit@libreboot.org>
  8. * Copyright (C) 2014,2015,2019 Leah Rowe <info@minifree.org>
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. /* Initially based on proof of concept by Steve Shenton. */
  24. /* Original utility can be found at https://gitorious.org/ich9descriptortool */
  25. /*
  26. * Read a factory.rom dump (ich9m/gm45 systems) and
  27. * modify the flash descriptor to remove all regions except descriptor,
  28. * Gbe and BIOS. Set BIOS region to full size of the ROM image (after
  29. * the flash descriptor and gbe). Basically, deblob the descriptor.
  30. *
  31. * This will will generate a concatenated descriptor+gbe dump suitable
  32. * for use in libreboot. Currently tested: ThinkPad X200 (coreboot/libreboot)
  33. */
  34. /*
  35. * See docs/hardware/x200_remove_me.html for info plus links to datasheet (also linked below)
  36. *
  37. * Info about flash descriptor (read page 845 onwards):
  38. * http://www.intel.co.uk/content/dam/doc/datasheet/io-controller-hub-9-datasheet.pdf
  39. *
  40. * Info about Gbe region (read whole datasheet):
  41. * http://www.intel.co.uk/content/dam/doc/application-note/i-o-controller-hub-9m-82567lf-lm-v-nvm-map-appl-note.pdf
  42. * https://web.archive.org/web/20150912070329/https://communities.intel.com/community/wired/blog/2010/10/14/how-to-basic-eeprom-checksums
  43. */
  44. #include "ich9deblob.h"
  45. int main(int argc, char *argv[])
  46. {
  47. struct DESCRIPTORREGIONRECORD descriptorStruct;
  48. uint8_t* descriptorBuffer = (uint8_t*)&descriptorStruct;
  49. struct GBEREGIONRECORD_8K gbeStruct8k;
  50. uint8_t* gbeBuffer8k = (uint8_t*)&gbeStruct8k;
  51. uint32_t gbeRegionStart;
  52. char* romFilename = "factory.rom";
  53. if(argc>1) {
  54. romFilename = argv[1];
  55. }
  56. char* descriptorGbeFilename = "deblobbed_descriptor.bin";
  57. char* descriptorNoGbeFilename = "deblobbed_4kdescriptor.bin";
  58. unsigned int bufferLength;
  59. unsigned int romSize;
  60. /*
  61. * ------------------------------------------------------------------
  62. * Compatibility checks. This version of ich9deblob is not yet portable.
  63. * ------------------------------------------------------------------
  64. */
  65. if (systemOrCompilerIncompatible(descriptorStruct, gbeStruct8k)) return 1;
  66. /* If true, fail with error message */
  67. /*
  68. * ------------------------------------------------------------------
  69. * Extract the descriptor and gbe regions from the factory.rom dump
  70. * ------------------------------------------------------------------
  71. */
  72. FILE* fp = NULL;
  73. fp = fopen(romFilename, "rb"); /* open factory.rom */
  74. if (NULL == fp)
  75. {
  76. printf("\nerror: could not open %s\n", romFilename);
  77. fclose(fp);
  78. return 1;
  79. }
  80. printf("\n%s opened successfully\n", romFilename);
  81. /*
  82. * Get the descriptor region dump from the factory.rom
  83. * (goes in factoryDescriptorBuffer variable)
  84. */
  85. bufferLength = fread(descriptorBuffer, 1, DESCRIPTORREGIONSIZE, fp);
  86. if (DESCRIPTORREGIONSIZE != bufferLength) //
  87. {
  88. printf("\nerror: could not read descriptor from %s (%i) bytes read\n", romFilename, bufferLength);
  89. fclose(fp);
  90. return 1;
  91. }
  92. printf("\ndescriptor region read successfully\n");
  93. if(!validDescriptor(descriptorStruct)) {
  94. printf("Invalid input: incorrect signature in the given descriptor.");
  95. fclose(fp);
  96. return 1;
  97. }
  98. if (descriptorDefinesGbeRegion(descriptorStruct))
  99. {
  100. gbeRegionStart = descriptorStruct.regionSection.flReg3.BASE << FLREGIONBITSHIFT;
  101. /*
  102. * Set offset so that we can read the data from
  103. * the gbe region
  104. */
  105. fseek(fp, gbeRegionStart, SEEK_SET);
  106. /* Read the gbe data from the factory.rom and put it in factoryGbeBuffer8k */
  107. bufferLength = fread(gbeBuffer8k, 1, GBEREGIONSIZE_8K, fp);
  108. if (GBEREGIONSIZE_8K != bufferLength)
  109. {
  110. printf("\nerror: could not read GBe region from %s (%i) bytes read\n", romFilename, bufferLength);
  111. fclose(fp);
  112. return 1;
  113. }
  114. printf("\ngbe (8KiB) region read successfully\n");
  115. }
  116. fseek(fp, 0L, SEEK_END);
  117. romSize = ftell(fp);
  118. printf("\n%s size: [%i] bytes\n", romFilename, romSize);
  119. fclose(fp);
  120. /* Debugging (before modification) */
  121. printDescriptorRegionLocations(descriptorStruct, "Original");
  122. if (descriptorDefinesGbeRegion(descriptorStruct))
  123. printGbeChecksumDataFromStruct8k(gbeStruct8k, "Original");
  124. else printf("NO GBE REGION\n");
  125. /*
  126. * ------------------------------------------------------------------
  127. * Modify the descriptor and gbe regions, ready to go in libreboot.rom
  128. * ------------------------------------------------------------------
  129. */
  130. /* Delete the ME/Platform regions, place Gbe after the descriptor, resize BIOS region to fill the gap */
  131. descriptorStruct = librebootDescriptorStructFromFactory(descriptorStruct, romSize);
  132. /* The ME is disallowed read-write access to all regions
  133. * (this is probably redundant, since the ME firmware is already removed from libreboot) */
  134. descriptorStruct = descriptorMeRegionsForbidden(descriptorStruct);
  135. /* Host/CPU is allowed to read/write all regions.
  136. * This makes flashrom -p internal work */
  137. descriptorStruct = descriptorHostRegionsUnlocked(descriptorStruct);
  138. /* Set OEM string */
  139. descriptorStruct = descriptorOemString(descriptorStruct);
  140. /* Modify the Gbe region (see function for details) */
  141. if (descriptorDefinesGbeRegion(descriptorStruct))
  142. gbeStruct8k = deblobbedGbeStructFromFactory(gbeStruct8k);
  143. /* Debugging (after modifying the descriptor and gbe regions) */
  144. printDescriptorRegionLocations(descriptorStruct, "Modified");
  145. if (descriptorDefinesGbeRegion(descriptorStruct))
  146. printGbeChecksumDataFromStruct8k(gbeStruct8k, "Modified");
  147. else printf("NO GBE REGION\n");
  148. /*
  149. * ------------------------------------------------------------------
  150. * Create the file with the modified descriptor and gbe inside
  151. * ------------------------------------------------------------------
  152. */
  153. printf("\n");
  154. if (descriptorDefinesGbeRegion(descriptorStruct))
  155. {
  156. if (notCreatedDescriptorGbeFile(descriptorStruct, gbeStruct8k, descriptorGbeFilename)) {
  157. return 1;
  158. }
  159. }
  160. else
  161. {
  162. if (notCreated4kDescriptorFile(descriptorStruct, descriptorNoGbeFilename)) {
  163. return 1;
  164. }
  165. }
  166. /*
  167. * ------------------------------------------------------------------
  168. * Generate ich9gen data (C code that will recreate the deblobbed descriptor+gbe from scratch)
  169. * ------------------------------------------------------------------
  170. */
  171. /* Code for generating the Descriptor struct */
  172. /* mkdescriptor.h */
  173. if (notCreatedHFileForDescriptorCFile("mkdescriptor.h", "mkdescriptor.c")) {
  174. return 1;
  175. } /* and now mkdescriptor.c */
  176. if (notCreatedCFileFromDescriptorStruct(descriptorStruct, "mkdescriptor.c", "mkdescriptor.h")) {
  177. return 1;
  178. }
  179. if (descriptorDefinesGbeRegion(descriptorStruct))
  180. {
  181. /* Code for generating the Gbe struct */
  182. /* mkgbe.h */
  183. if (notCreatedHFileForGbeCFile("mkgbe.h", "mkgbe.c")) {
  184. return 1;
  185. } /* and now mkgbe.c */
  186. if (notCreatedCFileFromGbeStruct4k(gbeStruct8k.backup, "mkgbe.c", "mkgbe.h")) {
  187. return 1;
  188. }
  189. }
  190. if (descriptorDefinesGbeRegion(descriptorStruct))
  191. {
  192. printf("The modified descriptor and gbe regions have also been dumped as src files: mkdescriptor.c, mkdescriptor.h, mkgbe.c, mkgbe.h\n");
  193. printf("To use these in ich9gen, place them in src/ich9gen/ and re-build ich9gen.\n\n");
  194. }
  195. else
  196. {
  197. printf("The modified descriptor region have also been dumped as src files: mkdescriptor.c, mkdescriptor.h\n");
  198. printf("To use these in ich9gen, place them in src/ich9gen/ and re-build ich9gen.\n\n");
  199. }
  200. return 0;
  201. }