Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* Builtin firmware support */
0003 
0004 #include <linux/firmware.h>
0005 #include "../firmware.h"
0006 
0007 /* Only if FW_LOADER=y */
0008 #ifdef CONFIG_FW_LOADER
0009 
0010 struct builtin_fw {
0011     char *name;
0012     void *data;
0013     unsigned long size;
0014 };
0015 
0016 extern struct builtin_fw __start_builtin_fw[];
0017 extern struct builtin_fw __end_builtin_fw[];
0018 
0019 static bool fw_copy_to_prealloc_buf(struct firmware *fw,
0020                     void *buf, size_t size)
0021 {
0022     if (!buf)
0023         return true;
0024     if (size < fw->size)
0025         return false;
0026     memcpy(buf, fw->data, fw->size);
0027     return true;
0028 }
0029 
0030 /**
0031  * firmware_request_builtin() - load builtin firmware
0032  * @fw: pointer to firmware struct
0033  * @name: name of firmware file
0034  *
0035  * Some use cases in the kernel have a requirement so that no memory allocator
0036  * is involved as these calls take place early in boot process. An example is
0037  * the x86 CPU microcode loader. In these cases all the caller wants is to see
0038  * if the firmware was built-in and if so use it right away. This can be used
0039  * for such cases.
0040  *
0041  * This looks for the firmware in the built-in kernel. Only if the kernel was
0042  * built-in with the firmware you are looking for will this return successfully.
0043  *
0044  * Callers of this API do not need to use release_firmware() as the pointer to
0045  * the firmware is expected to be provided locally on the stack of the caller.
0046  **/
0047 bool firmware_request_builtin(struct firmware *fw, const char *name)
0048 {
0049     struct builtin_fw *b_fw;
0050 
0051     if (!fw)
0052         return false;
0053 
0054     for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
0055         if (strcmp(name, b_fw->name) == 0) {
0056             fw->size = b_fw->size;
0057             fw->data = b_fw->data;
0058             return true;
0059         }
0060     }
0061 
0062     return false;
0063 }
0064 EXPORT_SYMBOL_NS_GPL(firmware_request_builtin, TEST_FIRMWARE);
0065 
0066 /**
0067  * firmware_request_builtin_buf() - load builtin firmware into optional buffer
0068  * @fw: pointer to firmware struct
0069  * @name: name of firmware file
0070  * @buf: If set this lets you use a pre-allocated buffer so that the built-in
0071  *  firmware into is copied into. This field can be NULL. It is used by
0072  *  callers such as request_firmware_into_buf() and
0073  *  request_partial_firmware_into_buf()
0074  * @size: if buf was provided, the max size of the allocated buffer available.
0075  *  If the built-in firmware does not fit into the pre-allocated @buf this
0076  *  call will fail.
0077  *
0078  * This looks for the firmware in the built-in kernel. Only if the kernel was
0079  * built-in with the firmware you are looking for will this call possibly
0080  * succeed. If you passed a @buf the firmware will be copied into it *iff* the
0081  * built-in firmware fits into the pre-allocated buffer size specified in
0082  * @size.
0083  *
0084  * This caller is to be used internally by the firmware_loader only.
0085  **/
0086 bool firmware_request_builtin_buf(struct firmware *fw, const char *name,
0087                   void *buf, size_t size)
0088 {
0089     if (!firmware_request_builtin(fw, name))
0090         return false;
0091 
0092     return fw_copy_to_prealloc_buf(fw, buf, size);
0093 }
0094 
0095 bool firmware_is_builtin(const struct firmware *fw)
0096 {
0097     struct builtin_fw *b_fw;
0098 
0099     for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
0100         if (fw->data == b_fw->data)
0101             return true;
0102 
0103     return false;
0104 }
0105 
0106 #endif