Skip to content

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

add xmodem support for uart

Geo180664a29b7a
diff --git a/platformio.ini b/platformio.ini
index c95cdf3f..5021ddc1 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -20,6 +20,7 @@ lib_deps =
paulstoffregen/OneWire@^2.3.8
esp32ping
hideakitai/ESP32SPISlave@^0.6.8
+ gilman88/XModem@^1.0.3
build_flags =
-D CONFIG_TINYUSB_ENABLED=1
-D CONFIG_TINYUSB_CDC_ENABLED=1
@@ -84,6 +85,7 @@ lib_deps =
paulstoffregen/OneWire@^2.3.8
esp32ping
hideakitai/ESP32SPISlave@^0.6.8
+ gilman88/XModem@^1.0.3
build_flags =
-DDEVICE_M5STICK
diff --git a/src/Controllers/UartController.cpp b/src/Controllers/UartController.cpp
index 1714deb6..97c75032 100644
--- a/src/Controllers/UartController.cpp
+++ b/src/Controllers/UartController.cpp
@@ -4,8 +4,8 @@
Constructor
*/
UartController::UartController(ITerminalView& terminalView, IInput& terminalInput, IInput& deviceInput,
- UartService& uartService, HdUartService& hDUartService, ArgTransformer& argTransformer, UserInputManager& userInputManager)
- : terminalView(terminalView), terminalInput(terminalInput), deviceInput(deviceInput), uartService(uartService), hdUartService(hdUartService), argTransformer(argTransformer), userInputManager(userInputManager) {}
+ UartService& uartService, SdService& sdService, HdUartService& hDUartService, ArgTransformer& argTransformer, UserInputManager& userInputManager)
+ : terminalView(terminalView), terminalInput(terminalInput), deviceInput(deviceInput), uartService(uartService), sdService(sdService), hdUartService(hdUartService), argTransformer(argTransformer), userInputManager(userInputManager) {}
/*
@@ -41,6 +41,10 @@ void UartController::handleCommand(const TerminalCommand& cmd) {
handleGlitch();
}
+ else if (cmd.getRoot() == "xmodem") {
+ handleXmodem(cmd);
+ }
+
else if (cmd.getRoot() == "config") {
handleConfig();
}
@@ -348,6 +352,131 @@ void UartController::handleSpam(const TerminalCommand& cmd) {
}
}
+/*
+Xmodem
+*/
+void UartController::handleXmodem(const TerminalCommand& cmd) {
+ std::string action = cmd.getSubcommand();
+ std::string path = cmd.getArgs();
+
+ if (action.empty()) {
+ terminalView.println("Usage: xmodem <recv/send> <path>");
+ return;
+ }
+
+ if (path.empty()) {
+ terminalView.println("Error: missing path argument (ex: /file.txt)");
+ return;
+ }
+
+ // Normalize path
+ if (!path.empty() && path[0] != '/') {
+ path = "/" + path;
+ }
+
+ terminalView.println("\nXmodem Configuration:");
+
+ // Xmodem block size
+ uint8_t defaultBlockSize = uartService.getXmodemBlockSize();
+ uint8_t blockSize = userInputManager.readValidatedUint8("Block size (typ. 128 or 1024)", defaultBlockSize, 1, 128);
+ uartService.setXmodemBlockSize(blockSize);
+
+ // Xmodem id size
+ uint8_t defaultIdSize = uartService.getXmodemIdSize();
+ uint8_t idSize = userInputManager.readValidatedUint8("Block ID size in bytes (1-4)", defaultIdSize, 1, 4);
+ uartService.setXmodemIdSize(idSize);
+
+ // Xmodem CRC
+ bool useCrc = userInputManager.readYesNo("Use CRC?", true);
+ uartService.setXmodemCrc(useCrc);
+
+ terminalView.println("\nXmodem configured\n");
+
+ if (action == "recv") {
+ handleXmodemReceive(path);
+ } else if (action == "send") {
+ handleXmodemSend(path);
+ } else {
+ terminalView.println("Usage: xmodem <recv/send> <path>");
+ }
+}
+
+void UartController::handleXmodemSend(const std::string& path) {
+ // Open SD with SPI pin
+ auto sdMounted = sdService.configure(state.getSpiCLKPin(), state.getSpiMISOPin(),
+ state.getSpiMOSIPin(), state.getSpiCSPin());
+
+ //Check SD mounted
+ if (!sdMounted) {
+ terminalView.println("UART XMODEM: No SD card detected. Check SPI pins");
+ return;
+ }
+
+ // Open the file
+ File file = sdService.openFileRead(path);
+ if (!file) {
+ terminalView.println("UART XMODEM: Could not open file");
+ return;
+ }
+
+ // Infos
+ terminalView.println(" [INFO] No progress bar will be shown.");
+ terminalView.println(" Please be patient during the transfer.\n");
+ std::stringstream ss;
+ ss << " Estimated duration: ~"
+ << (uint32_t)((file.size() * 10.0) / state.getUartBaudRate())
+ << " seconds.\n";
+ terminalView.println(ss.str());
+
+ // Send it
+ terminalView.println("UART XMODEM: Sending...");
+ bool ok = uartService.xmodemSendFile(file);
+ file.close();
+ sdService.close();
+
+ // result
+ terminalView.println(ok ? "UART XMODEM: Success, file is transferred" : "UART XMODEM: Failed to transfer file");
+}
+
+void UartController::handleXmodemReceive(const std::string& path) {
+ // Open sd card with SPI pin
+ auto sdMounted = sdService.configure(state.getSpiCLKPin(), state.getSpiMISOPin(),
+ state.getSpiMOSIPin(), state.getSpiCSPin());
+
+ //Check SD mounted
+ if (!sdMounted) {
+ terminalView.println("UART XMODEM: No SD card detected. Check SPI pins");
+ return;
+ }
+
+ // Create target file
+ File file = sdService.openFileWrite(path);
+ if (!file) {
+ terminalView.println("UART XMODEM: Could not create file.");
+ return;
+ }
+
+ // Infos
+ terminalView.println("");
+ terminalView.println(" [INFO] XMODEM receive mode is blocking.");
+ terminalView.println(" No progress bar will be shown.");
+ terminalView.println(" The device will wait for incoming data");
+ terminalView.println(" for up to 2 minutes. Once started,");
+ terminalView.println(" the transfer must complete before exiting.\n");
+
+ // Receive
+ terminalView.println("UART XMODEM: Receiving...");
+ bool ok = uartService.xmodemReceiveToFile(file);
+ file.close();
+ sdService.close();
+
+ // Result
+ terminalView.println(ok ?
+ ("UART XMODEM: Receive OK, File saved to " + path) :
+ "UART XMODEM: Receive failed");
+ terminalView.println("");
+}
+
/*
Config
*/
@@ -400,6 +529,8 @@ void UartController::handleHelp() {
terminalView.println(" bridge");
terminalView.println(" spam <text> <ms>");
terminalView.println(" glitch");
+ terminalView.println(" xmodem recv <dest path>");
+ terminalView.println(" xmodem send <file path>");
terminalView.println(" config");
terminalView.println(" raw instructions, ['AT' D:100 r:128]");
terminalView.println("");
diff --git a/src/Controllers/UartController.h b/src/Controllers/UartController.h
index f412f021..a02d3820 100644
--- a/src/Controllers/UartController.h
+++ b/src/Controllers/UartController.h
@@ -7,6 +7,7 @@
#include "Models/ByteCode.h"
#include "Services/UartService.h"
#include "Services/HdUartService.h"
+#include "Services/SdService.h"
#include "Interfaces/ITerminalView.h"
#include "Interfaces/IInput.h"
#include "States/GlobalState.h"
@@ -20,11 +21,12 @@ public:
IInput& terminalInput,
IInput& deviceInput,
UartService& uartService,
+ SdService& sdService,
HdUartService& hdUartService,
ArgTransformer& argTransformer,
UserInputManager& userInputManager);
- // Entry pooint for UART command
+ // Entry point for UART command
void handleCommand(const TerminalCommand& cmd);
// Entry point for handle parsed bytecode instructions
@@ -40,7 +42,7 @@ public:
// Perform a simple read
void handleRead();
- // Send simple ping command
+ // Send probes to get a response
void handlePing();
// Write data to UART
@@ -61,6 +63,15 @@ public:
// Spam given input every given ms
void handleSpam(const TerminalCommand& cmd);
+ // Xmodem cmd handler
+ void handleXmodem(const TerminalCommand& cmd);
+
+ // Receive file from xmodem
+ void handleXmodemReceive(const std::string& path);
+
+ // Send file to xmodem
+ void handleXmodemSend(const std::string& path);
+
// Scan a baudrate
bool scanAtBaudrate(int baud);
@@ -83,6 +94,7 @@ public:
IInput& terminalInput;
IInput& deviceInput;
UartService& uartService;
+ SdService& sdService;
HdUartService& hdUartService;
ArgTransformer& argTransformer;
UserInputManager& userInputManager;
diff --git a/src/Controllers/UtilityController.cpp b/src/Controllers/UtilityController.cpp
index cf2c6a20..fda93b15 100644
--- a/src/Controllers/UtilityController.cpp
+++ b/src/Controllers/UtilityController.cpp
@@ -265,6 +265,8 @@ void UtilityController::handleHelp() {
terminalView.println(" bridge - Full-duplex mode");
terminalView.println(" spam <text> <ms> - Write text envery ms");
terminalView.println(" glitch - Timing attack");
+ terminalView.println(" xmodem <send> <path> - Send file via XMODEM");
+ terminalView.println(" xmodem <recv> <path> - Receive file via XMODEM");
terminalView.println(" config - Configure settings");
terminalView.println(" ['Hello'] [r:64]... - Instruction syntax");
diff --git a/src/Providers/DependencyProvider.cpp b/src/Providers/DependencyProvider.cpp
index 0bc34d34..37b205f6 100644
--- a/src/Providers/DependencyProvider.cpp
+++ b/src/Providers/DependencyProvider.cpp
@@ -30,7 +30,7 @@ DependencyProvider::DependencyProvider(ITerminalView& terminalView, IDeviceView&
userInputManager(terminalView, terminalInput, argTransformer),
horizontalSelector(deviceView, deviceInput),
terminalTypeConfigurator(horizontalSelector),
- uartController(terminalView, terminalInput, deviceInput, uartService, hdUartService, argTransformer, userInputManager),
+ uartController(terminalView, terminalInput, deviceInput, uartService, sdService, hdUartService, argTransformer, userInputManager),
i2cController(terminalView, terminalInput, i2cService, argTransformer, userInputManager),
oneWireController(terminalView, terminalInput, oneWireService, argTransformer, userInputManager),
infraredController(terminalView, terminalInput, infraredService, argTransformer, userInputManager),
diff --git a/src/Services/SdService.cpp b/src/Services/SdService.cpp
index bce73e64..1a48d477 100644
--- a/src/Services/SdService.cpp
+++ b/src/Services/SdService.cpp
@@ -224,4 +224,14 @@ bool SdService::ensureDirectory(const std::string& directory) {
return SD.mkdir(directory.c_str()); // Create forlder
}
return true; // Folder already exists
-}
\ No newline at end of file
+}
+
+File SdService::openFileRead(const std::string& path) {
+ if (!sdCardMounted) return File();
+ return SD.open(path.c_str(), FILE_READ);
+}
+
+File SdService::openFileWrite(const std::string& path) {
+ if (!sdCardMounted) return File();
+ return SD.open(path.c_str(), FILE_WRITE);
+}
diff --git a/src/Services/SdService.h b/src/Services/SdService.h
index 6a813eb2..31d758fe 100644
--- a/src/Services/SdService.h
+++ b/src/Services/SdService.h
@@ -29,7 +29,6 @@ public:
bool writeBinaryFile(const std::string& filePath, const std::vector<uint8_t>& data);
bool appendToFile(const std::string& filePath, const std::string& data);
bool deleteFile(const std::string& filePath);
- bool validateVaultFile(const std::string& filePath);
bool ensureDirectory(const std::string& directory);
std::string getFileExt(const std::string& path);
@@ -38,6 +37,10 @@ public:
std::vector<std::string> getCachedDirectoryElements(const std::string& path);
void setCachedDirectoryElements(const std::string& path, const std::vector<std::string>& elements);
void removeCachedPath(const std::string& path);
+
+ File openFileRead(const std::string& path);
+ File openFileWrite(const std::string& path);
+
};
#endif // SD_SERVICE_H
diff --git a/src/Services/UartService.cpp b/src/Services/UartService.cpp
index 7499834b..6e8d2a7b 100644
--- a/src/Services/UartService.cpp
+++ b/src/Services/UartService.cpp
@@ -144,3 +144,140 @@ uint32_t UartService::buildUartConfig(uint8_t dataBits, char parity, uint8_t sto
return config;
}
+
+/*
+XMODEM
+*/
+File* UartService::currentFile = nullptr;
+
+void UartService::setXmodemBlockSize(int32_t size) {
+ xmodemBlockSize = size;
+}
+
+void UartService::setXmodemIdSize(int8_t size) {
+ xmodemIdSize = size;
+}
+
+int32_t UartService::getXmodemBlockSize() const {
+ return xmodemBlockSize;
+}
+
+int8_t UartService::getXmodemIdSize() const {
+ return xmodemIdSize;
+}
+
+void UartService::setXmodemCrc(bool enabled) {
+ xmodemProtocol = enabled ? XModem::ProtocolType::CRC_XMODEM : XModem::ProtocolType::XMODEM;
+}
+
+void UartService::setXmodemReceiveHandler(bool (*handler)(void*, size_t, byte*, size_t)) {
+ xmodem.setRecieveBlockHandler(handler);
+}
+
+void UartService::setXmodemSendHandler(void (*handler)(void*, size_t, byte*, size_t)) {
+ xmodem.setBlockLookupHandler(handler);
+}
+
+void UartService::initXmodem() {
+ xmodem.begin(Serial1, xmodemProtocol);
+ xmodem.setDataSize(xmodemBlockSize);
+ xmodem.setIdSize(xmodemIdSize);
+}
+
+void UartService::blockLookupHandler(void* blk_id, size_t idSize, byte* data, size_t dataSize) {
+ if (!currentFile) {
+ return;
+ }
+
+ uint32_t blockId = 0;
+ for (size_t i = 0; i < idSize; ++i) {
+ blockId = (blockId << 8) | ((uint8_t*)blk_id)[i];
+ }
+
+ size_t offset = blockId * dataSize;
+
+ if (!currentFile->seek(offset)) {
+ return;
+ }
+
+ size_t readBytes = currentFile->read(data, dataSize);
+
+ if (readBytes < dataSize) {
+ memset(data + readBytes, 0x1A, dataSize - readBytes);
+ }
+}
+
+bool UartService::receiveBlockHandler(void* blk_id, size_t idSize, byte* data, size_t dataSize) {
+ if (!currentFile) {
+ return false;
+ }
+
+ return currentFile->write(data, dataSize) == dataSize;
+}
+
+bool UartService::xmodemSendFile(File& file) {
+ if (!file || file.isDirectory()) return false;
+
+ // Xmodem Init
+ initXmodem();
+ currentFile = &file;
+ xmodem.setBlockLookupHandler(blockLookupHandler);
+
+ // Calculate
+ size_t fileSize = file.size();
+ size_t blockSize = xmodemBlockSize;
+ size_t totalBlocks = (fileSize + blockSize - 1) / blockSize;
+ const size_t idSize = xmodemIdSize;
+
+ // Construct Ids
+ byte* all_ids = (byte*)malloc(totalBlocks * idSize);
+ for (size_t i = 0; i < totalBlocks; ++i) {
+ unsigned long long blk_id = i + 1;
+ for (size_t j = 0; j < idSize; ++j) {
+ all_ids[i * idSize + j] = (blk_id >> (8 * (idSize - j - 1))) & 0xFF;
+ }
+ }
+
+ // Data, lookup_handler will deal with empty data
+ byte** dummy_data = (byte**)malloc(sizeof(byte*) * totalBlocks);
+ size_t* dummy_lens = (size_t*)malloc(sizeof(size_t) * totalBlocks);
+ for (size_t i = 0; i < totalBlocks; ++i) {
+ dummy_data[i] = nullptr;
+ dummy_lens[i] = blockSize;
+ }
+
+ // Configure container for send_bulk_data
+ struct XModem::bulk_data container = {
+ .data_arr = dummy_data,
+ .len_arr = dummy_lens,
+ .id_arr = all_ids,
+ .count = totalBlocks
+ };
+
+ // Send
+ bool result = xmodem.send_bulk_data(container);
+
+ // Release
+ free(all_ids);
+ free(dummy_data);
+ free(dummy_lens);
+ currentFile = nullptr;
+
+ return result;
+}
+
+
+bool UartService::xmodemReceiveToFile(File& file) {
+ if (!file || file.isDirectory()) return false;
+
+ // Init
+ initXmodem();
+ currentFile = &file;
+
+ // Receive
+ xmodem.setRecieveBlockHandler(receiveBlockHandler);
+ bool ok = xmodem.receive();
+
+ currentFile = nullptr;
+ return ok;
+}
diff --git a/src/Services/UartService.h b/src/Services/UartService.h
index 9b2dc2b9..9b6eedf0 100644
--- a/src/Services/UartService.h
+++ b/src/Services/UartService.h
@@ -2,12 +2,14 @@
#include <string>
#include <vector>
#include "Arduino.h"
+#include <XModem.h>
#include "driver/uart.h"
#include "driver/gpio.h"
#include "esp_rom_gpio.h"
#include "hal/uart_types.h"
#include "soc/uart_periph.h"
#include "Models/ByteCode.h"
+#include <SD.h>
#define UART_PORT UART_NUM_1
@@ -27,4 +29,23 @@ public:
void clearUartBuffer();
void end();
uint32_t buildUartConfig(uint8_t dataBits, char parity, uint8_t stopBits);
+ void initXmodem();
+ bool xmodemReceiveToFile(File& file);
+ bool xmodemSendFile(File& file);
+ static void blockLookupHandler(void* blk_id, size_t idSize, byte* data, size_t dataSize);
+ static bool receiveBlockHandler(void* blk_id, size_t idSize, byte* data, size_t dataSize);
+ void setXmodemReceiveHandler(bool (*handler)(void*, size_t, byte*, size_t));
+ void setXmodemSendHandler(void (*handler)(void*, size_t, byte*, size_t));
+ void setXmodemBlockSize(int32_t size);
+ void setXmodemIdSize(int8_t size);
+ void setXmodemCrc(bool enabled);
+ int32_t getXmodemBlockSize() const;
+ int8_t getXmodemIdSize() const;
+
+private:
+ XModem xmodem;
+ static File* currentFile;
+ int32_t xmodemBlockSize = 128;
+ int8_t xmodemIdSize = 1;
+ XModem::ProtocolType xmodemProtocol = XModem::ProtocolType::CRC_XMODEM;
};