Skip to content

A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol

add 1wire eeproms methods for the service

Geo3310ebd66f80
diff --git a/src/Services/OneWireService.cpp b/src/Services/OneWireService.cpp
index 59bd8458..241efe15 100644
--- a/src/Services/OneWireService.cpp
+++ b/src/Services/OneWireService.cpp
@@ -11,6 +11,13 @@ void OneWireService::configure(uint8_t pin) {
oneWire = new OneWire(pin);
}
+void OneWireService::close() {
+ if (oneWire) {
+ delete oneWire;
+ oneWire = nullptr;
+ }
+}
+
bool OneWireService::reset() {
if (oneWire) return oneWire->reset();
}
@@ -146,3 +153,168 @@ bool OneWireService::search(uint8_t* rom) {
return oneWire->search(rom);
}
+// EEPROM, not supported on M5STICK
+
+#ifndef DEVICE_M5STICK
+
+void OneWireService::configureEeprom(uint8_t pin) {
+ if (owEeprom) {
+ delete owEeprom;
+ owEeprom = nullptr;
+ }
+
+ owEeprom = new OneWireNg_CurrentPlatform(pin, false); // parasitic=false
+}
+
+void OneWireService::closeEeprom() {
+ if (owEeprom) {
+ delete owEeprom;
+ owEeprom = nullptr;
+ }
+}
+
+bool OneWireService::eeprom2431Probe(uint8_t* outId) {
+ if (!owEeprom) return false;
+
+ const uint32_t timeoutMs = 500;
+ uint32_t start = millis();
+
+ owEeprom->searchReset();
+
+ OneWireNg::Id id;
+ while (millis() - start < timeoutMs) {
+ if (owEeprom->search(id) < OneWireNg::EC_SUCCESS) break;
+
+ uint8_t family = id[0];
+ if (family == DS2431_FAMILY || family == DS2433_FAMILY || family == DS28EC20_FAMILY) {
+ memcpy(eepromId, id, sizeof(OneWireNg::Id));
+ if (outId) memcpy(outId, id, sizeof(OneWireNg::Id));
+ return true;
+ }
+ }
+
+ memset(eepromId, 0, sizeof(OneWireNg::Id));
+ return false;
+}
+
+uint8_t OneWireService::eeprom2431ReadByte(uint16_t address) {
+ if (!owEeprom) return 0xFF;
+
+ uint8_t cmd[3] = {
+ 0xF0,
+ static_cast<uint8_t>(address & 0xFF),
+ static_cast<uint8_t>((address >> 8) & 0xFF)
+ };
+ uint8_t value = 0xFF;
+
+ owEeprom->addressSingle(eepromId);
+ owEeprom->touchBytes(cmd, 3);
+ owEeprom->touchBytes(&value, 1);
+
+ return value;
+}
+
+bool OneWireService::eeprom2431WriteRow(uint8_t rowAddr, const uint8_t* rowData, bool checkDataIntegrity) {
+ if (!owEeprom || rowAddr > 15) return false;
+
+ constexpr uint8_t ROW_SIZE = 8;
+ constexpr uint8_t CMD_WRITE_SCRATCHPAD = 0x0F;
+ constexpr uint8_t CMD_READ_SCRATCHPAD = 0xAA;
+ constexpr uint8_t CMD_COPY_SCRATCHPAD = 0x55;
+
+ uint8_t ta1 = rowAddr * ROW_SIZE;
+ uint8_t ta2 = 0x00;
+
+ // WRITE SCRATCHPAD
+ uint8_t cmd1[3 + ROW_SIZE] = { CMD_WRITE_SCRATCHPAD, ta1, ta2 };
+ memcpy(cmd1 + 3, rowData, ROW_SIZE);
+
+ owEeprom->addressSingle(eepromId);
+ owEeprom->touchBytes(cmd1, sizeof(cmd1));
+
+ uint8_t crc1[2];
+ owEeprom->readBytes(crc1, 2);
+ if (owEeprom->checkInvCrc16(cmd1, sizeof(cmd1), owEeprom->getLSB_u16(crc1)) != OneWireNg::EC_SUCCESS) {
+ return false;
+ }
+
+ // READ SCRATCHPAD
+ uint8_t rsp[1 + 2 + 1 + ROW_SIZE + 2]; // cmd + TA1/TA2 + ES + data + CRC
+ rsp[0] = CMD_READ_SCRATCHPAD;
+ memset(rsp + 1, 0xFF, sizeof(rsp) - 1);
+
+ owEeprom->resume();
+ owEeprom->touchBytes(rsp, sizeof(rsp));
+
+ uint8_t es = rsp[3];
+ uint8_t* crc2 = rsp + sizeof(rsp) - 2;
+
+ if (owEeprom->checkInvCrc16(rsp, sizeof(rsp) - 2, owEeprom->getLSB_u16(crc2)) != OneWireNg::EC_SUCCESS) {
+ return false;
+ }
+
+ if (rsp[1] != ta1 || rsp[2] != ta2 || (es & 0x20) || (es & 0x80)) {
+ return false;
+ }
+
+ if (checkDataIntegrity && memcmp(rsp + 4, rowData, ROW_SIZE) != 0) {
+ return false;
+ }
+
+ // COPY SCRATCHPAD
+ uint8_t cmd3[4] = { CMD_COPY_SCRATCHPAD, ta1, ta2, es };
+ owEeprom->resume();
+ owEeprom->touchBytes(cmd3, sizeof(cmd3));
+
+ delay(15); // wait for EEPROM commit
+
+ return true;
+}
+
+std::vector<uint8_t> OneWireService::eeprom2431Dump(uint16_t startAddress, uint16_t length) {
+ std::vector<uint8_t> data(length, 0xFF);
+ if (!owEeprom) return data;
+
+ uint8_t cmd[3] = {
+ 0xF0,
+ static_cast<uint8_t>(startAddress & 0xFF),
+ static_cast<uint8_t>((startAddress >> 8) & 0xFF)
+ };
+
+ owEeprom->addressSingle(eepromId);
+ owEeprom->touchBytes(cmd, 3);
+ owEeprom->touchBytes(data.data(), length);
+
+ return data;
+}
+
+bool OneWireService::getEepromModelInfos(uint8_t* romId, std::string& model, uint16_t& size, uint8_t& pageSize) {
+ if (!romId) return false;
+
+ uint8_t familyCode = romId[0];
+ model = "Unknown";
+ size = 128; // fallback default
+ pageSize = 8; // fallback default
+
+ switch (familyCode) {
+ case DS2431_FAMILY:
+ model = "DS2431 (1 Kbit)";
+ size = 128;
+ pageSize = 8;
+ return true;
+ case DS2433_FAMILY:
+ model = "DS2433 (4 Kbit)";
+ size = 512;
+ pageSize = 32;
+ return true;
+ case DS28EC20_FAMILY:
+ model = "DS28EC20 (20 Kbit)";
+ size = 2560;
+ pageSize = 32;
+ return true;
+ default:
+ return false;
+ }
+}
+
+#endif
\ No newline at end of file
diff --git a/src/Services/OneWireService.h b/src/Services/OneWireService.h
index d722f89f..8cf3d759 100644
--- a/src/Services/OneWireService.h
+++ b/src/Services/OneWireService.h
@@ -4,25 +4,67 @@
#include <vector>
#include "Models/ByteCode.h"
+#define DS2431_FAMILY 0x2D
+#define DS2433_FAMILY 0x23
+#define DS28EC20_FAMILY 0x43
+
class OneWireService {
public:
OneWireService();
void configure(uint8_t pin);
+ void close();
bool reset();
void write(uint8_t data);
void writeBytes(const uint8_t* data, uint8_t len);
- void writeRw1990(uint8_t pin, uint8_t* data, size_t len);
uint8_t read();
void readBytes(uint8_t* buffer, uint8_t length);
void skip();
void select(const uint8_t rom[8]);
uint8_t crc8(const uint8_t* data, uint8_t len);
- std::string executeByteCode(const std::vector<ByteCode>& bytecodes);
void resetSearch();
bool search(uint8_t* rom);
+ std::string executeByteCode(const std::vector<ByteCode>& bytecodes);
+
+ // RW1990
+ void writeRw1990(uint8_t pin, uint8_t* data, size_t len);
+
+ #ifndef DEVICE_M5STICK
+
+ #include "OneWireNg_CurrentPlatform.h"
+
+ // EEPROM
+ void configureEeprom(uint8_t pin);
+ void closeEeprom();
+ bool getEepromModelInfos(uint8_t* romId, std::string& model, uint16_t& size, uint8_t& pageSize);
+
+ // EEPROM DS24/28
+ bool eeprom2431Probe(uint8_t* outId = nullptr);
+ uint8_t eeprom2431ReadByte(uint16_t address);
+ std::vector<uint8_t> eeprom2431Dump(uint16_t startAddress, uint16_t length);
+ bool eeprom2431WriteRow(uint8_t rowAddr, const uint8_t* rowData, bool checkDataIntegrity = true);
+
+ #else
+
+ // Dummy implementations, no space left on the M5STICK for the EEPROM lib
+ inline void configureEeprom(uint8_t) {}
+ inline void closeEeprom() {}
+ inline bool getEepromModelInfos(uint8_t*, std::string&, uint16_t&, uint8_t&) { return false; }
+ inline bool eeprom2431Probe(uint8_t* = nullptr) { return false; }
+ inline uint8_t eeprom2431ReadByte(uint16_t) { return 0xFF; }
+ inline std::vector<uint8_t> eeprom2431Dump(uint16_t, uint16_t len) { return std::vector<uint8_t>(len, 0xFF); }
+ inline bool eeprom2431WriteRow(uint8_t, const uint8_t*, bool = true) { return false; }
+
+ #endif
private:
OneWire* oneWire = nullptr;
uint8_t oneWirePin = 0;
+
+ #ifndef DEVICE_M5STICK
+
+ OneWireNg* owEeprom = nullptr;
+ OneWireNg::Id eepromId = {};
+
+ #endif
};