dotfiles/.local/bin/fbpack.py

86 lines
2.4 KiB
Python
Raw Normal View History

# Copyright 2021 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import struct
from packedstruct import PackedStruct
from collections import OrderedDict
FBPACK_MAGIC = 0x4b504246 # "FBPK" FastBook PacK
FBPACK_VERSION = 2
FBPACK_DEFAULT_DATA_ALIGN = 16
FBPACK_PARTITION_TABLE = 0
FBPACK_PARTITION_DATA = 1
FBPACK_SIDELOAD_DATA = 2
class PackEntry(PackedStruct):
"""Pack entry info."""
_FIELDS = OrderedDict([
('type', 'I'),
('name', '36s'),
('product', '40s'),
('offset', 'Q'),
('size', 'Q'),
('slotted', 'I'),
('crc32', 'I'),
])
def __init__(self, type_=0, name=b'', prod=b'', offset=0, size=0, slotted=0, crc32=0):
super(PackEntry, self).__init__(type_, name, prod, offset, size, slotted, crc32)
class PackHeader(PackedStruct):
""" A packed image representation """
_FIELDS = OrderedDict([
('magic', 'I'),
('version', 'I'),
('header_size', 'I'),
('entry_header_size', 'I'),
('platform', '16s'),
('pack_version', '64s'),
('slot_type', 'I'),
('data_align', 'I'),
('total_entries', 'I'),
('total_size', 'I'),
])
def __init__(
self,
magic=FBPACK_MAGIC,
version=FBPACK_VERSION,
header_size=0,
entry_header_size=len(PackEntry()),
platform=b'',
pack_version=b'',
slot_type=0,
data_align=FBPACK_DEFAULT_DATA_ALIGN,
total_entries=0,
total_size=0):
super(PackHeader, self).__init__(
magic,
version,
header_size,
entry_header_size,
platform,
pack_version,
slot_type,
data_align,
total_entries,
total_size)
# update header size once we know all fields
self.header_size = len(self)