写驱动的朋友都应该知道,有些时候驱动程序编译没有出错,运行也没有出现错误,但是运行却达不到我们想要的效果,这个时候调试真的让你束手无策了,这时候我们就可以想办法直接查看跟硬件相关寄存器的值,但是Linux内核并没这样的工具,所以自己制作了一个查看寄存器的值得工具。
先动态加载我们调试程序的工具,然后执行我们调试工具的应用程序就可以实现查看寄存器的值或者给寄存器写值。
下面红色部分不能乱定义,Linux-2.6.31之后的版本已经明确规定了,各个cmd有自己的含义,具体内容参考这篇文章http://blog.csdn.net/qq_21792169/article/details/51090001 。 开始我依次定义成0 1 2 3 4 5。就出错了,我找这个原因花了两个小时,老版本内核并没有这样的要求哦。就来查看ioctl-number.txt文件才找到原因了。
友情提示:不要轻易换内核版本,有些内核中还是有BUG的。
regeditor_drv.c /* 驱动程序 */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define KER_RW_R8 100
#define KER_RW_R16 101 /* 切记!cmd命令值不能乱定义 */
#define KER_RW_R32 102
#define KER_RW_W8 103
#define KER_RW_W16 104
#define KER_RW_W32 105
static int major;
static struct class *class;
static long ker_rw_ioctl( struct file *file, unsigned int cmd, unsigned long arg)
{
volatile unsigned char *p8;
volatile unsigned short *p16;
volatile unsigned int *p32;
unsigned int val;
unsigned int addr;
unsigned int buf[2];
copy_from_user(buf, (const void __user *)arg,8);
addr = buf[0];
val = buf[1];
p8 = (volatile unsigned char *)ioremap(addr, 4);
p16 =p8;
p32 = p8;
switch (cmd)
{
case KER_RW_R8:
{
val = *p8;
copy_to_user((void __user *)(arg+4), &val, 4);
break;
}
case KER_RW_R16:
{
val = *p16;
copy_to_user((void __user *)(arg+4), &val, 4);
break;
}
case KER_RW_R32:
{
val = *p32;
copy_to_user((void __user *)(arg+4), &val, 4);
break;
}
case KER_RW_W8:
{
*p8 = val;
break;
}
case KER_RW_W16:
{
*p16 = val;
break;
}
case KER_RW_W32:
{
*p32 = val;
break;
}
}
iounmap(p8);
return 0;
}
static struct file_operations ker_rw_ops = {
.owner = THIS_MODULE,
.unlocked_ioctl = ker_rw_ioctl,
};
static int ker_rw_init(void)
{
major = register_chrdev(0, "ker_rw", &ker_rw_ops);
class = class_create(THIS_MODULE, "ker_rw");
/* 为了让mdev根据这些信息来创建设备节点 */
device_create(class, NULL, MKDEV(major, 0), NULL, "ker_rw"); /* /dev/ker_rw */
return 0;
}
static void ker_rw_exit(void)
{
device_destroy(class,MKDEV(major, 0));
class_destroy(class);
unregister_chrdev(major, "ker_rw");
}
module_init(ker_rw_init);
module_exit(ker_rw_exit);
MODULE_LICENSE("GPL");
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define KER_RW_R8 100
#define KER_RW_R16 101
#define KER_RW_R32 102
#define KER_RW_W8 103
#define KER_RW_W16 104
#define KER_RW_W32 105
/* Usage:
* ./regeditor r8 addr [num] /* 用法 */
* ./regeditor r16 addr [num]
* ./regeditor r32 addr [num]
*
* ./regeditor w8 addr val
* ./regeditor w16 addr val
* ./regeditor w32 addr val
*/
void print_usage(char *file)
{
printf("Usage:\n");
printf("%s [num]\n", file);
printf("%s \n", file);
}
int main(int argc, char **argv)
{
int fd;
unsigned int buf[2];
unsigned int i;
unsigned int num;
if ((argc != 3) && (argc != 4))
{
print_usage(argv[0]);
return -1;
}
fd = open("/dev/ker_rw", O_RDWR);
if (fd < 0)
{
printf("can't open /dev/ker_rw\n");
return -2;
}
/* addr */
buf[0] = strtoul(argv[2], NULL, 0);
if (argc == 4)
{
buf[1] = strtoul(argv[3], NULL, 0);
num = buf[1];
}
else
{
num = 1;
}
if (strcmp(argv[1], "r8") == 0)
{
for ( i = 0; i < num; i++)
{
ioctl(fd, KER_RW_R8, buf); /* val = buf[1] */
printf("%02d. [%08x] = %02x\n", i, buf[0], (unsigned char)buf[1]);
buf[0] += 1;
}
}
else if (strcmp(argv[1], "r16") == 0)
{
for ( i = 0; i < num; i++)
{
ioctl(fd, KER_RW_R16, buf); /* val = buf[1] */
printf("%02d. [%08x] = %04x\n", i, buf[0], (unsigned short)buf[1]);
buf[0] += 2;
}
}
else if (strcmp(argv[1], "r32") == 0)
{
for ( i = 0; i < num; i++)
{
ioctl(fd, KER_RW_R32, buf); /* val = buf[1] */
printf("%02d. [%08x] = %08x\n", i, buf[0], (unsigned int)buf[1]);
buf[0] += 4;
}
}
else if (strcmp(argv[1], "w8") == 0)
{
ioctl(fd, KER_RW_W8, buf); /* val = buf[1] */
}
else if (strcmp(argv[1], "w16") == 0)
{
ioctl(fd, KER_RW_W16, buf); /* val = buf[1] */
}
else if (strcmp(argv[1], "w32") == 0)
{
ioctl(fd, KER_RW_W32, buf); /* val = buf[1] */
}
else
{
printf(argv[0]);
return -1;
}
return 0;
}
Makefile:
KERN_DIR = /home/book/workspace/kernel/linux-3.4.2 /* Linux内核路径 */
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m +=regeditor——drv.o
驱动程序编译:make
应用程序编译:arm-linux-gcc regeditor.c -o regeditor