ae4afe4dfd988003e8f8d2d31d6523db8ae93fd7
[GitHub/LineageOS/G12/android_hardware_amlogic_tools_dtbtool.git] / dtbSplit.cpp
1 /*
2 * Copyright (c) 2016 Wilhansen Li. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above
10 copyright notice, this list of conditions and the following
11 disclaimer in the documentation and/or other materials provided
12 with the distribution.
13 * Neither the name of Wilhansen Li nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <iostream>
31 #include <fstream>
32 #include <sstream>
33 #include <cstdint>
34 #include <string>
35 #include <cstdio>
36 #include <vector>
37 using namespace std;
38
39 #define AML_DT_HEADER 0x5f4c4d41
40 #define DT_HEADER_MAGIC 0xedfe0dd0
41 #define AML_DT_ID_VARI_TOTAL 3
42
43 struct DTHeader {
44 uint32_t magic; /* magic word of OF_DT_HEADER */
45 uint32_t totalsize; /* total size of DT block */
46 };
47 struct Header {
48 uint32_t magic;
49 uint32_t version;
50 uint32_t entry_count;
51 };
52
53 template<unsigned int ID_SIZE=4>
54 struct HeaderEntry {
55 char soc[ID_SIZE];
56 char plat[ID_SIZE];
57 char vari[ID_SIZE];
58 uint32_t offset;
59 char padding[4];
60 };
61
62 typedef HeaderEntry<4> HeaderEntryV1;
63 typedef HeaderEntry<16> HeaderEntryV2;
64
65 void trimSpace(char *b, const int len) {
66 int len2 = len;
67 while (len2 > 0 && isspace(b[len2 - 1])) {
68 len2--;
69 }
70 if (len2 < len && len2 > 0) {
71 b[len2] = 0;
72 b[len - 1] = 0;
73 }
74 }
75 template<unsigned int ID_SIZE>
76 void dumpData(const uint32_t entries, const string &dest, ifstream &dtb) {
77 typedef HeaderEntry<ID_SIZE> HeaderType;
78
79 vector<HeaderType> headers;
80 for ( uint32_t i = 0; i < entries; ++i ) {
81 HeaderType h;
82 dtb.read((char*)&h, sizeof(h));
83
84 headers.push_back(h);
85 }
86 for ( uint32_t i = 0; i < headers.size(); ++i ) {
87 auto &h = headers[i];
88 ostringstream id;
89
90 auto u32soc = reinterpret_cast<uint32_t*>(h.soc);
91 auto u32plat = reinterpret_cast<uint32_t*>(h.plat);
92 auto u32vari = reinterpret_cast<uint32_t*>(h.vari);
93 for ( uint32_t j = 0; j < ID_SIZE/sizeof(uint32_t); ++j ) {
94 *(u32soc + j) = ntohl(*(u32soc + j));
95 *(u32plat + j) = ntohl(*(u32plat + j));
96 *(u32vari + j) = ntohl(*(u32vari + j));
97 }
98 trimSpace(h.soc, ID_SIZE);
99 trimSpace(h.plat, ID_SIZE);
100 trimSpace(h.vari, ID_SIZE);
101
102 if ( h.soc[ID_SIZE-1] == 0 ) {
103 id << h.soc;
104 } else {
105 id.write(h.soc, sizeof(h.soc));
106 }
107 id << '-';
108 if ( h.plat[ID_SIZE-1] == 0 ) {
109 id << h.plat;
110 } else {
111 id.write(h.plat, sizeof(h.plat));
112 }
113 id << '-';
114 if ( h.vari[ID_SIZE-1] == 0 ) {
115 id << h.vari;
116 } else {
117 id.write(h.vari, sizeof(h.vari));
118 }
119 cout << "Found header: " << id.str() << '\n';
120
121 dtb.seekg(h.offset);
122 DTHeader dtheader;
123 dtb.read((char*)&dtheader, sizeof(dtheader));
124 if ( dtheader.magic != DT_HEADER_MAGIC ) {
125 cout.setf(ios::hex);
126 cout << "\tDTB Header mismatch. Found: " << dtheader.magic;
127 continue;
128 }
129 dtheader.totalsize = ntohl(dtheader.totalsize);
130 cout.setf(ios::dec);
131 cout << "\t offset: " << h.offset << " size: " << dtheader.totalsize << '\n';
132 dtb.seekg(h.offset);
133 vector<char> data(dtheader.totalsize);
134 dtb.read(data.data(), data.size());
135 ofstream output(dest + id.str() + ".dtb", ios::binary);
136 output.write(data.data(), data.size());
137 }
138
139 }
140
141 int main(int argc, char **argv) {
142 if ( argc < 3 ) {
143 cerr << "Usage: " << argv[0] << " boot.img out_prefix\n";
144 return 1;
145 }
146
147 ifstream dtb(argv[1], ios::binary);
148 if ( !dtb ) {
149 cerr << "Unable to open dtb file: " << argv[2] << endl;
150 return 1;
151 }
152 string dest;
153 if ( argc > 2 ) {
154 dest = argv[2];
155 }
156 Header header;
157 dtb.read((char*)&header, sizeof(header));
158
159 if ( header.magic != AML_DT_HEADER ) {
160 cerr << "Invalid AML DTB header." << endl;
161 return 1;
162 }
163 cout << "DTB Version: " << header.version << " entries: " << header.entry_count << endl;
164
165 if(header.version == 1) {
166 dumpData<4>(header.entry_count, dest, dtb);
167 } else if(header.version == 2) {
168 dumpData<16>(header.entry_count, dest, dtb);
169 } else {
170 cerr << "Unrecognized DTB version" << endl;
171 return 1;
172 }
173
174 return 0;
175 }