This adds support for authenticating an image against the K3 ROM API. Also included in this patch is a new command for testing the authentication. Signed-off-by: Sascha Hauer <s.hauer@xxxxxxxxxxxxxx> --- arch/arm/mach-k3/Kconfig | 7 ++++ arch/arm/mach-k3/common.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++ include/mach/k3/common.h | 1 + 3 files changed, 107 insertions(+) diff --git a/arch/arm/mach-k3/Kconfig b/arch/arm/mach-k3/Kconfig index 561ad1dac4..37d5155577 100644 --- a/arch/arm/mach-k3/Kconfig +++ b/arch/arm/mach-k3/Kconfig @@ -37,4 +37,11 @@ config MACH_BEAGLEPLAY help Say Y here if you are using a TI AM62x based BeaglePlay board +config ARCH_K3_COMMAND_AUTHENTICATE + bool "k3_authenticate_image command" + depends on COMMAND_SUPPORT + help + Add k3_authenticate_image command to test authenticating images + against the K3 ROM API. + endmenu diff --git a/arch/arm/mach-k3/common.c b/arch/arm/mach-k3/common.c index 8dc9d3bc9e..a324e2d5f6 100644 --- a/arch/arm/mach-k3/common.c +++ b/arch/arm/mach-k3/common.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-only #include <of.h> #include <io.h> +#include <dma.h> #include <deep-probe.h> #include <init.h> #include <envfs.h> @@ -8,11 +9,15 @@ #include <stdio.h> #include <xfuncs.h> #include <malloc.h> +#include <command.h> +#include <getopt.h> +#include <libfile.h> #include <pm_domain.h> #include <bootsource.h> #include <linux/bits.h> #include <linux/bitfield.h> #include <mach/k3/common.h> +#include <soc/ti/ti_sci_protocol.h> static const struct of_device_id k3_of_match[] = { { @@ -334,3 +339,97 @@ static int omap_env_init(void) return 0; } late_initcall(omap_env_init); + +int k3_authenticate_image(void **buf, size_t *size) +{ + const struct ti_sci_handle *ti_sci; + u64 image_addr; + int ret; + unsigned int s = *size; + + ti_sci = ti_sci_get_handle(NULL); + if (IS_ERR(ti_sci)) + return -EINVAL; + + image_addr = dma_map_single(NULL, *buf, *size, DMA_BIDIRECTIONAL); + + ret = ti_sci->ops.proc_ops.proc_auth_boot_image(ti_sci, &image_addr, &s); + + dma_unmap_single(NULL, image_addr, *size, DMA_BIDIRECTIONAL); + + if (ret) + *size = 0; + else + *size = s; + + return ret; +} + +#ifdef CONFIG_ARCH_K3_COMMAND_AUTHENTICATE +static int do_k3_authenticate_image(int argc, char *argv[]) +{ + void *buf; + size_t size; + int ret; + int opt; + const char *outfile = NULL; + const char *filename; + + while ((opt = getopt(argc, argv, "o:")) > 0) { + switch (opt) { + case 'o': + outfile = optarg; + break; + } + } + + argc -= optind; + + if (argc == 0) + return COMMAND_ERROR_USAGE; + + filename = argv[0]; + + ret = read_file_2(filename, &size, &buf, FILESIZE_MAX); + if (ret) + return ret; + + ret = k3_authenticate_image(&buf, &size); + if (ret) { + printf("authenticating %s failed: %pe\n", filename, ERR_PTR(ret)); + ret = COMMAND_ERROR; + goto err; + } + + printf("%s successfully authenticated\n", filename); + + if (outfile) { + ret = write_file(outfile, buf, size); + if (ret) { + printf("Failed to write output file: %pe\n", ERR_PTR(ret)); + goto err; + } + } + + ret = 0; +err: + free(buf); + + return ret; +} + +BAREBOX_CMD_HELP_START(k3_authenticate_image) +BAREBOX_CMD_HELP_TEXT("Authenticate image with K3 ROM API") +BAREBOX_CMD_HELP_TEXT("") +BAREBOX_CMD_HELP_TEXT("Options:") +BAREBOX_CMD_HELP_OPT ("-o <out>", "store unpacked authenticated image in <out>") +BAREBOX_CMD_HELP_END + +BAREBOX_CMD_START(k3_authenticate_image) + .cmd = do_k3_authenticate_image, + BAREBOX_CMD_DESC("authenticate file with K3 ROM API") + BAREBOX_CMD_OPTS("[-o <out>] file") + BAREBOX_CMD_GROUP(CMD_GRP_MISC) + BAREBOX_CMD_HELP(k3_authenticate_image) +BAREBOX_CMD_END +#endif diff --git a/include/mach/k3/common.h b/include/mach/k3/common.h index 871e9f39e3..94c5fba19d 100644 --- a/include/mach/k3/common.h +++ b/include/mach/k3/common.h @@ -13,6 +13,7 @@ bool k3_boot_is_emmc(void); u64 am625_sdram_size(void); void am625_register_dram(void); void am625_enable_32k_crystal(void); +int k3_authenticate_image(void **buf, size_t *size); #define K3_EMMC_BOOTPART_TIBOOT3_BIN_SIZE SZ_1M -- 2.39.5