Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

Commit

Permalink
WIP remove GD32 recvControl workaround
Browse files Browse the repository at this point in the history
Remove the GD32 `recvControl` workaround, because changes to the USB
core mean that it's no longer required, and the automatic buffer will
cause stack corruption due to being written to after `recvControl`
returns.
  • Loading branch information
tlyu committed Jan 11, 2023
1 parent 1c88577 commit f5bc8e7
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 54 deletions.
29 changes: 2 additions & 27 deletions src/BootKeyboard/BootKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,37 +179,12 @@ bool BootKeyboard_::setup(USBSetup& setup) {
// Check if data has the correct length afterwards
int length = setup.wLength;


// ------------------------------------------------------------
// Workaround for a bug in the GD32 core:
//
// On GD32, when we call `USV_RecvControl`, it casts the (void*) pointer
// we give it to `uint16_t*`, which means that it will alway write an even
// number of bytes to that pointer. Because we don't want to overwrite
// the next byte in memory past `leds`, we use a temporary array that is
// guaranteed to be big enough, and copy the data from that:
if (setup.wValueH == HID_REPORT_TYPE_OUTPUT) {
if (length == sizeof(leds)) {
uint8_t raw_report_data[2];
USB_RecvControl(&raw_report_data, length);
leds = raw_report_data[0];
USB_RecvControl(&leds, length);
return true;
// } else {
// char tmp[8];
// USB_RecvControl(&tmp, length);
// return true;
}
}
}
// Once the GD32 core bug is fixed, we can replace the above code with the
// original code below:
// ------------------------------------------------------------
// if (setup.wValueH == HID_REPORT_TYPE_OUTPUT) {
// if (length == sizeof(leds)) {
// USB_RecvControl(&leds, length);
// return true;
// }
// }
// ------------------------------------------------------------

// Input (set HID report)
else if (setup.wValueH == HID_REPORT_TYPE_INPUT) {
Expand Down
29 changes: 2 additions & 27 deletions src/HID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,37 +151,12 @@ bool HID_::setup(USBSetup& setup) {
if (request == HID_SET_REPORT) {
uint16_t length = setup.wLength;

// ------------------------------------------------------------
// Workaround for a bug in the GD32 core:
//
// On GD32, when we call `USV_RecvControl`, it casts the (void*) pointer
// we give it to `uint16_t*`, which means that it will alway write an even
// number of bytes to that pointer. Because we might be trying to just
// read the `leds` byte, and we don't want to overwrite the next byte in
// memory, instead of giving it the pointer to the `setReportData` member
// variable directly, we have it write into to temporary `raw_report_data`
// array that's guaranteed to be big enough, then copy the data from that
// array into `setReportData`:
uint8_t raw_report_data[sizeof(setReportData) + 1];
if (length == sizeof(setReportData)) {
USB_RecvControl(&raw_report_data, length);
setReportData.reportId = raw_report_data[0];
setReportData.leds = raw_report_data[1];
USB_RecvControl(&setReportData, length);
} else if (length == sizeof(setReportData.leds)) {
USB_RecvControl(&raw_report_data, length);
USB_RecvControl(&setReportData.leds, length);
setReportData.reportId = 0;
setReportData.leds = raw_report_data[0];
}
// Once the GD32 core bug is fixed, we can replace the above code with the
// original code below:
// ------------------------------------------------------------
// if (length == sizeof(setReportData)) {
// USB_RecvControl(&setReportData, length);
// } else if (length == sizeof(setReportData.leds)) {
// USB_RecvControl(&setReportData.leds, length);
// setReportData.reportId = 0;
// }
// ------------------------------------------------------------
return true;
}
}
Expand Down

0 comments on commit f5bc8e7

Please sign in to comment.