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 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 13 | //usage:#define devmem_trivial_usage |
| 14 | //usage: "ADDRESS [WIDTH [VALUE]]" |
| 15 | //usage:#define devmem_full_usage "\n\n" |
| 16 | //usage: "Read/write from physical address\n" |
| 17 | //usage: "\n ADDRESS Address to act upon" |
| 18 | //usage: "\n WIDTH Width (8/16/...)" |
| 19 | //usage: "\n VALUE Data to be written" |
| 20 | |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 21 | #include "libbb.h" |
| 22 | |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 23 | int devmem_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 24 | int devmem_main(int argc UNUSED_PARAM, char **argv) |
| 25 | { |
| 26 | void *map_base, *virt_addr; |
| 27 | uint64_t read_result; |
| 28 | uint64_t writeval = writeval; /* for compiler */ |
| 29 | off_t target; |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 30 | unsigned page_size, mapped_size, offset_in_page; |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 31 | int fd; |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 32 | unsigned width = 8 * sizeof(int); |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 33 | |
| 34 | /* devmem ADDRESS [WIDTH [VALUE]] */ |
Denis Vlasenko | 0432465 | 2008-10-26 17:26:55 +0000 | [diff] [blame] | 35 | // TODO: options? |
| 36 | // -r: read and output only the value in hex, with 0x prefix |
| 37 | // -w: write only, no reads before or after, and no output |
| 38 | // or make this behavior default? |
| 39 | // Let's try this and see how users react. |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 40 | |
| 41 | /* ADDRESS */ |
| 42 | if (!argv[1]) |
| 43 | bb_show_usage(); |
| 44 | errno = 0; |
| 45 | target = bb_strtoull(argv[1], NULL, 0); /* allows hex, oct etc */ |
| 46 | |
| 47 | /* WIDTH */ |
| 48 | if (argv[2]) { |
| 49 | if (isdigit(argv[2][0]) || argv[2][1]) |
| 50 | width = xatou(argv[2]); |
| 51 | else { |
| 52 | static const char bhwl[] ALIGN1 = "bhwl"; |
| 53 | static const uint8_t sizes[] ALIGN1 = { |
| 54 | 8 * sizeof(char), |
| 55 | 8 * sizeof(short), |
| 56 | 8 * sizeof(int), |
| 57 | 8 * sizeof(long), |
| 58 | 0 /* bad */ |
| 59 | }; |
| 60 | width = strchrnul(bhwl, (argv[2][0] | 0x20)) - bhwl; |
| 61 | width = sizes[width]; |
| 62 | } |
| 63 | /* VALUE */ |
| 64 | if (argv[3]) |
| 65 | writeval = bb_strtoull(argv[3], NULL, 0); |
| 66 | } else { /* argv[2] == NULL */ |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 67 | /* make argv[3] to be a valid thing to fetch */ |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 68 | argv--; |
| 69 | } |
| 70 | if (errno) |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 71 | bb_show_usage(); /* one of bb_strtouXX failed */ |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 72 | |
| 73 | 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] | 74 | mapped_size = page_size = getpagesize(); |
| 75 | offset_in_page = (unsigned)target & (page_size - 1); |
| 76 | if (offset_in_page + width > page_size) { |
| 77 | /* This access spans pages. |
| 78 | * Must map two pages to make it possible: */ |
| 79 | mapped_size *= 2; |
| 80 | } |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 81 | map_base = mmap(NULL, |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 82 | mapped_size, |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 83 | argv[3] ? (PROT_READ | PROT_WRITE) : PROT_READ, |
| 84 | MAP_SHARED, |
| 85 | fd, |
Denis Vlasenko | 0432465 | 2008-10-26 17:26:55 +0000 | [diff] [blame] | 86 | target & ~(off_t)(page_size - 1)); |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 87 | if (map_base == MAP_FAILED) |
| 88 | bb_perror_msg_and_die("mmap"); |
| 89 | |
| 90 | // printf("Memory mapped at address %p.\n", map_base); |
| 91 | |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 92 | virt_addr = (char*)map_base + offset_in_page; |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 93 | |
Denis Vlasenko | 0432465 | 2008-10-26 17:26:55 +0000 | [diff] [blame] | 94 | if (!argv[3]) { |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 95 | switch (width) { |
| 96 | case 8: |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 97 | read_result = *(volatile uint8_t*)virt_addr; |
| 98 | break; |
| 99 | case 16: |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 100 | read_result = *(volatile uint16_t*)virt_addr; |
| 101 | break; |
| 102 | case 32: |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 103 | read_result = *(volatile uint32_t*)virt_addr; |
| 104 | break; |
| 105 | case 64: |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 106 | read_result = *(volatile uint64_t*)virt_addr; |
| 107 | break; |
Denis Vlasenko | 0432465 | 2008-10-26 17:26:55 +0000 | [diff] [blame] | 108 | default: |
| 109 | bb_error_msg_and_die("bad width"); |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 110 | } |
Denis Vlasenko | 0432465 | 2008-10-26 17:26:55 +0000 | [diff] [blame] | 111 | // printf("Value at address 0x%"OFF_FMT"X (%p): 0x%llX\n", |
| 112 | // target, virt_addr, |
| 113 | // (unsigned long long)read_result); |
| 114 | /* Zero-padded output shows the width of access just done */ |
| 115 | printf("0x%0*llX\n", (width >> 2), (unsigned long long)read_result); |
| 116 | } else { |
| 117 | switch (width) { |
| 118 | case 8: |
| 119 | *(volatile uint8_t*)virt_addr = writeval; |
| 120 | // read_result = *(volatile uint8_t*)virt_addr; |
| 121 | break; |
| 122 | case 16: |
| 123 | *(volatile uint16_t*)virt_addr = writeval; |
| 124 | // read_result = *(volatile uint16_t*)virt_addr; |
| 125 | break; |
| 126 | case 32: |
| 127 | *(volatile uint32_t*)virt_addr = writeval; |
| 128 | // read_result = *(volatile uint32_t*)virt_addr; |
| 129 | break; |
| 130 | case 64: |
| 131 | *(volatile uint64_t*)virt_addr = writeval; |
| 132 | // read_result = *(volatile uint64_t*)virt_addr; |
| 133 | break; |
| 134 | default: |
| 135 | bb_error_msg_and_die("bad width"); |
| 136 | } |
| 137 | // printf("Written 0x%llX; readback 0x%llX\n", |
| 138 | // (unsigned long long)writeval, |
| 139 | // (unsigned long long)read_result); |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | if (ENABLE_FEATURE_CLEAN_UP) { |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 143 | if (munmap(map_base, mapped_size) == -1) |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 144 | bb_perror_msg_and_die("munmap"); |
| 145 | close(fd); |
| 146 | } |
| 147 | |
| 148 | return EXIT_SUCCESS; |
| 149 | } |