Skip to content

Commit

Permalink
flashrom: Add a plugin for updating using the flashrom command line tool
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsie committed Nov 4, 2017
1 parent 674ed34 commit 1bf1636
Show file tree
Hide file tree
Showing 15 changed files with 354 additions and 0 deletions.
1 change: 1 addition & 0 deletions contrib/fwupd.spec.in
Expand Up @@ -223,6 +223,7 @@ mkdir -p --mode=0700 $RPM_BUILD_ROOT%{_localstatedir}/lib/fwupd/gnupg
%endif
%{_libdir}/fwupd-plugins-3/libfu_plugin_dfu.so
%{_libdir}/fwupd-plugins-3/libfu_plugin_ebitdo.so
%{_libdir}/fwupd-plugins-3/libfu_plugin_flashrom.so
%{_libdir}/fwupd-plugins-3/libfu_plugin_raspberrypi.so
%{_libdir}/fwupd-plugins-3/libfu_plugin_steelseries.so
%if 0%{?have_dell}
Expand Down
7 changes: 7 additions & 0 deletions plugins/flashrom/README.md
@@ -0,0 +1,7 @@
Flashrom
========

Introduction
------------

This plugin uses `flashrom` to update the system firmware.
4 changes: 4 additions & 0 deletions plugins/flashrom/example/build.sh
@@ -0,0 +1,4 @@
#/bin/sh
appstream-util validate-relax com.Flashrom.Laptop.metainfo.xml
tar -cf firmware.tar startup.sh random-tool
gcab --create --nopath Flashrom-Laptop-1.2.3.cab firmware.tar com.Flashrom.Laptop.metainfo.xml
42 changes: 42 additions & 0 deletions plugins/flashrom/example/com.Flashrom.Laptop.metainfo.xml
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright 2017 Richard Hughes <richard@hughsie.com> -->
<component type="firmware">
<id>com.Flashrom.Laptop.firmware</id>
<name>Flashrom Laptop Firmware</name>
<summary>System firmware for a Flashrom laptop</summary>
<description>
<p>
The laptop can be updated using flashrom.
</p>
</description>
<provides>
<!-- this is a suitable HWID, found using `fwupdmgr hwids` -->
<firmware type="flashed">a0ce5085-2dea-5086-ae72-45810a186ad0</firmware>
</provides>
<url type="homepage">http://www.bbc.co.uk/</url>
<metadata_license>CC0-1.0</metadata_license>
<project_license>Proprietary</project_license> <!-- FIXME? -->
<developer_name>Flashrom</developer_name>
<releases>
<release urgency="high" version="1.2.3" date="2017-08-15">
<checksum filename="firmware.tar" target="content"/>
<description>
<p>
This release updates a frobnicator to frob faster.
</p>
</description>
</release>
</releases>

<!-- this script is run with bubblewrap -- the old firmware is in /boot -->
<custom>
<value key="fwupd::BuilderScript">startup.sh</value>
<value key="fwupd::BuilderOutput">firmware.bin</value>
</custom>

<!-- only newer versions of fwupd know how to write to this hardware -->
<requires>
<id compare="ge" version="1.0.1">org.freedesktop.fwupd</id>
</requires>

</component>
2 changes: 2 additions & 0 deletions plugins/flashrom/example/random-tool
@@ -0,0 +1,2 @@
#/bin/sh
echo "hello from the sandbox"
10 changes: 10 additions & 0 deletions plugins/flashrom/example/startup.sh
@@ -0,0 +1,10 @@
#/bin/sh

# do something with the old firmware
sha1sum /boot/flashrom-librem15v3.bin

# run a random tool
./random-tool

# this is the deliverable
cp /boot/flashrom-librem15v3.bin firmware.bin
4 changes: 4 additions & 0 deletions plugins/flashrom/flashrom.quirk
@@ -0,0 +1,4 @@
[fwupd-flashrom-hwid]

# Purism
a0ce5085-2dea-5086-ae72-45810a186ad0=librem15v3
203 changes: 203 additions & 0 deletions plugins/flashrom/fu-plugin-flashrom.c
@@ -0,0 +1,203 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2017 Richard Hughes <richard@hughsie.com>
*
* Licensed under the GNU General Public License Version 2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "config.h"

#include <string.h>

#include "fu-plugin.h"
#include "fu-plugin-vfuncs.h"

struct FuPluginData {
gchar *flashrom_fn;
};

void
fu_plugin_init (FuPlugin *plugin)
{
fu_plugin_alloc_data (plugin, sizeof (FuPluginData));
}

void
fu_plugin_destroy (FuPlugin *plugin)
{
FuPluginData *data = fu_plugin_get_data (plugin);
g_free (data->flashrom_fn);
}

