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

📄 sysenv.c

📁 MIPS下的boottloader yamon 的源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/************************************************************************
 *
 *      sysenv.c
 *
 *      The 'sysenv' module implements the system environment variable
 *      write and read functions, which operate on a RAM and FLASH
 *      storage media. The functions are normally called through the
 *      SYSCON api.
 *
 *
 * ######################################################################
 *
 * 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 <string.h>
#include <stdio.h>
#include <sysdefs.h>
#include <syserror.h>
#include <sysdev.h>
#include <syscon_api.h>
#include <product.h>
#include <sysenv_api.h>

/* generic device driver interface */
#include <io_api.h>

/* drivers */
#include <flash_api.h>

/************************************************************************
 *      Definitions
 ************************************************************************/
/* File FLASH state definitions */
#define SYSENV_STATE_OK        0x42424242
#define SYSENV_STATE_ERROR     0x00000000

/* Header bytes in each record */
/* Register addresses per record */
#define SYSENV_HEADER          0

/* Header field encoding */
#define SYSENV_HEADER_SIZE     4

/* field: 'CONTROL' */
#define SYSENV_HEADER_CONTROL_SHF        0
#define SYSENV_HEADER_CONTROL_MSK        (MSK(8) << SYSENV_HEADER_CONTROL_SHF)

/* field: 'CHKSUM' */
#define SYSENV_HEADER_CHKSUM_SHF         8
#define SYSENV_HEADER_CHKSUM_MSK         (MSK(8) << SYSENV_HEADER_CHKSUM_SHF)

/* field: 'ID' */
#define SYSENV_HEADER_ID_SHF             16
#define SYSENV_HEADER_ID_MSK             (MSK(8) << SYSENV_HEADER_ID_SHF)

/* field: 'SIZE' */
#define SYSENV_HEADER_SIZE_SHF           24
#define SYSENV_HEADER_SIZE_MSK           (MSK(8) << SYSENV_HEADER_SIZE_SHF)


#define SYSENV_DATA                      0
/* field: '0' */
#define SYSENV_DATA_BYTE0_SHF            0
#define SYSENV_DATA_BYTE0_MSK            (MSK(8) << SYSENV_DATA_BYTE0_SHF)

/* field: '1' */
#define SYSENV_DATA_BYTE1_SHF            8
#define SYSENV_DATA_BYTE1_MSK            (MSK(8) << SYSENV_DATA_BYTE1_SHF)

/* field: '2' */
#define SYSENV_DATA_BYTE2_SHF            16
#define SYSENV_DATA_BYTE2_MSK            (MSK(8) << SYSENV_DATA_BYTE2_SHF)

/* field: '3' */
#define SYSENV_DATA_BYTE3_SHF            24
#define SYSENV_DATA_BYTE3_MSK            (MSK(8) << SYSENV_DATA_BYTE3_SHF)


/* control byte definitions: */
#define SYSENV_RECORD_IS_FREE  0xff
#define SYSENV_RECORD_IN_USE   0x42

/* Environment variable record */
typedef UINT8  t_SYSENV_elem[SYS_USER_ENVIRONMENT_DATA_SIZE+SYSENV_HEADER_SIZE] ;

/* lookup table entry element */
typedef struct sysenv_lookup_entry
{
    void  *ram_record ;    /* may keep a record reference to ram space,
                              during garbage collection. Normally
                              this variable is set to NULL */
    void  *flash_record ;  /* may keep a record reference to flash space
                              when a record is allocated. When no record
                              has been allocated for this entry, this
                              variable is set to NULL */
} t_SYSENV_lookup_entry ;

/************************************************************************
 *      Public variables
 ************************************************************************/

/************************************************************************
 *      Static variables
 ************************************************************************/

/* Global state of file flash */
static UINT32 SYSENV_state ;

/* lookup table */
static t_SYSENV_lookup_entry SYSENV_lookup[SYS_USER_ENVIRONMENT_MAX_INDEX+1] ;

/* maximum number of records in file flash */
static UINT32 SYSENV_max_records ;

/* index for next free record in file flash */
static UINT32  SYSENV_next_free ;

/* start and size of file flash */
static void  *SYSENV_file_flash_start ;
static UINT32 SYSENV_file_flash_size  ;

/* storage, used by write operations to format buffer for variable */
static t_SYSENV_elem SYSENV_write_buffer ;

