ich9deblob.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 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://communities.intel.com/community/wired/blog/2010/10/14/how-to-basic-eeprom-checksums
  43. */
  44. #include "ich9deblob.h"
  45. int main()
  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. char* descriptorGbeFilename = "deblobbed_descriptor.bin";
  54. char* descriptorNoGbeFilename = "deblobbed_4kdescriptor.bin";
  55. unsigned int bufferLength;
  56. unsigned int romSize;
  57. /*
  58. * ------------------------------------------------------------------
  59. * Compatibility checks. This version of ich9deblob is not yet portable.
  60. * ------------------------------------------------------------------
  61. */
  62. if (systemOrCompilerIncompatible(descriptorStruct, gbeStruct8k)) return 1;
  63. /* If true, fail with error message */
  64. /*
  65. * ------------------------------------------------------------------
  66. * Extract the descriptor and gbe regions from the factory.rom dump
  67. * ------------------------------------------------------------------
  68. */
  69. FILE* fp = NULL;
  70. fp = fopen(romFilename, "rb"); /* open factory.rom */
  71. if (NULL == fp)
  72. {
  73. printf("\nerror: could not open %s\n", romFilename);
  74. fclose(fp);
  75. return 1;
  76. }
  77. printf("\n%s opened successfully\n", romFilename);
  78. /*
  79. * Get the descriptor region dump from the factory.rom
  80. * (goes in factoryDescriptorBuffer variable)
  81. */
  82. bufferLength = fread(descriptorBuffer, 1, DESCRIPTORREGIONSIZE, fp);
  83. if (DESCRIPTORREGIONSIZE != bufferLength) //
  84. {
  85. printf("\nerror: could not read descriptor from %s (%i) bytes read\n", romFilename, bufferLength);
  86. fclose(fp);
  87. return 1;
  88. }
  89. printf("\ndescriptor region read successfully\n");
  90. if (descriptorDefinesGbeRegion(descriptorStruct))
  91. {
  92. gbeRegionStart = descriptorStruct.regionSection.flReg3.BASE << FLREGIONBITSHIFT;
  93. /*
  94. * Set offset so that we can read the data from
  95. * the gbe region
  96. */
  97. fseek(fp, gbeRegionStart, SEEK_SET);
  98. /* Read the gbe data from the factory.rom and put it in factoryGbeBuffer8k */
  99. bufferLength = fread(gbeBuffer8k, 1, GBEREGIONSIZE_8K, fp);
  100. if (GBEREGIONSIZE_8K != bufferLength)
  101. {
  102. printf("\nerror: could not read GBe region from %s (%i) bytes read\n", romFilename, bufferLength);
  103. fclose(fp);
  104. return 1;
  105. }
  106. printf("\ngbe (8KiB) region read successfully\n");
  107. }
  108. fseek(fp, 0L, SEEK_END);
  109. romSize = ftell(fp);
  110. printf("\n%s size: [%i] bytes\n", romFilename, romSize);
  111. fclose(fp);
  112. /* Debugging (before modification) */
  113. printDescriptorRegionLocations(descriptorStruct, "Original");
  114. if (descriptorDefinesGbeRegion(descriptorStruct))
  115. printGbeChecksumDataFromStruct8k(gbeStruct8k, "Original");
  116. else printf("NO GBE REGION\n");
  117. /*
  118. * ------------------------------------------------------------------
  119. * Modify the descriptor and gbe regions, ready to go in libreboot.rom
  120. * ------------------------------------------------------------------
  121. */
  122. /* Delete the ME/Platform regions, place Gbe after the descriptor, resize BIOS region to fill the gap */
  123. descriptorStruct = librebootDescriptorStructFromFactory(descriptorStruct, romSize);
  124. /* The ME is disallowed read-write access to all regions
  125. * (this is probably redundant, since the ME firmware is already removed from libreboot) */
  126. descriptorStruct = descriptorMeRegionsForbidden(descriptorStruct);
  127. /* Host/CPU is allowed to read/write all regions.
  128. * This makes flashrom -p internal work */
  129. descriptorStruct = descriptorHostRegionsUnlocked(descriptorStruct);
  130. /* Set OEM string */
  131. descriptorStruct = descriptorOemString(descriptorStruct);
  132. /* Modify the Gbe region (see function for details) */
  133. if (descriptorDefinesGbeRegion(descriptorStruct))
  134. gbeStruct8k = deblobbedGbeStructFromFactory(gbeStruct8k);
  135. /* Debugging (after modifying the descriptor and gbe regions) */
  136. printDescriptorRegionLocations(descriptorStruct, "Modified");
  137. if (descriptorDefinesGbeRegion(descriptorStruct))
  138. printGbeChecksumDataFromStruct8k(gbeStruct8k, "Modified");
  139. else printf("NO GBE REGION\n");
  140. /*
  141. * ------------------------------------------------------------------
  142. * Create the file with the modified descriptor and gbe inside
  143. * ------------------------------------------------------------------
  144. */
  145. printf("\n");
  146. if (descriptorDefinesGbeRegion(descriptorStruct))
  147. {
  148. if (notCreatedDescriptorGbeFile(descriptorStruct, gbeStruct8k, descriptorGbeFilename)) {
  149. return 1;
  150. }
  151. }
  152. else
  153. {
  154. if (notCreated4kDescriptorFile(descriptorStruct, descriptorNoGbeFilename)) {
  155. return 1;
  156. }
  157. }
  158. /*
  159. * ------------------------------------------------------------------
  160. * Generate ich9gen data (C code that will recreate the deblobbed descriptor+gbe from scratch)
  161. * ------------------------------------------------------------------
  162. */
  163. /* Code for generating the Descriptor struct */
  164. /* mkdescriptor.h */
  165. if (notCreatedHFileForDescriptorCFile("mkdescriptor.h", "mkdescriptor.c")) {
  166. return 1;
  167. } /* and now mkdescriptor.c */
  168. if (notCreatedCFileFromDescriptorStruct(descriptorStruct, "mkdescriptor.c", "mkdescriptor.h")) {
  169. return 1;
  170. }
  171. if (descriptorDefinesGbeRegion(descriptorStruct))
  172. {
  173. /* Code for generating the Gbe struct */
  174. /* mkgbe.h */
  175. if (notCreatedHFileForGbeCFile("mkgbe.h", "mkgbe.c")) {
  176. return 1;
  177. } /* and now mkgbe.c */
  178. if (notCreatedCFileFromGbeStruct4k(gbeStruct8k.backup, "mkgbe.c", "mkgbe.h")) {
  179. return 1;
  180. }
  181. }
  182. if (descriptorDefinesGbeRegion(descriptorStruct))
  183. {
  184. printf("The modified descriptor and gbe regions have also been dumped as src files: mkdescriptor.c, mkdescriptor.h, mkgbe.c, mkgbe.h\n");
  185. printf("To use these in ich9gen, place them in src/ich9gen/ and re-build ich9gen.\n\n");
  186. }
  187. else
  188. {
  189. printf("The modified descriptor region have also been dumped as src files: mkdescriptor.c, mkdescriptor.h\n");
  190. printf("To use these in ich9gen, place them in src/ich9gen/ and re-build ich9gen.\n\n");
  191. }
  192. return 0;
  193. }