blob: f2649d3ee9eed69407d73fe1365ef403e9b3a7b7 [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);
63
64 conf.define("IMAGE_EXECUTABLE_RAM_START", "0x10000");
65 conf.define("IMAGE_EXECUTABLE_RAM_SIZE", "0x10000");
66 }
67
David Brown11ffa0a2021-05-26 17:10:47 -060068 if direct_xip {
69 conf.define("MCUBOOT_DIRECT_XIP", None);
70 }
71
Fabio Utzig39297432019-05-08 18:51:10 -030072 // Currently no more than one sig type can be used simultaneously.
Fabio Utzig97710282019-05-24 17:44:49 -030073 if vec![sig_rsa, sig_rsa3072, sig_ecdsa, sig_ed25519].iter()
Fabio Utzig39297432019-05-08 18:51:10 -030074 .fold(0, |sum, &v| sum + v as i32) > 1 {
75 panic!("mcuboot does not support more than one sig type at the same time");
David Brown704ac6f2017-07-12 10:14:47 -060076 }
David Brown63902772017-07-12 09:47:49 -060077
Fabio Utzig39297432019-05-08 18:51:10 -030078 if sig_rsa || sig_rsa3072 {
David Brown63902772017-07-12 09:47:49 -060079 conf.define("MCUBOOT_SIGN_RSA", None);
Fabio Utzig39297432019-05-08 18:51:10 -030080 // The Kconfig style defines must be added here as well because
81 // they are used internally by "config-rsa.h"
82 if sig_rsa {
83 conf.define("MCUBOOT_SIGN_RSA_LEN", "2048");
Fabio Utzig46268532020-01-04 21:12:55 -030084 conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "2048");
Fabio Utzig39297432019-05-08 18:51:10 -030085 } else {
86 conf.define("MCUBOOT_SIGN_RSA_LEN", "3072");
Fabio Utzig46268532020-01-04 21:12:55 -030087 conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "3072");
Fabio Utzig39297432019-05-08 18:51:10 -030088 }
David Brown63902772017-07-12 09:47:49 -060089 conf.define("MCUBOOT_USE_MBED_TLS", None);
90
Sherry Zhangf4580cb2021-07-13 22:07:31 +080091 conf.include("../../ext/mbedtls/include");
92 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzig806af0e2018-04-26 10:53:54 -030093 conf.file("csupport/keys.c");
David Brown63902772017-07-12 09:47:49 -060094
Sherry Zhangf4580cb2021-07-13 22:07:31 +080095 conf.file("../../ext/mbedtls/library/rsa.c");
96 conf.file("../../ext/mbedtls/library/bignum.c");
97 conf.file("../../ext/mbedtls/library/platform.c");
98 conf.file("../../ext/mbedtls/library/platform_util.c");
99 conf.file("../../ext/mbedtls/library/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -0600100 } else if sig_ecdsa {
Fabio Utzigc7865402017-12-05 08:50:52 -0200101 conf.define("MCUBOOT_SIGN_EC256", None);
David Brown63902772017-07-12 09:47:49 -0600102 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigc7865402017-12-05 08:50:52 -0200103
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200104 if !enc_kw {
David Brownb748f6f2019-10-11 10:07:31 -0600105 conf.include("../../ext/mbedtls-asn1/include");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200106 }
Fabio Utzigc7865402017-12-05 08:50:52 -0200107 conf.include("../../ext/tinycrypt/lib/include");
108
Fabio Utzig806af0e2018-04-26 10:53:54 -0300109 conf.file("csupport/keys.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200110
111 conf.file("../../ext/tinycrypt/lib/source/utils.c");
112 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
113 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
114 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
115 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800116 conf.include("../../ext/mbedtls/library");
David Brownb748f6f2019-10-11 10:07:31 -0600117 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
118 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
David Brown641af452021-02-19 12:16:48 -0700119 } else if sig_ecdsa_mbedtls {
120 conf.define("MCUBOOT_SIGN_EC256", None);
121 conf.define("MCUBOOT_USE_MBED_TLS", None);
122
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800123 conf.include("../../ext/mbedtls/include");
124 conf.file("../../ext/mbedtls/library/sha256.c");
David Brown641af452021-02-19 12:16:48 -0700125 conf.file("csupport/keys.c");
126
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800127 conf.file("../../ext/mbedtls/library/asn1parse.c");
128 conf.file("../../ext/mbedtls/library/bignum.c");
129 conf.file("../../ext/mbedtls/library/ecdsa.c");
130 conf.file("../../ext/mbedtls/library/ecp.c");
131 conf.file("../../ext/mbedtls/library/ecp_curves.c");
132 conf.file("../../ext/mbedtls/library/platform.c");
133 conf.file("../../ext/mbedtls/library/platform_util.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300134 } else if sig_ed25519 {
135 conf.define("MCUBOOT_SIGN_ED25519", None);
Fabio Utziga1c142d2020-01-03 08:28:11 -0300136 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzig97710282019-05-24 17:44:49 -0300137
Fabio Utziga1c142d2020-01-03 08:28:11 -0300138 conf.include("../../ext/tinycrypt/lib/include");
139 conf.include("../../ext/tinycrypt-sha512/lib/include");
140 conf.include("../../ext/mbedtls-asn1/include");
141 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
142 conf.file("../../ext/tinycrypt-sha512/lib/source/sha512.c");
143 conf.file("../../ext/tinycrypt/lib/source/utils.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300144 conf.file("csupport/keys.c");
145 conf.file("../../ext/fiat/src/curve25519.c");
Fabio Utziga1c142d2020-01-03 08:28:11 -0300146 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
147 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300148 } else if !enc_ec256 && !enc_x25519 {
Fabio Utzig90f449e2019-10-24 07:43:53 -0300149 // No signature type, only sha256 validation. The default
Marti Bolivara4818a52018-04-12 13:02:38 -0400150 // configuration file bundled with mbedTLS is sufficient.
Fabio Utzig90f449e2019-10-24 07:43:53 -0300151 // When using ECIES-P256 rely on Tinycrypt.
David Brown704ac6f2017-07-12 10:14:47 -0600152 conf.define("MCUBOOT_USE_MBED_TLS", None);
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800153 conf.include("../../ext/mbedtls/include");
154 conf.file("../../ext/mbedtls/library/sha256.c");
155 conf.file("../../ext/mbedtls/library/platform_util.c");
David Brown63902772017-07-12 09:47:49 -0600156 }
157
158 if overwrite_only {
159 conf.define("MCUBOOT_OVERWRITE_ONLY", None);
160 }
161
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300162 if swap_move {
163 conf.define("MCUBOOT_SWAP_USING_MOVE", None);
Andrzej Puzdrowski137d7972021-05-13 13:39:30 +0200164 } else if !overwrite_only {
165 conf.define("CONFIG_BOOT_SWAP_USING_SCRATCH", None);
166 conf.define("MCUBOOT_SWAP_USING_SCRATCH", None);
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300167 }
168
Salome Thirot6fdbf552021-05-14 16:46:14 +0100169 if enc_rsa || enc_aes256_rsa {
170 if enc_aes256_rsa {
171 conf.define("MCUBOOT_AES_256", None);
172 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300173 conf.define("MCUBOOT_ENCRYPT_RSA", None);
174 conf.define("MCUBOOT_ENC_IMAGES", None);
175 conf.define("MCUBOOT_USE_MBED_TLS", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300176
177 conf.file("../../boot/bootutil/src/encrypted.c");
178 conf.file("csupport/keys.c");
179
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800180 conf.include("../../ext/mbedtls/include");
181 conf.include("../../ext/mbedtls/library");
182 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300183
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800184 conf.file("../../ext/mbedtls/library/platform.c");
185 conf.file("../../ext/mbedtls/library/platform_util.c");
186 conf.file("../../ext/mbedtls/library/rsa.c");
187 conf.file("../../ext/mbedtls/library/rsa_alt_helpers.c");
188 conf.file("../../ext/mbedtls/library/md.c");
189 conf.file("../../ext/mbedtls/library/aes.c");
190 conf.file("../../ext/mbedtls/library/bignum.c");
191 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300192 }
193
Salome Thirot6fdbf552021-05-14 16:46:14 +0100194 if enc_kw || enc_aes256_kw {
195 if enc_aes256_kw {
196 conf.define("MCUBOOT_AES_256", None);
197 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300198 conf.define("MCUBOOT_ENCRYPT_KW", None);
199 conf.define("MCUBOOT_ENC_IMAGES", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300200
201 conf.file("../../boot/bootutil/src/encrypted.c");
202 conf.file("csupport/keys.c");
203
Fabio Utzig39297432019-05-08 18:51:10 -0300204 if sig_rsa || sig_rsa3072 {
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800205 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200206 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300207
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200208 /* Simulator uses Mbed-TLS to wrap keys */
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800209 conf.include("../../ext/mbedtls/include");
210 conf.file("../../ext/mbedtls/library/platform.c");
211 conf.include("../../ext/mbedtls/library");
212 conf.file("../../ext/mbedtls/library/platform_util.c");
213 conf.file("../../ext/mbedtls/library/nist_kw.c");
214 conf.file("../../ext/mbedtls/library/cipher.c");
215 conf.file("../../ext/mbedtls/library/cipher_wrap.c");
216 conf.file("../../ext/mbedtls/library/aes.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200217
218 if sig_ecdsa {
219 conf.define("MCUBOOT_USE_TINYCRYPT", None);
220
221 conf.include("../../ext/tinycrypt/lib/include");
222
223 conf.file("../../ext/tinycrypt/lib/source/utils.c");
224 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
225 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
226 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900227 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200228 }
Fabio Utzig97710282019-05-24 17:44:49 -0300229
230 if sig_ed25519 {
231 panic!("ed25519 does not support image encryption with KW yet");
232 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300233 }
234
Fabio Utzig90f449e2019-10-24 07:43:53 -0300235 if enc_ec256 {
236 conf.define("MCUBOOT_ENCRYPT_EC256", None);
237 conf.define("MCUBOOT_ENC_IMAGES", None);
238 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzig4b4ed982020-01-06 09:09:51 -0300239 conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Fabio Utzig90f449e2019-10-24 07:43:53 -0300240
241 conf.file("../../boot/bootutil/src/encrypted.c");
242 conf.file("csupport/keys.c");
243
244 conf.include("../../ext/mbedtls-asn1/include");
245 conf.include("../../ext/tinycrypt/lib/include");
246
247 /* FIXME: fail with other signature schemes ? */
248
249 conf.file("../../ext/tinycrypt/lib/source/utils.c");
250 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
251 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
252 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
253 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
254
255 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
256 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
257
258 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
259 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
260 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
261 conf.file("../../ext/tinycrypt/lib/source/hmac.c");
262 conf.file("../../ext/tinycrypt/lib/source/ecc_dh.c");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100263 } else if enc_ec256_mbedtls || enc_aes256_ec256 {
264 if enc_aes256_ec256 {
265 conf.define("MCUBOOT_AES_256", None);
266 }
Fabio Utzig6c553d62021-05-06 19:56:18 -0300267 conf.define("MCUBOOT_ENCRYPT_EC256", None);
268 conf.define("MCUBOOT_ENC_IMAGES", None);
269 conf.define("MCUBOOT_USE_MBED_TLS", None);
270 conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
271
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800272 conf.include("../../ext/mbedtls/include");
Fabio Utzig6c553d62021-05-06 19:56:18 -0300273
274 conf.file("../../boot/bootutil/src/encrypted.c");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800275 conf.file("../../ext/mbedtls/library/sha256.c");
276 conf.file("../../ext/mbedtls/library/asn1parse.c");
277 conf.file("../../ext/mbedtls/library/bignum.c");
278 conf.file("../../ext/mbedtls/library/ecdh.c");
279 conf.file("../../ext/mbedtls/library/md.c");
280 conf.file("../../ext/mbedtls/library/aes.c");
281 conf.file("../../ext/mbedtls/library/ecp.c");
282 conf.file("../../ext/mbedtls/library/ecp_curves.c");
283 conf.file("../../ext/mbedtls/library/platform.c");
284 conf.file("../../ext/mbedtls/library/platform_util.c");
Fabio Utzig6c553d62021-05-06 19:56:18 -0300285 conf.file("csupport/keys.c");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300286 }
287
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300288 if enc_x25519 {
289 conf.define("MCUBOOT_ENCRYPT_X25519", None);
290 conf.define("MCUBOOT_ENC_IMAGES", None);
291 conf.define("MCUBOOT_USE_TINYCRYPT", None);
292 conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
293
294 conf.file("../../boot/bootutil/src/encrypted.c");
295 conf.file("csupport/keys.c");
296
297 conf.include("../../ext/mbedtls-asn1/include");
298 conf.include("../../ext/tinycrypt/lib/include");
299 conf.include("../../ext/tinycrypt-sha512/lib/include");
300
301 conf.file("../../ext/fiat/src/curve25519.c");
302
303 conf.file("../../ext/tinycrypt/lib/source/utils.c");
304 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
305
306 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
307 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
308
309 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
310 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
311 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
312 conf.file("../../ext/tinycrypt/lib/source/hmac.c");
313 }
Fabio Utzig90f449e2019-10-24 07:43:53 -0300314
Salome Thirot6fdbf552021-05-14 16:46:14 +0100315 else if enc_aes256_x25519 {
316 conf.define("MCUBOOT_AES_256", None);
317 conf.define("MCUBOOT_ENCRYPT_X25519", None);
318 conf.define("MCUBOOT_ENC_IMAGES", None);
319 conf.define("MCUBOOT_USE_MBED_TLS", None);
320 conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
321
322 conf.file("../../boot/bootutil/src/encrypted.c");
323 conf.file("csupport/keys.c");
324
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800325 conf.include("../../ext/mbedtls/include");
326 conf.include("../../ext/mbedtls-asn1/include");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100327 conf.file("../../ext/fiat/src/curve25519.c");
328 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
329 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800330 conf.file("../../ext/mbedtls/library/platform.c");
331 conf.file("../../ext/mbedtls/library/platform_util.c");
332 conf.file("../../ext/mbedtls/library/aes.c");
333 conf.file("../../ext/mbedtls/library/sha256.c");
334 conf.file("../../ext/mbedtls/library/md.c");
335 conf.file("../../ext/mbedtls/library/sha512.c");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100336 }
337
Fabio Utzig251ef1d2018-12-18 17:20:19 -0200338 if sig_rsa && enc_kw {
339 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa-kw.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100340 } else if sig_rsa || sig_rsa3072 || enc_rsa || enc_aes256_rsa {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200341 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100342 } else if sig_ecdsa_mbedtls || enc_ec256_mbedtls || enc_aes256_ec256 {
Fabio Utzig6c553d62021-05-06 19:56:18 -0300343 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ec.h>"));
Fabio Utzig90f449e2019-10-24 07:43:53 -0300344 } else if (sig_ecdsa || enc_ec256) && !enc_kw {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200345 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300346 } else if sig_ed25519 || enc_x25519 {
Fabio Utziga1c142d2020-01-03 08:28:11 -0300347 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100348 } else if enc_kw || enc_aes256_kw {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200349 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100350 } else if enc_aes256_x25519 {
351 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ed25519.h>"));
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200352 }
353
David Brown704ac6f2017-07-12 10:14:47 -0600354 conf.file("../../boot/bootutil/src/image_validate.c");
Fabio Utzig39297432019-05-08 18:51:10 -0300355 if sig_rsa || sig_rsa3072 {
Fabio Utzigc7865402017-12-05 08:50:52 -0200356 conf.file("../../boot/bootutil/src/image_rsa.c");
David Brown641af452021-02-19 12:16:48 -0700357 } else if sig_ecdsa || sig_ecdsa_mbedtls {
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800358 conf.include("../../ext/mbedtls/include");
Fabio Utzigc7865402017-12-05 08:50:52 -0200359 conf.file("../../boot/bootutil/src/image_ec256.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300360 } else if sig_ed25519 {
361 conf.file("../../boot/bootutil/src/image_ed25519.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200362 }
David Brown63902772017-07-12 09:47:49 -0600363 conf.file("../../boot/bootutil/src/loader.c");
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300364 conf.file("../../boot/bootutil/src/swap_misc.c");
365 conf.file("../../boot/bootutil/src/swap_scratch.c");
366 conf.file("../../boot/bootutil/src/swap_move.c");
David Brown63902772017-07-12 09:47:49 -0600367 conf.file("../../boot/bootutil/src/caps.c");
368 conf.file("../../boot/bootutil/src/bootutil_misc.c");
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100369 conf.file("../../boot/bootutil/src/bootutil_public.c");
Fabio Utzig61fd8882019-09-14 20:00:20 -0300370 conf.file("../../boot/bootutil/src/tlv.c");
Raef Colese8fe6cf2020-05-26 13:07:40 +0100371 conf.file("../../boot/bootutil/src/fault_injection_hardening.c");
David Brownd2b18532017-07-12 09:51:31 -0600372 conf.file("csupport/run.c");
David Brown63902772017-07-12 09:47:49 -0600373 conf.include("../../boot/bootutil/include");
Fabio Utzig57c40f72017-12-12 21:48:30 -0200374 conf.include("csupport");
Fabio Utzig9a4b9ba2018-05-07 08:31:27 -0300375 conf.include("../../boot/zephyr/include");
David Brown63902772017-07-12 09:47:49 -0600376 conf.debug(true);
377 conf.flag("-Wall");
David Brown0b693c02017-07-12 12:34:33 -0600378 conf.flag("-Werror");
David Brown63902772017-07-12 09:47:49 -0600379
Fabio Utzig0bccf9d2017-12-07 12:13:57 -0200380 // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90.
381 // It has incomplete std=c11 and std=c99 support but std=c99 was checked
382 // to build correctly so leaving it here to updated in the future...
383 conf.flag("-std=c99");
384
David Brown63902772017-07-12 09:47:49 -0600385 conf.compile("libbootutil.a");
386
387 walk_dir("../../boot").unwrap();
Fabio Utzigc7865402017-12-05 08:50:52 -0200388 walk_dir("../../ext/tinycrypt/lib/source").unwrap();
David Brownb748f6f2019-10-11 10:07:31 -0600389 walk_dir("../../ext/mbedtls-asn1").unwrap();
David Brownd2b18532017-07-12 09:51:31 -0600390 walk_dir("csupport").unwrap();
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800391 walk_dir("../../ext/mbedtls/include").unwrap();
392 walk_dir("../../ext/mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -0600393}
394
395// Output the names of all files within a directory so that Cargo knows when to rebuild.
396fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
397 for ent in fs::read_dir(path.as_ref())? {
398 let ent = ent?;
399 let p = ent.path();
400 if p.is_dir() {
401 walk_dir(p)?;
402 } else {
403 // Note that non-utf8 names will fail.
404 let name = p.to_str().unwrap();
405 if name.ends_with(".c") || name.ends_with(".h") {
406 println!("cargo:rerun-if-changed={}", name);
407 }
408 }
409 }
410
411 Ok(())
412}