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

📄 syslog.c

📁 基于ecos的redboot
💻 C
字号:
/* syslog.c
---------------------------------------------------------------------------
                 Copyright (c) 2002, 2003 Intel Corporation
						 All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer. 
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution. 
* Neither the name of Intel Corporation nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission. 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      
---------------------------------------------------------------------------
system: IXDP2400
subsystem: BootMonitor
author: gvaddadi
revisions:
--------------------------------------------------------------------------
*/

#include <redboot.h>
#include <cyg/io/flash.h>
#include <cyg/infra/cyg_type.h>
#include <cyg/hal/syslog.h>
#include <cyg/hal/hal_ixdp2400.h> 
#include <cyg/hal/hal_platform_ints.h>

static cmd_fun do_syslog;

RedBoot_cmd("syslog", 
            "Manage system log", 
            "{cmds}",
            do_syslog
    );

// show syslog entry
local_cmd_entry("show",
                "display logs",
                "-n <no.>",
                syslog_show,
                SYSLOG_cmds
    );

// clear all syslog entries
local_cmd_entry("clear",
                "clear all logs",
                "",
                syslog_clear,
                SYSLOG_cmds
    );

// clear all syslog entries
local_cmd_entry("init",
                "initialize syslog",
                "",
                syslog_init,
                SYSLOG_cmds
    );

// Define table boundaries
CYG_HAL_TABLE_BEGIN( __SYSLOG_cmds_TAB__, SYSLOG_cmds);
CYG_HAL_TABLE_END( __SYSLOG_cmds_TAB_END__, SYSLOG_cmds);

extern struct cmd __SYSLOG_cmds_TAB__[], __SYSLOG_cmds_TAB_END__;

static void
syslog_usage(char *why)
{
    printf("*** invalid 'syslog' command: %s\n", why);
    cmd_usage(__SYSLOG_cmds_TAB__, &__SYSLOG_cmds_TAB_END__, "syslog ");
}

static void
do_syslog(int argc, char *argv[])
{
    struct cmd *cmd;

	if(!(strap_options_val() & CFG_PROM_BOOT))
	{
		printf("NPU doesn't have flash\n");
		return;
	}

    if (argc < 2) {
        syslog_usage("too few arguments");
        return;
    }
    if ((cmd = cmd_search(__SYSLOG_cmds_TAB__, &__SYSLOG_cmds_TAB_END__, 
                          argv[1])) != (struct cmd *)0) {
        (cmd->fun)(argc, argv);
        return;
    }
    syslog_usage("unrecognized command");
}

/*****************************************************************************
Function :syslog_init() will initialize the System Log Buffer with the cookie.
          The value 0x54525545 is used as cookie which is represents the ASCII
          'TRUE'.Eachtime the syslog is wriitten or read, it is checked.
Arguments:None
Return   :None
******************************************************************************/
void syslog_init(int argc, char *argv[])
{

	void *err_addr;
	SYSLOG_CNTRL_BLOCK cntrl_block;

	cntrl_block.cookie = SYSLOG_VALID;
	cntrl_block.rsvd1 = cntrl_block.rsvd1 =0;
	
	flash_program((void*)(SYSLOG_START_ADDR),
        (unsigned char*)&(cntrl_block), 
          sizeof(SYSLOG_CNTRL_BLOCK), (void**)&err_addr);

	
	return;

}

/*****************************************************************************
Function :syslog_validity_chk(void) will check for the cookie 0x54525545 at 
          SYSLOG_START_ADDR.
Arguments:None
Return   :SYSLOG_OK if cookie matches else SYSLOG_ERROR
******************************************************************************/
int syslog_validity_chk(void)
{
	unsigned char buf[4];
	unsigned int cookie = SYSLOG_VALID;

	
	flash_read((void*)SYSLOG_START_ADDR,buf,4);
	/*memcpy((void*)SYSLOG_START_ADDR,buf,4);*/
    if (strncmp(buf,(char *)&cookie,4)==0)
        return SYSLOG_OK;
    else
        return SYSLOG_ERROR;
	/*cookie = buf[0];
	cookie=cookie|(buf[1]<<8);
	cookie=cookie|(buf[2]<<16);
	cookie=cookie|(buf[3]<<24);
	printf("\ncookie =%x", cookie);
	if( cookie == (unsigned int)SYSLOG_VALID)
		return SYSLOG_OK;
	else
		return SYSLOG_ERROR;*/
	
}




/******************************************************************************
Function : syslog_clear() will erase all the log entries when requested by 
           the user. 
Arguments: None
Return   : None
******************************************************************************/

void syslog_clear(int argc, char * argv[]) 

{
	void *err_addr;
	if(syslog_validity_chk())
	{
		flash_erase((void*)SYSLOG_START_ADDR, SYSLOG_SIZE, (void*)&err_addr);
		syslog_init(0, NULL);
		

	}
	return;
}




/******************************************************************************
Function :syslog_show() will display the log entries when requested by the user. 
          If the user enters the optional parameter, which indicates how many 
          entries to display,it displays those many entries form the last. By 
          default it display all the entries.
Arguments:no_of_entries - number of entries to display from the lats entry. 
		  0 will display all entries
Return   :none

******************************************************************************/

