⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sysenv.c

📁 MIPS下的boottloader yamon 的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
 *  Description :
 *  -------------
 *
 *  Initializes the 'sysenv' module.
 *
 *
 *
 *
 *  Parameters :
 *  ------------
 *
 *  -
 *
 *
 *  Return values :
 *  ---------------
 *
 *  'OK'(=0), successfull initialization
 *
 ************************************************************************/
INT32 SYSENV_init( void )
{
    t_sys_error_lookup_registration  registration;
    t_FLASH_ctrl_descriptor	     flash_ctrl;
    INT32			     rcode;
    UINT8			     *pb;
    int				     i, j ;
    UINT32			     default_switch;


    SYSCON_read( SYSCON_BOARD_USE_DEFAULT_ID,
		 (void *)&default_switch,
		 sizeof(UINT32) );

    if( default_switch )
    {
        /* Erase fileflash */
        flash_ctrl.command = FLASH_CTRL_ERASE_FILEFLASH;
        IO_ctrl( SYS_MAJOR_FLASH_STRATA, 0, (UINT8 *)(&flash_ctrl) );
    }

    /* register lookup syserror */
    registration.prefix = SYSERROR_DOMAIN( ERROR_SYSENV ) ;
    registration.lookup = SYSENV_error_lookup ;
    SYSCON_write( SYSCON_ERROR_REGISTER_LOOKUP_ID,
                  &registration,
                  sizeof( registration ) );

    /* Set default SYSENV state = 'ERROR' */
    SYSENV_state = SYSENV_STATE_ERROR ;

    /* initialize lookup table */
    for ( i = 0; i <= SYS_USER_ENVIRONMENT_MAX_INDEX; i++)
    {
        SYSENV_lookup[i].ram_record   = &SYSENV_ram_cache[i] ;
        SYSENV_lookup[i].flash_record = NULL ;
    }

    /* get start of file flash */
    rcode = SYSCON_read( SYSCON_BOARD_FILEFLASH_BASE_ID,
                         &SYSENV_file_flash_start,
                         sizeof(SYSENV_file_flash_start) ) ;
    if (rcode != OK)
    {
        return(rcode) ;
    }
    else
        SYSENV_file_flash_start = (void *)KSEG1(SYSENV_file_flash_start);

    /* get size for file flash  */
    rcode = SYSCON_read( SYSCON_BOARD_FILEFLASH_SIZE_ID,
                         &SYSENV_file_flash_size,
                         sizeof(SYSENV_file_flash_size) ) ;
    if (rcode != OK)
    {
        return(rcode) ;
    }

    /* calculate maximum number of records in file flash */
    SYSENV_max_records = SYSENV_file_flash_size / sizeof(t_SYSENV_elem) ;

    /* initialize current context */
    pb = SYSENV_file_flash_start ;
    for ( i = 0; i < SYSENV_max_records; i++)
    {
        if ( REGPRD(pb,SYSENV_HEADER,CONTROL) == SYSENV_RECORD_IN_USE )
        { 
            /* this record is in use */
            if ( REGPRD(pb,SYSENV_HEADER,ID) <= SYS_USER_ENVIRONMENT_MAX_INDEX )
            {
                /* check for record removal */
                if ( REGPRD(pb,SYSENV_HEADER,SIZE)  == 0 )
                {
                    /* delete context */
                    SYSENV_lookup[REGPRD(pb,SYSENV_HEADER,ID)].flash_record = NULL ;
                }
                else
                {
                    /* check size: */
                    if ( REGPRD(pb,SYSENV_HEADER,SIZE)  <= SYS_USER_ENVIRONMENT_DATA_SIZE )
                    {
                        /* insert this context in lookup table */
                        SYSENV_lookup[REGPRD(pb,SYSENV_HEADER,ID)].flash_record = pb ;
                        rcode = SYSENV_check_inuse_record( pb, REGPRD(pb,SYSENV_HEADER,ID) ) ;
                        if ( rcode != OK )
                        {
                            return( rcode ) ;
                        }
                    }
                    else
                    {
                       /* This is an error, file FLASH is corrupted ! */
                       printf("WARNING1: environment variable FLASH area is invalid !!\n") ;
                       return( ERROR_SYSENV_FLASH_INVALID ) ;
                    }
                }
            }
            else
            {
                /* This is an error, file FLASH is corrupted ! */
                printf("WARNING2: environment variable FLASH area is invalid !!\n") ;
                return( ERROR_SYSENV_FLASH_INVALID ) ;
            }
        }
        else
        {
            if ( REGPRD(pb,SYSENV_HEADER,CONTROL) == SYSENV_RECORD_IS_FREE )
            { 
                /* this is the first free record in file flash */
                SYSENV_next_free = i ;
                break ;
            }
            else
            {
                /* This is an error, file FLASH is corrupted ! */
                printf("WARNING3: environment variable FLASH area is invalid !!\n") ;
                return( ERROR_SYSENV_FLASH_INVALID ) ;
            }
        }

        /* address next record */
        pb = pb + sizeof(t_SYSENV_elem) ;
    }

    /* validate the free FLASH */

    pb = SYSENV_file_flash_start + SYSENV_next_free * sizeof(t_SYSENV_elem) ;
    for ( i = SYSENV_next_free; i < SYSENV_max_records; i++)
    {
        for ( j=0; j<(sizeof(t_SYSENV_elem)/sizeof(UINT32)); j++ ) 
        {
            if ( REG32( pb + j*sizeof(UINT32) ) != 0xffffffff )    
            {
                /* This is an error, file FLASH is corrupted ! */
                printf("WARNING4: environment variable FLASH area is invalid !!\n") ;
                return( ERROR_SYSENV_FLASH_INVALID ) ;
            }
        }
        pb = pb + sizeof(t_SYSENV_elem) ;
    }

    /* Set SYSENV state = 'OK' */


    SYSENV_state = SYSENV_STATE_OK ;
    return(OK) ;
}


