Merge patch series "Modernize U-Boot shell"

Francis Laniel <francis.laniel@amarulasolutions.com> says:

During 2021 summer, Sean Anderson wrote a contribution to add a new shell, based
on LIL, to U-Boot [1, 2].
While one of the goals of this contribution was to address the fact actual
U-Boot shell, which is based on Busybox hush, is old there was a discussion
about adding a new shell versus updating the actual one [3, 4].

So, in this series, with Harald Seiler, we updated the actual U-Boot shell to
reflect what is currently in Busybox source code.
Basically, this contribution is about taking a snapshot of Busybox shell/hush.c
file (as it exists in commit 37460f5da) and adapt it to suit U-Boot needs.

This contribution was written to be as backward-compatible as possible to avoid
breaking the existing.
So, the modern hush flavor offers the same as the actual, that is to say:
1. Variable expansion.
2. Instruction lists (;, && and ||).
3. If, then and else.
4. Loops (for, while and until).
No new features offered by Busybox hush were implemented (e.g. functions).

It is possible to change the parser at runtime using the "cli" command:
=> cli print
old
=> cli set modern
=> cli print
modern
=> cli set old
The default parser is the old one.
Note that to use both parser, you would need to set both
CONFIG_HUSH_MODERN_PARSER and CONFIG_HUSH_OLD_PARSER.

In terms of testing, new unit tests were added to ut to ensure the new behavior
is the same as the old one and it does not add regression.
Nonetheless, if old behavior was buggy and fixed upstream, the fix is then added
to U-Boot [5].
In sandbox, all of these tests pass smoothly:
=> printenv board
board=sandbox
=> ut hush
Running 20 hush tests
...
Failures: 0
=> cli set modern
=> ut hush
Running 20 hush tests
...
Failures: 0

Thanks to the effort of Harald Seiler, I was successful booting a board:
=> printenv fdtfile
fdtfile=amlogic/meson-gxl-s905x-libretech-cc.dtb
=> cli get
old
=> boot
...
root@lepotato:~#
root@lepotato:~# reboot
...
=> cli set modern
=> cli get
modern
=> printenv fdtfile
fdtfile=amlogic/meson-gxl-s905x-libretech-cc.dtb
=> boot
...
root@lepotato:~#

This contribution indeed adds a lot of code and there were concern about its
size [6, 7].
With regard to the amount of code added, the cli_hush_upstream.c is 13030 lines
long but it seems a smaller subset is really used:
gcc -D__U_BOOT__ -E common/cli_hush_upstream.c | wc -l
2870
Despite this, it is better to still have the whole upstream code for the sake of
easing maintenance.
With regard to memory size, I conducted some experiments for version 8 of this
series and for a subset of arm64 boards and found the worst case to be 4K [8].
Tom Rini conducted more research on this and also found the increase to be
acceptable [9].

If you want to review it - your review will really be appreciated - here are
some information regarding the commits:
* commits marked as "test:" deal with unit tests.
* commit "cli: Add Busybox upstream hush.c file." copies Busybox shell/hush.c
into U-Boot tree, this explain why this commit contains around 12000 additions.
* commit "cli: Port Busybox 2021 hush to U-Boot." modifies previously added file
to permit us to use this as new shell.
The really good idea of #include'ing Busybox code into a wrapper file to define
some particular functions while minimizing modifications to upstream code comes
from Harald Seiler.
* commit "cmd: Add new parser command" adds a new command which permits
selecting parser at runtime.
I am not really satisfied with the fact it calls cli_init() and cli_loop() each
time the parser is set, so your reviews would be welcomed.
* Other commits focus on enabling features we need (e.g. if).
This commit is contained in:
Tom Rini 2023-12-28 12:03:25 -05:00
commit 2b28c3b871
37 changed files with 14574 additions and 212 deletions

View file

@ -22,6 +22,27 @@ config HUSH_PARSER
If disabled, you get the old, much simpler behaviour with a somewhat
smaller memory footprint.
menu "Hush flavor to use"
depends on HUSH_PARSER
config HUSH_OLD_PARSER
bool "Use hush old parser"
help
This option enables the old flavor of hush based on hush Busybox from
2005.
config HUSH_MODERN_PARSER
bool "Use hush modern parser"
default y
help
This option enables the new flavor of hush based on hush upstream
Busybox.
config HUSH_SELECTABLE
bool
default y if HUSH_OLD_PARSER && HUSH_MODERN_PARSER
endmenu
config CMDLINE_EDITING
bool "Enable command line editing"
default y

View file

@ -229,6 +229,8 @@ obj-$(CONFIG_CMD_AVB) += avb.o
# Foundries.IO SCP03
obj-$(CONFIG_CMD_SCP03) += scp03.o
obj-$(CONFIG_HUSH_SELECTABLE) += cli.o
obj-$(CONFIG_ARM) += arm/
obj-$(CONFIG_RISCV) += riscv/
obj-$(CONFIG_SANDBOX) += sandbox/

133
cmd/cli.c Normal file
View file

@ -0,0 +1,133 @@
// SPDX-License-Identifier: GPL-2.0+
#include <cli.h>
#include <command.h>
#include <string.h>
#include <asm/global_data.h>
DECLARE_GLOBAL_DATA_PTR;
static const char *gd_flags_to_parser_name(void)
{
if (gd->flags & GD_FLG_HUSH_OLD_PARSER)
return "old";
if (gd->flags & GD_FLG_HUSH_MODERN_PARSER)
return "modern";
return NULL;
}
static int do_cli_get(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
const char *current = gd_flags_to_parser_name();
if (!current) {
printf("current cli value is not valid, this should not happen!\n");
return CMD_RET_FAILURE;
}
printf("%s\n", current);
return CMD_RET_SUCCESS;
}
static int parser_string_to_gd_flags(const char *parser)
{
if (!strcmp(parser, "old"))
return GD_FLG_HUSH_OLD_PARSER;
if (!strcmp(parser, "modern"))
return GD_FLG_HUSH_MODERN_PARSER;
return -1;
}
static int gd_flags_to_parser_config(int flag)
{
if (gd->flags & GD_FLG_HUSH_OLD_PARSER)
return CONFIG_VAL(HUSH_OLD_PARSER);
if (gd->flags & GD_FLG_HUSH_MODERN_PARSER)
return CONFIG_VAL(HUSH_MODERN_PARSER);
return -1;
}
static void reset_parser_gd_flags(void)
{
gd->flags &= ~GD_FLG_HUSH_OLD_PARSER;
gd->flags &= ~GD_FLG_HUSH_MODERN_PARSER;
}
static int do_cli_set(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
char *parser_name;
int parser_config;
int parser_flag;
if (argc < 2)
return CMD_RET_USAGE;
parser_name = argv[1];
parser_flag = parser_string_to_gd_flags(parser_name);
if (parser_flag == -1) {
printf("Bad value for parser name: %s\n", parser_name);
return CMD_RET_USAGE;
}
parser_config = gd_flags_to_parser_config(parser_flag);
switch (parser_config) {
case -1:
printf("Bad value for parser flags: %d\n", parser_flag);
return CMD_RET_FAILURE;
case 0:
printf("Want to set current parser to %s, but its code was not compiled!\n",
parser_name);
return CMD_RET_FAILURE;
}
reset_parser_gd_flags();
gd->flags |= parser_flag;
cli_init();
cli_loop();
/* cli_loop() should never return. */
return CMD_RET_FAILURE;
}
static struct cmd_tbl parser_sub[] = {
U_BOOT_CMD_MKENT(get, 1, 1, do_cli_get, "", ""),
U_BOOT_CMD_MKENT(set, 2, 1, do_cli_set, "", ""),
};
static int do_cli(struct cmd_tbl *cmdtp, int flag, int argc,
char *const argv[])
{
struct cmd_tbl *cp;
if (argc < 2)
return CMD_RET_USAGE;
/* drop initial "parser" arg */
argc--;
argv++;
cp = find_cmd_tbl(argv[0], parser_sub, ARRAY_SIZE(parser_sub));
if (cp)
return cp->cmd(cmdtp, flag, argc, argv);
return CMD_RET_USAGE;
}
#if CONFIG_IS_ENABLED(SYS_LONGHELP)
static char cli_help_text[] =
"get - print current cli\n"
"set - set the current cli, possible value are: old, modern"
;
#endif
U_BOOT_CMD(cli, 3, 1, do_cli,
"cli",
#if CONFIG_IS_ENABLED(SYS_LONGHELP)
cli_help_text
#endif
);

