天天看點

imx515 uboot UBIFS移植及android UBIFS檔案系統燒寫

作者 :longfeey

1.1        Uboot UBI的移植

關于uboot的UBI的移植幾乎沒有說明介紹,移植首先要保證你的flash驅動能夠跑起來,我是在nand flash 上跑的UBI。剛開始的時候我也沒有什麼頭緒,隻能夠從uboot的readme開始查找一些蛛絲馬迹。

- MTD Support (mtdparts command, UBI support)

              CONFIG_MTD_DEVICE

              Adds the MTD device infrastructure from the Linux kernel.

              Needed for mtdparts command support.

              CONFIG_MTD_PARTITIONS

              Adds the MTD partitioning infrastructure from the Linux

              kernel. Needed for UBI support.

是以,要UBI支援首先得要MTD支援,是以在配置檔案中要添加以上兩項的定義。

要移植UBI還要添加:

#define CONFIG_CMD_UBIFS           

#define CONFIG_CMD_UBI         

總的關于UBI的部分是以下幾個宏:

#define CONFIG_CMD_UBI

#define CONFIG_CMD_UBIFS

#define CONFIG_CMD_MTDPARTS

#define CONFIG_MTD_DEVICE

#define CONFIG_MTD_PARTITIONS

#define CONFIG_RBTREE

#define CONFIG_LZO 

同時要給NAND建立個預設的分區,友善以後操作。分區如下:

#define MTDIDS_DEFAULT "nand0=nand0"

#define MTDPARTS_DEFAULT "mtdparts=nand0:[email protected](u-boot),[email protected](kernel),[email protected](rootfs),-(reserved)"

#define MTD_ACTIVE_PART "nand0,2"

以上的配置都在uboot_imx/include/configs/mx51_vdphone.h檔案中進行配置。

需要注意的是增加UBI的支援之後uboot會增大到300多KB,在NAND中啟動,需要修改以下檔案uboot-imx/cpu/arm_cortexa8/mx51/mxc_nand_load.S 

      add r6, r0, #0x1E00

      ldr r5, =_end             

      add r5, r2, #0x00060000  

這裡使用0x60000(384K)大小,已經足夠,如果實際有變化,可以進行相應調節。如果uboot傳給Copy_Good_Blk 拷貝的uboot的大小小于uboot的長度的話,uboot跑不起來,移植的時候被這個問題必須注意。

這個時候就可以make 了,執行以下指令:

make clean

make mx51_vdphone_config

make all

如果正常的話會編譯出u-boot.bin在根目錄下。

1.2       u-boot 下ubi的使用

1.2.1        配置u-boot nand 分區

通過mtdpart指令配置u-boot下的nand 分區,本項目已經在配置頭檔案裡面設定了預設nand 分區,

#define MTDPARTS_DEFAULT "mtdparts=nand0:[email protected](u-boot),[email protected](kernel),[email protected](rootfs),-(reserved)"

如果需要修改,可以通過修改預設分區清單,也可以通過指令mtdpart進行重新分區。這裡使用預設分區,通過以下指令使預設分區生效:

       mtdpart default       //設定預設分區

       saveevn             //儲存分區資訊

1.2.2        nand u-boot 燒寫

通過以上的配置編譯,如果成功生成u-boot.bin,那就可以通過SD卡啟動,直接燒寫u-boot.bin到nand flash了。操作步驟如下:

1)         下載下傳u-boot.bin 到記憶體

tftp 0x90800000 /tftpboot/mx51/u-boot.bin

2)         擦除u-boot分區

nand erase u-boot

3)         燒寫u-boot到nand flash分區

nand write 0x90800000 u-boot 0x60000

1.2.3        核心的燒寫

 核心的燒寫和平常燒寫方式一樣,隻需用nand 指令寫入nand 即可,操作步驟如下:

1)       擦除kernel分區

nand erase kernel

2)       下載下傳kernel到記憶體

tftp 0x90800000 /tftpboot/mx51/uImage  将核心通過tftp下載下傳到記憶體中

3)       燒寫kernel 到nand kernel分區

nand write 0x90800000 kernel 0x300000 

1.2.4        UBI檔案系統的燒寫

本項目使用的檔案系統将根檔案系統和system檔案系統整合在一起。是以,隻需要燒寫整合後的檔案系統即可。如果要使用ubifs檔案系統作為根檔案系統,在燒寫之前必須通過mkfs.ubifs工具将做好的檔案系統制作鏡像檔案。mkfs.ubifs 工具是通過編譯mtd-utils工具下的mkfs.ubifs目錄即可生成的PC端UBIFS檔案系統鏡像制作工具。操作步驟如下:

