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$"); 30 31 #include <sys/endian.h> 32 33 #include <stand.h> 34 #include "openfirm.h" 35 #include "libofw.h" 36 #include "bootstrap.h" 37 38 #include <machine/asm.h> 39 #include <machine/psl.h> 40 41 struct arch_switch archsw; /* MI/MD interface boundary */ 42 43 extern char end[]; 44 45 uint32_t acells, scells; 46 47 static char bootargs[128]; 48 49 #define HEAP_SIZE 0x800000 50 static char heap[HEAP_SIZE]; // In BSS, so uses no space 51 52 #define OF_puts(fd, text) OF_write(fd, text, strlen(text)) 53 54 static __inline register_t 55 mfmsr(void) 56 { 57 register_t value; 58 59 __asm __volatile ("mfmsr %0" : "=r"(value)); 60 61 return (value); 62 } 63 64 void 65 init_heap(void) 66 { 67 bzero(heap, HEAP_SIZE); 68 69 setheap(heap, (void *)((uintptr_t)heap + HEAP_SIZE)); 70 } 71 72 uint64_t 73 memsize(void) 74 { 75 phandle_t memoryp; 76 cell_t reg[24]; 77 int i, sz; 78 uint64_t memsz; 79 80 memsz = 0; 81 memoryp = OF_instance_to_package(memory); 82 83 sz = OF_getencprop(memoryp, "reg", ®[0], sizeof(reg)); 84 sz /= sizeof(reg[0]); 85 86 for (i = 0; i < sz; i += (acells + scells)) { 87 if (scells > 1) 88 memsz += (uint64_t)reg[i + acells] << 32; 89 memsz += reg[i + acells + scells - 1]; 90 } 91 92 return (memsz); 93 } 94 95 #ifdef CAS 96 extern int ppc64_cas(void); 97 98 static int 99 ppc64_autoload(void) 100 { 101 const char *cas; 102 103 if ((cas = getenv("cas")) && cas[0] == '1') 104 if (ppc64_cas() != 0) 105 return (-1); 106 return (ofw_autoload()); 107 } 108 #endif 109 110 #if BYTE_ORDER == LITTLE_ENDIAN 111 /* 112 * In Little-endian, we cannot just branch to the client interface. Since 113 * the client interface is big endian, we have to rfid to it. 114 * Likewise, when execution resumes, we are in the wrong endianness so 115 * we must do a fixup before returning to the caller. 116 */ 117 static int (*openfirmware_entry)(void *); 118 extern int openfirmware_trampoline(void *buf, int (*cb)(void *)); 119 120 /* 121 * Wrapper to pass the real entry point to our trampoline. 122 */ 123 static int 124 openfirmware_docall(void *buf) 125 { 126 return openfirmware_trampoline(buf, openfirmware_entry); 127 } 128 #endif 129 130 int 131 main(int (*openfirm)(void *)) 132 { 133 phandle_t root; 134 int i; 135 char bootpath[64]; 136 char *ch; 137 int bargc; 138 char **bargv; 139 140 /* 141 * Initialise the Open Firmware routines by giving them the entry point. 142 */ 143 #if BYTE_ORDER == LITTLE_ENDIAN 144 /* 145 * Use a trampoline entry point for endian fixups. 146 */ 147 openfirmware_entry = openfirm; 148 OF_init(openfirmware_docall); 149 #else 150 OF_init(openfirm); 151 #endif 152 153 root = OF_finddevice("/"); 154 155 scells = acells = 1; 156 OF_getencprop(root, "#address-cells", &acells, sizeof(acells)); 157 OF_getencprop(root, "#size-cells", &scells, sizeof(scells)); 158 159 /* 160 * Initialise the heap as early as possible. Once this is done, 161 * alloc() is usable. The stack is buried inside us, so this is 162 * safe. 163 */ 164 init_heap(); 165 166 /* 167 * Set up console. 168 */ 169 cons_probe(); 170 171 archsw.arch_getdev = ofw_getdev; 172 archsw.arch_copyin = ofw_copyin; 173 archsw.arch_copyout = ofw_copyout; 174 archsw.arch_readin = ofw_readin; 175 #ifdef CAS 176 setenv("cas", "1", 0); 177 archsw.arch_autoload = ppc64_autoload; 178 #else 179 archsw.arch_autoload = ofw_autoload; 180 #endif 181 182 /* Set up currdev variable to have hooks in place. */ 183 env_setenv("currdev", EV_VOLATILE, "", ofw_setcurrdev, env_nounset); 184 185 /* 186 * March through the device switch probing for things. 187 */ 188 for (i = 0; devsw[i] != NULL; i++) 189 if (devsw[i]->dv_init != NULL) 190 (devsw[i]->dv_init)(); 191 192 printf("\n%s", bootprog_info); 193 printf("Memory: %lldKB\n", memsize() / 1024); 194 195 OF_getprop(chosen, "bootpath", bootpath, 64); 196 ch = strchr(bootpath, ':'); 197 *ch = '\0'; 198 printf("Booted from: %s\n", bootpath); 199 200 printf("\n"); 201 202 /* 203 * Only parse the first bootarg if present. It should 204 * be simple to handle extra arguments 205 */ 206 OF_getprop(chosen, "bootargs", bootargs, sizeof(bootargs)); 207 bargc = 0; 208 parse(&bargc, &bargv, bootargs); 209 if (bargc == 1) 210 env_setenv("currdev", EV_VOLATILE, bargv[0], ofw_setcurrdev, 211 env_nounset); 212 else 213 env_setenv("currdev", EV_VOLATILE, bootpath, 214 ofw_setcurrdev, env_nounset); 215 env_setenv("loaddev", EV_VOLATILE, bootpath, env_noset, 216 env_nounset); 217 setenv("LINES", "24", 1); /* optional */ 218 219 /* 220 * On non-Apple hardware, where it works reliably, pass flattened 221 * device trees to the kernel by default instead of OF CI pointers. 222 * Apple hardware is the only virtual-mode OF implementation in 223 * existence, so far as I am aware, so use that as a flag. 224 */ 225 if (!(mfmsr() & PSL_DR)) 226 setenv("usefdt", "1", 1); 227 228 interact(); /* doesn't return */ 229 230 OF_exit(); 231 232 return 0; 233 } 234 235 COMMAND_SET(halt, "halt", "halt the system", command_halt); 236 237 static int 238 command_halt(int argc, char *argv[]) 239 { 240 241 OF_exit(); 242 return (CMD_OK); 243 } 244 245 COMMAND_SET(memmap, "memmap", "print memory map", command_memmap); 246 247 int 248 command_memmap(int argc, char **argv) 249 { 250 251 ofw_memmap(acells); 252 return (CMD_OK); 253 } 254