| 1 |
|
| 2 |
|
| 3 |
#include <assert.h> |
| 4 |
#include <commonlib/bsd/compiler.h> |
| 5 |
#include <endian.h> |
| 6 |
#include <fmap.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include "rom.h" |
| 9 |
#include "utils.h" |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
static uint64_t gen_signature(uint32_t rom_offset, uint32_t size) |
| 22 |
{ |
| 23 |
uint64_t signature = rom_offset; |
| 24 |
signature |= ((uint64_t)size & 0xffffffULL) << 32; |
| 25 |
uint8_t crc = 0; |
| 26 |
|
| 27 |
for (int i = 0; i < 7; i++) |
| 28 |
crc += (signature >> (i * 8)) & 0xff; |
| 29 |
|
| 30 |
signature |= (0xffULL - (uint64_t)crc) << 56; |
| 31 |
|
| 32 |
return signature; |
| 33 |
} |
| 34 |
|
| 35 |
static bool check_signature(uint64_t signature) |
| 36 |
{ |
| 37 |
uint8_t crc = 0; |
| 38 |
|
| 39 |
for (int i = 0; i < 8; i++) |
| 40 |
crc += (signature >> (i * 8)) & 0xff; |
| 41 |
|
| 42 |
return crc == 0xff; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
bool rom_set_pd_fw_ptr(const char rom_file[], const char *fmap_region_name, int slot, bool automatic) |
| 59 |
{ |
| 60 |
uint64_t *ec_pd_fw_ptr; |
| 61 |
struct mem_range file = {0}; |
| 62 |
bool ret = false; |
| 63 |
|
| 64 |
file = map_file(rom_file, true); |
| 65 |
if (file.start == NULL) { |
| 66 |
fprintf(stderr, "ERROR: Failed to load \"%s\"\n", rom_file); |
| 67 |
return false; |
| 68 |
} |
| 69 |
|
| 70 |
if (automatic) |
| 71 |
slot = 0; |
| 72 |
|
| 73 |
long fmap_offset = fmap_find(file.start, file.length); |
| 74 |
if (fmap_offset >= 0) { |
| 75 |
struct fmap *fmap = (void *)(file.start + fmap_offset); |
| 76 |
const struct fmap_area *area = fmap_find_area(fmap, fmap_region_name); |
| 77 |
if (area == NULL) { |
| 78 |
fprintf(stderr, |
| 79 |
"ERROR: Found FMAP without '%s' in \"%s\"\n\n", |
| 80 |
fmap_region_name, rom_file); |
| 81 |
printf("FMAP is:\n"); |
| 82 |
fmap_print(fmap); |
| 83 |
goto error; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
ec_pd_fw_ptr = (uint64_t *)(file.start + 0x10 + slot * 8); |
| 88 |
|
| 89 |
|
| 90 |
for (; automatic && check_signature(le64toh(*ec_pd_fw_ptr)) && slot < 12; slot++) |
| 91 |
ec_pd_fw_ptr++; |
| 92 |
|
| 93 |
if (slot == 12) { |
| 94 |
fprintf(stderr, "ERROR: No free slots left\n\n"); |
| 95 |
goto error; |
| 96 |
} |
| 97 |
|
| 98 |
*ec_pd_fw_ptr = htole64(gen_signature(area->offset, area->size)); |
| 99 |
} else { |
| 100 |
fprintf(stderr, "ERROR: FMAP not found \"%s\"\n", rom_file); |
| 101 |
goto error; |
| 102 |
} |
| 103 |
|
| 104 |
ret = true; |
| 105 |
error: |
| 106 |
unmap_file(file); |
| 107 |
return ret; |
| 108 |
} |