From patchwork Wed Oct 14 04:40:09 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Cyril Bur X-Patchwork-Id: 529999 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42F7F1402B7 for ; Wed, 14 Oct 2015 15:42:03 +1100 (AEDT) Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 285181A080F for ; Wed, 14 Oct 2015 15:42:03 +1100 (AEDT) X-Original-To: skiboot@lists.ozlabs.org Delivered-To: skiboot@lists.ozlabs.org Received: from e23smtp02.au.ibm.com (e23smtp02.au.ibm.com [202.81.31.144]) (using TLSv1 with cipher CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 6A1F71A08DA for ; Wed, 14 Oct 2015 15:41:53 +1100 (AEDT) Received: from /spool/local by e23smtp02.au.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Wed, 14 Oct 2015 14:41:51 +1000 Received: from d23dlp01.au.ibm.com (202.81.31.203) by e23smtp02.au.ibm.com (202.81.31.208) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Wed, 14 Oct 2015 14:41:49 +1000 X-Helo: d23dlp01.au.ibm.com X-MailFrom: cyril.bur@au1.ibm.com X-RcptTo: skiboot@lists.ozlabs.org Received: from d23relay10.au.ibm.com (d23relay10.au.ibm.com [9.190.26.77]) by d23dlp01.au.ibm.com (Postfix) with ESMTP id 6AEE62CE8054 for ; Wed, 14 Oct 2015 15:41:49 +1100 (EST) Received: from d23av04.au.ibm.com (d23av04.au.ibm.com [9.190.235.139]) by d23relay10.au.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id t9E4ff2b51314752 for ; Wed, 14 Oct 2015 15:41:49 +1100 Received: from d23av04.au.ibm.com (localhost [127.0.0.1]) by d23av04.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVout) with ESMTP id t9E4fHT0011831 for ; Wed, 14 Oct 2015 15:41:17 +1100 Received: from ozlabs.au.ibm.com (ozlabs.au.ibm.com [9.192.253.14]) by d23av04.au.ibm.com (8.14.4/8.14.4/NCO v10.0 AVin) with ESMTP id t9E4fGJl011331 for ; Wed, 14 Oct 2015 15:41:16 +1100 Received: from camb691.ozlabs.ibm.com (haven.au.ibm.com [9.192.254.114]) (using TLSv1.2 with cipher AES128-SHA256 (128/128 bits)) (No client certificate requested) by ozlabs.au.ibm.com (Postfix) with ESMTPSA id A8E84A03E1 for ; Wed, 14 Oct 2015 15:40:42 +1100 (AEDT) From: Cyril Bur To: skiboot@lists.ozlabs.org Date: Wed, 14 Oct 2015 15:40:09 +1100 Message-Id: <1444797610-32428-7-git-send-email-cyril.bur@au1.ibm.com> X-Mailer: git-send-email 2.6.1 In-Reply-To: <1444797610-32428-1-git-send-email-cyril.bur@au1.ibm.com> References: <1444797610-32428-1-git-send-email-cyril.bur@au1.ibm.com> X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 15101404-0005-0000-0000-00000295FED2 Subject: [Skiboot] [PATCH 6/7] external/gard: Make use of the common/ flash reading code X-BeenThere: skiboot@lists.ozlabs.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Mailing list for skiboot development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: skiboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Skiboot" Signed-off-by: Cyril Bur --- external/gard/gard.c | 148 +-------------------------------------------------- 1 file changed, 2 insertions(+), 146 deletions(-) diff --git a/external/gard/gard.c b/external/gard/gard.c index d27f9b8..3cb5c55 100644 --- a/external/gard/gard.c +++ b/external/gard/gard.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "gard.h" @@ -225,143 +226,6 @@ static const char *path_type_to_str(enum path_type t) return "Unknown"; } -static bool get_dev_attr(const char *dev, const char *attr_file, uint32_t *attr) -{ - char dev_path[PATH_MAX] = SYSFS_MTD_PATH; - /* - * Needs to be large enough to hold at most uint32_t represented as a - * string in hex with leading 0x - */ - char attr_buf[10]; - int fd, rc; - - /* - * sizeof(dev_path) - (strlen(dev_path) + 1) is the remaining space in - * dev_path, + 1 to account for the '\0'. As strncat could write n+1 bytes - * to dev_path the correct calulcation for n is: - * (sizeof(dev_path) - (strlen(dev_path) + 1) - 1) - */ - strncat(dev_path, dev, (sizeof(dev_path) - (strlen(dev_path) + 1) - 1)); - strncat(dev_path, "/", (sizeof(dev_path) - (strlen(dev_path) + 1) - 1)); - strncat(dev_path, attr_file, (sizeof(dev_path) - (strlen(dev_path) + 1) - 1)); - fd = open(dev_path, O_RDONLY); - if (fd == -1) - goto out; - - rc = read(fd, attr_buf, sizeof(attr_buf)); - close(fd); - if (rc == -1) - goto out; - - if (attr) - *attr = strtol(attr_buf, NULL, 0); - - return 0; - -out: - fprintf(stderr, "Couldn't get MTD device attribute '%s' from '%s'\n", dev, attr_file); - return -1; -} - -static int get_dev_mtd(const char *fdt_flash_path, char **r_path) -{ - struct dirent **namelist; - char fdt_node_path[PATH_MAX]; - int count, i, rc, fd; - bool done; - - if (!fdt_flash_path) - return -1; - - fd = open(fdt_flash_path, O_RDONLY); - if (fd == -1) { - fprintf(stderr, "Couldn't open '%s' FDT attribute to determine which flash device to use\n", - fdt_flash_path); - return -1; - } - - rc = read(fd, fdt_node_path, sizeof(fdt_node_path) - 1); - close(fd); - if (rc == -1) { - fprintf(stderr, "Couldn't read flash FDT node from '%s'\n", fdt_flash_path); - return -1; - } - fdt_node_path[rc] = '\0'; - - count = scandir(SYSFS_MTD_PATH, &namelist, NULL, alphasort); - if (count == -1) { - fprintf(stderr, "Couldn't scan '%s' for MTD devices\n", SYSFS_MTD_PATH); - return -1; - } - - rc = 0; - done = false; - for (i = 0; i < count; i++) { - struct dirent *dirent; - char dev_path[PATH_MAX] = SYSFS_MTD_PATH; - char fdt_node_path_tmp[PATH_MAX]; - - dirent = namelist[i]; - if (dirent->d_name[0] == '.' || rc || done) { - free(namelist[i]); - continue; - } - - strncat(dev_path, dirent->d_name, sizeof(dev_path) - strlen(dev_path) - 2); - strncat(dev_path, "/device/of_node", sizeof(dev_path) - strlen(dev_path) - 2); - - rc = readlink(dev_path, fdt_node_path_tmp, sizeof(fdt_node_path_tmp) - 1); - if (rc == -1) { - /* - * This might fail because it could not exist if the system has flash - * devices that present as mtd but don't have corresponding FDT - * nodes, just continue silently. - */ - free(namelist[i]); - /* Should still try the next dir so reset rc */ - rc = 0; - continue; - } - fdt_node_path_tmp[rc] = '\0'; - - if (strstr(fdt_node_path_tmp, fdt_node_path)) { - uint32_t flags, size; - - /* - * size and flags could perhaps have be gotten another way but this - * method is super unlikely to fail so it will do. - */ - - /* Check to see if device is writeable */ - rc = get_dev_attr(dirent->d_name, "flags", &flags); - if (rc) { - free(namelist[i]); - continue; - } - - /* Get the size of the mtd device while we're at it */ - rc = get_dev_attr(dirent->d_name, "size", &size); - if (rc) { - free(namelist[i]); - continue; - } - - strcpy(dev_path, "/dev/"); - strncat(dev_path, dirent->d_name, sizeof(dev_path) - strlen(dev_path) - 2); - *r_path = strdup(dev_path); - done = true; - } - free(namelist[i]); - } - free(namelist); - - if (!done) - fprintf(stderr, "Couldn't find '%s' corresponding MTD\n", fdt_flash_path); - - /* explicit negative value so as to not return a libflash code */ - return done ? rc : -1; -} - static int do_iterate(struct gard_ctx *ctx, int (*func)(struct gard_ctx *ctx, int pos, struct gard_record *gard, void *priv), @@ -680,7 +544,6 @@ static const char *global_optstring = "+ef:p"; int main(int argc, char **argv) { const char *action, *progname; - const char *fdt_flash_path = FDT_ACTIVE_FLASH_PATH; char *filename = NULL; struct gard_ctx _ctx, *ctx; int rc, i = 0; @@ -742,14 +605,7 @@ int main(int argc, char **argv) argv += optind; action = argv[0]; - if (!filename) { - rc = get_dev_mtd(fdt_flash_path, &filename); - if (rc) - return EXIT_FAILURE; - } - - rc = file_init_path(filename, NULL, &(ctx->bl)); - if (rc) + if (arch_flash_init(&(ctx->bl), filename)) return EXIT_FAILURE; rc = blocklevel_get_info(ctx->bl, NULL, &(ctx->f_size), NULL);