Svyatoslav Ryhel | f8700e4 | 2023-02-14 19:35:38 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (c) 2022, Ramin <raminterex@yahoo.com> |
| 4 | * Copyright (c) 2022, Svyatoslav Ryhel <clamor95@gmail.com> |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <command.h> |
| 9 | #include <log.h> |
| 10 | #include <asm/arch-tegra/crypto.h> |
| 11 | #include "bct.h" |
| 12 | #include "uboot_aes.h" |
| 13 | |
| 14 | /* |
| 15 | * @param bct boot config table start in RAM |
| 16 | * @param ect bootloader start in RAM |
| 17 | * @param ebt_size bootloader file size in bytes |
| 18 | * Return: 0, or 1 if failed |
| 19 | */ |
| 20 | static int bct_patch(u8 *bct, u8 *ebt, u32 ebt_size) |
| 21 | { |
| 22 | struct nvboot_config_table *bct_tbl = NULL; |
| 23 | u8 ebt_hash[AES128_KEY_LENGTH] = { 0 }; |
| 24 | u8 sbk[AES128_KEY_LENGTH] = { 0 }; |
| 25 | u8 *bct_hash = bct; |
| 26 | int ret; |
| 27 | |
| 28 | bct += BCT_HASH; |
| 29 | |
| 30 | memcpy(sbk, (u8 *)(bct + BCT_LENGTH), |
| 31 | NVBOOT_CMAC_AES_HASH_LENGTH * 4); |
| 32 | |
| 33 | ret = decrypt_data_block(bct, BCT_LENGTH, sbk); |
| 34 | if (ret) |
| 35 | return 1; |
| 36 | |
| 37 | ebt_size = roundup(ebt_size, EBT_ALIGNMENT); |
| 38 | |
| 39 | ret = encrypt_data_block(ebt, ebt_size, sbk); |
| 40 | if (ret) |
| 41 | return 1; |
| 42 | |
| 43 | ret = sign_enc_data_block(ebt, ebt_size, ebt_hash, sbk); |
| 44 | if (ret) |
| 45 | return 1; |
| 46 | |
| 47 | bct_tbl = (struct nvboot_config_table *)bct; |
| 48 | |
| 49 | memcpy((u8 *)&bct_tbl->bootloader[0].crypto_hash, |
| 50 | ebt_hash, NVBOOT_CMAC_AES_HASH_LENGTH * 4); |
| 51 | bct_tbl->bootloader[0].entry_point = CONFIG_SPL_TEXT_BASE; |
| 52 | bct_tbl->bootloader[0].load_addr = CONFIG_SPL_TEXT_BASE; |
| 53 | bct_tbl->bootloader[0].length = ebt_size; |
| 54 | |
| 55 | ret = encrypt_data_block(bct, BCT_LENGTH, sbk); |
| 56 | if (ret) |
| 57 | return 1; |
| 58 | |
| 59 | ret = sign_enc_data_block(bct, BCT_LENGTH, bct_hash, sbk); |
| 60 | if (ret) |
| 61 | return 1; |
| 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
| 66 | static int do_ebtupdate(struct cmd_tbl *cmdtp, int flag, int argc, |
| 67 | char *const argv[]) |
| 68 | { |
| 69 | u32 bct_addr = hextoul(argv[1], NULL); |
| 70 | u32 ebt_addr = hextoul(argv[2], NULL); |
| 71 | u32 ebt_size = hextoul(argv[3], NULL); |
| 72 | |
| 73 | return bct_patch((u8 *)bct_addr, (u8 *)ebt_addr, ebt_size); |
| 74 | } |
| 75 | |
| 76 | U_BOOT_CMD(ebtupdate, 4, 0, do_ebtupdate, |
| 77 | "update bootloader on re-crypted Tegra20 devices", |
| 78 | "" |
| 79 | ); |