2 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
3 * Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
4 * Copyright (C) 2008, BusyBox Team. -solar 4/26/08
9 int devmem_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
10 int devmem_main(int argc UNUSED_PARAM, char **argv)
12 void *map_base, *virt_addr;
14 uint64_t writeval = writeval; /* for compiler */
16 unsigned page_size = getpagesize();
18 int width = 8 * sizeof(int);
20 /* devmem ADDRESS [WIDTH [VALUE]] */
22 // -r: read and output only the value in hex, with 0x prefix
23 // -w: write only, no reads before or after, and no output
24 // or make this behavior default?
25 // Let's try this and see how users react.
31 target = bb_strtoull(argv[1], NULL, 0); /* allows hex, oct etc */
35 if (isdigit(argv[2][0]) || argv[2][1])
36 width = xatou(argv[2]);
38 static const char bhwl[] ALIGN1 = "bhwl";
39 static const uint8_t sizes[] ALIGN1 = {
46 width = strchrnul(bhwl, (argv[2][0] | 0x20)) - bhwl;
51 writeval = bb_strtoull(argv[3], NULL, 0);
52 } else { /* argv[2] == NULL */
53 /* make argv[3] to be a valid thing to use */
57 bb_show_usage(); /* bb_strtouXX failed */
59 fd = xopen("/dev/mem", argv[3] ? (O_RDWR | O_SYNC) : (O_RDONLY | O_SYNC));
61 page_size * 2 /* in case value spans page */,
62 argv[3] ? (PROT_READ | PROT_WRITE) : PROT_READ,
65 target & ~(off_t)(page_size - 1));
66 if (map_base == MAP_FAILED)
67 bb_perror_msg_and_die("mmap");
69 // printf("Memory mapped at address %p.\n", map_base);
71 virt_addr = (char*)map_base + (target & (page_size - 1));
76 read_result = *(volatile uint8_t*)virt_addr;
79 read_result = *(volatile uint16_t*)virt_addr;
82 read_result = *(volatile uint32_t*)virt_addr;
85 read_result = *(volatile uint64_t*)virt_addr;
88 bb_error_msg_and_die("bad width");
90 // printf("Value at address 0x%"OFF_FMT"X (%p): 0x%llX\n",
92 // (unsigned long long)read_result);
93 /* Zero-padded output shows the width of access just done */
94 printf("0x%0*llX\n", (width >> 2), (unsigned long long)read_result);
98 *(volatile uint8_t*)virt_addr = writeval;
99 // read_result = *(volatile uint8_t*)virt_addr;
102 *(volatile uint16_t*)virt_addr = writeval;
103 // read_result = *(volatile uint16_t*)virt_addr;
106 *(volatile uint32_t*)virt_addr = writeval;
107 // read_result = *(volatile uint32_t*)virt_addr;
110 *(volatile uint64_t*)virt_addr = writeval;
111 // read_result = *(volatile uint64_t*)virt_addr;
114 bb_error_msg_and_die("bad width");
116 // printf("Written 0x%llX; readback 0x%llX\n",
117 // (unsigned long long)writeval,
118 // (unsigned long long)read_result);
121 if (ENABLE_FEATURE_CLEAN_UP) {
122 if (munmap(map_base, page_size * 2) == -1)
123 bb_perror_msg_and_die("munmap");