Skip to content
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

Add bootstrapping support #30

Merged
merged 8 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

members = [
"orca",
"serial",
"rootshell",
"wavehunter",
]
resolver = "2"
8 changes: 8 additions & 0 deletions make.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
cargo build
# Force a switch into the debug mode to enable ADB
target/x86_64-unknown-linux-gnu/debug/serial AT
adb push target/armv7-unknown-linux-gnueabihf/debug/rootshell /tmp/
target/x86_64-unknown-linux-gnu/debug/serial "AT+SYSCMD=mv /tmp/rootshell /bin/rootshell"
sleep 1
target/x86_64-unknown-linux-gnu/debug/serial "AT+SYSCMD=chown root /bin/rootshell"
sleep 1
target/x86_64-unknown-linux-gnu/debug/serial "AT+SYSCMD=chmod 4755 /bin/rootshell"
adb push target/armv7-unknown-linux-gnueabihf/debug/wavehunter /data/wavehunter/wavehunter
adb push target/armv7-unknown-linux-gnueabihf/debug/wavehunter-reader /data/wavehunter/wavehunter-reader
8 changes: 8 additions & 0 deletions rootshell/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "rootshell"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
15 changes: 15 additions & 0 deletions rootshell/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::process::Command;
use std::os::unix::process::CommandExt;
use std::env;

fn main() {
let mut args = env::args();

// discard argv[0]
let _ = args.next();
Command::new("/bin/bash")
.args(args)
.uid(0)
.gid(0)
.exec();
}
10 changes: 10 additions & 0 deletions serial/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "serial"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rusb = "0.9.3"

134 changes: 134 additions & 0 deletions serial/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
use std::str;
use std::thread::sleep;
use std::time::Duration;

use rusb::{
Context, DeviceHandle, UsbContext,
};

fn main() {
let args: Vec<String> = std::env::args().collect();

if args.len() < 2 {
println!("usage: {0} <command>", args[0]);
return;
}

match Context::new() {
Ok(mut context) => match open_orbic(&mut context) {
Some(mut handle) => {
send_command(&mut handle, &args[1])
},
None => panic!("No Orbic device found"),
},
Err(e) => panic!("Failed to initialize libusb: {0}", e),
}
}

fn send_command<T: UsbContext>(
handle: &mut DeviceHandle<T>,
command: &String,
) {
let mut data = String::new();
data.push_str("\r\n");
data.push_str(command);
data.push_str("\r\n");

let timeout = Duration::from_secs(1);
let mut response = [0; 256];
match handle.write_control(0x21, 0x22, 3, 1, &[], timeout) {
Ok(_) => match handle.write_bulk(0x2, data.as_bytes(), timeout) {
// Consume the echoed back command
Ok(_) => match handle.read_bulk(0x82, &mut response, timeout) {
// Read the actual response
Ok(_) => match handle.read_bulk(0x82, &mut response, timeout) {
Ok(_) => {
let responsestr = str::from_utf8(&response).unwrap();
if !responsestr.starts_with("\r\nOK\r\n") {
println!("Received unexpected response{0}", responsestr);
}
},
Err(e) => panic!("Failed to read response: {0}", e),
},
Err(e) => panic!("Failed to read submitted command: {0}", e),
}
Err(e) => panic!("Failed to write command: {0}", e),
},
Err(e) => panic!("Failed to send control request: {0}", e),
}
}

fn switch_device<T: UsbContext>(
handle: &mut DeviceHandle<T>,
) {
// Send a command to switch the device into generic mode, exposing serial
let timeout = Duration::from_secs(1);
match handle.write_control(0x40, 0xa0, 0, 0, &[], timeout) {
Ok(_) => (),
Err(e) => {
// If the device reboots while the command is still executing we
// may get a pipe error here
if e == rusb::Error::Pipe {
return;
}
panic!("Failed to send device switch control request: {0}", e);
},
}
}

fn open_orbic<T: UsbContext>(
context: &mut T,
) -> Option<DeviceHandle<T>> {
// Device after initial mode switch
match open_device(context, 0x05c6, 0xf601) {
Some(handle) => return Some(handle),
None => (),
}
mjg59 marked this conversation as resolved.
Show resolved Hide resolved
// Device with rndis enabled as well
match open_device(context, 0x05c6, 0xf622) {
Some(handle) => return Some(handle),
None => (),
}

// Device in out-of-the-box state, need to switch to diag mode
match open_device(context, 0x05c6, 0xf626) {
Some(mut handle) => switch_device(&mut handle),
None => panic!("No Orbic device detected")
}

for _ in 1..10 {
match open_device(context, 0x05c6, 0xf601) {
Some(handle) => return Some(handle),
None => (),
}
sleep(Duration::from_secs(10))
}
panic!("No Orbic device detected")
}

fn open_device<T: UsbContext>(
context: &mut T,
vid: u16,
pid: u16,
) -> Option<DeviceHandle<T>> {
let devices = match context.devices() {
Ok(d) => d,
Err(_) => return None,
};

for device in devices.iter() {
let device_desc = match device.device_descriptor() {
Ok(d) => d,
Err(_) => continue,
};

if device_desc.vendor_id() == vid && device_desc.product_id() == pid {
match device.open() {
Ok(handle) => return Some(handle),
Err(e) => panic!("device found but failed to open: {}", e),
}
}
}
mjg59 marked this conversation as resolved.
Show resolved Hide resolved

None
}