123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- #include "libbb.h"
- int devmem_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int devmem_main(int argc UNUSED_PARAM, char **argv)
- {
- void *map_base, *virt_addr;
- uint64_t read_result;
- off_t target;
- unsigned page_size, mapped_size, offset_in_page;
- int fd;
- unsigned width = 8 * sizeof(int);
-
-
- if (!argv[1])
- bb_show_usage();
- errno = 0;
- target = bb_strtoull(argv[1], NULL, 0);
-
- if (argv[2]) {
- if (isdigit(argv[2][0]) || argv[2][1])
- width = xatou(argv[2]);
- else {
- static const char bhwl[] ALIGN1 = "bhwl";
- static const uint8_t sizes[] ALIGN1 = {
- 8 * sizeof(char),
- 8 * sizeof(short),
- 8 * sizeof(int),
- 8 * sizeof(long),
- 0
- };
- width = strchrnul(bhwl, (argv[2][0] | 0x20)) - bhwl;
- width = sizes[width];
- }
- } else {
-
- argv--;
- }
- if (errno)
- bb_show_usage();
- fd = xopen("/dev/mem", argv[3] ? (O_RDWR | O_SYNC) : (O_RDONLY | O_SYNC));
- mapped_size = page_size = bb_getpagesize();
- offset_in_page = (unsigned)target & (page_size - 1);
- if (offset_in_page + width > page_size) {
-
- mapped_size *= 2;
- }
- map_base = mmap(NULL,
- mapped_size,
- argv[3] ? (PROT_READ | PROT_WRITE) : PROT_READ,
- MAP_SHARED,
- fd,
- target & ~(off_t)(page_size - 1));
- if (map_base == MAP_FAILED)
- bb_simple_perror_msg_and_die("mmap");
- virt_addr = (char*)map_base + offset_in_page;
- if (!argv[3]) {
- #ifdef __SIZEOF_INT128__
- if (width == 128) {
- unsigned __int128 rd =
- *(volatile unsigned __int128 *)virt_addr;
- printf("0x%016llX%016llX\n",
- (unsigned long long)(uint64_t)(rd >> 64),
- (unsigned long long)(uint64_t)rd
- );
- } else
- #endif
- {
- switch (width) {
- case 8:
- read_result = *(volatile uint8_t*)virt_addr;
- break;
- case 16:
- read_result = *(volatile uint16_t*)virt_addr;
- break;
- case 32:
- read_result = *(volatile uint32_t*)virt_addr;
- break;
- case 64:
- read_result = *(volatile uint64_t*)virt_addr;
- break;
- default:
- bb_simple_error_msg_and_die("bad width");
- }
-
- printf("0x%0*llX\n", (width >> 2), (unsigned long long)read_result);
- }
- } else {
-
- #ifdef __SIZEOF_INT128__
- unsigned __int128 writeval = strtoumax(argv[3], NULL, 0);
- #else
- uint64_t writeval = bb_strtoull(argv[3], NULL, 0);
- #endif
- switch (width) {
- case 8:
- *(volatile uint8_t*)virt_addr = writeval;
- break;
- case 16:
- *(volatile uint16_t*)virt_addr = writeval;
- break;
- case 32:
- *(volatile uint32_t*)virt_addr = writeval;
- break;
- case 64:
- *(volatile uint64_t*)virt_addr = writeval;
- break;
- #ifdef __SIZEOF_INT128__
- case 128:
- *(volatile unsigned __int128 *)virt_addr = writeval;
- break;
- #endif
- default:
- bb_simple_error_msg_and_die("bad width");
- }
- }
- if (ENABLE_FEATURE_CLEAN_UP) {
- if (munmap(map_base, mapped_size) == -1)
- bb_simple_perror_msg_and_die("munmap");
- close(fd);
- }
- return EXIT_SUCCESS;
- }
|