Skip to content

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

handle instructions for spi mode

Geo6939eb36685d
diff --git a/src/Controllers/SpiController.cpp b/src/Controllers/SpiController.cpp
index 455fda43..6cc28c7e 100644
--- a/src/Controllers/SpiController.cpp
+++ b/src/Controllers/SpiController.cpp
@@ -33,16 +33,7 @@ void SpiController::handleCommand(const TerminalCommand& cmd) {
}
else {
- terminalView.println("");
- terminalView.println("Unknown SPI command. Usage:");
- terminalView.println(" sniff");
- terminalView.println(" flash probe");
- terminalView.println(" flash read <addr> <len>");
- terminalView.println(" flash write");
- terminalView.println(" flash erase");
- terminalView.println(" config");
- terminalView.println(" [..]");
- terminalView.println("");
+ handleHelp();
}
}
@@ -50,7 +41,11 @@ void SpiController::handleCommand(const TerminalCommand& cmd) {
Entry point for instructions
*/
void SpiController::handleInstruction(const std::vector<ByteCode>& bytecodes) {
- terminalView.println("SPI Instruction [NYI]");
+ auto result = spiService.executeByteCode(bytecodes);
+ if (!result.empty()) {
+ terminalView.println("SPI Read:\n");
+ terminalView.println(result);
+ }
}
/*
@@ -318,7 +313,7 @@ void SpiController::handleFlashErase(const TerminalCommand& cmd) {
// Erase sectors and display progression
const uint32_t totalSectors = flashSize / sectorSize;
- terminalView.print("In progress ");
+ terminalView.print("In progress");
for (uint32_t i = 0; i < totalSectors; ++i) {
uint32_t addr = i * sectorSize;
spiService.eraseSector(addr, freq);
@@ -330,6 +325,22 @@ void SpiController::handleFlashErase(const TerminalCommand& cmd) {
terminalView.println("\r\nSPI Flash Erase: Complete.\n");
}
+/*
+Help
+*/
+void SpiController::handleHelp() {
+ terminalView.println("");
+ terminalView.println("Unknown SPI command. Usage:");
+ terminalView.println(" sniff");
+ terminalView.println(" flash probe");
+ terminalView.println(" flash read <addr> <len>");
+ terminalView.println(" flash write <addr> <data>");
+ terminalView.println(" flash erase");
+ terminalView.println(" config");
+ terminalView.println(" raw instructions, e.g: [0x9F r:3]");
+ terminalView.println("");
+}
+
/*
Config
*/
diff --git a/src/Controllers/SpiController.h b/src/Controllers/SpiController.h
index ead1df54..f52b7483 100644
--- a/src/Controllers/SpiController.h
+++ b/src/Controllers/SpiController.h
@@ -55,4 +55,7 @@ private:
// Configure SPI bus parameters
void handleConfig();
+ // Available commands
+ void handleHelp();
+
};
diff --git a/src/Controllers/UtilityController.cpp b/src/Controllers/UtilityController.cpp
index 11417eab..37ecb9a8 100644
--- a/src/Controllers/UtilityController.cpp
+++ b/src/Controllers/UtilityController.cpp
@@ -233,10 +233,10 @@ void UtilityController::handleHelp() {
terminalView.println(" sdcard - SD operations");
terminalView.println(" flash probe - Identify chip");
terminalView.println(" flash read <ad> <ln> - Read content");
- terminalView.println(" flash write - Write to flash");
+ terminalView.println(" flash write <ad> <d> - Write to flash");
terminalView.println(" flash erase - Erase flash");
terminalView.println(" config - Configure settings");
- terminalView.println(" [..] - Instruction syntax");
+ terminalView.println(" [0x9F r:3] - Instruction syntax");
terminalView.println("");
terminalView.println(" 7. 2WIRE:");
diff --git a/src/Services/SpiService.cpp b/src/Services/SpiService.cpp
index 59d59f67..d102a2a4 100644
--- a/src/Services/SpiService.cpp
+++ b/src/Services/SpiService.cpp
@@ -165,4 +165,60 @@ void SpiService::writeFlashPatch(uint32_t address, const std::vector<uint8_t>& d
std::vector<uint8_t> page(sectorData.begin() + i, sectorData.begin() + i + 256);
writeFlashPage(sectorStart + i, page, freq);
}
-}
\ No newline at end of file
+}
+
+std::string SpiService::executeByteCode(const std::vector<ByteCode>& bytecodes) {
+ std::string result;
+ bool inTransaction = false;
+
+ for (const auto& code : bytecodes) {
+ switch (code.getCommand()) {
+ case ByteCodeEnum::Start:
+ if (!inTransaction) {
+ beginTransaction();
+ inTransaction = true;
+ }
+ break;
+
+ case ByteCodeEnum::Stop:
+ if (inTransaction) {
+ endTransaction();
+ inTransaction = false;
+ }
+ break;
+
+ case ByteCodeEnum::Write:
+ for (uint32_t i = 0; i < code.getRepeat(); ++i) {
+ transfer(code.getData());
+ }
+ break;
+
+ case ByteCodeEnum::Read:
+ for (uint32_t i = 0; i < code.getRepeat(); ++i) {
+ uint8_t val = transfer(0x00); // dummy byte
+ char hex[5];
+ snprintf(hex, sizeof(hex), "%02X ", val);
+ result += hex;
+ }
+ break;
+
+ case ByteCodeEnum::DelayMs:
+ delay(code.getRepeat());
+ break;
+
+ case ByteCodeEnum::DelayUs:
+ delayMicroseconds(code.getRepeat());
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ // Close transaction if left open
+ if (inTransaction) {
+ endTransaction();
+ }
+
+ return result;
+}
diff --git a/src/Services/SpiService.h b/src/Services/SpiService.h
index 8b43c725..49e8155f 100644
--- a/src/Services/SpiService.h
+++ b/src/Services/SpiService.h
@@ -1,9 +1,10 @@
#pragma once
+#include <vector>
#include <Arduino.h>
#include <SPI.h>
#include <Data/FlashDatabase.h>
-#include <vector>
+#include <Models/ByteCode.h>
class SpiService {
public:
@@ -27,6 +28,8 @@ public:
// Write patch, we save a sector data, modify data, erase sector, write modified data to sector
void writeFlashPatch(uint32_t address, const std::vector<uint8_t>& data, uint32_t freq);
+
+ std::string executeByteCode(const std::vector<ByteCode>& bytecodes);
private:
uint8_t csPin;
uint32_t spiFrequency = 1000000;