Skip to content

uefi: serial: add read_exact() and write_exact() + fix core::fmt::Write for Serial#1875

Merged
nicholasbishop merged 3 commits intomainfrom
uefi-serial-improve-read-write
Mar 21, 2026
Merged

uefi: serial: add read_exact() and write_exact() + fix core::fmt::Write for Serial#1875
nicholasbishop merged 3 commits intomainfrom
uefi-serial-improve-read-write

Conversation

@phip1611
Copy link
Copy Markdown
Member

@phip1611 phip1611 commented Jan 25, 2026

Serial Protocol Improvements

  • Added Serial::read_exact() and Serial::write_exact() methods, which block until the entire buffer is read or written, automatically retrying on timeouts and stalling based on baud rate to avoid busy-waiting. These methods help prevent partial I/O and improve reliability when communicating over serial devices.
  • Fixed the fmt::Write implementation for Serial to use write_exact()

Timing and Reliability Enhancements

  • Introduced helper functions (duration_per_byte_estimate and duration_fifo_estimate) to conservatively estimate the time needed for serial operations, taking into account baud rate, data bits, parity, and stop bits. These are used to determine appropriate stalling durations between retries.

Testing and Documentation

  • Added unit tests for the timing estimation logic to ensure correct behavior under various serial configurations.
  • I tested this on real hardware - multiple times

@phip1611 phip1611 self-assigned this Jan 25, 2026
@phip1611 phip1611 force-pushed the uefi-serial-improve-read-write branch from fae721f to da6a8ed Compare January 25, 2026 09:35
@phip1611 phip1611 force-pushed the uefi-serial-improve-read-write branch from da6a8ed to 84787aa Compare January 26, 2026 12:32
@phip1611 phip1611 force-pushed the uefi-serial-improve-read-write branch 3 times, most recently from 465e4d3 to df9a189 Compare February 1, 2026 12:09
Copy link
Copy Markdown
Member

@nicholasbishop nicholasbishop left a comment

Choose a reason for hiding this comment

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

Initial comments in. For ease of review, I'd like to move the read/write changes to a separate PR, those seem independent of the new methods being added.

Comment thread uefi-test-runner/src/main.rs Outdated
Comment thread uefi/src/proto/console/serial.rs Outdated
Comment thread uefi/src/proto/console/serial.rs
Comment thread uefi/src/proto/console/serial.rs
Comment thread uefi/src/proto/console/serial.rs
@phip1611 phip1611 marked this pull request as draft February 22, 2026 11:20
@phip1611 phip1611 force-pushed the uefi-serial-improve-read-write branch from df9a189 to c12f302 Compare February 22, 2026 11:24
@phip1611 phip1611 force-pushed the uefi-serial-improve-read-write branch 2 times, most recently from 2b1e723 to 655f2f7 Compare March 19, 2026 13:47
@phip1611 phip1611 changed the title uefi: serial: improve read() and write(), add read_exact() and write_exact(), add read_to_end() uefi: serial: add read_exact() and write_exact() + fix core::fmt::Write for Serial Mar 19, 2026
@phip1611 phip1611 force-pushed the uefi-serial-improve-read-write branch from 655f2f7 to 625238c Compare March 19, 2026 13:50
@phip1611
Copy link
Copy Markdown
Member Author

phip1611 commented Mar 19, 2026

thanks for your helpful remarks and ideas! The new state is much better: no infinite loops, cleaner interfaces. I'll do one more test on real hardware, then this is good to go

I tested this on real hardware - multiple times. Hope we are good to go now - looking forward to your feedback!

Knowing how long a single byte takes to transmit at the current baud
rate lets retry logic stall for just the right amount of time - long
enough for the FIFO to drain or fill, short enough to stay responsive.
Required groundwork for the exact-read/write helpers that follow.
@phip1611 phip1611 force-pushed the uefi-serial-improve-read-write branch from 625238c to 65c57d7 Compare March 21, 2026 14:23
@phip1611 phip1611 marked this pull request as ready for review March 21, 2026 14:24
Callers that simply want to send or receive a fixed number of bytes
should not have to implement their own retry loop. These helpers absorb
the TIMEOUT-and-retry pattern internally, keeping call sites clean.

Endless-loop protection ensures a hardware fault cannot stall the system
forever. Validated on real hardware with reading/writing 16 KiB chunks.
@phip1611 phip1611 force-pushed the uefi-serial-improve-read-write branch from 65c57d7 to 27248a8 Compare March 21, 2026 14:25
On real hardware, Status::TIMEOUT is common even during normal
operation, not just on failure. The previous implementation treated it
as a hard error, silently dropping bytes. Retrying until all bytes are
sent makes the fmt::Write impl reliable and match the expectations of
its callers.

This uses an the same endless loop protection mentioned earlier.
@phip1611 phip1611 force-pushed the uefi-serial-improve-read-write branch from 27248a8 to 62f4691 Compare March 21, 2026 14:28
@nicholasbishop nicholasbishop added this pull request to the merge queue Mar 21, 2026
Merged via the queue into main with commit 3210a82 Mar 21, 2026
20 checks passed
@nicholasbishop nicholasbishop deleted the uefi-serial-improve-read-write branch March 21, 2026 22:16
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.

2 participants