From: "Alex Bennée" <alex.bennee@linaro.org>
To: qemu-devel@nongnu.org, maxim.uvarov@linaro.org,
joakim.bech@linaro.org, ilias.apalodimas@linaro.org,
tomas.winkler@intel.com, yang.huang@intel.com,
bing.zhu@intel.com, Matti.Moell@opensynergy.com,
hmo@opensynergy.com
Cc: jean-philippe@linaro.org, takahiro.akashi@linaro.org,
virtualization@lists.linuxfoundation.org,
"Alex Bennée" <alex.bennee@linaro.org>,
arnd@linaro.org, stratos-dev@op-lists.linaro.org
Subject: [RFC PATCH 11/19] tools/vhost-user-rpmb: add --flash-path for backing store
Date: Fri, 25 Sep 2020 13:51:39 +0100 [thread overview]
Message-ID: <20200925125147.26943-12-alex.bennee@linaro.org> (raw)
In-Reply-To: <20200925125147.26943-1-alex.bennee@linaro.org>
We will need to store the data somewhere so add the option to point to
the file where we will keep the data.
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
tools/vhost-user-rpmb/main.c | 58 +++++++++++++++++++++++++++++++++++-
1 file changed, 57 insertions(+), 1 deletion(-)
diff --git a/tools/vhost-user-rpmb/main.c b/tools/vhost-user-rpmb/main.c
index 7b3b29ccfc5b..64bd7e79f573 100644
--- a/tools/vhost-user-rpmb/main.c
+++ b/tools/vhost-user-rpmb/main.c
@@ -13,9 +13,15 @@
#include <gio/gio.h>
#include <gio/gunixsocketaddress.h>
#include <glib-unix.h>
+#include <glib/gstdio.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+#include <unistd.h>
#include "contrib/libvhost-user/libvhost-user-glib.h"
#include "contrib/libvhost-user/libvhost-user.h"
@@ -27,6 +33,7 @@
#endif
static gchar *socket_path;
+static char *flash_path;
static gint socket_fd = -1;
static gboolean print_cap;
static gboolean verbose;
@@ -35,6 +42,7 @@ static gboolean debug;
static GOptionEntry options[] =
{
{ "socket-path", 0, 0, G_OPTION_ARG_FILENAME, &socket_path, "Location of vhost-user Unix domain socket, incompatible with --fd", "PATH" },
+ { "flash-path", 0, 0, G_OPTION_ARG_FILENAME, &flash_path, "Location of raw flash image file", "PATH" },
{ "fd", 0, 0, G_OPTION_ARG_INT, &socket_fd, "Specify the file-descriptor of the backend, incompatible with --socket-path", "FD" },
{ "print-capabilities", 0, 0, G_OPTION_ARG_NONE, &print_cap, "Output to stdout the backend capabilities in JSON format and exit", NULL},
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "Be more verbose in output", NULL},
@@ -47,6 +55,8 @@ enum {
};
/* These structures are defined in the specification */
+#define KiB (1UL << 10)
+#define MAX_RPMB_SIZE (KiB * 128 * 256)
struct virtio_rpmb_config {
uint8_t capacity;
@@ -75,6 +85,8 @@ typedef struct VuRpmb {
VugDev dev;
struct virtio_rpmb_config virtio_config;
GMainLoop *loop;
+ int flash_fd;
+ void *flash_map;
} VuRpmb;
struct virtio_rpmb_ctrl_command {
@@ -116,6 +128,8 @@ vrpmb_get_config(VuDev *dev, uint8_t *config, uint32_t len)
VuRpmb *r = container_of(dev, VuRpmb, dev.parent);
g_return_val_if_fail(len <= sizeof(struct virtio_rpmb_config), -1);
memcpy(config, &r->virtio_config, len);
+
+ g_info("%s: done", __func__);
return 0;
}
@@ -192,6 +206,41 @@ static const VuDevIface vuiface = {
.set_config = vrpmb_set_config,
};
+static bool vrpmb_load_flash_image(VuRpmb *r, char *img_path)
+{
+ GStatBuf statbuf;
+ size_t map_size;
+
+ if (g_stat(img_path, &statbuf) < 0) {
+ g_error("couldn't stat %s", img_path);
+ return false;
+ }
+
+ r->flash_fd = g_open(img_path, O_RDWR, 0);
+ if (r->flash_fd < 0) {
+ g_error("couldn't open %s (%s)", img_path, strerror(errno));
+ return false;
+ }
+
+ if (statbuf.st_size > MAX_RPMB_SIZE) {
+ g_warning("%s larger than maximum size supported", img_path);
+ map_size = MAX_RPMB_SIZE;
+ } else {
+ map_size = statbuf.st_size;
+ }
+ r->virtio_config.capacity = map_size / (128 *KiB);
+ r->virtio_config.max_wr_cnt = 1;
+ r->virtio_config.max_rd_cnt = 1;
+
+ r->flash_map = mmap(NULL, map_size, PROT_READ, MAP_SHARED, r->flash_fd, 0);
+ if (r->flash_map == MAP_FAILED) {
+ g_error("failed to mmap file");
+ return false;
+ }
+
+ return true;
+}
+
static void vrpmb_destroy(VuRpmb *r)
{
vug_deinit(&r->dev);
@@ -216,7 +265,7 @@ static gboolean hangup(gpointer user_data)
return true;
}
-int main (int argc, char *argv[])
+int main(int argc, char *argv[])
{
GError *error = NULL;
GOptionContext *context;
@@ -236,6 +285,13 @@ int main (int argc, char *argv[])
exit(0);
}
+ if (!flash_path || !g_file_test(flash_path, G_FILE_TEST_EXISTS)) {
+ g_printerr("Please specify a valid --flash-path for the flash image\n");
+ exit(EXIT_FAILURE);
+ } else {
+ vrpmb_load_flash_image(&rpmb, flash_path);
+ }
+
if (!socket_path && socket_fd < 0) {
g_printerr("Please specify either --fd or --socket-path\n");
exit(EXIT_FAILURE);
--
2.20.1
next prev parent reply other threads:[~2020-09-25 12:57 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-09-25 12:51 [RFC PATCH 00/19] vhost-user-rpmb (Replay Protected Memory Block) Alex Bennée
2020-09-25 12:51 ` [RFC PATCH 01/19] tools/virtiofsd: add support for --socket-group Alex Bennée
2020-10-07 10:48 ` Dr. David Alan Gilbert
2020-09-25 12:51 ` [RFC PATCH 02/19] hw/block: add boilerplate for vhost-user-rpmb device Alex Bennée
2020-09-25 12:51 ` [RFC PATCH 03/19] hw/virtio: move virtio-pci.h into shared include space Alex Bennée
2020-09-25 12:51 ` [RFC PATCH 04/19] hw/block: add vhost-user-rpmb-pci boilerplate Alex Bennée
2020-09-25 12:51 ` [RFC PATCH 05/19] virtio-pci: add notification trace points Alex Bennée
2020-09-25 13:06 ` Philippe Mathieu-Daudé
2020-09-25 12:51 ` [RFC PATCH 06/19] tools/vhost-user-rpmb: add boilerplate and initial main Alex Bennée
2020-09-25 12:51 ` [RFC PATCH 07/19] tools/vhost-user-rpmb: implement --print-capabilities Alex Bennée
2020-09-25 12:51 ` [RFC PATCH 08/19] tools/vhost-user-rpmb: connect to fd and instantiate basic run loop Alex Bennée
2020-09-25 12:51 ` [RFC PATCH 09/19] tools/vhost-user-rpmb: add a --verbose/debug flags for logging Alex Bennée
2020-09-25 12:51 ` [RFC PATCH 10/19] tools/vhost-user-rpmb: handle shutdown and SIGINT/SIGHUP cleanly Alex Bennée
2020-09-25 12:51 ` Alex Bennée [this message]
2020-09-25 12:51 ` [RFC PATCH 12/19] tools/vhost-user-rpmb: import hmac_sha256 functions Alex Bennée
2020-09-25 12:51 ` [RFC PATCH 13/19] tools/vhost-user-rpmb: implement the PROGRAM_KEY handshake Alex Bennée
2020-09-25 12:51 ` [RFC PATCH 14/19] tools/vhost-user-rpmb: implement VIRTIO_RPMB_REQ_GET_WRITE_COUNTER Alex Bennée
2020-09-25 12:51 ` [RFC PATCH 15/19] tools/vhost-user-rpmb: implement VIRTIO_RPMB_REQ_DATA_WRITE Alex Bennée
2020-09-28 13:52 ` Joakim Bech
2020-09-28 14:56 ` Alex Bennée
2020-09-28 15:18 ` Joakim Bech
2020-09-25 12:51 ` [RFC PATCH 16/19] tools/vhost-user-rpmb: implement VIRTIO_RPMB_REQ_DATA_READ Alex Bennée
2020-09-25 12:51 ` [RFC PATCH 17/19] tools/vhost-user-rpmb: add key persistence Alex Bennée
2020-09-25 12:51 ` [RFC PATCH 18/19] tools/vhost-user-rpmb: allow setting of the write_count Alex Bennée
2020-09-25 12:51 ` [RFC PATCH 19/19] docs: add a man page for vhost-user-rpmb Alex Bennée
2020-09-25 14:07 ` [RFC PATCH 00/19] vhost-user-rpmb (Replay Protected Memory Block) no-reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200925125147.26943-12-alex.bennee@linaro.org \
--to=alex.bennee@linaro.org \
--cc=Matti.Moell@opensynergy.com \
--cc=arnd@linaro.org \
--cc=bing.zhu@intel.com \
--cc=hmo@opensynergy.com \
--cc=ilias.apalodimas@linaro.org \
--cc=jean-philippe@linaro.org \
--cc=joakim.bech@linaro.org \
--cc=maxim.uvarov@linaro.org \
--cc=qemu-devel@nongnu.org \
--cc=stratos-dev@op-lists.linaro.org \
--cc=takahiro.akashi@linaro.org \
--cc=tomas.winkler@intel.com \
--cc=virtualization@lists.linuxfoundation.org \
--cc=yang.huang@intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).