|
| 1 | +.. _Application_Notes_Android_Secure_Build: |
| 2 | + |
| 3 | +##################################### |
| 4 | +Android Secure Build Application Note |
| 5 | +##################################### |
| 6 | + |
| 7 | +This page describes how to setup secure Android build. |
| 8 | + |
| 9 | +The process is an example, feel free to adapt following your needs. |
| 10 | + |
| 11 | +.. warning:: |
| 12 | + |
| 13 | + This process is only supported with Android user images |
| 14 | + |
| 15 | +Fetching Bootloader, Kernel, and Android |
| 16 | +---------------------------------------- |
| 17 | + |
| 18 | +- Bootloaders: |
| 19 | + |
| 20 | + .. code-block:: console |
| 21 | +
|
| 22 | + $ export YOUR_PATH=~/src/ |
| 23 | + $ mkdir ${YOUR_PATH}/ti-bootloader-aosp/ && cd $_ |
| 24 | + $ repo init -u https://git.ti.com/git/android/manifest.git -b android16-release -m releases/RLS_11_00_00_Bootloader.xml |
| 25 | + $ repo sync |
| 26 | +
|
| 27 | +- Kernel: |
| 28 | + |
| 29 | + .. code-block:: console |
| 30 | +
|
| 31 | + $ mkdir ${YOUR_PATH}/ti-kernel-aosp/ && cd $_ |
| 32 | + $ repo init -u https://git.ti.com/git/android/manifest.git -b android16-release -m releases/RLS_11_00_00_Kernel.xml |
| 33 | + $ repo sync |
| 34 | +
|
| 35 | +- Android: |
| 36 | + |
| 37 | + .. code-block:: console |
| 38 | +
|
| 39 | + $ export YOUR_PATH=~/src/ |
| 40 | + $ mkdir ${YOUR_PATH}/ti-aosp-16 && cd $_ |
| 41 | + $ repo init -u https://git.ti.com/git/android/manifest.git -b android16-release -m releases/RLS_11_00_00.xml |
| 42 | + $ repo sync |
| 43 | +
|
| 44 | +Build Secure Bootloaders |
| 45 | +------------------------ |
| 46 | + |
| 47 | +- Generate TA cryptographic keys: |
| 48 | + |
| 49 | + .. code-block:: console |
| 50 | +
|
| 51 | + $ cd ${YOUR_PATH}/ti-bootloader-aosp/build |
| 52 | + $ ./secure.sh generate_ta_keys |
| 53 | + $ ls -1 .keys/ |
| 54 | + ta.key |
| 55 | + ta_pub.key |
| 56 | +
|
| 57 | +- Generate AVB public/private keys: |
| 58 | + |
| 59 | + .. code-block:: console |
| 60 | +
|
| 61 | + $ cd ${YOUR_PATH}/ti-bootloader-aosp/build |
| 62 | + $ ./secure.sh generate_avb_keys |
| 63 | + $ ls -1 .keys/ |
| 64 | + avb.key |
| 65 | + avb_pub.key |
| 66 | +
|
| 67 | +That will generate ``avb.key`` and ``avb_pub.key`` under ``build/.keys``. |
| 68 | + |
| 69 | +The bootloaders scripts will detect that ``avb_pub.key`` is present and will include it into U-Boot. |
| 70 | + |
| 71 | +.. ifconfig:: CONFIG_part_variant in ('AM62X') |
| 72 | + |
| 73 | + The user can also specify the absolute path of the avb_pub key in yaml config (``build/config/boards/am62x-sk.yaml``): |
| 74 | + |
| 75 | +.. ifconfig:: CONFIG_part_variant in ('AM62PX') |
| 76 | + |
| 77 | + The user can also specify the absolute path of the avb_pub key in yaml config (``build/config/boards/am62p-sk.yaml``): |
| 78 | + |
| 79 | +.. code-block:: yaml |
| 80 | +
|
| 81 | + secure: |
| 82 | + avb_pub_key: ${YOUR_PATH}/ti/avb_pub.key |
| 83 | +
|
| 84 | +.. table:: Security Keys |
| 85 | + |
| 86 | + +---------------------+-----------------------------------------------------------------------------+ |
| 87 | + | Key | Description | |
| 88 | + +=====================+=============================================================================+ |
| 89 | + | ``avb.key`` | (Optional) Private key used by Android to sign images | |
| 90 | + +---------------------+-----------------------------------------------------------------------------+ |
| 91 | + | ``avb_pub.key`` | (Optional) Public key used by U-Boot to verify Android images | |
| 92 | + +---------------------+-----------------------------------------------------------------------------+ |
| 93 | + | ``ta.key`` | (Optional) Private key used to sign Trusted Application (OP-TEE) | |
| 94 | + +---------------------+-----------------------------------------------------------------------------+ |
| 95 | + | ``ta_pub.key`` | (Optional) Public key used to sign Trusted Application (OP-TEE) | |
| 96 | + +---------------------+-----------------------------------------------------------------------------+ |
| 97 | + |
| 98 | + |
| 99 | +Build and copy secure images to Android: |
| 100 | + |
| 101 | +.. code-block:: console |
| 102 | +
|
| 103 | + $ ./build/release_android.sh --aosp=~${YOUR_PATH}/ti-aosp-16 --mode=factory |
| 104 | +
|
| 105 | +.. warning:: |
| 106 | + |
| 107 | + By default on factory mode, OP-TEE is built with this flag: |
| 108 | + |
| 109 | + - ``CFG_RPMB_WRITE_KEY``: |
| 110 | + |
| 111 | + This flag configure the RPMB if this one is not initialized yet. |
| 112 | + |
| 113 | + This operation is performed one time only, once the RPMB is initialized with the key we cannot override it anymore. |
| 114 | + |
| 115 | +.. note:: |
| 116 | + |
| 117 | + **RPMB corruptions**: |
| 118 | + |
| 119 | + If we do several flashes with different builds, we may be faced with RPMB corruptions. |
| 120 | + Indeed some metadata are stored in the RPMB and by default if there is a mismatch OP-TEE fails to load Trusted Applications. |
| 121 | + Thus the OP-TEE keymaster service fails and the device doesn't boot. |
| 122 | + To avoid this situation we can add the flag ``CFG_REE_FS_ALLOW_RESET=y`` in `build_optee.sh`: |
| 123 | + |
| 124 | + Allow secure storage in the REE FS to be entirely deleted without causing anti-rollback errors. |
| 125 | + This is used to reset the secure storage to a clean, empty state. |
| 126 | + |
| 127 | + **Testing Case**: |
| 128 | + |
| 129 | + If you want to test and be sure to reuse RPMB partition you can set ``CFG_RPMB_TESTKEY=y`` in `build_optee.sh`: |
| 130 | + |
| 131 | + OP-TEE can either embed a built-in RPMB key or derive it from platform specific secrets. |
| 132 | + The former case might be useful during development while the later is recommended for production devices. |
| 133 | + |
| 134 | +Copy ``avb.key`` in Android: |
| 135 | + |
| 136 | +.. code-block:: console |
| 137 | +
|
| 138 | + $ cd ${YOUR_PATH}/ti-bootloader-aosp/build |
| 139 | + $ cp .keys/avb.key "${YOUR_PATH}/ti-aosp-16/device/ti/am62x/" |
| 140 | +
|
| 141 | +Replace the following configs in ``device/ti/am62x/BoardConfigCommon.mk``:: |
| 142 | + |
| 143 | + BOARD_AVB_ALGORITHM := SHA256_RSA4096 |
| 144 | + BOARD_AVB_KEY_PATH := device/ti/am62x/avb.key |
| 145 | + BOARD_AVB_BOOT_KEY_PATH := device/ti/am62x/avb.key |
| 146 | + BOARD_AVB_INIT_BOOT_KEY_PATH := device/ti/am62x/avb.key |
| 147 | + BOARD_AVB_VBMETA_VENDOR_DLKM_KEY_PATH := device/ti/am62x/avb.key |
| 148 | + BOARD_AVB_VBMETA_SYSTEM_DLKM_KEY_PATH := device/ti/am62x/avb.key |
| 149 | + |
| 150 | + |
| 151 | +Check images generated in Android: |
| 152 | + |
| 153 | +.. code-block:: console |
| 154 | +
|
| 155 | + $ cd ~${YOUR_PATH}/ti-aosp-16 |
| 156 | + $ git status |
| 157 | +
|
| 158 | +
|
| 159 | +Build Kernel |
| 160 | +------------ |
| 161 | + |
| 162 | +Before building Kernel we need to modify it to support RPMB features. |
| 163 | + |
| 164 | +Download patch : :download:`RPMB_enabling.patch <patches/RPMB_enabling.patch>` and put it ``${YOUR_PATH}/ti-kernel-aosp/common`` folder. |
| 165 | + |
| 166 | +Update symbol list and build kernel: |
| 167 | + |
| 168 | +.. code-block:: console |
| 169 | +
|
| 170 | + $ cd ${YOUR_PATH}/ti-kernel-aosp/common |
| 171 | + $ git am RPMB_enabling.patch |
| 172 | + $ cd ${YOUR_PATH}/ti-kernel-aosp/ |
| 173 | + $ export DIST_DIR=${YOUR_PATH}/ti-aosp-16/device/ti/am62x-kernel/kernel/6.12 |
| 174 | + $ ./update_symbol_list.sh -d ti -b private/devices/ti/am6x |
| 175 | + $ ./tools/bazel run --config=ti //private/devices/ti/am6x:ti_dist -- --destdir=$DIST_DIR |
| 176 | +
|
| 177 | +Build Android |
| 178 | +------------- |
| 179 | + |
| 180 | +.. ifconfig:: CONFIG_part_variant in ('AM62X') |
| 181 | + |
| 182 | + .. code-block:: console |
| 183 | +
|
| 184 | + $ cd ${YOUR_PATH}/ti-aosp-16 |
| 185 | + $ source build/envsetup.sh |
| 186 | + $ lunch am62x-bp2a-user |
| 187 | +
|
| 188 | + $ export FACTORY_BUILD=true |
| 189 | + $ m -j$(nproc) # OR nice -n19 build/soong/soong_ui.bash --make-mode -j$(nproc) |
| 190 | +
|
| 191 | +.. ifconfig:: CONFIG_part_variant in ('AM62PX') |
| 192 | + |
| 193 | + .. code-block:: console |
| 194 | +
|
| 195 | + $ cd ${YOUR_PATH}/ti-aosp-16 |
| 196 | + $ source build/envsetup.sh |
| 197 | + $ lunch am62p-bp2a-user |
| 198 | +
|
| 199 | + $ export FACTORY_BUILD=true |
| 200 | + $ m -j$(nproc) # OR nice -n19 build/soong/soong_ui.bash --make-mode -j$(nproc) |
| 201 | +
|
| 202 | +Flash Android |
| 203 | +------------- |
| 204 | + |
| 205 | +To reflash on a device which already has Android installed, run: |
| 206 | + |
| 207 | +Reboot to U-Boot |
| 208 | + |
| 209 | +.. code-block:: console |
| 210 | +
|
| 211 | + $ adb reboot bootloader |
| 212 | +
|
| 213 | +Flash using the script: in a different terminal, |
| 214 | + |
| 215 | +.. ifconfig:: CONFIG_part_variant in ('AM62X') |
| 216 | + |
| 217 | + change directory to ``out/target/product/am62x`` and run the ``flashall.sh`` script: |
| 218 | + |
| 219 | + .. code-block:: console |
| 220 | +
|
| 221 | + # for AM62x SK EVM (GP) |
| 222 | + $ sudo ./flashall.sh --board am62x-sk |
| 223 | +
|
| 224 | + # for AM62x SK EVM (HS-FS) |
| 225 | + $ sudo ./flashall.sh --board am62x-sk --hsfs |
| 226 | +
|
| 227 | +.. ifconfig:: CONFIG_part_variant in ('AM62PX') |
| 228 | + |
| 229 | + change directory to ``out/target/product/am62p`` and run the ``flashall.sh`` script: |
| 230 | + |
| 231 | + .. code-block:: console |
| 232 | +
|
| 233 | + # for AM62P SK EVM |
| 234 | + $ sudo ./flashall.sh --board am62px-sk |
| 235 | +
|
| 236 | +.. warning:: |
| 237 | + |
| 238 | + **Device Locking and Unlock Capability** |
| 239 | + |
| 240 | + After flashing with secure build, the device will be locked and ``fastboot flashing unlock`` commands will be disabled by default. |
| 241 | + |
| 242 | + To enable unlock capability, you have several options: |
| 243 | + |
| 244 | + **Option 1: Enable at compile time** |
| 245 | + |
| 246 | + Set the U-Boot environment variable before compilation: |
| 247 | + |
| 248 | + .. code-block:: console |
| 249 | +
|
| 250 | + fastboot.unlock_ability=1 |
| 251 | +
|
| 252 | + **Option 2: Enable at runtime** |
| 253 | + |
| 254 | + Set the U-Boot environment variable in the console (note that ``saveenv`` is disabled in factory mode, so these settings will be lost at each reboot): |
| 255 | + |
| 256 | + .. code-block:: console |
| 257 | +
|
| 258 | + => setenv fastboot.unlock_ability 1 |
| 259 | +
|
| 260 | + **Unlock Process** |
| 261 | + |
| 262 | + When running ``fastboot flashing unlock`` or ``fastboot flashing unlock_critical``, the system will prompt for confirmation in the console. To bypass this confirmation prompt, you can set (these settings will also be lost at each reboot): |
| 263 | + |
| 264 | + .. code-block:: console |
| 265 | +
|
| 266 | + => setenv fastboot.unlock_confirm 1 |
| 267 | + => setenv fastboot.unlock_critical_confirm 1 |
0 commit comments