File: init.c

package info (click to toggle)
hwtools 0.5-0.2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,304 kB
  • ctags: 1,213
  • sloc: ansic: 9,522; tcl: 2,140; asm: 802; makefile: 295; sh: 262; cpp: 160; csh: 42
file content (554 lines) | stat: -rw-r--r-- 11,749 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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
/* init.c - MemTest-86  Version 2.2
 *
 * Copyright 1999,  Chris Brady
 *
 * Permission to use, copy, modify, and distribute this
 * software and its documentation for any purpose and without fee
 * is granted provided that the above copyright notice appears in all copies.
 * It is provided "as is" without express or implied warranty.
 */

/* To enable BIOS memory sizing un-comment the following line */
/* #define BIOS_MEMSZ 1 */

#undef DEBUG
#include "test.h"
#include "defs.h"
#include "io.h"

extern unsigned long p1, p2;
extern volatile long *p;
extern struct vars *v;

unsigned long m_lim = 0;
volatile char *pp;
struct cpu_ident cpu_id;
long mem_info[3];
long st_low, st_high;
long end_low, end_high;

/*
 * Initialize test, setup screen and find out how much memory there is.
 */
void init()
{
	int i, n;

	outb(0x8, 0x3f2);  /* Kill Floppy Motor */
	serial_echo_init();
        serial_echo_print("INE_SCROLL;24r");    /* Set scrolling region row 7-23 */
        serial_echo_print("");   /* Clear Screen */

	/* Set background to blue */
	for(i=0, pp=(char *)(SCREEN_ADR+1); i<80*24; i++, pp+=2) {
		*pp = 0x17;
	}

	/* Make the name background red */
	for(i=0, pp=(char *)(SCREEN_ADR+1); i<TITLE_WIDTH; i++, pp+=2) {
		*pp = 0x47;
	}

	/* Do reverse video for the bottom display line */
	for(i=0, pp=(char *)(SCREEN_ADR+1+(24 * 160)); i<80; i++, pp+=2) {
		*pp = 0x71;
	}

        serial_echo_print("");
        serial_echo_print("");

        serial_echo_print("");
	cprint(0, 0, " Memtest-86 v2.2 ");
	cpu_type();
        serial_echo_print("");
	cprint(0, 56, "Memory: ");

#ifdef BIOS_MEMSZ
	m_lim =  (mem_info[0] << 6) + mem_info[1];
	if (m_lim < mem_info[2]) {
		m_lim = mem_info[2];
	}
	m_lim += 0x400;
	m_lim *= 0x400;
#else
	/* Since all address bits are not decoded, the search for memory
	 * must be limited.  The max address is found by checking for
	 * memory wrap from 1mb to 4gb.  */
	v->map[0].start = (long *)0x1234569;
	p1 = (long)&v->map[0].start;
	m_lim = 0xffffffff; 
	for (p2 = 0x100000; p2; p2 <<= 1) {  
		p = (long *)(p1 + p2);
		if (*p == 0x1234569) {
			m_lim = --p2;
			break;
		}
	}
#endif

	/* Find all segments of RAM */

	p = (long *)(START_ADR + 0x400);
	i = 0;
	v->map[i].start = p;
	aprint(0, 64, (long)0);
	cprint(0, 69, " - ");

	/* Limit search for memory to m_lim and make sure we don't 
	 * overflow the 32 bit size of p.  */
	while ((long)p < m_lim && (long)p > START_ADR) {
		/*
		 * Skip over reserved memory
		 */
		if ((long)p < SKIP_END && (long)p >= SKIP_START) {
			v->map[i].end = (long *)SKIP_START;
			v->map[i].v = 1;
			aprint(i, 72, (long)p);
			p = (long *)SKIP_END;
			i++;
			v->map[i].start = 0;
			goto fstart;
		}

		if (check_ram() == 0) {
			/* ROM or nothing at this address, record end addrs */
			v->map[i].end = p;
			v->map[i].v = 1;
			aprint(i, 72, (long)p);
			i++;
			v->map[i].start = 0;
fstart:

			/* We get here when there is a gap in memory.
			 * Loop until we find more ram, the gap is more
			 * than 16384k or we hit m_lim */
			n = 16384*4;
			while ((long)p < m_lim && (long)p > START_ADR) {

				/* Skip over video memory */
				if ((long)p < SKIP_END &&
					(long)p >= SKIP_START) {
					p = (long *)SKIP_END;
				}
				if (check_ram() == 1) {
					/* More RAM, record start addrs */
					v->map[i].start = p;
					aprint(i, 64, (long)p);
					cprint(i, 69, " - ");
					break;
				}

				/* If the gap is 16384k or more then there
				 * is probably no more memory so bail out */
				if (--n <= 0) {
					p = (long *)m_lim;
					break;
				}
				p += 0x100;
			}
		}
		p += 0x100;
	}

	/* If there is ram right up to the memory limit this will record
	 * the last address.  */
	if (v->map[i].start) {
		v->map[i].end = (long *)m_lim;
		v->map[i].v = 1;
		aprint(i, 72, (long)m_lim);
		i++;
		v->map[i].start = 0;
	}
	v->testsel = -1;
	v->msegs = i;
	v->msg_line = LINE_SCROLL-1;
	v->scroll_start = v->msg_line * 160;

	/* Make a copy of the memory map */
	for (i=0; i< v->msegs; i++) {
		v->rmap[i].start = v->map[i].start;
		v->rmap[i].end = v->map[i].end;
	}
	v->lim_lower = 0;
	v->lim_upper = (long)v->rmap[v->msegs-1].end;
	cprint(LINE_TST, 0, "Test No:");
	cprint(LINE_RANGE, 0, "Testing: ");
	cprint(LINE_PAT, 0, "Pattern:");
	cprint(LINE_CACHE, 0, "  Cache:");
	cprint(LINE_REF, 0, "Refresh:");
	cprint(LINE_ERR, 64, "Errors:");
	dprint(LINE_ERR, 72, 0, 6, 1);
	cprint(LINE_PASS, 66, "Pass:");
	dprint(LINE_PASS, 72, v->pass, 6, 1);
	if (v->rdtsc) {
		cprint(0, TIMEX+5, ":  :");
	}
/*
	cprint(LINE_SEQ, 0, "TestSeq:");
	switch(v->xtst_flag) {
	case 0:
		cprint(LINE_SEQ, 9, "Default ");
		break;
	case 1:
		cprint(LINE_SEQ, 9, "Extended");
		break;
	case 2:
		cprint(LINE_SEQ, 9, "All     ");
		break;
	}
*/
	footer();
}

