2021-04-04 21:04:12 +01:00
|
|
|
#include "WriteMemory.hpp"
|
2021-10-02 17:39:27 +01:00
|
|
|
|
2022-03-31 21:52:46 +01:00
|
|
|
#include "src/DebugServer/Gdb/ResponsePackets/ErrorResponsePacket.hpp"
|
|
|
|
|
#include "src/DebugServer/Gdb/ResponsePackets/OkResponsePacket.hpp"
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-03-24 19:17:41 +00:00
|
|
|
#include "src/Logger/Logger.hpp"
|
|
|
|
|
#include "src/Exceptions/Exception.hpp"
|
|
|
|
|
|
2022-03-31 16:05:39 +01:00
|
|
|
namespace Bloom::DebugServer::Gdb::AvrGdb::CommandPackets
|
2022-02-05 15:32:08 +00:00
|
|
|
{
|
2022-12-26 21:27:19 +00:00
|
|
|
using Services::TargetControllerService;
|
2022-04-09 15:57:24 +01:00
|
|
|
|
2022-03-24 19:17:41 +00:00
|
|
|
using ResponsePackets::ErrorResponsePacket;
|
|
|
|
|
using ResponsePackets::OkResponsePacket;
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-03-24 19:17:41 +00:00
|
|
|
using namespace Bloom::Exceptions;
|
2021-04-04 21:04:12 +01:00
|
|
|
|
2022-10-01 21:01:37 +01:00
|
|
|
WriteMemory::WriteMemory(const RawPacket& rawPacket, const TargetDescriptor& gdbTargetDescriptor)
|
2022-08-30 02:04:35 +01:00
|
|
|
: CommandPacket(rawPacket)
|
2022-04-03 17:42:00 +01:00
|
|
|
{
|
2022-02-05 15:32:08 +00:00
|
|
|
if (this->data.size() < 4) {
|
|
|
|
|
throw Exception("Invalid packet length");
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 19:13:02 +01:00
|
|
|
const auto packetString = QString::fromLocal8Bit(
|
2022-02-05 15:32:08 +00:00
|
|
|
reinterpret_cast<const char*>(this->data.data() + 1),
|
|
|
|
|
static_cast<int>(this->data.size() - 1)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* The write memory ('M') packet consists of three segments, an address, a length and a buffer.
|
|
|
|
|
* The address and length are separated by a comma character, and the buffer proceeds a colon character.
|
|
|
|
|
*/
|
2022-10-25 19:13:02 +01:00
|
|
|
const auto packetSegments = packetString.split(",");
|
2022-02-05 15:32:08 +00:00
|
|
|
if (packetSegments.size() != 2) {
|
|
|
|
|
throw Exception(
|
|
|
|
|
"Unexpected number of segments in packet data: " + std::to_string(packetSegments.size())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool conversionStatus = false;
|
2022-04-03 20:35:53 +01:00
|
|
|
const auto gdbStartAddress = packetSegments.at(0).toUInt(&conversionStatus, 16);
|
2022-02-05 15:32:08 +00:00
|
|
|
|
|
|
|
|
if (!conversionStatus) {
|
|
|
|
|
throw Exception("Failed to parse start address from write memory packet data");
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 02:04:35 +01:00
|
|
|
this->memoryType = gdbTargetDescriptor.getMemoryTypeFromGdbAddress(gdbStartAddress);
|
|
|
|
|
this->startAddress = gdbStartAddress & ~(gdbTargetDescriptor.getMemoryOffset(this->memoryType));
|
2022-04-03 20:35:53 +01:00
|
|
|
|
2022-10-25 19:13:02 +01:00
|
|
|
const auto lengthAndBufferSegments = packetSegments.at(1).split(":");
|
2022-02-05 15:32:08 +00:00
|
|
|
if (lengthAndBufferSegments.size() != 2) {
|
|
|
|
|
throw Exception(
|
|
|
|
|
"Unexpected number of segments in packet data: "
|
|
|
|
|
+ std::to_string(lengthAndBufferSegments.size())
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-25 19:13:02 +01:00
|
|
|
const auto bufferSize = lengthAndBufferSegments.at(0).toUInt(&conversionStatus, 16);
|
2022-02-05 15:32:08 +00:00
|
|
|
if (!conversionStatus) {
|
|
|
|
|
throw Exception("Failed to parse write length from write memory packet data");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this->buffer = Packet::hexToData(lengthAndBufferSegments.at(1).toStdString());
|
|
|
|
|
|
|
|
|
|
if (this->buffer.size() != bufferSize) {
|
|
|
|
|
throw Exception("Buffer size does not match length value given in write memory packet");
|
|
|
|
|
}
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|
2022-03-24 19:17:41 +00:00
|
|
|
|
2022-12-26 21:27:19 +00:00
|
|
|
void WriteMemory::handle(DebugSession& debugSession, TargetControllerService& targetControllerService) {
|
2022-03-24 19:17:41 +00:00
|
|
|
Logger::debug("Handling WriteMemory packet");
|
|
|
|
|
|
|
|
|
|
try {
|
2022-05-04 19:57:41 +01:00
|
|
|
const auto& memoryDescriptorsByType = debugSession.gdbTargetDescriptor.targetDescriptor.memoryDescriptorsByType;
|
2022-12-03 22:16:21 +00:00
|
|
|
const auto memoryDescriptorIt = memoryDescriptorsByType.find(this->memoryType);
|
2022-05-04 19:57:41 +01:00
|
|
|
|
2022-12-03 22:16:21 +00:00
|
|
|
if (memoryDescriptorIt == memoryDescriptorsByType.end()) {
|
2022-05-04 19:57:41 +01:00
|
|
|
throw Exception("Target does not support the requested memory type.");
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-28 23:31:16 +01:00
|
|
|
if (this->memoryType == Targets::TargetMemoryType::FLASH) {
|
|
|
|
|
/*
|
|
|
|
|
* This shouldn't happen - GDB should send the FlashWrite (vFlashWrite) packet to write to the target's
|
|
|
|
|
* program memory.
|
|
|
|
|
*
|
|
|
|
|
* A number of actions have to be taken before we can write to the target's program memory - this is
|
|
|
|
|
* all covered in the FlashWrite and FlashDone command classes. I don't want to cover it again in here,
|
|
|
|
|
* so just respond with an error and request that this issue be reported.
|
|
|
|
|
*/
|
|
|
|
|
throw Exception(
|
|
|
|
|
"GDB attempted to write to program memory via an \"M\" packet - this is not supported. Please "
|
|
|
|
|
"report this issue to Bloom developers with the full debug log."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 19:57:41 +01:00
|
|
|
if (this->buffer.size() == 0) {
|
|
|
|
|
debugSession.connection.writePacket(OkResponsePacket());
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-03 22:16:21 +00:00
|
|
|
const auto& memoryDescriptor = memoryDescriptorIt->second;
|
2022-05-04 19:57:41 +01:00
|
|
|
|
2022-12-11 15:26:14 +00:00
|
|
|
if (this->memoryType == Targets::TargetMemoryType::EEPROM) {
|
|
|
|
|
// GDB sends EEPROM addresses in relative form - we convert them to absolute form, here.
|
|
|
|
|
this->startAddress = memoryDescriptor.addressRange.startAddress + this->startAddress;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 20:47:48 +01:00
|
|
|
/*
|
|
|
|
|
* In AVR targets, RAM is mapped to many registers and peripherals - we don't want to block GDB from
|
|
|
|
|
* accessing them.
|
|
|
|
|
*/
|
|
|
|
|
const auto memoryStartAddress = (this->memoryType == Targets::TargetMemoryType::RAM)
|
|
|
|
|
? 0x00
|
|
|
|
|
: memoryDescriptor.addressRange.startAddress;
|
|
|
|
|
|
2022-05-04 19:57:41 +01:00
|
|
|
if (
|
2022-05-04 20:47:48 +01:00
|
|
|
this->startAddress < memoryStartAddress
|
2022-05-04 19:57:41 +01:00
|
|
|
|| (this->startAddress + (this->buffer.size() - 1)) > memoryDescriptor.addressRange.endAddress
|
|
|
|
|
) {
|
|
|
|
|
throw Exception(
|
|
|
|
|
"GDB requested access to memory which is outside the target's memory range"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-26 21:27:19 +00:00
|
|
|
targetControllerService.writeMemory(
|
2022-04-03 20:35:53 +01:00
|
|
|
this->memoryType,
|
|
|
|
|
this->startAddress,
|
|
|
|
|
this->buffer
|
|
|
|
|
);
|
2022-03-24 19:17:41 +00:00
|
|
|
|
|
|
|
|
debugSession.connection.writePacket(OkResponsePacket());
|
|
|
|
|
|
|
|
|
|
} catch (const Exception& exception) {
|
|
|
|
|
Logger::error("Failed to write memory to target - " + exception.getMessage());
|
|
|
|
|
debugSession.connection.writePacket(ErrorResponsePacket());
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-04 21:04:12 +01:00
|
|
|
}
|