David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 1 | // Build mcuboot as a library, based on the requested features. |
| 2 | |
Fabio Utzig | 455cad5 | 2018-10-15 14:36:33 -0700 | [diff] [blame] | 3 | extern crate cc; |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 4 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 5 | use std::collections::BTreeSet; |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 6 | use std::env; |
| 7 | use std::fs; |
| 8 | use std::io; |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 9 | use std::path::{Path, PathBuf}; |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 10 | |
| 11 | fn main() { |
| 12 | // Feature flags. |
| 13 | let sig_rsa = env::var("CARGO_FEATURE_SIG_RSA").is_ok(); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 14 | let sig_rsa3072 = env::var("CARGO_FEATURE_SIG_RSA3072").is_ok(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 15 | let sig_ecdsa = env::var("CARGO_FEATURE_SIG_ECDSA").is_ok(); |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 16 | let sig_ecdsa_mbedtls = env::var("CARGO_FEATURE_SIG_ECDSA_MBEDTLS").is_ok(); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 17 | let sig_ed25519 = env::var("CARGO_FEATURE_SIG_ED25519").is_ok(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 18 | let overwrite_only = env::var("CARGO_FEATURE_OVERWRITE_ONLY").is_ok(); |
Fabio Utzig | 031eb7d | 2019-11-28 10:13:14 -0300 | [diff] [blame] | 19 | let swap_move = env::var("CARGO_FEATURE_SWAP_MOVE").is_ok(); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 20 | let swap_status = env::var("CARGO_FEATURE_SWAP_STATUS").is_ok(); |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 21 | let validate_primary_slot = |
| 22 | env::var("CARGO_FEATURE_VALIDATE_PRIMARY_SLOT").is_ok(); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 23 | let enc_rsa = env::var("CARGO_FEATURE_ENC_RSA").is_ok(); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 24 | let enc_aes256_rsa = env::var("CARGO_FEATURE_ENC_AES256_RSA").is_ok(); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 25 | let enc_kw = env::var("CARGO_FEATURE_ENC_KW").is_ok(); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 26 | let enc_aes256_kw = env::var("CARGO_FEATURE_ENC_AES256_KW").is_ok(); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 27 | let enc_ec256 = env::var("CARGO_FEATURE_ENC_EC256").is_ok(); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 28 | let enc_ec256_mbedtls = env::var("CARGO_FEATURE_ENC_EC256_MBEDTLS").is_ok(); |
| 29 | let enc_aes256_ec256 = env::var("CARGO_FEATURE_ENC_AES256_EC256").is_ok(); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 30 | let enc_x25519 = env::var("CARGO_FEATURE_ENC_X25519").is_ok(); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 31 | let enc_aes256_x25519 = env::var("CARGO_FEATURE_ENC_AES256_X25519").is_ok(); |
Fabio Utzig | 9b97b13 | 2018-12-18 17:21:51 -0200 | [diff] [blame] | 32 | let bootstrap = env::var("CARGO_FEATURE_BOOTSTRAP").is_ok(); |
David Brown | 5e6f5e0 | 2019-04-04 10:50:05 +0700 | [diff] [blame] | 33 | let multiimage = env::var("CARGO_FEATURE_MULTIIMAGE").is_ok(); |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 34 | let downgrade_prevention = env::var("CARGO_FEATURE_DOWNGRADE_PREVENTION").is_ok(); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 35 | let ram_load = env::var("CARGO_FEATURE_RAM_LOAD").is_ok(); |
| 36 | let direct_xip = env::var("CARGO_FEATURE_DIRECT_XIP").is_ok(); |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 37 | let max_align_32 = env::var("CARGO_FEATURE_MAX_ALIGN_32").is_ok(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 38 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 39 | let mut conf = CachedBuild::new(); |
| 40 | conf.conf.define("__BOOTSIM__", None); |
| 41 | conf.conf.define("MCUBOOT_HAVE_LOGGING", None); |
| 42 | conf.conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None); |
| 43 | conf.conf.define("MCUBOOT_HAVE_ASSERT_H", None); |
| 44 | conf.conf.define("MCUBOOT_MAX_IMG_SECTORS", Some("128")); |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 45 | |
| 46 | if max_align_32 { |
| 47 | conf.conf.define("MCUBOOT_BOOT_MAX_ALIGN", Some("32")); |
| 48 | } else { |
| 49 | conf.conf.define("MCUBOOT_BOOT_MAX_ALIGN", Some("8")); |
| 50 | } |
| 51 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 52 | conf.conf.define("MCUBOOT_IMAGE_NUMBER", Some(if multiimage { "2" } else { "1" })); |
Fabio Utzig | ebdc969 | 2017-11-23 16:28:25 -0200 | [diff] [blame] | 53 | |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 54 | if downgrade_prevention && !overwrite_only { |
| 55 | panic!("Downgrade prevention requires overwrite only"); |
| 56 | } |
| 57 | |
Fabio Utzig | 9b97b13 | 2018-12-18 17:21:51 -0200 | [diff] [blame] | 58 | if bootstrap { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 59 | conf.conf.define("MCUBOOT_BOOTSTRAP", None); |
| 60 | conf.conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None); |
Fabio Utzig | 9b97b13 | 2018-12-18 17:21:51 -0200 | [diff] [blame] | 61 | } |
| 62 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 63 | if validate_primary_slot { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 64 | conf.conf.define("MCUBOOT_VALIDATE_PRIMARY_SLOT", None); |
Fabio Utzig | ebdc969 | 2017-11-23 16:28:25 -0200 | [diff] [blame] | 65 | } |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 66 | |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 67 | if downgrade_prevention { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 68 | conf.conf.define("MCUBOOT_DOWNGRADE_PREVENTION", None); |
| 69 | } |
| 70 | |
| 71 | if ram_load { |
| 72 | conf.conf.define("MCUBOOT_RAM_LOAD", None); |
| 73 | } |
| 74 | |
| 75 | if direct_xip { |
| 76 | conf.conf.define("MCUBOOT_DIRECT_XIP", None); |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 79 | // Currently no more than one sig type can be used simultaneously. |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 80 | if vec![sig_rsa, sig_rsa3072, sig_ecdsa, sig_ed25519].iter() |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 81 | .fold(0, |sum, &v| sum + v as i32) > 1 { |
| 82 | panic!("mcuboot does not support more than one sig type at the same time"); |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 83 | } |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 84 | |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 85 | if sig_rsa || sig_rsa3072 { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 86 | conf.conf.define("MCUBOOT_SIGN_RSA", None); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 87 | // The Kconfig style defines must be added here as well because |
| 88 | // they are used internally by "config-rsa.h" |
| 89 | if sig_rsa { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 90 | conf.conf.define("MCUBOOT_SIGN_RSA_LEN", "2048"); |
| 91 | conf.conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "2048"); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 92 | } else { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 93 | conf.conf.define("MCUBOOT_SIGN_RSA_LEN", "3072"); |
| 94 | conf.conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "3072"); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 95 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 96 | conf.conf.define("MCUBOOT_USE_MBED_TLS", None); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 97 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 98 | conf.conf.include("../../ext/mbedtls/include"); |
| 99 | conf.file("../../ext/mbedtls/library/sha256.c"); |
Fabio Utzig | 806af0e | 2018-04-26 10:53:54 -0300 | [diff] [blame] | 100 | conf.file("csupport/keys.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 101 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 102 | conf.file("../../ext/mbedtls/library/rsa.c"); |
| 103 | conf.file("../../ext/mbedtls/library/bignum.c"); |
| 104 | conf.file("../../ext/mbedtls/library/platform.c"); |
| 105 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
| 106 | conf.file("../../ext/mbedtls/library/asn1parse.c"); |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 107 | } else if sig_ecdsa { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 108 | conf.conf.define("MCUBOOT_SIGN_EC256", None); |
| 109 | conf.conf.define("MCUBOOT_USE_TINYCRYPT", None); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 110 | |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 111 | if !enc_kw { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 112 | conf.conf.include("../../ext/mbedtls/include"); |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 113 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 114 | conf.conf.include("../../ext/tinycrypt/lib/include"); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 115 | |
Fabio Utzig | 806af0e | 2018-04-26 10:53:54 -0300 | [diff] [blame] | 116 | conf.file("csupport/keys.c"); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 117 | |
| 118 | conf.file("../../ext/tinycrypt/lib/source/utils.c"); |
| 119 | conf.file("../../ext/tinycrypt/lib/source/sha256.c"); |
| 120 | conf.file("../../ext/tinycrypt/lib/source/ecc.c"); |
| 121 | conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c"); |
| 122 | conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c"); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 123 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
| 124 | conf.file("../../ext/mbedtls/library/asn1parse.c"); |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 125 | } else if sig_ecdsa_mbedtls { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 126 | conf.conf.define("MCUBOOT_SIGN_EC256", None); |
| 127 | conf.conf.define("MCUBOOT_USE_MBED_TLS", None); |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 128 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 129 | conf.conf.include("../../ext/mbedtls/include"); |
| 130 | conf.file("../../ext/mbedtls/library/sha256.c"); |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 131 | conf.file("csupport/keys.c"); |
| 132 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 133 | conf.file("../../ext/mbedtls/library/asn1parse.c"); |
| 134 | conf.file("../../ext/mbedtls/library/bignum.c"); |
| 135 | conf.file("../../ext/mbedtls/library/ecdsa.c"); |
| 136 | conf.file("../../ext/mbedtls/library/ecp.c"); |
| 137 | conf.file("../../ext/mbedtls/library/ecp_curves.c"); |
| 138 | conf.file("../../ext/mbedtls/library/platform.c"); |
| 139 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 140 | } else if sig_ed25519 { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 141 | conf.conf.define("MCUBOOT_SIGN_ED25519", None); |
| 142 | conf.conf.define("MCUBOOT_USE_TINYCRYPT", None); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 143 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 144 | conf.conf.include("../../ext/tinycrypt/lib/include"); |
| 145 | conf.conf.include("../../ext/tinycrypt-sha512/lib/include"); |
| 146 | conf.conf.include("../../ext/mbedtls/include"); |
Fabio Utzig | a1c142d | 2020-01-03 08:28:11 -0300 | [diff] [blame] | 147 | conf.file("../../ext/tinycrypt/lib/source/sha256.c"); |
| 148 | conf.file("../../ext/tinycrypt-sha512/lib/source/sha512.c"); |
| 149 | conf.file("../../ext/tinycrypt/lib/source/utils.c"); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 150 | conf.file("csupport/keys.c"); |
| 151 | conf.file("../../ext/fiat/src/curve25519.c"); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 152 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
| 153 | conf.file("../../ext/mbedtls/library/asn1parse.c"); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 154 | } else if !enc_ec256 && !enc_x25519 { |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 155 | // No signature type, only sha256 validation. The default |
Marti Bolivar | a4818a5 | 2018-04-12 13:02:38 -0400 | [diff] [blame] | 156 | // configuration file bundled with mbedTLS is sufficient. |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 157 | // When using ECIES-P256 rely on Tinycrypt. |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 158 | conf.conf.define("MCUBOOT_USE_MBED_TLS", None); |
| 159 | conf.conf.include("../../ext/mbedtls/include"); |
| 160 | conf.file("../../ext/mbedtls/library/sha256.c"); |
| 161 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | if overwrite_only { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 165 | conf.conf.define("MCUBOOT_OVERWRITE_ONLY", None); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 166 | } |
| 167 | |
Fabio Utzig | 031eb7d | 2019-11-28 10:13:14 -0300 | [diff] [blame] | 168 | if swap_move { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 169 | conf.conf.define("MCUBOOT_SWAP_USING_MOVE", None); |
| 170 | } else if !overwrite_only { |
| 171 | conf.conf.define("CONFIG_BOOT_SWAP_USING_SCRATCH", None); |
| 172 | conf.conf.define("MCUBOOT_SWAP_USING_SCRATCH", None); |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 173 | } |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 174 | |
Roman Okhrimenko | 13f79ed | 2021-03-11 19:05:41 +0200 | [diff] [blame] | 175 | if swap_status { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 176 | conf.conf.define("MCUBOOT_SWAP_USING_STATUS", None); |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 177 | conf.conf.define("MEMORY_ALIGN", "512"); |
| 178 | conf.conf.define("PLATFORM_MAX_TRAILER_PAGE_SIZE", "512"); |
| 179 | conf.conf.define("SLOTS_FOR_IMAGE", "2"); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 180 | conf.conf.file("../../boot/bootutil/src/swap_status.c"); |
| 181 | conf.conf.file("../../boot/bootutil/src/swap_status_part.c"); |
| 182 | conf.conf.file("../../boot/bootutil/src/swap_status_misc.c"); |
| 183 | conf.conf.file("../../boot/bootutil/src/crc32c.c"); |
Fabio Utzig | 031eb7d | 2019-11-28 10:13:14 -0300 | [diff] [blame] | 184 | } |
| 185 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 186 | if enc_rsa || enc_aes256_rsa { |
| 187 | if enc_aes256_rsa { |
| 188 | conf.conf.define("MCUBOOT_AES_256", None); |
| 189 | } |
| 190 | conf.conf.define("MCUBOOT_ENCRYPT_RSA", None); |
| 191 | conf.conf.define("MCUBOOT_ENC_IMAGES", None); |
| 192 | conf.conf.define("MCUBOOT_USE_MBED_TLS", None); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 193 | |
| 194 | conf.file("../../boot/bootutil/src/encrypted.c"); |
| 195 | conf.file("csupport/keys.c"); |
| 196 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 197 | conf.conf.include("../../ext/mbedtls/include"); |
| 198 | conf.conf.include("../../ext/mbedtls/library"); |
| 199 | conf.file("../../ext/mbedtls/library/sha256.c"); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 200 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 201 | conf.file("../../ext/mbedtls/library/platform.c"); |
| 202 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
| 203 | conf.file("../../ext/mbedtls/library/rsa.c"); |
| 204 | conf.file("../../ext/mbedtls/library/rsa_alt_helpers.c"); |
| 205 | conf.file("../../ext/mbedtls/library/md.c"); |
| 206 | conf.file("../../ext/mbedtls/library/aes.c"); |
| 207 | conf.file("../../ext/mbedtls/library/bignum.c"); |
| 208 | conf.file("../../ext/mbedtls/library/asn1parse.c"); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 209 | } |
| 210 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 211 | if enc_kw || enc_aes256_kw { |
| 212 | if enc_aes256_kw { |
| 213 | conf.conf.define("MCUBOOT_AES_256", None); |
| 214 | } |
| 215 | conf.conf.define("MCUBOOT_ENCRYPT_KW", None); |
| 216 | conf.conf.define("MCUBOOT_ENC_IMAGES", None); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 217 | |
| 218 | conf.file("../../boot/bootutil/src/encrypted.c"); |
| 219 | conf.file("csupport/keys.c"); |
| 220 | |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 221 | if sig_rsa || sig_rsa3072 { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 222 | conf.file("../../ext/mbedtls/library/sha256.c"); |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 223 | } |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 224 | |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 225 | /* Simulator uses Mbed-TLS to wrap keys */ |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 226 | conf.conf.include("../../ext/mbedtls/include"); |
| 227 | conf.file("../../ext/mbedtls/library/platform.c"); |
| 228 | conf.conf.include("../../ext/mbedtls/library"); |
| 229 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
| 230 | conf.file("../../ext/mbedtls/library/nist_kw.c"); |
| 231 | conf.file("../../ext/mbedtls/library/cipher.c"); |
| 232 | conf.file("../../ext/mbedtls/library/cipher_wrap.c"); |
| 233 | conf.file("../../ext/mbedtls/library/aes.c"); |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 234 | |
| 235 | if sig_ecdsa { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 236 | conf.conf.define("MCUBOOT_USE_TINYCRYPT", None); |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 237 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 238 | conf.conf.include("../../ext/tinycrypt/lib/include"); |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 239 | |
| 240 | conf.file("../../ext/tinycrypt/lib/source/utils.c"); |
| 241 | conf.file("../../ext/tinycrypt/lib/source/sha256.c"); |
| 242 | conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c"); |
| 243 | conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c"); |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 244 | conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c"); |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 245 | } |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 246 | |
| 247 | if sig_ed25519 { |
| 248 | panic!("ed25519 does not support image encryption with KW yet"); |
| 249 | } |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 250 | } |
| 251 | |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 252 | if enc_ec256 { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 253 | conf.conf.define("MCUBOOT_ENCRYPT_EC256", None); |
| 254 | conf.conf.define("MCUBOOT_ENC_IMAGES", None); |
| 255 | conf.conf.define("MCUBOOT_USE_TINYCRYPT", None); |
| 256 | conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 257 | |
| 258 | conf.file("../../boot/bootutil/src/encrypted.c"); |
| 259 | conf.file("csupport/keys.c"); |
| 260 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 261 | conf.conf.include("../../ext/mbedtls/include"); |
| 262 | conf.conf.include("../../ext/tinycrypt/lib/include"); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 263 | |
| 264 | /* FIXME: fail with other signature schemes ? */ |
| 265 | |
| 266 | conf.file("../../ext/tinycrypt/lib/source/utils.c"); |
| 267 | conf.file("../../ext/tinycrypt/lib/source/sha256.c"); |
| 268 | conf.file("../../ext/tinycrypt/lib/source/ecc.c"); |
| 269 | conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c"); |
| 270 | conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c"); |
| 271 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 272 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
| 273 | conf.file("../../ext/mbedtls/library/asn1parse.c"); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 274 | |
| 275 | conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c"); |
| 276 | conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c"); |
| 277 | conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c"); |
| 278 | conf.file("../../ext/tinycrypt/lib/source/hmac.c"); |
| 279 | conf.file("../../ext/tinycrypt/lib/source/ecc_dh.c"); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 280 | } else if enc_ec256_mbedtls || enc_aes256_ec256 { |
| 281 | if enc_aes256_ec256 { |
| 282 | conf.conf.define("MCUBOOT_AES_256", None); |
| 283 | } |
| 284 | conf.conf.define("MCUBOOT_ENCRYPT_EC256", None); |
| 285 | conf.conf.define("MCUBOOT_ENC_IMAGES", None); |
| 286 | conf.conf.define("MCUBOOT_USE_MBED_TLS", None); |
| 287 | conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None); |
| 288 | |
| 289 | conf.conf.include("../../ext/mbedtls/include"); |
| 290 | |
| 291 | conf.file("../../boot/bootutil/src/encrypted.c"); |
| 292 | conf.file("../../ext/mbedtls/library/sha256.c"); |
| 293 | conf.file("../../ext/mbedtls/library/asn1parse.c"); |
| 294 | conf.file("../../ext/mbedtls/library/bignum.c"); |
| 295 | conf.file("../../ext/mbedtls/library/ecdh.c"); |
| 296 | conf.file("../../ext/mbedtls/library/md.c"); |
| 297 | conf.file("../../ext/mbedtls/library/aes.c"); |
| 298 | conf.file("../../ext/mbedtls/library/ecp.c"); |
| 299 | conf.file("../../ext/mbedtls/library/ecp_curves.c"); |
| 300 | conf.file("../../ext/mbedtls/library/platform.c"); |
| 301 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
| 302 | conf.file("csupport/keys.c"); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 303 | } |
| 304 | |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 305 | if enc_x25519 { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 306 | conf.conf.define("MCUBOOT_ENCRYPT_X25519", None); |
| 307 | conf.conf.define("MCUBOOT_ENC_IMAGES", None); |
| 308 | conf.conf.define("MCUBOOT_USE_TINYCRYPT", None); |
| 309 | conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 310 | |
| 311 | conf.file("../../boot/bootutil/src/encrypted.c"); |
| 312 | conf.file("csupport/keys.c"); |
| 313 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 314 | conf.conf.include("../../ext/mbedtls/include"); |
| 315 | conf.conf.include("../../ext/tinycrypt/lib/include"); |
| 316 | conf.conf.include("../../ext/tinycrypt-sha512/lib/include"); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 317 | |
| 318 | conf.file("../../ext/fiat/src/curve25519.c"); |
| 319 | |
| 320 | conf.file("../../ext/tinycrypt/lib/source/utils.c"); |
| 321 | conf.file("../../ext/tinycrypt/lib/source/sha256.c"); |
| 322 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 323 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
| 324 | conf.file("../../ext/mbedtls/library/asn1parse.c"); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 325 | |
| 326 | conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c"); |
| 327 | conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c"); |
| 328 | conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c"); |
| 329 | conf.file("../../ext/tinycrypt/lib/source/hmac.c"); |
| 330 | } |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 331 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 332 | else if enc_aes256_x25519 { |
| 333 | conf.conf.define("MCUBOOT_AES_256", None); |
| 334 | conf.conf.define("MCUBOOT_ENCRYPT_X25519", None); |
| 335 | conf.conf.define("MCUBOOT_ENC_IMAGES", None); |
| 336 | conf.conf.define("MCUBOOT_USE_MBED_TLS", None); |
| 337 | conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None); |
| 338 | |
| 339 | conf.file("../../boot/bootutil/src/encrypted.c"); |
| 340 | conf.file("csupport/keys.c"); |
| 341 | |
| 342 | conf.conf.include("../../ext/mbedtls/include"); |
| 343 | conf.file("../../ext/fiat/src/curve25519.c"); |
| 344 | conf.file("../../ext/mbedtls/library/asn1parse.c"); |
| 345 | conf.file("../../ext/mbedtls/library/platform.c"); |
| 346 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
| 347 | conf.file("../../ext/mbedtls/library/aes.c"); |
| 348 | conf.file("../../ext/mbedtls/library/sha256.c"); |
| 349 | conf.file("../../ext/mbedtls/library/md.c"); |
| 350 | conf.file("../../ext/mbedtls/library/sha512.c"); |
| 351 | } |
| 352 | |
Fabio Utzig | 251ef1d | 2018-12-18 17:20:19 -0200 | [diff] [blame] | 353 | if sig_rsa && enc_kw { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 354 | conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa-kw.h>")); |
| 355 | } else if sig_rsa || sig_rsa3072 || enc_rsa || enc_aes256_rsa { |
| 356 | conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>")); |
| 357 | } else if sig_ecdsa_mbedtls || enc_ec256_mbedtls || enc_aes256_ec256 { |
| 358 | conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ec.h>")); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 359 | } else if (sig_ecdsa || enc_ec256) && !enc_kw { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 360 | conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>")); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 361 | } else if sig_ed25519 || enc_x25519 { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 362 | conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>")); |
| 363 | } else if enc_kw || enc_aes256_kw { |
| 364 | conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>")); |
| 365 | } else if enc_aes256_x25519 { |
| 366 | conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ed25519.h>")); |
Fabio Utzig | 04fd63e | 2018-12-14 06:43:31 -0200 | [diff] [blame] | 367 | } |
| 368 | |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 369 | conf.file("../../boot/bootutil/src/image_validate.c"); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 370 | if sig_rsa || sig_rsa3072 { |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 371 | conf.file("../../boot/bootutil/src/image_rsa.c"); |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 372 | } else if sig_ecdsa || sig_ecdsa_mbedtls { |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 373 | conf.conf.include("../../ext/mbedtls/include"); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 374 | conf.file("../../boot/bootutil/src/image_ec256.c"); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 375 | } else if sig_ed25519 { |
| 376 | conf.file("../../boot/bootutil/src/image_ed25519.c"); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 377 | } |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 378 | conf.file("../../boot/bootutil/src/loader.c"); |
Fabio Utzig | 031eb7d | 2019-11-28 10:13:14 -0300 | [diff] [blame] | 379 | conf.file("../../boot/bootutil/src/swap_misc.c"); |
| 380 | conf.file("../../boot/bootutil/src/swap_scratch.c"); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 381 | conf.file("../../boot/bootutil/src/swap_move.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 382 | conf.file("../../boot/bootutil/src/caps.c"); |
| 383 | conf.file("../../boot/bootutil/src/bootutil_misc.c"); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 384 | conf.file("../../boot/bootutil/src/bootutil_public.c"); |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 385 | conf.file("../../boot/bootutil/src/tlv.c"); |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 386 | conf.file("../../boot/bootutil/src/fault_injection_hardening.c"); |
David Brown | d2b1853 | 2017-07-12 09:51:31 -0600 | [diff] [blame] | 387 | conf.file("csupport/run.c"); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 388 | conf.conf.include("../../boot/bootutil/include"); |
Roman Okhrimenko | dc0ca08 | 2023-06-21 20:49:51 +0300 | [diff] [blame^] | 389 | conf.conf.include("../../boot/boot/bootutil/include/bootutil/fault_injection_hardening.h"); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 390 | conf.conf.include("csupport"); |
| 391 | conf.conf.include("../../boot/zephyr/include"); |
| 392 | conf.conf.debug(true); |
| 393 | conf.conf.flag("-Wall"); |
| 394 | conf.conf.flag("-Werror"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 395 | |
Fabio Utzig | 0bccf9d | 2017-12-07 12:13:57 -0200 | [diff] [blame] | 396 | // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90. |
| 397 | // It has incomplete std=c11 and std=c99 support but std=c99 was checked |
| 398 | // to build correctly so leaving it here to updated in the future... |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 399 | conf.conf.flag("-std=c99"); |
Fabio Utzig | 0bccf9d | 2017-12-07 12:13:57 -0200 | [diff] [blame] | 400 | |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 401 | conf.conf.compile("libbootutil.a"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 402 | |
| 403 | walk_dir("../../boot").unwrap(); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 404 | walk_dir("../../ext/tinycrypt/lib/source").unwrap(); |
David Brown | b748f6f | 2019-10-11 10:07:31 -0600 | [diff] [blame] | 405 | walk_dir("../../ext/mbedtls-asn1").unwrap(); |
David Brown | d2b1853 | 2017-07-12 09:51:31 -0600 | [diff] [blame] | 406 | walk_dir("csupport").unwrap(); |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 407 | walk_dir("../../ext/mbedtls/include").unwrap(); |
| 408 | walk_dir("../../ext/mbedtls/library").unwrap(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | // Output the names of all files within a directory so that Cargo knows when to rebuild. |
| 412 | fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> { |
| 413 | for ent in fs::read_dir(path.as_ref())? { |
| 414 | let ent = ent?; |
| 415 | let p = ent.path(); |
| 416 | if p.is_dir() { |
| 417 | walk_dir(p)?; |
| 418 | } else { |
| 419 | // Note that non-utf8 names will fail. |
| 420 | let name = p.to_str().unwrap(); |
| 421 | if name.ends_with(".c") || name.ends_with(".h") { |
| 422 | println!("cargo:rerun-if-changed={}", name); |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | Ok(()) |
| 428 | } |
Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame] | 429 | |
| 430 | /// Wrap the cc::Build type so that we can make sure that files are only added a single time. |
| 431 | /// Other methods can be passed through as needed. |
| 432 | struct CachedBuild { |
| 433 | conf: cc::Build, |
| 434 | seen: BTreeSet<PathBuf>, |
| 435 | } |
| 436 | |
| 437 | impl CachedBuild { |
| 438 | fn new() -> CachedBuild { |
| 439 | CachedBuild { |
| 440 | conf: cc::Build::new(), |
| 441 | seen: BTreeSet::new(), |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | /// Works like `file` in the Build, but doesn't add a file if the same path has already been |
| 446 | /// given. |
| 447 | fn file<P: AsRef<Path>>(&mut self, p: P) -> &mut CachedBuild { |
| 448 | let p = p.as_ref(); |
| 449 | if !self.seen.contains(p) { |
| 450 | self.conf.file(p); |
| 451 | self.seen.insert(p.to_owned()); |
| 452 | } |
| 453 | self |
| 454 | } |
| 455 | } |