uboot 修改代码 增加 环境变量

--- title: uboot修改代码增加环境变量 date: 2019-12-27 21:26:39 categories: tags: - uboot ---

以"tftp下载kernel和自动挂载NFS根文件系统" 为例。

背景

为了开发的方便我们一般都会有这么一个需求:

  1. uboot启动不做任何操作从nand启动

  2. 执行一个简单的命令, 从网络下载内核并启动, 启动之后自动挂接NFS根文件系统

为了实现上述的需求我们就不能修改nand启动的bootcmd, 那我们可以新建一个启动命令. 姑且叫它netboot 吧, 下边我们来看如何实现netboot

修改uboot代码

第一处

include/configs/com335x.h 里添加 netboot(可以添加到CONFIG_BOOTCMD上方)

#define CONFIG_NETBOOT "tftp 0x81000000 uImage;"\
"run netargs;"\
"bootm 0x81000000"

第二处

include/env_default.h 里添加 netboot

#ifdef CONFIG_NETBOOT
"netboot=" CONFIG_NETBOOT "\0"
#endif

重新编译uboot, 并烧写

make com335x_nand_512_config

make ARCH=arm CROSS_COMPILE=arm-none-linux-gnueabi-

代码里修改之后直接烧写到nand里还是不能立刻生效。

因为uboot是先读取nand里有没有环境变量, 如果有的话就用nand里的 如果nand里没有才使用默认的环境变量.所以我们在烧写uboot的时候需要将环境变量分区一起擦掉, 这样就可以使用我们代码里的默认环境变量了.

首先看下板子的分区信息

U-Boot# mtdpart
device nand0 <omap2-nand.0>, # parts = 9
#: name size offset mask_flags

0: SPL 0x00020000 0x00000000 0
1: SPL.backup1 0x00020000 0x00020000 0
2: SPL.backup2 0x00020000 0x00040000 0
3: SPL.backup3 0x00020000 0x00060000 0
4: u-boot 0x001e0000 0x00080000 0
5: u-boot-env 0x00020000 0x00260000 0
6: logo 0x00300000 0x00280000 0
7: kernel 0x00500000 0x00580000 0
8: rootfs 0x1f580000 0x00a80000 0

active partition: nand0,0 - (SPL) 0x00020000 @ 0x00000000

defaults:
mtdids : nand0=omap2-nand.0
mtdparts:
   mtdparts=omap2-nand.0:
   128k(SPL),128k(SPL.backup1),128k(SPL.backup2),128k(SPL.backup3),
   1920k(u-boot),128k(u-boot-env),3m(logo),5m(kernel),-(rootfs)

然后执行如下语句:

nand erase.part u-boot
nand erase.part u-boot-env
tftp 0x81000000 u-boot.img
nand write 0x81000000 u-boot ${filesize}

重新设置ip等环境变量, 添加netargs

ip地址的配置需要根据自身的情况进行更改

U-Boot# setenv serverip 192.168.1.102
U-Boot# setenv ipaddr 192.168.1.105
U-Boot# setenv gatewayip 192.168.1.1
U-Boot# setenv netargs setenv bootargs noinitrd console=ttyO0,115200n8 lcdtype=AUO_AT070TN94 root=/dev/nfs ip=ipaddr:{serverip}:gatewayip:
{netmask}:com335x:eth0:off nfsroot=192.168.1.102:/home/eac/nfsboot

U-Boot# save
U-Boot# run netboot

问题

问题1: 为什么不用setenv netboot tftp 0x81000000 uImage; run netboot; bootm 0x81000000 这条命令而要去修改uboot代码?

因为这条语句执行了之后效果只有 netboot=tftp 0x81000000 第一个分号之后的内容丢失了, 所以必须更该代码

问题2: 如果不设置netargs会有什么后果? netargs各部分是什么含义?

设置netargs是为了能够挂载nfs, 如果netboot 设置为: tftp 0x81000000 uImage; bootm 0x81000000 就会因为没有设置根文件系统这个环境变量而出错.

setenv netargs 'setenv bootargs noinitrd console=ttyO0,115200n8 lcdtype=AUO_AT070TN94 root=/dev/nfs ip=ipaddr:{serverip}:gatewayip:
{netmask}:com335x:eth0:off nfsroot=192.168.1.102:/home/eac/nfsboot'
 
${ipaddr} 开发板本身的地址
${serverip} tftp及nfs目录所在系统的地址
${gatewayip} 网关
${netmask} 子网掩码
com335x eth0:off 网卡名(这里随意)
nfsroot=192.168.1.102:/home/eac/nfsboot nfs主机ip地址以及目录
posted @   schips  阅读(2545)  评论(0)    收藏  举报
编辑推荐:
· 一个自认为理想主义者的程序员,写了5年公众号、博客的初衷
· 大数据高并发核心场景实战,数据持久化之冷热分离
· 运维排查 | SaltStack 远程命令执行中文乱码问题
· Java线程池详解:高效并发编程的核心利器
· 从“看懂世界”到“改造世界”:AI发展的四个阶段你了解了吗?
阅读排行:
· .NET 8 gRPC 实现高效100G大文件断点续传工具
· STM32学会要花费多长时间?一个从机械转行老程序员的血泪史
· LinqPad:C#代码测试学习一品神器
· .NET入行第4年(第二集)
· C#/.NET/.NET Core技术前沿周刊 | 第 43 期(2025年6.16-6.22)
历史上的今天:
2018-12-27 linux中ldconfig的使用介绍
点击右上角即可分享
微信分享提示