2014年10月18日 星期六

Banana Pi - 小企鵝的香蕉派食譜

編譯u-boot, linux kernel與script.bin

安裝編譯所需的套件:
$ sudo apt-get install build-essential u-boot-tools binutils-arm-linux-gnueabihf \
gcc-arm-linux-gnueabihf cpp-arm-linux-gnueabihf g++-arm-linux-gnueabihf
安裝usblib套件,因為在編譯sunxi-tool需要這個libary:
$ sudo apt-get install libusb-1.0-0 libusb-1.0-0-dev
安裝其他工具:
$ sudo apt-get install git wget vim libncurses5-dev
下載source code:
$ git clone https://github.com/LeMaker/bananapro-bsp.git
編譯source code:
$ cd bananapro-bsp
$ ./configure BananaPi
$ make
編譯結束後,在build/Bananapi_hwpack資料夾內,就會可以看bootloader與kernel資料夾內編譯好的檔案,還有rootfs內的kernel modules檔案。
$ cd build/BananaPi_hwpack
$ tree
.
├── bootloader
│   ├── sunxi-spl.bin
│   ├── u-boot.bin
│   ├── u-boot.img
│   └── u-boot-sunxi-with-spl.bin
├── kernel
│   ├── script.bin
│   └── uImage
└── rootfs
...
製作一張可開機的SD卡

因為我的SD card是透過USB轉接插在電腦,所以device node是顯示sdb名稱,如果非USB轉接且直接插在電腦,顯示名稱是mmcblk0。當SD插入電腦,desktop版linux作業系統會幫你掛載SD卡,所以你要先用umount指令卸載SD卡。
$ sudo fdisk /dev/sdb
# 或是
$ sudo fdisk /dev/mmcblk0
Command (m for help): o                         #刪除所有partitions
Command (m for help): n                         #建立第一個partition
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1):             #按Enter鍵
Using default value 1
First sector (2048-15466495, default 2048):    #按Enter鍵
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-15466495, default 15466495): +20M

Command (m for help): n                        #建立第二個partition
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (1-4, default 2):             #按Enter鍵
Using default value 2
First sector (43008-15466495, default 43008):  #按Enter鍵
Using default value 43008
Last sector, +sectors or +size{K,M,G} (43008-15466495, default 15466495): 
Using default value 15466495

Command (m for help): w                        #將partition規劃結果寫入SD卡,此時partitions建好了
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
格式化partitions:
$ sudo mkfs.vfat /dev/sdb1
$ sudo mkfs.ext4 /dev/sdb2
將u-boot燒入SD卡:
$ sudo dd if=/dev/zero of=/dev/sdb bs=1k count=1023 seek=1
$ sudo dd if=./bootloader/u-boot-sunxi-with-spl.bin of=/dev/sdb bs=1024 seek=8
掛載第一個partition:
$ sudo mount -t vfat /dev/sdb1 /mnt
在/mnt底下建立uEnv.txt檔案,並將下面內容寫入uEnv.txt:
$ sudo vim /mnt/uEnv.txt
bootargs=console=ttyS0,115200 disp.screen0_output_mode=EDID:1024x768p50 hdmi.audio=EDID:0 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
aload_script=fatload mmc 0 0x43000000 script.bin;
aload_kernel=fatload mmc 0 0x48000000 uImage;bootm 0x48000000;
uenvcmd=run aload_script aload_kernel
複製uImage與script.bin檔案到第一個partition:
$ sudo cp ./kernel/uImage /mnt
$ sudo cp ./kernel/script.bin /mnt
卸載第一個partition:
$ sudo umount /mnt
下載ubuntu desktop rootfs:
$ cd ~/
$ wget https://releases.linaro.org/12.11/ubuntu/precise-images/ubuntu-desktop/\
linaro-precise-ubuntu-desktop-20121124-560.tar.gz
掛載第二個partition:
$ sudo mount -t ext4 /dev/sdb2 /mnt
解壓縮到/mnt底下:
$ sudo tar --strip-components=3 -pzxvf linaro-precise-ubuntu-desktop-20121124-560.tar.gz -C /mnt
複製編譯出來rootfs資料夾的內容到/mnt:
$ sudo cp -r ./bananapro-bsp/build/BananaPi_hwpack/rootfs/* /mnt
設定以太網路eth0介面:
$ sudo vim /mnt/etc/network/interfaces
auto eth0
iface eth0 inet dhcp
$ sudo vim /mnt/etc/NetworkManager/NetworkManager.conf
[ifupdown]
managed=true
先做sync,再卸載第二個partition:
$ sudo sync
$ sudo umount /mnt
接console線

Banana Pi開發版是可以接uart console,所以就買了一條UB-391 PL2303HX USB轉TTL的Console線,新竹光復路上的電子行就有在賣: 將console線接上開發版,順便接了一顆sata介面硬碟,之後會將rootfs搬到硬碟,這樣比較不會lag: 下圖是GPIO放大對照圖,console黑色線接黑色點,綠與白以此類堆:
Note: console紅色線不需要接,千萬千萬不要亂接開發版,不然開發版可能會燒掉,因為紅色線是輸出電壓5V喔!

將SD卡插入開發版準備開機

SD卡好後開機,就會看到ubuntu畫面,如下: 透過console下指令,或是透過HDMI連到螢幕進入ubuntu桌面開啟terminal下指令。先為硬碟切partitions,再格式化partition為ext4格式:
$ sudo fdisk /dev/sda1

Command (m for help): o                       #刪除所有partitions
Building a new DOS disklabel with disk identifier 0xc6fab6a4.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help): n                       #建立一個partition
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1):            #按Enter鍵
Using default value 1
First sector (2048-625142447, default 2048):  #按Enter鍵
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-625142447, default 625142447):
Using default value 625142447

Command (m for help): w                       #將partition規劃結果寫入硬碟,此時partitions建好了

The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

$ sudo mkfs -t ext4 /dev/sda1
將SD卡的rootfs複製到硬碟:
$ sudo mount -t ext4 /dev/sda1 /mnt
$ sudo rsync -ax / /mnt
$ sudo umount /mnt
修改uEnv.txt的boot參數,將mmcblk0p2改為sda1,如下:
$ sudo mount -t vfat /dev/mmcblk0p1 /mnt
$ sudo vim /mnt/uEnv.txt
bootargs=console=ttyS0,115200 disp.screen0_output_mode=EDID:1024x768p50 hdmi.audio=EDID:0 root=/dev/sda1 rootfstype=ext4 elevator=deadline rootwait
aload_script=fatload mmc 0 0x43000000 script.bin;
aload_kernel=fatload mmc 0 0x48000000 uImage;bootm 0x48000000;
uenvcmd=run aload_script aload_kernel
重開機:
$ sudo reboot

沒有留言:

張貼留言