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