1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4# Copyright (c) 2022 Huawei Device Co., Ltd. 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16""" 17Description : Unpack updater package 18""" 19import os 20import struct 21import time 22from create_update_package import UPGRADE_FILE_HEADER_LEN 23from create_update_package import UPGRADE_COMPINFO_SIZE 24from create_update_package import UPGRADE_COMPINFO_SIZE_L2 25from create_update_package import COMPONENT_ADDR_SIZE 26from create_update_package import COMPONENT_ADDR_SIZE_L2 27from create_update_package import UPGRADE_RESERVE_LEN 28from create_update_package import UPGRADE_SIGNATURE_LEN 29 30from create_hashdata import HASH_TYPE_SIZE 31from create_hashdata import HASH_LENGTH_SIZE 32from create_hashdata import HASH_TLV_SIZE 33from create_hashdata import UPGRADE_HASHINFO_SIZE 34from create_hashdata import CreateHash 35from create_hashdata import HashType 36 37from log_exception import UPDATE_LOGGER 38from utils import OPTIONS_MANAGER 39 40COMPINFO_LEN_OFFSET = 178 41COMPINFO_LEN_SIZE = 2 42COMPONENT_ADDR_OFFSET = UPGRADE_FILE_HEADER_LEN 43COMPONENT_TYPE_OFFSET = COMPONENT_ADDR_OFFSET + COMPONENT_ADDR_SIZE + 4 44COMPONENT_TYPE_OFFSET_L2 = COMPONENT_ADDR_OFFSET + COMPONENT_ADDR_SIZE_L2 + 4 45COMPONENT_TYPE_SIZE = 1 46COMPONENT_SIZE_OFFSET = 11 47COMPONENT_SIZE_SIZE = 4 48 49""" 50Format 51H: unsigned short 52I: unsigned int 53B: unsigned char 54s: char[] 55""" 56COMPINFO_LEN_FMT = "H" 57COMPONENT_TYPE_FMT = "B" 58COMPONENT_SIZE_FMT = "I" 59 60 61class UnpackPackage(object): 62 """ 63 Unpack the update.bin file 64 """ 65 66 def __init__(self): 67 self.count = 0 68 self.component_offset = 0 69 self.save_path = None 70 71 if OPTIONS_MANAGER.not_l2: 72 self.compinfo_size = UPGRADE_COMPINFO_SIZE 73 self.type_offset = COMPONENT_TYPE_OFFSET 74 self.addr_size = COMPONENT_ADDR_SIZE 75 else: 76 self.compinfo_size = UPGRADE_COMPINFO_SIZE_L2 77 self.type_offset = COMPONENT_TYPE_OFFSET_L2 78 self.addr_size = COMPONENT_ADDR_SIZE_L2 79 80 self.addr_offset = COMPONENT_ADDR_OFFSET 81 self.size_offset = self.type_offset + COMPONENT_SIZE_OFFSET 82 83 def check_args(self): 84 if not os.access(OPTIONS_MANAGER.unpack_package_path, os.R_OK) and \ 85 not os.path.exists(self.save_path): 86 UPDATE_LOGGER.print_log( 87 "Access unpack_package_path fail! path: %s" % \ 88 OPTIONS_MANAGER.unpack_package_path, UPDATE_LOGGER.ERROR_LOG) 89 return False 90 return True 91 92 def parse_package_file(self, package_file): 93 try: 94 package_file.seek(COMPINFO_LEN_OFFSET) 95 compinfo_len_buffer = package_file.read(COMPINFO_LEN_SIZE) 96 compinfo_len = struct.unpack(COMPINFO_LEN_FMT, compinfo_len_buffer) 97 except (struct.error, IOError): 98 return False 99 100 self.count = compinfo_len[0] // self.compinfo_size 101 self.component_offset = \ 102 UPGRADE_FILE_HEADER_LEN + compinfo_len[0] + \ 103 UPGRADE_RESERVE_LEN + UPGRADE_SIGNATURE_LEN 104 UPDATE_LOGGER.print_log( 105 "parse package file success! components: %d" % self.count) 106 return True 107 108 def parse_component(self, package_file): 109 try: 110 package_file.seek(self.addr_offset) 111 component_addr = package_file.read(self.addr_size) 112 component_addr = component_addr.split(b"\x00")[0].decode('utf-8') 113 114 package_file.seek(self.type_offset) 115 component_type_buffer = package_file.read(COMPONENT_TYPE_SIZE) 116 component_type = struct.unpack(COMPONENT_TYPE_FMT, component_type_buffer) 117 118 package_file.seek(self.size_offset) 119 component_size_buffer = package_file.read(COMPONENT_SIZE_SIZE) 120 component_size = struct.unpack(COMPONENT_SIZE_FMT, component_size_buffer) 121 except (struct.error, IOError): 122 UPDATE_LOGGER.print_log( 123 "parse component failed!", UPDATE_LOGGER.ERROR_LOG) 124 return False, False, False 125 126 return component_addr, component_type[0], component_size[0] 127 128 def create_image_file(self, package_file): 129 component_name, component_type, component_size = \ 130 self.parse_component(package_file) 131 if component_name is None and component_type is None and component_size is None: 132 UPDATE_LOGGER.print_log( 133 "get component_info failed!", UPDATE_LOGGER.ERROR_LOG) 134 return False 135 component_name = component_name.strip('/') 136 if component_name == "version_list": 137 component_name = "VERSION.mbn" 138 elif component_name == "board_list": 139 component_name = "BOARD.list" 140 elif component_type == 0: 141 component_name = ''.join([component_name, '.img']) 142 elif component_type == 1: 143 component_name = ''.join([component_name, '.zip']) 144 145 image_file_path = os.path.join(self.save_path, component_name) 146 147 package_file.seek(self.component_offset) 148 149 UPDATE_LOGGER.print_log("component name: %s" % component_name) 150 UPDATE_LOGGER.print_log("component offset: %s" % self.component_offset) 151 UPDATE_LOGGER.print_log("component size: %s" % component_size) 152 153 image_fd = os.open(image_file_path, os.O_RDWR | os.O_CREAT | os.O_TRUNC, 0o755) 154 with os.fdopen(image_fd, "wb") as image_file: 155 image_buffer = package_file.read(component_size) 156 image_file.write(image_buffer) 157 158 self.addr_offset += self.compinfo_size 159 self.type_offset += self.compinfo_size 160 self.size_offset += self.compinfo_size 161 self.component_offset += component_size 162 UPDATE_LOGGER.print_log("Create file: %s" % image_file_path) 163 return True 164 165 def unpack_package(self): 166 """ 167 Unpack the update.bin file 168 return: result 169 """ 170 UPDATE_LOGGER.print_log( 171 "Start unpack updater package: %s" % OPTIONS_MANAGER.unpack_package_path) 172 filename = ''.join(['unpack_result_', time.strftime("%H%M%S", time.localtime())]) 173 self.save_path = os.path.join(OPTIONS_MANAGER.target_package, filename) 174 os.makedirs(self.save_path) 175 176 if not self.check_args(): 177 UPDATE_LOGGER.print_log( 178 "check args failed!", UPDATE_LOGGER.ERROR_LOG) 179 return False 180 181 with open(OPTIONS_MANAGER.unpack_package_path, "rb") as package_file: 182 if not self.parse_package_file(package_file): 183 UPDATE_LOGGER.print_log( 184 "parse package file failed!", UPDATE_LOGGER.ERROR_LOG) 185 return False 186 187 for image_id in range(0, self.count): 188 UPDATE_LOGGER.print_log("Start to parse component_%d" % image_id) 189 if not self.create_image_file(package_file): 190 UPDATE_LOGGER.print_log( 191 "create image file failed!", UPDATE_LOGGER.ERROR_LOG) 192 return False 193 return True 194 195