Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 1 | /* |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 2 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 3 | * Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl) |
| 4 | * Copyright (C) 2008, BusyBox Team. -solar 4/26/08 |
| 5 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 6 | //config:config DEVMEM |
| 7 | //config: bool "devmem" |
| 8 | //config: default y |
| 9 | //config: help |
| 10 | //config: devmem is a small program that reads and writes from physical |
| 11 | //config: memory using /dev/mem. |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 12 | |
Denys Vlasenko | f88e3bf | 2016-11-22 23:54:17 +0100 | [diff] [blame^] | 13 | //applet:IF_DEVMEM(APPLET(devmem, BB_DIR_SBIN, BB_SUID_DROP)) |
| 14 | |
| 15 | //kbuild:lib-$(CONFIG_DEVMEM) += devmem.o |
| 16 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 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" |
| 24 | |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 25 | #include "libbb.h" |
| 26 | |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 27 | int devmem_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 28 | int devmem_main(int argc UNUSED_PARAM, char **argv) |
| 29 | { |
| 30 | void *map_base, *virt_addr; |
| 31 | uint64_t read_result; |
| 32 | uint64_t writeval = writeval; /* for compiler */ |
| 33 | off_t target; |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 34 | unsigned page_size, mapped_size, offset_in_page; |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 35 | int fd; |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 36 | unsigned width = 8 * sizeof(int); |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 37 | |
| 38 | /* devmem ADDRESS [WIDTH [VALUE]] */ |
Denis Vlasenko | 0432465 | 2008-10-26 17:26:55 +0000 | [diff] [blame] | 39 | // TODO: options? |
| 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. |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 44 | |
| 45 | /* ADDRESS */ |
| 46 | if (!argv[1]) |
| 47 | bb_show_usage(); |
| 48 | errno = 0; |
| 49 | target = bb_strtoull(argv[1], NULL, 0); /* allows hex, oct etc */ |
| 50 | |
| 51 | /* WIDTH */ |
| 52 | if (argv[2]) { |
| 53 | if (isdigit(argv[2][0]) || argv[2][1]) |
| 54 | width = xatou(argv[2]); |
| 55 | else { |
| 56 | static const char bhwl[] ALIGN1 = "bhwl"; |
| 57 | static const uint8_t sizes[] ALIGN1 = { |
| 58 | 8 * sizeof(char), |
| 59 | 8 * sizeof(short), |
| 60 | 8 * sizeof(int), |
| 61 | 8 * sizeof(long), |
| 62 | 0 /* bad */ |
| 63 | }; |
| 64 | width = strchrnul(bhwl, (argv[2][0] | 0x20)) - bhwl; |
| 65 | width = sizes[width]; |
| 66 | } |
| 67 | /* VALUE */ |
| 68 | if (argv[3]) |
| 69 | writeval = bb_strtoull(argv[3], NULL, 0); |
| 70 | } else { /* argv[2] == NULL */ |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 71 | /* make argv[3] to be a valid thing to fetch */ |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 72 | argv--; |
| 73 | } |
| 74 | if (errno) |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 75 | bb_show_usage(); /* one of bb_strtouXX failed */ |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 76 | |
| 77 | fd = xopen("/dev/mem", argv[3] ? (O_RDWR | O_SYNC) : (O_RDONLY | O_SYNC)); |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 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: */ |
| 83 | mapped_size *= 2; |
| 84 | } |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 85 | map_base = mmap(NULL, |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 86 | mapped_size, |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 87 | argv[3] ? (PROT_READ | PROT_WRITE) : PROT_READ, |
| 88 | MAP_SHARED, |
| 89 | fd, |
Denis Vlasenko | 0432465 | 2008-10-26 17:26:55 +0000 | [diff] [blame] | 90 | target & ~(off_t)(page_size - 1)); |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 91 | if (map_base == MAP_FAILED) |
| 92 | bb_perror_msg_and_die("mmap"); |
| 93 | |
| 94 | // printf("Memory mapped at address %p.\n", map_base); |
| 95 | |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 96 | virt_addr = (char*)map_base + offset_in_page; |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 97 | |
Denis Vlasenko | 0432465 | 2008-10-26 17:26:55 +0000 | [diff] [blame] | 98 | if (!argv[3]) { |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 99 | switch (width) { |
| 100 | case 8: |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 101 | read_result = *(volatile uint8_t*)virt_addr; |
| 102 | break; |
| 103 | case 16: |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 104 | read_result = *(volatile uint16_t*)virt_addr; |
| 105 | break; |
| 106 | case 32: |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 107 | read_result = *(volatile uint32_t*)virt_addr; |
| 108 | break; |
| 109 | case 64: |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 110 | read_result = *(volatile uint64_t*)virt_addr; |
| 111 | break; |
Denis Vlasenko | 0432465 | 2008-10-26 17:26:55 +0000 | [diff] [blame] | 112 | default: |
| 113 | bb_error_msg_and_die("bad width"); |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 114 | } |
Denis Vlasenko | 0432465 | 2008-10-26 17:26:55 +0000 | [diff] [blame] | 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); |
| 120 | } else { |
| 121 | switch (width) { |
| 122 | case 8: |
| 123 | *(volatile uint8_t*)virt_addr = writeval; |
| 124 | // read_result = *(volatile uint8_t*)virt_addr; |
| 125 | break; |
| 126 | case 16: |
| 127 | *(volatile uint16_t*)virt_addr = writeval; |
| 128 | // read_result = *(volatile uint16_t*)virt_addr; |
| 129 | break; |
| 130 | case 32: |
| 131 | *(volatile uint32_t*)virt_addr = writeval; |
| 132 | // read_result = *(volatile uint32_t*)virt_addr; |
| 133 | break; |
| 134 | case 64: |
| 135 | *(volatile uint64_t*)virt_addr = writeval; |
| 136 | // read_result = *(volatile uint64_t*)virt_addr; |
| 137 | break; |
| 138 | default: |
| 139 | bb_error_msg_and_die("bad width"); |
| 140 | } |
| 141 | // printf("Written 0x%llX; readback 0x%llX\n", |
| 142 | // (unsigned long long)writeval, |
| 143 | // (unsigned long long)read_result); |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | if (ENABLE_FEATURE_CLEAN_UP) { |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 147 | if (munmap(map_base, mapped_size) == -1) |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 148 | bb_perror_msg_and_die("munmap"); |
| 149 | close(fd); |
| 150 | } |
| 151 | |
| 152 | return EXIT_SUCCESS; |
| 153 | } |