1 /* $OpenBSD: main.c,v 1.8 2013/03/21 21:51:00 deraadt Exp $ */ 2 /* $NetBSD: boot.c,v 1.1 1997/04/16 20:29:17 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1997 Jason R. Thorpe. All rights reserved. 6 * Copyright (C) 1995, 1996 Wolfgang Solfrank. 7 * Copyright (C) 1995, 1996 TooLs GmbH. 8 * All rights reserved. 9 * 10 * ELF support derived from NetBSD/alpha's boot loader, written 11 * by Christopher G. Demetriou. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 2. Redistributions in binary form must reproduce the above copyright 19 * notice, this list of conditions and the following disclaimer in the 20 * documentation and/or other materials provided with the distribution. 21 * 3. All advertising materials mentioning features or use of this software 22 * must display the following acknowledgement: 23 * This product includes software developed by TooLs GmbH. 24 * 4. The name of TooLs GmbH may not be used to endorse or promote products 25 * derived from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 30 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 31 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 33 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 34 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 35 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * First try for the boot code 41 * 42 * Input syntax is: 43 * [promdev[{:|,}partition]]/[filename] [flags] 44 */ 45 46 #define ELFSIZE 32 /* We use 32-bit ELF. */ 47 48 #include <sys/param.h> 49 #include <sys/exec.h> 50 #include <sys/exec_elf.h> 51 #include <sys/reboot.h> 52 #include <sys/disklabel.h> 53 54 #include <lib/libkern/libkern.h> 55 #include <lib/libsa/stand.h> 56 #include <lib/libsa/loadfile.h> 57 #include <stand/boot/cmd.h> 58 59 60 #include <machine/cpu.h> 61 62 #include <macppc/stand/ofdev.h> 63 #include <macppc/stand/openfirm.h> 64 65 char bootdev[128]; 66 int boothowto; 67 int debug; 68 69 70 void 71 get_alt_bootdev(char *, size_t, char *, size_t); 72 73 static void 74 prom2boot(char *dev) 75 { 76 char *cp, *lp = 0; 77 78 for (cp = dev; *cp; cp++) 79 if (*cp == ':') 80 lp = cp; 81 if (!lp) 82 lp = cp; 83 *lp = 0; 84 } 85 86 static void 87 chain(void (*entry)(), char *args, void *ssym, void *esym) 88 { 89 extern char end[]; 90 int l; 91 #ifdef __notyet__ 92 int machine_tag; 93 #endif 94 95 freeall(); 96 97 /* 98 * Stash pointer to end of symbol table after the argument 99 * strings. 100 */ 101 l = strlen(args) + 1; 102 bcopy(&ssym, args + l, sizeof(ssym)); 103 l += sizeof(ssym); 104 bcopy(&esym, args + l, sizeof(esym)); 105 l += sizeof(esym); 106 107 #ifdef __notyet__ 108 /* 109 * Tell the kernel we're an OpenFirmware system. 110 */ 111 machine_tag = POWERPC_MACHINE_OPENFIRMWARE; 112 bcopy(&machine_tag, args + l, sizeof(machine_tag)); 113 l += sizeof(machine_tag); 114 #endif 115 116 OF_chain((void *)RELOC, end - (char *)RELOC, entry, args, l); 117 panic("chain"); 118 } 119 120 /* 121 * XXX This limits the maximum size of the (uncompressed) bsd.rd to a 122 * little under 11MB. 123 */ 124 #define CLAIM_LIMIT 0x00c00000 125 126 char bootline[512]; 127 128 extern char *kernelfile; 129 int 130 main() 131 { 132 int chosen; 133 134 /* 135 * Get the boot arguments from Openfirmware 136 */ 137 if ((chosen = OF_finddevice("/chosen")) == -1 || 138 OF_getprop(chosen, "bootpath", bootdev, sizeof bootdev) < 0 || 139 OF_getprop(chosen, "bootargs", bootline, sizeof bootline) < 0) { 140 printf("Invalid Openfirmware environment\n"); 141 exit(); 142 } 143 prom2boot(bootdev); 144 get_alt_bootdev(bootdev, sizeof(bootdev), bootline, sizeof(bootline)); 145 if (bootline[0] != '\0') 146 kernelfile = bootline; 147 148 OF_claim((void *)0x00100000, CLAIM_LIMIT, 0); /* XXX */ 149 boot(0); 150 return 0; 151 } 152 153 void 154 get_alt_bootdev(char *dev, size_t devsz, char *line, size_t linesz) 155 { 156 char *p; 157 int len; 158 /* 159 * if the kernel image specified contains a ':' it is 160 * [device]:[kernel], so separate the two fields. 161 */ 162 p = strrchr(line, ':'); 163 if (p == NULL) 164 return; 165 /* user specified boot device for kernel */ 166 len = p - line + 1; /* str len plus nil */ 167 strlcpy(dev, line, len > devsz ? devsz : len); 168 169 strlcpy(line, p+1, linesz); /* rest of string after ':' */ 170 } 171 172 173 void 174 devboot(dev_t dev, char *p) 175 { 176 strlcpy(p, bootdev, BOOTDEVLEN); 177 } 178 179 int 180 run_loadfile(u_long *marks, int howto) 181 { 182 char bootline[512]; /* Should check size? */ 183 u_int32_t entry; 184 char *cp; 185 void *ssym, *esym; 186 187 strlcpy(bootline, opened_name, sizeof bootline); 188 cp = bootline + strlen(bootline); 189 *cp++ = ' '; 190 *cp = '-'; 191 if (howto & RB_ASKNAME) 192 *++cp = 'a'; 193 if (howto & RB_CONFIG) 194 *++cp = 'c'; 195 if (howto & RB_SINGLE) 196 *++cp = 's'; 197 if (howto & RB_KDB) 198 *++cp = 'd'; 199 if (*cp == '-') 200 *--cp = 0; 201 else 202 *++cp = 0; 203 204 entry = marks[MARK_ENTRY]; 205 ssym = (void *)marks[MARK_SYM]; 206 esym = (void *)marks[MARK_END]; 207 { 208 u_int32_t lastpage; 209 lastpage = roundup(marks[MARK_END], PAGE_SIZE); 210 OF_release((void*)lastpage, CLAIM_LIMIT - lastpage); 211 } 212 213 chain((void *)entry, bootline, ssym, esym); 214 215 _rtt(); 216 return 0; 217 } 218 219 int 220 cnspeed(dev_t dev, int sp) 221 { 222 return CONSPEED; 223 } 224 225 char ttyname_buf[8]; 226 227 char * 228 ttyname(int fd) 229 { 230 snprintf(ttyname_buf, sizeof ttyname_buf, "ofc0"); 231 return ttyname_buf; 232 } 233 234 dev_t 235 ttydev(char *name) 236 { 237 return makedev(0,0); 238 } 239