Recovery简介
- 1.Android利用Recovery模式,进行恢复出厂设置,OTA升级,patch升级及firmware升级。运行升级包中的META-INF/com/google/android/update-script脚本来执行自定义升级。
- a.脚本中是一组recovery系统能识别的UI控制,文件系统操作命令
- b.升级包一般被下载至SDCARD和CACHE分区下
- 2.升级中还涉及到包的数字签名,签名方式和普通JAR文件签名差不多。公钥会被硬编译入recovery,编译时生成在:out/target/product/XX/obj/PACKAGING/ota_keys_inc_intermediates/keys.inc
手机中三种启动模式
- MAGIC KEY:
- 1.camera + power:bootloader模式,ADP里则可以使用fastboot模式
- 2.home + power:recovery模式
- 3.正常启动
- Bootloader正常启动,又有三种方式,按照BCB(Bootloader Control Block, 下节介绍)中的command分类:
- 1.command == ‘boot-recovery’ → 启动recovery.img。recovery模式
- 2.command == ‘update-radio/hboot’ → 更新firmware:写在ROM上的叫固件(bootloader)
- 3.其他 → 启动boot.img
2.Recovery涉及到的其他系统及文件
CACHE分区文件
Recovery 工具通过NAND cache分区上的三个文件和主系统打交道。主系统(包括恢复出厂设置和OTA升级)可以写入recovery所需的命令,读出recovery过程中的LOG和intent。
/cache/recovery/command:recovery命令,由主系统写入。所有命令如下:
--send_intent=anystring - write the text out to recovery.intent
--update_package=root:path - verify install an OTA package file
--wipe_data - erase user data (and cache), then reboot
--wipe_cache - wipe cache (but not user data), then reboot
- /cache/recovery/log:recovery过程日志,由主系统读出
- /cache/recovery/intent:recovery输出的intent
MISC分区内容
Bootloader Control Block (BCB) 存放recovery bootloader message。结构如下:
struct bootloader_message {
char command[32];
char recovery[1024];};
command可以有以下两个值
* boot-recovery
:标示recovery正在进行,或指示bootloader应该进入recovery mode
* update-hboot/radio
:指示bootloader更新firmware
recovery内容
其中recovery command为CACHE:/recovery/command命令
两种Recovery Case
FACTORY RESET(恢复出厂设置)
设置系统将”–wipe_data”命令写入/cache/recovery/command,系统重启,并进入recover模式(/sbin/recovery)
- get_args() 将 “boot-recovery”和”–wipe_data”写入BCB
- erase_root() 格式化(擦除)DATA分区
- erase_root() 格式化(擦除)CACHE分区
- finish_recovery() 擦除BCB
OTA INSTALL(OTA升级)
- 升级系统下载 OTA包到/cache/some-filename.zip
- 升级系统写入recovery命令”–update_package=CACHE:some-filename.zip”
- 重启,并进入recovery模式
- get_args() 将”boot-recovery” 和 “–update_package=…” 写入BCB
- install_package() 作升级
- finish_recovery() 擦除 BCB
- * 如果安装包失败 * prompt_and_wait() 等待用户操作,选择ALT+S或ALT+W 升级或恢复出厂设置
- main() 调用 maybe_install_firmware_update()
- 如果包里有hboot/radio的firmware则继续,否则返回将 “boot-recovery” 和 “–wipe_cache” 写入BCB将 firmware image写入cache分区 将 “update-radio/hboot” 和 “–wipe_cache” 写入BCB
- 重启系统
bootloader自身更新firmware - bootloader 将 “boot-recovery” 写入BCB
- erase_root() 擦除CACHE分区
- 清除 BCB
- main() 调用 reboot() 重启系统
4.Recovery模式流程
- 1./init → init.rc → /sbin/recovery →main():recovery.c
- ui_init():ui.c [UI initialize]
- gr_init():minui/graphics.c [set tty0 to graphic mode, open fb0]
- ev_init():minui/events.c [open /dev/input/event*] res_create_surface:minui/resource.c [create surfaces for all bitmaps used later, include icons, bmps]create 2 threads: progress/input_thread [create progress show and input event handler thread]
- get_args():recovery.c
- get_bootloader_message():bootloader.c [read mtdblock0(misc partition) 2nd page for commandline]
check if nand misc partition has boot message. If yes, fill argc/argv.
If no, get arguments from /cache/recovery/command, and fill argc/argv.
set_bootloader_message():bootloader.c [set bootloader message back to mtdblock0]
Parser argv[] filled above
register_update_commands():commands.c [ register all commands with name and hook function ]
registerCommand():commands.c
Register command with name, hook, type, cookie.
Commands, e.g: assert, delete, copy_dir, symlink, write_raw_image.
registerFunction():commands.c
Register function with name, hook, cookie.
Function, e.g: get_mark, matches, getprop, file_contains
install_package():
- get_bootloader_message():bootloader.c [read mtdblock0(misc partition) 2nd page for commandline]
- ui_init():ui.c [UI initialize]