Werner's Miscellanea
Sign in or create your account | Project List | Help
Werner's Miscellanea Git Source Tree
Root/
| 1 | /* |
| 2 | * poke.c - Read or write any CPU register |
| 3 | * |
| 4 | * Copyright (C) 2008 by OpenMoko, Inc. |
| 5 | * Written by Werner Almesberger <werner@openmoko.org> |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | */ |
| 13 | |
| 14 | |
| 15 | #include <stdint.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <sys/mman.h> |
| 21 | |
| 22 | |
| 23 | #define PAGE_SIZE 4096 |
| 24 | #define MEM(bits, addr) (*(uint##bits##_t *) (mem+((addr) & (PAGE_SIZE-1)))) |
| 25 | |
| 26 | static volatile void *mem; |
| 27 | |
| 28 | |
| 29 | /* ----- Command-line parsing ---------------------------------------------- */ |
| 30 | |
| 31 | |
| 32 | static void __attribute__((noreturn)) usage(const char *name) |
| 33 | { |
| 34 | fprintf(stderr, "usage: %s [-8|-16|-32] hex_address [value]\n", name); |
| 35 | exit(1); |
| 36 | } |
| 37 | |
| 38 | |
| 39 | int main(int argc, char **argv) |
| 40 | { |
| 41 | int fd; |
| 42 | char *end; |
| 43 | unsigned long addr; |
| 44 | uint32_t val; |
| 45 | int bits = 32; |
| 46 | char **args = argv+1; |
| 47 | |
| 48 | fd = open("/dev/mem", O_RDWR); |
| 49 | if (fd < 0) { |
| 50 | perror("/dev/mem"); |
| 51 | exit(1); |
| 52 | } |
| 53 | if (argc > 1 && *argv[1] == '-') { |
| 54 | if (!strcmp(argv[1], "-8")) |
| 55 | bits = 8; |
| 56 | else if (!strcmp(argv[1], "-16")) |
| 57 | bits = 16; |
| 58 | else if (!strcmp(argv[1], "-32")) |
| 59 | bits = 32; |
| 60 | else |
| 61 | usage(*argv); |
| 62 | args++; |
| 63 | argc--; /* dirty */ |
| 64 | } |
| 65 | switch (argc) { |
| 66 | case 3: |
| 67 | val = strtoul(args[1], &end, 0); |
| 68 | if (*end) |
| 69 | usage(*argv); |
| 70 | /* fall through */ |
| 71 | case 2: |
| 72 | addr = strtoul(args[0], &end, 16); |
| 73 | if (*end) |
| 74 | usage(*argv); |
| 75 | break; |
| 76 | default: |
| 77 | usage(*argv); |
| 78 | } |
| 79 | mem = mmap(NULL, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, |
| 80 | addr & ~(PAGE_SIZE-1)); |
| 81 | if (mem == MAP_FAILED) { |
| 82 | perror("mmap"); |
| 83 | exit(1); |
| 84 | } |
| 85 | if (argc == 2) { |
| 86 | switch (bits) { |
| 87 | case 8: |
| 88 | printf("0x%02lx\n", (unsigned long) MEM(8, addr)); |
| 89 | break; |
| 90 | case 16: |
| 91 | printf("0x%04lx\n", (unsigned long) MEM(16, addr)); |
| 92 | break; |
| 93 | case 32: |
| 94 | printf("0x%08lx\n", (unsigned long) MEM(32, addr)); |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | else { |
| 99 | switch (bits) { |
| 100 | case 8: |
| 101 | MEM(8, addr) = val; |
| 102 | break; |
| 103 | case 16: |
| 104 | MEM(16, addr) = val; |
| 105 | break; |
| 106 | case 32: |
| 107 | MEM(32, addr) = val; |
| 108 | break; |
| 109 | } |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 |
Branches:
master
