blob: 088b470931687265533f60a95f742e519b5828f1 [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();
13 let sig_ecdsa = env::var("CARGO_FEATURE_SIG_ECDSA").is_ok();
14 let overwrite_only = env::var("CARGO_FEATURE_OVERWRITE_ONLY").is_ok();
David Vincze2d736ad2019-02-18 11:50:22 +010015 let validate_primary_slot =
16 env::var("CARGO_FEATURE_VALIDATE_PRIMARY_SLOT").is_ok();
Fabio Utzig1e48b912018-09-18 09:04:18 -030017 let enc_rsa = env::var("CARGO_FEATURE_ENC_RSA").is_ok();
18 let enc_kw = env::var("CARGO_FEATURE_ENC_KW").is_ok();
Fabio Utzig9b97b132018-12-18 17:21:51 -020019 let bootstrap = env::var("CARGO_FEATURE_BOOTSTRAP").is_ok();
David Brown63902772017-07-12 09:47:49 -060020
Fabio Utzig455cad52018-10-15 14:36:33 -070021 let mut conf = cc::Build::new();
David Brown63902772017-07-12 09:47:49 -060022 conf.define("__BOOTSIM__", None);
Fabio Utzig08fcfe92018-11-26 10:18:18 -020023 conf.define("MCUBOOT_HAVE_LOGGING", None);
David Brown63902772017-07-12 09:47:49 -060024 conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None);
Marti Bolivar248da082018-04-24 15:11:39 -040025 conf.define("MCUBOOT_HAVE_ASSERT_H", None);
Marti Bolivarf9bfddd2018-04-24 14:28:33 -040026 conf.define("MCUBOOT_MAX_IMG_SECTORS", Some("128"));
Fabio Utzigebdc9692017-11-23 16:28:25 -020027
Fabio Utzig9b97b132018-12-18 17:21:51 -020028 if bootstrap {
29 conf.define("MCUBOOT_BOOTSTRAP", None);
30 }
31
David Vincze2d736ad2019-02-18 11:50:22 +010032 if validate_primary_slot {
33 conf.define("MCUBOOT_VALIDATE_PRIMARY_SLOT", None);
Fabio Utzigebdc9692017-11-23 16:28:25 -020034 }
David Brown63902772017-07-12 09:47:49 -060035
David Brown704ac6f2017-07-12 10:14:47 -060036 // Currently, mbed TLS cannot build with both RSA and ECDSA.
37 if sig_rsa && sig_ecdsa {
38 panic!("mcuboot does not support RSA and ECDSA at the same time");
39 }
David Brown63902772017-07-12 09:47:49 -060040
David Brown704ac6f2017-07-12 10:14:47 -060041 if sig_rsa {
David Brown63902772017-07-12 09:47:49 -060042 conf.define("MCUBOOT_SIGN_RSA", None);
43 conf.define("MCUBOOT_USE_MBED_TLS", None);
44
David Brown82bf7c22017-07-12 09:49:31 -060045 conf.include("mbedtls/include");
46 conf.file("mbedtls/library/sha256.c");
Fabio Utzig806af0e2018-04-26 10:53:54 -030047 conf.file("csupport/keys.c");
David Brown63902772017-07-12 09:47:49 -060048
David Brown82bf7c22017-07-12 09:49:31 -060049 conf.file("mbedtls/library/rsa.c");
50 conf.file("mbedtls/library/bignum.c");
Fabio Utzigb04afa92018-09-12 15:27:04 -030051 conf.file("mbedtls/library/platform.c");
52 conf.file("mbedtls/library/platform_util.c");
David Brown82bf7c22017-07-12 09:49:31 -060053 conf.file("mbedtls/library/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060054 } else if sig_ecdsa {
Fabio Utzigc7865402017-12-05 08:50:52 -020055 conf.define("MCUBOOT_SIGN_EC256", None);
David Brown63902772017-07-12 09:47:49 -060056 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigc7865402017-12-05 08:50:52 -020057
Fabio Utzigb4d20c82018-12-27 16:08:39 -020058 if !enc_kw {
59 conf.include("../../ext/mbedtls/include");
60 }
Fabio Utzigc7865402017-12-05 08:50:52 -020061 conf.include("../../ext/tinycrypt/lib/include");
62
Fabio Utzig806af0e2018-04-26 10:53:54 -030063 conf.file("csupport/keys.c");
Fabio Utzigc7865402017-12-05 08:50:52 -020064
65 conf.file("../../ext/tinycrypt/lib/source/utils.c");
66 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
67 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
68 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
69 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
70
Fabio Utzigb4d20c82018-12-27 16:08:39 -020071 conf.file("../../ext/mbedtls/src/platform_util.c");
Fabio Utzigba05f2a2017-12-05 11:00:41 -020072 conf.file("../../ext/mbedtls/src/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060073 } else {
Marti Bolivara4818a52018-04-12 13:02:38 -040074 // Neither signature type, only verify sha256. The default
75 // configuration file bundled with mbedTLS is sufficient.
David Brown704ac6f2017-07-12 10:14:47 -060076 conf.define("MCUBOOT_USE_MBED_TLS", None);
David Brown704ac6f2017-07-12 10:14:47 -060077 conf.include("mbedtls/include");
78 conf.file("mbedtls/library/sha256.c");
David Brown63902772017-07-12 09:47:49 -060079 }
80
81 if overwrite_only {
82 conf.define("MCUBOOT_OVERWRITE_ONLY", None);
Fabio Utzig13d9e352017-10-05 20:32:31 -030083 conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None);
David Brown63902772017-07-12 09:47:49 -060084 }
85
Fabio Utzig1e48b912018-09-18 09:04:18 -030086 if enc_rsa {
87 conf.define("MCUBOOT_ENCRYPT_RSA", None);
88 conf.define("MCUBOOT_ENC_IMAGES", None);
89 conf.define("MCUBOOT_USE_MBED_TLS", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -030090
91 conf.file("../../boot/bootutil/src/encrypted.c");
92 conf.file("csupport/keys.c");
93
94 conf.include("mbedtls/include");
95 conf.file("mbedtls/library/sha256.c");
96
97 conf.file("mbedtls/library/platform.c");
98 conf.file("mbedtls/library/platform_util.c");
99 conf.file("mbedtls/library/rsa.c");
100 conf.file("mbedtls/library/rsa_internal.c");
101 conf.file("mbedtls/library/md.c");
102 conf.file("mbedtls/library/md_wrap.c");
103 conf.file("mbedtls/library/aes.c");
104 conf.file("mbedtls/library/bignum.c");
105 conf.file("mbedtls/library/asn1parse.c");
106 }
107
108 if enc_kw {
109 conf.define("MCUBOOT_ENCRYPT_KW", None);
110 conf.define("MCUBOOT_ENC_IMAGES", None);
Fabio Utzig1e48b912018-09-18 09:04:18 -0300111
112 conf.file("../../boot/bootutil/src/encrypted.c");
113 conf.file("csupport/keys.c");
114
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200115 if sig_rsa {
116 conf.file("mbedtls/library/sha256.c");
117 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300118
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200119 /* Simulator uses Mbed-TLS to wrap keys */
120 conf.include("mbedtls/include");
Fabio Utzig1e48b912018-09-18 09:04:18 -0300121 conf.file("mbedtls/library/platform.c");
122 conf.file("mbedtls/library/platform_util.c");
123 conf.file("mbedtls/library/nist_kw.c");
124 conf.file("mbedtls/library/cipher.c");
125 conf.file("mbedtls/library/cipher_wrap.c");
126 conf.file("mbedtls/library/aes.c");
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200127
128 if sig_ecdsa {
129 conf.define("MCUBOOT_USE_TINYCRYPT", None);
130
131 conf.include("../../ext/tinycrypt/lib/include");
132
133 conf.file("../../ext/tinycrypt/lib/source/utils.c");
134 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
135 conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c");
136 conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c");
137 }
Fabio Utzig1e48b912018-09-18 09:04:18 -0300138 }
139
Fabio Utzig251ef1d2018-12-18 17:20:19 -0200140 if sig_rsa && enc_kw {
141 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa-kw.h>"));
142 } else if sig_rsa || enc_rsa {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200143 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
Fabio Utzigb4d20c82018-12-27 16:08:39 -0200144 } else if sig_ecdsa && !enc_kw {
Fabio Utzig04fd63e2018-12-14 06:43:31 -0200145 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
146 } else if enc_kw {
147 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>"));
148 }
149
David Brown704ac6f2017-07-12 10:14:47 -0600150 conf.file("../../boot/bootutil/src/image_validate.c");
Fabio Utzigc7865402017-12-05 08:50:52 -0200151 if sig_rsa {
152 conf.file("../../boot/bootutil/src/image_rsa.c");
153 } else if sig_ecdsa {
154 conf.file("../../boot/bootutil/src/image_ec256.c");
155 }
David Brown63902772017-07-12 09:47:49 -0600156 conf.file("../../boot/bootutil/src/loader.c");
157 conf.file("../../boot/bootutil/src/caps.c");
158 conf.file("../../boot/bootutil/src/bootutil_misc.c");
David Brownd2b18532017-07-12 09:51:31 -0600159 conf.file("csupport/run.c");
David Brown63902772017-07-12 09:47:49 -0600160 conf.include("../../boot/bootutil/include");
Fabio Utzig57c40f72017-12-12 21:48:30 -0200161 conf.include("csupport");
Fabio Utzig9a4b9ba2018-05-07 08:31:27 -0300162 conf.include("../../boot/zephyr/include");
David Brown63902772017-07-12 09:47:49 -0600163 conf.debug(true);
164 conf.flag("-Wall");
David Brown0b693c02017-07-12 12:34:33 -0600165 conf.flag("-Werror");
David Brown63902772017-07-12 09:47:49 -0600166
Fabio Utzig0bccf9d2017-12-07 12:13:57 -0200167 // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90.
168 // It has incomplete std=c11 and std=c99 support but std=c99 was checked
169 // to build correctly so leaving it here to updated in the future...
170 conf.flag("-std=c99");
171
David Brown63902772017-07-12 09:47:49 -0600172 conf.compile("libbootutil.a");
173
174 walk_dir("../../boot").unwrap();
Fabio Utzigc7865402017-12-05 08:50:52 -0200175 walk_dir("../../ext/tinycrypt/lib/source").unwrap();
Fabio Utzigd32fd642017-12-18 15:19:47 -0200176 walk_dir("../../ext/mbedtls").unwrap();
David Brownd2b18532017-07-12 09:51:31 -0600177 walk_dir("csupport").unwrap();
David Brown82bf7c22017-07-12 09:49:31 -0600178 walk_dir("mbedtls/include").unwrap();
179 walk_dir("mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -0600180}
181
182// Output the names of all files within a directory so that Cargo knows when to rebuild.
183fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
184 for ent in fs::read_dir(path.as_ref())? {
185 let ent = ent?;
186 let p = ent.path();
187 if p.is_dir() {
188 walk_dir(p)?;
189 } else {
190 // Note that non-utf8 names will fail.
191 let name = p.to_str().unwrap();
192 if name.ends_with(".c") || name.ends_with(".h") {
193 println!("cargo:rerun-if-changed={}", name);
194 }
195 }
196 }
197
198 Ok(())
199}