blob: 1a3f8b52985c4c9d8f04684dc8783203b36022b6 [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
5use std::env;
6use std::fs;
7use std::io;
8use std::path::Path;
9
10fn main() {
11 // Feature flags.
12 let sig_rsa = env::var("CARGO_FEATURE_SIG_RSA").is_ok();
Fabio Utzig39297432019-05-08 18:51:10 -030013 let sig_rsa3072 = env::var("CARGO_FEATURE_SIG_RSA3072").is_ok();
David Brown63902772017-07-12 09:47:49 -060014 let sig_ecdsa = env::var("CARGO_FEATURE_SIG_ECDSA").is_ok();
David Brown641af452021-02-19 12:16:48 -070015 let sig_ecdsa_mbedtls = env::var("CARGO_FEATURE_SIG_ECDSA_MBEDTLS").is_ok();
Fabio Utzig97710282019-05-24 17:44:49 -030016 let sig_ed25519 = env::var("CARGO_FEATURE_SIG_ED25519").is_ok();
David Brown63902772017-07-12 09:47:49 -060017 let overwrite_only = env::var("CARGO_FEATURE_OVERWRITE_ONLY").is_ok();
Fabio Utzig031eb7d2019-11-28 10:13:14 -030018 let swap_move = env::var("CARGO_FEATURE_SWAP_MOVE").is_ok();
David Vincze2d736ad2019-02-18 11:50:22 +010019 let validate_primary_slot =
20 env::var("CARGO_FEATURE_VALIDATE_PRIMARY_SLOT").is_ok();
Fabio Utzig1e48b912018-09-18 09:04:18 -030021 let enc_rsa = env::var("CARGO_FEATURE_ENC_RSA").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010022 let enc_aes256_rsa = env::var("CARGO_FEATURE_ENC_AES256_RSA").is_ok();
Fabio Utzig1e48b912018-09-18 09:04:18 -030023 let enc_kw = env::var("CARGO_FEATURE_ENC_KW").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010024 let enc_aes256_kw = env::var("CARGO_FEATURE_ENC_AES256_KW").is_ok();
Fabio Utzig90f449e2019-10-24 07:43:53 -030025 let enc_ec256 = env::var("CARGO_FEATURE_ENC_EC256").is_ok();
Fabio Utzig6c553d62021-05-06 19:56:18 -030026 let enc_ec256_mbedtls = env::var("CARGO_FEATURE_ENC_EC256_MBEDTLS").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010027 let enc_aes256_ec256 = env::var("CARGO_FEATURE_ENC_AES256_EC256").is_ok();
Fabio Utzig3fa72ca2020-04-02 11:20:37 -030028 let enc_x25519 = env::var("CARGO_FEATURE_ENC_X25519").is_ok();
Salome Thirot6fdbf552021-05-14 16:46:14 +010029 let enc_aes256_x25519 = env::var("CARGO_FEATURE_ENC_AES256_X25519").is_ok();
Fabio Utzig9b97b132018-12-18 17:21:51 -020030 let bootstrap = env::var("CARGO_FEATURE_BOOTSTRAP").is_ok();
David Brown5e6f5e02019-04-04 10:50:05 +070031 let multiimage = env::var("CARGO_FEATURE_MULTIIMAGE").is_ok();
David Brown2ee5f7f2020-01-13 14:04:01 -070032 let downgrade_prevention = env::var("CARGO_FEATURE_DOWNGRADE_PREVENTION").is_ok();
David Brown7e377ab2021-05-26 16:33:39 -060033 let ram_load = env::var("CARGO_FEATURE_RAM_LOAD").is_ok();
David Brown11ffa0a2021-05-26 17:10:47 -060034 let direct_xip = env::var("CARGO_FEATURE_DIRECT_XIP").is_ok();
David Brown63902772017-07-12 09:47:49 -060035
Fabio Utzig455cad52018-10-15 14:36:33 -070036 let mut conf = cc::Build::new();
David Brown63902772017-07-12 09:47:49 -060037 conf.define("__BOOTSIM__", None);
Fabio Utzig08fcfe92018-11-26 10:18:18 -020038 conf.define("MCUBOOT_HAVE_LOGGING", None);
David Brown63902772017-07-12 09:47:49 -060039 conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None);
Marti Bolivar248da082018-04-24 15:11:39 -040040 conf.define("MCUBOOT_HAVE_ASSERT_H", None);
Marti Bolivarf9bfddd2018-04-24 14:28:33 -040041 conf.define("MCUBOOT_MAX_IMG_SECTORS", Some("128"));
David Brown5e6f5e02019-04-04 10:50:05 +070042 conf.define("MCUBOOT_IMAGE_NUMBER", Some(if multiimage { "2" } else { "1" }));
Fabio Utzigebdc9692017-11-23 16:28:25 -020043
David Brown2ee5f7f2020-01-13 14:04:01 -070044 if downgrade_prevention && !overwrite_only {
45 panic!("Downgrade prevention requires overwrite only");
46 }
47
Fabio Utzig9b97b132018-12-18 17:21:51 -020048 if bootstrap {
49 conf.define("MCUBOOT_BOOTSTRAP", None);
Fabio Utzig3c9d5c42020-10-04 10:12:53 -030050 conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None);
Fabio Utzig9b97b132018-12-18 17:21:51 -020051 }
52
David Vincze2d736ad2019-02-18 11:50:22 +010053 if validate_primary_slot {
54 conf.define("MCUBOOT_VALIDATE_PRIMARY_SLOT", None);
Fabio Utzigebdc9692017-11-23 16:28:25 -020055 }
David Brown63902772017-07-12 09:47:49 -060056
David Brown2ee5f7f2020-01-13 14:04:01 -070057 if downgrade_prevention {
58 conf.define("MCUBOOT_DOWNGRADE_PREVENTION", None);
59 }
60
David Brown7e377ab2021-05-26 16:33:39 -060061 if ram_load {
62 conf.define("MCUBOOT_RAM_LOAD", None);
David Brown7e377ab2021-05-26 16:33:39 -060063 }
64
David Brown11ffa0a2021-05-26 17:10:47 -060065 if direct_xip {
66 conf.define("MCUBOOT_DIRECT_XIP", None);
67 }
68
Fabio Utzig39297432019-05-08 18:51:10 -030069 // Currently no more than one sig type can be used simultaneously.
Fabio Utzig97710282019-05-24 17:44:49 -030070 if vec![sig_rsa, sig_rsa3072, sig_ecdsa, sig_ed25519].iter()
Fabio Utzig39297432019-05-08 18:51:10 -030071 .fold(0, |sum, &v| sum + v as i32) > 1 {
72 panic!("mcuboot does not support more than one sig type at the same time");
David Brown704ac6f2017-07-12 10:14:47 -060073 }
David Brown63902772017-07-12 09:47:49 -060074
Fabio Utzig39297432019-05-08 18:51:10 -030075 if sig_rsa || sig_rsa3072 {
David Brown63902772017-07-12 09:47:49 -060076 conf.define("MCUBOOT_SIGN_RSA", None);
Fabio Utzig39297432019-05-08 18:51:10 -030077 // The Kconfig style defines must be added here as well because
78 // they are used internally by "config-rsa.h"
79 if sig_rsa {
80 conf.define("MCUBOOT_SIGN_RSA_LEN", "2048");
Fabio Utzig46268532020-01-04 21:12:55 -030081 conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "2048");
Fabio Utzig39297432019-05-08 18:51:10 -030082 } else {
83 conf.define("MCUBOOT_SIGN_RSA_LEN", "3072");
Fabio Utzig46268532020-01-04 21:12:55 -030084 conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "3072");
Fabio Utzig39297432019-05-08 18:51:10 -030085 }
David Brown63902772017-07-12 09:47:49 -060086 conf.define("MCUBOOT_USE_MBED_TLS", None);
87
Sherry Zhangf4580cb2021-07-13 22:07:31 +080088 conf.include("../../ext/mbedtls/include");
89 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzig806af0e2018-04-26 10:53:54 -030090 conf.file("csupport/keys.c");
David Brown63902772017-07-12 09:47:49 -060091
Sherry Zhangf4580cb2021-07-13 22:07:31 +080092 conf.file("../../ext/mbedtls/library/rsa.c");
93 conf.file("../../ext/mbedtls/library/bignum.c");
94 conf.file("../../ext/mbedtls/library/platform.c");
95 conf.file("../../ext/mbedtls/library/platform_util.c");
96 conf.file("../../ext/mbedtls/library/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060097 } else if sig_ecdsa {
Fabio Utzigc7865402017-12-05 08:50:52 -020098 conf.define("MCUBOOT_SIGN_EC256", None);
David Brown63902772017-07-12 09:47:49 -060099 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigc7865402017-12-05 08:50:52 -0200100
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200101 if !enc_kw {
David Brownb748f6f2019-10-11 10:07:31 -0600102 conf.include("../../ext/mbedtls-asn1/include");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200103 }
Fabio Utzigc7865402017-12-05 08:50:52 -0200104 conf.include("../../ext/tinycrypt/lib/include");
105
Fabio Utzig806af0e2018-04-26 10:53:54 -0300106 conf.file("csupport/keys.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200107
108 conf.file("../../ext/tinycrypt/lib/source/utils.c");
109 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
110 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
111 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
112 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800113 conf.include("../../ext/mbedtls/library");
David Brownb748f6f2019-10-11 10:07:31 -0600114 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
115 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
David Brown641af452021-02-19 12:16:48 -0700116 } else if sig_ecdsa_mbedtls {
117 conf.define("MCUBOOT_SIGN_EC256", None);
118 conf.define("MCUBOOT_USE_MBED_TLS", None);
119
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800120 conf.include("../../ext/mbedtls/include");
121 conf.file("../../ext/mbedtls/library/sha256.c");
David Brown641af452021-02-19 12:16:48 -0700122 conf.file("csupport/keys.c");
123
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800124 conf.file("../../ext/mbedtls/library/asn1parse.c");
125 conf.file("../../ext/mbedtls/library/bignum.c");
126 conf.file("../../ext/mbedtls/library/ecdsa.c");
127 conf.file("../../ext/mbedtls/library/ecp.c");
128 conf.file("../../ext/mbedtls/library/ecp_curves.c");
129 conf.file("../../ext/mbedtls/library/platform.c");
130 conf.file("../../ext/mbedtls/library/platform_util.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300131 } else if sig_ed25519 {
132 conf.define("MCUBOOT_SIGN_ED25519", None);
Fabio Utziga1c142d2020-01-03 08:28:11 -0300133 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzig97710282019-05-24 17:44:49 -0300134
Fabio Utziga1c142d2020-01-03 08:28:11 -0300135 conf.include("../../ext/tinycrypt/lib/include");
136 conf.include("../../ext/tinycrypt-sha512/lib/include");
137 conf.include("../../ext/mbedtls-asn1/include");
138 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
139 conf.file("../../ext/tinycrypt-sha512/lib/source/sha512.c");
140 conf.file("../../ext/tinycrypt/lib/source/utils.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300141 conf.file("csupport/keys.c");
142 conf.file("../../ext/fiat/src/curve25519.c");
Fabio Utziga1c142d2020-01-03 08:28:11 -0300143 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
144 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300145 } else if !enc_ec256 && !enc_x25519 {
Fabio Utzig90f449e2019-10-24 07:43:53 -0300146 // No signature type, only sha256 validation. The default
Marti Bolivara4818a52018-04-12 13:02:38 -0400147 // configuration file bundled with mbedTLS is sufficient.
Fabio Utzig90f449e2019-10-24 07:43:53 -0300148 // When using ECIES-P256 rely on Tinycrypt.
David Brown704ac6f2017-07-12 10:14:47 -0600149 conf.define("MCUBOOT_USE_MBED_TLS", None);
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800150 conf.include("../../ext/mbedtls/include");
151 conf.file("../../ext/mbedtls/library/sha256.c");
152 conf.file("../../ext/mbedtls/library/platform_util.c");
David Brown63902772017-07-12 09:47:49 -0600153 }
154
155 if overwrite_only {
156 conf.define("MCUBOOT_OVERWRITE_ONLY", None);
157 }
158
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300159 if swap_move {
160 conf.define("MCUBOOT_SWAP_USING_MOVE", None);
Andrzej Puzdrowski137d7972021-05-13 13:39:30 +0200161 } else if !overwrite_only {
162 conf.define("CONFIG_BOOT_SWAP_USING_SCRATCH", None);
163 conf.define("MCUBOOT_SWAP_USING_SCRATCH", None);
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300164 }
165
Salome Thirot6fdbf552021-05-14 16:46:14 +0100166 if enc_rsa || enc_aes256_rsa {
167 if enc_aes256_rsa {
168 conf.define("MCUBOOT_AES_256", None);
169 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300170 conf.define("MCUBOOT_ENCRYPT_RSA", None);
171 conf.define("MCUBOOT_ENC_IMAGES", None);
172 conf.define("MCUBOOT_USE_MBED_TLS", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300173
174 conf.file("../../boot/bootutil/src/encrypted.c");
175 conf.file("csupport/keys.c");
176
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800177 conf.include("../../ext/mbedtls/include");
178 conf.include("../../ext/mbedtls/library");
179 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300180
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800181 conf.file("../../ext/mbedtls/library/platform.c");
182 conf.file("../../ext/mbedtls/library/platform_util.c");
183 conf.file("../../ext/mbedtls/library/rsa.c");
184 conf.file("../../ext/mbedtls/library/rsa_alt_helpers.c");
185 conf.file("../../ext/mbedtls/library/md.c");
186 conf.file("../../ext/mbedtls/library/aes.c");
187 conf.file("../../ext/mbedtls/library/bignum.c");
188 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300189 }
190
Salome Thirot6fdbf552021-05-14 16:46:14 +0100191 if enc_kw || enc_aes256_kw {
192 if enc_aes256_kw {
193 conf.define("MCUBOOT_AES_256", None);
194 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300195 conf.define("MCUBOOT_ENCRYPT_KW", None);
196 conf.define("MCUBOOT_ENC_IMAGES", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300197
198 conf.file("../../boot/bootutil/src/encrypted.c");
199 conf.file("csupport/keys.c");
200
Fabio Utzig39297432019-05-08 18:51:10 -0300201 if sig_rsa || sig_rsa3072 {
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800202 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200203 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300204
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200205 /* Simulator uses Mbed-TLS to wrap keys */
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800206 conf.include("../../ext/mbedtls/include");
207 conf.file("../../ext/mbedtls/library/platform.c");
208 conf.include("../../ext/mbedtls/library");
209 conf.file("../../ext/mbedtls/library/platform_util.c");
210 conf.file("../../ext/mbedtls/library/nist_kw.c");
211 conf.file("../../ext/mbedtls/library/cipher.c");
212 conf.file("../../ext/mbedtls/library/cipher_wrap.c");
213 conf.file("../../ext/mbedtls/library/aes.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200214
215 if sig_ecdsa {
216 conf.define("MCUBOOT_USE_TINYCRYPT", None);
217
218 conf.include("../../ext/tinycrypt/lib/include");
219
220 conf.file("../../ext/tinycrypt/lib/source/utils.c");
221 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
222 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
223 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900224 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200225 }
Fabio Utzig97710282019-05-24 17:44:49 -0300226
227 if sig_ed25519 {
228 panic!("ed25519 does not support image encryption with KW yet");
229 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300230 }
231
Fabio Utzig90f449e2019-10-24 07:43:53 -0300232 if enc_ec256 {
233 conf.define("MCUBOOT_ENCRYPT_EC256", None);
234 conf.define("MCUBOOT_ENC_IMAGES", None);
235 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzig4b4ed982020-01-06 09:09:51 -0300236 conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Fabio Utzig90f449e2019-10-24 07:43:53 -0300237
238 conf.file("../../boot/bootutil/src/encrypted.c");
239 conf.file("csupport/keys.c");
240
241 conf.include("../../ext/mbedtls-asn1/include");
242 conf.include("../../ext/tinycrypt/lib/include");
243
244 /* FIXME: fail with other signature schemes ? */
245
246 conf.file("../../ext/tinycrypt/lib/source/utils.c");
247 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
248 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
249 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
250 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
251
252 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
253 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
254
255 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
256 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
257 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
258 conf.file("../../ext/tinycrypt/lib/source/hmac.c");
259 conf.file("../../ext/tinycrypt/lib/source/ecc_dh.c");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100260 } else if enc_ec256_mbedtls || enc_aes256_ec256 {
261 if enc_aes256_ec256 {
262 conf.define("MCUBOOT_AES_256", None);
263 }
Fabio Utzig6c553d62021-05-06 19:56:18 -0300264 conf.define("MCUBOOT_ENCRYPT_EC256", None);
265 conf.define("MCUBOOT_ENC_IMAGES", None);
266 conf.define("MCUBOOT_USE_MBED_TLS", None);
267 conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
268
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800269 conf.include("../../ext/mbedtls/include");
Fabio Utzig6c553d62021-05-06 19:56:18 -0300270
271 conf.file("../../boot/bootutil/src/encrypted.c");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800272 conf.file("../../ext/mbedtls/library/sha256.c");
273 conf.file("../../ext/mbedtls/library/asn1parse.c");
274 conf.file("../../ext/mbedtls/library/bignum.c");
275 conf.file("../../ext/mbedtls/library/ecdh.c");
276 conf.file("../../ext/mbedtls/library/md.c");
277 conf.file("../../ext/mbedtls/library/aes.c");
278 conf.file("../../ext/mbedtls/library/ecp.c");
279 conf.file("../../ext/mbedtls/library/ecp_curves.c");
280 conf.file("../../ext/mbedtls/library/platform.c");
281 conf.file("../../ext/mbedtls/library/platform_util.c");
Fabio Utzig6c553d62021-05-06 19:56:18 -0300282 conf.file("csupport/keys.c");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300283 }
284
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300285 if enc_x25519 {
286 conf.define("MCUBOOT_ENCRYPT_X25519", None);
287 conf.define("MCUBOOT_ENC_IMAGES", None);
288 conf.define("MCUBOOT_USE_TINYCRYPT", None);
289 conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
290
291 conf.file("../../boot/bootutil/src/encrypted.c");
292 conf.file("csupport/keys.c");
293
294 conf.include("../../ext/mbedtls-asn1/include");
295 conf.include("../../ext/tinycrypt/lib/include");
296 conf.include("../../ext/tinycrypt-sha512/lib/include");
297
298 conf.file("../../ext/fiat/src/curve25519.c");
299
300 conf.file("../../ext/tinycrypt/lib/source/utils.c");
301 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
302
303 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
304 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
305
306 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
307 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
308 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
309 conf.file("../../ext/tinycrypt/lib/source/hmac.c");
310 }
Fabio Utzig90f449e2019-10-24 07:43:53 -0300311
Salome Thirot6fdbf552021-05-14 16:46:14 +0100312 else if enc_aes256_x25519 {
313 conf.define("MCUBOOT_AES_256", None);
314 conf.define("MCUBOOT_ENCRYPT_X25519", None);
315 conf.define("MCUBOOT_ENC_IMAGES", None);
316 conf.define("MCUBOOT_USE_MBED_TLS", None);
317 conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
318
319 conf.file("../../boot/bootutil/src/encrypted.c");
320 conf.file("csupport/keys.c");
321
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800322 conf.include("../../ext/mbedtls/include");
323 conf.include("../../ext/mbedtls-asn1/include");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100324 conf.file("../../ext/fiat/src/curve25519.c");
325 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
326 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800327 conf.file("../../ext/mbedtls/library/platform.c");
328 conf.file("../../ext/mbedtls/library/platform_util.c");
329 conf.file("../../ext/mbedtls/library/aes.c");
330 conf.file("../../ext/mbedtls/library/sha256.c");
331 conf.file("../../ext/mbedtls/library/md.c");
332 conf.file("../../ext/mbedtls/library/sha512.c");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100333 }
334
Fabio Utzig251ef1d2018-12-18 17:20:19 -0200335 if sig_rsa && enc_kw {
336 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa-kw.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100337 } else if sig_rsa || sig_rsa3072 || enc_rsa || enc_aes256_rsa {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200338 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100339 } else if sig_ecdsa_mbedtls || enc_ec256_mbedtls || enc_aes256_ec256 {
Fabio Utzig6c553d62021-05-06 19:56:18 -0300340 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ec.h>"));
Fabio Utzig90f449e2019-10-24 07:43:53 -0300341 } else if (sig_ecdsa || enc_ec256) && !enc_kw {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200342 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300343 } else if sig_ed25519 || enc_x25519 {
Fabio Utziga1c142d2020-01-03 08:28:11 -0300344 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100345 } else if enc_kw || enc_aes256_kw {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200346 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100347 } else if enc_aes256_x25519 {
348 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ed25519.h>"));
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200349 }
350
David Brown704ac6f2017-07-12 10:14:47 -0600351 conf.file("../../boot/bootutil/src/image_validate.c");
Fabio Utzig39297432019-05-08 18:51:10 -0300352 if sig_rsa || sig_rsa3072 {
Fabio Utzigc7865402017-12-05 08:50:52 -0200353 conf.file("../../boot/bootutil/src/image_rsa.c");
David Brown641af452021-02-19 12:16:48 -0700354 } else if sig_ecdsa || sig_ecdsa_mbedtls {
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800355 conf.include("../../ext/mbedtls/include");
Fabio Utzigc7865402017-12-05 08:50:52 -0200356 conf.file("../../boot/bootutil/src/image_ec256.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300357 } else if sig_ed25519 {
358 conf.file("../../boot/bootutil/src/image_ed25519.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200359 }
David Brown63902772017-07-12 09:47:49 -0600360 conf.file("../../boot/bootutil/src/loader.c");
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300361 conf.file("../../boot/bootutil/src/swap_misc.c");
362 conf.file("../../boot/bootutil/src/swap_scratch.c");
363 conf.file("../../boot/bootutil/src/swap_move.c");
David Brown63902772017-07-12 09:47:49 -0600364 conf.file("../../boot/bootutil/src/caps.c");
365 conf.file("../../boot/bootutil/src/bootutil_misc.c");
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100366 conf.file("../../boot/bootutil/src/bootutil_public.c");
Fabio Utzig61fd8882019-09-14 20:00:20 -0300367 conf.file("../../boot/bootutil/src/tlv.c");
Raef Colese8fe6cf2020-05-26 13:07:40 +0100368 conf.file("../../boot/bootutil/src/fault_injection_hardening.c");
David Brownd2b18532017-07-12 09:51:31 -0600369 conf.file("csupport/run.c");
David Brown63902772017-07-12 09:47:49 -0600370 conf.include("../../boot/bootutil/include");
Fabio Utzig57c40f72017-12-12 21:48:30 -0200371 conf.include("csupport");
Fabio Utzig9a4b9ba2018-05-07 08:31:27 -0300372 conf.include("../../boot/zephyr/include");
David Brown63902772017-07-12 09:47:49 -0600373 conf.debug(true);
374 conf.flag("-Wall");
David Brown0b693c02017-07-12 12:34:33 -0600375 conf.flag("-Werror");
David Brown63902772017-07-12 09:47:49 -0600376
Fabio Utzig0bccf9d2017-12-07 12:13:57 -0200377 // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90.
378 // It has incomplete std=c11 and std=c99 support but std=c99 was checked
379 // to build correctly so leaving it here to updated in the future...
380 conf.flag("-std=c99");
381
David Brown63902772017-07-12 09:47:49 -0600382 conf.compile("libbootutil.a");
383
384 walk_dir("../../boot").unwrap();
Fabio Utzigc7865402017-12-05 08:50:52 -0200385 walk_dir("../../ext/tinycrypt/lib/source").unwrap();
David Brownb748f6f2019-10-11 10:07:31 -0600386 walk_dir("../../ext/mbedtls-asn1").unwrap();
David Brownd2b18532017-07-12 09:51:31 -0600387 walk_dir("csupport").unwrap();
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800388 walk_dir("../../ext/mbedtls/include").unwrap();
389 walk_dir("../../ext/mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -0600390}
391
392// Output the names of all files within a directory so that Cargo knows when to rebuild.
393fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
394 for ent in fs::read_dir(path.as_ref())? {
395 let ent = ent?;
396 let p = ent.path();
397 if p.is_dir() {
398 walk_dir(p)?;
399 } else {
400 // Note that non-utf8 names will fail.
401 let name = p.to_str().unwrap();
402 if name.ends_with(".c") || name.ends_with(".h") {
403 println!("cargo:rerun-if-changed={}", name);
404 }
405 }
406 }
407
408 Ok(())
409}