1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * (C) Copyright 2015 Google, Inc 4 * Written by Simon Glass <sjg@chromium.org> 5 */ 6 7 #include <common.h> 8 #include <command.h> 9 #include <efi.h> 10 #include <errno.h> 11 #include <malloc.h> 12 13 static const char *const type_name[] = { 14 "reserved", 15 "loader_code", 16 "loader_data", 17 "bs_code", 18 "bs_data", 19 "rt_code", 20 "rt_data", 21 "conv", 22 "unusable", 23 "acpi_reclaim", 24 "acpi_nvs", 25 "io", 26 "io_port", 27 "pal_code", 28 }; 29 30 static struct attr_info { 31 int shift; 32 const char *name; 33 } mem_attr[] = { 34 { EFI_MEMORY_UC_SHIFT, "uncached" }, 35 { EFI_MEMORY_WC_SHIFT, "write-coalescing" }, 36 { EFI_MEMORY_WT_SHIFT, "write-through" }, 37 { EFI_MEMORY_WB_SHIFT, "write-back" }, 38 { EFI_MEMORY_UCE_SHIFT, "uncached & exported" }, 39 { EFI_MEMORY_WP_SHIFT, "write-protect" }, 40 { EFI_MEMORY_RP_SHIFT, "read-protect" }, 41 { EFI_MEMORY_XP_SHIFT, "execute-protect" }, 42 { EFI_MEMORY_RUNTIME_SHIFT, "needs runtime mapping" } 43 }; 44 45 /* Maximum different attribute values we can track */ 46 #define ATTR_SEEN_MAX 30 47 48 static inline bool is_boot_services(int type) 49 { 50 return type == EFI_LOADER_CODE || type == EFI_LOADER_DATA || 51 type == EFI_BOOT_SERVICES_CODE || 52 type == EFI_BOOT_SERVICES_DATA; 53 } 54 55 static int h_cmp_entry(const void *v1, const void *v2) 56 { 57 const struct efi_mem_desc *desc1 = v1; 58 const struct efi_mem_desc *desc2 = v2; 59 int64_t diff = desc1->physical_start - desc2->physical_start; 60 61 /* 62 * Manually calculate the difference to avoid sign loss in the 64-bit 63 * to 32-bit conversion 64 */ 65 return diff < 0 ? -1 : diff > 0 ? 1 : 0; 66 } 67 68 void *efi_build_mem_table(struct efi_entry_memmap *map, int size, bool skip_bs) 69 { 70 struct efi_mem_desc *desc, *end, *base, *dest, *prev; 71 int count; 72 u64 addr; 73 74 base = malloc(size + sizeof(*desc)); 75 if (!base) { 76 debug("%s: Cannot allocate %#x bytes\n", __func__, size); 77 return NULL; 78 } 79 end = (struct efi_mem_desc *)((ulong)map + size); 80 count = ((ulong)end - (ulong)map->desc) / map->desc_size; 81 memcpy(base, map->desc, (ulong)end - (ulong)map->desc); 82 qsort(base, count, map->desc_size, h_cmp_entry); 83 prev = NULL; 84 addr = 0; 85 dest = base; 86 end = (struct efi_mem_desc *)((ulong)base + count * map->desc_size); 87 for (desc = base; desc < end; desc = efi_get_next_mem_desc(map, desc)) { 88 bool merge = true; 89 int type = desc->type; 90 91 if (skip_bs && is_boot_services(desc->type)) 92 type = EFI_CONVENTIONAL_MEMORY; 93 94 memcpy(dest, desc, map->desc_size); 95 dest->type = type; 96 if (!skip_bs || !prev) 97 merge = false; 98 else if (desc->physical_start != addr) 99 merge = false; 100 else if (type != EFI_CONVENTIONAL_MEMORY) 101 merge = false; 102 else if (prev->type != EFI_CONVENTIONAL_MEMORY) 103 merge = false; 104 105 if (merge) { 106 prev->num_pages += desc->num_pages; 107 } else { 108 prev = dest; 109 dest = efi_get_next_mem_desc(map, dest); 110 } 111 addr = desc->physical_start + (desc->num_pages << 112 EFI_PAGE_SHIFT); 113 } 114 115 /* Mark the end */ 116 dest->type = EFI_TABLE_END; 117 118 return base; 119 } 120 121 static void efi_print_mem_table(struct efi_entry_memmap *map, 122 struct efi_mem_desc *desc, bool skip_bs) 123 { 124 u64 attr_seen[ATTR_SEEN_MAX]; 125 int attr_seen_count; 126 int upto, i; 127 u64 addr; 128 129 printf(" # %-14s %10s %10s %10s %s\n", "Type", "Physical", 130 "Virtual", "Size", "Attributes"); 131 132 /* Keep track of all the different attributes we have seen */ 133 attr_seen_count = 0; 134 addr = 0; 135 for (upto = 0; desc->type != EFI_TABLE_END; 136 upto++, desc = efi_get_next_mem_desc(map, desc)) { 137 const char *name; 138 u64 size; 139 140 if (skip_bs && is_boot_services(desc->type)) 141 continue; 142 if (desc->physical_start != addr) { 143 printf(" %-14s %010llx %10s %010llx\n", "<gap>", 144 addr, "", desc->physical_start - addr); 145 } 146 size = desc->num_pages << EFI_PAGE_SHIFT; 147 148 name = desc->type < ARRAY_SIZE(type_name) ? 149 type_name[desc->type] : "<invalid>"; 150 printf("%2d %x:%-12s %010llx %010llx %010llx ", upto, 151 desc->type, name, desc->physical_start, 152 desc->virtual_start, size); 153 if (desc->attribute & EFI_MEMORY_RUNTIME) 154 putc('r'); 155 printf("%llx", desc->attribute & ~EFI_MEMORY_RUNTIME); 156 putc('\n'); 157 158 for (i = 0; i < attr_seen_count; i++) { 159 if (attr_seen[i] == desc->attribute) 160 break; 161 } 162 if (i == attr_seen_count && i < ATTR_SEEN_MAX) 163 attr_seen[attr_seen_count++] = desc->attribute; 164 addr = desc->physical_start + size; 165 } 166 167 printf("\nAttributes key:\n"); 168 for (i = 0; i < attr_seen_count; i++) { 169 u64 attr = attr_seen[i]; 170 bool first; 171 int j; 172 173 printf("%c%llx: ", attr & EFI_MEMORY_RUNTIME ? 'r' : ' ', 174 attr & ~EFI_MEMORY_RUNTIME); 175 for (j = 0, first = true; j < ARRAY_SIZE(mem_attr); j++) { 176 if (attr & (1ULL << mem_attr[j].shift)) { 177 if (first) 178 first = false; 179 else 180 printf(", "); 181 printf("%s", mem_attr[j].name); 182 } 183 } 184 putc('\n'); 185 } 186 if (skip_bs) 187 printf("*Some areas are merged (use 'all' to see)\n"); 188 } 189 190 static int do_efi_mem(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 191 { 192 struct efi_mem_desc *desc; 193 struct efi_entry_memmap *map; 194 int size, ret; 195 bool skip_bs; 196 197 skip_bs = !argc || *argv[0] != 'a'; 198 ret = efi_info_get(EFIET_MEMORY_MAP, (void **)&map, &size); 199 switch (ret) { 200 case -ENOENT: 201 printf("No EFI table available\n"); 202 goto done; 203 case -EPROTONOSUPPORT: 204 printf("Incorrect EFI table version\n"); 205 goto done; 206 } 207 printf("EFI table at %lx, memory map %p, size %x, version %x, descr. size %#x\n", 208 gd->arch.table, map, size, map->version, map->desc_size); 209 if (map->version != EFI_MEM_DESC_VERSION) { 210 printf("Incorrect memory map version\n"); 211 ret = -EPROTONOSUPPORT; 212 goto done; 213 } 214 215 desc = efi_build_mem_table(map, size, skip_bs); 216 if (!desc) { 217 ret = -ENOMEM; 218 goto done; 219 } 220 221 efi_print_mem_table(map, desc, skip_bs); 222 free(desc); 223 done: 224 if (ret) 225 printf("Error: %d\n", ret); 226 227 return ret ? CMD_RET_FAILURE : 0; 228 } 229 230 static cmd_tbl_t efi_commands[] = { 231 U_BOOT_CMD_MKENT(mem, 1, 1, do_efi_mem, "", ""), 232 }; 233 234 static int do_efi(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) 235 { 236 cmd_tbl_t *efi_cmd; 237 int ret; 238 239 if (argc < 2) 240 return CMD_RET_USAGE; 241 efi_cmd = find_cmd_tbl(argv[1], efi_commands, ARRAY_SIZE(efi_commands)); 242 argc -= 2; 243 argv += 2; 244 if (!efi_cmd || argc > efi_cmd->maxargs) 245 return CMD_RET_USAGE; 246 247 ret = efi_cmd->cmd(efi_cmd, flag, argc, argv); 248 249 return cmd_process_error(efi_cmd, ret); 250 } 251 252 U_BOOT_CMD( 253 efi, 3, 1, do_efi, 254 "EFI access", 255 "mem [all] Dump memory information [include boot services]" 256 ); 257