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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
|
/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/* Console module for Chrome EC */
#include "clock.h"
#include "console.h"
#include "link_defs.h"
#include "system.h"
#include "task.h"
#include "uart.h"
#include "usb_console.h"
#include "util.h"
#define MAX_ARGS_PER_COMMAND 10
#define PROMPT "> "
/* ASCII control character; for example, CTRL('C') = ^C */
#define CTRL(c) ((c) - '@')
#ifdef CONFIG_CONSOLE_HISTORY
/* History buffers */
static char history[CONFIG_CONSOLE_HISTORY][CONFIG_CONSOLE_INPUT_LINE_SIZE];
static int history_next, history_pos;
#endif
/* Current console command line */
static char input_buf[CONFIG_CONSOLE_INPUT_LINE_SIZE];
/* Length of current line */
static int input_len;
/* Cursor position in current line */
static int input_pos;
/* Was last received character a carriage return? */
static int last_rx_was_cr;
/* State of input escape code */
static enum {
ESC_OUTSIDE, /* Not in escape code */
ESC_START, /* Got ESC */
ESC_BAD, /* Bad escape sequence */
ESC_BRACKET, /* Got ESC [ */
ESC_BRACKET_1, /* Got ESC [ 1 */
ESC_BRACKET_3, /* Got ESC [ 3 */
ESC_O, /* Got ESC O */
} esc_state;
/* Extended key code values, from multi-byte escape sequences */
enum extended_key_code {
KEY_UP_ARROW = 0x100,
KEY_DOWN_ARROW,
KEY_RIGHT_ARROW,
KEY_LEFT_ARROW,
KEY_END,
KEY_HOME,
KEY_DEL
};
/**
* Split a line of input into words.
*
* @param input Input line; modified to add nulls after each word.
* @param argc Destination for number of words.
* @param argv Destination array for pointers to words; must be at
* least MAX_ARGS_PER_COMMAND entries long.
*
* @return EC_SUCCESS. If more than MAX_ARGS_PER_COMMAND words are found,
* discards the excess and returns EC_ERROR_OVERFLOW.
*/
static int split_words(char *input, int *argc, char **argv)
{
char *c;
int in_word = 0;
int in_line = 1;
/* Parse input into words */
*argc = 0;
for (c = input; in_line; c++) {
if (!*c)
in_line = 0;
if (isspace(*c) || !*c) {
if (in_word) {
/* Ending a word */
*c = '\0';
++*argc;
in_word = 0;
}
} else if (*c == '#') {
/* Comments start with hash and go to end of line */
break;
} else if (!in_word) {
/* Starting a new word */
if (*argc >= MAX_ARGS_PER_COMMAND)
return EC_ERROR_OVERFLOW;
argv[*argc] = c;
in_word = 1;
}
}
return EC_SUCCESS;
}
/**
* Find a command by name.
*
* Allows partial matches, as long as the partial match is unique to one
* command. So "foo" will match "foobar" as long as there isn't also a
* command "food".
*
* @param name Command name to find.
*
* @return A pointer to the command structure, or NULL if no match found.
*/
static const struct console_command *find_command(char *name)
{
const struct console_command *cmd, *match = NULL;
int match_length = strlen(name);
for (cmd = __cmds; cmd < __cmds_end; cmd++) {
if (!strncasecmp(name, cmd->name, match_length)) {
if (match)
return NULL;
/*
* Check if 'cmd->name' is of the same length as
* 'name'. If yes, then we have a full match.
*/
if (cmd->name[match_length] == '\0')
return cmd;
match = cmd;
}
}
return match;
}
static const char const *errmsgs[] = {
"OK",
"Unknown error",
"Unimplemented",
"Overflow",
"Timeout",
"Invalid argument",
"Busy",
"Access Denied",
"Not Powered",
"Not Calibrated",
};
/**
* Handle a line of input containing a single command.
*
* @param input Input buffer; modified during parsing.
*
* @return EC_SUCCESS, or non-zero if error.
*/
static int handle_command(char *input)
{
const struct console_command *cmd;
char *argv[MAX_ARGS_PER_COMMAND];
int argc = 0;
int rv;
/* Split input into words. Ignore words past our limit. */
split_words(input, &argc, argv);
/* If no command, nothing to do */
if (!argc)
return EC_SUCCESS;
cmd = find_command(argv[0]);
if (!cmd) {
ccprintf("Command '%s' not found or ambiguous.\n", argv[0]);
return EC_ERROR_UNKNOWN;
}
rv = cmd->handler(argc, argv);
if (rv == EC_SUCCESS)
return rv;
/* Print more info for errors */
if (rv < ARRAY_SIZE(errmsgs))
ccprintf("%s\n", errmsgs[rv]);
else if (rv >= EC_ERROR_PARAM1 && rv < EC_ERROR_PARAM_COUNT)
ccprintf("Parameter %d invalid\n", rv - EC_ERROR_PARAM1 + 1);
else if (rv == EC_ERROR_PARAM_COUNT)
ccputs("Wrong number of params\n");
else if (rv != EC_SUCCESS)
ccprintf("Command returned error %d\n", rv);
#ifdef CONFIG_CONSOLE_CMDHELP
if (cmd->argdesc)
ccprintf("Usage: %s %s\n", cmd->name, cmd->argdesc);
#endif
return rv;
}
static void console_init(void)
{
*input_buf = '\0';
ccprintf("Console is enabled; type HELP for help.\n");
ccputs(PROMPT);
}
static int console_putc(int c)
{
int rv1 = uart_putc(c);
int rv2 = usb_putc(c);
return rv1 == EC_SUCCESS ? rv2 : rv1;
}
static void move_cursor_right(void)
{
if (input_pos == input_len)
return;
ccputs("\x1b[1C");
input_pos++;
}
static void move_cursor_end(void)
{
if (input_pos == input_len)
return;
ccprintf("\x1b[%dC", input_len - input_pos);
input_pos = input_len;
}
static void move_cursor_left(void)
{
if (input_pos == 0)
return;
ccputs("\x1b[1D");
input_pos--;
}
static void move_cursor_begin(void)
{
if (input_pos == 0)
return;
ccprintf("\x1b[%dD", input_pos);
input_pos = 0;
}
static void repeat_char(char c, int cnt)
{
while (cnt--)
console_putc(c);
}
#ifdef CONFIG_CONSOLE_HISTORY
/**
* Load input history
*
* @param idx History index to load
*/
static void load_history(int idx)
{
/* Copy history */
strzcpy(input_buf, history[idx], CONFIG_CONSOLE_INPUT_LINE_SIZE);
/* Print history */
move_cursor_begin();
ccputs(input_buf);
/* Clear everything past end of history */
input_pos = strlen(input_buf);
if (input_len > input_pos) {
repeat_char(' ', input_len - input_pos);
repeat_char('\b', input_len - input_pos);
}
input_len = input_pos;
}
/**
* Save line to the next history slot
*/
static void save_history(void)
{
strzcpy(history[history_next], input_buf,
CONFIG_CONSOLE_INPUT_LINE_SIZE);
}
#endif /* CONFIG_CONSOLE_HISTORY */
static void handle_backspace(void)
{
if (!input_pos)
return; /* Already at beginning of line */
/* Move cursor back */
console_putc('\b');
/* Print and move anything following the cursor position */
if (input_pos != input_len) {
ccputs(input_buf + input_pos);
memmove(input_buf + input_pos - 1,
input_buf + input_pos,
input_len - input_pos + 1);
} else {
input_buf[input_len - 1] = '\0';
}
/* Space over last character and move cursor to correct position */
console_putc(' ');
repeat_char('\b', input_len - input_pos + 1);
input_len--;
input_pos--;
}
/**
* Escape code handler
*
* @param c Next received character.
* @return Key code, or -1 if character was eaten
*/
static int handle_esc(int c)
{
switch (esc_state) {
case ESC_START:
if (c == '[') {
esc_state = ESC_BRACKET;
return -1;
} else if (c == 'O') {
esc_state = ESC_O;
return -1;
}
break;
case ESC_BRACKET:
if (c == '1') {
esc_state = ESC_BRACKET_1;
return -1;
} else if (c == '3') {
esc_state = ESC_BRACKET_3;
return -1;
}
if (c == 'A')
return KEY_UP_ARROW;
else if (c == 'B')
return KEY_DOWN_ARROW;
else if (c == 'C')
return KEY_RIGHT_ARROW;
else if (c == 'D')
return KEY_LEFT_ARROW;
break;
case ESC_O:
if (c == 'F')
return KEY_END;
break;
case ESC_BRACKET_1:
if (c == '~')
return KEY_HOME;
break;
case ESC_BRACKET_3:
if (c == '~')
return KEY_DEL;
break;
default:
break;
}
/* Check if the escape code is done */
if (isalpha(c) || c == '~')
esc_state = ESC_OUTSIDE;
else
esc_state = ESC_BAD;
return -1;
}
static void console_handle_char(int c)
{
/* Translate CR and CRLF to LF (newline) */
if (c == '\r') {
last_rx_was_cr = 1;
c = '\n';
} else if (c == '\n' && last_rx_was_cr) {
last_rx_was_cr = 0;
return;
} else {
last_rx_was_cr = 0;
}
/* Handle terminal escape sequences (ESC [ ...) */
if (c == 0x1B) {
esc_state = ESC_START;
return;
} else if (esc_state) {
c = handle_esc(c);
if (c != -1)
esc_state = ESC_OUTSIDE;
}
switch (c) {
case KEY_DEL:
if (input_pos == input_len)
break; /* Already at end */
move_cursor_right();
/* Drop through to backspace handling */
case '\b':
case 0x7f:
handle_backspace();
break;
case '\n':
/* Terminate this line */
console_putc('\n');
#ifdef CONFIG_CONSOLE_HISTORY
/* Save command in history buffer */
if (input_len) {
save_history();
history_next = (history_next + 1) %
CONFIG_CONSOLE_HISTORY;
history_pos = history_next;
}
#endif
/* Handle command */
handle_command(input_buf);
/* Start new line */
input_pos = input_len = 0;
input_buf[0] = '\0';
/* Reprint prompt */
ccputs(PROMPT);
break;
case CTRL('A'):
case KEY_HOME:
move_cursor_begin();
break;
case CTRL('B'):
case KEY_LEFT_ARROW:
move_cursor_left();
break;
case CTRL('E'):
case KEY_END:
move_cursor_end();
break;
case CTRL('F'):
case KEY_RIGHT_ARROW:
move_cursor_right();
break;
case CTRL('K'):
/* Kill to end of line */
if (input_pos == input_len)
break;
repeat_char(' ', input_len - input_pos);
repeat_char('\b', input_len - input_pos);
input_len = input_pos;
input_buf[input_len] = '\0';
break;
case CTRL('L'):
/* Reprint current */
ccputs("\x0c" PROMPT);
ccputs(input_buf);
repeat_char('\b', input_len - input_pos);
break;
#ifdef CONFIG_CONSOLE_HISTORY
case CTRL('P'):
case KEY_UP_ARROW:
/* History previous */
if (history_pos == history_next)
save_history();
if (--history_pos < 0)
history_pos = CONFIG_CONSOLE_HISTORY - 1;
load_history(history_pos);
break;
case CTRL('N'):
case KEY_DOWN_ARROW:
/* History next */
if (history_pos == history_next)
save_history();
if (++history_pos >= CONFIG_CONSOLE_HISTORY)
history_pos = 0;
load_history(history_pos);
break;
#endif /* CONFIG_CONSOLE_HISTORY */
default:
/* Ignore non-printing characters */
if (!isprint(c))
break;
/* Ignore if line is full (leaving room for terminating null) */
if (input_len >= sizeof(input_buf) - 1)
break;
/* Print character */
console_putc(c);
/* If not at end of line, print rest of line and move it down */
if (input_pos != input_len) {
ccputs(input_buf + input_pos);
memmove(input_buf + input_pos + 1,
input_buf + input_pos,
input_len - input_pos + 1);
repeat_char('\b', input_len - input_pos);
}
/* Add character to buffer and terminate it */
input_buf[input_pos++] = c;
input_buf[++input_len] = '\0';
}
}
void console_has_input(void)
{
#ifdef CONFIG_LOW_POWER_IDLE
/* Notify the clock module that the console is in use. */
clock_refresh_console_in_use();
#endif
/* Wake up the console task */
task_wake(TASK_ID_CONSOLE);
}
void console_task(void)
{
#ifdef CONFIG_CONSOLE_RESTRICTED_INPUT
/* the console is not available due to security restrictions */
if (system_is_locked() && !system_get_console_force_enabled()) {
ccprintf("Console is DISABLED (WP is ON).\n");
while (1)
task_wait_event(-1);
}
#endif
console_init();
while (1) {
int c;
while (1) {
c = uart_getc();
if (c == -1)
break;
console_handle_char(c);
}
while (1) {
c = usb_getc();
if (c == -1)
break;
console_handle_char(c);
}
task_wait_event(-1); /* Wait for more input */
}
}
/*****************************************************************************/
/* Console commands */
/* Command handler - prints help. */
static int command_help(int argc, char **argv)
{
const int ncmds = __cmds_end - __cmds;
const int cols = 5; /* printing in five columns */
const int rows = (ncmds + cols - 1) / cols;
int i, j;
#ifdef CONFIG_CONSOLE_CMDHELP
if (argc == 2) {
const struct console_command *cmd;
if (!strcasecmp(argv[1], "list")) {
ccputs("Known commands:\n");
for (i = 0; i < ncmds; i++) {
ccprintf(" %-15s%s\n",
__cmds[i].name, __cmds[i].shorthelp);
cflush();
}
ccputs("HELP CMD = help on CMD.\n");
return EC_SUCCESS;
}
cmd = find_command(argv[1]);
if (!cmd) {
ccprintf("Command '%s' not found or ambiguous.\n",
argv[1]);
return EC_ERROR_UNKNOWN;
}
ccprintf("Usage: %s %s\n", cmd->name,
(cmd->argdesc ? cmd->argdesc : ""));
if (cmd->shorthelp)
ccprintf("%s\n", cmd->shorthelp);
return EC_SUCCESS;
}
#endif
ccputs("Known commands:\n");
for (i = 0; i < rows; i++) {
ccputs(" ");
for (j = 0; j < cols; j++) {
int index = j * rows + i;
if (index >= ncmds)
break;
ccprintf("%-15s", __cmds[index].name);
}
ccputs("\n");
cflush();
}
#ifdef CONFIG_CONSOLE_CMDHELP
ccputs("HELP LIST = more info; ");
ccputs("HELP CMD = help on CMD.\n");
#endif
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(help, command_help,
"[ list | <name> ]",
"Print command help",
NULL);
#ifdef CONFIG_CONSOLE_RESTRICTED_INPUT
static int command_force_enabled(int argc, char **argv)
{
int val;
if (argc < 2)
return EC_ERROR_PARAM_COUNT;
if (!parse_bool(argv[1], &val))
return EC_ERROR_PARAM1;
system_set_console_force_enabled(val);
ccprintf("Console force enabled = %s\n", val ? "on" : "off");
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(forceen, command_force_enabled,
"<on | off>",
"Force enable console",
NULL);
#endif
#ifdef CONFIG_CONSOLE_HISTORY
static int command_history(int argc, char **argv)
{
int i;
for (i = 0; i < CONFIG_CONSOLE_HISTORY; i++) {
int idx = (history_next + i) % CONFIG_CONSOLE_HISTORY;
if (history[idx][0])
ccprintf("%s\n", history[idx]);
}
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(history, command_history,
NULL,
"Print console history",
NULL);
#endif
|