示例#1
0
文件: readnvram.c 项目: keedon/harvey
/* returns with *locp filled in and locp->fd open, if possible */
static void
findnvram(Nvrwhere *locp)
{
	char *nvrlen, *nvroff, *nvrcopy, *db, *v[2];
	int fd, i, safeoff, safelen;

	if (nvrfile == nil) {
		nvrfile = getenv("nvram");
		db = getenv("nvramdebug");
		nvramdebug = db != nil;
		free(db);
	}
	if (cputype == nil)
		cputype = getenv("cputype");
	if(cputype == nil)
		cputype = strdup("mips");
	if(strcmp(cputype, "386")==0 || strcmp(cputype, "alpha")==0) {
		free(cputype);
		cputype = strdup("pc");
	}

	fd = -1;
	safeoff = -1;
	safelen = -1;
	if(nvrfile != nil && *nvrfile != '\0'){
		/* accept device and device!file */
		nvrcopy = strdup(nvrfile);
		i = gettokens(nvrcopy, v, nelem(v), "!");
		if (i < 1) {
			i = 1;
			v[0] = "";
			v[1] = nil;
		}
		if(nvramdebug)
			fprint(2, "nvram at %s?...", v[0]);
		fd = open(v[0], ORDWR);
		if (fd < 0)
			fd = open(v[0], OREAD);
		safelen = sizeof(Nvrsafe);
		if(strstr(v[0], "/9fat") == nil)
			safeoff = 0;
		nvrlen = getenv("nvrlen");
		if(nvrlen != nil)
			safelen = atoi(nvrlen);
		nvroff = getenv("nvroff");
		if(nvroff != nil){
			if(strcmp(nvroff, "dos") == 0)
				safeoff = -1;
			else
				safeoff = atoi(nvroff);
		}
		if(safeoff < 0 && fd >= 0){
			safelen = 512;
			safeoff = finddosfile(fd, i == 2? v[1]: "plan9.nvr");
			if(safeoff < 0){	/* didn't find plan9.nvr? */
				close(fd);
				fd = -1;
			}
		}
		free(nvrcopy);
		free(nvroff);
		free(nvrlen);
	}else{
		for(i=0; i<nelem(nvtab); i++){
			if(strcmp(cputype, nvtab[i].cputype) != 0)
				continue;
			if(nvramdebug)
				fprint(2, "nvram at %s?...", nvtab[i].file);
			if((fd = open(nvtab[i].file, ORDWR)) < 0 &&
			   (fd = open(nvtab[i].file, OREAD)) < 0)
				continue;
			safeoff = nvtab[i].off;
			safelen = nvtab[i].len;
			if(safeoff == -1){
				safeoff = finddosfile(fd, "plan9.nvr");
				if(safeoff < 0){  /* didn't find plan9.nvr? */
					close(fd);
					fd = -1;
					continue;
				}
			}
			nvrfile = strdup(nvtab[i].file);
			break;
		}
		if(i >= nelem(nvtab))		/* tried them all? */
			werrstr("");		/* ignore failed opens */
	}
	locp->fd = fd;
	locp->safelen = safelen;
	locp->safeoff = safeoff;
}
示例#2
0
/*
 *  get key info out of nvram.  since there isn't room in the PC's nvram use
 *  a disk partition there.
 */
int
readnvram(Nvrsafe *safep, int flag)
{
	char buf[1024], in[128], *cputype, *nvrfile, *nvrlen, *nvroff, *v[2];
	int fd, err, i, safeoff, safelen;
	Nvrsafe *safe;

	err = 0;
	memset(safep, 0, sizeof(*safep));

	nvrfile = getenv("nvram");
	cputype = getenv("cputype");
	if(cputype == nil)
		cputype = "mips";
	if(strcmp(cputype, "386")==0 || strcmp(cputype, "alpha")==0)
		cputype = "pc";

	fd = -1;
	safeoff = -1;
	safelen = -1;
	if(nvrfile != nil){
		/* accept device and device!file */
		i = gettokens(nvrfile, v, nelem(v), "!");
		fd = open(v[0], ORDWR);
		safelen = sizeof(Nvrsafe);
		if(strstr(v[0], "/9fat") == nil)
			safeoff = 0;
		nvrlen = getenv("nvrlen");
		if(nvrlen != nil)
			safelen = atoi(nvrlen);
		nvroff = getenv("nvroff");
		if(nvroff != nil){
			if(strcmp(nvroff, "dos") == 0)
				safeoff = -1;
			else
				safeoff = atoi(nvroff);
		}
		if(safeoff < 0 && fd >= 0){
			safelen = 512;
			safeoff = finddosfile(fd, i == 2 ? v[1] : "plan9.nvr");
			if(safeoff < 0){
				close(fd);
				fd = -1;
			}
		}
		free(nvrfile);
		if(nvrlen != nil)
			free(nvrlen);
		if(nvroff != nil)
			free(nvroff);
	}else{
		for(i=0; i<nelem(nvtab); i++){
			if(strcmp(cputype, nvtab[i].cputype) != 0)
				continue;
			if((fd = open(nvtab[i].file, ORDWR)) < 0)
				continue;
			safeoff = nvtab[i].off;
			safelen = nvtab[i].len;
			if(safeoff == -1){
				safeoff = finddosfile(fd, "plan9.nvr");
				if(safeoff < 0){
					close(fd);
					fd = -1;
					continue;
				}
			}
			break;
		}
	}

	if(fd < 0
	|| seek(fd, safeoff, 0) < 0
	|| read(fd, buf, safelen) != safelen){
		err = 1;
		if(flag&(NVwrite|NVwriteonerr))
			fprint(2, "can't read nvram: %r\n");
		memset(safep, 0, sizeof(*safep));
		safe = safep;
	}else{
		memmove(safep, buf, sizeof *safep);
		safe = safep;

		err |= check(safe->machkey, DESKEYLEN, safe->machsum, "bad nvram key");
/*		err |= check(safe->config, CONFIGLEN, safe->configsum, "bad secstore key"); */
		err |= check(safe->authid, ANAMELEN, safe->authidsum, "bad authentication id");
		err |= check(safe->authdom, DOMLEN, safe->authdomsum, "bad authentication domain");
	}

	if((flag&NVwrite) || (err && (flag&NVwriteonerr))){
		xreadcons("authid", nil, 0, safe->authid, sizeof(safe->authid));
		xreadcons("authdom", nil, 0, safe->authdom, sizeof(safe->authdom));
		xreadcons("secstore key", nil, 1, safe->config, sizeof(safe->config));
		for(;;){
			if(xreadcons("password", nil, 1, in, sizeof in) == nil)
				goto Out;
			if(passtokey(safe->machkey, in))
				break;
		}
		safe->machsum = nvcsum(safe->machkey, DESKEYLEN);
		safe->configsum = nvcsum(safe->config, CONFIGLEN);
		safe->authidsum = nvcsum(safe->authid, sizeof(safe->authid));
		safe->authdomsum = nvcsum(safe->authdom, sizeof(safe->authdom));
		memmove(buf, safe, sizeof *safe);
		if(seek(fd, safeoff, 0) < 0
		|| write(fd, buf, safelen) != safelen){
			fprint(2, "can't write key to nvram: %r\n");
			err = 1;
		}else
			err = 0;
	}
Out:
	close(fd);
	return err ? -1 : 0;
}