Updated TDF address space, memory segment and memory segment section extraction to align with new TDF format

This commit is contained in:
Nav
2024-02-13 20:22:18 +00:00
parent 46d75b3f4b
commit f5677b6235
5 changed files with 316 additions and 139 deletions

View File

@@ -2,9 +2,11 @@
#include <cstdint>
#include <optional>
#include <QDomElement>
#include "src/Helpers/BiMap.hpp"
#include "MemorySegmentSection.hpp"
#include "src/Services/StringService.hpp"
#include "src/Exceptions/Exception.hpp"
namespace Targets::TargetDescription
{
@@ -19,34 +21,64 @@ namespace Targets::TargetDescription
RAM,
LOCKBITS,
OSCCAL,
PRODUCTION_SIGNATURES,
SIGNATURES,
USER_SIGNATURES,
};
struct MemorySegment
{
std::string key;
std::string name;
MemorySegmentType type;
std::uint32_t startAddress;
std::uint32_t size;
std::optional<std::uint16_t> pageSize;
std::map<std::string, MemorySegmentSection, std::less<void>> sectionsByKey;
/**
* Mapping of all known memory segment types by their name. Any memory segments belonging to a type
* not defined in here should be ignored.
*/
static const inline BiMap<std::string, MemorySegmentType> typesMappedByName = {
{"aliased", MemorySegmentType::ALIASED},
{"regs", MemorySegmentType::REGISTERS},
{"eeprom", MemorySegmentType::EEPROM},
{"flash", MemorySegmentType::FLASH},
{"fuses", MemorySegmentType::FUSES},
{"io", MemorySegmentType::IO},
{"ram", MemorySegmentType::RAM},
{"lockbits", MemorySegmentType::LOCKBITS},
{"osccal", MemorySegmentType::OSCCAL},
{"signatures", MemorySegmentType::SIGNATURES},
{"user_signatures", MemorySegmentType::USER_SIGNATURES},
};
MemorySegment(
const std::string& key,
const std::string& name,
MemorySegmentType type,
std::uint32_t startAddress,
std::uint32_t size,
const std::optional<std::uint16_t>& pageSize,
const std::map<std::string, MemorySegmentSection, std::less<void>>& sectionsByKey
)
: key(key)
, name(name)
, type(type)
, startAddress(startAddress)
, size(size)
, pageSize(pageSize)
, sectionsByKey(sectionsByKey)
{}
std::optional<std::reference_wrapper<const MemorySegmentSection>> tryGetSection(
std::string_view keyStr
) const {
const auto keys = Services::StringService::split(keyStr, '.');
const auto firstSubgroupIt = this->sectionsByKey.find(*keys.begin());
return firstSubgroupIt != this->sectionsByKey.end()
? keys.size() > 1
? firstSubgroupIt->second.tryGetSubSection(keys | std::ranges::views::drop(1))
: std::optional(std::cref(firstSubgroupIt->second))
: std::nullopt;
}
std::optional<std::reference_wrapper<const MemorySegmentSection>> getSection(
std::string_view keyStr
) const {
const auto propertyGroup = this->tryGetSection(keyStr);
if (!propertyGroup.has_value()) {
throw Exceptions::Exception(
"Failed to get memory segment section \"" + std::string(keyStr)
+ "\" from memory segment in TDF - section not found"
);
}
return propertyGroup->get();
}
};
}