From 926c7d64b19991e4f650c2f8237d8e550f5bc8d3 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Thu, 20 Apr 2023 09:32:32 +1000 Subject: [PATCH] AP_HAL_ChibiOS: add and use AP_BOOTLOADER_FLASHING_ENABLED --- libraries/AP_HAL_ChibiOS/Util.cpp | 11 +++++--- libraries/AP_HAL_ChibiOS/Util.h | 6 ++++- .../hwdef/f103-RangeFinder/hwdef.dat | 1 + .../AP_HAL_ChibiOS/hwdef/iomcu/hwdef.dat | 1 + .../hwdef/iomcu_f103_8MHz/hwdef.dat | 2 ++ .../hwdef/scripts/chibios_hwdef.py | 26 ++++++++++--------- 6 files changed, 31 insertions(+), 16 deletions(-) diff --git a/libraries/AP_HAL_ChibiOS/Util.cpp b/libraries/AP_HAL_ChibiOS/Util.cpp index 81c8c0d92c..43414073fc 100644 --- a/libraries/AP_HAL_ChibiOS/Util.cpp +++ b/libraries/AP_HAL_ChibiOS/Util.cpp @@ -253,9 +253,10 @@ uint64_t Util::get_hw_rtc() const return stm32_get_utc_usec(); } -#if !defined(HAL_NO_FLASH_SUPPORT) && !defined(HAL_NO_ROMFS_SUPPORT) - #include + +#if AP_BOOTLOADER_FLASHING_ENABLED + #if HAL_GCS_ENABLED #define Debug(fmt, args ...) do { gcs().send_text(MAV_SEVERITY_INFO, fmt, ## args); } while (0) #endif // HAL_GCS_ENABLED @@ -264,6 +265,10 @@ uint64_t Util::get_hw_rtc() const #define Debug(fmt, args ...) do { hal.console->printf(fmt, ## args); } while (0) #endif +#ifdef HAL_NO_FLASH_SUPPORT +#error "Bootloader-flashing enabled but no flashing support" +#endif + Util::FlashBootloader Util::flash_bootloader() { uint32_t fw_size; @@ -371,7 +376,7 @@ Util::FlashBootloader Util::flash_bootloader() AP_ROMFS::free(fw); return FlashBootloader::FAIL; } -#endif // !HAL_NO_FLASH_SUPPORT && !HAL_NO_ROMFS_SUPPORT +#endif // AP_BOOTLOADER_FLASHING_ENABLED /* display system identifer - board type and serial number diff --git a/libraries/AP_HAL_ChibiOS/Util.h b/libraries/AP_HAL_ChibiOS/Util.h index d84a13d214..74fd8db0f8 100644 --- a/libraries/AP_HAL_ChibiOS/Util.h +++ b/libraries/AP_HAL_ChibiOS/Util.h @@ -30,6 +30,10 @@ class ExpandingString; #define HAL_ENABLE_SAVE_PERSISTENT_PARAMS (defined(STM32F7) || defined(STM32H7)) #endif +#ifndef AP_BOOTLOADER_FLASHING_ENABLED +#define AP_BOOTLOADER_FLASHING_ENABLED 0 +#endif + class ChibiOS::Util : public AP_HAL::Util { public: static Util *from(AP_HAL::Util *util) { @@ -126,7 +130,7 @@ private: get system clock in UTC microseconds */ uint64_t get_hw_rtc() const override; -#if !defined(HAL_NO_FLASH_SUPPORT) && !defined(HAL_NO_ROMFS_SUPPORT) +#if AP_BOOTLOADER_FLASHING_ENABLED FlashBootloader flash_bootloader() override; #endif diff --git a/libraries/AP_HAL_ChibiOS/hwdef/f103-RangeFinder/hwdef.dat b/libraries/AP_HAL_ChibiOS/hwdef/f103-RangeFinder/hwdef.dat index bdd8f0d6e4..3c7af41081 100644 --- a/libraries/AP_HAL_ChibiOS/hwdef/f103-RangeFinder/hwdef.dat +++ b/libraries/AP_HAL_ChibiOS/hwdef/f103-RangeFinder/hwdef.dat @@ -12,6 +12,7 @@ define AP_PERIPH_RANGEFINDER_PORT_DEFAULT 3 # we're too low on flash with old compiler for bootloader define HAL_NO_ROMFS_SUPPORT +define AP_BOOTLOADER_FLASHING_ENABLED 0 # setup for MSP define HAL_MSP_ENABLED 1 diff --git a/libraries/AP_HAL_ChibiOS/hwdef/iomcu/hwdef.dat b/libraries/AP_HAL_ChibiOS/hwdef/iomcu/hwdef.dat index a958a4909a..a24eee83fa 100644 --- a/libraries/AP_HAL_ChibiOS/hwdef/iomcu/hwdef.dat +++ b/libraries/AP_HAL_ChibiOS/hwdef/iomcu/hwdef.dat @@ -137,5 +137,6 @@ IOMCU_FW 1 MAIN_STACK 0x200 PROCESS_STACK 0x250 define HAL_DISABLE_LOOP_DELAY +define AP_BOOTLOADER_FLASHING_ENABLED 0 AUTOBUILD_TARGETS iofirmware diff --git a/libraries/AP_HAL_ChibiOS/hwdef/iomcu_f103_8MHz/hwdef.dat b/libraries/AP_HAL_ChibiOS/hwdef/iomcu_f103_8MHz/hwdef.dat index 8c825d676d..d1a91ed683 100644 --- a/libraries/AP_HAL_ChibiOS/hwdef/iomcu_f103_8MHz/hwdef.dat +++ b/libraries/AP_HAL_ChibiOS/hwdef/iomcu_f103_8MHz/hwdef.dat @@ -138,4 +138,6 @@ MAIN_STACK 0x200 PROCESS_STACK 0x250 define HAL_DISABLE_LOOP_DELAY +define AP_BOOTLOADER_FLASHING_ENABLED 0 + AUTOBUILD_TARGETS iofirmware diff --git a/libraries/AP_HAL_ChibiOS/hwdef/scripts/chibios_hwdef.py b/libraries/AP_HAL_ChibiOS/hwdef/scripts/chibios_hwdef.py index 86e25a1bc3..0da5fa66bf 100644 --- a/libraries/AP_HAL_ChibiOS/hwdef/scripts/chibios_hwdef.py +++ b/libraries/AP_HAL_ChibiOS/hwdef/scripts/chibios_hwdef.py @@ -2381,21 +2381,23 @@ def bootloader_path(): "Tools", "bootloaders", bootloader_filename) - if os.path.exists(bootloader_path): - return os.path.realpath(bootloader_path) - - return None + return bootloader_path -def add_bootloader(): +def embed_bootloader(f): '''added bootloader to ROMFS''' - bp = bootloader_path() - if bp is not None and int(get_config('BOOTLOADER_EMBED', required=False, default='1')): - romfs["bootloader.bin"] = bp - env_vars['BOOTLOADER_EMBED'] = 1 - else: - env_vars['BOOTLOADER_EMBED'] = 0 + if not intdefines.get('AP_BOOTLOADER_FLASHING_ENABLED', 1): + # or, you know, not... + return + bp = bootloader_path() + if not os.path.exists(bp): + return + + bp = os.path.realpath(bp) + + romfs["bootloader.bin"] = bp + f.write("#define AP_BOOTLOADER_FLASHING_ENABLED 1\n") def write_ROMFS(outdir): @@ -2559,7 +2561,7 @@ def write_hwdef_header(outfilename): setup_apj_IDs() write_USB_config(f) - add_bootloader() + embed_bootloader(f) if len(romfs) > 0: f.write('#define HAL_HAVE_AP_ROMFS_EMBEDDED_H 1\n')