Skip to content

Commit 5658c76

Browse files
dwmw2David Woodhouse
authored andcommitted
firmware: allow firmware files to be built into kernel image
Some drivers have their own hacks to bypass the kernel's firmware loader and build their firmware into the kernel; this renders those unnecessary. Other drivers don't use the firmware loader at all, because they always want the firmware to be available. This allows them to start using the firmware loader. A third set of drivers already use the firmware loader, but can't be used without help from userspace, which sometimes requires an initrd. This allows them to work in a static kernel. Signed-off-by: David Woodhouse <dwmw2@infradead.org>
1 parent b7a39bd commit 5658c76

3 files changed

Lines changed: 59 additions & 2 deletions

File tree

‎drivers/base/firmware_class.c‎

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ struct firmware_priv {
4949
struct timer_list timeout;
5050
};
5151

52+
#ifdef CONFIG_FW_LOADER
53+
extern struct builtin_fw __start_builtin_fw[];
54+
extern struct builtin_fw __end_builtin_fw[];
55+
#else /* Module case. Avoid ifdefs later; it'll all optimise out */
56+
static struct builtin_fw *__start_builtin_fw;
57+
static struct builtin_fw *__end_builtin_fw;
58+
#endif
59+
5260
static void
5361
fw_load_abort(struct firmware_priv *fw_priv)
5462
{
@@ -391,13 +399,12 @@ _request_firmware(const struct firmware **firmware_p, const char *name,
391399
struct device *f_dev;
392400
struct firmware_priv *fw_priv;
393401
struct firmware *firmware;
402+
struct builtin_fw *builtin;
394403
int retval;
395404

396405
if (!firmware_p)
397406
return -EINVAL;
398407

399-
printk(KERN_INFO "firmware: requesting %s\n", name);
400-
401408
*firmware_p = firmware = kzalloc(sizeof(*firmware), GFP_KERNEL);
402409
if (!firmware) {
403410
printk(KERN_ERR "%s: kmalloc(struct firmware) failed\n",
@@ -406,6 +413,20 @@ _request_firmware(const struct firmware **firmware_p, const char *name,
406413
goto out;
407414
}
408415

416+
for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
417+
builtin++) {
418+
if (strcmp(name, builtin->name))
419+
continue;
420+
printk(KERN_INFO "firmware: using built-in firmware %s\n",
421+
name);
422+
firmware->size = builtin->size;
423+
firmware->data = builtin->data;
424+
return 0;
425+
}
426+
427+
if (uevent)
428+
printk(KERN_INFO "firmware: requesting %s\n", name);
429+
409430
retval = fw_setup_device(firmware, &f_dev, name, device, uevent);
410431
if (retval)
411432
goto error_kfree_fw;
@@ -473,8 +494,16 @@ request_firmware(const struct firmware **firmware_p, const char *name,
473494
void
474495
release_firmware(const struct firmware *fw)
475496
{
497+
struct builtin_fw *builtin;
498+
476499
if (fw) {
500+
for (builtin = __start_builtin_fw; builtin != __end_builtin_fw;
501+
builtin++) {
502+
if (fw->data == builtin->data)
503+
goto free_fw;
504+
}
477505
vfree(fw->data);
506+
free_fw:
478507
kfree(fw);
479508
}
480509
}

‎include/asm-generic/vmlinux.lds.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@
8686
VMLINUX_SYMBOL(__end_pci_fixups_resume) = .; \
8787
} \
8888
\
89+
/* Built-in firmware blobs */ \
90+
.builtin_fw : AT(ADDR(.builtin_fw) - LOAD_OFFSET) { \
91+
VMLINUX_SYMBOL(__start_builtin_fw) = .; \
92+
*(.builtin_fw) \
93+
VMLINUX_SYMBOL(__end_builtin_fw) = .; \
94+
} \
95+
\
8996
/* RapidIO route ops */ \
9097
.rio_route : AT(ADDR(.rio_route) - LOAD_OFFSET) { \
9198
VMLINUX_SYMBOL(__start_rio_route_ops) = .; \

‎include/linux/firmware.h‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#ifndef _LINUX_FIRMWARE_H
22
#define _LINUX_FIRMWARE_H
3+
34
#include <linux/module.h>
45
#include <linux/types.h>
6+
#include <linux/compiler.h>
7+
58
#define FIRMWARE_NAME_MAX 30
69
#define FW_ACTION_NOHOTPLUG 0
710
#define FW_ACTION_HOTPLUG 1
@@ -13,6 +16,24 @@ struct firmware {
1316

1417
struct device;
1518

19+
struct builtin_fw {
20+
char *name;
21+
void *data;
22+
unsigned long size;
23+
};
24+
25+
/* We have to play tricks here much like stringify() to get the
26+
__COUNTER__ macro to be expanded as we want it */
27+
#define __fw_concat1(x, y) x##y
28+
#define __fw_concat(x, y) __fw_concat1(x, y)
29+
30+
#define DECLARE_BUILTIN_FIRMWARE(name, blob) \
31+
DECLARE_BUILTIN_FIRMWARE_SIZE(name, &(blob), sizeof(blob))
32+
33+
#define DECLARE_BUILTIN_FIRMWARE_SIZE(name, blob, size) \
34+
static const struct builtin_fw __fw_concat(__builtin_fw,__COUNTER__) \
35+
__used __section(.builtin_fw) = { name, blob, size }
36+
1637
#if defined(CONFIG_FW_LOADER) || (defined(CONFIG_FW_LOADER_MODULE) && defined(MODULE))
1738
int request_firmware(const struct firmware **fw, const char *name,
1839
struct device *device);

0 commit comments

Comments
 (0)