1/*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2022 Beckhoff Automation GmbH & Co. KG
5 * Author: Corvin K��hne <c.koehne@beckhoff.com>
6 */
7
8#pragma once
9
10#include "qemu_fwcfg.h"
11
12struct qemu_loader;
13
14/*
15 * Some guest bios like seabios assume the RSDP to be located in the FSEG. Bhyve
16 * only supports OVMF which has no such requirement.
17 */
18enum qemu_loader_zone {
19	QEMU_LOADER_ALLOC_HIGH = 1,
20	QEMU_LOADER_ALLOC_FSEG, /* 0x0F000000 - 0x100000 */
21};
22
23/**
24 * Loads a fwcfg item into guest memory. This command has to be issued before
25 * any subsequent command can be used.
26 *
27 * @param loader     Qemu loader instance the command should be added to.
28 * @param name       Name of the fwcfg item which should be allocated.
29 * @param alignment  Alignment required by the data.
30 * @param zone       Memory zone in which it should be loaded.
31 */
32int qemu_loader_alloc(struct qemu_loader *loader, const uint8_t *name,
33    uint32_t alignment, enum qemu_loader_zone zone);
34/**
35 * Calculates a checksum for @p name and writes it to @p name + @p off . The
36 * checksum calculation ranges from @p start to @p start + @p len. The checksum
37 * field is always one byte large and all bytes in the specified range,
38 * including the checksum, have to sum up to 0.
39 *
40 * @param loader Qemu loader instance the command should be added to.
41 * @param name   Name of the fwcfg item which should be patched.
42 * @param off    Offset into @p name .
43 * @param start  Start offset of checksum calculation.
44 * @param len    Length of the checksum calculation.
45 */
46int qemu_loader_add_checksum(struct qemu_loader *loader, const uint8_t *name,
47    uint32_t off, uint32_t start, uint32_t len);
48/**
49 * Adds the address of @p src_name to the value at @p dest_name + @p off . The
50 * size of the pointer is determined by @p dest_size and should be 1, 2, 4 or 8.
51 *
52 * @param loader     Qemu loader instance the command should be added to.
53 * @param dest_name  Name of the fwcfg item which should be patched.
54 * @param src_name   Name of the fwcfg item which address should be written to
55 *                   @p dest_name + @p off.
56 * @param off        Offset into @p dest_name .
57 * @param size       Size of the pointer (1, 2, 4 or 8).
58 */
59int qemu_loader_add_pointer(struct qemu_loader *loader,
60    const uint8_t *dest_name, const uint8_t *src_name, uint32_t off,
61    uint8_t size);
62
63/**
64 * Creates a qemu loader instance.
65 *
66 * @param new_loader  Returns the newly created qemu loader instance.
67 * @param fwcfg_name  Name of the FwCfg item which represents the qemu loader
68 */
69int qemu_loader_create(struct qemu_loader **new_loader,
70    const uint8_t *fwcfg_name);
71/**
72 * Signals that all commands are written to the qemu loader. This function
73 * creates a proper FwCfg item and registers it.
74 *
75 * @param loader  Qemu loader instance which should be finished.
76 */
77int qemu_loader_finish(struct qemu_loader *loader);
78