git: fb3c549738bf - main - LinuxKPI: firmware: add request_partial_firmware_into_buf()

From: Bjoern A. Zeeb <bz_at_FreeBSD.org>
Date: Wed, 07 Sep 2022 23:55:45 UTC
The branch main has been updated by bz:

URL: https://cgit.FreeBSD.org/src/commit/?id=fb3c549738bfed0607d95120e5dbf03a62e61b49

commit fb3c549738bfed0607d95120e5dbf03a62e61b49
Author:     Bjoern A. Zeeb <bz@FreeBSD.org>
AuthorDate: 2022-09-07 23:33:45 +0000
Commit:     Bjoern A. Zeeb <bz@FreeBSD.org>
CommitDate: 2022-09-07 23:33:45 +0000

    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
---
 .../linuxkpi/common/include/linux/firmware.h       | 13 ++++++++++++
 sys/compat/linuxkpi/common/src/linux_firmware.c    | 24 ++++++++++++++++++++++
 2 files changed, 37 insertions(+)

diff --git a/sys/compat/linuxkpi/common/include/linux/firmware.h b/sys/compat/linuxkpi/common/include/linux/firmware.h
index ada7d0d73edf..ed84d6fbf58f 100644
--- a/sys/compat/linuxkpi/common/include/linux/firmware.h
+++ b/sys/compat/linuxkpi/common/include/linux/firmware.h
@@ -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 */
diff --git a/sys/compat/linuxkpi/common/src/linux_firmware.c b/sys/compat/linuxkpi/common/src/linux_firmware.c
index 47cccd42da20..7e8418151c64 100644
--- a/sys/compat/linuxkpi/common/src/linux_firmware.c
+++ b/sys/compat/linuxkpi/common/src/linux_firmware.c
@@ -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);
+}