Embedded Linux Direct Read and write to memory (DEVMEM)

Source: Internet
Author: User

"Summary" is not a lot of debugging tools used in Linux development. The DEVMEM approach is to provide driver developers with the ability to detect data changes in memory addresses in the application layer, which can be used to verify the correctness of memory or related configurations in the driver.

http://blog.csdn.net/hens007/article/details/7268447

The principle of this tool is also relatively simple, is the application through the MMAP function of the/dev/mem drive in the Mmap method of use, mapped the device's memory to the user space, to achieve the read and write operations of these physical addresses.

#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)intMainintargcChar**ARGV) {intFdvoid*map_base, *virt_addr;unsigned LongRead_result, Writeval; off_t Target;intAccess_type =' W ';if(ARGC <2) {//If the number of parameters is less than two, print how to use this tool        fprintf(stderr,"\nusage:\t%s {address} [type [data]]\n"            "\taddress:memory Address to act upon\n"            "\ttype:access operation type: [B]yte, [H]alfword, [w]ord\n]            "\tdata:data to be written\n\n", argv[0]);Exit(1); } target = Strtoul (argv[1],0,0);if(Argc >2) Access_type =ToLower(argv[2][0]);if(FD = open ("/dev/mem", O_RDWR | O_sync)) = =-1) FATAL;printf("/dev/mem opened.\n"); Fflush (stdout);/ * MAP one page * / //Map kernel space to user spaceMap_base = Mmap (0, Map_size, Prot_read | Prot_write, map_shared, FD, Target & ~map_mask);if(Map_base = = (void*) -1) FATAL;printf("Memory mapped at address%p.\n", map_base);    Fflush (stdout); VIRT_ADDR = Map_base + (target & Map_mask);//Get different types of memory data for different parameters&nbsp;Switch(Access_type) { Case ' B ': Read_result = * ((unsigned Char*) virt_addr); Break; Case ' h ': Read_result = * ((unsigned  Short*) virt_addr); Break; Case ' W ': Read_result = * ((unsigned Long*) virt_addr); Break;default:fprintf(stderr,"Illegal data type '%c '. \ n", Access_type);Exit(2); }printf("Value at address 0x%X (%p): 0x%x\n", Target, VIRT_ADDR, Read_result); Fflush (stdout);//If the parameter is greater than 3, it is a write operation that writes different types of data for different parameters    if(Argc >3) {Writeval = Strtoul (argv[3],0,0);Switch(Access_type) { Case ' B ':                *((unsigned Char*) virt_addr) = Writeval; Read_result = * ((unsigned Char*) virt_addr); Break; Case ' h ':                *((unsigned  Short*) virt_addr) = Writeval; Read_result = * ((unsigned  Short*) virt_addr); Break; Case ' W ':                *((unsigned Long*) virt_addr) = Writeval; Read_result = * ((unsigned Long*) virt_addr); Break; }printf("written 0x%X; Readback 0x%x\n ", Writeval, Read_result);    Fflush (stdout); }if(Munmap (map_base, map_size) = =-1) FATAL; Close (FD);return 0;}
Memdev: Read and write memory directly. Can be found in miscellaneous in BUSYBOX: Config_user_busybox_devmem:devmem isA small program thatReads andWrites fromPhysical memory Using/dev/mem.                                                      Symbol:user_busybox_devmem [=y] Prompt:devmem Defined at.. /user/busybox/busybox-1.14. 3/miscutils/kconfig:216Depends on: User_busybox_busybox Location:                                  -BusyBox (User_busybox_busybox [=y]), miscellaneous Utilities Usage Usage:devmem address [WIDTH [VALUE]] read: in Address0x97000000Read +Bit value (width default equals +, the optional value is [8, -, +, -])/dev# Devmem 0x970000000x11111111READ: In Address0x97000000Read -Bit value/dev# Devmem 0x970000000x1111Write: At address0x97000000Write +Bit value0X7777ABCD/dev# devmem 0x97000000 0X7777ABCD/dev# Devmem 0x970000000X7777ABCDNote: If there is no mem node in/dev, there will be an error:/dev# Devmem 0x97000000Devmem:can ' t open '/dev/mem ': No suchfile orDirectory can then be manually created in the host system (for example, in NFS root filesystem mode): [Email protected]:~/embedded/tftpboot/nfsroot/dev$ sudo mknod mem-m666 C1 1Note that the permissions here are666, allowing anyone to read and write, can be very good with the program debug. /dev# Devmem 0x970000000X7777ABCD

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Embedded Linux Direct Read and write to memory (DEVMEM)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.