Files
BloomPatched/src/DebugServer/Gdb/RiscVGdb/CommandPackets/FlashErase.cpp

75 lines
2.5 KiB
C++
Raw Normal View History

2024-11-16 20:43:22 +00:00
#include "FlashErase.hpp"
#include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp"
#include "src/DebugServer/Gdb/ResponsePackets/OkResponsePacket.hpp"
#include "src/Services/StringService.hpp"
#include "src/Logger/Logger.hpp"
#include "src/Exceptions/Exception.hpp"
namespace DebugServer::Gdb::RiscVGdb::CommandPackets
{
using Services::TargetControllerService;
using ResponsePackets::ErrorResponsePacket;
using ResponsePackets::OkResponsePacket;
using namespace Exceptions;
FlashErase::FlashErase(const RawPacket& rawPacket)
: CommandPacket(rawPacket)
{
using Services::StringService;
if (rawPacket.size() < 8) {
throw Exception{"Invalid packet length"};
}
const auto command = std::string{this->data.begin() + 12, this->data.end()};
const auto delimiterPos = command.find_first_of(',');
if (delimiterPos == std::string::npos) {
throw Exception{"Invalid packet"};
}
this->startAddress = StringService::toUint32(command.substr(0, delimiterPos), 16);
this->bytes = StringService::toUint32(command.substr(delimiterPos + 1), 16);
}
void FlashErase::handle(
Gdb::DebugSession& debugSession,
const RiscVGdbTargetDescriptor& gdbTargetDescriptor,
const Targets::TargetDescriptor& targetDescriptor,
TargetControllerService& targetControllerService
) {
Logger::info("Handling FlashErase packet");
try {
targetControllerService.enableProgrammingMode();
Logger::warning("Erasing program memory, in preparation for programming");
// We don't erase a specific address range - we just erase the entire program memory.
targetControllerService.eraseMemory(
gdbTargetDescriptor.systemAddressSpaceDescriptor,
gdbTargetDescriptor.programMemorySegmentDescriptor
);
debugSession.connection.writePacket(OkResponsePacket{});
} catch (const Exception& exception) {
Logger::error("Failed to erase flash memory - " + exception.getMessage());
debugSession.programmingSession.reset();
try {
targetControllerService.disableProgrammingMode();
} catch (const Exception& exception) {
Logger::error("Failed to disable programming mode - " + exception.getMessage());
}
debugSession.connection.writePacket(ErrorResponsePacket{});
}
}
}