Skip to contentSkip to conversation

Bluetooth: btrtl: Add firmware format v3 support

PATCH In review Hilda Wu 2 participants 2 messages Nov 6 Bluetooth drivers

lore
Share & tools
lore

Hilda Wu

author
#0 lore raw

PATCH v9 1/1 Bluetooth: btrtl: Add firmware format v3 support

Realtek has introduced a new Bluetooth firmware format, firmware format v3. This is a Realtek-internal specification; the format differences from v2 are documented inline in this patch.

This patch extends the btrtl driver to recognize and parse the new v3 file format, including:
- New signature string and subsection ID definitions
- Extension of btrtl_device_info to store v3-specific metadata
- Logic to extract and load firmware data out of v3
- Maintains compatibility with the existing v2 firmware format

The RTL8922D is the first IC to use firmware format v3. The firmware binary (rtl8922du_fw.bin) payload is approximately 267 KB (0x40a7c bytes), and on a typical boot the full sequence from rtl_load_file() to rtl_finalize_download() completing takes average ~483 ms.

The following is RTL8922D log as expected fw format v3 output:

[  975.086879] Bluetooth: hci0: RTL: chip_id status=0x00 id=0x3e
[  975.087279] Bluetooth: hci0: RTL: hci 0e.000d lmp 0e.8922
[  975.087552] Bluetooth: hci0: RTL: rom_version status=0 version=0
[  975.087823] Bluetooth: hci0: RTL: key id 0
[  975.087826] Bluetooth: hci0: RTL: loading rtl_bt/rtl8922du_fw.bin
[  975.124215] Bluetooth: hci0: RTL: loading rtl_bt/rtl8922du_config.bin
[  975.124257] Bluetooth: hci0: RTL: no cfg, ignored
[  975.124259] Bluetooth: hci0: RTL: fw key id 0
[  975.124260] Bluetooth: hci0: RTL: sub f000:00 chip 62 cut 01 len 0000e9d8
[  975.124268] Bluetooth: hci0: RTL: subsection ver b99a1ad9
[  975.124269] Bluetooth: hci0: RTL: cfg rtl_bt/rtl8922du_config_f000.bin
[  975.124287] Bluetooth: hci0: RTL: cfg not found, use default
[  975.124288] Bluetooth: hci0: RTL: sub f002:00 chip 62 cut 01 len 000313c0
[  975.124304] Bluetooth: hci0: RTL: subsection ver 258faaa1
[  975.124305] Bluetooth: hci0: RTL: cfg rtl_bt/rtl8922du_config_f002.bin
[  975.143601] Bluetooth: hci0: RTL: cfg found
[  975.143608] Bluetooth: hci0: RTL: skip f000:00: key_id 1 != 0
[  975.143610] Bluetooth: hci0: RTL: skip f002:00: key_id 1 != 0
[  975.143611] Bluetooth: hci0: RTL: skip f000:00: key_id 2 != 0
[  975.143613] Bluetooth: hci0: RTL: skip f002:00: key_id 2 != 0
[  975.143614] Bluetooth: hci0: RTL: fw payload len 0x0003fd98
[  975.565004] Bluetooth: hci0: RTL: Watchdog reset status 00
[  975.565401] Bluetooth: hci0: RTL: fw version 0x258faaa1

---
Note: checkpatch reports FILE_PATH_CHANGES for the new files. They are already covered by "F: drivers/bluetooth/" in the BLUETOOTH SUBSYSTEM entry, so MAINTAINERS is not updated.

V8 -> V9:
- Adjust the length of each log message (< 80)
- Add Tested-by

