[Skiboot] [PATCH v2] libflash: move ffs_flash_read into libflash

Jeremy Kerr jk at ozlabs.org
Thu Feb 26 14:46:09 AEDT 2015


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 <jk at ozlabs.org>

---
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 <ccan/endian/endian.h>
 
 #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 <libflash/libflash-priv.h>
 
 #include "../libflash.c"
+#include "../ecc.c"
 
 #define __unused		__attribute__((unused))
 


More information about the Skiboot mailing list