-
-
Notifications
You must be signed in to change notification settings - Fork 60
Flash operations support #172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
be29d95
feat(flash_commands): provide flash operations for the target
eulerdisk f89c6a2
feat(flash_commands): update README
eulerdisk 21acf1d
feat(flash_commands): updated example armv4t
eulerdisk 338d1b2
feat(flash_commands): fix clippy issues
eulerdisk fbdefa0
feat(flash_commands): improved docs and armv4t logging for flash comm…
eulerdisk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| use crate::emu::Emu; | ||
| use gdbstub::target; | ||
| use gdbstub::target::TargetResult; | ||
| use log::info; | ||
|
|
||
| impl target::ext::flash::Flash for Emu { | ||
| fn flash_erase(&mut self, start_addr: u32, length: u32) -> TargetResult<(), Self> { | ||
| info!("flash_erase start_addr: {start_addr:08x}, length: {length:08x}"); | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn flash_write(&mut self, start_addr: u32, _data: &[u8]) -> TargetResult<(), Self> { | ||
| info!("flash_write start_addr: {start_addr:08x}"); | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn flash_done(&mut self) -> TargetResult<(), Self> { | ||
| info!("flash_done"); | ||
| Ok(()) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| use super::prelude::*; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct vFlashDone; | ||
|
|
||
| impl<'a> ParseCommand<'a> for vFlashDone { | ||
| #[inline(always)] | ||
| fn from_packet(_buf: PacketBuf<'a>) -> Option<Self> { | ||
| Some(vFlashDone) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| use super::prelude::*; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct vFlashErase<'a> { | ||
| pub addr: &'a [u8], | ||
| pub length: &'a [u8], | ||
| } | ||
|
|
||
| impl<'a> ParseCommand<'a> for vFlashErase<'a> { | ||
| #[inline(always)] | ||
| fn from_packet(buf: PacketBuf<'a>) -> Option<Self> { | ||
| let body = buf.into_body(); | ||
|
|
||
| let mut body = body.splitn_mut(3, |&b| b == b',' || b == b':'); | ||
| let _first_colon = body.next()?; | ||
| let addr = decode_hex_buf(body.next()?).ok()?; | ||
| let length = decode_hex_buf(body.next()?) | ||
| .ok() | ||
| .filter(|l| !l.is_empty())?; | ||
| Some(Self { addr, length }) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| macro_rules! test_buf { | ||
| ($bufname:ident, $body:literal) => { | ||
| let mut test = $body.to_vec(); | ||
| let mut buf = PacketBuf::new_with_raw_body(&mut test).unwrap(); | ||
| if !buf.strip_prefix(b"vFlashErase") { | ||
| panic!("invalid test"); | ||
| } | ||
| let $bufname = buf; | ||
| }; | ||
| } | ||
|
|
||
| #[test] | ||
| fn valid_vFlashErase() { | ||
| test_buf!(buf, b"vFlashErase:08000000,00004000"); | ||
|
|
||
| let pkt = vFlashErase::from_packet(buf).unwrap(); | ||
|
|
||
| assert_eq!(pkt.addr, [0x08, 0, 0, 0]); | ||
| assert_eq!(pkt.length, [0, 0, 0x40, 0]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_vFlashErase_wrong_address() { | ||
| test_buf!(buf, b"vFlashErase:abcdefg:00004000"); | ||
|
|
||
| assert!(vFlashErase::from_packet(buf).is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_vFlashErase_wrong_length() { | ||
| test_buf!(buf, b"vFlashErase:08000000:abcdefg"); | ||
|
|
||
| assert!(vFlashErase::from_packet(buf).is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_vFlashErase_missing_address() { | ||
| test_buf!(buf, b"vFlashErase:"); | ||
|
|
||
| assert!(vFlashErase::from_packet(buf).is_none()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_vFlashErase_missing_length() { | ||
| test_buf!(buf, b"vFlashErase:08000000:"); | ||
|
|
||
| assert!(vFlashErase::from_packet(buf).is_none()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| use super::prelude::*; | ||
| use crate::protocol::common::hex::decode_bin_buf; | ||
|
|
||
| #[derive(Debug)] | ||
| pub struct vFlashWrite<'a> { | ||
| pub addr: &'a [u8], | ||
| pub val: &'a [u8], | ||
| } | ||
|
|
||
| impl<'a> ParseCommand<'a> for vFlashWrite<'a> { | ||
| #[inline(always)] | ||
| fn from_packet(buf: PacketBuf<'a>) -> Option<Self> { | ||
| let body = buf.into_body(); | ||
|
|
||
| let mut body = body.splitn_mut(3, |&b| b == b':'); | ||
| let _first_colon = body.next()?; | ||
| let addr = decode_hex_buf(body.next()?) | ||
| .ok() | ||
| .filter(|a| !a.is_empty())?; | ||
| let val = decode_bin_buf(body.next()?)?; | ||
|
|
||
| Some(vFlashWrite { addr, val }) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| macro_rules! test_buf { | ||
| ($bufname:ident, $body:literal) => { | ||
| let mut test = $body.to_vec(); | ||
| let mut buf = PacketBuf::new_with_raw_body(&mut test).unwrap(); | ||
| if !buf.strip_prefix(b"vFlashWrite") { | ||
| panic!("invalid test"); | ||
| } | ||
| let $bufname = buf; | ||
| }; | ||
| } | ||
|
|
||
| #[test] | ||
| fn valid_vFlashWrite() { | ||
| test_buf!( | ||
| buf, | ||
| b"vFlashWrite:08000000:\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A" | ||
| ); | ||
|
|
||
| let pkt = vFlashWrite::from_packet(buf).unwrap(); | ||
|
|
||
| assert_eq!(pkt.addr, [0x08, 0, 0, 0]); | ||
| assert_eq!(pkt.val, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_vFlashWrite_wrong_address() { | ||
| test_buf!( | ||
| buf, | ||
| b"vFlashWrite:abcdefg:\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A" | ||
| ); | ||
|
|
||
| assert!(vFlashWrite::from_packet(buf).is_none()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_vFlashWrite_missing_data() { | ||
| test_buf!(buf, b"vFlashWrite:abcdefg:"); | ||
|
|
||
| assert!(vFlashWrite::from_packet(buf).is_none()) | ||
| } | ||
|
|
||
| #[test] | ||
| fn invalid_vFlashWrite_missing_address() { | ||
| test_buf!(buf, b"vFlashWrite:"); | ||
|
|
||
| assert!(vFlashWrite::from_packet(buf).is_none()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| use super::prelude::*; | ||
| use crate::arch::Arch; | ||
| use crate::protocol::commands::ext::FlashOperations; | ||
|
|
||
| impl<T: Target, C: Connection> GdbStubImpl<T, C> { | ||
| pub(crate) fn handle_flash_operations( | ||
| &mut self, | ||
| _res: &mut ResponseWriter<'_, C>, | ||
| target: &mut T, | ||
| command: FlashOperations<'_>, | ||
| ) -> Result<HandlerStatus, Error<T::Error, C::Error>> { | ||
| let ops = match target.support_flash_operations() { | ||
| Some(ops) => ops, | ||
| None => return Ok(HandlerStatus::Handled), | ||
| }; | ||
| let handler_status = match command { | ||
| FlashOperations::vFlashErase(cmd) => { | ||
| let addr = <T::Arch as Arch>::Usize::from_be_bytes(cmd.addr) | ||
| .ok_or(Error::TargetMismatch)?; | ||
|
|
||
| let length = <T::Arch as Arch>::Usize::from_be_bytes(cmd.length) | ||
| .ok_or(Error::TargetMismatch)?; | ||
|
|
||
| ops.flash_erase(addr, length).handle_error()?; | ||
| HandlerStatus::NeedsOk | ||
| } | ||
| FlashOperations::vFlashWrite(cmd) => { | ||
| let addr = <T::Arch as Arch>::Usize::from_be_bytes(cmd.addr) | ||
| .ok_or(Error::TargetMismatch)?; | ||
|
|
||
| ops.flash_write(addr, cmd.val).handle_error()?; | ||
| HandlerStatus::NeedsOk | ||
| } | ||
| FlashOperations::vFlashDone(_) => { | ||
| ops.flash_done().handle_error()?; | ||
| HandlerStatus::NeedsOk | ||
| } | ||
| }; | ||
|
|
||
| Ok(handler_status) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| //! Provide flash operations on the target. | ||
| use crate::arch::Arch; | ||
| use crate::target::Target; | ||
| use crate::target::TargetResult; | ||
|
|
||
| /// Flash memory operations. | ||
| /// It's necessary to implement this extension to support GDB `load` command. | ||
| /// | ||
| /// Typically, a GDB `load` command sequence starts by issuing a `flash_erase` | ||
| /// command, followed by multiple `flash_write` commands (typically one for each | ||
| /// loadable ELF section), and ends with a `flash_done` command. | ||
| /// | ||
| /// The regions containing the addresses to be flashed must be specified as | ||
| /// "flash" regions in the memory map xml, returned by | ||
| /// [MemoryMap::memory_map_xml][crate::target::ext::memory_map::MemoryMap::memory_map_xml]. | ||
| pub trait Flash: Target { | ||
| /// Erase `length` bytes of the target's flash memory starting from | ||
| /// `start_addr`. | ||
| /// | ||
| /// GDB ensures `start_addr` and `length` are aligned to flash memory | ||
| /// block boundaries as defined by the memory map xml. | ||
| fn flash_erase( | ||
| &mut self, | ||
| start_addr: <Self::Arch as Arch>::Usize, | ||
| length: <Self::Arch as Arch>::Usize, | ||
| ) -> TargetResult<(), Self>; | ||
|
|
||
| /// Write bytes to the target's flash memory. | ||
| /// | ||
| /// GDB guarantees that the memory ranges specified by `flash_write` | ||
| /// commands sent before a `flash_done` do not overlap and appear in | ||
| /// order of increasing addresses. | ||
| /// | ||
| /// See [GDB Documentation] for more details. | ||
| /// | ||
| /// [GDB Documentation]: https://sourceware.org/gdb/current/onlinedocs/gdb.html/Packets.html | ||
| fn flash_write( | ||
| &mut self, | ||
| start_addr: <Self::Arch as Arch>::Usize, | ||
| data: &[u8], | ||
| ) -> TargetResult<(), Self>; | ||
|
|
||
| /// Indicate to the target that flash programming is finished. | ||
| /// | ||
| /// By GDB documentation, you can batch flash erase and write operations | ||
| /// until this is called. | ||
| fn flash_done(&mut self) -> TargetResult<(), Self>; | ||
| } | ||
|
|
||
| define_ext!(FlashOps, Flash); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.