From patchwork Wed May 4 11:51:34 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nico Huber X-Patchwork-Id: 618382 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail.coreboot.org (mail.coreboot.org [80.81.252.135]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3r0Gf21ljPz9sCY for ; Wed, 4 May 2016 21:53:45 +1000 (AEST) Received: from [127.0.0.1] (helo=ra.coresystems.de) by mail.coreboot.org with esmtp (Exim 4.86_2) (envelope-from ) id 1axvLo-0006mM-EL; Wed, 04 May 2016 13:52:20 +0200 Received: from a.mx.secunet.com ([62.96.220.36]) by mail.coreboot.org with esmtps (TLSv1:DHE-RSA-AES256-SHA:256) (Exim 4.86_2) (envelope-from ) id 1axvLT-0006jB-IB for flashrom@flashrom.org; Wed, 04 May 2016 13:52:16 +0200 Received: from localhost (alg1 [127.0.0.1]) by a.mx.secunet.com (Postfix) with ESMTP id EE21D1A06BD for ; Wed, 4 May 2016 13:51:56 +0200 (CEST) X-Virus-Scanned: by secunet Received: from a.mx.secunet.com ([127.0.0.1]) by localhost (a.mx.secunet.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id P7nwbKUsIjBr for ; Wed, 4 May 2016 13:51:55 +0200 (CEST) Received: from mail-essen-01.secunet.de (unknown [10.53.40.204]) by a.mx.secunet.com (Postfix) with ESMTP id 5CF141A04B1 for ; Wed, 4 May 2016 13:51:55 +0200 (CEST) Received: from schawa.localdomain (10.182.9.137) by mail-essen-01.secunet.de (10.53.40.204) with Microsoft SMTP Server (TLS) id 14.3.279.2; Wed, 4 May 2016 13:51:56 +0200 From: Nico Huber To: Date: Wed, 4 May 2016 13:51:34 +0200 Message-ID: <1462362706-31216-3-git-send-email-nico.huber@secunet.com> X-Mailer: git-send-email 2.7.0 In-Reply-To: <1462362706-31216-1-git-send-email-nico.huber@secunet.com> References: <1462362706-31216-1-git-send-email-nico.huber@secunet.com> MIME-Version: 1.0 X-Originating-IP: [10.182.9.137] X-G-Data-MailSecurity-for-Exchange-SpamLevel: 0 X-G-Data-MailSecurity-for-Exchange-SpamFilter: 0; 1; str=0001.0A0C0205.5729E25C.00A9, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0 X-G-Data-MailSecurity-for-Exchange-State: 0 X-G-Data-MailSecurity-for-Exchange-Error: 0 X-G-Data-MailSecurity-for-Exchange-Sender: 32 X-G-Data-MailSecurity-for-Exchange-Server: d65e63f7-5c15-413f-8f63-c0d707471c93 X-EXCLAIMER-MD-CONFIG: 2c86f778-e09b-4440-8b15-867914633a10 X-G-Data-MailSecurity-for-Exchange-Guid: 1D003695-8244-4430-874A-EF6400753DBF X-Spam-Score: -1.6 (-) Subject: [flashrom] [PATCH 02/14] Give layouts their own type X-BeenThere: flashrom@flashrom.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: flashrom discussion and development mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: flashrom-bounces@flashrom.org Sender: "flashrom" X-Duff: Orig. Duff, Duff Lite, Duff Dry, Duff Dark, Raspberry Duff, Lady Duff, Red Duff, Tartar Control Duff Introduce `struct fl_layout` and refactor layout.c a little, so we can reuse the layout from there and have other sources of layouts beside it. I didn't want to clutter up flash.h any more. So things went into a new layout.h. Signed-off-by: Nico Huber --- flash.h | 10 ++------ layout.c | 80 ++++++++++++++++++++++++++++++---------------------------------- layout.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 50 deletions(-) create mode 100644 layout.h diff --git a/flash.h b/flash.h index da049d1..bf381cf 100644 --- a/flash.h +++ b/flash.h @@ -37,6 +37,8 @@ #undef max #endif +#include "layout.h" + #define ERROR_PTR ((void*)-1) /* Error codes */ @@ -47,14 +49,6 @@ typedef uintptr_t chipaddr; #define PRIxPTR_WIDTH ((int)(sizeof(uintptr_t)*2)) -/* Types and macros regarding the maximum flash space size supported by generic code. */ -typedef uint32_t chipoff_t; /* Able to store any addressable offset within a supported flash memory. */ -typedef uint32_t chipsize_t; /* Able to store the number of bytes of any supported flash memory. */ -#define FL_MAX_CHIPOFF_BITS (24) -#define FL_MAX_CHIPOFF ((chipoff_t)(1ULL< #include "flash.h" #include "programmer.h" +#include "layout.h" -#define MAX_ROMLAYOUT 32 - -typedef struct { - chipoff_t start; - chipoff_t end; - unsigned int included; - char name[256]; -} romentry_t; - -/* rom_entries store the entries specified in a layout file and associated run-time data */ -static romentry_t rom_entries[MAX_ROMLAYOUT]; -static int num_rom_entries = 0; /* the number of successfully parsed rom_entries */ +struct fl_romentry entries[MAX_ROMLAYOUT]; +static struct fl_layout layout = { entries, 0 }; /* include_args holds the arguments specified at the command line with -i. They must be processed at some point - * so that desired regions are marked as "included" in the rom_entries list. */ + * so that desired regions are marked as "included" in the layout. */ static char *include_args[MAX_ROMLAYOUT]; static int num_include_args = 0; /* the number of valid include_args. */ +const struct fl_layout *get_global_layout(void) +{ + return &layout; +} + #ifndef __LIBPAYLOAD__ int read_romlayout(const char *name) { @@ -62,13 +58,13 @@ int read_romlayout(const char *name) while (!feof(romlayout)) { char *tstr1, *tstr2; - if (num_rom_entries >= MAX_ROMLAYOUT) { + if (layout.num_entries >= MAX_ROMLAYOUT) { msg_gerr("Maximum number of ROM images (%i) in layout " "file reached.\n", MAX_ROMLAYOUT); (void)fclose(romlayout); return 1; } - if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, rom_entries[num_rom_entries].name)) + if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, layout.entries[layout.num_entries].name)) continue; #if 0 // fscanf does not like arbitrary comments like that :( later @@ -83,16 +79,16 @@ int read_romlayout(const char *name) (void)fclose(romlayout); return 1; } - rom_entries[num_rom_entries].start = strtol(tstr1, (char **)NULL, 16); - rom_entries[num_rom_entries].end = strtol(tstr2, (char **)NULL, 16); - rom_entries[num_rom_entries].included = 0; - num_rom_entries++; + layout.entries[layout.num_entries].start = strtol(tstr1, (char **)NULL, 16); + layout.entries[layout.num_entries].end = strtol(tstr2, (char **)NULL, 16); + layout.entries[layout.num_entries].included = 0; + layout.num_entries++; } - for (i = 0; i < num_rom_entries; i++) { + for (i = 0; i < layout.num_entries; i++) { msg_gdbg("romlayout %08x - %08x named %s\n", - rom_entries[i].start, - rom_entries[i].end, rom_entries[i].name); + layout.entries[i].start, + layout.entries[i].end, layout.entries[i].name); } (void)fclose(romlayout); @@ -140,13 +136,13 @@ static int find_romentry(char *name) { int i; - if (num_rom_entries == 0) + if (layout.num_entries == 0) return -1; msg_gspew("Looking for region \"%s\"... ", name); - for (i = 0; i < num_rom_entries; i++) { - if (!strcmp(rom_entries[i].name, name)) { - rom_entries[i].included = 1; + for (i = 0; i < layout.num_entries; i++) { + if (!strcmp(layout.entries[i].name, name)) { + layout.entries[i].included = 1; msg_gspew("found.\n"); return i; } @@ -167,7 +163,7 @@ int process_include_args(void) return 0; /* User has specified an area, but no layout file is loaded. */ - if (num_rom_entries == 0) { + if (layout.num_entries == 0) { msg_gerr("Region requested (with -i \"%s\"), " "but no layout data is available.\n", include_args[0]); @@ -200,22 +196,22 @@ void layout_cleanup(void) } num_include_args = 0; - for (i = 0; i < num_rom_entries; i++) { - rom_entries[i].included = 0; + for (i = 0; i < layout.num_entries; i++) { + layout.entries[i].included = 0; } - num_rom_entries = 0; + layout.num_entries = 0; } -romentry_t *get_next_included_romentry(unsigned int start) +struct fl_romentry *get_next_included_romentry(unsigned int start) { int i; unsigned int best_start = UINT_MAX; - romentry_t *best_entry = NULL; - romentry_t *cur; + struct fl_romentry *best_entry = NULL; + struct fl_romentry *cur; /* First come, first serve for overlapping regions. */ - for (i = 0; i < num_rom_entries; i++) { - cur = &rom_entries[i]; + for (i = 0; i < layout.num_entries; i++) { + cur = &layout.entries[i]; if (!cur->included) continue; /* Already past the current entry? */ @@ -240,16 +236,16 @@ int normalize_romentries(const struct flashctx *flash) int ret = 0; int i; - for (i = 0; i < num_rom_entries; i++) { - if (rom_entries[i].start >= total_size || rom_entries[i].end >= total_size) { + for (i = 0; i < layout.num_entries; i++) { + if (layout.entries[i].start >= total_size || layout.entries[i].end >= total_size) { msg_gwarn("Warning: Address range of region \"%s\" exceeds the current chip's " - "address space.\n", rom_entries[i].name); - if (rom_entries[i].included) + "address space.\n", layout.entries[i].name); + if (layout.entries[i].included) ret = 1; } - if (rom_entries[i].start > rom_entries[i].end) { + if (layout.entries[i].start > layout.entries[i].end) { msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n", - rom_entries[i].name); + layout.entries[i].name); ret = 1; } } @@ -281,7 +277,7 @@ static int copy_old_content(struct flashctx *flash, int oldcontents_valid, uint8 int build_new_image(struct flashctx *flash, bool oldcontents_valid, uint8_t *oldcontents, uint8_t *newcontents) { unsigned int start = 0; - romentry_t *entry; + struct fl_romentry *entry; unsigned int size = flash->chip->total_size * 1024; /* If no regions were specified for inclusion, assume diff --git a/layout.h b/layout.h new file mode 100644 index 0000000..3aebe8a --- /dev/null +++ b/layout.h @@ -0,0 +1,59 @@ +/* + * This file is part of the flashrom project. + * + * Copyright (C) 2005-2008 coresystems GmbH + * (Written by Stefan Reinauer for coresystems GmbH) + * Copyright (C) 2011-2013 Stefan Tauner + * Copyright (C) 2016 secunet Security Networks AG + * (Written by Nico Huber for secunet) + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __LAYOUT_H__ +#define __LAYOUT_H__ 1 + +/* Types and macros regarding the maximum flash space size supported by generic code. */ +typedef uint32_t chipoff_t; /* Able to store any addressable offset within a supported flash memory. */ +typedef uint32_t chipsize_t; /* Able to store the number of bytes of any supported flash memory. */ +#define FL_MAX_CHIPOFF_BITS (24) +#define FL_MAX_CHIPOFF ((chipoff_t)(1ULL<