1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#![deny(clippy::all)]
#![feature(crate_visibility_modifier)]
#![feature(try_trait)]
#![feature(wrapping_int_impl)]
#![warn(rust_2018_idioms)]

mod cargo;
mod cli;
mod elf;
mod fs;
mod header;
mod ipl3;

use colored::Colorize;
use failure::Fail;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::process;
use std::time::Instant;

use crate::cargo::SubcommandError;
use crate::cli::{ArgParseError, Args, BuildArgs};
use crate::elf::ElfError;
use crate::fs::FSError;
use crate::header::{N64Header, HEADER_SIZE};
use crate::ipl3::{IPL_SIZE, PROGRAM_SIZE};

#[derive(Debug, Fail)]
pub enum RunError {
    #[fail(display = "Argument parsing error")]
    ArgParseError(#[cause] ArgParseError),

    #[fail(display = "Error running subcommand")]
    UnknownSubcommand,

    #[fail(display = "Build error")]
    BuildError(#[cause] BuildError),
}

impl From<ArgParseError> for RunError {
    fn from(e: ArgParseError) -> Self {
        RunError::ArgParseError(e)
    }
}

impl From<BuildError> for RunError {
    fn from(e: BuildError) -> Self {
        RunError::BuildError(e)
    }
}

#[derive(Debug, Fail)]
pub enum BuildError {
    #[fail(display = "Argument parsing error")]
    ArgParseError(#[cause] ArgParseError),

    #[fail(display = "Subcommand failed")]
    SubcommandError(#[cause] SubcommandError),

    #[fail(display = "Elf parsing error")]
    ElfError(#[cause] ElfError),

    #[fail(display = "Error while creating filesystem")]
    FSError(#[cause] FSError),

    #[fail(display = "Elf program is larger than 1MB")]
    ProgramTooBigError,

    #[fail(display = "Empty filename")]
    EmptyFilenameError,

    #[fail(display = "Filename encoding error")]
    FilenameEncodingError,

    #[fail(display = "Could not create file `{}`", _0)]
    CreateFileError(String),

    #[fail(display = "Could not write file `{}`", _0)]
    WriteFileError(String),
}

impl From<ArgParseError> for BuildError {
    fn from(e: ArgParseError) -> Self {
        BuildError::ArgParseError(e)
    }
}

impl From<SubcommandError> for BuildError {
    fn from(e: SubcommandError) -> Self {
        BuildError::SubcommandError(e)
    }
}

impl From<ElfError> for BuildError {
    fn from(e: ElfError) -> Self {
        BuildError::ElfError(e)
    }
}

impl From<FSError> for BuildError {
    fn from(e: FSError) -> Self {
        BuildError::FSError(e)
    }
}

fn print_backtrace(error: &dyn Fail) {
    if let Some(backtrace) = error.backtrace() {
        let backtrace = backtrace.to_string();
        if backtrace != "" {
            eprintln!("{}", backtrace);
        }
    }
}

pub fn handle_errors<F, R>(run: R)
where
    F: Fail,
    R: Fn() -> Result<(), F>,
{
    let start = Instant::now();

    match run() {
        Err(e) => {
            eprintln!("{} {}", "error:".red(), e);
            print_backtrace(&e);

            for cause in Fail::iter_causes(&e) {
                eprintln!("{} {}", "caused by:".bright_red(), cause);
                print_backtrace(cause);
            }

            process::exit(1);
        }
        Ok(()) => {
            eprintln!(
                "{:>12} nintendo64 target(s) in {}",
                "Finished".green().bold(),
                get_runtime(start)
            );
        }
    };
}

/// This is the entrypoint. It is responsible for parsing the cli args common to
/// all subcommands, and ultimately executing the requested subcommand.
pub fn run() -> Result<(), RunError> {
    use self::RunError::*;

    let args = cli::parse_args()?;
    match args.subcommand {
        cli::Subcommand::Build => build(args)?,
        _ => Err(UnknownSubcommand)?,
    }

    Ok(())
}

/// The build subcommand. Parses cli args specific to build, executes
/// `cargo xbuild`, and transforms the ELF to a ROM file.
fn build(args: Args) -> Result<(), BuildError> {
    use self::BuildError::*;

    let mut args = cli::parse_build_args(args)?;

    eprintln!("{:>12} with cargo xbuild", "Building".green().bold());
    let artifact = cargo::run(&args)?;

    // Set default program name
    if args.name.is_empty() {
        args.name = artifact.target.name;
    }
    let args = args;

    eprintln!("{:>12} ELF to binary", "Dumping".green().bold());
    let filename = artifact
        .filenames
        .first()
        .expect("Cargo build message is missing build artifacts");
    let (entry_point, program) = elf::dump(filename)?;

    // XXX: See https://github.com/parasyte/technek/issues/1
    if program.len() > 1024 * 1024 {
        Err(ProgramTooBigError)?;
    }

    let path = get_output_filename(filename)?;
    let fs = args
        .fs
        .as_ref()
        .map(|fs_path| {
            eprintln!(
                "{:>12} file system at `{}` to the ROM image",
                "Appending".green().bold(),
                fs_path,
            );

            fs::create_filesystem(fs_path)
        })
        .transpose()?;

    eprintln!("{:>12} final ROM image", "Building".green().bold());
    create_rom_image(path, &args, entry_point, program, fs)
}

/// Creates a ROM image, generating the header and IPL3 from `args`. An optional
/// file system (FAT image) is appended to the ROM image if provided.
fn create_rom_image(
    path: PathBuf,
    args: &BuildArgs,
    entry_point: u32,
    program: Vec<u8>,
    fs: Option<Vec<u8>>,
) -> Result<(), BuildError> {
    use self::BuildError::*;

    let fs = if let Some(fs) = fs { fs } else { Vec::new() };

    let mut file =
        File::create(&path).map_err(|_| CreateFileError(path.to_string_lossy().to_string()))?;

    let header = N64Header::new(entry_point, &args.name, &program, &fs, &args.ipl3).to_vec();
    file.write_all(&header)
        .map_err(|_| WriteFileError(path.to_string_lossy().to_string()))?;

    let ipl = args.ipl3.get_ipl();
    file.write_all(ipl)
        .map_err(|_| WriteFileError(path.to_string_lossy().to_string()))?;

    file.write_all(&program)
        .map_err(|_| WriteFileError(path.to_string_lossy().to_string()))?;

    let padding_length = (2 - (program.len() & 1)) & 1;
    let padding = [0; 1];
    file.write_all(&padding[0..padding_length])
        .map_err(|_| WriteFileError(path.to_string_lossy().to_string()))?;

    file.write_all(&fs)
        .map_err(|_| WriteFileError(path.to_string_lossy().to_string()))?;

    let rom_length = HEADER_SIZE + IPL_SIZE + program.len() + padding_length + fs.len();
    const ROM_SIZE: usize = PROGRAM_SIZE + HEADER_SIZE + IPL_SIZE;
    if rom_length < ROM_SIZE {
        let padding = std::iter::repeat(0)
            .take(ROM_SIZE - rom_length)
            .collect::<Vec<u8>>();
        file.write_all(&padding)
            .map_err(|_| WriteFileError(path.to_string_lossy().to_string()))?;
    }

    // TODO: Padding up to nearest 4 KB?

    Ok(())
}

fn get_output_filename(filename: &str) -> Result<PathBuf, BuildError> {
    use self::BuildError::*;

    let mut path = PathBuf::from(filename);
    let stem = path
        .file_stem()
        .ok_or(EmptyFilenameError)?
        .to_str()
        .ok_or(FilenameEncodingError)?
        .to_owned();

    path.pop();
    path.push(format!("{}.n64", stem));

    Ok(path)
}

fn get_runtime(start: Instant) -> String {
    let total = start.elapsed();
    format!("{}.{}s", total.as_secs(), total.subsec_millis())
}