Created
May 16, 2017 18:17
-
-
Save Demon000/96917475e3617b8dac01adc0e12e824c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <iomanip> | |
#include <vector> | |
#include <algorithm> | |
#include <string> | |
#include <sys/stat.h> | |
using namespace std; | |
vector<uint8_t> readBytes(string filename) { | |
ifstream ifs(filename, ios::binary | ios::ate); | |
ifstream::pos_type pos = ifs.tellg(); | |
vector<uint8_t> result(pos); | |
ifs.seekg(0, ios::beg); | |
ifs.read((char*) &result[0], pos); | |
return result; | |
} | |
void writeBytes(string filename, vector<uint8_t> data, size_t start, size_t length) { | |
ofstream ofs(filename, ios::out | ios::binary); | |
ofs.write((char*) &data[start], length); | |
} | |
uint32_t bytesToUint32(vector<uint8_t> data, size_t start) { | |
uint32_t r = 0; | |
for(size_t i = start + 3; i >= start; i--) { | |
r <<= 8; | |
r |= data[i]; | |
} | |
return r; | |
} | |
int main(int argc, char *argv[]) { | |
vector<string> args(argv + 1, argv + argc); | |
if(args.size() < 2) { | |
return -1; | |
} | |
string dtPath = args[0]; | |
string dtbPath = args[1]; | |
struct stat st; | |
if (stat(dtPath.c_str(), &st) == -1) { | |
return -1; | |
} | |
if (stat(dtbPath.c_str(), &st) == -1) { | |
mkdir(dtbPath.c_str(), 0766); | |
} | |
vector<uint8_t> dtData = readBytes(dtPath); | |
vector<uint32_t> dtbOffsets; | |
uint32_t numDtbs = bytesToUint32(dtData, 4 * 2); | |
for(uint32_t i = 0; i < numDtbs; i++) { | |
cout << "dtb #" << i << "\n"; | |
uint32_t headerOffset = 4 * 3 + 4 * 10 * i; | |
uint32_t offset = bytesToUint32(dtData, headerOffset + 4 * 8); | |
uint32_t size = bytesToUint32(dtData, headerOffset + 4 * 9); | |
cout << "offset: " << offset << "\n"; | |
cout << "size: " << size << "\n"; | |
if(find(dtbOffsets.begin(), dtbOffsets.end(), offset) == dtbOffsets.end()) { | |
string filename = dtbPath + "/" + to_string(i) + ".dtb"; | |
writeBytes(filename, dtData, offset, size); | |
dtbOffsets.push_back(offset); | |
} | |
cout << "\n"; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment