bios-linker-loader.c 13 KB

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