樹莓派添加DTS功能

交叉編譯工具鏈:arm-bcm2708-linux-gnueabi-

u-boot:u-boot-2015.10(http://www.denx.de/wiki/U-Boot/SourceCode)

Linux內核:linux-rpi-4.1.y (https://github.com/raspberrypi/linux/tree/rpi-4.1.y)

硬件單板:樹莓派b


本文基於u-boot-2005和linux4.1.y,使用最新的DTS方式從外接U盤來引導樹莓派b的Linux系統。


1 環境準備

1.1 u盤分區

樹莓派的啓動使用到了SD卡。這裏爲了調試方便,使用u-boot和U盤來引導Linux系統啓動。首先準備一個U盤,並進行分區和格式化。具體步如下:

[apple@apple ~]$ sudofdisk /dev/sdb
WARNING: DOS-compatible mode is deprecated. It's stronglyrecommended to
         switch off the mode(command 'c') and change display units to
         sectors (command'u').
 
Command (m for help): p                                   #查看現有分區
 
Disk /dev/sdb: 8004 MB, 8004304896 bytes
255 heads, 63 sectors/track, 973 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xcad4ebea
 
   Device Boot      Start         End      Blocks  Id  System
/dev/sdb4   *           1         974    7816576    c W95 FAT32 (LBA)
Partition 4 has different physical/logical endings:
     phys=(972, 254, 63)logical=(973, 34, 21)
 
Command (m for help): d                                   #刪除分區
Selected partition 4
 
Command (m for help): n                                   #添加第一個分區
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1020,default 1): 1
Using default value 1
Last cylinder, +cylindersor +size{K,M,G} (1-1020, default 1020): 20
 
Command (m for help): n                                   #添加第二個分區
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (21-1020,default 21): 21
Last cylinder, +cylindersor +size{K,M,G} (21-1020, default 1020): 1020
 
Command (m for help): p                                   #再次查看分區信息
 
Disk /dev/sdb: 8004 MB, 8004304896 bytes
247 heads, 62 sectors/track, 1020 cylinders
Units = cylinders of 15314 * 512 = 7840768 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xcad4ebea
 
Device Boot      Start         End      Blocks  Id  System
/dev/sdb1               1          20      153109  83  Linux
/dev/sdb2              21        1020    7657000   83  Linux
 
Command (m for help): w                                   #寫入分區
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.
 
分區完成後查看/dev/目錄下產生了兩個分區:
[apple@apple ~]$ ls/dev/sdb*

/dev/sdb  /dev/sdb1  /dev/sdb2

1.2 分區格式化
第一個分區sdb1用來存放u-boot需要加載的內核Image以及dtb文件,格式化爲vfat文件系統;第二個分區sdb2用來存放根文件系統,格式化爲ext4文件系統。具體步驟如下:
[apple@apple ~]$ sudo mkfs.vfat /dev/sdb1
[apple@apple ~]$ sudo mkfs.ext4 /dev/sdb2
 
2 安裝u-boot
2015.10版的u-boot已經對樹莓派有了較好的支持,不僅可以從SD卡加載引導內核,還可以從U盤和網絡加載引導,因此選用該版本的u-boot。
2.1 編譯u-boot
[apple@applebuild]$ tar xvf ../source/u-boot-2015.10.tar.bz2
在u-boot根目錄的configs已經包含了樹莓派的默認配置文件:
 [apple@apple configs]$ ls rpi*
rpi_2_defconfig  rpi_defconfig
 

這裏直接使用rpi_defconfig即可,在u-boot根目錄下編寫一個簡單的編譯腳本:

[[email protected]]$ vi build.sh
#!/bin/sh
make distclean
make rpi_config
make all

[[email protected]]$ chmod +x build.sh


然後執行編譯(注意添加環境變量CROSS_COMPILE指定交叉編譯工具):
[[email protected]]$ ./build.sh
最後在根目錄下生成u-boot.bin即是u-boot的二進制執行文件,將他放在SD卡的fat目錄下即可引導啓動(具體見《樹莓派的啓動引導方式》和《移植u-boot到樹莓派》)。

2.2 配置u-boot環境變量
從u-boot的啓動打印信息中可以看出,它會嘗試從SD卡中獲取u-boot配置文件"uboot.env"並讀取其中的環境變量如果讀取失敗則使用默認的環境變量。該環境變量可以通過setenv和saveenv命令修改。
由於使用的是U盤引導,所以需要首先使能usb,同時掃描usb設備並讀取內核image和dtb文件,所以修改環境變量如下並保存:
U-Boot> setenv bootcmd "usb start;fatload usb 0:1 ${fdt_addr_r}/bcm2708-rpi-b.dtb;fatload usb 0:1 ${loadaddr} /uImage;bootm ${loadaddr} -${fdt_addr_r}"
U-Boot> setenv ipaddr 192.168.0.20
U-Boot> setenv serverip 192.168.0.17
U-Boot> setenv bootargs "console=ttyAMA0,115200kgdboc=ttyAMA0,115200 root=/dev/sda2 rootfstype=ext4 rootwait"
U-Boot> saveenv
這裏配置了內核的啓動參數和引導命令,首先啓動usb並掃描外掛器件,然後加載DTB和內核,最後引導內核啓動。同時這裏的bootargs參數會在引導啓動時修改DTB中的chosen項並寫入。
保存後就會在SD卡的fat目錄下生成uboot.env文件了,下次啓動u-boot時就可以直接讀取了。注意不可以直接修改該文件,因爲在uboot讀取該文件時會做CRC校驗。
        
3  內核及dtb編譯
內核選用樹莓派Linux4.1.12+版本內核,已經對LinuxDevice Tree有較好的支持了。
3.1 下載並配置內核
首先從github上下載內核4.1.y的分支:
[apple@apple RpiSource]$ sudo git clonehttps://github.com/raspberrypi/linux.git /linux-rpi-4.1.y
下載完成後啓動配置,在arch/arm/configs/目錄下包含了bcmrpi_defconfig,直接使用即可(指定交叉編譯工具)。
[[email protected]]$ make bcmrpi_defconfig

[[email protected]]$ make menuconfig

查看如下配置,內核已經支持DTS:
General setup  --->
(arm-linux-gnueabihf-)Cross-compiler tool prefix
System Type  --->
ARM system type (Broadcom BCM2708 family)  --->                                           

Broadcom BCM2708 Implementations  --->

[*]   BCM2708 DeviceTree support

Bootoptions  --->
 -*-Flattened Device Tree support
[*]   Support for the traditional ATAGS boot datapassing
上面配置文件中勾選了BCM2708Device Tree support,在內核編譯dtb時會默認編譯/arch/arm/boot/dts/bcm2708-rpi-*.dtb(詳細可見Makefile)。
 
3.2      編譯內核和DTB
創建目標生成目錄,該目錄用於存放編譯後的內核鏡像文件和內核:
[[email protected]]$ mkdir release
編寫編譯腳本build.sh:
#!/bin/sh
make uImage -j4
make dtbs
cp arch/arm/boot/uImage release/
cp arch/arm/boot/zImage release/
cp arch/arm/boot/dts/bcm2708-rpi-b.dtb release/
執行腳本編譯,編譯完成後在release目錄下生成了uImage和bcm2708-rpi-b.dtb文件。將他們拷貝到U盤的第一個fat分區中,然後啓動樹莓派,uboot引導內核啓動時輸出打印如下:
reading /bcm2708-rpi-b.dtb
10212 bytes read in 26 ms (382.8 KiB/s)
reading /uImage
8115848 bytes read in 4252 ms (1.8 MiB/s)
## Booting kernel from Legacy Image at 00200000 ...
   Image Name:   Linux-4.1.15
   Image Type:   ARM Linux Kernel Image (uncompressed)
   Data Size:    8115784 Bytes = 7.7 MiB
   Load Address: 00008000
   Entry Point:  00008000
   Verifying Checksum ... OK
## Flattened Device Tree blob at 00000100
   Booting using the fdt blob at0x000100
   Loading Kernel Image ... OK
   Loading Device Tree to07b42000, end 07b477e3 ... OK
 
Starting kernel ...
 
Uncompressing Linux... done, booting the kernel.
[    0.000000] Booting Linuxon physical CPU 0x0
[    0.000000] Initializingcgroup subsys cpuset
[    0.000000] Initializingcgroup subsys cpu
……
至此內核啓動成功。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章