下面的程序可以在linux2.6内核直接读写硬盘的指定扇区,也是根据网上一个朋友的做法做了修改的;
有两个不是很明白的地方就是:1、bd_claim函数的使用,这个是个递归函数,像是匹配内存指针和设备,但是调用会返回错误;2、bdev = open_by_devnum(0x00800000, FMODE_READ | FMODE_WRITE); 中0x00800000数字的确认,不知从何而来:
#include <linux/module.h>
#include <linux/init.h>
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/buffer_head.h>
#include <linux/blkdev.h>
#include <linux/msdos_fs.h>
#include <linux/fcntl.h>
#include <linux/delay.h>
static int set_size = 512;
static int nr = 0;
static char pages_addr[PAGE_SIZE];
static char pages_write_addr[PAGE_SIZE];
module_param(set_size,int,S_IRUGO);
MODULE_PARM_DESC(set_size,"how many bytes you want to read,not more than 4096");
module_param(nr,long,S_IRUGO);
MODULE_PARM_DESC(nr,"which sectors you want to read");
MODULE_LICENSE("GPL");
static struct block_device *bdev;
static char *usage = "You can change the value:set_size nr devn";
int bdev_write_one_page(struct block_device *bdev, unsigned long blocknr, void *page_addr)
{
int ret = -1;
struct buffer_head *bh;
if (!bdev || !page_addr)
{
printk("%s error ", __func__);
return -1;