-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdell-bios-fan-control.c
138 lines (115 loc) · 3.46 KB
/
dell-bios-fan-control.c
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* dell-bios-fan-control
* user space utility to set control of fans by bios on Dell 9560 Laptops.
*
* SMM Management code from i8k. See file drivers/char/i8k.c at Linux kernel.
*
* Copyright (C) 2001 Massimo Dal Zotto <[email protected]>
* Copyright (C) 2014 Carlos Alberto Lopez Perez <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/io.h>
/*
*
* Most of the code was taken from https://github.com/clopez/dellfan
*
*/
#define DISABLE_BIOS_METHOD2 0x34a3
#define ENABLE_BIOS_METHOD2 0x35a3
void init_ioperm(void)
{
if (ioperm(0xb2, 4, 1) || ioperm(0x84, 4, 1)) {
perror("ioperm");
exit(EXIT_FAILURE);
}
}
struct smm_regs {
unsigned int eax;
unsigned int ebx __attribute__ ((packed));
unsigned int ecx __attribute__ ((packed));
unsigned int edx __attribute__ ((packed));
unsigned int esi __attribute__ ((packed));
unsigned int edi __attribute__ ((packed));
};
static int i8k_smm(struct smm_regs *regs)
{
int rc;
int eax = regs->eax;
asm volatile("pushq %%rax\n\t"
"movl 0(%%rax),%%edx\n\t"
"pushq %%rdx\n\t"
"movl 4(%%rax),%%ebx\n\t"
"movl 8(%%rax),%%ecx\n\t"
"movl 12(%%rax),%%edx\n\t"
"movl 16(%%rax),%%esi\n\t"
"movl 20(%%rax),%%edi\n\t"
"popq %%rax\n\t"
"out %%al,$0xb2\n\t"
"out %%al,$0x84\n\t"
"xchgq %%rax,(%%rsp)\n\t"
"movl %%ebx,4(%%rax)\n\t"
"movl %%ecx,8(%%rax)\n\t"
"movl %%edx,12(%%rax)\n\t"
"movl %%esi,16(%%rax)\n\t"
"movl %%edi,20(%%rax)\n\t"
"popq %%rdx\n\t"
"movl %%edx,0(%%rax)\n\t"
"pushfq\n\t"
"popq %%rax\n\t"
"andl $1,%%eax\n"
:"=a"(rc)
: "a"(regs)
: "%ebx", "%ecx", "%edx", "%esi", "%edi");
if (rc != 0 || (regs->eax & 0xffff) == 0xffff || regs->eax == eax)
return -1;
return 0;
}
int send(unsigned int cmd, unsigned int arg) {
struct smm_regs regs = { .eax = cmd, };
regs.ebx = arg;
i8k_smm(®s);
return regs.eax ;
}
int main(int argc, char **argv) {
int enable;
if (geteuid() != 0) {
printf("need root privileges\n");
exit(EXIT_FAILURE);
}
if (argc != 2) {
printf ("Use: %s enable\n", argv[0]);
printf ("\tenable = {0,1}\n");
exit(EXIT_FAILURE);
}
init_ioperm();
enable = atoi(argv[1]);
if (enable == 1) {
send(ENABLE_BIOS_METHOD2, 0);
printf ("BIOS CONTROL ENABLED\n");
}
else
if (enable == 0) {
send(DISABLE_BIOS_METHOD2, 0);
printf ("BIOS CONTROL DISABLED\n");
}
else {
printf ("Use 1 to enable bios control or 0 to disable\n");
exit(EXIT_FAILURE);
}
}