History log of /u-boot/include/os.h
Revision Date Author Comments
(<<< Hide modified files)
(Show modified files >>>)
# 9181cb05 04-Nov-2023 Sean Anderson <seanga2@gmail.com>

arch: sandbox: Add function to create temporary files

When working with sparse data buffers that may be larger than the address
space, it is convenient to work with files instead. Add a function to create
temporary files of a certain size.

Signed-off-by: Sean Anderson <seanga2@gmail.com>

# e2d22f78 24-Aug-2023 Simon Glass <sjg@chromium.org>

sandbox: Add a way to access persistent test files

Some pytests create files in the persistent-data directory. It is useful
to be able to access these files in C tests. Add a function which can
locate a file given its leaf name, using the environment variable set
up in test/py/conftest.py

Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a07d395 05-Apr-2023 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: fix return type of os_filesize()

Given a file ../img of size 4294967296 with GPT partition table and
partitions:

=> host bind 0 ../img
=> part list host 0
Disk host-0.blk not ready

The cause is os_filesize() returning int. File sizes must use off_t.

Correct all uses of os_filesize() too.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 6ca4d5b9 29-Oct-2022 Simon Glass <sjg@chromium.org>

sandbox: Add missing comments for os_alarm()

Add the documentation to avoid a warning with 'make htmldocs'.

Fixes: 10107efedd5 ("sandbox: add SIGALRM-based watchdog device")

Signed-off-by: Simon Glass <sjg@chromium.org>

# 10107efe 27-Sep-2022 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

sandbox: add SIGALRM-based watchdog device

In order to test that U-Boot actually maintains the watchdog device(s)
during long-running busy-loops, such as those where we wait for the
user to stop autoboot, we need a watchdog device that actually does
something during those loops; we cannot test that behaviour via the DM
test framework.

So introduce a relatively simple watchdog device which is simply based
on calling the host OS' alarm() function; that has the nice property
that a new call to alarm() simply sets a new deadline, and alarm(0)
cancels any existing alarm. These properties are precisely what we
need to implement start/reset/stop. We install our own handler so that
we get a known message printed if and when the watchdog fires, and by
just invoking that handler directly, we get expire_now for free.

