main.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Builtin firmware support */
  3. #include <linux/firmware.h>
  4. #include "../firmware.h"
  5. /* Only if FW_LOADER=y */
  6. #ifdef CONFIG_FW_LOADER
  7. struct builtin_fw {
  8. char *name;
  9. void *data;
  10. unsigned long size;
  11. };
  12. extern struct builtin_fw __start_builtin_fw[];
  13. extern struct builtin_fw __end_builtin_fw[];
  14. static bool fw_copy_to_prealloc_buf(struct firmware *fw,
  15. void *buf, size_t size)
  16. {
  17. if (!buf)
  18. return true;
  19. if (size < fw->size)
  20. return false;
  21. memcpy(buf, fw->data, fw->size);
  22. return true;
  23. }
  24. /**
  25. * firmware_request_builtin() - load builtin firmware
  26. * @fw: pointer to firmware struct
  27. * @name: name of firmware file
  28. *
  29. * Some use cases in the kernel have a requirement so that no memory allocator
  30. * is involved as these calls take place early in boot process. An example is
  31. * the x86 CPU microcode loader. In these cases all the caller wants is to see
  32. * if the firmware was built-in and if so use it right away. This can be used
  33. * for such cases.
  34. *
  35. * This looks for the firmware in the built-in kernel. Only if the kernel was
  36. * built-in with the firmware you are looking for will this return successfully.
  37. *
  38. * Callers of this API do not need to use release_firmware() as the pointer to
  39. * the firmware is expected to be provided locally on the stack of the caller.
  40. **/
  41. bool firmware_request_builtin(struct firmware *fw, const char *name)
  42. {
  43. struct builtin_fw *b_fw;
  44. if (!fw)
  45. return false;
  46. for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++) {
  47. if (strcmp(name, b_fw->name) == 0) {
  48. fw->size = b_fw->size;
  49. fw->data = b_fw->data;
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. EXPORT_SYMBOL_NS_GPL(firmware_request_builtin, TEST_FIRMWARE);
  56. /**
  57. * firmware_request_builtin_buf() - load builtin firmware into optional buffer
  58. * @fw: pointer to firmware struct
  59. * @name: name of firmware file
  60. * @buf: If set this lets you use a pre-allocated buffer so that the built-in
  61. * firmware into is copied into. This field can be NULL. It is used by
  62. * callers such as request_firmware_into_buf() and
  63. * request_partial_firmware_into_buf()
  64. * @size: if buf was provided, the max size of the allocated buffer available.
  65. * If the built-in firmware does not fit into the pre-allocated @buf this
  66. * call will fail.
  67. *
  68. * This looks for the firmware in the built-in kernel. Only if the kernel was
  69. * built-in with the firmware you are looking for will this call possibly
  70. * succeed. If you passed a @buf the firmware will be copied into it *iff* the
  71. * built-in firmware fits into the pre-allocated buffer size specified in
  72. * @size.
  73. *
  74. * This caller is to be used internally by the firmware_loader only.
  75. **/
  76. bool firmware_request_builtin_buf(struct firmware *fw, const char *name,
  77. void *buf, size_t size)
  78. {
  79. if (!firmware_request_builtin(fw, name))
  80. return false;
  81. return fw_copy_to_prealloc_buf(fw, buf, size);
  82. }
  83. bool firmware_is_builtin(const struct firmware *fw)
  84. {
  85. struct builtin_fw *b_fw;
  86. for (b_fw = __start_builtin_fw; b_fw != __end_builtin_fw; b_fw++)
  87. if (fw->data == b_fw->data)
  88. return true;
  89. return false;
  90. }
  91. #endif