Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * ROMSIG At ROMBASE + 0x20000: |
zbao | c3b0b72 | 2016-02-19 13:47:31 +0800 | [diff] [blame] | 5 | * 0 4 8 C |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 6 | * +------------+---------------+----------------+------------+ |
| 7 | * | 0x55AA55AA |EC ROM Address |GEC ROM Address |USB3 ROM | |
| 8 | * +------------+---------------+----------------+------------+ |
zbao | c3b0b72 | 2016-02-19 13:47:31 +0800 | [diff] [blame] | 9 | * | PSPDIR ADDR|PSPDIR ADDR |<-- Field 0x14 could be either |
| 10 | * +------------+---------------+ 2nd PSP directory or PSP COMBO directory |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 11 | * EC ROM should be 64K aligned. |
| 12 | * |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 13 | * PSP directory (Where "PSPDIR ADDR" points) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 14 | * +------------+---------------+----------------+------------+ |
| 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 Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 31 | * |
zbao | c3b0b72 | 2016-02-19 13:47:31 +0800 | [diff] [blame] | 32 | * PSP Combo directory |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 33 | * +------------+---------------+----------------+------------+ |
zbao | 6e2f3d1 | 2016-02-19 13:34:59 +0800 | [diff] [blame] | 34 | * | 'PSP2' | Fletcher | Count |Look up mode| |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 35 | * +------------+---------------+----------------+------------+ |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 36 | * | R e s e r v e d | |
| 37 | * +------------+---------------+----------------+------------+ |
zbao | 6e2f3d1 | 2016-02-19 13:34:59 +0800 | [diff] [blame] | 38 | * | ID-Sel | PSP ID | PSPDIR ADDR | | 2nd PSP directory |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 39 | * +------------+---------------+----------------+------------+ |
zbao | 6e2f3d1 | 2016-02-19 13:34:59 +0800 | [diff] [blame] | 40 | * | ID-Sel | PSP ID | PSPDIR ADDR | | 3rd PSP directory |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 41 | * +------------+---------------+----------------+------------+ |
| 42 | * | | |
| 43 | * | Other PSP | |
| 44 | * | | |
| 45 | * +------------+---------------+----------------+------------+ |
| 46 | * |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 47 | */ |
| 48 | |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 49 | #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 Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 63 | #define AMD_ROMSIG_OFFSET 0x20000 |
| 64 | #define MIN_ROM_KB 256 |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 65 | |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 66 | #define ALIGN(val, by) (((val) + (by) - 1) & ~((by) - 1)) |
Marshall Dawson | 7c1e142 | 2019-04-11 09:44:43 -0600 | [diff] [blame] | 67 | #define _MAX(A, B) (((A) > (B)) ? (A) : (B)) |
| 68 | #define ERASE_ALIGNMENT 0x1000U |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 69 | #define TABLE_ALIGNMENT 0x1000U |
| 70 | #define BLOB_ALIGNMENT 0x100U |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 71 | #define TABLE_ERASE_ALIGNMENT _MAX(TABLE_ALIGNMENT, ERASE_ALIGNMENT) |
Marshall Dawson | 7c1e142 | 2019-04-11 09:44:43 -0600 | [diff] [blame] | 72 | #define BLOB_ERASE_ALIGNMENT _MAX(BLOB_ALIGNMENT, ERASE_ALIGNMENT) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 73 | |
Marshall Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 74 | #define DEFAULT_SOFT_FUSE_CHAIN "0x1" |
| 75 | |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 76 | #define EMBEDDED_FW_SIGNATURE 0x55aa55aa |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 77 | #define PSP_COOKIE 0x50535024 /* 'PSP$' */ |
| 78 | #define PSPL2_COOKIE 0x324c5024 /* '2LP$' */ |
| 79 | #define PSP2_COOKIE 0x50535032 /* 'PSP2' */ |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 80 | #define BDT1_COOKIE 0x44484224 /* 'DHB$ */ |
| 81 | #define BDT2_COOKIE 0x324c4224 /* '2LB$ */ |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 82 | |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 83 | /* |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 84 | * 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. |
zbao | c3b0b72 | 2016-02-19 13:47:31 +0800 | [diff] [blame] | 97 | */ |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 98 | #define PSP_COMBO 0 |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 99 | |
Idwer Vollering | a682fc8 | 2019-12-16 15:33:12 +0100 | [diff] [blame] | 100 | #if defined(__GLIBC__) |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 101 | typedef unsigned long long int uint64_t; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 102 | typedef unsigned int uint32_t; |
| 103 | typedef unsigned char uint8_t; |
| 104 | typedef unsigned short uint16_t; |
Idwer Vollering | a682fc8 | 2019-12-16 15:33:12 +0100 | [diff] [blame] | 105 | #endif |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 106 | |
| 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 Dawson | 8a45a4d | 2019-02-24 07:18:44 -0700 | [diff] [blame] | 127 | static uint32_t fletcher32(const void *data, int length) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 128 | { |
| 129 | uint32_t c0; |
| 130 | uint32_t c1; |
| 131 | uint32_t checksum; |
| 132 | int index; |
Marshall Dawson | 8a45a4d | 2019-02-24 07:18:44 -0700 | [diff] [blame] | 133 | const uint16_t *pptr = data; |
| 134 | |
| 135 | length /= 2; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 136 | |
| 137 | c0 = 0xFFFF; |
| 138 | c1 = 0xFFFF; |
| 139 | |
Marshall Dawson | b85ddc5 | 2019-07-23 07:24:30 -0600 | [diff] [blame] | 140 | while (length) { |
| 141 | index = length >= 359 ? 359 : length; |
| 142 | length -= index; |
| 143 | do { |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 144 | c0 += *(pptr++); |
| 145 | c1 += c0; |
Marshall Dawson | b85ddc5 | 2019-07-23 07:24:30 -0600 | [diff] [blame] | 146 | } while (--index); |
| 147 | c0 = (c0 & 0xFFFF) + (c0 >> 16); |
| 148 | c1 = (c1 & 0xFFFF) + (c1 >> 16); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 149 | } |
| 150 | |
Marshall Dawson | 8a45a4d | 2019-02-24 07:18:44 -0700 | [diff] [blame] | 151 | /* Sums[0,1] mod 64K + overflow */ |
| 152 | c0 = (c0 & 0xFFFF) + (c0 >> 16); |
| 153 | c1 = (c1 & 0xFFFF) + (c1 >> 16); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 154 | checksum = (c1 << 16) | c0; |
| 155 | |
| 156 | return checksum; |
| 157 | } |
| 158 | |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 159 | static void usage(void) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 160 | { |
Martin Roth | 0e94062 | 2016-11-08 10:37:53 -0700 | [diff] [blame] | 161 | printf("amdfwtool: Create AMD Firmware combination\n"); |
| 162 | printf("Usage: amdfwtool [options] -f <size> -o <filename>\n"); |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 163 | 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 Roth | 0e94062 | 2016-11-08 10:37:53 -0700 | [diff] [blame] | 166 | |
| 167 | printf("\nPSP options:\n"); |
Marshall Dawson | 67d868d | 2019-02-28 11:43:40 -0700 | [diff] [blame] | 168 | printf("-A | --combo-capable Place PSP directory pointer at Embedded Firmware\n"); |
| 169 | printf(" offset able to support combo directory\n"); |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 170 | printf("-M | --multilevel Generate primary and secondary tables\n"); |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 171 | printf("-p | --pubkey <FILE> Add pubkey\n"); |
| 172 | printf("-b | --bootloader <FILE> Add bootloader\n"); |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 173 | printf("-S | --subprogram <number> Sets subprogram field for the next firmware\n"); |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 174 | 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 Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 184 | printf("-T | --soft-fuse <HEX_VAL> Override default soft fuse values\n"); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 185 | 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 Roth | d3ce8c8 | 2019-07-13 20:13:07 -0600 | [diff] [blame] | 193 | printf("-Z | --verstage <FILE> Add verstage\n"); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 194 | 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 Roth | ec93313 | 2019-07-13 20:03:34 -0600 | [diff] [blame] | 202 | printf("-O | --ucode <FILE> Add microcode patch\n"); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 203 | 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 Roth | 0e94062 | 2016-11-08 10:37:53 -0700 | [diff] [blame] | 208 | printf("\n-o | --output <filename> output filename\n"); |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 209 | printf("-f | --flashsize <HEX_VAL> ROM size in bytes\n"); |
| 210 | printf(" size must be larger than %dKB\n", |
Martin Roth | 0e94062 | 2016-11-08 10:37:53 -0700 | [diff] [blame] | 211 | MIN_ROM_KB); |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 212 | printf(" and must a multiple of 1024\n"); |
Martin Roth | 0d3b118 | 2017-10-03 14:16:04 -0600 | [diff] [blame] | 213 | printf("-l | --location Location of Directory\n"); |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 214 | printf("-h | --help show this help\n"); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 215 | } |
| 216 | |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 217 | typedef 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) |
| 234 | typedef 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 Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 250 | typedef 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 Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 264 | 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 Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 280 | AMD_FW_L2_PTR = 0x40, |
Martin Roth | d3ce8c8 | 2019-07-13 20:13:07 -0600 | [diff] [blame] | 281 | AMD_FW_PSP_VERSTAGE = 0x52, |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 282 | AMD_FW_IMC, |
| 283 | AMD_FW_GEC, |
| 284 | AMD_FW_XHCI, |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 285 | AMD_FW_INVALID, |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 286 | } amd_fw_type; |
| 287 | |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 288 | #define PSP_LVL1 0x1 |
| 289 | #define PSP_LVL2 0x2 |
| 290 | #define PSP_BOTH (PSP_LVL1 | PSP_LVL2) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 291 | typedef struct _amd_fw_entry { |
| 292 | amd_fw_type type; |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 293 | uint8_t subprog; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 294 | char *filename; |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 295 | int level; |
Marshall Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 296 | uint64_t other; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 297 | } amd_fw_entry; |
| 298 | |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 299 | static amd_fw_entry amd_psp_fw_table[] = { |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 300 | { .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 Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 317 | { .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 Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 334 | { .type = AMD_FW_PSP_SMU_FIRMWARE, .subprog = 1, .level = PSP_BOTH }, |
| 335 | { .type = AMD_FW_PSP_SMU_FIRMWARE2, .subprog = 1, .level = PSP_BOTH }, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 336 | { .type = AMD_FW_PSP_WHITELIST, .level = PSP_LVL2 }, |
Martin Roth | d3ce8c8 | 2019-07-13 20:13:07 -0600 | [diff] [blame] | 337 | { .type = AMD_FW_PSP_VERSTAGE, .level = PSP_BOTH }, |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 338 | { .type = AMD_FW_INVALID }, |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 339 | }; |
| 340 | |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 341 | static amd_fw_entry amd_fw_table[] = { |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 342 | { .type = AMD_FW_XHCI }, |
| 343 | { .type = AMD_FW_IMC }, |
| 344 | { .type = AMD_FW_GEC }, |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 345 | { .type = AMD_FW_INVALID }, |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 346 | }; |
| 347 | |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 348 | static amd_bios_entry amd_bios_table[] = { |
Marshall Dawson | 0581bf6 | 2019-09-25 11:03:53 -0600 | [diff] [blame] | 349 | { .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 Barnes | 18fd26c | 2020-03-03 10:35:02 -0700 | [diff] [blame] | 354 | { .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 Dawson | 2dd3b5c | 2020-01-03 17:57:48 -0700 | [diff] [blame] | 365 | { .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 Barnes | 18fd26c | 2020-03-03 10:35:02 -0700 | [diff] [blame] | 370 | { .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 Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 381 | { .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 Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 400 | typedef 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 Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 407 | uint32_t bios0_entry; /* todo: add way to select correct entry */ |
| 408 | uint32_t bios1_entry; |
Marshall Dawson | 94f2492 | 2019-09-28 08:49:09 -0600 | [diff] [blame] | 409 | uint32_t bios2_entry; |
| 410 | uint32_t reserved[0x2c]; /* 0x24 - 0x4f */ |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 411 | } __attribute__((packed, aligned(16))) embedded_firmware; |
| 412 | |
| 413 | typedef 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 | |
| 420 | typedef struct _psp_directory_entry { |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 421 | uint8_t type; |
| 422 | uint8_t subprog; |
| 423 | uint16_t rsvd; |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 424 | uint32_t size; |
| 425 | uint64_t addr; /* or a value in some cases */ |
| 426 | } __attribute__((packed)) psp_directory_entry; |
| 427 | |
| 428 | typedef struct _psp_directory_table { |
| 429 | psp_directory_header header; |
| 430 | psp_directory_entry entries[]; |
| 431 | } __attribute__((packed)) psp_directory_table; |
| 432 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 433 | #define MAX_PSP_ENTRIES 0x1f |
| 434 | |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 435 | typedef 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 | |
| 443 | typedef 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 | |
| 449 | typedef struct _psp_combo_directory { |
| 450 | psp_combo_header header; |
| 451 | psp_combo_entry entries[]; |
| 452 | } __attribute__((packed)) psp_combo_directory; |
| 453 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 454 | #define MAX_COMBO_ENTRIES 1 |
| 455 | |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 456 | typedef 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 | |
| 463 | typedef 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 | |
| 477 | typedef struct _bios_directory_table { |
| 478 | bios_directory_hdr header; |
| 479 | bios_directory_entry entries[]; |
| 480 | } bios_directory_table; |
| 481 | |
Rob Barnes | 18fd26c | 2020-03-03 10:35:02 -0700 | [diff] [blame] | 482 | #define MAX_BIOS_ENTRIES 0x22 |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 483 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 484 | typedef 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 Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 498 | static void *new_psp_dir(context *ctx, int multi) |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 499 | { |
| 500 | void *ptr; |
| 501 | |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 502 | /* |
| 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 Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 512 | 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 Roth | ec93313 | 2019-07-13 20:03:34 -0600 | [diff] [blame] | 518 | #if PSP_COMBO |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 519 | static 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 Roth | ec93313 | 2019-07-13 20:03:34 -0600 | [diff] [blame] | 529 | #endif |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 530 | |
Marshall Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 531 | static void fill_dir_header(void *directory, uint32_t count, uint32_t cookie) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 532 | { |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 533 | psp_combo_directory *cdir = directory; |
| 534 | psp_directory_table *dir = directory; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 535 | bios_directory_table *bdir = directory; |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 536 | |
| 537 | if (!count) |
| 538 | return; |
| 539 | |
| 540 | switch (cookie) { |
| 541 | case PSP2_COOKIE: |
Marshall Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 542 | /* caller is responsible for lookup mode */ |
Marshall Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 543 | 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 Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 553 | break; |
| 554 | case PSP_COOKIE: |
| 555 | case PSPL2_COOKIE: |
Marshall Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 556 | 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 Dawson | 8a45a4d | 2019-02-24 07:18:44 -0700 | [diff] [blame] | 561 | count * sizeof(psp_directory_entry) |
Marshall Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 562 | + sizeof(dir->header.num_entries) |
| 563 | + sizeof(dir->header.reserved)); |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 564 | break; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 565 | 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 Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 576 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 577 | } |
| 578 | |
Marshall Dawson | 8e0dca0 | 2019-02-27 18:40:49 -0700 | [diff] [blame] | 579 | static 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 Peers | af50567 | 2020-03-05 16:04:15 -0700 | [diff] [blame] | 587 | printf("Error opening file: %s: %s\n", |
| 588 | src_file, strerror(errno)); |
Marshall Dawson | 8e0dca0 | 2019-02-27 18:40:49 -0700 | [diff] [blame] | 589 | return -1; |
| 590 | } |
| 591 | |
| 592 | if (fstat(fd, &fd_stat)) { |
| 593 | printf("fstat error: %s\n", strerror(errno)); |
Jacob Garber | 967f862 | 2019-07-02 10:35:10 -0600 | [diff] [blame] | 594 | close(fd); |
Marshall Dawson | 8e0dca0 | 2019-02-27 18:40:49 -0700 | [diff] [blame] | 595 | return -2; |
| 596 | } |
| 597 | |
| 598 | if (fd_stat.st_size > room) { |
| 599 | printf("Error: %s will not fit. Exiting.\n", src_file); |
Jacob Garber | 967f862 | 2019-07-02 10:35:10 -0600 | [diff] [blame] | 600 | close(fd); |
Marshall Dawson | 8e0dca0 | 2019-02-27 18:40:49 -0700 | [diff] [blame] | 601 | 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 Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 614 | static void integrate_firmwares(context *ctx, |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 615 | embedded_firmware *romsig, |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 616 | amd_fw_entry *fw_table) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 617 | { |
Richard Spiegel | 137484d | 2018-01-17 10:23:19 -0700 | [diff] [blame] | 618 | ssize_t bytes; |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 619 | int i; |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 620 | |
| 621 | ctx->current += sizeof(embedded_firmware); |
| 622 | ctx->current = ALIGN(ctx->current, BLOB_ALIGNMENT); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 623 | |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 624 | for (i = 0; fw_table[i].type != AMD_FW_INVALID; i++) { |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 625 | if (fw_table[i].filename != NULL) { |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 626 | switch (fw_table[i].type) { |
| 627 | case AMD_FW_IMC: |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 628 | ctx->current = ALIGN(ctx->current, 0x10000U); |
| 629 | romsig->imc_entry = RUN_CURRENT(*ctx); |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 630 | break; |
| 631 | case AMD_FW_GEC: |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 632 | romsig->gec_entry = RUN_CURRENT(*ctx); |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 633 | break; |
| 634 | case AMD_FW_XHCI: |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 635 | romsig->xhci_entry = RUN_CURRENT(*ctx); |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 636 | break; |
| 637 | default: |
| 638 | /* Error */ |
| 639 | break; |
| 640 | } |
| 641 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 642 | bytes = copy_blob(BUFF_CURRENT(*ctx), |
| 643 | fw_table[i].filename, BUFF_ROOM(*ctx)); |
Marshall Dawson | 02bd773 | 2019-03-13 14:43:17 -0600 | [diff] [blame] | 644 | if (bytes < 0) { |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 645 | free(ctx->rom); |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 646 | exit(1); |
| 647 | } |
| 648 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 649 | ctx->current = ALIGN(ctx->current + bytes, |
| 650 | BLOB_ALIGNMENT); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 651 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 652 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 653 | } |
| 654 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 655 | static void integrate_psp_firmwares(context *ctx, |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 656 | psp_directory_table *pspdir, |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 657 | psp_directory_table *pspdir2, |
| 658 | amd_fw_entry *fw_table, |
| 659 | uint32_t cookie) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 660 | { |
Richard Spiegel | 137484d | 2018-01-17 10:23:19 -0700 | [diff] [blame] | 661 | ssize_t bytes; |
Marshall Dawson | c38c0c9 | 2019-02-23 16:41:35 -0700 | [diff] [blame] | 662 | unsigned int i, count; |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 663 | 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 Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 678 | |
| 679 | ctx->current = ALIGN(ctx->current, BLOB_ALIGNMENT); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 680 | |
Marshall Dawson | c38c0c9 | 2019-02-23 16:41:35 -0700 | [diff] [blame] | 681 | for (i = 0, count = 0; fw_table[i].type != AMD_FW_INVALID; i++) { |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 682 | if (!(fw_table[i].level & level)) |
| 683 | continue; |
| 684 | |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 685 | 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 Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 697 | pspdir->entries[count].type = fw_table[i].type; |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 698 | pspdir->entries[count].subprog = fw_table[i].subprog; |
| 699 | pspdir->entries[count].rsvd = 0; |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 700 | pspdir->entries[count].size = 0xFFFFFFFF; |
Marshall Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 701 | pspdir->entries[count].addr = fw_table[i].other; |
Marshall Dawson | c38c0c9 | 2019-02-23 16:41:35 -0700 | [diff] [blame] | 702 | count++; |
Marshall Dawson | 7c1e142 | 2019-04-11 09:44:43 -0600 | [diff] [blame] | 703 | } 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++; |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 728 | } else if (fw_table[i].filename != NULL) { |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 729 | bytes = copy_blob(BUFF_CURRENT(*ctx), |
| 730 | fw_table[i].filename, BUFF_ROOM(*ctx)); |
Marshall Dawson | 02bd773 | 2019-03-13 14:43:17 -0600 | [diff] [blame] | 731 | if (bytes < 0) { |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 732 | free(ctx->rom); |
Marshall Dawson | 8e0dca0 | 2019-02-27 18:40:49 -0700 | [diff] [blame] | 733 | exit(1); |
| 734 | } |
| 735 | |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 736 | pspdir->entries[count].type = fw_table[i].type; |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 737 | pspdir->entries[count].subprog = fw_table[i].subprog; |
| 738 | pspdir->entries[count].rsvd = 0; |
Marshall Dawson | 8e0dca0 | 2019-02-27 18:40:49 -0700 | [diff] [blame] | 739 | pspdir->entries[count].size = (uint32_t)bytes; |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 740 | pspdir->entries[count].addr = RUN_CURRENT(*ctx); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 741 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 742 | ctx->current = ALIGN(ctx->current + bytes, |
| 743 | BLOB_ALIGNMENT); |
Marshall Dawson | c38c0c9 | 2019-02-23 16:41:35 -0700 | [diff] [blame] | 744 | count++; |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 745 | } else { |
| 746 | /* This APU doesn't have this firmware. */ |
| 747 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 748 | } |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 749 | |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 750 | 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 Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 762 | 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 Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 768 | fill_dir_header(pspdir, count, cookie); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 769 | } |
| 770 | |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 771 | static 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 | |
| 790 | static 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 | |
| 810 | static 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 Dawson | c4a8c48 | 2020-01-21 17:17:59 -0700 | [diff] [blame] | 821 | static 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 Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 832 | static 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 Roth | ec93313 | 2019-07-13 20:03:34 -0600 | [diff] [blame] | 839 | unsigned int i, count; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 840 | int level; |
Marshall Dawson | c4a8c48 | 2020-01-21 17:17:59 -0700 | [diff] [blame] | 841 | int apob_idx; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 842 | |
| 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 Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 868 | |
| 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 Dawson | c4a8c48 | 2020-01-21 17:17:59 -0700 | [diff] [blame] | 879 | /* 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 Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 892 | |
| 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 Barnes | 18fd26c | 2020-03-03 10:35:02 -0700 | [diff] [blame] | 1016 | printf("Error: BIOS entries (%d) exceeds max allowed items " |
| 1017 | "(%d)\n", count, MAX_BIOS_ENTRIES); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1018 | free(ctx->rom); |
| 1019 | exit(1); |
| 1020 | } |
| 1021 | |
| 1022 | fill_dir_header(biosdir, count, cookie); |
| 1023 | } |
Martin Roth | d3ce8c8 | 2019-07-13 20:13:07 -0600 | [diff] [blame] | 1024 | // Unused values: CDEPqR |
| 1025 | static 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 Jones | 90099b6 | 2016-09-20 21:05:45 -0600 | [diff] [blame] | 1026 | |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1027 | static struct option long_options[] = { |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 1028 | {"xhci", required_argument, 0, 'x' }, |
| 1029 | {"imc", required_argument, 0, 'i' }, |
| 1030 | {"gec", required_argument, 0, 'g' }, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1031 | /* PSP Directory Table items */ |
Marshall Dawson | 67d868d | 2019-02-28 11:43:40 -0700 | [diff] [blame] | 1032 | {"combo-capable", no_argument, 0, 'A' }, |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 1033 | {"multilevel", no_argument, 0, 'M' }, |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1034 | {"subprogram", required_argument, 0, 'S' }, |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 1035 | {"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 Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 1047 | {"soft-fuse", required_argument, 0, 'T' }, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1048 | {"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 Roth | d3ce8c8 | 2019-07-13 20:13:07 -0600 | [diff] [blame] | 1057 | {"verstage", required_argument, 0, 'Z' }, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1058 | /* 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 Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 1073 | {"output", required_argument, 0, 'o' }, |
| 1074 | {"flashsize", required_argument, 0, 'f' }, |
Martin Roth | 0d3b118 | 2017-10-03 14:16:04 -0600 | [diff] [blame] | 1075 | {"location", required_argument, 0, 'l' }, |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 1076 | {"help", no_argument, 0, 'h' }, |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 1077 | {NULL, 0, 0, 0 } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1078 | }; |
| 1079 | |
Marshall Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 1080 | static 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 Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1093 | static 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 Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1106 | static void register_fw_filename(amd_fw_type type, uint8_t sub, char filename[]) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1107 | { |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 1108 | unsigned int i; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1109 | |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 1110 | for (i = 0; i < sizeof(amd_fw_table) / sizeof(amd_fw_entry); i++) { |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1111 | if (amd_fw_table[i].type == type) { |
| 1112 | amd_fw_table[i].filename = filename; |
| 1113 | return; |
| 1114 | } |
| 1115 | } |
| 1116 | |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 1117 | for (i = 0; i < sizeof(amd_psp_fw_table) / sizeof(amd_fw_entry); i++) { |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1118 | if (amd_psp_fw_table[i].type != type) |
| 1119 | continue; |
| 1120 | |
| 1121 | if (amd_psp_fw_table[i].subprog == sub) { |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 1122 | amd_psp_fw_table[i].filename = filename; |
| 1123 | return; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1124 | } |
| 1125 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1126 | } |
| 1127 | |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1128 | static 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 Roth | ec93313 | 2019-07-13 20:03:34 -0600 | [diff] [blame] | 1142 | static void register_fw_addr(amd_bios_type type, char *src_str, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1143 | 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 Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1161 | int main(int argc, char **argv) |
| 1162 | { |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 1163 | int c; |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1164 | int retval = 0; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1165 | char *tmp; |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 1166 | char *rom = NULL; |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 1167 | embedded_firmware *amd_romsig; |
| 1168 | psp_directory_table *pspdir; |
Marshall Dawson | 67d868d | 2019-02-28 11:43:40 -0700 | [diff] [blame] | 1169 | int comboable = 0; |
Marshall Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 1170 | int fuse_defined = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1171 | int targetfd; |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 1172 | char *output = NULL; |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1173 | context ctx = { |
| 1174 | .rom_size = CONFIG_ROM_SIZE, |
| 1175 | }; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1176 | /* Values cleared after each firmware or parameter, regardless if N/A */ |
| 1177 | uint8_t sub = 0, instance = 0; |
| 1178 | int abl_image = 0; |
Martin Roth | 0d3b118 | 2017-10-03 14:16:04 -0600 | [diff] [blame] | 1179 | uint32_t dir_location = 0; |
| 1180 | uint32_t romsig_offset; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1181 | uint32_t rom_base_address; |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 1182 | int multi = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1183 | |
| 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 Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1194 | register_fw_filename(AMD_FW_XHCI, sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1195 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1196 | break; |
| 1197 | case 'i': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1198 | register_fw_filename(AMD_FW_IMC, sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1199 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1200 | break; |
| 1201 | case 'g': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1202 | register_fw_filename(AMD_FW_GEC, sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1203 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1204 | break; |
Marshall Dawson | 67d868d | 2019-02-28 11:43:40 -0700 | [diff] [blame] | 1205 | case 'A': |
| 1206 | comboable = 1; |
| 1207 | break; |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 1208 | case 'M': |
| 1209 | multi = 1; |
| 1210 | break; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1211 | case 'U': |
| 1212 | register_fw_token_unlock(); |
| 1213 | sub = instance = 0; |
| 1214 | break; |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1215 | case 'S': |
| 1216 | sub = (uint8_t)strtoul(optarg, &tmp, 16); |
| 1217 | break; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1218 | case 'I': |
| 1219 | instance = strtoul(optarg, &tmp, 16); |
| 1220 | break; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1221 | case 'p': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1222 | register_fw_filename(AMD_FW_PSP_PUBKEY, sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1223 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1224 | break; |
| 1225 | case 'b': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1226 | register_fw_filename(AMD_FW_PSP_BOOTLOADER, |
| 1227 | sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1228 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1229 | break; |
| 1230 | case 's': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1231 | register_fw_filename(AMD_FW_PSP_SMU_FIRMWARE, |
| 1232 | sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1233 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1234 | break; |
| 1235 | case 'r': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1236 | register_fw_filename(AMD_FW_PSP_RECOVERY, sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1237 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1238 | break; |
| 1239 | case 'k': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1240 | register_fw_filename(AMD_FW_PSP_RTM_PUBKEY, |
| 1241 | sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1242 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1243 | break; |
| 1244 | case 'c': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1245 | register_fw_filename(AMD_FW_PSP_SECURED_OS, |
| 1246 | sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1247 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1248 | break; |
| 1249 | case 'n': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1250 | register_fw_filename(AMD_FW_PSP_NVRAM, sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1251 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1252 | break; |
| 1253 | case 'd': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1254 | register_fw_filename(AMD_FW_PSP_SECURED_DEBUG, |
| 1255 | sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1256 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1257 | break; |
| 1258 | case 't': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1259 | register_fw_filename(AMD_FW_PSP_TRUSTLETS, sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1260 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1261 | break; |
| 1262 | case 'u': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1263 | register_fw_filename(AMD_FW_PSP_TRUSTLETKEY, |
| 1264 | sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1265 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1266 | break; |
| 1267 | case 'w': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1268 | register_fw_filename(AMD_FW_PSP_SMU_FIRMWARE2, |
| 1269 | sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1270 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1271 | break; |
| 1272 | case 'm': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1273 | register_fw_filename(AMD_FW_PSP_SMUSCS, sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1274 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1275 | break; |
Marshall Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 1276 | case 'T': |
| 1277 | register_fw_fuse(optarg); |
| 1278 | fuse_defined = 1; |
| 1279 | sub = 0; |
| 1280 | break; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1281 | 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 Roth | d3ce8c8 | 2019-07-13 20:13:07 -0600 | [diff] [blame] | 1372 | case 'Z': |
| 1373 | register_fw_filename(AMD_FW_PSP_VERSTAGE, sub, optarg); |
| 1374 | sub = instance = 0; |
| 1375 | break; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1376 | case 'o': |
| 1377 | output = optarg; |
| 1378 | break; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1379 | case 'f': |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1380 | ctx.rom_size = (uint32_t)strtoul(optarg, &tmp, 16); |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1381 | if (*tmp != '\0') { |
| 1382 | printf("Error: ROM size specified" |
| 1383 | " incorrectly (%s)\n\n", optarg); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1384 | retval = 1; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1385 | } |
| 1386 | break; |
Martin Roth | 0d3b118 | 2017-10-03 14:16:04 -0600 | [diff] [blame] | 1387 | 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 Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1396 | case 'h': |
| 1397 | usage(); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1398 | return 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1399 | default: |
| 1400 | break; |
| 1401 | } |
| 1402 | } |
| 1403 | |
Marshall Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 1404 | if (!fuse_defined) |
| 1405 | register_fw_fuse(DEFAULT_SOFT_FUSE_CHAIN); |
| 1406 | |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 1407 | if (!output) { |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1408 | printf("Error: Output value is not specified.\n\n"); |
| 1409 | retval = 1; |
| 1410 | } |
| 1411 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1412 | if (ctx.rom_size % 1024 != 0) { |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1413 | printf("Error: ROM Size (%d bytes) should be a multiple of" |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1414 | " 1024 bytes.\n\n", ctx.rom_size); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1415 | retval = 1; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1416 | } |
| 1417 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1418 | if (ctx.rom_size < MIN_ROM_KB * 1024) { |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1419 | printf("Error: ROM Size (%dKB) must be at least %dKB.\n\n", |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1420 | ctx.rom_size / 1024, MIN_ROM_KB); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1421 | retval = 1; |
| 1422 | } |
| 1423 | |
| 1424 | if (retval) { |
| 1425 | usage(); |
| 1426 | return retval; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1427 | } |
| 1428 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1429 | printf(" AMDFWTOOL Using ROM size of %dKB\n", ctx.rom_size / 1024); |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1430 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1431 | rom_base_address = 0xFFFFFFFF - ctx.rom_size + 1; |
Martin Roth | 0d3b118 | 2017-10-03 14:16:04 -0600 | [diff] [blame] | 1432 | 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 Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1453 | ctx.rom = malloc(ctx.rom_size); |
| 1454 | if (!ctx.rom) { |
| 1455 | printf("Error: Failed to allocate memory\n"); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1456 | return 1; |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1457 | } |
| 1458 | memset(ctx.rom, 0xFF, ctx.rom_size); |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1459 | |
Martin Roth | 0d3b118 | 2017-10-03 14:16:04 -0600 | [diff] [blame] | 1460 | if (dir_location) |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1461 | romsig_offset = ctx.current = dir_location - rom_base_address; |
Martin Roth | 0d3b118 | 2017-10-03 14:16:04 -0600 | [diff] [blame] | 1462 | else |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1463 | romsig_offset = ctx.current = AMD_ROMSIG_OFFSET; |
| 1464 | printf(" AMDFWTOOL Using firmware directory location of 0x%08x\n", |
| 1465 | RUN_CURRENT(ctx)); |
Martin Roth | 0d3b118 | 2017-10-03 14:16:04 -0600 | [diff] [blame] | 1466 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1467 | amd_romsig = BUFF_OFFSET(ctx, romsig_offset); |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 1468 | 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 Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1472 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1473 | integrate_firmwares(&ctx, amd_romsig, amd_fw_table); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1474 | |
Patrick Georgi | 900a254 | 2020-02-17 16:52:40 +0100 | [diff] [blame] | 1475 | ctx.current = ALIGN(ctx.current, 0x10000U); /* TODO: is it necessary? */ |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1476 | |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 1477 | 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 Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1492 | |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 1493 | if (comboable) |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1494 | amd_romsig->comboable = BUFF_TO_RUN(ctx, pspdir); |
Marshall Dawson | 67d868d | 2019-02-28 11:43:40 -0700 | [diff] [blame] | 1495 | else |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1496 | amd_romsig->psp_entry = BUFF_TO_RUN(ctx, pspdir); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1497 | |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 1498 | #if PSP_COMBO |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1499 | psp_combo_directory *combo_dir = new_combo_dir(&ctx); |
| 1500 | amd_romsig->comboable = BUFF_TO_RUN(ctx, combo_dir); |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 1501 | /* 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 Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1505 | combo_dir->entries[0].lvl2_addr = BUFF_TO_RUN(ctx, pspdir); |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 1506 | |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 1507 | combo_dir->header.lookup = 1; |
Marshall Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 1508 | fill_dir_header(combo_dir, 1, PSP2_COOKIE); |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 1509 | #endif |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 1510 | |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1511 | 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 Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1532 | targetfd = open(output, O_RDWR | O_CREAT | O_TRUNC, 0666); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1533 | if (targetfd >= 0) { |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1534 | write(targetfd, amd_romsig, ctx.current - romsig_offset); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1535 | close(targetfd); |
| 1536 | } else { |
| 1537 | printf("Error: could not open file: %s\n", output); |
| 1538 | retval = 1; |
| 1539 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1540 | |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1541 | free(rom); |
| 1542 | return retval; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1543 | } |