1 // Copyright (c) 2017-2021 Linaro LTD
2 // Copyright (c) 2019 JUUL Labs
3 // Copyright (c) 2019-2023 Arm Limited
4 //
5 // SPDX-License-Identifier: Apache-2.0
6 
7 // Query the bootloader's capabilities.
8 
9 #[repr(u32)]
10 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
11 #[allow(unused)]
12 pub enum Caps {
13     RSA2048              = (1 << 0),
14     EcdsaP256            = (1 << 1),
15     SwapUsingScratch     = (1 << 2),
16     OverwriteUpgrade     = (1 << 3),
17     EncRsa               = (1 << 4),
18     EncKw                = (1 << 5),
19     ValidatePrimarySlot  = (1 << 6),
20     RSA3072              = (1 << 7),
21     Ed25519              = (1 << 8),
22     EncEc256             = (1 << 9),
23     SwapUsingMove        = (1 << 10),
24     DowngradePrevention  = (1 << 11),
25     EncX25519            = (1 << 12),
26     Bootstrap            = (1 << 13),
27     Aes256               = (1 << 14),
28     RamLoad              = (1 << 15),
29     DirectXip            = (1 << 16),
30 }
31 
32 impl Caps {
present(self) -> bool33     pub fn present(self) -> bool {
34         let caps = unsafe { bootutil_get_caps() };
35         (caps as u32) & (self as u32) != 0
36     }
37 
38     /// Does this build have ECDSA of some type enabled for signatures.
has_ecdsa() -> bool39     pub fn has_ecdsa() -> bool {
40         Caps::EcdsaP256.present()
41     }
42 
43     /// Query for the number of images that have been configured into this
44     /// MCUboot build.
get_num_images() -> usize45     pub fn get_num_images() -> usize {
46         (unsafe { bootutil_get_num_images() }) as usize
47     }
48 
49     /// Query if this configuration performs some kind of upgrade by writing to flash.
modifies_flash() -> bool50     pub fn modifies_flash() -> bool {
51         // All other configurations perform upgrades by writing to flash.
52         !(Self::RamLoad.present() || Self::DirectXip.present())
53     }
54 }
55 
56 extern "C" {
bootutil_get_caps() -> Caps57     fn bootutil_get_caps() -> Caps;
bootutil_get_num_images() -> u3258     fn bootutil_get_num_images() -> u32;
59 }
60