View file

@ -9,7 +9,8 @@ obj-y += init/
obj-y += main.o
obj-y += exports.o
obj-y += cli_getch.o cli_simple.o cli_readline.o
obj-$(CONFIG_HUSH_PARSER) += cli_hush.o
obj-$(CONFIG_HUSH_OLD_PARSER) += cli_hush.o
obj-$(CONFIG_HUSH_MODERN_PARSER) += cli_hush_modern.o
obj-$(CONFIG_AUTOBOOT) += autoboot.o
obj-y += version.o

View file

@ -25,6 +25,14 @@
#include <linux/errno.h>
#ifdef CONFIG_CMDLINE
static inline bool use_hush_old(void)
{
return IS_ENABLED(CONFIG_HUSH_SELECTABLE) ?
gd->flags & GD_FLG_HUSH_OLD_PARSER :
IS_ENABLED(CONFIG_HUSH_OLD_PARSER);
}
/*
* Run a command using the selected parser.
*
@ -44,11 +52,29 @@ int run_command(const char *cmd, int flag)
return 0;
#else
int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
if (use_hush_old()) {
int hush_flags = FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP;
if (flag & CMD_FLAG_ENV)
hush_flags |= FLAG_CONT_ON_NEWLINE;
return parse_string_outer(cmd, hush_flags);
if (flag & CMD_FLAG_ENV)
hush_flags |= FLAG_CONT_ON_NEWLINE;
return parse_string_outer(cmd, hush_flags);
}
/*
* Possible values for flags are the following:
* FLAG_EXIT_FROM_LOOP: This flags ensures we exit from loop in
* parse_and_run_stream() after first iteration while normal
* behavior, * i.e. when called from cli_loop(), is to loop
* infinitely.
* FLAG_PARSE_SEMICOLON: modern Hush parses ';' and does not stop
* first time it sees one. So, I think we do not need this flag.
* FLAG_REPARSING: For the moment, I do not understand the goal
* of this flag.
* FLAG_CONT_ON_NEWLINE: This flag seems to be used to continue
* parsing even when reading '\n' when coming from
* run_command(). In this case, modern Hush reads until it finds
* '\0'. So, I think we do not need this flag.
*/
return parse_string_outer_modern(cmd, FLAG_EXIT_FROM_LOOP);
#endif
}
@ -64,12 +90,23 @@ int run_command_repeatable(const char *cmd, int flag)
#ifndef CONFIG_HUSH_PARSER
return cli_simple_run_command(cmd, flag);
#else
int ret;
if (use_hush_old()) {
ret = parse_string_outer(cmd,
FLAG_PARSE_SEMICOLON
| FLAG_EXIT_FROM_LOOP);
} else {
ret = parse_string_outer_modern(cmd,
FLAG_PARSE_SEMICOLON
| FLAG_EXIT_FROM_LOOP);
}
/*
* parse_string_outer() returns 1 for failure, so clean up
* its result.
*/
if (parse_string_outer(cmd,
FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP))
if (ret)
return -1;
return 0;
@ -108,7 +145,11 @@ int run_command_list(const char *cmd, int len, int flag)
buff[len] = '\0';
}
#ifdef CONFIG_HUSH_PARSER
rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
if (use_hush_old()) {
rcode = parse_string_outer(buff, FLAG_PARSE_SEMICOLON);
} else {
rcode = parse_string_outer_modern(buff, FLAG_PARSE_SEMICOLON);
}
#else
/*
* This function will overwrite any \n it sees with a \0, which
@ -254,8 +295,13 @@ err:
void cli_loop(void)
{
bootstage_mark(BOOTSTAGE_ID_ENTER_CLI_LOOP);
#ifdef CONFIG_HUSH_PARSER
parse_file_outer();
#if CONFIG_IS_ENABLED(HUSH_PARSER)
if (gd->flags & GD_FLG_HUSH_MODERN_PARSER)
parse_and_run_file();
else if (gd->flags & GD_FLG_HUSH_OLD_PARSER)
parse_file_outer();
printf("Problem\n");
/* This point is never reached */
for (;;);
#elif defined(CONFIG_CMDLINE)
@ -268,7 +314,23 @@ void cli_loop(void)
void cli_init(void)
{
#ifdef CONFIG_HUSH_PARSER
u_boot_hush_start();
/* This if block is used to initialize hush parser gd flag. */
if (!(gd->flags & GD_FLG_HUSH_OLD_PARSER)
&& !(gd->flags & GD_FLG_HUSH_MODERN_PARSER)) {
if (CONFIG_IS_ENABLED(HUSH_OLD_PARSER))
gd->flags |= GD_FLG_HUSH_OLD_PARSER;
else if (CONFIG_IS_ENABLED(HUSH_MODERN_PARSER))
gd->flags |= GD_FLG_HUSH_MODERN_PARSER;
}
if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
u_boot_hush_start();
} else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
u_boot_hush_start_modern();
} else {
printf("No valid hush parser to use, cli will not initialized!\n");
return;
}
#endif
#if defined(CONFIG_HUSH_INIT_VAR)

323
common/cli_hush_modern.c Normal file
View file

