| 1 |
|
| 2 |
|
| 3 |
#include <errno.h> |
| 4 |
#include <inttypes.h> |
| 5 |
#include <stdbool.h> |
| 6 |
#include <stdint.h> |
| 7 |
#include <stdio.h> |
| 8 |
#include <stdlib.h> |
| 9 |
#include <string.h> |
| 10 |
|
| 11 |
#include <dirent.h> |
| 12 |
#include <fcntl.h> |
| 13 |
#include <unistd.h> |
| 14 |
#include <sys/mman.h> |
| 15 |
#include <sys/stat.h> |
| 16 |
#include <sys/types.h> |
| 17 |
|
| 18 |
#if defined(__linux__) || defined(__ANDROID__) |
| 19 |
#include <linux/fs.h> |
| 20 |
#include <sys/ioctl.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
#include <commonlib/bsd/cbmem_id.h> |
| 24 |
#include <commonlib/bsd/ipchksum.h> |
| 25 |
#include <commonlib/coreboot_tables.h> |
| 26 |
#include <commonlib/helpers.h> |
| 27 |
#include <commonlib/timestamp_serialized.h> |
| 28 |
#include <commonlib/tpm_log_serialized.h> |
| 29 |
|
| 30 |
#include "cbmem_util.h" |
| 31 |
|
| 32 |
#define CBMEM_SYSFS_BASE_DIR "/sys/bus/coreboot/devices/" |
| 33 |
#define CBMEM_SYSFS_ENTRY_DIR_NAME_PREFIX "cbmem-" |
| 34 |
#define CBMEM_SYSFS_ENTRY_DIR_NAME_SCN_FMT CBMEM_SYSFS_ENTRY_DIR_NAME_PREFIX "%8" SCNx32 |
| 35 |
#define CBMEM_SYSFS_ENTRY_DIR_NAME_PRI_FMT CBMEM_SYSFS_ENTRY_DIR_NAME_PREFIX "%08" PRIx32 |
| 36 |
#define CBMEM_SYSFS_ENTRY_DIR_PATH_PRI_FMT \ |
| 37 |
CBMEM_SYSFS_BASE_DIR CBMEM_SYSFS_ENTRY_DIR_NAME_PRI_FMT |
| 38 |
|
| 39 |
enum cbmem_sysfs_path_type { |
| 40 |
CBMEM_SYSFS_PATH_BASE, |
| 41 |
CBMEM_SYSFS_PATH_ADDRESS, |
| 42 |
CBMEM_SYSFS_PATH_MEM, |
| 43 |
CBMEM_SYSFS_PATH_SIZE, |
| 44 |
}; |
| 45 |
|
| 46 |
|
| 47 |
#define PATH_MAX_LEN 1024 |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
static void new_sysfs_path(uint32_t id, enum cbmem_sysfs_path_type t, char *path_out, size_t path_out_max_size) |
| 57 |
{ |
| 58 |
const char *const type2str[] = { |
| 59 |
[CBMEM_SYSFS_PATH_BASE] = "", |
| 60 |
[CBMEM_SYSFS_PATH_ADDRESS] = "/address", |
| 61 |
[CBMEM_SYSFS_PATH_MEM] = "/mem", |
| 62 |
[CBMEM_SYSFS_PATH_SIZE] = "/size" |
| 63 |
}; |
| 64 |
|
| 65 |
if (t >= ARRAY_SIZE(type2str)) |
| 66 |
die("Incorrect path type requested: %d\n", t); |
| 67 |
|
| 68 |
if (snprintf(path_out, path_out_max_size, CBMEM_SYSFS_ENTRY_DIR_PATH_PRI_FMT "%s", id, |
| 69 |
type2str[t]) <= 0) |
| 70 |
die("Unable to create sysfs path string for coreboot table: %#" PRIx32 |
| 71 |
". Path type: %d. Error: %s\n", |
| 72 |
id, t, strerror(errno)); |
| 73 |
} |
| 74 |
|
| 75 |
bool cbmem_sysfs_init(void) |
| 76 |
{ |
| 77 |
char path[PATH_MAX_LEN]; |
| 78 |
|
| 79 |
for (enum cbmem_sysfs_path_type t = CBMEM_SYSFS_PATH_ADDRESS; t <= CBMEM_SYSFS_PATH_SIZE; ++t) { |
| 80 |
new_sysfs_path(CBMEM_ID_CBTABLE, t, path, sizeof(path)); |
| 81 |
|
| 82 |
FILE *f = fopen(path, "rb"); |
| 83 |
if (!f) { |
| 84 |
debug("Unable to open path %s for reading. Error: %s\n", path, strerror(errno)); |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
uint8_t test_byte = 0; |
| 89 |
if (fread(&test_byte, 1, 1, f) != 1) { |
| 90 |
debug("Unable to read data from %s. Error: %s\n", path, strerror(errno)); |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
fclose(f); |
| 95 |
} |
| 96 |
return true; |
| 97 |
} |
| 98 |
|
| 99 |
static bool cbmem_sysfs_probe_cbmem_entry(uint32_t id, uint64_t *addr_out, size_t *size_out) |
| 100 |
{ |
| 101 |
char path[PATH_MAX_LEN]; |
| 102 |
|
| 103 |
new_sysfs_path(id, CBMEM_SYSFS_PATH_ADDRESS, path, sizeof(path)); |
| 104 |
|
| 105 |
FILE *address_file = fopen(path, "rb"); |
| 106 |
if (!address_file) { |
| 107 |
debug("Unable to access CBMEM entry id: %#" PRIx32 |
| 108 |
" address file at %s. Error: %s\n", |
| 109 |
id, path, strerror(errno)); |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
if (fscanf(address_file, "%" SCNx64, addr_out) != 1) { |
| 114 |
debug("Read from %s failed.\n", path); |
| 115 |
fclose(address_file); |
| 116 |
return false; |
| 117 |
} |
| 118 |
fclose(address_file); |
| 119 |
|
| 120 |
new_sysfs_path(id, CBMEM_SYSFS_PATH_SIZE, path, sizeof(path)); |
| 121 |
|
| 122 |
FILE *size_file = fopen(path, "rb"); |
| 123 |
if (!size_file) { |
| 124 |
debug("Unable to access CBMEM entry id: %#" PRIx32 |
| 125 |
" size file at %s. Error: %s\n", |
| 126 |
id, path, strerror(errno)); |
| 127 |
return false; |
| 128 |
} |
| 129 |
|
| 130 |
if (fscanf(size_file, "%zx", size_out) != 1) { |
| 131 |
debug("Read from %s failed.\n", path); |
| 132 |
fclose(size_file); |
| 133 |
*addr_out = 0; |
| 134 |
return false; |
| 135 |
} |
| 136 |
fclose(size_file); |
| 137 |
|
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
static void fetch_cbmem_entry(const uint32_t id, const size_t size, uint8_t **buf_out) |
| 142 |
{ |
| 143 |
char path[PATH_MAX_LEN]; |
| 144 |
|
| 145 |
new_sysfs_path(id, CBMEM_SYSFS_PATH_MEM, path, sizeof(path)); |
| 146 |
|
| 147 |
FILE *mem_file = fopen(path, "rb"); |
| 148 |
if (!mem_file) |
| 149 |
die("Unable to open mem file for CBMEM entry id: %#" PRIx32 |
| 150 |
" at %s. Error: %s\n", |
| 151 |
id, path, strerror(errno)); |
| 152 |
|
| 153 |
*buf_out = malloc(size); |
| 154 |
if (!buf_out) |
| 155 |
die("Unable to allocate memory for CBMEM entry id: %#" PRIx32 |
| 156 |
" of size: %zuB.\n", |
| 157 |
id, size); |
| 158 |
|
| 159 |
if (fread(*buf_out, 1, size, mem_file) != size) |
| 160 |
die("Unable to correctly read memory of CBMEM entry id: %#" PRIx32 |
| 161 |
" at %s. Error: %s\n", |
| 162 |
id, path, strerror(errno)); |
| 163 |
|
| 164 |
fclose(mem_file); |
| 165 |
} |
| 166 |
|
| 167 |
bool cbmem_sysfs_get_cbmem_entry(uint32_t id, uint8_t **buf_out, size_t *size_out, uint64_t *addr_out) |
| 168 |
{ |
| 169 |
uint64_t addr; |
| 170 |
size_t size; |
| 171 |
|
| 172 |
if (!cbmem_sysfs_probe_cbmem_entry(id, &addr, &size)) { |
| 173 |
debug("CBMEM entry id: %#" PRIx32 " not found.\n", id); |
| 174 |
return false; |
| 175 |
} |
| 176 |
|
| 177 |
fetch_cbmem_entry(id, size, buf_out); |
| 178 |
if (size_out) |
| 179 |
*size_out = size; |
| 180 |
if (addr_out) |
| 181 |
*addr_out = addr; |
| 182 |
return true; |
| 183 |
} |
| 184 |
|
| 185 |
bool cbmem_sysfs_write_cbmem_entry(uint32_t id, uint8_t *buf, size_t buf_size) |
| 186 |
{ |
| 187 |
char path[PATH_MAX_LEN]; |
| 188 |
uint64_t addr; |
| 189 |
size_t size; |
| 190 |
|
| 191 |
if (!cbmem_sysfs_probe_cbmem_entry(id, &addr, &size)) { |
| 192 |
debug("Unable to find CBMEM entry id: %#" PRIx32 "\n", id); |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
if (buf_size > size) |
| 197 |
die("Attempting to write %zu bytes to CBMEM entry id: %#" PRIx32 |
| 198 |
" of %zu bytes. Operation not possible.\n", |
| 199 |
buf_size, id, size); |
| 200 |
|
| 201 |
new_sysfs_path(id, CBMEM_SYSFS_PATH_MEM, path, sizeof(path)); |
| 202 |
|
| 203 |
FILE *mem_file = fopen(path, "rb+"); |
| 204 |
if (!mem_file) |
| 205 |
die("Unable to open mem file for CBMEM entry id: %#" PRIx32 |
| 206 |
" at %s. Error: %s\n", |
| 207 |
id, path, strerror(errno)); |
| 208 |
|
| 209 |
if (fwrite(buf, 1, buf_size, mem_file) != buf_size) |
| 210 |
die("Unable to correctly write memory of CBMEM entry id: %#" PRIx32 |
| 211 |
" at %s. Error: %s\n", |
| 212 |
id, path, strerror(errno)); |
| 213 |
|
| 214 |
fclose(mem_file); |
| 215 |
|
| 216 |
return true; |
| 217 |
} |
| 218 |
|
| 219 |
void cbmem_sysfs_foreach_cbmem_entry(cbmem_iterator_callback cb, void *data, bool with_contents) |
| 220 |
{ |
| 221 |
DIR *dir; |
| 222 |
struct dirent *entry; |
| 223 |
|
| 224 |
if ((dir = opendir(CBMEM_SYSFS_BASE_DIR)) == NULL) |
| 225 |
die("Unable to open directory containing CBMEM entries. Path: %s. Error: %s\n", |
| 226 |
CBMEM_SYSFS_BASE_DIR, strerror(errno)); |
| 227 |
|
| 228 |
while ((entry = readdir(dir)) != NULL) { |
| 229 |
debug("Checking path: %s%s\n", CBMEM_SYSFS_BASE_DIR, entry->d_name); |
| 230 |
|
| 231 |
if (strncmp(entry->d_name, CBMEM_SYSFS_ENTRY_DIR_NAME_PREFIX, |
| 232 |
strlen(CBMEM_SYSFS_ENTRY_DIR_NAME_PREFIX)) != 0) |
| 233 |
continue; |
| 234 |
|
| 235 |
uint32_t id = 0; |
| 236 |
|
| 237 |
|
| 238 |
if (sscanf(entry->d_name, CBMEM_SYSFS_ENTRY_DIR_NAME_SCN_FMT, &id) != 1) |
| 239 |
continue; |
| 240 |
|
| 241 |
uint64_t addr = 0; |
| 242 |
size_t size = 0; |
| 243 |
uint8_t *buf; |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
if (!cbmem_sysfs_probe_cbmem_entry(id, &addr, &size)) |
| 248 |
continue; |
| 249 |
|
| 250 |
if (with_contents) |
| 251 |
fetch_cbmem_entry(id, size, &buf); |
| 252 |
|
| 253 |
debug("Invoking callback on %s%s\n", CBMEM_SYSFS_BASE_DIR, entry->d_name); |
| 254 |
|
| 255 |
const bool res = cb(id, addr, buf, size, data); |
| 256 |
|
| 257 |
if (with_contents) |
| 258 |
free(buf); |
| 259 |
|
| 260 |
|
| 261 |
if (res) |
| 262 |
break; |
| 263 |
} |
| 264 |
|
| 265 |
closedir(dir); |
| 266 |
} |