📄 env.c
字号:
/************************************************************************
*
* env.c
*
* ENV module
*
* This module implements YAMON environment variables. It will create
* default variables as well.
*
* This file holds the generic part of the ENV module (except for TTY
* related parts, which are found in ./env_tty.c).
*
* Platform specific parts are found in yamon/arch/env directory.
*
* ENV uses the SYSENV module, which manages "raw" records.
*
*
* ######################################################################
*
* Copyright (c) 1999-2000 MIPS Technologies, Inc. All rights reserved.
*
* Unpublished rights reserved under the Copyright Laws of the United States of
* America.
*
* This document contains information that is proprietary to MIPS Technologies,
* Inc. ("MIPS Technologies"). Any copying, modifying or use of this information
* (in whole or in part) which is not expressly permitted in writing by MIPS
* Technologies or a contractually-authorized third party is strictly
* prohibited. At a minimum, this information is protected under unfair
* competition laws and the expression of the information contained herein is
* protected under federal copyright laws. Violations thereof may result in
* criminal penalties and fines.
* MIPS Technologies or any contractually-authorized third party reserves the
* right to change the information contained in this document to improve
* function, design or otherwise. MIPS Technologies does not assume any
* liability arising out of the application or use of this information. Any
* license under patent rights or any other intellectual property rights owned
* by MIPS Technologies or third parties shall be conveyed by MIPS Technologies
* or any contractually-authorized third party in a separate license agreement
* between the parties.
* The information contained in this document constitutes one or more of the
* following: commercial computer software, commercial computer software
* documentation or other commercial items. If the user of this information, or
* any related documentation of any kind, including related technical data or
* manuals, is an agency, department, or other entity of the United States
* government ("Government"), the use, duplication, reproduction, release,
* modification, disclosure, or transfer of this information, or any related
* documentation of any kind, is restricted in accordance with Federal
* Acquisition Regulation 12.212 for civilian agencies and Defense Federal
* Acquisition Regulation Supplement 227.7202 for military agencies. The use of
* this information by the Government is further restricted in accordance with
* the terms of the license agreement(s) and/or applicable contract terms and
* conditions covering this information from MIPS Technologies or any
* contractually-authorized third party.
*
************************************************************************/
/************************************************************************
* Include files
************************************************************************/
#include <sysdefs.h>
#include <sys_api.h>
#include <syscon_api.h>
#include <sysenv_api.h>
#include <shell_api.h> /* Due to env_print_all */
#include <env_api.h>
#include <env.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <syserror.h>
#include <errno.h>
#define DISP_STR( s ) sys_disp_str( s )
/************************************************************************
* Definitions
************************************************************************/
typedef struct
{
UINT32 index;
UINT8 attr;
char *default_value;
t_env_decode decode;
char data[SYS_USER_ENVIRONMENT_DATA_SIZE];
}
t_env_var_info;
#define INDEX_NONE (SYS_USER_ENVIRONMENT_MAX_INDEX + 1)
#define INDEX_REARRANGED (SYS_USER_ENVIRONMENT_MAX_INDEX + 2)
/************************************************************************
* Public variables
************************************************************************/
/************************************************************************
* Static variables
************************************************************************/
static char* error_string[] =
{
/* ERROR_ENV_VAR_NAME_LEN */ "Too long variable name",
/* ERROR_ENV_VAR_RO */ "Read only variable",
/* ERROR_ENV_VAR_VALUE */ "Illegal value",
/* ERROR_ENV_VAR_OVERFLOW */ "Too many environment variables",
/* ERROR_ENV_VAR_VAL_LEN */ "Too long variable string"
};
static char *default_prompt = DEFAULT_PROMPT;
static char *default_cpuconfig = "";
static bool cache_configurable, mmu_configurable;
static UINT32 config_init;
static t_env_var env_vars[SYS_USER_ENVIRONMENT_MAX_INDEX + 2];
static t_env_var_info env_var_info[SYS_USER_ENVIRONMENT_MAX_INDEX + 1];
static UINT32 env_var_count;
static bool env_corrupted;
/* If TRUE, variables that are otherwise RO, may be written
* (used during initialisation of system variables).
*/
static bool ext_priviliges = FALSE;
/************************************************************************
* Static function prototypes
************************************************************************/
static bool
decode_number(
char *raw, /* The string */
void *decoded, /* Decoded data */
UINT32 size, /* Size of decoded data */
UINT32 base ); /* Base (8/10/16) */
static bool
hex_s2num(
char *raw, /* The string */
void *decoded, /* Decoded data */
UINT32 size ); /* Size of decoded data */
static bool
validate_cache(
bool icache, /* TRUE -> ICACHE, FALSE -> DCACHE */
UINT32 bpw,
UINT32 assoc );
static bool
validate_cpuconfig(
char *raw, /* The string */
void *decoded, /* Decoded data */
UINT32 size ); /* Size of decoded data */
static INT32
error_lookup(
t_sys_error_string *param );
static UINT32
access_var(
char *name,
char **value,
UINT8 attr,
char *default_value,
t_env_decode decode );
static void
set_env_var(
t_env_var *env_var,
t_env_var_info *info );
static bool
lookup_env(
char *name,
UINT32 *index );
static int
compare(
UINT32 *x,
UINT32 *y );
static UINT32
remove_var(
UINT32 index,
bool user,
bool system,
bool error_on_ro );
static UINT32
env_to_flash(
UINT32 index_local,
UINT32 *index_flash );
static void
env_remove_from_flash(
UINT32 index );
static void
convert_endianness(
char *s );
static void
re_init( void );
/************************************************************************
* Implementation : Static functions
************************************************************************/
/************************************************************************
*
* decode_number
* Description :
* -------------
*
* Decode number
*
* Return values :
* ---------------
*
* TRUE -> OK, FALSE -> Failed
*
************************************************************************/
static bool
decode_number(
char *raw, /* The string */
void *decoded, /* Decoded data */
UINT32 size, /* Size of decoded data */
UINT32 base ) /* Base (8/10/16) */
{
char *endp;
UINT32 number;
if( !raw )
return FALSE;
/* Do the decoding */
errno = 0;
number = strtoul( raw, &endp, base );
if( decoded )
{
switch( size )
{
case sizeof(UINT8) :
*(UINT8 *)decoded = (UINT8)number;
break;
case sizeof(UINT16) :
*(UINT16 *)decoded = (UINT16)number;
break;
case sizeof(UINT32) :
*(UINT32 *)decoded = (UINT32)number;
break;
default :
return FALSE;
}
}
return
( errno || (*endp != '\0') ) ?
FALSE : TRUE;
}
/************************************************************************
*
* hex_s2num
*
* Description :
* -------------
*
* Decode decimal number
*
* Return values :
* ---------------
*
* TRUE -> OK, FALSE -> Failed
*
************************************************************************/
static bool
hex_s2num(
char *raw, /* The string */
void *decoded, /* Decoded data */
UINT32 size ) /* Size of decoded data */
{
return decode_number( raw, decoded, size, 16 );
}
/************************************************************************
*
* validate_cache
* Description :
* -------------
*
* Validate cache setting
*
* Return values :
* ---------------
*
* TRUE -> OK, FALSE -> Failed
*
************************************************************************/
static bool
validate_cache(
bool icache, /* TRUE -> ICACHE, FALSE -> DCACHE */
UINT32 bpw,
UINT32 assoc )
{
t_sys_array sys_array;
INT32 dummy;
UINT32 i;
sys_cpu_cache_bpw( icache, &sys_array );
for( i=0;
( i < sys_array.count ) &&
( bpw != sys_array.array[i] );
i++ );
if( i == sys_array.count ) return FALSE;
sys_cpu_cache_assoc( icache, &sys_array );
for( i=0;
( i < sys_array.count ) &&
( assoc != sys_array.array[i] );
i++ );
if( i == sys_array.count ) return FALSE;
return TRUE;
}
/************************************************************************
*
* validate_cpuconfig
* Description :
* -------------
*
* Validate and decode cpuconfig environment variable of format :
*
* [<i_bpw>,<i_assoc>,<d_bpw>,<d_assoc>[,(tlb|fixed)]] | (tlb|fixed)
*
* Return values :
* ---------------
*
* TRUE -> OK, FALSE -> Failed
*
************************************************************************/
static bool
validate_cpuconfig(
char *raw, /* The string */
void *decoded, /* Decoded data */
UINT32 size ) /* Size of decoded data */
{
char *endp;
UINT32 i_bpw, i_assoc, d_bpw, d_assoc;
bool mmu_tlb;
if( !raw ) return FALSE;
errno = 0;
if( *raw == '\0' )
{
i_bpw = sys_icache_lines/sys_icache_assoc*sys_icache_linesize;
i_assoc = sys_icache_assoc;
d_bpw = sys_dcache_lines/sys_dcache_assoc*sys_dcache_linesize;
d_assoc = sys_dcache_assoc;
mmu_tlb = ((config_init >> 8) & 1) ? FALSE : TRUE;
}
else
{
if( cache_configurable )
{
/* Get i_bpw */
i_bpw = (UINT32)strtoul( raw, &endp, 10 );
if( (*endp != ',') || errno )
return FALSE;
endp++; /* Skip ',' */
raw = endp;
if( *endp == ' ' )
return FALSE;
/* Get i_assoc */
i_assoc = (UINT32)strtoul( raw, &endp, 10 );
if( (*endp != ',') || errno )
return FALSE;
endp++; /* Skip ',' */
raw = endp;
if( *endp == ' ' )
return FALSE;
/* Get d_bpw */
d_bpw = (UINT32)strtoul( raw, &endp, 10 );
if( (*endp != ',') || errno )
return FALSE;
endp++; /* Skip ',' */
raw = endp;
if( *endp == ' ' )
return FALSE;
/* Get d_assoc */
d_assoc = (UINT32)strtoul( raw, &endp, 10 );
if( errno )
return FALSE;
if( mmu_configurable )
{
if( *endp != ',' )
return FALSE;
endp++; /* Skip ',' */
raw = endp;
if( *endp == ' ' )
return FALSE;
}
else
{
if( *endp != '\0' )
return FALSE;
}
/* Validate I-cache */
if( !validate_cache( TRUE, i_bpw, i_assoc ) )
return FALSE;
/* Validate D-cache */
if( !validate_cache( FALSE, d_bpw, d_assoc ) )
return FALSE;
}
if( mmu_configurable )
{
/* Get mmu */
if( strcmp( raw, "tlb" ) == 0 )
mmu_tlb = TRUE;
else if( strcmp( raw, "fixed" ) == 0 )
mmu_tlb = FALSE;
else
return FALSE;
}
}
/* Store decoded data */
if( decoded )
{
if( size != sizeof( t_sys_cpu_decoded ) )
return FALSE;
((t_sys_cpu_decoded *)decoded)->i_bpw = i_bpw;
((t_sys_cpu_decoded *)decoded)->i_assoc = i_assoc;
((t_sys_cpu_decoded *)decoded)->d_bpw = d_bpw;
((t_sys_cpu_decoded *)decoded)->d_assoc = d_assoc;
((t_sys_cpu_decoded *)decoded)->mmu_tlb = mmu_tlb;
}
return TRUE;
}
/************************************************************************
*
* error_lookup
* Description :
* -------------
* Lookup error code to error string(s)
*
* Parameters :
* ------------
*
* 'param', INOUT, variable of type, t_sys_error_string.
*
* Return values :
* ---------------
*
* 'OK' = 0x00:
*
************************************************************************/
static INT32
error_lookup(
t_sys_error_string *param )
{
UINT32 t = SYSERROR_ID( param->syserror );
/* check for recognized error code */
if( t < sizeof(error_string)/sizeof(char *) )
{
param->count = 1;
/* fill in mandatory error message string */
param->strings[SYSCON_ERRORMSG_IDX] = error_string[t];
}
else
param->count = 0;
return OK;
}
/************************************************************************
* access_var
************************************************************************/
static UINT32
access_var(
char *name,
char **value,
UINT8 attr,
char *default_value,
t_env_decode decode )
{
t_env_var *env_var;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -