root/lm-sensors/trunk/prog/dump/isaset.c @ 5709

Revision 5709, 6.1 KB (checked in by khali, 4 years ago)

Use geteuid instead of getuid so that setuid bit works.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    isaset.c - isaset, a user-space program to write ISA registers
3    Copyright (C) 2000  Frodo Looijaard <frodol@dds.nl>, and
4                        Mark D. Studebaker <mdsxyz123@yahoo.com>
5    Copyright (C) 2004,2007  Jean Delvare <khali@linux-fr.org>
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20    MA 02110-1301 USA.
21*/
22
23/*
24        Typical usage:
25        isaset 0x295 0x296 0x10 0x12    Write 0x12 to address 0x10 using address/data registers
26        isaset -f 0x5010 0x12           Write 0x12 to location 0x5010
27*/
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <string.h>
33#include "util.h"
34
35
36/* To keep glibc2 happy */
37#if defined(__GLIBC__) && __GLIBC__ == 2 && __GLIBC_MINOR__ >= 0
38#include <sys/io.h>
39#else
40#include <asm/io.h>
41#endif
42
43#ifdef __powerpc__
44unsigned long isa_io_base = 0; /* XXX for now */
45#endif /* __powerpc__ */
46
47static void help(void)
48{
49        fprintf(stderr,
50                "Syntax for I2C-like access:\n"
51                "  isaset [-y] ADDRREG DATAREG ADDRESS VALUE [MASK]\n"
52                "Syntax for flat address space:\n"
53                "  isaset [-y] -f ADDRESS VALUE [MASK]\n");
54}
55
56int main(int argc, char *argv[])
57{
58        int addrreg, datareg = 0, value, addr = 0, vmask = 0;
59        unsigned char res;
60        int flags = 0;
61        int flat = 0, yes = 0;
62        char *end;
63
64        /* handle (optional) flags first */
65        while (1+flags < argc && argv[1+flags][0] == '-') {
66                switch (argv[1+flags][1]) {
67                case 'f': flat = 1; break;
68                case 'y': yes = 1; break;
69                default:
70                        fprintf(stderr, "Warning: Unsupported flag "
71                                "\"-%c\"!\n", argv[1+flags][1]);
72                        help();
73                        exit(1);
74                }
75                flags++;
76        }
77
78        /* verify that the argument count is correct */
79        if ((!flat && (argc < 1+flags+4 || argc > 1+flags+5))
80         || (flat && (argc < 1+flags+2 || argc > 1+flags+3))) {
81                help();
82                exit(1);
83        }
84
85        addrreg = strtol(argv[1+flags], &end, 0);
86        if (*end) {
87                fprintf(stderr, "Error: Invalid address!\n");
88                help();
89                exit(1);
90        }
91        if (addrreg < 0 || addrreg > (flat?0xffff:0x3fff)) {
92                fprintf(stderr,
93                        "Error: Address out of range (0x0000-0x%04x)!\n",
94                        flat?0xffff:0x3fff);
95                help();
96                exit(1);
97        }
98
99        if (!flat) {
100                datareg = strtol(argv[1+flags+1], &end, 0);
101                if (*end) {
102                        fprintf(stderr, "Error: Invalid data register!\n");
103                        help();
104                        exit(1);
105                }
106                if (datareg < 0 || datareg > 0x3fff) {
107                        fprintf(stderr, "Error: Data register out of range "
108                                "(0x0000-0x3fff)!\n");
109                        help();
110                        exit(1);
111                }
112
113                addr = strtol(argv[1+flags+2], &end, 0);
114                if (*end) {
115                        fprintf(stderr, "Error: Invalid address!\n");
116                        help();
117                        exit(1);
118                }
119                if (addr < 0 || addr > 0xff) {
120                        fprintf(stderr, "Error: Address out of range "
121                                "(0x00-0xff)!\n");
122                        help();
123                        exit(1);
124                }
125        }
126
127        /* rest is the same for both modes so we cheat on flags */
128        if (!flat)
129                flags += 2;
130
131        value = strtol(argv[flags+2], &end, 0);
132        if (*end) {
133                fprintf(stderr, "Error: Invalid value!\n");
134                help();
135                exit(1);
136        }
137        if (value < 0 || value > 0xff) {
138                fprintf(stderr, "Error: Value out of range "
139                        "(0x00-0xff)!\n");
140                help();
141                exit(1);
142        }
143
144        if (flags+3 < argc) {
145                vmask = strtol(argv[flags+3], &end, 0);
146                if (*end) {
147                        fprintf(stderr, "Error: Invalid mask!\n");
148                        help();
149                        exit(1);
150                }
151                if (vmask < 0 || vmask > 0xff) {
152                        fprintf(stderr, "Error: Mask out of range "
153                                "(0x00-0xff)!\n");
154                        help();
155                        exit(1);
156                }
157        }
158
159        if (geteuid()) {
160                fprintf(stderr, "Error: Can only be run as root "
161                        "(or make it suid root)\n");
162                exit(1);
163        }
164
165        if (!yes) {
166                fprintf(stderr, "WARNING! Running this program can cause "
167                        "system crashes, data loss and worse!\n");
168
169                if (flat)
170                        fprintf(stderr, "I will write value 0x%02x%s to address "
171                                "0x%x.\n", value, vmask ? " (masked)" : "",
172                                addrreg);
173                else
174                        fprintf(stderr, "I will write value 0x%02x%s to address "
175                                "0x%02x of chip with address register 0x%x\n"
176                                "and data register 0x%x.\n",
177                                value, vmask ? " (masked)" : "", addr,
178                                addrreg, datareg);
179
180                fprintf(stderr, "Continue? [Y/n] ");
181                fflush(stderr);
182                if (!user_ack(1)) {
183                        fprintf(stderr, "Aborting on user request.\n");
184                        exit(0);
185                }
186        }
187
188#ifndef __powerpc__
189        if (!flat && datareg < 0x400 && addrreg < 0x400) {
190                if (ioperm(datareg, 1, 1)) {
191                        fprintf(stderr, "Error: Could not ioperm() data "
192                                "register!\n");
193                        exit(1);
194                }
195                if (ioperm(addrreg, 1, 1)) {
196                        fprintf(stderr, "Error: Could not ioperm() address "
197                                "register!\n");
198                        exit(1);
199                }
200        } else {
201                if (iopl(3)) {
202                        fprintf(stderr, "Error: Could not do iopl(3)!\n");
203                        exit(1);
204                }
205        }
206#endif
207
208        if (vmask) {
209                int oldvalue;
210
211                if (flat) {
212                        oldvalue = inb(addrreg);
213                } else {       
214                        outb(addr, addrreg);
215                        oldvalue = inb(datareg);
216                }
217
218                if (oldvalue < 0) {
219                        fprintf(stderr, "Error: Failed to read old value\n");
220                        exit(1);
221                }
222
223                value = (value & vmask) | (oldvalue & ~vmask);
224
225                if (!yes) {
226                        fprintf(stderr, "Old value 0x%02x, write mask "
227                                "0x%02x: Will write 0x%02x to %s "
228                                "0x%02x\n", oldvalue, vmask, value,
229                                flat ? "address" : "register",
230                                flat ? addrreg : addr);
231
232                        fprintf(stderr, "Continue? [Y/n] ");
233                        fflush(stderr);
234                        if (!user_ack(1)) {
235                                fprintf(stderr, "Aborting on user request.\n");
236                                exit(0);
237                        }
238                }
239        }
240
241        /* do the real thing */
242        if (flat) {
243                /* write */
244                outb(value, addrreg);
245                /* readback */
246                res = inb(addrreg);
247        } else {       
248                /* write */
249                outb(addr, addrreg);
250                outb(value, datareg);
251                /* readback */
252                res = inb(datareg);
253        }
254
255        if (res != value) {
256                fprintf(stderr, "Data mismatch, wrote 0x%02x, "
257                        "read 0x%02x back.\n", value, res);
258        }
259
260        exit(0);
261}
Note: See TracBrowser for help on using the browser.