xref: /rk3399_ARM-atf/docs/components/romlib-design.rst (revision 29c02529592fb2489edee6c92e418918e5732105)
140d553cfSPaul BeesleyLibrary at ROM
240d553cfSPaul Beesley==============
340d553cfSPaul Beesley
440d553cfSPaul BeesleyThis document provides an overview of the "library at ROM" implementation in
540d553cfSPaul BeesleyTrusted Firmware-A (TF-A).
640d553cfSPaul Beesley
740d553cfSPaul BeesleyIntroduction
840d553cfSPaul Beesley~~~~~~~~~~~~
940d553cfSPaul Beesley
1040d553cfSPaul BeesleyThe "library at ROM" feature allows platforms to build a library of functions to
1140d553cfSPaul Beesleybe placed in ROM. This reduces SRAM usage by utilising the available space in
1240d553cfSPaul BeesleyROM. The "library at ROM" contains a jump table with the list of functions that
1340d553cfSPaul Beesleyare placed in ROM. The capabilities of the "library at ROM" are:
1440d553cfSPaul Beesley
1540d553cfSPaul Beesley1. Functions can be from one or several libraries.
1640d553cfSPaul Beesley
1740d553cfSPaul Beesley2. Functions can be patched after they have been programmed into ROM.
1840d553cfSPaul Beesley
1940d553cfSPaul Beesley3. Platform-specific libraries can be placed in ROM.
2040d553cfSPaul Beesley
2140d553cfSPaul Beesley4. Functions can be accessed by one or more BL images.
2240d553cfSPaul Beesley
2340d553cfSPaul BeesleyIndex file
2440d553cfSPaul Beesley~~~~~~~~~~
2540d553cfSPaul Beesley
2640d553cfSPaul Beesley.. image:: diagrams/romlib_design.png
2740d553cfSPaul Beesley    :width: 600
2840d553cfSPaul Beesley
2940d553cfSPaul BeesleyLibrary at ROM is described by an index file with the list of functions to be
3040d553cfSPaul Beesleyplaced in ROM. The index file is platform specific and its format is:
3140d553cfSPaul Beesley
3240d553cfSPaul Beesley::
3340d553cfSPaul Beesley
3440d553cfSPaul Beesley    lib function    [patch]
3540d553cfSPaul Beesley
3640d553cfSPaul Beesley    lib      -- Name of the library the function belongs to
3740d553cfSPaul Beesley    function -- Name of the function to be placed in library at ROM
3840d553cfSPaul Beesley    [patch]  -- Option to patch the function
3940d553cfSPaul Beesley
4040d553cfSPaul BeesleyIt is also possible to insert reserved spaces in the list by using the keyword
4140d553cfSPaul Beesley"reserved" rather than the "lib" and "function" names as shown below:
4240d553cfSPaul Beesley
4340d553cfSPaul Beesley::
4440d553cfSPaul Beesley
4540d553cfSPaul Beesley    reserved    reserved
4640d553cfSPaul Beesley
4740d553cfSPaul BeesleyThe reserved spaces can be used to add more functions in the future without
4840d553cfSPaul Beesleyaffecting the order and location of functions already existing in the jump
4940d553cfSPaul Beesleytable. Also, for additional flexibility and modularity, the index file can
5040d553cfSPaul Beesleyinclude other index files.
5140d553cfSPaul Beesley
5240d553cfSPaul BeesleyFor an index file example, refer to ``lib/romlib/jmptbl.i``.
5340d553cfSPaul Beesley
5440d553cfSPaul BeesleyWrapper functions
5540d553cfSPaul Beesley~~~~~~~~~~~~~~~~~
5640d553cfSPaul Beesley
5740d553cfSPaul Beesley.. image:: diagrams/romlib_wrapper.png
5840d553cfSPaul Beesley    :width: 600
5940d553cfSPaul Beesley
6040d553cfSPaul BeesleyWhen invoking a function of the "library at ROM", the calling sequence is as
6140d553cfSPaul Beesleyfollows:
6240d553cfSPaul Beesley
6340d553cfSPaul BeesleyBL image --> wrapper function --> jump table entry --> library at ROM
6440d553cfSPaul Beesley
6540d553cfSPaul BeesleyThe index file is used to create a jump table which is placed in ROM. Then, the
6640d553cfSPaul Beesleywrappers refer to the jump table to call the "library at ROM" functions. The
6740d553cfSPaul Beesleywrappers essentially contain a branch instruction to the jump table entry
6840d553cfSPaul Beesleycorresponding to the original function. Finally, the original function in the BL
6940d553cfSPaul Beesleyimage(s) is replaced with the wrapper function.
7040d553cfSPaul Beesley
7140d553cfSPaul BeesleyThe "library at ROM" contains a necessary init function that initialises the
7240d553cfSPaul Beesleyglobal variables defined by the functions inside "library at ROM".
7340d553cfSPaul Beesley
7440d553cfSPaul BeesleyScripts
7540d553cfSPaul Beesley~~~~~~~
7640d553cfSPaul Beesley
7740d553cfSPaul BeesleyThere are several scripts that generate the necessary files for the "library at
7840d553cfSPaul BeesleyROM" to work:
7940d553cfSPaul Beesley
8040d553cfSPaul Beesley1. ``gentbl.sh`` - Generates the jump table by parsing the index file.
8140d553cfSPaul Beesley
8240d553cfSPaul Beesley2. ``genvar.sh`` - Generates the jump table global variable (**not** the jump
8340d553cfSPaul Beesley   table itself) with the absolute address in ROM. This global variable is,
8440d553cfSPaul Beesley   basically, a pointer to the jump table.
8540d553cfSPaul Beesley
8640d553cfSPaul Beesley3. ``genwrappers.sh`` - Generates a wrapper function for each entry in the index
8740d553cfSPaul Beesley   file except for the ones that contain the keyword ``patch``. The generated
8840d553cfSPaul Beesley   wrapper file is called ``<lib>_<fn_name>.S``.
8940d553cfSPaul Beesley
9040d553cfSPaul BeesleyPatching of functions in library at ROM
9140d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9240d553cfSPaul Beesley
9340d553cfSPaul BeesleyThe ``genwrappers.sh`` script does not generate wrappers for the entries in the
9440d553cfSPaul Beesleyindex file that contain the keyword ``patch``. Thus, it allows calling the
9540d553cfSPaul Beesleyfunction from the actual library by breaking the link to the  "library at ROM"
9640d553cfSPaul Beesleyversion of this function.
9740d553cfSPaul Beesley
9840d553cfSPaul BeesleyThe calling sequence for a patched function is as follows:
9940d553cfSPaul Beesley
10040d553cfSPaul BeesleyBL image --> function
10140d553cfSPaul Beesley
10240d553cfSPaul BeesleyBuild library at ROM
10340d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~
10440d553cfSPaul Beesley
10540d553cfSPaul BeesleyThe environment variable ``CROSS_COMPILE`` must be set as per the user guide.
10640d553cfSPaul BeesleyIn the below example the usage of ROMLIB together with mbed TLS is demonstrated
10740d553cfSPaul Beesleyto showcase the benefits of library at ROM - it's not mandatory.
10840d553cfSPaul Beesley
109*29c02529SPaul Beesley.. code:: shell
11040d553cfSPaul Beesley
11140d553cfSPaul Beesley    make PLAT=fvp                                                   \
11240d553cfSPaul Beesley    MBEDTLS_DIR=</path/to/mbedtls/>                                 \
11340d553cfSPaul Beesley    TRUSTED_BOARD_BOOT=1 GENERATE_COT=1                             \
11440d553cfSPaul Beesley    ARM_ROTPK_LOCATION=devel_rsa                                    \
11540d553cfSPaul Beesley    ROT_KEY=plat/arm/board/common/rotpk/arm_rotprivk_rsa.pem        \
11640d553cfSPaul Beesley    BL33=</path/to/bl33.bin>                                        \
11740d553cfSPaul Beesley    USE_ROMLIB=1                                                    \
11840d553cfSPaul Beesley    all fip
11940d553cfSPaul Beesley
12040d553cfSPaul BeesleyKnown issue
12140d553cfSPaul Beesley-----------
12240d553cfSPaul BeesleyWhen building library at ROM, a clean build is always required. This is
12340d553cfSPaul Beesleynecessary when changes are made to the index files, e.g. adding new functions,
12440d553cfSPaul Beesleypatching existing ones etc.
12540d553cfSPaul Beesley
12640d553cfSPaul Beesley--------------
12740d553cfSPaul Beesley
12840d553cfSPaul Beesley*Copyright (c) 2019, Arm Limited. All rights reserved.*
129