blob: 61276e27cbe2621f75ca240d082c58fbcc546c1d [file] [log] [blame]
David Browne2acfae2020-01-21 16:45:01 -07001// Copyright (c) 2017-2019 Linaro LTD
2// Copyright (c) 2017-2019 JUUL Labs
3// Copyright (c) 2019 Arm Limited
4//
5// SPDX-License-Identifier: Apache-2.0
6
7//! Interface wrappers to C API entering to the bootloader
David Brownde7729e2017-01-09 10:41:35 -07008
David Brown65de6d12019-01-02 11:38:38 -07009use crate::area::AreaDesc;
David Brown76101572019-02-28 11:29:03 -070010use simflash::SimMultiFlash;
David Brownde7729e2017-01-09 10:41:35 -070011use libc;
David Brown65de6d12019-01-02 11:38:38 -070012use crate::api;
David Brownde7729e2017-01-09 10:41:35 -070013
14/// Invoke the bootloader on this flash device.
David Brown76101572019-02-28 11:29:03 -070015pub fn boot_go(multiflash: &mut SimMultiFlash, areadesc: &AreaDesc,
Fabio Utzigafb2bc92018-11-19 16:11:52 -020016 counter: Option<&mut i32>, catch_asserts: bool) -> (i32, u8) {
David Brownee61c832017-11-06 11:13:25 -070017 unsafe {
David Brown76101572019-02-28 11:29:03 -070018 for (&dev_id, flash) in multiflash.iter_mut() {
Fabio Utzigafb2bc92018-11-19 16:11:52 -020019 api::set_flash(dev_id, flash);
20 }
Fabio Utzig8000e322019-08-05 08:14:32 -030021 }
22 let mut sim_ctx = api::CSimContext {
23 flash_counter: match counter {
David Brownee61c832017-11-06 11:13:25 -070024 None => 0,
25 Some(ref c) => **c as libc::c_int
Fabio Utzig8000e322019-08-05 08:14:32 -030026 },
27 jumped: 0,
28 c_asserts: 0,
29 c_catch_asserts: if catch_asserts { 1 } else { 0 },
30 boot_jmpbuf: [0; 16],
31 };
32 let result = unsafe {
33 raw::invoke_boot_go(&mut sim_ctx as *mut _, &areadesc.get_c() as *const _) as i32
34 };
35 let asserts = sim_ctx.c_asserts;
36 counter.map(|c| *c = sim_ctx.flash_counter);
David Brownee61c832017-11-06 11:13:25 -070037 unsafe {
David Brown76101572019-02-28 11:29:03 -070038 for (&dev_id, _) in multiflash {
Fabio Utzigafb2bc92018-11-19 16:11:52 -020039 api::clear_flash(dev_id);
40 }
David Brownee61c832017-11-06 11:13:25 -070041 };
Fabio Utzig9b0ee902017-11-23 19:49:00 -020042 (result, asserts)
David Brownde7729e2017-01-09 10:41:35 -070043}
44
Fabio Utzig3fbbdac2019-12-19 15:18:23 -030045pub fn boot_trailer_sz(align: u32) -> u32 {
Christopher Collins2adef702019-05-22 14:37:31 -070046 unsafe { raw::boot_trailer_sz(align) }
David Brownde7729e2017-01-09 10:41:35 -070047}
48
Fabio Utzig3fbbdac2019-12-19 15:18:23 -030049pub fn boot_status_sz(align: u32) -> u32 {
50 unsafe { raw::boot_status_sz(align) }
51}
52
Fabio Utziga0bc9b52017-06-28 09:19:55 -030053pub fn boot_magic_sz() -> usize {
David Brown2b8a6952019-10-01 16:14:53 -060054 unsafe { raw::boot_magic_sz() as usize }
Fabio Utziga0bc9b52017-06-28 09:19:55 -030055}
56
57pub fn boot_max_align() -> usize {
David Browne0bb1f92019-10-01 15:57:01 -060058 unsafe { raw::boot_max_align() as usize }
Fabio Utziga0bc9b52017-06-28 09:19:55 -030059}
60
Fabio Utzig1e48b912018-09-18 09:04:18 -030061pub fn rsa_oaep_encrypt(pubkey: &[u8], seckey: &[u8]) -> Result<[u8; 256], &'static str> {
62 unsafe {
63 let mut encbuf: [u8; 256] = [0; 256];
64 if raw::rsa_oaep_encrypt_(pubkey.as_ptr(), pubkey.len() as u32,
65 seckey.as_ptr(), seckey.len() as u32,
66 encbuf.as_mut_ptr()) == 0 {
67 return Ok(encbuf);
68 }
69 return Err("Failed to encrypt buffer");
70 }
71}
72
73pub fn kw_encrypt(kek: &[u8], seckey: &[u8]) -> Result<[u8; 24], &'static str> {
74 unsafe {
75 let mut encbuf = [0u8; 24];
76 if raw::kw_encrypt_(kek.as_ptr(), seckey.as_ptr(), encbuf.as_mut_ptr()) == 0 {
77 return Ok(encbuf);
78 }
79 return Err("Failed to encrypt buffer");
80 }
81}
82
David Brownde7729e2017-01-09 10:41:35 -070083mod raw {
David Brown65de6d12019-01-02 11:38:38 -070084 use crate::area::CAreaDesc;
Fabio Utzig8000e322019-08-05 08:14:32 -030085 use crate::api::CSimContext;
David Brownde7729e2017-01-09 10:41:35 -070086 use libc;
87
88 extern "C" {
89 // This generates a warning about `CAreaDesc` not being foreign safe. There doesn't appear to
90 // be any way to get rid of this warning. See https://github.com/rust-lang/rust/issues/34798
91 // for information and tracking.
Fabio Utzig8000e322019-08-05 08:14:32 -030092 pub fn invoke_boot_go(sim_ctx: *mut CSimContext, areadesc: *const CAreaDesc) -> libc::c_int;
David Brownde7729e2017-01-09 10:41:35 -070093
Fabio Utzig3fbbdac2019-12-19 15:18:23 -030094 pub fn boot_trailer_sz(min_write_sz: u32) -> u32;
95 pub fn boot_status_sz(min_write_sz: u32) -> u32;
Fabio Utziga0bc9b52017-06-28 09:19:55 -030096
David Brown2b8a6952019-10-01 16:14:53 -060097 pub fn boot_magic_sz() -> u32;
David Browne0bb1f92019-10-01 15:57:01 -060098 pub fn boot_max_align() -> u32;
Fabio Utzig92be3fb2017-12-05 08:52:53 -020099
Fabio Utzig1e48b912018-09-18 09:04:18 -0300100 pub fn rsa_oaep_encrypt_(pubkey: *const u8, pubkey_len: libc::c_uint,
101 seckey: *const u8, seckey_len: libc::c_uint,
102 encbuf: *mut u8) -> libc::c_int;
103
104 pub fn kw_encrypt_(kek: *const u8, seckey: *const u8,
105 encbuf: *mut u8) -> libc::c_int;
David Brownde7729e2017-01-09 10:41:35 -0700106 }
107}