File: sci.c

package info (click to toggle)
toshset 1.46-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 284 kB
  • ctags: 429
  • sloc: cpp: 1,997; sh: 1,507; ansic: 909; makefile: 132
file content (463 lines) | stat: -rw-r--r-- 11,074 bytes parent folder | download
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/* sci.c -- System Configuration Interface
 *
 * Copyright (c) 1998/99  Jonathan A. Buzzard (jonathan@buzzard.org.uk)
 *
 * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
 *
 *   This code is covered by the GNU GPL and you are free to make any
 *   changes you wish to it under the terms of the license. However the
 *   code has the potential to render your computer and/or someone else's
 *   unuseable. Unless you truely understand what is going on, I urge you
 *   not to make any modifications and use it as it stands.
 *   
 * $Log: sci.c,v $
 * Revision 1.3  2002/01/19 23:00:56  schwitrs
 * work to try to get password stuff working
 *
 * Revision 1.2  2001/05/14 16:39:51  schwitrs
 * copied Jonathan's new code for SciSet. Motivated by passwd stuff.
 *
 * Revision 1.1  2000/02/03 02:49:03  schwitrs
 * Initial revision
 *
 * Revision 1.12  1999/11/17 16:00:42  jab
 * changed assembler to manually save registers, hopefully this should
 * make the programs more stable
 *
 * Revision 1.11  1999/07/25 14:39:49  jab
 * fixed bugs in SciOpenInterface and SciSetPassword
 * updated email address
 *
 * Revision 1.10  1999/03/11 20:22:14  jab
 * changed get and set routines to use SciRegisters
 *
 * Revision 1.9  1999/03/06 16:46:30  jab
 * moved the BiosVersion and MachineID functions to hci.c
 *
 * Revision 1.8  1998/09/07 18:23:36  jab
 * removed SciGetMachineID2 as no longer required
 * added a routine to extract the model string from the BIOS
 *
 * Revision 1.7  1998/09/04 18:14:31  jab
 * fixed the compile warning about rcsid
 *
 * Revision 1.6  1998/08/24 18:05:04  jab
 * implemented the SciGetBiosVersion function
 *
 * Revision 1.5  1998/08/23 12:16:45  jab
 * fixed the SciACPower function so it now works
 *
 * Revision 1.4  1998/08/19 08:45:10  jab
 * changed GetMachineId to return SCI_SUCCESS/SCI_FAILURE
 *
 * Revision 1.3  1998/08/18 16:54:54  jab
 * fixes to SciGetMachineId curtesy of Patrick Reyonlds <patrick@cs.virgina.edu>
 *
 * Revision 1.2  1998/08/06 08:27:14  jab
 * prepended all functions with Sci
 *
 * Revision 1.1  1998/05/23 08:08:53  jab
 * Initial revision
 *
 *
 * 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, write to the Free Software 
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * 
 */

static const char rcsid[]="$Id: sci.c,v 1.3 2002/01/19 23:00:56 schwitrs Exp $";

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<sys/mman.h>

#include"sci.h"


/*
 * Is this a supported Machine? (ie. is it a Toshiba)
 */
int SciSupportCheck(int *version)
{
	unsigned short dx;
	unsigned char ah;

	asm ("pushl %%eax\n\t" \
		"pushl %%ebx\n\t" \
		"pushl %%ecx\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" \
		:"=dx" (dx), "=ah" (ah) : : "memory" );

	*version = (int) dx;

	return (int) (ah & 0xff);
}


/*
 * Open an interface to the Toshiba hardware.
 *
 *   Note: Set and Get will not work unless an interface has been opened.
 */
int SciOpenInterface(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;
}


/*
 * Close any open interface to the hardware
 */
int SciCloseInterface(void)
{
	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" \
		:"=ah" (ah) : : "memory" );

	return (int) (ah & 0xff);
}


/*
 * Get the setting of a given mode of the laptop
 */
