From nobody Thu Dec 08 17:43:59 2022 X-Original-To: dev-commits-src-branches@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4NShNH6YD8z4k2XY; Thu, 8 Dec 2022 17:43:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4NShNH64vsz4D6m; Thu, 8 Dec 2022 17:43:59 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1670521439; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=BF84BJASEjBDUS+L7xmoI6CbD97IA5B9vfmAIkxiwMM=; b=EYWDvH3ZR+R8IqxhoVUq7mHBuRCNLD7Jt/nNP8XjL7GFICcjj5UROCTu2m+O89Rj5dH8ph OZP33Chk3/LQ9zmu5cr3TXq7KHnifpDkYLotBYBvyN0coN0nYmGLBN7veZA2n8zGH8JFzu qHrz0Dn2A0BtgkVo6UnYERuX4H3b9bGFK0MuZ0sc700OEc/h0XmwbF9KalPFw7hD6ZfShg rtzAWtObNjV6WD4ue9EpIm3R14VPewnQWavjjcCJl/qpSeJODZSbms1lmzqUi7HsOMaT+8 fdN/DrgnN4vl28nMitAvsCw3mT/tTaWyjTanxqrmaOcOB6X2Ow2GSr2+pnKoXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1670521439; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=BF84BJASEjBDUS+L7xmoI6CbD97IA5B9vfmAIkxiwMM=; b=xLthSr1Uklv/H0KWgSM6cWvi3DC2B90uJ+Ea5CSZ1s6Dd//IxOM5ZwdNj5rfDoaMDa5ou0 sy6erTjUsdwg5UqtQspAAVkliA7/ig0z1oEw2lFFYfEjCIMdaQHMWbBfO0rDyIES0LvPZM 7SHZQqPdDayimo7c/YLTsknA07Q/X4iSnvH2VH76dvP6I174SfB8qgy0zkYaMAiEsxU5cQ fD1XUjFPXM2AZQ0Tb2RPS5lYj0zv+h2kNJzlBB0zXPAjc0S8+YTIGYXdPAOjKUd3ydbaLK qfXnBQKJMY4q/sjeQjcAmvPk9IKMB1Ark/D+GW2JA/l9vEtj+hnVS5n4g72buA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1670521439; a=rsa-sha256; cv=none; b=hL0gdN0Y3fMC7P1ZX2OYQwP70SgXQ4J26s4cmSrnxB/y0V3xdhetVk39Y/tNlVD+WfYJM7 ELOMzLLTZxYUVkwPrkdqzb9hKkMHfdqf9uWZRRkHvvXwRrPn2g1Ee10xzLfD56T+zTHWfj T9yNFas0oKfJKt9/igf5PUZFFHiEyIsiI8hz15XKv0CaY+q6b72FEmed8/teEUbJMUdsGa 4URMRJ1UcbwgvnIwyBrJZPloKuWu8bI8KiAXrBJqwmX37AMHgg/ElSRSuqsyG/cmAx/bj9 w1K11boKyLPVNFGjoGDtMmSiyCn4Egf4mYg1090cUwlk7Yv+Z4Vs2it+/UPf5w== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4NShNH4vx9z14Sv; Thu, 8 Dec 2022 17:43:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 2B8Hhxs7098786; Thu, 8 Dec 2022 17:43:59 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 2B8HhxvY098785; Thu, 8 Dec 2022 17:43:59 GMT (envelope-from git) Date: Thu, 8 Dec 2022 17:43:59 GMT Message-Id: <202212081743.2B8HhxvY098785@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: =?utf-8?Q?Corvin=20K=C3=B6hne?= Subject: git: d84b694a589d - stable/13 - bhyve: add varfile option to nvlist of lpc device List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: corvink X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: d84b694a589d516add11022ba69501ee0a772e31 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by corvink: URL: https://cgit.FreeBSD.org/src/commit/?id=d84b694a589d516add11022ba69501ee0a772e31 commit d84b694a589d516add11022ba69501ee0a772e31 Author: Corvin Köhne AuthorDate: 2022-03-03 07:45:20 +0000 Commit: Corvin Köhne CommitDate: 2022-12-08 13:54:14 +0000 bhyve: add varfile option to nvlist of lpc device Use seperate nvlist entries for the romfile and the varfile. While here, don't leak varfd in bootrom_loadrom(). Reviewed by: jhb, markj Differential Revision: https://reviews.freebsd.org/D33433 (cherry picked from commit 87f6367f10614f58e5f93130b7be3364d2f83068) --- usr.sbin/bhyve/bhyve_config.5 | 7 +++++- usr.sbin/bhyve/bootrom.c | 52 +++++++++++++++++++++++++++++-------------- usr.sbin/bhyve/bootrom.h | 4 +++- usr.sbin/bhyve/pci_lpc.c | 23 ++++++++++++++----- 4 files changed, 62 insertions(+), 24 deletions(-) diff --git a/usr.sbin/bhyve/bhyve_config.5 b/usr.sbin/bhyve/bhyve_config.5 index 882479bf7b00..f14773313180 100644 --- a/usr.sbin/bhyve/bhyve_config.5 +++ b/usr.sbin/bhyve/bhyve_config.5 @@ -23,7 +23,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd August 19, 2022 +.Dd December 8, 2022 .Dt BHYVE_CONFIG 5 .Os .Sh NAME @@ -514,6 +514,11 @@ The contents of this file are copied into the guest's memory ending just before the 4GB physical address. If a boot ROM is present, a firmware interface device is also enabled for use by the boot ROM. +.It Va bootvars Ta path Ta Ta +Path to boot VARS. +The contents of this file are copied beneath the boot ROM. +Firmware can write to it to save variables. +All variables will be persistent even on reboots of the guest. .It Va com1 Ta node Ta Ta Settings for the COM1 serial port device. .It Va com2 Ta node Ta Ta diff --git a/usr.sbin/bhyve/bootrom.c b/usr.sbin/bhyve/bootrom.c index 757ec07d4a54..2406d4539e55 100644 --- a/usr.sbin/bhyve/bootrom.c +++ b/usr.sbin/bhyve/bootrom.c @@ -191,19 +191,33 @@ bootrom_alloc(struct vmctx *ctx, size_t len, int prot, int flags, } int -bootrom_loadrom(struct vmctx *ctx, const char *romfile) +bootrom_loadrom(struct vmctx *ctx, const nvlist_t *nvl) { struct stat sbuf; ssize_t rlen; off_t rom_size, var_size, total_size; - char *ptr, *varfile; + char *ptr, *romfile; int fd, varfd, i, rv; + const char *bootrom, *varfile; rv = -1; varfd = -1; - varfile = strdup(romfile); - romfile = strsep(&varfile, ","); + bootrom = get_config_value_node(nvl, "bootrom"); + if (bootrom == NULL) { + return (-1); + } + + /* + * get_config_value_node may use a thread local buffer to return + * variables. So, when we query the second variable, the first variable + * might get overwritten. For that reason, the bootrom should be + * duplicated. + */ + romfile = strdup(bootrom); + if (romfile == NULL) { + return (-1); + } fd = open(romfile, O_RDONLY); if (fd < 0) { @@ -212,6 +226,16 @@ bootrom_loadrom(struct vmctx *ctx, const char *romfile) goto done; } + if (fstat(fd, &sbuf) < 0) { + EPRINTLN("Could not fstat bootrom file \"%s\": %s", romfile, + strerror(errno)); + goto done; + } + + rom_size = sbuf.st_size; + + varfile = get_config_value_node(nvl, "bootvars"); + var_size = 0; if (varfile != NULL) { varfd = open(varfile, O_RDWR); if (varfd < 0) { @@ -219,23 +243,14 @@ bootrom_loadrom(struct vmctx *ctx, const char *romfile) "\"%s\": %s\n", varfile, strerror(errno)); goto done; } - } - - if (fstat(fd, &sbuf) < 0) { - EPRINTLN("Could not fstat bootrom file \"%s\": %s", - romfile, strerror(errno)); - goto done; - } - rom_size = sbuf.st_size; - if (varfd < 0) { - var_size = 0; - } else { if (fstat(varfd, &sbuf) < 0) { - fprintf(stderr, "Could not fstat bootrom variable file \"%s\": %s\n", - varfile, strerror(errno)); + fprintf(stderr, + "Could not fstat bootrom variable file \"%s\": %s\n", + varfile, strerror(errno)); goto done; } + var_size = sbuf.st_size; } @@ -291,7 +306,10 @@ bootrom_loadrom(struct vmctx *ctx, const char *romfile) rv = 0; done: + if (varfd >= 0) + close(varfd); if (fd >= 0) close(fd); + free(romfile); return (rv); } diff --git a/usr.sbin/bhyve/bootrom.h b/usr.sbin/bhyve/bootrom.h index da802343eefc..f718d5728c84 100644 --- a/usr.sbin/bhyve/bootrom.h +++ b/usr.sbin/bhyve/bootrom.h @@ -36,6 +36,8 @@ #include #include +#include "config.h" + struct vmctx; void init_bootrom(struct vmctx *ctx); @@ -45,6 +47,6 @@ enum { }; int bootrom_alloc(struct vmctx *ctx, size_t len, int prot, int flags, char **region_out, uint64_t *gpa_out); -int bootrom_loadrom(struct vmctx *ctx, const char *romfile); +int bootrom_loadrom(struct vmctx *ctx, const nvlist_t *nvl); #endif diff --git a/usr.sbin/bhyve/pci_lpc.c b/usr.sbin/bhyve/pci_lpc.c index 85c8ef86fed4..f430d32dbd49 100644 --- a/usr.sbin/bhyve/pci_lpc.c +++ b/usr.sbin/bhyve/pci_lpc.c @@ -36,6 +36,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #include #include #include @@ -95,13 +96,24 @@ lpc_device_parse(const char *opts) { int unit, error; char *str, *cpy, *lpcdev, *node_name; + const char *romfile, *varfile; error = -1; str = cpy = strdup(opts); lpcdev = strsep(&str, ","); if (lpcdev != NULL) { if (strcasecmp(lpcdev, "bootrom") == 0) { - set_config_value("lpc.bootrom", str); + romfile = strsep(&str, ","); + if (romfile == NULL) { + errx(4, "invalid bootrom option \"%s\"", opts); + } + set_config_value("lpc.bootrom", romfile); + + varfile = strsep(&str, ","); + if (varfile != NULL) { + set_config_value("lpc.bootvars", varfile); + } + error = 0; goto done; } @@ -204,13 +216,14 @@ lpc_init(struct vmctx *ctx) { struct lpc_uart_softc *sc; struct inout_port iop; - const char *backend, *name, *romfile; + const char *backend, *name; char *node_name; int unit, error; + const nvlist_t *nvl; - romfile = get_config_value("lpc.bootrom"); - if (romfile != NULL) { - error = bootrom_loadrom(ctx, romfile); + nvl = find_config_node("lpc"); + if (nvl != NULL && nvlist_exists(nvl, "bootrom")) { + error = bootrom_loadrom(ctx, nvl); if (error) return (error); }