@ -0,0 +1,323 @@
// SPDX-License-Identifier: GPL-2.0+
/*
* This file defines the compilation unit for the new hush shell version. The
* actual implementation from upstream BusyBox can be found in
* `cli_hush_upstream.c` which is included at the end of this file.
*
* This "wrapper" technique is used to keep the changes to the upstream version
* as minmal as possible. Instead, all defines and redefines necessary are done
* here, outside the upstream sources. This will hopefully make upgrades to
* newer revisions much easier.
*
* Copyright (c) 2021, Harald Seiler, DENX Software Engineering, hws@denx.de
*/
#include <env.h>
#include <malloc.h> /* malloc, free, realloc*/
#include <linux/ctype.h> /* isalpha, isdigit */
#include <console.h>
#include <bootretry.h>
#include <cli.h>
#include <cli_hush.h>
#include <command.h> /* find_cmd */
#include <asm/global_data.h>
/*
* BusyBox Version: UPDATE THIS WHEN PULLING NEW UPSTREAM REVISION!
*/
#define BB_VER "1.35.0.git7d1c7d833785"
/*
* Define hush features by the names used upstream.
*/
#define ENABLE_HUSH_INTERACTIVE 1
#define ENABLE_FEATURE_EDITING 1
#define ENABLE_HUSH_IF 1
#define ENABLE_HUSH_LOOPS 1
/* No MMU in U-Boot */
#define BB_MMU 0
#define USE_FOR_NOMMU(...) __VA_ARGS__
#define USE_FOR_MMU(...)
/*
* Size-saving "small" ints (arch-dependent)
*/
#if CONFIG_IS_ENABLED(X86) || CONFIG_IS_ENABLED(X86_64) || CONFIG_IS_ENABLED(MIPS)
/* add other arches which benefit from this... */
typedef signed char smallint;
typedef unsigned char smalluint;
#else
/* for arches where byte accesses generate larger code: */
typedef int smallint;
typedef unsigned smalluint;
#endif
/*
* Alignment defines used by BusyBox.
*/
#define ALIGN1 __attribute__((aligned(1)))
#define ALIGN2 __attribute__((aligned(2)))
#define ALIGN4 __attribute__((aligned(4)))
#define ALIGN8 __attribute__((aligned(8)))
#define ALIGN_PTR __attribute__((aligned(sizeof(void*))))
/*
* Miscellaneous compiler/platform defines.
*/
#define FAST_FUNC /* not used in U-Boot */
#define UNUSED_PARAM __always_unused
#define ALWAYS_INLINE __always_inline
#define NOINLINE noinline
/*
* Defines to provide equivalents to what libc/BusyBox defines.
*/
#define EOF (-1)
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
/*
* Stubs to provide libc/BusyBox functions based on U-Boot equivalents where it
* makes sense.
*/
#define utoa simple_itoa
static void __noreturn xfunc_die(void)
{
panic("HUSH died!");
}
#define bb_error_msg_and_die(format, ...) do { \
panic("HUSH: " format, __VA_ARGS__); \
} while (0);
#define bb_simple_error_msg_and_die(msg) do { \
panic_str("HUSH: " msg); \
} while (0);
/* fdprintf() is used for debug output. */
static int __maybe_unused fdprintf(int fd, const char *format, ...)
{
va_list args;
uint i;
assert(fd == 2);
va_start(args, format);
i = vprintf(format, args);
va_end(args);
return i;
}
static void bb_verror_msg(const char *s, va_list p, const char* strerr)
{
/* TODO: what to do with strerr arg? */
vprintf(s, p);
}
static void bb_error_msg(const char *s, ...)
{
va_list p;
va_start(p, s);
bb_verror_msg(s, p, NULL);
va_end(p);
}
static void bb_simple_error_msg(const char *s)
{
bb_error_msg("%s", s);
}
static void *xmalloc(size_t size)
{
void *p = NULL;
if (!(p = malloc(size)))
panic("out of memory");
return p;
}
static void *xzalloc(size_t size)
{
void *p = xmalloc(size);
memset(p, 0, size);
return p;
}
static void *xrealloc(void *ptr, size_t size)
{
void *p = NULL;
if (!(p = realloc(ptr, size)))
panic("out of memory");
return p;
}
static void *xmemdup(const void *s, int n)
{
return memcpy(xmalloc(n), s, n);
}
#define xstrdup strdup
#define xstrndup strndup
static void *mempcpy(void *dest, const void *src, size_t len)
{
return memcpy(dest, src, len) + len;
}
/* Like strcpy but can copy overlapping strings. */
static void overlapping_strcpy(char *dst, const char *src)
{
/*
* Cheap optimization for dst == src case -
* better to have it here than in many callers.
*/
if (dst != src) {
while ((*dst = *src) != '\0') {
dst++;
src++;
}
}
}
static char* skip_whitespace(const char *s)
{
/*
* In POSIX/C locale (the only locale we care about: do we REALLY want
* to allow Unicode whitespace in, say, .conf files? nuts!)
* isspace is only these chars: "\t\n\v\f\r" and space.
* "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13.
* Use that.
*/
while (*s == ' ' || (unsigned char)(*s - 9) <= (13 - 9))
s++;
return (char *) s;
}
static char* skip_non_whitespace(const char *s)
{
while (*s != '\0' && *s != ' ' && (unsigned char)(*s - 9) > (13 - 9))
s++;
return (char *) s;
}
#define is_name(c) ((c) == '_' || isalpha((unsigned char)(c)))
#define is_in_name(c) ((c) == '_' || isalnum((unsigned char)(c)))
static const char* endofname(const char *name)
{
if (!is_name(*name))
return name;
while (*++name) {
if (!is_in_name(*name))
break;
}
return name;
}
/**
* list_size() - returns the number of elements in char ** before NULL.
*
* Argument must contain NULL to signalize its end.
*
* @list The list to count the number of element.
* @return The number of element in list.
*/
static size_t list_size(char **list)
{
size_t size;
for (size = 0; list[size] != NULL; size++);
return size;
}
static int varcmp(const char *p, const char *q)
{
int c, d;
while ((c = *p) == (d = *q)) {
if (c == '\0' || c == '=')
goto out;
p++;
q++;
}
if (c == '=')
c = '\0';
if (d == '=')
d = '\0';
out:
return c - d;
}
struct in_str;
static int u_boot_cli_readline(struct in_str *i);
struct in_str;
static int u_boot_cli_readline(struct in_str *i);
/*
* BusyBox globals which are needed for hush.
*/
static uint8_t xfunc_error_retval;
static const char defifsvar[] __aligned(1) = "IFS= \t\n";
#define defifs (defifsvar + 4)
/* This define is used to check if exit command was called. */
#define EXIT_RET_CODE -2
/*
* This define is used for changes that need be done directly in the upstream
* sources still. Ideally, its use should be minimized as much as possible.
*/
#define __U_BOOT__
/*
*
* +-- Include of the upstream sources --+ *
* V V
*/
#include "cli_hush_upstream.c"
/*
* A A
* +-- Include of the upstream sources --+ *
*
*/
int u_boot_hush_start_modern(void)
{
INIT_G();
return 0;
}
static int u_boot_cli_readline(struct in_str *i)
{
char *prompt;
char __maybe_unused *ps_prompt = NULL;
if (!G.promptmode)
prompt = CONFIG_SYS_PROMPT;
#ifdef CONFIG_SYS_PROMPT_HUSH_PS2
else
prompt = CONFIG_SYS_PROMPT_HUSH_PS2;
#else
/* TODO: default value? */
#error "SYS_PROMPT_HUSH_PS2 is not defined!"
#endif
if (CONFIG_IS_ENABLED(CMDLINE_PS_SUPPORT)) {
if (!G.promptmode)
ps_prompt = env_get("PS1");
else
ps_prompt = env_get("PS2");
if (ps_prompt)
prompt = ps_prompt;
}
return cli_readline(prompt);
}

13030
common/cli_hush_upstream.c Normal file

File diff suppressed because it is too large Load diff

View file

@ -111,3 +111,4 @@ CONFIG_BCH=y
CONFIG_PANIC_HANG=y
CONFIG_LZO=y
CONFIG_POST=y
CONFIG_HUSH_OLD_PARSER=y

View file

@ -202,3 +202,4 @@ CONFIG_QE=y
CONFIG_SYS_NS16550=y
CONFIG_BCH=y
CONFIG_POST=y
CONFIG_HUSH_OLD_PARSER=y