#ifdef NOT_USED
int SciGet(SMMRegisters *reg)
{
	unsigned short ax,bx,cx,dx;

	ax = 0;
	bx = reg->setting;
	cx = reg->current;
	dx = reg->possible;

	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" \
		:"=ax" (ax), "=bx" (bx), "=cx" (cx), "=dx" (dx) \
		:"bx" (bx), "cx" (cx) \
		: "memory" );

	reg->attribute = (ax & 0xff);
	reg->setting = bx;
	reg->current = cx;
	reg->possible = dx;

	return (int) (ax & 0xff00)>>8;
}
#endif /* NOT_USED */

int SciGet(SMMRegisters *reg)
{
	unsigned short ax,bx,cx,dx;

	// note confusing mapping
	ax = 0;
	bx = reg->eax;
	cx = reg->ebx;
	dx = reg->ecx;

	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" \
		:"=ax" (ax), "=bx" (bx), "=cx" (cx), "=dx" (dx) \
		:"bx" (bx), "cx" (cx) \
		: "memory" );

	reg->esi = (ax & 0xff);
	reg->eax = bx;
	reg->ebx = cx;
	reg->ecx = dx;

	return (int) (ax & 0xff00)>>8;
}



#ifdef NOT_USED
/*
 * Set the setting of a given mode of the laptop
 */
int SciSet(SciRegisters *reg)
{
 //#define OLD 
#ifdef OLD
	  unsigned short ax,bx,cx,dx;

	   unsigned short code = 0xf4f4;
	   bx = reg->setting;
	   cx = reg->current;
	   dx = reg->possible;

	   //		   "movw $0xf4f4,%%ax\n\t"
	   asm ("pushl %%esi\n\t" \
		"pushl %%edi\n\t" \
		"movw %1,%%ax\n\t"
		"inb $0xb2,%%al\n\t" \
		"popl %%edi\n\t" \
		"popl %%esi\n" \
		:"=m" (ax) \
		:"m" (code), "bx" (bx), "cx" (cx), "dx" (dx) \
		: "memory" );

	   reg->attribute = (ax & 0xff);

	   return (int) (ax & 0xff00)>>8;

#else
	   //reg->setting = 0xf4f4;
	/*  reg->setting = 0xf400;*/
	int eax =  0;
	int code = 0xf4f4;
	asm ("# load the values into the registers\n\t" \
	     "movl 0(%%eax),%%ebx\n\t" \
	     "movl 4(%%eax),%%ecx\n\t" \
	     "movl 8(%%eax),%%edx\n\t" \
	     "movl 12(%%eax),%%esi\n\t" \
	     "movl 16(%%eax),%%edi\n\t" \
	     "movl %1,%%eax\n\t" \
	     "# call the System Management mode\n\t" \
	     "inb $0xb2,%%al\n" \
	     : "=a" (eax)
	     : "m" (code), "a" (reg)
	     : "memory", "%ebx", "%ecx", "%edx", "%esi", "%edi");

	//	return eax;
	return (int) (eax & 0xff00)>>8;
	//	return (int) (reg->setting & 0xff00)>>8;
#endif
}
#endif /* NOT_USED */

/*
 * Set the setting of a given mode of the laptop
 */
int SciSet(SMMRegisters *reg)
{
#ifdef OLD
	   //reg->setting = 0xf4f4;
	/*  reg->setting = 0xf400;*/
	int eax =  0;
	int code = 0xf4f4;
	asm ("# load the values into the registers\n\t" \
	     "movl 0(%%eax),%%ebx\n\t" \
	     "movl 4(%%eax),%%ecx\n\t" \
	     "movl 8(%%eax),%%edx\n\t" \
	     "movl 12(%%eax),%%esi\n\t" \
	     "movl 16(%%eax),%%edi\n\t" \
	     "movl %1,%%eax\n\t" \
	     "# call the System Management mode\n\t" \
	     "inb $0xb2,%%al\n" \
	     : "=a" (eax)
	     : "m" (code), "a" (reg)
	     : "memory", "%ebx", "%ecx", "%edx", "%esi", "%edi");

	//	return eax;
	return (int) (eax & 0xff00)>>8;
	//	return (int) (reg->setting & 0xff00)>>8;
	int eax;
#endif /* OLD */
	int eax;
	reg->eax = 0xf4f4;

	asm ("# load the values into the registers\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"
		"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");

	return eax;
}