gboolean
fu_plugin_startup (FuPlugin *plugin, GError **error)
{
FuPluginData *data = fu_plugin_get_data (plugin);
GPtrArray *hwids;

/* we need flashrom from the host system */
data->flashrom_fn = g_find_program_in_path ("flashrom");
if (data->flashrom_fn == NULL) {
g_set_error_literal (error,
FWUPD_ERROR,
FWUPD_ERROR_NOT_SUPPORTED,
"flashrom is not installed");
return FALSE;
}

/* search for devices */
hwids = fu_plugin_get_hwids (plugin);
for (guint i = 0; i < hwids->len; i++) {
const gchar *quirk;
const gchar *guid = g_ptr_array_index (hwids, i);
quirk = fu_plugin_lookup_quirk_by_id (plugin,
FU_QUIRKS_FLASHROM_HWID,
guid);
if (quirk != NULL) {
g_autofree gchar *device_id = g_strdup_printf ("flashrom-%s", quirk);
g_autoptr(FuDevice) dev = fu_device_new ();
fu_device_set_id (dev, device_id);
fu_device_add_flag (dev, FWUPD_DEVICE_FLAG_INTERNAL);
fu_device_add_flag (dev, FWUPD_DEVICE_FLAG_UPDATABLE);
fu_device_add_guid (dev, guid);
fu_device_set_version (dev, fu_plugin_get_dmi_value (plugin, FU_HWIDS_KEY_BIOS_VERSION));
fu_device_set_name (dev, fu_plugin_get_dmi_value (plugin, FU_HWIDS_KEY_PRODUCT_NAME));
fu_device_set_vendor (dev, fu_plugin_get_dmi_value (plugin, FU_HWIDS_KEY_MANUFACTURER));
fu_plugin_device_add (plugin, dev);
fu_plugin_cache_add (plugin, device_id, dev);
break;
}
}
return TRUE;
}

static guint
fu_plugin_flashrom_parse_percentage (FuPlugin *plugin, const gchar *lines_verbose)
{
const guint64 addr_highest = 0xffffff;
guint64 addr_best = 0x0;
g_auto(GStrv) chunks = NULL;

/* parse 0x000000-0x000fff:S, 0x001000-0x001fff:S */
chunks = g_strsplit_set (lines_verbose, "x-:S, \n\r", -1);
for (guint i = 0; chunks[i] != NULL; i++) {
guint64 addr_tmp;
if (strlen (chunks[i]) != 6)
continue;
addr_tmp = g_ascii_strtoull (chunks[i], NULL, 16);
if (addr_tmp > addr_best)
addr_best = addr_tmp;
}
return (addr_best * 100) / addr_highest;
}

static void
fu_plugin_flashrom_read_cb (const gchar *line, gpointer user_data)
{
FuPlugin *plugin = FU_PLUGIN (user_data);
if (g_strcmp0 (line, "Reading flash...") == 0)
fu_plugin_set_status (plugin, FWUPD_STATUS_DEVICE_VERIFY);
fu_plugin_set_percentage (plugin, fu_plugin_flashrom_parse_percentage (plugin, line));
}

static void
fu_plugin_flashrom_write_cb (const gchar *line, gpointer user_data)
{
FuPlugin *plugin = FU_PLUGIN (user_data);
if (g_strcmp0 (line, "Writing flash...") == 0)
fu_plugin_set_status (plugin, FWUPD_STATUS_DEVICE_WRITE);
fu_plugin_set_percentage (plugin, fu_plugin_flashrom_parse_percentage (plugin, line));
}

gboolean
fu_plugin_update_prepare (FuPlugin *plugin,
FuDevice *device,
GError **error)
{
FuPluginData *data = fu_plugin_get_data (plugin);
g_autofree gchar *firmware_orig = NULL;
g_autofree gchar *basename = NULL;

/* not us */
if (fu_plugin_cache_lookup (plugin, fu_device_get_id (device)) == NULL)
return TRUE;

/* if the original firmware doesn't exist, grab it now */
basename = g_strdup_printf ("flashrom-%s.bin", fu_device_get_id (device));
firmware_orig = g_build_filename (LOCALSTATEDIR, "lib", "fwupd",
"builder", basename, NULL);
if (!fu_common_mkdir_parent (firmware_orig, error))
return FALSE;
if (!g_file_test (firmware_orig, G_FILE_TEST_EXISTS)) {
const gchar *argv[] = {
data->flashrom_fn,
"--programmer", "internal:laptop=force_I_want_a_brick",
"--read", firmware_orig,
"--verbose", NULL };
if (!fu_common_spawn_sync ((const gchar * const *) argv,
fu_plugin_flashrom_read_cb, plugin,
NULL, error)) {
g_prefix_error (error, "failed to get original firmware: ");
return FALSE;
}
fu_plugin_set_status (plugin, FWUPD_STATUS_IDLE);
}

return TRUE;
}

