summaryrefslogtreecommitdiffstats
path: root/setmem.c
blob: 261ba98ebf072782708137ad547b93d6d700690a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 2005 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
 *   Boston MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * setmem.c
 *
 * Utility to write to I/O memory
 */

#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <inttypes.h>
#include <sysexits.h>
#include <sys/types.h>
#include <sys/mman.h>

#include "version.h"

struct option long_options[] = {
  { "help",        0, 0, 'h' },
  { "version",     0, 0, 'V' },
  { "quad",        0, 0, 'q' },
  { "long",        0, 0, 'l' },
  { "dword",       0, 0, 'd' },
  { "short",       0, 0, 's' },
  { "word",        0, 0, 'w' },
  { "byte",        0, 0, 'b' },
  { 0, 0, 0, 0 }
};

const char *program;

void usage(void)
{
  fprintf(stderr, "Usage: %s [options] address value...\n"
	  "  --help         -h  Print this help\n"
	  "  --version      -V  Print current version\n"
	  "  --quad         -q  Write quadword (qword)\n"
	  "  --long         -l  Write longword (dword)\n"
	  "  --dword        -d  Write longword (dword)\n"
	  "  --short        -s  Write shortword (word)\n"
	  "  --word         -w  Write shortword (word)\n"
	  "  --byte         -b  Write byte (default)\n"
	  , program);
}

int main(int argc, char *argv[])
{
  uintptr_t reg, page_mask, start, len;
  uint64_t data;
  int fd;
  int c;
  int size = 1;
  void *map, *ptr;

  program = argv[0];

  while ( (c = getopt_long(argc,argv,"hVqldswb",long_options,NULL)) != - 1 ) {
    switch( c ) {
    case 'h':
      usage();
      exit(0);
    case 'V':
      fprintf(stderr, "%s: version %s\n", program, VERSION_STRING);
      exit(0);
    case 'q':
      size = 8;
      break;
    case 'l':
    case 'd':
      size = 4;
      break;
    case 'w':
    case 's':
      size = 2;
      break;
    case 'b':
      size = 1;
      break;
    default:
      usage();
      exit(127);
    }
  }

  if ( argc-optind < 1 ) {
    /* Should have at least two arguments */
    usage();
    exit(127);
  }

  reg = (uintptr_t)strtoumax(argv[optind++], NULL, 0);

  fd = open("/dev/mem", O_RDWR);
  if ( fd < 0 ) {
    perror("setmem:/dev/mem");
    exit(EX_OSFILE);
  }

  page_mask = ~((uintptr_t)getpagesize()-1);
  start = reg & page_mask;
  len = ((reg + size*(argc-optind) + ~page_mask) & page_mask) - start;

  map = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_SHARED, fd, start);
  if ( map == MAP_FAILED ) {
    perror("setmem:mmap");
    exit(EX_OSERR);
  }

  ptr = (char *)map + (reg & ~page_mask);
  
  while ( optind < argc ) {
    data = (uint64_t)strtoumax(argv[optind++], NULL, 0);

    switch ( size ) {
    case 1:
      *(volatile uint8_t *)ptr = data;
      break;
    case 2:
      *(volatile uint16_t *)ptr = data;
      break;
    case 4:
      *(volatile uint32_t *)ptr = data;
      break;
    case 8:
      *(volatile uint64_t *)ptr = data;
      break;
    default:
      abort();			/* Should never happen */
    }

    ptr = (char *)ptr + size;
  }

  close(fd);

  exit(0);
}