summaryrefslogtreecommitdiff
path: root/util/intelmetool/msr.c
blob: 1a5ead9737dcf7d39a4b17de855badb67945e951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* intelmetool */
/* SPDX-License-Identifier: GPL-2.0-or-later */

#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;

static int rdmsr(int addr, uint64_t *msr)
{
	if (lseek(fd_msr, (off_t) addr, SEEK_SET) == -1) {
		perror("Could not lseek() to MSR");
		close(fd_msr);
		return -1;
	}

	if (read(fd_msr, msr, 8) == 8) {
		close(fd_msr);
		return 0;
	}

	if (errno == EIO) {
		perror("IO error couldn't read MSR.");
		close(fd_msr);
		/* On older platforms the MSR might not exists */
		return -2;
	}

	perror("Couldn't read() MSR");
	close(fd_msr);
	return -1;
}
#endif

int msr_bootguard(uint64_t *msr)
{

#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;
	}

	if (rdmsr(MSR_BOOTGUARD, msr) < 0)
		return -1;
#endif

	return 0;
}