Skip to main content

编译uboot

编译uboot

0. 交叉编译器

关于交叉编译器的安装,写在另一篇文章里-- 安装交叉编译器

1. 首先下载arm可信任模块的项目

git clone https://github.com/ARM-software/arm-trusted-firmware.git

2. 下载uboot项目

git clone https://github.com/walnutpi/uboot.git

3. 声明所用编译器

export CROSS_COMPILE=aarch64-none-linux-gnu-

4. 先编译可信任模块

cd arm-trusted-firmware
make PLAT=sun50i_h616 DEBUG=1 bl31

5. 进入uboot文件夹,调用配置进行编译

cd ../uboot/
make walnutpi_1b_defconfig
make BL31=../arm-trusted-firmware/build/sun50i_h616/debug/bl31.bin

编译完成后,会在uboot文件夹下会出现一个u-boot-sunxi-with-spl.bin文件

6. 将u-boot-sunxi-with-spl.bin烧录到sd卡起始8k偏移的位置

sudo dd if=u-boot-sunxi-with-spl.bin of=/dev/sda bs=1k seek=8

boot.scr与config.txt

为了实现用config.txt文件来修改一些启动参数,我们把uboot配置成了开机时去寻找并运行boot.scr,然后让这个uboot脚本读取config.txt内的一些变量值进行配置。

1. 创建一个boot.cmd文件,填入以下内容

boot.cmd
# DO NOT EDIT THIS FILE

# default values
setenv load_addr "0x45000000"
setenv overlay_error "false"
setenv rootdev "/dev/mmcblk1p2"
setenv rootfstype "ext4"
setenv console "both"
setenv docker_optimizations "on"
setenv bootlogo "false"

# Print boot source
itest.b *0x10028 == 0x00 && echo "U-boot loaded from SD"
itest.b *0x10028 == 0x02 && echo "U-boot loaded from eMMC or secondary SD"
itest.b *0x10028 == 0x03 && echo "U-boot loaded from SPI"

echo "Boot script loaded from ${devtype}"

if test -e ${devtype} ${devnum} ${prefix}config.txt; then
load ${devtype} ${devnum} ${load_addr} ${prefix}config.txt
env import -t ${load_addr} ${filesize}
fi

if test "${console}" = "display"; then setenv consoleargs "console=ttyS0,115200 console=tty0 console=tty1 "; fi
if test "${console}" = "serial"; then setenv consoleargs "console=ttyS0,115200"; fi
if test "${console}" = "null"; then setenv consoleargs "console=/dev/null"; fi

if test "${bootlogo}" = "true"; then setenv consoleargs "bootsplash.bootfile=bootsplash.armbian ${consoleargs}"; fi

# get PARTUUID of first partition on SD/eMMC it was loaded from
# mmc 0 is always mapped to device u-boot (2016.09+) was loaded from
if test "${devtype}" = "mmc"; then part uuid mmc 0:1 partuuid; fi

setenv bootargs "root=${rootdev} rootwait rw rootfstype=${rootfstype} ${consoleargs}, consoleblank=0 loglevel=${printk_level} ubootpart=${partuuid} usb-storage.quirks=${usbstoragequirks} ${extraargs} ${extraboardargs}"

if test "${docker_optimizations}" = "on"; then setenv bootargs "${bootargs} cgroup_enable=memory swapaccount=1"; fi

load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}${fdtfile}.dtb
fdt addr ${fdt_addr_r}
fdt resize 65536
for overlay_file in ${overlays}; do
if load ${devtype} ${devnum} ${load_addr} ${prefix}overlays/${overlay_prefix}-${overlay_file}.dtbo; then
echo "Applying kernel provided DT overlay ${overlay_prefix}-${overlay_file}.dtbo"
fdt apply ${load_addr} || setenv overlay_error "true"
fi
done
for overlay_file in ${user_overlays}; do
if load ${devtype} ${devnum} ${load_addr} ${prefix}overlay-user/${overlay_file}.dtbo; then
echo "Applying user provided DT overlay ${overlay_file}.dtbo"
fdt apply ${load_addr} || setenv overlay_error "true"
fi
done
if test "${overlay_error}" = "true"; then
echo "Error applying DT overlays, restoring original DT"
load ${devtype} ${devnum} ${fdt_addr_r} ${prefix}${fdtfile}.dtb
else
if load ${devtype} ${devnum} ${load_addr} ${prefix}overlays/${overlay_prefix}-fixup.scr; then
echo "Applying kernel provided DT fixup script (${overlay_prefix}-fixup.scr)"
source ${load_addr}
fi
if test -e ${devtype} ${devnum} ${prefix}fixup.scr; then
load ${devtype} ${devnum} ${load_addr} ${prefix}fixup.scr
echo "Applying user provided fixup script (fixup.scr)"
source ${load_addr}
fi
fi

# load ${devtype} ${devnum} ${ramdisk_addr_r} ${prefix}uInitrd
load ${devtype} ${devnum} ${kernel_addr_r} ${prefix}Image

# booti ${kernel_addr_r} ${ramdisk_addr_r} ${fdt_addr_r}
booti ${kernel_addr_r} - ${fdt_addr_r}

# Recompile with:
# mkimage -C none -A arm -T script -d boot.cmd boot.scr

2. 将boot.cmd编译为boot.scr

mkimage -C none -A arm -T script -d boot.cmd boot.scr

3. 创建一个config.txt文件,填入以下内容

config.txt
bootlogo=false
overlay_prefix=sun50i-h616
fdtfile=sun50i-h616-walnutpi-1b

## 'display' for screen, 'serial' for uart0
console=display

## loglevel
printk_level=3

## Specify HDMI output resolution (eg. extraargs=video=HDMI-A-1:800x480-24@60)
#extraargs=video=HDMI-A-1:1024x600-24@60


#spi : spidev0_0 spidev1_0 spidev1_1 spidev1_2
#i2c : i2c1 i2c2 i2c4
#uart : uart2 uart4
#pwm : pwm1 pwm2 pwm3 pwm4
overlays=spi1 i2c1 i2c2 uart2 pwm1 pwm2 pwm3 pwm4

#------------------------------------------------#
rootfstype=ext4
rootdev=/dev/mmcblk1p2

4. 将文件boot.cmdboot.scrconfig.txt复制到sd卡分区1内。注意:sd卡分区1要设置为可启动

sudo mount /dev/sda1 /mnt

sudo cp boot.cmd /mnt
sudo cp boot.scr /mnt
sudo cp config.txt /mnt

sudo umount /mnt