main.c revision 170854
1/*- 2 * Copyright (c) 2000 Benno Rice <benno@jeamland.net> 3 * Copyright (c) 2000 Stephane Potvin <sepotvin@videotron.ca> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD: head/sys/boot/ofw/common/main.c 170854 2007-06-17 00:17:15Z marius $"); 30 31#include <stand.h> 32#include "openfirm.h" 33#include "libofw.h" 34#include "bootstrap.h" 35 36struct arch_switch archsw; /* MI/MD interface boundary */ 37 38extern char end[]; 39extern char bootprog_name[]; 40extern char bootprog_rev[]; 41extern char bootprog_date[]; 42extern char bootprog_maker[]; 43 44u_int32_t acells; 45 46static char bootargs[128]; 47 48#define HEAP_SIZE 0x80000 49 50void 51init_heap(void) 52{ 53 void *base; 54 55 if ((base = ofw_alloc_heap(HEAP_SIZE)) == (void *)0xffffffff) { 56 printf("Heap memory claim failed!\n"); 57 OF_enter(); 58 } 59 60 setheap(base, (void *)((int)base + HEAP_SIZE)); 61} 62 63uint64_t 64memsize(void) 65{ 66 phandle_t memoryp; 67 struct ofw_reg reg[4]; 68 struct ofw_reg2 reg2[8]; 69 int i; 70 u_int64_t sz, memsz; 71 72 memoryp = OF_instance_to_package(memory); 73 74 if (acells == 1) { 75 sz = OF_getprop(memoryp, "reg", ®, sizeof(reg)); 76 sz /= sizeof(struct ofw_reg); 77 78 for (i = 0, memsz = 0; i < sz; i++) 79 memsz += reg[i].size; 80 } else if (acells == 2) { 81 sz = OF_getprop(memoryp, "reg", ®2, sizeof(reg2)); 82 sz /= sizeof(struct ofw_reg2); 83 84 for (i = 0, memsz = 0; i < sz; i++) 85 memsz += reg2[i].size; 86 } 87 88 return (memsz); 89} 90 91int 92main(int (*openfirm)(void *)) 93{ 94 phandle_t root; 95 int i; 96 char bootpath[64]; 97 char *ch; 98 int bargc; 99 char **bargv; 100 101 /* 102 * Initalise the Open Firmware routines by giving them the entry point. 103 */ 104 OF_init(openfirm); 105 106 root = OF_finddevice("/"); 107 108 acells = 1; 109 OF_getprop(root, "#address-cells", &acells, sizeof(acells)); 110 111 /* 112 * Set up console. 113 */ 114 cons_probe(); 115 116 /* 117 * Initialise the heap as early as possible. Once this is done, 118 * alloc() is usable. The stack is buried inside us, so this is 119 * safe. 120 */ 121 init_heap(); 122 123 /* 124 * March through the device switch probing for things. 125 */ 126 for (i = 0; devsw[i] != NULL; i++) 127 if (devsw[i]->dv_init != NULL) 128 (devsw[i]->dv_init)(); 129 130 printf("\n"); 131 printf("%s, Revision %s\n", bootprog_name, bootprog_rev); 132 printf("(%s, %s)\n", bootprog_maker, bootprog_date); 133 printf("Memory: %lldKB\n", memsize() / 1024); 134 135 OF_getprop(chosen, "bootpath", bootpath, 64); 136 ch = index(bootpath, ':'); 137 *ch = '\0'; 138 printf("Booted from: %s\n", bootpath); 139 140 printf("\n"); 141 142 /* 143 * Only parse the first bootarg if present. It should 144 * be simple to handle extra arguments 145 */ 146 OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs)); 147 bargc = 0; 148 parse(&bargc, &bargv, bootargs); 149 if (bargc == 1) 150 env_setenv("currdev", EV_VOLATILE, bargv[0], ofw_setcurrdev, 151 env_nounset); 152 else 153 env_setenv("currdev", EV_VOLATILE, bootpath, 154 ofw_setcurrdev, env_nounset); 155 env_setenv("loaddev", EV_VOLATILE, bootpath, env_noset, 156 env_nounset); 157 setenv("LINES", "24", 1); /* optional */ 158 159 archsw.arch_getdev = ofw_getdev; 160 archsw.arch_copyin = ofw_copyin; 161 archsw.arch_copyout = ofw_copyout; 162 archsw.arch_readin = ofw_readin; 163 archsw.arch_autoload = ofw_autoload; 164 165 interact(); /* doesn't return */ 166 167 OF_exit(); 168 169 return 0; 170} 171 172COMMAND_SET(halt, "halt", "halt the system", command_halt); 173 174static int 175command_halt(int argc, char *argv[]) 176{ 177 178 OF_exit(); 179 return (CMD_OK); 180} 181 182COMMAND_SET(memmap, "memmap", "print memory map", command_memmap); 183 184int 185command_memmap(int argc, char **argv) 186{ 187 188 ofw_memmap(acells); 189 return (CMD_OK); 190} 191