blob: 6d67909f35210a1f4f205874a8c2a2c5aef00e84 [file] [log] [blame]
Denis 'GNUtoo' Cariklibfe07892016-02-21 14:27:21 +01001/*
2 * Copyright (C) 2016 Denis 'GNUtoo' Carikli <GNUtoo@no-log.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <errno.h>
16#include <limits.h>
17#include <stdarg.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <sys/io.h>
21
22#define POST_DEFAULT_IO_PORT 0x80
23
24void usage(char *progname, const char *error, ...)
25{
26 printf("Usage: %s <VALUE> [PORT]\n", progname);
27 printf("The VALUE argument is an integer between 0x00 and 0xff\n");
28 printf("The PORT argument is an integer between 0x00 and 0xffff\n");
29
30 if (error) {
31 va_list args;
32
33 va_start(args, error);
34 vprintf(error, args);
35 va_end(args);
36 };
37}
38
39void check_int(long val, int min, int max, int err, char *string, char *endptr,
40 char *progname)
41{
42 if (val < min || val > max) {
43 usage(progname,
44 "\nError: The value has to be between 0x%x and 0x%x\n",
45 min, max);
46 exit(EXIT_FAILURE);
47 }
48
49 if (endptr == string || *endptr != '\0') {
50 usage(progname, "\nError: An integer is required\n");
51 exit(EXIT_FAILURE);
52 }
53
54 if ((err) && (!val)) {
55 perror("strtol");
56 exit(EXIT_FAILURE);
57 }
58}
59
60int main(int argc, char *argv[])
61{
62 unsigned long val;
63 unsigned long port = POST_DEFAULT_IO_PORT;
64 char *endptr;
65 int err;
66
67 if (argc != 2 && argc != 3) {
68 usage(argv[0], NULL);
69 exit(EXIT_FAILURE);
70 }
71
72 val = strtol(argv[1], &endptr, 0);
73 err = errno;
74 check_int(val, 0x00, 0xff, err, argv[1], endptr, argv[0]);
75
76 if (argc > 2) {
77 port = strtol(argv[2], &endptr, 0);
78 err = errno;
79 check_int(port, 0x0000, 0xffff, err, argv[2], endptr, argv[0]);
80 }
81
82 err = iopl(3);
83 if (err == -1) {
84 perror("Not root");
85 exit(EXIT_FAILURE);
86 }
87
88 outb(val, port);
89
90 return 0;
91}