Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 1 | /* |
Denys Vlasenko | ebe6d9d | 2017-10-05 14:40:24 +0200 | [diff] [blame] | 2 | * Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl) |
| 3 | * Copyright (C) 2008, BusyBox Team. -solar 4/26/08 |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 4 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 5 | */ |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 6 | //config:config DEVMEM |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 7 | //config: bool "devmem (2.5 kb)" |
Denys Vlasenko | fb4da16 | 2016-11-22 23:14:24 +0100 | [diff] [blame] | 8 | //config: default y |
| 9 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 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; |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 32 | off_t target; |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 33 | unsigned page_size, mapped_size, offset_in_page; |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 34 | int fd; |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 35 | unsigned width = 8 * sizeof(int); |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 36 | |
| 37 | /* devmem ADDRESS [WIDTH [VALUE]] */ |
Denis Vlasenko | 0432465 | 2008-10-26 17:26:55 +0000 | [diff] [blame] | 38 | // TODO: options? |
| 39 | // -r: read and output only the value in hex, with 0x prefix |
| 40 | // -w: write only, no reads before or after, and no output |
| 41 | // or make this behavior default? |
| 42 | // Let's try this and see how users react. |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 43 | |
| 44 | /* ADDRESS */ |
| 45 | if (!argv[1]) |
| 46 | bb_show_usage(); |
| 47 | errno = 0; |
| 48 | target = bb_strtoull(argv[1], NULL, 0); /* allows hex, oct etc */ |
| 49 | |
| 50 | /* WIDTH */ |
| 51 | if (argv[2]) { |
| 52 | if (isdigit(argv[2][0]) || argv[2][1]) |
| 53 | width = xatou(argv[2]); |
| 54 | else { |
| 55 | static const char bhwl[] ALIGN1 = "bhwl"; |
| 56 | static const uint8_t sizes[] ALIGN1 = { |
| 57 | 8 * sizeof(char), |
| 58 | 8 * sizeof(short), |
| 59 | 8 * sizeof(int), |
| 60 | 8 * sizeof(long), |
| 61 | 0 /* bad */ |
| 62 | }; |
| 63 | width = strchrnul(bhwl, (argv[2][0] | 0x20)) - bhwl; |
| 64 | width = sizes[width]; |
| 65 | } |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 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 | c7b858f | 2020-12-14 18:49:23 +0100 | [diff] [blame] | 74 | mapped_size = page_size = bb_getpagesize(); |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 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) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 88 | bb_simple_perror_msg_and_die("mmap"); |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 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]) { |
Aaro Koskinen | d432049 | 2022-08-25 18:47:02 +0300 | [diff] [blame] | 95 | #ifdef __SIZEOF_INT128__ |
| 96 | if (width == 128) { |
| 97 | unsigned __int128 rd = |
| 98 | *(volatile unsigned __int128 *)virt_addr; |
| 99 | printf("0x%016llX%016llX\n", |
| 100 | (unsigned long long)(uint64_t)(rd >> 64), |
| 101 | (unsigned long long)(uint64_t)rd |
| 102 | ); |
| 103 | } else |
| 104 | #endif |
| 105 | { |
| 106 | switch (width) { |
| 107 | case 8: |
| 108 | read_result = *(volatile uint8_t*)virt_addr; |
| 109 | break; |
| 110 | case 16: |
| 111 | read_result = *(volatile uint16_t*)virt_addr; |
| 112 | break; |
| 113 | case 32: |
| 114 | read_result = *(volatile uint32_t*)virt_addr; |
| 115 | break; |
| 116 | case 64: |
| 117 | read_result = *(volatile uint64_t*)virt_addr; |
| 118 | break; |
| 119 | default: |
| 120 | bb_simple_error_msg_and_die("bad width"); |
| 121 | } |
| 122 | // printf("Value at address 0x%"OFF_FMT"X (%p): 0x%llX\n", |
| 123 | // target, virt_addr, |
| 124 | // (unsigned long long)read_result); |
| 125 | /* Zero-padded output shows the width of access just done */ |
| 126 | printf("0x%0*llX\n", (width >> 2), (unsigned long long)read_result); |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 127 | } |
Denis Vlasenko | 0432465 | 2008-10-26 17:26:55 +0000 | [diff] [blame] | 128 | } else { |
Aaro Koskinen | d432049 | 2022-08-25 18:47:02 +0300 | [diff] [blame] | 129 | /* parse VALUE */ |
| 130 | #ifdef __SIZEOF_INT128__ |
| 131 | unsigned __int128 writeval = strtoumax(argv[3], NULL, 0); |
| 132 | #else |
| 133 | uint64_t writeval = bb_strtoull(argv[3], NULL, 0); |
| 134 | #endif |
Denis Vlasenko | 0432465 | 2008-10-26 17:26:55 +0000 | [diff] [blame] | 135 | switch (width) { |
| 136 | case 8: |
| 137 | *(volatile uint8_t*)virt_addr = writeval; |
| 138 | // read_result = *(volatile uint8_t*)virt_addr; |
| 139 | break; |
| 140 | case 16: |
| 141 | *(volatile uint16_t*)virt_addr = writeval; |
| 142 | // read_result = *(volatile uint16_t*)virt_addr; |
| 143 | break; |
| 144 | case 32: |
| 145 | *(volatile uint32_t*)virt_addr = writeval; |
| 146 | // read_result = *(volatile uint32_t*)virt_addr; |
| 147 | break; |
| 148 | case 64: |
| 149 | *(volatile uint64_t*)virt_addr = writeval; |
| 150 | // read_result = *(volatile uint64_t*)virt_addr; |
| 151 | break; |
Aaro Koskinen | d432049 | 2022-08-25 18:47:02 +0300 | [diff] [blame] | 152 | #ifdef __SIZEOF_INT128__ |
| 153 | case 128: |
| 154 | *(volatile unsigned __int128 *)virt_addr = writeval; |
| 155 | // read_result = *(volatile uint64_t*)virt_addr; |
| 156 | break; |
| 157 | #endif |
Denis Vlasenko | 0432465 | 2008-10-26 17:26:55 +0000 | [diff] [blame] | 158 | default: |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 159 | bb_simple_error_msg_and_die("bad width"); |
Denis Vlasenko | 0432465 | 2008-10-26 17:26:55 +0000 | [diff] [blame] | 160 | } |
| 161 | // printf("Written 0x%llX; readback 0x%llX\n", |
| 162 | // (unsigned long long)writeval, |
| 163 | // (unsigned long long)read_result); |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | if (ENABLE_FEATURE_CLEAN_UP) { |
Denys Vlasenko | 351ef71 | 2010-04-14 13:37:25 -0700 | [diff] [blame] | 167 | if (munmap(map_base, mapped_size) == -1) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 168 | bb_simple_perror_msg_and_die("munmap"); |
Denis Vlasenko | c94fa56 | 2008-10-26 11:08:14 +0000 | [diff] [blame] | 169 | close(fd); |
| 170 | } |
| 171 | |
| 172 | return EXIT_SUCCESS; |
| 173 | } |