From 1920965edf9556cffcf520f23c6f43e989935bda Mon Sep 17 00:00:00 2001 From: C Freeman Date: Tue, 22 Aug 2023 17:43:14 -0400 Subject: [PATCH] Set LastKnownGoodTime using UTC via python-generated header (#28668) * WIP: set LKGT using UTC via python * appease the linter * Changes from review * Remove header guard cruft --- build/chip/write_build_time_header.py | 62 +++++++++++++++++++ src/credentials/BUILD.gn | 24 ++++++- .../GenericConfigurationManagerImpl.ipp | 7 +++ src/platform/BUILD.gn | 1 + 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100755 build/chip/write_build_time_header.py diff --git a/build/chip/write_build_time_header.py b/build/chip/write_build_time_header.py new file mode 100755 index 00000000000000..7b421f19844c17 --- /dev/null +++ b/build/chip/write_build_time_header.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python +# Copyright (c) 2023 Project CHIP Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import argparse +import os +from datetime import datetime, timezone + + +def utc_time_in_matter_epoch_s(): + """ 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) + return int(utc_matter.total_seconds()) + + +class Options: + def __init__(self, output, define_name, define_val): + self.output = output + self.define_name = define_name + self.define_val = define_val + + +def GetOptions(): + 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.") + 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() + + return Options(output=output, + define_name=define_name, + define_val=str(build_time)) + + +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) diff --git a/src/credentials/BUILD.gn b/src/credentials/BUILD.gn index badfeaf6e4e4e7..bd4b96912cef90 100644 --- a/src/credentials/BUILD.gn +++ b/src/credentials/BUILD.gn @@ -16,11 +16,32 @@ import("//build_overrides/chip.gni") import("//build_overrides/nlassert.gni") import("${chip_root}/src/crypto/crypto.gni") import("${chip_root}/src/platform/device.gni") - declare_args() { chip_build_example_creds = true } +action("gen_build_time_header") { + script = "${chip_root}/build/chip/write_build_time_header.py" + + header_file = "FirmwareBuildTime.h" + include_dir = "${root_gen_dir}/include" + outputs = [ "${include_dir}/${header_file}" ] + + args = [ + "--output", + header_file, + "--gen-dir", + rebase_path(include_dir, root_build_dir), + ] + + visibility = [ ":build_time_header" ] +} + +source_set("build_time_header") { + sources = get_target_outputs(":gen_build_time_header") + deps = [ ":gen_build_time_header" ] +} + static_library("credentials") { output_name = "libCredentials" @@ -87,6 +108,7 @@ static_library("credentials") { cflags = [ "-Wconversion" ] public_deps = [ + ":build_time_header", "${chip_root}/src/crypto", "${chip_root}/src/lib/asn1", "${chip_root}/src/lib/core", diff --git a/src/include/platform/internal/GenericConfigurationManagerImpl.ipp b/src/include/platform/internal/GenericConfigurationManagerImpl.ipp index ca7d27ae47b332..4f77ae9f975162 100644 --- a/src/include/platform/internal/GenericConfigurationManagerImpl.ipp +++ b/src/include/platform/internal/GenericConfigurationManagerImpl.ipp @@ -26,6 +26,7 @@ #ifndef GENERIC_CONFIGURATION_MANAGER_IMPL_CPP #define GENERIC_CONFIGURATION_MANAGER_IMPL_CPP +#include #include #include #include @@ -290,6 +291,12 @@ CHIP_ERROR GenericConfigurationManagerImpl::GetFirmwareBuildChipEpo chipEpochTime = sFirmwareBuildChipEpochTime.Value(); return CHIP_NO_ERROR; } +#ifdef CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME_MATTER_EPOCH_S + { + chipEpochTime = chip::System::Clock::Seconds32(CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME_MATTER_EPOCH_S); + return CHIP_NO_ERROR; + } +#endif // Else, attempt to read the hard-coded values. VerifyOrReturnError(!BUILD_DATE_IS_BAD(CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_DATE), CHIP_ERROR_INTERNAL); VerifyOrReturnError(!BUILD_TIME_IS_BAD(CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME), CHIP_ERROR_INTERNAL); diff --git a/src/platform/BUILD.gn b/src/platform/BUILD.gn index 5f906139170b29..d0706b47ec91cd 100644 --- a/src/platform/BUILD.gn +++ b/src/platform/BUILD.gn @@ -452,6 +452,7 @@ if (chip_device_platform != "none") { ":platform_base", "${chip_root}/src/app:app_config", "${chip_root}/src/app/common:cluster-objects", + "${chip_root}/src/credentials:build_time_header", "${chip_root}/src/crypto", "${chip_root}/src/lib/support", ]