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