File: linuxboot.c

package info (click to toggle)
atari-bootstrap 3.3-3
  • links: PTS
  • area: main
  • in suites: potato, woody
  • size: 468 kB
  • ctags: 528
  • sloc: ansic: 3,531; perl: 529; makefile: 115; sh: 26
file content (1394 lines) | stat: -rw-r--r-- 41,006 bytes parent folder | download | duplicates (2)
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
/*
 * linuxboot.c -- Do actual booting of Linux kernel
 *
 * Copyright (c) 1993-98 by
 *   Arjan Knor
 *   Robert de Vries
 *   Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
 *   Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file COPYING in the main directory of this archive
 * for more details.
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stddef.h>
#include <string.h>
#include <ctype.h>
#include <osbind.h>
#include <sys/file.h>

#include "linuxboot.h"
#include "stream.h"
#include "inline-funcs.h"

#define MIN_RAMSIZE     (3)	/* 3 MB */
#define TEMP_STACKSIZE 256

/* This is missing in <unistd.h> */
extern int sync (void);

static struct atari_bootinfo bi;

#ifdef BOOTINFO_COMPAT_1_0
static struct compat_bootinfo compat_bootinfo;
#endif /* BOOTINFO_COMPAT_1_0 */

#define MAX_BI_SIZE     (4096)
static u_long bi_size;
static union {
struct bi_record record;
    u_char fake[MAX_BI_SIZE];
} bi_union;
static u_long userstk;
static u_long *cookiejar;

/* for boot_exit() inline function */
#include "bootstrap.h"

/* declarations of stream modules */
extern MODULE file_mod;
extern MODULE gunzip_mod;
#ifdef USE_BOOTP
extern MODULE bootp_mod;
#endif

/* global variables for communicating options */
int debugflag = 0;		/* debugging */
int ignore_ttram = 0;		/* ignore TT RAM */
int load_to_stram = 0;		/* put kernel into ST RAM */
int force_st_size = -1;		/* force size of ST RAM (-1=autodetect) */
int force_tt_size = -1;		/* force size of TT RAM (-1=autodetect) */
unsigned long extramem_start=0;	/* start of extra memory block (0=none) */
unsigned long extramem_size=0;	/* size of that block */
char *kernel_name = "vmlinux";	/* name of kernel image */
char *ramdisk_name = NULL;	/* name of ramdisk image */
char command_line[CL_SIZE];	/* kernel command line */

/* these are defined in the mover code */
extern char copyall, copyallend;

/* TOS system variables */
#define phystop    ((unsigned long *)0x42e)
#define _p_cookies ((unsigned long **)0x5a0)
#define ramtop     ((unsigned long *)0x5a4)

/***************************** Prototypes *****************************/

static void get_cpu_infos( void );
static void get_mch_type( void );
static void get_mem_infos( void );
static char *format_mb( unsigned long size );
static int getcookie( char *cookie, u_long *value);
static int check_bootinfo_version( char *memptr);
static int create_bootinfo( void);
static int add_bi_record( u_short tag, u_short size, const void *data);
static int add_bi_string( u_short tag, const u_char *s);
static int create_compat_bootinfo( void);
static int test_cpu_type (void);
static int test_software_fpu( void);
static void get_medusa_bank_sizes( u_long *bank1, u_long *bank2 );
static int get_ab040_bank_sizes( int maxres, u_long *result );

/************************* End of Prototypes **************************/


