Path: blob/master/drivers/base/firmware_loader/builtin/main.c
26494 views
// SPDX-License-Identifier: GPL-2.01/* Builtin firmware support */23#include <linux/firmware.h>4#include "../firmware.h"56/* Only if FW_LOADER=y */7#ifdef CONFIG_FW_LOADER89struct builtin_fw {10char *name;11void *data;12unsigned long size;13};1415extern struct builtin_fw __start_builtin_fw[];16extern struct builtin_fw __end_builtin_fw[];1718static bool fw_copy_to_prealloc_buf(struct firmware *fw,19void *buf, size_t size)20{21if (!buf)22return true;23if (size < fw->size)24return false;25memcpy(buf, fw->data, fw->size);26return true;27}2829/**30* firmware_request_builtin() - load builtin firmware31* @fw: pointer to firmware struct32* @name: name of firmware file33*34* Some use cases in the kernel have a requirement so that no memory allocator35* is involved as these calls take place early in boot process. An example is36* the x86 CPU microcode loader. In these cases all the caller wants is to see37* if the firmware was built-in and if so use it right away. This can be used38* for such cases.39*40* This looks for the firmware in the built-in kernel. Only if the kernel was41* built-in with the firmware you are looking for will this return successfully.42*43* Callers of this API do not need to use release_firmware() as the pointer to44* the firmware is expected to be provided locally on the stack of the caller.45**/46bool firmware_request_builtin(struct firmware *fw, const char *name)47{48struct builtin_fw *b_fw;4950if (!fw)51return false;5253for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {54if (strcmp(name, b_fw->name) == 0) {55fw->size = b_fw->size;56fw->data = b_fw->data;57return true;58}59}6061return false;62}63EXPORT_SYMBOL_NS_GPL(firmware_request_builtin, "TEST_FIRMWARE");6465/**66* firmware_request_builtin_buf() - load builtin firmware into optional buffer67* @fw: pointer to firmware struct68* @name: name of firmware file69* @buf: If set this lets you use a pre-allocated buffer so that the built-in70* firmware into is copied into. This field can be NULL. It is used by71* callers such as request_firmware_into_buf() and72* request_partial_firmware_into_buf()73* @size: if buf was provided, the max size of the allocated buffer available.74* If the built-in firmware does not fit into the pre-allocated @buf this75* call will fail.76*77* This looks for the firmware in the built-in kernel. Only if the kernel was78* built-in with the firmware you are looking for will this call possibly79* succeed. If you passed a @buf the firmware will be copied into it *iff* the80* built-in firmware fits into the pre-allocated buffer size specified in81* @size.82*83* This caller is to be used internally by the firmware_loader only.84**/85bool firmware_request_builtin_buf(struct firmware *fw, const char *name,86void *buf, size_t size)87{88if (!firmware_request_builtin(fw, name))89return false;9091return fw_copy_to_prealloc_buf(fw, buf, size);92}9394bool firmware_is_builtin(const struct firmware *fw)95{96struct builtin_fw *b_fw;9798for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)99if (fw->data == b_fw->data)100return true;101102return false;103}104105#endif106107108