diff --git a/Cargo.lock b/Cargo.lock index 9ca5d5c2a..cfabdae16 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -102,6 +102,27 @@ version = "1.0.58" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bb07d2053ccdbe10e2af2995a2f116c1330396493dc1269f6a91d0ae82e19704" +[[package]] +name = "app-loader" +version = "0.1.0" +dependencies = [ + "gam", + "graphics-server", + "locales", + "log", + "modals", + "num-derive", + "num-traits", + "rkyv", + "ureq", + "url", + "xous 0.9.53 (registry+https://github.com/rust-lang/crates.io-index)", + "xous-api-log", + "xous-api-names", + "xous-api-ticktimer", + "xous-ipc 0.9.53 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "argh" version = "0.1.8" @@ -1836,11 +1857,10 @@ checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] name = "form_urlencoded" -version = "1.0.1" +version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191" +checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652" dependencies = [ - "matches", "percent-encoding", ] @@ -2312,11 +2332,10 @@ checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" [[package]] name = "idna" -version = "0.2.3" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "418a0a6fab821475f634efe3ccc45c013f742efe03d853e8d3355d5cb850ecf8" +checksum = "7d20d6b07bfbc108882d88ed8e37d39636dcc260e15e30c45e6ba089610b917c" dependencies = [ - "matches", "unicode-bidi", "unicode-normalization", ] @@ -2697,12 +2716,6 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ca88d725a0a943b096803bd34e73a4437208b6077654cc4ecb2947a5f91618d" -[[package]] -name = "matches" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3e378b66a060d48947b590737b30a1be76706c8dd7b8ba0f2fe3989c68a853f" - [[package]] name = "memchr" version = "2.5.0" @@ -3321,9 +3334,9 @@ dependencies = [ [[package]] name = "percent-encoding" -version = "2.2.0" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "478c572c3d73181ff3c2539045f6eb99e5491218eae919370993b890cdbdd98e" +checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" [[package]] name = "perflib" @@ -4487,6 +4500,15 @@ dependencies = [ "no-std-net", ] +[[package]] +name = "spawn" +version = "0.1.0" +dependencies = [ + "log", + "xous 0.9.53 (registry+https://github.com/rust-lang/crates.io-index)", + "xous-api-log", +] + [[package]] name = "spin" version = "0.5.2" @@ -5059,9 +5081,9 @@ dependencies = [ [[package]] name = "unicode-bidi" -version = "0.3.8" +version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "099b7128301d285f79ddd55b9a83d5e6b9e97c92e0ea0daebee7263e932de992" +checksum = "92888ba5573ff080736b3648696b70cafad7d250551175acbaa4e0385b3e1460" [[package]] name = "unicode-ident" @@ -5071,9 +5093,9 @@ checksum = "d22af068fba1eb5edcb4aea19d382b2a3deb4c8f9d475c589b6ada9e0fd493ee" [[package]] name = "unicode-normalization" -version = "0.1.21" +version = "0.1.22" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "854cbdc4f7bc6ae19c820d44abdc3277ac3e1b2b93db20a636825d9322fb60e6" +checksum = "5c5713f0fc4b5db668a2ac63cdb7bb4469d8c9fed047b1d0292cc7b0ce2ba921" dependencies = [ "tinyvec", ] @@ -5132,13 +5154,12 @@ dependencies = [ [[package]] name = "url" -version = "2.2.2" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a507c383b2d33b5fc35d1861e77e6b383d158b2da5e14fe51b83dfedf6fd578c" +checksum = "143b538f18257fac9cad154828a57c6bf5157e1aa604d4816b5995bf6de87ae5" dependencies = [ "form_urlencoded", "idna", - "matches", "percent-encoding", ] @@ -5974,8 +5995,6 @@ dependencies = [ [[package]] name = "xous-api-log" version = "0.1.49" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45a48cf4707962cd5e0cf5a471918da348dd586c50018d3d2cba45e1471a720f" dependencies = [ "log", "num-derive", diff --git a/Cargo.toml b/Cargo.toml index 85906d56c..10f2c6b66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -72,6 +72,8 @@ members = [ "services/dns", "services/modals", "services/early_settings", + "apps/app-loader", + "apps/app-loader/spawn", "apps/ball", "apps/hello", "apps/mtxchat", diff --git a/api/xous-api-log/Cargo.toml b/api/xous-api-log/Cargo.toml index 638ffc2f0..a2f9742b8 100644 --- a/api/xous-api-log/Cargo.toml +++ b/api/xous-api-log/Cargo.toml @@ -19,4 +19,5 @@ num-traits = {version = "0.2.14", default-features = false} [features] debugprint = [] # adding this allocates the UART for debugging the logger logging = [] # adding this allocates the hardware UART for console interactions +nostd = [] # adding this causes the program to be built without std default = ["logging"] diff --git a/api/xous-api-log/src/lib.rs b/api/xous-api-log/src/lib.rs index 7748c54b0..282c5d0f6 100644 --- a/api/xous-api-log/src/lib.rs +++ b/api/xous-api-log/src/lib.rs @@ -1,4 +1,4 @@ -#![cfg_attr(target_os = "none", no_std)] +#![cfg_attr(any(target_os = "none", feature = "nostd"), no_std)] use core::fmt::Write; use core::sync::atomic::{AtomicU32, Ordering}; use num_traits::ToPrimitive; @@ -96,8 +96,10 @@ pub fn init() -> Result<(), LogError> { } pub fn init_wait() -> Result<(), ()> { + let sid = xous::SID::from_bytes(b"xous-log-server ").unwrap(); + let cid = xous::connect(sid).or(Err(()))?; XOUS_LOGGER_CONNECTION.store( - xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap()).or(Err(()))?, + cid, Ordering::Relaxed, ); log::set_logger(&XOUS_LOGGER).or(Err(()))?; diff --git a/apps/app-loader/Cargo.toml b/apps/app-loader/Cargo.toml new file mode 100644 index 000000000..3a787cdf0 --- /dev/null +++ b/apps/app-loader/Cargo.toml @@ -0,0 +1,27 @@ +[package] +name = "app-loader" +version = "0.1.0" +authors = ["Vihaan Jim "] +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +xous = "0.9.52" +xous-ipc = "0.9.52" +log-server = { package = "xous-api-log", version = "0.1.35" } +log = "0.4.14" +xous-names = { package = "xous-api-names", version = "0.9.37" } +gam = { path = "../../services/gam" } +graphics-server = { path = "../../services/graphics-server" } +modals = { path = "../../services/modals" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.44" } +locales = { path = "../../locales" } + +num-derive = {version = "0.3.3", default-features = false} +num-traits = {version = "0.2.14", default-features = false} + +rkyv = {version = "0.4.3", features = ["const_generics"], default-features = false} + +ureq = { version = "2.5.0", features = ["json"] } +url = "2.4" \ No newline at end of file diff --git a/apps/app-loader/README.md b/apps/app-loader/README.md new file mode 100644 index 000000000..0bb974ab4 --- /dev/null +++ b/apps/app-loader/README.md @@ -0,0 +1,47 @@ +# An App Loader for Xous + +This app allows you to load apps onto your device at runtime and is intended to be used by developers who would like to quickly test their apps on physical hardware. +At the moment the apps are loaded onto the device using a primitive HTTP server; however, in the future, other (faster) methods may be implemented. +Depending on how good the device's WiFi connection is, loading the app can take anywhere from 20 seconds for a small app to a couple of minutes for a large app. Additionally, if you are loading a particularly large app, your device's WiFi connection may cut out while you are trying to load it, leading to your device not being able to download the app (this is another reason why different methods of app loading may need to be implemented). +Note that at the moment you can only load apps once each time the device runs since process destruction has not been implemented in Xous yet. + +## Usage + +First, add the app loader to your device's image as you would with any other app: +``` +$ cargo xtask app-image-xip --app app-loader --feature unsafe-app-loading +$ sudo python3 tools/usb_update.py -k target/riscv32imac-unknown-xous-elf/release/xous.img -l target/riscv32imac-unknown-xous-elf/release/loader.bin +``` +Then, compile the apps you would like on your device and start the app server. For example, if you would like to serve the apps `hello`, `ball`, and `vault` on port 8000, run: +``` +$ cargo xtask compile-apps --app hello --app ball --app vault +$ python3 tools/app_server.py hello ball vault -p 8000 +``` +Now, after selecting `App Loader` from the app menu on the device, you should see a menu that looks like: +``` +/------------\ +| Set Server | +| Close | +\------------/ +``` +From here, you can select `Set Server` and then input the address of your server (e.g., `http://` followed by the ip address of your server, a `:`, and the port you are running it on). If you do not prefix the address with `http://`, the app will present an error and make you re-enter it. +Now, the screen should look like: +``` +/-----------------\ +| Add App | +| Reload App List | +| Set Server | +| Close | +\-----------------/ +``` +If you select `Add App`, this should bring up a submenu with a list of the apps served on your app server. Selecting one of these apps will load it onto the device. The app you load will now show up in the app loader menu. For example, if you loaded `Hello World`, the menu should look like: +``` +/-----------------\ +| Hello World | +| Add App | +| Reload App List | +| Set Server | +| Close | +\-----------------/ +``` +Clicking on `Hello World` (or the name of some other app you loaded) should now open the app. diff --git a/apps/app-loader/locales/i18n.json b/apps/app-loader/locales/i18n.json new file mode 100644 index 000000000..57801ed9d --- /dev/null +++ b/apps/app-loader/locales/i18n.json @@ -0,0 +1,50 @@ +{ + "apploader.close": { + "en": "Close", + "en-tts": "Close" + }, + "apploader.menu.setserver": { + "en": "Set Server", + "en-tts": "Set Server" + }, + "apploader.setserver.error": { + "en": "Invalid URL: ", + "en-tts": "Invalid URL: " + }, + "apploader.menu.reloadapplist": { + "en": "Reload App List", + "en-tts": "Reload App List" + }, + "apploader.reloadapplist.loading": { + "en": "Loading App List...", + "en-tts": "Loading App List" + }, + "apploader.reloadapplist.connection_error": { + "en": "Could not connect to server: ", + "en-tts": "Could not connect to server: " + }, + "apploader.reloadapplist.json_error": { + "en": "Could not convert to JSON: ", + "en-tts": "Could not convert to JSON: " + }, + "apploader.menu.addapp": { + "en": "Add App", + "en-tts": "Add App" + }, + "apploader.addapp.loading": { + "en": "Loading app...", + "en-tts": "Loading app" + }, + "apploader.addapp.error": { + "en": "Binary could not be loaded :(", + "en-tts": "Binary could not be loaded sad face" + }, + "apploader.addapp.content_length_error": { + "en": "No Content-Length header in server response", + "en-tts": "No Content-Length header in server response" + }, + "apploader.addapp.server_error": { + "en": "Could not connect to server: ", + "en-tts": "Could not connect to server: " + } +} diff --git a/apps/app-loader/spawn/Cargo.toml b/apps/app-loader/spawn/Cargo.toml new file mode 100644 index 000000000..1a13d63bf --- /dev/null +++ b/apps/app-loader/spawn/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "spawn" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +xous = "0.9.42" +log-server = { package = "xous-api-log", version = "0.1.35", features = ["nostd"] } +log = "0.4.17" diff --git a/apps/app-loader/spawn/README.md b/apps/app-loader/spawn/README.md new file mode 100644 index 000000000..807eef81d --- /dev/null +++ b/apps/app-loader/spawn/README.md @@ -0,0 +1,24 @@ +# Spawn a process & Read an ELF File + +This crate is based on the one in services/test_spawn. The main +difference is that this one has the ability to read, load, and +execute an ELF file through the opcode `LoadElf`. + +## API + +The following messages are supported: + +| Mnemonic | Opcode | Type | Description | +|--------------|--------|------|----------------------------------------------------------------------------------------------------------| +| LoadElf | 1 | M | Reads and loads an ELF file sent as a MemoryMessage. `Offset` is used to determine where the file starts | +| PingResponse | 2 | S | Returns the scalar sent except that arg1 += 1 | + +## A Note on Building + +In case you need to recompile this program for use in `app-loader`, +use the following commands: +``` +$ cargo build --package spawn --target riscv32imac-unknown-xous-elf --release +$ cargo run --package tools --bin copy-object target/riscv32imac-unknown-xous-elf/release/spawn +$ cp target/riscv32imac-unknown-xous-elf/release/spawn.bin apps/app-loader/src +``` diff --git a/apps/app-loader/spawn/build.rs b/apps/app-loader/spawn/build.rs new file mode 100644 index 000000000..2fbc2e034 --- /dev/null +++ b/apps/app-loader/spawn/build.rs @@ -0,0 +1,33 @@ +// NOTE: Adapted from cortex-m/build.rs +use std::env; +use std::fs; +use std::io::Write; +use std::path::PathBuf; + +fn main() { + let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap()); + let target = env::var("TARGET").unwrap(); + // let name = env::var("CARGO_PKG_NAME").unwrap(); + + if target.starts_with("riscv") { + // fs::copy( + // format!("bin/{}.a", target), + // out_dir.join(format!("lib{}.a", name)), + // ).unwrap(); + + // println!("cargo:rustc-link-lib=static={}", name); + // println!("cargo:rustc-link-search={}", out_dir.display()); + // println!("cargo:rerun-if-changed=bin/{}.a", target); + println!("cargo:rustc-link-arg=-Tlink.x"); + } + + // Put the linker script somewhere the linker can find it + fs::File::create(out_dir.join("link.x")) + .unwrap() + .write_all(include_bytes!("link.x")) + .unwrap(); + println!("cargo:rustc-link-search={}", out_dir.display()); + + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-changed=link.x"); +} diff --git a/apps/app-loader/spawn/link.x b/apps/app-loader/spawn/link.x new file mode 100644 index 000000000..c24dd7e3d --- /dev/null +++ b/apps/app-loader/spawn/link.x @@ -0,0 +1,190 @@ +MEMORY +{ + RAM : ORIGIN = 0x40000000, LENGTH = 16M + FLASH : ORIGIN = 0x20501000, LENGTH = 188k + FONTS : ORIGIN = 0x20530000, LENGTH = 4352k + MEMLCD: ORIGIN = 0xB0000000, LENGTH = 32k +} +/* +Fonts go from 0x2053_0000 to 0x2097_0000 +*/ + +REGION_ALIAS("REGION_TEXT", FLASH); +REGION_ALIAS("REGION_RODATA", FLASH); +REGION_ALIAS("REGION_DATA", RAM); +REGION_ALIAS("REGION_BSS", RAM); +REGION_ALIAS("REGION_STACK", RAM); +REGION_ALIAS("REGION_HEAP", RAM); + +_lcdfb = ORIGIN(MEMLCD); +/* Size of the main kernel stack */ +_stack_size = 16K; +_eheap = ORIGIN(RAM) + LENGTH(RAM); + + +PROVIDE(_stext = ORIGIN(REGION_TEXT)); +PROVIDE(_stack_start = ORIGIN(REGION_STACK) + LENGTH(REGION_STACK)); +PROVIDE(_max_hart_id = 0); +PROVIDE(_hart_stack_size = 2K); +PROVIDE(_heap_size = 0); + +PROVIDE(trap_handler = default_trap_handler); + +/* # Pre-initialization function */ +/* If the user overrides this using the `#[pre_init]` attribute or by creating a `__pre_init` function, + then the function this points to will be called before the RAM is initialized. */ +PROVIDE(__pre_init = default_pre_init); + +/* # Multi-processing hook function + fn _mp_hook() -> bool; + + This function is called from all the harts and must return true only for one hart, + which will perform memory initialization. For other harts it must return false + and implement wake-up in platform-dependent way (e.g. after waiting for a user interrupt). +*/ +PROVIDE(_mp_hook = default_mp_hook); + +ENTRY(init) + +SECTIONS +{ + .text.dummy (NOLOAD) : + { + /* This section is intended to make _stext address work */ + . = _stext; + } > REGION_TEXT + + .text _stext : + { + /* Put reset handler first in .text section so it ends up as the entry */ + /* point of the program. */ + KEEP(*(.text.init)); + KEEP(*(.init)); + KEEP(*(.init.rust)); + . = ALIGN(4); + KEEP(*(.trap)); + KEEP(*(.trap.rust)); + + *(.text .text.*); + } > REGION_TEXT + + .rodata : ALIGN(4) + { + *(.rodata .rodata.*); + + /* 4-byte align the end (VMA) of this section. + This is required by LLD to ensure the LMA of the following .data + section will have the correct alignment. */ + . = ALIGN(4); + _etext = .; + } > REGION_RODATA + + .data : ALIGN(4) + { + _sidata = LOADADDR(.data); + _sdata = .; + /* Must be called __global_pointer$ for linker relaxations to work. */ + PROVIDE(__global_pointer$ = . + 0x800); + *(.sdata .sdata.* .sdata2 .sdata2.*); + *(.data .data.*); + . = ALIGN(4); + _edata = .; + } > REGION_DATA AT > REGION_RODATA + + .fonts ALIGN(65536) : SUBALIGN(65536) + { + KEEP(*(.fontdata)); + } > FONTS + + .bss (NOLOAD) : + { + _sbss = .; + *(.sbss .sbss.* .bss .bss.*); + . = ALIGN(4); + _ebss = .; + } > REGION_BSS + + /* fictitious region that represents the memory available for the stack */ + .stack (NOLOAD) : + { + _sstack = .; + . += _stack_size; + . = ALIGN(4096); + _estack = .; + } > REGION_STACK + + /* fictitious region that represents the memory available for the heap */ + .heap (NOLOAD) : + { + . = ALIGN(4); + _sheap = .; + /* _eheap is defined elsewhere and is the remainder of RAM */ + } > REGION_HEAP + + /* fake output .got section */ + /* Dynamic relocations are unsupported. This section is only used to detect + relocatable code in the input files and raise an error if relocatable code + is found */ + .got (INFO) : + { + KEEP(*(.got .got.*)); + } + + /* Discard .eh_frame, we are not doing unwind on panic so it is not needed */ + /DISCARD/ : + { + *(.eh_frame); + *(.eh_frame_hdr); + } +} + +/* Do not exceed this mark in the error messages above | */ +ASSERT(ORIGIN(REGION_TEXT) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_TEXT must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_RODATA) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_RODATA must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_DATA) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_DATA must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_HEAP) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_HEAP must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_TEXT) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_TEXT must be 4-byte aligned"); + +ASSERT(ORIGIN(REGION_STACK) % 4 == 0, " +ERROR(riscv-rt): the start of the REGION_STACK must be 4-byte aligned"); + +ASSERT(_stext % 4 == 0, " +ERROR(riscv-rt): `_stext` must be 4-byte aligned"); + +ASSERT(_sdata % 4 == 0 && _edata % 4 == 0, " +BUG(riscv-rt): .data is not 4-byte aligned"); + +ASSERT(_sidata % 4 == 0, " +BUG(riscv-rt): the LMA of .data is not 4-byte aligned"); + +ASSERT(_sbss % 4 == 0 && _ebss % 4 == 0, " +BUG(riscv-rt): .bss is not 4-byte aligned"); + +ASSERT(_sheap % 4 == 0, " +BUG(riscv-rt): start of .heap is not 4-byte aligned"); + +ASSERT(_stext + SIZEOF(.text) < ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT), " +ERROR(riscv-rt): The .text section must be placed inside the REGION_TEXT region. +Set _stext to an address smaller than 'ORIGIN(REGION_TEXT) + LENGTH(REGION_TEXT)'"); + +ASSERT(SIZEOF(.stack) > (_max_hart_id + 1) * _hart_stack_size, " +ERROR(riscv-rt): .stack section is too small for allocating stacks for all the harts. +Consider changing `_max_hart_id` or `_hart_stack_size`."); + +ASSERT(SIZEOF(.got) == 0, " +.got section detected in the input files. Dynamic relocations are not +supported. If you are linking to C code compiled using the `gcc` crate +then modify your build script to compile the C code _without_ the +-fPIC flag. See the documentation of the `gcc::Config.fpic` method for +details."); + +/* Do not exceed this mark in the error messages above | */ diff --git a/apps/app-loader/spawn/src/main.rs b/apps/app-loader/spawn/src/main.rs new file mode 100644 index 000000000..699725170 --- /dev/null +++ b/apps/app-loader/spawn/src/main.rs @@ -0,0 +1,190 @@ +#![no_std] +#![no_main] + +enum StartupCommand { + Unhandled = 0, + LoadElf = 1, + PingResponse = 2, +} + +impl From for StartupCommand { + fn from(src: xous::MessageId) -> StartupCommand { + match src { + 1 => StartupCommand::LoadElf, + 2 => StartupCommand::PingResponse, + _ => StartupCommand::Unhandled, + } + } +} + +#[panic_handler] +fn handle_panic(arg: &core::panic::PanicInfo) -> ! { + log::info!("{arg}"); + xous::terminate_process(1); +} + +#[no_mangle] +pub extern "C" fn init(server1: u32, server2: u32, server3: u32, server4: u32) -> ! { + let server = xous::SID::from_u32(server1, server2, server3, server4); + + // recreate the extra sections that were cut out of the stub + { + let mut memory = xous::map_memory( + None, + core::num::NonZeroUsize::new(0x40000000), + 0x1000, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .unwrap(); + let connection = core::sync::atomic::AtomicU32::new(0); + let slice = unsafe { + core::slice::from_raw_parts( + &connection as *const _ as *const u8, + core::mem::size_of::(), + ) + }; + // safety: memory is freshly allocated by map_memory, should be aligned and init'd to 0 + unsafe { + for (dest, src) in memory.as_slice_mut::().iter_mut().skip(8).zip(slice) { + *dest = *src; + } + } + // everything should drop now + } + + log_server::init_wait().unwrap(); + log::set_max_level(log::LevelFilter::Info); + log::info!("my PID is {}", xous::process::id()); + loop { + if let Ok(xous::Result::MessageEnvelope(mut envelope)) = + xous::rsyscall(xous::SysCall::ReceiveMessage(server)) + { + match envelope.id().into() { + StartupCommand::LoadElf => { + let entry_point = read_elf(envelope.body.memory_message_mut()); + drop(envelope); // we have to get rid of all messages to destroy the server + // destroy the server + xous::destroy_server(server).expect("Couldn't destroy spawn server"); + jump(entry_point); + } + StartupCommand::PingResponse => ping_response(envelope), + _ => panic!("Unsupported"), + } + } + } +} + +fn ping_response(envelope: xous::MessageEnvelope) { + if let Some(msg) = envelope.body.scalar_message() { + if envelope.body.is_blocking() { + xous::syscall::return_scalar(envelope.sender, msg.arg1 + 1).unwrap(); + } + } +} + +fn read_elf(memory: Option<&mut xous::MemoryMessage>) -> usize { + let memory = match memory { + Some(s) => s, + None => panic!(), + }; + + // get the elf binary from the message + // safety: buf should be aligned and correctly sized inside the MemoryMessage + let mut bin = unsafe {memory.buf.as_slice::()}; + + // go to the beginning of the ELF file using the provided offset + bin = &bin[memory.offset.and_then(|n| Some(n.get())).unwrap_or(0)..]; + + let max = bin.len(); + + // make sure that this is a 32 bit + assert!(bin[4] == 0x01); + + // a helper function to get a region of the file as a usize + let to_usize = |start, size| { + if size == 1 { + return bin[start] as usize; + } + if size == 2 { + // assumes little endianness + return u16::from_le_bytes(bin[start..start + size].try_into().unwrap()) as usize; + } + if size == 4 { + return u32::from_le_bytes(bin[start..start + size].try_into().unwrap()) as usize; + } + panic!("Tried to get usize of invalid size!"); + }; + + // some basic stuff to know + let entry_point = to_usize(0x18, 4); + let ph_start = to_usize(0x1c, 4); + let ph_size = to_usize(0x2A, 2); + let ph_count = to_usize(0x2C, 2); + + // add the segments we should load + for i in 0..ph_count { + let start = ph_start + i * ph_size; + // only load PT_LOAD segments + if to_usize(start, 4) == 0x00000001 { + let src_addr = to_usize(start + 0x04, 4); + let vaddr = to_usize(start + 0x08, 4); + let padding = if vaddr & 0xFFF == 0 { 0 } else { vaddr & 0xFFF }; + let file_size = to_usize(start + 0x10, 4); + let mem_size = to_usize(start + 0x14, 4); + let mem_size = mem_size + padding; + let mem_size = mem_size + + if mem_size & 0xFFF == 0 { + 0 + } else { + 0x1000 - (mem_size & 0xFFF) + }; + + assert_eq!(0, mem_size & 0xFFF); + assert_eq!(0, (vaddr - padding) & 0xFFF); + + log::info!( + "Loading offset {} to virtual address {} with memory size {}", + src_addr, + vaddr, + mem_size + ); + let mut target_memory = xous::map_memory( + None, + core::num::NonZeroUsize::new(vaddr - padding), + mem_size, + xous::MemoryFlags::R | xous::MemoryFlags::W | xous::MemoryFlags::X, + ) + .unwrap(); + + // TODO: rationalize why this is safe + unsafe { + for (dest, src) in target_memory + .as_slice_mut() + .iter_mut() + .skip(padding) + .zip(bin[src_addr..core::cmp::min(max, src_addr + file_size)].iter()) + { + *dest = *src; + } + for dest in target_memory + .as_slice_mut() + .iter_mut() + .skip(padding + file_size) + .take(mem_size - file_size - padding) + { + *dest = 0; + } + } + } + } + + memory.offset = None; + log::info!("Finished writing"); + return entry_point; +} + +fn jump(entry_point: usize) -> ! { + log::info!("Jumping to {}", entry_point); + let entry_fn = unsafe { core::mem::transmute::<_, fn() -> !>(entry_point as *const u8) }; + entry_fn(); +} diff --git a/apps/app-loader/src/api.rs b/apps/app-loader/src/api.rs new file mode 100644 index 000000000..9a8a39101 --- /dev/null +++ b/apps/app-loader/src/api.rs @@ -0,0 +1,448 @@ +use crate::SERVER_NAME_APP_LOADER; +use gam::{ + menu_matic, Gam, MenuItem, MenuMatic, TextEntryPayload, UxRegistration, APP_MENU_0_APP_LOADER, + APP_MENU_1_APP_LOADER, APP_NAME_APP_LOADER, +}; +use locales::t; +use modals::Modals; +use num_traits::ToPrimitive; +use std::io::Read; + +pub(crate) struct AppLoader { + gam: Gam, + modals: Modals, + auth: [u32; 4], + ticktimer: ticktimer_server::Ticktimer, + menu: MenuMatic, + load_menu: MenuMatic, + conn: xous::CID, + apps: Vec>, + possible_apps: Vec<(xous_ipc::String<64>, usize)>, + server: Option, + current_menu: String, +} + +impl AppLoader { + pub(crate) fn new(xns: &xous_names::XousNames, sid: &xous::SID) -> AppLoader { + // gam + let gam = Gam::new(xns).expect("can't connect to GAM"); + + // the gam token + let auth = gam + .register_ux(UxRegistration { + app_name: xous_ipc::String::from_str(APP_NAME_APP_LOADER), + ux_type: gam::UxType::Modal, + predictor: None, + listener: sid.to_array(), + redraw_id: Opcode::Redraw.to_u32().unwrap(), + gotinput_id: None, + audioframe_id: None, + focuschange_id: None, + rawkeys_id: None, + }) + .expect("Couldn't register") + .expect("Didn't get an auth token"); + + let modals = Modals::new(xns).expect("Couldn't get modals"); + + // a connection to the server + let conn = xns + .request_connection_blocking(SERVER_NAME_APP_LOADER) + .expect("Couldn't connect to server"); + + // the ticktimer + let ticktimer = ticktimer_server::Ticktimer::new().expect("Couldn't connect to Ticktimer"); + + // the menu + let set_server_item = MenuItem { + name: xous_ipc::String::from_str(t!("apploader.menu.setserver", locales::LANG)), + action_conn: Some(conn), + action_opcode: Opcode::SetServer.to_u32().unwrap(), + action_payload: gam::MenuPayload::Scalar([0, 0, 0, 0]), + close_on_select: true, + }; + let close_item = MenuItem { + name: xous_ipc::String::from_str(t!("apploader.close", locales::LANG)), + action_conn: None, + action_opcode: 0, + action_payload: gam::MenuPayload::Scalar([0, 0, 0, 0]), + close_on_select: true, + }; + let menu = menu_matic( + vec![set_server_item, close_item.clone()], + APP_MENU_0_APP_LOADER, + Some(xous::create_server().unwrap()), + ) + .expect("Couldn't create menu"); + let load_menu = menu_matic( + vec![close_item.clone()], + APP_MENU_1_APP_LOADER, + Some(xous::create_server().unwrap()), + ) + .expect("Couldn't create menu"); + + AppLoader { + gam, + modals, + auth, + conn, + ticktimer, + menu, + load_menu, + apps: Vec::new(), + possible_apps: Vec::new(), + server: None, + current_menu: APP_MENU_0_APP_LOADER.to_string(), + } + } + + pub(crate) fn add_app(&mut self, index: usize) { + let (name, menus) = self.possible_apps[index]; + + self.modals + .start_progress(t!("apploader.addapp.loading", locales::LANG), 0, 3, 0) + .expect("Couldn't set up progress bar"); + + ////////////////////////// + // The downloading part // + ////////////////////////// + let response = match ureq::get(&format!( + "{}/{}", + self.server + .as_ref() + .expect("AddApp was somehow called without a server!"), + name + )) + .call() + { + Ok(response) => response, + Err(e) => { + self.modals + .show_notification( + &format!( + "{}{}", + t!("apploader.addapp.server_error", locales::LANG), + e + ), + None, + ) + .expect("Couldn't show modal"); + return; + } + }; + + let len = match response.header("Content-Length") { + Some(len) => len + .parse::() + .expect("Couldn't parse Content-Length header"), + None => { + self.modals + .show_notification( + t!("apploader.addapp.content_length_error", locales::LANG), + None, + ) + .expect("Couldn't show modal"); + return; + } + }; + + let mut memory = xous::map_memory( + None, + None, + len + if len & 0xFFF == 0 { + 0 + } else { + 0x1000 - (len & 0xFFF) + }, + xous::MemoryFlags::R | xous::MemoryFlags::W, + ) + .expect("Couldn't map memory"); + response + .into_reader() + .read_exact(&mut unsafe { memory.as_slice_mut() }[..len]) + .expect("Couldn't read"); + + self.modals + .update_progress(1) + .expect("Couldn't update progress"); + ////////////////////// + // The loading part // + ////////////////////// + + // create the spawn process + let stub = include_bytes!("spawn.bin"); + let args = xous::ProcessArgs::new( + stub, + xous::MemoryAddress::new(0x2050_1000).unwrap(), + xous::MemoryAddress::new(0x2050_1000).unwrap(), + ); + let spawn = xous::create_process(args).expect("Couldn't create spawn process"); + log::info!("Spawn PID: {}, Spawn CID: {}", spawn.pid, spawn.cid); + + // perform a ping to make sure that spawn is running + let result = + xous::send_message(spawn.cid, xous::Message::new_blocking_scalar(2, 1, 2, 3, 4)) + .unwrap(); + assert_eq!(xous::Result::Scalar1(2), result); + + self.modals + .update_progress(2) + .expect("Couldn't update progress"); + + // load the app from the binary file + let res = xous::send_message( + spawn.cid, + xous::Message::new_lend_mut(1, memory, None, None), + ) + .expect("Couldn't send a message to spawn"); + // we are just going to do some very basic error handling: if the "offset" is None, we are good, otherwise there was a problem + // TODO: make this better. Perhaps Buffer::from_raw_parts? + match res { + xous::Result::MemoryReturned(None, _) => self + .modals + .update_progress(3) + .expect("Couldn't update progress"), + _ => { + self.modals + .finish_progress() + .expect("Couldn't close progressbar"); + self.modals + .show_notification(t!("apploader.addapp.error", locales::LANG), None) + .expect("Couldn't show modal"); + return; + } + } + + ////////////////////// + // back to graphics // + ////////////////////// + + // add its name to GAM + self.gam + .register_name(name.to_str(), self.auth) + .expect("Couldn't register name"); + for menu in 0..menus { + self.gam + .register_name(&format!("{} Submenu {}", name, menu), self.auth) + .expect("Couldn't register name"); + } + + // add it to the menu + self.menu.insert_item( + MenuItem { + name, + action_conn: Some(self.conn), + action_opcode: Opcode::DispatchApp.to_u32().unwrap(), + action_payload: gam::MenuPayload::Scalar([ + self.apps.len().try_into().unwrap(), + 0, + 0, + 0, + ]), + close_on_select: true, + }, + 0, + ); + self.apps.push(name); + + log::info!("Added app `{}'!", name); + + self.modals + .finish_progress() + .expect("Couldn't close progressbar"); + let _ = self.gam.switch_to_app(APP_NAME_APP_LOADER, self.auth); // try to switch back to the menu + } + + pub(crate) fn set_server(&mut self) { + let payload = self + .modals + .alert_builder("Server Address") + .field( + Some("e.g. http://ip:port".to_string()), + Some( + |payload: TextEntryPayload| match url::Url::parse(payload.as_str()) { + Ok(_) => None, + Err(e) => Some(xous_ipc::String::from_str(&format!( + "{}{}", + t!("apploader.setserver.error", locales::LANG), + e + ))), + }, + ), + ) + .build() + .ok(); + + if self.server.is_none() && payload.is_some() { + self.menu.insert_item( + MenuItem { + name: xous_ipc::String::from_str(t!( + "apploader.menu.reloadapplist", + locales::LANG + )), + action_conn: Some(self.conn), + action_opcode: Opcode::ReloadAppList.to_u32().unwrap(), + action_payload: gam::MenuPayload::Scalar([0, 0, 0, 0]), + close_on_select: true, + }, + 0, + ); + self.menu.insert_item( + MenuItem { + name: xous_ipc::String::from_str(t!("apploader.menu.addapp", locales::LANG)), + action_conn: Some(self.conn), + action_opcode: Opcode::AddAppMenu.to_u32().unwrap(), + action_payload: gam::MenuPayload::Scalar([0, 0, 0, 0]), + close_on_select: true, + }, + 0, + ); + } + + self.server = payload.and_then(|p| Some(p.first().as_str().to_string())); + self.reload_app_list(); + } + + pub(crate) fn open_load_menu(&mut self) { + self.current_menu = APP_MENU_1_APP_LOADER.to_string(); + let _ = self.gam.switch_to_app(APP_NAME_APP_LOADER, self.auth); + } + + pub(crate) fn reload_app_list(&mut self) { + // without a path, the server responds with a JSON list of strings representing the list of app names + self.modals + .start_progress( + t!("apploader.reloadapplist.loading", locales::LANG), + 0, + 3, + 0, + ) + .expect("Couldn't start progressbar"); + + let old = self.possible_apps.clone(); + + // this... disgusting error handling is so that if there is an error on the server side there isn't a panic + self.possible_apps = match match ureq::get( + &self + .server + .as_ref() + .expect("ReloadAppList was somehow called without a server!"), + ) + .call() + { + Ok(c) => { + self.modals + .update_progress(1) + .expect("Couldn't update progress"); + c + } + Err(e) => { + self.modals + .show_notification( + &format!( + "{}{}", + t!("apploader.reloadapplist.connection_error", locales::LANG), + e + ), + None, + ) + .expect("Couldn't show modal"); + return; + } + } + .into_json::>() + { + Ok(json) => { + self.modals + .update_progress(2) + .expect("Couldn't update progress"); + json + } + Err(e) => { + self.modals + .show_notification( + &format!( + "{}{}", + t!("apploader.reloadapplist.json_error", locales::LANG), + e + ), + None, + ) + .expect("Couldn't show modal"); + return; + } + } + .iter() + .map(|(name, menus)| (xous_ipc::String::<64>::from_str(&name), *menus)) + .collect(); + + for (old_name, _) in old { + self.load_menu.delete_item(old_name.as_str().unwrap()); + } + + for (i, (app, _)) in self.possible_apps.iter().enumerate() { + self.load_menu.insert_item( + MenuItem { + name: xous_ipc::String::from_str(app.to_str()), + action_conn: Some(self.conn), + action_opcode: Opcode::AddApp.to_u32().unwrap(), + action_payload: gam::MenuPayload::Scalar([i.try_into().unwrap(), 0, 0, 0]), + close_on_select: true, + }, + 0, + ); + } + self.modals + .finish_progress() + .expect("Couldn't close progress bar"); + + let _ = self.gam.switch_to_app(APP_NAME_APP_LOADER, self.auth); // try to switch back to the menu + } + + pub(crate) fn dispatch_app(&self, index: usize) { + if index < self.apps.len() { + let name: xous_ipc::String<64> = self.apps[index]; + log::info!("Switching to app `{}'", name); + self.gam + .switch_to_app(name.as_ref(), self.auth) + .expect(&format!("Could not dispatch app `{}'", name)); + } else { + panic!("Unrecognized app index"); + } + } + + pub(crate) fn redraw(&mut self) { + // Properly close the app menu + // for anyone who needs this I found this in Menu::key_event + self.gam.relinquish_focus().unwrap(); + xous::yield_slice(); + + self.ticktimer.sleep_ms(100).ok(); // yield for a moment to allow the previous menu to close + + // open the submenu if possible + let _ = self.gam.raise_menu(&self.current_menu); + // we only ever need the other menu for one thing + self.current_menu = APP_MENU_0_APP_LOADER.to_string(); + } +} + +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] +pub(crate) enum Opcode { + /// set the server + SetServer, + + /// get a list of apps from the server + ReloadAppList, + + /// open the menu for adding apps + AddAppMenu, + + /// load an app and add it to the menu + AddApp, + + /// dispatch the app + DispatchApp, + + /// Redraw the UI + Redraw, +} diff --git a/apps/app-loader/src/main.rs b/apps/app-loader/src/main.rs new file mode 100644 index 000000000..f7fd08f2d --- /dev/null +++ b/apps/app-loader/src/main.rs @@ -0,0 +1,45 @@ +mod api; +use api::*; +use num_traits::FromPrimitive; + +const SERVER_NAME_APP_LOADER: &str = "_App Loader_"; + +fn main() -> ! { + log_server::init_wait().unwrap(); + log::set_max_level(log::LevelFilter::Info); + log::info!("my PID is {}", xous::process::id()); + + let xns = xous_names::XousNames::new().unwrap(); + let sid = xns + .register_name(SERVER_NAME_APP_LOADER, None) + .expect("Couldn't create server"); + + // start off by adding hello world + let mut app_loader = AppLoader::new(&xns, &sid); + + loop { + let msg = xous::receive_message(sid).unwrap(); + match FromPrimitive::from_usize(msg.body.id()).expect("Couldn't load message") { + Opcode::SetServer => { + app_loader.set_server(); + } + Opcode::ReloadAppList => { + app_loader.reload_app_list(); + } + Opcode::AddAppMenu => { + app_loader.open_load_menu(); + } + Opcode::AddApp => { + let index = msg.body.scalar_message().expect("Not a scalar").arg1; + app_loader.add_app(index); + } + Opcode::DispatchApp => { + let index = msg.body.scalar_message().expect("Not a scalar").arg1; + app_loader.dispatch_app(index); + } + Opcode::Redraw => { + app_loader.redraw(); + } + } + } +} diff --git a/apps/app-loader/src/spawn.bin b/apps/app-loader/src/spawn.bin new file mode 100644 index 000000000..b479ecd84 Binary files /dev/null and b/apps/app-loader/src/spawn.bin differ diff --git a/apps/manifest.json b/apps/manifest.json index 2c89d6054..8de98dabd 100644 --- a/apps/manifest.json +++ b/apps/manifest.json @@ -1,4 +1,14 @@ { + "app-loader": { + "context_name": "App Loader", + "menu_name": { + "appmenu.app-loader": { + "en": "App Loader", + "en-tts": "App Loader" + } + }, + "submenu": 2 + }, "ball": { "context_name": "ball demo app", "menu_name": { diff --git a/kernel/src/arch/riscv/process.rs b/kernel/src/arch/riscv/process.rs index f78fb11d4..28c4c939a 100644 --- a/kernel/src/arch/riscv/process.rs +++ b/kernel/src/arch/riscv/process.rs @@ -386,6 +386,7 @@ impl Process { } process.inner = Default::default(); + process.inner.pid = pid; // Mark the stack as "unallocated-but-free" let init_sp = (thread_init.stack.as_ptr() as usize) & !0xfff; diff --git a/services/gam/Cargo.toml b/services/gam/Cargo.toml index ec9b4d837..f14d489d4 100644 --- a/services/gam/Cargo.toml +++ b/services/gam/Cargo.toml @@ -52,3 +52,4 @@ tts = [] # default = ["debugprint"] # "debugprint" default = [] ditherpunk = [] +unsafe-app-loading = [] # allow new contexts to be registered dynamically - this is unsafe and should not be set for release builds. diff --git a/services/gam/src/api.rs b/services/gam/src/api.rs index 9c6b12767..ff995df69 100644 --- a/services/gam/src/api.rs +++ b/services/gam/src/api.rs @@ -110,6 +110,12 @@ pub struct UxRegistration { /// If the LayoutType is not an App, this field is ignored and does nothing pub focuschange_id: Option, } +#[cfg(feature="unsafe-app-loading")] +#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] +pub struct NameRegistration { + pub name: String::<128>, + pub auth_token: [u32; 4], +} #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] @@ -211,6 +217,10 @@ pub(crate) enum Opcode { /// Allow main menu activation. Used by the PDDB to turn ungate the main menu once it is mounted. /// This resolves race conditions that depend upon the PDDB configurations. AllowMainMenu = 33, + + /// Register a name that can acquire a token. This is only intended to be used with pre-registered apps + #[cfg(feature="unsafe-app-loading")] + RegisterName = 34, } // small wart -- we have to reset the size of a modal to max size for resize computations diff --git a/services/gam/src/contexts.rs b/services/gam/src/contexts.rs index 640d0d311..4ef21dcfd 100644 --- a/services/gam/src/contexts.rs +++ b/services/gam/src/contexts.rs @@ -130,6 +130,10 @@ impl ContextManager { pub(crate) fn claim_token(&mut self, name: &str) -> Option<[u32; 4]> { self.tm.claim_token(name) } + #[cfg(feature="unsafe-app-loading")] + pub(crate) fn register_name(&mut self, name: &str, auth_token: &[u32; 4]) { + self.tm.register_name(name, auth_token); + } pub(crate) fn allow_untrusted_code(&self) -> bool { self.tm.allow_untrusted_code() } diff --git a/services/gam/src/lib.rs b/services/gam/src/lib.rs index 9e351624c..5ead4ecd3 100644 --- a/services/gam/src/lib.rs +++ b/services/gam/src/lib.rs @@ -311,6 +311,16 @@ impl Gam { Ok(returned_claim.token) } + #[cfg(feature="unsafe-app-loading")] + pub fn register_name(&self, name: &str, auth_token: [u32; 4]) -> Result<(), xous::Error> { + let name_registration = NameRegistration { + name: String::<128>::from_str(name), + auth_token + }; + let buf = Buffer::into_buf(name_registration).or(Err(xous::Error::InternalError))?; + buf.lend(self.conn, Opcode::RegisterName.to_u32().unwrap()).or(Err(xous::Error::InternalError))?; + Ok(()) + } pub fn set_predictor_api_token(&self, api_token: [u32; 4], gam_token: [u32; 4]) -> Result<(), xous::Error> { let at = ApiToken { gam_token, diff --git a/services/gam/src/main.rs b/services/gam/src/main.rs index bf0bd19c9..fbe88933b 100644 --- a/services/gam/src/main.rs +++ b/services/gam/src/main.rs @@ -567,7 +567,10 @@ fn wrapped_main() -> ! { // set before the leaving state due to interior-mutability issues (see the block under the debug statement // titled "resolving visibility rules"). The fix is to simply not issue a context switch if it's the same app. // It saves CPU thrashing and also works around this problem. (see issue #145) + #[cfg(not(feature="unsafe-app-loading"))] let authorized_switchers = vec![MAIN_MENU_NAME, ROOTKEY_MODAL_NAME, gam::STATUS_BAR_NAME]; + #[cfg(feature="unsafe-app-loading")] + let authorized_switchers = [&[MAIN_MENU_NAME, ROOTKEY_MODAL_NAME, gam::STATUS_BAR_NAME], gam::EXPECTED_APP_CONTEXTS].concat().to_vec(); for switchers in authorized_switchers { if let Some(auth_token) = context_mgr.find_app_token_by_name(switchers) { if auth_token == switchapp.token { @@ -670,6 +673,13 @@ fn wrapped_main() -> ! { context_mgr.allow_mainmenu(); xous::return_scalar(msg.sender, 0).ok(); } + #[cfg(feature="unsafe-app-loading")] + Some(Opcode::RegisterName) => { + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let registration = buffer.to_original::().unwrap(); + gfx.set_devboot(); // indicate to users that we are no longer in a codebase that is exclusively trusted code + context_mgr.register_name(registration.name.to_str(), ®istration.auth_token); + } Some(Opcode::Quit) => break, None => {log::error!("unhandled message {:?}", msg);} } diff --git a/services/gam/src/tokens.rs b/services/gam/src/tokens.rs index ce8f2fde1..083629ae0 100644 --- a/services/gam/src/tokens.rs +++ b/services/gam/src/tokens.rs @@ -18,18 +18,26 @@ pub(crate) struct NamedToken { } pub(crate) struct TokenManager { tokens: Vec::, + #[cfg(feature="unsafe-app-loading")] + extra_names: Vec, trng: trng::Trng, } impl<'a> TokenManager { pub(crate) fn new(xns: &xous_names::XousNames) -> TokenManager { TokenManager { tokens: Vec::new(), + #[cfg(feature="unsafe-app-loading")] + extra_names: Vec::new(), trng: trng::Trng::new(&xns).unwrap(), } } /// checks to see if all the slots have been occupied. We can't allow untrusted code to run until all slots have checked in pub(crate) fn allow_untrusted_code(&self) -> bool { - if self.tokens.len() == (EXPECTED_BOOT_CONTEXTS.len() + EXPECTED_APP_CONTEXTS.len()) { + #[cfg(feature="unsafe-app-loading")] + let expected_len = EXPECTED_BOOT_CONTEXTS.len() + EXPECTED_APP_CONTEXTS.len() + self.extra_names.len(); + #[cfg(not(feature="unsafe-app-loading"))] + let expected_len = EXPECTED_BOOT_CONTEXTS.len() + EXPECTED_APP_CONTEXTS.len(); + if self.tokens.len() == expected_len { true } else { // throw a bone to the dev who has to debug this error. This typically only triggers after a major @@ -51,6 +59,10 @@ impl<'a> TokenManager { if EXPECTED_APP_CONTEXTS.iter().find(|&&context| context == name).is_some() { found = true; } + #[cfg(feature="unsafe-app-loading")] + if self.extra_names.iter().find(|&context| context == name).is_some() { + found = true; + } if !found { log::error!("Server {} is not pre-registered in gam/lib.rs/EXPECTED_BOOT_CONTEXTS or apps.rs/EXPECTED_APP_CONTEXTS. Did you forget to register it?", name); return None @@ -90,4 +102,21 @@ impl<'a> TokenManager { } None } + /// Register a new name that can then claim a token. Note that only pre-registered applications are allowed to do this. + #[cfg(feature="unsafe-app-loading")] + pub(crate) fn register_name(&mut self, name: &str, auth_token: &[u32; 4]) { + if let Some(registrant) = self.lookup_name(auth_token) { + if EXPECTED_BOOT_CONTEXTS.iter() + .find( + |&&context| context == registrant).is_some() + || EXPECTED_APP_CONTEXTS.iter() + .find(|&&context| context == registrant).is_some() { + self.extra_names.push(name.to_string()); + } else { + log::error!("`{}' does not have permission to register a new name because it is not pre-registered", registrant); + } + } else { + log::error!("Token {:?} does not correspond with a name", auth_token); + } + } } diff --git a/tools/app_server.py b/tools/app_server.py new file mode 100644 index 000000000..965c771bb --- /dev/null +++ b/tools/app_server.py @@ -0,0 +1,70 @@ +#! /usr/bin/env python3 + +import argparse +import json +from http.server import BaseHTTPRequestHandler, HTTPServer +from urllib.parse import unquote + +class XousAppServer(BaseHTTPRequestHandler): + def __init__(self, profile, target, context_to_app, context_to_menus, *args): + self.profile = profile + self.target = target + self.context_to_app = context_to_app + self.context_to_menus = context_to_menus + BaseHTTPRequestHandler.__init__(self, *args) + + def do_GET(self): + # an empty path should return a list of apps + path = self.path.split('/')[-1] + if path == '': + self.send_response(200) + self.send_header('Content-Type', 'application/json') + self.end_headers() + + l = json.dumps(list(self.context_to_menus.items())) + + self.wfile.write(l.encode()) + else: + name = self.context_to_app[unquote(path)] + with open('target/{}/{}/{}'.format(self.target, self.profile, name), 'rb') as f: + data = f.read() + self.send_response(200) + self.send_header('Content-Type', 'application/octet-stream') + self.send_header('Content-Length', str(len(data))) + self.end_headers() + + self.wfile.write(data) + +def main(): + parser = argparse.ArgumentParser(description="Xous App Server") + parser.add_argument('apps', metavar='APP', nargs='+', + help='Specifies which apps to serve') + parser.add_argument('-p', '--port', + help='Specifies the port that the server runs on', + required=True, type=int) + parser.add_argument('--profile', default="release", + help="Either debug or release depending on how the apps were compiled. Defaults to release") + parser.add_argument('--target', default="riscv32imac-unknown-xous-elf", + help="The target that the apps were compile to. Defaults to riscv32imac-unknown-xous-elf") + args = parser.parse_args() + + # get the GAM names for each of the apps + with open('apps/manifest.json', 'r') as f: + manifest = json.loads(f.read()) + + context_to_app = {} + context_to_menus = {} + + for app in args.apps: + context_name = manifest[app]['context_name'] + context_to_app[context_name] = app + if 'submenu' in manifest[app]: + context_to_menus[context_name] = manifest[app]['submenu'] + else: + context_to_menus[context_name] = 0 + + server = HTTPServer(('0.0.0.0', args.port), lambda *server_args: XousAppServer(args.profile, args.target, context_to_app, context_to_menus, *server_args)) + server.serve_forever() + +if __name__ == "__main__": + main() diff --git a/xtask/src/app_manifest.rs b/xtask/src/app_manifest.rs index 076f47df7..77a768cf4 100644 --- a/xtask/src/app_manifest.rs +++ b/xtask/src/app_manifest.rs @@ -67,7 +67,7 @@ pub(crate) fn generate_app_menus(apps: &Vec) { writeln!( gam_tokens, "pub const APP_NAME_{}: &'static str = \"{}\";", - app_name.to_uppercase(), + app_name.to_uppercase().replace("-", "_"), manifest.context_name, ) .unwrap(); @@ -77,7 +77,7 @@ pub(crate) fn generate_app_menus(apps: &Vec) { gam_tokens, "pub const APP_MENU_{}_{}: &'static str = \"{} Submenu {}\";", i, - app_name.to_uppercase(), + app_name.to_uppercase().replace("-", "_"), manifest.context_name, i, ) @@ -89,16 +89,16 @@ pub(crate) fn generate_app_menus(apps: &Vec) { gam_tokens, "\npub const EXPECTED_APP_CONTEXTS: &[&'static str] = &[" ) - .unwrap(); + .unwrap(); for (app_name, manifest) in working_set.iter() { - writeln!(gam_tokens, " APP_NAME_{},", app_name.to_uppercase(),).unwrap(); + writeln!(gam_tokens, " APP_NAME_{},", app_name.to_uppercase().replace("-", "_"),).unwrap(); if let Some(menu_count) = manifest.submenu { for i in 0..menu_count { writeln!( gam_tokens, " APP_MENU_{}_{},", i, - app_name.to_uppercase(), + app_name.to_uppercase().replace("-", "_"), ) .unwrap(); } @@ -151,7 +151,7 @@ pub(crate) fn app_dispatch(gam: &gam::Gam, token: [u32; 4], index: usize) -> Res Ok(()) }},", index, - app_name.to_uppercase() + app_name.to_uppercase().replace("-", "_") ) .unwrap(); } diff --git a/xtask/src/builder.rs b/xtask/src/builder.rs index d517f205f..60b7e6caa 100644 --- a/xtask/src/builder.rs +++ b/xtask/src/builder.rs @@ -130,6 +130,8 @@ pub(crate) struct Builder { locale_stash: String, /// when set to true, hosted mode builds but does not run dry_run: bool, + /// when set to true, user selected packages are compiled but no image is created + no_image: bool, } impl Builder { @@ -154,6 +156,7 @@ impl Builder { locale_override: None, locale_stash: String::new(), dry_run: false, + no_image: false, } } /// Specify an alternate loader key, as a String that can encode a file name @@ -237,6 +240,14 @@ impl Builder { self.kernel = CrateSpec::Local("xous-kernel".to_string(), false); self } + pub fn target_precursor_no_image<'a>(&'a mut self, soc_version: &str) -> &'a mut Builder { + self.target = Some(crate::TARGET_TRIPLE_RISCV32.to_string()); + self.stream = BuildStream::Release; + self.utra_target = format!("precursor-{}", soc_version).to_string(); + self.run_svd2repl = false; + self.no_image = true; + self + } /// Configure for ARM targets pub fn target_arm(&mut self) -> &mut Builder { @@ -597,8 +608,13 @@ impl Builder { false )?; - // ------ either create an image, or launch hosted mode ------ - if self.target.is_none() { // hosted mode doesn't specify a cross-compilation target! + // ------ either stop here, create an image, or launch hosted mode ------ + if self.no_image { + println!("The following apps/services were compiled:"); + for path in services_path { + println!("{}", path); + } + } else if self.target.is_none() { // hosted mode doesn't specify a cross-compilation target! // throw a warning if prebuilts are specified for hosted mode for item in [&self.services[..], &self.apps[..]].concat() { match item { diff --git a/xtask/src/main.rs b/xtask/src/main.rs index 77cffe033..b8a0bbb4e 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -363,6 +363,10 @@ fn main() -> Result<(), Box> { .add_services(&user_pkgs.into_iter().map(String::from).collect()) .add_feature("avalanchetest"); } + Some("compile-apps") => { + builder.target_precursor_no_image(PRECURSOR_SOC_VERSION) + .add_services(&gfx_base_pkgs.into_iter().map(String::from).collect()); + } // ------ Cramium hardware image configs ------ Some("cramium-fpga") | Some("cramium-soc") => { @@ -502,6 +506,7 @@ Other commands: generate-locales (re)generate the locales include for the language selected in locales/src/locale.rs wycheproof-import generate binary test vectors for engine-25519 from whycheproof-import/x25519.json install-toolkit installs Xous toolkit with no prompt, useful in CI. Specify `--force` to remove existing toolchains + compile-apps Just compiles the apps specified in [cratespecs], for example in order to use app server Note: By default, the `ticktimer` will get rebuilt every time. You can skip this by appending `--no-timestamp` to the command. "