Skip to content

Commit 3da3181

Browse files
committed
feat(test_runner): jump directly to the entry point
1 parent 3811aeb commit 3da3181

File tree

1 file changed

+24
-15
lines changed
  • src/constance_test_runner/src/targets

1 file changed

+24
-15
lines changed

src/constance_test_runner/src/targets/jlink.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,20 +47,20 @@ impl DebugProbe for Fe310JLinkDebugProbe {
4747
let exe = exe.to_owned();
4848
Box::pin(async move {
4949
// Extract loadable sections
50-
let sections = read_loaded_region_from_elf(&exe)
50+
let LoadableCode { regions, entry } = read_elf(&exe)
5151
.await
5252
.map_err(Fe310JLinkDebugProbeGetOutputError::ProcessElf)?;
5353

54-
// Extract loadable sections to separate binary files
54+
// Extract loadable regions to separate binary files
5555
let tempdir = TempDir::new("constance_test_runner")
5656
.map_err(Fe310JLinkDebugProbeGetOutputError::CreateTempDir)?;
57-
let section_files: Vec<_> = (0..sections.len())
57+
let section_files: Vec<_> = (0..regions.len())
5858
.map(|i| {
5959
let name = format!("{}.bin", i);
6060
tempdir.path().join(&name)
6161
})
6262
.collect();
63-
for (path, (data, _)) in section_files.iter().zip(sections.iter()) {
63+
for (path, (data, _)) in section_files.iter().zip(regions.iter()) {
6464
log::debug!("Writing {} byte(s) to '{}'", data.len(), path.display());
6565
tokio::fs::write(&path, data)
6666
.await
@@ -69,10 +69,12 @@ impl DebugProbe for Fe310JLinkDebugProbe {
6969

7070
// Generate commands for `JLinkExe`
7171
let mut cmd = String::new();
72-
for (path, (_, offset)) in section_files.iter().zip(sections.iter()) {
72+
writeln!(cmd, "r").unwrap();
73+
for (path, (_, offset)) in section_files.iter().zip(regions.iter()) {
7374
writeln!(cmd, "loadbin \"{}\" 0x{:08x}", path.display(), offset).unwrap();
7475
}
75-
writeln!(cmd, "rnh").unwrap();
76+
writeln!(cmd, "setpc 0x{:x}", entry).unwrap();
77+
writeln!(cmd, "g").unwrap();
7678
writeln!(cmd, "q").unwrap();
7779

7880
// Flash the program and reset the chip
@@ -99,14 +101,11 @@ impl DebugProbe for Fe310JLinkDebugProbe {
99101
.await
100102
.map_err(Fe310JLinkDebugProbeGetOutputError::Flash)?;
101103

102-
log::debug!("Waiting for 2 seconds");
103-
104-
// Red-V's bootloader doesn't start the user program right away
105-
// so wait for some time. The stale RTT data from a previous run
106-
// might still be there until the new startup code zero-fills the
107-
// memory.
108-
tokio::time::delay_for(std::time::Duration::from_secs(2)).await;
104+
log::debug!("Waiting for 1 seconds");
109105

106+
// The stale RTT data from a previous run might still be there until
107+
// the new startup code zero-fills the memory.
108+
tokio::time::delay_for(std::time::Duration::from_secs(1)).await;
110109
log::debug!("Opening the debug probe using `probe-rs`");
111110

112111
// Open the probe using `probe-rs`
@@ -150,8 +149,15 @@ enum ProcessElfError {
150149
Parse(#[source] goblin::error::Error),
151150
}
152151

152+
struct LoadableCode {
153+
/// The regions to be loaded onto the target.
154+
regions: Vec<(Vec<u8>, u64)>,
155+
/// The entry point.
156+
entry: u64,
157+
}
158+
153159
/// Read the specified ELF file and return regions to be loaded onto the target.
154-
async fn read_loaded_region_from_elf(exe: &Path) -> Result<Vec<(Vec<u8>, u64)>, ProcessElfError> {
160+
async fn read_elf(exe: &Path) -> Result<LoadableCode, ProcessElfError> {
155161
let elf_bytes = tokio::fs::read(&exe).await.map_err(ProcessElfError::Read)?;
156162
let elf = goblin::elf::Elf::parse(&elf_bytes).map_err(ProcessElfError::Parse)?;
157163

@@ -170,7 +176,10 @@ async fn read_loaded_region_from_elf(exe: &Path) -> Result<Vec<(Vec<u8>, u64)>,
170176
})
171177
.collect();
172178

173-
Ok(regions)
179+
Ok(LoadableCode {
180+
regions,
181+
entry: elf.entry,
182+
})
174183
}
175184

176185
struct OutputReader {

0 commit comments

Comments
 (0)