Skip to content

Commit

Permalink
FirmwareValidator: automatically validate previously validated image …
Browse files Browse the repository at this point in the history
…on revert
  • Loading branch information
ljahn committed Jan 12, 2025
1 parent 3e23ee7 commit cc497ac
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 2 deletions.
30 changes: 29 additions & 1 deletion src/components/firmwarevalidator/FirmwareValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,44 @@

using namespace Pinetime::Controllers;

FirmwareValidator::FirmwareValidator(Pinetime::Controllers::FS& fs) : fs {fs} {
}

bool FirmwareValidator::IsValidated() const {
auto* imageOkPtr = reinterpret_cast<uint32_t*>(validBitAdress);
return (*imageOkPtr) == validBitValue;
}

void FirmwareValidator::Validate() {
if (!IsValidated())
if (!IsValidated()) {
Pinetime::Drivers::InternalFlash::WriteWord(validBitAdress, validBitValue);

const char* thisVersion = Version::GitCommitHash();
lfs_file_t autovalidateFile;
if (fs.FileOpen(&autovalidateFile, "/autovalidator.bin", LFS_O_WRONLY | LFS_O_CREAT) != LFS_ERR_OK) {
return;
}
fs.FileWrite(&autovalidateFile, reinterpret_cast<const uint8_t*>(thisVersion), commitHashBufferLength);
fs.FileClose(&autovalidateFile);
}
}

void FirmwareValidator::Reset() {
NVIC_SystemReset();
}

// make sure to call this after fs.Init() was already called or the fs read will
// fail and nothing happens here.
void FirmwareValidator::AutoValidate() {
const std::string thisVersion = Version::GitCommitHash();
char validVersion[commitHashBufferLength];
lfs_file_t autovalidateFile;
if (fs.FileOpen(&autovalidateFile, "/autovalidator.bin", LFS_O_RDONLY) != LFS_ERR_OK) {
return;
}
fs.FileRead(&autovalidateFile, reinterpret_cast<uint8_t*>(validVersion), commitHashBufferLength);
fs.FileClose(&autovalidateFile);
if (thisVersion.compare(validVersion) == 0) {
Validate();
}
}
10 changes: 10 additions & 0 deletions src/components/firmwarevalidator/FirmwareValidator.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
#pragma once

#include <cstdint>
#include <string>

#include "components/fs/FS.h"
#include "Version.h"

