博主注:本文讲解了如何使用uboot-envtools(fw_printenv、fw_setenv)来读取和修改uboot-env,以及uboot-envtools配置文件的设置方法。
1、查看系统分区
root@OpenWrt:/usr# cat /proc/mtd dev: size erasesize name mtd0: 00030000 00010000 "u-boot" mtd1: 00010000 00010000 "u-boot-env" mtd2: 00010000 00010000 "factory" mtd3: 00fa0000 00010000 "firmware" mtd4: 0016c8dc 00010000 "kernel" mtd5: 00e33724 00010000 "rootfs" mtd6: 00750000 00010000 "rootfs_data"
menuconfig添加uboot_envtools模块
.config - OpenWrt Configuration > Utilities > Boot Loaders ───────────────────────────────────────────────────────────────── ┌──────────────────────────────────── Boot Loaders ─────────────────────────────────────┐ │ Arrow keys navigate the menu. <Enter> selects submenus ---> (or empty submenus │ │ ----). Highlighted letters are hotkeys. Pressing <Y> includes, <N> excludes, <M> │ │ modularizes features. Press <Esc><Esc> to exit, <?> for Help, </> for Search. │ │ Legend: [*] built-in [ ] excluded <M> module < > module capable │ │ ┌───────────────────────────────────────────────────────────────────────────────────┐ │ │ │ < > fconfig..................................... RedBoot configuration editor│ │ │ │ <*> uboot-envtools................. read/modify U-Boot bootloader environment│ │ │ │ │ │ │ │ │ │ │ └───────────────────────────────────────────────────────────────────────────────────┘ │ ├───────────────────────────────────────────────────────────────────────────────────────┤ │ <Select> < Exit > < Help > < Save > < Load > │ └───────────────────────────────────────────────────────────────────────────────────────┘
2、编译后,进入系统,测试
查看env
root@OpenWrt:/# fw_printenvbootcmd=bootp; setenv bootargs root=/dev/nfs nfsroot=${serverip}:${rootpath} ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}::off; bootm bootdelay=5 baudrate=115200 ssid=12345678
添加env,uuid=1234567890
root@OpenWrt:/# fw_setenv uuid 1234567890 root@OpenWrt:/# fw_printenv bootcmd=bootp; setenv bootargs root=/dev/nfs nfsroot=${serverip}:${rootpath} ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}::off; bootm bootdelay=5 baudrate=115200 ssid=12345678 uuid=1234567890
删除env
root@OpenWrt:/# fw_setenv uuid root@OpenWrt:/# fw_printenv bootcmd=bootp; setenv bootargs root=/dev/nfs nfsroot=${serverip}:${rootpath} ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}::off; bootm bootdelay=5 baudrate=115200 ssid=12345678 root@OpenWrt:/#
3、透过system、popen 管道,在c代码中读写操作
int fw_env_del(char *key){ char buffer[256]; sprintf(buffer,"fw_setenv %s",key); return system(buffer);}int fw_env_set(char *key,char *val){ char buffer[256]; sprintf(buffer,"fw_setenv %s %s",key,val); return system(buffer);}int fw_env_get(char *key,char *val){ FILE * fp; char buffer[256]; char read_key[32]; char read_val[128]; fp=popen("fw_printenv","r"); if (fp<0){ return -1; } while(fgets(buffer,sizeof(buffer),fp)){ sscanf(buffer,"%[^=]=%s",read_key,read_val); if(!strcmp(key,read_key)){ strcpy(val,read_val); pclose(fp); return 0; } } pclose(fp); return -404;}
本站的文章和资源来自互联网或者站长的原创,按照 CC BY -NC -SA 3.0 CN协议发布和共享,转载或引用本站文章应遵循相同协议。如果有侵犯版权的资 源请尽快联系站长,我们会在24h内删除有争议的资源。欢迎大家多多交流,期待共同学习进步。