blob: 1c6584fd54da778d458195e9f67607ddfb4b00b3 [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 Brown63902772017-07-12 09:47:49 -060025
Fabio Utzig455cad52018-10-15 14:36:33 -070026 let mut conf = cc::Build::new();
David Brown63902772017-07-12 09:47:49 -060027 conf.define("__BOOTSIM__", None);
Fabio Utzig08fcfe92018-11-26 10:18:18 -020028 conf.define("MCUBOOT_HAVE_LOGGING", None);
David Brown63902772017-07-12 09:47:49 -060029 conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None);
Marti Bolivar248da082018-04-24 15:11:39 -040030 conf.define("MCUBOOT_HAVE_ASSERT_H", None);
Marti Bolivarf9bfddd2018-04-24 14:28:33 -040031 conf.define("MCUBOOT_MAX_IMG_SECTORS", Some("128"));
David Brown5e6f5e02019-04-04 10:50:05 +070032 conf.define("MCUBOOT_IMAGE_NUMBER", Some(if multiimage { "2" } else { "1" }));
Fabio Utzigebdc9692017-11-23 16:28:25 -020033
Fabio Utzig9b97b132018-12-18 17:21:51 -020034 if bootstrap {
35 conf.define("MCUBOOT_BOOTSTRAP", None);
36 }
37
David Vincze2d736ad2019-02-18 11:50:22 +010038 if validate_primary_slot {
39 conf.define("MCUBOOT_VALIDATE_PRIMARY_SLOT", None);
Fabio Utzigebdc9692017-11-23 16:28:25 -020040 }
David Brown63902772017-07-12 09:47:49 -060041
Fabio Utzig39297432019-05-08 18:51:10 -030042 // Currently no more than one sig type can be used simultaneously.
Fabio Utzig97710282019-05-24 17:44:49 -030043 if vec![sig_rsa, sig_rsa3072, sig_ecdsa, sig_ed25519].iter()
Fabio Utzig39297432019-05-08 18:51:10 -030044 .fold(0, |sum, &v| sum + v as i32) > 1 {
45 panic!("mcuboot does not support more than one sig type at the same time");
David Brown704ac6f2017-07-12 10:14:47 -060046 }
David Brown63902772017-07-12 09:47:49 -060047
Fabio Utzig39297432019-05-08 18:51:10 -030048 if sig_rsa || sig_rsa3072 {
David Brown63902772017-07-12 09:47:49 -060049 conf.define("MCUBOOT_SIGN_RSA", None);
Fabio Utzig39297432019-05-08 18:51:10 -030050 // The Kconfig style defines must be added here as well because
51 // they are used internally by "config-rsa.h"
52 if sig_rsa {
53 conf.define("MCUBOOT_SIGN_RSA_LEN", "2048");
Fabio Utzig46268532020-01-04 21:12:55 -030054 conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "2048");
Fabio Utzig39297432019-05-08 18:51:10 -030055 } else {
56 conf.define("MCUBOOT_SIGN_RSA_LEN", "3072");
Fabio Utzig46268532020-01-04 21:12:55 -030057 conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "3072");
Fabio Utzig39297432019-05-08 18:51:10 -030058 }
David Brown63902772017-07-12 09:47:49 -060059 conf.define("MCUBOOT_USE_MBED_TLS", None);
60
David Brownf984b952019-10-11 10:32:36 -060061 conf.include("../../ext/mbedtls/include");
62 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzig806af0e2018-04-26 10:53:54 -030063 conf.file("csupport/keys.c");
David Brown63902772017-07-12 09:47:49 -060064
David Brownf984b952019-10-11 10:32:36 -060065 conf.file("../../ext/mbedtls/library/rsa.c");
66 conf.file("../../ext/mbedtls/library/bignum.c");
67 conf.file("../../ext/mbedtls/library/platform.c");
68 conf.file("../../ext/mbedtls/library/platform_util.c");
69 conf.file("../../ext/mbedtls/library/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060070 } else if sig_ecdsa {
Fabio Utzigc7865402017-12-05 08:50:52 -020071 conf.define("MCUBOOT_SIGN_EC256", None);
David Brown63902772017-07-12 09:47:49 -060072 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigc7865402017-12-05 08:50:52 -020073
Fabio Utzigb4d20c82018-12-27 16:08:39 -020074 if !enc_kw {
David Brownb748f6f2019-10-11 10:07:31 -060075 conf.include("../../ext/mbedtls-asn1/include");
Fabio Utzigb4d20c82018-12-27 16:08:39 -020076 }
Fabio Utzigc7865402017-12-05 08:50:52 -020077 conf.include("../../ext/tinycrypt/lib/include");
78
Fabio Utzig806af0e2018-04-26 10:53:54 -030079 conf.file("csupport/keys.c");
Fabio Utzigc7865402017-12-05 08:50:52 -020080
81 conf.file("../../ext/tinycrypt/lib/source/utils.c");
82 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
83 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
84 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
85 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
86
David Brownb748f6f2019-10-11 10:07:31 -060087 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
88 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
Fabio Utzig97710282019-05-24 17:44:49 -030089 } else if sig_ed25519 {
90 conf.define("MCUBOOT_SIGN_ED25519", None);
91 conf.define("MCUBOOT_USE_MBED_TLS", None);
92
David Brownf984b952019-10-11 10:32:36 -060093 conf.include("../../ext/mbedtls/include");
94 conf.file("../../ext/mbedtls/library/sha256.c");
95 conf.file("../../ext/mbedtls/library/sha512.c");
Fabio Utzig97710282019-05-24 17:44:49 -030096 conf.file("csupport/keys.c");
97 conf.file("../../ext/fiat/src/curve25519.c");
David Brownf984b952019-10-11 10:32:36 -060098 conf.file("../../ext/mbedtls/library/platform.c");
99 conf.file("../../ext/mbedtls/library/platform_util.c");
100 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300101 } else if !enc_ec256 {
102 // No signature type, only sha256 validation. The default
Marti Bolivara4818a52018-04-12 13:02:38 -0400103 // configuration file bundled with mbedTLS is sufficient.
Fabio Utzig90f449e2019-10-24 07:43:53 -0300104 // When using ECIES-P256 rely on Tinycrypt.
David Brown704ac6f2017-07-12 10:14:47 -0600105 conf.define("MCUBOOT_USE_MBED_TLS", None);
David Brownf984b952019-10-11 10:32:36 -0600106 conf.include("../../ext/mbedtls/include");
107 conf.file("../../ext/mbedtls/library/sha256.c");
David Brown63902772017-07-12 09:47:49 -0600108 }
109
110 if overwrite_only {
111 conf.define("MCUBOOT_OVERWRITE_ONLY", None);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300112 conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None);
David Brown63902772017-07-12 09:47:49 -0600113 }
114
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300115 if swap_move {
116 conf.define("MCUBOOT_SWAP_USING_MOVE", None);
117 }
118
Fabio Utzig1e48b912018-09-18 09:04:18 -0300119 if enc_rsa {
120 conf.define("MCUBOOT_ENCRYPT_RSA", None);
121 conf.define("MCUBOOT_ENC_IMAGES", None);
122 conf.define("MCUBOOT_USE_MBED_TLS", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300123
124 conf.file("../../boot/bootutil/src/encrypted.c");
125 conf.file("csupport/keys.c");
126
David Brownf984b952019-10-11 10:32:36 -0600127 conf.include("../../ext/mbedtls/include");
128 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300129
David Brownf984b952019-10-11 10:32:36 -0600130 conf.file("../../ext/mbedtls/library/platform.c");
131 conf.file("../../ext/mbedtls/library/platform_util.c");
132 conf.file("../../ext/mbedtls/library/rsa.c");
133 conf.file("../../ext/mbedtls/library/rsa_internal.c");
134 conf.file("../../ext/mbedtls/library/md.c");
135 conf.file("../../ext/mbedtls/library/md_wrap.c");
136 conf.file("../../ext/mbedtls/library/aes.c");
137 conf.file("../../ext/mbedtls/library/bignum.c");
138 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300139 }
140
141 if enc_kw {
142 conf.define("MCUBOOT_ENCRYPT_KW", None);
143 conf.define("MCUBOOT_ENC_IMAGES", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300144
145 conf.file("../../boot/bootutil/src/encrypted.c");
146 conf.file("csupport/keys.c");
147
Fabio Utzig39297432019-05-08 18:51:10 -0300148 if sig_rsa || sig_rsa3072 {
David Brownf984b952019-10-11 10:32:36 -0600149 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200150 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300151
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200152 /* Simulator uses Mbed-TLS to wrap keys */
David Brownf984b952019-10-11 10:32:36 -0600153 conf.include("../../ext/mbedtls/include");
154 conf.file("../../ext/mbedtls/library/platform.c");
155 conf.file("../../ext/mbedtls/library/platform_util.c");
156 conf.file("../../ext/mbedtls/library/nist_kw.c");
157 conf.file("../../ext/mbedtls/library/cipher.c");
158 conf.file("../../ext/mbedtls/library/cipher_wrap.c");
159 conf.file("../../ext/mbedtls/library/aes.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200160
161 if sig_ecdsa {
162 conf.define("MCUBOOT_USE_TINYCRYPT", None);
163
164 conf.include("../../ext/tinycrypt/lib/include");
165
166 conf.file("../../ext/tinycrypt/lib/source/utils.c");
167 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
168 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
169 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
170 }
Fabio Utzig97710282019-05-24 17:44:49 -0300171
172 if sig_ed25519 {
173 panic!("ed25519 does not support image encryption with KW yet");
174 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300175 }
176
Fabio Utzig90f449e2019-10-24 07:43:53 -0300177 if enc_ec256 {
178 conf.define("MCUBOOT_ENCRYPT_EC256", None);
179 conf.define("MCUBOOT_ENC_IMAGES", None);
180 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzig4b4ed982020-01-06 09:09:51 -0300181 conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None);
Fabio Utzig90f449e2019-10-24 07:43:53 -0300182
183 conf.file("../../boot/bootutil/src/encrypted.c");
184 conf.file("csupport/keys.c");
185
186 conf.include("../../ext/mbedtls-asn1/include");
187 conf.include("../../ext/tinycrypt/lib/include");
188
189 /* FIXME: fail with other signature schemes ? */
190
191 conf.file("../../ext/tinycrypt/lib/source/utils.c");
192 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
193 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
194 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
195 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
196
197 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
198 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
199
200 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
201 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
202 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
203 conf.file("../../ext/tinycrypt/lib/source/hmac.c");
204 conf.file("../../ext/tinycrypt/lib/source/ecc_dh.c");
205 }
206
207
Fabio Utzig251ef1d2018-12-18 17:20:19 -0200208 if sig_rsa && enc_kw {
209 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa-kw.h>"));
Fabio Utzig39297432019-05-08 18:51:10 -0300210 } else if sig_rsa || sig_rsa3072 || enc_rsa {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200211 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
Fabio Utzig90f449e2019-10-24 07:43:53 -0300212 } else if (sig_ecdsa || enc_ec256) && !enc_kw {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200213 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Fabio Utzig97710282019-05-24 17:44:49 -0300214 } else if sig_ed25519 {
215 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ed25519.h>"));
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200216 } else if enc_kw {
217 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
218 }
219
David Brown704ac6f2017-07-12 10:14:47 -0600220 conf.file("../../boot/bootutil/src/image_validate.c");
Fabio Utzig39297432019-05-08 18:51:10 -0300221 if sig_rsa || sig_rsa3072 {
Fabio Utzigc7865402017-12-05 08:50:52 -0200222 conf.file("../../boot/bootutil/src/image_rsa.c");
223 } else if sig_ecdsa {
224 conf.file("../../boot/bootutil/src/image_ec256.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300225 } else if sig_ed25519 {
226 conf.file("../../boot/bootutil/src/image_ed25519.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200227 }
David Brown63902772017-07-12 09:47:49 -0600228 conf.file("../../boot/bootutil/src/loader.c");
Fabio Utzig031eb7d2019-11-28 10:13:14 -0300229 conf.file("../../boot/bootutil/src/swap_misc.c");
230 conf.file("../../boot/bootutil/src/swap_scratch.c");
231 conf.file("../../boot/bootutil/src/swap_move.c");
David Brown63902772017-07-12 09:47:49 -0600232 conf.file("../../boot/bootutil/src/caps.c");
233 conf.file("../../boot/bootutil/src/bootutil_misc.c");
Fabio Utzig61fd8882019-09-14 20:00:20 -0300234 conf.file("../../boot/bootutil/src/tlv.c");
David Brownd2b18532017-07-12 09:51:31 -0600235 conf.file("csupport/run.c");
David Brown63902772017-07-12 09:47:49 -0600236 conf.include("../../boot/bootutil/include");
Fabio Utzig57c40f72017-12-12 21:48:30 -0200237 conf.include("csupport");
Fabio Utzig9a4b9ba2018-05-07 08:31:27 -0300238 conf.include("../../boot/zephyr/include");
David Brown63902772017-07-12 09:47:49 -0600239 conf.debug(true);
240 conf.flag("-Wall");
David Brown0b693c02017-07-12 12:34:33 -0600241 conf.flag("-Werror");
David Brown63902772017-07-12 09:47:49 -0600242
Fabio Utzig0bccf9d2017-12-07 12:13:57 -0200243 // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90.
244 // It has incomplete std=c11 and std=c99 support but std=c99 was checked
245 // to build correctly so leaving it here to updated in the future...
246 conf.flag("-std=c99");
247
David Brown63902772017-07-12 09:47:49 -0600248 conf.compile("libbootutil.a");
249
250 walk_dir("../../boot").unwrap();
Fabio Utzigc7865402017-12-05 08:50:52 -0200251 walk_dir("../../ext/tinycrypt/lib/source").unwrap();
David Brownb748f6f2019-10-11 10:07:31 -0600252 walk_dir("../../ext/mbedtls-asn1").unwrap();
David Brownd2b18532017-07-12 09:51:31 -0600253 walk_dir("csupport").unwrap();
David Brownf984b952019-10-11 10:32:36 -0600254 walk_dir("../../ext/mbedtls/include").unwrap();
255 walk_dir("../../ext/mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -0600256}
257
258// Output the names of all files within a directory so that Cargo knows when to rebuild.
259fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
260 for ent in fs::read_dir(path.as_ref())? {
261 let ent = ent?;
262 let p = ent.path();
263 if p.is_dir() {
264 walk_dir(p)?;
265 } else {
266 // Note that non-utf8 names will fail.
267 let name = p.to_str().unwrap();
268 if name.ends_with(".c") || name.ends_with(".h") {
269 println!("cargo:rerun-if-changed={}", name);
270 }
271 }
272 }
273
274 Ok(())
275}