9ae3a8
From a577ed3eaa3c3ba84133d9bea1907c69c01063ad Mon Sep 17 00:00:00 2001
9ae3a8
From: Laszlo Ersek <lersek@redhat.com>
9ae3a8
Date: Sat, 11 Jan 2014 18:00:06 +0100
9ae3a8
Subject: [PATCH 16/22] i440fx-test: generate temporary firmware blob
9ae3a8
9ae3a8
RH-Author: Laszlo Ersek <lersek@redhat.com>
9ae3a8
Message-id: <1389463208-6278-17-git-send-email-lersek@redhat.com>
9ae3a8
Patchwork-id: 56629
9ae3a8
O-Subject: [RHEL-7.0 qemu-kvm PATCH 16/18] i440fx-test: generate temporary firmware blob
9ae3a8
Bugzilla: 1032346
9ae3a8
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
9ae3a8
RH-Acked-by: Amos Kong <akong@redhat.com>
9ae3a8
RH-Acked-by: Andrew Jones <drjones@redhat.com>
9ae3a8
9ae3a8
The blob is 64K in size and contains 0x00..0xFF repeatedly.
9ae3a8
9ae3a8
The client code added to main() wouldn't make much sense in the long term.
9ae3a8
It helps with debugging and it silences gcc about create_blob_file() being
9ae3a8
unused, and we'll replace it in the next patch anyway.
9ae3a8
9ae3a8
Signed-off-by: Laszlo Ersek <lersek@redhat.com>
9ae3a8
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
9ae3a8
(cherry picked from commit 27d59ccd89a5b112e5a5804250440ea30dbfb891)
9ae3a8
---
9ae3a8
 tests/i440fx-test.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++
9ae3a8
 1 file changed, 60 insertions(+)
9ae3a8
9ae3a8
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
9ae3a8
---
9ae3a8
 tests/i440fx-test.c |   60 +++++++++++++++++++++++++++++++++++++++++++++++++++
9ae3a8
 1 files changed, 60 insertions(+), 0 deletions(-)
9ae3a8
9ae3a8
diff --git a/tests/i440fx-test.c b/tests/i440fx-test.c
9ae3a8
index 3962bca..b6e0cd3 100644
9ae3a8
--- a/tests/i440fx-test.c
9ae3a8
+++ b/tests/i440fx-test.c
9ae3a8
@@ -20,6 +20,11 @@
9ae3a8
 
9ae3a8
 #include <glib.h>
9ae3a8
 #include <string.h>
9ae3a8
+#include <stdio.h>
9ae3a8
+#include <unistd.h>
9ae3a8
+#include <errno.h>
9ae3a8
+#include <sys/mman.h>
9ae3a8
+#include <stdlib.h>
9ae3a8
 
9ae3a8
 #define BROKEN 1
9ae3a8
 
9ae3a8
@@ -272,13 +277,68 @@ static void test_i440fx_pam(gconstpointer opaque)
9ae3a8
     qtest_end();
9ae3a8
 }
9ae3a8
 
9ae3a8
+#define BLOB_SIZE ((size_t)65536)
9ae3a8
+
9ae3a8
+/* Create a blob file, and return its absolute pathname as a dynamically
9ae3a8
+ * allocated string.
9ae3a8
+ * The file is closed before the function returns.
9ae3a8
+ * In case of error, NULL is returned. The function prints the error message.
9ae3a8
+ */
9ae3a8
+static char *create_blob_file(void)
9ae3a8
+{
9ae3a8
+    int ret, fd;
9ae3a8
+    char *pathname;
9ae3a8
+    GError *error = NULL;
9ae3a8
+
9ae3a8
+    ret = -1;
9ae3a8
+    fd = g_file_open_tmp("blob_XXXXXX", &pathname, &error);
9ae3a8
+    if (fd == -1) {
9ae3a8
+        fprintf(stderr, "unable to create blob file: %s\n", error->message);
9ae3a8
+        g_error_free(error);
9ae3a8
+    } else {
9ae3a8
+        if (ftruncate(fd, BLOB_SIZE) == -1) {
9ae3a8
+            fprintf(stderr, "ftruncate(\"%s\", %zu): %s\n", pathname,
9ae3a8
+                    BLOB_SIZE, strerror(errno));
9ae3a8
+        } else {
9ae3a8
+            void *buf;
9ae3a8
+
9ae3a8
+            buf = mmap(NULL, BLOB_SIZE, PROT_WRITE, MAP_SHARED, fd, 0);
9ae3a8
+            if (buf == MAP_FAILED) {
9ae3a8
+                fprintf(stderr, "mmap(\"%s\", %zu): %s\n", pathname, BLOB_SIZE,
9ae3a8
+                        strerror(errno));
9ae3a8
+            } else {
9ae3a8
+                size_t i;
9ae3a8
+
9ae3a8
+                for (i = 0; i < BLOB_SIZE; ++i) {
9ae3a8
+                    ((uint8_t *)buf)[i] = i;
9ae3a8
+                }
9ae3a8
+                munmap(buf, BLOB_SIZE);
9ae3a8
+                ret = 0;
9ae3a8
+            }
9ae3a8
+        }
9ae3a8
+        close(fd);
9ae3a8
+        if (ret == -1) {
9ae3a8
+            unlink(pathname);
9ae3a8
+            g_free(pathname);
9ae3a8
+        }
9ae3a8
+    }
9ae3a8
+
9ae3a8
+    return ret == -1 ? NULL : pathname;
9ae3a8
+}
9ae3a8
+
9ae3a8
 int main(int argc, char **argv)
9ae3a8
 {
9ae3a8
+    char *fw_pathname;
9ae3a8
     TestData data;
9ae3a8
     int ret;
9ae3a8
 
9ae3a8
     g_test_init(&argc, &argv, NULL);
9ae3a8
 
9ae3a8
+    fw_pathname = create_blob_file();
9ae3a8
+    g_assert(fw_pathname != NULL);
9ae3a8
+    unlink(fw_pathname);
9ae3a8
+    g_free(fw_pathname);
9ae3a8
+
9ae3a8
     data.num_cpus = 1;
9ae3a8
 
9ae3a8
     g_test_add_data_func("/i440fx/defaults", &data, test_i440fx_defaults);
9ae3a8
-- 
9ae3a8
1.7.1
9ae3a8