1)       制作根檔案系統

mkfs.ubifs -r root/ -m 2048 -e 129024 -c 2364 -o root-fs.img

root目錄為整合android root和system檔案系統後的目錄,應當能夠通過NFS系統的

2)       擦除root分區

 nand erase root

3)      激活root 分區為UBI格式

 ubi part root

             4)    建立root分區

ubi create root

5)       将檔案系統下載下傳到記憶體

 tftp 0x90800000 root-fs.img

6)       将檔案系統燒寫到rootfs 中

ubi write 0x90800000 rootfs 0x339600//0x339600為tftp 下載下傳到的root-fs.img鏡像大小,

1.2.5         設定啟動參數

設定bootargs:

setenv bootargs ubi.mtd=2 root=ubi0:rootfs rootfstype=ubifs console=ttymxc0,115200 wvga calibration init=/init rw

啟動撥碼開關5,8位置設定為ON,上電重新啟動,即可從Nand flash 啟動。

               1.3       android FLASH UBI檔案系統的制作和燒寫

将android編譯為UBI檔案系統格式,生成的system.img,userdata.img,recover.img就可以直接在u-boot中通過ubi write 指令燒寫,前提條件是uboot已經支援或完成ubi和UBIFS的移植工作,并且linux kernel也要支援UBIFS檔案系統。

              1.3.1   設定mtdpart分區

             1)    U-Boot中配置預設分區參數,路徑如下:

# 闆級相關的配置檔案include/configs/mx51/xxxx.h 

mtdparts: mtdparts=nand0:[email protected](u-boot),[email protected](kernel),[email protected](ramdisk),[email protected](system),[email protected](userdata),[email protected](cache),-(reserved)

            2)     第一次燒寫完boot後,設定mtdpart分區:

BBG U-Boot > mtdparts default # 加載預設分區配置

BBG U-Boot > save # 儲存配置

BBG U-Boot > mtdpart # 檢視分區配置

device nand0 <nand0>, # parts = 7

 #: name                size            offset          mask_flags

 0: u-boot              0x00100000      0x00000000      0

 1: kernel              0x00300000      0x00120000      0

 2: ramdisk             0x00100000      0x00420000      0

 3: system              0x04b00000      0x00520000      0

 4: userdata            0x01e00000      0x05020000      0

 5: cache               0x00d00000      0x06e20000      0

 6: reserved            0x004e0000      0x07b20000      0

active partition: nand0,0 - (u-boot) 0x00100000 @ 0x00000000

defaults:

mtdids  : nand0=nand0

               3)    燒寫U-Boot到FLASH

BBG U-Boot > tftp 0x90800000 u-boot.bin # 擷取U-Boot到記憶體

BBG U-Boot > nand erase u-boot # 格式化u-boot分區

BBG U-Boot > nand write 0x90800000 u-boot 0x100000 # 燒寫u-boot到對應分區

              4)    燒寫Linux核心到FLASH

BBG U-Boot > tftp 0x90800000 uImage # 擷取核心到記憶體

BBG U-Boot > nand erase kernel # 格式化記憶體分區

BBG U-Boot > nand write 0x90800000 kernel 0x300000 # 燒寫核心到對應分區

              5)    燒寫Ramdisk到FLASH

BBG U-Boot > tftp 0x90800000 uramdisk.img # 擷取uramdisk到記憶體

BBG U-Boot > nand erase ramdisk # 格式化uramdisk分區

BBG U-Boot > nand write 0x90800000 ramdisk 0x100000 # 燒寫uramdisk到對應分區          

              6)    燒寫System到FLASH

BBG U-Boot > nand erase system # 擦除system分區

BBG U-Boot > tftp 0x90800000 system.img # 擷取system到記憶體

BBG U-Boot > ubi part system # 激活system分區為ubi格式

Creating 1 MTD partitions on "nand0":

0x000097855f98-0x000000520000 : "<NULL>"

UBI: attaching mtd1 to ubi0

UBI: physical eraseblock size:   131072 bytes (128 KiB)

UBI: logical eraseblock size:    129024 bytes

UBI: smallest flash I/O unit:    2048

UBI: sub-page size:              512

UBI: VID header offset:          512 (aligned 512)

UBI: data offset:                2048

UBI: attached mtd1 to ubi0

UBI: MTD device name:            "mtd=3"