#define ERROR(fmt,rest...)			\
    do {					\
	fprintf( stderr, fmt, ##rest );		\
	boot_exit( EXIT_FAILURE );		\
    } while(0)

#define SERROR(fmt,rest...)			\
    do {					\
	fprintf( stderr, fmt, ##rest );		\
	sclose();				\
	boot_exit( EXIT_FAILURE );		\
    } while(0)


void linux_boot( void )
{
    int i;
    char *kname;
    void *bi_ptr;		/* pointer to bootinfo */
    Elf32_Ehdr kexec_elf;	/* header of kernel executable */
    Elf32_Phdr *kernel_phdrs = NULL;
    struct exec kexec;
    u_long start_mem, mem_size;	/* addr and size of mem chunk where the kernel
				 * goes to */
    u_long kernel_size;		/* size of kernel image */
    char *memptr;		/* addr and size of load region */
    u_long memreq;
    u_long rd_size = 0;		/* size of ramdisk and array of pointers to
				 * its data */
    char **rdptr = NULL;
#ifdef AOUT_KERNEL
    int elf_kernel;
    u_long text_offset = 0;
#else
#define elf_kernel 1
#endif

    /* We have to access some system variables to get
     * the information we need, so we must switch to
     * supervisor mode first.
     */
    userstk = Super(0L);

    /* get the info we need from the cookie-jar */
    cookiejar = *_p_cookies;
    if(cookiejar == 0L)
	/* if we find no cookies, it's probably an ST */
	ERROR( "Error: No cookiejar found. Is this an ST?\n" );

    /* Here we used to warn if MiNT was running, but this proved to be
     * unnecessary */

    /* machine is Atari */
    bi.machtype = MACH_ATARI;

    /* Copy command line options into the kernel command line */
    strcpy( bi.command_line, command_line );
    kname = kernel_name;
    if (strncmp( kernel_name, "local:", 6 ) == 0)
	kname += 6;
    if (strlen(kname)+12 < CL_SIZE-1) {
	if (*bi.command_line)
	    strcat( bi.command_line, " " );
	strcat( bi.command_line, "BOOT_IMAGE=" );
	strcat( bi.command_line, kname );
    }
    printf ("Kernel command line: %s\n", bi.command_line );

    get_cpu_infos();
    get_mch_type();
    
    /* On the Afterburner040, neither bootstrap code nor data may reside in
     * FastRAM. The FastRAM there is joined to one contiguous block with the
     * MMU, and thus FastRAM addresses can become invalid as soon as we turn
     * off the MMU. */
    if (bi.mch_type == ATARI_MACH_AB40 &&
	((unsigned long)linux_boot & 0xff000000))
	ERROR( "Error: Bootstrap can't run in FastRAM on Afterburner040\n" );
    /* For similar reasons, the bootstrap doesn't work in Centurbo2 TT-RAM */
    if (getcookie("_CT2", NULL) != -1 &&
	((unsigned long)linux_boot & 0xff000000))
	ERROR( "Error: Bootstrap can't run in TT-RAM on Centurbo2\n" );
    
    get_mem_infos();
    /* extract data of first chunk, there goes the kernel to */
    start_mem = bi.memory[0].addr;
    mem_size = bi.memory[0].size;

#ifdef TEST
    /*
    ** Temporary exit point for testing
    */
    boot_exit(-1);
#endif /* TEST */

    /*
     * load the ramdisk
     *
     * This must be done before creating the bootinfo (there the ramdisk size
     * is needed). But the ramdisk image must also reside physically *after*
     * the kernel image. (This condition is needed by the mover to avoid
     * overwriting data.) This is ensured by using only one malloc() for
     * kernel+ramdisk, but the size for that malloc() is known only after the
     * ramdisk is loaded... I can't see a really nice solution for this :-(
     * I've choosen the scheme below because it is faster than using realloc()
     * to dynamically extend the ramdisk block (avoids potentially many copy
     * actions). 
     */
    if (ramdisk_name) {
	int n, n_rdptrs = 0;

	/* init a new stream stack, and omit gunzip_mod, the kernel can
	 * decompress the ramdisk itself */
	stream_init();
	stream_push( &file_mod );
#ifdef USE_BOOTP
	stream_push( &bootp_mod );
#endif

	if (sopen( ramdisk_name ) < 0)
	    ERROR( "Unable to open ramdisk file %s\n", ramdisk_name );
	
#define RD_CHUNK_SIZE	(128*1024)
	do {
	    rdptr = realloc( rdptr, (n_rdptrs+1)*sizeof(char *) );
	    if (!rdptr || !(rdptr[n_rdptrs] = malloc( RD_CHUNK_SIZE )))
		SERROR( "Out of memory for ramdisk image\n" );

	    n = sread( rdptr[n_rdptrs], RD_CHUNK_SIZE );
	    if (n < 0)
		SERROR( "Error while reading ramdisk image\n" );
	    if (n == 0) {
		free( rdptr[n_rdptrs]);
		break;
	    }
	    rd_size += n;
	    n_rdptrs++;
	} while( n == RD_CHUNK_SIZE );
	sclose();
    }
    bi.ramdisk.size = rd_size;
    
    /*
     * Open the kernel image and analyze it
     */

    stream_init();
    stream_push( &file_mod );
#ifdef USE_BOOTP
    stream_push( &bootp_mod );
#endif
    stream_push( &gunzip_mod );
    
    if (sopen( kernel_name ) < 0)
	ERROR( "Unable to get kernel image %s\n", kernel_name );

    if (sread( &kexec_elf, sizeof(kexec_elf) ) != sizeof(kexec_elf))
	SERROR( "Cannot read ELF header of kernel image\n" );

    if (memcmp( &kexec_elf.e_ident[EI_MAG0], ELFMAG, SELFMAG ) == 0) {
#ifdef AOUT_KERNEL
	elf_kernel = 1;
#endif
	if (kexec_elf.e_type != ET_EXEC || kexec_elf.e_machine != EM_68K ||
	    kexec_elf.e_version != EV_CURRENT)
	    SERROR( "Invalid ELF header contents in kernel\n" );

	/* Load the program headers */
	kernel_phdrs = (Elf32_Phdr *)malloc( kexec_elf.e_phnum *
					     sizeof (Elf32_Phdr) );
	if (!kernel_phdrs)
	    SERROR( "Unable to allocate memory for program headers\n" );
	sseek( kexec_elf.e_phoff, SEEK_SET );
	if (sread( kernel_phdrs, kexec_elf.e_phnum * sizeof (*kernel_phdrs) )
	    != kexec_elf.e_phnum * sizeof (*kernel_phdrs))
	    SERROR( "Unable to read program headers from %s\n", kernel_name );
    }
    else {
#ifdef AOUT_KERNEL
	/* try to interprete as a.out kernel */
	if (sread( &kexec, sizeof(kexec) ) != sizeof(kexec))
	    SERROR( "Unable to read exec header from %s\n", kernel_name );
	switch (N_MAGIC(kexec)) {
	  case ZMAGIC:
	    text_offset = N_TXTOFF(kexec);
	    break;
	  case QMAGIC:
	    text_offset = sizeof(kexec);
	    /* the text size includes the exec header; remove this */
	    kexec.a_text -= sizeof(kexec);
	    break;
	  default:
	    SERROR( "Wrong magic number %lo in kernel header\n",
		    N_MAGIC(kexec) );
	}
	elf_kernel = 0;
#else
	SERROR( "Kernel image is no ELF executable\n" );
#endif
    }

    /* Load the kernel one page after start of mem */
    start_mem += PAGE_SIZE;
    mem_size -= PAGE_SIZE;
    /* Align bss size to multiple of four */
    if (!elf_kernel)
      kexec.a_bss = (kexec.a_bss + 3) & ~3;

    /* calculate the total required amount of memory */
    if (elf_kernel) {
	u_long min_addr = 0xffffffff, max_addr = 0;
	for (i = 0; i < kexec_elf.e_phnum; i++) {
	    if (min_addr > kernel_phdrs[i].p_vaddr)
	      min_addr = kernel_phdrs[i].p_vaddr;
	    if (max_addr < kernel_phdrs[i].p_vaddr + kernel_phdrs[i].p_memsz)
	      max_addr = kernel_phdrs[i].p_vaddr + kernel_phdrs[i].p_memsz;
	}
	/* This is needed for newer linkers that include the header in
	   the first segment.  */
	if (min_addr == 0) {
	    min_addr = PAGE_SIZE;
	    kernel_phdrs[0].p_vaddr += PAGE_SIZE;
	    kernel_phdrs[0].p_offset += PAGE_SIZE;
	    kernel_phdrs[0].p_filesz -= PAGE_SIZE;
	    kernel_phdrs[0].p_memsz -= PAGE_SIZE;
	}
	kernel_size = max_addr - min_addr;
    }
#ifdef AOUT_KERNEL
    else
	kernel_size = kexec.a_text + kexec.a_data + kexec.a_bss;
#endif

    /* Locate ramdisk in dest. memory */
    if (rd_size) {
	if (rd_size + kernel_size > mem_size - MB/2 && bi.num_memory > 1)
	    /* If running low on ST ram load ramdisk into alternate ram.  */
	    bi.ramdisk.addr = (u_long) bi.memory[1].addr + bi.memory[1].size -
			      rd_size;
	else
	    /* Else hopefully there is enough ST ram. */
	    bi.ramdisk.addr = (u_long)start_mem + mem_size - rd_size;
    }
    
    /* create the bootinfo structure */
    if (!create_bootinfo())
	SERROR( "Couldn't create bootinfo\n" );

    memreq = kernel_size + bi_size;
#ifdef BOOTINFO_COMPAT_1_0
    if (sizeof(compat_bootinfo) > bi_size)
	memreq = kernel_size+sizeof(compat_bootinfo);
#endif /* BOOTINFO_COMPAT_1_0 */
    /* align load address of ramdisk image, read() is sloooow on odd addr. */
    memreq = ((memreq + 3) & ~3) + rd_size;
    
    /* allocate RAM for the kernel */
    if (!(memptr = malloc( memreq )))
	SERROR( "Unable to allocate memory for kernel\n" );
    /* Second part of the AB40 no-FastRAM test */
    if (bi.mch_type == ATARI_MACH_AB40 && ((unsigned long)memptr & 0xff000000))
	SERROR( "Error: Bootstrap may not allocate memory from FastRAM "
		"on Afterburner040\n" );
    if (getcookie("_CT2", NULL) != -1 &&
	((unsigned long)memptr & 0xff000000))
	SERROR( "Error: Bootstrap may not allocate memory from TT-RAM "
		"on Centurbo2\n" );

    /* clearing the kernel's memory perhaps avoids "uninitialized bss"
     * types of bugs... */
    memset(memptr, 0, memreq - rd_size);

    /* if we have a ramdisk, move it above the kernel code; the moving scheme
     * at boot time requires the ramdisk to be physically above the kernel! */
    if (rd_size) {
	char **srcp = rdptr, *dst = memptr + memreq - rd_size;
	u_long left = rd_size;

	/* keep the non-constant-length part out of this loop, the memcpy can
	 * be better optimized then */
	for( ; left > RD_CHUNK_SIZE;
	     ++srcp, dst += RD_CHUNK_SIZE, left -= RD_CHUNK_SIZE ) {
	    memcpy( dst, *srcp, RD_CHUNK_SIZE );
	    free( *srcp );
	}
	if (left) {
	    memcpy( dst, *srcp, left );
	    free( *srcp );
	}
	free( rdptr );
    }
    
    /* read the text and data segments from the kernel image */
    if (elf_kernel) {
	for (i = 0; i < kexec_elf.e_phnum; i++) {
	    if (sseek( kernel_phdrs[i].p_offset, SEEK_SET) == -1)
		SERROR( "Failed to seek to segment %d\n", i );
	    if (sread( memptr + kernel_phdrs[i].p_vaddr - PAGE_SIZE,
		       kernel_phdrs[i].p_filesz )
		!= kernel_phdrs[i].p_filesz)
		SERROR( "Failed to read segment %d\n", i );
	}
    }
#ifdef AOUT_KERNEL
    else {
	if (sseek( text_offset, SEEK_SET) == -1)
	    SERROR( "Failed to seek to text segment\n" );
	if (sread( memptr, kexec.a_text) != kexec.a_text)
	    SERROR( "Failed to read text segment\n" );
	/* data follows immediately after text */
	if (sread( memptr + kexec.a_text, kexec.a_data) != kexec.a_data)
	    SERROR( "Failed to read data segment\n" );
    }
#endif
    sclose();

    /* Check kernel's bootinfo version */
    switch (check_bootinfo_version(memptr)) {
      case BI_VERSION_MAJOR(ATARI_BOOTI_VERSION):
	bi_ptr = &bi_union.record;
	break;

#ifdef BOOTINFO_COMPAT_1_0
      case BI_VERSION_MAJOR(COMPAT_ATARI_BOOTI_VERSION):
	if (!create_compat_bootinfo())
	    ERROR( "Couldn't create compat bootinfo\n" );
	bi_ptr = &compat_bootinfo;
	bi_size = sizeof(compat_bootinfo);
	break;
#endif /* BOOTINFO_COMPAT_1_0 */

      default:
	ERROR( "Kernel has unsupported bootinfo version\n" );
    }

    /* copy the boot_info struct to the end of the kernel image */
    memcpy( memptr + kernel_size, bi_ptr, bi_size );

    /* for those who want to debug */
    if (debugflag) {
	if (rd_size) {
	    printf ("ramdisk src at %#lx, size is %ld\n",
		    (u_long)memptr - rd_size, bi.ramdisk.size);
	    printf ("ramdisk dest is %#lx ... %#lx\n",
		    bi.ramdisk.addr, bi.ramdisk.addr + rd_size - 1 );
	}

	if (elf_kernel) {
	    for (i = 0; i < kexec_elf.e_phnum; i++) {
		printf ("Kernel segment %d at %#lx, size %d\n", i,
			start_mem + kernel_phdrs[i].p_vaddr - PAGE_SIZE,
			kernel_phdrs[i].p_memsz);
	    }
	}
#ifdef AOUT_KERNEL
	else {
	    printf ("\nKernel text at %#lx, code size %d\n",
		    start_mem, kexec.a_text);
	    printf ("Kernel data at %#lx, data size %d\n",
		    start_mem + kexec.a_text, kexec.a_data );
	    printf ("Kernel bss  at %#lx, bss  size %d\n",
		    start_mem + kexec.a_text + kexec.a_data, kexec.a_bss );
	}
#endif
	printf ("boot_info is at %#lx\n",
		start_mem + kernel_size);

	printf ("\nType a key to continue the Linux boot...");
	fflush (stdout);
	getchar();
    }

    /* now here's the point of no return... */
    printf("Booting Linux...\n");
    sync ();

    /* turn off interrupts... */
    disable_interrupts();

    /* turn off caches... */
    disable_cache();

    /* ..and any MMU translation */
    disable_mmu();

    /* ++guenther: allow reset if launched with MiNT */
    *(long*)0x426 = 0;

    /* copy mover code to a safe place if needed */
    memcpy ((void *) 0x400, &copyall, &copyallend - &copyall);

    /* setup stack */
    change_stack ((void *) PAGE_SIZE);

    /*
     * To avoid that the kernel or the ramdisk overwrite the code moving them
     * to there, we first copy the mover code to a safe place.
     * Then this program jumps to the mover code. After the mover code
     * has finished it jumps to the start of the kernel in its new position.
     * I thought the memory just after the interrupt vector table was a safe
     * place because it is used by TOS to store some system variables.
     * This range goes from 0x400 to approx. 0x5B0.
     * This is more than enough for the miniscule mover routine (16 bytes).
     *
     * Some care is also needed to bring both the kernel and the ramdisk into
     * their final resting position without corrupting them (overwriting some
     * data while copying in source or destination areas). It's solved as
     * follows: The source area is a load region, where we have the kernel
     * code and ramdisk data in memory currently. It is important that the
     * ramdisk is above the kernel. The mover then copies the kernel (upwards)
     * to start of memory, and the ramdisk (downwards) to the end of RAM. This
     * ensures none of the copy actions can overwrite the load region. (E.g.,
     * the kernel can't overwrite the ramdisk source, because that is above
     * the kernel source.) If the load region or the ramdisk destination are
     * in another memory block, we don't have any problem anyway.
     */

    jump_to_mover((char *) start_mem, memptr,
		  (char *) bi.ramdisk.addr + rd_size, memptr + memreq,
		  kernel_size + bi_size, rd_size,
		  (void *) 0x400);

    for (;;);
    /* NOTREACHED */
}

static void get_cpu_infos( void )
{
    u_long cpu_type;

    switch( cpu_type = test_cpu_type() ) {
      case  0: ERROR( "Machine type currently not supported. Aborting..." );
      case 20: bi.cputype = CPU_68020; bi.mmutype = MMU_68851; break;
      case 30: bi.cputype = CPU_68030; bi.mmutype = MMU_68030; break;
      case 40: bi.cputype = CPU_68040; bi.mmutype = MMU_68040; break;
      case 60: bi.cputype = CPU_68060; bi.mmutype = MMU_68060; break;
      default:
	ERROR( "Error: Unknown CPU type. Aborting...\n" );
    }
    printf("CPU: %ld; ", cpu_type + 68000);

    printf("FPU: ");
    switch( cpu_type ) {
      case 40: bi.fputype = FPU_68040; puts( "68040" ); break;
      case 60: bi.fputype = FPU_68060; puts( "68060" ); break;
      default:
	if (test_software_fpu()) {
	    puts( "none or software emulation" );
	}
	else {
	    if (fpu_idle_frame_size () != 0x18) {
		bi.fputype = FPU_68882;
	    	puts("68882");
	    }
	    else {
		bi.fputype = FPU_68881;
		puts("68881");
	    }
	}
    }
}

/* Test for Medusa/Hades/Afterburner: These are the machine on which address 0
 * is writeable.
 * Further distinction is made by readability of 0x00ff82fe, which gives a bus
 * error on the Falcon, but not on Medusa/Hades.
 * Medusa and Hades are separated by reading 0xb0000000, a PCI address that
 * exists only on Hades (buserr on Medusa).
 * Return values of the asm are:
 *  0 = standard machine (0x0 not writeable)
 *  1 = failed 0x00ff82fe test -> Afterburner
 *  2 = failed 0xb0000000 test -> Medusa
 *  3 = all tests passed -> Hades
 */

static void get_mch_type( void )
{
    u_long mch_cookie;
    int rv;

    /* Pass contents of the _MCH cookie to the kernel */
    getcookie("_MCH", &mch_cookie);
    bi.mch_cookie = mch_cookie;

    bi.mch_type = ATARI_MACH_NORMAL;
    if (getcookie("_CT2", NULL) != -1)
	/* On the Centurbo2 board, no memory tests will give bus errors! So
	 * don't even execute the test code below, but proceed */
	goto finish;
	
    __asm__ __volatile__
	( "movel	0x8,a0\n\t"	/* save buserr vector */
	  "movel	sp,a1\n\t" 	/* save stack pointer */
	  "movew	sr,d2\n\t"	/* save sr */
	  "orw		#0x700,sr\n\t"	/* disable interrupts */
	  "moveb	0x0,d1\n\t"	/* save old value of 0x0 */
	  "movel	#1f,0x8\n\t"	/* setup new buserr vector */
	  "moveq	#0,%0\n\t"	/* assume no Medusa */
	  "clrb		0x0\n\t"	/* try to write to 0x0 */
	  "nop		\n\t"		/* clear insn pipe */
	  "moveq	#1,%0\n\t"	/* if come here, 0x0 is writeable */
	  "moveb	d1,0x0\n\t"	/* write back saved value */
	  "nop		\n\t"
	  "tstb		0x00ff82fe\n\t"	/* Medusa asserts DTACK here (so no
					 * buserr), but Falcon with AB40 not */
	  "nop		\n\t"
	  "moveq	#2,%0\n\t"	/* if come here, it's a Medusa or
					 * Hades */
	  "nop		\n\t"
	  "tstb		0xb0000000\n\t"	/* PCI address on Hades */
	  "nop		\n\t"
	  "moveq	#3,%0\n"	/* if PCI exists, then no Medusa */
	  "1:\t"
	  "movel	a1,sp\n\t"	/* restore stack pointer */
	  "movel	a0,0x8\n\t"	/* restore buserr vector */
	  "movew	d2,sr"		/* reenable ints */
	  : "=d" (rv)
	  : /* no inputs */
	  : "d1", "d2", "a0", "a1", "memory" );

    switch( rv ) {
      case 1:
	bi.mch_type = ATARI_MACH_AB40;
	break;
      case 2:
	bi.mch_type = ATARI_MACH_MEDUSA;
	break;
      case 3:
	bi.mch_type = ATARI_MACH_HADES;
	break;
    }

    /* Unfortunately, 0x0 isn't writeable on all Afterburners (yes, it seems
     * so...), so the test above for AB40 may fail. But it surely is an AB40
     * if an "AB40" cookie exists, or with 99% if it's a Falcon and has a '040
     * processor. */
    if (bi.mch_type == ATARI_MACH_NORMAL) {
	if (getcookie("AB40", NULL) != -1 ||
	    ((bi.mch_cookie >> 16) == ATARI_MCH_FALCON &&
	     bi.cputype == CPU_68040))
	    bi.mch_type = ATARI_MACH_AB40;
    }

  finish:
    printf( "Model: " );
    switch( bi.mch_cookie >> 16 ) {
      case ATARI_MCH_ST:
	puts( "ST" );
	break;
      case ATARI_MCH_STE:
	if (bi.mch_cookie & 0xffff)
	    puts( "Mega STE" );
	else
	    puts( "STE" );
	break;
      case ATARI_MCH_TT:
	/* Medusa and Hades have TT _MCH cookie */
	if (bi.mch_type == ATARI_MACH_MEDUSA)
	    puts( "Medusa" );
	else if (bi.mch_type == ATARI_MACH_HADES)
	    puts( "Hades" );
	else
	    puts( "TT" );
	break;
      case ATARI_MCH_FALCON:
	printf( "Falcon" );
	if (bi.mch_type == ATARI_MACH_AB40)
	    printf( " (with Afterburner040)" );
	printf( "\n" );
	break;
      default:
	printf( "unknown mach cookie 0x%lx", bi.mch_cookie );
	break;
    }
}

#define GRANULARITY (256*1024) /* min unit for memory */
#define ADD_CHUNK(start,siz,force,kindstr)			\
    do {							\
	unsigned long _start = (start);				\
	unsigned long _size  = (siz) & ~(GRANULARITY-1);	\
	int _force = (force);					\
								\
	if (_force >= 0) _size = _force;			\
	if (_size > 0) {					\
	    bi.memory[chunk].addr = _start;			\
	    bi.memory[chunk].size = _size;			\
	    total += _size;					\
	    printf( kindstr ": %s MB at 0x%08lx\n",		\
		    format_mb(_size), _start );			\
	    chunk++;						\
	}							\
    } while(0)

/* Get the amounts of ST- and TT-RAM. */
static void get_mem_infos( void )
{
    int chunk = 0;
    unsigned long total = 0;

    if (force_st_size >= 0 && force_st_size < 256*1024) {
	force_st_size = 256*1024;
	printf( "Need at least 256k ST-RAM! Changing -S to 256k.\n" );
    }

    if (bi.mch_type == ATARI_MACH_MEDUSA) {

	/* For the Medusa, some things are different... */
	
        unsigned long bank1, bank2, medusa_st_ram;
	int fake_force;

        get_medusa_bank_sizes( &bank1, &bank2 );
	medusa_st_ram = *phystop & ~(MB - 1);
        bank1 -= medusa_st_ram;

	/* For the Medusa, load_to_stram is ignored, the kernel is always at
	 * 0; both kinds of RAM are the same and equally fast */
	if (load_to_stram)
	    printf( "(Note: -s ignored on Medusa)\n" );

	ADD_CHUNK( 0, medusa_st_ram,
		   force_st_size, "Medusa pseudo ST-RAM from bank 1" );

	fake_force = force_tt_size < 0 ? -1 :
		     force_tt_size <= bank1 ? force_tt_size : bank1;
        if (!ignore_ttram && bank1 > 0)
	    ADD_CHUNK( 0x20000000 + medusa_st_ram, bank1,
		       fake_force, "TT-RAM bank 1" );
	fake_force = force_tt_size < 0 ? -1 :
		     force_tt_size <= bank1 ? 0 : force_tt_size - bank1;
        if (!ignore_ttram && bank2 > 0)
	    ADD_CHUNK( 0x24000000, bank2,
		       fake_force, "TT-RAM bank 2" );
			
        bi.num_memory = chunk;
    }
    else if (bi.mch_type == ATARI_MACH_AB40) {

	/* Assume that only Falcon with a '040 is Afterburner040 */

        struct {
	    unsigned long start, size;
	} banks[2];
	int n_banks;

        n_banks = get_ab040_bank_sizes( 2, (unsigned long *)banks );
	
        if (!ignore_ttram && n_banks >= 1)
	    ADD_CHUNK( banks[0].start, banks[0].size,
		       force_tt_size, "FastRAM bank 1" );
        if (!ignore_ttram && n_banks >= 2 && force_tt_size < 0)
	    ADD_CHUNK( banks[1].start, banks[1].size,
		       force_tt_size, "FastRAM bank 2" );

    }
    else {

	/* TT RAM tests for standard machines */
	struct {
		unsigned short version; /* version - currently 1 */
		unsigned long fr_start; /* start addr FastRAM */
		unsigned long fr_len;   /* length FastRAM */
	} *magn_cookie;
	struct {
		unsigned long version;
		unsigned long fr_start; /* start addr */
		unsigned long fr_len;   /* length */
	} *fx_cookie;

        if (!ignore_ttram) {
	    /* all these kinds of TT-RAM are mutually exclusive */

	    /* "Original" or properly emulated TT-Ram */
	    if (*ramtop) {
		/* the 'ramtop' variable at 0x05a4 is not
		 * officially documented. We use it anyway
		 * because it is the only way to get the TTram size.
		 * (It is zero if there is no TTram.)
		 */
		if (getcookie("_CT2", NULL) != -1)
		    /* TT-RAM starts at phys. 0x04000000 on Centurbo2 but is
		     * remapped for TOS to 0x01000000 with the MMU */
		    ADD_CHUNK( CT2_FAST_START, *ramtop - TT_RAM_BASE,
			       force_tt_size, "TT-RAM" );
		else
		    ADD_CHUNK( TT_RAM_BASE, *ramtop - TT_RAM_BASE,
			       force_tt_size, "TT-RAM" );
	    }
	    /* test for MAGNUM alternate RAM
	     * added 26.9.1995 M. Schwingen, rincewind@discworld.oche.de
	     */
	    else if (getcookie("MAGN", (u_long *)&magn_cookie) != -1)
		ADD_CHUNK( magn_cookie->fr_start,
			   magn_cookie->fr_len,
			   force_tt_size, "MAGNUM alternate RAM" );
	    /* BlowUps FX */
	    else if (getcookie("BPFX", (u_long *)&fx_cookie) != -1 &&
		     fx_cookie)
		/* if fx is set (cookie call above),
		 * we assume that BlowUps FX-card
		 * is installed. (Nat!)
		 */
		ADD_CHUNK( fx_cookie->fr_start,
			   fx_cookie->fr_len,
			   force_tt_size, "FX alternate RAM" );

	}
    }

    if (bi.mch_type != ATARI_MACH_MEDUSA) {
	/* add ST-RAM */
	ADD_CHUNK( 0, *phystop,
		   force_st_size, "ST-RAM" );
        bi.num_memory = chunk;

	/* If the user wants the kernel to reside in ST-RAM, put the ST-RAM
	 * block first in the list of mem blocks; the kernel is always located
	 * in the first block. */
	if (load_to_stram && chunk > 1) {
	    struct mem_info temp = bi.memory[chunk - 1];
	    bi.memory[chunk - 1] = bi.memory[0];
	    bi.memory[0] = temp;
	}
    }

    if (extramem_start && extramem_size) {
	ADD_CHUNK( extramem_start, extramem_size,
		   -1, "User-specified alternate RAM" );
    }
    
    /* verify that there is enough RAM; ST- and TT-RAM combined */
    if (total < MIN_RAMSIZE)
	ERROR( "Not enough RAM (need %d MB). Aborting...", MIN_RAMSIZE/MB );
    printf( "Total %s MB\n", format_mb(total) );
}

static char *format_mb( unsigned long size )
{
    static char buf[12];

    sprintf( buf, "%ld", size/MB );
    if (size % MB) {
	size /= 1024;
	sprintf( buf+strlen(buf), ".%02ld", (size*100+1024/2)/1024 );
    }
    return( buf );
}


/* getcookie -- function to get the value of the given cookie. */
static int getcookie(char *cookie, u_long *value)
{
    int i = 0;

    while(cookiejar[i] != 0L) {
	if(cookiejar[i] == *(u_long *)cookie) {
	    if (value)
		*value = cookiejar[i + 1];
	    return 1;
	}
	i += 2;
    }
    return -1;
}

static int check_bootinfo_version(char *memptr)
{
    struct bootversion *bv = (struct bootversion *)memptr;
    unsigned long version = 0;
    int i, kernel_major, kernel_minor, boots_major, boots_minor;

    printf( "\n" );
    if (bv->magic == BOOTINFOV_MAGIC) {
	for( i = 0; bv->machversions[i].machtype != 0; ++i ) {
	    if (bv->machversions[i].machtype == MACH_ATARI) {
		version = bv->machversions[i].version;
		break;
	    }
	}
    }
    if (!version)
	printf("Kernel has no bootinfo version info, assuming 0.0\n");

    kernel_major = BI_VERSION_MAJOR(version);
    kernel_minor = BI_VERSION_MINOR(version);
    boots_major  = BI_VERSION_MAJOR(ATARI_BOOTI_VERSION);
    boots_minor  = BI_VERSION_MINOR(ATARI_BOOTI_VERSION);
    printf("Bootstrap's bootinfo version: %d.%d\n",
	   boots_major, boots_minor);
    printf("Kernel's bootinfo version   : %d.%d\n",
	   kernel_major, kernel_minor);
    
    switch (kernel_major) {
	case BI_VERSION_MAJOR(ATARI_BOOTI_VERSION):
	    if (kernel_minor > boots_minor) {
		printf("Warning: Bootinfo version of bootstrap and kernel "
		       "differ!\n");
		printf("         Certain features may not work.\n");
	    }
	    break;

#ifdef BOOTINFO_COMPAT_1_0
	case BI_VERSION_MAJOR(COMPAT_ATARI_BOOTI_VERSION):
	    printf("(using backwards compatibility mode)\n");
	    break;
#endif /* BOOTINFO_COMPAT_1_0 */

	default:
	    printf("\nThis bootstrap is too %s for this kernel!\n",
		   boots_major < kernel_major ? "old" : "new");
	    return 0;
    }
    return kernel_major;
}

    /*
     *  Create the Bootinfo Structure
     */

static int create_bootinfo(void)
{
    int i;
    struct bi_record *record;

    /* Initialization */
    bi_size = 0;

    /* Generic tags */
    if (!add_bi_record(BI_MACHTYPE, sizeof(bi.machtype), &bi.machtype))
	return(0);
    if (!add_bi_record(BI_CPUTYPE, sizeof(bi.cputype), &bi.cputype))
	return(0);
    if (!add_bi_record(BI_FPUTYPE, sizeof(bi.fputype), &bi.fputype))
	return(0);
    if (!add_bi_record(BI_MMUTYPE, sizeof(bi.mmutype), &bi.mmutype))
	return(0);
    for (i = 0; i < bi.num_memory; i++)
	if (!add_bi_record(BI_MEMCHUNK, sizeof(bi.memory[i]), &bi.memory[i]))
	    return(0);
    if (bi.ramdisk.size)
	if (!add_bi_record(BI_RAMDISK, sizeof(bi.ramdisk), &bi.ramdisk))
	    return(0);
    if (!add_bi_string(BI_COMMAND_LINE, bi.command_line))
	return(0);

    /* Atari tags */
    if (!add_bi_record(BI_ATARI_MCH_COOKIE, sizeof(bi.mch_cookie),
		       &bi.mch_cookie))
	return(0);
    if (!add_bi_record(BI_ATARI_MCH_TYPE, sizeof(bi.mch_type),
		       &bi.mch_type))
	return(0);

    /* Trailer */
    record = (struct bi_record *)((u_long)&bi_union.record+bi_size);
    record->tag = BI_LAST;
    bi_size += sizeof(bi_union.record.tag);

    return(1);
}

    /*
     *  Add a Record to the Bootinfo Structure
     */

static int add_bi_record(u_short tag, u_short size, const void *data)
{
    struct bi_record *record;
    u_short size2;

    size2 = (sizeof(struct bi_record)+size+3)&-4;
    if (bi_size+size2+sizeof(bi_union.record.tag) > MAX_BI_SIZE) {
	fprintf (stderr, "Can't add bootinfo record. Ask a wizard to enlarge me.\n");
	return(0);
    }
    record = (struct bi_record *)((u_long)&bi_union.record+bi_size);
    record->tag = tag;
    record->size = size2;
    memcpy(record->data, data, size);
    bi_size += size2;
    return(1);
}

    /*
     *  Add a String Record to the Bootinfo Structure
     */

static int add_bi_string(u_short tag, const u_char *s)
{
    return add_bi_record(tag, strlen(s)+1, (void *)s);
}


#ifdef BOOTINFO_COMPAT_1_0

    /*
     *  Create the Bootinfo structure for backwards compatibility mode
     */

static int create_compat_bootinfo(void)
{
    u_int i;

    compat_bootinfo.machtype = bi.machtype;
    if (bi.cputype & CPU_68020)
	compat_bootinfo.cputype = COMPAT_CPU_68020;
    else if (bi.cputype & CPU_68030)
	compat_bootinfo.cputype = COMPAT_CPU_68030;
    else if (bi.cputype & CPU_68040)
	compat_bootinfo.cputype = COMPAT_CPU_68040;
    else if (bi.cputype & CPU_68060)
	compat_bootinfo.cputype = COMPAT_CPU_68060;
    else {
	printf("CPU type 0x%08lx not supported by kernel\n", bi.cputype);
	return(0);
    }
    if (bi.fputype & FPU_68881)
	compat_bootinfo.cputype |= COMPAT_FPU_68881;
    else if (bi.fputype & FPU_68882)
	compat_bootinfo.cputype |= COMPAT_FPU_68882;
    else if (bi.fputype & FPU_68040)
	compat_bootinfo.cputype |= COMPAT_FPU_68040;
    else if (bi.fputype & FPU_68060)
	compat_bootinfo.cputype |= COMPAT_FPU_68060;
    else if (bi.fputype) {
	printf("FPU type 0x%08lx not supported by kernel\n", bi.fputype);
	return(0);
    }
    compat_bootinfo.num_memory = bi.num_memory;
    if (compat_bootinfo.num_memory > COMPAT_NUM_MEMINFO) {
	printf("Warning: using only %d blocks of memory\n",
	       COMPAT_NUM_MEMINFO);
	compat_bootinfo.num_memory = COMPAT_NUM_MEMINFO;
    }
    for (i = 0; i < compat_bootinfo.num_memory; i++) {
	compat_bootinfo.memory[i].addr = bi.memory[i].addr;
	compat_bootinfo.memory[i].size = bi.memory[i].size;
    }
    if (bi.ramdisk.size) {
	bi.ramdisk.addr &= 0xfffffc00;
	compat_bootinfo.ramdisk_size = (bi.ramdisk.size+1023)/1024;
	compat_bootinfo.ramdisk_addr = bi.ramdisk.addr;
    } else {
	compat_bootinfo.ramdisk_size = 0;
	compat_bootinfo.ramdisk_addr = 0;
    }
    strncpy(compat_bootinfo.command_line, bi.command_line, COMPAT_CL_SIZE);
    compat_bootinfo.command_line[COMPAT_CL_SIZE-1] = '\0';

    compat_bootinfo.bi_atari.hw_present = 0;
    compat_bootinfo.bi_atari.mch_cookie = bi.mch_cookie;
    return(1);
}
#endif /* BOOTINFO_COMPAT_1_0 */

/*
 * Copy the kernel and the ramdisk to their final resting places.
 *
 * I assume that the kernel data and the ramdisk reside somewhere
 * in the middle of the memory.
 *
 * This program itself should be somewhere in the first 4096 bytes of memory
 * where the kernel never will be. In this way it can never be overwritten
 * by itself.
 *
 * At this point the registers have:
 * a0: the start of the final kernel
 * a1: the start of the current kernel
 * a2: the end of the final ramdisk
 * a3: the end of the current ramdisk
 * d0: the kernel size
 * d1: the ramdisk size 
 */
asm ("
.text
_copyall:

	movel	a0,a4	    	/* save the start of the kernel for booting */

1:	movel	a1@+,a0@+   	/* copy the kernel starting at the beginning */
	subql	#4,d0
	jcc	1b

	tstl	d1
	beq		3f

2:	movel	a3@-,a2@-   	/* copy the ramdisk starting at the end */
	subql	#4,d1
	jcc	2b

3:	jmp	a4@ 	    	/* jump to the start of the kernel */
_copyallend:
");


static int test_cpu_type (void)
{
    int rv;
    static char testarray[4] = { 0, 0, 1, 1 };
    
    __asm__ __volatile__ (
	"movel	0x2c,a0\n\t"		/* save line F vector */
	"movel	sp,a1\n\t"		/* save stack pointer */
	"movew	sr,d2\n\t"		/* save sr */
	"orw	#0x700,sr\n\t"		/* disable interrupts */
	"moveq	#0,%0\n\t"		/* assume 68000 (or 010) */

	"moveq	#1,d1\n\t"
	"tstb	%1@(d1:l:2)\n\t"	/* pre-020 CPU ignores scale and reads
					 * 0, otherwise 1 */
	"beq	1f\n\t"			/* if 0 is 68000, end */
	"moveq	#20,%0\n\t"		/* now assume 68020 */

	"movel	#2f,0x2c\n\t"	 	/* continue if trap */
	"movel	%1,a2\n\t"
	"nop	\n\t"
	".long	0xf0120a00\n\t"		/* pmove tt0,a2@, tt0 only on '030 */
	"nop	\n\t"
	"moveq	#30,%0\n\t"		/* surely is 68030 */
	"bra	1f\n"
	"2:\t"
					/* now could be '020 or '040+ */
	"movel	#1f,0x2c\n\t"	 	/* end if trap */
	"movel	%1,a2\n\t"
	"nop	\n\t"
	".long	0xf622a000\n\t"		/* move16 a2@+,a2@+ only on '040+ */
	"nop	\n\t"
	"moveq	#40,%0\n\t"		/* assume 68040 */

	"nop	\n\t"
	".word	0xf5ca\n\t"		/* plpar a2@, only on '060 */
	"nop	\n\t"
	"moveq	#60,%0\n"		/* surely is 68060 */

	"1:\t"
	"movel	a1,sp\n\t"		/* restore stack */
	"movel	a0,0x2c\n\t"		/* restore line F vector */
	"movew	d2,sr"			/* reenable ints */
	: "=&d" (rv)
	: "a" (testarray)
	: "d1", "d2", "a0", "a1", "a2", "memory" );
    return( rv );
}


/* Test if FPU instructions are executed in hardware, or if they're
   emulated in software.  For this, the F-line vector is temporarily
   replaced. */

static int test_software_fpu(void)
{
    int rv;
    
    __asm__ __volatile__
	( "movel	0x2c,a0\n\t"	/* save line F vector */
	  "movel	sp,a1\n\t"	/* save stack pointer */
	  "movew	sr,d2\n\t"	/* save sr */
	  "orw		#0x700,sr\n\t"	/* disable interrupts */
	  "movel	#1f,0x2c\n\t" /* new line F vector */
	  "moveq	#1,%0\n\t"	/* assume no FPU */
	  "fnop 	\n\t"		/* exec one FPU insn */
	  "nop		\n\t"
	  "moveq	#0,%0\n"	/* if come here, is a hard FPU */
	  "1:\t"
	  "movel	a1,sp\n\t"	/* restore stack */
	  "movel	a0,0x2c\n\t"	/* restore line F vector */
	  "movew	d2,sr"		/* reenable ints */
	  : "=d" (rv)
	  : /* no inputs */
	  : "d2", "a0", "a1" );
    return rv;
}


static void get_medusa_bank_sizes( u_long *bank1, u_long *bank2 )
{
    static u_long save_addr;
    u_long test_base, save_dtt0, saved_contents[16];
#define	TESTADDR(i)	(*((u_long *)((char *)test_base + i*8*MB)))
#define	TESTPAT		0x12345678
    unsigned short oldflags;
    int i;

    /* This ensures at least that none of the test addresses conflicts
     * with the test code itself; assume that MMU programming is modulo 8MB. */
    test_base = ((unsigned long)&save_addr & 0x007fffff) | 0x20000000;
    *bank1 = *bank2 = 0;
	
    /* Interrupts must be disabled because arbitrary addresses may be
     * temporarily overwritten, even code of an interrupt handler */
    __asm__ __volatile__ ( "movew sr,%0; oriw #0x700,sr" : "=g" (oldflags) : );
    disable_cache();
    /* make transparent translation for 0x2xxxxxxx area, enabled for
     * super+user, non-cacheable/serialized */
    __asm__ __volatile__ (
	".chip 68040\n\t"
	"movec	%/dtt0,%0\n\t"
	"movec	%1,%/dtt0\n\t"
	"nop\n\t"
	".chip 68k"
	: "=&d" (save_dtt0)
	: "d" (0x200fe040) );
	
    /* save contents of the test addresses */
    for( i = 0; i < 16; ++i )
	saved_contents[i] = TESTADDR(i);
	
    /* write 0s into all test addresses */
    for( i = 0; i < 16; ++i )
	TESTADDR(i) = 0;

    /* test for bank 1 */
#if 0
    /* This is Freddi's original test, but it didn't work. */
    TESTADDR(0) = TESTADDR(1) = TESTPAT;
    if (TESTADDR(1) == TESTPAT) {
	if (TESTADDR(2) == TESTPAT)
	    *bank1 = 8*MB;
	else if (TESTADDR(3) == TESTPAT)
	    *bank1 = 16*MB;
	else
	    *bank1 = 32*MB;
    }
    else {
	if (TESTADDR(2) == TESTPAT)
	    *bank1 = 0;
	else
	    *bank1 = 16*MB;
    }
#else
    TESTADDR(0) = TESTPAT;
    if (TESTADDR(1) == TESTPAT)
	*bank1 = 8*MB;
    else if (TESTADDR(2) == TESTPAT)
	*bank1 = 16*MB;
    else if (TESTADDR(4) == TESTPAT)
	*bank1 = 32*MB;
    else
	*bank1 = 64*MB;
#endif

    /* test for bank2 */
    if (TESTADDR(8) != 0)
	*bank2 = 0;
    else {
	TESTADDR(8) = TESTPAT;
	if (TESTADDR(9) != 0) {
	    if (TESTADDR(10) == TESTPAT)
		*bank2 = 8*MB;
	    else
		*bank2 = 32*MB;
	}
	else {
	    TESTADDR(9) = TESTPAT;
	    if (TESTADDR(10) == TESTPAT)
		*bank2 = 16*MB;
	    else
		*bank2 = 64*MB;
	}
    }
	
    /* restore contents of the test addresses and restore interrupt mask */
    for( i = 0; i < 16; ++i )
	TESTADDR(i) = saved_contents[i];
    /* remove transparent mapping */
    __asm__ __volatile__ (
	".chip 68040\n\t"
	"movec	%0,%/dtt0\n\t"
	"nop\n\t"
	".chip 68k"
	: /* no outputs */
	: "d" (save_dtt0) );
    __asm__ __volatile__ ( "movew %0,sr" : : "g" (oldflags) );
}

#undef TESTADDR
#undef TESTPAT


#define AB40_FAST_START	0x01000000
#define STEPSIZE	(4*MB)
#define BANKSIZE	(32*MB)
#define BANK_PAGES	(BANKSIZE/STEPSIZE)

static __inline__ unsigned long read_transparent( unsigned long addr )
{
    unsigned long ttreg_val, save_dtt0, val;
    
    ttreg_val = (addr & 0xff000000) | 0xe040;
    	/* 0xe040: enable super+user, non-cacheable/serialized */
    __asm__ __volatile__ (
	".chip 68040\n\t"
	"movec	%/dtt0,%0\n\t"
	"movec	%2,%/dtt0\n\t"
	"nop\n\t"
	"movel	%3@,%1\n\t"
	"nop\n\t"
	"movec	%0,%/dtt0\n\t"
	".chip 68k"
	: "=&d" (save_dtt0), "=d" (val)
	: "d" (ttreg_val), "a" (addr) );
    return( val );
}

static __inline__ void write_transparent( unsigned long addr,
										  unsigned long val )
{
    unsigned long ttreg_val, save_dtt0;
	
    ttreg_val = (addr & 0xff000000) | 0xe040;
    	/* 0xe040: enable super+user, non-cacheable/serialized */
    __asm__ __volatile__ (
	".chip 68040\n\t"
	"movec	%/dtt0,%0\n\t"
	"movec	%1,%/dtt0\n\t"
	"nop\n\t"
	"movel	%3,%2@\n\t"
	"nop\n\t"
	"movec	%0,%/dtt0\n\t"
	".chip 68k"
	: "=&d" (save_dtt0)
	: "d" (ttreg_val), "a" (addr), "d" (val) );
}

static __inline__ void pflusha( void )
{
    __asm__ __volatile__ ( ".chip 68040; nop; pflusha; nop; .chip 68k" );
}

static int get_ab040_bank_sizes( int maxres, u_long *result )
{
    unsigned long addr, addr2, val, npages, start, end;
    unsigned long saved_contents[2*BANK_PAGES];
    unsigned short oldflags;
    int n_result = 0;

    __asm__ __volatile__ ( "movew %/sr,%0; orw #0x700,%/sr"
			   : "=d" (oldflags) );

    pflusha();
    /* write test patterns at addresses */
    for( addr = AB40_FAST_START+2*BANKSIZE, npages = 2*BANK_PAGES;
	 npages; --npages ) {
	addr -= STEPSIZE;
	saved_contents[npages-1] = read_transparent( addr );
	write_transparent( addr, addr );
    }
    pflusha();

    addr = AB40_FAST_START;
    npages = 2*BANK_PAGES;
    while( npages ) {
	val = read_transparent( addr2 = addr );
	--npages;
	addr += STEPSIZE;
	if (val != addr2)
	    continue;
	/* note start addr of real mem (first addr where can read back) */
	start = addr2;
	/* loop while can read back, i.e. memory valid */
	do {
	    val = read_transparent( addr2 = addr );
	    --npages;
	    addr += STEPSIZE;
	    if (val != addr2)
		break;
	} while( npages );
	end = npages ? addr2 : addr;

	if (end - start >= MB) {
	    if (maxres-- > 0) {
		*result++ = start;
		*result++ = end - start;
		++n_result;
	    }
	    else
		npages = 0;
	}
    }

    /* restore previous contents */
    for( addr = AB40_FAST_START+2*BANKSIZE, npages = 2*BANK_PAGES;
	 npages; --npages ) {
	addr -= STEPSIZE;
	write_transparent( addr, saved_contents[npages-1] );
    }
    pflusha();
    
    __asm__ __volatile__ ( "movew %0,%/sr" : : "d" (oldflags) );
    return( n_result );
}

/* Local Variables: */
/* tab-width: 8     */
/* End:             */