huangcm
2025-07-03 a76b2fadf6ad4adf86e241e3753a63efe03ef80c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include "include.h"
 
#define     DEBUG_SHELL 0
 
#define     MAX_CMD_LEN         64      /* max command length */
#define     MAX_CMD_ARGS        5       /* max arguments nr for one command */
 
/* Build in commands prototype */
typedef int (*builtin_func_ptr)(const char **);
 
struct builtincmd {
   const char *name;
   builtin_func_ptr builtinfunc;
};
 
static void parseargs(char *argstr, s32 *argc_p, char **argv);
static builtin_func_ptr inbuilt(const char *s);
static s32 execute_command(const char **argv);
static void execute_one_command(char *buf);
static int do_echo(const char **args);
static int do_set_debug_level(const char **args);
static int do_set_baudrate(const char **args);
static int do_cat(const char **args);
static int do_help(const char **args);
static int do_print(const char **args);
 
/* builtin commands table, shell execute commands */
const struct builtincmd Commands_Table[] = {
   { "echo", do_echo},
   { "set_debug_level", do_set_debug_level},
   { "set_baudrate", do_set_baudrate},
   { "cat", do_cat},
   { "print", do_print},
   { "help", do_help},
   { NULL, NULL},
};
 
/* parse token state */
enum parseState {
   PS_WHITESPACE,  /* space find */
   PS_TOKEN,       /* valid parameters find */
   PS_STRING,
   PS_ESCAPE
};
 
char cmd_buf[MAX_CMD_LEN];
 
/*
 * getcmd - Get the command input
 * @parg: cmd_buf--Input command string
 */
s32 getcmd(void *parg)
{
   char c;
   u32 count;
   static u32 curpos;  /* current position - index __s32o cmd_buf */
 
/*
* two scenes:
* 1. input character one by one, the 'count' is 1, 'c' is the input.
* 2. input chatacters burstly, 'count' is number of characters, 'c' is the \
*    last word, usualy is '\r', without '0x08', '0x7E'.
*/
   count = debugger_get(cmd_buf + curpos);
   c = *(cmd_buf + curpos + count - 1);
#if DEBUG_SHELL
   LOG("uart received:%x\n", c);
   do {
       int i;
       LOG("CMDBUF:");
       for (i = 0; i < curpos; i++)
           LOG("%x", *(cmd_buf + i));
       LOG("\n");
   } while (0);
   return OK;
#endif
   switch (c) {
   /* backspace */
   case 0x08:
           /* we're not at the beginning of the line */
           if (count && curpos--) { /* not & */
 
               debugger_putc(0x08);    /* go backwards */
               debugger_putc(' ');     /* overwrite the char */
               debugger_putc(0x08);    /* go back again */
           }
           cmd_buf[curpos] = '\0';
           break;
   /* delete */
   case 0x7E:
           debugger_putc('\r');
           for (count = 0; count < curpos; count++)
               debugger_putc(' ');
           debugger_putc('\r');
           curpos = 0;
           break;
   /*end of command*/
   case '\r':
   case '\n':
   case '\0':
           /* send message(ucos used) or execute command */
           debugger_putc('\r');
           debugger_putc('\n');
           cmd_buf[curpos] = 0;
           if (curpos > 1)
               execute_one_command(cmd_buf);
           debugger_putc('$');
           debugger_putc(' ');
 
           curpos = 0;
           break;
   default:
           /*
            * echo it back out to the screen,
            * and adjust buffer index
            */
           if (curpos + count < (MAX_CMD_LEN - 1))
               for (; count > 0; count--)
                   debugger_putc(*(cmd_buf + curpos++));
           break;
       }
 
   return OK;
}
 
/*
 * execute_one_command - parse and execute a string
 * @buf: cmd_buf--execute string pointer
 */
static void execute_one_command(char *buf)
{
   char *argv[MAX_CMD_ARGS]; /* only used for save address, not data */
   s32 i;
   s32 argc;
 
   /* init argv pointer table */
   for (i = 0; i < MAX_CMD_ARGS; i++)
       argv[i] = NULL;
 
   /* parse input string */
   parseargs(buf, &argc, argv);
   if (argc > 0)
       execute_command((const char **)argv);
}
 
/*
 * execute_command - execute one command
 * @agrv: execute command parater pointer
 * @returns: Comand execute function pointer except
 */
static s32 execute_command(const char **argv)
{
   builtin_func_ptr bltin = NULL;
   int DoResult = FAIL; /* defult failed, is not a command */
 
   if (NULL == argv)
       goto Error_Command;
 
   /* find execute command */
   bltin = inbuilt(argv[0]);
   if (bltin) {
       /* execute builtin command */
       DoResult = bltin(argv);
 
       return DoResult;
   }
 
Error_Command:
   ERR("\"%s\" is not a command, type \"help\" for more information!\n", argv[0]);
 
   return DoResult;
}
 
/*
 * parseargs - Parse user command line
 * @argstr: parameters
 * @agrgc_p: parameters
 * @argv: parameters
 */
