David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 1 | // Build mcuboot as a library, based on the requested features. |
| 2 | |
| 3 | extern crate gcc; |
| 4 | |
| 5 | use std::env; |
| 6 | use std::fs; |
| 7 | use std::io; |
| 8 | use std::path::Path; |
| 9 | |
| 10 | fn 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(); |
Fabio Utzig | ebdc969 | 2017-11-23 16:28:25 -0200 | [diff] [blame] | 15 | let validate_slot0 = env::var("CARGO_FEATURE_VALIDATE_SLOT0").is_ok(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 16 | |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 17 | let mut conf = gcc::Build::new(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 18 | conf.define("__BOOTSIM__", None); |
| 19 | conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None); |
Marti Bolivar | f9bfddd | 2018-04-24 14:28:33 -0400 | [diff] [blame^] | 20 | conf.define("MCUBOOT_MAX_IMG_SECTORS", Some("128")); |
Fabio Utzig | ebdc969 | 2017-11-23 16:28:25 -0200 | [diff] [blame] | 21 | |
| 22 | if validate_slot0 { |
| 23 | conf.define("MCUBOOT_VALIDATE_SLOT0", None); |
| 24 | } |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 25 | |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 26 | // Currently, mbed TLS cannot build with both RSA and ECDSA. |
| 27 | if sig_rsa && sig_ecdsa { |
| 28 | panic!("mcuboot does not support RSA and ECDSA at the same time"); |
| 29 | } |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 30 | |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 31 | if sig_rsa { |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 32 | conf.define("MCUBOOT_SIGN_RSA", None); |
| 33 | conf.define("MCUBOOT_USE_MBED_TLS", None); |
| 34 | |
Marti Bolivar | a4818a5 | 2018-04-12 13:02:38 -0400 | [diff] [blame] | 35 | conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>")); |
David Brown | 82bf7c2 | 2017-07-12 09:49:31 -0600 | [diff] [blame] | 36 | conf.include("mbedtls/include"); |
| 37 | conf.file("mbedtls/library/sha256.c"); |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 38 | conf.file("../../boot/zephyr/keys.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 39 | |
David Brown | 82bf7c2 | 2017-07-12 09:49:31 -0600 | [diff] [blame] | 40 | conf.file("mbedtls/library/rsa.c"); |
| 41 | conf.file("mbedtls/library/bignum.c"); |
| 42 | conf.file("mbedtls/library/asn1parse.c"); |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 43 | } else if sig_ecdsa { |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 44 | conf.define("MCUBOOT_SIGN_EC256", None); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 45 | conf.define("MCUBOOT_USE_TINYCRYPT", None); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 46 | |
Marti Bolivar | a4818a5 | 2018-04-12 13:02:38 -0400 | [diff] [blame] | 47 | conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>")); |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 48 | conf.include("../../ext/mbedtls/include"); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 49 | conf.include("../../ext/tinycrypt/lib/include"); |
| 50 | |
| 51 | conf.file("../../boot/zephyr/keys.c"); |
| 52 | |
| 53 | conf.file("../../ext/tinycrypt/lib/source/utils.c"); |
| 54 | conf.file("../../ext/tinycrypt/lib/source/sha256.c"); |
| 55 | conf.file("../../ext/tinycrypt/lib/source/ecc.c"); |
| 56 | conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c"); |
| 57 | conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c"); |
| 58 | |
Fabio Utzig | ba05f2a | 2017-12-05 11:00:41 -0200 | [diff] [blame] | 59 | conf.file("../../ext/mbedtls/src/asn1parse.c"); |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 60 | } else { |
Marti Bolivar | a4818a5 | 2018-04-12 13:02:38 -0400 | [diff] [blame] | 61 | // Neither signature type, only verify sha256. The default |
| 62 | // configuration file bundled with mbedTLS is sufficient. |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 63 | conf.define("MCUBOOT_USE_MBED_TLS", None); |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 64 | conf.include("mbedtls/include"); |
| 65 | conf.file("mbedtls/library/sha256.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | if overwrite_only { |
| 69 | conf.define("MCUBOOT_OVERWRITE_ONLY", None); |
Fabio Utzig | 13d9e35 | 2017-10-05 20:32:31 -0300 | [diff] [blame] | 70 | conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 71 | } |
| 72 | |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 73 | conf.file("../../boot/bootutil/src/image_validate.c"); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 74 | if sig_rsa { |
| 75 | conf.file("../../boot/bootutil/src/image_rsa.c"); |
| 76 | } else if sig_ecdsa { |
| 77 | conf.file("../../boot/bootutil/src/image_ec256.c"); |
| 78 | } |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 79 | conf.file("../../boot/bootutil/src/loader.c"); |
| 80 | conf.file("../../boot/bootutil/src/caps.c"); |
| 81 | conf.file("../../boot/bootutil/src/bootutil_misc.c"); |
David Brown | d2b1853 | 2017-07-12 09:51:31 -0600 | [diff] [blame] | 82 | conf.file("csupport/run.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 83 | conf.include("../../boot/bootutil/include"); |
| 84 | conf.include("../../boot/zephyr/include"); |
Fabio Utzig | 57c40f7 | 2017-12-12 21:48:30 -0200 | [diff] [blame] | 85 | conf.include("csupport"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 86 | conf.debug(true); |
| 87 | conf.flag("-Wall"); |
David Brown | 0b693c0 | 2017-07-12 12:34:33 -0600 | [diff] [blame] | 88 | conf.flag("-Werror"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 89 | |
Fabio Utzig | 0bccf9d | 2017-12-07 12:13:57 -0200 | [diff] [blame] | 90 | // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90. |
| 91 | // It has incomplete std=c11 and std=c99 support but std=c99 was checked |
| 92 | // to build correctly so leaving it here to updated in the future... |
| 93 | conf.flag("-std=c99"); |
| 94 | |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 95 | conf.compile("libbootutil.a"); |
| 96 | |
| 97 | walk_dir("../../boot").unwrap(); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 98 | walk_dir("../../ext/tinycrypt/lib/source").unwrap(); |
Fabio Utzig | d32fd64 | 2017-12-18 15:19:47 -0200 | [diff] [blame] | 99 | walk_dir("../../ext/mbedtls").unwrap(); |
David Brown | d2b1853 | 2017-07-12 09:51:31 -0600 | [diff] [blame] | 100 | walk_dir("csupport").unwrap(); |
David Brown | 82bf7c2 | 2017-07-12 09:49:31 -0600 | [diff] [blame] | 101 | walk_dir("mbedtls/include").unwrap(); |
| 102 | walk_dir("mbedtls/library").unwrap(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | // Output the names of all files within a directory so that Cargo knows when to rebuild. |
| 106 | fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> { |
| 107 | for ent in fs::read_dir(path.as_ref())? { |
| 108 | let ent = ent?; |
| 109 | let p = ent.path(); |
| 110 | if p.is_dir() { |
| 111 | walk_dir(p)?; |
| 112 | } else { |
| 113 | // Note that non-utf8 names will fail. |
| 114 | let name = p.to_str().unwrap(); |
| 115 | if name.ends_with(".c") || name.ends_with(".h") { |
| 116 | println!("cargo:rerun-if-changed={}", name); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | Ok(()) |
| 122 | } |