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

📄 f2mem.c

📁 umon bootloader source code, support mips cpu.
💻 C
📖 第 1 页 / 共 2 页
字号:
		err(1,"Can't open output file:",Outfile);

	if (write(fd,data,size) != size)
		err(1,"Can't write file:",Outfile);

	close(fd);
}

void
buf2srec(uchar *data, int size)
{
	FILE	*fp;
	uchar	*cp, *dp;
	unsigned long	address;
	int		cnt, csum, i;

	if (verbose)
		fprintf(stderr,"Building S-record file...\n");
	
	fp = fopen(Outfile,"w+");
	if (fp == NULL)
		err(1,"Can't open output file:",Outfile);

	/* Create Srecord output file: */
	fprintf(fp,"S0030000FC\n");
	address = SrecBase;
	dp = data;

	while(size) {
		if (size > 32)
			cnt = 32;
		else
			cnt = size;

		/* Print S3 idendifier with byte count for line plus
		   4-byte address: */
		fprintf(fp,"S3%02X%08X",cnt+5,address);

		/* Accumlate checksum subtotal: */
		csum = 0;
		cp = (uchar *)&address;
		csum += (cnt+5);
		csum += cp[0]; csum += cp[1]; csum += cp[2]; csum += cp[3];

		/* Print each byte of data: */
		for(i=0;i<cnt;i++) {
			fprintf(fp,"%02X",dp[i]);
			csum += (int)dp[i];
		}

		/* Print sumcheck (includes bytecount, address & data): */
		/* (binary one's compliment) */
		fprintf(fp,"%02X\n",255 - (csum & 0xff));
		size -= cnt;
		dp += cnt;
		address += cnt;
	}
	fprintf(fp,"S705000019E001\n");
	fclose(fp);
}