V7 -> V8:
- Fix potential memory leak of v3 firmware patch images: merge btrtl_enh.c into the same btrtl_core.ko module via kbuild composite object, instead of a separate module accessed through symbol_get()/symbol_put(). This removes the mismatch between the module's reference count and the lifetime of the dynamically allocated patch image/config buffers (Reported by Sashiko)
- Add a bounds check on skb->len before accessing skb->data[0] in btusb_recv_event_realtek(), to avoid an out-of-bounds read when a zero-length packet is received (Reported by Sashiko)
- Return the error immediately instead of silently continuing to the next subsection when rtlbt_parse_section_v3() fails, to avoid loading incomplete firmware onto the controller (Reported by Sashiko)
- Fix a skb leak in the btrtl_recv_event() (Reported by Sashiko)
- Add a comment clarifying that the security project ID (key_id) register is only present starting from RTL8922A and V3-capable chips; legacy chips intentionally skip this read (Reported by Sashiko)

V6 -> V7:
- Fix smatch warning in btrtl_free(): move the NULL check on btrtl_dev to the very beginning of the function, before any dereference, instead of after it has already been dereferenced multiple times (Reported by kernel test / Dan Carpenter)
- Revise log message to use "subsection" terminology for clarity
- Add informational message when a config file is not found

V5 -> V6:
- Add missing parentheses around the entire is_v3_fw() macro expression to avoid incorrect evaluation with the logical NOT operator on legacy firmware types
- Add NULL check on ic_info->cfg_name before use in rtlbt_parse_config() to avoid NULL pointer dereference when a crafted firmware specifies FW_TYPE_V3_2 on legacy chips without configuration
- Add bounds check to reject patch_image_len < 4 before reading the image version, preventing an out-of-bounds read
- Propagate the return value of rtlbt_parse_config() and abort parsing on failure instead of silently continuing without the required configuration
- Fix a race condition in rtl_check_download_state() by setting the REALTEK_DOWNLOADING flag before issuing the check-download command, instead of after, to avoid missing the completion event
- Split v3 firmware parse/download logic into new btrtl_enh.c to keep btrtl.c size manageable

V4 -> V5:
- Add independent support for RTL8922D section
- Introduce macros to improve code readability
- Document firmware format v3 and its differences
- Align implementation with reviewer feedback

V3 -> V4:
- Rework skb->data access and add clarifying comments
- Fix latent issues

V2 -> V3:
- Address coccinelle warning

V1 -> V2:
- Add missing symbols
- Resolve build warnings

---
 drivers/bluetooth/Makefile     |   4 +-
 drivers/bluetooth/btrtl.c      | 208 ++++++---
 drivers/bluetooth/btrtl.h      |  87 +++-
 drivers/bluetooth/btrtl_enh.c  | 804 +++++++++++++++++++++++++++++++++
 drivers/bluetooth/btusb_main.c |  25 +-
 5 files changed, 1049 insertions(+), 79 deletions(-)
 create mode 100644 drivers/bluetooth/btrtl_enh.c
drivers/bluetooth/Makefile +3 -1
index 7d933755b291..68f00a867fc8 100644
@@ -22,7 +22,7 @@ obj-$(CONFIG_BT_MTKSDIO)	+= btmtksdio.o
 obj-$(CONFIG_BT_MTKUART)	+= btmtkuart.o
 obj-$(CONFIG_BT_QCOMSMD)	+= btqcomsmd.o
 obj-$(CONFIG_BT_BCM)		+= btbcm.o
-obj-$(CONFIG_BT_RTL)		+= btrtl.o
+obj-$(CONFIG_BT_RTL)		+= btrtl_core.o
 obj-$(CONFIG_BT_QCA)		+= btqca.o
 obj-$(CONFIG_BT_MTK)		+= btmtk.o
 
@@ -36,6 +36,8 @@ obj-$(CONFIG_BT_HCIRSI)		+= btrsi.o
 btmrvl-y			:= btmrvl_main.o
 btmrvl-$(CONFIG_DEBUG_FS)	+= btmrvl_debugfs.o
 
