xref: /titanic_51/usr/src/psm/stand/boot/sparc/common/inetboot.c (revision 75e04b8fa133ad94dc80199131b8ec3e2729bdaf)
1986fd29aSsetje /*
2986fd29aSsetje  * CDDL HEADER START
3986fd29aSsetje  *
4986fd29aSsetje  * The contents of this file are subject to the terms of the
5986fd29aSsetje  * Common Development and Distribution License (the "License").
6986fd29aSsetje  * You may not use this file except in compliance with the License.
7986fd29aSsetje  *
8986fd29aSsetje  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9986fd29aSsetje  * or http://www.opensolaris.org/os/licensing.
10986fd29aSsetje  * See the License for the specific language governing permissions
11986fd29aSsetje  * and limitations under the License.
12986fd29aSsetje  *
13986fd29aSsetje  * When distributing Covered Code, include this CDDL HEADER in each
14986fd29aSsetje  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15986fd29aSsetje  * If applicable, add the following below this CDDL HEADER, with the
16986fd29aSsetje  * fields enclosed by brackets "[]" replaced with your own identifying
17986fd29aSsetje  * information: Portions Copyright [yyyy] [name of copyright owner]
18986fd29aSsetje  *
19986fd29aSsetje  * CDDL HEADER END
20986fd29aSsetje  */
21986fd29aSsetje /*
22*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23986fd29aSsetje  * Use is subject to license terms.
24986fd29aSsetje  */
25986fd29aSsetje 
26986fd29aSsetje 
27986fd29aSsetje #include <sys/types.h>
28986fd29aSsetje #include <sys/param.h>
29986fd29aSsetje #include <sys/fcntl.h>
30986fd29aSsetje #include <sys/obpdefs.h>
31986fd29aSsetje #include <sys/reboot.h>
32986fd29aSsetje #include <sys/promif.h>
33986fd29aSsetje #include <sys/stat.h>
34986fd29aSsetje #include <sys/bootvfs.h>
35986fd29aSsetje #include <sys/platnames.h>
36986fd29aSsetje #include <sys/salib.h>
37986fd29aSsetje #include <sys/elf.h>
38986fd29aSsetje #include <sys/link.h>
39986fd29aSsetje #include <sys/auxv.h>
40986fd29aSsetje #include <sys/boot_policy.h>
41986fd29aSsetje #include <sys/boot_redirect.h>
42986fd29aSsetje #include <sys/bootconf.h>
43986fd29aSsetje #include <sys/boot.h>
44986fd29aSsetje #include "boot_plat.h"
45986fd29aSsetje #include "ramdisk.h"
46986fd29aSsetje 
47986fd29aSsetje #define	SUCCESS		0
48986fd29aSsetje #define	FAILURE		-1
49986fd29aSsetje 
50986fd29aSsetje #ifdef DEBUG
51986fd29aSsetje extern int debug = 0;
52986fd29aSsetje #else
53986fd29aSsetje static const int debug = 0;
54986fd29aSsetje #endif
55986fd29aSsetje 
56986fd29aSsetje #define	dprintf		if (debug) printf
57986fd29aSsetje 
58986fd29aSsetje char		*def_boot_archive = "boot_archive";
59986fd29aSsetje char		*def_miniroot = "miniroot";
60986fd29aSsetje extern char	cmd_line_boot_archive[];
61986fd29aSsetje 
62986fd29aSsetje extern int	openfile(char *filename);
63986fd29aSsetje 
64986fd29aSsetje static int
65986fd29aSsetje read_and_boot_ramdisk(int fd)
66986fd29aSsetje {
67986fd29aSsetje 	struct stat	st;
68986fd29aSsetje 	caddr_t		virt;
69986fd29aSsetje 	size_t		size;
70986fd29aSsetje 	extern		ssize_t xread(int, char *, size_t);
71986fd29aSsetje 
72986fd29aSsetje 	if ((fstat(fd, &st) != 0) ||
73986fd29aSsetje 	    ((virt = create_ramdisk(RD_ROOTFS, st.st_size, NULL)) == NULL))
74986fd29aSsetje 		return (-1);
75986fd29aSsetje 
76986fd29aSsetje 	dprintf("reading boot archive ...\n");
77986fd29aSsetje 	if ((size = xread(fd, (char *)virt, st.st_size)) != st.st_size) {
78986fd29aSsetje 		(void) printf("Error reading boot archive, bytes read = %ld, "
79986fd29aSsetje 		    "filesize = %ld\n", (long)size, (long)st.st_size);
80986fd29aSsetje 		destroy_ramdisk(RD_ROOTFS);
81986fd29aSsetje 		return (-1);
82986fd29aSsetje 	}
83986fd29aSsetje 
84986fd29aSsetje 	boot_ramdisk(RD_ROOTFS);
85986fd29aSsetje 	/* NOT REACHED */
86986fd29aSsetje 	return (0);	/* to make cc happy */
87986fd29aSsetje }
88986fd29aSsetje 
89986fd29aSsetje 
90986fd29aSsetje static void
91986fd29aSsetje post_mountroot_nfs(void)
92986fd29aSsetje {
93986fd29aSsetje 	int	fd;
94986fd29aSsetje 	char	*fn;
95986fd29aSsetje 	char	tmpname[MAXPATHLEN];
96986fd29aSsetje 
97986fd29aSsetje 	for (;;) {
98986fd29aSsetje 		fn = NULL;
99986fd29aSsetje 		if (boothowto & RB_ASKNAME) {
100*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 			char ctmpname[MAXPATHLEN];
101*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 
102986fd29aSsetje 			fn = (cmd_line_boot_archive[0] != '\0') ?
103986fd29aSsetje 			    cmd_line_boot_archive : def_boot_archive;
104*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 
105*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 			/* Avoid buffer overrun */
106*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 			(void) strncpy(tmpname, fn, strlen(fn)+1);
107986fd29aSsetje 			fn = tmpname;
108*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 
109*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 			printf("Enter filename [%s]: ", fn);
110*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 			(void) cons_gets(ctmpname, sizeof (ctmpname));
111*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 			if (ctmpname[0] != '\0') {
112*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 				(void) strncpy(tmpname, ctmpname,
113*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 				    strlen(ctmpname)+1);
114*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 				fn = tmpname;
115*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 			}
116986fd29aSsetje 		}
117986fd29aSsetje 
118986fd29aSsetje 		if (boothowto & RB_HALT) {
119986fd29aSsetje 			printf("Boot halted.\n");
120986fd29aSsetje 			prom_enter_mon();
121986fd29aSsetje 		}
122986fd29aSsetje 
123*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 
124*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 		if (fn != NULL) {
125986fd29aSsetje 			fd = openfile(fn);
126*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 		} else if (cmd_line_boot_archive[0] != '\0') {
127*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 			(void) strncpy(tmpname, cmd_line_boot_archive,
128*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 			    strlen(cmd_line_boot_archive)+1);
129*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 			fn = tmpname;
130986fd29aSsetje 			fd = openfile(fn);
131986fd29aSsetje 		} else {
132*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 			(void) strncpy(tmpname, def_boot_archive,
133*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 			    strlen(def_boot_archive)+1);
134*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 			fn = tmpname;
135986fd29aSsetje 			if ((fd = openfile(fn)) == FAILURE) {
136*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 				(void) strncpy(tmpname, def_miniroot,
137*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 				    strlen(def_miniroot)+1);
138*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 				fn = tmpname;
139986fd29aSsetje 				fd = openfile(fn);
140986fd29aSsetje 			}
141986fd29aSsetje 		}
142986fd29aSsetje 
143*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 		if (fn != tmpname || tmpname[0] == '\0') {
144*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 			printf("Possible buffer overrun, "
145*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 			    "entering boot prompt\n");
146*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 			prom_enter_mon();
147*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 		}
148*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 
149*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 
150986fd29aSsetje 		if (fd == FAILURE) {
151*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 			if (strncmp(fn, def_miniroot,
152*75e04b8fSphaniram rampura krishnamurthy - Sun Microsystems - Bangalore India 			    strlen(def_miniroot)+1) != 0)
153986fd29aSsetje 				printf("cannot open %s\n", fn);
154986fd29aSsetje 			else
155986fd29aSsetje 				printf("cannot open neither %s nor %s\n",
156986fd29aSsetje 				    def_boot_archive, def_miniroot);
157986fd29aSsetje 		} else {
158986fd29aSsetje 			/*
159986fd29aSsetje 			 * this function does not return if successful.
160986fd29aSsetje 			 */
161986fd29aSsetje 			(void) read_and_boot_ramdisk(fd);
162986fd29aSsetje 
163986fd29aSsetje 			printf("boot failed\n");
164986fd29aSsetje 			(void) close(fd);
165986fd29aSsetje 		}
166986fd29aSsetje 		boothowto |= RB_ASKNAME;
167986fd29aSsetje 	}
168986fd29aSsetje }
169986fd29aSsetje 
170986fd29aSsetje 
171986fd29aSsetje /*
172986fd29aSsetje  * bpath is the boot device path buffer.
173986fd29aSsetje  * bargs is the boot arguments buffer.
174986fd29aSsetje  */
175986fd29aSsetje /*ARGSUSED*/
176986fd29aSsetje int
177986fd29aSsetje bootprog(char *bpath, char *bargs, boolean_t user_specified_filename)
178986fd29aSsetje {
179986fd29aSsetje 	systype = set_fstype(v2path, bpath);
180986fd29aSsetje 
181986fd29aSsetje 	if (verbosemode) {
182986fd29aSsetje 		printf("device path '%s'\n", bpath);
183986fd29aSsetje 		if (strcmp(bpath, v2path) != 0)
184986fd29aSsetje 			printf("client path '%s'\n", v2path);
185986fd29aSsetje 	}
186986fd29aSsetje 
187986fd29aSsetje 	if (mountroot(bpath) != SUCCESS)
188986fd29aSsetje 		prom_panic("Could not mount filesystem.");
189986fd29aSsetje 
190986fd29aSsetje 	/*
191986fd29aSsetje 	 * kernname (default-name) might have changed if mountroot() called
192986fd29aSsetje 	 * boot_nfs_mountroot(), and it called set_default_filename().
193986fd29aSsetje 	 */
194986fd29aSsetje 	if (!user_specified_filename)
195986fd29aSsetje 		(void) strcpy(filename, kernname);
196986fd29aSsetje 
197986fd29aSsetje 	if (verbosemode)
198986fd29aSsetje 		printf("standalone = `%s', args = `%s'\n", filename, bargs);
199986fd29aSsetje 
200986fd29aSsetje 	set_client_bootargs(filename, bargs);
201986fd29aSsetje 
202986fd29aSsetje 	post_mountroot_nfs();
203986fd29aSsetje 
204986fd29aSsetje 	return (1);
205986fd29aSsetje }
206