blob: 8729301d1452a3bf9ad11e3458e1339f8e3e762a [file] [log] [blame]
David Browne2acfae2020-01-21 16:45:01 -07001// Copyright (c) 2017-2019 Linaro LTD
2// Copyright (c) 2017-2019 JUUL Labs
Salome Thirot6fdbf552021-05-14 16:46:14 +01003// Copyright (c) 2019-2021 Arm Limited
David Browne2acfae2020-01-21 16:45:01 -07004//
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 Brown65de6d12019-01-02 11:38:38 -070011use crate::api;
David Brownde7729e2017-01-09 10:41:35 -070012
David Brownc423ac42021-06-04 13:47:34 -060013/// 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)]
16pub struct BootGoResult {
17 result: i32,
18 asserts: u8,
19}
20
21impl BootGoResult {
22 /// Was this run interrupted.
23 pub fn interrupted(&self) -> bool {
24 self.result == -0x13579
25 }
26
27 /// Was this boot run successful (returned 0)
28 pub fn success(&self) -> bool {
29 self.result == 0
30 }
31
32 /// Success, but also no asserts.
33 pub fn success_no_asserts(&self) -> bool {
34 self.result == 0 && self.asserts == 0
35 }
36
37 /// Get the asserts count.
38 pub fn asserts(&self) -> u8 {
39 self.asserts
40 }
41}
42
David Brownde7729e2017-01-09 10:41:35 -070043/// Invoke the bootloader on this flash device.
David Brown76101572019-02-28 11:29:03 -070044pub fn boot_go(multiflash: &mut SimMultiFlash, areadesc: &AreaDesc,
David Brownc423ac42021-06-04 13:47:34 -060045 counter: Option<&mut i32>, catch_asserts: bool) -> BootGoResult {
David Brown7cc45262021-03-10 05:13:44 -070046 for (&dev_id, flash) in multiflash.iter_mut() {
47 api::set_flash(dev_id, flash);
Fabio Utzig8000e322019-08-05 08:14:32 -030048 }
49 let mut sim_ctx = api::CSimContext {
50 flash_counter: match counter {
David Brownee61c832017-11-06 11:13:25 -070051 None => 0,
52 Some(ref c) => **c as libc::c_int
Fabio Utzig8000e322019-08-05 08:14:32 -030053 },
54 jumped: 0,
55 c_asserts: 0,
56 c_catch_asserts: if catch_asserts { 1 } else { 0 },
57 boot_jmpbuf: [0; 16],
58 };
David Brownd216b202021-06-04 10:14:33 -060059 let mut rsp = api::BootRsp {
60 br_hdr: std::ptr::null(),
61 flash_dev_id: 0,
62 image_off: 0,
63 };
Fabio Utzig8000e322019-08-05 08:14:32 -030064 let result = unsafe {
David Brownd216b202021-06-04 10:14:33 -060065 raw::invoke_boot_go(&mut sim_ctx as *mut _, &areadesc.get_c() as *const _,
66 &mut rsp as *mut _) as i32
Fabio Utzig8000e322019-08-05 08:14:32 -030067 };
68 let asserts = sim_ctx.c_asserts;
David Brown8608c532021-03-10 05:18:11 -070069 if let Some(c) = counter {
70 *c = sim_ctx.flash_counter;
71 }
David Brown7cc45262021-03-10 05:13:44 -070072 for &dev_id in multiflash.keys() {
73 api::clear_flash(dev_id);
74 }
David Brownc423ac42021-06-04 13:47:34 -060075 BootGoResult { result, asserts }
David Brownde7729e2017-01-09 10:41:35 -070076}
77
Fabio Utzig3fbbdac2019-12-19 15:18:23 -030078pub fn boot_trailer_sz(align: u32) -> u32 {
Christopher Collins2adef702019-05-22 14:37:31 -070079 unsafe { raw::boot_trailer_sz(align) }
David Brownde7729e2017-01-09 10:41:35 -070080}
81
Fabio Utzig3fbbdac2019-12-19 15:18:23 -030082pub fn boot_status_sz(align: u32) -> u32 {
83 unsafe { raw::boot_status_sz(align) }
84}
85
Fabio Utziga0bc9b52017-06-28 09:19:55 -030086pub fn boot_magic_sz() -> usize {
David Brown2b8a6952019-10-01 16:14:53 -060087 unsafe { raw::boot_magic_sz() as usize }
Fabio Utziga0bc9b52017-06-28 09:19:55 -030088}
89
90pub fn boot_max_align() -> usize {
David Browne0bb1f92019-10-01 15:57:01 -060091 unsafe { raw::boot_max_align() as usize }
Fabio Utziga0bc9b52017-06-28 09:19:55 -030092}
93
Fabio Utzig1e48b912018-09-18 09:04:18 -030094pub fn rsa_oaep_encrypt(pubkey: &[u8], seckey: &[u8]) -> Result<[u8; 256], &'static str> {
95 unsafe {
96 let mut encbuf: [u8; 256] = [0; 256];
97 if raw::rsa_oaep_encrypt_(pubkey.as_ptr(), pubkey.len() as u32,
98 seckey.as_ptr(), seckey.len() as u32,
99 encbuf.as_mut_ptr()) == 0 {
100 return Ok(encbuf);
101 }
David Brownc20bbb22021-03-10 05:19:14 -0700102 Err("Failed to encrypt buffer")
Fabio Utzig1e48b912018-09-18 09:04:18 -0300103 }
104}
105
Salome Thirot6fdbf552021-05-14 16:46:14 +0100106pub fn kw_encrypt(kek: &[u8], seckey: &[u8], keylen: u32) -> Result<Vec<u8>, &'static str> {
Fabio Utzig1e48b912018-09-18 09:04:18 -0300107 unsafe {
Salome Thirot6fdbf552021-05-14 16:46:14 +0100108 let mut encbuf = vec![0u8; 24];
109 if keylen == 32 {
110 encbuf = vec![0u8; 40];
111 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300112 if raw::kw_encrypt_(kek.as_ptr(), seckey.as_ptr(), encbuf.as_mut_ptr()) == 0 {
113 return Ok(encbuf);
114 }
David Brownc20bbb22021-03-10 05:19:14 -0700115 Err("Failed to encrypt buffer")
Fabio Utzig1e48b912018-09-18 09:04:18 -0300116 }
117}
118
David Brownde7729e2017-01-09 10:41:35 -0700119mod raw {
David Brown65de6d12019-01-02 11:38:38 -0700120 use crate::area::CAreaDesc;
David Brownd216b202021-06-04 10:14:33 -0600121 use crate::api::{BootRsp, CSimContext};
David Brownde7729e2017-01-09 10:41:35 -0700122
123 extern "C" {
124 // This generates a warning about `CAreaDesc` not being foreign safe. There doesn't appear to
125 // be any way to get rid of this warning. See https://github.com/rust-lang/rust/issues/34798
126 // for information and tracking.
David Brownd216b202021-06-04 10:14:33 -0600127 pub fn invoke_boot_go(sim_ctx: *mut CSimContext, areadesc: *const CAreaDesc,
128 rsp: *mut BootRsp) -> libc::c_int;
David Brownde7729e2017-01-09 10:41:35 -0700129
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300130 pub fn boot_trailer_sz(min_write_sz: u32) -> u32;
131 pub fn boot_status_sz(min_write_sz: u32) -> u32;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300132
David Brown2b8a6952019-10-01 16:14:53 -0600133 pub fn boot_magic_sz() -> u32;
David Browne0bb1f92019-10-01 15:57:01 -0600134 pub fn boot_max_align() -> u32;
Fabio Utzig92be3fb2017-12-05 08:52:53 -0200135
Fabio Utzig1e48b912018-09-18 09:04:18 -0300136 pub fn rsa_oaep_encrypt_(pubkey: *const u8, pubkey_len: libc::c_uint,
137 seckey: *const u8, seckey_len: libc::c_uint,
138 encbuf: *mut u8) -> libc::c_int;
139
140 pub fn kw_encrypt_(kek: *const u8, seckey: *const u8,
141 encbuf: *mut u8) -> libc::c_int;
David Brownde7729e2017-01-09 10:41:35 -0700142 }
143}