blob: b431cb3b527e48ff2bb3574639267bc979246842 [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();
Fabio Utzig97710282019-05-24 17:44:49 -030015 let sig_ed25519 = env::var("CARGO_FEATURE_SIG_ED25519").is_ok();
David Brown63902772017-07-12 09:47:49 -060016 let overwrite_only = env::var("CARGO_FEATURE_OVERWRITE_ONLY").is_ok();
Fabio Utzig031eb7d2019-11-28 10:13:14 -030017 let swap_move = env::var("CARGO_FEATURE_SWAP_MOVE").is_ok();
David Vincze2d736ad2019-02-18 11:50:22 +010018 let validate_primary_slot =
19 env::var("CARGO_FEATURE_VALIDATE_PRIMARY_SLOT").is_ok();
Fabio Utzig1e48b912018-09-18 09:04:18 -030020 let enc_rsa = env::var("CARGO_FEATURE_ENC_RSA").is_ok();
21 let enc_kw = env::var("CARGO_FEATURE_ENC_KW").is_ok();
Fabio Utzig90f449e2019-10-24 07:43:53 -030022 let enc_ec256 = env::var("CARGO_FEATURE_ENC_EC256").is_ok();
Fabio Utzig9b97b132018-12-18 17:21:51 -020023 let bootstrap = env::var("CARGO_FEATURE_BOOTSTRAP").is_ok();
David Brown5e6f5e02019-04-04 10:50:05 +070024 let multiimage = env::var("CARGO_FEATURE_MULTIIMAGE").is_ok();
David Brown2ee5f7f2020-01-13 14:04:01 -070025 let downgrade_prevention = env::var("CARGO_FEATURE_DOWNGRADE_PREVENTION").is_ok();
David Brown63902772017-07-12 09:47:49 -060026
Fabio Utzig455cad52018-10-15 14:36:33 -070027 let mut conf = cc::Build::new();
David Brown63902772017-07-12 09:47:49 -060028 conf.define("__BOOTSIM__", None);
Fabio Utzig08fcfe92018-11-26 10:18:18 -020029 conf.define("MCUBOOT_HAVE_LOGGING", None);
David Brown63902772017-07-12 09:47:49 -060030 conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None);
Marti Bolivar248da082018-04-24 15:11:39 -040031 conf.define("MCUBOOT_HAVE_ASSERT_H", None);
Marti Bolivarf9bfddd2018-04-24 14:28:33 -040032 conf.define("MCUBOOT_MAX_IMG_SECTORS", Some("128"));
David Brown5e6f5e02019-04-04 10:50:05 +070033 conf.define("MCUBOOT_IMAGE_NUMBER", Some(if multiimage { "2" } else { "1" }));
Fabio Utzigebdc9692017-11-23 16:28:25 -020034
David Brown2ee5f7f2020-01-13 14:04:01 -070035 if downgrade_prevention && !overwrite_only {
36 panic!("Downgrade prevention requires overwrite only");
37 }
38
Fabio Utzig9b97b132018-12-18 17:21:51 -020039 if bootstrap {
40 conf.define("MCUBOOT_BOOTSTRAP", None);
41 }
42
David Vincze2d736ad2019-02-18 11:50:22 +010043 if validate_primary_slot {
44 conf.define("MCUBOOT_VALIDATE_PRIMARY_SLOT", None);
Fabio Utzigebdc9692017-11-23 16:28:25 -020045 }
David Brown63902772017-07-12 09:47:49 -060046
David Brown2ee5f7f2020-01-13 14:04:01 -070047 if downgrade_prevention {
48 conf.define("MCUBOOT_DOWNGRADE_PREVENTION", None);
49 }
50
Fabio Utzig39297432019-05-08 18:51:10 -030051 // Currently no more than one sig type can be used simultaneously.
Fabio Utzig97710282019-05-24 17:44:49 -030052 if vec![sig_rsa, sig_rsa3072, sig_ecdsa, sig_ed25519].iter()
Fabio Utzig39297432019-05-08 18:51:10 -030053 .fold(0, |sum, &v| sum + v as i32) > 1 {
54 panic!("mcuboot does not support more than one sig type at the same time");
David Brown704ac6f2017-07-12 10:14:47 -060055 }
David Brown63902772017-07-12 09:47:49 -060056
Fabio Utzig39297432019-05-08 18:51:10 -030057 if sig_rsa || sig_rsa3072 {
David Brown63902772017-07-12 09:47:49 -060058 conf.define("MCUBOOT_SIGN_RSA", None);
Fabio Utzig39297432019-05-08 18:51:10 -030059 // The Kconfig style defines must be added here as well because
60 // they are used internally by "config-rsa.h"
61 if sig_rsa {
62 conf.define("MCUBOOT_SIGN_RSA_LEN", "2048");
Fabio Utzig46268532020-01-04 21:12:55 -030063 conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "2048");
Fabio Utzig39297432019-05-08 18:51:10 -030064 } else {
65 conf.define("MCUBOOT_SIGN_RSA_LEN", "3072");
Fabio Utzig46268532020-01-04 21:12:55 -030066 conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "3072");
Fabio Utzig39297432019-05-08 18:51:10 -030067 }
David Brown63902772017-07-12 09:47:49 -060068 conf.define("MCUBOOT_USE_MBED_TLS", None);
69
Fabio Utzige60b12f2020-02-06 07:15:30 -030070 conf.include("../../ext/mbedtls/crypto/include");
71 conf.file("../../ext/mbedtls/crypto/library/sha256.c");
Fabio Utzig806af0e2018-04-26 10:53:54 -030072 conf.file("csupport/keys.c");
David Brown63902772017-07-12 09:47:49 -060073
Fabio Utzige60b12f2020-02-06 07:15:30 -030074 conf.file("../../ext/mbedtls/crypto/library/rsa.c");
75 conf.file("../../ext/mbedtls/crypto/library/bignum.c");
76 conf.file("../../ext/mbedtls/crypto/library/platform.c");
77 conf.file("../../ext/mbedtls/crypto/library/platform_util.c");
78 conf.file("../../ext/mbedtls/crypto/library/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060079 } else if sig_ecdsa {
Fabio Utzigc7865402017-12-05 08:50:52 -020080 conf.define("MCUBOOT_SIGN_EC256", None);
David Brown63902772017-07-12 09:47:49 -060081 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigc7865402017-12-05 08:50:52 -020082
Fabio Utzigb4d20c82018-12-27 16:08:39 -020083 if !enc_kw {
David Brownb748f6f2019-10-11 10:07:31 -060084 conf.include("../../ext/mbedtls-asn1/include");
Fabio Utzigb4d20c82018-12-27 16:08:39 -020085 }
Fabio Utzigc7865402017-12-05 08:50:52 -020086 conf.include("../../ext/tinycrypt/lib/include");
87
Fabio Utzig806af0e2018-04-26 10:53:54 -030088 conf.file("csupport/keys.c");
Fabio Utzigc7865402017-12-05 08:50:52 -020089
90 conf.file("../../ext/tinycrypt/lib/source/utils.c");
91 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
92 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
93 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
94 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
95
David Brownb748f6f2019-10-11 10:07:31 -060096 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
97 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
Fabio Utzig97710282019-05-24 17:44:49 -030098 } else if sig_ed25519 {
99 conf.define("MCUBOOT_SIGN_ED25519", None);
Fabio Utziga1c142d2020-01-03 08:28:11 -0300100 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzig97710282019-05-24 17:44:49 -0300101
Fabio Utziga1c142d2020-01-03 08:28:11 -0300102 conf.include("../../ext/tinycrypt/lib/include");
103 conf.include("../../ext/tinycrypt-sha512/lib/include");
104 conf.include("../../ext/mbedtls-asn1/include");
105 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
106 conf.file("../../ext/tinycrypt-sha512/lib/source/sha512.c");
107 conf.file("../../ext/tinycrypt/lib/source/utils.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300108 conf.file("csupport/keys.c");
109 conf.file("../../ext/fiat/src/curve25519.c");
Fabio Utziga1c142d2020-01-03 08:28:11 -0300110 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
111 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300112 } else if !enc_ec256 {
113 // No signature type, only sha256 validation. The default
Marti Bolivara4818a52018-04-12 13:02:38 -0400114 // configuration file bundled with mbedTLS is sufficient.
Fabio Utzig90f449e2019-10-24 07:43:53 -0300115 // When using ECIES-P256 rely on Tinycrypt.
David Brown704ac6f2017-07-12 10:14:47 -0600116 conf.define("MCUBOOT_USE_MBED_TLS", None);
Fabio Utzige60b12f2020-02-06 07:15:30 -0300117 conf.include("../../ext/mbedtls/crypto/include");
118 conf.file("../../ext/mbedtls/crypto/library/sha256.c");
David Brown63902772017-07-12 09:47:49 -0600119 }
120
121 if overwrite_only {
122 conf.define("MCUBOOT_OVERWRITE_ONLY", None);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300123 conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None);
David Brown63902772017-07-12 09:47:49 -0600124 }
125
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300126 if swap_move {
127 conf.define("MCUBOOT_SWAP_USING_MOVE", None);
128 }
129
Fabio Utzig1e48b912018-09-18 09:04:18 -0300130 if enc_rsa {
131 conf.define("MCUBOOT_ENCRYPT_RSA", None);
132 conf.define("MCUBOOT_ENC_IMAGES", None);
133 conf.define("MCUBOOT_USE_MBED_TLS", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300134
135 conf.file("../../boot/bootutil/src/encrypted.c");
136 conf.file("csupport/keys.c");
137
Fabio Utzige60b12f2020-02-06 07:15:30 -0300138 conf.include("../../ext/mbedtls/crypto/include");
139 conf.file("../../ext/mbedtls/crypto/library/sha256.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300140
Fabio Utzige60b12f2020-02-06 07:15:30 -0300141 conf.file("../../ext/mbedtls/crypto/library/platform.c");
142 conf.file("../../ext/mbedtls/crypto/library/platform_util.c");
143 conf.file("../../ext/mbedtls/crypto/library/rsa.c");
144 conf.file("../../ext/mbedtls/crypto/library/rsa_internal.c");
145 conf.file("../../ext/mbedtls/crypto/library/md.c");
146 conf.file("../../ext/mbedtls/crypto/library/aes.c");
147 conf.file("../../ext/mbedtls/crypto/library/bignum.c");
148 conf.file("../../ext/mbedtls/crypto/library/asn1parse.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300149 }
150
151 if enc_kw {
152 conf.define("MCUBOOT_ENCRYPT_KW", None);
153 conf.define("MCUBOOT_ENC_IMAGES", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300154
155 conf.file("../../boot/bootutil/src/encrypted.c");
156 conf.file("csupport/keys.c");
157
Fabio Utzig39297432019-05-08 18:51:10 -0300158 if sig_rsa || sig_rsa3072 {
Fabio Utzige60b12f2020-02-06 07:15:30 -0300159 conf.file("../../ext/mbedtls/crypto/library/sha256.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200160 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300161
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200162 /* Simulator uses Mbed-TLS to wrap keys */
Fabio Utzige60b12f2020-02-06 07:15:30 -0300163 conf.include("../../ext/mbedtls/crypto/include");
164 conf.file("../../ext/mbedtls/crypto/library/platform.c");
165 conf.file("../../ext/mbedtls/crypto/library/platform_util.c");
166 conf.file("../../ext/mbedtls/crypto/library/nist_kw.c");
167 conf.file("../../ext/mbedtls/crypto/library/cipher.c");
168 conf.file("../../ext/mbedtls/crypto/library/cipher_wrap.c");
169 conf.file("../../ext/mbedtls/crypto/library/aes.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200170
171 if sig_ecdsa {
172 conf.define("MCUBOOT_USE_TINYCRYPT", None);
173
174 conf.include("../../ext/tinycrypt/lib/include");
175
176 conf.file("../../ext/tinycrypt/lib/source/utils.c");
177 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
178 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
179 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
180 }
Fabio Utzig97710282019-05-24 17:44:49 -0300181
182 if sig_ed25519 {
183 panic!("ed25519 does not support image encryption with KW yet");
184 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300185 }
186
Fabio Utzig90f449e2019-10-24 07:43:53 -0300187 if enc_ec256 {
188 conf.define("MCUBOOT_ENCRYPT_EC256", None);
189 conf.define("MCUBOOT_ENC_IMAGES", None);
190 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzig4b4ed982020-01-06 09:09:51 -0300191 conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Fabio Utzig90f449e2019-10-24 07:43:53 -0300192
193 conf.file("../../boot/bootutil/src/encrypted.c");
194 conf.file("csupport/keys.c");
195
196 conf.include("../../ext/mbedtls-asn1/include");
197 conf.include("../../ext/tinycrypt/lib/include");
198
199 /* FIXME: fail with other signature schemes ? */
200
201 conf.file("../../ext/tinycrypt/lib/source/utils.c");
202 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
203 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
204 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
205 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
206
207 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
208 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
209
210 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
211 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
212 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
213 conf.file("../../ext/tinycrypt/lib/source/hmac.c");
214 conf.file("../../ext/tinycrypt/lib/source/ecc_dh.c");
215 }
216
217
Fabio Utzig251ef1d2018-12-18 17:20:19 -0200218 if sig_rsa && enc_kw {
219 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa-kw.h>"));
Fabio Utzig39297432019-05-08 18:51:10 -0300220 } else if sig_rsa || sig_rsa3072 || enc_rsa {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200221 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
Fabio Utzig90f449e2019-10-24 07:43:53 -0300222 } else if (sig_ecdsa || enc_ec256) && !enc_kw {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200223 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Fabio Utzig97710282019-05-24 17:44:49 -0300224 } else if sig_ed25519 {
Fabio Utziga1c142d2020-01-03 08:28:11 -0300225 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200226 } else if enc_kw {
227 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
228 }
229
David Brown704ac6f2017-07-12 10:14:47 -0600230 conf.file("../../boot/bootutil/src/image_validate.c");
Fabio Utzig39297432019-05-08 18:51:10 -0300231 if sig_rsa || sig_rsa3072 {
Fabio Utzigc7865402017-12-05 08:50:52 -0200232 conf.file("../../boot/bootutil/src/image_rsa.c");
233 } else if sig_ecdsa {
234 conf.file("../../boot/bootutil/src/image_ec256.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300235 } else if sig_ed25519 {
236 conf.file("../../boot/bootutil/src/image_ed25519.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200237 }
David Brown63902772017-07-12 09:47:49 -0600238 conf.file("../../boot/bootutil/src/loader.c");
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300239 conf.file("../../boot/bootutil/src/swap_misc.c");
240 conf.file("../../boot/bootutil/src/swap_scratch.c");
241 conf.file("../../boot/bootutil/src/swap_move.c");
David Brown63902772017-07-12 09:47:49 -0600242 conf.file("../../boot/bootutil/src/caps.c");
243 conf.file("../../boot/bootutil/src/bootutil_misc.c");
Fabio Utzig61fd8882019-09-14 20:00:20 -0300244 conf.file("../../boot/bootutil/src/tlv.c");
David Brownd2b18532017-07-12 09:51:31 -0600245 conf.file("csupport/run.c");
David Brown63902772017-07-12 09:47:49 -0600246 conf.include("../../boot/bootutil/include");
Fabio Utzig57c40f72017-12-12 21:48:30 -0200247 conf.include("csupport");
Fabio Utzig9a4b9ba2018-05-07 08:31:27 -0300248 conf.include("../../boot/zephyr/include");
David Brown63902772017-07-12 09:47:49 -0600249 conf.debug(true);
250 conf.flag("-Wall");
David Brown0b693c02017-07-12 12:34:33 -0600251 conf.flag("-Werror");
David Brown63902772017-07-12 09:47:49 -0600252
Fabio Utzig0bccf9d2017-12-07 12:13:57 -0200253 // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90.
254 // It has incomplete std=c11 and std=c99 support but std=c99 was checked
255 // to build correctly so leaving it here to updated in the future...
256 conf.flag("-std=c99");
257
David Brown63902772017-07-12 09:47:49 -0600258 conf.compile("libbootutil.a");
259
260 walk_dir("../../boot").unwrap();
Fabio Utzigc7865402017-12-05 08:50:52 -0200261 walk_dir("../../ext/tinycrypt/lib/source").unwrap();
David Brownb748f6f2019-10-11 10:07:31 -0600262 walk_dir("../../ext/mbedtls-asn1").unwrap();
David Brownd2b18532017-07-12 09:51:31 -0600263 walk_dir("csupport").unwrap();
Fabio Utzige60b12f2020-02-06 07:15:30 -0300264 walk_dir("../../ext/mbedtls/crypto/include").unwrap();
265 walk_dir("../../ext/mbedtls/crypto/library").unwrap();
David Brown63902772017-07-12 09:47:49 -0600266}
267
268// Output the names of all files within a directory so that Cargo knows when to rebuild.
269fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
270 for ent in fs::read_dir(path.as_ref())? {
271 let ent = ent?;
272 let p = ent.path();
273 if p.is_dir() {
274 walk_dir(p)?;
275 } else {
276 // Note that non-utf8 names will fail.
277 let name = p.to_str().unwrap();
278 if name.ends_with(".c") || name.ends_with(".h") {
279 println!("cargo:rerun-if-changed={}", name);
280 }
281 }
282 }
283
284 Ok(())
285}