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