1/*  *********************************************************************
2    *  Broadcom Common Firmware Environment (CFE)
3    *
4    *  Miscellaneous commands			File: ui_misccmds.c
5    *
6    *  Some small but useful commands
7    *
8    *  Author:  Mitch Lichtenberg
9    *
10    *********************************************************************
11    *
12    *  Copyright 2000,2001,2002,2003
13    *  Broadcom Corporation. All rights reserved.
14    *
15    *  This software is furnished under license and may be used and
16    *  copied only in accordance with the following terms and
17    *  conditions.  Subject to these conditions, you may download,
18    *  copy, install, use, modify and distribute modified or unmodified
19    *  copies of this software in source and/or binary form.  No title
20    *  or ownership is transferred hereby.
21    *
22    *  1) Any source code used, modified or distributed must reproduce
23    *     and retain this copyright notice and list of conditions
24    *     as they appear in the source file.
25    *
26    *  2) No right is granted to use any trade name, trademark, or
27    *     logo of Broadcom Corporation.  The "Broadcom Corporation"
28    *     name may not be used to endorse or promote products derived
29    *     from this software without the prior written permission of
30    *     Broadcom Corporation.
31    *
32    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
33    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
34    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
35    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
36    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
37    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
38    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
40    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
42    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
43    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
44    *     THE POSSIBILITY OF SUCH DAMAGE.
45    ********************************************************************* */
46
47
48#include "cfe.h"
49#include "ui_command.h"
50
51
52static int ui_cmd_loop(ui_cmdline_t *cmd,int argc,char *argv[]);
53static int ui_cmd_sleep(ui_cmdline_t *cmd,int argc,char *argv[]);
54#ifdef _FUNCSIM_
55static int ui_cmd_exit(ui_cmdline_t *cmd,int argc,char *argv[]);
56#endif
57static int ui_cmd_console(ui_cmdline_t *cmd,int argc,char *argv[]);
58
59int ui_init_misccmds(void);
60
61int ui_init_misccmds(void)
62{
63
64    cmd_addcmd("loop",
65	       ui_cmd_loop,
66	       NULL,
67	       "Loop a command",
68	       "loop \"command\" [-count=*]\n"
69	       "The 'loop' command causes the specified command or list of commands\n"
70	       "to be repeated 'count' times or forever, or until a character is typed",
71	       "-count=*;Specifies number of iterations|"
72	       "-forever;Loops forever");
73
74    cmd_addcmd("sleep",
75	       ui_cmd_sleep,
76	       NULL,
77	       "Wait for some period of time",
78	       "sleep [time]\n"
79	       "The 'sleep' command pauses CFE's execution for the specified number\n"
80	       "of seconds.  Without any arguments, CFE will wait one second.\n",
81	       "");
82
83#ifdef _FUNCSIM_
84    cmd_addcmd("exit",
85	       ui_cmd_exit,
86	       NULL,
87	       "exit from the functional simulator",
88	       "exit [n]\n\n"
89	       "This command is useful only when running under the functional\n"
90	       "simulator.   It causes the simulator to exit and return to the\n"
91	       "operating system.  If specified, 'n' will be placed in $4 as a\n"
92	       "return code.",
93	       "");
94#endif
95
96    cmd_addcmd("set console",
97	       ui_cmd_console,
98	       NULL,
99	       "Change the active console device",
100	       "set console device-name\n\n"
101	       "Changes the console device to the specified device name.  The console\n"
102	       "must be a serial-style device.  Be careful not to change the console\n"
103	       "to a device that is not connected!",
104	       "");
105
106    return 0;
107}
108
109static int ui_cmd_sleep(ui_cmdline_t *cmd,int argc,char *argv[])
110{
111    int seconds = 1;
112    char *x;
113    cfe_timer_t timer;
114
115    if ((x = cmd_getarg(cmd,0))) seconds = atoi(x);
116
117    TIMER_SET(timer,seconds*CFE_HZ);
118    while (!TIMER_EXPIRED(timer)) {
119	POLL();
120	}
121
122    return 0;
123}
124
125static int ui_cmd_loop(ui_cmdline_t *cmd,int argc,char *argv[])
126{
127    int count = 10;
128    char *x;
129    int res;
130    int forever;
131
132    if (cmd_sw_value(cmd,"-count",&x)) count = atoi(x);
133
134    forever = cmd_sw_isset(cmd,"-forever");
135
136    x = cmd_getarg(cmd,0);
137    if (!x) return ui_showusage(cmd);
138
139    res = 0;
140    while (count || forever) {
141	if (console_status()) break;
142	res = ui_docommands(x);
143	if (res != 0) break;
144	count--;
145	}
146
147    return res;
148}
149
150
151#ifdef _FUNCSIM_
152static int ui_cmd_exit(ui_cmdline_t *cmd,int argc,char *argv[])
153{
154    int val = 0;
155    char *x;
156
157    x = cmd_getarg(cmd,0);
158    if (x) val = atoi(x);
159
160    __asm __volatile ("move $4,%0 ; li $2,1 ; syscall 0xca" : "=r"(val));
161
162    return -1;
163}
164#endif
165
166
167static int ui_cmd_console(ui_cmdline_t *cmd,int argc,char *argv[])
168{
169    int res;
170    char *dev;
171
172    dev = cmd_getarg(cmd,0);
173    if (!dev) return -1; 	/* XXX usage */
174
175    res = cfe_getdevinfo(dev);
176    if (res < 0) {
177	xprintf("Device '%s' is not valid\n",dev);
178	return CFE_ERR_DEVNOTFOUND;
179	}
180
181    if ((res & CFE_DEV_MASK) != CFE_DEV_SERIAL) {
182	xprintf("Device '%s' is not the appropriate type to be a console\n",
183		dev);
184	return CFE_ERR_WRONGDEVTYPE;
185	}
186
187    cfe_set_console(dev);
188
189    return 0;
190}
191