blob: 42c63f1cf1294fb9451283939bdd2ab988721f2b [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>
51#include <stdio.h>
52#include <sys/stat.h>
53#include <sys/types.h>
54#include <unistd.h>
55#include <string.h>
56#include <stdlib.h>
57#include <getopt.h>
58
59#ifndef CONFIG_ROM_SIZE
60#define CONFIG_ROM_SIZE 0x400000
61#endif
62
Martin Roth60f15512016-11-08 09:55:01 -070063#define AMD_ROMSIG_OFFSET 0x20000
64#define MIN_ROM_KB 256
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080065
Martin Rothcd15bc82016-11-08 11:34:02 -070066#define ALIGN(val, by) (((val) + (by) - 1) & ~((by) - 1))
Marshall Dawson7c1e1422019-04-11 09:44:43 -060067#define _MAX(A, B) (((A) > (B)) ? (A) : (B))
68#define ERASE_ALIGNMENT 0x1000U
Marshall Dawson2794a862019-03-04 16:53:15 -070069#define TABLE_ALIGNMENT 0x1000U
70#define BLOB_ALIGNMENT 0x100U
Marshall Dawson24f73d42019-04-01 10:48:43 -060071#define TABLE_ERASE_ALIGNMENT _MAX(TABLE_ALIGNMENT, ERASE_ALIGNMENT)
Marshall Dawson7c1e1422019-04-11 09:44:43 -060072#define BLOB_ERASE_ALIGNMENT _MAX(BLOB_ALIGNMENT, ERASE_ALIGNMENT)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080073
Marshall Dawsonef79fcc2019-04-01 10:16:41 -060074#define DEFAULT_SOFT_FUSE_CHAIN "0x1"
75
Marshall Dawson239286c2019-02-23 16:42:46 -070076#define EMBEDDED_FW_SIGNATURE 0x55aa55aa
Marshall Dawson24f73d42019-04-01 10:48:43 -060077#define PSP_COOKIE 0x50535024 /* 'PSP$' */
78#define PSPL2_COOKIE 0x324c5024 /* '2LP$' */
79#define PSP2_COOKIE 0x50535032 /* 'PSP2' */
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -060080#define BDT1_COOKIE 0x44484224 /* 'DHB$ */
81#define BDT2_COOKIE 0x324c4224 /* '2LB$ */
Marshall Dawson239286c2019-02-23 16:42:46 -070082
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080083/*
Marshall Dawson0e02ce82019-03-04 16:50:37 -070084 * Beginning with Family 15h Models 70h-7F, a.k.a Stoney Ridge, the PSP
85 * can support an optional "combo" implementation. If the PSP sees the
86 * PSP2 cookie, it interprets the table as a roadmap to additional PSP
87 * tables. Using this, support for multiple product generations may be
88 * built into one image. If the PSP$ cookie is found, the table is a
89 * normal directory table.
90 *
91 * Modern generations supporting the combo directories require the
92 * pointer to be at offset 0x14 of the Embedded Firmware Structure,
93 * regardless of the type of directory used. The --combo-capable
94 * argument enforces this placement.
95 *
96 * TODO: Future work may require fully implementing the PSP_COMBO feature.
zbaoc3b0b722016-02-19 13:47:31 +080097 */
Marshall Dawson0e02ce82019-03-04 16:50:37 -070098#define PSP_COMBO 0
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080099
Idwer Volleringa682fc82019-12-16 15:33:12 +0100100#if defined(__GLIBC__)
Marshall Dawson239286c2019-02-23 16:42:46 -0700101typedef unsigned long long int uint64_t;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800102typedef unsigned int uint32_t;
103typedef unsigned char uint8_t;
104typedef unsigned short uint16_t;
Idwer Volleringa682fc82019-12-16 15:33:12 +0100105#endif
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800106
107/*
108 * Creates the OSI Fletcher checksum. See 8473-1, Appendix C, section C.3.
109 * The checksum field of the passed PDU does not need to be reset to zero.
110 *
111 * The "Fletcher Checksum" was proposed in a paper by John G. Fletcher of
112 * Lawrence Livermore Labs. The Fletcher Checksum was proposed as an
113 * alternative to cyclical redundancy checks because it provides error-
114 * detection properties similar to cyclical redundancy checks but at the
115 * cost of a simple summation technique. Its characteristics were first
116 * published in IEEE Transactions on Communications in January 1982. One
117 * version has been adopted by ISO for use in the class-4 transport layer
118 * of the network protocol.
119 *
120 * This program expects:
121 * stdin: The input file to compute a checksum for. The input file
122 * not be longer than 256 bytes.
123 * stdout: Copied from the input file with the Fletcher's Checksum
124 * inserted 8 bytes after the beginning of the file.
125 * stderr: Used to print out error messages.
126 */
Marshall Dawson8a45a4d2019-02-24 07:18:44 -0700127static uint32_t fletcher32(const void *data, int length)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800128{
129 uint32_t c0;
130 uint32_t c1;
131 uint32_t checksum;
132 int index;
Marshall Dawson8a45a4d2019-02-24 07:18:44 -0700133 const uint16_t *pptr = data;
134
135 length /= 2;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800136
137 c0 = 0xFFFF;
138 c1 = 0xFFFF;
139
Marshall Dawsonb85ddc52019-07-23 07:24:30 -0600140 while (length) {
141 index = length >= 359 ? 359 : length;
142 length -= index;
143 do {
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800144 c0 += *(pptr++);
145 c1 += c0;
Marshall Dawsonb85ddc52019-07-23 07:24:30 -0600146 } while (--index);
147 c0 = (c0 & 0xFFFF) + (c0 >> 16);
148 c1 = (c1 & 0xFFFF) + (c1 >> 16);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800149 }
150
Marshall Dawson8a45a4d2019-02-24 07:18:44 -0700151 /* Sums[0,1] mod 64K + overflow */
152 c0 = (c0 & 0xFFFF) + (c0 >> 16);
153 c1 = (c1 & 0xFFFF) + (c1 >> 16);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800154 checksum = (c1 << 16) | c0;
155
156 return checksum;
157}
158
Martin Roth8806f7f2016-11-08 10:44:18 -0700159static void usage(void)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800160{
Martin Roth0e940622016-11-08 10:37:53 -0700161 printf("amdfwtool: Create AMD Firmware combination\n");
162 printf("Usage: amdfwtool [options] -f <size> -o <filename>\n");
Marshall Dawsonf4b9b412017-03-17 16:30:51 -0600163 printf("-x | --xhci <FILE> Add XHCI blob\n");
164 printf("-i | --imc <FILE> Add IMC blob\n");
165 printf("-g | --gec <FILE> Add GEC blob\n");
Martin Roth0e940622016-11-08 10:37:53 -0700166
167 printf("\nPSP options:\n");
Marshall Dawson67d868d2019-02-28 11:43:40 -0700168 printf("-A | --combo-capable Place PSP directory pointer at Embedded Firmware\n");
169 printf(" offset able to support combo directory\n");
Marshall Dawson24f73d42019-04-01 10:48:43 -0600170 printf("-M | --multilevel Generate primary and secondary tables\n");
Marshall Dawsonf4b9b412017-03-17 16:30:51 -0600171 printf("-p | --pubkey <FILE> Add pubkey\n");
172 printf("-b | --bootloader <FILE> Add bootloader\n");
Marshall Dawsondbae6322019-03-04 10:31:03 -0700173 printf("-S | --subprogram <number> Sets subprogram field for the next firmware\n");
Marshall Dawsonf4b9b412017-03-17 16:30:51 -0600174 printf("-s | --smufirmware <FILE> Add smufirmware\n");
175 printf("-r | --recovery <FILE> Add recovery\n");
176 printf("-k | --rtmpubkey <FILE> Add rtmpubkey\n");
177 printf("-c | --secureos <FILE> Add secureos\n");
178 printf("-n | --nvram <FILE> Add nvram\n");
179 printf("-d | --securedebug <FILE> Add securedebug\n");
180 printf("-t | --trustlets <FILE> Add trustlets\n");
181 printf("-u | --trustletkey <FILE> Add trustletkey\n");
182 printf("-w | --smufirmware2 <FILE> Add smufirmware2\n");
183 printf("-m | --smuscs <FILE> Add smuscs\n");
Marshall Dawsonef79fcc2019-04-01 10:16:41 -0600184 printf("-T | --soft-fuse <HEX_VAL> Override default soft fuse values\n");
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600185 printf("-z | --abl-image <FILE> Add AGESA Binary\n");
186 printf("-J | --sec-gasket <FILE> Add security gasket\n");
187 printf("-B | --mp2-fw <FILE> Add MP2 firmware\n");
188 printf("-N | --secdebug <FILE> Add secure unlock image\n");
189 printf("-U | --token-unlock Reserve space for debug token\n");
190 printf("-K | --drv-entry-pts <FILE> Add PSP driver entry points\n");
191 printf("-L | --ikek <FILE> Add Wrapped iKEK\n");
192 printf("-Y | --s0i3drv <FILE> Add s0i3 driver\n");
Martin Rothd3ce8c82019-07-13 20:13:07 -0600193 printf("-Z | --verstage <FILE> Add verstage\n");
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600194 printf("\nBIOS options:\n");
195 printf("-I | --instance <number> Sets instance field for the next BIOS firmware\n");
196 printf("-a | --apcb <FILE> Add AGESA PSP customization block\n");
197 printf("-Q | --apob-base <HEX_VAL> Destination for AGESA PSP output block\n");
198 printf("-F | --apob-nv-base <HEX_VAL> Location of S3 resume data\n");
199 printf("-H | --apob-nv-size <HEX_VAL> Size of S3 resume data\n");
200 printf("-y | --pmu-inst <FILE> Add PMU firmware instruction portion\n");
201 printf("-G | --pmu-data <FILE> Add PMU firmware data portion\n");
Martin Rothec933132019-07-13 20:03:34 -0600202 printf("-O | --ucode <FILE> Add microcode patch\n");
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600203 printf("-X | --mp2-config <FILE> Add MP2 configuration\n");
204 printf("-V | --bios-bin <FILE> Add compressed image; auto source address\n");
205 printf("-e | --bios-bin-src <HEX_VAL> Address in flash of source if -V not used\n");
206 printf("-v | --bios-bin-dest <HEX_VAL> Destination for uncompressed BIOS\n");
207 printf("-j | --bios-uncomp-size <HEX> Uncompressed size of BIOS image\n");
Martin Roth0e940622016-11-08 10:37:53 -0700208 printf("\n-o | --output <filename> output filename\n");
Marshall Dawsonf4b9b412017-03-17 16:30:51 -0600209 printf("-f | --flashsize <HEX_VAL> ROM size in bytes\n");
210 printf(" size must be larger than %dKB\n",
Martin Roth0e940622016-11-08 10:37:53 -0700211 MIN_ROM_KB);
Marshall Dawsonf4b9b412017-03-17 16:30:51 -0600212 printf(" and must a multiple of 1024\n");
Martin Roth0d3b1182017-10-03 14:16:04 -0600213 printf("-l | --location Location of Directory\n");
Marshall Dawsonf4b9b412017-03-17 16:30:51 -0600214 printf("-h | --help show this help\n");
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800215}
216
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600217typedef enum _amd_bios_type {
218 AMD_BIOS_APCB = 0x60,
219 AMD_BIOS_APOB = 0x61,
220 AMD_BIOS_BIN = 0x62,
221 AMD_BIOS_APOB_NV = 0x63,
222 AMD_BIOS_PMUI = 0x64,
223 AMD_BIOS_PMUD = 0x65,
224 AMD_BIOS_UCODE = 0x66,
225 AMD_BIOS_APCB_BK = 0x68,
226 AMD_BIOS_MP2_CFG = 0x6a,
227 AMD_BIOS_L2_PTR = 0x70,
228 AMD_BIOS_INVALID,
229} amd_bios_type;
230
231#define BDT_LVL1 0x1
232#define BDT_LVL2 0x2
233#define BDT_BOTH (BDT_LVL1 | BDT_LVL2)
234typedef struct _amd_bios_entry {
235 amd_bios_type type;
236 int region_type;
237 int reset;
238 int copy;
239 int ro;
240 int zlib;
241 int inst;
242 int subpr;
243 uint64_t src;
244 uint64_t dest;
245 size_t size;
246 char *filename;
247 int level;
248} amd_bios_entry;
249
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800250typedef enum _amd_fw_type {
251 AMD_FW_PSP_PUBKEY = 0,
252 AMD_FW_PSP_BOOTLOADER = 1,
253 AMD_FW_PSP_SMU_FIRMWARE = 8,
254 AMD_FW_PSP_RECOVERY = 3,
255 AMD_FW_PSP_RTM_PUBKEY = 5,
256 AMD_FW_PSP_SECURED_OS = 2,
257 AMD_FW_PSP_NVRAM = 4,
258 AMD_FW_PSP_SECURED_DEBUG = 9,
259 AMD_FW_PSP_TRUSTLETS = 12,
260 AMD_FW_PSP_TRUSTLETKEY = 13,
261 AMD_FW_PSP_SMU_FIRMWARE2 = 18,
262 AMD_PSP_FUSE_CHAIN = 11,
263 AMD_FW_PSP_SMUSCS = 95,
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600264 AMD_DEBUG_UNLOCK = 0x13,
265 AMD_WRAPPED_IKEK = 0x21,
266 AMD_TOKEN_UNLOCK = 0x22,
267 AMD_SEC_GASKET = 0x24,
268 AMD_MP2_FW = 0x25,
269 AMD_DRIVER_ENTRIES = 0x28,
270 AMD_S0I3_DRIVER = 0x2d,
271 AMD_ABL0 = 0x30,
272 AMD_ABL1 = 0x31,
273 AMD_ABL2 = 0x32,
274 AMD_ABL3 = 0x33,
275 AMD_ABL4 = 0x34,
276 AMD_ABL5 = 0x35,
277 AMD_ABL6 = 0x36,
278 AMD_ABL7 = 0x37,
279 AMD_FW_PSP_WHITELIST = 0x3a,
Marshall Dawson24f73d42019-04-01 10:48:43 -0600280 AMD_FW_L2_PTR = 0x40,
Martin Rothd3ce8c82019-07-13 20:13:07 -0600281 AMD_FW_PSP_VERSTAGE = 0x52,
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800282 AMD_FW_IMC,
283 AMD_FW_GEC,
284 AMD_FW_XHCI,
zbaoc3a08a92016-03-02 14:47:27 +0800285 AMD_FW_INVALID,
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800286} amd_fw_type;
287
Marshall Dawson24f73d42019-04-01 10:48:43 -0600288#define PSP_LVL1 0x1
289#define PSP_LVL2 0x2
290#define PSP_BOTH (PSP_LVL1 | PSP_LVL2)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800291typedef struct _amd_fw_entry {
292 amd_fw_type type;
Marshall Dawsondbae6322019-03-04 10:31:03 -0700293 uint8_t subprog;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800294 char *filename;
Marshall Dawson24f73d42019-04-01 10:48:43 -0600295 int level;
Marshall Dawsonef79fcc2019-04-01 10:16:41 -0600296 uint64_t other;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800297} amd_fw_entry;
298
Martin Roth8806f7f2016-11-08 10:44:18 -0700299static amd_fw_entry amd_psp_fw_table[] = {
Marshall Dawson24f73d42019-04-01 10:48:43 -0600300 { .type = AMD_FW_PSP_PUBKEY, .level = PSP_BOTH },
301 { .type = AMD_FW_PSP_BOOTLOADER, .level = PSP_BOTH },
302 { .type = AMD_FW_PSP_SMU_FIRMWARE, .subprog = 0, .level = PSP_BOTH },
303 { .type = AMD_FW_PSP_RECOVERY, .level = PSP_LVL1 },
304 { .type = AMD_FW_PSP_RTM_PUBKEY, .level = PSP_BOTH },
305 { .type = AMD_FW_PSP_SECURED_OS, .level = PSP_LVL2 },
306 { .type = AMD_FW_PSP_NVRAM, .level = PSP_LVL2 },
307 { .type = AMD_FW_PSP_SMU_FIRMWARE, .subprog = 2, .level = PSP_BOTH },
308 { .type = AMD_FW_PSP_SECURED_DEBUG, .level = PSP_LVL2 },
309 { .type = AMD_FW_PSP_TRUSTLETS, .level = PSP_LVL2 },
310 { .type = AMD_FW_PSP_TRUSTLETKEY, .level = PSP_LVL2 },
311 { .type = AMD_FW_PSP_SMU_FIRMWARE2, .subprog = 2, .level = PSP_BOTH },
312 { .type = AMD_FW_PSP_SMU_FIRMWARE, .subprog = 1, .level = PSP_BOTH },
313 { .type = AMD_FW_PSP_SMU_FIRMWARE2, .subprog = 1, .level = PSP_BOTH },
314 { .type = AMD_FW_PSP_SMU_FIRMWARE2, .level = PSP_BOTH },
315 { .type = AMD_FW_PSP_SMUSCS, .level = PSP_BOTH },
316 { .type = AMD_PSP_FUSE_CHAIN, .level = PSP_LVL2 },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600317 { .type = AMD_DEBUG_UNLOCK, .level = PSP_LVL2 },
318 { .type = AMD_WRAPPED_IKEK, .level = PSP_BOTH },
319 { .type = AMD_TOKEN_UNLOCK, .level = PSP_BOTH },
320 { .type = AMD_SEC_GASKET, .subprog = 2, .level = PSP_BOTH },
321 { .type = AMD_SEC_GASKET, .subprog = 1, .level = PSP_BOTH },
322 { .type = AMD_MP2_FW, .subprog = 2, .level = PSP_LVL2 },
323 { .type = AMD_MP2_FW, .subprog = 1, .level = PSP_LVL2 },
324 { .type = AMD_DRIVER_ENTRIES, .level = PSP_LVL2 },
325 { .type = AMD_S0I3_DRIVER, .level = PSP_LVL2 },
326 { .type = AMD_ABL0, .level = PSP_BOTH },
327 { .type = AMD_ABL1, .level = PSP_BOTH },
328 { .type = AMD_ABL2, .level = PSP_BOTH },
329 { .type = AMD_ABL3, .level = PSP_BOTH },
330 { .type = AMD_ABL4, .level = PSP_BOTH },
331 { .type = AMD_ABL5, .level = PSP_BOTH },
332 { .type = AMD_ABL6, .level = PSP_BOTH },
333 { .type = AMD_ABL7, .level = PSP_BOTH },
Marshall Dawson24f73d42019-04-01 10:48:43 -0600334 { .type = AMD_FW_PSP_SMU_FIRMWARE, .subprog = 1, .level = PSP_BOTH },
335 { .type = AMD_FW_PSP_SMU_FIRMWARE2, .subprog = 1, .level = PSP_BOTH },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600336 { .type = AMD_FW_PSP_WHITELIST, .level = PSP_LVL2 },
Martin Rothd3ce8c82019-07-13 20:13:07 -0600337 { .type = AMD_FW_PSP_VERSTAGE, .level = PSP_BOTH },
zbaoc3a08a92016-03-02 14:47:27 +0800338 { .type = AMD_FW_INVALID },
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800339};
340
Martin Roth8806f7f2016-11-08 10:44:18 -0700341static amd_fw_entry amd_fw_table[] = {
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800342 { .type = AMD_FW_XHCI },
343 { .type = AMD_FW_IMC },
344 { .type = AMD_FW_GEC },
zbaoc3a08a92016-03-02 14:47:27 +0800345 { .type = AMD_FW_INVALID },
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800346};
347
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600348static amd_bios_entry amd_bios_table[] = {
Marshall Dawson0581bf62019-09-25 11:03:53 -0600349 { .type = AMD_BIOS_APCB, .inst = 0, .level = BDT_BOTH },
350 { .type = AMD_BIOS_APCB, .inst = 1, .level = BDT_BOTH },
351 { .type = AMD_BIOS_APCB, .inst = 2, .level = BDT_BOTH },
352 { .type = AMD_BIOS_APCB, .inst = 3, .level = BDT_BOTH },
353 { .type = AMD_BIOS_APCB, .inst = 4, .level = BDT_BOTH },
Rob Barnes18fd26c2020-03-03 10:35:02 -0700354 { .type = AMD_BIOS_APCB, .inst = 5, .level = BDT_BOTH },
355 { .type = AMD_BIOS_APCB, .inst = 6, .level = BDT_BOTH },
356 { .type = AMD_BIOS_APCB, .inst = 7, .level = BDT_BOTH },
357 { .type = AMD_BIOS_APCB, .inst = 8, .level = BDT_BOTH },
358 { .type = AMD_BIOS_APCB, .inst = 9, .level = BDT_BOTH },
359 { .type = AMD_BIOS_APCB, .inst = 10, .level = BDT_BOTH },
360 { .type = AMD_BIOS_APCB, .inst = 11, .level = BDT_BOTH },
361 { .type = AMD_BIOS_APCB, .inst = 12, .level = BDT_BOTH },
362 { .type = AMD_BIOS_APCB, .inst = 13, .level = BDT_BOTH },
363 { .type = AMD_BIOS_APCB, .inst = 14, .level = BDT_BOTH },
364 { .type = AMD_BIOS_APCB, .inst = 15, .level = BDT_BOTH },
Marshall Dawson2dd3b5c2020-01-03 17:57:48 -0700365 { .type = AMD_BIOS_APCB_BK, .inst = 0, .level = BDT_BOTH },
366 { .type = AMD_BIOS_APCB_BK, .inst = 1, .level = BDT_BOTH },
367 { .type = AMD_BIOS_APCB_BK, .inst = 2, .level = BDT_BOTH },
368 { .type = AMD_BIOS_APCB_BK, .inst = 3, .level = BDT_BOTH },
369 { .type = AMD_BIOS_APCB_BK, .inst = 4, .level = BDT_BOTH },
Rob Barnes18fd26c2020-03-03 10:35:02 -0700370 { .type = AMD_BIOS_APCB_BK, .inst = 5, .level = BDT_BOTH },
371 { .type = AMD_BIOS_APCB_BK, .inst = 6, .level = BDT_BOTH },
372 { .type = AMD_BIOS_APCB_BK, .inst = 7, .level = BDT_BOTH },
373 { .type = AMD_BIOS_APCB_BK, .inst = 8, .level = BDT_BOTH },
374 { .type = AMD_BIOS_APCB_BK, .inst = 9, .level = BDT_BOTH },
375 { .type = AMD_BIOS_APCB_BK, .inst = 10, .level = BDT_BOTH },
376 { .type = AMD_BIOS_APCB_BK, .inst = 11, .level = BDT_BOTH },
377 { .type = AMD_BIOS_APCB_BK, .inst = 12, .level = BDT_BOTH },
378 { .type = AMD_BIOS_APCB_BK, .inst = 13, .level = BDT_BOTH },
379 { .type = AMD_BIOS_APCB_BK, .inst = 14, .level = BDT_BOTH },
380 { .type = AMD_BIOS_APCB_BK, .inst = 15, .level = BDT_BOTH },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600381 { .type = AMD_BIOS_APOB, .level = BDT_BOTH },
382 { .type = AMD_BIOS_BIN,
383 .reset = 1, .copy = 1, .zlib = 1, .level = BDT_BOTH },
384 { .type = AMD_BIOS_APOB_NV, .level = BDT_LVL2 },
385 { .type = AMD_BIOS_PMUI, .inst = 1, .subpr = 0, .level = BDT_BOTH },
386 { .type = AMD_BIOS_PMUD, .inst = 1, .subpr = 0, .level = BDT_BOTH },
387 { .type = AMD_BIOS_PMUI, .inst = 4, .subpr = 0, .level = BDT_BOTH },
388 { .type = AMD_BIOS_PMUD, .inst = 4, .subpr = 0, .level = BDT_BOTH },
389 { .type = AMD_BIOS_PMUI, .inst = 1, .subpr = 1, .level = BDT_BOTH },
390 { .type = AMD_BIOS_PMUD, .inst = 1, .subpr = 1, .level = BDT_BOTH },
391 { .type = AMD_BIOS_PMUI, .inst = 4, .subpr = 1, .level = BDT_BOTH },
392 { .type = AMD_BIOS_PMUD, .inst = 4, .subpr = 1, .level = BDT_BOTH },
393 { .type = AMD_BIOS_UCODE, .inst = 0, .level = BDT_LVL2 },
394 { .type = AMD_BIOS_UCODE, .inst = 1, .level = BDT_LVL2 },
395 { .type = AMD_BIOS_UCODE, .inst = 2, .level = BDT_LVL2 },
396 { .type = AMD_BIOS_MP2_CFG, .level = BDT_LVL2 },
397 { .type = AMD_BIOS_INVALID },
398};
399
Marshall Dawson239286c2019-02-23 16:42:46 -0700400typedef struct _embedded_firmware {
401 uint32_t signature; /* 0x55aa55aa */
402 uint32_t imc_entry;
403 uint32_t gec_entry;
404 uint32_t xhci_entry;
405 uint32_t psp_entry;
406 uint32_t comboable;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600407 uint32_t bios0_entry; /* todo: add way to select correct entry */
408 uint32_t bios1_entry;
Marshall Dawson94f24922019-09-28 08:49:09 -0600409 uint32_t bios2_entry;
410 uint32_t reserved[0x2c]; /* 0x24 - 0x4f */
Marshall Dawson239286c2019-02-23 16:42:46 -0700411} __attribute__((packed, aligned(16))) embedded_firmware;
412
413typedef struct _psp_directory_header {
414 uint32_t cookie;
415 uint32_t checksum;
416 uint32_t num_entries;
417 uint32_t reserved;
418} __attribute__((packed, aligned(16))) psp_directory_header;
419
420typedef struct _psp_directory_entry {
Marshall Dawsondbae6322019-03-04 10:31:03 -0700421 uint8_t type;
422 uint8_t subprog;
423 uint16_t rsvd;
Marshall Dawson239286c2019-02-23 16:42:46 -0700424 uint32_t size;
425 uint64_t addr; /* or a value in some cases */
426} __attribute__((packed)) psp_directory_entry;
427
428typedef struct _psp_directory_table {
429 psp_directory_header header;
430 psp_directory_entry entries[];
431} __attribute__((packed)) psp_directory_table;
432
Marshall Dawson2794a862019-03-04 16:53:15 -0700433#define MAX_PSP_ENTRIES 0x1f
434
Marshall Dawson239286c2019-02-23 16:42:46 -0700435typedef struct _psp_combo_header {
436 uint32_t cookie;
437 uint32_t checksum;
438 uint32_t num_entries;
439 uint32_t lookup;
440 uint64_t reserved[2];
441} __attribute__((packed, aligned(16))) psp_combo_header;
442
443typedef struct _psp_combo_entry {
444 uint32_t id_sel;
445 uint32_t id;
446 uint64_t lvl2_addr;
447} __attribute__((packed)) psp_combo_entry;
448
449typedef struct _psp_combo_directory {
450 psp_combo_header header;
451 psp_combo_entry entries[];
452} __attribute__((packed)) psp_combo_directory;
453
Marshall Dawson2794a862019-03-04 16:53:15 -0700454#define MAX_COMBO_ENTRIES 1
455
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600456typedef struct _bios_directory_hdr {
457 uint32_t cookie;
458 uint32_t checksum;
459 uint32_t num_entries;
460 uint32_t reserved;
461} __attribute__((packed, aligned(16))) bios_directory_hdr;
462
463typedef struct _bios_directory_entry {
464 uint8_t type;
465 uint8_t region_type;
466 int reset:1;
467 int copy:1;
468 int ro:1;
469 int compressed:1;
470 int inst:4;
471 uint8_t subprog; /* b[7:3] reserved */
472 uint32_t size;
473 uint64_t source;
474 uint64_t dest;
475} __attribute__((packed)) bios_directory_entry;
476
477typedef struct _bios_directory_table {
478 bios_directory_hdr header;
479 bios_directory_entry entries[];
480} bios_directory_table;
481
Rob Barnes18fd26c2020-03-03 10:35:02 -0700482#define MAX_BIOS_ENTRIES 0x22
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600483
Marshall Dawson2794a862019-03-04 16:53:15 -0700484typedef struct _context {
485 char *rom; /* target buffer, size of flash device */
486 uint32_t rom_size; /* size of flash device */
487 uint32_t current; /* pointer within flash & proxy buffer */
488} context;
489
490#define RUN_BASE(ctx) (0xFFFFFFFF - (ctx).rom_size + 1)
491#define RUN_OFFSET(ctx, offset) (RUN_BASE(ctx) + (offset))
492#define RUN_CURRENT(ctx) RUN_OFFSET((ctx), (ctx).current)
493#define BUFF_OFFSET(ctx, offset) ((void *)((ctx).rom + (offset)))
494#define BUFF_CURRENT(ctx) BUFF_OFFSET((ctx), (ctx).current)
495#define BUFF_TO_RUN(ctx, ptr) RUN_OFFSET((ctx), ((char *)(ptr) - (ctx).rom))
496#define BUFF_ROOM(ctx) ((ctx).rom_size - (ctx).current)
497
Marshall Dawson24f73d42019-04-01 10:48:43 -0600498static void *new_psp_dir(context *ctx, int multi)
Marshall Dawson2794a862019-03-04 16:53:15 -0700499{
500 void *ptr;
501
Marshall Dawson24f73d42019-04-01 10:48:43 -0600502 /*
503 * Force both onto boundary when multi. Primary table is after
504 * updatable table, so alignment ensures primary can stay intact
505 * if secondary is reprogrammed.
506 */
507 if (multi)
508 ctx->current = ALIGN(ctx->current, TABLE_ERASE_ALIGNMENT);
509 else
510 ctx->current = ALIGN(ctx->current, TABLE_ALIGNMENT);
511
Marshall Dawson2794a862019-03-04 16:53:15 -0700512 ptr = BUFF_CURRENT(*ctx);
513 ctx->current += sizeof(psp_directory_header)
514 + MAX_PSP_ENTRIES * sizeof(psp_directory_entry);
515 return ptr;
516}
517
Martin Rothec933132019-07-13 20:03:34 -0600518#if PSP_COMBO
Marshall Dawson2794a862019-03-04 16:53:15 -0700519static void *new_combo_dir(context *ctx)
520{
521 void *ptr;
522
523 ctx->current = ALIGN(ctx->current, TABLE_ALIGNMENT);
524 ptr = BUFF_CURRENT(*ctx);
525 ctx->current += sizeof(psp_combo_header)
526 + MAX_COMBO_ENTRIES * sizeof(psp_combo_entry);
527 return ptr;
528}
Martin Rothec933132019-07-13 20:03:34 -0600529#endif
Marshall Dawson2794a862019-03-04 16:53:15 -0700530
Marshall Dawsona378c222019-03-04 16:52:07 -0700531static void fill_dir_header(void *directory, uint32_t count, uint32_t cookie)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800532{
Marshall Dawson24f73d42019-04-01 10:48:43 -0600533 psp_combo_directory *cdir = directory;
534 psp_directory_table *dir = directory;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600535 bios_directory_table *bdir = directory;
Marshall Dawson24f73d42019-04-01 10:48:43 -0600536
537 if (!count)
538 return;
539
540 switch (cookie) {
541 case PSP2_COOKIE:
Marshall Dawsona378c222019-03-04 16:52:07 -0700542 /* caller is responsible for lookup mode */
Marshall Dawsona378c222019-03-04 16:52:07 -0700543 cdir->header.cookie = cookie;
544 cdir->header.num_entries = count;
545 cdir->header.reserved[0] = 0;
546 cdir->header.reserved[1] = 0;
547 /* checksum everything that comes after the Checksum field */
548 cdir->header.checksum = fletcher32(&cdir->header.num_entries,
549 count * sizeof(psp_combo_entry)
550 + sizeof(cdir->header.num_entries)
551 + sizeof(cdir->header.lookup)
552 + 2 * sizeof(cdir->header.reserved[0]));
Marshall Dawson24f73d42019-04-01 10:48:43 -0600553 break;
554 case PSP_COOKIE:
555 case PSPL2_COOKIE:
Marshall Dawsona378c222019-03-04 16:52:07 -0700556 dir->header.cookie = cookie;
557 dir->header.num_entries = count;
558 dir->header.reserved = 0;
559 /* checksum everything that comes after the Checksum field */
560 dir->header.checksum = fletcher32(&dir->header.num_entries,
Marshall Dawson8a45a4d2019-02-24 07:18:44 -0700561 count * sizeof(psp_directory_entry)
Marshall Dawsona378c222019-03-04 16:52:07 -0700562 + sizeof(dir->header.num_entries)
563 + sizeof(dir->header.reserved));
Marshall Dawson24f73d42019-04-01 10:48:43 -0600564 break;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600565 case BDT1_COOKIE:
566 case BDT2_COOKIE:
567 bdir->header.cookie = cookie;
568 bdir->header.num_entries = count;
569 bdir->header.reserved = 0;
570 /* checksum everything that comes after the Checksum field */
571 bdir->header.checksum = fletcher32(&bdir->header.num_entries,
572 count * sizeof(bios_directory_entry)
573 + sizeof(bdir->header.num_entries)
574 + sizeof(bdir->header.reserved));
575 break;
Marshall Dawsona378c222019-03-04 16:52:07 -0700576 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800577}
578
Marshall Dawson8e0dca02019-02-27 18:40:49 -0700579static ssize_t copy_blob(void *dest, const char *src_file, size_t room)
580{
581 int fd;
582 struct stat fd_stat;
583 ssize_t bytes;
584
585 fd = open(src_file, O_RDONLY);
586 if (fd < 0) {
Eric Peersaf505672020-03-05 16:04:15 -0700587 printf("Error opening file: %s: %s\n",
588 src_file, strerror(errno));
Marshall Dawson8e0dca02019-02-27 18:40:49 -0700589 return -1;
590 }
591
592 if (fstat(fd, &fd_stat)) {
593 printf("fstat error: %s\n", strerror(errno));
Jacob Garber967f8622019-07-02 10:35:10 -0600594 close(fd);
Marshall Dawson8e0dca02019-02-27 18:40:49 -0700595 return -2;
596 }
597
598 if (fd_stat.st_size > room) {
599 printf("Error: %s will not fit. Exiting.\n", src_file);
Jacob Garber967f8622019-07-02 10:35:10 -0600600 close(fd);
Marshall Dawson8e0dca02019-02-27 18:40:49 -0700601 return -3;
602 }
603
604 bytes = read(fd, dest, (size_t)fd_stat.st_size);
605 close(fd);
606 if (bytes != (ssize_t)fd_stat.st_size) {
607 printf("Error while reading %s\n", src_file);
608 return -4;
609 }
610
611 return bytes;
612}
613
Marshall Dawson2794a862019-03-04 16:53:15 -0700614static void integrate_firmwares(context *ctx,
Marshall Dawson239286c2019-02-23 16:42:46 -0700615 embedded_firmware *romsig,
Marshall Dawson2794a862019-03-04 16:53:15 -0700616 amd_fw_entry *fw_table)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800617{
Richard Spiegel137484d2018-01-17 10:23:19 -0700618 ssize_t bytes;
zbaoc3a08a92016-03-02 14:47:27 +0800619 int i;
Marshall Dawson2794a862019-03-04 16:53:15 -0700620
621 ctx->current += sizeof(embedded_firmware);
622 ctx->current = ALIGN(ctx->current, BLOB_ALIGNMENT);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800623
Martin Rothcd15bc82016-11-08 11:34:02 -0700624 for (i = 0; fw_table[i].type != AMD_FW_INVALID; i++) {
zbaoc3a08a92016-03-02 14:47:27 +0800625 if (fw_table[i].filename != NULL) {
zbaoc3a08a92016-03-02 14:47:27 +0800626 switch (fw_table[i].type) {
627 case AMD_FW_IMC:
Marshall Dawson2794a862019-03-04 16:53:15 -0700628 ctx->current = ALIGN(ctx->current, 0x10000U);
629 romsig->imc_entry = RUN_CURRENT(*ctx);
zbaoc3a08a92016-03-02 14:47:27 +0800630 break;
631 case AMD_FW_GEC:
Marshall Dawson2794a862019-03-04 16:53:15 -0700632 romsig->gec_entry = RUN_CURRENT(*ctx);
zbaoc3a08a92016-03-02 14:47:27 +0800633 break;
634 case AMD_FW_XHCI:
Marshall Dawson2794a862019-03-04 16:53:15 -0700635 romsig->xhci_entry = RUN_CURRENT(*ctx);
zbaoc3a08a92016-03-02 14:47:27 +0800636 break;
637 default:
638 /* Error */
639 break;
640 }
641
Marshall Dawson2794a862019-03-04 16:53:15 -0700642 bytes = copy_blob(BUFF_CURRENT(*ctx),
643 fw_table[i].filename, BUFF_ROOM(*ctx));
Marshall Dawson02bd7732019-03-13 14:43:17 -0600644 if (bytes < 0) {
Marshall Dawson2794a862019-03-04 16:53:15 -0700645 free(ctx->rom);
Martin Roth60f15512016-11-08 09:55:01 -0700646 exit(1);
647 }
648
Marshall Dawson2794a862019-03-04 16:53:15 -0700649 ctx->current = ALIGN(ctx->current + bytes,
650 BLOB_ALIGNMENT);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800651 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800652 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800653}
654
Marshall Dawson2794a862019-03-04 16:53:15 -0700655static void integrate_psp_firmwares(context *ctx,
Marshall Dawson239286c2019-02-23 16:42:46 -0700656 psp_directory_table *pspdir,
Marshall Dawson24f73d42019-04-01 10:48:43 -0600657 psp_directory_table *pspdir2,
658 amd_fw_entry *fw_table,
659 uint32_t cookie)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800660{
Richard Spiegel137484d2018-01-17 10:23:19 -0700661 ssize_t bytes;
Marshall Dawsonc38c0c92019-02-23 16:41:35 -0700662 unsigned int i, count;
Marshall Dawson24f73d42019-04-01 10:48:43 -0600663 int level;
664
665 /* This function can create a primary table, a secondary table, or a
666 * flattened table which contains all applicable types. These if-else
667 * statements infer what the caller intended. If a 2nd-level cookie
668 * is passed, clearly a 2nd-level table is intended. However, a
669 * 1st-level cookie may indicate level 1 or flattened. If the caller
670 * passes a pointer to a 2nd-level table, then assume not flat.
671 */
672 if (cookie == PSPL2_COOKIE)
673 level = PSP_LVL2;
674 else if (pspdir2)
675 level = PSP_LVL1;
676 else
677 level = PSP_BOTH;
Marshall Dawson2794a862019-03-04 16:53:15 -0700678
679 ctx->current = ALIGN(ctx->current, BLOB_ALIGNMENT);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800680
Marshall Dawsonc38c0c92019-02-23 16:41:35 -0700681 for (i = 0, count = 0; fw_table[i].type != AMD_FW_INVALID; i++) {
Marshall Dawson24f73d42019-04-01 10:48:43 -0600682 if (!(fw_table[i].level & level))
683 continue;
684
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600685 if (fw_table[i].type == AMD_TOKEN_UNLOCK) {
686 if (!fw_table[i].other)
687 continue;
688 ctx->current = ALIGN(ctx->current, ERASE_ALIGNMENT);
689 pspdir->entries[count].type = fw_table[i].type;
690 pspdir->entries[count].size = 4096; /* TODO: doc? */
691 pspdir->entries[count].addr = RUN_CURRENT(*ctx);
692 pspdir->entries[count].subprog = fw_table[i].subprog;
693 pspdir->entries[count].rsvd = 0;
694 ctx->current = ALIGN(ctx->current + 4096, 0x100U);
695 count++;
696 } else if (fw_table[i].type == AMD_PSP_FUSE_CHAIN) {
Marshall Dawson239286c2019-02-23 16:42:46 -0700697 pspdir->entries[count].type = fw_table[i].type;
Marshall Dawsondbae6322019-03-04 10:31:03 -0700698 pspdir->entries[count].subprog = fw_table[i].subprog;
699 pspdir->entries[count].rsvd = 0;
Marshall Dawson239286c2019-02-23 16:42:46 -0700700 pspdir->entries[count].size = 0xFFFFFFFF;
Marshall Dawsonef79fcc2019-04-01 10:16:41 -0600701 pspdir->entries[count].addr = fw_table[i].other;
Marshall Dawsonc38c0c92019-02-23 16:41:35 -0700702 count++;
Marshall Dawson7c1e1422019-04-11 09:44:43 -0600703 } else if (fw_table[i].type == AMD_FW_PSP_NVRAM) {
704 if (fw_table[i].filename == NULL)
705 continue;
706 /* TODO: Add a way to reserve for NVRAM without
707 * requiring a filename. This isn't a feature used
708 * by coreboot systems, so priority is very low.
709 */
710 ctx->current = ALIGN(ctx->current, ERASE_ALIGNMENT);
711 bytes = copy_blob(BUFF_CURRENT(*ctx),
712 fw_table[i].filename, BUFF_ROOM(*ctx));
713 if (bytes <= 0) {
714 free(ctx->rom);
715 exit(1);
716 }
717
718 pspdir->entries[count].type = fw_table[i].type;
719 pspdir->entries[count].subprog = fw_table[i].subprog;
720 pspdir->entries[count].rsvd = 0;
721 pspdir->entries[count].size = ALIGN(bytes,
722 ERASE_ALIGNMENT);
723 pspdir->entries[count].addr = RUN_CURRENT(*ctx);
724
725 ctx->current = ALIGN(ctx->current + bytes,
726 BLOB_ERASE_ALIGNMENT);
727 count++;
zbaoc3a08a92016-03-02 14:47:27 +0800728 } else if (fw_table[i].filename != NULL) {
Marshall Dawson2794a862019-03-04 16:53:15 -0700729 bytes = copy_blob(BUFF_CURRENT(*ctx),
730 fw_table[i].filename, BUFF_ROOM(*ctx));
Marshall Dawson02bd7732019-03-13 14:43:17 -0600731 if (bytes < 0) {
Marshall Dawson2794a862019-03-04 16:53:15 -0700732 free(ctx->rom);
Marshall Dawson8e0dca02019-02-27 18:40:49 -0700733 exit(1);
734 }
735
Marshall Dawson239286c2019-02-23 16:42:46 -0700736 pspdir->entries[count].type = fw_table[i].type;
Marshall Dawsondbae6322019-03-04 10:31:03 -0700737 pspdir->entries[count].subprog = fw_table[i].subprog;
738 pspdir->entries[count].rsvd = 0;
Marshall Dawson8e0dca02019-02-27 18:40:49 -0700739 pspdir->entries[count].size = (uint32_t)bytes;
Marshall Dawson2794a862019-03-04 16:53:15 -0700740 pspdir->entries[count].addr = RUN_CURRENT(*ctx);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800741
Marshall Dawson2794a862019-03-04 16:53:15 -0700742 ctx->current = ALIGN(ctx->current + bytes,
743 BLOB_ALIGNMENT);
Marshall Dawsonc38c0c92019-02-23 16:41:35 -0700744 count++;
zbaoc3a08a92016-03-02 14:47:27 +0800745 } else {
746 /* This APU doesn't have this firmware. */
747 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800748 }
Marshall Dawson2794a862019-03-04 16:53:15 -0700749
Marshall Dawson24f73d42019-04-01 10:48:43 -0600750 if (pspdir2) {
751 pspdir->entries[count].type = AMD_FW_L2_PTR;
752 pspdir->entries[count].subprog = 0;
753 pspdir->entries[count].rsvd = 0;
754 pspdir->entries[count].size = sizeof(pspdir2->header)
755 + pspdir2->header.num_entries
756 * sizeof(psp_directory_entry);
757
758 pspdir->entries[count].addr = BUFF_TO_RUN(*ctx, pspdir2);
759 count++;
760 }
761
Marshall Dawson2794a862019-03-04 16:53:15 -0700762 if (count > MAX_PSP_ENTRIES) {
763 printf("Error: PSP entries exceed max allowed items\n");
764 free(ctx->rom);
765 exit(1);
766 }
767
Marshall Dawson24f73d42019-04-01 10:48:43 -0600768 fill_dir_header(pspdir, count, cookie);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800769}
770
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600771static void *new_bios_dir(context *ctx, int multi)
772{
773 void *ptr;
774
775 /*
776 * Force both onto boundary when multi. Primary table is after
777 * updatable table, so alignment ensures primary can stay intact
778 * if secondary is reprogrammed.
779 */
780 if (multi)
781 ctx->current = ALIGN(ctx->current, TABLE_ERASE_ALIGNMENT);
782 else
783 ctx->current = ALIGN(ctx->current, TABLE_ALIGNMENT);
784 ptr = BUFF_CURRENT(*ctx);
785 ctx->current += sizeof(bios_directory_hdr)
786 + MAX_BIOS_ENTRIES * sizeof(bios_directory_entry);
787 return ptr;
788}
789
790static int locate_bdt2_bios(bios_directory_table *level2,
791 uint64_t *source, uint32_t *size)
792{
793 int i;
794
795 *source = 0;
796 *size = 0;
797 if (!level2)
798 return 0;
799
800 for (i = 0 ; i < level2->header.num_entries ; i++) {
801 if (level2->entries[i].type == AMD_BIOS_BIN) {
802 *source = level2->entries[i].source;
803 *size = level2->entries[i].size;
804 return 1;
805 }
806 }
807 return 0;
808}
809
810static int have_bios_tables(amd_bios_entry *table)
811{
812 int i;
813
814 for (i = 0 ; table[i].type != AMD_BIOS_INVALID; i++) {
815 if (table[i].level & BDT_LVL1 && table[i].filename)
816 return 1;
817 }
818 return 0;
819}
820
Marshall Dawsonc4a8c482020-01-21 17:17:59 -0700821static int find_bios_entry(amd_bios_type type)
822{
823 int i;
824
825 for (i = 0; amd_bios_table[i].type != AMD_BIOS_INVALID; i++) {
826 if (amd_bios_table[i].type == type)
827 return i;
828 }
829 return -1;
830}
831
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600832static void integrate_bios_firmwares(context *ctx,
833 bios_directory_table *biosdir,
834 bios_directory_table *biosdir2,
835 amd_bios_entry *fw_table,
836 uint32_t cookie)
837{
838 ssize_t bytes;
Martin Rothec933132019-07-13 20:03:34 -0600839 unsigned int i, count;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600840 int level;
Marshall Dawsonc4a8c482020-01-21 17:17:59 -0700841 int apob_idx;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600842
843 /* This function can create a primary table, a secondary table, or a
844 * flattened table which contains all applicable types. These if-else
845 * statements infer what the caller intended. If a 2nd-level cookie
846 * is passed, clearly a 2nd-level table is intended. However, a
847 * 1st-level cookie may indicate level 1 or flattened. If the caller
848 * passes a pointer to a 2nd-level table, then assume not flat.
849 */
850 if (cookie == BDT2_COOKIE)
851 level = BDT_LVL2;
852 else if (biosdir2)
853 level = BDT_LVL1;
854 else
855 level = BDT_BOTH;
856
857 ctx->current = ALIGN(ctx->current, BLOB_ALIGNMENT);
858
859 for (i = 0, count = 0; fw_table[i].type != AMD_BIOS_INVALID; i++) {
860 if (!(fw_table[i].level & level))
861 continue;
862 if (fw_table[i].filename == NULL && (
863 fw_table[i].type != AMD_BIOS_APOB &&
864 fw_table[i].type != AMD_BIOS_APOB_NV &&
865 fw_table[i].type != AMD_BIOS_L2_PTR &&
866 fw_table[i].type != AMD_BIOS_BIN))
867 continue;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600868
869 /* BIOS Directory items may have additional requirements */
870
871 /* APOB_NV must have a size if it has a source */
872 if (fw_table[i].type == AMD_BIOS_APOB_NV && fw_table[i].src) {
873 if (!fw_table[i].size) {
874 printf("Error: APOB NV address provided, but no size\n");
875 free(ctx->rom);
876 exit(1);
877 }
878 }
Marshall Dawsonc4a8c482020-01-21 17:17:59 -0700879 /* APOB_NV needs a size, else no choice but to skip the item */
880 if (fw_table[i].type == AMD_BIOS_APOB_NV && !fw_table[i].size) {
881 /* Attempt to determine whether this is an error */
882 apob_idx = find_bios_entry(AMD_BIOS_APOB);
883 if (apob_idx < 0 || !fw_table[apob_idx].dest) {
884 /* APOV NV not expected to be used */
885 continue;
886 } else {
887 printf("Error: APOB NV must have a size\n");
888 free(ctx->rom);
889 exit(1);
890 }
891 }
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600892
893 /* APOB_DATA needs destination */
894 if (fw_table[i].type == AMD_BIOS_APOB && !fw_table[i].dest) {
895 printf("Error: APOB destination not provided\n");
896 free(ctx->rom);
897 exit(1);
898 }
899
900 /* BIOS binary must have destination and uncompressed size. If
901 * no filename given, then user must provide a source address.
902 */
903 if (fw_table[i].type == AMD_BIOS_BIN) {
904 if (!fw_table[i].dest || !fw_table[i].size) {
905 printf("Error: BIOS binary destination and uncompressed size are required\n");
906 free(ctx->rom);
907 exit(1);
908 }
909 if (!fw_table[i].filename && !fw_table[i].src) {
910 printf("Error: BIOS binary assumed outside amdfw.rom but no source address given\n");
911 free(ctx->rom);
912 exit(1);
913 }
914 }
915
916 biosdir->entries[count].type = fw_table[i].type;
917 biosdir->entries[count].region_type = fw_table[i].region_type;
918 biosdir->entries[count].dest = fw_table[i].dest ?
919 fw_table[i].dest : (uint64_t)-1;
920 biosdir->entries[count].reset = fw_table[i].reset;
921 biosdir->entries[count].copy = fw_table[i].copy;
922 biosdir->entries[count].ro = fw_table[i].ro;
923 biosdir->entries[count].compressed = fw_table[i].zlib;
924 biosdir->entries[count].inst = fw_table[i].inst;
925 biosdir->entries[count].subprog = fw_table[i].subpr;
926
927 switch (fw_table[i].type) {
928 case AMD_BIOS_APOB:
929 biosdir->entries[count].size = fw_table[i].size;
930 biosdir->entries[count].source = fw_table[i].src;
931 break;
932 case AMD_BIOS_APOB_NV:
933 if (fw_table[i].src) {
934 /* If source is given, use that and its size */
935 biosdir->entries[count].source = fw_table[i].src;
936 biosdir->entries[count].size = fw_table[i].size;
937 } else {
938 /* Else reserve size bytes within amdfw.rom */
939 ctx->current = ALIGN(ctx->current, ERASE_ALIGNMENT);
940 biosdir->entries[count].source = RUN_CURRENT(*ctx);
941 biosdir->entries[count].size = ALIGN(
942 fw_table[i].size, ERASE_ALIGNMENT);
943 memset(BUFF_CURRENT(*ctx), 0xff,
944 biosdir->entries[count].size);
945 ctx->current = ctx->current
946 + biosdir->entries[count].size;
947 }
948 break;
949 case AMD_BIOS_BIN:
950 /* Don't make a 2nd copy, point to the same one */
951 if (level == BDT_LVL1 && locate_bdt2_bios(biosdir2,
952 &biosdir->entries[count].source,
953 &biosdir->entries[count].size))
954 break;
955
956 /* level 2, or level 1 and no copy found in level 2 */
957 biosdir->entries[count].source = fw_table[i].src;
958 biosdir->entries[count].dest = fw_table[i].dest;
959 biosdir->entries[count].size = fw_table[i].size;
960
961 if (!fw_table[i].filename)
962 break;
963
964 bytes = copy_blob(BUFF_CURRENT(*ctx),
965 fw_table[i].filename, BUFF_ROOM(*ctx));
966 if (bytes <= 0) {
967 free(ctx->rom);
968 exit(1);
969 }
970
971 biosdir->entries[count].source = RUN_CURRENT(*ctx);
972
973 ctx->current = ALIGN(ctx->current + bytes, 0x100U);
974 break;
975 default: /* everything else is copied from input */
976 if (fw_table[i].type == AMD_BIOS_APCB ||
977 fw_table[i].type == AMD_BIOS_APCB_BK)
978 ctx->current = ALIGN(
979 ctx->current, ERASE_ALIGNMENT);
980
981 bytes = copy_blob(BUFF_CURRENT(*ctx),
982 fw_table[i].filename, BUFF_ROOM(*ctx));
983 if (bytes <= 0) {
984 free(ctx->rom);
985 exit(1);
986 }
987
988 biosdir->entries[count].size = (uint32_t)bytes;
989 biosdir->entries[count].source = RUN_CURRENT(*ctx);
990
991 ctx->current = ALIGN(ctx->current + bytes, 0x100U);
992 break;
993 }
994
995 count++;
996 }
997
998 if (biosdir2) {
999 biosdir->entries[count].type = AMD_BIOS_L2_PTR;
1000 biosdir->entries[count].size =
1001 + MAX_BIOS_ENTRIES
1002 * sizeof(bios_directory_entry);
1003 biosdir->entries[count].source =
1004 BUFF_TO_RUN(*ctx, biosdir2);
1005 biosdir->entries[count].subprog = 0;
1006 biosdir->entries[count].inst = 0;
1007 biosdir->entries[count].copy = 0;
1008 biosdir->entries[count].compressed = 0;
1009 biosdir->entries[count].dest = -1;
1010 biosdir->entries[count].reset = 0;
1011 biosdir->entries[count].ro = 0;
1012 count++;
1013 }
1014
1015 if (count > MAX_BIOS_ENTRIES) {
Rob Barnes18fd26c2020-03-03 10:35:02 -07001016 printf("Error: BIOS entries (%d) exceeds max allowed items "
1017 "(%d)\n", count, MAX_BIOS_ENTRIES);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001018 free(ctx->rom);
1019 exit(1);
1020 }
1021
1022 fill_dir_header(biosdir, count, cookie);
1023}
Martin Rothd3ce8c82019-07-13 20:13:07 -06001024// Unused values: CDEPqR
1025static const char *optstring = "x:i:g:AMS:p:b:s:r:k:c:n:d:t:u:w:m:T:z:J:B:K:L:Y:N:UW:I:a:Q:V:e:v:j:y:G:O:X:F:H:o:f:l:hZ:";
Marc Jones90099b62016-09-20 21:05:45 -06001026
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001027static struct option long_options[] = {
Marshall Dawsonf4b9b412017-03-17 16:30:51 -06001028 {"xhci", required_argument, 0, 'x' },
1029 {"imc", required_argument, 0, 'i' },
1030 {"gec", required_argument, 0, 'g' },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001031 /* PSP Directory Table items */
Marshall Dawson67d868d2019-02-28 11:43:40 -07001032 {"combo-capable", no_argument, 0, 'A' },
Marshall Dawson24f73d42019-04-01 10:48:43 -06001033 {"multilevel", no_argument, 0, 'M' },
Marshall Dawsondbae6322019-03-04 10:31:03 -07001034 {"subprogram", required_argument, 0, 'S' },
Marshall Dawsonf4b9b412017-03-17 16:30:51 -06001035 {"pubkey", required_argument, 0, 'p' },
1036 {"bootloader", required_argument, 0, 'b' },
1037 {"smufirmware", required_argument, 0, 's' },
1038 {"recovery", required_argument, 0, 'r' },
1039 {"rtmpubkey", required_argument, 0, 'k' },
1040 {"secureos", required_argument, 0, 'c' },
1041 {"nvram", required_argument, 0, 'n' },
1042 {"securedebug", required_argument, 0, 'd' },
1043 {"trustlets", required_argument, 0, 't' },
1044 {"trustletkey", required_argument, 0, 'u' },
1045 {"smufirmware2", required_argument, 0, 'w' },
1046 {"smuscs", required_argument, 0, 'm' },
Marshall Dawsonef79fcc2019-04-01 10:16:41 -06001047 {"soft-fuse", required_argument, 0, 'T' },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001048 {"abl-image", required_argument, 0, 'z' },
1049 {"sec-gasket", required_argument, 0, 'J' },
1050 {"mp2-fw", required_argument, 0, 'B' },
1051 {"drv-entry-pts", required_argument, 0, 'K' },
1052 {"ikek", required_argument, 0, 'L' },
1053 {"s0i3drv", required_argument, 0, 'Y' },
1054 {"secdebug", required_argument, 0, 'N' },
1055 {"token-unlock", no_argument, 0, 'U' },
1056 {"whitelist", required_argument, 0, 'W' },
Martin Rothd3ce8c82019-07-13 20:13:07 -06001057 {"verstage", required_argument, 0, 'Z' },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001058 /* BIOS Directory Table items */
1059 {"instance", required_argument, 0, 'I' },
1060 {"apcb", required_argument, 0, 'a' },
1061 {"apob-base", required_argument, 0, 'Q' },
1062 {"bios-bin", required_argument, 0, 'V' },
1063 {"bios-bin-src", required_argument, 0, 'e' },
1064 {"bios-bin-dest", required_argument, 0, 'v' },
1065 {"bios-uncomp-size", required_argument, 0, 'j' },
1066 {"pmu-inst", required_argument, 0, 'y' },
1067 {"pmu-data", required_argument, 0, 'G' },
1068 {"ucode", required_argument, 0, 'O' },
1069 {"mp2-config", required_argument, 0, 'X' },
1070 {"apob-nv-base", required_argument, 0, 'F' },
1071 {"apob-nv-size", required_argument, 0, 'H' },
1072 /* other */
Marshall Dawsonf4b9b412017-03-17 16:30:51 -06001073 {"output", required_argument, 0, 'o' },
1074 {"flashsize", required_argument, 0, 'f' },
Martin Roth0d3b1182017-10-03 14:16:04 -06001075 {"location", required_argument, 0, 'l' },
Marshall Dawsonf4b9b412017-03-17 16:30:51 -06001076 {"help", no_argument, 0, 'h' },
Marshall Dawsonf4b9b412017-03-17 16:30:51 -06001077 {NULL, 0, 0, 0 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001078};
1079
Marshall Dawsonef79fcc2019-04-01 10:16:41 -06001080static void register_fw_fuse(char *str)
1081{
1082 int i;
1083
1084 for (i = 0; i < sizeof(amd_psp_fw_table) / sizeof(amd_fw_entry); i++) {
1085 if (amd_psp_fw_table[i].type != AMD_PSP_FUSE_CHAIN)
1086 continue;
1087
1088 amd_psp_fw_table[i].other = strtoull(str, NULL, 16);
1089 return;
1090 }
1091}
1092
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001093static void register_fw_token_unlock(void)
1094{
1095 int i;
1096
1097 for (i = 0; i < sizeof(amd_psp_fw_table) / sizeof(amd_fw_entry); i++) {
1098 if (amd_psp_fw_table[i].type != AMD_TOKEN_UNLOCK)
1099 continue;
1100
1101 amd_psp_fw_table[i].other = 1;
1102 return;
1103 }
1104}
1105
Marshall Dawsondbae6322019-03-04 10:31:03 -07001106static void register_fw_filename(amd_fw_type type, uint8_t sub, char filename[])
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001107{
Martin Roth8806f7f2016-11-08 10:44:18 -07001108 unsigned int i;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001109
Martin Rothcd15bc82016-11-08 11:34:02 -07001110 for (i = 0; i < sizeof(amd_fw_table) / sizeof(amd_fw_entry); i++) {
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001111 if (amd_fw_table[i].type == type) {
1112 amd_fw_table[i].filename = filename;
1113 return;
1114 }
1115 }
1116
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001117 for (i = 0; i < sizeof(amd_psp_fw_table) / sizeof(amd_fw_entry); i++) {
Marshall Dawsondbae6322019-03-04 10:31:03 -07001118 if (amd_psp_fw_table[i].type != type)
1119 continue;
1120
1121 if (amd_psp_fw_table[i].subprog == sub) {
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001122 amd_psp_fw_table[i].filename = filename;
1123 return;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001124 }
1125 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001126}
1127
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001128static void register_bdt_data(amd_bios_type type, int sub, int ins, char name[])
1129{
1130 int i;
1131
1132 for (i = 0; i < sizeof(amd_bios_table) / sizeof(amd_bios_entry); i++) {
1133 if (amd_bios_table[i].type == type
1134 && amd_bios_table[i].inst == ins
1135 && amd_bios_table[i].subpr == sub) {
1136 amd_bios_table[i].filename = name;
1137 return;
1138 }
1139 }
1140}
1141
Martin Rothec933132019-07-13 20:03:34 -06001142static void register_fw_addr(amd_bios_type type, char *src_str,
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001143 char *dst_str, char *size_str)
1144{
1145 int i;
1146 for (i = 0; i < sizeof(amd_bios_table) / sizeof(amd_bios_entry); i++) {
1147 if (amd_bios_table[i].type != type)
1148 continue;
1149
1150 if (src_str)
1151 amd_bios_table[i].src = strtoull(src_str, NULL, 16);
1152 if (dst_str)
1153 amd_bios_table[i].dest = strtoull(dst_str, NULL, 16);
1154 if (size_str)
1155 amd_bios_table[i].size = strtoul(size_str, NULL, 16);
1156
1157 return;
1158 }
1159}
1160
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001161int main(int argc, char **argv)
1162{
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001163 int c;
Martin Roth31d95a22016-11-08 11:22:12 -07001164 int retval = 0;
Martin Roth60f15512016-11-08 09:55:01 -07001165 char *tmp;
Martin Roth8806f7f2016-11-08 10:44:18 -07001166 char *rom = NULL;
Marshall Dawson239286c2019-02-23 16:42:46 -07001167 embedded_firmware *amd_romsig;
1168 psp_directory_table *pspdir;
Marshall Dawson67d868d2019-02-28 11:43:40 -07001169 int comboable = 0;
Marshall Dawsonef79fcc2019-04-01 10:16:41 -06001170 int fuse_defined = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001171 int targetfd;
Martin Roth8806f7f2016-11-08 10:44:18 -07001172 char *output = NULL;
Marshall Dawson2794a862019-03-04 16:53:15 -07001173 context ctx = {
1174 .rom_size = CONFIG_ROM_SIZE,
1175 };
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001176 /* Values cleared after each firmware or parameter, regardless if N/A */
1177 uint8_t sub = 0, instance = 0;
1178 int abl_image = 0;
Martin Roth0d3b1182017-10-03 14:16:04 -06001179 uint32_t dir_location = 0;
1180 uint32_t romsig_offset;
Martin Roth60f15512016-11-08 09:55:01 -07001181 uint32_t rom_base_address;
Marshall Dawson24f73d42019-04-01 10:48:43 -06001182 int multi = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001183
1184 while (1) {
1185 int optindex = 0;
1186
1187 c = getopt_long(argc, argv, optstring, long_options, &optindex);
1188
1189 if (c == -1)
1190 break;
1191
1192 switch (c) {
1193 case 'x':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001194 register_fw_filename(AMD_FW_XHCI, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001195 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001196 break;
1197 case 'i':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001198 register_fw_filename(AMD_FW_IMC, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001199 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001200 break;
1201 case 'g':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001202 register_fw_filename(AMD_FW_GEC, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001203 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001204 break;
Marshall Dawson67d868d2019-02-28 11:43:40 -07001205 case 'A':
1206 comboable = 1;
1207 break;
Marshall Dawson24f73d42019-04-01 10:48:43 -06001208 case 'M':
1209 multi = 1;
1210 break;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001211 case 'U':
1212 register_fw_token_unlock();
1213 sub = instance = 0;
1214 break;
Marshall Dawsondbae6322019-03-04 10:31:03 -07001215 case 'S':
1216 sub = (uint8_t)strtoul(optarg, &tmp, 16);
1217 break;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001218 case 'I':
1219 instance = strtoul(optarg, &tmp, 16);
1220 break;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001221 case 'p':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001222 register_fw_filename(AMD_FW_PSP_PUBKEY, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001223 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001224 break;
1225 case 'b':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001226 register_fw_filename(AMD_FW_PSP_BOOTLOADER,
1227 sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001228 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001229 break;
1230 case 's':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001231 register_fw_filename(AMD_FW_PSP_SMU_FIRMWARE,
1232 sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001233 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001234 break;
1235 case 'r':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001236 register_fw_filename(AMD_FW_PSP_RECOVERY, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001237 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001238 break;
1239 case 'k':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001240 register_fw_filename(AMD_FW_PSP_RTM_PUBKEY,
1241 sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001242 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001243 break;
1244 case 'c':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001245 register_fw_filename(AMD_FW_PSP_SECURED_OS,
1246 sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001247 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001248 break;
1249 case 'n':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001250 register_fw_filename(AMD_FW_PSP_NVRAM, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001251 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001252 break;
1253 case 'd':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001254 register_fw_filename(AMD_FW_PSP_SECURED_DEBUG,
1255 sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001256 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001257 break;
1258 case 't':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001259 register_fw_filename(AMD_FW_PSP_TRUSTLETS, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001260 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001261 break;
1262 case 'u':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001263 register_fw_filename(AMD_FW_PSP_TRUSTLETKEY,
1264 sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001265 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001266 break;
1267 case 'w':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001268 register_fw_filename(AMD_FW_PSP_SMU_FIRMWARE2,
1269 sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001270 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001271 break;
1272 case 'm':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001273 register_fw_filename(AMD_FW_PSP_SMUSCS, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001274 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001275 break;
Marshall Dawsonef79fcc2019-04-01 10:16:41 -06001276 case 'T':
1277 register_fw_fuse(optarg);
1278 fuse_defined = 1;
1279 sub = 0;
1280 break;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001281 case 'a':
1282 register_bdt_data(AMD_BIOS_APCB, sub, instance, optarg);
1283 register_bdt_data(AMD_BIOS_APCB_BK, sub,
1284 instance, optarg);
1285 sub = instance = 0;
1286 break;
1287 case 'Q':
1288 /* APOB destination */
1289 register_fw_addr(AMD_BIOS_APOB, 0, optarg, 0);
1290 sub = instance = 0;
1291 break;
1292 case 'F':
1293 /* APOB NV source */
1294 register_fw_addr(AMD_BIOS_APOB_NV, optarg, 0, 0);
1295 sub = instance = 0;
1296 break;
1297 case 'H':
1298 /* APOB NV size */
1299 register_fw_addr(AMD_BIOS_APOB_NV, 0, 0, optarg);
1300 sub = instance = 0;
1301 break;
1302 case 'V':
1303 register_bdt_data(AMD_BIOS_BIN, sub, instance, optarg);
1304 sub = instance = 0;
1305 break;
1306 case 'e':
1307 /* BIOS source */
1308 register_fw_addr(AMD_BIOS_BIN, optarg, 0, 0);
1309 sub = instance = 0;
1310 break;
1311 case 'v':
1312 /* BIOS destination */
1313 register_fw_addr(AMD_BIOS_BIN, 0, optarg, 0);
1314 sub = instance = 0;
1315 break;
1316 case 'j':
1317 /* BIOS destination size */
1318 register_fw_addr(AMD_BIOS_BIN, 0, 0, optarg);
1319 sub = instance = 0;
1320 break;
1321 case 'y':
1322 register_bdt_data(AMD_BIOS_PMUI, sub, instance, optarg);
1323 sub = instance = 0;
1324 break;
1325 case 'G':
1326 register_bdt_data(AMD_BIOS_PMUD, sub, instance, optarg);
1327 sub = instance = 0;
1328 break;
1329 case 'O':
1330 register_bdt_data(AMD_BIOS_UCODE, sub,
1331 instance, optarg);
1332 sub = instance = 0;
1333 break;
1334 case 'J':
1335 register_fw_filename(AMD_SEC_GASKET, sub, optarg);
1336 sub = instance = 0;
1337 break;
1338 case 'B':
1339 register_fw_filename(AMD_MP2_FW, sub, optarg);
1340 sub = instance = 0;
1341 break;
1342 case 'z':
1343 register_fw_filename(AMD_ABL0 + abl_image++,
1344 sub, optarg);
1345 sub = instance = 0;
1346 break;
1347 case 'X':
1348 register_bdt_data(AMD_BIOS_MP2_CFG, sub,
1349 instance, optarg);
1350 sub = instance = 0;
1351 break;
1352 case 'K':
1353 register_fw_filename(AMD_DRIVER_ENTRIES, sub, optarg);
1354 sub = instance = 0;
1355 break;
1356 case 'L':
1357 register_fw_filename(AMD_WRAPPED_IKEK, sub, optarg);
1358 sub = instance = 0;
1359 break;
1360 case 'Y':
1361 register_fw_filename(AMD_S0I3_DRIVER, sub, optarg);
1362 sub = instance = 0;
1363 break;
1364 case 'N':
1365 register_fw_filename(AMD_DEBUG_UNLOCK, sub, optarg);
1366 sub = instance = 0;
1367 break;
1368 case 'W':
1369 register_fw_filename(AMD_FW_PSP_WHITELIST, sub, optarg);
1370 sub = instance = 0;
1371 break;
Martin Rothd3ce8c82019-07-13 20:13:07 -06001372 case 'Z':
1373 register_fw_filename(AMD_FW_PSP_VERSTAGE, sub, optarg);
1374 sub = instance = 0;
1375 break;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001376 case 'o':
1377 output = optarg;
1378 break;
Martin Roth60f15512016-11-08 09:55:01 -07001379 case 'f':
Marshall Dawson2794a862019-03-04 16:53:15 -07001380 ctx.rom_size = (uint32_t)strtoul(optarg, &tmp, 16);
Martin Roth60f15512016-11-08 09:55:01 -07001381 if (*tmp != '\0') {
1382 printf("Error: ROM size specified"
1383 " incorrectly (%s)\n\n", optarg);
Martin Roth31d95a22016-11-08 11:22:12 -07001384 retval = 1;
Martin Roth60f15512016-11-08 09:55:01 -07001385 }
1386 break;
Martin Roth0d3b1182017-10-03 14:16:04 -06001387 case 'l':
1388 dir_location = (uint32_t)strtoul(optarg, &tmp, 16);
1389 if (*tmp != '\0') {
1390 printf("Error: Directory Location specified"
1391 " incorrectly (%s)\n\n", optarg);
1392 retval = 1;
1393 }
1394 break;
1395
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001396 case 'h':
1397 usage();
Martin Roth31d95a22016-11-08 11:22:12 -07001398 return 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001399 default:
1400 break;
1401 }
1402 }
1403
Marshall Dawsonef79fcc2019-04-01 10:16:41 -06001404 if (!fuse_defined)
1405 register_fw_fuse(DEFAULT_SOFT_FUSE_CHAIN);
1406
Martin Roth8806f7f2016-11-08 10:44:18 -07001407 if (!output) {
Martin Roth31d95a22016-11-08 11:22:12 -07001408 printf("Error: Output value is not specified.\n\n");
1409 retval = 1;
1410 }
1411
Marshall Dawson2794a862019-03-04 16:53:15 -07001412 if (ctx.rom_size % 1024 != 0) {
Martin Roth60f15512016-11-08 09:55:01 -07001413 printf("Error: ROM Size (%d bytes) should be a multiple of"
Marshall Dawson2794a862019-03-04 16:53:15 -07001414 " 1024 bytes.\n\n", ctx.rom_size);
Martin Roth31d95a22016-11-08 11:22:12 -07001415 retval = 1;
Martin Roth60f15512016-11-08 09:55:01 -07001416 }
1417
Marshall Dawson2794a862019-03-04 16:53:15 -07001418 if (ctx.rom_size < MIN_ROM_KB * 1024) {
Martin Roth31d95a22016-11-08 11:22:12 -07001419 printf("Error: ROM Size (%dKB) must be at least %dKB.\n\n",
Marshall Dawson2794a862019-03-04 16:53:15 -07001420 ctx.rom_size / 1024, MIN_ROM_KB);
Martin Roth31d95a22016-11-08 11:22:12 -07001421 retval = 1;
1422 }
1423
1424 if (retval) {
1425 usage();
1426 return retval;
Martin Roth60f15512016-11-08 09:55:01 -07001427 }
1428
Marshall Dawson2794a862019-03-04 16:53:15 -07001429 printf(" AMDFWTOOL Using ROM size of %dKB\n", ctx.rom_size / 1024);
Martin Roth60f15512016-11-08 09:55:01 -07001430
Marshall Dawson2794a862019-03-04 16:53:15 -07001431 rom_base_address = 0xFFFFFFFF - ctx.rom_size + 1;
Martin Roth0d3b1182017-10-03 14:16:04 -06001432 if (dir_location && (dir_location < rom_base_address)) {
1433 printf("Error: Directory location outside of ROM.\n\n");
1434 return 1;
1435 }
1436
1437 switch (dir_location) {
1438 case 0: /* Fall through */
1439 case 0xFFFA0000: /* Fall through */
1440 case 0xFFF20000: /* Fall through */
1441 case 0xFFE20000: /* Fall through */
1442 case 0xFFC20000: /* Fall through */
1443 case 0xFF820000: /* Fall through */
1444 case 0xFF020000: /* Fall through */
1445 break;
1446 default:
1447 printf("Error: Invalid Directory location.\n");
1448 printf(" Valid locations are 0xFFFA0000, 0xFFF20000,\n");
1449 printf(" 0xFFE20000, 0xFFC20000, 0xFF820000, 0xFF020000\n");
1450 return 1;
1451 }
1452
Marshall Dawson2794a862019-03-04 16:53:15 -07001453 ctx.rom = malloc(ctx.rom_size);
1454 if (!ctx.rom) {
1455 printf("Error: Failed to allocate memory\n");
Martin Roth31d95a22016-11-08 11:22:12 -07001456 return 1;
Marshall Dawson2794a862019-03-04 16:53:15 -07001457 }
1458 memset(ctx.rom, 0xFF, ctx.rom_size);
Martin Roth60f15512016-11-08 09:55:01 -07001459
Martin Roth0d3b1182017-10-03 14:16:04 -06001460 if (dir_location)
Marshall Dawson2794a862019-03-04 16:53:15 -07001461 romsig_offset = ctx.current = dir_location - rom_base_address;
Martin Roth0d3b1182017-10-03 14:16:04 -06001462 else
Marshall Dawson2794a862019-03-04 16:53:15 -07001463 romsig_offset = ctx.current = AMD_ROMSIG_OFFSET;
1464 printf(" AMDFWTOOL Using firmware directory location of 0x%08x\n",
1465 RUN_CURRENT(ctx));
Martin Roth0d3b1182017-10-03 14:16:04 -06001466
Marshall Dawson2794a862019-03-04 16:53:15 -07001467 amd_romsig = BUFF_OFFSET(ctx, romsig_offset);
Marshall Dawson239286c2019-02-23 16:42:46 -07001468 amd_romsig->signature = EMBEDDED_FW_SIGNATURE;
1469 amd_romsig->imc_entry = 0;
1470 amd_romsig->gec_entry = 0;
1471 amd_romsig->xhci_entry = 0;
Martin Roth60f15512016-11-08 09:55:01 -07001472
Marshall Dawson2794a862019-03-04 16:53:15 -07001473 integrate_firmwares(&ctx, amd_romsig, amd_fw_table);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001474
Patrick Georgi900a2542020-02-17 16:52:40 +01001475 ctx.current = ALIGN(ctx.current, 0x10000U); /* TODO: is it necessary? */
Marshall Dawson2794a862019-03-04 16:53:15 -07001476
Marshall Dawson24f73d42019-04-01 10:48:43 -06001477 if (multi) {
1478 /* Do 2nd PSP directory followed by 1st */
1479 psp_directory_table *pspdir2 = new_psp_dir(&ctx, multi);
1480 integrate_psp_firmwares(&ctx, pspdir2, 0,
1481 amd_psp_fw_table, PSPL2_COOKIE);
1482
1483 pspdir = new_psp_dir(&ctx, multi);
1484 integrate_psp_firmwares(&ctx, pspdir, pspdir2,
1485 amd_psp_fw_table, PSP_COOKIE);
1486 } else {
1487 /* flat: PSP 1 cookie and no pointer to 2nd table */
1488 pspdir = new_psp_dir(&ctx, multi);
1489 integrate_psp_firmwares(&ctx, pspdir, 0,
1490 amd_psp_fw_table, PSP_COOKIE);
1491 }
Marshall Dawson2794a862019-03-04 16:53:15 -07001492
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001493 if (comboable)
Marshall Dawson2794a862019-03-04 16:53:15 -07001494 amd_romsig->comboable = BUFF_TO_RUN(ctx, pspdir);
Marshall Dawson67d868d2019-02-28 11:43:40 -07001495 else
Marshall Dawson2794a862019-03-04 16:53:15 -07001496 amd_romsig->psp_entry = BUFF_TO_RUN(ctx, pspdir);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001497
zbaoc3a08a92016-03-02 14:47:27 +08001498#if PSP_COMBO
Marshall Dawson2794a862019-03-04 16:53:15 -07001499 psp_combo_directory *combo_dir = new_combo_dir(&ctx);
1500 amd_romsig->comboable = BUFF_TO_RUN(ctx, combo_dir);
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001501 /* 0 -Compare PSP ID, 1 -Compare chip family ID */
1502 combo_dir->entries[0].id_sel = 0;
1503 /* TODO: PSP ID. Documentation is needed. */
1504 combo_dir->entries[0].id = 0x10220B00;
Marshall Dawson2794a862019-03-04 16:53:15 -07001505 combo_dir->entries[0].lvl2_addr = BUFF_TO_RUN(ctx, pspdir);
Zheng Bao4fcc9f22015-11-20 12:29:04 +08001506
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001507 combo_dir->header.lookup = 1;
Marshall Dawsona378c222019-03-04 16:52:07 -07001508 fill_dir_header(combo_dir, 1, PSP2_COOKIE);
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001509#endif
Zheng Bao4fcc9f22015-11-20 12:29:04 +08001510
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001511 if (have_bios_tables(amd_bios_table)) {
1512 bios_directory_table *biosdir;
1513 if (multi) {
1514 /* Do 2nd level BIOS directory followed by 1st */
1515 bios_directory_table *biosdir2 =
1516 new_bios_dir(&ctx, multi);
1517 integrate_bios_firmwares(&ctx, biosdir2, 0,
1518 amd_bios_table, BDT2_COOKIE);
1519
1520 biosdir = new_bios_dir(&ctx, multi);
1521 integrate_bios_firmwares(&ctx, biosdir, biosdir2,
1522 amd_bios_table, BDT1_COOKIE);
1523 } else {
1524 /* flat: BDT1 cookie and no pointer to 2nd table */
1525 biosdir = new_bios_dir(&ctx, multi);
1526 integrate_bios_firmwares(&ctx, biosdir, 0,
1527 amd_bios_table, BDT1_COOKIE);
1528 }
1529 amd_romsig->bios1_entry = BUFF_TO_RUN(ctx, biosdir);
1530 }
1531
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001532 targetfd = open(output, O_RDWR | O_CREAT | O_TRUNC, 0666);
Martin Roth31d95a22016-11-08 11:22:12 -07001533 if (targetfd >= 0) {
Marshall Dawson2794a862019-03-04 16:53:15 -07001534 write(targetfd, amd_romsig, ctx.current - romsig_offset);
Martin Roth31d95a22016-11-08 11:22:12 -07001535 close(targetfd);
1536 } else {
1537 printf("Error: could not open file: %s\n", output);
1538 retval = 1;
1539 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001540
Martin Roth31d95a22016-11-08 11:22:12 -07001541 free(rom);
1542 return retval;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001543}