Home | History | Annotate | Line # | Download | only in boot32
boot32.c revision 1.2.2.5
      1 /*	$NetBSD: boot32.c,v 1.2.2.5 2003/01/07 20:46:48 thorpej Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2002 Reinoud Zandijk
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. The name of the author may not be used to endorse or promote products
     16  *    derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  *
     29  * Thanks a bunch for Ben's framework for the bootloader and its suporting
     30  * libs. This file tries to actually boot NetBSD/acorn32 !
     31  *
     32  * XXX eventually to be partly merged back with boot26 ? XXX
     33  */
     34 
     35 #include <lib/libsa/stand.h>
     36 #include <lib/libsa/loadfile.h>
     37 #include <lib/libkern/libkern.h>
     38 #include <riscoscalls.h>
     39 #include <srt0.h>
     40 #include <sys/boot_flag.h>
     41 #include <machine/vmparam.h>
     42 #include <arm/arm32/pte.h>
     43 #include <machine/bootconfig.h>
     44 
     45 
     46 /* debugging flags */
     47 int debug = 1;
     48 
     49 
     50 /* constants */
     51 #define PODRAM_START   (512*1024*1024)				/* XXX Kinetic cards XXX	*/
     52 
     53 #define MAX_RELOCPAGES	4096
     54 
     55 #define DEFAULT_ROOT	"/dev/wd0a"
     56 
     57 
     58 #define IO_BLOCKS	 16	/* move these to the bootloader structure? */
     59 #define ROM_BLOCKS	 16
     60 #define PODRAM_BLOCKS	 16
     61 
     62 
     63 /* booter variables */
     64 char	 scrap[80], twirl_cnt;					/* misc				*/
     65 char	 booted_file[80];
     66 
     67 struct bootconfig *bconfig;					/* bootconfig passing		*/
     68 u_long	 bconfig_new_phys;					/* physical address its bound	*/
     69 
     70 u_int	 monitor_type, monitor_sync, ioeb_flags, lcd_flags;	/* computer knowledge		*/
     71 u_int	 superio_flags, superio_flags_basic, superio_flags_extra;
     72 
     73 int	 nbpp, memory_table_size, memory_image_size;		/* sizes			*/
     74 int	 reloc_tablesize, *reloc_instruction_table;		/* relocate info		*/
     75 int	*reloc_pos;						/* current empty entry		*/
     76 int	 reloc_entries;						/* number of relocations	*/
     77 int	 first_mapped_DRAM_page_index;				/* offset in RISC OS blob	*/
     78 int	 first_mapped_PODRAM_page_index;			/* offset in RISC OS blob	*/
     79 
     80 struct page_info *mem_pages_info;				/* {nr, virt, phys}*		*/
     81 struct page_info *free_relocation_page;				/* points to the page_info chain*/
     82 struct page_info *relocate_table_pages;				/* points to seq. relocate info */
     83 struct page_info *relocate_code_page;				/* points to the copied code	*/
     84 struct page_info *bconfig_page;					/* page for passing on settings	*/
     85 
     86 unsigned char *memory_page_types;				/* packed array of 4 bit typeId	*/
     87 
     88 u_long	*initial_page_tables;					/* pagetables to be booted from	*/
     89 
     90 
     91 /* XXX rename *_BLOCKS to MEM_BLOCKS */
     92 /* DRAM/VRAM/ROM/IO info */
     93 u_long	 videomem_start, videomem_pages, display_size;		/* where the display is		*/
     94 
     95 u_long	 pv_offset, top_physdram;				/* kernel_base - phys. diff	*/
     96 u_long	 top_1Mb_dram;						/* the lower mapped top 1Mb	*/
     97 u_long	 new_L1_pages_phys;					/* physical address of L1 pages	*/
     98 
     99 u_long	 total_podram_pages, total_dram_pages, total_vram_pages;/* for bootconfig passing	*/
    100 int	 dram_blocks, podram_blocks;				/* number of mem. objects/type  */
    101 int	 vram_blocks, rom_blocks, io_blocks;
    102 
    103 u_long	 DRAM_addr[DRAM_BLOCKS],     DRAM_pages[DRAM_BLOCKS];
    104 u_long	 PODRAM_addr[PODRAM_BLOCKS], PODRAM_pages[PODRAM_BLOCKS];	/* processor only RAM	*/
    105 u_long	 VRAM_addr[VRAM_BLOCKS],     VRAM_pages[VRAM_BLOCKS];
    106 u_long	 ROM_addr[ROM_BLOCKS],       ROM_pages[ROM_BLOCKS];
    107 u_long	 IO_addr[IO_BLOCKS],         IO_pages[IO_BLOCKS];
    108 
    109 
    110 /* RISC OS memory pages we claimed */
    111 u_long	 firstpage, lastpage, totalpages;			/* RISC OS pagecounters		*/
    112 char	*memory_image, *bottom_memory, *top_memory;		/* RISC OS memory		*/
    113 
    114 u_long	 videomem_start_ro;					/* for debugging mainly		*/
    115 
    116 /* kernel info */
    117 u_long	 marks[MARK_MAX];					/* loader mark pointers 	*/
    118 u_long	 kernel_physical_start;					/* where does it get relocated	*/
    119 u_long	 kernel_free_vm_start;					/* where does the free VM start	*/
    120 u_long	 scratch_virtualbase, scratch_physicalbase;		/* some free space to mess with	*/
    121 
    122 
    123 /* bootprogram identifiers */
    124 extern const char bootprog_rev[];
    125 extern const char bootprog_name[];
    126 extern const char bootprog_date[];
    127 extern const char bootprog_maker[];
    128 
    129 
    130 /* predefines / prototypes */
    131 void	 init_datastructures(void);
    132 void	 get_memory_configuration(void);
    133 void	 get_memory_map(void);
    134 void	 create_initial_page_tables(void);
    135 void	 add_pagetables_at_top(void);
    136 void	 sort_memory_map(void);
    137 void	 add_initvectors(void);
    138 void	 create_configuration(int argc, char **argv, int start_args);
    139 void	 prepare_and_check_relocation_system(void);
    140 void	 twirl(void);
    141 int	 vdu_var(int);
    142 void	 process_args(int argc, char **argv, int *howto, char *file, int *start_args);
    143 
    144 char		 *sprint0(int width, char prefix, char base, int value);
    145 struct page_info *get_relocated_page(u_long destination, int size);
    146 
    147 extern void start_kernel(
    148 		int relocate_code_page,
    149 		int relocation_pv_offset,
    150 		int configuration_structure_in_flat_physical_space,
    151 		int physical_address_of_relocation_tables,
    152 		int physical_address_of_new_L1_pages,
    153 		int kernel_entry_point
    154 		);	/* asm */
    155 
    156 
    157 /* the loader itself */
    158 void init_datastructures(void) {
    159 	/* Get number of pages and the memorytablesize */
    160 	osmemory_read_arrangement_table_size(&memory_table_size, &nbpp);
    161 
    162 	/* reserve some space for heap etc... 512 might be bigish though */
    163 	memory_image_size = (int) HIMEM - 512*1024;
    164 	if (memory_image_size <= 256*1024) panic("I need more memory to boot up; increase Wimp slot");
    165 	memory_image = alloc(memory_image_size);
    166 	if (!memory_image) panic("Can't alloc get my memory image ?");
    167 
    168 	bottom_memory = memory_image;
    169 	top_memory    = memory_image + memory_image_size;
    170 
    171 	firstpage  = ((int) bottom_memory / nbpp) + 1;	/* safety */
    172 	lastpage   = ((int) top_memory    / nbpp) - 1;
    173 	totalpages = lastpage - firstpage;
    174 
    175 	printf("Got %ld memory pages each %d kilobytes to mess with.\n\n", totalpages, nbpp>>10);
    176 
    177 	/* allocate some space for the relocation table */
    178 	reloc_tablesize = (MAX_RELOCPAGES+1)*3*sizeof(int);	/* 3 entry table */
    179 	reloc_instruction_table = alloc(reloc_tablesize);
    180 	if (!reloc_instruction_table) panic("Can't alloc my relocate instructions pages");
    181 
    182 	/* set up relocation table. First word gives number of relocations to be done */
    183 	reloc_entries = 0;
    184 	reloc_pos     = reloc_instruction_table;
    185 	*reloc_pos++  = 0;
    186 
    187 	/* set up the memory translation info structure; alloc one more for	*
    188 	 * end of list marker; see get_memory_map				*/
    189 	mem_pages_info = alloc((totalpages + 1)*sizeof(struct page_info));
    190 	if (!mem_pages_info) panic("Can't alloc my phys->virt page info");
    191 
    192 	/* allocate memory for the memory arrangement table */
    193 	memory_page_types = alloc(memory_table_size);
    194 	if (!memory_page_types) panic("Can't alloc my memory page type block");
    195 
    196 	initial_page_tables = alloc(16*1024);			/* size is 16 kb per definition */
    197 	if (!initial_page_tables) panic("Can't alloc my initial page tables");
    198 }
    199 
    200 
    201 void prepare_and_check_relocation_system(void) {
    202 	int   relocate_size, relocate_pages;
    203 	char *dst, *src;
    204 	int   pages;
    205 	int  *reloc_entry, last_phys, phys, logical, length;
    206 
    207 	/* set the number of relocation entries in the 1st word */
    208 	*reloc_instruction_table = reloc_entries;
    209 
    210 	/* the relocate information needs to be in one sequential physical space	*/
    211 	relocate_size = (reloc_tablesize + nbpp-1) & ~(nbpp-1);	/* round space up	*/
    212 	printf("\nPreparing for booting %s ... ", booted_file);
    213 	relocate_pages = relocate_size / nbpp;
    214 
    215 	relocate_table_pages = free_relocation_page;
    216 	pages = 0;
    217 	while (pages < relocate_pages) {
    218 		src = (char *)  reloc_instruction_table + pages*nbpp, nbpp;
    219 		dst = (void *) (relocate_table_pages + pages)->logical;
    220 		memcpy(dst, src, nbpp);
    221 
    222 		if (pages < relocate_pages-1) {
    223 			/* check if next page is sequential physically */
    224 			if ((relocate_table_pages+pages+1)->physical - (relocate_table_pages+pages)->physical != nbpp) {
    225 				/* Ieee! non contigunous relocate area -> try again */
    226 				printf("*");
    227 				relocate_table_pages += pages;
    228 				pages = -1;			/* will be incremented till zero later */
    229 			};
    230 		};
    231 		pages++;
    232 	};
    233 	free_relocation_page = relocate_table_pages + pages;
    234 
    235 	/* copy the relocation code into this page in start_kernel */
    236 	relocate_code_page = free_relocation_page++;
    237 
    238 	/*
    239 	 * All relocations are pages allocated in one big strict increasing
    240 	 * physical DRAM address sequence. When the MMU is switched off all
    241 	 * code and data is in this increasing order but not at the right
    242 	 * place. This is where the relocation code kicks in; relocation is
    243 	 * done in flat physical memory without MMU.
    244 	 */
    245 
    246 	printf("checking ... ");
    247 	reloc_entry = reloc_instruction_table + 1;
    248 	last_phys = -1;
    249 	while (reloc_entry < reloc_pos) {
    250 		phys    = reloc_entry[0];
    251 		logical = reloc_entry[1];
    252 		length  = reloc_entry[2];
    253 		reloc_entry[1] -= pv_offset;
    254 
    255 		if (last_phys - phys >= 0) printf("relocation sequence challenged -- booting might fail ");
    256 		last_phys = phys;
    257 
    258 		if (logical > top_physdram)
    259 			panic("Internal error: relocating outside RAM (%x > %lx) .. ", logical, top_physdram);
    260 
    261 		reloc_entry+=3;
    262 	};
    263 	if (reloc_entry != reloc_pos) panic("Relocation instructions table is corrupted");
    264 
    265 	printf("OK!\n");
    266 }
    267 
    268 
    269 void get_memory_configuration(void) {
    270 	int loop, current_page_type, page_count, phys_page;
    271 	int page, count, bank;
    272 	int mapped_screen_memory;
    273 
    274 	printf("Getting memory configuration ");
    275 
    276 	osmemory_read_arrangement_table(memory_page_types);
    277 
    278 	/* init counters */
    279 	vram_blocks = dram_blocks = rom_blocks = io_blocks = podram_blocks = 0;
    280 
    281 	current_page_type = -1;
    282 	phys_page = 0;			/* physical address in pages	*/
    283 	page_count = 0;			/* page counter in this block	*/
    284 	loop = 0;			/* loop variable over entries	*/
    285 
    286 	/* iterating over a packed array of 2 page types/byte i.e. 8 kb/byte */
    287 	while (loop < 2*memory_table_size) {
    288 		page = (memory_page_types[loop / 2]);	/* read	twice       	   */
    289 		if (loop & 1) page >>= 4;		/* take other nibble	   */
    290 
    291 		/* bits 0-2 give type, bit3 means the bit page is allocatable 	   */
    292 		page &= 0x7;				/* only take bottom 3 bits */
    293 		if (page != current_page_type) {
    294 			/* passed a boundary ... note this block		   */
    295 			/* splitting in different vars is for compatability reasons*/
    296 			switch (current_page_type) {
    297 				case -1 :
    298 				case  0 :
    299 					break;
    300 				case osmemory_TYPE_DRAM :
    301 					if (phys_page < PODRAM_START) {
    302 						DRAM_addr[dram_blocks]  = phys_page * nbpp;
    303 						DRAM_pages[dram_blocks] = page_count;
    304 						dram_blocks++;
    305 					} else {
    306 						PODRAM_addr[podram_blocks]  = phys_page * nbpp;
    307 						PODRAM_pages[podram_blocks] = page_count;
    308 						podram_blocks++;
    309 					};
    310 					break;
    311 				case osmemory_TYPE_VRAM :
    312 					VRAM_addr[vram_blocks]  = phys_page * nbpp;
    313 					VRAM_pages[vram_blocks] = page_count;
    314 					vram_blocks++;
    315 					break;
    316 				case osmemory_TYPE_ROM :
    317 					ROM_addr[rom_blocks]  = phys_page * nbpp;
    318 					ROM_pages[rom_blocks] = page_count;
    319 					rom_blocks++;
    320 					break;
    321 				case osmemory_TYPE_IO :
    322 					IO_addr[io_blocks]  = phys_page * nbpp;
    323 					IO_pages[io_blocks] = page_count;
    324 					io_blocks++;
    325 					break;
    326 				default :
    327 					printf("WARNING : found unknown memory object %d ", current_page_type);
    328 					printf(" at 0x%s", sprint0(8,'0','x', phys_page * nbpp));
    329 					printf(" for %s k\n", sprint0(5,' ','d', (page_count*nbpp)>>10));
    330 					break;
    331 			};
    332 			current_page_type = page;
    333 			phys_page = loop;
    334 			page_count = 0;
    335 		};
    336 		/* smallest unit we recognise is one page ... silly could be upto 64 pages i.e. 256 kb */
    337 		page_count += 1;
    338 		loop       += 1;
    339 		if ((loop & 31) == 0) twirl();
    340 	};
    341 
    342 	printf(" \n\n");
    343 
    344 	if (VRAM_pages[0] == 0) {
    345 		/* map bottom DRAM as video memory */
    346 		display_size	 = (vdu_var(os_VDUVAR_TOTAL_SCREEN_SIZE) & ~(nbpp-1));
    347 #if 0
    348 		mapped_screen_memory = 1024 * 1024;		/* max allowed on RiscPC	*/
    349 		videomem_pages   = (mapped_screen_memory / nbpp);
    350 		videomem_start   = DRAM_addr[0];
    351 		DRAM_addr[0]	+= videomem_pages * nbpp;
    352 		DRAM_pages[0]	-= videomem_pages;
    353 #else
    354 		mapped_screen_memory = display_size;
    355 		bank = dram_blocks-1;				/* pick last SIMM		*/
    356 		videomem_pages   = (mapped_screen_memory / nbpp);
    357 
    358 		/* Map video memory at the end of the SIMM	*/
    359 		videomem_start   = DRAM_addr[bank] + (DRAM_pages[bank] - videomem_pages)*nbpp;
    360 		DRAM_pages[bank]-= videomem_pages;
    361 #endif
    362 	} else {
    363 		/* use VRAM */
    364 		mapped_screen_memory = 0;
    365 		videomem_start	 = VRAM_addr[0];
    366 		videomem_pages	 = VRAM_pages[0];
    367 		display_size	 = videomem_pages * nbpp;
    368 	};
    369 
    370 	if (mapped_screen_memory) {
    371 		printf("Used %d kb DRAM ", (mapped_screen_memory)/1024);
    372 		printf("at 0x%s for video memory\n", sprint0(8,'0','x', videomem_start));
    373 	};
    374 
    375 	/* find top of DRAM pages */
    376 	top_physdram = 0;
    377 
    378 	for (loop = podram_blocks-1; (loop >= 0) && (PODRAM_addr[loop] == 0); loop++);
    379 	if (loop >= 0) top_physdram = PODRAM_addr[loop] + PODRAM_pages[loop]*nbpp;
    380 	if (top_physdram == 0) {
    381 		for (loop = dram_blocks-1; (loop >= 0) && (DRAM_addr[loop] == 0); loop++);
    382 		if (loop >= 0) top_physdram = DRAM_addr[loop] + DRAM_pages[loop]*nbpp;
    383 	};
    384 	if (top_physdram == 0) panic("reality check: No DRAM in this machine?");
    385 
    386 
    387 	videomem_start_ro = vdu_var(os_VDUVAR_DISPLAY_START);
    388 
    389 	/* pretty print the individual page types */
    390 	for (count = 0; count < rom_blocks; count++) {
    391 		printf("Found ROM  (%d)", count);
    392 		printf(" at 0x%s", sprint0(8,'0','x', ROM_addr[count]));
    393 		printf(" for %s k\n", sprint0(5,' ','d', (ROM_pages[count]*nbpp)>>10));
    394 	};
    395 
    396 	for (count = 0; count < io_blocks; count++) {
    397 		printf("Found I/O  (%d)", count);
    398 		printf(" at 0x%s", sprint0(8,'0','x', IO_addr[count]));
    399 		printf(" for %s k\n", sprint0(5,' ','d', (IO_pages[count]*nbpp)>>10));
    400 	};
    401 
    402 	/* for DRAM/VRAM also count the number of pages */
    403 	total_dram_pages = 0;
    404 	for (count = 0; count < dram_blocks; count++) {
    405 		total_dram_pages += DRAM_pages[count];
    406 		printf("Found DRAM (%d)", count);
    407 		printf(" at 0x%s", sprint0(8,'0','x', DRAM_addr[count]));
    408 		printf(" for %s k\n", sprint0(5,' ','d', (DRAM_pages[count]*nbpp)>>10));
    409 	};
    410 
    411 	total_vram_pages = 0;
    412 	for (count = 0; count < vram_blocks; count++) {
    413 		total_vram_pages += VRAM_pages[count];
    414 		printf("Found VRAM (%d)", count);
    415 		printf(" at 0x%s", sprint0(8,'0','x', VRAM_addr[count]));
    416 		printf(" for %s k\n", sprint0(5,' ','d', (VRAM_pages[count]*nbpp)>>10));
    417 	};
    418 
    419 	total_podram_pages = 0;
    420 	for (count = 0; count < podram_blocks; count++) {
    421 		total_podram_pages += PODRAM_pages[count];
    422 		printf("Found Processor only (S)DRAM (%d)", count);
    423 		printf(" at 0x%s", sprint0(8,'0','x', PODRAM_addr[count]));
    424 		printf(" for %s k\n", sprint0(5,' ','d', (PODRAM_pages[count]*nbpp)>>10));
    425 	};
    426 }
    427 
    428 
    429 void get_memory_map(void) {
    430 	struct page_info *page_info;
    431 	int	page, inout;
    432 	int	phys_addr;
    433 
    434 	printf("\nGetting actual memorymapping");
    435 	for (page = 0, page_info = mem_pages_info; page < totalpages; page++, page_info++) {
    436 		page_info->pagenumber = 0;	/* not used */
    437 		page_info->logical    = (firstpage + page) * nbpp;
    438 		page_info->physical   = 0;	/* result comes here */
    439 		/* to avoid triggering a `bug' in RISC OS 4, page it in */
    440 		*((int *) page_info->logical) = 0;
    441 	};
    442 	/* close list */
    443 	page_info->pagenumber = -1;
    444 
    445 	inout = osmemory_GIVEN_LOG_ADDR | osmemory_RETURN_PAGE_NO | osmemory_RETURN_PHYS_ADDR;
    446 	osmemory_page_op(inout, mem_pages_info, totalpages);
    447 
    448 	printf(" ; sorting ");
    449 	sort_memory_map();
    450 	printf(".\n");
    451 
    452 	/* get the first DRAM index and show the physical memory fragments we got */
    453 	printf("\nFound physical memory blocks :\n");
    454 	first_mapped_DRAM_page_index = -1;
    455 	first_mapped_PODRAM_page_index = -1;
    456 	for (page=0; page < totalpages; page++) {
    457 		phys_addr = mem_pages_info[page].physical;
    458 		printf("[0x%x", phys_addr);
    459 		while (mem_pages_info[page+1].physical - phys_addr == nbpp) {
    460 			if ((first_mapped_DRAM_page_index<0) && (phys_addr >= DRAM_addr[0])) {
    461 				first_mapped_DRAM_page_index = page;
    462 			};
    463 			if ((first_mapped_PODRAM_page_index<0) && (phys_addr >= PODRAM_addr[0])) {
    464 				first_mapped_PODRAM_page_index = page;
    465 			};
    466 			page++;
    467 			phys_addr = mem_pages_info[page].physical;
    468 		};
    469 		printf("-0x%x]  ", (phys_addr + nbpp -1));
    470 	};
    471 	printf("\n\n");
    472 	if (first_mapped_PODRAM_page_index < 0) {
    473 		if (PODRAM_addr[0]) panic("Found no (S)DRAM mapped in the bootloader ... increase Wimpslot!");
    474 	};
    475 	if (first_mapped_DRAM_page_index < 0) panic("No DRAM  mapped in the bootloader ... increase Wimpslot!");
    476 }
    477 
    478 
    479 void create_initial_page_tables(void) {
    480 	u_long page, section, addr, kpage;
    481 
    482 	/* mark a section by the following bits and domain 0, AP=01, CB=0 */
    483 	/*         A         P         C        B        section                 domain		*/
    484 	section = (0<<11) | (1<<10) | (0<<3) | (0<<2) | (1<<4) | (1<<1) | (0) | (0 << 5);
    485 
    486 	/* first of all a full 1:1 mapping */
    487 	for (page = 0; page < 4*1024; page++) {
    488 		initial_page_tables[page] = (page<<20) | section;
    489 	};
    490 
    491 	/* video memory is mapped 1:1 in the DRAM section or in VRAM section	*/
    492 
    493 	/* map 1Mb from top of memory to bottom 1Mb of virtual memmap		*/
    494 	top_1Mb_dram = (((top_physdram - 1024*1024) >> 20) << 20);
    495 	initial_page_tables[0] = top_1Mb_dram | section;
    496 
    497 	/* map 16 Mb of kernel space to KERNEL_BASE i.e. marks[KERNEL_START]	*/
    498 	for (page = 0; page < 16; page++) {
    499 		addr  = (kernel_physical_start >> 20) + page;
    500 		kpage = (marks[MARK_START]     >> 20) + page;
    501 		initial_page_tables[kpage] = (addr << 20) | section;
    502 	};
    503 }
    504 
    505 
    506 void add_pagetables_at_top(void) {
    507 	int page;
    508 	u_long src, dst, fragaddr;
    509 
    510 	/* Special : destination must be on a 16 Kb boundary			*/
    511 	/* get 4 pages on the top of the physical memeory and copy PT's in it	*/
    512 	new_L1_pages_phys = top_physdram - 4*nbpp;
    513 
    514 	/* If the L1 page tables are not 16 kb aligned, adjust base until it is	*/
    515 	while (new_L1_pages_phys & (16*1024-1)) {
    516 		new_L1_pages_phys -= nbpp;
    517 	};
    518 	if (new_L1_pages_phys & (16*1024-1)) panic("Paranoia : L1 pages not on 16Kb boundary");
    519 
    520 	dst = new_L1_pages_phys;
    521 	src = (u_long) initial_page_tables;
    522 
    523 	for (page = 0; page < 4; page++) {
    524 		/* get a page for a fragment */
    525 		fragaddr = get_relocated_page(dst, nbpp)->logical;
    526 		memcpy((void *) fragaddr, (void *) src, nbpp);
    527 
    528 		src += nbpp;
    529 		dst += nbpp;
    530 	};
    531 }
    532 
    533 
    534 void add_initvectors(void) {
    535 	u_long *pos;
    536 	u_long  vectoraddr, count;
    537 
    538 	/* the top 1Mb of the physical DRAM pages is mapped at address 0 */
    539 	vectoraddr = get_relocated_page(top_1Mb_dram, nbpp)->logical;
    540 
    541 	/* fill the vectors with `movs pc, lr' opcodes */
    542 	pos = (u_long *) vectoraddr; memset(pos, 0, nbpp);
    543 	for (count = 0; count < 128; count++) *pos++ = 0xE1B0F00E;
    544 }
    545 
    546 
    547 void create_configuration(int argc, char **argv, int start_args) {
    548 	int   i, root_specified, id_low, id_high;
    549 	char *pos;
    550 
    551 	bconfig_new_phys = kernel_free_vm_start - pv_offset;
    552 	bconfig_page = get_relocated_page(bconfig_new_phys, nbpp);
    553 	bconfig = (struct bootconfig *) (bconfig_page->logical);
    554 	kernel_free_vm_start += nbpp;
    555 
    556 	/* get some miscelanious info for the bootblock */
    557 	os_readsysinfo_monitor_info(NULL, &monitor_type, &monitor_sync);
    558 	os_readsysinfo_chip_presence(&ioeb_flags, &superio_flags, &lcd_flags);
    559 	os_readsysinfo_superio_features(&superio_flags_basic, &superio_flags_extra);
    560 	os_readsysinfo_unique_id(&id_low, &id_high);
    561 
    562 	/* fill in the bootconfig *bconfig structure : generic version II */
    563 	memset(bconfig, 0, sizeof(bconfig));
    564 	bconfig->magic			= BOOTCONFIG_MAGIC;
    565 	bconfig->version		= BOOTCONFIG_VERSION;
    566 	strcpy(bconfig->kernelname, booted_file);
    567 
    568 	/* get the kernel base name and update the RiscOS name to a Unix name */
    569 	i = strlen(bconfig->kernelname);
    570 	while ((i >= 0) && (bconfig->kernelname[i] != '.')) i--;
    571 	if (i) memcpy(bconfig->kernelname, bconfig->kernelname+i+1, strlen(booted_file)-i);
    572 
    573 	pos = bconfig->kernelname;
    574 	while (*pos) {
    575 		if (*pos == '/') *pos = '.';
    576 		pos++;
    577 	};
    578 
    579 	/* set the machine_id */
    580 	memcpy(&(bconfig->machine_id), &id_low, 4);
    581 
    582 	/* check if the `root' is specified */
    583 	root_specified = 0;
    584 	strcpy(bconfig->args, "");
    585 	for (i = start_args; i < argc; i++) {
    586 		if (strncmp(argv[i], "root=",5) ==0) root_specified = 1;
    587 		strcat(bconfig->args, argv[i]);
    588 	};
    589 	if (!root_specified) {
    590 		strcat(bconfig->args, "root=");
    591 		strcat(bconfig->args, DEFAULT_ROOT);
    592 	};
    593 
    594 	/* mark kernel pointers */
    595 	bconfig->kernvirtualbase	= marks[MARK_START];
    596 	bconfig->kernphysicalbase	= kernel_physical_start;
    597 	bconfig->kernsize		= kernel_free_vm_start - marks[MARK_START];
    598 	bconfig->ksym_start		= marks[MARK_SYM];
    599 	bconfig->ksym_end		= marks[MARK_SYM] + marks[MARK_NSYM];
    600 
    601 	/* setup display info */
    602 	bconfig->display_phys		= videomem_start;
    603 	bconfig->display_start		= videomem_start;
    604 	bconfig->display_size		= display_size;
    605 	bconfig->width			= vdu_var(os_MODEVAR_XWIND_LIMIT);
    606 	bconfig->height			= vdu_var(os_MODEVAR_YWIND_LIMIT);
    607 	bconfig->log2_bpp		= vdu_var(os_MODEVAR_LOG2_BPP);
    608 	bconfig->framerate		= 56;		/* XXX why? better guessing possible? XXX */
    609 
    610 	/* fill in memory info */
    611 	bconfig->pagesize		= nbpp;
    612 	bconfig->drampages		= total_dram_pages + total_podram_pages;	/* XXX */
    613 	bconfig->vrampages		= total_vram_pages;
    614 	bconfig->dramblocks		= dram_blocks + podram_blocks;			/* XXX */
    615 	bconfig->vramblocks		= vram_blocks;
    616 
    617 	for (i = 0; i < dram_blocks; i++) {
    618 		bconfig->dram[i].address = DRAM_addr[i];
    619 		bconfig->dram[i].pages   = DRAM_pages[i];
    620 		bconfig->dram[i].flags   = PHYSMEM_TYPE_GENERIC;
    621 	};
    622 	for (; i < dram_blocks + podram_blocks; i++) {
    623 		bconfig->dram[i].address = PODRAM_addr[i];
    624 		bconfig->dram[i].pages   = PODRAM_pages[i];
    625 		bconfig->dram[i].flags   = PHYSMEM_TYPE_PROCESSOR_ONLY;
    626 	};
    627 	for (i = 0; i < vram_blocks; i++) {
    628 		bconfig->vram[i].address = VRAM_addr[i];
    629 		bconfig->vram[i].pages   = VRAM_pages[i];
    630 		bconfig->vram[i].flags   = PHYSMEM_TYPE_GENERIC;
    631 	};
    632 }
    633 
    634 
    635 int main(int argc, char **argv) {
    636 	int howto, start_args, ret;
    637 
    638 	printf("\n\n");
    639 	printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
    640 	printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
    641 	printf(">> Booting NetBSD/acorn32 on a RiscPC/A7000/NC\n");
    642 	printf("\n");
    643 
    644 	process_args(argc, argv, &howto, booted_file, &start_args);
    645 
    646 	printf("Booting %s (howto = 0x%x)\n", booted_file, howto);
    647 
    648 	init_datastructures();
    649 	get_memory_configuration();
    650 	get_memory_map();
    651 
    652 	/* point to the first free DRAM page guaranteed to be in strict order up */
    653 	if (first_mapped_PODRAM_page_index) {
    654 		free_relocation_page = mem_pages_info + first_mapped_PODRAM_page_index;
    655 		kernel_physical_start = PODRAM_addr[0];
    656 	} else {
    657 		free_relocation_page = mem_pages_info + first_mapped_DRAM_page_index;
    658 		kernel_physical_start = DRAM_addr[0];
    659 	};
    660 
    661 	printf("\nLoading %s ", booted_file);
    662 
    663 	/* first count the kernel to get the markers */
    664 	ret = loadfile(booted_file, marks, COUNT_KERNEL);
    665 	if (ret == -1) panic("Kernel load failed");				/* lie to the user ...	*/
    666 	close(ret);
    667 
    668 	/* calculate how much the difference is between physical and virtual space for the kernel	*/
    669 	pv_offset = ((u_long) marks[MARK_START] - kernel_physical_start);
    670 	kernel_free_vm_start = (marks[MARK_END] + nbpp-1) & ~(nbpp-1);		/* round on a page	*/
    671 
    672 	/* we seem to be forced to clear the marks() ? */
    673 	bzero(marks, sizeof(marks[MARK_MAX]));
    674 
    675 	/* really load it ! */
    676 	ret = loadfile(booted_file, marks, LOAD_KERNEL);
    677 	if (ret == -1) panic("Kernel load failed");
    678 	close(ret);
    679 
    680 	/* finish off the relocation information */
    681 	create_initial_page_tables();
    682 	add_initvectors();
    683 	add_pagetables_at_top();
    684 	create_configuration(argc, argv, start_args);
    685 
    686 	/* done relocating and creating information, now update and check the relocation mechanism */
    687 	prepare_and_check_relocation_system();
    688 
    689 	printf("\nStarting at 0x%lx\n", marks[MARK_ENTRY]);
    690 	printf("Will boot in a few secs due to relocation....\nbye bye from RISC OS!");
    691 
    692 	/* dismount all filesystems */
    693 	xosfscontrol_shutdown();
    694 
    695 	/* reset devices, well they try to anyway */
    696 	service_pre_reset();
    697 
    698 	start_kernel(
    699 		/* r0 relocation code page (V)	*/ relocate_code_page->logical,
    700 		/* r1 relocation pv offset	*/ relocate_code_page->physical-relocate_code_page->logical,
    701 		/* r2 configuration structure	*/ bconfig_new_phys,
    702 		/* r3 relocation table (P)	*/ relocate_table_pages->physical,	/* one piece! */
    703 		/* r4 L1 page descriptor (P)	*/ new_L1_pages_phys,
    704 		/* r5 kernel entry point	*/ marks[MARK_ENTRY]
    705 	);
    706 	return 0;
    707 }
    708 
    709 
    710 ssize_t boot32_read(int f, void *addr, size_t size) {
    711 	caddr_t fragaddr;
    712 	size_t fragsize;
    713 	ssize_t bytes_read, total;
    714 
    715 	/* printf("read at %p for %ld bytes\n", addr, size); */
    716 	total = 0;
    717 	while (size > 0) {
    718 		fragsize = nbpp;				/* select one page	*/
    719 		if (size < nbpp) fragsize = size;		/* clip to size left	*/
    720 
    721 		/* get a page for a fragment */
    722 		fragaddr = (caddr_t) get_relocated_page((u_long) addr - pv_offset, fragsize)->logical;
    723 
    724 		bytes_read = read(f, fragaddr, fragsize);
    725 		if (bytes_read < 0) return bytes_read;		/* error!		*/
    726 		total += bytes_read;				/* account read bytes	*/
    727 
    728 		if (bytes_read < fragsize) return total;	/* does this happen?	*/
    729 
    730 		size -= fragsize;				/* advance		*/
    731 		addr += fragsize;
    732 	};
    733 	return total;
    734 }
    735 
    736 
    737 void *boot32_memcpy(void *dst, const void *src, size_t size) {
    738 	caddr_t fragaddr;
    739 	size_t fragsize;
    740 
    741 	/* printf("memcpy to %p from %p for %ld bytes\n", dst, src, size); */
    742 	while (size > 0) {
    743 		fragsize = nbpp;				/* select one page	*/
    744 		if (size < nbpp) fragsize = size;		/* clip to size left	*/
    745 
    746 		/* get a page for a fragment */
    747 		fragaddr = (caddr_t) get_relocated_page((u_long) dst - pv_offset, fragsize)->logical;
    748 		memcpy(fragaddr, src, size);
    749 
    750 		src += fragsize;				/* account copy		*/
    751 		dst += fragsize;
    752 		size-= fragsize;
    753 	};
    754 	return dst;
    755 };
    756 
    757 
    758 void *boot32_memset(void *dst, int c, size_t size) {
    759 	caddr_t fragaddr;
    760 	size_t fragsize;
    761 
    762 	/* printf("memset %p for %ld bytes with %d\n", dst, size, c); */
    763 	while (size > 0) {
    764 		fragsize = nbpp;				/* select one page	*/
    765 		if (size < nbpp) fragsize = size;		/* clip to size left	*/
    766 
    767 		/* get a page for a fragment */
    768 		fragaddr = (caddr_t) get_relocated_page((u_long) dst - pv_offset, fragsize)->logical;
    769 		memset(fragaddr, c, fragsize);
    770 
    771 		dst += fragsize;				/* account memsetting	*/
    772 		size-= fragsize;
    773 
    774 	};
    775 	return dst;
    776 }
    777 
    778 
    779 /* This sort routine needs to be re-implemented in either assembler or use other algorithm one day; its slow */
    780 void sort_memory_map(void) {
    781 	int out, in, count;
    782 	struct page_info *out_page, *in_page, temp_page;
    783 
    784 	count = 0;
    785 	for (out = 0, out_page = mem_pages_info; out < totalpages; out++, out_page++) {
    786 		for (in = out+1, in_page = out_page+1; in < totalpages; in++, in_page++) {
    787 			if (in_page->physical < out_page->physical) {
    788 				memcpy(&temp_page, in_page,    sizeof(struct page_info));
    789 				memcpy(in_page,    out_page,   sizeof(struct page_info));
    790 				memcpy(out_page,   &temp_page, sizeof(struct page_info));
    791 			};
    792 			count++;
    793 			if ((count & 0x3ffff) == 0) twirl();
    794 		};
    795 	};
    796 }
    797 
    798 
    799 struct page_info *get_relocated_page(u_long destination, int size) {
    800 	struct page_info *page;
    801 
    802 	/* get a page for a fragment */
    803 	page = free_relocation_page;
    804 	if (free_relocation_page->pagenumber < 0) panic("\n\nOut of pages; increase Wimpslot and try again");
    805 	reloc_entries++;
    806 	if (reloc_entries >= MAX_RELOCPAGES) panic("\n\nToo many relocations! What are you loading ??");
    807 
    808 	/* record the relocation */
    809 	*reloc_pos++ = free_relocation_page->physical;
    810 	*reloc_pos++ = destination;
    811 	*reloc_pos++ = size;
    812 	free_relocation_page++;				/* advance 		*/
    813 
    814 	return page;
    815 }
    816 
    817 
    818 int vdu_var(int var) {
    819 	int varlist[2], vallist[2];
    820 
    821 	varlist[0] = var;
    822 	varlist[1] = -1;
    823 	os_read_vdu_variables(varlist, vallist);
    824 	return vallist[0];
    825 }
    826 
    827 
    828 void twirl(void) {
    829 	printf("%c%c", "|/-\\"[(int) twirl_cnt], 8);
    830 	twirl_cnt++;
    831 	twirl_cnt &= 3;
    832 }
    833 
    834 
    835 void process_args(int argc, char **argv, int *howto, char *file, int *start_args) {
    836 	int i, j;
    837 	static char filename[80];
    838 
    839 	*howto = 0;
    840 	*file = NULL; *start_args = 1;
    841 	for (i = 1; i < argc; i++) {
    842 		if (argv[i][0] == '-')
    843 			for (j = 1; argv[i][j]; j++)
    844 				BOOT_FLAG(argv[i][j], *howto);
    845 		else {
    846 			if (*file)
    847 				*start_args = i;
    848 			else {
    849 				strcpy(file, argv[i]);
    850 				*start_args = i+1;
    851 			};
    852 			break;
    853 		};
    854 	};
    855 	if (*file == NULL) {
    856 		if (*howto & RB_ASKNAME) {
    857 			printf("boot: ");
    858 			gets(filename);
    859 			strcpy(file, filename);
    860 		} else
    861 			strcpy(file, "netbsd");
    862 	};
    863 }
    864 
    865 
    866 char *sprint0(int width, char prefix, char base, int value) {
    867 	static char format[50], scrap[50];
    868 	char *pos;
    869 	int length;
    870 
    871 	for (pos = format, length = 0; length<width; length++) *pos++ = prefix;
    872 	*pos++ = '%';
    873 	*pos++ = base;
    874 	*pos++ = (char) 0;
    875 
    876 	sprintf(scrap, format, value);
    877 	length = strlen(scrap);
    878 
    879 	return scrap+length-width;
    880 }
    881 
    882