fwupd/plugins/synaptics-prometheus/fu-dump.c
Richard Hughes 7afd7cba0d Use FuFirmware as a container for firmware images
In many plugins we've wanted to use ->prepare_firmware() to parse the firmware
ahead of ->detach() and ->write_firmware() but this has the limitation that it
can only return a single blob of data.

For many devices, multiple binary blobs are required from one parsed image,
for instance providing signatures, config and data blobs that have to be pushed
to the device in different way.

This also means we parse the firmware *before* we ask the user to detach.

Break the internal FuDevice API to support these firmware types as they become
more popular.

This also allows us to move the Intel HEX and SREC parsing out of the dfu plugin
as they are used by a few plugins now, and resolving symbols between plugins
isn't exactly awesome.
2019-08-08 13:10:57 +01:00

59 lines
1.4 KiB
C

/*
* Copyright (C) 2019 Richard Hughes <richard@hughsie.com>
*
* SPDX-License-Identifier: LGPL-2.1+
*/
#include "config.h"
#include "fu-synaprom-firmware.h"
static gboolean
fu_dump_parse (const gchar *filename, GError **error)
{
gchar *data = NULL;
gsize len = 0;
g_autoptr(GBytes) blob = NULL;
g_autoptr(FuFirmware) firmware = fu_synaprom_firmware_new ();
if (!g_file_get_contents (filename, &data, &len, error))
return FALSE;
blob = g_bytes_new_take (data, len);
return fu_firmware_parse (firmware, blob, 0, error);
}
static gboolean
fu_dump_generate (const gchar *filename, GError **error)
{
const gchar *data;
gsize len = 0;
g_autoptr(GBytes) blob = NULL;
g_autoptr(FuFirmware) firmware = fu_synaprom_firmware_new ();
blob = fu_firmware_write (firmware, error);
if (blob == NULL)
return FALSE;
data = g_bytes_get_data (blob, &len);
return g_file_set_contents (filename, data, len, error);
}
int
main (int argc, char **argv)
{
g_autoptr(GError) error = NULL;
if (argc == 2) {
if (!fu_dump_parse (argv[1], &error)) {
g_printerr ("parse failed: %s\n", error->message);
return 1;
}
} else if (argc == 3 && g_strcmp0 (argv[2], "gen") == 0) {
if (!fu_dump_generate (argv[1], &error)) {
g_printerr ("generate failed: %s\n", error->message);
return 1;
}
} else {
g_printerr ("firmware filename required\n");
return 2;
}
g_print ("OK!\n");
return 0;
}