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