📄 env.c
字号:
{
/* printf("Corrupted during memsize %08x ", error); */
env_corrupted = TRUE;
}
/* Prompt */
if( default_switch || !env_get( "prompt", &raw, NULL, 0 ) )
raw = default_prompt; /* Create new */
if( (error = env_set( "prompt", raw, ENV_ATTR_RW, default_prompt, NULL )) != OK )
{
/* printf("Corrupted during prompt %08x ", error); */
env_corrupted = TRUE;
}
/* Start command */
if( default_switch || !env_get( "start", &raw, NULL, 0 ) )
raw = ""; /* Create new */
if( (error = env_set( "start", raw, ENV_ATTR_RW, "", NULL )) != OK )
{
/* printf("Corrupted during start %08x ", error); */
env_corrupted = TRUE;
}
/**** Cache configuration ****/
if( cache_configurable || mmu_configurable )
{
if( default_switch || !env_get( "cpuconfig", &raw, NULL, 0 ) ||
!validate_cpuconfig( raw, NULL, 0) )
{
raw = default_cpuconfig; /* Create new */
}
if( (error = env_set( "cpuconfig", raw, ENV_ATTR_RW,
default_cpuconfig, validate_cpuconfig )) != OK )
{
/* printf("Corrupted during set cpuconfig %08x ", error); */
env_corrupted = TRUE;
}
}
else
{
if( (error = env_unset( "cpuconfig" )) != OK )
{
/* printf("Corrupted during env_unset %08x ", error); */
env_corrupted = TRUE;
}
}
/* TTY variables */
if( !env_setup_env_tty( default_switch ) )
{
/* printf("Corrupted during env_tty "); */
env_corrupted = TRUE;
}
/* Board settings */
if( !env_setup_env_board( default_switch ) )
{
/* printf("Corrupted during env_board "); */
env_corrupted = TRUE;
}
ext_priviliges = FALSE;
}
/************************************************************************
*
* env_get
* Description :
* -------------
*
* Lookup environment variable based on name.
*
* Return values :
* ---------------
*
* TRUE -> found, FALSE -> not found
*
************************************************************************/
bool
env_get(
char *name, /* Name of environment variable */
char **raw, /* Raw text string */
void *decoded, /* Decoded data */
UINT32 size ) /* Size of decoded data */
{
UINT32 index;
bool rc = TRUE;
if( !lookup_env( name, &index ) )
{
rc = FALSE;
}
else
{
/* Found */
if( raw )
*raw = env_vars[index].val;
if( env_var_info[index].decode )
{
/* decoded parameter may be NULL, in which case this is
* simply a validation of the data.
*/
rc = env_var_info[index].decode( env_vars[index].val, decoded, size );
}
else
{
/* No decode function, so reply TRUE unless a decoded
* parameter is given, which we can't fill out.
*/
rc = decoded ? FALSE : TRUE;
}
}
return rc;
}
/************************************************************************
*
* env_set
* Description :
* -------------
*
* Set (and possibly create) environment variable
*
* Return values :
* ---------------
*
* OK if no error, else error code
*
************************************************************************/
UINT32
env_set(
char *name,
char *value,
UINT8 attr,
char *default_value,
t_env_decode decode )
{
return access_var( name, &value, attr, default_value, decode );
}
/************************************************************************
*
* env_unset
* Description :
* -------------
*
* Delete environment variable
*
* Return values :
* ---------------
*
* OK if no error, else error code
*
************************************************************************/
UINT32
env_unset(
char *name ) /* Name of variable */
{
UINT32 index;
return
lookup_env( name, &index ) ?
/* Found */
remove_var( index, TRUE, TRUE, TRUE ) :
/* Not found */
OK;
}
/************************************************************************
*
* env_print_all
* Description :
* -------------
*
* Display all env. variables
*
* Return values :
* ---------------
*
* None
*
************************************************************************/
void
env_print_all( void )
{
t_env_var *env_var;
t_env_var_info *info;
UINT32 index;
char *s;
UINT32 sort[SYS_USER_ENVIRONMENT_MAX_INDEX + 1];
UINT32 i;
UINT32 maxlen = 0;
/* Sort and print variables */
for( i = 0; i < env_var_count; i++ )
sort[i] = i;
qsort(
(char *)sort,
(int)env_var_count,
(int)sizeof(UINT32),
compare );
/* Print variables */
if( env_var_count != 0 )
{
/* Find longest name */
for( i=0; i < env_var_count; i++ )
{
env_var = &env_vars[sort[i]];
maxlen = MAX( maxlen, strlen( env_var->name ) );
}
/* Print variables */
for( i=0; i < env_var_count; i++ )
{
index = sort[i];
env_var = &env_vars[index];
info = &env_var_info[index];
if(SHELL_PUTC( '\n' )) return;
if(SHELL_PUTS( env_var->name )) return;
switch( info->attr )
{
case ENV_ATTR_RW :
s = "(R/W)"; break;
case ENV_ATTR_RO :
s = "(RO)"; break;
case ENV_ATTR_USER :
s = "(USER)"; break;
default :
s = NULL; break;
}
if( s )
{
if(SHELL_PUTS_INDENT( s, maxlen + 1 )) return;
}
if(SHELL_PUTS_INDENT( env_var->val, maxlen + 8 ))
return;
}
if(SHELL_PUTS( "\n\n" )) return;
}
}
/************************************************************************
*
* env_remove
* Description :
* -------------
*
* Remove all user and or system variables
*
* Return values :
* ---------------
*
* Error code (OK = No error)
*
************************************************************************/
UINT32
env_remove(
bool user, /* TRUE -> remove user variables */
bool system ) /* TRUE -> remove system variables */
{
UINT32 index;
UINT32 old_count;
UINT32 rc = OK;
index = 0;
while( (index < env_var_count) && (rc == OK) )
{
old_count = env_var_count;
rc = remove_var( index, user, system, FALSE );
/* If env_var_count was decreased, the
* variable was deleted and the last variable
* was moved to it's place in the array
*/
if( old_count == env_var_count )
{
/* Variable was not deleted */
index++;
}
}
return rc;
}
/************************************************************************
*
* env_ip_s2num
* Description :
* -------------
*
* Decode a string of format x.y.z.w to 4 bytes
*
* Return values :
* ---------------
*
* TRUE -> OK, FALSE -> Failed
*
************************************************************************/
bool
env_ip_s2num(
char *raw, /* The string */
void *decoded, /* Decoded data */
UINT32 size ) /* Size of decoded data */
{
char *endp;
UINT32 number;
UINT32 i;
UINT32 value = 0;
/* Check that we have the string */
if( !raw )
return FALSE;
/* Check size of variable for decoded data */
if( decoded && (size != sizeof(UINT32)) )
return FALSE;
for(i=0; i<sizeof(UINT32); i++)
{
value <<= 8;
errno = 0;
number = strtoul( raw, &endp, 10 );
if( errno || (number > 255) || (endp == raw) )
return FALSE;
value |= number;
if( i != (sizeof(UINT32) - 1) )
{
/* Not last number */
if( *endp != '.' )
return FALSE;
else
raw = endp + 1; /* Skip dot */
}
}
if( decoded )
{
/* Always deliver data in network byte order (big endian) */
#ifdef EL
*(UINT32 *)decoded = SWAPEND32(value);
#else
*(UINT32 *)decoded = value;
#endif
}
return ( *endp == '\0' ) ? TRUE : FALSE;
}
/************************************************************************
*
* env_setup_cpuconfig
* Description :
* -------------
*
* Setup string for "cpuconfig" environment variable
*
* Return values :
* ---------------
*
* None
*
************************************************************************/
void
env_setup_cpuconfig(
char *s,
t_sys_cpu_decoded *decoded )
{
*s = '\0';
if( cache_configurable )
{
sprintf( s, "%d,%d,%d,%d",
decoded->i_bpw, decoded->i_assoc,
decoded->d_bpw, decoded->d_assoc );
if( mmu_configurable )
strcat( s, "," );
}
if( mmu_configurable )
strcat( s, decoded->mmu_tlb ? "tlb" : "fixed" );
}
/************************************************************************
*
* env_get_all
* Description :
* -------------
*
* Get a pointer to the array of environment variables
*
* Return values :
* ---------------
*
* Pointer to array of env. variables
*
************************************************************************/
t_env_var
*env_get_all( void )
{
return env_vars;
}
/************************************************************************
*
* env_mac_s2num
* Description :
* -------------
*
* Decode a string of format xx.xx.xx.xx.xx.xx to 6 bytes
*
* Return values :
* ---------------
*
* TRUE -> OK, FALSE -> Failed
*
************************************************************************/
bool
env_mac_s2num(
char *raw, /* The string */
void *decoded, /* Decoded data */
UINT32 size ) /* Size of decoded data */
{
t_mac_addr *mac_addr;
char *endp;
UINT8 number;
UINT32 i;
if( !raw || (strlen(raw) != 5+6*2) )
{
return FALSE;
}
if( decoded && (size != sizeof(t_mac_addr)) )
{
return FALSE;
}
mac_addr = (t_mac_addr *)decoded;
errno = 0;
for( i=0; i<sizeof(t_mac_addr); i++)
{
if( *raw == '\0' )
{
return FALSE;
}
number = strtoul( raw, &endp, 16 );
if( errno )
return FALSE;
if( endp == raw )
return FALSE;
raw = &endp[1];
if( mac_addr )
(*mac_addr)[i] = number;
}
return (*endp == '\0') ? TRUE : FALSE;
}
/************************************************************************
*
* env_check
* Description :
* -------------
*
* Determine whether env. variables have been corrupted (due to flash
* corruption). If this is the case, print warning.
*
* Return values :
* ---------------
*
* TRUE -> corrupted, FALSE -> not corrupted (normal state)
*
************************************************************************/
bool
env_check(void)
{
if( env_corrupted )
{
printf("\nWARNING: Environment variables not valid!"
"\nHINT : Perform \"erase -e\"" );
return TRUE;
}
else
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -