blob: 6f16f041b49df06f50f07959e4f5074aed19c355 [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 Brown63902772017-07-12 09:47:49 -060033
Fabio Utzig455cad52018-10-15 14:36:33 -070034 let mut conf = cc::Build::new();
David Brown63902772017-07-12 09:47:49 -060035 conf.define("__BOOTSIM__", None);
Fabio Utzig08fcfe92018-11-26 10:18:18 -020036 conf.define("MCUBOOT_HAVE_LOGGING", None);
David Brown63902772017-07-12 09:47:49 -060037 conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None);
Marti Bolivar248da082018-04-24 15:11:39 -040038 conf.define("MCUBOOT_HAVE_ASSERT_H", None);
Marti Bolivarf9bfddd2018-04-24 14:28:33 -040039 conf.define("MCUBOOT_MAX_IMG_SECTORS", Some("128"));
David Brown5e6f5e02019-04-04 10:50:05 +070040 conf.define("MCUBOOT_IMAGE_NUMBER", Some(if multiimage { "2" } else { "1" }));
Fabio Utzigebdc9692017-11-23 16:28:25 -020041
David Brown2ee5f7f2020-01-13 14:04:01 -070042 if downgrade_prevention && !overwrite_only {
43 panic!("Downgrade prevention requires overwrite only");
44 }
45
Fabio Utzig9b97b132018-12-18 17:21:51 -020046 if bootstrap {
47 conf.define("MCUBOOT_BOOTSTRAP", None);
Fabio Utzig3c9d5c42020-10-04 10:12:53 -030048 conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None);
Fabio Utzig9b97b132018-12-18 17:21:51 -020049 }
50
David Vincze2d736ad2019-02-18 11:50:22 +010051 if validate_primary_slot {
52 conf.define("MCUBOOT_VALIDATE_PRIMARY_SLOT", None);
Fabio Utzigebdc9692017-11-23 16:28:25 -020053 }
David Brown63902772017-07-12 09:47:49 -060054
David Brown2ee5f7f2020-01-13 14:04:01 -070055 if downgrade_prevention {
56 conf.define("MCUBOOT_DOWNGRADE_PREVENTION", None);
57 }
58
Fabio Utzig39297432019-05-08 18:51:10 -030059 // Currently no more than one sig type can be used simultaneously.
Fabio Utzig97710282019-05-24 17:44:49 -030060 if vec![sig_rsa, sig_rsa3072, sig_ecdsa, sig_ed25519].iter()
Fabio Utzig39297432019-05-08 18:51:10 -030061 .fold(0, |sum, &v| sum + v as i32) > 1 {
62 panic!("mcuboot does not support more than one sig type at the same time");
David Brown704ac6f2017-07-12 10:14:47 -060063 }
David Brown63902772017-07-12 09:47:49 -060064
Fabio Utzig39297432019-05-08 18:51:10 -030065 if sig_rsa || sig_rsa3072 {
David Brown63902772017-07-12 09:47:49 -060066 conf.define("MCUBOOT_SIGN_RSA", None);
Fabio Utzig39297432019-05-08 18:51:10 -030067 // The Kconfig style defines must be added here as well because
68 // they are used internally by "config-rsa.h"
69 if sig_rsa {
70 conf.define("MCUBOOT_SIGN_RSA_LEN", "2048");
Fabio Utzig46268532020-01-04 21:12:55 -030071 conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "2048");
Fabio Utzig39297432019-05-08 18:51:10 -030072 } else {
73 conf.define("MCUBOOT_SIGN_RSA_LEN", "3072");
Fabio Utzig46268532020-01-04 21:12:55 -030074 conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "3072");
Fabio Utzig39297432019-05-08 18:51:10 -030075 }
David Brown63902772017-07-12 09:47:49 -060076 conf.define("MCUBOOT_USE_MBED_TLS", None);
77
Sherry Zhangf4580cb2021-07-13 22:07:31 +080078 conf.include("../../ext/mbedtls/include");
79 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzig806af0e2018-04-26 10:53:54 -030080 conf.file("csupport/keys.c");
David Brown63902772017-07-12 09:47:49 -060081
Sherry Zhangf4580cb2021-07-13 22:07:31 +080082 conf.file("../../ext/mbedtls/library/rsa.c");
83 conf.file("../../ext/mbedtls/library/bignum.c");
84 conf.file("../../ext/mbedtls/library/platform.c");
85 conf.file("../../ext/mbedtls/library/platform_util.c");
86 conf.file("../../ext/mbedtls/library/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060087 } else if sig_ecdsa {
Fabio Utzigc7865402017-12-05 08:50:52 -020088 conf.define("MCUBOOT_SIGN_EC256", None);
David Brown63902772017-07-12 09:47:49 -060089 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigc7865402017-12-05 08:50:52 -020090
Fabio Utzigb4d20c82018-12-27 16:08:39 -020091 if !enc_kw {
David Brownb748f6f2019-10-11 10:07:31 -060092 conf.include("../../ext/mbedtls-asn1/include");
Fabio Utzigb4d20c82018-12-27 16:08:39 -020093 }
Fabio Utzigc7865402017-12-05 08:50:52 -020094 conf.include("../../ext/tinycrypt/lib/include");
95
Fabio Utzig806af0e2018-04-26 10:53:54 -030096 conf.file("csupport/keys.c");
Fabio Utzigc7865402017-12-05 08:50:52 -020097
98 conf.file("../../ext/tinycrypt/lib/source/utils.c");
99 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
100 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
101 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
102 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800103 conf.include("../../ext/mbedtls/library");
David Brownb748f6f2019-10-11 10:07:31 -0600104 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
105 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
David Brown641af452021-02-19 12:16:48 -0700106 } else if sig_ecdsa_mbedtls {
107 conf.define("MCUBOOT_SIGN_EC256", None);
108 conf.define("MCUBOOT_USE_MBED_TLS", None);
109
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800110 conf.include("../../ext/mbedtls/include");
111 conf.file("../../ext/mbedtls/library/sha256.c");
David Brown641af452021-02-19 12:16:48 -0700112 conf.file("csupport/keys.c");
113
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800114 conf.file("../../ext/mbedtls/library/asn1parse.c");
115 conf.file("../../ext/mbedtls/library/bignum.c");
116 conf.file("../../ext/mbedtls/library/ecdsa.c");
117 conf.file("../../ext/mbedtls/library/ecp.c");
118 conf.file("../../ext/mbedtls/library/ecp_curves.c");
119 conf.file("../../ext/mbedtls/library/platform.c");
120 conf.file("../../ext/mbedtls/library/platform_util.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300121 } else if sig_ed25519 {
122 conf.define("MCUBOOT_SIGN_ED25519", None);
Fabio Utziga1c142d2020-01-03 08:28:11 -0300123 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzig97710282019-05-24 17:44:49 -0300124
Fabio Utziga1c142d2020-01-03 08:28:11 -0300125 conf.include("../../ext/tinycrypt/lib/include");
126 conf.include("../../ext/tinycrypt-sha512/lib/include");
127 conf.include("../../ext/mbedtls-asn1/include");
128 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
129 conf.file("../../ext/tinycrypt-sha512/lib/source/sha512.c");
130 conf.file("../../ext/tinycrypt/lib/source/utils.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300131 conf.file("csupport/keys.c");
132 conf.file("../../ext/fiat/src/curve25519.c");
Fabio Utziga1c142d2020-01-03 08:28:11 -0300133 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
134 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300135 } else if !enc_ec256 && !enc_x25519 {
Fabio Utzig90f449e2019-10-24 07:43:53 -0300136 // No signature type, only sha256 validation. The default
Marti Bolivara4818a52018-04-12 13:02:38 -0400137 // configuration file bundled with mbedTLS is sufficient.
Fabio Utzig90f449e2019-10-24 07:43:53 -0300138 // When using ECIES-P256 rely on Tinycrypt.
David Brown704ac6f2017-07-12 10:14:47 -0600139 conf.define("MCUBOOT_USE_MBED_TLS", None);
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800140 conf.include("../../ext/mbedtls/include");
141 conf.file("../../ext/mbedtls/library/sha256.c");
142 conf.file("../../ext/mbedtls/library/platform_util.c");
David Brown63902772017-07-12 09:47:49 -0600143 }
144
145 if overwrite_only {
146 conf.define("MCUBOOT_OVERWRITE_ONLY", None);
147 }
148
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300149 if swap_move {
150 conf.define("MCUBOOT_SWAP_USING_MOVE", None);
Andrzej Puzdrowski137d7972021-05-13 13:39:30 +0200151 } else if !overwrite_only {
152 conf.define("CONFIG_BOOT_SWAP_USING_SCRATCH", None);
153 conf.define("MCUBOOT_SWAP_USING_SCRATCH", None);
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300154 }
155
Salome Thirot6fdbf552021-05-14 16:46:14 +0100156 if enc_rsa || enc_aes256_rsa {
157 if enc_aes256_rsa {
158 conf.define("MCUBOOT_AES_256", None);
159 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300160 conf.define("MCUBOOT_ENCRYPT_RSA", None);
161 conf.define("MCUBOOT_ENC_IMAGES", None);
162 conf.define("MCUBOOT_USE_MBED_TLS", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300163
164 conf.file("../../boot/bootutil/src/encrypted.c");
165 conf.file("csupport/keys.c");
166
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800167 conf.include("../../ext/mbedtls/include");
168 conf.include("../../ext/mbedtls/library");
169 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300170
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800171 conf.file("../../ext/mbedtls/library/platform.c");
172 conf.file("../../ext/mbedtls/library/platform_util.c");
173 conf.file("../../ext/mbedtls/library/rsa.c");
174 conf.file("../../ext/mbedtls/library/rsa_alt_helpers.c");
175 conf.file("../../ext/mbedtls/library/md.c");
176 conf.file("../../ext/mbedtls/library/aes.c");
177 conf.file("../../ext/mbedtls/library/bignum.c");
178 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300179 }
180
Salome Thirot6fdbf552021-05-14 16:46:14 +0100181 if enc_kw || enc_aes256_kw {
182 if enc_aes256_kw {
183 conf.define("MCUBOOT_AES_256", None);
184 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300185 conf.define("MCUBOOT_ENCRYPT_KW", None);
186 conf.define("MCUBOOT_ENC_IMAGES", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300187
188 conf.file("../../boot/bootutil/src/encrypted.c");
189 conf.file("csupport/keys.c");
190
Fabio Utzig39297432019-05-08 18:51:10 -0300191 if sig_rsa || sig_rsa3072 {
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800192 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200193 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300194
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200195 /* Simulator uses Mbed-TLS to wrap keys */
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800196 conf.include("../../ext/mbedtls/include");
197 conf.file("../../ext/mbedtls/library/platform.c");
198 conf.include("../../ext/mbedtls/library");
199 conf.file("../../ext/mbedtls/library/platform_util.c");
200 conf.file("../../ext/mbedtls/library/nist_kw.c");
201 conf.file("../../ext/mbedtls/library/cipher.c");
202 conf.file("../../ext/mbedtls/library/cipher_wrap.c");
203 conf.file("../../ext/mbedtls/library/aes.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200204
205 if sig_ecdsa {
206 conf.define("MCUBOOT_USE_TINYCRYPT", None);
207
208 conf.include("../../ext/tinycrypt/lib/include");
209
210 conf.file("../../ext/tinycrypt/lib/source/utils.c");
211 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
212 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
213 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
Blaž Hrastnik4f4833d2020-09-14 13:53:31 +0900214 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200215 }
Fabio Utzig97710282019-05-24 17:44:49 -0300216
217 if sig_ed25519 {
218 panic!("ed25519 does not support image encryption with KW yet");
219 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300220 }
221
Fabio Utzig90f449e2019-10-24 07:43:53 -0300222 if enc_ec256 {
223 conf.define("MCUBOOT_ENCRYPT_EC256", None);
224 conf.define("MCUBOOT_ENC_IMAGES", None);
225 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzig4b4ed982020-01-06 09:09:51 -0300226 conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Fabio Utzig90f449e2019-10-24 07:43:53 -0300227
228 conf.file("../../boot/bootutil/src/encrypted.c");
229 conf.file("csupport/keys.c");
230
231 conf.include("../../ext/mbedtls-asn1/include");
232 conf.include("../../ext/tinycrypt/lib/include");
233
234 /* FIXME: fail with other signature schemes ? */
235
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/ecc.c");
239 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
240 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
241
242 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
243 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
244
245 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
246 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
247 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
248 conf.file("../../ext/tinycrypt/lib/source/hmac.c");
249 conf.file("../../ext/tinycrypt/lib/source/ecc_dh.c");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100250 } else if enc_ec256_mbedtls || enc_aes256_ec256 {
251 if enc_aes256_ec256 {
252 conf.define("MCUBOOT_AES_256", None);
253 }
Fabio Utzig6c553d62021-05-06 19:56:18 -0300254 conf.define("MCUBOOT_ENCRYPT_EC256", None);
255 conf.define("MCUBOOT_ENC_IMAGES", None);
256 conf.define("MCUBOOT_USE_MBED_TLS", None);
257 conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
258
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800259 conf.include("../../ext/mbedtls/include");
Fabio Utzig6c553d62021-05-06 19:56:18 -0300260
261 conf.file("../../boot/bootutil/src/encrypted.c");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800262 conf.file("../../ext/mbedtls/library/sha256.c");
263 conf.file("../../ext/mbedtls/library/asn1parse.c");
264 conf.file("../../ext/mbedtls/library/bignum.c");
265 conf.file("../../ext/mbedtls/library/ecdh.c");
266 conf.file("../../ext/mbedtls/library/md.c");
267 conf.file("../../ext/mbedtls/library/aes.c");
268 conf.file("../../ext/mbedtls/library/ecp.c");
269 conf.file("../../ext/mbedtls/library/ecp_curves.c");
270 conf.file("../../ext/mbedtls/library/platform.c");
271 conf.file("../../ext/mbedtls/library/platform_util.c");
Fabio Utzig6c553d62021-05-06 19:56:18 -0300272 conf.file("csupport/keys.c");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300273 }
274
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300275 if enc_x25519 {
276 conf.define("MCUBOOT_ENCRYPT_X25519", None);
277 conf.define("MCUBOOT_ENC_IMAGES", None);
278 conf.define("MCUBOOT_USE_TINYCRYPT", None);
279 conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
280
281 conf.file("../../boot/bootutil/src/encrypted.c");
282 conf.file("csupport/keys.c");
283
284 conf.include("../../ext/mbedtls-asn1/include");
285 conf.include("../../ext/tinycrypt/lib/include");
286 conf.include("../../ext/tinycrypt-sha512/lib/include");
287
288 conf.file("../../ext/fiat/src/curve25519.c");
289
290 conf.file("../../ext/tinycrypt/lib/source/utils.c");
291 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
292
293 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
294 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
295
296 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
297 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
298 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
299 conf.file("../../ext/tinycrypt/lib/source/hmac.c");
300 }
Fabio Utzig90f449e2019-10-24 07:43:53 -0300301
Salome Thirot6fdbf552021-05-14 16:46:14 +0100302 else if enc_aes256_x25519 {
303 conf.define("MCUBOOT_AES_256", None);
304 conf.define("MCUBOOT_ENCRYPT_X25519", None);
305 conf.define("MCUBOOT_ENC_IMAGES", None);
306 conf.define("MCUBOOT_USE_MBED_TLS", None);
307 conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
308
309 conf.file("../../boot/bootutil/src/encrypted.c");
310 conf.file("csupport/keys.c");
311
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800312 conf.include("../../ext/mbedtls/include");
313 conf.include("../../ext/mbedtls-asn1/include");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100314 conf.file("../../ext/fiat/src/curve25519.c");
315 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
316 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800317 conf.file("../../ext/mbedtls/library/platform.c");
318 conf.file("../../ext/mbedtls/library/platform_util.c");
319 conf.file("../../ext/mbedtls/library/aes.c");
320 conf.file("../../ext/mbedtls/library/sha256.c");
321 conf.file("../../ext/mbedtls/library/md.c");
322 conf.file("../../ext/mbedtls/library/sha512.c");
Salome Thirot6fdbf552021-05-14 16:46:14 +0100323 }
324
Fabio Utzig251ef1d2018-12-18 17:20:19 -0200325 if sig_rsa && enc_kw {
326 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa-kw.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100327 } else if sig_rsa || sig_rsa3072 || enc_rsa || enc_aes256_rsa {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200328 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100329 } else if sig_ecdsa_mbedtls || enc_ec256_mbedtls || enc_aes256_ec256 {
Fabio Utzig6c553d62021-05-06 19:56:18 -0300330 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ec.h>"));
Fabio Utzig90f449e2019-10-24 07:43:53 -0300331 } else if (sig_ecdsa || enc_ec256) && !enc_kw {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200332 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Fabio Utzig3fa72ca2020-04-02 11:20:37 -0300333 } else if sig_ed25519 || enc_x25519 {
Fabio Utziga1c142d2020-01-03 08:28:11 -0300334 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100335 } else if enc_kw || enc_aes256_kw {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200336 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
Salome Thirot6fdbf552021-05-14 16:46:14 +0100337 } else if enc_aes256_x25519 {
338 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ed25519.h>"));
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200339 }
340
David Brown704ac6f2017-07-12 10:14:47 -0600341 conf.file("../../boot/bootutil/src/image_validate.c");
Fabio Utzig39297432019-05-08 18:51:10 -0300342 if sig_rsa || sig_rsa3072 {
Fabio Utzigc7865402017-12-05 08:50:52 -0200343 conf.file("../../boot/bootutil/src/image_rsa.c");
David Brown641af452021-02-19 12:16:48 -0700344 } else if sig_ecdsa || sig_ecdsa_mbedtls {
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800345 conf.include("../../ext/mbedtls/include");
Fabio Utzigc7865402017-12-05 08:50:52 -0200346 conf.file("../../boot/bootutil/src/image_ec256.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300347 } else if sig_ed25519 {
348 conf.file("../../boot/bootutil/src/image_ed25519.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200349 }
David Brown63902772017-07-12 09:47:49 -0600350 conf.file("../../boot/bootutil/src/loader.c");
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300351 conf.file("../../boot/bootutil/src/swap_misc.c");
352 conf.file("../../boot/bootutil/src/swap_scratch.c");
353 conf.file("../../boot/bootutil/src/swap_move.c");
David Brown63902772017-07-12 09:47:49 -0600354 conf.file("../../boot/bootutil/src/caps.c");
355 conf.file("../../boot/bootutil/src/bootutil_misc.c");
Andrzej Puzdrowskif573b392020-11-10 14:35:15 +0100356 conf.file("../../boot/bootutil/src/bootutil_public.c");
Fabio Utzig61fd8882019-09-14 20:00:20 -0300357 conf.file("../../boot/bootutil/src/tlv.c");
Raef Colese8fe6cf2020-05-26 13:07:40 +0100358 conf.file("../../boot/bootutil/src/fault_injection_hardening.c");
David Brownd2b18532017-07-12 09:51:31 -0600359 conf.file("csupport/run.c");
David Brown63902772017-07-12 09:47:49 -0600360 conf.include("../../boot/bootutil/include");
Fabio Utzig57c40f72017-12-12 21:48:30 -0200361 conf.include("csupport");
Fabio Utzig9a4b9ba2018-05-07 08:31:27 -0300362 conf.include("../../boot/zephyr/include");
David Brown63902772017-07-12 09:47:49 -0600363 conf.debug(true);
364 conf.flag("-Wall");
David Brown0b693c02017-07-12 12:34:33 -0600365 conf.flag("-Werror");
David Brown63902772017-07-12 09:47:49 -0600366
Fabio Utzig0bccf9d2017-12-07 12:13:57 -0200367 // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90.
368 // It has incomplete std=c11 and std=c99 support but std=c99 was checked
369 // to build correctly so leaving it here to updated in the future...
370 conf.flag("-std=c99");
371
David Brown63902772017-07-12 09:47:49 -0600372 conf.compile("libbootutil.a");
373
374 walk_dir("../../boot").unwrap();
Fabio Utzigc7865402017-12-05 08:50:52 -0200375 walk_dir("../../ext/tinycrypt/lib/source").unwrap();
David Brownb748f6f2019-10-11 10:07:31 -0600376 walk_dir("../../ext/mbedtls-asn1").unwrap();
David Brownd2b18532017-07-12 09:51:31 -0600377 walk_dir("csupport").unwrap();
Sherry Zhangf4580cb2021-07-13 22:07:31 +0800378 walk_dir("../../ext/mbedtls/include").unwrap();
379 walk_dir("../../ext/mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -0600380}
381
382// Output the names of all files within a directory so that Cargo knows when to rebuild.
383fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
384 for ent in fs::read_dir(path.as_ref())? {
385 let ent = ent?;
386 let p = ent.path();
387 if p.is_dir() {
388 walk_dir(p)?;
389 } else {
390 // Note that non-utf8 names will fail.
391 let name = p.to_str().unwrap();
392 if name.ends_with(".c") || name.ends_with(".h") {
393 println!("cargo:rerun-if-changed={}", name);
394 }
395 }
396 }
397
398 Ok(())
399}