static void syslog_show(int argc, char * argv[])
{
	volatile unsigned char* start;
	int status,i;
	SYSLOG_DATA buf;
	SYSLOG_INFO info;
	struct option_info opts[1];
	unsigned long no_of_log;
    bool no_of_log_set = false;
	int no_of_entries = 0;
	
	init_opts(&opts[0], 'n', true, OPTION_ARG_TYPE_NUM,(void **)&no_of_log, 
                         (bool *)&no_of_log_set, "no. of logs to show");

	if (!scan_opts(argc, argv, 2, opts, 1, (void *)&no_of_entries, 
                                 OPTION_ARG_TYPE_NUM, "no. of logs"))
    {
        syslog_usage("invalid arguments");
        return;
    }

	if(syslog_validity_chk())
	{
		status=compute_last_logentry_ptr(&info);

	
		/*printf("\nFrom syslog show: status =%d, last_entry = %x, no=%d",
                                  status,(unsigned int)info.last_entry,info.num_of_entries);*/

	/*Compute the address of the starting logentry to be displayed 
     *from NoOfLogEntries and last_entry.
     */
	
		if(status==SYSLOG_EMPTY)
		{
			printf("\nSyslog Empty !!!!!\n");
			return;
		}



		if(no_of_entries !=0 && 
             (no_of_entries < info.num_of_entries) && (status==1))
			start = info.last_entry - (64*(no_of_entries));	
		else
        {
		    start = (volatile unsigned char*)SYSLOG_START_ADDR;
			no_of_entries = info.num_of_entries;
		}
	
	

		for(i=0;i<no_of_entries;i++)
		{
			printf("\n showing entry at %x\n",
                  ((unsigned int)FIRST_ENTRY_PTR+(i*64)));
			flash_read(((unsigned char *)FIRST_ENTRY_PTR+(i*64)),
                  (unsigned char *)&buf, sizeof(SYSLOG_DATA));
            /*memcpy(((unsigned char *)FIRST_ENTRY_PTR+(i*64)),
                  (unsigned char *)&buf, sizeof(SYSLOG_DATA));*/

			syslog_display(&buf);
		
		}
	}/*syslog_validity_chk*/

}


/*****************************************************************************
Function : syslog_display() will display the syslog entries in the required 
           format
Arguments: ptr to syslog entry to be displayed
Return   : none
******************************************************************************/

void syslog_display(SYSLOG_DATA *x)
{
	printf("\nTimestamp	:%d",x->timestamp);
	switch(x->type){
	case 1:printf("\nType:	\tError"); break;
	case 2:printf("\nType:	\tWarning"); break;
	case 3:printf("\nType:	\tDBG MSG1"); break;
	case 4:printf("\nType:	\tDBG MSG2"); break;
	case 5:printf("\nType:	\tDBG MSG3"); break;

	}
	switch(x->source_comp){
	case 1:printf("\nSource:	\tMaster_BM"); break;
	case 2:printf("\nSource:	\tMaster_Diag"); break;
	case 3:printf("\nSource:	\tMaster_Linux"); break;
	case 4:printf("\nSource:	\tMaster_VxWorks"); break;
	case 5:printf("\nSource:	\tSlave_BM"); break;
	case 6:printf("\nSource:	\tSlave_Diag"); break;
	case 7:printf("\nSource:	\tSlave_Linux"); break;
	case 8:printf("\nSource:	\tSlave_VxWorks"); break;
	}
	
	printf("\nErrorcode:	%x",x->error_code);

	printf("\nDesc:	\t%s",x->desc);
	printf("\n**********\n");
	return;
	
}

/*****************************************************************************
Function : compute_last_logentry_ptr() will search for a 64 byte window to 
           compute the the addr of the last entry in the syslog.
Arguments: SYSLOG_INFO to update the last_entry addr and no. of entries
Return   : SYSLOG_FULL if syslog is full, 
           SYSLOG_OK if succesfull in computing last_entry addr 
           SYSLOG_EMPTY- if syslog is empty.
******************************************************************************/

int compute_last_logentry_ptr(SYSLOG_INFO *x)
{
	
	/*status 0- error, 1-success, 2 syslog emty*/
	char buf[64];
	int status=SYSLOG_ERROR,cnt,i;
	x->num_of_entries = 0;

	x->last_entry = (unsigned char *)FIRST_ENTRY_PTR;
	cnt =0;
	while(cnt!=64 && 
       (x->last_entry < (unsigned char *)(SYSLOG_START_ADDR + SYSLOG_SIZE-1)))
	{
		
		flash_read(x->last_entry,&buf[0], 64);
        /*memcpy(x->last_entry,&buf[0], 64);*/
		
		cnt =0;
		for(i=0;i<64;i++)
		{
		
			if(buf[i] != (char)0xFF)
			{
				
				break;
			}
			else
			{
				cnt++;
			}

		}

		if(cnt ==64)
			
		{
			status  =SYSLOG_OK;
			break;
		}
		else
		{
			x->last_entry+=64;
			x->num_of_entries++;
		}
	}

	if((x->last_entry+5*64)> (unsigned char *)(SYSLOG_START_ADDR + SYSLOG_SIZE -1))
		printf("\n Warning: Syslog will be full soon and will be erased!!!\n");
	
	if(x->last_entry == (unsigned char *)FIRST_ENTRY_PTR)
	{
        printf("\nSyslog is empty!!!!\n"); 
        status =SYSLOG_EMPTY;
    }
	
	if(x->last_entry >=(unsigned char*)(SYSLOG_START_ADDR + SYSLOG_SIZE -SYSLOG_ENTRY_SIZE-1))
	{
        status = SYSLOG_FULL; 
        printf("\nSyslog is full\n"); 
    }
	return status;
}

void flash_read(volatile unsigned char *addr,unsigned char *buf, int len)
{
	int i;
	for(i = 0; i < len; i++)
	{
		buf[i] = addr[i];
//		eprintf("addr=0x%X buf=0x%X\n", &addr[i], buf[i]);
	}
}

⌨️ 快捷键说明

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