diff options
author | Kelvin Zhang <zhangkelvin@google.com> | 2020-08-04 10:32:59 -0400 |
---|---|---|
committer | Kelvin Zhang <zhangkelvin@google.com> | 2020-08-06 20:07:47 +0000 |
commit | aba70abe81618542044dc20907f281a56b8b500e (patch) | |
tree | dc310775f553d2e7b78a72f84ee1472f015451c9 /scripts/update_device.py | |
parent | f6f75c208f3fa34d5cd38bdaf473dc36ef95ac8b (diff) |
Improve payload magic header handling
Currently, we use central directory's extra fields and filenames to
determine starting position of a zipentry's file data. However, central
directory's extra field might differ from extra field in local file
header. For example, the Extended-Timestamp field has different formats
depending on whether it's in local file header or central directory. We
should use local file header for computing offsets.
Test: Serve an OTA by update_device.py
Change-Id: I00d150d874b9c874bb713569ea14938e036f854e
Diffstat (limited to 'scripts/update_device.py')
-rwxr-xr-x | scripts/update_device.py | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/scripts/update_device.py b/scripts/update_device.py index 7be3edbf..1cd4b6ae 100755 --- a/scripts/update_device.py +++ b/scripts/update_device.py @@ -20,12 +20,14 @@ from __future__ import absolute_import import argparse +import binascii import hashlib import logging import os import socket import subprocess import sys +import struct import threading import xml.etree.ElementTree import zipfile @@ -89,6 +91,7 @@ class AndroidOTAPackage(object): OTA_PAYLOAD_PROPERTIES_TXT = 'payload_properties.txt' SECONDARY_OTA_PAYLOAD_BIN = 'secondary/payload.bin' SECONDARY_OTA_PAYLOAD_PROPERTIES_TXT = 'secondary/payload_properties.txt' + PAYLOAD_MAGIC_HEADER = b'CrAU' def __init__(self, otafilename, secondary_payload=False): self.otafilename = otafilename @@ -97,10 +100,34 @@ class AndroidOTAPackage(object): payload_entry = (self.SECONDARY_OTA_PAYLOAD_BIN if secondary_payload else self.OTA_PAYLOAD_BIN) payload_info = otazip.getinfo(payload_entry) - self.offset = payload_info.header_offset - self.offset += zipfile.sizeFileHeader - self.offset += len(payload_info.extra) + len(payload_info.filename) - self.size = payload_info.file_size + + if payload_info.compress_type != 0: + logging.error( + "Expected layload to be uncompressed, got compression method %d", + payload_info.compress_type) + # Don't use len(payload_info.extra). Because that returns size of extra + # fields in central directory. We need to look at local file directory, + # as these two might have different sizes. + with open(otafilename, "rb") as fp: + fp.seek(payload_info.header_offset) + data = fp.read(zipfile.sizeFileHeader) + fheader = struct.unpack(zipfile.structFileHeader, data) + # Last two fields of local file header are filename length and + # extra length + filename_len = fheader[-2] + extra_len = fheader[-1] + self.offset = payload_info.header_offset + self.offset += zipfile.sizeFileHeader + self.offset += filename_len + extra_len + self.size = payload_info.file_size + fp.seek(self.offset) + payload_header = fp.read(4) + if payload_header != self.PAYLOAD_MAGIC_HEADER: + logging.warning( + "Invalid header, expeted %s, got %s." + "Either the offset is not correct, or payload is corrupted", + binascii.hexlify(self.PAYLOAD_MAGIC_HEADER), + payload_header) property_entry = (self.SECONDARY_OTA_PAYLOAD_PROPERTIES_TXT if secondary_payload else self.OTA_PAYLOAD_PROPERTIES_TXT) |