開發與維運

RISC-V ICE EVB 開發板 linux環境下燒寫更新開發板鏡像

本文作者:泓銘


簡介

ICE EVB 是基於平頭哥 RISCV 架構的 C910 CPU 的 ICE 高性能 SoC 開發板。

ICE 是一款通用智能數字 SoC 芯片,主要集了一個雙核RISCV 64位 C910 處理器, 一個 GPU 和 DPU 單元。同時提供了高速接口以及通用外設接口,用於與主控設備間的數據和命令交互。

  • 內嵌平頭哥雙核 [email protected]
  • 支持 DDR4 up to 2400Mbps
  • 支持 GMAC 接口
  • 支持 GPU,支持 3D
  • 支持 RGB888 LCD 顯示,最大支持 1080P

1.png

開發板燒寫鏡像方式

ICE EVB 開發板可以使用 Linux、安卓系統,拿到的開發板出廠會燒寫默認系統,當要更新或更換系統的時候,就需要進行鏡像的燒寫。

燒寫開發板系統,大致分為以下三種情況:

  • 板載系統正常,升級內核鏡像、rootfs 鏡像
  • 板載系統正常,只更新內核、或更改設備樹配置
  • 開發板無法啟動,燒寫 u-boot 鏡像

通常情況下,開發者只需要燒寫、更新內核和 rootfs 鏡像。從 eMMC 啟動的方式,u-boot 鏡像使用 eMMC 的 Boot Area Partition,一般情況不會被內核鏡像和文件系統的用戶數據破壞。

PC 環境準備

在 Linux 環境下燒寫鏡像、應用開發的時候,需要用到一些工具和依賴環境,推薦開發者使用 ubuntu18.04 或 ubuntu20.04 環境開發。本文檔以 ubuntu 18.04 環境為例說明。

  • 安裝工具
sudo apt-get update

# 安裝通用工具
sudo apt-get install git gzip unzip wget

# 串口工具可以使用 minicom 或 picocom
sudo apt-get install minicom
sudo apt-get install picocom

# 安裝 python3 環境
sudo apt-get install python3 python3-pip
# 安裝 fastboot
sudo apt-get install fastboot
# 或者從官網下載二進制文件,
# 下載地址:https://developer.android.google.cn/studio/releases/platform-tools

  • 安裝 thead-tools
    通過 pip 命令來安裝 thead-tools 到你的系統中,thead-tools 支持 python2.7+、python3.6+。
sudo pip install thead-tools

# 如果使用官方地址下載過慢,可使用國內清華鏡像源加速
sudo pip install -i https://pypi.tuna.tsinghua.edu.cn/simple thead-tools

  • 通過 thead-tools 命令可以燒寫 u-boot 鏡像,通過命令 thead cct --help 查使用說明:
root@linux > thead cct --help
Usage: thead cct <uart|list|download> [param]

Options:
  -h, --help            show this help message and exit
  -u UART               CCT serial port device
  -f FILE
  -o OFFSET             Device start address
  -b BLOCK, --block=BLOCK
  -d DEVICE, --device=DEVICE
                        Device name
  -c, --compress
  -D, --debug           Enable debbug trace info

鏡像介紹

訪問 https://gitee.com/thead-linux/ice_images/ 軟件倉庫可以下載最新鏡像,也可直接使用下面命令行下載 Linux SDK 鏡像文件:

wget "https://gitee.com/thead-linux/ice_images/repository/archive/master?format=zip" -O ice_images.zip
unzip ice_images.zip
cd ice_images
ls -l
-rw-r--r--  1 thead  users      11558 Feb 25 11:39 LICENSE
-rw-r--r--  1 thead  users       1214 Feb 25 11:39 README.md
drwxr-xr-x  5 thead  users        160 Feb 25 11:39 boot
-rw-r--r--  1 thead  users   20971520 Feb 25 11:39 boot.ext4
-rw-r--r--  1 thead  users  167772160 Feb 25 11:39 debian-rootfs.ext4
-rw-r--r--  1 thead  users      18652 Feb 25 11:39 ice_ck910_imgwriter_asci_v5.bin
-rw-r--r--  1 thead  users     562536 Feb 25 11:39 u-boot-with-spl.bin

需要用到的是如下三個文件:

  • u-boot-with-spl.bin: u-boot 引導鏡像文件
  • boot.ext4: /boot 分區文件系統,ext4格式,包含 fw_jump.bin、uImage、hw.dtb
  • debian-rootfs.ext4: Debian 基礎根文件系統鏡像,ext4 格式
  • boot/: boot.ext4 鏡像中的文件,直接更新內核、dtb 時候可以使用

燒寫 boot 分區鏡像

1.按照如下系統連接圖連接好開發板:

2.png

※ USB轉TTL 串口設備使用常用的 PL2303*、CH340*、CP2102* 等均可。

2.啟動串口工具minicom 或 picocom

    • minicom
