From patchwork Thu Feb 26 03:46:09 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeremy Kerr X-Patchwork-Id: 443784 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [103.22.144.68]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 6966514010F for ; Thu, 26 Feb 2015 14:46:19 +1100 (AEDT) Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 528631A0045 for ; Thu, 26 Feb 2015 14:46:19 +1100 (AEDT) X-Original-To: skiboot@lists.ozlabs.org Delivered-To: skiboot@lists.ozlabs.org Received: from ozlabs.org (ozlabs.org [103.22.144.67]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id 20DEC1A0010 for ; Thu, 26 Feb 2015 14:46:15 +1100 (AEDT) Received: by ozlabs.org (Postfix, from userid 1023) id A229214011D; Thu, 26 Feb 2015 14:46:14 +1100 (AEDT) MIME-Version: 1.0 Message-Id: <1424922369.504285.893285036764.1.gpush@pablo> To: skiboot@lists.ozlabs.org From: Jeremy Kerr Date: Thu, 26 Feb 2015 11:46:09 +0800 Subject: [Skiboot] [PATCH v2] libflash: move ffs_flash_read into libflash X-BeenThere: skiboot@lists.ozlabs.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Mailing list for skiboot development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: skiboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Skiboot" We have ffs_flash_read to do optionally-ecc-ed reads of flash data. However, this isn't really related to the ffs partitioning. This change moves ffs_flash_read into libflash.c, named flash_read_corrected. The function itself isn't changed. Signed-off-by: Jeremy Kerr --- v2: Add ecc.c into tests --- core/flash.c | 6 +-- libflash/libffs.c | 55 ----------------------------------- libflash/libffs.h | 4 -- libflash/libflash.c | 55 +++++++++++++++++++++++++++++++++++ libflash/libflash.h | 2 + libflash/test/Makefile.check | 2 - libflash/test/test-flash.c | 1 7 files changed, 62 insertions(+), 63 deletions(-) diff --git a/core/flash.c b/core/flash.c index 6fe9250..bf6f587 100644 --- a/core/flash.c +++ b/core/flash.c @@ -375,8 +375,8 @@ static int flash_find_subpartition(struct flash_chip *chip, uint32_t subid, partsize = BUFFER_SIZE_MINUS_ECC(*total_size); /* Get the TOC */ - rc = ffs_flash_read(chip, *start, header, FLASH_SUBPART_HEADER_SIZE, - ecc); + rc = flash_read_corrected(chip, *start, header, + FLASH_SUBPART_HEADER_SIZE, ecc); if (rc) { prerror("FLASH: flash subpartition TOC read failed %i\n", rc); goto end; @@ -541,7 +541,7 @@ bool flash_load_resource(enum resource_id id, uint32_t subid, goto out_free_ffs; } - rc = ffs_flash_read(flash->chip, part_start, buf, size, ecc); + rc = flash_read_corrected(flash->chip, part_start, buf, size, ecc); if (rc) { prerror("FLASH: failed to read %s partition\n", name); goto out_free_ffs; diff --git a/libflash/libffs.c b/libflash/libffs.c index bce4ac4..109ac40 100644 --- a/libflash/libffs.c +++ b/libflash/libffs.c @@ -22,11 +22,6 @@ #include #include "libffs.h" -#include "ecc.h" - -#ifndef MIN -#define MIN(a, b) ((a) < (b) ? (a) : (b)) -#endif enum ffs_type { ffs_type_flash, @@ -292,53 +287,3 @@ int ffs_update_act_size(struct ffs_handle *ffs, uint32_t part_idx, return flash_smart_write(ffs->chip, offset, ent, FFS_ENTRY_SIZE); } -#define COPY_BUFFER_LENGTH 4096 - -/* - * This provides a wrapper around flash_read on ECCed data - * len is length of data without ECC attached - */ -int ffs_flash_read(struct flash_chip *c, uint32_t pos, void *buf, uint32_t len, - bool ecc) -{ - uint64_t *bufecc; - uint32_t copylen; - int rc; - uint8_t ret; - - if (!ecc) - return flash_read(c, pos, buf, len); - - /* Copy the buffer in chunks */ - bufecc = malloc(ECC_BUFFER_SIZE(COPY_BUFFER_LENGTH)); - if (!bufecc) - return FLASH_ERR_MALLOC_FAILED; - - while (len > 0) { - /* What's left to copy? */ - copylen = MIN(len, COPY_BUFFER_LENGTH); - - /* Read ECCed data from flash */ - rc = flash_read(c, pos, bufecc, ECC_BUFFER_SIZE(copylen)); - if (rc) - goto err; - - /* Extract data from ECCed data */ - ret = eccmemcpy(buf, bufecc, copylen); - if (ret == UE) { - rc = FLASH_ERR_ECC_INVALID; - goto err; - } - - /* Update for next copy */ - len -= copylen; - buf = (uint8_t *)buf + copylen; - pos += ECC_BUFFER_SIZE(copylen); - } - - return 0; - -err: - free(bufecc); - return rc; -} diff --git a/libflash/libffs.h b/libflash/libffs.h index 15ed3c5..b597118 100644 --- a/libflash/libffs.h +++ b/libflash/libffs.h @@ -53,8 +53,4 @@ int ffs_part_info(struct ffs_handle *ffs, uint32_t part_idx, int ffs_update_act_size(struct ffs_handle *ffs, uint32_t part_idx, uint32_t act_size); -int ffs_flash_read(struct flash_chip *c, uint32_t pos, void *buf, uint32_t len, - bool ecc); - - #endif /* __LIBFFS_H */ diff --git a/libflash/libflash.c b/libflash/libflash.c index 5badbff..31a347b 100644 --- a/libflash/libflash.c +++ b/libflash/libflash.c @@ -19,6 +19,11 @@ #include "libflash.h" #include "libflash-priv.h" +#include "ecc.h" + +#ifndef MIN +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#endif static const struct flash_info flash_info[] = { { 0xc22019, 0x02000000, FL_ERASE_ALL | FL_CAN_4B, "Macronix MXxxL25635F"}, @@ -123,6 +128,56 @@ int flash_read(struct flash_chip *c, uint32_t pos, void *buf, uint32_t len) return ct->cmd_rd(ct, CMD_READ, true, pos, buf, len); } +#define COPY_BUFFER_LENGTH 4096 + +/* + * This provides a wrapper around flash_read on ECCed data + * len is length of data without ECC attached + */ +int flash_read_corrected(struct flash_chip *c, uint32_t pos, void *buf, + uint32_t len, bool ecc) +{ + uint64_t *bufecc; + uint32_t copylen; + int rc; + uint8_t ret; + + if (!ecc) + return flash_read(c, pos, buf, len); + + /* Copy the buffer in chunks */ + bufecc = malloc(ECC_BUFFER_SIZE(COPY_BUFFER_LENGTH)); + if (!bufecc) + return FLASH_ERR_MALLOC_FAILED; + + while (len > 0) { + /* What's left to copy? */ + copylen = MIN(len, COPY_BUFFER_LENGTH); + + /* Read ECCed data from flash */ + rc = flash_read(c, pos, bufecc, ECC_BUFFER_SIZE(copylen)); + if (rc) + goto err; + + /* Extract data from ECCed data */ + ret = eccmemcpy(buf, bufecc, copylen); + if (ret == UE) { + rc = FLASH_ERR_ECC_INVALID; + goto err; + } + + /* Update for next copy */ + len -= copylen; + buf = (uint8_t *)buf + copylen; + pos += ECC_BUFFER_SIZE(copylen); + } + + return 0; + +err: + free(bufecc); + return rc; +} static void fl_get_best_erase(struct flash_chip *c, uint32_t dst, uint32_t size, uint32_t *chunk, uint8_t *cmd) { diff --git a/libflash/libflash.h b/libflash/libflash.h index f9ec977..01806d5 100644 --- a/libflash/libflash.h +++ b/libflash/libflash.h @@ -70,6 +70,8 @@ int flash_get_info(struct flash_chip *chip, const char **name, int flash_force_4b_mode(struct flash_chip *chip, bool enable_4b); int flash_read(struct flash_chip *c, uint32_t pos, void *buf, uint32_t len); +int flash_read_corrected(struct flash_chip *c, uint32_t pos, void *buf, + uint32_t len, bool ecc); int flash_erase(struct flash_chip *c, uint32_t dst, uint32_t size); int flash_write(struct flash_chip *c, uint32_t dst, const void *src, uint32_t size, bool verify); diff --git a/libflash/test/Makefile.check b/libflash/test/Makefile.check index 5d32a41..4000395 100644 --- a/libflash/test/Makefile.check +++ b/libflash/test/Makefile.check @@ -16,7 +16,7 @@ $(LIBFLASH_TEST:%=%-check) : %-check: % libflash/test/stubs.o: libflash/test/stubs.c $(call Q, HOSTCC ,$(HOSTCC) $(HOSTCFLAGS) -g -c -o $@ $<, $<) -$(LIBFLASH_TEST) : libflash/test/stubs.o libflash/libflash.c +$(LIBFLASH_TEST) : libflash/test/stubs.o libflash/libflash.c libflash/ecc.c $(LIBFLASH_TEST) : % : %.c $(call Q, HOSTCC ,$(HOSTCC) $(HOSTCFLAGS) -O0 -g -I include -I . -o $@ $< libflash/test/stubs.o, $<) diff --git a/libflash/test/test-flash.c b/libflash/test/test-flash.c index 5f48797..375b338 100644 --- a/libflash/test/test-flash.c +++ b/libflash/test/test-flash.c @@ -22,6 +22,7 @@ #include #include "../libflash.c" +#include "../ecc.c" #define __unused __attribute__((unused))