1 /* 2 * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <assert.h> 8 #include <string.h> 9 10 #include <drivers/coreboot/cbmem_console.h> 11 #include <common/debug.h> 12 #include <lib/coreboot.h> 13 #include <lib/mmio.h> 14 #include <lib/xlat_tables/xlat_tables_v2.h> 15 16 /* 17 * Structures describing coreboot's in-memory descriptor tables. See 18 * <coreboot>/src/commonlib/include/commonlib/coreboot_tables.h for 19 * canonical implementation. 20 */ 21 22 typedef struct { 23 char signature[4]; 24 uint32_t header_bytes; 25 uint32_t header_checksum; 26 uint32_t table_bytes; 27 uint32_t table_checksum; 28 uint32_t table_entries; 29 } cb_header_t; 30 31 typedef enum { 32 CB_TAG_MEMORY = 0x1, 33 CB_TAG_SERIAL = 0xf, 34 CB_TAG_CBMEM_CONSOLE = 0x17, 35 } cb_tag_t; 36 37 typedef struct { 38 uint32_t tag; 39 uint32_t size; 40 union { 41 coreboot_memrange_t memranges[COREBOOT_MAX_MEMRANGES]; 42 coreboot_serial_t serial; 43 uint64_t uint64; 44 }; 45 } cb_entry_t; 46 47 coreboot_memrange_t coreboot_memranges[COREBOOT_MAX_MEMRANGES]; 48 coreboot_serial_t coreboot_serial; 49 50 /* 51 * The coreboot table is parsed before the MMU is enabled (i.e. with strongly 52 * ordered memory), so we cannot make unaligned accesses. The table entries 53 * immediately follow one another without padding, so nothing after the header 54 * is guaranteed to be naturally aligned. Therefore, we need to define safety 55 * functions that can read unaligned integers. 56 */ 57 static uint32_t read_le32(uint32_t *p) 58 { 59 uintptr_t addr = (uintptr_t)p; 60 return mmio_read_8(addr) | 61 mmio_read_8(addr + 1) << 8 | 62 mmio_read_8(addr + 2) << 16 | 63 mmio_read_8(addr + 3) << 24; 64 } 65 static uint64_t read_le64(uint64_t *p) 66 { 67 return read_le32((void *)p) | (uint64_t)read_le32((void *)p + 4) << 32; 68 } 69 70 static void expand_and_mmap(uintptr_t baseaddr, size_t size) 71 { 72 uintptr_t pageaddr = round_down(baseaddr, PAGE_SIZE); 73 size_t expanded = round_up(baseaddr - pageaddr + size, PAGE_SIZE); 74 mmap_add_region(pageaddr, pageaddr, expanded, 75 MT_MEMORY | MT_RW | MT_NS | MT_EXECUTE_NEVER); 76 } 77 78 static void setup_cbmem_console(uintptr_t baseaddr) 79 { 80 static console_cbmc_t console; 81 assert(!console.console.base); /* should only have one CBMEM console */ 82 83 /* CBMEM console structure stores its size in first header field. */ 84 uint32_t size = *(uint32_t *)baseaddr; 85 expand_and_mmap(baseaddr, size); 86 console_cbmc_register(baseaddr, &console); 87 console_set_scope(&console.console, CONSOLE_FLAG_BOOT | 88 CONSOLE_FLAG_RUNTIME | 89 CONSOLE_FLAG_CRASH); 90 } 91 92 coreboot_memory_t coreboot_get_memory_type(uintptr_t address) 93 { 94 int i; 95 96 for (i = 0; i < COREBOOT_MAX_MEMRANGES; i++) { 97 coreboot_memrange_t *range = &coreboot_memranges[i]; 98 99 if (range->type == CB_MEM_NONE) 100 break; /* end of table reached */ 101 if (address >= range->start && 102 address - range->start < range->size) 103 return range->type; 104 } 105 106 return CB_MEM_NONE; 107 } 108 109 void coreboot_table_setup(void *base) 110 { 111 cb_header_t *header = base; 112 void *ptr; 113 int i; 114 115 if (strncmp(header->signature, "LBIO", 4)) { 116 ERROR("coreboot table signature corrupt!\n"); 117 return; 118 } 119 120 ptr = base + header->header_bytes; 121 for (i = 0; i < header->table_entries; i++) { 122 size_t size; 123 cb_entry_t *entry = ptr; 124 125 if (ptr - base >= header->header_bytes + header->table_bytes) { 126 ERROR("coreboot table exceeds its bounds!\n"); 127 break; 128 } 129 130 switch (read_le32(&entry->tag)) { 131 case CB_TAG_MEMORY: 132 size = read_le32(&entry->size) - 133 offsetof(cb_entry_t, memranges); 134 if (size > sizeof(coreboot_memranges)) { 135 ERROR("Need to truncate coreboot memranges!\n"); 136 size = sizeof(coreboot_memranges); 137 } 138 memcpy(&coreboot_memranges, &entry->memranges, size); 139 break; 140 case CB_TAG_SERIAL: 141 memcpy(&coreboot_serial, &entry->serial, 142 sizeof(coreboot_serial)); 143 break; 144 case CB_TAG_CBMEM_CONSOLE: 145 setup_cbmem_console(read_le64(&entry->uint64)); 146 break; 147 default: 148 /* There are many tags TF doesn't need to care about. */ 149 break; 150 } 151 152 ptr += read_le32(&entry->size); 153 } 154 } 155