minicom -c on -b 115200 -D /dev/ttyUSB0
# 如無法啟動 minicom,可以加上 sudo 執行或設置用戶組權限。

※ 退出 minicom:CRTL+AX

      • -c on :打開顏色顯示
      • -b 115200 :ICE-EVB 串口控制檯的默認波特率為:115200 bps
      • -D :指令PC上與ICE-EVB 開發板連接的串口設備,使用時要注意串口設備是否正確

    • picocom
# 或使用 picocom
picocom -b 115200 -f n -r -l /dev/ttyUSB0

※ 退出 picocom:CTRL + ACTRL + X

3.開發板開機後進入 u-boot,當 Console 串口出下提示時,按任鍵即可進行命令模式。

Warning: ethernet@3fffc0000 (eth0) using random MAC address - a6:7d:bc:02:7d:4d
eth0: ethernet@3fffc0000
Hit any key to stop autoboot:  3    <--- 出現此信息時按任意鍵進入u-boot

4.u-boot 環境配置
u-boot 配置命令列表如下(通過串口終端,在開發板上執行):

# 恢復默認值
env default -a

# 配置 eMMC 分區
setenv uuid_rootfs "80a5a8e9-c744-491a-93c1-4f4194fd690b"
setenv partitions "name=table,size=2031KB"
setenv partitions "$partitions;name=boot,size=60MiB,type=boot"
setenv partitions "$partitions;name=root,size=-,type=linux,uuid=$uuid_rootfs"
gpt write mmc 0 $partitions

# 配置網絡
# !!!!!!!!!!!!!!!!!!!! 特別注意 !!!!!!!!!!!!!!!!!!!!!!!!
# 如果網絡中接入多塊 ICE-EVB 開發板,每塊開發板的 ethaddr 要唯一
setenv ethaddr 00:a0:a0:a0:a0:a1   # 如果網絡中接入多塊 ICE-EVB 開發板,ethaddr 要唯一
setenv ipaddr 192.168.1.100        # 開發板 IP 地址
setenv netmask 255.255.255.0
# ping 192.168.1.1                 # ping 主機的IP,可檢驗網絡是否正常
saveenv                            # 將配置保存到 eMMC, 下次啟動時無需再配置

# 開啟fastboot模式
fastboot udp

printenv: 可查看 u-boot 環境變量

5.燒寫 boot 分區
在 Linux 終端上使用 fastboot 命令完成鏡像燒寫,192.168.1.100 是開發板的運行 u-boot 時配置的 IP 地址,使用 fastboot 命令前確認電腦與開發板在同一個子網。在電腦的終端上執行:

 fastboot -s udp:192.168.1.100 flash boot boot.ext4

6.調整分區空間
重啟開發板進入系統,執行 resize2fs 命令調整分區空間:

cd /boot
resize2fs /dev/mmcblk1p2

# 調整後分區
root@thead-910:/boot# df -h
Filesystem      Size  Used Avail Use% Mounted on
...(省略)
/dev/mmcblk1p2   56M   14M   41M  25% /boot

燒寫 rootfs 分區鏡像

  • 1 ~ 4:與《燒寫 boot 分區鏡像》 一致。
  • 5.燒寫 rootfs 鏡像
    ICE-EVB 開發板提供三種根文件系統鏡像,開發者可以根據自己的需要選擇下載:

選項一:燒寫基本系統鏡像
在下載的 ice_images 中默認提供 debian-rootfs.ext4 鏡像,使用 fastboot 命令燒寫:

fastboot -s udp:192.168.1.100 -S 5M flash root debian-rootfs.ext4

選項二:燒寫開發者鏡像
需要另外下載 debian-dev-rootfs.ext4 鏡像,再用 fastboot 命令燒寫:

wget "http://mirrors.aliyun.com/thead/images/riscv64-ice/debian-dev-rootfs.ext4.gz"
gzip -d debian-dev-rootfs.ext4.gz
fastboot -s udp:192.168.1.100 -S 5M flash root debian-dev-rootfs.ext4

選項三:燒寫 xFce 桌面系統鏡像
需要另外下載 debian-desktop-rootfs.ext4 鏡像,再用 fastboot 命令燒寫:

wget "http://mirrors.aliyun.com/thead/images/riscv64-ice/debian-desktop-rootfs.ext4.gz"
gzip -d debian-desktop-rootfs.ext4.gz
fastboot -s udp:192.168.1.100 -S 5M flash root debian-desktop-rootfs.ext4

6.調整分區空間
重啟開發板進入系統,執行 resize2fs 命令調整分區空間:

resize2fs /dev/mmcblk1p3

# 調整後分區
root@thead-910:/boot# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/root       3.6G  421M  3.0G  13% /
...(省略)

更新內核、dtb 配置

如果只更新內核、更改設備樹配置,可以直接對操作 /boot  下文件進行更新。

