blob: f8211d1e5a318e06622293fd6b6c9095a9d7f582 [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)]
David Brown6d47d422021-06-04 14:41:33 -060016pub 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,
23 },
David Brownc423ac42021-06-04 13:47:34 -060024}
25
26impl BootGoResult {
27 /// Was this run interrupted.
28 pub fn interrupted(&self) -> bool {
David Brown6d47d422021-06-04 14:41:33 -060029 matches!(self, BootGoResult::Stopped)
David Brownc423ac42021-06-04 13:47:34 -060030 }
31
32 /// Was this boot run successful (returned 0)
33 pub fn success(&self) -> bool {
David Brown6d47d422021-06-04 14:41:33 -060034 matches!(self, BootGoResult::Normal { result: 0, .. })
David Brownc423ac42021-06-04 13:47:34 -060035 }
36
37 /// Success, but also no asserts.
38 pub fn success_no_asserts(&self) -> bool {
David Brown6d47d422021-06-04 14:41:33 -060039 matches!(self, BootGoResult::Normal {
40 result: 0,
41 asserts: 0,
42 })
David Brownc423ac42021-06-04 13:47:34 -060043 }
44
David Brown6d47d422021-06-04 14:41:33 -060045 /// Get the asserts count. An interrupted run will be considered to have no asserts.
David Brownc423ac42021-06-04 13:47:34 -060046 pub fn asserts(&self) -> u8 {
David Brown6d47d422021-06-04 14:41:33 -060047 match self {
48 BootGoResult::Normal { asserts, .. } => *asserts,
49 _ => 0,
50 }
David Brownc423ac42021-06-04 13:47:34 -060051 }
52}
53
David Brownde7729e2017-01-09 10:41:35 -070054/// Invoke the bootloader on this flash device.
David Brown76101572019-02-28 11:29:03 -070055pub fn boot_go(multiflash: &mut SimMultiFlash, areadesc: &AreaDesc,
David Brownc423ac42021-06-04 13:47:34 -060056 counter: Option<&mut i32>, catch_asserts: bool) -> BootGoResult {
David Brown7cc45262021-03-10 05:13:44 -070057 for (&dev_id, flash) in multiflash.iter_mut() {
58 api::set_flash(dev_id, flash);
Fabio Utzig8000e322019-08-05 08:14:32 -030059 }
60 let mut sim_ctx = api::CSimContext {
61 flash_counter: match counter {
David Brownee61c832017-11-06 11:13:25 -070062 None => 0,
63 Some(ref c) => **c as libc::c_int
Fabio Utzig8000e322019-08-05 08:14:32 -030064 },
65 jumped: 0,
66 c_asserts: 0,
67 c_catch_asserts: if catch_asserts { 1 } else { 0 },
68 boot_jmpbuf: [0; 16],
69 };
David Brownd216b202021-06-04 10:14:33 -060070 let mut rsp = api::BootRsp {
71 br_hdr: std::ptr::null(),
72 flash_dev_id: 0,
73 image_off: 0,
74 };
Fabio Utzig8000e322019-08-05 08:14:32 -030075 let result = unsafe {
David Brownd216b202021-06-04 10:14:33 -060076 raw::invoke_boot_go(&mut sim_ctx as *mut _, &areadesc.get_c() as *const _,
77 &mut rsp as *mut _) as i32
Fabio Utzig8000e322019-08-05 08:14:32 -030078 };
79 let asserts = sim_ctx.c_asserts;
David Brown8608c532021-03-10 05:18:11 -070080 if let Some(c) = counter {
81 *c = sim_ctx.flash_counter;
82 }
David Brown7cc45262021-03-10 05:13:44 -070083 for &dev_id in multiflash.keys() {
84 api::clear_flash(dev_id);
85 }
David Brown6d47d422021-06-04 14:41:33 -060086 if result == -0x13579 {
87 BootGoResult::Stopped
88 } else {
89 BootGoResult::Normal { result, asserts }
90 }
David Brownde7729e2017-01-09 10:41:35 -070091}
92
Fabio Utzig3fbbdac2019-12-19 15:18:23 -030093pub fn boot_trailer_sz(align: u32) -> u32 {
Christopher Collins2adef702019-05-22 14:37:31 -070094 unsafe { raw::boot_trailer_sz(align) }
David Brownde7729e2017-01-09 10:41:35 -070095}
96
Fabio Utzig3fbbdac2019-12-19 15:18:23 -030097pub fn boot_status_sz(align: u32) -> u32 {
98 unsafe { raw::boot_status_sz(align) }
99}
100
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300101pub fn boot_magic_sz() -> usize {
David Brown2b8a6952019-10-01 16:14:53 -0600102 unsafe { raw::boot_magic_sz() as usize }
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300103}
104
105pub fn boot_max_align() -> usize {
David Browne0bb1f92019-10-01 15:57:01 -0600106 unsafe { raw::boot_max_align() as usize }
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300107}
108
Fabio Utzig1e48b912018-09-18 09:04:18 -0300109pub fn rsa_oaep_encrypt(pubkey: &[u8], seckey: &[u8]) -> Result<[u8; 256], &'static str> {
110 unsafe {
111 let mut encbuf: [u8; 256] = [0; 256];
112 if raw::rsa_oaep_encrypt_(pubkey.as_ptr(), pubkey.len() as u32,
113 seckey.as_ptr(), seckey.len() as u32,
114 encbuf.as_mut_ptr()) == 0 {
115 return Ok(encbuf);
116 }
David Brownc20bbb22021-03-10 05:19:14 -0700117 Err("Failed to encrypt buffer")
Fabio Utzig1e48b912018-09-18 09:04:18 -0300118 }
119}
120
Salome Thirot6fdbf552021-05-14 16:46:14 +0100121pub fn kw_encrypt(kek: &[u8], seckey: &[u8], keylen: u32) -> Result<Vec<u8>, &'static str> {
Fabio Utzig1e48b912018-09-18 09:04:18 -0300122 unsafe {
Salome Thirot6fdbf552021-05-14 16:46:14 +0100123 let mut encbuf = vec![0u8; 24];
124 if keylen == 32 {
125 encbuf = vec![0u8; 40];
126 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300127 if raw::kw_encrypt_(kek.as_ptr(), seckey.as_ptr(), encbuf.as_mut_ptr()) == 0 {
128 return Ok(encbuf);
129 }
David Brownc20bbb22021-03-10 05:19:14 -0700130 Err("Failed to encrypt buffer")
Fabio Utzig1e48b912018-09-18 09:04:18 -0300131 }
132}
133
David Brownde7729e2017-01-09 10:41:35 -0700134mod raw {
David Brown65de6d12019-01-02 11:38:38 -0700135 use crate::area::CAreaDesc;
David Brownd216b202021-06-04 10:14:33 -0600136 use crate::api::{BootRsp, CSimContext};
David Brownde7729e2017-01-09 10:41:35 -0700137
138 extern "C" {
139 // This generates a warning about `CAreaDesc` not being foreign safe. There doesn't appear to
140 // be any way to get rid of this warning. See https://github.com/rust-lang/rust/issues/34798
141 // for information and tracking.
David Brownd216b202021-06-04 10:14:33 -0600142 pub fn invoke_boot_go(sim_ctx: *mut CSimContext, areadesc: *const CAreaDesc,
143 rsp: *mut BootRsp) -> libc::c_int;
David Brownde7729e2017-01-09 10:41:35 -0700144
Fabio Utzig3fbbdac2019-12-19 15:18:23 -0300145 pub fn boot_trailer_sz(min_write_sz: u32) -> u32;
146 pub fn boot_status_sz(min_write_sz: u32) -> u32;
Fabio Utziga0bc9b52017-06-28 09:19:55 -0300147
David Brown2b8a6952019-10-01 16:14:53 -0600148 pub fn boot_magic_sz() -> u32;
David Browne0bb1f92019-10-01 15:57:01 -0600149 pub fn boot_max_align() -> u32;
Fabio Utzig92be3fb2017-12-05 08:52:53 -0200150
Fabio Utzig1e48b912018-09-18 09:04:18 -0300151 pub fn rsa_oaep_encrypt_(pubkey: *const u8, pubkey_len: libc::c_uint,
152 seckey: *const u8, seckey_len: libc::c_uint,
153 encbuf: *mut u8) -> libc::c_int;
154
155 pub fn kw_encrypt_(kek: *const u8, seckey: *const u8,
156 encbuf: *mut u8) -> libc::c_int;
David Brownde7729e2017-01-09 10:41:35 -0700157 }
158}