boot32.c revision 1.23 1 /* $NetBSD: boot32.c,v 1.23 2006/03/23 22:27:22 bjh21 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 extern char end[];
46
47 /* debugging flags */
48 int debug = 1;
49
50
51 /* constants */
52 #define PODRAM_START (512*1024*1024) /* XXX Kinetic cards XXX */
53
54 #define MAX_RELOCPAGES 4096
55
56 #define DEFAULT_ROOT "/dev/wd0a"
57
58
59 #define IO_BLOCKS 16 /* move these to the bootloader structure? */
60 #define ROM_BLOCKS 16
61 #define PODRAM_BLOCKS 16
62
63
64 /* booter variables */
65 char scrap[80], twirl_cnt; /* misc */
66 char booted_file[80];
67
68 struct bootconfig *bconfig; /* bootconfig passing */
69 u_long bconfig_new_phys; /* physical address its bound */
70
71 /* computer knowledge */
72 u_int monitor_type, monitor_sync, ioeb_flags, lcd_flags;
73 u_int superio_flags, superio_flags_basic, superio_flags_extra;
74
75 /* sizes */
76 int nbpp, memory_table_size, memory_image_size;
77 /* relocate info */
78 u_long reloc_tablesize, *reloc_instruction_table;
79 u_long *reloc_pos; /* current empty entry */
80 int reloc_entries; /* number of relocations */
81 int first_mapped_DRAM_page_index; /* offset in RISC OS blob */
82 int first_mapped_PODRAM_page_index;/* offset in RISC OS blob */
83
84 struct page_info *mem_pages_info; /* {nr, virt, phys}* */
85 struct page_info *free_relocation_page; /* points to the page_info chain*/
86 struct page_info *relocate_table_pages; /* points to seq. relocate info */
87 struct page_info *relocate_code_page; /* points to the copied code */
88 struct page_info *bconfig_page; /* page for passing on settings */
89
90 unsigned char *memory_page_types; /* packed array of 4 bit typeId */
91
92 u_long *initial_page_tables; /* pagetables to be booted from */
93
94
95 /* XXX rename *_BLOCKS to MEM_BLOCKS */
96 /* DRAM/VRAM/ROM/IO info */
97 /* where the display is */
98 u_long videomem_start, videomem_pages, display_size;
99
100 u_long pv_offset, top_physdram; /* kernel_base - phys. diff */
101 u_long top_1Mb_dram; /* the lower mapped top 1Mb */
102 u_long new_L1_pages_phys; /* physical address of L1 pages */
103
104 /* for bootconfig passing */
105 u_long total_podram_pages, total_dram_pages, total_vram_pages;
106 int dram_blocks, podram_blocks; /* number of mem. objects/type */
107 int vram_blocks, rom_blocks, io_blocks;
108
109 u_long DRAM_addr[DRAM_BLOCKS], DRAM_pages[DRAM_BLOCKS];
110 /* processor only RAM */
111 u_long PODRAM_addr[PODRAM_BLOCKS], PODRAM_pages[PODRAM_BLOCKS];
112 u_long VRAM_addr[VRAM_BLOCKS], VRAM_pages[VRAM_BLOCKS];
113 u_long ROM_addr[ROM_BLOCKS], ROM_pages[ROM_BLOCKS];
114 u_long IO_addr[IO_BLOCKS], IO_pages[IO_BLOCKS];
115
116
117 /* RISC OS memory pages we claimed */
118 u_long firstpage, lastpage, totalpages; /* RISC OS pagecounters */
119 /* RISC OS memory */
120 char *memory_image, *bottom_memory, *top_memory;
121
122 u_long videomem_start_ro; /* for debugging mainly */
123
124 /* kernel info */
125 u_long marks[MARK_MAX]; /* loader mark pointers */
126 u_long kernel_physical_start; /* where does it get relocated */
127 u_long kernel_free_vm_start; /* where does the free VM start */
128 /* some free space to mess with */
129 u_long scratch_virtualbase, scratch_physicalbase;
130
131
132 /* bootprogram identifiers */
133 extern const char bootprog_rev[];
134 extern const char bootprog_name[];
135 extern const char bootprog_date[];
136 extern const char bootprog_maker[];
137
138
139 /* predefines / prototypes */
140 void init_datastructures(void);
141 void get_memory_configuration(void);
142 void get_memory_map(void);
143 void create_initial_page_tables(void);
144 void add_pagetables_at_top(void);
145 int page_info_cmp(const void *a, const void *);
146 void add_initvectors(void);
147 void create_configuration(int argc, char **argv, int start_args);
148 void prepare_and_check_relocation_system(void);
149 void twirl(void);
150 int vdu_var(int);
151 void process_args(int argc, char **argv, int *howto, char *file,
152 int *start_args);
153
154 struct page_info *get_relocated_page(u_long destination, int size);
155
156 extern void start_kernel(
157 int relocate_code_page,
158 int relocation_pv_offset,
159 int configuration_structure_in_flat_physical_space,
160 int physical_address_of_relocation_tables,
161 int physical_address_of_new_L1_pages,
162 int kernel_entry_point
163 ); /* asm */
164
165
166 /* the loader itself */
167 void
168 init_datastructures(void)
169 {
170
171 /* Get number of pages and the memorytablesize */
172 osmemory_read_arrangement_table_size(&memory_table_size, &nbpp);
173
174 /* Allocate 99% - (small fixed amount) of the heap for memory_image */
175 memory_image_size = (int)HIMEM - (int)end - 512 * 1024;
176 memory_image_size /= 100;
177 memory_image_size *= 99;
178 if (memory_image_size <= 256*1024)
179 panic("Insufficient memory");
180
181 memory_image = alloc(memory_image_size);
182 if (!memory_image)
183 panic("Can't alloc get my memory image ?");
184
185 bottom_memory = memory_image;
186 top_memory = memory_image + memory_image_size;
187
188 firstpage = ((int)bottom_memory / nbpp) + 1; /* safety */
189 lastpage = ((int)top_memory / nbpp) - 1;
190 totalpages = lastpage - firstpage;
191
192 printf("Allocated %ld memory pages, each of %d kilobytes.\n\n",
193 totalpages, nbpp>>10 );
194
195 /*
196 * Setup the relocation table. Its a simple array of 3 * 32 bit
197 * entries. The first word in the array is the number of relocations
198 * to be done
199 */
200 reloc_tablesize = (MAX_RELOCPAGES+1)*3*sizeof(u_long);
201 reloc_instruction_table = alloc(reloc_tablesize);
202 if (!reloc_instruction_table)
203 panic("Can't alloc my relocate instructions pages");
204
205 reloc_entries = 0;
206 reloc_pos = reloc_instruction_table;
207 *reloc_pos++ = 0;
208
209 /*
210 * Set up the memory translation info structure. We need to allocate
211 * one more for the end of list marker. See get_memory_map.
212 */
213 mem_pages_info = alloc((totalpages + 1)*sizeof(struct page_info));
214 if (!mem_pages_info)
215 panic("Can't alloc my phys->virt page info");
216
217 /*
218 * Allocate memory for the memory arrangement table. We use this
219 * structure to retrieve memory page properties to clasify them.
220 */
221 memory_page_types = alloc(memory_table_size);
222 if (!memory_page_types)
223 panic("Can't alloc my memory page type block");
224
225 /*
226 * Initial page tables is 16 kb per definition since only sections are
227 * used.
228 */
229 initial_page_tables = alloc(16*1024);
230 if (!initial_page_tables)
231 panic("Can't alloc my initial page tables");
232 }
233
234
235 void
236 prepare_and_check_relocation_system(void)
237 {
238 int relocate_size, relocate_pages;
239 int bank, pages, found;
240 u_long dst, src, base, destination, extend;
241 u_long *reloc_entry, last_src, length;
242
243 /* set the number of relocation entries in the 1st word */
244 *reloc_instruction_table = reloc_entries;
245
246 /*
247 * The relocate information needs to be in one sequential physical
248 * space in order to be able to access it as one stream when the MMU
249 * is switched off later.
250 */
251 relocate_size = (reloc_tablesize + nbpp-1) & ~(nbpp-1); /* round up */
252 printf("\nPreparing for booting %s ... ", booted_file);
253 relocate_pages = relocate_size / nbpp;
254
255 relocate_table_pages = free_relocation_page;
256 pages = 0;
257 while (pages < relocate_pages) {
258 src = (u_long)reloc_instruction_table + pages*nbpp;
259 dst = (relocate_table_pages + pages)->logical;
260 memcpy((void *)dst, (void *)src, nbpp);
261
262 if (pages < relocate_pages - 1) {
263 /* check if next page is sequential physically */
264 if ((relocate_table_pages+pages+1)->physical -
265 (relocate_table_pages+pages)->physical != nbpp) {
266 /*
267 * Non contigunous relocate area ->
268 * try again
269 */
270 printf("*");
271 relocate_table_pages += pages;
272 continue; /* while */
273 }
274 }
275 pages++;
276 }
277 free_relocation_page = relocate_table_pages + pages;
278
279 /* copy the relocation code into this page in start_kernel */
280 relocate_code_page = free_relocation_page++;
281
282 /*
283 * All relocations are pages allocated in one big strict increasing
284 * physical DRAM address sequence. When the MMU is switched off all
285 * code and data is in this increasing order but not at the right
286 * place. This is where the relocation code kicks in; relocation is
287 * done in flat physical memory without MMU.
288 */
289
290 printf("shift and check ... ");
291 reloc_entry = reloc_instruction_table + 1;
292 last_src = -1;
293 while (reloc_entry < reloc_pos) {
294 src = reloc_entry[0];
295 destination = reloc_entry[1];
296 length = reloc_entry[2];
297
298 /* paranoia check */
299 if ((long) (src - last_src) <= 0)
300 printf("relocation sequence challenged -- "
301 "booting might fail ");
302 last_src = src;
303
304 /* check if its gonna be relocated into (PO)DRAM ! */
305 extend = destination + length;
306 found = 0;
307 for (bank = 0; (bank < dram_blocks) && !found; bank++) {
308 base = DRAM_addr[bank];
309 found = (destination >= base) &&
310 (extend <= base + DRAM_pages[bank]*nbpp);
311 }
312 for (bank = 0; (bank < podram_blocks) && !found; bank++) {
313 base = PODRAM_addr[bank];
314 found = (destination >= base) &&
315 (extend <= base + PODRAM_pages[bank]*nbpp);
316 }
317 if (!found || (extend > top_physdram)) {
318 panic("Internal error: relocating range "
319 "[%lx +%lx => %lx] outside (PO)DRAM banks!",
320 src, length, destination);
321 }
322
323 reloc_entry += 3;
324 }
325 if (reloc_entry != reloc_pos)
326 panic("Relocation instruction table is corrupted");
327
328 printf("OK!\n");
329 }
330
331
332 void
333 get_memory_configuration(void)
334 {
335 int loop, current_page_type, page_count, phys_page;
336 int page, count, bank, top_bank, video_bank;
337 int mapped_screen_memory;
338 int one_mb_pages;
339 u_long top;
340
341 printf("Getting memory configuration ");
342
343 osmemory_read_arrangement_table(memory_page_types);
344
345 /* init counters */
346 bank = vram_blocks = dram_blocks = rom_blocks = io_blocks =
347 podram_blocks = 0;
348
349 current_page_type = -1;
350 phys_page = 0; /* physical address in pages */
351 page_count = 0; /* page counter in this block */
352 loop = 0; /* loop variable over entries */
353
354 /* iterating over a packed array of 2 page types/byte i.e. 8 kb/byte */
355 while (loop < 2*memory_table_size) {
356 page = memory_page_types[loop / 2]; /* read twice */
357 if (loop & 1) page >>= 4; /* take other nibble */
358
359 /*
360 * bits 0-2 give type, bit3 means the bit page is
361 * allocatable
362 */
363 page &= 0x7; /* only take bottom 3 bits */
364 if (page != current_page_type) {
365 /* passed a boundary ... note this block */
366 /*
367 * splitting in different vars is for
368 * compatability reasons
369 */
370 switch (current_page_type) {
371 case -1:
372 case 0:
373 break;
374 case osmemory_TYPE_DRAM:
375 if (phys_page < PODRAM_START) {
376 DRAM_addr[dram_blocks] =
377 phys_page * nbpp;
378 DRAM_pages[dram_blocks] =
379 page_count;
380 dram_blocks++;
381 } else {
382 PODRAM_addr[podram_blocks] =
383 phys_page * nbpp;
384 PODRAM_pages[podram_blocks] =
385 page_count;
386 podram_blocks++;
387 }
388 break;
389 case osmemory_TYPE_VRAM:
390 VRAM_addr[vram_blocks] = phys_page * nbpp;
391 VRAM_pages[vram_blocks] = page_count;
392 vram_blocks++;
393 break;
394 case osmemory_TYPE_ROM:
395 ROM_addr[rom_blocks] = phys_page * nbpp;
396 ROM_pages[rom_blocks] = page_count;
397 rom_blocks++;
398 break;
399 case osmemory_TYPE_IO:
400 IO_addr[io_blocks] = phys_page * nbpp;
401 IO_pages[io_blocks] = page_count;
402 io_blocks++;
403 break;
404 default:
405 printf("WARNING : found unknown "
406 "memory object %d ", current_page_type);
407 printf(" at 0x%08x", phys_page * nbpp);
408 printf(" for %5d k\n", (page_count*nbpp)>>10);
409 break;
410 }
411 current_page_type = page;
412 phys_page = loop;
413 page_count = 0;
414 }
415 /*
416 * smallest unit we recognise is one page ... silly
417 * could be upto 64 pages i.e. 256 kb
418 */
419 page_count += 1;
420 loop += 1;
421 if ((loop & 31) == 0) twirl();
422 }
423
424 printf(" \n\n");
425
426 if (VRAM_pages[0] == 0) {
427 /* map DRAM as video memory */
428 display_size =
429 vdu_var(os_VDUVAR_TOTAL_SCREEN_SIZE) & ~(nbpp-1);
430 #if 0
431 mapped_screen_memory = 1024 * 1024; /* max allowed on RiscPC */
432 videomem_pages = (mapped_screen_memory / nbpp);
433 videomem_start = DRAM_addr[0];
434 DRAM_addr[0] += videomem_pages * nbpp;
435 DRAM_pages[0] -= videomem_pages;
436 #else
437 mapped_screen_memory = display_size;
438 videomem_pages = mapped_screen_memory / nbpp;
439 one_mb_pages = (1024*1024)/nbpp;
440
441 /*
442 * OK... we need one Mb at the top for compliance with current
443 * kernel structure. This ought to be abolished one day IMHO.
444 * Also we have to take care that the kernel needs to be in
445 * DRAM0a and even has to start there.
446 * XXX one Mb simms are the smallest supported XXX
447 */
448 top_bank = dram_blocks-1;
449 video_bank = top_bank;
450 if (DRAM_pages[top_bank] == one_mb_pages) video_bank--;
451
452 if (DRAM_pages[video_bank] < videomem_pages)
453 panic("Weird memory configuration found; please "
454 "contact acorn32 portmaster.");
455
456 /* split off the top 1Mb */
457 DRAM_addr [top_bank+1] = DRAM_addr[top_bank] +
458 (DRAM_pages[top_bank] - one_mb_pages)*nbpp;
459 DRAM_pages[top_bank+1] = one_mb_pages;
460 DRAM_pages[top_bank ] -= one_mb_pages;
461 dram_blocks++;
462
463 /* Map video memory at the end of the choosen DIMM */
464 videomem_start = DRAM_addr[video_bank] +
465 (DRAM_pages[video_bank] - videomem_pages)*nbpp;
466 DRAM_pages[video_bank] -= videomem_pages;
467
468 /* sanity */
469 if (DRAM_pages[top_bank] == 0) {
470 DRAM_addr [top_bank] = DRAM_addr [top_bank+1];
471 DRAM_pages[top_bank] = DRAM_pages[top_bank+1];
472 dram_blocks--;
473 }
474 #endif
475 } else {
476 /* use VRAM */
477 mapped_screen_memory = 0;
478 videomem_start = VRAM_addr[0];
479 videomem_pages = VRAM_pages[0];
480 display_size = videomem_pages * nbpp;
481 }
482
483 if (mapped_screen_memory) {
484 printf("Used %d kb DRAM ", mapped_screen_memory / 1024);
485 printf("at 0x%08lx for video memory\n", videomem_start);
486 }
487
488 /* find top of (PO)DRAM pages */
489 top_physdram = 0;
490 for (loop = 0; loop < podram_blocks; loop++) {
491 top = PODRAM_addr[loop] + PODRAM_pages[loop]*nbpp;
492 if (top > top_physdram) top_physdram = top;
493 }
494 for (loop = 0; loop < dram_blocks; loop++) {
495 top = DRAM_addr[loop] + DRAM_pages[loop]*nbpp;
496 if (top > top_physdram) top_physdram = top;
497 }
498 if (top_physdram == 0)
499 panic("reality check: No DRAM in this machine?");
500 if (((top_physdram >> 20) << 20) != top_physdram)
501 panic("Top is not not aligned on a Mb; "
502 "remove very small DIMMS?");
503
504 videomem_start_ro = vdu_var(os_VDUVAR_DISPLAY_START);
505
506 /* pretty print the individual page types */
507 for (count = 0; count < rom_blocks; count++) {
508 printf("Found ROM (%d)", count);
509 printf(" at 0x%08lx", ROM_addr[count]);
510 printf(" for %5lu k\n", (ROM_pages[count]*nbpp)>>10);
511 }
512
513 for (count = 0; count < io_blocks; count++) {
514 printf("Found I/O (%d)", count);
515 printf(" at 0x%08lx", IO_addr[count]);
516 printf(" for %5lu k\n", (IO_pages[count]*nbpp)>>10);
517 }
518
519 /* for DRAM/VRAM also count the number of pages */
520 total_dram_pages = 0;
521 for (count = 0; count < dram_blocks; count++) {
522 total_dram_pages += DRAM_pages[count];
523 printf("Found DRAM (%d)", count);
524 printf(" at 0x%08lx", DRAM_addr[count]);
525 printf(" for %5lu k\n", (DRAM_pages[count]*nbpp)>>10);
526 }
527
528 total_vram_pages = 0;
529 for (count = 0; count < vram_blocks; count++) {
530 total_vram_pages += VRAM_pages[count];
531 printf("Found VRAM (%d)", count);
532 printf(" at 0x%08lx", VRAM_addr[count]);
533 printf(" for %5lu k\n", (VRAM_pages[count]*nbpp)>>10);
534 }
535
536 total_podram_pages = 0;
537 for (count = 0; count < podram_blocks; count++) {
538 total_podram_pages += PODRAM_pages[count];
539 printf("Found Processor only (S)DRAM (%d)", count);
540 printf(" at 0x%08lx", PODRAM_addr[count]);
541 printf(" for %5lu k\n", (PODRAM_pages[count]*nbpp)>>10);
542 }
543 }
544
545
546 void
547 get_memory_map(void)
548 {
549 struct page_info *page_info;
550 int page, inout;
551 int phys_addr;
552
553 printf("\nGetting actual memorymapping");
554 for (page = 0, page_info = mem_pages_info;
555 page < totalpages;
556 page++, page_info++) {
557 page_info->pagenumber = 0; /* not used */
558 page_info->logical = (firstpage + page) * nbpp;
559 page_info->physical = 0; /* result comes here */
560 /* to avoid triggering a `bug' in RISC OS 4, page it in */
561 *((int *)page_info->logical) = 0;
562 }
563 /* close list */
564 page_info->pagenumber = -1;
565
566 inout = osmemory_GIVEN_LOG_ADDR | osmemory_RETURN_PAGE_NO |
567 osmemory_RETURN_PHYS_ADDR;
568 osmemory_page_op(inout, mem_pages_info, totalpages);
569
570 printf(" ; sorting ");
571 qsort(mem_pages_info, totalpages, sizeof(struct page_info),
572 &page_info_cmp);
573 printf(".\n");
574
575 /*
576 * get the first DRAM index and show the physical memory
577 * fragments we got
578 */
579 printf("\nFound physical memory blocks :\n");
580 first_mapped_DRAM_page_index = -1;
581 first_mapped_PODRAM_page_index = -1;
582 for (page=0; page < totalpages; page++) {
583 phys_addr = mem_pages_info[page].physical;
584 printf("[0x%x", phys_addr);
585 while (mem_pages_info[page+1].physical - phys_addr == nbpp) {
586 if (first_mapped_DRAM_page_index < 0 &&
587 phys_addr >= DRAM_addr[0])
588 first_mapped_DRAM_page_index = page;
589 if (first_mapped_PODRAM_page_index < 0 &&
590 phys_addr >= PODRAM_addr[0])
591 first_mapped_PODRAM_page_index = page;
592 page++;
593 phys_addr = mem_pages_info[page].physical;
594 }
595 printf("-0x%x] ", phys_addr + nbpp -1);
596 }
597 printf("\n\n");
598 if (first_mapped_PODRAM_page_index < 0 && PODRAM_addr[0])
599 panic("Found no (S)DRAM mapped in the bootloader");
600 if (first_mapped_DRAM_page_index < 0)
601 panic("No DRAM mapped in the bootloader");
602 }
603
604
605 void
606 create_initial_page_tables(void)
607 {
608 u_long page, section, addr, kpage;
609
610 /* mark a section by the following bits and domain 0, AP=01, CB=0 */
611 /* A P C B section
612 domain */
613 section = (0<<11) | (1<<10) | (0<<3) | (0<<2) | (1<<4) | (1<<1) |
614 (0) | (0 << 5);
615
616 /* first of all a full 1:1 mapping */
617 for (page = 0; page < 4*1024; page++)
618 initial_page_tables[page] = (page<<20) | section;
619
620 /*
621 * video memory is mapped 1:1 in the DRAM section or in VRAM
622 * section
623 *
624 * map 1Mb from top of DRAM memory to bottom 1Mb of virtual memmap
625 */
626 top_1Mb_dram = (((top_physdram - 1024*1024) >> 20) << 20);
627
628 initial_page_tables[0] = top_1Mb_dram | section;
629
630 /*
631 * map 16 Mb of kernel space to KERNEL_BASE
632 * i.e. marks[KERNEL_START]
633 */
634 for (page = 0; page < 16; page++) {
635 addr = (kernel_physical_start >> 20) + page;
636 kpage = (marks[MARK_START] >> 20) + page;
637 initial_page_tables[kpage] = (addr << 20) | section;
638 }
639 }
640
641
642 void
643 add_pagetables_at_top(void)
644 {
645 int page;
646 u_long src, dst, fragaddr;
647
648 /* Special : destination must be on a 16 Kb boundary */
649 /* get 4 pages on the top of the physical memory and copy PT's in it */
650 new_L1_pages_phys = top_physdram - 4 * nbpp;
651
652 /*
653 * If the L1 page tables are not 16 kb aligned, adjust base
654 * until it is
655 */
656 while (new_L1_pages_phys & (16*1024-1))
657 new_L1_pages_phys -= nbpp;
658 if (new_L1_pages_phys & (16*1024-1))
659 panic("Paranoia : L1 pages not on 16Kb boundary");
660
661 dst = new_L1_pages_phys;
662 src = (u_long)initial_page_tables;
663
664 for (page = 0; page < 4; page++) {
665 /* get a page for a fragment */
666 fragaddr = get_relocated_page(dst, nbpp)->logical;
667 memcpy((void *)fragaddr, (void *)src, nbpp);
668
669 src += nbpp;
670 dst += nbpp;
671 }
672 }
673
674
675 void
676 add_initvectors(void)
677 {
678 u_long *pos;
679 u_long vectoraddr, count;
680
681 /* the top 1Mb of the physical DRAM pages is mapped at address 0 */
682 vectoraddr = get_relocated_page(top_1Mb_dram, nbpp)->logical;
683
684 /* fill the vectors with `movs pc, lr' opcodes */
685 pos = (u_long *)vectoraddr; memset(pos, 0, nbpp);
686 for (count = 0; count < 128; count++) *pos++ = 0xE1B0F00E;
687 }
688
689
690 void
691 create_configuration(int argc, char **argv, int start_args)
692 {
693 int i, root_specified, id_low, id_high;
694 char *pos;
695
696 bconfig_new_phys = kernel_free_vm_start - pv_offset;
697 bconfig_page = get_relocated_page(bconfig_new_phys, nbpp);
698 bconfig = (struct bootconfig *)(bconfig_page->logical);
699 kernel_free_vm_start += nbpp;
700
701 /* get some miscelanious info for the bootblock */
702 os_readsysinfo_monitor_info(NULL, &monitor_type, &monitor_sync);
703 os_readsysinfo_chip_presence(&ioeb_flags, &superio_flags, &lcd_flags);
704 os_readsysinfo_superio_features(&superio_flags_basic,
705 &superio_flags_extra);
706 os_readsysinfo_unique_id(&id_low, &id_high);
707
708 /* fill in the bootconfig *bconfig structure : generic version II */
709 memset(bconfig, 0, sizeof(bconfig));
710 bconfig->magic = BOOTCONFIG_MAGIC;
711 bconfig->version = BOOTCONFIG_VERSION;
712 strcpy(bconfig->kernelname, booted_file);
713
714 /*
715 * get the kernel base name and update the RiscOS name to a
716 * Unix name
717 */
718 i = strlen(booted_file);
719 while (i >= 0 && booted_file[i] != '.') i--;
720 if (i) {
721 strcpy(bconfig->kernelname, "/");
722 strcat(bconfig->kernelname, booted_file+i+1);
723 }
724
725 pos = bconfig->kernelname+1;
726 while (*pos) {
727 if (*pos == '/') *pos = '.';
728 pos++;
729 }
730
731 /* set the machine_id */
732 memcpy(&(bconfig->machine_id), &id_low, 4);
733
734 /* check if the `root' is specified */
735 root_specified = 0;
736 strcpy(bconfig->args, "");
737 for (i = start_args; i < argc; i++) {
738 if (strncmp(argv[i], "root=",5) ==0) root_specified = 1;
739 strcat(bconfig->args, argv[i]);
740 }
741 if (!root_specified) {
742 strcat(bconfig->args, "root=");
743 strcat(bconfig->args, DEFAULT_ROOT);
744 }
745
746 /* mark kernel pointers */
747 bconfig->kernvirtualbase = marks[MARK_START];
748 bconfig->kernphysicalbase = kernel_physical_start;
749 bconfig->kernsize = kernel_free_vm_start -
750 marks[MARK_START];
751 bconfig->ksym_start = marks[MARK_SYM];
752 bconfig->ksym_end = marks[MARK_SYM] + marks[MARK_NSYM];
753
754 /* setup display info */
755 bconfig->display_phys = videomem_start;
756 bconfig->display_start = videomem_start;
757 bconfig->display_size = display_size;
758 bconfig->width = vdu_var(os_MODEVAR_XWIND_LIMIT);
759 bconfig->height = vdu_var(os_MODEVAR_YWIND_LIMIT);
760 bconfig->log2_bpp = vdu_var(os_MODEVAR_LOG2_BPP);
761 /* XXX why? better guessing possible? XXX */
762 bconfig->framerate = 56;
763
764 /* fill in memory info */
765 bconfig->pagesize = nbpp;
766 bconfig->drampages = total_dram_pages +
767 total_podram_pages; /* XXX */
768 bconfig->vrampages = total_vram_pages;
769 bconfig->dramblocks = dram_blocks + podram_blocks; /*XXX*/
770 bconfig->vramblocks = vram_blocks;
771
772 for (i = 0; i < dram_blocks; i++) {
773 bconfig->dram[i].address = DRAM_addr[i];
774 bconfig->dram[i].pages = DRAM_pages[i];
775 bconfig->dram[i].flags = PHYSMEM_TYPE_GENERIC;
776 }
777 for (; i < dram_blocks + podram_blocks; i++) {
778 bconfig->dram[i].address = PODRAM_addr[i];
779 bconfig->dram[i].pages = PODRAM_pages[i];
780 bconfig->dram[i].flags = PHYSMEM_TYPE_PROCESSOR_ONLY;
781 }
782 for (i = 0; i < vram_blocks; i++) {
783 bconfig->vram[i].address = VRAM_addr[i];
784 bconfig->vram[i].pages = VRAM_pages[i];
785 bconfig->vram[i].flags = PHYSMEM_TYPE_GENERIC;
786 }
787 }
788
789
790 int
791 main(int argc, char **argv)
792 {
793 int howto, start_args, ret;
794
795 printf("\n\n");
796 printf(">> %s, Revision %s\n", bootprog_name, bootprog_rev);
797 printf(">> (%s, %s)\n", bootprog_maker, bootprog_date);
798 printf(">> Booting NetBSD/acorn32 on a RiscPC/A7000/NC\n");
799 printf("\n");
800
801 process_args(argc, argv, &howto, booted_file, &start_args);
802
803 printf("Booting %s (howto = 0x%x)\n", booted_file, howto);
804
805 init_datastructures();
806 get_memory_configuration();
807 get_memory_map();
808
809 /*
810 * point to the first free DRAM page guaranteed to be in
811 * strict order up
812 */
813 if (first_mapped_PODRAM_page_index) {
814 free_relocation_page =
815 mem_pages_info + first_mapped_PODRAM_page_index;
816 kernel_physical_start = PODRAM_addr[0];
817 } else {
818 free_relocation_page =
819 mem_pages_info + first_mapped_DRAM_page_index;
820 kernel_physical_start = DRAM_addr[0];
821 }
822
823 printf("\nLoading %s ", booted_file);
824
825 /* first count the kernel to get the markers */
826 ret = loadfile(booted_file, marks, COUNT_KERNEL);
827 if (ret == -1) panic("Kernel load failed"); /* lie to the user ... */
828 close(ret);
829
830 /*
831 * calculate how much the difference is between physical and
832 * virtual space for the kernel
833 */
834 pv_offset = ((u_long)marks[MARK_START] - kernel_physical_start);
835 /* round on a page */
836 kernel_free_vm_start = (marks[MARK_END] + nbpp-1) & ~(nbpp-1);
837
838 /* we seem to be forced to clear the marks[] ? */
839 bzero(marks, sizeof(marks[MARK_MAX]));
840
841 /* really load it ! */
842 ret = loadfile(booted_file, marks, LOAD_KERNEL);
843 if (ret == -1) panic("Kernel load failed");
844 close(ret);
845
846 /* finish off the relocation information */
847 create_initial_page_tables();
848 add_initvectors();
849 add_pagetables_at_top();
850 create_configuration(argc, argv, start_args);
851
852 /*
853 * done relocating and creating information, now update and
854 * check the relocation mechanism
855 */
856 prepare_and_check_relocation_system();
857
858 printf("\nStarting at 0x%lx\n", marks[MARK_ENTRY]);
859 printf("Will boot in a few secs due to relocation....\n"
860 "bye bye from RISC OS!");
861
862 /* dismount all filesystems */
863 xosfscontrol_shutdown();
864
865 /* reset devices, well they try to anyway */
866 service_pre_reset();
867
868 start_kernel(
869 /* r0 relocation code page (V) */ relocate_code_page->logical,
870 /* r1 relocation pv offset */
871 relocate_code_page->physical-relocate_code_page->logical,
872 /* r2 configuration structure */ bconfig_new_phys,
873 /* r3 relocation table (P) */
874 relocate_table_pages->physical, /* one piece! */
875 /* r4 L1 page descriptor (P) */ new_L1_pages_phys,
876 /* r5 kernel entry point */ marks[MARK_ENTRY]
877 );
878 return 0;
879 }
880
881
882 ssize_t
883 boot32_read(int f, void *addr, size_t size)
884 {
885 caddr_t fragaddr;
886 size_t fragsize;
887 ssize_t bytes_read, total;
888
889 /* printf("read at %p for %ld bytes\n", addr, size); */
890 total = 0;
891 while (size > 0) {
892 fragsize = nbpp; /* select one page */
893 if (size < nbpp) fragsize = size;/* clip to size left */
894
895 /* get a page for a fragment */
896 fragaddr = (caddr_t)get_relocated_page((u_long) addr -
897 pv_offset, fragsize)->logical;
898
899 bytes_read = read(f, fragaddr, fragsize);
900 if (bytes_read < 0) return bytes_read; /* error! */
901 total += bytes_read; /* account read bytes */
902
903 if (bytes_read < fragsize)
904 return total; /* does this happen? */
905
906 size -= fragsize; /* advance */
907 addr += fragsize;
908 }
909 return total;
910 }
911
912
913 void *
914 boot32_memcpy(void *dst, const void *src, size_t size)
915 {
916 caddr_t fragaddr;
917 size_t fragsize;
918
919 /* printf("memcpy to %p from %p for %ld bytes\n", dst, src, size); */
920 while (size > 0) {
921 fragsize = nbpp; /* select one page */
922 if (size < nbpp) fragsize = size;/* clip to size left */
923
924 /* get a page for a fragment */
925 fragaddr = (caddr_t)get_relocated_page((u_long) dst -
926 pv_offset, fragsize)->logical;
927 memcpy(fragaddr, src, size);
928
929 src += fragsize; /* account copy */
930 dst += fragsize;
931 size-= fragsize;
932 }
933 return dst;
934 }
935
936
937 void *
938 boot32_memset(void *dst, int c, size_t size)
939 {
940 caddr_t fragaddr;
941 size_t fragsize;
942
943 /* printf("memset %p for %ld bytes with %d\n", dst, size, c); */
944 while (size > 0) {
945 fragsize = nbpp; /* select one page */
946 if (size < nbpp) fragsize = size;/* clip to size left */
947
948 /* get a page for a fragment */
949 fragaddr = (caddr_t)get_relocated_page((u_long)dst - pv_offset,
950 fragsize)->logical;
951 memset(fragaddr, c, fragsize);
952
953 dst += fragsize; /* account memsetting */
954 size-= fragsize;
955
956 }
957 return dst;
958 }
959
960
961 /* We can rely on the fact that two entries never have identical ->physical */
962 int
963 page_info_cmp(const void *a, const void *b)
964 {
965
966 return (((struct page_info *)a)->physical <
967 ((struct page_info *)b)->physical) ? -1 : 1;
968 }
969
970 struct page_info *
971 get_relocated_page(u_long destination, int size)
972 {
973 struct page_info *page;
974
975 /* get a page for a fragment */
976 page = free_relocation_page;
977 if (free_relocation_page->pagenumber < 0) panic("\n\nOut of pages");
978 reloc_entries++;
979 if (reloc_entries >= MAX_RELOCPAGES)
980 panic("\n\nToo many relocations! What are you loading ??");
981
982 /* record the relocation */
983 *reloc_pos++ = free_relocation_page->physical;
984 *reloc_pos++ = destination;
985 *reloc_pos++ = size;
986 free_relocation_page++; /* advance */
987
988 return page;
989 }
990
991
992 int
993 vdu_var(int var)
994 {
995 int varlist[2], vallist[2];
996
997 varlist[0] = var;
998 varlist[1] = -1;
999 os_read_vdu_variables(varlist, vallist);
1000 return vallist[0];
1001 }
1002
1003
1004 void
1005 twirl(void)
1006 {
1007
1008 printf("%c%c", "|/-\\"[(int) twirl_cnt], 8);
1009 twirl_cnt++;
1010 twirl_cnt &= 3;
1011 }
1012
1013
1014 void
1015 process_args(int argc, char **argv, int *howto, char *file, int *start_args)
1016 {
1017 int i, j;
1018 static char filename[80];
1019
1020 *howto = 0;
1021 *file = NULL; *start_args = 1;
1022 for (i = 1; i < argc; i++) {
1023 if (argv[i][0] == '-')
1024 for (j = 1; argv[i][j]; j++)
1025 BOOT_FLAG(argv[i][j], *howto);
1026 else {
1027 if (*file)
1028 *start_args = i;
1029 else {
1030 strcpy(file, argv[i]);
1031 *start_args = i+1;
1032 }
1033 break;
1034 }
1035 }
1036 if (*file == NULL) {
1037 if (*howto & RB_ASKNAME) {
1038 printf("boot: ");
1039 gets(filename);
1040 strcpy(file, filename);
1041 } else
1042 strcpy(file, "netbsd");
1043 }
1044 }
1045