简单的验证写入内存和值与读出来的是否相同
64位编译需要加上gcc -D_LARGEFILE64_SOURCE=1, 否则会有编译warning导致后续运行段错误
-
/* this program write mem with rand numbers,
-
then read to check it, can be used to check resv mem is ok or not*/
-
-
#include <stdio.h>
-
#include <sys/mman.h>
-
#include <errno.h>
-
#include <sys/types.h>
-
#include <sys/stat.h>
-
#include <fcntl.h>
-
#include <unistd.h>
-
#include <sys/ioctl.h>
-
#include <string.h>
-
#include <stdlib.h>
-
#include <time.h>
-
-
int main(int argc,char* argv[])
-
{
-
unsigned char* addrbase;
-
volatile unsigned char reg_val;
-
int memfd = -1;
-
errno = 0;
-
int i = 0;
-
int j = 0;
-
-
if (argc < 5)
-
printf("./test phystart(0x800xx) phy_len(0x100000) 1/0(0-read all and print, 1-write and read and check) 5(default wait 5 to check)\n");
-
-
unsigned long start = strtoul(argv[1], NULL, 16);
-
unsigned long len = strtoul(argv[2], NULL, 16);
-
unsigned long end = start + len;
-
-
int set_flag = atoi(argv[3]);
-
int wait_s = atoi(argv[4]);
-
-
printf("phy start=0x%x, end==0x%x, len=0x%x\n", start, end, len);
-
-
srand( (unsigned)time( NULL ) );
-
unsigned int rand_base = rand();
-
printf("rand() = 0x%x\n", rand_base);
-
-
if ((memfd = open("/dev/mem", O_RDWR )) < 0)
-
{
-
printf("open /dev/mem error\n");
-
return -1;
-
}
-
if (sizeof(unsigned long) == 4){
-
printf("mmap !!!\n");
-
addrbase = (unsigned char*)mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, memfd, start);//0x3a800000 start is absolute phy address
-
} else {
-
printf("mmap64 !!!\n");
-
addrbase = (unsigned char*)mmap64(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, memfd, start);
-
}
-
printf("addrbase=0x%lx\n", addrbase);
-
-
if (set_flag == 0) {
-
for (i = 0; i < len ; i++) {
-
reg_val = *(addrbase+i);
-
printf("[read] i=%d, reg_val=0x%x\n", i, reg_val);
-
}
-
}
-
-
-
-
if (set_flag == 1) {
-
printf("before memset, addrbase=0x%lx\n", addrbase);
-
for (i = 0; i < 10 ; i++) {
-
reg_val = *(addrbase+i);
-
printf("[before] i=%d, reg_val=0x%x\n", i, reg_val);
-
}
-
printf("in memset, addrbase=0x%lx\n", addrbase);
-
for (i = 0; i < len; i++) {
-
if (i%(1024*1024) == 0) {
-
printf(".");
-
fflush ( stdout );
-
}
-
memset((addrbase+i), (rand_base*i)%255, 1);
-
}
-
printf("waiting %d s to scan mem\n", wait_s);
-
sleep(wait_s);
-
printf("to scan...\n");
-
for (i = 0; i < len ; i++) {
-
if (i%(1024*1024) == 0) {
-
printf(".");
-
fflush ( stdout );
-
}
-
reg_val = *(addrbase + i);
-
if (reg_val != (rand_base*i)%255)
-
printf("[*** after check fail ***] i=%d, reg_val=0x%x, (rand_base*i)%255=0x%x\n", i, reg_val, (rand_base*i)%255);
-
}
-
printf("check sucess=0x%lx\n", addrbase);
-
}
-
sleep(1);
-
printf("over=0x%lx\n", addrbase);
-
-
if (munmap((void*)addrbase, len))
-
{
-
printf("munmap fail errno:%d\n", errno);
-
}
-
-
close(memfd);
-
-
printf("return sucess!\n");
-
-
return 0;
-
}
阅读(871) | 评论(0) | 转发(0) |