ich9gen.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Copyright (C) 2014, 2015, 2019 Leah Rowe <info@minifree.org>
  3. * Copyright (C) 2016 Swift Geek <swiftgeek@gmail.com>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* Generate deblobbed descriptor and gbe 12KiB file from scratch
  19. * without relying on a factory.rom dump */
  20. #include "ich9gen.h"
  21. int main(int argc, char *argv[])
  22. {
  23. int i, j;
  24. struct GBEREGIONRECORD_8K gbeStruct8k = generatedGbeStruct8k();
  25. struct DESCRIPTORREGIONRECORD descriptorStruct4M = generatedDescriptorStruct(ROMSIZE_4MB, WITHGBE);
  26. struct DESCRIPTORREGIONRECORD descriptorStruct8M = generatedDescriptorStruct(ROMSIZE_8MB, WITHGBE);
  27. struct DESCRIPTORREGIONRECORD descriptorStruct16M = generatedDescriptorStruct(ROMSIZE_16MB, WITHGBE);
  28. struct DESCRIPTORREGIONRECORD descriptorStructNoGbe4M = generatedDescriptorStruct(ROMSIZE_4MB, WITHOUTGBE);
  29. struct DESCRIPTORREGIONRECORD descriptorStructNoGbe8M = generatedDescriptorStruct(ROMSIZE_8MB, WITHOUTGBE);
  30. struct DESCRIPTORREGIONRECORD descriptorStructNoGbe16M = generatedDescriptorStruct(ROMSIZE_16MB, WITHOUTGBE);
  31. /* Only for the compatibility checks */
  32. struct DESCRIPTORREGIONRECORD dummyDescriptorStruct;
  33. struct GBEREGIONRECORD_8K dummyGbeStruct8k;
  34. /*
  35. * ------------------------------------------------------------------
  36. * Compatibility checks. This version of ich9deblob is not yet portable.
  37. * ------------------------------------------------------------------
  38. */
  39. if (systemOrCompilerIncompatible(dummyDescriptorStruct, dummyGbeStruct8k)) return 1;
  40. /* If true, fail with error message */
  41. /*
  42. * ------------------------------------------------------------------
  43. * Arguments given on the terminal
  44. * ------------------------------------------------------------------
  45. */
  46. if(argc==3) {
  47. /* If user provides their own MAC address, it will be used.
  48. * Otherwise, ich9gen will simply use the default one.
  49. *
  50. * However, if the user provides an invalid MAC address, then ich9gen
  51. * will exit. */
  52. if(0==strcmp(argv[1],"--macaddress")) {
  53. /* 6 hex chars format (example): AA:BB:CC:DD:EE:FF */
  54. if (strlen(argv[2]) != 17) {
  55. printf("ich9gen: invalid mac address format (wrong length)\n");
  56. return 1;
  57. }
  58. for(i=2; i<14; i+=3) {
  59. if(argv[2][i]!=':') {
  60. printf("ich9gen: invalid mac address format (non-colon characters used as spacing)\n");
  61. return 1;
  62. }
  63. }
  64. for(i=0; i<6; i++) {
  65. gbeStruct8k.main.macAddress[i] = 0;
  66. /* Go through each nibble of the byte */
  67. for(j=0; j<2; j++) {
  68. if(argv[2][(i*3)+j]>='a' && argv[2][(i*3)+j]<='f')
  69. gbeStruct8k.main.macAddress[i] |= (uint8_t)((argv[2][(i*3)+j] - 87) << ((j^1) << 2));
  70. else if(argv[2][(i*3)+j]>='A' && argv[2][(i*3)+j]<='F')
  71. gbeStruct8k.main.macAddress[i] |= (uint8_t)((argv[2][(i*3)+j] - 55) << ((j^1) << 2));
  72. else if(argv[2][(i*3)+j]>='0' && argv[2][(i*3)+j]<='9')
  73. gbeStruct8k.main.macAddress[i] |= (uint8_t)((argv[2][(i*3)+j] - 48) << ((j^1) << 2));
  74. else {
  75. printf("ich9gen: invalid mac address format (non-hex characters)\n");
  76. return 1;
  77. }
  78. }
  79. }
  80. gbeStruct8k.main.checkSum = gbeGetChecksumFrom4kStruct(gbeStruct8k.main, GBECHECKSUMTOTAL); /* Fix the checksum */
  81. memcpy(&gbeStruct8k.backup, &gbeStruct8k.main, GBEREGIONSIZE_4K); /* Copy to the backup */
  82. /* Generate ich9gen data (C code for Gbe region): */
  83. /* mkgbe.h */
  84. if (notCreatedHFileForGbeCFile("mkgbe.h", "mkgbe.c")) {
  85. return 1;
  86. } /* and now mkgbe.c */
  87. if (notCreatedCFileFromGbeStruct4k(gbeStruct8k.backup, "mkgbe.c", "mkgbe.h")) {
  88. return 1;
  89. }
  90. printf("You selected to change the MAC address in the Gbe section. This has been done.\n\n");
  91. printf("The modified gbe region has also been dumped as src files: mkgbe.c, mkgbe.h\n");
  92. printf("To use these in ich9gen, place them in src/ich9gen/ and re-build ich9gen.\n\n");
  93. }
  94. }
  95. /*
  96. * ------------------------------------------------------------------
  97. * Generate the 12KiB files, ready to be used in a libreboot image
  98. * ------------------------------------------------------------------
  99. */
  100. if (notCreatedDescriptorGbeFile(descriptorStruct4M, gbeStruct8k, "ich9fdgbe_4m.bin")) {
  101. return 1;
  102. }
  103. if (notCreatedDescriptorGbeFile(descriptorStruct8M, gbeStruct8k, "ich9fdgbe_8m.bin")) {
  104. return 1;
  105. }
  106. if (notCreatedDescriptorGbeFile(descriptorStruct16M, gbeStruct8k, "ich9fdgbe_16m.bin")) {
  107. return 1;
  108. }
  109. /*
  110. * ------------------------------------------------------------------
  111. * Generate the 12KiB files, ready to be used in a libreboot image
  112. * These are special descriptor files where the flash chip is set to read-only
  113. * in FLMSTR1
  114. * ------------------------------------------------------------------
  115. */
  116. descriptorStruct4M = descriptorHostRegionsReadOnly(descriptorStruct4M);
  117. descriptorStruct8M = descriptorHostRegionsReadOnly(descriptorStruct8M);
  118. descriptorStruct16M = descriptorHostRegionsReadOnly(descriptorStruct16M);
  119. if (notCreatedDescriptorGbeFile(descriptorStruct4M, gbeStruct8k, "ich9fdgbe_4m_ro.bin")) {
  120. return 1;
  121. }
  122. if (notCreatedDescriptorGbeFile(descriptorStruct8M, gbeStruct8k, "ich9fdgbe_8m_ro.bin")) {
  123. return 1;
  124. }
  125. if (notCreatedDescriptorGbeFile(descriptorStruct16M, gbeStruct8k, "ich9fdgbe_16m_ro.bin")) {
  126. return 1;
  127. }
  128. /*
  129. * ------------------------------------------------------------------
  130. * Generate the 4KiB files (descriptors without GbE), ready to be used in a libreboot image
  131. * In these descriptors, the onboard Intel GbE NIC is disabled; a discrete one is used instead
  132. * ------------------------------------------------------------------
  133. */
  134. if (notCreated4kDescriptorFile(descriptorStructNoGbe4M, "ich9fdnogbe_4m.bin")) {
  135. return 1;
  136. }
  137. if (notCreated4kDescriptorFile(descriptorStructNoGbe8M, "ich9fdnogbe_8m.bin")) {
  138. return 1;
  139. }
  140. if (notCreated4kDescriptorFile(descriptorStructNoGbe16M, "ich9fdnogbe_16m.bin")) {
  141. return 1;
  142. }
  143. /*
  144. * ------------------------------------------------------------------
  145. * Generate the 4KiB files (descriptors without GbE), ready to be used in a libreboot image
  146. * In these descriptors, the onboard Intel GbE NIC is disabled; a discrete one is used instead
  147. *
  148. * This is a special version where the flash chip is read-only as specified
  149. * in flmstr1
  150. *
  151. * ------------------------------------------------------------------
  152. */
  153. descriptorStructNoGbe4M = descriptorHostRegionsReadOnly(descriptorStructNoGbe4M);
  154. descriptorStructNoGbe8M = descriptorHostRegionsReadOnly(descriptorStructNoGbe8M);
  155. descriptorStructNoGbe16M = descriptorHostRegionsReadOnly(descriptorStructNoGbe16M);
  156. if (notCreated4kDescriptorFile(descriptorStructNoGbe4M, "ich9fdnogbe_4m_ro.bin")) {
  157. return 1;
  158. }
  159. if (notCreated4kDescriptorFile(descriptorStructNoGbe8M, "ich9fdnogbe_8m_ro.bin")) {
  160. return 1;
  161. }
  162. if (notCreated4kDescriptorFile(descriptorStructNoGbe16M, "ich9fdnogbe_16m_ro.bin")) {
  163. return 1;
  164. }
  165. return 0;
  166. }