Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | #include <tests/test.h> |
| 4 | #include <boardid.h> |
| 5 | #include <boot/coreboot_tables.h> |
| 6 | #include <boot/tables.h> |
| 7 | #include <cbfs.h> |
| 8 | #include <cbmem.h> |
| 9 | #include <commonlib/helpers.h> |
| 10 | #include <commonlib/region.h> |
| 11 | #include <fmap_config.h> |
| 12 | #include <fw_config.h> |
| 13 | #include <stdbool.h> |
| 14 | #include <version.h> |
| 15 | |
| 16 | |
| 17 | /* Copy of lb_table_init() implementation for testing purposes */ |
| 18 | static struct lb_header *lb_table_init(unsigned long addr) |
| 19 | { |
| 20 | struct lb_header *header; |
| 21 | |
| 22 | /* 16 byte align the address */ |
Elyes Haouas | f25c112 | 2022-09-11 12:18:46 +0200 | [diff] [blame] | 23 | addr = ALIGN_UP(addr, 16); |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 24 | |
| 25 | header = (void *)addr; |
| 26 | header->signature[0] = 'L'; |
| 27 | header->signature[1] = 'B'; |
| 28 | header->signature[2] = 'I'; |
| 29 | header->signature[3] = 'O'; |
| 30 | header->header_bytes = sizeof(*header); |
| 31 | header->header_checksum = 0; |
| 32 | header->table_bytes = 0; |
| 33 | header->table_checksum = 0; |
| 34 | header->table_entries = 0; |
| 35 | return header; |
| 36 | } |
| 37 | |
| 38 | static struct lb_record *lb_first_record(struct lb_header *header) |
| 39 | { |
| 40 | struct lb_record *rec; |
| 41 | rec = (void *)(((char *)header) + sizeof(*header)); |
| 42 | return rec; |
| 43 | } |
| 44 | |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 45 | #define LB_RECORD_FOR_EACH(record_ptr, index, header) \ |
| 46 | for (index = 0, record_ptr = lb_first_record(header); index < header->table_entries; \ |
| 47 | record_ptr = (struct lb_record *)((uintptr_t)record_ptr + record_ptr->size), \ |
| 48 | index++) |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 49 | |
| 50 | static void test_lb_add_gpios(void **state) |
| 51 | { |
| 52 | struct lb_gpio gpios[] = { |
| 53 | {-1, ACTIVE_HIGH, 1, "lid"}, |
| 54 | {-1, ACTIVE_HIGH, 0, "power"}, |
| 55 | {-1, ACTIVE_HIGH, 1, "oprom"}, |
| 56 | {-1, ACTIVE_HIGH, 0, "EC in RW"}, |
| 57 | }; |
| 58 | const size_t gpios_buf_size = sizeof(struct lb_gpios) + sizeof(struct lb_gpio) * 32; |
| 59 | uint8_t gpios_buf[gpios_buf_size]; |
| 60 | struct lb_gpios *gpios_table = (struct lb_gpios *)gpios_buf; |
| 61 | gpios_table->count = 0; |
| 62 | gpios_table->size = 0; |
| 63 | gpios_table->tag = LB_TAG_GPIO; |
| 64 | |
| 65 | /* Add GPIOs an check if they have been added to the table. |
| 66 | GPIOs are added in the same order to the end of the table. */ |
| 67 | lb_add_gpios(gpios_table, gpios, ARRAY_SIZE(gpios)); |
| 68 | assert_int_equal(ARRAY_SIZE(gpios), gpios_table->count); |
| 69 | assert_int_equal(sizeof(gpios), gpios_table->size); |
| 70 | assert_memory_equal(&gpios_table->gpios[0], gpios, sizeof(gpios)); |
| 71 | |
| 72 | /* Add subset of gpios and check if they have been added correctly. */ |
| 73 | lb_add_gpios(gpios_table, &gpios[1], 2); |
| 74 | assert_int_equal(ARRAY_SIZE(gpios) + 2, gpios_table->count); |
| 75 | assert_int_equal(sizeof(gpios) + 2 * sizeof(gpios[0]), gpios_table->size); |
| 76 | assert_memory_equal(&gpios_table->gpios[0], gpios, sizeof(gpios)); |
| 77 | assert_memory_equal(&gpios_table->gpios[ARRAY_SIZE(gpios)], &gpios[1], |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 78 | 2 * sizeof(gpios[0])); |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | uint8_t tables_buffer[sizeof(struct lb_header) + 10 * KiB]; |
| 82 | static int setup_test_header(void **state) |
| 83 | { |
| 84 | *state = lb_table_init((uintptr_t)tables_buffer); |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | static void test_lb_new_record(void **state) |
| 90 | { |
| 91 | struct lb_header *header = *state; |
| 92 | const size_t entries = 10; |
| 93 | int i; |
| 94 | size_t entries_offset; |
| 95 | size_t accumulated_size = 0; |
| 96 | struct lb_record *curr; |
| 97 | |
| 98 | |
| 99 | assert_int_equal(0, header->table_entries); |
| 100 | assert_int_equal(0, header->table_bytes); |
| 101 | |
| 102 | lb_new_record(header); |
| 103 | assert_int_equal(1, header->table_entries); |
| 104 | assert_int_equal(0, header->table_bytes); |
| 105 | |
| 106 | /* Create few entries with varying sizes (but at least of sizeof(struct lb_record)) |
| 107 | Accumulate and check size of table after each lb_new_record() call. */ |
| 108 | entries_offset = header->table_entries; |
| 109 | accumulated_size = sizeof(struct lb_record); |
| 110 | for (i = 0; i < entries; ++i) { |
| 111 | curr = lb_new_record(header); |
| 112 | curr->size = sizeof(struct lb_record) + ((i + 2) * 7) % 32; |
| 113 | |
| 114 | assert_int_equal(entries_offset + (i + 1), header->table_entries); |
| 115 | assert_int_equal(accumulated_size, header->table_bytes); |
| 116 | accumulated_size += curr->size; |
| 117 | } |
| 118 | } |
| 119 | |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 120 | static void test_lb_add_console(void **state) |
| 121 | { |
| 122 | struct lb_header *header = *state; |
| 123 | |
| 124 | lb_add_console(LB_TAG_CONSOLE_SERIAL8250MEM, header); |
| 125 | assert_int_equal(1, header->table_entries); |
| 126 | /* Table bytes and checksum should be zero, because it is updated with size of previous |
| 127 | record or when table is closed. No previous record is present. */ |
| 128 | assert_int_equal(0, header->table_bytes); |
| 129 | assert_int_equal(0, header->table_checksum); |
| 130 | } |
| 131 | |
| 132 | static void test_multiple_entries(void **state) |
| 133 | { |
| 134 | struct lb_header *header = *state; |
| 135 | |
| 136 | /* Add two entries */ |
| 137 | lb_add_console(LB_TAG_CONSOLE_SERIAL8250, header); |
| 138 | lb_add_console(LB_TAG_CONSOLE_SERIAL8250MEM, header); |
| 139 | |
| 140 | assert_int_equal(2, header->table_entries); |
| 141 | assert_int_equal(sizeof(struct lb_console), header->table_bytes); |
| 142 | } |
| 143 | |
| 144 | static void test_write_coreboot_forwarding_table(void **state) |
| 145 | { |
| 146 | struct lb_header *header = *state; |
| 147 | uint8_t forwarding_table_buffer[sizeof(struct lb_header) |
| 148 | + 2 * sizeof(struct lb_forward)]; |
| 149 | struct lb_header *forward_header = |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 150 | (struct lb_header *)ALIGN_UP((uintptr_t)forwarding_table_buffer, 16); |
| 151 | size_t forwarding_table_size = write_coreboot_forwarding_table( |
| 152 | (uintptr_t)forwarding_table_buffer, (uintptr_t)header); |
| 153 | size_t expected_forwarding_table_size = |
| 154 | ALIGN_UP((uintptr_t)forwarding_table_buffer, 16) + sizeof(struct lb_header) |
| 155 | + sizeof(struct lb_forward) - (uintptr_t)forwarding_table_buffer; |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 156 | assert_int_equal(expected_forwarding_table_size, forwarding_table_size); |
| 157 | |
| 158 | assert_int_equal(1, forward_header->table_entries); |
| 159 | assert_int_equal(sizeof(struct lb_forward), forward_header->table_bytes); |
| 160 | assert_ptr_equal(header, |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 161 | ((struct lb_forward *)lb_first_record(forward_header))->forward); |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* Mocks for write_tables() */ |
| 165 | const char mainboard_vendor[] = CONFIG_MAINBOARD_VENDOR; |
| 166 | const char mainboard_part_number[] = CONFIG_MAINBOARD_PART_NUMBER; |
| 167 | |
| 168 | const char coreboot_version[] = "4.13"; |
| 169 | const char coreboot_extra_version[] = "abcdef"; |
| 170 | const char coreboot_build[] = "Coreboot build info"; |
| 171 | const unsigned int coreboot_version_timestamp = 1617191902U; |
| 172 | const unsigned int coreboot_major_revision = 4; |
| 173 | const unsigned int coreboot_minor_revision = 13; |
| 174 | |
| 175 | const char coreboot_compile_time[] = "13:58:22"; |
| 176 | const char coreboot_dmi_date[] = "03/31/2021"; |
| 177 | |
| 178 | const struct bcd_date coreboot_build_date = { |
| 179 | .century = 0x20, |
| 180 | .year = 0x20, |
| 181 | .month = 0x03, |
| 182 | .day = 0x31, |
| 183 | .weekday = 0x2, |
| 184 | }; |
| 185 | |
| 186 | const unsigned int asl_revision = 0x20200925; |
| 187 | |
| 188 | void arch_write_tables(uintptr_t coreboot_table) |
| 189 | { |
| 190 | } |
| 191 | |
Arthur Heymans | 2e7e2d9 | 2022-03-03 22:28:27 +0100 | [diff] [blame] | 192 | static const uintptr_t ebda_base = 0xf0000; |
| 193 | uintptr_t get_coreboot_rsdp(void) |
| 194 | { |
| 195 | return ebda_base; |
| 196 | } |
| 197 | |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 198 | struct resource mock_bootmem_ranges[] = { |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 199 | {.base = 0x1000, .size = 0x2000, .flags = LB_MEM_RAM}, |
| 200 | {.base = 0x0000, .size = 0x4000, .flags = LB_MEM_RAM}, |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | void bootmem_write_memory_table(struct lb_memory *mem) |
| 204 | { |
| 205 | struct lb_memory_range *lb_r = &mem->map[0]; |
| 206 | int i; |
| 207 | |
| 208 | /* Insert entries for testing */ |
| 209 | for (i = 0; i < ARRAY_SIZE(mock_bootmem_ranges); ++i) { |
| 210 | struct resource *res = &mock_bootmem_ranges[i]; |
Jianjun Wang | b2537bd | 2022-04-08 16:57:28 +0800 | [diff] [blame] | 211 | lb_r->start = res->base; |
| 212 | lb_r->size = res->size; |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 213 | lb_r->type = res->flags; |
| 214 | lb_r++; |
| 215 | mem->size += sizeof(struct lb_memory_range); |
| 216 | } |
| 217 | } |
| 218 | |
Arthur Heymans | 9948c52 | 2022-10-24 14:37:40 +0200 | [diff] [blame^] | 219 | enum cb_err fill_lb_serial(struct lb_serial *serial) |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 220 | { |
Arthur Heymans | 9948c52 | 2022-10-24 14:37:40 +0200 | [diff] [blame^] | 221 | serial->type = LB_SERIAL_TYPE_MEMORY_MAPPED; |
| 222 | serial->baseaddr = 0xFEDC6000; |
| 223 | serial->baud = 115200; |
| 224 | serial->regwidth = 1; |
| 225 | serial->input_hertz = 115200 * 16; |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 226 | |
Arthur Heymans | 9948c52 | 2022-10-24 14:37:40 +0200 | [diff] [blame^] | 227 | return CB_SUCCESS; |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | struct cbfs_boot_device cbfs_boot_dev = { |
| 231 | .rdev = REGION_DEV_INIT(NULL, 0, 0x1000), |
| 232 | .mcache = (void *)0x1000, |
| 233 | .mcache_size = 0x1000, |
| 234 | }; |
| 235 | |
| 236 | const struct cbfs_boot_device *cbfs_get_boot_device(bool force_ro) |
| 237 | { |
| 238 | return &cbfs_boot_dev; |
| 239 | } |
| 240 | |
| 241 | void cbmem_run_init_hooks(int is_recovery) |
| 242 | { |
| 243 | } |
| 244 | |
| 245 | extern uintptr_t _cbmem_top_ptr; |
Patrick Georgi | ce55ca2 | 2021-06-09 18:37:42 +0200 | [diff] [blame] | 246 | void *cbmem_top_chipset(void) |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 247 | { |
| 248 | return (void *)_cbmem_top_ptr; |
| 249 | } |
| 250 | |
| 251 | #define CBMEM_SIZE (64 * KiB) |
| 252 | |
| 253 | static int teardown_write_tables_test(void **state) |
| 254 | { |
| 255 | free(*state); |
| 256 | _cbmem_top_ptr = 0; |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | static int setup_write_tables_test(void **state) |
| 261 | { |
| 262 | /* Allocate more data to have space for alignment */ |
| 263 | void *top_ptr = malloc(CBMEM_SIZE + DYN_CBMEM_ALIGN_SIZE); |
| 264 | int32_t *mmc_status = NULL; |
| 265 | |
| 266 | if (!top_ptr) |
| 267 | return -1; |
| 268 | |
| 269 | *state = top_ptr; |
| 270 | |
| 271 | _cbmem_top_ptr = ALIGN_UP((uintptr_t)top_ptr + CBMEM_SIZE, DYN_CBMEM_ALIGN_SIZE); |
| 272 | |
| 273 | cbmem_initialize_empty(); |
| 274 | |
| 275 | mmc_status = cbmem_add(CBMEM_ID_MMC_STATUS, sizeof(int32_t)); |
| 276 | |
| 277 | if (mmc_status == NULL) { |
| 278 | teardown_write_tables_test(state); |
| 279 | return -1; |
| 280 | } |
| 281 | |
| 282 | *mmc_status = 0x4433AADD; |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | const struct region_device *boot_device_ro(void) |
| 288 | { |
| 289 | return &cbfs_boot_dev.rdev; |
| 290 | } |
| 291 | |
| 292 | uint64_t get_fmap_flash_offset(void) |
| 293 | { |
| 294 | return FMAP_OFFSET; |
| 295 | } |
| 296 | |
| 297 | uint32_t freq_khz = 5000 * 1000; |
| 298 | void lb_arch_add_records(struct lb_header *header) |
| 299 | { |
| 300 | struct lb_tsc_info *tsc_info; |
| 301 | |
| 302 | tsc_info = (void *)lb_new_record(header); |
| 303 | tsc_info->tag = LB_TAG_TSC_INFO; |
| 304 | tsc_info->size = sizeof(*tsc_info); |
| 305 | tsc_info->freq_khz = freq_khz; |
| 306 | } |
| 307 | |
| 308 | static void test_write_tables(void **state) |
| 309 | { |
| 310 | void *cbtable_start; |
| 311 | struct lb_header *header; |
| 312 | struct lb_record *record; |
| 313 | int32_t *mmc_status = cbmem_find(CBMEM_ID_MMC_STATUS); |
| 314 | size_t i = 0; |
| 315 | |
| 316 | /* Expect function to store cbtable entry in cbmem */ |
| 317 | cbtable_start = write_tables(); |
| 318 | assert_ptr_equal(cbtable_start, cbmem_find(CBMEM_ID_CBTABLE)); |
| 319 | |
| 320 | /* Expect correct lb_header at cbtable_start address */ |
| 321 | header = (struct lb_header *)cbtable_start; |
| 322 | assert_non_null(header); |
| 323 | assert_memory_equal("LBIO", header, 4); |
| 324 | assert_int_equal(sizeof(*header), header->header_bytes); |
| 325 | /* At least one entry should be present. */ |
| 326 | assert_int_not_equal(0, header->table_entries); |
| 327 | |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 328 | LB_RECORD_FOR_EACH(record, i, header) |
| 329 | { |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 330 | switch (record->tag) { |
| 331 | case LB_TAG_MEMORY: |
| 332 | /* Should be the same as in bootmem_write_memory_table() */ |
| 333 | assert_int_equal(sizeof(struct lb_memory) |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 334 | + ARRAY_SIZE(mock_bootmem_ranges) |
| 335 | * sizeof(struct lb_memory_range), |
| 336 | record->size); |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 337 | |
| 338 | const struct lb_memory *memory = (struct lb_memory *)record; |
| 339 | const struct lb_memory_range *range; |
| 340 | const struct resource *res; |
Jianjun Wang | b2537bd | 2022-04-08 16:57:28 +0800 | [diff] [blame] | 341 | lb_uint64_t value; |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 342 | |
| 343 | for (int i = 0; i < ARRAY_SIZE(mock_bootmem_ranges); ++i) { |
| 344 | res = &mock_bootmem_ranges[i]; |
| 345 | range = &memory->map[i]; |
| 346 | |
Jianjun Wang | b2537bd | 2022-04-08 16:57:28 +0800 | [diff] [blame] | 347 | value = res->base; |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 348 | assert_memory_equal(&value, &range->start, |
Jianjun Wang | b2537bd | 2022-04-08 16:57:28 +0800 | [diff] [blame] | 349 | sizeof(lb_uint64_t)); |
| 350 | value = res->size; |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 351 | assert_memory_equal(&value, &range->size, |
Jianjun Wang | b2537bd | 2022-04-08 16:57:28 +0800 | [diff] [blame] | 352 | sizeof(lb_uint64_t)); |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 353 | assert_int_equal(range->type, res->flags); |
| 354 | } |
| 355 | break; |
| 356 | case LB_TAG_MAINBOARD: |
| 357 | /* Mainboard record contains its header followed |
| 358 | by two null-terminated strings */ |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 359 | assert_int_equal(ALIGN_UP(sizeof(struct lb_mainboard) |
| 360 | + ARRAY_SIZE(mainboard_vendor) |
| 361 | + ARRAY_SIZE(mainboard_part_number), |
| 362 | 8), |
| 363 | record->size); |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 364 | break; |
| 365 | case LB_TAG_VERSION: |
| 366 | assert_int_equal(ALIGN_UP(sizeof(struct lb_string) |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 367 | + ARRAY_SIZE(coreboot_version), |
| 368 | 8), |
| 369 | record->size); |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 370 | break; |
| 371 | case LB_TAG_EXTRA_VERSION: |
| 372 | assert_int_equal(ALIGN_UP(sizeof(struct lb_string) |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 373 | + ARRAY_SIZE(coreboot_extra_version), |
| 374 | 8), |
| 375 | record->size); |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 376 | break; |
| 377 | case LB_TAG_BUILD: |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 378 | assert_int_equal( |
| 379 | ALIGN_UP(sizeof(struct lb_string) + ARRAY_SIZE(coreboot_build), |
| 380 | 8), |
| 381 | record->size); |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 382 | break; |
| 383 | case LB_TAG_COMPILE_TIME: |
| 384 | assert_int_equal(ALIGN_UP(sizeof(struct lb_string) |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 385 | + ARRAY_SIZE(coreboot_compile_time), |
| 386 | 8), |
| 387 | record->size); |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 388 | break; |
| 389 | case LB_TAG_SERIAL: |
| 390 | assert_int_equal(sizeof(struct lb_serial), record->size); |
| 391 | |
| 392 | /* This struct have the same values as created in uart_fill_lb() */ |
| 393 | const struct lb_serial *serial = (struct lb_serial *)record; |
| 394 | assert_int_equal(LB_SERIAL_TYPE_MEMORY_MAPPED, serial->type); |
| 395 | assert_int_equal(0xFEDC6000, serial->baseaddr); |
| 396 | assert_int_equal(115200, serial->baud); |
| 397 | assert_int_equal(1, serial->regwidth); |
| 398 | assert_int_equal(115200 * 16, serial->input_hertz); |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 399 | break; |
| 400 | case LB_TAG_CONSOLE: |
| 401 | assert_int_equal(sizeof(struct lb_console), record->size); |
| 402 | |
| 403 | /* This struct have the same values as created in uart_fill_lb() */ |
| 404 | const struct lb_console *console = (struct lb_console *)record; |
| 405 | assert_int_equal(LB_TAG_CONSOLE_SERIAL8250MEM, console->type); |
| 406 | break; |
| 407 | case LB_TAG_VERSION_TIMESTAMP: |
| 408 | assert_int_equal(sizeof(struct lb_timestamp), record->size); |
| 409 | |
| 410 | const struct lb_timestamp *timestamp = (struct lb_timestamp *)record; |
| 411 | assert_int_equal(coreboot_version_timestamp, timestamp->timestamp); |
| 412 | break; |
| 413 | case LB_TAG_BOOT_MEDIA_PARAMS: |
| 414 | assert_int_equal(sizeof(struct lb_boot_media_params), record->size); |
| 415 | |
| 416 | const struct lb_boot_media_params *bmp = |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 417 | (struct lb_boot_media_params *)record; |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 418 | const struct cbfs_boot_device *cbd = cbfs_get_boot_device(false); |
| 419 | const struct region_device *boot_dev = boot_device_ro(); |
| 420 | assert_int_equal(region_device_offset(&cbd->rdev), bmp->cbfs_offset); |
| 421 | assert_int_equal(region_device_sz(&cbd->rdev), bmp->cbfs_size); |
| 422 | assert_int_equal(region_device_sz(boot_dev), bmp->boot_media_size); |
| 423 | assert_int_equal(get_fmap_flash_offset(), bmp->fmap_offset); |
| 424 | |
| 425 | break; |
| 426 | case LB_TAG_CBMEM_ENTRY: |
| 427 | assert_int_equal(sizeof(struct lb_cbmem_entry), record->size); |
| 428 | |
| 429 | const struct lb_cbmem_entry *cbmem_entry = |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 430 | (struct lb_cbmem_entry *)record; |
| 431 | const LargestIntegralType expected_tags[] = {CBMEM_ID_CBTABLE, |
| 432 | CBMEM_ID_MMC_STATUS}; |
| 433 | assert_in_set(cbmem_entry->id, expected_tags, |
| 434 | ARRAY_SIZE(expected_tags)); |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 435 | break; |
| 436 | case LB_TAG_TSC_INFO: |
| 437 | assert_int_equal(sizeof(struct lb_tsc_info), record->size); |
| 438 | |
| 439 | const struct lb_tsc_info *tsc_info = (struct lb_tsc_info *)record; |
| 440 | assert_int_equal(freq_khz, tsc_info->freq_khz); |
| 441 | break; |
| 442 | case LB_TAG_MMC_INFO: |
| 443 | assert_int_equal(sizeof(struct lb_mmc_info), record->size); |
| 444 | |
| 445 | const struct lb_mmc_info *mmc_info = (struct lb_mmc_info *)record; |
| 446 | assert_int_equal(*mmc_status, mmc_info->early_cmd1_status); |
| 447 | break; |
| 448 | case LB_TAG_BOARD_CONFIG: |
| 449 | assert_int_equal(sizeof(struct lb_board_config), record->size); |
| 450 | |
| 451 | const struct lb_board_config *board_config = |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 452 | (struct lb_board_config *)record; |
Jianjun Wang | b2537bd | 2022-04-08 16:57:28 +0800 | [diff] [blame] | 453 | const lb_uint64_t expected_fw_version = fw_config_get(); |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 454 | assert_memory_equal(&expected_fw_version, &board_config->fw_config, |
Jianjun Wang | b2537bd | 2022-04-08 16:57:28 +0800 | [diff] [blame] | 455 | sizeof(lb_uint64_t)); |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 456 | assert_int_equal(board_id(), board_config->board_id); |
| 457 | assert_int_equal(ram_code(), board_config->ram_code); |
| 458 | assert_int_equal(sku_id(), board_config->sku_id); |
| 459 | break; |
Arthur Heymans | 2e7e2d9 | 2022-03-03 22:28:27 +0100 | [diff] [blame] | 460 | case LB_TAG_ACPI_RSDP: |
| 461 | assert_int_equal(sizeof(struct lb_acpi_rsdp), record->size); |
| 462 | |
| 463 | const struct lb_acpi_rsdp *acpi_rsdp = (struct lb_acpi_rsdp *)record; |
Jianjun Wang | b2537bd | 2022-04-08 16:57:28 +0800 | [diff] [blame] | 464 | assert_int_equal(ebda_base, acpi_rsdp->rsdp_pointer); |
Arthur Heymans | 2e7e2d9 | 2022-03-03 22:28:27 +0100 | [diff] [blame] | 465 | break; |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 466 | default: |
| 467 | fail_msg("Unexpected tag found in record. Tag ID: 0x%x", record->tag); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | int main(void) |
| 473 | { |
| 474 | const struct CMUnitTest tests[] = { |
| 475 | cmocka_unit_test(test_lb_add_gpios), |
| 476 | cmocka_unit_test_setup(test_lb_new_record, setup_test_header), |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 477 | cmocka_unit_test_setup(test_lb_add_console, setup_test_header), |
| 478 | cmocka_unit_test_setup(test_multiple_entries, setup_test_header), |
| 479 | cmocka_unit_test_setup(test_write_coreboot_forwarding_table, setup_test_header), |
Jakub Czapiga | c08b6a7 | 2022-01-10 13:36:47 +0000 | [diff] [blame] | 480 | cmocka_unit_test_setup_teardown(test_write_tables, setup_write_tables_test, |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 481 | teardown_write_tables_test), |
| 482 | }; |
| 483 | |
Jakub Czapiga | 7c6081e | 2021-08-25 16:27:35 +0200 | [diff] [blame] | 484 | return cb_run_group_tests(tests, NULL, NULL); |
Jakub Czapiga | 5d88483 | 2021-04-06 09:42:02 +0200 | [diff] [blame] | 485 | } |