Skip to content

Commit

Permalink
Implement NPDM support
Browse files Browse the repository at this point in the history
  • Loading branch information
roblabla committed Aug 20, 2019
1 parent 8c92dcc commit 68e9fe5
Show file tree
Hide file tree
Showing 7 changed files with 405 additions and 14 deletions.
28 changes: 23 additions & 5 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ derive_more = "0.13"
cmac = "0.2.0"
blz-nx = { git = "https://github.com/Thog/blz-nx-rs" }
bit_field = "0.10.0"
bincode = "1.1.4"

[features]
binaries = ["structopt", "cargo_metadata", "scroll", "goblin", "clap"]
22 changes: 21 additions & 1 deletion src/bin/linkle_clap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ enum Opt {
/// Key file to use
#[structopt(parse(from_os_str), short = "k", long = "keyset")]
keyfile: Option<PathBuf>,
},
/// Create an NPDM from a JSON-NPDM formatted file.
#[structopt(name = "npdm")]
Npdm {
/// Sets the input JSON file to use.
#[structopt(parse(from_os_str))]
input_file: PathBuf,
/// Sets the output NPDM file to use.
#[structopt(parse(from_os_str))]
output_file: PathBuf,
}
}

Expand Down Expand Up @@ -134,7 +144,7 @@ fn create_kip(input_file: &str, npdm_file: &str, output_file: &str) -> Result<()
let mut option = OpenOptions::new();
let output_option = option.write(true).create(true).truncate(true);
output_option.open(output_file)?;

nxo.write_kip1(&mut output_option.open(output_file).map_err(|err| (err, output_file))?, &npdm).map_err(|err| (err, output_file))?;
Ok(())
}
Expand Down Expand Up @@ -193,6 +203,15 @@ fn print_keys(is_dev: bool, key_path: Option<&Path>) -> Result<(), linkle::error
Ok(())
}

fn create_npdm(input_file: &Path, output_file: &Path) -> Result<(), linkle::error::Error> {
let mut npdm = linkle::format::npdm::NpdmJson::from_file(&input_file)?;
let mut option = OpenOptions::new();
let output_option = option.write(true).create(true).truncate(true);
let mut out_file = output_option.open(output_file).map_err(|err| (err, output_file))?;
npdm.into_npdm(&mut out_file, false)?;
Ok(())
}

fn to_opt_ref<U: ?Sized, T: AsRef<U>>(s: &Option<T>) -> Option<&U> {
s.as_ref().map(AsRef::as_ref)
}
Expand All @@ -207,6 +226,7 @@ fn process_args(app: &Opt) {
Opt::Nacp { ref input_file, ref output_file } => create_nacp(input_file, output_file),
Opt::Romfs { ref input_directory, ref output_file } => create_romfs(input_directory, output_file),
Opt::Keygen { dev, ref keyfile } => print_keys(*dev, to_opt_ref(keyfile)),
Opt::Npdm { ref input_file, ref output_file } => create_npdm(input_file, output_file)
};

if let Err(e) = res {
Expand Down
11 changes: 11 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use failure::Backtrace;
use block_modes::BlockModeError;
use failure::Fail;
use derive_more::Display;
use std::borrow::Cow;

#[derive(Debug, Fail, Display)]
pub enum Error {
Expand All @@ -32,6 +33,10 @@ pub enum Error {
RomFsSymlink(PathBuf, Backtrace),
#[display(fmt = "Unknown file type at {}", "_0.display()")]
RomFsFiletype(PathBuf, Backtrace),
#[display(fmt = "Invalid NPDM value for field {}", "_0")]
InvalidNpdmValue(Cow<'static, str>, Backtrace),
#[display(fmt = "Failed to serialize NPDM.")]
BincodeError(#[cause] Box<bincode::ErrorKind>, Backtrace),
}

impl Error {
Expand Down Expand Up @@ -96,3 +101,9 @@ impl From<(usize, cmac::crypto_mac::MacError)> for Error {
Error::MacError(err, id, Backtrace::new())
}
}

impl From<Box<bincode::ErrorKind>> for Error {
fn from(err: Box<bincode::ErrorKind>) -> Error {
Error::BincodeError(err, Backtrace::new())
}
}
2 changes: 1 addition & 1 deletion src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ pub mod nacp;
pub mod nxo;
pub mod pfs0;
pub mod romfs;
mod npdm;
pub mod npdm;
mod utils;
Loading

0 comments on commit 68e9fe5

Please sign in to comment.