blob: feac409e4dd20c9c7c729bf3adc59285f7193cab [file] [log] [blame]
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08001/*
2 * This file is part of the coreboot project.
3 *
Marshall Dawsone7d892c2016-10-08 14:49:41 -06004 * Copyright (C) 2015 - 2016 Advanced Micro Devices, Inc.
Zheng Bao9c7ff7b2015-11-17 22:57:39 +08005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
16/*
17 * ROMSIG At ROMBASE + 0x20000:
zbaoc3b0b722016-02-19 13:47:31 +080018 * 0 4 8 C
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080019 * +------------+---------------+----------------+------------+
20 * | 0x55AA55AA |EC ROM Address |GEC ROM Address |USB3 ROM |
21 * +------------+---------------+----------------+------------+
zbaoc3b0b722016-02-19 13:47:31 +080022 * | PSPDIR ADDR|PSPDIR ADDR |<-- Field 0x14 could be either
23 * +------------+---------------+ 2nd PSP directory or PSP COMBO directory
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080024 * EC ROM should be 64K aligned.
25 *
Zheng Bao4fcc9f22015-11-20 12:29:04 +080026 * PSP directory (Where "PSPDIR ADDR" points)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080027 * +------------+---------------+----------------+------------+
28 * | 'PSP$' | Fletcher | Count | Reserved |
29 * +------------+---------------+----------------+------------+
30 * | 0 | size | Base address | Reserved | Pubkey
31 * +------------+---------------+----------------+------------+
32 * | 1 | size | Base address | Reserved | Bootloader
33 * +------------+---------------+----------------+------------+
34 * | 8 | size | Base address | Reserved | Smu Firmware
35 * +------------+---------------+----------------+------------+
36 * | 3 | size | Base address | Reserved | Recovery Firmware
37 * +------------+---------------+----------------+------------+
38 * | |
39 * | |
40 * | Other PSP Firmware |
41 * | |
42 * | |
43 * +------------+---------------+----------------+------------+
Zheng Bao4fcc9f22015-11-20 12:29:04 +080044 *
zbaoc3b0b722016-02-19 13:47:31 +080045 * PSP Combo directory
Zheng Bao4fcc9f22015-11-20 12:29:04 +080046 * +------------+---------------+----------------+------------+
zbao6e2f3d12016-02-19 13:34:59 +080047 * | 'PSP2' | Fletcher | Count |Look up mode|
Zheng Bao4fcc9f22015-11-20 12:29:04 +080048 * +------------+---------------+----------------+------------+
zbaoc3a08a92016-03-02 14:47:27 +080049 * | R e s e r v e d |
50 * +------------+---------------+----------------+------------+
zbao6e2f3d12016-02-19 13:34:59 +080051 * | ID-Sel | PSP ID | PSPDIR ADDR | | 2nd PSP directory
Zheng Bao4fcc9f22015-11-20 12:29:04 +080052 * +------------+---------------+----------------+------------+
zbao6e2f3d12016-02-19 13:34:59 +080053 * | ID-Sel | PSP ID | PSPDIR ADDR | | 3rd PSP directory
Zheng Bao4fcc9f22015-11-20 12:29:04 +080054 * +------------+---------------+----------------+------------+
55 * | |
56 * | Other PSP |
57 * | |
58 * +------------+---------------+----------------+------------+
59 *
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080060 */
61
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080062#include <fcntl.h>
63#include <errno.h>
64#include <stdio.h>
65#include <sys/stat.h>
66#include <sys/types.h>
67#include <unistd.h>
68#include <string.h>
69#include <stdlib.h>
70#include <getopt.h>
71
72#ifndef CONFIG_ROM_SIZE
73#define CONFIG_ROM_SIZE 0x400000
74#endif
75
Martin Roth60f15512016-11-08 09:55:01 -070076#define AMD_ROMSIG_OFFSET 0x20000
77#define MIN_ROM_KB 256
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080078
Martin Rothcd15bc82016-11-08 11:34:02 -070079#define ALIGN(val, by) (((val) + (by) - 1) & ~((by) - 1))
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080080
81/*
82 Reserved for future.
83 TODO: PSP2 is for Combo BIOS, which is the idea that one image supports 2
84 kinds of APU.
85*/
86#define PSP2 1
zbaoc3b0b722016-02-19 13:47:31 +080087#if PSP2
88/* Use PSP combo directory or not.
89 * Currently we dont have to squeeze 3 PSP directories into 1 image. So
90 * we skip the combo directory.
91 */
92 #define PSP_COMBO 0
93#endif
Zheng Bao9c7ff7b2015-11-17 22:57:39 +080094
95typedef unsigned int uint32_t;
96typedef unsigned char uint8_t;
97typedef unsigned short uint16_t;
98
99/*
100 * Creates the OSI Fletcher checksum. See 8473-1, Appendix C, section C.3.
101 * The checksum field of the passed PDU does not need to be reset to zero.
102 *
103 * The "Fletcher Checksum" was proposed in a paper by John G. Fletcher of
104 * Lawrence Livermore Labs. The Fletcher Checksum was proposed as an
105 * alternative to cyclical redundancy checks because it provides error-
106 * detection properties similar to cyclical redundancy checks but at the
107 * cost of a simple summation technique. Its characteristics were first
108 * published in IEEE Transactions on Communications in January 1982. One
109 * version has been adopted by ISO for use in the class-4 transport layer
110 * of the network protocol.
111 *
112 * This program expects:
113 * stdin: The input file to compute a checksum for. The input file
114 * not be longer than 256 bytes.
115 * stdout: Copied from the input file with the Fletcher's Checksum
116 * inserted 8 bytes after the beginning of the file.
117 * stderr: Used to print out error messages.
118 */
Martin Roth8806f7f2016-11-08 10:44:18 -0700119static uint32_t fletcher32(const uint16_t *pptr, int length)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800120{
121 uint32_t c0;
122 uint32_t c1;
123 uint32_t checksum;
124 int index;
125
126 c0 = 0xFFFF;
127 c1 = 0xFFFF;
128
129 for (index = 0; index < length; index++) {
130 /*
131 * Ignore the contents of the checksum field.
132 */
133 c0 += *(pptr++);
134 c1 += c0;
135 if ((index % 360) == 0) {
136 c0 = (c0 & 0xFFFF) + (c0 >> 16); // Sum0 modulo 65535 + the overflow
137 c1 = (c1 & 0xFFFF) + (c1 >> 16); // Sum1 modulo 65535 + the overflow
138 }
139 }
140
141 c0 = (c0 & 0xFFFF) + (c0 >> 16); // Sum0 modulo 65535 + the overflow
142 c1 = (c1 & 0xFFFF) + (c1 >> 16); // Sum1 modulo 65535 + the overflow
143 checksum = (c1 << 16) | c0;
144
145 return checksum;
146}
147
Martin Roth8806f7f2016-11-08 10:44:18 -0700148static void usage(void)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800149{
Martin Roth0e940622016-11-08 10:37:53 -0700150 printf("amdfwtool: Create AMD Firmware combination\n");
151 printf("Usage: amdfwtool [options] -f <size> -o <filename>\n");
152 printf("-x | --xhci <FILE> Add XHCI blob\n");
153 printf("-i | --imc <FILE> Add IMC blob\n");
154 printf("-g | --gec <FILE> Add GEC blob\n");
155
156 printf("\nPSP options:\n");
157 printf("-p | --pubkey <FILE> Add pubkey\n");
158 printf("-b | --bootloader <FILE> Add bootloader\n");
159 printf("-s | --smufirmware <FILE> Add smufirmware\n");
160 printf("-r | --recovery <FILE> Add recovery\n");
161 printf("-k | --rtmpubkey <FILE> Add rtmpubkey\n");
162 printf("-c | --secureos <FILE> Add secureos\n");
163 printf("-n | --nvram <FILE> Add nvram\n");
164 printf("-d | --securedebug <FILE> Add securedebug\n");
165 printf("-t | --trustlets <FILE> Add trustlets\n");
166 printf("-u | --trustletkey <FILE> Add trustletkey\n");
167 printf("-w | --smufirmware2 <FILE> Add smufirmware2\n");
168 printf("-m | --smuscs <FILE> Add smuscs\n");
169
170#if PSP2
171 printf("\nPSP2 options:\n");
172 printf("-P | --pubkey2 <FILE> Add pubkey\n");
173 printf("-B | --bootloader2 <FILE> Add bootloader\n");
174 printf("-S | --smufirmware_2 <FILE> Add smufirmware\n");
175 printf("-R | --recovery2 <FILE> Add recovery\n");
176 printf("-K | --rtmpubkey2 <FILE> Add rtmpubkey\n");
177 printf("-C | --secureos2 <FILE> Add secureos\n");
178 printf("-N | --nvram2 <FILE> Add nvram\n");
179 printf("-D | --securedebug2 <FILE> Add securedebug\n");
180 printf("-T | --trustlets2 <FILE> Add trustlets\n");
181 printf("-U | --trustletkey2 <FILE> Add trustletkey\n");
182 printf("-W | --smufirmware2_2 <FILE> Add smufirmware2\n");
183 printf("-M | --smuscs2 <FILE> Add smuscs\n");
184#endif
185
186 printf("\n-o | --output <filename> output filename\n");
187 printf("-f | --flashsize <HEX_VAL> ROM size in bytes\n");
188 printf(" size must be larger than %dKB\n",
189 MIN_ROM_KB);
190 printf(" and must a multiple of 1024\n");
191 printf("-h | --help show this help\n");
192
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800193}
194
195typedef enum _amd_fw_type {
196 AMD_FW_PSP_PUBKEY = 0,
197 AMD_FW_PSP_BOOTLOADER = 1,
198 AMD_FW_PSP_SMU_FIRMWARE = 8,
199 AMD_FW_PSP_RECOVERY = 3,
200 AMD_FW_PSP_RTM_PUBKEY = 5,
201 AMD_FW_PSP_SECURED_OS = 2,
202 AMD_FW_PSP_NVRAM = 4,
203 AMD_FW_PSP_SECURED_DEBUG = 9,
204 AMD_FW_PSP_TRUSTLETS = 12,
205 AMD_FW_PSP_TRUSTLETKEY = 13,
206 AMD_FW_PSP_SMU_FIRMWARE2 = 18,
207 AMD_PSP_FUSE_CHAIN = 11,
208 AMD_FW_PSP_SMUSCS = 95,
209
210 AMD_FW_IMC,
211 AMD_FW_GEC,
212 AMD_FW_XHCI,
zbaoc3a08a92016-03-02 14:47:27 +0800213 AMD_FW_INVALID,
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800214} amd_fw_type;
215
216typedef struct _amd_fw_entry {
217 amd_fw_type type;
218 char *filename;
219} amd_fw_entry;
220
Martin Roth8806f7f2016-11-08 10:44:18 -0700221static amd_fw_entry amd_psp_fw_table[] = {
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800222 { .type = AMD_FW_PSP_PUBKEY },
223 { .type = AMD_FW_PSP_BOOTLOADER },
224 { .type = AMD_FW_PSP_SMU_FIRMWARE },
225 { .type = AMD_FW_PSP_RECOVERY },
226 { .type = AMD_FW_PSP_RTM_PUBKEY },
227 { .type = AMD_FW_PSP_SECURED_OS },
228 { .type = AMD_FW_PSP_NVRAM },
229 { .type = AMD_FW_PSP_SECURED_DEBUG },
230 { .type = AMD_FW_PSP_TRUSTLETS },
231 { .type = AMD_FW_PSP_TRUSTLETKEY },
232 { .type = AMD_FW_PSP_SMU_FIRMWARE2 },
233 { .type = AMD_FW_PSP_SMUSCS },
234 { .type = AMD_PSP_FUSE_CHAIN },
zbaoc3a08a92016-03-02 14:47:27 +0800235 { .type = AMD_FW_INVALID },
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800236};
237
238#if PSP2
Martin Roth8806f7f2016-11-08 10:44:18 -0700239static amd_fw_entry amd_psp2_fw_table[] = {
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800240 { .type = AMD_FW_PSP_PUBKEY },
241 { .type = AMD_FW_PSP_BOOTLOADER },
242 { .type = AMD_FW_PSP_SMU_FIRMWARE },
243 { .type = AMD_FW_PSP_RECOVERY },
244 { .type = AMD_FW_PSP_RTM_PUBKEY },
245 { .type = AMD_FW_PSP_SECURED_OS },
246 { .type = AMD_FW_PSP_NVRAM },
247 { .type = AMD_FW_PSP_SECURED_DEBUG },
248 { .type = AMD_FW_PSP_TRUSTLETS },
249 { .type = AMD_FW_PSP_TRUSTLETKEY },
250 { .type = AMD_FW_PSP_SMU_FIRMWARE2 },
251 { .type = AMD_FW_PSP_SMUSCS },
252 { .type = AMD_PSP_FUSE_CHAIN },
zbaoc3a08a92016-03-02 14:47:27 +0800253 { .type = AMD_FW_INVALID },
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800254};
255#endif
256
Martin Roth8806f7f2016-11-08 10:44:18 -0700257static amd_fw_entry amd_fw_table[] = {
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800258 { .type = AMD_FW_XHCI },
259 { .type = AMD_FW_IMC },
260 { .type = AMD_FW_GEC },
zbaoc3a08a92016-03-02 14:47:27 +0800261 { .type = AMD_FW_INVALID },
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800262};
263
Martin Roth8806f7f2016-11-08 10:44:18 -0700264static void fill_psp_head(uint32_t *pspdir, uint32_t count)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800265{
Martin Rothcd15bc82016-11-08 11:34:02 -0700266 pspdir[0] = 0x50535024; /* 'PSP$' */
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800267 pspdir[2] = count; /* size */
268 pspdir[3] = 0;
Martin Roth8955d552016-11-08 11:46:31 -0700269 pspdir[1] = fletcher32((uint16_t *)&pspdir[1],
270 (count * 16 + 16) / 2 - 2);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800271}
272
Martin Roth8955d552016-11-08 11:46:31 -0700273static uint32_t integrate_firmwares(char *base, uint32_t pos, uint32_t *romsig,
274 amd_fw_entry *fw_table, uint32_t rom_size)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800275{
276 int fd;
277 struct stat fd_stat;
zbaoc3a08a92016-03-02 14:47:27 +0800278 int i;
Martin Roth60f15512016-11-08 09:55:01 -0700279 uint32_t rom_base_address = 0xFFFFFFFF - rom_size + 1;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800280
Martin Rothcd15bc82016-11-08 11:34:02 -0700281 for (i = 0; fw_table[i].type != AMD_FW_INVALID; i++) {
zbaoc3a08a92016-03-02 14:47:27 +0800282 if (fw_table[i].filename != NULL) {
Martin Rothcd15bc82016-11-08 11:34:02 -0700283 fd = open(fw_table[i].filename, O_RDONLY);
zbaoc3a08a92016-03-02 14:47:27 +0800284 fstat(fd, &fd_stat);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800285
zbaoc3a08a92016-03-02 14:47:27 +0800286 switch (fw_table[i].type) {
287 case AMD_FW_IMC:
Martin Roth8806f7f2016-11-08 10:44:18 -0700288 pos = ALIGN(pos, 0x10000U);
Martin Roth60f15512016-11-08 09:55:01 -0700289 romsig[1] = pos + rom_base_address;
zbaoc3a08a92016-03-02 14:47:27 +0800290 break;
291 case AMD_FW_GEC:
Martin Roth60f15512016-11-08 09:55:01 -0700292 romsig[2] = pos + rom_base_address;
zbaoc3a08a92016-03-02 14:47:27 +0800293 break;
294 case AMD_FW_XHCI:
Martin Roth60f15512016-11-08 09:55:01 -0700295 romsig[3] = pos + rom_base_address;
zbaoc3a08a92016-03-02 14:47:27 +0800296 break;
297 default:
298 /* Error */
299 break;
300 }
301
Martin Roth60f15512016-11-08 09:55:01 -0700302 if (pos + fd_stat.st_size > rom_size) {
303 printf("Error: Specified ROM size of %d"
304 " will not fit %s. Exiting.\n",
305 rom_size, fw_table[i].filename);
306 free(base);
307 exit(1);
308 }
309
Martin Roth8806f7f2016-11-08 10:44:18 -0700310 read(fd, (void *)(base + pos), (size_t)fd_stat.st_size);
zbaoc3a08a92016-03-02 14:47:27 +0800311
312 pos += fd_stat.st_size;
Martin Rothcd15bc82016-11-08 11:34:02 -0700313 close(fd);
Martin Roth8806f7f2016-11-08 10:44:18 -0700314 pos = ALIGN(pos, 0x100U);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800315 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800316 }
317
318 return pos;
319}
320
Martin Roth8955d552016-11-08 11:46:31 -0700321static uint32_t integrate_psp_firmwares(char *base, uint32_t pos,
322 uint32_t *pspdir,
323 amd_fw_entry *fw_table,
324 uint32_t rom_size)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800325{
326 int fd;
327 struct stat fd_stat;
Martin Roth8806f7f2016-11-08 10:44:18 -0700328 unsigned int i;
Martin Roth60f15512016-11-08 09:55:01 -0700329 uint32_t rom_base_address = 0xFFFFFFFF - rom_size + 1;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800330
Martin Rothcd15bc82016-11-08 11:34:02 -0700331 for (i = 0; fw_table[i].type != AMD_FW_INVALID; i++) {
zbaoc3a08a92016-03-02 14:47:27 +0800332 if (fw_table[i].type == AMD_PSP_FUSE_CHAIN) {
333 pspdir[4+4*i+0] = fw_table[i].type;
334 pspdir[4+4*i+1] = 0xFFFFFFFF;
335 pspdir[4+4*i+2] = 1;
336 pspdir[4+4*i+3] = 0;
337 } else if (fw_table[i].filename != NULL) {
338 pspdir[4+4*i+0] = fw_table[i].type;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800339
Martin Rothcd15bc82016-11-08 11:34:02 -0700340 fd = open(fw_table[i].filename, O_RDONLY);
zbaoc3a08a92016-03-02 14:47:27 +0800341 fstat(fd, &fd_stat);
Martin Roth8806f7f2016-11-08 10:44:18 -0700342 pspdir[4+4*i+1] = (uint32_t)fd_stat.st_size;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800343
Martin Roth60f15512016-11-08 09:55:01 -0700344 pspdir[4+4*i+2] = pos + rom_base_address;
zbaoc3a08a92016-03-02 14:47:27 +0800345 pspdir[4+4*i+3] = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800346
Martin Roth60f15512016-11-08 09:55:01 -0700347 if (pos + fd_stat.st_size > rom_size) {
348 printf("Error: Specified ROM size of %d"
349 " will not fit %s. Exiting.\n",
350 rom_size, fw_table[i].filename);
Martin Rothcd15bc82016-11-08 11:34:02 -0700351 free(base);
Martin Roth60f15512016-11-08 09:55:01 -0700352 exit(1);
353 }
354
Martin Roth8806f7f2016-11-08 10:44:18 -0700355 read(fd, (void *)(base + pos), (size_t)fd_stat.st_size);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800356
zbaoc3a08a92016-03-02 14:47:27 +0800357 pos += fd_stat.st_size;
Martin Rothcd15bc82016-11-08 11:34:02 -0700358 close(fd);
Martin Roth8806f7f2016-11-08 10:44:18 -0700359 pos = ALIGN(pos, 0x100U);
zbaoc3a08a92016-03-02 14:47:27 +0800360 } else {
361 /* This APU doesn't have this firmware. */
362 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800363 }
zbaoc3a08a92016-03-02 14:47:27 +0800364 fill_psp_head(pspdir, i);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800365 return pos;
366}
367
Marc Jones90099b62016-09-20 21:05:45 -0600368#if PSP2
Martin Roth8955d552016-11-08 11:46:31 -0700369static const char *optstring =
370 "x:i:g:p:b:s:r:k:c:n:d:t:u:w:m:P:B:S:R:K:C:N:D:T:U:W:M:o:f:h";
Marc Jones90099b62016-09-20 21:05:45 -0600371#else
Martin Roth60f15512016-11-08 09:55:01 -0700372static const char *optstring = "x:i:g:p:b:s:r:k:c:n:d:t:u:w:m:o:f:h";
Marc Jones90099b62016-09-20 21:05:45 -0600373#endif
374
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800375static struct option long_options[] = {
Martin Rothcd15bc82016-11-08 11:34:02 -0700376 {"xhci", required_argument, 0, 'x' },
377 {"imc", required_argument, 0, 'i' },
378 {"gec", required_argument, 0, 'g' },
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800379 /* PSP */
Martin Rothcd15bc82016-11-08 11:34:02 -0700380 {"pubkey", required_argument, 0, 'p' },
381 {"bootloader", required_argument, 0, 'b' },
382 {"smufirmware", required_argument, 0, 's' },
383 {"recovery", required_argument, 0, 'r' },
384 {"rtmpubkey", required_argument, 0, 'k' },
385 {"secureos", required_argument, 0, 'c' },
386 {"nvram", required_argument, 0, 'n' },
387 {"securedebug", required_argument, 0, 'd' },
388 {"trustlets", required_argument, 0, 't' },
389 {"trustletkey", required_argument, 0, 'u' },
390 {"smufirmware2", required_argument, 0, 'w' },
391 {"smuscs", required_argument, 0, 'm' },
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800392
393 /* TODO: PSP2 */
394#if PSP2
Martin Rothcd15bc82016-11-08 11:34:02 -0700395 {"pubkey2", required_argument, 0, 'P' },
396 {"bootloader2", required_argument, 0, 'B' },
397 {"smufirmware_2", required_argument, 0, 'S' },
398 {"recovery2", required_argument, 0, 'R' },
399 {"rtmpubkey2", required_argument, 0, 'K' },
400 {"secureos2", required_argument, 0, 'C' },
401 {"nvram2", required_argument, 0, 'N' },
402 {"securedebug2", required_argument, 0, 'D' },
403 {"trustlets2", required_argument, 0, 'T' },
404 {"trustletkey2", required_argument, 0, 'U' },
405 {"smufirmware2_2", required_argument, 0, 'W' },
406 {"smuscs2", required_argument, 0, 'M' },
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800407#endif
408
Martin Rothcd15bc82016-11-08 11:34:02 -0700409 {"output", required_argument, 0, 'o' },
410 {"flashsize", required_argument, 0, 'f' },
411 {"help", no_argument, 0, 'h' },
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800412
Martin Rothcd15bc82016-11-08 11:34:02 -0700413 {NULL, 0, 0, 0 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800414};
415
Martin Roth8806f7f2016-11-08 10:44:18 -0700416static void register_fw_filename(amd_fw_type type, char filename[], int pspflag)
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800417{
Martin Roth8806f7f2016-11-08 10:44:18 -0700418 unsigned int i;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800419
Martin Rothcd15bc82016-11-08 11:34:02 -0700420 for (i = 0; i < sizeof(amd_fw_table) / sizeof(amd_fw_entry); i++) {
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800421 if (amd_fw_table[i].type == type) {
422 amd_fw_table[i].filename = filename;
423 return;
424 }
425 }
426
427 if (pspflag == 1) {
Martin Roth8955d552016-11-08 11:46:31 -0700428 for (i = 0; i < sizeof(amd_psp_fw_table) /
429 sizeof(amd_fw_entry); i++) {
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800430 if (amd_psp_fw_table[i].type == type) {
431 amd_psp_fw_table[i].filename = filename;
432 return;
433 }
434 }
435 }
436
437#if PSP2
438 if (pspflag == 2) {
Martin Roth8955d552016-11-08 11:46:31 -0700439 for (i = 0; i < sizeof(amd_psp2_fw_table) /
440 sizeof(amd_fw_entry); i++) {
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800441 if (amd_psp2_fw_table[i].type == type) {
442 amd_psp2_fw_table[i].filename = filename;
443 return;
444 }
445 }
446 }
447#endif
448}
449
450int main(int argc, char **argv)
451{
zbaoc3a08a92016-03-02 14:47:27 +0800452 int c, pspflag = 0;
Martin Roth31d95a22016-11-08 11:22:12 -0700453 int retval = 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800454#if PSP2
455 int psp2flag = 0;
Zheng Bao4fcc9f22015-11-20 12:29:04 +0800456 uint32_t *psp2dir;
Martin Roth60f15512016-11-08 09:55:01 -0700457 char *tmp;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800458#endif
zbaoc3a08a92016-03-02 14:47:27 +0800459#if PSP_COMBO
460 int psp2count;
461#endif
462
Martin Roth8806f7f2016-11-08 10:44:18 -0700463 char *rom = NULL;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800464 uint32_t current;
465 uint32_t *amd_romsig, *pspdir;
466
467 int targetfd;
Martin Roth8806f7f2016-11-08 10:44:18 -0700468 char *output = NULL;
Martin Roth60f15512016-11-08 09:55:01 -0700469 uint32_t rom_size = CONFIG_ROM_SIZE;
470 uint32_t rom_base_address;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800471
472 while (1) {
473 int optindex = 0;
474
475 c = getopt_long(argc, argv, optstring, long_options, &optindex);
476
477 if (c == -1)
478 break;
479
480 switch (c) {
481 case 'x':
482 register_fw_filename(AMD_FW_XHCI, optarg, 0);
483 break;
484 case 'i':
485 register_fw_filename(AMD_FW_IMC, optarg, 0);
486 break;
487 case 'g':
488 register_fw_filename(AMD_FW_GEC, optarg, 0);
489 break;
490 case 'p':
491 register_fw_filename(AMD_FW_PSP_PUBKEY, optarg, 1);
492 pspflag = 1;
493 break;
494 case 'b':
495 register_fw_filename(AMD_FW_PSP_BOOTLOADER, optarg, 1);
496 pspflag = 1;
497 break;
498 case 's':
Martin Roth8955d552016-11-08 11:46:31 -0700499 register_fw_filename(AMD_FW_PSP_SMU_FIRMWARE,
500 optarg, 1);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800501 pspflag = 1;
502 break;
503 case 'r':
504 register_fw_filename(AMD_FW_PSP_RECOVERY, optarg, 1);
505 pspflag = 1;
506 break;
507 case 'k':
508 register_fw_filename(AMD_FW_PSP_RTM_PUBKEY, optarg, 1);
509 pspflag = 1;
510 break;
511 case 'c':
512 register_fw_filename(AMD_FW_PSP_SECURED_OS, optarg, 1);
513 pspflag = 1;
514 break;
515 case 'n':
516 register_fw_filename(AMD_FW_PSP_NVRAM, optarg, 1);
517 pspflag = 1;
518 break;
519 case 'd':
Martin Roth8955d552016-11-08 11:46:31 -0700520 register_fw_filename(AMD_FW_PSP_SECURED_DEBUG,
521 optarg, 1);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800522 pspflag = 1;
523 break;
524 case 't':
525 register_fw_filename(AMD_FW_PSP_TRUSTLETS, optarg, 1);
526 pspflag = 1;
527 break;
528 case 'u':
529 register_fw_filename(AMD_FW_PSP_TRUSTLETKEY, optarg, 1);
530 pspflag = 1;
531 break;
532 case 'w':
Martin Roth8955d552016-11-08 11:46:31 -0700533 register_fw_filename(AMD_FW_PSP_SMU_FIRMWARE2,
534 optarg, 1);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800535 pspflag = 1;
536 break;
537 case 'm':
538 register_fw_filename(AMD_FW_PSP_SMUSCS, optarg, 1);
539 pspflag = 1;
540 break;
541#if PSP2
542 case 'P':
543 register_fw_filename(AMD_FW_PSP_PUBKEY, optarg, 2);
544 psp2flag = 1;
545 break;
546 case 'B':
547 register_fw_filename(AMD_FW_PSP_BOOTLOADER, optarg, 2);
548 psp2flag = 1;
549 break;
550 case 'S':
Martin Roth8955d552016-11-08 11:46:31 -0700551 register_fw_filename(AMD_FW_PSP_SMU_FIRMWARE,
552 optarg, 2);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800553 psp2flag = 1;
554 break;
555 case 'R':
556 register_fw_filename(AMD_FW_PSP_RECOVERY, optarg, 2);
557 psp2flag = 1;
558 break;
559 case 'K':
560 register_fw_filename(AMD_FW_PSP_RTM_PUBKEY, optarg, 2);
561 psp2flag = 1;
562 break;
563 case 'C':
564 register_fw_filename(AMD_FW_PSP_SECURED_OS, optarg, 2);
565 psp2flag = 1;
566 break;
567 case 'N':
568 register_fw_filename(AMD_FW_PSP_NVRAM, optarg, 2);
569 psp2flag = 1;
570 break;
571 case 'D':
Martin Roth8955d552016-11-08 11:46:31 -0700572 register_fw_filename(AMD_FW_PSP_SECURED_DEBUG,
573 optarg, 2);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800574 psp2flag = 1;
575 break;
576 case 'T':
577 register_fw_filename(AMD_FW_PSP_TRUSTLETS, optarg, 2);
578 psp2flag = 1;
579 break;
580 case 'U':
581 register_fw_filename(AMD_FW_PSP_TRUSTLETKEY, optarg, 2);
582 psp2flag = 1;
583 break;
584 case 'W':
Martin Roth8955d552016-11-08 11:46:31 -0700585 register_fw_filename(AMD_FW_PSP_SMU_FIRMWARE2,
586 optarg, 2);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800587 psp2flag = 1;
588 break;
589 case 'M':
590 register_fw_filename(AMD_FW_PSP_SMUSCS, optarg, 2);
591 psp2flag = 1;
592 break;
593#endif
594 case 'o':
595 output = optarg;
596 break;
Martin Roth60f15512016-11-08 09:55:01 -0700597 case 'f':
598 rom_size = (uint32_t)strtoul(optarg, &tmp, 16);
599 if (*tmp != '\0') {
600 printf("Error: ROM size specified"
601 " incorrectly (%s)\n\n", optarg);
Martin Roth31d95a22016-11-08 11:22:12 -0700602 retval = 1;
Martin Roth60f15512016-11-08 09:55:01 -0700603 }
604 break;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800605 case 'h':
606 usage();
Martin Roth31d95a22016-11-08 11:22:12 -0700607 return 0;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800608 default:
609 break;
610 }
611 }
612
Martin Roth8806f7f2016-11-08 10:44:18 -0700613 if (!output) {
Martin Roth31d95a22016-11-08 11:22:12 -0700614 printf("Error: Output value is not specified.\n\n");
615 retval = 1;
616 }
617
618 if (!rom_size) {
619 printf("Error: ROM Size is not specified.\n\n");
620 retval = 1;
Martin Roth8806f7f2016-11-08 10:44:18 -0700621 }
622
Martin Roth60f15512016-11-08 09:55:01 -0700623 if (rom_size % 1024 != 0) {
624 printf("Error: ROM Size (%d bytes) should be a multiple of"
Martin Roth31d95a22016-11-08 11:22:12 -0700625 " 1024 bytes.\n\n", rom_size);
626 retval = 1;
Martin Roth60f15512016-11-08 09:55:01 -0700627 }
628
629 if (rom_size < MIN_ROM_KB * 1024) {
Martin Roth31d95a22016-11-08 11:22:12 -0700630 printf("Error: ROM Size (%dKB) must be at least %dKB.\n\n",
Martin Roth60f15512016-11-08 09:55:01 -0700631 rom_size / 1024, MIN_ROM_KB);
Martin Roth31d95a22016-11-08 11:22:12 -0700632 retval = 1;
633 }
634
635 if (retval) {
636 usage();
637 return retval;
Martin Roth60f15512016-11-08 09:55:01 -0700638 }
639
640 printf(" AMDFWTOOL Using ROM size of %dKB\n", rom_size / 1024);
641
642 rom_base_address = 0xFFFFFFFF - rom_size + 1;
643 rom = malloc(rom_size);
644 if (!rom)
Martin Roth31d95a22016-11-08 11:22:12 -0700645 return 1;
Martin Rothcd15bc82016-11-08 11:34:02 -0700646 memset(rom, 0xFF, rom_size);
Martin Roth60f15512016-11-08 09:55:01 -0700647
648 current = AMD_ROMSIG_OFFSET;
Martin Roth8806f7f2016-11-08 10:44:18 -0700649 amd_romsig = (void *)(rom + AMD_ROMSIG_OFFSET);
Martin Roth60f15512016-11-08 09:55:01 -0700650 amd_romsig[0] = 0x55AA55AA; /* romsig */
651 amd_romsig[1] = 0;
652 amd_romsig[2] = 0;
653 amd_romsig[3] = 0;
654
655 current += 0x20; /* size of ROMSIG */
656 current = ALIGN(current, 0x1000U);
Martin Roth8955d552016-11-08 11:46:31 -0700657 current = integrate_firmwares(rom, current, amd_romsig,
658 amd_fw_table, rom_size);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800659
660 if (pspflag == 1) {
Martin Roth8806f7f2016-11-08 10:44:18 -0700661 current = ALIGN(current, 0x10000U);
662 pspdir = (void *)(rom + current);
Martin Roth60f15512016-11-08 09:55:01 -0700663 amd_romsig[4] = current + rom_base_address;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800664
665 current += 0x200; /* Conservative size of pspdir */
Martin Roth8955d552016-11-08 11:46:31 -0700666 current = integrate_psp_firmwares(rom, current, pspdir,
667 amd_psp_fw_table, rom_size);
zbaoc3a08a92016-03-02 14:47:27 +0800668 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800669
670#if PSP2
zbaoc3a08a92016-03-02 14:47:27 +0800671 if (psp2flag == 1) {
Martin Roth8806f7f2016-11-08 10:44:18 -0700672 current = ALIGN(current, 0x10000U); /* PSP2 dir */
673 psp2dir = (void *)(rom + current);
Martin Roth60f15512016-11-08 09:55:01 -0700674 amd_romsig[5] = current + rom_base_address;
Marc Jones350630a2016-09-20 22:55:54 -0600675 current += 0x200; /* Add conservative size of psp2dir. */
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800676
zbaoc3a08a92016-03-02 14:47:27 +0800677#if PSP_COMBO
678 /* TODO: remove the hardcode. */
679 psp2count = 1; /* Start from 1. */
680 /* for (; psp2count <= PSP2COUNT; psp2count++, current=ALIGN(current, 0x100)) { */
681 /* Now the psp2dir is psp combo dir. */
682 psp2dir[psp2count*4 + 0 + 4] = 0; /* 0 -Compare PSP ID, 1 -Compare chip family ID */
683 psp2dir[psp2count*4 + 1 + 4] = 0x10220B00; /* TODO: PSP ID. Documentation is needed. */
Martin Rothcd15bc82016-11-08 11:34:02 -0700684 psp2dir[psp2count*4 + 2 + 4] = current + rom_base_address;
zbaoc3a08a92016-03-02 14:47:27 +0800685 pspdir = rom + current;
686 psp2dir[psp2count*4 + 3 + 4] = 0;
Zheng Bao4fcc9f22015-11-20 12:29:04 +0800687
zbaoc3a08a92016-03-02 14:47:27 +0800688 current += 0x200; /* Add conservative size of pspdir. Start of PSP entries. */
Martin Roth8955d552016-11-08 11:46:31 -0700689 current = integrate_psp_firmwares(rom, current, pspdir,
690 amd_psp2_fw_table, rom_size);
zbaoc3a08a92016-03-02 14:47:27 +0800691 /* } */ /* End of loop */
Zheng Bao4fcc9f22015-11-20 12:29:04 +0800692
zbaoc3a08a92016-03-02 14:47:27 +0800693 /* fill the PSP combo head */
694 psp2dir[0] = 0x50535032; /* 'PSP2' */
695 psp2dir[2] = psp2count; /* Count */
696 psp2dir[3] = 1; /* 0-Dynamic look up through all entries, 1-PSP/chip ID match */
697 psp2dir[4] = 0; /* reserved 4 dwords. */
698 psp2dir[5] = 0;
699 psp2dir[6] = 0;
700 psp2dir[7] = 0;
Martin Roth8955d552016-11-08 11:46:31 -0700701 psp2dir[1] = fletcher32((uint16_t *)&psp2dir[1],
702 (psp2count * 16 + 32) / 2 - 2);
zbaoc3a08a92016-03-02 14:47:27 +0800703#else
Martin Roth8955d552016-11-08 11:46:31 -0700704 current = integrate_psp_firmwares(rom, current, psp2dir,
705 amd_psp2_fw_table, rom_size);
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800706#endif
707 }
zbaoc3a08a92016-03-02 14:47:27 +0800708#endif
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800709
710 targetfd = open(output, O_RDWR | O_CREAT | O_TRUNC, 0666);
Martin Roth31d95a22016-11-08 11:22:12 -0700711 if (targetfd >= 0) {
712 write(targetfd, amd_romsig, current - AMD_ROMSIG_OFFSET);
713 close(targetfd);
714 } else {
715 printf("Error: could not open file: %s\n", output);
716 retval = 1;
717 }
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800718
Martin Roth31d95a22016-11-08 11:22:12 -0700719 free(rom);
720 return retval;
Zheng Bao9c7ff7b2015-11-17 22:57:39 +0800721}