/* this storage is used during garbage collection to store the variables
   temporary during the update, where variables are copied from flash
   to ram, flash is deleted and variables are copied back to flash. */
static t_SYSENV_elem SYSENV_ram_cache[SYS_USER_ENVIRONMENT_MAX_INDEX+1] ;

static char* sysenv_error_string[] =
{
    /* ERROR_SYSENV_OUT_OF_MEMORY    */  "Int. ERROR: No more dynamic memory",
    /* ERROR_SYSENV_ENV_VAR_TOO_BIG  */  "Int. ERROR: Environment var size too big",
    /* ERROR_SYSENV_NO_VARIABLE      */  "Int. ERROR: Environment var not created",
    /* ERROR_SYSENV_INVALID_INDEX    */  "Int. ERROR: Environment var index invalid",
    /* ERROR_SYSENV_UPDATE_READ_REFS */  "WARNING: All references for env variables must be re-read",
    /* ERROR_SYSENV_FLASH_INVALID    */  "Int. ERROR: FLASH is not usable"
} ;

static char* sysenv_error_hint_string[] =
{
    /* ERROR_SYSENV_OUT_OF_MEMORY    */  NULL,
    /* ERROR_SYSENV_ENV_VAR_TOO_BIG  */  NULL,
    /* ERROR_SYSENV_NO_VARIABLE      */  NULL,
    /* ERROR_SYSENV_INVALID_INDEX    */  NULL,
    /* ERROR_SYSENV_UPDATE_READ_REFS */  NULL,
    /* ERROR_SYSENV_FLASH_INVALID    */  "Re-init environment: erase -e",
} ;

/************************************************************************
 *      Static function prototypes
 ************************************************************************/

/************************************************************************
 *
 *                          SYSENV_error_lookup
 *  Description :
 *  -------------
 *  Lookup error code to error string(s)
 * 
 *
 *  Parameters :
 *  ------------
 *
 *  'p_param',   INOUT,    variable of type, t_sys_error_string.
 *
 *
 *  Return values :
 *  ---------------
 *
 * 'OK' = 0x00: 
 *
 *
 ************************************************************************/
static
INT32 SYSENV_error_lookup( t_sys_error_string *p_param ) ;

/************************************************************************
 *
 *                          SYSENV_collect_garbage
 *  Description :
 *  -------------
 *
 *  Execute garbage collection.
 *
 *
 *
 *
 *  Parameters :
 *  ------------
 *
 *  -
 *
 *
 *  Return values :
 *  ---------------
 *
 *  'ERROR_SYSENV_UPDATE_READ_REFS', all references for env variables 
                                     must be re-read.
 *
 ************************************************************************/
static INT32 SYSENV_collect_garbage( void ) ;


/************************************************************************
 *
 *                          SYSENV_check_inuse_record
 *  Description :
 *  -------------
 *  Check referenced record
 * 
 *
 *  Parameters :
 *  ------------
 *
 *  'pb',      INOUT,    environment flash record.
 *  'index',   INOUT,    environment flash record index.
 *
 *
 *  Return values :
 *  ---------------
 *
 * 'ERROR_SYSENV_FLASH_INVALID':  Environment is corrupted.
 * 'OK' = 0x00: 
 *
 *
 ************************************************************************/
static
INT32 SYSENV_check_inuse_record( UINT8 *pb, UINT8 index ) ;


/************************************************************************
 *      Implementation : Public functions
 ************************************************************************/

/************************************************************************
 *
 *                          SYSENV_check_state
 *  Description :
 *  -------------
 *
 *  Check the 'sysenv' state.
 *
 *
 *
 *
 *  Parameters :
 *  ------------
 *
 *  -
 *
 *
 *  Return values :
 *  ---------------
 *  ERROR_SYSENV_FLASH_INVALID, sysenv is corrupted.
 *  'OK'(=0), successfull initialization
 *
 ************************************************************************/
INT32 SYSENV_check_state( void )
{
    if (SYSENV_state != SYSENV_STATE_OK)
    {
        printf("\nWARNING: Environment variable flash area is invalid!"
	       "\nHINT   : Perform \"erase -e\"" );

        return( ERROR_SYSENV_FLASH_INVALID ) ;
    }
    return(OK) ;
}

/************************************************************************
 *
 *                          SYSENV_init

⌨️ 快捷键说明

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