From patchwork Wed Jun 27 20:23:01 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eduardo Habkost X-Patchwork-Id: 167742 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 1326BB6FAB for ; Thu, 28 Jun 2012 06:47:36 +1000 (EST) Received: from localhost ([::1]:37601 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Sjylm-0005Ox-MH for incoming@patchwork.ozlabs.org; Wed, 27 Jun 2012 16:23:22 -0400 Received: from eggs.gnu.org ([208.118.235.92]:56794) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Sjyl3-0003El-EN for qemu-devel@nongnu.org; Wed, 27 Jun 2012 16:22:38 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Sjykx-0003L2-Js for qemu-devel@nongnu.org; Wed, 27 Jun 2012 16:22:37 -0400 Received: from mx1.redhat.com ([209.132.183.28]:55068) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Sjykx-0003Iq-9K for qemu-devel@nongnu.org; Wed, 27 Jun 2012 16:22:31 -0400 Received: from int-mx12.intmail.prod.int.phx2.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q5RKMS6o011416 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 27 Jun 2012 16:22:28 -0400 Received: from blackpad.lan.raisama.net (vpn1-4-120.gru2.redhat.com [10.97.4.120]) by int-mx12.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q5RKMR9x021677; Wed, 27 Jun 2012 16:22:28 -0400 Received: by blackpad.lan.raisama.net (Postfix, from userid 500) id 5983A203BA7; Wed, 27 Jun 2012 17:23:13 -0300 (BRT) From: Eduardo Habkost To: qemu-devel@nongnu.org Date: Wed, 27 Jun 2012 17:23:01 -0300 Message-Id: <1340828587-15201-6-git-send-email-ehabkost@redhat.com> In-Reply-To: <1340828587-15201-1-git-send-email-ehabkost@redhat.com> References: <1340828587-15201-1-git-send-email-ehabkost@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.25 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.132.183.28 Cc: Isaku Yamahata , Michael Tokarev , Gleb Natapov Subject: [Qemu-devel] [PATCH 05/11] acpi_table_add(): extract ACPI header creation to separate function 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 No behavior change, just code movement. Signed-off-by: Eduardo Habkost --- hw/acpi.c | 157 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 84 insertions(+), 73 deletions(-) diff --git a/hw/acpi.c b/hw/acpi.c index b5f0531..16e2065 100644 --- a/hw/acpi.c +++ b/hw/acpi.c @@ -81,81 +81,14 @@ static void init_acpi_tables(void) } } -/* XXX fixme: this function uses obsolete argument parsing interface */ -int acpi_table_add(const char *t) +static int acpi_make_table_header(const char *t, bool has_header, char *f, + size_t qemu_len) { - char buf[1024], *p, *f; - unsigned long val; - size_t start, allen; - size_t qemu_len, acpi_len; - bool has_header; - int changed; - int r; struct acpi_table_header hdr; - - r = 0; - r |= get_param_value(buf, sizeof(buf), "data", t) ? 1 : 0; - r |= get_param_value(buf, sizeof(buf), "file", t) ? 2 : 0; - switch (r) { - case 0: - buf[0] = '\0'; - /* fallthrough for default behavior */ - case 1: - has_header = false; - break; - case 2: - has_header = true; - break; - default: - fprintf(stderr, "acpitable: both data and file are specified\n"); - return -1; - } - - init_acpi_tables(); - - allen = acpi_tables_len; - start = allen; - acpi_tables = g_realloc(acpi_tables, start + ACPI_TABLE_HDR_SIZE); - allen += has_header ? ACPI_TABLE_PFX_SIZE : ACPI_TABLE_HDR_SIZE; - - /* now read in the data files, reallocating buffer as needed */ - - for (f = strtok(buf, ":"); f; f = strtok(NULL, ":")) { - int fd = open(f, O_RDONLY); - - if (fd < 0) { - fprintf(stderr, "can't open file %s: %s\n", f, strerror(errno)); - return -1; - } - - for (;;) { - char data[8192]; - r = read(fd, data, sizeof(data)); - if (r == 0) { - break; - } else if (r > 0) { - acpi_tables = g_realloc(acpi_tables, allen + r); - memcpy(acpi_tables + allen, data, r); - allen += r; - } else if (errno != EINTR) { - fprintf(stderr, "can't read file %s: %s\n", - f, strerror(errno)); - close(fd); - return -1; - } - } - - close(fd); - } - - /* now fill in the header fields */ - - f = acpi_tables + start; /* start of the table */ - - /* length of the whole table, including our prefix */ - qemu_len = allen - start; - - changed = 0; + size_t acpi_len; + char buf[1024], *p; + unsigned long val; + int changed = 0; /* copy the header to temp place to align the fields */ memcpy(&hdr, has_header ? f : dfl_hdr, ACPI_TABLE_HDR_SIZE); @@ -249,6 +182,84 @@ int acpi_table_add(const char *t) acpi_checksum((uint8_t *)f + ACPI_TABLE_PFX_SIZE, acpi_len); } + return 0; +} + +/* XXX fixme: this function uses obsolete argument parsing interface */ +int acpi_table_add(const char *t) +{ + char buf[1024], *f; + size_t start, allen; + size_t qemu_len; + bool has_header; + int r; + + r = 0; + r |= get_param_value(buf, sizeof(buf), "data", t) ? 1 : 0; + r |= get_param_value(buf, sizeof(buf), "file", t) ? 2 : 0; + switch (r) { + case 0: + buf[0] = '\0'; + /* fallthrough for default behavior */ + case 1: + has_header = false; + break; + case 2: + has_header = true; + break; + default: + fprintf(stderr, "acpitable: both data and file are specified\n"); + return -1; + } + + init_acpi_tables(); + + allen = acpi_tables_len; + start = allen; + acpi_tables = g_realloc(acpi_tables, start + ACPI_TABLE_HDR_SIZE); + allen += has_header ? ACPI_TABLE_PFX_SIZE : ACPI_TABLE_HDR_SIZE; + + /* now read in the data files, reallocating buffer as needed */ + + for (f = strtok(buf, ":"); f; f = strtok(NULL, ":")) { + int fd = open(f, O_RDONLY); + + if (fd < 0) { + fprintf(stderr, "can't open file %s: %s\n", f, strerror(errno)); + return -1; + } + + for (;;) { + char data[8192]; + r = read(fd, data, sizeof(data)); + if (r == 0) { + break; + } else if (r > 0) { + acpi_tables = g_realloc(acpi_tables, allen + r); + memcpy(acpi_tables + allen, data, r); + allen += r; + } else if (errno != EINTR) { + fprintf(stderr, "can't read file %s: %s\n", + f, strerror(errno)); + close(fd); + return -1; + } + } + + close(fd); + } + + /* now fill in the header fields */ + + f = acpi_tables + start; /* start of the table */ + + /* length of the whole table, including our prefix */ + qemu_len = allen - start; + + if (acpi_make_table_header(t, has_header, f, qemu_len) < 0) { + return -1; + } + /* increase number of tables */ (*(uint16_t *)acpi_tables) = cpu_to_le32(le32_to_cpu(*(uint16_t *)acpi_tables) + 1);