Device validation

This commit is contained in:
Ben V. Brown
2022-06-21 20:52:05 +10:00
parent 51ad2f71c7
commit 85f30f2da6
5 changed files with 437 additions and 434 deletions

View File

@@ -79,6 +79,8 @@ uint64_t getDeviceID();
// If device has burned in validation code's, return the code
uint32_t getDeviceValidation();
// If device validation passes returns 0
uint8_t getDeviceValidationStatus();
// Status LED controls

View File

@@ -8,7 +8,7 @@
#include "TipThermoModel.h"
#include "USBPD.h"
#include "configuration.h"
#include "crc16.hpp"
#include "crc32.h"
#include "history.hpp"
#include "main.hpp"
@@ -328,32 +328,29 @@ uint64_t getDeviceID() {
return __builtin_bswap64(tmp);
}
uint16_t gethash() {
uint32_t userData = 0;
EF_Ctrl_Read_Sw_Usage(0, &userData);
userData &= 0xFFFF; // We only want the lower two bytes
// TODO FOR TESTING OVERRIDE
userData = 0xDEAD;
const uint16_t crcInitialVector = 0xCAFE;
uint8_t crcPayload[] = {userData & 0xFF, (userData >> 8) & 0xFF, 0, 0, 0, 0, 0, 0, 0, 0};
EF_Ctrl_Read_Chip_ID(crcPayload + 2);
auto crc32Table = CRC32Table<>();
uint32_t result = crcInitialVector;
for (int i = 0; i < 10; i++) {
result = crc16(result, crcPayload[i]);
uint32_t gethash() {
static uint32_t computedHash = 0;
if (computedHash != 0) {
return computedHash;
}
return result & 0xFFFF;
uint32_t deviceKey = EF_Ctrl_Get_Key_Slot_w0();
const uint32_t crcInitialVector = 0xCAFEF00D;
uint8_t crcPayload[] = {(uint8_t)(deviceKey), (uint8_t)(deviceKey >> 8), (uint8_t)(deviceKey >> 16), (uint8_t)(deviceKey >> 24), 0, 0, 0, 0, 0, 0, 0, 0};
EF_Ctrl_Read_Chip_ID(crcPayload + sizeof(deviceKey)); // Load device key into second half
computedHash = crc32Table.computeCRC32(crcInitialVector, crcPayload, sizeof(crcPayload));
return computedHash;
}
uint32_t getDeviceValidation() {
uint32_t userData = 0;
EF_Ctrl_Read_Sw_Usage(0, &userData);
// 4 byte user data burned in at factory
// TODO TESTING
// userData |= gethash() << 16;
return userData;
return EF_Ctrl_Get_Key_Slot_w1();
}
uint8_t getDeviceValidationStatus() {
return 1; // Device is OK
uint32_t programmedHash = EF_Ctrl_Get_Key_Slot_w1();
uint32_t computedHash = gethash();
return programmedHash == computedHash ? 0 : 1;
}

View File

@@ -83,6 +83,7 @@ struct TranslationIndexTable {
uint16_t SleepingAdvancedString;
uint16_t SleepingTipAdvancedString;
uint16_t OffString;
uint16_t DeviceFailedValidationWarning;
uint16_t SettingsResetMessage;
uint16_t NoAccelerometerMessage;

View File

@@ -897,9 +897,12 @@ static void showPDDebug(void) {
void showWarnings() {
// Display alert if settings were reset
if (settingsWereReset) {
warnUser(translatedString(Tr->SettingsResetMessage), 10 * TICKS_SECOND);
}
if (getDeviceValidationStatus()) {
// Warn user this device might be counterfeit
warnUser(translatedString(Tr->DeviceFailedValidationWarning), 10 * TICKS_SECOND);
}
#ifndef NO_WARN_MISSING
// We also want to alert if accel or pd is not detected / not responding
// In this case though, we dont want to nag the user _too_ much