xref: /rk3399_ARM-atf/docs/components/romlib-design.rst (revision 40d553cfde38d4f68449c62967cd1ce0d6478750)
1*40d553cfSPaul BeesleyLibrary at ROM
2*40d553cfSPaul Beesley==============
3*40d553cfSPaul Beesley
4*40d553cfSPaul Beesley.. section-numbering::
5*40d553cfSPaul Beesley    :suffix: .
6*40d553cfSPaul Beesley
7*40d553cfSPaul Beesley.. contents::
8*40d553cfSPaul Beesley
9*40d553cfSPaul BeesleyThis document provides an overview of the "library at ROM" implementation in
10*40d553cfSPaul BeesleyTrusted Firmware-A (TF-A).
11*40d553cfSPaul Beesley
12*40d553cfSPaul BeesleyIntroduction
13*40d553cfSPaul Beesley~~~~~~~~~~~~
14*40d553cfSPaul Beesley
15*40d553cfSPaul BeesleyThe "library at ROM" feature allows platforms to build a library of functions to
16*40d553cfSPaul Beesleybe placed in ROM. This reduces SRAM usage by utilising the available space in
17*40d553cfSPaul BeesleyROM. The "library at ROM" contains a jump table with the list of functions that
18*40d553cfSPaul Beesleyare placed in ROM. The capabilities of the "library at ROM" are:
19*40d553cfSPaul Beesley
20*40d553cfSPaul Beesley1. Functions can be from one or several libraries.
21*40d553cfSPaul Beesley
22*40d553cfSPaul Beesley2. Functions can be patched after they have been programmed into ROM.
23*40d553cfSPaul Beesley
24*40d553cfSPaul Beesley3. Platform-specific libraries can be placed in ROM.
25*40d553cfSPaul Beesley
26*40d553cfSPaul Beesley4. Functions can be accessed by one or more BL images.
27*40d553cfSPaul Beesley
28*40d553cfSPaul BeesleyIndex file
29*40d553cfSPaul Beesley~~~~~~~~~~
30*40d553cfSPaul Beesley
31*40d553cfSPaul Beesley.. image:: diagrams/romlib_design.png
32*40d553cfSPaul Beesley    :width: 600
33*40d553cfSPaul Beesley
34*40d553cfSPaul BeesleyLibrary at ROM is described by an index file with the list of functions to be
35*40d553cfSPaul Beesleyplaced in ROM. The index file is platform specific and its format is:
36*40d553cfSPaul Beesley
37*40d553cfSPaul Beesley::
38*40d553cfSPaul Beesley
39*40d553cfSPaul Beesley    lib function    [patch]
40*40d553cfSPaul Beesley
41*40d553cfSPaul Beesley    lib      -- Name of the library the function belongs to
42*40d553cfSPaul Beesley    function -- Name of the function to be placed in library at ROM
43*40d553cfSPaul Beesley    [patch]  -- Option to patch the function
44*40d553cfSPaul Beesley
45*40d553cfSPaul BeesleyIt is also possible to insert reserved spaces in the list by using the keyword
46*40d553cfSPaul Beesley"reserved" rather than the "lib" and "function" names as shown below:
47*40d553cfSPaul Beesley
48*40d553cfSPaul Beesley::
49*40d553cfSPaul Beesley
50*40d553cfSPaul Beesley    reserved    reserved
51*40d553cfSPaul Beesley
52*40d553cfSPaul BeesleyThe reserved spaces can be used to add more functions in the future without
53*40d553cfSPaul Beesleyaffecting the order and location of functions already existing in the jump
54*40d553cfSPaul Beesleytable. Also, for additional flexibility and modularity, the index file can
55*40d553cfSPaul Beesleyinclude other index files.
56*40d553cfSPaul Beesley
57*40d553cfSPaul BeesleyFor an index file example, refer to ``lib/romlib/jmptbl.i``.
58*40d553cfSPaul Beesley
59*40d553cfSPaul BeesleyWrapper functions
60*40d553cfSPaul Beesley~~~~~~~~~~~~~~~~~
61*40d553cfSPaul Beesley
62*40d553cfSPaul Beesley.. image:: diagrams/romlib_wrapper.png
63*40d553cfSPaul Beesley    :width: 600
64*40d553cfSPaul Beesley
65*40d553cfSPaul BeesleyWhen invoking a function of the "library at ROM", the calling sequence is as
66*40d553cfSPaul Beesleyfollows:
67*40d553cfSPaul Beesley
68*40d553cfSPaul BeesleyBL image --> wrapper function --> jump table entry --> library at ROM
69*40d553cfSPaul Beesley
70*40d553cfSPaul BeesleyThe index file is used to create a jump table which is placed in ROM. Then, the
71*40d553cfSPaul Beesleywrappers refer to the jump table to call the "library at ROM" functions. The
72*40d553cfSPaul Beesleywrappers essentially contain a branch instruction to the jump table entry
73*40d553cfSPaul Beesleycorresponding to the original function. Finally, the original function in the BL
74*40d553cfSPaul Beesleyimage(s) is replaced with the wrapper function.
75*40d553cfSPaul Beesley
76*40d553cfSPaul BeesleyThe "library at ROM" contains a necessary init function that initialises the
77*40d553cfSPaul Beesleyglobal variables defined by the functions inside "library at ROM".
78*40d553cfSPaul Beesley
79*40d553cfSPaul BeesleyScripts
80*40d553cfSPaul Beesley~~~~~~~
81*40d553cfSPaul Beesley
82*40d553cfSPaul BeesleyThere are several scripts that generate the necessary files for the "library at
83*40d553cfSPaul BeesleyROM" to work:
84*40d553cfSPaul Beesley
85*40d553cfSPaul Beesley1. ``gentbl.sh`` - Generates the jump table by parsing the index file.
86*40d553cfSPaul Beesley
87*40d553cfSPaul Beesley2. ``genvar.sh`` - Generates the jump table global variable (**not** the jump
88*40d553cfSPaul Beesley   table itself) with the absolute address in ROM. This global variable is,
89*40d553cfSPaul Beesley   basically, a pointer to the jump table.
90*40d553cfSPaul Beesley
91*40d553cfSPaul Beesley3. ``genwrappers.sh`` - Generates a wrapper function for each entry in the index
92*40d553cfSPaul Beesley   file except for the ones that contain the keyword ``patch``. The generated
93*40d553cfSPaul Beesley   wrapper file is called ``<lib>_<fn_name>.S``.
94*40d553cfSPaul Beesley
95*40d553cfSPaul BeesleyPatching of functions in library at ROM
96*40d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
97*40d553cfSPaul Beesley
98*40d553cfSPaul BeesleyThe ``genwrappers.sh`` script does not generate wrappers for the entries in the
99*40d553cfSPaul Beesleyindex file that contain the keyword ``patch``. Thus, it allows calling the
100*40d553cfSPaul Beesleyfunction from the actual library by breaking the link to the  "library at ROM"
101*40d553cfSPaul Beesleyversion of this function.
102*40d553cfSPaul Beesley
103*40d553cfSPaul BeesleyThe calling sequence for a patched function is as follows:
104*40d553cfSPaul Beesley
105*40d553cfSPaul BeesleyBL image --> function
106*40d553cfSPaul Beesley
107*40d553cfSPaul BeesleyBuild library at ROM
108*40d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~
109*40d553cfSPaul Beesley
110*40d553cfSPaul BeesleyThe environment variable ``CROSS_COMPILE`` must be set as per the user guide.
111*40d553cfSPaul BeesleyIn the below example the usage of ROMLIB together with mbed TLS is demonstrated
112*40d553cfSPaul Beesleyto showcase the benefits of library at ROM - it's not mandatory.
113*40d553cfSPaul Beesley
114*40d553cfSPaul Beesley::
115*40d553cfSPaul Beesley
116*40d553cfSPaul Beesley    make PLAT=fvp                                                   \
117*40d553cfSPaul Beesley    MBEDTLS_DIR=</path/to/mbedtls/>                                 \
118*40d553cfSPaul Beesley    TRUSTED_BOARD_BOOT=1 GENERATE_COT=1                             \
119*40d553cfSPaul Beesley    ARM_ROTPK_LOCATION=devel_rsa                                    \
120*40d553cfSPaul Beesley    ROT_KEY=plat/arm/board/common/rotpk/arm_rotprivk_rsa.pem        \
121*40d553cfSPaul Beesley    BL33=</path/to/bl33.bin>                                        \
122*40d553cfSPaul Beesley    USE_ROMLIB=1                                                    \
123*40d553cfSPaul Beesley    all fip
124*40d553cfSPaul Beesley
125*40d553cfSPaul BeesleyKnown issue
126*40d553cfSPaul Beesley-----------
127*40d553cfSPaul BeesleyWhen building library at ROM, a clean build is always required. This is
128*40d553cfSPaul Beesleynecessary when changes are made to the index files, e.g. adding new functions,
129*40d553cfSPaul Beesleypatching existing ones etc.
130*40d553cfSPaul Beesley
131*40d553cfSPaul Beesley--------------
132*40d553cfSPaul Beesley
133*40d553cfSPaul Beesley*Copyright (c) 2019, Arm Limited. All rights reserved.*
134