-
Notifications
You must be signed in to change notification settings - Fork 1
/
direct32.c
365 lines (312 loc) · 8.96 KB
/
direct32.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "kernelInterface.h"
#include "direct.h"
#include "hci.h"
#include "sci.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
/*
code for direct access to the Toshiba BIOS registers- this does not use
/dev/toshiba, and most likely will not work under ACPI.
*/
int dHciFunction(SMMRegisters *reg)
{
int id=0;
unsigned int eax,ebx,ecx,edx,esi,edi;
eax = reg->eax;
ebx = reg->ebx;
ecx = reg->ecx;
edx = reg->edx;
esi = reg->esi;
edi = reg->edi;
/* emulate the HCI fan function for the Portage 610 and Tecra 700 */
if ((id==0xfccb) && (ebx==HCI_FAN)) {
asm("movw %1,%%ax\n\t" \
"movw %2,%%cx\n\t" \
"cli\n\t" \
"movb $0xbe,%%al\n\t" \
"outb %%al,$0xe4\n\t" \
"inb $0xe5,%%al\n\t" \
"cmpb $0xfe,%%ah\n\t" \
"je 3f\n\t" \
"cmpw $0x0001,%%cx\n\t" \
"jne 1f\n\t" \
"andb $0xfe,%%al\n\t" \
"jmp 2f\n\t" \
"1: orb $0x01,%%al\n\t" \
"2: movb %%al,%%ah\n\t" \
"movb $0xbe,%%al\n\t" \
"outb %%al,$0xe4\n\t" \
"movb %%ah,%%al\n\t" \
"outb %%al,$0xe5\n\t" \
"3: sti\n\t" \
"andw $0x0001,%%ax\n\t" \
"movw %%ax,%0\n" \
:"=m" (ecx) : "m" (eax), "m" (ecx) : "memory" );
ecx = (ecx==0x00) ? 0x01:0x00;
eax = 0x0000;
} else if ((id==0xfccb) && (ebx==HCI_FAN)) {
asm("movw %1,%%ax\n\t" \
"movw %2,%%cx\n\t" \
"cli\n\t" \
"movw $0x00e4,%%dx\n\t" \
"movb $0xe0,%%al\n\t" \
"outb %%al,%%dx\n\t" \
"incw %%dx\n\t" \
"inb %%dx,%%al\n\t" \
"cmpb $0xfe,%%ah\n\t" \
"je 3f\n\t" \
"cmpw $0x0001,%%cx\n\t" \
"jne 1f\n\t" \
"orw $0x0001,%%ax\n\t" \
"jmp 2f\n\t" \
"1: andw $0xfffe,%%ax\n\t" \
"2: decw %%dx\n\t" \
"movb %%al,%%ah\n\t" \
"movb $0xe0,%%al\n\t" \
"outw %%ax,%%dx\n\t" \
"3: sti\n\t" \
"andw $0x0001,%%ax\n\t" \
"movw %%ax,%0\n" \
:"=m" (ecx) : "m" (eax), "m" (ecx) : "memory" );
eax = 0x0000;
} else {
/* note that an extra dummy push onto the stack is made.
* Somehow this entry sometimes get overwritten causing an
* eventual segfault.
*/
asm("# load the values into the registers\n\t" \
"pushl %%eax\n\t" \
"pushl %%eax\n\t" \
"movl 0(%%eax),%%edx\n\t" \
"pushl %%edx\n\t" \
"movl 4(%%eax),%%ebx\n\t" \
"movl 8(%%eax),%%ecx\n\t" \
"movl 12(%%eax),%%edx\n\t" \
"movl 16(%%eax),%%esi\n\t" \
"movl 20(%%eax),%%edi\n\t" \
"popl %%eax\n\t" \
"# call the System Management mode\n\t" \
"inb $0xb2,%%al\n\t" \
"# fill out the memory with the values in the registers\n\t" \
"xchgl %%eax,(%%esp)\n\t" \
"popl %%eax\n\t" \
"xchgl %%eax,(%%esp)\n\t" \
"movl %%ebx,4(%%eax)\n\t" \
"movl %%ecx,8(%%eax)\n\t" \
"movl %%edx,12(%%eax)\n\t" \
"movl %%esi,16(%%eax)\n\t" \
"movl %%edi,20(%%eax)\n\t" \
"popl %%edx\n\t" \
"movl %%edx,0(%%eax)\n\t" \
:
: "eax" (reg)
: "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory", "cc");
}
return (int) (reg->eax & 0xff00)>>8;
} /* dHciFunction */
/*
* Return the BIOS version of the laptop
*
* I may or may not follow the Toshiba version of this function. The
* Toshiba function would appear to read from this area of memory, but
* exactly what it does I am not sure. This implemenation is impirically
* based on the contents of these memory locations on my Satellite Pro
* 400CS and Libretto 50CT. I have confirmed that it returns the correct
* information on a Tecra 750CDT and a T4900 as well.
*/
int
dHciGetBiosVersion(void)
{
int id = 0;
int device,major,minor;
unsigned char *mem;
if ((device = open("/dev/mem", O_RDWR))==-1)
return HCI_FAILURE;
mem = mmap(0, 0x1000, PROT_READ, MAP_SHARED, device, 0xfe000);
if (mem==(unsigned char *)-1)
return HCI_FAILURE;
major = (char) mem[0x0009]-'0';
minor = (((char) mem[0x000b]-'0')*10)+((char) mem[0x000c]-'0');
munmap(mem, 0x1000);
close(device);
id = (major*0x100)+minor;
return id;
} /* dHciGetBiosVersion */
int
dHciGetMachineID(int *id)
{
int device;
unsigned char *mem;
unsigned short bx;
// unsigned char fl;
if ((device = open("/dev/mem", O_RDWR))==-1)
return HCI_FAILURE;
mem = mmap(0, 0x100000, PROT_READ, MAP_SHARED, device,0); // 0xfe000);
if (mem==(unsigned char *)-1)
return HCI_FAILURE;
*id = (0x100*((int) mem[0xffffe]))+((int) mem[0xffffa]);
/* do we have a SCTTable machine identication number on our hands */
if (*id==0xfc2f) {
unsigned char ah;
unsigned short cx;
unsigned long address;
/* start by getting a pointer into the BIOS */
asm ("movw $0xc000,%%ax\n\t" \
"movw $0x0000,%%bx\n\t" \
"movw $0x0000,%%cx\n\t" \
"inb $0xb2,%%al\n\t" \
"movb %%ah,%%al\n" \
: "=b" (bx), "=a" (ah) : : "%ecx");
/* printf("bx: %x mem: %x\n",bx,mem); */
/* At this point in the Toshiba routines under MS Windows
the bx register holds 0xe6f5. However my code is producing
a different value! For the time being I will just fudge the
value. This has been verified on a Satellite Pro 430CDT,
Tecra 750CDT, Tecra 780DVD and Satellite 310CDT. */
bx = 0xe6f5;
/* now twiddle with our pointer a bit */
address = 0x000f0000+bx;
// cx = readw(address);
// address = bx - 0xe000;
cx = *(unsigned short*)(mem+address);
address = 0x000f0009+bx+cx;
// address = 0x9+bx+cx - 0xe000;
// cx = readw(address);
cx = *(unsigned short*)(mem+address);
address = 0x000f000a+cx;
// address = 0xa+cx - 0xe000;
// cx = readw(address);
cx = *(unsigned short*)(mem+address);
/* now construct our machine identification number */
*id = ((cx & 0xff)<<8)+((cx & 0xff00)>>8);
munmap(mem, 0x1000);
close(device);
}
return HCI_SUCCESS;
} /* dHciGetMachineID */
/*
* Is this a supported Machine? (ie. is it a Toshiba)
*/
int
dSciSupportCheck(int *version)
{
unsigned short dx;
unsigned char ah;
asm ("pushl %%eax\n\t" \
"pushl %%ebx\n\t" \
"pushl %%ecx\n\t" \
"pushl %%edx\n\t" \
"movw $0xf0f0,%%ax\n\t" \
"movw $0x0000,%%bx\n\t" \
"movw $0x0000,%%cx\n\t" \
"inb $0xb2,%%al\n\t" \
"popl %%ecx\n\t" \
"popl %%ebx\n\t" \
"popl %%eax\n" \
:"=d" (dx), "=a" (ah) : : "memory" );
*version = (int) dx;
return (int) (ah & 0xff);
} /* dSciSupportCheck */
int
dSciOpenInterface(void)
{
unsigned char ah;
asm ("pushl %%eax\n\t" \
"pushl %%ebx\n\t" \
"pushl %%ecx\n\t" \
"movw $0xf1f1,%%ax\n\t" \
"movw $0x0000,%%bx\n\t" \
"movw $0x0000,%%cx\n\t" \
"inb $0xb2,%%al\n\t" \
"movb %%ah,%0\n\t" \
"popl %%ecx\n\t" \
"popl %%ebx\n\t" \
"popl %%eax\n" \
:"=m" (ah) : : "memory" );
return (int) ah;
} /* dSciOpenInterface */
int
dSciCloseInterface()
{
unsigned char ah;
asm ("pushl %%eax\n\t" \
"pushl %%ebx\n\t" \
"pushl %%ecx\n\t" \
"movw $0xf2f2,%%ax\n\t" \
"movw $0x0000,%%bx\n\t" \
"movw $0x0000,%%cx\n\t" \
"inb $0xb2,%%al\n\t" \
"popl %%ecx\n\t" \
"popl %%ebx\n\t" \
"popl %%eax\n" \
:"=a" (ah) : : "memory" );
return (int) (ah & 0xff);
} /* dSciCloseInterface */
int
dSciGet(SMMRegisters *reg)
{
unsigned short ax,bx,cx,dx;
// note confusing mapping
//ax = 0;
bx = reg->ebx;
cx = reg->ecx;
dx = reg->edx;
asm ("pushl %%esi\n\t" \
"pushl %%edi\n\t" \
"movw $0xf3f3,%%ax\n\t" \
"inb $0xb2,%%al\n\t" \
"popl %%edi\n\t" \
"popl %%esi\n" \
:"=a" (ax), "=b" (bx), "=c" (cx), "=d" (dx) \
:"b" (bx), "c" (cx), "d" (dx) \
: "memory" );
reg->esi = (ax & 0xff);
reg->ebx = bx;
reg->ecx = cx;
reg->edx = dx;
return (int) (ax & 0xff00)>>8;
} /* dSciGet */
/*
* Set the setting of a given mode of the laptop
*/
int
dSciSet(SMMRegisters *reg)
{
int eax;
reg->eax = 0xf4f4;
asm ("# load the values into the registers\n\t" \
"pushl %%eax\n\t" \
"pushl %%eax\n\t" \
"movl 0(%%eax),%%edx\n\t" \
"push %%edx\n\t" \
"movl 4(%%eax),%%ebx\n\t" \
"movl 8(%%eax),%%ecx\n\t" \
"movl 12(%%eax),%%edx\n\t" \
"movl 16(%%eax),%%esi\n\t" \
"movl 20(%%eax),%%edi\n\t" \
"popl %%eax\n\t" \
"# call the System Management mode\n\t" \
"inb $0xb2,%%al\n\t"
"# fill out the memory with the values in the registers\n\t" \
"xchgl %%eax,(%%esp)\n\t" \
"popl %%eax\n\t" \
"xchgl %%eax,(%%esp)\n\t"
"movl %%ebx,4(%%eax)\n\t" \
"movl %%ecx,8(%%eax)\n\t" \
"movl %%edx,12(%%eax)\n\t" \
"movl %%esi,16(%%eax)\n\t" \
"movl %%edi,20(%%eax)\n\t" \
"popl %%edx\n\t" \
"movl %%edx,0(%%eax)\n\t" \
"# setup the return value to the carry flag\n\t" \
"lahf\n\t" \
"shrl $8,%%eax\n\t" \
"andl $1,%%eax\n" \
: "=a" (eax)
: "a" (reg)
: "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory");
return eax;
} /* dSciSet */