178 lines
5.4 KiB
C++
178 lines
5.4 KiB
C++
/*****************************************************************************
|
|
* Copyright (c) 2020, rrcSmall FM10K-Documentation Contributors
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions are met:
|
|
*
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
* this list of conditions and the following disclaimer.
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
* * Neither the name of the copyright holder nor the names of its contributors
|
|
* may be used to endorse or promote products derived from this software
|
|
* without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*****************************************************************************/
|
|
|
|
#pragma once
|
|
|
|
#include <utility>
|
|
#include <vector>
|
|
#include <cstdint>
|
|
#include <map>
|
|
|
|
#include "SmallFirmwareFormat.h"
|
|
#include "Configuration.h"
|
|
#include "instructions/Instruction.h"
|
|
|
|
class ImageFormat {
|
|
|
|
public:
|
|
|
|
enum class HeaderSpeed : uint8_t {
|
|
SPEED_390_KHZ = 0,
|
|
SPEED_780_KHZ,
|
|
SPEED_1560_KHZ,
|
|
SPEED_3125_KHZ,
|
|
SPEED_6250_KHZ,
|
|
SPEED_12500_KHZ,
|
|
SPEED_25000_KHZ,
|
|
SPEED_50000_KHZ
|
|
};
|
|
|
|
enum class HeaderMode : uint8_t {
|
|
MODE_SINGLE = 0,
|
|
MODE_DUAL,
|
|
MODE_QUAD,
|
|
MODE_SINGLE_FAST
|
|
};
|
|
|
|
|
|
static ImageFormat fromBytes(const std::vector<uint8_t> &image);
|
|
|
|
void decodeAnalyzeInstructionsAt(uint32_t offset);
|
|
|
|
const std::unique_ptr<Instruction::Instruction> &
|
|
findConstInstructionByAddress(uint32_t addr, bool indirect = false) const {
|
|
if (instructions.find(addr) != instructions.end()) {
|
|
return instructions.find(addr)->second;
|
|
}
|
|
|
|
if (indirect) {
|
|
for (auto &instruction : instructions) {
|
|
if (
|
|
(instruction.second->getEndAddress() == 0 && addr == instruction.second->getAddress())
|
|
|| (
|
|
instruction.second->getEndAddress() != 0
|
|
&& addr >= instruction.second->getAddress()
|
|
&& addr < instruction.second->getEndAddress()
|
|
)
|
|
) {
|
|
return instruction.second;
|
|
}
|
|
}
|
|
}
|
|
|
|
return NULL_INSTRUCTION;
|
|
}
|
|
|
|
std::unique_ptr<Instruction::Instruction> &findInstructionByAddress(uint32_t addr, bool indirect = false) {
|
|
if (instructions.find(addr) != instructions.end()) {
|
|
return instructions.find(addr)->second;
|
|
}
|
|
|
|
if (indirect) {
|
|
for (auto &instruction : instructions) {
|
|
if (
|
|
(instruction.second->getEndAddress() == 0 && addr == instruction.second->getAddress())
|
|
|| (
|
|
instruction.second->getEndAddress() != 0
|
|
&& addr >= instruction.second->getAddress()
|
|
&& addr < instruction.second->getEndAddress()
|
|
)
|
|
) {
|
|
return instruction.second;
|
|
}
|
|
}
|
|
}
|
|
|
|
return NULL_INSTRUCTION;
|
|
}
|
|
|
|
ImageFormat() {
|
|
|
|
}
|
|
|
|
const auto &getHeader() const {
|
|
return header;
|
|
}
|
|
|
|
const auto &getInstructions() const {
|
|
return instructions;
|
|
}
|
|
|
|
const auto &getJumpTable() const {
|
|
return jumpTable;
|
|
}
|
|
|
|
const auto &getBaseImage() const {
|
|
return baseImage;
|
|
}
|
|
|
|
const auto &getBootConfig() const {
|
|
return bootConfig;
|
|
}
|
|
|
|
auto &getModifiableBootConfig() {
|
|
return bootConfig;
|
|
}
|
|
|
|
std::vector<uint8_t> toBytes() const;
|
|
|
|
std::string imageSignature;
|
|
|
|
private:
|
|
|
|
struct {
|
|
uint8_t reserved: 3;
|
|
ImageFormat::HeaderSpeed speed: 3;
|
|
ImageFormat::HeaderMode mode: 2;
|
|
uint32_t baseAddress: 24;
|
|
} header;
|
|
|
|
static const int CFG_SIGNATURE = 0x100;
|
|
static const int CFG_HEADER = 16;
|
|
static const int CFG_LENGTH = 16;
|
|
|
|
static std::unique_ptr<Instruction::Instruction> NULL_INSTRUCTION;
|
|
|
|
struct {
|
|
uint8_t length: 8;
|
|
uint32_t base: 24;
|
|
} cfgHeader;
|
|
|
|
struct {
|
|
uint8_t fileFormat: 8;
|
|
uint8_t version: 8;
|
|
uint16_t length: 16;
|
|
} cfg;
|
|
|
|
Configuration bootConfig;
|
|
|
|
std::map<uint32_t, std::unique_ptr<Instruction::Instruction>> instructions;
|
|
|
|
std::vector<uint8_t> baseImage;
|
|
|
|
AnalysisJumpTable jumpTable;
|
|
}; |