The actual calls to the various OS functions (alarm, signal, raise)
need to be done in os.c, and since the driver code cannot get access
to the values of SIGALRM or SIG_DFL (that would require including a
host header, and that's only os.c which can do that), we cannot simply
do trivial wrappers for signal() and raise(), but instead create
specialized functions just for use by this driver.

Apart from enabling this driver for sandbox{,64}_defconfig, also
enable the wdt command which was useful for hand-testing this new
driver (especially with running u-boot under strace).

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>

# 7df5b353 05-Sep-2022 This contributor prefers not to receive mails <noreply@example.com>

sandbox: Add function os_flush()

It flushes stdout.

Signed-off-by: Pali Rohár <pali@kernel.org>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 7750ee45 04-Apr-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add function os_printf()

Before setting up the devices U-Boot's printf() function cannot be used
for console output. Provide function os_printf() to print to stderr.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 880dbc5f 10-Jan-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: compatibility of os_get_filesize()

U-Boot define loff_t as long long. But the header
/usr/include/linux/types.h may not define it.
This has lead to a build error on Alpine Linux.

So let's use long long instead of loff_t for
the size parameter of function os_get_filesize().

Reported-by: Milan P. Stanić <mps@arvanta.net>
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Milan P. Stanić <mps@arvanta.net>
Reviewed-by: Simon Glass <sjg@chromium.org>

# a0ff280a 23-Oct-2021 Simon Glass <sjg@chromium.org>

sandbox: Support unmapping a file

Add the opposite of mapping, so that we can unmap and avoid running out of
address space.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b9274095 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to map a file into memory

It is useful to map a file into memory so that it can be accessed using
simple pointers. Add a function to support this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b4467fae 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to find the size of a file

Add a function to return the size of a file. This is useful in situations
where we need to allocate memory for it before reading it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Behún <marek.behun@nic.cz>

# f178bebf 05-Jul-2021 Simon Glass <sjg@chromium.org>

sandbox: Support executables for more phases

The SPL header has a function for obtaining the phase in capital letters,
e.g. 'SPL'. Add one for lower-case also, as used by sandbox.

Use this to generalise the sandbox logic for determining the filename of
the next sandbox executable. This can provide support for VPL.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4c30d18e 28-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: document parameters of os_realloc()

Avoid 'make htmldocs' build warnings:

./include/os.h:139: warning:
Function parameter or member 'ptr' not described in 'os_realloc'
./include/os.h:139: warning:
Function parameter or member 'length' not described in 'os_realloc'

Fixes: 14e46dfb176b ("sandbox: Add os_realloc()")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 01ad9f75 07-Mar-2021 Simon Glass <sjg@chromium.org>

sandbox: Update os_find_u_boot() to find the .img file

At present this function can only locate the u-boot ELF file. For SPL it
is handy to be able to locate u-boot.img since this is what would normally
be loaded by SPL.

Add another argument to allow this to be selected.

While we are here, update the function to load SPL when running in TPL,
since that is the next stage.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b308d9fd 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Avoid using malloc() for system state

This state is not accessible to the running U-Boot but at present it is
allocated in the emulated SDRAM. This doesn't seem very useful. Adjust
it to allocate from the OS instead.

The RAM buffer is currently not freed, but should be, so add that into
state_uninit(). Update the comment for os_free() to indicate that NULL is
a valid parameter value.

Note that the strdup() in spl_board_load_image() is changed as well, since
strdup() allocates memory in the RAM buffer.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 14e46dfb 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Add os_realloc()

We provide os_malloc() and os_free() but not os_realloc(). Add this,
following the usual semantics. Also update os_malloc() to behave correctly
when passed a zero size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c30a7093 31-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: mark os_abort() as noreturn

gcc -fanalyzer needs the information that a function does not return to
provide accurate information.

os_abort() does not return. Mark it accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 43db0750 30-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: keep time offset when resetting

The UEFI Self Certification Test (SCT) checks the SetTime() service with
the following steps:

* set date
* reset
* check date matches

To be compliant the sandbox should keep the offset to the host RTC during
resets. The implementation uses the environment variable
UBOOT_SB_TIME_OFFSET to persist the offset.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# b46f30a3 11-Nov-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add handler for exceptions

Add a handler for SIGILL, SIGBUS, SIGSEGV.

When an exception occurs print the program counter and the loaded
UEFI binaries and reset the system if CONFIG_SANDBOX_CRASH_RESET=y
or exit to the OS otherwise.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 063790cb 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: update function descriptions in os.h

Use Sphinx style function descriptions.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 329dccc0 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: implement reset

Up to now the sandbox would shutdown upon a cold reset request. Instead it
should be reset.

In our coding we use static variables like LIST_HEAD(efi_obj_list). A reset
can occur at any time, e.g. via an UEFI binary calling the reset service.
The only safe way to return to an initial state is to relaunch the U-Boot
binary.

The reset implementation uses execv() to relaunch U-Boot.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 89cdb0b5 03-Feb-2020 Simon Glass <sjg@chromium.org>

sandbox: Drop os_realloc()

Due to recent changes this function is no-longer used. Drop it.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@csgraf.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <masahiroy@kernel.org>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <masahiroy@kernel.org>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# e2d22f78 24-Aug-2023 Simon Glass <sjg@chromium.org>

sandbox: Add a way to access persistent test files

Some pytests create files in the persistent-data directory. It is useful
to be able to access these files in C tests. Add a function which can
locate a file given its leaf name, using the environment variable set
up in test/py/conftest.py

Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a07d395 05-Apr-2023 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: fix return type of os_filesize()

Given a file ../img of size 4294967296 with GPT partition table and
partitions:

=> host bind 0 ../img
=> part list host 0
Disk host-0.blk not ready

The cause is os_filesize() returning int. File sizes must use off_t.

Correct all uses of os_filesize() too.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 6ca4d5b9 29-Oct-2022 Simon Glass <sjg@chromium.org>

sandbox: Add missing comments for os_alarm()

Add the documentation to avoid a warning with 'make htmldocs'.

Fixes: 10107efedd5 ("sandbox: add SIGALRM-based watchdog device")

Signed-off-by: Simon Glass <sjg@chromium.org>

# 10107efe 27-Sep-2022 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

sandbox: add SIGALRM-based watchdog device

In order to test that U-Boot actually maintains the watchdog device(s)
during long-running busy-loops, such as those where we wait for the
user to stop autoboot, we need a watchdog device that actually does
something during those loops; we cannot test that behaviour via the DM
test framework.

So introduce a relatively simple watchdog device which is simply based
on calling the host OS' alarm() function; that has the nice property
that a new call to alarm() simply sets a new deadline, and alarm(0)
cancels any existing alarm. These properties are precisely what we
need to implement start/reset/stop. We install our own handler so that
we get a known message printed if and when the watchdog fires, and by
just invoking that handler directly, we get expire_now for free.

The actual calls to the various OS functions (alarm, signal, raise)
need to be done in os.c, and since the driver code cannot get access
to the values of SIGALRM or SIG_DFL (that would require including a
host header, and that's only os.c which can do that), we cannot simply
do trivial wrappers for signal() and raise(), but instead create
specialized functions just for use by this driver.

Apart from enabling this driver for sandbox{,64}_defconfig, also
enable the wdt command which was useful for hand-testing this new
driver (especially with running u-boot under strace).

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>

# 7df5b353 05-Sep-2022 Pali Rohár <pali@kernel.org>

sandbox: Add function os_flush()

It flushes stdout.

Signed-off-by: Pali Rohár <pali@kernel.org>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 7750ee45 04-Apr-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add function os_printf()

Before setting up the devices U-Boot's printf() function cannot be used
for console output. Provide function os_printf() to print to stderr.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 880dbc5f 10-Jan-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: compatibility of os_get_filesize()

U-Boot define loff_t as long long. But the header
/usr/include/linux/types.h may not define it.
This has lead to a build error on Alpine Linux.

So let's use long long instead of loff_t for
the size parameter of function os_get_filesize().

Reported-by: Milan P. Stanić <mps@arvanta.net>
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Milan P. Stanić <mps@arvanta.net>
Reviewed-by: Simon Glass <sjg@chromium.org>

# a0ff280a 23-Oct-2021 Simon Glass <sjg@chromium.org>

sandbox: Support unmapping a file

Add the opposite of mapping, so that we can unmap and avoid running out of
address space.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b9274095 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to map a file into memory

It is useful to map a file into memory so that it can be accessed using
simple pointers. Add a function to support this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b4467fae 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to find the size of a file

Add a function to return the size of a file. This is useful in situations
where we need to allocate memory for it before reading it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Behún <marek.behun@nic.cz>

# f178bebf 05-Jul-2021 Simon Glass <sjg@chromium.org>

sandbox: Support executables for more phases

The SPL header has a function for obtaining the phase in capital letters,
e.g. 'SPL'. Add one for lower-case also, as used by sandbox.

Use this to generalise the sandbox logic for determining the filename of
the next sandbox executable. This can provide support for VPL.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4c30d18e 28-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: document parameters of os_realloc()

Avoid 'make htmldocs' build warnings:

./include/os.h:139: warning:
Function parameter or member 'ptr' not described in 'os_realloc'
./include/os.h:139: warning:
Function parameter or member 'length' not described in 'os_realloc'

Fixes: 14e46dfb176b ("sandbox: Add os_realloc()")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 01ad9f75 07-Mar-2021 Simon Glass <sjg@chromium.org>

sandbox: Update os_find_u_boot() to find the .img file

At present this function can only locate the u-boot ELF file. For SPL it
is handy to be able to locate u-boot.img since this is what would normally
be loaded by SPL.

Add another argument to allow this to be selected.

While we are here, update the function to load SPL when running in TPL,
since that is the next stage.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b308d9fd 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Avoid using malloc() for system state

This state is not accessible to the running U-Boot but at present it is
allocated in the emulated SDRAM. This doesn't seem very useful. Adjust
it to allocate from the OS instead.

The RAM buffer is currently not freed, but should be, so add that into
state_uninit(). Update the comment for os_free() to indicate that NULL is
a valid parameter value.

Note that the strdup() in spl_board_load_image() is changed as well, since
strdup() allocates memory in the RAM buffer.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 14e46dfb 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Add os_realloc()

We provide os_malloc() and os_free() but not os_realloc(). Add this,
following the usual semantics. Also update os_malloc() to behave correctly
when passed a zero size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c30a7093 31-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: mark os_abort() as noreturn

gcc -fanalyzer needs the information that a function does not return to
provide accurate information.

os_abort() does not return. Mark it accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 43db0750 30-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: keep time offset when resetting

The UEFI Self Certification Test (SCT) checks the SetTime() service with
the following steps:

* set date
* reset
* check date matches

To be compliant the sandbox should keep the offset to the host RTC during
resets. The implementation uses the environment variable
UBOOT_SB_TIME_OFFSET to persist the offset.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# b46f30a3 11-Nov-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add handler for exceptions

Add a handler for SIGILL, SIGBUS, SIGSEGV.

When an exception occurs print the program counter and the loaded
UEFI binaries and reset the system if CONFIG_SANDBOX_CRASH_RESET=y
or exit to the OS otherwise.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 063790cb 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: update function descriptions in os.h

Use Sphinx style function descriptions.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 329dccc0 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: implement reset

Up to now the sandbox would shutdown upon a cold reset request. Instead it
should be reset.

In our coding we use static variables like LIST_HEAD(efi_obj_list). A reset
can occur at any time, e.g. via an UEFI binary calling the reset service.
The only safe way to return to an initial state is to relaunch the U-Boot
binary.

The reset implementation uses execv() to relaunch U-Boot.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 89cdb0b5 03-Feb-2020 Simon Glass <sjg@chromium.org>

sandbox: Drop os_realloc()

Due to recent changes this function is no-longer used. Drop it.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@csgraf.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <masahiroy@kernel.org>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <masahiroy@kernel.org>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a07d395 05-Apr-2023 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: fix return type of os_filesize()

Given a file ../img of size 4294967296 with GPT partition table and
partitions:

=> host bind 0 ../img
=> part list host 0
Disk host-0.blk not ready

The cause is os_filesize() returning int. File sizes must use off_t.

Correct all uses of os_filesize() too.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 6ca4d5b9 29-Oct-2022 Simon Glass <sjg@chromium.org>

sandbox: Add missing comments for os_alarm()

Add the documentation to avoid a warning with 'make htmldocs'.

Fixes: 10107efedd5 ("sandbox: add SIGALRM-based watchdog device")

Signed-off-by: Simon Glass <sjg@chromium.org>

# 10107efe 27-Sep-2022 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

sandbox: add SIGALRM-based watchdog device

In order to test that U-Boot actually maintains the watchdog device(s)
during long-running busy-loops, such as those where we wait for the
user to stop autoboot, we need a watchdog device that actually does
something during those loops; we cannot test that behaviour via the DM
test framework.

So introduce a relatively simple watchdog device which is simply based
on calling the host OS' alarm() function; that has the nice property
that a new call to alarm() simply sets a new deadline, and alarm(0)
cancels any existing alarm. These properties are precisely what we
need to implement start/reset/stop. We install our own handler so that
we get a known message printed if and when the watchdog fires, and by
just invoking that handler directly, we get expire_now for free.

The actual calls to the various OS functions (alarm, signal, raise)
need to be done in os.c, and since the driver code cannot get access
to the values of SIGALRM or SIG_DFL (that would require including a
host header, and that's only os.c which can do that), we cannot simply
do trivial wrappers for signal() and raise(), but instead create
specialized functions just for use by this driver.

Apart from enabling this driver for sandbox{,64}_defconfig, also
enable the wdt command which was useful for hand-testing this new
driver (especially with running u-boot under strace).

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>

# 7df5b353 05-Sep-2022 Pali Rohár <pali@kernel.org>

sandbox: Add function os_flush()

It flushes stdout.

Signed-off-by: Pali Rohár <pali@kernel.org>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 7750ee45 04-Apr-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add function os_printf()

Before setting up the devices U-Boot's printf() function cannot be used
for console output. Provide function os_printf() to print to stderr.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 880dbc5f 10-Jan-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: compatibility of os_get_filesize()

U-Boot define loff_t as long long. But the header
/usr/include/linux/types.h may not define it.
This has lead to a build error on Alpine Linux.

So let's use long long instead of loff_t for
the size parameter of function os_get_filesize().

Reported-by: Milan P. Stanić <mps@arvanta.net>
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Milan P. Stanić <mps@arvanta.net>
Reviewed-by: Simon Glass <sjg@chromium.org>

# a0ff280a 23-Oct-2021 Simon Glass <sjg@chromium.org>

sandbox: Support unmapping a file

Add the opposite of mapping, so that we can unmap and avoid running out of
address space.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b9274095 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to map a file into memory

It is useful to map a file into memory so that it can be accessed using
simple pointers. Add a function to support this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b4467fae 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to find the size of a file

Add a function to return the size of a file. This is useful in situations
where we need to allocate memory for it before reading it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Behún <marek.behun@nic.cz>

# f178bebf 05-Jul-2021 Simon Glass <sjg@chromium.org>

sandbox: Support executables for more phases

The SPL header has a function for obtaining the phase in capital letters,
e.g. 'SPL'. Add one for lower-case also, as used by sandbox.

Use this to generalise the sandbox logic for determining the filename of
the next sandbox executable. This can provide support for VPL.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4c30d18e 28-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: document parameters of os_realloc()

Avoid 'make htmldocs' build warnings:

./include/os.h:139: warning:
Function parameter or member 'ptr' not described in 'os_realloc'
./include/os.h:139: warning:
Function parameter or member 'length' not described in 'os_realloc'

Fixes: 14e46dfb176b ("sandbox: Add os_realloc()")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 01ad9f75 07-Mar-2021 Simon Glass <sjg@chromium.org>

sandbox: Update os_find_u_boot() to find the .img file

At present this function can only locate the u-boot ELF file. For SPL it
is handy to be able to locate u-boot.img since this is what would normally
be loaded by SPL.

Add another argument to allow this to be selected.

While we are here, update the function to load SPL when running in TPL,
since that is the next stage.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b308d9fd 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Avoid using malloc() for system state

This state is not accessible to the running U-Boot but at present it is
allocated in the emulated SDRAM. This doesn't seem very useful. Adjust
it to allocate from the OS instead.

The RAM buffer is currently not freed, but should be, so add that into
state_uninit(). Update the comment for os_free() to indicate that NULL is
a valid parameter value.

Note that the strdup() in spl_board_load_image() is changed as well, since
strdup() allocates memory in the RAM buffer.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 14e46dfb 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Add os_realloc()

We provide os_malloc() and os_free() but not os_realloc(). Add this,
following the usual semantics. Also update os_malloc() to behave correctly
when passed a zero size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c30a7093 31-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: mark os_abort() as noreturn

gcc -fanalyzer needs the information that a function does not return to
provide accurate information.

os_abort() does not return. Mark it accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 43db0750 30-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: keep time offset when resetting

The UEFI Self Certification Test (SCT) checks the SetTime() service with
the following steps:

* set date
* reset
* check date matches

To be compliant the sandbox should keep the offset to the host RTC during
resets. The implementation uses the environment variable
UBOOT_SB_TIME_OFFSET to persist the offset.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# b46f30a3 11-Nov-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add handler for exceptions

Add a handler for SIGILL, SIGBUS, SIGSEGV.

When an exception occurs print the program counter and the loaded
UEFI binaries and reset the system if CONFIG_SANDBOX_CRASH_RESET=y
or exit to the OS otherwise.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 063790cb 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: update function descriptions in os.h

Use Sphinx style function descriptions.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 329dccc0 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: implement reset

Up to now the sandbox would shutdown upon a cold reset request. Instead it
should be reset.

In our coding we use static variables like LIST_HEAD(efi_obj_list). A reset
can occur at any time, e.g. via an UEFI binary calling the reset service.
The only safe way to return to an initial state is to relaunch the U-Boot
binary.

The reset implementation uses execv() to relaunch U-Boot.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 89cdb0b5 03-Feb-2020 Simon Glass <sjg@chromium.org>

sandbox: Drop os_realloc()

Due to recent changes this function is no-longer used. Drop it.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@csgraf.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 6ca4d5b9 29-Oct-2022 Simon Glass <sjg@chromium.org>

sandbox: Add missing comments for os_alarm()

Add the documentation to avoid a warning with 'make htmldocs'.

Fixes: 10107efedd5 ("sandbox: add SIGALRM-based watchdog device")

Signed-off-by: Simon Glass <sjg@chromium.org>

# 10107efe 27-Sep-2022 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

sandbox: add SIGALRM-based watchdog device

In order to test that U-Boot actually maintains the watchdog device(s)
during long-running busy-loops, such as those where we wait for the
user to stop autoboot, we need a watchdog device that actually does
something during those loops; we cannot test that behaviour via the DM
test framework.

So introduce a relatively simple watchdog device which is simply based
on calling the host OS' alarm() function; that has the nice property
that a new call to alarm() simply sets a new deadline, and alarm(0)
cancels any existing alarm. These properties are precisely what we
need to implement start/reset/stop. We install our own handler so that
we get a known message printed if and when the watchdog fires, and by
just invoking that handler directly, we get expire_now for free.

The actual calls to the various OS functions (alarm, signal, raise)
need to be done in os.c, and since the driver code cannot get access
to the values of SIGALRM or SIG_DFL (that would require including a
host header, and that's only os.c which can do that), we cannot simply
do trivial wrappers for signal() and raise(), but instead create
specialized functions just for use by this driver.

Apart from enabling this driver for sandbox{,64}_defconfig, also
enable the wdt command which was useful for hand-testing this new
driver (especially with running u-boot under strace).

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>

# 7df5b353 05-Sep-2022 Pali Rohár <pali@kernel.org>

sandbox: Add function os_flush()

It flushes stdout.

Signed-off-by: Pali Rohár <pali@kernel.org>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 7750ee45 04-Apr-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add function os_printf()

Before setting up the devices U-Boot's printf() function cannot be used
for console output. Provide function os_printf() to print to stderr.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 880dbc5f 10-Jan-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: compatibility of os_get_filesize()

U-Boot define loff_t as long long. But the header
/usr/include/linux/types.h may not define it.
This has lead to a build error on Alpine Linux.

So let's use long long instead of loff_t for
the size parameter of function os_get_filesize().

Reported-by: Milan P. Stanić <mps@arvanta.net>
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Milan P. Stanić <mps@arvanta.net>
Reviewed-by: Simon Glass <sjg@chromium.org>

# a0ff280a 23-Oct-2021 Simon Glass <sjg@chromium.org>

sandbox: Support unmapping a file

Add the opposite of mapping, so that we can unmap and avoid running out of
address space.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b9274095 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to map a file into memory

It is useful to map a file into memory so that it can be accessed using
simple pointers. Add a function to support this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b4467fae 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to find the size of a file

Add a function to return the size of a file. This is useful in situations
where we need to allocate memory for it before reading it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Behún <marek.behun@nic.cz>

# f178bebf 05-Jul-2021 Simon Glass <sjg@chromium.org>

sandbox: Support executables for more phases

The SPL header has a function for obtaining the phase in capital letters,
e.g. 'SPL'. Add one for lower-case also, as used by sandbox.

Use this to generalise the sandbox logic for determining the filename of
the next sandbox executable. This can provide support for VPL.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4c30d18e 28-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: document parameters of os_realloc()

Avoid 'make htmldocs' build warnings:

./include/os.h:139: warning:
Function parameter or member 'ptr' not described in 'os_realloc'
./include/os.h:139: warning:
Function parameter or member 'length' not described in 'os_realloc'

Fixes: 14e46dfb176b ("sandbox: Add os_realloc()")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 01ad9f75 07-Mar-2021 Simon Glass <sjg@chromium.org>

sandbox: Update os_find_u_boot() to find the .img file

At present this function can only locate the u-boot ELF file. For SPL it
is handy to be able to locate u-boot.img since this is what would normally
be loaded by SPL.

Add another argument to allow this to be selected.

While we are here, update the function to load SPL when running in TPL,
since that is the next stage.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b308d9fd 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Avoid using malloc() for system state

This state is not accessible to the running U-Boot but at present it is
allocated in the emulated SDRAM. This doesn't seem very useful. Adjust
it to allocate from the OS instead.

The RAM buffer is currently not freed, but should be, so add that into
state_uninit(). Update the comment for os_free() to indicate that NULL is
a valid parameter value.

Note that the strdup() in spl_board_load_image() is changed as well, since
strdup() allocates memory in the RAM buffer.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 14e46dfb 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Add os_realloc()

We provide os_malloc() and os_free() but not os_realloc(). Add this,
following the usual semantics. Also update os_malloc() to behave correctly
when passed a zero size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c30a7093 31-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: mark os_abort() as noreturn

gcc -fanalyzer needs the information that a function does not return to
provide accurate information.

os_abort() does not return. Mark it accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 43db0750 30-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: keep time offset when resetting

The UEFI Self Certification Test (SCT) checks the SetTime() service with
the following steps:

* set date
* reset
* check date matches

To be compliant the sandbox should keep the offset to the host RTC during
resets. The implementation uses the environment variable
UBOOT_SB_TIME_OFFSET to persist the offset.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# b46f30a3 11-Nov-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add handler for exceptions

Add a handler for SIGILL, SIGBUS, SIGSEGV.

When an exception occurs print the program counter and the loaded
UEFI binaries and reset the system if CONFIG_SANDBOX_CRASH_RESET=y
or exit to the OS otherwise.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 063790cb 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: update function descriptions in os.h

Use Sphinx style function descriptions.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 329dccc0 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: implement reset

Up to now the sandbox would shutdown upon a cold reset request. Instead it
should be reset.

In our coding we use static variables like LIST_HEAD(efi_obj_list). A reset
can occur at any time, e.g. via an UEFI binary calling the reset service.
The only safe way to return to an initial state is to relaunch the U-Boot
binary.

The reset implementation uses execv() to relaunch U-Boot.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 89cdb0b5 03-Feb-2020 Simon Glass <sjg@chromium.org>

sandbox: Drop os_realloc()

Due to recent changes this function is no-longer used. Drop it.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@csgraf.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 10107efe 27-Sep-2022 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

sandbox: add SIGALRM-based watchdog device

In order to test that U-Boot actually maintains the watchdog device(s)
during long-running busy-loops, such as those where we wait for the
user to stop autoboot, we need a watchdog device that actually does
something during those loops; we cannot test that behaviour via the DM
test framework.

So introduce a relatively simple watchdog device which is simply based
on calling the host OS' alarm() function; that has the nice property
that a new call to alarm() simply sets a new deadline, and alarm(0)
cancels any existing alarm. These properties are precisely what we
need to implement start/reset/stop. We install our own handler so that
we get a known message printed if and when the watchdog fires, and by
just invoking that handler directly, we get expire_now for free.

The actual calls to the various OS functions (alarm, signal, raise)
need to be done in os.c, and since the driver code cannot get access
to the values of SIGALRM or SIG_DFL (that would require including a
host header, and that's only os.c which can do that), we cannot simply
do trivial wrappers for signal() and raise(), but instead create
specialized functions just for use by this driver.

Apart from enabling this driver for sandbox{,64}_defconfig, also
enable the wdt command which was useful for hand-testing this new
driver (especially with running u-boot under strace).

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>

# 7df5b353 05-Sep-2022 Pali Rohár <pali@kernel.org>

sandbox: Add function os_flush()

It flushes stdout.

Signed-off-by: Pali Rohár <pali@kernel.org>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 7750ee45 04-Apr-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add function os_printf()

Before setting up the devices U-Boot's printf() function cannot be used
for console output. Provide function os_printf() to print to stderr.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 880dbc5f 10-Jan-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: compatibility of os_get_filesize()

U-Boot define loff_t as long long. But the header
/usr/include/linux/types.h may not define it.
This has lead to a build error on Alpine Linux.

So let's use long long instead of loff_t for
the size parameter of function os_get_filesize().

Reported-by: Milan P. Stanić <mps@arvanta.net>
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Milan P. Stanić <mps@arvanta.net>
Reviewed-by: Simon Glass <sjg@chromium.org>

# a0ff280a 23-Oct-2021 Simon Glass <sjg@chromium.org>

sandbox: Support unmapping a file

Add the opposite of mapping, so that we can unmap and avoid running out of
address space.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b9274095 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to map a file into memory

It is useful to map a file into memory so that it can be accessed using
simple pointers. Add a function to support this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b4467fae 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to find the size of a file

Add a function to return the size of a file. This is useful in situations
where we need to allocate memory for it before reading it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Behún <marek.behun@nic.cz>

# f178bebf 05-Jul-2021 Simon Glass <sjg@chromium.org>

sandbox: Support executables for more phases

The SPL header has a function for obtaining the phase in capital letters,
e.g. 'SPL'. Add one for lower-case also, as used by sandbox.

Use this to generalise the sandbox logic for determining the filename of
the next sandbox executable. This can provide support for VPL.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4c30d18e 28-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: document parameters of os_realloc()

Avoid 'make htmldocs' build warnings:

./include/os.h:139: warning:
Function parameter or member 'ptr' not described in 'os_realloc'
./include/os.h:139: warning:
Function parameter or member 'length' not described in 'os_realloc'

Fixes: 14e46dfb176b ("sandbox: Add os_realloc()")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 01ad9f75 07-Mar-2021 Simon Glass <sjg@chromium.org>

sandbox: Update os_find_u_boot() to find the .img file

At present this function can only locate the u-boot ELF file. For SPL it
is handy to be able to locate u-boot.img since this is what would normally
be loaded by SPL.

Add another argument to allow this to be selected.

While we are here, update the function to load SPL when running in TPL,
since that is the next stage.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b308d9fd 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Avoid using malloc() for system state

This state is not accessible to the running U-Boot but at present it is
allocated in the emulated SDRAM. This doesn't seem very useful. Adjust
it to allocate from the OS instead.

The RAM buffer is currently not freed, but should be, so add that into
state_uninit(). Update the comment for os_free() to indicate that NULL is
a valid parameter value.

Note that the strdup() in spl_board_load_image() is changed as well, since
strdup() allocates memory in the RAM buffer.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 14e46dfb 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Add os_realloc()

We provide os_malloc() and os_free() but not os_realloc(). Add this,
following the usual semantics. Also update os_malloc() to behave correctly
when passed a zero size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c30a7093 31-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: mark os_abort() as noreturn

gcc -fanalyzer needs the information that a function does not return to
provide accurate information.

os_abort() does not return. Mark it accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 43db0750 30-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: keep time offset when resetting

The UEFI Self Certification Test (SCT) checks the SetTime() service with
the following steps:

* set date
* reset
* check date matches

To be compliant the sandbox should keep the offset to the host RTC during
resets. The implementation uses the environment variable
UBOOT_SB_TIME_OFFSET to persist the offset.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# b46f30a3 11-Nov-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add handler for exceptions

Add a handler for SIGILL, SIGBUS, SIGSEGV.

When an exception occurs print the program counter and the loaded
UEFI binaries and reset the system if CONFIG_SANDBOX_CRASH_RESET=y
or exit to the OS otherwise.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 063790cb 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: update function descriptions in os.h

Use Sphinx style function descriptions.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 329dccc0 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: implement reset

Up to now the sandbox would shutdown upon a cold reset request. Instead it
should be reset.

In our coding we use static variables like LIST_HEAD(efi_obj_list). A reset
can occur at any time, e.g. via an UEFI binary calling the reset service.
The only safe way to return to an initial state is to relaunch the U-Boot
binary.

The reset implementation uses execv() to relaunch U-Boot.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 89cdb0b5 03-Feb-2020 Simon Glass <sjg@chromium.org>

sandbox: Drop os_realloc()

Due to recent changes this function is no-longer used. Drop it.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@csgraf.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 7df5b353 05-Sep-2022 Pali Rohár <pali@kernel.org>

sandbox: Add function os_flush()

It flushes stdout.

Signed-off-by: Pali Rohár <pali@kernel.org>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 7750ee45 04-Apr-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add function os_printf()

Before setting up the devices U-Boot's printf() function cannot be used
for console output. Provide function os_printf() to print to stderr.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 880dbc5f 10-Jan-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: compatibility of os_get_filesize()

U-Boot define loff_t as long long. But the header
/usr/include/linux/types.h may not define it.
This has lead to a build error on Alpine Linux.

So let's use long long instead of loff_t for
the size parameter of function os_get_filesize().

Reported-by: Milan P. Stanić <mps@arvanta.net>
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Milan P. Stanić <mps@arvanta.net>
Reviewed-by: Simon Glass <sjg@chromium.org>

# a0ff280a 23-Oct-2021 Simon Glass <sjg@chromium.org>

sandbox: Support unmapping a file

Add the opposite of mapping, so that we can unmap and avoid running out of
address space.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b9274095 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to map a file into memory

It is useful to map a file into memory so that it can be accessed using
simple pointers. Add a function to support this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b4467fae 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to find the size of a file

Add a function to return the size of a file. This is useful in situations
where we need to allocate memory for it before reading it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Behún <marek.behun@nic.cz>

# f178bebf 05-Jul-2021 Simon Glass <sjg@chromium.org>

sandbox: Support executables for more phases

The SPL header has a function for obtaining the phase in capital letters,
e.g. 'SPL'. Add one for lower-case also, as used by sandbox.

Use this to generalise the sandbox logic for determining the filename of
the next sandbox executable. This can provide support for VPL.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4c30d18e 28-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: document parameters of os_realloc()

Avoid 'make htmldocs' build warnings:

./include/os.h:139: warning:
Function parameter or member 'ptr' not described in 'os_realloc'
./include/os.h:139: warning:
Function parameter or member 'length' not described in 'os_realloc'

Fixes: 14e46dfb176b ("sandbox: Add os_realloc()")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 01ad9f75 07-Mar-2021 Simon Glass <sjg@chromium.org>

sandbox: Update os_find_u_boot() to find the .img file

At present this function can only locate the u-boot ELF file. For SPL it
is handy to be able to locate u-boot.img since this is what would normally
be loaded by SPL.

Add another argument to allow this to be selected.

While we are here, update the function to load SPL when running in TPL,
since that is the next stage.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b308d9fd 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Avoid using malloc() for system state

This state is not accessible to the running U-Boot but at present it is
allocated in the emulated SDRAM. This doesn't seem very useful. Adjust
it to allocate from the OS instead.

The RAM buffer is currently not freed, but should be, so add that into
state_uninit(). Update the comment for os_free() to indicate that NULL is
a valid parameter value.

Note that the strdup() in spl_board_load_image() is changed as well, since
strdup() allocates memory in the RAM buffer.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 14e46dfb 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Add os_realloc()

We provide os_malloc() and os_free() but not os_realloc(). Add this,
following the usual semantics. Also update os_malloc() to behave correctly
when passed a zero size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c30a7093 31-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: mark os_abort() as noreturn

gcc -fanalyzer needs the information that a function does not return to
provide accurate information.

os_abort() does not return. Mark it accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 43db0750 30-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: keep time offset when resetting

The UEFI Self Certification Test (SCT) checks the SetTime() service with
the following steps:

* set date
* reset
* check date matches

To be compliant the sandbox should keep the offset to the host RTC during
resets. The implementation uses the environment variable
UBOOT_SB_TIME_OFFSET to persist the offset.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# b46f30a3 11-Nov-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add handler for exceptions

Add a handler for SIGILL, SIGBUS, SIGSEGV.

When an exception occurs print the program counter and the loaded
UEFI binaries and reset the system if CONFIG_SANDBOX_CRASH_RESET=y
or exit to the OS otherwise.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 063790cb 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: update function descriptions in os.h

Use Sphinx style function descriptions.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 329dccc0 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: implement reset

Up to now the sandbox would shutdown upon a cold reset request. Instead it
should be reset.

In our coding we use static variables like LIST_HEAD(efi_obj_list). A reset
can occur at any time, e.g. via an UEFI binary calling the reset service.
The only safe way to return to an initial state is to relaunch the U-Boot
binary.

The reset implementation uses execv() to relaunch U-Boot.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 89cdb0b5 03-Feb-2020 Simon Glass <sjg@chromium.org>

sandbox: Drop os_realloc()

Due to recent changes this function is no-longer used. Drop it.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@csgraf.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 7750ee45 04-Apr-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add function os_printf()

Before setting up the devices U-Boot's printf() function cannot be used
for console output. Provide function os_printf() to print to stderr.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>

# 880dbc5f 10-Jan-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: compatibility of os_get_filesize()

U-Boot define loff_t as long long. But the header
/usr/include/linux/types.h may not define it.
This has lead to a build error on Alpine Linux.

So let's use long long instead of loff_t for
the size parameter of function os_get_filesize().

Reported-by: Milan P. Stanić <mps@arvanta.net>
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Milan P. Stanić <mps@arvanta.net>
Reviewed-by: Simon Glass <sjg@chromium.org>

# a0ff280a 23-Oct-2021 Simon Glass <sjg@chromium.org>

sandbox: Support unmapping a file

Add the opposite of mapping, so that we can unmap and avoid running out of
address space.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b9274095 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to map a file into memory

It is useful to map a file into memory so that it can be accessed using
simple pointers. Add a function to support this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b4467fae 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to find the size of a file

Add a function to return the size of a file. This is useful in situations
where we need to allocate memory for it before reading it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Behún <marek.behun@nic.cz>

# f178bebf 05-Jul-2021 Simon Glass <sjg@chromium.org>

sandbox: Support executables for more phases

The SPL header has a function for obtaining the phase in capital letters,
e.g. 'SPL'. Add one for lower-case also, as used by sandbox.

Use this to generalise the sandbox logic for determining the filename of
the next sandbox executable. This can provide support for VPL.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4c30d18e 28-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: document parameters of os_realloc()

Avoid 'make htmldocs' build warnings:

./include/os.h:139: warning:
Function parameter or member 'ptr' not described in 'os_realloc'
./include/os.h:139: warning:
Function parameter or member 'length' not described in 'os_realloc'

Fixes: 14e46dfb176b ("sandbox: Add os_realloc()")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 01ad9f75 07-Mar-2021 Simon Glass <sjg@chromium.org>

sandbox: Update os_find_u_boot() to find the .img file

At present this function can only locate the u-boot ELF file. For SPL it
is handy to be able to locate u-boot.img since this is what would normally
be loaded by SPL.

Add another argument to allow this to be selected.

While we are here, update the function to load SPL when running in TPL,
since that is the next stage.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b308d9fd 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Avoid using malloc() for system state

This state is not accessible to the running U-Boot but at present it is
allocated in the emulated SDRAM. This doesn't seem very useful. Adjust
it to allocate from the OS instead.

The RAM buffer is currently not freed, but should be, so add that into
state_uninit(). Update the comment for os_free() to indicate that NULL is
a valid parameter value.

Note that the strdup() in spl_board_load_image() is changed as well, since
strdup() allocates memory in the RAM buffer.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 14e46dfb 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Add os_realloc()

We provide os_malloc() and os_free() but not os_realloc(). Add this,
following the usual semantics. Also update os_malloc() to behave correctly
when passed a zero size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c30a7093 31-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: mark os_abort() as noreturn

gcc -fanalyzer needs the information that a function does not return to
provide accurate information.

os_abort() does not return. Mark it accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 43db0750 30-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: keep time offset when resetting

The UEFI Self Certification Test (SCT) checks the SetTime() service with
the following steps:

* set date
* reset
* check date matches

To be compliant the sandbox should keep the offset to the host RTC during
resets. The implementation uses the environment variable
UBOOT_SB_TIME_OFFSET to persist the offset.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# b46f30a3 11-Nov-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add handler for exceptions

Add a handler for SIGILL, SIGBUS, SIGSEGV.

When an exception occurs print the program counter and the loaded
UEFI binaries and reset the system if CONFIG_SANDBOX_CRASH_RESET=y
or exit to the OS otherwise.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 063790cb 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: update function descriptions in os.h

Use Sphinx style function descriptions.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 329dccc0 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: implement reset

Up to now the sandbox would shutdown upon a cold reset request. Instead it
should be reset.

In our coding we use static variables like LIST_HEAD(efi_obj_list). A reset
can occur at any time, e.g. via an UEFI binary calling the reset service.
The only safe way to return to an initial state is to relaunch the U-Boot
binary.

The reset implementation uses execv() to relaunch U-Boot.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 89cdb0b5 03-Feb-2020 Simon Glass <sjg@chromium.org>

sandbox: Drop os_realloc()

Due to recent changes this function is no-longer used. Drop it.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@csgraf.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 880dbc5f 10-Jan-2022 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: compatibility of os_get_filesize()

U-Boot define loff_t as long long. But the header
/usr/include/linux/types.h may not define it.
This has lead to a build error on Alpine Linux.

So let's use long long instead of loff_t for
the size parameter of function os_get_filesize().

Reported-by: Milan P. Stanić <mps@arvanta.net>
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Tested-by: Milan P. Stanić <mps@arvanta.net>
Reviewed-by: Simon Glass <sjg@chromium.org>

# a0ff280a 23-Oct-2021 Simon Glass <sjg@chromium.org>

sandbox: Support unmapping a file

Add the opposite of mapping, so that we can unmap and avoid running out of
address space.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b9274095 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to map a file into memory

It is useful to map a file into memory so that it can be accessed using
simple pointers. Add a function to support this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b4467fae 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to find the size of a file

Add a function to return the size of a file. This is useful in situations
where we need to allocate memory for it before reading it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Behún <marek.behun@nic.cz>

# f178bebf 05-Jul-2021 Simon Glass <sjg@chromium.org>

sandbox: Support executables for more phases

The SPL header has a function for obtaining the phase in capital letters,
e.g. 'SPL'. Add one for lower-case also, as used by sandbox.

Use this to generalise the sandbox logic for determining the filename of
the next sandbox executable. This can provide support for VPL.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4c30d18e 28-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: document parameters of os_realloc()

Avoid 'make htmldocs' build warnings:

./include/os.h:139: warning:
Function parameter or member 'ptr' not described in 'os_realloc'
./include/os.h:139: warning:
Function parameter or member 'length' not described in 'os_realloc'

Fixes: 14e46dfb176b ("sandbox: Add os_realloc()")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 01ad9f75 07-Mar-2021 Simon Glass <sjg@chromium.org>

sandbox: Update os_find_u_boot() to find the .img file

At present this function can only locate the u-boot ELF file. For SPL it
is handy to be able to locate u-boot.img since this is what would normally
be loaded by SPL.

Add another argument to allow this to be selected.

While we are here, update the function to load SPL when running in TPL,
since that is the next stage.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b308d9fd 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Avoid using malloc() for system state

This state is not accessible to the running U-Boot but at present it is
allocated in the emulated SDRAM. This doesn't seem very useful. Adjust
it to allocate from the OS instead.

The RAM buffer is currently not freed, but should be, so add that into
state_uninit(). Update the comment for os_free() to indicate that NULL is
a valid parameter value.

Note that the strdup() in spl_board_load_image() is changed as well, since
strdup() allocates memory in the RAM buffer.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 14e46dfb 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Add os_realloc()

We provide os_malloc() and os_free() but not os_realloc(). Add this,
following the usual semantics. Also update os_malloc() to behave correctly
when passed a zero size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c30a7093 31-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: mark os_abort() as noreturn

gcc -fanalyzer needs the information that a function does not return to
provide accurate information.

os_abort() does not return. Mark it accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 43db0750 30-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: keep time offset when resetting

The UEFI Self Certification Test (SCT) checks the SetTime() service with
the following steps:

* set date
* reset
* check date matches

To be compliant the sandbox should keep the offset to the host RTC during
resets. The implementation uses the environment variable
UBOOT_SB_TIME_OFFSET to persist the offset.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# b46f30a3 11-Nov-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add handler for exceptions

Add a handler for SIGILL, SIGBUS, SIGSEGV.

When an exception occurs print the program counter and the loaded
UEFI binaries and reset the system if CONFIG_SANDBOX_CRASH_RESET=y
or exit to the OS otherwise.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 063790cb 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: update function descriptions in os.h

Use Sphinx style function descriptions.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 329dccc0 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: implement reset

Up to now the sandbox would shutdown upon a cold reset request. Instead it
should be reset.

In our coding we use static variables like LIST_HEAD(efi_obj_list). A reset
can occur at any time, e.g. via an UEFI binary calling the reset service.
The only safe way to return to an initial state is to relaunch the U-Boot
binary.

The reset implementation uses execv() to relaunch U-Boot.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 89cdb0b5 03-Feb-2020 Simon Glass <sjg@chromium.org>

sandbox: Drop os_realloc()

Due to recent changes this function is no-longer used. Drop it.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@csgraf.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <yamada.m@jp.panasonic.com>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# a0ff280a 23-Oct-2021 Simon Glass <sjg@chromium.org>

sandbox: Support unmapping a file

Add the opposite of mapping, so that we can unmap and avoid running out of
address space.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b9274095 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to map a file into memory

It is useful to map a file into memory so that it can be accessed using
simple pointers. Add a function to support this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b4467fae 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to find the size of a file

Add a function to return the size of a file. This is useful in situations
where we need to allocate memory for it before reading it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Behún <marek.behun@nic.cz>

# f178bebf 05-Jul-2021 Simon Glass <sjg@chromium.org>

sandbox: Support executables for more phases

The SPL header has a function for obtaining the phase in capital letters,
e.g. 'SPL'. Add one for lower-case also, as used by sandbox.

Use this to generalise the sandbox logic for determining the filename of
the next sandbox executable. This can provide support for VPL.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4c30d18e 28-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: document parameters of os_realloc()

Avoid 'make htmldocs' build warnings:

./include/os.h:139: warning:
Function parameter or member 'ptr' not described in 'os_realloc'
./include/os.h:139: warning:
Function parameter or member 'length' not described in 'os_realloc'

Fixes: 14e46dfb176b ("sandbox: Add os_realloc()")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 01ad9f75 07-Mar-2021 Simon Glass <sjg@chromium.org>

sandbox: Update os_find_u_boot() to find the .img file

At present this function can only locate the u-boot ELF file. For SPL it
is handy to be able to locate u-boot.img since this is what would normally
be loaded by SPL.

Add another argument to allow this to be selected.

While we are here, update the function to load SPL when running in TPL,
since that is the next stage.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b308d9fd 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Avoid using malloc() for system state

This state is not accessible to the running U-Boot but at present it is
allocated in the emulated SDRAM. This doesn't seem very useful. Adjust
it to allocate from the OS instead.

The RAM buffer is currently not freed, but should be, so add that into
state_uninit(). Update the comment for os_free() to indicate that NULL is
a valid parameter value.

Note that the strdup() in spl_board_load_image() is changed as well, since
strdup() allocates memory in the RAM buffer.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 14e46dfb 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Add os_realloc()

We provide os_malloc() and os_free() but not os_realloc(). Add this,
following the usual semantics. Also update os_malloc() to behave correctly
when passed a zero size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c30a7093 31-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: mark os_abort() as noreturn

gcc -fanalyzer needs the information that a function does not return to
provide accurate information.

os_abort() does not return. Mark it accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 43db0750 30-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: keep time offset when resetting

The UEFI Self Certification Test (SCT) checks the SetTime() service with
the following steps:

* set date
* reset
* check date matches

To be compliant the sandbox should keep the offset to the host RTC during
resets. The implementation uses the environment variable
UBOOT_SB_TIME_OFFSET to persist the offset.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# b46f30a3 11-Nov-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add handler for exceptions

Add a handler for SIGILL, SIGBUS, SIGSEGV.

When an exception occurs print the program counter and the loaded
UEFI binaries and reset the system if CONFIG_SANDBOX_CRASH_RESET=y
or exit to the OS otherwise.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 063790cb 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: update function descriptions in os.h

Use Sphinx style function descriptions.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 329dccc0 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: implement reset

Up to now the sandbox would shutdown upon a cold reset request. Instead it
should be reset.

In our coding we use static variables like LIST_HEAD(efi_obj_list). A reset
can occur at any time, e.g. via an UEFI binary calling the reset service.
The only safe way to return to an initial state is to relaunch the U-Boot
binary.

The reset implementation uses execv() to relaunch U-Boot.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 89cdb0b5 03-Feb-2020 Simon Glass <sjg@chromium.org>

sandbox: Drop os_realloc()

Due to recent changes this function is no-longer used. Drop it.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@csgraf.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <yamada.m@jp.panasonic.com>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b9274095 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to map a file into memory

It is useful to map a file into memory so that it can be accessed using
simple pointers. Add a function to support this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b4467fae 18-Aug-2021 Simon Glass <sjg@chromium.org>

sandbox: Add a way to find the size of a file

Add a function to return the size of a file. This is useful in situations
where we need to allocate memory for it before reading it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Behún <marek.behun@nic.cz>

# f178bebf 05-Jul-2021 Simon Glass <sjg@chromium.org>

sandbox: Support executables for more phases

The SPL header has a function for obtaining the phase in capital letters,
e.g. 'SPL'. Add one for lower-case also, as used by sandbox.

Use this to generalise the sandbox logic for determining the filename of
the next sandbox executable. This can provide support for VPL.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4c30d18e 28-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: document parameters of os_realloc()

Avoid 'make htmldocs' build warnings:

./include/os.h:139: warning:
Function parameter or member 'ptr' not described in 'os_realloc'
./include/os.h:139: warning:
Function parameter or member 'length' not described in 'os_realloc'

Fixes: 14e46dfb176b ("sandbox: Add os_realloc()")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 01ad9f75 07-Mar-2021 Simon Glass <sjg@chromium.org>

sandbox: Update os_find_u_boot() to find the .img file

At present this function can only locate the u-boot ELF file. For SPL it
is handy to be able to locate u-boot.img since this is what would normally
be loaded by SPL.

Add another argument to allow this to be selected.

While we are here, update the function to load SPL when running in TPL,
since that is the next stage.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b308d9fd 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Avoid using malloc() for system state

This state is not accessible to the running U-Boot but at present it is
allocated in the emulated SDRAM. This doesn't seem very useful. Adjust
it to allocate from the OS instead.

The RAM buffer is currently not freed, but should be, so add that into
state_uninit(). Update the comment for os_free() to indicate that NULL is
a valid parameter value.

Note that the strdup() in spl_board_load_image() is changed as well, since
strdup() allocates memory in the RAM buffer.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 14e46dfb 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Add os_realloc()

We provide os_malloc() and os_free() but not os_realloc(). Add this,
following the usual semantics. Also update os_malloc() to behave correctly
when passed a zero size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c30a7093 31-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: mark os_abort() as noreturn

gcc -fanalyzer needs the information that a function does not return to
provide accurate information.

os_abort() does not return. Mark it accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 43db0750 30-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: keep time offset when resetting

The UEFI Self Certification Test (SCT) checks the SetTime() service with
the following steps:

* set date
* reset
* check date matches

To be compliant the sandbox should keep the offset to the host RTC during
resets. The implementation uses the environment variable
UBOOT_SB_TIME_OFFSET to persist the offset.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# b46f30a3 11-Nov-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add handler for exceptions

Add a handler for SIGILL, SIGBUS, SIGSEGV.

When an exception occurs print the program counter and the loaded
UEFI binaries and reset the system if CONFIG_SANDBOX_CRASH_RESET=y
or exit to the OS otherwise.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 063790cb 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: update function descriptions in os.h

Use Sphinx style function descriptions.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 329dccc0 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: implement reset

Up to now the sandbox would shutdown upon a cold reset request. Instead it
should be reset.

In our coding we use static variables like LIST_HEAD(efi_obj_list). A reset
can occur at any time, e.g. via an UEFI binary calling the reset service.
The only safe way to return to an initial state is to relaunch the U-Boot
binary.

The reset implementation uses execv() to relaunch U-Boot.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 89cdb0b5 03-Feb-2020 Simon Glass <sjg@chromium.org>

sandbox: Drop os_realloc()

Due to recent changes this function is no-longer used. Drop it.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@csgraf.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <yamada.m@jp.panasonic.com>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# f178bebf 05-Jul-2021 Simon Glass <sjg@chromium.org>

sandbox: Support executables for more phases

The SPL header has a function for obtaining the phase in capital letters,
e.g. 'SPL'. Add one for lower-case also, as used by sandbox.

Use this to generalise the sandbox logic for determining the filename of
the next sandbox executable. This can provide support for VPL.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4c30d18e 28-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: document parameters of os_realloc()

Avoid 'make htmldocs' build warnings:

./include/os.h:139: warning:
Function parameter or member 'ptr' not described in 'os_realloc'
./include/os.h:139: warning:
Function parameter or member 'length' not described in 'os_realloc'

Fixes: 14e46dfb176b ("sandbox: Add os_realloc()")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 01ad9f75 07-Mar-2021 Simon Glass <sjg@chromium.org>

sandbox: Update os_find_u_boot() to find the .img file

At present this function can only locate the u-boot ELF file. For SPL it
is handy to be able to locate u-boot.img since this is what would normally
be loaded by SPL.

Add another argument to allow this to be selected.

While we are here, update the function to load SPL when running in TPL,
since that is the next stage.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b308d9fd 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Avoid using malloc() for system state

This state is not accessible to the running U-Boot but at present it is
allocated in the emulated SDRAM. This doesn't seem very useful. Adjust
it to allocate from the OS instead.

The RAM buffer is currently not freed, but should be, so add that into
state_uninit(). Update the comment for os_free() to indicate that NULL is
a valid parameter value.

Note that the strdup() in spl_board_load_image() is changed as well, since
strdup() allocates memory in the RAM buffer.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 14e46dfb 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Add os_realloc()

We provide os_malloc() and os_free() but not os_realloc(). Add this,
following the usual semantics. Also update os_malloc() to behave correctly
when passed a zero size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c30a7093 31-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: mark os_abort() as noreturn

gcc -fanalyzer needs the information that a function does not return to
provide accurate information.

os_abort() does not return. Mark it accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 43db0750 30-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: keep time offset when resetting

The UEFI Self Certification Test (SCT) checks the SetTime() service with
the following steps:

* set date
* reset
* check date matches

To be compliant the sandbox should keep the offset to the host RTC during
resets. The implementation uses the environment variable
UBOOT_SB_TIME_OFFSET to persist the offset.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# b46f30a3 11-Nov-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add handler for exceptions

Add a handler for SIGILL, SIGBUS, SIGSEGV.

When an exception occurs print the program counter and the loaded
UEFI binaries and reset the system if CONFIG_SANDBOX_CRASH_RESET=y
or exit to the OS otherwise.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 063790cb 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: update function descriptions in os.h

Use Sphinx style function descriptions.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 329dccc0 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: implement reset

Up to now the sandbox would shutdown upon a cold reset request. Instead it
should be reset.

In our coding we use static variables like LIST_HEAD(efi_obj_list). A reset
can occur at any time, e.g. via an UEFI binary calling the reset service.
The only safe way to return to an initial state is to relaunch the U-Boot
binary.

The reset implementation uses execv() to relaunch U-Boot.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 89cdb0b5 03-Feb-2020 Simon Glass <sjg@chromium.org>

sandbox: Drop os_realloc()

Due to recent changes this function is no-longer used. Drop it.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@csgraf.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <yamada.m@jp.panasonic.com>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4c30d18e 28-Mar-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: document parameters of os_realloc()

Avoid 'make htmldocs' build warnings:

./include/os.h:139: warning:
Function parameter or member 'ptr' not described in 'os_realloc'
./include/os.h:139: warning:
Function parameter or member 'length' not described in 'os_realloc'

Fixes: 14e46dfb176b ("sandbox: Add os_realloc()")
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 01ad9f75 07-Mar-2021 Simon Glass <sjg@chromium.org>

sandbox: Update os_find_u_boot() to find the .img file

At present this function can only locate the u-boot ELF file. For SPL it
is handy to be able to locate u-boot.img since this is what would normally
be loaded by SPL.

Add another argument to allow this to be selected.

While we are here, update the function to load SPL when running in TPL,
since that is the next stage.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b308d9fd 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Avoid using malloc() for system state

This state is not accessible to the running U-Boot but at present it is
allocated in the emulated SDRAM. This doesn't seem very useful. Adjust
it to allocate from the OS instead.

The RAM buffer is currently not freed, but should be, so add that into
state_uninit(). Update the comment for os_free() to indicate that NULL is
a valid parameter value.

Note that the strdup() in spl_board_load_image() is changed as well, since
strdup() allocates memory in the RAM buffer.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 14e46dfb 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Add os_realloc()

We provide os_malloc() and os_free() but not os_realloc(). Add this,
following the usual semantics. Also update os_malloc() to behave correctly
when passed a zero size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c30a7093 31-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: mark os_abort() as noreturn

gcc -fanalyzer needs the information that a function does not return to
provide accurate information.

os_abort() does not return. Mark it accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 43db0750 30-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: keep time offset when resetting

The UEFI Self Certification Test (SCT) checks the SetTime() service with
the following steps:

* set date
* reset
* check date matches

To be compliant the sandbox should keep the offset to the host RTC during
resets. The implementation uses the environment variable
UBOOT_SB_TIME_OFFSET to persist the offset.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# b46f30a3 11-Nov-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add handler for exceptions

Add a handler for SIGILL, SIGBUS, SIGSEGV.

When an exception occurs print the program counter and the loaded
UEFI binaries and reset the system if CONFIG_SANDBOX_CRASH_RESET=y
or exit to the OS otherwise.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 063790cb 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: update function descriptions in os.h

Use Sphinx style function descriptions.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 329dccc0 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: implement reset

Up to now the sandbox would shutdown upon a cold reset request. Instead it
should be reset.

In our coding we use static variables like LIST_HEAD(efi_obj_list). A reset
can occur at any time, e.g. via an UEFI binary calling the reset service.
The only safe way to return to an initial state is to relaunch the U-Boot
binary.

The reset implementation uses execv() to relaunch U-Boot.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 89cdb0b5 03-Feb-2020 Simon Glass <sjg@chromium.org>

sandbox: Drop os_realloc()

Due to recent changes this function is no-longer used. Drop it.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@csgraf.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <yamada.m@jp.panasonic.com>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 01ad9f75 07-Mar-2021 Simon Glass <sjg@chromium.org>

sandbox: Update os_find_u_boot() to find the .img file

At present this function can only locate the u-boot ELF file. For SPL it
is handy to be able to locate u-boot.img since this is what would normally
be loaded by SPL.

Add another argument to allow this to be selected.

While we are here, update the function to load SPL when running in TPL,
since that is the next stage.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b308d9fd 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Avoid using malloc() for system state

This state is not accessible to the running U-Boot but at present it is
allocated in the emulated SDRAM. This doesn't seem very useful. Adjust
it to allocate from the OS instead.

The RAM buffer is currently not freed, but should be, so add that into
state_uninit(). Update the comment for os_free() to indicate that NULL is
a valid parameter value.

Note that the strdup() in spl_board_load_image() is changed as well, since
strdup() allocates memory in the RAM buffer.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 14e46dfb 06-Feb-2021 Simon Glass <sjg@chromium.org>

sandbox: Add os_realloc()

We provide os_malloc() and os_free() but not os_realloc(). Add this,
following the usual semantics. Also update os_malloc() to behave correctly
when passed a zero size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# c30a7093 31-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: mark os_abort() as noreturn

gcc -fanalyzer needs the information that a function does not return to
provide accurate information.

os_abort() does not return. Mark it accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 43db0750 30-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: keep time offset when resetting

The UEFI Self Certification Test (SCT) checks the SetTime() service with
the following steps:

* set date
* reset
* check date matches

To be compliant the sandbox should keep the offset to the host RTC during
resets. The implementation uses the environment variable
UBOOT_SB_TIME_OFFSET to persist the offset.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# b46f30a3 11-Nov-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add handler for exceptions

Add a handler for SIGILL, SIGBUS, SIGSEGV.

When an exception occurs print the program counter and the loaded
UEFI binaries and reset the system if CONFIG_SANDBOX_CRASH_RESET=y
or exit to the OS otherwise.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 063790cb 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: update function descriptions in os.h

Use Sphinx style function descriptions.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 329dccc0 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: implement reset

Up to now the sandbox would shutdown upon a cold reset request. Instead it
should be reset.

In our coding we use static variables like LIST_HEAD(efi_obj_list). A reset
can occur at any time, e.g. via an UEFI binary calling the reset service.
The only safe way to return to an initial state is to relaunch the U-Boot
binary.

The reset implementation uses execv() to relaunch U-Boot.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 89cdb0b5 03-Feb-2020 Simon Glass <sjg@chromium.org>

sandbox: Drop os_realloc()

Due to recent changes this function is no-longer used. Drop it.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@csgraf.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <yamada.m@jp.panasonic.com>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# c30a7093 31-Jan-2021 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: mark os_abort() as noreturn

gcc -fanalyzer needs the information that a function does not return to
provide accurate information.

os_abort() does not return. Mark it accordingly.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 43db0750 30-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: keep time offset when resetting

The UEFI Self Certification Test (SCT) checks the SetTime() service with
the following steps:

* set date
* reset
* check date matches

To be compliant the sandbox should keep the offset to the host RTC during
resets. The implementation uses the environment variable
UBOOT_SB_TIME_OFFSET to persist the offset.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# b46f30a3 11-Nov-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add handler for exceptions

Add a handler for SIGILL, SIGBUS, SIGSEGV.

When an exception occurs print the program counter and the loaded
UEFI binaries and reset the system if CONFIG_SANDBOX_CRASH_RESET=y
or exit to the OS otherwise.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 063790cb 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: update function descriptions in os.h

Use Sphinx style function descriptions.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 329dccc0 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: implement reset

Up to now the sandbox would shutdown upon a cold reset request. Instead it
should be reset.

In our coding we use static variables like LIST_HEAD(efi_obj_list). A reset
can occur at any time, e.g. via an UEFI binary calling the reset service.
The only safe way to return to an initial state is to relaunch the U-Boot
binary.

The reset implementation uses execv() to relaunch U-Boot.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 89cdb0b5 03-Feb-2020 Simon Glass <sjg@chromium.org>

sandbox: Drop os_realloc()

Due to recent changes this function is no-longer used. Drop it.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@csgraf.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <yamada.m@jp.panasonic.com>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 43db0750 30-Dec-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: keep time offset when resetting

The UEFI Self Certification Test (SCT) checks the SetTime() service with
the following steps:

* set date
* reset
* check date matches

To be compliant the sandbox should keep the offset to the host RTC during
resets. The implementation uses the environment variable
UBOOT_SB_TIME_OFFSET to persist the offset.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# b46f30a3 11-Nov-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add handler for exceptions

Add a handler for SIGILL, SIGBUS, SIGSEGV.

When an exception occurs print the program counter and the loaded
UEFI binaries and reset the system if CONFIG_SANDBOX_CRASH_RESET=y
or exit to the OS otherwise.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 063790cb 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: update function descriptions in os.h

Use Sphinx style function descriptions.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 329dccc0 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: implement reset

Up to now the sandbox would shutdown upon a cold reset request. Instead it
should be reset.

In our coding we use static variables like LIST_HEAD(efi_obj_list). A reset
can occur at any time, e.g. via an UEFI binary calling the reset service.
The only safe way to return to an initial state is to relaunch the U-Boot
binary.

The reset implementation uses execv() to relaunch U-Boot.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 89cdb0b5 03-Feb-2020 Simon Glass <sjg@chromium.org>

sandbox: Drop os_realloc()

Due to recent changes this function is no-longer used. Drop it.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@csgraf.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <yamada.m@jp.panasonic.com>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# b46f30a3 11-Nov-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: add handler for exceptions

Add a handler for SIGILL, SIGBUS, SIGSEGV.

When an exception occurs print the program counter and the loaded
UEFI binaries and reset the system if CONFIG_SANDBOX_CRASH_RESET=y
or exit to the OS otherwise.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 063790cb 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: update function descriptions in os.h

Use Sphinx style function descriptions.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 329dccc0 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: implement reset

Up to now the sandbox would shutdown upon a cold reset request. Instead it
should be reset.

In our coding we use static variables like LIST_HEAD(efi_obj_list). A reset
can occur at any time, e.g. via an UEFI binary calling the reset service.
The only safe way to return to an initial state is to relaunch the U-Boot
binary.

The reset implementation uses execv() to relaunch U-Boot.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 89cdb0b5 03-Feb-2020 Simon Glass <sjg@chromium.org>

sandbox: Drop os_realloc()

Due to recent changes this function is no-longer used. Drop it.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@csgraf.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <yamada.m@jp.panasonic.com>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 063790cb 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: update function descriptions in os.h

Use Sphinx style function descriptions.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 329dccc0 27-Oct-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

sandbox: implement reset

Up to now the sandbox would shutdown upon a cold reset request. Instead it
should be reset.

In our coding we use static variables like LIST_HEAD(efi_obj_list). A reset
can occur at any time, e.g. via an UEFI binary calling the reset service.
The only safe way to return to an initial state is to relaunch the U-Boot
binary.

The reset implementation uses execv() to relaunch U-Boot.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 89cdb0b5 03-Feb-2020 Simon Glass <sjg@chromium.org>

sandbox: Drop os_realloc()

Due to recent changes this function is no-longer used. Drop it.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@csgraf.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <yamada.m@jp.panasonic.com>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 89cdb0b5 03-Feb-2020 Simon Glass <sjg@chromium.org>

sandbox: Drop os_realloc()

Due to recent changes this function is no-longer used. Drop it.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@csgraf.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <yamada.m@jp.panasonic.com>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 001d1885 08-Apr-2019 Simon Glass <sjg@chromium.org>

sandbox: Improve debugging in initcall_run_list()

At present if one of the initcalls fails on sandbox the address printing
is not help, e.g.:

initcall sequence 0000557678967c80 failed at call 00005576709dfe1f (err=-96)

This is because U-Boot gets relocated high into memory and the relocation
offset (gd->reloc_off) does not work correctly for sandbox.

Add support for finding the base address of the text region (at least on
Linux) and use that to set the relocation offset. This makes the output
better:

initcall sequence 0000560775957c80 failed at call 0000000000048134 (err=-96)

Then you use can use grep to see which init call failed, e.g.:

$ grep 0000000000048134 u-boot.map
stdio_add_devices

Of course another option is to run it with a debugger such as gdb:

$ gdb u-boot
...
(gdb) br initcall.h:41
Breakpoint 1 at 0x4db9d: initcall.h:41. (2 locations)

Note that two locations are reported, since this function is used in both
board_init_f() and board_init_r().

(gdb) r
Starting program: /tmp/b/sandbox/u-boot
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

U-Boot 2018.09-00264-ge0c2ba9814-dirty (Sep 22 2018 - 12:21:46 -0600)

DRAM: 128 MiB
MMC:

Breakpoint 1, initcall_run_list (init_sequence=0x5555559619e0 <init_sequence_f>)
at /scratch/sglass/cosarm/src/third_party/u-boot/files/include/initcall.h:41
41 printf("initcall sequence %p failed at call %p (err=%d)\n",
(gdb) print *init_fnc_ptr
$1 = (const init_fnc_t) 0x55555559c114 <stdio_add_devices>
(gdb)

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@suse.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47b98ad0 16-Jul-2017 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>

# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>

# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>

# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>

# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>

# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>

# 347d06de 14-Jan-2014 Masahiro Yamada <yamada.m@jp.panasonic.com>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>

# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>

# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>

# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>

# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>

# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>

# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>

# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 566bf3a8 06-Nov-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a function to read a host file

Add a way to read a file from the host filesystem. This can be useful for
reading test data, for example. Also fix up the writing function which was
not the right version, and drop the debugging lines.

Signed-off-by: Simon Glass <sjg@chromium.org>


# 4af3e9ad 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Restore blocking I/O on exit

At present sandbox sets non-blocking I/O as soon as any input is read
from the terminal. However it does not restore the previous state on
exit. Fix this and drop the old os_read_no_block() function.

This means that we always enable blocking I/O in sandbox (if input is a
terminal) whereas previously it would only happen on the first call to
tstc() or getc(). However, the difference is likely not important.

Signed-off-by: Simon Glass <sjg@chromium.org>


# 056a5cea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a way to write data to the host filesystem

For debugging it is sometimes useful to write out data for inspection
using an external tool. Add a function which can write this data to a
given file.

Signed-off-by: Simon Glass <sjg@chromium.org>


# 50b288ac 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Support file truncation with os_open()

At present files are not truncated on writing. This is a useful feature.
Add support for this.

Signed-off-by: Simon Glass <sjg@chromium.org>


# 9f8037ea 01-Oct-2018 Simon Glass <sjg@chromium.org>

sandbox: Unprotect DATA regions in bus tests

On my Ubuntu 18.04.1 machine two driver-model bus tests have started
failing recently. The problem appears to be that the DATA region of the
executable is protected. This does not seem correct, but perhaps there
is a reason.

To work around it, unprotect the regions in these tests before accessing
them.

Signed-off-by: Simon Glass <sjg@chromium.org>


# 3fcb7147 22-Jun-2018 Alexander Graf <agraf@suse.de>

sandbox: Fix setjmp/longjmp

In sandbox, longjmp returns to itself in an endless loop because
os_longjmp() calls into longjmp() which is provided by U-Boot which
again calls os_longjmp().

Setjmp on the other hand must not return because otherwise the
return freees up stack elements that we need during longjmp().

The only straight forward fix that doesn't involve nasty hacks I
could find is to directly link against the system setjmp/longjmp
implementations. That means we just provide the compiler with
hints that the symbol will be available and actually fill them
out with versions from libc.

This approach should be reasonably platform agnostic

Signed-off-by: Alexander Graf <agraf@suse.de>
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>


# fe938fb0 15-Sep-2018 Simon Glass <sjg@chromium.org>

sandbox: Add support for calling abort()

This function is useful to signal that the application needs to exit
immediate. It can be caught with a debugger (e.g. gdb). Add a stub for it
so that it can be called from within sandbox when an internal error
occurs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 30eef21f 16-May-2018 Simon Glass <sjg@chromium.org>

sandbox: Add a setjmp() implementation

Add an implementation of setjmp() and longjmp() which rely on the
underlying host C library. Since we cannot know how large the jump buffer
needs to be, pick something that should be suitable and check it at
runtime. At present we need access to the underlying struct as well.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Alexander Graf <agraf@suse.de>


# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>


# 0b189b6c 04-Dec-2017 Simon Glass <sjg@chromium.org>

Revert "sandbox: remove os_putc() and os_puts()"

While sandbox works OK without the special-case code, it does result in
console output being stored in the pre-console buffer while sandbox starts
up. If there is a crash or a problem then there is no indication of what
is going on.

For ease of debugging it seems better to revert this change.

This reverts commit 47b98ad0f6779485d0f0c14f337c3eece273eb54.

Signed-off-by: Simon Glass <sjg@chromium.org>


# 47b98ad0 16-Jul-2017 Masahiro Yamada <yamada.masahiro@socionext.com>

sandbox: remove os_putc() and os_puts()

They are unused since commit d8c6fb8cedbc ("sandbox: Drop special
case console code for sandbox").

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
Acked-by: Simon Glass <sjg@chromium.org>


# 86167089 01-Oct-2016 Stefan Brüns <stefan.bruens@rwth-aachen.de>

sandbox/fs: Free memory allocated by os_dirent_ls

Signed-off-by: Stefan Brüns <stefan.bruens@rwth-aachen.de>
Acked-by: Simon Glass <sjg@chromium.org>


# d4e33f5a 04-Jul-2016 Simon Glass <sjg@chromium.org>

sandbox: Allow chaining from SPL to U-Boot proper

SPL is expected to load and run U-Boot. This needs to work with sandbox also.
Provide a function to locate the U-Boot image, and another to start it. This
allows SPL to function on sandbox as it does on other archs.

Signed-off-by: Simon Glass <sjg@chromium.org>


# 8939df09 10-May-2015 Simon Glass <sjg@chromium.org>

sandbox: Tidy up terminal restore

For some reason 'u-boot -D' does not restore the terminal correctly when
the 'reset' command is used. Call the terminal restore function explicitly
in this case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Joe Hershberger <joe.hershberger@ni.com>


# 94eefdee 20-Apr-2015 Simon Glass <sjg@chromium.org>

dm: sandbox: Add os_localtime() to obtain the system time

Add a function to read the system time into U-Boot.

Signed-off-by: Simon Glass <sjg@chromium.org>


# 887bd416 25-Mar-2015 Simon Glass <sjg@chromium.org>

sandbox: Fix comment for os_open()

This has the wrong #define in the function comment. Fix it.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Marek Vasut <marex@denx.de>


# 96b1046d 17-Nov-2014 Suriyan Ramasami <suriyan.r@gmail.com>

sandbox: Prepare API change for files greater than 2GB

Change the internal sandbox functions to use loff_t for file offsets.

Signed-off-by: Suriyan Ramasami <suriyan.r@gmail.com>

Acked-by: Simon Glass <sjg@chromium.org>


# ffb87905 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Allow Ctrl-C to work in sandbox

It is useful for Cltl-C to be handled by U-Boot as it is on other boards.
But it is also useful to be able to terminate U-Boot with Ctrl-C.

Add an option to enable signals while in raw mode, and make this the
default. Add an option to leave the terminal cooked, which is useful for
redirecting output.

Signed-off-by: Simon Glass <sjg@chromium.org>


# 47f5fcfb 27-Feb-2014 Simon Glass <sjg@chromium.org>

sandbox: Add os_jump_to_image() to run another executable

For some tests it is useful to be able to run U-Boot again but pass on the
same memory contents. Add a function to achieve this.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>


# cfd13e8d 01-Mar-2014 Stephen Warren <swarren@wwwdotorg.org>

unit-test: make "test -e" test independent of $CWD

The unit-test for hush's "test -e" currently relies upon being run in
the U-Boot build directory, because it tests for the existence of a file
that exists in that directory.

Fix this by explicitly creating the file we use for the existence test,
and deleting it afterwards so that multiple successive unit-test
invocations succeed. This required adding an os.c function to erase
files.

Reported-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Stephen Warren <swarren@wwwdotorg.org>


# 347d06de 14-Jan-2014 Masahiro Yamada <yamada.m@jp.panasonic.com>

sandbox: fix the return type of os_free() function

The function os_free() returns nothing.
Its return type should be "void" rather than "void *".

Signed-off-by: Masahiro Yamada <yamada.m@jp.panasonic.com>


# 5c2859cd 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow reading/writing of RAM buffer

It is useful to be able to save and restore the RAM contents of sandbox
U-Boot either for setting up tests, for later analysys, or for chaining
together multiple tests which need to keep the same memory contents.

Add a function to provide a memory file for U-Boot. This is read on
start-up and written when shutting down. If the file does not exist
on start-up, it will be created when shutting down.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>


# 91b136c7 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Allow the console to work earlier

With sandbox, errors and problems may be reported before console_init_f()
is executed. For example, an argument may not parse correctly or U-Boot may
panic(). At present this output is swallowed so there is no indication what
is going wrong.

Adjust the console to deal with a very early sandbox setup, by detecting that
there is no global_data yet, and calling os functions in that case.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>


# 77595c6d 10-Nov-2013 Simon Glass <sjg@chromium.org>

sandbox: Improve/augment memory allocation functions

Implement realloc() and free() for sandbox, by adding a header to each
block which contains the block size.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Che-Liang Chiou <clchiou@chromium.org>
Reviewed-by: Hung-ying Tyan <tyanh@chromium.org>


# 2a54d159 19-May-2013 Simon Glass <sjg@chromium.org>

sandbox: Use uint64_t instead of u64 for time

The uint64_t type is defined in linux/types.h, so is safer than u64, which
is not actually a Linux type.

Change-Id: Ifc9a369e6543250c49117b8d3cb3a676eee43e04
Signed-off-by: Simon Glass <sjg@chromium.org>


# 1a459660 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add GPL-2.0+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>
[trini: Fixup common/cmd_io.c]
Signed-off-by: Tom Rini <trini@ti.com>


# 62584db1 26-Dec-2012 Simon Glass <sjg@chromium.org>

sandbox: Add a way of obtaining directory listings

This implementation uses opendir()/readdir() to access the directory
information and then puts it in a linked list for the caller's use.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@ti.com>


# e101550a 24-Feb-2013 Taylor Hutt <thutt@chromium.org>

sandbox: Improve sandbox serial port keyboard interface

Implements the tstc() interface for the serial driver. Multiplexing
the console between the serial port and a keyboard uses a polling
method of checking if characters are available; this means that the
serial console must be non-blocking when attempting to read
characters.

Signed-off-by: Taylor Hutt <thutt@chromium.org>
Signed-off-by: Simon Glass <sjg@chromium.org>


# 9d72e67b 26-Feb-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: mark os_exit as noreturn

Signed-off-by: Mike Frysinger <vapier@gentoo.org>


# 70db4212 15-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add getopt support

This adds simple command-line parsing to sandbox. The idea is that it
sets up the state with options provided, and this state can then be
queried later, as needed.

New flags are declared with the SB_CMDLINE_OPT_SHORT helper macro,
pointers are automatically gathered up in a special section, and
then the core code takes care of gathering them up and processing
at runtime. This way there is no central place where we have to
store a list of flags with ifdefs.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>


# d9165153 20-Feb-2012 Simon Glass <sjg@chromium.org>

sandbox: add flags for open() call

This provides a way for callers to create files for writing. The flags
are translated at runtime, for the ones we support.

Signed-off-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>


# e2dcefcb 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: add lseek helper

Follow up patches want to be able to seek fd's.

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>


# 4f345d56 19-Jan-2012 Mike Frysinger <vapier@gentoo.org>

sandbox: add ifdef protection to os.h

Acked-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>


# d99a6874 28-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add timer simulation

Making sleep command work

Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>


# 21899b10 05-Nov-2011 Matthias Weisser <weisserm@arcor.de>

sandbox: Add improved RAM simulation

Using mmap to allocate memory from the OS for RAM simulation we can use
u-boot own malloc implementation.

Tested-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Matthias Weisser <weisserm@arcor.de>
Signed-off-by: Mike Frysinger <vapier@gentoo.org>


# ab06a758 25-Oct-2011 Mike Frysinger <vapier@gentoo.org>

sandbox: put stdin into raw mode

This allows us to act like a serial device: we get tab chars and CTRL+C
and respond appropriately.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Tested-by: Simon Glass <sjg@chromium.org>


# 7a9219c1 03-Oct-2011 Simon Glass <sjg@chromium.org>

sandbox: Add OS dependent layer

We want to keep all OS-dependent code in once place, with a simple interface
to U-Boot. For now, this is that place.

Signed-off-by: Simon Glass <sjg@chromium.org>