coreboot-kgpe-d16/util/pmh7tool/pmh7tool.c

220 lines
4.4 KiB
C
Raw Normal View History

util/: Replace GPLv2 boiler plate with SPDX header Used commands: perl -i -p0e 's|\/\*[\s*]*.*is free software[:;][\s*]*you[\s*]*can[\s*]*redistribute[\s*]*it[\s*]*and\/or[\s*]*modify[\s*]*it[\s*]*under[\s*]*the[\s*]*terms[\s*]*of[\s*]*the[\s*]*GNU[\s*]*General[\s*]*Public[\s*]*License[\s*]*as[\s*]*published[\s*]*by[\s*]*the[\s*]*Free[\s*]*Software[\s*]*Foundation[;,][\s*]*version[\s*]*2[\s*]*of[\s*]*the[\s*]*License.[\s*]*This[\s*]*program[\s*]*is[\s*]*distributed[\s*]*in[\s*]*the[\s*]*hope[\s*]*that[\s*]*it[\s*]*will[\s*]*be[\s*]*useful,[\s*]*but[\s*]*WITHOUT[\s*]*ANY[\s*]*WARRANTY;[\s*]*without[\s*]*even[\s*]*the[\s*]*implied[\s*]*warranty[\s*]*of[\s*]*MERCHANTABILITY[\s*]*or[\s*]*FITNESS[\s*]*FOR[\s*]*A[\s*]*PARTICULAR[\s*]*PURPOSE.[\s*]*See[\s*]*the[\s*]*GNU[\s*]*General[\s*]*Public[\s*]*License[\s*]*for[\s*]*more[\s*]*details.[\s*]*\*\/|/* SPDX-License-Identifier: GPL-2.0-only */|' $(cat filelist) perl -i -p0e 's|This[\s*]*program[\s*]*is[\s*]*free[\s*]*software[:;][\s*]*you[\s*]*can[\s*]*redistribute[\s*]*it[\s*]*and/or[\s*]*modify[\s*]*it[\s*]*under[\s*]*the[\s*]*terms[\s*]*of[\s*]*the[\s*]*GNU[\s*]*General[\s*]*Public[\s*]*License[\s*]*as[\s*]*published[\s*]*by[\s*]*the[\s*]*Free[\s*]*Software[\s*]*Foundation[;,][\s*]*either[\s*]*version[\s*]*2[\s*]*of[\s*]*the[\s*]*License,[\s*]*or[\s*]*.at[\s*]*your[\s*]*option.*[\s*]*any[\s*]*later[\s*]*version.[\s*]*This[\s*]*program[\s*]*is[\s*]*distributed[\s*]*in[\s*]*the[\s*]*hope[\s*]*that[\s*]*it[\s*]*will[\s*]*be[\s*]*useful,[\s*]*but[\s*]*WITHOUT[\s*]*ANY[\s*]*WARRANTY;[\s*]*without[\s*]*even[\s*]*the[\s*]*implied[\s*]*warranty[\s*]*of[\s*]*MERCHANTABILITY[\s*]*or[\s*]*FITNESS[\s*]*FOR[\s*]*A[\s*]*PARTICULAR[\s*]*PURPOSE.[\s*]*See[\s*]*the[\s*]*GNU[\s*]*General[\s*]*Public[\s*]*License[\s*]*for[\s*]*more[\s*]*details.[\s*]*\*\/|/* SPDX-License-Identifier: GPL-2.0-or-later */|' $(cat filelist) perl -i -p0e 's|\/\*[\s*]*.*This[\s*#]*program[\s*#]*is[\s*#]*free[\s*#]*software[;:,][\s*#]*you[\s*#]*can[\s*#]*redistribute[\s*#]*it[\s*#]*and/or[\s*#]*modify[\s*#]*it[\s*#]*under[\s*#]*the[\s*#]*terms[\s*#]*of[\s*#]*the[\s*#]*GNU[\s*#]*General[\s*#]*Public[\s*#]*License[\s*#]*as[\s*#]*published[\s*#]*by[\s*#]*the[\s*#]*Free[\s*#]*Software[\s*#]*Foundation[;:,][\s*#]*either[\s*#]*version[\s*#]*3[\s*#]*of[\s*#]*the[\s*#]*License[;:,][\s*#]*or[\s*#]*.at[\s*#]*your[\s*#]*option.*[\s*#]*any[\s*#]*later[\s*#]*version.[\s*#]*This[\s*#]*program[\s*#]*is[\s*#]*distributed[\s*#]*in[\s*#]*the[\s*#]*hope[\s*#]*that[\s*#]*it[\s*#]*will[\s*#]*be[\s*#]*useful[;:,][\s*#]*but[\s*#]*WITHOUT[\s*#]*ANY[\s*#]*WARRANTY[;:,][\s*#]*without[\s*#]*even[\s*#]*the[\s*#]*implied[\s*#]*warranty[\s*#]*of[\s*#]*MERCHANTABILITY[\s*#]*or[\s*#]*FITNESS[\s*#]*FOR[\s*#]*A[\s*#]*PARTICULAR[\s*#]*PURPOSE.[\s*#]*See[\s*#]*the[\s*#]*GNU[\s*#]*General[\s*#]*Public[\s*#]*License[\s*#]*for[\s*#]*more[\s*#]*details.[\s*]*\*\/|/* SPDX-License-Identifier: GPL-3.0-or-later */|' $(cat filelist) perl -i -p0e 's|(\#\#*)[\w]*.*is free software[:;][\#\s]*you[\#\s]*can[\#\s]*redistribute[\#\s]*it[\#\s]*and\/or[\#\s]*modify[\#\s]*it[\s\#]*under[\s \#]*the[\s\#]*terms[\s\#]*of[\s\#]*the[\s\#]*GNU[\s\#]*General[\s\#]*Public[\s\#]*License[\s\#]*as[\s\#]*published[\s\#]*by[\s\#]*the[\s\#]*Free[\s\#]*Software[\s\#]*Foundation[;,][\s\#]*version[\s\#]*2[\s\#]*of[\s\#]*the[\s\#]*License.*[\s\#]*This[\s\#]*program[\s\#]*is[\s\#]*distributed[\s\#]*in[\s\#]*the[\s\#]*hope[\s\#]*that[\s\#]*it[\s\#]*will[\#\s]*be[\#\s]*useful,[\#\s]*but[\#\s]*WITHOUT[\#\s]*ANY[\#\s]*WARRANTY;[\#\s]*without[\#\s]*even[\#\s]*the[\#\s]*implied[\#\s]*warranty[\#\s]*of[\#\s]*MERCHANTABILITY[\#\s]*or[\#\s]*FITNESS[\#\s]*FOR[\#\s]*A[\#\s]*PARTICULAR[\#\s]*PURPOSE.[\#\s]*See[\#\s]*the[\#\s]*GNU[\#\s]*General[\#\s]*Public[\#\s]*License[\#\s]*for[\#\s]*more[\#\s]*details.\s(#* *\n)*|\1 SPDX-License-Identifier: GPL-2.0-only\n\n|' $(cat filelist) perl -i -p0e 's|(\#\#*)[\w*]*.*is free software[:;][\s*]*you[\s*]*can[\s*]*redistribute[\s*]*it[\s*]*and\/or[\s*]*modify[\s*]*it[\s*]*under[\s*]*the[\s*]*terms[\s*]*of[\s*]*the[\s*]*GNU[\s*]*General[\s*]*Public[\s*]*License[\s*]*as[\s*]*published[\s*]*by[\s*]*the[\s*]*Free[\s*]*Software[\s*]*Foundation[;,][\s*]*version[\s*]*2[\s*]*of[\s*]*the[\s*]*License.[\s*]*This[\s*]*program[\s*]*is[\s*]*distributed[\s*]*in[\s*]*the[\s*]*hope[\s*]*that[\s*]*it[\s*]*will[\s*]*be[\s*]*useful,[\s*]*but[\s*]*WITHOUT[\s*]*ANY[\s*]*WARRANTY;[\s*]*without[\s*]*even[\s*]*the[\s*]*implied[\s*]*warranty[\s*]*of[\s*]*MERCHANTABILITY[\s*]*or[\s*]*FITNESS[\s*]*FOR[\s*]*A[\s*]*PARTICULAR[\s*]*PURPOSE.[\s*]*See[\s*]*the[\s*]*GNU[\s*]*General[\s*]*Public[\s*]*License[\s*]*for[\s*]*more[\s*]*details.\s(#* *\n)*|\1 SPDX-License-Identifier: GPL-2.0-only\n\n|' $(cat filelist) Change-Id: I1008a63b804f355a916221ac994701d7584f60ff Signed-off-by: Patrick Georgi <pgeorgi@google.com> Signed-off-by: Elyes HAOUAS <ehaouas@noos.fr> Reviewed-on: https://review.coreboot.org/c/coreboot/+/41177 Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
2020-05-08 20:48:04 +02:00
/* SPDX-License-Identifier: GPL-2.0-only */
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <sys/io.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include "pmh7tool.h"
uint8_t pmh7_register_read(uint16_t reg)
{
outb(reg & 0xff, EC_LENOVO_PMH7_ADDR_L);
outb((reg & 0xff00) >> 8, EC_LENOVO_PMH7_ADDR_H);
return inb(EC_LENOVO_PMH7_DATA);
}
void pmh7_register_write(uint16_t reg, uint8_t val)
{
outb(reg & 0xff, EC_LENOVO_PMH7_ADDR_L);
outb((reg & 0xff00) >> 8, EC_LENOVO_PMH7_ADDR_H);
outb(val, EC_LENOVO_PMH7_DATA);
}
void pmh7_register_set_bit(uint16_t reg, uint8_t bit)
{
uint8_t val;
val = pmh7_register_read(reg);
pmh7_register_write(reg, val | (1 << bit));
}
void pmh7_register_clear_bit(uint16_t reg, uint8_t bit)
{
uint8_t val;
val = pmh7_register_read(reg);
pmh7_register_write(reg, val & ~(1 << bit));
}
uint8_t pmh7_register_read_bit(int16_t reg, uint8_t bit)
{
uint8_t val;
val = pmh7_register_read(reg);
return (val >> bit) & 1;
}
void print_usage(const char *name)
{
printf("usage: %s\n", name);
printf("\n"
" -h, --help: print this help\n"
" -d, --dump: print registers\n"
" -w, --write <addr> <data>: write to register\n"
" -r, --read <addr>: read from register\n"
" -b, --read-bit <addr> <bit> read bit\n"
" -c, --clear-bit <addr> <bit> clear bit\n"
" -s, --set-bit <addr> <bit> set bit\n"
"\n"
"Attention! Writing to PMH7 registers is very dangerous, as you\n"
" directly manipulate the power rails, enable lines,\n"
" interrupt lines or something else of the device.\n"
" Proceed with caution."
"\n");
}
enum action {HELP, DUMP, WRITE, READ, READBIT, CLEAR, SET};
int main(int argc, char *argv[])
{
enum action act = HELP;
int opt, option_index = 0;
long input_addr = 0, input_data = 0;
static struct option long_options[] = {
{"help", 0, 0, 'h'},
{"dump", 0, 0, 'd'},
{"write", 1, 0, 'w'},
{"read", 1, 0, 'r'},
{"read-bit", 1, 0, 'b'},
{"clear-bit", 1, 0, 'c'},
{"set-bit", 1, 0, 's'},
{0, 0, 0, 0}
};
if (argv[1] == NULL) {
print_usage(argv[0]);
exit(0);
}
while ((opt = getopt_long(argc, argv, "hdw:r:c:s:b:",
long_options, &option_index)) != EOF) {
switch (opt) {
case 'd':
act = DUMP;
break;
case 'r':
input_addr = strtoul(optarg, NULL, 16);
act = READ;
break;
case 'w':
case 'b':
case 'c':
case 's':
input_addr = strtoul(optarg, NULL, 16);
if (optind < argc && *argv[optind] != '-') {
input_data = strtoul(argv[optind], NULL, 16);
optind++;
} else {
fprintf(stderr,
"Error: -%c option requires two arguments\n",
opt);
exit(1);
}
switch (opt) {
case 'w':
act = WRITE;
break;
case 'b':
act = READBIT;
break;
case 'c':
act = CLEAR;
break;
case 's':
act = SET;
break;
}
break;
}
}
if (optind < argc) {
fprintf(stderr, "Error: Extra parameter found.\n");
print_usage(argv[0]);
exit(1);
}
if (act == HELP) {
print_usage(argv[0]);
exit(0);
}
if (input_addr > 0x1ff) {
fprintf(stderr,
"Error: <addr> cannot be greater than 9 bits long.\n");
exit(1);
}
if (act == SET || act == CLEAR || act == READBIT) {
if (input_data > 7) {
fprintf(stderr,
"Error: <bit> cannot be greater than 7.\n");
exit(1);
}
} else {
if (input_data > 0xff) {
fprintf(stderr,
"Error: <data> cannot be greater than 8 bits long.\n");
exit(1);
}
}
if (geteuid() != 0) {
fprintf(stderr, "You must be root.\n");
exit(1);
}
if (ioperm(EC_LENOVO_PMH7_BASE, 0x10, 1)) {
fprintf(stderr, "ioperm: %s\n", strerror(errno));
exit(1);
}
switch (act) {
case DUMP:
for (int i = 0; i < 0x200; i++) {
if ((i % 0x10) == 0) {
if (i != 0)
printf("\n");
printf("%04x: ", i);
}
printf("%02x ", pmh7_register_read(i));
}
printf("\n");
break;
case READ:
printf("%02x\n", pmh7_register_read(input_addr));
break;
case WRITE:
pmh7_register_write(input_addr, input_data);
break;
case READBIT:
printf("%d\n", pmh7_register_read_bit(input_addr, input_data));
break;
case CLEAR:
pmh7_register_clear_bit(input_addr, input_data);
break;
case SET:
pmh7_register_set_bit(input_addr, input_data);
break;
default:
break;
}
return 0;
}