1*82357f6dSdsl /* $NetBSD: bootxx.c,v 1.33 2009/03/14 21:04:14 dsl Exp $ */
29bbac582Ssimonb
39bbac582Ssimonb /*-
49bbac582Ssimonb * Copyright (c) 1999 The NetBSD Foundation, Inc.
59bbac582Ssimonb * All rights reserved.
69bbac582Ssimonb *
79bbac582Ssimonb * This code is derived from software contributed to The NetBSD Foundation
89bbac582Ssimonb * by Jonathan Stone, Michael Hitch and Simon Burge.
99bbac582Ssimonb *
109bbac582Ssimonb * Redistribution and use in source and binary forms, with or without
119bbac582Ssimonb * modification, are permitted provided that the following conditions
129bbac582Ssimonb * are met:
139bbac582Ssimonb * 1. Redistributions of source code must retain the above copyright
149bbac582Ssimonb * notice, this list of conditions and the following disclaimer.
159bbac582Ssimonb * 2. Redistributions in binary form must reproduce the above copyright
169bbac582Ssimonb * notice, this list of conditions and the following disclaimer in the
179bbac582Ssimonb * documentation and/or other materials provided with the distribution.
189bbac582Ssimonb *
199bbac582Ssimonb * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
209bbac582Ssimonb * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
219bbac582Ssimonb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
229bbac582Ssimonb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
239bbac582Ssimonb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
249bbac582Ssimonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
259bbac582Ssimonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
269bbac582Ssimonb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
279bbac582Ssimonb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
289bbac582Ssimonb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
299bbac582Ssimonb * POSSIBILITY OF SUCH DAMAGE.
309bbac582Ssimonb */
319bbac582Ssimonb
329bbac582Ssimonb /*
339bbac582Ssimonb * Copyright (c) 1992, 1993
349bbac582Ssimonb * The Regents of the University of California. All rights reserved.
359bbac582Ssimonb *
369bbac582Ssimonb * This code is derived from software contributed to Berkeley by
379bbac582Ssimonb * Ralph Campbell.
389bbac582Ssimonb *
399bbac582Ssimonb * Redistribution and use in source and binary forms, with or without
409bbac582Ssimonb * modification, are permitted provided that the following conditions
419bbac582Ssimonb * are met:
429bbac582Ssimonb * 1. Redistributions of source code must retain the above copyright
439bbac582Ssimonb * notice, this list of conditions and the following disclaimer.
449bbac582Ssimonb * 2. Redistributions in binary form must reproduce the above copyright
459bbac582Ssimonb * notice, this list of conditions and the following disclaimer in the
469bbac582Ssimonb * documentation and/or other materials provided with the distribution.
47aad01611Sagc * 3. Neither the name of the University nor the names of its contributors
489bbac582Ssimonb * may be used to endorse or promote products derived from this software
499bbac582Ssimonb * without specific prior written permission.
509bbac582Ssimonb *
519bbac582Ssimonb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
529bbac582Ssimonb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
539bbac582Ssimonb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
549bbac582Ssimonb * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
559bbac582Ssimonb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
569bbac582Ssimonb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
579bbac582Ssimonb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
589bbac582Ssimonb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
599bbac582Ssimonb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
609bbac582Ssimonb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
619bbac582Ssimonb * SUCH DAMAGE.
629bbac582Ssimonb *
639bbac582Ssimonb * @(#)boot.c 8.1 (Berkeley) 6/10/93
649bbac582Ssimonb */
659bbac582Ssimonb
669bbac582Ssimonb #include <sys/param.h>
679bbac582Ssimonb #include <sys/exec_elf.h>
68c3c29ad5Ssimonb #include <lib/libsa/stand.h>
694f2661e4Sjdolecek #include <lib/libkern/libkern.h>
709bbac582Ssimonb #include <machine/dec_prom.h>
719bbac582Ssimonb
7202cdf4d2Sdsl typedef void (*entrypt)(int, char **, int, const void *);
739bbac582Ssimonb
7402cdf4d2Sdsl int main(int, char **);
7502cdf4d2Sdsl entrypt loadfile(char *path, char *name);
769bbac582Ssimonb
7702cdf4d2Sdsl extern int clear_cache(char *addr, int len);
789bbac582Ssimonb
799bbac582Ssimonb /*
809bbac582Ssimonb * This gets arguments from the PROM, calls other routines to open
819bbac582Ssimonb * and load the secondary boot loader called boot, and then transfers
829bbac582Ssimonb * execution to that program.
839bbac582Ssimonb *
849bbac582Ssimonb * Argv[0] should be something like "rz(0,0,0)netbsd" on a DECstation 3100.
859bbac582Ssimonb * Argv[0,1] should be something like "boot 5/rz0/netbsd" on a DECstation 5000.
869bbac582Ssimonb * The argument "-a" means netbsd should do an automatic reboot.
879bbac582Ssimonb */
889bbac582Ssimonb int
main(int argc,char ** argv)89454af1c0Sdsl main(int argc, char **argv)
909bbac582Ssimonb {
919bbac582Ssimonb char *cp;
929bbac582Ssimonb entrypt entry;
939bbac582Ssimonb
949bbac582Ssimonb /* check for DS5000 boot */
959bbac582Ssimonb if (strcmp(argv[0], "boot") == 0) {
969bbac582Ssimonb argc--;
979bbac582Ssimonb argv++;
989bbac582Ssimonb }
999bbac582Ssimonb cp = *argv;
1009bbac582Ssimonb
101c3c29ad5Ssimonb printf("\nNetBSD/pmax " NETBSD_VERS " " BOOTXX_FS_NAME " Primary Bootstrap\n");
1029bbac582Ssimonb
103c3c29ad5Ssimonb entry = loadfile(cp, "/boot.pmax");
104c3c29ad5Ssimonb if ((int)entry != -1)
105c3c29ad5Ssimonb goto goodload;
106c3c29ad5Ssimonb
107c3c29ad5Ssimonb /* Give old /boot a go... */
108c3c29ad5Ssimonb entry = loadfile(cp, "/boot");
109c3c29ad5Ssimonb if ((int)entry != -1)
110c3c29ad5Ssimonb goto goodload;
111c3c29ad5Ssimonb
112c3c29ad5Ssimonb /* Booting off an 8.3 filesystem? */
113c3c29ad5Ssimonb entry = loadfile(cp, "/boot.pma");
114c3c29ad5Ssimonb if ((int)entry != -1)
115c3c29ad5Ssimonb goto goodload;
116c3c29ad5Ssimonb
117c3c29ad5Ssimonb goto bad;
118c3c29ad5Ssimonb goodload:
119c3c29ad5Ssimonb
120c3c29ad5Ssimonb clear_cache((char *)PRIMARY_LOAD_ADDRESS, 1024 * 1024);
1219bbac582Ssimonb if (callv == &callvec)
1229bbac582Ssimonb entry(argc, argv, 0, 0);
1239bbac582Ssimonb else
1249bbac582Ssimonb entry(argc, argv, DEC_PROM_MAGIC, callv);
125c3c29ad5Ssimonb bad:
126c3c29ad5Ssimonb /* XXX would calling prom_halt here be cleaner? */
1279bbac582Ssimonb return (1);
1289bbac582Ssimonb }
1299bbac582Ssimonb
1309bbac582Ssimonb /*
1319bbac582Ssimonb * Open 'filename', read in program and return the entry point or -1 if error.
1329bbac582Ssimonb */
1339bbac582Ssimonb entrypt
loadfile(char * path,char * name)134*82357f6dSdsl loadfile(char *path, char *name)
1359bbac582Ssimonb {
1369bbac582Ssimonb int fd, i;
1379bbac582Ssimonb char c, *buf, bootfname[64];
1389bbac582Ssimonb Elf32_Ehdr ehdr;
1399bbac582Ssimonb Elf32_Phdr phdr;
1409bbac582Ssimonb
141c3c29ad5Ssimonb strcpy(bootfname, path);
1429bbac582Ssimonb buf = bootfname;
1439bbac582Ssimonb while ((c = *buf++) != '\0') {
1449bbac582Ssimonb if (c == ')')
1459bbac582Ssimonb break;
1469bbac582Ssimonb if (c != '/')
1479bbac582Ssimonb continue;
1489bbac582Ssimonb while ((c = *buf++) != '\0')
1499bbac582Ssimonb if (c == '/')
1509bbac582Ssimonb break;
1519bbac582Ssimonb /*
1529bbac582Ssimonb * Make "N/rzY" with no trailing '/' valid by adding
153c3c29ad5Ssimonb * the extra '/' before appending 'bootpmax' to the path.
1549bbac582Ssimonb */
1559bbac582Ssimonb if (c != '/') {
1569bbac582Ssimonb buf--;
1579bbac582Ssimonb *buf++ = '/';
1589bbac582Ssimonb *buf = '\0';
1599bbac582Ssimonb }
1609bbac582Ssimonb break;
1619bbac582Ssimonb }
162c3c29ad5Ssimonb strcpy(buf, name);
1639bbac582Ssimonb if ((fd = open(bootfname, 0)) < 0) {
1649bbac582Ssimonb printf("open %s: %d\n", bootfname, errno);
1659bbac582Ssimonb goto err;
1669bbac582Ssimonb }
1679bbac582Ssimonb
1689bbac582Ssimonb /* read the exec header */
1699bbac582Ssimonb i = read(fd, (char *)&ehdr, sizeof(ehdr));
1709bbac582Ssimonb if ((i != sizeof(ehdr)) ||
17172a4c73dShe (memcmp(ehdr.e_ident, ELFMAG, SELFMAG) != 0) ||
1729bbac582Ssimonb (ehdr.e_ident[EI_CLASS] != ELFCLASS32)) {
1739bbac582Ssimonb printf("%s: No ELF header\n", bootfname);
1749bbac582Ssimonb goto cerr;
1759bbac582Ssimonb }
1769bbac582Ssimonb
1779bbac582Ssimonb for (i = 0; i < ehdr.e_phnum; i++) {
1789bbac582Ssimonb if (lseek(fd, (off_t) ehdr.e_phoff + i * sizeof(phdr), 0) < 0)
1799bbac582Ssimonb goto cerr;
1809bbac582Ssimonb if (read(fd, &phdr, sizeof(phdr)) != sizeof(phdr))
1819bbac582Ssimonb goto cerr;
1829bbac582Ssimonb if (phdr.p_type != PT_LOAD)
1839bbac582Ssimonb continue;
1849bbac582Ssimonb if (lseek(fd, (off_t)phdr.p_offset, 0) < 0)
1859bbac582Ssimonb goto cerr;
1869bbac582Ssimonb if (read(fd, (char *)phdr.p_paddr, phdr.p_filesz) != phdr.p_filesz)
1879bbac582Ssimonb goto cerr;
1889bbac582Ssimonb }
1899bbac582Ssimonb return ((entrypt)ehdr.e_entry);
1909bbac582Ssimonb
1919bbac582Ssimonb cerr:
1929bbac582Ssimonb #ifndef LIBSA_NO_FS_CLOSE
1939bbac582Ssimonb (void) close(fd);
1949bbac582Ssimonb #endif
1959bbac582Ssimonb err:
1969bbac582Ssimonb printf("Can't load '%s'\n", bootfname);
1979bbac582Ssimonb return ((entrypt)-1);
1989bbac582Ssimonb }
199