fastboot有快速启动的意思, 但这里指的是安卓系统的线刷模式。

安卓手机刷机一般使用两种模式,  fastboot 线刷,  recovery 卡刷,

通常: 一个使用usb线连接电脑,  另一个使用sd卡。


fastboot相对更底层点, 是启动过程中进入的一种模式。 

uboot最新代码就集成了fastboot模块。故只要使用uboot作为引导程序且支持usb接口(从设备模式)的产品, 一般都能支持fastboot线刷。

查看fastboot相关代码日志, 

git log README.android-fastboot

commit 3aab70afc531d1eea0e6fcfcf49001163ad13137
Author: Sebastian Siewior <bigeasy@linutronix.de>
Date:   Mon May 5 15:08:10 2014 -0500

    usb/gadget: add the fastboot gadget
    
    This patch contains an implementation of the fastboot protocol on the
    device side and documentation. This is based on USB download gadget
    infrastructure. The fastboot function implements the getvar, reboot,
    download and reboot commands. What is missing is the flash handling i.e.
    writting the image to media.

翻到最下面, 可以看到大约是2014年5月5日合入的代码。


recovery卡刷相对运行的更高层一点。其实就类似linux电脑的恢复模式, windows电脑的安全模式。

内核是引导起来的, 只不过加载的文件系统是一个recovery分区中的文件系统, 该文件系统通常不去更改, 里面存放一些必要的软件。如busybox, gpart, 网络工具等等。

安卓手机就是利用该recovery模式进行刷机操作, 将sd卡中的数据刷入手机emmc存储介质中。


ok, 这里记录下利用uboot fastboot功能更新相应分区镜像。

1. 这里使用的环境是xilinx zynq-ultrascale开发环境, 使用sd卡做为启动存储介质。即类似树莓派, 并不使用内部emmc作为存储介质。 而使用sdio接口读写外部sd/emmc卡。

2. sd卡分区模式使用传统的mbr模式, 即前512byte为mbr, main boot record主引导记录。(其中前446字节为引导程序, 后面跟着共四个分区表信息, 每个16字节,  共64字节。 最后以0xaa55的结束符2个字节。)

之后跟着2个分区, 一个为fat32分区,存放bootloader, kernel, uboot env, dtb等等文件。灵一个为ext4分区, 就是根文件系统。

具体分区信息如下:

river@river-VirtualBox:~/image$ fdisk sdimage.img

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk sdimage.img: 576 MiB, 603979776 bytes, 1179648 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x09dcfcef

Device       Boot  Start     End Sectors  Size Id Type
sdimage.img1        2048  133120  131073   64M  b W95 FAT32
sdimage.img2      133121 1179647 1046527  511M 83 Linux

Command (m for help):

3. 启动进入uboot shell

查看mmc 信息

ZynqMP> mmc info
Device: sdhci@ff170000
Manufacturer ID: 3
OEM: 5344
Name: SC16G 
Tran Speed: 50000000
Rd Block Len: 512
SD version 3.0
High Capacity: Yes
Capacity: 14.8 GiB
Bus Width: 4-bit
Erase Group Size: 512 Bytes
ZynqMP> mmc list
sdhci@ff170000: 0 (SD)
ZynqMP> mmc part

Partition Map for MMC device 0  --   Partition Type: DOS

Part	Start Sector	Num Sectors	UUID		Type
  1	2048      	131072    	a69ff1bd-01	0b
  2	133120    	1048576   	a69ff1bd-02	83
ZynqMP>

4. 开发板启动fastboot

ZynqMP> fastboot
fastboot - use USB Fastboot protocol

Usage:
fastboot <USB_controller>
    - run as a fastboot usb device
ZynqMP> fastboot usb0

 

5. windows pc端就能发现USB download gadget设备, 是的fastboot对应的usb设备驱动就是download gadget。

windows pc安装fastboot客户端可参考安卓官网攻略, 相对步骤多点。

ubuntu电脑的话apt软件仓库中就有。 sudo apt-get install fastboot即可。

常用命令有

fastboot reboot

fastboot getvar xxx

fastboot erase <partition_name>

fastboot flash <partition_name> <partition_image>

fastboot flashall

下面演示主机端操作

river@river-VirtualBox:~/image$ fastboot devices 
????????????	fastboot
river@river-VirtualBox:~/image$ fastboot erase mmcsda1 
erasing 'mmcsda1'...
OKAY [  0.135s]
finished. total time: 0.135s
river@river-VirtualBox:~/image$ 
river@river-VirtualBox:~/image$ 
river@river-VirtualBox:~/image$ fastboot flash mmcsda1 boot.img
target reported max download size of 100663296 bytes
sending 'mmcsda1' (65536 KB)...
OKAY [  3.308s]
writing 'mmcsda1'...
OKAY [  4.781s]
finished. total time: 8.089s
river@river-VirtualBox:~/image$

开发板相应log

