From 3fe21f028ad3dce6109954fa282a82fb9292cd67 Mon Sep 17 00:00:00 2001 From: Joseph Chen Date: Mon, 30 Mar 2020 11:58:37 +0800 Subject: [PATCH 1/5] regulator: rk8xx: fix buck get wrong value Before this, we always get the 1st of BUCK range group which brings the problems we face now. Let's traverse all the possible BUCK ranges to fix this issue. Signed-off-by: Joseph Chen Change-Id: I61a341fcbd190bcc0baff8267a94063c15abcc30 --- drivers/power/regulator/rk8xx.c | 100 +++++++++++++++++++------------- 1 file changed, 61 insertions(+), 39 deletions(-) diff --git a/drivers/power/regulator/rk8xx.c b/drivers/power/regulator/rk8xx.c index ad1dc8b1b0..744b99ca30 100644 --- a/drivers/power/regulator/rk8xx.c +++ b/drivers/power/regulator/rk8xx.c @@ -87,62 +87,65 @@ struct rk8xx_reg_info { u8 config_reg; u8 vsel_mask; u8 min_sel; + /* only for buck now */ + u8 max_sel; + u8 range_num; }; static const struct rk8xx_reg_info rk808_buck[] = { - { 712500, 12500, REG_BUCK1_ON_VSEL, REG_BUCK1_SLP_VSEL, REG_BUCK1_CONFIG, RK808_BUCK_VSEL_MASK, }, - { 712500, 12500, REG_BUCK2_ON_VSEL, REG_BUCK2_SLP_VSEL, REG_BUCK2_CONFIG, RK808_BUCK_VSEL_MASK, }, - { 712500, 12500, NA, NA, REG_BUCK3_CONFIG, RK808_BUCK_VSEL_MASK, }, - { 1800000, 100000, REG_BUCK4_ON_VSEL, REG_BUCK4_SLP_VSEL, REG_BUCK4_CONFIG, RK808_BUCK4_VSEL_MASK, }, + { 712500, 12500, REG_BUCK1_ON_VSEL, REG_BUCK1_SLP_VSEL, REG_BUCK1_CONFIG, RK808_BUCK_VSEL_MASK, 0x00, 0x3f, 1}, + { 712500, 12500, REG_BUCK2_ON_VSEL, REG_BUCK2_SLP_VSEL, REG_BUCK2_CONFIG, RK808_BUCK_VSEL_MASK, 0x00, 0x3f, 1}, + { NA, NA, NA, NA, REG_BUCK3_CONFIG, NA, NA, NA, 1}, + { 1800000, 100000, REG_BUCK4_ON_VSEL, REG_BUCK4_SLP_VSEL, REG_BUCK4_CONFIG, RK808_BUCK4_VSEL_MASK,0x00, 0x0f, 1}, }; static const struct rk8xx_reg_info rk816_buck[] = { /* buck 1 */ - { 712500, 12500, REG_BUCK1_ON_VSEL, REG_BUCK1_SLP_VSEL, REG_BUCK1_CONFIG, RK818_BUCK_VSEL_MASK, 0x00, }, - { 1800000, 200000, REG_BUCK1_ON_VSEL, REG_BUCK1_SLP_VSEL, REG_BUCK1_CONFIG, RK818_BUCK_VSEL_MASK, 0x3c, }, - { 2300000, 0, REG_BUCK1_ON_VSEL, REG_BUCK1_SLP_VSEL, REG_BUCK1_CONFIG, RK818_BUCK_VSEL_MASK, 0x3f, }, + { 712500, 12500, REG_BUCK1_ON_VSEL, REG_BUCK1_SLP_VSEL, REG_BUCK1_CONFIG, RK818_BUCK_VSEL_MASK, 0x00, 0x3b, 3}, + { 1800000, 200000, REG_BUCK1_ON_VSEL, REG_BUCK1_SLP_VSEL, REG_BUCK1_CONFIG, RK818_BUCK_VSEL_MASK, 0x3c, 0x3e, 3}, + { 2300000, 0, REG_BUCK1_ON_VSEL, REG_BUCK1_SLP_VSEL, REG_BUCK1_CONFIG, RK818_BUCK_VSEL_MASK, 0x3f, 0x3f, 3}, /* buck 2 */ - { 712500, 12500, REG_BUCK2_ON_VSEL, REG_BUCK2_SLP_VSEL, REG_BUCK2_CONFIG, RK818_BUCK_VSEL_MASK, 0x00, }, - { 1800000, 200000, REG_BUCK2_ON_VSEL, REG_BUCK2_SLP_VSEL, REG_BUCK2_CONFIG, RK818_BUCK_VSEL_MASK, 0x3c, }, - { 2300000, 0, REG_BUCK2_ON_VSEL, REG_BUCK2_SLP_VSEL, REG_BUCK2_CONFIG, RK818_BUCK_VSEL_MASK, 0x3f, }, + { 712500, 12500, REG_BUCK2_ON_VSEL, REG_BUCK2_SLP_VSEL, REG_BUCK2_CONFIG, RK818_BUCK_VSEL_MASK, 0x00, 0x3b, 3}, + { 1800000, 200000, REG_BUCK2_ON_VSEL, REG_BUCK2_SLP_VSEL, REG_BUCK2_CONFIG, RK818_BUCK_VSEL_MASK, 0x3c, 0x3e, 3}, + { 2300000, 0, REG_BUCK2_ON_VSEL, REG_BUCK2_SLP_VSEL, REG_BUCK2_CONFIG, RK818_BUCK_VSEL_MASK, 0x3f, 0x3f, 3}, /* buck 3 */ - { 712500, 12500, NA, NA, REG_BUCK3_CONFIG, RK818_BUCK_VSEL_MASK, }, + { NA, NA, NA, NA, REG_BUCK3_CONFIG, NA, NA, NA, 1}, /* buck 4 */ - { 800000, 100000, REG_BUCK4_ON_VSEL, REG_BUCK4_SLP_VSEL, REG_BUCK4_CONFIG, RK818_BUCK4_VSEL_MASK, }, + { 800000, 100000, REG_BUCK4_ON_VSEL, REG_BUCK4_SLP_VSEL, REG_BUCK4_CONFIG, RK818_BUCK4_VSEL_MASK,0x00, 0x1b, 1}, }; static const struct rk8xx_reg_info rk809_buck5[] = { /* buck 5 */ - { 1500000, 0, RK809_BUCK5_CONFIG(0), RK809_BUCK5_CONFIG(1), NA, RK809_BUCK5_VSEL_MASK, 0x00, }, - { 1800000, 200000, RK809_BUCK5_CONFIG(0), RK809_BUCK5_CONFIG(1), NA, RK809_BUCK5_VSEL_MASK, 0x01, }, - { 2800000, 200000, RK809_BUCK5_CONFIG(0), RK809_BUCK5_CONFIG(1), NA, RK809_BUCK5_VSEL_MASK, 0x04, }, - { 3300000, 300000, RK809_BUCK5_CONFIG(0), RK809_BUCK5_CONFIG(1), NA, RK809_BUCK5_VSEL_MASK, 0x06, }, + { 1500000, 0, RK809_BUCK5_CONFIG(0), RK809_BUCK5_CONFIG(1), NA, RK809_BUCK5_VSEL_MASK, 0x00, 0x00, 4}, + { 1800000, 200000, RK809_BUCK5_CONFIG(0), RK809_BUCK5_CONFIG(1), NA, RK809_BUCK5_VSEL_MASK, 0x01, 0x03, 4}, + { 2800000, 200000, RK809_BUCK5_CONFIG(0), RK809_BUCK5_CONFIG(1), NA, RK809_BUCK5_VSEL_MASK, 0x04, 0x05, 4}, + { 3300000, 300000, RK809_BUCK5_CONFIG(0), RK809_BUCK5_CONFIG(1), NA, RK809_BUCK5_VSEL_MASK, 0x06, 0x07, 4}, }; static const struct rk8xx_reg_info rk817_buck[] = { /* buck 1 */ - { 500000, 12500, RK817_BUCK_ON_VSEL(1), RK817_BUCK_SLP_VSEL(1), RK817_BUCK_CONFIG(1), RK817_BUCK_VSEL_MASK, 0x00, }, - { 1500000, 100000, RK817_BUCK_ON_VSEL(1), RK817_BUCK_SLP_VSEL(1), RK817_BUCK_CONFIG(1), RK817_BUCK_VSEL_MASK, 0x50, }, - { 2400000, 0, RK817_BUCK_ON_VSEL(1), RK817_BUCK_SLP_VSEL(1), RK817_BUCK_CONFIG(1), RK817_BUCK_VSEL_MASK, 0x59, }, + { 500000, 12500, RK817_BUCK_ON_VSEL(1), RK817_BUCK_SLP_VSEL(1), RK817_BUCK_CONFIG(1), RK817_BUCK_VSEL_MASK, 0x00, 0x4f, 3}, + { 1500000, 100000, RK817_BUCK_ON_VSEL(1), RK817_BUCK_SLP_VSEL(1), RK817_BUCK_CONFIG(1), RK817_BUCK_VSEL_MASK, 0x50, 0x58, 3}, + { 2400000, 0, RK817_BUCK_ON_VSEL(1), RK817_BUCK_SLP_VSEL(1), RK817_BUCK_CONFIG(1), RK817_BUCK_VSEL_MASK, 0x59, 0x59, 3}, /* buck 2 */ - { 500000, 12500, RK817_BUCK_ON_VSEL(2), RK817_BUCK_SLP_VSEL(2), RK817_BUCK_CONFIG(2), RK817_BUCK_VSEL_MASK, 0x00, }, - { 1500000, 100000, RK817_BUCK_ON_VSEL(2), RK817_BUCK_SLP_VSEL(2), RK817_BUCK_CONFIG(2), RK817_BUCK_VSEL_MASK, 0x50, }, - { 2400000, 0, RK817_BUCK_ON_VSEL(2), RK817_BUCK_SLP_VSEL(2), RK817_BUCK_CONFIG(2), RK817_BUCK_VSEL_MASK, 0x59, }, + { 500000, 12500, RK817_BUCK_ON_VSEL(2), RK817_BUCK_SLP_VSEL(2), RK817_BUCK_CONFIG(2), RK817_BUCK_VSEL_MASK, 0x00, 0x4f, 3}, + { 1500000, 100000, RK817_BUCK_ON_VSEL(2), RK817_BUCK_SLP_VSEL(2), RK817_BUCK_CONFIG(2), RK817_BUCK_VSEL_MASK, 0x50, 0x58, 3}, + { 2400000, 0, RK817_BUCK_ON_VSEL(2), RK817_BUCK_SLP_VSEL(2), RK817_BUCK_CONFIG(2), RK817_BUCK_VSEL_MASK, 0x59, 0x59, 3}, /* buck 3 */ - { 500000, 12500, RK817_BUCK_ON_VSEL(3), RK817_BUCK_SLP_VSEL(3), RK817_BUCK_CONFIG(3), RK817_BUCK_VSEL_MASK, 0x00, }, - { 1500000, 100000, RK817_BUCK_ON_VSEL(3), RK817_BUCK_SLP_VSEL(3), RK817_BUCK_CONFIG(3), RK817_BUCK_VSEL_MASK, 0x50, }, - { 2400000, 0, RK817_BUCK_ON_VSEL(3), RK817_BUCK_SLP_VSEL(3), RK817_BUCK_CONFIG(3), RK817_BUCK_VSEL_MASK, 0x59, }, + { 500000, 12500, RK817_BUCK_ON_VSEL(3), RK817_BUCK_SLP_VSEL(3), RK817_BUCK_CONFIG(3), RK817_BUCK_VSEL_MASK, 0x00, 0x4f, 3}, + { 1500000, 100000, RK817_BUCK_ON_VSEL(3), RK817_BUCK_SLP_VSEL(3), RK817_BUCK_CONFIG(3), RK817_BUCK_VSEL_MASK, 0x50, 0x58, 3}, + { 2400000, 0, RK817_BUCK_ON_VSEL(3), RK817_BUCK_SLP_VSEL(3), RK817_BUCK_CONFIG(3), RK817_BUCK_VSEL_MASK, 0x59, 0x59, 3}, /* buck 4 */ - { 500000, 12500, RK817_BUCK_ON_VSEL(4), RK817_BUCK_SLP_VSEL(4), RK817_BUCK_CONFIG(4), RK817_BUCK_VSEL_MASK, 0x00, }, - { 1500000, 100000, RK817_BUCK_ON_VSEL(4), RK817_BUCK_SLP_VSEL(4), RK817_BUCK_CONFIG(4), RK817_BUCK_VSEL_MASK, 0x50, }, - { 3400000, 0, RK817_BUCK_ON_VSEL(4), RK817_BUCK_SLP_VSEL(4), RK817_BUCK_CONFIG(4), RK817_BUCK_VSEL_MASK, 0x63, }, + { 500000, 12500, RK817_BUCK_ON_VSEL(4), RK817_BUCK_SLP_VSEL(4), RK817_BUCK_CONFIG(4), RK817_BUCK_VSEL_MASK, 0x00, 0x4f, 3}, + { 1500000, 100000, RK817_BUCK_ON_VSEL(4), RK817_BUCK_SLP_VSEL(4), RK817_BUCK_CONFIG(4), RK817_BUCK_VSEL_MASK, 0x50, 0x62, 3}, + { 3400000, 0, RK817_BUCK_ON_VSEL(4), RK817_BUCK_SLP_VSEL(4), RK817_BUCK_CONFIG(4), RK817_BUCK_VSEL_MASK, 0x63, 0x63, 3}, }; static const struct rk8xx_reg_info rk818_buck[] = { - { 712500, 12500, REG_BUCK1_ON_VSEL, REG_BUCK1_SLP_VSEL, REG_BUCK1_CONFIG, RK818_BUCK_VSEL_MASK, }, - { 712500, 12500, REG_BUCK2_ON_VSEL, REG_BUCK2_SLP_VSEL, REG_BUCK2_CONFIG, RK818_BUCK_VSEL_MASK, }, - { 712500, 12500, NA, NA, REG_BUCK3_CONFIG, RK818_BUCK_VSEL_MASK, }, - { 1800000, 100000, REG_BUCK4_ON_VSEL, REG_BUCK4_SLP_VSEL, REG_BUCK4_CONFIG, RK818_BUCK4_VSEL_MASK, }, + { 712500, 12500, REG_BUCK1_ON_VSEL, REG_BUCK1_SLP_VSEL, REG_BUCK1_CONFIG, RK818_BUCK_VSEL_MASK, 0x00, 0x3f, 1}, + { 712500, 12500, REG_BUCK2_ON_VSEL, REG_BUCK2_SLP_VSEL, REG_BUCK2_CONFIG, RK818_BUCK_VSEL_MASK, 0x00, 0x3f, 1}, + { NA, NA, NA, NA, REG_BUCK3_CONFIG, NA, NA, NA, 1}, + { 1800000, 100000, REG_BUCK4_ON_VSEL, REG_BUCK4_SLP_VSEL, REG_BUCK4_CONFIG, RK818_BUCK4_VSEL_MASK,0x00, 0x10, 1}, }; #ifdef ENABLE_DRIVER @@ -784,10 +787,9 @@ static int _ldo_get_suspend_enable(struct udevice *pmic, int ldo) static int buck_get_value(struct udevice *dev) { int buck = dev->driver_data - 1; - /* We assume level-1 voltage is enough for usage in U-Boot */ const struct rk8xx_reg_info *info = get_buck_reg(dev->parent, buck, 0); int mask = info->vsel_mask; - int ret, val; + int i, ret, val; if (info->vsel_reg == NA) return -ENOSYS; @@ -795,9 +797,20 @@ static int buck_get_value(struct udevice *dev) ret = pmic_reg_read(dev->parent, info->vsel_reg); if (ret < 0) return ret; - val = ret & mask; - return info->min_uv + val * info->step_uv; + val = ret & mask; + if (val >= info->min_sel && val <= info->max_sel) + goto finish; + + /* unlucky to try */ + for (i = 1; i < info->range_num; i++) { + info++; + if (val <= info->max_sel && val >= info->min_sel) + break; + } + +finish: + return info->min_uv + (val - info->min_sel) * info->step_uv; } static int buck_set_value(struct udevice *dev, int uvolt) @@ -810,10 +823,9 @@ static int buck_set_value(struct udevice *dev, int uvolt) static int buck_get_suspend_value(struct udevice *dev) { int buck = dev->driver_data - 1; - /* We assume level-1 voltage is enough for usage in U-Boot */ const struct rk8xx_reg_info *info = get_buck_reg(dev->parent, buck, 0); int mask = info->vsel_mask; - int ret, val; + int i, ret, val; if (info->vsel_sleep_reg == NA) return -ENOSYS; @@ -823,8 +835,18 @@ static int buck_get_suspend_value(struct udevice *dev) return ret; val = ret & mask; + if (val <= info->max_sel && val >= info->min_sel) + goto finish; - return info->min_uv + val * info->step_uv; + /* unlucky to try */ + for (i = 1; i < info->range_num; i++) { + info++; + if (val <= info->max_sel && val >= info->min_sel) + break; + } + +finish: + return info->min_uv + (val - info->min_sel) * info->step_uv; } static int buck_set_suspend_value(struct udevice *dev, int uvolt) From 3a6927377899763dae7974adf00831271468e4dc Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Sat, 28 Mar 2020 15:51:09 +0800 Subject: [PATCH 2/5] mtd: nand: rockchip: fix compile error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit error: implicit declaration of function ‘fdt_get_base_address’ [-Werror=implicit-function-declaration] Signed-off-by: Jason Zhu Change-Id: Ic950b9382bd200d854028773b7a4c63b4bed5c16 --- drivers/mtd/nand/raw/rockchip_nand_spl.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/mtd/nand/raw/rockchip_nand_spl.c b/drivers/mtd/nand/raw/rockchip_nand_spl.c index 98dfe27a81..7cf318141d 100644 --- a/drivers/mtd/nand/raw/rockchip_nand_spl.c +++ b/drivers/mtd/nand/raw/rockchip_nand_spl.c @@ -6,6 +6,7 @@ #include #include +#include #include #include #include From cdd8fbe39efdfb18aa05f2f53d0e8888055a8956 Mon Sep 17 00:00:00 2001 From: Joseph Chen Date: Tue, 31 Mar 2020 15:35:53 +0800 Subject: [PATCH 3/5] tools: fit_info: add END address of property Signed-off-by: Joseph Chen Change-Id: Icf988f3620d261f7742ee3db4e427e1783211f13 --- tools/fit_info.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/fit_info.c b/tools/fit_info.c index 652abcd2bd..82a1a9f4c4 100644 --- a/tools/fit_info.c +++ b/tools/fit_info.c @@ -102,6 +102,7 @@ int main(int argc, char **argv) printf("NAME: %s\n", fit_get_name(fit_blob, nodeoffset, NULL)); printf("LEN: %d\n", len); printf("OFF: %d\n", (int)(nodep - fit_blob)); + printf("END: %d\n", (int)(nodep + len - fit_blob)); (void) munmap((void *)fit_blob, fsbuf.st_size); close(ffd); From d46373c14f91f281057ab95cc38fd374f4acc780 Mon Sep 17 00:00:00 2001 From: Joseph Chen Date: Tue, 31 Mar 2020 15:41:59 +0800 Subject: [PATCH 4/5] lib: rsa: generate data to be signed Signed-off-by: Joseph Chen Change-Id: I125f61051c9c9604903603ef06cd7f368b48f3d8 --- lib/rsa/rsa-sign.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/lib/rsa/rsa-sign.c b/lib/rsa/rsa-sign.c index c9b240e16c..bdfc2f1521 100644 --- a/lib/rsa/rsa-sign.c +++ b/lib/rsa/rsa-sign.c @@ -383,6 +383,30 @@ static void rsa_engine_remove(ENGINE *e) } } +/* + * With this data2sign.bin, we can provide it to who real holds the RAS-private + * key to sign current fit image. Then we replace the signature in fit image + * with a valid one. + */ +static void gen_data2sign(const struct image_region region[], int region_count) +{ + char *file = "data2sign.bin"; + FILE *fd; + int i; + + fd = fopen(file, "wb"); + if (!fd) { + fprintf(stderr, "Failed to create %s: %s\n", + file, strerror(errno)); + return -ENOENT; + } + + for (i = 0; i < region_count; i++) + fwrite(region[i].data, region[i].size, 1, fd); + + fclose(fd); +} + static int rsa_sign_with_key(RSA *rsa, struct checksum_algo *checksum_algo, const struct image_region region[], int region_count, uint8_t **sigp, uint *sig_size) @@ -445,6 +469,8 @@ static int rsa_sign_with_key(RSA *rsa, struct checksum_algo *checksum_algo, *sigp = sig; *sig_size = size; + gen_data2sign(region, region_count); + return 0; err_sign: From 3301aa1a14d90d7ce0b25aeac8abc03fb0311a1d Mon Sep 17 00:00:00 2001 From: Joseph Chen Date: Tue, 31 Mar 2020 16:06:03 +0800 Subject: [PATCH 5/5] scripts: add a script to resign the image Usage: scripts/fit-resign.sh [signed fit image] [new signature] It replaces the /configurations/conf@1/signature@1/value. Signed-off-by: Joseph Chen Change-Id: I73ff5ec9a625a87ef91f29773ad241f833ef1758 --- scripts/fit-resign.sh | 47 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 scripts/fit-resign.sh diff --git a/scripts/fit-resign.sh b/scripts/fit-resign.sh new file mode 100755 index 0000000000..d4b6693297 --- /dev/null +++ b/scripts/fit-resign.sh @@ -0,0 +1,47 @@ +#!/bin/bash +# +# Copyright (c) 2020 Fuzhou Rockchip Electronics Co., Ltd +# SPDX-License-Identifier: GPL-2.0 +# +set -e + +FIT_IMG=$1 +FIT_SIG=$2 + +if [ $# -ne 2 ]; then + echo "Usage: $0 [signed fit image] [new signature]" + exit +elif [ ! -f $FIT_IMG ]; then + echo "ERROR: No $FIT_IMG" + exit +elif [ ! -f $FIT_SIG ]; then + echo "ERROR: No $FIT_SIG" + exit +fi + +SIG_SZ=`ls -l ${FIT_SIG} | awk '{ print $5 }'` +LEN=`./tools/fit_info -f $FIT_IMG -n /configurations/conf@1/signature@1 -p value | sed -n "/LEN:/p" | awk '{ print $2 }'` +OFF=`./tools/fit_info -f $FIT_IMG -n /configurations/conf@1/signature@1 -p value | sed -n "/OFF:/p" | awk '{ print $2 }'` +END=`./tools/fit_info -f $FIT_IMG -n /configurations/conf@1/signature@1 -p value | sed -n "/END:/p" | awk '{ print $2 }'` + +if [ -z $LEN ]; then + echo "ERROR: No valid signature in $FIT_IMG" + exit +elif [ "$SIG_SZ" -ne "$LEN" ]; then + echo "ERROR: $FIT_SIG size $SIG_SZ != $FIT_IMG Signature size $LEN" + exit +fi + +dd if=$FIT_IMG of=$FIT_IMG.part1 count=1 bs=$OFF +dd if=$FIT_IMG of=$FIT_IMG.part2 skip=1 ibs=$END + +cat $FIT_IMG.part1 > $FIT_IMG.resig +cat $FIT_SIG >> $FIT_IMG.resig +cat $FIT_IMG.part2 >> $FIT_IMG.resig + +rm $FIT_IMG.part1 && $FIT_IMG.part2 + +echo +echo "Re-signed fit image is OK: $FIT_IMG.resig" +echo +