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 45d8210dc6SImre 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*d95d56bdSJimmy BrissonWrapper functions are specified at the link stage of compilation and cannot 75*d95d56bdSJimmy Brissoninterpose uppon functions within the same translation unit. For example, if 76*d95d56bdSJimmy Brissonfunction ``fn_a`` calls ``fn_b`` within translation unit ``functions.c`` and 77*d95d56bdSJimmy Brissonthe romlib jump table includes an entry for ``fn_b``, ``fn_a`` will include 78*d95d56bdSJimmy Brissona reference to ``fn_b``'s original program text instead of the wrapper. Thus 79*d95d56bdSJimmy Brissonthe jumptable author must take care to include public entry points into 80*d95d56bdSJimmy Brissontranslation units to avoid paying the program text cost twice, once in the 81*d95d56bdSJimmy Brissonoriginal executable and once in romlib. 82*d95d56bdSJimmy Brisson 83d8210dc6SImre KisScript 84d8210dc6SImre Kis~~~~~~ 8540d553cfSPaul Beesley 863b57ae23SManish V BadarkheThere is a ``romlib_generator.py`` Python script that generates the necessary 87d8210dc6SImre Kisfiles for the "library at ROM" to work. It implements multiple functions: 8840d553cfSPaul Beesley 893b57ae23SManish V Badarkhe1. ``romlib_generator.py gentbl [args]`` - Generates the jump table by parsing 90d8210dc6SImre Kis the index file. 9140d553cfSPaul Beesley 92d8210dc6SImre Kis2. ``romlib_generator.py genvar [args]`` - Generates the jump table global 93d8210dc6SImre Kis variable (**not** the jump table itself) with the absolute address in ROM. 94d8210dc6SImre Kis This global variable is, basically, a pointer to the jump table. 9540d553cfSPaul Beesley 96d8210dc6SImre Kis3. ``romlib_generator.py genwrappers [args]`` - Generates a wrapper function for 97d8210dc6SImre Kis each entry in the index file except for the ones that contain the keyword 98*d95d56bdSJimmy Brisson ``patch``. The generated wrapper file is called ``wrappers.s``. 99d8210dc6SImre Kis 100d8210dc6SImre Kis4. ``romlib_generator.py pre [args]`` - Preprocesses the index file which means 101d8210dc6SImre Kis it resolves all the include commands in the file recursively. It can also 102d8210dc6SImre Kis generate a dependency file of the included index files which can be directly 103d8210dc6SImre Kis used in makefiles. 104d8210dc6SImre Kis 1053b57ae23SManish V BadarkheEach ``romlib_generator.py`` function has its own manual which is accessible by 106d8210dc6SImre Kisruning ``romlib_generator.py [function] --help``. 107d8210dc6SImre Kis 1083b57ae23SManish V Badarkhe``romlib_generator.py`` requires Python 3 environment. 109d8210dc6SImre Kis 11040d553cfSPaul Beesley 11140d553cfSPaul BeesleyPatching of functions in library at ROM 11240d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 11340d553cfSPaul Beesley 114d8210dc6SImre KisThe ``romlib_generator.py genwrappers`` does not generate wrappers for the 115d8210dc6SImre Kisentries in the index file that contain the keyword ``patch``. Thus, it allows 116d8210dc6SImre Kiscalling the function from the actual library by breaking the link to the 117d8210dc6SImre Kis"library at ROM" version of this function. 11840d553cfSPaul Beesley 11940d553cfSPaul BeesleyThe calling sequence for a patched function is as follows: 12040d553cfSPaul Beesley 12140d553cfSPaul BeesleyBL image --> function 12240d553cfSPaul Beesley 1234685b64fSLouis MayencourtMemory impact 1244685b64fSLouis Mayencourt~~~~~~~~~~~~~ 1254685b64fSLouis Mayencourt 1264685b64fSLouis MayencourtUsing library at ROM will modify the memory layout of the BL images: 12743f35ef5SPaul Beesley 1284685b64fSLouis Mayencourt- The ROM library needs a page aligned RAM section to hold the RW data. This 1294685b64fSLouis Mayencourt section is defined by the ROMLIB_RW_BASE and ROMLIB_RW_END macros. 1304685b64fSLouis Mayencourt On Arm platforms a section of 1 page (0x1000) is allocated at the top of SRAM. 1314685b64fSLouis Mayencourt This will have for effect to shift down all the BL images by 1 page. 13243f35ef5SPaul Beesley 1334685b64fSLouis Mayencourt- Depending on the functions moved to the ROM library, the size of the BL images 1344685b64fSLouis Mayencourt will be reduced. 1354685b64fSLouis Mayencourt For example: moving MbedTLS function into the ROM library reduces BL1 and 1364685b64fSLouis Mayencourt BL2, but not BL31. 13743f35ef5SPaul Beesley 1384685b64fSLouis Mayencourt- This change in BL images size can be taken into consideration to optimize the 1394685b64fSLouis Mayencourt memory layout when defining the BLx_BASE macros. 1404685b64fSLouis Mayencourt 14140d553cfSPaul BeesleyBuild library at ROM 14240d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~ 14340d553cfSPaul Beesley 14443f35ef5SPaul BeesleyThe environment variable ``CROSS_COMPILE`` must be set appropriately. Refer to 14543f35ef5SPaul Beesley:ref:`Performing an Initial Build` for more information about setting this 14643f35ef5SPaul Beesleyvariable. 14743f35ef5SPaul Beesley 14840d553cfSPaul BeesleyIn the below example the usage of ROMLIB together with mbed TLS is demonstrated 14940d553cfSPaul Beesleyto showcase the benefits of library at ROM - it's not mandatory. 15040d553cfSPaul Beesley 15129c02529SPaul Beesley.. code:: shell 15240d553cfSPaul Beesley 15340d553cfSPaul Beesley make PLAT=fvp \ 15440d553cfSPaul Beesley MBEDTLS_DIR=</path/to/mbedtls/> \ 15540d553cfSPaul Beesley TRUSTED_BOARD_BOOT=1 GENERATE_COT=1 \ 15640d553cfSPaul Beesley ARM_ROTPK_LOCATION=devel_rsa \ 15740d553cfSPaul Beesley ROT_KEY=plat/arm/board/common/rotpk/arm_rotprivk_rsa.pem \ 15840d553cfSPaul Beesley BL33=</path/to/bl33.bin> \ 15940d553cfSPaul Beesley USE_ROMLIB=1 \ 16040d553cfSPaul Beesley all fip 16140d553cfSPaul Beesley 16240d553cfSPaul Beesley-------------- 16340d553cfSPaul Beesley 16440d553cfSPaul Beesley*Copyright (c) 2019, Arm Limited. All rights reserved.* 165