Android的Fastboot模式相信熟悉android平台以及广大刷机爱好者都不会陌生,fastboot是一种比recovery更底层但是比低格高级的刷机模式。用fastboot模式来进行手机中各分区img的更新或者获取手机某些信息非常方便,因为不需要启动内核。进入fastboot模式的方式各个手机厂家互不相同,有的按着上键插USB线可以进入,有的按着下键插USB线进入,有的按着电源键和上键或下键进入。不管采用什么方式进入,所采用的fastboot协议是相同的。

        进入fastboot之后,要想通过PC跟手机进行通讯完成刷机等功能,PC机上必须也要有一个fastboot的可执行文件,而PC机上的这个可执行文件的生成方式可以通过android平台代码自身编译生成,代码路径在system/core/fastboot,里面的最主要的一个文件就是fastboot.cpp,main函数实现就在这个文件中。

          仔细阅读这个cpp文件我们可以发现fastboot默认支持的命令如下:


usage: fastboot [ <option> ] <command>
 
commands:
   update <filename>                                  reflash device from update.zip
   flashall                                                     flash boot + recovery + system
   flash <partition> [ <filename> ]              write a file to a flash partition
   erase <partition>                                     erase a flash partition
   format <partition>                                   format a flash partition
   getvar <variable>                                    display a bootloader variable
   boot <kernel> [ <ramdisk> ]                   download and boot kernel
   flash:raw boot <kernel> [ <ramdisk> ]    create bootimage and flash it
   devices                                                     list all connected devices
   continue                                                    continue with autoboot
   reboot                                                       reboot device normally
   reboot-bootloader                                      reboot device into bootloader
   help                                                           show this help message 
options:
   -w                                       erase userdata and cache (and format if supported by partition type)
   -u                                        do not first erase partition before formatting
   -s <specific device>             specify device serial number or path to device port
   -l                                         with "devices", lists device paths
   -p <product>                             specify product name
   -c <cmdline>                             override kernel commandline
   -i <vendor id>                           specify a custom USB vendor id
   -b <base_addr>                         specify a custom kernel base address
   -n <page size>                           specify the nand page size. default: 2048
   -S <size>[K|M|G]                       automatically sparse files greater than size.  0 to disable


 

在命令行中输入fastboot -h就会打印出这些信息。另外fastboot还支持用户扩展命令,但是扩展的命令格式必须如下面所示:fastboot oem command args,也就是说扩展的命令必须要以oem开头,否则即使手机侧用fastboot_register注册成功了命令,但是通过PC侧fastboot输入该命令时也不会发到手机侧,而是直接返回help的内容。

        那是不是就不能扩展别的命令了呢,其实也不是,但是需要修改fastboot.cpp的代码,并且重新编译PC侧的fastboot工具。PC侧的fastboot工具分为linux下的工具和windows下的工具两种。linux下用的工具很简单,我们只要按照模块编译的方法在android版本的根目录下输入make fastboot就可以了,编译之后生产的fastboot可执行文件位于out/host/linux-x86/bin/fastboot目录下,这个只能在linux环境下执行。如果需要编译出能在windows环境下执行的fastboot比较麻烦,步骤如下:

        1.按照交叉编译环境mingw32

        2.设置编译参数和环境变量。

        3.编译:make USE_MINGW=y fastboot。

编译出的文件存放在out/host/windows-x86/bin目录下。

windows下的fastboot.exe工具很小只有几百K,但是linux下的fastboot有8MB左右。一般SDK包里都包含windows下的fastboot.exe工具,当然这个fastboot支持的命令只有默认的那些。