demefactory.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * demefactory.c
  3. * This file is part of the demefactory utility from the libreboot project
  4. *
  5. * Purpose: disable ME on GM45 factory firmware, but leave region intact
  6. * enable read-write on all regions
  7. *
  8. * Copyright (C) 2014, 2015 Leah Rowe <info@minifree.org>
  9. * Copyright (C) 2014 Steve Shenton <sgsit@libreboot.org>
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. /*
  25. * demfactory utility - main
  26. */
  27. #include "demefactory.h"
  28. int main()
  29. {
  30. struct DESCRIPTORREGIONRECORD descriptorStruct;
  31. uint8_t* descriptorBuffer = (uint8_t*)&descriptorStruct;
  32. struct GBEREGIONRECORD_8K gbeStruct8k; /* not needed, except for compatibility checking */
  33. char* romFilename = "factory.rom";
  34. char* descriptorFilename = "demefactory_4kdescriptor.bin";
  35. unsigned int bufferLength;
  36. unsigned int romSize;
  37. /*
  38. * ------------------------------------------------------------------
  39. * Compatibility checks. This version of ich9deblob is not yet portable.
  40. * ------------------------------------------------------------------
  41. */
  42. if (systemOrCompilerIncompatible(descriptorStruct, gbeStruct8k)) return 1;
  43. /* If true, fail with error message */
  44. /*
  45. * ------------------------------------------------------------------
  46. * Extract the descriptor region from the factory.rom dump
  47. * ------------------------------------------------------------------
  48. */
  49. FILE* fp = NULL;
  50. fp = fopen(romFilename, "rb"); /* open factory.rom */
  51. if (NULL == fp)
  52. {
  53. printf("\nerror: could not open %s\n", romFilename);
  54. fclose(fp);
  55. return 1;
  56. }
  57. printf("\n%s opened successfully\n", romFilename);
  58. /*
  59. * Get the descriptor region dump from the factory.rom
  60. * (goes in factoryDescriptorBuffer variable)
  61. */
  62. bufferLength = fread(descriptorBuffer, 1, DESCRIPTORREGIONSIZE, fp);
  63. if (DESCRIPTORREGIONSIZE != bufferLength) //
  64. {
  65. printf("\nerror: could not read descriptor from %s (%i) bytes read\n", romFilename, bufferLength);
  66. fclose(fp);
  67. return 1;
  68. }
  69. printf("\ndescriptor region read successfully\n");
  70. /* ------------------------------------------------- */
  71. fseek(fp, 0L, SEEK_END);
  72. romSize = ftell(fp);
  73. printf("\n%s size: [%i] bytes\n", romFilename, romSize);
  74. /* -------------------------------------------------- */
  75. fclose(fp);
  76. /* Debugging (before modification) */
  77. printDescriptorRegionLocations(descriptorStruct, "Original");
  78. /*
  79. * ------------------------------------------------------------------
  80. * Modify the descriptor region, ready to go in the modified factory.rom
  81. * ------------------------------------------------------------------
  82. */
  83. // Disable the ME/TPM:
  84. descriptorStruct = descriptorDisableMe(descriptorStruct);
  85. descriptorStruct = descriptorDisableTpm(descriptorStruct);
  86. /* Host/CPU is allowed to read/write all regions. */
  87. descriptorStruct = descriptorHostRegionsUnlocked(descriptorStruct);
  88. /* The ME is disallowed read-write access to all regions
  89. * (this is probably redundant, since the ME is already removed from libreboot) */
  90. descriptorStruct = descriptorMeRegionsForbidden(descriptorStruct);
  91. /* Debugging (after modifying the descriptor region) */
  92. printDescriptorRegionLocations(descriptorStruct, "Modified");
  93. /*
  94. * ------------------------------------------------------------------
  95. * Create the file with the modified descriptor inside
  96. * ------------------------------------------------------------------
  97. */
  98. printf("\n");
  99. if (notCreated4kDescriptorFile(descriptorStruct, descriptorFilename)) {
  100. return 1;
  101. }
  102. /*
  103. * ------------------------------------------------------------------
  104. * Generate ich9gen data (C code that will recreate the deactivatedME descriptor from scratch)
  105. * ------------------------------------------------------------------
  106. */
  107. /* Code for generating the Descriptor struct */
  108. /* mkdescriptor.h */
  109. if (notCreatedHFileForDescriptorCFile("mkdescriptor.h", "mkdescriptor.c")) {
  110. return 1;
  111. } /* and now mkdescriptor.c */
  112. if (notCreatedCFileFromDescriptorStruct(descriptorStruct, "mkdescriptor.c", "mkdescriptor.h")) {
  113. return 1;
  114. }
  115. printf("The modified descriptor region has also been dumped as src files: mkdescriptor.c, mkdescriptor.h\n\n");
  116. return 0;
  117. }