From bd0c7de2bf8b0d4de397c31bb390b184c33a45e8 Mon Sep 17 00:00:00 2001 From: Gabriel Ozouf Date: Wed, 31 Mar 2021 13:19:46 +0200 Subject: [PATCH] [ion] Create method to lock PCB versions on old boards --- apps/on_boarding/power_on_self_test.cpp | 5 +++++ ion/include/ion.h | 1 + ion/include/ion/board.h | 12 ++++++++++++ ion/src/device/n0100/drivers/board.cpp | 2 ++ ion/src/device/n0110/drivers/board.cpp | 7 ++++++- ion/src/device/shared/drivers/board.cpp | 23 +++++++++++++++++++++++ ion/src/device/shared/drivers/board.h | 6 ++++++ ion/src/shared/dummy/board.cpp | 4 ++++ ion/src/simulator/Makefile | 1 + 9 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 ion/include/ion/board.h create mode 100644 ion/src/shared/dummy/board.cpp diff --git a/apps/on_boarding/power_on_self_test.cpp b/apps/on_boarding/power_on_self_test.cpp index 5bdeb1807..85d4378cd 100644 --- a/apps/on_boarding/power_on_self_test.cpp +++ b/apps/on_boarding/power_on_self_test.cpp @@ -1,5 +1,6 @@ #include "power_on_self_test.h" #include +#include #include namespace OnBoarding { @@ -8,6 +9,10 @@ KDColor PowerOnSelfTest::Perform() { KDColor previousLEDColor = Ion::LED::getColor(); KDColor resultColor = KDColorWhite; + /* Lock OTP on older devices to prevent garbage being written where the PCB + * version is read. */ + Ion::Board::lockUnlockedPCBVersion(); + // Screen tests bool screenTestsOK = Shared::POSTAndHardwareTests::VBlankOK() && (Shared::POSTAndHardwareTests::TextLCDGlyphFailures() <= k_textErrorsLimit); // We push a white screen so that the LCD Data test is invisible for the user. diff --git a/ion/include/ion.h b/ion/include/ion.h index e23ba8268..3eeb87745 100644 --- a/ion/include/ion.h +++ b/ion/include/ion.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include diff --git a/ion/include/ion/board.h b/ion/include/ion/board.h new file mode 100644 index 000000000..6c7f09a42 --- /dev/null +++ b/ion/include/ion/board.h @@ -0,0 +1,12 @@ +#ifndef ION_BOARD_H +#define ION_BOARD_H + +namespace Ion { +namespace Board { + +void lockUnlockedPCBVersion(); + +} +} + +#endif diff --git a/ion/src/device/n0100/drivers/board.cpp b/ion/src/device/n0100/drivers/board.cpp index 5dfe99e0d..cc62fff89 100644 --- a/ion/src/device/n0100/drivers/board.cpp +++ b/ion/src/device/n0100/drivers/board.cpp @@ -242,6 +242,8 @@ void writePCBVersion(PCBVersion) {} void lockPCBVersion() {} +bool pcbVersionIsLocked() { return true; } + } } } diff --git a/ion/src/device/n0110/drivers/board.cpp b/ion/src/device/n0110/drivers/board.cpp index f2a1db2a2..3c11fc5b6 100644 --- a/ion/src/device/n0110/drivers/board.cpp +++ b/ion/src/device/n0110/drivers/board.cpp @@ -385,7 +385,8 @@ PCBVersion pcbVersion() { * way, flashing the PCB version can be done last. */ return PCB_LATEST; #else - return readPCBVersionInMemory(); + PCBVersion version = readPCBVersionInMemory(); + return (version == alternateBlankVersion ? 0 : version); #endif } @@ -405,6 +406,10 @@ void lockPCBVersion() { InternalFlash::WriteMemory(destination, &zero, sizeof(zero)); } +bool pcbVersionIsLocked() { + return *reinterpret_cast(InternalFlash::Config::OTPLocksAddress + pcbVersionOTPIndex) == 0; +} + } } } diff --git a/ion/src/device/shared/drivers/board.cpp b/ion/src/device/shared/drivers/board.cpp index 2407ec993..d0bab734a 100644 --- a/ion/src/device/shared/drivers/board.cpp +++ b/ion/src/device/shared/drivers/board.cpp @@ -93,3 +93,26 @@ void setClockFrequency(Frequency f) { } } } + +namespace Ion { +namespace Board { + +using namespace Device::Board; + +void lockUnlockedPCBVersion() { + if (pcbVersionIsLocked()) { + return; + } + /* PCB version is unlocked : the device is a N0110 that has been + * produced prior to the pcb revision. */ + PCBVersion version = pcbVersion(); + if (version != 0) { + /* Some garbage has been written in OTP0. We overwrite it fully, which is + * interepreted as blank. */ + writePCBVersion(alternateBlankVersion); + } + lockPCBVersion(); +} + +} +} diff --git a/ion/src/device/shared/drivers/board.h b/ion/src/device/shared/drivers/board.h index a94d8cd4f..8cf626a16 100644 --- a/ion/src/device/shared/drivers/board.h +++ b/ion/src/device/shared/drivers/board.h @@ -26,11 +26,17 @@ void setStandardFrequency(Frequency f); void setClockFrequency(Frequency f); typedef uint32_t PCBVersion; +/* On N0110 released before the PCB revision, OTP0 is supposed to be blank and + * unlocked. However, such a device with something written in OTP0 will be + * unable to configure Vbus properly. In this case, and if OTP0 is still + * unlocked, we fully write OTP0 and treat it the same as fully blank. */ +constexpr PCBVersion alternateBlankVersion = -1; PCBVersion pcbVersion(); PCBVersion readPCBVersionInMemory(); void writePCBVersion(PCBVersion version); void lockPCBVersion(); +bool pcbVersionIsLocked(); } } diff --git a/ion/src/shared/dummy/board.cpp b/ion/src/shared/dummy/board.cpp new file mode 100644 index 000000000..ad38e793a --- /dev/null +++ b/ion/src/shared/dummy/board.cpp @@ -0,0 +1,4 @@ +#include + +void Ion::Board::lockUnlockedPCBVersion() { +} diff --git a/ion/src/simulator/Makefile b/ion/src/simulator/Makefile index 862742bd7..61c90073b 100644 --- a/ion/src/simulator/Makefile +++ b/ion/src/simulator/Makefile @@ -1,5 +1,6 @@ ion_src += $(addprefix ion/src/shared/dummy/, \ backlight.cpp \ + board.cpp \ battery.cpp \ display.cpp \ exam_mode.cpp \