Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 1 | # Copyright 2021 The Android Open Source Project |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | # |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 15 | import collections |
| 16 | |
| 17 | #from google3.third_party.devsite.androidsource.en.docs.core.architecture.bootloader.tools.pixel.fw_unpack import packedstruct |
| 18 | import packedstruct |
Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 19 | |
| 20 | FBPACK_MAGIC = 0x4b504246 # "FBPK" FastBook PacK |
| 21 | FBPACK_VERSION = 2 |
| 22 | FBPACK_DEFAULT_DATA_ALIGN = 16 |
| 23 | |
| 24 | FBPACK_PARTITION_TABLE = 0 |
| 25 | FBPACK_PARTITION_DATA = 1 |
| 26 | FBPACK_SIDELOAD_DATA = 2 |
| 27 | |
| 28 | |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 29 | class PackEntry(packedstruct.PackedStruct): |
| 30 | """Pack entry info.""" |
Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 31 | |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 32 | type: int |
| 33 | name: bytes |
| 34 | product: bytes |
| 35 | offset: int |
| 36 | size: int |
| 37 | slotted: int |
| 38 | crc32: int |
| 39 | _FIELDS = collections.OrderedDict([ |
| 40 | ('type', 'I'), |
| 41 | ('name', '36s'), |
| 42 | ('product', '40s'), |
| 43 | ('offset', 'Q'), |
| 44 | ('size', 'Q'), |
| 45 | ('slotted', 'I'), |
| 46 | ('crc32', 'I'), |
| 47 | ]) |
Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 48 | |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 49 | # Provide defaults. |
| 50 | # pylint: disable=useless-super-delegation |
| 51 | def __init__(self, |
| 52 | type_=0, |
| 53 | name=b'', |
| 54 | prod=b'', |
| 55 | offset=0, |
| 56 | size=0, |
| 57 | slotted=0, |
| 58 | crc32=0): |
| 59 | super(PackEntry, self).__init__(type_, name, prod, offset, size, slotted, |
| 60 | crc32) |
Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 61 | |
| 62 | |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 63 | class PackHeader(packedstruct.PackedStruct): |
| 64 | """ A packed image representation""" |
Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 65 | |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 66 | magic: int |
| 67 | version: int |
| 68 | header_size: int |
| 69 | entry_header_size: int |
| 70 | platform: bytes |
| 71 | pack_version: bytes |
| 72 | slot_type: int |
| 73 | data_align: int |
| 74 | total_entries: int |
| 75 | total_size: int |
| 76 | _FIELDS = collections.OrderedDict([ |
| 77 | ('magic', 'I'), |
| 78 | ('version', 'I'), |
| 79 | ('header_size', 'I'), |
| 80 | ('entry_header_size', 'I'), |
| 81 | ('platform', '16s'), |
| 82 | ('pack_version', '64s'), |
| 83 | ('slot_type', 'I'), |
| 84 | ('data_align', 'I'), |
| 85 | ('total_entries', 'I'), |
| 86 | ('total_size', 'I'), |
| 87 | ]) |
Chirayu Desai | 7119eb6 | 2021-12-07 03:07:13 +0530 | [diff] [blame] | 88 | |
Michael Bestas | 0382f30 | 2023-06-12 02:22:51 +0300 | [diff] [blame] | 89 | def __init__(self, |
| 90 | magic=FBPACK_MAGIC, |
| 91 | version=FBPACK_VERSION, |
| 92 | header_size=0, |
| 93 | entry_header_size=len(PackEntry()), |
| 94 | platform=b'', |
| 95 | pack_version=b'', |
| 96 | slot_type=0, |
| 97 | data_align=FBPACK_DEFAULT_DATA_ALIGN, |
| 98 | total_entries=0, |
| 99 | total_size=0): |
| 100 | super(PackHeader, |
| 101 | self).__init__(magic, version, header_size, entry_header_size, |
| 102 | platform, pack_version, slot_type, data_align, |
| 103 | total_entries, total_size) |
| 104 | # update header size once we know all fields |
| 105 | self.header_size = len(self) |