main(int argc,char *argv[])
{
	unsigned long	tfsoffset, flashbase, padto;
	struct	stat statbuf;
	struct	finfo	*fip;
	int		ftot, totalmem;
	int		i, opt, ifd, size, swapmonitor, swapall;
	uchar	fillbyte;
	struct	tfshdr	hdrbuf;
	char	*membase, *memptr, outtype;

	if (argc == 1)
		usage(0);

	endianSwap = 1;
	swapmonitor = swapall = 0;
	TFSHeaderVersion = 1;
	SrecBase = 0;
	verbose = 0;
	padto = 0;
	outtype = 's';
	tfsoffset = 0;
	flashbase = 0;
	Outfile = (char *)0;
	fillbyte = 0xff;
	for(i=0;i<MAXFILES;i++)
		FileTable[0].name = (char *)0;
	ftot = 1;

	while ((opt=getopt(argc,argv,"a:B:b:ef:m:O:o:p:s:S:t:T:vV")) != EOF) {
		switch(opt) {
		case 'a':	/* ascii file */
			storeFileinfo(ftot++,optarg,TYPE_ASCII);
			break;
		case 'B':	/* CPU address that base of flash resides at */
			flashbase = strtoul(optarg,(char **)0,0);
			break;
		case 'b':	/* binary file */
			storeFileinfo(ftot++,optarg,TYPE_BINARY);
			break;
		case 'e':	/* disable endian swap */
			endianSwap = 0;
			break;
		case 'f':	/* filler byte */
			fillbyte = (uchar)strtoul(optarg,(char **)0,0);
			break;
		case 'm':	/* monitor file */
			/* Slot 0 of the file table is reserved for the monitor. */
			if (FileTable[0].name)
				err(1,"only one monitor file should be specified",0);
			FileTable[0].name = optarg;
			FileTable[0].type = TYPE_MONITOR;
			break;
		case 'O':	/* output file type */
			outtype = *optarg;
			if ((outtype != 's') && (outtype != 'b'))
				err(1,"output type must be 's' or 'b'",0);
			break;
		case 'o':	/* output file */
			Outfile = optarg;
			break;
		case 'p':	/* output file */
			padto = strtoul(optarg,(char **)0,0);
			break;
		case 's':	/* swap binary */
			if (*optarg == 'm')
				swapmonitor = 1;
			else if (*optarg == 'a')
				swapall = 1;
			else {
				fprintf(stderr,"Invalid argument to 's' option\n");
				usage(0);
			}
			break;
		case 'S':	/* S-record base */
			SrecBase = strtoul(optarg,(char **)0,0);
			break;
		case 'T':	/* TFS header version */
			TFSHeaderVersion = atoi(optarg);
			break;
		case 't':	/* TFS offset */
			tfsoffset = strtoul(optarg,(char **)0,0);
			break;
		case 'V':
			showVersion();
			break;
		case 'v':
			verbose++;
			break;
		default:
			usage(0);
		}
	}

	if (!FileTable[0].name)
		err(1,"monitor file must be specified",0);
	
	if (stat(FileTable[0].name,&statbuf) < 0)
		err(1,"can't find file:",FileTable[0].name);

	if (statbuf.st_size > tfsoffset) {
		fprintf(stderr,"TFS offset = 0x%x, monitor size = 0x%x\n",
			tfsoffset,statbuf.st_size);
		err(1,"TFS offset must be greater than size of monitor",0);
	}

	if (swapall & swapmonitor)
		err(1,"Can't swap monitor, then swap all... Do one or the other.",0);

	FileTable[0].size = statbuf.st_size;

	/* Now build each of the FileTable entries after the monitor... */
	/* Determine the size of the file, and copy the entire file into a */
	/* newly allocated buffer pointed to by filecontent... */
	/* Also keep track of just how much memory must be allocated to */
	/* fit the entire image into (store this in totalmem). */
	totalmem = tfsoffset;
	fip = &FileTable[1];
	for(i=1;i<ftot;i++) {
		if (stat(fip->name,&statbuf) < 0)
			err(1,"can't find file:",fip->name);
		totalmem += statbuf.st_size + TFSHDRSIZ;
		fip->size = statbuf.st_size;
		
		if ((ifd = open(fip->name,O_RDONLY | O_BINARY)) < 0)
			err(1,"can't open file:",fip->name);
		fip->filecontent = malloc(fip->size);
		if (!fip->filecontent)
			err(1,"can't allocate memory",0);

		if (read(ifd,fip->filecontent,fip->size) != fip->size)
			err(1,"read failed:",fip->name);
		close(ifd);
		fip++;
	}
	totalmem;

	memptr = membase = malloc(totalmem+1024);
	if (!memptr)
		err(1,"can't allocate memory",0);

	/* Load the memory with the fill byte prior to loading anything else... */
	memset(membase,fillbyte,totalmem+1024);

	if (verbose > 1)
		fprintf(stderr,"TFS header size: %d\n",TFSHDRSIZ);

	/* First transfer the monitor file directly into allocated space... */
	fip = &FileTable[0];
	if ((ifd = open(fip->name,O_RDONLY | O_BINARY)) < 0)
		err(1,"can't open file:",fip->name);

	if (read(ifd,memptr,fip->size) != fip->size)
		err(1,"read failed:",fip->name);

	close(ifd);

	if (swapmonitor) {
		char tmp;
		i = 0;
		while(i < fip->size) {
			tmp = membase[i];
			membase[i] = membase[i+1];
			membase[i+1] = tmp;
			i += 2;
		}
	}

	if (verbose) {
		fprintf(stderr,"Processing files...\n");
		fprintf(stderr," 0: %-20s @ 0x%08x (%d bytes)\n",
			fip->name,flashbase,fip->size);
	}

	/* Now that we have loaded the monitor binary into ram, adjust the		*/
	/* memory pointer to the start of TFS and begin loading each file and	*/
	/* create the TFS header for each... */
	memptr += tfsoffset;
	fip = &FileTable[1];
	for(i=1;i<ftot;i++,fip++) {
		int	alignadjust;

		/* If filetype is TYPE_ASCII, strip out all CRs first... */
		if (fip->type == TYPE_ASCII) {
			int	j, choppedCRcnt;
			char *from, *to;

			choppedCRcnt = 0;
			from = to = fip->filecontent;
			for(j=0;j<fip->size;j++,from++) {
				if (*from == 0x0d)
					choppedCRcnt++;
				else
					*to++ = *from;
			}
			fip->size -= choppedCRcnt;
			totalmem -= choppedCRcnt;
		}

		if (verbose)
			fprintf(stderr,"%2d: %-20s @ 0x%08x (%d bytes)\n",
				i,fip->name,flashbase+(memptr-membase)+TFSHDRSIZ,fip->size);

		/* Build the header: */
		alignadjust = buildTfsHeader(flashbase+(memptr-membase),fip,&hdrbuf);

		/* Transfer header to memory: */
		memcpy(memptr,(char *)&hdrbuf,TFSHDRSIZ);
		memptr += TFSHDRSIZ;

		/* Copy file content to buffer... */
		memcpy(memptr,fip->filecontent,fip->size);
		memptr += fip->size;
		totalmem += alignadjust;
		memptr += alignadjust;
	}

	/* If padto is non-zero, and it is smaller than the current value of */
	/* totalmem, then use the fillbyte value to padd the memory out to the */
	/* size specified by padto... */
	if (padto) {
		char *paddedspace;

		if (padto > totalmem) {
			paddedspace = realloc(membase,padto);
			if (!paddedspace)
				fprintf(stderr,"Can't allocate space for padding\n");
			else {
				memset(paddedspace+totalmem,fillbyte,padto-totalmem);
				totalmem = padto;
				membase = paddedspace;
			}
		}
		else {
			fprintf(stderr,
				"Warning: pad-to value (0x%x) <= total memspace (0x%x)\n", 
				padto,totalmem);
		}
	}

	if (verbose)
		fprintf(stderr,"Total flash space required: %d (0x%x) bytes.\n",
			totalmem,totalmem);
	if (verbose > 1) {
		unsigned long	gap;
		gap = tfsoffset - FileTable[0].size;
		fprintf(stderr,
			"Gap between end of monitor and TFS: %d (0x%x) bytes.\n",gap,gap);
	}
			
	if (swapall) {
		char tmp;
		i = 0;
		while(i < totalmem) {
			tmp = membase[i];
			membase[i] = membase[i+1];
			membase[i+1] = tmp;
			i += 2;
		}
	}

	/* Now convert buffer to Srecord... */
	if (outtype == 's') {
		if (!Outfile)
			Outfile = "mem.srec";
		buf2srec(membase,totalmem);
	}
	else {
		if (!Outfile)
			Outfile = "mem.bin";
		buf2bin(membase,totalmem);
	}

	if (verbose)
		fprintf(stderr,"Output saved to file %s.\n",Outfile);

	/* Cleanup... */
	for(i=1;i<ftot;i++) 
		free(FileTable[i].filecontent);
	
	free(membase);
	exit(0);
}

⌨️ 快捷键说明

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