|
| 1 | +use super::Cli; |
| 2 | +use std::fs::{metadata, remove_file}; |
| 3 | +use std::io::ErrorKind; |
| 4 | +use std::path::PathBuf; |
| 5 | +use std::process::Command; |
| 6 | + |
| 7 | +// Converts the ELF file specified on the command line into TBF and TAB files, |
| 8 | +// and returns the paths to those files. |
| 9 | +pub fn convert_elf(cli: &Cli) -> OutFiles { |
| 10 | + let package_name = cli.elf.file_stem().expect("ELF must be a file"); |
| 11 | + let mut tab_path = cli.elf.clone(); |
| 12 | + tab_path.set_extension("tab"); |
| 13 | + let protected_size = TBF_HEADER_SIZE.to_string(); |
| 14 | + if cli.verbose { |
| 15 | + println!("Package name: {:?}", package_name); |
| 16 | + println!("TAB path: {}", tab_path.display()); |
| 17 | + println!("Protected region size: {}", protected_size); |
| 18 | + } |
| 19 | + let stack_size = read_stack_size(cli); |
| 20 | + let elf = cli.elf.as_os_str(); |
| 21 | + let mut tbf_path = cli.elf.clone(); |
| 22 | + tbf_path.set_extension("tbf"); |
| 23 | + if cli.verbose { |
| 24 | + println!("ELF file: {:?}", elf); |
| 25 | + println!("TBF path: {}", tbf_path.display()); |
| 26 | + } |
| 27 | + |
| 28 | + // If elf2tab returns a successful status but does not write to the TBF |
| 29 | + // file, then we run the risk of using an outdated TBF file, creating a |
| 30 | + // hard-to-debug situation. Therefore, we delete the TBF file, forcing |
| 31 | + // elf2tab to create it, and later verify that it exists. |
| 32 | + if let Err(io_error) = remove_file(&tbf_path) { |
| 33 | + // Ignore file-no-found errors, panic on any other error. |
| 34 | + assert_eq!( |
| 35 | + io_error.kind(), |
| 36 | + ErrorKind::NotFound, |
| 37 | + "Unable to remove the TBF file" |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + let mut command = Command::new("elf2tab"); |
| 42 | + #[rustfmt::skip] |
| 43 | + command.args([ |
| 44 | + // TODO: libtock-rs' crates are designed for Tock 2.1's Allow interface, |
| 45 | + // so we should increment this as soon as the Tock kernel will accept a |
| 46 | + // 2.1 app. |
| 47 | + "--kernel-major".as_ref(), "2".as_ref(), |
| 48 | + "--kernel-minor".as_ref(), "0".as_ref(), |
| 49 | + "-n".as_ref(), package_name, |
| 50 | + "-o".as_ref(), tab_path.as_os_str(), |
| 51 | + "--protected-region-size".as_ref(), protected_size.as_ref(), |
| 52 | + "--stack".as_ref(), stack_size.as_ref(), |
| 53 | + elf, |
| 54 | + ]); |
| 55 | + if cli.verbose { |
| 56 | + command.arg("-v"); |
| 57 | + println!("elf2tab command: {:?}", command); |
| 58 | + println!("Spawning elf2tab"); |
| 59 | + } |
| 60 | + let mut child = command.spawn().expect("failed to spawn elf2tab"); |
| 61 | + let status = child.wait().expect("failed to wait for elf2tab"); |
| 62 | + if cli.verbose { |
| 63 | + println!("elf2tab finished. {}", status); |
| 64 | + } |
| 65 | + assert!(status.success(), "elf2tab returned an error. {}", status); |
| 66 | + |
| 67 | + // Verify that elf2tab created the TBF file, and that it is a file. |
| 68 | + match metadata(&tbf_path) { |
| 69 | + Err(io_error) => { |
| 70 | + if io_error.kind() == ErrorKind::NotFound { |
| 71 | + panic!("elf2tab did not create {}", tbf_path.display()); |
| 72 | + } |
| 73 | + panic!( |
| 74 | + "Unable to query metadata for {}: {}", |
| 75 | + tbf_path.display(), |
| 76 | + io_error |
| 77 | + ); |
| 78 | + } |
| 79 | + Ok(metadata) => { |
| 80 | + assert!(metadata.is_file(), "{} is not a file", tbf_path.display()); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + OutFiles { tab_path, tbf_path } |
| 85 | +} |
| 86 | + |
| 87 | +// Paths to the files output by elf2tab. |
| 88 | +pub struct OutFiles { |
| 89 | + pub tab_path: PathBuf, |
| 90 | + pub tbf_path: PathBuf, |
| 91 | +} |
| 92 | + |
| 93 | +// The amount of space to reserve for the TBF header. |
| 94 | +const TBF_HEADER_SIZE: u32 = 0x48; |
| 95 | + |
| 96 | +// Reads the stack size, and returns it as a String for use on elf2tab's command |
| 97 | +// line. |
| 98 | +fn read_stack_size(cli: &Cli) -> String { |
| 99 | + let file = elf::File::open_path(&cli.elf).expect("Unable to open ELF"); |
| 100 | + for section in file.sections { |
| 101 | + // This section name comes from runtime/libtock_layout.ld, and it |
| 102 | + // matches the size (and location) of the process binary's stack. |
| 103 | + if section.shdr.name == ".stack" { |
| 104 | + let stack_size = section.shdr.size.to_string(); |
| 105 | + if cli.verbose { |
| 106 | + println!("Found .stack section, size: {}", stack_size); |
| 107 | + } |
| 108 | + return stack_size; |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + panic!("Unable to find the .stack section in {}", cli.elf.display()); |
| 113 | +} |
0 commit comments