from:http://bbs.chinaunix.net/thread-1937272-1-1.html
这篇文章最早的作者是 chinaunix 社区的一位大牛,排版也相当漂亮,但是不知道为什么后来无法找到了。这里我就将它重新整理了下,方便大家及自己以后查阅。作者分析recovery 是基于android 早期版本的,我做的是omap4460android 4.0.x,发现里面的一些流程有些变动,这个体现在本文的第五,六章,如register_update_commands,这个函数我在代码中就没找到。有空我将基于android 4.0.x做流程的分析。虽然androidrecovery version有变动,但是整个系统逻辑没有变化,因此整理了它,以做总结。
1. Recovery简介
Android利用Recovery模式,进行恢复出厂设置,OTA升级,patch升级及firmware升级。
升级一般通过运行升级包中的META-INF/com/google/android/update-script脚本来执行自定义升级,脚本中是一组recovery系统能识别的UI控制,文件系统操作命令,例如write_raw_image(写FLASH分区),copy_dir(复制目录)。该包一般被下载至SDCARD和CACHE分区下。如果对该包内容感兴趣,可以从http://forum.xda-developers.com/showthread.php?t=442480下载JF升级包来看看。
升级中还涉及到包的数字签名,签名方式和普通JAR文件签名差不多。公钥会被copy到recovery.img中的res/keys文件,编译时生成在:out/target/product/XX/obj/PACKAGING/ota_keys_inc_intermediates/keys
2. G1中的三种启动模式
2.1 MAGICKEY:
1. camera + power:bootloader模式,ADP里则可以使用fastboot模式
2. home + power:recovery模式
2.2 正常启动
Bootloader正常启动,又有三种方式,按照BCB(Bootloader Control Block, 下节介绍)中的command分类:
1. command == 'boot-recovery' → 启动recovery.img。recovery模式
2. command == 'update-radio/hboot'→更新firmware(bootloader)
3. 启动boot.img
3. Recovery涉及到的其他文件
3.1 CACHE分区文件
Recovery 通过NAND cache分区上的三个文件和主系统打交道。主系统(包括恢复出厂设置和OTA升级)可以写入recovery所需的命令,读出recovery过程中的LOG和intent。
/cache/recovery/command: recovery命令,由主系统写入。所有命令如下:
--send_intent=anystring- write the text out to /cache/recovery/intentafter finish recovery
--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
--set_encrypted_filesystem=on|off– enable/disable encrypted fs
/cache/recovery/log:recovery过程日志,由主系统读出
/cache/recovery/intent:recovery输出的intent
3.2 MISC分区内容
MISC 分区是NAND 上的一个分区,这个分区放的是结构体bootloader_message。它是bootloader和 recovery system 进行通信的桥梁。具体体现在:
1. 在进入recovery时,写”boot-recovery”到command 字段,写参数到recovery字段;在完成recovery时,清楚misc分区。已达到recovery由于意外失败后可以重入
2. 主系统想bootinto recovery或updateradio或updatebootloader firmware时,写MISC分区的 command及recovery字段。
BootloaderControl Block (BCB) 存放recovery bootloader message,结构如下, 详见mydroid/bootable/recovery/bootloader.h,
struct bootloader_message {
char command[32];
char status[32]; // 未知用途
char recovery[1024];
};
command可以有以下两个值:
“boot-recovery”:标示recovery正在进行,或指示bootloader应该进入recovery mode
“update-hboot/radio”:指示bootloader更新firmware
recovery内容
“recovery\n
\n”
其中recovery command为CACHE:/recovery/command命令
4. 两种Recovery Case:
4.1 FACTORY RESET(恢复出厂设置)
1. 用户选择“恢复出厂设置”,主系统将"--wipe_data"命令写入/cache/recovery/command
2. 系统重启,并进入recover模式(/sbin/recovery)
3. get_args() 将"boot-recovery"和"--wipe_data"写入BCB
4. erase_volume() 格式化(擦除)DATA分区
5. erase_ volume() 格式化(擦除)CACHE分区
6. finish_recovery() 擦除BCB
7. 重启系统
4.2 OTA INSTALL(OTA升级)
1. 主系统下载 OTA包到/cache/some-filename.zip主系统写入recovery命令"--update_package=CACHE:some-filename.zip"
2. 重启并进入recovery模式
3. get_args() 将"boot-recovery"和"--update_package=..." 写入BCB
4. install_package() 做升级
5. finish_recovery() 擦除 BCB
6. ** 如果安装包失败 ** prompt_and_wait() 等待用户操作,选择ALT+S或ALT+W 升级或恢复出厂设置
7. main() 调用 maybe_install_firmware_update()
如果包里有hboot/radio的firmware则继续,否则返回
将"boot-recovery" 和"--wipe_cache" 写入BCB
将firmware image写入cache分区
将"update-radio/hboot" 和"--wipe_cache" 写入BCB
8. 重启系统
9. bootloader自身更新firmware
10. bootloader 将"boot-recovery" 写入BCB
erase_root() 擦除CACHE分区
清除 BCB
11.main() 调用 reboot() 重启系统
5. Recovery模式流程
Bootloaderàrecovery.imgà/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 usedlater, include icons, bmps]
create 2threads: progress/input_thread [create progress show and inputevent handlerthread]
get_args():recovery.c
get_bootloader_message():bootloader.c[read mtdblock0(misc partition) 2ndpage for commandline], check if NAND MISC partition has boot message. If yes,fill argc/argv.If no, getarguments from /cache/recovery/command, and fillargc/argv.
set_bootloader_message():bootloader.c[set bootloader message back tomtdblock0]
Parser argv[]filled above
register_update_commands():commands.c [register all commands with name andhook function ]
registerCommand():commands.c
Register commandwith name, hook, type, cookie.
Commands, e.g: assert, delete, copy_dir,symlink, write_raw_image.
registerFunction():commands.c
Registerfunction with name, hook, cookie.
Function, e.g: get_mark, matches, getprop,file_contains
install_package():
translate_root_path():roots.c ["SYSTEM:lib" and turns it into astring like "/system/lib",translate the updater.zip path ]
mzOpenZipArchive():zip.c [ open updater.zipfile (uncompass) ]
handle_update_package():install.c
verify_jar_signature():verifier.c [ verifysignature with keys.inc key;verify manifest and zip package archive ]
verifySignature() [ verify the signaturefile: CERT.sf/rsa. ]
digestEntry():verifier.c [ get SHA-1 digestof CERT.sf file ]
RSA_verify(public key:keys.inc,signature:CERT.rsa, CERT.sf'sdigest):libc/rsa.c [ Verify a 2048 bit RSAPKCS1.5 signature against an expectedSHA-1 hash. Use public key to decrypt theCERT.rsa to get original SHA digest,then compare to digest of CERT.sf ]
verifyManifest() [ Get manifest SHA1-Digestfrom CERT.sf. Then do digest toMANIFEST.MF. Compare them ]
verifyArchive() [ verify all the files inupdate.zip with digest listed inMANIFEST.MF ]
find_update_script():install.c [ findMETA-INF/com/google/android/update-scriptupdater script ]
handle_update_script():install.c [ readcmds from script file, and doparser, exec ]
parseAmendScript():amend.c [ call yyparse()to parse to command ]
exeCommandList():install.c
exeCommand():execute.c [ call command hookfunction ]
erase DATA/CACHE partition
prompt_and_wait():recovery.c [ wait foruser input: 1) reboot 2) update.zip3) wipe data ]
ui_key_xxx get ALT+x keys
1) do nothing
2) install_package('SDCARD:update.zip')
3) erase_root() → format_root_device() DATA/CACHE
may_install_firmware_update():firmware.c [remember_firmware_update() iscalled by write_hboot/radio_image command, itstores the bootloader image toCACHE partition, and write update-hboot/radiocommand to MISC partition forbootloader message to let bootloader updateitself after reboot ]
set_bootloader_message()
write_update_for_bootloader():bootloader.c[ write firmware image intoCACHE partition with update_header, busyimage andfailimage ]
finish_recovery():recovery.c [ clear therecovery command and prepare toboot a (hopefully working) system, copy our logfile to cache as well (for thesystem to read), and record any intent we wereasked to communicate back to thesystem. ]
reboot()
6. Recovery模式流程图
以下流程图绘制了系统从启动加载bootloader后的行为流程。