blob: e155b98cf65545b78ea383fa4422290df755ce57 [file] [log] [blame]
Svyatoslav Ryhelf8700e42023-02-14 19:35:38 +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
Svyatoslav Ryhelf8700e42023-02-14 19:35:38 +02007#include <command.h>
8#include <log.h>
9#include <asm/arch-tegra/crypto.h>
10#include "bct.h"
11#include "uboot_aes.h"
12
Svyatoslav Ryhel9a303462023-10-03 09:36:44 +030013/* Device with "sbk burned: false" will expose zero key */
14const u8 nosbk[AES128_KEY_LENGTH] = { 0 };
15
Svyatoslav Ryhelf8700e42023-02-14 19:35:38 +020016/*
17 * @param bct boot config table start in RAM
18 * @param ect bootloader start in RAM
19 * @param ebt_size bootloader file size in bytes
20 * Return: 0, or 1 if failed
21 */
22static int bct_patch(u8 *bct, u8 *ebt, u32 ebt_size)
23{
24 struct nvboot_config_table *bct_tbl = NULL;
25 u8 ebt_hash[AES128_KEY_LENGTH] = { 0 };
26 u8 sbk[AES128_KEY_LENGTH] = { 0 };
27 u8 *bct_hash = bct;
Svyatoslav Ryhel9a303462023-10-03 09:36:44 +030028 bool encrypted;
Svyatoslav Ryhelf8700e42023-02-14 19:35:38 +020029 int ret;
30
31 bct += BCT_HASH;
32
Svyatoslav Ryhel9a303462023-10-03 09:36:44 +030033 ebt_size = roundup(ebt_size, EBT_ALIGNMENT);
34
Svyatoslav Ryhelf8700e42023-02-14 19:35:38 +020035 memcpy(sbk, (u8 *)(bct + BCT_LENGTH),
36 NVBOOT_CMAC_AES_HASH_LENGTH * 4);
37
Svyatoslav Ryhel9a303462023-10-03 09:36:44 +030038 encrypted = memcmp(&sbk, &nosbk, AES128_KEY_LENGTH);
Svyatoslav Ryhelf8700e42023-02-14 19:35:38 +020039
Svyatoslav Ryhel9a303462023-10-03 09:36:44 +030040 if (encrypted) {
41 ret = decrypt_data_block(bct, BCT_LENGTH, sbk);
42 if (ret)
43 return 1;
Svyatoslav Ryhelf8700e42023-02-14 19:35:38 +020044
Svyatoslav Ryhel9a303462023-10-03 09:36:44 +030045 ret = encrypt_data_block(ebt, ebt_size, sbk);
46 if (ret)
47 return 1;
48 }
Svyatoslav Ryhelf8700e42023-02-14 19:35:38 +020049
50 ret = sign_enc_data_block(ebt, ebt_size, ebt_hash, sbk);
51 if (ret)
52 return 1;
53
54 bct_tbl = (struct nvboot_config_table *)bct;
55
56 memcpy((u8 *)&bct_tbl->bootloader[0].crypto_hash,
57 ebt_hash, NVBOOT_CMAC_AES_HASH_LENGTH * 4);
58 bct_tbl->bootloader[0].entry_point = CONFIG_SPL_TEXT_BASE;
59 bct_tbl->bootloader[0].load_addr = CONFIG_SPL_TEXT_BASE;
60 bct_tbl->bootloader[0].length = ebt_size;
61
Svyatoslav Ryhel9a303462023-10-03 09:36:44 +030062 if (encrypted) {
63 ret = encrypt_data_block(bct, BCT_LENGTH, sbk);
64 if (ret)
65 return 1;
66 }
Svyatoslav Ryhelf8700e42023-02-14 19:35:38 +020067
68 ret = sign_enc_data_block(bct, BCT_LENGTH, bct_hash, sbk);
69 if (ret)
70 return 1;
71
72 return 0;
73}
74
75static int do_ebtupdate(struct cmd_tbl *cmdtp, int flag, int argc,
76 char *const argv[])
77{
78 u32 bct_addr = hextoul(argv[1], NULL);
79 u32 ebt_addr = hextoul(argv[2], NULL);
80 u32 ebt_size = hextoul(argv[3], NULL);
81
82 return bct_patch((u8 *)bct_addr, (u8 *)ebt_addr, ebt_size);
83}
84
85U_BOOT_CMD(ebtupdate, 4, 0, do_ebtupdate,
86 "update bootloader on re-crypted Tegra20 devices",
87 ""
88);