From patchwork Thu Nov 28 18:09:14 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laszlo Ersek X-Patchwork-Id: 294994 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 9AB1A2C007A for ; Fri, 29 Nov 2013 05:11:37 +1100 (EST) Received: from localhost ([::1]:43417 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vm63r-0007Pe-0j for incoming@patchwork.ozlabs.org; Thu, 28 Nov 2013 13:11:35 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:44133) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vm61r-0004jg-0l for qemu-devel@nongnu.org; Thu, 28 Nov 2013 13:09:36 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Vm61i-0002cq-OV for qemu-devel@nongnu.org; Thu, 28 Nov 2013 13:09:30 -0500 Received: from mx1.redhat.com ([209.132.183.28]:6124) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Vm61i-0002br-F1 for qemu-devel@nongnu.org; Thu, 28 Nov 2013 13:09:22 -0500 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id rASI9LOr026847 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Thu, 28 Nov 2013 13:09:21 -0500 Received: from lacos-laptop-7.usersys.redhat.com (ovpn-116-104.ams2.redhat.com [10.36.116.104]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id rASI9HnK029081 for ; Thu, 28 Nov 2013 13:09:21 -0500 From: Laszlo Ersek To: qemu-devel@nongnu.org Date: Thu, 28 Nov 2013 19:09:14 +0100 Message-Id: <1385662155-15212-4-git-send-email-lersek@redhat.com> In-Reply-To: <1385662155-15212-1-git-send-email-lersek@redhat.com> References: <1385662155-15212-1-git-send-email-lersek@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.12 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH v2 3/4] i440fx-test: generate temporary firmware blob X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org The blob is 64K in size and contains 0x00..0xFF repeatedly. The client code added to main() wouldn't make much sense in the long term. It helps with debugging and it silences gcc about create_firmware() being unused, and we'll replace it in the next patch anyway. Signed-off-by: Laszlo Ersek --- tests/i440fx-test.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/tests/i440fx-test.c b/tests/i440fx-test.c index 3962bca..5506421 100644 --- a/tests/i440fx-test.c +++ b/tests/i440fx-test.c @@ -20,6 +20,11 @@ #include #include +#include +#include +#include +#include +#include #define BROKEN 1 @@ -272,13 +277,70 @@ static void test_i440fx_pam(gconstpointer opaque) qtest_end(); } +#define FW_SIZE ((size_t)65536) + +/* Create a temporary firmware blob, and return its absolute pathname as a + * dynamically allocated string. + * The file is closed before the function returns; it should be unlinked after + * use. + * In case of error, NULL is returned. The function prints the error message. + */ +static char *create_firmware(void) +{ + int ret, fd; + char *pathname; + GError *error = NULL; + + ret = -1; + fd = g_file_open_tmp("fw_blob_XXXXXX", &pathname, &error); + if (fd == -1) { + fprintf(stderr, "unable to create temporary firmware blob: %s\n", + error->message); + g_error_free(error); + } else { + if (ftruncate(fd, FW_SIZE) == -1) { + fprintf(stderr, "ftruncate(\"%s\", %zu): %s\n", pathname, FW_SIZE, + strerror(errno)); + } else { + void *buf; + + buf = mmap(NULL, FW_SIZE, PROT_WRITE, MAP_SHARED, fd, 0); + if (buf == MAP_FAILED) { + fprintf(stderr, "mmap(\"%s\", %zu): %s\n", pathname, FW_SIZE, + strerror(errno)); + } else { + size_t i; + + for (i = 0; i < FW_SIZE; ++i) { + ((char unsigned *)buf)[i] = i; + } + munmap(buf, FW_SIZE); + ret = 0; + } + } + close(fd); + if (ret == -1) { + unlink(pathname); + g_free(pathname); + } + } + + return ret == -1 ? NULL : pathname; +} + int main(int argc, char **argv) { + char *fw_pathname; TestData data; int ret; g_test_init(&argc, &argv, NULL); + fw_pathname = create_firmware(); + g_assert(fw_pathname != NULL); + unlink(fw_pathname); + g_free(fw_pathname); + data.num_cpus = 1; g_test_add_data_func("/i440fx/defaults", &data, test_i440fx_defaults);