diff --git a/src/lib/CONFIG/config.cpp b/src/lib/CONFIG/config.cpp index dae375f544..669013f6d6 100644 --- a/src/lib/CONFIG/config.cpp +++ b/src/lib/CONFIG/config.cpp @@ -735,12 +735,14 @@ void RxConfig::CheckUpdateFlashedUid(bool skipDescrimCheck) if (!firmwareOptions.hasUID) return; // If already copied binding info, do not replace - if (!skipDescrimCheck && m_config.flash_discriminator == flash_discriminator) + if (!skipDescrimCheck && m_config.flash_discriminator == firmwareOptions.flash_discriminator) return; // Save the new UID along with this discriminator to prevent resetting every boot SetUID(firmwareOptions.uid); - m_config.flash_discriminator = flash_discriminator; + m_config.flash_discriminator = firmwareOptions.flash_discriminator; + // Reset the power on counter because this is following a flash, may have taken a few boots to flash + m_config.powerOnCounter = 0; // SetUID should set this but just in case that gets removed, flash_discriminator needs to be saved m_modified = true; @@ -897,7 +899,7 @@ void RxConfig::UpgradeUid(uint8_t *onLoanUid, uint8_t *boundUid) else if (firmwareOptions.hasUID) { memcpy(m_config.uid, firmwareOptions.uid, UID_LEN); - m_config.flash_discriminator = flash_discriminator; + m_config.flash_discriminator = firmwareOptions.flash_discriminator; } else if (boundUid) { @@ -1030,7 +1032,7 @@ RxConfig::SetDefaults(bool commit) if (commit) { // Prevent rebinding to the flashed UID on first boot - m_config.flash_discriminator = flash_discriminator; + m_config.flash_discriminator = firmwareOptions.flash_discriminator; m_modified = true; Commit(); } diff --git a/src/lib/OPTIONS/options.cpp b/src/lib/OPTIONS/options.cpp index 28ae74e12c..4962ce0f19 100644 --- a/src/lib/OPTIONS/options.cpp +++ b/src/lib/OPTIONS/options.cpp @@ -24,11 +24,10 @@ const char *wifi_ap_ssid = "ExpressLRS RX"; const char *wifi_ap_password = "expresslrs"; const char *wifi_ap_address = "10.0.0.1"; -// Discriminator value used to determine if the device has been reflashed and therefore -// the SPIFSS settings are obsolete and the flashed settings should be used in preference -uint32_t flash_discriminator; - #if !defined(TARGET_UNIFIED_TX) && !defined(TARGET_UNIFIED_RX) +#include "flashdiscrim.h" +// This is created by the build_flags.py and used by STM32 (ESP gets it from json) + const char device_name[] = DEVICE_NAME; const char *product_name = (const char *)(target_name+4); @@ -65,6 +64,11 @@ __attribute__ ((used)) static firmware_options_t flashedOptions = { .hasUID = false, .uid = {}, #endif +#if defined(FLASH_DISCRIM) + .flash_discriminator = FLASH_DISCRIM, +#else + .flash_discriminator = 0, +#endif #if defined(PLATFORM_ESP32) || defined(PLATFORM_ESP8266) #if defined(AUTO_WIFI_ON_INTERVAL) .wifi_auto_on_interval = AUTO_WIFI_ON_INTERVAL * 1000, @@ -166,11 +170,6 @@ firmware_options_t firmwareOptions; bool options_init() { firmwareOptions = flashedOptions; -#if defined(PLATFORM_STM32) - #include "flashdiscrim.h" - // This is created by the build_flags.py and used by STM32 (ESP gets it from json) - flash_discriminator = FLASH_DISCRIM; -#endif return true; } @@ -228,7 +227,7 @@ void saveOptions(Stream &stream, bool customised) doc["is-airport"] = firmwareOptions.is_airport; doc["domain"] = firmwareOptions.domain; doc["customised"] = customised; - doc["flash-discriminator"] = flash_discriminator; + doc["flash-discriminator"] = firmwareOptions.flash_discriminator; serializeJson(doc, stream); } @@ -339,7 +338,7 @@ static void options_LoadFromFlashOrFile(EspFlashStream &strmFlash) firmwareOptions.lock_on_first_connection = doc["lock-on-first-connection"] | true; #endif firmwareOptions.domain = doc["domain"] | 0; - flash_discriminator = doc["flash-discriminator"] | 0U; + firmwareOptions.flash_discriminator = doc["flash-discriminator"] | 0U; builtinOptions.clear(); saveOptions(builtinOptions, doc["customised"] | false); @@ -353,7 +352,7 @@ void options_SetTrueDefaults() DynamicJsonDocument doc(128); // The Regulatory Domain is retained, as there is no sensible default doc["domain"] = firmwareOptions.domain; - doc["flash-discriminator"] = flash_discriminator; + doc["flash-discriminator"] = firmwareOptions.flash_discriminator; File options = SPIFFS.open("/options.json", "w"); serializeJson(doc, options); diff --git a/src/lib/OPTIONS/options.h b/src/lib/OPTIONS/options.h index 64604d5ffe..bece7d4ba7 100644 --- a/src/lib/OPTIONS/options.h +++ b/src/lib/OPTIONS/options.h @@ -12,8 +12,6 @@ extern const char *wifi_ap_ssid; extern const char *wifi_ap_password; extern const char *wifi_ap_address; -extern uint32_t flash_discriminator; - enum BuzzerMode { buzzerQuiet, buzzerOne, @@ -26,6 +24,8 @@ typedef struct _options { uint8_t domain; // depends on radio chip uint8_t hasUID; uint8_t uid[6]; // MY_UID derived from MY_BINDING_PHRASE + uint32_t flash_discriminator; // Discriminator value used to determine if the device has been reflashed and therefore + // the SPIFSS settings are obsolete and the flashed settings should be used in preference #if defined(PLATFORM_ESP32) || defined(PLATFORM_ESP8266) int32_t wifi_auto_on_interval; char home_wifi_ssid[33]; diff --git a/src/lib/WIFI/devWIFI.cpp b/src/lib/WIFI/devWIFI.cpp index 3fd2ca743a..420afd9cc5 100644 --- a/src/lib/WIFI/devWIFI.cpp +++ b/src/lib/WIFI/devWIFI.cpp @@ -253,7 +253,7 @@ static void HandleReset(AsyncWebServerRequest *request) static void UpdateSettings(AsyncWebServerRequest *request, JsonVariant &json) { - if (flash_discriminator != json["flash-discriminator"].as()) { + if (firmwareOptions.flash_discriminator != json["flash-discriminator"].as()) { request->send(409, "text/plain", "Mismatched device identifier, refresh the page and try again."); return; } diff --git a/src/python/binary_configurator.py b/src/python/binary_configurator.py index bf25a21881..8459653c8b 100644 --- a/src/python/binary_configurator.py +++ b/src/python/binary_configurator.py @@ -89,6 +89,9 @@ def patch_uid(mm, pos, args): pos += 7 return pos +def patch_flash_discriminator(mm, pos, args): + return write32(mm, pos, args.flash_discriminator) + def patch_wifi(mm, pos, args): interval = None if args.no_auto_wifi: @@ -208,6 +211,7 @@ def patch_firmware(options, mm, pos, args): mm[pos] = domain_number(args.domain) pos += 1 pos = patch_uid(mm, pos, args) + pos = patch_flash_discriminator(mm, pos, args) if options.deviceType is DeviceType.TX: pos = patch_tx_params(mm, pos, args, options) elif options.deviceType is DeviceType.RX: @@ -325,6 +329,7 @@ def main(): parser.add_argument('--fdir', action=readable_dir, default=None, help='If specified, then the firmware files are loaded from this directory') # Bind phrase parser.add_argument('--phrase', type=str, help='Your personal binding phrase') + parser.add_argument('--flash-discriminator', type=int, default=randint(1,2**32-1), dest='flash_discriminator', help='Force a fixed flash-descriminator instead of random') # WiFi Params parser.add_argument('--ssid', type=length_check(32, "ssid"), required=False, help='Home network SSID') parser.add_argument('--password', type=length_check(64, "password"), required=False, help='Home network password') @@ -359,7 +364,7 @@ def main(): parser.add_argument("--force", action='store_true', default=False, help="Force upload even if target does not match") parser.add_argument("--confirm", action='store_true', default=False, help="Confirm upload if a mismatched target was previously uploaded") parser.add_argument("--tx", action='store_true', default=False, help="Flash a TX module, RX if not specified") - parser.add_argument("--lbt", action='store_true', default=False, help="Use LBT firmware, default is FCC (onl for 2.4GHz firmware)") + parser.add_argument("--lbt", action='store_true', default=False, help="Use LBT firmware, default is FCC (only for 2.4GHz firmware)") # Deprecated options, left for backward compatibility parser.add_argument('--uart-inverted', action=deprecate_action, nargs=0, help='Deprecated') parser.add_argument('--no-uart-inverted', action=deprecate_action, nargs=0, help='Deprecated')