Alexander Goncharov has uploaded this change for review.

View Change

dummyflasher: use new API to register shutdown function

This allows masters to register shutdown function in *_master
struct, which means there is no need to call register_shutdown in init
function, since this call is now a part of register_*_master.

A dummy programmer can register masters for multiple buses that share a
programmer's data (a pointer to struct emu_data) with each other. To
avoid unexpected memory freeing by shutdown function, we need to keep
track of how many buses are using the shared resource. Use the
reference counting technique to achieve this.

TEST=ninja test

Change-Id: I0c67c25b0f53cd8c564c4ea0f09f2728e856f6ea
Signed-off-by: Alexander Goncharov <chat@joursoir.net>
---
M dummyflasher.c
1 file changed, 41 insertions(+), 6 deletions(-)

git pull ssh://review.coreboot.org:29418/flashrom refs/changes/08/72408/1
diff --git a/dummyflasher.c b/dummyflasher.c
index 1960e92..7305b46 100644
--- a/dummyflasher.c
+++ b/dummyflasher.c
@@ -75,6 +75,8 @@

unsigned int spi_write_256_chunksize;
uint8_t *flashchip_contents;
+
+ uint8_t refs_cnt;
};

/* A legit complete SFDP table based on the MX25L6436E (rev. 1.8) datasheet. */
@@ -905,6 +907,11 @@
{
msg_pspew("%s\n", __func__);
struct emu_data *emu_data = (struct emu_data *)data;
+
+ emu_data->refs_cnt--;
+ if (emu_data->refs_cnt != 0)
+ return 0;
+
if (emu_data->emu_chip != EMULATE_NONE) {
if (emu_data->emu_persistent_image && emu_data->emu_modified) {
msg_pdbg("Writing %s\n", emu_data->emu_persistent_image);
@@ -929,6 +936,7 @@
.multicommand = default_spi_send_multicommand,
.read = default_spi_read,
.write_256 = dummy_spi_write_256,
+ .shutdown = dummy_shutdown,
.probe_opcode = dummy_spi_probe_opcode,
};

@@ -943,6 +951,7 @@
.chip_writew = dummy_chip_writew,
.chip_writel = dummy_chip_writel,
.chip_writen = dummy_chip_writen,
+ .shutdown = dummy_shutdown,
};

static const struct opaque_master opaque_master_dummyflasher = {
@@ -950,6 +959,7 @@
.read = dummy_opaque_read,
.write = dummy_opaque_write,
.erase = dummy_opaque_erase,
+ .shutdown = dummy_shutdown,
};

static int init_data(const struct programmer_cfg *cfg,
@@ -1402,12 +1412,15 @@
}

dummy_init_out:
- if (register_shutdown(dummy_shutdown, data)) {
- free(data->emu_persistent_image);
- free(data->flashchip_contents);
- free(data);
- return 1;
- }
+ data->refs_cnt += ((dummy_buses_supported & BUS_PROG) != BUS_NONE);
+ data->refs_cnt += ((dummy_buses_supported & BUS_NONSPI) != BUS_NONE);
+ data->refs_cnt += ((dummy_buses_supported & BUS_SPI) != BUS_NONE);
+
+#if 0
+ data->refs_cnt += (dummy_buses_supported & BUS_PROG) ? 1 : 0;
+ data->refs_cnt += (dummy_buses_supported & BUS_NONSPI) ? 1 : 0;
+ data->refs_cnt += (dummy_buses_supported & BUS_SPI) ? 1 : 0;
+#endif

if (dummy_buses_supported & BUS_PROG)
register_opaque_master(&opaque_master_dummyflasher, data);

To view, visit change 72408. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: flashrom
Gerrit-Branch: master
Gerrit-Change-Id: I0c67c25b0f53cd8c564c4ea0f09f2728e856f6ea
Gerrit-Change-Number: 72408
Gerrit-PatchSet: 1
Gerrit-Owner: Alexander Goncharov <chat@joursoir.net>
Gerrit-MessageType: newchange