ae4afe4dfd988003e8f8d2d31d6523db8ae93fd7
2 * Copyright (c) 2016 Wilhansen Li. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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.
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.
39 #define AML_DT_HEADER 0x5f4c4d41
40 #define DT_HEADER_MAGIC 0xedfe0dd0
41 #define AML_DT_ID_VARI_TOTAL 3
44 uint32_t magic
; /* magic word of OF_DT_HEADER */
45 uint32_t totalsize
; /* total size of DT block */
53 template<unsigned int ID_SIZE
=4>
62 typedef HeaderEntry
<4> HeaderEntryV1
;
63 typedef HeaderEntry
<16> HeaderEntryV2
;
65 void trimSpace(char *b
, const int len
) {
67 while (len2
> 0 && isspace(b
[len2
- 1])) {
70 if (len2
< len
&& len2
> 0) {
75 template<unsigned int ID_SIZE
>
76 void dumpData(const uint32_t entries
, const string
&dest
, ifstream
&dtb
) {
77 typedef HeaderEntry
<ID_SIZE
> HeaderType
;
79 vector
<HeaderType
> headers
;
80 for ( uint32_t i
= 0; i
< entries
; ++i
) {
82 dtb
.read((char*)&h
, sizeof(h
));
86 for ( uint32_t i
= 0; i
< headers
.size(); ++i
) {
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
));
98 trimSpace(h
.soc
, ID_SIZE
);
99 trimSpace(h
.plat
, ID_SIZE
);
100 trimSpace(h
.vari
, ID_SIZE
);
102 if ( h
.soc
[ID_SIZE
-1] == 0 ) {
105 id
.write(h
.soc
, sizeof(h
.soc
));
108 if ( h
.plat
[ID_SIZE
-1] == 0 ) {
111 id
.write(h
.plat
, sizeof(h
.plat
));
114 if ( h
.vari
[ID_SIZE
-1] == 0 ) {
117 id
.write(h
.vari
, sizeof(h
.vari
));
119 cout
<< "Found header: " << id
.str() << '\n';
123 dtb
.read((char*)&dtheader
, sizeof(dtheader
));
124 if ( dtheader
.magic
!= DT_HEADER_MAGIC
) {
126 cout
<< "\tDTB Header mismatch. Found: " << dtheader
.magic
;
129 dtheader
.totalsize
= ntohl(dtheader
.totalsize
);
131 cout
<< "\t offset: " << h
.offset
<< " size: " << dtheader
.totalsize
<< '\n';
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());
141 int main(int argc
, char **argv
) {
143 cerr
<< "Usage: " << argv
[0] << " boot.img out_prefix\n";
147 ifstream
dtb(argv
[1], ios::binary
);
149 cerr
<< "Unable to open dtb file: " << argv
[2] << endl
;
157 dtb
.read((char*)&header
, sizeof(header
));
159 if ( header
.magic
!= AML_DT_HEADER
) {
160 cerr
<< "Invalid AML DTB header." << endl
;
163 cout
<< "DTB Version: " << header
.version
<< " entries: " << header
.entry_count
<< endl
;
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
);
170 cerr
<< "Unrecognized DTB version" << endl
;