blob: 5fc7cc9989bfeff96e784e6ccdbb7abbd11ab515 [file] [log] [blame]
Chirayu Desai7119eb62021-12-07 03:07:13 +05301# 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 Bestas0382f302023-06-12 02:22:51 +030015import collections
16
17#from google3.third_party.devsite.androidsource.en.docs.core.architecture.bootloader.tools.pixel.fw_unpack import packedstruct
18import packedstruct
Chirayu Desai7119eb62021-12-07 03:07:13 +053019
20FBPACK_MAGIC = 0x4b504246 # "FBPK" FastBook PacK
21FBPACK_VERSION = 2
22FBPACK_DEFAULT_DATA_ALIGN = 16
23
24FBPACK_PARTITION_TABLE = 0
25FBPACK_PARTITION_DATA = 1
26FBPACK_SIDELOAD_DATA = 2
27
28
Michael Bestas0382f302023-06-12 02:22:51 +030029class PackEntry(packedstruct.PackedStruct):
30 """Pack entry info."""
Chirayu Desai7119eb62021-12-07 03:07:13 +053031
Michael Bestas0382f302023-06-12 02:22:51 +030032 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 Desai7119eb62021-12-07 03:07:13 +053048
Michael Bestas0382f302023-06-12 02:22:51 +030049 # 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 Desai7119eb62021-12-07 03:07:13 +053061
62
Michael Bestas0382f302023-06-12 02:22:51 +030063class PackHeader(packedstruct.PackedStruct):
64 """ A packed image representation"""
Chirayu Desai7119eb62021-12-07 03:07:13 +053065
Michael Bestas0382f302023-06-12 02:22:51 +030066 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 Desai7119eb62021-12-07 03:07:13 +053088
Michael Bestas0382f302023-06-12 02:22:51 +030089 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)