c3e8ed52ff2c9a130ccba485cd9521a475688809
[mw/micromonitor-lm32.git] / umon_main / target / common / cmdtbl.c
1 /* cmdtbl.c:
2  *      This is the command table used by the monitor.
3  *
4  *      General notice:
5  *      This code is part of a boot-monitor package developed as a generic base
6  *      platform for embedded system designs.  As such, it is likely to be
7  *      distributed to various projects beyond the control of the original
8  *      author.  Please notify the author of any enhancements made or bugs found
9  *      so that all may benefit from the changes.  In addition, notification back
10  *      to the author will allow the new user to pick up changes that may have
11  *      been made by other users after this version of the code was distributed.
12  *
13  *      Note1: the majority of this code was edited with 4-space tabs.
14  *      Note2: as more and more contributions are accepted, the term "author"
15  *                 is becoming a mis-representation of credit.
16  *
17  *      Original author:        Ed Sutter
18  *      Email:                          esutter@lucent.com
19  *      Phone:                          908-582-2351
20  */
21 #include "config.h"
22 #include "tfs.h"
23 #include "tfsprivate.h"
24 #include "cli.h"
25 #include "genlib.h"
26 #include "xcmddcl.h"
27
28 #define ULVLCMD  "ulvl"
29
30 #if INCLUDE_MEMCMDS
31 #define INCLUDE_PM 1
32 #define INCLUDE_DM 1
33 #define INCLUDE_FM 1
34 #define INCLUDE_CM 1
35 #define INCLUDE_SM 1
36 #define INCLUDE_MT 1
37 #endif
38
39 extern  int     Arp(int, char **);
40 extern  int     BbcCmd(int, char **);
41 extern  int     BinfoCmd(int, char **);
42 extern  int     BmemCmd(int, char **);
43 extern  int Call(int, char **);
44 extern  int     Cast(int, char **);
45 extern  int Cm(int, char **);
46 extern  int CfCmd(int, char **);
47 extern  int     Dis(int, char **);
48 extern  int     Dm(int, char **);
49 extern  int Dhcp(int, char **);
50 extern  int Echo(int, char **);
51 extern  int     Edit(int, char **);
52 extern  int Ether(int, char **);
53 extern  int Exit(int, char **);
54 extern  int     FatfsCmd(int, char **);
55 extern  int     FlashCmd(int, char **);
56 extern  int Fm(int, char **);
57 extern  int     Gdb(int, char **);
58 extern  int Goto(int, char **);
59 extern  int Gosub(int, char **);
60 extern  int     Heap(int, char **);
61 extern  int     Help(int, char **);
62 extern  int     History(int, char **);
63 extern  int Icmp(int, char **);
64 extern  int     Ide(int, char **);
65 extern  int     If(int, char **);
66 extern  int     Igmp(int, char **);
67 extern  int Item(int, char **);
68 extern  int Jffs2Cmd(int, char **);
69 extern  int Mt(int, char **);
70 extern  int     MtraceCmd(int, char **);
71 extern  int Pm(int, char **);
72 extern  int     Prof(int, char **);
73 extern  int     Read(int, char **);
74 extern  int     Reg(int, char **);
75 extern  int Reset(int, char **);
76 extern  int Return(int, char **);
77 extern  int SdCmd(int, char **);
78 extern  int     Set(int, char **);
79 extern  int Sleep(int, char **);
80 extern  int Sm(int, char **);
81 extern  int     Strace(int, char **);
82 extern  int     StructCmd(int, char **);
83 extern  int     SyslogCmd(int, char **);
84 extern  int     Tfs(int, char **);
85 extern  int     Tftp(int, char **);
86 extern  int Ulvl(int, char **);
87 extern  int     Unzip(int, char **);
88 extern  int Version(int, char **);
89 extern  int     Xmodem(int, char **);
90
91 extern  char *ArpHelp[];
92 extern  char *BbcHelp[];
93 extern  char *BinfoHelp[];
94 extern  char *BmemHelp[];
95 extern  char *CallHelp[];
96 extern  char *CastHelp[];
97 extern  char *CfHelp[];
98 extern  char *CmHelp[];
99 extern  char *DisHelp[];
100 extern  char *DhcpHelp[];
101 extern  char *DmHelp[];
102 extern  char *EchoHelp[];
103 extern  char *EditHelp[];
104 extern  char *EtherHelp[];
105 extern  char *ExitHelp[];
106 extern  char *FatfsHelp[];
107 extern  char *FlashHelp[];
108 extern  char *FmHelp[];
109 extern  char *GdbHelp[];
110 extern  char *GosubHelp[];
111 extern  char *GotoHelp[];
112 extern  char *HelpHelp[];
113 extern  char *HeapHelp[];
114 extern  char *HistoryHelp[];
115 extern  char *IcmpHelp[];
116 extern  char *IdeHelp[];
117 extern  char *IfHelp[];
118 extern  char *IgmpHelp[];
119 extern  char *ItemHelp[];
120 extern  char *Jffs2Help[];
121 extern  char *MtHelp[];
122 extern  char *MtraceHelp[];
123 extern  char *PmHelp[];
124 extern  char *ProfHelp[];
125 extern  char *ReadHelp[];
126 extern  char *RegHelp[];
127 extern  char *ResetHelp[];
128 extern  char *ReturnHelp[];
129 extern  char *SdHelp[];
130 extern  char *SetHelp[];
131 extern  char *SleepHelp[];
132 extern  char *SmHelp[];
133 extern  char *StraceHelp[];
134 extern  char *StructHelp[];
135 extern  char *SyslogHelp[];
136 extern  char *TfsHelp[];
137 extern  char *TftpHelp[];
138 extern  char *UlvlHelp[];
139 extern  char *UnzipHelp[];
140 extern  char *VersionHelp[];
141 extern  char *XmodemHelp[];
142
143 struct monCommand cmdlist[] = {
144 #if INCLUDE_ETHERNET
145         { "arp",                Arp,            ArpHelp,                CMDFLAG_NOMONRC },
146 #endif
147 #if INCLUDE_BBC
148         { "bbc",                BbcCmd,         BbcHelp,                0 },
149 #endif
150 #if INCLUDE_BMEM
151         { "bmem",               BmemCmd,        BmemHelp,               0 },
152 #endif
153 #if INCLUDE_BOARDINFO
154         { "brdinfo",    BinfoCmd,       BinfoHelp,              0 },
155 #endif
156         { "call",               Call,           CallHelp,               CMDFLAG_NOMONRC },
157 #if INCLUDE_CAST
158         { "cast",               Cast,           CastHelp,               0 },
159 #endif
160 #if INCLUDE_CF
161         { "cf",                 CfCmd,          CfHelp,                 0 },
162 #endif
163 #if INCLUDE_CM
164         { "cm",                 Cm,                     CmHelp,                 0 },
165 #endif
166 #if INCLUDE_DHCPBOOT
167         { "dhcp",               Dhcp,           DhcpHelp,               CMDFLAG_NOMONRC },
168 #endif
169 #if INCLUDE_DISASSEMBLER
170         { "dis",                Dis,            DisHelp,                0 },
171 #endif
172 #if INCLUDE_DM
173         { "dm",                 Dm,                     DmHelp,                 0 },
174 #endif
175 #if INCLUDE_TFSSCRIPT
176         { "echo",               Echo,           EchoHelp,               0 },
177 #endif
178 #if INCLUDE_EDIT
179         { "edit",               Edit,           EditHelp,               0 },
180 #endif
181 #if INCLUDE_ETHERNET
182         { "ether",              Ether,          EtherHelp,              CMDFLAG_NOMONRC },
183 #endif
184 #if INCLUDE_TFSSCRIPT
185         { "exit",               Exit,           ExitHelp,               0 },
186 #endif
187 #if INCLUDE_FATFS
188         { "fatfs",              FatfsCmd,       FatfsHelp,              0 },
189 #endif
190 #if INCLUDE_FLASH
191         { "flash",              FlashCmd,       FlashHelp,              0 },
192 #endif
193 #if INCLUDE_FM
194         { "fm",                 Fm,                     FmHelp,                 0 },
195 #endif
196
197 #if INCLUDE_GDB
198         { "gdb",                Gdb,            GdbHelp,                CMDFLAG_NOMONRC },
199 #endif
200
201 #if INCLUDE_TFSSCRIPT
202         { "gosub",              Gosub,          GosubHelp,              0 },
203         { "goto",               Goto,           GotoHelp,               0 },
204 #endif
205
206 #if INCLUDE_MALLOC
207         { "heap",               Heap,           HeapHelp,               0 },
208 #endif
209
210         { "help",               Help,           HelpHelp,               0 },
211         { "?",                  Help,           HelpHelp,               0 },
212
213 #if INCLUDE_LINEEDIT
214         { "history",    History,        HistoryHelp,    0 },
215 #endif
216
217 #if INCLUDE_ICMP
218         { "icmp",               Icmp,           IcmpHelp,               CMDFLAG_NOMONRC },
219 #endif
220
221 #if INCLUDE_IDE
222         { "ide",                Ide,            IdeHelp,                CMDFLAG_NOMONRC },
223 #endif
224
225 #if INCLUDE_IGMP
226         { "igmp",               Igmp,           IgmpHelp,               CMDFLAG_NOMONRC },
227 #endif
228
229 #if INCLUDE_TFSSCRIPT
230         { "if",                 If,                     IfHelp,                 0 },
231 #endif
232
233 #if INCLUDE_TFSSCRIPT
234         { "item",               Item,           ItemHelp,               0 },
235 #endif
236
237 #if INCLUDE_JFFS2
238         { "jffs2",              Jffs2Cmd,       Jffs2Help,              0 },
239 #endif
240
241 #if INCLUDE_MT
242         { "mt",                 Mt,                     MtHelp,                 0 },
243 #endif
244
245 #if INCLUDE_MEMTRACE
246         { "mtrace",             MtraceCmd,      MtraceHelp,             0 },
247 #endif
248
249 #if INCLUDE_PM
250         { "pm",                 Pm,                     PmHelp,                 0 },
251 #endif
252
253 #if INCLUDE_PROFILER
254         { "prof",               Prof,           ProfHelp,               0 },
255 #endif
256
257 #if INCLUDE_TFSSCRIPT
258         { "read",               Read,           ReadHelp,               0 },
259 #endif
260
261 #if INCLUDE_STRACE
262         { "reg",                Reg,            RegHelp,                0 },
263 #endif
264
265         { "reset",              Reset,          ResetHelp,              0 },
266
267 #if INCLUDE_TFSSCRIPT
268         { "return",             Return,         ReturnHelp,             0 },
269 #endif
270 #if INCLUDE_SD
271         { "sd",                 SdCmd,          SdHelp,                 0 },
272 #endif
273
274         { "set",                Set,            SetHelp,                0 },
275
276 #if INCLUDE_TFSSCRIPT
277         { "sleep",              Sleep,          SleepHelp,              0 },
278 #endif
279
280 #if INCLUDE_SM
281         { "sm",                 Sm,                     SmHelp,                 0 },
282 #endif
283
284 #if INCLUDE_STRACE
285         { "strace",             Strace,         StraceHelp,             0 },
286 #endif
287
288 #if INCLUDE_STRUCT
289         { "struct",             StructCmd,      StructHelp,             0 },
290 #endif
291
292 #if INCLUDE_SYSLOG
293         { "syslog",             SyslogCmd,      SyslogHelp,             CMDFLAG_NOMONRC },
294 #endif
295
296 #if INCLUDE_USRLVL
297         { ULVLCMD,              Ulvl,           UlvlHelp,               0 },
298 #endif
299
300 #if INCLUDE_TFTP
301         { "tftp",               Tftp,           TftpHelp,               CMDFLAG_NOMONRC },
302 #endif
303
304 #if INCLUDE_TFSCLI
305         { "tfs",                Tfs,            TfsHelp,                0 },
306 #endif
307
308 #if INCLUDE_UNZIP
309         { "unzip",              Unzip,          UnzipHelp,              0 },
310 #endif
311
312 #if INCLUDE_XMODEM
313         { "xmodem",             Xmodem,         XmodemHelp,             0 },
314 #endif
315
316         { "version",    Version,        VersionHelp,    0 },
317
318 #include "xcmdtbl.h"                            /* For non-generic commands that are */
319                                                                         /* specific to a particular target.  */
320         { 0,0,0,0 },
321 };
322
323 #if INCLUDE_USRLVL
324
325 /* cmdUlvl[]:
326  *      This table stores one char per command that contains that command's
327  *      user level.  The default user level of all commands is 0, but can
328  *      be re-defined by the ulvl -c command.
329  */
330 char cmdUlvl[(sizeof(cmdlist)/sizeof(struct monCommand))];
331
332 /* setCmdUlvl():
333  *      The incoming string is a command name followed by a comma and a user
334  *      level (ranging from 0 thru 4).
335  *      Return 0 if pass, 1 if new level was user-level rejected, -1 if error.
336  */
337 int
338 setCmdUlvl(char *cmdlvl, int verbose)
339 {
340         extern char     *appcmdUlvl;
341         extern struct monCommand *appCmdlist;
342         struct monCommand *cptr;
343         int     newlevel, idx, pass, doall;
344         char *comma, *lvlptr, buffer[32], *cmdandlevel;
345
346         /* Make a copy of the incoming string so that we can
347          * modify it...
348          */
349         if (strlen(cmdlvl) > (sizeof(buffer)-1))
350                 goto showerr;
351
352         strcpy(buffer,cmdlvl);
353         cmdandlevel = buffer;
354
355         /* First verify that the comma is in the string... */
356         comma = strchr(cmdandlevel,',');
357         if (!comma)
358                 goto showerr;
359
360         /* Retrieve and verify the new level to be assigned...
361          * If the level value is the string "off", then we assign a level
362          * value that is greater than MAXUSRLEVEL so that the command is
363          * essentially disabled as a built-in.
364          */
365         if (strcmp(comma+1,"off") == 0) {
366                 newlevel = MAXUSRLEVEL+1;
367         }
368         else {
369                 newlevel = atoi(comma+1);
370                 if ((newlevel < MINUSRLEVEL) || (newlevel > MAXUSRLEVEL))
371                         goto showerr;
372         }
373
374         *comma = 0;
375
376         /* Don't allow adjustment of the ulvl command itself.  It must be
377          * able to run as user level 0 all the time...
378          */
379         if (!strcmp(cmdandlevel,ULVLCMD)) {
380                 printf("Can't adjust '%s' user level.\n",ULVLCMD);
381                 return(-1);
382         }
383
384         if (appCmdlist) {
385                 pass = 0;
386                 cptr = appCmdlist;
387                 lvlptr = appcmdUlvl;
388         }
389         else {
390                 pass = 1;
391                 cptr = cmdlist;
392                 lvlptr = cmdUlvl;
393         }
394
395         /* If the command string is "ALL" then we set all commands
396          * to the requested level.
397          */
398         if (!strcmp(cmdandlevel,"ALL"))
399                 doall = 1;
400         else
401                 doall = 0;
402
403         while(pass < 2) {
404                 if ((cptr == cmdlist) && (cmdandlevel[0] == '_'))
405                         cmdandlevel++;
406
407                 /* Find the command in the table that is to be adjusted... */
408                 for(idx=0;cptr->name;cptr++,idx++) {
409                         if (doall || (!strcmp(cmdandlevel,cptr->name))) {
410                                 /* Even with doall set, we don't want to touch
411                                  * the ULVLCMD level...
412                                  */
413                                 if (doall && !strcmp(cptr->name,ULVLCMD))
414                                         continue;
415
416                                 /* If the command's user level is to be lowered, then the
417                                  * current monitor userlevel must be at least as high as the
418                                  * command's current user level...
419                                  */
420                                 if ((newlevel < lvlptr[idx]) && (getUsrLvl() < lvlptr[idx])) {
421                                         if (verbose)
422                                                 printf("User-level access denied: %s\n",cmdandlevel);
423                                         return(1);
424                                 }
425                                 lvlptr[idx] = newlevel;
426                                 if (!doall)
427                                         return(0);
428                         }
429                 }
430                 cptr = cmdlist;
431                 lvlptr = cmdUlvl;
432                 pass++;
433         }
434
435         if (doall)
436                 return(0);
437
438 showerr:
439         if (verbose)
440                 printf("Input error: %s\n",cmdlvl);
441         return(-1);
442 }
443
444 #endif