xref: /arm-trusted-firmware/docs/components/romlib-design.rst (revision 91f16700b400a8c0651d24a598fc48ee2997a0d7)
1*91f16700SchasingluluLibrary at ROM
2*91f16700Schasinglulu==============
3*91f16700Schasinglulu
4*91f16700SchasingluluThis document provides an overview of the "library at ROM" implementation in
5*91f16700SchasingluluTrusted Firmware-A (TF-A).
6*91f16700Schasinglulu
7*91f16700SchasingluluIntroduction
8*91f16700Schasinglulu~~~~~~~~~~~~
9*91f16700Schasinglulu
10*91f16700SchasingluluThe "library at ROM" feature allows platforms to build a library of functions to
11*91f16700Schasinglulube placed in ROM. This reduces SRAM usage by utilising the available space in
12*91f16700SchasingluluROM. The "library at ROM" contains a jump table with the list of functions that
13*91f16700Schasingluluare placed in ROM. The capabilities of the "library at ROM" are:
14*91f16700Schasinglulu
15*91f16700Schasinglulu1. Functions can be from one or several libraries.
16*91f16700Schasinglulu
17*91f16700Schasinglulu2. Functions can be patched after they have been programmed into ROM.
18*91f16700Schasinglulu
19*91f16700Schasinglulu3. Platform-specific libraries can be placed in ROM.
20*91f16700Schasinglulu
21*91f16700Schasinglulu4. Functions can be accessed by one or more BL images.
22*91f16700Schasinglulu
23*91f16700SchasingluluIndex file
24*91f16700Schasinglulu~~~~~~~~~~
25*91f16700Schasinglulu
26*91f16700Schasinglulu.. image:: ../resources/diagrams/romlib_design.png
27*91f16700Schasinglulu    :width: 600
28*91f16700Schasinglulu
29*91f16700SchasingluluLibrary at ROM is described by an index file with the list of functions to be
30*91f16700Schasingluluplaced in ROM. The index file is platform specific and its format is:
31*91f16700Schasinglulu
32*91f16700Schasinglulu::
33*91f16700Schasinglulu
34*91f16700Schasinglulu    lib function    [patch]
35*91f16700Schasinglulu
36*91f16700Schasinglulu    lib      -- Name of the library the function belongs to
37*91f16700Schasinglulu    function -- Name of the function to be placed in library at ROM
38*91f16700Schasinglulu    [patch]  -- Option to patch the function
39*91f16700Schasinglulu
40*91f16700SchasingluluIt is also possible to insert reserved spaces in the list by using the keyword
41*91f16700Schasinglulu"reserved" rather than the "lib" and "function" names as shown below:
42*91f16700Schasinglulu
43*91f16700Schasinglulu::
44*91f16700Schasinglulu
45*91f16700Schasinglulu    reserved
46*91f16700Schasinglulu
47*91f16700SchasingluluThe reserved spaces can be used to add more functions in the future without
48*91f16700Schasingluluaffecting the order and location of functions already existing in the jump
49*91f16700Schasinglulutable. Also, for additional flexibility and modularity, the index file can
50*91f16700Schasingluluinclude other index files.
51*91f16700Schasinglulu
52*91f16700SchasingluluFor an index file example, refer to ``lib/romlib/jmptbl.i``.
53*91f16700Schasinglulu
54*91f16700SchasingluluWrapper functions
55*91f16700Schasinglulu~~~~~~~~~~~~~~~~~
56*91f16700Schasinglulu
57*91f16700Schasinglulu.. image:: ../resources/diagrams/romlib_wrapper.png
58*91f16700Schasinglulu    :width: 600
59*91f16700Schasinglulu
60*91f16700SchasingluluWhen invoking a function of the "library at ROM", the calling sequence is as
61*91f16700Schasinglulufollows:
62*91f16700Schasinglulu
63*91f16700SchasingluluBL image --> wrapper function --> jump table entry --> library at ROM
64*91f16700Schasinglulu
65*91f16700SchasingluluThe index file is used to create a jump table which is placed in ROM. Then, the
66*91f16700Schasingluluwrappers refer to the jump table to call the "library at ROM" functions. The
67*91f16700Schasingluluwrappers essentially contain a branch instruction to the jump table entry
68*91f16700Schasinglulucorresponding to the original function. Finally, the original function in the BL
69*91f16700Schasingluluimage(s) is replaced with the wrapper function.
70*91f16700Schasinglulu
71*91f16700SchasingluluThe "library at ROM" contains a necessary init function that initialises the
72*91f16700Schasingluluglobal variables defined by the functions inside "library at ROM".
73*91f16700Schasinglulu
74*91f16700SchasingluluScript
75*91f16700Schasinglulu~~~~~~
76*91f16700Schasinglulu
77*91f16700SchasingluluThere is a ``romlib_generate.py`` Python script that generates the necessary
78*91f16700Schasinglulufiles for the "library at ROM" to work. It implements multiple functions:
79*91f16700Schasinglulu
80*91f16700Schasinglulu1. ``romlib_generate.py gentbl [args]`` - Generates the jump table by parsing
81*91f16700Schasinglulu   the index file.
82*91f16700Schasinglulu
83*91f16700Schasinglulu2. ``romlib_generator.py genvar [args]`` - Generates the jump table global
84*91f16700Schasinglulu   variable (**not** the jump table itself) with the absolute address in ROM.
85*91f16700Schasinglulu   This global variable is, basically, a pointer to the jump table.
86*91f16700Schasinglulu
87*91f16700Schasinglulu3. ``romlib_generator.py genwrappers [args]`` - Generates a wrapper function for
88*91f16700Schasinglulu   each entry in the index file except for the ones that contain the keyword
89*91f16700Schasinglulu   ``patch``. The generated wrapper file is called ``<fn_name>.s``.
90*91f16700Schasinglulu
91*91f16700Schasinglulu4. ``romlib_generator.py pre [args]`` - Preprocesses the index file which means
92*91f16700Schasinglulu   it resolves all the include commands in the file recursively. It can also
93*91f16700Schasinglulu   generate a dependency file of the included index files which can be directly
94*91f16700Schasinglulu   used in makefiles.
95*91f16700Schasinglulu
96*91f16700SchasingluluEach ``romlib_generate.py`` function has its own manual which is accessible by
97*91f16700Schasingluluruning ``romlib_generator.py [function] --help``.
98*91f16700Schasinglulu
99*91f16700Schasinglulu``romlib_generate.py`` requires Python 3 environment.
100*91f16700Schasinglulu
101*91f16700Schasinglulu
102*91f16700SchasingluluPatching of functions in library at ROM
103*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
104*91f16700Schasinglulu
105*91f16700SchasingluluThe ``romlib_generator.py genwrappers`` does not generate wrappers for the
106*91f16700Schasingluluentries in the index file that contain the keyword ``patch``. Thus, it allows
107*91f16700Schasinglulucalling the function from the actual library by breaking the link to the
108*91f16700Schasinglulu"library at ROM" version of this function.
109*91f16700Schasinglulu
110*91f16700SchasingluluThe calling sequence for a patched function is as follows:
111*91f16700Schasinglulu
112*91f16700SchasingluluBL image --> function
113*91f16700Schasinglulu
114*91f16700SchasingluluMemory impact
115*91f16700Schasinglulu~~~~~~~~~~~~~
116*91f16700Schasinglulu
117*91f16700SchasingluluUsing library at ROM will modify the memory layout of the BL images:
118*91f16700Schasinglulu
119*91f16700Schasinglulu- The ROM library needs a page aligned RAM section to hold the RW data. This
120*91f16700Schasinglulu  section is defined by the ROMLIB_RW_BASE and ROMLIB_RW_END macros.
121*91f16700Schasinglulu  On Arm platforms a section of 1 page (0x1000) is allocated at the top of SRAM.
122*91f16700Schasinglulu  This will have for effect to shift down all the BL images by 1 page.
123*91f16700Schasinglulu
124*91f16700Schasinglulu- Depending on the functions moved to the ROM library, the size of the BL images
125*91f16700Schasinglulu  will be reduced.
126*91f16700Schasinglulu  For example: moving MbedTLS function into the ROM library reduces BL1 and
127*91f16700Schasinglulu  BL2, but not BL31.
128*91f16700Schasinglulu
129*91f16700Schasinglulu- This change in BL images size can be taken into consideration to optimize the
130*91f16700Schasinglulu  memory layout when defining the BLx_BASE macros.
131*91f16700Schasinglulu
132*91f16700SchasingluluBuild library at ROM
133*91f16700Schasinglulu~~~~~~~~~~~~~~~~~~~~~
134*91f16700Schasinglulu
135*91f16700SchasingluluThe environment variable ``CROSS_COMPILE`` must be set appropriately. Refer to
136*91f16700Schasinglulu:ref:`Performing an Initial Build` for more information about setting this
137*91f16700Schasingluluvariable.
138*91f16700Schasinglulu
139*91f16700SchasingluluIn the below example the usage of ROMLIB together with mbed TLS is demonstrated
140*91f16700Schasingluluto showcase the benefits of library at ROM - it's not mandatory.
141*91f16700Schasinglulu
142*91f16700Schasinglulu.. code:: shell
143*91f16700Schasinglulu
144*91f16700Schasinglulu    make PLAT=fvp                                                   \
145*91f16700Schasinglulu    MBEDTLS_DIR=</path/to/mbedtls/>                                 \
146*91f16700Schasinglulu    TRUSTED_BOARD_BOOT=1 GENERATE_COT=1                             \
147*91f16700Schasinglulu    ARM_ROTPK_LOCATION=devel_rsa                                    \
148*91f16700Schasinglulu    ROT_KEY=plat/arm/board/common/rotpk/arm_rotprivk_rsa.pem        \
149*91f16700Schasinglulu    BL33=</path/to/bl33.bin>                                        \
150*91f16700Schasinglulu    USE_ROMLIB=1                                                    \
151*91f16700Schasinglulu    all fip
152*91f16700Schasinglulu
153*91f16700Schasinglulu--------------
154*91f16700Schasinglulu
155*91f16700Schasinglulu*Copyright (c) 2019, Arm Limited. All rights reserved.*
156