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