google/butterfly: add cpu/gpu pwm backlight register values
[coreboot.git] / util / post / post.c
blob6d67909f35210a1f4f205874a8c2a2c5aef00e84
1 /*
2 * Copyright (C) 2016 Denis 'GNUtoo' Carikli <GNUtoo@no-log.org>
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.
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.
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>
22 #define POST_DEFAULT_IO_PORT 0x80
24 void usage(char *progname, const char *error, ...)
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");
30 if (error) {
31 va_list args;
33 va_start(args, error);
34 vprintf(error, args);
35 va_end(args);
39 void check_int(long val, int min, int max, int err, char *string, char *endptr,
40 char *progname)
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);
49 if (endptr == string || *endptr != '\0') {
50 usage(progname, "\nError: An integer is required\n");
51 exit(EXIT_FAILURE);
54 if ((err) && (!val)) {
55 perror("strtol");
56 exit(EXIT_FAILURE);
60 int main(int argc, char *argv[])
62 unsigned long val;
63 unsigned long port = POST_DEFAULT_IO_PORT;
64 char *endptr;
65 int err;
67 if (argc != 2 && argc != 3) {
68 usage(argv[0], NULL);
69 exit(EXIT_FAILURE);
72 val = strtol(argv[1], &endptr, 0);
73 err = errno;
74 check_int(val, 0x00, 0xff, err, argv[1], endptr, argv[0]);
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]);
82 err = iopl(3);
83 if (err == -1) {
84 perror("Not root");
85 exit(EXIT_FAILURE);
88 outb(val, port);
90 return 0;