David Brown | e2acfae | 2020-01-21 16:45:01 -0700 | [diff] [blame] | 1 | // Copyright (c) 2017-2019 Linaro LTD |
| 2 | // Copyright (c) 2017-2019 JUUL Labs |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 3 | // Copyright (c) 2019-2021 Arm Limited |
David Brown | e2acfae | 2020-01-21 16:45:01 -0700 | [diff] [blame] | 4 | // |
| 5 | // SPDX-License-Identifier: Apache-2.0 |
| 6 | |
| 7 | //! Interface wrappers to C API entering to the bootloader |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 8 | |
David Brown | 65de6d1 | 2019-01-02 11:38:38 -0700 | [diff] [blame] | 9 | use crate::area::AreaDesc; |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 10 | use simflash::SimMultiFlash; |
David Brown | 65de6d1 | 2019-01-02 11:38:38 -0700 | [diff] [blame] | 11 | use crate::api; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 12 | |
David Brown | c423ac4 | 2021-06-04 13:47:34 -0600 | [diff] [blame] | 13 | /// The result of an invocation of `boot_go`. This is intentionally opaque so that we can provide |
| 14 | /// accessors for everything we need from this. |
| 15 | #[derive(Debug)] |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 16 | pub enum BootGoResult { |
| 17 | /// This run was stopped by the flash simulation mechanism. |
| 18 | Stopped, |
| 19 | /// The bootloader ran to completion with the following data. |
| 20 | Normal { |
| 21 | result: i32, |
| 22 | asserts: u8, |
David Brown | d82de8c | 2021-06-04 15:08:25 -0600 | [diff] [blame] | 23 | |
| 24 | resp: api::BootRsp, |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 25 | }, |
David Brown | c423ac4 | 2021-06-04 13:47:34 -0600 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | impl BootGoResult { |
| 29 | /// Was this run interrupted. |
| 30 | pub fn interrupted(&self) -> bool { |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 31 | matches!(self, BootGoResult::Stopped) |
David Brown | c423ac4 | 2021-06-04 13:47:34 -0600 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /// Was this boot run successful (returned 0) |
| 35 | pub fn success(&self) -> bool { |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 36 | matches!(self, BootGoResult::Normal { result: 0, .. }) |
David Brown | c423ac4 | 2021-06-04 13:47:34 -0600 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /// Success, but also no asserts. |
| 40 | pub fn success_no_asserts(&self) -> bool { |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 41 | matches!(self, BootGoResult::Normal { |
| 42 | result: 0, |
| 43 | asserts: 0, |
David Brown | d82de8c | 2021-06-04 15:08:25 -0600 | [diff] [blame] | 44 | .. |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 45 | }) |
David Brown | c423ac4 | 2021-06-04 13:47:34 -0600 | [diff] [blame] | 46 | } |
| 47 | |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 48 | /// Get the asserts count. An interrupted run will be considered to have no asserts. |
David Brown | c423ac4 | 2021-06-04 13:47:34 -0600 | [diff] [blame] | 49 | pub fn asserts(&self) -> u8 { |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 50 | match self { |
| 51 | BootGoResult::Normal { asserts, .. } => *asserts, |
| 52 | _ => 0, |
| 53 | } |
David Brown | c423ac4 | 2021-06-04 13:47:34 -0600 | [diff] [blame] | 54 | } |
David Brown | d82de8c | 2021-06-04 15:08:25 -0600 | [diff] [blame] | 55 | |
| 56 | /// Retrieve the 'resp' field that is filled in. |
| 57 | pub fn resp(&self) -> Option<&api::BootRsp> { |
| 58 | match self { |
| 59 | BootGoResult::Normal { resp, .. } => Some(resp), |
| 60 | _ => None, |
| 61 | } |
| 62 | } |
David Brown | c423ac4 | 2021-06-04 13:47:34 -0600 | [diff] [blame] | 63 | } |
| 64 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 65 | /// Invoke the bootloader on this flash device. |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 66 | pub fn boot_go(multiflash: &mut SimMultiFlash, areadesc: &AreaDesc, |
Raef Coles | 3fd3ecc | 2021-10-15 11:14:12 +0100 | [diff] [blame^] | 67 | counter: Option<&mut i32>, image_index: Option<i32>, |
| 68 | catch_asserts: bool) -> BootGoResult { |
David Brown | 7cc4526 | 2021-03-10 05:13:44 -0700 | [diff] [blame] | 69 | for (&dev_id, flash) in multiflash.iter_mut() { |
| 70 | api::set_flash(dev_id, flash); |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 71 | } |
| 72 | let mut sim_ctx = api::CSimContext { |
| 73 | flash_counter: match counter { |
David Brown | ee61c83 | 2017-11-06 11:13:25 -0700 | [diff] [blame] | 74 | None => 0, |
| 75 | Some(ref c) => **c as libc::c_int |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 76 | }, |
| 77 | jumped: 0, |
| 78 | c_asserts: 0, |
| 79 | c_catch_asserts: if catch_asserts { 1 } else { 0 }, |
| 80 | boot_jmpbuf: [0; 16], |
| 81 | }; |
David Brown | d216b20 | 2021-06-04 10:14:33 -0600 | [diff] [blame] | 82 | let mut rsp = api::BootRsp { |
| 83 | br_hdr: std::ptr::null(), |
| 84 | flash_dev_id: 0, |
| 85 | image_off: 0, |
| 86 | }; |
Raef Coles | 3fd3ecc | 2021-10-15 11:14:12 +0100 | [diff] [blame^] | 87 | let result: i32 = unsafe { |
| 88 | match image_index { |
| 89 | None => raw::invoke_boot_go(&mut sim_ctx as *mut _, |
| 90 | &areadesc.get_c() as *const _, |
| 91 | &mut rsp as *mut _, -1) as i32, |
| 92 | Some(i) => raw::invoke_boot_go(&mut sim_ctx as *mut _, |
| 93 | &areadesc.get_c() as *const _, |
| 94 | &mut rsp as *mut _, |
| 95 | i as i32) as i32 |
| 96 | } |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 97 | }; |
| 98 | let asserts = sim_ctx.c_asserts; |
David Brown | 8608c53 | 2021-03-10 05:18:11 -0700 | [diff] [blame] | 99 | if let Some(c) = counter { |
| 100 | *c = sim_ctx.flash_counter; |
| 101 | } |
David Brown | 7cc4526 | 2021-03-10 05:13:44 -0700 | [diff] [blame] | 102 | for &dev_id in multiflash.keys() { |
| 103 | api::clear_flash(dev_id); |
| 104 | } |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 105 | if result == -0x13579 { |
| 106 | BootGoResult::Stopped |
| 107 | } else { |
David Brown | d82de8c | 2021-06-04 15:08:25 -0600 | [diff] [blame] | 108 | BootGoResult::Normal { result, asserts, resp: rsp } |
David Brown | 6d47d42 | 2021-06-04 14:41:33 -0600 | [diff] [blame] | 109 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Fabio Utzig | 3fbbdac | 2019-12-19 15:18:23 -0300 | [diff] [blame] | 112 | pub fn boot_trailer_sz(align: u32) -> u32 { |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 113 | unsafe { raw::boot_trailer_sz(align) } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Fabio Utzig | 3fbbdac | 2019-12-19 15:18:23 -0300 | [diff] [blame] | 116 | pub fn boot_status_sz(align: u32) -> u32 { |
| 117 | unsafe { raw::boot_status_sz(align) } |
| 118 | } |
| 119 | |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 120 | pub fn boot_magic_sz() -> usize { |
David Brown | 2b8a695 | 2019-10-01 16:14:53 -0600 | [diff] [blame] | 121 | unsafe { raw::boot_magic_sz() as usize } |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | pub fn boot_max_align() -> usize { |
David Brown | e0bb1f9 | 2019-10-01 15:57:01 -0600 | [diff] [blame] | 125 | unsafe { raw::boot_max_align() as usize } |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 126 | } |
| 127 | |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 128 | pub fn rsa_oaep_encrypt(pubkey: &[u8], seckey: &[u8]) -> Result<[u8; 256], &'static str> { |
| 129 | unsafe { |
| 130 | let mut encbuf: [u8; 256] = [0; 256]; |
| 131 | if raw::rsa_oaep_encrypt_(pubkey.as_ptr(), pubkey.len() as u32, |
| 132 | seckey.as_ptr(), seckey.len() as u32, |
| 133 | encbuf.as_mut_ptr()) == 0 { |
| 134 | return Ok(encbuf); |
| 135 | } |
David Brown | c20bbb2 | 2021-03-10 05:19:14 -0700 | [diff] [blame] | 136 | Err("Failed to encrypt buffer") |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 140 | pub fn kw_encrypt(kek: &[u8], seckey: &[u8], keylen: u32) -> Result<Vec<u8>, &'static str> { |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 141 | unsafe { |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 142 | let mut encbuf = vec![0u8; 24]; |
| 143 | if keylen == 32 { |
| 144 | encbuf = vec![0u8; 40]; |
| 145 | } |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 146 | if raw::kw_encrypt_(kek.as_ptr(), seckey.as_ptr(), encbuf.as_mut_ptr()) == 0 { |
| 147 | return Ok(encbuf); |
| 148 | } |
David Brown | c20bbb2 | 2021-03-10 05:19:14 -0700 | [diff] [blame] | 149 | Err("Failed to encrypt buffer") |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 153 | mod raw { |
David Brown | 65de6d1 | 2019-01-02 11:38:38 -0700 | [diff] [blame] | 154 | use crate::area::CAreaDesc; |
David Brown | d216b20 | 2021-06-04 10:14:33 -0600 | [diff] [blame] | 155 | use crate::api::{BootRsp, CSimContext}; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 156 | |
| 157 | extern "C" { |
| 158 | // This generates a warning about `CAreaDesc` not being foreign safe. There doesn't appear to |
| 159 | // be any way to get rid of this warning. See https://github.com/rust-lang/rust/issues/34798 |
| 160 | // for information and tracking. |
David Brown | d216b20 | 2021-06-04 10:14:33 -0600 | [diff] [blame] | 161 | pub fn invoke_boot_go(sim_ctx: *mut CSimContext, areadesc: *const CAreaDesc, |
Raef Coles | 3fd3ecc | 2021-10-15 11:14:12 +0100 | [diff] [blame^] | 162 | rsp: *mut BootRsp, image_index: libc::c_int) -> libc::c_int; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 163 | |
Fabio Utzig | 3fbbdac | 2019-12-19 15:18:23 -0300 | [diff] [blame] | 164 | pub fn boot_trailer_sz(min_write_sz: u32) -> u32; |
| 165 | pub fn boot_status_sz(min_write_sz: u32) -> u32; |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 166 | |
David Brown | 2b8a695 | 2019-10-01 16:14:53 -0600 | [diff] [blame] | 167 | pub fn boot_magic_sz() -> u32; |
David Brown | e0bb1f9 | 2019-10-01 15:57:01 -0600 | [diff] [blame] | 168 | pub fn boot_max_align() -> u32; |
Fabio Utzig | 92be3fb | 2017-12-05 08:52:53 -0200 | [diff] [blame] | 169 | |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 170 | pub fn rsa_oaep_encrypt_(pubkey: *const u8, pubkey_len: libc::c_uint, |
| 171 | seckey: *const u8, seckey_len: libc::c_uint, |
| 172 | encbuf: *mut u8) -> libc::c_int; |
| 173 | |
| 174 | pub fn kw_encrypt_(kek: *const u8, seckey: *const u8, |
| 175 | encbuf: *mut u8) -> libc::c_int; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 176 | } |
| 177 | } |