2 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
3 * Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
4 * Copyright (C) 2008, BusyBox Team. -solar 4/26/08
7 //config: bool "devmem"
10 //config: devmem is a small program that reads and writes from physical
11 //config: memory using /dev/mem.
13 //applet:IF_DEVMEM(APPLET(devmem, BB_DIR_SBIN, BB_SUID_DROP))
15 //kbuild:lib-$(CONFIG_DEVMEM) += devmem.o
17 //usage:#define devmem_trivial_usage
18 //usage: "ADDRESS [WIDTH [VALUE]]"
19 //usage:#define devmem_full_usage "\n\n"
20 //usage: "Read/write from physical address\n"
21 //usage: "\n ADDRESS Address to act upon"
22 //usage: "\n WIDTH Width (8/16/...)"
23 //usage: "\n VALUE Data to be written"
27 int devmem_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
28 int devmem_main(int argc UNUSED_PARAM, char **argv)
30 void *map_base, *virt_addr;
32 uint64_t writeval = writeval; /* for compiler */
34 unsigned page_size, mapped_size, offset_in_page;
36 unsigned width = 8 * sizeof(int);
38 /* devmem ADDRESS [WIDTH [VALUE]] */
40 // -r: read and output only the value in hex, with 0x prefix
41 // -w: write only, no reads before or after, and no output
42 // or make this behavior default?
43 // Let's try this and see how users react.
49 target = bb_strtoull(argv[1], NULL, 0); /* allows hex, oct etc */
53 if (isdigit(argv[2][0]) || argv[2][1])
54 width = xatou(argv[2]);
56 static const char bhwl[] ALIGN1 = "bhwl";
57 static const uint8_t sizes[] ALIGN1 = {
64 width = strchrnul(bhwl, (argv[2][0] | 0x20)) - bhwl;
69 writeval = bb_strtoull(argv[3], NULL, 0);
70 } else { /* argv[2] == NULL */
71 /* make argv[3] to be a valid thing to fetch */
75 bb_show_usage(); /* one of bb_strtouXX failed */
77 fd = xopen("/dev/mem", argv[3] ? (O_RDWR | O_SYNC) : (O_RDONLY | O_SYNC));
78 mapped_size = page_size = getpagesize();
79 offset_in_page = (unsigned)target & (page_size - 1);
80 if (offset_in_page + width > page_size) {
81 /* This access spans pages.
82 * Must map two pages to make it possible: */
87 argv[3] ? (PROT_READ | PROT_WRITE) : PROT_READ,
90 target & ~(off_t)(page_size - 1));
91 if (map_base == MAP_FAILED)
92 bb_perror_msg_and_die("mmap");
94 // printf("Memory mapped at address %p.\n", map_base);
96 virt_addr = (char*)map_base + offset_in_page;
101 read_result = *(volatile uint8_t*)virt_addr;
104 read_result = *(volatile uint16_t*)virt_addr;
107 read_result = *(volatile uint32_t*)virt_addr;
110 read_result = *(volatile uint64_t*)virt_addr;
113 bb_error_msg_and_die("bad width");
115 // printf("Value at address 0x%"OFF_FMT"X (%p): 0x%llX\n",
116 // target, virt_addr,
117 // (unsigned long long)read_result);
118 /* Zero-padded output shows the width of access just done */
119 printf("0x%0*llX\n", (width >> 2), (unsigned long long)read_result);
123 *(volatile uint8_t*)virt_addr = writeval;
124 // read_result = *(volatile uint8_t*)virt_addr;
127 *(volatile uint16_t*)virt_addr = writeval;
128 // read_result = *(volatile uint16_t*)virt_addr;
131 *(volatile uint32_t*)virt_addr = writeval;
132 // read_result = *(volatile uint32_t*)virt_addr;
135 *(volatile uint64_t*)virt_addr = writeval;
136 // read_result = *(volatile uint64_t*)virt_addr;
139 bb_error_msg_and_die("bad width");
141 // printf("Written 0x%llX; readback 0x%llX\n",
142 // (unsigned long long)writeval,
143 // (unsigned long long)read_result);
146 if (ENABLE_FEATURE_CLEAN_UP) {
147 if (munmap(map_base, mapped_size) == -1)
148 bb_perror_msg_and_die("munmap");