xref: /netbsd-src/sys/arch/mips/pmon/pmon.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$OpenBSD: pmon.c,v 1.4 2010/02/16 21:29:54 miod Exp $	*/
2 
3 /*
4  * Copyright (c) 2009 Miodrag Vallat.
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/param.h>
20 #include <sys/systm.h>
21 #include <sys/proc.h>
22 
23 #include <machine/cpu.h>
24 #include <mips/pmon/pmon.h>
25 
26 int	pmon_argc;
27 int32_t	*pmon_argv;
28 int32_t	*pmon_envp;
29 
30 void
31 pmon_init(int32_t argc, int32_t argv, int32_t envp, int32_t callvec)
32 {
33 	pmon_callvec = callvec;
34 
35 	pmon_argc = argc;
36 	/* sign extend pointers */
37 	pmon_argv = (int32_t *)(vaddr_t)argv;
38 	pmon_envp = (int32_t *)(vaddr_t)envp;
39 }
40 
41 const char *
42 pmon_getarg(const int argno)
43 {
44 	if (argno < 0 || argno >= pmon_argc)
45 		return NULL;
46 
47 	return (const char *)(vaddr_t)pmon_argv[argno];
48 }
49 
50 const char *
51 pmon_getenv(const char *var)
52 {
53 	int32_t *envptr = pmon_envp;
54 	const char *envstr;
55 	size_t varlen;
56 
57 	if (envptr == NULL)
58 		return NULL;
59 
60 	varlen = strlen(var);
61 	while (*envptr != 0) {
62 		envstr = (const char *)(vaddr_t)*envptr;
63 		/*
64 		 * There is a PMON2000 bug, at least on Lemote Yeeloong,
65 		 * which causes it to override part of the environment
66 		 * pointers array with the environment data itself.
67 		 *
68 		 * This only happens on cold boot, and if the BSD kernel
69 		 * is loaded without symbols (i.e. no option -k passed
70 		 * to the boot command).
71 		 *
72 		 * Until a suitable workaround is found or the bug is
73 		 * fixed, ignore broken environment information and
74 		 * tell the user (in case this prevents us from finding
75 		 * important information).
76 		 */
77 		if ((vaddr_t)envstr < (vaddr_t)MIPS_KSEG1_START ||
78 		    (vaddr_t)envstr >= (vaddr_t)MIPS_KSEG2_START) {
79 			printf("WARNING! CORRUPTED ENVIRONMENT!\n");
80 			printf("Unable to search for %s.\n", var);
81 #ifdef _STANDALONE
82 			printf("If boot fails, power-cycle the machine.\n");
83 #else
84 			printf("If the kernel fails to identify the system"
85 			    " type, please boot it again with `-k' option.\n");
86 #endif
87 
88 			/* terminate environment for further calls */
89 			*envptr = 0;
90 			break;
91 		}
92 		if (strncmp(envstr, var, varlen) == 0 &&
93 		    envstr[varlen] == '=')
94 			return envstr + varlen + 1;
95 		envptr++;
96 	}
97 
98 	return NULL;
99 }
100