mirror of
https://github.com/Ralim/IronOS.git
synced 2025-02-26 07:53:55 +00:00
Device validation
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -83,6 +83,7 @@ struct TranslationIndexTable {
|
||||
uint16_t SleepingAdvancedString;
|
||||
uint16_t SleepingTipAdvancedString;
|
||||
uint16_t OffString;
|
||||
uint16_t DeviceFailedValidationWarning;
|
||||
|
||||
uint16_t SettingsResetMessage;
|
||||
uint16_t NoAccelerometerMessage;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user