Debian Codegraph @

util/amdfwtool/amdfwtool.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 /*
4 * ROMSIG At ROMBASE + 0x[0,2,4,8]20000:
5 * 0 4 8 C
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
14 *
15 * EC ROM should be 64K aligned.
16 *
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 * +------------+---------------+----------------+------------+
29 * | |
30 * | |
31 * | Other PSP Firmware |
32 * | |
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 |
41 * for A/B recovery |
42 * |
43 * |
44 * +------------+---------------+----------------+------------+ |
45 * | '2LP$' | Fletcher | Count | Reserved |<--+
46 * +------------+---------------+----------------+------------+
47 * | |
48 * | |
49 * | PSP Firmware |
50 * | (2nd-level is not required on all families) |
51 * | |
52 * +------------+---------------+----------------+------------+
53 * BIOS Directory Table (BDT) is similar
54 *
55 * PSP Combo directory
56 * +------------+---------------+----------------+------------+
57 * | 'PSP2' | Fletcher | Count |Look up mode|
58 * +------------+---------------+----------------+------------+
59 * | R e s e r v e d |
60 * +------------+---------------+----------------+------------+
61 * | ID-Sel | PSP ID | PSPDIR ADDR | | 1st PSP directory
62 * +------------+---------------+----------------+------------+
63 * | ID-Sel | PSP ID | PSPDIR ADDR | | 2nd PSP directory
64 * +------------+---------------+----------------+------------+
65 * | |
66 * | Other PSP |
67 * | |
68 * +------------+---------------+----------------+------------+
69 * BDT Combo is similar
70 */
71
72 #include <commonlib/bsd/helpers.h>
73 #include <fcntl.h>
74 #include <errno.h>
75 #include <limits.h>
76 #include <stdbool.h>
77 #include <stdio.h>
78 #include <sys/stat.h>
79 #include <sys/types.h>
80 #include <unistd.h>
81 #include <string.h>
82 #include <stdlib.h>
83 #include <libgen.h>
84 #include <stdint.h>
85
86 #include "amdfwtool.h"
87
88 #define AMD_ROMSIG_OFFSET 0x20000
89
90 #define _MAX(A, B) (((A) > (B)) ? (A) : (B))
91
92 static void output_manifest(int manifest_fd, amd_fw_entry *fw_entry);
93
94 /*
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.
101 *
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.
106 *
107 * TODO: Future work may require fully implementing the PSP_COMBO feature.
108 */
109
110 /*
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.
113 *
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.
122 *
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.
129 */
130 static uint32_t fletcher32(const void *data, int length)
131 {
132 uint32_t c0;
133 uint32_t c1;
134 uint32_t checksum;
135 int index;
136 const uint16_t *pptr = data;
137
138 length /= 2;
139
140 c0 = 0xFFFF;
141 c1 = 0xFFFF;
142
143 while (length) {
144 index = length >= 359 ? 359 : length;
145 length -= index;
146 do {
147 c0 += *(pptr++);
148 c1 += c0;
149 } while (--index);
150 c0 = (c0 & 0xFFFF) + (c0 >> 16);
151 c1 = (c1 & 0xFFFF) + (c1 >> 16);
152 }
153
154 /* Sums[0,1] mod 64K + overflow */
155 c0 = (c0 & 0xFFFF) + (c0 >> 16);
156 c1 = (c1 & 0xFFFF) + (c1 >> 16);
157 checksum = (c1 << 16) | c0;
158
159 return checksum;
160 }
161
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_PSP_TEEIPKEY, .subprog = 0, .level = PSP_LVL2 | PSP_LVL2_AB,
177 .skip_hashing = true },
178 { .type = AMD_FW_ABL_PUBKEY, .level = PSP_BOTH | PSP_BOTH_AB },
179 { .type = AMD_PSP_FUSE_CHAIN, .level = PSP_LVL2 | PSP_LVL2_AB },
180 { .type = AMD_FW_PSP_TRUSTLETS, .level = PSP_LVL2 | PSP_LVL2_AB },
181 { .type = AMD_FW_PSP_TRUSTLETKEY, .level = PSP_LVL2 | PSP_LVL2_AB },
182 { .type = AMD_FW_PSP_SMU_FIRMWARE2, .subprog = 0, .level = PSP_BOTH | PSP_LVL2_AB },
183 { .type = AMD_FW_PSP_SMU_FIRMWARE2, .subprog = 1, .level = PSP_BOTH | PSP_LVL2_AB },
184 { .type = AMD_FW_PSP_SMU_FIRMWARE2, .subprog = 2, .level = PSP_BOTH | PSP_LVL2_AB },
185 { .type = AMD_BOOT_DRIVER, .level = PSP_BOTH | PSP_LVL2_AB },
186 { .type = AMD_SOC_DRIVER, .level = PSP_BOTH | PSP_LVL2_AB },
187 { .type = AMD_DEBUG_DRIVER, .level = PSP_BOTH | PSP_LVL2_AB },
188 { .type = AMD_INTERFACE_DRIVER, .level = PSP_BOTH | PSP_LVL2_AB },
189 { .type = AMD_DEBUG_UNLOCK, .level = PSP_LVL2 | PSP_LVL2_AB },
190 { .type = AMD_HW_IPCFG, .subprog = 0, .level = PSP_LVL2 | PSP_LVL2_AB },
191 { .type = AMD_HW_IPCFG, .subprog = 1, .level = PSP_LVL2 | PSP_LVL2_AB },
192 { .type = AMD_HW_IPCFG, .subprog = 2, .level = PSP_LVL2 | PSP_LVL2_AB },
193 { .type = AMD_WRAPPED_IKEK, .level = PSP_BOTH | PSP_LVL2_AB, .skip_hashing = true },
194 { .type = AMD_TOKEN_UNLOCK, .level = PSP_BOTH | PSP_LVL2_AB },
195 { .type = AMD_SEC_GASKET, .subprog = 0, .level = PSP_BOTH | PSP_LVL2_AB },
196 { .type = AMD_SEC_GASKET, .subprog = 1, .level = PSP_BOTH | PSP_LVL2_AB },
197 { .type = AMD_SEC_GASKET, .subprog = 2, .level = PSP_BOTH | PSP_LVL2_AB },
198 { .type = AMD_MP2_FW, .subprog = 0, .level = PSP_LVL2 | PSP_LVL2_AB },
199 { .type = AMD_MP2_FW, .subprog = 1, .level = PSP_LVL2 | PSP_LVL2_AB },
200 { .type = AMD_MP2_FW, .subprog = 2, .level = PSP_LVL2 | PSP_LVL2_AB },
201 { .type = AMD_DRIVER_ENTRIES, .level = PSP_LVL2 | PSP_LVL2_AB },
202 { .type = AMD_FW_KVM_IMAGE, .level = PSP_LVL2 | PSP_LVL2_AB },
203 { .type = AMD_FW_MP5, .subprog = 0, .level = PSP_BOTH | PSP_BOTH_AB },
204 { .type = AMD_FW_MP5, .subprog = 1, .level = PSP_BOTH | PSP_BOTH_AB },
205 { .type = AMD_FW_MP5, .subprog = 2, .level = PSP_BOTH | PSP_BOTH_AB },
206 { .type = AMD_S0I3_DRIVER, .level = PSP_LVL2 | PSP_LVL2_AB },
207 { .type = AMD_ABL0, .level = PSP_BOTH | PSP_LVL2_AB,
208 .generate_manifest = true },
209 { .type = AMD_ABL1, .level = PSP_BOTH | PSP_LVL2_AB },
210 { .type = AMD_ABL2, .level = PSP_BOTH | PSP_LVL2_AB },
211 { .type = AMD_ABL3, .level = PSP_BOTH | PSP_LVL2_AB },
212 { .type = AMD_ABL4, .level = PSP_BOTH | PSP_LVL2_AB },
213 { .type = AMD_ABL5, .level = PSP_BOTH | PSP_LVL2_AB },
214 { .type = AMD_ABL6, .level = PSP_BOTH | PSP_LVL2_AB },
215 { .type = AMD_ABL7, .level = PSP_BOTH | PSP_LVL2_AB },
216 { .type = AMD_SEV_DATA, .level = PSP_LVL2 | PSP_LVL2_AB },
217 { .type = AMD_SEV_CODE, .level = PSP_LVL2 | PSP_LVL2_AB },
218 { .type = AMD_FW_PSP_WHITELIST, .level = PSP_LVL2 | PSP_LVL2_AB },
219 { .type = AMD_VBIOS_BTLOADER, .level = PSP_BOTH | PSP_LVL2_AB },
220 { .type = AMD_FW_DXIO, .level = PSP_BOTH | PSP_BOTH_AB },
221 { .type = AMD_FW_USB_PHY, .level = PSP_LVL2 | PSP_LVL2_AB },
222 { .type = AMD_FW_TOS_SEC_POLICY, .level = PSP_BOTH | PSP_LVL2_AB },
223 { .type = AMD_FW_DRTM_TA, .level = PSP_LVL2 | PSP_LVL2_AB },
224 { .type = AMD_FW_KEYDB_BL, .level = PSP_BOTH | PSP_LVL2_AB },
225 { .type = AMD_FW_KEYDB_TOS, .level = PSP_LVL2 | PSP_LVL2_AB },
226 { .type = AMD_FW_PSP_VERSTAGE, .level = PSP_BOTH | PSP_LVL2_AB },
227 { .type = AMD_FW_VERSTAGE_SIG, .level = PSP_BOTH | PSP_LVL2_AB },
228 { .type = AMD_RPMC_NVRAM, .level = PSP_LVL2 | PSP_LVL2_AB },
229 { .type = AMD_FW_SPL, .level = PSP_LVL2 | PSP_LVL2_AB },
230 { .type = AMD_FW_DMCU_ERAM, .level = PSP_LVL2 | PSP_LVL2_AB },
231 { .type = AMD_FW_DMCU_ISR, .level = PSP_LVL2 | PSP_LVL2_AB },
232 { .type = AMD_FW_MSMU, .subprog = 0, .level = PSP_LVL2 | PSP_LVL2_AB },
233 { .type = AMD_FW_MSMU, .subprog = 1, .level = PSP_LVL2 | PSP_LVL2_AB },
234 { .type = AMD_FW_SPIROM_CFG, .level = PSP_LVL2 | PSP_LVL2_AB },
235 { .type = AMD_FW_MPIO, .level = PSP_LVL2 | PSP_LVL2_AB },
236 { .type = AMD_FW_PSP_SMUSCS, .level = PSP_BOTH | PSP_LVL2_AB },
237 { .type = AMD_FW_DMCUB, .level = PSP_LVL2 | PSP_LVL2_AB },
238 { .type = AMD_FW_PSP_BOOTLOADER_AB, .level = PSP_LVL2 | PSP_LVL2_AB,
239 .generate_manifest = true },
240 { .type = AMD_RIB, .subprog = 0, .level = PSP_LVL2 | PSP_LVL2_AB },
241 { .type = AMD_RIB, .subprog = 1, .level = PSP_LVL2 | PSP_LVL2_AB },
242 { .type = AMD_RIB, .subprog = 2, .level = PSP_LVL2 | PSP_LVL2_AB },
243 { .type = AMD_FW_MPDMA_TF, .level = PSP_BOTH | PSP_BOTH_AB },
244 { .type = AMD_TA_IKEK, .level = PSP_BOTH | PSP_LVL2_AB, .skip_hashing = true },
245 { .type = AMD_FW_GMI3_PHY, .level = PSP_BOTH | PSP_BOTH_AB },
246 { .type = AMD_FW_MPDMA_PM, .level = PSP_BOTH | PSP_BOTH_AB },
247 { .type = AMD_FW_AMF_SRAM, .level = PSP_LVL2 | PSP_LVL2_AB },
248 { .type = AMD_FW_AMF_DRAM, .inst = 0, .level = PSP_LVL2 | PSP_LVL2_AB },
249 { .type = AMD_FW_AMF_DRAM, .inst = 1, .level = PSP_LVL2 | PSP_LVL2_AB },
250 { .type = AMD_FW_MFD_MPM, .inst = 0, .level = PSP_LVL2 | PSP_LVL2_AB },
251 { .type = AMD_FW_MFD_MPM, .inst = 1, .level = PSP_LVL2 | PSP_LVL2_AB },
252 { .type = AMD_FW_FCFG_TABLE, .level = PSP_LVL2 | PSP_LVL2_AB },
253 { .type = AMD_FW_AMF_WLAN, .inst = 0, .level = PSP_LVL2 | PSP_LVL2_AB },
254 { .type = AMD_FW_AMF_WLAN, .inst = 1, .level = PSP_LVL2 | PSP_LVL2_AB },
255 { .type = AMD_FW_AMF_WLAN, .inst = 2, .level = PSP_LVL2 | PSP_LVL2_AB },
256 { .type = AMD_FW_AMF_WLAN, .inst = 3, .level = PSP_LVL2 | PSP_LVL2_AB },
257 { .type = AMD_FW_AMF_MFD, .level = PSP_LVL2 | PSP_LVL2_AB },
258 { .type = AMD_TA_IKEK, .level = PSP_BOTH | PSP_LVL2_AB, .skip_hashing = true },
259 { .type = AMD_FW_MPCCX, .subprog = 0, .level = PSP_LVL2 | PSP_LVL2_AB },
260 { .type = AMD_FW_MPCCX, .subprog = 1, .level = PSP_LVL2 | PSP_LVL2_AB },
261 { .type = AMD_FW_LSDMA, .level = PSP_LVL2 | PSP_LVL2_AB },
262 { .type = AMD_FW_C20_MP, .level = PSP_BOTH | PSP_LVL2_AB },
263 { .type = AMD_FW_MINIMSMU, .inst = 0, .level = PSP_BOTH | PSP_LVL2_AB },
264 { .type = AMD_FW_MINIMSMU, .inst = 1, .level = PSP_BOTH | PSP_LVL2_AB },
265 { .type = AMD_FW_MINIMSMU, .subprog = 1, .level = PSP_BOTH | PSP_LVL2_AB },
266 { .type = AMD_FW_GFXIMU_0, .subprog = 0, .level = PSP_BOTH | PSP_LVL2_AB },
267 { .type = AMD_FW_GFXIMU_0, .subprog = 1, .level = PSP_BOTH | PSP_LVL2_AB },
268 { .type = AMD_FW_GFXIMU_1, .subprog = 0, .level = PSP_BOTH | PSP_LVL2_AB },
269 { .type = AMD_FW_GFXIMU_1, .subprog = 1, .level = PSP_BOTH | PSP_LVL2_AB },
270 { .type = AMD_FW_GFXIMU_2, .level = PSP_BOTH | PSP_LVL2_AB },
271 { .type = AMD_FW_SRAM_FW_EXT, .level = PSP_LVL2 | PSP_LVL2_AB },
272 { .type = AMD_FW_UMSMU, .level = PSP_LVL2 | PSP_LVL2_AB },
273 { .type = AMD_FW_S3IMG, .level = PSP_LVL2 | PSP_LVL2_AB },
274 { .type = AMD_FW_USBDP, .level = PSP_LVL2 | PSP_LVL2_AB },
275 { .type = AMD_FW_USBSS, .level = PSP_LVL2 | PSP_LVL2_AB },
276 { .type = AMD_FW_USB4, .level = PSP_LVL2 | PSP_LVL2_AB },
277 { .type = AMD_FW_INVALID },
278 };
279
280 amd_fw_entry amd_fw_table[] = {
281 { .type = AMD_FW_XHCI },
282 { .type = AMD_FW_IMC },
283 { .type = AMD_FW_GEC },
284 { .type = AMD_FW_INVALID },
285 };
286
287 amd_bios_entry amd_bios_table[] = {
288 { .type = AMD_BIOS_RTM_PUBKEY, .inst = 0, .level = BDT_BOTH },
289 { .type = AMD_BIOS_SIG, .inst = 0, .level = BDT_BOTH },
290 { .type = AMD_BIOS_APCB, .inst = 0, .level = BDT_BOTH },
291 { .type = AMD_BIOS_APCB, .inst = 1, .level = BDT_BOTH },
292 { .type = AMD_BIOS_APCB, .inst = 2, .level = BDT_BOTH },
293 { .type = AMD_BIOS_APCB, .inst = 3, .level = BDT_BOTH },
294 { .type = AMD_BIOS_APCB, .inst = 4, .level = BDT_BOTH },
295 { .type = AMD_BIOS_APCB, .inst = 5, .level = BDT_BOTH },
296 { .type = AMD_BIOS_APCB, .inst = 6, .level = BDT_BOTH },
297 { .type = AMD_BIOS_APCB, .inst = 7, .level = BDT_BOTH },
298 { .type = AMD_BIOS_APCB, .inst = 8, .level = BDT_BOTH },
299 { .type = AMD_BIOS_APCB, .inst = 9, .level = BDT_BOTH },
300 { .type = AMD_BIOS_APCB, .inst = 10, .level = BDT_BOTH },
301 { .type = AMD_BIOS_APCB, .inst = 11, .level = BDT_BOTH },
302 { .type = AMD_BIOS_APCB, .inst = 12, .level = BDT_BOTH },
303 { .type = AMD_BIOS_APCB, .inst = 13, .level = BDT_BOTH },
304 { .type = AMD_BIOS_APCB, .inst = 14, .level = BDT_BOTH },
305 { .type = AMD_BIOS_APCB, .inst = 15, .level = BDT_BOTH },
306 { .type = AMD_BIOS_APCB_BK, .inst = 0, .level = BDT_BOTH },
307 { .type = AMD_BIOS_APCB_BK, .inst = 1, .level = BDT_BOTH },
308 { .type = AMD_BIOS_APCB_BK, .inst = 2, .level = BDT_BOTH },
309 { .type = AMD_BIOS_APCB_BK, .inst = 3, .level = BDT_BOTH },
310 { .type = AMD_BIOS_APCB_BK, .inst = 4, .level = BDT_BOTH },
311 { .type = AMD_BIOS_APCB_BK, .inst = 5, .level = BDT_BOTH },
312 { .type = AMD_BIOS_APCB_BK, .inst = 6, .level = BDT_BOTH },
313 { .type = AMD_BIOS_APCB_BK, .inst = 7, .level = BDT_BOTH },
314 { .type = AMD_BIOS_APCB_BK, .inst = 8, .level = BDT_BOTH },
315 { .type = AMD_BIOS_APCB_BK, .inst = 9, .level = BDT_BOTH },
316 { .type = AMD_BIOS_APCB_BK, .inst = 10, .level = BDT_BOTH },
317 { .type = AMD_BIOS_APCB_BK, .inst = 11, .level = BDT_BOTH },
318 { .type = AMD_BIOS_APCB_BK, .inst = 12, .level = BDT_BOTH },
319 { .type = AMD_BIOS_APCB_BK, .inst = 13, .level = BDT_BOTH },
320 { .type = AMD_BIOS_APCB_BK, .inst = 14, .level = BDT_BOTH },
321 { .type = AMD_BIOS_APCB_BK, .inst = 15, .level = BDT_BOTH },
322 { .type = AMD_BIOS_APOB, .level = BDT_BOTH },
323 { .type = AMD_BIOS_BIN,
324 .reset = 1, .copy = 1, .zlib = 1, .inst = 0, .level = BDT_BOTH },
325 { .type = AMD_BIOS_APOB_NV, .level = BDT_LVL2 },
326 { .type = AMD_BIOS_NV_ST, .level = BDT_LVL2 },
327 { .type = AMD_BIOS_PMUI, .inst = 1, .subpr = 0, .level = BDT_BOTH },
328 { .type = AMD_BIOS_PMUD, .inst = 1, .subpr = 0, .level = BDT_BOTH },
329 { .type = AMD_BIOS_PMUI, .inst = 2, .subpr = 0, .level = BDT_BOTH },
330 { .type = AMD_BIOS_PMUD, .inst = 2, .subpr = 0, .level = BDT_BOTH },
331 { .type = AMD_BIOS_PMUI, .inst = 3, .subpr = 0, .level = BDT_BOTH },
332 { .type = AMD_BIOS_PMUD, .inst = 3, .subpr = 0, .level = BDT_BOTH },
333 { .type = AMD_BIOS_PMUI, .inst = 4, .subpr = 0, .level = BDT_BOTH },
334 { .type = AMD_BIOS_PMUD, .inst = 4, .subpr = 0, .level = BDT_BOTH },
335 { .type = AMD_BIOS_PMUI, .inst = 5, .subpr = 0, .level = BDT_BOTH },
336 { .type = AMD_BIOS_PMUD, .inst = 5, .subpr = 0, .level = BDT_BOTH },
337 { .type = AMD_BIOS_PMUI, .inst = 6, .subpr = 0, .level = BDT_BOTH },
338 { .type = AMD_BIOS_PMUD, .inst = 6, .subpr = 0, .level = BDT_BOTH },
339 { .type = AMD_BIOS_PMUI, .inst = 7, .subpr = 0, .level = BDT_BOTH },
340 { .type = AMD_BIOS_PMUD, .inst = 7, .subpr = 0, .level = BDT_BOTH },
341 { .type = AMD_BIOS_PMUI, .inst = 9, .subpr = 0, .level = BDT_BOTH },
342 { .type = AMD_BIOS_PMUD, .inst = 9, .subpr = 0, .level = BDT_BOTH },
343 { .type = AMD_BIOS_PMUI, .inst = 10, .subpr = 0, .level = BDT_BOTH },
344 { .type = AMD_BIOS_PMUD, .inst = 10, .subpr = 0, .level = BDT_BOTH },
345 { .type = AMD_BIOS_PMUI, .inst = 11, .subpr = 0, .level = BDT_BOTH },
346 { .type = AMD_BIOS_PMUD, .inst = 11, .subpr = 0, .level = BDT_BOTH },
347 { .type = AMD_BIOS_PMUI, .inst = 12, .subpr = 0, .level = BDT_BOTH },
348 { .type = AMD_BIOS_PMUD, .inst = 12, .subpr = 0, .level = BDT_BOTH },
349 { .type = AMD_BIOS_PMUI, .inst = 13, .subpr = 0, .level = BDT_BOTH },
350 { .type = AMD_BIOS_PMUD, .inst = 13, .subpr = 0, .level = BDT_BOTH },
351 { .type = AMD_BIOS_PMUI, .inst = 1, .subpr = 1, .level = BDT_BOTH },
352 { .type = AMD_BIOS_PMUD, .inst = 1, .subpr = 1, .level = BDT_BOTH },
353 { .type = AMD_BIOS_PMUI, .inst = 2, .subpr = 1, .level = BDT_BOTH },
354 { .type = AMD_BIOS_PMUD, .inst = 2, .subpr = 1, .level = BDT_BOTH },
355 { .type = AMD_BIOS_PMUI, .inst = 3, .subpr = 1, .level = BDT_BOTH },
356 { .type = AMD_BIOS_PMUD, .inst = 3, .subpr = 1, .level = BDT_BOTH },
357 { .type = AMD_BIOS_PMUI, .inst = 4, .subpr = 1, .level = BDT_BOTH },
358 { .type = AMD_BIOS_PMUD, .inst = 4, .subpr = 1, .level = BDT_BOTH },
359 { .type = AMD_BIOS_PMUI, .inst = 5, .subpr = 1, .level = BDT_BOTH },
360 { .type = AMD_BIOS_PMUD, .inst = 5, .subpr = 1, .level = BDT_BOTH },
361 { .type = AMD_BIOS_PMUI, .inst = 6, .subpr = 1, .level = BDT_BOTH },
362 { .type = AMD_BIOS_PMUD, .inst = 6, .subpr = 1, .level = BDT_BOTH },
363 { .type = AMD_BIOS_PMUI, .inst = 7, .subpr = 1, .level = BDT_BOTH },
364 { .type = AMD_BIOS_PMUD, .inst = 7, .subpr = 1, .level = BDT_BOTH },
365 { .type = AMD_BIOS_PMUI, .inst = 9, .subpr = 1, .level = BDT_BOTH },
366 { .type = AMD_BIOS_PMUD, .inst = 9, .subpr = 1, .level = BDT_BOTH },
367 { .type = AMD_BIOS_PMUI, .inst = 10, .subpr = 1, .level = BDT_BOTH },
368 { .type = AMD_BIOS_PMUD, .inst = 10, .subpr = 1, .level = BDT_BOTH },
369 { .type = AMD_BIOS_PMUI, .inst = 11, .subpr = 1, .level = BDT_BOTH },
370 { .type = AMD_BIOS_PMUD, .inst = 11, .subpr = 1, .level = BDT_BOTH },
371 { .type = AMD_BIOS_PMUI, .inst = 12, .subpr = 1, .level = BDT_BOTH },
372 { .type = AMD_BIOS_PMUD, .inst = 12, .subpr = 1, .level = BDT_BOTH },
373 { .type = AMD_BIOS_PMUI, .inst = 13, .subpr = 1, .level = BDT_BOTH },
374 { .type = AMD_BIOS_PMUD, .inst = 13, .subpr = 1, .level = BDT_BOTH },
375 { .type = AMD_BIOS_UCODE, .inst = 0, .level = BDT_LVL2 },
376 { .type = AMD_BIOS_UCODE, .inst = 1, .level = BDT_LVL2 },
377 { .type = AMD_BIOS_UCODE, .inst = 2, .level = BDT_LVL2 },
378 { .type = AMD_BIOS_UCODE, .inst = 3, .level = BDT_LVL2 },
379 { .type = AMD_BIOS_UCODE, .inst = 4, .level = BDT_LVL2 },
380 { .type = AMD_BIOS_UCODE, .inst = 5, .level = BDT_LVL2 },
381 { .type = AMD_BIOS_UCODE, .inst = 6, .level = BDT_LVL2 },
382 { .type = AMD_BIOS_MP2_CFG, .level = BDT_LVL2 },
383 { .type = AMD_BIOS_PSP_SHARED_MEM, .inst = 0, .level = BDT_BOTH },
384 { .type = AMD_BIOS_INVALID },
385 };
386
387 #define RUN_BASE(ctx) (0xFFFFFFFF - (ctx).rom_size + 1)
388 #define RUN_OFFSET_MODE(ctx, offset, mode) \
389 ((mode) == AMD_ADDR_PHYSICAL ? RUN_BASE(ctx) + (offset) : \
390 ((mode) == AMD_ADDR_REL_BIOS ? (offset) : \
391 ((mode) == AMD_ADDR_REL_TAB ? (offset) - (ctx).current_table : (offset))))
392 #define RUN_OFFSET(ctx, offset) RUN_OFFSET_MODE((ctx), (offset), (ctx).address_mode)
393 #define RUN_TO_OFFSET(ctx, run) ((ctx).address_mode == AMD_ADDR_PHYSICAL ? \
394 (run) - RUN_BASE(ctx) : (run)) /* TODO: */
395 #define RUN_CURRENT(ctx) RUN_OFFSET((ctx), (ctx).current)
396 /* The mode in entry can not be higher than the header's.
397 For example, if table mode is 0, all the entry mode will be 0. */
398 #define RUN_CURRENT_MODE(ctx, mode) RUN_OFFSET_MODE((ctx), (ctx).current, \
399 (ctx).address_mode < (mode) ? (ctx).address_mode : (mode))
400 #define BUFF_OFFSET(ctx, offset) ((void *)((ctx).rom + (offset)))
401 #define BUFF_CURRENT(ctx) BUFF_OFFSET((ctx), (ctx).current)
402 #define BUFF_TO_RUN(ctx, ptr) RUN_OFFSET((ctx), ((char *)(ptr) - (ctx).rom))
403 #define BUFF_TO_RUN_MODE(ctx, ptr, mode) RUN_OFFSET_MODE((ctx), ((char *)(ptr) - (ctx).rom), \
404 (ctx).address_mode < (mode) ? (ctx).address_mode : (mode))
405 #define BUFF_ROOM(ctx) ((ctx).rom_size - (ctx).current)
406 /* AMD PSP Spec: Only set the address mode in entry if the table is mode 2 or 3. */
407 /* For address mode 3, it is not be used in any SOC family yet.
408 For address mode 1, we can use it to store and transfer the address mode.
409 It can reduce the complexity. */
410 #define SET_ADDR_MODE(table, mode) \
411 ((table)->header.additional_info_fields.address_mode == AMD_ADDR_REL_TAB || \
412 (table)->header.additional_info_fields.address_mode == AMD_ADDR_REL_BIOS || \
413 (table)->header.additional_info_fields.address_mode == AMD_ADDR_REL_SLOT \
414 ? (mode) : 0)
415 #define SET_ADDR_MODE_BY_TABLE(table) \
416 SET_ADDR_MODE((table), (table)->header.additional_info_fields.address_mode)
417
418
419 static void free_psp_firmware_filenames(amd_fw_entry *fw_table)
420 {
421 amd_fw_entry *index;
422
423 for (index = fw_table; index->type != AMD_FW_INVALID; index++) {
424 if (index->filename &&
425 index->type != AMD_FW_VERSTAGE_SIG &&
426 index->type != AMD_FW_PSP_VERSTAGE &&
427 index->type != AMD_FW_SPL &&
428 index->type != AMD_FW_PSP_WHITELIST) {
429 free(index->filename);
430 index->filename = NULL;
431 }
432 }
433 }
434
435 static void free_bdt_firmware_filenames(amd_bios_entry *fw_table)
436 {
437 amd_bios_entry *index;
438
439 for (index = fw_table; index->type != AMD_BIOS_INVALID; index++) {
440 if (index->filename &&
441 index->type != AMD_BIOS_APCB &&
442 index->type != AMD_BIOS_BIN &&
443 index->type != AMD_BIOS_APCB_BK &&
444 index->type != AMD_BIOS_UCODE) {
445 free(index->filename);
446 index->filename = NULL;
447 }
448 }
449 }
450
451 static void amdfwtool_cleanup(context *ctx)
452 {
453 free(ctx->rom);
454 ctx->rom = NULL;
455
456 /* Free the filename. */
457 free_psp_firmware_filenames(amd_psp_fw_table);
458 free_bdt_firmware_filenames(amd_bios_table);
459
460 free(ctx->amd_psp_fw_table_clean);
461 ctx->amd_psp_fw_table_clean = NULL;
462 free(ctx->amd_bios_table_clean);
463 ctx->amd_bios_table_clean = NULL;
464 }
465
466 void assert_fw_entry(uint32_t count, uint32_t max, context *ctx)
467 {
468 if (count >= max) {
469 fprintf(stderr, "Error: BIOS entries (%d) exceeds max allowed items "
470 "(%d)\n", count, max);
471 amdfwtool_cleanup(ctx);
472 exit(1);
473 }
474 }
475
476 static void set_current_pointer(context *ctx, uint32_t value)
477 {
478 if (ctx->current_pointer_saved != 0xFFFFFFFF &&
479 ctx->current_pointer_saved != ctx->current) {
480 fprintf(stderr, "Error: The pointer is changed elsewhere\n");
481 amdfwtool_cleanup(ctx);
482 exit(1);
483 }
484
485 ctx->current = value;
486
487 if (ctx->current > ctx->rom_size) {
488 fprintf(stderr, "Error: Packing data causes overflow\n");
489 amdfwtool_cleanup(ctx);
490 exit(1);
491 }
492
493 ctx->current_pointer_saved = ctx->current;
494 }
495
496 static void adjust_current_pointer(context *ctx, uint32_t add, uint32_t align)
497 {
498 /* Get */
499 set_current_pointer(ctx, ALIGN_UP(ctx->current + add, align));
500 }
501
502 static void *new_psp_dir(context *ctx, int multi, uint32_t cookie)
503 {
504 void *ptr;
505
506 /*
507 * Force both onto boundary when multi. Primary table is after
508 * updatable table, so alignment ensures primary can stay intact
509 * if secondary is reprogrammed.
510 */
511 if (multi)
512 adjust_current_pointer(ctx, 0, TABLE_ERASE_ALIGNMENT);
513 else
514 adjust_current_pointer(ctx, 0, TABLE_ALIGNMENT);
515
516 ptr = BUFF_CURRENT(*ctx);
517 ((psp_directory_header *)ptr)->cookie = cookie;
518 ((psp_directory_header *)ptr)->num_entries = 0;
519 ((psp_directory_header *)ptr)->additional_info = 0;
520 ((psp_directory_header *)ptr)->additional_info_fields.address_mode = ctx->address_mode;
521 ((psp_directory_header *)ptr)->additional_info_fields.spi_block_size = 1;
522 ((psp_directory_header *)ptr)->additional_info_fields.base_addr = 0;
523 adjust_current_pointer(ctx,
524 sizeof(psp_directory_header) + MAX_PSP_ENTRIES * sizeof(psp_directory_entry),
525 1);
526 return ptr;
527 }
528
529 static void *new_ish_dir(context *ctx)
530 {
531 void *ptr;
532 adjust_current_pointer(ctx, 0, TABLE_ALIGNMENT);
533 ptr = BUFF_CURRENT(*ctx);
534 adjust_current_pointer(ctx, TABLE_ALIGNMENT, 1);
535
536 return ptr;
537 }
538
539 static void *new_combo_dir(context *ctx, uint32_t cookie)
540 {
541 void *ptr;
542
543 adjust_current_pointer(ctx, 0, TABLE_ALIGNMENT);
544 ptr = BUFF_CURRENT(*ctx);
545 ((psp_combo_header *)ptr)->cookie = cookie;
546 /* lookup mode is hardcoded for now. */
547 ((psp_combo_header *)ptr)->lookup = 1;
548 ((psp_combo_header *)ptr)->reserved[0] = 0;
549 ((psp_combo_header *)ptr)->reserved[1] = 0;
550
551 adjust_current_pointer(ctx,
552 sizeof(psp_combo_header) + MAX_COMBO_ENTRIES * sizeof(psp_combo_entry),
553 1);
554 return ptr;
555 }
556
557 /*
558 * For some SOC generations the APOB_NV binary seems to be treated special regarding the
559 * interpretaion of the source address. No matter the address_mode specified for the address
560 * the memory ABL always seems to the interpret the source address as MMIO address even if
561 * AMD_ADDR_REL_BIOS is specified. So for them we need to always use an MMIO address.
562 * This seems to be a bug which affects all SOCs before phoenix generation.
563 */
564 static bool has_apob_nv_quirk(enum platform platform_type)
565 {
566 switch (platform_type) {
567 case PLATFORM_CARRIZO:
568 case PLATFORM_STONEYRIDGE:
569 case PLATFORM_RAVEN:
570 case PLATFORM_PICASSO:
571 case PLATFORM_RENOIR:
572 case PLATFORM_CEZANNE:
573 case PLATFORM_MENDOCINO:
574 case PLATFORM_LUCIENNE:
575 return true;
576 default:
577 return false;
578 }
579 }
580
581 static void copy_psp_header(void *bak, void *orig)
582 {
583 uint32_t count = ((psp_directory_header *)orig)->num_entries;
584 memcpy(bak, orig, count * sizeof(bios_directory_entry) + sizeof(psp_directory_table));
585 }
586
587 static void fill_dir_header(void *directory, uint32_t count, context *ctx)
588 {
589 if (ctx == NULL || directory == NULL) {
590 fprintf(stderr, "Calling %s with NULL pointers\n", __func__);
591 return;
592 }
593
594 psp_combo_directory *cdir = directory;
595 psp_directory_table *dir = directory;
596 bios_directory_table *bdir = directory;
597 /* The cookies have same offsets. */
598 uint32_t cookie = ((psp_directory_table *)directory)->header.cookie;
599 uint32_t table_size = 0;
600
601 /* The table size needs to be 0x1000 aligned. So align the end of table. */
602 adjust_current_pointer(ctx, 0, TABLE_ALIGNMENT);
603
604 switch (cookie) {
605 case PSP2_COOKIE:
606 case BHD2_COOKIE:
607 cdir->header.num_entries = count;
608 /* checksum everything that comes after the Checksum field */
609 cdir->header.checksum = fletcher32(&cdir->header.num_entries,
610 count * sizeof(psp_combo_entry)
611 + sizeof(cdir->header.num_entries)
612 + sizeof(cdir->header.lookup)
613 + 2 * sizeof(cdir->header.reserved[0]));
614 break;
615 case PSP_COOKIE:
616 case PSPL2_COOKIE:
617 /* The table size is only set once. Later calls only update
618 * the count and fletcher. So does the BIOS table. */
619 if (dir->header.additional_info_fields.dir_size == 0) {
620 table_size = ctx->current - ctx->current_table;
621 if ((table_size % TABLE_ALIGNMENT) != 0 &&
622 (table_size / TABLE_ALIGNMENT) != 0) {
623 fprintf(stderr, "The PSP table size should be 4K aligned\n");
624 amdfwtool_cleanup(ctx);
625 exit(1);
626 }
627 dir->header.additional_info_fields.dir_size =
628 table_size / TABLE_ALIGNMENT;
629 }
630 dir->header.num_entries = count;
631 /* checksum everything that comes after the Checksum field */
632 dir->header.checksum = fletcher32(&dir->header.num_entries,
633 count * sizeof(psp_directory_entry)
634 + sizeof(dir->header.num_entries)
635 + sizeof(dir->header.additional_info));
636 break;
637 case BHD_COOKIE:
638 case BHDL2_COOKIE:
639 if (bdir->header.additional_info_fields.dir_size == 0) {
640 table_size = ctx->current - ctx->current_table;
641 if ((table_size % TABLE_ALIGNMENT) != 0 &&
642 table_size / TABLE_ALIGNMENT != 0) {
643 fprintf(stderr, "The BIOS table size should be 4K aligned\n");
644 amdfwtool_cleanup(ctx);
645 exit(1);
646 }
647 bdir->header.additional_info_fields.dir_size =
648 table_size / TABLE_ALIGNMENT;
649 }
650 bdir->header.num_entries = count;
651 /* checksum everything that comes after the Checksum field */
652 bdir->header.checksum = fletcher32(&bdir->header.num_entries,
653 count * sizeof(bios_directory_entry)
654 + sizeof(bdir->header.num_entries)
655 + sizeof(bdir->header.additional_info));
656 break;
657 }
658 }
659
660 static void fill_psp_directory_to_efs(embedded_firmware *amd_romsig, void *pspdir,
661 context *ctx, amd_cb_config *cb_config)
662 {
663 switch (cb_config->soc_id) {
664 case PLATFORM_UNKNOWN:
665 amd_romsig->psp_directory =
666 BUFF_TO_RUN_MODE(*ctx, pspdir, AMD_ADDR_REL_BIOS);
667 break;
668 case PLATFORM_CEZANNE:
669 case PLATFORM_MENDOCINO:
670 case PLATFORM_PHOENIX:
671 case PLATFORM_GLINDA:
672 case PLATFORM_CARRIZO:
673 case PLATFORM_STONEYRIDGE:
674 case PLATFORM_RAVEN:
675 case PLATFORM_PICASSO:
676 case PLATFORM_LUCIENNE:
677 case PLATFORM_RENOIR:
678 case PLATFORM_GENOA:
679 case PLATFORM_FAEGAN:
680 default:
681 /* for combo, it is also combo_psp_directory */
682 amd_romsig->new_psp_directory =
683 BUFF_TO_RUN_MODE(*ctx, pspdir, AMD_ADDR_REL_BIOS);
684 break;
685 }
686 }
687
688 static void fill_psp_bak_directory_to_efs(embedded_firmware *amd_romsig, void *pspdir_bak,
689 context *ctx, amd_cb_config *cb_config)
690 {
691 if (cb_config->recovery_ab)
692 amd_romsig->psp_bak_directory =
693 BUFF_TO_RUN_MODE(*ctx, pspdir_bak, AMD_ADDR_REL_BIOS);
694 }
695
696 static void fill_bios_directory_to_efs(embedded_firmware *amd_romsig, void *biosdir,
697 context *ctx, amd_cb_config *cb_config)
698 {
699 switch (cb_config->soc_id) {
700 case PLATFORM_RENOIR:
701 case PLATFORM_LUCIENNE:
702 case PLATFORM_CEZANNE:
703 case PLATFORM_GENOA:
704 if (!cb_config->recovery_ab)
705 amd_romsig->bios3_entry =
706 BUFF_TO_RUN_MODE(*ctx, biosdir, AMD_ADDR_REL_BIOS);
707 break;
708 case PLATFORM_MENDOCINO:
709 case PLATFORM_PHOENIX:
710 case PLATFORM_GLINDA:
711 case PLATFORM_FAEGAN:
712 break;
713 case PLATFORM_CARRIZO:
714 case PLATFORM_STONEYRIDGE:
715 case PLATFORM_RAVEN:
716 case PLATFORM_PICASSO:
717 default:
718 amd_romsig->bios1_entry =
719 BUFF_TO_RUN_MODE(*ctx, biosdir, AMD_ADDR_REL_BIOS);
720 break;
721 }
722 }
723
724 static uint32_t get_psp_id(enum platform soc_id)
725 {
726 uint32_t psp_id;
727 switch (soc_id) {
728 case PLATFORM_RAVEN:
729 case PLATFORM_PICASSO:
730 psp_id = 0xBC0A0000;
731 break;
732 case PLATFORM_RENOIR:
733 case PLATFORM_LUCIENNE:
734 psp_id = 0xBC0C0000;
735 break;
736 case PLATFORM_CEZANNE:
737 psp_id = 0xBC0C0140;
738 break;
739 case PLATFORM_MENDOCINO:
740 psp_id = 0xBC0D0900;
741 break;
742 case PLATFORM_STONEYRIDGE:
743 psp_id = 0x10220B00;
744 break;
745 case PLATFORM_GLINDA:
746 psp_id = 0xBC0E0200;
747 break;
748 case PLATFORM_PHOENIX:
749 psp_id = 0xBC0D0400;
750 break;
751 case PLATFORM_GENOA:
752 psp_id = 0xBC0C0111;
753 break;
754 case PLATFORM_FAEGAN:
755 psp_id = 0xbc0e1000;
756 break;
757 case PLATFORM_CARRIZO:
758 default:
759 psp_id = 0;
760 break;
761 }
762 return psp_id;
763 }
764
765 static void integrate_firmwares(context *ctx,
766 embedded_firmware *romsig,
767 amd_fw_entry *fw_table)
768 {
769 ssize_t bytes;
770 uint32_t i;
771
772 adjust_current_pointer(ctx, 0, BLOB_ALIGNMENT);
773
774 for (i = 0; fw_table[i].type != AMD_FW_INVALID; i++) {
775 if (fw_table[i].filename != NULL) {
776 switch (fw_table[i].type) {
777 case AMD_FW_IMC:
778 adjust_current_pointer(ctx, 0, 0x10000U);
779 romsig->imc_entry = RUN_CURRENT(*ctx);
780 break;
781 case AMD_FW_GEC:
782 romsig->gec_entry = RUN_CURRENT(*ctx);
783 break;
784 case AMD_FW_XHCI:
785 romsig->xhci_entry = RUN_CURRENT(*ctx);
786 break;
787 default:
788 /* Error */
789 break;
790 }
791
792 bytes = copy_blob(BUFF_CURRENT(*ctx),
793 fw_table[i].filename, BUFF_ROOM(*ctx));
794 if (bytes < 0) {
795 amdfwtool_cleanup(ctx);
796 exit(1);
797 }
798
799 adjust_current_pointer(ctx, bytes, BLOB_ALIGNMENT);
800 }
801 }
802 }
803
804 static void output_manifest(int manifest_fd, amd_fw_entry *fw_entry)
805 {
806 struct amd_fw_header hdr;
807 int blob_fd;
808 ssize_t bytes;
809
810 blob_fd = open(fw_entry->filename, O_RDONLY);
811 if (blob_fd < 0) {
812 fprintf(stderr, "Error opening file: %s: %s\n",
813 fw_entry->filename, strerror(errno));
814 return;
815 }
816
817 bytes = read(blob_fd, &hdr, sizeof(hdr));
818 if (bytes != sizeof(hdr)) {
819 close(blob_fd);
820 fprintf(stderr, "Error while reading %s\n", fw_entry->filename);
821 return;
822 }
823
824 dprintf(manifest_fd, "type: 0x%02x ver:%02x.%02x.%02x.%02x\n",
825 fw_entry->type, hdr.version[3], hdr.version[2],
826 hdr.version[1], hdr.version[0]);
827
828 close(blob_fd);
829
830 }
831
832 static void dump_blob_version(char *manifest_file, amd_fw_entry *fw_table)
833 {
834 amd_fw_entry *index;
835 int manifest_fd;
836
837 manifest_fd = open(manifest_file, O_WRONLY | O_CREAT | O_TRUNC, 0666);
838 if (manifest_fd < 0) {
839 fprintf(stderr, "Error opening file: %s: %s\n",
840 manifest_file, strerror(errno));
841 return;
842 }
843
844 for (index = fw_table; index->type != AMD_FW_INVALID; index++) {
845 if (!(index->filename))
846 continue;
847
848 if (index->generate_manifest == true)
849 output_manifest(manifest_fd, index);
850 }
851
852 close(manifest_fd);
853 }
854
855 /* For debugging */
856 static void dump_psp_firmwares(amd_fw_entry *fw_table)
857 {
858 amd_fw_entry *index;
859
860 printf("PSP firmware components:\n");
861 for (index = fw_table; index->type != AMD_FW_INVALID; index++) {
862 if (index->type == AMD_PSP_FUSE_CHAIN)
863 printf(" %2x: level=%x, subprog=%x, inst=%x\n",
864 index->type, index->level, index->subprog, index->inst);
865 else if (index->filename)
866 printf(" %2x: level=%x, subprog=%x, inst=%x, %s\n",
867 index->type, index->level, index->subprog, index->inst,
868 index->filename);
869 }
870 }
871
872 static void dump_bdt_firmwares(amd_bios_entry *fw_table)
873 {
874 amd_bios_entry *index;
875
876 printf("BIOS Directory Table (BDT) components:\n");
877 for (index = fw_table; index->type != AMD_BIOS_INVALID; index++) {
878 if (index->filename)
879 printf(" %2x: level=%x, %s\n",
880 index->type, index->level, index->filename);
881 }
882 }
883
884 static void dump_image_addresses(context *ctx)
885 {
886 printf("romsig offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->amd_romsig_ptr));
887 printf("PSP L1 offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->pspdir));
888 if (ctx->pspdir_bak != NULL)
889 printf("PSP L1 backup offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->pspdir_bak));
890 if (ctx->pspdir2 != NULL)
891 printf("PSP L2(A) offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->pspdir2));
892 if (ctx->ish_a_dir != NULL)
893 printf("ISHA offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->ish_a_dir));
894 if (ctx->ish_b_dir != NULL)
895 printf("ISHB offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->ish_b_dir));
896 if (ctx->pspdir2_b != NULL)
897 printf("PSP L2B offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->pspdir2_b));
898 if (ctx->biosdir != NULL)
899 printf("BHD offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->biosdir));
900 if (ctx->biosdir2 != NULL)
901 printf("BHD L2(A) offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->biosdir2));
902 if (ctx->biosdir2_b != NULL)
903 printf("BHD L2B offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->biosdir2_b));
904 if (ctx->psp_combo_dir != NULL)
905 printf("PSP combo offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->psp_combo_dir));
906 if (ctx->bhd_combo_dir != NULL)
907 printf("BHD combo offset:%lx\n", BUFF_TO_RUN(*ctx, ctx->bhd_combo_dir));
908 }
909
910 static void integrate_psp_ab(context *ctx, psp_directory_table *pspdir,
911 psp_directory_table *pspdir2, ish_directory_table *ish,
912 amd_fw_type ab, enum platform soc_id)
913 {
914 uint32_t count;
915 uint32_t current_table_save;
916
917 current_table_save = ctx->current_table;
918 ctx->current_table = BUFF_TO_RUN_MODE(*ctx, pspdir, AMD_ADDR_REL_BIOS);
919 count = pspdir->header.num_entries;
920 assert_fw_entry(count, MAX_PSP_ENTRIES, ctx);
921 pspdir->entries[count].type = (uint8_t)ab;
922 pspdir->entries[count].subprog = 0;
923 pspdir->entries[count].rsvd = 0;
924 if (ish != NULL) {
925 ish->pl2_location = BUFF_TO_RUN_MODE(*ctx, pspdir2, AMD_ADDR_REL_BIOS);
926 ish->boot_priority = ab == AMD_FW_RECOVERYAB_A ? 0xFFFFFFFF : 1;
927 ish->update_retry_count = 2;
928 ish->glitch_retry_count = 0;
929 ish->psp_id = get_psp_id(soc_id);
930 ish->checksum = fletcher32(&ish->boot_priority,
931 sizeof(ish_directory_table) - sizeof(uint32_t));
932 pspdir->entries[count].addr =
933 BUFF_TO_RUN_MODE(*ctx, ish, AMD_ADDR_REL_BIOS);
934 pspdir->entries[count].address_mode =
935 SET_ADDR_MODE(pspdir, AMD_ADDR_REL_BIOS);
936 pspdir->entries[count].size = TABLE_ALIGNMENT;
937 } else {
938 pspdir->entries[count].addr =
939 BUFF_TO_RUN_MODE(*ctx, pspdir2, AMD_ADDR_REL_BIOS);
940 pspdir->entries[count].address_mode =
941 SET_ADDR_MODE(pspdir, AMD_ADDR_REL_BIOS);
942 pspdir->entries[count].size = _MAX(TABLE_L2_SIZE_MAX,
943 pspdir2->header.num_entries *
944 sizeof(psp_directory_entry) +
945 sizeof(psp_directory_header));
946 }
947
948 count++;
949 fill_dir_header(pspdir, count, ctx);
950 ctx->current_table = current_table_save;
951 }
952
953 static void integrate_psp_levels(context *ctx,
954 amd_cb_config *cb_config)
955 {
956 uint32_t current_table_save;
957 bool recovery_ab = cb_config->recovery_ab;
958 unsigned int count;
959 psp_directory_table *pspdir, *pspdir2, *pspdir2_b;
960 bool use_only_a = (cb_config->soc_id == PLATFORM_PHOENIX); /* TODO: b:285390041 */
961
962 pspdir = ctx->pspdir;
963 pspdir2 = ctx->pspdir2;
964 pspdir2_b = ctx->pspdir2_b;
965 count = pspdir->header.num_entries;
966
967 current_table_save = ctx->current_table;
968 ctx->current_table = BUFF_TO_RUN_MODE(*ctx, pspdir, AMD_ADDR_REL_BIOS);
969 if (recovery_ab && (pspdir2 != NULL)) {
970 if (cb_config->need_ish) { /* Need ISH */
971 ctx->ish_a_dir = new_ish_dir(ctx);
972 if (pspdir2_b != NULL)
973 ctx->ish_b_dir = new_ish_dir(ctx);
974 }
975 integrate_psp_ab(ctx, pspdir, pspdir2, ctx->ish_a_dir,
976 AMD_FW_RECOVERYAB_A, cb_config->soc_id);
977 if (pspdir2_b != NULL)
978 integrate_psp_ab(ctx, pspdir, pspdir2_b, ctx->ish_b_dir,
979 use_only_a ? AMD_FW_RECOVERYAB_A : AMD_FW_RECOVERYAB_B,
980 cb_config->soc_id);
981 else
982 integrate_psp_ab(ctx, pspdir, pspdir2, ctx->ish_a_dir,
983 use_only_a ? AMD_FW_RECOVERYAB_A : AMD_FW_RECOVERYAB_B,
984 cb_config->soc_id);
985
986 copy_psp_header(ctx->pspdir_bak, ctx->pspdir);
987 } else if (pspdir2 != NULL) {
988 assert_fw_entry(count, MAX_PSP_ENTRIES, ctx);
989 pspdir->entries[count].type = AMD_FW_L2_PTR;
990 pspdir->entries[count].subprog = 0;
991 pspdir->entries[count].rsvd = 0;
992 pspdir->entries[count].size = sizeof(pspdir2->header)
993 + pspdir2->header.num_entries
994 * sizeof(psp_directory_entry);
995
996 pspdir->entries[count].addr =
997 BUFF_TO_RUN_MODE(*ctx, pspdir2, AMD_ADDR_REL_BIOS);
998 pspdir->entries[count].address_mode =
999 SET_ADDR_MODE(pspdir, AMD_ADDR_REL_BIOS);
1000 count++;
1001 fill_dir_header(pspdir, count, ctx);
1002 }
1003 ctx->current_table = current_table_save;
1004 }
1005
1006 static void integrate_psp_firmwares(context *ctx,
1007 amd_fw_entry *fw_table,
1008 uint32_t cookie,
1009 amd_cb_config *cb_config)
1010 {
1011 ssize_t bytes;
1012 unsigned int i, count;
1013 int level;
1014 uint32_t size;
1015 psp_directory_table *pspdir;
1016 uint64_t addr;
1017 uint32_t current_table_save;
1018 bool recovery_ab = cb_config->recovery_ab;
1019
1020 /* This function can create a primary table, a secondary table, or a
1021 * flattened table which contains all applicable types. These if-else
1022 * statements infer what the caller intended. If a 2nd-level cookie
1023 * is passed, clearly a 2nd-level table is intended. However, a
1024 * 1st-level cookie may indicate level 1 or flattened.
1025 */
1026 pspdir = new_psp_dir(ctx, cb_config->multi_level, cookie);
1027
1028 if (cookie == PSP_COOKIE) {
1029 ctx->pspdir = pspdir;
1030 if (recovery_ab)
1031 ctx->pspdir_bak = new_psp_dir(ctx, cb_config->multi_level, cookie);
1032 } else if (cookie == PSPL2_COOKIE) {
1033 if (ctx->pspdir2 == NULL)
1034 ctx->pspdir2 = pspdir;
1035 else if (ctx->pspdir2_b == NULL)
1036 ctx->pspdir2_b = pspdir;
1037 }
1038
1039 if (!cb_config->multi_level)
1040 level = PSP_BOTH;
1041 else if (cookie == PSPL2_COOKIE)
1042 level = PSP_LVL2;
1043 else if (cookie == PSP_COOKIE)
1044 level = PSP_LVL1;
1045 else
1046 level = PSP_BOTH;
1047
1048 if (recovery_ab) {
1049 if (cookie == PSPL2_COOKIE)
1050 level = PSP_LVL2_AB;
1051 else if (cookie == PSP_COOKIE)
1052 level = PSP_LVL1_AB;
1053 else
1054 level = PSP_BOTH_AB;
1055 }
1056 current_table_save = ctx->current_table;
1057 ctx->current_table = BUFF_TO_RUN_MODE(*ctx, pspdir, AMD_ADDR_REL_BIOS);
1058 adjust_current_pointer(ctx, 0, TABLE_ALIGNMENT);
1059
1060 for (i = 0, count = 0; fw_table[i].type != AMD_FW_INVALID; i++) {
1061 if (!(fw_table[i].level & level))
1062 continue;
1063
1064 assert_fw_entry(count, MAX_PSP_ENTRIES, ctx);
1065
1066 if (fw_table[i].type == AMD_TOKEN_UNLOCK) {
1067 if (!fw_table[i].other)
1068 continue;
1069 adjust_current_pointer(ctx, 0, ERASE_ALIGNMENT);
1070 pspdir->entries[count].type = fw_table[i].type;
1071 pspdir->entries[count].size = 4096; /* TODO: doc? */
1072 pspdir->entries[count].addr = RUN_CURRENT(*ctx);
1073 pspdir->entries[count].address_mode = SET_ADDR_MODE_BY_TABLE(pspdir);
1074 pspdir->entries[count].subprog = fw_table[i].subprog;
1075 pspdir->entries[count].rsvd = 0;
1076 adjust_current_pointer(ctx, 4096, 0x100U);
1077 count++;
1078 } else if (fw_table[i].type == AMD_PSP_FUSE_CHAIN) {
1079 pspdir->entries[count].type = fw_table[i].type;
1080 pspdir->entries[count].subprog = fw_table[i].subprog;
1081 pspdir->entries[count].rsvd = 0;
1082 pspdir->entries[count].size = 0xFFFFFFFF;
1083 pspdir->entries[count].addr = fw_table[i].other;
1084 pspdir->entries[count].address_mode = 0;
1085 count++;
1086 } else if (fw_table[i].type == AMD_FW_PSP_NVRAM ||
1087 fw_table[i].type == AMD_RPMC_NVRAM) {
1088 if (fw_table[i].filename == NULL) {
1089 if (fw_table[i].size == 0)
1090 continue;
1091 size = fw_table[i].size;
1092 addr = fw_table[i].dest;
1093 if (addr != ALIGN_UP(addr, ERASE_ALIGNMENT)) {
1094 fprintf(stderr,
1095 "Error: PSP NVRAM section not aligned with erase block size.\n\n");
1096 amdfwtool_cleanup(ctx);
1097 exit(1);
1098 }
1099 } else {
1100 adjust_current_pointer(ctx, 0, ERASE_ALIGNMENT);
1101 bytes = copy_blob(BUFF_CURRENT(*ctx),
1102 fw_table[i].filename, BUFF_ROOM(*ctx));
1103 if (bytes <= 0) {
1104 amdfwtool_cleanup(ctx);
1105 exit(1);
1106 }
1107
1108 size = ALIGN_UP(bytes, ERASE_ALIGNMENT);
1109 addr = RUN_CURRENT(*ctx);
1110 adjust_current_pointer(ctx, bytes, BLOB_ERASE_ALIGNMENT);
1111 }
1112
1113 pspdir->entries[count].type = fw_table[i].type;
1114 pspdir->entries[count].subprog = fw_table[i].subprog;
1115 pspdir->entries[count].rsvd = 0;
1116 pspdir->entries[count].size = size;
1117 pspdir->entries[count].addr = addr;
1118
1119 pspdir->entries[count].address_mode =
1120 SET_ADDR_MODE(pspdir, AMD_ADDR_REL_BIOS);
1121
1122 count++;
1123 } else if (fw_table[i].filename != NULL) {
1124 if (fw_table[i].addr_signed) {
1125 pspdir->entries[count].addr =
1126 RUN_OFFSET(*ctx, fw_table[i].addr_signed);
1127 pspdir->entries[count].address_mode =
1128 SET_ADDR_MODE_BY_TABLE(pspdir);
1129 bytes = fw_table[i].file_size;
1130 } else {
1131 bytes = copy_blob(BUFF_CURRENT(*ctx),
1132 fw_table[i].filename, BUFF_ROOM(*ctx));
1133 if (bytes < 0) {
1134 amdfwtool_cleanup(ctx);
1135 exit(1);
1136 }
1137 pspdir->entries[count].addr = RUN_CURRENT(*ctx);
1138 pspdir->entries[count].address_mode =
1139 SET_ADDR_MODE_BY_TABLE(pspdir);
1140 adjust_current_pointer(ctx, bytes, BLOB_ALIGNMENT);
1141 }
1142
1143 pspdir->entries[count].type = fw_table[i].type;
1144 pspdir->entries[count].subprog = fw_table[i].subprog;
1145 pspdir->entries[count].rsvd = 0;
1146 pspdir->entries[count].inst = fw_table[i].inst;
1147 pspdir->entries[count].size = (uint32_t)bytes;
1148
1149 count++;
1150 } else {
1151 /* This APU doesn't have this firmware. */
1152 }
1153 }
1154
1155 fill_dir_header(pspdir, count, ctx);
1156 ctx->current_table = current_table_save;
1157 }
1158
1159 static void add_psp_firmware_entry(context *ctx,
1160 psp_directory_table *pspdir,
1161 void *table, amd_fw_type type, uint32_t size)
1162 {
1163 uint32_t count = pspdir->header.num_entries;
1164 uint32_t index;
1165 uint32_t current_table_save;
1166
1167 current_table_save = ctx->current_table;
1168 ctx->current_table = BUFF_TO_RUN_MODE(*ctx, pspdir, AMD_ADDR_REL_BIOS);
1169
1170 /* If there is an entry of "type", replace it. */
1171 for (index = 0; index < count; index++) {
1172 if (pspdir->entries[index].type == (uint8_t)type)
1173 break;
1174 }
1175
1176 assert_fw_entry(count, MAX_PSP_ENTRIES, ctx);
1177 pspdir->entries[index].type = (uint8_t)type;
1178 pspdir->entries[index].subprog = 0;
1179 pspdir->entries[index].rsvd = 0;
1180 pspdir->entries[index].addr = BUFF_TO_RUN(*ctx, table);
1181 pspdir->entries[index].address_mode = SET_ADDR_MODE_BY_TABLE(pspdir);
1182 pspdir->entries[index].size = size;
1183 if (index == count)
1184 count++;
1185
1186 fill_dir_header(pspdir, count, ctx);
1187 ctx->current_table = current_table_save;
1188 }
1189
1190 static void *new_bios_dir(context *ctx, bool multi, uint32_t cookie)
1191 {
1192 void *ptr;
1193
1194 /*
1195 * Force both onto boundary when multi. Primary table is after
1196 * updatable table, so alignment ensures primary can stay intact
1197 * if secondary is reprogrammed.
1198 */
1199 if (multi)
1200 adjust_current_pointer(ctx, 0, TABLE_ERASE_ALIGNMENT);
1201 else
1202 adjust_current_pointer(ctx, 0, TABLE_ALIGNMENT);
1203 ptr = BUFF_CURRENT(*ctx);
1204 ((bios_directory_hdr *) ptr)->cookie = cookie;
1205 ((bios_directory_hdr *) ptr)->additional_info = 0;
1206 ((bios_directory_hdr *) ptr)->additional_info_fields.address_mode = ctx->address_mode;
1207 ((bios_directory_hdr *) ptr)->additional_info_fields.spi_block_size = 1;
1208 ((bios_directory_hdr *) ptr)->additional_info_fields.base_addr = 0;
1209 adjust_current_pointer(ctx,
1210 sizeof(bios_directory_hdr) + MAX_BIOS_ENTRIES * sizeof(bios_directory_entry),
1211 1);
1212 return ptr;
1213 }
1214
1215 static int locate_bdt_bios(bios_directory_table *level,
1216 uint64_t *source, uint32_t *size)
1217 {
1218 uint32_t i;
1219
1220 *source = 0;
1221 *size = 0;
1222 if (!level)
1223 return 0;
1224
1225 for (i = 0 ; i < level->header.num_entries ; i++) {
1226 if (level->entries[i].type == AMD_BIOS_BIN) {
1227 *source = level->entries[i].source;
1228 *size = level->entries[i].size;
1229 return 1;
1230 }
1231 }
1232 return 0;
1233 }
1234
1235 static int have_bios_tables(amd_bios_entry *table)
1236 {
1237 int i;
1238
1239 for (i = 0 ; table[i].type != AMD_BIOS_INVALID; i++) {
1240 if (table[i].level & BDT_LVL1 && table[i].filename)
1241 return 1;
1242 }
1243 return 0;
1244 }
1245
1246 int find_bios_entry(amd_bios_type type)
1247 {
1248 int i;
1249
1250 for (i = 0; amd_bios_table[i].type != AMD_BIOS_INVALID; i++) {
1251 if (amd_bios_table[i].type == type)
1252 return i;
1253 }
1254 return -1;
1255 }
1256
1257 static void add_bios_apcb_bk_entry(bios_directory_table *biosdir, unsigned int idx,
1258 int inst, uint32_t size, uint64_t source)
1259 {
1260 int i;
1261
1262 for (i = 0; amd_bios_table[i].type != AMD_BIOS_INVALID; i++) {
1263 if (amd_bios_table[i].type == AMD_BIOS_APCB_BK &&
1264 amd_bios_table[i].inst == inst)
1265 break;
1266 }
1267
1268 if (amd_bios_table[i].type != AMD_BIOS_APCB_BK)
1269 return;
1270
1271 biosdir->entries[idx].type = amd_bios_table[i].type;
1272 biosdir->entries[idx].region_type = amd_bios_table[i].region_type;
1273 biosdir->entries[idx].dest = amd_bios_table[i].dest ?
1274 amd_bios_table[i].dest : (uint64_t)-1;
1275 biosdir->entries[idx].reset = amd_bios_table[i].reset;
1276 biosdir->entries[idx].copy = amd_bios_table[i].copy;
1277 biosdir->entries[idx].ro = amd_bios_table[i].ro;
1278 biosdir->entries[idx].compressed = amd_bios_table[i].zlib;
1279 biosdir->entries[idx].inst = amd_bios_table[i].inst;
1280 biosdir->entries[idx].subprog = amd_bios_table[i].subpr;
1281 biosdir->entries[idx].size = size;
1282 biosdir->entries[idx].source = source;
1283 biosdir->entries[idx].address_mode = SET_ADDR_MODE_BY_TABLE(biosdir);
1284 }
1285
1286 static void integrate_bios_levels(context *ctx, amd_cb_config *cb_config)
1287 {
1288 unsigned int count;
1289 uint32_t current_table_save;
1290
1291 if (cb_config->recovery_ab) {
1292 add_psp_firmware_entry(ctx, ctx->pspdir2, ctx->biosdir2,
1293 AMD_FW_BIOS_TABLE, TABLE_L2_SIZE_MAX);
1294 if (ctx->pspdir2_b != NULL)
1295 add_psp_firmware_entry(ctx, ctx->pspdir2_b,
1296 ctx->biosdir2_b, AMD_FW_BIOS_TABLE,
1297 TABLE_L2_SIZE_MAX);
1298 } else if (ctx->biosdir2) {
1299 current_table_save = ctx->current_table;
1300 ctx->current_table = BUFF_TO_RUN_MODE(*ctx, ctx->biosdir, AMD_ADDR_REL_BIOS);
1301 count = ctx->biosdir->header.num_entries;
1302 assert_fw_entry(count, MAX_BIOS_ENTRIES, ctx);
1303 ctx->biosdir->entries[count].type = AMD_BIOS_L2_PTR;
1304 ctx->biosdir->entries[count].region_type = 0;
1305 ctx->biosdir->entries[count].size =
1306 + MAX_BIOS_ENTRIES
1307 * sizeof(bios_directory_entry);
1308 ctx->biosdir->entries[count].source =
1309 BUFF_TO_RUN(*ctx, ctx->biosdir2);
1310 ctx->biosdir->entries[count].address_mode =
1311 SET_ADDR_MODE(ctx->biosdir, AMD_ADDR_REL_BIOS);
1312 ctx->biosdir->entries[count].subprog = 0;
1313 ctx->biosdir->entries[count].inst = 0;
1314 ctx->biosdir->entries[count].copy = 0;
1315 ctx->biosdir->entries[count].compressed = 0;
1316 ctx->biosdir->entries[count].dest = -1;
1317 ctx->biosdir->entries[count].reset = 0;
1318 ctx->biosdir->entries[count].ro = 0;
1319 count++;
1320 fill_dir_header(ctx->biosdir, count, ctx);
1321 ctx->current_table = current_table_save;
1322 }
1323 }
1324 static void integrate_bios_firmwares(context *ctx,
1325 amd_bios_entry *fw_table,
1326 uint32_t cookie,
1327 amd_cb_config *cb_config)
1328 {
1329 ssize_t bytes;
1330 unsigned int i, count;
1331 int level;
1332 int apob_idx;
1333 uint32_t size;
1334 uint64_t source;
1335 uint32_t current_table_save;
1336 bios_directory_table *biosdir;
1337
1338 biosdir = new_bios_dir(ctx, cb_config->multi_level, cookie);
1339
1340 if (cookie == BHD_COOKIE)
1341 ctx->biosdir = biosdir;
1342 else if (cookie == BHDL2_COOKIE) {
1343 if (ctx->biosdir2 == NULL)
1344 ctx->biosdir2 = biosdir;
1345 else if (ctx->biosdir2_b == NULL)
1346 ctx->biosdir2_b = biosdir;
1347 }
1348
1349 /* This function can create a primary table, a secondary table, or a
1350 * flattened table which contains all applicable types. These if-else
1351 * statements infer what the caller intended. If a 2nd-level cookie
1352 * is passed, clearly a 2nd-level table is intended. However, a
1353 * 1st-level cookie may indicate level 1 or flattened.
1354 */
1355 if (!cb_config->multi_level)
1356 level = BDT_BOTH;
1357 else if (cookie == BHDL2_COOKIE)
1358 level = BDT_LVL2;
1359 else if (cookie == BHD_COOKIE)
1360 level = BDT_LVL1;
1361 else
1362 level = BDT_BOTH;
1363
1364 current_table_save = ctx->current_table;
1365 ctx->current_table = BUFF_TO_RUN_MODE(*ctx, biosdir, AMD_ADDR_REL_BIOS);
1366 adjust_current_pointer(ctx, 0, TABLE_ALIGNMENT);
1367
1368 for (i = 0, count = 0; fw_table[i].type != AMD_BIOS_INVALID; i++) {
1369 if (!(fw_table[i].level & level))
1370 continue;
1371 if (fw_table[i].filename == NULL && (
1372 fw_table[i].type != AMD_BIOS_SIG &&
1373 fw_table[i].type != AMD_BIOS_APOB &&
1374 fw_table[i].type != AMD_BIOS_APOB_NV &&
1375 fw_table[i].type != AMD_BIOS_L2_PTR &&
1376 fw_table[i].type != AMD_BIOS_BIN &&
1377 fw_table[i].type != AMD_BIOS_PSP_SHARED_MEM))
1378 continue;
1379
1380 /* BIOS Directory items may have additional requirements */
1381
1382 /* SIG needs a size, else no choice but to skip */
1383 if (fw_table[i].type == AMD_BIOS_SIG && !fw_table[i].size)
1384 continue;
1385
1386 /* Check APOB_NV requirements */
1387 if (fw_table[i].type == AMD_BIOS_APOB_NV) {
1388 if (!fw_table[i].size && !fw_table[i].src)
1389 continue; /* APOB_NV not used */
1390 if (fw_table[i].src && !fw_table[i].size) {
1391 fprintf(stderr, "Error: APOB NV address provided, but no size\n");
1392 amdfwtool_cleanup(ctx);
1393 exit(1);
1394 }
1395 /* If the APOB isn't used, APOB_NV isn't used either */
1396 apob_idx = find_bios_entry(AMD_BIOS_APOB);
1397 if (apob_idx < 0 || !fw_table[apob_idx].dest)
1398 continue; /* APOV NV not supported */
1399 }
1400
1401 /* APOB_DATA needs destination */
1402 if (fw_table[i].type == AMD_BIOS_APOB && !fw_table[i].dest) {
1403 fprintf(stderr, "Error: APOB destination not provided\n");
1404 amdfwtool_cleanup(ctx);
1405 exit(1);
1406 }
1407
1408 /* BIOS binary must have destination and uncompressed size. If
1409 * no filename given, then user must provide a source address.
1410 */
1411 if (fw_table[i].type == AMD_BIOS_BIN) {
1412 if (!fw_table[i].dest || !fw_table[i].size) {
1413 fprintf(stderr, "Error: BIOS binary destination and uncompressed size are required\n");
1414 amdfwtool_cleanup(ctx);
1415 exit(1);
1416 }
1417 if (!fw_table[i].filename && !fw_table[i].src) {
1418 fprintf(stderr, "Error: BIOS binary assumed outside amdfw.rom but no source address given\n");
1419 amdfwtool_cleanup(ctx);
1420 exit(1);
1421 }
1422 }
1423
1424 /* PSP_SHARED_MEM needs a destination and size */
1425 if (fw_table[i].type == AMD_BIOS_PSP_SHARED_MEM &&
1426 (!fw_table[i].dest || !fw_table[i].size))
1427 continue;
1428 assert_fw_entry(count, MAX_BIOS_ENTRIES, ctx);
1429
1430 biosdir->entries[count].type = fw_table[i].type;
1431 biosdir->entries[count].region_type = fw_table[i].region_type;
1432 biosdir->entries[count].dest = fw_table[i].dest ?
1433 fw_table[i].dest : (uint64_t)-1;
1434 biosdir->entries[count].reset = fw_table[i].reset;
1435 biosdir->entries[count].copy = fw_table[i].copy;
1436 biosdir->entries[count].ro = fw_table[i].ro;
1437 biosdir->entries[count].compressed = fw_table[i].zlib;
1438 biosdir->entries[count].inst = fw_table[i].inst;
1439 biosdir->entries[count].subprog = fw_table[i].subpr;
1440
1441 switch (fw_table[i].type) {
1442 case AMD_BIOS_SIG:
1443 /* Reserve size bytes within amdfw.rom */
1444 biosdir->entries[count].size = fw_table[i].size;
1445 biosdir->entries[count].source = RUN_CURRENT(*ctx);
1446 biosdir->entries[count].address_mode =
1447 SET_ADDR_MODE_BY_TABLE(biosdir);
1448 memset(BUFF_CURRENT(*ctx), 0xff,
1449 biosdir->entries[count].size);
1450 adjust_current_pointer(ctx, biosdir->entries[count].size, 0x100U);
1451 break;
1452 case AMD_BIOS_APOB:
1453 biosdir->entries[count].size = fw_table[i].size;
1454 biosdir->entries[count].source = fw_table[i].src;
1455 biosdir->entries[count].address_mode = SET_ADDR_MODE_BY_TABLE(biosdir);
1456 break;
1457 case AMD_BIOS_APOB_NV:
1458 if (has_apob_nv_quirk(cb_config->soc_id)) {
1459 /*
1460 * once ROM3 mapping (>16MiB) is used on any SOC that
1461 * has the apob quirk, this needs to be updated, since
1462 * using an MMIO address is then not as simply as adding
1463 * the SPI_ROM_BASE offset anymore.
1464 */
1465 if (fw_table[i].src + fw_table[i].size >= 16*MiB) {
1466 fprintf(stderr,
1467 "APOB_NV location too high (0x%lx + 0x%lx)\n",
1468 fw_table[i].src, fw_table[i].size);
1469 amdfwtool_cleanup(ctx);
1470 exit(1);
1471 }
1472 biosdir->entries[count].source = fw_table[i].src + SPI_ROM_BASE; // convert to MMIO address
1473 biosdir->entries[count].address_mode = AMD_ADDR_PHYSICAL;
1474 } else {
1475 biosdir->entries[count].source = fw_table[i].src;
1476 biosdir->entries[count].address_mode =
1477 SET_ADDR_MODE(biosdir, AMD_ADDR_REL_BIOS);
1478 }
1479 biosdir->entries[count].size = fw_table[i].size;
1480 break;
1481 case AMD_BIOS_BIN:
1482 /* Don't make a 2nd copy, point to the same one */
1483 if ((level == BDT_LVL1 && locate_bdt_bios(ctx->biosdir2, &source, &size)) ||
1484 (level == BDT_LVL2 && locate_bdt_bios(ctx->biosdir, &source, &size))) {
1485 biosdir->entries[count].source = source;
1486 biosdir->entries[count].address_mode =
1487 SET_ADDR_MODE(biosdir, AMD_ADDR_REL_BIOS);
1488 biosdir->entries[count].size = size;
1489 break;
1490 }
1491
1492 /* Level 2 and no copy found in level 1, or level 1 and no copy found in level 2 */
1493 biosdir->entries[count].source = fw_table[i].src;
1494 biosdir->entries[count].address_mode =
1495 SET_ADDR_MODE(biosdir, AMD_ADDR_REL_BIOS);
1496 biosdir->entries[count].dest = fw_table[i].dest;
1497 biosdir->entries[count].size = fw_table[i].size;
1498
1499 if (!fw_table[i].filename)
1500 break;
1501
1502 bytes = copy_blob(BUFF_CURRENT(*ctx),
1503 fw_table[i].filename, BUFF_ROOM(*ctx));
1504 if (bytes <= 0) {
1505 amdfwtool_cleanup(ctx);
1506 exit(1);
1507 }
1508
1509 biosdir->entries[count].source =
1510 RUN_CURRENT(*ctx);
1511 biosdir->entries[count].address_mode =
1512 SET_ADDR_MODE_BY_TABLE(biosdir);
1513
1514 adjust_current_pointer(ctx, bytes, 0x100U);
1515 break;
1516 case AMD_BIOS_PSP_SHARED_MEM:
1517 biosdir->entries[count].dest = fw_table[i].dest;
1518 biosdir->entries[count].size = fw_table[i].size;
1519 break;
1520
1521 default: /* everything else is copied from input */
1522 if (fw_table[i].type == AMD_BIOS_APCB ||
1523 fw_table[i].type == AMD_BIOS_APCB_BK)
1524 adjust_current_pointer(ctx, 0, ERASE_ALIGNMENT);
1525 bytes = copy_blob(BUFF_CURRENT(*ctx),
1526 fw_table[i].filename, BUFF_ROOM(*ctx));
1527 if (bytes <= 0) {
1528 amdfwtool_cleanup(ctx);
1529 exit(1);
1530 }
1531
1532 biosdir->entries[count].size = (uint32_t)bytes;
1533 biosdir->entries[count].source = RUN_CURRENT(*ctx);
1534 biosdir->entries[count].address_mode = SET_ADDR_MODE_BY_TABLE(biosdir);
1535
1536 adjust_current_pointer(ctx, bytes, 0x100U);
1537 if (fw_table[i].type == AMD_BIOS_APCB && !cb_config->have_apcb_bk) {
1538 size = biosdir->entries[count].size;
1539 source = biosdir->entries[count].source;
1540 count++;
1541 add_bios_apcb_bk_entry(biosdir, count, fw_table[i].inst, size, source);
1542 }
1543 break;
1544 }
1545
1546 count++;
1547 }
1548
1549 fill_dir_header(biosdir, count, ctx);
1550 ctx->current_table = current_table_save;
1551 }
1552
1553 static void add_combo_entry(void *combo_dir, void *dir, uint32_t combo_index,
1554 context *ctx, amd_cb_config *cb_config)
1555 {
1556 psp_combo_directory *cdir = combo_dir;
1557 assert_fw_entry(combo_index, MAX_COMBO_ENTRIES, ctx);
1558 /* 0 -Compare PSP ID, 1 -Compare chip family ID */
1559 cdir->entries[combo_index].id_sel = 0;
1560 cdir->entries[combo_index].id = get_psp_id(cb_config->soc_id);
1561 cdir->entries[combo_index].lvl2_addr =
1562 BUFF_TO_RUN_MODE(*ctx, dir, AMD_ADDR_REL_BIOS);
1563
1564 fill_dir_header(combo_dir, combo_index + 1, ctx);
1565 }
1566
1567 static int set_efs_table(uint8_t soc_id, amd_cb_config *cb_config,
1568 embedded_firmware *amd_romsig)
1569 {
1570 if ((cb_config->efs_spi_readmode == 0xFF) || (cb_config->efs_spi_speed == 0xFF)) {
1571 fprintf(stderr, "Error: EFS read mode and SPI speed must be set\n");
1572 return 1;
1573 }
1574
1575 /* amd_romsig->efs_gen introduced after RAVEN/PICASSO.
1576 * Leave as 0xffffffff for first gen */
1577 if (cb_config->second_gen) {
1578 amd_romsig->efs_gen.gen = EFS_SECOND_GEN;
1579 amd_romsig->efs_gen.reserved = 0;
1580 } else {
1581 amd_romsig->efs_gen.gen = EFS_BEFORE_SECOND_GEN;
1582 amd_romsig->efs_gen.reserved = ~0;
1583 }
1584
1585 switch (soc_id) {
1586 case PLATFORM_CARRIZO:
1587 case PLATFORM_STONEYRIDGE:
1588 amd_romsig->spi_readmode_f15_mod_60_6f = cb_config->efs_spi_readmode;
1589 amd_romsig->fast_speed_new_f15_mod_60_6f = cb_config->efs_spi_speed;
1590 break;
1591 case PLATFORM_RAVEN:
1592 case PLATFORM_PICASSO:
1593 amd_romsig->spi_readmode_f17_mod_00_2f = cb_config->efs_spi_readmode;
1594 amd_romsig->spi_fastspeed_f17_mod_00_2f = cb_config->efs_spi_speed;
1595 switch (cb_config->efs_spi_micron_flag) {
1596 case 0:
1597 amd_romsig->qpr_dummy_cycle_f17_mod_00_2f = 0xff;
1598 break;
1599 case 1:
1600 amd_romsig->qpr_dummy_cycle_f17_mod_00_2f = 0xa;
1601 break;
1602 default:
1603 fprintf(stderr, "Error: EFS Micron flag must be correctly set.\n\n");
1604 return 1;
1605 }
1606 break;
1607 case PLATFORM_RENOIR:
1608 case PLATFORM_LUCIENNE:
1609 case PLATFORM_CEZANNE:
1610 case PLATFORM_MENDOCINO:
1611 case PLATFORM_PHOENIX:
1612 case PLATFORM_GLINDA:
1613 case PLATFORM_GENOA:
1614 case PLATFORM_FAEGAN:
1615 amd_romsig->spi_readmode_f17_mod_30_3f = cb_config->efs_spi_readmode;
1616 amd_romsig->spi_fastspeed_f17_mod_30_3f = cb_config->efs_spi_speed;
1617 switch (cb_config->efs_spi_micron_flag) {
1618 case 0:
1619 amd_romsig->micron_detect_f17_mod_30_3f = 0xff;
1620 break;
1621 case 1:
1622 amd_romsig->micron_detect_f17_mod_30_3f = 0xaa;
1623 break;
1624 case 2:
1625 amd_romsig->micron_detect_f17_mod_30_3f = 0x55;
1626 break;
1627 default:
1628 fprintf(stderr, "Error: EFS Micron flag must be correctly set.\n\n");
1629 return 1;
1630 }
1631 break;
1632 case PLATFORM_UNKNOWN:
1633 default:
1634 fprintf(stderr, "Error: Invalid SOC name.\n\n");
1635 return 1;
1636 }
1637 return 0;
1638 }
1639
1640 void open_process_config(char *config, amd_cb_config *cb_config)
1641 {
1642 FILE *config_handle;
1643
1644 if (config) {
1645 config_handle = fopen(config, "r");
1646 if (config_handle == NULL) {
1647 fprintf(stderr, "Can not open file %s for reading: %s\n",
1648 config, strerror(errno));
1649 exit(1);
1650 }
1651 if (process_config(config_handle, cb_config) == 0) {
1652 fprintf(stderr, "Configuration file %s parsing error\n",
1653 config);
1654 fclose(config_handle);
1655 exit(1);
1656 }
1657 fclose(config_handle);
1658 }
1659
1660 /* For debug. */
1661 if (cb_config->debug) {
1662 dump_psp_firmwares(amd_psp_fw_table);
1663 dump_bdt_firmwares(amd_bios_table);
1664 }
1665 }
1666
1667 static bool is_initial_alignment_required(enum platform soc_id)
1668 {
1669 switch (soc_id) {
1670 case PLATFORM_MENDOCINO:
1671 case PLATFORM_PHOENIX:
1672 case PLATFORM_GLINDA:
1673 case PLATFORM_FAEGAN:
1674 return false;
1675 default:
1676 return true;
1677 }
1678 }
1679
1680 static bool needs_new_combo_layout(enum platform soc_id)
1681 {
1682 return needs_ish(soc_id);
1683 }
1684
1685 int main(int argc, char **argv)
1686 {
1687 int retval = 0;
1688 int targetfd;
1689 context ctx = { 0 };
1690 uint32_t romsig_offset;
1691 amd_cb_config cb_config = {
1692 .efs_spi_readmode = 0xff, .efs_spi_speed = 0xff, .efs_spi_micron_flag = 0xff
1693 };
1694
1695 ctx.current_pointer_saved = 0xFFFFFFFF;
1696
1697 retval = amdfwtool_getopt(argc, argv, &cb_config, &ctx);
1698
1699 if (retval) {
1700 return retval;
1701 }
1702
1703 if (cb_config.use_combo) {
1704 ctx.amd_psp_fw_table_clean = malloc(sizeof(amd_psp_fw_table));
1705 ctx.amd_bios_table_clean = malloc(sizeof(amd_bios_table));
1706 memcpy(ctx.amd_psp_fw_table_clean, amd_psp_fw_table, sizeof(amd_psp_fw_table));
1707 memcpy(ctx.amd_bios_table_clean, amd_bios_table, sizeof(amd_bios_table));
1708 }
1709
1710 open_process_config(cb_config.config, &cb_config);
1711
1712 if (cb_config.use_combo && needs_new_combo_layout(cb_config.soc_id))
1713 cb_config.combo_new_rab = true;
1714
1715 ctx.rom = malloc(ctx.rom_size);
1716 if (!ctx.rom) {
1717 fprintf(stderr, "Error: Failed to allocate memory\n");
1718 return 1;
1719 }
1720 memset(ctx.rom, 0xFF, ctx.rom_size);
1721
1722 romsig_offset = cb_config.efs_location ? cb_config.efs_location : AMD_ROMSIG_OFFSET;
1723 set_current_pointer(&ctx, romsig_offset);
1724
1725 ctx.amd_romsig_ptr = BUFF_OFFSET(ctx, romsig_offset);
1726 ctx.amd_romsig_ptr->signature = EMBEDDED_FW_SIGNATURE;
1727 ctx.amd_romsig_ptr->imc_entry = 0;
1728 ctx.amd_romsig_ptr->gec_entry = 0;
1729 ctx.amd_romsig_ptr->xhci_entry = 0;
1730
1731 if (cb_config.soc_id != PLATFORM_UNKNOWN) {
1732 retval = set_efs_table(cb_config.soc_id, &cb_config, ctx.amd_romsig_ptr);
1733 if (retval) {
1734 fprintf(stderr, "ERROR: Failed to initialize EFS table!\n");
1735 return retval;
1736 }
1737 } else {
1738 fprintf(stderr, "WARNING: No SOC name specified.\n");
1739 }
1740
1741 if (cb_config.need_ish)
1742 ctx.address_mode = AMD_ADDR_REL_TAB;
1743 else if (cb_config.second_gen)
1744 ctx.address_mode = AMD_ADDR_REL_BIOS;
1745 else
1746 ctx.address_mode = AMD_ADDR_PHYSICAL;
1747
1748 if (cb_config.efs_location != cb_config.body_location)
1749 set_current_pointer(&ctx, cb_config.body_location);
1750 else
1751 set_current_pointer(&ctx, romsig_offset + sizeof(embedded_firmware));
1752
1753 integrate_firmwares(&ctx, ctx.amd_romsig_ptr, amd_fw_table);
1754
1755 if (is_initial_alignment_required(cb_config.soc_id)) {
1756 /* TODO: Check for older platforms. */
1757 adjust_current_pointer(&ctx, 0, 0x10000U);
1758 }
1759 ctx.current_table = 0;
1760
1761 /* If the tool is invoked with command-line options to keep the signed PSP
1762 binaries separate, process the signed binaries first. */
1763 if (cb_config.signed_output_file && cb_config.signed_start_addr)
1764 process_signed_psp_firmwares(cb_config.signed_output_file,
1765 amd_psp_fw_table,
1766 cb_config.signed_start_addr,
1767 cb_config.soc_id);
1768
1769 if (cb_config.use_combo && !cb_config.combo_new_rab) {
1770 ctx.psp_combo_dir = new_combo_dir(&ctx, PSP2_COOKIE);
1771
1772 adjust_current_pointer(&ctx, 0, 0x1000U);
1773
1774 if (!cb_config.recovery_ab)
1775 ctx.bhd_combo_dir = new_combo_dir(&ctx, BHD2_COOKIE);
1776 }
1777
1778 ctx.combo_index = 0;
1779 if (cb_config.config)
1780 cb_config.combo_config[0] = cb_config.config;
1781
1782 do {
1783 if (cb_config.use_combo && cb_config.debug)
1784 printf("Processing %dth combo entry\n", ctx.combo_index);
1785
1786 /* The pspdir level 1 is special. For new combo layout, all the combo entries
1787 share one pspdir L1. It should not be cleared at each iteration. */
1788 if (!cb_config.combo_new_rab || ctx.combo_index == 0) {
1789 ctx.pspdir = NULL;
1790 ctx.pspdir_bak = NULL;
1791 }
1792 ctx.pspdir2 = NULL;
1793 ctx.pspdir2_b = NULL;
1794 ctx.biosdir = NULL;
1795 ctx.biosdir2 = NULL;
1796 ctx.biosdir2_b = NULL;
1797 ctx.ish_a_dir = NULL;
1798 ctx.ish_b_dir = NULL;
1799 /* for non-combo image, combo_config[0] == config, and
1800 * it already is processed. Actually "combo_index >
1801 * 0" is enough. Put both of them here to make sure
1802 * and make it clear this will not affect non-combo
1803 * case.
1804 */
1805 if (cb_config.use_combo && ctx.combo_index > 0) {
1806 /* Restore the table as clean data. */
1807 memcpy(amd_psp_fw_table, ctx.amd_psp_fw_table_clean,
1808 sizeof(amd_psp_fw_table));
1809 memcpy(amd_bios_table, ctx.amd_bios_table_clean,
1810 sizeof(amd_bios_table));
1811 assert_fw_entry(ctx.combo_index, MAX_COMBO_ENTRIES, &ctx);
1812 open_process_config(cb_config.combo_config[ctx.combo_index], &cb_config);
1813
1814 /* In most cases, the address modes are same. */
1815 if (cb_config.need_ish)
1816 ctx.address_mode = AMD_ADDR_REL_TAB;
1817 else if (cb_config.second_gen)
1818 ctx.address_mode = AMD_ADDR_REL_BIOS;
1819 else
1820 ctx.address_mode = AMD_ADDR_PHYSICAL;
1821
1822 register_apcb_combo(&cb_config, ctx.combo_index, &ctx);
1823 }
1824
1825 if (cb_config.multi_level) {
1826 /* PSP L1 */
1827 if (!cb_config.combo_new_rab || ctx.combo_index == 0)
1828 integrate_psp_firmwares(&ctx,
1829 amd_psp_fw_table, PSP_COOKIE, &cb_config);
1830 /* PSP L2 & BIOS L2 (if AB recovery) */
1831 integrate_psp_firmwares(&ctx,
1832 amd_psp_fw_table, PSPL2_COOKIE, &cb_config);
1833 if (cb_config.recovery_ab) {
1834 integrate_bios_firmwares(&ctx,
1835 amd_bios_table, BHDL2_COOKIE, &cb_config);
1836 if (!cb_config.recovery_ab_single_copy) {
1837 integrate_psp_firmwares(&ctx,
1838 amd_psp_fw_table, PSPL2_COOKIE, &cb_config);
1839 integrate_bios_firmwares(&ctx,
1840 amd_bios_table, BHDL2_COOKIE, &cb_config);
1841 }
1842 integrate_bios_levels(&ctx, &cb_config);
1843 }
1844 integrate_psp_levels(&ctx, &cb_config);
1845 } else {
1846 /* flat: PSP 1 cookie and no pointer to 2nd table */
1847 integrate_psp_firmwares(&ctx,
1848 amd_psp_fw_table, PSP_COOKIE, &cb_config);
1849 }
1850
1851 if (!cb_config.use_combo || (cb_config.combo_new_rab && ctx.combo_index == 0)) {
1852 /* For new combo layout, there is only 1 PSP level 1 directory. */
1853 fill_psp_directory_to_efs(ctx.amd_romsig_ptr, ctx.pspdir, &ctx, &cb_config);
1854 fill_psp_bak_directory_to_efs(ctx.amd_romsig_ptr, ctx.pspdir_bak, &ctx, &cb_config);
1855 } else if (cb_config.use_combo && !cb_config.combo_new_rab) {
1856 fill_psp_directory_to_efs(ctx.amd_romsig_ptr, ctx.psp_combo_dir, &ctx, &cb_config);
1857 add_combo_entry(ctx.psp_combo_dir, ctx.pspdir, ctx.combo_index, &ctx, &cb_config);
1858 }
1859
1860 if (have_bios_tables(amd_bios_table) && !cb_config.recovery_ab) {
1861 if (cb_config.multi_level) {
1862 integrate_bios_firmwares(&ctx,
1863 amd_bios_table, BHD_COOKIE, &cb_config);
1864 integrate_bios_firmwares(&ctx,
1865 amd_bios_table, BHDL2_COOKIE, &cb_config);
1866 integrate_bios_levels(&ctx, &cb_config);
1867 } else {
1868 /* flat: BHD1 cookie and no pointer to 2nd table */
1869 integrate_bios_firmwares(&ctx,
1870 amd_bios_table, BHD_COOKIE, &cb_config);
1871 }
1872 if (!cb_config.use_combo) {
1873 fill_bios_directory_to_efs(ctx.amd_romsig_ptr, ctx.biosdir,
1874 &ctx, &cb_config);
1875 } else if (ctx.bhd_combo_dir != NULL) {
1876 /* In recovery A/B mode, there isn't a BHD combo directory.
1877 * Instead, the BIOS tables level 2 are linked by PSP tables.
1878 */
1879 fill_bios_directory_to_efs(ctx.amd_romsig_ptr, ctx.bhd_combo_dir,
1880 &ctx, &cb_config);
1881 add_combo_entry(ctx.bhd_combo_dir, ctx.biosdir, ctx.combo_index, &ctx, &cb_config);
1882 }
1883 }
1884 if (cb_config.debug)
1885 dump_image_addresses(&ctx);
1886 } while (cb_config.use_combo && ++ctx.combo_index < MAX_COMBO_ENTRIES &&
1887 cb_config.combo_config[ctx.combo_index] != NULL);
1888
1889 targetfd = open(cb_config.output, O_RDWR | O_CREAT | O_TRUNC, 0666);
1890 if (targetfd >= 0) {
1891 uint32_t offset = cb_config.efs_location;
1892 uint32_t bytes = cb_config.efs_location == cb_config.body_location ?
1893 ctx.current - offset : sizeof(embedded_firmware);
1894 uint32_t ret_bytes;
1895
1896 ret_bytes = write_from_buf_to_file(targetfd, BUFF_OFFSET(ctx, offset), bytes);
1897 if (bytes != ret_bytes) {
1898 fprintf(stderr, "Error: Writing to file %s failed\n", cb_config.output);
1899 retval = 1;
1900 }
1901 close(targetfd);
1902 } else {
1903 fprintf(stderr, "Error: could not open file: %s\n", cb_config.output);
1904 retval = 1;
1905 }
1906
1907 if (cb_config.efs_location != cb_config.body_location) {
1908 ssize_t bytes;
1909
1910 bytes = write_body(cb_config.output, BUFF_OFFSET(ctx, cb_config.body_location),
1911 ctx.current - cb_config.body_location);
1912 if (bytes != ctx.current - cb_config.body_location) {
1913 fprintf(stderr, "Error: Writing body\n");
1914 retval = 1;
1915 }
1916 }
1917
1918 if (cb_config.manifest_file) {
1919 dump_blob_version(cb_config.manifest_file, amd_psp_fw_table);
1920 }
1921
1922 amdfwtool_cleanup(&ctx);
1923 return retval;
1924 }