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