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