gboolean
fu_plugin_update (FuPlugin *plugin,
FuDevice *device,
GBytes *blob_fw,
FwupdInstallFlags flags,
GError **error)
{
FuPluginData *data = fu_plugin_get_data (plugin);
g_autofree gchar *firmware_fn = NULL;
g_autofree gchar *tmpdir = NULL;
const gchar *argv[] = {
data->flashrom_fn,
"--programmer", "internal:laptop=force_I_want_a_brick",
"--write", "xxx",
"--verbose", NULL };

/* write blob to temp location */
tmpdir = g_dir_make_tmp ("fwupd-XXXXXX", error);
if (tmpdir == NULL)
return FALSE;
firmware_fn = g_build_filename (tmpdir, "flashrom-firmware.bin", NULL);
if (!fu_common_set_contents_bytes (firmware_fn, blob_fw, error))
return FALSE;

/* use flashrom to write image */
argv[4] = firmware_fn;
if (!fu_common_spawn_sync ((const gchar * const *) argv,
fu_plugin_flashrom_write_cb, plugin,
NULL, error)) {
g_prefix_error (error, "failed to write firmware: ");
return FALSE;
}

/* delete temp location */
if (!fu_common_rmtree (tmpdir, error))
return FALSE;

/* success */
fu_plugin_set_status (plugin, FWUPD_STATUS_IDLE);
return TRUE;
}
25 changes: 25 additions & 0 deletions plugins/flashrom/meson.build
@@ -0,0 +1,25 @@
cargs = ['-DG_LOG_DOMAIN="FuPluginFlashrom"']

install_data(['flashrom.quirk'],
install_dir: join_paths(get_option('datadir'), 'fwupd', 'quirks.d')
)

shared_module('fu_plugin_flashrom',
sources : [
'fu-plugin-flashrom.c',
],
include_directories : [
include_directories('../..'),
include_directories('../../src'),
include_directories('../../libfwupd'),
],
install : true,
install_dir: plugin_dir,
c_args : [
cargs,
'-DLOCALSTATEDIR="' + localstatedir + '"',
],
dependencies : [
plugin_deps,
],
)
1 change: 1 addition & 0 deletions plugins/meson.build
@@ -1,5 +1,6 @@
subdir('dfu')
subdir('ebitdo')
subdir('flashrom')
subdir('raspberrypi')
subdir('steelseries')
subdir('test')
Expand Down
19 changes: 19 additions & 0 deletions src/fu-hwids.c
Expand Up @@ -34,6 +34,7 @@ struct _FuHwids {
GHashTable *hash_dmi_hw; /* BiosVersion->"1.2.3 " */
GHashTable *hash_dmi_display; /* BiosVersion->"1.2.3" */
GHashTable *hash_guid; /* a-c-b-d->1 */
GPtrArray *array_guids; /* a-c-b-d */
};

G_DEFINE_TYPE (FuHwids, fu_hwids, G_TYPE_OBJECT)
Expand Down Expand Up @@ -69,6 +70,20 @@ fu_hwids_has_guid (FuHwids *self, const gchar *guid)
return g_hash_table_lookup (self->hash_guid, guid) != NULL;
}

/**
* fu_hwids_get_guids:
* @self: A #FuHwids
*
* Returns all the defined HWIDs
*
* Returns: (transfer none) (element-type utf-8): An array of GUIDs
**/
GPtrArray *
fu_hwids_get_guids (FuHwids *self)
{
return self->array_guids;
}

static gchar *
fu_hwids_get_guid_for_str (const gchar *str, GError **error)
{
Expand Down Expand Up @@ -372,6 +387,7 @@ fu_hwids_setup (FuHwids *self, FuSmbios *smbios, GError **error)
g_hash_table_insert (self->hash_guid,
g_strdup (guid),
GUINT_TO_POINTER (1));
g_ptr_array_add (self->array_guids, g_strdup (guid));

/* show what makes up the GUID */
values = fu_hwids_get_replace_values (self, key, NULL);
Expand All @@ -391,6 +407,8 @@ fu_hwids_finalize (GObject *object)
g_hash_table_unref (self->hash_dmi_hw);
g_hash_table_unref (self->hash_dmi_display);
g_hash_table_unref (self->hash_guid);
g_ptr_array_unref (self->array_guids);

G_OBJECT_CLASS (fu_hwids_parent_class)->finalize (object);
}

Expand All @@ -407,6 +425,7 @@ fu_hwids_init (FuHwids *self)
self->hash_dmi_hw = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
self->hash_dmi_display = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
self->hash_guid = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
self->array_guids = g_ptr_array_new_with_free_func (g_free);
}

FuHwids *
Expand Down
1 change: 1 addition & 0 deletions src/fu-hwids.h
Expand Up @@ -56,6 +56,7 @@ gchar *fu_hwids_get_replace_values (FuHwids *self,
gchar *fu_hwids_get_guid (FuHwids *self,
const gchar *keys,
GError **error);
GPtrArray *fu_hwids_get_guids (FuHwids *self);
gboolean fu_hwids_has_guid (FuHwids *self,
const gchar *guid);
gboolean fu_hwids_setup (FuHwids *self,
Expand Down

0 comments on commit 1bf1636

Please sign in to comment.