namespace Pinetime {
namespace Controllers {
class FirmwareValidator {
public:
FirmwareValidator(Pinetime::Controllers::FS& fs);

void Validate();
void AutoValidate();
bool IsValidated() const;

void Reset();

private:
Pinetime::Controllers::FS& fs;

static constexpr uint32_t validBitAdress {0x7BFE8};
static constexpr uint32_t validBitValue {1};
static constexpr uint32_t commitHashBufferLength = (uint32_t) std::char_traits<char>::length(Version::GitCommitHash()) + 1;
};
}
}
2 changes: 2 additions & 0 deletions src/displayapp/DisplayApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
Pinetime::Controllers::NotificationManager& notificationManager,
Pinetime::Controllers::HeartRateController& heartRateController,
Controllers::Settings& settingsController,
Pinetime::Controllers::FirmwareValidator& validator,
Pinetime::Controllers::MotorController& motorController,
Pinetime::Controllers::MotionController& motionController,
Pinetime::Controllers::AlarmController& alarmController,
Expand All @@ -92,6 +93,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
notificationManager {notificationManager},
heartRateController {heartRateController},
settingsController {settingsController},
validator {validator},
motorController {motorController},
motionController {motionController},
alarmController {alarmController},
Expand Down
4 changes: 3 additions & 1 deletion src/displayapp/DisplayApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ namespace Pinetime {

namespace Controllers {
class Settings;
class FirmwareValidator;
class Battery;
class Ble;
class DateTime;
Expand Down Expand Up @@ -61,6 +62,7 @@ namespace Pinetime {
Pinetime::Controllers::NotificationManager& notificationManager,
Pinetime::Controllers::HeartRateController& heartRateController,
Controllers::Settings& settingsController,
Pinetime::Controllers::FirmwareValidator& validator,
Pinetime::Controllers::MotorController& motorController,
Pinetime::Controllers::MotionController& motionController,
Pinetime::Controllers::AlarmController& alarmController,
Expand Down Expand Up @@ -91,6 +93,7 @@ namespace Pinetime {
Pinetime::Controllers::NotificationManager& notificationManager;
Pinetime::Controllers::HeartRateController& heartRateController;
Pinetime::Controllers::Settings& settingsController;
Pinetime::Controllers::FirmwareValidator validator;
Pinetime::Controllers::MotorController& motorController;
Pinetime::Controllers::MotionController& motionController;
Pinetime::Controllers::AlarmController& alarmController;
Expand All @@ -99,7 +102,6 @@ namespace Pinetime {
Pinetime::Controllers::FS& filesystem;
Pinetime::Drivers::SpiNorFlash& spiNorFlash;

Pinetime::Controllers::FirmwareValidator validator;
Pinetime::Components::LittleVgl lvgl;
Pinetime::Controllers::Timer timer;

Expand Down
1 change: 1 addition & 0 deletions src/displayapp/DisplayAppRecovery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ DisplayApp::DisplayApp(Drivers::St7789& lcd,
Pinetime::Controllers::NotificationManager& /*notificationManager*/,
Pinetime::Controllers::HeartRateController& /*heartRateController*/,
Controllers::Settings& /*settingsController*/,
Pinetime::Controllers::FirmwareValidator& /*validator*/,
Pinetime::Controllers::MotorController& /*motorController*/,
Pinetime::Controllers::MotionController& /*motionController*/,
Pinetime::Controllers::AlarmController& /*alarmController*/,
Expand Down
2 changes: 2 additions & 0 deletions src/displayapp/DisplayAppRecovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace Pinetime {

namespace Controllers {
class Settings;
class FirmwareValidator;
class Battery;
class Ble;
class DateTime;
Expand Down Expand Up @@ -55,6 +56,7 @@ namespace Pinetime {
Pinetime::Controllers::NotificationManager& notificationManager,
Pinetime::Controllers::HeartRateController& heartRateController,
Controllers::Settings& settingsController,
Pinetime::Controllers::FirmwareValidator& validator,
Pinetime::Controllers::MotorController& motorController,
Pinetime::Controllers::MotionController& motionController,
Pinetime::Controllers::AlarmController& alarmController,
Expand Down
4 changes: 4 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "components/datetime/DateTimeController.h"
#include "components/heartrate/HeartRateController.h"
#include "components/fs/FS.h"
#include "components/firmwarevalidator/FirmwareValidator.h"
#include "drivers/Spi.h"
#include "drivers/SpiMaster.h"
#include "drivers/SpiNorFlash.h"
Expand Down Expand Up @@ -98,6 +99,7 @@ Pinetime::Applications::HeartRateTask heartRateApp(heartRateSensor, heartRateCon

Pinetime::Controllers::FS fs {spiNorFlash};
Pinetime::Controllers::Settings settingsController {fs};
Pinetime::Controllers::FirmwareValidator validator {fs};
Pinetime::Controllers::MotorController motorController {};

Pinetime::Controllers::DateTime dateTimeController {settingsController};
Expand All @@ -118,6 +120,7 @@ Pinetime::Applications::DisplayApp displayApp(lcd,
notificationManager,
heartRateController,
settingsController,
validator,
motorController,
motionController,
alarmController,
Expand All @@ -140,6 +143,7 @@ Pinetime::System::SystemTask systemTask(spi,
motionController,
motionSensor,
settingsController,
validator,
heartRateController,
displayApp,
heartRateApp,
Expand Down
3 changes: 3 additions & 0 deletions src/systemtask/SystemTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi,
Pinetime::Controllers::MotionController& motionController,
Pinetime::Drivers::Bma421& motionSensor,
Controllers::Settings& settingsController,
Pinetime::Controllers::FirmwareValidator& validator,
Pinetime::Controllers::HeartRateController& heartRateController,
Pinetime::Applications::DisplayApp& displayApp,
Pinetime::Applications::HeartRateTask& heartRateApp,
Expand All @@ -65,6 +66,7 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi,
heartRateSensor {heartRateSensor},
motionSensor {motionSensor},
settingsController {settingsController},
validator {validator},
heartRateController {heartRateController},
motionController {motionController},
displayApp {displayApp},
Expand Down Expand Up @@ -111,6 +113,7 @@ void SystemTask::Work() {
spiNorFlash.Wakeup();

fs.Init();
validator.AutoValidate();

nimbleController.Init();

Expand Down
3 changes: 3 additions & 0 deletions src/systemtask/SystemTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "components/ble/NotificationManager.h"
#include "components/alarm/AlarmController.h"
#include "components/fs/FS.h"
#include "components/firmwarevalidator/FirmwareValidator.h"
#include "touchhandler/TouchHandler.h"
#include "buttonhandler/ButtonHandler.h"
#include "buttonhandler/ButtonActions.h"
Expand Down Expand Up @@ -67,6 +68,7 @@ namespace Pinetime {
Pinetime::Controllers::MotionController& motionController,
Pinetime::Drivers::Bma421& motionSensor,
Controllers::Settings& settingsController,
Pinetime::Controllers::FirmwareValidator& validator,
Pinetime::Controllers::HeartRateController& heartRateController,
Pinetime::Applications::DisplayApp& displayApp,
Pinetime::Applications::HeartRateTask& heartRateApp,
Expand Down Expand Up @@ -107,6 +109,7 @@ namespace Pinetime {
Pinetime::Drivers::Hrs3300& heartRateSensor;
Pinetime::Drivers::Bma421& motionSensor;
Pinetime::Controllers::Settings& settingsController;
Pinetime::Controllers::FirmwareValidator& validator;
Pinetime::Controllers::HeartRateController& heartRateController;
Pinetime::Controllers::MotionController& motionController;

Expand Down

0 comments on commit cc497ac

Please sign in to comment.