summaryrefslogtreecommitdiffstats
path: root/setio.c
blob: c77d0ae7abd1ca349b59dcbb4f550e467a5e2922 (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
#ident "$Id$"
/* ----------------------------------------------------------------------- *
 *   
 *   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 <sys/types.h>
#include <sys/io.h>

#include "version.h"

struct option long_options[] = {
  { "help",        0, 0, 'h' },
  { "version",     0, 0, 'V' },
  { "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"
	  "  --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;
  uint64_t data;
  int c;
  int size = 1;

  program = argv[0];

  while ( (c = getopt_long(argc,argv,"hVldswb",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 'l':
    case 'd':
      size = 4;
      break;
    case 'w':
    case 's':
      size = 2;
      break;
    case 'b':
      size = 1;
      break;
    default:
      usage();
      exit(127);
    }
  }

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

  iopl(3);

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

  while ( optind < argc ) {
    data = (uint64_t)strtoumax(argv[optind++], NULL, 0);

    switch ( size ) {
    case 1:
      outb(data, reg);
      break;
    case 2:
      outw(data, reg);
      break;
    case 4:
      outl(data, reg);
      break;
    default:
      abort();			/* Should never happen */
    }
  }

  exit(0);
}