flashrom: lspcon-i2c-spi: read active_partion and version

Reads the active_partition and firmware version from active_partition
diff --git a/plugins/flashrom/fu-flashrom-lspcon-i2c-spi-device.c b/plugins/flashrom/fu-flashrom-lspcon-i2c-spi-device.c
index c5c8df7..07c7184 100644
--- a/plugins/flashrom/fu-flashrom-lspcon-i2c-spi-device.c
+++ b/plugins/flashrom/fu-flashrom-lspcon-i2c-spi-device.c
@@ -18,11 +18,38 @@
 struct _FuFlashromLspconI2cSpiDevice {
 	FuFlashromDevice	  parent_instance;
 	gint			  bus_number;
+	guint8			  active_partition;
 };
 
 G_DEFINE_TYPE (FuFlashromLspconI2cSpiDevice, fu_flashrom_lspcon_i2c_spi_device,
 	       FU_TYPE_FLASHROM_DEVICE)
 
+struct romentry {
+	guint32			 start;
+	guint32			 end;
+	gboolean		 included;
+	const gchar		*name;
+	const gchar		*file;
+};
+
+struct flashrom_layout {
+	struct romentry		*entries;
+	gsize			 num_entries;
+};
+
+static struct romentry entries[5] = {
+	{ .start = 0x00002, .end = 0x00003, .included = TRUE, .name = "FLAG" },
+	{ .start = 0x10000, .end = 0x1ffff, .included = FALSE, .name = "PAR1" },
+	{ .start = 0x20000, .end = 0x2ffff, .included = FALSE, .name = "PAR2" },
+	{ .start = 0x15000, .end = 0x15002, .included = TRUE, .name = "VER1" },
+	{ .start = 0x25000, .end = 0x25002, .included = TRUE, .name = "VER2" },
+};
+
+static struct flashrom_layout layout = {
+	.entries = entries, .num_entries = sizeof(entries) / sizeof(entries[0])
+};
+
+
 static void
 fu_flashrom_lspcon_i2c_spi_device_init (FuFlashromLspconI2cSpiDevice *self)
 {
@@ -85,12 +112,19 @@
 static gboolean
 fu_flashrom_lspcon_i2c_spi_device_setup (FuDevice *device, GError **error)
 {
+	FuFlashromLspconI2cSpiDevice *self = FU_FLASHROM_LSPCON_I2C_SPI_DEVICE (device);
+	FuFlashromDevice *flashrom_device = FU_FLASHROM_DEVICE (device);
 	const gchar *hw_id = NULL;
 	g_autofree gchar *vid = NULL;
 	g_autofree gchar *pid = NULL;
 	g_autofree gchar *vendor_id = NULL;
 	g_autofree gchar *temp = NULL;
+	g_autofree gchar *version = NULL;
 	g_autofree gchar *guid = NULL;
+	g_autofree gchar *contents = NULL;
+	struct flashrom_flashctx *flashctx = NULL;
+	gsize flash_size;
+	guint32 addr;
 
 	hw_id = fu_udev_device_get_sysfs_attr (FU_UDEV_DEVICE (device), "name", error);
 	if (hw_id == NULL) {
@@ -110,8 +144,33 @@
 	guid = fwupd_guid_hash_string (temp);
 	fu_device_add_guid (device, guid);
 
-	/* TODO: Get the real version number. */
-	fu_device_set_version (device, "0.0");
+	/* set up flashrom layout */
+	flashctx = fu_flashrom_device_get_flashctx (flashrom_device);
+	flashrom_layout_set (flashctx, &layout);
+
+	/* read the current flash contents */
+	fu_device_set_status (device, FWUPD_STATUS_DEVICE_READ);
+	flash_size = fu_flashrom_device_get_flash_size (flashrom_device);
+	contents = g_malloc0 (flash_size);
+	if (flashrom_image_read (flashctx, contents, flash_size)) {
+		g_set_error_literal (error, FWUPD_ERROR, FWUPD_ERROR_READ,
+				     "failed to read flash contents");
+		return FALSE;
+	}
+
+	/* grab the active partition */
+	self->active_partition = contents[entries[0].start];
+
+	if (self->active_partition != 1 && self->active_partition != 2) {
+		g_set_error_literal (error, FWUPD_ERROR, FWUPD_ERROR_READ,
+				     "failed to read active partition");
+		return FALSE;
+	}
+
+	/* find the current version for the active partition */
+	addr = entries[self->active_partition + 2].start;
+	version = g_strdup_printf ("%d.%d", contents[addr], contents[addr + 2]);
+	fu_device_set_version (device, version);
 
 	return TRUE;
 }