boot32.c revision 1.15 1 /* $NetBSD: boot32.c,v 1.15 2003/06/03 12:53:47 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 u_long reloc_tablesize, *reloc_instruction_table; /* relocate info */
75 u_long *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(u_long); /* 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 int bank, pages, found;
204 u_long dst, src, base, destination, extend;
205 u_long *reloc_entry, last_src, 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 = (u_long) reloc_instruction_table + pages*nbpp;
219 dst = (relocate_table_pages + pages)->logical;
220 memcpy((void *) dst, (void *) 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("shift and check ... ");
247 reloc_entry = reloc_instruction_table + 1;
248 last_src = -1;
249 while (reloc_entry < reloc_pos) {
250 src = reloc_entry[0];
251 destination = reloc_entry[1];
252 length = reloc_entry[2];
253
254 /* paranoia check */
255 if ((long) (src - last_src) <= 0) printf("relocation sequence challenged -- booting might fail ");
256 last_src = src;
257
258 /* check if its gonna be relocated into (PO)DRAM ! */
259 extend = destination + length;
260 found = 0;
261 for (bank = 0; (bank < dram_blocks) && !found; bank++) {
262 base = DRAM_addr[bank];
263 found = (destination >= base) && (extend <= base + DRAM_pages[bank]*nbpp);
264 };
265 for (bank = 0; (bank < podram_blocks) && !found; bank++) {
266 base = PODRAM_addr[bank];
267 found = (destination >= base) && (extend <= base + PODRAM_pages[bank]*nbpp);
268 };
269 if (!found || (extend > top_physdram)) {
270 panic( "Internal error: relocating range [%lx +%lx => %lx] outside (PO)DRAM banks!",
271 src, length, destination
272 );
273 };
274
275 reloc_entry+=3;
276 };
277 if (reloc_entry != reloc_pos) panic("Relocation instruction table is corrupted");
278
279 printf("OK!\n");
280 }
281
282
283 void get_memory_configuration(void) {
284 int loop, current_page_type, page_count, phys_page;
285 int page, count, bank, top_bank, video_bank;
286 int mapped_screen_memory;
287 int one_mb_pages;
288 u_long top;
289
290 printf("Getting memory configuration ");
291
292 osmemory_read_arrangement_table(memory_page_types);
293
294 /* init counters */
295 bank = vram_blocks = dram_blocks = rom_blocks = io_blocks = podram_blocks = 0;
296
297 current_page_type = -1;
298 phys_page = 0; /* physical address in pages */
299 page_count = 0; /* page counter in this block */
300 loop = 0; /* loop variable over entries */
301
302 /* iterating over a packed array of 2 page types/byte i.e. 8 kb/byte */
303 while (loop < 2*memory_table_size) {
304 page = (memory_page_types[loop / 2]); /* read twice */
305 if (loop & 1) page >>= 4; /* take other nibble */
306
307 /* bits 0-2 give type, bit3 means the bit page is allocatable */
308 page &= 0x7; /* only take bottom 3 bits */
309 if (page != current_page_type) {
310 /* passed a boundary ... note this block */
311 /* splitting in different vars is for compatability reasons*/
312 switch (current_page_type) {
313 case -1 :
314 case 0 :
315 break;
316 case osmemory_TYPE_DRAM :
317 if (phys_page < PODRAM_START) {
318 DRAM_addr[dram_blocks] = phys_page * nbpp;
319 DRAM_pages[dram_blocks] = page_count;
320 dram_blocks++;
321 } else {
322 PODRAM_addr[podram_blocks] = phys_page * nbpp;
323 PODRAM_pages[podram_blocks] = page_count;
324 podram_blocks++;
325 };
326 break;
327 case osmemory_TYPE_VRAM :
328 VRAM_addr[vram_blocks] = phys_page * nbpp;
329 VRAM_pages[vram_blocks] = page_count;
330 vram_blocks++;
331 break;
332 case osmemory_TYPE_ROM :
333 ROM_addr[rom_blocks] = phys_page * nbpp;
334 ROM_pages[rom_blocks] = page_count;
335 rom_blocks++;
336 break;
337 case osmemory_TYPE_IO :
338 IO_addr[io_blocks] = phys_page * nbpp;
339 IO_pages[io_blocks] = page_count;
340 io_blocks++;
341 break;
342 default :
343 printf("WARNING : found unknown memory object %d ", current_page_type);
344 printf(" at 0x%s", sprint0(8,'0','x', phys_page * nbpp));
345 printf(" for %s k\n", sprint0(5,' ','d', (page_count*nbpp)>>10));
346 break;
347 };
348 current_page_type = page;
349 phys_page = loop;
350 page_count = 0;
351 };
352 /* smallest unit we recognise is one page ... silly could be upto 64 pages i.e. 256 kb */
353 page_count += 1;
354 loop += 1;
355 if ((loop & 31) == 0) twirl();
356 };
357
358 printf(" \n\n");
359
360 if (VRAM_pages[0] == 0) {
361 /* map DRAM as video memory */
362 display_size = (vdu_var(os_VDUVAR_TOTAL_SCREEN_SIZE) & ~(nbpp-1));
363 #if 0
364 mapped_screen_memory = 1024 * 1024; /* max allowed on RiscPC */
365 videomem_pages = (mapped_screen_memory / nbpp);
366 videomem_start = DRAM_addr[0];
367 DRAM_addr[0] += videomem_pages * nbpp;
368 DRAM_pages[0] -= videomem_pages;
369 #else
370 mapped_screen_memory = display_size;
371 videomem_pages = (mapped_screen_memory / nbpp);
372 one_mb_pages = (1024*1024)/nbpp;
373
374 /*
375 * OK... we need one Mb at the top for compliance with current
376 * kernel structure. This ought to be abolished one day IMHO.
377 * Also we have to take care that the kernel needs to be in
378 * DRAM0a and even has to start there.
379 * XXX one Mb simms are the smallest supported XXX
380 */
381 top_bank = dram_blocks-1;
382 video_bank = top_bank;
383 if (DRAM_pages[top_bank] == one_mb_pages) video_bank--;
384
385 if (DRAM_pages[video_bank] < videomem_pages)
386 panic("Weird memory configuration found; please contact acorn32 portmaster.");
387
388 /* split off the top 1Mb */
389 DRAM_addr [top_bank+1] = DRAM_addr[top_bank] + (DRAM_pages[top_bank] - one_mb_pages)*nbpp;
390 DRAM_pages[top_bank+1] = one_mb_pages;
391 DRAM_pages[top_bank ] -= one_mb_pages;
392 dram_blocks++;
393
394 /* Map video memory at the end of the choosen DIMM */
395 videomem_start = DRAM_addr[video_bank] + (DRAM_pages[video_bank] - videomem_pages)*nbpp;
396 DRAM_pages[video_bank] -= videomem_pages;
397
398 /* sanity */
399 if (DRAM_pages[top_bank] == 0) {
400 DRAM_addr [top_bank] = DRAM_addr [top_bank+1];
401 DRAM_pages[top_bank] = DRAM_pages[top_bank+1];
402 dram_blocks--;
403 };
404 #endif
405 } else {
406 /* use VRAM */
407 mapped_screen_memory = 0;
408 videomem_start = VRAM_addr[0];
409 videomem_pages = VRAM_pages[0];
410 display_size = videomem_pages * nbpp;
411 };
412
413 if (mapped_screen_memory) {
414 printf("Used %d kb DRAM ", (mapped_screen_memory)/1024);
415 printf("at 0x%s for video memory\n", sprint0(8,'0','x', videomem_start));
416 };
417
418 /* find top of (PO)DRAM pages */
419 top_physdram = 0;
420 for (loop = 0; loop < podram_blocks; loop++) {
421 top = PODRAM_addr[loop] + PODRAM_pages[loop]*nbpp;
422 if (top > top_physdram) top_physdram = top;
423 };
424 for (loop = 0; loop < dram_blocks; loop++) {
425 top = DRAM_addr[loop] + DRAM_pages[loop]*nbpp;
426 if (top > top_physdram) top_physdram = top;
427 };
428 if (top_physdram == 0) panic("reality check: No DRAM in this machine?");
429 if (((top_physdram >> 20) << 20) != top_physdram)
430 panic("Top is not not aligned on a Mb; remove very small DIMMS?");
431
432 videomem_start_ro = vdu_var(os_VDUVAR_DISPLAY_START);
433
434 /* pretty print the individual page types */
435 for (count = 0; count < rom_blocks; count++) {
436 printf("Found ROM (%d)", count);
437 printf(" at 0x%s", sprint0(8,'0','x', ROM_addr[count]));
438 printf(" for %s k\n", sprint0(5,' ','d', (ROM_pages[count]*nbpp)>>10));
439 };
440
441 for (count = 0; count < io_blocks; count++) {
442 printf("Found I/O (%d)", count);
443 printf(" at 0x%s", sprint0(8,'0','x', IO_addr[count]));
444 printf(" for %s k\n", sprint0(5,' ','d', (IO_pages[count]*nbpp)>>10));
445 };
446
447 /* for DRAM/VRAM also count the number of pages */
448 total_dram_pages = 0;
449 for (count = 0; count < dram_blocks; count++) {
450 total_dram_pages += DRAM_pages[count];
451 printf("Found DRAM (%d)", count);
452 printf(" at 0x%s", sprint0(8,'0','x', DRAM_addr[count]));
453 printf(" for %s k\n", sprint0(5,' ','d', (DRAM_pages[count]*nbpp)>>10));
454 };
455
456 total_vram_pages = 0;
457 for (count = 0; count < vram_blocks; count++) {
458 total_vram_pages += VRAM_pages[count];
459 printf("Found VRAM (%d)", count);
460 printf(" at 0x%s", sprint0(8,'0','x', VRAM_addr[count]));
461 printf(" for %s k\n", sprint0(5,' ','d', (VRAM_pages[count]*nbpp)>>10));
462 };
463
464 total_podram_pages = 0;
465 for (count = 0; count < podram_blocks; count++) {
466 total_podram_pages += PODRAM_pages[count];
467 printf("Found Processor only (S)DRAM (%d)", count);
468 printf(" at 0x%s", sprint0(8,'0','x', PODRAM_addr[count]));
469 printf(" for %s k\n", sprint0(5,' ','d', (PODRAM_pages[count]*nbpp)>>10));
470 };
471 }
472
473
474 void get_memory_map(void) {
475 struct page_info *page_info;
476 int page, inout;
477 int phys_addr;
478
479 printf("\nGetting actual memorymapping");
480 for (page = 0, page_info = mem_pages_info; page < totalpages; page++, page_info++) {
481 page_info->pagenumber = 0; /* not used */
482 page_info->logical = (firstpage + page) * nbpp;
483 page_info->physical = 0; /* result comes here */
484 /* to avoid triggering a `bug' in RISC OS 4, page it in */
485 *((int *) page_info->logical) = 0;
486 };
487 /* close list */
488 page_info->pagenumber = -1;
489
490 inout = osmemory_GIVEN_LOG_ADDR | osmemory_RETURN_PAGE_NO | osmemory_RETURN_PHYS_ADDR;
491 osmemory_page_op(inout, mem_pages_info, totalpages);
492
493 printf(" ; sorting ");
494 sort_memory_map();
495 printf(".\n");
496
497 /* get the first DRAM index and show the physical memory fragments we got */
498 printf("\nFound physical memory blocks :\n");
499 first_mapped_DRAM_page_index = -1;
500 first_mapped_PODRAM_page_index = -1;
501 for (page=0; page < totalpages; page++) {
502 phys_addr = mem_pages_info[page].physical;
503 printf("[0x%x", phys_addr);
504 while (mem_pages_info[page+1].physical - phys_addr == nbpp) {
505 if ((first_mapped_DRAM_page_index<0) && (phys_addr >= DRAM_addr[0])) {
506 first_mapped_DRAM_page_index = page;
507 };
508 if ((first_mapped_PODRAM_page_index<0) && (phys_addr >= PODRAM_addr[0])) {
509 first_mapped_PODRAM_page_index = page;
510 };
511 page++;
512 phys_addr = mem_pages_info[page].physical;
513 };
514 printf("-0x%x] ", (phys_addr + nbpp -1));
515 };
516 printf("\n\n");
517 if (first_mapped_PODRAM_page_index < 0) {
518 if (PODRAM_addr[0]) panic("Found no (S)DRAM mapped in the bootloader ... increase Wimpslot!");
519 };
520 if (first_mapped_DRAM_page_index < 0) panic("No DRAM mapped in the bootloader ... increase Wimpslot!");
521 }
522
523
524 void create_initial_page_tables(void) {
525 u_long page, section, addr, kpage;
526
527 /* mark a section by the following bits and domain 0, AP=01, CB=0 */
528 /* A P C B section domain */
529 section = (0<<11) | (1<<10) | (0<<3) | (0<<2) | (1<<4) | (1<<1) | (0) | (0 << 5);
530
531 /* first of all a full 1:1 mapping */
532 for (page = 0; page < 4*1024; page++) {
533 initial_page_tables[page] = (page<<20) | section;
534 };
535
536 /* video memory is mapped 1:1 in the DRAM section or in VRAM section */
537
538 /* map 1Mb from top of DRAM memory to bottom 1Mb of virtual memmap */
539 top_1Mb_dram = (((top_physdram - 1024*1024) >> 20) << 20);
540
541 initial_page_tables[0] = top_1Mb_dram | section;
542
543 /* map 16 Mb of kernel space to KERNEL_BASE i.e. marks[KERNEL_START] */
544 for (page = 0; page < 16; page++) {
545 addr = (kernel_physical_start >> 20) + page;
546 kpage = (marks[MARK_START] >> 20) + page;
547 initial_page_tables[kpage] = (addr << 20) | section;
548 };
549 }
550
551
552 void add_pagetables_at_top(void) {
553 int page;
554 u_long src, dst, fragaddr;
555
556 /* Special : destination must be on a 16 Kb boundary */
557 /* get 4 pages on the top of the physical memory and copy PT's in it */
558 new_L1_pages_phys = top_physdram - 4*nbpp;
559
560 /* If the L1 page tables are not 16 kb aligned, adjust base until it is */
561 while (new_L1_pages_phys & (16*1024-1)) {
562 new_L1_pages_phys -= nbpp;
563 };
564 if (new_L1_pages_phys & (16*1024-1)) panic("Paranoia : L1 pages not on 16Kb boundary");
565
566 dst = new_L1_pages_phys;
567 src = (u_long) initial_page_tables;
568
569 for (page = 0; page < 4; page++) {
570 /* get a page for a fragment */
571 fragaddr = get_relocated_page(dst, nbpp)->logical;
572 memcpy((void *) fragaddr, (void *) src, nbpp);
573
574 src += nbpp;
575 dst += nbpp;
576 };
577 }
578
579
580 void add_initvectors(void) {
581 u_long *pos;
582 u_long vectoraddr, count;
583
584 /* the top 1Mb of the physical DRAM pages is mapped at address 0 */
585 vectoraddr = get_relocated_page(top_1Mb_dram, nbpp)->logical;
586
587 /* fill the vectors with `movs pc, lr' opcodes */
588 pos = (u_long *) vectoraddr; memset(pos, 0, nbpp);
589 for (count = 0; count < 128; count++) *pos++ = 0xE1B0F00E;
590 }
591
592
593 void create_configuration(int argc, char **argv, int start_args) {
594 int i, root_specified, id_low, id_high;
595 char *pos;
596
597 bconfig_new_phys = kernel_free_vm_start - pv_offset;
598 bconfig_page = get_relocated_page(bconfig_new_phys, nbpp);
599 bconfig = (struct bootconfig *) (bconfig_page->logical);
600 kernel_free_vm_start += nbpp;
601
602 /* get some miscelanious info for the bootblock */
603 os_readsysinfo_monitor_info(NULL, &monitor_type, &monitor_sync);
604 os_readsysinfo_chip_presence(&ioeb_flags, &superio_flags, &lcd_flags);
605 os_readsysinfo_superio_features(&superio_flags_basic, &superio_flags_extra);
606 os_readsysinfo_unique_id(&id_low, &id_high);
607
608 /* fill in the bootconfig *bconfig structure : generic version II */
609 memset(bconfig, 0, sizeof(bconfig));
610 bconfig->magic = BOOTCONFIG_MAGIC;
611 bconfig->version = BOOTCONFIG_VERSION;
612 strcpy(bconfig->kernelname, booted_file);
613
614 /* get the kernel base name and update the RiscOS name to a Unix name */
615 i = strlen(bconfig->kernelname);
616 while ((i >= 0) && (bconfig->kernelname[i] != '.')) i--;
617 if (i) memcpy(bconfig->kernelname, bconfig->kernelname+i+1, strlen(booted_file)-i);
618
619 pos = bconfig->kernelname;
620 while (*pos) {
621 if (*pos == '/') *pos = '.';
622 pos++;
623 };
624
625 /* set the machine_id */
626 memcpy(&(bconfig->machine_id), &id_low, 4);
627
628 /* check if the `root' is specified */
629 root_specified = 0;
630 strcpy(bconfig->args, "");
631 for (i = start_args; i < argc; i++) {
632 if (strncmp(argv[i], "root=",5) ==0) root_specified = 1;
633 strcat(bconfig->args, argv[i]);
634 };
635 if (!root_specified) {
636 strcat(bconfig->args, "root=");
637 strcat(bconfig->args, DEFAULT_ROOT);
638 };
639
640 /* mark kernel pointers */
641 bconfig->kernvirtualbase = marks[MARK_START];
642 bconfig->kernphysicalbase = kernel_physical_start;
643 bconfig->kernsize = kernel_free_vm_start - marks[MARK_START];
644 bconfig->ksym_start = marks[MARK_SYM];
645 bconfig->ksym_end = marks[MARK_SYM] + marks[MARK_NSYM];
646
647 /* setup display info */
648 bconfig->display_phys = videomem_start;
649 bconfig->display_start = videomem_start;
650 bconfig->display_size = display_size;
651 bconfig->width = vdu_var(os_MODEVAR_XWIND_LIMIT);
652 bconfig->height = vdu_var(os_MODEVAR_YWIND_LIMIT);
653 bconfig->log2_bpp = vdu_var(os_MODEVAR_LOG2_BPP);
654 bconfig->framerate = 56; /* XXX why? better guessing possible? XXX */
655
656 /* fill in memory info */
657 bconfig->pagesize = nbpp;
658 bconfig->drampages = total_dram_pages + total_podram_pages; /* XXX */
659 bconfig->vrampages = total_vram_pages;
660 bconfig->dramblocks = dram_blocks + podram_blocks; /* XXX */
661 bconfig->vramblocks = vram_blocks;
662
663 for (i = 0; i < dram_blocks; i++) {
664 bconfig->dram[i].address = DRAM_addr[i];
665 bconfig->dram[i].pages = DRAM_pages[i];
666 bconfig->dram[i].flags = PHYSMEM_TYPE_GENERIC;
667 };
668 for (; i < dram_blocks + podram_blocks; i++) {
669 bconfig->dram[i].address = PODRAM_addr[i];
670 bconfig->dram[i].pages = PODRAM_pages[i];
671 bconfig->dram[i].flags = PHYSMEM_TYPE_PROCESSOR_ONLY;
672 };
673 for (i = 0; i < vram_blocks; i++) {
674 bconfig->vram[i].address = VRAM_addr[i];
675 bconfig->vram[i].pages = VRAM_pages[i];
676 bconfig->vram[i].flags = PHYSMEM_TYPE_GENERIC;
677 };
678 }
679
680
681 int main(int argc, char **argv) {
682 int howto, start_args, ret;
683
684 printf("\n\n");
685 printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
686 printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
687 printf(">> Booting NetBSD/acorn32 on a RiscPC/A7000/NC\n");
688 printf("\n");
689
690 process_args(argc, argv, &howto, booted_file, &start_args);
691
692 printf("Booting %s (howto = 0x%x)\n", booted_file, howto);
693
694 init_datastructures();
695 get_memory_configuration();
696 get_memory_map();
697
698 /* point to the first free DRAM page guaranteed to be in strict order up */
699 if (first_mapped_PODRAM_page_index) {
700 free_relocation_page = mem_pages_info + first_mapped_PODRAM_page_index;
701 kernel_physical_start = PODRAM_addr[0];
702 } else {
703 free_relocation_page = mem_pages_info + first_mapped_DRAM_page_index;
704 kernel_physical_start = DRAM_addr[0];
705 };
706
707 printf("\nLoading %s ", booted_file);
708
709 /* first count the kernel to get the markers */
710 ret = loadfile(booted_file, marks, COUNT_KERNEL);
711 if (ret == -1) panic("Kernel load failed"); /* lie to the user ... */
712 close(ret);
713
714 /* calculate how much the difference is between physical and virtual space for the kernel */
715 pv_offset = ((u_long) marks[MARK_START] - kernel_physical_start);
716 kernel_free_vm_start = (marks[MARK_END] + nbpp-1) & ~(nbpp-1); /* round on a page */
717
718 /* we seem to be forced to clear the marks[] ? */
719 bzero(marks, sizeof(marks[MARK_MAX]));
720
721 /* really load it ! */
722 ret = loadfile(booted_file, marks, LOAD_KERNEL);
723 if (ret == -1) panic("Kernel load failed");
724 close(ret);
725
726 /* finish off the relocation information */
727 create_initial_page_tables();
728 add_initvectors();
729 add_pagetables_at_top();
730 create_configuration(argc, argv, start_args);
731
732 /* done relocating and creating information, now update and check the relocation mechanism */
733 prepare_and_check_relocation_system();
734
735 printf("\nStarting at 0x%lx\n", marks[MARK_ENTRY]);
736 printf("Will boot in a few secs due to relocation....\nbye bye from RISC OS!");
737
738 /* dismount all filesystems */
739 xosfscontrol_shutdown();
740
741 /* reset devices, well they try to anyway */
742 service_pre_reset();
743
744 start_kernel(
745 /* r0 relocation code page (V) */ relocate_code_page->logical,
746 /* r1 relocation pv offset */ relocate_code_page->physical-relocate_code_page->logical,
747 /* r2 configuration structure */ bconfig_new_phys,
748 /* r3 relocation table (P) */ relocate_table_pages->physical, /* one piece! */
749 /* r4 L1 page descriptor (P) */ new_L1_pages_phys,
750 /* r5 kernel entry point */ marks[MARK_ENTRY]
751 );
752 return 0;
753 }
754
755
756 ssize_t boot32_read(int f, void *addr, size_t size) {
757 caddr_t fragaddr;
758 size_t fragsize;
759 ssize_t bytes_read, total;
760
761 /* printf("read at %p for %ld bytes\n", addr, size); */
762 total = 0;
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) addr - pv_offset, fragsize)->logical;
769
770 bytes_read = read(f, fragaddr, fragsize);
771 if (bytes_read < 0) return bytes_read; /* error! */
772 total += bytes_read; /* account read bytes */
773
774 if (bytes_read < fragsize) return total; /* does this happen? */
775
776 size -= fragsize; /* advance */
777 addr += fragsize;
778 };
779 return total;
780 }
781
782
783 void *boot32_memcpy(void *dst, const void *src, size_t size) {
784 caddr_t fragaddr;
785 size_t fragsize;
786
787 /* printf("memcpy to %p from %p for %ld bytes\n", dst, src, size); */
788 while (size > 0) {
789 fragsize = nbpp; /* select one page */
790 if (size < nbpp) fragsize = size; /* clip to size left */
791
792 /* get a page for a fragment */
793 fragaddr = (caddr_t) get_relocated_page((u_long) dst - pv_offset, fragsize)->logical;
794 memcpy(fragaddr, src, size);
795
796 src += fragsize; /* account copy */
797 dst += fragsize;
798 size-= fragsize;
799 };
800 return dst;
801 };
802
803
804 void *boot32_memset(void *dst, int c, size_t size) {
805 caddr_t fragaddr;
806 size_t fragsize;
807
808 /* printf("memset %p for %ld bytes with %d\n", dst, size, c); */
809 while (size > 0) {
810 fragsize = nbpp; /* select one page */
811 if (size < nbpp) fragsize = size; /* clip to size left */
812
813 /* get a page for a fragment */
814 fragaddr = (caddr_t) get_relocated_page((u_long) dst - pv_offset, fragsize)->logical;
815 memset(fragaddr, c, fragsize);
816
817 dst += fragsize; /* account memsetting */
818 size-= fragsize;
819
820 };
821 return dst;
822 }
823
824
825 /* This sort routine needs to be re-implemented in either assembler or use other algorithm one day; its slow */
826 void sort_memory_map(void) {
827 int out, in, count;
828 struct page_info *out_page, *in_page, temp_page;
829
830 count = 0;
831 for (out = 0, out_page = mem_pages_info; out < totalpages; out++, out_page++) {
832 for (in = out+1, in_page = out_page+1; in < totalpages; in++, in_page++) {
833 if (in_page->physical < out_page->physical) {
834 memcpy(&temp_page, in_page, sizeof(struct page_info));
835 memcpy(in_page, out_page, sizeof(struct page_info));
836 memcpy(out_page, &temp_page, sizeof(struct page_info));
837 };
838 count++;
839 if ((count & 0x3ffff) == 0) twirl();
840 };
841 };
842 }
843
844
845 struct page_info *get_relocated_page(u_long destination, int size) {
846 struct page_info *page;
847
848 /* get a page for a fragment */
849 page = free_relocation_page;
850 if (free_relocation_page->pagenumber < 0) panic("\n\nOut of pages; increase Wimpslot and try again");
851 reloc_entries++;
852 if (reloc_entries >= MAX_RELOCPAGES) panic("\n\nToo many relocations! What are you loading ??");
853
854 /* record the relocation */
855 *reloc_pos++ = free_relocation_page->physical;
856 *reloc_pos++ = destination;
857 *reloc_pos++ = size;
858 free_relocation_page++; /* advance */
859
860 return page;
861 }
862
863
864 int vdu_var(int var) {
865 int varlist[2], vallist[2];
866
867 varlist[0] = var;
868 varlist[1] = -1;
869 os_read_vdu_variables(varlist, vallist);
870 return vallist[0];
871 }
872
873
874 void twirl(void) {
875 printf("%c%c", "|/-\\"[(int) twirl_cnt], 8);
876 twirl_cnt++;
877 twirl_cnt &= 3;
878 }
879
880
881 void process_args(int argc, char **argv, int *howto, char *file, int *start_args) {
882 int i, j;
883 static char filename[80];
884
885 *howto = 0;
886 *file = NULL; *start_args = 1;
887 for (i = 1; i < argc; i++) {
888 if (argv[i][0] == '-')
889 for (j = 1; argv[i][j]; j++)
890 BOOT_FLAG(argv[i][j], *howto);
891 else {
892 if (*file)
893 *start_args = i;
894 else {
895 strcpy(file, argv[i]);
896 *start_args = i+1;
897 };
898 break;
899 };
900 };
901 if (*file == NULL) {
902 if (*howto & RB_ASKNAME) {
903 printf("boot: ");
904 gets(filename);
905 strcpy(file, filename);
906 } else
907 strcpy(file, "netbsd");
908 };
909 }
910
911
912 char *sprint0(int width, char prefix, char base, int value) {
913 static char format[50], scrap[50];
914 char *pos;
915 int length;
916
917 for (pos = format, length = 0; length<width; length++) *pos++ = prefix;
918 *pos++ = '%';
919 *pos++ = base;
920 *pos++ = (char) 0;
921
922 sprintf(scrap, format, value);
923 length = strlen(scrap);
924
925 return scrap+length-width;
926 }
927
928