From f8d931d944ec45f4ab6e9fd824d814a1cb2f8cfc Mon Sep 17 00:00:00 2001 From: cecille Date: Wed, 27 Sep 2023 18:42:31 -0400 Subject: [PATCH 1/4] Add build flag for changing firmware build time Adding a default fallback firmware build time. This makes the firmware build more consistent, so you get the same binary unless you specifically ask for the build time to be updated. --- build/chip/write_build_time_header.py | 13 ++++++++++--- src/credentials/BUILD.gn | 4 ++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/build/chip/write_build_time_header.py b/build/chip/write_build_time_header.py index 7b421f19844c17..86e9879614db68 100755 --- a/build/chip/write_build_time_header.py +++ b/build/chip/write_build_time_header.py @@ -18,10 +18,10 @@ from datetime import datetime, timezone -def utc_time_in_matter_epoch_s(): +def utc_time_in_matter_epoch_s(time: datetime): """ Returns the time in matter epoch in s. """ # Matter epoch is 0 hours, 0 minutes, 0 seconds on Jan 1, 2000 UTC - utc_matter = datetime.now(tz=timezone.utc) - datetime(2000, 1, 1, 0, 0, 0, 0, timezone.utc) + utc_matter = time - datetime(2000, 1, 1, 0, 0, 0, 0, timezone.utc) return int(utc_matter.total_seconds()) @@ -33,17 +33,24 @@ def __init__(self, output, define_name, define_val): def GetOptions(): + fallback_lkgt = datetime(2023, 10, 3, 0, 0, 0, 0, timezone.utc) + parser = argparse.ArgumentParser() parser.add_argument('--output', help="Output header name (inside gen dir)") parser.add_argument('--gen-dir', help="Path to root of generated file directory tree.") + parser.add_argument('--use-current-time', default=False, action='store_true', + help="Set the LKGT to the current time. If this flag is not used, the LKGT is set to a hardcoded time.") cmdline_options = parser.parse_args() # The actual output file is inside the gen dir. output = os.path.join(cmdline_options.gen_dir, cmdline_options.output) define_name = 'CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME_MATTER_EPOCH_S' - build_time = utc_time_in_matter_epoch_s() + if cmdline_options.use_current_time: + build_time = utc_time_in_matter_epoch_s(datetime.now(tz=timezone.utc)) + else: + build_time = utc_time_in_matter_epoch_s(fallback_lkgt) return Options(output=output, define_name=define_name, diff --git a/src/credentials/BUILD.gn b/src/credentials/BUILD.gn index 2abc6e2795fd38..eb2d9c4884d51b 100644 --- a/src/credentials/BUILD.gn +++ b/src/credentials/BUILD.gn @@ -20,6 +20,7 @@ import("${chip_root}/src/platform/device.gni") declare_args() { chip_build_example_creds = true + update_last_known_good_time = false } action("gen_build_time_header") { @@ -35,6 +36,9 @@ action("gen_build_time_header") { "--gen-dir", rebase_path(include_dir, root_build_dir), ] + if (update_last_known_good_time) { + args += [ "--use-current-time" ] + } visibility = [ ":build_time_header" ] } From 641e331a415705d1f96be280153264d7064aa6b8 Mon Sep 17 00:00:00 2001 From: cecille Date: Tue, 10 Oct 2023 14:20:43 -0400 Subject: [PATCH 2/4] Get fallback from from file so it can be updated This lets us update the fallback time via a script using a cron job if we want to. --- build/chip/fallback_lkgt | 1 + build/chip/write_build_time_header.py | 42 ++++++++++++++++++--------- 2 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 build/chip/fallback_lkgt diff --git a/build/chip/fallback_lkgt b/build/chip/fallback_lkgt new file mode 100644 index 00000000000000..736e04b6f10840 --- /dev/null +++ b/build/chip/fallback_lkgt @@ -0,0 +1 @@ +750275722 \ No newline at end of file diff --git a/build/chip/write_build_time_header.py b/build/chip/write_build_time_header.py index 86e9879614db68..605800c01b8cfc 100755 --- a/build/chip/write_build_time_header.py +++ b/build/chip/write_build_time_header.py @@ -18,6 +18,10 @@ from datetime import datetime, timezone +SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) +FALLBACK_LKGT_FILENAME = os.path.abspath(os.path.join(SCRIPT_DIR, 'fallback_lkgt')) + + def utc_time_in_matter_epoch_s(time: datetime): """ Returns the time in matter epoch in s. """ # Matter epoch is 0 hours, 0 minutes, 0 seconds on Jan 1, 2000 UTC @@ -32,17 +36,34 @@ def __init__(self, output, define_name, define_val): self.define_val = define_val -def GetOptions(): - fallback_lkgt = datetime(2023, 10, 3, 0, 0, 0, 0, timezone.utc) +def write_header(options): + with open(options.output, "w") as output_file: + output_file.write("// Generated by write_build_time_header.py\n") + output_file.write('#pragma once\n\n') + + output_file.write(f'#define {options.define_name} {options.define_val}\n') + + +def update_fallback_time_in_file(): + with open(FALLBACK_LKGT_FILENAME, "w") as output_file: + output_file.write(str(utc_time_in_matter_epoch_s(datetime.now(tz=timezone.utc)))) + +def main(): parser = argparse.ArgumentParser() parser.add_argument('--output', help="Output header name (inside gen dir)") parser.add_argument('--gen-dir', help="Path to root of generated file directory tree.") parser.add_argument('--use-current-time', default=False, action='store_true', help="Set the LKGT to the current time. If this flag is not used, the LKGT is set to a hardcoded time.") + parser.add_argument('--update-fallback-time-in-file', default=False, action='store_true', + help='Write the current UTC time out to the fallback file') cmdline_options = parser.parse_args() + if cmdline_options.update_fallback_time_in_file: + update_fallback_time_in_file() + return + # The actual output file is inside the gen dir. output = os.path.join(cmdline_options.gen_dir, cmdline_options.output) @@ -50,20 +71,13 @@ def GetOptions(): if cmdline_options.use_current_time: build_time = utc_time_in_matter_epoch_s(datetime.now(tz=timezone.utc)) else: - build_time = utc_time_in_matter_epoch_s(fallback_lkgt) + with open(FALLBACK_LKGT_FILENAME, "r") as input_file: + build_time = int(input_file.read()) - return Options(output=output, + opts = Options(output=output, define_name=define_name, define_val=str(build_time)) + write_header(opts) -def WriteHeader(options): - with open(options.output, "w") as output_file: - output_file.write("// Generated by write_build_time_header.py\n") - output_file.write('#pragma once\n\n') - - output_file.write(f'#define {options.define_name} {options.define_val}\n') - - -options = GetOptions() -WriteHeader(options) +main() From 523cca18837336d3dea86a45395a18eb0ffb5a0a Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Tue, 10 Oct 2023 18:23:54 +0000 Subject: [PATCH 3/4] Restyled by isort --- build/chip/write_build_time_header.py | 1 - 1 file changed, 1 deletion(-) diff --git a/build/chip/write_build_time_header.py b/build/chip/write_build_time_header.py index 605800c01b8cfc..3d395a8ade49b3 100755 --- a/build/chip/write_build_time_header.py +++ b/build/chip/write_build_time_header.py @@ -17,7 +17,6 @@ import os from datetime import datetime, timezone - SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) FALLBACK_LKGT_FILENAME = os.path.abspath(os.path.join(SCRIPT_DIR, 'fallback_lkgt')) From 877d93069472abbc70efbc50a5f28791e23e4cda Mon Sep 17 00:00:00 2001 From: cecille Date: Fri, 13 Oct 2023 21:21:02 -0400 Subject: [PATCH 4/4] Change filename --- build/chip/fallback_last_known_good_time | 1 + build/chip/fallback_lkgt | 1 - build/chip/write_build_time_header.py | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) create mode 100644 build/chip/fallback_last_known_good_time delete mode 100644 build/chip/fallback_lkgt diff --git a/build/chip/fallback_last_known_good_time b/build/chip/fallback_last_known_good_time new file mode 100644 index 00000000000000..2a9cae328a14db --- /dev/null +++ b/build/chip/fallback_last_known_good_time @@ -0,0 +1 @@ +750561408 \ No newline at end of file diff --git a/build/chip/fallback_lkgt b/build/chip/fallback_lkgt deleted file mode 100644 index 736e04b6f10840..00000000000000 --- a/build/chip/fallback_lkgt +++ /dev/null @@ -1 +0,0 @@ -750275722 \ No newline at end of file diff --git a/build/chip/write_build_time_header.py b/build/chip/write_build_time_header.py index 3d395a8ade49b3..fbd8f87ce84d4b 100755 --- a/build/chip/write_build_time_header.py +++ b/build/chip/write_build_time_header.py @@ -18,7 +18,7 @@ from datetime import datetime, timezone SCRIPT_DIR = os.path.abspath(os.path.dirname(__file__)) -FALLBACK_LKGT_FILENAME = os.path.abspath(os.path.join(SCRIPT_DIR, 'fallback_lkgt')) +FALLBACK_LKGT_FILENAME = os.path.abspath(os.path.join(SCRIPT_DIR, 'fallback_last_known_good_time')) def utc_time_in_matter_epoch_s(time: datetime):