diff --git a/tests/devmem.test b/tests/devmem.test new file mode 100755 index 00000000..6a9c327a --- /dev/null +++ b/tests/devmem.test @@ -0,0 +1,22 @@ +#!/bin/bash + +#testing "name" "command" "result" "infile" "stdin" + +echo "xxxxxxxxhello, world!" > foo +testcmd 'read default (4)' '-f foo 0x8' '0x6c6c6568\n' '' '' +testcmd 'read 1' '-f foo 0x8 1' '0x68\n' '' '' +testcmd 'read 2' '-f foo 0x8 2' '0x6568\n' '' '' +testcmd 'read 4' '-f foo 0x8 4' '0x6c6c6568\n' '' '' +testcmd 'read 8' '-f foo 0x8 8' '0x77202c6f6c6c6568\n' '' '' + +head -c 32 /dev/zero > foo +testcmd 'write 1' '-f foo 0x8 1 0x12 && od -t x foo' '0000000 00000000 00000000 00000012 00000000\n0000020 00000000 00000000 00000000 00000000\n0000040\n' '' '' +testcmd 'write 2' '-f foo 0x8 2 0x1234 && od -t x foo' '0000000 00000000 00000000 00001234 00000000\n0000020 00000000 00000000 00000000 00000000\n0000040\n' '' '' +testcmd 'write 4' '-f foo 0x8 4 0x12345678 && od -t x foo' '0000000 00000000 00000000 12345678 00000000\n0000020 00000000 00000000 00000000 00000000\n0000040\n' '' '' +testcmd 'write 8' '-f foo 0x8 8 0x12345678abcdef01 && od -t x foo' '0000000 00000000 00000000 abcdef01 12345678\n0000020 00000000 00000000 00000000 00000000\n0000040\n' '' '' + +head -c 32 /dev/zero > foo +testcmd 'write 1 multiple' '-f foo 0x8 1 0x12 0x34 && od -t x foo' '0000000 00000000 00000000 00003412 00000000\n0000020 00000000 00000000 00000000 00000000\n0000040\n' '' '' +testcmd 'write 2 multiple' '-f foo 0x8 2 0x1234 0x5678 && od -t x foo' '0000000 00000000 00000000 56781234 00000000\n0000020 00000000 00000000 00000000 00000000\n0000040\n' '' '' +testcmd 'write 4 multiple' '-f foo 0x8 4 0x12345678 0xabcdef01 && od -t x foo' '0000000 00000000 00000000 12345678 abcdef01\n0000020 00000000 00000000 00000000 00000000\n0000040\n' '' '' +testcmd 'write 8 multiple' '-f foo 0x8 8 0x12345678abcdef01 0x1122334455667788 && od -t x foo' '0000000 00000000 00000000 abcdef01 12345678\n0000020 55667788 11223344 00000000 00000000\n0000040\n' '' '' diff --git a/toys/other/devmem.c b/toys/other/devmem.c index 43ddacd2..7fe427f0 100644 --- a/toys/other/devmem.c +++ b/toys/other/devmem.c @@ -2,21 +2,27 @@ * * Copyright 2019 The Android Open Source Project -USE_DEVMEM(NEWTOY(devmem, "<1>3", TOYFLAG_USR|TOYFLAG_SBIN)) +USE_DEVMEM(NEWTOY(devmem, "<1f:", TOYFLAG_USR|TOYFLAG_SBIN)) config DEVMEM bool "devmem" default y help - usage: devmem ADDR [WIDTH [DATA]] + usage: devmem -f FILE ADDR [WIDTH [DATA...]] - Read/write physical address. WIDTH is 1, 2, 4, or 8 bytes (default 4). + Read/write physical addresses. WIDTH is 1, 2, 4, or 8 bytes (default 4). Prefix ADDR with 0x for hexadecimal, output is in same base as address. + + -f FILE File to operate on (default /dev/mem) */ #define FOR_devmem #include "toys.h" +GLOBALS( + char *f; +) + unsigned long xatolu(char *str, int bytes) { char *end = str; @@ -34,7 +40,7 @@ unsigned long xatolu(char *str, int bytes) void devmem_main(void) { - int writing = toys.optc == 3, page_size = sysconf(_SC_PAGESIZE), bytes = 4,fd; + int writing = toys.optc > 2, page_size = sysconf(_SC_PAGESIZE), bytes = 4, fd; unsigned long data = 0, map_off, map_len, addr = xatolu(*toys.optargs, sizeof(long)); char *sizes = sizeof(long)==8 ? "1248" : "124"; @@ -49,12 +55,9 @@ void devmem_main(void) bytes = 1<