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 74d8210dc6SImre KisScript 75d8210dc6SImre Kis~~~~~~ 7640d553cfSPaul Beesley 77*3b57ae23SManish V BadarkheThere is a ``romlib_generator.py`` Python script that generates the necessary 78d8210dc6SImre Kisfiles for the "library at ROM" to work. It implements multiple functions: 7940d553cfSPaul Beesley 80*3b57ae23SManish V Badarkhe1. ``romlib_generator.py gentbl [args]`` - Generates the jump table by parsing 81d8210dc6SImre Kis the index file. 8240d553cfSPaul Beesley 83d8210dc6SImre Kis2. ``romlib_generator.py genvar [args]`` - Generates the jump table global 84d8210dc6SImre Kis variable (**not** the jump table itself) with the absolute address in ROM. 85d8210dc6SImre Kis This global variable is, basically, a pointer to the jump table. 8640d553cfSPaul Beesley 87d8210dc6SImre Kis3. ``romlib_generator.py genwrappers [args]`` - Generates a wrapper function for 88d8210dc6SImre Kis each entry in the index file except for the ones that contain the keyword 89d8210dc6SImre Kis ``patch``. The generated wrapper file is called ``<fn_name>.s``. 90d8210dc6SImre Kis 91d8210dc6SImre Kis4. ``romlib_generator.py pre [args]`` - Preprocesses the index file which means 92d8210dc6SImre Kis it resolves all the include commands in the file recursively. It can also 93d8210dc6SImre Kis generate a dependency file of the included index files which can be directly 94d8210dc6SImre Kis used in makefiles. 95d8210dc6SImre Kis 96*3b57ae23SManish V BadarkheEach ``romlib_generator.py`` function has its own manual which is accessible by 97d8210dc6SImre Kisruning ``romlib_generator.py [function] --help``. 98d8210dc6SImre Kis 99*3b57ae23SManish V Badarkhe``romlib_generator.py`` requires Python 3 environment. 100d8210dc6SImre Kis 10140d553cfSPaul Beesley 10240d553cfSPaul BeesleyPatching of functions in library at ROM 10340d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 10440d553cfSPaul Beesley 105d8210dc6SImre KisThe ``romlib_generator.py genwrappers`` does not generate wrappers for the 106d8210dc6SImre Kisentries in the index file that contain the keyword ``patch``. Thus, it allows 107d8210dc6SImre Kiscalling the function from the actual library by breaking the link to the 108d8210dc6SImre 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 1144685b64fSLouis MayencourtMemory impact 1154685b64fSLouis Mayencourt~~~~~~~~~~~~~ 1164685b64fSLouis Mayencourt 1174685b64fSLouis MayencourtUsing library at ROM will modify the memory layout of the BL images: 11843f35ef5SPaul Beesley 1194685b64fSLouis Mayencourt- The ROM library needs a page aligned RAM section to hold the RW data. This 1204685b64fSLouis Mayencourt section is defined by the ROMLIB_RW_BASE and ROMLIB_RW_END macros. 1214685b64fSLouis Mayencourt On Arm platforms a section of 1 page (0x1000) is allocated at the top of SRAM. 1224685b64fSLouis Mayencourt This will have for effect to shift down all the BL images by 1 page. 12343f35ef5SPaul Beesley 1244685b64fSLouis Mayencourt- Depending on the functions moved to the ROM library, the size of the BL images 1254685b64fSLouis Mayencourt will be reduced. 1264685b64fSLouis Mayencourt For example: moving MbedTLS function into the ROM library reduces BL1 and 1274685b64fSLouis Mayencourt BL2, but not BL31. 12843f35ef5SPaul Beesley 1294685b64fSLouis Mayencourt- This change in BL images size can be taken into consideration to optimize the 1304685b64fSLouis Mayencourt memory layout when defining the BLx_BASE macros. 1314685b64fSLouis Mayencourt 13240d553cfSPaul BeesleyBuild library at ROM 13340d553cfSPaul Beesley~~~~~~~~~~~~~~~~~~~~~ 13440d553cfSPaul Beesley 13543f35ef5SPaul BeesleyThe environment variable ``CROSS_COMPILE`` must be set appropriately. Refer to 13643f35ef5SPaul Beesley:ref:`Performing an Initial Build` for more information about setting this 13743f35ef5SPaul Beesleyvariable. 13843f35ef5SPaul Beesley 13940d553cfSPaul BeesleyIn the below example the usage of ROMLIB together with mbed TLS is demonstrated 14040d553cfSPaul Beesleyto showcase the benefits of library at ROM - it's not mandatory. 14140d553cfSPaul Beesley 14229c02529SPaul Beesley.. code:: shell 14340d553cfSPaul Beesley 14440d553cfSPaul Beesley make PLAT=fvp \ 14540d553cfSPaul Beesley MBEDTLS_DIR=</path/to/mbedtls/> \ 14640d553cfSPaul Beesley TRUSTED_BOARD_BOOT=1 GENERATE_COT=1 \ 14740d553cfSPaul Beesley ARM_ROTPK_LOCATION=devel_rsa \ 14840d553cfSPaul Beesley ROT_KEY=plat/arm/board/common/rotpk/arm_rotprivk_rsa.pem \ 14940d553cfSPaul Beesley BL33=</path/to/bl33.bin> \ 15040d553cfSPaul Beesley USE_ROMLIB=1 \ 15140d553cfSPaul Beesley all fip 15240d553cfSPaul Beesley 15340d553cfSPaul Beesley-------------- 15440d553cfSPaul Beesley 15540d553cfSPaul Beesley*Copyright (c) 2019, Arm Limited. All rights reserved.* 156