Date:2012-11-06 13:08:29 (6 years 10 months ago)
Author:Werner Almesberger
Commit:355614bb346c58cb5e5359bac0a1411edb037c7e
Message:poke/: move over from svn.openmoko.org/developers/werner/poke/

Thought I had done that already a while ago ...
Files: poke/Makefile (1 diff)
poke/poke.c (1 diff)

Change Details

poke/Makefile
1# CC=arm-angstrom-linux-gnueabi-gcc
2# CC=mipsel-openwrt-linux-uclibc-gcc
3
4CFLAGS=-Wall -g
5
6.PHONY: all clean spotless
7
8all: poke
9
10clean:
11        rm -f poke
12
13spotless: clean
poke/poke.c
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
26static volatile void *mem;
27
28
29/* ----- Command-line parsing ---------------------------------------------- */
30
31
32static 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
39int 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}

Archive Download the corresponding diff file

Branches:
master



interactive