blob: 57424b985ae803dd9198f326ae756b926fa893ee [file] [log] [blame]
Patrick Georgi7333a112020-05-08 20:48:04 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08002
3/*
4 * ROMSIG At ROMBASE + 0x20000:
zbaoc3b0b722016-02-19 13:47:31 +08005 * 0 4 8 C
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08006 * +------------+---------------+----------------+------------+
7 * | 0x55AA55AA |EC ROM Address |GEC ROM Address |USB3 ROM |
8 * +------------+---------------+----------------+------------+
zbaoc3b0b722016-02-19 13:47:31 +08009 * | PSPDIR ADDR|PSPDIR ADDR |<-- Field 0x14 could be either
10 * +------------+---------------+ 2nd PSP directory or PSP COMBO directory
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080011 * EC ROM should be 64K aligned.
12 *
Zheng Bao4fcc9f22015-11-20 12:29:04 +080013 * PSP directory (Where "PSPDIR ADDR" points)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080014 * +------------+---------------+----------------+------------+
15 * | 'PSP$' | Fletcher | Count | Reserved |
16 * +------------+---------------+----------------+------------+
17 * | 0 | size | Base address | Reserved | Pubkey
18 * +------------+---------------+----------------+------------+
19 * | 1 | size | Base address | Reserved | Bootloader
20 * +------------+---------------+----------------+------------+
21 * | 8 | size | Base address | Reserved | Smu Firmware
22 * +------------+---------------+----------------+------------+
23 * | 3 | size | Base address | Reserved | Recovery Firmware
24 * +------------+---------------+----------------+------------+
25 * | |
26 * | |
27 * | Other PSP Firmware |
28 * | |
29 * | |
30 * +------------+---------------+----------------+------------+
Zheng Bao4fcc9f22015-11-20 12:29:04 +080031 *
zbaoc3b0b722016-02-19 13:47:31 +080032 * PSP Combo directory
Zheng Bao4fcc9f22015-11-20 12:29:04 +080033 * +------------+---------------+----------------+------------+
zbao6e2f3d12016-02-19 13:34:59 +080034 * | 'PSP2' | Fletcher | Count |Look up mode|
Zheng Bao4fcc9f22015-11-20 12:29:04 +080035 * +------------+---------------+----------------+------------+
zbaoc3a08a92016-03-02 14:47:27 +080036 * | R e s e r v e d |
37 * +------------+---------------+----------------+------------+
zbao6e2f3d12016-02-19 13:34:59 +080038 * | ID-Sel | PSP ID | PSPDIR ADDR | | 2nd PSP directory
Zheng Bao4fcc9f22015-11-20 12:29:04 +080039 * +------------+---------------+----------------+------------+
zbao6e2f3d12016-02-19 13:34:59 +080040 * | ID-Sel | PSP ID | PSPDIR ADDR | | 3rd PSP directory
Zheng Bao4fcc9f22015-11-20 12:29:04 +080041 * +------------+---------------+----------------+------------+
42 * | |
43 * | Other PSP |
44 * | |
45 * +------------+---------------+----------------+------------+
46 *
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080047 */
48
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080049#include <fcntl.h>
50#include <errno.h>
Martin Roth37305e72020-04-07 14:16:39 -060051#include <stdbool.h>
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080052#include <stdio.h>
53#include <sys/stat.h>
54#include <sys/types.h>
55#include <unistd.h>
56#include <string.h>
57#include <stdlib.h>
58#include <getopt.h>
Zheng Baoc5e28ab2020-10-28 11:38:09 +080059#include <libgen.h>
Idwer Vollering93df1d92020-12-30 00:01:59 +010060#include <stdint.h>
Zheng Baoc5e28ab2020-10-28 11:38:09 +080061
62#include "amdfwtool.h"
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080063
Martin Roth60f15512016-11-08 09:55:01 -070064#define AMD_ROMSIG_OFFSET 0x20000
65#define MIN_ROM_KB 256
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080066
Martin Rothcd15bc82016-11-08 11:34:02 -070067#define ALIGN(val, by) (((val) + (by) - 1) & ~((by) - 1))
Marshall Dawson7c1e1422019-04-11 09:44:43 -060068#define _MAX(A, B) (((A) > (B)) ? (A) : (B))
69#define ERASE_ALIGNMENT 0x1000U
Marshall Dawson2794a862019-03-04 16:53:15 -070070#define TABLE_ALIGNMENT 0x1000U
71#define BLOB_ALIGNMENT 0x100U
Marshall Dawson24f73d42019-04-01 10:48:43 -060072#define TABLE_ERASE_ALIGNMENT _MAX(TABLE_ALIGNMENT, ERASE_ALIGNMENT)
Marshall Dawson7c1e1422019-04-11 09:44:43 -060073#define BLOB_ERASE_ALIGNMENT _MAX(BLOB_ALIGNMENT, ERASE_ALIGNMENT)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080074
Marshall Dawsonef79fcc2019-04-01 10:16:41 -060075#define DEFAULT_SOFT_FUSE_CHAIN "0x1"
76
Marshall Dawson239286c2019-02-23 16:42:46 -070077#define EMBEDDED_FW_SIGNATURE 0x55aa55aa
Marshall Dawson24f73d42019-04-01 10:48:43 -060078#define PSP_COOKIE 0x50535024 /* 'PSP$' */
79#define PSPL2_COOKIE 0x324c5024 /* '2LP$' */
80#define PSP2_COOKIE 0x50535032 /* 'PSP2' */
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -060081#define BDT1_COOKIE 0x44484224 /* 'DHB$ */
82#define BDT2_COOKIE 0x324c4224 /* '2LB$ */
Marshall Dawson239286c2019-02-23 16:42:46 -070083
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080084/*
Marshall Dawson0e02ce82019-03-04 16:50:37 -070085 * Beginning with Family 15h Models 70h-7F, a.k.a Stoney Ridge, the PSP
86 * can support an optional "combo" implementation. If the PSP sees the
87 * PSP2 cookie, it interprets the table as a roadmap to additional PSP
88 * tables. Using this, support for multiple product generations may be
89 * built into one image. If the PSP$ cookie is found, the table is a
90 * normal directory table.
91 *
92 * Modern generations supporting the combo directories require the
93 * pointer to be at offset 0x14 of the Embedded Firmware Structure,
94 * regardless of the type of directory used. The --combo-capable
95 * argument enforces this placement.
96 *
97 * TODO: Future work may require fully implementing the PSP_COMBO feature.
zbaoc3b0b722016-02-19 13:47:31 +080098 */
Marshall Dawson0e02ce82019-03-04 16:50:37 -070099#define PSP_COMBO 0
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800100
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800101/*
102 * Creates the OSI Fletcher checksum. See 8473-1, Appendix C, section C.3.
103 * The checksum field of the passed PDU does not need to be reset to zero.
104 *
105 * The "Fletcher Checksum" was proposed in a paper by John G. Fletcher of
106 * Lawrence Livermore Labs. The Fletcher Checksum was proposed as an
107 * alternative to cyclical redundancy checks because it provides error-
108 * detection properties similar to cyclical redundancy checks but at the
109 * cost of a simple summation technique. Its characteristics were first
110 * published in IEEE Transactions on Communications in January 1982. One
111 * version has been adopted by ISO for use in the class-4 transport layer
112 * of the network protocol.
113 *
114 * This program expects:
115 * stdin: The input file to compute a checksum for. The input file
116 * not be longer than 256 bytes.
117 * stdout: Copied from the input file with the Fletcher's Checksum
118 * inserted 8 bytes after the beginning of the file.
119 * stderr: Used to print out error messages.
120 */
Marshall Dawson8a45a4d2019-02-24 07:18:44 -0700121static uint32_t fletcher32(const void *data, int length)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800122{
123 uint32_t c0;
124 uint32_t c1;
125 uint32_t checksum;
126 int index;
Marshall Dawson8a45a4d2019-02-24 07:18:44 -0700127 const uint16_t *pptr = data;
128
129 length /= 2;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800130
131 c0 = 0xFFFF;
132 c1 = 0xFFFF;
133
Marshall Dawsonb85ddc52019-07-23 07:24:30 -0600134 while (length) {
135 index = length >= 359 ? 359 : length;
136 length -= index;
137 do {
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800138 c0 += *(pptr++);
139 c1 += c0;
Marshall Dawsonb85ddc52019-07-23 07:24:30 -0600140 } while (--index);
141 c0 = (c0 & 0xFFFF) + (c0 >> 16);
142 c1 = (c1 & 0xFFFF) + (c1 >> 16);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800143 }
144
Marshall Dawson8a45a4d2019-02-24 07:18:44 -0700145 /* Sums[0,1] mod 64K + overflow */
146 c0 = (c0 & 0xFFFF) + (c0 >> 16);
147 c1 = (c1 & 0xFFFF) + (c1 >> 16);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800148 checksum = (c1 << 16) | c0;
149
150 return checksum;
151}
152
Martin Roth8806f7f2016-11-08 10:44:18 -0700153static void usage(void)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800154{
Martin Roth0e940622016-11-08 10:37:53 -0700155 printf("amdfwtool: Create AMD Firmware combination\n");
Zheng Bao806892a2021-04-27 17:21:54 +0800156 printf("Usage: amdfwtool [options] --flashsize <size> --output <filename>\n");
157 printf("--xhci <FILE> Add XHCI blob\n");
158 printf("--imc <FILE> Add IMC blob\n");
159 printf("--gec <FILE> Add GEC blob\n");
Martin Roth0e940622016-11-08 10:37:53 -0700160
161 printf("\nPSP options:\n");
Zheng Bao806892a2021-04-27 17:21:54 +0800162 printf("--combo-capable Place PSP directory pointer at Embedded\n");
163 printf(" Firmware\n");
Marshall Dawson67d868d2019-02-28 11:43:40 -0700164 printf(" offset able to support combo directory\n");
Zheng Bao806892a2021-04-27 17:21:54 +0800165 printf("--multilevel Generate primary and secondary tables\n");
166 printf("--nvram <FILE> Add nvram binary\n");
167 printf("--soft-fuse Set soft fuse\n");
168 printf("--token-unlock Set token unlock\n");
169 printf("--whitelist Set if there is a whitelist\n");
170 printf("--use-pspsecureos Set if psp secure OS is needed\n");
171 printf("--load-mp2-fw Set if load MP2 firmware\n");
172 printf("--load-s0i3 Set if load s0i3 firmware\n");
173 printf("--verstage <FILE> Add verstage\n");
174 printf("--verstage_sig Add verstage signature\n");
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600175 printf("\nBIOS options:\n");
Zheng Bao806892a2021-04-27 17:21:54 +0800176 printf("--instance <number> Sets instance field for the next BIOS\n");
Zheng Bao6f0b3612021-04-27 17:19:43 +0800177 printf(" firmware\n");
Zheng Bao806892a2021-04-27 17:21:54 +0800178 printf("--apcb <FILE> Add AGESA PSP customization block\n");
179 printf("--apob-base <HEX_VAL> Destination for AGESA PSP output block\n");
180 printf("--apob-nv-base <HEX_VAL> Location of S3 resume data\n");
181 printf("--apob-nv-size <HEX_VAL> Size of S3 resume data\n");
182 printf("--ucode <FILE> Add microcode patch\n");
183 printf("--bios-bin <FILE> Add compressed image; auto source address\n");
184 printf("--bios-bin-src <HEX_VAL> Address in flash of source if -V not used\n");
185 printf("--bios-bin-dest <HEX_VAL> Destination for uncompressed BIOS\n");
186 printf("--bios-uncomp-size <HEX> Uncompressed size of BIOS image\n");
187 printf("--output <filename> output filename\n");
188 printf("--flashsize <HEX_VAL> ROM size in bytes\n");
Marshall Dawsonf4b9b412017-03-17 16:30:51 -0600189 printf(" size must be larger than %dKB\n",
Martin Roth0e940622016-11-08 10:37:53 -0700190 MIN_ROM_KB);
Marshall Dawsonf4b9b412017-03-17 16:30:51 -0600191 printf(" and must a multiple of 1024\n");
Zheng Bao806892a2021-04-27 17:21:54 +0800192 printf("--location Location of Directory\n");
193 printf("--anywhere Use any 64-byte aligned addr for Directory\n");
194 printf("--sharedmem Location of PSP/FW shared memory\n");
195 printf("--sharedmem-size Maximum size of the PSP/FW shared memory\n");
Zheng Bao6f0b3612021-04-27 17:19:43 +0800196 printf(" area\n");
Zheng Bao806892a2021-04-27 17:21:54 +0800197 printf("--soc-name <socname> Specify SOC name. Supported names are\n");
Zheng Bao6f0b3612021-04-27 17:19:43 +0800198 printf(" Stoneyridge, Raven, Picasso, Renoir, Cezanne\n");
199 printf(" or Lucienne\n");
Matt Papageorgebe4376c2020-06-15 11:18:15 -0500200 printf("\nEmbedded Firmware Structure options used by the PSP:\n");
201 printf("--spi-speed <HEX_VAL> SPI fast speed to place in EFS Table\n");
202 printf(" 0x0 66.66Mhz\n");
203 printf(" 0x1 33.33MHz\n");
204 printf(" 0x2 22.22MHz\n");
205 printf(" 0x3 16.66MHz\n");
206 printf(" 0x4 100MHz\n");
207 printf(" 0x5 800KHz\n");
208 printf("--spi-read-mode <HEX_VAL> SPI read mode to place in EFS Table\n");
209 printf(" 0x0 Normal Read (up to 33M)\n");
210 printf(" 0x1 Reserved\n");
211 printf(" 0x2 Dual IO (1-1-2)\n");
212 printf(" 0x3 Quad IO (1-1-4)\n");
213 printf(" 0x4 Dual IO (1-2-2)\n");
214 printf(" 0x5 Quad IO (1-4-4)\n");
215 printf(" 0x6 Normal Read (up to 66M)\n");
216 printf(" 0x7 Fast Read\n");
217 printf("--spi-micron-flag <HEX_VAL> Micron SPI part support for RV and later SOC\n");
218 printf(" 0x0 Micron parts are not used\n");
219 printf(" 0x1 Micron parts are always used\n");
220 printf(" 0x2 Micron parts optional, this option is only\n");
221 printf(" supported with RN/LCN SOC\n");
Zheng Bao806892a2021-04-27 17:21:54 +0800222 printf("\nGeneral options:\n");
223 printf("-c|--config <config file> Config file\n");
224 printf("-d|--debug Print debug message\n");
225 printf("-l|--list List out the firmware files\n");
226 printf("-h|--help Show this help\n");
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800227}
228
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800229amd_fw_entry amd_psp_fw_table[] = {
Marshall Dawson24f73d42019-04-01 10:48:43 -0600230 { .type = AMD_FW_PSP_PUBKEY, .level = PSP_BOTH },
231 { .type = AMD_FW_PSP_BOOTLOADER, .level = PSP_BOTH },
232 { .type = AMD_FW_PSP_SMU_FIRMWARE, .subprog = 0, .level = PSP_BOTH },
233 { .type = AMD_FW_PSP_RECOVERY, .level = PSP_LVL1 },
234 { .type = AMD_FW_PSP_RTM_PUBKEY, .level = PSP_BOTH },
235 { .type = AMD_FW_PSP_SECURED_OS, .level = PSP_LVL2 },
236 { .type = AMD_FW_PSP_NVRAM, .level = PSP_LVL2 },
237 { .type = AMD_FW_PSP_SMU_FIRMWARE, .subprog = 2, .level = PSP_BOTH },
238 { .type = AMD_FW_PSP_SECURED_DEBUG, .level = PSP_LVL2 },
239 { .type = AMD_FW_PSP_TRUSTLETS, .level = PSP_LVL2 },
240 { .type = AMD_FW_PSP_TRUSTLETKEY, .level = PSP_LVL2 },
241 { .type = AMD_FW_PSP_SMU_FIRMWARE2, .subprog = 2, .level = PSP_BOTH },
242 { .type = AMD_FW_PSP_SMU_FIRMWARE, .subprog = 1, .level = PSP_BOTH },
243 { .type = AMD_FW_PSP_SMU_FIRMWARE2, .subprog = 1, .level = PSP_BOTH },
244 { .type = AMD_FW_PSP_SMU_FIRMWARE2, .level = PSP_BOTH },
245 { .type = AMD_FW_PSP_SMUSCS, .level = PSP_BOTH },
246 { .type = AMD_PSP_FUSE_CHAIN, .level = PSP_LVL2 },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600247 { .type = AMD_DEBUG_UNLOCK, .level = PSP_LVL2 },
Zheng Baobf29a0d2020-12-03 23:00:48 +0800248 { .type = AMD_HW_IPCFG, .level = PSP_LVL2 },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600249 { .type = AMD_WRAPPED_IKEK, .level = PSP_BOTH },
250 { .type = AMD_TOKEN_UNLOCK, .level = PSP_BOTH },
Zheng Baobf29a0d2020-12-03 23:00:48 +0800251 { .type = AMD_SEC_GASKET, .subprog = 0, .level = PSP_BOTH },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600252 { .type = AMD_SEC_GASKET, .subprog = 2, .level = PSP_BOTH },
253 { .type = AMD_SEC_GASKET, .subprog = 1, .level = PSP_BOTH },
254 { .type = AMD_MP2_FW, .subprog = 2, .level = PSP_LVL2 },
255 { .type = AMD_MP2_FW, .subprog = 1, .level = PSP_LVL2 },
Zheng Baobf29a0d2020-12-03 23:00:48 +0800256 { .type = AMD_MP2_FW, .subprog = 0, .level = PSP_LVL2 },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600257 { .type = AMD_DRIVER_ENTRIES, .level = PSP_LVL2 },
Zheng Baobf29a0d2020-12-03 23:00:48 +0800258 { .type = AMD_FW_KVM_IMAGE, .level = PSP_LVL2},
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600259 { .type = AMD_S0I3_DRIVER, .level = PSP_LVL2 },
Zheng Baobf29a0d2020-12-03 23:00:48 +0800260 { .type = AMD_VBIOS_BTLOADER, .level = PSP_BOTH },
261 { .type = AMD_FW_TOS_SEC_POLICY, .level = PSP_BOTH },
262 { .type = AMD_FW_USB_PHY, .level = PSP_LVL2 },
263 { .type = AMD_FW_DRTM_TA, .level = PSP_LVL2 },
264 { .type = AMD_FW_KEYDB_BL, .level = PSP_BOTH },
265 { .type = AMD_FW_KEYDB_TOS, .level = PSP_LVL2 },
266 { .type = AMD_FW_DMCU_ERAM, .level = PSP_LVL2 },
267 { .type = AMD_FW_DMCU_ISR, .level = PSP_LVL2 },
268 { .type = AMD_RPMC_NVRAM, .level = PSP_LVL2 },
Zheng Baob993cb22021-02-02 18:48:23 +0800269 { .type = AMD_FW_PSP_BOOTLOADER_AB, .level = PSP_LVL2 },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600270 { .type = AMD_ABL0, .level = PSP_BOTH },
271 { .type = AMD_ABL1, .level = PSP_BOTH },
272 { .type = AMD_ABL2, .level = PSP_BOTH },
273 { .type = AMD_ABL3, .level = PSP_BOTH },
274 { .type = AMD_ABL4, .level = PSP_BOTH },
275 { .type = AMD_ABL5, .level = PSP_BOTH },
276 { .type = AMD_ABL6, .level = PSP_BOTH },
277 { .type = AMD_ABL7, .level = PSP_BOTH },
Marshall Dawson24f73d42019-04-01 10:48:43 -0600278 { .type = AMD_FW_PSP_SMU_FIRMWARE, .subprog = 1, .level = PSP_BOTH },
279 { .type = AMD_FW_PSP_SMU_FIRMWARE2, .subprog = 1, .level = PSP_BOTH },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600280 { .type = AMD_FW_PSP_WHITELIST, .level = PSP_LVL2 },
Martin Rothd3ce8c82019-07-13 20:13:07 -0600281 { .type = AMD_FW_PSP_VERSTAGE, .level = PSP_BOTH },
Martin Rothb1f648f2020-09-01 09:36:59 -0600282 { .type = AMD_FW_VERSTAGE_SIG, .level = PSP_BOTH },
zbaoc3a08a92016-03-02 14:47:27 +0800283 { .type = AMD_FW_INVALID },
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800284};
285
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800286amd_fw_entry amd_fw_table[] = {
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800287 { .type = AMD_FW_XHCI },
288 { .type = AMD_FW_IMC },
289 { .type = AMD_FW_GEC },
zbaoc3a08a92016-03-02 14:47:27 +0800290 { .type = AMD_FW_INVALID },
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800291};
292
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800293amd_bios_entry amd_bios_table[] = {
Zheng Baobf29a0d2020-12-03 23:00:48 +0800294 { .type = AMD_BIOS_RTM_PUBKEY, .inst = 0, .level = BDT_BOTH },
Marshall Dawson0581bf62019-09-25 11:03:53 -0600295 { .type = AMD_BIOS_APCB, .inst = 0, .level = BDT_BOTH },
296 { .type = AMD_BIOS_APCB, .inst = 1, .level = BDT_BOTH },
297 { .type = AMD_BIOS_APCB, .inst = 2, .level = BDT_BOTH },
298 { .type = AMD_BIOS_APCB, .inst = 3, .level = BDT_BOTH },
299 { .type = AMD_BIOS_APCB, .inst = 4, .level = BDT_BOTH },
Rob Barnes18fd26c2020-03-03 10:35:02 -0700300 { .type = AMD_BIOS_APCB, .inst = 5, .level = BDT_BOTH },
301 { .type = AMD_BIOS_APCB, .inst = 6, .level = BDT_BOTH },
302 { .type = AMD_BIOS_APCB, .inst = 7, .level = BDT_BOTH },
303 { .type = AMD_BIOS_APCB, .inst = 8, .level = BDT_BOTH },
304 { .type = AMD_BIOS_APCB, .inst = 9, .level = BDT_BOTH },
305 { .type = AMD_BIOS_APCB, .inst = 10, .level = BDT_BOTH },
306 { .type = AMD_BIOS_APCB, .inst = 11, .level = BDT_BOTH },
307 { .type = AMD_BIOS_APCB, .inst = 12, .level = BDT_BOTH },
308 { .type = AMD_BIOS_APCB, .inst = 13, .level = BDT_BOTH },
309 { .type = AMD_BIOS_APCB, .inst = 14, .level = BDT_BOTH },
310 { .type = AMD_BIOS_APCB, .inst = 15, .level = BDT_BOTH },
Marshall Dawson2dd3b5c2020-01-03 17:57:48 -0700311 { .type = AMD_BIOS_APCB_BK, .inst = 0, .level = BDT_BOTH },
312 { .type = AMD_BIOS_APCB_BK, .inst = 1, .level = BDT_BOTH },
313 { .type = AMD_BIOS_APCB_BK, .inst = 2, .level = BDT_BOTH },
314 { .type = AMD_BIOS_APCB_BK, .inst = 3, .level = BDT_BOTH },
315 { .type = AMD_BIOS_APCB_BK, .inst = 4, .level = BDT_BOTH },
Rob Barnes18fd26c2020-03-03 10:35:02 -0700316 { .type = AMD_BIOS_APCB_BK, .inst = 5, .level = BDT_BOTH },
317 { .type = AMD_BIOS_APCB_BK, .inst = 6, .level = BDT_BOTH },
318 { .type = AMD_BIOS_APCB_BK, .inst = 7, .level = BDT_BOTH },
319 { .type = AMD_BIOS_APCB_BK, .inst = 8, .level = BDT_BOTH },
320 { .type = AMD_BIOS_APCB_BK, .inst = 9, .level = BDT_BOTH },
321 { .type = AMD_BIOS_APCB_BK, .inst = 10, .level = BDT_BOTH },
322 { .type = AMD_BIOS_APCB_BK, .inst = 11, .level = BDT_BOTH },
323 { .type = AMD_BIOS_APCB_BK, .inst = 12, .level = BDT_BOTH },
324 { .type = AMD_BIOS_APCB_BK, .inst = 13, .level = BDT_BOTH },
325 { .type = AMD_BIOS_APCB_BK, .inst = 14, .level = BDT_BOTH },
326 { .type = AMD_BIOS_APCB_BK, .inst = 15, .level = BDT_BOTH },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600327 { .type = AMD_BIOS_APOB, .level = BDT_BOTH },
328 { .type = AMD_BIOS_BIN,
329 .reset = 1, .copy = 1, .zlib = 1, .level = BDT_BOTH },
330 { .type = AMD_BIOS_APOB_NV, .level = BDT_LVL2 },
331 { .type = AMD_BIOS_PMUI, .inst = 1, .subpr = 0, .level = BDT_BOTH },
332 { .type = AMD_BIOS_PMUD, .inst = 1, .subpr = 0, .level = BDT_BOTH },
333 { .type = AMD_BIOS_PMUI, .inst = 4, .subpr = 0, .level = BDT_BOTH },
334 { .type = AMD_BIOS_PMUD, .inst = 4, .subpr = 0, .level = BDT_BOTH },
335 { .type = AMD_BIOS_PMUI, .inst = 1, .subpr = 1, .level = BDT_BOTH },
336 { .type = AMD_BIOS_PMUD, .inst = 1, .subpr = 1, .level = BDT_BOTH },
337 { .type = AMD_BIOS_PMUI, .inst = 4, .subpr = 1, .level = BDT_BOTH },
338 { .type = AMD_BIOS_PMUD, .inst = 4, .subpr = 1, .level = BDT_BOTH },
339 { .type = AMD_BIOS_UCODE, .inst = 0, .level = BDT_LVL2 },
340 { .type = AMD_BIOS_UCODE, .inst = 1, .level = BDT_LVL2 },
341 { .type = AMD_BIOS_UCODE, .inst = 2, .level = BDT_LVL2 },
342 { .type = AMD_BIOS_MP2_CFG, .level = BDT_LVL2 },
Martin Roth94554742020-04-14 14:59:36 -0600343 { .type = AMD_BIOS_PSP_SHARED_MEM, .inst = 0, .level = BDT_BOTH },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600344 { .type = AMD_BIOS_INVALID },
345};
346
Marshall Dawson13ec0292020-11-19 14:02:29 -0700347struct second_gen_efs { /* todo: expand for Server products */
348 int gen:1; /* Client products only use bit 0 */
349 int reserved:31;
350} __attribute__((packed));
351
352#define EFS_SECOND_GEN 0
353
Marshall Dawson239286c2019-02-23 16:42:46 -0700354typedef struct _embedded_firmware {
355 uint32_t signature; /* 0x55aa55aa */
356 uint32_t imc_entry;
357 uint32_t gec_entry;
358 uint32_t xhci_entry;
359 uint32_t psp_entry;
360 uint32_t comboable;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600361 uint32_t bios0_entry; /* todo: add way to select correct entry */
362 uint32_t bios1_entry;
Marshall Dawson94f24922019-09-28 08:49:09 -0600363 uint32_t bios2_entry;
Marshall Dawson13ec0292020-11-19 14:02:29 -0700364 struct second_gen_efs efs_gen;
Matt Papageorgebe4376c2020-06-15 11:18:15 -0500365 uint32_t bios3_entry;
366 uint32_t reserved_2Ch;
367 uint32_t promontory_fw_ptr;
368 uint32_t lp_promontory_fw_ptr;
369 uint32_t reserved_38h;
370 uint32_t reserved_3Ch;
371 uint8_t spi_readmode_f15_mod_60_6f;
372 uint8_t fast_speed_new_f15_mod_60_6f;
373 uint8_t reserved_42h;
374 uint8_t spi_readmode_f17_mod_00_2f;
375 uint8_t spi_fastspeed_f17_mod_00_2f;
376 uint8_t qpr_dummy_cycle_f17_mod_00_2f;
377 uint8_t reserved_46h;
378 uint8_t spi_readmode_f17_mod_30_3f;
379 uint8_t spi_fastspeed_f17_mod_30_3f;
380 uint8_t micron_detect_f17_mod_30_3f;
381 uint8_t reserved_4Ah;
382 uint8_t reserved_4Bh;
383 uint32_t reserved_4Ch;
Marshall Dawson239286c2019-02-23 16:42:46 -0700384} __attribute__((packed, aligned(16))) embedded_firmware;
385
386typedef struct _psp_directory_header {
387 uint32_t cookie;
388 uint32_t checksum;
389 uint32_t num_entries;
Zheng Baobf29a0d2020-12-03 23:00:48 +0800390 uint32_t additional_info;
Marshall Dawson239286c2019-02-23 16:42:46 -0700391} __attribute__((packed, aligned(16))) psp_directory_header;
392
393typedef struct _psp_directory_entry {
Marshall Dawsondbae6322019-03-04 10:31:03 -0700394 uint8_t type;
395 uint8_t subprog;
396 uint16_t rsvd;
Marshall Dawson239286c2019-02-23 16:42:46 -0700397 uint32_t size;
398 uint64_t addr; /* or a value in some cases */
399} __attribute__((packed)) psp_directory_entry;
400
401typedef struct _psp_directory_table {
402 psp_directory_header header;
403 psp_directory_entry entries[];
Martin Rotha8e31ca2021-02-13 21:42:46 -0700404} __attribute__((packed, aligned(16))) psp_directory_table;
Marshall Dawson239286c2019-02-23 16:42:46 -0700405
Marshall Dawson2794a862019-03-04 16:53:15 -0700406#define MAX_PSP_ENTRIES 0x1f
407
Marshall Dawson239286c2019-02-23 16:42:46 -0700408typedef struct _psp_combo_header {
409 uint32_t cookie;
410 uint32_t checksum;
411 uint32_t num_entries;
412 uint32_t lookup;
413 uint64_t reserved[2];
414} __attribute__((packed, aligned(16))) psp_combo_header;
415
416typedef struct _psp_combo_entry {
417 uint32_t id_sel;
418 uint32_t id;
419 uint64_t lvl2_addr;
420} __attribute__((packed)) psp_combo_entry;
421
422typedef struct _psp_combo_directory {
423 psp_combo_header header;
424 psp_combo_entry entries[];
Martin Rotha8e31ca2021-02-13 21:42:46 -0700425} __attribute__((packed, aligned(16))) psp_combo_directory;
Marshall Dawson239286c2019-02-23 16:42:46 -0700426
Marshall Dawson2794a862019-03-04 16:53:15 -0700427#define MAX_COMBO_ENTRIES 1
428
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600429typedef struct _bios_directory_hdr {
430 uint32_t cookie;
431 uint32_t checksum;
432 uint32_t num_entries;
Zheng Baobf29a0d2020-12-03 23:00:48 +0800433 uint32_t additional_info;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600434} __attribute__((packed, aligned(16))) bios_directory_hdr;
435
436typedef struct _bios_directory_entry {
437 uint8_t type;
438 uint8_t region_type;
439 int reset:1;
440 int copy:1;
441 int ro:1;
442 int compressed:1;
443 int inst:4;
444 uint8_t subprog; /* b[7:3] reserved */
445 uint32_t size;
446 uint64_t source;
447 uint64_t dest;
448} __attribute__((packed)) bios_directory_entry;
449
450typedef struct _bios_directory_table {
451 bios_directory_hdr header;
452 bios_directory_entry entries[];
453} bios_directory_table;
454
Martin Roth94554742020-04-14 14:59:36 -0600455#define MAX_BIOS_ENTRIES 0x2f
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600456
Marshall Dawson2794a862019-03-04 16:53:15 -0700457typedef struct _context {
458 char *rom; /* target buffer, size of flash device */
459 uint32_t rom_size; /* size of flash device */
460 uint32_t current; /* pointer within flash & proxy buffer */
461} context;
462
463#define RUN_BASE(ctx) (0xFFFFFFFF - (ctx).rom_size + 1)
464#define RUN_OFFSET(ctx, offset) (RUN_BASE(ctx) + (offset))
465#define RUN_CURRENT(ctx) RUN_OFFSET((ctx), (ctx).current)
466#define BUFF_OFFSET(ctx, offset) ((void *)((ctx).rom + (offset)))
467#define BUFF_CURRENT(ctx) BUFF_OFFSET((ctx), (ctx).current)
468#define BUFF_TO_RUN(ctx, ptr) RUN_OFFSET((ctx), ((char *)(ptr) - (ctx).rom))
469#define BUFF_ROOM(ctx) ((ctx).rom_size - (ctx).current)
470
Marshall Dawson24f73d42019-04-01 10:48:43 -0600471static void *new_psp_dir(context *ctx, int multi)
Marshall Dawson2794a862019-03-04 16:53:15 -0700472{
473 void *ptr;
474
Marshall Dawson24f73d42019-04-01 10:48:43 -0600475 /*
476 * Force both onto boundary when multi. Primary table is after
477 * updatable table, so alignment ensures primary can stay intact
478 * if secondary is reprogrammed.
479 */
480 if (multi)
481 ctx->current = ALIGN(ctx->current, TABLE_ERASE_ALIGNMENT);
482 else
483 ctx->current = ALIGN(ctx->current, TABLE_ALIGNMENT);
484
Marshall Dawson2794a862019-03-04 16:53:15 -0700485 ptr = BUFF_CURRENT(*ctx);
Zheng Baobf29a0d2020-12-03 23:00:48 +0800486 ((psp_directory_header *)ptr)->additional_info = ctx->current;
Marshall Dawson2794a862019-03-04 16:53:15 -0700487 ctx->current += sizeof(psp_directory_header)
488 + MAX_PSP_ENTRIES * sizeof(psp_directory_entry);
489 return ptr;
490}
491
Martin Rothec933132019-07-13 20:03:34 -0600492#if PSP_COMBO
Marshall Dawson2794a862019-03-04 16:53:15 -0700493static void *new_combo_dir(context *ctx)
494{
495 void *ptr;
496
497 ctx->current = ALIGN(ctx->current, TABLE_ALIGNMENT);
498 ptr = BUFF_CURRENT(*ctx);
499 ctx->current += sizeof(psp_combo_header)
500 + MAX_COMBO_ENTRIES * sizeof(psp_combo_entry);
501 return ptr;
502}
Martin Rothec933132019-07-13 20:03:34 -0600503#endif
Marshall Dawson2794a862019-03-04 16:53:15 -0700504
Zheng Baobf29a0d2020-12-03 23:00:48 +0800505static void fill_dir_header(void *directory, uint32_t count, uint32_t cookie, context *ctx)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800506{
Marshall Dawson24f73d42019-04-01 10:48:43 -0600507 psp_combo_directory *cdir = directory;
508 psp_directory_table *dir = directory;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600509 bios_directory_table *bdir = directory;
Zheng Baobf29a0d2020-12-03 23:00:48 +0800510 uint32_t table_size = 0;
Marshall Dawson24f73d42019-04-01 10:48:43 -0600511
512 if (!count)
513 return;
514
Zheng Baobf29a0d2020-12-03 23:00:48 +0800515 /* The table size needs to be 0x1000 aligned. So align the end of table. */
516 if (ctx != NULL)
517 ctx->current = ALIGN(ctx->current, TABLE_ALIGNMENT);
518
Marshall Dawson24f73d42019-04-01 10:48:43 -0600519 switch (cookie) {
520 case PSP2_COOKIE:
Marshall Dawsona378c222019-03-04 16:52:07 -0700521 /* caller is responsible for lookup mode */
Marshall Dawsona378c222019-03-04 16:52:07 -0700522 cdir->header.cookie = cookie;
523 cdir->header.num_entries = count;
524 cdir->header.reserved[0] = 0;
525 cdir->header.reserved[1] = 0;
526 /* checksum everything that comes after the Checksum field */
527 cdir->header.checksum = fletcher32(&cdir->header.num_entries,
528 count * sizeof(psp_combo_entry)
529 + sizeof(cdir->header.num_entries)
530 + sizeof(cdir->header.lookup)
531 + 2 * sizeof(cdir->header.reserved[0]));
Marshall Dawson24f73d42019-04-01 10:48:43 -0600532 break;
533 case PSP_COOKIE:
534 case PSPL2_COOKIE:
Zheng Baobf29a0d2020-12-03 23:00:48 +0800535 table_size = ctx->current - dir->header.additional_info;
536 if ((table_size % TABLE_ALIGNMENT) != 0) {
537 fprintf(stderr, "The PSP table size should be 4K aligned\n");
538 exit(1);
539 }
Marshall Dawsona378c222019-03-04 16:52:07 -0700540 dir->header.cookie = cookie;
541 dir->header.num_entries = count;
Zheng Baobf29a0d2020-12-03 23:00:48 +0800542 dir->header.additional_info = (table_size / 0x1000) | (1 << 10);
Marshall Dawsona378c222019-03-04 16:52:07 -0700543 /* checksum everything that comes after the Checksum field */
544 dir->header.checksum = fletcher32(&dir->header.num_entries,
Marshall Dawson8a45a4d2019-02-24 07:18:44 -0700545 count * sizeof(psp_directory_entry)
Marshall Dawsona378c222019-03-04 16:52:07 -0700546 + sizeof(dir->header.num_entries)
Zheng Baobf29a0d2020-12-03 23:00:48 +0800547 + sizeof(dir->header.additional_info));
Marshall Dawson24f73d42019-04-01 10:48:43 -0600548 break;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600549 case BDT1_COOKIE:
550 case BDT2_COOKIE:
Zheng Baobf29a0d2020-12-03 23:00:48 +0800551 table_size = ctx->current - bdir->header.additional_info;
552 if ((table_size % TABLE_ALIGNMENT) != 0) {
553 fprintf(stderr, "The BIOS table size should be 4K aligned\n");
554 exit(1);
555 }
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600556 bdir->header.cookie = cookie;
557 bdir->header.num_entries = count;
Zheng Baobf29a0d2020-12-03 23:00:48 +0800558 bdir->header.additional_info = (table_size / 0x1000) | (1 << 10);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600559 /* checksum everything that comes after the Checksum field */
560 bdir->header.checksum = fletcher32(&bdir->header.num_entries,
561 count * sizeof(bios_directory_entry)
562 + sizeof(bdir->header.num_entries)
Zheng Baobf29a0d2020-12-03 23:00:48 +0800563 + sizeof(bdir->header.additional_info));
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600564 break;
Marshall Dawsona378c222019-03-04 16:52:07 -0700565 }
Zheng Baobf29a0d2020-12-03 23:00:48 +0800566
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800567}
568
Marshall Dawson8e0dca02019-02-27 18:40:49 -0700569static ssize_t copy_blob(void *dest, const char *src_file, size_t room)
570{
571 int fd;
572 struct stat fd_stat;
573 ssize_t bytes;
574
575 fd = open(src_file, O_RDONLY);
576 if (fd < 0) {
Zheng Bao77a2c672020-10-01 17:05:43 +0800577 fprintf(stderr, "Error opening file: %s: %s\n",
Eric Peersaf505672020-03-05 16:04:15 -0700578 src_file, strerror(errno));
Marshall Dawson8e0dca02019-02-27 18:40:49 -0700579 return -1;
580 }
581
582 if (fstat(fd, &fd_stat)) {
Zheng Bao77a2c672020-10-01 17:05:43 +0800583 fprintf(stderr, "fstat error: %s\n", strerror(errno));
Jacob Garber967f8622019-07-02 10:35:10 -0600584 close(fd);
Marshall Dawson8e0dca02019-02-27 18:40:49 -0700585 return -2;
586 }
587
Zheng Bao6d402ac2020-10-01 16:16:30 +0800588 if ((size_t)fd_stat.st_size > room) {
Zheng Bao77a2c672020-10-01 17:05:43 +0800589 fprintf(stderr, "Error: %s will not fit. Exiting.\n", src_file);
Jacob Garber967f8622019-07-02 10:35:10 -0600590 close(fd);
Marshall Dawson8e0dca02019-02-27 18:40:49 -0700591 return -3;
592 }
593
594 bytes = read(fd, dest, (size_t)fd_stat.st_size);
595 close(fd);
596 if (bytes != (ssize_t)fd_stat.st_size) {
Zheng Bao77a2c672020-10-01 17:05:43 +0800597 fprintf(stderr, "Error while reading %s\n", src_file);
Marshall Dawson8e0dca02019-02-27 18:40:49 -0700598 return -4;
599 }
600
601 return bytes;
602}
603
Marshall Dawson2794a862019-03-04 16:53:15 -0700604static void integrate_firmwares(context *ctx,
Marshall Dawson239286c2019-02-23 16:42:46 -0700605 embedded_firmware *romsig,
Marshall Dawson2794a862019-03-04 16:53:15 -0700606 amd_fw_entry *fw_table)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800607{
Richard Spiegel137484d2018-01-17 10:23:19 -0700608 ssize_t bytes;
Zheng Bao6d402ac2020-10-01 16:16:30 +0800609 uint32_t i;
Marshall Dawson2794a862019-03-04 16:53:15 -0700610
611 ctx->current += sizeof(embedded_firmware);
612 ctx->current = ALIGN(ctx->current, BLOB_ALIGNMENT);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800613
Martin Rothcd15bc82016-11-08 11:34:02 -0700614 for (i = 0; fw_table[i].type != AMD_FW_INVALID; i++) {
zbaoc3a08a92016-03-02 14:47:27 +0800615 if (fw_table[i].filename != NULL) {
zbaoc3a08a92016-03-02 14:47:27 +0800616 switch (fw_table[i].type) {
617 case AMD_FW_IMC:
Marshall Dawson2794a862019-03-04 16:53:15 -0700618 ctx->current = ALIGN(ctx->current, 0x10000U);
619 romsig->imc_entry = RUN_CURRENT(*ctx);
zbaoc3a08a92016-03-02 14:47:27 +0800620 break;
621 case AMD_FW_GEC:
Marshall Dawson2794a862019-03-04 16:53:15 -0700622 romsig->gec_entry = RUN_CURRENT(*ctx);
zbaoc3a08a92016-03-02 14:47:27 +0800623 break;
624 case AMD_FW_XHCI:
Marshall Dawson2794a862019-03-04 16:53:15 -0700625 romsig->xhci_entry = RUN_CURRENT(*ctx);
zbaoc3a08a92016-03-02 14:47:27 +0800626 break;
627 default:
628 /* Error */
629 break;
630 }
631
Marshall Dawson2794a862019-03-04 16:53:15 -0700632 bytes = copy_blob(BUFF_CURRENT(*ctx),
633 fw_table[i].filename, BUFF_ROOM(*ctx));
Marshall Dawson02bd7732019-03-13 14:43:17 -0600634 if (bytes < 0) {
Marshall Dawson2794a862019-03-04 16:53:15 -0700635 free(ctx->rom);
Martin Roth60f15512016-11-08 09:55:01 -0700636 exit(1);
637 }
638
Marshall Dawson2794a862019-03-04 16:53:15 -0700639 ctx->current = ALIGN(ctx->current + bytes,
640 BLOB_ALIGNMENT);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800641 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800642 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800643}
644
Zheng Bao9e908072020-10-28 11:39:13 +0800645/* For debugging */
646static void dump_psp_firmwares(amd_fw_entry *fw_table)
647{
648 amd_fw_entry *index;
649
650 printf("PSP firmware components:");
651 for (index = fw_table; index->type != AMD_FW_INVALID; index++) {
652 if (index->filename)
653 printf(" filename=%s\n", index->filename);
654 }
655}
656
657static void dump_bdt_firmwares(amd_bios_entry *fw_table)
658{
659 amd_bios_entry *index;
660
661 printf("BIOS Directory Table (BDT) components:");
662 for (index = fw_table; index->type != AMD_BIOS_INVALID; index++) {
663 if (index->filename)
664 printf(" filename=%s\n", index->filename);
665 }
666}
667
Zheng Baoc5e28ab2020-10-28 11:38:09 +0800668static void free_psp_firmware_filenames(amd_fw_entry *fw_table)
669{
670 amd_fw_entry *index;
671
672 for (index = fw_table; index->type != AMD_FW_INVALID; index++) {
673 if (index->filename &&
674 index->type != AMD_FW_VERSTAGE_SIG &&
675 index->type != AMD_FW_PSP_VERSTAGE &&
676 index->type != AMD_FW_PSP_WHITELIST) {
677 free(index->filename);
678 }
679 }
680}
681
682static void free_bdt_firmware_filenames(amd_bios_entry *fw_table)
683{
684 amd_bios_entry *index;
685
686 for (index = fw_table; index->type != AMD_BIOS_INVALID; index++) {
687 if (index->filename &&
688 index->type != AMD_BIOS_APCB &&
689 index->type != AMD_BIOS_BIN &&
690 index->type != AMD_BIOS_APCB_BK)
691 free(index->filename);
692 }
693}
694
Marshall Dawson2794a862019-03-04 16:53:15 -0700695static void integrate_psp_firmwares(context *ctx,
Marshall Dawson239286c2019-02-23 16:42:46 -0700696 psp_directory_table *pspdir,
Marshall Dawson24f73d42019-04-01 10:48:43 -0600697 psp_directory_table *pspdir2,
698 amd_fw_entry *fw_table,
699 uint32_t cookie)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800700{
Richard Spiegel137484d2018-01-17 10:23:19 -0700701 ssize_t bytes;
Marshall Dawsonc38c0c92019-02-23 16:41:35 -0700702 unsigned int i, count;
Marshall Dawson24f73d42019-04-01 10:48:43 -0600703 int level;
704
705 /* This function can create a primary table, a secondary table, or a
706 * flattened table which contains all applicable types. These if-else
707 * statements infer what the caller intended. If a 2nd-level cookie
708 * is passed, clearly a 2nd-level table is intended. However, a
709 * 1st-level cookie may indicate level 1 or flattened. If the caller
710 * passes a pointer to a 2nd-level table, then assume not flat.
711 */
712 if (cookie == PSPL2_COOKIE)
713 level = PSP_LVL2;
714 else if (pspdir2)
715 level = PSP_LVL1;
716 else
717 level = PSP_BOTH;
Marshall Dawson2794a862019-03-04 16:53:15 -0700718
Zheng Baobf29a0d2020-12-03 23:00:48 +0800719 ctx->current = ALIGN(ctx->current, TABLE_ALIGNMENT);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800720
Marshall Dawsonc38c0c92019-02-23 16:41:35 -0700721 for (i = 0, count = 0; fw_table[i].type != AMD_FW_INVALID; i++) {
Marshall Dawson24f73d42019-04-01 10:48:43 -0600722 if (!(fw_table[i].level & level))
723 continue;
724
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600725 if (fw_table[i].type == AMD_TOKEN_UNLOCK) {
726 if (!fw_table[i].other)
727 continue;
728 ctx->current = ALIGN(ctx->current, ERASE_ALIGNMENT);
729 pspdir->entries[count].type = fw_table[i].type;
730 pspdir->entries[count].size = 4096; /* TODO: doc? */
731 pspdir->entries[count].addr = RUN_CURRENT(*ctx);
732 pspdir->entries[count].subprog = fw_table[i].subprog;
733 pspdir->entries[count].rsvd = 0;
734 ctx->current = ALIGN(ctx->current + 4096, 0x100U);
735 count++;
736 } else if (fw_table[i].type == AMD_PSP_FUSE_CHAIN) {
Marshall Dawson239286c2019-02-23 16:42:46 -0700737 pspdir->entries[count].type = fw_table[i].type;
Marshall Dawsondbae6322019-03-04 10:31:03 -0700738 pspdir->entries[count].subprog = fw_table[i].subprog;
739 pspdir->entries[count].rsvd = 0;
Marshall Dawson239286c2019-02-23 16:42:46 -0700740 pspdir->entries[count].size = 0xFFFFFFFF;
Marshall Dawsonef79fcc2019-04-01 10:16:41 -0600741 pspdir->entries[count].addr = fw_table[i].other;
Marshall Dawsonc38c0c92019-02-23 16:41:35 -0700742 count++;
Marshall Dawson7c1e1422019-04-11 09:44:43 -0600743 } else if (fw_table[i].type == AMD_FW_PSP_NVRAM) {
744 if (fw_table[i].filename == NULL)
745 continue;
746 /* TODO: Add a way to reserve for NVRAM without
747 * requiring a filename. This isn't a feature used
748 * by coreboot systems, so priority is very low.
749 */
750 ctx->current = ALIGN(ctx->current, ERASE_ALIGNMENT);
751 bytes = copy_blob(BUFF_CURRENT(*ctx),
752 fw_table[i].filename, BUFF_ROOM(*ctx));
753 if (bytes <= 0) {
754 free(ctx->rom);
755 exit(1);
756 }
757
758 pspdir->entries[count].type = fw_table[i].type;
759 pspdir->entries[count].subprog = fw_table[i].subprog;
760 pspdir->entries[count].rsvd = 0;
761 pspdir->entries[count].size = ALIGN(bytes,
762 ERASE_ALIGNMENT);
763 pspdir->entries[count].addr = RUN_CURRENT(*ctx);
764
765 ctx->current = ALIGN(ctx->current + bytes,
766 BLOB_ERASE_ALIGNMENT);
767 count++;
zbaoc3a08a92016-03-02 14:47:27 +0800768 } else if (fw_table[i].filename != NULL) {
Marshall Dawson2794a862019-03-04 16:53:15 -0700769 bytes = copy_blob(BUFF_CURRENT(*ctx),
770 fw_table[i].filename, BUFF_ROOM(*ctx));
Marshall Dawson02bd7732019-03-13 14:43:17 -0600771 if (bytes < 0) {
Marshall Dawson2794a862019-03-04 16:53:15 -0700772 free(ctx->rom);
Marshall Dawson8e0dca02019-02-27 18:40:49 -0700773 exit(1);
774 }
775
Marshall Dawson239286c2019-02-23 16:42:46 -0700776 pspdir->entries[count].type = fw_table[i].type;
Marshall Dawsondbae6322019-03-04 10:31:03 -0700777 pspdir->entries[count].subprog = fw_table[i].subprog;
778 pspdir->entries[count].rsvd = 0;
Marshall Dawson8e0dca02019-02-27 18:40:49 -0700779 pspdir->entries[count].size = (uint32_t)bytes;
Marshall Dawson2794a862019-03-04 16:53:15 -0700780 pspdir->entries[count].addr = RUN_CURRENT(*ctx);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800781
Marshall Dawson2794a862019-03-04 16:53:15 -0700782 ctx->current = ALIGN(ctx->current + bytes,
783 BLOB_ALIGNMENT);
Marshall Dawsonc38c0c92019-02-23 16:41:35 -0700784 count++;
zbaoc3a08a92016-03-02 14:47:27 +0800785 } else {
786 /* This APU doesn't have this firmware. */
787 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800788 }
Marshall Dawson2794a862019-03-04 16:53:15 -0700789
Marshall Dawson24f73d42019-04-01 10:48:43 -0600790 if (pspdir2) {
791 pspdir->entries[count].type = AMD_FW_L2_PTR;
792 pspdir->entries[count].subprog = 0;
793 pspdir->entries[count].rsvd = 0;
794 pspdir->entries[count].size = sizeof(pspdir2->header)
795 + pspdir2->header.num_entries
796 * sizeof(psp_directory_entry);
797
798 pspdir->entries[count].addr = BUFF_TO_RUN(*ctx, pspdir2);
799 count++;
800 }
801
Marshall Dawson2794a862019-03-04 16:53:15 -0700802 if (count > MAX_PSP_ENTRIES) {
Zheng Bao77a2c672020-10-01 17:05:43 +0800803 fprintf(stderr, "Error: PSP entries exceed max allowed items\n");
Marshall Dawson2794a862019-03-04 16:53:15 -0700804 free(ctx->rom);
805 exit(1);
806 }
807
Zheng Baobf29a0d2020-12-03 23:00:48 +0800808 fill_dir_header(pspdir, count, cookie, ctx);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800809}
810
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600811static void *new_bios_dir(context *ctx, int multi)
812{
813 void *ptr;
814
815 /*
816 * Force both onto boundary when multi. Primary table is after
817 * updatable table, so alignment ensures primary can stay intact
818 * if secondary is reprogrammed.
819 */
820 if (multi)
821 ctx->current = ALIGN(ctx->current, TABLE_ERASE_ALIGNMENT);
822 else
823 ctx->current = ALIGN(ctx->current, TABLE_ALIGNMENT);
824 ptr = BUFF_CURRENT(*ctx);
Zheng Baobf29a0d2020-12-03 23:00:48 +0800825 ((bios_directory_hdr *) ptr)->additional_info = ctx->current;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600826 ctx->current += sizeof(bios_directory_hdr)
827 + MAX_BIOS_ENTRIES * sizeof(bios_directory_entry);
828 return ptr;
829}
830
831static int locate_bdt2_bios(bios_directory_table *level2,
832 uint64_t *source, uint32_t *size)
833{
Zheng Bao6d402ac2020-10-01 16:16:30 +0800834 uint32_t i;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600835
836 *source = 0;
837 *size = 0;
838 if (!level2)
839 return 0;
840
841 for (i = 0 ; i < level2->header.num_entries ; i++) {
842 if (level2->entries[i].type == AMD_BIOS_BIN) {
843 *source = level2->entries[i].source;
844 *size = level2->entries[i].size;
845 return 1;
846 }
847 }
848 return 0;
849}
850
851static int have_bios_tables(amd_bios_entry *table)
852{
853 int i;
854
855 for (i = 0 ; table[i].type != AMD_BIOS_INVALID; i++) {
856 if (table[i].level & BDT_LVL1 && table[i].filename)
857 return 1;
858 }
859 return 0;
860}
861
Marshall Dawsonc4a8c482020-01-21 17:17:59 -0700862static int find_bios_entry(amd_bios_type type)
863{
864 int i;
865
866 for (i = 0; amd_bios_table[i].type != AMD_BIOS_INVALID; i++) {
867 if (amd_bios_table[i].type == type)
868 return i;
869 }
870 return -1;
871}
872
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600873static void integrate_bios_firmwares(context *ctx,
874 bios_directory_table *biosdir,
875 bios_directory_table *biosdir2,
876 amd_bios_entry *fw_table,
877 uint32_t cookie)
878{
879 ssize_t bytes;
Martin Rothec933132019-07-13 20:03:34 -0600880 unsigned int i, count;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600881 int level;
Marshall Dawsonc4a8c482020-01-21 17:17:59 -0700882 int apob_idx;
Martin Rotheca423b2020-09-01 10:54:11 -0600883 uint32_t size;
884 uint64_t source;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600885
886 /* This function can create a primary table, a secondary table, or a
887 * flattened table which contains all applicable types. These if-else
888 * statements infer what the caller intended. If a 2nd-level cookie
889 * is passed, clearly a 2nd-level table is intended. However, a
890 * 1st-level cookie may indicate level 1 or flattened. If the caller
891 * passes a pointer to a 2nd-level table, then assume not flat.
892 */
893 if (cookie == BDT2_COOKIE)
894 level = BDT_LVL2;
895 else if (biosdir2)
896 level = BDT_LVL1;
897 else
898 level = BDT_BOTH;
899
Zheng Baobf29a0d2020-12-03 23:00:48 +0800900 ctx->current = ALIGN(ctx->current, TABLE_ALIGNMENT);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600901
902 for (i = 0, count = 0; fw_table[i].type != AMD_BIOS_INVALID; i++) {
903 if (!(fw_table[i].level & level))
904 continue;
905 if (fw_table[i].filename == NULL && (
906 fw_table[i].type != AMD_BIOS_APOB &&
907 fw_table[i].type != AMD_BIOS_APOB_NV &&
908 fw_table[i].type != AMD_BIOS_L2_PTR &&
Martin Roth94554742020-04-14 14:59:36 -0600909 fw_table[i].type != AMD_BIOS_BIN &&
910 fw_table[i].type != AMD_BIOS_PSP_SHARED_MEM))
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600911 continue;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600912
913 /* BIOS Directory items may have additional requirements */
914
Martin Roth48dd9fe2020-07-29 16:32:25 -0600915 /* Check APOB_NV requirements */
916 if (fw_table[i].type == AMD_BIOS_APOB_NV) {
917 if (!fw_table[i].size && !fw_table[i].src)
918 continue; /* APOB_NV not used */
919 if (fw_table[i].src && !fw_table[i].size) {
Zheng Bao77a2c672020-10-01 17:05:43 +0800920 fprintf(stderr, "Error: APOB NV address provided, but no size\n");
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600921 free(ctx->rom);
922 exit(1);
923 }
Martin Roth48dd9fe2020-07-29 16:32:25 -0600924 /* If the APOB isn't used, APOB_NV isn't used either */
Marshall Dawsonc4a8c482020-01-21 17:17:59 -0700925 apob_idx = find_bios_entry(AMD_BIOS_APOB);
Martin Roth48dd9fe2020-07-29 16:32:25 -0600926 if (apob_idx < 0 || !fw_table[apob_idx].dest)
927 continue; /* APOV NV not supported */
Marshall Dawsonc4a8c482020-01-21 17:17:59 -0700928 }
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600929
930 /* APOB_DATA needs destination */
931 if (fw_table[i].type == AMD_BIOS_APOB && !fw_table[i].dest) {
Zheng Bao77a2c672020-10-01 17:05:43 +0800932 fprintf(stderr, "Error: APOB destination not provided\n");
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600933 free(ctx->rom);
934 exit(1);
935 }
936
937 /* BIOS binary must have destination and uncompressed size. If
938 * no filename given, then user must provide a source address.
939 */
940 if (fw_table[i].type == AMD_BIOS_BIN) {
941 if (!fw_table[i].dest || !fw_table[i].size) {
Zheng Bao77a2c672020-10-01 17:05:43 +0800942 fprintf(stderr, "Error: BIOS binary destination and uncompressed size are required\n");
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600943 free(ctx->rom);
944 exit(1);
945 }
946 if (!fw_table[i].filename && !fw_table[i].src) {
Zheng Bao77a2c672020-10-01 17:05:43 +0800947 fprintf(stderr, "Error: BIOS binary assumed outside amdfw.rom but no source address given\n");
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600948 free(ctx->rom);
949 exit(1);
950 }
951 }
952
Martin Roth94554742020-04-14 14:59:36 -0600953 /* PSP_SHARED_MEM needs a destination and size */
954 if (fw_table[i].type == AMD_BIOS_PSP_SHARED_MEM &&
955 (!fw_table[i].dest || !fw_table[i].size))
956 continue;
957
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600958 biosdir->entries[count].type = fw_table[i].type;
959 biosdir->entries[count].region_type = fw_table[i].region_type;
960 biosdir->entries[count].dest = fw_table[i].dest ?
961 fw_table[i].dest : (uint64_t)-1;
962 biosdir->entries[count].reset = fw_table[i].reset;
963 biosdir->entries[count].copy = fw_table[i].copy;
964 biosdir->entries[count].ro = fw_table[i].ro;
965 biosdir->entries[count].compressed = fw_table[i].zlib;
966 biosdir->entries[count].inst = fw_table[i].inst;
967 biosdir->entries[count].subprog = fw_table[i].subpr;
968
969 switch (fw_table[i].type) {
970 case AMD_BIOS_APOB:
971 biosdir->entries[count].size = fw_table[i].size;
972 biosdir->entries[count].source = fw_table[i].src;
973 break;
974 case AMD_BIOS_APOB_NV:
975 if (fw_table[i].src) {
976 /* If source is given, use that and its size */
977 biosdir->entries[count].source = fw_table[i].src;
978 biosdir->entries[count].size = fw_table[i].size;
979 } else {
980 /* Else reserve size bytes within amdfw.rom */
981 ctx->current = ALIGN(ctx->current, ERASE_ALIGNMENT);
982 biosdir->entries[count].source = RUN_CURRENT(*ctx);
983 biosdir->entries[count].size = ALIGN(
984 fw_table[i].size, ERASE_ALIGNMENT);
985 memset(BUFF_CURRENT(*ctx), 0xff,
986 biosdir->entries[count].size);
987 ctx->current = ctx->current
988 + biosdir->entries[count].size;
989 }
990 break;
991 case AMD_BIOS_BIN:
992 /* Don't make a 2nd copy, point to the same one */
Martin Rotheca423b2020-09-01 10:54:11 -0600993 if (level == BDT_LVL1 && locate_bdt2_bios(biosdir2, &source, &size)) {
994 biosdir->entries[count].source = source;
995 biosdir->entries[count].size = size;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600996 break;
Martin Rotheca423b2020-09-01 10:54:11 -0600997 }
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600998
999 /* level 2, or level 1 and no copy found in level 2 */
1000 biosdir->entries[count].source = fw_table[i].src;
1001 biosdir->entries[count].dest = fw_table[i].dest;
1002 biosdir->entries[count].size = fw_table[i].size;
1003
1004 if (!fw_table[i].filename)
1005 break;
1006
1007 bytes = copy_blob(BUFF_CURRENT(*ctx),
1008 fw_table[i].filename, BUFF_ROOM(*ctx));
1009 if (bytes <= 0) {
1010 free(ctx->rom);
1011 exit(1);
1012 }
1013
1014 biosdir->entries[count].source = RUN_CURRENT(*ctx);
1015
1016 ctx->current = ALIGN(ctx->current + bytes, 0x100U);
1017 break;
Martin Roth94554742020-04-14 14:59:36 -06001018 case AMD_BIOS_PSP_SHARED_MEM:
1019 biosdir->entries[count].dest = fw_table[i].dest;
1020 biosdir->entries[count].size = fw_table[i].size;
1021 break;
1022
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001023 default: /* everything else is copied from input */
1024 if (fw_table[i].type == AMD_BIOS_APCB ||
1025 fw_table[i].type == AMD_BIOS_APCB_BK)
1026 ctx->current = ALIGN(
1027 ctx->current, ERASE_ALIGNMENT);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001028 bytes = copy_blob(BUFF_CURRENT(*ctx),
1029 fw_table[i].filename, BUFF_ROOM(*ctx));
1030 if (bytes <= 0) {
1031 free(ctx->rom);
1032 exit(1);
1033 }
1034
1035 biosdir->entries[count].size = (uint32_t)bytes;
1036 biosdir->entries[count].source = RUN_CURRENT(*ctx);
1037
1038 ctx->current = ALIGN(ctx->current + bytes, 0x100U);
1039 break;
1040 }
1041
1042 count++;
1043 }
1044
1045 if (biosdir2) {
1046 biosdir->entries[count].type = AMD_BIOS_L2_PTR;
Zheng Baoe8e60432021-05-24 16:11:12 +08001047 biosdir->entries[count].region_type = 0;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001048 biosdir->entries[count].size =
1049 + MAX_BIOS_ENTRIES
1050 * sizeof(bios_directory_entry);
1051 biosdir->entries[count].source =
1052 BUFF_TO_RUN(*ctx, biosdir2);
1053 biosdir->entries[count].subprog = 0;
1054 biosdir->entries[count].inst = 0;
1055 biosdir->entries[count].copy = 0;
1056 biosdir->entries[count].compressed = 0;
1057 biosdir->entries[count].dest = -1;
1058 biosdir->entries[count].reset = 0;
1059 biosdir->entries[count].ro = 0;
1060 count++;
1061 }
1062
1063 if (count > MAX_BIOS_ENTRIES) {
Zheng Bao77a2c672020-10-01 17:05:43 +08001064 fprintf(stderr, "Error: BIOS entries (%d) exceeds max allowed items "
Rob Barnes18fd26c2020-03-03 10:35:02 -07001065 "(%d)\n", count, MAX_BIOS_ENTRIES);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001066 free(ctx->rom);
1067 exit(1);
1068 }
1069
Zheng Baobf29a0d2020-12-03 23:00:48 +08001070 fill_dir_header(biosdir, count, cookie, ctx);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001071}
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001072
1073enum {
Zheng Bao806892a2021-04-27 17:21:54 +08001074 AMDFW_OPT_CONFIG = 'c',
1075 AMDFW_OPT_DEBUG = 'd',
1076 AMDFW_OPT_HELP = 'h',
1077 AMDFW_OPT_LIST_DEPEND = 'l',
1078
1079 AMDFW_OPT_XHCI = 128,
1080 AMDFW_OPT_IMC,
1081 AMDFW_OPT_GEC,
1082 AMDFW_OPT_COMBO,
1083 AMDFW_OPT_MULTILEVEL,
1084 AMDFW_OPT_NVRAM,
1085
1086 AMDFW_OPT_FUSE,
1087 AMDFW_OPT_UNLOCK,
1088 AMDFW_OPT_WHITELIST,
1089 AMDFW_OPT_USE_PSPSECUREOS,
1090 AMDFW_OPT_LOAD_MP2FW,
1091 AMDFW_OPT_LOAD_S0I3,
1092 AMDFW_OPT_VERSTAGE,
1093 AMDFW_OPT_VERSTAGE_SIG,
1094
1095 AMDFW_OPT_INSTANCE,
1096 AMDFW_OPT_APCB,
1097 AMDFW_OPT_APOBBASE,
1098 AMDFW_OPT_BIOSBIN,
1099 AMDFW_OPT_BIOSBIN_SOURCE,
1100 AMDFW_OPT_BIOSBIN_DEST,
1101 AMDFW_OPT_BIOS_UNCOMP_SIZE,
1102 AMDFW_OPT_UCODE,
1103 AMDFW_OPT_APOB_NVBASE,
1104 AMDFW_OPT_APOB_NVSIZE,
1105
1106 AMDFW_OPT_OUTPUT,
1107 AMDFW_OPT_FLASHSIZE,
1108 AMDFW_OPT_LOCATION,
1109 AMDFW_OPT_ANYWHERE,
1110 AMDFW_OPT_SHAREDMEM,
1111 AMDFW_OPT_SHAREDMEM_SIZE,
1112 AMDFW_OPT_SOC_NAME,
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001113 /* begin after ASCII characters */
1114 LONGOPT_SPI_READ_MODE = 256,
1115 LONGOPT_SPI_SPEED = 257,
1116 LONGOPT_SPI_MICRON_FLAG = 258,
1117};
1118
Zheng Bao806892a2021-04-27 17:21:54 +08001119static char const optstring[] = {AMDFW_OPT_CONFIG, ':',
1120 AMDFW_OPT_DEBUG, AMDFW_OPT_HELP, AMDFW_OPT_LIST_DEPEND
1121};
Marc Jones90099b62016-09-20 21:05:45 -06001122
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001123static struct option long_options[] = {
Zheng Bao806892a2021-04-27 17:21:54 +08001124 {"xhci", required_argument, 0, AMDFW_OPT_XHCI },
1125 {"imc", required_argument, 0, AMDFW_OPT_IMC },
1126 {"gec", required_argument, 0, AMDFW_OPT_GEC },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001127 /* PSP Directory Table items */
Zheng Bao806892a2021-04-27 17:21:54 +08001128 {"combo-capable", no_argument, 0, AMDFW_OPT_COMBO },
1129 {"multilevel", no_argument, 0, AMDFW_OPT_MULTILEVEL },
1130 {"nvram", required_argument, 0, AMDFW_OPT_NVRAM },
1131 {"soft-fuse", required_argument, 0, AMDFW_OPT_FUSE },
1132 {"token-unlock", no_argument, 0, AMDFW_OPT_UNLOCK },
1133 {"whitelist", required_argument, 0, AMDFW_OPT_WHITELIST },
1134 {"use-pspsecureos", no_argument, 0, AMDFW_OPT_USE_PSPSECUREOS },
1135 {"load-mp2-fw", no_argument, 0, AMDFW_OPT_LOAD_MP2FW },
1136 {"load-s0i3", no_argument, 0, AMDFW_OPT_LOAD_S0I3 },
1137 {"verstage", required_argument, 0, AMDFW_OPT_VERSTAGE },
1138 {"verstage_sig", required_argument, 0, AMDFW_OPT_VERSTAGE_SIG },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001139 /* BIOS Directory Table items */
Zheng Bao806892a2021-04-27 17:21:54 +08001140 {"instance", required_argument, 0, AMDFW_OPT_INSTANCE },
1141 {"apcb", required_argument, 0, AMDFW_OPT_APCB },
1142 {"apob-base", required_argument, 0, AMDFW_OPT_APOBBASE },
1143 {"bios-bin", required_argument, 0, AMDFW_OPT_BIOSBIN },
1144 {"bios-bin-src", required_argument, 0, AMDFW_OPT_BIOSBIN_SOURCE },
1145 {"bios-bin-dest", required_argument, 0, AMDFW_OPT_BIOSBIN_DEST },
1146 {"bios-uncomp-size", required_argument, 0, AMDFW_OPT_BIOS_UNCOMP_SIZE },
1147 {"ucode", required_argument, 0, AMDFW_OPT_UCODE },
1148 {"apob-nv-base", required_argument, 0, AMDFW_OPT_APOB_NVBASE },
1149 {"apob-nv-size", required_argument, 0, AMDFW_OPT_APOB_NVSIZE },
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001150 /* Embedded Firmware Structure items*/
1151 {"spi-read-mode", required_argument, 0, LONGOPT_SPI_READ_MODE },
1152 {"spi-speed", required_argument, 0, LONGOPT_SPI_SPEED },
1153 {"spi-micron-flag", required_argument, 0, LONGOPT_SPI_MICRON_FLAG },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001154 /* other */
Zheng Bao806892a2021-04-27 17:21:54 +08001155 {"output", required_argument, 0, AMDFW_OPT_OUTPUT },
1156 {"flashsize", required_argument, 0, AMDFW_OPT_FLASHSIZE },
1157 {"location", required_argument, 0, AMDFW_OPT_LOCATION },
1158 {"anywhere", no_argument, 0, AMDFW_OPT_ANYWHERE },
1159 {"sharedmem", required_argument, 0, AMDFW_OPT_SHAREDMEM },
1160 {"sharedmem-size", required_argument, 0, AMDFW_OPT_SHAREDMEM_SIZE },
1161 {"soc-name", required_argument, 0, AMDFW_OPT_SOC_NAME },
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001162
Zheng Bao806892a2021-04-27 17:21:54 +08001163 {"config", required_argument, 0, AMDFW_OPT_CONFIG },
1164 {"debug", no_argument, 0, AMDFW_OPT_DEBUG },
1165 {"help", no_argument, 0, AMDFW_OPT_HELP },
1166 {"list", no_argument, 0, AMDFW_OPT_LIST_DEPEND },
Marshall Dawsonf4b9b412017-03-17 16:30:51 -06001167 {NULL, 0, 0, 0 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001168};
1169
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001170void register_fw_fuse(char *str)
Marshall Dawsonef79fcc2019-04-01 10:16:41 -06001171{
Zheng Bao6d402ac2020-10-01 16:16:30 +08001172 uint32_t i;
Marshall Dawsonef79fcc2019-04-01 10:16:41 -06001173
1174 for (i = 0; i < sizeof(amd_psp_fw_table) / sizeof(amd_fw_entry); i++) {
1175 if (amd_psp_fw_table[i].type != AMD_PSP_FUSE_CHAIN)
1176 continue;
1177
1178 amd_psp_fw_table[i].other = strtoull(str, NULL, 16);
1179 return;
1180 }
1181}
1182
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001183static void register_fw_token_unlock(void)
1184{
Zheng Bao6d402ac2020-10-01 16:16:30 +08001185 uint32_t i;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001186
1187 for (i = 0; i < sizeof(amd_psp_fw_table) / sizeof(amd_fw_entry); i++) {
1188 if (amd_psp_fw_table[i].type != AMD_TOKEN_UNLOCK)
1189 continue;
1190
1191 amd_psp_fw_table[i].other = 1;
1192 return;
1193 }
1194}
1195
Marshall Dawsondbae6322019-03-04 10:31:03 -07001196static void register_fw_filename(amd_fw_type type, uint8_t sub, char filename[])
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001197{
Martin Roth8806f7f2016-11-08 10:44:18 -07001198 unsigned int i;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001199
Martin Rothcd15bc82016-11-08 11:34:02 -07001200 for (i = 0; i < sizeof(amd_fw_table) / sizeof(amd_fw_entry); i++) {
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001201 if (amd_fw_table[i].type == type) {
1202 amd_fw_table[i].filename = filename;
1203 return;
1204 }
1205 }
1206
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001207 for (i = 0; i < sizeof(amd_psp_fw_table) / sizeof(amd_fw_entry); i++) {
Marshall Dawsondbae6322019-03-04 10:31:03 -07001208 if (amd_psp_fw_table[i].type != type)
1209 continue;
1210
1211 if (amd_psp_fw_table[i].subprog == sub) {
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001212 amd_psp_fw_table[i].filename = filename;
1213 return;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001214 }
1215 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001216}
1217
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001218static void register_bdt_data(amd_bios_type type, int sub, int ins, char name[])
1219{
Zheng Bao6d402ac2020-10-01 16:16:30 +08001220 uint32_t i;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001221
1222 for (i = 0; i < sizeof(amd_bios_table) / sizeof(amd_bios_entry); i++) {
1223 if (amd_bios_table[i].type == type
1224 && amd_bios_table[i].inst == ins
1225 && amd_bios_table[i].subpr == sub) {
1226 amd_bios_table[i].filename = name;
1227 return;
1228 }
1229 }
1230}
1231
Martin Rothec933132019-07-13 20:03:34 -06001232static void register_fw_addr(amd_bios_type type, char *src_str,
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001233 char *dst_str, char *size_str)
1234{
Zheng Bao6d402ac2020-10-01 16:16:30 +08001235 uint32_t i;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001236 for (i = 0; i < sizeof(amd_bios_table) / sizeof(amd_bios_entry); i++) {
1237 if (amd_bios_table[i].type != type)
1238 continue;
1239
1240 if (src_str)
1241 amd_bios_table[i].src = strtoull(src_str, NULL, 16);
1242 if (dst_str)
1243 amd_bios_table[i].dest = strtoull(dst_str, NULL, 16);
1244 if (size_str)
1245 amd_bios_table[i].size = strtoul(size_str, NULL, 16);
1246
1247 return;
1248 }
1249}
1250
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001251enum platform {
1252 PLATFORM_UNKNOWN,
1253 PLATFORM_STONEYRIDGE,
1254 PLATFORM_RAVEN,
1255 PLATFORM_PICASSO,
1256 PLATFORM_RENOIR,
Zheng Baobf29a0d2020-12-03 23:00:48 +08001257 PLATFORM_CEZANNE,
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001258 PLATFORM_LUCIENNE,
1259};
1260
1261static int set_efs_table(uint8_t soc_id, embedded_firmware *amd_romsig,
1262 uint8_t efs_spi_readmode, uint8_t efs_spi_speed,
1263 uint8_t efs_spi_micron_flag)
1264{
1265 if ((efs_spi_readmode == 0xFF) || (efs_spi_speed == 0xFF)) {
Zheng Bao77a2c672020-10-01 17:05:43 +08001266 fprintf(stderr, "Error: EFS read mode and SPI speed must be set\n");
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001267 return 1;
1268 }
1269 switch (soc_id) {
1270 case PLATFORM_STONEYRIDGE:
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001271 amd_romsig->spi_readmode_f15_mod_60_6f = efs_spi_readmode;
1272 amd_romsig->fast_speed_new_f15_mod_60_6f = efs_spi_speed;
1273 break;
1274 case PLATFORM_RAVEN:
1275 case PLATFORM_PICASSO:
Marshall Dawson13ec0292020-11-19 14:02:29 -07001276 /* amd_romsig->efs_gen introduced after RAVEN/PICASSO.
1277 * Leave as 0xffffffff for first gen */
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001278 amd_romsig->spi_readmode_f17_mod_00_2f = efs_spi_readmode;
1279 amd_romsig->spi_fastspeed_f17_mod_00_2f = efs_spi_speed;
1280 switch (efs_spi_micron_flag) {
1281 case 0:
1282 amd_romsig->qpr_dummy_cycle_f17_mod_00_2f = 0xff;
1283 break;
1284 case 1:
1285 amd_romsig->qpr_dummy_cycle_f17_mod_00_2f = 0xa;
1286 break;
1287 default:
Zheng Bao77a2c672020-10-01 17:05:43 +08001288 fprintf(stderr, "Error: EFS Micron flag must be correctly set.\n\n");
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001289 return 1;
1290 }
1291 break;
1292 case PLATFORM_RENOIR:
1293 case PLATFORM_LUCIENNE:
Zheng Baobf29a0d2020-12-03 23:00:48 +08001294 case PLATFORM_CEZANNE:
Marshall Dawson13ec0292020-11-19 14:02:29 -07001295 amd_romsig->efs_gen.gen = EFS_SECOND_GEN;
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001296 amd_romsig->spi_readmode_f17_mod_30_3f = efs_spi_readmode;
1297 amd_romsig->spi_fastspeed_f17_mod_30_3f = efs_spi_speed;
1298 switch (efs_spi_micron_flag) {
1299 case 0:
1300 amd_romsig->micron_detect_f17_mod_30_3f = 0xff;
1301 break;
1302 case 1:
1303 amd_romsig->micron_detect_f17_mod_30_3f = 0xaa;
1304 break;
1305 case 2:
1306 amd_romsig->micron_detect_f17_mod_30_3f = 0x55;
1307 break;
1308 default:
Zheng Bao77a2c672020-10-01 17:05:43 +08001309 fprintf(stderr, "Error: EFS Micron flag must be correctly set.\n\n");
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001310 return 1;
1311 }
1312 break;
1313 case PLATFORM_UNKNOWN:
1314 default:
Zheng Bao77a2c672020-10-01 17:05:43 +08001315 fprintf(stderr, "Error: Invalid SOC name.\n\n");
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001316 return 1;
1317 }
1318 return 0;
1319}
1320
1321static int identify_platform(char *soc_name)
1322{
1323 if (!strcasecmp(soc_name, "Stoneyridge"))
1324 return PLATFORM_STONEYRIDGE;
1325 else if (!strcasecmp(soc_name, "Raven"))
1326 return PLATFORM_RAVEN;
1327 else if (!strcasecmp(soc_name, "Picasso"))
1328 return PLATFORM_PICASSO;
Zheng Baobf29a0d2020-12-03 23:00:48 +08001329 else if (!strcasecmp(soc_name, "Cezanne"))
1330 return PLATFORM_CEZANNE;
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001331 else if (!strcasecmp(soc_name, "Renoir"))
1332 return PLATFORM_RENOIR;
1333 else if (!strcasecmp(soc_name, "Lucienne"))
1334 return PLATFORM_LUCIENNE;
1335 else
1336 return PLATFORM_UNKNOWN;
1337
1338}
1339
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001340int main(int argc, char **argv)
1341{
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001342 int c;
Martin Roth31d95a22016-11-08 11:22:12 -07001343 int retval = 0;
Martin Roth60f15512016-11-08 09:55:01 -07001344 char *tmp;
Martin Roth8806f7f2016-11-08 10:44:18 -07001345 char *rom = NULL;
Marshall Dawson239286c2019-02-23 16:42:46 -07001346 embedded_firmware *amd_romsig;
1347 psp_directory_table *pspdir;
Marshall Dawson67d868d2019-02-28 11:43:40 -07001348 int comboable = 0;
Marshall Dawsonef79fcc2019-04-01 10:16:41 -06001349 int fuse_defined = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001350 int targetfd;
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001351 char *output = NULL, *config = NULL;
1352 FILE *config_handle;
Zheng Bao9c8ce3e2020-09-28 10:36:29 +08001353 context ctx = { 0 };
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001354 /* Values cleared after each firmware or parameter, regardless if N/A */
1355 uint8_t sub = 0, instance = 0;
Martin Roth0d3b1182017-10-03 14:16:04 -06001356 uint32_t dir_location = 0;
Martin Roth37305e72020-04-07 14:16:39 -06001357 bool any_location = 0;
Martin Roth0d3b1182017-10-03 14:16:04 -06001358 uint32_t romsig_offset;
Martin Roth60f15512016-11-08 09:55:01 -07001359 uint32_t rom_base_address;
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001360 uint8_t soc_id = PLATFORM_UNKNOWN;
1361 uint8_t efs_spi_readmode = 0xff;
1362 uint8_t efs_spi_speed = 0xff;
1363 uint8_t efs_spi_micron_flag = 0xff;
1364
Marshall Dawson24f73d42019-04-01 10:48:43 -06001365 int multi = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001366 amd_cb_config cb_config;
Zheng Bao9e908072020-10-28 11:39:13 +08001367 int debug = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001368 int list_deps = 0;
1369
1370 cb_config.have_whitelist = 0;
1371 cb_config.unlock_secure = 0;
1372 cb_config.use_secureos = 0;
1373 cb_config.load_mp2_fw = 0;
1374 cb_config.s0i3 = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001375
1376 while (1) {
1377 int optindex = 0;
1378
1379 c = getopt_long(argc, argv, optstring, long_options, &optindex);
1380
1381 if (c == -1)
1382 break;
1383
1384 switch (c) {
Zheng Bao806892a2021-04-27 17:21:54 +08001385 case AMDFW_OPT_XHCI:
Marshall Dawsondbae6322019-03-04 10:31:03 -07001386 register_fw_filename(AMD_FW_XHCI, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001387 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001388 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001389 case AMDFW_OPT_IMC:
Marshall Dawsondbae6322019-03-04 10:31:03 -07001390 register_fw_filename(AMD_FW_IMC, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001391 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001392 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001393 case AMDFW_OPT_GEC:
Marshall Dawsondbae6322019-03-04 10:31:03 -07001394 register_fw_filename(AMD_FW_GEC, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001395 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001396 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001397 case AMDFW_OPT_COMBO:
Marshall Dawson67d868d2019-02-28 11:43:40 -07001398 comboable = 1;
1399 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001400 case AMDFW_OPT_MULTILEVEL:
Marshall Dawson24f73d42019-04-01 10:48:43 -06001401 multi = 1;
1402 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001403 case AMDFW_OPT_UNLOCK:
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001404 register_fw_token_unlock();
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001405 cb_config.unlock_secure = 1;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001406 sub = instance = 0;
1407 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001408 case AMDFW_OPT_USE_PSPSECUREOS:
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001409 cb_config.use_secureos = 1;
Marshall Dawsondbae6322019-03-04 10:31:03 -07001410 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001411 case AMDFW_OPT_INSTANCE:
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001412 instance = strtoul(optarg, &tmp, 16);
1413 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001414 case AMDFW_OPT_LOAD_MP2FW:
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001415 cb_config.load_mp2_fw = 1;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001416 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001417 case AMDFW_OPT_NVRAM:
Marshall Dawsondbae6322019-03-04 10:31:03 -07001418 register_fw_filename(AMD_FW_PSP_NVRAM, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001419 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001420 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001421 case AMDFW_OPT_FUSE:
Marshall Dawsonef79fcc2019-04-01 10:16:41 -06001422 register_fw_fuse(optarg);
1423 fuse_defined = 1;
1424 sub = 0;
1425 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001426 case AMDFW_OPT_APCB:
Zheng Bao5caca942020-12-04 16:39:38 +08001427 if ((instance & 0xF0) == 0)
1428 register_bdt_data(AMD_BIOS_APCB, sub, instance & 0xF, optarg);
1429 else
1430 register_bdt_data(AMD_BIOS_APCB_BK, sub,
1431 instance & 0xF, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001432 sub = instance = 0;
1433 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001434 case AMDFW_OPT_APOBBASE:
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001435 /* APOB destination */
1436 register_fw_addr(AMD_BIOS_APOB, 0, optarg, 0);
1437 sub = instance = 0;
1438 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001439 case AMDFW_OPT_APOB_NVBASE:
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001440 /* APOB NV source */
1441 register_fw_addr(AMD_BIOS_APOB_NV, optarg, 0, 0);
1442 sub = instance = 0;
1443 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001444 case AMDFW_OPT_APOB_NVSIZE:
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001445 /* APOB NV size */
1446 register_fw_addr(AMD_BIOS_APOB_NV, 0, 0, optarg);
1447 sub = instance = 0;
1448 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001449 case AMDFW_OPT_BIOSBIN:
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001450 register_bdt_data(AMD_BIOS_BIN, sub, instance, optarg);
1451 sub = instance = 0;
1452 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001453 case AMDFW_OPT_BIOSBIN_SOURCE:
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001454 /* BIOS source */
1455 register_fw_addr(AMD_BIOS_BIN, optarg, 0, 0);
1456 sub = instance = 0;
1457 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001458 case AMDFW_OPT_BIOSBIN_DEST:
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001459 /* BIOS destination */
1460 register_fw_addr(AMD_BIOS_BIN, 0, optarg, 0);
1461 sub = instance = 0;
1462 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001463 case AMDFW_OPT_BIOS_UNCOMP_SIZE:
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001464 /* BIOS destination size */
1465 register_fw_addr(AMD_BIOS_BIN, 0, 0, optarg);
1466 sub = instance = 0;
1467 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001468 case AMDFW_OPT_UCODE:
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001469 register_bdt_data(AMD_BIOS_UCODE, sub,
1470 instance, optarg);
1471 sub = instance = 0;
1472 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001473 case AMDFW_OPT_LOAD_S0I3:
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001474 cb_config.s0i3 = 1;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001475 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001476 case AMDFW_OPT_WHITELIST:
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001477 register_fw_filename(AMD_FW_PSP_WHITELIST, sub, optarg);
1478 sub = instance = 0;
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001479 cb_config.have_whitelist = 1;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001480 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001481 case AMDFW_OPT_VERSTAGE:
Martin Rothd3ce8c82019-07-13 20:13:07 -06001482 register_fw_filename(AMD_FW_PSP_VERSTAGE, sub, optarg);
1483 sub = instance = 0;
1484 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001485 case AMDFW_OPT_VERSTAGE_SIG:
Martin Rothb1f648f2020-09-01 09:36:59 -06001486 register_fw_filename(AMD_FW_VERSTAGE_SIG, sub, optarg);
1487 sub = instance = 0;
1488 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001489 case AMDFW_OPT_SOC_NAME:
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001490 soc_id = identify_platform(optarg);
1491 if (soc_id == PLATFORM_UNKNOWN) {
Zheng Bao77a2c672020-10-01 17:05:43 +08001492 fprintf(stderr, "Error: Invalid SOC name specified\n\n");
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001493 retval = 1;
1494 }
1495 sub = instance = 0;
1496 break;
1497 case LONGOPT_SPI_READ_MODE:
1498 efs_spi_readmode = strtoull(optarg, NULL, 16);
1499 sub = instance = 0;
1500 break;
1501 case LONGOPT_SPI_SPEED:
1502 efs_spi_speed = strtoull(optarg, NULL, 16);
1503 sub = instance = 0;
1504 break;
1505 case LONGOPT_SPI_MICRON_FLAG:
1506 efs_spi_micron_flag = strtoull(optarg, NULL, 16);
1507 sub = instance = 0;
1508 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001509 case AMDFW_OPT_OUTPUT:
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001510 output = optarg;
1511 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001512 case AMDFW_OPT_FLASHSIZE:
Marshall Dawson2794a862019-03-04 16:53:15 -07001513 ctx.rom_size = (uint32_t)strtoul(optarg, &tmp, 16);
Martin Roth60f15512016-11-08 09:55:01 -07001514 if (*tmp != '\0') {
Zheng Bao77a2c672020-10-01 17:05:43 +08001515 fprintf(stderr, "Error: ROM size specified"
Martin Roth60f15512016-11-08 09:55:01 -07001516 " incorrectly (%s)\n\n", optarg);
Martin Roth31d95a22016-11-08 11:22:12 -07001517 retval = 1;
Martin Roth60f15512016-11-08 09:55:01 -07001518 }
1519 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001520 case AMDFW_OPT_LOCATION:
Martin Roth0d3b1182017-10-03 14:16:04 -06001521 dir_location = (uint32_t)strtoul(optarg, &tmp, 16);
1522 if (*tmp != '\0') {
Zheng Bao77a2c672020-10-01 17:05:43 +08001523 fprintf(stderr, "Error: Directory Location specified"
Martin Roth0d3b1182017-10-03 14:16:04 -06001524 " incorrectly (%s)\n\n", optarg);
1525 retval = 1;
1526 }
1527 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001528 case AMDFW_OPT_ANYWHERE:
Martin Roth37305e72020-04-07 14:16:39 -06001529 any_location = 1;
1530 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001531 case AMDFW_OPT_SHAREDMEM:
Martin Roth94554742020-04-14 14:59:36 -06001532 /* shared memory destination */
1533 register_fw_addr(AMD_BIOS_PSP_SHARED_MEM, 0, optarg, 0);
1534 sub = instance = 0;
1535 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001536 case AMDFW_OPT_SHAREDMEM_SIZE:
Martin Roth94554742020-04-14 14:59:36 -06001537 /* shared memory size */
1538 register_fw_addr(AMD_BIOS_PSP_SHARED_MEM, NULL, NULL, optarg);
1539 sub = instance = 0;
1540 break;
Martin Roth0d3b1182017-10-03 14:16:04 -06001541
Zheng Bao806892a2021-04-27 17:21:54 +08001542 case AMDFW_OPT_CONFIG:
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001543 config = optarg;
1544 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001545 case AMDFW_OPT_DEBUG:
Zheng Bao9e908072020-10-28 11:39:13 +08001546 debug = 1;
1547 break;
Zheng Bao806892a2021-04-27 17:21:54 +08001548 case AMDFW_OPT_HELP:
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001549 usage();
Martin Roth31d95a22016-11-08 11:22:12 -07001550 return 0;
Zheng Bao806892a2021-04-27 17:21:54 +08001551 case AMDFW_OPT_LIST_DEPEND:
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001552 list_deps = 1;
1553 break;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001554 default:
1555 break;
1556 }
1557 }
1558
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001559 if (config) {
1560 config_handle = fopen(config, "r");
1561 if (config_handle == NULL) {
1562 fprintf(stderr, "Can not open file %s for reading: %s\n",
1563 config, strerror(errno));
1564 exit(1);
1565 }
1566 if (process_config(config_handle, &cb_config, list_deps) == 0) {
1567 fprintf(stderr, "Configuration file %s parsing error\n", config);
1568 fclose(config_handle);
1569 exit(1);
1570 }
1571 fclose(config_handle);
1572 }
Zheng Bao9e908072020-10-28 11:39:13 +08001573 /* For debug. */
1574 if (debug) {
1575 dump_psp_firmwares(amd_psp_fw_table);
1576 dump_bdt_firmwares(amd_bios_table);
1577 }
1578
Marshall Dawsonef79fcc2019-04-01 10:16:41 -06001579 if (!fuse_defined)
1580 register_fw_fuse(DEFAULT_SOFT_FUSE_CHAIN);
1581
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001582 if (!output && !list_deps) {
1583 fprintf(stderr, "Error: Output value is not specified.\n\n");
Martin Roth31d95a22016-11-08 11:22:12 -07001584 retval = 1;
1585 }
1586
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001587 if ((ctx.rom_size % 1024 != 0) && !list_deps) {
1588 fprintf(stderr, "Error: ROM Size (%d bytes) should be a multiple of"
Marshall Dawson2794a862019-03-04 16:53:15 -07001589 " 1024 bytes.\n\n", ctx.rom_size);
Martin Roth31d95a22016-11-08 11:22:12 -07001590 retval = 1;
Martin Roth60f15512016-11-08 09:55:01 -07001591 }
1592
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001593 if ((ctx.rom_size < MIN_ROM_KB * 1024) && !list_deps) {
1594 fprintf(stderr, "Error: ROM Size (%dKB) must be at least %dKB.\n\n",
Marshall Dawson2794a862019-03-04 16:53:15 -07001595 ctx.rom_size / 1024, MIN_ROM_KB);
Martin Roth31d95a22016-11-08 11:22:12 -07001596 retval = 1;
1597 }
1598
1599 if (retval) {
1600 usage();
1601 return retval;
Martin Roth60f15512016-11-08 09:55:01 -07001602 }
1603
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001604 if (list_deps) {
1605 return retval;
1606 }
1607
Marshall Dawson2794a862019-03-04 16:53:15 -07001608 printf(" AMDFWTOOL Using ROM size of %dKB\n", ctx.rom_size / 1024);
Martin Roth60f15512016-11-08 09:55:01 -07001609
Marshall Dawson2794a862019-03-04 16:53:15 -07001610 rom_base_address = 0xFFFFFFFF - ctx.rom_size + 1;
Martin Roth0d3b1182017-10-03 14:16:04 -06001611 if (dir_location && (dir_location < rom_base_address)) {
Zheng Bao77a2c672020-10-01 17:05:43 +08001612 fprintf(stderr, "Error: Directory location outside of ROM.\n\n");
Martin Roth0d3b1182017-10-03 14:16:04 -06001613 return 1;
1614 }
1615
Martin Roth37305e72020-04-07 14:16:39 -06001616 if (any_location) {
1617 if (dir_location & 0x3f) {
Zheng Bao77a2c672020-10-01 17:05:43 +08001618 fprintf(stderr, "Error: Invalid Directory location.\n");
1619 fprintf(stderr, " Valid locations are 64-byte aligned\n");
Martin Roth37305e72020-04-07 14:16:39 -06001620 return 1;
1621 }
1622 } else {
1623 switch (dir_location) {
1624 case 0: /* Fall through */
1625 case 0xFFFA0000: /* Fall through */
1626 case 0xFFF20000: /* Fall through */
1627 case 0xFFE20000: /* Fall through */
1628 case 0xFFC20000: /* Fall through */
1629 case 0xFF820000: /* Fall through */
1630 case 0xFF020000: /* Fall through */
1631 break;
1632 default:
Zheng Bao77a2c672020-10-01 17:05:43 +08001633 fprintf(stderr, "Error: Invalid Directory location.\n");
1634 fprintf(stderr, " Valid locations are 0xFFFA0000, 0xFFF20000,\n");
1635 fprintf(stderr, " 0xFFE20000, 0xFFC20000, 0xFF820000, 0xFF020000\n");
Martin Roth37305e72020-04-07 14:16:39 -06001636 return 1;
1637 }
Martin Roth0d3b1182017-10-03 14:16:04 -06001638 }
Marshall Dawson2794a862019-03-04 16:53:15 -07001639 ctx.rom = malloc(ctx.rom_size);
1640 if (!ctx.rom) {
Zheng Bao77a2c672020-10-01 17:05:43 +08001641 fprintf(stderr, "Error: Failed to allocate memory\n");
Martin Roth31d95a22016-11-08 11:22:12 -07001642 return 1;
Marshall Dawson2794a862019-03-04 16:53:15 -07001643 }
1644 memset(ctx.rom, 0xFF, ctx.rom_size);
Martin Roth60f15512016-11-08 09:55:01 -07001645
Martin Roth0d3b1182017-10-03 14:16:04 -06001646 if (dir_location)
Marshall Dawson2794a862019-03-04 16:53:15 -07001647 romsig_offset = ctx.current = dir_location - rom_base_address;
Martin Roth0d3b1182017-10-03 14:16:04 -06001648 else
Marshall Dawson2794a862019-03-04 16:53:15 -07001649 romsig_offset = ctx.current = AMD_ROMSIG_OFFSET;
1650 printf(" AMDFWTOOL Using firmware directory location of 0x%08x\n",
1651 RUN_CURRENT(ctx));
Martin Roth0d3b1182017-10-03 14:16:04 -06001652
Marshall Dawson2794a862019-03-04 16:53:15 -07001653 amd_romsig = BUFF_OFFSET(ctx, romsig_offset);
Marshall Dawson239286c2019-02-23 16:42:46 -07001654 amd_romsig->signature = EMBEDDED_FW_SIGNATURE;
1655 amd_romsig->imc_entry = 0;
1656 amd_romsig->gec_entry = 0;
1657 amd_romsig->xhci_entry = 0;
Martin Roth60f15512016-11-08 09:55:01 -07001658
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001659 if (soc_id != PLATFORM_UNKNOWN) {
1660 retval = set_efs_table(soc_id, amd_romsig, efs_spi_readmode,
1661 efs_spi_speed, efs_spi_micron_flag);
1662 if (retval) {
Zheng Bao77a2c672020-10-01 17:05:43 +08001663 fprintf(stderr, "ERROR: Failed to initialize EFS table!\n");
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001664 return retval;
1665 }
1666 } else {
Zheng Bao77a2c672020-10-01 17:05:43 +08001667 fprintf(stderr, "WARNING: No SOC name specified.\n");
Matt Papageorgebe4376c2020-06-15 11:18:15 -05001668 }
1669
Marshall Dawson2794a862019-03-04 16:53:15 -07001670 integrate_firmwares(&ctx, amd_romsig, amd_fw_table);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001671
Patrick Georgi900a2542020-02-17 16:52:40 +01001672 ctx.current = ALIGN(ctx.current, 0x10000U); /* TODO: is it necessary? */
Marshall Dawson2794a862019-03-04 16:53:15 -07001673
Marshall Dawson24f73d42019-04-01 10:48:43 -06001674 if (multi) {
1675 /* Do 2nd PSP directory followed by 1st */
1676 psp_directory_table *pspdir2 = new_psp_dir(&ctx, multi);
1677 integrate_psp_firmwares(&ctx, pspdir2, 0,
1678 amd_psp_fw_table, PSPL2_COOKIE);
1679
1680 pspdir = new_psp_dir(&ctx, multi);
1681 integrate_psp_firmwares(&ctx, pspdir, pspdir2,
1682 amd_psp_fw_table, PSP_COOKIE);
1683 } else {
1684 /* flat: PSP 1 cookie and no pointer to 2nd table */
1685 pspdir = new_psp_dir(&ctx, multi);
1686 integrate_psp_firmwares(&ctx, pspdir, 0,
1687 amd_psp_fw_table, PSP_COOKIE);
1688 }
Marshall Dawson2794a862019-03-04 16:53:15 -07001689
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001690 if (comboable)
Marshall Dawson2794a862019-03-04 16:53:15 -07001691 amd_romsig->comboable = BUFF_TO_RUN(ctx, pspdir);
Marshall Dawson67d868d2019-02-28 11:43:40 -07001692 else
Marshall Dawson2794a862019-03-04 16:53:15 -07001693 amd_romsig->psp_entry = BUFF_TO_RUN(ctx, pspdir);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001694
zbaoc3a08a92016-03-02 14:47:27 +08001695#if PSP_COMBO
Marshall Dawson2794a862019-03-04 16:53:15 -07001696 psp_combo_directory *combo_dir = new_combo_dir(&ctx);
1697 amd_romsig->comboable = BUFF_TO_RUN(ctx, combo_dir);
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001698 /* 0 -Compare PSP ID, 1 -Compare chip family ID */
1699 combo_dir->entries[0].id_sel = 0;
1700 /* TODO: PSP ID. Documentation is needed. */
1701 combo_dir->entries[0].id = 0x10220B00;
Marshall Dawson2794a862019-03-04 16:53:15 -07001702 combo_dir->entries[0].lvl2_addr = BUFF_TO_RUN(ctx, pspdir);
Zheng Bao4fcc9f22015-11-20 12:29:04 +08001703
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001704 combo_dir->header.lookup = 1;
Zheng Baobf29a0d2020-12-03 23:00:48 +08001705 fill_dir_header(combo_dir, 1, PSP2_COOKIE, NULL);
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001706#endif
Zheng Bao4fcc9f22015-11-20 12:29:04 +08001707
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001708 if (have_bios_tables(amd_bios_table)) {
1709 bios_directory_table *biosdir;
1710 if (multi) {
1711 /* Do 2nd level BIOS directory followed by 1st */
1712 bios_directory_table *biosdir2 =
1713 new_bios_dir(&ctx, multi);
1714 integrate_bios_firmwares(&ctx, biosdir2, 0,
1715 amd_bios_table, BDT2_COOKIE);
1716
1717 biosdir = new_bios_dir(&ctx, multi);
1718 integrate_bios_firmwares(&ctx, biosdir, biosdir2,
1719 amd_bios_table, BDT1_COOKIE);
1720 } else {
1721 /* flat: BDT1 cookie and no pointer to 2nd table */
1722 biosdir = new_bios_dir(&ctx, multi);
1723 integrate_bios_firmwares(&ctx, biosdir, 0,
1724 amd_bios_table, BDT1_COOKIE);
1725 }
Zheng Baobf29a0d2020-12-03 23:00:48 +08001726 switch (soc_id) {
1727 case PLATFORM_RENOIR:
1728 case PLATFORM_LUCIENNE:
1729 case PLATFORM_CEZANNE:
1730 amd_romsig->bios3_entry = BUFF_TO_RUN(ctx, biosdir);
1731 break;
1732 case PLATFORM_STONEYRIDGE:
1733 case PLATFORM_RAVEN:
1734 case PLATFORM_PICASSO:
1735 default:
1736 amd_romsig->bios1_entry = BUFF_TO_RUN(ctx, biosdir);
1737 break;
1738 }
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001739 }
1740
Zheng Baoc5e28ab2020-10-28 11:38:09 +08001741 /* Free the filename. */
1742 free_psp_firmware_filenames(amd_psp_fw_table);
1743 free_bdt_firmware_filenames(amd_bios_table);
1744
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001745 targetfd = open(output, O_RDWR | O_CREAT | O_TRUNC, 0666);
Martin Roth31d95a22016-11-08 11:22:12 -07001746 if (targetfd >= 0) {
Zheng Bao47396912020-09-29 17:33:17 +08001747 ssize_t bytes;
1748 bytes = write(targetfd, amd_romsig, ctx.current - romsig_offset);
1749 if (bytes != ctx.current - romsig_offset) {
1750 fprintf(stderr, "Error: Writing to file %s failed\n", output);
1751 retval = 1;
1752 }
Martin Roth31d95a22016-11-08 11:22:12 -07001753 close(targetfd);
1754 } else {
Zheng Bao77a2c672020-10-01 17:05:43 +08001755 fprintf(stderr, "Error: could not open file: %s\n", output);
Martin Roth31d95a22016-11-08 11:22:12 -07001756 retval = 1;
1757 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001758
Martin Roth31d95a22016-11-08 11:22:12 -07001759 free(rom);
1760 return retval;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001761}