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