0015-util-ifdtool-add-nuke-flag-all-0xFF-on-region.patch 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. From 158b79e6057e071d039619f617c112d31fb13f64 Mon Sep 17 00:00:00 2001
  2. From: Leah Rowe <leah@libreboot.org>
  3. Date: Sun, 19 Feb 2023 18:21:43 +0000
  4. Subject: [PATCH 15/30] util/ifdtool: add --nuke flag (all 0xFF on region)
  5. When this option is used, the region's contents are overwritten
  6. with all ones (0xFF).
  7. Example:
  8. ./ifdtool --nuke gbe coreboot.rom
  9. ./ifdtool --nuke bios coreboot.com
  10. ./ifdtool --nuke me coreboot.com
  11. Rebased since the last revision update in lbmk.
  12. Signed-off-by: Leah Rowe <leah@libreboot.org>
  13. ---
  14. util/ifdtool/ifdtool.c | 112 +++++++++++++++++++++++++++++------------
  15. 1 file changed, 81 insertions(+), 31 deletions(-)
  16. diff --git a/util/ifdtool/ifdtool.c b/util/ifdtool/ifdtool.c
  17. index 191b3216de..38132b4a28 100644
  18. --- a/util/ifdtool/ifdtool.c
  19. +++ b/util/ifdtool/ifdtool.c
  20. @@ -1942,6 +1942,7 @@ static void print_usage(const char *name)
  21. " tgl - Tiger Lake\n"
  22. " wbg - Wellsburg\n"
  23. " -S | --setpchstrap Write a PCH strap\n"
  24. + " -N | --nuke <region> Overwrite the specified region with 0xFF (all ones)\n"
  25. " -V | --newvalue The new value to write into PCH strap specified by -S\n"
  26. " -v | --version: print the version\n"
  27. " -h | --help: print this help\n\n"
  28. @@ -1950,6 +1951,60 @@ static void print_usage(const char *name)
  29. "\n");
  30. }
  31. +static int
  32. +get_region_type_string(const char *region_type_string)
  33. +{
  34. + if (!strcasecmp("Descriptor", region_type_string))
  35. + return 0;
  36. + else if (!strcasecmp("BIOS", region_type_string))
  37. + return 1;
  38. + else if (!strcasecmp("ME", region_type_string))
  39. + return 2;
  40. + else if (!strcasecmp("GbE", region_type_string))
  41. + return 3;
  42. + else if (!strcasecmp("Platform Data", region_type_string))
  43. + return 4;
  44. + else if (!strcasecmp("Device Exp1", region_type_string))
  45. + return 5;
  46. + else if (!strcasecmp("Secondary BIOS", region_type_string))
  47. + return 6;
  48. + else if (!strcasecmp("Reserved", region_type_string))
  49. + return 7;
  50. + else if (!strcasecmp("EC", region_type_string))
  51. + return 8;
  52. + else if (!strcasecmp("Device Exp2", region_type_string))
  53. + return 9;
  54. + else if (!strcasecmp("IE", region_type_string))
  55. + return 10;
  56. + else if (!strcasecmp("10GbE_0", region_type_string))
  57. + return 11;
  58. + else if (!strcasecmp("10GbE_1", region_type_string))
  59. + return 12;
  60. + else if (!strcasecmp("PTT", region_type_string))
  61. + return 15;
  62. + return -1;
  63. +}
  64. +
  65. +static void
  66. +nuke(const char *filename, char *image, int size, int region_type)
  67. +{
  68. + int i;
  69. + struct region region;
  70. + const struct frba *frba = find_frba(image, size);
  71. + if (!frba)
  72. + exit(EXIT_FAILURE);
  73. +
  74. + region = get_region(frba, region_type);
  75. + if (region.size > 0) {
  76. + for (i = region.base; i <= region.limit; i++) {
  77. + if ((i + 1) > (size))
  78. + break;
  79. + image[i] = 0xFF;
  80. + }
  81. + write_image(filename, image, size);
  82. + }
  83. +}
  84. +
  85. int main(int argc, char *argv[])
  86. {
  87. int opt, option_index = 0;
  88. @@ -1957,6 +2012,7 @@ int main(int argc, char *argv[])
  89. int mode_em100 = 0, mode_locked = 0, mode_unlocked = 0, mode_validate = 0;
  90. int mode_layout = 0, mode_newlayout = 0, mode_density = 0, mode_setstrap = 0;
  91. int mode_read = 0, mode_altmedisable = 0, altmedisable = 0, mode_fmap_template = 0;
  92. + int mode_nuke = 0;
  93. int mode_gpr0_disable = 0;
  94. char *region_type_string = NULL, *region_fname = NULL;
  95. const char *layout_fname = NULL;
  96. @@ -1990,6 +2046,7 @@ int main(int argc, char *argv[])
  97. {"validate", 0, NULL, 't'},
  98. {"setpchstrap", 1, NULL, 'S'},
  99. {"newvalue", 1, NULL, 'V'},
  100. + {"nuke", 1, NULL, 'N'},
  101. {0, 0, 0, 0}
  102. };
  103. @@ -2039,35 +2096,8 @@ int main(int argc, char *argv[])
  104. region_fname++;
  105. // Descriptor, BIOS, ME, GbE, Platform
  106. // valid type?
  107. - if (!strcasecmp("Descriptor", region_type_string))
  108. - region_type = 0;
  109. - else if (!strcasecmp("BIOS", region_type_string))
  110. - region_type = 1;
  111. - else if (!strcasecmp("ME", region_type_string))
  112. - region_type = 2;
  113. - else if (!strcasecmp("GbE", region_type_string))
  114. - region_type = 3;
  115. - else if (!strcasecmp("Platform Data", region_type_string))
  116. - region_type = 4;
  117. - else if (!strcasecmp("Device Exp1", region_type_string))
  118. - region_type = 5;
  119. - else if (!strcasecmp("Secondary BIOS", region_type_string))
  120. - region_type = 6;
  121. - else if (!strcasecmp("Reserved", region_type_string))
  122. - region_type = 7;
  123. - else if (!strcasecmp("EC", region_type_string))
  124. - region_type = 8;
  125. - else if (!strcasecmp("Device Exp2", region_type_string))
  126. - region_type = 9;
  127. - else if (!strcasecmp("IE", region_type_string))
  128. - region_type = 10;
  129. - else if (!strcasecmp("10GbE_0", region_type_string))
  130. - region_type = 11;
  131. - else if (!strcasecmp("10GbE_1", region_type_string))
  132. - region_type = 12;
  133. - else if (!strcasecmp("PTT", region_type_string))
  134. - region_type = 15;
  135. - if (region_type == -1) {
  136. + if ((region_type =
  137. + get_region_type_string(region_type_string)) == -1) {
  138. fprintf(stderr, "No such region type: '%s'\n\n",
  139. region_type_string);
  140. fprintf(stderr, "run '%s -h' for usage\n", argv[0]);
  141. @@ -2236,6 +2266,22 @@ int main(int argc, char *argv[])
  142. case 't':
  143. mode_validate = 1;
  144. break;
  145. + case 'N':
  146. + region_type_string = strdup(optarg);
  147. + if (!region_type_string) {
  148. + fprintf(stderr, "No region specified\n");
  149. + print_usage(argv[0]);
  150. + exit(EXIT_FAILURE);
  151. + }
  152. + if ((region_type =
  153. + get_region_type_string(region_type_string)) == -1) {
  154. + fprintf(stderr, "No such region type: '%s'\n\n",
  155. + region_type_string);
  156. + print_usage(argv[0]);
  157. + exit(EXIT_FAILURE);
  158. + }
  159. + mode_nuke = 1;
  160. + break;
  161. case 'v':
  162. print_version();
  163. exit(EXIT_SUCCESS);
  164. @@ -2252,7 +2298,7 @@ int main(int argc, char *argv[])
  165. if ((mode_dump + mode_layout + mode_fmap_template + mode_extract + mode_inject +
  166. mode_setstrap + mode_newlayout + (mode_spifreq | mode_em100 |
  167. mode_unlocked | mode_locked) + mode_altmedisable + mode_validate +
  168. - mode_gpr0_disable) > 1) {
  169. + mode_gpr0_disable + mode_nuke) > 1) {
  170. fprintf(stderr, "You may not specify more than one mode.\n\n");
  171. fprintf(stderr, "run '%s -h' for usage\n", argv[0]);
  172. exit(EXIT_FAILURE);
  173. @@ -2261,7 +2307,7 @@ int main(int argc, char *argv[])
  174. if ((mode_dump + mode_layout + mode_fmap_template + mode_extract + mode_inject +
  175. mode_setstrap + mode_newlayout + mode_spifreq + mode_em100 +
  176. mode_locked + mode_unlocked + mode_density + mode_altmedisable +
  177. - mode_validate + mode_gpr0_disable) == 0) {
  178. + mode_validate + mode_gpr0_disable + mode_nuke) == 0) {
  179. fprintf(stderr, "You need to specify a mode.\n\n");
  180. fprintf(stderr, "run '%s -h' for usage\n", argv[0]);
  181. exit(EXIT_FAILURE);
  182. @@ -2368,6 +2414,10 @@ int main(int argc, char *argv[])
  183. write_image(new_filename, image, size);
  184. }
  185. + if (mode_nuke) {
  186. + nuke(new_filename, image, size, region_type);
  187. + }
  188. +
  189. if (mode_altmedisable) {
  190. struct fpsba *fpsba = find_fpsba(image, size);
  191. struct fmsba *fmsba = find_fmsba(image, size);
  192. --
  193. 2.39.2