/* check_ram - Determine if this address points to memory by checking
 * for a wrap pattern and then reading and then writing the complement.
 * We then check that at least one bit changed in each byte before
 * believing that it really is memory.  */

int check_ram() {
	int s;

	p1 = *p;

	/* write the complement */
	*p = ~p1;
	p2 = *p;
	s = 0;

	/* Now make sure a bit changed in each byte */
	if ((0xff & p1) != (0xff & p2)) {
		s++;
	}
	if ((0xff00 & p1) != (0xff00 & p2)) {
		s++;
	}
	if ((0xff0000 & p1) != (0xff0000 & p2)) {
		s++;
	}
	if ((0xff000000 & p1) != (0xff000000 & p2)) {
		s++;
	}
	if (s == 4) {
		/* RAM at this address */
		return 1;
	}

	return 0;
}

/*
 * Find CPU type and cache sizes
 */
void cpu_type()
{
	int i, off=0;
	int dcache=0, icache=0, l2_cache=0;
	long speed;

	v->rdtsc = 0;
	cprint(LINE_CPU, COL_CPU-10, "CPU Type:");

#ifdef DEBUG
	dprint(8,0,cpu_id.type,3,1);
	dprint(9,0,cpu_id.model,3,1);
	dprint(10,0,cpu_id.cpuid,3,1);
#endif

	/* If the CPUID instruction is not supported then this is */
	/* a 386, 486 or one of the early Cyrix CPU's */
	if (cpu_id.cpuid < 1) {
		switch (cpu_id.type) {
		case 2:
			/* This is a Cyrix CPU without CPUID */
			i = getCx86(0xfe);
			i &= 0xf0;
			i >>= 4;
			switch(i) {
			case 0:
			case 1:
				cprint(LINE_CPU, COL_CPU, "Cyrix Cx486");
				break;
			case 2:
				cprint(LINE_CPU, COL_CPU,"Cyrix 5x86");
				break;
			case 3:
				cprint(LINE_CPU, COL_CPU,"Cyrix 6x86");
				break;
			case 4:
				cprint(LINE_CPU, COL_CPU,"Cyrix MediaGX");
				break;
			case 5:
				cprint(LINE_CPU, COL_CPU,"Cyrix 6x86MX");
				break;
			case 6:
				cprint(LINE_CPU, COL_CPU,"Cyrix MII");
				break;
			default:
				cprint(LINE_CPU, COL_CPU,"Cyrix ???");
				break;
			}
			break;
		case 3:
			cprint(LINE_CPU, COL_CPU, "386");
			break;

		case 4:
			cprint(LINE_CPU, COL_CPU, "486");
			break;
		}
		return;
	}

	switch(cpu_id.vend_id[0]) {
	/* AMD Processors */
	case 'A':
		switch(cpu_id.type) {
		case 4:
			cprint(LINE_CPU, COL_CPU, "AMD 486/586");
			/* Since we can't get CPU speed or cache info return */
			return;
		case 5:
			switch(cpu_id.model) {
			case 0:
			case 1:
			case 2:
			case 3:
				cprint(LINE_CPU, COL_CPU, "AMD K5");
				off = 6;
				break;
			case 6:
			case 7:
				cprint(LINE_CPU, COL_CPU, "AMD K6");
				off = 6;
				dcache = cpu_id.cache_info[3];
				icache = cpu_id.cache_info[7];
				break;
			case 8:
				cprint(LINE_CPU, COL_CPU, "AMD K6-2");
				off = 8;
				dcache = cpu_id.cache_info[3];
				icache = cpu_id.cache_info[7];
				break;
			case 9:
				cprint(LINE_CPU, COL_CPU, "AMD K6-III");
				off = 10;
				dcache = cpu_id.cache_info[3];
				icache = cpu_id.cache_info[7];
				l2_cache = (cpu_id.cache_info[11] << 8);
				l2_cache += cpu_id.cache_info[10];
				break;
			}
			break;
		case 6:
			cprint(LINE_CPU, COL_CPU, "AMD Athlon");
			off = 10;
			dcache = cpu_id.cache_info[3];
			icache = cpu_id.cache_info[7];
			l2_cache = (cpu_id.cache_info[11] << 8);
			l2_cache += cpu_id.cache_info[10];
		}
		break;

	/* Intel Processors */
	case 'G':
		if (cpu_id.type == 4) {
			switch(cpu_id.model) {
			case 0:
			case 1:
				cprint(LINE_CPU, COL_CPU, "Intel 486DX");
				off = 11;
				break;
			case 2:
				cprint(LINE_CPU, COL_CPU, "Intel 486SX");
				off = 11;
				break;
			case 3:
				cprint(LINE_CPU, COL_CPU, "Intel 486DX2");
				off = 12;
				break;
			case 4:
				cprint(LINE_CPU, COL_CPU, "Intel 486SL");
				off = 11;
				break;
			case 5:
				cprint(LINE_CPU, COL_CPU, "Intel 486SX2");
				off = 12;
				break;
			case 8:
				cprint(LINE_CPU, COL_CPU, "Intel 486DX4");
				off = 12;
				break;
			}
			/* Since we can't get CPU speed or cache info return */
			return;
		}

		/* Get the cache info */
		for (i=0; i<16; i++) {
#ifdef DEBUG
			dprint(11,i*3,cpu_id.cache_info[i],2,1);
#endif
			switch(cpu_id.cache_info[i]) {
			case 0x6:
				icache = 8;
				break;
			case 0x8:
				icache = 16;
				break;
			case 0xa:
				dcache = 8;
				break;
			case 0xc:
				dcache = 16;
				break;
			case 0x40:
				l2_cache = 0;
				break;
			case 0x41:
				l2_cache = 128;
				break;
			case 0x42:
				l2_cache = 256;
				break;
			case 0x43:
				l2_cache = 512;
				break;
			case 0x44:
				l2_cache = 1024;
				break;
			case 0x45:
				l2_cache = 2048;
				break;
			}
		}

		switch(cpu_id.type) {
		case 5:
			if (cpu_id.model == 4) {
				cprint(LINE_CPU, COL_CPU, "Pentium-MMX");
				off = 11;
			} else {
				cprint(LINE_CPU, COL_CPU, "Pentium");
				off = 7;
			}
			break;
		case 6:
			switch(cpu_id.model) {
			case 1:
				cprint(LINE_CPU, COL_CPU, "Pentium Pro");
				off = 11;
				break;
			case 3:
				cprint(LINE_CPU, COL_CPU, "Pentium II");
				off = 10;
				break;
			case 5:
				if (l2_cache) {
					cprint(LINE_CPU, COL_CPU, "Pentium II");
					off = 10;
				} else {
					cprint(LINE_CPU, COL_CPU, "Celeron");
					off = 7;
				}
				break;
			case 6:
				cprint(LINE_CPU, COL_CPU, "Celeron");
				off = 7;
				break;
			case 7:
				cprint(LINE_CPU, COL_CPU, "Pentium III");
				off = 11;
				break;
			}
		}
		break;

	/* Cyrix Processors with CPUID */
	case 'C':
		switch(cpu_id.model) {
		case 0:
			cprint(LINE_CPU, COL_CPU, "Cyrix 6x86MX/MII");
			off = 16;
			break;
		case 4:
			cprint(LINE_CPU, COL_CPU, "Cyrix GXm");
			off = 9;
			break;
		}
		return;
		break;

	/* Unknown processor */
	default:
		off = 3;
		/* Make a guess at the family */
		switch(cpu_id.type) {
		case 5:
			cprint(LINE_CPU, COL_CPU, "586");
			return;
		case 6:
			cprint(LINE_CPU, COL_CPU, "686");
			return;
		}
	}

	/* We are here only if the CPU type supports the rdtsc instruction */
	/* Print CPU speed */
	if ((speed = cpuspeed()) > 0) {
		speed += 50; /* for rounding */
		cprint(LINE_CPU, COL_CPU+off, " (   . mhz)");
		dprint(LINE_CPU, COL_CPU+off+2, speed/1000, 3, 1);
		dprint(LINE_CPU, COL_CPU+off+6, (speed/100)%10, 1, 0);
	}

	/* Print out cache info */
	if (icache+dcache) {
		cprint(LINE_CPU+1, COL_CPU-10, "L1 Cache:     k");
		dprint(LINE_CPU+1, COL_CPU, icache + dcache, 4, 0);
	}
	if (l2_cache) {
		cprint(LINE_CPU+2, COL_CPU-10, "L2 Cache:     k");
		dprint(LINE_CPU+2, COL_CPU, l2_cache, 4, 0);
	}

        asm __volatile__ ("rdtsc":"=a" (v->startl),"=d" (v->starth));
        v->snapl = v->startl;
        v->snaph = v->starth;
	v->rdtsc = 1;
}

/* #define TICKS 5 * 11832 (count = 6376)*/
/* #define TICKS (65536 - 12752) */
#define TICKS (65536 - 8271)
int cpuspeed()
{
	int loops;

	/* Setup timer */
	outb((inb(0x61) & ~0x02) | 0x01, 0x61);
	outb(0xb0, 0x43); 
	outb(TICKS & 0xff, 0x42);
	outb(TICKS >> 8, 0x42);

	asm __volatile__ ("rdtsc":"=a" (st_low),"=d" (st_high));

	loops = 0;
	do {
		loops++;
	} while ((inb(0x61) & 0x20) == 0);

	asm __volatile__ (
		"rdtsc\n\t" \
		"subl st_low,%%eax\n\t" \
		"subl st_high,%%edx\n\t" \
		:"=a" (end_low), "=d" (end_high)
		:: "esi", "edi", "ecx"
	);

	/* Make sure we have a credible result */
	if (loops < 4 || end_low < 50000) {
		return(-1);
	}
	v->clks_msec = end_low/48;
	return(v->clks_msec);
}