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                /* reserved (1 << 1) */
15     EcdsaP256            = (1 << 2),
16     SwapUsingScratch     = (1 << 3),
17     OverwriteUpgrade     = (1 << 4),
18     EncRsa               = (1 << 5),
19     EncKw                = (1 << 6),
20     ValidatePrimarySlot  = (1 << 7),
21     RSA3072              = (1 << 8),
22     Ed25519              = (1 << 9),
23     EncEc256             = (1 << 10),
24     SwapUsingMove        = (1 << 11),
25     DowngradePrevention  = (1 << 12),
26     EncX25519            = (1 << 13),
27     Bootstrap            = (1 << 14),
28     Aes256               = (1 << 15),
29     RamLoad              = (1 << 16),
30     DirectXip            = (1 << 17),
31     HwRollbackProtection = (1 << 18),
32     EcdsaP384            = (1 << 19),
33 }
34 
35 impl Caps {
present(self) -> bool36     pub fn present(self) -> bool {
37         let caps = unsafe { bootutil_get_caps() };
38         (caps as u32) & (self as u32) != 0
39     }
40 
41     /// Does this build have ECDSA of some type enabled for signatures.
has_ecdsa() -> bool42     pub fn has_ecdsa() -> bool {
43         Caps::EcdsaP256.present() || Caps::EcdsaP384.present()
44     }
45 
46     /// Query for the number of images that have been configured into this
47     /// MCUboot build.
get_num_images() -> usize48     pub fn get_num_images() -> usize {
49         (unsafe { bootutil_get_num_images() }) as usize
50     }
51 
52     /// Query if this configuration performs some kind of upgrade by writing to flash.
modifies_flash() -> bool53     pub fn modifies_flash() -> bool {
54         // All other configurations perform upgrades by writing to flash.
55         !(Self::RamLoad.present() || Self::DirectXip.present())
56     }
57 }
58 
59 extern "C" {
bootutil_get_caps() -> Caps60     fn bootutil_get_caps() -> Caps;
bootutil_get_num_images() -> u3261     fn bootutil_get_num_images() -> u32;
62 }
63