驱动部分见前一篇文章:http://blog.youkuaiyun.com/bripengandre/archive/2009/03/20/4009615.aspx
应用程序部分很简单:读取新的BIOS文件 ,以及flash中的当前内容,如果两者有差异,则将相关扇区先擦掉,然后再将新的BIOS内容写进去.即程序的擦写策略为:只有有差异时,才擦写,这能减小程序在擦写时,突然断电带来的坏作用.
不说什么了,直接看源码.
/****************************RefreshBIOS.h*****************************************/
#ifndef _REFRESH_BIOS_H_
#define _REFRESH_BIOS_H_
#define SPI_IOCTL_BASE 0xDE
#define IOCTL_ERASE_CHIP _IOWR(SPI_IOCTL_BASE, 0, ioctl_arg_t)
#define IOCTL_ERASE_BLOCK _IOWR(SPI_IOCTL_BASE, 1, ioctl_arg_t)
#define SPI_NAME "/dev/spi_name"
#define MAJOR_ID 252
#define MINOR_ID 0
#define CMD_SIZE 250
#define BUF_SIZE 500
#define SPI_FLASH_SIZE (2048*1024)
#define SPI_SECTOR_SIZE 0x1000
typedef struct _erase_block
{
int start;
int end;
}erase_block_t;
typedef struct _ioctl_arg
{
unsigned int type;
union
{
struct
{
unsigned int start;
unsigned int end;
}erase_range;
}data;
}ioctl_arg_t;
#endif /* _REFRESH_BIOS_H_ */
/**************************RefreshBIOS.c*****************************************/
#include "RefreshBIOS.h"
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
static int write_range(int start, int end);
static int check_block(erase_block_t *block_old, erase_block_t *block_new);
static ssize_t readn(int fd, void *buf, size_t n);
static ssize_t writen(int fd, const void *buf, size_t n);
static void dbg_print(char *buf_s, char *buf_d, int cnt);
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