/************************************************************************
 *
 *                          SYSENV_read
 *  Description :
 *  -------------
 *
 *  Read a system environment variable. Unformatted data are read from
 *  a data array, accessed via an 'index'. Data are not copied, just
 *  a pointer reference is being returned to the data array and the
 *  actual size of the data, which have been stored.
 *
 *
 *  Parameters :
 *  ------------
 *
 *  'var',          INOUT, data are not copied
 *
 *  Return values :
 *  ---------------
 *
 *  'OK'(=0), returned parameter value and size are valid.
 *
 *
 ************************************************************************/
INT32 SYSENV_read( t_user_environment_var *var )
{
    INT32 rcode = OK ;
    UINT8 *pb ;

    if (SYSENV_state != SYSENV_STATE_OK)
    {
        return( ERROR_SYSENV_FLASH_INVALID ) ;
    }

    if ( var->index <= SYS_USER_ENVIRONMENT_MAX_INDEX )
    {
        /* index OK, just try and get the context */
        if ( SYSENV_lookup[ var->index ].flash_record != NULL )
        {
            /* OK, there is a environment variable */
            pb              = SYSENV_lookup[ var->index ].flash_record ;

            /* verify inuse record */
            rcode = SYSENV_check_inuse_record( pb, var->index ) ;
            if (rcode == OK)
            {
                var->size       = REGPRD(pb,SYSENV_HEADER,SIZE) ;
                var->data_inout = (void*)&(REGP(pb,SYSENV_HEADER_SIZE)) ;
            }
            else
            {
                var->size       = 0 ;
                var->data_inout = NULL ;
            }

            /* Check for deleted records */
            if (var->size == 0)
            {
                /* Record has been deleted */
                rcode = ERROR_SYSENV_NO_VARIABLE ;
            }
        }
        else
        {
            /* No environment variable created for this index */
            rcode = ERROR_SYSENV_NO_VARIABLE ;
        }
    }
    else
    {
        /* Index is out of valid range */
        rcode = ERROR_SYSENV_INVALID_INDEX ;
    }
    return( rcode ) ;
}


/************************************************************************
 *
 *                          SYSENV_write
 *  Description :
 *  -------------
 *
 *  Write data to a system environment variable. Unformatted data are
 *  written to a data array, accessed via an 'index'. Data are copied,
 *  into non-volatile memory (FLASH) and a pointer reference is
 *  returned to the data-array of this variable in FLASH. The actual
 *  size is stored too, to be returned in a 'read' for this variable.
 *  A system variable is being created with the first 'write' operation
 *  to this variable and deleted, if any succeeding 'write' to
 *  this 'index' contains a 'size' parameter of '0'.
 *
 *
 *  Parameters :
 *  ------------
 *
 *  'var',          INOUT, data are copied into non-volatile memory
 *                         and the storage-pointer for this memory
 *                         is being returned in the 'data_inout'
 *                         parameter of the parameter block.
 *
 *  Return values :
 *  ---------------
 *
 *  'OK'(=0), returned parameter value and size are valid.
 *
 *
 ************************************************************************/
INT32 SYSENV_write( t_user_environment_var *var )
{
    int   i ;
    INT32 completion, rcode = OK ;
    UINT8 *pb ;
    UINT8 *pdata ;
    UINT8 checksum ;
    t_FLASH_write_descriptor flash_write ;
    t_FLASH_ctrl_descriptor  flash_ctrl ;

    if (SYSENV_state != SYSENV_STATE_OK)
    {
        return( ERROR_SYSENV_FLASH_INVALID ) ;
    }

    /* validate user parameters */
    if ( var->index <= SYS_USER_ENVIRONMENT_MAX_INDEX )
    {
        if ( var->size <= SYS_USER_ENVIRONMENT_DATA_SIZE )
        {
            /* Index and size is OK, just try and store the context */
            if (SYSENV_next_free < SYSENV_max_records)
            {
                /* normal case */
                completion = OK ;
            }
            else
            {
                /* This is the garbage collection case */
                completion = SYSENV_collect_garbage() ;
                if (completion != ERROR_SYSENV_UPDATE_READ_REFS)
                {
                    return(completion) ;
                }
            }

            /* format buffer */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -