Debian Codegraph @

util/amdtools/ec_usb_pd_fw/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 /**
12 * @brief Generate pointer to PD firmware.
13 * @param rom_offset Offset of the PD firmware
14 * @param size Size of the PD firmware
15 *
16 * Each pointer is 8 byte wide consists of:
17 * - 4 byte offset
18 * - 3 byte size
19 * - 1 byte checksum (all 8 bytes sum up to 0xff)
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 * @brief Write the EC PD pointer to the ROM
47 * @param rom_file The filename of the ROM to update
48 * The ROM must contain a valid FMAP
49 * @param fmap_region_name FMAP region name containing the PD firmware
50 * @return True on success
51 *
52 * The EC firmware copies the USB Type-C PD firmware from the x86 SPI flash
53 * to the corresponding controller when necessary. It expects to find a list
54 * of pointers at offset 10h. The ROM can contain multiple PD firmwares depending
55 * on the used IC vendor/familiy.
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 /* EC PD pointer resides at offset 0x10 */
87 ec_pd_fw_ptr = (uint64_t *)(file.start + 0x10 + slot * 8);
88
89 /* Find free slot if in auto mode */
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 }