| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include <fcntl.h> |
| 5 |
#include <unistd.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <string.h> |
| 9 |
#include <errno.h> |
| 10 |
|
| 11 |
#include "msr.h" |
| 12 |
|
| 13 |
#ifndef __DARWIN__ |
| 14 |
static int fd_msr = 0; |
| 15 |
|
| 16 |
static int rdmsr(int addr, uint64_t *msr) |
| 17 |
{ |
| 18 |
if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) { |
| 19 |
perror("Could not lseek() to MSR"); |
| 20 |
close(fd_msr); |
| 21 |
return -1; |
| 22 |
} |
| 23 |
|
| 24 |
if (read(fd_msr, msr, 8) == 8) { |
| 25 |
close(fd_msr); |
| 26 |
return 0; |
| 27 |
} |
| 28 |
|
| 29 |
if (errno == EIO) { |
| 30 |
perror("IO error couldn't read MSR."); |
| 31 |
close(fd_msr); |
| 32 |
|
| 33 |
return -2; |
| 34 |
} |
| 35 |
|
| 36 |
perror("Couldn't read() MSR"); |
| 37 |
close(fd_msr); |
| 38 |
return -1; |
| 39 |
} |
| 40 |
#endif |
| 41 |
|
| 42 |
int msr_bootguard(uint64_t *msr) |
| 43 |
{ |
| 44 |
|
| 45 |
#ifndef __DARWIN__ |
| 46 |
fd_msr = open("/dev/cpu/0/msr", O_RDONLY); |
| 47 |
if (fd_msr < 0) { |
| 48 |
perror("Error while opening /dev/cpu/0/msr"); |
| 49 |
printf("Did you run 'modprobe msr'?\n"); |
| 50 |
return -1; |
| 51 |
} |
| 52 |
|
| 53 |
if (rdmsr(MSR_BOOTGUARD, msr) < 0) |
| 54 |
return -1; |
| 55 |
#endif |
| 56 |
|
| 57 |
return 0; |
| 58 |
} |