View file

@ -173,3 +173,4 @@ CONFIG_DM_ETH_PHY=y
CONFIG_QE_UEC=y
CONFIG_QE=y
CONFIG_SYS_NS16550=y
CONFIG_HUSH_OLD_PARSER=y

View file

@ -183,3 +183,4 @@ CONFIG_QE_UEC=y
# CONFIG_PINCTRL_FULL is not set
CONFIG_QE=y
CONFIG_SYS_NS16550=y
CONFIG_HUSH_OLD_PARSER=y

View file

@ -166,3 +166,4 @@ CONFIG_QE_UEC=y
# CONFIG_PINCTRL_FULL is not set
CONFIG_QE=y
CONFIG_SYS_NS16550=y
CONFIG_HUSH_OLD_PARSER=y

View file

@ -182,3 +182,4 @@ CONFIG_QE_UEC=y
# CONFIG_PINCTRL_FULL is not set
CONFIG_QE=y
CONFIG_SYS_NS16550=y
CONFIG_HUSH_OLD_PARSER=y

View file

@ -105,3 +105,4 @@ CONFIG_DM_SERIAL=y
CONFIG_SYS_NS16550=y
CONFIG_LZO=y
CONFIG_POST=y
CONFIG_HUSH_OLD_PARSER=y

View file

@ -103,3 +103,4 @@ CONFIG_DM_SERIAL=y
CONFIG_SYS_NS16550=y
CONFIG_LZO=y
CONFIG_POST=y
CONFIG_HUSH_OLD_PARSER=y

View file

@ -105,3 +105,4 @@ CONFIG_DM_SERIAL=y
CONFIG_SYS_NS16550=y
CONFIG_LZO=y
CONFIG_POST=y
CONFIG_HUSH_OLD_PARSER=y

View file

@ -103,3 +103,4 @@ CONFIG_DM_SERIAL=y
CONFIG_SYS_NS16550=y
CONFIG_LZO=y
CONFIG_POST=y
CONFIG_HUSH_OLD_PARSER=y

View file

@ -113,3 +113,4 @@ CONFIG_DESIGNWARE_WATCHDOG=y
CONFIG_WDT=y
CONFIG_SYS_TIMER_COUNTS_DOWN=y
# CONFIG_GZIP is not set
CONFIG_HUSH_OLD_PARSER=y

View file

@ -166,3 +166,4 @@ CONFIG_QE_UEC=y
# CONFIG_PINCTRL_FULL is not set
CONFIG_QE=y
CONFIG_SYS_NS16550=y
CONFIG_HUSH_OLD_PARSER=y

View file

@ -183,3 +183,4 @@ CONFIG_QE_UEC=y
# CONFIG_PINCTRL_FULL is not set
CONFIG_QE=y
CONFIG_SYS_NS16550=y
CONFIG_HUSH_OLD_PARSER=y

74
doc/usage/cmd/cli.rst Normal file
View file

@ -0,0 +1,74 @@
.. SPDX-License-Identifier: GPL-2.0+
cli command
===========
Synopis
-------
::
cli get
cli set cli_flavor
Description
-----------
The cli command permits getting and changing the current parser at runtime.
cli get
~~~~~~~
It shows the current value of the parser used by the CLI.
cli set
~~~~~~~
It permits setting the value of the parser used by the CLI.
Possible values are old and modern.
Note that, to use a specific parser its code should have been compiled, that
is to say you need to enable the corresponding CONFIG_HUSH*.
Otherwise, an error message is printed.
Examples
--------
Get the current parser::
=> cli get
old
Change the current parser::
=> cli get
old
=> cli set modern
=> cli get
modern
=> cli set old
=> cli get
old
Trying to set the current parser to an unknown value::
=> cli set foo
Bad value for parser name: foo
cli - cli
Usage:
cli get - print current cli
set - set the current cli, possible values are: old, modern
Trying to set the current parser to a correct value but its code was not
compiled::
=> cli get
modern
=> cli set old
Want to set current parser to old, but its code was not compiled!
Return value
------------
The return value $? indicates whether the command succeeded.

View file

@ -43,6 +43,7 @@ Shell commands
cmd/cat
cmd/cbsysinfo
cmd/cedit
cmd/cli
cmd/cls
cmd/cmp
cmd/coninfo

View file

@ -697,6 +697,14 @@ enum gd_flags {
* @GD_FLG_BLOBLIST_READY: bloblist is ready for use
*/
GD_FLG_BLOBLIST_READY = 0x800000,
/**
* @GD_FLG_HUSH_OLD_PARSER: Use hush old parser.
*/
GD_FLG_HUSH_OLD_PARSER = 0x1000000,
/**
* @GD_FLG_HUSH_MODERN_PARSER: Use hush 2021 parser.
*/
GD_FLG_HUSH_MODERN_PARSER = 0x2000000,
};
#endif /* __ASSEMBLY__ */

View file

@ -12,11 +12,58 @@
#define FLAG_REPARSING (1 << 2) /* >=2nd pass */
#define FLAG_CONT_ON_NEWLINE (1 << 3) /* continue when we see \n */
#if CONFIG_IS_ENABLED(HUSH_OLD_PARSER)
extern int u_boot_hush_start(void);
extern int parse_string_outer(const char *, int);
extern int parse_string_outer(const char *str, int flag);
extern int parse_file_outer(void);
int set_local_var(const char *s, int flg_export);
#else
static inline int u_boot_hush_start(void)
{
return 0;
}
static inline int parse_string_outer(const char *str, int flag)
{
return 1;
}
static inline int parse_file_outer(void)
{
return 0;
}
static inline int set_local_var(const char *s, int flg_export)
{
return 0;
}
#endif
#if CONFIG_IS_ENABLED(HUSH_MODERN_PARSER)
extern int u_boot_hush_start_modern(void);
extern int parse_string_outer_modern(const char *str, int flag);
extern void parse_and_run_file(void);
int set_local_var_modern(char *s, int flg_export);
#else
static inline int u_boot_hush_start_modern(void)
{
return 0;
}
static inline int parse_string_outer_modern(const char *str, int flag)
{
return 1;
}
static inline void parse_and_run_file(void)
{
}
static inline int set_local_var_modern(char *s, int flg_export)
{
return 0;
}
#endif
void unset_local_var(const char *name);
char *get_local_var(const char *s);

15
include/test/hush.h Normal file
View file

@ -0,0 +1,15 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* (C) Copyright 2021
* Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com
*/
#ifndef __TEST_HUSH_H__
#define __TEST_HUSH_H__
#include <test/test.h>
/* Declare a new environment test */
#define HUSH_TEST(_name, _flags) UNIT_TEST(_name, _flags, hush_test)
#endif /* __TEST_HUSH_H__ */

View file

