Skip to content

Commit 969a247

Browse files
committed
Merge remote-tracking branch 'origin/GP-1436_lazybinding-dev_DTB_FTD_Support--SQUASHED'
2 parents e2188d4 + 629924e commit 969a247

File tree

11 files changed

+1246
-0
lines changed

11 files changed

+1246
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* ###
2+
* IP: GHIDRA
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package ghidra.file.formats.dtb;
17+
18+
/**
19+
* Device Tree (DT) constants.
20+
*
21+
*/
22+
public final class DtConstants {
23+
24+
/** Device Tree (DT) Magic Value */
25+
public static final int DT_TABLE_MAGIC = 0xd7b7ab1e;
26+
27+
/** Device Tree (DT) Magic Value Bytes */
28+
public static final byte[] DT_TABLE_MAGIC_BYTES =
29+
new byte[] { (byte) 0xd7, (byte) 0xb7, (byte) 0xab, (byte) 0x1e };
30+
31+
/** Device Tree (DT) Magic Value Size */
32+
public static final int DT_TABLE_MAGIC_SIZE = 4;
33+
34+
/** Device Tree (DT) Page Size */
35+
public static final int DT_TABLE_DEFAULT_PAGE_SIZE = 2048;
36+
37+
/** Device Tree (DT) Default Version */
38+
public static final int DT_TABLE_DEFAULT_VERSION = 0;
39+
40+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/* ###
2+
* IP: GHIDRA
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package ghidra.file.formats.dtb;
17+
18+
import java.io.IOException;
19+
20+
import ghidra.app.util.bin.*;
21+
import ghidra.program.model.data.DataType;
22+
import ghidra.util.exception.DuplicateNameException;
23+
24+
/**
25+
* Class to represent a Device Tree (DT) Table Entry.
26+
*
27+
* @see <a href="https://github.com/u-boot/u-boot/blob/master/include/dt_table.h#L40">master/include/dt_table.h</a>
28+
*/
29+
public class DtTableEntry implements StructConverter {
30+
31+
private int dt_size;
32+
private int dt_offset;
33+
private int id;
34+
private int rev;
35+
private int[] custom;
36+
37+
private FdtHeader _fdtHeader;
38+
39+
public DtTableEntry(BinaryReader reader) throws IOException {
40+
if (!reader.isBigEndian()) {
41+
throw new IOException("DTB is always big endian.");
42+
}
43+
44+
dt_size = reader.readNextInt();
45+
dt_offset = reader.readNextInt();
46+
id = reader.readNextInt();
47+
rev = reader.readNextInt();
48+
custom = reader.readNextIntArray(4);
49+
50+
BinaryReader clonedReader = reader.clone(dt_offset);
51+
if (clonedReader.peekNextInt() == FdtConstants.FDT_MAGIC) {
52+
_fdtHeader = new FdtHeader(clonedReader);
53+
}
54+
}
55+
56+
/**
57+
* Returns the Device Tree (DT) Table Size.
58+
* @return the Device Tree (DT) Table Size
59+
*/
60+
public int getDtSize() {
61+
return dt_size;
62+
}
63+
64+
/**
65+
* Returns the Device Tree (DT) Table Offset.
66+
* @return the Device Tree (DT) Table Offset
67+
*/
68+
public int getDtOffset() {
69+
return dt_offset;
70+
}
71+
72+
/**
73+
* Returns the Device Tree (DT) Table ID.
74+
* @return the Device Tree (DT) Table ID
75+
*/
76+
public int getId() {
77+
return id;
78+
}
79+
80+
/**
81+
* Returns the Device Tree (DT) Table Revision.
82+
* @return the Device Tree (DT) Table Revision
83+
*/
84+
public int getRev() {
85+
return rev;
86+
}
87+
88+
/**
89+
* Returns the Device Tree (DT) Table Custom Bytes.
90+
* @return the Device Tree (DT) Table Custom Bytes
91+
*/
92+
public int[] getCustom() {
93+
return custom;
94+
}
95+
96+
/**
97+
* Returns the FDT Header.
98+
* @return the FDT Header
99+
*/
100+
public FdtHeader getFdtHeader() {
101+
return _fdtHeader;
102+
}
103+
104+
@Override
105+
public DataType toDataType() throws DuplicateNameException, IOException {
106+
return StructConverterUtil.toDataType(this);
107+
}
108+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/* ###
2+
* IP: GHIDRA
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package ghidra.file.formats.dtb;
17+
18+
import java.io.IOException;
19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import ghidra.app.util.bin.*;
23+
import ghidra.program.model.data.DataType;
24+
import ghidra.util.exception.DuplicateNameException;
25+
26+
/**
27+
* Class to represent a Device Tree (DT) Table Header.
28+
*
29+
* @see <a href="https://source.android.com/devices/architecture/dto/partitions">devices/architecture/dto/partitions</a>
30+
*
31+
* @see <a href="https://github.com/u-boot/u-boot/blob/master/include/dt_table.h#L19">master/include/dt_table.h</a>
32+
*/
33+
public class DtTableHeader implements StructConverter {
34+
35+
private int magic;
36+
private int total_size;
37+
private int header_size;
38+
private int dt_entry_size;
39+
private int dt_entry_count;
40+
private int dt_entries_offset;
41+
private int page_size;
42+
private int version;
43+
44+
private List<DtTableEntry> _entries = new ArrayList<>();
45+
46+
public DtTableHeader(BinaryReader reader) throws IOException {
47+
if (!reader.isBigEndian()) {
48+
throw new IOException("DTB is always big endian.");
49+
}
50+
51+
magic = reader.readNextInt();
52+
if ( magic != DtConstants.DT_TABLE_MAGIC) {
53+
throw new IOException("Invalid DTB Header magic.");
54+
}
55+
56+
total_size = reader.readNextInt();
57+
header_size = reader.readNextInt();
58+
dt_entry_size = reader.readNextInt();
59+
dt_entry_count = reader.readNextInt();
60+
dt_entries_offset = reader.readNextInt();
61+
page_size = reader.readNextInt();
62+
version = reader.readNextInt();
63+
64+
reader.setPointerIndex(dt_entries_offset);
65+
for (int i = 0; i < dt_entry_count; ++i) {
66+
_entries.add(new DtTableEntry(reader));
67+
}
68+
}
69+
70+
/**
71+
* Returns the Device Tree (DT) magic value.
72+
* @return the Device Tree (DT) magic value
73+
*/
74+
public int getMagic() {
75+
return magic;
76+
}
77+
78+
/**
79+
* Returns the Device Tree (DT) total size.
80+
* @return the Device Tree (DT) total size
81+
*/
82+
public int getTotalSize() {
83+
return total_size;
84+
}
85+
86+
/**
87+
* Returns the Device Tree (DT) header size.
88+
* @return the Device Tree (DT) header size
89+
*/
90+
public int getHeaderSize() {
91+
return header_size;
92+
}
93+
94+
/**
95+
* Returns the Device Tree (DT) entry size.
96+
* @return the Device Tree (DT) entry size
97+
*/
98+
public int getDtEntrySize() {
99+
return dt_entry_size;
100+
}
101+
102+
/**
103+
* Returns the Device Tree (DT) entry count.
104+
* @return the Device Tree (DT) entry count
105+
*/
106+
public int getDtEntryCount() {
107+
return dt_entry_count;
108+
}
109+
110+
/**
111+
* Returns the Device Tree (DT) entries offset.
112+
* @return the Device Tree (DT) entries offset
113+
*/
114+
public int getDtEntriesOffset() {
115+
return dt_entries_offset;
116+
}
117+
118+
/**
119+
* Returns the Device Tree (DT) page size.
120+
* @return the Device Tree (DT) page size
121+
*/
122+
public int getPageSize() {
123+
return page_size;
124+
}
125+
126+
/**
127+
* Returns the Device Tree (DT) version.
128+
* @return the Device Tree (DT) version
129+
*/
130+
public int getVersion() {
131+
return version;
132+
}
133+
134+
/**
135+
* Returns the Device Tree (DT) entries.
136+
* @return the Device Tree (DT) entries
137+
*/
138+
public List<DtTableEntry> getEntries() {
139+
return _entries;
140+
}
141+
142+
@Override
143+
public DataType toDataType() throws DuplicateNameException, IOException {
144+
return StructConverterUtil.toDataType(this);
145+
}
146+
}

0 commit comments

Comments
 (0)