/*----------------------------------------------------------------
|
/*----------------------------------------------------------------
|
// //
|
// //
|
// amber-bin2mem.c //
|
// amber-bin2mem.c //
|
// //
|
// //
|
// This file is part of the Amber project //
|
// This file is part of the Amber project //
|
// http://www.opencores.org/project,amber //
|
// http://www.opencores.org/project,amber //
|
// //
|
// //
|
// Description //
|
// Description //
|
// Read in a binary file from tcpdump and write it out in //
|
// Read in a binary file from tcpdump and write it out in //
|
// in Verilog readmem format. //
|
// in Verilog readmem format. //
|
// //
|
// //
|
// Author(s): //
|
// Author(s): //
|
// - Conor Santifort, csantifort.amber@gmail.com //
|
// - Conor Santifort, csantifort.amber@gmail.com //
|
// //
|
// //
|
//////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////
|
// //
|
// //
|
// Copyright (C) 2010 Authors and OPENCORES.ORG //
|
// Copyright (C) 2010 Authors and OPENCORES.ORG //
|
// //
|
// //
|
// This source file may be used and distributed without //
|
// This source file may be used and distributed without //
|
// restriction provided that this copyright statement is not //
|
// restriction provided that this copyright statement is not //
|
// removed from the file and that any derivative work contains //
|
// removed from the file and that any derivative work contains //
|
// the original copyright notice and the associated disclaimer. //
|
// the original copyright notice and the associated disclaimer. //
|
// //
|
// //
|
// This source file is free software; you can redistribute it //
|
// This source file is free software; you can redistribute it //
|
// and/or modify it under the terms of the GNU Lesser General //
|
// and/or modify it under the terms of the GNU Lesser General //
|
// Public License as published by the Free Software Foundation; //
|
// Public License as published by the Free Software Foundation; //
|
// either version 2.1 of the License, or (at your option) any //
|
// either version 2.1 of the License, or (at your option) any //
|
// later version. //
|
// later version. //
|
// //
|
// //
|
// This source is distributed in the hope that it will be //
|
// This source is distributed in the hope that it will be //
|
// useful, but WITHOUT ANY WARRANTY; without even the implied //
|
// useful, but WITHOUT ANY WARRANTY; without even the implied //
|
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
|
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
|
// PURPOSE. See the GNU Lesser General Public License for more //
|
// PURPOSE. See the GNU Lesser General Public License for more //
|
// details. //
|
// details. //
|
// //
|
// //
|
// You should have received a copy of the GNU Lesser General //
|
// You should have received a copy of the GNU Lesser General //
|
// Public License along with this source; if not, download it //
|
// Public License along with this source; if not, download it //
|
// from http://www.opencores.org/lgpl.shtml //
|
// from http://www.opencores.org/lgpl.shtml //
|
// //
|
// //
|
----------------------------------------------------------------*/
|
----------------------------------------------------------------*/
|
|
|
|
|
#include <stdio.h>
|
#include <stdio.h>
|
#include <stdlib.h>
|
#include <stdlib.h>
|
#include "../boot-loader/boot-loader.h"
|
#include "../boot-loader-serial/boot-loader.h"
|
|
|
/* Function prototypes */
|
/* Function prototypes */
|
int fsize(FILE *f);
|
int fsize(FILE *f);
|
int conv_hstring(char*, unsigned int*);
|
int conv_hstring(char*, unsigned int*);
|
unsigned int buf_uint (unsigned char *buf);
|
unsigned int buf_uint (unsigned char *buf);
|
|
|
|
|
int main(int argc,char *argv[])
|
int main(int argc,char *argv[])
|
{
|
{
|
FILE *infile;
|
FILE *infile;
|
char filename_mem[80], filename_nopath[80];
|
char filename_mem[80], filename_nopath[80];
|
unsigned char *buf;
|
unsigned char *buf;
|
FILE *file_mem;
|
FILE *file_mem;
|
int infile_size, buf_size, i, j;
|
int infile_size, buf_size, i, j;
|
int ret;
|
int ret;
|
int buf_pos;
|
int buf_pos;
|
unsigned int incl_len;
|
unsigned int incl_len;
|
unsigned int orig_len;
|
unsigned int orig_len;
|
int mem_pos = 0;
|
int mem_pos = 0;
|
|
|
if (argc<2){
|
if (argc<2){
|
fprintf(stderr, "ERROR: no input file specified. Quitting\n");
|
fprintf(stderr, "ERROR: no input file specified. Quitting\n");
|
exit(1);
|
exit(1);
|
}
|
}
|
|
|
|
|
infile=fopen(argv[1],"rb");
|
infile=fopen(argv[1],"rb");
|
if(infile==NULL) {
|
if(infile==NULL) {
|
fprintf(stderr, "ERROR: Can't open %s. Quitting\n", argv[1]);
|
fprintf(stderr, "ERROR: Can't open %s. Quitting\n", argv[1]);
|
exit(1);
|
exit(1);
|
}
|
}
|
infile_size = fsize(infile);
|
infile_size = fsize(infile);
|
|
|
buf=(unsigned char*)malloc(infile_size);
|
buf=(unsigned char*)malloc(infile_size);
|
buf_size=fread(buf,1,infile_size,infile);
|
buf_size=fread(buf,1,infile_size,infile);
|
fclose(infile);
|
fclose(infile);
|
|
|
if ( buf_size != infile_size ) {
|
if ( buf_size != infile_size ) {
|
fprintf(stderr, "%s ERROR: Input %s file length is %d bytes long, buffer read buf_size %d\n",
|
fprintf(stderr, "%s ERROR: Input %s file length is %d bytes long, buffer read buf_size %d\n",
|
argv[0], argv[1], infile_size, buf_size);
|
argv[0], argv[1], infile_size, buf_size);
|
exit(1);
|
exit(1);
|
}
|
}
|
|
|
|
|
if ( infile_size > 0x1000000 ) {
|
if ( infile_size > 0x1000000 ) {
|
fprintf(stderr, "%s WARNING: Input %s file length is %d bytes long, greater than boot-loader can handle \n",
|
fprintf(stderr, "%s WARNING: Input %s file length is %d bytes long, greater than boot-loader can handle \n",
|
argv[0], argv[1], infile_size);
|
argv[0], argv[1], infile_size);
|
}
|
}
|
|
|
|
|
/* Start of file */
|
/* Start of file */
|
buf_pos = 6*4;
|
buf_pos = 6*4;
|
|
|
/* Main loop. once per packet */
|
/* Main loop. once per packet */
|
while (buf_pos < infile_size) {
|
while (buf_pos < infile_size) {
|
buf_pos=buf_pos+8;
|
buf_pos=buf_pos+8;
|
incl_len = buf_uint(&buf[buf_pos]); buf_pos+=4;
|
incl_len = buf_uint(&buf[buf_pos]); buf_pos+=4;
|
orig_len = buf_uint(&buf[buf_pos]); buf_pos+=4;
|
orig_len = buf_uint(&buf[buf_pos]); buf_pos+=4;
|
|
|
/* error check included packet length */
|
/* error check included packet length */
|
if (incl_len != orig_len) {
|
if (incl_len != orig_len) {
|
fprintf(stderr, "Warning: the full packet was not captured in the tcpdump file\n");
|
fprintf(stderr, "Warning: the full packet was not captured in the tcpdump file\n");
|
fprintf(stderr, "incl_len %d != orig_len %d\n", incl_len, orig_len);
|
fprintf(stderr, "incl_len %d != orig_len %d\n", incl_len, orig_len);
|
//goto error_return;
|
//goto error_return;
|
}
|
}
|
|
|
/* error check max packet length */
|
/* error check max packet length */
|
if (orig_len > 255) {
|
if (orig_len > 255) {
|
fprintf(stderr, "Error: the packet is too long. Max length is 255 bytes\n");
|
fprintf(stderr, "Error: the packet is too long. Max length is 255 bytes\n");
|
fprintf(stderr, "orig_len %d\n", orig_len);
|
fprintf(stderr, "orig_len %d\n", orig_len);
|
goto error_return;
|
goto error_return;
|
}
|
}
|
|
|
/* check if full packet is contained in file */
|
/* check if full packet is contained in file */
|
if (buf_pos + incl_len > infile_size) {
|
if (buf_pos + incl_len > infile_size) {
|
fprintf(stderr, "Error: end of current packet is after the end of the buffer\n");
|
fprintf(stderr, "Error: end of current packet is after the end of the buffer\n");
|
goto error_return;
|
goto error_return;
|
}
|
}
|
|
|
|
|
printf("// incl_len %d, orig_len %d\n", incl_len, orig_len);
|
printf("// incl_len %d, orig_len %d\n", incl_len, orig_len);
|
printf("// ip-sa %d.%d.%d.%d, ip-da %d.%d.%d.%d, protocol %d\n",
|
printf("// ip-sa %d.%d.%d.%d, ip-da %d.%d.%d.%d, protocol %d\n",
|
buf[buf_pos+26], buf[buf_pos+27], buf[buf_pos+28], buf[buf_pos+29],
|
buf[buf_pos+26], buf[buf_pos+27], buf[buf_pos+28], buf[buf_pos+29],
|
buf[buf_pos+30], buf[buf_pos+31], buf[buf_pos+32], buf[buf_pos+33],
|
buf[buf_pos+30], buf[buf_pos+31], buf[buf_pos+32], buf[buf_pos+33],
|
buf[buf_pos+23]
|
buf[buf_pos+23]
|
);
|
);
|
|
|
/* First byte gives the packet length */
|
/* First byte gives the packet length */
|
printf("@%04x %02x\n", mem_pos++, orig_len);
|
printf("@%04x %02x\n", mem_pos++, orig_len);
|
for (i=buf_pos;i<buf_pos+incl_len;i++,mem_pos++) {
|
for (i=buf_pos;i<buf_pos+incl_len;i++,mem_pos++) {
|
printf("@%04x %02x\n", mem_pos, buf[i]);
|
printf("@%04x %02x\n", mem_pos, buf[i]);
|
}
|
}
|
|
|
/* pad out any missing bytes */
|
/* pad out any missing bytes */
|
if (orig_len > incl_len) {
|
if (orig_len > incl_len) {
|
for (i=incl_len;i<orig_len;i++,mem_pos++) {
|
for (i=incl_len;i<orig_len;i++,mem_pos++) {
|
printf("@%04x 00\n", mem_pos);
|
printf("@%04x 00\n", mem_pos);
|
}
|
}
|
}
|
}
|
|
|
/* advance to the start of the next entry */
|
/* advance to the start of the next entry */
|
buf_pos += incl_len;
|
buf_pos += incl_len;
|
}
|
}
|
|
|
free(buf);
|
free(buf);
|
return 0;
|
return 0;
|
|
|
|
|
error_return:
|
error_return:
|
free(buf);
|
free(buf);
|
return 1;
|
return 1;
|
}
|
}
|
|
|
|
|
|
|
unsigned int buf_uint (unsigned char *buf)
|
unsigned int buf_uint (unsigned char *buf)
|
{
|
{
|
return buf[3]<<24 | buf[2]<<16 | buf[1]<<8 | buf[0];
|
return buf[3]<<24 | buf[2]<<16 | buf[1]<<8 | buf[0];
|
}
|
}
|
|
|
|
|
/* Return the buf_size of a file in bytes */
|
/* Return the buf_size of a file in bytes */
|
int fsize( FILE *f )
|
int fsize( FILE *f )
|
{
|
{
|
int end;
|
int end;
|
|
|
/* Set current position at end of file */
|
/* Set current position at end of file */
|
fseek( f, 0, SEEK_END );
|
fseek( f, 0, SEEK_END );
|
|
|
/* File buf_size in bytes */
|
/* File buf_size in bytes */
|
end = ftell( f );
|
end = ftell( f );
|
|
|
/* Set current position at start of file */
|
/* Set current position at start of file */
|
fseek( f, 0, SEEK_SET );
|
fseek( f, 0, SEEK_SET );
|
|
|
return end;
|
return end;
|
}
|
}
|
|
|
|
|
int conv_hstring ( char *string, unsigned int * num)
|
int conv_hstring ( char *string, unsigned int * num)
|
{
|
{
|
int pos = 0;
|
int pos = 0;
|
*num = 0;
|
*num = 0;
|
|
|
while (((string[pos] >= '0' && string[pos] <= '9') ||
|
while (((string[pos] >= '0' && string[pos] <= '9') ||
|
(string[pos] >= 'a' && string[pos] <= 'f')) && pos < 9) {
|
(string[pos] >= 'a' && string[pos] <= 'f')) && pos < 9) {
|
if (string[pos] >= '0' && string[pos] <= '9')
|
if (string[pos] >= '0' && string[pos] <= '9')
|
*num = (*num << 4) + ( string[pos++] - '0' );
|
*num = (*num << 4) + ( string[pos++] - '0' );
|
else
|
else
|
*num = (*num << 4) + ( string[pos++] - 'a' ) + 10;
|
*num = (*num << 4) + ( string[pos++] - 'a' ) + 10;
|
}
|
}
|
|
|
return pos;
|
return pos;
|
}
|
}
|
|
|
|
|