@ -43,6 +43,7 @@ int do_ut_env(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
int do_ut_exit(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
int do_ut_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
int do_ut_font(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
int do_ut_hush(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
int do_ut_lib(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
int do_ut_loadm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
int do_ut_log(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[]);

View file

@ -17,6 +17,9 @@ obj-$(CONFIG_FUZZ) += fuzz/
ifndef CONFIG_SANDBOX_VPL
obj-$(CONFIG_UNIT_TEST) += lib/
endif
ifneq ($(CONFIG_HUSH_PARSER),)
obj-$(CONFIG_$(SPL_)CMDLINE) += hush/
endif
obj-$(CONFIG_$(SPL_)CMDLINE) += print_ut.o
obj-$(CONFIG_$(SPL_)CMDLINE) += str_ut.o
obj-$(CONFIG_UT_TIME) += time_ut.o

View file

@ -710,7 +710,21 @@ static int bootflow_scan_menu_boot(struct unit_test_state *uts)
ut_assert_skip_to_line("(2 bootflows, 2 valid)");
ut_assert_nextline("Selected: Armbian");
ut_assert_skip_to_line("Boot failed (err=-14)");
if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
/*
* With old hush, despite booti failing to boot, i.e. returning
* CMD_RET_FAILURE, run_command() returns 0 which leads bootflow_boot(), as
* we are using bootmeth_script here, to return -EFAULT.
*/
ut_assert_skip_to_line("Boot failed (err=-14)");
} else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
/*
* While with modern one, run_command() propagates CMD_RET_FAILURE returned
* by booti, so we get 1 here.
*/
ut_assert_skip_to_line("Boot failed (err=1)");
}
ut_assertnonnull(std->cur_bootflow);
ut_assert_console_end();

View file

@ -121,6 +121,9 @@ static struct cmd_tbl cmd_ut_sub[] = {
#ifdef CONFIG_CMD_ADDRMAP
U_BOOT_CMD_MKENT(addrmap, CONFIG_SYS_MAXARGS, 1, do_ut_addrmap, "", ""),
#endif
#if CONFIG_IS_ENABLED(HUSH_PARSER)
U_BOOT_CMD_MKENT(hush, CONFIG_SYS_MAXARGS, 1, do_ut_hush, "", ""),
#endif
#ifdef CONFIG_CMD_LOADM
U_BOOT_CMD_MKENT(loadm, CONFIG_SYS_MAXARGS, 1, do_ut_loadm, "", ""),
#endif
@ -216,6 +219,9 @@ U_BOOT_LONGHELP(ut,
#ifdef CONFIG_CONSOLE_TRUETYPE
"\nfont - font command"
#endif
#if CONFIG_IS_ENABLED(HUSH_PARSER)
"\nhush - Test hush behavior"
#endif
#ifdef CONFIG_CMD_LOADM
"\nloadm - loadm command parameters and loading memory blob"
#endif

10
test/hush/Makefile Normal file
View file

@ -0,0 +1,10 @@
# SPDX-License-Identifier: GPL-2.0+
#
# (C) Copyright 2021
# Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com
obj-y += cmd_ut_hush.o
obj-y += if.o
obj-y += dollar.o
obj-y += list.o
obj-y += loop.o

19
test/hush/cmd_ut_hush.c Normal file
View file

@ -0,0 +1,19 @@
// SPDX-License-Identifier: GPL-2.0
/*
* (C) Copyright 2021
* Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com
*/
#include <command.h>
#include <test/hush.h>
#include <test/suites.h>
#include <test/ut.h>
int do_ut_hush(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
{
struct unit_test *tests = UNIT_TEST_SUITE_START(hush_test);
const int n_ents = UNIT_TEST_SUITE_COUNT(hush_test);
return cmd_ut_category("hush", "hush_test_",
tests, n_ents, argc, argv);
}

225
test/hush/dollar.c Normal file
View file

@ -0,0 +1,225 @@
// SPDX-License-Identifier: GPL-2.0
/*
* (C) Copyright 2021
* Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com
*/
#include <command.h>
#include <env_attr.h>
#include <test/hush.h>
#include <test/ut.h>
#include <asm/global_data.h>
DECLARE_GLOBAL_DATA_PTR;
static int hush_test_simple_dollar(struct unit_test_state *uts)
{
console_record_reset_enable();
ut_assertok(run_command("echo $dollar_foo", 0));
ut_assert_nextline_empty();
ut_assert_console_end();
ut_assertok(run_command("echo ${dollar_foo}", 0));
ut_assert_nextline_empty();
ut_assert_console_end();
ut_assertok(run_command("dollar_foo=bar", 0));
ut_assertok(run_command("echo $dollar_foo", 0));
ut_assert_nextline("bar");
ut_assert_console_end();
ut_assertok(run_command("echo ${dollar_foo}", 0));
ut_assert_nextline("bar");
ut_assert_console_end();
ut_assertok(run_command("dollar_foo=\\$bar", 0));
ut_assertok(run_command("echo $dollar_foo", 0));
ut_assert_nextline("$bar");
ut_assert_console_end();
ut_assertok(run_command("dollar_foo='$bar'", 0));
ut_assertok(run_command("echo $dollar_foo", 0));
ut_assert_nextline("$bar");
ut_assert_console_end();
ut_asserteq(1, run_command("dollar_foo=bar quux", 0));
/* Next line contains error message */
ut_assert_skipline();
ut_assert_console_end();
ut_asserteq(1, run_command("dollar_foo='bar quux", 0));
/* Next line contains error message */
ut_assert_skipline();
if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
/*
* For some strange reasons, the console is not empty after
* running above command.
* So, we reset it to not have side effects for other tests.
*/
console_record_reset_enable();
} else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
ut_assert_console_end();
}
ut_asserteq(1, run_command("dollar_foo=bar quux\"", 0));
/* Two next lines contain error message */
ut_assert_skipline();
ut_assert_skipline();
if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
/* See above comments. */
console_record_reset_enable();
} else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
ut_assert_console_end();
}
ut_assertok(run_command("dollar_foo='bar \"quux'", 0));
ut_assertok(run_command("echo $dollar_foo", 0));
/*
* This one is buggy.
* ut_assert_nextline("bar \"quux");
* ut_assert_console_end();
*
* So, let's reset output:
*/
console_record_reset_enable();
if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
/*
* Old parser returns an error because it waits for closing
* '\'', but this behavior is wrong as the '\'' is surrounded by
* '"', so no need to wait for a closing one.
*/
ut_assertok(run_command("dollar_foo=\"bar 'quux\"", 0));
ut_assertok(run_command("echo $dollar_foo", 0));
ut_assert_nextline("bar 'quux");
ut_assert_console_end();
} else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
ut_asserteq(1, run_command("dollar_foo=\"bar 'quux\"", 0));
/* Next line contains error message */
ut_assert_skipline();
ut_assert_console_end();
}
ut_assertok(run_command("dollar_foo='bar quux'", 0));
ut_assertok(run_command("echo $dollar_foo", 0));
ut_assert_nextline("bar quux");
ut_assert_console_end();
if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
/* Reset local variable. */
ut_assertok(run_command("dollar_foo=", 0));
} else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
puts("Beware: this test set local variable dollar_foo and it cannot be unset!");
}
return 0;
}
HUSH_TEST(hush_test_simple_dollar, 0);
static int hush_test_env_dollar(struct unit_test_state *uts)
{
env_set("env_foo", "bar");
console_record_reset_enable();
ut_assertok(run_command("echo $env_foo", 0));
ut_assert_nextline("bar");
ut_assert_console_end();
ut_assertok(run_command("echo ${env_foo}", 0));
ut_assert_nextline("bar");
ut_assert_console_end();
/* Environment variables have priority over local variable */
ut_assertok(run_command("env_foo=quux", 0));
ut_assertok(run_command("echo ${env_foo}", 0));
ut_assert_nextline("bar");
ut_assert_console_end();
/* Clean up setting the variable */
env_set("env_foo", NULL);
if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
/* Reset local variable. */
ut_assertok(run_command("env_foo=", 0));
} else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
puts("Beware: this test set local variable env_foo and it cannot be unset!");
}
return 0;
}
HUSH_TEST(hush_test_env_dollar, 0);
static int hush_test_command_dollar(struct unit_test_state *uts)
{
console_record_reset_enable();
ut_assertok(run_command("dollar_bar=\"echo bar\"", 0));
ut_assertok(run_command("$dollar_bar", 0));
ut_assert_nextline("bar");
ut_assert_console_end();
ut_assertok(run_command("${dollar_bar}", 0));
ut_assert_nextline("bar");
ut_assert_console_end();
ut_assertok(run_command("dollar_bar=\"echo\nbar\"", 0));
ut_assertok(run_command("$dollar_bar", 0));
ut_assert_nextline("bar");
ut_assert_console_end();
ut_assertok(run_command("dollar_bar='echo bar\n'", 0));
ut_assertok(run_command("$dollar_bar", 0));
ut_assert_nextline("bar");
ut_assert_console_end();
ut_assertok(run_command("dollar_bar='echo bar\\n'", 0));
ut_assertok(run_command("$dollar_bar", 0));
if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
/*
* This difference seems to come from a bug solved in Busybox
* hush.
* Behavior of hush 2021 is coherent with bash and other shells.
*/
ut_assert_nextline("bar\\n");
} else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
ut_assert_nextline("barn");
}
ut_assert_console_end();
ut_assertok(run_command("dollar_bar='echo $bar'", 0));
ut_assertok(run_command("$dollar_bar", 0));
ut_assert_nextline("$bar");
ut_assert_console_end();
ut_assertok(run_command("dollar_quux=quux", 0));
ut_assertok(run_command("dollar_bar=\"echo $dollar_quux\"", 0));
ut_assertok(run_command("$dollar_bar", 0));
ut_assert_nextline("quux");
ut_assert_console_end();
if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
/* Reset local variables. */
ut_assertok(run_command("dollar_bar=", 0));
ut_assertok(run_command("dollar_quux=", 0));
} else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
puts("Beware: this test sets local variable dollar_bar and dollar_quux and they cannot be unset!");
}
return 0;
}
HUSH_TEST(hush_test_command_dollar, 0);

