1 /* Dynamic linker/loader of ACPI tables 2 * 3 * Copyright (C) 2013 Red Hat Inc 4 * 5 * Author: Michael S. Tsirkin <mst@redhat.com> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 17 * You should have received a copy of the GNU General Public License along 18 * with this program; if not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 #include "qemu/osdep.h" 22 #include "hw/acpi/bios-linker-loader.h" 23 #include "hw/nvram/fw_cfg.h" 24 25 /* 26 * Linker/loader is a paravirtualized interface that passes commands to guest. 27 * The commands can be used to request guest to 28 * - allocate memory chunks and initialize them from QEMU FW CFG files 29 * - link allocated chunks by storing pointer to one chunk into another 30 * - calculate ACPI checksum of part of the chunk and store into same chunk 31 */ 32 #define BIOS_LINKER_LOADER_FILESZ FW_CFG_MAX_FILE_PATH 33 34 struct BiosLinkerLoaderEntry { 35 uint32_t command; 36 union { 37 /* 38 * COMMAND_ALLOCATE - allocate a table from @alloc.file 39 * subject to @alloc.align alignment (must be power of 2) 40 * and @alloc.zone (can be HIGH or FSEG) requirements. 41 * 42 * Must appear exactly once for each file, and before 43 * this file is referenced by any other command. 44 */ 45 struct { 46 char file[BIOS_LINKER_LOADER_FILESZ]; 47 uint32_t align; 48 uint8_t zone; 49 } alloc; 50 51 /* 52 * COMMAND_ADD_POINTER - patch the table (originating from 53 * @dest_file) at @pointer.offset, by adding a pointer to the table 54 * originating from @src_file. 1,2,4 or 8 byte unsigned 55 * addition is used depending on @pointer.size. 56 */ 57 struct { 58 char dest_file[BIOS_LINKER_LOADER_FILESZ]; 59 char src_file[BIOS_LINKER_LOADER_FILESZ]; 60 uint32_t offset; 61 uint8_t size; 62 } pointer; 63 64 /* 65 * COMMAND_ADD_CHECKSUM - calculate checksum of the range specified by 66 * @cksum_start and @cksum_length fields, 67 * and then add the value at @cksum.offset. 68 * Checksum simply sums -X for each byte X in the range 69 * using 8-bit math. 70 */ 71 struct { 72 char file[BIOS_LINKER_LOADER_FILESZ]; 73 uint32_t offset; 74 uint32_t start; 75 uint32_t length; 76 } cksum; 77 78 /* 79 * COMMAND_WRITE_POINTER - write the fw_cfg file (originating from 80 * @dest_file) at @wr_pointer.offset, by adding a pointer to 81 * @src_offset within the table originating from @src_file. 82 * 1,2,4 or 8 byte unsigned addition is used depending on 83 * @wr_pointer.size. 84 */ 85 struct { 86 char dest_file[BIOS_LINKER_LOADER_FILESZ]; 87 char src_file[BIOS_LINKER_LOADER_FILESZ]; 88 uint32_t dst_offset; 89 uint32_t src_offset; 90 uint8_t size; 91 } wr_pointer; 92 93 /* padding */ 94 char pad[124]; 95 }; 96 } QEMU_PACKED; 97 typedef struct BiosLinkerLoaderEntry BiosLinkerLoaderEntry; 98 99 enum { 100 BIOS_LINKER_LOADER_COMMAND_ALLOCATE = 0x1, 101 BIOS_LINKER_LOADER_COMMAND_ADD_POINTER = 0x2, 102 BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM = 0x3, 103 BIOS_LINKER_LOADER_COMMAND_WRITE_POINTER = 0x4, 104 }; 105 106 enum { 107 BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH = 0x1, 108 BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG = 0x2, 109 }; 110 111 /* 112 * BiosLinkerFileEntry: 113 * 114 * An internal type used for book-keeping file entries 115 */ 116 typedef struct BiosLinkerFileEntry { 117 char *name; /* file name */ 118 GArray *blob; /* data accosiated with @name */ 119 } BiosLinkerFileEntry; 120 121 /* 122 * bios_linker_loader_init: allocate a new linker object instance. 123 * 124 * After initialization, linker commands can be added, and will 125 * be stored in the linker.cmd_blob array. 126 */ 127 BIOSLinker *bios_linker_loader_init(void) 128 { 129 BIOSLinker *linker = g_new(BIOSLinker, 1); 130 131 linker->cmd_blob = g_array_new(false, true /* clear */, 1); 132 linker->file_list = g_array_new(false, true /* clear */, 133 sizeof(BiosLinkerFileEntry)); 134 return linker; 135 } 136 137 /* Free linker wrapper */ 138 void bios_linker_loader_cleanup(BIOSLinker *linker) 139 { 140 int i; 141 BiosLinkerFileEntry *entry; 142 143 g_array_free(linker->cmd_blob, true); 144 145 for (i = 0; i < linker->file_list->len; i++) { 146 entry = &g_array_index(linker->file_list, BiosLinkerFileEntry, i); 147 g_free(entry->name); 148 } 149 g_array_free(linker->file_list, true); 150 g_free(linker); 151 } 152 153 static const BiosLinkerFileEntry * 154 bios_linker_find_file(const BIOSLinker *linker, const char *name) 155 { 156 int i; 157 BiosLinkerFileEntry *entry; 158 159 for (i = 0; i < linker->file_list->len; i++) { 160 entry = &g_array_index(linker->file_list, BiosLinkerFileEntry, i); 161 if (!strcmp(entry->name, name)) { 162 return entry; 163 } 164 } 165 return NULL; 166 } 167 168 /* 169 * board code must realize fw_cfg first, as a fixed device, before 170 * another device realize function call bios_linker_loader_can_write_pointer() 171 */ 172 bool bios_linker_loader_can_write_pointer(void) 173 { 174 FWCfgState *fw_cfg = fw_cfg_find(); 175 return fw_cfg && fw_cfg_dma_enabled(fw_cfg); 176 } 177 178 /* 179 * bios_linker_loader_alloc: ask guest to load file into guest memory. 180 * 181 * @linker: linker object instance 182 * @file_name: name of the file blob to be loaded 183 * @file_blob: pointer to blob corresponding to @file_name 184 * @alloc_align: required minimal alignment in bytes. Must be a power of 2. 185 * @alloc_fseg: request allocation in FSEG zone (useful for the RSDP ACPI table) 186 * 187 * Note: this command must precede any other linker command using this file. 188 */ 189 void bios_linker_loader_alloc(BIOSLinker *linker, 190 const char *file_name, 191 GArray *file_blob, 192 uint32_t alloc_align, 193 bool alloc_fseg) 194 { 195 BiosLinkerLoaderEntry entry; 196 BiosLinkerFileEntry file = { g_strdup(file_name), file_blob}; 197 198 assert(!(alloc_align & (alloc_align - 1))); 199 200 assert(!bios_linker_find_file(linker, file_name)); 201 g_array_append_val(linker->file_list, file); 202 203 memset(&entry, 0, sizeof entry); 204 strncpy(entry.alloc.file, file_name, sizeof entry.alloc.file - 1); 205 entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ALLOCATE); 206 entry.alloc.align = cpu_to_le32(alloc_align); 207 entry.alloc.zone = alloc_fseg ? BIOS_LINKER_LOADER_ALLOC_ZONE_FSEG : 208 BIOS_LINKER_LOADER_ALLOC_ZONE_HIGH; 209 210 /* Alloc entries must come first, so prepend them */ 211 g_array_prepend_vals(linker->cmd_blob, &entry, sizeof entry); 212 } 213 214 /* 215 * bios_linker_loader_add_checksum: ask guest to add checksum of ACPI 216 * table in the specified file at the specified offset. 217 * 218 * Checksum calculation simply sums -X for each byte X in the range 219 * using 8-bit math (i.e. ACPI checksum). 220 * 221 * @linker: linker object instance 222 * @file: file that includes the checksum to be calculated 223 * and the data to be checksummed 224 * @start_offset, @size: range of data in the file to checksum, 225 * relative to the start of file blob 226 * @checksum_offset: location of the checksum to be patched within file blob, 227 * relative to the start of file blob 228 */ 229 void bios_linker_loader_add_checksum(BIOSLinker *linker, const char *file_name, 230 unsigned start_offset, unsigned size, 231 unsigned checksum_offset) 232 { 233 BiosLinkerLoaderEntry entry; 234 const BiosLinkerFileEntry *file = bios_linker_find_file(linker, file_name); 235 236 assert(file); 237 assert(start_offset < file->blob->len); 238 assert(start_offset + size <= file->blob->len); 239 assert(checksum_offset >= start_offset); 240 assert(checksum_offset + 1 <= start_offset + size); 241 242 *(file->blob->data + checksum_offset) = 0; 243 memset(&entry, 0, sizeof entry); 244 strncpy(entry.cksum.file, file_name, sizeof entry.cksum.file - 1); 245 entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ADD_CHECKSUM); 246 entry.cksum.offset = cpu_to_le32(checksum_offset); 247 entry.cksum.start = cpu_to_le32(start_offset); 248 entry.cksum.length = cpu_to_le32(size); 249 250 g_array_append_vals(linker->cmd_blob, &entry, sizeof entry); 251 } 252 253 /* 254 * bios_linker_loader_add_pointer: ask guest to patch address in 255 * destination file with a pointer to source file 256 * 257 * @linker: linker object instance 258 * @dest_file: destination file that must be changed 259 * @dst_patched_offset: location within destination file blob to be patched 260 * with the pointer to @src_file+@src_offset (i.e. source 261 * blob allocated in guest memory + @src_offset), in bytes 262 * @dst_patched_offset_size: size of the pointer to be patched 263 * at @dst_patched_offset in @dest_file blob, in bytes 264 * @src_file: source file who's address must be taken 265 * @src_offset: location within source file blob to which 266 * @dest_file+@dst_patched_offset will point to after 267 * firmware's executed ADD_POINTER command 268 */ 269 void bios_linker_loader_add_pointer(BIOSLinker *linker, 270 const char *dest_file, 271 uint32_t dst_patched_offset, 272 uint8_t dst_patched_size, 273 const char *src_file, 274 uint32_t src_offset) 275 { 276 uint64_t le_src_offset; 277 BiosLinkerLoaderEntry entry; 278 const BiosLinkerFileEntry *dst_file = 279 bios_linker_find_file(linker, dest_file); 280 const BiosLinkerFileEntry *source_file = 281 bios_linker_find_file(linker, src_file); 282 283 assert(dst_file); 284 assert(source_file); 285 assert(dst_patched_offset < dst_file->blob->len); 286 assert(dst_patched_offset + dst_patched_size <= dst_file->blob->len); 287 assert(src_offset < source_file->blob->len); 288 289 memset(&entry, 0, sizeof entry); 290 strncpy(entry.pointer.dest_file, dest_file, 291 sizeof entry.pointer.dest_file - 1); 292 strncpy(entry.pointer.src_file, src_file, 293 sizeof entry.pointer.src_file - 1); 294 entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_ADD_POINTER); 295 entry.pointer.offset = cpu_to_le32(dst_patched_offset); 296 entry.pointer.size = dst_patched_size; 297 assert(dst_patched_size == 1 || dst_patched_size == 2 || 298 dst_patched_size == 4 || dst_patched_size == 8); 299 300 le_src_offset = cpu_to_le64(src_offset); 301 memcpy(dst_file->blob->data + dst_patched_offset, 302 &le_src_offset, dst_patched_size); 303 304 g_array_append_vals(linker->cmd_blob, &entry, sizeof entry); 305 } 306 307 /* 308 * bios_linker_loader_write_pointer: ask guest to write a pointer to the 309 * source file into the destination file, and write it back to QEMU via 310 * fw_cfg DMA. 311 * 312 * @linker: linker object instance 313 * @dest_file: destination file that must be written 314 * @dst_patched_offset: location within destination file blob to be patched 315 * with the pointer to @src_file, in bytes 316 * @dst_patched_offset_size: size of the pointer to be patched 317 * at @dst_patched_offset in @dest_file blob, in bytes 318 * @src_file: source file who's address must be taken 319 * @src_offset: location within source file blob to which 320 * @dest_file+@dst_patched_offset will point to after 321 * firmware's executed WRITE_POINTER command 322 */ 323 void bios_linker_loader_write_pointer(BIOSLinker *linker, 324 const char *dest_file, 325 uint32_t dst_patched_offset, 326 uint8_t dst_patched_size, 327 const char *src_file, 328 uint32_t src_offset) 329 { 330 BiosLinkerLoaderEntry entry; 331 const BiosLinkerFileEntry *source_file = 332 bios_linker_find_file(linker, src_file); 333 334 assert(source_file); 335 assert(src_offset < source_file->blob->len); 336 memset(&entry, 0, sizeof entry); 337 strncpy(entry.wr_pointer.dest_file, dest_file, 338 sizeof entry.wr_pointer.dest_file - 1); 339 strncpy(entry.wr_pointer.src_file, src_file, 340 sizeof entry.wr_pointer.src_file - 1); 341 entry.command = cpu_to_le32(BIOS_LINKER_LOADER_COMMAND_WRITE_POINTER); 342 entry.wr_pointer.dst_offset = cpu_to_le32(dst_patched_offset); 343 entry.wr_pointer.src_offset = cpu_to_le32(src_offset); 344 entry.wr_pointer.size = dst_patched_size; 345 assert(dst_patched_size == 1 || dst_patched_size == 2 || 346 dst_patched_size == 4 || dst_patched_size == 8); 347 348 g_array_append_vals(linker->cmd_blob, &entry, sizeof entry); 349 } 350