[2/4] libflash/ecc: Add helpers to align a position within an ecc buffer

Message ID 20170418072927.692-3-cyril.bur@au1.ibm.com
State New
Headers show

Commit Message

Cyril Bur April 18, 2017, 7:29 a.m.
As part of ongoing work to make ECC invisible to higher levels up the
stack this function converts a 'position' which should be ECC agnostic
to the equivalent position within an ECC region starting at a specified
location.

Signed-off-by: Cyril Bur <cyril.bur@au1.ibm.com>
---
 libflash/ecc.h           |  5 +++++
 libflash/test/test-ecc.c | 31 +++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)

Patch

diff --git a/libflash/ecc.h b/libflash/ecc.h
index 2ce22152..bd669484 100644
--- a/libflash/ecc.h
+++ b/libflash/ecc.h
@@ -67,4 +67,9 @@  static inline uint64_t ecc_buffer_size_minus_ecc(uint64_t len)
 	return len * BYTES_PER_ECC / (BYTES_PER_ECC + 1);
 }
 
+static inline uint64_t ecc_buffer_align(uint64_t start, uint64_t pos)
+{
+	return pos - ((pos - start) % (BYTES_PER_ECC + 1));
+}
+
 #endif
diff --git a/libflash/test/test-ecc.c b/libflash/test/test-ecc.c
index fb6ee64e..5ce4c571 100644
--- a/libflash/test/test-ecc.c
+++ b/libflash/test/test-ecc.c
@@ -487,5 +487,36 @@  int main(void)
 
 	free(buf);
 	free(ret_buf);
+
+	/* Check that unaligned address become aligned */
+	if (ecc_buffer_align(0, 5) != 0) {
+		ERR("ecc_buffer_align(0, 5) not 0 -> %ld\n", ecc_buffer_align(0, 5));
+		exit(1);
+	}
+
+	if (ecc_buffer_align(0, 8) != 0) {
+		ERR("ecc_buffer_align(0, 8) not 0 -> %ld\n", ecc_buffer_align(0, 8));
+		exit(1);
+	}
+	if (ecc_buffer_align(0, 9) != 9) {
+		ERR("ecc_buffer_align(0, 9) not 9 -> %ld\n", ecc_buffer_align(0, 9));
+		exit(1);
+	}
+	if (ecc_buffer_align(0, 15) != 9) {
+		ERR("ecc_buffer_align(0, 15) not 9 -> %ld\n", ecc_buffer_align(0, 15));
+		exit(1);
+	}
+	if (ecc_buffer_align(5, 10) != 5) {
+		ERR("ecc_buffer_align(5, 10) not 5 -> %ld\n", ecc_buffer_align(5, 10));
+		exit(1);
+	}
+	if (ecc_buffer_align(5, 18) != 14) {
+		ERR("ecc_buffer_align(5, 18) not 14 -> %ld\n", ecc_buffer_align(5, 18));
+		exit(1);
+	}
+	if (ecc_buffer_align(0, 50) != 45) {
+		ERR("ecc_buffer_align(0, 50) not 45 -> %ld\n", ecc_buffer_align(0, 50));
+		exit(1);
+	}
 	return 0;
 }