└── README.md /README.md: -------------------------------------------------------------------------------- 1 | ## 課程名稱:Android OTA 介紹 2 | ## 課程大綱 3 | - Android 更新模式介紹 4 | -- Fastboot 5 | -- Recovery mode 6 | -- OTA 7 | - Android Image 製作 8 | -- Engineering image 9 | -- Recovery deploy 10 | -- OTA update image 11 | - Android Image 客製化與 OTA 12 | -- OTA 更新模式 13 | -- OTA Image 的客製化 14 | -- 實作示範 15 | 16 | ## Android 更新模式介紹 17 | 18 | 有 3 種更新 Android 軟體的方式: 19 | 20 | - Full update image 21 | - Update files 22 | - Wipe and re-flash partitions(使用 fastboot) 23 | 24 | Full image 的做法,是製做出更新的 ROM,即 update.zip,再將手機切換至 Recovery mode 來更新。Update files 則是使用 bsdiff 來更新 /system 裡的部份檔案。 25 | 26 | 最後一個做法,是 re-flash 特定的 partition,例如更新存放 boot.img 的 partition;這種做法是使用一個稱為 fastboot 的工具。這種方式,手機上 Second bootloader 必須實現 Fastboot protocol。 27 | 28 | 29 | 30 | ### Release Tools 31 | 32 | release tools 能產生 signed update image,這個工具位於 build/tools/releasetools/,並透過 Android build system 來產生 update.zip。 33 | 34 | 如何產生 update.zip: 35 | 36 | ``` 37 | make target-files-package 38 | ``` 39 | 40 | target-files-package 也稱為 TFP。完成編譯後,可以找到以下二個 image file: 41 | 42 | - out/target/product/mokoid/obj/PACKAGING/apkcerts_intermediates/full-apkcerts-eng.txt 43 | - out/target/product/mokoid/obj/PACKAGING/target_files_intermediates/full-target_files-eng.zip 44 | 45 | 上述的 image 是利用 mkbookimg 來製作。針對使用 U-Boot 的裝置,請記得利用 mkimage 來製作 U-Boot 格式的 image file。 46 | 47 | ### android.os.RecoverySystem 48 | 49 | 這是 Android framework 所提供的標準 API,用來驗證與安裝 update image。以 OTA 方式更新 Android 的話,就是使用這個 API 來驗證 update image。 50 | 51 | 在下載 Update image 後,要先利用 android.os.RecoverySystem 將 RC command files 寫至 /cache/recovery,再將手機重開機至 Recovery mode。 52 | 53 | ### Recovery Console 54 | 55 | RC 是 Recovery Console 的意思,這其實是一個精簡的開機環境,有點像以往的 Embedded Linux 的 Initial Root Filesystem。進入 Recovery Console 後,它會自動驗證 update image,接著執行更新命令。 56 | 57 | ### Updater 58 | 59 | RC command files 採用一個稱為 Edify 的語言撰寫。請注意,RC 的 command file 並不是使用典型的 shell script 語法撰寫。 60 | 61 | ### OTA 62 | 63 | OTA 是 Over-The-Air 的意思,它會從製造商所提供的 Server 下載 update image,並透過 Recovery Console 來更新手機軟體。 64 | 65 | 市面上的 Android 裝置大多採用這種做法,因為對一般使用者來說,這是最簡單的方式。要使用 OTA,需要有以下的基礎建設: 66 | 67 | - 存放 update image 的 Server 68 | - 檢查並下載 update image 的 App(the Fetcher) 69 | - 通知用戶有軟體更新的 Notification UI,一般是與上述的 App 整合在一起 70 | 71 | 要實現 OTA,需要 Server 與 App,這二個部份都沒有 Open Source,必須廠商自行開發。不過,去年(2012)出現了一個 OTA 的免費服務: 72 | 73 | ``` 74 | ``` 75 | 76 | 它提供了免費的 Server,以及 Open source 的手機端 App。只要將自已的 update image 放到 otaupdater.com 上,再配合它的 App,就可以在手機加入「第三方軟體更新」的機制。 77 | 78 | 例如,知名的 CM ROM 現在就能用這種方式更新,相當的方便。除了一些社群所製作的 update image 外,其它手機廠,都是使用自已的 Server 以及 update App。 79 | 80 | ### OTA Server 81 | 82 | Server 的部份沒有 Open Source 的解決方案,只能自行設計與實作。不過,它的原理並不複雜: 83 | 84 | - 使用 HTTP 下載 update image 85 | - Server 與 Device 需要定義簡單的協定(OTA Protocol) 86 | 87 | 從 Server 的角度來看,一個簡單的 RESTful Web Service,以及簡易的 Protocol 就能滿足 OTA 的需求。RESTful Web Service 使用 JSON 格式來回應 App 的請求,在 Android App 裡處理 JSON 也是很容易的事情。 88 | 89 | ### OTA Update 步驟 90 | 91 | OTA update 的流程如下: 92 | 93 | 1. 安裝 Update Client 端 App,通常是每日檢查一次更新 94 | 2. Client 端由 Server 下載 update ROM 95 | 3. 將 update ROM 儲存至 /sdcard 目錄下 96 | 4. 將裝置重開機至 Recovery Mode 97 | 5. 可選擇性地清除 data partition 或 cache 98 | 99 | 研發重點: 100 | 101 | - OTA Server 的架構,一般是以 HTTP Server 為主 102 | - OTA Client 端開發,重點在使用 HTTP 下載 update ROM 103 | - 製作 update ROM 104 | - 過去有許多可行的架構方式,現在建議以 RESTful 架構為主 105 | 106 | ### Boot Image 107 | 108 | Boot Image 是利用 system/core/mkbootimg 工具,將 kernel image 與 ramdisk image 再打包而成。有時也會將 second bootloader image 加入。 109 | 110 | Android Build System 會自動幫我們調用 mkbootimg,並製作出 boot.img。使用 TFP 時,也會自動調用 mkbootimg,以製作出 update image。 111 | 112 | ### Flash Partitioning 113 | 114 | 要了解 update.zip 的更新原理,必須先研究 flash 的 partition 結構。我目前使用的工程手機,將 flash 切割如下: 115 | 116 | ``` 117 | # mount -t 118 | ``` 119 | 120 | AOSP 階段的 partiton layout 通常只有 3 個: 121 | 122 | - boot 123 | - system 124 | - data 125 | 126 | Production 時,會增加至少 3 個 partition: 127 | 128 | - recovery 129 | - misc 130 | - cache 131 | 132 | 實際的規劃方式,因不同的廠商以及裝置型號,也會有所不同,要依現況為主。此外,不管是採用什麼更新方式,都無法重新分割 partition。所以 Parititon Layout 在 Production 前就要定義好,後續無法再修改。 133 | 134 | ### OTA Server 135 | 136 | 可以自行開發,或使用知名的 OTA Update Center。使用 OTA Update Center 前,需要修改 build.prop 檔案,加入 ROM image 的下載連結。oOTA Update Center 也提供一個用戶端 App。 137 | 138 | OTA Update Center 的特點: 139 | 140 | - 提供 Server 服務 141 | - 每日檢查更新一次 142 | - 自動下載並更新 ROM 143 | 144 | 將以下設定加入 build.prop: 145 | 146 | ``` 147 | otaupdater.otaid=(write your ota id here without spaces or brackets) 148 | otaupdater.otaver=(write your ota version here without spaces or brackets) 149 | otaupdater.otatime=(write the date+time here as: 20120820-1516 without spaces or backets) 150 | ``` 151 | 支援 quirky sdcard 的裝置,可再加入以下設定: 152 | 153 | ``` 154 | otaupdater.sdcard.os=(sdcard name (e.g. sdcard2 for /sdcard2) in the main system here without spaces or brackets) 155 | otaupdater.sdcard.recovery=(sdcard name (e.g. sdcard2 for /sdcard2) in recovery here without spaces or brackets) 156 | ``` 157 | 158 | # 159 | otaupdater.otaid=SmoothROM7 160 | otaupdater.otaver=201309 161 | otaupdater.otatime=20120831-1516 162 | 163 | 164 | # Edify Script 簡介 165 | 166 | Edify 是撰寫 Android update script 的語言,它的語法非常簡單。在 bootable/recovery/{edify,edifyscripting,updater} 目錄下可以找到 Edify。Edify 的幾個基本函數: 167 | 168 | - apply_patch(source_filename, target_filename, target_sha1, target_size, sha1, patch, [[sha1, patch], ...]) 169 | - getprop 170 | - install_zip 171 | - package_extract_dir 172 | - package_extract_file 173 | - read_file 174 | - run_program 175 | 176 | Edify script 並非直接撰寫,而是透過 build/tools/releasetools/edify_generator.py 來生成。 177 | 178 | Edify 的說明文件位於 bootable/recovery/edify/README 179 | 180 | ### Mount Point 的對應 181 | 182 | Recovery 怎麼知道 partition 與 mount point 的對應關係呢?方式是透過 recovery.fstab 文件。recovery.fstab 的寫法,與 Linux 的 fstab 相同。 183 | 184 | RC 與 releasetools 都會用到 recovery.fstab 文件。 185 | 186 | ## Android Image 客製化與 OTA 187 | 188 | ### TFP 189 | 190 | 前面已介紹過,可以使用 target-files-package 這個 Makefile rule 來製作 update image。如果有額外的文件,想要另外加到 update image 裡,可以自行擴充 releasetoos。 191 | 192 | 如果 APK 是 signed,在 update image 裡的新版 APK,也必須使用同樣的 key 來 sign APK。否則新的 APK 將無法讀取原來的 app data。 193 | 194 | APK 的 key 是利用 LOCAL_CERTIFICATE 來設定,這部份是在該 APK 的 Android.mk 裡處理。AOSP 提供 4 個 key,位於 build/target/product/security,廠商可自行新增 key: 195 | 196 | - devkey (Development 階段使用) 197 | - testkey (default、用於 engineering build 測試) 198 | - platform (platform package) 199 | - shared (與 Home/Contacts 共享資料) 200 | - media (在 media/download 裡的 package) 201 | 202 | TFP 會透過 ota_from_target_files 來製作最後的 update image。 203 | 204 | Production 的裝置,不能使用上述由 AOSP 提供的 key。ota_from_target_files 的 --package_key 參數,用來指定 key。如何指定 production key?releasetools 的 sign_target_files_apks 工具會幫我們處理這個工作。 205 | 206 | ### TARGET_RELEASETOOLS_EXTENSIONS 207 | 208 | 這是用於 BoardConfig.mk 的變數,用來撰寫 release tools 的 extension。可參考 ota_from_target_files。 209 | 210 | ### 211 | 212 | $ reboot recovery 213 | 214 | ### BCB 215 | 216 | RC 與 bootloader 的共用 /misc partition,這個 partition 存放一種稱為 Bootloader Control Block (BCB) 的資料。BCB 分為 3 個區塊: 217 | 218 | - BCB.command[32]:給 bootloader 的 command 219 | - BCB.status[32]:bootloader 回傳的 status 220 | - BCB.recovery[1024]:RC 的 command line 221 | 222 | BCB.command 裡的資料,是由 Linux kernel 所寫入,基本上是一行 'boot-recovery' 命令;另外,Linux kernel 也要將 BCB.recovery 清空。BCB.command 是 bootloader 選擇 Boot Image 的依據。也有廠商用來實作 Dual OS 開機。 223 | 224 | $(TARGET_DEVICE_DIR)/recovery/res 裡存放一個 8-bit PNG 圖檔。 225 | 226 | ### Updater 227 | 228 | Android Build System 會將 updater 打包到 update.zip。bootable/recovery/install.c 229 | 230 | ### SYSTEM/build.prop 231 | 232 | ### META/otakeys.txt 233 | 234 | 235 | ## Asus Nexus 7 Unlock 236 | 237 | 首先,要把你的 Nexus 7 Unlock,只要利用 Fastbook 即可輕鬆完成: 238 | 239 | ``` 240 | $ ./fastboot oem unlock 241 | ``` 242 | 243 | 244 | --------------------------------------------------------------------------------