radio_tool 0.2.1
Loading...
Searching...
No Matches
ailunce_fw.cpp
1
19#include <radio_tool/fw/ailunce_fw.hpp>
20#include <radio_tool/util.hpp>
21
22#include <iomanip>
23
24using namespace radio_tool::fw;
25
26auto AilunceFW::Read(const std::string &file) -> void
27{
28 auto i = std::ifstream(file, std::ios_base::binary);
29 if (i.is_open())
30 {
31 i.seekg(0, std::ios_base::end);
32 auto binarySize = i.tellg();
33 memory_ranges.push_back(std::make_pair(0, binarySize));
34 i.seekg(std::ios_base::beg);
35 data.resize(binarySize);
36 i.read((char *)data.data(), data.size());
37 }
38 i.close();
39}
40
41auto AilunceFW::Write(const std::string &file) -> void
42{
43 std::ofstream fout(file, std::ios_base::binary);
44 if (fout.is_open())
45 {
46 fout.write((char *)data.data(), data.size());
47 fout.close();
48 }
49}
50
51auto AilunceFW::ToString() const -> std::string
52{
53 std::stringstream out;
54 out << "== Ailunce Firmware == " << std::endl;
55 auto n = 0;
56 for (const auto &m : memory_ranges)
57 {
58 out << " " << n++ << ": Start=0x" << std::setfill('0') << std::setw(8) << std::hex << m.first
59 << ", Length=0x" << std::setfill('0') << std::setw(8) << std::hex << m.second
60 << std::endl;
61 }
62 return out.str();
63}
64
65auto AilunceFW::SupportsFirmwareFile(const std::string &file) -> bool
66{
67 std::ifstream i;
68 i.open(file, i.binary);
69 if (i.is_open())
70 {
71 i.close();
72 return true;
73 }
74 else
75 {
76 throw std::runtime_error("Can't open firmware file");
77 }
78}
79
80// Ailunce hts are flashed with a USB serial adapter, no way to identify
81auto AilunceFW::SupportsRadioModel(const std::string &model) -> bool
82{
83 return model == "HD1";
84}
85
86auto AilunceFW::GetRadioModel() const -> const std::string
87{
88 return "Ailunce HD1";
89}
90
91auto AilunceFW::SetRadioModel(const std::string &model) -> void
92{
93}
94
95auto AilunceFW::Decrypt() -> void
96{
97 ApplyXOR();
98}
99
100auto AilunceFW::Encrypt() -> void
101{
102 ApplyXOR();
103}
104
105auto AilunceFW::ApplyXOR() -> void
106{
107 for (uint32_t i = 0; i < (data.size() / sizeof(uint32_t)); i++)
108 {
109 uint32_t *word = reinterpret_cast<uint32_t *>(data.data()) + i;
110 if (*word == 0x0 || *word == 0xffffffff)
111 *word ^= 0xffffffff;
112 else if (*word & (1 << 28))
113 *word ^= 0x01111111;
114 else
115 *word ^= 0x07777777;
116 }
117 // Last bytes
118 for (auto z = data.size() - (data.size() % sizeof(uint32_t));
119 z < data.size(); z++)
120 {
121 if (data[z] == 0x00 || data[z] == 0xff)
122 data[z] ^= 0xff;
123 else if (data[z] & 1)
124 data[z] ^= 0x01;
125 else
126 data[z] ^= 0x07;
127 }
128}
129
130auto AilunceFW::IsCompatible(const FirmwareSupport* Other) const -> bool
131{
132 if (typeid(Other) != typeid(this)) {
133 return false;
134 }
135
136 auto afw = dynamic_cast<const AilunceFW*>(Other);
137 return afw->radio_model == radio_model;
138}
auto ToString() const -> std::string override
Definition: ailunce_fw.cpp:51
auto SetRadioModel(const std::string &) -> void override
Definition: ailunce_fw.cpp:91
auto IsCompatible(const FirmwareSupport *Other) const -> bool override
Definition: ailunce_fw.cpp:130
auto Decrypt() -> void override
Definition: ailunce_fw.cpp:95
static auto SupportsRadioModel(const std::string &model) -> bool
Definition: ailunce_fw.cpp:81
auto Encrypt() -> void override
Definition: ailunce_fw.cpp:100
auto Read(const std::string &file) -> void override
Definition: ailunce_fw.cpp:26
static auto SupportsFirmwareFile(const std::string &file) -> bool
Definition: ailunce_fw.cpp:65
auto Write(const std::string &file) -> void override
Definition: ailunce_fw.cpp:41
auto GetRadioModel() const -> const std::string override
Definition: ailunce_fw.cpp:86
std::vector< std::pair< uint32_t, uint32_t > > memory_ranges
Definition: fw.hpp:172