+btrtl_core-y			:= btrtl.o btrtl_enh.o
+
 hci_uart-y				:= hci_ldisc.o
 hci_uart-$(CONFIG_BT_HCIUART_SERDEV)	+= hci_serdev.o
 hci_uart-$(CONFIG_BT_HCIUART_H4)	+= hci_h4.o
drivers/bluetooth/btrtl.c +142 -66

466 lines not on this page · raw message

drivers/bluetooth/btrtl.h +86 -1

120 lines not on this page · raw message

drivers/bluetooth/btrtl_enh.c new file +804 -0

804 lines not on this page · raw message

drivers/bluetooth/btusb_main.c +14 -11
index 95398e2ca098..812ff575a560 100644
@@ -2976,20 +2976,23 @@ static int btusb_setup_realtek(struct hci_dev *hdev)
 
 static int btusb_recv_event_realtek(struct hci_dev *hdev, struct sk_buff *skb)
 {
-	if (skb->len >= HCI_EVENT_HDR_SIZE + 1 &&
-	    skb->data[0] == HCI_EV_VENDOR &&
-	    skb->data[2] == RTK_SUB_EVENT_CODE_COREDUMP) {
-		struct rtk_dev_coredump_hdr hdr = {
-			.code = RTK_DEVCOREDUMP_CODE_MEMDUMP,
-		};
+	if (skb->len >= 1 && skb->data[0] == HCI_EV_VENDOR) {
+		if (skb->len >= HCI_EVENT_HDR_SIZE + 1 &&
+		    skb->data[2] == RTK_SUB_EVENT_CODE_COREDUMP) {
+			struct rtk_dev_coredump_hdr hdr = {
+				.code = RTK_DEVCOREDUMP_CODE_MEMDUMP,
+			};
 
-		bt_dev_dbg(hdev, "RTL: received coredump vendor evt, len %u",
-			skb->len);
+			bt_dev_dbg(hdev, "RTL: received coredump vendor evt, len %u",
+				   skb->len);
 
-		btusb_rtl_alloc_devcoredump(hdev, &hdr, skb->data, skb->len);
-		kfree_skb(skb);
+			btusb_rtl_alloc_devcoredump(hdev, &hdr, skb->data, skb->len);
+			kfree_skb(skb);
 
-		return 0;
+			return 0;
+		}
+
+		return btrtl_recv_event(hdev, skb);
 	}
 
 	return hci_recv_frame(hdev, skb);
Signature · 1 line
2.34.1
#1 lore raw

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list. This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/series/1204022/

---Test result---

Test Summary:

CheckPatch                    FAIL      2.39 seconds
VerifyFixes                   PASS      0.13 seconds
VerifySignedoff               PASS      0.09 seconds
GitLint                       PASS      0.23 seconds
SubjectPrefix                 PASS      0.08 seconds
BuildKernel                   PASS      25.11 seconds
CheckAllWarning               PASS      27.94 seconds
CheckSparse                   PASS      30.44 seconds
BuildKernel32                 PASS      24.12 seconds
CheckKernelLLVM               PASS      24.32 seconds
TestRunnerSetup               PASS      564.97 seconds
IncrementalBuild              PASS      29.28 seconds

Details
##############################
Test: CheckPatch - FAIL
Desc: Run checkpatch.pl script
Output:
[v9] Bluetooth: btrtl: Add firmware format v3 support
WARNING: added, moved or deleted file(s), does MAINTAINERS need updating? #883:
new file mode 100644

total: 0 errors, 1 warnings, 1440 lines checked

NOTE: For some of the reported defects, checkpatch may be able to
mechanically convert to the typical style using --fix or --fix-inplace.

/home/runner/work/bluetooth-next/bluetooth-next/src/patch/14957122.patch has style problems, please review.

NOTE: Ignored message types: UNKNOWN_COMMIT_ID

NOTE: If any of the errors are false positives, please report
them to the maintainer, see CHECKPATCH in MAINTAINERS.

https://github.com/bluez/bluetooth-next/pull/1078

---
Regards,
Linux Bluetooth