|
| 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