diff --git a/Cargo.lock b/Cargo.lock index 4db1c2f57..8d5308846 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4480,8 +4480,10 @@ dependencies = [ "rand 0.7.3", "rand_chacha 0.3.1", "rkyv", + "trng", "usb-device", "usbd-human-interface-device 0.2.1", + "usbd-serial", "usbd_mass_storage 0.1.0", "usbd_scsi 0.1.1", "utralib", @@ -4552,6 +4554,17 @@ dependencies = [ "usb-device", ] +[[package]] +name = "usbd-serial" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db75519b86287f12dcf0d171c7cf4ecc839149fe9f3b720ac4cfce52959e1dfe" +dependencies = [ + "embedded-hal", + "nb 0.1.3", + "usb-device", +] + [[package]] name = "usbd_bulk_only_transport" version = "0.1.0" @@ -5192,9 +5205,9 @@ dependencies = [ [[package]] name = "xous-api-log" -version = "0.1.35" +version = "0.1.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37e5fc7b10d5fe52535fae54c223b5684ecef172fce2a136fe9e59d370ce06d1" +checksum = "b82ebf0502939b72c0fb63403d6c12b4847ee1a720069bf73ce35980edeecdbb" dependencies = [ "log", "num-derive", @@ -5205,9 +5218,9 @@ dependencies = [ [[package]] name = "xous-api-names" -version = "0.9.37" +version = "0.9.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c4775bac0894064e2d8028c7b7c470f62b4fb266950ff7f73eca69f5fc0a055" +checksum = "84bceedef22f50d5c809c60bbd16dfa4cfc8d4f463c001b3c50b1e613a820d47" dependencies = [ "log", "num-derive", @@ -5220,9 +5233,9 @@ dependencies = [ [[package]] name = "xous-api-susres" -version = "0.9.35" +version = "0.9.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "90ffb0b3fe55eb591fc6e5c0decb1f497b03f497322da356b01367ab613c719e" +checksum = "20805762535b886706a727ee57fdb8355dc9d5770619b926ae697c6f1754e9b9" dependencies = [ "log", "num-derive", @@ -5237,9 +5250,9 @@ dependencies = [ [[package]] name = "xous-api-ticktimer" -version = "0.9.35" +version = "0.9.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc56e99340b3ccc0b01b960e55d51054336f71bcb5bce2fb793aac31f95ca57b" +checksum = "25c60d1d4962904198a693e5b4ad3b560e3a04ef13b7a88a2a27ec2925f4f9ec" dependencies = [ "log", "num-derive", @@ -5302,6 +5315,7 @@ dependencies = [ "log", "num-derive", "num-traits", + "rkyv", "utralib", "xous 0.9.40 (registry+https://github.com/rust-lang/crates.io-index)", "xous-api-log", diff --git a/Cargo.toml b/Cargo.toml index ada1be467..d896ce944 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -179,3 +179,5 @@ rev="4296c2e7904898766cf7d8d589759a129794783b" # use the commitref because we're # path = "./svd2utra" # [patch.crates-io.xous-api-susres] # path = "./api/xous-api-susres" +# [patch.crates-io.xous-api-log] +# path = "./api/xous-api-log" diff --git a/api/xous-api-log/Cargo.toml b/api/xous-api-log/Cargo.toml index 612f2b42c..c1d8d8dd4 100644 --- a/api/xous-api-log/Cargo.toml +++ b/api/xous-api-log/Cargo.toml @@ -3,7 +3,7 @@ authors = ["Sean Cross "] description = "Log server API" edition = "2018" name = "xous-api-log" -version = "0.1.35" +version = "0.1.36" license = "MIT OR Apache-2.0" repository = "https://github.com/betrusted-io/xous-core/" homepage = "https://betrusted.io/xous-book/" diff --git a/api/xous-api-log/src/api.rs b/api/xous-api-log/src/api.rs index e6bd5896b..b0f64ffea 100644 --- a/api/xous-api-log/src/api.rs +++ b/api/xous-api-log/src/api.rs @@ -30,6 +30,10 @@ pub enum Opcode { /// A `xous::StringBuffer` containing this program's name ProgramName = 3, + /// Try to log console output to a USB serial port. Best-effort only; failures will not crash, will not be noted + TryHookUsbMirror = 4, + UnhookUsbMirror = 5, + /// A panic occurred, and a panic log is forthcoming PanicStarted = 1000, diff --git a/api/xous-api-names/Cargo.toml b/api/xous-api-names/Cargo.toml index 27fc6aa50..c3fa36a26 100644 --- a/api/xous-api-names/Cargo.toml +++ b/api/xous-api-names/Cargo.toml @@ -3,14 +3,14 @@ authors = ["bunnie "] description = "Xous microkernel OS inter-process name resolution server" edition = "2018" name = "xous-api-names" -version = "0.9.37" +version = "0.9.38" license = "MIT OR Apache-2.0" repository = "https://github.com/betrusted-io/xous-core/" homepage = "https://betrusted.io/" # Dependency versions enforced by Cargo.lock. [dependencies] -log-server = {package = "xous-api-log", version = "0.1.35"} +log-server = {package = "xous-api-log", version = "0.1.36"} xous = "0.9.40" xous-ipc = "0.9.40" num-derive = {version = "0.3.3", default-features = false} diff --git a/api/xous-api-susres/Cargo.toml b/api/xous-api-susres/Cargo.toml index 42653f04b..e99c07ac7 100644 --- a/api/xous-api-susres/Cargo.toml +++ b/api/xous-api-susres/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "xous-api-susres" -version = "0.9.35" +version = "0.9.36" authors = ["bunnie "] edition = "2018" description = "Manager of suspend/resume operations" @@ -12,8 +12,8 @@ homepage = "https://betrusted.io/xous-book/" [dependencies] xous = "0.9.40" xous-ipc = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } log = "0.4.14" num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} diff --git a/api/xous-api-ticktimer/Cargo.toml b/api/xous-api-ticktimer/Cargo.toml index 5eebcba85..fd1b7089d 100644 --- a/api/xous-api-ticktimer/Cargo.toml +++ b/api/xous-api-ticktimer/Cargo.toml @@ -3,7 +3,7 @@ authors = ["bunnie ", "Sean Cross "] description = "Provide high-resolution, non-rollover system time" edition = "2018" name = "xous-api-ticktimer" -version = "0.9.35" +version = "0.9.36" license = "MIT OR Apache-2.0" repository = "https://github.com/betrusted-io/xous-core/" homepage = "https://betrusted.io/xous-book/" @@ -12,9 +12,9 @@ homepage = "https://betrusted.io/xous-book/" [dependencies] xous = "0.9.40" xous-ipc = "0.9.40" -log-server = {package = "xous-api-log", version = "0.1.35"} -susres = {package = "xous-api-susres", version = "0.9.35"} -xous-names = {package = "xous-api-names", version = "0.9.37"} +log-server = {package = "xous-api-log", version = "0.1.36"} +susres = {package = "xous-api-susres", version = "0.9.36"} +xous-names = {package = "xous-api-names", version = "0.9.38"} log = "0.4.14" rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} num-derive = {version = "0.3.3", default-features = false} diff --git a/apps/ball/Cargo.toml b/apps/ball/Cargo.toml index 571b8bf26..9324de5df 100644 --- a/apps/ball/Cargo.toml +++ b/apps/ball/Cargo.toml @@ -12,9 +12,9 @@ num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} xous = "0.9.40" xous-ipc = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } gam = {path = "../../services/gam" } trng = {path = "../../services/trng"} modals = {path = "../../services/modals"} diff --git a/apps/hello/Cargo.toml b/apps/hello/Cargo.toml index 0c1cf04fa..788990675 100644 --- a/apps/hello/Cargo.toml +++ b/apps/hello/Cargo.toml @@ -9,8 +9,8 @@ num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} xous = "0.9.40" xous-ipc = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } gam = {path = "../../services/gam" } graphics-server = {path = "../../services/graphics-server" } locales = {path = "../../locales"} diff --git a/apps/mtxcli/Cargo.toml b/apps/mtxcli/Cargo.toml index a0db3d7ed..03bf687fc 100644 --- a/apps/mtxcli/Cargo.toml +++ b/apps/mtxcli/Cargo.toml @@ -12,9 +12,9 @@ num-derive = { version = "0.3.3", default-features = false} num-traits = { version = "0.2.14", default-features = false} xous = "0.9.40" xous-ipc = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } gam = { path = "../../services/gam" } graphics-server = { path = "../../services/graphics-server" } trng = { path = "../../services/trng"} diff --git a/apps/repl/Cargo.toml b/apps/repl/Cargo.toml index 053579886..6c7d8dbae 100644 --- a/apps/repl/Cargo.toml +++ b/apps/repl/Cargo.toml @@ -12,9 +12,9 @@ num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} xous = "0.9.40" xous-ipc = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } gam = {path = "../../services/gam" } graphics-server = {path = "../../services/graphics-server" } trng = {path = "../../services/trng"} diff --git a/apps/transientdisk/Cargo.toml b/apps/transientdisk/Cargo.toml index adf433b45..890975750 100644 --- a/apps/transientdisk/Cargo.toml +++ b/apps/transientdisk/Cargo.toml @@ -11,8 +11,8 @@ num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} xous = "0.9.40" xous-ipc = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } gam = {path = "../../services/gam" } graphics-server = {path = "../../services/graphics-server" } usb-device-xous = {path="../../services/usb-device-xous"} diff --git a/apps/vault/Cargo.toml b/apps/vault/Cargo.toml index f4623d327..7d37976ca 100644 --- a/apps/vault/Cargo.toml +++ b/apps/vault/Cargo.toml @@ -10,8 +10,8 @@ num-traits = {version = "0.2.14", default-features = false} xous = "0.9.40" xous-ipc = "0.9.40" rkyv = {version = "0.4.3", features = ["const_generics"], default-features = false} -log-server = { package = "xous-api-log", version = "0.1.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } gam = {path = "../../services/gam" } graphics-server = {path = "../../services/graphics-server" } locales = {path = "../../locales"} @@ -21,7 +21,7 @@ usbd-human-interface-device = "0.2.1" pddb = {path = "../../services/pddb" } modals = {path = "../../services/modals" } trng = {path="../../services/trng"} -susres = {package = "xous-api-susres", version = "0.9.35"} +susres = {package = "xous-api-susres", version = "0.9.36"} ime-plugin-api = {path = "../../services/ime-plugin-api"} content-plugin-api = {path = "../../services/content-plugin-api"} # all content canvas providers must provide this API backup = {path = "libraries/backup"} @@ -29,7 +29,7 @@ byteorder = { version = "1.4.3", default-features = false } arrayref = "0.3.6" subtle = { version = "2.2.3", default-features = false, features = ["nightly"] } rand_core = "0.6.3" -ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.35"} +ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.36"} # opensk ctap-crypto = {path = "libraries/crypto"} diff --git a/apps/vault/libraries/crypto/Cargo.toml b/apps/vault/libraries/crypto/Cargo.toml index 2106c6f1a..d93101c91 100644 --- a/apps/vault/libraries/crypto/Cargo.toml +++ b/apps/vault/libraries/crypto/Cargo.toml @@ -14,7 +14,7 @@ cbor = { path = "../cbor" } arrayref = "0.3.6" subtle = { version = "2.2.3", default-features = false} trng = {path = "../../../../services/trng"} -xous-names = {package = "xous-api-names", version = "0.9.37"} +xous-names = {package = "xous-api-names", version = "0.9.38"} rand_core = "0.6.3" p256 = {version = "0.10.1", default-features = false, features = ["ecdsa", "ecdh", "std"]} cbc = "0.1.2" diff --git a/imports/getrandom/Cargo.toml b/imports/getrandom/Cargo.toml index 12db83bda..3444c734d 100644 --- a/imports/getrandom/Cargo.toml +++ b/imports/getrandom/Cargo.toml @@ -30,7 +30,7 @@ js-sys = { version = "0.3", optional = true } wasm-bindgen-test = "0.3.18" [target.'cfg(target_os = "xous")'.dependencies] -xous-names = {package = "xous-api-names", version = "0.9.37"} +xous-names = {package = "xous-api-names", version = "0.9.38"} xous = "0.9.40" xous-ipc = "0.9.40" rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} diff --git a/libs/mass-storage/usbd_bulk_only_transport/Cargo.toml b/libs/mass-storage/usbd_bulk_only_transport/Cargo.toml index 7a87558d5..98871e36e 100644 --- a/libs/mass-storage/usbd_bulk_only_transport/Cargo.toml +++ b/libs/mass-storage/usbd_bulk_only_transport/Cargo.toml @@ -21,6 +21,6 @@ usbd_mass_storage = { version = "0.1.0", path = "../usbd_mass_storage" } packing = { version = "0.2.0", path = "../packing/packing" } log = "0.4.17" xous = "0.9.40" -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } [features] diff --git a/libs/mass-storage/usbd_scsi/Cargo.toml b/libs/mass-storage/usbd_scsi/Cargo.toml index 95d209373..50d6bb32a 100644 --- a/libs/mass-storage/usbd_scsi/Cargo.toml +++ b/libs/mass-storage/usbd_scsi/Cargo.toml @@ -23,6 +23,6 @@ packing = { version = "0.2.0", path = "../packing/packing" } usbd_bulk_only_transport = { version = "0.1.0", path = "../usbd_bulk_only_transport" } log = "0.4.17" xous = "0.9.40" -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } [features] diff --git a/libs/userprefs/Cargo.toml b/libs/userprefs/Cargo.toml index f201b9577..d998c88b8 100644 --- a/libs/userprefs/Cargo.toml +++ b/libs/userprefs/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] -xous-names = {package = "xous-api-names", version = "0.9.37"} +xous-names = {package = "xous-api-names", version = "0.9.38"} pddb = {path = "../../services/pddb"} keyboard = {path = "../../services/keyboard"} bincode = {version = "2.0.0-rc.2"} diff --git a/precursors/soc_csr_pvt.bin b/precursors/soc_csr_pvt.bin index f086f4273..fac1d2089 100644 Binary files a/precursors/soc_csr_pvt.bin and b/precursors/soc_csr_pvt.bin differ diff --git a/services/aes-test/Cargo.toml b/services/aes-test/Cargo.toml index 1296a025a..7896a9881 100644 --- a/services/aes-test/Cargo.toml +++ b/services/aes-test/Cargo.toml @@ -8,7 +8,7 @@ version = "0.1.0" # Dependency versions enforced by Cargo.lock. [dependencies] log = "0.4.14" -log-server = {package = "xous-api-log", version = "0.1.35"} +log-server = {package = "xous-api-log", version = "0.1.36"} hex-literal = "0.3.1" xous = "0.9.40" diff --git a/services/aes/Cargo.toml b/services/aes/Cargo.toml index f6b3592cf..4f575131f 100644 --- a/services/aes/Cargo.toml +++ b/services/aes/Cargo.toml @@ -8,7 +8,7 @@ description = "AES library for Xous" # Dependency versions enforced by Cargo.lock. [dependencies] xous = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } +log-server = { package = "xous-api-log", version = "0.1.36" } log = "0.4.14" hex-literal = "0.3.1" cipher = "0.4.2" diff --git a/services/codec/Cargo.toml b/services/codec/Cargo.toml index 33e48f6d2..a5358f87f 100644 --- a/services/codec/Cargo.toml +++ b/services/codec/Cargo.toml @@ -8,11 +8,11 @@ description = "Audio CODEC server" # Dependency versions enforced by Cargo.lock. [dependencies] xous = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } log = "0.4.14" -susres = {package = "xous-api-susres", version = "0.9.35"} +susres = {package = "xous-api-susres", version = "0.9.36"} llio = {path = "../llio"} trng = {path = "../trng"} diff --git a/services/com/Cargo.toml b/services/com/Cargo.toml index 54bcb4d2f..ab53055d5 100644 --- a/services/com/Cargo.toml +++ b/services/com/Cargo.toml @@ -9,13 +9,13 @@ version = "0.1.0" [dependencies] com_rs-ref = {path = "../../imports/com_rs-ref"} log = "0.4.14" -log-server = {package = "xous-api-log", version = "0.1.35"} -ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.35"} -susres = {package = "xous-api-susres", version = "0.9.35"} +log-server = {package = "xous-api-log", version = "0.1.36"} +ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.36"} +susres = {package = "xous-api-susres", version = "0.9.36"} typenum = "1.12" xous = "0.9.40" xous-ipc = "0.9.40" -xous-names = {package = "xous-api-names", version = "0.9.37"} +xous-names = {package = "xous-api-names", version = "0.9.38"} trng = {path = "../trng"} llio = {path = "../llio"} diff --git a/services/content-plugin-api/Cargo.toml b/services/content-plugin-api/Cargo.toml index cd50c9f1b..9a6721948 100644 --- a/services/content-plugin-api/Cargo.toml +++ b/services/content-plugin-api/Cargo.toml @@ -10,6 +10,6 @@ description = "Content Canvas Plugin Common APIs" xous = "0.9.40" rkyv = { version = "0.4.3", default_features = false } graphics-server = { path="../graphics-server"} # this is used by the IMEF portion of the API -xous-names = {package = "xous-api-names", version = "0.9.37"} # used by the IMEF for registering listeners +xous-names = {package = "xous-api-names", version = "0.9.38"} # used by the IMEF for registering listeners log = "0.4.14" diff --git a/services/dns/Cargo.toml b/services/dns/Cargo.toml index dcc7097c7..9f0f767d6 100644 --- a/services/dns/Cargo.toml +++ b/services/dns/Cargo.toml @@ -8,9 +8,9 @@ description = "Xous DNS resolver" # Dependency versions enforced by Cargo.lock. [dependencies] xous = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } log = "0.4.14" num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} @@ -28,7 +28,7 @@ chrono = {version = "0.4.19", default-features = false, features = ["std"]} sntpc = {version = "0.3.1"} #, features = ["log"] locales = {path = "../../locales"} gam = {path = "../gam"} -susres = {package = "xous-api-susres", version = "0.9.35"} +susres = {package = "xous-api-susres", version = "0.9.36"} userprefs = {path = "../../libs/userprefs"} modals = {path = "../modals"} diff --git a/services/engine-25519/Cargo.toml b/services/engine-25519/Cargo.toml index 39a430c89..97a6610c7 100644 --- a/services/engine-25519/Cargo.toml +++ b/services/engine-25519/Cargo.toml @@ -8,11 +8,11 @@ description = "Curve25519 Engine" # Dependency versions enforced by Cargo.lock. [dependencies] xous = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } log = "0.4.14" -susres = {package = "xous-api-susres", version = "0.9.35"} +susres = {package = "xous-api-susres", version = "0.9.36"} llio = {path = "../llio"} # temporary for testing some power stuff utralib = { version = "0.1.17", optional = true, default-features = false } diff --git a/services/engine-sha512/Cargo.toml b/services/engine-sha512/Cargo.toml index bd358735b..82c654b38 100644 --- a/services/engine-sha512/Cargo.toml +++ b/services/engine-sha512/Cargo.toml @@ -8,11 +8,11 @@ description = "Sha512 hardware accelerator engine" # Dependency versions enforced by Cargo.lock. [dependencies] xous = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } log = "0.4.14" -susres = {package = "xous-api-susres", version = "0.9.35"} +susres = {package = "xous-api-susres", version = "0.9.36"} trng = { path = "../trng" } xous-ipc = "0.9.40" diff --git a/services/ffi-test/Cargo.toml b/services/ffi-test/Cargo.toml index 8a1c99480..da1e323c5 100644 --- a/services/ffi-test/Cargo.toml +++ b/services/ffi-test/Cargo.toml @@ -8,13 +8,13 @@ description = "FFI test integration" # Dependency versions enforced by Cargo.lock. [dependencies] xous = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } log = "0.4.14" num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} -susres = {package = "xous-api-susres", version = "0.9.35"} +susres = {package = "xous-api-susres", version = "0.9.36"} ffi-sys = {path = "sys"} keyboard = {path = "../keyboard"} diff --git a/services/gam/Cargo.toml b/services/gam/Cargo.toml index 5cd639dcf..57728805f 100644 --- a/services/gam/Cargo.toml +++ b/services/gam/Cargo.toml @@ -14,18 +14,18 @@ ime-plugin-api = {path = "../ime-plugin-api"} ime-plugin-shell = {path = "../ime-plugin-shell"} keyboard = {path = "../keyboard"} log = "0.4.14" -log-server = {package = "xous-api-log", version = "0.1.35"} -ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.35"} +log-server = {package = "xous-api-log", version = "0.1.36"} +ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.36"} trng = {path = "../trng"} xous = "0.9.40" xous-ipc = "0.9.40" -xous-names = {package = "xous-api-names", version = "0.9.37"} +xous-names = {package = "xous-api-names", version = "0.9.38"} num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} -susres = {package = "xous-api-susres", version = "0.9.35"} # used for the sleep now menu item +susres = {package = "xous-api-susres", version = "0.9.36"} # used for the sleep now menu item enum_dispatch = "0.3.7" # used for trait-based dispatch off of multiple layout objects. locales = {path = "../../locales"} diff --git a/services/graphics-server/Cargo.toml b/services/graphics-server/Cargo.toml index ce78772b3..227006e74 100644 --- a/services/graphics-server/Cargo.toml +++ b/services/graphics-server/Cargo.toml @@ -9,13 +9,13 @@ version = "0.1.0" [dependencies] keyboard = {path = "../keyboard"} log = "0.4.14" -log-server = {package = "xous-api-log", version = "0.1.35"} +log-server = {package = "xous-api-log", version = "0.1.36"} xous = "0.9.40" -susres = {package = "xous-api-susres", version = "0.9.35"} -ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.35"} +susres = {package = "xous-api-susres", version = "0.9.36"} +ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.36"} xous-ipc = "0.9.40" -xous-names = {package = "xous-api-names", version = "0.9.37"} +xous-names = {package = "xous-api-names", version = "0.9.38"} num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} diff --git a/services/ime-frontend/Cargo.toml b/services/ime-frontend/Cargo.toml index 8a02d2d21..a9656fcaa 100644 --- a/services/ime-frontend/Cargo.toml +++ b/services/ime-frontend/Cargo.toml @@ -12,12 +12,12 @@ graphics-server = {path = "../graphics-server"} ime-plugin-api = {path = "../ime-plugin-api"} keyboard = {path = "../keyboard"} log = "0.4.14" -log-server = {package = "xous-api-log", version = "0.1.35"} -ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.35"} +log-server = {package = "xous-api-log", version = "0.1.36"} +ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.36"} xous = "0.9.40" locales = {path = "../../locales"} xous-ipc = "0.9.40" -xous-names = {package = "xous-api-names", version = "0.9.37"} +xous-names = {package = "xous-api-names", version = "0.9.38"} num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} diff --git a/services/ime-plugin-api/Cargo.toml b/services/ime-plugin-api/Cargo.toml index 62fe7c6c3..0ef6c111d 100644 --- a/services/ime-plugin-api/Cargo.toml +++ b/services/ime-plugin-api/Cargo.toml @@ -11,7 +11,7 @@ graphics-server = {path = "../graphics-server"}# this is used by the IMEF portio log = "0.4.14" xous = "0.9.40" xous-ipc = "0.9.40" -xous-names = {package = "xous-api-names", version = "0.9.37"}# used by the IMEF for registering listeners +xous-names = {package = "xous-api-names", version = "0.9.38"}# used by the IMEF for registering listeners num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} diff --git a/services/ime-plugin-shell/Cargo.toml b/services/ime-plugin-shell/Cargo.toml index 690e3b7a4..2790bc2ca 100644 --- a/services/ime-plugin-shell/Cargo.toml +++ b/services/ime-plugin-shell/Cargo.toml @@ -9,11 +9,11 @@ version = "0.1.0" [dependencies] ime-plugin-api = {path = "../ime-plugin-api"} log = "0.4.14" -log-server = {package = "xous-api-log", version = "0.1.35"} -ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.35"} +log-server = {package = "xous-api-log", version = "0.1.36"} +ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.36"} xous = "0.9.40" xous-ipc = "0.9.40" -xous-names = {package = "xous-api-names", version = "0.9.37"} +xous-names = {package = "xous-api-names", version = "0.9.38"} num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} diff --git a/services/ime-plugin-tts/Cargo.toml b/services/ime-plugin-tts/Cargo.toml index 3d4fce306..43ca2ca83 100644 --- a/services/ime-plugin-tts/Cargo.toml +++ b/services/ime-plugin-tts/Cargo.toml @@ -9,11 +9,11 @@ version = "0.1.0" [dependencies] ime-plugin-api = {path = "../ime-plugin-api"} log = "0.4.14" -log-server = {package = "xous-api-log", version = "0.1.35"} -ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.35"} +log-server = {package = "xous-api-log", version = "0.1.36"} +ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.36"} xous = "0.9.40" xous-ipc = "0.9.40" -xous-names = {package = "xous-api-names", version = "0.9.37"} +xous-names = {package = "xous-api-names", version = "0.9.38"} num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} diff --git a/services/jtag/Cargo.toml b/services/jtag/Cargo.toml index fe8726979..ed985b716 100644 --- a/services/jtag/Cargo.toml +++ b/services/jtag/Cargo.toml @@ -8,13 +8,13 @@ description = "JTAG port server" # Dependency versions enforced by Cargo.lock. [dependencies] xous = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } log = "0.4.14" num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} -susres = {package = "xous-api-susres", version = "0.9.35"} +susres = {package = "xous-api-susres", version = "0.9.36"} xous-ipc = "0.9.40" rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} diff --git a/services/kernel-test/Cargo.toml b/services/kernel-test/Cargo.toml index c5198c76c..80256df67 100644 --- a/services/kernel-test/Cargo.toml +++ b/services/kernel-test/Cargo.toml @@ -8,8 +8,8 @@ description = "Program to test various aspects of the kernel" # Dependency versions enforced by Cargo.lock. [dependencies] xous = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } log = "0.4.14" utralib = { version = "0.1.17", optional = true, default-features = false } diff --git a/services/keyboard/Cargo.toml b/services/keyboard/Cargo.toml index 69ea7926a..f0d6e879f 100644 --- a/services/keyboard/Cargo.toml +++ b/services/keyboard/Cargo.toml @@ -8,13 +8,13 @@ version = "0.1.0" # Dependency versions enforced by Cargo.lock. [dependencies] log = {version = "0.4", features = ["max_level_trace", "release_max_level_trace"]} -log-server = {package = "xous-api-log", version = "0.1.35"} -ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.35"} +log-server = {package = "xous-api-log", version = "0.1.36"} +ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.36"} xous = "0.9.40" xous-ipc = "0.9.40" -xous-names = {package = "xous-api-names", version = "0.9.37"} +xous-names = {package = "xous-api-names", version = "0.9.38"} llio = {path = "../llio"} -susres = {package = "xous-api-susres", version = "0.9.35"} +susres = {package = "xous-api-susres", version = "0.9.36"} spinor = {path = "../spinor"} num-derive = {version = "0.3.3", default-features = false} @@ -29,7 +29,8 @@ hosted = ["utralib/hosted"] renode = ["utralib/renode"] dvt = [] debugprint = [] -debuginject = [] # used only if you want key injection via the UART +debuginject = [] # used only if you want direct key injection via serial or USB. Could be a security risk! rawserial = [] braille = [] -default = ["debuginject"] # "debugprint" +inject-api = [] # used by the serial feature in usb +default = ["debuginject", "inject-api"] # "debugprint" diff --git a/services/keyboard/src/lib.rs b/services/keyboard/src/lib.rs index 3a4f57290..4eadb4590 100644 --- a/services/keyboard/src/lib.rs +++ b/services/keyboard/src/lib.rs @@ -112,6 +112,16 @@ impl Keyboard { c as u32 as usize, 0, 0, 0 )).unwrap(); } + + /// This is used to shove keys into the keyboard module as if you were typing on the + /// physical keyboard. + #[cfg(feature="inject-api")] + pub fn inject_key(&self, c: char) { + send_message(self.conn, + Message::new_scalar(Opcode::InjectKey.to_usize().unwrap(), + c as u32 as usize, 0, 0, 0 + )).unwrap(); + } } use core::sync::atomic::{AtomicU32, Ordering}; diff --git a/services/libstd-test/Cargo.toml b/services/libstd-test/Cargo.toml index fac588806..785571770 100644 --- a/services/libstd-test/Cargo.toml +++ b/services/libstd-test/Cargo.toml @@ -8,9 +8,9 @@ description = "libstd test stub" # Dependency versions enforced by Cargo.lock. [dependencies] xous = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } log = "0.4.14" num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} diff --git a/services/llio/Cargo.toml b/services/llio/Cargo.toml index 7401d35c8..4249cacbd 100644 --- a/services/llio/Cargo.toml +++ b/services/llio/Cargo.toml @@ -8,12 +8,12 @@ version = "0.1.0" # Dependency versions enforced by Cargo.lock. [dependencies] log = "0.4.14" -log-server = {package = "xous-api-log", version = "0.1.35"} -ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.35"} +log-server = {package = "xous-api-log", version = "0.1.36"} +ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.36"} xous = "0.9.40" xous-ipc = "0.9.40" -xous-names = {package = "xous-api-names", version = "0.9.37"} -susres = {package = "xous-api-susres", version = "0.9.35"} +xous-names = {package = "xous-api-names", version = "0.9.38"} +susres = {package = "xous-api-susres", version = "0.9.36"} # RTC dependencies bitflags = "1.2.1" diff --git a/services/modals/Cargo.toml b/services/modals/Cargo.toml index a1837e3c2..92d3b618a 100644 --- a/services/modals/Cargo.toml +++ b/services/modals/Cargo.toml @@ -8,9 +8,9 @@ description = "Ux modals server" # Dependency versions enforced by Cargo.lock. [dependencies] xous = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } log = "0.4.14" num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} diff --git a/services/net/Cargo.toml b/services/net/Cargo.toml index 99d852119..0111f2083 100644 --- a/services/net/Cargo.toml +++ b/services/net/Cargo.toml @@ -9,13 +9,13 @@ description = "Network middleware" [dependencies] xous = "0.9.40" xous-ipc = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } log = "0.4.14" num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} -susres = {package = "xous-api-susres", version = "0.9.35"} +susres = {package = "xous-api-susres", version = "0.9.36"} #rkyv = "0.7.18" rkyv = {version = "0.4.3", features = ["const_generics"], default-features = false} llio = {path = "../llio"} diff --git a/services/pddb/Cargo.toml b/services/pddb/Cargo.toml index 2fdfa3ee6..b86eda4ce 100644 --- a/services/pddb/Cargo.toml +++ b/services/pddb/Cargo.toml @@ -10,10 +10,10 @@ description = "Plausibly Deniable Database" bitflags = {version = "1"} xous = "0.9.40" xous-ipc = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } -susres = {package = "xous-api-susres", version = "0.9.35"} +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } +susres = {package = "xous-api-susres", version = "0.9.36"} log = "0.4.14" num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} diff --git a/services/root-keys/Cargo.toml b/services/root-keys/Cargo.toml index e8b54475b..ba8faeae3 100644 --- a/services/root-keys/Cargo.toml +++ b/services/root-keys/Cargo.toml @@ -8,11 +8,11 @@ description = "Xous root keys server" # Dependency versions enforced by Cargo.lock. [dependencies] xous = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } log = "0.4.14" -susres = {package = "xous-api-susres", version = "0.9.35"} +susres = {package = "xous-api-susres", version = "0.9.36"} trng = {path= "../trng"} spinor = {path="../spinor"} llio = {path="../llio"} diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index fb3163e0e..05de565e0 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -16,13 +16,13 @@ ime-plugin-shell = {path = "../ime-plugin-shell"} ime-plugin-tts = {path = "../ime-plugin-tts"} llio = {path = "../llio"} log = "0.4.14" -log-server = {package = "xous-api-log", version = "0.1.35"} -ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.35"} +log-server = {package = "xous-api-log", version = "0.1.36"} +ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.36"} xous = "0.9.40" xous-ipc = "0.9.40" -xous-names = {package = "xous-api-names", version = "0.9.37"} +xous-names = {package = "xous-api-names", version = "0.9.38"} keyboard = {path = "../keyboard"} -susres = {package = "xous-api-susres", version = "0.9.35"} +susres = {package = "xous-api-susres", version = "0.9.36"} codec = {path = "../codec"} #engine-sha512 = {path="../engine-sha512"} sha2 = {path="../engine-sha512"} diff --git a/services/shellchat/src/cmds/usb.rs b/services/shellchat/src/cmds/usb.rs index e5e7135e7..a1c823c6b 100644 --- a/services/shellchat/src/cmds/usb.rs +++ b/services/shellchat/src/cmds/usb.rs @@ -22,7 +22,7 @@ impl<'a> ShellCmdApi<'a> for Usb { #[cfg(not(feature="mass-storage"))] let helpstring = "usb [hid] [fido] [debug] [send ] [status] [leds] [lock] [unlock] [kbdtest]"; #[cfg(feature="mass-storage")] - let helpstring = "usb [hid] [fido] [ms] [debug] [send ] [status] [leds] [lock] [unlock] [kbdtest]"; + let helpstring = "usb [hid] [fido] [ms] [debug] [send ] [status] [leds] [lock] [unlock] [kbdtest] [console] [noconsole]"; let mut tokens = args.as_str().unwrap().split(' '); @@ -45,9 +45,46 @@ impl<'a> ShellCmdApi<'a> for Usb { self.usb_dev.debug_usb(Some(false)).unwrap(); write!(ret, "USB connected to Debug core, secrets readable!").unwrap(); } + "serial" => { + self.usb_dev.ensure_core(usb_device_xous::UsbDeviceType::Serial).unwrap(); + write!(ret, "USB connected to serial core").unwrap(); + } + "console" => { + self.usb_dev.ensure_core(usb_device_xous::UsbDeviceType::Serial).unwrap(); + // this will enable input injection mode + self.usb_dev.serial_console_input_injection(); + write!(ret, "USB console connected.").ok(); + } + "noconsole" => { + // this will disable any hooks (including the console input hook) + self.usb_dev.serial_clear_input_hooks(); + write!(ret, "USB console disconnected.").ok(); + } + "trng" => { + self.usb_dev.ensure_core(usb_device_xous::UsbDeviceType::Serial).unwrap(); + let mode = if let Some(sub_cmd) = tokens.next() { + match sub_cmd { + "ro" => trng::api::TrngTestMode::Ro, + "av" => trng::api::TrngTestMode::Av, + "both" => trng::api::TrngTestMode::Both, + "cprng" => trng::api::TrngTestMode::Cprng, + _ => trng::api::TrngTestMode::Cprng, // this is the "normal" mode of operation + } + } else { + // "normal" mode of operation: Ro + Av fed into CPRNG and then used + trng::api::TrngTestMode::Cprng + }; + self.usb_dev.serial_set_trng_mode(mode); + write!(ret, "USB TRNG serial sending requested").ok(); + } + "notrng" => { + self.usb_dev.serial_clear_input_hooks(); + write!(ret, "USB TRNG serial sending should be stopped.").ok(); + } "send" => { match self.usb_dev.get_current_core() { - Ok(UsbDeviceType::FidoKbd) => { + Ok(UsbDeviceType::FidoKbd) + | Ok(UsbDeviceType::Serial) => { let mut val = String::new(); join_tokens(&mut val, &mut tokens); match self.usb_dev.send_str(&val) { diff --git a/services/skeleton/Cargo.toml b/services/skeleton/Cargo.toml index a4e054d30..cd3f5931d 100644 --- a/services/skeleton/Cargo.toml +++ b/services/skeleton/Cargo.toml @@ -8,13 +8,13 @@ description = "Audio CODEC server" # Dependency versions enforced by Cargo.lock. [dependencies] xous = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } log = "0.4.14" num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} -susres = {package = "xous-api-susres", version = "0.9.35"} +susres = {package = "xous-api-susres", version = "0.9.36"} utralib = { version = "0.1.17", optional = true, default-features = false } diff --git a/services/spinor/Cargo.toml b/services/spinor/Cargo.toml index 5fdee4eff..a37da280e 100644 --- a/services/spinor/Cargo.toml +++ b/services/spinor/Cargo.toml @@ -8,11 +8,11 @@ description = "SPINOR ROM operations server" # Dependency versions enforced by Cargo.lock. [dependencies] xous = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } log = "0.4.14" -susres = {package = "xous-api-susres", version = "0.9.35"} +susres = {package = "xous-api-susres", version = "0.9.36"} trng = { path = "../trng" } com = { path = "../com" } llio = { path = "../llio" } diff --git a/services/status/Cargo.toml b/services/status/Cargo.toml index 6563db13c..f865b30c8 100644 --- a/services/status/Cargo.toml +++ b/services/status/Cargo.toml @@ -10,17 +10,17 @@ version = "0.1.0" com = {path = "../com"} content-plugin-api = {path = "../content-plugin-api"} log = "0.4.14" -log-server = {package = "xous-api-log", version = "0.1.35"} -ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.35"} +log-server = {package = "xous-api-log", version = "0.1.36"} +ticktimer-server = {package = "xous-api-ticktimer", version = "0.9.36"} trng = {path = "../trng"} llio = {path = "../llio"} xous = "0.9.40" xous-ipc = "0.9.40" -xous-names = {package = "xous-api-names", version = "0.9.37"} +xous-names = {package = "xous-api-names", version = "0.9.38"} graphics-server = {path = "../graphics-server"} gam = {path = "../gam"} locales = {path = "../../locales"} -susres = {package = "xous-api-susres", version = "0.9.35"} +susres = {package = "xous-api-susres", version = "0.9.36"} root-keys = {path = "../root-keys"} modals = {path = "../modals"} pddb = {path = "../pddb"} diff --git a/services/ticktimer-test-client/Cargo.toml b/services/ticktimer-test-client/Cargo.toml index 7822777cd..e903ef3f0 100644 --- a/services/ticktimer-test-client/Cargo.toml +++ b/services/ticktimer-test-client/Cargo.toml @@ -9,7 +9,7 @@ version = "0.1.0" [dependencies] xous = "0.9.40" log = "0.4.14" -log-server = {package = "xous-api-log", version = "0.1.35"} +log-server = {package = "xous-api-log", version = "0.1.36"} [features] default = [] diff --git a/services/trng/Cargo.toml b/services/trng/Cargo.toml index 46dc93e40..054c18519 100644 --- a/services/trng/Cargo.toml +++ b/services/trng/Cargo.toml @@ -8,13 +8,13 @@ description = "TRNG server" # Dependency versions enforced by Cargo.lock. [dependencies] xous = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } log = "0.4.14" num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} -susres = {package = "xous-api-susres", version = "0.9.35"} +susres = {package = "xous-api-susres", version = "0.9.36"} rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} xous-ipc = "0.9.40" rand_core = "0.5.1" # the 0.5.1 API is necessary for compatibility with curve25519-dalek crates @@ -29,8 +29,4 @@ precursor = ["utralib/precursor"] hosted = ["utralib/hosted"] renode = ["utralib/renode"] debugprint = [] -# note: these test routines are now deprecated, because the "Messible" has been removed from the SoC -avalanchetest = [] -ringosctest = [] -urandomtest = [] default = [] # "debugprint" diff --git a/services/trng/src/api.rs b/services/trng/src/api.rs index 52a3d24ce..bd60dcdfd 100644 --- a/services/trng/src/api.rs +++ b/services/trng/src/api.rs @@ -76,6 +76,13 @@ pub(crate) enum Opcode { ErrorStats = 6, Quit = 7, + + /// Set test mode to `RngTestMode`. System will best-effort provide "normal" TRNG data + /// but the TRNG will block while the generators are switched into test modes during buffer refills + TestSetMode = 8, + + /// Get test data. Fails (returns no data) if test mode was not previously set. + TestGetData = 9, } #[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] @@ -90,3 +97,27 @@ pub(crate) struct ScalarHook { pub id: u32, // ID of the scalar message to send through (e.g. the discriminant of the Enum on the caller's side API) pub cid: xous::CID, // caller-side connection ID for the scalar message to route to. Created by the caller before hooking. } + +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive, PartialEq, Eq, Copy, Clone)] +pub enum TrngTestMode { + // No test mode configured. + None, + // Avalanche data only + Av, + // Ring oscillator data only + Ro, + // Combined RO + AV data without any CPRNG conditioning + Both, + // Output of the CPRNG that is constantly re-seeded by RO + AV data. This is the "normal" mode of operation. + // The CPRNG serves as a "belt and suspenders" safety measure over raw RO + AV data, so that small drop-outs + // in the TRNG don't lead to disastrous consequences. Of course this also masks large failures, but there are + // online tests that should help to pick that up. + Cprng, +} + +pub const TRNG_TEST_BUF_LEN: usize = 2048; +#[derive(Debug, Copy, Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +pub struct TrngTestBuf { + pub data: [u8; TRNG_TEST_BUF_LEN], + pub len: u16, +} diff --git a/services/trng/src/lib.rs b/services/trng/src/lib.rs index 5d89e1a7c..010964d75 100644 --- a/services/trng/src/lib.rs +++ b/services/trng/src/lib.rs @@ -1,6 +1,7 @@ #![cfg_attr(target_os = "none", no_std)] pub mod api; +use api::{TrngTestMode, TRNG_TEST_BUF_LEN}; use num_traits::*; use xous::{send_message, CID}; use xous_ipc::Buffer; @@ -144,6 +145,41 @@ impl Trng { left.copy_from_slice(&chunk[..n]); } } + /// Sets the test mode according to the argument. Blocks until mode is set. + pub fn set_test_mode(&self, test_mode: TrngTestMode) { + send_message( + self.conn, + xous::Message::new_blocking_scalar( + api::Opcode::TestSetMode.to_usize().unwrap(), + test_mode.to_usize().unwrap(), + 0, + 0, + 0, + ), + ) + .expect("TRNG|LIB: can't set test mode"); + } + /// Gets test data from the TRNG. If hte test mode was not previously set, this will + /// eventually cause a panic. We don't add extra overhead code to make this safer + /// because as a test mode the caller expected to know what they are doing (and adding + /// more safety code increases overhead for the 99.9999999% of the time when we aren't + /// using this test code). + pub fn get_test_data(&self) -> Result<[u8; TRNG_TEST_BUF_LEN], xous::Error> { + let tb = api::TrngTestBuf { + data: [0; TRNG_TEST_BUF_LEN], + len: 0, + }; + let mut buf = Buffer::into_buf(tb).or(Err(xous::Error::InternalError))?; + buf.lend_mut(self.conn, api::Opcode::TestGetData.to_u32().unwrap()) + .or(Err(xous::Error::InternalError))?; + let rtb: api::TrngTestBuf = buf.to_original().unwrap(); + if rtb.len as usize != TRNG_TEST_BUF_LEN { + // we somehow couldn't get enough test data from the TRNG. + Err(xous::Error::OutOfMemory) + } else { + Ok(rtb.data) + } + } } impl RngCore for Trng { diff --git a/services/trng/src/main.rs b/services/trng/src/main.rs index e9d606651..c3e821a8a 100644 --- a/services/trng/src/main.rs +++ b/services/trng/src/main.rs @@ -10,6 +10,9 @@ use xous_ipc::Buffer; use log::info; +// when we are testing, allocate a fairly large chunk of data, because the overhead of switching and conditioning +// the TRNG into the test mode is fairly high +const TEST_CACHE_LEN: usize = 128 * 1024; #[derive(Copy, Clone, Debug)] struct ScalarCallback { server_to_cb_cid: CID, @@ -19,7 +22,8 @@ struct ScalarCallback { #[cfg(any(feature="precursor", feature="renode"))] mod implementation { - use crate::api::{ExcursionTest, HealthTests, MiniRunsTest, NistTests, TrngBuf, TrngErrors}; + use crate::api::{ExcursionTest, HealthTests, MiniRunsTest, NistTests, TrngBuf, TrngErrors, TrngTestMode, TRNG_TEST_BUF_LEN, TrngTestBuf}; + use crate::TEST_CACHE_LEN; use num_traits::*; use susres::{RegManager, RegOrField, SuspendResume}; use utralib::generated::*; @@ -30,6 +34,7 @@ mod implementation { conn: xous::CID, errors: TrngErrors, err_stat: HealthTests, + last_test_mode: TrngTestMode, } fn trng_handler(_irq_no: usize, arg: *mut usize) { @@ -129,55 +134,18 @@ mod implementation { pending_mask: 0, }, err_stat: HealthTests::default(), + last_test_mode: TrngTestMode::None, }; - ///// configure power settings and which generator to use - if !(cfg!(feature = "avalanchetest") || cfg!(feature = "ringosctest")) { - trng.csr.wo( - utra::trng_server::CONTROL, - trng.csr.ms(utra::trng_server::CONTROL_ENABLE, 1) - | trng.csr.ms(utra::trng_server::CONTROL_POWERSAVE, 1), - ); - log::trace!( - "TRNG configured for normal operation (av+ro): 0x{:08x}", - trng.csr.r(utra::trng_server::CONTROL) - ); - } else if cfg!(feature = "avalanchetest") && !cfg!(feature = "ringosctest") { - // avalanche test only - trng.csr.wo( - utra::trng_server::CONTROL, - trng.csr.ms(utra::trng_server::CONTROL_ENABLE, 1) - | trng.csr.ms(utra::trng_server::CONTROL_POWERSAVE, 1) // question: do we want power save on or off for testing?? let's leave it on for now, it will make the test run slower but maybe more accurate to our usual use case - | trng.csr.ms(utra::trng_server::CONTROL_RO_DIS, 1), // disable the RO to characterize only the AV - ); - log::info!( - "TRNG configured for avalanche testing: 0x{:08x}", - trng.csr.r(utra::trng_server::CONTROL) - ); - } else if !cfg!(feature = "avalanchetest") && cfg!(feature = "ringosctest") { - // ring osc test only - trng.csr.wo( - utra::trng_server::CONTROL, - trng.csr.ms(utra::trng_server::CONTROL_ENABLE, 1) - | trng.csr.ms(utra::trng_server::CONTROL_POWERSAVE, 1) - | trng.csr.ms(utra::trng_server::CONTROL_AV_DIS, 1), // disable the AV generator to characterize the RO - ); - log::info!( - "TRNG configured for ring oscillator testing: 0x{:08x}", - trng.csr.r(utra::trng_server::CONTROL) - ); - } else { - // both on - trng.csr.wo( - utra::trng_server::CONTROL, - trng.csr.ms(utra::trng_server::CONTROL_ENABLE, 1) - | trng.csr.ms(utra::trng_server::CONTROL_POWERSAVE, 1), - ); - log::info!( - "TRNG configured for both avalanche and ring oscillator testing: 0x{:08x}", - trng.csr.r(utra::trng_server::CONTROL) - ); - } + trng.csr.wo( + utra::trng_server::CONTROL, + trng.csr.ms(utra::trng_server::CONTROL_ENABLE, 1) + | trng.csr.ms(utra::trng_server::CONTROL_POWERSAVE, 1), + ); + log::trace!( + "TRNG configured for normal operation (av+ro): 0x{:08x}", + trng.csr.r(utra::trng_server::CONTROL) + ); trng.susres_manager .push(RegOrField::Reg(utra::trng_server::CONTROL), None); @@ -266,15 +234,6 @@ mod implementation { trng } - // for the test procedure - #[cfg(any( - feature = "avalanchetest", - feature = "ringosctest", - feature = "urandomtest" - ))] - pub fn get_trng_csr(&self) -> *mut u32 { - self.csr.base - } pub fn get_errors(&self) -> TrngErrors { self.errors @@ -461,6 +420,110 @@ mod implementation { // pump the engine to discard the initial 0's in the execution pipeline self.get_trng(2); } + + fn flush_trng_fifo(&mut self) { + // The hardware FIFO Is 1024 entries deep x 32 bits wide. This is a hard-coded parameter derived from the trng_managed.py source code (line 1014) + for _ in 0..1024 { + while self.csr.rf(utra::trng_server::STATUS_AVAIL) == 0 { + xous::yield_slice(); + } + let _ = self.csr.rf(utra::trng_server::DATA_DATA); + } + } + /// This routine will "always" return a full buffer; if it is not full, that should be considered an error condition. + pub fn get_trng_testmode(&mut self, test_data: &mut Vec::, test_mode: TrngTestMode) -> TrngTestBuf { + let mut retbuf = TrngTestBuf { + data: [0u8; TRNG_TEST_BUF_LEN], + len: 0 + }; + // bail if the test mode isn't set + if test_mode == TrngTestMode::None { + return retbuf; + } + if self.last_test_mode != test_mode { + // the test_data buffer is forfeit; clear it + test_data.clear(); + self.last_test_mode = test_mode; + } + if test_data.len() >= TRNG_TEST_BUF_LEN { + retbuf.data.copy_from_slice(test_data.drain(..TRNG_TEST_BUF_LEN).as_slice()); + retbuf.len = TRNG_TEST_BUF_LEN as u16; + retbuf + } else { + match test_mode { + TrngTestMode::Av => { + // avalanche test only + self.csr.wo( + utra::trng_server::CONTROL, + self.csr.ms(utra::trng_server::CONTROL_ENABLE, 1) + | self.csr.ms(utra::trng_server::CONTROL_POWERSAVE, 1) + | self.csr.ms(utra::trng_server::CONTROL_RO_DIS, 1), // disable the RO to characterize only the AV + ); + self.flush_trng_fifo(); + } + TrngTestMode::Ro => { + // ring osc test only + self.csr.wo( + utra::trng_server::CONTROL, + self.csr.ms(utra::trng_server::CONTROL_ENABLE, 1) + | self.csr.ms(utra::trng_server::CONTROL_POWERSAVE, 1) + | self.csr.ms(utra::trng_server::CONTROL_AV_DIS, 1), // disable the AV generator to characterize the RO + ); + self.flush_trng_fifo(); + } + TrngTestMode::Both | TrngTestMode::Cprng => { + assert!(self.csr.rf(utra::trng_server::CONTROL_RO_DIS) == 0, "TRNG test mode inconsistency (RO reads as disabled; expected it to be enabled)"); + assert!(self.csr.rf(utra::trng_server::CONTROL_AV_DIS) == 0, "TRNG test mode inconsistency (AV reads as disabled; expected it to be enabled)"); + } + TrngTestMode::None => { + panic!("TRNG test buffer request with no test mode set."); + } + } + + // now refill the test_data buffer + while test_data.len() < TEST_CACHE_LEN { + match test_mode { + TrngTestMode::Av | TrngTestMode::Ro | TrngTestMode::Both => { + while self.csr.rf(utra::trng_server::STATUS_AVAIL) == 0 { + xous::yield_slice(); + } + let word = self.csr.rf(utra::trng_server::DATA_DATA); + test_data.extend_from_slice(&word.to_le_bytes()); + } + TrngTestMode::Cprng => { + while self.csr.rf(utra::trng_server::URANDOM_VALID_URANDOM_VALID) == 0 { + xous::yield_slice(); + } + let word = self.csr.rf(utra::trng_server::URANDOM_URANDOM); + test_data.extend_from_slice(&word.to_le_bytes()); + } + TrngTestMode::None => { + unreachable!(); + } + } + } + + // restore the normal operation mode + match test_mode { + TrngTestMode::Av | TrngTestMode::Ro => { + // reinstate "normal" operation (both on) + self.csr.wo( + utra::trng_server::CONTROL, + self.csr.ms(utra::trng_server::CONTROL_ENABLE, 1) + | self.csr.ms(utra::trng_server::CONTROL_POWERSAVE, 1), + ); + self.flush_trng_fifo(); + } + // the raw TRNG data already matches the expected system mode; nothing to do + _ => {}, + } + + // copy the test data into a return structure + retbuf.data.copy_from_slice(test_data.drain(..TRNG_TEST_BUF_LEN).as_slice()); + retbuf.len = TRNG_TEST_BUF_LEN as u16; + retbuf + } + } } } @@ -470,7 +533,7 @@ mod implementation { use rand_chacha::ChaCha8Rng; use rand_chacha::rand_core::SeedableRng; use rand_chacha::rand_core::RngCore; - use crate::api::{HealthTests, TrngBuf, TrngErrors}; + use crate::api::{HealthTests, TrngBuf, TrngErrors, TRNG_TEST_BUF_LEN, TrngTestBuf, TrngTestMode}; pub struct Trng { rng: ChaCha8Rng, @@ -546,207 +609,13 @@ mod implementation { pub fn get_err_stats(&self) -> HealthTests { HealthTests::default() } - } -} - -#[cfg(any( - feature = "avalanchetest", - feature = "ringosctest", - feature = "urandomtest" -))] -pub const TRNG_BUFF_LEN: usize = 512 * 1024; -#[cfg(any( - feature = "avalanchetest", - feature = "ringosctest", - feature = "urandomtest" -))] -pub enum WhichMessible { - One, - Two, -} -#[cfg(any( - feature = "avalanchetest", - feature = "ringosctest", - feature = "urandomtest" -))] -struct Tester { - server_csr: utralib::CSR, - messible_csr: utralib::CSR, - messible2_csr: utralib::CSR, - buffer_a: xous::MemoryRange, - buffer_b: xous::MemoryRange, - ticktimer: ticktimer_server::Ticktimer, -} -#[cfg(any( - feature = "avalanchetest", - feature = "ringosctest", - feature = "urandomtest" -))] -impl Tester { - pub fn new(server_csr: *mut u32) -> Tester { - use utralib::generated::*; - let buff_a = xous::syscall::map_memory( - xous::MemoryAddress::new(0x4080_0000), // fix this at a known physical address - None, - crate::TRNG_BUFF_LEN, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't map TRNG comms buffer A"); - - let buff_b = xous::syscall::map_memory( - xous::MemoryAddress::new(0x4088_0000), // fix this at a known physical address - None, - crate::TRNG_BUFF_LEN, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't map TRNG comms buffer B"); - - let messible = xous::syscall::map_memory( - xous::MemoryAddress::new(utra::messible::HW_MESSIBLE_BASE), - None, - 4096, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't map messible"); - - let messible2 = xous::syscall::map_memory( - xous::MemoryAddress::new(utra::messible2::HW_MESSIBLE2_BASE), - None, - 4096, - xous::MemoryFlags::R | xous::MemoryFlags::W, - ) - .expect("couldn't map messible"); - - Tester { - server_csr: CSR::new(server_csr), - buffer_a: buff_a, - buffer_b: buff_b, - messible_csr: CSR::new(messible.as_mut_ptr() as *mut u32), - messible2_csr: CSR::new(messible2.as_mut_ptr() as *mut u32), - ticktimer: ticktimer_server::Ticktimer::new().unwrap(), - } - } - - pub fn messible_send(&mut self, which: WhichMessible, value: u8) { - use utralib::generated::*; - match which { - WhichMessible::One => self.messible_csr.wfo(utra::messible::IN_IN, value as u32), - WhichMessible::Two => self.messible2_csr.wfo(utra::messible2::IN_IN, value as u32), - } - } - #[allow(dead_code)] - pub fn messible_get(&mut self, which: WhichMessible) -> u8 { - use utralib::generated::*; - match which { - WhichMessible::One => self.messible_csr.rf(utra::messible::OUT_OUT) as u8, - WhichMessible::Two => self.messible2_csr.rf(utra::messible2::OUT_OUT) as u8, - } - } - pub fn messible_wait_get(&mut self, which: WhichMessible) -> u8 { - use utralib::generated::*; - match which { - WhichMessible::One => { - while self.messible_csr.rf(utra::messible::STATUS_HAVE) == 0 { - //xous::yield_slice(); - self.ticktimer.sleep_ms(10).unwrap(); - } - self.messible_csr.rf(utra::messible::OUT_OUT) as u8 - } - WhichMessible::Two => { - while self.messible2_csr.rf(utra::messible2::STATUS_HAVE) == 0 { - //xous::yield_slice(); - self.ticktimer.sleep_ms(10).unwrap(); - } - self.messible2_csr.rf(utra::messible2::OUT_OUT) as u8 + pub fn get_trng_testmode(&mut self, _test_data: &mut Vec::, _test_mode: TrngTestMode) -> TrngTestBuf { + TrngTestBuf { + data: [0u8; TRNG_TEST_BUF_LEN], + len: 0 } } } - - pub fn get_buff_a(&self) -> *mut u32 { - self.buffer_a.as_mut_ptr() as *mut u32 - } - pub fn get_buff_b(&self) -> *mut u32 { - self.buffer_b.as_mut_ptr() as *mut u32 - } - - pub fn get_data_eager(&self) -> u32 { - use utralib::generated::*; - if cfg!(feature = "avalanchetest") || cfg!(feature = "ringosctest") { - while self.server_csr.rf(utra::trng_server::STATUS_AVAIL) == 0 { - xous::yield_slice(); - } - self.server_csr.rf(utra::trng_server::DATA_DATA) - } else { - while self - .server_csr - .rf(utra::trng_server::URANDOM_VALID_URANDOM_VALID) - == 0 - {} - self.server_csr.rf(utra::trng_server::URANDOM_URANDOM) - } - } - #[allow(dead_code)] - pub fn wait_full(&self) { - use utralib::generated::*; - while self.server_csr.rf(utra::trng_server::STATUS_FULL) == 0 { - xous::yield_slice(); - } - } -} -#[cfg(any( - feature = "avalanchetest", - feature = "ringosctest", - feature = "urandomtest" -))] -fn tester_thread(csr: usize) { - let mut trng = Tester::new(csr as *mut u32); - - // just create buffers out of pointers. Definitely. Unsafe. - let buff_a = trng.get_buff_a() as *mut u32; - let buff_b = trng.get_buff_b() as *mut u32; - - for i in 0..TRNG_BUFF_LEN / 4 { - // buff_a[i] = trng.get_data_eager(); - unsafe { buff_a.add(i).write_volatile(trng.get_data_eager()) }; - } - for i in 0..TRNG_BUFF_LEN / 4 { - // buff_b[i] = trng.get_data_eager(); - unsafe { buff_b.add(i).write_volatile(trng.get_data_eager()) }; - } - log::info!("TRNG_TESTER: starting service"); - // confirm that the config flags work as embedded in get_data_eager - if cfg!(feature = "avalanchetest") || cfg!(feature = "ringosctest") { - log::info!("TRNG_TESTER: using raw data sources"); - if cfg!(feature = "avalanchetest") { - log::info!("TRNG_TESTER: avalanche enabled"); - } - if cfg!(feature = "ringosctest") { - log::info!("TRNG_TESTER: ring oscillator enabled"); - } - } else { - log::info!("TRNG_TESTER: using urandom data sources"); - } - let mut phase = 1; - trng.messible_send(WhichMessible::One, phase); // indicate buffer A is ready to go - - loop { - trng.messible_wait_get(WhichMessible::Two); - if phase % 2 == 0 { - log::info!("TRNG_TESTER: filling A"); - for i in 0..TRNG_BUFF_LEN / 4 { - //buff_a[i] = trng.get_data_eager(); - unsafe { buff_a.add(i).write_volatile(trng.get_data_eager()) }; - } - } else { - log::info!("TRNG_TESTER: filling B"); - for i in 0..TRNG_BUFF_LEN / 4 { - //buff_b[i] = trng.get_data_eager(); - unsafe { buff_b.add(i).write_volatile(trng.get_data_eager()) }; - } - } - phase += 1; - trng.messible_send(WhichMessible::One, phase); - } } fn main() -> ! { @@ -792,6 +661,11 @@ fn main() -> ! { .expect("couldn't create suspend/resume object"); let mut error_cb_conns: [Option; 32] = [None; 32]; + + // storage for testing data. We heap-allocate it, so that it's not a burden when we aren't testing the TRNG + let mut test_data = Vec::::new(); + let mut test_mode = TrngTestMode::None; + loop { let mut msg = xous::receive_message(trng_sid).unwrap(); let op: Option = FromPrimitive::from_usize(msg.body.id()); @@ -842,6 +716,36 @@ fn main() -> ! { let len = buffer.as_flat::().unwrap().len; buffer.replace(trng.get_buf(len)).unwrap(); } + Some(api::Opcode::TestSetMode) => xous::msg_blocking_scalar_unpack!(msg, mode_code, _, _, _, { + if let Some(mode) = FromPrimitive::from_usize(mode_code) { + test_mode = mode; + match mode { + TrngTestMode::None => { + // de-allocate the test memory + test_data.clear(); + test_data.shrink_to(0); + } + _ => { + // pre-allocate the expected test memory, to avoid thrashing the heap + if test_data.capacity() < TEST_CACHE_LEN { + test_data.reserve(TEST_CACHE_LEN - test_data.len()); + } + } + } + } else { + test_mode = TrngTestMode::None; + // de-allocate the test memory + test_data.clear(); + test_data.shrink_to(0); + } + xous::return_scalar(msg.sender, 1).ok(); + }), + Some(api::Opcode::TestGetData) => { + let mut buffer = unsafe { + Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) + }; + buffer.replace(trng.get_trng_testmode(&mut test_data, test_mode)).unwrap(); + } Some(api::Opcode::Quit) => break, None => { log::error!("couldn't convert opcode, ignoring"); diff --git a/services/tts/Cargo.toml b/services/tts/Cargo.toml index 8f7c42a3b..c0fafcb38 100644 --- a/services/tts/Cargo.toml +++ b/services/tts/Cargo.toml @@ -9,9 +9,9 @@ description = "Text to speech integration server" [dependencies] xous = "0.9.40" xous-ipc = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } log = "0.4.14" num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} diff --git a/services/usb-device-xous/Cargo.toml b/services/usb-device-xous/Cargo.toml index c9d8c86eb..d7314fccc 100644 --- a/services/usb-device-xous/Cargo.toml +++ b/services/usb-device-xous/Cargo.toml @@ -9,21 +9,22 @@ description = "Xous USB device drivers" [dependencies] xous = "0.9.40" xous-ipc = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } log = "0.4.14" llio = {path="../llio"} num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} -susres = {package = "xous-api-susres", version = "0.9.35"} +susres = {package = "xous-api-susres", version = "0.9.36"} modals = {path = "../modals"} -keyboard = {path = "../keyboard"} +keyboard = {path = "../keyboard", feature="inject-api"} bitfield = "0.13.2" vcell = "0.1.3" xous-semver = "0.1.2" utralib = { version = "0.1.17", optional = true, default-features = false } +trng = {path="../trng"} usbd-human-interface-device = "0.2.1" embedded-time = "0.12.1" # required by the keyboard interface @@ -34,6 +35,9 @@ num_enum = { version = "0.5", default-features = false } # used by the usbd-huma usbd_scsi = { path = "../../libs/mass-storage/usbd_scsi", optional = true } usbd_mass_storage = { path = "../../libs/mass-storage/usbd_mass_storage", optional = true } +# Serial support +usbd-serial = "0.1.1" + ghostfat = "0.5.0" [dependencies.usb-device] diff --git a/services/usb-device-xous/src/api.rs b/services/usb-device-xous/src/api.rs index dde347414..6bcaae4a5 100644 --- a/services/usb-device-xous/src/api.rs +++ b/services/usb-device-xous/src/api.rs @@ -1,53 +1,74 @@ +// Note: the log server relies on this name not changing in order to hook the serial port for logging output. +// changing this name shouldn't lead to a crash, but it will lead to the USB driver being undiscoverable by the log crate. pub(crate) const SERVER_NAME_USB_DEVICE: &'static str = "_Xous USB device driver_"; #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub(crate) enum Opcode { /// Returns the link status - LinkStatus, + LinkStatus = 0, /// Send a keyboard code - SendKeyCode, - /// "Type" a string to the keyboard - SendString, + SendKeyCode = 1, + /// "Type" a string to the keyboard. This API is relied upon by the log crate. + SendString = 2, /// Get the current LED state - GetLedState, + GetLedState = 3, /// Switch to a specified device core - SwitchCores, + SwitchCores = 4, /// Makes sure a given core is selected - EnsureCore, + EnsureCore = 5, /// Check which core is connected - WhichCore, + WhichCore = 6, /// Restrict the debug core - RestrictDebugAccess, + RestrictDebugAccess = 7, /// Retrieve restriction state - IsRestricted, + IsRestricted = 8, /// Set-and-check of USB debug restriction - DebugUsbOp, + DebugUsbOp = 9, /// Send a U2F message - U2fTx, + U2fTx = 128, /// Blocks the caller, waiting for a U2F message - U2fRxDeferred, + U2fRxDeferred = 129, /// A bump from the timeout process to check if U2fRx has timed out - U2fRxTimeout, + U2fRxTimeout = 130, /// Query if the HID driver was able to start - IsSocCompatible, + IsSocCompatible = 256, + + /// Hook serial ASCII listener + SerialHookAscii = 512, + /// Hook serial binary listener + SerialHookBinary = 513, + /// Flush any serial buffers + SerialFlush = 514, + /// Hook eager serial sender for TRNG output. This will not succeed if hooked for console mode already. + SerialHookTrngSender = 515, + /// Hook serial to the console input + SerialHookConsole = 516, + /// Clear any hooks + SerialClearHooks = 517, + /// TRNG send poll + SerialTrngPoll = 518, #[cfg(feature="mass-storage")] - SetBlockDevice, + SetBlockDevice = 1024, #[cfg(feature="mass-storage")] - SetBlockDeviceSID, + SetBlockDeviceSID = 1025, #[cfg(feature="mass-storage")] - ResetBlockDevice, + ResetBlockDevice = 1026, /// Handle the USB interrupt - UsbIrqHandler, + UsbIrqHandler = 2048, /// Suspend/resume callback - SuspendResume, + SuspendResume = 2049, /// Exits the server - Quit, + Quit = 4096, + + /// API used by the logging crate. The number is hard-coded; don't change it. + LogString = 8192, } +// The log crate depends on this API not changing. #[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] pub struct UsbString { pub s: xous_ipc::String::<4000>, @@ -83,6 +104,7 @@ pub enum UsbDeviceType { Fido = 2, #[cfg(feature="mass-storage")] MassStorage = 3, + Serial = 4, } use std::convert::TryFrom; impl TryFrom for UsbDeviceType { @@ -95,7 +117,22 @@ impl TryFrom for UsbDeviceType { 2 => Ok(UsbDeviceType::Fido), #[cfg(feature="mass-storage")] 3 => Ok(UsbDeviceType::MassStorage), + 4 => Ok(UsbDeviceType::Serial), _ => Err("Invalid UsbDeviceType specifier"), } } -} \ No newline at end of file +} + +pub const SERIAL_ASCII_BUFLEN: usize = 512; +pub const SERIAL_BINARY_BUFLEN: usize = 128; +#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] +pub struct UsbSerialAscii { + pub s: xous_ipc::String::, + pub delimiter: Option, +} + +#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] +pub struct UsbSerialBinary { + pub d: [u8; SERIAL_BINARY_BUFLEN], + pub len: usize, +} diff --git a/services/usb-device-xous/src/lib.rs b/services/usb-device-xous/src/lib.rs index aa3103fd9..977a421b0 100644 --- a/services/usb-device-xous/src/lib.rs +++ b/services/usb-device-xous/src/lib.rs @@ -2,6 +2,7 @@ pub mod api; pub use api::*; +use trng::api::TrngTestMode; use xous::{CID, send_message, Message}; use num_traits::*; pub use usb_device::device::UsbDeviceState; @@ -131,6 +132,9 @@ impl UsbHid { 0 => Ok(UsbDeviceType::Debug), 1 => Ok(UsbDeviceType::FidoKbd), 2 => Ok(UsbDeviceType::Fido), + #[cfg(feature="mass-storage")] + 3 => Ok(UsbDeviceType::MassStorage), + 4 => Ok(UsbDeviceType::Serial), _ => Err(xous::Error::InternalError) } } @@ -239,6 +243,8 @@ impl UsbHid { _ => Err(xous::Error::UseBeforeInit), } } + /// This will attempt to send a string using an API based on the currently connected device + /// If it's a Keyboard, it will "type" it; if it's a UART, it will just blast it out the Tx. pub fn send_str(&self, s: &str) -> Result { let serializer = UsbString { s: xous_ipc::String::<4000>::from_str(s), @@ -335,6 +341,69 @@ impl UsbHid { _ => Err(xous::Error::InternalError), } } + /// Blocks until an ASCII string terminated by `delimiter` is received on serial; if `None`, it + /// will return as soon as a character (or series of characters) have been received (thus the return + /// `String` will be piecemeal) + pub fn serial_wait_ascii(&self, delimiter: Option) -> String { + let req = UsbSerialAscii { + s: xous_ipc::String::new(), + delimiter + }; + let mut buf = Buffer::into_buf(req).or(Err(xous::Error::InternalError)).expect("Internal error"); + buf.lend_mut(self.conn, Opcode::SerialHookAscii.to_u32().unwrap()).or(Err(xous::Error::InternalError)).expect("Internal error"); + let resp = buf.to_original::().unwrap(); + resp.s.to_str().to_string() + } + /// Blocks until enough binary data has been received to fill the buffer + /// Another thread can be used to call serial_flush() if we don't want to + /// block forever and we're receiving small amounts of binary data. + pub fn serial_wait_binary(&self) -> Vec:: { + let req = UsbSerialBinary { + d: [0u8; SERIAL_BINARY_BUFLEN], + len: 0, + }; + let mut buf = Buffer::into_buf(req).or(Err(xous::Error::InternalError)).expect("Internal error"); + buf.lend_mut(self.conn, Opcode::SerialHookBinary.to_u32().unwrap()).or(Err(xous::Error::InternalError)).expect("Internal error"); + let resp = buf.to_original::().unwrap(); + resp.d[..resp.len].to_vec() + } + /// Non-blocking call that issues a serial flush command to the USB stack + pub fn serial_flush(&self) -> Result<(), xous::Error> { + send_message( + self.conn, + Message::new_scalar( + Opcode::SerialFlush.to_usize().unwrap(), + 0, 0, 0, 0 + ) + ).map(|_| ()) + } + /// Inject serial input over USB to the debug console. Dangerous! + /// This will also override/discard any existing hooked listeners. + pub fn serial_console_input_injection(&self) { + send_message( + self.conn, + Message::new_scalar( + Opcode::SerialHookConsole.to_usize().unwrap(), 0, 0, 0, 0 + ) + ).unwrap(); + } + pub fn serial_clear_input_hooks(&self) { + send_message( + self.conn, + Message::new_scalar( + Opcode::SerialClearHooks.to_usize().unwrap(), 0, 0, 0, 0 + ) + ).unwrap(); + } + /// Tries to set the serial port in TRNG mode. Will silently fail if already in console mode. + pub fn serial_set_trng_mode(&self, mode: TrngTestMode) { + send_message( + self.conn, + Message::new_scalar( + Opcode::SerialHookTrngSender.to_usize().unwrap(), mode.to_usize().unwrap(), 0, 0, 0 + ) + ).unwrap(); + } } use core::sync::atomic::{AtomicU32, Ordering}; diff --git a/services/usb-device-xous/src/main_hosted.rs b/services/usb-device-xous/src/main_hosted.rs index 4968fc6dd..2038c4b6e 100644 --- a/services/usb-device-xous/src/main_hosted.rs +++ b/services/usb-device-xous/src/main_hosted.rs @@ -278,9 +278,7 @@ pub(crate) fn main_hosted() -> ! { log::warn!("Quit received, goodbye world!"); break; }, - None => { - log::error!("couldn't convert opcode: {:?}", msg); - } + _ => log::warn!("Opcode not supported: {:?}", msg), } } // clean up our program diff --git a/services/usb-device-xous/src/main_hw.rs b/services/usb-device-xous/src/main_hw.rs index 31e3df9f7..aa0d6fbef 100644 --- a/services/usb-device-xous/src/main_hw.rs +++ b/services/usb-device-xous/src/main_hw.rs @@ -27,6 +27,8 @@ use keyboard::KeyMap; use xous_ipc::Buffer; use std::collections::VecDeque; +use usbd_serial::SerialPort; + pub struct EmbeddedClock { start: std::time::Instant, } @@ -55,6 +57,7 @@ enum Views { FidoOnly = 1, #[cfg(feature="mass-storage")] MassStorage = 2, + Serial = 3, } #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] @@ -64,6 +67,29 @@ enum TimeoutOp { Quit, } +#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] +enum TrngOp { + Pump, + InvalidCall, + Quit, +} + +#[derive(Debug)] +enum SerialListenMode { + // this just causes data incoming to be printed to the debug log; it is the default + NoListener, + // this assumes there will be a CR/LF character to delimit lines (the `char` arg), and + // will buffer data until two conditions are met: 1) a listener is hooked and 2) a CR/LF is received. + // This will "infinitely" buffer incoming characters if no listener is hooked. + AsciiListener(Option), + // this will simply buffer the data until the `usize` argument is met and passes it back to + // hooked listener. If this mode is set and there is no listener, it will buffer data "indefinitely" + // (e.g. until local heap is exhausted and the system panics) + BinaryListener, + // this will take any serial input and pass it on as if one was typing at the console + ConsoleListener, +} + pub(crate) fn main_hw() -> ! { log_server::init_wait().unwrap(); log::set_max_level(log::LevelFilter::Info); @@ -174,6 +200,7 @@ pub(crate) fn main_hw() -> ! { // do the same thing for mass storage #[cfg(feature="mass-storage")] let ums_dev = SpinalUsbDevice::new(usbdev_sid, usb.clone(), csr.clone()); + let serial_dev = SpinalUsbDevice::new(usbdev_sid, usb.clone(), csr.clone()); // track which view is visible on the device core #[cfg(not(feature="minimal"))] @@ -247,6 +274,32 @@ pub(crate) fn main_hw() -> ! { .max_power(500) .build(); + // Serial + const SERIAL_BUF_LEN: usize = 1024; // length of the internal character buffer. This is not the *hardware* buffer; this is a buffer we maintain in the driver to improve performance + let serial_alloc = UsbBusAllocator::new(serial_dev); + // this will create a default port with 128 bytes of backing store + let mut serial_port = SerialPort::new(&serial_alloc); + let mut serial_device = UsbDeviceBuilder::new(&serial_alloc, UsbVidPid(0x1209, 0x3613)) + .manufacturer("Kosagi") + .product("Precursor") + .serial_number(&serial_number) + .self_powered(false) + .max_power(500) + .build(); + let mut serial_listener: Option = None; + let mut serial_listen_mode: SerialListenMode = SerialListenMode::NoListener; + let mut serial_buf = Vec::::new(); + let mut serial_rx_trigger = false; // when true, the condition was met to pass data to the listener (but the listener was not yet installed) + let trng = trng::Trng::new(&xns).unwrap(); + let mut serial_trng_buf = Vec::::new(); + let serial_trng_interval = Arc::new(AtomicU32::new(0)); + let mut serial_trng_cid: Option:: = None; + const TRNG_PKT_SIZE: usize = 64; // size of a TRNG packet being sent. This is inferred from the spec. + const TRNG_INITIAL_DELAY_MS: u32 = 200; // the very first poll takes longer, because we have to fill the TRNG back-end + const TRNG_REFILL_DELAY_MS: u32 = 1; // we re-poll very fast once we see the host taking data + const TRNG_BACKOFF_MS: u32 = 1; + const TRNG_BACKOFF_MAX_MS: u32 = 1000; // cap on how far we backoff the polling rate + let mut led_state: KeyboardLedsReport = KeyboardLedsReport::default(); let mut fido_listener: Option = None; // under the theory that PIDs cannot be forged. TODO: check that PIDs cannot be forged. @@ -356,7 +409,7 @@ pub(crate) fn main_hw() -> ! { #[cfg(feature="mass-storage")] Some(Opcode::SetBlockDevice) => msg_blocking_scalar_unpack!(msg, read_id, write_id, max_lba_id, _, { xous::send_message( - abdcid, + abdcid, xous::Message::new_blocking_scalar( apps_block_device::BlockDeviceMgmtOp::SetOps.to_usize().unwrap(), read_id, write_id, max_lba_id, 0, ) @@ -407,6 +460,12 @@ pub(crate) fn main_hw() -> ! { _ => () }; } + Views::Serial => { + match serial_device.force_reset() { + Err(e) => log::warn!("USB reset on resume failed: {:?}", e), + _ => () + } + } } // resume2 brings us to our last application state usbmgmt.xous_resume2(); @@ -489,7 +548,8 @@ pub(crate) fn main_hw() -> ! { Views::FidoWithKbd => composite.interface::, _>(), Views::FidoOnly => fido_class.interface::, _>(), #[cfg(feature="mass-storage")] - Views::MassStorage => panic!("expected u2f tx when in mass storage mode!"), + Views::MassStorage => panic!("did not expect u2f tx when in mass storage mode!"), + Views::Serial => panic!("did not expect u2f tx while in serial mode!"), }; u2f.write_report(&u2f_msg).ok(); log::debug!("sent U2F packet {:x?}", u2f_ipc.data); @@ -529,7 +589,110 @@ pub(crate) fn main_hw() -> ! { log::debug!("ums device had something to do!") } None - } + }, + Views::Serial => { + if serial_device.poll(&mut [&mut serial_port]) { + let mut data = [0u8; SERIAL_BUF_LEN]; + match serial_listen_mode { + SerialListenMode::NoListener => { + match serial_port.read(&mut data) { + Ok(len) => { + match std::str::from_utf8(&data[..len]) { + Ok(s) => log::debug!("No listener ascii: {}", s), + Err(_) => { + log::debug!("No listener binary: {:x?}", &data[..len]); + } + } + } + Err(e) => { + log::debug!("No listener: {:?}", e); + } + } + } + SerialListenMode::ConsoleListener => { + match serial_port.read(&mut data) { + Ok(len) => { + match std::str::from_utf8(&data[..len]) { + Ok(s) => { + for c in s.chars() { + native_kbd.inject_key(c); + } + }, + Err(_) => { + log::info!("Non UTF-8 received on console: {:x?}", &data[..len]); + } + } + } + Err(e) => { + log::info!("Serial read error: {:?}", e); + } + } + } + SerialListenMode::AsciiListener(maybe_delimiter) => { + let readlen = serial_port.read(&mut data).unwrap_or(0); + if readlen == 0 { + continue; + } + if let Some(delimiter) = maybe_delimiter { + if !delimiter.is_ascii() { + log::warn!("Chosen ASCII delimiter {} is not ASCII. Serial receive will not function properly.", delimiter); + } + if !serial_rx_trigger { // once true, sticks as true + serial_rx_trigger = data[..readlen].iter().find(|&&c| c == (delimiter as u8)).is_some(); + } + } else { + serial_rx_trigger = true; + } + // append the incoming data to the main buffer + for &d in &data[..readlen] { + serial_buf.push(d); + } + // now see if we should pass it back to the listener (if it is hooked) + if serial_rx_trigger && serial_listener.is_some() { + let mut rx_msg = serial_listener.take().unwrap(); + let mut response = unsafe { + Buffer::from_memory_message_mut(rx_msg.body.memory_message_mut().unwrap()) + }; + let mut buf = response.to_original::().unwrap(); + use std::fmt::Write; // is this really the best way to do it? probably not. + write!(buf.s, "{}", std::string::String::from_utf8_lossy(&serial_buf)).ok(); + + response.replace(buf).unwrap(); + // the rx_msg will drop and respond to the listener + serial_rx_trigger = false; + } + } + SerialListenMode::BinaryListener => { + let readlen = serial_port.read(&mut data).unwrap_or(0); + if readlen == 0 { + continue; + } + // append the incoming data to the main buffer + for &d in &data[..readlen] { + serial_buf.push(d); + } + if serial_buf.len() >= SERIAL_BINARY_BUFLEN { + match serial_listener.take() { + Some(mut rx_msg) => { + let mut response = unsafe { + Buffer::from_memory_message_mut(rx_msg.body.memory_message_mut().unwrap()) + }; + let mut buf = response.to_original::().unwrap(); + buf.d.copy_from_slice(serial_buf.drain(..SERIAL_BINARY_BUFLEN).as_slice()); + buf.len = SERIAL_BINARY_BUFLEN; + response.replace(buf).unwrap(); + // the rx_msg will drop and respond to the listener + } + None => { + // do nothing, keep queuing data... + } + } + } + } + } + } + None + }, }; if let Some(u2f) = maybe_u2f { match u2f.read_report() { @@ -559,6 +722,7 @@ pub(crate) fn main_hw() -> ! { Views::FidoOnly => fido_dev.state() == UsbDeviceState::Suspend, #[cfg(feature="mass-storage")] Views::MassStorage => ums_device.state() == UsbDeviceState::Suspend, + Views::Serial => serial_device.state() == UsbDeviceState::Suspend, }; if is_suspend { log::info!("suspend detected"); @@ -582,6 +746,25 @@ pub(crate) fn main_hw() -> ! { }, // always triggers a reset when called Some(Opcode::SwitchCores) => msg_blocking_scalar_unpack!(msg, core, _, _, _, { + // ensure unhook the logger if it's connected to serial + let log_conn = xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap()).unwrap(); + // it is never harmful to double-unhook this + xous::send_message(log_conn, + xous::Message::new_blocking_scalar(log_server::api::Opcode::UnhookUsbMirror.to_usize().unwrap(), 0, 0, 0, 0) + ).ok(); + // reset any serial listeners that may have been set + serial_listen_mode = SerialListenMode::NoListener; + serial_listener.take(); + // shut down the TRNG sender if it's set + if let Some(trng_cid) = serial_trng_cid.take() { + serial_trng_interval.store(0, Ordering::SeqCst); + xous::send_message(trng_cid, + xous::Message::new_blocking_scalar(TrngOp::Quit.to_usize().unwrap(), + 0, 0, 0, 0) + ).ok(); + trng.set_test_mode(trng::api::TrngTestMode::None); + } + let devtype: UsbDeviceType = core.try_into().unwrap(); match devtype { UsbDeviceType::Debug => { @@ -634,12 +817,47 @@ pub(crate) fn main_hw() -> ! { } } } + UsbDeviceType::Serial => { + log::info!("Connecting device serial"); + match view { + Views::Serial => usbmgmt.connect_device_core(true), + _ => { + view = Views::Serial; + usbmgmt.ll_reset(true); + tt.sleep_ms(1000).ok(); + usbmgmt.ll_connect_device_core(true); + tt.sleep_ms(EXTENDED_CORE_RESET_MS).ok(); + usbmgmt.ll_reset(false); + } + } + } } xous::return_scalar(msg.sender, 0).unwrap(); }), // does not trigger a reset if we're already on the core Some(Opcode::EnsureCore) => msg_blocking_scalar_unpack!(msg, core, _, _, _, { let devtype: UsbDeviceType = core.try_into().unwrap(); + // if we are switching away from serial, unhook any possible listeners, and the logger + if view == Views::Serial && devtype != UsbDeviceType::Serial { + let log_conn = xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap()).unwrap(); + // it is never harmful to double-unhook this + xous::send_message(log_conn, + xous::Message::new_blocking_scalar(log_server::api::Opcode::UnhookUsbMirror.to_usize().unwrap(), 0, 0, 0, 0) + ).ok(); + // reset any serial listeners that may have been set + serial_listen_mode = SerialListenMode::NoListener; + serial_listener.take(); + // shut down the TRNG sender if it's set + if let Some(trng_cid) = serial_trng_cid.take() { + serial_trng_interval.store(0, Ordering::SeqCst); + xous::send_message(trng_cid, + xous::Message::new_blocking_scalar(TrngOp::Quit.to_usize().unwrap(), + 0, 0, 0, 0) + ).ok(); + trng.set_test_mode(trng::api::TrngTestMode::None); + } + } + match devtype { UsbDeviceType::Debug => { if usbmgmt.is_device_connected() { @@ -705,6 +923,22 @@ pub(crate) fn main_hw() -> ! { } } } + UsbDeviceType::Serial => { + log::info!("Ensuring serial device"); + if !usbmgmt.is_device_connected() { + view = Views::Serial; + usbmgmt.connect_device_core(true); + } else { + if view != Views::Serial { + view = Views::Serial; + usbmgmt.ll_reset(true); + tt.sleep_ms(1000).ok(); + usbmgmt.ll_connect_device_core(true); + tt.sleep_ms(EXTENDED_CORE_RESET_MS).ok(); + usbmgmt.ll_reset(false); + } + } + } } xous::return_scalar(msg.sender, 0).unwrap(); }), @@ -715,6 +949,7 @@ pub(crate) fn main_hw() -> ! { Views::FidoOnly => xous::return_scalar(msg.sender, UsbDeviceType::Fido as usize).unwrap(), #[cfg(feature="mass-storage")] Views::MassStorage => xous::return_scalar(msg.sender, UsbDeviceType::MassStorage as usize).unwrap(), + Views::Serial => xous::return_scalar(msg.sender, UsbDeviceType::Serial as usize).unwrap(), } } else { xous::return_scalar(msg.sender, UsbDeviceType::Debug as usize).unwrap(); @@ -770,6 +1005,7 @@ pub(crate) fn main_hw() -> ! { Views::FidoOnly => xous::return_scalar(msg.sender, fido_dev.state() as usize).unwrap(), #[cfg(feature="mass-storage")] Views::MassStorage => xous::return_scalar(msg.sender, ums_device.state() as usize).unwrap(), + Views::Serial => xous::return_scalar(msg.sender, serial_device.state() as usize).unwrap(), } }), Some(Opcode::SendKeyCode) => msg_blocking_scalar_unpack!(msg, code0, code1, code2, autoup, { @@ -806,6 +1042,38 @@ pub(crate) fn main_hw() -> ! { } } }), + Some(Opcode::LogString) => { + // the logger API is "best effort" only. Because retries and response codes can cause problems + // in the logger API, if anything goes wrong, we prefer to discard characters rather than get + // the whole subsystem stuck in some awful recursive error handling hell. + match view { + Views::Serial => { + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let usb_send = buffer.to_original::().unwrap(); + // this is implemented as a "blocking write": the routine will block until the data has all been written. + let send_data = usb_send.s.as_bytes(); + let to_send = usb_send.s.len(); + let mut sent = 0; + while sent < to_send { + match serial_port.write(&send_data[sent..to_send]) { + Ok(written) => { + sent += written; + } + Err(_) => { + // just drop characters + } + } + match serial_port.flush() { + Ok(_) => {}, + Err(_) => { + // just drop characters + } + } + } + } + _ => {} // do nothing; don't fail, don't report any error. + } + } Some(Opcode::SendString) => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut usb_send = buffer.to_original::().unwrap(); @@ -832,9 +1100,34 @@ pub(crate) fn main_hw() -> ! { sent += 1; } } + Views::Serial => { + // this is implemented as a "blocking write": the routine will block until the data has all been written. + let send_data = usb_send.s.as_bytes(); + let to_send = usb_send.s.len(); + // log::debug!("serial RTS: {:?}", serial_port.rts()); + // log::debug!("serial DTR: {:?}", serial_port.dtr()); + while sent < to_send { + match serial_port.write(&send_data[sent..to_send]) { + Ok(written) => { + sent += written; + } + Err(_) => { + log::warn!("Serial send is blocking. Delaying and trying again."); + tt.sleep_ms(100).ok(); + } + } + match serial_port.flush() { + Ok(_) => {}, + Err(_) => { + log::warn!("Serial port reported WouldBlock on flush"); + tt.sleep_ms(100).ok(); + } + } + } + } _ => {} // do nothing; will report that 0 characters were sent } - usb_send.sent = Some(sent); + usb_send.sent = Some(sent as _); buffer.replace(usb_send).unwrap(); } Some(Opcode::GetLedState) => msg_blocking_scalar_unpack!(msg, _, _, _, _, { @@ -842,6 +1135,264 @@ pub(crate) fn main_hw() -> ! { led_state.pack_to_slice(&mut code).unwrap(); xous::return_scalar(msg.sender, code[0] as usize).unwrap(); }), + Some(Opcode::SerialHookAscii) => { + let maybe_delimiter = { + let buffer = unsafe { + Buffer::from_memory_message(msg.body.memory_message().unwrap()) + }; + let data = buffer.to_original::().unwrap(); + data.delimiter + }; + serial_listen_mode = SerialListenMode::AsciiListener(maybe_delimiter); + serial_listener = Some(msg); + }, + Some(Opcode::SerialHookBinary) => { + serial_listen_mode = SerialListenMode::BinaryListener; + serial_listener = Some(msg); + }, + Some(Opcode::SerialHookConsole) => msg_scalar_unpack!(msg, _, _, _, _, { + let log_conn = xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap()).unwrap(); + match xous::send_message(log_conn, + xous::Message::new_blocking_scalar(log_server::api::Opcode::TryHookUsbMirror.to_usize().unwrap(), 0, 0, 0, 0) + ) { + Ok(xous::Result::Scalar1(result)) => { + if result == 1 { + serial_listen_mode = SerialListenMode::ConsoleListener; + // unhook any previous pending listener + serial_listener.take(); + } else { + log::error!("Error trying to connect USB console."); + } + } + _ => { + log::error!("Could not connect USB console"); + } + } + }), + Some(Opcode::SerialClearHooks) => { + let log_conn = xous::connect(xous::SID::from_bytes(b"xous-log-server ").unwrap()).unwrap(); + // it is never harmful to double-unhook this + xous::send_message(log_conn, + xous::Message::new_blocking_scalar(log_server::api::Opcode::UnhookUsbMirror.to_usize().unwrap(), 0, 0, 0, 0) + ).ok(); + + serial_listen_mode = SerialListenMode::NoListener; + serial_listener.take(); + // shut down the TRNG sender if it's set + if let Some(trng_cid) = serial_trng_cid.take() { + serial_trng_interval.store(0, Ordering::SeqCst); + xous::send_message(trng_cid, + xous::Message::new_blocking_scalar(TrngOp::Quit.to_usize().unwrap(), + 0, 0, 0, 0) + ).ok(); + trng.set_test_mode(trng::api::TrngTestMode::None); + } + } + Some(Opcode::SerialFlush) => msg_scalar_unpack!(msg, _, _, _, _, { + // this will hardware flush any pending items in usb_serial driver + serial_port.flush().ok(); + // this tries to return any data that's pending within the main loop's buffers + match serial_listen_mode { + SerialListenMode::BinaryListener => { + match serial_listener.take() { + Some(mut rx_msg) => { + let mut response = unsafe { + Buffer::from_memory_message_mut(rx_msg.body.memory_message_mut().unwrap()) + }; + let mut buf = response.to_original::().unwrap(); + let chars_avail = serial_buf.len().min(SERIAL_BINARY_BUFLEN); + buf.len = chars_avail; + buf.d.copy_from_slice(serial_buf.drain(..chars_avail).as_slice()); + response.replace(buf).unwrap(); + // the rx_msg will drop and respond to the listener + } + None => { + // do nothing, keep queuing data... + } + } + } + SerialListenMode::AsciiListener(_) => { + match serial_listener.take() { + Some(mut rx_msg) => { + let mut response = unsafe { + Buffer::from_memory_message_mut(rx_msg.body.memory_message_mut().unwrap()) + }; + let mut buf = response.to_original::().unwrap(); + use std::fmt::Write; // is this really the best way to do it? probably not. + write!(buf.s, "{}", std::string::String::from_utf8_lossy(&serial_buf)).ok(); + + response.replace(buf).unwrap(); + // the rx_msg will drop and respond to the listener + serial_rx_trigger = false; + } + None => {} // do nothing + } + } + _ => {} + } + }), + Some(Opcode::SerialHookTrngSender) => msg_scalar_unpack!(msg, trng_mode_code, _, _, _, { + if view != Views::Serial { + log::error!("USB is not in serial mode. Ignoring request to hook TRNG sender"); + continue; + } + match serial_listen_mode { + SerialListenMode::ConsoleListener => { + log::error!("Serial is already hooked as a console. Refusing to turn on TRNG source mode"); + continue; + } + SerialListenMode::NoListener => {}, + _ => { + log::warn!("Serial already has a listener {:?} attached, hooking TRNG at your risk!", serial_listen_mode); + } + } + + let trng_mode: trng::api::TrngTestMode = num_traits::FromPrimitive::from_usize(trng_mode_code) + .unwrap_or(trng::api::TrngTestMode::None); + if trng_mode == trng::api::TrngTestMode::None { + // ignore the call in case of a bad parameter + continue; + } else { + trng.set_test_mode(trng_mode); + } + log::info!("TRNG set to mode {:?}", trng_mode); + + // The strategy here is when this is called, we start a thread that polls at some + // interval in milliseconds to see if the Tx buffer is empty and + // if rts() is true; and if so, jams more binary data into the pipe. If rts() is not true + // or the buffer is not available to write, the interval backs off. + // + // It also takes a trng_mode argument which is used to select either CPRNG whitened output + // (which is the default mode), or direct raw data from the RO or the Avalanche generator; + // this is used to help characterize the raw entropy sources. + if serial_trng_cid.is_none() { + let trng_sid = xous::create_server().unwrap(); + let trng_cid = xous::connect(trng_sid).unwrap(); + std::thread::spawn({ + let serial_trng_interval = serial_trng_interval.clone(); + let main_conn = cid.clone(); + let trng_cid = trng_cid.clone(); + move || { + let tt = ticktimer_server::Ticktimer::new().unwrap(); + let mut msg_opt = None; + let mut return_type = 0; + serial_trng_interval.store(TRNG_INITIAL_DELAY_MS, Ordering::SeqCst); + log::info!("TRNG polling loop started"); + let mut debug_count = 0; + loop { + xous::reply_and_receive_next_legacy(trng_sid, &mut msg_opt, &mut return_type) + .unwrap(); + let msg = msg_opt.as_mut().unwrap(); + match num_traits::FromPrimitive::from_usize(msg.body.id()) + .unwrap_or(TrngOp::InvalidCall) + { + TrngOp::Pump => { + let next_interval = serial_trng_interval.load(Ordering::SeqCst); + if debug_count < 20 && next_interval < 100 { + log::debug!("TRNG serial poller, delay = {}", next_interval); + debug_count += 1; + } + if next_interval > 0 { + xous::try_send_message(main_conn, + xous::Message::new_scalar(Opcode::SerialTrngPoll.to_usize().unwrap(), + 0, 0, 0, 0) + ).ok(); + tt.sleep_ms(next_interval as _).ok(); + xous::try_send_message(trng_cid, + xous::Message::new_scalar(TrngOp::Pump.to_usize().unwrap(), + 0, 0, 0, 0) + ).ok(); + // reset debug_count so when the next trigger comes we can see the output + if next_interval > 100 { + debug_count = 0; + } + } else { + debug_count = 0; + } + } + TrngOp::Quit => { + if let Some(scalar) = msg.body.scalar_message_mut() { + scalar.id = 0; + scalar.arg1 = 1; + log::info!("Quit called to Trng helper thread"); + // I think there might be a bug in the kernel where quitting/disconnecting + // a reply_and_receive_next_legacy() loop is broken? + // This results in an inexplicable kernel hang... + // for now we can work around this by keeping the thread around once the server is started. + // break; + } + } + TrngOp::InvalidCall => { + log::error!("Unknown opcode received in TRNG source handler: {:?}", msg.body.id()); + } + } + } + /* + unsafe{xous::disconnect(trng_cid).ok()}; + xous::destroy_server(trng_sid).ok(); + log::info!("TRNG polling loop exited"); + */ + } + }); + serial_trng_cid = Some(trng_cid); + } + // kick off the polling thread + if let Some(trng_cid) = serial_trng_cid.as_ref() { + xous::try_send_message(*trng_cid, + xous::Message::new_scalar(TrngOp::Pump.to_usize().unwrap(), + 0, 0, 0, 0) + ).ok(); + } + }), + Some(Opcode::SerialTrngPoll) => { + if serial_trng_cid.is_none() { + // stale request from previously configured TRNG system + continue; + } + let mut sent = false; + if serial_port.rts() { + if serial_trng_buf.len() < TRNG_PKT_SIZE { + match trng.get_test_data() { + Ok(data) => { + serial_trng_buf.extend_from_slice(&data); + } + Err(e) => { + log::warn!("TRNG returned error while polling to refill: {:?}", e); + continue; + } + } + } + // at this point, we should have data we can copy to the buffer. Pull it from the end of the buffer + // so the Vec can efficiently de-allocate data. + match serial_port.flush() { + Ok(_) => { + let available = serial_trng_buf.len(); + match serial_port.write(&serial_trng_buf[available - TRNG_PKT_SIZE..available]) { + Ok(_) => { + serial_trng_buf.drain(available - TRNG_PKT_SIZE..available); + sent = true; + } + Err(_) => { + // do nothing, the host port is too full and can't take any more data + } + } + } + Err(_) => { + // do nothing, we're still sending older data + } + } + } + if !sent { + let prev_interval = serial_trng_interval.fetch_add(TRNG_BACKOFF_MS, Ordering::SeqCst); + // cap the backoff rate + if prev_interval > TRNG_BACKOFF_MAX_MS { + log::info!("Max backoff delay encountered"); + serial_trng_interval.store(TRNG_BACKOFF_MAX_MS, Ordering::SeqCst); + } + } else { + serial_trng_interval.store(TRNG_REFILL_DELAY_MS, Ordering::SeqCst); + } + } Some(Opcode::Quit) => { log::warn!("Quit received, goodbye world!"); break; diff --git a/services/usb-test/Cargo.toml b/services/usb-test/Cargo.toml index 7b701037a..dfa7409f7 100644 --- a/services/usb-test/Cargo.toml +++ b/services/usb-test/Cargo.toml @@ -8,13 +8,13 @@ description = "USB test & development stub" # Dependency versions enforced by Cargo.lock. [dependencies] xous = "0.9.40" -log-server = { package = "xous-api-log", version = "0.1.35" } -ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.35" } -xous-names = { package = "xous-api-names", version = "0.9.37" } +log-server = { package = "xous-api-log", version = "0.1.36" } +ticktimer-server = { package = "xous-api-ticktimer", version = "0.9.36" } +xous-names = { package = "xous-api-names", version = "0.9.38" } log = "0.4.14" num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} -susres = {package = "xous-api-susres", version = "0.9.35"} +susres = {package = "xous-api-susres", version = "0.9.36"} keyboard = {path = "../keyboard"} bitfield = "0.13.2" vcell = "0.1.3" diff --git a/services/xous-log/Cargo.toml b/services/xous-log/Cargo.toml index ebdcbb4f9..095ef9728 100644 --- a/services/xous-log/Cargo.toml +++ b/services/xous-log/Cargo.toml @@ -10,7 +10,7 @@ homepage = "https://betrusted.io/xous-book/" # Dependency versions enforced by Cargo.lock. [dependencies] -xous-api-log = {package = "xous-api-log", version = "0.1.35"} +xous-api-log = {package = "xous-api-log", version = "0.1.36"} xous = "0.9.40" xous-ipc = "0.9.40" log = "0.4.14" @@ -19,6 +19,9 @@ num-traits = {version = "0.2.14", default-features = false} utralib = {version = "0.1.17", optional = true, default-features = false } +# for USB serial logging +rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"], optional = true} + [target.'cfg(target_arch = "arm")'.dependencies] atsama5d27 = { git = "https://github.com/Foundation-Devices/atsama5d27.git", branch = "master", features = ["lcd-console"]} xous = { version = "0.9.40", features = ["v2p"] } # v2p feature is used when lcd-console feature is turned on @@ -32,6 +35,7 @@ atsama5d27 = ["utralib/atsama5d27"] lcd-console = [] debugprint = [] # adding this allocates the UART for debugging the logger logging = [] # adding this allocates the hardware UART for console interactions +usb = ["rkyv"] #default = [] -default = ["logging"] +default = ["logging", "usb"] # default = ["debugprint", "logging"] diff --git a/services/xous-log/src/main.rs b/services/xous-log/src/main.rs index 7153c51d7..8f24b3ffa 100644 --- a/services/xous-log/src/main.rs +++ b/services/xous-log/src/main.rs @@ -11,149 +11,37 @@ use num_traits::FromPrimitive; use platform::implementation; -fn handle_scalar( - output: &mut implementation::OutputWriter, - sender: xous::MessageSender, - msg: &xous::ScalarMessage, - sender_pid: xous::PID, -) { - match msg.id { - 1000 => writeln!(output, "PANIC in PID {}:", sender_pid).unwrap(), - 1100 => (), - 1101..=1132 => { - let mut output_bfr = [0u8; core::mem::size_of::() * 4]; - let output_iter = output_bfr.iter_mut(); - - // Combine the four arguments to form a single - // contiguous buffer. Note: The buffer size will change - // depending on the platfor's `usize` length. - let arg1_bytes = msg.arg1.to_le_bytes(); - let arg2_bytes = msg.arg2.to_le_bytes(); - let arg3_bytes = msg.arg3.to_le_bytes(); - let arg4_bytes = msg.arg4.to_le_bytes(); - let input_iter = arg1_bytes - .iter() - .chain(arg2_bytes.iter()) - .chain(arg3_bytes.iter()) - .chain(arg4_bytes.iter()); - for (dest, src) in output_iter.zip(input_iter) { - *dest = *src; - } - let total_chars = msg.id - 1100; - for (idx, c) in output_bfr.iter().enumerate() { - if idx >= total_chars { - break; - } - output.putc(*c); - } - } - 1200 => writeln!(output, "Terminating process").unwrap(), - 2000 => { - #[cfg(any(feature="precursor", feature="renode"))] - crate::platform::debug::DEFAULT.enable_rx(); - writeln!(output, "Resuming logger").unwrap(); - } - _ => writeln!( - output, - "Unrecognized scalar message from {}: {:#?}", - sender, msg - ) - .unwrap(), +/// A page-aligned stack allocation for connection requests (used by USB resolver) +#[cfg(feature="usb")] +#[repr(C, align(4096))] +struct ConnectRequest { + name: [u8; 64], + len: u32, + _padding: [u8; 4096 - 4 - 64], +} +#[cfg(feature="usb")] +impl Default for ConnectRequest { + fn default() -> Self { + ConnectRequest { name: [0u8; 64], len: 0, _padding: [0u8; 4096 - 4 - 64] } } } -fn handle_opcode( - output: &mut implementation::OutputWriter, - sender: xous::MessageSender, - opcode: api::Opcode, - message: &xous::Message, -) { - if let Some(mem) = message.memory_message() { - match opcode { - api::Opcode::LogRecord => { - // This transmute is safe because even if the resulting buffer is garbage, - // there are no invalid values in the resulting struct. - let lr = unsafe { &*(mem.buf.as_ptr() as *const api::LogRecord) }; - let level = if log::Level::Error as u32 == lr.level { - "ERR " - } else if log::Level::Warn as u32 == lr.level { - "WARN" - } else if log::Level::Info as u32 == lr.level { - "INFO" - } else if log::Level::Debug as u32 == lr.level { - "DBG " - } else if log::Level::Trace as u32 == lr.level { - "TRCE" - } else { - "UNKNOWN" - }; - if lr.file_length as usize > lr.file.len() { - return; - } - if lr.args_length as usize > lr.args.len() { - return; - } - if lr.module_length as usize > lr.module.len() { - return; - } +#[cfg(feature="usb")] +#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, Copy, Clone)] +pub struct UsbString { + pub s: xous_ipc::String::<4000>, + pub sent: Option, +} - let file_slice = &lr.file[0..lr.file_length as usize]; - - let args_slice = &lr.args[0..lr.args_length as usize]; - - let module_slice = &lr.module[0..lr.module_length as usize]; - - write!(output, "{}:", level).ok(); - for c in module_slice { - output.putc(*c); - } - write!(output, ": ").ok(); - for c in args_slice { - output.putc(*c); - } - - write!(output, " (").ok(); - for c in file_slice { - output.putc(*c); - } - if let Some(line) = lr.line { - write!(output, ":{}", line.get()).ok(); - } - writeln!(output, ")").ok(); - } - api::Opcode::StandardOutput | api::Opcode::StandardError => { - // let mut buffer_start_offset = mem.offset.map(|o| o.get()).unwrap_or(0); - let mut buffer_start_offset = 0; - let mut buffer_length = mem.valid.map(|v| v.get()).unwrap_or(mem.buf.len()); - - // Ensure that `buffer_start_offset` is within the range of `buffer`. - if buffer_start_offset >= mem.buf.len() { - buffer_start_offset = mem.buf.len() - 1; - } - - // Clamp the buffer length so that it fits within the buffer - if buffer_start_offset + buffer_length >= mem.buf.len() { - buffer_length = mem.buf.len() - buffer_start_offset; - } - - // Safe because we validated the offsets above - let buffer = unsafe { - core::slice::from_raw_parts( - mem.buf.as_ptr().add(buffer_start_offset), - buffer_length, - ) - }; - output.write_all(buffer).unwrap(); - // TODO: If the buffer is mutable, set `length` to 0. - } - _ => { - writeln!(output, "Unhandled opcode").unwrap(); - } - } - } else if let Some(scalar) = message.scalar_message() { - // Scalar message - handle_scalar(output, sender, scalar, sender.pid().unwrap()); - } +#[cfg(feature="usb")] +fn usb_send_str(conn: xous::CID, s: &str) { + let serializer = UsbString { + s: xous_ipc::String::<4000>::from_str(s), + sent: None + }; + let buf = xous_ipc::Buffer::into_buf(serializer).expect("usb error"); + // failures to send are silent & ignored; also, this API doesn't block. + buf.send(conn, 8192 /* LogString */).expect("usb error"); } fn reader_thread(arg: usize) { @@ -161,6 +49,11 @@ fn reader_thread(arg: usize) { writeln!(output, "LOG: Xous Logging Server starting up...").ok(); let server_addr = xous::create_server_with_address(b"xous-log-server ").expect("create server"); writeln!(output, "LOG: Server listening on address {:?}", server_addr).ok(); + #[cfg(feature="usb")] + let mut usb_serial: Option = None; + // use a stack-allocated string to ensure no heap thrashing results from String manipulations + #[cfg(feature="usb")] + let mut usb_str = xous_ipc::String::<4000>::new(); println!("LOG: my PID is {}", xous::process::id()); let mut counter: usize = 0; @@ -173,7 +66,233 @@ fn reader_thread(arg: usize) { let envelope = xous::syscall::receive_message(server_addr).expect("couldn't get address"); let sender = envelope.sender; if let Some(opcode) = FromPrimitive::from_usize(envelope.body.id()) { - handle_opcode(output, sender, opcode, &envelope.body); + if let Some(mem) = envelope.body.memory_message() { + match opcode { + api::Opcode::LogRecord => { + // This transmute is safe because even if the resulting buffer is garbage, + // there are no invalid values in the resulting struct. + let lr = unsafe { &*(mem.buf.as_ptr() as *const api::LogRecord) }; + let level = if log::Level::Error as u32 == lr.level { + "ERR " + } else if log::Level::Warn as u32 == lr.level { + "WARN" + } else if log::Level::Info as u32 == lr.level { + "INFO" + } else if log::Level::Debug as u32 == lr.level { + "DBG " + } else if log::Level::Trace as u32 == lr.level { + "TRCE" + } else { + "UNKNOWN" + }; + if lr.file_length as usize > lr.file.len() { + return; + } + if lr.args_length as usize > lr.args.len() { + return; + } + if lr.module_length as usize > lr.module.len() { + return; + } + + let file_slice = &lr.file[0..lr.file_length as usize]; + + let args_slice = &lr.args[0..lr.args_length as usize]; + + let module_slice = &lr.module[0..lr.module_length as usize]; + + write!(output, "{}:", level).ok(); + for c in module_slice { + output.putc(*c); + } + write!(output, ": ").ok(); + for c in args_slice { + output.putc(*c); + } + + write!(output, " (").ok(); + for c in file_slice { + output.putc(*c); + } + if let Some(line) = lr.line { + write!(output, ":{}", line.get()).ok(); + } + writeln!(output, ")").ok(); + #[cfg(feature="usb")] + if let Some(conn) = usb_serial { + if usb_str.len() > 0 { // test length so we aren't constantly copying 4096 bytes of 0's clearing an already cleared structure. + usb_str.clear(); + } + // duplicate the above code because doing repeated calls to the USB stack is inefficient + write!(usb_str, "{}:", level).ok(); + for c in module_slice { + usb_str.push_byte(*c).ok(); + } + write!(usb_str, ": ").ok(); + for c in args_slice { + usb_str.push_byte(*c).ok(); + } + write!(usb_str, " (").ok(); + for c in file_slice { + usb_str.push_byte(*c).ok(); + } + if let Some(line) = lr.line { + write!(usb_str, ":{}", line.get()).ok(); + } + writeln!(usb_str, ")").ok(); + usb_send_str(conn, usb_str.to_str()); + } + } + api::Opcode::StandardOutput | api::Opcode::StandardError => { + // let mut buffer_start_offset = mem.offset.map(|o| o.get()).unwrap_or(0); + let mut buffer_start_offset = 0; + let mut buffer_length = mem.valid.map(|v| v.get()).unwrap_or(mem.buf.len()); + + // Ensure that `buffer_start_offset` is within the range of `buffer`. + if buffer_start_offset >= mem.buf.len() { + buffer_start_offset = mem.buf.len() - 1; + } + + // Clamp the buffer length so that it fits within the buffer + if buffer_start_offset + buffer_length >= mem.buf.len() { + buffer_length = mem.buf.len() - buffer_start_offset; + } + + // Safe because we validated the offsets above + let buffer = unsafe { + core::slice::from_raw_parts( + mem.buf.as_ptr().add(buffer_start_offset), + buffer_length, + ) + }; + output.write_all(buffer).unwrap(); + // TODO: If the buffer is mutable, set `length` to 0. + + #[cfg(feature="usb")] + if let Some(conn) = usb_serial { + // safety: this routine will just blow up if you try to pass non utf-8 to it, + // so it's not very safe. On the other hand, it's fast and shame on you for sending + // non-utf8 to this API. + usb_send_str(conn, unsafe { std::str::from_utf8_unchecked(buffer) }); + } + } + _ => { + writeln!(output, "Unhandled opcode").unwrap(); + } + } + } else if let Some(scalar) = envelope.body.scalar_message() { + // Scalar message + let sender_pid = sender.pid().unwrap(); + match scalar.id { + 1000 => { + writeln!(output, "PANIC in PID {}:", sender_pid).unwrap(); + #[cfg(feature="usb")] + if let Some(conn) = usb_serial { + usb_send_str(conn, &format!("PANIC in PID {}:", sender_pid)); + } + }, + 1100 => (), + 1101..=1132 => { + let mut output_bfr = [0u8; core::mem::size_of::() * 4]; + let output_iter = output_bfr.iter_mut(); + + // Combine the four arguments to form a single + // contiguous buffer. Note: The buffer size will change + // depending on the platfor's `usize` length. + let arg1_bytes = scalar.arg1.to_le_bytes(); + let arg2_bytes = scalar.arg2.to_le_bytes(); + let arg3_bytes = scalar.arg3.to_le_bytes(); + let arg4_bytes = scalar.arg4.to_le_bytes(); + let input_iter = arg1_bytes + .iter() + .chain(arg2_bytes.iter()) + .chain(arg3_bytes.iter()) + .chain(arg4_bytes.iter()); + for (dest, src) in output_iter.zip(input_iter) { + *dest = *src; + } + let total_chars = scalar.id - 1100; + for (idx, c) in output_bfr.iter().enumerate() { + if idx >= total_chars { + break; + } + output.putc(*c); + } + #[cfg(feature="usb")] + // safety: this definitely blows up if you send illegal characters here. But if you're + // doing that, we really don't have any mechanism to handle that since this is the panic handler. + // Erring on the side of simplicity/"get any message out" versus correctness for this API. + if let Some(conn) = usb_serial { + usb_send_str(conn, unsafe{std::str::from_utf8_unchecked(&output_bfr[..total_chars])}); + } + } + 1200 => { + writeln!(output, "Terminating process").unwrap(); + #[cfg(feature="usb")] + if let Some(conn) = usb_serial { + usb_send_str(conn, "Terminating process"); + } + }, + 2000 => { + #[cfg(any(feature="precursor", feature="renode"))] + crate::platform::debug::DEFAULT.enable_rx(); + writeln!(output, "Resuming logger").unwrap(); + }, + #[cfg(feature="usb")] + 4 /* api::Opcode::TryHookUsbMirror */ => { + // The hook must be implemented with no dependencies (to avoid circular dependencies on crates). + // This this code is somewhat fragile as we copy in the API calls to these functions and assume they do not + // change. + let xns_conn = xous::connect(xous::SID::from_bytes(b"xous-name-server").unwrap()) + .expect("Couldn't connect to XousNames"); + let mut cr: ConnectRequest = Default::default(); + let name_bytes = b"_Xous USB device driver_"; + + // Set the string length to the length of the passed-in String, + // or the maximum possible length. Which ever is smaller. + cr.len = cr.name.len().min(name_bytes.len()) as u32; + + // Copy the string into our backing store. + for (&src_byte, dest_byte) in name_bytes.iter().zip(&mut cr.name) { + *dest_byte = src_byte; + } + let msg = xous::MemoryMessage { + id: 7 /* TryConnect */, + buf: unsafe{ // safety: `cr` is #[repr(C, align(4096))], and should be exactly on page in size + xous::MemoryRange::new(&mut cr as *mut _ as *mut u8 as usize, core::mem::size_of::()).unwrap() + }, + offset: None, + valid: xous::MemorySize::new(cr.len as usize), + }; + xous::send_message(xns_conn, xous::Message::MutableBorrow(msg)).unwrap(); + + let response_ptr = &cr as *const ConnectRequest as *const u32; + let result = unsafe { response_ptr.read() }; // safety: because that's how it was packed on the server, a naked u32 + + if result == 0 { + let cid = unsafe { response_ptr.add(1).read() }.into(); // safety: because that's how it was packed on the server + writeln!(output, "USB serial connection established, mirroring console output!").ok(); + usb_serial.replace(cid); + xous::return_scalar(envelope.sender, 1).ok(); + } else { + writeln!(output, "USB serial connection failed, console mirror not established").ok(); + xous::return_scalar(envelope.sender, 0).ok(); + } + }, + #[cfg(feature="usb")] + 5 /* api::Opcode::UnhookUsbMirror */ => { + // Note: this routine should be coded so that it is never harmful if unhook is called in an already unhooked state. + usb_serial.take(); + xous::return_scalar(envelope.sender, 1).ok(); + }, + _ => writeln!( + output, + "Unrecognized scalar message from {}: {:#?}", + sender, scalar + ) + .unwrap(), + } + } } else { writeln!( output, diff --git a/services/xous-names/Cargo.toml b/services/xous-names/Cargo.toml index 151a49b6e..4e26498a1 100644 --- a/services/xous-names/Cargo.toml +++ b/services/xous-names/Cargo.toml @@ -10,8 +10,8 @@ homepage = "https://betrusted.io/" # Dependency versions enforced by Cargo.lock. [dependencies] -xous-api-names = "0.9.37" -log-server = {package = "xous-api-log", version = "0.1.35"} +xous-api-names = "0.9.38" +log-server = {package = "xous-api-log", version = "0.1.36"} xous = "0.9.40" xous-ipc = "0.9.40" num-derive = {version = "0.3.3", default-features = false} diff --git a/services/xous-susres/Cargo.toml b/services/xous-susres/Cargo.toml index 31246268d..a0688a440 100644 --- a/services/xous-susres/Cargo.toml +++ b/services/xous-susres/Cargo.toml @@ -10,9 +10,9 @@ homepage = "https://betrusted.io/xous-book/" # Dependency versions enforced by Cargo.lock. [dependencies] -xous-api-susres = "0.9.35" -xous-names = { package = "xous-api-names", version = "0.9.37" } -log-server = { package = "xous-api-log", version = "0.1.35" } +xous-api-susres = "0.9.36" +xous-names = { package = "xous-api-names", version = "0.9.38" } +log-server = { package = "xous-api-log", version = "0.1.36" } xous = "0.9.40" xous-ipc = "0.9.40" log = "0.4.14" diff --git a/services/xous-ticktimer/Cargo.toml b/services/xous-ticktimer/Cargo.toml index 054af6b92..c2c4bed2a 100644 --- a/services/xous-ticktimer/Cargo.toml +++ b/services/xous-ticktimer/Cargo.toml @@ -10,12 +10,12 @@ homepage = "https://betrusted.io/xous-book/" # Dependency versions enforced by Cargo.lock. [dependencies] -xous-api-ticktimer = "0.9.35" +xous-api-ticktimer = "0.9.36" xous = "0.9.40" xous-ipc = "0.9.40" -xous-names = {package = "xous-api-names", version = "0.9.37"} -log-server = {package = "xous-api-log", version = "0.1.35"} -susres = {package = "xous-api-susres", version = "0.9.35"} +xous-names = {package = "xous-api-names", version = "0.9.38"} +log-server = {package = "xous-api-log", version = "0.1.36"} +susres = {package = "xous-api-susres", version = "0.9.36"} log = "0.4.14" rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} num-derive = {version = "0.3.3", default-features = false} diff --git a/xtask/src/verifier.rs b/xtask/src/verifier.rs index 8b6468439..984ed5963 100644 --- a/xtask/src/verifier.rs +++ b/xtask/src/verifier.rs @@ -14,10 +14,10 @@ pub fn check_project_consistency() -> Result<(), DynError> { // this set updates with kernel API changes "xous@0.9.40", "xous-ipc@0.9.40", - "xous-api-log@0.1.35", - "xous-api-names@0.9.37", - "xous-api-susres@0.9.35", - "xous-api-ticktimer@0.9.35", + "xous-api-log@0.1.36", + "xous-api-names@0.9.38", + "xous-api-susres@0.9.36", + "xous-api-ticktimer@0.9.36", // this set is only updated if the utralib changes "utralib@0.1.17", "svd2utra@0.1.15",