Skip to content

Fel error decoding - #259

Open
jameshilliard wants to merge 1 commit into
linux-sunxi:masterfrom
jameshilliard:fel-error-decoding
Open

jameshilliard wants to merge 1 commit into
linux-sunxi:masterfrom
jameshilliard:fel-error-decoding

Conversation

@jameshilliard

@jameshilliard jameshilliard commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

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.

@deece

deece commented Oct 7, 2026

Copy link
Copy Markdown

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):

  1. Short reads on valid memory:
    Tested reading lengths of 4, 8, 16, 32, 64, 128, 256, 512, and 1024 bytes from SRAM. All succeeded with exit code 0, and a 1024-byte binary test pattern written to SRAM verified bit-for-bit on readback (cmp clean).

  2. Rejected / protected memory reads (the failure from Add FEL support for Allwinner SoC A733 #223 / fel_lib: Handle USB bulk transfers smaller than 512 bytes safely #257):
    Attempting to read rejected BROM address spaces (e.g., sunxi-fel read 0x17000 0x200):

    • Before: The while (length > 0) loop consumed the 8-byte status and the subsequent 13-byte AWUS frame, hanging until usb_bulk_recv() ERROR -7: Operation timed out, followed by ERROR -8: Overflow and permanent USB desync.
    • With Fel error decoding #259: It immediately and cleanly catches the error packet without timing out:
      FEL command failed with status 0x01
      
  3. Transport sync recovery:
    Immediately following the rejected read above, subsequent commands (ver, sid, readl, write) on the same USB session executed cleanly without needing a USB bus reset or board power cycle.

  4. SPL execution:
    Successfully transferred and executed a 136 KB native U-Boot SPL via SRAM swap buffers (sunxi-fel spl sunxi-spl.bin), bringing up LPDDR5 and DRAM diagnostics.
    This addresses the true root cause cleanly without needing the bounce buffer workaround in fel_lib: Handle USB bulk transfers smaller than 512 bytes safely #257. I will close fel_lib: Handle USB bulk transfers smaller than 512 bytes safely #257 in favor of this PR.

Tested-by: Alastair D'Silva alastair@d-silva.org

@jameshilliard
jameshilliard force-pushed the fel-error-decoding branch 2 times, most recently from cbd3685 to a90b1b7 Compare October 7, 2026 19:09
@jameshilliard

Copy link
Copy Markdown
Contributor Author

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 paulkocialkowski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread fel_lib.c Outdated
/* 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. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Comment thread fel_lib.c Outdated
uint16_t tag;
uint8_t state;
uint8_t rev[3];
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be packed.

Comment thread fel_lib.c Outdated
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");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"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.

Comment thread fel_lib.c Outdated
fprintf(stderr, "Invalid AWUS response\n");
exit(2);
}
if (buf[12] != 0) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make it a packed struct instead of accessing offsets manually?

Comment thread fel_lib.c
if (le16toh(status->mark) != 0xffff || status->tag != 0) {
fprintf(stderr, "Invalid FEL status response\n");
exit(2);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a newline here.

Comment thread fel_lib.c Outdated
length -= recv;
data += recv;
}
/* A short transfer ends this stage; the next packet may be AWUS. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread fel_lib.c Outdated
buf, capacity);
aw_read_usb_response(dev);
if ((size_t)received != len) {
if (received == sizeof(status)) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The two nested ifs would look better side-by-side.

Comment thread fel_lib.c Outdated
memmove(&status, buf, sizeof(status));
if (le16toh(status.mark) == 0xffff)
aw_check_fel_status(&status);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably not fall through.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread fel_lib.c Outdated
Comment on lines +260 to +261
aw_fel_read(dev, offset, buf, len / 2);
aw_fel_read(dev, offset + len / 2, (char *)buf + len / 2, len / 2);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly this looks pretty ugly.

Comment thread fel_lib.c Outdated
received = usb_bulk_recv(dev->usb->handle, dev->usb->endpoint_in,
buf, capacity);
aw_read_usb_response(dev);
if ((size_t)received != len) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jameshilliard

Copy link
Copy Markdown
Contributor Author

Overall the code is not very good and needs a significant rework.

Reworked and applied the suggested changes, does that look better?

@paulkocialkowski paulkocialkowski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread fel_lib.c Outdated

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. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That comment is confusing and useless. Please remove it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in cf4bd35.

Comment thread fel_lib.c Outdated
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");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the diagnostic to the start of the block, before copying or validating the status.

Comment thread fel_lib.c Outdated

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread fel_lib.c
size_t max_len = len;
size_t received;

assert(len != sizeof(status));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment above aw_read_fel_data() explaining the size-based detection and that callers must split requests of that size.

@paulkocialkowski

Copy link
Copy Markdown
Member

I think we are very close now. The commit message is almost good:

  • "and keep the length in the USB request header unchanged." is incorrect (or at best irrelevant). Remove it.
  • "Other response sizes must match the requested data length." is obvious and stating it in the list of changes is confusing.

@paulkocialkowski paulkocialkowski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry forgot to send the review along with the previous message.

Comment thread fel_lib.c Outdated
}
}

/*

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this comment above the assert in the function below.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the size-check comment directly above assert(len != sizeof(status)) in 8a625e2.

Comment thread fel_lib.c Outdated

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@paulkocialkowski

Copy link
Copy Markdown
Member

Looks good to me! @apritzel do you have thoughts on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants