文章目录
- 一、前言
- 二、Linux U-boot 相关命令
- (1)help 命令
- (2)printenv 命令
- (3)setenv 函数
- (4)saveenv 函数
- 三、tftp启动linux内核步骤
- (1)进入u-boot模式
- (2)ping通主机
- (3)修改IP
- (4)从网络启动
一、前言
先来讲一下为什么要使用tftp网络启动加载zImage和设备树。
在做驱动开发学习的时候,我们每次涉及到一个学习目标,比如学习led灯设备驱动开发,我们需要去设备树中添加节点,然后进行编译,编译好之后需要将我们的设备树和zImage文件上传到开发板,替换掉开发板上面相对应的文件,然后进行sudo reboot
重新启动。
所以为了方便,可以使用Linux嵌入式uboot使用tftp网络启动加载zImage、设备树。也就是每次开启的时候,会从我们虚拟机tftpboot
路径下去寻找我们的zImage和设备树启动,就不需要每次都下载然后重新reboot了。
如何搭建tftp服务器可以参考这篇文章:wpa_supplicant无线网络配置imx6ull以及搭建tftp服务器
在重启开发板的时候,随意按键即可进入u-boot模式。
二、Linux U-boot 相关命令
- help: printenv命令描述/用法
- printenv: 打印环境变量
- setenv: 设置环境变量
- saveenv: 将环境变量保存到持久存储
接下来我们详细讲解这几个命令。
(1)help 命令
可用于察看当前开发板所支持的命令。
[u-boot@igkboard]# help
? - alias for 'help'
base - print or set address offset
bdinfo - print Board Info structure
blkcache - block cache diagnostics and control
bmode - sd1|sd2|qspi1|normal|usb|sata|ecspi1:0|ecspi1:1|ecspi1:2|ecspi1:3|esdhc1|esdhc2|esdhc3|esdhc4
bmode - getprisec
bmp - manipulate BMP image data
boot - boot default, i.e., run 'bootcmd'
bootd - boot default, i.e., run 'bootcmd'
bootefi - Boots an EFI payload from memory
bootelf - Boot from an ELF image in memory
bootm - boot application image from memory
bootp - boot image via network using BOOTP/TFTP protocol
bootvx - Boot vxWorks from an ELF image
bootz - boot Linux zImage image from memory
clocks - display clocks
cmp - memory compare
coninfo - print console devices and information
cp - memory copy
crc32 - checksum calculation
dcache - enable or disable data cache
dhcp - boot image via network using DHCP/TFTP protocol
dm - Driver model low level access
echo - echo args to console
editenv - edit environment variable
env - environment handling commands
erase - erase FLASH memory
exit - exit script
ext2load - load binary file from a Ext2 filesystem
ext2ls - list files in a directory (default /)
ext4load - load binary file from a Ext4 filesystem
ext4ls - list files in a directory (default /)
ext4size - determine a file's size
ext4write - create a file in the root directory
false - do nothing, unsuccessfully
fastboot - run as a fastboot usb or udp device
fatinfo - print information about filesystem
fatload - load binary file from a dos filesystem
fatls - list files in a directory (default /)
fatmkdir - create a directory
fatrm - delete a file
fatsize - determine a file's size
fatwrite - write file into a dos filesystem
fdt - flattened device tree utility commands
flinfo - print FLASH memory information
fstype - Look up a filesystem type
fstypes - List supported filesystem types
fuse - Fuse sub-system
go - start application at address 'addr'
gpio - query and control gpio pins
gpt - GUID Partition Table
help - print command description/usage
i2c - I2C sub-system
icache - enable or disable instruction cache
iminfo - print header information for application image
imxtract - extract a part of a multi-image
itest - return true/false on integer compare
lcdputs - print string on video framebuffer
ln - Create a symbolic link
load - load binary file from a filesystem
loadb - load binary file over serial line (kermit mode)
loads - load S-Record file over serial line
loadx - load binary file over serial line (xmodem mode)
loady - load binary file over serial line (ymodem mode)
loop - infinite loop on address range
ls - list files in a directory (default /)
md - memory display
mm - memory modify (auto-incrementing address)
mmc - MMC sub system
mmcinfo - display MMC info
mtest - simple RAM read/write test
mw - memory write (fill)
net - NET sub-system
nfs - boot image via network using NFS protocol
nm - memory modify (constant address)
panic - Panic with optional message
ping - send ICMP ECHO_REQUEST to network host
pinmux - show pin-controller muxing
printenv - print environment variables
protect - enable or disable FLASH write protection
qspihdr - Q(F)SPI Boot Config sub-system
random - fill memory with random pattern
read - Load binary data from a partition
reset - Perform RESET of the CPU
run - run commands in an environment variable
save - save file to a filesystem
saveenv - save environment variables to persistent storage
setcurs - set cursor position within screen
setenv - set environment variables
setexpr - set environment variable as the result of eval expression
sf - SPI flash sub-system
showvar - print local hushshell variables
size - determine a file's size
sleep - delay execution for some time
source - run script from memory
test - minimal test like /bin/sh
tftpboot - boot image via network using TFTP protocol
true - do nothing, successfully
ums - Use the UMS [USB Mass Storage]
usb - USB sub-system
usbboot - boot from USB device
version - print monitor, compiler and linker version
videolink - list and select video link
(2)printenv 命令
printenv 命令查看环境变量也可以使用缩写:print
[u-boot@igkboard]# printenv
author=weihuihong
baudrate=115200
bbl=tftp ${loadaddr} u-boot-igkboard.imx && mmc dev ${mmcdev} 1 && mmc write ${loadaddr} 2 0x500
bdtb=tftp $fdt_addr $fdt_file && fatwrite mmc 1:1 $fdt_addr $fdt_file $filesize
bker=tftp $loadaddr $image&& fatwrite mmc 1:1 $loadaddr $image $filesize
board_name=EVK
board_rev=14X14
bootcmd=run mmcbootdto
bootdelay=3
bsys=run bdtb && run bker
console=ttymxc0
dtbo_addr=0x83010000
dtbo_dir=overlays
emmc_ack=1
emmc_dev=1
env_conf=config.txt
eth1addr=be:19:97:40:6d:ff
ethact=ethernet@20b4000
ethaddr=3e:0a:c4:12:ae:e2
ethprime=eth1
fastboot_dev=mmc1
fdt_addr=0x83000000
fdt_file=igkboard.dtb
fdt_size=0x10000
fdtcontroladdr=9de744c0
image=zImage
ipaddr=192.168.137.213
loadaddr=0x80800000
loadenvconf=fatload mmc ${mmcdev}:${mmcpart} ${loadaddr} ${env_conf};env import -t ${loadaddr} ${filesize}
loadfdt=fatload mmc ${mmcdev}:${mmcpart} ${fdt_addr} ${fdt_file}
loadimage=fatload mmc ${mmcdev}:${mmcpart} ${loadaddr} ${image}
mmc_no=1
mmcargs=setenv bootargs console=${console},${baudrate} root=/dev/mmcblk${mmc_no}p2 rootwait rw net.ifnames=0
mmcboot=echo Booting from mmc ...; mmc dev ${mmcdev}; run mmcargs; run loadenvconf;run loadimage; run loadfdt; bootz ${loadaddr} - ${fdt_addr}
mmcbootdto=echo Booting from mmc with overlay...; mmc dev ${mmcdev}; run mmcargs; run loadenvconf; run loadimage; run loadfdt; if env exists dtoverlay_lcd && test ${dtoverlay_lcd} = 1 -o ${dtoverlay_lcd} = yes ; then setenv dtbo_file lcd.dtbo; echo Applying DT overlay ==> ${dtbo_file}; fatload mmc ${mmcdev}:${mmcpart} ${dtbo_addr} ${dtbo_dir}/${dtbo_file}; fdt addr ${fdt_addr}; fdt resize ${fdt_size}; fdt apply ${dtbo_addr}; fi; if env exists dtoverlay_cam && test ${dtoverlay_cam} = 1 -o ${dtoverlay_cam} = yes ; then setenv dtbo_file cam.dtbo; echo Applying DT overlay ==> ${dtbo_file}; fatload mmc ${mmcdev}:${mmcpart} ${dtbo_addr} ${dtbo_dir}/${dtbo_file}; fdt addr ${fdt_addr}; fdt resize ${fdt_size}; fdt apply ${dtbo_addr}; fi; if env exists dtoverlay_i2c1 && test ${dtoverlay_i2c1} = 1 -o ${dtoverlay_i2c1} = yes ; then setenv dtbo_file i2c1.dtbo; echo Applying DT overlay ==> ${dtbo_file}; fatload mmc ${mmcdev}:${mmcpart} ${dtbo_addr} ${dtbo_dir}/${dtbo_file}; fdt addr ${fdt_addr}; fdt resize ${fdt_size}; fdt apply ${dtbo_addr}; fi; if env exists dtoverlay_spi1 && test ${dtoverlay_spi1} = 1 -o ${dtoverlay_spi1} = yes ; then setenv dtbo_file spi1.dtbo; echo Applying DT overlay ==> ${dtbo_file}; fatload mmc ${mmcdev}:${mmcpart} ${dtbo_addr} ${dtbo_dir}/${dtbo_file}; fdt addr ${fdt_addr}; fdt resize ${fdt_size}; fdt apply ${dtbo_addr}; fi; if env exists dtoverlay_uart; then for i in ${dtoverlay_uart}; do setenv dtbo_file uart$i.dtbo; echo Applying DT overlay ==> ${dtbo_file}; fatload mmc ${mmcdev}:${mmcpart} ${dtbo_addr} ${dtbo_dir}/${dtbo_file}; fdt addr ${fdt_addr}; fdt resize ${fdt_size}; fdt apply ${dtbo_addr}; done;fi; if env exists dtoverlay_can; then for i in ${dtoverlay_can}; do setenv dtbo_file can$i.dtbo; echo Applying DT overlay ==> ${dtbo_file}; fatload mmc ${mmcdev}:${mmcpart} ${dtbo_addr} ${dtbo_dir}/${dtbo_file}; fdt addr ${fdt_addr}; fdt resize ${fdt_size}; fdt apply ${dtbo_addr}; done;fi; if env exists dtoverlay_pwm; then for i in ${dtoverlay_pwm}; do setenv dtbo_file pwm$i.dtbo; echo Applying DT overlay ==> ${dtbo_file}; fatload mmc ${mmcdev}:${mmcpart} ${dtbo_addr} ${dtbo_dir}/${dtbo_file}; fdt addr ${fdt_addr}; fdt resize ${fdt_size}; fdt apply ${dtbo_addr}; done;fi; if env exists dtoverlay_extra; then for f in ${dtoverlay_extra}; do setenv dtbo_file $f.dtbo; echo Applying DT overlay ==> ${dtbo_file}; fatload mmc ${mmcdev}:${mmcpart} ${dtbo_addr} ${dtbo_dir}/${dtbo_file}; fdt addr ${fdt_addr}; fdt resize ${fdt_size}; fdt apply ${dtbo_addr}; done;fi; bootz ${loadaddr} - ${fdt_addr}
mmcdev=1
mmcpart=1
netboot=echo Booting from net ...; tftp $loadaddr $image; tftp $fdt_addr ${fdt_file};run mmcargs; bootz ${loadaddr} - ${fdt_addr}
serial#=303769d759a6d668
serverip=192.168.137.91
splashimage=0x8c000000
tee=no
upmode=fastboot 0
Environment size: 4012/8188 bytes
里面的内容也是学长之前写好的或者添加的,直接拿来用就可。
也可以查看某一个环境变量的值:
[u-boot@igkboard]# printenv serverip
serverip=192.168.137.91
(3)setenv 函数
setenv name value 添加/修改环境变量:
[u-boot@igkboard]# setenv serverip 192.168.137.99
修改后:
setenv name 删除环境变量:
[u-boot@igkboard]# setenv serverip
修改后:
(4)saveenv 函数
保存环境变量,将当前定义的所有变量及其值存入flash中。
[u-boot@igkboard]# saveenv
Saving Environment to MMC... Writing to MMC(1)... OK
当设置完环境变量之后,如果执行了saveenv的话,则下次重新运行的时候环境变量则会被保存为上次保存的值;如果不保存,只会在设置好时还没重启之前生效,重启之后设置值会恢复成原来的值。
三、tftp启动linux内核步骤
(1)进入u-boot模式
开始启动的时候在倒数三秒时间按随机键进入到u-boot模式:
Hit any key to stop autoboot: 0
[u-boot@igkboard]#
(2)ping通主机
查看是否ping通tftp所在服务器主机:
[u-boot@igkboard]# ping 192.168.137.91
Using ethernet@20b4000 device
host 192.168.137.91 is alive
提示 xxx alive说明成功,可以ping通。
(3)修改IP
需要修改环境变量中的主机IP和服务器IP,注意,一般服务器IP都是不会变的,主机IP会变,但是主机IP需要在一个网段内就可,不需要每次都去修改的。
[u-boot@igkboard]# setenv ipaddr 192.168.137.99
[u-boot@igkboard]# setenv serverip 192.168.137.91
[u-boot@igkboard]# print ipaddr
ipaddr=192.168.137.99
[u-boot@igkboard]# print serverip
serverip=192.168.137.91
然后我们需要保存一下saveenv
(4)从网络启动
执行命令run netboot
[u-boot@igkboard]# run netboot
Booting from net ...
Using ethernet@20b4000 device
TFTP from server 192.168.137.91; our IP address is 192.168.137.99
Filename 'zImage'.
Load address: 0x80800000
Loading: #################################################################
#################################################################
#################################################################
#################################################################
#################################################################
#################################################################
#################################################################
#################################################################
#################################################################
############################################################
1.4 MiB/s
done
Bytes transferred = 9466232 (907178 hex)
Using ethernet@20b4000 device
TFTP from server 192.168.137.91; our IP address is 192.168.137.99
Filename 'igkboard.dtb'.
Load address: 0x83000000
Loading: ####
1.1 MiB/s
done
说明成功了。
具体实现的代码在配置中写了,但是还不是很清楚,后面学到再补充。