Skip to content

Instantly share code, notes, and snippets.

@jsfaint
Created June 20, 2014 09:36
Show Gist options
  • Save jsfaint/8f486a3ceb14e6b21dea to your computer and use it in GitHub Desktop.
Save jsfaint/8f486a3ceb14e6b21dea to your computer and use it in GitHub Desktop.
/*++
Module Name:
write_ver.c
Abstract:
Add version string in wrp file header for VSC7398.
A part of auto provision function implement.
Author:
jason (jasonjia@net-star.com.cn) Dec-01-2009
Compile:
compatible with ansi c, you can compile this tool with any ansi C Compiler.
--*/
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
/*
* struct of wrp file head. (32 bytes)
0x00 - 0x03: unsigned long signature; //image signature, 1234567820H
0x04 : unsigned char header_len; //image address
0x05 : unsigned char header_version;
0x06 - 0x07: unsigned short file_checksum; //file_checksum;
0x08 - 0x0B: unsigned long file_len;
0x0C : unsigned char file_type;
0xOD : unsigned char header_checksum_adjust;
0x0E - 0x0F: unsigned short chip_part_no;
0x10 0x1F: unsigned char kernel_version;
*/
#define VERSION_OFFSET 0x10
long getfilesize(char *filename);
int main(int argc, char** argv)
{
char version_filename[] = "..\\src\\config\\version.c";
char szPrefix[] = "const uchar far kernel_version [] = \"";
char* buf = NULL;
char* start = NULL;
char* end = NULL;
FILE *fp = NULL, *fp_ver = NULL;
char version[16];
if (argc <2) {
puts("Bad arguments!\nPlease exec as follow: write_ver d:\\lutnup8.wrp");
exit(0);
}
fp = fopen(argv[1], "r+");
if (fp == NULL)
{
printf("fopen \"%s\" failed.\n", argv[1]);
exit(0);
}
long filesize = getfilesize(version_filename);
fp_ver = fopen(version_filename, "r");
if (fp_ver == NULL) {
puts("open version.c failed.");
exit(0);
}
buf = (char*)malloc(filesize);
if (buf == NULL)
{
puts("malloc memory failed\n");
exit(0);
}
fread(buf, filesize, 1, fp_ver); //load version.c to buf.
start = (char*)strstr(buf, szPrefix); //search szPrefix
end = (char*)strstr(start, "\";"); //search the end double quotation marks.
*end = 0x00;
memset(version, 0, sizeof(version));
memcpy(version, start+strlen(szPrefix)+1, (end-start));
/*write Kennel version.*/
fseek(fp, VERSION_OFFSET, SEEK_SET);
fprintf(fp, "%s", version);
fclose(fp);
fclose(fp_ver);
return 0;
}
long getfilesize(char *filename)
{
FILE *fp;
long lRet;
if ((fp = fopen(filename, "r")) == NULL)
return 0;
fseek(fp, 0, SEEK_END);
lRet = ftell(fp);
fclose(fp);
return lRet;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment