blob: a01844e4287761bae18e311096b05f6f15e77d31 [file] [log] [blame]
David Brown63902772017-07-12 09:47:49 -06001// Build mcuboot as a library, based on the requested features.
2
Fabio Utzig455cad52018-10-15 14:36:33 -07003extern crate cc;
David Brown63902772017-07-12 09:47:49 -06004
David Brown5f4e1482021-09-16 16:44:09 -06005use std::collections::BTreeSet;
David Brown63902772017-07-12 09:47:49 -06006use std::env;
7use std::fs;
8use std::io;
David Brown5f4e1482021-09-16 16:44:09 -06009use std::path::{Path, PathBuf};
David Brown63902772017-07-12 09:47:49 -060010
11fn main() {
12 // Feature flags.
13 let sig_rsa = env::var("CARGO_FEATURE_SIG_RSA").is_ok();
Fabio Utzig39297432019-05-08 18:51:10 -030014 let sig_rsa3072 = env::var("CARGO_FEATURE_SIG_RSA3072").is_ok();
David Brown63902772017-07-12 09:47:49 -060015 let sig_ecdsa = env::var("CARGO_FEATURE_SIG_ECDSA").is_ok();
David Brown641af452021-02-19 12:16:48 -070016 let sig_ecdsa_mbedtls = env::var("CARGO_FEATURE_SIG_ECDSA_MBEDTLS").is_ok();
Fabio Utzig97710282019-05-24 17:44:49 -030017 let sig_ed25519 = env::var("CARGO_FEATURE_SIG_ED25519").is_ok();
David Brown63902772017-07-12 09:47:49 -060018 let overwrite_only = env::var("CARGO_FEATURE_OVERWRITE_ONLY").is_ok();
Fabio Utzig031eb7d2019-11-28 10:13:14 -030019 let swap_move = env::var("CARGO_FEATURE_SWAP_MOVE").is_ok();
David Vincze2d736ad2019-02-18 11:50:22 +010020 let validate_primary_slot =
21 env::var("CARGO_FEATURE_VALIDATE_PRIMARY_SLOT").is_ok();
Fabio Utzig1e48b912018-09-18 09:04:18 -030022 let enc_rsa = env::var("CARGO_FEATURE_ENC_RSA").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010023 let enc_aes256_rsa = env::var("CARGO_FEATURE_ENC_AES256_RSA").is_ok();
Fabio Utzig1e48b912018-09-18 09:04:18 -030024 let enc_kw = env::var("CARGO_FEATURE_ENC_KW").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010025 let enc_aes256_kw = env::var("CARGO_FEATURE_ENC_AES256_KW").is_ok();
Fabio Utzig90f449e2019-10-24 07:43:53 -030026 let enc_ec256 = env::var("CARGO_FEATURE_ENC_EC256").is_ok();
Fabio Utzig6c553d62021-05-06 19:56:18 -030027 let enc_ec256_mbedtls = env::var("CARGO_FEATURE_ENC_EC256_MBEDTLS").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010028 let enc_aes256_ec256 = env::var("CARGO_FEATURE_ENC_AES256_EC256").is_ok();
Fabio Utzig3fa72ca2020-04-02 11:20:37 -030029 let enc_x25519 = env::var("CARGO_FEATURE_ENC_X25519").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010030 let enc_aes256_x25519 = env::var("CARGO_FEATURE_ENC_AES256_X25519").is_ok();
Fabio Utzig9b97b132018-12-18 17:21:51 -020031 let bootstrap = env::var("CARGO_FEATURE_BOOTSTRAP").is_ok();
David Brown5e6f5e02019-04-04 10:50:05 +070032 let multiimage = env::var("CARGO_FEATURE_MULTIIMAGE").is_ok();
David Brown2ee5f7f2020-01-13 14:04:01 -070033 let downgrade_prevention = env::var("CARGO_FEATURE_DOWNGRADE_PREVENTION").is_ok();
David Brown7e377ab2021-05-26 16:33:39 -060034 let ram_load = env::var("CARGO_FEATURE_RAM_LOAD").is_ok();
David Brown11ffa0a2021-05-26 17:10:47 -060035 let direct_xip = env::var("CARGO_FEATURE_DIRECT_XIP").is_ok();
David Brown1bc106e2021-12-16 13:23:52 -070036 let max_align_32 = env::var("CARGO_FEATURE_MAX_ALIGN_32").is_ok();
Roland Mikheld6703522023-04-27 14:24:30 +020037 let hw_rollback_protection = env::var("CARGO_FEATURE_HW_ROLLBACK_PROTECTION").is_ok();
David Brown63902772017-07-12 09:47:49 -060038
David Brown5f4e1482021-09-16 16:44:09 -060039 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"));
Gustavo Henrique Nihei7bfd14b2021-11-24 23:27:22 -030045
David Brown1bc106e2021-12-16 13:23:52 -070046 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 }
Gustavo Henrique Nihei7bfd14b2021-11-24 23:27:22 -030051
David Brown5f4e1482021-09-16 16:44:09 -060052 conf.conf.define("MCUBOOT_IMAGE_NUMBER", Some(if multiimage { "2" } else { "1" }));
Fabio Utzigebdc9692017-11-23 16:28:25 -020053
David Brown2ee5f7f2020-01-13 14:04:01 -070054 if downgrade_prevention && !overwrite_only {
55 panic!("Downgrade prevention requires overwrite only");
56 }
57
Fabio Utzig9b97b132018-12-18 17:21:51 -020058 if bootstrap {
David Brown5f4e1482021-09-16 16:44:09 -060059 conf.conf.define("MCUBOOT_BOOTSTRAP", None);
60 conf.conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None);
Fabio Utzig9b97b132018-12-18 17:21:51 -020061 }
62
David Vincze2d736ad2019-02-18 11:50:22 +010063 if validate_primary_slot {
David Brown5f4e1482021-09-16 16:44:09 -060064 conf.conf.define("MCUBOOT_VALIDATE_PRIMARY_SLOT", None);
Fabio Utzigebdc9692017-11-23 16:28:25 -020065 }
David Brown63902772017-07-12 09:47:49 -060066
David Brown2ee5f7f2020-01-13 14:04:01 -070067 if downgrade_prevention {
David Brown5f4e1482021-09-16 16:44:09 -060068 conf.conf.define("MCUBOOT_DOWNGRADE_PREVENTION", None);
David Brown2ee5f7f2020-01-13 14:04:01 -070069 }
70
David Brown7e377ab2021-05-26 16:33:39 -060071 if ram_load {
David Brown5f4e1482021-09-16 16:44:09 -060072 conf.conf.define("MCUBOOT_RAM_LOAD", None);
David Brown7e377ab2021-05-26 16:33:39 -060073 }
74
David Brown11ffa0a2021-05-26 17:10:47 -060075 if direct_xip {
David Brown5f4e1482021-09-16 16:44:09 -060076 conf.conf.define("MCUBOOT_DIRECT_XIP", None);
David Brown11ffa0a2021-05-26 17:10:47 -060077 }
78
Roland Mikheld6703522023-04-27 14:24:30 +020079 if hw_rollback_protection {
80 conf.conf.define("MCUBOOT_HW_ROLLBACK_PROT", None);
81 conf.file("csupport/security_cnt.c");
82 }
83
Fabio Utzig39297432019-05-08 18:51:10 -030084 // Currently no more than one sig type can be used simultaneously.
Fabio Utzig97710282019-05-24 17:44:49 -030085 if vec![sig_rsa, sig_rsa3072, sig_ecdsa, sig_ed25519].iter()
Fabio Utzig39297432019-05-08 18:51:10 -030086 .fold(0, |sum, &v| sum + v as i32) > 1 {
87 panic!("mcuboot does not support more than one sig type at the same time");
David Brown704ac6f2017-07-12 10:14:47 -060088 }
David Brown63902772017-07-12 09:47:49 -060089
Fabio Utzig39297432019-05-08 18:51:10 -030090 if sig_rsa || sig_rsa3072 {
David Brown5f4e1482021-09-16 16:44:09 -060091 conf.conf.define("MCUBOOT_SIGN_RSA", None);
Fabio Utzig39297432019-05-08 18:51:10 -030092 // The Kconfig style defines must be added here as well because
93 // they are used internally by "config-rsa.h"
94 if sig_rsa {
David Brown5f4e1482021-09-16 16:44:09 -060095 conf.conf.define("MCUBOOT_SIGN_RSA_LEN", "2048");
96 conf.conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "2048");
Fabio Utzig39297432019-05-08 18:51:10 -030097 } else {
David Brown5f4e1482021-09-16 16:44:09 -060098 conf.conf.define("MCUBOOT_SIGN_RSA_LEN", "3072");
99 conf.conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "3072");
Fabio Utzig39297432019-05-08 18:51:10 -0300100 }
David Brown5f4e1482021-09-16 16:44:09 -0600101 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
David Brown63902772017-07-12 09:47:49 -0600102
David Brown5f4e1482021-09-16 16:44:09 -0600103 conf.conf.include("../../ext/mbedtls/include");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800104 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzig806af0e2018-04-26 10:53:54 -0300105 conf.file("csupport/keys.c");
David Brown63902772017-07-12 09:47:49 -0600106
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800107 conf.file("../../ext/mbedtls/library/rsa.c");
108 conf.file("../../ext/mbedtls/library/bignum.c");
109 conf.file("../../ext/mbedtls/library/platform.c");
110 conf.file("../../ext/mbedtls/library/platform_util.c");
111 conf.file("../../ext/mbedtls/library/asn1parse.c");
Antonio de Angelis02bf0722022-11-22 15:35:43 +0000112 conf.file("../../ext/mbedtls/library/md.c");
113
David Brown704ac6f2017-07-12 10:14:47 -0600114 } else if sig_ecdsa {
David Brown5f4e1482021-09-16 16:44:09 -0600115 conf.conf.define("MCUBOOT_SIGN_EC256", None);
116 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigc7865402017-12-05 08:50:52 -0200117
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200118 if !enc_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600119 conf.conf.include("../../ext/mbedtls/include");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200120 }
David Brown5f4e1482021-09-16 16:44:09 -0600121 conf.conf.include("../../ext/tinycrypt/lib/include");
Fabio Utzigc7865402017-12-05 08:50:52 -0200122
Fabio Utzig806af0e2018-04-26 10:53:54 -0300123 conf.file("csupport/keys.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200124
125 conf.file("../../ext/tinycrypt/lib/source/utils.c");
126 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
127 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
128 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
129 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
David Brown5f4e1482021-09-16 16:44:09 -0600130 conf.file("../../ext/mbedtls/library/platform_util.c");
131 conf.file("../../ext/mbedtls/library/asn1parse.c");
David Brown641af452021-02-19 12:16:48 -0700132 } else if sig_ecdsa_mbedtls {
David Brown5f4e1482021-09-16 16:44:09 -0600133 conf.conf.define("MCUBOOT_SIGN_EC256", None);
134 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
David Brown641af452021-02-19 12:16:48 -0700135
David Brown5f4e1482021-09-16 16:44:09 -0600136 conf.conf.include("../../ext/mbedtls/include");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800137 conf.file("../../ext/mbedtls/library/sha256.c");
David Brown641af452021-02-19 12:16:48 -0700138 conf.file("csupport/keys.c");
139
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800140 conf.file("../../ext/mbedtls/library/asn1parse.c");
141 conf.file("../../ext/mbedtls/library/bignum.c");
142 conf.file("../../ext/mbedtls/library/ecdsa.c");
143 conf.file("../../ext/mbedtls/library/ecp.c");
144 conf.file("../../ext/mbedtls/library/ecp_curves.c");
145 conf.file("../../ext/mbedtls/library/platform.c");
146 conf.file("../../ext/mbedtls/library/platform_util.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300147 } else if sig_ed25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600148 conf.conf.define("MCUBOOT_SIGN_ED25519", None);
149 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzig97710282019-05-24 17:44:49 -0300150
David Brown5f4e1482021-09-16 16:44:09 -0600151 conf.conf.include("../../ext/tinycrypt/lib/include");
152 conf.conf.include("../../ext/tinycrypt-sha512/lib/include");
153 conf.conf.include("../../ext/mbedtls/include");
Fabio Utziga1c142d2020-01-03 08:28:11 -0300154 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
155 conf.file("../../ext/tinycrypt-sha512/lib/source/sha512.c");
156 conf.file("../../ext/tinycrypt/lib/source/utils.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300157 conf.file("csupport/keys.c");
158 conf.file("../../ext/fiat/src/curve25519.c");
David Brown5f4e1482021-09-16 16:44:09 -0600159 conf.file("../../ext/mbedtls/library/platform_util.c");
160 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300161 } else if !enc_ec256 && !enc_x25519 {
Fabio Utzig90f449e2019-10-24 07:43:53 -0300162 // No signature type, only sha256 validation. The default
Marti Bolivara4818a52018-04-12 13:02:38 -0400163 // configuration file bundled with mbedTLS is sufficient.
Fabio Utzig90f449e2019-10-24 07:43:53 -0300164 // When using ECIES-P256 rely on Tinycrypt.
David Brown5f4e1482021-09-16 16:44:09 -0600165 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
166 conf.conf.include("../../ext/mbedtls/include");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800167 conf.file("../../ext/mbedtls/library/sha256.c");
168 conf.file("../../ext/mbedtls/library/platform_util.c");
David Brown63902772017-07-12 09:47:49 -0600169 }
170
171 if overwrite_only {
David Brown5f4e1482021-09-16 16:44:09 -0600172 conf.conf.define("MCUBOOT_OVERWRITE_ONLY", None);
David Brown63902772017-07-12 09:47:49 -0600173 }
174
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300175 if swap_move {
David Brown5f4e1482021-09-16 16:44:09 -0600176 conf.conf.define("MCUBOOT_SWAP_USING_MOVE", None);
Andrzej Puzdrowski137d7972021-05-13 13:39:30 +0200177 } else if !overwrite_only {
David Brown5f4e1482021-09-16 16:44:09 -0600178 conf.conf.define("CONFIG_BOOT_SWAP_USING_SCRATCH", None);
179 conf.conf.define("MCUBOOT_SWAP_USING_SCRATCH", None);
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300180 }
181
Salome Thirot6fdbf552021-05-14 16:46:14 +0100182 if enc_rsa || enc_aes256_rsa {
183 if enc_aes256_rsa {
David Brown5f4e1482021-09-16 16:44:09 -0600184 conf.conf.define("MCUBOOT_AES_256", None);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100185 }
David Brown5f4e1482021-09-16 16:44:09 -0600186 conf.conf.define("MCUBOOT_ENCRYPT_RSA", None);
187 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
188 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300189
190 conf.file("../../boot/bootutil/src/encrypted.c");
191 conf.file("csupport/keys.c");
192
David Brown5f4e1482021-09-16 16:44:09 -0600193 conf.conf.include("../../ext/mbedtls/include");
194 conf.conf.include("../../ext/mbedtls/library");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800195 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300196
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800197 conf.file("../../ext/mbedtls/library/platform.c");
198 conf.file("../../ext/mbedtls/library/platform_util.c");
199 conf.file("../../ext/mbedtls/library/rsa.c");
200 conf.file("../../ext/mbedtls/library/rsa_alt_helpers.c");
201 conf.file("../../ext/mbedtls/library/md.c");
202 conf.file("../../ext/mbedtls/library/aes.c");
203 conf.file("../../ext/mbedtls/library/bignum.c");
204 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300205 }
206
Salome Thirot6fdbf552021-05-14 16:46:14 +0100207 if enc_kw || enc_aes256_kw {
208 if enc_aes256_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600209 conf.conf.define("MCUBOOT_AES_256", None);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100210 }
David Brown5f4e1482021-09-16 16:44:09 -0600211 conf.conf.define("MCUBOOT_ENCRYPT_KW", None);
212 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300213
214 conf.file("../../boot/bootutil/src/encrypted.c");
215 conf.file("csupport/keys.c");
216
Fabio Utzig39297432019-05-08 18:51:10 -0300217 if sig_rsa || sig_rsa3072 {
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800218 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200219 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300220
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200221 /* Simulator uses Mbed-TLS to wrap keys */
David Brown5f4e1482021-09-16 16:44:09 -0600222 conf.conf.include("../../ext/mbedtls/include");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800223 conf.file("../../ext/mbedtls/library/platform.c");
David Brown5f4e1482021-09-16 16:44:09 -0600224 conf.conf.include("../../ext/mbedtls/library");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800225 conf.file("../../ext/mbedtls/library/platform_util.c");
226 conf.file("../../ext/mbedtls/library/nist_kw.c");
227 conf.file("../../ext/mbedtls/library/cipher.c");
228 conf.file("../../ext/mbedtls/library/cipher_wrap.c");
229 conf.file("../../ext/mbedtls/library/aes.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200230
231 if sig_ecdsa {
David Brown5f4e1482021-09-16 16:44:09 -0600232 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200233
David Brown5f4e1482021-09-16 16:44:09 -0600234 conf.conf.include("../../ext/tinycrypt/lib/include");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200235
236 conf.file("../../ext/tinycrypt/lib/source/utils.c");
237 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
238 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
239 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900240 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200241 }
Fabio Utzig97710282019-05-24 17:44:49 -0300242
243 if sig_ed25519 {
244 panic!("ed25519 does not support image encryption with KW yet");
245 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300246 }
247
Fabio Utzig90f449e2019-10-24 07:43:53 -0300248 if enc_ec256 {
David Brown5f4e1482021-09-16 16:44:09 -0600249 conf.conf.define("MCUBOOT_ENCRYPT_EC256", None);
250 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
251 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
252 conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Fabio Utzig90f449e2019-10-24 07:43:53 -0300253
254 conf.file("../../boot/bootutil/src/encrypted.c");
255 conf.file("csupport/keys.c");
256
David Brown5f4e1482021-09-16 16:44:09 -0600257 conf.conf.include("../../ext/mbedtls/include");
258 conf.conf.include("../../ext/tinycrypt/lib/include");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300259
260 /* FIXME: fail with other signature schemes ? */
261
262 conf.file("../../ext/tinycrypt/lib/source/utils.c");
263 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
264 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
265 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
266 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
267
David Brown5f4e1482021-09-16 16:44:09 -0600268 conf.file("../../ext/mbedtls/library/platform_util.c");
269 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300270
271 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
272 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
273 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
274 conf.file("../../ext/tinycrypt/lib/source/hmac.c");
275 conf.file("../../ext/tinycrypt/lib/source/ecc_dh.c");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100276 } else if enc_ec256_mbedtls || enc_aes256_ec256 {
277 if enc_aes256_ec256 {
David Brown5f4e1482021-09-16 16:44:09 -0600278 conf.conf.define("MCUBOOT_AES_256", None);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100279 }
David Brown5f4e1482021-09-16 16:44:09 -0600280 conf.conf.define("MCUBOOT_ENCRYPT_EC256", None);
281 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
282 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
283 conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Fabio Utzig6c553d62021-05-06 19:56:18 -0300284
David Brown5f4e1482021-09-16 16:44:09 -0600285 conf.conf.include("../../ext/mbedtls/include");
Fabio Utzig6c553d62021-05-06 19:56:18 -0300286
287 conf.file("../../boot/bootutil/src/encrypted.c");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800288 conf.file("../../ext/mbedtls/library/sha256.c");
289 conf.file("../../ext/mbedtls/library/asn1parse.c");
290 conf.file("../../ext/mbedtls/library/bignum.c");
291 conf.file("../../ext/mbedtls/library/ecdh.c");
292 conf.file("../../ext/mbedtls/library/md.c");
293 conf.file("../../ext/mbedtls/library/aes.c");
294 conf.file("../../ext/mbedtls/library/ecp.c");
295 conf.file("../../ext/mbedtls/library/ecp_curves.c");
296 conf.file("../../ext/mbedtls/library/platform.c");
297 conf.file("../../ext/mbedtls/library/platform_util.c");
Fabio Utzig6c553d62021-05-06 19:56:18 -0300298 conf.file("csupport/keys.c");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300299 }
300
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300301 if enc_x25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600302 conf.conf.define("MCUBOOT_ENCRYPT_X25519", None);
303 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
304 conf.conf.define("MCUBOOT_USE_TINYCRYPT", None);
305 conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300306
307 conf.file("../../boot/bootutil/src/encrypted.c");
308 conf.file("csupport/keys.c");
309
David Brown5f4e1482021-09-16 16:44:09 -0600310 conf.conf.include("../../ext/mbedtls/include");
311 conf.conf.include("../../ext/tinycrypt/lib/include");
312 conf.conf.include("../../ext/tinycrypt-sha512/lib/include");
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300313
314 conf.file("../../ext/fiat/src/curve25519.c");
315
316 conf.file("../../ext/tinycrypt/lib/source/utils.c");
317 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
318
David Brown5f4e1482021-09-16 16:44:09 -0600319 conf.file("../../ext/mbedtls/library/platform_util.c");
320 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300321
322 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
323 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
324 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
325 conf.file("../../ext/tinycrypt/lib/source/hmac.c");
326 }
Fabio Utzig90f449e2019-10-24 07:43:53 -0300327
Salome Thirot6fdbf552021-05-14 16:46:14 +0100328 else if enc_aes256_x25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600329 conf.conf.define("MCUBOOT_AES_256", None);
330 conf.conf.define("MCUBOOT_ENCRYPT_X25519", None);
331 conf.conf.define("MCUBOOT_ENC_IMAGES", None);
332 conf.conf.define("MCUBOOT_USE_MBED_TLS", None);
333 conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Salome Thirot6fdbf552021-05-14 16:46:14 +0100334
335 conf.file("../../boot/bootutil/src/encrypted.c");
336 conf.file("csupport/keys.c");
337
David Brown5f4e1482021-09-16 16:44:09 -0600338 conf.conf.include("../../ext/mbedtls/include");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100339 conf.file("../../ext/fiat/src/curve25519.c");
David Brown5f4e1482021-09-16 16:44:09 -0600340 conf.file("../../ext/mbedtls/library/asn1parse.c");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800341 conf.file("../../ext/mbedtls/library/platform.c");
342 conf.file("../../ext/mbedtls/library/platform_util.c");
343 conf.file("../../ext/mbedtls/library/aes.c");
344 conf.file("../../ext/mbedtls/library/sha256.c");
345 conf.file("../../ext/mbedtls/library/md.c");
346 conf.file("../../ext/mbedtls/library/sha512.c");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100347 }
348
Fabio Utzig251ef1d2018-12-18 17:20:19 -0200349 if sig_rsa && enc_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600350 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa-kw.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100351 } else if sig_rsa || sig_rsa3072 || enc_rsa || enc_aes256_rsa {
David Brown5f4e1482021-09-16 16:44:09 -0600352 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100353 } else if sig_ecdsa_mbedtls || enc_ec256_mbedtls || enc_aes256_ec256 {
David Brown5f4e1482021-09-16 16:44:09 -0600354 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ec.h>"));
Fabio Utzig90f449e2019-10-24 07:43:53 -0300355 } else if (sig_ecdsa || enc_ec256) && !enc_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600356 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300357 } else if sig_ed25519 || enc_x25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600358 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100359 } else if enc_kw || enc_aes256_kw {
David Brown5f4e1482021-09-16 16:44:09 -0600360 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100361 } else if enc_aes256_x25519 {
David Brown5f4e1482021-09-16 16:44:09 -0600362 conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ed25519.h>"));
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200363 }
364
David Brown704ac6f2017-07-12 10:14:47 -0600365 conf.file("../../boot/bootutil/src/image_validate.c");
Fabio Utzig39297432019-05-08 18:51:10 -0300366 if sig_rsa || sig_rsa3072 {
Fabio Utzigc7865402017-12-05 08:50:52 -0200367 conf.file("../../boot/bootutil/src/image_rsa.c");
David Brown641af452021-02-19 12:16:48 -0700368 } else if sig_ecdsa || sig_ecdsa_mbedtls {
David Brown5f4e1482021-09-16 16:44:09 -0600369 conf.conf.include("../../ext/mbedtls/include");
Antonio de Angelis10529d32023-04-21 21:43:14 +0100370 conf.file("../../boot/bootutil/src/image_ecdsa.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300371 } else if sig_ed25519 {
372 conf.file("../../boot/bootutil/src/image_ed25519.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200373 }
David Brown63902772017-07-12 09:47:49 -0600374 conf.file("../../boot/bootutil/src/loader.c");
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300375 conf.file("../../boot/bootutil/src/swap_misc.c");
376 conf.file("../../boot/bootutil/src/swap_scratch.c");
377 conf.file("../../boot/bootutil/src/swap_move.c");
David Brown63902772017-07-12 09:47:49 -0600378 conf.file("../../boot/bootutil/src/caps.c");
379 conf.file("../../boot/bootutil/src/bootutil_misc.c");
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100380 conf.file("../../boot/bootutil/src/bootutil_public.c");
Fabio Utzig61fd8882019-09-14 20:00:20 -0300381 conf.file("../../boot/bootutil/src/tlv.c");
Raef Colese8fe6cf2020-05-26 13:07:40 +0100382 conf.file("../../boot/bootutil/src/fault_injection_hardening.c");
David Brownd2b18532017-07-12 09:51:31 -0600383 conf.file("csupport/run.c");
David Brown5f4e1482021-09-16 16:44:09 -0600384 conf.conf.include("../../boot/bootutil/include");
385 conf.conf.include("csupport");
David Brown5f4e1482021-09-16 16:44:09 -0600386 conf.conf.debug(true);
387 conf.conf.flag("-Wall");
388 conf.conf.flag("-Werror");
David Brown63902772017-07-12 09:47:49 -0600389
Fabio Utzig0bccf9d2017-12-07 12:13:57 -0200390 // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90.
391 // It has incomplete std=c11 and std=c99 support but std=c99 was checked
392 // to build correctly so leaving it here to updated in the future...
David Brown5f4e1482021-09-16 16:44:09 -0600393 conf.conf.flag("-std=c99");
Fabio Utzig0bccf9d2017-12-07 12:13:57 -0200394
David Brown5f4e1482021-09-16 16:44:09 -0600395 conf.conf.compile("libbootutil.a");
David Brown63902772017-07-12 09:47:49 -0600396
397 walk_dir("../../boot").unwrap();
Fabio Utzigc7865402017-12-05 08:50:52 -0200398 walk_dir("../../ext/tinycrypt/lib/source").unwrap();
David Brownb748f6f2019-10-11 10:07:31 -0600399 walk_dir("../../ext/mbedtls-asn1").unwrap();
David Brownd2b18532017-07-12 09:51:31 -0600400 walk_dir("csupport").unwrap();
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800401 walk_dir("../../ext/mbedtls/include").unwrap();
402 walk_dir("../../ext/mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -0600403}
404
405// Output the names of all files within a directory so that Cargo knows when to rebuild.
406fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
407 for ent in fs::read_dir(path.as_ref())? {
408 let ent = ent?;
409 let p = ent.path();
410 if p.is_dir() {
411 walk_dir(p)?;
412 } else {
413 // Note that non-utf8 names will fail.
414 let name = p.to_str().unwrap();
415 if name.ends_with(".c") || name.ends_with(".h") {
416 println!("cargo:rerun-if-changed={}", name);
417 }
418 }
419 }
420
421 Ok(())
422}
David Brown5f4e1482021-09-16 16:44:09 -0600423
424/// Wrap the cc::Build type so that we can make sure that files are only added a single time.
425/// Other methods can be passed through as needed.
426struct CachedBuild {
427 conf: cc::Build,
428 seen: BTreeSet<PathBuf>,
429}
430
431impl CachedBuild {
432 fn new() -> CachedBuild {
433 CachedBuild {
434 conf: cc::Build::new(),
435 seen: BTreeSet::new(),
436 }
437 }
438
439 /// Works like `file` in the Build, but doesn't add a file if the same path has already been
440 /// given.
441 fn file<P: AsRef<Path>>(&mut self, p: P) -> &mut CachedBuild {
442 let p = p.as_ref();
443 if !self.seen.contains(p) {
444 self.conf.file(p);
445 self.seen.insert(p.to_owned());
446 }
447 self
448 }
449}