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

📄 wrapcsys.c

📁 Boot code for ADM5120 with serial console for Edimax router.
💻 C
字号:
/* *	Tool to wrap a stripped ELF kernel image in a CSYS wrapper *	Original code found in EdiLinux/AP/goahead-2.1.1/LINUX/cvimg.c *	Original Author:		David Hsu	<davidhsu@realtek.com.tw> *	Modified by:			Dan Everett *	Copyright 2006 Dan Everett, All Rights Reserved */#include <stdlib.h>#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <unistd.h>#include <string.h>/*	Start of things pulled from apmib.h */#define FW_HEADER			((char *) "CSYS")#define DEFAULT_START_ADDR	0x80500000#define SIGNATURE_LEN		4#define WORD_SWAP(v)		(v)#define DWORD_SWAP(v)		(v)typedef struct img_header {	unsigned char signature[SIGNATURE_LEN] __attribute__ ((packed));	unsigned long startAddr __attribute__ ((packed));	unsigned long len __attribute__ ((packed));} IMG_HEADER_T, *IMG_HEADER_Tp;/*	End of things pulled from apmib.h */unsigned short calculateChecksum(char *buf, int len){	int i, j;	unsigned short tmp, sum = 0;	j = (len / 2) * 2;	for (i = 0; i < j; i += 2) {		tmp = *((unsigned short *) (buf + i));		sum += WORD_SWAP(tmp);	}	if (len % 2) {		tmp = buf[len - 1];		sum += WORD_SWAP(tmp);	}	return ~sum + 1;}int main(int argc, char** argv){//	char inFile[80] = {0}, outFile[80] = {0};	char *infile = NULL, *outfile = NULL;	int fh, size;	struct stat status;	char *buf;//	unsigned long startAddr;	unsigned int startAddr;	unsigned short checksum;	IMG_HEADER_Tp pHeader;	/* Parse input arguments */	if (argc != 3 && argc != 4) {		printf("Usage: %s input-file output-file [start-addr]\n", basename(argv[0]));		printf("       \"start-addr\" is optional.  If not supplied,\n");		printf("       the default value of %.8X will be used.\n", DEFAULT_START_ADDR);		return 1;	}	infile  = argv[1];	outfile = argv[2];//	sscanf(argv[1], "%s", inFile);//	sscanf(argv[2], "%s", outFile);	if (argc == 4)		sscanf(argv[3], "%x", &startAddr);	else		startAddr = DEFAULT_START_ADDR;	printf("\nUsing start address: %.8X\n\n", startAddr);	/* Check input file and allocate a buffer *///	if (stat(inFile, &status) < 0) {	if (stat(infile, &status) < 0) {		printf("Can't stat file \"%s\"\n", infile);		return 2;	}	size = status.st_size + sizeof(IMG_HEADER_T) + sizeof(checksum);	if (size % 2)		size++;		/* Length must be an even number */	buf = calloc(size, 1);	if (buf == NULL) {		printf("Failed to allocate a buffer\n");		return 3;	}//	memset(buf, '\0', size);	pHeader = (IMG_HEADER_Tp) buf;	buf += sizeof(IMG_HEADER_T);	/* Read the file and assemble a header *///	fh = open(inFile, O_RDONLY);	fh = open(infile, O_RDONLY);	if (fh == -1) {		printf("Error opening input file %s\n", infile);		free(pHeader);		return 4;	}	lseek(fh, 0L, SEEK_SET);	if (read(fh, buf, status.st_size) != status.st_size) {		printf("Error reading input file %s\n", infile);		close(fh);		free(pHeader);		return 5;	}	close(fh);	/* Set the signature (usually CSYS) */	memcpy(pHeader->signature, FW_HEADER, SIGNATURE_LEN);	pHeader->len = DWORD_SWAP((size - sizeof(IMG_HEADER_T)));	pHeader->startAddr = DWORD_SWAP(startAddr);	/* Calculate and save checksum */	checksum = WORD_SWAP(calculateChecksum(buf, status.st_size));	*((unsigned short *) &buf[size - sizeof(IMG_HEADER_T) - sizeof(checksum)]) = checksum;	/* Write image to output file *///	fh = open(outFile, O_RDWR|O_CREAT|O_TRUNC);	fh = open(outfile, O_RDWR|O_CREAT|O_TRUNC);	if ( fh == -1 ) {		printf("Error opening output file %s\n", outfile);		free(pHeader);		return 6;	}	write(fh, pHeader, size);	close(fh);	/* Set permission bits for new file */	chmod(outfile, S_IRUSR | S_IWRITE | S_IRGRP | S_IROTH);	printf("Image generated successfully.  Length:   %.8X (decimal %d)\n", (unsigned int) DWORD_SWAP(pHeader->len), (int) DWORD_SWAP(pHeader->len));	printf("                               Checksum: %.4X\n\n", checksum);	/* Do this last since those last printf stmts need buffer data! */	free(pHeader);	return 0;}

⌨️ 快捷键说明

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