1.確保開發板正確聯網,在開發板 Console 執行 ifconfig 查看開發板 IP 地址:

root@thead-910:~# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.100  netmask 255.255.255.0  broadcast 192.168.1.1
        inet6 fe80::2a0:a0ff:fea0:a0a1  prefixlen 64  scopeid 0x20<link>
        ether 00:a0:a0:a0:a0:a1  txqueuelen 1000  (Ethernet)
        RX packets 48  bytes 12429 (12.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 50  bytes 7348 (7.1 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 7  base 0xa000
        
lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

※ 配置開發板 IP 地址,可執行 dhclient 自動獲取 IP 地址,或對  /etc/network/interfaces 進行配置。

2.確保開發板上已經配置好登錄密碼或已經上傳公鑰

# 更改密碼
root@thead-910:~# passwd
New password:
Retype new password:
passwd: password updated successfully

3.scp 上傳 uImage 文件,在 PC 上操作:

 scp uImage [email protected]:/boot/.

※ 注意:如果上傳內核鏡像文件錯誤,會造成系統無法啟動,此時需要參考上面步驟,重新燒寫 boot 分區鏡像文件。

4.更改設備樹 dtb 配置

# 安裝 dtc 工具
apt update
apt install device-tree-compiler

# 修改設備樹配置
cd /boot
dtc -I dtb -O dts hw.dtb > hw.dts
# 使用 vi 或其他 editor 修改 hw.dts 配置
# 修改後生成新的 dtb
dtc -I dts -O dtb hw.dts > hw.dtb

sync

5.開發板重啟

root@thead-910:~# sync
root@thead-910:~# reboot

燒寫 u-boot 鏡像

當開發板上 u-boot 鏡像異常,造成無法啟動,俗稱“變磚”的時候,就需要通過串口,先將引導程序載入到內存,正常引導後,再將固件鏡像燒寫到 eMMC 上。通過使用 python 環境下的 thead 命令可自動完成以上操作,將 u-boot 鏡像燒寫到 eMMC 的Boot Area Partition 分區。

  • 按照如下CCT燒寫連接圖連接好開發板:

3.png

  • 查看系統環境和串口
    通過 thead-tools 燒寫 u-boot 時需要使用開發板的 CCT 燒寫串口,通過命令thead cct uart 命令看到電腦上安裝的串口列表,並確認電腦上串口與開發板串的對應關係:
root@linux > thead cct uart
uart device list:
   /dev/ttyUSB0 - USB-Serial Controller

如果出現上面狀態,說明 thead 工具安裝正常,串口設備連接正常。

  • 查找開發板存儲器列表
    通過命令:thead cct -u /dev/ttyUSB0 list 可以查看開發板支持的燒寫存儲器列表,如下:
root@linux > thead cct -u /dev/ttyUSB0 list
Wait .....................
CCT Version: 2
memory device list:
  dev = ram0   , size =  256.0KB
  dev = emmc0  , size =    2.0MB
  dev = emmc1  , size =    2.0MB
  dev = emmc2  , size =    3.7GB

注意: 使用該命令時,先確定串口的連接是否正確,運行命令前先關閉開發板電源後再運行該命令,等到出現Wait .......... 信息後再開啟開發板電源。

如果未出現上述打印信息,先確認連線和開發板電源狀態,同時電腦是接有多個串口,請更新另一個串口再次嘗試。

  • 燒寫 u-boot
    通過命令:thead cct -u /dev/ttyUSB0 download -f u-boot-with-spl.bin -d emmc0 將 u-boot 鏡像燒寫到 eMMC 的 Boot Area Partition 分區,燒寫過程信息如下:
CCT Version: 2
Send file 'u-boot-with-spl.bin' to 21:0 ...
Writing at 0x00009800... (3%)

待打印 File u-boot-with-spl.bin download success. 後燒寫成功。

啟動登錄 Linux 系統

完成 Linux 鏡像燒寫後,重新啟動 ICE EVB 開發板,開發板 Console 串口顯示如下:

[  OK  ] Started Serial Getty on hvc0.
[  OK  ] Started Serial Getty on ttyS0.
[  OK  ] Reached target Login Prompts.
[  OK  ] Started System Logging Service.
[  OK  ] Finished Remove Stale Onli…ext4 Metadata Check Snapshots.
[  OK  ] Reached target Multi-User System.
[  OK  ] Reached target Graphical Interface.
         Starting Update UTMP about System Runlevel Changes...
[  OK  ] Finished Update UTMP about System Runlevel Changes.
Debian GNU/Linux bullseye/sid thead-910 ttyS0
thead-910 login: [   94.362252] random: crng init done
[   94.365674] random: 6 urandom warning(s) missed due to ratelimiting
thead-910 login:

登錄用戶名 root,初始無密碼,開發者可根據需要更改密碼。

Leave a Reply

Your email address will not be published. Required fields are marked *