rrcSmall/src/main.cpp
2020-12-17 20:01:57 +01:00

93 lines
4.3 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.
*****************************************************************************/
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include "ImageFormat.h"
int main(int argc, char* argv[]) {
if(argc < 2){
std::cout << "Usage: " << argv[0] << " nvmImage.bin > decoded.txt\n";
return 1;
}
/*
std::vector<uint8_t> v{0x00, 0x04, 0x01, 0x04, 0x00, 0x00, 0x00, 0x19};
for (uint8_t &c: v)
std::cout << std::hex << (uint32_t) c << "\n";
auto instruction = ImageFormat::Instruction::fromBytes(0, v);
std::cout << instruction.toString();
return 0;
*/
std::ifstream image(argv[1]);
if(image.is_open()){
std::vector<uint8_t> bytes;
while(!image.eof()){
bytes.push_back(image.get());
}
auto imageObject = ImageFormat::fromBytes(bytes, false);
for(auto& k : imageObject.getBootConfig()){
std::cout << k << "\n";
}
uint32_t prevAddress = 0;
for(const auto& instruction : imageObject.getInstructions()){
if(instruction.address < prevAddress){
std::cout << "====== DECODE ERROR? ========== " << std::hex << std::setw(8) << std::setfill('0') << prevAddress << " - " << std::hex << std::setw(8) << std::setfill('0') << instruction.address << "\n";
}else if((instruction.address - prevAddress) >= 4 && (instruction.address - prevAddress) < 1024){
for(uint32_t addr = prevAddress; addr < instruction.address; addr += 4){
std::stringstream op;
std::string printable;
op << std::hex << std::setw(8) << std::setfill('0') << addr << ": ";
for(uint32_t i = 0; i < 4; ++i){
op << std::hex << std::setw(2) << std::setfill('0') << (uint32_t)bytes[addr + i];
if(bytes[addr + i] >= 0x20 && bytes[addr + i] < 0x7F){
printable += bytes[addr + i];
}else{
printable += ".";
}
}
op << " " << printable << "\n";
std::cout << op.str();
}
}else if((instruction.address - prevAddress) >= 1024){
std::cout << "================ " << std::hex << std::setw(8) << std::setfill('0') << prevAddress << " - " << std::hex << std::setw(8) << std::setfill('0') << instruction.address << "\n";
}
std::cout << instruction.toString() << "\n";
prevAddress = instruction.endAddress;
}
}
return 0;
}