2020-05-05 23:43:18 +02:00
|
|
|
/* intelmetool */
|
2020-05-05 22:49:26 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-or-later */
|
2016-08-26 02:10:51 +02:00
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "msr.h"
|
|
|
|
|
|
|
|
#ifndef __DARWIN__
|
|
|
|
static int fd_msr = 0;
|
|
|
|
|
2017-11-25 14:43:06 +01:00
|
|
|
static int rdmsr(int addr, uint64_t *msr)
|
2016-08-26 02:10:51 +02:00
|
|
|
{
|
|
|
|
if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
|
|
|
|
perror("Could not lseek() to MSR");
|
|
|
|
close(fd_msr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-11-25 14:43:06 +01:00
|
|
|
if (read(fd_msr, msr, 8) == 8) {
|
2016-08-26 02:10:51 +02:00
|
|
|
close(fd_msr);
|
2017-11-25 14:43:06 +01:00
|
|
|
return 0;
|
2016-08-26 02:10:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (errno == EIO) {
|
|
|
|
perror("IO error couldn't read MSR.");
|
|
|
|
close(fd_msr);
|
2017-11-25 14:43:06 +01:00
|
|
|
/* On older platforms the MSR might not exists */
|
2016-08-26 02:10:51 +02:00
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
perror("Couldn't read() MSR");
|
|
|
|
close(fd_msr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-09-18 10:32:22 +02:00
|
|
|
int msr_bootguard(uint64_t *msr)
|
2016-08-26 02:10:51 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
#ifndef __DARWIN__
|
|
|
|
fd_msr = open("/dev/cpu/0/msr", O_RDONLY);
|
|
|
|
if (fd_msr < 0) {
|
|
|
|
perror("Error while opening /dev/cpu/0/msr");
|
|
|
|
printf("Did you run 'modprobe msr'?\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-11-25 14:43:06 +01:00
|
|
|
if (rdmsr(MSR_BOOTGUARD, msr) < 0)
|
|
|
|
return -1;
|
2016-08-26 02:10:51 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|