u-boot/common/cli_hush_modern.c
Tom Rini 2b28c3b871 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).
2023-12-28 14:38:25 -05:00

324 lines
6.8 KiB
C

// 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);
}