| 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 |
static uint32_t gen_signature(uint32_t rom_offset) |
| 13 |
{ |
| 14 |
uint32_t signature = rom_offset >> 8; |
| 15 |
uint8_t crc = crc8_itu((uint8_t *)&signature, 3); |
| 16 |
|
| 17 |
signature |= ((uint32_t)crc) << 24; |
| 18 |
|
| 19 |
return signature; |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
bool rom_set_ec_fw_ptr(const char rom_file[], const char *fmap_region_name) |
| 34 |
{ |
| 35 |
struct mem_range file = {0}; |
| 36 |
bool ret = false; |
| 37 |
|
| 38 |
file = map_file(rom_file, true); |
| 39 |
if (file.start == NULL) { |
| 40 |
fprintf(stderr, "ERROR: Failed to load \"%s\"\n", rom_file); |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
long fmap_offset = fmap_find(file.start, file.length); |
| 45 |
if (fmap_offset >= 0) { |
| 46 |
struct fmap *fmap = (void *)(file.start + fmap_offset); |
| 47 |
const struct fmap_area *area = fmap_find_area(fmap, fmap_region_name); |
| 48 |
if (area == NULL) { |
| 49 |
fprintf(stderr, |
| 50 |
"ERROR: Found FMAP without '%s' in \"%s\"\n\n", |
| 51 |
fmap_region_name, rom_file); |
| 52 |
printf("FMAP is:\n"); |
| 53 |
fmap_print(fmap); |
| 54 |
goto error; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
if (area->offset % 256) { |
| 59 |
fprintf(stderr, |
| 60 |
"ERROR: Region '%s' not aligned to 256 in \"%s\"\n", |
| 61 |
fmap_region_name, rom_file); |
| 62 |
goto error; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
uint32_t *ec_fw_ptr = (uint32_t *)file.start; |
| 67 |
*ec_fw_ptr = htole32(gen_signature(area->offset)); |
| 68 |
} else { |
| 69 |
fprintf(stderr, "ERROR: FMAP not found \"%s\"\n", rom_file); |
| 70 |
goto error; |
| 71 |
} |
| 72 |
|
| 73 |
ret = true; |
| 74 |
error: |
| 75 |
unmap_file(file); |
| 76 |
return ret; |
| 77 |
} |