Skip to content

Commit

Permalink
Allow patching flash_discriminator with binary_configurator.py
Browse files Browse the repository at this point in the history
  • Loading branch information
CapnBry committed Feb 12, 2024
1 parent a8243b9 commit 87f65c4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 20 deletions.
10 changes: 6 additions & 4 deletions src/lib/CONFIG/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
}
Expand Down
23 changes: 11 additions & 12 deletions src/lib/OPTIONS/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/OPTIONS/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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];
Expand Down
2 changes: 1 addition & 1 deletion src/lib/WIFI/devWIFI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ static void HandleReset(AsyncWebServerRequest *request)

static void UpdateSettings(AsyncWebServerRequest *request, JsonVariant &json)
{
if (flash_discriminator != json["flash-discriminator"].as<uint32_t>()) {
if (firmwareOptions.flash_discriminator != json["flash-discriminator"].as<uint32_t>()) {
request->send(409, "text/plain", "Mismatched device identifier, refresh the page and try again.");
return;
}
Expand Down
7 changes: 6 additions & 1 deletion src/python/binary_configurator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 87f65c4

Please sign in to comment.