Android recovery 模式

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文件簽名差不錯。公鑰會被硬編譯入recovery,編譯時生成在:out/target/product/XX/obj/PACKAGING/ota_keys_inc_intermediates/keys.inc
G1中的三種啓動模式
        MAGIC KEY:
php


  • camera +
            power:bootloader模式,ADP裏則可使用fastboot模式
           

  • home + power:recovery模式
           

  • 正常啓動


        Bootloader正常啓動,又有三種方式,按照BCB(Bootloader
Control Block, 下節介紹)中的command分類:
android


  • command == 'boot-recovery'
            → 啓動recovery.img。recovery模式
           

  • command ==
            'update-radio/hboot' → 更新firmware(bootloader)
           

  • 其餘 → 啓動boot.img


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
                      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命令
             


兩種Recovery
Case
cookie

  • 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()
            重啓系統
    Recovery模式流程
    /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():
             
      • translate_root_path():roots.c
                         [ "SYSTEM:lib" and turns it into a string like
                        "/system/lib", translate the updater.zip path ]
                       
      • mzOpenZipArchive():zip.c
                         [ open updater.zip file (uncompass) ]
                       
      • handle_update_package():install.c
                       
        • verify_jar_signature():verifier.c
                                   [ verify signature with keys.inc key; verify manifest and zip
                                  package archive ]
                                 
          • verifySignature()
                                             [ verify the signature file: CERT.sf/rsa. ]
                                           
            • digestEntry():verifier.c
                                                       [ get SHA-1 digest of CERT.sf file ]
                                                     
            • RSA_verify(public
                                                      key:keys.inc, signature:CERT.rsa, CERT.sf's digest):libc/rsa.c
                                                      [ Verify a 2048 bit RSA PKCS1.5 signature against an expected
                                                      SHA-1 hash. Use public key to decrypt the CERT.rsa to get
                                                      original SHA digest, then compare to digest of CERT.sf ]
                                             

                                           
          • verifyManifest()
                                             [ Get manifest SHA1-Digest from CERT.sf. Then do  digest to
                                            MANIFEST.MF. Compare them ]
                                           
          • verifyArchive()
                                             [ verify all the files in update.zip with digest  listed in
                                            MANIFEST.MF ]
                                   

                                 
        • find_update_script():install.c
                                  [ find META-INF/com/google/android/update-script updater script ]
                                 
        • handle_update_script():install.c
                                  [ read cmds from script file, and do parser, exec ]
                                 
          • parseAmendScript():amend.c
                                             [ call yyparse() to parse to command ]
                                           
          • exeCommandList():install.c
                                           
            • exeCommand():execute.c
                                                       [ call command hook function ]
                                             

                                   

                         

               

             
    • erase
              DATA/CACHE partition
             
    • prompt_and_wait():recovery.c
               [ wait for user input: 1) reboot 2) update.zip 3) 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() is called by write_hboot/radio_image
              command, it stores the bootloader image to CACHE partition, and
              write update-hboot/radio command to MISC partition for bootloader
              message to let bootloader update itself after reboot ]
             
      • set_bootloader_message()
                       
      • write_update_for_bootloader():bootloader.c
                        [ write firmware image into CACHE partition with update_header,
                        busyimage and failimage ]
               

             
    • finish_recovery():recovery.c
               [ clear the recovery command and prepare to boot a (hopefully
              working) system, copy our log file to cache as well (for the system
              to read), and record any intent we were asked to communicate back to
              the system. ]
             
    • reboot()

    Recovery模式流程圖
    如下流程圖繪製了系統從啓動加載bootloader後的行爲流程。

    Click here to open new window
CTRL+Mouse wheel to zoom in/out

                   

     

     WIPE 清除手機的信息 就是格式化 而後你在刷你喜歡的ROM。

    五、Wipe——清除
    5-一、Wipe data/factory reset——清除內存數據和緩存數據
    5-二、Wipe Dalvik-cache——清除緩存數據 + ext 分區內數據
    5-三、Wipe SD:ext partition——只清除ext 分區內數據
    5-四、Wipe battery stats——清除電池數據
    5-五、Wipe rotate settings——清除傳感器內設置的數據

    分區
    六、Partition sdcard——分區sd 卡
    6-一、Partition SD——自動爲sd 卡分區
    6-二、Repair SD:ext——修復ext 分區
    6-三、SD:ext2 to ext3——將ext2 分區轉換爲ext3 分區(推薦)
    6-四、SD:ext3 to ext4——將ext3 分區轉換爲ext4 分區(C4 卡不推薦,C6 卡推薦)

    想多瞭解 請看下面:

    一、Reboot system now——重啓

    二、USB-MS toggle——在recovery 模式下直接鏈接USB,讀取你的sd卡,這個很是方便。刷不成的話你還能夠往sd卡拷貝別的ROM 進行補救,按小房子退出該模式。很是棒的功能,最近才發現的。2010.05.27.

    三、Backup/Restore——備份和還原
    3-一、Nand backup——Nand 備份
    3-二、Nand + ext backup——Nand 備份(系統和ext 分區一同備份)
    3-三、Nand restore——還原(就是還原3-一、3-2 的最後一次備份)
    3-四、BART backup——BART 備份 (包括系統和ext 分區)
    3-五、BART restore——還原最後一次的BART備份

    Nand 備份相似於系統的備份 而BART 則像是PC 上ghost 的備份。
    Nand 備份 它不會備份ext 分區(就是第二分區 沒有分區的能夠無論這個)
    因此你的若是app2sd 了 那麼裝在第二分區的程序用Nand 恢復是辦不到的
    BAR T則能夠備份到ext 分區 用BART 恢復則能夠恢復整個系統 可使它和你備份前如出一轍,不會有一點文件信息的丟失(包括聯繫人、短信、圖片、影音等,因此若是你裝的東西比較多,那麼備份和恢復會比較慢)

    四、Flash zip from sdcard——從sd卡根目錄的.zip ROM 刷機包刷機

    五、Wipe——清除
    5-一、Wipe data/factory reset——清除內存數據和緩存數據
    5-二、Wipe Dalvik-cache——清除緩存數據 + ext 分區內數據
    5-三、Wipe SD:ext partition——只清除ext 分區內數據
    5-四、Wipe battery stats——清除電池數據
    5-五、Wipe rotate settings——清除傳感器內設置的數據

    六、Partition sdcard——分區sd 卡
    6-一、Partition SD——自動爲sd 卡分區
    6-二、Repair SD:ext——修復ext 分區
    6-三、SD:ext2 to ext3——將ext2 分區轉換爲ext3 分區(推薦)
    6-四、SD:ext3 to ext4——將ext3 分區轉換爲ext4 分區(C4 卡不推薦,C6 卡推薦)

    七、Other——其它
    7-一、Fix apk uid mismatches——修復apk 程序
    7-二、Move apps+dalv to SD——移動程序和虛擬緩存到sd 卡(這個可不是 app2sd)
    7-三、Move recovery.log to SD——移動刷機日誌文件到sd 卡(執行此操做後,sd 卡根目錄會出現一個「recovery.log」 文件 即爲刷機日誌文件)

    八、Power off——關機
    app

  • 相關文章
    相關標籤/搜索