1 /* SPDX-License-Identifier: GPL-2.0-only */
4 * ROMSIG At ROMBASE + 0x[0,2,4,8]20000:
6 * +------------+---------------+----------------+------------+
7 * | 0x55AA55AA |EC ROM Address |GEC ROM Address |USB3 ROM |
8 * +------------+---------------+----------------+------------+
9 * | PSPDIR ADDR|PSPDIR ADDR(C) | BDT ADDR 0 | BDT ADDR 1 |
10 * +------------+---------------+----------------+------------+
11 * | BDT ADDR 2 | | BDT ADDR 3(C) | |
12 * +------------+---------------+----------------+------------+
13 * (C): Could be a combo header
15 * EC ROM should be 64K aligned.
17 * PSP directory (Where "PSPDIR ADDR" points)
18 * +------------+---------------+----------------+------------+
19 * | 'PSP$' | Fletcher | Count | Reserved |
20 * +------------+---------------+----------------+------------+
21 * | 0 | size | Base address | Reserved | Pubkey
22 * +------------+---------------+----------------+------------+
23 * | 1 | size | Base address | Reserved | Bootloader
24 * +------------+---------------+----------------+------------+
25 * | 8 | size | Base address | Reserved | Smu Firmware
26 * +------------+---------------+----------------+------------+
27 * | 3 | size | Base address | Reserved | Recovery Firmware
28 * +------------+---------------+----------------+------------+
31 * | Other PSP Firmware |
33 * +------------+---------------+----------------+------------+
34 * | 40 | size | Base address | Reserved |---+
35 * +------------+---------------+----------------+------------+ |
36 * :or 48(A/B A): size : Base address : Reserved : |
37 * + - - + - - + - - + - - + |
38 * : 4A(A/B B): size : Base address : Reserved : |
39 * +------------+---------------+----------------+------------+ |
40 * (A/B A) & (A/B B): Similar as 40, pointing to PSP level 2 |
44 * +------------+---------------+----------------+------------+ |
45 * | '2LP$' | Fletcher | Count | Reserved |<--+
46 * +------------+---------------+----------------+------------+
50 * | (2nd-level is not required on all families) |
52 * +------------+---------------+----------------+------------+
53 * BIOS Directory Table (BDT) is similar
56 * +------------+---------------+----------------+------------+
57 * | 'PSP2' | Fletcher | Count |Look up mode|
58 * +------------+---------------+----------------+------------+
60 * +------------+---------------+----------------+------------+
61 * | ID-Sel | PSP ID | PSPDIR ADDR | | 1st PSP directory
62 * +------------+---------------+----------------+------------+
63 * | ID-Sel | PSP ID | PSPDIR ADDR | | 2nd PSP directory
64 * +------------+---------------+----------------+------------+
68 * +------------+---------------+----------------+------------+
69 * BDT Combo is similar
72 #include <commonlib/bsd/helpers.h>
79 #include <sys/types.h>
86 #include "amdfwtool.h"
88 #define AMD_ROMSIG_OFFSET 0x20000
90 #define _MAX(A, B) (((A) > (B)) ? (A) : (B))
92 static void output_manifest(int manifest_fd
, amd_fw_entry
*fw_entry
);
95 * Beginning with Family 15h Models 70h-7F, a.k.a Stoney Ridge, the PSP
96 * can support an optional "combo" implementation. If the PSP sees the
97 * PSP2 cookie, it interprets the table as a roadmap to additional PSP
98 * tables. Using this, support for multiple product generations may be
99 * built into one image. If the PSP$ cookie is found, the table is a
100 * normal directory table.
102 * Modern generations supporting the combo directories require the
103 * pointer to be at offset 0x14 of the Embedded Firmware Structure,
104 * regardless of the type of directory used. The --use-combo
105 * argument enforces this placement.
107 * TODO: Future work may require fully implementing the PSP_COMBO feature.
111 * Creates the OSI Fletcher checksum. See 8473-1, Appendix C, section C.3.
112 * The checksum field of the passed PDU does not need to be reset to zero.
114 * The "Fletcher Checksum" was proposed in a paper by John G. Fletcher of
115 * Lawrence Livermore Labs. The Fletcher Checksum was proposed as an
116 * alternative to cyclical redundancy checks because it provides error-
117 * detection properties similar to cyclical redundancy checks but at the
118 * cost of a simple summation technique. Its characteristics were first
119 * published in IEEE Transactions on Communications in January 1982. One
120 * version has been adopted by ISO for use in the class-4 transport layer
121 * of the network protocol.
123 * This program expects:
124 * stdin: The input file to compute a checksum for. The input file
125 * not be longer than 256 bytes.
126 * stdout: Copied from the input file with the Fletcher's Checksum
127 * inserted 8 bytes after the beginning of the file.
128 * stderr: Used to print out error messages.
130 static uint32_t fletcher32(const void *data
, int length
)
136 const uint16_t *pptr
= data
;
144 index
= length
>= 359 ? 359 : length
;
150 c0
= (c0
& 0xFFFF) + (c0
>> 16);
151 c1
= (c1
& 0xFFFF) + (c1
>> 16);
154 /* Sums[0,1] mod 64K + overflow */
155 c0
= (c0
& 0xFFFF) + (c0
>> 16);
156 c1
= (c1
& 0xFFFF) + (c1
>> 16);
157 checksum
= (c1
<< 16) | c0
;
162 amd_fw_entry amd_psp_fw_table
[] = {
163 { .type
= AMD_FW_PSP_PUBKEY
, .level
= PSP_BOTH
| PSP_LVL2_AB
, .skip_hashing
= true },
164 { .type
= AMD_FW_PSP_BOOTLOADER
, .level
= PSP_BOTH
| PSP_LVL2_AB
,
165 .generate_manifest
= true },
166 { .type
= AMD_FW_PSP_SECURED_OS
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
167 { .type
= AMD_FW_PSP_RECOVERY
, .level
= PSP_LVL1
},
168 { .type
= AMD_FW_PSP_NVRAM
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
169 { .type
= AMD_FW_PSP_RTM_PUBKEY
, .level
= PSP_BOTH
},
170 { .type
= AMD_FW_PSP_SMU_FIRMWARE
, .subprog
= 0, .level
= PSP_BOTH
| PSP_LVL2_AB
,
171 .generate_manifest
= true },
172 { .type
= AMD_FW_PSP_SMU_FIRMWARE
, .subprog
= 1, .level
= PSP_BOTH
| PSP_LVL2_AB
},
173 { .type
= AMD_FW_PSP_SMU_FIRMWARE
, .subprog
= 2, .level
= PSP_BOTH
| PSP_LVL2_AB
},
174 { .type
= AMD_FW_PSP_SECURED_DEBUG
, .level
= PSP_LVL2
| PSP_LVL2_AB
,
175 .skip_hashing
= true },
176 { .type
= AMD_FW_ABL_PUBKEY
, .level
= PSP_BOTH
| PSP_BOTH_AB
},
177 { .type
= AMD_PSP_FUSE_CHAIN
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
178 { .type
= AMD_FW_PSP_TRUSTLETS
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
179 { .type
= AMD_FW_PSP_TRUSTLETKEY
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
180 { .type
= AMD_FW_PSP_SMU_FIRMWARE2
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
181 { .type
= AMD_FW_PSP_SMU_FIRMWARE2
, .subprog
= 1, .level
= PSP_BOTH
| PSP_LVL2_AB
},
182 { .type
= AMD_FW_PSP_SMU_FIRMWARE2
, .subprog
= 2, .level
= PSP_BOTH
| PSP_LVL2_AB
},
183 { .type
= AMD_BOOT_DRIVER
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
184 { .type
= AMD_SOC_DRIVER
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
185 { .type
= AMD_DEBUG_DRIVER
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
186 { .type
= AMD_INTERFACE_DRIVER
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
187 { .type
= AMD_DEBUG_UNLOCK
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
188 { .type
= AMD_HW_IPCFG
, .subprog
= 0, .level
= PSP_LVL2
| PSP_LVL2_AB
},
189 { .type
= AMD_HW_IPCFG
, .subprog
= 1, .level
= PSP_LVL2
| PSP_LVL2_AB
},
190 { .type
= AMD_WRAPPED_IKEK
, .level
= PSP_BOTH
| PSP_LVL2_AB
, .skip_hashing
= true },
191 { .type
= AMD_TOKEN_UNLOCK
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
192 { .type
= AMD_SEC_GASKET
, .subprog
= 0, .level
= PSP_BOTH
| PSP_LVL2_AB
},
193 { .type
= AMD_SEC_GASKET
, .subprog
= 1, .level
= PSP_BOTH
| PSP_LVL2_AB
},
194 { .type
= AMD_SEC_GASKET
, .subprog
= 2, .level
= PSP_BOTH
| PSP_LVL2_AB
},
195 { .type
= AMD_MP2_FW
, .subprog
= 0, .level
= PSP_LVL2
| PSP_LVL2_AB
},
196 { .type
= AMD_MP2_FW
, .subprog
= 1, .level
= PSP_LVL2
| PSP_LVL2_AB
},
197 { .type
= AMD_MP2_FW
, .subprog
= 2, .level
= PSP_LVL2
| PSP_LVL2_AB
},
198 { .type
= AMD_DRIVER_ENTRIES
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
199 { .type
= AMD_FW_KVM_IMAGE
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
200 { .type
= AMD_FW_MP5
, .subprog
= 0, .level
= PSP_BOTH
| PSP_BOTH_AB
},
201 { .type
= AMD_FW_MP5
, .subprog
= 1, .level
= PSP_BOTH
| PSP_BOTH_AB
},
202 { .type
= AMD_FW_MP5
, .subprog
= 2, .level
= PSP_BOTH
| PSP_BOTH_AB
},
203 { .type
= AMD_S0I3_DRIVER
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
204 { .type
= AMD_ABL0
, .level
= PSP_BOTH
| PSP_LVL2_AB
,
205 .generate_manifest
= true },
206 { .type
= AMD_ABL1
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
207 { .type
= AMD_ABL2
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
208 { .type
= AMD_ABL3
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
209 { .type
= AMD_ABL4
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
210 { .type
= AMD_ABL5
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
211 { .type
= AMD_ABL6
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
212 { .type
= AMD_ABL7
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
213 { .type
= AMD_SEV_DATA
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
214 { .type
= AMD_SEV_CODE
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
215 { .type
= AMD_FW_PSP_WHITELIST
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
216 { .type
= AMD_VBIOS_BTLOADER
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
217 { .type
= AMD_FW_DXIO
, .level
= PSP_BOTH
| PSP_BOTH_AB
},
218 { .type
= AMD_FW_USB_PHY
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
219 { .type
= AMD_FW_TOS_SEC_POLICY
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
220 { .type
= AMD_FW_DRTM_TA
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
221 { .type
= AMD_FW_KEYDB_BL
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
222 { .type
= AMD_FW_KEYDB_TOS
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
223 { .type
= AMD_FW_PSP_VERSTAGE
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
224 { .type
= AMD_FW_VERSTAGE_SIG
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
225 { .type
= AMD_RPMC_NVRAM
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
226 { .type
= AMD_FW_SPL
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
227 { .type
= AMD_FW_DMCU_ERAM
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
228 { .type
= AMD_FW_DMCU_ISR
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
229 { .type
= AMD_FW_MSMU
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
230 { .type
= AMD_FW_SPIROM_CFG
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
231 { .type
= AMD_FW_MPIO
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
232 { .type
= AMD_FW_PSP_SMUSCS
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
233 { .type
= AMD_FW_DMCUB
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
234 { .type
= AMD_FW_PSP_BOOTLOADER_AB
, .level
= PSP_LVL2
| PSP_LVL2_AB
,
235 .generate_manifest
= true },
236 { .type
= AMD_RIB
, .subprog
= 0, .level
= PSP_LVL2
| PSP_LVL2_AB
},
237 { .type
= AMD_RIB
, .subprog
= 1, .level
= PSP_LVL2
| PSP_LVL2_AB
},
238 { .type
= AMD_FW_MPDMA_TF
, .level
= PSP_BOTH
| PSP_BOTH_AB
},
239 { .type
= AMD_TA_IKEK
, .level
= PSP_BOTH
| PSP_LVL2_AB
, .skip_hashing
= true },
240 { .type
= AMD_FW_GMI3_PHY
, .level
= PSP_BOTH
| PSP_BOTH_AB
},
241 { .type
= AMD_FW_MPDMA_PM
, .level
= PSP_BOTH
| PSP_BOTH_AB
},
242 { .type
= AMD_FW_AMF_SRAM
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
243 { .type
= AMD_FW_AMF_DRAM
, .inst
= 0, .level
= PSP_LVL2
| PSP_LVL2_AB
},
244 { .type
= AMD_FW_AMF_DRAM
, .inst
= 1, .level
= PSP_LVL2
| PSP_LVL2_AB
},
245 { .type
= AMD_FW_FCFG_TABLE
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
246 { .type
= AMD_FW_AMF_WLAN
, .inst
= 0, .level
= PSP_LVL2
| PSP_LVL2_AB
},
247 { .type
= AMD_FW_AMF_WLAN
, .inst
= 1, .level
= PSP_LVL2
| PSP_LVL2_AB
},
248 { .type
= AMD_FW_AMF_MFD
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
249 { .type
= AMD_TA_IKEK
, .level
= PSP_BOTH
| PSP_LVL2_AB
, .skip_hashing
= true },
250 { .type
= AMD_FW_MPCCX
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
251 { .type
= AMD_FW_LSDMA
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
252 { .type
= AMD_FW_C20_MP
, .level
= PSP_BOTH
| PSP_LVL2_AB
},
253 { .type
= AMD_FW_MINIMSMU
, .inst
= 0, .level
= PSP_BOTH
| PSP_LVL2_AB
},
254 { .type
= AMD_FW_MINIMSMU
, .inst
= 1, .level
= PSP_BOTH
| PSP_LVL2_AB
},
255 { .type
= AMD_FW_SRAM_FW_EXT
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
256 { .type
= AMD_FW_UMSMU
, .level
= PSP_LVL2
| PSP_LVL2_AB
},
257 { .type
= AMD_FW_INVALID
},
260 amd_fw_entry amd_fw_table
[] = {
261 { .type
= AMD_FW_XHCI
},
262 { .type
= AMD_FW_IMC
},
263 { .type
= AMD_FW_GEC
},
264 { .type
= AMD_FW_INVALID
},
267 amd_bios_entry amd_bios_table
[] = {
268 { .type
= AMD_BIOS_RTM_PUBKEY
, .inst
= 0, .level
= BDT_BOTH
},
269 { .type
= AMD_BIOS_SIG
, .inst
= 0, .level
= BDT_BOTH
},
270 { .type
= AMD_BIOS_APCB
, .inst
= 0, .level
= BDT_BOTH
},
271 { .type
= AMD_BIOS_APCB
, .inst
= 1, .level
= BDT_BOTH
},
272 { .type
= AMD_BIOS_APCB
, .inst
= 2, .level
= BDT_BOTH
},
273 { .type
= AMD_BIOS_APCB
, .inst
= 3, .level
= BDT_BOTH
},
274 { .type
= AMD_BIOS_APCB
, .inst
= 4, .level
= BDT_BOTH
},
275 { .type
= AMD_BIOS_APCB
, .inst
= 5, .level
= BDT_BOTH
},
276 { .type
= AMD_BIOS_APCB
, .inst
= 6, .level
= BDT_BOTH
},
277 { .type
= AMD_BIOS_APCB
, .inst
= 7, .level
= BDT_BOTH
},
278 { .type
= AMD_BIOS_APCB
, .inst
= 8, .level
= BDT_BOTH
},
279 { .type
= AMD_BIOS_APCB
, .inst
= 9, .level
= BDT_BOTH
},
280 { .type
= AMD_BIOS_APCB
, .inst
= 10, .level
= BDT_BOTH
},
281 { .type
= AMD_BIOS_APCB
, .inst
= 11, .level
= BDT_BOTH
},
282 { .type
= AMD_BIOS_APCB
, .inst
= 12, .level
= BDT_BOTH
},
283 { .type
= AMD_BIOS_APCB
, .inst
= 13, .level
= BDT_BOTH
},
284 { .type
= AMD_BIOS_APCB
, .inst
= 14, .level
= BDT_BOTH
},
285 { .type
= AMD_BIOS_APCB
, .inst
= 15, .level
= BDT_BOTH
},
286 { .type
= AMD_BIOS_APCB_BK
, .inst
= 0, .level
= BDT_BOTH
},
287 { .type
= AMD_BIOS_APCB_BK
, .inst
= 1, .level
= BDT_BOTH
},
288 { .type
= AMD_BIOS_APCB_BK
, .inst
= 2, .level
= BDT_BOTH
},
289 { .type
= AMD_BIOS_APCB_BK
, .inst
= 3, .level
= BDT_BOTH
},
290 { .type
= AMD_BIOS_APCB_BK
, .inst
= 4, .level
= BDT_BOTH
},
291 { .type
= AMD_BIOS_APCB_BK
, .inst
= 5, .level
= BDT_BOTH
},
292 { .type
= AMD_BIOS_APCB_BK
, .inst
= 6, .level
= BDT_BOTH
},
293 { .type
= AMD_BIOS_APCB_BK
, .inst
= 7, .level
= BDT_BOTH
},
294 { .type
= AMD_BIOS_APCB_BK
, .inst
= 8, .level
= BDT_BOTH
},
295 { .type
= AMD_BIOS_APCB_BK
, .inst
= 9, .level
= BDT_BOTH
},
296 { .type
= AMD_BIOS_APCB_BK
, .inst
= 10, .level
= BDT_BOTH
},
297 { .type
= AMD_BIOS_APCB_BK
, .inst
= 11, .level
= BDT_BOTH
},
298 { .type
= AMD_BIOS_APCB_BK
, .inst
= 12, .level
= BDT_BOTH
},
299 { .type
= AMD_BIOS_APCB_BK
, .inst
= 13, .level
= BDT_BOTH
},
300 { .type
= AMD_BIOS_APCB_BK
, .inst
= 14, .level
= BDT_BOTH
},
301 { .type
= AMD_BIOS_APCB_BK
, .inst
= 15, .level
= BDT_BOTH
},
302 { .type
= AMD_BIOS_APOB
, .level
= BDT_BOTH
},
303 { .type
= AMD_BIOS_BIN
,
304 .reset
= 1, .copy
= 1, .zlib
= 1, .inst
= 0, .level
= BDT_BOTH
},
305 { .type
= AMD_BIOS_APOB_NV
, .level
= BDT_LVL2
},
306 { .type
= AMD_BIOS_PMUI
, .inst
= 1, .subpr
= 0, .level
= BDT_BOTH
},
307 { .type
= AMD_BIOS_PMUD
, .inst
= 1, .subpr
= 0, .level
= BDT_BOTH
},
308 { .type
= AMD_BIOS_PMUI
, .inst
= 2, .subpr
= 0, .level
= BDT_BOTH
},
309 { .type
= AMD_BIOS_PMUD
, .inst
= 2, .subpr
= 0, .level
= BDT_BOTH
},
310 { .type
= AMD_BIOS_PMUI
, .inst
= 3, .subpr
= 0, .level
= BDT_BOTH
},
311 { .type
= AMD_BIOS_PMUD
, .inst
= 3, .subpr
= 0, .level
= BDT_BOTH
},
312 { .type
= AMD_BIOS_PMUI
, .inst
= 4, .subpr
= 0, .level
= BDT_BOTH
},
313 { .type
= AMD_BIOS_PMUD
, .inst
= 4, .subpr
= 0, .level
= BDT_BOTH
},
314 { .type
= AMD_BIOS_PMUI
, .inst
= 5, .subpr
= 0, .level
= BDT_BOTH
},
315 { .type
= AMD_BIOS_PMUD
, .inst
= 5, .subpr
= 0, .level
= BDT_BOTH
},
316 { .type
= AMD_BIOS_PMUI
, .inst
= 6, .subpr
= 0, .level
= BDT_BOTH
},
317 { .type
= AMD_BIOS_PMUD
, .inst
= 6, .subpr
= 0, .level
= BDT_BOTH
},
318 { .type
= AMD_BIOS_PMUI
, .inst
= 7, .subpr
= 0, .level
= BDT_BOTH
},
319 { .type
= AMD_BIOS_PMUD
, .inst
= 7, .subpr
= 0, .level
= BDT_BOTH
},
320 { .type
= AMD_BIOS_PMUI
, .inst
= 9, .subpr
= 0, .level
= BDT_BOTH
},
321 { .type
= AMD_BIOS_PMUD
, .inst
= 9, .subpr
= 0, .level
= BDT_BOTH
},
322 { .type
= AMD_BIOS_PMUI
, .inst
= 10, .subpr
= 0, .level
= BDT_BOTH
},
323 { .type
= AMD_BIOS_PMUD
, .inst
= 10, .subpr
= 0, .level
= BDT_BOTH
},
324 { .type
= AMD_BIOS_PMUI
, .inst
= 11, .subpr
= 0, .level
= BDT_BOTH
},
325 { .type
= AMD_BIOS_PMUD
, .inst
= 11, .subpr
= 0, .level
= BDT_BOTH
},
326 { .type
= AMD_BIOS_PMUI
, .inst
= 12, .subpr
= 0, .level
= BDT_BOTH
},
327 { .type
= AMD_BIOS_PMUD
, .inst
= 12, .subpr
= 0, .level
= BDT_BOTH
},
328 { .type
= AMD_BIOS_PMUI
, .inst
= 13, .subpr
= 0, .level
= BDT_BOTH
},
329 { .type
= AMD_BIOS_PMUD
, .inst
= 13, .subpr
= 0, .level
= BDT_BOTH
},
330 { .type
= AMD_BIOS_PMUI
, .inst
= 1, .subpr
= 1, .level
= BDT_BOTH
},
331 { .type
= AMD_BIOS_PMUD
, .inst
= 1, .subpr
= 1, .level
= BDT_BOTH
},
332 { .type
= AMD_BIOS_PMUI
, .inst
= 2, .subpr
= 1, .level
= BDT_BOTH
},
333 { .type
= AMD_BIOS_PMUD
, .inst
= 2, .subpr
= 1, .level
= BDT_BOTH
},
334 { .type
= AMD_BIOS_PMUI
, .inst
= 3, .subpr
= 1, .level
= BDT_BOTH
},
335 { .type
= AMD_BIOS_PMUD
, .inst
= 3, .subpr
= 1, .level
= BDT_BOTH
},
336 { .type
= AMD_BIOS_PMUI
, .inst
= 4, .subpr
= 1, .level
= BDT_BOTH
},
337 { .type
= AMD_BIOS_PMUD
, .inst
= 4, .subpr
= 1, .level
= BDT_BOTH
},
338 { .type
= AMD_BIOS_PMUI
, .inst
= 5, .subpr
= 1, .level
= BDT_BOTH
},
339 { .type
= AMD_BIOS_PMUD
, .inst
= 5, .subpr
= 1, .level
= BDT_BOTH
},
340 { .type
= AMD_BIOS_PMUI
, .inst
= 6, .subpr
= 1, .level
= BDT_BOTH
},
341 { .type
= AMD_BIOS_PMUD
, .inst
= 6, .subpr
= 1, .level
= BDT_BOTH
},
342 { .type
= AMD_BIOS_PMUI
, .inst
= 7, .subpr
= 1, .level
= BDT_BOTH
},
343 { .type
= AMD_BIOS_PMUD
, .inst
= 7, .subpr
= 1, .level
= BDT_BOTH
},
344 { .type
= AMD_BIOS_PMUI
, .inst
= 9, .subpr
= 1, .level
= BDT_BOTH
},
345 { .type
= AMD_BIOS_PMUD
, .inst
= 9, .subpr
= 1, .level
= BDT_BOTH
},
346 { .type
= AMD_BIOS_PMUI
, .inst
= 10, .subpr
= 1, .level
= BDT_BOTH
},
347 { .type
= AMD_BIOS_PMUD
, .inst
= 10, .subpr
= 1, .level
= BDT_BOTH
},
348 { .type
= AMD_BIOS_PMUI
, .inst
= 11, .subpr
= 1, .level
= BDT_BOTH
},
349 { .type
= AMD_BIOS_PMUD
, .inst
= 11, .subpr
= 1, .level
= BDT_BOTH
},
350 { .type
= AMD_BIOS_PMUI
, .inst
= 12, .subpr
= 1, .level
= BDT_BOTH
},
351 { .type
= AMD_BIOS_PMUD
, .inst
= 12, .subpr
= 1, .level
= BDT_BOTH
},
352 { .type
= AMD_BIOS_PMUI
, .inst
= 13, .subpr
= 1, .level
= BDT_BOTH
},
353 { .type
= AMD_BIOS_PMUD
, .inst
= 13, .subpr
= 1, .level
= BDT_BOTH
},
354 { .type
= AMD_BIOS_UCODE
, .inst
= 0, .level
= BDT_LVL2
},
355 { .type
= AMD_BIOS_UCODE
, .inst
= 1, .level
= BDT_LVL2
},
356 { .type
= AMD_BIOS_UCODE
, .inst
= 2, .level
= BDT_LVL2
},
357 { .type
= AMD_BIOS_UCODE
, .inst
= 3, .level
= BDT_LVL2
},
358 { .type
= AMD_BIOS_UCODE
, .inst
= 4, .level
= BDT_LVL2
},
359 { .type
= AMD_BIOS_UCODE
, .inst
= 5, .level
= BDT_LVL2
},
360 { .type
= AMD_BIOS_UCODE
, .inst
= 6, .level
= BDT_LVL2
},
361 { .type
= AMD_BIOS_MP2_CFG
, .level
= BDT_LVL2
},
362 { .type
= AMD_BIOS_PSP_SHARED_MEM
, .inst
= 0, .level
= BDT_BOTH
},
363 { .type
= AMD_BIOS_INVALID
},
366 #define RUN_BASE(ctx) (0xFFFFFFFF - (ctx).rom_size + 1)
367 #define RUN_OFFSET_MODE(ctx, offset, mode) \
368 ((mode) == AMD_ADDR_PHYSICAL ? RUN_BASE(ctx) + (offset) : \
369 ((mode) == AMD_ADDR_REL_BIOS ? (offset) : \
370 ((mode) == AMD_ADDR_REL_TAB ? (offset) - (ctx).current_table : (offset))))
371 #define RUN_OFFSET(ctx, offset) RUN_OFFSET_MODE((ctx), (offset), (ctx).address_mode)
372 #define RUN_TO_OFFSET(ctx, run) ((ctx).address_mode == AMD_ADDR_PHYSICAL ? \
373 (run) - RUN_BASE(ctx) : (run)) /* TODO: */
374 #define RUN_CURRENT(ctx) RUN_OFFSET((ctx), (ctx).current)
375 /* The mode in entry can not be higher than the header's.
376 For example, if table mode is 0, all the entry mode will be 0. */
377 #define RUN_CURRENT_MODE(ctx, mode) RUN_OFFSET_MODE((ctx), (ctx).current, \
378 (ctx).address_mode < (mode) ? (ctx).address_mode : (mode))
379 #define BUFF_OFFSET(ctx, offset) ((void *)((ctx).rom + (offset)))
380 #define BUFF_CURRENT(ctx) BUFF_OFFSET((ctx), (ctx).current)
381 #define BUFF_TO_RUN(ctx, ptr) RUN_OFFSET((ctx), ((char *)(ptr) - (ctx).rom))
382 #define BUFF_TO_RUN_MODE(ctx, ptr, mode) RUN_OFFSET_MODE((ctx), ((char *)(ptr) - (ctx).rom), \
383 (ctx).address_mode < (mode) ? (ctx).address_mode : (mode))
384 #define BUFF_ROOM(ctx) ((ctx).rom_size - (ctx).current)
385 /* Only set the address mode in entry if the table is mode 2. */
386 #define SET_ADDR_MODE(table, mode) \
387 ((table)->header.additional_info_fields.address_mode == \
388 AMD_ADDR_REL_TAB ? (mode) : 0)
389 #define SET_ADDR_MODE_BY_TABLE(table) \
390 SET_ADDR_MODE((table), (table)->header.additional_info_fields.address_mode)
393 static void free_psp_firmware_filenames(amd_fw_entry
*fw_table
)
397 for (index
= fw_table
; index
->type
!= AMD_FW_INVALID
; index
++) {
398 if (index
->filename
&&
399 index
->type
!= AMD_FW_VERSTAGE_SIG
&&
400 index
->type
!= AMD_FW_PSP_VERSTAGE
&&
401 index
->type
!= AMD_FW_SPL
&&
402 index
->type
!= AMD_FW_PSP_WHITELIST
) {
403 free(index
->filename
);
404 index
->filename
= NULL
;
409 static void free_bdt_firmware_filenames(amd_bios_entry
*fw_table
)
411 amd_bios_entry
*index
;
413 for (index
= fw_table
; index
->type
!= AMD_BIOS_INVALID
; index
++) {
414 if (index
->filename
&&
415 index
->type
!= AMD_BIOS_APCB
&&
416 index
->type
!= AMD_BIOS_BIN
&&
417 index
->type
!= AMD_BIOS_APCB_BK
&&
418 index
->type
!= AMD_BIOS_UCODE
) {
419 free(index
->filename
);
420 index
->filename
= NULL
;
425 static void amdfwtool_cleanup(context
*ctx
)
430 /* Free the filename. */
431 free_psp_firmware_filenames(amd_psp_fw_table
);
432 free_bdt_firmware_filenames(amd_bios_table
);
434 free(ctx
->amd_psp_fw_table_clean
);
435 ctx
->amd_psp_fw_table_clean
= NULL
;
436 free(ctx
->amd_bios_table_clean
);
437 ctx
->amd_bios_table_clean
= NULL
;
440 void assert_fw_entry(uint32_t count
, uint32_t max
, context
*ctx
)
443 fprintf(stderr
, "Error: BIOS entries (%d) exceeds max allowed items "
444 "(%d)\n", count
, max
);
445 amdfwtool_cleanup(ctx
);
450 static void set_current_pointer(context
*ctx
, uint32_t value
)
452 if (ctx
->current_pointer_saved
!= 0xFFFFFFFF &&
453 ctx
->current_pointer_saved
!= ctx
->current
) {
454 fprintf(stderr
, "Error: The pointer is changed elsewhere\n");
455 amdfwtool_cleanup(ctx
);
459 ctx
->current
= value
;
461 if (ctx
->current
> ctx
->rom_size
) {
462 fprintf(stderr
, "Error: Packing data causes overflow\n");
463 amdfwtool_cleanup(ctx
);
467 ctx
->current_pointer_saved
= ctx
->current
;
470 static void adjust_current_pointer(context
*ctx
, uint32_t add
, uint32_t align
)
473 set_current_pointer(ctx
, ALIGN_UP(ctx
->current
+ add
, align
));
476 static void *new_psp_dir(context
*ctx
, int multi
, uint32_t cookie
)
481 * Force both onto boundary when multi. Primary table is after
482 * updatable table, so alignment ensures primary can stay intact
483 * if secondary is reprogrammed.
486 adjust_current_pointer(ctx
, 0, TABLE_ERASE_ALIGNMENT
);
488 adjust_current_pointer(ctx
, 0, TABLE_ALIGNMENT
);
490 ptr
= BUFF_CURRENT(*ctx
);
491 ((psp_directory_header
*)ptr
)->cookie
= cookie
;
492 ((psp_directory_header
*)ptr
)->num_entries
= 0;
493 ((psp_directory_header
*)ptr
)->additional_info
= 0;
494 ((psp_directory_header
*)ptr
)->additional_info_fields
.address_mode
= ctx
->address_mode
;
495 adjust_current_pointer(ctx
,
496 sizeof(psp_directory_header
) + MAX_PSP_ENTRIES
* sizeof(psp_directory_entry
),
501 static void *new_ish_dir(context
*ctx
)
504 adjust_current_pointer(ctx
, 0, TABLE_ALIGNMENT
);
505 ptr
= BUFF_CURRENT(*ctx
);
506 adjust_current_pointer(ctx
, TABLE_ALIGNMENT
, 1);
511 static void *new_combo_dir(context
*ctx
, uint32_t cookie
)
515 adjust_current_pointer(ctx
, 0, TABLE_ALIGNMENT
);
516 ptr
= BUFF_CURRENT(*ctx
);
517 ((psp_combo_header
*)ptr
)->cookie
= cookie
;
518 adjust_current_pointer(ctx
,
519 sizeof(psp_combo_header
) + MAX_COMBO_ENTRIES
* sizeof(psp_combo_entry
),
524 static void fill_dir_header(void *directory
, uint32_t count
, context
*ctx
)
526 psp_combo_directory
*cdir
= directory
;
527 psp_directory_table
*dir
= directory
;
528 bios_directory_table
*bdir
= directory
;
529 /* The cookies have same offsets. */
530 uint32_t cookie
= ((psp_directory_table
*)directory
)->header
.cookie
;
531 uint32_t table_size
= 0;
533 if (ctx
== NULL
|| directory
== NULL
) {
534 fprintf(stderr
, "Calling %s with NULL pointers\n", __func__
);
538 /* The table size needs to be 0x1000 aligned. So align the end of table. */
539 adjust_current_pointer(ctx
, 0, TABLE_ALIGNMENT
);
544 /* lookup mode is hardcoded for now. */
545 cdir
->header
.lookup
= 1;
546 cdir
->header
.num_entries
= count
;
547 cdir
->header
.reserved
[0] = 0;
548 cdir
->header
.reserved
[1] = 0;
549 /* checksum everything that comes after the Checksum field */
550 cdir
->header
.checksum
= fletcher32(&cdir
->header
.num_entries
,
551 count
* sizeof(psp_combo_entry
)
552 + sizeof(cdir
->header
.num_entries
)
553 + sizeof(cdir
->header
.lookup
)
554 + 2 * sizeof(cdir
->header
.reserved
[0]));
558 /* The table size is only set once. Later calls only update
559 * the count and fletcher. So does the BIOS table. */
560 if (dir
->header
.additional_info_fields
.dir_size
== 0) {
561 table_size
= ctx
->current
- ctx
->current_table
;
562 if ((table_size
% TABLE_ALIGNMENT
) != 0 &&
563 (table_size
/ TABLE_ALIGNMENT
) != 0) {
564 fprintf(stderr
, "The PSP table size should be 4K aligned\n");
565 amdfwtool_cleanup(ctx
);
568 dir
->header
.additional_info_fields
.dir_size
=
569 table_size
/ TABLE_ALIGNMENT
;
571 dir
->header
.num_entries
= count
;
572 dir
->header
.additional_info_fields
.spi_block_size
= 1;
573 dir
->header
.additional_info_fields
.base_addr
= 0;
574 /* checksum everything that comes after the Checksum field */
575 dir
->header
.checksum
= fletcher32(&dir
->header
.num_entries
,
576 count
* sizeof(psp_directory_entry
)
577 + sizeof(dir
->header
.num_entries
)
578 + sizeof(dir
->header
.additional_info
));
582 if (bdir
->header
.additional_info_fields
.dir_size
== 0) {
583 table_size
= ctx
->current
- ctx
->current_table
;
584 if ((table_size
% TABLE_ALIGNMENT
) != 0 &&
585 table_size
/ TABLE_ALIGNMENT
!= 0) {
586 fprintf(stderr
, "The BIOS table size should be 4K aligned\n");
587 amdfwtool_cleanup(ctx
);
590 bdir
->header
.additional_info_fields
.dir_size
=
591 table_size
/ TABLE_ALIGNMENT
;
593 bdir
->header
.num_entries
= count
;
594 bdir
->header
.additional_info_fields
.spi_block_size
= 1;
595 bdir
->header
.additional_info_fields
.base_addr
= 0;
596 /* checksum everything that comes after the Checksum field */
597 bdir
->header
.checksum
= fletcher32(&bdir
->header
.num_entries
,
598 count
* sizeof(bios_directory_entry
)
599 + sizeof(bdir
->header
.num_entries
)
600 + sizeof(bdir
->header
.additional_info
));
606 static void fill_psp_directory_to_efs(embedded_firmware
*amd_romsig
, void *pspdir
,
607 context
*ctx
, amd_cb_config
*cb_config
)
609 switch (cb_config
->soc_id
) {
610 case PLATFORM_UNKNOWN
:
611 amd_romsig
->psp_directory
=
612 BUFF_TO_RUN_MODE(*ctx
, pspdir
, AMD_ADDR_REL_BIOS
);
614 case PLATFORM_CEZANNE
:
615 case PLATFORM_MENDOCINO
:
616 case PLATFORM_PHOENIX
:
617 case PLATFORM_GLINDA
:
618 case PLATFORM_CARRIZO
:
619 case PLATFORM_STONEYRIDGE
:
621 case PLATFORM_PICASSO
:
622 case PLATFORM_LUCIENNE
:
623 case PLATFORM_RENOIR
:
626 /* for combo, it is also combo_psp_directory */
627 amd_romsig
->new_psp_directory
=
628 BUFF_TO_RUN_MODE(*ctx
, pspdir
, AMD_ADDR_REL_BIOS
);
633 static void fill_bios_directory_to_efs(embedded_firmware
*amd_romsig
, void *biosdir
,
634 context
*ctx
, amd_cb_config
*cb_config
)
636 switch (cb_config
->soc_id
) {
637 case PLATFORM_RENOIR
:
638 case PLATFORM_LUCIENNE
:
639 case PLATFORM_CEZANNE
:
641 if (!cb_config
->recovery_ab
)
642 amd_romsig
->bios3_entry
=
643 BUFF_TO_RUN_MODE(*ctx
, biosdir
, AMD_ADDR_REL_BIOS
);
645 case PLATFORM_MENDOCINO
:
646 case PLATFORM_PHOENIX
:
647 case PLATFORM_GLINDA
:
649 case PLATFORM_CARRIZO
:
650 case PLATFORM_STONEYRIDGE
:
652 case PLATFORM_PICASSO
:
654 amd_romsig
->bios1_entry
=
655 BUFF_TO_RUN_MODE(*ctx
, biosdir
, AMD_ADDR_REL_BIOS
);
660 static uint32_t get_psp_id(enum platform soc_id
)
665 case PLATFORM_PICASSO
:
668 case PLATFORM_RENOIR
:
669 case PLATFORM_LUCIENNE
:
672 case PLATFORM_CEZANNE
:
675 case PLATFORM_MENDOCINO
:
678 case PLATFORM_STONEYRIDGE
:
681 case PLATFORM_GLINDA
:
684 case PLATFORM_PHOENIX
:
690 case PLATFORM_CARRIZO
:
698 static void integrate_firmwares(context
*ctx
,
699 embedded_firmware
*romsig
,
700 amd_fw_entry
*fw_table
)
705 adjust_current_pointer(ctx
, 0, BLOB_ALIGNMENT
);
707 for (i
= 0; fw_table
[i
].type
!= AMD_FW_INVALID
; i
++) {
708 if (fw_table
[i
].filename
!= NULL
) {
709 switch (fw_table
[i
].type
) {
711 adjust_current_pointer(ctx
, 0, 0x10000U
);
712 romsig
->imc_entry
= RUN_CURRENT(*ctx
);
715 romsig
->gec_entry
= RUN_CURRENT(*ctx
);
718 romsig
->xhci_entry
= RUN_CURRENT(*ctx
);
725 bytes
= copy_blob(BUFF_CURRENT(*ctx
),
726 fw_table
[i
].filename
, BUFF_ROOM(*ctx
));
728 amdfwtool_cleanup(ctx
);
732 adjust_current_pointer(ctx
, bytes
, BLOB_ALIGNMENT
);
737 static void output_manifest(int manifest_fd
, amd_fw_entry
*fw_entry
)
739 struct amd_fw_header hdr
;
743 blob_fd
= open(fw_entry
->filename
, O_RDONLY
);
745 fprintf(stderr
, "Error opening file: %s: %s\n",
746 fw_entry
->filename
, strerror(errno
));
750 bytes
= read(blob_fd
, &hdr
, sizeof(hdr
));
751 if (bytes
!= sizeof(hdr
)) {
753 fprintf(stderr
, "Error while reading %s\n", fw_entry
->filename
);
757 dprintf(manifest_fd
, "type: 0x%02x ver:%02x.%02x.%02x.%02x\n",
758 fw_entry
->type
, hdr
.version
[3], hdr
.version
[2],
759 hdr
.version
[1], hdr
.version
[0]);
765 static void dump_blob_version(char *manifest_file
, amd_fw_entry
*fw_table
)
770 manifest_fd
= open(manifest_file
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
771 if (manifest_fd
< 0) {
772 fprintf(stderr
, "Error opening file: %s: %s\n",
773 manifest_file
, strerror(errno
));
777 for (index
= fw_table
; index
->type
!= AMD_FW_INVALID
; index
++) {
778 if (!(index
->filename
))
781 if (index
->generate_manifest
== true)
782 output_manifest(manifest_fd
, index
);
789 static void dump_psp_firmwares(amd_fw_entry
*fw_table
)
793 printf("PSP firmware components:\n");
794 for (index
= fw_table
; index
->type
!= AMD_FW_INVALID
; index
++) {
795 if (index
->type
== AMD_PSP_FUSE_CHAIN
)
796 printf(" %2x: level=%x, subprog=%x, inst=%x\n",
797 index
->type
, index
->level
, index
->subprog
, index
->inst
);
798 else if (index
->filename
)
799 printf(" %2x: level=%x, subprog=%x, inst=%x, %s\n",
800 index
->type
, index
->level
, index
->subprog
, index
->inst
,
805 static void dump_bdt_firmwares(amd_bios_entry
*fw_table
)
807 amd_bios_entry
*index
;
809 printf("BIOS Directory Table (BDT) components:\n");
810 for (index
= fw_table
; index
->type
!= AMD_BIOS_INVALID
; index
++) {
812 printf(" %2x: level=%x, %s\n",
813 index
->type
, index
->level
, index
->filename
);
817 static void dump_image_addresses(context
*ctx
)
819 printf("romsig offset:%lx\n", BUFF_TO_RUN(*ctx
, ctx
->amd_romsig_ptr
));
820 printf("PSP L1 offset:%lx\n", BUFF_TO_RUN(*ctx
, ctx
->pspdir
));
821 if (ctx
->pspdir2
!= NULL
)
822 printf("PSP L2(A) offset:%lx\n", BUFF_TO_RUN(*ctx
, ctx
->pspdir2
));
823 if (ctx
->ish_a_dir
!= NULL
)
824 printf("ISHA offset:%lx\n", BUFF_TO_RUN(*ctx
, ctx
->ish_a_dir
));
825 if (ctx
->ish_b_dir
!= NULL
)
826 printf("ISHB offset:%lx\n", BUFF_TO_RUN(*ctx
, ctx
->ish_b_dir
));
827 if (ctx
->pspdir2_b
!= NULL
)
828 printf("PSP L2B offset:%lx\n", BUFF_TO_RUN(*ctx
, ctx
->pspdir2_b
));
829 if (ctx
->biosdir
!= NULL
)
830 printf("BHD offset:%lx\n", BUFF_TO_RUN(*ctx
, ctx
->biosdir
));
831 if (ctx
->biosdir2
!= NULL
)
832 printf("BHD L2(A) offset:%lx\n", BUFF_TO_RUN(*ctx
, ctx
->biosdir2
));
833 if (ctx
->biosdir2_b
!= NULL
)
834 printf("BHD L2B offset:%lx\n", BUFF_TO_RUN(*ctx
, ctx
->biosdir2_b
));
835 if (ctx
->psp_combo_dir
!= NULL
)
836 printf("PSP combo offset:%lx\n", BUFF_TO_RUN(*ctx
, ctx
->psp_combo_dir
));
837 if (ctx
->bhd_combo_dir
!= NULL
)
838 printf("BHD combo offset:%lx\n", BUFF_TO_RUN(*ctx
, ctx
->bhd_combo_dir
));
841 static void integrate_psp_ab(context
*ctx
, psp_directory_table
*pspdir
,
842 psp_directory_table
*pspdir2
, ish_directory_table
*ish
,
843 amd_fw_type ab
, enum platform soc_id
)
846 uint32_t current_table_save
;
848 current_table_save
= ctx
->current_table
;
849 ctx
->current_table
= BUFF_TO_RUN_MODE(*ctx
, pspdir
, AMD_ADDR_REL_BIOS
);
850 count
= pspdir
->header
.num_entries
;
851 assert_fw_entry(count
, MAX_PSP_ENTRIES
, ctx
);
852 pspdir
->entries
[count
].type
= (uint8_t)ab
;
853 pspdir
->entries
[count
].subprog
= 0;
854 pspdir
->entries
[count
].rsvd
= 0;
856 ish
->pl2_location
= BUFF_TO_RUN_MODE(*ctx
, pspdir2
, AMD_ADDR_REL_BIOS
);
857 ish
->boot_priority
= ab
== AMD_FW_RECOVERYAB_A
? 0xFFFFFFFF : 1;
858 ish
->update_retry_count
= 2;
859 ish
->glitch_retry_count
= 0;
860 ish
->psp_id
= get_psp_id(soc_id
);
861 ish
->checksum
= fletcher32(&ish
->boot_priority
,
862 sizeof(ish_directory_table
) - sizeof(uint32_t));
863 pspdir
->entries
[count
].addr
=
864 BUFF_TO_RUN_MODE(*ctx
, ish
, AMD_ADDR_REL_BIOS
);
865 pspdir
->entries
[count
].address_mode
=
866 SET_ADDR_MODE(pspdir
, AMD_ADDR_REL_BIOS
);
867 pspdir
->entries
[count
].size
= TABLE_ALIGNMENT
;
869 pspdir
->entries
[count
].addr
=
870 BUFF_TO_RUN_MODE(*ctx
, pspdir2
, AMD_ADDR_REL_BIOS
);
871 pspdir
->entries
[count
].address_mode
=
872 SET_ADDR_MODE(pspdir
, AMD_ADDR_REL_BIOS
);
873 pspdir
->entries
[count
].size
= _MAX(TABLE_ALIGNMENT
,
874 pspdir2
->header
.num_entries
*
875 sizeof(psp_directory_entry
) +
876 sizeof(psp_directory_header
));
880 fill_dir_header(pspdir
, count
, ctx
);
881 ctx
->current_table
= current_table_save
;
884 static void integrate_psp_levels(context
*ctx
,
885 amd_cb_config
*cb_config
)
887 uint32_t current_table_save
;
888 bool recovery_ab
= cb_config
->recovery_ab
;
890 psp_directory_table
*pspdir
, *pspdir2
, *pspdir2_b
;
891 bool use_only_a
= (cb_config
->soc_id
== PLATFORM_PHOENIX
); /* TODO: b:285390041 */
893 pspdir
= ctx
->pspdir
;
894 pspdir2
= ctx
->pspdir2
;
895 pspdir2_b
= ctx
->pspdir2_b
;
896 count
= pspdir
->header
.num_entries
;
898 current_table_save
= ctx
->current_table
;
899 ctx
->current_table
= BUFF_TO_RUN_MODE(*ctx
, pspdir
, AMD_ADDR_REL_BIOS
);
900 if (recovery_ab
&& (pspdir2
!= NULL
)) {
901 if (cb_config
->need_ish
) { /* Need ISH */
902 ctx
->ish_a_dir
= new_ish_dir(ctx
);
903 if (pspdir2_b
!= NULL
)
904 ctx
->ish_b_dir
= new_ish_dir(ctx
);
906 integrate_psp_ab(ctx
, pspdir
, pspdir2
, ctx
->ish_a_dir
,
907 AMD_FW_RECOVERYAB_A
, cb_config
->soc_id
);
908 if (pspdir2_b
!= NULL
)
909 integrate_psp_ab(ctx
, pspdir
, pspdir2_b
, ctx
->ish_b_dir
,
910 use_only_a
? AMD_FW_RECOVERYAB_A
: AMD_FW_RECOVERYAB_B
,
913 integrate_psp_ab(ctx
, pspdir
, pspdir2
, ctx
->ish_a_dir
,
914 use_only_a
? AMD_FW_RECOVERYAB_A
: AMD_FW_RECOVERYAB_B
,
917 } else if (pspdir2
!= NULL
) {
918 assert_fw_entry(count
, MAX_PSP_ENTRIES
, ctx
);
919 pspdir
->entries
[count
].type
= AMD_FW_L2_PTR
;
920 pspdir
->entries
[count
].subprog
= 0;
921 pspdir
->entries
[count
].rsvd
= 0;
922 pspdir
->entries
[count
].size
= sizeof(pspdir2
->header
)
923 + pspdir2
->header
.num_entries
924 * sizeof(psp_directory_entry
);
926 pspdir
->entries
[count
].addr
=
927 BUFF_TO_RUN_MODE(*ctx
, pspdir2
, AMD_ADDR_REL_BIOS
);
928 pspdir
->entries
[count
].address_mode
=
929 SET_ADDR_MODE(pspdir
, AMD_ADDR_REL_BIOS
);
931 fill_dir_header(pspdir
, count
, ctx
);
933 ctx
->current_table
= current_table_save
;
936 static void integrate_psp_firmwares(context
*ctx
,
937 amd_fw_entry
*fw_table
,
939 amd_cb_config
*cb_config
)
942 unsigned int i
, count
;
945 psp_directory_table
*pspdir
;
947 uint32_t current_table_save
;
948 bool recovery_ab
= cb_config
->recovery_ab
;
950 /* This function can create a primary table, a secondary table, or a
951 * flattened table which contains all applicable types. These if-else
952 * statements infer what the caller intended. If a 2nd-level cookie
953 * is passed, clearly a 2nd-level table is intended. However, a
954 * 1st-level cookie may indicate level 1 or flattened.
956 pspdir
= new_psp_dir(ctx
, cb_config
->multi_level
, cookie
);
958 if (cookie
== PSP_COOKIE
)
959 ctx
->pspdir
= pspdir
;
960 else if (cookie
== PSPL2_COOKIE
) {
961 if (ctx
->pspdir2
== NULL
)
962 ctx
->pspdir2
= pspdir
;
963 else if (ctx
->pspdir2_b
== NULL
)
964 ctx
->pspdir2_b
= pspdir
;
967 if (!cb_config
->multi_level
)
969 else if (cookie
== PSPL2_COOKIE
)
971 else if (cookie
== PSP_COOKIE
)
977 if (cookie
== PSPL2_COOKIE
)
979 else if (cookie
== PSP_COOKIE
)
984 current_table_save
= ctx
->current_table
;
985 ctx
->current_table
= BUFF_TO_RUN_MODE(*ctx
, pspdir
, AMD_ADDR_REL_BIOS
);
986 adjust_current_pointer(ctx
, 0, TABLE_ALIGNMENT
);
988 for (i
= 0, count
= 0; fw_table
[i
].type
!= AMD_FW_INVALID
; i
++) {
989 if (!(fw_table
[i
].level
& level
))
992 assert_fw_entry(count
, MAX_PSP_ENTRIES
, ctx
);
994 if (fw_table
[i
].type
== AMD_TOKEN_UNLOCK
) {
995 if (!fw_table
[i
].other
)
997 adjust_current_pointer(ctx
, 0, ERASE_ALIGNMENT
);
998 pspdir
->entries
[count
].type
= fw_table
[i
].type
;
999 pspdir
->entries
[count
].size
= 4096; /* TODO: doc? */
1000 pspdir
->entries
[count
].addr
= RUN_CURRENT(*ctx
);
1001 pspdir
->entries
[count
].address_mode
= SET_ADDR_MODE_BY_TABLE(pspdir
);
1002 pspdir
->entries
[count
].subprog
= fw_table
[i
].subprog
;
1003 pspdir
->entries
[count
].rsvd
= 0;
1004 adjust_current_pointer(ctx
, 4096, 0x100U
);
1006 } else if (fw_table
[i
].type
== AMD_PSP_FUSE_CHAIN
) {
1007 pspdir
->entries
[count
].type
= fw_table
[i
].type
;
1008 pspdir
->entries
[count
].subprog
= fw_table
[i
].subprog
;
1009 pspdir
->entries
[count
].rsvd
= 0;
1010 pspdir
->entries
[count
].size
= 0xFFFFFFFF;
1011 pspdir
->entries
[count
].addr
= fw_table
[i
].other
;
1012 pspdir
->entries
[count
].address_mode
= 0;
1014 } else if (fw_table
[i
].type
== AMD_FW_PSP_NVRAM
||
1015 fw_table
[i
].type
== AMD_RPMC_NVRAM
) {
1016 if (fw_table
[i
].filename
== NULL
) {
1017 if (fw_table
[i
].size
== 0)
1019 size
= fw_table
[i
].size
;
1020 addr
= fw_table
[i
].dest
;
1021 if (addr
!= ALIGN_UP(addr
, ERASE_ALIGNMENT
)) {
1023 "Error: PSP NVRAM section not aligned with erase block size.\n\n");
1024 amdfwtool_cleanup(ctx
);
1028 adjust_current_pointer(ctx
, 0, ERASE_ALIGNMENT
);
1029 bytes
= copy_blob(BUFF_CURRENT(*ctx
),
1030 fw_table
[i
].filename
, BUFF_ROOM(*ctx
));
1032 amdfwtool_cleanup(ctx
);
1036 size
= ALIGN_UP(bytes
, ERASE_ALIGNMENT
);
1037 addr
= RUN_CURRENT(*ctx
);
1038 adjust_current_pointer(ctx
, bytes
, BLOB_ERASE_ALIGNMENT
);
1041 pspdir
->entries
[count
].type
= fw_table
[i
].type
;
1042 pspdir
->entries
[count
].subprog
= fw_table
[i
].subprog
;
1043 pspdir
->entries
[count
].rsvd
= 0;
1044 pspdir
->entries
[count
].size
= size
;
1045 pspdir
->entries
[count
].addr
= addr
;
1047 pspdir
->entries
[count
].address_mode
=
1048 SET_ADDR_MODE(pspdir
, AMD_ADDR_REL_BIOS
);
1051 } else if (fw_table
[i
].filename
!= NULL
) {
1052 if (fw_table
[i
].addr_signed
) {
1053 pspdir
->entries
[count
].addr
=
1054 RUN_OFFSET(*ctx
, fw_table
[i
].addr_signed
);
1055 pspdir
->entries
[count
].address_mode
=
1056 SET_ADDR_MODE_BY_TABLE(pspdir
);
1057 bytes
= fw_table
[i
].file_size
;
1059 bytes
= copy_blob(BUFF_CURRENT(*ctx
),
1060 fw_table
[i
].filename
, BUFF_ROOM(*ctx
));
1062 amdfwtool_cleanup(ctx
);
1065 pspdir
->entries
[count
].addr
= RUN_CURRENT(*ctx
);
1066 pspdir
->entries
[count
].address_mode
=
1067 SET_ADDR_MODE_BY_TABLE(pspdir
);
1068 adjust_current_pointer(ctx
, bytes
, BLOB_ALIGNMENT
);
1071 pspdir
->entries
[count
].type
= fw_table
[i
].type
;
1072 pspdir
->entries
[count
].subprog
= fw_table
[i
].subprog
;
1073 pspdir
->entries
[count
].rsvd
= 0;
1074 pspdir
->entries
[count
].inst
= fw_table
[i
].inst
;
1075 pspdir
->entries
[count
].size
= (uint32_t)bytes
;
1079 /* This APU doesn't have this firmware. */
1083 fill_dir_header(pspdir
, count
, ctx
);
1084 ctx
->current_table
= current_table_save
;
1087 static void add_psp_firmware_entry(context
*ctx
,
1088 psp_directory_table
*pspdir
,
1089 void *table
, amd_fw_type type
, uint32_t size
)
1091 uint32_t count
= pspdir
->header
.num_entries
;
1093 uint32_t current_table_save
;
1095 current_table_save
= ctx
->current_table
;
1096 ctx
->current_table
= BUFF_TO_RUN_MODE(*ctx
, pspdir
, AMD_ADDR_REL_BIOS
);
1098 /* If there is an entry of "type", replace it. */
1099 for (index
= 0; index
< count
; index
++) {
1100 if (pspdir
->entries
[index
].type
== (uint8_t)type
)
1104 assert_fw_entry(count
, MAX_PSP_ENTRIES
, ctx
);
1105 pspdir
->entries
[index
].type
= (uint8_t)type
;
1106 pspdir
->entries
[index
].subprog
= 0;
1107 pspdir
->entries
[index
].rsvd
= 0;
1108 pspdir
->entries
[index
].addr
= BUFF_TO_RUN(*ctx
, table
);
1109 pspdir
->entries
[index
].address_mode
= SET_ADDR_MODE_BY_TABLE(pspdir
);
1110 pspdir
->entries
[index
].size
= size
;
1114 fill_dir_header(pspdir
, count
, ctx
);
1115 ctx
->current_table
= current_table_save
;
1118 static void *new_bios_dir(context
*ctx
, bool multi
, uint32_t cookie
)
1123 * Force both onto boundary when multi. Primary table is after
1124 * updatable table, so alignment ensures primary can stay intact
1125 * if secondary is reprogrammed.
1128 adjust_current_pointer(ctx
, 0, TABLE_ERASE_ALIGNMENT
);
1130 adjust_current_pointer(ctx
, 0, TABLE_ALIGNMENT
);
1131 ptr
= BUFF_CURRENT(*ctx
);
1132 ((bios_directory_hdr
*) ptr
)->cookie
= cookie
;
1133 ((bios_directory_hdr
*) ptr
)->additional_info
= 0;
1134 ((bios_directory_hdr
*) ptr
)->additional_info_fields
.address_mode
= ctx
->address_mode
;
1135 adjust_current_pointer(ctx
,
1136 sizeof(bios_directory_hdr
) + MAX_BIOS_ENTRIES
* sizeof(bios_directory_entry
),
1141 static int locate_bdt2_bios(bios_directory_table
*level2
,
1142 uint64_t *source
, uint32_t *size
)
1151 for (i
= 0 ; i
< level2
->header
.num_entries
; i
++) {
1152 if (level2
->entries
[i
].type
== AMD_BIOS_BIN
) {
1153 *source
= level2
->entries
[i
].source
;
1154 *size
= level2
->entries
[i
].size
;
1161 static int have_bios_tables(amd_bios_entry
*table
)
1165 for (i
= 0 ; table
[i
].type
!= AMD_BIOS_INVALID
; i
++) {
1166 if (table
[i
].level
& BDT_LVL1
&& table
[i
].filename
)
1172 int find_bios_entry(amd_bios_type type
)
1176 for (i
= 0; amd_bios_table
[i
].type
!= AMD_BIOS_INVALID
; i
++) {
1177 if (amd_bios_table
[i
].type
== type
)
1183 static void add_bios_apcb_bk_entry(bios_directory_table
*biosdir
, unsigned int idx
,
1184 int inst
, uint32_t size
, uint64_t source
)
1188 for (i
= 0; amd_bios_table
[i
].type
!= AMD_BIOS_INVALID
; i
++) {
1189 if (amd_bios_table
[i
].type
== AMD_BIOS_APCB_BK
&&
1190 amd_bios_table
[i
].inst
== inst
)
1194 if (amd_bios_table
[i
].type
!= AMD_BIOS_APCB_BK
)
1197 biosdir
->entries
[idx
].type
= amd_bios_table
[i
].type
;
1198 biosdir
->entries
[idx
].region_type
= amd_bios_table
[i
].region_type
;
1199 biosdir
->entries
[idx
].dest
= amd_bios_table
[i
].dest
?
1200 amd_bios_table
[i
].dest
: (uint64_t)-1;
1201 biosdir
->entries
[idx
].reset
= amd_bios_table
[i
].reset
;
1202 biosdir
->entries
[idx
].copy
= amd_bios_table
[i
].copy
;
1203 biosdir
->entries
[idx
].ro
= amd_bios_table
[i
].ro
;
1204 biosdir
->entries
[idx
].compressed
= amd_bios_table
[i
].zlib
;
1205 biosdir
->entries
[idx
].inst
= amd_bios_table
[i
].inst
;
1206 biosdir
->entries
[idx
].subprog
= amd_bios_table
[i
].subpr
;
1207 biosdir
->entries
[idx
].size
= size
;
1208 biosdir
->entries
[idx
].source
= source
;
1209 biosdir
->entries
[idx
].address_mode
= SET_ADDR_MODE_BY_TABLE(biosdir
);
1212 static void integrate_bios_levels(context
*ctx
, amd_cb_config
*cb_config
)
1215 uint32_t current_table_save
;
1217 if (cb_config
->recovery_ab
) {
1218 add_psp_firmware_entry(ctx
, ctx
->pspdir2
, ctx
->biosdir2
,
1219 AMD_FW_BIOS_TABLE
, TABLE_ALIGNMENT
);
1220 if (ctx
->pspdir2_b
!= NULL
)
1221 add_psp_firmware_entry(ctx
, ctx
->pspdir2_b
,
1222 ctx
->biosdir2_b
, AMD_FW_BIOS_TABLE
,
1224 } else if (ctx
->biosdir2
) {
1225 current_table_save
= ctx
->current_table
;
1226 ctx
->current_table
= BUFF_TO_RUN_MODE(*ctx
, ctx
->biosdir
, AMD_ADDR_REL_BIOS
);
1227 count
= ctx
->biosdir
->header
.num_entries
;
1228 assert_fw_entry(count
, MAX_BIOS_ENTRIES
, ctx
);
1229 ctx
->biosdir
->entries
[count
].type
= AMD_BIOS_L2_PTR
;
1230 ctx
->biosdir
->entries
[count
].region_type
= 0;
1231 ctx
->biosdir
->entries
[count
].size
=
1233 * sizeof(bios_directory_entry
);
1234 ctx
->biosdir
->entries
[count
].source
=
1235 BUFF_TO_RUN(*ctx
, ctx
->biosdir2
);
1236 ctx
->biosdir
->entries
[count
].address_mode
=
1237 SET_ADDR_MODE(ctx
->biosdir
, AMD_ADDR_REL_BIOS
);
1238 ctx
->biosdir
->entries
[count
].subprog
= 0;
1239 ctx
->biosdir
->entries
[count
].inst
= 0;
1240 ctx
->biosdir
->entries
[count
].copy
= 0;
1241 ctx
->biosdir
->entries
[count
].compressed
= 0;
1242 ctx
->biosdir
->entries
[count
].dest
= -1;
1243 ctx
->biosdir
->entries
[count
].reset
= 0;
1244 ctx
->biosdir
->entries
[count
].ro
= 0;
1246 fill_dir_header(ctx
->biosdir
, count
, ctx
);
1247 ctx
->current_table
= current_table_save
;
1250 static void integrate_bios_firmwares(context
*ctx
,
1251 amd_bios_entry
*fw_table
,
1253 amd_cb_config
*cb_config
)
1256 unsigned int i
, count
;
1261 uint32_t current_table_save
;
1262 bios_directory_table
*biosdir
;
1264 biosdir
= new_bios_dir(ctx
, cb_config
->multi_level
, cookie
);
1266 if (cookie
== BHD_COOKIE
)
1267 ctx
->biosdir
= biosdir
;
1268 else if (cookie
== BHDL2_COOKIE
) {
1269 if (ctx
->biosdir2
== NULL
)
1270 ctx
->biosdir2
= biosdir
;
1271 else if (ctx
->biosdir2_b
== NULL
)
1272 ctx
->biosdir2_b
= biosdir
;
1275 /* This function can create a primary table, a secondary table, or a
1276 * flattened table which contains all applicable types. These if-else
1277 * statements infer what the caller intended. If a 2nd-level cookie
1278 * is passed, clearly a 2nd-level table is intended. However, a
1279 * 1st-level cookie may indicate level 1 or flattened.
1281 if (!cb_config
->multi_level
)
1283 else if (cookie
== BHDL2_COOKIE
)
1285 else if (cookie
== BHD_COOKIE
)
1290 current_table_save
= ctx
->current_table
;
1291 ctx
->current_table
= BUFF_TO_RUN_MODE(*ctx
, biosdir
, AMD_ADDR_REL_BIOS
);
1292 adjust_current_pointer(ctx
, 0, TABLE_ALIGNMENT
);
1294 for (i
= 0, count
= 0; fw_table
[i
].type
!= AMD_BIOS_INVALID
; i
++) {
1295 if (!(fw_table
[i
].level
& level
))
1297 if (fw_table
[i
].filename
== NULL
&& (
1298 fw_table
[i
].type
!= AMD_BIOS_SIG
&&
1299 fw_table
[i
].type
!= AMD_BIOS_APOB
&&
1300 fw_table
[i
].type
!= AMD_BIOS_APOB_NV
&&
1301 fw_table
[i
].type
!= AMD_BIOS_L2_PTR
&&
1302 fw_table
[i
].type
!= AMD_BIOS_BIN
&&
1303 fw_table
[i
].type
!= AMD_BIOS_PSP_SHARED_MEM
))
1306 /* BIOS Directory items may have additional requirements */
1308 /* SIG needs a size, else no choice but to skip */
1309 if (fw_table
[i
].type
== AMD_BIOS_SIG
&& !fw_table
[i
].size
)
1312 /* Check APOB_NV requirements */
1313 if (fw_table
[i
].type
== AMD_BIOS_APOB_NV
) {
1314 if (!fw_table
[i
].size
&& !fw_table
[i
].src
)
1315 continue; /* APOB_NV not used */
1316 if (fw_table
[i
].src
&& !fw_table
[i
].size
) {
1317 fprintf(stderr
, "Error: APOB NV address provided, but no size\n");
1318 amdfwtool_cleanup(ctx
);
1321 /* If the APOB isn't used, APOB_NV isn't used either */
1322 apob_idx
= find_bios_entry(AMD_BIOS_APOB
);
1323 if (apob_idx
< 0 || !fw_table
[apob_idx
].dest
)
1324 continue; /* APOV NV not supported */
1327 /* APOB_DATA needs destination */
1328 if (fw_table
[i
].type
== AMD_BIOS_APOB
&& !fw_table
[i
].dest
) {
1329 fprintf(stderr
, "Error: APOB destination not provided\n");
1330 amdfwtool_cleanup(ctx
);
1334 /* BIOS binary must have destination and uncompressed size. If
1335 * no filename given, then user must provide a source address.
1337 if (fw_table
[i
].type
== AMD_BIOS_BIN
) {
1338 if (!fw_table
[i
].dest
|| !fw_table
[i
].size
) {
1339 fprintf(stderr
, "Error: BIOS binary destination and uncompressed size are required\n");
1340 amdfwtool_cleanup(ctx
);
1343 if (!fw_table
[i
].filename
&& !fw_table
[i
].src
) {
1344 fprintf(stderr
, "Error: BIOS binary assumed outside amdfw.rom but no source address given\n");
1345 amdfwtool_cleanup(ctx
);
1350 /* PSP_SHARED_MEM needs a destination and size */
1351 if (fw_table
[i
].type
== AMD_BIOS_PSP_SHARED_MEM
&&
1352 (!fw_table
[i
].dest
|| !fw_table
[i
].size
))
1354 assert_fw_entry(count
, MAX_BIOS_ENTRIES
, ctx
);
1356 biosdir
->entries
[count
].type
= fw_table
[i
].type
;
1357 biosdir
->entries
[count
].region_type
= fw_table
[i
].region_type
;
1358 biosdir
->entries
[count
].dest
= fw_table
[i
].dest
?
1359 fw_table
[i
].dest
: (uint64_t)-1;
1360 biosdir
->entries
[count
].reset
= fw_table
[i
].reset
;
1361 biosdir
->entries
[count
].copy
= fw_table
[i
].copy
;
1362 biosdir
->entries
[count
].ro
= fw_table
[i
].ro
;
1363 biosdir
->entries
[count
].compressed
= fw_table
[i
].zlib
;
1364 biosdir
->entries
[count
].inst
= fw_table
[i
].inst
;
1365 biosdir
->entries
[count
].subprog
= fw_table
[i
].subpr
;
1367 switch (fw_table
[i
].type
) {
1369 /* Reserve size bytes within amdfw.rom */
1370 biosdir
->entries
[count
].size
= fw_table
[i
].size
;
1371 biosdir
->entries
[count
].source
= RUN_CURRENT(*ctx
);
1372 biosdir
->entries
[count
].address_mode
=
1373 SET_ADDR_MODE_BY_TABLE(biosdir
);
1374 memset(BUFF_CURRENT(*ctx
), 0xff,
1375 biosdir
->entries
[count
].size
);
1376 adjust_current_pointer(ctx
, biosdir
->entries
[count
].size
, 0x100U
);
1379 biosdir
->entries
[count
].size
= fw_table
[i
].size
;
1380 biosdir
->entries
[count
].source
= fw_table
[i
].src
;
1381 biosdir
->entries
[count
].address_mode
= SET_ADDR_MODE_BY_TABLE(biosdir
);
1383 case AMD_BIOS_APOB_NV
:
1384 if (fw_table
[i
].src
) {
1385 /* If source is given, use that and its size */
1386 biosdir
->entries
[count
].source
= fw_table
[i
].src
;
1387 biosdir
->entries
[count
].address_mode
=
1388 SET_ADDR_MODE(biosdir
, AMD_ADDR_REL_BIOS
);
1389 biosdir
->entries
[count
].size
= fw_table
[i
].size
;
1391 /* Else reserve size bytes within amdfw.rom */
1392 adjust_current_pointer(ctx
, 0, ERASE_ALIGNMENT
);
1393 biosdir
->entries
[count
].source
= RUN_CURRENT(*ctx
);
1394 biosdir
->entries
[count
].address_mode
=
1395 SET_ADDR_MODE(biosdir
, AMD_ADDR_REL_BIOS
);
1396 biosdir
->entries
[count
].size
= ALIGN_UP(
1397 fw_table
[i
].size
, ERASE_ALIGNMENT
);
1398 memset(BUFF_CURRENT(*ctx
), 0xff,
1399 biosdir
->entries
[count
].size
);
1400 adjust_current_pointer(ctx
, biosdir
->entries
[count
].size
, 1);
1404 /* Don't make a 2nd copy, point to the same one */
1405 if (level
== BDT_LVL1
&& locate_bdt2_bios(ctx
->biosdir2
, &source
, &size
)) {
1406 biosdir
->entries
[count
].source
= source
;
1407 biosdir
->entries
[count
].address_mode
=
1408 SET_ADDR_MODE(biosdir
, AMD_ADDR_REL_BIOS
);
1409 biosdir
->entries
[count
].size
= size
;
1413 /* level 2, or level 1 and no copy found in level 2 */
1414 biosdir
->entries
[count
].source
= fw_table
[i
].src
;
1415 biosdir
->entries
[count
].address_mode
=
1416 SET_ADDR_MODE(biosdir
, AMD_ADDR_REL_BIOS
);
1417 biosdir
->entries
[count
].dest
= fw_table
[i
].dest
;
1418 biosdir
->entries
[count
].size
= fw_table
[i
].size
;
1420 if (!fw_table
[i
].filename
)
1423 bytes
= copy_blob(BUFF_CURRENT(*ctx
),
1424 fw_table
[i
].filename
, BUFF_ROOM(*ctx
));
1426 amdfwtool_cleanup(ctx
);
1430 biosdir
->entries
[count
].source
=
1431 RUN_CURRENT_MODE(*ctx
, AMD_ADDR_REL_BIOS
);
1432 biosdir
->entries
[count
].address_mode
=
1433 SET_ADDR_MODE(biosdir
, AMD_ADDR_REL_BIOS
);
1435 adjust_current_pointer(ctx
, bytes
, 0x100U
);
1437 case AMD_BIOS_PSP_SHARED_MEM
:
1438 biosdir
->entries
[count
].dest
= fw_table
[i
].dest
;
1439 biosdir
->entries
[count
].size
= fw_table
[i
].size
;
1442 default: /* everything else is copied from input */
1443 if (fw_table
[i
].type
== AMD_BIOS_APCB
||
1444 fw_table
[i
].type
== AMD_BIOS_APCB_BK
)
1445 adjust_current_pointer(ctx
, 0, ERASE_ALIGNMENT
);
1446 bytes
= copy_blob(BUFF_CURRENT(*ctx
),
1447 fw_table
[i
].filename
, BUFF_ROOM(*ctx
));
1449 amdfwtool_cleanup(ctx
);
1453 biosdir
->entries
[count
].size
= (uint32_t)bytes
;
1454 biosdir
->entries
[count
].source
= RUN_CURRENT(*ctx
);
1455 biosdir
->entries
[count
].address_mode
= SET_ADDR_MODE_BY_TABLE(biosdir
);
1457 adjust_current_pointer(ctx
, bytes
, 0x100U
);
1458 if (fw_table
[i
].type
== AMD_BIOS_APCB
&& !cb_config
->have_apcb_bk
) {
1459 size
= biosdir
->entries
[count
].size
;
1460 source
= biosdir
->entries
[count
].source
;
1462 add_bios_apcb_bk_entry(biosdir
, count
, fw_table
[i
].inst
, size
, source
);
1470 fill_dir_header(biosdir
, count
, ctx
);
1471 ctx
->current_table
= current_table_save
;
1474 static int set_efs_table(uint8_t soc_id
, amd_cb_config
*cb_config
,
1475 embedded_firmware
*amd_romsig
)
1477 if ((cb_config
->efs_spi_readmode
== 0xFF) || (cb_config
->efs_spi_speed
== 0xFF)) {
1478 fprintf(stderr
, "Error: EFS read mode and SPI speed must be set\n");
1482 /* amd_romsig->efs_gen introduced after RAVEN/PICASSO.
1483 * Leave as 0xffffffff for first gen */
1484 if (cb_config
->second_gen
) {
1485 amd_romsig
->efs_gen
.gen
= EFS_SECOND_GEN
;
1486 amd_romsig
->efs_gen
.reserved
= 0;
1488 amd_romsig
->efs_gen
.gen
= EFS_BEFORE_SECOND_GEN
;
1489 amd_romsig
->efs_gen
.reserved
= ~0;
1493 case PLATFORM_CARRIZO
:
1494 case PLATFORM_STONEYRIDGE
:
1495 amd_romsig
->spi_readmode_f15_mod_60_6f
= cb_config
->efs_spi_readmode
;
1496 amd_romsig
->fast_speed_new_f15_mod_60_6f
= cb_config
->efs_spi_speed
;
1498 case PLATFORM_RAVEN
:
1499 case PLATFORM_PICASSO
:
1500 amd_romsig
->spi_readmode_f17_mod_00_2f
= cb_config
->efs_spi_readmode
;
1501 amd_romsig
->spi_fastspeed_f17_mod_00_2f
= cb_config
->efs_spi_speed
;
1502 switch (cb_config
->efs_spi_micron_flag
) {
1504 amd_romsig
->qpr_dummy_cycle_f17_mod_00_2f
= 0xff;
1507 amd_romsig
->qpr_dummy_cycle_f17_mod_00_2f
= 0xa;
1510 fprintf(stderr
, "Error: EFS Micron flag must be correctly set.\n\n");
1514 case PLATFORM_RENOIR
:
1515 case PLATFORM_LUCIENNE
:
1516 case PLATFORM_CEZANNE
:
1517 case PLATFORM_MENDOCINO
:
1518 case PLATFORM_PHOENIX
:
1519 case PLATFORM_GLINDA
:
1520 case PLATFORM_GENOA
:
1521 amd_romsig
->spi_readmode_f17_mod_30_3f
= cb_config
->efs_spi_readmode
;
1522 amd_romsig
->spi_fastspeed_f17_mod_30_3f
= cb_config
->efs_spi_speed
;
1523 switch (cb_config
->efs_spi_micron_flag
) {
1525 amd_romsig
->micron_detect_f17_mod_30_3f
= 0xff;
1528 amd_romsig
->micron_detect_f17_mod_30_3f
= 0xaa;
1531 amd_romsig
->micron_detect_f17_mod_30_3f
= 0x55;
1534 fprintf(stderr
, "Error: EFS Micron flag must be correctly set.\n\n");
1538 case PLATFORM_UNKNOWN
:
1540 fprintf(stderr
, "Error: Invalid SOC name.\n\n");
1546 void open_process_config(char *config
, amd_cb_config
*cb_config
)
1548 FILE *config_handle
;
1551 config_handle
= fopen(config
, "r");
1552 if (config_handle
== NULL
) {
1553 fprintf(stderr
, "Can not open file %s for reading: %s\n",
1554 config
, strerror(errno
));
1557 if (process_config(config_handle
, cb_config
) == 0) {
1558 fprintf(stderr
, "Configuration file %s parsing error\n",
1560 fclose(config_handle
);
1563 fclose(config_handle
);
1567 if (cb_config
->debug
) {
1568 dump_psp_firmwares(amd_psp_fw_table
);
1569 dump_bdt_firmwares(amd_bios_table
);
1573 static bool is_initial_alignment_required(enum platform soc_id
)
1576 case PLATFORM_MENDOCINO
:
1577 case PLATFORM_PHOENIX
:
1578 case PLATFORM_GLINDA
:
1585 int main(int argc
, char **argv
)
1588 int combo_index
= 0;
1590 context ctx
= { 0 };
1591 uint32_t romsig_offset
;
1592 amd_cb_config cb_config
= {
1593 .efs_spi_readmode
= 0xff, .efs_spi_speed
= 0xff, .efs_spi_micron_flag
= 0xff
1596 ctx
.current_pointer_saved
= 0xFFFFFFFF;
1598 retval
= amdfwtool_getopt(argc
, argv
, &cb_config
, &ctx
);
1604 if (cb_config
.use_combo
) {
1605 ctx
.amd_psp_fw_table_clean
= malloc(sizeof(amd_psp_fw_table
));
1606 ctx
.amd_bios_table_clean
= malloc(sizeof(amd_bios_table
));
1607 memcpy(ctx
.amd_psp_fw_table_clean
, amd_psp_fw_table
, sizeof(amd_psp_fw_table
));
1608 memcpy(ctx
.amd_bios_table_clean
, amd_bios_table
, sizeof(amd_bios_table
));
1611 open_process_config(cb_config
.config
, &cb_config
);
1613 ctx
.rom
= malloc(ctx
.rom_size
);
1615 fprintf(stderr
, "Error: Failed to allocate memory\n");
1618 memset(ctx
.rom
, 0xFF, ctx
.rom_size
);
1620 romsig_offset
= cb_config
.efs_location
? cb_config
.efs_location
: AMD_ROMSIG_OFFSET
;
1621 set_current_pointer(&ctx
, romsig_offset
);
1623 ctx
.amd_romsig_ptr
= BUFF_OFFSET(ctx
, romsig_offset
);
1624 ctx
.amd_romsig_ptr
->signature
= EMBEDDED_FW_SIGNATURE
;
1625 ctx
.amd_romsig_ptr
->imc_entry
= 0;
1626 ctx
.amd_romsig_ptr
->gec_entry
= 0;
1627 ctx
.amd_romsig_ptr
->xhci_entry
= 0;
1629 if (cb_config
.soc_id
!= PLATFORM_UNKNOWN
) {
1630 retval
= set_efs_table(cb_config
.soc_id
, &cb_config
, ctx
.amd_romsig_ptr
);
1632 fprintf(stderr
, "ERROR: Failed to initialize EFS table!\n");
1636 fprintf(stderr
, "WARNING: No SOC name specified.\n");
1639 if (cb_config
.need_ish
)
1640 ctx
.address_mode
= AMD_ADDR_REL_TAB
;
1641 else if (cb_config
.second_gen
)
1642 ctx
.address_mode
= AMD_ADDR_REL_BIOS
;
1644 ctx
.address_mode
= AMD_ADDR_PHYSICAL
;
1646 if (cb_config
.efs_location
!= cb_config
.body_location
)
1647 set_current_pointer(&ctx
, cb_config
.body_location
);
1649 set_current_pointer(&ctx
, romsig_offset
+ sizeof(embedded_firmware
));
1651 integrate_firmwares(&ctx
, ctx
.amd_romsig_ptr
, amd_fw_table
);
1653 if (is_initial_alignment_required(cb_config
.soc_id
)) {
1654 /* TODO: Check for older platforms. */
1655 adjust_current_pointer(&ctx
, 0, 0x10000U
);
1657 ctx
.current_table
= 0;
1659 /* If the tool is invoked with command-line options to keep the signed PSP
1660 binaries separate, process the signed binaries first. */
1661 if (cb_config
.signed_output_file
&& cb_config
.signed_start_addr
)
1662 process_signed_psp_firmwares(cb_config
.signed_output_file
,
1664 cb_config
.signed_start_addr
,
1667 if (cb_config
.use_combo
) {
1668 ctx
.psp_combo_dir
= new_combo_dir(&ctx
, PSP2_COOKIE
);
1670 adjust_current_pointer(&ctx
, 0, 0x1000U
);
1672 if (!cb_config
.recovery_ab
)
1673 ctx
.bhd_combo_dir
= new_combo_dir(&ctx
, BHD2_COOKIE
);
1677 if (cb_config
.config
)
1678 cb_config
.combo_config
[0] = cb_config
.config
;
1681 if (cb_config
.use_combo
&& cb_config
.debug
)
1682 printf("Processing %dth combo entry\n", combo_index
);
1686 ctx
.pspdir2_b
= NULL
;
1688 ctx
.biosdir2
= NULL
;
1689 ctx
.biosdir2_b
= NULL
;
1690 ctx
.ish_a_dir
= NULL
;
1691 ctx
.ish_b_dir
= NULL
;
1692 /* for non-combo image, combo_config[0] == config, and
1693 * it already is processed. Actually "combo_index >
1694 * 0" is enough. Put both of them here to make sure
1695 * and make it clear this will not affect non-combo
1698 if (cb_config
.use_combo
&& combo_index
> 0) {
1699 /* Restore the table as clean data. */
1700 memcpy(amd_psp_fw_table
, ctx
.amd_psp_fw_table_clean
,
1701 sizeof(amd_psp_fw_table
));
1702 memcpy(amd_bios_table
, ctx
.amd_bios_table_clean
,
1703 sizeof(amd_bios_table
));
1704 assert_fw_entry(combo_index
, MAX_COMBO_ENTRIES
, &ctx
);
1705 open_process_config(cb_config
.combo_config
[combo_index
], &cb_config
);
1707 /* In most cases, the address modes are same. */
1708 if (cb_config
.need_ish
)
1709 ctx
.address_mode
= AMD_ADDR_REL_TAB
;
1710 else if (cb_config
.second_gen
)
1711 ctx
.address_mode
= AMD_ADDR_REL_BIOS
;
1713 ctx
.address_mode
= AMD_ADDR_PHYSICAL
;
1715 register_apcb_combo(&cb_config
, combo_index
, &ctx
);
1718 if (cb_config
.multi_level
) {
1719 /* Do 2nd PSP directory followed by 1st */
1720 integrate_psp_firmwares(&ctx
,
1721 amd_psp_fw_table
, PSPL2_COOKIE
, &cb_config
);
1722 if (cb_config
.recovery_ab
&& !cb_config
.recovery_ab_single_copy
) {
1723 /* Create a copy of PSP Directory 2 in the backup slot B.
1724 Related biosdir2_b copy will be created later. */
1725 integrate_psp_firmwares(&ctx
,
1726 amd_psp_fw_table
, PSPL2_COOKIE
, &cb_config
);
1729 * Either the platform is using only
1730 * one slot or B is same as above
1731 * directories for A. Skip creating
1732 * pspdir2_b here to save flash space.
1733 * Related biosdir2_b will be skipped
1736 ctx
.pspdir2_b
= NULL
; /* More explicitly */
1738 integrate_psp_firmwares(&ctx
,
1739 amd_psp_fw_table
, PSP_COOKIE
, &cb_config
);
1740 integrate_psp_levels(&ctx
, &cb_config
);
1742 /* flat: PSP 1 cookie and no pointer to 2nd table */
1743 integrate_psp_firmwares(&ctx
,
1744 amd_psp_fw_table
, PSP_COOKIE
, &cb_config
);
1747 if (!cb_config
.use_combo
) {
1748 fill_psp_directory_to_efs(ctx
.amd_romsig_ptr
, ctx
.pspdir
, &ctx
, &cb_config
);
1750 fill_psp_directory_to_efs(ctx
.amd_romsig_ptr
, ctx
.psp_combo_dir
, &ctx
, &cb_config
);
1751 /* 0 -Compare PSP ID, 1 -Compare chip family ID */
1752 assert_fw_entry(combo_index
, MAX_COMBO_ENTRIES
, &ctx
);
1753 ctx
.psp_combo_dir
->entries
[combo_index
].id_sel
= 0;
1754 ctx
.psp_combo_dir
->entries
[combo_index
].id
= get_psp_id(cb_config
.soc_id
);
1755 ctx
.psp_combo_dir
->entries
[combo_index
].lvl2_addr
=
1756 BUFF_TO_RUN_MODE(ctx
, ctx
.pspdir
, AMD_ADDR_REL_BIOS
);
1758 fill_dir_header(ctx
.psp_combo_dir
, combo_index
+ 1, &ctx
);
1761 if (have_bios_tables(amd_bios_table
)) {
1762 if (cb_config
.multi_level
) {
1763 /* Do 2nd level BIOS directory followed by 1st */
1764 integrate_bios_firmwares(&ctx
,
1765 amd_bios_table
, BHDL2_COOKIE
, &cb_config
);
1766 if (cb_config
.recovery_ab
) {
1767 if (ctx
.pspdir2_b
!= NULL
) {
1768 integrate_bios_firmwares(&ctx
,
1769 amd_bios_table
, BHDL2_COOKIE
,
1773 integrate_bios_firmwares(&ctx
,
1774 amd_bios_table
, BHD_COOKIE
, &cb_config
);
1776 integrate_bios_levels(&ctx
, &cb_config
);
1778 /* flat: BHD1 cookie and no pointer to 2nd table */
1779 integrate_bios_firmwares(&ctx
,
1780 amd_bios_table
, BHD_COOKIE
, &cb_config
);
1782 if (!cb_config
.use_combo
) {
1783 fill_bios_directory_to_efs(ctx
.amd_romsig_ptr
, ctx
.biosdir
,
1785 } else if (ctx
.bhd_combo_dir
!= NULL
) {
1786 /* In recovery A/B mode, there isn't a BHD combo directory.
1787 * Instead, the BIOS tables level 2 are linked by PSP tables.
1789 fill_bios_directory_to_efs(ctx
.amd_romsig_ptr
, ctx
.bhd_combo_dir
,
1791 assert_fw_entry(combo_index
, MAX_COMBO_ENTRIES
, &ctx
);
1792 ctx
.bhd_combo_dir
->entries
[combo_index
].id_sel
= 0;
1793 ctx
.bhd_combo_dir
->entries
[combo_index
].id
=
1794 get_psp_id(cb_config
.soc_id
);
1795 ctx
.bhd_combo_dir
->entries
[combo_index
].lvl2_addr
=
1796 BUFF_TO_RUN_MODE(ctx
, ctx
.biosdir
, AMD_ADDR_REL_BIOS
);
1798 fill_dir_header(ctx
.bhd_combo_dir
, combo_index
+ 1, &ctx
);
1801 if (cb_config
.debug
)
1802 dump_image_addresses(&ctx
);
1803 } while (cb_config
.use_combo
&& ++combo_index
< MAX_COMBO_ENTRIES
&&
1804 cb_config
.combo_config
[combo_index
] != NULL
);
1806 targetfd
= open(cb_config
.output
, O_RDWR
| O_CREAT
| O_TRUNC
, 0666);
1807 if (targetfd
>= 0) {
1808 uint32_t offset
= cb_config
.efs_location
;
1809 uint32_t bytes
= cb_config
.efs_location
== cb_config
.body_location
?
1810 ctx
.current
- offset
: sizeof(embedded_firmware
);
1813 ret_bytes
= write_from_buf_to_file(targetfd
, BUFF_OFFSET(ctx
, offset
), bytes
);
1814 if (bytes
!= ret_bytes
) {
1815 fprintf(stderr
, "Error: Writing to file %s failed\n", cb_config
.output
);
1820 fprintf(stderr
, "Error: could not open file: %s\n", cb_config
.output
);
1824 if (cb_config
.efs_location
!= cb_config
.body_location
) {
1827 bytes
= write_body(cb_config
.output
, BUFF_OFFSET(ctx
, cb_config
.body_location
),
1828 ctx
.current
- cb_config
.body_location
);
1829 if (bytes
!= ctx
.current
- cb_config
.body_location
) {
1830 fprintf(stderr
, "Error: Writing body\n");
1835 if (cb_config
.manifest_file
) {
1836 dump_blob_version(cb_config
.manifest_file
, amd_psp_fw_table
);
1839 amdfwtool_cleanup(&ctx
);