openwrt设置uboot环境变量在flash上的存储地址

 1.分析如下

ubootenv_add_app_config

ubootenv_add_uci_config "/dev/mtd1" "0x40000" "0x2000"  "0x20000"

这两个函数定义在文件package/boot/uboot-envtools/files/uboot-envtools.sh中,先来解析以上语句:

/dev/mtd1 : 指定boot_env所在flash上的分区

0x40000 : 指定boot_env所在分区上的偏移量(这个参数有点费解,这个参数是否是多余的?

0x2000 :指定uboot的环境变量存储空间大小,注意这个大小,必须与在package/boot/uboot-${boardname}/Makefile中定义的ENV_SIZE大小一致,否则会报CRC校验出错,比如mtd2分区的大小为1MiB,uboot指定的大小为0x40000,那么应该使用0x40000

0x20000 :指定flash的sector大小

ubootenv_add_app_config这个函数就是将"/dev/mtd1" "0x40000" "0x2000"  "0x20000" 这些内容追加到/etc/fw_env.config

2.举例论证

笔者使用的flash分区如下:

root@OpenWrt:/# cat /proc/mtd
dev: size erasesize name
mtd0: 00200000 00040000 "u-boot"
mtd1: 00100000 00040000 "u-boot-env"
mtd2: 00100000 00040000 "dts"
mtd3: 00800000 00040000 "kernel"

mtd4: 00800000 00040000 "rootfs"

flash的扇区大小为0x40000=256KiB

uboot的环境变量保存在flash的mtd1分区上

那么/etc/fw_env.config的值如下:

/dev/mtd1 0x0000 0x40000  0x40000

0x0000:这个值为0比较费解

第一个0x40000来自uboot的宏定义ENV_SIZE

第二个0x40000来自uboot的宏定义CONFIG_ENV_SECT_SIZE

3.fw_printenv和fw_setenv报错分析(假设只有其中一项参数不正确)

3.1 当指定的环境变量大小不正确时提示:

root@OpenWrt:/etc# fw_printenv

Warning: Bad CRC, using default environment

3.2 当指定的第一个参数分区偏移量不正确且该值不等于该分区在flash的偏移量大小:

root@OpenWrt:/etc# fw_printenv
Warning: Bad CRC, using default environment

3.3 当指定的分区偏移量没有与flash的sector大小成倍数时:

root@OpenWrt:/etc# fw_printenv
Environment does not start on (erase) block boundary

3.4 当指定的第一个参数分区偏移量等于该分区在flash上的偏移量时:

可以读取出环境变量,但是没法修改环境变量,错误提示如下:

root@OpenWrt:/etc# fw_setenv bootdelay 4
MTD erase error on /dev/mtd2: Invalid argument
Error: can't write fw_env to flash

原文地址:https://www.cnblogs.com/dakewei/p/10035770.html