From 09f2a38fdf685c8e732d31391a64cd2ee02a96f4 Mon Sep 17 00:00:00 2001 From: Joel Stanley Date: Thu, 14 Jul 2022 15:25:47 +0930 Subject: [PATCH 1/6] cmd/otp: Use any MOD_EXP driver Instead of requesting the ACRY driver specifically, ask for the first DM device that implements MOD_EXP. Selecting RSA ensures that one of the MOD_EXP drivers will be built in. On Aspeed platforms this will be either the ACRY or the software implementation; Kconfig logic stops both from being built in. Signed-off-by: Joel Stanley --- cmd/Kconfig | 1 - cmd/otp.c | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/cmd/Kconfig b/cmd/Kconfig index 1df26de5ed23..ff90a5d99acc 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -622,7 +622,6 @@ config CMD_OTP select SHA384 select SHA256 select RSA - select ASPEED_ACRY default y config CMD_RNG diff --git a/cmd/otp.c b/cmd/otp.c index 4e901fbff996..049c217d6048 100644 --- a/cmd/otp.c +++ b/cmd/otp.c @@ -2642,9 +2642,9 @@ static int otp_verify_boot_image(phys_addr_t addr) int i; int pass = 0; - ret = uclass_get_device_by_driver(UCLASS_MOD_EXP, DM_GET_DRIVER(aspeed_acry), &mod_exp_dev); + ret = uclass_get_device(UCLASS_MOD_EXP, 0, &mod_exp_dev); if (ret) { - printf("RSA engine: Can't find aspeed_acry\n"); + printf("RSA: Can't find RSA driver\n"); return OTP_FAILURE; } From b16076b164ffd89b1c0b2aecffb9c809fdd9253b Mon Sep 17 00:00:00 2001 From: Joel Stanley Date: Thu, 14 Jul 2022 15:54:21 +0930 Subject: [PATCH 2/6] cmd/otp: Depend on SHA variants Indicate which SHA algorithms are required by depending on them. Don't select them, as this force enables the options. Signed-off-by: Joel Stanley --- cmd/Kconfig | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/cmd/Kconfig b/cmd/Kconfig index ff90a5d99acc..8b90f0f23406 100644 --- a/cmd/Kconfig +++ b/cmd/Kconfig @@ -616,11 +616,9 @@ menu "Device access commands" config CMD_OTP depends on ASPEED_AST2600 + depends on SHA256 && SHA384 && SHA512 bool "ASPEED otp program" - select SHA512_ALGO - select SHA512 - select SHA384 - select SHA256 + select HASH select RSA default y From 948e73aa965b47af8871c76d05e0465f5d953d12 Mon Sep 17 00:00:00 2001 From: Joel Stanley Date: Thu, 14 Jul 2022 17:40:54 +0930 Subject: [PATCH 3/6] cmd/otp: Use hashing API Instead of calling the hashing functions directly, which uses the software implementation of the algorithms, use the hash API which allows the use of the HACE driver. Saves 12.5KB: Before=279327, After=266547, chg -4.58% Signed-off-by: Joel Stanley --- cmd/otp.c | 49 +++++++++++++++++-------------------------------- 1 file changed, 17 insertions(+), 32 deletions(-) diff --git a/cmd/otp.c b/cmd/otp.c index 049c217d6048..add70c841405 100644 --- a/cmd/otp.c +++ b/cmd/otp.c @@ -305,33 +305,6 @@ static int get_rid_num(u32 *rid) return rid_num; } -static void sb_sha256(u8 *src, u32 len, u8 *digest_ret) -{ - sha256_context ctx; - - sha256_starts(&ctx); - sha256_update(&ctx, src, len); - sha256_finish(&ctx, digest_ret); -} - -static void sb_sha384(u8 *src, u32 len, u8 *digest_ret) -{ - sha512_context ctx; - - sha384_starts(&ctx); - sha384_update(&ctx, src, len); - sha384_finish(&ctx, digest_ret); -} - -static void sb_sha512(u8 *src, u32 len, u8 *digest_ret) -{ - sha512_context ctx; - - sha512_starts(&ctx); - sha512_update(&ctx, src, len); - sha512_finish(&ctx, digest_ret); -} - static u32 chip_version(void) { u32 revid0, revid1; @@ -1912,6 +1885,18 @@ static int otp_check_scu_image(struct otp_image_layout *image_layout, u32 *scu_p return OTP_SUCCESS; } +static void do_hash(const void *data, int data_len, const char *algo_name, uint8_t *value) +{ + struct hash_algo *algo; + + if (hash_lookup_algo(algo_name, &algo)) { + debug("Unsupported hash alogrithm\n"); + return; + } + + algo->hash_func_ws(data, data_len, value, algo->chunk_size); +} + static int otp_verify_image(u8 *src_buf, u32 length, u8 *digest_buf, int version) { u8 digest_ret[48]; @@ -1919,11 +1904,11 @@ static int otp_verify_image(u8 *src_buf, u32 length, u8 *digest_buf, int version switch (version) { case 1: - sb_sha256(src_buf, length, digest_ret); + do_hash(src_buf, length, "sha256", digest_ret); digest_len = 32; break; case 2: - sb_sha384(src_buf, length, digest_ret); + do_hash(src_buf, length, "sha384", digest_ret); digest_len = 48; break; default: @@ -2549,13 +2534,13 @@ static int sb_sha(struct sb_info *si, u8 *sec_image, u32 sign_image_size, u8 *di printf("otp verify does not support SHA224\n"); return OTP_FAILURE; case 256: - sb_sha256(sec_image, sign_image_size, digest_ret); + do_hash(sec_image, sign_image_size, "sha256", digest_ret); break; case 384: - sb_sha384(sec_image, sign_image_size, digest_ret); + do_hash(sec_image, sign_image_size, "sha384", digest_ret); break; case 512: - sb_sha512(sec_image, sign_image_size, digest_ret); + do_hash(sec_image, sign_image_size, "sha512", digest_ret); break; default: printf("SHA Algorithm is invalid\n"); From 22fda007d30eeea87a8190406c0f97eee45293ae Mon Sep 17 00:00:00 2001 From: Joel Stanley Date: Thu, 14 Jul 2022 16:51:00 +0930 Subject: [PATCH 4/6] cmd/otp: Reduce size of otpkey Don't store more information than is used. Saves 3KB. Function old new delta _otp_print_key 540 580 +40 otp_verify_boot_image 1276 1268 -8 a2_key_type 640 40 -600 a1_key_type 640 40 -600 a3_key_type 1024 64 -960 a0_key_type 1024 64 -960 Total: Before=266547, After=263459, chg -1.16% Signed-off-by: Joel Stanley --- cmd/otp.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/cmd/otp.c b/cmd/otp.c index add70c841405..2df410dfd024 100644 --- a/cmd/otp.c +++ b/cmd/otp.c @@ -133,11 +133,11 @@ struct otpstrap_status { }; struct otpkey_type { - int value; - int key_type; - int order; - int need_id; - char information[110]; + int value: 4; + int key_type: 4; + int order: 1; + int need_id: 1; + char *information; }; struct otp_pro_sts { From 982f6418713d6f12a21004e25e02f2800ba21eb3 Mon Sep 17 00:00:00 2001 From: Joel Stanley Date: Thu, 14 Jul 2022 16:09:05 +0930 Subject: [PATCH 5/6] ARM: dts: ast2600: Undo I2C hacks The device tree matches Linux, and shouldn't be changed. Signed-off-by: Joel Stanley --- arch/arm/dts/ast2600.dtsi | 32 ++++++++++++++++---------------- drivers/i2c/ast2600_i2c.c | 2 +- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/arch/arm/dts/ast2600.dtsi b/arch/arm/dts/ast2600.dtsi index 1c9ce4aa7606..0c091f0095e9 100644 --- a/arch/arm/dts/ast2600.dtsi +++ b/arch/arm/dts/ast2600.dtsi @@ -796,7 +796,7 @@ #interrupt-cells = <1>; reg = <0x80 0x80 0xC00 0x20>; - compatible = "aspeed,ast2600-i2c"; + compatible = "aspeed,ast2600-i2c-bus"; bus-frequency = <100000>; interrupts = ; clocks = <&scu ASPEED_CLK_APB2>; @@ -809,7 +809,7 @@ #interrupt-cells = <1>; reg = <0x100 0x80 0xC20 0x20>; - compatible = "aspeed,ast2600-i2c"; + compatible = "aspeed,ast2600-i2c-bus"; bus-frequency = <100000>; interrupts = ; clocks = <&scu ASPEED_CLK_APB2>; @@ -822,7 +822,7 @@ #interrupt-cells = <1>; reg = <0x180 0x80 0xC40 0x20>; - compatible = "aspeed,ast2600-i2c"; + compatible = "aspeed,ast2600-i2c-bus"; bus-frequency = <100000>; interrupts = ; clocks = <&scu ASPEED_CLK_APB2>; @@ -834,7 +834,7 @@ #interrupt-cells = <1>; reg = <0x200 0x40 0xC60 0x20>; - compatible = "aspeed,ast2600-i2c"; + compatible = "aspeed,ast2600-i2c-bus"; bus-frequency = <100000>; interrupts = ; clocks = <&scu ASPEED_CLK_APB2>; @@ -846,7 +846,7 @@ #interrupt-cells = <1>; reg = <0x280 0x80 0xC80 0x20>; - compatible = "aspeed,ast2600-i2c"; + compatible = "aspeed,ast2600-i2c-bus"; bus-frequency = <100000>; interrupts = ; clocks = <&scu ASPEED_CLK_APB2>; @@ -858,7 +858,7 @@ #interrupt-cells = <1>; reg = <0x300 0x40 0xCA0 0x20>; - compatible = "aspeed,ast2600-i2c"; + compatible = "aspeed,ast2600-i2c-bus"; bus-frequency = <100000>; interrupts = ; clocks = <&scu ASPEED_CLK_APB2>; @@ -870,7 +870,7 @@ #interrupt-cells = <1>; reg = <0x380 0x80 0xCC0 0x20>; - compatible = "aspeed,ast2600-i2c"; + compatible = "aspeed,ast2600-i2c-bus"; bus-frequency = <100000>; interrupts = ; clocks = <&scu ASPEED_CLK_APB2>; @@ -882,7 +882,7 @@ #interrupt-cells = <1>; reg = <0x400 0x80 0xCE0 0x20>; - compatible = "aspeed,ast2600-i2c"; + compatible = "aspeed,ast2600-i2c-bus"; bus-frequency = <100000>; interrupts = ; clocks = <&scu ASPEED_CLK_APB2>; @@ -894,7 +894,7 @@ #interrupt-cells = <1>; reg = <0x480 0x80 0xD00 0x20>; - compatible = "aspeed,ast2600-i2c"; + compatible = "aspeed,ast2600-i2c-bus"; bus-frequency = <100000>; interrupts = ; clocks = <&scu ASPEED_CLK_APB2>; @@ -906,7 +906,7 @@ #interrupt-cells = <1>; reg = <0x500 0x80 0xD20 0x20>; - compatible = "aspeed,ast2600-i2c"; + compatible = "aspeed,ast2600-i2c-bus"; bus-frequency = <100000>; interrupts = ; clocks = <&scu ASPEED_CLK_APB2>; @@ -919,7 +919,7 @@ #interrupt-cells = <1>; reg = <0x580 0x80 0xD40 0x20>; - compatible = "aspeed,ast2600-i2c"; + compatible = "aspeed,ast2600-i2c-bus"; bus-frequency = <100000>; interrupts = ; clocks = <&scu ASPEED_CLK_APB2>; @@ -932,7 +932,7 @@ #interrupt-cells = <1>; reg = <0x600 0x80 0xD60 0x20>; - compatible = "aspeed,ast2600-i2c"; + compatible = "aspeed,ast2600-i2c-bus"; bus-frequency = <100000>; interrupts = ; clocks = <&scu ASPEED_CLK_APB2>; @@ -945,7 +945,7 @@ #interrupt-cells = <1>; reg = <0x680 0x80 0xD80 0x20>; - compatible = "aspeed,ast2600-i2c"; + compatible = "aspeed,ast2600-i2c-bus"; bus-frequency = <100000>; interrupts = ; clocks = <&scu ASPEED_CLK_APB2>; @@ -958,7 +958,7 @@ #interrupt-cells = <1>; reg = <0x700 0x80 0xDA0 0x20>; - compatible = "aspeed,ast2600-i2c"; + compatible = "aspeed,ast2600-i2c-bus"; bus-frequency = <100000>; interrupts = ; clocks = <&scu ASPEED_CLK_APB2>; @@ -971,7 +971,7 @@ #interrupt-cells = <1>; reg = <0x780 0x80 0xDC0 0x20>; - compatible = "aspeed,ast2600-i2c"; + compatible = "aspeed,ast2600-i2c-bus"; bus-frequency = <100000>; interrupts = ; clocks = <&scu ASPEED_CLK_APB2>; @@ -984,7 +984,7 @@ #interrupt-cells = <1>; reg = <0x800 0x80 0xDE0 0x20>; - compatible = "aspeed,ast2600-i2c"; + compatible = "aspeed,ast2600-i2c-bus"; bus-frequency = <100000>; interrupts = ; clocks = <&scu ASPEED_CLK_APB2>; diff --git a/drivers/i2c/ast2600_i2c.c b/drivers/i2c/ast2600_i2c.c index f1aebe0b733f..5eda2909c661 100644 --- a/drivers/i2c/ast2600_i2c.c +++ b/drivers/i2c/ast2600_i2c.c @@ -385,7 +385,7 @@ static const struct dm_i2c_ops ast2600_i2c_ops = { }; static const struct udevice_id ast2600_i2c_ids[] = { - { .compatible = "aspeed,ast2600-i2c" }, + { .compatible = "aspeed,ast2600-i2c-bus" }, {}, }; From aad8f358373a1b5f7298ba3ca054c2d0b02f59ea Mon Sep 17 00:00:00 2001 From: Joel Stanley Date: Thu, 14 Jul 2022 16:14:59 +0930 Subject: [PATCH 6/6] i2c/aspeed: Only use new mode when driver is enabled Don't attempt to switch to the AST2600 "new" register mode if the driver is not compiled. Signed-off-by: Joel Stanley --- drivers/i2c/aspeed_i2c_global.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/i2c/aspeed_i2c_global.c b/drivers/i2c/aspeed_i2c_global.c index c76c29821083..86ed8f746d56 100644 --- a/drivers/i2c/aspeed_i2c_global.c +++ b/drivers/i2c/aspeed_i2c_global.c @@ -60,7 +60,8 @@ static int aspeed_i2c_global_probe(struct udevice *dev) reset_deassert(&i2c_global->reset); - if (i2c_global->version == AST2600_I2C_GLOBAL) { + if (IS_ENABLED(SYS_I2C_AST2600) && + i2c_global->version == AST2600_I2C_GLOBAL) { writel(AST2600_GLOBAL_INIT, i2c_global->regs + AST2600_I2CG_CTRL); writel(I2CCG_DIV_CTRL, i2c_global->regs +