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 | |
| 13 | /// Invoke the bootloader on this flash device. |
David Brown | 7610157 | 2019-02-28 11:29:03 -0700 | [diff] [blame] | 14 | pub fn boot_go(multiflash: &mut SimMultiFlash, areadesc: &AreaDesc, |
Fabio Utzig | afb2bc9 | 2018-11-19 16:11:52 -0200 | [diff] [blame] | 15 | counter: Option<&mut i32>, catch_asserts: bool) -> (i32, u8) { |
David Brown | 7cc4526 | 2021-03-10 05:13:44 -0700 | [diff] [blame] | 16 | for (&dev_id, flash) in multiflash.iter_mut() { |
| 17 | api::set_flash(dev_id, flash); |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 18 | } |
| 19 | let mut sim_ctx = api::CSimContext { |
| 20 | flash_counter: match counter { |
David Brown | ee61c83 | 2017-11-06 11:13:25 -0700 | [diff] [blame] | 21 | None => 0, |
| 22 | Some(ref c) => **c as libc::c_int |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 23 | }, |
| 24 | jumped: 0, |
| 25 | c_asserts: 0, |
| 26 | c_catch_asserts: if catch_asserts { 1 } else { 0 }, |
| 27 | boot_jmpbuf: [0; 16], |
| 28 | }; |
| 29 | let result = unsafe { |
| 30 | raw::invoke_boot_go(&mut sim_ctx as *mut _, &areadesc.get_c() as *const _) as i32 |
| 31 | }; |
| 32 | let asserts = sim_ctx.c_asserts; |
David Brown | 8608c53 | 2021-03-10 05:18:11 -0700 | [diff] [blame] | 33 | if let Some(c) = counter { |
| 34 | *c = sim_ctx.flash_counter; |
| 35 | } |
David Brown | 7cc4526 | 2021-03-10 05:13:44 -0700 | [diff] [blame] | 36 | for &dev_id in multiflash.keys() { |
| 37 | api::clear_flash(dev_id); |
| 38 | } |
Fabio Utzig | 9b0ee90 | 2017-11-23 19:49:00 -0200 | [diff] [blame] | 39 | (result, asserts) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Fabio Utzig | 3fbbdac | 2019-12-19 15:18:23 -0300 | [diff] [blame] | 42 | pub fn boot_trailer_sz(align: u32) -> u32 { |
Christopher Collins | 2adef70 | 2019-05-22 14:37:31 -0700 | [diff] [blame] | 43 | unsafe { raw::boot_trailer_sz(align) } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Fabio Utzig | 3fbbdac | 2019-12-19 15:18:23 -0300 | [diff] [blame] | 46 | pub fn boot_status_sz(align: u32) -> u32 { |
| 47 | unsafe { raw::boot_status_sz(align) } |
| 48 | } |
| 49 | |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 50 | pub fn boot_magic_sz() -> usize { |
David Brown | 2b8a695 | 2019-10-01 16:14:53 -0600 | [diff] [blame] | 51 | unsafe { raw::boot_magic_sz() as usize } |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | pub fn boot_max_align() -> usize { |
David Brown | e0bb1f9 | 2019-10-01 15:57:01 -0600 | [diff] [blame] | 55 | unsafe { raw::boot_max_align() as usize } |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 56 | } |
| 57 | |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 58 | pub fn rsa_oaep_encrypt(pubkey: &[u8], seckey: &[u8]) -> Result<[u8; 256], &'static str> { |
| 59 | unsafe { |
| 60 | let mut encbuf: [u8; 256] = [0; 256]; |
| 61 | if raw::rsa_oaep_encrypt_(pubkey.as_ptr(), pubkey.len() as u32, |
| 62 | seckey.as_ptr(), seckey.len() as u32, |
| 63 | encbuf.as_mut_ptr()) == 0 { |
| 64 | return Ok(encbuf); |
| 65 | } |
David Brown | c20bbb2 | 2021-03-10 05:19:14 -0700 | [diff] [blame] | 66 | Err("Failed to encrypt buffer") |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame^] | 70 | 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] | 71 | unsafe { |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame^] | 72 | let mut encbuf = vec![0u8; 24]; |
| 73 | if keylen == 32 { |
| 74 | encbuf = vec![0u8; 40]; |
| 75 | } |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 76 | if raw::kw_encrypt_(kek.as_ptr(), seckey.as_ptr(), encbuf.as_mut_ptr()) == 0 { |
| 77 | return Ok(encbuf); |
| 78 | } |
David Brown | c20bbb2 | 2021-03-10 05:19:14 -0700 | [diff] [blame] | 79 | Err("Failed to encrypt buffer") |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 83 | mod raw { |
David Brown | 65de6d1 | 2019-01-02 11:38:38 -0700 | [diff] [blame] | 84 | use crate::area::CAreaDesc; |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 85 | use crate::api::CSimContext; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 86 | |
| 87 | extern "C" { |
| 88 | // This generates a warning about `CAreaDesc` not being foreign safe. There doesn't appear to |
| 89 | // be any way to get rid of this warning. See https://github.com/rust-lang/rust/issues/34798 |
| 90 | // for information and tracking. |
Fabio Utzig | 8000e32 | 2019-08-05 08:14:32 -0300 | [diff] [blame] | 91 | pub fn invoke_boot_go(sim_ctx: *mut CSimContext, areadesc: *const CAreaDesc) -> libc::c_int; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 92 | |
Fabio Utzig | 3fbbdac | 2019-12-19 15:18:23 -0300 | [diff] [blame] | 93 | pub fn boot_trailer_sz(min_write_sz: u32) -> u32; |
| 94 | pub fn boot_status_sz(min_write_sz: u32) -> u32; |
Fabio Utzig | a0bc9b5 | 2017-06-28 09:19:55 -0300 | [diff] [blame] | 95 | |
David Brown | 2b8a695 | 2019-10-01 16:14:53 -0600 | [diff] [blame] | 96 | pub fn boot_magic_sz() -> u32; |
David Brown | e0bb1f9 | 2019-10-01 15:57:01 -0600 | [diff] [blame] | 97 | pub fn boot_max_align() -> u32; |
Fabio Utzig | 92be3fb | 2017-12-05 08:52:53 -0200 | [diff] [blame] | 98 | |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 99 | pub fn rsa_oaep_encrypt_(pubkey: *const u8, pubkey_len: libc::c_uint, |
| 100 | seckey: *const u8, seckey_len: libc::c_uint, |
| 101 | encbuf: *mut u8) -> libc::c_int; |
| 102 | |
| 103 | pub fn kw_encrypt_(kek: *const u8, seckey: *const u8, |
| 104 | encbuf: *mut u8) -> libc::c_int; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 105 | } |
| 106 | } |