blob: 922219f8b10c89a94f86ce36a3b9710c1564e7b1 [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();
David Vincze2d736ad2019-02-18 11:50:22 +010017 let validate_primary_slot =
18 env::var("CARGO_FEATURE_VALIDATE_PRIMARY_SLOT").is_ok();
Fabio Utzig1e48b912018-09-18 09:04:18 -030019 let enc_rsa = env::var("CARGO_FEATURE_ENC_RSA").is_ok();
20 let enc_kw = env::var("CARGO_FEATURE_ENC_KW").is_ok();
Fabio Utzig9b97b132018-12-18 17:21:51 -020021 let bootstrap = env::var("CARGO_FEATURE_BOOTSTRAP").is_ok();
David Brown5e6f5e02019-04-04 10:50:05 +070022 let multiimage = env::var("CARGO_FEATURE_MULTIIMAGE").is_ok();
David Brown63902772017-07-12 09:47:49 -060023
Fabio Utzig455cad52018-10-15 14:36:33 -070024 let mut conf = cc::Build::new();
David Brown63902772017-07-12 09:47:49 -060025 conf.define("__BOOTSIM__", None);
Fabio Utzig08fcfe92018-11-26 10:18:18 -020026 conf.define("MCUBOOT_HAVE_LOGGING", None);
David Brown63902772017-07-12 09:47:49 -060027 conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None);
Marti Bolivar248da082018-04-24 15:11:39 -040028 conf.define("MCUBOOT_HAVE_ASSERT_H", None);
Marti Bolivarf9bfddd2018-04-24 14:28:33 -040029 conf.define("MCUBOOT_MAX_IMG_SECTORS", Some("128"));
David Brown5e6f5e02019-04-04 10:50:05 +070030 conf.define("MCUBOOT_IMAGE_NUMBER", Some(if multiimage { "2" } else { "1" }));
Fabio Utzigebdc9692017-11-23 16:28:25 -020031
Fabio Utzig9b97b132018-12-18 17:21:51 -020032 if bootstrap {
33 conf.define("MCUBOOT_BOOTSTRAP", None);
34 }
35
David Vincze2d736ad2019-02-18 11:50:22 +010036 if validate_primary_slot {
37 conf.define("MCUBOOT_VALIDATE_PRIMARY_SLOT", None);
Fabio Utzigebdc9692017-11-23 16:28:25 -020038 }
David Brown63902772017-07-12 09:47:49 -060039
Fabio Utzig39297432019-05-08 18:51:10 -030040 // Currently no more than one sig type can be used simultaneously.
Fabio Utzig97710282019-05-24 17:44:49 -030041 if vec![sig_rsa, sig_rsa3072, sig_ecdsa, sig_ed25519].iter()
Fabio Utzig39297432019-05-08 18:51:10 -030042 .fold(0, |sum, &v| sum + v as i32) > 1 {
43 panic!("mcuboot does not support more than one sig type at the same time");
David Brown704ac6f2017-07-12 10:14:47 -060044 }
David Brown63902772017-07-12 09:47:49 -060045
Fabio Utzig39297432019-05-08 18:51:10 -030046 if sig_rsa || sig_rsa3072 {
David Brown63902772017-07-12 09:47:49 -060047 conf.define("MCUBOOT_SIGN_RSA", None);
Fabio Utzig39297432019-05-08 18:51:10 -030048 // The Kconfig style defines must be added here as well because
49 // they are used internally by "config-rsa.h"
50 if sig_rsa {
51 conf.define("MCUBOOT_SIGN_RSA_LEN", "2048");
52 conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_2048", None);
53 } else {
54 conf.define("MCUBOOT_SIGN_RSA_LEN", "3072");
55 conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_3072", None);
56 }
David Brown63902772017-07-12 09:47:49 -060057 conf.define("MCUBOOT_USE_MBED_TLS", None);
58
David Brown82bf7c22017-07-12 09:49:31 -060059 conf.include("mbedtls/include");
60 conf.file("mbedtls/library/sha256.c");
Fabio Utzig806af0e2018-04-26 10:53:54 -030061 conf.file("csupport/keys.c");
David Brown63902772017-07-12 09:47:49 -060062
David Brown82bf7c22017-07-12 09:49:31 -060063 conf.file("mbedtls/library/rsa.c");
64 conf.file("mbedtls/library/bignum.c");
Fabio Utzigb04afa92018-09-12 15:27:04 -030065 conf.file("mbedtls/library/platform.c");
66 conf.file("mbedtls/library/platform_util.c");
David Brown82bf7c22017-07-12 09:49:31 -060067 conf.file("mbedtls/library/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060068 } else if sig_ecdsa {
Fabio Utzigc7865402017-12-05 08:50:52 -020069 conf.define("MCUBOOT_SIGN_EC256", None);
David Brown63902772017-07-12 09:47:49 -060070 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigc7865402017-12-05 08:50:52 -020071
Fabio Utzigb4d20c82018-12-27 16:08:39 -020072 if !enc_kw {
73 conf.include("../../ext/mbedtls/include");
74 }
Fabio Utzigc7865402017-12-05 08:50:52 -020075 conf.include("../../ext/tinycrypt/lib/include");
76
Fabio Utzig806af0e2018-04-26 10:53:54 -030077 conf.file("csupport/keys.c");
Fabio Utzigc7865402017-12-05 08:50:52 -020078
79 conf.file("../../ext/tinycrypt/lib/source/utils.c");
80 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
81 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
82 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
83 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
84
Fabio Utzigb4d20c82018-12-27 16:08:39 -020085 conf.file("../../ext/mbedtls/src/platform_util.c");
Fabio Utzigba05f2a2017-12-05 11:00:41 -020086 conf.file("../../ext/mbedtls/src/asn1parse.c");
Fabio Utzig97710282019-05-24 17:44:49 -030087 } else if sig_ed25519 {
88 conf.define("MCUBOOT_SIGN_ED25519", None);
89 conf.define("MCUBOOT_USE_MBED_TLS", None);
90
91 conf.include("mbedtls/include");
92 conf.file("mbedtls/library/sha256.c");
93 conf.file("mbedtls/library/sha512.c");
94 conf.file("csupport/keys.c");
95 conf.file("../../ext/fiat/src/curve25519.c");
96 conf.file("mbedtls/library/platform.c");
97 conf.file("mbedtls/library/platform_util.c");
98 conf.file("mbedtls/library/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060099 } else {
Marti Bolivara4818a52018-04-12 13:02:38 -0400100 // Neither signature type, only verify sha256. The default
101 // configuration file bundled with mbedTLS is sufficient.
David Brown704ac6f2017-07-12 10:14:47 -0600102 conf.define("MCUBOOT_USE_MBED_TLS", None);
David Brown704ac6f2017-07-12 10:14:47 -0600103 conf.include("mbedtls/include");
104 conf.file("mbedtls/library/sha256.c");
David Brown63902772017-07-12 09:47:49 -0600105 }
106
107 if overwrite_only {
108 conf.define("MCUBOOT_OVERWRITE_ONLY", None);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300109 conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None);
David Brown63902772017-07-12 09:47:49 -0600110 }
111
Fabio Utzig1e48b912018-09-18 09:04:18 -0300112 if enc_rsa {
113 conf.define("MCUBOOT_ENCRYPT_RSA", None);
114 conf.define("MCUBOOT_ENC_IMAGES", None);
115 conf.define("MCUBOOT_USE_MBED_TLS", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300116
117 conf.file("../../boot/bootutil/src/encrypted.c");
118 conf.file("csupport/keys.c");
119
120 conf.include("mbedtls/include");
121 conf.file("mbedtls/library/sha256.c");
122
123 conf.file("mbedtls/library/platform.c");
124 conf.file("mbedtls/library/platform_util.c");
125 conf.file("mbedtls/library/rsa.c");
126 conf.file("mbedtls/library/rsa_internal.c");
127 conf.file("mbedtls/library/md.c");
128 conf.file("mbedtls/library/md_wrap.c");
129 conf.file("mbedtls/library/aes.c");
130 conf.file("mbedtls/library/bignum.c");
131 conf.file("mbedtls/library/asn1parse.c");
132 }
133
134 if enc_kw {
135 conf.define("MCUBOOT_ENCRYPT_KW", None);
136 conf.define("MCUBOOT_ENC_IMAGES", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300137
138 conf.file("../../boot/bootutil/src/encrypted.c");
139 conf.file("csupport/keys.c");
140
Fabio Utzig39297432019-05-08 18:51:10 -0300141 if sig_rsa || sig_rsa3072 {
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200142 conf.file("mbedtls/library/sha256.c");
143 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300144
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200145 /* Simulator uses Mbed-TLS to wrap keys */
146 conf.include("mbedtls/include");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300147 conf.file("mbedtls/library/platform.c");
148 conf.file("mbedtls/library/platform_util.c");
149 conf.file("mbedtls/library/nist_kw.c");
150 conf.file("mbedtls/library/cipher.c");
151 conf.file("mbedtls/library/cipher_wrap.c");
152 conf.file("mbedtls/library/aes.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200153
154 if sig_ecdsa {
155 conf.define("MCUBOOT_USE_TINYCRYPT", None);
156
157 conf.include("../../ext/tinycrypt/lib/include");
158
159 conf.file("../../ext/tinycrypt/lib/source/utils.c");
160 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
161 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
162 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
163 }
Fabio Utzig97710282019-05-24 17:44:49 -0300164
165 if sig_ed25519 {
166 panic!("ed25519 does not support image encryption with KW yet");
167 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300168 }
169
Fabio Utzig251ef1d2018-12-18 17:20:19 -0200170 if sig_rsa && enc_kw {
171 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa-kw.h>"));
Fabio Utzig39297432019-05-08 18:51:10 -0300172 } else if sig_rsa || sig_rsa3072 || enc_rsa {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200173 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200174 } else if sig_ecdsa && !enc_kw {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200175 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Fabio Utzig97710282019-05-24 17:44:49 -0300176 } else if sig_ed25519 {
177 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ed25519.h>"));
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200178 } else if enc_kw {
179 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
180 }
181
David Brown704ac6f2017-07-12 10:14:47 -0600182 conf.file("../../boot/bootutil/src/image_validate.c");
Fabio Utzig39297432019-05-08 18:51:10 -0300183 if sig_rsa || sig_rsa3072 {
Fabio Utzigc7865402017-12-05 08:50:52 -0200184 conf.file("../../boot/bootutil/src/image_rsa.c");
185 } else if sig_ecdsa {
186 conf.file("../../boot/bootutil/src/image_ec256.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300187 } else if sig_ed25519 {
188 conf.file("../../boot/bootutil/src/image_ed25519.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200189 }
David Brown63902772017-07-12 09:47:49 -0600190 conf.file("../../boot/bootutil/src/loader.c");
191 conf.file("../../boot/bootutil/src/caps.c");
192 conf.file("../../boot/bootutil/src/bootutil_misc.c");
Fabio Utzig61fd8882019-09-14 20:00:20 -0300193 conf.file("../../boot/bootutil/src/tlv.c");
David Brownd2b18532017-07-12 09:51:31 -0600194 conf.file("csupport/run.c");
David Brown63902772017-07-12 09:47:49 -0600195 conf.include("../../boot/bootutil/include");
Fabio Utzig57c40f72017-12-12 21:48:30 -0200196 conf.include("csupport");
Fabio Utzig9a4b9ba2018-05-07 08:31:27 -0300197 conf.include("../../boot/zephyr/include");
David Brown63902772017-07-12 09:47:49 -0600198 conf.debug(true);
199 conf.flag("-Wall");
David Brown0b693c02017-07-12 12:34:33 -0600200 conf.flag("-Werror");
David Brown63902772017-07-12 09:47:49 -0600201
Fabio Utzig0bccf9d2017-12-07 12:13:57 -0200202 // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90.
203 // It has incomplete std=c11 and std=c99 support but std=c99 was checked
204 // to build correctly so leaving it here to updated in the future...
205 conf.flag("-std=c99");
206
David Brown63902772017-07-12 09:47:49 -0600207 conf.compile("libbootutil.a");
208
209 walk_dir("../../boot").unwrap();
Fabio Utzigc7865402017-12-05 08:50:52 -0200210 walk_dir("../../ext/tinycrypt/lib/source").unwrap();
Fabio Utzigd32fd642017-12-18 15:19:47 -0200211 walk_dir("../../ext/mbedtls").unwrap();
David Brownd2b18532017-07-12 09:51:31 -0600212 walk_dir("csupport").unwrap();
David Brown82bf7c22017-07-12 09:49:31 -0600213 walk_dir("mbedtls/include").unwrap();
214 walk_dir("mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -0600215}
216
217// Output the names of all files within a directory so that Cargo knows when to rebuild.
218fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
219 for ent in fs::read_dir(path.as_ref())? {
220 let ent = ent?;
221 let p = ent.path();
222 if p.is_dir() {
223 walk_dir(p)?;
224 } else {
225 // Note that non-utf8 names will fail.
226 let name = p.to_str().unwrap();
227 if name.ends_with(".c") || name.ends_with(".h") {
228 println!("cargo:rerun-if-changed={}", name);
229 }
230 }
231 }
232
233 Ok(())
234}