Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandruradovici committed Apr 18, 2023
0 parents commit d212dc7
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "tockloader"
version = "0.1.0"
edition = "2021"

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

[dependencies]
clap = { version = "4.1.1", features = ["cargo"] }
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 The Tock Project Developers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
19 changes: 19 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This Makefile has been inspired by 'tock'
# Link to their Makefile here: https://github.com/tock/tock/blob/master/Makefile
# Naming scheme and conventions have been lifted from thier "Code Review" policy set.
# Reference: https://github.com/tock/tock/blob/master/doc/CodeReview.md#3-continuous-integration

.PHONY: ci-job-format
ci-job-format:
@echo "Checking formating of source files..."
@./tools/run_fmt_check.sh

.PHONY: ci-job-clippy
ci-job-clippy:
@echo "Running clippy on source files..."
@./tools/run_clippy.sh

.PHONY: ci-runner-github
ci-runner-github: ci-job-format ci-job-clippy
@echo "Running cargo check..."
@cargo check
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# ![TockLoader](http://www.tockos.org/assets/img/tockloader.svg#a "Tockloader Logo")

This is a work-in-progress port to Rust for Tock Loader.

Please use the original Python version of [TockLoader](https://www.github.com/tock/tockloader).

## Roadmap

This is a non exhaustive list of functionalities that should be
implemented to make TockLoader usable.

- [x] Setup the directory structure
- [x] Implement the command line arguments parser
- [ ] Implement the serial port listener
- [ ] Implement the tockloader serial protocol
- [ ] Implement the `openocd` transport interface
- [ ] Implement the `jlink` transport interface
- [ ] Implement the TBF Parser
72 changes: 72 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use clap::{arg, crate_version, Command};

/// Create the [command](clap::Command) object which will handle all of the command line arguments.
pub fn make_cli() -> Command {
Command::new("tockloader")
.about("This is a sample description.")
.version(crate_version!())
.arg_required_else_help(true)
.subcommands(get_subcommands())
.args([
arg!(--debug "Print additional debugging information").action(clap::ArgAction::SetTrue)
])
// Note: arg_require_else_help will trigger the help command if no argument/subcommand is given.
// This means that the --debug flag will not trigger the help menu, even if alone it does nothing.
}

/// Generate all of the [subcommands](clap::Command) used by the program.
fn get_subcommands() -> Vec<Command> {
vec![Command::new("listen")
.about("Open a terminal to receive UART data")
.args(get_app_args())
.args(get_channel_args())
.arg_required_else_help(true)]
}

/// Generate all of the [arguments](clap::Arg) that are required by subcommands which work with apps.
fn get_app_args() -> Vec<clap::Arg> {
vec![
arg!(-a --"app-address" <ADDRESS> "Address where apps are located"),
arg!(--force "Allow apps on boards that are not listed as compatible")
.action(clap::ArgAction::SetTrue),
arg!(--"bundle-apps" "Concatenate apps and flash all together, re-flashing apps as needed")
.action(clap::ArgAction::SetTrue),
]
// Note: the .action(clap::ArgAction::SetTrue) doesn't seem to be necessary, though in clap documentation it is used.
}

/// Generate all of the [arguments](clap::Arg) that are required by subcommands which work
/// with channels and computer-board communication.
fn get_channel_args() -> Vec<clap::Arg> {
vec![
arg!(-p --port "The serial port or device name to use"),
arg!(--serial "Use the serial bootloader to flash")
.action(clap::ArgAction::SetTrue),
arg!(--jlink "Use JLinkExe to flash")
.action(clap::ArgAction::SetTrue),
arg!(--openocd "Use OpenOCD to flash")
.action(clap::ArgAction::SetTrue),
arg!(--"jlink-device" <DEVICE> "The device type to pass to JLinkExe. Useful for initial commissioning.")
.default_value("cortex-m0"),
arg!(--"jlink-cmd" <CMD> "The JLinkExe binary to invoke"),
arg!(--"jlink-speed" <SPEED> "The JLink speed to pass to JLinkExe"),
arg!(--"jlink-if" <INTERFACE> "The interface type to pass to JLinkExe"),
arg!(--"openocd-board" <CFG_FILE> "The cfg file in OpenOCD `board` folder"),
arg!(--"openocd-cmd" <CMD> "The openocd binary to invoke")
.default_value("openocd"),
// These may not work out of the box
arg!(--"openocd-options" <OPTIONS> "Tockloader-specific flags to direct how Tockloader uses OpenOCD"),
arg!(--"openocd-commands" <CMDS> "Directly specify which OpenOCD commands to use for \"program\", \"read\", or \"erase\" actions"),
// -----
arg!(--"flash-file" "Operate on a binary flash file instead of a proper board")
.action(clap::ArgAction::SetTrue),
arg!(--board <BOARD> "Explicitly specify the board that is being targeted"),
arg!(--arch <ARCH> "Explicitly specify the architecture of the board that is being targeted"),
arg!(--"page-size" <SIZE> "Explicitly specify how many bytes in a flash page")
.default_value("0"),
arg!(--"baud-rate" <RATE> "If using serial, set the target baud rate")
.default_value("115200"),
arg!(--"no-bootloader-entry" "Tell Tockloader to assume the bootloader is already active")
.action(clap::ArgAction::SetTrue),
]
}
27 changes: 27 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
mod cli;
use cli::make_cli;

fn main() {
let matches = make_cli().get_matches();

if matches.get_flag("debug") {
println!("Debug mode enabled");
}

match matches.subcommand() {
Some(("listen", sub_matches)) => {
println!("Got the listen subcommand");
let default_adr = "NONE".to_string();
let adr = sub_matches
.get_one::<String>("app-address")
.unwrap_or(&default_adr);
println!("With App Address {adr}");
}
// If only the "--debug" flag is set, then this branch is executed
// Or, more likely at this stage, a subcommand hasn't been implemented yet.
_ => {
println!("Could not run the provided subcommand.");
_ = make_cli().print_help();
}
}
}
16 changes: 16 additions & 0 deletions tools/run_clippy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash
# Script (heavily) inspired by 'tock'
# Reference: https://github.com/tock/tock/blob/master/tools/run_clippy.sh

# Check to see if we can execute `cargo clippy`.
# We don't want to force an installation onto the user, so for we
# will only notify them of the issue.
if ! rustup component list | grep 'clippy.*(installed)' -q; then
echo "Could not check formatting with clippy, 'clippy' must be installed!"
exit 1
fi

# TODO: What arguments do we want to pass to clippy?
CLIPPY_ARGS="-D warnings"

cargo clippy -- $CLIPPY_ARGS
33 changes: 33 additions & 0 deletions tools/run_fmt_check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env bash
# Script (heavily) inspired by 'tock'
# Reference: https://github.com/tock/tock/blob/master/tools/run_cargo_fmt.sh

let FAIL=0

# Check to see if we can execute `cargo rust`.
# We don't want to force an installation onto the user, so for we
# will only notify them of the issue.
if ! rustup component list | grep 'rustfmt.*(installed)' -q; then
echo "Could not check formatting, 'rustfmt' must be installed!"
exit 1
fi

if ! cargo fmt -q -- --check; then
printf "<- Contains formatting errors!\n"
cargo fmt -- --check || let FAIL=FAIL+1
printf "\n"
fi

RUST_FILES_WITH_TABS="$(git grep --files-with-matches $'\t' -- '*.rs' || grep -lr --include '*.rs' $'\t' . || true)"
if [ "$RUST_FILES_WITH_TABS" != "" ]; then
echo "ERROR: The following files contain tab characters, please use spaces instead:"
echo "$RUST_FILES_WITH_TABS" | sed 's/^/ -> /'
let FAIL=FAIL+1
fi

if [[ $FAIL -ne 0 ]]; then
echo
echo "$(tput bold)$(tput setaf 1)Formatting errors.$(tput sgr0)"
echo "See above for details"
fi
exit $FAIL

0 comments on commit d212dc7

Please sign in to comment.