busybox/miscutils/devmem.c

154 lines
4.2 KiB
C
Raw Normal View History

2008-10-26 16:38:14 +05:30
/*
* Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
* Copyright (C) 2008, BusyBox Team. -solar 4/26/08
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
2008-10-26 16:38:14 +05:30
*/
//config:config DEVMEM
//config: bool "devmem (2.5 kb)"
//config: default y
//config: help
//config: devmem is a small program that reads and writes from physical
//config: memory using /dev/mem.
2008-10-26 16:38:14 +05:30
//applet:IF_DEVMEM(APPLET(devmem, BB_DIR_SBIN, BB_SUID_DROP))
//kbuild:lib-$(CONFIG_DEVMEM) += devmem.o
//usage:#define devmem_trivial_usage
//usage: "ADDRESS [WIDTH [VALUE]]"
//usage:#define devmem_full_usage "\n\n"
//usage: "Read/write from physical address\n"
//usage: "\n ADDRESS Address to act upon"
//usage: "\n WIDTH Width (8/16/...)"
//usage: "\n VALUE Data to be written"
2008-10-26 16:38:14 +05:30
#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;
uint64_t writeval = writeval; /* for compiler */
off_t target;
unsigned page_size, mapped_size, offset_in_page;
2008-10-26 16:38:14 +05:30
int fd;
unsigned width = 8 * sizeof(int);
2008-10-26 16:38:14 +05:30
/* devmem ADDRESS [WIDTH [VALUE]] */
// TODO: options?
// -r: read and output only the value in hex, with 0x prefix
// -w: write only, no reads before or after, and no output
// or make this behavior default?
// Let's try this and see how users react.
2008-10-26 16:38:14 +05:30
/* ADDRESS */
if (!argv[1])
bb_show_usage();
errno = 0;
target = bb_strtoull(argv[1], NULL, 0); /* allows hex, oct etc */
/* WIDTH */
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 /* bad */
};
width = strchrnul(bhwl, (argv[2][0] | 0x20)) - bhwl;
width = sizes[width];
}
/* VALUE */
if (argv[3])
writeval = bb_strtoull(argv[3], NULL, 0);
} else { /* argv[2] == NULL */
/* make argv[3] to be a valid thing to fetch */
2008-10-26 16:38:14 +05:30
argv--;
}
if (errno)
bb_show_usage(); /* one of bb_strtouXX failed */
2008-10-26 16:38:14 +05:30
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) {
/* This access spans pages.
* Must map two pages to make it possible: */
mapped_size *= 2;
}
2008-10-26 16:38:14 +05:30
map_base = mmap(NULL,
mapped_size,
2008-10-26 16:38:14 +05:30
argv[3] ? (PROT_READ | PROT_WRITE) : PROT_READ,
MAP_SHARED,
fd,
target & ~(off_t)(page_size - 1));
2008-10-26 16:38:14 +05:30
if (map_base == MAP_FAILED)
libbb: reduce the overhead of single parameter bb_error_msg() calls Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower overhead call to bb_perror_msg() when only a string was being printed with no parameters. This saves space for some CPU architectures because it avoids the overhead of a call to a variadic function. However there has never been a simple version of bb_error_msg(), and since 2007 many new calls to bb_perror_msg() have been added that only take a single parameter and so could have been using bb_simple_perror_message(). This changeset introduces 'simple' versions of bb_info_msg(), bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and bb_herror_msg_and_die(), and replaces all calls that only take a single parameter, or use something like ("%s", arg), with calls to the corresponding 'simple' version. Since it is likely that single parameter calls to the variadic functions may be accidentally reintroduced in the future a new debugging config option WARN_SIMPLE_MSG has been introduced. This uses some macro magic which will cause any such calls to generate a warning, but this is turned off by default to avoid use of the unpleasant macros in normal circumstances. This is a large changeset due to the number of calls that have been replaced. The only files that contain changes other than simple substitution of function calls are libbb.h, libbb/herror_msg.c, libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c, networking/udhcp/common.h and util-linux/mdev.c additonal macros have been added for logging so that single parameter and multiple parameter logging variants exist. The amount of space saved varies considerably by architecture, and was found to be as follows (for 'defconfig' using GCC 7.4): Arm: -92 bytes MIPS: -52 bytes PPC: -1836 bytes x86_64: -938 bytes Note that for the MIPS architecture only an exception had to be made disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h) because it made these files larger on MIPS. Signed-off-by: James Byrne <james.byrne@origamienergy.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
bb_simple_perror_msg_and_die("mmap");
2008-10-26 16:38:14 +05:30
// printf("Memory mapped at address %p.\n", map_base);
virt_addr = (char*)map_base + offset_in_page;
2008-10-26 16:38:14 +05:30
if (!argv[3]) {
2008-10-26 16:38:14 +05:30
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:
libbb: reduce the overhead of single parameter bb_error_msg() calls Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower overhead call to bb_perror_msg() when only a string was being printed with no parameters. This saves space for some CPU architectures because it avoids the overhead of a call to a variadic function. However there has never been a simple version of bb_error_msg(), and since 2007 many new calls to bb_perror_msg() have been added that only take a single parameter and so could have been using bb_simple_perror_message(). This changeset introduces 'simple' versions of bb_info_msg(), bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and bb_herror_msg_and_die(), and replaces all calls that only take a single parameter, or use something like ("%s", arg), with calls to the corresponding 'simple' version. Since it is likely that single parameter calls to the variadic functions may be accidentally reintroduced in the future a new debugging config option WARN_SIMPLE_MSG has been introduced. This uses some macro magic which will cause any such calls to generate a warning, but this is turned off by default to avoid use of the unpleasant macros in normal circumstances. This is a large changeset due to the number of calls that have been replaced. The only files that contain changes other than simple substitution of function calls are libbb.h, libbb/herror_msg.c, libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c, networking/udhcp/common.h and util-linux/mdev.c additonal macros have been added for logging so that single parameter and multiple parameter logging variants exist. The amount of space saved varies considerably by architecture, and was found to be as follows (for 'defconfig' using GCC 7.4): Arm: -92 bytes MIPS: -52 bytes PPC: -1836 bytes x86_64: -938 bytes Note that for the MIPS architecture only an exception had to be made disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h) because it made these files larger on MIPS. Signed-off-by: James Byrne <james.byrne@origamienergy.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
bb_simple_error_msg_and_die("bad width");
}
// printf("Value at address 0x%"OFF_FMT"X (%p): 0x%llX\n",
// target, virt_addr,
// (unsigned long long)read_result);
/* Zero-padded output shows the width of access just done */
printf("0x%0*llX\n", (width >> 2), (unsigned long long)read_result);
} else {
switch (width) {
case 8:
*(volatile uint8_t*)virt_addr = writeval;
// read_result = *(volatile uint8_t*)virt_addr;
break;
case 16:
*(volatile uint16_t*)virt_addr = writeval;
// read_result = *(volatile uint16_t*)virt_addr;
break;
case 32:
*(volatile uint32_t*)virt_addr = writeval;
// read_result = *(volatile uint32_t*)virt_addr;
break;
case 64:
*(volatile uint64_t*)virt_addr = writeval;
// read_result = *(volatile uint64_t*)virt_addr;
break;
default:
libbb: reduce the overhead of single parameter bb_error_msg() calls Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower overhead call to bb_perror_msg() when only a string was being printed with no parameters. This saves space for some CPU architectures because it avoids the overhead of a call to a variadic function. However there has never been a simple version of bb_error_msg(), and since 2007 many new calls to bb_perror_msg() have been added that only take a single parameter and so could have been using bb_simple_perror_message(). This changeset introduces 'simple' versions of bb_info_msg(), bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and bb_herror_msg_and_die(), and replaces all calls that only take a single parameter, or use something like ("%s", arg), with calls to the corresponding 'simple' version. Since it is likely that single parameter calls to the variadic functions may be accidentally reintroduced in the future a new debugging config option WARN_SIMPLE_MSG has been introduced. This uses some macro magic which will cause any such calls to generate a warning, but this is turned off by default to avoid use of the unpleasant macros in normal circumstances. This is a large changeset due to the number of calls that have been replaced. The only files that contain changes other than simple substitution of function calls are libbb.h, libbb/herror_msg.c, libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c, networking/udhcp/common.h and util-linux/mdev.c additonal macros have been added for logging so that single parameter and multiple parameter logging variants exist. The amount of space saved varies considerably by architecture, and was found to be as follows (for 'defconfig' using GCC 7.4): Arm: -92 bytes MIPS: -52 bytes PPC: -1836 bytes x86_64: -938 bytes Note that for the MIPS architecture only an exception had to be made disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h) because it made these files larger on MIPS. Signed-off-by: James Byrne <james.byrne@origamienergy.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
bb_simple_error_msg_and_die("bad width");
2008-10-26 16:38:14 +05:30
}
// printf("Written 0x%llX; readback 0x%llX\n",
// (unsigned long long)writeval,
// (unsigned long long)read_result);
2008-10-26 16:38:14 +05:30
}
if (ENABLE_FEATURE_CLEAN_UP) {
if (munmap(map_base, mapped_size) == -1)
libbb: reduce the overhead of single parameter bb_error_msg() calls Back in 2007, commit 0c97c9d43707 ("'simple' error message functions by Loic Grenie") introduced bb_simple_perror_msg() to allow for a lower overhead call to bb_perror_msg() when only a string was being printed with no parameters. This saves space for some CPU architectures because it avoids the overhead of a call to a variadic function. However there has never been a simple version of bb_error_msg(), and since 2007 many new calls to bb_perror_msg() have been added that only take a single parameter and so could have been using bb_simple_perror_message(). This changeset introduces 'simple' versions of bb_info_msg(), bb_error_msg(), bb_error_msg_and_die(), bb_herror_msg() and bb_herror_msg_and_die(), and replaces all calls that only take a single parameter, or use something like ("%s", arg), with calls to the corresponding 'simple' version. Since it is likely that single parameter calls to the variadic functions may be accidentally reintroduced in the future a new debugging config option WARN_SIMPLE_MSG has been introduced. This uses some macro magic which will cause any such calls to generate a warning, but this is turned off by default to avoid use of the unpleasant macros in normal circumstances. This is a large changeset due to the number of calls that have been replaced. The only files that contain changes other than simple substitution of function calls are libbb.h, libbb/herror_msg.c, libbb/verror_msg.c and libbb/xfuncs_printf.c. In miscutils/devfsd.c, networking/udhcp/common.h and util-linux/mdev.c additonal macros have been added for logging so that single parameter and multiple parameter logging variants exist. The amount of space saved varies considerably by architecture, and was found to be as follows (for 'defconfig' using GCC 7.4): Arm: -92 bytes MIPS: -52 bytes PPC: -1836 bytes x86_64: -938 bytes Note that for the MIPS architecture only an exception had to be made disabling the 'simple' calls for 'udhcp' (in networking/udhcp/common.h) because it made these files larger on MIPS. Signed-off-by: James Byrne <james.byrne@origamienergy.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2019-07-02 15:05:03 +05:30
bb_simple_perror_msg_and_die("munmap");
2008-10-26 16:38:14 +05:30
close(fd);
}
return EXIT_SUCCESS;
}