From 06137ec72f84bdae8095600882e96330e28bc351 Mon Sep 17 00:00:00 2001 From: Timothy Pearson Date: Wed, 7 Sep 2016 16:40:48 -0500 Subject: Reformat for easier development --- devmem-aspeed.c | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) diff --git a/devmem-aspeed.c b/devmem-aspeed.c index 7732ecb..4831b12 100644 --- a/devmem-aspeed.c +++ b/devmem-aspeed.c @@ -30,7 +30,7 @@ * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA @@ -48,20 +48,20 @@ #include #include #include - + #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \ __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0) - + #define MAP_SIZE 4096UL #define MAP_MASK (MAP_SIZE - 1) int main(int argc, char **argv) { - int fd; - void *map_base, *virt_addr; + int fd; + void *map_base, *virt_addr; unsigned long read_result, writeval; off_t target; int access_type = 'w'; - + if(argc < 2) { fprintf(stderr, "\nUsage:\t%s { address } [ type [ data ] ]\n" "\taddress : memory address to act upon\n" @@ -72,22 +72,22 @@ int main(int argc, char **argv) { } target = strtoul(argv[1], 0, 0); - if(argc > 2) + if(argc > 2) { access_type = tolower(argv[2][0]); + } + + if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL; + printf("/dev/mem opened.\n"); + fflush(stdout); + /* Map one page */ + map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK); + if(map_base == (void *) -1) FATAL; + printf("Memory mapped at address %p.\n", map_base); + fflush(stdout); - if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL; - printf("/dev/mem opened.\n"); - fflush(stdout); - - /* Map one page */ - map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK); - if(map_base == (void *) -1) FATAL; - printf("Memory mapped at address %p.\n", map_base); - fflush(stdout); - - virt_addr = map_base + (target & MAP_MASK); - switch(access_type) { + virt_addr = map_base + (target & MAP_MASK); + switch(access_type) { case 'b': read_result = *((unsigned char *) virt_addr); break; @@ -101,8 +101,8 @@ int main(int argc, char **argv) { fprintf(stderr, "Illegal data type '%c'.\n", access_type); exit(2); } - printf("Value at address 0x%X (%p): 0x%X\n", target, virt_addr, read_result); - fflush(stdout); + printf("Value at address 0x%X (%p): 0x%X\n", target, virt_addr, read_result); + fflush(stdout); if(argc > 3) { writeval = strtoul(argv[3], 0, 0); @@ -123,9 +123,10 @@ int main(int argc, char **argv) { printf("Written 0x%X; readback 0x%X\n", writeval, read_result); fflush(stdout); } - + if(munmap(map_base, MAP_SIZE) == -1) FATAL; - close(fd); - return 0; + + close(fd); + return 0; } -- cgit v1.2.1