From patchwork Tue Aug 14 08:45:25 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colin Ian King X-Patchwork-Id: 957384 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=fwts-devel-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41qR4t4lyZz9s7c; Tue, 14 Aug 2018 18:45:34 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1fpUxD-0004al-TI; Tue, 14 Aug 2018 08:45:27 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.86_2) (envelope-from ) id 1fpUxD-0004aZ-5f for fwts-devel@lists.ubuntu.com; Tue, 14 Aug 2018 08:45:27 +0000 Received: from 1.general.cking.uk.vpn ([10.172.193.212] helo=localhost) by youngberry.canonical.com with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.76) (envelope-from ) id 1fpUxC-00011I-T1; Tue, 14 Aug 2018 08:45:26 +0000 From: Colin King To: fwts-devel@lists.ubuntu.com Subject: [PATCH 1/2] Add MIN, MAX and ARRAY_SIZE macros to fwts.h Date: Tue, 14 Aug 2018 09:45:25 +0100 Message-Id: <20180814084526.18006-1-colin.king@canonical.com> X-Mailer: git-send-email 2.17.1 MIME-Version: 1.0 X-BeenThere: fwts-devel@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Firmware Test Suite Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: fwts-devel-bounces@lists.ubuntu.com Sender: "fwts-devel" From: Colin Ian King These are general purpose enough helper macros to be added to fwts.h. Prefix them with FWTS_ and update fwts_coreboot_cbmem.c to use these instead. Signed-off-by: Colin Ian King Acked-by: Alex Hung Acked-by: Ivan Hu --- src/lib/include/fwts.h | 7 +++++++ src/lib/src/fwts_coreboot_cbmem.c | 11 ++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/lib/include/fwts.h b/src/lib/include/fwts.h index a9115da7..62834ec3 100644 --- a/src/lib/include/fwts.h +++ b/src/lib/include/fwts.h @@ -23,6 +23,13 @@ #include "config.h" +/* + * Helper macros, minimum, maximum and array size + */ +#define FWTS_MIN(a, b) ((a) < (b) ? (a) : (b)) +#define FWTS_MAX(a, b) ((a) > (b) ? (a) : (b)) +#define FWTS_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + /* * convert version to a large integer for easier comparison */ diff --git a/src/lib/src/fwts_coreboot_cbmem.c b/src/lib/src/fwts_coreboot_cbmem.c index 3add0871..214fa55e 100644 --- a/src/lib/src/fwts_coreboot_cbmem.c +++ b/src/lib/src/fwts_coreboot_cbmem.c @@ -38,9 +38,6 @@ #define LB_TAG_CBMEM_CONSOLE 0x0017 #define LB_TAG_FORWARD 0x0011 -#define MIN(a,b) ((a)<(b) ? (a):(b)) -#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) - struct lb_record { uint32_t tag; /* tag ID */ uint32_t size; /* size of record (in bytes) */ @@ -277,10 +274,10 @@ static ssize_t memconsole_coreboot_read(struct cbmem_console *con, char *buf, si seg[0] = (struct seg){.phys = cursor, .len = count - cursor}; seg[1] = (struct seg){.phys = 0, .len = cursor}; } else { - seg[0] = (struct seg){.phys = 0, .len = MIN(cursor, count)}; + seg[0] = (struct seg){.phys = 0, .len = FWTS_MIN(cursor, count)}; } - for (i = 0; i < ARRAY_SIZE(seg) && count > done; i++) { + for (i = 0; i < FWTS_ARRAY_SIZE(seg) && count > done; i++) { done += memory_read_from_buffer(buf + done, count - done, &pos, con->body + seg[i].phys, seg[i].len); pos -= seg[i].len; @@ -300,11 +297,11 @@ char *fwts_coreboot_cbmem_console_dump(void) ssize_t count; /* Find and parse coreboot table */ - for (j = 0; j < ARRAY_SIZE(possible_base_addresses); j++) { + for (j = 0; j < FWTS_ARRAY_SIZE(possible_base_addresses); j++) { if (!parse_cbtable(possible_base_addresses[j], 0, &cbmem_console_addr)) break; } - if (j == ARRAY_SIZE(possible_base_addresses)) + if (j == FWTS_ARRAY_SIZE(possible_base_addresses)) return NULL; console_p = map_memory(cbmem_console_addr, sizeof(*console_p)); From patchwork Tue Aug 14 08:45:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Colin Ian King X-Patchwork-Id: 957385 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=lists.ubuntu.com (client-ip=91.189.94.19; helo=huckleberry.canonical.com; envelope-from=fwts-devel-bounces@lists.ubuntu.com; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=canonical.com Received: from huckleberry.canonical.com (huckleberry.canonical.com [91.189.94.19]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41qR4t4lvQz9rxx; Tue, 14 Aug 2018 18:45:34 +1000 (AEST) Received: from localhost ([127.0.0.1] helo=huckleberry.canonical.com) by huckleberry.canonical.com with esmtp (Exim 4.86_2) (envelope-from ) id 1fpUxF-0004bB-VA; Tue, 14 Aug 2018 08:45:29 +0000 Received: from youngberry.canonical.com ([91.189.89.112]) by huckleberry.canonical.com with esmtps (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.86_2) (envelope-from ) id 1fpUxD-0004af-Ku for fwts-devel@lists.ubuntu.com; Tue, 14 Aug 2018 08:45:27 +0000 Received: from 1.general.cking.uk.vpn ([10.172.193.212] helo=localhost) by youngberry.canonical.com with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.76) (envelope-from ) id 1fpUxD-00011M-Bl; Tue, 14 Aug 2018 08:45:27 +0000 From: Colin King To: fwts-devel@lists.ubuntu.com Subject: [PATCH 2/2] fwts_coreboot_cbmem: fwts coding style changes, use off_t for addresses Date: Tue, 14 Aug 2018 09:45:26 +0100 Message-Id: <20180814084526.18006-2-colin.king@canonical.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180814084526.18006-1-colin.king@canonical.com> References: <20180814084526.18006-1-colin.king@canonical.com> MIME-Version: 1.0 X-BeenThere: fwts-devel@lists.ubuntu.com X-Mailman-Version: 2.1.20 Precedence: list List-Id: Firmware Test Suite Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: fwts-devel-bounces@lists.ubuntu.com Sender: "fwts-devel" From: Colin Ian King Some cosmetic changes to use the fwts coding style. Also replace uint64_t and unsigned long long with off_t for addresses that are memory mapped to make it 64/32 bit agnostic. Signed-off-by: Colin Ian King Acked-by: Alex Hung Acked-by: Ivan Hu --- src/lib/src/fwts_coreboot_cbmem.c | 56 ++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 19 deletions(-) diff --git a/src/lib/src/fwts_coreboot_cbmem.c b/src/lib/src/fwts_coreboot_cbmem.c index 214fa55e..456ac694 100644 --- a/src/lib/src/fwts_coreboot_cbmem.c +++ b/src/lib/src/fwts_coreboot_cbmem.c @@ -72,20 +72,18 @@ struct cbmem_console { } __attribute__ ((packed)); /* Return < 0 on error, 0 on success. */ -static int parse_cbtable(uint64_t address, size_t table_size, uint64_t *cbmen_console_addr); +static int parse_cbtable(const off_t address, const size_t table_size, off_t *cbmen_console_addr); -static void *map_memory(unsigned long long addr, size_t size) +static void *map_memory(const off_t addr, const size_t size) { void *mem; void *phy; phy = fwts_mmap(addr, size); - if (phy == FWTS_MAP_FAILED) return NULL; mem = malloc(size); - if (!mem) { fwts_munmap(phy, size); return NULL; @@ -117,7 +115,8 @@ static uint16_t ipchcksum(const void *addr, unsigned size) return (uint16_t) sum; } -/* This is a work-around for a nasty problem introduced by initially having +/* + * This is a work-around for a nasty problem introduced by initially having * pointer sized entries in the lb_cbmem_ref structures. This caused problems * on 64bit x86 systems because coreboot is 32bit on those systems. * When the problem was found, it was corrected, but there are a lot of @@ -136,8 +135,13 @@ static struct lb_cbmem_ref parse_cbmem_ref(const struct lb_cbmem_ref *cbmem_ref) return ret; } -/* Return < 0 on error, 0 on success, 1 if forwarding table entry found. */ -static int parse_cbtable_entries(const void *lbtable, size_t table_size, uint64_t *cbmem_console_addr) +/* + * Return < 0 on error, 0 on success, 1 if forwarding table entry found. + */ +static int parse_cbtable_entries( + const void *lbtable, + const size_t table_size, + off_t *cbmem_console_addr) { size_t i; const struct lb_record* lbr_p; @@ -147,7 +151,7 @@ static int parse_cbtable_entries(const void *lbtable, size_t table_size, uint64_ lbr_p = lbtable + i; switch (lbr_p->tag) { case LB_TAG_CBMEM_CONSOLE: { - *cbmem_console_addr = parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p).cbmem_addr; + *cbmem_console_addr = (off_t)parse_cbmem_ref((struct lb_cbmem_ref *) lbr_p).cbmem_addr; if (cbmem_console_addr) return 0; continue; @@ -176,8 +180,13 @@ static int parse_cbtable_entries(const void *lbtable, size_t table_size, uint64_ return forwarding_table_found; } -/* Return < 0 on error, 0 on success. */ -static int parse_cbtable(uint64_t address, size_t table_size, uint64_t *cbmem_console_table) +/* + * Return < 0 on error, 0 on success. + */ +static int parse_cbtable( + const off_t address, + const size_t table_size, + off_t *cbmem_console_table) { void *buf; size_t req_size; @@ -190,7 +199,6 @@ static int parse_cbtable(uint64_t address, size_t table_size, uint64_t *cbmem_co req_size = 4 * 1024; buf = map_memory(address, req_size); - if (!buf) return -1; @@ -220,7 +228,7 @@ static int parse_cbtable(uint64_t address, size_t table_size, uint64_t *cbmem_co continue; } - ret = parse_cbtable_entries(map,lbh->table_bytes, cbmem_console_table); + ret = parse_cbtable_entries(map, lbh->table_bytes, cbmem_console_table); /* Table parsing failed. */ if (ret < 0) { @@ -239,8 +247,12 @@ static int parse_cbtable(uint64_t address, size_t table_size, uint64_t *cbmem_co return -1; } -static ssize_t memory_read_from_buffer(void *to, size_t count, size_t *ppos, - const void *from, size_t available) +static ssize_t memory_read_from_buffer( + void *to, + size_t count, + size_t *ppos, + const void *from, + const size_t available) { size_t pos = *ppos; @@ -250,18 +262,24 @@ static ssize_t memory_read_from_buffer(void *to, size_t count, size_t *ppos, if (count > available - pos) count = available - pos; - memcpy(to, from+pos, count); + memcpy(to, from + pos, count); *ppos = pos + count; return count; } -static ssize_t memconsole_coreboot_read(struct cbmem_console *con, char *buf, size_t pos, size_t count) +static ssize_t memconsole_coreboot_read( + struct cbmem_console *con, + char *buf, + size_t pos, + size_t count) { uint32_t cursor = con->cursor & CURSOR_MASK; uint32_t flags = con->cursor & ~CURSOR_MASK; - struct seg { /* describes ring buffer segments in logical order */ + + /* describes ring buffer segments in logical order */ + struct seg { uint32_t phys; /* physical offset from start of mem buffer */ uint32_t len; /* length of segment */ } seg[2] = { { 0, 0 }, { 0, 0 } }; @@ -289,7 +307,7 @@ static ssize_t memconsole_coreboot_read(struct cbmem_console *con, char *buf, si char *fwts_coreboot_cbmem_console_dump(void) { unsigned int j; - uint64_t cbmem_console_addr; + off_t cbmem_console_addr; unsigned long long possible_base_addresses[] = { 0, 0xf0000 }; struct cbmem_console *console_p; struct cbmem_console *console; @@ -322,7 +340,7 @@ char *fwts_coreboot_cbmem_console_dump(void) return NULL; } - coreboot_log[console->size+1] = '\0'; + coreboot_log[console->size + 1] = '\0'; count = memconsole_coreboot_read(console, coreboot_log, 0, console->size); free(console);