All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Peng Fan (OSS)" <peng.fan@oss.nxp.com>
To: sbabic@denx.de, festevam@gmail.com,
	"NXP i.MX U-Boot Team" <uboot-imx@nxp.com>
Cc: u-boot@lists.denx.de, Ye Li <ye.li@nxp.com>
Subject: [PATCH V2 25/49] imx: imx9: Add AHAB boot support
Date: Mon, 27 Jun 2022 11:24:31 +0800	[thread overview]
Message-ID: <20220627032455.28280-26-peng.fan@oss.nxp.com> (raw)
In-Reply-To: <20220627032455.28280-1-peng.fan@oss.nxp.com>

From: Ye Li <ye.li@nxp.com>

Add AHAB driver for iMX9 to do authentication by calling sentinel API

Signed-off-by: Ye Li <ye.li@nxp.com>
Signed-off-by: Peng Fan <peng.fan@nxp.com>
---
 arch/arm/mach-imx/imx9/Kconfig  |   5 +
 arch/arm/mach-imx/imx9/Makefile |   1 +
 arch/arm/mach-imx/imx9/ahab.c   | 346 ++++++++++++++++++++++++++++++++
 3 files changed, 352 insertions(+)
 create mode 100644 arch/arm/mach-imx/imx9/ahab.c

diff --git a/arch/arm/mach-imx/imx9/Kconfig b/arch/arm/mach-imx/imx9/Kconfig
index ce58e41428f..dae9f658e65 100644
--- a/arch/arm/mach-imx/imx9/Kconfig
+++ b/arch/arm/mach-imx/imx9/Kconfig
@@ -1,5 +1,10 @@
 if ARCH_IMX9
 
+config AHAB_BOOT
+    bool "Support i.MX9 AHAB features"
+    help
+    This option enables the support for AHAB secure boot.
+
 config IMX9
 	bool
 	select HAS_CAAM
diff --git a/arch/arm/mach-imx/imx9/Makefile b/arch/arm/mach-imx/imx9/Makefile
index 0124212f266..41a22500c95 100644
--- a/arch/arm/mach-imx/imx9/Makefile
+++ b/arch/arm/mach-imx/imx9/Makefile
@@ -4,3 +4,4 @@
 
 obj-y += lowlevel_init.o
 obj-y += soc.o clock.o clock_root.o trdc.o
