1 /* $NetBSD: boot.c,v 1.19 2011/01/22 19:19:21 joerg Exp $ */ 2 3 /*- 4 * Copyright (C) 1999 Tsubai Masanari. 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 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <lib/libkern/libkern.h> 30 #include <lib/libsa/stand.h> 31 #include <lib/libsa/loadfile.h> 32 33 #include <machine/apcall.h> 34 #include <machine/bootinfo.h> 35 #include <machine/cpu.h> 36 #include <machine/romcall.h> 37 38 void boot(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t); 39 40 void mips1_flushicache(void *, int); 41 extern char _edata[], _end[]; 42 43 /* version strings in vers.c (generated by newvers.sh) */ 44 extern const char bootprog_name[]; 45 extern const char bootprog_rev[]; 46 47 struct apbus_sysinfo *_sip; 48 int apbus; 49 50 char *devs[] = { "sd", "fh", "fd", NULL, NULL, "rd", "st" }; 51 char *kernels[] = { "/netbsd", "/netbsd.gz", NULL }; 52 53 #ifdef BOOT_DEBUG 54 # define DPRINTF printf 55 #else 56 # define DPRINTF while (0) printf 57 #endif 58 59 void 60 boot(uint32_t a0, uint32_t a1, uint32_t a2, uint32_t a3, uint32_t a4, 61 uint32_t a5) 62 { 63 int fd, i; 64 char *netbsd = ""; 65 int maxmem; 66 u_long marks[MARK_MAX]; 67 char devname[32], file[32]; 68 void (*entry)(uint32_t, uint32_t, uint32_t, uint32_t, uint32_t, 69 uint32_t); 70 struct btinfo_symtab bi_sym; 71 struct btinfo_bootarg bi_arg; 72 struct btinfo_bootpath bi_bpath; 73 struct btinfo_systype bi_sys; 74 int loadflag; 75 76 /* Clear BSS. */ 77 memset(_edata, 0, _end - _edata); 78 79 /* 80 * XXX a3 contains: 81 * maxmem (nws-3xxx) 82 * argv (apbus-based machine) 83 */ 84 if (a3 >= 0x80000000) 85 apbus = 1; 86 else 87 apbus = 0; 88 89 if (apbus) 90 _sip = (void *)a4; 91 92 printf("%s Secondary Boot, Revision %s\n", 93 bootprog_name, bootprog_rev); 94 95 if (apbus) { 96 char *bootdev = (char *)a1; 97 int argc = a2; 98 char **argv = (char **)a3; 99 100 DPRINTF("APbus-based system\n"); 101 102 DPRINTF("argc = %d\n", argc); 103 for (i = 0; i < argc; i++) { 104 DPRINTF("argv[%d] = %s\n", i, argv[i]); 105 if (argv[i][0] != '-' && *netbsd == 0) 106 netbsd = argv[i]; 107 } 108 maxmem = _sip->apbsi_memsize; 109 maxmem -= 0x100000; /* reserve 1MB for ROM monitor */ 110 111 DPRINTF("howto = 0x%x\n", a0); 112 DPRINTF("bootdev = %s\n", (char *)a1); 113 DPRINTF("bootname = %s\n", netbsd); 114 DPRINTF("maxmem = 0x%x\n", maxmem); 115 116 /* XXX use "sonic()" instead of "tftp()" */ 117 if (strncmp(bootdev, "tftp", 4) == 0) 118 bootdev = "sonic"; 119 120 strcpy(devname, bootdev); 121 if (strchr(devname, '(') == NULL) 122 strcat(devname, "()"); 123 } else { 124 int bootdev = a1; 125 char *bootname = (char *)a2; 126 int ctlr, unit, part, type; 127 128 DPRINTF("HB system.\n"); 129 130 /* bootname is "/boot" by default on HB system. */ 131 if (bootname && strcmp(bootname, "/boot") != 0) 132 netbsd = bootname; 133 maxmem = a3; 134 135 DPRINTF("howto = 0x%x\n", a0); 136 DPRINTF("bootdev = 0x%x\n", a1); 137 DPRINTF("bootname = %s\n", netbsd); 138 DPRINTF("maxmem = 0x%x\n", maxmem); 139 140 ctlr = BOOTDEV_CTLR(bootdev); 141 unit = BOOTDEV_UNIT(bootdev); 142 part = BOOTDEV_PART(bootdev); 143 type = BOOTDEV_TYPE(bootdev); 144 145 if (devs[type] == NULL) { 146 printf("unknown bootdev (0x%x)\n", bootdev); 147 _rtt(); 148 } 149 150 sprintf(devname, "%s(%d,%d,%d)", devs[type], ctlr, unit, part); 151 } 152 153 printf("Booting %s%s\n", devname, netbsd); 154 155 /* use user specified kernel name if exists */ 156 if (*netbsd) { 157 kernels[0] = netbsd; 158 kernels[1] = NULL; 159 } 160 161 loadflag = LOAD_KERNEL; 162 if (devname[0] == 'f') /* XXX */ 163 loadflag &= ~LOAD_BACKWARDS; 164 165 marks[MARK_START] = 0; 166 167 for (i = 0; kernels[i]; i++) { 168 sprintf(file, "%s%s", devname, kernels[i]); 169 DPRINTF("trying %s...\n", file); 170 fd = loadfile(file, marks, loadflag); 171 if (fd != -1) 172 break; 173 } 174 if (kernels[i] == NULL) 175 _rtt(); 176 177 DPRINTF("entry = 0x%x\n", (int)marks[MARK_ENTRY]); 178 DPRINTF("ssym = 0x%x\n", (int)marks[MARK_SYM]); 179 DPRINTF("esym = 0x%x\n", (int)marks[MARK_END]); 180 181 bi_init(BOOTINFO_ADDR); 182 183 bi_sym.nsym = marks[MARK_NSYM]; 184 bi_sym.ssym = marks[MARK_SYM]; 185 bi_sym.esym = marks[MARK_END]; 186 bi_add(&bi_sym, BTINFO_SYMTAB, sizeof(bi_sym)); 187 188 bi_arg.howto = a0; 189 bi_arg.bootdev = a1; 190 bi_arg.maxmem = maxmem; 191 bi_arg.sip = (int)_sip; 192 bi_add(&bi_arg, BTINFO_BOOTARG, sizeof(bi_arg)); 193 194 strcpy(bi_bpath.bootpath, file); 195 bi_add(&bi_bpath, BTINFO_BOOTPATH, sizeof(bi_bpath)); 196 197 bi_sys.type = apbus ? NEWS5000 : NEWS3400; /* XXX */ 198 bi_add(&bi_sys, BTINFO_SYSTYPE, sizeof(bi_sys)); 199 200 entry = (void *)marks[MARK_ENTRY]; 201 202 if (apbus) 203 apcall_flushcache(); 204 else 205 mips1_flushicache(entry, marks[MARK_SYM] - marks[MARK_ENTRY]); 206 207 printf("\n"); 208 (*entry)(a0, a1, a2, a3, a4, a5); 209 } 210 211 void 212 putchar(int x) 213 { 214 char c = x; 215 216 if (apbus) 217 apcall_write(1, &c, 1); 218 else 219 rom_write(1, &c, 1); 220 } 221 222 int 223 getchar(void) 224 { 225 unsigned char c = '\0'; 226 int i; 227 228 for (;;) { 229 i = apbus ? apcall_read(1, &c, 1) : rom_read(1, &c, 1); 230 if (i == 1) 231 break; 232 if (i != -2 && i != 0) 233 return -1; 234 } 235 return c; 236 } 237 238 void 239 _rtt(void) 240 { 241 if (apbus) 242 apcall_exit(8); 243 else 244 rom_halt(); 245 246 for (;;); 247 } 248