/****************************************************************************** 版权所有: 文件名称: rt_env.c 文件版本: 01.01 创建作者: zhaoyang 创建日期: 2023-12-17 功能说明: 其它说明: 修改记录: */ /*------------------------------- 头文件 -------------------------------------- */ #include "rt.h" /*------------------------------- 宏定义 -------------------------------------- */ #define SETENV_PROG "/usr/sbin/fw_setenv" /*------------------------------ 类型结构 ------------------------------------- */ struct env_file g_env_file[] = { {"ethact=", NULL}, {"ipaddr=", NULL}, {"netmask=", NULL}, {"gatewayip=", NULL}, {"ver_u_boot=", NULL}, {"ver_kernel=", NULL}, {"ver_root_fs=", NULL}, {"auth=", NULL}, {"mt_port_on=", NULL}, {"pid=", NULL}, {"net1mac=", NULL}, {"net2mac=", NULL}, {"net3mac=", NULL}, }; #define ENV_FILE_NUM ((int)(sizeof(g_env_file) / sizeof(struct env_file))) /*------------------------------ 全局变量 ------------------------------------- */ u8 *g_file_buf; /*------------------------------ 函数声明 ------------------------------------- */ /*------------------------------ 外部函数 ------------------------------------- 外部函数供其它实体文件引用,必须仔细检查传入参数的合法性. */ int env_init(void) { int i, j, file_length, name_len, buf_len, row_len, file_offset; int pfile; off_t pos; // return 0; //版本文件设置为默认值 // 打开文件 // pfile = rt_file_open("/tmp/env.dat",O_CREAT|O_RDONLY ,0); pfile = rt_file_open("/tmp/env.dat", O_RDONLY, 0); if (IS_ERR(pfile)) { rt_printf("env data open err!\r\n"); return -1; } // 得到文件长度 file_length = rt_file_getfile_size(pfile); if (file_length <= 0) { rt_file_close(pfile, 0); return -2; } // 分配内存 // printf("file_length = %d\n", file_length); g_file_buf = (u8 *)rt_malloc(file_length); if (g_file_buf == NULL) { rt_file_close(pfile, 0); return -3; } pos = 0; if (rt_file_read(pfile, (char *)g_file_buf, file_length, &pos) != file_length) { rt_file_close(pfile, 0); rt_free(g_file_buf); return -4; } // 关闭文件 rt_file_close(pfile, 0); file_offset = 0; row_len = 0; name_len = 0; for (i = 0; i < ENV_FILE_NUM; i++) { // 找到\r\n位置,得到一行长度 for (j = file_offset; j < file_length; j++) { if (g_file_buf[j] == 0x0a) { row_len = j + 1; break; } } name_len = strlen(g_env_file[i].name); buf_len = row_len - file_offset - name_len - 1; if (strncmp(g_env_file[i].name, (char *)g_file_buf + file_offset, name_len) == 0) { g_env_file[i].buf = (s8 *)(g_file_buf + file_offset + name_len); *(g_env_file[i].buf + buf_len) = 0; } file_offset = row_len; } return 0; } int env_exit(void) { if (g_file_buf) { rt_free(g_file_buf); g_file_buf = 0; } return 0; } char *env_get_info(int no) { if (no >= ENV_FILE_NUM) { return NULL; } else { return (char *)g_env_file[no].buf; } } int env_setenv(char *name, char *value) { return 0; /* int ret; char argv[256] = {0x00}; ret = access(SETENV_PROG, X_OK); if(!ret) { sprintf(argv, "%s %s %s", SETENV_PROG, name, value); ret = system(argv); } return ret; */ } int env_restart(void) { return 0; /* int ret; ret = access("/app/m", X_OK); if(!ret) { ret = system("/app/m"); } rt_printf("env_restart:ret=%d", ret); return ret; */ } /*------------------------------ 文件结束 ------------------------------------- */