Files
u-boot/examples/ulib
Simon Glass 1db2a3f13b ulib: Add a way to obtain the version
It is possible to simply read the 'version_string' variable, but it
seems better to add a proper API call to get this. It is already
defined in lib/ulib/ulib.c

Add a prototype to u-boot-lib.h

Make use of it from the demo program.

Fix the comment for ulib_uninit() while we are here.

Signed-off-by: Simon Glass <sjg@chromium.org>
2025-09-11 15:19:22 -06:00
..
2025-09-10 03:37:08 -06:00
2025-09-10 03:37:08 -06:00
2025-09-10 03:37:08 -06:00
2025-09-11 15:19:22 -06:00
2025-09-10 03:37:08 -06:00
2025-09-10 03:37:08 -06:00

U-Boot Library Example
======================

This directory contains example programs showing how to use the U-Boot library
(libu-boot.so) in external C programs.

Building U-Boot Library
-----------------------

First, build U-Boot with library support:

    make O=/tmp/b/sandbox -j$(nproc) sandbox_defconfig all

This creates:
- /tmp/b/sandbox/libu-boot.so (shared library)
- /tmp/b/sandbox/libu-boot.a (static library)

Example Programs
----------------

The examples are built automatically as part of the U-Boot build. Each program
demonstrates different aspects of the U-Boot library.

**demo.c** - Demonstrates using U-Boot library functions

- Shows how to init the library with ulib_init()
- Uses U-Boot's OS functions (os_open(), os_fgets(), os_close())
- Reads and displays system information
- Shows the U-Boot version

**boot.c** - Demonstrates booting an OS using U-Boot bootflow

- Shows bootflow scanning and booting functionality
- Uses system headers for main program logic (boot.c)
- Uses U-Boot headers for bootflow internals (bootflow.c)
- Demonstrates attaching disk images and scanning for bootable OS
- Attempts to boot the first discovered bootflow

Building Examples
-----------------

The examples are built automatically when U-Boot has these options enabled::

    CONFIG_ULIB=y
    CONFIG_EXAMPLES=y

To build manually:

    # From this directory examples/ulib
    make UBOOT_BUILD=/tmp/b/sandbox/ srctree=../..

Running Examples
----------------

    # Run the demo
    LD_LIBRARY_PATH=/tmp/b/sandbox ./demo

    # Run the demo (static version)
    ./demo_static

    # Run the boot example (requires disk image at /home/sglass/u/mmc1.img)
    LD_LIBRARY_PATH=/tmp/b/sandbox ./boot

    # Run the boot example (static version)
    ./boot_static

Key Points
----------

- Files are compiled with U-Boot headers by default, except those listed in
  sys-objs which use system headers
- Use ulib_init() to init the library
- Use ulib_uninit() to clean up
- Set LD_LIBRARY_PATH when running dynamically linked programs
- Each program automatically gets both dynamic and static versions built

Copying for External Use
-------------------------

This directory can be copied elsewhere and used independently:

    cp -r examples/ulib ~/my-project/
    cd ~/my-project/ulib
    make UBOOT_BUILD=/path/to/u-boot-build  srctree=/path/to/u-boot-source

License
-------

All examples are licensed under GPL-2.0+