LinuxKPI: firmware: add request_partial_firmware_into_buf()

A (so far out-of-tree) driver update needs
request_partial_firmware_into_buf().  Given we load the full .ko file
using firmware(9) just do that and copy the requeste data into the
buffer (rather than poissibly only reading portions of the firmware
file).

MFC after:	1 week
This commit is contained in:
Bjoern A. Zeeb 2022-09-07 23:33:45 +00:00
parent c994352a88
commit fb3c549738
2 changed files with 37 additions and 0 deletions

View File

@ -2,6 +2,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2020-2021 The FreeBSD Foundation
* Copyright (c) 2022 Bjoern A. Zeeb
*
* This software was developed by Björn Zeeb under sponsorship from
* the FreeBSD Foundation.
@ -56,6 +57,8 @@ int linuxkpi_request_firmware(const struct linuxkpi_firmware **,
int linuxkpi_firmware_request_nowarn(const struct linuxkpi_firmware **,
const char *, struct device *);
void linuxkpi_release_firmware(const struct linuxkpi_firmware *);
int linuxkpi_request_partial_firmware_into_buf(const struct linuxkpi_firmware **,
const char *, struct device *, uint8_t *, size_t, size_t);
static __inline int
@ -100,6 +103,16 @@ release_firmware(const struct linuxkpi_firmware *fw)
linuxkpi_release_firmware(fw);
}
static inline int
request_partial_firmware_into_buf(const struct linuxkpi_firmware **fw,
const char *fw_name, struct device *dev, void *buf, size_t buflen,
size_t offset)
{
return (linuxkpi_request_partial_firmware_into_buf(fw, fw_name,
dev, buf, buflen, offset));
}
#define firmware linuxkpi_firmware
#endif /* _LINUXKPI_LINUX_FIRMWARE_H */

View File

@ -2,6 +2,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2020-2021 The FreeBSD Foundation
* Copyright (c) 2022 Bjoern A. Zeeb
*
* This software was developed by Björn Zeeb under sponsorship from
* the FreeBSD Foundation.
@ -223,3 +224,26 @@ linuxkpi_release_firmware(const struct linuxkpi_firmware *fw)
firmware_put(fw->fbdfw, FIRMWARE_UNLOAD);
free(__DECONST(void *, fw), M_LKPI_FW);
}
int
linuxkpi_request_partial_firmware_into_buf(const struct linuxkpi_firmware **fw,
const char *fw_name, struct device *dev, uint8_t *buf, size_t buflen,
size_t offset)
{
const struct linuxkpi_firmware *lfw;
int error;
error = linuxkpi_request_firmware(fw, fw_name, dev);
if (error != 0)
return (error);
lfw = *fw;
if ((offset + buflen) >= lfw->size) {
linuxkpi_release_firmware(lfw);
return (-ERANGE);
}
memcpy(buf, lfw->data + offset, buflen);
return (0);
}