#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 = -1; static char *devn = "/dev/sda";
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_param(devn,charp,S_IRUGO); MODULE_PARM_DESC(devn,"which device"); MODULE_LICENSE("GPL");
static struct block_device *bdev; static char *usage = "You can change the value:set_size nr devn";
static int __init init_read(void) { struct buffer_head *bh = NULL; int size; if(nr == -1) { printk("Using this programm,you need set \"nr\"\n"); printk("%s\n",usage); return -1; } printk("read disk\n"); bdev = open_bdev_excl(devn,0x8000,NULL); if(IS_ERR(bdev)) { printk("open failed\n"); return PTR_ERR(bdev); } size = bdev_hardsect_size(bdev); printk("size = %d\n",size); if(set_blocksize(bdev,set_size)){ printk("set block size error\n"); return -1; } /* read disk */ bh = __bread(bdev,nr,set_size); if(bh == NULL) return 0; //if you want to modify the disk contents,do this operations
memset(bh->b_data,0x30,set_size); set_buffer_uptodate(bh); /* write disk */ mark_buffer_dirty(bh); if(bh) brelse(bh); return 0; }
static void __exit exit_read(void) { printk("exit\n"); close_bdev_excl(bdev); }
module_init(init_read); module_exit(exit_read);
|