From 1597f80de42f2669e579bed1eafed69ea1389660 Mon Sep 17 00:00:00 2001 From: Jacky Bai Date: Tue, 8 Sep 2020 09:55:59 +0800 Subject: MLK-24721 plat: imx8m: Fix the out of bound access to rank setting array Fix the out of bound access to the rank setting array. Signed-off-by: Jacky Bai Reviewed-by: Ye Li --- plat/imx/imx8m/ddr/dram.c | 6 ++++++ plat/imx/imx8m/ddr/dram_retention.c | 3 +++ plat/imx/imx8m/include/dram.h | 2 ++ 3 files changed, 11 insertions(+) diff --git a/plat/imx/imx8m/ddr/dram.c b/plat/imx/imx8m/ddr/dram.c index 3424e92c..333fbaba 100644 --- a/plat/imx/imx8m/ddr/dram.c +++ b/plat/imx/imx8m/ddr/dram.c @@ -76,6 +76,9 @@ static void save_rank_setting(void) uint32_t i, offset; uint32_t pstate_num = dram_info.num_fsp; + /* only support maximum 3 setpoints */ + pstate_num = (pstate_num > MAX_FSP_NUM) ? MAX_FSP_NUM : pstate_num; + for(i = 0; i < pstate_num; i++) { offset = i ? (i + 1) * 0x1000 : 0; if (dram_info.dram_type == DDRC_LPDDR4) { @@ -200,6 +203,9 @@ void dram_info_init(unsigned long dram_timing_base) break; dram_info.num_fsp = i; + /* only support maximum 3 setpoints */ + dram_info.num_fsp = (i > MAX_FSP_NUM) ? MAX_FSP_NUM : i; + /* save the DRAMTMG2/9 for rank to rank workaround */ save_rank_setting(); diff --git a/plat/imx/imx8m/ddr/dram_retention.c b/plat/imx/imx8m/ddr/dram_retention.c index 6a19f0e7..2dd79b61 100644 --- a/plat/imx/imx8m/ddr/dram_retention.c +++ b/plat/imx/imx8m/ddr/dram_retention.c @@ -24,6 +24,9 @@ static void rank_setting_update(void) uint32_t i, offset; uint32_t pstate_num = dram_info.num_fsp; + /* only support maximum 3 setpoints */ + pstate_num = (pstate_num > MAX_FSP_NUM) ? MAX_FSP_NUM : pstate_num; + for (i = 0; i < pstate_num; i++) { offset = i ? (i + 1) * 0x1000 : 0; if (dram_info.dram_type == DDRC_LPDDR4) { diff --git a/plat/imx/imx8m/include/dram.h b/plat/imx/imx8m/include/dram.h index d0d5bf81..144316af 100644 --- a/plat/imx/imx8m/include/dram.h +++ b/plat/imx/imx8m/include/dram.h @@ -22,6 +22,8 @@ #define DDRC_ACTIVE_ONE_RANK U(0x1) #define DDRC_ACTIVE_TWO_RANK U(0x2) +#define MAX_FSP_NUM U(3) + /* reg & config param */ struct dram_cfg_param { unsigned int reg; -- cgit v1.2.3