U-Boot常用命令
U-Boot (Universal Boot Loader) 提供了丰富的命令,用于系统引导、固件烧写、内存调试、网络通信等。下面列出并详细解释一些最常用的 U-Boot 命令,这些命令在嵌入式开发和调试中非常关键。
1. 信息查询命令
这些命令用于查看系统硬件和软件信息。
help
或?
- 功能:列出所有支持的命令及其简要说明。
- 用法:
help [command]
(例如help printenv
会显示printenv
命令的详细帮助)
printenv
或pri
- 功能:打印(显示)当前所有的环境变量。环境变量是 U-Boot 工作的基础,定义了启动参数、IP 地址、MAC 地址等关键信息。
- 用法:
printenv [var_name]
(例如printenv bootargs
)
version
- 功能:显示 U-Boot 的版本号、编译时间等信息。
bdinfo
- 功能:显示开发板相关信息,如内存大小、时钟频率等。
2. 环境变量操作命令
环境变量是 U-Boot 的核心配置,控制着其行为。
setenv <var_name> <value>
- 功能:设置或修改一个环境变量的值。
- 用法:
setenv bootargs 'console=ttyS0,115200 root=/dev/mtdblock3'
- 注意:这个修改只在当前会话中有效,掉电会丢失。
saveenv
- 功能:保存当前所有环境变量到 Flash(通常是 NOR/NAND/eMMC/SPI Flash)中。这是让
setenv
的修改永久生效的关键命令。 - 用法:
saveenv
- 功能:保存当前所有环境变量到 Flash(通常是 NOR/NAND/eMMC/SPI Flash)中。这是让
清除uboot环境变量,使用默认环境变量
env default -a
saveenv
3. 内存操作命令
用于读、写、拷贝内存。地址通常用十六进制表示。
md [.b, .w, .l] <address> [# of objects]
- 功能:Memory Display。显示指定内存地址处的内容。
.b
(byte),.w
(word, 2 bytes),.l
(long, 4 bytes) 分别指定显示单位。 - 用法:
md.l 0x30000000 20
(显示从0x30000000
开始的 32 个 long 型数据)
- 功能:Memory Display。显示指定内存地址处的内容。
mm [.b, .w, .l] <address>
- 功能:Memory Modify。修改指定内存地址处的内容,会进入交互模式。
mw [.b, .w, .l] <address> <value> [count]
- 功能:Memory Write。向指定内存地址写入一个值。
- 用法:
mw.l 0x30000000 0x12345678
cp [.b, .w, .l] <source_addr> <dest_addr> <count>
- 功能:Memory Copy。在内存之间拷贝数据。
- 用法:
cp.b 0x30000000 0x31000000 0x1000
4. Flash 操作命令 (NAND/NOR/SPI/eMMC)
用于擦除和读写 Flash 存储器。
nand <command> [args]
(NAND Flash)nand info
: 显示 NAND Flash 的信息。nand device
: 列出或选择当前 NAND 设备。nand erase <offset> <size>
: 擦除指定偏移和大小的区域。在写之前必须先擦除。nand read <mem_addr> <offset> <size>
: 从 NAND 读取数据到内存。nand write <mem_addr> <offset> <size>
: 将内存中的数据写入到 NAND。
sf <command> [args]
(SPI Flash)sf probe
: 探测并初始化 SPI Flash。sf erase <offset> <size>
: 擦除。sf read <mem_addr> <offset> <size>
: 读。sf write <mem_addr> <offset> <size>
: 写。
mmc <command> [args]
(eMMC/SD Card)mmc info
: 显示 MMC 设备信息。mmc rescan
: 重新扫描 MMC 设备。mmc read <mem_addr> <block_start> <block_count>
: 读。mmc write <mem_addr> <block_start> <block_count>
: 写。
5. 网络操作命令
用于通过网络下载文件,通常是内核和文件系统。
ping <ip_address>
- 功能:测试与另一台主机的网络连通性。
tftpboot <mem_addr> <filename>
或tftp <mem_addr> <filename>
- 功能:通过 TFTP 协议,从 TFTP 服务器下载一个文件到指定的内存地址。这是最常用的内核下载方式。
- 用法:
tftpboot 0x30000000 uImage
- 前提:需要先用
setenv
设置好ipaddr
(板子IP),serverip
(服务器IP),gatewayip
(网关)。
NFS
nfs(Network File System)网络文件系统,通过 nfs 可以在计算机之间通过网络来分享资源,比如我们将 linux 镜像和设备树文件放到 Ubuntu 中,然后在 uboot 中使用 nfs 命令将 Ubuntu 中 的 linux 镜像和设备树下载到开发板的 DRAM 中。
这样做的目的是为了方便调试 linux 镜像和设备树,也就是网络调试,通过网络调试是 Linux 开发中最常用的调试方法。原因是嵌入式 linux开发不像单片机开发,可以直接通过 JLINK 或 STLink 等仿真器将代码直接烧写到单片机内部的 flash 中,嵌入式 Linux 通常是烧写到 EMMC、NAND Flash、SPI Flash 等外置 flash 中,但是嵌入式 Linux 开发也没有 MDK,IAR 这样的 IDE,更没有烧写算法,因此不可能通过点击一个“download”按钮就将固件烧写到外部 flash 中。虽然半导体厂商一般都会提供一个烧写固件的软件,但是这个软件使用起来比较复杂,这个烧写软件一般用于量产的。其远没有 MDK、IAR的一键下载方便,在 Linux 内核调试阶段,如果用这个烧写软件的话将会非常浪费时间,而这个时候网络调试的优势就显现出来了,可以通过网络将编译好的 linux 镜像和设备树文件下载到 DRAM 中,然后就可以直接运行。
在使用 之前需要开启 Ubuntu 主机的 NFS 服务,并且要新建一个 NFS 使用的目录,以后所有要通过NFS 访问的文件都需要放到这个 NFS 目录中。
nfs 80800000 192.168.1.253:/home/zuozhongkai/linux/nfs/zImage
命 令 中 的 “ 80800000 ”表示 zImage 保 存在DRAM中的地址 ,“192.168.1.253:/home/zuozhongkai/linux/nfs/zImage”表示 zImage 在 192.168.1.253 这个主机中, 路径为/home/zuozhongkai/linux/nfs/zImage。
6. 启动执行命令
用于启动内核或执行脚本。
bootm <kernel_addr> [ramdisk_addr] [dtb_addr]
- 功能:Boot from Memory。从内存地址启动一个符合 U-Boot uImage 格式的内核。
- 用法:
bootm 0x30000000
- 说明:它会自动解析 uImage 的头部信息,并根据环境变量
bootargs
设置内核启动参数。
go <address>
- 功能:跳转到指定的内存地址开始执行代码,不会传递任何参数。用于执行简单的裸机程序。
run <var_name>
- 功能:执行一个环境变量中定义的命令序列(宏)。
- 用法:
run bootcmd
(执行bootcmd
变量中定义的启动命令)
reset
- 功能:软复位开发板。
常用组合示例
场景:通过网络下载内核并启动
# 1. 设置网络参数
setenv ipaddr 192.168.1.10
setenv serverip 192.168.1.2
setenv gatewayip 192.168.1.1
# 2. 从 TFTP 服务器下载内核到内存 0x30000000
tftpboot 0x30000000 uImage
# 3. 设置内核启动参数
setenv bootargs 'console=ttyS0,115200 root=/dev/nfs rw nfsroot=192.168.1.2:/path/to/rootfs'
# 4. 启动内核
bootm 0x30000000
# 5. (可选) 保存环境变量以备下次使用
saveenv
命令大全
=> ?
? - alias for 'help'
base - print or set address offset
bdinfo - print Board Info structure
bmode - sd1|sd2|qspi1|normal|usb|sata|ecspi1:0|ecspi1:1|ecspi1:2|ecspi1:3|esdhc1|esdhc2|esdhc3|esdhc4 [noreset]
boot - boot default, i.e., run 'bootcmd'
bootd - boot default, i.e., run 'bootcmd'
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
fatinfo - print information about filesystem
fatload - load binary file from a dos filesystem
fatls - list files in a directory (default /)
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
fuse - Fuse sub-system
go - start application at address 'addr'
gpio - query and control gpio pins
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
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
mdio - MDIO utility commands
mii - MII utility commands
mm - memory modify (auto-incrementing address)
mmc - MMC sub system
mmcinfo - display MMC info
mtest - simple RAM read/write test
mw - memory write (fill)
nfs - boot image via network using NFS protocol
nm - memory modify (constant address)
ping - send ICMP ECHO_REQUEST to network host
pmic - PMIC
printenv- print environment variables
protect - enable or disable FLASH write protection
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
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
usb - USB sub-system
usbboot - boot from USB device
version - print monitor, compiler and linker version