summaryrefslogtreecommitdiff
path: root/view/md1rom/md1rom.h
blob: 445e27aa06ffca2d287aefb034ae543d5d45100d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "binaryninjaapi.h"

namespace BinaryNinja
{
	const uint32_t MAGIC_1 = 0x58881688;
	const uint32_t MAGIC_2 = 0x58891689;
	const uint64_t MAIN_ROM_BASE = 0x90000000;

	struct Md1romSegment
	{
		uint32_t magic;
		uint32_t length;
		std::string name;
		uint32_t addr;
		uint32_t mode;
		uint32_t magic2;
		uint32_t offset;
		// start of the header in the raw view
		uint32_t headerStart;

		// start of the data in the raw view
		uint32_t dataStart;
	};

	struct Md1DebugSymbol
	{
		std::string name;
		uint32_t start;
		uint32_t end;
	};

	struct CATDEntryHeader
	{
		uint32_t unknown;
		uint32_t offset;
		uint32_t size;
	};

	struct CATDEntry
	{
		uint32_t offset;
		uint32_t size;
		uint32_t uncompressedSize;
	};

	class Md1romView: public BinaryView
	{
		bool m_parseOnly;
		uint64_t m_entryPoint{};
		BNEndianness m_endian = LittleEndian;
		size_t m_addressSize = 4;
		Ref<Architecture> m_arch = nullptr;
		Ref<Platform> m_plat = nullptr;
		Ref<Logger> m_logger;
		bool m_relocatable = false;

		std::vector<Md1romSegment> m_segments;
		std::vector<Md1DebugSymbol> m_debugSymbols;
		std::vector<CATDEntryHeader> m_catdEntryHeaders;
		std::vector<CATDEntry> m_catdEntries;

		bool m_mainRomFound = false, m_dbgInfoFound = false, m_dbgDbFound = false;
		Md1romSegment m_mainRom, m_dbgInfoSeg, m_dbgDatabaseSeg;

		SymbolQueue* m_symbolQueue = nullptr;

		virtual uint64_t PerformGetEntryPoint() const override;

		virtual bool PerformIsExecutable() const override { return true; }
		virtual BNEndianness PerformGetDefaultEndianness() const override;
		virtual bool PerformIsRelocatable() const override;
		virtual size_t PerformGetAddressSize() const override;

		void ParseDebugInfo();
		void ParseDebugDatabase();
		void DefineMd1RomSymbol(BNSymbolType type, const std::string& name, uint64_t addr,
							 	BNSymbolBinding binding=NoBinding, Ref<Type> typeObj=nullptr);

	public:
		Md1romView(BinaryView* data, bool parseOnly = false);
		~Md1romView();

		virtual bool Init() override;
	};

	class Md1romViewType: public BinaryViewType
	{
		Ref<Logger> m_logger;
	public:
		Md1romViewType();
		virtual Ref<BinaryView> Create(BinaryView* data) override;
		virtual Ref<BinaryView> Parse(BinaryView* data) override;
		virtual bool IsTypeValidForData(BinaryView* data) override;
		virtual Ref<Settings> GetLoadSettingsForData(BinaryView* data) override;
	};

	void InitMd1romViewType();
}