xref: /rk3399_ARM-atf/docs/components/romlib-design.rst (revision d8210dc67af5b8c5f17a799bc2af5186dd30ba21)
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
26a2c320a8SPaul Beesley.. image:: ../resources/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
45*d8210dc6SImre Kis    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
57a2c320a8SPaul Beesley.. image:: ../resources/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
74*d8210dc6SImre KisScript
75*d8210dc6SImre Kis~~~~~~
7640d553cfSPaul Beesley
77*d8210dc6SImre KisThere is a ``romlib_generate.py`` Python script that generates the necessary
78*d8210dc6SImre Kisfiles for the "library at ROM" to work. It implements multiple functions:
7940d553cfSPaul Beesley
80*d8210dc6SImre Kis1. ``romlib_generate.py gentbl [args]`` - Generates the jump table by parsing
81*d8210dc6SImre Kis   the index file.
8240d553cfSPaul Beesley
83*d8210dc6SImre Kis2. ``romlib_generator.py genvar [args]`` - Generates the jump table global
84*d8210dc6SImre Kis   variable (**not** the jump table itself) with the absolute address in ROM.
85*d8210dc6SImre Kis   This global variable is, basically, a pointer to the jump table.
8640d553cfSPaul Beesley
87*d8210dc6SImre Kis3. ``romlib_generator.py genwrappers [args]`` - Generates a wrapper function for
88*d8210dc6SImre Kis   each entry in the index file except for the ones that contain the keyword
89*d8210dc6SImre Kis   ``patch``. The generated wrapper file is called ``<fn_name>.s``.
90*d8210dc6SImre Kis
91*d8210dc6SImre Kis4. ``romlib_generator.py pre [args]`` - Preprocesses the index file which means
92*d8210dc6SImre Kis   it resolves all the include commands in the file recursively. It can also
93*d8210dc6SImre Kis   generate a dependency file of the included index files which can be directly
94*d8210dc6SImre Kis   used in makefiles.
95*d8210dc6SImre Kis
96*d8210dc6SImre KisEach ``romlib_generate.py`` function has its own manual which is accessible by
97*d8210dc6SImre Kisruning ``romlib_generator.py [function] --help``.
98*d8210dc6SImre Kis
99*d8210dc6SImre Kis``romlib_generate.py`` requires Python 3 environment.
100*d8210dc6SImre Kis
10140d553cfSPaul Beesley
10240d553cfSPaul BeesleyPatching of functions in library at ROM
10340d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10440d553cfSPaul Beesley
105*d8210dc6SImre KisThe ``romlib_generator.py genwrappers`` does not generate wrappers for the
106*d8210dc6SImre Kisentries in the index file that contain the keyword ``patch``. Thus, it allows
107*d8210dc6SImre Kiscalling the function from the actual library by breaking the link to the
108*d8210dc6SImre Kis"library at ROM" version of this function.
10940d553cfSPaul Beesley
11040d553cfSPaul BeesleyThe calling sequence for a patched function is as follows:
11140d553cfSPaul Beesley
11240d553cfSPaul BeesleyBL image --> function
11340d553cfSPaul Beesley
11440d553cfSPaul BeesleyBuild library at ROM
11540d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~
11640d553cfSPaul Beesley
11740d553cfSPaul BeesleyThe environment variable ``CROSS_COMPILE`` must be set as per the user guide.
11840d553cfSPaul BeesleyIn the below example the usage of ROMLIB together with mbed TLS is demonstrated
11940d553cfSPaul Beesleyto showcase the benefits of library at ROM - it's not mandatory.
12040d553cfSPaul Beesley
12129c02529SPaul Beesley.. code:: shell
12240d553cfSPaul Beesley
12340d553cfSPaul Beesley    make PLAT=fvp                                                   \
12440d553cfSPaul Beesley    MBEDTLS_DIR=</path/to/mbedtls/>                                 \
12540d553cfSPaul Beesley    TRUSTED_BOARD_BOOT=1 GENERATE_COT=1                             \
12640d553cfSPaul Beesley    ARM_ROTPK_LOCATION=devel_rsa                                    \
12740d553cfSPaul Beesley    ROT_KEY=plat/arm/board/common/rotpk/arm_rotprivk_rsa.pem        \
12840d553cfSPaul Beesley    BL33=</path/to/bl33.bin>                                        \
12940d553cfSPaul Beesley    USE_ROMLIB=1                                                    \
13040d553cfSPaul Beesley    all fip
13140d553cfSPaul Beesley
13240d553cfSPaul Beesley--------------
13340d553cfSPaul Beesley
13440d553cfSPaul Beesley*Copyright (c) 2019, Arm Limited. All rights reserved.*
135