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

📄 tgt_machdep.c

📁 MIPS处理器的bootloader,龙芯就是用的修改过的PMON2
💻 C
📖 第 1 页 / 共 2 页
字号:
 *  Flash programming support code. *//* *  Table of flash devices on target. See pflash_tgt.h. */struct fl_map tgt_fl_map_boot8[] = {        TARGET_FLASH_DEVICES_8};struct fl_map tgt_fl_map_boot32[] = {        TARGET_FLASH_DEVICES_32};struct fl_map *tgt_flashmap(){	if((GT_READ(GT_BOOT_PAR) & 0x00300000) == 0) {		return tgt_fl_map_boot8;	}	else {		return tgt_fl_map_boot32;	}}   voidtgt_flashwrite_disable(){}inttgt_flashwrite_enable(){	if(in8(PLD_BSTAT) & 0x40) {		return(1);	}	else {		return(0);	/* can't enable. jumper selected! */	}}voidtgt_flashinfo(void *p, size_t *t){	struct fl_map *map;	map = fl_find_map(p);	if(map) {		*t = map->fl_map_size;	}	else {		*t = 0;	}}voidtgt_flashprogram(void *p, int size, void *s, int endian){	printf("Programming flash %p:%p into %p\n", s, size, p);	if(fl_erase_device(p, size, TRUE)) {		printf("Erase failed!\n");		return;	}	if(fl_program_device(p, s, size, TRUE)) {		printf("Programming failed!\n");	}	fl_verify_device(p, s, size, TRUE);}#endif /* PFLASH *//* *  Network stuff. */voidtgt_netinit(){}inttgt_ethaddr(char *p){	bcopy((void *)&hwethadr, p, 6);	return(0);}voidtgt_netreset(){}/*************************************************************************//* *	Target dependent Non volatile memory support code *	================================================= * * *  On this target a part of the boot flash memory is used to store *  environment. See EV64260.h for mapping details. (offset and size). *//* *  Read in environment from NV-ram and set. */voidtgt_mapenv(int (*func) __P((char *, char *))){        char *ep;        char env[512];        char *nvram;	int i;        /*         *  Check integrity of the NVRAM env area. If not in order         *  initialize it to empty.         */#ifdef NVRAM_IN_FLASH        nvram = (char *)(tgt_flashmap())->fl_map_base;	if(fl_devident(nvram, NULL) == 0 ||           cksum(nvram + NVRAM_OFFS, NVRAM_SIZE, 0) != 0) {#else        nvram = (char *)malloc(512);	nvram_get(nvram);	if(cksum(nvram, NVRAM_SIZE, 0) != 0) {#endif                nvram_invalid = 1;        }        else {		nvram += NVRAM_OFFS;                ep = nvram+2;;                while(*ep != 0) {                        char *val = 0, *p = env;			i = 0;                        while((*p++ = *ep++) && (ep <= nvram + NVRAM_SIZE - 1) && i++ < 255) {                                if((*(p - 1) == '=') && (val == NULL)) {                                        *(p - 1) = '\0';                                        val = p;                                }                        }                        if(ep <= nvram + NVRAM_SIZE - 1 && i < 255) {                                (*func)(env, val);                        }                        else {                                nvram_invalid = 2;                                break;                        }                }        }	/*	 *  Ethernet address for Galileo ethernet is stored in the last	 *  six bytes of nvram storage. Set environment to it.	 */	bcopy(&nvram[ETHER_OFFS], hwethadr, 6);	sprintf(env, "%02x:%02x:%02x:%02x:%02x:%02x", hwethadr[0], hwethadr[1],	    hwethadr[2], hwethadr[3], hwethadr[4], hwethadr[5]);	(*func)("ethaddr", env);#ifndef NVRAM_IN_FLASH	free(nvram);#endif#ifdef no_thank_you        (*func)("vxWorks", env);#endif	sprintf(env, "%d", memorysize / (1024 * 1024));	(*func)("memsize", env);	sprintf(env, "%d", md_pipefreq);	(*func)("cpuclock", env);	sprintf(env, "%d", md_cpufreq);	(*func)("busclock", env);	(*func)("systype", SYSTYPE);	sprintf(env, "%d", CpuTertiaryCacheSize / (1024*1024));	(*func)("l3cache", env);	sprintf(env, "%x", GT_BASE_ADDR);	(*func)("gtbase", env);}inttgt_unsetenv(char *name){        char *ep, *np, *sp;        char *nvram;        char *nvrambuf;        char *nvramsecbuf;	int status;        if(nvram_invalid) {                return(0);        }	/* Use first defined flash device (we probably have only one) */#ifdef NVRAM_IN_FLASH        nvram = (char *)(tgt_flashmap())->fl_map_base;	/* Map. Deal with an entire sector even if we only use part of it */        nvram += NVRAM_OFFS & ~(NVRAM_SECSIZE - 1);	nvramsecbuf = (char *)malloc(NVRAM_SECSIZE);	if(nvramsecbuf == 0) {		printf("Warning! Unable to malloc nvrambuffer!\n");		return(-1);	}        memcpy(nvramsecbuf, nvram, NVRAM_SECSIZE);	nvrambuf = nvramsecbuf + (NVRAM_OFFS & (NVRAM_SECSIZE - 1));#else        nvramsecbuf = nvrambuf = nvram = (char *)malloc(512);	nvram_get(nvram);#endif        ep = nvrambuf + 2;	status = 0;        while((*ep != '\0') && (ep <= nvrambuf + NVRAM_SIZE)) {                np = name;                sp = ep;                while((*ep == *np) && (*ep != '=') && (*np != '\0')) {                        ep++;                        np++;                }                if((*np == '\0') && ((*ep == '\0') || (*ep == '='))) {                        while(*ep++);                        while(ep <= nvrambuf + NVRAM_SIZE) {                                *sp++ = *ep++;                        }                        if(nvrambuf[2] == '\0') {                                nvrambuf[3] = '\0';                        }                        cksum(nvrambuf, NVRAM_SIZE, 1);#ifdef NVRAM_IN_FLASH                        if(fl_erase_device(nvram, NVRAM_SECSIZE, FALSE)) {                                status = -1;				break;                        }                        if(fl_program_device(nvram, nvramsecbuf, NVRAM_SECSIZE, FALSE)) {                                status = -1;				break;                        }#else			nvram_put(nvram);#endif			status = 1;			break;                }                else if(*ep != '\0') {                        while(*ep++ != '\0');                }        }	free(nvramsecbuf);        return(status);}inttgt_setenv(char *name, char *value){        char *ep;        int envlen;        char *nvrambuf;        char *nvramsecbuf;#ifdef NVRAM_IN_FLASH        char *nvram;#endif	/* Non permanent vars. */	if(strcmp(EXPERT, name) == 0) {		return(1);	}        /* Calculate total env mem size requiered */        envlen = strlen(name);        if(envlen == 0) {                return(0);        }        if(value != NULL) {                envlen += strlen(value);        }        envlen += 2;    /* '=' + null byte */        if(envlen > 255) {                return(0);      /* Are you crazy!? */        }	/* Use first defined flash device (we probably have only one) */#ifdef NVRAM_IN_FLASH        nvram = (char *)(tgt_flashmap())->fl_map_base;	/* Deal with an entire sector even if we only use part of it */        nvram += NVRAM_OFFS & ~(NVRAM_SECSIZE - 1);#endif        /* If NVRAM is found to be uninitialized, reinit it. */	if(nvram_invalid) {		nvramsecbuf = (char *)malloc(NVRAM_SECSIZE);		if(nvramsecbuf == 0) {			printf("Warning! Unable to malloc nvrambuffer!\n");			return(-1);		}#ifdef NVRAM_IN_FLASH		memcpy(nvramsecbuf, nvram, NVRAM_SECSIZE);#endif		nvrambuf = nvramsecbuf + (NVRAM_OFFS & (NVRAM_SECSIZE - 1));                memset(nvrambuf, -1, NVRAM_SIZE);                nvrambuf[2] = '\0';                nvrambuf[3] = '\0';                cksum((void *)nvrambuf, NVRAM_SIZE, 1);		printf("Warning! NVRAM checksum fail. Reset!\n");#ifdef NVRAM_IN_FLASH                if(fl_erase_device(nvram, NVRAM_SECSIZE, FALSE)) {			printf("Error! Nvram erase failed!\n");			free(nvramsecbuf);                        return(-1);                }                if(fl_program_device(nvram, nvramsecbuf, NVRAM_SECSIZE, FALSE)) {			printf("Error! Nvram init failed!\n");			free(nvramsecbuf);                        return(-1);                }#else		nvram_put(nvramsecbuf);#endif                nvram_invalid = 0;		free(nvramsecbuf);        }        /* Remove any current setting */        tgt_unsetenv(name);        /* Find end of evironment strings */	nvramsecbuf = (char *)malloc(NVRAM_SECSIZE);	if(nvramsecbuf == 0) {		printf("Warning! Unable to malloc nvrambuffer!\n");		return(-1);	}#ifndef NVRAM_IN_FLASH	nvram_get(nvramsecbuf);#else        memcpy(nvramsecbuf, nvram, NVRAM_SECSIZE);#endif	nvrambuf = nvramsecbuf + (NVRAM_OFFS & (NVRAM_SECSIZE - 1));	/* Etheraddr is special case to save space */	if (strcmp("ethaddr", name) == 0) {		char *s = value;		int i;		int32_t v;		for(i = 0; i < 6; i++) {			gethex(&v, s, 2);			hwethadr[i] = v;			s += 3;         /* Don't get to fancy here :-) */		} 	} else {		ep = nvrambuf+2;		if(*ep != '\0') {			do {				while(*ep++ != '\0');			} while(*ep++ != '\0');			ep--;		}		if(((int)ep + NVRAM_SIZE - (int)ep) < (envlen + 1)) {			free(nvramsecbuf);			return(0);      /* Bummer! */		}		/*		 *  Special case heaptop must always be first since it		 *  can change how memory allocation works.		 */		if(strcmp("heaptop", name) == 0) {			bcopy(nvrambuf+2, nvrambuf+2 + envlen,				 ep - nvrambuf+1);			ep = nvrambuf+2;			while(*name != '\0') {				*ep++ = *name++;			}			if(value != NULL) {				*ep++ = '=';				while((*ep++ = *value++) != '\0');			}			else {				*ep++ = '\0';			}		}		else {			while(*name != '\0') {				*ep++ = *name++;			}			if(value != NULL) {				*ep++ = '=';				while((*ep++ = *value++) != '\0');			}			else {				*ep++ = '\0';			}			*ep++ = '\0';   /* End of env strings */		}	}        cksum(nvrambuf, NVRAM_SIZE, 1);	bcopy(hwethadr, &nvramsecbuf[ETHER_OFFS], 6);#ifdef NVRAM_IN_FLASH        if(fl_erase_device(nvram, NVRAM_SECSIZE, FALSE)) {		printf("Error! Nvram erase failed!\n");		free(nvramsecbuf);                return(0);        }        if(fl_program_device(nvram, nvramsecbuf, NVRAM_SECSIZE, FALSE)) {		printf("Error! Nvram program failed!\n");		free(nvramsecbuf);                return(0);        }#else	nvram_put(nvramsecbuf);#endif	free(nvramsecbuf);        return(1);}/* *  Calculate checksum. If 'set' checksum is calculated and set. */static intcksum(void *p, size_t s, int set){	u_int16_t sum = 0;	u_int8_t *sp = p;	int sz = s / 2;	if(set) {		*sp = 0;	/* Clear checksum */		*(sp+1) = 0;	/* Clear checksum */	}	while(sz--) {		sum += (*sp++) << 8;		sum += *sp++;	}	if(set) {		sum = -sum;		*(u_int8_t *)p = sum >> 8;		*((u_int8_t *)p+1) = sum;	}	return(sum);}#ifndef NVRAM_IN_FLASH/* *  Read and write data into non volatile memory in clock chip. */voidnvram_get(char *buffer){	int i;	for(i = 0; i < 256; i++) {		outb(RTC_BASE+0x10, i);	/* Address */		buffer[i] = inb(RTC_BASE+0x13);	}}voidnvram_put(char *buffer){	int i;	for(i = 0; i < 256; i++) {		outb(RTC_BASE+0x10, i);	/* Address */		outb(RTC_BASE+0x13, buffer[i]);	}}#endif/* *  Simple display function to display a 4 char string or code. *  Called during startup to display progress on any feasible *  display before any serial port have been initialized. */voidtgt_display(char *msg, int x){	/* Have simple serial port driver */	tgt_putchar(msg[0]);	tgt_putchar(msg[1]);	tgt_putchar(msg[2]);	tgt_putchar(msg[3]);	tgt_putchar('\r');	tgt_putchar('\n');}voidclrhndlrs(){}inttgt_getmachtype(){	return(md_cputype());}/* *  Create stubs if network is not compiled in */#ifdef INETvoidtgt_netpoll(){	splx(splhigh());}#elseextern void longjmp(label_t *, int);void gsignal(label_t *jb, int sig);voidgsignal(label_t *jb, int sig){	if(jb != NULL) {		longjmp(jb, 1);	}}int	netopen (const char *, int);int	netread (int, void *, int);int	netwrite (int, const void *, int);long	netlseek (int, long, int);int	netioctl (int, int, void *);int	netclose (int);int netopen(const char *p, int i)	{ return -1;}int netread(int i, void *p, int j)	{ return -1;}int netwrite(int i, const void *p, int j)	{ return -1;}int netclose(int i)	{ return -1;}long int netlseek(int i, long j, int k)	{ return -1;}int netioctl(int j, int i, void *p)	{ return -1;}void tgt_netpoll()	{}#endif /*INET*/

⌨️ 快捷键说明

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