Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Marshall Dawson | e7d892c | 2016-10-08 14:49:41 -0600 | [diff] [blame] | 4 | * Copyright (C) 2015 - 2016 Advanced Micro Devices, Inc. |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 5 | * |
| 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: |
zbao | c3b0b72 | 2016-02-19 13:47:31 +0800 | [diff] [blame] | 18 | * 0 4 8 C |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 19 | * +------------+---------------+----------------+------------+ |
| 20 | * | 0x55AA55AA |EC ROM Address |GEC ROM Address |USB3 ROM | |
| 21 | * +------------+---------------+----------------+------------+ |
zbao | c3b0b72 | 2016-02-19 13:47:31 +0800 | [diff] [blame] | 22 | * | PSPDIR ADDR|PSPDIR ADDR |<-- Field 0x14 could be either |
| 23 | * +------------+---------------+ 2nd PSP directory or PSP COMBO directory |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 24 | * EC ROM should be 64K aligned. |
| 25 | * |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 26 | * PSP directory (Where "PSPDIR ADDR" points) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 27 | * +------------+---------------+----------------+------------+ |
| 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 Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 44 | * |
zbao | c3b0b72 | 2016-02-19 13:47:31 +0800 | [diff] [blame] | 45 | * PSP Combo directory |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 46 | * +------------+---------------+----------------+------------+ |
zbao | 6e2f3d1 | 2016-02-19 13:34:59 +0800 | [diff] [blame] | 47 | * | 'PSP2' | Fletcher | Count |Look up mode| |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 48 | * +------------+---------------+----------------+------------+ |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 49 | * | R e s e r v e d | |
| 50 | * +------------+---------------+----------------+------------+ |
zbao | 6e2f3d1 | 2016-02-19 13:34:59 +0800 | [diff] [blame] | 51 | * | ID-Sel | PSP ID | PSPDIR ADDR | | 2nd PSP directory |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 52 | * +------------+---------------+----------------+------------+ |
zbao | 6e2f3d1 | 2016-02-19 13:34:59 +0800 | [diff] [blame] | 53 | * | ID-Sel | PSP ID | PSPDIR ADDR | | 3rd PSP directory |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 54 | * +------------+---------------+----------------+------------+ |
| 55 | * | | |
| 56 | * | Other PSP | |
| 57 | * | | |
| 58 | * +------------+---------------+----------------+------------+ |
| 59 | * |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 60 | */ |
| 61 | |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 62 | #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 Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 76 | #define AMD_ROMSIG_OFFSET 0x20000 |
| 77 | #define MIN_ROM_KB 256 |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 78 | |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 79 | #define ALIGN(val, by) (((val) + (by) - 1) & ~((by) - 1)) |
Marshall Dawson | 7c1e142 | 2019-04-11 09:44:43 -0600 | [diff] [blame] | 80 | #define _MAX(A, B) (((A) > (B)) ? (A) : (B)) |
| 81 | #define ERASE_ALIGNMENT 0x1000U |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 82 | #define TABLE_ALIGNMENT 0x1000U |
| 83 | #define BLOB_ALIGNMENT 0x100U |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 84 | #define TABLE_ERASE_ALIGNMENT _MAX(TABLE_ALIGNMENT, ERASE_ALIGNMENT) |
Marshall Dawson | 7c1e142 | 2019-04-11 09:44:43 -0600 | [diff] [blame] | 85 | #define BLOB_ERASE_ALIGNMENT _MAX(BLOB_ALIGNMENT, ERASE_ALIGNMENT) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 86 | |
Marshall Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 87 | #define DEFAULT_SOFT_FUSE_CHAIN "0x1" |
| 88 | |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 89 | #define EMBEDDED_FW_SIGNATURE 0x55aa55aa |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 90 | #define PSP_COOKIE 0x50535024 /* 'PSP$' */ |
| 91 | #define PSPL2_COOKIE 0x324c5024 /* '2LP$' */ |
| 92 | #define PSP2_COOKIE 0x50535032 /* 'PSP2' */ |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 93 | #define BDT1_COOKIE 0x44484224 /* 'DHB$ */ |
| 94 | #define BDT2_COOKIE 0x324c4224 /* '2LB$ */ |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 95 | |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 96 | /* |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 97 | * 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. |
zbao | c3b0b72 | 2016-02-19 13:47:31 +0800 | [diff] [blame] | 110 | */ |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 111 | #define PSP_COMBO 0 |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 112 | |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 113 | typedef unsigned long long int uint64_t; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 114 | typedef unsigned int uint32_t; |
| 115 | typedef unsigned char uint8_t; |
| 116 | typedef 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 Dawson | 8a45a4d | 2019-02-24 07:18:44 -0700 | [diff] [blame] | 138 | static uint32_t fletcher32(const void *data, int length) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 139 | { |
| 140 | uint32_t c0; |
| 141 | uint32_t c1; |
| 142 | uint32_t checksum; |
| 143 | int index; |
Marshall Dawson | 8a45a4d | 2019-02-24 07:18:44 -0700 | [diff] [blame] | 144 | const uint16_t *pptr = data; |
| 145 | |
| 146 | length /= 2; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 147 | |
| 148 | c0 = 0xFFFF; |
| 149 | c1 = 0xFFFF; |
| 150 | |
Marshall Dawson | b85ddc5 | 2019-07-23 07:24:30 -0600 | [diff] [blame] | 151 | while (length) { |
| 152 | index = length >= 359 ? 359 : length; |
| 153 | length -= index; |
| 154 | do { |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 155 | c0 += *(pptr++); |
| 156 | c1 += c0; |
Marshall Dawson | b85ddc5 | 2019-07-23 07:24:30 -0600 | [diff] [blame] | 157 | } while (--index); |
| 158 | c0 = (c0 & 0xFFFF) + (c0 >> 16); |
| 159 | c1 = (c1 & 0xFFFF) + (c1 >> 16); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 160 | } |
| 161 | |
Marshall Dawson | 8a45a4d | 2019-02-24 07:18:44 -0700 | [diff] [blame] | 162 | /* Sums[0,1] mod 64K + overflow */ |
| 163 | c0 = (c0 & 0xFFFF) + (c0 >> 16); |
| 164 | c1 = (c1 & 0xFFFF) + (c1 >> 16); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 165 | checksum = (c1 << 16) | c0; |
| 166 | |
| 167 | return checksum; |
| 168 | } |
| 169 | |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 170 | static void usage(void) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 171 | { |
Martin Roth | 0e94062 | 2016-11-08 10:37:53 -0700 | [diff] [blame] | 172 | printf("amdfwtool: Create AMD Firmware combination\n"); |
| 173 | printf("Usage: amdfwtool [options] -f <size> -o <filename>\n"); |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 174 | 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 Roth | 0e94062 | 2016-11-08 10:37:53 -0700 | [diff] [blame] | 177 | |
| 178 | printf("\nPSP options:\n"); |
Marshall Dawson | 67d868d | 2019-02-28 11:43:40 -0700 | [diff] [blame] | 179 | printf("-A | --combo-capable Place PSP directory pointer at Embedded Firmware\n"); |
| 180 | printf(" offset able to support combo directory\n"); |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 181 | printf("-M | --multilevel Generate primary and secondary tables\n"); |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 182 | printf("-p | --pubkey <FILE> Add pubkey\n"); |
| 183 | printf("-b | --bootloader <FILE> Add bootloader\n"); |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 184 | printf("-S | --subprogram <number> Sets subprogram field for the next firmware\n"); |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 185 | 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 Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 195 | printf("-T | --soft-fuse <HEX_VAL> Override default soft fuse values\n"); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 196 | 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 Roth | d3ce8c8 | 2019-07-13 20:13:07 -0600 | [diff] [blame] | 204 | printf("-Z | --verstage <FILE> Add verstage\n"); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 205 | 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 Roth | ec93313 | 2019-07-13 20:03:34 -0600 | [diff] [blame] | 213 | printf("-O | --ucode <FILE> Add microcode patch\n"); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 214 | 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 Roth | 0e94062 | 2016-11-08 10:37:53 -0700 | [diff] [blame] | 219 | printf("\n-o | --output <filename> output filename\n"); |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 220 | printf("-f | --flashsize <HEX_VAL> ROM size in bytes\n"); |
| 221 | printf(" size must be larger than %dKB\n", |
Martin Roth | 0e94062 | 2016-11-08 10:37:53 -0700 | [diff] [blame] | 222 | MIN_ROM_KB); |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 223 | printf(" and must a multiple of 1024\n"); |
Martin Roth | 0d3b118 | 2017-10-03 14:16:04 -0600 | [diff] [blame] | 224 | printf("-l | --location Location of Directory\n"); |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 225 | printf("-h | --help show this help\n"); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 226 | } |
| 227 | |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 228 | typedef 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) |
| 245 | typedef 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 Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 261 | typedef 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 Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 275 | 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 Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 291 | AMD_FW_L2_PTR = 0x40, |
Martin Roth | d3ce8c8 | 2019-07-13 20:13:07 -0600 | [diff] [blame] | 292 | AMD_FW_PSP_VERSTAGE = 0x52, |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 293 | AMD_FW_IMC, |
| 294 | AMD_FW_GEC, |
| 295 | AMD_FW_XHCI, |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 296 | AMD_FW_INVALID, |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 297 | } amd_fw_type; |
| 298 | |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 299 | #define PSP_LVL1 0x1 |
| 300 | #define PSP_LVL2 0x2 |
| 301 | #define PSP_BOTH (PSP_LVL1 | PSP_LVL2) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 302 | typedef struct _amd_fw_entry { |
| 303 | amd_fw_type type; |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 304 | uint8_t subprog; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 305 | char *filename; |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 306 | int level; |
Marshall Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 307 | uint64_t other; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 308 | } amd_fw_entry; |
| 309 | |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 310 | static amd_fw_entry amd_psp_fw_table[] = { |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 311 | { .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 Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 328 | { .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 Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 345 | { .type = AMD_FW_PSP_SMU_FIRMWARE, .subprog = 1, .level = PSP_BOTH }, |
| 346 | { .type = AMD_FW_PSP_SMU_FIRMWARE2, .subprog = 1, .level = PSP_BOTH }, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 347 | { .type = AMD_FW_PSP_WHITELIST, .level = PSP_LVL2 }, |
Martin Roth | d3ce8c8 | 2019-07-13 20:13:07 -0600 | [diff] [blame] | 348 | { .type = AMD_FW_PSP_VERSTAGE, .level = PSP_BOTH }, |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 349 | { .type = AMD_FW_INVALID }, |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 350 | }; |
| 351 | |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 352 | static amd_fw_entry amd_fw_table[] = { |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 353 | { .type = AMD_FW_XHCI }, |
| 354 | { .type = AMD_FW_IMC }, |
| 355 | { .type = AMD_FW_GEC }, |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 356 | { .type = AMD_FW_INVALID }, |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 357 | }; |
| 358 | |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 359 | static amd_bios_entry amd_bios_table[] = { |
Marshall Dawson | 0581bf6 | 2019-09-25 11:03:53 -0600 | [diff] [blame] | 360 | { .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 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 }, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 370 | { .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 Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 389 | typedef 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 Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 396 | uint32_t bios0_entry; /* todo: add way to select correct entry */ |
| 397 | uint32_t bios1_entry; |
Marshall Dawson | 94f2492 | 2019-09-28 08:49:09 -0600 | [diff] [blame] | 398 | uint32_t bios2_entry; |
| 399 | uint32_t reserved[0x2c]; /* 0x24 - 0x4f */ |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 400 | } __attribute__((packed, aligned(16))) embedded_firmware; |
| 401 | |
| 402 | typedef 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 | |
| 409 | typedef struct _psp_directory_entry { |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 410 | uint8_t type; |
| 411 | uint8_t subprog; |
| 412 | uint16_t rsvd; |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 413 | uint32_t size; |
| 414 | uint64_t addr; /* or a value in some cases */ |
| 415 | } __attribute__((packed)) psp_directory_entry; |
| 416 | |
| 417 | typedef struct _psp_directory_table { |
| 418 | psp_directory_header header; |
| 419 | psp_directory_entry entries[]; |
| 420 | } __attribute__((packed)) psp_directory_table; |
| 421 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 422 | #define MAX_PSP_ENTRIES 0x1f |
| 423 | |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 424 | typedef 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 | |
| 432 | typedef 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 | |
| 438 | typedef struct _psp_combo_directory { |
| 439 | psp_combo_header header; |
| 440 | psp_combo_entry entries[]; |
| 441 | } __attribute__((packed)) psp_combo_directory; |
| 442 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 443 | #define MAX_COMBO_ENTRIES 1 |
| 444 | |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 445 | typedef 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 | |
| 452 | typedef 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 | |
| 466 | typedef 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 Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 473 | typedef 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 Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 487 | static void *new_psp_dir(context *ctx, int multi) |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 488 | { |
| 489 | void *ptr; |
| 490 | |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 491 | /* |
| 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 Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 501 | 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 Roth | ec93313 | 2019-07-13 20:03:34 -0600 | [diff] [blame] | 507 | #if PSP_COMBO |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 508 | static 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 Roth | ec93313 | 2019-07-13 20:03:34 -0600 | [diff] [blame] | 518 | #endif |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 519 | |
Marshall Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 520 | 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] | 521 | { |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 522 | psp_combo_directory *cdir = directory; |
| 523 | psp_directory_table *dir = directory; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 524 | bios_directory_table *bdir = directory; |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 525 | |
| 526 | if (!count) |
| 527 | return; |
| 528 | |
| 529 | switch (cookie) { |
| 530 | case PSP2_COOKIE: |
Marshall Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 531 | /* caller is responsible for lookup mode */ |
Marshall Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 532 | 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 Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 542 | break; |
| 543 | case PSP_COOKIE: |
| 544 | case PSPL2_COOKIE: |
Marshall Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 545 | 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 Dawson | 8a45a4d | 2019-02-24 07:18:44 -0700 | [diff] [blame] | 550 | count * sizeof(psp_directory_entry) |
Marshall Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 551 | + sizeof(dir->header.num_entries) |
| 552 | + sizeof(dir->header.reserved)); |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 553 | break; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 554 | 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 Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 565 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 566 | } |
| 567 | |
Marshall Dawson | 8e0dca0 | 2019-02-27 18:40:49 -0700 | [diff] [blame] | 568 | static 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 Garber | 967f862 | 2019-07-02 10:35:10 -0600 | [diff] [blame] | 582 | close(fd); |
Marshall Dawson | 8e0dca0 | 2019-02-27 18:40:49 -0700 | [diff] [blame] | 583 | return -2; |
| 584 | } |
| 585 | |
| 586 | if (fd_stat.st_size > room) { |
| 587 | printf("Error: %s will not fit. Exiting.\n", src_file); |
Jacob Garber | 967f862 | 2019-07-02 10:35:10 -0600 | [diff] [blame] | 588 | close(fd); |
Marshall Dawson | 8e0dca0 | 2019-02-27 18:40:49 -0700 | [diff] [blame] | 589 | 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 Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 602 | static void integrate_firmwares(context *ctx, |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 603 | embedded_firmware *romsig, |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 604 | amd_fw_entry *fw_table) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 605 | { |
Richard Spiegel | 137484d | 2018-01-17 10:23:19 -0700 | [diff] [blame] | 606 | ssize_t bytes; |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 607 | int i; |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 608 | |
| 609 | ctx->current += sizeof(embedded_firmware); |
| 610 | ctx->current = ALIGN(ctx->current, BLOB_ALIGNMENT); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 611 | |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 612 | for (i = 0; fw_table[i].type != AMD_FW_INVALID; i++) { |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 613 | if (fw_table[i].filename != NULL) { |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 614 | switch (fw_table[i].type) { |
| 615 | case AMD_FW_IMC: |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 616 | ctx->current = ALIGN(ctx->current, 0x10000U); |
| 617 | romsig->imc_entry = RUN_CURRENT(*ctx); |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 618 | break; |
| 619 | case AMD_FW_GEC: |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 620 | romsig->gec_entry = RUN_CURRENT(*ctx); |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 621 | break; |
| 622 | case AMD_FW_XHCI: |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 623 | romsig->xhci_entry = RUN_CURRENT(*ctx); |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 624 | break; |
| 625 | default: |
| 626 | /* Error */ |
| 627 | break; |
| 628 | } |
| 629 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 630 | bytes = copy_blob(BUFF_CURRENT(*ctx), |
| 631 | fw_table[i].filename, BUFF_ROOM(*ctx)); |
Marshall Dawson | 02bd773 | 2019-03-13 14:43:17 -0600 | [diff] [blame] | 632 | if (bytes < 0) { |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 633 | free(ctx->rom); |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 634 | exit(1); |
| 635 | } |
| 636 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 637 | ctx->current = ALIGN(ctx->current + bytes, |
| 638 | BLOB_ALIGNMENT); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 639 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 640 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 641 | } |
| 642 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 643 | static void integrate_psp_firmwares(context *ctx, |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 644 | psp_directory_table *pspdir, |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 645 | psp_directory_table *pspdir2, |
| 646 | amd_fw_entry *fw_table, |
| 647 | uint32_t cookie) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 648 | { |
Richard Spiegel | 137484d | 2018-01-17 10:23:19 -0700 | [diff] [blame] | 649 | ssize_t bytes; |
Marshall Dawson | c38c0c9 | 2019-02-23 16:41:35 -0700 | [diff] [blame] | 650 | unsigned int i, count; |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 651 | 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 Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 666 | |
| 667 | ctx->current = ALIGN(ctx->current, BLOB_ALIGNMENT); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 668 | |
Marshall Dawson | c38c0c9 | 2019-02-23 16:41:35 -0700 | [diff] [blame] | 669 | 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] | 670 | if (!(fw_table[i].level & level)) |
| 671 | continue; |
| 672 | |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 673 | 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 Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 685 | pspdir->entries[count].type = fw_table[i].type; |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 686 | pspdir->entries[count].subprog = fw_table[i].subprog; |
| 687 | pspdir->entries[count].rsvd = 0; |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 688 | pspdir->entries[count].size = 0xFFFFFFFF; |
Marshall Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 689 | pspdir->entries[count].addr = fw_table[i].other; |
Marshall Dawson | c38c0c9 | 2019-02-23 16:41:35 -0700 | [diff] [blame] | 690 | count++; |
Marshall Dawson | 7c1e142 | 2019-04-11 09:44:43 -0600 | [diff] [blame] | 691 | } 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++; |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 716 | } else if (fw_table[i].filename != NULL) { |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 717 | bytes = copy_blob(BUFF_CURRENT(*ctx), |
| 718 | fw_table[i].filename, BUFF_ROOM(*ctx)); |
Marshall Dawson | 02bd773 | 2019-03-13 14:43:17 -0600 | [diff] [blame] | 719 | if (bytes < 0) { |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 720 | free(ctx->rom); |
Marshall Dawson | 8e0dca0 | 2019-02-27 18:40:49 -0700 | [diff] [blame] | 721 | exit(1); |
| 722 | } |
| 723 | |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 724 | pspdir->entries[count].type = fw_table[i].type; |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 725 | pspdir->entries[count].subprog = fw_table[i].subprog; |
| 726 | pspdir->entries[count].rsvd = 0; |
Marshall Dawson | 8e0dca0 | 2019-02-27 18:40:49 -0700 | [diff] [blame] | 727 | pspdir->entries[count].size = (uint32_t)bytes; |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 728 | pspdir->entries[count].addr = RUN_CURRENT(*ctx); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 729 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 730 | ctx->current = ALIGN(ctx->current + bytes, |
| 731 | BLOB_ALIGNMENT); |
Marshall Dawson | c38c0c9 | 2019-02-23 16:41:35 -0700 | [diff] [blame] | 732 | count++; |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 733 | } else { |
| 734 | /* This APU doesn't have this firmware. */ |
| 735 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 736 | } |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 737 | |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 738 | 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 Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 750 | 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 Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 756 | fill_dir_header(pspdir, count, cookie); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 757 | } |
| 758 | |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 759 | static 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 | |
| 778 | static 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 | |
| 798 | static 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 | |
| 809 | static 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 Roth | ec93313 | 2019-07-13 20:03:34 -0600 | [diff] [blame] | 816 | unsigned int i, count; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 817 | 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 Roth | d3ce8c8 | 2019-07-13 20:13:07 -0600 | [diff] [blame] | 989 | // Unused values: CDEPqR |
| 990 | 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] | 991 | |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 992 | static struct option long_options[] = { |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 993 | {"xhci", required_argument, 0, 'x' }, |
| 994 | {"imc", required_argument, 0, 'i' }, |
| 995 | {"gec", required_argument, 0, 'g' }, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 996 | /* PSP Directory Table items */ |
Marshall Dawson | 67d868d | 2019-02-28 11:43:40 -0700 | [diff] [blame] | 997 | {"combo-capable", no_argument, 0, 'A' }, |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 998 | {"multilevel", no_argument, 0, 'M' }, |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 999 | {"subprogram", required_argument, 0, 'S' }, |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 1000 | {"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 Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 1012 | {"soft-fuse", required_argument, 0, 'T' }, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1013 | {"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 Roth | d3ce8c8 | 2019-07-13 20:13:07 -0600 | [diff] [blame] | 1022 | {"verstage", required_argument, 0, 'Z' }, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1023 | /* 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 Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 1038 | {"output", required_argument, 0, 'o' }, |
| 1039 | {"flashsize", required_argument, 0, 'f' }, |
Martin Roth | 0d3b118 | 2017-10-03 14:16:04 -0600 | [diff] [blame] | 1040 | {"location", required_argument, 0, 'l' }, |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 1041 | {"help", no_argument, 0, 'h' }, |
Marshall Dawson | f4b9b41 | 2017-03-17 16:30:51 -0600 | [diff] [blame] | 1042 | {NULL, 0, 0, 0 } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1043 | }; |
| 1044 | |
Marshall Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 1045 | static 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 Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1058 | static 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 Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1071 | 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] | 1072 | { |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 1073 | unsigned int i; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1074 | |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 1075 | 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] | 1076 | if (amd_fw_table[i].type == type) { |
| 1077 | amd_fw_table[i].filename = filename; |
| 1078 | return; |
| 1079 | } |
| 1080 | } |
| 1081 | |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 1082 | 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] | 1083 | if (amd_psp_fw_table[i].type != type) |
| 1084 | continue; |
| 1085 | |
| 1086 | if (amd_psp_fw_table[i].subprog == sub) { |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 1087 | amd_psp_fw_table[i].filename = filename; |
| 1088 | return; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1089 | } |
| 1090 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1091 | } |
| 1092 | |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1093 | static 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 Roth | ec93313 | 2019-07-13 20:03:34 -0600 | [diff] [blame] | 1107 | static void register_fw_addr(amd_bios_type type, char *src_str, |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1108 | 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 Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1126 | int main(int argc, char **argv) |
| 1127 | { |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 1128 | int c; |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1129 | int retval = 0; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1130 | char *tmp; |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 1131 | char *rom = NULL; |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 1132 | embedded_firmware *amd_romsig; |
| 1133 | psp_directory_table *pspdir; |
Marshall Dawson | 67d868d | 2019-02-28 11:43:40 -0700 | [diff] [blame] | 1134 | int comboable = 0; |
Marshall Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 1135 | int fuse_defined = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1136 | int targetfd; |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 1137 | char *output = NULL; |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1138 | context ctx = { |
| 1139 | .rom_size = CONFIG_ROM_SIZE, |
| 1140 | }; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1141 | /* Values cleared after each firmware or parameter, regardless if N/A */ |
| 1142 | uint8_t sub = 0, instance = 0; |
| 1143 | int abl_image = 0; |
Martin Roth | 0d3b118 | 2017-10-03 14:16:04 -0600 | [diff] [blame] | 1144 | uint32_t dir_location = 0; |
| 1145 | uint32_t romsig_offset; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1146 | uint32_t rom_base_address; |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 1147 | int multi = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1148 | |
| 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 Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1159 | register_fw_filename(AMD_FW_XHCI, sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1160 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1161 | break; |
| 1162 | case 'i': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1163 | register_fw_filename(AMD_FW_IMC, sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1164 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1165 | break; |
| 1166 | case 'g': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1167 | register_fw_filename(AMD_FW_GEC, sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1168 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1169 | break; |
Marshall Dawson | 67d868d | 2019-02-28 11:43:40 -0700 | [diff] [blame] | 1170 | case 'A': |
| 1171 | comboable = 1; |
| 1172 | break; |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 1173 | case 'M': |
| 1174 | multi = 1; |
| 1175 | break; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1176 | case 'U': |
| 1177 | register_fw_token_unlock(); |
| 1178 | sub = instance = 0; |
| 1179 | break; |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1180 | case 'S': |
| 1181 | sub = (uint8_t)strtoul(optarg, &tmp, 16); |
| 1182 | break; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1183 | case 'I': |
| 1184 | instance = strtoul(optarg, &tmp, 16); |
| 1185 | break; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1186 | case 'p': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1187 | register_fw_filename(AMD_FW_PSP_PUBKEY, sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1188 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1189 | break; |
| 1190 | case 'b': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1191 | register_fw_filename(AMD_FW_PSP_BOOTLOADER, |
| 1192 | sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1193 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1194 | break; |
| 1195 | case 's': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1196 | register_fw_filename(AMD_FW_PSP_SMU_FIRMWARE, |
| 1197 | sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1198 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1199 | break; |
| 1200 | case 'r': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1201 | register_fw_filename(AMD_FW_PSP_RECOVERY, sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1202 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1203 | break; |
| 1204 | case 'k': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1205 | register_fw_filename(AMD_FW_PSP_RTM_PUBKEY, |
| 1206 | sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1207 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1208 | break; |
| 1209 | case 'c': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1210 | register_fw_filename(AMD_FW_PSP_SECURED_OS, |
| 1211 | sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1212 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1213 | break; |
| 1214 | case 'n': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1215 | register_fw_filename(AMD_FW_PSP_NVRAM, sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1216 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1217 | break; |
| 1218 | case 'd': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1219 | register_fw_filename(AMD_FW_PSP_SECURED_DEBUG, |
| 1220 | sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1221 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1222 | break; |
| 1223 | case 't': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1224 | register_fw_filename(AMD_FW_PSP_TRUSTLETS, sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1225 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1226 | break; |
| 1227 | case 'u': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1228 | register_fw_filename(AMD_FW_PSP_TRUSTLETKEY, |
| 1229 | sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1230 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1231 | break; |
| 1232 | case 'w': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1233 | register_fw_filename(AMD_FW_PSP_SMU_FIRMWARE2, |
| 1234 | sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1235 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1236 | break; |
| 1237 | case 'm': |
Marshall Dawson | dbae632 | 2019-03-04 10:31:03 -0700 | [diff] [blame] | 1238 | register_fw_filename(AMD_FW_PSP_SMUSCS, sub, optarg); |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1239 | sub = instance = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1240 | break; |
Marshall Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 1241 | case 'T': |
| 1242 | register_fw_fuse(optarg); |
| 1243 | fuse_defined = 1; |
| 1244 | sub = 0; |
| 1245 | break; |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1246 | 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 Roth | d3ce8c8 | 2019-07-13 20:13:07 -0600 | [diff] [blame] | 1337 | case 'Z': |
| 1338 | register_fw_filename(AMD_FW_PSP_VERSTAGE, sub, optarg); |
| 1339 | sub = instance = 0; |
| 1340 | break; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1341 | case 'o': |
| 1342 | output = optarg; |
| 1343 | break; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1344 | case 'f': |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1345 | ctx.rom_size = (uint32_t)strtoul(optarg, &tmp, 16); |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1346 | if (*tmp != '\0') { |
| 1347 | printf("Error: ROM size specified" |
| 1348 | " incorrectly (%s)\n\n", optarg); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1349 | retval = 1; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1350 | } |
| 1351 | break; |
Martin Roth | 0d3b118 | 2017-10-03 14:16:04 -0600 | [diff] [blame] | 1352 | 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 Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1361 | case 'h': |
| 1362 | usage(); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1363 | return 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1364 | default: |
| 1365 | break; |
| 1366 | } |
| 1367 | } |
| 1368 | |
Marshall Dawson | ef79fcc | 2019-04-01 10:16:41 -0600 | [diff] [blame] | 1369 | if (!fuse_defined) |
| 1370 | register_fw_fuse(DEFAULT_SOFT_FUSE_CHAIN); |
| 1371 | |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 1372 | if (!output) { |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1373 | printf("Error: Output value is not specified.\n\n"); |
| 1374 | retval = 1; |
| 1375 | } |
| 1376 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1377 | if (ctx.rom_size % 1024 != 0) { |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1378 | printf("Error: ROM Size (%d bytes) should be a multiple of" |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1379 | " 1024 bytes.\n\n", ctx.rom_size); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1380 | retval = 1; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1381 | } |
| 1382 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1383 | if (ctx.rom_size < MIN_ROM_KB * 1024) { |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1384 | printf("Error: ROM Size (%dKB) must be at least %dKB.\n\n", |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1385 | ctx.rom_size / 1024, MIN_ROM_KB); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1386 | retval = 1; |
| 1387 | } |
| 1388 | |
| 1389 | if (retval) { |
| 1390 | usage(); |
| 1391 | return retval; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1394 | printf(" AMDFWTOOL Using ROM size of %dKB\n", ctx.rom_size / 1024); |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1395 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1396 | rom_base_address = 0xFFFFFFFF - ctx.rom_size + 1; |
Martin Roth | 0d3b118 | 2017-10-03 14:16:04 -0600 | [diff] [blame] | 1397 | 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 Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1418 | ctx.rom = malloc(ctx.rom_size); |
| 1419 | if (!ctx.rom) { |
| 1420 | printf("Error: Failed to allocate memory\n"); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1421 | return 1; |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1422 | } |
| 1423 | memset(ctx.rom, 0xFF, ctx.rom_size); |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1424 | |
Martin Roth | 0d3b118 | 2017-10-03 14:16:04 -0600 | [diff] [blame] | 1425 | if (dir_location) |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1426 | romsig_offset = ctx.current = dir_location - rom_base_address; |
Martin Roth | 0d3b118 | 2017-10-03 14:16:04 -0600 | [diff] [blame] | 1427 | else |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1428 | romsig_offset = ctx.current = AMD_ROMSIG_OFFSET; |
| 1429 | printf(" AMDFWTOOL Using firmware directory location of 0x%08x\n", |
| 1430 | RUN_CURRENT(ctx)); |
Martin Roth | 0d3b118 | 2017-10-03 14:16:04 -0600 | [diff] [blame] | 1431 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1432 | amd_romsig = BUFF_OFFSET(ctx, romsig_offset); |
Marshall Dawson | 239286c | 2019-02-23 16:42:46 -0700 | [diff] [blame] | 1433 | 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 Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 1437 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1438 | integrate_firmwares(&ctx, amd_romsig, amd_fw_table); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1439 | |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1440 | ctx.current = ALIGN(ctx.current, 0x10000U); /* todo: is necessary? */ |
| 1441 | |
Marshall Dawson | 24f73d4 | 2019-04-01 10:48:43 -0600 | [diff] [blame] | 1442 | 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 Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1457 | |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 1458 | if (comboable) |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1459 | amd_romsig->comboable = BUFF_TO_RUN(ctx, pspdir); |
Marshall Dawson | 67d868d | 2019-02-28 11:43:40 -0700 | [diff] [blame] | 1460 | else |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1461 | amd_romsig->psp_entry = BUFF_TO_RUN(ctx, pspdir); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1462 | |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 1463 | #if PSP_COMBO |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1464 | psp_combo_directory *combo_dir = new_combo_dir(&ctx); |
| 1465 | amd_romsig->comboable = BUFF_TO_RUN(ctx, combo_dir); |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 1466 | /* 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 Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1470 | combo_dir->entries[0].lvl2_addr = BUFF_TO_RUN(ctx, pspdir); |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 1471 | |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 1472 | combo_dir->header.lookup = 1; |
Marshall Dawson | a378c22 | 2019-03-04 16:52:07 -0700 | [diff] [blame] | 1473 | fill_dir_header(combo_dir, 1, PSP2_COOKIE); |
Marshall Dawson | 0e02ce8 | 2019-03-04 16:50:37 -0700 | [diff] [blame] | 1474 | #endif |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 1475 | |
Marshall Dawson | ce2b2ba | 2019-03-19 14:45:31 -0600 | [diff] [blame] | 1476 | 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 Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1497 | targetfd = open(output, O_RDWR | O_CREAT | O_TRUNC, 0666); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1498 | if (targetfd >= 0) { |
Marshall Dawson | 2794a86 | 2019-03-04 16:53:15 -0700 | [diff] [blame] | 1499 | write(targetfd, amd_romsig, ctx.current - romsig_offset); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1500 | close(targetfd); |
| 1501 | } else { |
| 1502 | printf("Error: could not open file: %s\n", output); |
| 1503 | retval = 1; |
| 1504 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1505 | |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 1506 | free(rom); |
| 1507 | return retval; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1508 | } |