GUID Partition Table Header signature is wrong: 0x0 != 0x5452415020494645
part_get_info_efi: *** ERROR: Invalid GPT ***
GUID Partition Table Header signature is wrong: 0x0 != 0x5452415020494645
part_get_info_efi: *** ERROR: Invalid Backup GPT ***
Erasing blocks 2048 to 133120 due to alignment
........ erased 67108864 bytes from 'mmcsda1'
request 000000007dee0400 was not queued to ep1in-bulk
WARNING: unknown variable: slot-count
request 000000007dee0400 was not queued to ep1in-bulk
WARNING: unknown variable: slot-suffixes
request 000000007dee0400 was not queued to ep1in-bulk
WARNING: unknown variable: slot-suffixes
request 000000007dee0400 was not queued to ep1in-bulk
WARNING: unknown variable: has-slot:mmcsda1
request 000000007dee0400 was not queued to ep1in-bulk
WARNING: unknown variable: partition-type:mmcsda1
request 000000007dee0400 was not queued to ep1in-bulk
request 000000007dee0400 was not queued to ep1in-bulk
Starting download of 67108864 bytes
request 000000007dee0400 was not queued to ep1in-bulk
..........................................................................
..........................................................................
..........................................................................
..........................................................................
..........................................................................
..........................................................................
....................................................................request 000000007dee0400 was not queued to ep1in-bulk

downloading of 67108864 bytes finished
GUID Partition Table Header signature is wrong: 0x0 != 0x5452415020494645
part_get_info_efi: *** ERROR: Invalid GPT ***
GUID Partition Table Header signature is wrong: 0x0 != 0x5452415020494645
part_get_info_efi: *** ERROR: Invalid Backup GPT ***
Flashing Raw Image
........ wrote 67108864 bytes to 'mmcsda1'
request 000000007dee0400 was not queued to ep1in-bulk

是的, 开发板上挺多警告log, 关于gpt, mbr相关。


6. fastboot 支持gpt分区,  同时也支持mbr分区。

gpt分区: 安卓系统多采用gpt分区形式, 如以下就是其中一个分区例子。

Partition#	Name#	Size#	Strat LBA-End LBR	Description#
0	MBR & GPT	 	0x00000000-0x000000FF	Master Boot Record and GUID Partition Table
1	x-loader(MLO)	256KB	0x00000100-0x000001FF	First stage bootloader
2	bootloader(u-boot.bin)	384KB	0x00000200-0x000005FF	Second stage bootloader
3	misc	128KB	0x00000600-0x000006FF	Reserved.This partition can be used for internal purpose
4	recovery (zImage + recovery-ramdisk.img)	8MB	0x00000A00-0x000049FF	recovery partition
5	boot (boot.img = zImage + ramdisk.img)	8MB	0x00004A00-0x000089FF	Partition contains kernel + ramdisk images.
6	system (system.img)	256MB	0x00008A00-0x000889FF	android file system
7	cache (cache.img)	32MB	0x00088A00-0x000989FF	Partition will be used to store application cache
8	userdata (userdata.img)	32MB	0x00098A00-0x000A89FF	Partition contains userdata supplied by oem vendor like configuration files,utilities etc..
9	media	Remaining	0x000A8A00-0x00762761	Partition contains media files; supplied by oem vendor

可以看到第二行, 可采用partition_name进行分区的擦除烧写等操作。

例如

fastboot erase boot

fastboot flash boot boot.img

fastboot flash system.img

 

关于mbr分区表格式,  由于没有partition_name, 该如何进行分区操作呢?  继续参考uboot/doc/README.android.fastboot

Partition Names                                                                                                                                        
===============                                                                                                                                        
The Fastboot implementation in U-boot allows to write images into disk                                                                                 
partitions (currently on eMMC). Target partitions are referred on the host                                                                             
computer by their names.                                                                                                                               
                                                                                                                                                       
For GPT/EFI the respective partition name is used.                                                                                                     
                                                                                                                                                       
For MBR the partitions are referred by generic names according to the                                                                                  
following schema:                                                                                                                                      
                                                                                                                                                       
  <device type> <device index letter> <partition index>                                                                                                
                                                                                                                                                       
Example: hda3, sdb1, usbda1                                                                                                                            
                                                                                                                                                       
The device type is as follows:                                                                                                                         
                                                                                                                                                       
  * IDE, ATAPI and SATA disks: hd                                                                                                                      
  * SCSI disks: sd                                                                                                                                     
  * USB media: usbd                                                                                                                                    
  * MMC and SD cards: mmcsd                                                                                                                            
  * Disk on chip: docd                                                                                                                                 
  * other: xx                                                                                                                                          
                                                                                                                                                       
The device index starts from 'a' and refers to the interface (e.g. USB                                                                                 
controller, SD/MMC controller) or disk index. The partition index starts                                                                               
from 1 and describes the partition number on the particular device.

所以以上操作中使用的命令为

fastboot erase mmcsda1

fastboot flash mmcsda1 boot.img

fastboot flash mmcsda2 rootfs.img


7. 关于boot.img, rootfs.img这些景象该如何制作

sdimage中是多分区的镜像集合。可以将其拆解开。

我这里依然采用制作sdimage的方法,

sudo dd if=/dev/zero of=boot.img bs=1M count=64M

sync

losetup -f boot.img                  // -f 是find第一个可用的loop回环设备

losetup -a                                // 查看所有loop设备, 假设是/dev/loop1

mkfs.vfat /dev/loop1

sudo mount /dev/loop1 /mnt/boot/

然后拷贝相应的boot文件, 如uboot, kernel, dtb等到该目录。

即可


多镜像分区利用fdisk命令即可。

 

后续利用gdisk, parted等工具制作一个gpt分区的sd卡。并尝试运行。(mbr + gpt)