- UID
- 1029342
- 性别
- 男
|
static int fd_s, fd_d;
static char buf_s[BUF_SIZE], buf_d[BUF_SIZE];
int main(int argc, char *argv[])
{
int total, cnt;
ioctl_arg_t arg;
erase_block_t block_old, block_new;
char cmd[CMD_SIZE];
if(argc != 2)
{
fprintf(stderr, "Usage: %s <file_name>/n", argv[0]);
return (1);
}
snprintf(cmd, sizeof(cmd), "rm -f %s ", SPI_NAME);
if(system(cmd) < 0)
{
fprintf(stderr, "%s error: %s/n", cmd, strerror(errno));
return (1);
}
snprintf(cmd, sizeof(cmd), "mknod %s c %d %d ", SPI_NAME, MAJOR_ID, MINOR_ID);
if(system(cmd) < 0)
{
fprintf(stderr, "%s error: %s/n", cmd, strerror(errno));
return (1);
}
if( (fd_d = open(SPI_NAME, O_RDWR)) < 0)
{
fprintf(stderr, "open %s error: %s/n", SPI_NAME, strerror(errno));
return (1);
}
if( (fd_s = open(argv[1], O_RDONLY)) < 0)
{
fprintf(stderr, "open %s error: %s/n", argv[0], strerror(errno));
return (1);
}
block_old.start = -1;
block_old.end = -1;
total = 0;
while(total < SPI_FLASH_SIZE)
{
cnt = (total + BUF_SIZE > SPI_FLASH_SIZE) ? SPI_FLASH_SIZE-total: BUF_SIZE;
if((readn(fd_d, buf_d, cnt)) < cnt)
{
fprintf(stderr, "%s::readn error: %s/n", argv[0], strerror(errno));
return (1);
}
if((readn(fd_s, buf_s, cnt)) < cnt)
{
fprintf(stderr, "%s::readn error: %s/n", argv[0], strerror(errno));
return (1);
}
if(memcmp(buf_s, buf_d, cnt) == 0)
{
total += cnt;
continue;
}
//dbg_print(buf_s, buf_d, cnt);
block_new.start = total;
block_new.end = total+cnt-1;
if(check_block(&block_old, &block_new))
{
arg.data.erase_range.start = block_new.start;
arg.data.erase_range.end = block_new.end;
ioctl(fd_d, IOCTL_ERASE_BLOCK, &arg);
//lseek(fd_s, block_new.start, SEEK_SET);
//lseek(fd_d, block_new.start, SEEK_SET);
if(write_range(block_new.start, block_new.end) < 0)
{
fprintf(stderr, "write_range error. /n");
return (1);
}
total = block_new.end+1;
}
else
{
total += cnt;
}
}
return (0);
}
static int write_range(int start, int end)
{
int cnt, total;
int len = end-start+1;
lseek(fd_s, start, SEEK_SET);
lseek(fd_d, start, SEEK_SET);
total = 0;
while(total < len)
{
cnt = (total + BUF_SIZE > len) ? len-total: BUF_SIZE;
if((readn(fd_s, buf_s, cnt)) < cnt)
{
fprintf(stderr, "write_range::readn error: %s/n", strerror(errno));
return (-1);
}
if((writen(fd_d, buf_s, cnt)) < cnt)
{
fprintf(stderr, "write_range::readn error: %s/n", strerror(errno));
return (-1);
}
total += cnt;
}
return (total);
} |
|