Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 系統更新防互刷功能的實作與原理分析

編輯:關於 Android 程式設計

場景描述

一個專案因為硬體差異(例如電池容量不同、鏡頭解析度不同)產生了兩個版本,共用同一套代碼基線。問題在於,兩者的 OTA 更新包外觀相似,一旦刷錯,設備可能直接無法開機。因此需要一套「防互刷」機制,讓 A 版本的更新包拒絕安裝在 B 版本的機器上。

讀者可能會問:既然容易混淆,直接拆成兩個專案不就解決了?確實可以,而且這是最快的方案。本文討論的是「必須維持單一專案」這個前提下的做法。

第一步:理解校驗機制

熟悉 ota_from_target_files、edify_generator.py,或讀過 install.c 與 updater-script 執行流程的人會知道,更新包在安裝之初就會對一批屬性做校驗。以下是腳本產生器中幾個關鍵函式。

校驗時間戳:

def __AssertOlderBuild(self, timestamp, timestamp_text):
    self.script.append(
        ('(!less_than_int(%s, getprop("ro.build.date.utc"))) || '
         'abort("Can\'t install this package (%s) over newer '
         'build (" + getprop("ro.build.date") + ").");'
        ) % (timestamp, timestamp_text))

這個函式本身只產生一行 edify 語句,實際寫進 updater-script 的長這樣:

(!less_than_int(時間戳, getprop("ro.build.date.utc"))) || abort(...);

前半段為真就繼續,否則執行 abort 中斷。也就是說,若更新包的打包時間比機器上 Build 的時間還舊,安裝就會被拒絕。

校驗裝置資訊:

def __AssertDevice(self, device):
    cmd = ('getprop("ro.product.device") == "%s" || '
           'abort("This package is for \\"%s\\" devices; '
           'this is a \\"" + getprop("ro.product.device") + "\\".");'
          ) % (device, device)
    self.script.append(cmd)

更新包內記錄的裝置型號若與機器不一致,同樣中斷。

校驗系統指紋:

def __AssertSomeFingerprint(self, *fp):
    cmd = (' ||\n '.join([
        ('file_getprop("/system/build.prop", "ro.build.fingerprint") == "%s"')
        % i for i in fp]) +
        ' ||\n abort("Package expects build fingerprint of %s; this '
        'device has " + getprop("ro.build.fingerprint") + ".");'
        ) % (" or ".join(fp),)
    self.script.append(cmd)

新舊版本的 fingerprint 與機器現況不符時,一樣拒絕安裝。

這三項數值都取自 build.prop。其中 fingerprint(系統指紋)值得多說兩句:它是每個專案每個版本獨一無二的識別字,Google 要求前後不得有空格或斷行,長度不超過 91 個字元。可以透過 adb 下 getprop ro.build.fingerprint 查看,格式定義如下:

BUILD_FINGERPRINT := $(PRODUCT_BRAND)/$(TARGET_PRODUCT)/$(TARGET_DEVICE):$(PLATFORM_VERSION)/$(BUILD_ID)/$(BUILD_NUMBER):$(TARGET_BUILD_VARIANT)/$(BUILD_VERSION_TAGS)

例如:

Google/Y100-T00/GoogleY100-T:4.2.2/GoogleY100-T00/CHSC01B001:user/release-keys

各欄位對應的 getprop 項目:PRODUCT_BRAND 對應 ro.product.brand;TARGET_PRODUCT 對應 ro.product.name;TARGET_DEVICE 對應 ro.product.device;BUILD_ID 對應 ro.build.id;version.incremental 對應 ro.build.version.incremental(需為真實的內部版本號);VERSION.RELEASE 對應 ro.build.version.release,一般即 Android 版本號。此外 TYPE 必須是 user 而非 eng,TAGS 必須是 release-keys 而非 test-keys;若該版本要支援 OTA,則需採用 ota-rel-keys。

釐清之後可以發現:fingerprint 主要在差分升級時才會被校驗,而裝置資訊與簽名對同一個產品而言通常不可變動。既然如此,思路就清楚了——只要新增一個自有的系統屬性來標識版本,就能達到防互刷的效果。

第二步:加入校驗標識

先在 system.prop 的最後一行加上自訂屬性(路徑依平台與公司而異,本文位於 vendor/{專案}/{分支}/config/system.prop):

ro.update.version=a1

接著在 edify_generator.py 中定義對應的校驗方法:

def AssertUpdateVersion(self, update_version):
    """Assert that the update_version identifier is the given string."""
    cmd = ('getprop("ro.update.version") == "%s" || '
           'abort("This package is for \\"%s\\" update_version; '
           'this is a \\"" + getprop("ro.update.version") + "\\".");'
          ) % (update_version, update_version)
    self.script.append(cmd)

然後在 ota_from_target_file 的 AppendAssertions 中掛上去:

def AppendAssertions(script, info_dict):
    device = GetBuildProp("ro.product.device", info_dict)
    script.AssertDevice(device)
    update_version = GetBuildProp("ro.update.version", info_dict)
    script.AssertUpdateVersion(update_version)

最後修改 WriteFullOTAPackage(),把新屬性寫進 metadata:

metadata = {
    "post-build": GetBuildProp("ro.build.fingerprint", OPTIONS.info_dict),
    "pre-device": GetBuildProp("ro.product.device", OPTIONS.info_dict),
    "post-timestamp": GetBuildProp("ro.build.date.utc", OPTIONS.info_dict),
    "update-version": GetBuildProp("ro.update.version", OPTIONS.info_dict),
}

第三步:驗證

用 ro.update.version 為 a1 的版本產出整包,然後在屬性為 a2 的機器上以 SD 卡整包升級。若機制生效,安裝會被拒絕並留下類似以下的日誌:

script aborted: This package is for "a1" update_version; this is a "a2".
E:Error in /sdcard/dload/update.zip (Status 7)
Installation aborted.

對應的 updater-script 內容就是我們插入的那一行:

getprop("ro.update.version") == "a1" || abort("This package is for \"a1\" update_version; this is a \"" + getprop("ro.update.version") + "\".");

一個千萬別偷懶的地方

驗證時請務必重新產包裝機,不要直接改更新包裡的 build.prop。build.prop 已被簽名涵蓋,事後竄改會導致簽名校驗失敗,你看到的錯誤訊息會是簽名不符,而非你要驗證的 update-version 不符——症狀看似類似,原因卻完全不同,很容易誤判成機制沒生效。

這套做法的價值在於成本低、擴充容易。同一個專案若有 N 個硬體變體,只要維護 system.prop 中的一組屬性,不必複製 N 份代碼基線,也不必更動既有的 fingerprint 與簽名流程。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved