|
1 | 1 | use std::{ |
2 | | - io::Read, |
3 | 2 | net::TcpStream, |
4 | | - process::{Child, Command, Stdio}, |
| 3 | + process::{Child, Command}, |
5 | 4 | time::Duration, |
6 | 5 | }; |
7 | 6 |
|
| 7 | +use log::{debug, info}; |
8 | 8 | use uboot_shell::UbootShell; |
9 | 9 |
|
10 | 10 | fn main() { |
| 11 | + env_logger::init(); |
| 12 | + |
11 | 13 | let (mut out, mut uboot) = new_uboot(); |
12 | 14 |
|
13 | | - uboot.loady(0x40200000, "Cargo.toml", |_r, _a| {}).unwrap(); |
| 15 | + uboot |
| 16 | + .loady(0x40200000, "Cargo.toml", |r, a| { |
| 17 | + debug!("{r}/{a}"); |
| 18 | + }) |
| 19 | + .unwrap(); |
14 | 20 |
|
15 | | - println!("finish"); |
| 21 | + info!("finish"); |
16 | 22 | let _ = out.kill(); |
17 | 23 | let _ = out.wait(); |
18 | 24 | } |
19 | 25 |
|
20 | 26 | fn new_uboot() -> (Child, UbootShell) { |
21 | | - // qemu-system-aarch64 -machine virt -cpu cortex-a57 -nographic -bios assets/u-boot.bin |
22 | | - let mut out = Command::new("qemu-system-aarch64") |
| 27 | + // qemu-system-aarch64 -machine virt -cpu cortex-a57 -nographic -bios assets/u-boot.bin -serial tcp::12345,server |
| 28 | + let out = Command::new("qemu-system-aarch64") |
23 | 29 | .args([ |
24 | 30 | "-machine", |
25 | 31 | "virt", |
26 | 32 | "-cpu", |
27 | 33 | "cortex-a57", |
28 | 34 | "-nographic", |
29 | 35 | "-serial", |
30 | | - "tcp::12345,server,nowait", |
| 36 | + "tcp::12345,server", |
31 | 37 | "-bios", |
32 | 38 | "assets/u-boot.bin", |
33 | 39 | ]) |
34 | | - .stdout(Stdio::piped()) |
35 | 40 | .spawn() |
36 | 41 | .unwrap(); |
37 | 42 |
|
38 | | - let stdout = out.stdout.take().unwrap(); |
39 | | - let mut buff = vec![]; |
40 | | - for i in stdout.bytes() { |
41 | | - buff.push(i.unwrap()); |
42 | | - if String::from_utf8_lossy(&buff).contains("qemu") { |
43 | | - break; |
| 43 | + let tx; |
| 44 | + |
| 45 | + loop { |
| 46 | + std::thread::sleep(Duration::from_millis(100)); |
| 47 | + match TcpStream::connect("127.0.0.1:12345") { |
| 48 | + Ok(s) => { |
| 49 | + tx = s; |
| 50 | + break; |
| 51 | + } |
| 52 | + Err(e) => { |
| 53 | + println!("wait for qemu serial port ready: {e}"); |
| 54 | + } |
44 | 55 | } |
45 | 56 | } |
46 | 57 |
|
47 | | - let tx = TcpStream::connect("127.0.0.1:12345").unwrap(); |
48 | | - |
49 | 58 | let rx = tx.try_clone().unwrap(); |
50 | | - rx.set_read_timeout(Some(Duration::from_secs(1))).unwrap(); |
51 | | - |
| 59 | + rx.set_read_timeout(Some(Duration::from_millis(300))) |
| 60 | + .unwrap(); |
| 61 | + println!("connect ok"); |
52 | 62 | (out, UbootShell::new(tx, rx).unwrap()) |
53 | 63 | } |
0 commit comments