blob: 5c84f2b58273ab076446c8240063566d2b95c8e9 [file] [log] [blame]
Philipp Deppenwiese73add172016-08-26 02:10:51 +02001/* 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 Zammitf0a91282019-02-23 12:38:15 +11009 * the License, or (at your option) any later version.
Philipp Deppenwiese73add172016-08-26 02:10:51 +020010 *
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__
27static int fd_msr = 0;
28
Patrick Rudolph3df9dbe2017-11-25 14:43:06 +010029static int rdmsr(int addr, uint64_t *msr)
Philipp Deppenwiese73add172016-08-26 02:10:51 +020030{
Philipp Deppenwiese73add172016-08-26 02:10:51 +020031 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 Rudolph3df9dbe2017-11-25 14:43:06 +010037 if (read(fd_msr, msr, 8) == 8) {
Philipp Deppenwiese73add172016-08-26 02:10:51 +020038 close(fd_msr);
Patrick Rudolph3df9dbe2017-11-25 14:43:06 +010039 return 0;
Philipp Deppenwiese73add172016-08-26 02:10:51 +020040 }
41
42 if (errno == EIO) {
43 perror("IO error couldn't read MSR.");
44 close(fd_msr);
Patrick Rudolph3df9dbe2017-11-25 14:43:06 +010045 /* On older platforms the MSR might not exists */
Philipp Deppenwiese73add172016-08-26 02:10:51 +020046 return -2;
47 }
48
49 perror("Couldn't read() MSR");
50 close(fd_msr);
51 return -1;
52}
53#endif
54
55int 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 Rudolph3df9dbe2017-11-25 14:43:06 +010066 if (rdmsr(MSR_BOOTGUARD, msr) < 0)
67 return -1;
Philipp Deppenwiese73add172016-08-26 02:10:51 +020068#endif
69
70 if (!debug)
71 *msr &= ~0xff;
72
73 return 0;
74}