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