Skip to content

Commit

Permalink
Add KIP support (#37)
Browse files Browse the repository at this point in the history
* Add KIP support

* Sane flags default for ARM32 KIPs
  • Loading branch information
roblabla authored and Thomas Guillemard committed May 27, 2019
1 parent f87eee9 commit 8c92dcc
Show file tree
Hide file tree
Showing 8 changed files with 385 additions and 31 deletions.
77 changes: 48 additions & 29 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ aes = "0.3"
num-traits = "0.2"
derive_more = "0.13"
cmac = "0.2.0"
blz-nx = { git = "https://github.com/Thog/blz-nx-rs" }
bit_field = "0.10.0"

[features]
binaries = ["structopt", "cargo_metadata", "scroll", "goblin", "clap"]
23 changes: 23 additions & 0 deletions src/bin/linkle_clap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ enum Opt {
/// Sets the output file to use.
output_file: String,
},
/// Create a KIP file from an ELF and an NPDM file.
#[structopt(name = "kip")]
Kip {
/// Sets the input ELF file to use.
input_file: String,
/// Sets the input NPDM JSON file to use.
npdm_file: String,
/// Sets the output file to use.
output_file: String,
},
/// Create a PFS0 or NSP file from a directory.
#[structopt(name = "pfs0"/*, raw(alias = "nsp")*/)]
Pfs0 {
Expand Down Expand Up @@ -117,6 +127,18 @@ fn create_nxo(format: &str, input_file: &str, output_file: &str, icon_file: Opti
Ok(())
}

fn create_kip(input_file: &str, npdm_file: &str, output_file: &str) -> Result<(), linkle::error::Error> {
let mut nxo = linkle::format::nxo::NxoFile::from_elf(&input_file).map_err(|err| (err, &input_file))?;
let npdm = serde_json::from_reader(File::open(npdm_file).map_err(|err| (err, npdm_file))?)?;

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(())
}

fn create_pfs0(input_directory: &str, output_file: &str) -> Result<(), linkle::error::Error> {
let mut pfs0 = linkle::format::pfs0::Pfs0::from_directory(&input_directory)?;
let mut option = OpenOptions::new();
Expand Down Expand Up @@ -179,6 +201,7 @@ fn process_args(app: &Opt) {
let res = match app {
Opt::Nro { ref input_file, ref output_file, ref icon, ref romfs, ref nacp } => create_nxo("nro", input_file, output_file, to_opt_ref(icon), to_opt_ref(romfs), to_opt_ref(nacp)),
Opt::Nso { ref input_file, ref output_file } => create_nxo("nso", input_file, output_file, None, None, None),
Opt::Kip { ref input_file, ref npdm_file, ref output_file } => create_kip(input_file, npdm_file, output_file),
Opt::Pfs0 { ref input_directory, ref output_file } => create_pfs0(input_directory, output_file),
Opt::Pfs0Extract { ref input_file, ref output_directory } => extract_pfs0(input_file, output_directory),
Opt::Nacp { ref input_file, ref output_file } => create_nacp(input_file, output_file),
Expand Down
8 changes: 8 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use derive_more::Display;

#[derive(Debug, Fail, Display)]
pub enum Error {
#[display(fmt = "Failed to deserialize: {}", _0)]
Deserialization(#[cause] serde_json::error::Error),
#[display(fmt = "{}: {}", "_1.display()", _0)]
Io(#[cause] io::Error, PathBuf, Backtrace),
#[display(fmt = "Internal IO Error (please submit a bug report with the backtrace): {}", _0)]
Expand Down Expand Up @@ -76,6 +78,12 @@ impl From<BlockModeError> for Error {
}
}

impl From<serde_json::error::Error> for Error {
fn from(err: serde_json::error::Error) -> Error {
Error::Deserialization(err)
}
}

impl From<FromUtf8Error> for Error {
fn from(err: FromUtf8Error) -> Error {
// Why the heck does OsStr not have display()?
Expand Down
1 change: 1 addition & 0 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pub mod nacp;
pub mod nxo;
pub mod pfs0;
pub mod romfs;
mod npdm;
mod utils;
Loading

0 comments on commit 8c92dcc

Please sign in to comment.