2023-02-14 17:35:38 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Ramin <raminterex@yahoo.com>
|
|
|
|
* Copyright (c) 2022, Svyatoslav Ryhel <clamor95@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
|
|
|
#include <log.h>
|
|
|
|
#include <asm/arch-tegra/crypto.h>
|
|
|
|
#include "bct.h"
|
|
|
|
#include "uboot_aes.h"
|
|
|
|
|
2023-10-03 06:36:44 +00:00
|
|
|
/* Device with "sbk burned: false" will expose zero key */
|
|
|
|
const u8 nosbk[AES128_KEY_LENGTH] = { 0 };
|
|
|
|
|
2023-02-14 17:35:38 +00:00
|
|
|
/*
|
|
|
|
* @param bct boot config table start in RAM
|
|
|
|
* @param ect bootloader start in RAM
|
|
|
|
* @param ebt_size bootloader file size in bytes
|
|
|
|
* Return: 0, or 1 if failed
|
|
|
|
*/
|
|
|
|
static int bct_patch(u8 *bct, u8 *ebt, u32 ebt_size)
|
|
|
|
{
|
|
|
|
struct nvboot_config_table *bct_tbl = NULL;
|
|
|
|
u8 ebt_hash[AES128_KEY_LENGTH] = { 0 };
|
|
|
|
u8 sbk[AES128_KEY_LENGTH] = { 0 };
|
|
|
|
u8 *bct_hash = bct;
|
2023-10-03 06:36:44 +00:00
|
|
|
bool encrypted;
|
2023-02-14 17:35:38 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
bct += BCT_HASH;
|
|
|
|
|
2023-10-03 06:36:44 +00:00
|
|
|
ebt_size = roundup(ebt_size, EBT_ALIGNMENT);
|
|
|
|
|
2023-02-14 17:35:38 +00:00
|
|
|
memcpy(sbk, (u8 *)(bct + BCT_LENGTH),
|
|
|
|
NVBOOT_CMAC_AES_HASH_LENGTH * 4);
|
|
|
|
|
2023-10-03 06:36:44 +00:00
|
|
|
encrypted = memcmp(&sbk, &nosbk, AES128_KEY_LENGTH);
|
2023-02-14 17:35:38 +00:00
|
|
|
|
2023-10-03 06:36:44 +00:00
|
|
|
if (encrypted) {
|
|
|
|
ret = decrypt_data_block(bct, BCT_LENGTH, sbk);
|
|
|
|
if (ret)
|
|
|
|
return 1;
|
2023-02-14 17:35:38 +00:00
|
|
|
|
2023-10-03 06:36:44 +00:00
|
|
|
ret = encrypt_data_block(ebt, ebt_size, sbk);
|
|
|
|
if (ret)
|
|
|
|
return 1;
|
|
|
|
}
|
2023-02-14 17:35:38 +00:00
|
|
|
|
|
|
|
ret = sign_enc_data_block(ebt, ebt_size, ebt_hash, sbk);
|
|
|
|
if (ret)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
bct_tbl = (struct nvboot_config_table *)bct;
|
|
|
|
|
|
|
|
memcpy((u8 *)&bct_tbl->bootloader[0].crypto_hash,
|
|
|
|
ebt_hash, NVBOOT_CMAC_AES_HASH_LENGTH * 4);
|
|
|
|
bct_tbl->bootloader[0].entry_point = CONFIG_SPL_TEXT_BASE;
|
|
|
|
bct_tbl->bootloader[0].load_addr = CONFIG_SPL_TEXT_BASE;
|
|
|
|
bct_tbl->bootloader[0].length = ebt_size;
|
|
|
|
|
2023-10-03 06:36:44 +00:00
|
|
|
if (encrypted) {
|
|
|
|
ret = encrypt_data_block(bct, BCT_LENGTH, sbk);
|
|
|
|
if (ret)
|
|
|
|
return 1;
|
|
|
|
}
|
2023-02-14 17:35:38 +00:00
|
|
|
|
|
|
|
ret = sign_enc_data_block(bct, BCT_LENGTH, bct_hash, sbk);
|
|
|
|
if (ret)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int do_ebtupdate(struct cmd_tbl *cmdtp, int flag, int argc,
|
|
|
|
char *const argv[])
|
|
|
|
{
|
|
|
|
u32 bct_addr = hextoul(argv[1], NULL);
|
|
|
|
u32 ebt_addr = hextoul(argv[2], NULL);
|
|
|
|
u32 ebt_size = hextoul(argv[3], NULL);
|
|
|
|
|
|
|
|
return bct_patch((u8 *)bct_addr, (u8 *)ebt_addr, ebt_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_BOOT_CMD(ebtupdate, 4, 0, do_ebtupdate,
|
|
|
|
"update bootloader on re-crypted Tegra20 devices",
|
|
|
|
""
|
|
|
|
);
|