+obj-$(CONFIG_AHAB_BOOT) += ahab.o
diff --git a/arch/arm/mach-imx/imx9/ahab.c b/arch/arm/mach-imx/imx9/ahab.c
new file mode 100644
index 00000000000..6aa949619b5
--- /dev/null
+++ b/arch/arm/mach-imx/imx9/ahab.c
@@ -0,0 +1,346 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright 2022 NXP
+ */
+
+#include <common.h>
+#include <command.h>
+#include <errno.h>
+#include <asm/io.h>
+#include <asm/mach-imx/s400_api.h>
+#include <asm/mach-imx/sys_proto.h>
+#include <asm/arch-imx/cpu.h>
+#include <asm/arch/sys_proto.h>
+#include <asm/mach-imx/image.h>
+#include <console.h>
+#include <cpu_func.h>
+#include <asm/mach-imx/ahab.h>
+#include <asm/global_data.h>
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#define IMG_CONTAINER_BASE             (0x80000000UL)
+#define IMG_CONTAINER_END_BASE         (IMG_CONTAINER_BASE + 0xFFFFUL)
+
+#define AHAB_NO_AUTHENTICATION_IND 0xee
+#define AHAB_BAD_KEY_HASH_IND 0xfa
+#define AHAB_INVALID_KEY_IND 0xf9
+#define AHAB_BAD_SIGNATURE_IND 0xf0
+#define AHAB_BAD_HASH_IND 0xf1
+
+static void display_ahab_auth_ind(u32 event)
+{
+	u8 resp_ind = (event >> 8) & 0xff;
+
+	switch (resp_ind) {
+	case AHAB_NO_AUTHENTICATION_IND:
+		printf("AHAB_NO_AUTHENTICATION_IND (0x%02X)\n\n", resp_ind);
+		break;
+	case AHAB_BAD_KEY_HASH_IND:
+		printf("AHAB_BAD_KEY_HASH_IND (0x%02X)\n\n", resp_ind);
+		break;
+	case AHAB_INVALID_KEY_IND:
+		printf("AHAB_INVALID_KEY_IND (0x%02X)\n\n", resp_ind);
+		break;
+	case AHAB_BAD_SIGNATURE_IND:
+		printf("AHAB_BAD_SIGNATURE_IND (0x%02X)\n\n", resp_ind);
+		break;
+	case AHAB_BAD_HASH_IND:
+		printf("AHAB_BAD_HASH_IND (0x%02X)\n\n", resp_ind);
+		break;
+	default:
+		printf("Unknown Indicator (0x%02X)\n\n", resp_ind);
+		break;
+	}
+}
+
+int ahab_auth_cntr_hdr(struct container_hdr *container, u16 length)
+{
+	int err;
+	u32 resp;
+
+	memcpy((void *)IMG_CONTAINER_BASE, (const void *)container,
+	       ALIGN(length, CONFIG_SYS_CACHELINE_SIZE));
+
+	flush_dcache_range(IMG_CONTAINER_BASE,
+			   IMG_CONTAINER_BASE + ALIGN(length, CONFIG_SYS_CACHELINE_SIZE) - 1);
+
+	err = ahab_auth_oem_ctnr(IMG_CONTAINER_BASE, &resp);
+	if (err) {
+		printf("Authenticate container hdr failed, return %d, resp 0x%x\n",
+		       err, resp);
+		display_ahab_auth_ind(resp);
+	}
+
+	return err;
+}
+
+int ahab_auth_release(void)
+{
+	int err;
+	u32 resp;
+
+	err = ahab_release_container(&resp);
+	if (err) {
+		printf("Error: release container failed, resp 0x%x!\n", resp);
+		display_ahab_auth_ind(resp);
+	}
+
+	return err;
+}
+
+int ahab_verify_cntr_image(struct boot_img_t *img, int image_index)
+{
+	int err;
+	u32 resp;
+
+	err = ahab_verify_image(image_index, &resp);
+	if (err) {
+		printf("Authenticate img %d failed, return %d, resp 0x%x\n",
+		       image_index, err, resp);
+		display_ahab_auth_ind(resp);
+
+		return -EIO;
+	}
+
+	return 0;
+}
+
+static inline bool check_in_dram(ulong addr)
+{
+	int i;
+	struct bd_info *bd = gd->bd;
+
+	for (i = 0; i < CONFIG_NR_DRAM_BANKS; ++i) {
+		if (bd->bi_dram[i].size) {
+			if (addr >= bd->bi_dram[i].start &&
+			    addr < (bd->bi_dram[i].start + bd->bi_dram[i].size))
+				return true;
+		}
+	}
+
+	return false;
+}
+
+int authenticate_os_container(ulong addr)
+{
+	struct container_hdr *phdr;
+	int i, ret = 0;
+	int err;
+	u16 length;
+	struct boot_img_t *img;
+	unsigned long s, e;
+
+	if (addr % 4) {
+		puts("Error: Image's address is not 4 byte aligned\n");
+		return -EINVAL;
+	}
+
+	if (!check_in_dram(addr)) {
+		puts("Error: Image's address is invalid\n");
+		return -EINVAL;
+	}
+
+	phdr = (struct container_hdr *)addr;
+	if (phdr->tag != 0x87 || phdr->version != 0x0) {
+		printf("Error: Wrong container header\n");
+		return -EFAULT;
+	}
+
+	if (!phdr->num_images) {
+		printf("Error: Wrong container, no image found\n");
+		return -EFAULT;
+	}
+
+	length = phdr->length_lsb + (phdr->length_msb << 8);
+
+	debug("container length %u\n", length);
+
+	err = ahab_auth_cntr_hdr(phdr, length);
+	if (err) {
+		ret = -EIO;
+		goto exit;
+	}
+
+	debug("Verify images\n");
+
+	/* Copy images to dest address */
+	for (i = 0; i < phdr->num_images; i++) {
+		img = (struct boot_img_t *)(addr +
+					    sizeof(struct container_hdr) +
+					    i * sizeof(struct boot_img_t));
+
+		debug("img %d, dst 0x%x, src 0x%lx, size 0x%x\n",
+		      i, (uint32_t)img->dst, img->offset + addr, img->size);
+
+		memcpy((void *)img->dst, (const void *)(img->offset + addr),
+		       img->size);
+
+		s = img->dst & ~(CONFIG_SYS_CACHELINE_SIZE - 1);
+		e = ALIGN(img->dst + img->size, CONFIG_SYS_CACHELINE_SIZE) - 1;
+
+		flush_dcache_range(s, e);
+
+		ret = ahab_verify_cntr_image(img, i);
+		if (ret)
+			goto exit;
+	}
+
+exit:
+	debug("ahab_auth_release, 0x%x\n", ret);
+	ahab_auth_release();
+
+	return ret;
+}
+
+static int do_authenticate(struct cmd_tbl *cmdtp, int flag, int argc,
+			   char *const argv[])
+{
+	ulong addr;
+
+	if (argc < 2)
+		return CMD_RET_USAGE;
+
+	addr = simple_strtoul(argv[1], NULL, 16);
+
+	printf("Authenticate OS container at 0x%lx\n", addr);
+
+	if (authenticate_os_container(addr))
+		return CMD_RET_FAILURE;
+
+	return CMD_RET_SUCCESS;
+}
+
+static void display_life_cycle(u32 lc)
+{
+	printf("Lifecycle: 0x%08X, ", lc);
+	switch (lc) {
+	case 0x1:
+		printf("BLANK\n\n");
+		break;
+	case 0x2:
+		printf("FAB\n\n");
+		break;
+	case 0x4:
+		printf("NXP Provisioned\n\n");
+		break;
+	case 0x8:
+		printf("OEM Open\n\n");
+		break;
+	case 0x10:
+		printf("OEM Secure World Closed\n\n");
+		break;
+	case 0x20:
+		printf("OEM closed\n\n");
+		break;
+	case 0x40:
+		printf("Field Return OEM\n\n");
+		break;
+	case 0x80:
+		printf("Field Return NXP\n\n");
+		break;
+	case 0x100:
+		printf("OEM Locked\n\n");
+		break;
+	case 0x200:
+		printf("BRICKED\n\n");
+		break;
+	default:
+		printf("Unknown\n\n");
+		break;
+	}
+}
+
+static int confirm_close(void)
+{
+	puts("Warning: Please ensure your sample is in NXP closed state, "
+	     "OEM SRK hash has been fused, \n"
+	     "         and you are able to boot a signed image successfully "
+	     "without any SECO events reported.\n"
+	     "         If not, your sample will be unrecoverable.\n"
+	     "\nReally perform this operation? <y/N>\n");
+
+	if (confirm_yesno())
+		return 1;
+
+	puts("Ahab close aborted\n");
+	return 0;
+}
+
+static int do_ahab_close(struct cmd_tbl *cmdtp, int flag, int argc,
+			 char *const argv[])
+{
+	int err;
+	u32 resp;
+
+	if (!confirm_close())
+		return -EACCES;
+
+	err = ahab_forward_lifecycle(8, &resp);
+	if (err != 0) {
+		printf("Error in forward lifecycle to OEM closed\n");
+		return -EIO;
+	}
+
+	printf("Change to OEM closed successfully\n");
+
+	return 0;
+}
+
+int ahab_dump(void)
+{
+	u32 buffer[32];
+	int ret, i = 0;
+
+	do {
+		ret = ahab_dump_buffer(buffer, 32);
+		if (ret < 0) {
+			printf("Error in dump AHAB log\n");
+			return -EIO;
+		}
+
+		if (ret == 1)
+			break;
+		for (i = 0; i < ret; i++)
+			printf("0x%x\n", buffer[i]);
+	} while (ret >= 21);
+
+	return 0;
+}
+
+static int do_ahab_dump(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+	return ahab_dump();
+}
+
+static int do_ahab_status(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
+{
+	u32 lc;
+
+	lc = readl(FSB_BASE_ADDR + 0x41c);
+	lc &= 0x3ff;
+
+	display_life_cycle(lc);
+	return 0;
+}
+
+U_BOOT_CMD(auth_cntr, CONFIG_SYS_MAXARGS, 1, do_authenticate,
+	   "autenticate OS container via AHAB",
+	   "addr\n"
+	   "addr - OS container hex address\n"
+);
+
+U_BOOT_CMD(ahab_close, CONFIG_SYS_MAXARGS, 1, do_ahab_close,
+	   "Change AHAB lifecycle to OEM closed",
+	   ""
+);
+
+U_BOOT_CMD(ahab_dump, CONFIG_SYS_MAXARGS, 1, do_ahab_dump,
+	   "Dump AHAB log for debug",
+	   ""
+);
+
+U_BOOT_CMD(ahab_status, CONFIG_SYS_MAXARGS, 1, do_ahab_status,
+	   "display AHAB lifecycle only",
+	   ""
+);
-- 
2.36.0


  parent reply	other threads:[~2022-06-27  2:46 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-27  3:24 [PATCH V2 00/49] imx: support i.MX93 Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 01/49] spl: imx8mm: enlarge SPL_MAX_SIZE Peng Fan (OSS)
2022-06-29 10:09   ` Frieder Schrempf
2022-07-05  1:35     ` Peng Fan
2022-07-05  2:08       ` Peng Fan
2022-07-05  6:40         ` Frieder Schrempf
2022-06-27  3:24 ` [PATCH V2 02/49] arm: makefile: cleanup mach-imx usage Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 03/49] imx: Change USB boot device type Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 04/49] imx: spl: Allow iMX7/8/8M to overwrite spl_board_boot_device Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 05/49] imx: simplify dependency with SPL_BOOTROM_SUPPORT Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 06/49] imx: move get_boot_device to common header Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 07/49] imx: move get_boot_device to common file Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 08/49] imx: add USB2_BOOT type Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 09/49] imx: add basic i.MX9 support Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 10/49] fsl_lpuart: add " Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 11/49] gpio: pca953x: support pcal6524 Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 12/49] imx: pinctrl: add pinctrl and pinfunc file for i.MX93 Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 13/49] imx: imx9: Add CCM and clock API support Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 14/49] mmc: fsl_esdhc_imx: Support i.MX9 Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 15/49] spl: Use SPL_FIT_IMAGE_TINY for iMX9 Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 16/49] imx: imx9: Add function to initialize timer Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 17/49] imx: imx9: disable watchdog Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 18/49] imx: imx9: support romapi Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 19/49] misc: imx: S400_API: Move S400 MU and API to a common place Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 20/49] misc: s4mu: Support iMX93 with Sentinel MU Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 21/49] misc: S400_API: Update release RDC API Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 22/49] misc: S400_API: New API for FW status and chip info Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 23/49] misc: s400_api: introduce ahab_release_m33_trout Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 24/49] imx: imx9: Add TRDC driver for TRDC init Peng Fan (OSS)
2022-06-27  3:24 ` Peng Fan (OSS) [this message]
2022-06-27  3:24 ` [PATCH V2 26/49] imx: imx9: Get the chip revision through S400 API Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 27/49] misc: S400_API: Rename imx8ulp_s400_msg to sentinel_msg Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 28/49] misc: imx8ulp: move fuse.c from imx8ulp to sentinel Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 29/49] misc: fuse: support to access fuse on i.MX93 Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 30/49] misc: fuse: update the code for accessing fuse of i.MX93 Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 31/49] imx: imx9: Add gpio registers structure Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 32/49] imx: imx9: Add MIX power init Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 33/49] imx: imx9: Add M33 release prepare function Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 34/49] imx: imx9: Support booting m33 from Acore Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 35/49] imx: imx9: Support multiple env storages at runtime Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 36/49] imx: imx9: clock: Add DDR clock support Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 37/49] ddr: imx: Add i.MX9 DDR controller driver Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 38/49] ddr: imx9: enable Performance monitor counter Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 39/49] arm: dts: Add i.MX93 SoC DTSi file Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 40/49] imx: imx93_evk: Add basic board support Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 41/49] imx: imx93_evk: Set ARM clock to 1.7Ghz Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 42/49] net: fec_mxc: support i.MX93 Peng Fan (OSS)
2022-07-03 19:13   ` Ramon Fried
2022-06-27  3:24 ` [PATCH V2 43/49] net: dwc_eth_qos: fix build break when CLK not enabled Peng Fan (OSS)
2022-07-03 19:21   ` Ramon Fried
2022-06-27  3:24 ` [PATCH V2 44/49] net: dwc_eth_qos: public some functions Peng Fan (OSS)
2022-07-03 19:14   ` Ramon Fried
2022-06-27  3:24 ` [PATCH V2 45/49] net: dwc_eth_qos: move i.MX code out Peng Fan (OSS)
2022-07-03 19:15   ` Ramon Fried
2022-06-27  3:24 ` [PATCH V2 46/49] net: eqos: add function to get phy node and address Peng Fan (OSS)
2022-07-03 19:17   ` Ramon Fried
2022-06-27  3:24 ` [PATCH V2 47/49] net: dwc_eth_qos: intrdouce eqos hook eqos_get_enetaddr Peng Fan (OSS)
2022-07-03 19:19   ` Ramon Fried
2022-06-27  3:24 ` [PATCH V2 48/49] board: freescale: imx93_evk: support ethernet Peng Fan (OSS)
2022-06-27  3:24 ` [PATCH V2 49/49] tools: image: support i.MX93 Peng Fan (OSS)
2022-06-27  6:45 ` [PATCH V2 00/49] imx: " Peng Fan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220627032455.28280-26-peng.fan@oss.nxp.com \
    --to=peng.fan@oss.nxp.com \
    --cc=festevam@gmail.com \
    --cc=sbabic@denx.de \
    --cc=u-boot@lists.denx.de \
    --cc=uboot-imx@nxp.com \
    --cc=ye.li@nxp.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.