本頁概述一種方法 (可信度最高),可確保您在 Pixel 裝置上執行合法的 Pixel 映像檔版本。這包括將原廠映像檔下載到電腦,並在下載的構件上執行記錄納入證明,然後再將其刷入裝置。
如何驗證 Pixel 原廠映像檔
通過認證的必要條件
在驗證構件 (韌體或 APK) 是否符合聲明前,請先確認網路連線電腦上已備妥下列工具。視驗證目標而定,您可能不需要全部提供。
Golang
驗證工具是以 Go 語言編寫。如要建構這項工具,請從 Go 網站安裝 Go 1.24 以上版本。
Android Debug Bridge (ADB)
ADB 是一種工具,可與 Android 裝置通訊,檢查映像檔或擷取 APK,可在 Android SDK 平台工具網站取得。
納入證明驗證工具
納入證明驗證工具是 Go 模組,可查詢透明度記錄,並輸出構件是否包含在記錄中。發布於 android-binary-transparency GitHub 存放區,網址為 https://github.com/android/android-binary-transparency/tree/main
如要複製 AOSP avb 存放區,請執行下列指令:
computer:~$ git clone https://android.googlesource.com/platform/external/avb
如要複製 android-binary-transparency GitHub 存放區,請按照下列步驟操作:
computer:~$ git clone https://github.com/android/android-binary-transparency
AAPT2
AAPT2 (Android 資產封裝工具) 是一種建構工具,用於編譯及封裝 Android 應用程式的資源。在 Android SDK Build Tools 26.0.2 以上版本中,您可以找到這個獨立工具。
bundletool
bundletool 是用於建構 Android App Bundle (AAB) 的工具。也可以用來驗證 APK 程式碼簽章。您可以從 GitHub 下載。
wget 或 curl
wget 和 curl 是可用於從網際網路擷取或下載檔案的工具。請務必安裝其中一項工具,因為驗證指令碼可能會依賴這些工具。
Avbtool
avbtool 用於計算原廠映像檔的 VBMeta 摘要,以及驗證二進位檔。複製 avb 存放區後 (如「納入證明驗證器」一節所示),avbtool.py 會位於複製的 avb 目錄中。您應將這個目錄新增至 $PATH 環境變數。
computer:~$ PATH=$PATH:DIRECTORY_CONTAINING_AVBTOOL
Fbpacktool
fbpacktool 是 Python 指令碼,用於解壓縮 Pixel bootloader.img 中的個別元件。如果您要驗證 APK,則不需要這個。這項工具非常重要,因為可讓avbtool.py找出驗證所需的圖片。
fbpacktool 需要 Python 3.6 以上版本和 PyYAML 套件 (例如 python3 -m pip install pyyaml)。
如要使用這項工具,請將下列六個 Python 指令碼下載到 avbtool.py 所在的目錄中,將 fbpacktool.py 設為可執行,並建立名為 fbpacktool 的符號連結到 fbpacktool.py,方便使用。
如果你的裝置有 wget:
computer:dir_to_avbtool$ wget https://developers.google.com/android/binary_transparency/tools/pixel/fbpacktool/fbpacktool.py \
https://developers.google.com/android/binary_transparency/tools/pixel/fbpacktool/fbpack.py \
https://developers.google.com/android/binary_transparency/tools/pixel/fbpacktool/packedstruct.py \
https://developers.google.com/android/binary_transparency/tools/pixel/fbpacktool/xmlparser.py \
https://developers.google.com/android/binary_transparency/tools/pixel/fbpacktool/xmlserializer.py \
https://developers.google.com/android/binary_transparency/tools/pixel/fbpacktool/yamlparser.py
computer:dir_to_avbtool$ chmod +x fbpacktool.py
computer:dir_to_avbtool$ ln -s fbpacktool.py fbpacktool
如果你有 curl:
computer:dir_to_avbtool$ curl -LO https://developers.google.com/android/binary_transparency/tools/pixel/fbpacktool/fbpacktool.py
computer:dir_to_avbtool$ curl -LO https://developers.google.com/android/binary_transparency/tools/pixel/fbpacktool/fbpack.py
computer:dir_to_avbtool$ curl -LO https://developers.google.com/android/binary_transparency/tools/pixel/fbpacktool/packedstruct.py
computer:dir_to_avbtool$ curl -LO https://developers.google.com/android/binary_transparency/tools/pixel/fbpacktool/xmlparser.py
computer:dir_to_avbtool$ curl -LO https://developers.google.com/android/binary_transparency/tools/pixel/fbpacktool/xmlserializer.py
computer:dir_to_avbtool$ curl -LO https://developers.google.com/android/binary_transparency/tools/pixel/fbpacktool/yamlparser.py
computer:dir_to_avbtool$ chmod +x fbpacktool.py
computer:dir_to_avbtool$ ln -s fbpacktool.py fbpacktool
OpenSSL
openssl 可協助您在驗證工作流程中,使用終端機執行各種密碼編譯作業。確認系統已安裝該程式庫。 您可以在終端機執行下列指令,確認傳回的結果不是空白:
computer:~$ which openssl
/usr/bin/openssl
建構驗證酬載
如要驗證裝置上的映像檔或從 Pixel 原廠映像檔網站下載的映像檔,您必須先從版本指紋和 VBMeta 摘要建構與該映像檔對應的酬載。有手動和自動兩種選項,如下所述。
- 版本指紋:原廠映像檔的字串 ID
- 例如:
google/oriole/oriole:12/SD1A.210817.015.A4/7697517:user/release-keys
- 例如:
- VBMeta 摘要:SHA-256 密碼編譯摘要,可將原廠映像檔的不同部分連結在一起
- 例如:
4d5e41b7c675a821de81f7d2c744623fe808c8c2d3e19a83e894dab5d0829dbe
- 例如:
手動建構酬載
如要手動產生驗證用的酬載,請建立文字檔 payload.txt,其中一行包含版本指紋,下一行包含 VBMeta 摘要,並在最後一行留空 (請參閱「記錄內容」一節,瞭解確切格式)。
按照原廠映像檔網站的步驟操作後,payload.txt 檔案的內容應如下所示:
google/oriole/oriole:12/SD1A.210817.015.A4/7697517:user/release-keys
4d5e41b7c675a821de81f7d2c744623fe808c8c2d3e19a83e894dab5d0829dbe
從 Pixel 原廠映像檔網站擷取 VBMeta 摘要和版本指紋
從 Pixel 原廠映像檔網站下載原廠映像檔時,您需要解壓縮下載的檔案,這會在原處建立另一個資料夾。前往新建立的資料夾,然後解壓縮相關聯的 image-*.zip 檔案,取得個別 *.img 檔案。這些二進位檔是分區映像檔。確認 img 檔案中包含 vbmeta.img 檔案。畫面應如下所示:
computer:oriole-sd1a.210817.015.a4$ unzip image-oriole-sd1a.210817.015.a4.zip
Archive: image-oriole-sd1a.210817.015.a4.zip
inflating: android-info.txt
inflating: boot.img
inflating: vendor_boot.img
inflating: system.img
inflating: vendor.img
inflating: product.img
inflating: system_ext.img
inflating: system_other.img
inflating: dtbo.img
inflating: vbmeta_system.img
inflating: vbmeta.img
inflating: super_empty.img
您可以從所有下載的檔案中擷取版本指紋,並計算 VBMeta 摘要。
$PATH 取得 avbtool.py 後,您可以在終端機中執行下列指令,擷取裝置專屬建構指紋,並計算剛下載的原廠映像檔 VBMeta 摘要:
computer:dir_containing_unzipped_factory_image~$ grep -a 'ro\.product\.build\.fingerprint=google/.*/release-keys' product.img | cut -f2 -d'='
google/oriole/oriole:12/SD1A.210817.015.A4/7697517:user/release-keys
computer:dir_containing_unzipped_factory_image$ avbtool.py calculate_vbmeta_digest --image vbmeta.img
4d5e41b7c675a821de81f7d2c744623fe808c8c2d3e19a83e894dab5d0829dbe
手動驗證下載的原廠映像檔
如要正確驗證下載的原廠映像檔,您需要下載 fbpacktool.py 和其他依附的 Python 檔案 (如需完整清單和下載指令,請參閱「Fbpacktool」的先決條件),藉此解壓縮 bootloader.img 中的元件,因為這些元件會個別說明於 VBMeta struct 中。取得解壓縮工具後,您可以使用 fbpacktool.py 解壓縮系統啟動載入程式映像檔。例如:
computer:dir_containing_unzipped_images$ python3 fbpacktool.py unpack bootloader-oriole-slider-1.0-7683913.img
您應該會在該目錄中看到額外的 .img 檔案。現在,您可以繼續使用 avbtool.py 手動驗證各個分區的簽章是否相符。
computer:dir_containing_unzipped_images$ avbtool.py verify_image --image vbmeta.img --follow_chain_partitions
Verifying image vbmeta.img using embedded public key
vbmeta: Successfully verified SHA256_RSA4096 vbmeta struct in vbmeta.img
vbmeta_system: Chained but ROLLBACK_SLOT (which is 1) and KEY (which has sha1 df529646b7225015196a714006346f0768b87fcf) not specified
--
Verifying image vbmeta_system.img using embedded public key
vbmeta: Successfully verified SHA256_RSA4096 vbmeta struct in vbmeta_system.img
product: Successfully verified sha256 hashtree of product.img for image of 2700468224 bytes
system: Successfully verified sha256 hashtree of system.img for image of 878493696 bytes
system_ext: Successfully verified sha256 hashtree of system_ext.img for image of 251334656 bytes
vbmeta_vendor: Chained but ROLLBACK_SLOT (which is 3) and KEY (which has sha1 85322346680a860c091fa14a64cef1fe4a3ffe31) not specified
--
Verifying image vbmeta_vendor.img using embedded public key
vbmeta: Successfully verified SHA256_RSA4096 vbmeta struct in vbmeta_vendor.img
vendor: Successfully verified sha256 hashtree of vendor.img for image of 472940544 bytes
boot: Chained but ROLLBACK_SLOT (which is 2) and KEY (which has sha1 85322346680a860c091fa14a64cef1fe4a3ffe31) not specified
--
Verifying image boot.img using embedded public key
vbmeta: Successfully verified footer and SHA256_RSA4096 vbmeta struct in boot.img
boot: Successfully verified sha256 hash of boot.img for image of 24481792 bytes
abl: Successfully verified sha256 hash of abl.img for image of 1744896 bytes
bl1: Successfully verified sha256 hash of bl1.img for image of 12288 bytes
bl2: Successfully verified sha256 hash of bl2.img for image of 544768 bytes
bl31: Successfully verified sha256 hash of bl31.img for image of 86016 bytes
dtbo: Successfully verified sha256 hash of dtbo.img for image of 2152234 bytes
gsa: Successfully verified sha256 hash of gsa.img for image of 262144 bytes
ldfw: Successfully verified sha256 hash of ldfw.img for image of 4096000 bytes
pbl: Successfully verified sha256 hash of pbl.img for image of 49152 bytes
tzsw: Successfully verified sha256 hash of tzsw.img for image of 4304896 bytes
vendor_boot: Successfully verified sha256 hash of vendor_boot.img for image of 25718784 bytes
vendor_dlkm: Successfully verified sha256 hashtree of vendor_dlkm.img for image of 54505472 bytes
如果沒有看到任何錯誤或失敗訊息,表示下載映像檔的 VBMeta 已成功驗證。
自動建構酬載
為方便起見,我們也提供可使用的 Python 指令碼。使用這項工具時,您只需指定原廠映像檔的網址,不必手動擷取及解壓縮檔案。
computer:dir_to_avb_repo/tools/transparency$ python3 ./pixel_factory_image_verify.py https://dl.google.com/dl/android/aosp/oriole-sd1a.210817.015.a4-factory-074b7f51.zip
Fetching file from: https://dl.google.com/dl/android/aosp/oriole-sd1a.210817.015.a4-factory-074b7f51.zip
Successfully downloaded file.
Successfully unpacked factory image.
Successfully unpacked factory image partitions.
Successfully unpacked bootloader image.
Successfully verified VBmeta.
Successfully extracted build fingerprint.
Successfully calculated VBMeta Digest.
The build fingerprint for factory image is: google/oriole/oriole:12/SD1A.210817.015.A4/7697517:user/release-keys
The VBMeta Digest for factory image is: 4d5e41b7c675a821de81f7d2c744623fe808c8c2d3e19a83e894dab5d0829dbe
A corresponding "payload.txt" file has been created.
執行上述指令後,系統會下載原廠映像檔套件、解壓縮套件,並驗證已簽署分區的簽章。驗證成功後,系統會建立名為 payload.txt 的新檔案,內容應與下方類似:
google/oriole/oriole:12/SD1A.210817.015.A4/7697517:user/release-keys
4d5e41b7c675a821de81f7d2c744623fe808c8c2d3e19a83e894dab5d0829dbe
成功建立 payload.txt 後,即可開始驗證圖片是否已納入。
驗證圖片是否已納入 (納入證明)
建構酬載後,請設定殼層變數 (例如 $PAYLOAD_PATH),指向酬載的路徑。
Android 開放原始碼計畫的 avb 存放區中已發布納入證明工具。執行方式如下:
computer:external/avb/tools/transparency/verify$ PAYLOAD_PATH=../payload.txt
computer:external/avb/tools/transparency/verify$ go build cmd/verifier/verifier.go
computer:external/avb/tools/transparency/verify$ ./verifier --payload_path=${PAYLOAD_PATH} --log_type=pixel
驗證工具會使用檢查點和記錄內容 (位於圖塊目錄),確認映像檔酬載位於透明度記錄中,也就是由 Google 發布。
指令的輸出內容會寫入 stdout:
- 如果記錄包含圖片,則為
OK. inclusion check success! - 如果不是,則為
FAILURE。