Philipp Deppenwiese | 73add17 | 2016-08-26 02:10:51 +0200 | [diff] [blame] | 1 | /* intelmetool |
| 2 | * |
| 3 | * Copyright (C) 2013-2016 Philipp Deppenwiese <zaolin@das-labor.org>, |
| 4 | * Copyright (C) 2013-2016 Alexander Couzens <lynxis@fe80.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2 of |
Damien Zammit | f0a9128 | 2019-02-23 12:38:15 +1100 | [diff] [blame] | 9 | * the License, or (at your option) any later version. |
Philipp Deppenwiese | 73add17 | 2016-08-26 02:10:51 +0200 | [diff] [blame] | 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <fcntl.h> |
| 18 | #include <unistd.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <errno.h> |
| 23 | |
| 24 | #include "msr.h" |
| 25 | |
| 26 | #ifndef __DARWIN__ |
| 27 | static int fd_msr = 0; |
| 28 | |
Patrick Rudolph | 3df9dbe | 2017-11-25 14:43:06 +0100 | [diff] [blame] | 29 | static int rdmsr(int addr, uint64_t *msr) |
Philipp Deppenwiese | 73add17 | 2016-08-26 02:10:51 +0200 | [diff] [blame] | 30 | { |
Philipp Deppenwiese | 73add17 | 2016-08-26 02:10:51 +0200 | [diff] [blame] | 31 | if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) { |
| 32 | perror("Could not lseek() to MSR"); |
| 33 | close(fd_msr); |
| 34 | return -1; |
| 35 | } |
| 36 | |
Patrick Rudolph | 3df9dbe | 2017-11-25 14:43:06 +0100 | [diff] [blame] | 37 | if (read(fd_msr, msr, 8) == 8) { |
Philipp Deppenwiese | 73add17 | 2016-08-26 02:10:51 +0200 | [diff] [blame] | 38 | close(fd_msr); |
Patrick Rudolph | 3df9dbe | 2017-11-25 14:43:06 +0100 | [diff] [blame] | 39 | return 0; |
Philipp Deppenwiese | 73add17 | 2016-08-26 02:10:51 +0200 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | if (errno == EIO) { |
| 43 | perror("IO error couldn't read MSR."); |
| 44 | close(fd_msr); |
Patrick Rudolph | 3df9dbe | 2017-11-25 14:43:06 +0100 | [diff] [blame] | 45 | /* On older platforms the MSR might not exists */ |
Philipp Deppenwiese | 73add17 | 2016-08-26 02:10:51 +0200 | [diff] [blame] | 46 | return -2; |
| 47 | } |
| 48 | |
| 49 | perror("Couldn't read() MSR"); |
| 50 | close(fd_msr); |
| 51 | return -1; |
| 52 | } |
| 53 | #endif |
| 54 | |
| 55 | int msr_bootguard(uint64_t *msr, int debug) |
| 56 | { |
| 57 | |
| 58 | #ifndef __DARWIN__ |
| 59 | fd_msr = open("/dev/cpu/0/msr", O_RDONLY); |
| 60 | if (fd_msr < 0) { |
| 61 | perror("Error while opening /dev/cpu/0/msr"); |
| 62 | printf("Did you run 'modprobe msr'?\n"); |
| 63 | return -1; |
| 64 | } |
| 65 | |
Patrick Rudolph | 3df9dbe | 2017-11-25 14:43:06 +0100 | [diff] [blame] | 66 | if (rdmsr(MSR_BOOTGUARD, msr) < 0) |
| 67 | return -1; |
Philipp Deppenwiese | 73add17 | 2016-08-26 02:10:51 +0200 | [diff] [blame] | 68 | #endif |
| 69 | |
| 70 | if (!debug) |
| 71 | *msr &= ~0xff; |
| 72 | |
| 73 | return 0; |
| 74 | } |