blob: 39cc45f08046ba217f640927b083105af4e91fcc [file] [log] [blame]
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001/*
2 * This file is part of the coreboot project.
3 *
Marshall Dawsone7d892c2016-10-08 14:49:41 -06004 * Copyright (C) 2015 - 2016 Advanced Micro Devices, Inc.
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16/*
17 * ROMSIG At ROMBASE + 0x20000:
zbaoc3b0b722016-02-19 13:47:31 +080018 * 0 4 8 C
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080019 * +------------+---------------+----------------+------------+
20 * | 0x55AA55AA |EC ROM Address |GEC ROM Address |USB3 ROM |
21 * +------------+---------------+----------------+------------+
zbaoc3b0b722016-02-19 13:47:31 +080022 * | PSPDIR ADDR|PSPDIR ADDR |<-- Field 0x14 could be either
23 * +------------+---------------+ 2nd PSP directory or PSP COMBO directory
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080024 * EC ROM should be 64K aligned.
25 *
Zheng Bao4fcc9f22015-11-20 12:29:04 +080026 * PSP directory (Where "PSPDIR ADDR" points)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080027 * +------------+---------------+----------------+------------+
28 * | 'PSP$' | Fletcher | Count | Reserved |
29 * +------------+---------------+----------------+------------+
30 * | 0 | size | Base address | Reserved | Pubkey
31 * +------------+---------------+----------------+------------+
32 * | 1 | size | Base address | Reserved | Bootloader
33 * +------------+---------------+----------------+------------+
34 * | 8 | size | Base address | Reserved | Smu Firmware
35 * +------------+---------------+----------------+------------+
36 * | 3 | size | Base address | Reserved | Recovery Firmware
37 * +------------+---------------+----------------+------------+
38 * | |
39 * | |
40 * | Other PSP Firmware |
41 * | |
42 * | |
43 * +------------+---------------+----------------+------------+
Zheng Bao4fcc9f22015-11-20 12:29:04 +080044 *
zbaoc3b0b722016-02-19 13:47:31 +080045 * PSP Combo directory
Zheng Bao4fcc9f22015-11-20 12:29:04 +080046 * +------------+---------------+----------------+------------+
zbao6e2f3d12016-02-19 13:34:59 +080047 * | 'PSP2' | Fletcher | Count |Look up mode|
Zheng Bao4fcc9f22015-11-20 12:29:04 +080048 * +------------+---------------+----------------+------------+
zbaoc3a08a92016-03-02 14:47:27 +080049 * | R e s e r v e d |
50 * +------------+---------------+----------------+------------+
zbao6e2f3d12016-02-19 13:34:59 +080051 * | ID-Sel | PSP ID | PSPDIR ADDR | | 2nd PSP directory
Zheng Bao4fcc9f22015-11-20 12:29:04 +080052 * +------------+---------------+----------------+------------+
zbao6e2f3d12016-02-19 13:34:59 +080053 * | ID-Sel | PSP ID | PSPDIR ADDR | | 3rd PSP directory
Zheng Bao4fcc9f22015-11-20 12:29:04 +080054 * +------------+---------------+----------------+------------+
55 * | |
56 * | Other PSP |
57 * | |
58 * +------------+---------------+----------------+------------+
59 *
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080060 */
61
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080062#include <fcntl.h>
63#include <errno.h>
64#include <stdio.h>
65#include <sys/stat.h>
66#include <sys/types.h>
67#include <unistd.h>
68#include <string.h>
69#include <stdlib.h>
70#include <getopt.h>
71
72#ifndef CONFIG_ROM_SIZE
73#define CONFIG_ROM_SIZE 0x400000
74#endif
75
Martin Roth60f15512016-11-08 09:55:01 -070076#define AMD_ROMSIG_OFFSET 0x20000
77#define MIN_ROM_KB 256
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080078
Martin Rothcd15bc82016-11-08 11:34:02 -070079#define ALIGN(val, by) (((val) + (by) - 1) & ~((by) - 1))
Marshall Dawson7c1e1422019-04-11 09:44:43 -060080#define _MAX(A, B) (((A) > (B)) ? (A) : (B))
81#define ERASE_ALIGNMENT 0x1000U
Marshall Dawson2794a862019-03-04 16:53:15 -070082#define TABLE_ALIGNMENT 0x1000U
83#define BLOB_ALIGNMENT 0x100U
Marshall Dawson24f73d42019-04-01 10:48:43 -060084#define TABLE_ERASE_ALIGNMENT _MAX(TABLE_ALIGNMENT, ERASE_ALIGNMENT)
Marshall Dawson7c1e1422019-04-11 09:44:43 -060085#define BLOB_ERASE_ALIGNMENT _MAX(BLOB_ALIGNMENT, ERASE_ALIGNMENT)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080086
Marshall Dawsonef79fcc2019-04-01 10:16:41 -060087#define DEFAULT_SOFT_FUSE_CHAIN "0x1"
88
Marshall Dawson239286c2019-02-23 16:42:46 -070089#define EMBEDDED_FW_SIGNATURE 0x55aa55aa
Marshall Dawson24f73d42019-04-01 10:48:43 -060090#define PSP_COOKIE 0x50535024 /* 'PSP$' */
91#define PSPL2_COOKIE 0x324c5024 /* '2LP$' */
92#define PSP2_COOKIE 0x50535032 /* 'PSP2' */
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -060093#define BDT1_COOKIE 0x44484224 /* 'DHB$ */
94#define BDT2_COOKIE 0x324c4224 /* '2LB$ */
Marshall Dawson239286c2019-02-23 16:42:46 -070095
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080096/*
Marshall Dawson0e02ce82019-03-04 16:50:37 -070097 * Beginning with Family 15h Models 70h-7F, a.k.a Stoney Ridge, the PSP
98 * can support an optional "combo" implementation. If the PSP sees the
99 * PSP2 cookie, it interprets the table as a roadmap to additional PSP
100 * tables. Using this, support for multiple product generations may be
101 * built into one image. If the PSP$ cookie is found, the table is a
102 * normal directory table.
103 *
104 * Modern generations supporting the combo directories require the
105 * pointer to be at offset 0x14 of the Embedded Firmware Structure,
106 * regardless of the type of directory used. The --combo-capable
107 * argument enforces this placement.
108 *
109 * TODO: Future work may require fully implementing the PSP_COMBO feature.
zbaoc3b0b722016-02-19 13:47:31 +0800110 */
Marshall Dawson0e02ce82019-03-04 16:50:37 -0700111#define PSP_COMBO 0
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800112
Marshall Dawson239286c2019-02-23 16:42:46 -0700113typedef unsigned long long int uint64_t;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800114typedef unsigned int uint32_t;
115typedef unsigned char uint8_t;
116typedef unsigned short uint16_t;
117
118/*
119 * Creates the OSI Fletcher checksum. See 8473-1, Appendix C, section C.3.
120 * The checksum field of the passed PDU does not need to be reset to zero.
121 *
122 * The "Fletcher Checksum" was proposed in a paper by John G. Fletcher of
123 * Lawrence Livermore Labs. The Fletcher Checksum was proposed as an
124 * alternative to cyclical redundancy checks because it provides error-
125 * detection properties similar to cyclical redundancy checks but at the
126 * cost of a simple summation technique. Its characteristics were first
127 * published in IEEE Transactions on Communications in January 1982. One
128 * version has been adopted by ISO for use in the class-4 transport layer
129 * of the network protocol.
130 *
131 * This program expects:
132 * stdin: The input file to compute a checksum for. The input file
133 * not be longer than 256 bytes.
134 * stdout: Copied from the input file with the Fletcher's Checksum
135 * inserted 8 bytes after the beginning of the file.
136 * stderr: Used to print out error messages.
137 */
Marshall Dawson8a45a4d2019-02-24 07:18:44 -0700138static uint32_t fletcher32(const void *data, int length)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800139{
140 uint32_t c0;
141 uint32_t c1;
142 uint32_t checksum;
143 int index;
Marshall Dawson8a45a4d2019-02-24 07:18:44 -0700144 const uint16_t *pptr = data;
145
146 length /= 2;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800147
148 c0 = 0xFFFF;
149 c1 = 0xFFFF;
150
Marshall Dawsonb85ddc52019-07-23 07:24:30 -0600151 while (length) {
152 index = length >= 359 ? 359 : length;
153 length -= index;
154 do {
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800155 c0 += *(pptr++);
156 c1 += c0;
Marshall Dawsonb85ddc52019-07-23 07:24:30 -0600157 } while (--index);
158 c0 = (c0 & 0xFFFF) + (c0 >> 16);
159 c1 = (c1 & 0xFFFF) + (c1 >> 16);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800160 }
161
Marshall Dawson8a45a4d2019-02-24 07:18:44 -0700162 /* Sums[0,1] mod 64K + overflow */
163 c0 = (c0 & 0xFFFF) + (c0 >> 16);
164 c1 = (c1 & 0xFFFF) + (c1 >> 16);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800165 checksum = (c1 << 16) | c0;
166
167 return checksum;
168}
169
Martin Roth8806f7f2016-11-08 10:44:18 -0700170static void usage(void)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800171{
Martin Roth0e940622016-11-08 10:37:53 -0700172 printf("amdfwtool: Create AMD Firmware combination\n");
173 printf("Usage: amdfwtool [options] -f <size> -o <filename>\n");
Marshall Dawsonf4b9b412017-03-17 16:30:51 -0600174 printf("-x | --xhci <FILE> Add XHCI blob\n");
175 printf("-i | --imc <FILE> Add IMC blob\n");
176 printf("-g | --gec <FILE> Add GEC blob\n");
Martin Roth0e940622016-11-08 10:37:53 -0700177
178 printf("\nPSP options:\n");
Marshall Dawson67d868d2019-02-28 11:43:40 -0700179 printf("-A | --combo-capable Place PSP directory pointer at Embedded Firmware\n");
180 printf(" offset able to support combo directory\n");
Marshall Dawson24f73d42019-04-01 10:48:43 -0600181 printf("-M | --multilevel Generate primary and secondary tables\n");
Marshall Dawsonf4b9b412017-03-17 16:30:51 -0600182 printf("-p | --pubkey <FILE> Add pubkey\n");
183 printf("-b | --bootloader <FILE> Add bootloader\n");
Marshall Dawsondbae6322019-03-04 10:31:03 -0700184 printf("-S | --subprogram <number> Sets subprogram field for the next firmware\n");
Marshall Dawsonf4b9b412017-03-17 16:30:51 -0600185 printf("-s | --smufirmware <FILE> Add smufirmware\n");
186 printf("-r | --recovery <FILE> Add recovery\n");
187 printf("-k | --rtmpubkey <FILE> Add rtmpubkey\n");
188 printf("-c | --secureos <FILE> Add secureos\n");
189 printf("-n | --nvram <FILE> Add nvram\n");
190 printf("-d | --securedebug <FILE> Add securedebug\n");
191 printf("-t | --trustlets <FILE> Add trustlets\n");
192 printf("-u | --trustletkey <FILE> Add trustletkey\n");
193 printf("-w | --smufirmware2 <FILE> Add smufirmware2\n");
194 printf("-m | --smuscs <FILE> Add smuscs\n");
Marshall Dawsonef79fcc2019-04-01 10:16:41 -0600195 printf("-T | --soft-fuse <HEX_VAL> Override default soft fuse values\n");
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600196 printf("-z | --abl-image <FILE> Add AGESA Binary\n");
197 printf("-J | --sec-gasket <FILE> Add security gasket\n");
198 printf("-B | --mp2-fw <FILE> Add MP2 firmware\n");
199 printf("-N | --secdebug <FILE> Add secure unlock image\n");
200 printf("-U | --token-unlock Reserve space for debug token\n");
201 printf("-K | --drv-entry-pts <FILE> Add PSP driver entry points\n");
202 printf("-L | --ikek <FILE> Add Wrapped iKEK\n");
203 printf("-Y | --s0i3drv <FILE> Add s0i3 driver\n");
Martin Rothd3ce8c82019-07-13 20:13:07 -0600204 printf("-Z | --verstage <FILE> Add verstage\n");
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600205 printf("\nBIOS options:\n");
206 printf("-I | --instance <number> Sets instance field for the next BIOS firmware\n");
207 printf("-a | --apcb <FILE> Add AGESA PSP customization block\n");
208 printf("-Q | --apob-base <HEX_VAL> Destination for AGESA PSP output block\n");
209 printf("-F | --apob-nv-base <HEX_VAL> Location of S3 resume data\n");
210 printf("-H | --apob-nv-size <HEX_VAL> Size of S3 resume data\n");
211 printf("-y | --pmu-inst <FILE> Add PMU firmware instruction portion\n");
212 printf("-G | --pmu-data <FILE> Add PMU firmware data portion\n");
Martin Rothec933132019-07-13 20:03:34 -0600213 printf("-O | --ucode <FILE> Add microcode patch\n");
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600214 printf("-X | --mp2-config <FILE> Add MP2 configuration\n");
215 printf("-V | --bios-bin <FILE> Add compressed image; auto source address\n");
216 printf("-e | --bios-bin-src <HEX_VAL> Address in flash of source if -V not used\n");
217 printf("-v | --bios-bin-dest <HEX_VAL> Destination for uncompressed BIOS\n");
218 printf("-j | --bios-uncomp-size <HEX> Uncompressed size of BIOS image\n");
Martin Roth0e940622016-11-08 10:37:53 -0700219 printf("\n-o | --output <filename> output filename\n");
Marshall Dawsonf4b9b412017-03-17 16:30:51 -0600220 printf("-f | --flashsize <HEX_VAL> ROM size in bytes\n");
221 printf(" size must be larger than %dKB\n",
Martin Roth0e940622016-11-08 10:37:53 -0700222 MIN_ROM_KB);
Marshall Dawsonf4b9b412017-03-17 16:30:51 -0600223 printf(" and must a multiple of 1024\n");
Martin Roth0d3b1182017-10-03 14:16:04 -0600224 printf("-l | --location Location of Directory\n");
Marshall Dawsonf4b9b412017-03-17 16:30:51 -0600225 printf("-h | --help show this help\n");
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800226}
227
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600228typedef enum _amd_bios_type {
229 AMD_BIOS_APCB = 0x60,
230 AMD_BIOS_APOB = 0x61,
231 AMD_BIOS_BIN = 0x62,
232 AMD_BIOS_APOB_NV = 0x63,
233 AMD_BIOS_PMUI = 0x64,
234 AMD_BIOS_PMUD = 0x65,
235 AMD_BIOS_UCODE = 0x66,
236 AMD_BIOS_APCB_BK = 0x68,
237 AMD_BIOS_MP2_CFG = 0x6a,
238 AMD_BIOS_L2_PTR = 0x70,
239 AMD_BIOS_INVALID,
240} amd_bios_type;
241
242#define BDT_LVL1 0x1
243#define BDT_LVL2 0x2
244#define BDT_BOTH (BDT_LVL1 | BDT_LVL2)
245typedef struct _amd_bios_entry {
246 amd_bios_type type;
247 int region_type;
248 int reset;
249 int copy;
250 int ro;
251 int zlib;
252 int inst;
253 int subpr;
254 uint64_t src;
255 uint64_t dest;
256 size_t size;
257 char *filename;
258 int level;
259} amd_bios_entry;
260
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800261typedef enum _amd_fw_type {
262 AMD_FW_PSP_PUBKEY = 0,
263 AMD_FW_PSP_BOOTLOADER = 1,
264 AMD_FW_PSP_SMU_FIRMWARE = 8,
265 AMD_FW_PSP_RECOVERY = 3,
266 AMD_FW_PSP_RTM_PUBKEY = 5,
267 AMD_FW_PSP_SECURED_OS = 2,
268 AMD_FW_PSP_NVRAM = 4,
269 AMD_FW_PSP_SECURED_DEBUG = 9,
270 AMD_FW_PSP_TRUSTLETS = 12,
271 AMD_FW_PSP_TRUSTLETKEY = 13,
272 AMD_FW_PSP_SMU_FIRMWARE2 = 18,
273 AMD_PSP_FUSE_CHAIN = 11,
274 AMD_FW_PSP_SMUSCS = 95,
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600275 AMD_DEBUG_UNLOCK = 0x13,
276 AMD_WRAPPED_IKEK = 0x21,
277 AMD_TOKEN_UNLOCK = 0x22,
278 AMD_SEC_GASKET = 0x24,
279 AMD_MP2_FW = 0x25,
280 AMD_DRIVER_ENTRIES = 0x28,
281 AMD_S0I3_DRIVER = 0x2d,
282 AMD_ABL0 = 0x30,
283 AMD_ABL1 = 0x31,
284 AMD_ABL2 = 0x32,
285 AMD_ABL3 = 0x33,
286 AMD_ABL4 = 0x34,
287 AMD_ABL5 = 0x35,
288 AMD_ABL6 = 0x36,
289 AMD_ABL7 = 0x37,
290 AMD_FW_PSP_WHITELIST = 0x3a,
Marshall Dawson24f73d42019-04-01 10:48:43 -0600291 AMD_FW_L2_PTR = 0x40,
Martin Rothd3ce8c82019-07-13 20:13:07 -0600292 AMD_FW_PSP_VERSTAGE = 0x52,
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800293 AMD_FW_IMC,
294 AMD_FW_GEC,
295 AMD_FW_XHCI,
zbaoc3a08a92016-03-02 14:47:27 +0800296 AMD_FW_INVALID,
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800297} amd_fw_type;
298
Marshall Dawson24f73d42019-04-01 10:48:43 -0600299#define PSP_LVL1 0x1
300#define PSP_LVL2 0x2
301#define PSP_BOTH (PSP_LVL1 | PSP_LVL2)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800302typedef struct _amd_fw_entry {
303 amd_fw_type type;
Marshall Dawsondbae6322019-03-04 10:31:03 -0700304 uint8_t subprog;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800305 char *filename;
Marshall Dawson24f73d42019-04-01 10:48:43 -0600306 int level;
Marshall Dawsonef79fcc2019-04-01 10:16:41 -0600307 uint64_t other;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800308} amd_fw_entry;
309
Martin Roth8806f7f2016-11-08 10:44:18 -0700310static amd_fw_entry amd_psp_fw_table[] = {
Marshall Dawson24f73d42019-04-01 10:48:43 -0600311 { .type = AMD_FW_PSP_PUBKEY, .level = PSP_BOTH },
312 { .type = AMD_FW_PSP_BOOTLOADER, .level = PSP_BOTH },
313 { .type = AMD_FW_PSP_SMU_FIRMWARE, .subprog = 0, .level = PSP_BOTH },
314 { .type = AMD_FW_PSP_RECOVERY, .level = PSP_LVL1 },
315 { .type = AMD_FW_PSP_RTM_PUBKEY, .level = PSP_BOTH },
316 { .type = AMD_FW_PSP_SECURED_OS, .level = PSP_LVL2 },
317 { .type = AMD_FW_PSP_NVRAM, .level = PSP_LVL2 },
318 { .type = AMD_FW_PSP_SMU_FIRMWARE, .subprog = 2, .level = PSP_BOTH },
319 { .type = AMD_FW_PSP_SECURED_DEBUG, .level = PSP_LVL2 },
320 { .type = AMD_FW_PSP_TRUSTLETS, .level = PSP_LVL2 },
321 { .type = AMD_FW_PSP_TRUSTLETKEY, .level = PSP_LVL2 },
322 { .type = AMD_FW_PSP_SMU_FIRMWARE2, .subprog = 2, .level = PSP_BOTH },
323 { .type = AMD_FW_PSP_SMU_FIRMWARE, .subprog = 1, .level = PSP_BOTH },
324 { .type = AMD_FW_PSP_SMU_FIRMWARE2, .subprog = 1, .level = PSP_BOTH },
325 { .type = AMD_FW_PSP_SMU_FIRMWARE2, .level = PSP_BOTH },
326 { .type = AMD_FW_PSP_SMUSCS, .level = PSP_BOTH },
327 { .type = AMD_PSP_FUSE_CHAIN, .level = PSP_LVL2 },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600328 { .type = AMD_DEBUG_UNLOCK, .level = PSP_LVL2 },
329 { .type = AMD_WRAPPED_IKEK, .level = PSP_BOTH },
330 { .type = AMD_TOKEN_UNLOCK, .level = PSP_BOTH },
331 { .type = AMD_SEC_GASKET, .subprog = 2, .level = PSP_BOTH },
332 { .type = AMD_SEC_GASKET, .subprog = 1, .level = PSP_BOTH },
333 { .type = AMD_MP2_FW, .subprog = 2, .level = PSP_LVL2 },
334 { .type = AMD_MP2_FW, .subprog = 1, .level = PSP_LVL2 },
335 { .type = AMD_DRIVER_ENTRIES, .level = PSP_LVL2 },
336 { .type = AMD_S0I3_DRIVER, .level = PSP_LVL2 },
337 { .type = AMD_ABL0, .level = PSP_BOTH },
338 { .type = AMD_ABL1, .level = PSP_BOTH },
339 { .type = AMD_ABL2, .level = PSP_BOTH },
340 { .type = AMD_ABL3, .level = PSP_BOTH },
341 { .type = AMD_ABL4, .level = PSP_BOTH },
342 { .type = AMD_ABL5, .level = PSP_BOTH },
343 { .type = AMD_ABL6, .level = PSP_BOTH },
344 { .type = AMD_ABL7, .level = PSP_BOTH },
Marshall Dawson24f73d42019-04-01 10:48:43 -0600345 { .type = AMD_FW_PSP_SMU_FIRMWARE, .subprog = 1, .level = PSP_BOTH },
346 { .type = AMD_FW_PSP_SMU_FIRMWARE2, .subprog = 1, .level = PSP_BOTH },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600347 { .type = AMD_FW_PSP_WHITELIST, .level = PSP_LVL2 },
Martin Rothd3ce8c82019-07-13 20:13:07 -0600348 { .type = AMD_FW_PSP_VERSTAGE, .level = PSP_BOTH },
zbaoc3a08a92016-03-02 14:47:27 +0800349 { .type = AMD_FW_INVALID },
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800350};
351
Martin Roth8806f7f2016-11-08 10:44:18 -0700352static amd_fw_entry amd_fw_table[] = {
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800353 { .type = AMD_FW_XHCI },
354 { .type = AMD_FW_IMC },
355 { .type = AMD_FW_GEC },
zbaoc3a08a92016-03-02 14:47:27 +0800356 { .type = AMD_FW_INVALID },
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800357};
358
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600359static amd_bios_entry amd_bios_table[] = {
Marshall Dawson0581bf62019-09-25 11:03:53 -0600360 { .type = AMD_BIOS_APCB, .inst = 0, .level = BDT_BOTH },
361 { .type = AMD_BIOS_APCB, .inst = 1, .level = BDT_BOTH },
362 { .type = AMD_BIOS_APCB, .inst = 2, .level = BDT_BOTH },
363 { .type = AMD_BIOS_APCB, .inst = 3, .level = BDT_BOTH },
364 { .type = AMD_BIOS_APCB, .inst = 4, .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 },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600370 { .type = AMD_BIOS_APOB, .level = BDT_BOTH },
371 { .type = AMD_BIOS_BIN,
372 .reset = 1, .copy = 1, .zlib = 1, .level = BDT_BOTH },
373 { .type = AMD_BIOS_APOB_NV, .level = BDT_LVL2 },
374 { .type = AMD_BIOS_PMUI, .inst = 1, .subpr = 0, .level = BDT_BOTH },
375 { .type = AMD_BIOS_PMUD, .inst = 1, .subpr = 0, .level = BDT_BOTH },
376 { .type = AMD_BIOS_PMUI, .inst = 4, .subpr = 0, .level = BDT_BOTH },
377 { .type = AMD_BIOS_PMUD, .inst = 4, .subpr = 0, .level = BDT_BOTH },
378 { .type = AMD_BIOS_PMUI, .inst = 1, .subpr = 1, .level = BDT_BOTH },
379 { .type = AMD_BIOS_PMUD, .inst = 1, .subpr = 1, .level = BDT_BOTH },
380 { .type = AMD_BIOS_PMUI, .inst = 4, .subpr = 1, .level = BDT_BOTH },
381 { .type = AMD_BIOS_PMUD, .inst = 4, .subpr = 1, .level = BDT_BOTH },
382 { .type = AMD_BIOS_UCODE, .inst = 0, .level = BDT_LVL2 },
383 { .type = AMD_BIOS_UCODE, .inst = 1, .level = BDT_LVL2 },
384 { .type = AMD_BIOS_UCODE, .inst = 2, .level = BDT_LVL2 },
385 { .type = AMD_BIOS_MP2_CFG, .level = BDT_LVL2 },
386 { .type = AMD_BIOS_INVALID },
387};
388
Marshall Dawson239286c2019-02-23 16:42:46 -0700389typedef struct _embedded_firmware {
390 uint32_t signature; /* 0x55aa55aa */
391 uint32_t imc_entry;
392 uint32_t gec_entry;
393 uint32_t xhci_entry;
394 uint32_t psp_entry;
395 uint32_t comboable;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600396 uint32_t bios0_entry; /* todo: add way to select correct entry */
397 uint32_t bios1_entry;
Marshall Dawson94f24922019-09-28 08:49:09 -0600398 uint32_t bios2_entry;
399 uint32_t reserved[0x2c]; /* 0x24 - 0x4f */
Marshall Dawson239286c2019-02-23 16:42:46 -0700400} __attribute__((packed, aligned(16))) embedded_firmware;
401
402typedef struct _psp_directory_header {
403 uint32_t cookie;
404 uint32_t checksum;
405 uint32_t num_entries;
406 uint32_t reserved;
407} __attribute__((packed, aligned(16))) psp_directory_header;
408
409typedef struct _psp_directory_entry {
Marshall Dawsondbae6322019-03-04 10:31:03 -0700410 uint8_t type;
411 uint8_t subprog;
412 uint16_t rsvd;
Marshall Dawson239286c2019-02-23 16:42:46 -0700413 uint32_t size;
414 uint64_t addr; /* or a value in some cases */
415} __attribute__((packed)) psp_directory_entry;
416
417typedef struct _psp_directory_table {
418 psp_directory_header header;
419 psp_directory_entry entries[];
420} __attribute__((packed)) psp_directory_table;
421
Marshall Dawson2794a862019-03-04 16:53:15 -0700422#define MAX_PSP_ENTRIES 0x1f
423
Marshall Dawson239286c2019-02-23 16:42:46 -0700424typedef struct _psp_combo_header {
425 uint32_t cookie;
426 uint32_t checksum;
427 uint32_t num_entries;
428 uint32_t lookup;
429 uint64_t reserved[2];
430} __attribute__((packed, aligned(16))) psp_combo_header;
431
432typedef struct _psp_combo_entry {
433 uint32_t id_sel;
434 uint32_t id;
435 uint64_t lvl2_addr;
436} __attribute__((packed)) psp_combo_entry;
437
438typedef struct _psp_combo_directory {
439 psp_combo_header header;
440 psp_combo_entry entries[];
441} __attribute__((packed)) psp_combo_directory;
442
Marshall Dawson2794a862019-03-04 16:53:15 -0700443#define MAX_COMBO_ENTRIES 1
444
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600445typedef struct _bios_directory_hdr {
446 uint32_t cookie;
447 uint32_t checksum;
448 uint32_t num_entries;
449 uint32_t reserved;
450} __attribute__((packed, aligned(16))) bios_directory_hdr;
451
452typedef struct _bios_directory_entry {
453 uint8_t type;
454 uint8_t region_type;
455 int reset:1;
456 int copy:1;
457 int ro:1;
458 int compressed:1;
459 int inst:4;
460 uint8_t subprog; /* b[7:3] reserved */
461 uint32_t size;
462 uint64_t source;
463 uint64_t dest;
464} __attribute__((packed)) bios_directory_entry;
465
466typedef struct _bios_directory_table {
467 bios_directory_hdr header;
468 bios_directory_entry entries[];
469} bios_directory_table;
470
471#define MAX_BIOS_ENTRIES 0x1f
472
Marshall Dawson2794a862019-03-04 16:53:15 -0700473typedef struct _context {
474 char *rom; /* target buffer, size of flash device */
475 uint32_t rom_size; /* size of flash device */
476 uint32_t current; /* pointer within flash & proxy buffer */
477} context;
478
479#define RUN_BASE(ctx) (0xFFFFFFFF - (ctx).rom_size + 1)
480#define RUN_OFFSET(ctx, offset) (RUN_BASE(ctx) + (offset))
481#define RUN_CURRENT(ctx) RUN_OFFSET((ctx), (ctx).current)
482#define BUFF_OFFSET(ctx, offset) ((void *)((ctx).rom + (offset)))
483#define BUFF_CURRENT(ctx) BUFF_OFFSET((ctx), (ctx).current)
484#define BUFF_TO_RUN(ctx, ptr) RUN_OFFSET((ctx), ((char *)(ptr) - (ctx).rom))
485#define BUFF_ROOM(ctx) ((ctx).rom_size - (ctx).current)
486
Marshall Dawson24f73d42019-04-01 10:48:43 -0600487static void *new_psp_dir(context *ctx, int multi)
Marshall Dawson2794a862019-03-04 16:53:15 -0700488{
489 void *ptr;
490
Marshall Dawson24f73d42019-04-01 10:48:43 -0600491 /*
492 * Force both onto boundary when multi. Primary table is after
493 * updatable table, so alignment ensures primary can stay intact
494 * if secondary is reprogrammed.
495 */
496 if (multi)
497 ctx->current = ALIGN(ctx->current, TABLE_ERASE_ALIGNMENT);
498 else
499 ctx->current = ALIGN(ctx->current, TABLE_ALIGNMENT);
500
Marshall Dawson2794a862019-03-04 16:53:15 -0700501 ptr = BUFF_CURRENT(*ctx);
502 ctx->current += sizeof(psp_directory_header)
503 + MAX_PSP_ENTRIES * sizeof(psp_directory_entry);
504 return ptr;
505}
506
Martin Rothec933132019-07-13 20:03:34 -0600507#if PSP_COMBO
Marshall Dawson2794a862019-03-04 16:53:15 -0700508static void *new_combo_dir(context *ctx)
509{
510 void *ptr;
511
512 ctx->current = ALIGN(ctx->current, TABLE_ALIGNMENT);
513 ptr = BUFF_CURRENT(*ctx);
514 ctx->current += sizeof(psp_combo_header)
515 + MAX_COMBO_ENTRIES * sizeof(psp_combo_entry);
516 return ptr;
517}
Martin Rothec933132019-07-13 20:03:34 -0600518#endif
Marshall Dawson2794a862019-03-04 16:53:15 -0700519
Marshall Dawsona378c222019-03-04 16:52:07 -0700520static void fill_dir_header(void *directory, uint32_t count, uint32_t cookie)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800521{
Marshall Dawson24f73d42019-04-01 10:48:43 -0600522 psp_combo_directory *cdir = directory;
523 psp_directory_table *dir = directory;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600524 bios_directory_table *bdir = directory;
Marshall Dawson24f73d42019-04-01 10:48:43 -0600525
526 if (!count)
527 return;
528
529 switch (cookie) {
530 case PSP2_COOKIE:
Marshall Dawsona378c222019-03-04 16:52:07 -0700531 /* caller is responsible for lookup mode */
Marshall Dawsona378c222019-03-04 16:52:07 -0700532 cdir->header.cookie = cookie;
533 cdir->header.num_entries = count;
534 cdir->header.reserved[0] = 0;
535 cdir->header.reserved[1] = 0;
536 /* checksum everything that comes after the Checksum field */
537 cdir->header.checksum = fletcher32(&cdir->header.num_entries,
538 count * sizeof(psp_combo_entry)
539 + sizeof(cdir->header.num_entries)
540 + sizeof(cdir->header.lookup)
541 + 2 * sizeof(cdir->header.reserved[0]));
Marshall Dawson24f73d42019-04-01 10:48:43 -0600542 break;
543 case PSP_COOKIE:
544 case PSPL2_COOKIE:
Marshall Dawsona378c222019-03-04 16:52:07 -0700545 dir->header.cookie = cookie;
546 dir->header.num_entries = count;
547 dir->header.reserved = 0;
548 /* checksum everything that comes after the Checksum field */
549 dir->header.checksum = fletcher32(&dir->header.num_entries,
Marshall Dawson8a45a4d2019-02-24 07:18:44 -0700550 count * sizeof(psp_directory_entry)
Marshall Dawsona378c222019-03-04 16:52:07 -0700551 + sizeof(dir->header.num_entries)
552 + sizeof(dir->header.reserved));
Marshall Dawson24f73d42019-04-01 10:48:43 -0600553 break;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600554 case BDT1_COOKIE:
555 case BDT2_COOKIE:
556 bdir->header.cookie = cookie;
557 bdir->header.num_entries = count;
558 bdir->header.reserved = 0;
559 /* 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)
563 + sizeof(bdir->header.reserved));
564 break;
Marshall Dawsona378c222019-03-04 16:52:07 -0700565 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800566}
567
Marshall Dawson8e0dca02019-02-27 18:40:49 -0700568static ssize_t copy_blob(void *dest, const char *src_file, size_t room)
569{
570 int fd;
571 struct stat fd_stat;
572 ssize_t bytes;
573
574 fd = open(src_file, O_RDONLY);
575 if (fd < 0) {
576 printf("Error: %s\n", strerror(errno));
577 return -1;
578 }
579
580 if (fstat(fd, &fd_stat)) {
581 printf("fstat error: %s\n", strerror(errno));
Jacob Garber967f8622019-07-02 10:35:10 -0600582 close(fd);
Marshall Dawson8e0dca02019-02-27 18:40:49 -0700583 return -2;
584 }
585
586 if (fd_stat.st_size > room) {
587 printf("Error: %s will not fit. Exiting.\n", src_file);
Jacob Garber967f8622019-07-02 10:35:10 -0600588 close(fd);
Marshall Dawson8e0dca02019-02-27 18:40:49 -0700589 return -3;
590 }
591
592 bytes = read(fd, dest, (size_t)fd_stat.st_size);
593 close(fd);
594 if (bytes != (ssize_t)fd_stat.st_size) {
595 printf("Error while reading %s\n", src_file);
596 return -4;
597 }
598
599 return bytes;
600}
601
Marshall Dawson2794a862019-03-04 16:53:15 -0700602static void integrate_firmwares(context *ctx,
Marshall Dawson239286c2019-02-23 16:42:46 -0700603 embedded_firmware *romsig,
Marshall Dawson2794a862019-03-04 16:53:15 -0700604 amd_fw_entry *fw_table)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800605{
Richard Spiegel137484d2018-01-17 10:23:19 -0700606 ssize_t bytes;
zbaoc3a08a92016-03-02 14:47:27 +0800607 int i;
Marshall Dawson2794a862019-03-04 16:53:15 -0700608
609 ctx->current += sizeof(embedded_firmware);
610 ctx->current = ALIGN(ctx->current, BLOB_ALIGNMENT);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800611
Martin Rothcd15bc82016-11-08 11:34:02 -0700612 for (i = 0; fw_table[i].type != AMD_FW_INVALID; i++) {
zbaoc3a08a92016-03-02 14:47:27 +0800613 if (fw_table[i].filename != NULL) {
zbaoc3a08a92016-03-02 14:47:27 +0800614 switch (fw_table[i].type) {
615 case AMD_FW_IMC:
Marshall Dawson2794a862019-03-04 16:53:15 -0700616 ctx->current = ALIGN(ctx->current, 0x10000U);
617 romsig->imc_entry = RUN_CURRENT(*ctx);
zbaoc3a08a92016-03-02 14:47:27 +0800618 break;
619 case AMD_FW_GEC:
Marshall Dawson2794a862019-03-04 16:53:15 -0700620 romsig->gec_entry = RUN_CURRENT(*ctx);
zbaoc3a08a92016-03-02 14:47:27 +0800621 break;
622 case AMD_FW_XHCI:
Marshall Dawson2794a862019-03-04 16:53:15 -0700623 romsig->xhci_entry = RUN_CURRENT(*ctx);
zbaoc3a08a92016-03-02 14:47:27 +0800624 break;
625 default:
626 /* Error */
627 break;
628 }
629
Marshall Dawson2794a862019-03-04 16:53:15 -0700630 bytes = copy_blob(BUFF_CURRENT(*ctx),
631 fw_table[i].filename, BUFF_ROOM(*ctx));
Marshall Dawson02bd7732019-03-13 14:43:17 -0600632 if (bytes < 0) {
Marshall Dawson2794a862019-03-04 16:53:15 -0700633 free(ctx->rom);
Martin Roth60f15512016-11-08 09:55:01 -0700634 exit(1);
635 }
636
Marshall Dawson2794a862019-03-04 16:53:15 -0700637 ctx->current = ALIGN(ctx->current + bytes,
638 BLOB_ALIGNMENT);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800639 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800640 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800641}
642
Marshall Dawson2794a862019-03-04 16:53:15 -0700643static void integrate_psp_firmwares(context *ctx,
Marshall Dawson239286c2019-02-23 16:42:46 -0700644 psp_directory_table *pspdir,
Marshall Dawson24f73d42019-04-01 10:48:43 -0600645 psp_directory_table *pspdir2,
646 amd_fw_entry *fw_table,
647 uint32_t cookie)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800648{
Richard Spiegel137484d2018-01-17 10:23:19 -0700649 ssize_t bytes;
Marshall Dawsonc38c0c92019-02-23 16:41:35 -0700650 unsigned int i, count;
Marshall Dawson24f73d42019-04-01 10:48:43 -0600651 int level;
652
653 /* This function can create a primary table, a secondary table, or a
654 * flattened table which contains all applicable types. These if-else
655 * statements infer what the caller intended. If a 2nd-level cookie
656 * is passed, clearly a 2nd-level table is intended. However, a
657 * 1st-level cookie may indicate level 1 or flattened. If the caller
658 * passes a pointer to a 2nd-level table, then assume not flat.
659 */
660 if (cookie == PSPL2_COOKIE)
661 level = PSP_LVL2;
662 else if (pspdir2)
663 level = PSP_LVL1;
664 else
665 level = PSP_BOTH;
Marshall Dawson2794a862019-03-04 16:53:15 -0700666
667 ctx->current = ALIGN(ctx->current, BLOB_ALIGNMENT);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800668
Marshall Dawsonc38c0c92019-02-23 16:41:35 -0700669 for (i = 0, count = 0; fw_table[i].type != AMD_FW_INVALID; i++) {
Marshall Dawson24f73d42019-04-01 10:48:43 -0600670 if (!(fw_table[i].level & level))
671 continue;
672
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600673 if (fw_table[i].type == AMD_TOKEN_UNLOCK) {
674 if (!fw_table[i].other)
675 continue;
676 ctx->current = ALIGN(ctx->current, ERASE_ALIGNMENT);
677 pspdir->entries[count].type = fw_table[i].type;
678 pspdir->entries[count].size = 4096; /* TODO: doc? */
679 pspdir->entries[count].addr = RUN_CURRENT(*ctx);
680 pspdir->entries[count].subprog = fw_table[i].subprog;
681 pspdir->entries[count].rsvd = 0;
682 ctx->current = ALIGN(ctx->current + 4096, 0x100U);
683 count++;
684 } else if (fw_table[i].type == AMD_PSP_FUSE_CHAIN) {
Marshall Dawson239286c2019-02-23 16:42:46 -0700685 pspdir->entries[count].type = fw_table[i].type;
Marshall Dawsondbae6322019-03-04 10:31:03 -0700686 pspdir->entries[count].subprog = fw_table[i].subprog;
687 pspdir->entries[count].rsvd = 0;
Marshall Dawson239286c2019-02-23 16:42:46 -0700688 pspdir->entries[count].size = 0xFFFFFFFF;
Marshall Dawsonef79fcc2019-04-01 10:16:41 -0600689 pspdir->entries[count].addr = fw_table[i].other;
Marshall Dawsonc38c0c92019-02-23 16:41:35 -0700690 count++;
Marshall Dawson7c1e1422019-04-11 09:44:43 -0600691 } else if (fw_table[i].type == AMD_FW_PSP_NVRAM) {
692 if (fw_table[i].filename == NULL)
693 continue;
694 /* TODO: Add a way to reserve for NVRAM without
695 * requiring a filename. This isn't a feature used
696 * by coreboot systems, so priority is very low.
697 */
698 ctx->current = ALIGN(ctx->current, ERASE_ALIGNMENT);
699 bytes = copy_blob(BUFF_CURRENT(*ctx),
700 fw_table[i].filename, BUFF_ROOM(*ctx));
701 if (bytes <= 0) {
702 free(ctx->rom);
703 exit(1);
704 }
705
706 pspdir->entries[count].type = fw_table[i].type;
707 pspdir->entries[count].subprog = fw_table[i].subprog;
708 pspdir->entries[count].rsvd = 0;
709 pspdir->entries[count].size = ALIGN(bytes,
710 ERASE_ALIGNMENT);
711 pspdir->entries[count].addr = RUN_CURRENT(*ctx);
712
713 ctx->current = ALIGN(ctx->current + bytes,
714 BLOB_ERASE_ALIGNMENT);
715 count++;
zbaoc3a08a92016-03-02 14:47:27 +0800716 } else if (fw_table[i].filename != NULL) {
Marshall Dawson2794a862019-03-04 16:53:15 -0700717 bytes = copy_blob(BUFF_CURRENT(*ctx),
718 fw_table[i].filename, BUFF_ROOM(*ctx));
Marshall Dawson02bd7732019-03-13 14:43:17 -0600719 if (bytes < 0) {
Marshall Dawson2794a862019-03-04 16:53:15 -0700720 free(ctx->rom);
Marshall Dawson8e0dca02019-02-27 18:40:49 -0700721 exit(1);
722 }
723
Marshall Dawson239286c2019-02-23 16:42:46 -0700724 pspdir->entries[count].type = fw_table[i].type;
Marshall Dawsondbae6322019-03-04 10:31:03 -0700725 pspdir->entries[count].subprog = fw_table[i].subprog;
726 pspdir->entries[count].rsvd = 0;
Marshall Dawson8e0dca02019-02-27 18:40:49 -0700727 pspdir->entries[count].size = (uint32_t)bytes;
Marshall Dawson2794a862019-03-04 16:53:15 -0700728 pspdir->entries[count].addr = RUN_CURRENT(*ctx);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800729
Marshall Dawson2794a862019-03-04 16:53:15 -0700730 ctx->current = ALIGN(ctx->current + bytes,
731 BLOB_ALIGNMENT);
Marshall Dawsonc38c0c92019-02-23 16:41:35 -0700732 count++;
zbaoc3a08a92016-03-02 14:47:27 +0800733 } else {
734 /* This APU doesn't have this firmware. */
735 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800736 }
Marshall Dawson2794a862019-03-04 16:53:15 -0700737
Marshall Dawson24f73d42019-04-01 10:48:43 -0600738 if (pspdir2) {
739 pspdir->entries[count].type = AMD_FW_L2_PTR;
740 pspdir->entries[count].subprog = 0;
741 pspdir->entries[count].rsvd = 0;
742 pspdir->entries[count].size = sizeof(pspdir2->header)
743 + pspdir2->header.num_entries
744 * sizeof(psp_directory_entry);
745
746 pspdir->entries[count].addr = BUFF_TO_RUN(*ctx, pspdir2);
747 count++;
748 }
749
Marshall Dawson2794a862019-03-04 16:53:15 -0700750 if (count > MAX_PSP_ENTRIES) {
751 printf("Error: PSP entries exceed max allowed items\n");
752 free(ctx->rom);
753 exit(1);
754 }
755
Marshall Dawson24f73d42019-04-01 10:48:43 -0600756 fill_dir_header(pspdir, count, cookie);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800757}
758
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600759static void *new_bios_dir(context *ctx, int multi)
760{
761 void *ptr;
762
763 /*
764 * Force both onto boundary when multi. Primary table is after
765 * updatable table, so alignment ensures primary can stay intact
766 * if secondary is reprogrammed.
767 */
768 if (multi)
769 ctx->current = ALIGN(ctx->current, TABLE_ERASE_ALIGNMENT);
770 else
771 ctx->current = ALIGN(ctx->current, TABLE_ALIGNMENT);
772 ptr = BUFF_CURRENT(*ctx);
773 ctx->current += sizeof(bios_directory_hdr)
774 + MAX_BIOS_ENTRIES * sizeof(bios_directory_entry);
775 return ptr;
776}
777
778static int locate_bdt2_bios(bios_directory_table *level2,
779 uint64_t *source, uint32_t *size)
780{
781 int i;
782
783 *source = 0;
784 *size = 0;
785 if (!level2)
786 return 0;
787
788 for (i = 0 ; i < level2->header.num_entries ; i++) {
789 if (level2->entries[i].type == AMD_BIOS_BIN) {
790 *source = level2->entries[i].source;
791 *size = level2->entries[i].size;
792 return 1;
793 }
794 }
795 return 0;
796}
797
798static int have_bios_tables(amd_bios_entry *table)
799{
800 int i;
801
802 for (i = 0 ; table[i].type != AMD_BIOS_INVALID; i++) {
803 if (table[i].level & BDT_LVL1 && table[i].filename)
804 return 1;
805 }
806 return 0;
807}
808
809static void integrate_bios_firmwares(context *ctx,
810 bios_directory_table *biosdir,
811 bios_directory_table *biosdir2,
812 amd_bios_entry *fw_table,
813 uint32_t cookie)
814{
815 ssize_t bytes;
Martin Rothec933132019-07-13 20:03:34 -0600816 unsigned int i, count;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600817 int level;
818
819 /* This function can create a primary table, a secondary table, or a
820 * flattened table which contains all applicable types. These if-else
821 * statements infer what the caller intended. If a 2nd-level cookie
822 * is passed, clearly a 2nd-level table is intended. However, a
823 * 1st-level cookie may indicate level 1 or flattened. If the caller
824 * passes a pointer to a 2nd-level table, then assume not flat.
825 */
826 if (cookie == BDT2_COOKIE)
827 level = BDT_LVL2;
828 else if (biosdir2)
829 level = BDT_LVL1;
830 else
831 level = BDT_BOTH;
832
833 ctx->current = ALIGN(ctx->current, BLOB_ALIGNMENT);
834
835 for (i = 0, count = 0; fw_table[i].type != AMD_BIOS_INVALID; i++) {
836 if (!(fw_table[i].level & level))
837 continue;
838 if (fw_table[i].filename == NULL && (
839 fw_table[i].type != AMD_BIOS_APOB &&
840 fw_table[i].type != AMD_BIOS_APOB_NV &&
841 fw_table[i].type != AMD_BIOS_L2_PTR &&
842 fw_table[i].type != AMD_BIOS_BIN))
843 continue;
844 /* APOB_NV needs a size, else no S3 and skip item */
845 if (fw_table[i].type == AMD_BIOS_APOB_NV && !fw_table[i].size)
846 continue;
847
848 /* BIOS Directory items may have additional requirements */
849
850 /* APOB_NV must have a size if it has a source */
851 if (fw_table[i].type == AMD_BIOS_APOB_NV && fw_table[i].src) {
852 if (!fw_table[i].size) {
853 printf("Error: APOB NV address provided, but no size\n");
854 free(ctx->rom);
855 exit(1);
856 }
857 }
858
859 /* APOB_DATA needs destination */
860 if (fw_table[i].type == AMD_BIOS_APOB && !fw_table[i].dest) {
861 printf("Error: APOB destination not provided\n");
862 free(ctx->rom);
863 exit(1);
864 }
865
866 /* BIOS binary must have destination and uncompressed size. If
867 * no filename given, then user must provide a source address.
868 */
869 if (fw_table[i].type == AMD_BIOS_BIN) {
870 if (!fw_table[i].dest || !fw_table[i].size) {
871 printf("Error: BIOS binary destination and uncompressed size are required\n");
872 free(ctx->rom);
873 exit(1);
874 }
875 if (!fw_table[i].filename && !fw_table[i].src) {
876 printf("Error: BIOS binary assumed outside amdfw.rom but no source address given\n");
877 free(ctx->rom);
878 exit(1);
879 }
880 }
881
882 biosdir->entries[count].type = fw_table[i].type;
883 biosdir->entries[count].region_type = fw_table[i].region_type;
884 biosdir->entries[count].dest = fw_table[i].dest ?
885 fw_table[i].dest : (uint64_t)-1;
886 biosdir->entries[count].reset = fw_table[i].reset;
887 biosdir->entries[count].copy = fw_table[i].copy;
888 biosdir->entries[count].ro = fw_table[i].ro;
889 biosdir->entries[count].compressed = fw_table[i].zlib;
890 biosdir->entries[count].inst = fw_table[i].inst;
891 biosdir->entries[count].subprog = fw_table[i].subpr;
892
893 switch (fw_table[i].type) {
894 case AMD_BIOS_APOB:
895 biosdir->entries[count].size = fw_table[i].size;
896 biosdir->entries[count].source = fw_table[i].src;
897 break;
898 case AMD_BIOS_APOB_NV:
899 if (fw_table[i].src) {
900 /* If source is given, use that and its size */
901 biosdir->entries[count].source = fw_table[i].src;
902 biosdir->entries[count].size = fw_table[i].size;
903 } else {
904 /* Else reserve size bytes within amdfw.rom */
905 ctx->current = ALIGN(ctx->current, ERASE_ALIGNMENT);
906 biosdir->entries[count].source = RUN_CURRENT(*ctx);
907 biosdir->entries[count].size = ALIGN(
908 fw_table[i].size, ERASE_ALIGNMENT);
909 memset(BUFF_CURRENT(*ctx), 0xff,
910 biosdir->entries[count].size);
911 ctx->current = ctx->current
912 + biosdir->entries[count].size;
913 }
914 break;
915 case AMD_BIOS_BIN:
916 /* Don't make a 2nd copy, point to the same one */
917 if (level == BDT_LVL1 && locate_bdt2_bios(biosdir2,
918 &biosdir->entries[count].source,
919 &biosdir->entries[count].size))
920 break;
921
922 /* level 2, or level 1 and no copy found in level 2 */
923 biosdir->entries[count].source = fw_table[i].src;
924 biosdir->entries[count].dest = fw_table[i].dest;
925 biosdir->entries[count].size = fw_table[i].size;
926
927 if (!fw_table[i].filename)
928 break;
929
930 bytes = copy_blob(BUFF_CURRENT(*ctx),
931 fw_table[i].filename, BUFF_ROOM(*ctx));
932 if (bytes <= 0) {
933 free(ctx->rom);
934 exit(1);
935 }
936
937 biosdir->entries[count].source = RUN_CURRENT(*ctx);
938
939 ctx->current = ALIGN(ctx->current + bytes, 0x100U);
940 break;
941 default: /* everything else is copied from input */
942 if (fw_table[i].type == AMD_BIOS_APCB ||
943 fw_table[i].type == AMD_BIOS_APCB_BK)
944 ctx->current = ALIGN(
945 ctx->current, ERASE_ALIGNMENT);
946
947 bytes = copy_blob(BUFF_CURRENT(*ctx),
948 fw_table[i].filename, BUFF_ROOM(*ctx));
949 if (bytes <= 0) {
950 free(ctx->rom);
951 exit(1);
952 }
953
954 biosdir->entries[count].size = (uint32_t)bytes;
955 biosdir->entries[count].source = RUN_CURRENT(*ctx);
956
957 ctx->current = ALIGN(ctx->current + bytes, 0x100U);
958 break;
959 }
960
961 count++;
962 }
963
964 if (biosdir2) {
965 biosdir->entries[count].type = AMD_BIOS_L2_PTR;
966 biosdir->entries[count].size =
967 + MAX_BIOS_ENTRIES
968 * sizeof(bios_directory_entry);
969 biosdir->entries[count].source =
970 BUFF_TO_RUN(*ctx, biosdir2);
971 biosdir->entries[count].subprog = 0;
972 biosdir->entries[count].inst = 0;
973 biosdir->entries[count].copy = 0;
974 biosdir->entries[count].compressed = 0;
975 biosdir->entries[count].dest = -1;
976 biosdir->entries[count].reset = 0;
977 biosdir->entries[count].ro = 0;
978 count++;
979 }
980
981 if (count > MAX_BIOS_ENTRIES) {
982 printf("Error: BIOS entries exceeds max allowed items\n");
983 free(ctx->rom);
984 exit(1);
985 }
986
987 fill_dir_header(biosdir, count, cookie);
988}
Martin Rothd3ce8c82019-07-13 20:13:07 -0600989// Unused values: CDEPqR
990static 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 -0600991
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800992static struct option long_options[] = {
Marshall Dawsonf4b9b412017-03-17 16:30:51 -0600993 {"xhci", required_argument, 0, 'x' },
994 {"imc", required_argument, 0, 'i' },
995 {"gec", required_argument, 0, 'g' },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -0600996 /* PSP Directory Table items */
Marshall Dawson67d868d2019-02-28 11:43:40 -0700997 {"combo-capable", no_argument, 0, 'A' },
Marshall Dawson24f73d42019-04-01 10:48:43 -0600998 {"multilevel", no_argument, 0, 'M' },
Marshall Dawsondbae6322019-03-04 10:31:03 -0700999 {"subprogram", required_argument, 0, 'S' },
Marshall Dawsonf4b9b412017-03-17 16:30:51 -06001000 {"pubkey", required_argument, 0, 'p' },
1001 {"bootloader", required_argument, 0, 'b' },
1002 {"smufirmware", required_argument, 0, 's' },
1003 {"recovery", required_argument, 0, 'r' },
1004 {"rtmpubkey", required_argument, 0, 'k' },
1005 {"secureos", required_argument, 0, 'c' },
1006 {"nvram", required_argument, 0, 'n' },
1007 {"securedebug", required_argument, 0, 'd' },
1008 {"trustlets", required_argument, 0, 't' },
1009 {"trustletkey", required_argument, 0, 'u' },
1010 {"smufirmware2", required_argument, 0, 'w' },
1011 {"smuscs", required_argument, 0, 'm' },
Marshall Dawsonef79fcc2019-04-01 10:16:41 -06001012 {"soft-fuse", required_argument, 0, 'T' },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001013 {"abl-image", required_argument, 0, 'z' },
1014 {"sec-gasket", required_argument, 0, 'J' },
1015 {"mp2-fw", required_argument, 0, 'B' },
1016 {"drv-entry-pts", required_argument, 0, 'K' },
1017 {"ikek", required_argument, 0, 'L' },
1018 {"s0i3drv", required_argument, 0, 'Y' },
1019 {"secdebug", required_argument, 0, 'N' },
1020 {"token-unlock", no_argument, 0, 'U' },
1021 {"whitelist", required_argument, 0, 'W' },
Martin Rothd3ce8c82019-07-13 20:13:07 -06001022 {"verstage", required_argument, 0, 'Z' },
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001023 /* BIOS Directory Table items */
1024 {"instance", required_argument, 0, 'I' },
1025 {"apcb", required_argument, 0, 'a' },
1026 {"apob-base", required_argument, 0, 'Q' },
1027 {"bios-bin", required_argument, 0, 'V' },
1028 {"bios-bin-src", required_argument, 0, 'e' },
1029 {"bios-bin-dest", required_argument, 0, 'v' },
1030 {"bios-uncomp-size", required_argument, 0, 'j' },
1031 {"pmu-inst", required_argument, 0, 'y' },
1032 {"pmu-data", required_argument, 0, 'G' },
1033 {"ucode", required_argument, 0, 'O' },
1034 {"mp2-config", required_argument, 0, 'X' },
1035 {"apob-nv-base", required_argument, 0, 'F' },
1036 {"apob-nv-size", required_argument, 0, 'H' },
1037 /* other */
Marshall Dawsonf4b9b412017-03-17 16:30:51 -06001038 {"output", required_argument, 0, 'o' },
1039 {"flashsize", required_argument, 0, 'f' },
Martin Roth0d3b1182017-10-03 14:16:04 -06001040 {"location", required_argument, 0, 'l' },
Marshall Dawsonf4b9b412017-03-17 16:30:51 -06001041 {"help", no_argument, 0, 'h' },
Marshall Dawsonf4b9b412017-03-17 16:30:51 -06001042 {NULL, 0, 0, 0 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001043};
1044
Marshall Dawsonef79fcc2019-04-01 10:16:41 -06001045static void register_fw_fuse(char *str)
1046{
1047 int i;
1048
1049 for (i = 0; i < sizeof(amd_psp_fw_table) / sizeof(amd_fw_entry); i++) {
1050 if (amd_psp_fw_table[i].type != AMD_PSP_FUSE_CHAIN)
1051 continue;
1052
1053 amd_psp_fw_table[i].other = strtoull(str, NULL, 16);
1054 return;
1055 }
1056}
1057
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001058static void register_fw_token_unlock(void)
1059{
1060 int i;
1061
1062 for (i = 0; i < sizeof(amd_psp_fw_table) / sizeof(amd_fw_entry); i++) {
1063 if (amd_psp_fw_table[i].type != AMD_TOKEN_UNLOCK)
1064 continue;
1065
1066 amd_psp_fw_table[i].other = 1;
1067 return;
1068 }
1069}
1070
Marshall Dawsondbae6322019-03-04 10:31:03 -07001071static void register_fw_filename(amd_fw_type type, uint8_t sub, char filename[])
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001072{
Martin Roth8806f7f2016-11-08 10:44:18 -07001073 unsigned int i;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001074
Martin Rothcd15bc82016-11-08 11:34:02 -07001075 for (i = 0; i < sizeof(amd_fw_table) / sizeof(amd_fw_entry); i++) {
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001076 if (amd_fw_table[i].type == type) {
1077 amd_fw_table[i].filename = filename;
1078 return;
1079 }
1080 }
1081
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001082 for (i = 0; i < sizeof(amd_psp_fw_table) / sizeof(amd_fw_entry); i++) {
Marshall Dawsondbae6322019-03-04 10:31:03 -07001083 if (amd_psp_fw_table[i].type != type)
1084 continue;
1085
1086 if (amd_psp_fw_table[i].subprog == sub) {
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001087 amd_psp_fw_table[i].filename = filename;
1088 return;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001089 }
1090 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001091}
1092
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001093static void register_bdt_data(amd_bios_type type, int sub, int ins, char name[])
1094{
1095 int i;
1096
1097 for (i = 0; i < sizeof(amd_bios_table) / sizeof(amd_bios_entry); i++) {
1098 if (amd_bios_table[i].type == type
1099 && amd_bios_table[i].inst == ins
1100 && amd_bios_table[i].subpr == sub) {
1101 amd_bios_table[i].filename = name;
1102 return;
1103 }
1104 }
1105}
1106
Martin Rothec933132019-07-13 20:03:34 -06001107static void register_fw_addr(amd_bios_type type, char *src_str,
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001108 char *dst_str, char *size_str)
1109{
1110 int i;
1111 for (i = 0; i < sizeof(amd_bios_table) / sizeof(amd_bios_entry); i++) {
1112 if (amd_bios_table[i].type != type)
1113 continue;
1114
1115 if (src_str)
1116 amd_bios_table[i].src = strtoull(src_str, NULL, 16);
1117 if (dst_str)
1118 amd_bios_table[i].dest = strtoull(dst_str, NULL, 16);
1119 if (size_str)
1120 amd_bios_table[i].size = strtoul(size_str, NULL, 16);
1121
1122 return;
1123 }
1124}
1125
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001126int main(int argc, char **argv)
1127{
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001128 int c;
Martin Roth31d95a22016-11-08 11:22:12 -07001129 int retval = 0;
Martin Roth60f15512016-11-08 09:55:01 -07001130 char *tmp;
Martin Roth8806f7f2016-11-08 10:44:18 -07001131 char *rom = NULL;
Marshall Dawson239286c2019-02-23 16:42:46 -07001132 embedded_firmware *amd_romsig;
1133 psp_directory_table *pspdir;
Marshall Dawson67d868d2019-02-28 11:43:40 -07001134 int comboable = 0;
Marshall Dawsonef79fcc2019-04-01 10:16:41 -06001135 int fuse_defined = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001136 int targetfd;
Martin Roth8806f7f2016-11-08 10:44:18 -07001137 char *output = NULL;
Marshall Dawson2794a862019-03-04 16:53:15 -07001138 context ctx = {
1139 .rom_size = CONFIG_ROM_SIZE,
1140 };
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001141 /* Values cleared after each firmware or parameter, regardless if N/A */
1142 uint8_t sub = 0, instance = 0;
1143 int abl_image = 0;
Martin Roth0d3b1182017-10-03 14:16:04 -06001144 uint32_t dir_location = 0;
1145 uint32_t romsig_offset;
Martin Roth60f15512016-11-08 09:55:01 -07001146 uint32_t rom_base_address;
Marshall Dawson24f73d42019-04-01 10:48:43 -06001147 int multi = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001148
1149 while (1) {
1150 int optindex = 0;
1151
1152 c = getopt_long(argc, argv, optstring, long_options, &optindex);
1153
1154 if (c == -1)
1155 break;
1156
1157 switch (c) {
1158 case 'x':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001159 register_fw_filename(AMD_FW_XHCI, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001160 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001161 break;
1162 case 'i':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001163 register_fw_filename(AMD_FW_IMC, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001164 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001165 break;
1166 case 'g':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001167 register_fw_filename(AMD_FW_GEC, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001168 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001169 break;
Marshall Dawson67d868d2019-02-28 11:43:40 -07001170 case 'A':
1171 comboable = 1;
1172 break;
Marshall Dawson24f73d42019-04-01 10:48:43 -06001173 case 'M':
1174 multi = 1;
1175 break;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001176 case 'U':
1177 register_fw_token_unlock();
1178 sub = instance = 0;
1179 break;
Marshall Dawsondbae6322019-03-04 10:31:03 -07001180 case 'S':
1181 sub = (uint8_t)strtoul(optarg, &tmp, 16);
1182 break;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001183 case 'I':
1184 instance = strtoul(optarg, &tmp, 16);
1185 break;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001186 case 'p':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001187 register_fw_filename(AMD_FW_PSP_PUBKEY, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001188 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001189 break;
1190 case 'b':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001191 register_fw_filename(AMD_FW_PSP_BOOTLOADER,
1192 sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001193 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001194 break;
1195 case 's':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001196 register_fw_filename(AMD_FW_PSP_SMU_FIRMWARE,
1197 sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001198 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001199 break;
1200 case 'r':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001201 register_fw_filename(AMD_FW_PSP_RECOVERY, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001202 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001203 break;
1204 case 'k':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001205 register_fw_filename(AMD_FW_PSP_RTM_PUBKEY,
1206 sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001207 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001208 break;
1209 case 'c':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001210 register_fw_filename(AMD_FW_PSP_SECURED_OS,
1211 sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001212 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001213 break;
1214 case 'n':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001215 register_fw_filename(AMD_FW_PSP_NVRAM, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001216 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001217 break;
1218 case 'd':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001219 register_fw_filename(AMD_FW_PSP_SECURED_DEBUG,
1220 sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001221 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001222 break;
1223 case 't':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001224 register_fw_filename(AMD_FW_PSP_TRUSTLETS, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001225 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001226 break;
1227 case 'u':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001228 register_fw_filename(AMD_FW_PSP_TRUSTLETKEY,
1229 sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001230 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001231 break;
1232 case 'w':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001233 register_fw_filename(AMD_FW_PSP_SMU_FIRMWARE2,
1234 sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001235 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001236 break;
1237 case 'm':
Marshall Dawsondbae6322019-03-04 10:31:03 -07001238 register_fw_filename(AMD_FW_PSP_SMUSCS, sub, optarg);
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001239 sub = instance = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001240 break;
Marshall Dawsonef79fcc2019-04-01 10:16:41 -06001241 case 'T':
1242 register_fw_fuse(optarg);
1243 fuse_defined = 1;
1244 sub = 0;
1245 break;
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001246 case 'a':
1247 register_bdt_data(AMD_BIOS_APCB, sub, instance, optarg);
1248 register_bdt_data(AMD_BIOS_APCB_BK, sub,
1249 instance, optarg);
1250 sub = instance = 0;
1251 break;
1252 case 'Q':
1253 /* APOB destination */
1254 register_fw_addr(AMD_BIOS_APOB, 0, optarg, 0);
1255 sub = instance = 0;
1256 break;
1257 case 'F':
1258 /* APOB NV source */
1259 register_fw_addr(AMD_BIOS_APOB_NV, optarg, 0, 0);
1260 sub = instance = 0;
1261 break;
1262 case 'H':
1263 /* APOB NV size */
1264 register_fw_addr(AMD_BIOS_APOB_NV, 0, 0, optarg);
1265 sub = instance = 0;
1266 break;
1267 case 'V':
1268 register_bdt_data(AMD_BIOS_BIN, sub, instance, optarg);
1269 sub = instance = 0;
1270 break;
1271 case 'e':
1272 /* BIOS source */
1273 register_fw_addr(AMD_BIOS_BIN, optarg, 0, 0);
1274 sub = instance = 0;
1275 break;
1276 case 'v':
1277 /* BIOS destination */
1278 register_fw_addr(AMD_BIOS_BIN, 0, optarg, 0);
1279 sub = instance = 0;
1280 break;
1281 case 'j':
1282 /* BIOS destination size */
1283 register_fw_addr(AMD_BIOS_BIN, 0, 0, optarg);
1284 sub = instance = 0;
1285 break;
1286 case 'y':
1287 register_bdt_data(AMD_BIOS_PMUI, sub, instance, optarg);
1288 sub = instance = 0;
1289 break;
1290 case 'G':
1291 register_bdt_data(AMD_BIOS_PMUD, sub, instance, optarg);
1292 sub = instance = 0;
1293 break;
1294 case 'O':
1295 register_bdt_data(AMD_BIOS_UCODE, sub,
1296 instance, optarg);
1297 sub = instance = 0;
1298 break;
1299 case 'J':
1300 register_fw_filename(AMD_SEC_GASKET, sub, optarg);
1301 sub = instance = 0;
1302 break;
1303 case 'B':
1304 register_fw_filename(AMD_MP2_FW, sub, optarg);
1305 sub = instance = 0;
1306 break;
1307 case 'z':
1308 register_fw_filename(AMD_ABL0 + abl_image++,
1309 sub, optarg);
1310 sub = instance = 0;
1311 break;
1312 case 'X':
1313 register_bdt_data(AMD_BIOS_MP2_CFG, sub,
1314 instance, optarg);
1315 sub = instance = 0;
1316 break;
1317 case 'K':
1318 register_fw_filename(AMD_DRIVER_ENTRIES, sub, optarg);
1319 sub = instance = 0;
1320 break;
1321 case 'L':
1322 register_fw_filename(AMD_WRAPPED_IKEK, sub, optarg);
1323 sub = instance = 0;
1324 break;
1325 case 'Y':
1326 register_fw_filename(AMD_S0I3_DRIVER, sub, optarg);
1327 sub = instance = 0;
1328 break;
1329 case 'N':
1330 register_fw_filename(AMD_DEBUG_UNLOCK, sub, optarg);
1331 sub = instance = 0;
1332 break;
1333 case 'W':
1334 register_fw_filename(AMD_FW_PSP_WHITELIST, sub, optarg);
1335 sub = instance = 0;
1336 break;
Martin Rothd3ce8c82019-07-13 20:13:07 -06001337 case 'Z':
1338 register_fw_filename(AMD_FW_PSP_VERSTAGE, sub, optarg);
1339 sub = instance = 0;
1340 break;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001341 case 'o':
1342 output = optarg;
1343 break;
Martin Roth60f15512016-11-08 09:55:01 -07001344 case 'f':
Marshall Dawson2794a862019-03-04 16:53:15 -07001345 ctx.rom_size = (uint32_t)strtoul(optarg, &tmp, 16);
Martin Roth60f15512016-11-08 09:55:01 -07001346 if (*tmp != '\0') {
1347 printf("Error: ROM size specified"
1348 " incorrectly (%s)\n\n", optarg);
Martin Roth31d95a22016-11-08 11:22:12 -07001349 retval = 1;
Martin Roth60f15512016-11-08 09:55:01 -07001350 }
1351 break;
Martin Roth0d3b1182017-10-03 14:16:04 -06001352 case 'l':
1353 dir_location = (uint32_t)strtoul(optarg, &tmp, 16);
1354 if (*tmp != '\0') {
1355 printf("Error: Directory Location specified"
1356 " incorrectly (%s)\n\n", optarg);
1357 retval = 1;
1358 }
1359 break;
1360
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001361 case 'h':
1362 usage();
Martin Roth31d95a22016-11-08 11:22:12 -07001363 return 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001364 default:
1365 break;
1366 }
1367 }
1368
Marshall Dawsonef79fcc2019-04-01 10:16:41 -06001369 if (!fuse_defined)
1370 register_fw_fuse(DEFAULT_SOFT_FUSE_CHAIN);
1371
Martin Roth8806f7f2016-11-08 10:44:18 -07001372 if (!output) {
Martin Roth31d95a22016-11-08 11:22:12 -07001373 printf("Error: Output value is not specified.\n\n");
1374 retval = 1;
1375 }
1376
Marshall Dawson2794a862019-03-04 16:53:15 -07001377 if (ctx.rom_size % 1024 != 0) {
Martin Roth60f15512016-11-08 09:55:01 -07001378 printf("Error: ROM Size (%d bytes) should be a multiple of"
Marshall Dawson2794a862019-03-04 16:53:15 -07001379 " 1024 bytes.\n\n", ctx.rom_size);
Martin Roth31d95a22016-11-08 11:22:12 -07001380 retval = 1;
Martin Roth60f15512016-11-08 09:55:01 -07001381 }
1382
Marshall Dawson2794a862019-03-04 16:53:15 -07001383 if (ctx.rom_size < MIN_ROM_KB * 1024) {
Martin Roth31d95a22016-11-08 11:22:12 -07001384 printf("Error: ROM Size (%dKB) must be at least %dKB.\n\n",
Marshall Dawson2794a862019-03-04 16:53:15 -07001385 ctx.rom_size / 1024, MIN_ROM_KB);
Martin Roth31d95a22016-11-08 11:22:12 -07001386 retval = 1;
1387 }
1388
1389 if (retval) {
1390 usage();
1391 return retval;
Martin Roth60f15512016-11-08 09:55:01 -07001392 }
1393
Marshall Dawson2794a862019-03-04 16:53:15 -07001394 printf(" AMDFWTOOL Using ROM size of %dKB\n", ctx.rom_size / 1024);
Martin Roth60f15512016-11-08 09:55:01 -07001395
Marshall Dawson2794a862019-03-04 16:53:15 -07001396 rom_base_address = 0xFFFFFFFF - ctx.rom_size + 1;
Martin Roth0d3b1182017-10-03 14:16:04 -06001397 if (dir_location && (dir_location < rom_base_address)) {
1398 printf("Error: Directory location outside of ROM.\n\n");
1399 return 1;
1400 }
1401
1402 switch (dir_location) {
1403 case 0: /* Fall through */
1404 case 0xFFFA0000: /* Fall through */
1405 case 0xFFF20000: /* Fall through */
1406 case 0xFFE20000: /* Fall through */
1407 case 0xFFC20000: /* Fall through */
1408 case 0xFF820000: /* Fall through */
1409 case 0xFF020000: /* Fall through */
1410 break;
1411 default:
1412 printf("Error: Invalid Directory location.\n");
1413 printf(" Valid locations are 0xFFFA0000, 0xFFF20000,\n");
1414 printf(" 0xFFE20000, 0xFFC20000, 0xFF820000, 0xFF020000\n");
1415 return 1;
1416 }
1417
Marshall Dawson2794a862019-03-04 16:53:15 -07001418 ctx.rom = malloc(ctx.rom_size);
1419 if (!ctx.rom) {
1420 printf("Error: Failed to allocate memory\n");
Martin Roth31d95a22016-11-08 11:22:12 -07001421 return 1;
Marshall Dawson2794a862019-03-04 16:53:15 -07001422 }
1423 memset(ctx.rom, 0xFF, ctx.rom_size);
Martin Roth60f15512016-11-08 09:55:01 -07001424
Martin Roth0d3b1182017-10-03 14:16:04 -06001425 if (dir_location)
Marshall Dawson2794a862019-03-04 16:53:15 -07001426 romsig_offset = ctx.current = dir_location - rom_base_address;
Martin Roth0d3b1182017-10-03 14:16:04 -06001427 else
Marshall Dawson2794a862019-03-04 16:53:15 -07001428 romsig_offset = ctx.current = AMD_ROMSIG_OFFSET;
1429 printf(" AMDFWTOOL Using firmware directory location of 0x%08x\n",
1430 RUN_CURRENT(ctx));
Martin Roth0d3b1182017-10-03 14:16:04 -06001431
Marshall Dawson2794a862019-03-04 16:53:15 -07001432 amd_romsig = BUFF_OFFSET(ctx, romsig_offset);
Marshall Dawson239286c2019-02-23 16:42:46 -07001433 amd_romsig->signature = EMBEDDED_FW_SIGNATURE;
1434 amd_romsig->imc_entry = 0;
1435 amd_romsig->gec_entry = 0;
1436 amd_romsig->xhci_entry = 0;
Martin Roth60f15512016-11-08 09:55:01 -07001437
Marshall Dawson2794a862019-03-04 16:53:15 -07001438 integrate_firmwares(&ctx, amd_romsig, amd_fw_table);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001439
Marshall Dawson2794a862019-03-04 16:53:15 -07001440 ctx.current = ALIGN(ctx.current, 0x10000U); /* todo: is necessary? */
1441
Marshall Dawson24f73d42019-04-01 10:48:43 -06001442 if (multi) {
1443 /* Do 2nd PSP directory followed by 1st */
1444 psp_directory_table *pspdir2 = new_psp_dir(&ctx, multi);
1445 integrate_psp_firmwares(&ctx, pspdir2, 0,
1446 amd_psp_fw_table, PSPL2_COOKIE);
1447
1448 pspdir = new_psp_dir(&ctx, multi);
1449 integrate_psp_firmwares(&ctx, pspdir, pspdir2,
1450 amd_psp_fw_table, PSP_COOKIE);
1451 } else {
1452 /* flat: PSP 1 cookie and no pointer to 2nd table */
1453 pspdir = new_psp_dir(&ctx, multi);
1454 integrate_psp_firmwares(&ctx, pspdir, 0,
1455 amd_psp_fw_table, PSP_COOKIE);
1456 }
Marshall Dawson2794a862019-03-04 16:53:15 -07001457
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001458 if (comboable)
Marshall Dawson2794a862019-03-04 16:53:15 -07001459 amd_romsig->comboable = BUFF_TO_RUN(ctx, pspdir);
Marshall Dawson67d868d2019-02-28 11:43:40 -07001460 else
Marshall Dawson2794a862019-03-04 16:53:15 -07001461 amd_romsig->psp_entry = BUFF_TO_RUN(ctx, pspdir);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001462
zbaoc3a08a92016-03-02 14:47:27 +08001463#if PSP_COMBO
Marshall Dawson2794a862019-03-04 16:53:15 -07001464 psp_combo_directory *combo_dir = new_combo_dir(&ctx);
1465 amd_romsig->comboable = BUFF_TO_RUN(ctx, combo_dir);
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001466 /* 0 -Compare PSP ID, 1 -Compare chip family ID */
1467 combo_dir->entries[0].id_sel = 0;
1468 /* TODO: PSP ID. Documentation is needed. */
1469 combo_dir->entries[0].id = 0x10220B00;
Marshall Dawson2794a862019-03-04 16:53:15 -07001470 combo_dir->entries[0].lvl2_addr = BUFF_TO_RUN(ctx, pspdir);
Zheng Bao4fcc9f22015-11-20 12:29:04 +08001471
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001472 combo_dir->header.lookup = 1;
Marshall Dawsona378c222019-03-04 16:52:07 -07001473 fill_dir_header(combo_dir, 1, PSP2_COOKIE);
Marshall Dawson0e02ce82019-03-04 16:50:37 -07001474#endif
Zheng Bao4fcc9f22015-11-20 12:29:04 +08001475
Marshall Dawsonce2b2ba2019-03-19 14:45:31 -06001476 if (have_bios_tables(amd_bios_table)) {
1477 bios_directory_table *biosdir;
1478 if (multi) {
1479 /* Do 2nd level BIOS directory followed by 1st */
1480 bios_directory_table *biosdir2 =
1481 new_bios_dir(&ctx, multi);
1482 integrate_bios_firmwares(&ctx, biosdir2, 0,
1483 amd_bios_table, BDT2_COOKIE);
1484
1485 biosdir = new_bios_dir(&ctx, multi);
1486 integrate_bios_firmwares(&ctx, biosdir, biosdir2,
1487 amd_bios_table, BDT1_COOKIE);
1488 } else {
1489 /* flat: BDT1 cookie and no pointer to 2nd table */
1490 biosdir = new_bios_dir(&ctx, multi);
1491 integrate_bios_firmwares(&ctx, biosdir, 0,
1492 amd_bios_table, BDT1_COOKIE);
1493 }
1494 amd_romsig->bios1_entry = BUFF_TO_RUN(ctx, biosdir);
1495 }
1496
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001497 targetfd = open(output, O_RDWR | O_CREAT | O_TRUNC, 0666);
Martin Roth31d95a22016-11-08 11:22:12 -07001498 if (targetfd >= 0) {
Marshall Dawson2794a862019-03-04 16:53:15 -07001499 write(targetfd, amd_romsig, ctx.current - romsig_offset);
Martin Roth31d95a22016-11-08 11:22:12 -07001500 close(targetfd);
1501 } else {
1502 printf("Error: could not open file: %s\n", output);
1503 retval = 1;
1504 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001505
Martin Roth31d95a22016-11-08 11:22:12 -07001506 free(rom);
1507 return retval;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001508}