316
test/hush/if.c Normal file
View file

@ -0,0 +1,316 @@
// SPDX-License-Identifier: GPL-2.0
/*
* (C) Copyright 2021
* Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com
*/
#include <command.h>
#include <env_attr.h>
#include <vsprintf.h>
#include <test/hush.h>
#include <test/ut.h>
/*
* All tests will execute the following:
* if condition_to_test; then
* true
* else
* false
* fi
* If condition is true, command returns 1, 0 otherwise.
*/
const char *if_format = "if %s; then true; else false; fi";
static int hush_test_if_base(struct unit_test_state *uts)
{
char if_formatted[128];
sprintf(if_formatted, if_format, "true");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "false");
ut_asserteq(1, run_command(if_formatted, 0));
return 0;
}
HUSH_TEST(hush_test_if_base, 0);
static int hush_test_if_basic_operators(struct unit_test_state *uts)
{
char if_formatted[128];
sprintf(if_formatted, if_format, "test aaa = aaa");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test aaa = bbb");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test aaa != bbb");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test aaa != aaa");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test aaa < bbb");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test bbb < aaa");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test bbb > aaa");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test aaa > bbb");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 123 -eq 123");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 123 -eq 456");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 123 -ne 456");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 123 -ne 123");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 123 -lt 456");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 123 -lt 123");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 456 -lt 123");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 123 -le 456");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 123 -le 123");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 456 -le 123");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 456 -gt 123");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 123 -gt 123");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 123 -gt 456");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 456 -ge 123");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 123 -ge 123");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 123 -ge 456");
ut_asserteq(1, run_command(if_formatted, 0));
return 0;
}
HUSH_TEST(hush_test_if_basic_operators, 0);
static int hush_test_if_octal(struct unit_test_state *uts)
{
char if_formatted[128];
sprintf(if_formatted, if_format, "test 010 -eq 010");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 010 -eq 011");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 010 -ne 011");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 010 -ne 010");
ut_asserteq(1, run_command(if_formatted, 0));
return 0;
}
HUSH_TEST(hush_test_if_octal, 0);
static int hush_test_if_hexadecimal(struct unit_test_state *uts)
{
char if_formatted[128];
sprintf(if_formatted, if_format, "test 0x2000000 -gt 0x2000001");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 0x2000000 -gt 0x2000000");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 0x2000000 -gt 0x1ffffff");
ut_assertok(run_command(if_formatted, 0));
return 0;
}
HUSH_TEST(hush_test_if_hexadecimal, 0);
static int hush_test_if_mixed(struct unit_test_state *uts)
{
char if_formatted[128];
sprintf(if_formatted, if_format, "test 010 -eq 10");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 010 -ne 10");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 0xa -eq 10");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 0xa -eq 012");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 2000000 -gt 0x1ffffff");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 0x2000000 -gt 1ffffff");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 0x2000000 -lt 1ffffff");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 0x2000000 -eq 2000000");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test 0x2000000 -ne 2000000");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test -z \"\"");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test -z \"aaa\"");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test -n \"aaa\"");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test -n \"\"");
ut_asserteq(1, run_command(if_formatted, 0));
return 0;
}
HUSH_TEST(hush_test_if_mixed, 0);
static int hush_test_if_inverted(struct unit_test_state *uts)
{
char if_formatted[128];
sprintf(if_formatted, if_format, "test ! aaa = aaa");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test ! aaa = bbb");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test ! ! aaa = aaa");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test ! ! aaa = bbb");
ut_asserteq(1, run_command(if_formatted, 0));
return 0;
}
HUSH_TEST(hush_test_if_inverted, 0);
static int hush_test_if_binary(struct unit_test_state *uts)
{
char if_formatted[128];
sprintf(if_formatted, if_format, "test aaa != aaa -o bbb != bbb");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test aaa != aaa -o bbb = bbb");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test aaa = aaa -o bbb != bbb");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test aaa = aaa -o bbb = bbb");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test aaa != aaa -a bbb != bbb");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test aaa != aaa -a bbb = bbb");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test aaa = aaa -a bbb != bbb");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test aaa = aaa -a bbb = bbb");
ut_assertok(run_command(if_formatted, 0));
return 0;
}
HUSH_TEST(hush_test_if_binary, 0);
static int hush_test_if_inverted_binary(struct unit_test_state *uts)
{
char if_formatted[128];
sprintf(if_formatted, if_format, "test ! aaa != aaa -o ! bbb != bbb");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test ! aaa != aaa -o ! bbb = bbb");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test ! aaa = aaa -o ! bbb != bbb");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test ! aaa = aaa -o ! bbb = bbb");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format,
"test ! ! aaa != aaa -o ! ! bbb != bbb");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format,
"test ! ! aaa != aaa -o ! ! bbb = bbb");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format,
"test ! ! aaa = aaa -o ! ! bbb != bbb");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test ! ! aaa = aaa -o ! ! bbb = bbb");
ut_assertok(run_command(if_formatted, 0));
return 0;
}
HUSH_TEST(hush_test_if_inverted_binary, 0);
static int hush_test_if_z_operator(struct unit_test_state *uts)
{
char if_formatted[128];
/* Deal with environment variable used during test. */
env_set("ut_var_nonexistent", NULL);
env_set("ut_var_exists", "1");
env_set("ut_var_unset", "1");
sprintf(if_formatted, if_format, "test -z \"$ut_var_nonexistent\"");
ut_assertok(run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test -z \"$ut_var_exists\"");
ut_asserteq(1, run_command(if_formatted, 0));
sprintf(if_formatted, if_format, "test -z \"$ut_var_unset\"");
ut_asserteq(1, run_command(if_formatted, 0));
env_set("ut_var_unset", NULL);
sprintf(if_formatted, if_format, "test -z \"$ut_var_unset\"");
ut_assertok(run_command(if_formatted, 0));
/* Clear the set environment variable. */
env_set("ut_var_exists", NULL);
return 0;
}
HUSH_TEST(hush_test_if_z_operator, 0);

