Repository navigation
Fel error decoding - #259
jameshilliard wants to merge 1 commit into
Conversation
|
Thanks @jameshilliard, this completely fixes the issue. I tested this series on physical Allwinner A733 hardware (Orange Pi 4 Pro) in FEL mode (rebased with the A733 SoC definitions from #258):
Tested-by: Alastair D'Silva alastair@d-silva.org |
cbd3685 to
a90b1b7
Compare
|
I cleaned up some of the error handling logic to better match the allwinner reference implementations, it shouldn't break anything but might be a good idea to double check with another test. |
paulkocialkowski
left a comment
There was a problem hiding this comment.
The first part of the commit message is quite unclear and doesn't provide enough context about the problem it's trying to solve. The rest sounds like an attempt of describing the code into a few words, which does not explain much either. It needs to be improved.
Overall the code is not very good and needs a significant rework.
| /* AW_FEL_1_READ request */ | ||
| void aw_fel_read(feldev_handle *dev, uint32_t offset, void *buf, size_t len) | ||
| { | ||
| /* An eight-byte memory value could be identical to an error status. */ |
There was a problem hiding this comment.
This is really not enough to be understandable. It needs to say that requests for exactly the size of the returned error status need to be split since we cannot reliably detect what is an error code and what is not (and mostly rely on the size for that).
| uint16_t tag; | ||
| uint8_t state; | ||
| uint8_t rev[3]; | ||
| }; |
| int len = usb_bulk_recv(dev->usb->handle, dev->usb->endpoint_in, | ||
| buf, sizeof(buf)); | ||
| if (len != sizeof(buf) || memcmp(buf, "AWUS", 4) != 0) { | ||
| fprintf(stderr, "Invalid AWUS response\n"); |
There was a problem hiding this comment.
"AWUS" is just a string identifier, it does not refer to anything and should certainly not be mentioned in the log. Also != 0 is redundant.
| fprintf(stderr, "Invalid AWUS response\n"); | ||
| exit(2); | ||
| } | ||
| if (buf[12] != 0) { |
There was a problem hiding this comment.
Maybe make it a packed struct instead of accessing offsets manually?
| if (le16toh(status->mark) != 0xffff || status->tag != 0) { | ||
| fprintf(stderr, "Invalid FEL status response\n"); | ||
| exit(2); | ||
| } |
There was a problem hiding this comment.
Maybe add a newline here.
| length -= recv; | ||
| data += recv; | ||
| } | ||
| /* A short transfer ends this stage; the next packet may be AWUS. */ |
There was a problem hiding this comment.
Honestly the while loop was the only point of this. There's no point in keeping a helper that just wraps libusb_bulk_transfer and does little else. It will also probably return an uninitialised recv value in case of failure. No good.
There was a problem hiding this comment.
Removed the wrapper. The previous error path already exited through usb_error(..., 2) before using the receive count. The new code calls libusb directly, retaining its required int count while using size_t between helpers.
| buf, capacity); | ||
| aw_read_usb_response(dev); | ||
| if ((size_t)received != len) { | ||
| if (received == sizeof(status)) { |
There was a problem hiding this comment.
The two nested ifs would look better side-by-side.
| memmove(&status, buf, sizeof(status)); | ||
| if (le16toh(status.mark) == 0xffff) | ||
| aw_check_fel_status(&status); | ||
| } |
There was a problem hiding this comment.
This should probably not fall through.
There was a problem hiding this comment.
This path now always terminates. A nonzero status reports the FEL error; a success status received instead of the requested data is also rejected. The USB response is consumed first.
| aw_fel_read(dev, offset, buf, len / 2); | ||
| aw_fel_read(dev, offset + len / 2, (char *)buf + len / 2, len / 2); |
There was a problem hiding this comment.
Honestly this looks pretty ugly.
| received = usb_bulk_recv(dev->usb->handle, dev->usb->endpoint_in, | ||
| buf, capacity); | ||
| aw_read_usb_response(dev); | ||
| if ((size_t)received != len) { |
There was a problem hiding this comment.
We should probably have a check that len is never sizeof(status), otherwise we cannot distinguish between legit read data and error code, and we cannot do any request splitting here.
There was a problem hiding this comment.
Added the check in aw_read_fel_data(). Normal FEL status reads are also eight bytes, so the USB transport helper must still allow them. Eight-byte memory reads are now split using a loop.
a90b1b7 to
3c5ca34
Compare
Reworked and applied the suggested changes, does that look better? |
paulkocialkowski
left a comment
There was a problem hiding this comment.
This is a getting better on the code side, but the commit message is still unacceptable as-is. It does not clearly explain what the commit does and even brings more confusion. Please replace it with a clear enumeration of the changes brought to the code, such as:
- aw_read_usb_reponse now prints an error instead of failing assert when the response is malformed;
- aw_read_usb_response now checks the response status;
- aw_usb_read now reads at least the size of the status structure and expects a buffer with at least this size;
- libusb_bulk_transfer is now used instead of usb_bulk_recv, with better error checking and the actual read size returned;
- aw_check_fel_status is introduced to validate a status structure (expected or not);
- aw_read_fel_data is introduced to wrap reads and catch unexpected status;
- aw_fel_read now has to split reads of the same length as an (unexpected) status in two.
|
|
||
| aw_send_usb_request(dev, AW_USB_READ, len); | ||
| usb_bulk_recv(dev->usb->handle, dev->usb->endpoint_in, data, len); | ||
| /* A short transfer ends the data stage; do not read into the response. */ |
There was a problem hiding this comment.
That comment is confusing and useless. Please remove it.
| if (received == sizeof(status)) { | ||
| memmove(&status, buf, sizeof(status)); | ||
| aw_check_fel_status(&status, received); | ||
| fprintf(stderr, "Unexpected FEL status instead of read data\n"); |
There was a problem hiding this comment.
This would be best moved at the start of the nested block, so we can still see it if the status is somehow corrupted too and aw_check_fel_status exits.
There was a problem hiding this comment.
Moved the diagnostic to the start of the block, before copying or validating the status.
|
|
||
| static void aw_usb_read(feldev_handle *dev, void *data, size_t len) | ||
| static size_t aw_usb_read(feldev_handle *dev, void *data, size_t len, | ||
| size_t max_len) |
There was a problem hiding this comment.
Calling this max_len is confusing. This is really the min_len that is used for read.
Also I think the length logic should be moved into this function, so min_len becomes a local variable here and we assume that the caller's data size is always sufficient to hold a status struct. There should be a comment in the code to clarify this expectation.
I believe we should never call aw_usb_read with less than enough memory to hold the status, so that max_len is not even really a variable argument in the first place and must always be the size of the status struct.
There was a problem hiding this comment.
Removed the argument. aw_usb_read() now uses a local min_len = sizeof(struct aw_fel_status) and documents the minimum buffer size required from callers. The length sent in the USB request header is unchanged.
| size_t max_len = len; | ||
| size_t received; | ||
|
|
||
| assert(len != sizeof(status)); |
There was a problem hiding this comment.
There should be a comment explaining that this function uses the status size in order to detect an unexpected status and thus cannot honor requests of the same size, which have to be split by the caller.
There was a problem hiding this comment.
Added a comment above aw_read_fel_data() explaining the size-based detection and that callers must split requests of that size.
3c5ca34 to
cf4bd35
Compare
|
I think we are very close now. The commit message is almost good:
|
cf4bd35 to
8f7cdd6
Compare
paulkocialkowski
left a comment
There was a problem hiding this comment.
Ah sorry forgot to send the review along with the previous message.
| } | ||
| } | ||
|
|
||
| /* |
There was a problem hiding this comment.
Please move this comment above the assert in the function below.
There was a problem hiding this comment.
Moved the size-check comment directly above assert(len != sizeof(status)) in 8a625e2.
|
|
||
| static void aw_usb_read(feldev_handle *dev, void *data, size_t len) | ||
| /* | ||
| * The caller's buffer must hold len bytes or a FEL status, whichever is larger. |
There was a problem hiding this comment.
Please move this comment above the min_len definition. Also rework it to fit in one line, something like "The data buffer must be large enough to fit the FEL status."
There was a problem hiding this comment.
Condensed the buffer requirement to one line directly above min_len in 8a625e2.
Decode error responses at both the USB transport and FEL command layers: - Describe USB responses and FEL statuses with packed structures. - Replace usb_bulk_recv() with direct libusb_bulk_transfer() calls, checking errors and retaining the actual received length. - Make aw_read_usb_response() report malformed responses instead of asserting, and reject nonzero USB transport status codes. - Make aw_usb_read() allow room for at least one FEL status in the receive transfer. Document the required buffer size. - Add aw_check_fel_status() to validate the status length, mark, tag and state, for both expected and unexpected status responses. - Add aw_read_fel_data() to catch unexpected statuses by their size, report them before validation, and use a temporary buffer for data reads smaller than a status. - Split eight-byte memory reads into two four-byte requests in aw_fel_read(), so read data cannot be confused with a FEL status. Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
8f7cdd6 to
8a625e2
Compare
|
Looks good to me! @apritzel do you have thoughts on this? |
Decode error responses at both the USB transport and FEL command layers:
usb_bulk_recv()with directlibusb_bulk_transfer()calls, checking errors and retaining the actual received length.aw_read_usb_response()report malformed responses instead of asserting, and reject nonzero USB transport status codes.aw_usb_read()allow room for at least one FEL status in the receive transfer. Document the required buffer size.aw_check_fel_status()to validate the status length, mark, tag and state, for both expected and unexpected status responses.aw_read_fel_data()to catch unexpected statuses by their size, report them before validation, and use a temporary buffer for data reads smaller than a status.aw_fel_read(), so read data cannot be confused with a FEL status.