Files
mdm/src/main/java/com/midm/model/file/OtaFile.java
T

785 lines
24 KiB
Java

package com.midm.model.file;
import com.google.common.base.Strings;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.midm.model.org.User;
import com.mitac.cell.db.DbObject;
import com.mitac.cell.io.McFile;
import com.mitac.cell.security.McOtaCert;
import com.mitac.cell.utils.McJsonUtils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import org.apache.commons.io.IOUtils;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* ======================================================================================
* Android / 嵌入式系统 OTA 固件镜像模型 (OtaFile)
* ======================================================================================
*
* <p>【IoT 固件空中升级 (FOTA) 核心实现】
* 1. 业务功能:
* - 管理受控终端的完整系统固件镜像(System Image / Full OTA)与区域增量镜像(Region Image / Incremental OTA)。
* 2. 【固件安全与包结构解析】:
* - 自动解包并解析 Android Recovery 规范的标准固件 ZIP 包结构:
* - `META-INF/com/google/android/updater-script`: 固件升级执行脚本;
* - `system/build.prop`: 提取目标硬件型号、Android 操作系统版本、编译日期(build_date)、基带版本等;
* - `META-INF/com/android/otacert`: 嵌入式固件发布签名证书,校验固件是否由官方私钥签发;
* - `META-INF/com/android/metadata`: 提取差分升级的前置版本与兼容约束。
* 3. 硬件兼容性矩阵(SKU Matching):
* - 严格匹配终端出厂 SKU,防止跨机型错刷固件导致设备“变砖”(Bricking)。
*
* @author MiDM Core Platform Team
*/
@Document
public class OtaFile extends BaseFile implements Comparable {
/** Android Recovery 固件升级脚本入口 */
private static final String ENTRY_META_INF = "META-INF/com/";
private static final String ENTRY_UPDATE_SCRIPT = "META-INF/com/google/android/updater-script";
/** Android 操作系统属性字典路径(包含型号、Build版本、指纹等关键参数) */
private static final String ENTRY_BUILD_PROP = "system/build.prop";
/** 固件合法性验签 X.509 官方证书路径 */
private static final String ENTRY_OTA_CERT = "META-INF/com/android/otacert";
/** OTA 元数据配置文件(版本区间、差分基线版本等) */
private static final String ENTRY_METADATA = "META-INF/com/android/metadata";
private static final String ENTRY_UPDATE_INFO = "META-INF/com/android/update_info";
/** 解析固件包后提取的硬件规格与构建元数据字典 */
private DbObject otaInfo;
public OtaFile() {
}
public OtaFile(User user, UUID fileUuid, McFile file) {
super(user, fileUuid, file);
this.setOtaInfo(file);
}
public boolean isOtaImage() {
if (Strings.isNullOrEmpty(this.getProductId()) && Strings.isNullOrEmpty(this.getDeviceId())) {
return false;
} else {
return this.isRegionImage() ? true : !Strings.isNullOrEmpty(this.getSkuId());
}
}
public DbObject getOtaInfo() {
if (this.otaInfo == null) {
this.otaInfo = new DbObject();
}
return this.otaInfo;
}
public void setOtaInfo(DbObject otaInfo) {
this.otaInfo = otaInfo;
}
public void setOtaInfo(String key, Object value) {
this.getOtaInfo().append(key, value);
}
public void setOtaInfo(McFile file) {
try {
this.retrieveBuildInfo(new ZipFile(file));
} catch (Exception var3) {
}
}
public String getType() {
return this.getOtaInfo().getString("type", "system");
}
public boolean isSystemImage() {
return this.getType().equals("system");
}
public boolean isRegionImage() {
return this.getType().equals("region");
}
public boolean isModemImage() {
return this.getType().equals("modem");
}
public boolean isAbUpdate() {
return this.getOtaInfo().getBoolean("abUpdate", false);
}
public boolean isPartial() {
return this.getOtaInfo().containsKey("preBuild");
}
public void setType(String type) {
if (type.toLowerCase().equals("base")) {
type = "system";
}
this.getOtaInfo().put("type", type);
}
public void setAbUpdate(boolean abUpdate) {
this.getOtaInfo().put("abUpdate", abUpdate);
}
public long getBuildDate() {
return this.getOtaInfo().getLong("buildDate", -1L);
}
public void setBuildDate(long date) {
this.setOtaInfo("buildDate", date);
}
public int getSdkLevel() {
return this.getOtaInfo().getInteger("sdk", -1);
}
public void setSdkLevel(int level) {
this.setOtaInfo("sdk", level);
}
public long getPostBuildDate() {
return this.getOtaInfo().getLong("postBuildDate", -1L);
}
public void setPostBuildDate(long date) {
this.setOtaInfo("postBuildDate", date);
}
public String getProductId() {
return this.getOtaInfo().getString("productId");
}
public void setProductId(String productId) {
this.setOtaInfo("productId", productId);
}
public String getTargetProduct() {
return this.getOtaInfo().getString("targetProduct");
}
public void setTargetProduct(String productId) {
this.setOtaInfo("targetProduct", productId);
}
public String getSkuId() {
return this.getOtaInfo().getString("skuId");
}
public void setSkuId(String skuId) {
this.setOtaInfo("skuId", skuId);
}
public boolean hasCompatibleSkuIds() {
List<String> ids = this.getOtaInfo().getList("compatibleSkuIds", String.class);
return ids != null && ids.size() > 0;
}
public List getCompatibleSkuIds() {
return this.getOtaInfo().getList("compatibleSkuIds", String.class);
}
public boolean isCompatibleSkuId(String skuId) {
if (Strings.isNullOrEmpty(skuId)) {
return false;
} else {
String curSkuId = this.getSkuId();
if (skuId.equals(curSkuId)) {
return true;
} else {
List<String> compatibleSkuIds = this.getCompatibleSkuIds();
return compatibleSkuIds != null && compatibleSkuIds.contains(skuId);
}
}
}
public void setCompatibleSkuIds(String skuIds) {
if (!Strings.isNullOrEmpty(skuIds)) {
String[] ids = skuIds.split("\\s*,\\s*");
if (ids != null && ids.length > 0) {
this.setOtaInfo("compatibleSkuIds", Arrays.asList(ids));
}
}
}
public String getDeviceId() {
return this.getOtaInfo().getString("deviceId");
}
public void setDeviceId(String deviceId) {
this.setOtaInfo("deviceId", deviceId);
}
public String getDisplayId() {
return this.getOtaInfo().getString("displayId");
}
public void setDisplayId(String displayId) {
this.setOtaInfo("displayId", displayId);
}
public void setProductDisplayId(String displayId) {
this.setOtaInfo("productDisplayId", displayId);
}
public String getPreDisplayId() {
return this.getOtaInfo().getString("preDisplayId");
}
public void setPreDisplayId(String displayId) {
this.setOtaInfo("preDisplayId", displayId);
}
public static String parseSkuId(String displayId) {
if (displayId != null) {
int startIndex = displayId.indexOf(46);
++startIndex;
startIndex = displayId.indexOf(46, startIndex);
if (startIndex > 0) {
int endIndex = displayId.indexOf(46, startIndex + 1);
if (endIndex > startIndex) {
++startIndex;
return displayId.substring(startIndex, endIndex);
}
}
}
return null;
}
public String getBrand() {
return this.getOtaInfo().getString("brand");
}
public void setBrand(String brand) {
this.setOtaInfo("brand", brand);
}
public String getCustomer() {
return this.getOtaInfo().getString("customer");
}
public void setCustomer(String customer) {
this.setOtaInfo("customer", customer);
}
public String getCustomerId() {
return this.getOtaInfo().getString("customerId");
}
public void setCustomerId(String customerId) {
this.setOtaInfo("customerId", customerId);
}
public String getPartNo() {
return this.getOtaInfo().getString("partNo");
}
public void setPartNo(String partNo) {
this.setOtaInfo("partNo", partNo);
}
public String getOsVersion() {
return this.getOtaInfo().getString("osVersion");
}
public void setOsVersion(String version) {
this.setOtaInfo("osVersion", version);
}
public String getScriptVersion() {
return this.getOtaInfo().getString("scriptVersion");
}
public void setScriptVersion(String partNo) {
this.setOtaInfo("scriptVersion", partNo);
}
public String getReleaseVersion() {
return this.getOtaInfo().getString("releaseVersion");
}
public void setReleaseVersion(String partNo) {
this.setOtaInfo("releaseVersion", partNo);
}
public String getMarketName1() {
return this.getOtaInfo().getString("marketName1");
}
public void setMarketName1(String name) {
this.setOtaInfo("marketName1", name);
}
public String getMarketName2() {
return this.getOtaInfo().getString("marketName2");
}
public void setMarketName2(String name) {
this.setOtaInfo("marketName2", name);
}
public String getSecurityPatchLevel() {
return this.getOtaInfo().getString("securityPatch");
}
public void setSecurityPatchLevel(String level) {
this.setOtaInfo("securityPatch", level);
}
public String getBuildType() {
return this.getOtaInfo().getString("buildType");
}
public void setBuildType(String buildType) {
this.setOtaInfo("buildType", buildType);
}
public String getBuildTags() {
return this.getOtaInfo().getString("buildTags");
}
public void setBuildTags(String buildTags) {
this.setOtaInfo("buildTags", buildTags);
}
public String getPreBuild() {
return this.getOtaInfo().getString("preBuild");
}
public void setPreBuild(String value) {
this.setOtaInfo("preBuild", value);
}
public String getOtaCertHash() {
return this.getOtaInfo().getString("otaCert");
}
public void setOtaCertHash(String hash) {
this.setOtaInfo("otaCert", hash);
}
public String getModemSku() {
return this.getOtaInfo().getString("modemSku");
}
public void setModemSku(String hash) {
this.setOtaInfo("modemSku", hash);
}
public String getModemFirmwareVersion() {
return this.getOtaInfo().getString("modemFirmwareVersion");
}
public void setModemFirmwareVersion(String hash) {
this.setOtaInfo("modemFirmwareVersion", hash);
}
public String getModemFirmwareLongVersion() {
return this.getOtaInfo().getString("modemFirmwareLongVersion");
}
public void setModemFirmwareLongVersion(String hash) {
this.setOtaInfo("modemFirmwareLongVersion", hash);
}
private void retrieveBuildInfo(ZipFile zipFile) {
try {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
ZipEntry bodyEntry = null;
boolean isAbImage = false;
while(entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry)entries.nextElement();
if (entry.getName().endsWith(".zip")) {
bodyEntry = entry;
} else if (entry.getName().endsWith(".sh")) {
isAbImage = true;
}
}
if (isAbImage && bodyEntry != null) {
InputStream input = zipFile.getInputStream(bodyEntry);
ZipInputStream zipBody;
ZipEntry entry;
for(zipBody = new ZipInputStream(input); (entry = zipBody.getNextEntry()) != null; zipBody.closeEntry()) {
String entryName = entry.getName();
if (entryName.equals("META-INF/com/android/metadata")) {
this.dispatchProp(zipBody);
} else if (entryName.equals("META-INF/com/android/update_info")) {
this.dispatchJson(zipBody);
} else if (entryName.equals("META-INF/com/android/otacert")) {
this.dispatchOtaCert(zipBody);
}
}
zipBody.close();
input.close();
} else {
ZipEntry udpateInfoEntry = zipFile.getEntry("META-INF/com/android/update_info");
if (udpateInfoEntry != null) {
this.dispatchJson(zipFile.getInputStream(udpateInfoEntry));
this.setType("region");
}
ZipEntry metaDataEntry = zipFile.getEntry("META-INF/com/android/metadata");
if (metaDataEntry != null) {
this.dispatchProp(zipFile.getInputStream(metaDataEntry));
}
ZipEntry updateScriptEntry = zipFile.getEntry("META-INF/com/google/android/updater-script");
if (updateScriptEntry != null) {
this.dispatchUpdaterScript(zipFile.getInputStream(updateScriptEntry));
}
ZipEntry otaCertEntry = zipFile.getEntry("META-INF/com/android/otacert");
if (otaCertEntry != null) {
InputStream otaInput = zipFile.getInputStream(otaCertEntry);
this.dispatchOtaCert(otaInput);
otaInput.close();
}
ZipEntry buildPropEntry = zipFile.getEntry("system/build.prop");
if (buildPropEntry != null) {
this.dispatchProp(zipFile.getInputStream(buildPropEntry));
}
zipFile.close();
if (!Strings.isNullOrEmpty(this.getDisplayId()) && this.isRegionImage()) {
this.setDisplayId(String.format("%s_%s_%s", this.getReleaseVersion(), this.getPartNo(), this.getMarketName2()));
}
if (Strings.isNullOrEmpty(this.getType())) {
this.setType("system");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void dispatchUpdaterScript(InputStream is) {
try {
String script = IOUtils.toString(is, "UTF-8");
String key = "ro.build.date.utc";
String buildDate = this.lastSubString(script, key, "(", ",");
this.setOtaProperty(key, buildDate);
key = "ro.product.device";
String value = this.subString(script, key, "\"", "\"");
if (!Strings.isNullOrEmpty(value)) {
this.setOtaProperty(key, value);
}
key = "ro.mitac.product";
value = this.subString(script, key, "\"", "\"");
if (!Strings.isNullOrEmpty(value)) {
this.setOtaProperty(key, value);
}
key = "ro.mitac.sku";
value = this.subString(script, key, "\"", "\"");
if (!Strings.isNullOrEmpty(value)) {
this.setOtaProperty(key, value);
}
if (Strings.isNullOrEmpty(this.getDeviceId())) {
value = this.subString(script, "get_device_compatible", "\"", "\"");
if (!Strings.isNullOrEmpty(value)) {
this.setDeviceId(value);
}
}
if (Strings.isNullOrEmpty(this.getBuildTags())) {
String fingerPrint = this.subString(script, "ui_print(\"Target", " ", "\"");
if (!Strings.isNullOrEmpty(fingerPrint)) {
String buildTags = fingerPrint.substring(fingerPrint.lastIndexOf(47) + 1);
if (!Strings.isNullOrEmpty(buildTags)) {
this.setBuildTags(buildTags);
}
}
}
} catch (IOException var8) {
}
}
private void dispatchProp(InputStream input) {
try {
Properties properties = new Properties();
properties.load(input);
for(Map.Entry entry : properties.entrySet()) {
this.setOtaProperty((String)entry.getKey(), (String)entry.getValue());
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void dispatchJson(InputStream input) {
if (input != null) {
JsonElement json = McJsonUtils.toJsonElement(input);
if (json != null && json.isJsonObject()) {
JsonObject jProps = json.getAsJsonObject();
for(Map.Entry entry : jProps.entrySet()) {
String key = (String)entry.getKey();
JsonElement value = (JsonElement)entry.getValue();
if (value.isJsonPrimitive()) {
this.setOtaProperty(key, value.getAsString());
}
}
}
}
}
private void dispatchOtaCert(InputStream otaCertIs) {
McOtaCert otaCert = new McOtaCert(otaCertIs);
this.setOtaCertHash(otaCert.getBase64Hash());
}
private void setOtaProperty(String key, String value) {
switch (key) {
case "ota-type":
this.setAbUpdate(value.equals("AB"));
break;
case "persist.sys.rgimg.brand":
if (Strings.isNullOrEmpty(this.getBrand())) {
this.setBrand(value);
}
break;
case "ro.build.date.utc":
case "post-timestamp":
case "persist.sys.rgimg.build_utc":
if (this.getBuildDate() < 0L) {
this.setBuildDate(Long.valueOf(value));
}
break;
case "persist.sys.rgimg.customer":
if (Strings.isNullOrEmpty(this.getCustomer())) {
this.setCustomer(value);
}
break;
case "persist.sys.rgimg.customer_id":
if (Strings.isNullOrEmpty(this.getCustomerId())) {
this.setCustomerId(value);
}
break;
case "persist.sys.rgimg.mkt_name1":
if (Strings.isNullOrEmpty(this.getMarketName1())) {
this.setMarketName1(value);
}
break;
case "persist.sys.rgimg.mkt_name2":
if (Strings.isNullOrEmpty(this.getMarketName2())) {
this.setMarketName2(value);
}
break;
case "persist.sys.rgimg.part_num":
if (Strings.isNullOrEmpty(this.getPartNo())) {
this.setPartNo(value);
}
break;
case "product-code":
case "ro.mitac.product":
case "persist.sys.rgimg.product":
if (Strings.isNullOrEmpty(this.getProductId())) {
this.setProductId(value);
}
break;
case "persist.sys.rgimg.release_ver":
if (Strings.isNullOrEmpty(this.getReleaseVersion())) {
this.setReleaseVersion(value);
}
break;
case "persist.sys.rgimg.script_ver":
if (Strings.isNullOrEmpty(this.getScriptVersion())) {
this.setScriptVersion(value);
}
break;
case "persist.sys.rgimg.target_device":
case "pre-device":
if (Strings.isNullOrEmpty(this.getDeviceId())) {
this.setDeviceId(value);
}
break;
case "persist.sys.rgimg.target_product":
if (Strings.isNullOrEmpty(this.getTargetProduct())) {
this.setTargetProduct(value);
}
break;
case "mitac-display-id":
String curDisplayId = this.getDisplayId();
this.setProductDisplayId(value);
if (Strings.isNullOrEmpty(curDisplayId)) {
this.setDisplayId(value);
}
if (Strings.isNullOrEmpty(this.getSkuId())) {
String skuId = parseSkuId(value);
if (!Strings.isNullOrEmpty(skuId)) {
this.setSkuId(skuId);
}
}
break;
case "display-id":
case "ro.build.display.id":
String curDisplayId = this.getDisplayId();
if (Strings.isNullOrEmpty(curDisplayId) || !curDisplayId.equals(value)) {
this.setDisplayId(value);
}
if (Strings.isNullOrEmpty(this.getSkuId())) {
String skuId = parseSkuId(value);
if (!Strings.isNullOrEmpty(skuId)) {
this.setSkuId(skuId);
}
}
break;
case "pre-display-id":
this.setPreDisplayId(value);
break;
case "mitac-package-type":
this.setType(value);
case "post-build-incremental":
default:
break;
case "post-sdk-level":
if (this.getSdkLevel() < 0) {
this.setSdkLevel(Integer.valueOf(value));
}
break;
case "post-security-patch-level":
if (Strings.isNullOrEmpty(this.getSecurityPatchLevel())) {
this.setSecurityPatchLevel(value);
}
break;
case "pre-build":
this.setPreBuild(value);
break;
case "post-build":
this.dispatchPostBuild(value);
break;
case "sku-id":
case "ro.mitac.sku":
this.setSkuId(value);
break;
case "base-sku-compatibility":
if (!this.hasCompatibleSkuIds()) {
this.setCompatibleSkuIds(value);
}
break;
case "ro.build.tags":
if (Strings.isNullOrEmpty(this.getBuildTags())) {
this.setBuildTags(value);
}
break;
case "modem-firmware-sku":
if (Strings.isNullOrEmpty(this.getModemSku())) {
this.setModemSku(value);
}
break;
case "modem-firmware-version":
if (Strings.isNullOrEmpty(this.getModemFirmwareVersion())) {
this.setModemFirmwareVersion(value);
}
break;
case "modem-firmware-long-version":
if (Strings.isNullOrEmpty(this.getModemFirmwareLongVersion())) {
this.setModemFirmwareLongVersion(value);
}
}
}
public String getPostBuild() {
return this.getOtaInfo().getString("postBuild");
}
private void dispatchPostBuild(String text) {
this.setOtaInfo("postBuild", text);
String[] tags = text.split(":");
if (tags.length >= 3) {
String[] tokens = tags[1].split("/");
if (Strings.isNullOrEmpty(this.getOsVersion())) {
this.setOsVersion(tokens[0]);
}
tokens = tags[2].split("/");
if (Strings.isNullOrEmpty(this.getBuildType())) {
this.setBuildType(tokens[0]);
}
if (Strings.isNullOrEmpty(this.getBuildTags())) {
this.setBuildTags(tokens[1]);
}
}
}
private String subString(String sb, String key, String prefix, String suffix) {
int keyIndex = sb.indexOf(key);
if (keyIndex < 0) {
return null;
} else {
int startIndex = sb.indexOf(prefix, keyIndex + key.length() + 1) + 1;
int endIndex = sb.indexOf(suffix, startIndex + 1);
return startIndex < endIndex ? sb.substring(startIndex, endIndex) : "";
}
}
private String lastSubString(String sb, String key, String prefix, String suffix) {
int keyIndex = sb.indexOf(key);
int endIndex = sb.lastIndexOf(",", keyIndex);
int startIndex = sb.lastIndexOf("(", endIndex) + 1;
return startIndex < endIndex ? sb.substring(startIndex, endIndex) : "";
}
public int compareTo(OtaFile other) {
int result = this.getSkuId().compareTo(other.getSkuId());
if (result == 0) {
boolean isThisSystemImage = this.isSystemImage();
boolean isOtherSystemImage = other.isSystemImage();
if (isThisSystemImage != isOtherSystemImage) {
if (isThisSystemImage) {
return -1;
}
return 1;
}
result = (int)(this.getBuildDate() - other.getBuildDate());
if (result == 0) {
if (this.isPartial() && !other.isPartial()) {
return -1;
}
if (!this.isPartial() && other.isPartial()) {
return 1;
}
}
}
return result;
}
}