UBI: MTD device size:            78643200 MiB

UBI: number of good PEBs:        600

UBI: number of bad PEBs:         0

UBI: max. allowed volumes:       128

UBI: wear-leveling threshold:    4096

UBI: number of internal volumes: 1

UBI: number of user volumes:     0

UBI: available PEBs:             590

UBI: total number of reserved PEBs: 10

UBI: number of PEBs reserved for bad PEB handling: 6

UBI: max/mean erase counter: 1/1

BBG U-Boot > ubi create system # 建立system分區

Creating dynamic volume system of size 76124160

# 燒寫sytem分區,大小為tftp下載下傳完成後提示的大小

BBG U-Boot > ubi write 0x90800000 system 0x3ca9800

Volume "system" found at volume id 0

               7)     燒寫userdata到FLASH

BBG U-Boot > nand erase userdata # 擦除userdata分區

BBG U-Boot > ubi part userdata # 激活userdata分區為ubi格式

UBI: mtd1 is detached from ubi0

Creating 1 MTD partitions on "nand0":

0x000097855f98-0x000005020000 : "<NULL>"

UBI: attaching mtd1 to ubi0

UBI: physical eraseblock size:   131072 bytes (128 KiB)

UBI: logical eraseblock size:    129024 bytes

UBI: smallest flash I/O unit:    2048

UBI: sub-page size:              512

UBI: VID header offset:          512 (aligned 512)

UBI: data offset:                2048

UBI: empty MTD device detected

UBI: create volume table (copy #1)

UBI: create volume table (copy #2)

UBI: attached mtd1 to ubi0

UBI: MTD device name:            "mtd=4"

UBI: MTD device size:            31457280 MiB

UBI: number of good PEBs:        240

UBI: number of bad PEBs:         0

UBI: max. allowed volumes:       128

UBI: wear-leveling threshold:    4096

UBI: number of internal volumes: 1

UBI: number of user volumes:     0

UBI: available PEBs:             234

UBI: total number of reserved PEBs: 6

UBI: number of PEBs reserved for bad PEB handling: 2

UBI: max/mean erase counter: 0/0

BBG U-Boot > ubi create userdata # 建立userdata分區

Creating dynamic volume userdata of size 30191616

BBG U-Boot > tftp 0x90800000 userdata.img # 擷取userdata到記憶體

# 燒寫userdata分區,大小為tftp下載下傳完成後提示的大小

BBG U-Boot > ubi write 0x90800000 userdata 0x979800

Volume "userdata" found at volume id 0

              8)    初始化Cache分區

BBG U-Boot > ubi part cache # 激活cache分區為ubi格式

UBI: mtd1 is detached from ubi0

Creating 1 MTD partitions on "nand0":

0x000097855f98-0x000006e20000 : "<NULL>"

UBI: attaching mtd1 to ubi0

UBI: physical eraseblock size:   131072 bytes (128 KiB)

UBI: logical eraseblock size:    129024 bytes

UBI: smallest flash I/O unit:    2048

UBI: sub-page size:              512

UBI: VID header offset:          512 (aligned 512)

UBI: data offset:                2048

UBI: empty MTD device detected

UBI: create volume table (copy #1)

UBI: create volume table (copy #2)

UBI: attached mtd1 to ubi0

UBI: MTD device name:            "mtd=5"

UBI: MTD device size:            13631488 MiB

UBI: number of good PEBs:        104

UBI: number of bad PEBs:         0

UBI: max. allowed volumes:       128

UBI: wear-leveling threshold:    4096

UBI: number of internal volumes: 1

UBI: number of user volumes:     0

UBI: available PEBs:             98

UBI: total number of reserved PEBs: 6

UBI: number of PEBs reserved for bad PEB handling: 2

UBI: max/mean erase counter: 0/0

BBG U-Boot > ubi create cache   # 建立cache分區

                9)     FLASH上Android的加載與啟動

設定啟動參數

setenv bootcmd_nand 'run bootargs_nand;nand read ${loadaddr} kernel; nand read ${rd_loadaddr} ramdisk; bootm ${loadaddr} ${rd_loadaddr}'

setenv bootargs_nand 'setenv bootargs ubi.mtd=3 ubi.mtd=4 ubi.mtd=5 console=ttymxc0,115200 androidboot.console=ttymxc0 wvga calibration init=/init rw'

setenv bootcmd 'run bootcmd_nand'

saveenv

重新開機即可從nand flash 啟動燒寫的ubi檔案系統

繼續閱讀