139
test/hush/list.c Normal file
View file

@ -0,0 +1,139 @@
// SPDX-License-Identifier: GPL-2.0
/*
* (C) Copyright 2021
* Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com
*/
#include <command.h>
#include <env_attr.h>
#include <test/hush.h>
#include <test/ut.h>
#include <asm/global_data.h>
static int hush_test_semicolon(struct unit_test_state *uts)
{
/* A; B = B truth table. */
ut_asserteq(1, run_command("false; false", 0));
ut_assertok(run_command("false; true", 0));
ut_assertok(run_command("true; true", 0));
ut_asserteq(1, run_command("true; false", 0));
return 0;
}
HUSH_TEST(hush_test_semicolon, 0);
static int hush_test_and(struct unit_test_state *uts)
{
/* A && B truth table. */
ut_asserteq(1, run_command("false && false", 0));
ut_asserteq(1, run_command("false && true", 0));
ut_assertok(run_command("true && true", 0));
ut_asserteq(1, run_command("true && false", 0));
return 0;
}
HUSH_TEST(hush_test_and, 0);
static int hush_test_or(struct unit_test_state *uts)
{
/* A || B truth table. */
ut_asserteq(1, run_command("false || false", 0));
ut_assertok(run_command("false || true", 0));
ut_assertok(run_command("true || true", 0));
ut_assertok(run_command("true || false", 0));
return 0;
}
HUSH_TEST(hush_test_or, 0);
DECLARE_GLOBAL_DATA_PTR;
static int hush_test_and_or(struct unit_test_state *uts)
{
/* A && B || C truth table. */
ut_asserteq(1, run_command("false && false || false", 0));
if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
ut_asserteq(1, run_command("false && false || true", 0));
} else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
/*
* This difference seems to come from a bug solved in Busybox
* hush.
*
* Indeed, the following expression can be seen like this:
* (false && false) || true
* So, (false && false) returns 1, the second false is not
* executed, and true is executed because of ||.
*/
ut_assertok(run_command("false && false || true", 0));
}
if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
ut_asserteq(1, run_command("false && true || true", 0));
} else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
/*
* This difference seems to come from a bug solved in Busybox
* hush.
*
* Indeed, the following expression can be seen like this:
* (false && true) || true
* So, (false && true) returns 1, the true is not executed, and
* true is executed because of ||.
*/
ut_assertok(run_command("false && true || true", 0));
}
ut_asserteq(1, run_command("false && true || false", 0));
ut_assertok(run_command("true && true || false", 0));
ut_asserteq(1, run_command("true && false || false", 0));
ut_assertok(run_command("true && false || true", 0));
ut_assertok(run_command("true && true || true", 0));
return 0;
}
HUSH_TEST(hush_test_and_or, 0);
static int hush_test_or_and(struct unit_test_state *uts)
{
/* A || B && C truth table. */
ut_asserteq(1, run_command("false || false && false", 0));
ut_asserteq(1, run_command("false || false && true", 0));
ut_assertok(run_command("false || true && true", 0));
ut_asserteq(1, run_command("false || true && false", 0));
if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
ut_assertok(run_command("true || true && false", 0));
} else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
/*
* This difference seems to come from a bug solved in Busybox
* hush.
*
* Indeed, the following expression can be seen like this:
* (true || true) && false
* So, (true || true) returns 0, the second true is not
* executed, and then false is executed because of &&.
*/
ut_asserteq(1, run_command("true || true && false", 0));
}
if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
ut_assertok(run_command("true || false && false", 0));
} else if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
/*
* This difference seems to come from a bug solved in Busybox
* hush.
*
* Indeed, the following expression can be seen like this:
* (true || false) && false
* So, (true || false) returns 0, the false is not executed, and
* then false is executed because of &&.
*/
ut_asserteq(1, run_command("true || false && false", 0));
}
ut_assertok(run_command("true || false && true", 0));
ut_assertok(run_command("true || true && true", 0));
return 0;
}
HUSH_TEST(hush_test_or_and, 0);

90
test/hush/loop.c Normal file
View file

@ -0,0 +1,90 @@
// SPDX-License-Identifier: GPL-2.0
/*
* (C) Copyright 2021
* Francis Laniel, Amarula Solutions, francis.laniel@amarulasolutions.com
*/
#include <command.h>
#include <env_attr.h>
#include <test/hush.h>
#include <test/ut.h>
#include <asm/global_data.h>
DECLARE_GLOBAL_DATA_PTR;
static int hush_test_for(struct unit_test_state *uts)
{
console_record_reset_enable();
ut_assertok(run_command("for loop_i in foo bar quux quux; do echo $loop_i; done", 0));
ut_assert_nextline("foo");
ut_assert_nextline("bar");
ut_assert_nextline("quux");
ut_assert_nextline("quux");
ut_assert_console_end();
if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
/* Reset local variable. */
ut_assertok(run_command("loop_i=", 0));
} else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
puts("Beware: this test set local variable loop_i and it cannot be unset!");
}
return 0;
}
HUSH_TEST(hush_test_for, 0);
static int hush_test_while(struct unit_test_state *uts)
{
console_record_reset_enable();
if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
/*
* Hush 2021 always returns 0 from while loop...
* You can see code snippet near this line to have a better
* understanding:
* debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n");
*/
ut_assertok(run_command("while test -z \"$loop_foo\"; do echo bar; loop_foo=quux; done", 0));
} else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
/*
* Exit status is that of test, so 1 since test is false to quit
* the loop.
*/
ut_asserteq(1, run_command("while test -z \"$loop_foo\"; do echo bar; loop_foo=quux; done", 0));
}
ut_assert_nextline("bar");
ut_assert_console_end();
if (gd->flags & GD_FLG_HUSH_MODERN_PARSER) {
/* Reset local variable. */
ut_assertok(run_command("loop_foo=", 0));
} else if (gd->flags & GD_FLG_HUSH_OLD_PARSER) {
puts("Beware: this test set local variable loop_foo and it cannot be unset!");
}
return 0;
}
HUSH_TEST(hush_test_while, 0);
static int hush_test_until(struct unit_test_state *uts)
{
console_record_reset_enable();
env_set("loop_bar", "bar");
/*
* WARNING We have to use environment variable because it is not possible
* resetting local variable.
*/
ut_assertok(run_command("until test -z \"$loop_bar\"; do echo quux; setenv loop_bar; done", 0));
ut_assert_nextline("quux");
ut_assert_console_end();
/*
* Loop normally resets foo environment variable, but we reset it here in
* case the test failed.
*/
env_set("loop_bar", NULL);
return 0;
}
HUSH_TEST(hush_test_until, 0);

