2# dianli
附上一个源文件。
;
;
Project : Common plateform
;
Creator :
;
File
: bsp_sup.c
;
Abstract: function library for application tasks.
;
;Modification History:
;
By
Date
Ver.
Modification Description
;
--------------- -------- -----
--------------------------------------
;
;*****************************************************************************/
#include <adm5000.h>
#include <netmall.h>
#include <knl_sup.h>
#include <bsp_sup.h>
#include <bsp_cfg.h>
#include <irqlib.h>
#include <stdlib.h>
static int mac_ckcnt = 0;
/************************************************************************
* Name:
int bsp_BootProductCode()
*
* should move this routine to apps directore, so that others can share
************************************************************************/
void bsp_BootProductCode(int btmode)
/* copy romInit.s in product code */
{
UINT32
i, cnt=0;
void (*funcptr)();
/* Disable all switch ports */
bsp_SwitchShutDown();
disable_int();
_icache_sync_all();
_dcache_sync_all();
if (btmode == BTMODE_NORMAL)
funcptr = (void *) PA2CACHEVA(HWPF_RUNTIME_CODE_START);
else
funcptr = (void *) PA2CACHEVA(HWPF_POST_DOWNLOAD_START);
funcptr();
return;
/* should not come to this pooint at all */
}
/************************************************************************
* Name:
int bsp_GetMacBase()
*
* This function print boot configuration.
************************************************************************/
int bsp_GetMacBase(char *buf, int *macnum)
{
BOARD_CFG_T *cfg = (BOARD_CFG_T *) PA2VA(HWPF_POST_PARMS);
int len;
int checkResult;
if(buf == NULL) return -1;
if(cfg->macmagic != MAC_MAGIC) return -1;
// have to use memcpy here.
memcpy(buf, cfg->mac, 6);
/*设置是否mac检验成功*/
checkResult=checkCopyrightCode(buf,6,cfg->MacCheckCode);
setMacCheck(checkResult);
if(!checkResult)
{
printf("Warnning!!The MAC addr is not legal!!\n Please contact the vender!\n");
printf("Warnning!!The MAC addr is not legal!!\n Please contact the vender!\n");
}
*macnum = (int) cfg->macnum;
return 0;
}
/************************************************************************
* Name:
int bsp_SetMacCheck()
*
用来设置MAC地址以及校验码
************************************************************************/
int bsp_SetMacCheck(char *mac, int macnum,char *MacCheckCode)
{
BOARD_CFG_T
cfg;
int len, i;
UINT32 sum;
if((mac == NULL) || (macnum < 1) || (macnum > BSP_MAX_MAC_NUM))
return -1;
// Allow only unicast mac address
if(mac[0] != 0) return -1;
// Read in the old configuration data.
memcpy((char *)&cfg, (char *) PA2VA(HWPF_POST_PARMS), sizeof (cfg));
cfg.macmagic = MAC_MAGIC;
// have to use memcpy here.
memcpy(cfg.mac, mac, 6);
cfg.mac[7] = cfg.mac[8] = 0;
cfg.macnum = macnum;
// Ripple adder
sum = macnum - 1;
for(i=5; i>2; i--)
{
if((sum += cfg.mac) > 255)
{
// ripple carry to the next byte.
cfg.mac = (unsigned char) (sum &0xff);
sum >>= 8;
}
else
{
cfg.mac = (unsigned char) sum;
break;
}
}
if(i == 2) return ERROR;
// have to use memcpy here.
memcpy(cfg.mac, mac, 6);
memcpy(cfg.MacCheckCode,MacCheckCode,32);
if (flash_erase((char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg)) != 0)
return ERROR;
if (flash_write((char *)PA2VA(HWPF_POST_PARMS), (char *)&cfg, sizeof(cfg)) != 0)
return ERROR;
return 0;
}
/************************************************************************
* Name:
int bsp_SetMac()
*
* This function print boot configuration.
************************************************************************/
int bsp_SetMac(char *mac, int macnum)
{
BOARD_CFG_T
cfg;
int len, i;
UINT32 sum;
if((mac == NULL) || (macnum < 1) || (macnum > BSP_MAX_MAC_NUM))
return -1;
// Allow only unicast mac address
if(mac[0] != 0) return -1;
// Read in the old configuration data.
memcpy((char *)&cfg, (char *) PA2VA(HWPF_POST_PARMS), sizeof (cfg));
cfg.macmagic = MAC_MAGIC;
// have to use memcpy here.
memcpy(cfg.mac, mac, 6);
cfg.mac[7] = cfg.mac[8] = 0;
cfg.macnum = macnum;
// Ripple adder
sum = macnum - 1;
for(i=5; i>2; i--)
{
if((sum += cfg.mac) > 255)
{
// ripple carry to the next byte.
cfg.mac = (unsigned char) (sum &0xff);
sum >>= 8;
}
else
{
cfg.mac = (unsigned char) sum;
break;
}
}
if(i == 2) return ERROR;
// have to use memcpy here.
memcpy(cfg.mac, mac, 6);
if (flash_erase((char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg)) != 0)
return ERROR;
if (flash_write((char *)PA2VA(HWPF_POST_PARMS), (char *)&cfg, sizeof(cfg)) != 0)
return ERROR;
return 0;
}
/************************************************************************
* Name:
int bsp_CheckoutMac()
*
* This function print boot configuration.
************************************************************************/
int bsp_CheckoutMac(char *buf)
{
BOARD_CFG_T *cfg = (BOARD_CFG_T *) PA2VA(HWPF_POST_PARMS);
unsigned char mac[8];
int i;
UINT32 sum;
if(buf == NULL) return -1;
if(cfg->macmagic != MAC_MAGIC) return -1;
if(mac_ckcnt >= cfg->macnum) return -1;
memcpy(mac, cfg->mac, 6);
// Ripple adder
sum = mac_ckcnt;
for(i=5; i>2; i--)
{
if((sum += mac) > 255)
{
// ripple carry to the next byte.
mac = (unsigned char) (sum &0xff);
sum >>= 8;
}
else
{
mac = (unsigned char) sum;
break;
}
}
if(i == 2) return ERROR;
mac_ckcnt ++;
memcpy(buf, mac, 6);
return 0;
}
/************************************************************************
* Name:
int bsp_AvialMacNum()
*
* This function print boot configuration.
************************************************************************/
int bsp_AvialMacNum(void)
{
BOARD_CFG_T *cfg = (BOARD_CFG_T *) PA2VA(HWPF_POST_PARMS);
if(cfg->macmagic != MAC_MAGIC) return 0;
return (cfg->macnum - mac_ckcnt);
}
/************************************************************************
* Name:
int bsp_GetBootLine()
*
* This function print boot configuration.
************************************************************************/
int bsp_GetBootLine(char *buf, int buflen)
{
BOARD_CFG_T *cfg = (BOARD_CFG_T *) PA2VA(HWPF_POST_PARMS);
int len;
if(buf == NULL) return -1;
if(cfg->blmagic != BL_MAGIC) return -1;
if((len = strlen(cfg->bootline)) > buflen) return -1;
strcpy(buf, cfg->bootline);
return 0;
}
/************************************************************************
* Name:
int bsp_SetBootLine()
*
* This function print boot configuration.
************************************************************************/
int bsp_SetBootLine(char *s)
{
BOARD_CFG_T
cfg;
int len;
if(s == NULL) return -1;
if((len=strlen(s)) > BOOT_LINE_SIZE) return -1;
// Read in the old configuration data.
memcpy((char *)&cfg, (char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg));
cfg.blmagic = BL_MAGIC;
strncpy(cfg.bootline, s, BOOT_LINE_SIZE+1);
if (flash_erase((char *) PA2VA(HWPF_POST_PARMS), sizeof (cfg)) != 0)
return ERROR;
if (flash_write((char *) PA2VA(HWPF_POST_PARMS), (char *)&cfg, sizeof (cfg)) != 0)
return ERROR;
return 0;
}
/************************************************************************
* Name:
int bsp_GetBoardVersion()
*
* This function print boot configuration.
************************************************************************/
int bsp_GetBoardVersion(char *buf, int buflen)
{
BOARD_CFG_T *cfg = (BOARD_CFG_T *) PA2VA(HWPF_POST_PARMS);
int len;
if(buf == NULL) return -1;
if(cfg->vermagic != VER_MAGIC) return -1;
if((len = strlen(cfg->ver)) > buflen) return -1;
strcpy(buf, cfg->ver);
return 0;
}
/************************************************************************
* Name:
int bsp_SetBoardVersion()
*
* This function print boot configuration.
************************************************************************/
int bsp_SetBoardVersion(char *s)
{
BOARD_CFG_T
cfg;
int len;
if(s == NULL) return -1;
if((len=strlen(s)) > BSP_STR_LEN) return -1;
// Read in the old configuration data.
memcpy((char *)&cfg, (char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg));
cfg.vermagic = VER_MAGIC;
strncpy(cfg.ver, s, BSP_STR_LEN+1);
if (flash_erase((char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg)) != 0)
return ERROR;
if (flash_write((char *)PA2VA(HWPF_POST_PARMS), (char *)&cfg, sizeof (cfg)) != 0)
return ERROR;
return 0;
}
/************************************************************************
* Name:
int bsp_GetBoardSerialNo()
*
* This function print boot configuration.
************************************************************************/
int bsp_GetBoardSerialNo(char *buf, int buflen)
{
BOARD_CFG_T *cfg = (BOARD_CFG_T *) PA2VA(HWPF_POST_PARMS);
int len;
if(buf == NULL) return -1;
if(cfg->idmagic != ID_MAGIC) return -1;
if((len = strlen(cfg->serial)) > buflen) return -1;
strcpy(buf, cfg->serial);
return 0;
}
/************************************************************************
* Name:
int bsp_SetBoardSerialNo()
*
* This function print boot configuration.
************************************************************************/
int bsp_SetBoardSerialNo(char *s)
{
BOARD_CFG_T
cfg;
int len;
if(s == NULL) return -1;
if((len=strlen(s)) > BSP_STR_LEN) return -1;
// Read in the old configuration data.
memcpy((char *)&cfg, (char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg));
cfg.idmagic = ID_MAGIC;
strncpy(cfg.serial, s, BSP_STR_LEN+1);
if (flash_erase((char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg)) != 0)
return ERROR;
if (flash_write((char *)PA2VA(HWPF_POST_PARMS), (char *)&cfg, sizeof(cfg)) != 0)
return ERROR;
return 0;
}
/************************************************************************
* Name:
int bsp_AddIfcfg()
*
* This function print boot configuration.
************************************************************************/
int bsp_AddIfcfg(const char *ifname, UINT32 ip, UINT32 mask, UINT32 gateway)
{
BOARD_CFG_T
cfg;
IF_CFG_T *ifcfg;
int ifcnt = 0, ifid = -1;
if(ifname == NULL || ip == 0 || mask == 0) return -1;
if(strlen(ifname) > BSP_IFNAME_MAX_LEN) return -1;
// Assum the ip, mask and gateway are already checked in the POST.
// Read in the old configuration data.
memcpy((char *)&cfg, (char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg));
ifcfg = cfg.ifcfg;
if(cfg.ifmagic == IF_MAGIC)
{
for(ifcnt = 0; ifcnt < BSP_MAX_IF_CNT; ifcnt ++)
{
if(ifcfg[ifcnt].ip == 0) break;
if(ifid < 0 && strcmp(ifname, ifcfg[ifcnt].ifname) == 0) ifid = ifcnt;
}
}
if(ifid < 0 && ifcnt == BSP_MAX_IF_CNT) return -1;
// No more space
if(ifid < 0) ifid = ifcnt;
strcpy(ifcfg[ifid].ifname, ifname);
ifcfg[ifid].ip = ip;
ifcfg[ifid].mask = mask;
ifcfg[ifid].gateway = gateway;
// Zero the unused slots
if(++ifcnt < BSP_MAX_IF_CNT)
{
memset(&ifcfg[ifcnt], 0, sizeof(IF_CFG_T)*(BSP_MAX_IF_CNT - ifcnt));
}
cfg.ifmagic = IF_MAGIC;
if (flash_erase((char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg)) != 0)
return ERROR;
if (flash_write((char *)PA2VA(HWPF_POST_PARMS), (char *)&cfg, sizeof(cfg)) != 0)
return ERROR;
return 0;
}
/************************************************************************
* Name:
void bsp_DelIfcfg()
*
* This function print boot configuration.
************************************************************************/
int bsp_DelIfcfg(const char *ifname)
{
BOARD_CFG_T
cfg;
IF_CFG_T *ifcfg;
int ifcnt, ifid = -1;
if(ifname == NULL) return -1;
// Read in the old configuration data.
memcpy((char *)&cfg, (char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg));
if(cfg.ifmagic != IF_MAGIC) return -1;
ifcfg = cfg.ifcfg;
// Find the id of deleting interface and the total interface number.
for(ifcnt = 0; ifcnt< BSP_MAX_IF_CNT; ifcnt++)
{
if(ifcfg[ifcnt].ip == 0) break;
if(ifid < 0 && strcmp(ifname, ifcfg[ifcnt].ifname) == 0) ifid = ifcnt;
}
if(ifid < 0) return -1;
// The interface does not exist.
// Copy the last interface configuration to the deleted slot.
ifcnt --;
if(ifcnt != ifid)
{
memcpy((void *) &ifcfg[ifid], &ifcfg[ifcnt], sizeof(IF_CFG_T));
}
// Zero out the last interface slot
memset(&ifcfg[ifcnt], 0, sizeof(IF_CFG_T));
if (flash_erase((char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg)) != 0)
return ERROR;
if (flash_write((char *)PA2VA(HWPF_POST_PARMS), (char *)&cfg, sizeof(cfg)) != 0)
return ERROR;
return 0;
}
/************************************************************************
* Name:
void bsp_GetIfcfg()
*
* This function print boot configuration.
************************************************************************/
int bsp_GetIfCfg(const char *ifname, BSP_IF_CFG_T *pIp)
{
BOARD_CFG_T
cfg;
IF_CFG_T *ifcfg;
int i;
if(ifname == NULL || pIp == NULL) return -1;
// Read in the old configuration data.
memcpy((char *)&cfg, (char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg));
if(cfg.ifmagic != IF_MAGIC) return -1;
ifcfg = cfg.ifcfg;
for(i=0; i<BSP_MAX_IF_CNT; i++)
{
if(ifcfg.ip == 0) break;
if(strcmp(ifname, ifcfg.ifname) == 0)
{
pIp->ip = ifcfg.ip;
pIp->mask = ifcfg.mask;
pIp->gateway = ifcfg.gateway;
return 0;
}
}
return -1;
}
/************************************************************************
* Name:
void bsp_GetIfcfgByIdx()
*
* This function print boot configuration.
************************************************************************/
int bsp_GetIfCfgByIdx(int idx, char *name, BSP_IF_CFG_T *pIp)
{
BOARD_CFG_T
cfg;
IF_CFG_T *ifcfg;
if(idx < 0 || idx >= BSP_MAX_IF_CNT) return -1;
if(name == NULL || pIp == NULL) return -1;
// Read in the old configuration data.
memcpy((char *)&cfg, (char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg));
if(cfg.ifmagic != IF_MAGIC) return -1;
ifcfg = cfg.ifcfg;
if(ifcfg[idx].ip == 0) return -1;
strcpy(name, ifcfg[idx].ifname);
pIp->ip = ifcfg[idx].ip;
pIp->mask = ifcfg[idx].mask;
pIp->gateway = ifcfg[idx].gateway;
return 0;
}
/************************************************************************
* Name:
void bsp_GetIfcfgCount()
*
* This function print boot configuration.
************************************************************************/
int bsp_GetIfcfgCount(void)
{
BOARD_CFG_T
cfg;
IF_CFG_T *ifcfg;
int i;
// Read in the old configuration data.
memcpy((char *)&cfg, (char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg));
if(cfg.ifmagic != IF_MAGIC) return 0;
ifcfg = cfg.ifcfg;
for(i=0; i<BSP_MAX_IF_CNT; i++)
{
if(ifcfg.ip == 0) return i;
}
return i;
}
/************************************************************************
* Name:
int bsp_GetBootMode()
*
* This function gets boot mode.
************************************************************************/
int bsp_GetBootMode(UINT32 *btmode)
{
BOARD_CFG_T
cfg;
if (btmode == NULL)
return -1;
// Read in the old configuration data.
memcpy((char *)&cfg, (char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg));
if (cfg.btmagic != BT_MAGIC)
return -1;
switch (cfg.bootmode.btmode)
{
case BTMODE_NORMAL:
case BTMODE_DBG:
break;
default:
return -1;
}
*btmode = cfg.bootmode.btmode;
return 0;
}
/************************************************************************
* Name:
int bsp_SetBootMode()
*
* This function sets boot mode.
************************************************************************/
int bsp_SetBootMode(UINT32 btmode)
{
BOARD_CFG_T
cfg;
switch (btmode)
{
case BTMODE_NORMAL:
case BTMODE_DBG:
break;
default:
return -1;
}
// Read in the old configuration data.
memcpy((char *)&cfg, (char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg));
cfg.btmagic = BT_MAGIC;
cfg.bootmode.btmode = btmode;
if (flash_erase((char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg)) != 0)
return -1;
if (flash_write((char *)PA2VA(HWPF_POST_PARMS), (char *)&cfg, sizeof (cfg)) != 0)
return -1;
return 0;
}
/************************************************************************
* Name:
int bsp_GetDownloadMethod()
*
* This function gets boot mode.
************************************************************************/
int bsp_GetDownloadMethod(UINT32 *method)
{
BOARD_CFG_T
cfg;
if (method == NULL)
return -1;
// Read in the old configuration data.
memcpy((char *)&cfg, (char *)PA2CACHEVA(HWPF_POST_PARMS), sizeof (cfg));
if (cfg.btmagic != BT_MAGIC)
return -1;
switch (cfg.bootmode.dlmethod)
{
case DLMETHOD_XMODEM:
case DLMETHOD_TFTPS:
case DLMETHOD_TFTPC:
break;
default:
return -1;
}
*method = cfg.bootmode.dlmethod;
return 0;
}
/************************************************************************
* Name:
int bsp_SetDownloadMethod()
*
* This function sets boot mode.
************************************************************************/
int bsp_SetDownloadMethod(UINT32 method)
{
BOARD_CFG_T
cfg;
switch (method)
{
case DLMETHOD_XMODEM:
case DLMETHOD_TFTPS:
case DLMETHOD_TFTPC:
break;
default:
return -1;
}
// Read in the old configuration data.
memcpy((char *)&cfg, (char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg));
cfg.btmagic = BT_MAGIC;
cfg.bootmode.dlmethod = method;
if (flash_erase((char *)PA2VA(HWPF_POST_PARMS), sizeof (cfg)) != 0)
return -1;
if (flash_write((char *)PA2VA(HWPF_POST_PARMS), (char *)&cfg, sizeof (cfg)) != 0)
return -1;
return 0;
}
/************************************************************************
* Name:
void bsp_SwitchShutDown()
*
* This function disable all switch ports.
************************************************************************/
void bsp_SwitchShutDown(void)
{
int s, i;
s = disable_int();
/* Disable All ports*/
ADM5000_SW_REG(Port_conf0_REG) |= SW_DISABLE_PORT_MASK;
/* Disable CPU port */
ADM5000_SW_REG(CPUp_conf_REG) |= SW_CPU_PORT_DISABLE;
restore_int(s);
// Wait until switch DMA idle. At least 1ms is required!!!!
for (i=0; i <1000000; i++);
}
void bsp_Reboot(void)
{
int i;
for (i=0; i<1000; i++);
ADM5000_SW_REG(SftReset_REG) = SOFTWARE_RESET;
} |