blob: cff1a3e98d2746800114eef60ceaed504a7cbe3e [file] [log] [blame]
Ramin Khonsari327ff8e2023-02-14 19:35:37 +02001// 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
Svyatoslav Ryhelf2cf7fe2023-10-03 09:36:44 +030014/* Device with "sbk burned: false" will expose zero key */
15const u8 nosbk[AES128_KEY_LENGTH] = { 0 };
16
Ramin Khonsari327ff8e2023-02-14 19:35:37 +020017/*
18 * @param bct boot config table start in RAM
19 * @param ect bootloader start in RAM
20 * @param ebt_size bootloader file size in bytes
21 * Return: 0, or 1 if failed
22 */
23static int bct_patch(u8 *bct, u8 *ebt, u32 ebt_size)
24{
25 struct nvboot_config_table *bct_tbl = NULL;
26 u8 ebt_hash[AES128_KEY_LENGTH] = { 0 };
27 u8 sbk[AES128_KEY_LENGTH] = { 0 };
28 u8 *bct_hash = bct;
Svyatoslav Ryhelf2cf7fe2023-10-03 09:36:44 +030029 bool encrypted;
Ramin Khonsari327ff8e2023-02-14 19:35:37 +020030 int ret;
31
32 bct += BCT_HASH;
33
Svyatoslav Ryhelf2cf7fe2023-10-03 09:36:44 +030034 ebt_size = roundup(ebt_size, EBT_ALIGNMENT);
35
Ramin Khonsari327ff8e2023-02-14 19:35:37 +020036 memcpy(sbk, (u8 *)(bct + BCT_LENGTH),
37 NVBOOT_CMAC_AES_HASH_LENGTH * 4);
38
Svyatoslav Ryhelf2cf7fe2023-10-03 09:36:44 +030039 encrypted = memcmp(&sbk, &nosbk, AES128_KEY_LENGTH);
Ramin Khonsari327ff8e2023-02-14 19:35:37 +020040
Svyatoslav Ryhelf2cf7fe2023-10-03 09:36:44 +030041 if (encrypted) {
42 ret = decrypt_data_block(bct, BCT_LENGTH, sbk);
43 if (ret)
44 return 1;
Ramin Khonsari327ff8e2023-02-14 19:35:37 +020045
Svyatoslav Ryhelf2cf7fe2023-10-03 09:36:44 +030046 ret = encrypt_data_block(ebt, ebt_size, sbk);
47 if (ret)
48 return 1;
49 }
Ramin Khonsari327ff8e2023-02-14 19:35:37 +020050
51 ret = sign_enc_data_block(ebt, ebt_size, ebt_hash, sbk);
52 if (ret)
53 return 1;
54
55 bct_tbl = (struct nvboot_config_table *)bct;
56
57 memcpy((u8 *)&bct_tbl->bootloader[0].crypto_hash,
58 ebt_hash, NVBOOT_CMAC_AES_HASH_LENGTH * 4);
59 bct_tbl->bootloader[0].entry_point = CONFIG_SPL_TEXT_BASE;
60 bct_tbl->bootloader[0].load_addr = CONFIG_SPL_TEXT_BASE;
61 bct_tbl->bootloader[0].length = ebt_size;
62
Svyatoslav Ryhelf2cf7fe2023-10-03 09:36:44 +030063 if (encrypted) {
64 ret = encrypt_data_block(bct, BCT_LENGTH, sbk);
65 if (ret)
66 return 1;
67 }
Ramin Khonsari327ff8e2023-02-14 19:35:37 +020068
69 ret = sign_enc_data_block(bct, BCT_LENGTH, bct_hash, sbk);
70 if (ret)
71 return 1;
72
73 return 0;
74}
75
76static int do_ebtupdate(struct cmd_tbl *cmdtp, int flag, int argc,
77 char *const argv[])
78{
79 u32 bct_addr = hextoul(argv[1], NULL);
80 u32 ebt_addr = hextoul(argv[2], NULL);
81 u32 ebt_size = hextoul(argv[3], NULL);
82
83 return bct_patch((u8 *)bct_addr, (u8 *)ebt_addr, ebt_size);
84}
85
86U_BOOT_CMD(ebtupdate, 4, 0, do_ebtupdate,
87 "update bootloader on re-crypted Tegra30 devices",
88 ""
89);