View file

@ -1,197 +0,0 @@
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2015-2016, NVIDIA CORPORATION. All rights reserved.
# Test operation of the "if" shell command.
import os
import os.path
import pytest
# TODO: These tests should be converted to a C test.
# For more information please take a look at the thread
# https://lists.denx.de/pipermail/u-boot/2019-October/388732.html
pytestmark = pytest.mark.buildconfigspec('hush_parser')
# The list of "if test" conditions to test.
subtests = (
# Base if functionality.
('true', True),
('false', False),
# Basic operators.
('test aaa = aaa', True),
('test aaa = bbb', False),
('test aaa != bbb', True),
('test aaa != aaa', False),
('test aaa < bbb', True),
('test bbb < aaa', False),
('test bbb > aaa', True),
('test aaa > bbb', False),
('test 123 -eq 123', True),
('test 123 -eq 456', False),
('test 123 -ne 456', True),
('test 123 -ne 123', False),
('test 123 -lt 456', True),
('test 123 -lt 123', False),
('test 456 -lt 123', False),
('test 123 -le 456', True),
('test 123 -le 123', True),
('test 456 -le 123', False),
('test 456 -gt 123', True),
('test 123 -gt 123', False),
('test 123 -gt 456', False),
('test 456 -ge 123', True),
('test 123 -ge 123', True),
('test 123 -ge 456', False),
# Octal tests
('test 010 -eq 010', True),
('test 010 -eq 011', False),
('test 010 -ne 011', True),
('test 010 -ne 010', False),
# Hexadecimal tests
('test 0x2000000 -gt 0x2000001', False),
('test 0x2000000 -gt 0x2000000', False),
('test 0x2000000 -gt 0x1ffffff', True),
# Mixed tests
('test 010 -eq 10', False),
('test 010 -ne 10', True),
('test 0xa -eq 10', True),
('test 0xa -eq 012', True),
('test 2000000 -gt 0x1ffffff', False),
('test 0x2000000 -gt 1ffffff', True),
('test 0x2000000 -lt 1ffffff', False),
('test 0x2000000 -eq 2000000', False),
('test 0x2000000 -ne 2000000', True),
('test -z ""', True),
('test -z "aaa"', False),
('test -n "aaa"', True),
('test -n ""', False),
# Inversion of simple tests.
('test ! aaa = aaa', False),
('test ! aaa = bbb', True),
('test ! ! aaa = aaa', True),
('test ! ! aaa = bbb', False),
# Binary operators.
('test aaa != aaa -o bbb != bbb', False),
('test aaa != aaa -o bbb = bbb', True),
('test aaa = aaa -o bbb != bbb', True),
('test aaa = aaa -o bbb = bbb', True),
('test aaa != aaa -a bbb != bbb', False),
('test aaa != aaa -a bbb = bbb', False),
('test aaa = aaa -a bbb != bbb', False),
('test aaa = aaa -a bbb = bbb', True),
# Inversion within binary operators.
('test ! aaa != aaa -o ! bbb != bbb', True),
('test ! aaa != aaa -o ! bbb = bbb', True),
('test ! aaa = aaa -o ! bbb != bbb', True),
('test ! aaa = aaa -o ! bbb = bbb', False),
('test ! ! aaa != aaa -o ! ! bbb != bbb', False),
('test ! ! aaa != aaa -o ! ! bbb = bbb', True),
('test ! ! aaa = aaa -o ! ! bbb != bbb', True),
('test ! ! aaa = aaa -o ! ! bbb = bbb', True),
)
def exec_hush_if(u_boot_console, expr, result):
"""Execute a shell "if" command, and validate its result."""
config = u_boot_console.config.buildconfig
maxargs = int(config.get('config_sys_maxargs', '0'))
args = len(expr.split(' ')) - 1
if args > maxargs:
u_boot_console.log.warning('CONFIG_SYS_MAXARGS too low; need ' +
str(args))
pytest.skip()
cmd = 'if ' + expr + '; then echo true; else echo false; fi'
response = u_boot_console.run_command(cmd)
assert response.strip() == str(result).lower()
@pytest.mark.buildconfigspec('cmd_echo')
@pytest.mark.parametrize('expr,result', subtests)
def test_hush_if_test(u_boot_console, expr, result):
"""Test a single "if test" condition."""
exec_hush_if(u_boot_console, expr, result)
def test_hush_z(u_boot_console):
"""Test the -z operator"""
u_boot_console.run_command('setenv ut_var_nonexistent')
u_boot_console.run_command('setenv ut_var_exists 1')
exec_hush_if(u_boot_console, 'test -z "$ut_var_nonexistent"', True)
exec_hush_if(u_boot_console, 'test -z "$ut_var_exists"', False)
u_boot_console.run_command('setenv ut_var_exists')
# We might test this on real filesystems via UMS, DFU, 'save', etc.
# Of those, only UMS currently allows file removal though.
@pytest.mark.buildconfigspec('cmd_echo')
@pytest.mark.boardspec('sandbox')
def test_hush_if_test_host_file_exists(u_boot_console):
"""Test the "if test -e" shell command."""
test_file = u_boot_console.config.result_dir + \
'/creating_this_file_breaks_u_boot_tests'
try:
os.unlink(test_file)
except:
pass
assert not os.path.exists(test_file)
expr = 'test -e hostfs - ' + test_file
exec_hush_if(u_boot_console, expr, False)
try:
with open(test_file, 'wb'):
pass
assert os.path.exists(test_file)
expr = 'test -e hostfs - ' + test_file
exec_hush_if(u_boot_console, expr, True)
finally:
os.unlink(test_file)
expr = 'test -e hostfs - ' + test_file
exec_hush_if(u_boot_console, expr, False)
def test_hush_var(u_boot_console):
"""Test the set and unset of variables"""
u_boot_console.run_command('ut_var_nonexistent=')
u_boot_console.run_command('ut_var_exists=1')
u_boot_console.run_command('ut_var_unset=1')
exec_hush_if(u_boot_console, 'test -z "$ut_var_nonexistent"', True)
exec_hush_if(u_boot_console, 'test -z "$ut_var_exists"', False)
exec_hush_if(u_boot_console, 'test -z "$ut_var_unset"', False)
exec_hush_if(u_boot_console, 'ut_var_unset=', True)
exec_hush_if(u_boot_console, 'test -z "$ut_var_unset"', True)
u_boot_console.run_command('ut_var_exists=')
u_boot_console.run_command('ut_var_unset=')

View file

@ -500,5 +500,11 @@ def test_ut(u_boot_console, ut_subtest):
execute command 'ut foo bar'
"""
output = u_boot_console.run_command('ut ' + ut_subtest)
if ut_subtest == 'hush hush_test_simple_dollar':
# ut hush hush_test_simple_dollar prints "Unknown command" on purpose.
with u_boot_console.disable_check('unknown_command'):
output = u_boot_console.run_command('ut ' + ut_subtest)
assert('Unknown command \'quux\' - try \'help\'' in output)
else:
output = u_boot_console.run_command('ut ' + ut_subtest)
assert output.endswith('Failures: 0')