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/cpu.h>
22 #include <sys/proc.h>
23 
24 #include <mips/cpuregs.h>
25 #include <mips/pmon/pmon.h>
26 
27 int       pmon_argc;
28 int32_t   *pmon_argv;
29 int32_t   *pmon_envp;
30 
31 void
pmon_init(int32_t argc,int32_t argv,int32_t envp,int32_t callvec)32 pmon_init(int32_t argc, int32_t argv, int32_t envp, int32_t callvec)
33 {
34           pmon_callvec = callvec;
35 
36           pmon_argc = argc;
37           /* sign extend pointers */
38           pmon_argv = (int32_t *)(vaddr_t)argv;
39           pmon_envp = (int32_t *)(vaddr_t)envp;
40 }
41 
42 const char *
pmon_getarg(const int argno)43 pmon_getarg(const int argno)
44 {
45           if (argno < 0 || argno >= pmon_argc)
46                     return NULL;
47 
48           return (const char *)(vaddr_t)pmon_argv[argno];
49 }
50 
51 const char *
pmon_getenv(const char * var)52 pmon_getenv(const char *var)
53 {
54           int32_t *envptr = pmon_envp;
55           const char *envstr;
56           size_t varlen;
57 
58           if (envptr == NULL)
59                     return NULL;
60 
61           varlen = strlen(var);
62           while (*envptr != 0) {
63                     envstr = (const char *)(vaddr_t)*envptr;
64                     /*
65                      * There is a PMON2000 bug, at least on Lemote Yeeloong,
66                      * which causes it to override part of the environment
67                      * pointers array with the environment data itself.
68                      *
69                      * This only happens on cold boot, and if the BSD kernel
70                      * is loaded without symbols (i.e. no option -k passed
71                      * to the boot command).
72                      *
73                      * Until a suitable workaround is found or the bug is
74                      * fixed, ignore broken environment information and
75                      * tell the user (in case this prevents us from finding
76                      * important information).
77                      */
78                     if ((vaddr_t)envstr < (vaddr_t)MIPS_KSEG1_START ||
79                         (vaddr_t)envstr >= (vaddr_t)MIPS_KSEG2_START) {
80                               printf("WARNING! CORRUPTED ENVIRONMENT!\n");
81                               printf("Unable to search for %s.\n", var);
82 #ifdef _STANDALONE
83                               printf("If boot fails, power-cycle the machine.\n");
84 #else
85                               printf("If the kernel fails to identify the system"
86                                   " type, please boot it again with `-k' option.\n");
87 #endif
88 
89                               /* terminate environment for further calls */
90                               *envptr = 0;
91                               break;
92                     }
93                     if (strncmp(envstr, var, varlen) == 0 &&
94                         envstr[varlen] == '=')
95                               return envstr + varlen + 1;
96                     envptr++;
97           }
98 
99           return NULL;
100 }
101