#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
int main()
{
int ifd,ofd;
int begin = 0x50000;
int end = 0x249999;
struct stat st;
unsigned char* ptr;
char* fname ="EV3 Firmware V1.06E.bin";
char* ofile ="uImage";
ifd = open(fname,O_RDONLY);
if(ifd == -1)
return -1;
ofd = open(ofile,O_CREAT | O_TRUNC | O_RDWR,0664);
if(ofd == -1)
return -1;
fstat(ifd,&st);
ptr = mmap(NULL,st.st_size,PROT_READ,MAP_SHARED,ifd,0);
if(ptr == MAP_FAILED)
return -1;
while(ptr[end] == 0xFF && end > begin )
end--;
write(ofd,ptr+begin, end-begin+1);
munmap(ptr,st.st_size);
close(ofd);
close(ifd);
return 0;
}