/*
 * Get the status of the AC Power on a Toshiba laptop.
 */
int SciACPower(void)
{
	unsigned short cx;
	unsigned char fl;

	asm ("pushl %%eax\n\t" \
		"pushl %%ebx\n\t" \
		"pushl %%ecx\n\t" \
		"movw $0xfe00,%%ax\n\t" \
		"movw $0x0003,%%bx\n\t" \
		"movw $0x0000,%%cx\n\t" \
		"movw $0x0000,%%dx\n\t" \
		"inb $0xb2,%%al\n\t" \
		"lahf\n\t" \
		"movw %%cx,%0\n\t" \
		"movb %%ah,%1\n\t" \
		"popl %%ecx\n\t" \
		"popl %%ebx\n\t" \
		"popl %%eax\n" \
		:"=m" (cx), "=m" (fl) \
		: \
		: "memory" );

	if ((fl & 0x01)==0)
		return (int) cx;
	else
		return 0;
}


/*
 * My own concoction that will hopefully provide extra identification
 * information.
 *
 *   The function will fill the minimum eight character array passed to it
 *   with the model as a null terminated string. So for example on my
 *   Libretto 50CT it will return "50CT\0", on my Satellite Pro 400CS it
 *   will return "400CS\0".
 *
 */
int SciGetModel(char *model)
{
	int device,loop;
	unsigned char *mem;


	if ((device = open("/dev/mem", O_RDWR))==-1)
		return SCI_FAILURE;

	mem = mmap(0, 0x1000, PROT_READ, MAP_SHARED, device, 0xfe000);
	if (mem==(unsigned char *)-1)
		return SCI_FAILURE;


	for (loop=0;loop<7 && mem[loop]!=0x20;loop++)
		model[loop] = mem[loop];
	model[loop] = 0x00;

	munmap(mem, 0x1000);
	close(device);

	return SCI_SUCCESS;
}


/*
 * Set a supervisor or user password for the laptop.
 *
 *   To remove a password, call this routine with the *same* password.
 *   In theory this should be done via the Set routine.
 *
 */
int SciSetPassword(const char *password, unsigned short type, int *trys)
{
	unsigned char ah;
	unsigned short di,dx;
	unsigned int edx,esi;

	edx = *(password+3) + *(password+2)*0x100 + *(password+1)*0x10000 +
	        *(password)*0x1000000;
	esi = *(password+7) + *(password+6)*0x100 + *(password+5)*0x10000 +
	        *(password+4)*0x1000000;
	di = *(password+9) + *(password+8)*0x100;

	asm ("pushl %%eax\n\t" \
	     "pushl %%ebx\n\t" \
	     "pushl %%ecx\n\t" \
	     "pushl %%edx\n\t" \
	     "pushl %%edi\n\t" \
	     "pushl %%esi\n\t" \
	     "movw $0xf4f4,%%ax\n\t" \
	     "movw $0x0602,%%bx\n\t" \
	     "movw %2,%%cx\n\t" \
	     "movl %3,%%edx\n\t" \
	     "movl %4,%%esi\n\t" \
	     "movw %5,%%di\n\t" \
	     "inb $0xb2,%%al\n\t" \
	     "movw %%dx,%0\n\t" \
	     "movb %%ah,%1\n\t"
	     "popl %%esi\n\t" \
	     "popl %%edi\n\t" \
	     "popl %%edx\n\t" \
	     "popl %%ecx\n\t" \
	     "popl %%ebx\n\t" \
	     "popl %%eax\n" \
	     :"=m" (dx), "=m" (ah) \
	     :"m" (type), "m" (edx), "m" (esi), "m" (di) \
	     : "memory" );

	*trys = (int) dx;

	return (int) ah;
}