Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Marshall Dawson | e7d892c | 2016-10-08 14:49:41 -0600 | [diff] [blame] | 4 | * Copyright (C) 2015 - 2016 Advanced Micro Devices, Inc. |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | /* |
| 17 | * ROMSIG At ROMBASE + 0x20000: |
zbao | c3b0b72 | 2016-02-19 13:47:31 +0800 | [diff] [blame] | 18 | * 0 4 8 C |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 19 | * +------------+---------------+----------------+------------+ |
| 20 | * | 0x55AA55AA |EC ROM Address |GEC ROM Address |USB3 ROM | |
| 21 | * +------------+---------------+----------------+------------+ |
zbao | c3b0b72 | 2016-02-19 13:47:31 +0800 | [diff] [blame] | 22 | * | PSPDIR ADDR|PSPDIR ADDR |<-- Field 0x14 could be either |
| 23 | * +------------+---------------+ 2nd PSP directory or PSP COMBO directory |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 24 | * EC ROM should be 64K aligned. |
| 25 | * |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 26 | * PSP directory (Where "PSPDIR ADDR" points) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 27 | * +------------+---------------+----------------+------------+ |
| 28 | * | 'PSP$' | Fletcher | Count | Reserved | |
| 29 | * +------------+---------------+----------------+------------+ |
| 30 | * | 0 | size | Base address | Reserved | Pubkey |
| 31 | * +------------+---------------+----------------+------------+ |
| 32 | * | 1 | size | Base address | Reserved | Bootloader |
| 33 | * +------------+---------------+----------------+------------+ |
| 34 | * | 8 | size | Base address | Reserved | Smu Firmware |
| 35 | * +------------+---------------+----------------+------------+ |
| 36 | * | 3 | size | Base address | Reserved | Recovery Firmware |
| 37 | * +------------+---------------+----------------+------------+ |
| 38 | * | | |
| 39 | * | | |
| 40 | * | Other PSP Firmware | |
| 41 | * | | |
| 42 | * | | |
| 43 | * +------------+---------------+----------------+------------+ |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 44 | * |
zbao | c3b0b72 | 2016-02-19 13:47:31 +0800 | [diff] [blame] | 45 | * PSP Combo directory |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 46 | * +------------+---------------+----------------+------------+ |
zbao | 6e2f3d1 | 2016-02-19 13:34:59 +0800 | [diff] [blame] | 47 | * | 'PSP2' | Fletcher | Count |Look up mode| |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 48 | * +------------+---------------+----------------+------------+ |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 49 | * | R e s e r v e d | |
| 50 | * +------------+---------------+----------------+------------+ |
zbao | 6e2f3d1 | 2016-02-19 13:34:59 +0800 | [diff] [blame] | 51 | * | ID-Sel | PSP ID | PSPDIR ADDR | | 2nd PSP directory |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 52 | * +------------+---------------+----------------+------------+ |
zbao | 6e2f3d1 | 2016-02-19 13:34:59 +0800 | [diff] [blame] | 53 | * | ID-Sel | PSP ID | PSPDIR ADDR | | 3rd PSP directory |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 54 | * +------------+---------------+----------------+------------+ |
| 55 | * | | |
| 56 | * | Other PSP | |
| 57 | * | | |
| 58 | * +------------+---------------+----------------+------------+ |
| 59 | * |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 60 | */ |
| 61 | |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 62 | #include <fcntl.h> |
| 63 | #include <errno.h> |
| 64 | #include <stdio.h> |
| 65 | #include <sys/stat.h> |
| 66 | #include <sys/types.h> |
| 67 | #include <unistd.h> |
| 68 | #include <string.h> |
| 69 | #include <stdlib.h> |
| 70 | #include <getopt.h> |
| 71 | |
| 72 | #ifndef CONFIG_ROM_SIZE |
| 73 | #define CONFIG_ROM_SIZE 0x400000 |
| 74 | #endif |
| 75 | |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 76 | #define AMD_ROMSIG_OFFSET 0x20000 |
| 77 | #define MIN_ROM_KB 256 |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 78 | |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 79 | #define ALIGN(val, by) (((val) + (by) - 1) & ~((by) - 1)) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 80 | |
| 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 |
zbao | c3b0b72 | 2016-02-19 13:47:31 +0800 | [diff] [blame] | 87 | #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 Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 94 | |
| 95 | typedef unsigned int uint32_t; |
| 96 | typedef unsigned char uint8_t; |
| 97 | typedef 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 Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 119 | static uint32_t fletcher32(const uint16_t *pptr, int length) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 120 | { |
| 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 Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 148 | static void usage(void) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 149 | { |
Martin Roth | 0e94062 | 2016-11-08 10:37:53 -0700 | [diff] [blame] | 150 | 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 Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | typedef 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, |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 213 | AMD_FW_INVALID, |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 214 | } amd_fw_type; |
| 215 | |
| 216 | typedef struct _amd_fw_entry { |
| 217 | amd_fw_type type; |
| 218 | char *filename; |
| 219 | } amd_fw_entry; |
| 220 | |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 221 | static amd_fw_entry amd_psp_fw_table[] = { |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 222 | { .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 }, |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 235 | { .type = AMD_FW_INVALID }, |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | #if PSP2 |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 239 | static amd_fw_entry amd_psp2_fw_table[] = { |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 240 | { .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 }, |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 253 | { .type = AMD_FW_INVALID }, |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 254 | }; |
| 255 | #endif |
| 256 | |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 257 | static amd_fw_entry amd_fw_table[] = { |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 258 | { .type = AMD_FW_XHCI }, |
| 259 | { .type = AMD_FW_IMC }, |
| 260 | { .type = AMD_FW_GEC }, |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 261 | { .type = AMD_FW_INVALID }, |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 262 | }; |
| 263 | |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 264 | static void fill_psp_head(uint32_t *pspdir, uint32_t count) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 265 | { |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 266 | pspdir[0] = 0x50535024; /* 'PSP$' */ |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 267 | pspdir[2] = count; /* size */ |
| 268 | pspdir[3] = 0; |
Martin Roth | 8955d55 | 2016-11-08 11:46:31 -0700 | [diff] [blame] | 269 | pspdir[1] = fletcher32((uint16_t *)&pspdir[1], |
| 270 | (count * 16 + 16) / 2 - 2); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 271 | } |
| 272 | |
Martin Roth | 8955d55 | 2016-11-08 11:46:31 -0700 | [diff] [blame] | 273 | static uint32_t integrate_firmwares(char *base, uint32_t pos, uint32_t *romsig, |
| 274 | amd_fw_entry *fw_table, uint32_t rom_size) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 275 | { |
| 276 | int fd; |
| 277 | struct stat fd_stat; |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 278 | int i; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 279 | uint32_t rom_base_address = 0xFFFFFFFF - rom_size + 1; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 280 | |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 281 | for (i = 0; fw_table[i].type != AMD_FW_INVALID; i++) { |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 282 | if (fw_table[i].filename != NULL) { |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 283 | fd = open(fw_table[i].filename, O_RDONLY); |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 284 | fstat(fd, &fd_stat); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 285 | |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 286 | switch (fw_table[i].type) { |
| 287 | case AMD_FW_IMC: |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 288 | pos = ALIGN(pos, 0x10000U); |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 289 | romsig[1] = pos + rom_base_address; |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 290 | break; |
| 291 | case AMD_FW_GEC: |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 292 | romsig[2] = pos + rom_base_address; |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 293 | break; |
| 294 | case AMD_FW_XHCI: |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 295 | romsig[3] = pos + rom_base_address; |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 296 | break; |
| 297 | default: |
| 298 | /* Error */ |
| 299 | break; |
| 300 | } |
| 301 | |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 302 | 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 Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 310 | read(fd, (void *)(base + pos), (size_t)fd_stat.st_size); |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 311 | |
| 312 | pos += fd_stat.st_size; |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 313 | close(fd); |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 314 | pos = ALIGN(pos, 0x100U); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 315 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | return pos; |
| 319 | } |
| 320 | |
Martin Roth | 8955d55 | 2016-11-08 11:46:31 -0700 | [diff] [blame] | 321 | static 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 Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 325 | { |
| 326 | int fd; |
| 327 | struct stat fd_stat; |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 328 | unsigned int i; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 329 | uint32_t rom_base_address = 0xFFFFFFFF - rom_size + 1; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 330 | |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 331 | for (i = 0; fw_table[i].type != AMD_FW_INVALID; i++) { |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 332 | 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 Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 339 | |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 340 | fd = open(fw_table[i].filename, O_RDONLY); |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 341 | fstat(fd, &fd_stat); |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 342 | pspdir[4+4*i+1] = (uint32_t)fd_stat.st_size; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 343 | |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 344 | pspdir[4+4*i+2] = pos + rom_base_address; |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 345 | pspdir[4+4*i+3] = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 346 | |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 347 | 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 Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 351 | free(base); |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 352 | exit(1); |
| 353 | } |
| 354 | |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 355 | read(fd, (void *)(base + pos), (size_t)fd_stat.st_size); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 356 | |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 357 | pos += fd_stat.st_size; |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 358 | close(fd); |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 359 | pos = ALIGN(pos, 0x100U); |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 360 | } else { |
| 361 | /* This APU doesn't have this firmware. */ |
| 362 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 363 | } |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 364 | fill_psp_head(pspdir, i); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 365 | return pos; |
| 366 | } |
| 367 | |
Marc Jones | 90099b6 | 2016-09-20 21:05:45 -0600 | [diff] [blame] | 368 | #if PSP2 |
Martin Roth | 8955d55 | 2016-11-08 11:46:31 -0700 | [diff] [blame] | 369 | static 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 Jones | 90099b6 | 2016-09-20 21:05:45 -0600 | [diff] [blame] | 371 | #else |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 372 | static const char *optstring = "x:i:g:p:b:s:r:k:c:n:d:t:u:w:m:o:f:h"; |
Marc Jones | 90099b6 | 2016-09-20 21:05:45 -0600 | [diff] [blame] | 373 | #endif |
| 374 | |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 375 | static struct option long_options[] = { |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 376 | {"xhci", required_argument, 0, 'x' }, |
| 377 | {"imc", required_argument, 0, 'i' }, |
| 378 | {"gec", required_argument, 0, 'g' }, |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 379 | /* PSP */ |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 380 | {"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 Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 392 | |
| 393 | /* TODO: PSP2 */ |
| 394 | #if PSP2 |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 395 | {"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 Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 407 | #endif |
| 408 | |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 409 | {"output", required_argument, 0, 'o' }, |
| 410 | {"flashsize", required_argument, 0, 'f' }, |
| 411 | {"help", no_argument, 0, 'h' }, |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 412 | |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 413 | {NULL, 0, 0, 0 } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 414 | }; |
| 415 | |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 416 | static void register_fw_filename(amd_fw_type type, char filename[], int pspflag) |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 417 | { |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 418 | unsigned int i; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 419 | |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 420 | for (i = 0; i < sizeof(amd_fw_table) / sizeof(amd_fw_entry); i++) { |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 421 | if (amd_fw_table[i].type == type) { |
| 422 | amd_fw_table[i].filename = filename; |
| 423 | return; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | if (pspflag == 1) { |
Martin Roth | 8955d55 | 2016-11-08 11:46:31 -0700 | [diff] [blame] | 428 | for (i = 0; i < sizeof(amd_psp_fw_table) / |
| 429 | sizeof(amd_fw_entry); i++) { |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 430 | 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 Roth | 8955d55 | 2016-11-08 11:46:31 -0700 | [diff] [blame] | 439 | for (i = 0; i < sizeof(amd_psp2_fw_table) / |
| 440 | sizeof(amd_fw_entry); i++) { |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 441 | 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 | |
| 450 | int main(int argc, char **argv) |
| 451 | { |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 452 | int c, pspflag = 0; |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 453 | int retval = 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 454 | #if PSP2 |
| 455 | int psp2flag = 0; |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 456 | uint32_t *psp2dir; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 457 | char *tmp; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 458 | #endif |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 459 | #if PSP_COMBO |
| 460 | int psp2count; |
| 461 | #endif |
| 462 | |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 463 | char *rom = NULL; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 464 | uint32_t current; |
| 465 | uint32_t *amd_romsig, *pspdir; |
| 466 | |
| 467 | int targetfd; |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 468 | char *output = NULL; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 469 | uint32_t rom_size = CONFIG_ROM_SIZE; |
| 470 | uint32_t rom_base_address; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 471 | |
| 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 Roth | 8955d55 | 2016-11-08 11:46:31 -0700 | [diff] [blame] | 499 | register_fw_filename(AMD_FW_PSP_SMU_FIRMWARE, |
| 500 | optarg, 1); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 501 | 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 Roth | 8955d55 | 2016-11-08 11:46:31 -0700 | [diff] [blame] | 520 | register_fw_filename(AMD_FW_PSP_SECURED_DEBUG, |
| 521 | optarg, 1); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 522 | 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 Roth | 8955d55 | 2016-11-08 11:46:31 -0700 | [diff] [blame] | 533 | register_fw_filename(AMD_FW_PSP_SMU_FIRMWARE2, |
| 534 | optarg, 1); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 535 | 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 Roth | 8955d55 | 2016-11-08 11:46:31 -0700 | [diff] [blame] | 551 | register_fw_filename(AMD_FW_PSP_SMU_FIRMWARE, |
| 552 | optarg, 2); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 553 | 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 Roth | 8955d55 | 2016-11-08 11:46:31 -0700 | [diff] [blame] | 572 | register_fw_filename(AMD_FW_PSP_SECURED_DEBUG, |
| 573 | optarg, 2); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 574 | 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 Roth | 8955d55 | 2016-11-08 11:46:31 -0700 | [diff] [blame] | 585 | register_fw_filename(AMD_FW_PSP_SMU_FIRMWARE2, |
| 586 | optarg, 2); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 587 | 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 Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 597 | 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 Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 602 | retval = 1; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 603 | } |
| 604 | break; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 605 | case 'h': |
| 606 | usage(); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 607 | return 0; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 608 | default: |
| 609 | break; |
| 610 | } |
| 611 | } |
| 612 | |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 613 | if (!output) { |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 614 | 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 Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 621 | } |
| 622 | |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 623 | if (rom_size % 1024 != 0) { |
| 624 | printf("Error: ROM Size (%d bytes) should be a multiple of" |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 625 | " 1024 bytes.\n\n", rom_size); |
| 626 | retval = 1; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | if (rom_size < MIN_ROM_KB * 1024) { |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 630 | printf("Error: ROM Size (%dKB) must be at least %dKB.\n\n", |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 631 | rom_size / 1024, MIN_ROM_KB); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 632 | retval = 1; |
| 633 | } |
| 634 | |
| 635 | if (retval) { |
| 636 | usage(); |
| 637 | return retval; |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 638 | } |
| 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 Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 645 | return 1; |
Martin Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 646 | memset(rom, 0xFF, rom_size); |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 647 | |
| 648 | current = AMD_ROMSIG_OFFSET; |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 649 | amd_romsig = (void *)(rom + AMD_ROMSIG_OFFSET); |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 650 | 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 Roth | 8955d55 | 2016-11-08 11:46:31 -0700 | [diff] [blame] | 657 | current = integrate_firmwares(rom, current, amd_romsig, |
| 658 | amd_fw_table, rom_size); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 659 | |
| 660 | if (pspflag == 1) { |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 661 | current = ALIGN(current, 0x10000U); |
| 662 | pspdir = (void *)(rom + current); |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 663 | amd_romsig[4] = current + rom_base_address; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 664 | |
| 665 | current += 0x200; /* Conservative size of pspdir */ |
Martin Roth | 8955d55 | 2016-11-08 11:46:31 -0700 | [diff] [blame] | 666 | current = integrate_psp_firmwares(rom, current, pspdir, |
| 667 | amd_psp_fw_table, rom_size); |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 668 | } |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 669 | |
| 670 | #if PSP2 |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 671 | if (psp2flag == 1) { |
Martin Roth | 8806f7f | 2016-11-08 10:44:18 -0700 | [diff] [blame] | 672 | current = ALIGN(current, 0x10000U); /* PSP2 dir */ |
| 673 | psp2dir = (void *)(rom + current); |
Martin Roth | 60f1551 | 2016-11-08 09:55:01 -0700 | [diff] [blame] | 674 | amd_romsig[5] = current + rom_base_address; |
Marc Jones | 350630a | 2016-09-20 22:55:54 -0600 | [diff] [blame] | 675 | current += 0x200; /* Add conservative size of psp2dir. */ |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 676 | |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 677 | #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 Roth | cd15bc8 | 2016-11-08 11:34:02 -0700 | [diff] [blame] | 684 | psp2dir[psp2count*4 + 2 + 4] = current + rom_base_address; |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 685 | pspdir = rom + current; |
| 686 | psp2dir[psp2count*4 + 3 + 4] = 0; |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 687 | |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 688 | current += 0x200; /* Add conservative size of pspdir. Start of PSP entries. */ |
Martin Roth | 8955d55 | 2016-11-08 11:46:31 -0700 | [diff] [blame] | 689 | current = integrate_psp_firmwares(rom, current, pspdir, |
| 690 | amd_psp2_fw_table, rom_size); |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 691 | /* } */ /* End of loop */ |
Zheng Bao | 4fcc9f2 | 2015-11-20 12:29:04 +0800 | [diff] [blame] | 692 | |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 693 | /* 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 Roth | 8955d55 | 2016-11-08 11:46:31 -0700 | [diff] [blame] | 701 | psp2dir[1] = fletcher32((uint16_t *)&psp2dir[1], |
| 702 | (psp2count * 16 + 32) / 2 - 2); |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 703 | #else |
Martin Roth | 8955d55 | 2016-11-08 11:46:31 -0700 | [diff] [blame] | 704 | current = integrate_psp_firmwares(rom, current, psp2dir, |
| 705 | amd_psp2_fw_table, rom_size); |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 706 | #endif |
| 707 | } |
zbao | c3a08a9 | 2016-03-02 14:47:27 +0800 | [diff] [blame] | 708 | #endif |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 709 | |
| 710 | targetfd = open(output, O_RDWR | O_CREAT | O_TRUNC, 0666); |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 711 | 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 Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 718 | |
Martin Roth | 31d95a2 | 2016-11-08 11:22:12 -0700 | [diff] [blame] | 719 | free(rom); |
| 720 | return retval; |
Zheng Bao | 9c7ff7b | 2015-11-17 22:57:39 +0800 | [diff] [blame] | 721 | } |