| 1 |
|
| 2 |
|
| 3 |
#include <assert.h> |
| 4 |
#include <fcntl.h> |
| 5 |
#include <errno.h> |
| 6 |
#include <limits.h> |
| 7 |
#include <openssl/sha.h> |
| 8 |
#include <stdio.h> |
| 9 |
#include <sys/stat.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <string.h> |
| 12 |
#include <stdlib.h> |
| 13 |
|
| 14 |
#include "amdfwtool.h" |
| 15 |
|
| 16 |
|
| 17 |
enum hash_header_ver { |
| 18 |
HASH_HDR_V1 = 1, |
| 19 |
HASH_HDR_V2, |
| 20 |
}; |
| 21 |
|
| 22 |
enum signature_id { |
| 23 |
SIG_ID_RSA2048, |
| 24 |
SIG_ID_RSA4096 = 2, |
| 25 |
}; |
| 26 |
|
| 27 |
#define HASH_FILE_SUFFIX ".hash" |
| 28 |
struct psp_fw_hash_file_info { |
| 29 |
int fd; |
| 30 |
bool present; |
| 31 |
struct psp_fw_hash_table hash_header; |
| 32 |
}; |
| 33 |
static struct psp_fw_hash_file_info hash_files[MAX_NUM_HASH_TABLES]; |
| 34 |
|
| 35 |
#define UUID_MAGIC "gpd.ta.appID" |
| 36 |
|
| 37 |
static uint16_t get_psp_fw_type(enum platform soc_id, struct amd_fw_header *header) |
| 38 |
{ |
| 39 |
switch (soc_id) { |
| 40 |
case PLATFORM_MENDOCINO: |
| 41 |
case PLATFORM_PHOENIX: |
| 42 |
case PLATFORM_GLINDA: |
| 43 |
case PLATFORM_FAEGAN: |
| 44 |
|
| 45 |
|
| 46 |
return header->fw_id ? header->fw_id : header->fw_type; |
| 47 |
default: |
| 48 |
return header->fw_type; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
static void get_psp_fw_uuid(void *buf, size_t buf_len, uint8_t *uuid) |
| 53 |
{ |
| 54 |
void *ptr = memmem(buf, buf_len, UUID_MAGIC, strlen(UUID_MAGIC)); |
| 55 |
|
| 56 |
assert(ptr != NULL); |
| 57 |
|
| 58 |
memcpy(uuid, ptr + strlen(UUID_MAGIC), UUID_LEN_BYTES); |
| 59 |
} |
| 60 |
|
| 61 |
static int add_single_sha(amd_fw_entry_hash *entry, void *buf, enum platform soc_id, |
| 62 |
fwid_type_t fwid_type) |
| 63 |
{ |
| 64 |
uint8_t hash[SHA384_DIGEST_LENGTH]; |
| 65 |
struct amd_fw_header *header = (struct amd_fw_header *)buf; |
| 66 |
|
| 67 |
size_t len = header->fw_size_signed + sizeof(struct amd_fw_header); |
| 68 |
uint8_t *body = (uint8_t *)buf; |
| 69 |
|
| 70 |
if (len > header->size_total) |
| 71 |
return -1; |
| 72 |
|
| 73 |
if (header->sig_id == SIG_ID_RSA4096) { |
| 74 |
SHA384(body, len, hash); |
| 75 |
entry->sha_len = SHA384_DIGEST_LENGTH; |
| 76 |
} else if (header->sig_id == SIG_ID_RSA2048) { |
| 77 |
SHA256(body, len, hash); |
| 78 |
entry->sha_len = SHA256_DIGEST_LENGTH; |
| 79 |
} else { |
| 80 |
fprintf(stderr, "%s: Unknown signature id: 0x%08x\n", |
| 81 |
__func__, header->sig_id); |
| 82 |
return -1; |
| 83 |
} |
| 84 |
|
| 85 |
memcpy(entry->sha, hash, entry->sha_len); |
| 86 |
entry->fwid_type = fwid_type; |
| 87 |
if (fwid_type == FWID_TYPE_UUID) |
| 88 |
get_psp_fw_uuid(buf, header->size_total, entry->uuid); |
| 89 |
else |
| 90 |
entry->fw_id = get_psp_fw_type(soc_id, header); |
| 91 |
entry->subtype = header->fw_subtype; |
| 92 |
|
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
static int get_num_binaries(void *buf, size_t buf_size) |
| 97 |
{ |
| 98 |
struct amd_fw_header *header = (struct amd_fw_header *)buf; |
| 99 |
size_t total_len = 0; |
| 100 |
int num_binaries = 0; |
| 101 |
|
| 102 |
while (total_len < buf_size) { |
| 103 |
num_binaries++; |
| 104 |
total_len += header->size_total; |
| 105 |
header = (struct amd_fw_header *)(buf + total_len); |
| 106 |
} |
| 107 |
|
| 108 |
if (total_len != buf_size) { |
| 109 |
fprintf(stderr, "Malformed binary\n"); |
| 110 |
return -1; |
| 111 |
} |
| 112 |
return num_binaries; |
| 113 |
} |
| 114 |
|
| 115 |
static int add_sha(amd_fw_entry *entry, void *buf, size_t buf_size, enum platform soc_id) |
| 116 |
{ |
| 117 |
struct amd_fw_header *header = (struct amd_fw_header *)buf; |
| 118 |
|
| 119 |
size_t total_len = 0; |
| 120 |
int num_binaries = get_num_binaries(buf, buf_size); |
| 121 |
|
| 122 |
if (num_binaries <= 0) |
| 123 |
return num_binaries; |
| 124 |
|
| 125 |
entry->hash_entries = calloc(num_binaries, sizeof(amd_fw_entry_hash)); |
| 126 |
if (!entry->hash_entries) { |
| 127 |
fprintf(stderr, "Error allocating memory to add FW hash\n"); |
| 128 |
return -1; |
| 129 |
} |
| 130 |
entry->num_hash_entries = num_binaries; |
| 131 |
|
| 132 |
|
| 133 |
for (int i = 0; i < num_binaries; i++) { |
| 134 |
if (add_single_sha(&entry->hash_entries[i], buf + total_len, soc_id, |
| 135 |
entry->fwid_type)) { |
| 136 |
free(entry->hash_entries); |
| 137 |
return -1; |
| 138 |
} |
| 139 |
total_len += header->size_total; |
| 140 |
header = (struct amd_fw_header *)(buf + total_len); |
| 141 |
} |
| 142 |
|
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
static void write_one_psp_firmware_hash_entry(int fd, amd_fw_entry_hash *entry, |
| 147 |
uint8_t hash_tbl_id) |
| 148 |
{ |
| 149 |
uint16_t subtype = entry->subtype; |
| 150 |
|
| 151 |
if (hash_files[hash_tbl_id].hash_header.version == HASH_HDR_V2) { |
| 152 |
write_or_fail(fd, entry->uuid, UUID_LEN_BYTES); |
| 153 |
} else { |
| 154 |
write_or_fail(fd, &entry->fw_id, sizeof(entry->fw_id)); |
| 155 |
write_or_fail(fd, &subtype, sizeof(subtype)); |
| 156 |
} |
| 157 |
write_or_fail(fd, entry->sha, entry->sha_len); |
| 158 |
} |
| 159 |
|
| 160 |
static void open_psp_fw_hash_files(const char *file_prefix) |
| 161 |
{ |
| 162 |
size_t hash_file_strlen; |
| 163 |
char *hash_file_name; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
assert(MAX_NUM_HASH_TABLES < 10); |
| 169 |
|
| 170 |
hash_file_strlen = strlen(file_prefix) + 2 + strlen(HASH_FILE_SUFFIX) + 1; |
| 171 |
hash_file_name = malloc(hash_file_strlen); |
| 172 |
if (!hash_file_name) { |
| 173 |
fprintf(stderr, "malloc(%lu) failed\n", hash_file_strlen); |
| 174 |
exit(-1); |
| 175 |
} |
| 176 |
|
| 177 |
for (unsigned int i = 0; i < MAX_NUM_HASH_TABLES; i++) { |
| 178 |
|
| 179 |
|
| 180 |
if (!hash_files[i].present) |
| 181 |
break; |
| 182 |
|
| 183 |
if (i) |
| 184 |
snprintf(hash_file_name, hash_file_strlen, "%s.%d%s", |
| 185 |
file_prefix, i, HASH_FILE_SUFFIX); |
| 186 |
else |
| 187 |
|
| 188 |
snprintf(hash_file_name, hash_file_strlen, "%s%s", |
| 189 |
file_prefix, HASH_FILE_SUFFIX); |
| 190 |
|
| 191 |
hash_files[i].fd = open(hash_file_name, O_RDWR | O_CREAT | O_TRUNC, 0666); |
| 192 |
if (hash_files[i].fd < 0) { |
| 193 |
fprintf(stderr, "Error opening file: %s: %s\n", |
| 194 |
hash_file_name, strerror(errno)); |
| 195 |
free(hash_file_name); |
| 196 |
exit(-1); |
| 197 |
} |
| 198 |
} |
| 199 |
free(hash_file_name); |
| 200 |
} |
| 201 |
|
| 202 |
static void close_psp_fw_hash_files(void) |
| 203 |
{ |
| 204 |
for (unsigned int i = 0; i < MAX_NUM_HASH_TABLES; i++) { |
| 205 |
if (!hash_files[i].present) |
| 206 |
break; |
| 207 |
|
| 208 |
close(hash_files[i].fd); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
static void write_psp_firmware_hash(amd_fw_entry *fw_table) |
| 213 |
{ |
| 214 |
uint8_t hash_tbl_id; |
| 215 |
|
| 216 |
for (unsigned int i = 0; fw_table[i].type != AMD_FW_INVALID; i++) { |
| 217 |
hash_tbl_id = fw_table[i].hash_tbl_id; |
| 218 |
assert(hash_files[hash_tbl_id].present); |
| 219 |
|
| 220 |
for (unsigned int j = 0; j < fw_table[i].num_hash_entries; j++) { |
| 221 |
if (fw_table[i].hash_entries[j].sha_len == SHA256_DIGEST_LENGTH) { |
| 222 |
hash_files[hash_tbl_id].hash_header.no_of_entries_256++; |
| 223 |
} else if (fw_table[i].hash_entries[j].sha_len == |
| 224 |
SHA384_DIGEST_LENGTH) { |
| 225 |
hash_files[hash_tbl_id].hash_header.no_of_entries_384++; |
| 226 |
} else if (fw_table[i].hash_entries[j].sha_len) { |
| 227 |
fprintf(stderr, "%s: Error invalid sha_len %d\n", |
| 228 |
__func__, fw_table[i].hash_entries[j].sha_len); |
| 229 |
exit(-1); |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
for (unsigned int i = 0; i < MAX_NUM_HASH_TABLES; i++) { |
| 235 |
if (!hash_files[i].present) |
| 236 |
continue; |
| 237 |
write_or_fail(hash_files[i].fd, &hash_files[i].hash_header, |
| 238 |
sizeof(hash_files[i].hash_header)); |
| 239 |
|
| 240 |
if (hash_files[i].hash_header.version == HASH_HDR_V2) { |
| 241 |
uint16_t reserved = 0; |
| 242 |
|
| 243 |
write_or_fail(hash_files[i].fd, &reserved, sizeof(reserved)); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
for (unsigned int i = 0; fw_table[i].type != AMD_FW_INVALID; i++) { |
| 251 |
hash_tbl_id = fw_table[i].hash_tbl_id; |
| 252 |
for (unsigned int j = 0; j < fw_table[i].num_hash_entries; j++) { |
| 253 |
if (fw_table[i].hash_entries[j].sha_len == SHA256_DIGEST_LENGTH) |
| 254 |
write_one_psp_firmware_hash_entry(hash_files[hash_tbl_id].fd, |
| 255 |
&fw_table[i].hash_entries[j], hash_tbl_id); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
for (unsigned int i = 0; fw_table[i].type != AMD_FW_INVALID; i++) { |
| 260 |
hash_tbl_id = fw_table[i].hash_tbl_id; |
| 261 |
for (unsigned int j = 0; j < fw_table[i].num_hash_entries; j++) { |
| 262 |
if (fw_table[i].hash_entries[j].sha_len == SHA384_DIGEST_LENGTH) |
| 263 |
write_one_psp_firmware_hash_entry(hash_files[hash_tbl_id].fd, |
| 264 |
&fw_table[i].hash_entries[j], hash_tbl_id); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
for (unsigned int i = 0; fw_table[i].type != AMD_FW_INVALID; i++) { |
| 269 |
if (!fw_table[i].num_hash_entries || !fw_table[i].hash_entries) |
| 270 |
continue; |
| 271 |
|
| 272 |
free(fw_table[i].hash_entries); |
| 273 |
fw_table[i].hash_entries = NULL; |
| 274 |
fw_table[i].num_hash_entries = 0; |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
static void update_hash_files_config(amd_fw_entry *fw_table) |
| 279 |
{ |
| 280 |
uint16_t version = fw_table->fwid_type == FWID_TYPE_UUID ? HASH_HDR_V2 : HASH_HDR_V1; |
| 281 |
|
| 282 |
hash_files[fw_table->hash_tbl_id].present = true; |
| 283 |
if (version > hash_files[fw_table->hash_tbl_id].hash_header.version) |
| 284 |
hash_files[fw_table->hash_tbl_id].hash_header.version = version; |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
void process_signed_psp_firmwares(const char *signed_rom, |
| 296 |
amd_fw_entry *fw_table, |
| 297 |
uint64_t signed_start_addr, |
| 298 |
enum platform soc_id) |
| 299 |
{ |
| 300 |
unsigned int i; |
| 301 |
int fd; |
| 302 |
int signed_rom_fd; |
| 303 |
ssize_t bytes, align_bytes; |
| 304 |
uint8_t *buf; |
| 305 |
struct amd_fw_header header; |
| 306 |
struct stat fd_stat; |
| 307 |
|
| 308 |
|
| 309 |
uint8_t align_data[BLOB_ALIGNMENT - 1]; |
| 310 |
|
| 311 |
memset(align_data, 0xff, sizeof(align_data)); |
| 312 |
signed_rom_fd = open(signed_rom, O_RDWR | O_CREAT | O_TRUNC, |
| 313 |
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); |
| 314 |
if (signed_rom_fd < 0) { |
| 315 |
fprintf(stderr, "Error opening file: %s: %s\n", |
| 316 |
signed_rom, strerror(errno)); |
| 317 |
return; |
| 318 |
} |
| 319 |
|
| 320 |
for (i = 0; fw_table[i].type != AMD_FW_INVALID; i++) { |
| 321 |
fw_table[i].num_hash_entries = 0; |
| 322 |
fw_table[i].hash_entries = NULL; |
| 323 |
|
| 324 |
if (!(fw_table[i].filename) || fw_table[i].skip_hashing) |
| 325 |
continue; |
| 326 |
|
| 327 |
memset(&header, 0, sizeof(header)); |
| 328 |
|
| 329 |
fd = open(fw_table[i].filename, O_RDONLY); |
| 330 |
if (fd < 0) { |
| 331 |
|
| 332 |
fprintf(stderr, "Error opening file: %s: %s\n", |
| 333 |
fw_table[i].filename, strerror(errno)); |
| 334 |
continue; |
| 335 |
} |
| 336 |
|
| 337 |
if (fstat(fd, &fd_stat)) { |
| 338 |
|
| 339 |
fprintf(stderr, "fstat error: %s\n", strerror(errno)); |
| 340 |
close(fd); |
| 341 |
continue; |
| 342 |
} |
| 343 |
|
| 344 |
bytes = read_from_file_to_buf(fd, &header, sizeof(struct amd_fw_header)); |
| 345 |
if (bytes != (ssize_t)sizeof(struct amd_fw_header)) { |
| 346 |
|
| 347 |
fprintf(stderr, "%s: Error reading header from %s\n", |
| 348 |
__func__, fw_table[i].filename); |
| 349 |
close(fd); |
| 350 |
continue; |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
if (!header.fw_type && !header.fw_id) { |
| 355 |
fprintf(stderr, "%s: Invalid FWID for %s\n", |
| 356 |
__func__, fw_table[i].filename); |
| 357 |
close(fd); |
| 358 |
continue; |
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
if (header.sig_opt != 1) { |
| 365 |
close(fd); |
| 366 |
continue; |
| 367 |
} |
| 368 |
|
| 369 |
buf = malloc(fd_stat.st_size); |
| 370 |
if (!buf) { |
| 371 |
|
| 372 |
fprintf(stderr, "%s: failed to allocate memory with size %lld\n", |
| 373 |
__func__, (long long)fd_stat.st_size); |
| 374 |
close(fd); |
| 375 |
continue; |
| 376 |
} |
| 377 |
|
| 378 |
lseek(fd, SEEK_SET, 0); |
| 379 |
bytes = read_from_file_to_buf(fd, buf, fd_stat.st_size); |
| 380 |
if (bytes != fd_stat.st_size) { |
| 381 |
|
| 382 |
fprintf(stderr, "%s: failed to read %s\n", |
| 383 |
__func__, fw_table[i].filename); |
| 384 |
free(buf); |
| 385 |
close(fd); |
| 386 |
continue; |
| 387 |
} |
| 388 |
|
| 389 |
bytes = write_from_buf_to_file(signed_rom_fd, buf, fd_stat.st_size); |
| 390 |
if (bytes != fd_stat.st_size) { |
| 391 |
|
| 392 |
fprintf(stderr, "%s: failed to write %s\n", |
| 393 |
__func__, fw_table[i].filename); |
| 394 |
free(buf); |
| 395 |
close(fd); |
| 396 |
continue; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
align_bytes = 0; |
| 401 |
if (fd_stat.st_size & (BLOB_ALIGNMENT - 1)) { |
| 402 |
align_bytes = BLOB_ALIGNMENT - |
| 403 |
(fd_stat.st_size & (BLOB_ALIGNMENT - 1)); |
| 404 |
bytes = write_from_buf_to_file(signed_rom_fd, align_data, align_bytes); |
| 405 |
if (bytes != align_bytes) { |
| 406 |
fprintf(stderr, "%s: failed to write alignment data for %s\n", |
| 407 |
__func__, fw_table[i].filename); |
| 408 |
lseek(signed_rom_fd, SEEK_CUR, -fd_stat.st_size); |
| 409 |
free(buf); |
| 410 |
close(fd); |
| 411 |
continue; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
if (add_sha(&fw_table[i], buf, fd_stat.st_size, soc_id)) |
| 416 |
exit(-1); |
| 417 |
|
| 418 |
|
| 419 |
fw_table[i].addr_signed = signed_start_addr; |
| 420 |
fw_table[i].file_size = (uint32_t)fd_stat.st_size; |
| 421 |
update_hash_files_config(&fw_table[i]); |
| 422 |
|
| 423 |
signed_start_addr += fd_stat.st_size + align_bytes; |
| 424 |
|
| 425 |
free(buf); |
| 426 |
close(fd); |
| 427 |
} |
| 428 |
|
| 429 |
close(signed_rom_fd); |
| 430 |
|
| 431 |
open_psp_fw_hash_files(signed_rom); |
| 432 |
write_psp_firmware_hash(fw_table); |
| 433 |
close_psp_fw_hash_files(); |
| 434 |
} |