/*
- * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
* Copyright (C) 2000, Jan-Derk Bakker (J.D.Bakker@its.tudelft.nl)
* Copyright (C) 2008, BusyBox Team. -solar 4/26/08
*/
+//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"
+
#include "libbb.h"
int devmem_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
uint64_t read_result;
uint64_t writeval = writeval; /* for compiler */
off_t target;
- unsigned page_size = getpagesize();
+ unsigned page_size, mapped_size, offset_in_page;
int fd;
- int width = 8 * sizeof(int);
+ unsigned width = 8 * sizeof(int);
/* devmem ADDRESS [WIDTH [VALUE]] */
// TODO: options?
if (argv[3])
writeval = bb_strtoull(argv[3], NULL, 0);
} else { /* argv[2] == NULL */
- /* make argv[3] to be a valid thing to use */
+ /* make argv[3] to be a valid thing to fetch */
argv--;
}
if (errno)
- bb_show_usage(); /* bb_strtouXX failed */
+ bb_show_usage(); /* one of bb_strtouXX failed */
fd = xopen("/dev/mem", argv[3] ? (O_RDWR | O_SYNC) : (O_RDONLY | O_SYNC));
+ mapped_size = page_size = 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;
+ }
map_base = mmap(NULL,
- page_size * 2 /* in case value spans page */,
+ mapped_size,
argv[3] ? (PROT_READ | PROT_WRITE) : PROT_READ,
MAP_SHARED,
fd,
// printf("Memory mapped at address %p.\n", map_base);
- virt_addr = (char*)map_base + (target & (page_size - 1));
+ virt_addr = (char*)map_base + offset_in_page;
if (!argv[3]) {
switch (width) {
}
if (ENABLE_FEATURE_CLEAN_UP) {
- if (munmap(map_base, page_size * 2) == -1)
+ if (munmap(map_base, mapped_size) == -1)
bb_perror_msg_and_die("munmap");
close(fd);
}