mboxd: Add a backend abstraction layer to mboxd.

Introduce a backend abstraction, enabling multiple implementations to be
compiled in at once. This change formally abstracts the two existing
backends, mtd and vpnor.

With the backend abstraction in place, subsequent backends are easier to
implement.

This change is based of Evan's work and he retains authorship credit. I
(AJ) have reworked the patch to pass the vpnor tests, refactored some
parts to enable broader use of const structures and others to clarify
the initialisation sequences.

Due to the existing lack of abstraction the patch has unfortunately
wide-ranging impacts. I've whittled it down as much as I consider
reasonable.

Change-Id: I29984a36dae4ea86ec00b853d2a756f0b9afb3ec
Signed-off-by: Evan Lojewski <github@meklort.com>
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/mboxd.h b/mboxd.h
index 1047e46..e046785 100644
--- a/mboxd.h
+++ b/mboxd.h
@@ -4,11 +4,13 @@
 #ifndef MBOX_H
 #define MBOX_H
 
+#include <assert.h>
 #include <mtd/mtd-abi.h>
 #include <systemd/sd-bus.h>
 #include <poll.h>
 #include <stdbool.h>
 
+#include "backend.h"
 #include "protocol.h"
 #include "transport.h"
 #include "vpnor/mboxd_pnor_partition_table.h"
@@ -48,8 +50,7 @@
 #define SIG_FD			2
 #define POLL_FDS		3 /* Number of FDs we poll on */
 #define LPC_CTRL_FD		3
-#define MTD_FD			4
-#define TOTAL_FDS		5
+#define TOTAL_FDS		4
 
 #define MAPS_FLASH		(1 << 0)
 #define MAPS_MEM		(1 << 1)
@@ -72,6 +73,10 @@
 	enum api_version version;
 	const struct protocol_ops *protocol;
 	const struct transport_ops *transport;
+	struct backend backend;
+
+	/* Commandline parameters */
+	const char *path;
 
 /* System State */
 	enum mbox_state state;
@@ -96,21 +101,32 @@
 	uint32_t mem_size;
 	/* LPC Bus Base Address (bytes) */
 	uint32_t lpc_base;
-	/* Flash size from command line (bytes) */
-	uint32_t flash_size;
-	/* Bytemap of the erased state of the entire flash */
-	uint8_t *flash_bmap;
-	/* Erase size (as a shift) */
-	uint32_t erase_size_shift;
-	/* Block size (as a shift) */
-	uint32_t block_size_shift;
-	/* Actual Flash Info */
-	struct mtd_info_user mtd_info;
-#ifdef VIRTUAL_PNOR_ENABLED
-	/* Virtual PNOR partition table */
-	struct vpnor_partition_table *vpnor;
-	struct vpnor_partition_paths paths;
-#endif
 };
 
+/* Temporary flash API compatibility */
+static inline int64_t flash_copy(struct mbox_context *context, uint32_t offset,
+				 void *mem, uint32_t size)
+{
+	return backend_copy(&context->backend, offset, mem, size);
+}
+
+static inline int flash_set_bytemap(struct mbox_context *context,
+				    uint32_t offset, uint32_t count,
+				    uint8_t val)
+{
+	return backend_set_bytemap(&context->backend, offset, count, val);
+}
+
+static inline int flash_erase(struct mbox_context *context, uint32_t offset,
+			      uint32_t count)
+{
+	return backend_erase(&context->backend, offset, count);
+}
+
+static inline int flash_write(struct mbox_context *context, uint32_t offset,
+			      void *buf, uint32_t count)
+{
+	return backend_write(&context->backend, offset, buf, count);
+}
+
 #endif /* MBOX_H */