欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

17.Linux驱动基础-devmem2工具调试工具使用

程序员文章站 2022-06-03 22:40:34
...

在驱动调试中有时候需要看SOC寄存器的值,可以使用devmem2工具来查看,

下载和编译

cd mydroid/external/
git clone https://github.com/giraffesnn/devmem2.git

cd mydroid
source build/envsetup.sh
lunch  xxxx
cd mydroid/external/devmem2/
mm

adb root
adb remount 
adb push out/target/product/xxx/system/bin/devmem2 /system/bin
    
chmod +x devmem2    

配置

kernel config 要增加 CFG_DEVMEM选项。打开这个选项后,板子上会出现 /dev/mem节点,对应的源代码在drivers/char/mem.c,在config文件中或者在驱动代码中定义

CFG_DEVMEM = y

使用

devmem2 { address } [ type [ data ] ]
address : 物理地址
向改地址写入数据,需要填入下面的参数;只是读取,省略即可
type :要访问的数据类型 : [b]yte, [h]alfword, [w]ord
data :想要写入的数据

例子:

读取:

地址:0xFE000000
板子上执行:devmem2 0xFE000000

修改

同时你也可以修改源代码,一次性读取多个寄存器,比如下面修改一次性读取7500个寄存器

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
  
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
  __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
 
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)

static inline void *fixup_addr(void *addr, size_t size);

int main(int argc, char **argv) {
    int fd;
	int i;
    void *map_base, *virt_addr; 
	unsigned long read_result, write_val;
	off_t target;
	int access_type = 'w';
	char fmt_str[128];
	size_t data_size;
	
	//target = strtoul(argv[1], 0, 0);//字符串转化
	
	char *str;
	
	str = "0xFE9A0000";
	target = strtoul(str, 0, 0);;


    if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
    printf("/dev/mem opened.\n"); 
    fflush(stdout);
    
    /* Map one page */
	for( i = 0;i < 7500;i++)
	{
		map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
		if(map_base == (void *) -1) FATAL;
		

		virt_addr = map_base + (target & MAP_MASK);
		data_size = sizeof(unsigned int);
		virt_addr = fixup_addr(virt_addr, data_size);
		read_result = *((unsigned int *) virt_addr);
		
		//sprintf(fmt_str, "Read at address  0x%%08lX (%%p): 0x%%0%dlX\n",2*data_size);
	       //	printf(fmt_str, (unsigned long)target, virt_addr, read_result);
		printf("address=0x%08X,0x%08X\n",(unsigned long)target,read_result);
		fflush(stdout);


		if(munmap(map_base, MAP_SIZE) == -1) FATAL;
		target+=0x04;
		
	}
    
    //virt_addr = map_base + (target & MAP_MASK);


    close(fd);
    return 0;
}

static inline void *fixup_addr(void *addr, size_t size)
{
#ifdef FORCE_STRICT_ALIGNMENT
	unsigned long aligned_addr = (unsigned long)addr;
	aligned_addr &= ~(size - 1);
	addr = (void *)aligned_addr;
#endif
	return addr;
}