blob: 197f3ac97166583b19afac32885f7a738633c4db [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 Utzig90f449e2019-10-24 07:43:53 -030021 let enc_ec256 = env::var("CARGO_FEATURE_ENC_EC256").is_ok();
Fabio Utzig9b97b132018-12-18 17:21:51 -020022 let bootstrap = env::var("CARGO_FEATURE_BOOTSTRAP").is_ok();
David Brown5e6f5e02019-04-04 10:50:05 +070023 let multiimage = env::var("CARGO_FEATURE_MULTIIMAGE").is_ok();
David Brown63902772017-07-12 09:47:49 -060024
Fabio Utzig455cad52018-10-15 14:36:33 -070025 let mut conf = cc::Build::new();
David Brown63902772017-07-12 09:47:49 -060026 conf.define("__BOOTSIM__", None);
Fabio Utzig08fcfe92018-11-26 10:18:18 -020027 conf.define("MCUBOOT_HAVE_LOGGING", None);
David Brown63902772017-07-12 09:47:49 -060028 conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None);
Marti Bolivar248da082018-04-24 15:11:39 -040029 conf.define("MCUBOOT_HAVE_ASSERT_H", None);
Marti Bolivarf9bfddd2018-04-24 14:28:33 -040030 conf.define("MCUBOOT_MAX_IMG_SECTORS", Some("128"));
David Brown5e6f5e02019-04-04 10:50:05 +070031 conf.define("MCUBOOT_IMAGE_NUMBER", Some(if multiimage { "2" } else { "1" }));
Fabio Utzigebdc9692017-11-23 16:28:25 -020032
Fabio Utzig9b97b132018-12-18 17:21:51 -020033 if bootstrap {
34 conf.define("MCUBOOT_BOOTSTRAP", None);
35 }
36
David Vincze2d736ad2019-02-18 11:50:22 +010037 if validate_primary_slot {
38 conf.define("MCUBOOT_VALIDATE_PRIMARY_SLOT", None);
Fabio Utzigebdc9692017-11-23 16:28:25 -020039 }
David Brown63902772017-07-12 09:47:49 -060040
Fabio Utzig39297432019-05-08 18:51:10 -030041 // Currently no more than one sig type can be used simultaneously.
Fabio Utzig97710282019-05-24 17:44:49 -030042 if vec![sig_rsa, sig_rsa3072, sig_ecdsa, sig_ed25519].iter()
Fabio Utzig39297432019-05-08 18:51:10 -030043 .fold(0, |sum, &v| sum + v as i32) > 1 {
44 panic!("mcuboot does not support more than one sig type at the same time");
David Brown704ac6f2017-07-12 10:14:47 -060045 }
David Brown63902772017-07-12 09:47:49 -060046
Fabio Utzig39297432019-05-08 18:51:10 -030047 if sig_rsa || sig_rsa3072 {
David Brown63902772017-07-12 09:47:49 -060048 conf.define("MCUBOOT_SIGN_RSA", None);
Fabio Utzig39297432019-05-08 18:51:10 -030049 // The Kconfig style defines must be added here as well because
50 // they are used internally by "config-rsa.h"
51 if sig_rsa {
52 conf.define("MCUBOOT_SIGN_RSA_LEN", "2048");
53 conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_2048", None);
54 } else {
55 conf.define("MCUBOOT_SIGN_RSA_LEN", "3072");
56 conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_3072", None);
57 }
David Brown63902772017-07-12 09:47:49 -060058 conf.define("MCUBOOT_USE_MBED_TLS", None);
59
David Brownf984b952019-10-11 10:32:36 -060060 conf.include("../../ext/mbedtls/include");
61 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzig806af0e2018-04-26 10:53:54 -030062 conf.file("csupport/keys.c");
David Brown63902772017-07-12 09:47:49 -060063
David Brownf984b952019-10-11 10:32:36 -060064 conf.file("../../ext/mbedtls/library/rsa.c");
65 conf.file("../../ext/mbedtls/library/bignum.c");
66 conf.file("../../ext/mbedtls/library/platform.c");
67 conf.file("../../ext/mbedtls/library/platform_util.c");
68 conf.file("../../ext/mbedtls/library/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060069 } else if sig_ecdsa {
Fabio Utzigc7865402017-12-05 08:50:52 -020070 conf.define("MCUBOOT_SIGN_EC256", None);
David Brown63902772017-07-12 09:47:49 -060071 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigc7865402017-12-05 08:50:52 -020072
Fabio Utzigb4d20c82018-12-27 16:08:39 -020073 if !enc_kw {
David Brownb748f6f2019-10-11 10:07:31 -060074 conf.include("../../ext/mbedtls-asn1/include");
Fabio Utzigb4d20c82018-12-27 16:08:39 -020075 }
Fabio Utzigc7865402017-12-05 08:50:52 -020076 conf.include("../../ext/tinycrypt/lib/include");
77
Fabio Utzig806af0e2018-04-26 10:53:54 -030078 conf.file("csupport/keys.c");
Fabio Utzigc7865402017-12-05 08:50:52 -020079
80 conf.file("../../ext/tinycrypt/lib/source/utils.c");
81 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
82 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
83 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
84 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
85
David Brownb748f6f2019-10-11 10:07:31 -060086 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
87 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
Fabio Utzig97710282019-05-24 17:44:49 -030088 } else if sig_ed25519 {
89 conf.define("MCUBOOT_SIGN_ED25519", None);
90 conf.define("MCUBOOT_USE_MBED_TLS", None);
91
David Brownf984b952019-10-11 10:32:36 -060092 conf.include("../../ext/mbedtls/include");
93 conf.file("../../ext/mbedtls/library/sha256.c");
94 conf.file("../../ext/mbedtls/library/sha512.c");
Fabio Utzig97710282019-05-24 17:44:49 -030095 conf.file("csupport/keys.c");
96 conf.file("../../ext/fiat/src/curve25519.c");
David Brownf984b952019-10-11 10:32:36 -060097 conf.file("../../ext/mbedtls/library/platform.c");
98 conf.file("../../ext/mbedtls/library/platform_util.c");
99 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig90f449e2019-10-24 07:43:53 -0300100 } else if !enc_ec256 {
101 // No signature type, only sha256 validation. The default
Marti Bolivara4818a52018-04-12 13:02:38 -0400102 // configuration file bundled with mbedTLS is sufficient.
Fabio Utzig90f449e2019-10-24 07:43:53 -0300103 // When using ECIES-P256 rely on Tinycrypt.
David Brown704ac6f2017-07-12 10:14:47 -0600104 conf.define("MCUBOOT_USE_MBED_TLS", None);
David Brownf984b952019-10-11 10:32:36 -0600105 conf.include("../../ext/mbedtls/include");
106 conf.file("../../ext/mbedtls/library/sha256.c");
David Brown63902772017-07-12 09:47:49 -0600107 }
108
109 if overwrite_only {
110 conf.define("MCUBOOT_OVERWRITE_ONLY", None);
Fabio Utzig13d9e352017-10-05 20:32:31 -0300111 conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None);
David Brown63902772017-07-12 09:47:49 -0600112 }
113
Fabio Utzig1e48b912018-09-18 09:04:18 -0300114 if enc_rsa {
115 conf.define("MCUBOOT_ENCRYPT_RSA", None);
116 conf.define("MCUBOOT_ENC_IMAGES", None);
117 conf.define("MCUBOOT_USE_MBED_TLS", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300118
119 conf.file("../../boot/bootutil/src/encrypted.c");
120 conf.file("csupport/keys.c");
121
David Brownf984b952019-10-11 10:32:36 -0600122 conf.include("../../ext/mbedtls/include");
123 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300124
David Brownf984b952019-10-11 10:32:36 -0600125 conf.file("../../ext/mbedtls/library/platform.c");
126 conf.file("../../ext/mbedtls/library/platform_util.c");
127 conf.file("../../ext/mbedtls/library/rsa.c");
128 conf.file("../../ext/mbedtls/library/rsa_internal.c");
129 conf.file("../../ext/mbedtls/library/md.c");
130 conf.file("../../ext/mbedtls/library/md_wrap.c");
131 conf.file("../../ext/mbedtls/library/aes.c");
132 conf.file("../../ext/mbedtls/library/bignum.c");
133 conf.file("../../ext/mbedtls/library/asn1parse.c");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300134 }
135
136 if enc_kw {
137 conf.define("MCUBOOT_ENCRYPT_KW", None);
138 conf.define("MCUBOOT_ENC_IMAGES", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300139
140 conf.file("../../boot/bootutil/src/encrypted.c");
141 conf.file("csupport/keys.c");
142
Fabio Utzig39297432019-05-08 18:51:10 -0300143 if sig_rsa || sig_rsa3072 {
David Brownf984b952019-10-11 10:32:36 -0600144 conf.file("../../ext/mbedtls/library/sha256.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200145 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300146
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200147 /* Simulator uses Mbed-TLS to wrap keys */
David Brownf984b952019-10-11 10:32:36 -0600148 conf.include("../../ext/mbedtls/include");
149 conf.file("../../ext/mbedtls/library/platform.c");
150 conf.file("../../ext/mbedtls/library/platform_util.c");
151 conf.file("../../ext/mbedtls/library/nist_kw.c");
152 conf.file("../../ext/mbedtls/library/cipher.c");
153 conf.file("../../ext/mbedtls/library/cipher_wrap.c");
154 conf.file("../../ext/mbedtls/library/aes.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200155
156 if sig_ecdsa {
157 conf.define("MCUBOOT_USE_TINYCRYPT", None);
158
159 conf.include("../../ext/tinycrypt/lib/include");
160
161 conf.file("../../ext/tinycrypt/lib/source/utils.c");
162 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
163 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
164 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
165 }
Fabio Utzig97710282019-05-24 17:44:49 -0300166
167 if sig_ed25519 {
168 panic!("ed25519 does not support image encryption with KW yet");
169 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300170 }
171
Fabio Utzig90f449e2019-10-24 07:43:53 -0300172 if enc_ec256 {
173 conf.define("MCUBOOT_ENCRYPT_EC256", None);
174 conf.define("MCUBOOT_ENC_IMAGES", None);
175 conf.define("MCUBOOT_USE_TINYCRYPT", None);
176
177 conf.file("../../boot/bootutil/src/encrypted.c");
178 conf.file("csupport/keys.c");
179
180 conf.include("../../ext/mbedtls-asn1/include");
181 conf.include("../../ext/tinycrypt/lib/include");
182
183 /* FIXME: fail with other signature schemes ? */
184
185 conf.file("../../ext/tinycrypt/lib/source/utils.c");
186 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
187 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
188 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
189 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
190
191 conf.file("../../ext/mbedtls-asn1/src/platform_util.c");
192 conf.file("../../ext/mbedtls-asn1/src/asn1parse.c");
193
194 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
195 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
196 conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c");
197 conf.file("../../ext/tinycrypt/lib/source/hmac.c");
198 conf.file("../../ext/tinycrypt/lib/source/ecc_dh.c");
199 }
200
201
Fabio Utzig251ef1d2018-12-18 17:20:19 -0200202 if sig_rsa && enc_kw {
203 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa-kw.h>"));
Fabio Utzig39297432019-05-08 18:51:10 -0300204 } else if sig_rsa || sig_rsa3072 || enc_rsa {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200205 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
Fabio Utzig90f449e2019-10-24 07:43:53 -0300206 } else if (sig_ecdsa || enc_ec256) && !enc_kw {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200207 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Fabio Utzig97710282019-05-24 17:44:49 -0300208 } else if sig_ed25519 {
209 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ed25519.h>"));
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200210 } else if enc_kw {
211 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
212 }
213
David Brown704ac6f2017-07-12 10:14:47 -0600214 conf.file("../../boot/bootutil/src/image_validate.c");
Fabio Utzig39297432019-05-08 18:51:10 -0300215 if sig_rsa || sig_rsa3072 {
Fabio Utzigc7865402017-12-05 08:50:52 -0200216 conf.file("../../boot/bootutil/src/image_rsa.c");
217 } else if sig_ecdsa {
218 conf.file("../../boot/bootutil/src/image_ec256.c");
Fabio Utzig97710282019-05-24 17:44:49 -0300219 } else if sig_ed25519 {
220 conf.file("../../boot/bootutil/src/image_ed25519.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200221 }
David Brown63902772017-07-12 09:47:49 -0600222 conf.file("../../boot/bootutil/src/loader.c");
223 conf.file("../../boot/bootutil/src/caps.c");
224 conf.file("../../boot/bootutil/src/bootutil_misc.c");
Fabio Utzig61fd8882019-09-14 20:00:20 -0300225 conf.file("../../boot/bootutil/src/tlv.c");
David Brownd2b18532017-07-12 09:51:31 -0600226 conf.file("csupport/run.c");
David Brown63902772017-07-12 09:47:49 -0600227 conf.include("../../boot/bootutil/include");
Fabio Utzig57c40f72017-12-12 21:48:30 -0200228 conf.include("csupport");
Fabio Utzig9a4b9ba2018-05-07 08:31:27 -0300229 conf.include("../../boot/zephyr/include");
David Brown63902772017-07-12 09:47:49 -0600230 conf.debug(true);
231 conf.flag("-Wall");
David Brown0b693c02017-07-12 12:34:33 -0600232 conf.flag("-Werror");
David Brown63902772017-07-12 09:47:49 -0600233
Fabio Utzig0bccf9d2017-12-07 12:13:57 -0200234 // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90.
235 // It has incomplete std=c11 and std=c99 support but std=c99 was checked
236 // to build correctly so leaving it here to updated in the future...
237 conf.flag("-std=c99");
238
David Brown63902772017-07-12 09:47:49 -0600239 conf.compile("libbootutil.a");
240
241 walk_dir("../../boot").unwrap();
Fabio Utzigc7865402017-12-05 08:50:52 -0200242 walk_dir("../../ext/tinycrypt/lib/source").unwrap();
David Brownb748f6f2019-10-11 10:07:31 -0600243 walk_dir("../../ext/mbedtls-asn1").unwrap();
David Brownd2b18532017-07-12 09:51:31 -0600244 walk_dir("csupport").unwrap();
David Brownf984b952019-10-11 10:32:36 -0600245 walk_dir("../../ext/mbedtls/include").unwrap();
246 walk_dir("../../ext/mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -0600247}
248
249// Output the names of all files within a directory so that Cargo knows when to rebuild.
250fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
251 for ent in fs::read_dir(path.as_ref())? {
252 let ent = ent?;
253 let p = ent.path();
254 if p.is_dir() {
255 walk_dir(p)?;
256 } else {
257 // Note that non-utf8 names will fail.
258 let name = p.to_str().unwrap();
259 if name.ends_with(".c") || name.ends_with(".h") {
260 println!("cargo:rerun-if-changed={}", name);
261 }
262 }
263 }
264
265 Ok(())
266}