A Hardware Hacking Tool with Web-Based CLI That Speaks Every Protocol
C arduinobluetoothcan-busdebuggingeepromesp32flipperzerogpiohardware-hackingi2cjtagopenocdprotocolspwmradiorfidspisubghzuartwifi
add shell for spi eeprom
Geo7d9d2cb65208
diff --git a/src/Shells/SpiEepromShell.cpp b/src/Shells/SpiEepromShell.cppnew file mode 100644index 00000000..a9399699--- /dev/null+++ b/src/Shells/SpiEepromShell.cpp@@ -0,0 +1,183 @@+#include "SpiEepromShell.h"++SpiEepromShell::SpiEepromShell(+ SpiService& spiService,+ ITerminalView& view,+ IInput& input,+ ArgTransformer& argTransformer,+ UserInputManager& userInputManager+) :+ spiService(spiService),+ terminalView(view),+ terminalInput(input),+ argTransformer(argTransformer),+ userInputManager(userInputManager)+{+}++void SpiEepromShell::run() {+ // Select EEPROM model+ int selectedModelIndex = userInputManager.readValidatedChoiceIndex("\nSelect EEPROM type", models, 0);+ if (selectedModelIndex < 0) {+ terminalView.println("Invalid selection. Aborting.\n");+ return;+ }++ // Get params+ eepromSize = memoryLengths[selectedModelIndex];+ pageSize = pageLengths[selectedModelIndex];+ auto mosi = state.getSpiMOSIPin();+ auto miso = state.getSpiMISOPin();+ auto sclk = state.getSpiCLKPin();+ auto cs = state.getSpiCSPin();++ // Initialize EEPROM+ bool ok = spiService.initEeprom(mosi, miso, sclk, cs, pageSize, eepromSize);+ if (!ok) {+ terminalView.println("Failed to initialize EEPROM. Please check connections.");+ return;+ }++ while (true) {+ terminalView.println("\n=== SPI EEPROM Shell ===");++ // Select action+ int index = userInputManager.readValidatedChoiceIndex("Select EEPROM action", actions, 0);++ // Quit+ if (index == -1 || actions[index] == "πͺ Exit Shell") {+ terminalView.println("Exiting SPI EEPROM Shell...\n");+ break;+ }+ // Run selected action+ switch (index) {+ case 0: cmdProbe(); break;+ case 1: cmdRead(); break;+ case 2: cmdWrite(); break;+ case 3: cmdDump(); break;+ case 4: cmdErase(); break;+ default:+ terminalView.println("Unknown action.");+ break;+ }+ }+ spiService.closeEeprom();+}++void SpiEepromShell::cmdProbe() {+ terminalView.println("\n[NYI] π SPI EEPROM Probe...");+}++void SpiEepromShell::cmdRead() {+ terminalView.println("\nπ Read EEPROM");++ auto addrStr = userInputManager.readValidatedHexString("Start address (e.g., 00FF00) ", 0, true);+ uint32_t addr = argTransformer.parseHexOrDec32("0x" + addrStr);++ if (addr >= eepromSize) {+ terminalView.println("\n β Error: Start address is beyond EEPROM size.\n");+ return;+ }++ uint32_t count = userInputManager.readValidatedUint32("Number of bytes to read:", 16);+ if (addr + count > eepromSize) {+ count = eepromSize - addr;+ }++ terminalView.println("");+ const uint8_t bytesPerLine = 16;++ for (uint32_t i = 0; i < count; i += bytesPerLine) {+ uint8_t buffer[bytesPerLine];+ uint8_t lenToRead = std::min<uint32_t>(bytesPerLine, count - i);++ bool ok = spiService.readEepromBuffer(addr + i, buffer, lenToRead);+ if (!ok) {+ terminalView.println("\n β Read failed at 0x" + argTransformer.toHex(addr + i, 6));+ return;+ }++ std::vector<uint8_t> line(buffer, buffer + lenToRead);+ std::string formattedLine = argTransformer.toAsciiLine(addr + i, line);+ terminalView.println(formattedLine);+ }++ terminalView.println("");+}+++void SpiEepromShell::cmdWrite() {+ terminalView.println("\nβοΈ Write EEPROM");++ uint32_t addr = userInputManager.readValidatedUint32("Start address:", 0);++ if (userInputManager.readYesNo("Write an ASCII string?", true)) {+ terminalView.print("Enter ASCII string: ");+ std::string input = userInputManager.getLine();+ std::string decoded = argTransformer.decodeEscapes(input);+ bool ok = spiService.writeEepromBuffer(addr, (const uint8_t*)decoded.data(), decoded.size());+ terminalView.println(ok ? "\n β Write OK" : "\n β Write failed");+ } else {+ std::string hexStr = userInputManager.readValidatedHexString("Enter hex bytes (e.g., AA BB CC) ", 0, true);+ std::vector<uint8_t> data = argTransformer.parseHexList(hexStr);+ bool ok = spiService.writeEepromBuffer(addr, data.data(), data.size());+ terminalView.println(ok ? "\n β Write OK" : "\n β Write failed");+ }+}++void SpiEepromShell::cmdDump() {+ terminalView.println("\nποΈ EEPROM Dump: Reading entire memory...");++ const uint32_t totalSize = eepromSize;+ const uint32_t lineSize = 16;+ uint8_t buffer[lineSize];++ for (uint32_t addr = 0; addr < totalSize; addr += lineSize) {+ // Read line+ bool ok = spiService.readEepromBuffer(addr, buffer, lineSize);+ if (!ok) {+ terminalView.println("\n β Read failed at 0x" + argTransformer.toHex(addr, 6));+ return;+ }++ // Format and print line+ std::vector<uint8_t> line(buffer, buffer + lineSize);+ std::string formattedLine = argTransformer.toAsciiLine(addr, line);+ terminalView.println(formattedLine);++ // Quit+ char c = terminalInput.readChar();+ if (c == '\r' || c == '\n') {+ terminalView.println("\n β Dump cancelled by user.");+ return;+ }+ }++ terminalView.println("\n β EEPROM Dump Done.");+}++void SpiEepromShell::cmdErase() {+ terminalView.println("\nπ£ EEPROM Erase: Writing 0x00 to entire memory...");++ if (!userInputManager.readYesNo("Confirm erase?", false)) {+ terminalView.println("Erase cancelled.");+ return;+ }++ const uint32_t totalSize = eepromSize;+ const uint32_t blockSize = 64;+ uint8_t zeros[blockSize] = {0};++ for (uint32_t addr = 0; addr < totalSize; addr += blockSize) {+ bool ok = spiService.writeEepromBuffer(addr, zeros, blockSize);+ if (!ok) {+ terminalView.println("\n β Write failed at 0x" + argTransformer.toHex(addr, 6));+ return;+ }++ // Progression feedback+ if (addr % 512 == 0) terminalView.print(".");+ }++ terminalView.println("\n β EEPROM Erase Done.");+}diff --git a/src/Shells/SpiEepromShell.h b/src/Shells/SpiEepromShell.hnew file mode 100644index 00000000..3c88ae91--- /dev/null+++ b/src/Shells/SpiEepromShell.h@@ -0,0 +1,97 @@+#pragma once++#include "Services/SpiService.h"+#include "Interfaces/ITerminalView.h"+#include "Interfaces/IInput.h"+#include "Transformers/ArgTransformer.h"+#include "Managers/UserInputManager.h"+#include "States/GlobalState.h"++class SpiEepromShell {+public:+ SpiEepromShell(+ SpiService& spiService,+ ITerminalView& view,+ IInput& input,+ ArgTransformer& argTransformer,+ UserInputManager& userInputManager+ );++ void run();++private:+ SpiService& spiService;+ ITerminalView& terminalView;+ IInput& terminalInput;+ ArgTransformer& argTransformer;+ UserInputManager& userInputManager;+ GlobalState& state = GlobalState::getInstance();+ uint32_t eepromSize = 8192; // default+ uint16_t pageSize = 64; // default++ const std::vector<std::string> actions = {+ "π Probe EEPROM",+ "π Read bytes",+ "βοΈ Write bytes",+ "ποΈ Dump EEPROM",+ "π£ Erase EEPROM",+ "πͺ Exit Shell"+ };++ std::vector<std::string> models = {+ " 25X010 | 128 B | 8 B page | 1 B addr | blk: 0b | max 10 MHz",+ " 25X020 | 256 B | 8 B page | 1 B addr | blk: 0b | max 10 MHz",+ " 25X040 | 512 B | 8 B page | 1 B addr | blk: 1b | max 10 MHz",+ " 25X080 | 1 KB | 16 B page | 2 B addr | blk: 0b | max 10 MHz",+ " 25X160 | 2 KB | 16 B page | 2 B addr | blk: 0b | max 10 MHz",+ " 25X320 | 4 KB | 32 B page | 2 B addr | blk: 0b | max 10 MHz",+ " 25X640 | 8 KB | 32 B page | 2 B addr | blk: 0b | max 10 MHz",+ " 25X128 | 16 KB | 64 B page | 2 B addr | blk: 0b | max 10 MHz",+ " 25X256 | 32 KB | 64 B page | 2 B addr | blk: 0b | max 10 MHz",+ "25X512 | 64 KB | 128 B page | 2 B addr | blk: 0b | max 10 MHz",+ "25X1024| 128 KB | 256 B page | 3 B addr | blk: 0b | max 10 MHz",+ "25XM01 | 128 KB | 256 B page | 3 B addr | blk: 0b | max 10 MHz",+ "25XM02 | 256 KB | 256 B page | 3 B addr | blk: 0b | max 5 MHz",+ "25XM04 | 512 KB | 256 B page | 3 B addr | blk: 0b | max 8 MHz"+ };++ std::vector<uint32_t> memoryLengths = {+ 128, // 25X010+ 256, // 25X020+ 512, // 25X040+ 1024, // 25X080+ 2048, // 25X160+ 4096, // 25X320+ 8192, // 25X640+ 16384, // 25X128+ 32768, // 25X256+ 65536, // 25X512+ 131072, // 25X1024+ 131072, // 25XM01+ 262144, // 25XM02+ 524288 // 25XM04+ };++ std::vector<uint16_t> pageLengths = {+ 8, // 25X010+ 8, // 25X020+ 8, // 25X040+ 16, // 25X080+ 16, // 25X160+ 32, // 25X320+ 32, // 25X640+ 64, // 25X128+ 64, // 25X256+ 128, // 25X512+ 256, // 25X1024+ 256, // 25XM01+ 256, // 25XM02+ 256 // 25XM04+ };++ void cmdProbe();+ void cmdRead();+ void cmdWrite();+ void cmdDump();+ void cmdErase();+};