Debian Codegraph @

util/mec152x/rom.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
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 /* Generate EC FW PTR. EC FW must be 256 byte aligned and has a CRC-8 checksum */
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 * @brief Write the EC FW pointer to the ROM
24 * @param rom_file The filename of the ROM to update
25 * The ROM must contain a valid FMAP
26 * @param fmap_region_name FMAP region name containing the EC firmware
27 * @return True on success
28 *
29 * On MEC15x1 the firmware resides in the shared x86 SPI flash. As the EC
30 * boots first and does the power sequencing there's no concurrent access.
31 * The EC expects to find a pointer to the FW at offset 0.
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 /* EC FW must be aligned */
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 /* EC FW pointer resides at offset 0 */
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 }