static void parseargs(char *argstr, s32 *argc_p, char **argv)
{
   char c;
   s32 argc = 0;
   enum parseState stackedState = PS_WHITESPACE;
   enum parseState lastState = PS_WHITESPACE;
 
   /* tokenize the argstr */
   c = *argstr; /* optimize read order */
   while ((c != 0) && (argc <= MAX_CMD_ARGS)) {
       if (c == ';' && lastState != PS_STRING && lastState != PS_ESCAPE)
           break;
 
       if (lastState == PS_ESCAPE) {
           lastState = stackedState;
       } else if (lastState == PS_STRING) {
           if (c == '"') {
               lastState = PS_WHITESPACE;
               *argstr = 0;
           } else
               lastState = PS_STRING;
       } else if ((c == ' ') || (c == '\r') || (c == '\n') || (c == '\t')) {
           /* whitespace character or 'enter'*/
           *argstr = 0;
           lastState = PS_WHITESPACE;
       } else if (c == '"') {
           lastState = PS_STRING;
           *argstr++ = 0;
           argv[argc++] = argstr;
       } else { /* token */
           if (lastState == PS_WHITESPACE)
               argv[argc++] = argstr; /* address save */
           lastState = PS_TOKEN;
       }
       c = *(++argstr);
   }
   argv[argc] = NULL;
   if (argc_p != NULL)
       *argc_p = argc;
 
   if (*argstr == ';')
       *argstr++ = '\0';
}
 
/*
 * inbuilt - find one command execute function
 * @s: name of command
 * @return: Comand execute function pointer except
 */
static builtin_func_ptr inbuilt(const char *s)
{
   const struct builtincmd *bp;
 
   /* first seach command table */
   for (bp = Commands_Table; bp->name; bp++) {
       if (0 == strcmp(bp->name, s))
           return bp->builtinfunc;
   }
 
   return NULL;
}
 
/*
 * do_echo - output the input
 * @args: parameters
 */
static int do_echo(const char **args)
{
   u32 add;
   u32 wda;
   /* input arguments check */
   if ((NULL == args[1]) || (NULL == args[3]) || ('>' != *args[2]) || \
       (NULL != args[4])) {
       ERR("invalid argument!\n");
       LOG("echo 0xdata > 0xaddr\n");
 
       return -EINVAL;
   }
   wda = hstr2int(args[1], strlen(args[1]));
   add = hstr2int(args[3], strlen(args[3]));
   if (add & 0x03) {
       LOG("address should be align 4B\n");
 
       return -EINVAL;
   }
   writel(wda, add);
   LOG("write: 0x%x to add: 0x%x\n", wda, add);
 
   return OK;
}
 
/*
 * do_cat - read a register
 * @args: parameters
 */
static int do_cat(const char **args)
{
   u32 *add;
   u32 len;
   u32 i;
 
   /* input arguments check */
   if ((NULL == args[1]) || (NULL != args[3])) {
       ERR("invalid argument!\n");
       LOG("cat addr [len]\n");
 
       return -EINVAL;
   }
   add = (u32 *)hstr2int(args[1], strlen(args[1]));
   if ((u32)add & 0x03) {
       LOG("address should be align 4B\n");
 
       return -EINVAL;
   }
   if (NULL != args[2]) {
       len = dstr2int(args[2], strlen(args[2]));
       if (!len)
           len = hstr2int(args[2], strlen(args[2]));
       LOG("cat start:%x, length:%d\n[0x%8x]", add, len--, add);
       for (i = 0; i < len; i++) {
           LOG("0x%8x  ", readl(add++));
               if ((i & 3) == 3)
                   LOG("\n[0x%8x]", add);
       }
       LOG("0x%8x  \n", readl(add));
 
       return OK;
   }
   LOG("address: 0x%8x is: 0x%8x\n", add, readl(add));
 
   return OK;
}
 
/*
 * do_set_debug_level - output the input
 * @args: parameters
 */
static int do_set_debug_level(const char **args)
{
   u32 debug_level = 0;
 
   /* input arguments check */
   if ((NULL == args[1]) || (NULL != args[2])) {
       ERR("invalid argument!\n");
       LOG("set_debug_level 3\n");
 
       return -EINVAL;
   }
   debug_level =  dstr2int(args[1], strlen(args[1]));
   LOG("the debug level set to be %d\n", debug_level);
   set_debug_level(debug_level);
 
   return OK;
}
 
/*
 * do_set_baudrate - output the input
 * @args: parameters
 */
static int do_set_baudrate(const char **args)
{
   u32 baud = 115200;
 
   /* input arguments check */
   if ((NULL == args[1]) || (NULL != args[2])) {
       ERR("invalid argument!\n");
 
       return -EINVAL;
   }
   baud = dstr2int(args[1], strlen(args[1]));
   uart_set_baudrate(baud);
   LOG("the baudrate set to be %d\n", baud);
 
   return OK;
}
 
/*
 * do_print - print buf, usually __log_buf
 * @args: parameters
 */
static int do_print(const char **args)
{
   char *add;
   u32 len;
 
   /* input arguments check */
   if ((NULL == args[1]) || (NULL != args[3])) {
       ERR("invalid argument!\n");
 
       return -EINVAL;
   }
   add = (char *)hstr2int(args[1], strlen(args[1]));
   if (NULL != args[2]) {
       len = dstr2int(args[2], strlen(args[2]));
       if (!len)
           len = hstr2int(args[2], strlen(args[2]));
       LOG("print start:%8x, length:%d\n", add, len--);
       cpucfg_set_little_endian_address((void *)add, (void *)((int)(add + len + (1<<10)) & ~0x01FF));
       debugger_puts(add);
       time_mdelay(10);
       cpucfg_remove_little_endian_address(add, 0);
 
       return OK;
   }
 
   return OK;
}
 
/*
 * do_help - show all commands
 * @args: parameters
 */
static int do_help(const char **args)
{
   const struct builtincmd *bp;
 
   LOG("support commands:\n");
   /* first seach command table */
   for (bp = Commands_Table; bp->name; bp++)
       LOG("\"%s\"\n", bp->name);
 
   return OK;
}