Re: [PATCH 1/5] pinctrl: qcom: add support for bias-pull-down

2024-05-28 Thread Sumit Garg
Hi Neil,

On Tue, 28 May 2024 at 14:02, Neil Armstrong  wrote:
>
> Add support for bias-pull-down as an alternate of bias-pull-up.

nit: s/alternate/alternative/

>
> Signed-off-by: Neil Armstrong 
> ---
>  drivers/pinctrl/qcom/pinctrl-qcom.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/drivers/pinctrl/qcom/pinctrl-qcom.c 
> b/drivers/pinctrl/qcom/pinctrl-qcom.c
> index 3c3336e7635..4f4e9a83949 100644
> --- a/drivers/pinctrl/qcom/pinctrl-qcom.c
> +++ b/drivers/pinctrl/qcom/pinctrl-qcom.c
> @@ -44,6 +44,7 @@ static const struct pinconf_param msm_conf_params[] = {
> { "drive-strength", PIN_CONFIG_DRIVE_STRENGTH, 2 },
> { "bias-disable", PIN_CONFIG_BIAS_DISABLE, 0 },
> { "bias-pull-up", PIN_CONFIG_BIAS_PULL_UP, 3 },
> +   { "bias-pull-down", PIN_CONFIG_BIAS_PULL_UP, 1 },

Did you mean PIN_CONFIG_BIAS_PULL_DOWN here instead?

-Sumit

> { "output-high", PIN_CONFIG_OUTPUT, 1, },
> { "output-low", PIN_CONFIG_OUTPUT, 0, },
>  };
>
> --
> 2.34.1
>


Re: [PATCH v2 7/7] configs: j721s2_evm_r5_defconfig: Add AVS Configs

2024-05-28 Thread Kumar, Udit



On 5/28/2024 1:43 PM, Manorit Chawdhry wrote:

Add AVS and PMIC regulator configs

Signed-off-by: Manorit Chawdhry 
---
  configs/am68_sk_r5_defconfig| 4 
  configs/j721s2_evm_r5_defconfig | 6 ++
  2 files changed, 10 insertions(+)

diff --git a/configs/am68_sk_r5_defconfig b/configs/am68_sk_r5_defconfig
index e9b6882c1f68..f4800ef1b8ac 100644
--- a/configs/am68_sk_r5_defconfig
+++ b/configs/am68_sk_r5_defconfig
@@ -5,6 +5,10 @@ CONFIG_ARCH_K3=y
  CONFIG_SOC_K3_J721S2=y
  CONFIG_TARGET_J721S2_R5_EVM=y
  
+CONFIG_K3_AVS0=n

+CONFIG_DM_REGULATOR_TPS65941=n
+CONFIG_PMIC_TPS65941=n
+


Extra line



  CONFIG_DEFAULT_DEVICE_TREE="k3-am68-sk-r5-base-board"
  CONFIG_SPL_OF_LIST="k3-am68-sk-r5-base-board"
  CONFIG_OF_LIST="k3-am68-sk-r5-base-board"
diff --git a/configs/j721s2_evm_r5_defconfig b/configs/j721s2_evm_r5_defconfig
index 3c958cafbe8f..f8a42a9a2ef6 100644
--- a/configs/j721s2_evm_r5_defconfig
+++ b/configs/j721s2_evm_r5_defconfig
@@ -111,6 +111,7 @@ CONFIG_DM_MAILBOX=y
  CONFIG_K3_SEC_PROXY=y
  CONFIG_FS_LOADER=y
  CONFIG_SPL_FS_LOADER=y
+CONFIG_K3_AVS0=y
  CONFIG_SUPPORT_EMMC_BOOT=y
  CONFIG_SPL_MMC_HS400_SUPPORT=y
  CONFIG_MMC_SDHCI=y
@@ -140,6 +141,11 @@ CONFIG_SPL_PINCTRL=y
  CONFIG_PINCTRL_SINGLE=y
  CONFIG_POWER_DOMAIN=y
  CONFIG_TI_POWER_DOMAIN=y
+CONFIG_DM_PMIC=y
+CONFIG_PMIC_TPS65941=y
+CONFIG_DM_REGULATOR=y
+CONFIG_SPL_DM_REGULATOR=y
+CONFIG_DM_REGULATOR_TPS65941=y
  CONFIG_K3_SYSTEM_CONTROLLER=y
  CONFIG_REMOTEPROC_TI_K3_ARM64=y
  CONFIG_RESET_TI_SCI=y



Re: [PATCH v2 3/7] drivers: misc: k3_avs: Check return code while programming AVS

2024-05-28 Thread Kumar, Udit



On 5/28/2024 1:43 PM, Manorit Chawdhry wrote:

Check if AVS could not be programmed and print a warning.

Signed-off-by: Manorit Chawdhry 
---
  drivers/misc/k3_avs.c | 5 -
  1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/k3_avs.c b/drivers/misc/k3_avs.c
index a0d7bb7595b3..d9037b4512c6 100644
--- a/drivers/misc/k3_avs.c
+++ b/drivers/misc/k3_avs.c
@@ -489,7 +489,10 @@ static int k3_avs_probe(struct udevice *dev)
if (vd->flags & VD_FLAG_INIT_DONE)
continue;
  
-		k3_avs_program_voltage(priv, vd, vd->opp);

+   ret = k3_avs_program_voltage(priv, vd, vd->opp);
+   if (ret)
+   dev_warn(dev, "Could not program AVS voltage for 
VD%d\n",
+vd->id);


Could you extend print with vd->opp as well.

With this change

Reviewed-by: Udit Kumar 


}
  
  	if (!device_is_compatible(priv->dev, "ti,am654-avs"))




Re: [PATCH v2 2/7] drivers: misc: k3_avs: Use soc_match_device instead of compatible.

2024-05-28 Thread Kumar, Udit

Hi Manorit

On 5/28/2024 1:43 PM, Manorit Chawdhry wrote:

vd_data remains the same for most of the SoCs but there are some
differences in power domains and clocks that don't need a change to
compatible and can be handled at SoC level.

Change the data population logic from compatible to soc_match_device to
add support for newer devices.

Signed-off-by: Manorit Chawdhry 
---
  drivers/misc/k3_avs.c | 189 +++---
  1 file changed, 103 insertions(+), 86 deletions(-)

diff --git a/drivers/misc/k3_avs.c b/drivers/misc/k3_avs.c
index 87471cc3b16a..a0d7bb7595b3 100644
--- a/drivers/misc/k3_avs.c
+++ b/drivers/misc/k3_avs.c
@@ -12,6 +12,7 @@
  #include 
  #include 
  #include 
+#include 
  #include 
  #include 
  #include 
@@ -111,6 +112,95 @@ static u32 am6_efuse_xlate(struct k3_avs_privdata *priv, 
int idx, int opp)
return 30 + 2 * val;
  }
  
[..]

+const struct soc_attr vtm_soc_list[] = {
+   { .family = "AM65X", .data = (void *)_vd_config },
+   { .family = "J721E", .data = (void *)_vd_config },
+   { .family = "J7200", .data = (void *)_vd_config },
+   {}
+};


We may end up updating this driver for each SOC family,

Even if VTM IP remain same in two SOC family.

In my view, driver should be specific to IP not specific to SOC or SOC 
family.




[..]



Re: [PATCH] sunxi: spl: h616: fix booting from high MMC offset

2024-05-28 Thread Ryan Walklin



On Fri, 10 May 2024, at 11:13 AM, Andre Przywara wrote:

> Extend the existing offset calculation code to consider the different
> sector offset when running on an H616 SoC. This allows to load U-Boot
> on any H616 device when the SPL is not located at 8KB.

Thanks Andre,

Confirmed with an H700-based board and u-boot at the 512-sector/256KB offset.

Tested-by: Ryan Walklin 

Regards,

Ryan


Re: [PATCH v2 0/8] efi_loader: improve device-tree loading

2024-05-28 Thread E Shattow
Hi,

On Tue, May 28, 2024 at 7:43 AM Heinrich Schuchardt
 wrote:
>
> In U-Boot EFI boot options can already specify both an EFI binary and
> an initrd. With this series we can additionally define the matching
> device-tree to be loaded in the boot option.
>
> With the last patch the boot manager will fall back the device-tree
> specified by $fdtfile in directories '/dtb/', '/', or '/dtb/current/'
> on the boot device if no device-tree is specified in the boot
> option or via a bootefi command parameter.
>

As tested the $fdtfile environment variable has no effect on
global EFI boot when i.e. EFI/BOOT/BOOTRISCV64.EFI
on EFI System Partition and no user-added boot option;
$fdtfile env variable is not used with "mmc 0" or whichever
global boot option is enabled by default in the boot order.

Adding a boot option for EFI/BOOT/BOOTRISCV64.EFI
and giving this priority in the boot order allows $fdtfile to
be effective here. This is consistent with what is described
by the series. Would the global EFI boot also get support
for $fdtfile either with this or a later series?

> v2:
> Update efi_dp_concat() instead of new function efi_dp_merge().
> Carve out a function efi_load_option_dp_join() which we can
> use both for the eficonfig and the efidebug command.
> Rename variables id_dp, final_dp_size.
> Rename create_initrd_dp() to create_lo_dp_part().
> Use enum as parameter for create_lo_dp_part().
> Put all related changes into one patch.
>
> Heinrich Schuchardt (8):
>   efi_loader: allow concatenation with contained end node
>   cmd: eficonfig: add support for setting fdt
>   cmd: efidebug: add support for setting fdt
>   efi_loader: load device-tree specified in boot option
>   efi_loader: move distro_efi_get_fdt_name()
>   efi_loader: return binary from efi_dp_from_lo()
>   efi_loader: export efi_load_image_from_path
>   efi_loader: load distro dtb in bootmgr
>
>  boot/bootmeth_efi.c|  60 +-
>  cmd/eficonfig.c|  83 +
>  cmd/efidebug.c | 130 +++--
>  include/efi_loader.h   |  24 +++-
>  lib/efi_loader/Makefile|   1 +
>  lib/efi_loader/efi_bootbin.c   |   2 +-
>  lib/efi_loader/efi_bootmgr.c   |  75 +++-
>  lib/efi_loader/efi_boottime.c  |   3 +-
>  lib/efi_loader/efi_device_path.c   |  40 ---
>  lib/efi_loader/efi_device_path_utilities.c |   2 +-
>  lib/efi_loader/efi_fdt.c   | 117 +++
>  lib/efi_loader/efi_helper.c|  44 +++
>  12 files changed, 445 insertions(+), 136 deletions(-)
>  create mode 100644 lib/efi_loader/efi_fdt.c
>
> --
> 2.43.0
>

Tested-by: E Shattow 


Fix splash screen display

2024-05-28 Thread EDM Rubino
The attached patch file consists of 2 small patches.

[PATCH 0/1] Invert the order for calling the weak function
"splash_screen_prepare" and checking for the "splashimage" environment
variable. This allows us to display the bitmap in bmp_logo_bitmap without
having to neither copy it nor specify the "splashimage" environment
variable. The user can implement its own "splash_screen_prepare" in which
he/she sets the "splashimage" environment variable to _logo_bitmap.
This is a kind of "hack" and there should be a way for the user to specify
that he/she wants to display the embedded bmp in the bmp_logo_bitmap array
and, in this case, there is no need for the "splashimage" environment
variable - eIther a config option or some other way to express that this is
the intent of the user.

[PATCH 1/1] Fix the test for CONFIG_CMD_BMP to CONFIG_BMP in order to
display the logo image. In fact, CONFIG_BMP is set to y when CONFIG_CMD_BMP
is set so there should be no compatibility issues. The user should not have
to make the "bmp" cmd available in the u-boot CLI in order to use the
splash screen.

.Best regards
diff --git a/common/splash.c b/common/splash.c
index 6820db683b..2e4c3fa070 100644
--- a/common/splash.c
+++ b/common/splash.c
@@ -164,15 +164,15 @@ int splash_display(void)
int x = 0, y = 0, ret;
if (!CONFIG_IS_ENABLED(SPLASH_SCREEN))
return -ENOSYS;
+   ret = splash_screen_prepare();
+   if (ret)
+   return ret;
+
s = env_get("splashimage");
if (!s)
return -EINVAL;
 
addr = hextoul(s, NULL);
-   ret = splash_screen_prepare();
-   if (ret)
-   return ret;
-
splash_get_pos(, );
 
if (CONFIG_IS_ENABLED(BMP))
diff --git a/common/stdio.c b/common/stdio.c
index e3354f092d..19b99b2e6f 100644
--- a/common/stdio.c
+++ b/common/stdio.c
@@ -352,7 +352,7 @@ int stdio_add_devices(void)
}
}
if (IS_ENABLED(CONFIG_SPLASH_SCREEN) &&
-   IS_ENABLED(CONFIG_CMD_BMP))
+   IS_ENABLED(CONFIG_BMP))
splash_display();
}
 


[PATCH v4 10/10] board: rockchip: add ArmSoM Sige7 Rk3588 board

2024-05-28 Thread Jianfeng Liu
ArmSoM Sige7 is a Rockchip RK3588 based SBC (Single Board Computer) by
ArmSoM.

There are two variants depending on the DRAM size : 8G and 16G.

Specification:

Rockchip Rk3588 SoC
4x ARM Cortex-A76, 4x ARM Cortex-A55
8/16GB memory LPDDR4x
Mali G610MC4 GPU
2x MIPI CSI 2 multiple lanes connector
64GB/128GB on board eMMC
uSD slot
1x USB 2.0 Type-A, 1x USB 3.0 Type-A, 1x USB 3.0 Type-C
1x HDMI 2.1 output
2x 2.5 Gbps Ethernet port
40-pin IO header including UART, SPI and I2C
USB PD over USB Type-C
Size: 92mm x 62mm

Kernel commit:
81c828a67c78 (arm64: dts: rockchip: Add ArmSom Sige7 board)

Signed-off-by: Jianfeng Liu 
---

Changes in v4:
- Pick all commits of rk3588s.dtsi and rk3588.dtsi from v6.10-rc1-dts
- Remove obsolete USB3 DRD nodes in u-boot.dtsi of rk3588s/rk3588
- Remove sdhci and usb nodes from rk3588-armsom-sige7-u-boot.dtsi

Changes in v3:
- Use update-dts-subtree.sh to pick upstream dts

Changes in v2:
- Fix alphabetical order of MAINTAINERS
- Use arch/arm/dts/rk3588-armsom-sige7* in board MAINTAINERS
- Remove spi flash related config
- Move kernel dts to dts/upstream/src/arm64/rockchip/

 MAINTAINERS  |  1 +
 arch/arm/dts/rk3588-armsom-sige7-u-boot.dtsi |  6 ++
 arch/arm/mach-rockchip/rk3588/Kconfig| 26 ++
 board/armsom/sige7-rk3588/Kconfig| 12 +++
 board/armsom/sige7-rk3588/MAINTAINERS|  7 ++
 configs/sige7-rk3588_defconfig   | 93 
 doc/board/rockchip/rockchip.rst  |  1 +
 include/configs/sige7-rk3588.h   | 15 
 8 files changed, 161 insertions(+)
 create mode 100644 arch/arm/dts/rk3588-armsom-sige7-u-boot.dtsi
 create mode 100644 board/armsom/sige7-rk3588/Kconfig
 create mode 100644 board/armsom/sige7-rk3588/MAINTAINERS
 create mode 100644 configs/sige7-rk3588_defconfig
 create mode 100644 include/configs/sige7-rk3588.h

diff --git a/MAINTAINERS b/MAINTAINERS
index 6c861b529df..fffcf898bf1 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -534,6 +534,7 @@ F:  arch/arm/include/asm/arch-rockchip/
 F: arch/arm/mach-rockchip/
 F: board/amarula/vyasa-rk3288/
 F: board/anbernic/rgxx3_rk3566/
+F: board/armsom/sige7-rk3588/
 F: board/chipspark/popmetal_rk3288
 F: board/engicam/px30_core/
 F: board/firefly/
diff --git a/arch/arm/dts/rk3588-armsom-sige7-u-boot.dtsi 
b/arch/arm/dts/rk3588-armsom-sige7-u-boot.dtsi
new file mode 100644
index 000..af96d2fa8fb
--- /dev/null
+++ b/arch/arm/dts/rk3588-armsom-sige7-u-boot.dtsi
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
+/*
+ * Copyright (c) 2024 ArmSoM Technology Co., Ltd.
+ */
+
+#include "rk3588-u-boot.dtsi"
diff --git a/arch/arm/mach-rockchip/rk3588/Kconfig 
b/arch/arm/mach-rockchip/rk3588/Kconfig
index 39049ab35a9..4c14b0be1eb 100644
--- a/arch/arm/mach-rockchip/rk3588/Kconfig
+++ b/arch/arm/mach-rockchip/rk3588/Kconfig
@@ -152,6 +152,31 @@ config TARGET_ROCK5B_RK3588
  USB PD over USB Type-C
  Size: 100mm x 72mm (Pico-ITX form factor)
 
+config TARGET_SIGE7_RK3588
+   bool "ArmSoM Sige7 RK3588 board"
+   select BOARD_LATE_INIT
+   help
+ ArmSoM Sige7 is a Rockchip RK3588 based SBC (Single Board Computer)
+ by ArmSoM.
+
+ There are two variants depending on the DRAM size : 8G and 16G.
+
+ Specification:
+
+ Rockchip Rk3588 SoC
+ 4x ARM Cortex-A76, 4x ARM Cortex-A55
+ 8/16GB memory LPDDR4x
+ Mali G610MC4 GPU
+ 2x MIPI CSI 2 multiple lanes connector
+ 64GB/128GB on board eMMC
+ uSD slot
+ 1x USB 2.0 Type-A, 1x USB 3.0 Type-A, 1x USB 3.0 Type-C
+ 1x HDMI 2.1 output
+ 2x 2.5 Gbps Ethernet port
+ 40-pin IO header including UART, SPI and I2C
+ USB PD over USB Type-C
+ Size: 92mm x 62mm
+
 config TARGET_QUARTZPRO64_RK3588
bool "Pine64 QuartzPro64 RK3588 board"
select BOARD_LATE_INIT
@@ -221,6 +246,7 @@ config ROCKCHIP_COMMON_STACK_ADDR
 config TEXT_BASE
default 0x00a0
 
+source "board/armsom/sige7-rk3588/Kconfig"
 source "board/edgeble/neural-compute-module-6/Kconfig"
 source "board/friendlyelec/nanopc-t6-rk3588/Kconfig"
 source "board/pine64/quartzpro64-rk3588/Kconfig"
diff --git a/board/armsom/sige7-rk3588/Kconfig 
b/board/armsom/sige7-rk3588/Kconfig
new file mode 100644
index 000..793985f531b
--- /dev/null
+++ b/board/armsom/sige7-rk3588/Kconfig
@@ -0,0 +1,12 @@
+if TARGET_SIGE7_RK3588
+
+config SYS_BOARD
+   default "sige7-rk3588"
+
+config SYS_VENDOR
+   default "armsom"
+
+config SYS_CONFIG_NAME
+   default "sige7-rk3588"
+
+endif
diff --git a/board/armsom/sige7-rk3588/MAINTAINERS 
b/board/armsom/sige7-rk3588/MAINTAINERS
new file mode 100644
index 000..0fba39b76c2
--- /dev/null
+++ b/board/armsom/sige7-rk3588/MAINTAINERS
@@ -0,0 +1,7 @@
+SIGE7-RK3588
+M: Jianfeng Liu 
+S: Maintained
+F: 

[PATCH v4 09/10] rockchip: rk3588: Remove USB3 DRD nodes in u-boot.dtsi

2024-05-28 Thread Jianfeng Liu
After we sync USB3 DRD nodes from v6.10-rc1, these obsolete nodes
can be removed.

Signed-off-by: Jianfeng Liu 
---

(no changes since v1)

 arch/arm/dts/rk3588-u-boot.dtsi  | 74 ---
 arch/arm/dts/rk3588s-u-boot.dtsi | 85 
 2 files changed, 159 deletions(-)

diff --git a/arch/arm/dts/rk3588-u-boot.dtsi b/arch/arm/dts/rk3588-u-boot.dtsi
index 4623580c610..bfe6645c30e 100644
--- a/arch/arm/dts/rk3588-u-boot.dtsi
+++ b/arch/arm/dts/rk3588-u-boot.dtsi
@@ -4,77 +4,3 @@
  */
 
 #include "rk3588s-u-boot.dtsi"
-
-/ {
-   usb_host1_xhci: usb@fc40 {
-   compatible = "rockchip,rk3588-dwc3", "snps,dwc3";
-   reg = <0x0 0xfc40 0x0 0x40>;
-   interrupts = ;
-   clocks = < REF_CLK_USB3OTG1>, < SUSPEND_CLK_USB3OTG1>,
-< ACLK_USB3OTG1>;
-   clock-names = "ref_clk", "suspend_clk", "bus_clk";
-   dr_mode = "otg";
-   phys = <_otg>, <_phy1 PHY_TYPE_USB3>;
-   phy-names = "usb2-phy", "usb3-phy";
-   phy_type = "utmi_wide";
-   power-domains = < RK3588_PD_USB>;
-   resets = < SRST_A_USB3OTG1>;
-   snps,dis_enblslpm_quirk;
-   snps,dis-u2-freeclk-exists-quirk;
-   snps,dis-del-phy-power-chg-quirk;
-   snps,dis-tx-ipgap-linecheck-quirk;
-   status = "disabled";
-   };
-
-   usbdpphy1_grf: syscon@fd5cc000 {
-   compatible = "rockchip,rk3588-usbdpphy-grf", "syscon";
-   reg = <0x0 0xfd5cc000 0x0 0x4000>;
-   };
-
-   usb2phy1_grf: syscon@fd5d4000 {
-   compatible = "rockchip,rk3588-usb2phy-grf", "syscon", 
"simple-mfd";
-   reg = <0x0 0xfd5d4000 0x0 0x4000>;
-   #address-cells = <1>;
-   #size-cells = <1>;
-
-   u2phy1: usb2phy@4000 {
-   compatible = "rockchip,rk3588-usb2phy";
-   reg = <0x4000 0x10>;
-   #clock-cells = <0>;
-   clocks = < CLK_USB2PHY_HDPTXRXPHY_REF>;
-   clock-names = "phyclk";
-   clock-output-names = "usb480m_phy1";
-   interrupts = ;
-   resets = < SRST_OTGPHY_U3_1>, < 
SRST_P_USB2PHY_U3_1_GRF0>;
-   reset-names = "phy", "apb";
-   status = "disabled";
-
-   u2phy1_otg: otg-port {
-   #phy-cells = <0>;
-   status = "disabled";
-   };
-   };
-   };
-
-   usbdp_phy1: phy@fed9 {
-   compatible = "rockchip,rk3588-usbdp-phy";
-   reg = <0x0 0xfed9 0x0 0x1>;
-   #phy-cells = <1>;
-   clocks = < CLK_USBDPPHY_MIPIDCPPHY_REF>,
-< CLK_USBDP_PHY1_IMMORTAL>,
-< PCLK_USBDPPHY1>,
-<>;
-   clock-names = "refclk", "immortal", "pclk", "utmi";
-   resets = < SRST_USBDP_COMBO_PHY1_INIT>,
-< SRST_USBDP_COMBO_PHY1_CMN>,
-< SRST_USBDP_COMBO_PHY1_LANE>,
-< SRST_USBDP_COMBO_PHY1_PCS>,
-< SRST_P_USBDPPHY1>;
-   reset-names = "init", "cmn", "lane", "pcs_apb", "pma_apb";
-   rockchip,u2phy-grf = <_grf>;
-   rockchip,usb-grf = <_grf>;
-   rockchip,usbdpphy-grf = <_grf>;
-   rockchip,vo-grf = <_grf>;
-   status = "disabled";
-   };
-};
diff --git a/arch/arm/dts/rk3588s-u-boot.dtsi b/arch/arm/dts/rk3588s-u-boot.dtsi
index e9d38d5c83b..09d8b311cec 100644
--- a/arch/arm/dts/rk3588s-u-boot.dtsi
+++ b/arch/arm/dts/rk3588s-u-boot.dtsi
@@ -19,95 +19,10 @@
bootph-all;
};
 
-   usb_host0_xhci: usb@fc00 {
-   compatible = "rockchip,rk3588-dwc3", "snps,dwc3";
-   reg = <0x0 0xfc00 0x0 0x40>;
-   interrupts = ;
-   clocks = < REF_CLK_USB3OTG0>, < SUSPEND_CLK_USB3OTG0>,
-< ACLK_USB3OTG0>;
-   clock-names = "ref_clk", "suspend_clk", "bus_clk";
-   dr_mode = "otg";
-   phys = <_otg>, <_phy0 PHY_TYPE_USB3>;
-   phy-names = "usb2-phy", "usb3-phy";
-   phy_type = "utmi_wide";
-   power-domains = < RK3588_PD_USB>;
-   resets = < SRST_A_USB3OTG0>;
-   snps,dis_enblslpm_quirk;
-   snps,dis-u1-entry-quirk;
-   snps,dis-u2-entry-quirk;
-   snps,dis-u2-freeclk-exists-quirk;
-   snps,dis-del-phy-power-chg-quirk;
-   snps,dis-tx-ipgap-linecheck-quirk;
-   status = "disabled";
-   };
-
-   vo0_grf: syscon@fd5a6000 {
-   compatible = "rockchip,rk3588-vo-grf", 

[PATCH v4 08/10] arm64: dts: rockchip: Add ArmSom Sige7 board

2024-05-28 Thread Jianfeng Liu
Specification:
Rockchip Rk3588 SoC
4x ARM Cortex-A76, 4x ARM Cortex-A55
8/16/32GB Memory LPDDR4/LPDDR4x
Mali G610MP4 GPU
2× MIPI-CSI Connector
1× MIPI-DSI Connector
1x M.2 Key M (PCIe 3.0 4-lanes)
2x RTL8125 2.5G Ethernet
Onboard AP6275P for WIFI6/BT5
32GB/64GB/128GB eMMC
MicroSD card slot
1x USB2.0, 1x USB3.0 Type-A, 1x US3.0 Type-C
1x HDMI Output, 1x type-C DP Output

Functions work normally:
USB2.0 Host
USB3.0 Type-A Host
M.2 Key M (PCIe 3.0 4-lanes)
2x RTL8125 2.5G Ethernet
eMMC
MicroSD card

More information can be obtained from the following website
https://docs.armsom.org/armsom-sige7

Signed-off-by: Jianfeng Liu 
Reviewed-by: Weizhao Ouyang 
Link: 
https://lore.kernel.org/r/20240420034300.176920-4-liujianfeng1...@gmail.com
Signed-off-by: Heiko Stuebner 

[ upstream commit: 81c828a67c78bb03ea75819c417c93c7f3d637b5 ]

(cherry picked from commit d427a11542bcf5364a5260280e077f0a2e030dcb)
---

(no changes since v1)

 .../arm64/rockchip/rk3588-armsom-sige7.dts| 721 ++
 1 file changed, 721 insertions(+)
 create mode 100644 dts/upstream/src/arm64/rockchip/rk3588-armsom-sige7.dts

diff --git a/dts/upstream/src/arm64/rockchip/rk3588-armsom-sige7.dts 
b/dts/upstream/src/arm64/rockchip/rk3588-armsom-sige7.dts
new file mode 100644
index 000..98c622b2764
--- /dev/null
+++ b/dts/upstream/src/arm64/rockchip/rk3588-armsom-sige7.dts
@@ -0,0 +1,721 @@
+// SPDX-License-Identifier: (GPL-2.0+ OR MIT)
+
+/dts-v1/;
+
+#include 
+#include 
+#include "rk3588.dtsi"
+
+/ {
+   model = "ArmSoM Sige7";
+   compatible = "armsom,sige7", "rockchip,rk3588";
+
+   aliases {
+   mmc0 = 
+   mmc1 = 
+   };
+
+   chosen {
+   stdout-path = "serial2:150n8";
+   };
+
+   analog-sound {
+   compatible = "audio-graph-card";
+   dais = <_8ch_p0>;
+   label = "rk3588-es8316";
+   hp-det-gpio = < RK_PD5 GPIO_ACTIVE_HIGH>;
+   pinctrl-names = "default";
+   pinctrl-0 = <_detect>;
+   routing = "MIC2", "Mic Jack",
+ "Headphones", "HPOL",
+ "Headphones", "HPOR";
+   widgets = "Microphone", "Mic Jack",
+ "Headphone", "Headphones";
+   };
+
+   leds {
+   compatible = "gpio-leds";
+   pinctrl-names = "default";
+   pinctrl-0 = <_rgb_g>;
+
+   led_green: led-0 {
+   color = ;
+   function = LED_FUNCTION_STATUS;
+   gpios = < RK_PB7 GPIO_ACTIVE_HIGH>;
+   linux,default-trigger = "heartbeat";
+   };
+
+   led_red: led-1 {
+   color = ;
+   function = LED_FUNCTION_STATUS;
+   gpios = < RK_PC5 GPIO_ACTIVE_HIGH>;
+   linux,default-trigger = "none";
+   };
+   };
+
+   fan: pwm-fan {
+   compatible = "pwm-fan";
+   cooling-levels = <0 95 145 195 255>;
+   fan-supply = <_sys>;
+   pwms = < 0 5 0>;
+   #cooling-cells = <2>;
+   };
+
+   vcc3v3_pcie2x1l2: vcc3v3-pcie2x1l2-regulator {
+   compatible = "regulator-fixed";
+   regulator-name = "vcc3v3_pcie2x1l2";
+   regulator-min-microvolt = <330>;
+   regulator-max-microvolt = <330>;
+   startup-delay-us = <5000>;
+   vin-supply = <_3v3_s3>;
+   };
+
+   vcc3v3_pcie30: vcc3v3-pcie30-regulator {
+   compatible = "regulator-fixed";
+   enable-active-high;
+   gpios = < RK_PA4 GPIO_ACTIVE_HIGH>;
+   regulator-name = "vcc3v3_pcie30";
+   regulator-min-microvolt = <330>;
+   regulator-max-microvolt = <330>;
+   startup-delay-us = <5000>;
+   vin-supply = <_sys>;
+   };
+
+   vcc5v0_host: vcc5v0-host-regulator {
+   compatible = "regulator-fixed";
+   regulator-name = "vcc5v0_host";
+   regulator-boot-on;
+   regulator-always-on;
+   regulator-min-microvolt = <500>;
+   regulator-max-microvolt = <500>;
+   enable-active-high;
+   gpio = < RK_PB0 GPIO_ACTIVE_HIGH>;
+   pinctrl-names = "default";
+   pinctrl-0 = <_host_en>;
+   vin-supply = <_sys>;
+   };
+
+   vcc5v0_sys: vcc5v0-sys-regulator {
+   compatible = "regulator-fixed";
+   regulator-name = "vcc5v0_sys";
+   regulator-always-on;
+   regulator-boot-on;
+   regulator-min-microvolt = <500>;
+   

[PATCH v4 07/10] arm64: dts: rockchip: add rk3588 pcie and php IOMMUs

2024-05-28 Thread Jianfeng Liu
From: Niklas Cassel 

The mmu600_pcie is connected with the five PCIe controllers.
The mmu600_php is connected with the USB3 controller, the GMAC
controllers, and the SATA controllers.

See 8.2 Block Diagram, in rk3588 TRM (Technical Reference Manual).

The IOMMUs are disabled by default, as further patches are needed to
program the SID/SSIDs in to the IOMMUs.

iommu: Default domain type: Translated
iommu: DMA domain TLB invalidation policy: strict mode
arm-smmu-v3 fc90.iommu: ias 48-bit, oas 48-bit (features 0x001c1eaf)
arm-smmu-v3 fc90.iommu: allocated 65536 entries for cmdq
arm-smmu-v3 fc90.iommu: allocated 32768 entries for evtq
arm-smmu-v3 fc90.iommu: msi_domain absent - falling back to wired irqs

Additionally, the IOMMU correctly triggers an IOMMU fault when
a PCIe device performs a write (since the device hasn't been
assigned a SID/SSID):
arm-smmu-v3 fc90.iommu: event 0x02 received:
arm-smmu-v3 fc90.iommu:  0x0102
arm-smmu-v3 fc90.iommu:  0x
arm-smmu-v3 fc90.iommu:  0x
arm-smmu-v3 fc90.iommu:  0x

While this doesn't provide much value as is, having the devices as
disabled in the device tree will allow developers to see that the rk3588
actually has IOMMUs on the SoC.

Signed-off-by: Niklas Cassel 
Link: https://lore.kernel.org/r/20240502140231.477049-2-cas...@kernel.org
Signed-off-by: Heiko Stuebner 

[ upstream commit: cd81d3a0695cc54ad6ac0ef4bbb67a7c8f55d592 ]

(cherry picked from commit ea9a34aa0d786cbf4b87f1ba528e69b07219738f)
Signed-off-by: Jianfeng Liu 
---

(no changes since v1)

 dts/upstream/src/arm64/rockchip/rk3588s.dtsi | 24 
 1 file changed, 24 insertions(+)

diff --git a/dts/upstream/src/arm64/rockchip/rk3588s.dtsi 
b/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
index b0a59ec5183..6ac5ac8b48a 100644
--- a/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
+++ b/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
@@ -579,6 +579,30 @@
status = "disabled";
};
 
+   mmu600_pcie: iommu@fc90 {
+   compatible = "arm,smmu-v3";
+   reg = <0x0 0xfc90 0x0 0x20>;
+   interrupts = ,
+,
+,
+;
+   interrupt-names = "eventq", "gerror", "priq", "cmdq-sync";
+   #iommu-cells = <1>;
+   status = "disabled";
+   };
+
+   mmu600_php: iommu@fcb0 {
+   compatible = "arm,smmu-v3";
+   reg = <0x0 0xfcb0 0x0 0x20>;
+   interrupts = ,
+,
+,
+;
+   interrupt-names = "eventq", "gerror", "priq", "cmdq-sync";
+   #iommu-cells = <1>;
+   status = "disabled";
+   };
+
pmu1grf: syscon@fd58a000 {
compatible = "rockchip,rk3588-pmugrf", "syscon", "simple-mfd";
reg = <0x0 0xfd58a000 0x0 0x1>;
-- 
2.34.1



[PATCH v4 06/10] arm64: dts: rockchip: add USB3 DRD controllers on rk3588

2024-05-28 Thread Jianfeng Liu
From: Sebastian Reichel 

Add both USB3 dual-role controllers to the RK3588 devicetree.

Signed-off-by: Sebastian Reichel 
Link: 
https://lore.kernel.org/r/20240408225109.128953-8-sebastian.reic...@collabora.com
Signed-off-by: Heiko Stuebner 

[ upstream commit: 33f393a2a990e16f56931ca708295f31d2b44415 ]

(cherry picked from commit c7ed588e14f7dd04a92fb55f12680f94c7b14edf)
Signed-off-by: Jianfeng Liu 
---

(no changes since v1)

 dts/upstream/src/arm64/rockchip/rk3588.dtsi  | 20 ++
 dts/upstream/src/arm64/rockchip/rk3588s.dtsi | 22 
 2 files changed, 42 insertions(+)

diff --git a/dts/upstream/src/arm64/rockchip/rk3588.dtsi 
b/dts/upstream/src/arm64/rockchip/rk3588.dtsi
index 4fdd047c9eb..5984016b5f9 100644
--- a/dts/upstream/src/arm64/rockchip/rk3588.dtsi
+++ b/dts/upstream/src/arm64/rockchip/rk3588.dtsi
@@ -7,6 +7,26 @@
 #include "rk3588-pinctrl.dtsi"
 
 / {
+   usb_host1_xhci: usb@fc40 {
+   compatible = "rockchip,rk3588-dwc3", "snps,dwc3";
+   reg = <0x0 0xfc40 0x0 0x40>;
+   interrupts = ;
+   clocks = < REF_CLK_USB3OTG1>, < SUSPEND_CLK_USB3OTG1>,
+< ACLK_USB3OTG1>;
+   clock-names = "ref_clk", "suspend_clk", "bus_clk";
+   dr_mode = "otg";
+   phys = <_otg>, <_phy1 PHY_TYPE_USB3>;
+   phy-names = "usb2-phy", "usb3-phy";
+   phy_type = "utmi_wide";
+   power-domains = < RK3588_PD_USB>;
+   resets = < SRST_A_USB3OTG1>;
+   snps,dis_enblslpm_quirk;
+   snps,dis-u2-freeclk-exists-quirk;
+   snps,dis-del-phy-power-chg-quirk;
+   snps,dis-tx-ipgap-linecheck-quirk;
+   status = "disabled";
+   };
+
pcie30_phy_grf: syscon@fd5b8000 {
compatible = "rockchip,rk3588-pcie3-phy-grf", "syscon";
reg = <0x0 0xfd5b8000 0x0 0x1>;
diff --git a/dts/upstream/src/arm64/rockchip/rk3588s.dtsi 
b/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
index 9063c0bb0f0..b0a59ec5183 100644
--- a/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
+++ b/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
@@ -492,6 +492,28 @@
};
};
 
+   usb_host0_xhci: usb@fc00 {
+   compatible = "rockchip,rk3588-dwc3", "snps,dwc3";
+   reg = <0x0 0xfc00 0x0 0x40>;
+   interrupts = ;
+   clocks = < REF_CLK_USB3OTG0>, < SUSPEND_CLK_USB3OTG0>,
+< ACLK_USB3OTG0>;
+   clock-names = "ref_clk", "suspend_clk", "bus_clk";
+   dr_mode = "otg";
+   phys = <_otg>, <_phy0 PHY_TYPE_USB3>;
+   phy-names = "usb2-phy", "usb3-phy";
+   phy_type = "utmi_wide";
+   power-domains = < RK3588_PD_USB>;
+   resets = < SRST_A_USB3OTG0>;
+   snps,dis_enblslpm_quirk;
+   snps,dis-u1-entry-quirk;
+   snps,dis-u2-entry-quirk;
+   snps,dis-u2-freeclk-exists-quirk;
+   snps,dis-del-phy-power-chg-quirk;
+   snps,dis-tx-ipgap-linecheck-quirk;
+   status = "disabled";
+   };
+
usb_host0_ehci: usb@fc80 {
compatible = "rockchip,rk3588-ehci", "generic-ehci";
reg = <0x0 0xfc80 0x0 0x4>;
-- 
2.34.1



[PATCH v4 05/10] arm64: dts: rockchip: add USBDP phys on rk3588

2024-05-28 Thread Jianfeng Liu
From: Sebastian Reichel 

Add both USB3-DisplayPort PHYs to RK3588 SoC DT.

Signed-off-by: Sebastian Reichel 
Link: 
https://lore.kernel.org/r/20240408225109.128953-7-sebastian.reic...@collabora.com
Signed-off-by: Heiko Stuebner 

[ upstream commit: e18e5e8188f2671abf63abe7db5f21555705130f ]

(cherry picked from commit 5110caca9865718616cf7093ed4a9a1bc54780db)
Signed-off-by: Jianfeng Liu 
---

(no changes since v1)

 dts/upstream/src/arm64/rockchip/rk3588.dtsi  | 52 
 dts/upstream/src/arm64/rockchip/rk3588s.dtsi | 63 
 2 files changed, 115 insertions(+)

diff --git a/dts/upstream/src/arm64/rockchip/rk3588.dtsi 
b/dts/upstream/src/arm64/rockchip/rk3588.dtsi
index 5519c1430cb..4fdd047c9eb 100644
--- a/dts/upstream/src/arm64/rockchip/rk3588.dtsi
+++ b/dts/upstream/src/arm64/rockchip/rk3588.dtsi
@@ -17,6 +17,36 @@
reg = <0x0 0xfd5c 0x0 0x100>;
};
 
+   usbdpphy1_grf: syscon@fd5cc000 {
+   compatible = "rockchip,rk3588-usbdpphy-grf", "syscon";
+   reg = <0x0 0xfd5cc000 0x0 0x4000>;
+   };
+
+   usb2phy1_grf: syscon@fd5d4000 {
+   compatible = "rockchip,rk3588-usb2phy-grf", "syscon", 
"simple-mfd";
+   reg = <0x0 0xfd5d4000 0x0 0x4000>;
+   #address-cells = <1>;
+   #size-cells = <1>;
+
+   u2phy1: usb2phy@4000 {
+   compatible = "rockchip,rk3588-usb2phy";
+   reg = <0x4000 0x10>;
+   #clock-cells = <0>;
+   clocks = < CLK_USB2PHY_HDPTXRXPHY_REF>;
+   clock-names = "phyclk";
+   clock-output-names = "usb480m_phy1";
+   interrupts = ;
+   resets = < SRST_OTGPHY_U3_1>, < 
SRST_P_USB2PHY_U3_1_GRF0>;
+   reset-names = "phy", "apb";
+   status = "disabled";
+
+   u2phy1_otg: otg-port {
+   #phy-cells = <0>;
+   status = "disabled";
+   };
+   };
+   };
+
i2s8_8ch: i2s@fddc8000 {
compatible = "rockchip,rk3588-i2s-tdm";
reg = <0x0 0xfddc8000 0x0 0x1000>;
@@ -310,6 +340,28 @@
};
};
 
+   usbdp_phy1: phy@fed9 {
+   compatible = "rockchip,rk3588-usbdp-phy";
+   reg = <0x0 0xfed9 0x0 0x1>;
+   #phy-cells = <1>;
+   clocks = < CLK_USBDPPHY_MIPIDCPPHY_REF>,
+< CLK_USBDP_PHY1_IMMORTAL>,
+< PCLK_USBDPPHY1>,
+<>;
+   clock-names = "refclk", "immortal", "pclk", "utmi";
+   resets = < SRST_USBDP_COMBO_PHY1_INIT>,
+< SRST_USBDP_COMBO_PHY1_CMN>,
+< SRST_USBDP_COMBO_PHY1_LANE>,
+< SRST_USBDP_COMBO_PHY1_PCS>,
+< SRST_P_USBDPPHY1>;
+   reset-names = "init", "cmn", "lane", "pcs_apb", "pma_apb";
+   rockchip,u2phy-grf = <_grf>;
+   rockchip,usb-grf = <_grf>;
+   rockchip,usbdpphy-grf = <_grf>;
+   rockchip,vo-grf = <_grf>;
+   status = "disabled";
+   };
+
combphy1_ps: phy@fee1 {
compatible = "rockchip,rk3588-naneng-combphy";
reg = <0x0 0xfee1 0x0 0x100>;
diff --git a/dts/upstream/src/arm64/rockchip/rk3588s.dtsi 
b/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
index 58d12969b7e..9063c0bb0f0 100644
--- a/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
+++ b/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
@@ -572,12 +572,23 @@
reg = <0x0 0xfd5a4000 0x0 0x2000>;
};
 
+   vo0_grf: syscon@fd5a6000 {
+   compatible = "rockchip,rk3588-vo-grf", "syscon";
+   reg = <0x0 0xfd5a6000 0x0 0x2000>;
+   clocks = < PCLK_VO0GRF>;
+   };
+
vo1_grf: syscon@fd5a8000 {
compatible = "rockchip,rk3588-vo-grf", "syscon";
reg = <0x0 0xfd5a8000 0x0 0x100>;
clocks = < PCLK_VO1GRF>;
};
 
+   usb_grf: syscon@fd5ac000 {
+   compatible = "rockchip,rk3588-usb-grf", "syscon";
+   reg = <0x0 0xfd5ac000 0x0 0x4000>;
+   };
+
php_grf: syscon@fd5b {
compatible = "rockchip,rk3588-php-grf", "syscon";
reg = <0x0 0xfd5b 0x0 0x1000>;
@@ -593,6 +604,36 @@
reg = <0x0 0xfd5c4000 0x0 0x100>;
};
 
+   usbdpphy0_grf: syscon@fd5c8000 {
+   compatible = "rockchip,rk3588-usbdpphy-grf", "syscon";
+   reg = <0x0 0xfd5c8000 0x0 0x4000>;
+   };
+
+   usb2phy0_grf: syscon@fd5d {
+   compatible = "rockchip,rk3588-usb2phy-grf", "syscon", 
"simple-mfd";
+   reg = <0x0 0xfd5d 0x0 0x4000>;
+   

[PATCH v4 04/10] arm64: dts: rockchip: reorder usb2phy properties for rk3588

2024-05-28 Thread Jianfeng Liu
From: Sebastian Reichel 

Reorder common DT properties alphabetically for usb2phy, according
to latest DT style rules.

Signed-off-by: Sebastian Reichel 
Link: 
https://lore.kernel.org/r/20240408225109.128953-6-sebastian.reic...@collabora.com
Signed-off-by: Heiko Stuebner 

[ upstream commit: abe68e0ca71dddce0e5419e35507cb464d61870d ]

(cherry picked from commit f6835a60a8a28ff14ffb3dd80c99ce1c137d06c5)
Signed-off-by: Jianfeng Liu 
---

(no changes since v1)

 dts/upstream/src/arm64/rockchip/rk3588s.dtsi | 16 
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/dts/upstream/src/arm64/rockchip/rk3588s.dtsi 
b/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
index 87df0902273..58d12969b7e 100644
--- a/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
+++ b/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
@@ -602,13 +602,13 @@
u2phy2: usb2phy@8000 {
compatible = "rockchip,rk3588-usb2phy";
reg = <0x8000 0x10>;
-   interrupts = ;
-   resets = < SRST_OTGPHY_U2_0>, < 
SRST_P_USB2PHY_U2_0_GRF0>;
-   reset-names = "phy", "apb";
+   #clock-cells = <0>;
clocks = < CLK_USB2PHY_HDPTXRXPHY_REF>;
clock-names = "phyclk";
clock-output-names = "usb480m_phy2";
-   #clock-cells = <0>;
+   interrupts = ;
+   resets = < SRST_OTGPHY_U2_0>, < 
SRST_P_USB2PHY_U2_0_GRF0>;
+   reset-names = "phy", "apb";
status = "disabled";
 
u2phy2_host: host-port {
@@ -627,13 +627,13 @@
u2phy3: usb2phy@c000 {
compatible = "rockchip,rk3588-usb2phy";
reg = <0xc000 0x10>;
-   interrupts = ;
-   resets = < SRST_OTGPHY_U2_1>, < 
SRST_P_USB2PHY_U2_1_GRF0>;
-   reset-names = "phy", "apb";
+   #clock-cells = <0>;
clocks = < CLK_USB2PHY_HDPTXRXPHY_REF>;
clock-names = "phyclk";
clock-output-names = "usb480m_phy3";
-   #clock-cells = <0>;
+   interrupts = ;
+   resets = < SRST_OTGPHY_U2_1>, < 
SRST_P_USB2PHY_U2_1_GRF0>;
+   reset-names = "phy", "apb";
status = "disabled";
 
u2phy3_host: host-port {
-- 
2.34.1



[PATCH v4 03/10] arm64: dts: rockchip: fix usb2phy nodename for rk3588

2024-05-28 Thread Jianfeng Liu
From: Sebastian Reichel 

usb2-phy should be named usb2phy according to the DT binding,
so let's fix it up accordingly.

Signed-off-by: Sebastian Reichel 
Link: 
https://lore.kernel.org/r/20240408225109.128953-5-sebastian.reic...@collabora.com
Signed-off-by: Heiko Stuebner 

[ upstream commit: 4e07a95f7402de092cd71b2cb96c69f85c98f251 ]

(cherry picked from commit 5a3e4638492497ae81b9bd4a8627f4727e312ccc)
Signed-off-by: Jianfeng Liu 
---

(no changes since v1)

 dts/upstream/src/arm64/rockchip/rk3588s.dtsi | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/dts/upstream/src/arm64/rockchip/rk3588s.dtsi 
b/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
index ac5bd630f15..87df0902273 100644
--- a/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
+++ b/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
@@ -599,7 +599,7 @@
#address-cells = <1>;
#size-cells = <1>;
 
-   u2phy2: usb2-phy@8000 {
+   u2phy2: usb2phy@8000 {
compatible = "rockchip,rk3588-usb2phy";
reg = <0x8000 0x10>;
interrupts = ;
@@ -624,7 +624,7 @@
#address-cells = <1>;
#size-cells = <1>;
 
-   u2phy3: usb2-phy@c000 {
+   u2phy3: usb2phy@c000 {
compatible = "rockchip,rk3588-usb2phy";
reg = <0xc000 0x10>;
interrupts = ;
-- 
2.34.1



[PATCH v4 02/10] arm64: dts: rockchip: Fix ordering of nodes on rk3588s

2024-05-28 Thread Jianfeng Liu
From: Diederik de Haas 

Fix the ordering of the main nodes by sorting them alphabetically and
then the ones with a memory address sequentially by that address.

Signed-off-by: Diederik de Haas 
Link: https://lore.kernel.org/r/20240406172821.34173-1-didi.deb...@cknow.org
Signed-off-by: Heiko Stuebner 

[ upstream commit: cbb97fe18e299ece1c0074924c630de6a19b320f ]

(cherry picked from commit bbf7c16f2f1208b96349f6f6648b69cfaa1a482b)
Signed-off-by: Jianfeng Liu 
---

(no changes since v1)

 dts/upstream/src/arm64/rockchip/rk3588s.dtsi | 304 +--
 1 file changed, 152 insertions(+), 152 deletions(-)

diff --git a/dts/upstream/src/arm64/rockchip/rk3588s.dtsi 
b/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
index 89d40cff635..ac5bd630f15 100644
--- a/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
+++ b/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
@@ -347,6 +347,11 @@
};
};
 
+   display_subsystem: display-subsystem {
+   compatible = "rockchip,display-subsystem";
+   ports = <_out>;
+   };
+
firmware {
optee: optee {
compatible = "linaro,optee-tz";
@@ -394,11 +399,6 @@
#clock-cells = <0>;
};
 
-   display_subsystem: display-subsystem {
-   compatible = "rockchip,display-subsystem";
-   ports = <_out>;
-   };
-
timer {
compatible = "arm,armv8-timer";
interrupts = ,
@@ -436,6 +436,62 @@
};
};
 
+   gpu: gpu@fb00 {
+   compatible = "rockchip,rk3588-mali", "arm,mali-valhall-csf";
+   reg = <0x0 0xfb00 0x0 0x20>;
+   #cooling-cells = <2>;
+   assigned-clocks = <_clk SCMI_CLK_GPU>;
+   assigned-clock-rates = <2>;
+   clocks = < CLK_GPU>, < CLK_GPU_COREGROUP>,
+< CLK_GPU_STACKS>;
+   clock-names = "core", "coregroup", "stacks";
+   dynamic-power-coefficient = <2982>;
+   interrupts = ,
+,
+;
+   interrupt-names = "job", "mmu", "gpu";
+   operating-points-v2 = <_opp_table>;
+   power-domains = < RK3588_PD_GPU>;
+   status = "disabled";
+
+   gpu_opp_table: opp-table {
+   compatible = "operating-points-v2";
+
+   opp-3 {
+   opp-hz = /bits/ 64 <3>;
+   opp-microvolt = <675000 675000 85>;
+   };
+   opp-4 {
+   opp-hz = /bits/ 64 <4>;
+   opp-microvolt = <675000 675000 85>;
+   };
+   opp-5 {
+   opp-hz = /bits/ 64 <5>;
+   opp-microvolt = <675000 675000 85>;
+   };
+   opp-6 {
+   opp-hz = /bits/ 64 <6>;
+   opp-microvolt = <675000 675000 85>;
+   };
+   opp-7 {
+   opp-hz = /bits/ 64 <7>;
+   opp-microvolt = <70 70 85>;
+   };
+   opp-8 {
+   opp-hz = /bits/ 64 <8>;
+   opp-microvolt = <75 75 85>;
+   };
+   opp-9 {
+   opp-hz = /bits/ 64 <9>;
+   opp-microvolt = <80 80 85>;
+   };
+   opp-10 {
+   opp-hz = /bits/ 64 <10>;
+   opp-microvolt = <85 85 85>;
+   };
+   };
+   };
+
usb_host0_ehci: usb@fc80 {
compatible = "rockchip,rk3588-ehci", "generic-ehci";
reg = <0x0 0xfc80 0x0 0x4>;
@@ -501,62 +557,6 @@
status = "disabled";
};
 
-   gpu: gpu@fb00 {
-   compatible = "rockchip,rk3588-mali", "arm,mali-valhall-csf";
-   reg = <0x0 0xfb00 0x0 0x20>;
-   #cooling-cells = <2>;
-   assigned-clocks = <_clk SCMI_CLK_GPU>;
-   assigned-clock-rates = <2>;
-   clocks = < CLK_GPU>, < CLK_GPU_COREGROUP>,
-< CLK_GPU_STACKS>;
-   clock-names = "core", "coregroup", "stacks";
-   dynamic-power-coefficient = <2982>;
-   interrupts = ,
-,
-;
-   interrupt-names = "job", 

[PATCH v4 01/10] arm64: dts: rockchip: Add rk3588 GPU node

2024-05-28 Thread Jianfeng Liu
From: Boris Brezillon 

Add Mali GPU Node to the RK3588 SoC DT including GPU clock
operating points

Signed-off-by: Boris Brezillon 
Signed-off-by: Sebastian Reichel 
Link: 
https://lore.kernel.org/r/20240326165232.73585-3-sebastian.reic...@collabora.com
Signed-off-by: Heiko Stuebner 

[ upstream commit: 6fca4edb93d335f29f81e484936f38a5eed6a9b1 ]

(cherry picked from commit 3cd15354ea0c8668812bc0b3a4136606c10803e9)
Signed-off-by: Jianfeng Liu 
---

(no changes since v1)

 dts/upstream/src/arm64/rockchip/rk3588s.dtsi | 56 
 1 file changed, 56 insertions(+)

diff --git a/dts/upstream/src/arm64/rockchip/rk3588s.dtsi 
b/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
index 87b83c87bd5..89d40cff635 100644
--- a/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
+++ b/dts/upstream/src/arm64/rockchip/rk3588s.dtsi
@@ -501,6 +501,62 @@
status = "disabled";
};
 
+   gpu: gpu@fb00 {
+   compatible = "rockchip,rk3588-mali", "arm,mali-valhall-csf";
+   reg = <0x0 0xfb00 0x0 0x20>;
+   #cooling-cells = <2>;
+   assigned-clocks = <_clk SCMI_CLK_GPU>;
+   assigned-clock-rates = <2>;
+   clocks = < CLK_GPU>, < CLK_GPU_COREGROUP>,
+< CLK_GPU_STACKS>;
+   clock-names = "core", "coregroup", "stacks";
+   dynamic-power-coefficient = <2982>;
+   interrupts = ,
+,
+;
+   interrupt-names = "job", "mmu", "gpu";
+   operating-points-v2 = <_opp_table>;
+   power-domains = < RK3588_PD_GPU>;
+   status = "disabled";
+
+   gpu_opp_table: opp-table {
+   compatible = "operating-points-v2";
+
+   opp-3 {
+   opp-hz = /bits/ 64 <3>;
+   opp-microvolt = <675000 675000 85>;
+   };
+   opp-4 {
+   opp-hz = /bits/ 64 <4>;
+   opp-microvolt = <675000 675000 85>;
+   };
+   opp-5 {
+   opp-hz = /bits/ 64 <5>;
+   opp-microvolt = <675000 675000 85>;
+   };
+   opp-6 {
+   opp-hz = /bits/ 64 <6>;
+   opp-microvolt = <675000 675000 85>;
+   };
+   opp-7 {
+   opp-hz = /bits/ 64 <7>;
+   opp-microvolt = <70 70 85>;
+   };
+   opp-8 {
+   opp-hz = /bits/ 64 <8>;
+   opp-microvolt = <75 75 85>;
+   };
+   opp-9 {
+   opp-hz = /bits/ 64 <9>;
+   opp-microvolt = <80 80 85>;
+   };
+   opp-10 {
+   opp-hz = /bits/ 64 <10>;
+   opp-microvolt = <85 85 85>;
+   };
+   };
+   };
+
pmu1grf: syscon@fd58a000 {
compatible = "rockchip,rk3588-pmugrf", "syscon", "simple-mfd";
reg = <0x0 0xfd58a000 0x0 0x1>;
-- 
2.34.1



[PATCH v4 00/10] add ArmSoM Sige7 Rk3588 board

2024-05-28 Thread Jianfeng Liu
This series also introduces dts commits of rk3588s.dtsi and rk3588.dtsi
from v6.10-rc1-dts.
Then dts of ArmSoM Sige7 is picked from upstream.
I also remove obsolete usb3 nodes of rk3588-u-boot.dtsi and
rk3588-u-boot.dtsi.

Changes in v4:
- Pick all commits of rk3588s.dtsi and rk3588.dtsi from v6.10-rc1-dts
- Remove obsolete USB3 DRD nodes in u-boot.dtsi of rk3588s/rk3588
- Remove sdhci and usb nodes from rk3588-armsom-sige7-u-boot.dtsi

Changes in v3:
- Use update-dts-subtree.sh to pick upstream dts

Changes in v2:
- Fix alphabetical order of MAINTAINERS
- Use arch/arm/dts/rk3588-armsom-sige7* in board MAINTAINERS
- Remove spi flash related config
- Move kernel dts to dts/upstream/src/arm64/rockchip/

Boris Brezillon (1):
  arm64: dts: rockchip: Add rk3588 GPU node

Diederik de Haas (1):
  arm64: dts: rockchip: Fix ordering of nodes on rk3588s

Jianfeng Liu (3):
  arm64: dts: rockchip: Add ArmSom Sige7 board
  rockchip: rk3588: Remove USB3 DRD nodes in u-boot.dtsi
  board: rockchip: add ArmSoM Sige7 Rk3588 board

Niklas Cassel (1):
  arm64: dts: rockchip: add rk3588 pcie and php IOMMUs

Sebastian Reichel (4):
  arm64: dts: rockchip: fix usb2phy nodename for rk3588
  arm64: dts: rockchip: reorder usb2phy properties for rk3588
  arm64: dts: rockchip: add USBDP phys on rk3588
  arm64: dts: rockchip: add USB3 DRD controllers on rk3588

 MAINTAINERS   |   1 +
 arch/arm/dts/rk3588-armsom-sige7-u-boot.dtsi  |   6 +
 arch/arm/dts/rk3588-u-boot.dtsi   |  74 --
 arch/arm/dts/rk3588s-u-boot.dtsi  |  85 ---
 arch/arm/mach-rockchip/rk3588/Kconfig |  26 +
 board/armsom/sige7-rk3588/Kconfig |  12 +
 board/armsom/sige7-rk3588/MAINTAINERS |   7 +
 configs/sige7-rk3588_defconfig|  93 +++
 doc/board/rockchip/rockchip.rst   |   1 +
 .../arm64/rockchip/rk3588-armsom-sige7.dts| 721 ++
 dts/upstream/src/arm64/rockchip/rk3588.dtsi   |  72 ++
 dts/upstream/src/arm64/rockchip/rk3588s.dtsi  | 377 ++---
 include/configs/sige7-rk3588.h|  15 +
 13 files changed, 1225 insertions(+), 265 deletions(-)
 create mode 100644 arch/arm/dts/rk3588-armsom-sige7-u-boot.dtsi
 create mode 100644 board/armsom/sige7-rk3588/Kconfig
 create mode 100644 board/armsom/sige7-rk3588/MAINTAINERS
 create mode 100644 configs/sige7-rk3588_defconfig
 create mode 100644 dts/upstream/src/arm64/rockchip/rk3588-armsom-sige7.dts
 create mode 100644 include/configs/sige7-rk3588.h

-- 
2.34.1



Re: [PATCH v5] test/py: net_boot: Add test cases for net boot

2024-05-28 Thread Tom Rini
On Tue, May 28, 2024 at 02:33:17PM +0530, Love Kumar wrote:

> Add tests for booting image using tftpboot/pxe boot commands, tftpboot
> boot case loads the FIT image into DDR and boots using bootm command
> whereas pxe boot cases downloads the pxe configuration file from the
> TFTP server and interprets it to boot the images mentioned in the pxe
> configurations file.
> This test relies on boardenv_* containing configuration values including
> the parameter 'pattern'. tftpboot/pxe boot cases boots the Linux till the
> boot log pattern value is matched. For example, if the parameter
> 'pattern' is defined as 'login:', it will boot till login prompt.

Good news is that the kernel test is working for me now. Bad news is the
PXE test fails, but good news is I figure out the problem.

[snip]
> +def setup_tftpboot_boot(u_boot_console):
> +f = u_boot_console.config.env.get('env__net_tftp_bootable_file', None)
> +if not f:
> +pytest.skip('No TFTP bootable file to read')
> +
> +test_net.test_net_dhcp(u_boot_console)
> +if not test_net.net_set_up:
> +test_net.test_net_setup_static(u_boot_console)

This works for me, and matches how other tests are written. However:

[snip]
> +def setup_pxe_boot(u_boot_console):
> +f = u_boot_console.config.env.get('env__net_pxe_bootable_file', None)
> +if not f:
> +pytest.skip('No PXE bootable file to read')
> +
> +test_net.test_net_dhcp(u_boot_console)
> +test_net.test_net_setup_static(u_boot_console)
> +return f

This doesn't check for if dhcp worked and skips the test when DHCP works
but there's no static config, which is how my lab is. For v6 we should
have a setup_networking function that both the TFTP and PXE tests call
in to that does what I suggested above. And what I suggested above
would be a good clean-up to the xilinx-specific tests.

That said, with a local fixup so that my network is configured, and
having pxelinux.cfg/01-b8-27-eb-fc-64-a6 (the first file pxe boot looks
for on my test Pi), pxeboot.cfg/default and pxeboot.cfg/default-arm all
configured from the examples given in the test itself, I get:
 Captured stdout call 
-
U-Boot> setenv autoload no
U-Boot> U-Boot> dhcp
Waiting for Ethernet connection... done.
BOOTP broadcast 1
BOOTP broadcast 2
DHCP client bound to address 192.168.1.100 (255 ms)
U-Boot> U-Boot> pxe get
missing environment variable: pxeuuid
Retrieving file: pxelinux.cfg/01-b8-27-eb-fc-64-a6
Waiting for Ethernet connection... done.
Using smsc95xx_eth device
TFTP from server 192.168.1.10; our IP address is 192.168.1.100
Filename 'pxelinux.cfg/01-b8-27-eb-fc-64-a6'.
Load address: 0x250
Loading: ##  64 Bytes
 0 Bytes/s
done
Bytes transferred = 64 (40 hex)
Config file '' found
U-Boot>
=== short test summary info 
===

-- 
Tom


signature.asc
Description: PGP signature


[PATCH 2/2] imx8mm-cl-iot-gate: Add support for the Realtek RTL8211E PHY

2024-05-28 Thread Fabio Estevam
From: Fabio Estevam 

Newer imx8mm-cl-iot-gate versions are populated with a Realtek RTL8211E
PHY instead of the Atheros AR8033.

Adapted Compulab's patch from:
https://github.com/compulab-yokneam/meta-bsp-imx8mm/blob/iot-gate-imx8_5.10.72/recipes-bsp/u-boot/compulab/imx8mm/0125-imx8mm-net-enable-phy-Realtek-RTL8211E.patch

to support both PHYs in U-Boot.

Signed-off-by: Fabio Estevam 
---
 .../imx8mm-cl-iot-gate/imx8mm-cl-iot-gate.c   | 106 +-
 include/configs/imx8mm-cl-iot-gate.h  |   2 +-
 2 files changed, 104 insertions(+), 4 deletions(-)

diff --git a/board/compulab/imx8mm-cl-iot-gate/imx8mm-cl-iot-gate.c 
b/board/compulab/imx8mm-cl-iot-gate/imx8mm-cl-iot-gate.c
index af070ec315c4..bf196e062bd0 100644
--- a/board/compulab/imx8mm-cl-iot-gate/imx8mm-cl-iot-gate.c
+++ b/board/compulab/imx8mm-cl-iot-gate/imx8mm-cl-iot-gate.c
@@ -9,6 +9,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -31,6 +32,8 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
+static int fec_phyaddr = -1;
+
 #if IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)
 struct efi_fw_image fw_images[] = {
 #if defined(CONFIG_TARGET_IMX8MM_CL_IOT_GATE)
@@ -110,10 +113,72 @@ static int setup_fec(void)
return 0;
 }
 
+#define FDT_PHYADDR "/soc@0/bus@3080/ethernet@30be/mdio/ethernet-phy@0"
+#define FLIP_32B(val) (((val >> 24) & 0xff) | ((val << 8) & 0xff) | ((val 
>> 8) & 0xff00) | ((val << 24) & 0xff00))
+static int fdt_set_fec_phy_addr(void *blob)
+{
+   u32 val;
+
+   if (fec_phyaddr < 0)
+   return -EINVAL;
+
+   val = FLIP_32B(fec_phyaddr);
+   return fdt_find_and_setprop(blob, FDT_PHYADDR, "reg", (const void 
*),
+   sizeof(val), 0);
+}
+
+int ft_board_setup(void *blob, struct bd_info *bd)
+{
+   fdt_set_fec_phy_addr(blob);
+   return 0;
+}
+
+/*
+ * These are specific ID, purposed to distiguish between PHY vendors.
+ * These values are not equal to real vendors' OUI (half of MAC address)
+ */
+#define OUI_PHY_ATHEROS 0x1374
+#define OUI_PHY_REALTEK 0x0732
+
 int board_phy_config(struct phy_device *phydev)
 {
-   if (IS_ENABLED(CONFIG_FEC_MXC)) {
+   unsigned int model, rev, oui;
+   int phyid1, phyid2;
+   unsigned int reg;
+
+   if (!IS_ENABLED(CONFIG_FEC_MXC))
+   return 0;
+
+   phyid1 = phy_read(phydev, MDIO_DEVAD_NONE, MII_PHYSID1);
+   if (phyid1 < 0) {
+   printf("%s: PHYID1 registry read fail %i\n", __func__, phyid1);
+   return phyid1;
+   }
+
+   phyid2 = phy_read(phydev, MDIO_DEVAD_NONE, MII_PHYSID2);
+   if (phyid2 < 0) {
+   printf("%s: PHYID2 registry read fail %i\n", __func__, phyid2);
+   return phyid2;
+   }
+
+   reg = phyid2 | phyid1 << 16;
+   if (reg == 0x) {
+   printf("%s: There is no device @%i\n", __func__, phydev->addr);
+   return -ENODEV;
+   }
+
+   rev = reg & 0xf;
+   reg >>= 4;
+   model = reg & 0x3f;
+   reg >>= 6;
+   oui = reg;
+   debug("%s: PHY @0x%x OUI 0x%06x model 0x%x rev 0x%x\n",
+ __func__, phydev->addr, oui, model, rev);
+
+   switch (oui) {
+   case OUI_PHY_ATHEROS:
/* enable rgmii rxc skew and phy mode select to RGMII copper */
+   printf("phy: AR803x@%x\t", phydev->addr);
phy_write(phydev, MDIO_DEVAD_NONE, 0x1d, 0x1f);
phy_write(phydev, MDIO_DEVAD_NONE, 0x1e, 0x8);
 
@@ -121,10 +186,45 @@ int board_phy_config(struct phy_device *phydev)
phy_write(phydev, MDIO_DEVAD_NONE, 0x1e, 0x82ee);
phy_write(phydev, MDIO_DEVAD_NONE, 0x1d, 0x05);
phy_write(phydev, MDIO_DEVAD_NONE, 0x1e, 0x100);
+   break;
+   case OUI_PHY_REALTEK:
+   printf("phy: RTL8211E@%x\t", phydev->addr);
+   /* RTL8211E-VB-CG - add TX and RX delay */
+   unsigned short val;
+
+   phy_write(phydev, MDIO_DEVAD_NONE, 0x1f, 0x07);
+   phy_write(phydev, MDIO_DEVAD_NONE, 0x1e, 0xa4);
+   val = phy_read(phydev, MDIO_DEVAD_NONE, 0x1c);
+   val |= (0x1 << 13) | (0x1 << 12) | (0x1 << 11);
+   phy_write(phydev, MDIO_DEVAD_NONE, 0x1c, val);
+   /* LEDs: set to extension page */
+   phy_write(phydev, MDIO_DEVAD_NONE, 0x1f, 0x0007);
+   /* extension Page44 */
+   phy_write(phydev, MDIO_DEVAD_NONE, 0x1e, 0x002c);
+   phy_write(phydev, MDIO_DEVAD_NONE, 0x1c, 0x0430);//LCR
+   phy_write(phydev, MDIO_DEVAD_NONE, 0x1a, 0x0010);//LACR
+   /*
+* To disable EEE LED mode (blinking .4s/2s)
+* Extension Page5
+*/
+   phy_write(phydev, MDIO_DEVAD_NONE, 0x1f, 0x0005);
+   phy_write(phydev, MDIO_DEVAD_NONE, 0x05, 0x8b82);//magic const
+   phy_write(phydev, 

[PATCH 1/2] imx8mm-cl-iot-gate: Add support for Samsung 4GB DDR

2024-05-28 Thread Fabio Estevam
From: Fabio Estevam 

Newer versions of the imx8mm-cl-iot-gate boards may come populated with a
Samsung 4GB DDR model.

Add support for it.

Signed-off-by: Fabio Estevam 
---
 board/compulab/imx8mm-cl-iot-gate/ddr/ddr.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/board/compulab/imx8mm-cl-iot-gate/ddr/ddr.c 
b/board/compulab/imx8mm-cl-iot-gate/ddr/ddr.c
index b230478b611a..f1048a1ab2ab 100644
--- a/board/compulab/imx8mm-cl-iot-gate/ddr/ddr.c
+++ b/board/compulab/imx8mm-cl-iot-gate/ddr/ddr.c
@@ -47,7 +47,9 @@ struct lpddr4_desc {
 static const struct lpddr4_desc lpddr4_array[] = {
{ .name = "Nanya",  .id = 0x0510, .subind = 0xff,
  .size = 2048, .count = 1, .timing = _dram_timing_01061010},
-   { .name = "Samsung",.id = 0x01061010, .subind = 0xff,
+   { .name = "Samsung",.id = 0x01061010, .subind = 0x04,
+ .size = 4096, .count = 1, .timing = _dram_timing_ff000110},
+   { .name = "Samsung",.id = 0x01061010, .subind = 0x02,
  .size = 2048, .count = 1, .timing = _dram_timing_01061010},
{ .name = "Kingston",   .id = 0xff10, .subind = 0x04,
  .size = 4096, .count = 1, .timing = _dram_timing_ff000110},
-- 
2.34.1



Re: [PATCH v2 1/8] efi_loader: allow concatenation with contained end node

2024-05-28 Thread Ilias Apalodimas
On Tue, 28 May 2024 at 19:59, Heinrich Schuchardt
 wrote:
>
> On 28.05.24 18:18, Ilias Apalodimas wrote:
> > [...]
> >
>  -   unsigned sz2 = efi_dp_size(dp2);
>  +   size_t sz1;
>  +   size_t sz2 = efi_dp_size(dp2);
>    void *p;
> 
>  +   if (split_end_node < sizeof(struct efi_device_path))
> >>>
> >>> Can we be more explicit here pls? Someone might misuse this in the future
> >>> split_end_node < =1 ? And we can document we can use values up to
> >>> sizeof(struct efi_device_path) if we ever need extra functionality
> >>
> >> size_t split_end_node cannot be negative.
> >>
> >> The case split_end_node == 0 is handled below. What are you missing?
> >
> > someone misusing it and passing a value of '3' for example and print
> > an error if the value is
> > 1 < value < sizeof(struct efi_device_path)
>
> I would like to avoid over-engineering. How about
>
> -   if (split_end_node < sizeof(struct efi_device_path))
> +   if (split_end_node < 2)
>
> and changing the function description to refer to >= 2?

Nop, in that case, I prefer what's already there, simply because
values between 0 < sizeof(struct efi_device_path) will be used for
functionality similar to the one we have for 0,1 in the future

Thanks
/Ilias
>
> Best regards
>
> Heinrich
>
> >
> > Thanks
> > /Ilias
> >>
> >> Best regards
> >>
> >> Heinrich
> >>
> >>>
>  +   sz1 = efi_dp_size(dp1);
>  +   else
>  +   sz1 = split_end_node;
>  +
>    if (split_end_node)
>    end_size = 2 * sizeof(END);
>    else
>  diff --git a/lib/efi_loader/efi_device_path_utilities.c 
>  b/lib/efi_loader/efi_device_path_utilities.c
>  index c95dbfa9b5f..ac250bbfcc9 100644
>  --- a/lib/efi_loader/efi_device_path_utilities.c
>  +++ b/lib/efi_loader/efi_device_path_utilities.c
>  @@ -76,7 +76,7 @@ static struct efi_device_path * EFIAPI 
>  append_device_path(
>    const struct efi_device_path *src2)
> {
>    EFI_ENTRY("%pD, %pD", src1, src2);
>  -   return EFI_EXIT(efi_dp_concat(src1, src2, false));
>  +   return EFI_EXIT(efi_dp_concat(src1, src2, 0));
> }
> 
> /*
>  --
>  2.43.0
> 
> >>>
> >>> Reviewed-by: Ilias Apalodimas 
> >>
>


Re: [PATCH v2 1/8] efi_loader: allow concatenation with contained end node

2024-05-28 Thread Heinrich Schuchardt

On 28.05.24 18:18, Ilias Apalodimas wrote:

[...]


-   unsigned sz2 = efi_dp_size(dp2);
+   size_t sz1;
+   size_t sz2 = efi_dp_size(dp2);
  void *p;

+   if (split_end_node < sizeof(struct efi_device_path))


Can we be more explicit here pls? Someone might misuse this in the future
split_end_node < =1 ? And we can document we can use values up to
sizeof(struct efi_device_path) if we ever need extra functionality


size_t split_end_node cannot be negative.

The case split_end_node == 0 is handled below. What are you missing?


someone misusing it and passing a value of '3' for example and print
an error if the value is
1 < value < sizeof(struct efi_device_path)


I would like to avoid over-engineering. How about

-   if (split_end_node < sizeof(struct efi_device_path))
+   if (split_end_node < 2)

and changing the function description to refer to >= 2?

Best regards

Heinrich



Thanks
/Ilias


Best regards

Heinrich




+   sz1 = efi_dp_size(dp1);
+   else
+   sz1 = split_end_node;
+
  if (split_end_node)
  end_size = 2 * sizeof(END);
  else
diff --git a/lib/efi_loader/efi_device_path_utilities.c 
b/lib/efi_loader/efi_device_path_utilities.c
index c95dbfa9b5f..ac250bbfcc9 100644
--- a/lib/efi_loader/efi_device_path_utilities.c
+++ b/lib/efi_loader/efi_device_path_utilities.c
@@ -76,7 +76,7 @@ static struct efi_device_path * EFIAPI append_device_path(
  const struct efi_device_path *src2)
   {
  EFI_ENTRY("%pD, %pD", src1, src2);
-   return EFI_EXIT(efi_dp_concat(src1, src2, false));
+   return EFI_EXIT(efi_dp_concat(src1, src2, 0));
   }

   /*
--
2.43.0



Reviewed-by: Ilias Apalodimas 






[PATCH] fs: ubifs: Add support for ZSTD decompression

2024-05-28 Thread Piotr Wojtaszczyk
ZSTD can be a better tradeoff between NAND IO operations and decompression
speed giving a better boot time.

Signed-off-by: Piotr Wojtaszczyk 
---

 fs/ubifs/ubifs-media.h |  2 ++
 fs/ubifs/ubifs.c   | 53 --
 2 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/fs/ubifs/ubifs-media.h b/fs/ubifs/ubifs-media.h
index 2b5b26a01b..299d80f928 100644
--- a/fs/ubifs/ubifs-media.h
+++ b/fs/ubifs/ubifs-media.h
@@ -320,12 +320,14 @@ enum {
  * UBIFS_COMPR_NONE: no compression
  * UBIFS_COMPR_LZO: LZO compression
  * UBIFS_COMPR_ZLIB: ZLIB compression
+ * UBIFS_COMPR_ZSTD: ZSTD compression
  * UBIFS_COMPR_TYPES_CNT: count of supported compression types
  */
 enum {
UBIFS_COMPR_NONE,
UBIFS_COMPR_LZO,
UBIFS_COMPR_ZLIB,
+   UBIFS_COMPR_ZSTD,
UBIFS_COMPR_TYPES_CNT,
 };
 
diff --git a/fs/ubifs/ubifs.c b/fs/ubifs/ubifs.c
index a509584e5d..ff3945e7fd 100644
--- a/fs/ubifs/ubifs.c
+++ b/fs/ubifs/ubifs.c
@@ -27,6 +27,11 @@
 #include 
 #include 
 
+#if IS_ENABLED(CONFIG_ZSTD)
+#include 
+#include 
+#endif
+
 DECLARE_GLOBAL_DATA_PTR;
 
 /* compress.c */
@@ -42,6 +47,25 @@ static int gzip_decompress(const unsigned char *in, size_t 
in_len,
  (unsigned long *)out_len, 0, 0);
 }
 
+#if IS_ENABLED(CONFIG_ZSTD)
+static int zstd_decompress_wrapper(const unsigned char *in, size_t in_len,
+  unsigned char *out, size_t *out_len)
+{
+   struct abuf abuf_in, abuf_out;
+   int ret;
+
+   abuf_init_set(_in, (void *)in, in_len);
+   abuf_init_set(_out, (void *)out, *out_len);
+
+   ret = zstd_decompress(_in, _out);
+   if (ret < 0)
+   return ret;
+
+   *out_len = ret;
+   return 0;
+}
+#endif
+
 /* Fake description object for the "none" compressor */
 static struct ubifs_compressor none_compr = {
.compr_type = UBIFS_COMPR_NONE,
@@ -71,8 +95,21 @@ static struct ubifs_compressor zlib_compr = {
.decompress = gzip_decompress,
 };
 
+#if IS_ENABLED(CONFIG_ZSTD)
+static struct ubifs_compressor zstd_compr = {
+   .compr_type = UBIFS_COMPR_ZSTD,
+#ifndef __UBOOT__
+   .comp_mutex = _enc_mutex,
+   .decomp_mutex = _dec_mutex,
+#endif
+   .name = "zstd",
+   .capi_name = "zstd",
+   .decompress = zstd_decompress_wrapper,
+};
+#endif
+
 /* All UBIFS compressors */
-struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
+struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT] = {NULL};
 
 
 #ifdef __UBOOT__
@@ -166,8 +203,14 @@ int ubifs_decompress(const struct ubifs_info *c, const 
void *in_buf,
 
compr = ubifs_compressors[compr_type];
 
+   if (unlikely(!compr)) {
+   ubifs_err(c, "compression type %d is not compiled in", 
compr_type);
+   return -EINVAL;
+   }
+
if (unlikely(!compr->capi_name)) {
-   ubifs_err(c, "%s compression is not compiled in", compr->name);
+   ubifs_err(c, "%s compression is not compiled in",
+ compr->name ? compr->name : "unknown");
return -EINVAL;
}
 
@@ -232,6 +275,12 @@ int __init ubifs_compressors_init(void)
if (err)
return err;
 
+#if IS_ENABLED(CONFIG_ZSTD)
+   err = compr_init(_compr);
+   if (err)
+   return err;
+#endif
+
err = compr_init(_compr);
if (err)
return err;
-- 
2.25.1



Re: [PATCH v2 08/14] net-lwip: add wget command

2024-05-28 Thread Maxim Uvarov
пт, 24 мая 2024 г. в 19:22, Jerome Forissier :
>
> Add support for the wget command with NET_LWIP.
>
> About the small change in cmd/efidebug.c: when the wget command based
> on the lwIP stack is used the wget command has a built-in URL
> validation function since it needs to parse it anyways (in parse_url()).
> Therefore wget_validate_uri() doesn't exist. So, guard the call in
> efidebug.c with CONFIG_CMD_WGET.
>
> Based on code initially developed by Maxim U.
>
> Signed-off-by: Jerome Forissier 
> Co-developed-by: Maxim Uvarov 
> Cc: Maxim Uvarov 
> ---
>  cmd/Kconfig|   7 ++
>  cmd/Makefile   |   5 +-
>  cmd/efidebug.c |   8 +-
>  cmd/net-common.c   | 112 
>  cmd/net-lwip.c |  12 +++
>  cmd/net.c  | 115 -
>  include/net-lwip.h |  51 +
>  net-lwip/Makefile  |   1 +
>  net-lwip/wget.c| 180 +
>  9 files changed, 372 insertions(+), 119 deletions(-)
>  create mode 100644 cmd/net-common.c
>  create mode 100644 net-lwip/wget.c
>
> diff --git a/cmd/Kconfig b/cmd/Kconfig
> index 6ef0b52cd34..d9a86540be6 100644
> --- a/cmd/Kconfig
> +++ b/cmd/Kconfig
> @@ -2117,6 +2117,13 @@ config CMD_TFTPBOOT
> help
>   tftpboot - load file via network using TFTP protocol
>
> +config CMD_WGET
> +   bool "wget"
> +   select PROT_TCP_LWIP
> +   help
> + wget is a simple command to download kernel, or other files,
> + from a http server over TCP.
> +
>  endif
>
>  endif
> diff --git a/cmd/Makefile b/cmd/Makefile
> index 535b6838ca5..e90f2f68211 100644
> --- a/cmd/Makefile
> +++ b/cmd/Makefile
> @@ -129,8 +129,11 @@ obj-$(CONFIG_CMD_MUX) += mux.o
>  obj-$(CONFIG_CMD_NAND) += nand.o
>  obj-$(CONFIG_CMD_NET) += net.o
>  obj-$(CONFIG_CMD_NET_LWIP) += net-lwip.o
> +obj-$(filter y,$(CONFIG_CMD_NET) $(CONFIG_CMD_NET_LWIP)) += net-common.o
>  ifdef CONFIG_CMD_NET_LWIP
> -CFLAGS_net-lwip.o := -I$(srctree)/lib/lwip/lwip/src/include 
> -I$(srctree)/lib/lwip/u-boot
> +lwip-includes := -I$(srctree)/lib/lwip/lwip/src/include 
> -I$(srctree)/lib/lwip/u-boot
> +CFLAGS_net-lwip.o := $(lwip-includes)
> +CFLAGS_net-common.o := $(lwip-includes)
>  endif
>  obj-$(CONFIG_ENV_SUPPORT) += nvedit.o
>  obj-$(CONFIG_CMD_NVEDIT_EFI) += nvedit_efi.o
> diff --git a/cmd/efidebug.c b/cmd/efidebug.c
> index c2c525f2351..d80e91ecadd 100644
> --- a/cmd/efidebug.c
> +++ b/cmd/efidebug.c
> @@ -741,9 +741,11 @@ static int efi_boot_add_uri(int argc, char *const 
> argv[], u16 *var_name16,
> if (!label)
> return CMD_RET_FAILURE;
>
> -   if (!wget_validate_uri(argv[3])) {
> -   printf("ERROR: invalid URI\n");
> -   return CMD_RET_FAILURE;
> +   if (IS_ENABLED(CONFIG_CMD_WGET)) {
> +   if (!wget_validate_uri(argv[3])) {
> +   printf("ERROR: invalid URI\n");
> +   return CMD_RET_FAILURE;
> +   }
> }
>
> efi_create_indexed_name(var_name16, var_name16_size, "Boot", id);
> diff --git a/cmd/net-common.c b/cmd/net-common.c
> new file mode 100644
> index 000..b5dfd2c8866
> --- /dev/null
> +++ b/cmd/net-common.c
> @@ -0,0 +1,112 @@
> +// SPDX-License-Identifier: GPL-2.0+
> +/*
> + * (C) Copyright 2000
> + * Wolfgang Denk, DENX Software Engineering, w...@denx.de.
> + */
> +
> +#include 
> +#include 
> +#include 
> +#ifdef CONFIG_NET
> +#include 
> +#elif defined CONFIG_NET_LWIP
> +#include 
> +#else
> +#error Either NET or NET_LWIP must be enabled
> +#endif
> +#include 
> +#include 
> +
> +static int do_net_list(struct cmd_tbl *cmdtp, int flag, int argc, char 
> *const argv[])
> +{
> +   const struct udevice *current = eth_get_dev();
> +   unsigned char env_enetaddr[ARP_HLEN];
> +   const struct udevice *dev;
> +   struct uclass *uc;
> +
> +   uclass_id_foreach_dev(UCLASS_ETH, dev, uc) {
> +   eth_env_get_enetaddr_by_index("eth", dev_seq(dev), 
> env_enetaddr);
> +   printf("eth%d : %s %pM %s\n", dev_seq(dev), dev->name, 
> env_enetaddr,
> +  current == dev ? "active" : "");
> +   }
> +   return CMD_RET_SUCCESS;
> +}
> +
> +static int do_net_stats(struct cmd_tbl *cmdtp, int flag, int argc, char 
> *const argv[])
> +{
> +   int nstats, err, i, off;
> +   struct udevice *dev;
> +   u64 *values;
> +   u8 *strings;
> +
> +   if (argc < 2)
> +   return CMD_RET_USAGE;
> +
> +   err = uclass_get_device_by_name(UCLASS_ETH, argv[1], );
> +   if (err) {
> +   printf("Could not find device %s\n", argv[1]);
> +   return CMD_RET_FAILURE;
> +   }
> +
> +   if (!eth_get_ops(dev)->get_sset_count ||
> +   !eth_get_ops(dev)->get_strings ||
> +   !eth_get_ops(dev)->get_stats) {
> +   printf("Driver does not implement stats dump!\n");
> +   return CMD_RET_FAILURE;
> +   }
> +
> +   

Re: [PATCH 1/2] arm: imx8mm-phycore: move to OF_UPSTREAM

2024-05-28 Thread Teresa Remmet
Am Dienstag, dem 28.05.2024 um 13:24 +0200 schrieb Yannic Moog:
> The PHYCORE_IMX8MM is used by the phyBOARD-Polis and the
> phyGATE-Tauri-L. Migrate both boards to OF_UPSTREAM. Linux kernel
> device
> trees for both boards can be used as is, corresponding U-Boot device
> tree files are removed. U-Boot tweaks are kept unchanged.
> 
> Signed-off-by: Yannic Moog 

Acked-by: Teresa Remmet 

> ---
>  arch/arm/dts/Makefile  |   2 -
>  arch/arm/dts/imx8mm-phyboard-polis-rdk.dts | 460 ---
> 
>  arch/arm/dts/imx8mm-phycore-som.dtsi   | 440 ---
> ---
>  arch/arm/dts/imx8mm-phygate-tauri-l.dts    | 489 ---
> --
>  arch/arm/mach-imx/imx8m/Kconfig    |   1 +
>  board/phytec/phycore_imx8mm/MAINTAINERS    |   3 -
>  configs/imx8mm-phygate-tauri-l_defconfig   |   2 +-
>  configs/phycore-imx8mm_defconfig   |   2 +-
>  8 files changed, 3 insertions(+), 1396 deletions(-)
> 
> diff --git a/arch/arm/dts/Makefile b/arch/arm/dts/Makefile
> index c9f1b25ad64..480f8ff569d 100644
> --- a/arch/arm/dts/Makefile
> +++ b/arch/arm/dts/Makefile
> @@ -1016,8 +1016,6 @@ dtb-$(CONFIG_ARCH_IMX8M) += \
> imx8mm-kontron-bl-osm-s.dtb \
> imx8mm-mx8menlo.dtb \
> imx8mm-phg.dtb \
> -   imx8mm-phyboard-polis-rdk.dtb \
> -   imx8mm-phygate-tauri-l.dtb \
> imx8mn-bsh-smm-s2.dtb \
> imx8mn-bsh-smm-s2pro.dtb \
> imx8mq-cm.dtb \
> diff --git a/arch/arm/dts/imx8mm-phyboard-polis-rdk.dts
> b/arch/arm/dts/imx8mm-phyboard-polis-rdk.dts
> deleted file mode 100644
> index 03e7679217b..000
> --- a/arch/arm/dts/imx8mm-phyboard-polis-rdk.dts
> +++ /dev/null
> @@ -1,460 +0,0 @@
> -// SPDX-License-Identifier: GPL-2.0
> -/*
> - * Copyright (C) 2022 PHYTEC Messtechnik GmbH
> - * Author: Teresa Remmet 
> - */
> -
> -/dts-v1/;
> -
> -#include 
> -#include 
> -#include 
> -#include "imx8mm-phycore-som.dtsi"
> -
> -/ {
> -   model = "PHYTEC phyBOARD-Polis-i.MX8MM RDK";
> -   compatible = "phytec,imx8mm-phyboard-polis-rdk",
> -    "phytec,imx8mm-phycore-som", "fsl,imx8mm";
> -
> -   chosen {
> -   stdout-path = 
> -   };
> -
> -   bt_osc_32k: bt-lp-clock {
> -   compatible = "fixed-clock";
> -   clock-frequency = <32768>;
> -   clock-output-names = "bt_osc_32k";
> -   #clock-cells = <0>;
> -   };
> -
> -   can_osc_40m: can-clock {
> -   compatible = "fixed-clock";
> -   clock-frequency = <4000>;
> -   clock-output-names = "can_osc_40m";
> -   #clock-cells = <0>;
> -   };
> -
> -   fan {
> -   compatible = "gpio-fan";
> -   gpios = < 8 GPIO_ACTIVE_HIGH>;
> -   gpio-fan,speed-map = <0 0
> - 13000 1>;
> -   pinctrl-names = "default";
> -   pinctrl-0 = <_fan>;
> -   #cooling-cells = <2>;
> -   };
> -
> -   leds {
> -   compatible = "gpio-leds";
> -   pinctrl-names = "default";
> -   pinctrl-0 = <_leds>;
> -
> -   led-0 {
> -   color = ;
> -   function = LED_FUNCTION_DISK;
> -   gpios = < 1 GPIO_ACTIVE_HIGH>;
> -   linux,default-trigger = "mmc2";
> -   };
> -
> -   led-1 {
> -   color = ;
> -   function = LED_FUNCTION_DISK;
> -   gpios = < 15 GPIO_ACTIVE_HIGH>;
> -   linux,default-trigger = "mmc1";
> -   };
> -
> -   led-2 {
> -   color = ;
> -   function = LED_FUNCTION_CPU;
> -   gpios = < 14 GPIO_ACTIVE_HIGH>;
> -   linux,default-trigger = "heartbeat";
> -   };
> -   };
> -
> -   usdhc1_pwrseq: pwr-seq {
> -   compatible = "mmc-pwrseq-simple";
> -   post-power-on-delay-ms = <100>;
> -   power-off-delay-us = <60>;
> -   reset-gpios = < 7 GPIO_ACTIVE_LOW>;
> -   };
> -
> -   reg_can_en: regulator-can-en {
> -   compatible = "regulator-fixed";
> -   gpio = < 9 GPIO_ACTIVE_LOW>;
> -   pinctrl-names = "default";
> -   pinctrl-0 = <_can_en>;
> -   regulator-max-microvolt = <330>;
> -   regulator-min-microvolt = <330>;
> -   regulator-name = "CAN_EN";
> -   startup-delay-us = <20>;
> -   };
> -
> -   reg_usb_otg1_vbus: regulator-usb-otg1 {
> -   compatible = "regulator-fixed";
> -   gpio = < 12 GPIO_ACTIVE_HIGH>;
> -   enable-active-high;
> -   pinctrl-names = "default";
> -   pinctrl-0 = <_usbotg1pwrgrp>;
> -   regulator-name = "usb_otg1_vbus";

Re: [PATCH v2 01/14] net: introduce alternative implementation as net-lwip/

2024-05-28 Thread Ilias Apalodimas
On Mon, May 27, 2024 at 09:34:34AM -0600, Tom Rini wrote:
> On Fri, May 24, 2024 at 06:19:55PM +0200, Jerome Forissier wrote:
>
> > Prepare the introduction of the lwIP (lightweight IP) TCP/IP stack by
> > adding a new net-lwip/ directory and the NET_LWIP symbol. At this
> > point, enabling NET_LWIP simply disables NET. Subsequent commits will
> > introduce the lwIP code, re-work the NETDEVICE integration and port
> > some of the NET commands and features to lwIP.
> >
> > SPL_NET cannot be enabled when NET_LWIP=y. SPL_NET pulls some symbols
> > that are part of NET (such as arp_init(), arp_timeout_check(),
> > arp_receive(), net_arp_wait_packet_ip()). lwIP support in SPL may be
> > added later.
> >
> > Similarly, DFU_TFTP is not compatible with NET_LWIP because it depends
> > on net_loop(), tftp_timeout_ms, tftp_timeout_count_max. Let's add a
> > dependency on !NET_LWIP for now.
> >
> > Signed-off-by: Jerome Forissier 
> [snip]
> > diff --git a/Kconfig b/Kconfig
> > index 82df59f176e..758256ab121 100644
> > --- a/Kconfig
> > +++ b/Kconfig
> > @@ -747,6 +747,8 @@ source "env/Kconfig"
> >
> >  source "net/Kconfig"
> >
> > +source "net-lwip/Kconfig"
> > +
> >  source "drivers/Kconfig"
> >
> >  source "fs/Kconfig"
>
> I think we need to instead rework this to a choice statement instead so
> that in the end we have something like:

+ 1

> choice "Networking stack"
> config NO_NET
>   bool "No networking support"
> config NET
>   bool "Legacy U-Boot networking stack"
> config NET_LWIP
>   bool "Use lwIP for networking stack"
>
> if NET_LEGACY
> source "net/Kconfig"
> endif
>
> if NET_LWIP
> source "net-lwip/Kconfig"
> endif
>
> And then SPL_NET still depends on !NET_LWIP for now and we sort out the
> problems with different networking stacks in SPL vs full U-Boot later
> on.

That's an excellent idea tbh. We can now decouple SPL and u-boot proper
using different stacks and fix the more difficult to debug SPL issues in
time. Once we feel happy we can switch over to LWIP completely

Thanks
/Ilias
>
> --
> Tom




Re: [PATCH v2 12/14] test/py: net: add _lwip variants of dhcp, ping and tftpboot tests

2024-05-28 Thread Maxim Uvarov
пт, 24 мая 2024 г. в 19:22, Jerome Forissier :
>
> WHen NET_LWIP is enabled, the dhcp/ping/tftpboot commands are enabled
> via CMD_DHCP_LWIP, CMD_PING_LWIP and CMD_TFTPBOOT_LWIP, respectively;
> therefore the config annotations in the Python test scripts need to be
> cmd_dhcp_lwip, cmd_ping_lwip and cmd_tftpboot_lwip.
>
> The console output of the tftpboot command with lwIP is slightly
> different from the non-lwIP implementation. This is taken care of in
> test_net_tftpboot().
>
> Signed-off-by: Jerome Forissier 
> ---
>  test/py/tests/test_net.py | 21 +
>  1 file changed, 17 insertions(+), 4 deletions(-)
>
> diff --git a/test/py/tests/test_net.py b/test/py/tests/test_net.py
> index 038a473b239..ba30780903f 100644
> --- a/test/py/tests/test_net.py
> +++ b/test/py/tests/test_net.py
> @@ -127,6 +127,10 @@ def test_net_dhcp(u_boot_console):
>  global net_set_up
>  net_set_up = True
>
> +@pytest.mark.buildconfigspec('cmd_dhcp_lwip')
> +def test_net_dhcp_lwip(u_boot_console):
> +test_net_dhcp(u_boot_console)
> +
>  @pytest.mark.buildconfigspec('cmd_dhcp')
>  @pytest.mark.buildconfigspec('cmd_mii')
>  def test_net_dhcp_abort(u_boot_console):
> @@ -230,6 +234,10 @@ def test_net_ping(u_boot_console):
>  output = u_boot_console.run_command('ping $serverip')
>  assert 'is alive' in output
>
> +@pytest.mark.buildconfigspec('cmd_ping_lwip')
> +def test_net_ping_lwip(u_boot_console):
> +test_net_ping(u_boot_console)
> +
>  @pytest.mark.buildconfigspec('IPV6_ROUTER_DISCOVERY')
>  def test_net_network_discovery(u_boot_console):
>  """Test the network discovery feature of IPv6.
> @@ -255,7 +263,7 @@ def test_net_network_discovery(u_boot_console):
>  assert ':::::::' not in output
>
>  @pytest.mark.buildconfigspec('cmd_net')
> -def test_net_tftpboot(u_boot_console):
> +def test_net_tftpboot(u_boot_console, lwip = False):
>  """Test the tftpboot command.
>
>  A file is downloaded from the TFTP server, its size and optionally its
> @@ -279,10 +287,11 @@ def test_net_tftpboot(u_boot_console):
>  output = u_boot_console.run_command('tftpboot %s' % (fn))
>  else:
>  output = u_boot_console.run_command('tftpboot %x %s' % (addr, fn))
> -expected_text = 'Bytes transferred = '
>  sz = f.get('size', None)
> -if sz:
> -expected_text += '%d' % sz
> +if lwip:
> +expected_text = f'{sz} bytes transferred'

Why not change patch 5 to print with capital character to match this pattern?

BR,
Maxim.

> +else:
> +expected_text = f'Bytes transferred = {sz}'
>  assert expected_text in output
>
>  expected_crc = f.get('crc32', None)
> @@ -295,6 +304,10 @@ def test_net_tftpboot(u_boot_console):
>  output = u_boot_console.run_command('crc32 $fileaddr $filesize')
>  assert expected_crc in output
>
> +@pytest.mark.buildconfigspec("cmd_net_lwip")
> +def test_net_tftpboot_lwip(u_boot_console):
> +test_net_tftpboot(u_boot_console, True)
> +
>  @pytest.mark.buildconfigspec('cmd_nfs')
>  def test_net_nfs(u_boot_console):
>  """Test the nfs command.
> --
> 2.40.1
>


-- 
Best regards,
Maxim Uvarov


[PATCH] Makefile: Fix include directory for OF_UPSTREAM

2024-05-28 Thread Patrick Barsanti
Always prioritizing u-boot includes causes problems when trying to
migrate boards to OF_UPSTREAM that have divergent devicetree files with
respect to the upstream ones.

For example, migrating a board based on `imx6ul.dtsi` to OF_UPSTREAM
breaks it, as there are some missing defines in the local dtsi file;
the solutions would be to either patch it, which defeats the purpose of
OF_UPSTREAM, or delete it entirely. This last option would then break all
the other boards which have not yet been migrated to OF_UPSTREAM.

The opposite problem also exists: by always prioritizing upstream
includes, if changes are made in the kernel headers and devicetree
files that are not backwards compatible, again all boards which have not
been migrated to OF_UPSTREAM will break.

This patch fixes this problem by prioritizing upstream includes when
`CONFIG_OF_UPSTREAM=y`, while keeping current prioritization when
it is not.

Signed-off-by: Patrick Barsanti 
---
 Makefile | 14 ++
 1 file changed, 14 insertions(+)

diff --git a/Makefile b/Makefile
index 79b28c2d81..899ae664ca 100644
--- a/Makefile
+++ b/Makefile
@@ -826,6 +826,19 @@ KBUILD_HOSTCFLAGS += $(if $(CONFIG_TOOLS_DEBUG),-g)
 
 # Use UBOOTINCLUDE when you must reference the include/ directory.
 # Needed to be compatible with the O= option
+ifeq ($(CONFIG_OF_UPSTREAM),y)
+UBOOTINCLUDE:= \
+   -I$(srctree)/dts/upstream/include \
+   -Iinclude \
+   $(if $(KBUILD_SRC), -I$(srctree)/include) \
+   $(if $(CONFIG_$(SPL_)SYS_THUMB_BUILD), \
+   $(if $(CONFIG_HAS_THUMB2), \
+   $(if $(CONFIG_CPU_V7M), \
+   -I$(srctree)/arch/arm/thumb1/include), \
+   -I$(srctree)/arch/arm/thumb1/include)) \
+   -I$(srctree)/arch/$(ARCH)/include \
+   -include $(srctree)/include/linux/kconfig.h
+else
 UBOOTINCLUDE:= \
-Iinclude \
$(if $(KBUILD_SRC), -I$(srctree)/include) \
@@ -837,6 +850,7 @@ UBOOTINCLUDE:= \
-I$(srctree)/arch/$(ARCH)/include \
-include $(srctree)/include/linux/kconfig.h \
-I$(srctree)/dts/upstream/include
+endif
 
 NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)
 
-- 
2.43.0



[PATCH] mmc: return immediately once the dwmci data transfer fails

2024-05-28 Thread linjunbao
Originally, when the host is in dma mode, the return status of
dwmci_data_transfer() gets overlapped by the latter DWMCI_IDINTEN query.
When the data transfer fails and the query succeeds, the failure gets
escaped. Add quick return when data transfer fails.

Signed-off-by: linjunbao 
---

 drivers/mmc/dw_mmc.c | 4 
 1 file changed, 4 insertions(+)

diff --git a/drivers/mmc/dw_mmc.c b/drivers/mmc/dw_mmc.c
index e103664145..e04872c432 100644
--- a/drivers/mmc/dw_mmc.c
+++ b/drivers/mmc/dw_mmc.c
@@ -372,6 +372,10 @@ static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd 
*cmd,
 
if (data) {
ret = dwmci_data_transfer(host, data);
+   if (ret) {
+   debug("DWMCI data transfer fail.\n");
+   return ret;
+   }
 
/* only dma mode need it */
if (!host->fifo_mode) {
-- 
2.25.1



[PATCH] mmc: return immediately once the dwmci data transfer fails

2024-05-28 Thread linjunbao
Originally, when the host is in dma mode, the return status of
dwmci_data_transfer() gets overlapped by the latter DWMCI_IDINTEN query.
When the data transfer fails and the query succeeds, the failure gets
escaped. Add quick return when data transfer fails.

Signed-off-by: linjunbao 
---

 drivers/mmc/dw_mmc.c | 4 
 1 file changed, 4 insertions(+)

diff --git a/drivers/mmc/dw_mmc.c b/drivers/mmc/dw_mmc.c
index e103664145..e04872c432 100644
--- a/drivers/mmc/dw_mmc.c
+++ b/drivers/mmc/dw_mmc.c
@@ -372,6 +372,10 @@ static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd 
*cmd,
 
if (data) {
ret = dwmci_data_transfer(host, data);
+   if (ret) {
+   debug("DWMCI data transfer fail.\n");
+   return ret;
+   }
 
/* only dma mode need it */
if (!host->fifo_mode) {
-- 
2.25.1



Re: [PATCH v2 1/8] dfu: add scsi backend

2024-05-28 Thread Caleb Connolly




On 28/05/2024 11:10, Mattijs Korpershoek wrote:

Hi Caleb,

Thank you for the patch.

On lun., mai 27, 2024 at 19:17, Caleb Connolly  
wrote:


This is extremely similar to the MMC backend, but there are some notable
differences.

Works with a DFU string like

 scsi 4=u-boot-bin part 11

Where "4" is the SCSI dev number (sequential LUN across all SCSI devices)
and "11" is the partition number >>
Signed-off-by: Caleb Connolly 
---
  doc/usage/dfu.rst  |  31 
  drivers/dfu/Kconfig|   7 +
  drivers/dfu/Makefile   |   1 +
  drivers/dfu/dfu.c  |   5 +-
  drivers/dfu/dfu_scsi.c | 437 +
  include/dfu.h  |  26 +++
  6 files changed, 506 insertions(+), 1 deletion(-)


I reviewed this here [1]. I could not find any response on that thread.
Could you please respond there or address the remarks for v3, please?



Hi Mattijs,

So sorry about that, will address your feedback in v3.

Thanks,


[1] https://lore.kernel.org/r/87o7a94pe1@baylibre.com



diff --git a/doc/usage/dfu.rst b/doc/usage/dfu.rst
index 8cc09c308d82..dc4f8d672f99 100644
--- a/doc/usage/dfu.rst
+++ b/doc/usage/dfu.rst
@@ -166,8 +166,38 @@ mmc


[...]


--
2.45.0


--
// Caleb (they/them)


Re: [PATCH v2 1/8] efi_loader: allow concatenation with contained end node

2024-05-28 Thread Ilias Apalodimas
[...]

> >> -   unsigned sz2 = efi_dp_size(dp2);
> >> +   size_t sz1;
> >> +   size_t sz2 = efi_dp_size(dp2);
> >>  void *p;
> >>
> >> +   if (split_end_node < sizeof(struct efi_device_path))
> >
> > Can we be more explicit here pls? Someone might misuse this in the future
> > split_end_node < =1 ? And we can document we can use values up to
> > sizeof(struct efi_device_path) if we ever need extra functionality
>
> size_t split_end_node cannot be negative.
>
> The case split_end_node == 0 is handled below. What are you missing?

someone misusing it and passing a value of '3' for example and print
an error if the value is
1 < value < sizeof(struct efi_device_path)

Thanks
/Ilias
>
> Best regards
>
> Heinrich
>
> >
> >> +   sz1 = efi_dp_size(dp1);
> >> +   else
> >> +   sz1 = split_end_node;
> >> +
> >>  if (split_end_node)
> >>  end_size = 2 * sizeof(END);
> >>  else
> >> diff --git a/lib/efi_loader/efi_device_path_utilities.c 
> >> b/lib/efi_loader/efi_device_path_utilities.c
> >> index c95dbfa9b5f..ac250bbfcc9 100644
> >> --- a/lib/efi_loader/efi_device_path_utilities.c
> >> +++ b/lib/efi_loader/efi_device_path_utilities.c
> >> @@ -76,7 +76,7 @@ static struct efi_device_path * EFIAPI 
> >> append_device_path(
> >>  const struct efi_device_path *src2)
> >>   {
> >>  EFI_ENTRY("%pD, %pD", src1, src2);
> >> -   return EFI_EXIT(efi_dp_concat(src1, src2, false));
> >> +   return EFI_EXIT(efi_dp_concat(src1, src2, 0));
> >>   }
> >>
> >>   /*
> >> --
> >> 2.43.0
> >>
> >
> > Reviewed-by: Ilias Apalodimas 
>


Re: [PATCH v2 2/8] cmd: eficonfig: add support for setting fdt

2024-05-28 Thread Ilias Apalodimas
On Tue, 28 May 2024 at 19:08, Heinrich Schuchardt
 wrote:
>
> On 28.05.24 17:16, Ilias Apalodimas wrote:
> > Hi Heinrich,
> >
> > [...]
> >
> >>
> >> -   const struct efi_initrd_dp id_dp = {
> >> +   const struct efi_initrd_dp initrd_prefix = {
> >>  .vendor = {
> >>  {
> >>  DEVICE_PATH_TYPE_MEDIA_DEVICE,
> >>  DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
> >> -   sizeof(id_dp.vendor),
> >> +   sizeof(initrd_prefix.vendor),
> >>  },
> >>  EFI_INITRD_MEDIA_GUID,
> >>  },
> >>  .end = {
> >>  DEVICE_PATH_TYPE_END,
> >>  DEVICE_PATH_SUB_TYPE_END,
> >> -   sizeof(id_dp.end),
> >> +   sizeof(initrd_prefix.end),
> >> +   }
> >> +   };
> >> +
> >> +   const struct efi_initrd_dp fdt_prefix = {
> >
> > We need to rename efi_initrd_dp to something more generic in the future
> >
> >> +   .vendor = {
> >> +   {
> >> +   DEVICE_PATH_TYPE_MEDIA_DEVICE,
> >> +   DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
> >> +   sizeof(fdt_prefix.vendor),
> >> +   },
> >> +   EFI_FDT_GUID,
> >> +   },
> >> +   .end = {
> >> +   DEVICE_PATH_TYPE_END,
> >> +   DEVICE_PATH_SUB_TYPE_END,
> >> +   sizeof(initrd_prefix.end),
> >>  }
> >>  };
> >>
> >
> > [...]
> >
> >> +/**
> >> + * efi_load_option_dp_join() - join device-paths for load option
> >> + *
> >> + * @dp:in: binary device-path, out: joined device-path
> >> + * @dp_size:   size of joined device-path
> >> + * @initrd_dp: initrd device-path or NULL
> >> + * @fdt_dp:device-tree device-path or NULL
> >> + * Return: status_code
> >> + */
> >> +efi_status_t efi_load_option_dp_join(struct efi_device_path **dp,
> >> +size_t *dp_size,
> >> +struct efi_device_path *initrd_dp,
> >> +struct efi_device_path *fdt_dp)
> >> +{
> >> +   if (!dp)
> >
> > Should we add && !*dp here?
>
> efi_dp_concat() handles the case of one or both device-paths being NULL.

Fair enough

>
> >
> >> +   return EFI_INVALID_PARAMETER;
> >> +
> >> +   *dp_size = efi_dp_size(*dp);
> >> +
> >> +   if (initrd_dp) {
> >> +   struct efi_device_path *tmp_dp = *dp;
> >> +
> >> +   *dp = efi_dp_concat(tmp_dp, initrd_dp, *dp_size);
> >> +   efi_free_pool(tmp_dp);
> >> +   if (!*dp)
> >> +   return EFI_OUT_OF_RESOURCES;
> >> +   *dp_size += efi_dp_size(initrd_dp) + sizeof(END);
> >> +   }
> >> +
> >> +   if (fdt_dp) {
> >> +   struct efi_device_path *tmp_dp = *dp;
> >> +
> >> +   *dp = efi_dp_concat(tmp_dp, fdt_dp, *dp_size);
> >> +   efi_free_pool(tmp_dp);
> >> +   if (!dp)
> >> +   return EFI_OUT_OF_RESOURCES;
> >> +   *dp_size += efi_dp_size(fdt_dp) + sizeof(END);
> >> +   }
> >> +
> >> +   *dp_size += sizeof(END);
> >
> > Why do we have to account for the end node twice if either fdt_dp or
> > initrd_dp are found?
>
> This is the length of the END node of the binary.

ah yes correct.

Feel free to add
Reviewed-by: Ilias Apalodimas 
but we need to fix the naming at some point

>
> Best regards
>
> Heinrich
>
> >
> > Thanks
> > /Ilias
> >> +
> >> +   return EFI_SUCCESS;
> >> +}
> >> +
> >>   const struct guid_to_hash_map {
> >>  efi_guid_t guid;
> >>  const char algo[32];
> >> --
> >> 2.43.0
> >>
>


Re: [PATCH v2 1/8] efi_loader: allow concatenation with contained end node

2024-05-28 Thread Heinrich Schuchardt

On 28.05.24 18:02, Ilias Apalodimas wrote:

On Tue, 28 May 2024 at 17:43, Heinrich Schuchardt
 wrote:


Allow appending a device-path to a device-path that contains an end node
as separator. We need this feature for creating boot options specifying
kernel, initrd, and dtb.

Signed-off-by: Heinrich Schuchardt 
---
v2:
 update efi_dp_concat() instead of new function efi_dp_merge()
---
  cmd/eficonfig.c|  6 +++---
  cmd/efidebug.c |  4 ++--
  include/efi_loader.h   |  2 +-
  lib/efi_loader/efi_bootbin.c   |  2 +-
  lib/efi_loader/efi_bootmgr.c   |  2 +-
  lib/efi_loader/efi_boottime.c  |  2 +-
  lib/efi_loader/efi_device_path.c   | 20 +---
  lib/efi_loader/efi_device_path_utilities.c |  2 +-
  8 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/cmd/eficonfig.c b/cmd/eficonfig.c
index 0ba92c60e03..b13d9a3d2d9 100644
--- a/cmd/eficonfig.c
+++ b/cmd/eficonfig.c
@@ -531,7 +531,7 @@ struct efi_device_path *eficonfig_create_device_path(struct 
efi_device_path *dp_
 dp = efi_dp_shorten(dp_volume);
 if (!dp)
 dp = dp_volume;
-   dp = efi_dp_concat(dp, >dp, false);
+   dp = efi_dp_concat(dp, >dp, 0);
 free(buf);

 return dp;
@@ -1485,7 +1485,7 @@ static efi_status_t eficonfig_edit_boot_option(u16 
*varname, struct eficonfig_bo
 goto out;
 }
 initrd_dp = efi_dp_concat((const struct efi_device_path 
*)_dp,
- dp, false);
+ dp, 0);
 efi_free_pool(dp);
 }

@@ -1496,7 +1496,7 @@ static efi_status_t eficonfig_edit_boot_option(u16 
*varname, struct eficonfig_bo
 }
 final_dp_size = efi_dp_size(dp) + sizeof(END);
 if (initrd_dp) {
-   final_dp = efi_dp_concat(dp, initrd_dp, true);
+   final_dp = efi_dp_concat(dp, initrd_dp, 1);
 final_dp_size += efi_dp_size(initrd_dp) + sizeof(END);
 } else {
 final_dp = efi_dp_dup(dp);
diff --git a/cmd/efidebug.c b/cmd/efidebug.c
index c2c525f2351..762027daf8a 100644
--- a/cmd/efidebug.c
+++ b/cmd/efidebug.c
@@ -697,7 +697,7 @@ struct efi_device_path *create_initrd_dp(const char *dev, 
const char *part,
 short_fp = tmp_fp;

 initrd_dp = efi_dp_concat((const struct efi_device_path *)_dp,
- short_fp, false);
+ short_fp, 0);

  out:
 efi_free_pool(tmp_dp);
@@ -917,7 +917,7 @@ static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
 goto out;
 }

-   final_fp = efi_dp_concat(file_path, initrd_dp, true);
+   final_fp = efi_dp_concat(file_path, initrd_dp, 1);
 if (!final_fp) {
 printf("Cannot create final device path\n");
 r = CMD_RET_FAILURE;
diff --git a/include/efi_loader.h b/include/efi_loader.h
index 9600941aa32..ddf2e41a95c 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -946,7 +946,7 @@ struct efi_device_path *efi_dp_from_lo(struct 
efi_load_option *lo,
const efi_guid_t *guid);
  struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
   const struct efi_device_path *dp2,
- bool split_end_node);
+ size_t split_end_node);
  struct efi_device_path *search_gpt_dp_node(struct efi_device_path 
*device_path);
  efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 *data,
  efi_uintn_t *size);
diff --git a/lib/efi_loader/efi_bootbin.c b/lib/efi_loader/efi_bootbin.c
index b7910f78fb6..a87006b3c0e 100644
--- a/lib/efi_loader/efi_bootbin.c
+++ b/lib/efi_loader/efi_bootbin.c
@@ -150,7 +150,7 @@ efi_status_t efi_run_image(void *source_buffer, efi_uintn_t 
source_size)
 msg_path = file_path;
 } else {
 file_path = efi_dp_concat(bootefi_device_path,
- bootefi_image_path, false);
+ bootefi_image_path, 0);
 msg_path = bootefi_image_path;
 log_debug("Loaded from disk\n");
 }
diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
index 7da3139f917..b0bf21cf841 100644
--- a/lib/efi_loader/efi_bootmgr.c
+++ b/lib/efi_loader/efi_bootmgr.c
@@ -130,7 +130,7 @@ static efi_status_t try_load_from_file_path(efi_handle_t 
*fs_handles,
 if (!dp)
 continue;

-   dp = efi_dp_concat(dp, fp, false);
+   dp = efi_dp_concat(dp, fp, 0);
 if (!dp)
 continue;

diff --git 

Re: [PATCH v2 2/8] cmd: eficonfig: add support for setting fdt

2024-05-28 Thread Heinrich Schuchardt

On 28.05.24 17:16, Ilias Apalodimas wrote:

Hi Heinrich,

[...]



-   const struct efi_initrd_dp id_dp = {
+   const struct efi_initrd_dp initrd_prefix = {
 .vendor = {
 {
 DEVICE_PATH_TYPE_MEDIA_DEVICE,
 DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
-   sizeof(id_dp.vendor),
+   sizeof(initrd_prefix.vendor),
 },
 EFI_INITRD_MEDIA_GUID,
 },
 .end = {
 DEVICE_PATH_TYPE_END,
 DEVICE_PATH_SUB_TYPE_END,
-   sizeof(id_dp.end),
+   sizeof(initrd_prefix.end),
+   }
+   };
+
+   const struct efi_initrd_dp fdt_prefix = {


We need to rename efi_initrd_dp to something more generic in the future


+   .vendor = {
+   {
+   DEVICE_PATH_TYPE_MEDIA_DEVICE,
+   DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
+   sizeof(fdt_prefix.vendor),
+   },
+   EFI_FDT_GUID,
+   },
+   .end = {
+   DEVICE_PATH_TYPE_END,
+   DEVICE_PATH_SUB_TYPE_END,
+   sizeof(initrd_prefix.end),
 }
 };



[...]


+/**
+ * efi_load_option_dp_join() - join device-paths for load option
+ *
+ * @dp:in: binary device-path, out: joined device-path
+ * @dp_size:   size of joined device-path
+ * @initrd_dp: initrd device-path or NULL
+ * @fdt_dp:device-tree device-path or NULL
+ * Return: status_code
+ */
+efi_status_t efi_load_option_dp_join(struct efi_device_path **dp,
+size_t *dp_size,
+struct efi_device_path *initrd_dp,
+struct efi_device_path *fdt_dp)
+{
+   if (!dp)


Should we add && !*dp here?


efi_dp_concat() handles the case of one or both device-paths being NULL.




+   return EFI_INVALID_PARAMETER;
+
+   *dp_size = efi_dp_size(*dp);
+
+   if (initrd_dp) {
+   struct efi_device_path *tmp_dp = *dp;
+
+   *dp = efi_dp_concat(tmp_dp, initrd_dp, *dp_size);
+   efi_free_pool(tmp_dp);
+   if (!*dp)
+   return EFI_OUT_OF_RESOURCES;
+   *dp_size += efi_dp_size(initrd_dp) + sizeof(END);
+   }
+
+   if (fdt_dp) {
+   struct efi_device_path *tmp_dp = *dp;
+
+   *dp = efi_dp_concat(tmp_dp, fdt_dp, *dp_size);
+   efi_free_pool(tmp_dp);
+   if (!dp)
+   return EFI_OUT_OF_RESOURCES;
+   *dp_size += efi_dp_size(fdt_dp) + sizeof(END);
+   }
+
+   *dp_size += sizeof(END);


Why do we have to account for the end node twice if either fdt_dp or
initrd_dp are found?


This is the length of the END node of the binary.

Best regards

Heinrich



Thanks
/Ilias

+
+   return EFI_SUCCESS;
+}
+
  const struct guid_to_hash_map {
 efi_guid_t guid;
 const char algo[32];
--
2.43.0





Re: [PATCH v2 1/8] efi_loader: allow concatenation with contained end node

2024-05-28 Thread Ilias Apalodimas
On Tue, 28 May 2024 at 17:43, Heinrich Schuchardt
 wrote:
>
> Allow appending a device-path to a device-path that contains an end node
> as separator. We need this feature for creating boot options specifying
> kernel, initrd, and dtb.
>
> Signed-off-by: Heinrich Schuchardt 
> ---
> v2:
> update efi_dp_concat() instead of new function efi_dp_merge()
> ---
>  cmd/eficonfig.c|  6 +++---
>  cmd/efidebug.c |  4 ++--
>  include/efi_loader.h   |  2 +-
>  lib/efi_loader/efi_bootbin.c   |  2 +-
>  lib/efi_loader/efi_bootmgr.c   |  2 +-
>  lib/efi_loader/efi_boottime.c  |  2 +-
>  lib/efi_loader/efi_device_path.c   | 20 +---
>  lib/efi_loader/efi_device_path_utilities.c |  2 +-
>  8 files changed, 23 insertions(+), 17 deletions(-)
>
> diff --git a/cmd/eficonfig.c b/cmd/eficonfig.c
> index 0ba92c60e03..b13d9a3d2d9 100644
> --- a/cmd/eficonfig.c
> +++ b/cmd/eficonfig.c
> @@ -531,7 +531,7 @@ struct efi_device_path 
> *eficonfig_create_device_path(struct efi_device_path *dp_
> dp = efi_dp_shorten(dp_volume);
> if (!dp)
> dp = dp_volume;
> -   dp = efi_dp_concat(dp, >dp, false);
> +   dp = efi_dp_concat(dp, >dp, 0);
> free(buf);
>
> return dp;
> @@ -1485,7 +1485,7 @@ static efi_status_t eficonfig_edit_boot_option(u16 
> *varname, struct eficonfig_bo
> goto out;
> }
> initrd_dp = efi_dp_concat((const struct efi_device_path 
> *)_dp,
> - dp, false);
> + dp, 0);
> efi_free_pool(dp);
> }
>
> @@ -1496,7 +1496,7 @@ static efi_status_t eficonfig_edit_boot_option(u16 
> *varname, struct eficonfig_bo
> }
> final_dp_size = efi_dp_size(dp) + sizeof(END);
> if (initrd_dp) {
> -   final_dp = efi_dp_concat(dp, initrd_dp, true);
> +   final_dp = efi_dp_concat(dp, initrd_dp, 1);
> final_dp_size += efi_dp_size(initrd_dp) + sizeof(END);
> } else {
> final_dp = efi_dp_dup(dp);
> diff --git a/cmd/efidebug.c b/cmd/efidebug.c
> index c2c525f2351..762027daf8a 100644
> --- a/cmd/efidebug.c
> +++ b/cmd/efidebug.c
> @@ -697,7 +697,7 @@ struct efi_device_path *create_initrd_dp(const char *dev, 
> const char *part,
> short_fp = tmp_fp;
>
> initrd_dp = efi_dp_concat((const struct efi_device_path *)_dp,
> - short_fp, false);
> + short_fp, 0);
>
>  out:
> efi_free_pool(tmp_dp);
> @@ -917,7 +917,7 @@ static int do_efi_boot_add(struct cmd_tbl *cmdtp, int 
> flag,
> goto out;
> }
>
> -   final_fp = efi_dp_concat(file_path, initrd_dp, true);
> +   final_fp = efi_dp_concat(file_path, initrd_dp, 1);
> if (!final_fp) {
> printf("Cannot create final device path\n");
> r = CMD_RET_FAILURE;
> diff --git a/include/efi_loader.h b/include/efi_loader.h
> index 9600941aa32..ddf2e41a95c 100644
> --- a/include/efi_loader.h
> +++ b/include/efi_loader.h
> @@ -946,7 +946,7 @@ struct efi_device_path *efi_dp_from_lo(struct 
> efi_load_option *lo,
>const efi_guid_t *guid);
>  struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
>   const struct efi_device_path *dp2,
> - bool split_end_node);
> + size_t split_end_node);
>  struct efi_device_path *search_gpt_dp_node(struct efi_device_path 
> *device_path);
>  efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 
> *data,
>  efi_uintn_t *size);
> diff --git a/lib/efi_loader/efi_bootbin.c b/lib/efi_loader/efi_bootbin.c
> index b7910f78fb6..a87006b3c0e 100644
> --- a/lib/efi_loader/efi_bootbin.c
> +++ b/lib/efi_loader/efi_bootbin.c
> @@ -150,7 +150,7 @@ efi_status_t efi_run_image(void *source_buffer, 
> efi_uintn_t source_size)
> msg_path = file_path;
> } else {
> file_path = efi_dp_concat(bootefi_device_path,
> - bootefi_image_path, false);
> + bootefi_image_path, 0);
> msg_path = bootefi_image_path;
> log_debug("Loaded from disk\n");
> }
> diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
> index 7da3139f917..b0bf21cf841 100644
> --- a/lib/efi_loader/efi_bootmgr.c
> +++ b/lib/efi_loader/efi_bootmgr.c
> @@ -130,7 +130,7 @@ static efi_status_t try_load_from_file_path(efi_handle_t 
> *fs_handles,
> if (!dp)
> continue;
>
> -   dp = efi_dp_concat(dp, fp, false);
> +   

Re: [PATCH v2 8/8] efi_loader: load distro dtb in bootmgr

2024-05-28 Thread Ilias Apalodimas
On Tue, 28 May 2024 at 17:43, Heinrich Schuchardt
 wrote:
>
> If no device-tree is specified, try to load a device-tree from the boot
> device use the $fdtfile concatenated to either of the paths '/dtb/', '/',
> '/dtb/current/'.
>
> Signed-off-by: Heinrich Schuchardt 
> ---
> v2:
> no change
> ---
>  include/efi_loader.h |  2 ++
>  lib/efi_loader/efi_bootmgr.c | 13 +--
>  lib/efi_loader/efi_fdt.c | 44 
>  3 files changed, 57 insertions(+), 2 deletions(-)
>
> diff --git a/include/efi_loader.h b/include/efi_loader.h
> index ab7bed22971..6c993e1a694 100644
> --- a/include/efi_loader.h
> +++ b/include/efi_loader.h
> @@ -1205,4 +1205,6 @@ efi_status_t efi_load_option_dp_join(struct 
> efi_device_path **dp,
>
>  int efi_get_distro_fdt_name(char *fname, int size, int seq);
>
> +void efi_load_distro_fdt(void **fdt, efi_uintn_t *fdt_size);
> +
>  #endif /* _EFI_LOADER_H */
> diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
> index b08d6e97ea3..68440542a37 100644
> --- a/lib/efi_loader/efi_bootmgr.c
> +++ b/lib/efi_loader/efi_bootmgr.c
> @@ -1249,7 +1249,8 @@ efi_status_t efi_bootmgr_run(void *fdt)
> efi_handle_t handle;
> void *load_options;
> efi_status_t ret;
> -   void *fdt_lo;
> +   void *fdt_lo, *fdt_distro = NULL;
> +   efi_uintn_t fdt_size;
>
> /* Initialize EFI drivers */
> ret = efi_init_obj_list();
> @@ -1269,6 +1270,10 @@ efi_status_t efi_bootmgr_run(void *fdt)
> return ret;
> if (fdt_lo)
> fdt = fdt_lo;
> +   if (!fdt) {
> +   efi_load_distro_fdt(_distro, _size);
> +   fdt = fdt_distro;
> +   }
> }
>
> /*
> @@ -1277,8 +1282,12 @@ efi_status_t efi_bootmgr_run(void *fdt)
>  */
> ret = efi_install_fdt(fdt);
>
> -   if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
> +   if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
> free(fdt_lo);
> +   if (fdt_distro)
> +   efi_free_pages((uintptr_t)fdt_distro,
> +  efi_size_in_pages(fdt_size));
> +   }
>
> if (ret != EFI_SUCCESS) {
> if (EFI_CALL(efi_unload_image(handle)) == EFI_SUCCESS)
> diff --git a/lib/efi_loader/efi_fdt.c b/lib/efi_loader/efi_fdt.c
> index 0edf0c1e2fc..86ba00c2bdd 100644
> --- a/lib/efi_loader/efi_fdt.c
> +++ b/lib/efi_loader/efi_fdt.c
> @@ -71,3 +71,47 @@ int efi_get_distro_fdt_name(char *fname, int size, int seq)
>
> return 0;
>  }
> +
> +/**
> + * efi_load_distro_fdt() - load distro device-tree
> + *
> + * @fdt:   on return device-tree, must be freed via efi_free_pages()
> + * @fdt_size:  buffer size
> + */
> +void efi_load_distro_fdt(void **fdt, efi_uintn_t *fdt_size)
> +{
> +   struct efi_device_path *rem, *dp;
> +   efi_status_t  ret;
> +   efi_handle_t device;
> +
> +   *fdt = NULL;
> +
> +   dp = efi_get_dp_from_boot(NULL);
> +   if (!dp)
> +   return;
> +   device = efi_dp_find_obj(dp, NULL, );
> +   ret = efi_search_protocol(device, 
> _simple_file_system_protocol_guid,
> + NULL);
> +   if (ret != EFI_SUCCESS)
> +   goto err;
> +   memcpy(rem, , sizeof(END));
> +
> +   /* try the various available names */
> +   for (int seq = 0; ; ++seq) {
> +   struct efi_device_path *file;
> +   char buf[255];
> +
> +   if (efi_get_distro_fdt_name(buf, sizeof(buf), seq))
> +   break;
> +   file = efi_dp_from_file(dp, buf);
> +   if (!file)
> +   break;
> +   ret = efi_load_image_from_path(true, file, fdt, fdt_size);
> +   efi_free_pool(file);
> +   if (ret == EFI_SUCCESS)
> +   break;
> +   }
> +
> +err:
> +   efi_free_pool(dp);
> +}
> --
> 2.43.0
>

Reviewed-by: Ilias Apalodimas 


Re: [PATCH v4] fdt: automatically add /chosen/kaslr-seed if DM_RNG is enabled

2024-05-28 Thread Tim Harvey
On Mon, May 27, 2024 at 1:30 AM Michal Simek  wrote:
>
>
>
> On 5/25/24 22:02, Tim Harvey wrote:
> > If RANDOMIZE_BASE is enabled in the Linux kernel instructing it to
> > randomize the virtual address at which the kernel image is loaded, it
> > expects entropy to be provided by the bootloader by populating
> > /chosen/kaslr-seed with a 64-bit value from source of entropy at boot.
> >
> > If we have DM_RNG enabled populate this value automatically when
> > fdt_chosen is called. We skip this if ARMV8_SEC_FIRMWARE_SUPPORT
> > is enabled as it's implementation uses a different source of entropy
> > that is not yet implemented as DM_RNG. We also skip this if
> > MEASURED_BOOT is enabled as in that case any modifications to the
> > dt will cause measured boot to fail (although there are many other
> > places the dt is altered).
> >
> > As this fdt node is added elsewhere create a library function and
> > use it to deduplicate code. We will provide a parameter to specify the
> > index of the rng device as well as a boolean to overwrite if present.
> >
> > For our automatic injection, we will use the first rng device and
> > not overwrite if already present with a non-zero value (which may
> > have been populated by an earlier boot stage). This way if a board
> > specific ft_board_setup() function wants to customize this behavior
> > it can call fdt_kaslrseed with a rng device index of its choosing and
> > set overwrite true.
> >
> > Note that the kalsrseed command (CMD_KASLRSEED) is likely pointless now
> > but left in place in case boot scripts exist that rely on this command
> > existing and returning success. An informational message is printed to
> > alert users of this command that it is likely no longer needed.
> >
> > Note that the Kernel's EFI STUB only relies on EFI_RNG_PROTOCOL for
> > randomization and completely ignores the kaslr-seed for its own
> > randomness needs (i.e the randomization of the physical placement of
> > the kernel). It gets weeded out from the DTB that gets handed over via
> > efi_install_fdt() as it would also mess up the measured boot DTB TPM
> > measurements as well.
> >
> > Signed-off-by: Tim Harvey 
> > Cc: Michal Simek 
> > Cc: Andy Yan 
> > Cc: Akash Gajjar 
> > Cc: Ilias Apalodimas 
> > Cc: Simon Glass 
> > Cc: Patrick Delaunay 
> > Cc: Patrice Chotard 
> > Cc: Devarsh Thakkar 
> > Cc: Heinrich Schuchardt 
> > Cc: Hugo Villeneuve 
> > Cc: Marek Vasut 
> > Cc: Tom Rini 
> > Cc: Chris Morgan 
> > ---
> > v4:
> >   - add missing /n to notice in kaslrseed cmd
> >   - combine ints in declaration
> >   - remove unused vars from board/xilinx/common/board.c ft_board_setup
> > v3:
> >   - skip if CONFIG_MEASURED_BOOT
> >   - fix skip for CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT
> >   - pass in rng index and bool to specify overwrite
> >   - remove duplicate error strings printed outside of fdt_kaslrseed
> >   - added note to commit log about how EFI STUB weeds out kalsr-seed
> > v2:
> >   - fix typo in commit msg
> >   - use stack for seed to avoid unecessary malloc/free
> >   - move to a library function and deduplicate code by using it
> > elsewhere
> > ---
> >   board/xilinx/common/board.c | 40 --
> >   boot/fdt_support.c  |  6 +
> >   boot/pxe_utils.c| 35 ++
> >   cmd/kaslrseed.c | 45 +-
> >   include/kaslrseed.h | 19 ++
> >   lib/Makefile|  1 +
> >   lib/kaslrseed.c | 49 +
> >   7 files changed, 83 insertions(+), 112 deletions(-)
> >   create mode 100644 include/kaslrseed.h
> >   create mode 100644 lib/kaslrseed.c
> >
> > diff --git a/board/xilinx/common/board.c b/board/xilinx/common/board.c
> > index 30a81376ac41..0b43407b9e94 100644
> > --- a/board/xilinx/common/board.c
> > +++ b/board/xilinx/common/board.c
> > @@ -701,11 +701,6 @@ phys_addr_t board_get_usable_ram_top(phys_size_t 
> > total_size)
> >   #define MAX_RAND_SIZE 8
> >   int ft_board_setup(void *blob, struct bd_info *bd)
> >   {
> > - size_t n = MAX_RAND_SIZE;
> > - struct udevice *dev;
> > - u8 buf[MAX_RAND_SIZE];
> > - int nodeoffset, ret;
> > -
> >   static const struct node_info nodes[] = {
> >   { "arm,pl353-nand-r2p1", MTD_DEV_TYPE_NAND, },
> >   };
> > @@ -713,41 +708,6 @@ int ft_board_setup(void *blob, struct bd_info *bd)
> >   if (IS_ENABLED(CONFIG_FDT_FIXUP_PARTITIONS) && 
> > IS_ENABLED(CONFIG_NAND_ZYNQ))
> >   fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
> >
> > - if (uclass_get_device(UCLASS_RNG, 0, ) || !dev) {
> > - debug("No RNG device\n");
> > - return 0;
> > - }
> > -
> > - if (dm_rng_read(dev, buf, n)) {
> > - debug("Reading RNG failed\n");
> > - return 0;
> > - }
> > -
> > - if (!blob) {
> > - debug("No FDT memory address configured. Please configure\n"
> > -   

Re: [PATCH v2 7/8] efi_loader: export efi_load_image_from_path

2024-05-28 Thread Ilias Apalodimas
On Tue, 28 May 2024 at 17:43, Heinrich Schuchardt
 wrote:
>
> We can reuse this function to load the device-tree.
>
> Signed-off-by: Heinrich Schuchardt 
> ---
> v2:
> Move unrelated changes to different patch.
> ---
>  include/efi_loader.h  | 4 
>  lib/efi_loader/efi_boottime.c | 1 -
>  2 files changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/include/efi_loader.h b/include/efi_loader.h
> index 1b4bc987a23..ab7bed22971 100644
> --- a/include/efi_loader.h
> +++ b/include/efi_loader.h
> @@ -664,6 +664,10 @@ efi_status_t EFIAPI efi_load_image(bool boot_policy,
>void *source_buffer,
>efi_uintn_t source_size,
>efi_handle_t *image_handle);
> +/* Load image from path */
> +efi_status_t efi_load_image_from_path(bool boot_policy,
> + struct efi_device_path *file_path,
> + void **buffer, efi_uintn_t *size);
>  /* Start image */
>  efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
> efi_uintn_t *exit_data_size,
> diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c
> index 630c5f52c4f..eedc5f39549 100644
> --- a/lib/efi_loader/efi_boottime.c
> +++ b/lib/efi_loader/efi_boottime.c
> @@ -1996,7 +1996,6 @@ error:
>   * @size:  size of the loaded image
>   * Return: status code
>   */
> -static
>  efi_status_t efi_load_image_from_path(bool boot_policy,
>   struct efi_device_path *file_path,
>   void **buffer, efi_uintn_t *size)
> --
> 2.43.0
>

Reviewed-by: Ilias Apalodimas 


Re: [PATCH v2 6/8] efi_loader: return binary from efi_dp_from_lo()

2024-05-28 Thread Ilias Apalodimas
On Tue, 28 May 2024 at 17:43, Heinrich Schuchardt
 wrote:
>
> For finding distro supplied device-trees we need to know from which device
> we are booting. This can be identified via the device-path of the binary.
>
> Up to now efi_dp_from_lo() only could return the initrd or fdt device-path.
> Allow returning the binary device-path, too.
>
> Signed-off-by: Heinrich Schuchardt 
> ---
> v2:
> no change
> ---
>  lib/efi_loader/efi_device_path.c | 20 
>  1 file changed, 12 insertions(+), 8 deletions(-)
>
> diff --git a/lib/efi_loader/efi_device_path.c 
> b/lib/efi_loader/efi_device_path.c
> index c8c8d54f733..52e3313c23a 100644
> --- a/lib/efi_loader/efi_device_path.c
> +++ b/lib/efi_loader/efi_device_path.c
> @@ -1133,17 +1133,18 @@ ssize_t efi_dp_check_length(const struct 
> efi_device_path *dp,
>  }
>
>  /**
> - * efi_dp_from_lo() - Get the instance of a VenMedia node in a
> - *multi-instance device path that matches
> - *a specific GUID. This kind of device paths
> - *is found in Boot options describing an
> - *initrd location
> + * efi_dp_from_lo() - get device-path from load option
>   *
> - * @lo:EFI_LOAD_OPTION containing a valid device path
> - * @guid:  guid to search for
> + * The load options in U-Boot may contain multiple concatenated device-paths.
> + * The first device-path indicates the EFI binary to execute. Subsequent
> + * device-paths start with a VenMedia node where the GUID identifies the
> + * function (initrd or fdt).
> + *
> + * @lo:EFI load option containing a valid device path
> + * @guid:  GUID identifying device-path or NULL for the EFI binary
>   *
>   * Return:
> - * device path including the VenMedia node or NULL.
> + * device path excluding the matched VenMedia node or NULL.

thanks for catching the typo as well

>   * Caller must free the returned value.
>   */
>  struct
> @@ -1154,6 +1155,9 @@ efi_device_path *efi_dp_from_lo(struct efi_load_option 
> *lo,
> struct efi_device_path_vendor *vendor;
> int lo_len = lo->file_path_length;
>
> +   if (!guid)
> +   return efi_dp_dup(fp);
> +
> for (; lo_len >=  sizeof(struct efi_device_path);
>  lo_len -= fp->length, fp = (void *)fp + fp->length) {
> if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
> --
> 2.43.0
>
Reviewed-by: Ilias Apalodimas 


Re: [PATCH v2 5/8] efi_loader: move distro_efi_get_fdt_name()

2024-05-28 Thread Ilias Apalodimas
On Tue, 28 May 2024 at 17:43, Heinrich Schuchardt
 wrote:
>
> Move distro_efi_get_fdt_name() to a separate C module
> and rename it to efi_get_distro_fdt_name().
>
> Signed-off-by: Heinrich Schuchardt 
> ---
> v2:
> rebase patch
> ---
>  boot/bootmeth_efi.c  | 60 ++---
>  include/efi_loader.h |  2 ++
>  lib/efi_loader/Makefile  |  1 +
>  lib/efi_loader/efi_fdt.c | 73 
>  4 files changed, 78 insertions(+), 58 deletions(-)
>  create mode 100644 lib/efi_loader/efi_fdt.c
>
> diff --git a/boot/bootmeth_efi.c b/boot/bootmeth_efi.c
> index aebc5207fc0..40da77c497b 100644
> --- a/boot/bootmeth_efi.c
> +++ b/boot/bootmeth_efi.c
> @@ -144,62 +144,6 @@ static int distro_efi_check(struct udevice *dev, struct 
> bootflow_iter *iter)
> return 0;
>  }
>
> -/**
> - * distro_efi_get_fdt_name() - Get the filename for reading the .dtb file
> - *
> - * @fname: Place to put filename
> - * @size: Max size of filename
> - * @seq: Sequence number, to cycle through options (0=first)
> - * Returns: 0 on success, -ENOENT if the "fdtfile" env var does not exist,
> - * -EINVAL if there are no more options, -EALREADY if the control FDT should 
> be
> - * used
> - */
> -static int distro_efi_get_fdt_name(char *fname, int size, int seq)
> -{
> -   const char *fdt_fname;
> -   const char *prefix;
> -
> -   /* select the prefix */
> -   switch (seq) {
> -   case 0:
> -   /* this is the default */
> -   prefix = "/dtb";
> -   break;
> -   case 1:
> -   prefix = "";
> -   break;
> -   case 2:
> -   prefix = "/dtb/current";
> -   break;
> -   default:
> -   return log_msg_ret("pref", -EINVAL);
> -   }
> -
> -   fdt_fname = env_get("fdtfile");
> -   if (fdt_fname) {
> -   snprintf(fname, size, "%s/%s", prefix, fdt_fname);
> -   log_debug("Using device tree: %s\n", fname);
> -   } else if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE)) {
> -   strcpy(fname, "");
> -   return log_msg_ret("pref", -EALREADY);
> -   /* Use this fallback only for 32-bit ARM */
> -   } else if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_ARM64)) {
> -   const char *soc = env_get("soc");
> -   const char *board = env_get("board");
> -   const char *boardver = env_get("boardver");
> -
> -   /* cf the code in label_boot() which seems very complex */
> -   snprintf(fname, size, "%s/%s%s%s%s.dtb", prefix,
> -soc ? soc : "", soc ? "-" : "", board ? board : "",
> -boardver ? boardver : "");
> -   log_debug("Using default device tree: %s\n", fname);
> -   } else {
> -   return log_msg_ret("env", -ENOENT);
> -   }
> -
> -   return 0;
> -}
> -
>  /*
>   * distro_efi_try_bootflow_files() - Check that files are present
>   *
> @@ -241,7 +185,7 @@ static int distro_efi_try_bootflow_files(struct udevice 
> *dev,
> ret = -ENOENT;
> *fname = '\0';
> for (seq = 0; ret == -ENOENT; seq++) {
> -   ret = distro_efi_get_fdt_name(fname, sizeof(fname), seq);
> +   ret = efi_get_distro_fdt_name(fname, sizeof(fname), seq);
> if (ret == -EALREADY)
> bflow->flags = BOOTFLOWF_USE_PRIOR_FDT;
> if (!ret) {
> @@ -340,7 +284,7 @@ static int distro_efi_read_bootflow_net(struct bootflow 
> *bflow)
> sprintf(file_addr, "%lx", fdt_addr);
>
> /* We only allow the first prefix with PXE */
> -   ret = distro_efi_get_fdt_name(fname, sizeof(fname), 0);
> +   ret = efi_get_distro_fdt_name(fname, sizeof(fname), 0);
> if (ret)
> return log_msg_ret("nam", ret);
>
> diff --git a/include/efi_loader.h b/include/efi_loader.h
> index 1236eecff0f..1b4bc987a23 100644
> --- a/include/efi_loader.h
> +++ b/include/efi_loader.h
> @@ -1199,4 +1199,6 @@ efi_status_t efi_load_option_dp_join(struct 
> efi_device_path **dp,
>  struct efi_device_path *initrd_dp,
>  struct efi_device_path *fdt_dp);
>
> +int efi_get_distro_fdt_name(char *fname, int size, int seq);
> +
>  #endif /* _EFI_LOADER_H */
> diff --git a/lib/efi_loader/Makefile b/lib/efi_loader/Makefile
> index 034e366967f..2af6f2066b5 100644
> --- a/lib/efi_loader/Makefile
> +++ b/lib/efi_loader/Makefile
> @@ -59,6 +59,7 @@ obj-y += efi_device_path.o
>  obj-$(CONFIG_EFI_DEVICE_PATH_TO_TEXT) += efi_device_path_to_text.o
>  obj-$(CONFIG_EFI_DEVICE_PATH_UTIL) += efi_device_path_utilities.o
>  obj-y += efi_dt_fixup.o
> +obj-y += efi_fdt.o
>  obj-y += efi_file.o
>  obj-$(CONFIG_EFI_LOADER_HII) += efi_hii.o
>  obj-y += efi_image_loader.o
> diff --git a/lib/efi_loader/efi_fdt.c b/lib/efi_loader/efi_fdt.c
> new file mode 100644
> index 

Re: [PATCH v2 4/8] efi_loader: load device-tree specified in boot option

2024-05-28 Thread Ilias Apalodimas
[...]

> +
> +   *fdt = malloc(filesize);

Use calloc instead

> +   if (!*fdt) {
> +   log_err("Out of memory\n");
> +   ret = EFI_OUT_OF_RESOURCES;
> +   goto out;
> +   }
> +   ret = EFI_CALL(f->read(f, , *fdt));
> +   if (ret != EFI_SUCCESS) {
> +   log_err("Can't read fdt\n");
> +   free(*fdt);
> +   *fdt = NULL;
> +   }
> +
> +out:
> +   efi_free_pool(dp);
> +   if (f)
> +   EFI_CALL(f->close(f));
> +
> +   return ret;
> +}
> +
>  /**
>   * efi_bootmgr_run() - execute EFI boot manager
>   * @fdt:   Flat device tree
> @@ -1200,6 +1253,7 @@ efi_status_t efi_bootmgr_run(void *fdt)
> efi_handle_t handle;
> void *load_options;
> efi_status_t ret;
> +   void *fdt_lo;
>
> /* Initialize EFI drivers */
> ret = efi_init_obj_list();
> @@ -1215,7 +1269,23 @@ efi_status_t efi_bootmgr_run(void *fdt)
> return ret;
> }
>
> +   if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
> +   ret = load_fdt_from_load_option(_lo);
> +   if (ret != EFI_SUCCESS)
> +   return ret;
> +   if (fdt_lo)
> +   fdt = fdt_lo;
> +   }
> +
> +   /*
> +* Needed in ACPI case to create reservations based on
> +* control device-tree.
> +*/
> ret = efi_install_fdt(fdt);
> +
> +   if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
> +   free(fdt_lo);
> +
> if (ret != EFI_SUCCESS) {
> if (EFI_CALL(efi_unload_image(handle)) == EFI_SUCCESS)
> free(load_options);
> --
> 2.43.0
>

Reviewed-by: Ilias Apalodimas 


Re: [PATCH v2 3/8] cmd: efidebug: add support for setting fdt

2024-05-28 Thread Ilias Apalodimas
On Tue, 28 May 2024 at 17:43, Heinrich Schuchardt
 wrote:
>
> We already support creating a load option where the device-path
> field contains the concatenation of the binary device-path and
> optionally the device path of the initrd which we expose via the
> EFI_LOAD_FILE2_PROTOCOL.
>
> Allow to append another device-path pointing to the device-tree
> identified by the device-tree GUID.
>
> Signed-off-by: Heinrich Schuchardt 
> ---
> v2:
> Rename create_initrd_dp() to create_lo_dp_part().
> Use enum as parameter for create_lo_dp_part().
> Use function efi_load_option_dp_join() to avoid code duplication.
> ---
>  cmd/efidebug.c | 130 +++--
>  1 file changed, 93 insertions(+), 37 deletions(-)
>
> diff --git a/cmd/efidebug.c b/cmd/efidebug.c
> index 762027daf8a..966b4b83361 100644
> --- a/cmd/efidebug.c
> +++ b/cmd/efidebug.c
> @@ -654,38 +654,80 @@ static int do_efi_show_tables(struct cmd_tbl *cmdtp, 
> int flag,
>  }
>
>  /**
> - * create_initrd_dp() - create a special device for our Boot### option
> + * enum efi_lo_dp_part - part of device path in load option
> + */
> +enum efi_lo_dp_part {
> +   /** @EFI_LO_DP_PART_BINARY: binary */
> +   EFI_LO_DP_PART_BINARY,
> +   /** @EFI_LO_DP_PART_INITRD: initial RAM disk */
> +   EFI_LO_DP_PART_INITRD,
> +   /** EFI_LP_DP_PART_FDT: device-tree */
> +   EFI_LP_DP_PART_FDT,
> +};
> +
> +/**
> + * create_lo_dp() - create a special device path for our Boot### option
>   *
>   * @dev:   device
>   * @part:  disk partition
>   * @file:  filename
>   * @shortform: create short form device path
> + * @type:  part of device path to be created
>   * Return: pointer to the device path or ERR_PTR
>   */
>  static
> -struct efi_device_path *create_initrd_dp(const char *dev, const char *part,
> -const char *file, int shortform)
> +struct efi_device_path *create_lo_dp_part(const char *dev, const char *part,
> + const char *file, bool shortform,
> + enum efi_lo_dp_part type)
>
>  {
> struct efi_device_path *tmp_dp = NULL, *tmp_fp = NULL, *short_fp = 
> NULL;
> -   struct efi_device_path *initrd_dp = NULL;
> +   struct efi_device_path *dp = NULL;
> +   const struct efi_device_path *dp_prefix;
> efi_status_t ret;
> -   const struct efi_initrd_dp id_dp = {
> +   const struct efi_initrd_dp fdt_dp = {
> +   .vendor = {
> +   {
> +   DEVICE_PATH_TYPE_MEDIA_DEVICE,
> +   DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
> +   sizeof(fdt_dp.vendor),
> +   },
> +   EFI_FDT_GUID,
> +   },
> +   .end = {
> +   DEVICE_PATH_TYPE_END,
> +   DEVICE_PATH_SUB_TYPE_END,
> +   sizeof(fdt_dp.end),
> +   }
> +   };
> +   const struct efi_initrd_dp initrd_dp = {
> .vendor = {
> {
> DEVICE_PATH_TYPE_MEDIA_DEVICE,
> DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
> -   sizeof(id_dp.vendor),
> +   sizeof(initrd_dp.vendor),
> },
> EFI_INITRD_MEDIA_GUID,
> },
> .end = {
> DEVICE_PATH_TYPE_END,
> DEVICE_PATH_SUB_TYPE_END,
> -   sizeof(id_dp.end),
> +   sizeof(initrd_dp.end),
> }
> };
>
> +   switch (type) {
> +   case EFI_LO_DP_PART_INITRD:
> +   dp_prefix = _dp.vendor.dp;
> +   break;
> +   case EFI_LP_DP_PART_FDT:
> +   dp_prefix = _dp.vendor.dp;
> +   break;
> +   default:
> +   dp_prefix = NULL;
> +   break;
> +   }
> +
> ret = efi_dp_from_name(dev, part, file, _dp, _fp);
> if (ret != EFI_SUCCESS) {
> printf("Cannot create device path for \"%s %s\"\n", part, 
> file);
> @@ -696,13 +739,12 @@ struct efi_device_path *create_initrd_dp(const char 
> *dev, const char *part,
> if (!short_fp)
> short_fp = tmp_fp;
>
> -   initrd_dp = efi_dp_concat((const struct efi_device_path *)_dp,
> - short_fp, 0);
> +   dp = efi_dp_concat(dp_prefix, short_fp, 0);
>
>  out:
> efi_free_pool(tmp_dp);
> efi_free_pool(tmp_fp);
> -   return initrd_dp;
> +   return dp;
>  }
>
>  /**
> @@ -793,9 +835,8 @@ static int do_efi_boot_add(struct cmd_tbl *cmdtp, int 
> flag,
> efi_guid_t guid;
> u16 *label;
> struct efi_device_path *file_path = NULL;
> -   struct efi_device_path *fp_free = NULL;
> -   struct 

Re: [PATCH v2 2/8] cmd: eficonfig: add support for setting fdt

2024-05-28 Thread Ilias Apalodimas
Hi Heinrich,

[...]

>
> -   const struct efi_initrd_dp id_dp = {
> +   const struct efi_initrd_dp initrd_prefix = {
> .vendor = {
> {
> DEVICE_PATH_TYPE_MEDIA_DEVICE,
> DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
> -   sizeof(id_dp.vendor),
> +   sizeof(initrd_prefix.vendor),
> },
> EFI_INITRD_MEDIA_GUID,
> },
> .end = {
> DEVICE_PATH_TYPE_END,
> DEVICE_PATH_SUB_TYPE_END,
> -   sizeof(id_dp.end),
> +   sizeof(initrd_prefix.end),
> +   }
> +   };
> +
> +   const struct efi_initrd_dp fdt_prefix = {

We need to rename efi_initrd_dp to something more generic in the future

> +   .vendor = {
> +   {
> +   DEVICE_PATH_TYPE_MEDIA_DEVICE,
> +   DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
> +   sizeof(fdt_prefix.vendor),
> +   },
> +   EFI_FDT_GUID,
> +   },
> +   .end = {
> +   DEVICE_PATH_TYPE_END,
> +   DEVICE_PATH_SUB_TYPE_END,
> +   sizeof(initrd_prefix.end),
> }
> };
>

[...]

> +/**
> + * efi_load_option_dp_join() - join device-paths for load option
> + *
> + * @dp:in: binary device-path, out: joined device-path
> + * @dp_size:   size of joined device-path
> + * @initrd_dp: initrd device-path or NULL
> + * @fdt_dp:device-tree device-path or NULL
> + * Return: status_code
> + */
> +efi_status_t efi_load_option_dp_join(struct efi_device_path **dp,
> +size_t *dp_size,
> +struct efi_device_path *initrd_dp,
> +struct efi_device_path *fdt_dp)
> +{
> +   if (!dp)

Should we add && !*dp here?

> +   return EFI_INVALID_PARAMETER;
> +
> +   *dp_size = efi_dp_size(*dp);
> +
> +   if (initrd_dp) {
> +   struct efi_device_path *tmp_dp = *dp;
> +
> +   *dp = efi_dp_concat(tmp_dp, initrd_dp, *dp_size);
> +   efi_free_pool(tmp_dp);
> +   if (!*dp)
> +   return EFI_OUT_OF_RESOURCES;
> +   *dp_size += efi_dp_size(initrd_dp) + sizeof(END);
> +   }
> +
> +   if (fdt_dp) {
> +   struct efi_device_path *tmp_dp = *dp;
> +
> +   *dp = efi_dp_concat(tmp_dp, fdt_dp, *dp_size);
> +   efi_free_pool(tmp_dp);
> +   if (!dp)
> +   return EFI_OUT_OF_RESOURCES;
> +   *dp_size += efi_dp_size(fdt_dp) + sizeof(END);
> +   }
> +
> +   *dp_size += sizeof(END);

Why do we have to account for the end node twice if either fdt_dp or
initrd_dp are found?

Thanks
/Ilias
> +
> +   return EFI_SUCCESS;
> +}
> +
>  const struct guid_to_hash_map {
> efi_guid_t guid;
> const char algo[32];
> --
> 2.43.0
>


Re: [PATCH] xilinx: zynqmp: Allow multiboot environment write even in saved environment

2024-05-28 Thread Kory Maincent
On Tue, 28 May 2024 16:53:42 +0200
Michal Simek  wrote:

> On 5/28/24 16:36, Kory Maincent wrote:
> > Once the environment was saved, we could not retrieve information about  
> 
> nit: use imperative mood.

Ah indeed.

> 
> > the multiboot image used. When dealing with firmware updates, this
> > information is necessary alongside the saved environment.  
> 
> what exactly are you trying to do?

Retrieving the multiboot information to know which boot000x.bin image we have
booted. This would allow to check the boot status of a firmware update.

> > Move the multiboot environment set operation before the saved environment
> > check to ensure this information is always available.  
> 
> that make sense.
> 
> > 
> > Signed-off-by: Kory Maincent 
> > ---
> >   board/xilinx/zynqmp/zynqmp.c | 8 
> >   1 file changed, 4 insertions(+), 4 deletions(-)
> > 
> > diff --git a/board/xilinx/zynqmp/zynqmp.c b/board/xilinx/zynqmp/zynqmp.c
> > index f370fb7347a..16292ed1c7e 100644
> > --- a/board/xilinx/zynqmp/zynqmp.c
> > +++ b/board/xilinx/zynqmp/zynqmp.c
> > @@ -519,6 +519,10 @@ int board_late_init(void)
> > usb_ether_init();
> >   #endif
> >   
> > +   multiboot = multi_boot();
> > +   if (multiboot >= 0)  
> 
> likely make sense to remove this check to because you want to have even 
> multiboot=0 too.

I don't understand, this check is >= so it supports multiboot=0.

Regards,
-- 
Köry Maincent, Bootlin
Embedded Linux and kernel engineering
https://bootlin.com


Re: [PATCH] xilinx: zynqmp: Allow multiboot environment write even in saved environment

2024-05-28 Thread Michal Simek




On 5/28/24 16:36, Kory Maincent wrote:

Once the environment was saved, we could not retrieve information about


nit: use imperative mood.


the multiboot image used. When dealing with firmware updates, this
information is necessary alongside the saved environment.


what exactly are you trying to do?



Move the multiboot environment set operation before the saved environment
check to ensure this information is always available.


that make sense.



Signed-off-by: Kory Maincent 
---
  board/xilinx/zynqmp/zynqmp.c | 8 
  1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/board/xilinx/zynqmp/zynqmp.c b/board/xilinx/zynqmp/zynqmp.c
index f370fb7347a..16292ed1c7e 100644
--- a/board/xilinx/zynqmp/zynqmp.c
+++ b/board/xilinx/zynqmp/zynqmp.c
@@ -519,6 +519,10 @@ int board_late_init(void)
usb_ether_init();
  #endif
  
+	multiboot = multi_boot();

+   if (multiboot >= 0)


likely make sense to remove this check to because you want to have even 
multiboot=0 too.



+   env_set_hex("multiboot", multiboot);
+
if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
debug("Saved variables - Skipping\n");
return 0;
@@ -531,10 +535,6 @@ int board_late_init(void)
if (ret)
return ret;
  
-	multiboot = multi_boot();

-   if (multiboot >= 0)
-   env_set_hex("multiboot", multiboot);
-
if (IS_ENABLED(CONFIG_DISTRO_DEFAULTS)) {
ret = boot_targets_setup();
if (ret)


M


Re: [PATCH] sunxi: spl: h616: fix booting from high MMC offset

2024-05-28 Thread Chen-Yu Tsai
On Fri, May 10, 2024 at 7:13 AM Andre Przywara  wrote:
>
> The BootROM in the Allwinner H616 tries to load the initial boot code
> from sector 16 (8KB) of an SD card or eMMC device, but also looks at
> sector 512 (256KB). This helps with GPT formatted cards.
> A "high" boot offset is also used on previous SoCs, but it's sector 256
> (128KB) there instead.
>
> Extend the existing offset calculation code to consider the different
> sector offset when running on an H616 SoC. This allows to load U-Boot
> on any H616 device when the SPL is not located at 8KB.
>
> Signed-off-by: Andre Przywara 

Make sense.

Reviewed-by: Chen-Yu Tsai 


[PATCH v2 5/8] efi_loader: move distro_efi_get_fdt_name()

2024-05-28 Thread Heinrich Schuchardt
Move distro_efi_get_fdt_name() to a separate C module
and rename it to efi_get_distro_fdt_name().

Signed-off-by: Heinrich Schuchardt 
---
v2:
rebase patch
---
 boot/bootmeth_efi.c  | 60 ++---
 include/efi_loader.h |  2 ++
 lib/efi_loader/Makefile  |  1 +
 lib/efi_loader/efi_fdt.c | 73 
 4 files changed, 78 insertions(+), 58 deletions(-)
 create mode 100644 lib/efi_loader/efi_fdt.c

diff --git a/boot/bootmeth_efi.c b/boot/bootmeth_efi.c
index aebc5207fc0..40da77c497b 100644
--- a/boot/bootmeth_efi.c
+++ b/boot/bootmeth_efi.c
@@ -144,62 +144,6 @@ static int distro_efi_check(struct udevice *dev, struct 
bootflow_iter *iter)
return 0;
 }
 
-/**
- * distro_efi_get_fdt_name() - Get the filename for reading the .dtb file
- *
- * @fname: Place to put filename
- * @size: Max size of filename
- * @seq: Sequence number, to cycle through options (0=first)
- * Returns: 0 on success, -ENOENT if the "fdtfile" env var does not exist,
- * -EINVAL if there are no more options, -EALREADY if the control FDT should be
- * used
- */
-static int distro_efi_get_fdt_name(char *fname, int size, int seq)
-{
-   const char *fdt_fname;
-   const char *prefix;
-
-   /* select the prefix */
-   switch (seq) {
-   case 0:
-   /* this is the default */
-   prefix = "/dtb";
-   break;
-   case 1:
-   prefix = "";
-   break;
-   case 2:
-   prefix = "/dtb/current";
-   break;
-   default:
-   return log_msg_ret("pref", -EINVAL);
-   }
-
-   fdt_fname = env_get("fdtfile");
-   if (fdt_fname) {
-   snprintf(fname, size, "%s/%s", prefix, fdt_fname);
-   log_debug("Using device tree: %s\n", fname);
-   } else if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE)) {
-   strcpy(fname, "");
-   return log_msg_ret("pref", -EALREADY);
-   /* Use this fallback only for 32-bit ARM */
-   } else if (IS_ENABLED(CONFIG_ARM) && !IS_ENABLED(CONFIG_ARM64)) {
-   const char *soc = env_get("soc");
-   const char *board = env_get("board");
-   const char *boardver = env_get("boardver");
-
-   /* cf the code in label_boot() which seems very complex */
-   snprintf(fname, size, "%s/%s%s%s%s.dtb", prefix,
-soc ? soc : "", soc ? "-" : "", board ? board : "",
-boardver ? boardver : "");
-   log_debug("Using default device tree: %s\n", fname);
-   } else {
-   return log_msg_ret("env", -ENOENT);
-   }
-
-   return 0;
-}
-
 /*
  * distro_efi_try_bootflow_files() - Check that files are present
  *
@@ -241,7 +185,7 @@ static int distro_efi_try_bootflow_files(struct udevice 
*dev,
ret = -ENOENT;
*fname = '\0';
for (seq = 0; ret == -ENOENT; seq++) {
-   ret = distro_efi_get_fdt_name(fname, sizeof(fname), seq);
+   ret = efi_get_distro_fdt_name(fname, sizeof(fname), seq);
if (ret == -EALREADY)
bflow->flags = BOOTFLOWF_USE_PRIOR_FDT;
if (!ret) {
@@ -340,7 +284,7 @@ static int distro_efi_read_bootflow_net(struct bootflow 
*bflow)
sprintf(file_addr, "%lx", fdt_addr);
 
/* We only allow the first prefix with PXE */
-   ret = distro_efi_get_fdt_name(fname, sizeof(fname), 0);
+   ret = efi_get_distro_fdt_name(fname, sizeof(fname), 0);
if (ret)
return log_msg_ret("nam", ret);
 
diff --git a/include/efi_loader.h b/include/efi_loader.h
index 1236eecff0f..1b4bc987a23 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -1199,4 +1199,6 @@ efi_status_t efi_load_option_dp_join(struct 
efi_device_path **dp,
 struct efi_device_path *initrd_dp,
 struct efi_device_path *fdt_dp);
 
+int efi_get_distro_fdt_name(char *fname, int size, int seq);
+
 #endif /* _EFI_LOADER_H */
diff --git a/lib/efi_loader/Makefile b/lib/efi_loader/Makefile
index 034e366967f..2af6f2066b5 100644
--- a/lib/efi_loader/Makefile
+++ b/lib/efi_loader/Makefile
@@ -59,6 +59,7 @@ obj-y += efi_device_path.o
 obj-$(CONFIG_EFI_DEVICE_PATH_TO_TEXT) += efi_device_path_to_text.o
 obj-$(CONFIG_EFI_DEVICE_PATH_UTIL) += efi_device_path_utilities.o
 obj-y += efi_dt_fixup.o
+obj-y += efi_fdt.o
 obj-y += efi_file.o
 obj-$(CONFIG_EFI_LOADER_HII) += efi_hii.o
 obj-y += efi_image_loader.o
diff --git a/lib/efi_loader/efi_fdt.c b/lib/efi_loader/efi_fdt.c
new file mode 100644
index 000..0edf0c1e2fc
--- /dev/null
+++ b/lib/efi_loader/efi_fdt.c
@@ -0,0 +1,73 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Bootmethod for distro boot via EFI
+ *
+ * Copyright 2021 Google LLC
+ * Written by Simon Glass 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/**
+ * 

[PATCH v2 4/8] efi_loader: load device-tree specified in boot option

2024-05-28 Thread Heinrich Schuchardt
We allow to specify the triple of binary, initrd, and device-tree in boot
options.

Add the code to actually load the specified device-tree.

Signed-off-by: Heinrich Schuchardt 
---
v2:
Put all related changes into one patch.
Do not boot if device-tree cannot be loaded.
---
 lib/efi_loader/efi_bootmgr.c | 70 
 1 file changed, 70 insertions(+)

diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
index b0bf21cf841..5ac116a7edf 100644
--- a/lib/efi_loader/efi_bootmgr.c
+++ b/lib/efi_loader/efi_bootmgr.c
@@ -1185,6 +1185,59 @@ out:
return ret;
 }
 
+/**
+ * load_fdt_from_load_option - load device-tree from load option
+ *
+ * fdt:pointer to loaded device-tree or NULL
+ * Return: status code
+ */
+static efi_status_t load_fdt_from_load_option(void **fdt)
+{
+   struct efi_device_path *dp = NULL;
+   struct efi_file_handle *f = NULL;
+   efi_uintn_t filesize;
+   efi_status_t ret;
+
+   *fdt = NULL;
+
+   dp = efi_get_dp_from_boot(_guid_fdt);
+   if (!dp)
+   return EFI_SUCCESS;
+
+   /* Open file */
+   f = efi_file_from_path(dp);
+   if (!f) {
+   log_err("Can't find %pD specified in Boot\n", dp);
+   ret = EFI_NOT_FOUND;
+   goto out;
+   }
+
+   /* Get file size */
+   ret = efi_file_size(f, );
+   if (ret != EFI_SUCCESS)
+   goto out;
+
+   *fdt = malloc(filesize);
+   if (!*fdt) {
+   log_err("Out of memory\n");
+   ret = EFI_OUT_OF_RESOURCES;
+   goto out;
+   }
+   ret = EFI_CALL(f->read(f, , *fdt));
+   if (ret != EFI_SUCCESS) {
+   log_err("Can't read fdt\n");
+   free(*fdt);
+   *fdt = NULL;
+   }
+
+out:
+   efi_free_pool(dp);
+   if (f)
+   EFI_CALL(f->close(f));
+
+   return ret;
+}
+
 /**
  * efi_bootmgr_run() - execute EFI boot manager
  * @fdt:   Flat device tree
@@ -1200,6 +1253,7 @@ efi_status_t efi_bootmgr_run(void *fdt)
efi_handle_t handle;
void *load_options;
efi_status_t ret;
+   void *fdt_lo;
 
/* Initialize EFI drivers */
ret = efi_init_obj_list();
@@ -1215,7 +1269,23 @@ efi_status_t efi_bootmgr_run(void *fdt)
return ret;
}
 
+   if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
+   ret = load_fdt_from_load_option(_lo);
+   if (ret != EFI_SUCCESS)
+   return ret;
+   if (fdt_lo)
+   fdt = fdt_lo;
+   }
+
+   /*
+* Needed in ACPI case to create reservations based on
+* control device-tree.
+*/
ret = efi_install_fdt(fdt);
+
+   if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
+   free(fdt_lo);
+
if (ret != EFI_SUCCESS) {
if (EFI_CALL(efi_unload_image(handle)) == EFI_SUCCESS)
free(load_options);
-- 
2.43.0



[PATCH v2 3/8] cmd: efidebug: add support for setting fdt

2024-05-28 Thread Heinrich Schuchardt
We already support creating a load option where the device-path
field contains the concatenation of the binary device-path and
optionally the device path of the initrd which we expose via the
EFI_LOAD_FILE2_PROTOCOL.

Allow to append another device-path pointing to the device-tree
identified by the device-tree GUID.

Signed-off-by: Heinrich Schuchardt 
---
v2:
Rename create_initrd_dp() to create_lo_dp_part().
Use enum as parameter for create_lo_dp_part().
Use function efi_load_option_dp_join() to avoid code duplication.
---
 cmd/efidebug.c | 130 +++--
 1 file changed, 93 insertions(+), 37 deletions(-)

diff --git a/cmd/efidebug.c b/cmd/efidebug.c
index 762027daf8a..966b4b83361 100644
--- a/cmd/efidebug.c
+++ b/cmd/efidebug.c
@@ -654,38 +654,80 @@ static int do_efi_show_tables(struct cmd_tbl *cmdtp, int 
flag,
 }
 
 /**
- * create_initrd_dp() - create a special device for our Boot### option
+ * enum efi_lo_dp_part - part of device path in load option
+ */
+enum efi_lo_dp_part {
+   /** @EFI_LO_DP_PART_BINARY: binary */
+   EFI_LO_DP_PART_BINARY,
+   /** @EFI_LO_DP_PART_INITRD: initial RAM disk */
+   EFI_LO_DP_PART_INITRD,
+   /** EFI_LP_DP_PART_FDT: device-tree */
+   EFI_LP_DP_PART_FDT,
+};
+
+/**
+ * create_lo_dp() - create a special device path for our Boot### option
  *
  * @dev:   device
  * @part:  disk partition
  * @file:  filename
  * @shortform: create short form device path
+ * @type:  part of device path to be created
  * Return: pointer to the device path or ERR_PTR
  */
 static
-struct efi_device_path *create_initrd_dp(const char *dev, const char *part,
-const char *file, int shortform)
+struct efi_device_path *create_lo_dp_part(const char *dev, const char *part,
+ const char *file, bool shortform,
+ enum efi_lo_dp_part type)
 
 {
struct efi_device_path *tmp_dp = NULL, *tmp_fp = NULL, *short_fp = NULL;
-   struct efi_device_path *initrd_dp = NULL;
+   struct efi_device_path *dp = NULL;
+   const struct efi_device_path *dp_prefix;
efi_status_t ret;
-   const struct efi_initrd_dp id_dp = {
+   const struct efi_initrd_dp fdt_dp = {
+   .vendor = {
+   {
+   DEVICE_PATH_TYPE_MEDIA_DEVICE,
+   DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
+   sizeof(fdt_dp.vendor),
+   },
+   EFI_FDT_GUID,
+   },
+   .end = {
+   DEVICE_PATH_TYPE_END,
+   DEVICE_PATH_SUB_TYPE_END,
+   sizeof(fdt_dp.end),
+   }
+   };
+   const struct efi_initrd_dp initrd_dp = {
.vendor = {
{
DEVICE_PATH_TYPE_MEDIA_DEVICE,
DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
-   sizeof(id_dp.vendor),
+   sizeof(initrd_dp.vendor),
},
EFI_INITRD_MEDIA_GUID,
},
.end = {
DEVICE_PATH_TYPE_END,
DEVICE_PATH_SUB_TYPE_END,
-   sizeof(id_dp.end),
+   sizeof(initrd_dp.end),
}
};
 
+   switch (type) {
+   case EFI_LO_DP_PART_INITRD:
+   dp_prefix = _dp.vendor.dp;
+   break;
+   case EFI_LP_DP_PART_FDT:
+   dp_prefix = _dp.vendor.dp;
+   break;
+   default:
+   dp_prefix = NULL;
+   break;
+   }
+
ret = efi_dp_from_name(dev, part, file, _dp, _fp);
if (ret != EFI_SUCCESS) {
printf("Cannot create device path for \"%s %s\"\n", part, file);
@@ -696,13 +739,12 @@ struct efi_device_path *create_initrd_dp(const char *dev, 
const char *part,
if (!short_fp)
short_fp = tmp_fp;
 
-   initrd_dp = efi_dp_concat((const struct efi_device_path *)_dp,
- short_fp, 0);
+   dp = efi_dp_concat(dp_prefix, short_fp, 0);
 
 out:
efi_free_pool(tmp_dp);
efi_free_pool(tmp_fp);
-   return initrd_dp;
+   return dp;
 }
 
 /**
@@ -793,9 +835,8 @@ static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
efi_guid_t guid;
u16 *label;
struct efi_device_path *file_path = NULL;
-   struct efi_device_path *fp_free = NULL;
-   struct efi_device_path *final_fp = NULL;
struct efi_device_path *initrd_dp = NULL;
+   struct efi_device_path *fdt_dp = NULL;
struct efi_load_option lo;
void *data = NULL;
efi_uintn_t size;
@@ -843,22 +884,31 @@ static int do_efi_boot_add(struct cmd_tbl *cmdtp, int 
flag,
lo.label 

[PATCH v2 1/8] efi_loader: allow concatenation with contained end node

2024-05-28 Thread Heinrich Schuchardt
Allow appending a device-path to a device-path that contains an end node
as separator. We need this feature for creating boot options specifying
kernel, initrd, and dtb.

Signed-off-by: Heinrich Schuchardt 
---
v2:
update efi_dp_concat() instead of new function efi_dp_merge()
---
 cmd/eficonfig.c|  6 +++---
 cmd/efidebug.c |  4 ++--
 include/efi_loader.h   |  2 +-
 lib/efi_loader/efi_bootbin.c   |  2 +-
 lib/efi_loader/efi_bootmgr.c   |  2 +-
 lib/efi_loader/efi_boottime.c  |  2 +-
 lib/efi_loader/efi_device_path.c   | 20 +---
 lib/efi_loader/efi_device_path_utilities.c |  2 +-
 8 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/cmd/eficonfig.c b/cmd/eficonfig.c
index 0ba92c60e03..b13d9a3d2d9 100644
--- a/cmd/eficonfig.c
+++ b/cmd/eficonfig.c
@@ -531,7 +531,7 @@ struct efi_device_path *eficonfig_create_device_path(struct 
efi_device_path *dp_
dp = efi_dp_shorten(dp_volume);
if (!dp)
dp = dp_volume;
-   dp = efi_dp_concat(dp, >dp, false);
+   dp = efi_dp_concat(dp, >dp, 0);
free(buf);
 
return dp;
@@ -1485,7 +1485,7 @@ static efi_status_t eficonfig_edit_boot_option(u16 
*varname, struct eficonfig_bo
goto out;
}
initrd_dp = efi_dp_concat((const struct efi_device_path 
*)_dp,
- dp, false);
+ dp, 0);
efi_free_pool(dp);
}
 
@@ -1496,7 +1496,7 @@ static efi_status_t eficonfig_edit_boot_option(u16 
*varname, struct eficonfig_bo
}
final_dp_size = efi_dp_size(dp) + sizeof(END);
if (initrd_dp) {
-   final_dp = efi_dp_concat(dp, initrd_dp, true);
+   final_dp = efi_dp_concat(dp, initrd_dp, 1);
final_dp_size += efi_dp_size(initrd_dp) + sizeof(END);
} else {
final_dp = efi_dp_dup(dp);
diff --git a/cmd/efidebug.c b/cmd/efidebug.c
index c2c525f2351..762027daf8a 100644
--- a/cmd/efidebug.c
+++ b/cmd/efidebug.c
@@ -697,7 +697,7 @@ struct efi_device_path *create_initrd_dp(const char *dev, 
const char *part,
short_fp = tmp_fp;
 
initrd_dp = efi_dp_concat((const struct efi_device_path *)_dp,
- short_fp, false);
+ short_fp, 0);
 
 out:
efi_free_pool(tmp_dp);
@@ -917,7 +917,7 @@ static int do_efi_boot_add(struct cmd_tbl *cmdtp, int flag,
goto out;
}
 
-   final_fp = efi_dp_concat(file_path, initrd_dp, true);
+   final_fp = efi_dp_concat(file_path, initrd_dp, 1);
if (!final_fp) {
printf("Cannot create final device path\n");
r = CMD_RET_FAILURE;
diff --git a/include/efi_loader.h b/include/efi_loader.h
index 9600941aa32..ddf2e41a95c 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -946,7 +946,7 @@ struct efi_device_path *efi_dp_from_lo(struct 
efi_load_option *lo,
   const efi_guid_t *guid);
 struct efi_device_path *efi_dp_concat(const struct efi_device_path *dp1,
  const struct efi_device_path *dp2,
- bool split_end_node);
+ size_t split_end_node);
 struct efi_device_path *search_gpt_dp_node(struct efi_device_path 
*device_path);
 efi_status_t efi_deserialize_load_option(struct efi_load_option *lo, u8 *data,
 efi_uintn_t *size);
diff --git a/lib/efi_loader/efi_bootbin.c b/lib/efi_loader/efi_bootbin.c
index b7910f78fb6..a87006b3c0e 100644
--- a/lib/efi_loader/efi_bootbin.c
+++ b/lib/efi_loader/efi_bootbin.c
@@ -150,7 +150,7 @@ efi_status_t efi_run_image(void *source_buffer, efi_uintn_t 
source_size)
msg_path = file_path;
} else {
file_path = efi_dp_concat(bootefi_device_path,
- bootefi_image_path, false);
+ bootefi_image_path, 0);
msg_path = bootefi_image_path;
log_debug("Loaded from disk\n");
}
diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
index 7da3139f917..b0bf21cf841 100644
--- a/lib/efi_loader/efi_bootmgr.c
+++ b/lib/efi_loader/efi_bootmgr.c
@@ -130,7 +130,7 @@ static efi_status_t try_load_from_file_path(efi_handle_t 
*fs_handles,
if (!dp)
continue;
 
-   dp = efi_dp_concat(dp, fp, false);
+   dp = efi_dp_concat(dp, fp, 0);
if (!dp)
continue;
 
diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c
index 1951291747c..630c5f52c4f 100644
--- a/lib/efi_loader/efi_boottime.c
+++ b/lib/efi_loader/efi_boottime.c

[PATCH v2 2/8] cmd: eficonfig: add support for setting fdt

2024-05-28 Thread Heinrich Schuchardt
We already support creating a load option where the device-path
field contains the concatenation of the binary device-path and
optionally the device path of the initrd which we expose via the
EFI_LOAD_FILE2_PROTOCOL.

Allow to append another device-path pointing to the device-tree
identified by the device-tree GUID.

Signed-off-by: Heinrich Schuchardt 
---
v2:
Carve out a function efi_load_option_dp_join() which we can
use both for the eficonfig and the efidebug command.
Rename variables id_dp, final_dp_size.
---
 cmd/eficonfig.c | 79 -
 include/efi_loader.h| 14 +++
 lib/efi_loader/efi_helper.c | 44 +
 3 files changed, 119 insertions(+), 18 deletions(-)

diff --git a/cmd/eficonfig.c b/cmd/eficonfig.c
index b13d9a3d2d9..a12df1e7d59 100644
--- a/cmd/eficonfig.c
+++ b/cmd/eficonfig.c
@@ -62,6 +62,7 @@ struct eficonfig_filepath_info {
 struct eficonfig_boot_option {
struct eficonfig_select_file_info file_info;
struct eficonfig_select_file_info initrd_info;
+   struct eficonfig_select_file_info fdt_info;
unsigned int boot_index;
u16 *description;
u16 *optional_data;
@@ -1308,6 +1309,10 @@ static efi_status_t eficonfig_show_boot_option(struct 
eficonfig_boot_option *bo,
if (ret != EFI_SUCCESS)
goto out;
 
+   ret = prepare_file_selection_entry(efi_menu, "Fdt File: ", 
>fdt_info);
+   if (ret != EFI_SUCCESS)
+   goto out;
+
ret = create_boot_option_entry(efi_menu, "Optional Data: ", 
bo->optional_data,
   eficonfig_boot_add_optional_data, bo);
if (ret != EFI_SUCCESS)
@@ -1388,27 +1393,44 @@ static efi_status_t eficonfig_edit_boot_option(u16 
*varname, struct eficonfig_bo
efi_status_t ret;
char *tmp = NULL, *p;
struct efi_load_option lo = {0};
-   efi_uintn_t final_dp_size;
+   efi_uintn_t dp_size;
struct efi_device_path *dp = NULL;
efi_uintn_t size = load_option_size;
-   struct efi_device_path *final_dp = NULL;
struct efi_device_path *device_dp = NULL;
struct efi_device_path *initrd_dp = NULL;
+   struct efi_device_path *fdt_dp = NULL;
struct efi_device_path *initrd_device_dp = NULL;
+   struct efi_device_path *fdt_device_dp = NULL;
 
-   const struct efi_initrd_dp id_dp = {
+   const struct efi_initrd_dp initrd_prefix = {
.vendor = {
{
DEVICE_PATH_TYPE_MEDIA_DEVICE,
DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
-   sizeof(id_dp.vendor),
+   sizeof(initrd_prefix.vendor),
},
EFI_INITRD_MEDIA_GUID,
},
.end = {
DEVICE_PATH_TYPE_END,
DEVICE_PATH_SUB_TYPE_END,
-   sizeof(id_dp.end),
+   sizeof(initrd_prefix.end),
+   }
+   };
+
+   const struct efi_initrd_dp fdt_prefix = {
+   .vendor = {
+   {
+   DEVICE_PATH_TYPE_MEDIA_DEVICE,
+   DEVICE_PATH_SUB_TYPE_VENDOR_PATH,
+   sizeof(fdt_prefix.vendor),
+   },
+   EFI_FDT_GUID,
+   },
+   .end = {
+   DEVICE_PATH_TYPE_END,
+   DEVICE_PATH_SUB_TYPE_END,
+   sizeof(initrd_prefix.end),
}
};
 
@@ -1424,6 +1446,12 @@ static efi_status_t eficonfig_edit_boot_option(u16 
*varname, struct eficonfig_bo
goto out;
}
 
+   bo->fdt_info.current_path = calloc(1, EFICONFIG_FILE_PATH_BUF_SIZE);
+   if (!bo->fdt_info.current_path) {
+   ret =  EFI_OUT_OF_RESOURCES;
+   goto out;
+   }
+
bo->description = calloc(1, EFICONFIG_DESCRIPTION_MAX * sizeof(u16));
if (!bo->description) {
ret =  EFI_OUT_OF_RESOURCES;
@@ -1456,13 +1484,20 @@ static efi_status_t eficonfig_edit_boot_option(u16 
*varname, struct eficonfig_bo
if (lo.file_path)
fill_file_info(lo.file_path, >file_info, device_dp);
 
-   /* Initrd file path(optional) is placed at second instance. */
+   /* Initrd file path (optional) is placed at second instance. */
initrd_dp = efi_dp_from_lo(, _lf2_initrd_guid);
if (initrd_dp) {
fill_file_info(initrd_dp, >initrd_info, 
initrd_device_dp);
efi_free_pool(initrd_dp);
}
 
+   /* Fdt file path (optional) is placed as third instance. */
+   fdt_dp = efi_dp_from_lo(, _guid_fdt);
+   if (fdt_dp) {
+   fill_file_info(fdt_dp, >fdt_info, 

[PATCH v2 8/8] efi_loader: load distro dtb in bootmgr

2024-05-28 Thread Heinrich Schuchardt
If no device-tree is specified, try to load a device-tree from the boot
device use the $fdtfile concatenated to either of the paths '/dtb/', '/',
'/dtb/current/'.

Signed-off-by: Heinrich Schuchardt 
---
v2:
no change
---
 include/efi_loader.h |  2 ++
 lib/efi_loader/efi_bootmgr.c | 13 +--
 lib/efi_loader/efi_fdt.c | 44 
 3 files changed, 57 insertions(+), 2 deletions(-)

diff --git a/include/efi_loader.h b/include/efi_loader.h
index ab7bed22971..6c993e1a694 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -1205,4 +1205,6 @@ efi_status_t efi_load_option_dp_join(struct 
efi_device_path **dp,
 
 int efi_get_distro_fdt_name(char *fname, int size, int seq);
 
+void efi_load_distro_fdt(void **fdt, efi_uintn_t *fdt_size);
+
 #endif /* _EFI_LOADER_H */
diff --git a/lib/efi_loader/efi_bootmgr.c b/lib/efi_loader/efi_bootmgr.c
index b08d6e97ea3..68440542a37 100644
--- a/lib/efi_loader/efi_bootmgr.c
+++ b/lib/efi_loader/efi_bootmgr.c
@@ -1249,7 +1249,8 @@ efi_status_t efi_bootmgr_run(void *fdt)
efi_handle_t handle;
void *load_options;
efi_status_t ret;
-   void *fdt_lo;
+   void *fdt_lo, *fdt_distro = NULL;
+   efi_uintn_t fdt_size;
 
/* Initialize EFI drivers */
ret = efi_init_obj_list();
@@ -1269,6 +1270,10 @@ efi_status_t efi_bootmgr_run(void *fdt)
return ret;
if (fdt_lo)
fdt = fdt_lo;
+   if (!fdt) {
+   efi_load_distro_fdt(_distro, _size);
+   fdt = fdt_distro;
+   }
}
 
/*
@@ -1277,8 +1282,12 @@ efi_status_t efi_bootmgr_run(void *fdt)
 */
ret = efi_install_fdt(fdt);
 
-   if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
+   if (!IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE)) {
free(fdt_lo);
+   if (fdt_distro)
+   efi_free_pages((uintptr_t)fdt_distro,
+  efi_size_in_pages(fdt_size));
+   }
 
if (ret != EFI_SUCCESS) {
if (EFI_CALL(efi_unload_image(handle)) == EFI_SUCCESS)
diff --git a/lib/efi_loader/efi_fdt.c b/lib/efi_loader/efi_fdt.c
index 0edf0c1e2fc..86ba00c2bdd 100644
--- a/lib/efi_loader/efi_fdt.c
+++ b/lib/efi_loader/efi_fdt.c
@@ -71,3 +71,47 @@ int efi_get_distro_fdt_name(char *fname, int size, int seq)
 
return 0;
 }
+
+/**
+ * efi_load_distro_fdt() - load distro device-tree
+ *
+ * @fdt:   on return device-tree, must be freed via efi_free_pages()
+ * @fdt_size:  buffer size
+ */
+void efi_load_distro_fdt(void **fdt, efi_uintn_t *fdt_size)
+{
+   struct efi_device_path *rem, *dp;
+   efi_status_t  ret;
+   efi_handle_t device;
+
+   *fdt = NULL;
+
+   dp = efi_get_dp_from_boot(NULL);
+   if (!dp)
+   return;
+   device = efi_dp_find_obj(dp, NULL, );
+   ret = efi_search_protocol(device, _simple_file_system_protocol_guid,
+ NULL);
+   if (ret != EFI_SUCCESS)
+   goto err;
+   memcpy(rem, , sizeof(END));
+
+   /* try the various available names */
+   for (int seq = 0; ; ++seq) {
+   struct efi_device_path *file;
+   char buf[255];
+
+   if (efi_get_distro_fdt_name(buf, sizeof(buf), seq))
+   break;
+   file = efi_dp_from_file(dp, buf);
+   if (!file)
+   break;
+   ret = efi_load_image_from_path(true, file, fdt, fdt_size);
+   efi_free_pool(file);
+   if (ret == EFI_SUCCESS)
+   break;
+   }
+
+err:
+   efi_free_pool(dp);
+}
-- 
2.43.0



[PATCH v2 7/8] efi_loader: export efi_load_image_from_path

2024-05-28 Thread Heinrich Schuchardt
We can reuse this function to load the device-tree.

Signed-off-by: Heinrich Schuchardt 
---
v2:
Move unrelated changes to different patch.
---
 include/efi_loader.h  | 4 
 lib/efi_loader/efi_boottime.c | 1 -
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/include/efi_loader.h b/include/efi_loader.h
index 1b4bc987a23..ab7bed22971 100644
--- a/include/efi_loader.h
+++ b/include/efi_loader.h
@@ -664,6 +664,10 @@ efi_status_t EFIAPI efi_load_image(bool boot_policy,
   void *source_buffer,
   efi_uintn_t source_size,
   efi_handle_t *image_handle);
+/* Load image from path */
+efi_status_t efi_load_image_from_path(bool boot_policy,
+ struct efi_device_path *file_path,
+ void **buffer, efi_uintn_t *size);
 /* Start image */
 efi_status_t EFIAPI efi_start_image(efi_handle_t image_handle,
efi_uintn_t *exit_data_size,
diff --git a/lib/efi_loader/efi_boottime.c b/lib/efi_loader/efi_boottime.c
index 630c5f52c4f..eedc5f39549 100644
--- a/lib/efi_loader/efi_boottime.c
+++ b/lib/efi_loader/efi_boottime.c
@@ -1996,7 +1996,6 @@ error:
  * @size:  size of the loaded image
  * Return: status code
  */
-static
 efi_status_t efi_load_image_from_path(bool boot_policy,
  struct efi_device_path *file_path,
  void **buffer, efi_uintn_t *size)
-- 
2.43.0



[PATCH v2 6/8] efi_loader: return binary from efi_dp_from_lo()

2024-05-28 Thread Heinrich Schuchardt
For finding distro supplied device-trees we need to know from which device
we are booting. This can be identified via the device-path of the binary.

Up to now efi_dp_from_lo() only could return the initrd or fdt device-path.
Allow returning the binary device-path, too.

Signed-off-by: Heinrich Schuchardt 
---
v2:
no change
---
 lib/efi_loader/efi_device_path.c | 20 
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/lib/efi_loader/efi_device_path.c b/lib/efi_loader/efi_device_path.c
index c8c8d54f733..52e3313c23a 100644
--- a/lib/efi_loader/efi_device_path.c
+++ b/lib/efi_loader/efi_device_path.c
@@ -1133,17 +1133,18 @@ ssize_t efi_dp_check_length(const struct 
efi_device_path *dp,
 }
 
 /**
- * efi_dp_from_lo() - Get the instance of a VenMedia node in a
- *multi-instance device path that matches
- *a specific GUID. This kind of device paths
- *is found in Boot options describing an
- *initrd location
+ * efi_dp_from_lo() - get device-path from load option
  *
- * @lo:EFI_LOAD_OPTION containing a valid device path
- * @guid:  guid to search for
+ * The load options in U-Boot may contain multiple concatenated device-paths.
+ * The first device-path indicates the EFI binary to execute. Subsequent
+ * device-paths start with a VenMedia node where the GUID identifies the
+ * function (initrd or fdt).
+ *
+ * @lo:EFI load option containing a valid device path
+ * @guid:  GUID identifying device-path or NULL for the EFI binary
  *
  * Return:
- * device path including the VenMedia node or NULL.
+ * device path excluding the matched VenMedia node or NULL.
  * Caller must free the returned value.
  */
 struct
@@ -1154,6 +1155,9 @@ efi_device_path *efi_dp_from_lo(struct efi_load_option 
*lo,
struct efi_device_path_vendor *vendor;
int lo_len = lo->file_path_length;
 
+   if (!guid)
+   return efi_dp_dup(fp);
+
for (; lo_len >=  sizeof(struct efi_device_path);
 lo_len -= fp->length, fp = (void *)fp + fp->length) {
if (lo_len < 0 || efi_dp_check_length(fp, lo_len) < 0)
-- 
2.43.0



[PATCH v2 0/8] efi_loader: improve device-tree loading

2024-05-28 Thread Heinrich Schuchardt
In U-Boot EFI boot options can already specify both an EFI binary and
an initrd. With this series we can additionally define the matching
device-tree to be loaded in the boot option.

With the last patch the boot manager will fall back the device-tree
specified by $fdtfile in directories '/dtb/', '/', or '/dtb/current/'
on the boot device if no device-tree is specified in the boot
option or via a bootefi command parameter.

v2:
Update efi_dp_concat() instead of new function efi_dp_merge().
Carve out a function efi_load_option_dp_join() which we can
use both for the eficonfig and the efidebug command.
Rename variables id_dp, final_dp_size.
Rename create_initrd_dp() to create_lo_dp_part().
Use enum as parameter for create_lo_dp_part().
Put all related changes into one patch.

Heinrich Schuchardt (8):
  efi_loader: allow concatenation with contained end node
  cmd: eficonfig: add support for setting fdt
  cmd: efidebug: add support for setting fdt
  efi_loader: load device-tree specified in boot option
  efi_loader: move distro_efi_get_fdt_name()
  efi_loader: return binary from efi_dp_from_lo()
  efi_loader: export efi_load_image_from_path
  efi_loader: load distro dtb in bootmgr

 boot/bootmeth_efi.c|  60 +-
 cmd/eficonfig.c|  83 +
 cmd/efidebug.c | 130 +++--
 include/efi_loader.h   |  24 +++-
 lib/efi_loader/Makefile|   1 +
 lib/efi_loader/efi_bootbin.c   |   2 +-
 lib/efi_loader/efi_bootmgr.c   |  75 +++-
 lib/efi_loader/efi_boottime.c  |   3 +-
 lib/efi_loader/efi_device_path.c   |  40 ---
 lib/efi_loader/efi_device_path_utilities.c |   2 +-
 lib/efi_loader/efi_fdt.c   | 117 +++
 lib/efi_loader/efi_helper.c|  44 +++
 12 files changed, 445 insertions(+), 136 deletions(-)
 create mode 100644 lib/efi_loader/efi_fdt.c

-- 
2.43.0



[PATCH] xilinx: zynqmp: Allow multiboot environment write even in saved environment

2024-05-28 Thread Kory Maincent
Once the environment was saved, we could not retrieve information about
the multiboot image used. When dealing with firmware updates, this
information is necessary alongside the saved environment.

Move the multiboot environment set operation before the saved environment
check to ensure this information is always available.

Signed-off-by: Kory Maincent 
---
 board/xilinx/zynqmp/zynqmp.c | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/board/xilinx/zynqmp/zynqmp.c b/board/xilinx/zynqmp/zynqmp.c
index f370fb7347a..16292ed1c7e 100644
--- a/board/xilinx/zynqmp/zynqmp.c
+++ b/board/xilinx/zynqmp/zynqmp.c
@@ -519,6 +519,10 @@ int board_late_init(void)
usb_ether_init();
 #endif
 
+   multiboot = multi_boot();
+   if (multiboot >= 0)
+   env_set_hex("multiboot", multiboot);
+
if (!(gd->flags & GD_FLG_ENV_DEFAULT)) {
debug("Saved variables - Skipping\n");
return 0;
@@ -531,10 +535,6 @@ int board_late_init(void)
if (ret)
return ret;
 
-   multiboot = multi_boot();
-   if (multiboot >= 0)
-   env_set_hex("multiboot", multiboot);
-
if (IS_ENABLED(CONFIG_DISTRO_DEFAULTS)) {
ret = boot_targets_setup();
if (ret)
-- 
2.34.1



[PATCH v3 25/25] configs: enable MbedTLS as default setting

2024-05-28 Thread Raymond Mao
Enable MbedTLS as default setting for qemu arm64

Signed-off-by: Raymond Mao 
---
Changes in v2
- None.
Changes in v3
- None.

 configs/qemu_arm64_defconfig | 5 +
 configs/sandbox_defconfig| 4 
 2 files changed, 9 insertions(+)

diff --git a/configs/qemu_arm64_defconfig b/configs/qemu_arm64_defconfig
index 7e166f43908..587a3fb9123 100644
--- a/configs/qemu_arm64_defconfig
+++ b/configs/qemu_arm64_defconfig
@@ -67,4 +67,9 @@ CONFIG_TPM2_MMIO=y
 CONFIG_USB_EHCI_HCD=y
 CONFIG_USB_EHCI_PCI=y
 CONFIG_SEMIHOSTING=y
+CONFIG_MBEDTLS_LIB=y
+CONFIG_MBEDTLS_LIB_CRYPTO=y
+CONFIG_MBEDTLS_LIB_X509=y
+# CONFIG_MBEDTLS_LIB_TLS is not set
 CONFIG_TPM=y
+CONFIG_EFI_SECURE_BOOT=y
diff --git a/configs/sandbox_defconfig b/configs/sandbox_defconfig
index 93b52f2de5c..6f36fa0ac86 100644
--- a/configs/sandbox_defconfig
+++ b/configs/sandbox_defconfig
@@ -343,6 +343,10 @@ CONFIG_FS_CBFS=y
 CONFIG_FS_CRAMFS=y
 CONFIG_ADDR_MAP=y
 CONFIG_CMD_DHRYSTONE=y
+CONFIG_MBEDTLS_LIB=y
+CONFIG_MBEDTLS_LIB_CRYPTO=y
+CONFIG_MBEDTLS_LIB_X509=y
+# CONFIG_MBEDTLS_LIB_TLS is not set
 CONFIG_ECDSA=y
 CONFIG_ECDSA_VERIFY=y
 CONFIG_TPM=y
-- 
2.25.1



[PATCH v3 24/25] test: Remove ASN1 library test

2024-05-28 Thread Raymond Mao
With MBEDTLS_LIB_X509 enabled, we don't build the original ASN1 lib,
So remove it from test.

Signed-off-by: Raymond Mao 
---
Changes in v2
- Initial patch.
Changes in v3
- None.

 test/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/test/Kconfig b/test/Kconfig
index e2ec0994a2e..558a9cd49b4 100644
--- a/test/Kconfig
+++ b/test/Kconfig
@@ -32,7 +32,7 @@ if UT_LIB
 
 config UT_LIB_ASN1
bool "Unit test for asn1 compiler and decoder function"
-   depends on SANDBOX
+   depends on SANDBOX && !MBEDTLS_LIB_X509
default y
imply ASYMMETRIC_KEY_TYPE
imply ASYMMETRIC_PUBLIC_KEY_SUBTYPE
-- 
2.25.1



[PATCH v3 23/25] asn1_decoder: remove ASN1 decoder when using MbedTLS

2024-05-28 Thread Raymond Mao
When building with MbedTLS, we are using MbedTLS to decode ASN1 data
for x509, pkcs7 and mscode. So we can remove asn1_decoder when
MBEDTLS_LIB_X509 is enabled.

Signed-off-by: Raymond Mao 
Reviewed-by: Ilias Apalodimas 
---
Changes in v2
- Initial patch.
Changes in v3
- None.

 lib/Makefile | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lib/Makefile b/lib/Makefile
index 3534b3301ae..7e3dc1084fb 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -82,7 +82,9 @@ obj-$(CONFIG_$(SPL_)SHA512) += sha512.o
 endif
 
 obj-$(CONFIG_CRYPT_PW) += crypt/
+ifneq ($(CONFIG_MBEDTLS_LIB_X509), y)
 obj-$(CONFIG_$(SPL_)ASN1_DECODER) += asn1_decoder.o
+endif
 
 obj-$(CONFIG_$(SPL_)ZLIB) += zlib/
 obj-$(CONFIG_$(SPL_)ZSTD) += zstd/
-- 
2.25.1



[PATCH v3 22/25] lib/rypto: Adapt rsa_helper to MbedTLS

2024-05-28 Thread Raymond Mao
Previous patch has introduced MbedTLS porting layer for RSA helper,
here to adjust the makefile accordingly.

Signed-off-by: Raymond Mao 
---
Changes in v2
- Initial patch.
Changes in v3
- Update commit message.

 lib/crypto/Makefile | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/lib/crypto/Makefile b/lib/crypto/Makefile
index e3232019df2..866a9a3f059 100644
--- a/lib/crypto/Makefile
+++ b/lib/crypto/Makefile
@@ -11,6 +11,7 @@ ifneq ($(CONFIG_MBEDTLS_LIB_X509), y)
 obj-$(CONFIG_$(SPL_)ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o
 endif
 
+ifneq ($(CONFIG_MBEDTLS_LIB_X509), y)
 #
 # RSA public key parser
 #
@@ -28,6 +29,7 @@ $(obj)/rsa_helper.o: $(obj)/rsapubkey.asn1.h
 ifdef CONFIG_SPL_BUILD
 CFLAGS_rsa_helper.o += -I$(obj)
 endif
+endif
 
 ifneq ($(CONFIG_MBEDTLS_LIB_X509), y)
 #
-- 
2.25.1



[PATCH v3 21/25] mbedtls: add RSA helper layer on MbedTLS

2024-05-28 Thread Raymond Mao
Add RSA helper layer on top on MbedTLS PK and RSA library.

Signed-off-by: Raymond Mao 
---
Changes in v2
- Initial patch.
Changes in v3
- None.

 lib/mbedtls/Makefile |  1 +
 lib/mbedtls/rsa_helper.c | 99 
 2 files changed, 100 insertions(+)
 create mode 100644 lib/mbedtls/rsa_helper.c

diff --git a/lib/mbedtls/Makefile b/lib/mbedtls/Makefile
index f0b8a1c4003..dab110891af 100644
--- a/lib/mbedtls/Makefile
+++ b/lib/mbedtls/Makefile
@@ -27,6 +27,7 @@ x509_mbedtls-$(CONFIG_$(SPL_)ASYMMETRIC_PUBLIC_KEY_SUBTYPE) 
+= public_key.o
 x509_mbedtls-$(CONFIG_$(SPL_)X509_CERTIFICATE_PARSER) += x509_cert_parser.o
 x509_mbedtls-$(CONFIG_$(SPL_)PKCS7_MESSAGE_PARSER) += pkcs7_parser.o
 x509_mbedtls-$(CONFIG_$(SPL_)MSCODE_PARSER) += mscode_parser.o
+x509_mbedtls-$(CONFIG_$(SPL_)RSA_PUBLIC_KEY_PARSER) += rsa_helper.o
 
 obj-$(CONFIG_MBEDTLS_LIB_CRYPTO) += mbedtls_lib_crypto.o
 mbedtls_lib_crypto-y := \
diff --git a/lib/mbedtls/rsa_helper.c b/lib/mbedtls/rsa_helper.c
new file mode 100644
index 000..956e550c856
--- /dev/null
+++ b/lib/mbedtls/rsa_helper.c
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * RSA helper functions using MbedTLS
+ *
+ * Copyright (c) 2024 Linaro Limited
+ * Author: Raymond Mao 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+/**
+ * rsa_parse_pub_key() - decodes the BER encoded buffer and stores in the
+ *   provided struct rsa_key, pointers to the raw key as 
is,
+ *   so that the caller can copy it or MPI parse it, etc.
+ *
+ * @rsa_key:   struct rsa_key key representation
+ * @key:   key in BER format
+ * @key_len:   length of key
+ *
+ * Return: 0 on success or error code in case of error
+ */
+int rsa_parse_pub_key(struct rsa_key *rsa_key, const void *key,
+ unsigned int key_len)
+{
+   int ret = 0;
+   mbedtls_pk_context pk;
+   mbedtls_rsa_context *rsa;
+
+   mbedtls_pk_init();
+
+   ret = mbedtls_pk_parse_public_key(, (const unsigned char *)key,
+ key_len);
+   if (ret) {
+   pr_err("Failed to parse public key, ret:-0x%04x\n",
+  (unsigned int)-ret);
+   ret = -EINVAL;
+   goto clean_pubkey;
+   }
+
+   /* Ensure that it is a RSA key */
+   if (mbedtls_pk_get_type() != MBEDTLS_PK_RSA) {
+   pr_err("Non-RSA keys are not supported\n");
+   ret = -EKEYREJECTED;
+   goto clean_pubkey;
+   }
+
+   /* Get RSA key context */
+   rsa = mbedtls_pk_rsa(pk);
+   if (!rsa) {
+   pr_err("Failed to get RSA key context, ret:-0x%04x\n",
+  (unsigned int)-ret);
+   ret = -EINVAL;
+   goto clean_pubkey;
+   }
+
+   /* Parse modulus (n) */
+   rsa_key->n_sz = mbedtls_mpi_size(>N);
+   rsa_key->n = kzalloc(rsa_key->n_sz, GFP_KERNEL);
+   if (!rsa_key->n) {
+   ret = -ENOMEM;
+   goto clean_pubkey;
+   }
+   ret = mbedtls_mpi_write_binary(>N, (unsigned char *)rsa_key->n,
+  rsa_key->n_sz);
+   if (ret) {
+   pr_err("Failed to parse modulus (n), ret:-0x%04x\n",
+  (unsigned int)-ret);
+   ret = -EINVAL;
+   goto clean_modulus;
+   }
+
+   /* Parse public exponent (e) */
+   rsa_key->e_sz = mbedtls_mpi_size(>E);
+   rsa_key->e = kzalloc(rsa_key->e_sz, GFP_KERNEL);
+   if (!rsa_key->e) {
+   ret = -ENOMEM;
+   goto clean_modulus;
+   }
+   ret = mbedtls_mpi_write_binary(>E, (unsigned char *)rsa_key->e,
+  rsa_key->e_sz);
+   if (!ret)
+   return 0;
+
+   pr_err("Failed to parse public exponent (e), ret:-0x%04x\n",
+  (unsigned int)-ret);
+   ret = -EINVAL;
+
+   kfree(rsa_key->e);
+clean_modulus:
+   kfree(rsa_key->n);
+clean_pubkey:
+   mbedtls_pk_free();
+   return ret;
+}
-- 
2.25.1



[PATCH v3 20/25] lib/crypto: Adapt mscode_parser to MbedTLS

2024-05-28 Thread Raymond Mao
Previous patch has introduced MbedTLS porting layer for mscode parser,
here to adjust the header and makefiles accordingly.

Signed-off-by: Raymond Mao 
---
Changes in v2
- Move the porting layer to MbedTLS dir.
Changes in v3
- Update commit message.

 include/crypto/mscode.h | 4 
 lib/crypto/Makefile | 2 ++
 2 files changed, 6 insertions(+)

diff --git a/include/crypto/mscode.h b/include/crypto/mscode.h
index 551058b96e6..c214fc87e40 100644
--- a/include/crypto/mscode.h
+++ b/include/crypto/mscode.h
@@ -9,6 +9,10 @@
 #ifndef __UBOOT__
 #include 
 #endif
+#if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
+#include 
+#include 
+#endif
 
 struct pefile_context {
 #ifndef __UBOOT__
diff --git a/lib/crypto/Makefile b/lib/crypto/Makefile
index c3fe9c9d2c0..e3232019df2 100644
--- a/lib/crypto/Makefile
+++ b/lib/crypto/Makefile
@@ -62,6 +62,7 @@ $(obj)/pkcs7.asn1.o: $(obj)/pkcs7.asn1.c $(obj)/pkcs7.asn1.h
 endif
 obj-$(CONFIG_$(SPL_)PKCS7_VERIFY) += pkcs7_verify.o
 
+ifneq ($(CONFIG_MBEDTLS_LIB_X509), y)
 #
 # Signed PE binary-wrapped key handling
 #
@@ -73,3 +74,4 @@ mscode-y := \
 
 $(obj)/mscode_parser.o: $(obj)/mscode.asn1.h $(obj)/mscode.asn1.h
 $(obj)/mscode.asn1.o: $(obj)/mscode.asn1.c $(obj)/mscode.asn1.h
+endif
-- 
2.25.1



[PATCH v3 19/25] mbedtls: add MSCode parser porting layer

2024-05-28 Thread Raymond Mao
Add porting layer for MSCode on top of MbedTLS ASN1 library.

Signed-off-by: Raymond Mao 
---
Changes in v2
- Move the porting layer to MbedTLS dir.
Changes in v3
- None.

 lib/mbedtls/Makefile|   1 +
 lib/mbedtls/mscode_parser.c | 111 
 2 files changed, 112 insertions(+)
 create mode 100644 lib/mbedtls/mscode_parser.c

diff --git a/lib/mbedtls/Makefile b/lib/mbedtls/Makefile
index 005b8a25320..f0b8a1c4003 100644
--- a/lib/mbedtls/Makefile
+++ b/lib/mbedtls/Makefile
@@ -26,6 +26,7 @@ obj-$(CONFIG_MBEDTLS_LIB_X509) += x509_mbedtls.o
 x509_mbedtls-$(CONFIG_$(SPL_)ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o
 x509_mbedtls-$(CONFIG_$(SPL_)X509_CERTIFICATE_PARSER) += x509_cert_parser.o
 x509_mbedtls-$(CONFIG_$(SPL_)PKCS7_MESSAGE_PARSER) += pkcs7_parser.o
+x509_mbedtls-$(CONFIG_$(SPL_)MSCODE_PARSER) += mscode_parser.o
 
 obj-$(CONFIG_MBEDTLS_LIB_CRYPTO) += mbedtls_lib_crypto.o
 mbedtls_lib_crypto-y := \
diff --git a/lib/mbedtls/mscode_parser.c b/lib/mbedtls/mscode_parser.c
new file mode 100644
index 000..34715f3a137
--- /dev/null
+++ b/lib/mbedtls/mscode_parser.c
@@ -0,0 +1,111 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * MSCode parser using MbedTLS ASN1 library
+ *
+ * Copyright (c) 2024 Linaro Limited
+ * Author: Raymond Mao 
+ */
+
+#include 
+#include 
+#include 
+#include 
+
+/*
+ * Parse a Microsoft Individual Code Signing blob
+ *
+ * U.P.SEQUENCE {
+ *U.P.OBJECTIDENTIFIER 1.3.6.1.4.1.311.2.1.15 (SPC_PE_IMAGE_DATA_OBJID)
+ *U.P.SEQUENCE {
+ *   U.P.BITSTRING NaN : 0 unused bit(s);
+ *   [C.P.0] {
+ *  [C.P.2] {
+ * [C.P.0] 
+ *  }
+ *   }
+ *}
+ * }
+ * U.P.SEQUENCE {
+ *U.P.SEQUENCE {
+ *   U.P.OBJECTIDENTIFIER 
+ *   U.P.NULL
+ *}
+ *U.P.OCTETSTRING 
+ * }
+ *
+ */
+int mscode_parse(void *_ctx, const void *content_data, size_t data_len,
+size_t asn1hdrlen)
+{
+   struct pefile_context *ctx = _ctx;
+   unsigned char *p = (unsigned char *)content_data;
+   unsigned char *end = (unsigned char *)content_data + data_len;
+   size_t len = 0;
+   int ret;
+   unsigned char *inner_p;
+   size_t seq_len = 0;
+
+   ret = mbedtls_asn1_get_tag(, end, _len,
+  MBEDTLS_ASN1_CONSTRUCTED |
+  MBEDTLS_ASN1_SEQUENCE);
+   if (ret)
+   return ret;
+
+   inner_p = p;
+   ret = mbedtls_asn1_get_tag(_p, inner_p + seq_len, , 
MBEDTLS_ASN1_OID);
+   if (ret)
+   return ret;
+
+   /* Sanity check on the PE Image Data OID (1.3.6.1.4.1.311.2.1.15) */
+   if (MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_MICROSOFT_PEIMAGEDATA, inner_p, 
len))
+   return -EINVAL;
+
+   p += seq_len;
+   ret = mbedtls_asn1_get_tag(, end, _len,
+  MBEDTLS_ASN1_CONSTRUCTED |
+  MBEDTLS_ASN1_SEQUENCE);
+   if (ret)
+   return ret;
+
+   ret = mbedtls_asn1_get_tag(, p + seq_len, _len,
+  MBEDTLS_ASN1_CONSTRUCTED |
+  MBEDTLS_ASN1_SEQUENCE);
+   if (ret)
+   return ret;
+
+   inner_p = p;
+
+   /*
+* Check if the inner sequence contains a supported hash
+* algorithm OID
+*/
+   ret = mbedtls_asn1_get_tag(_p, inner_p + seq_len, , 
MBEDTLS_ASN1_OID);
+   if (ret)
+   return ret;
+
+   if (!MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_DIGEST_ALG_MD5, inner_p, len))
+   ctx->digest_algo = "md5";
+   else if (!MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_DIGEST_ALG_SHA1, inner_p, 
len))
+   ctx->digest_algo = "sha1";
+   else if (!MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_DIGEST_ALG_SHA224, inner_p, 
len))
+   ctx->digest_algo = "sha224";
+   else if (!MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_DIGEST_ALG_SHA256, inner_p, 
len))
+   ctx->digest_algo = "sha256";
+   else if (!MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_DIGEST_ALG_SHA384, inner_p, 
len))
+   ctx->digest_algo = "sha384";
+   else if (!MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_DIGEST_ALG_SHA512, inner_p, 
len))
+   ctx->digest_algo = "sha512";
+
+   if (!ctx->digest_algo)
+   return -EINVAL;
+
+   p += seq_len;
+   ret = mbedtls_asn1_get_tag(, end, , MBEDTLS_ASN1_OCTET_STRING);
+   if (ret)
+   return ret;
+
+   ctx->digest = p;
+   ctx->digest_len = len;
+
+   return 0;
+}
-- 
2.25.1



[PATCH v3 18/25] lib/crypto: Adapt PKCS7 parser to MbedTLS

2024-05-28 Thread Raymond Mao
Previous patch has introduced MbedTLS porting layer for PKCS7 parser,
here to adjust the header and makefiles accordingly.

Signed-off-by: Raymond Mao 
---
Changes in v2
- Move the porting layer to MbedTLS dir.
Changes in v3
- Update commit message.

 include/crypto/pkcs7_parser.h | 56 +++
 lib/crypto/Makefile   |  4 ++-
 2 files changed, 59 insertions(+), 1 deletion(-)

diff --git a/include/crypto/pkcs7_parser.h b/include/crypto/pkcs7_parser.h
index 2c45cce5234..9f4549871f3 100644
--- a/include/crypto/pkcs7_parser.h
+++ b/include/crypto/pkcs7_parser.h
@@ -11,6 +11,12 @@
 #include 
 #include 
 #include 
+#if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
+#include 
+#include 
+#include 
+#include 
+#endif
 #include 
 
 #define kenter(FMT, ...) \
@@ -18,7 +24,54 @@
 #define kleave(FMT, ...) \
pr_devel("<== %s()"FMT"\n", __func__, ##__VA_ARGS__)
 
+/* Backup the parsed MedTLS context that we need */
+#if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
+struct pkcs7_mbedtls_ctx {
+   void *content_data;
+};
+
+struct pkcs7_sinfo_mbedtls_ctx {
+   void *authattrs_data;
+   void *content_data_digest;
+};
+#endif
+
+/*
+ * MbedTLS integration Notes:
+ *
+ * MbedTLS PKCS#7 library does not originally support parsing MicroSoft
+ * Authentication Code which is used for verifying the PE image digest.
+ *
+ * 1.  Authenticated Attributes (authenticatedAttributes)
+ * MbedTLS assumes unauthenticatedAttributes and authenticatedAttributes
+ * fields not exist.
+ * See MbedTLS function 'pkcs7_get_signer_info' for details.
+ *
+ * 2.  MicroSoft Authentication Code (mscode)
+ * MbedTLS only supports Content Data type defined as 1.2.840.113549.1.7.1
+ * (MBEDTLS_OID_PKCS7_DATA, aka OID_data).
+ * 1.3.6.1.4.1.311.2.1.4 (MicroSoft Authentication Code, aka
+ * OID_msIndirectData) is not supported.
+ * See MbedTLS function 'pkcs7_get_content_info_type' for details.
+ *
+ * But the EFI loader assumes that a PKCS#7 message with an EFI image always
+ * contains MicroSoft Authentication Code as Content Data (msg->data is NOT
+ * NULL), see function 'efi_signature_verify'.
+ *
+ * MbedTLS patch 
"0002-support-MicroSoft-authentication-code-in-PKCS7-lib.patch"
+ * is to support both above features by parsing the Content Data and
+ * Authenticate Attributes from a given PKCS#7 message.
+ *
+ * Other fields we don't need to populate from MbedTLS, which are used
+ * internally by pkcs7_verify:
+ * 'signer', 'unsupported_crypto', 'blacklisted'
+ * 'sig->digest' is used internally by pkcs7_digest to calculate the hash of
+ * Content Data or Authenticate Attributes.
+ */
 struct pkcs7_signed_info {
+#if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
+   struct pkcs7_sinfo_mbedtls_ctx *mbedtls_ctx;
+#endif
struct pkcs7_signed_info *next;
struct x509_certificate *signer; /* Signing certificate (in msg->certs) 
*/
unsignedindex;
@@ -55,6 +108,9 @@ struct pkcs7_signed_info {
 };
 
 struct pkcs7_message {
+#if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
+   struct pkcs7_mbedtls_ctx *mbedtls_ctx;
+#endif
struct x509_certificate *certs; /* Certificate list */
struct x509_certificate *crl;   /* Revocation list */
struct pkcs7_signed_info *signed_infos;
diff --git a/lib/crypto/Makefile b/lib/crypto/Makefile
index c89cef5685c..c3fe9c9d2c0 100644
--- a/lib/crypto/Makefile
+++ b/lib/crypto/Makefile
@@ -48,6 +48,7 @@ $(obj)/x509.asn1.o: $(obj)/x509.asn1.c $(obj)/x509.asn1.h
 $(obj)/x509_akid.asn1.o: $(obj)/x509_akid.asn1.c $(obj)/x509_akid.asn1.h
 endif
 
+ifneq ($(CONFIG_MBEDTLS_LIB_X509), y)
 #
 # PKCS#7 message handling
 #
@@ -55,10 +56,11 @@ obj-$(CONFIG_$(SPL_)PKCS7_MESSAGE_PARSER) += pkcs7_message.o
 pkcs7_message-y := \
pkcs7.asn1.o \
pkcs7_parser.o
-obj-$(CONFIG_$(SPL_)PKCS7_VERIFY) += pkcs7_verify.o
 
 $(obj)/pkcs7_parser.o: $(obj)/pkcs7.asn1.h
 $(obj)/pkcs7.asn1.o: $(obj)/pkcs7.asn1.c $(obj)/pkcs7.asn1.h
+endif
+obj-$(CONFIG_$(SPL_)PKCS7_VERIFY) += pkcs7_verify.o
 
 #
 # Signed PE binary-wrapped key handling
-- 
2.25.1



[PATCH v3 17/25] mbedtls: add PKCS7 parser porting layer

2024-05-28 Thread Raymond Mao
Add porting layer for PKCS7 parser on top of MbedTLS PKCS7 library.

Signed-off-by: Raymond Mao 
---
Changes in v2
- Move the porting layer to MbedTLS dir.
- Fix EFI Capsule CI test failures.
Changes in v3
- None.

 lib/mbedtls/Makefile   |   1 +
 lib/mbedtls/pkcs7_parser.c | 533 +
 2 files changed, 534 insertions(+)
 create mode 100644 lib/mbedtls/pkcs7_parser.c

diff --git a/lib/mbedtls/Makefile b/lib/mbedtls/Makefile
index e7cba1ad17c..005b8a25320 100644
--- a/lib/mbedtls/Makefile
+++ b/lib/mbedtls/Makefile
@@ -25,6 +25,7 @@ hash_mbedtls-$(CONFIG_$(SPL_)SHA512) += sha512.o
 obj-$(CONFIG_MBEDTLS_LIB_X509) += x509_mbedtls.o
 x509_mbedtls-$(CONFIG_$(SPL_)ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o
 x509_mbedtls-$(CONFIG_$(SPL_)X509_CERTIFICATE_PARSER) += x509_cert_parser.o
+x509_mbedtls-$(CONFIG_$(SPL_)PKCS7_MESSAGE_PARSER) += pkcs7_parser.o
 
 obj-$(CONFIG_MBEDTLS_LIB_CRYPTO) += mbedtls_lib_crypto.o
 mbedtls_lib_crypto-y := \
diff --git a/lib/mbedtls/pkcs7_parser.c b/lib/mbedtls/pkcs7_parser.c
new file mode 100644
index 000..a581224b469
--- /dev/null
+++ b/lib/mbedtls/pkcs7_parser.c
@@ -0,0 +1,533 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * PKCS#7 parser using MbedTLS PKCS#7 library
+ *
+ * Copyright (c) 2024 Linaro Limited
+ * Author: Raymond Mao 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+
+static void pkcs7_free_mbedtls_ctx(struct pkcs7_mbedtls_ctx *ctx)
+{
+   if (ctx) {
+   kfree(ctx->content_data);
+   kfree(ctx);
+   }
+}
+
+static void pkcs7_free_sinfo_mbedtls_ctx(struct pkcs7_sinfo_mbedtls_ctx *ctx)
+{
+   if (ctx) {
+   kfree(ctx->authattrs_data);
+   kfree(ctx->content_data_digest);
+   kfree(ctx);
+   }
+}
+
+/*
+ * Parse Authenticate Attributes
+ * TODO: Shall we consider to integrate decoding of authenticate attribute into
+ *  MbedTLS library?
+ *
+ * There are two kinds of structure for the Authenticate Attributes being used
+ * in U-Boot.
+ *
+ * Type 1 - contains in a PE/COFF EFI image:
+ *
+ * [C.P.0] {
+ *   U.P.SEQUENCE {
+ * U.P.OBJECTIDENTIFIER 1.2.840.113549.1.9.3 (OID_contentType)
+ * U.P.SET {
+ *U.P.OBJECTIDENTIFIER 1.3.6.1.4.1.311.2.1.4 (OID_msIndirectData)
+ * }
+ *  }
+ *  U.P.SEQUENCE {
+ * U.P.OBJECTIDENTIFIER 1.2.840.113549.1.9.5 (OID_signingTime)
+ * U.P.SET {
+ *U.P.UTCTime ''
+ * }
+ *  }
+ *  U.P.SEQUENCE {
+ * U.P.OBJECTIDENTIFIER 1.2.840.113549.1.9.4 (OID_messageDigest)
+ * U.P.SET {
+ *U.P.OCTETSTRING 
+ * }
+ *  }
+ *U.P.SEQUENCE {
+ *U.P.OBJECTIDENTIFIER 1.2.840.113549.1.9.15 (OID_smimeCapabilites)
+ *   U.P.SET {
+ *  U.P.SEQUENCE {
+ * <...>
+ *  }
+ *   }
+ *}
+ * }
+ *
+ * Type 2 - contains in an EFI Capsule:
+ *
+ * [C.P.0] {
+ *   U.P.SEQUENCE {
+ *  U.P.OBJECTIDENTIFIER 1.2.840.113549.1.9.3 (OID_contentType)
+ *  U.P.SET {
+ * U.P.OBJECTIDENTIFIER 1.2.840.113549.1.7.1 (OID_data)
+ *  }
+ *   }
+ *   U.P.SEQUENCE {
+ *  U.P.OBJECTIDENTIFIER 1.2.840.113549.1.9.5 (OID_signingTime)
+ *  U.P.SET {
+ * U.P.UTCTime ''
+ *  }
+ *   }
+ *   U.P.SEQUENCE {
+ *  U.P.OBJECTIDENTIFIER 1.2.840.113549.1.9.4 (OID_messageDigest)
+ *  U.P.SET {
+ * U.P.OCTETSTRING 
+ *  }
+ *  }
+ *}
+ *
+ * Note:
+ * They have different Content Type (OID_msIndirectData or OID_data).
+ * OID_smimeCapabilites only exists in a PE/COFF EFI image.
+ */
+static int authattrs_parse(struct pkcs7_message *msg, void *aa, size_t aa_len,
+  struct pkcs7_signed_info *sinfo)
+{
+   unsigned char *p = (unsigned char *)aa;
+   unsigned char *end = (unsigned char *)aa + aa_len;
+   size_t len = 0;
+   int ret;
+   unsigned char *inner_p;
+   size_t seq_len = 0;
+
+   ret = mbedtls_asn1_get_tag(, end, _len,
+  MBEDTLS_ASN1_CONTEXT_SPECIFIC |
+  MBEDTLS_ASN1_CONSTRUCTED);
+   if (ret)
+   return ret;
+
+   while (!mbedtls_asn1_get_tag(, end, _len,
+MBEDTLS_ASN1_CONSTRUCTED |
+MBEDTLS_ASN1_SEQUENCE)) {
+   inner_p = p;
+   ret = mbedtls_asn1_get_tag(_p, p + seq_len, ,
+  MBEDTLS_ASN1_OID);
+   if (ret)
+   return ret;
+
+   if (!MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_PKCS9_CONTENTTYPE, 
inner_p, len)) {
+   inner_p += len;
+   ret = mbedtls_asn1_get_tag(_p, p + seq_len, ,
+  MBEDTLS_ASN1_CONSTRUCTED |
+  MBEDTLS_ASN1_SET);
+   if (ret)
+   return ret;
+
+   ret = 

[PATCH v3 16/25] lib/crypto: Adapt x509_cert_parser to MbedTLS

2024-05-28 Thread Raymond Mao
Previous patch has introduced MbedTLS porting layer for x509 cert parser,
here to adjust the header and makefiles accordingly.

Signed-off-by: Raymond Mao 
---
Changes in v2
- Move the porting layer to MbedTLS dir.
Changes in v3
- Update commit message.

 include/crypto/x509_parser.h | 36 
 lib/crypto/Makefile  |  2 ++
 lib/crypto/x509_public_key.c |  4 
 3 files changed, 42 insertions(+)

diff --git a/include/crypto/x509_parser.h b/include/crypto/x509_parser.h
index 4cbdc1d6612..32ca9d5db79 100644
--- a/include/crypto/x509_parser.h
+++ b/include/crypto/x509_parser.h
@@ -11,8 +11,36 @@
 #include 
 #include 
 #include 
+#if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
+#include 
+#include 
+#include 
+#endif
 
+#if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
+/* Backup of part of the parsing context */
+struct x509_cert_mbedtls_ctx {
+   void*tbs;   /* Signed data */
+   void*raw_serial;/* Raw serial number in ASN.1 */
+   void*raw_issuer;/* Raw issuer name in ASN.1 */
+   void*raw_subject;   /* Raw subject name in ASN.1 */
+   void*raw_skid;  /* Raw subjectKeyId in ASN.1 */
+};
+#endif
+
+/*
+ * MbedTLS integration Notes:
+ *
+ * Fields we don't need to populate from MbedTLS:
+ * 'raw_sig' and 'raw_sig_size' are buffer for x509_parse_context,
+ * not needed for MbedTLS.
+ * 'signer' and 'seen' are used internally by pkcs7_verify.
+ * 'verified' is not inuse.
+ */
 struct x509_certificate {
+#if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
+   struct x509_cert_mbedtls_ctx *mbedtls_ctx;
+#endif
struct x509_certificate *next;
struct x509_certificate *signer;/* Certificate that signed this 
one */
struct public_key *pub; /* Public key details */
@@ -48,6 +76,12 @@ struct x509_certificate {
  * x509_cert_parser.c
  */
 extern void x509_free_certificate(struct x509_certificate *cert);
+#if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
+int x509_populate_pubkey(mbedtls_x509_crt *cert, struct public_key **pub_key);
+int x509_populate_cert(mbedtls_x509_crt *mbedtls_cert,
+  struct x509_certificate **pcert);
+time64_t x509_get_timestamp(const mbedtls_x509_time *x509_time);
+#endif
 extern struct x509_certificate *x509_cert_parse(const void *data, size_t 
datalen);
 extern int x509_decode_time(time64_t *_t,  size_t hdrlen,
unsigned char tag,
@@ -56,6 +90,8 @@ extern int x509_decode_time(time64_t *_t,  size_t hdrlen,
 /*
  * x509_public_key.c
  */
+#if !CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
 extern int x509_get_sig_params(struct x509_certificate *cert);
+#endif
 extern int x509_check_for_self_signed(struct x509_certificate *cert);
 #endif /* _X509_PARSER_H */
diff --git a/lib/crypto/Makefile b/lib/crypto/Makefile
index 8f7d9811f03..c89cef5685c 100644
--- a/lib/crypto/Makefile
+++ b/lib/crypto/Makefile
@@ -29,6 +29,7 @@ ifdef CONFIG_SPL_BUILD
 CFLAGS_rsa_helper.o += -I$(obj)
 endif
 
+ifneq ($(CONFIG_MBEDTLS_LIB_X509), y)
 #
 # X.509 Certificate handling
 #
@@ -45,6 +46,7 @@ $(obj)/x509_cert_parser.o: \
 
 $(obj)/x509.asn1.o: $(obj)/x509.asn1.c $(obj)/x509.asn1.h
 $(obj)/x509_akid.asn1.o: $(obj)/x509_akid.asn1.c $(obj)/x509_akid.asn1.h
+endif
 
 #
 # PKCS#7 message handling
diff --git a/lib/crypto/x509_public_key.c b/lib/crypto/x509_public_key.c
index a10145a7cdc..962611f3fee 100644
--- a/lib/crypto/x509_public_key.c
+++ b/lib/crypto/x509_public_key.c
@@ -30,6 +30,8 @@
 #include "x509_parser.h"
 #endif
 
+#if !CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
+
 /*
  * Set up the signature parameters in an X.509 certificate.  This involves
  * digesting the signed data and extracting the signature.
@@ -139,6 +141,8 @@ error:
return ret;
 }
 
+#endif /* !CONFIG_IS_ENABLED(MBEDTLS_LIB_X509) */
+
 /*
  * Check for self-signedness in an X.509 cert and if found, check the signature
  * immediately if we can.
-- 
2.25.1



[PATCH v3 15/25] mbedtls: add X509 cert parser porting layer

2024-05-28 Thread Raymond Mao
Add porting layer for X509 cert parser on top of MbedTLS X509
library.

Signed-off-by: Raymond Mao 
---
Changes in v2
- Move the porting layer to MbedTLS dir.
Changes in v3
- None.

 lib/mbedtls/Makefile   |   1 +
 lib/mbedtls/x509_cert_parser.c | 497 +
 2 files changed, 498 insertions(+)
 create mode 100644 lib/mbedtls/x509_cert_parser.c

diff --git a/lib/mbedtls/Makefile b/lib/mbedtls/Makefile
index cd0144eac1c..e7cba1ad17c 100644
--- a/lib/mbedtls/Makefile
+++ b/lib/mbedtls/Makefile
@@ -24,6 +24,7 @@ hash_mbedtls-$(CONFIG_$(SPL_)SHA512) += sha512.o
 # x509 libraries
 obj-$(CONFIG_MBEDTLS_LIB_X509) += x509_mbedtls.o
 x509_mbedtls-$(CONFIG_$(SPL_)ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o
+x509_mbedtls-$(CONFIG_$(SPL_)X509_CERTIFICATE_PARSER) += x509_cert_parser.o
 
 obj-$(CONFIG_MBEDTLS_LIB_CRYPTO) += mbedtls_lib_crypto.o
 mbedtls_lib_crypto-y := \
diff --git a/lib/mbedtls/x509_cert_parser.c b/lib/mbedtls/x509_cert_parser.c
new file mode 100644
index 000..b0867d31047
--- /dev/null
+++ b/lib/mbedtls/x509_cert_parser.c
@@ -0,0 +1,497 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * X509 cert parser using MbedTLS X509 library
+ *
+ * Copyright (c) 2024 Linaro Limited
+ * Author: Raymond Mao 
+ */
+
+#include 
+#include 
+#include 
+
+static void x509_free_mbedtls_ctx(struct x509_cert_mbedtls_ctx *ctx)
+{
+   if (ctx) {
+   kfree(ctx->tbs);
+   kfree(ctx->raw_serial);
+   kfree(ctx->raw_issuer);
+   kfree(ctx->raw_subject);
+   kfree(ctx->raw_skid);
+   kfree(ctx);
+   }
+}
+
+static int x509_set_cert_flags(struct x509_certificate *cert)
+{
+   struct public_key_signature *sig = cert->sig;
+
+   if (!sig || !cert->pub) {
+   pr_err("Signature or public key is not initialized\n");
+   return -ENOPKG;
+   }
+
+   if (!cert->pub->pkey_algo)
+   cert->unsupported_key = true;
+
+   if (!sig->pkey_algo)
+   cert->unsupported_sig = true;
+
+   if (!sig->hash_algo)
+   cert->unsupported_sig = true;
+
+   /* TODO: is_hash_blacklisted()? */
+
+   /* Detect self-signed certificates and set self_signed flag */
+   return x509_check_for_self_signed(cert);
+}
+
+/*
+ * Check for self-signedness in an X.509 cert and if found, check the signature
+ * immediately if we can.
+ */
+int x509_check_for_self_signed(struct x509_certificate *cert)
+{
+   int ret = 0;
+
+   if (cert->raw_subject_size != cert->raw_issuer_size ||
+   memcmp(cert->raw_subject, cert->raw_issuer,
+  cert->raw_issuer_size) != 0)
+   goto not_self_signed;
+
+   if (cert->sig->auth_ids[0] || cert->sig->auth_ids[1]) {
+   /* If the AKID is present it may have one or two parts.  If
+* both are supplied, both must match.
+*/
+   bool a = asymmetric_key_id_same(cert->skid, 
cert->sig->auth_ids[1]);
+   bool b = asymmetric_key_id_same(cert->id, 
cert->sig->auth_ids[0]);
+
+   if (!a && !b)
+   goto not_self_signed;
+
+   ret = -EKEYREJECTED;
+   if (((a && !b) || (b && !a)) &&
+   cert->sig->auth_ids[0] && cert->sig->auth_ids[1])
+   goto out;
+   }
+
+   ret = -EKEYREJECTED;
+   if (strcmp(cert->pub->pkey_algo, cert->sig->pkey_algo) != 0)
+   goto out;
+
+   ret = public_key_verify_signature(cert->pub, cert->sig);
+   if (ret < 0) {
+   if (ret == -ENOPKG) {
+   cert->unsupported_sig = true;
+   ret = 0;
+   }
+   goto out;
+   }
+
+   pr_devel("Cert Self-signature verified");
+   cert->self_signed = true;
+
+out:
+   return ret;
+
+not_self_signed:
+   return 0;
+}
+
+time64_t x509_get_timestamp(const mbedtls_x509_time *x509_time)
+{
+   unsigned int year, mon, day, hour, min, sec;
+
+   /* Adjust for year since 1900 */
+   year = x509_time->year - 1900;
+   /* Adjust for 0-based month */
+   mon = x509_time->mon - 1;
+   day = x509_time->day;
+   hour = x509_time->hour;
+   min = x509_time->min;
+   sec = x509_time->sec;
+
+   return (time64_t)mktime64(year, mon, day, hour, min, sec);
+}
+
+static char *x509_populate_dn_name_string(const mbedtls_x509_name *name)
+{
+   size_t len = 256;
+   size_t wb;
+   char *name_str;
+
+   do {
+   name_str = kzalloc(len, GFP_KERNEL);
+   if (!name_str)
+   return NULL;
+
+   wb = mbedtls_x509_dn_gets(name_str, len, name);
+   if (wb < 0) {
+   pr_err("Get DN string failed, ret:-0x%04x\n",
+  (unsigned int)-wb);
+   kfree(name_str);
+   len = len * 2; /* Try 

[PATCH v3 14/25] lib/crypto: Adapt public_key header with MbedTLS

2024-05-28 Thread Raymond Mao
Previous patch has introduced MbedTLS porting layer for public key,
here to adjust the header and makefiles accordingly.

Signed-off-by: Raymond Mao 
---
Changes in v2
- Move the porting layer to MbedTLS dir.
Changes in v3
- Update commit message.

 include/crypto/public_key.h  | 6 ++
 lib/crypto/Makefile  | 2 ++
 lib/crypto/asymmetric_type.c | 2 +-
 3 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/include/crypto/public_key.h b/include/crypto/public_key.h
index 3ba90fcc348..55cd4c2b012 100644
--- a/include/crypto/public_key.h
+++ b/include/crypto/public_key.h
@@ -12,6 +12,12 @@
 
 #ifdef __UBOOT__
 #include 
+#if CONFIG_IS_ENABLED(MBEDTLS_LIB_X509)
+#include 
+#include 
+#include 
+#include 
+#endif
 #else
 #include 
 #endif
diff --git a/lib/crypto/Makefile b/lib/crypto/Makefile
index bec1bc95a65..8f7d9811f03 100644
--- a/lib/crypto/Makefile
+++ b/lib/crypto/Makefile
@@ -7,7 +7,9 @@ obj-$(CONFIG_$(SPL_)ASYMMETRIC_KEY_TYPE) += asymmetric_keys.o
 
 asymmetric_keys-y := asymmetric_type.o
 
+ifneq ($(CONFIG_MBEDTLS_LIB_X509), y)
 obj-$(CONFIG_$(SPL_)ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o
+endif
 
 #
 # RSA public key parser
diff --git a/lib/crypto/asymmetric_type.c b/lib/crypto/asymmetric_type.c
index 24c2d15ef97..95b82cd8e84 100644
--- a/lib/crypto/asymmetric_type.c
+++ b/lib/crypto/asymmetric_type.c
@@ -12,7 +12,6 @@
 #include 
 #include 
 #endif
-#include 
 #ifdef __UBOOT__
 #include 
 #include 
@@ -26,6 +25,7 @@
 #include 
 #include 
 #endif
+#include 
 #ifdef __UBOOT__
 #include 
 #else
-- 
2.25.1



[PATCH v3 13/25] mbedtls: add public key porting layer

2024-05-28 Thread Raymond Mao
Add porting layer for public key on top of MbedTLS X509 library.

Signed-off-by: Raymond Mao 
---
Changes in v2
- Move the porting layer to MbedTLS dir.
Changes in v3
- None.

 lib/mbedtls/Makefile |   4 ++
 lib/mbedtls/public_key.c | 105 +++
 2 files changed, 109 insertions(+)
 create mode 100644 lib/mbedtls/public_key.c

diff --git a/lib/mbedtls/Makefile b/lib/mbedtls/Makefile
index b8eda9638f4..cd0144eac1c 100644
--- a/lib/mbedtls/Makefile
+++ b/lib/mbedtls/Makefile
@@ -21,6 +21,10 @@ hash_mbedtls-$(CONFIG_$(SPL_)SHA1) += sha1.o
 hash_mbedtls-$(CONFIG_$(SPL_)SHA256) += sha256.o
 hash_mbedtls-$(CONFIG_$(SPL_)SHA512) += sha512.o
 
+# x509 libraries
+obj-$(CONFIG_MBEDTLS_LIB_X509) += x509_mbedtls.o
+x509_mbedtls-$(CONFIG_$(SPL_)ASYMMETRIC_PUBLIC_KEY_SUBTYPE) += public_key.o
+
 obj-$(CONFIG_MBEDTLS_LIB_CRYPTO) += mbedtls_lib_crypto.o
 mbedtls_lib_crypto-y := \
$(MBEDTLS_LIB_DIR)/aes.o \
diff --git a/lib/mbedtls/public_key.c b/lib/mbedtls/public_key.c
new file mode 100644
index 000..2297b4397ba
--- /dev/null
+++ b/lib/mbedtls/public_key.c
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Public key helper functions using MbedTLS X509 library
+ *
+ * Copyright (c) 2024 Linaro Limited
+ * Author: Raymond Mao 
+ */
+
+#include 
+#include 
+
+void public_key_free(struct public_key *key)
+{
+   if (key) {
+   kfree(key->key);
+   kfree(key->params);
+   kfree(key);
+   }
+}
+
+void public_key_signature_free(struct public_key_signature *sig)
+{
+   int i;
+
+   if (sig) {
+   for (i = 0; i < ARRAY_SIZE(sig->auth_ids); i++)
+   kfree(sig->auth_ids[i]);
+   kfree(sig->s);
+   kfree(sig->digest);
+   kfree(sig);
+   }
+}
+
+int public_key_verify_signature(const struct public_key *pkey,
+   const struct public_key_signature *sig)
+{
+   mbedtls_md_type_t mb_hash_algo;
+   mbedtls_pk_context pk_ctx;
+   int ret;
+
+   if (!pkey || !sig || pkey->key_is_private)
+   return -EINVAL;
+
+   /*
+* ECRDSA (Elliptic Curve RedDSA) from Red Hat is not supported by
+* MbedTLS
+*/
+   if (strcmp(pkey->pkey_algo, "rsa")) {
+   pr_err("Encryption is not RSA: %s\n", sig->pkey_algo);
+   return -EINVAL;
+   }
+
+   /*
+* Can be pkcs1 or raw, but pkcs1 is expected.
+* This is just for argument checking, not necessarily passed to 
MbedTLS,
+* For RSA signatures, MbedTLS typically supports the PKCS#1 v1.5
+* (aka. pkcs1) encoding by default.
+* The library internally handles the details of decoding and verifying
+* the signature according to the expected encoding for the specified 
algorithm.
+*/
+   if (strcmp(sig->encoding, "pkcs1")) {
+   pr_err("Encoding %s is not supported, only supports pkcs1\n",
+  sig->encoding);
+   return -EINVAL;
+   }
+
+   if (!strcmp(sig->hash_algo, "sha1"))
+   mb_hash_algo = MBEDTLS_MD_SHA1;
+   else if (!strcmp(sig->hash_algo, "sha224"))
+   mb_hash_algo = MBEDTLS_MD_SHA224;
+   else if (!strcmp(sig->hash_algo, "sha256"))
+   mb_hash_algo = MBEDTLS_MD_SHA256;
+   else if (!strcmp(sig->hash_algo, "sha384"))
+   mb_hash_algo = MBEDTLS_MD_SHA384;
+   else if (!strcmp(sig->hash_algo, "sha512"))
+   mb_hash_algo = MBEDTLS_MD_SHA512;
+   else/* Unknown or unsupported hash algorithm */
+   return -EINVAL;
+   /* Initialize the mbedtls_pk_context with RSA key type */
+   mbedtls_pk_init(_ctx);
+
+   /* Parse the DER-encoded public key */
+   ret = mbedtls_pk_parse_public_key(_ctx, pkey->key, pkey->keylen);
+   if (ret) {
+   pr_err("Failed to parse public key, ret:-0x%04x\n",
+  (unsigned int)-ret);
+   ret = -EINVAL;
+   goto err_key;
+   }
+
+   /* Ensure that it is a RSA key */
+   if (mbedtls_pk_get_type(_ctx) != MBEDTLS_PK_RSA) {
+   pr_err("Only RSA keys are supported\n");
+   ret = -EKEYREJECTED;
+   goto err_key;
+   }
+
+   /* Verify the hash */
+   ret = mbedtls_pk_verify(_ctx, mb_hash_algo, sig->digest,
+   sig->digest_size, sig->s, sig->s_size);
+
+err_key:
+   mbedtls_pk_free(_ctx);
+   return ret;
+}
-- 
2.25.1



[PATCH v3 12/25] mbedtls/external: update MbedTLS PKCS7 test suites

2024-05-28 Thread Raymond Mao
Update the PKCS7 test suites for multiple certs.

The PR for this patch is at:
https://github.com/Mbed-TLS/mbedtls/pull/9001

For enabling EFI loader PKCS7 features with MbedTLS build,
we need this patch on top of MbedTLS v3.6.0 before it is merged into
the next MbedTLS LTS release.

Signed-off-by: Raymond Mao 
---
Changes in v2
- None.
Changes in v3
- Update commit message.

 .../external/mbedtls/tests/suites/test_suite_pkcs7.data   | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/mbedtls/external/mbedtls/tests/suites/test_suite_pkcs7.data 
b/lib/mbedtls/external/mbedtls/tests/suites/test_suite_pkcs7.data
index d3b83cdf0aa..2dd1c56109f 100644
--- a/lib/mbedtls/external/mbedtls/tests/suites/test_suite_pkcs7.data
+++ b/lib/mbedtls/external/mbedtls/tests/suites/test_suite_pkcs7.data
@@ -14,9 +14,9 @@ PKCS7 Signed Data Parse with zero signers
 depends_on:MBEDTLS_MD_CAN_SHA256
 pkcs7_parse:"data_files/pkcs7_data_no_signers.der":MBEDTLS_PKCS7_SIGNED_DATA
 
-PKCS7 Signed Data Parse Fail with multiple certs #4
+PKCS7 Signed Data Parse Pass with multiple certs #4
 depends_on:MBEDTLS_MD_CAN_SHA256:MBEDTLS_RSA_C
-pkcs7_parse:"data_files/pkcs7_data_multiple_certs_signed.der":MBEDTLS_ERR_PKCS7_FEATURE_UNAVAILABLE
+pkcs7_parse:"data_files/pkcs7_data_multiple_certs_signed.der":MBEDTLS_PKCS7_SIGNED_DATA
 
 PKCS7 Signed Data Parse Fail with corrupted cert #5.0
 depends_on:MBEDTLS_MD_CAN_SHA256:MBEDTLS_RSA_C
-- 
2.25.1



[PATCH v3 11/25] mbedtls/external: support decoding multiple signer's cert

2024-05-28 Thread Raymond Mao
Support decoding multiple signer's cert in the signed data within
a PKCS7 message.

The PR for this patch is at:
https://github.com/Mbed-TLS/mbedtls/pull/9001

For enabling EFI loader PKCS7 features with MbedTLS build,
we need this patch on top of MbedTLS v3.6.0 before it is merged into
the next MbedTLS LTS release.

Signed-off-by: Raymond Mao 
---
Changes in v2
- None.
Changes in v3
- Update commit message.

 lib/mbedtls/external/mbedtls/library/pkcs7.c | 75 
 1 file changed, 47 insertions(+), 28 deletions(-)

diff --git a/lib/mbedtls/external/mbedtls/library/pkcs7.c 
b/lib/mbedtls/external/mbedtls/library/pkcs7.c
index da73fb341d6..01105227d7a 100644
--- a/lib/mbedtls/external/mbedtls/library/pkcs7.c
+++ b/lib/mbedtls/external/mbedtls/library/pkcs7.c
@@ -61,6 +61,36 @@ static int pkcs7_get_next_content_len(unsigned char **p, 
unsigned char *end,
 return ret;
 }
 
+/**
+ * Get and decode one cert from a sequence.
+ * Return 0 for success,
+ * Return negative error code for failure.
+ **/
+static int pkcs7_get_one_cert(unsigned char **p, unsigned char *end,
+  mbedtls_x509_crt *certs)
+{
+int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
+size_t len = 0;
+unsigned char *start = *p;
+unsigned char *end_cert;
+
+ret = mbedtls_asn1_get_tag(p, end, , MBEDTLS_ASN1_CONSTRUCTED
+   | MBEDTLS_ASN1_SEQUENCE);
+if (ret != 0) {
+return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_CERT, ret);
+}
+
+end_cert = *p + len;
+
+if ((ret = mbedtls_x509_crt_parse_der(certs, start, end_cert - start)) < 
0) {
+return MBEDTLS_ERR_PKCS7_INVALID_CERT;
+}
+
+*p = end_cert;
+
+return 0;
+}
+
 /**
  * version Version
  * Version ::= INTEGER
@@ -178,11 +208,12 @@ static int pkcs7_get_certificates(unsigned char **p, 
unsigned char *end,
   mbedtls_x509_crt *certs)
 {
 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
-size_t len1 = 0;
-size_t len2 = 0;
-unsigned char *end_set, *end_cert, *start;
+size_t len = 0;
+unsigned char *end_set;
+int num_of_certs = 0;
 
-ret = mbedtls_asn1_get_tag(p, end, , MBEDTLS_ASN1_CONSTRUCTED
+/* Get the set of certs */
+ret = mbedtls_asn1_get_tag(p, end, , MBEDTLS_ASN1_CONSTRUCTED
| MBEDTLS_ASN1_CONTEXT_SPECIFIC);
 if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
 return 0;
@@ -190,38 +221,26 @@ static int pkcs7_get_certificates(unsigned char **p, 
unsigned char *end,
 if (ret != 0) {
 return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_FORMAT, ret);
 }
-start = *p;
-end_set = *p + len1;
+end_set = *p + len;
 
-ret = mbedtls_asn1_get_tag(p, end_set, , MBEDTLS_ASN1_CONSTRUCTED
-   | MBEDTLS_ASN1_SEQUENCE);
+ret = pkcs7_get_one_cert(p, end_set, certs);
 if (ret != 0) {
-return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_CERT, ret);
+return ret;
 }
 
-end_cert = *p + len2;
+num_of_certs++;
 
-/*
- * This is to verify that there is only one signer certificate. It seems 
it is
- * not easy to differentiate between the chain vs different signer's 
certificate.
- * So, we support only the root certificate and the single signer.
- * The behaviour would be improved with addition of multiple signer 
support.
- */
-if (end_cert != end_set) {
-return MBEDTLS_ERR_PKCS7_FEATURE_UNAVAILABLE;
-}
-
-if ((ret = mbedtls_x509_crt_parse_der(certs, start, len1)) < 0) {
-return MBEDTLS_ERR_PKCS7_INVALID_CERT;
+while (*p != end_set) {
+ret = pkcs7_get_one_cert(p, end_set, certs);
+if (ret != 0) {
+return ret;
+}
+num_of_certs++;
 }
 
-*p = end_cert;
+*p = end_set;
 
-/*
- * Since in this version we strictly support single certificate, and 
reaching
- * here implies we have parsed successfully, we return 1.
- */
-return 1;
+return num_of_certs;
 }
 
 /**
-- 
2.25.1



[PATCH v3 10/25] mbedtls/external: support PKCS9 Authenticate Attributes

2024-05-28 Thread Raymond Mao
Populate PKCS9 Authenticate Attributes from signer info if it exists
in a PKCS7 message.
Add OIDs for describing objects using for Authenticate Attributes.

The PR for this patch is at:
https://github.com/Mbed-TLS/mbedtls/pull/9001

For enabling EFI loader PKCS7 features with MbedTLS build,
we need this patch on top of MbedTLS v3.6.0 before it is merged into
the next MbedTLS LTS release.

Signed-off-by: Raymond Mao 
---
Changes in v2
- None.
Changes in v3
- Update commit message.

 .../external/mbedtls/include/mbedtls/oid.h|  5 +
 .../external/mbedtls/include/mbedtls/pkcs7.h  | 11 +++
 lib/mbedtls/external/mbedtls/library/pkcs7.c  | 19 ++-
 3 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/lib/mbedtls/external/mbedtls/include/mbedtls/oid.h 
b/lib/mbedtls/external/mbedtls/include/mbedtls/oid.h
index 2ee982808fa..43cef99f1e3 100644
--- a/lib/mbedtls/external/mbedtls/include/mbedtls/oid.h
+++ b/lib/mbedtls/external/mbedtls/include/mbedtls/oid.h
@@ -238,6 +238,11 @@
 #define MBEDTLS_OID_RSA_SHA_OBS "\x2B\x0E\x03\x02\x1D"
 
 #define MBEDTLS_OID_PKCS9_EMAIL MBEDTLS_OID_PKCS9 "\x01" /**< 
emailAddress AttributeType ::= { pkcs-9 1 } */
+#define MBEDTLS_OID_PKCS9_CONTENTTYPE   MBEDTLS_OID_PKCS9 "\x03" /**< 
contentType AttributeType ::= { pkcs-9 3 } */
+#define MBEDTLS_OID_PKCS9_MESSAGEDIGEST MBEDTLS_OID_PKCS9 "\x04" /**< 
messageDigest AttributeType ::= { pkcs-9 4 } */
+#define MBEDTLS_OID_PKCS9_SIGNINGTIME   MBEDTLS_OID_PKCS9 "\x05" /**< 
signingTime AttributeType ::= { pkcs-9 5 } */
+#define MBEDTLS_OID_PKCS9_SMIMECAP  MBEDTLS_OID_PKCS9 "\x0f" /**< 
smimeCapabilites AttributeType ::= { pkcs-9 15 } */
+#define MBEDTLS_OID_PKCS9_SMIMEAA   MBEDTLS_OID_PKCS9 "\x10\x02\x0b" /**< 
smimeCapabilites AttributeType ::= { pkcs-9 16 2 11} */
 
 /* RFC 4055 */
 #define MBEDTLS_OID_RSASSA_PSS  MBEDTLS_OID_PKCS1 "\x0a" /**< 
id-RSASSA-PSS ::= { pkcs-1 10 } */
diff --git a/lib/mbedtls/external/mbedtls/include/mbedtls/pkcs7.h 
b/lib/mbedtls/external/mbedtls/include/mbedtls/pkcs7.h
index 9e29b74af70..a88a5e858fc 100644
--- a/lib/mbedtls/external/mbedtls/include/mbedtls/pkcs7.h
+++ b/lib/mbedtls/external/mbedtls/include/mbedtls/pkcs7.h
@@ -102,6 +102,16 @@ typedef enum {
 }
 mbedtls_pkcs7_type;
 
+/*
+ * Authenticate Attributes for MicroSoft Authentication Code using in U-Boot
+ * Secure Boot
+ */
+typedef struct mbedtls_pkcs7_authattrs {
+size_t data_len;
+void *data;
+}
+mbedtls_pkcs7_authattrs;
+
 /**
  * Structure holding PKCS #7 signer info
  */
@@ -113,6 +123,7 @@ typedef struct mbedtls_pkcs7_signer_info {
 mbedtls_x509_buf MBEDTLS_PRIVATE(alg_identifier);
 mbedtls_x509_buf MBEDTLS_PRIVATE(sig_alg_identifier);
 mbedtls_x509_buf MBEDTLS_PRIVATE(sig);
+mbedtls_pkcs7_authattrs authattrs;
 struct mbedtls_pkcs7_signer_info *MBEDTLS_PRIVATE(next);
 }
 mbedtls_pkcs7_signer_info;
diff --git a/lib/mbedtls/external/mbedtls/library/pkcs7.c 
b/lib/mbedtls/external/mbedtls/library/pkcs7.c
index 0c2436b56b7..da73fb341d6 100644
--- a/lib/mbedtls/external/mbedtls/library/pkcs7.c
+++ b/lib/mbedtls/external/mbedtls/library/pkcs7.c
@@ -288,6 +288,7 @@ static int pkcs7_get_signer_info(unsigned char **p, 
unsigned char *end,
 unsigned char *end_signer, *end_issuer_and_sn;
 int asn1_ret = 0, ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
 size_t len = 0;
+unsigned char *tmp_p;
 
 asn1_ret = mbedtls_asn1_get_tag(p, end, , MBEDTLS_ASN1_CONSTRUCTED
 | MBEDTLS_ASN1_SEQUENCE);
@@ -349,7 +350,23 @@ static int pkcs7_get_signer_info(unsigned char **p, 
unsigned char *end,
 goto out;
 }
 
-/* Assume authenticatedAttributes is nonexistent */
+/* Save authenticatedAttributes if present */
+if (*p < end_signer &&
+**p == (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0)) 
{
+tmp_p = *p;
+
+ret = mbedtls_asn1_get_tag(p, end_signer, ,
+   MBEDTLS_ASN1_CONTEXT_SPECIFIC |
+   MBEDTLS_ASN1_CONSTRUCTED | 0);
+if (ret != 0) {
+goto out;
+}
+
+signer->authattrs.data = tmp_p;
+signer->authattrs.data_len = len + *p - tmp_p;
+*p += len;
+}
+
 ret = pkcs7_get_digest_algorithm(p, end_signer, 
>sig_alg_identifier);
 if (ret != 0) {
 goto out;
-- 
2.25.1



[PATCH v3 09/25] mbedtls/external: support Microsoft Authentication Code

2024-05-28 Thread Raymond Mao
Populate Microsoft Authentication Code from the content data
into PKCS7 decoding context if it exists in a PKCS7 message.
Add OIDs for describing objects using for Microsoft Authentication
Code.

The PR for this patch is at:
https://github.com/Mbed-TLS/mbedtls/pull/9001

For enabling EFI loader PKCS7 features with MbedTLS build,
we need this patch on top of MbedTLS v3.6.0 before it is merged into
the next MbedTLS LTS release.

Signed-off-by: Raymond Mao 
---
Changes in v2
- None.
Changes in v3
- Update commit message.

 .../external/mbedtls/include/mbedtls/oid.h| 30 ++
 .../external/mbedtls/include/mbedtls/pkcs7.h  | 10 
 lib/mbedtls/external/mbedtls/library/pkcs7.c  | 60 +++
 3 files changed, 90 insertions(+), 10 deletions(-)

diff --git a/lib/mbedtls/external/mbedtls/include/mbedtls/oid.h 
b/lib/mbedtls/external/mbedtls/include/mbedtls/oid.h
index fdc25ebf885..2ee982808fa 100644
--- a/lib/mbedtls/external/mbedtls/include/mbedtls/oid.h
+++ b/lib/mbedtls/external/mbedtls/include/mbedtls/oid.h
@@ -352,6 +352,36 @@
 #define MBEDTLS_OID_PKCS12_PBE_SHA1_RC2_128_CBC MBEDTLS_OID_PKCS12_PBE 
"\x05" /**< pbeWithSHAAnd128BitRC2-CBC OBJECT IDENTIFIER ::= {pkcs-12PbeIds 5} 
*/
 #define MBEDTLS_OID_PKCS12_PBE_SHA1_RC2_40_CBC  MBEDTLS_OID_PKCS12_PBE 
"\x06" /**< pbeWithSHAAnd40BitRC2-CBC OBJECT IDENTIFIER ::= {pkcs-12PbeIds 6} */
 
+/*
+ * MicroSoft Authenticate Code OIDs
+ */
+#define MBEDTLS_OID_PRIVATE_ENTERPRISE  MBEDTLS_OID_INTERNET 
"\x04\x01" /* {iso(1) identified-organization(3) dod(6) internet(1) private(4) 
enterprise(1) */
+#define MBEDTLS_OID_MICROSOFT   "\x82\x37"  /* 
{microsoft(311)} */
+/*
+ * OID_msIndirectData: (1.3.6.1.4.1.311.2.1.4)
+ * {iso(1) identified-organization(3) dod(6) internet(1) private(4) 
enterprise(1) microsoft(311) 2(2) 1(1) 4(4)}
+ */
+#define MBEDTLS_OID_MICROSOFT_INDIRECTDATA  MBEDTLS_OID_PRIVATE_ENTERPRISE 
MBEDTLS_OID_MICROSOFT \
+"\x02\x01\x04"
+/*
+ * OID_msStatementType: (1.3.6.1.4.1.311.2.1.11)
+ * {iso(1) identified-organization(3) dod(6) internet(1) private(4) 
enterprise(1) microsoft(311) 2(2) 1(1) 11(11)}
+ */
+#define MBEDTLS_OID_MICROSOFT_STATETYPE  MBEDTLS_OID_PRIVATE_ENTERPRISE 
MBEDTLS_OID_MICROSOFT \
+"\x02\x01\x0b"
+/*
+ * OID_msSpOpusInfo: (1.3.6.1.4.1.311.2.1.12)
+ * {iso(1) identified-organization(3) dod(6) internet(1) private(4) 
enterprise(1) microsoft(311) 2(2) 1(1) 12(12)}
+ */
+#define MBEDTLS_OID_MICROSOFT_SPOPUSINFO  MBEDTLS_OID_PRIVATE_ENTERPRISE 
MBEDTLS_OID_MICROSOFT \
+"\x02\x01\x0b"
+/*
+ * OID_msPeImageDataObjId: (1.3.6.1.4.1.311.2.1.15)
+ * {iso(1) identified-organization(3) dod(6) internet(1) private(4) 
enterprise(1) microsoft(311) 2(2) 1(1) 15(15)}
+ */
+#define MBEDTLS_OID_MICROSOFT_PEIMAGEDATA  MBEDTLS_OID_PRIVATE_ENTERPRISE 
MBEDTLS_OID_MICROSOFT \
+"\x02\x01\x0f"
+
 /*
  * EC key algorithms from RFC 5480
  */
diff --git a/lib/mbedtls/external/mbedtls/include/mbedtls/pkcs7.h 
b/lib/mbedtls/external/mbedtls/include/mbedtls/pkcs7.h
index e9b482208e6..9e29b74af70 100644
--- a/lib/mbedtls/external/mbedtls/include/mbedtls/pkcs7.h
+++ b/lib/mbedtls/external/mbedtls/include/mbedtls/pkcs7.h
@@ -132,12 +132,22 @@ typedef struct mbedtls_pkcs7_signed_data {
 }
 mbedtls_pkcs7_signed_data;
 
+/* Content Data for MicroSoft Authentication Code using in U-Boot Secure Boot 
*/
+typedef struct mbedtls_pkcs7_conten_data {
+int data_type;  /* Type of Data */
+size_t data_len;/* Length of Data */
+size_t data_hdrlen; /* Length of Data ASN.1 header */
+void *data; /* Content Data */
+}
+mbedtls_pkcs7_conten_data;
+
 /**
  * Structure holding PKCS #7 structure, only signed data for now
  */
 typedef struct mbedtls_pkcs7 {
 mbedtls_pkcs7_buf MBEDTLS_PRIVATE(raw);
 mbedtls_pkcs7_signed_data MBEDTLS_PRIVATE(signed_data);
+mbedtls_pkcs7_conten_data content_data;
 }
 mbedtls_pkcs7;
 
diff --git a/lib/mbedtls/external/mbedtls/library/pkcs7.c 
b/lib/mbedtls/external/mbedtls/library/pkcs7.c
index 3aac662ba69..0c2436b56b7 100644
--- a/lib/mbedtls/external/mbedtls/library/pkcs7.c
+++ b/lib/mbedtls/external/mbedtls/library/pkcs7.c
@@ -29,6 +29,13 @@
 #include 
 #endif
 
+enum OID {
+/* PKCS#7 {iso(1) member-body(2) us(840) rsadsi(113549) pkcs(1) pkcs-7(7)} 
*/
+MBEDTLS_OID_DATA = 13,  /* 1.2.840.113549.1.7.1 */
+/* Microsoft Authenticode & Software Publishing */
+MBEDTLS_OID_MS_INDIRECTDATA = 24,/* 1.3.6.1.4.1.311.2.1.4 */
+};
+
 /**
  * Initializes the mbedtls_pkcs7 structure.
  */
@@ -449,7 +456,7 @@ cleanup:
  *  signerInfos SignerInfos }
  */
 static int pkcs7_get_signed_data(unsigned char *buf, size_t buflen,
- mbedtls_pkcs7_signed_data *signed_data)
+ mbedtls_pkcs7 *pkcs7)
 {
 unsigned char *p = buf;
 unsigned char *end = buf + buflen;
@@ -457,6 +464,7 @@ static int pkcs7_get_signed_data(unsigned char *buf, size_t 
buflen,
 size_t len = 

[PATCH v3 08/25] makefile: add mbedtls include directories

2024-05-28 Thread Raymond Mao
Add the mbedtls include directories into the build system.

Signed-off-by: Raymond Mao 
---
Changes in v2
- None.
Changes in v3
- Remove changes for PLATFORM_CPPFLAGS.

 Makefile | 6 ++
 1 file changed, 6 insertions(+)

diff --git a/Makefile b/Makefile
index 79b28c2d81f..2e7eb51401f 100644
--- a/Makefile
+++ b/Makefile
@@ -829,6 +829,12 @@ KBUILD_HOSTCFLAGS += $(if $(CONFIG_TOOLS_DEBUG),-g)
 UBOOTINCLUDE:= \
-Iinclude \
$(if $(KBUILD_SRC), -I$(srctree)/include) \
+   $(if $(KBUILD_SRC), \
+   $(if $(CONFIG_MBEDTLS_LIB), \
+   "-DMBEDTLS_CONFIG_FILE=\"mbedtls_def_config.h\"" \
+   -I$(srctree)/lib/mbedtls \
+   -I$(srctree)/lib/mbedtls/port \
+   -I$(srctree)/lib/mbedtls/external/mbedtls/include)) \
$(if $(CONFIG_$(SPL_)SYS_THUMB_BUILD), \
$(if $(CONFIG_HAS_THUMB2), \
$(if $(CONFIG_CPU_V7M), \
-- 
2.25.1



[PATCH v3 07/25] hash: integrate hash on mbedtls

2024-05-28 Thread Raymond Mao
Integrate common/hash.c on the hash shim layer so that hash APIs
from mbedtls can be leveraged by boot/image and efi_loader.

Signed-off-by: Raymond Mao 
---
Changes in v2
- Use the original head files instead of creating new ones.
Changes in v3
- Add handle checkers for malloc.

 common/hash.c | 143 ++
 1 file changed, 143 insertions(+)

diff --git a/common/hash.c b/common/hash.c
index 3d6b84de473..1c8e7731afe 100644
--- a/common/hash.c
+++ b/common/hash.c
@@ -36,6 +36,141 @@
 #include 
 #include 
 
+#if CONFIG_IS_ENABLED(MBEDTLS_LIB_CRYPTO)
+
+static int hash_init_sha1(struct hash_algo *algo, void **ctxp)
+{
+   int ret;
+   mbedtls_sha1_context *ctx = malloc(sizeof(mbedtls_sha1_context));
+
+   if (!ctx)
+   return -ENOMEM;
+
+   mbedtls_sha1_init(ctx);
+   ret = mbedtls_sha1_starts(ctx);
+   if (!ret) {
+   *ctxp = ctx;
+   } else {
+   mbedtls_sha1_free(ctx);
+   free(ctx);
+   }
+
+   return ret;
+}
+
+static int hash_update_sha1(struct hash_algo *algo, void *ctx, const void *buf,
+   unsigned int size, int is_last)
+{
+   return mbedtls_sha1_update((mbedtls_sha1_context *)ctx, buf, size);
+}
+
+static int
+hash_finish_sha1(struct hash_algo *algo, void *ctx, void *dest_buf, int size)
+{
+   int ret;
+
+   if (size < algo->digest_size)
+   return -1;
+
+   ret = mbedtls_sha1_finish((mbedtls_sha1_context *)ctx, dest_buf);
+   if (!ret) {
+   mbedtls_sha1_free((mbedtls_sha1_context *)ctx);
+   free(ctx);
+   }
+
+   return ret;
+}
+
+static int hash_init_sha256(struct hash_algo *algo, void **ctxp)
+{
+   int ret;
+   int is224 = algo->digest_size == SHA224_SUM_LEN ? 1 : 0;
+   mbedtls_sha256_context *ctx = malloc(sizeof(mbedtls_sha256_context));
+
+   if (!ctx)
+   return -ENOMEM;
+
+   mbedtls_sha256_init(ctx);
+   ret = mbedtls_sha256_starts(ctx, is224);
+   if (!ret) {
+   *ctxp = ctx;
+   } else {
+   mbedtls_sha256_free(ctx);
+   free(ctx);
+   }
+
+   return ret;
+}
+
+static int hash_update_sha256(struct hash_algo *algo, void *ctx, const void 
*buf,
+ uint size, int is_last)
+{
+   return mbedtls_sha256_update((mbedtls_sha256_context *)ctx, buf, size);
+}
+
+static int
+hash_finish_sha256(struct hash_algo *algo, void *ctx, void *dest_buf, int size)
+{
+   int ret;
+
+   if (size < algo->digest_size)
+   return -1;
+
+   ret = mbedtls_sha256_finish((mbedtls_sha256_context *)ctx, dest_buf);
+   if (!ret) {
+   mbedtls_sha256_free((mbedtls_sha256_context *)ctx);
+   free(ctx);
+   }
+
+   return ret;
+}
+
+static int hash_init_sha512(struct hash_algo *algo, void **ctxp)
+{
+   int ret;
+   int is384 = algo->digest_size == SHA384_SUM_LEN ? 1 : 0;
+   mbedtls_sha512_context *ctx = malloc(sizeof(mbedtls_sha512_context));
+
+   if (!ctx)
+   return -ENOMEM;
+
+   mbedtls_sha512_init(ctx);
+   ret = mbedtls_sha512_starts(ctx, is384);
+   if (!ret) {
+   *ctxp = ctx;
+   } else {
+   mbedtls_sha512_free(ctx);
+   free(ctx);
+   }
+
+   return ret;
+}
+
+static int hash_update_sha512(struct hash_algo *algo, void *ctx, const void 
*buf,
+ uint size, int is_last)
+{
+   return mbedtls_sha512_update((mbedtls_sha512_context *)ctx, buf, size);
+}
+
+static int
+hash_finish_sha512(struct hash_algo *algo, void *ctx, void *dest_buf, int size)
+{
+   int ret;
+
+   if (size < algo->digest_size)
+   return -1;
+
+   ret = mbedtls_sha512_finish((mbedtls_sha512_context *)ctx, dest_buf);
+   if (!ret) {
+   mbedtls_sha512_free((mbedtls_sha512_context *)ctx);
+   free(ctx);
+   }
+
+   return ret;
+}
+
+#else /* CONFIG_IS_ENABLED(MBEDTLS_LIB_CRYPTO) */
+
 static int __maybe_unused hash_init_sha1(struct hash_algo *algo, void **ctxp)
 {
sha1_context *ctx = malloc(sizeof(sha1_context));
@@ -144,6 +279,8 @@ static int __maybe_unused hash_finish_sha512(struct 
hash_algo *algo, void *ctx,
return 0;
 }
 
+#endif /* CONFIG_IS_ENABLED(MBEDTLS_LIB_CRYPTO) */
+
 static int hash_init_crc16_ccitt(struct hash_algo *algo, void **ctxp)
 {
uint16_t *ctx = malloc(sizeof(uint16_t));
@@ -268,10 +405,16 @@ static struct hash_algo hash_algo[] = {
.hash_init  = hw_sha_init,
.hash_update= hw_sha_update,
.hash_finish= hw_sha_finish,
+#else
+#if CONFIG_IS_ENABLED(MBEDTLS_LIB_CRYPTO)
+   .hash_init  = hash_init_sha512,
+   .hash_update= hash_update_sha512,
+   .hash_finish= hash_finish_sha512,
 #else
.hash_init  = 

[PATCH v3 06/25] mbedtls: add digest shim layer for MbedTLS

2024-05-28 Thread Raymond Mao
Implement digest shim layer on top of MbedTLS crypto library.

Signed-off-by: Raymond Mao 
---
Changes in v2
- Split the shim layer into separated files and use the original head
  files instead of creating new ones.
Changes in v3
- Refactored sha1_hmac and removed non-watchdog md5 function.

 include/u-boot/sha1.h |   4 ++
 lib/mbedtls/Makefile  |   7 +++
 lib/mbedtls/md5.c |  59 ++
 lib/mbedtls/sha1.c| 111 ++
 lib/mbedtls/sha256.c  |  65 +
 lib/mbedtls/sha512.c  |  96 
 6 files changed, 342 insertions(+)
 create mode 100644 lib/mbedtls/md5.c
 create mode 100644 lib/mbedtls/sha1.c
 create mode 100644 lib/mbedtls/sha256.c
 create mode 100644 lib/mbedtls/sha512.c

diff --git a/include/u-boot/sha1.h b/include/u-boot/sha1.h
index ee46fe947a0..6120284ad4f 100644
--- a/include/u-boot/sha1.h
+++ b/include/u-boot/sha1.h
@@ -37,6 +37,10 @@ extern "C" {
 #define SHA1_SUM_LEN   20
 #define SHA1_DER_LEN   15
 
+#define K_IPAD_VAL 0x36
+#define K_OPAD_VAL 0x5C
+#define K_PAD_LEN 64
+
 extern const uint8_t sha1_der_prefix[];
 
 #if defined(CONFIG_MBEDTLS_LIB_CRYPTO)
diff --git a/lib/mbedtls/Makefile b/lib/mbedtls/Makefile
index 85f0a3cfd07..b8eda9638f4 100644
--- a/lib/mbedtls/Makefile
+++ b/lib/mbedtls/Makefile
@@ -14,6 +14,13 @@ ccflags-y += \
-I$(src)/external/mbedtls/library \
# This line is intentionally left blank
 
+# shim layer for hash
+obj-$(CONFIG_MBEDTLS_LIB_CRYPTO) += hash_mbedtls.o
+hash_mbedtls-$(CONFIG_$(SPL_)MD5) += md5.o
+hash_mbedtls-$(CONFIG_$(SPL_)SHA1) += sha1.o
+hash_mbedtls-$(CONFIG_$(SPL_)SHA256) += sha256.o
+hash_mbedtls-$(CONFIG_$(SPL_)SHA512) += sha512.o
+
 obj-$(CONFIG_MBEDTLS_LIB_CRYPTO) += mbedtls_lib_crypto.o
 mbedtls_lib_crypto-y := \
$(MBEDTLS_LIB_DIR)/aes.o \
diff --git a/lib/mbedtls/md5.c b/lib/mbedtls/md5.c
new file mode 100644
index 000..aa8c159f66f
--- /dev/null
+++ b/lib/mbedtls/md5.c
@@ -0,0 +1,59 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Hash shim layer on MbedTLS Crypto library
+ *
+ * Copyright (c) 2023 Linaro Limited
+ * Author: Raymond Mao 
+ */
+#include "compiler.h"
+
+#ifndef USE_HOSTCC
+#include 
+#endif /* USE_HOSTCC */
+#include 
+
+void MD5Init(MD5Context *ctx)
+{
+   mbedtls_md5_init(ctx);
+   mbedtls_md5_starts(ctx);
+}
+
+void MD5Update(MD5Context *ctx, unsigned char const *buf, unsigned int len)
+{
+   mbedtls_md5_update(ctx, buf, len);
+}
+
+void MD5Final(unsigned char digest[16], MD5Context *ctx)
+{
+   mbedtls_md5_finish(ctx, digest);
+   mbedtls_md5_free(ctx);
+}
+
+void md5_wd(const unsigned char *input, unsigned int len,
+   unsigned char output[16], unsigned int chunk_sz)
+{
+   MD5Context context;
+#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
+   const unsigned char *end, *curr;
+   int chunk;
+#endif
+
+   MD5Init();
+
+#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
+   curr = input;
+   end = input + len;
+   while (curr < end) {
+   chunk = end - curr;
+   if (chunk > chunk_sz)
+   chunk = chunk_sz;
+   MD5Update(, curr, chunk);
+   curr += chunk;
+   schedule();
+   }
+#else
+   MD5Update(, input, len);
+#endif
+
+   MD5Final(output, );
+}
diff --git a/lib/mbedtls/sha1.c b/lib/mbedtls/sha1.c
new file mode 100644
index 000..3d863b2af7f
--- /dev/null
+++ b/lib/mbedtls/sha1.c
@@ -0,0 +1,111 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Hash shim layer on MbedTLS Crypto library
+ *
+ * Copyright (c) 2023 Linaro Limited
+ * Author: Raymond Mao 
+ */
+#ifndef USE_HOSTCC
+#include 
+#endif /* USE_HOSTCC */
+#include 
+#include 
+
+const u8 sha1_der_prefix[SHA1_DER_LEN] = {
+   0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e,
+   0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14
+};
+
+void sha1_starts(sha1_context *ctx)
+{
+   mbedtls_sha1_init(ctx);
+   mbedtls_sha1_starts(ctx);
+}
+
+void sha1_update(sha1_context *ctx, const unsigned char *input,
+unsigned int length)
+{
+   mbedtls_sha1_update(ctx, input, length);
+}
+
+void sha1_finish(sha1_context *ctx, unsigned char output[SHA1_SUM_LEN])
+{
+   mbedtls_sha1_finish(ctx, output);
+   mbedtls_sha1_free(ctx);
+}
+
+void sha1_csum(const unsigned char *input, unsigned int ilen,
+  unsigned char *output)
+{
+   sha1_context ctx;
+
+   sha1_starts();
+   sha1_update(, input, ilen);
+   sha1_finish(, output);
+}
+
+void sha1_csum_wd(const unsigned char *input, unsigned int ilen,
+ unsigned char *output, unsigned int chunk_sz)
+{
+   sha1_context ctx;
+#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
+   const unsigned char *end, *curr;
+   int chunk;
+#endif
+
+   sha1_starts();
+
+#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
+   curr = 

[PATCH v3 05/25] md5: Remove md5 non-watchdog API

2024-05-28 Thread Raymond Mao
We don't need an API specially for non-watchdog since md5_wd supports
it by enabling CONFIG_HW_WATCHDOG or CONFIG_WATCHDOG.
Set 0x1 as default chunk size for MD5.

Signed-off-by: Raymond Mao 
---
Changes in v3
- Initial patch.

 board/friendlyarm/nanopi2/board.c |  3 ++-
 board/intel/edison/edison.c   |  3 ++-
 board/xilinx/zynq/bootimg.c   |  2 +-
 include/u-boot/md5.h  |  7 +--
 lib/md5.c | 15 ---
 5 files changed, 6 insertions(+), 24 deletions(-)

diff --git a/board/friendlyarm/nanopi2/board.c 
b/board/friendlyarm/nanopi2/board.c
index 393c5a447d6..f203499f544 100644
--- a/board/friendlyarm/nanopi2/board.c
+++ b/board/friendlyarm/nanopi2/board.c
@@ -264,7 +264,8 @@ static void make_ether_addr(u8 *addr)
hash[6] = readl(PHY_BASEADDR_ECID + 0x08);
hash[7] = readl(PHY_BASEADDR_ECID + 0x0c);
 
-   md5((unsigned char *)[4], 64, (unsigned char *)hash);
+   md5_wd((unsigned char *)[4], 64, (unsigned char *)hash,
+  MD5_DEF_CHUNK_SZ);
 
hash[0] ^= hash[2];
hash[1] ^= hash[3];
diff --git a/board/intel/edison/edison.c b/board/intel/edison/edison.c
index 11e7f74e47c..1583d11c74d 100644
--- a/board/intel/edison/edison.c
+++ b/board/intel/edison/edison.c
@@ -33,7 +33,8 @@ static void assign_serial(void)
if (!mmc)
return;
 
-   md5((unsigned char *)mmc->cid, sizeof(mmc->cid), ssn);
+   md5_wd((unsigned char *)mmc->cid, sizeof(mmc->cid), ssn,
+  MD5_DEF_CHUNK_SZ);
 
snprintf(usb0addr, sizeof(usb0addr), "02:00:86:%02x:%02x:%02x",
 ssn[13], ssn[14], ssn[15]);
diff --git a/board/xilinx/zynq/bootimg.c b/board/xilinx/zynq/bootimg.c
index 2f55078dd76..4b2245c0618 100644
--- a/board/xilinx/zynq/bootimg.c
+++ b/board/xilinx/zynq/bootimg.c
@@ -136,7 +136,7 @@ int zynq_validate_partition(u32 start_addr, u32 len, u32 
chksum_off)
 
memcpy([0], (u32 *)chksum_off, MD5_CHECKSUM_SIZE);
 
-   md5_wd((u8 *)start_addr, len, [0], 0x1);
+   md5_wd((u8 *)start_addr, len, [0], MD5_DEF_CHUNK_SZ);
 
if (!memcmp(checksum, calchecksum, MD5_CHECKSUM_SIZE))
return 0;
diff --git a/include/u-boot/md5.h b/include/u-boot/md5.h
index 3cfd33a8e56..3cb87328175 100644
--- a/include/u-boot/md5.h
+++ b/include/u-boot/md5.h
@@ -12,6 +12,7 @@
 #include "compiler.h"
 
 #define MD5_SUM_LEN16
+#define MD5_DEF_CHUNK_SZ 0x1
 
 #if defined(CONFIG_MBEDTLS_LIB_CRYPTO)
 typedef mbedtls_md5_context MD5Context;
@@ -30,12 +31,6 @@ void MD5Init(MD5Context *ctx);
 void MD5Update(MD5Context *ctx, unsigned char const *buf, unsigned int len);
 void MD5Final(unsigned char digest[16], MD5Context *ctx);
 
-/*
- * Calculate and store in 'output' the MD5 digest of 'len' bytes at
- * 'input'. 'output' must have enough space to hold 16 bytes.
- */
-void md5 (unsigned char *input, int len, unsigned char output[16]);
-
 /*
  * Calculate and store in 'output' the MD5 digest of 'len' bytes at 'input'.
  * 'output' must have enough space to hold 16 bytes. If 'chunk' Trigger the
diff --git a/lib/md5.c b/lib/md5.c
index 34343cf8e23..2d8977b2e85 100644
--- a/lib/md5.c
+++ b/lib/md5.c
@@ -262,21 +262,6 @@ MD5Transform(__u32 buf[4], __u32 const in[16])
buf[3] += d;
 }
 
-/*
- * Calculate and store in 'output' the MD5 digest of 'len' bytes at
- * 'input'. 'output' must have enough space to hold 16 bytes.
- */
-void
-md5 (unsigned char *input, int len, unsigned char output[16])
-{
-   MD5Context context;
-
-   MD5Init();
-   MD5Update(, input, len);
-   MD5Final(output, );
-}
-
-
 /*
  * Calculate and store in 'output' the MD5 digest of 'len' bytes at 'input'.
  * 'output' must have enough space to hold 16 bytes. If 'chunk' Trigger the
-- 
2.25.1



[PATCH v3 04/25] lib: Adapt digest header files to MbedTLS

2024-05-28 Thread Raymond Mao
Adapt digest header files to support both original libs and MbedTLS
by switching on/off MBEDTLS_LIB_CRYPTO

FIXME:
`IS_ENABLED` or `CONFIG_IS_ENABLED` is not applicable here, since
including  causes undefined reference on schedule()
with sandbox build.
As  includes  which enables
`CONFIG_HW_WATCHDOG` and `CONFIG_WATCHDOG` but no schedule() are
defined in sandbox build.
`#if defined(CONFIG_MBEDTLS_LIB_CRYPTO)` is a workaround.

Signed-off-by: Raymond Mao 
---
Changes in v2
- Initial patch.
Changes in v3
- Remove the changes that were done in previous clean-up patch set.

 include/u-boot/md5.h|  7 +++
 include/u-boot/sha1.h   | 21 -
 include/u-boot/sha256.h | 20 
 include/u-boot/sha512.h | 22 +++---
 lib/Makefile|  6 +-
 5 files changed, 71 insertions(+), 5 deletions(-)

diff --git a/include/u-boot/md5.h b/include/u-boot/md5.h
index c465925ea8d..3cfd33a8e56 100644
--- a/include/u-boot/md5.h
+++ b/include/u-boot/md5.h
@@ -6,10 +6,16 @@
 #ifndef _MD5_H
 #define _MD5_H
 
+#if defined(CONFIG_MBEDTLS_LIB_CRYPTO)
+#include 
+#endif
 #include "compiler.h"
 
 #define MD5_SUM_LEN16
 
+#if defined(CONFIG_MBEDTLS_LIB_CRYPTO)
+typedef mbedtls_md5_context MD5Context;
+#else
 typedef struct MD5Context {
__u32 buf[4];
__u32 bits[2];
@@ -18,6 +24,7 @@ typedef struct MD5Context {
__u32 in32[16];
};
 } MD5Context;
+#endif
 
 void MD5Init(MD5Context *ctx);
 void MD5Update(MD5Context *ctx, unsigned char const *buf, unsigned int len);
diff --git a/include/u-boot/sha1.h b/include/u-boot/sha1.h
index 09fee594d26..ee46fe947a0 100644
--- a/include/u-boot/sha1.h
+++ b/include/u-boot/sha1.h
@@ -14,6 +14,21 @@
 #ifndef _SHA1_H
 #define _SHA1_H
 
+#if defined(CONFIG_MBEDTLS_LIB_CRYPTO)
+/*
+ * FIXME:
+ * MbedTLS define the members of "mbedtls_sha256_context" as private,
+ * but "state" needs to be access by arch/arm/cpu/armv8/sha1_ce_glue.
+ * MBEDTLS_ALLOW_PRIVATE_ACCESS needs to be enabled to allow the external
+ * access.
+ * Directly including  is not allowed,
+ * since this will include  and break the sandbox test.
+ */
+#define MBEDTLS_ALLOW_PRIVATE_ACCESS
+
+#include 
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -24,6 +39,9 @@ extern "C" {
 
 extern const uint8_t sha1_der_prefix[];
 
+#if defined(CONFIG_MBEDTLS_LIB_CRYPTO)
+typedef mbedtls_sha1_context sha1_context;
+#else
 /**
  * \brief SHA-1 context structure
  */
@@ -34,13 +52,14 @@ typedef struct
 unsigned char buffer[64];  /*!< data block being processed */
 }
 sha1_context;
+#endif
 
 /**
  * \brief SHA-1 context setup
  *
  * \param ctx SHA-1 context to be initialized
  */
-void sha1_starts( sha1_context *ctx );
+void sha1_starts(sha1_context *ctx);
 
 /**
  * \brief SHA-1 process buffer
diff --git a/include/u-boot/sha256.h b/include/u-boot/sha256.h
index 9aa1251789a..e2b7fdd41c8 100644
--- a/include/u-boot/sha256.h
+++ b/include/u-boot/sha256.h
@@ -1,6 +1,22 @@
 #ifndef _SHA256_H
 #define _SHA256_H
 
+#if defined(CONFIG_MBEDTLS_LIB_CRYPTO)
+/*
+ * FIXME:
+ * MbedTLS define the members of "mbedtls_sha256_context" as private,
+ * but "state" needs to be access by arch/arm/cpu/armv8/sha256_ce_glue.
+ * MBEDTLS_ALLOW_PRIVATE_ACCESS needs to be enabled to allow the external
+ * access.
+ * Directly including  is not allowed,
+ * since this will include  and break the sandbox test.
+ */
+#define MBEDTLS_ALLOW_PRIVATE_ACCESS
+
+#include 
+#endif
+
+#define SHA224_SUM_LEN 28
 #define SHA256_SUM_LEN 32
 #define SHA256_DER_LEN 19
 
@@ -9,11 +25,15 @@ extern const uint8_t sha256_der_prefix[];
 /* Reset watchdog each time we process this many bytes */
 #define CHUNKSZ_SHA256 (64 * 1024)
 
+#if defined(CONFIG_MBEDTLS_LIB_CRYPTO)
+typedef mbedtls_sha256_context sha256_context;
+#else
 typedef struct {
uint32_t total[2];
uint32_t state[8];
uint8_t buffer[64];
 } sha256_context;
+#endif
 
 void sha256_starts(sha256_context * ctx);
 void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length);
diff --git a/include/u-boot/sha512.h b/include/u-boot/sha512.h
index 516729d7750..a0c0de89d60 100644
--- a/include/u-boot/sha512.h
+++ b/include/u-boot/sha512.h
@@ -1,6 +1,10 @@
 #ifndef _SHA512_H
 #define _SHA512_H
 
+#if defined(CONFIG_MBEDTLS_LIB_CRYPTO)
+#include 
+#endif
+
 #define SHA384_SUM_LEN  48
 #define SHA384_DER_LEN  19
 #define SHA512_SUM_LEN  64
@@ -10,11 +14,16 @@
 #define CHUNKSZ_SHA384 (16 * 1024)
 #define CHUNKSZ_SHA512 (16 * 1024)
 
+#if defined(CONFIG_MBEDTLS_LIB_CRYPTO)
+typedef mbedtls_sha512_context sha384_context;
+typedef mbedtls_sha512_context sha512_context;
+#else
 typedef struct {
uint64_t state[SHA512_SUM_LEN / 8];
uint64_t count[2];
uint8_t buf[SHA512_BLOCK_SIZE];
 } sha512_context;
+#endif
 
 extern const uint8_t sha512_der_prefix[];
 
@@ -27,12 +36,19 @@ void sha512_csum_wd(const unsigned char *input, unsigned 

[PATCH v3 02/25] mbedtls: Add script to update MbedTLS subtree

2024-05-28 Thread Raymond Mao
lib/mbedtls/update-mbedtls-subtree.sh is a wrapper of git subtree
commands.
Usage from U-Boot top directory, run:

$ ./lib/mbedtls/update-mbedtls-subtree.sh pull 
$ ./lib/mbedtls/update-mbedtls-subtree.sh pick 

Signed-off-by: Raymond Mao 
---
Changes in v2
- Initial patch.
Changes in v3
- None.

 lib/mbedtls/update-mbedtls-subtree.sh | 50 +++
 1 file changed, 50 insertions(+)
 create mode 100755 lib/mbedtls/update-mbedtls-subtree.sh

diff --git a/lib/mbedtls/update-mbedtls-subtree.sh 
b/lib/mbedtls/update-mbedtls-subtree.sh
new file mode 100755
index 000..f208e54a5af
--- /dev/null
+++ b/lib/mbedtls/update-mbedtls-subtree.sh
@@ -0,0 +1,50 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0+
+#
+# Copyright 2024 Linaro Ltd.
+#
+# Usage: from the top level U-Boot source tree, run:
+# $ ./lib/mbedtls/update-mbedtls-subtree.sh pull 
+# $ ./lib/mbedtls/update-mbedtls-subtree.sh pick 
+#
+# The script will pull changes from MbedTLS repo into U-Boot
+# as a subtree located as /lib/mbedtls/external/mbedtls sub-directory.
+# It will automatically create a squash/merge commit listing the commits
+# imported.
+
+set -e
+
+merge_commit_msg=$(cat << EOF
+Subtree merge tag '$2' of MbedTLS repo [1] into lib/mbedtls/external/mbedtls
+
+[1] https://github.com/Mbed-TLS/mbedtls.git
+EOF
+)
+
+remote_add_and_fetch() {
+if ! git remote get-url mbedtls_upstream 2>/dev/null
+then
+echo "Warning: Script automatically adds new git remote via:"
+echo "git remote add mbedtls_upstream \\"
+echo "https://github.com/Mbed-TLS/mbedtls.git;
+git remote add mbedtls_upstream \
+https://github.com/Mbed-TLS/mbedtls.git
+fi
+git fetch mbedtls_upstream master
+}
+
+if [ "$1" = "pull" ]
+then
+remote_add_and_fetch
+git subtree pull --prefix lib/mbedtls/external/mbedtls mbedtls_upstream \
+"$2" --squash -m "${merge_commit_msg}"
+elif [ "$1" = "pick" ]
+then
+remote_add_and_fetch
+git cherry-pick -x --strategy=subtree \
+-Xsubtree=lib/mbedtls/external/mbedtls/ "$2"
+else
+echo "usage: $0  "
+echo "   pull or pick"
+echo "  release tag [pull] or commit id [pick]"
+fi
-- 
2.25.1



[PATCH v3 01/25] CI: Exclude MbedTLS subtree for CONFIG checks

2024-05-28 Thread Raymond Mao
Since MbedTLS is an external repo with its own coding style,
exclude it from Azure and gitlab CI CONFIG checks.

Signed-off-by: Raymond Mao 
Reviewed-by: Tom Rini 
Reviewed-by: Ilias Apalodimas 
---
Changes in v2
- Initial patch.
Changes in v3
- None.

 .azure-pipelines.yml | 3 ++-
 .gitlab-ci.yml   | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/.azure-pipelines.yml b/.azure-pipelines.yml
index 27f69583c65..c8052771fa8 100644
--- a/.azure-pipelines.yml
+++ b/.azure-pipelines.yml
@@ -65,7 +65,8 @@ stages:
   # have no matches.
   - script: git grep -E '^#[[:blank:]]*(define|undef)[[:blank:]]*CONFIG_'
   :^doc/ :^arch/arm/dts/ :^scripts/kconfig/lkc.h
-  :^include/linux/kconfig.h :^tools/ :^dts/upstream/ &&
+  :^include/linux/kconfig.h :^tools/ :^dts/upstream/
+  :^lib/mbedtls/external :^lib/mbedtls/mbedtls_def_config.h &&
   exit 1 || exit 0
 
   - job: docs
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 165f765a833..a8f7f1940f3 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -156,7 +156,8 @@ check for new CONFIG symbols outside Kconfig:
 # have no matches.
 - git grep -E '^#[[:blank:]]*(define|undef)[[:blank:]]*CONFIG_'
 :^doc/ :^arch/arm/dts/ :^scripts/kconfig/lkc.h
-:^include/linux/kconfig.h :^tools/ :^dts/upstream/ &&
+:^include/linux/kconfig.h :^tools/ :^dts/upstream/
+:^lib/mbedtls/external :^lib/mbedtls/mbedtls_def_config.h &&
 exit 1 || exit 0
 
 # build documentation
-- 
2.25.1



[PATCH v3 00/25] [RFC] Integrate MbedTLS v3.6 LTS with U-Boot

2024-05-28 Thread Raymond Mao
Integrate MbedTLS v3.6 LTS (currently v3.6.0-RC1) with U-Boot.

Motivations:


1. MbedTLS is well maintained with LTS versions.
2. LWIP is integrated with MbedTLS and easily to enable HTTPS.
3. MbedTLS recently switched license back to GPLv2.

Prerequisite:
-

This patch series requires mbedtls git repo to be added as a
subtree to the main U-Boot repo via:
$ git subtree add --prefix lib/mbedtls/external/mbedtls \
  https://github.com/Mbed-TLS/mbedtls.git \
  v3.6.0 --squash
Moreover, due to the Windows-style files from mbedtls git repo,
we need to convert the CRLF endings to LF and do a commit manually:
$ git add --renormalize .
$ git commit

New Kconfig options:


`MBEDTLS_LIB` is for MbedTLS general switch.
`MBEDTLS_LIB_CRYPTO` is for replacing original digest and crypto libs with
MbedTLS.
`MBEDTLS_LIB_X509` is for replacing original X509, PKCS7, MSCode, ASN1,
and Pubkey parser with MbedTLS.
`MBEDTLS_LIB_TLS` is for SSL/TLS (Disabled until LWIP port for MbedTLS is
ready)
In this patch set, MBEDTLS_LIB, MBEDTLS_LIB_CRYPTO and MBEDTLS_LIB_X509
are by default enabled in qemu_arm64_defconfig for testing purpose.

Patches for external MbedTLS project:
-

Since U-Boot uses Microsoft Authentication Code to verify PE/COFFs
executables which is not supported by MbedTLS at the moment,
addtional patches for MbedTLS are created to adapt with the EFI loader: 
1. Decoding of Microsoft Authentication Code.
2. Decoding of PKCS#9 Authenticate Attributes.
3. Extending MbedTLS PKCS#7 lib to support multiple signer's certificates.
4. MbedTLS native test suites for PKCS#7 signer's info.

All above 4 patches (tagged with `mbedtls/external`) are submitted to
MbedTLS project and being reviewed, eventually they should be part of
MbedTLS LTS release.
But before that, please merge them into U-Boot, otherwise the building
will be broken when MBEDTLS_LIB_X509 is enabled. 

See below PR link for the reference:
https://github.com/Mbed-TLS/mbedtls/pull/9001

Miscellaneous:
--

Optimized MbedTLS library size by tailoring the config file
and disabling all unnecessary features for EFI loader.
>From v2, original libs (rsa, asn1_decoder, rsa_helper, md5, sha1, sha256,
sha512) are completely replaced when MbedTLS is enabled.
For v3, the size-growth is slightly reduced by refactoring Hash functions.

Target(QEMU arm64) size-growth when enabling MbedTLS:
v1: 6.03%
v2: 4.66%
v3: 4.55%

Please see the latest output of bloat-o-meter for the reference of the
size-growth on QEMU arm64 target [1].

Tests done:
---

EFI Secure Boot test (EFI variables loading and verifying, EFI signed image
verifying and booting) via U-Boot console.
EFI Secure Boot and Capsule sandbox test passed.

Known issues:
-

None.

[1]: bloat-o-meter output between disabling/enabling MbedTLS (QEMU arm64)
```
add/remove: 206/81 grow/shrink: 19/17 up/down: 55548/-17495 (38053)
Function old new   delta
mbedtls_internal_sha1_process  -4540   +4540
mbedtls_x509_crt_parse_der_internal-3072   +3072
mbedtls_internal_md5_process   -2928   +2928
mbedtls_internal_sha256_process-2052   +2052
mbedtls_pkcs7_parse_der-1608   +1608
mbedtls_rsa_private-1468   +1468
pkcs7_parse_message  3721648   +1276
mbedtls_mpi_div_mpi-1168   +1168
mbedtls_internal_sha512_process-1056   +1056
mbedtls_mpi_inv_mod-1000   +1000
mbedtls_x509_dn_gets   - 996+996
x509_populate_cert - 948+948
K  - 896+896
oid_x520_attr_type - 840+840
__udivti3  - 832+832
mbedtls_x509_parse_subject_alt_name- 724+724
mbedtls_rsa_deduce_primes  - 720+720
mbedtls_mpi_exp_mod- 668+668
mbedtls_rsa_rsaes_pkcs1_v15_decrypt- 652+652
pkcs7_get_signer_info  - 632+632
mbedtls_rsa_complete   - 624+624
mbedtls_rsa_validate_params- 608+608
mbedtls_mpi_core_exp_mod   - 560+560
mbedtls_sha512_finish  - 556+556
mscode_parse  28 580+552
mbedtls_x509_get_time  - 552+552
mbedtls_x509_get_name  - 516+516
mbedtls_sha256_finish  - 484+484
mbedtls_rsa_validate_crt   - 464+464
mbedtls_mpi_core_mla   - 

Re: [PATCH v4 2/5] lib/lz4: update LZ4 decompressor module

2024-05-28 Thread Gao Xiang




On 2024/5/28 21:28, Jonathan Liu wrote:

Hi Jianan,

Here are the LZ4 decompression times I got running "time unlz4 ..." on
Rock 4 SE with RK3399 CPU.

v2024.04: 1.329 seconds
v2024.04 with 26c7fdadcb7 ("lib/lz4: update LZ4 decompressor module")
reverted: 0.665 seconds
v2024.04 with your patch: 1.216 seconds

I managed to get better performance by optimizing it myself.
v2024.04 with my patch: 0.785 seconds

With my patch, it makes no difference if I use __builtin_memcpy or
memcpy in lz4.c and lz4_wrapper.c so I just left it using memcpy.
It is still slower than reverting the LZ4 update though.


I'm not sure what's the old version come from, but from the copyright
itself it seems some earlier 2015 version.  Note that there is no
absolute outperform between old version and new version, old version
may be lack of some necessary boundary check or lead to some msan
warning or something, like a new commit of year 2016:
https://github.com/lz4/lz4/commit/f094f531441140f10fd461ba769f49d10f5cd581


/* costs ~1%; silence an msan warning when offset == 0 */
/*
 * note : when partialDecoding, there is no guarantee that
 * at least 4 bytes remain available in output buffer
 */
if (!partialDecoding) {
assert(oend > op);
assert(oend - op >= 4);

LZ4_write32(op, (U32)offset);
}
For the example above, you could completely remove the line above if
you don't care about such warning, as it claims ~1% performance loss.

Also since no u-boot user uses in-place decompression and if
you memmove() doesn't behave well, you could update the
following line
/*
 * supports overlapping memory regions; only matters
 * for in-place decompression scenarios
 */
memmove(op, ip, length);
into memcpy() instead.

The new lz4 codebase relies on memcpy() / memmove() code optimization
more than old version, if memcpy() assembly doesn't generate well on
your platform, it might have some issue.

Thanks,
Gao Xiang



My patch:
--- a/lib/lz4.c
+++ b/lib/lz4.c
@@ -41,6 +41,16 @@ static FORCE_INLINE u16 LZ4_readLE16(const void *src)
 return get_unaligned_le16(src);
  }

+static FORCE_INLINE void LZ4_copy2(void *dst, const void *src)
+{
+ put_unaligned(get_unaligned((const u16 *)src), (u16 *)dst);
+}
+
+static FORCE_INLINE void LZ4_copy4(void *dst, const void *src)
+{
+ put_unaligned(get_unaligned((const u32 *)src), (u32 *)dst);
+}
+
  static FORCE_INLINE void LZ4_copy8(void *dst, const void *src)
  {
 put_unaligned(get_unaligned((const u64 *)src), (u64 *)dst);
@@ -215,7 +225,10 @@ static FORCE_INLINE int LZ4_decompress_generic(
&& likely((endOnInput ? ip < shortiend : 1) &
  (op <= shortoend))) {
 /* Copy the literals */
-   memcpy(op, ip, endOnInput ? 16 : 8);
+ LZ4_copy8(op, ip);
+ if (endOnInput)
+ LZ4_copy8(op + 8, ip + 8);
+
 op += length; ip += length;

 /*
@@ -234,9 +247,9 @@ static FORCE_INLINE int LZ4_decompress_generic(
 (offset >= 8) &&
 (dict == withPrefix64k || match >= lowPrefix)) {
 /* Copy the match. */
-   memcpy(op + 0, match + 0, 8);
-   memcpy(op + 8, match + 8, 8);
-   memcpy(op + 16, match + 16, 2);
+ LZ4_copy8(op, match);
+ LZ4_copy8(op + 8, match + 8);
+ LZ4_copy2(op + 16, match + 16);
 op += length + MINMATCH;
 /* Both stages worked, load the next token. */
 continue;
@@ -466,7 +479,7 @@ _copy_match:
 op[2] = match[2];
 op[3] = match[3];
 match += inc32table[offset];
-   memcpy(op + 4, match, 4);
+ LZ4_copy4(op + 4, match);
 match -= dec64table[offset];
 } else {
 LZ4_copy8(op, match);

Let me know if you have any further suggestions.

Thanks.

Regards,
Jonathan

On Sun, 26 May 2024 at 22:18, Jianan Huang  wrote:


Hi Jonathan,

Could you please try the following patch ? It replaces all memcpy() calls in 
lz4 with __builtin_memcpy().

diff --git a/lib/lz4.c b/lib/lz4.c
index d365dc727c..2afe31c1c3 100644
--- a/lib/lz4.c
+++ b/lib/lz4.c
@@ -34,6 +34,8 @@
  #include 
  #include 

+#define LZ4_memcpy(dst, src, size) __builtin_memcpy(dst, src, size)
+
  #define FORCE_INLINE inline __attribute__((always_inline))

  static FORCE_INLINE u16 

[PATCH 3/4] board: phytec: phycore_imx8mp: Make RAM size configuration fix

2024-05-28 Thread Teresa Remmet
We might not be able to always rely on the EEPROM introspection data.
So add a config option alternative which configures the RAM size
to a fix value.

We still try to read the EEPROM introspection data at this point.
So we can print the SoM information if available.

Signed-off-by: Teresa Remmet 
---
 board/phytec/phycore_imx8mp/Kconfig | 48 +
 board/phytec/phycore_imx8mp/spl.c   | 34 +++-
 2 files changed, 74 insertions(+), 8 deletions(-)

diff --git a/board/phytec/phycore_imx8mp/Kconfig 
b/board/phytec/phycore_imx8mp/Kconfig
index f846d10bad9e..5ede39abc52b 100644
--- a/board/phytec/phycore_imx8mp/Kconfig
+++ b/board/phytec/phycore_imx8mp/Kconfig
@@ -12,5 +12,53 @@ config SYS_CONFIG_NAME
 config IMX_CONFIG
default "board/phytec/phycore_imx8mp/imximage-8mp-sd.cfg"
 
+config PHYCORE_IMX8MP_RAM_SIZE_FIX
+   bool "Set phyCORE-i.MX8MP RAM size fix instead of detecting"
+   default false
+   help
+ RAM size is automatic being detected with the help of
+ the EEPROM introspection data. Set RAM size to a fix value
+ instead.
+
+choice
+   prompt "phyCORE-i.MX8MP RAM size"
+   depends on PHYCORE_IMX8MP_RAM_SIZE_FIX
+   default PHYCORE_IMX8MP_RAM_SIZE_2GB
+
+config PHYCORE_IMX8MP_RAM_SIZE_1GB
+   bool "1GB RAM"
+   help
+ Set RAM size fix to 1GB for phyCORE-i.MX8MP.
+ RAM frequency is configured independent.
+
+config PHYCORE_IMX8MP_RAM_SIZE_2GB
+   bool "2GB RAM"
+   help
+ Set RAM size fix to 2GB for phyCORE-i.MX8MP.
+ RAM frequency is configured independent.
+
+config PHYCORE_IMX8MP_RAM_SIZE_4GB
+   bool "4GB RAM"
+   help
+ Set RAM size fix to 4GB for phyCORE-i.MX8MP.
+ RAM frequency is configured independent.
+
+config PHYCORE_IMX8MP_RAM_SIZE_8GB
+   bool "8GB RAM"
+   select PHYCORE_IMX8MP_USE_2GHZ_RAM_TIMINGS
+   help
+ Set RAM size fix to 8GB for phyCORE-i.MX8MP.
+ Only 2GHz RAMs are supported.
+
+endchoice
+
+config PHYCORE_IMX8MP_USE_2GHZ_RAM_TIMINGS
+   bool "Use 2GHz RAM timings"
+   depends on PHYCORE_IMX8MP_RAM_SIZE_FIX
+   default false
+   help
+ Use fix 2GHz RAM timings for phyCORE-i.MX8MP instead of
+ 1.5GHz timings.
+
 source "board/phytec/common/Kconfig"
 endif
diff --git a/board/phytec/phycore_imx8mp/spl.c 
b/board/phytec/phycore_imx8mp/spl.c
index f24f09301f1e..8a0c456859e2 100644
--- a/board/phytec/phycore_imx8mp/spl.c
+++ b/board/phytec/phycore_imx8mp/spl.c
@@ -50,20 +50,38 @@ void spl_dram_init(void)
 
ret = phytec_eeprom_data_setup_fallback(NULL, 0, EEPROM_ADDR,
EEPROM_ADDR_FALLBACK);
-   if (ret)
+   if (ret && !IS_ENABLED(CONFIG_PHYCORE_IMX8MP_RAM_SIZE_FIX))
goto out;
 
ret = phytec_imx8m_detect(NULL);
if (!ret)
phytec_print_som_info(NULL);
 
-   u8 rev = phytec_get_rev(NULL);
-   u8 somtype = phytec_get_som_type(NULL);
-
-   if (rev != PHYTEC_EEPROM_INVAL && (rev >= 3 || (somtype == SOM_TYPE_PCL 
&& rev >= 1)))
-   use_2ghz_timings = true;
-
-   size = phytec_get_imx8m_ddr_size(NULL);
+   if (IS_ENABLED(CONFIG_PHYCORE_IMX8MP_RAM_SIZE_FIX)) {
+   if (IS_ENABLED(CONFIG_PHYCORE_IMX8MP_RAM_SIZE_1GB))
+   size = PHYTEC_IMX8MP_DDR_1GB;
+   else if (IS_ENABLED(CONFIG_PHYCORE_IMX8MP_RAM_SIZE_2GB))
+   size = PHYTEC_IMX8MP_DDR_2GB;
+   else if (IS_ENABLED(CONFIG_PHYCORE_IMX8MP_RAM_SIZE_4GB))
+   size = PHYTEC_IMX8MP_DDR_4GB;
+   else if (IS_ENABLED(CONFIG_PHYCORE_IMX8MP_RAM_SIZE_8GB))
+   size = PHYTEC_IMX8MP_DDR_8GB;
+   if (IS_ENABLED(CONFIG_PHYCORE_IMX8MP_USE_2GHZ_RAM_TIMINGS)) {
+   if (size == PHYTEC_IMX8MP_DDR_4GB)
+   size = PHYTEC_IMX8MP_DDR_4GB_2GHZ;
+   else
+   use_2ghz_timings = true;
+   }
+   } else {
+   u8 rev = phytec_get_rev(NULL);
+   u8 somtype = phytec_get_som_type(NULL);
+
+   if (rev != PHYTEC_EEPROM_INVAL &&
+   (rev >= 3 || (somtype == SOM_TYPE_PCL && rev >= 1)))
+   use_2ghz_timings = true;
+
+   size = phytec_get_imx8m_ddr_size(NULL);
+   }
 
switch (size) {
case PHYTEC_IMX8MP_DDR_1GB:
-- 
2.34.1



[PATCH 2/4] board: phytec: phycore_imx8mp: Add support for different RAM sizes

2024-05-28 Thread Teresa Remmet
Add support for different RAM sizes and speed grades on the
phyCORE-i.MX8MP.
Add support for 1GB 1.5GHz, 1GB 2GHz, 4GB 1.5GHz,
4GB 2GHz and 8GB 2GHz RAM.
The RAM size and speed grade is detected by the information
stored in the EEPROM on the SoM.

Co-developed-by: Benjamin Hahn 
Signed-off-by: Benjamin Hahn 
Co-developed-by: Yannic Moog 
Signed-off-by: Yannic Moog 
Co-developed-by: Yashwanth Varakala 
Signed-off-by: Yashwanth Varakala 
Signed-off-by: Teresa Remmet 
---
 board/phytec/phycore_imx8mp/lpddr4_timing.c  | 153 +++
 board/phytec/phycore_imx8mp/lpddr4_timing.h  |  16 ++
 board/phytec/phycore_imx8mp/phycore-imx8mp.c |  11 ++
 board/phytec/phycore_imx8mp/spl.c| 100 +---
 include/configs/phycore_imx8mp.h |   4 +-
 5 files changed, 224 insertions(+), 60 deletions(-)
 create mode 100644 board/phytec/phycore_imx8mp/lpddr4_timing.h

diff --git a/board/phytec/phycore_imx8mp/lpddr4_timing.c 
b/board/phytec/phycore_imx8mp/lpddr4_timing.c
index f2707b859606..9984b6c26013 100644
--- a/board/phytec/phycore_imx8mp/lpddr4_timing.c
+++ b/board/phytec/phycore_imx8mp/lpddr4_timing.c
@@ -1839,3 +1839,156 @@ struct dram_timing_info dram_timing = {
.ddrphy_pie_num = ARRAY_SIZE(ddr_phy_pie),
.fsp_table = { 3000, 400, 100, },
 };
+
+void set_dram_timings_2ghz_2gb(void)
+{
+   dram_timing.ddrc_cfg[3].val = 0x1323;
+   dram_timing.ddrc_cfg[4].val = 0x1e84800;
+   dram_timing.ddrc_cfg[5].val = 0x7a0118;
+   dram_timing.ddrc_cfg[8].val = 0xc00307a3;
+   dram_timing.ddrc_cfg[9].val = 0xc5;
+   dram_timing.ddrc_cfg[10].val = 0xf4003f;
+   dram_timing.ddrc_cfg[11].val = 0xf3;
+   dram_timing.ddrc_cfg[14].val = 0x2028222a;
+   dram_timing.ddrc_cfg[15].val = 0x8083f;
+   dram_timing.ddrc_cfg[16].val = 0xe0e000;
+   dram_timing.ddrc_cfg[17].val = 0x12040a12;
+   dram_timing.ddrc_cfg[18].val = 0x2050f0f;
+   dram_timing.ddrc_cfg[19].val = 0x1010009;
+   dram_timing.ddrc_cfg[20].val = 0x502;
+   dram_timing.ddrc_cfg[21].val = 0x20800;
+   dram_timing.ddrc_cfg[22].val = 0xe12;
+   dram_timing.ddrc_cfg[23].val = 0x120;
+   dram_timing.ddrc_cfg[24].val = 0xc80064;
+   dram_timing.ddrc_cfg[25].val = 0x3e8001e;
+   dram_timing.ddrc_cfg[26].val = 0x3207a12;
+   dram_timing.ddrc_cfg[28].val = 0x4a3820e;
+   dram_timing.ddrc_cfg[30].val = 0x230e;
+   dram_timing.ddrc_cfg[37].val = 0x799;
+   dram_timing.ddrc_cfg[38].val = 0x9141d1c;
+   dram_timing.ddrc_cfg[74].val = 0x302;
+   dram_timing.ddrc_cfg[83].val = 0x599;
+   dram_timing.ddrc_cfg[99].val = 0x302;
+   dram_timing.ddrc_cfg[108].val = 0x599;
+   dram_timing.ddrphy_cfg[66].val = 0x18;
+   dram_timing.ddrphy_cfg[75].val = 0x1e3;
+   dram_timing.ddrphy_cfg[77].val = 0x1e3;
+   dram_timing.ddrphy_cfg[79].val = 0x1e3;
+   dram_timing.ddrphy_cfg[145].val = 0x3e8;
+   dram_timing.fsp_msg[0].drate = 4000;
+   dram_timing.fsp_msg[0].fsp_cfg[1].val = 0xfa0;
+   dram_timing.fsp_msg[0].fsp_cfg[10].val = 0x3ff4;
+   dram_timing.fsp_msg[0].fsp_cfg[11].val = 0xf3;
+   dram_timing.fsp_msg[0].fsp_cfg[15].val = 0x3ff4;
+   dram_timing.fsp_msg[0].fsp_cfg[16].val = 0xf3;
+   dram_timing.fsp_msg[0].fsp_cfg[22].val = 0xf400;
+   dram_timing.fsp_msg[0].fsp_cfg[23].val = 0xf33f;
+   dram_timing.fsp_msg[0].fsp_cfg[28].val = 0xf400;
+   dram_timing.fsp_msg[0].fsp_cfg[29].val = 0xf33f;
+   dram_timing.fsp_msg[3].drate = 4000;
+   dram_timing.fsp_msg[3].fsp_cfg[1].val = 0xfa0;
+   dram_timing.fsp_msg[3].fsp_cfg[11].val = 0x3ff4;
+   dram_timing.fsp_msg[3].fsp_cfg[12].val = 0xf3;
+   dram_timing.fsp_msg[3].fsp_cfg[16].val = 0x3ff4;
+   dram_timing.fsp_msg[3].fsp_cfg[17].val = 0xf3;
+   dram_timing.fsp_msg[3].fsp_cfg[23].val = 0xf400;
+   dram_timing.fsp_msg[3].fsp_cfg[24].val = 0xf33f;
+   dram_timing.fsp_msg[3].fsp_cfg[29].val = 0xf400;
+   dram_timing.fsp_msg[3].fsp_cfg[30].val = 0xf33f;
+   dram_timing.ddrphy_pie[480].val = 0x465;
+   dram_timing.ddrphy_pie[481].val = 0xfa;
+   dram_timing.ddrphy_pie[482].val = 0x9c4;
+   dram_timing.fsp_table[0] = 4000;
+}
+
+void set_dram_timings_1_5ghz_1gb(void)
+{
+   dram_timing.ddrc_cfg[3].val = 0x1233;
+   dram_timing.ddrc_cfg[5].val = 0x5b0087;
+   dram_timing.ddrc_cfg[6].val = 0x61027f10;
+   dram_timing.ddrc_cfg[7].val = 0x7b0;
+   dram_timing.ddrc_cfg[11].val = 0xf3;
+   dram_timing.ddrc_cfg[23].val = 0x8d;
+   dram_timing.ddrc_cfg[45].val = 0xf070707;
+   dram_timing.ddrc_cfg[59].val = 0x1031;
+   dram_timing.ddrc_cfg[62].val = 0xc0012;
+   dram_timing.ddrc_cfg[77].val = 0x13;
+   dram_timing.ddrc_cfg[84].val = 0x1031;
+   dram_timing.ddrc_cfg[87].val = 0x30005;
+   dram_timing.ddrc_cfg[102].val = 0x5;
+   dram_timing.ddrphy_cfg[75].val = 0x1e3;
+   dram_timing.ddrphy_cfg[77].val = 0x1e3;
+   

[PATCH 4/4] board: phycore_imx8mp: enable setting 2GHz timings without RAM size

2024-05-28 Thread Teresa Remmet
From: Benjamin Hahn 

make it possible to set the RAM timing frequency statically independent
from the RAM size. Fixed RAM timing frequency can be used while the
RAM size is still determined by the EEPROM image.

Signed-off-by: Benjamin Hahn 
Signed-off-by: Teresa Remmet 
---
 board/phytec/phycore_imx8mp/Kconfig | 23 +--
 board/phytec/phycore_imx8mp/spl.c   | 12 ++--
 2 files changed, 31 insertions(+), 4 deletions(-)

diff --git a/board/phytec/phycore_imx8mp/Kconfig 
b/board/phytec/phycore_imx8mp/Kconfig
index 5ede39abc52b..bdf9e97beaa6 100644
--- a/board/phytec/phycore_imx8mp/Kconfig
+++ b/board/phytec/phycore_imx8mp/Kconfig
@@ -52,13 +52,32 @@ config PHYCORE_IMX8MP_RAM_SIZE_8GB
 
 endchoice
 
+config PHYCORE_IMX8MP_RAM_FREQ_FIX
+   bool "Set phyCORE-i.MX8MP RAM frequency fix instead of detecting"
+   default false
+   help
+ RAM frequency is automatic being detected with the help of
+ the EEPROM introspection data. Set RAM frequency to a fix value
+ instead.
+
+choice
+   prompt "phyCORE-i.MX8MP RAM frequency"
+   depends on PHYCORE_IMX8MP_RAM_FREQ_FIX
+   default PHYCORE_IMX8MP_USE_1_5GHZ_RAM_TIMINGS
+
 config PHYCORE_IMX8MP_USE_2GHZ_RAM_TIMINGS
bool "Use 2GHz RAM timings"
-   depends on PHYCORE_IMX8MP_RAM_SIZE_FIX
-   default false
help
  Use fix 2GHz RAM timings for phyCORE-i.MX8MP instead of
  1.5GHz timings.
 
+config PHYCORE_IMX8MP_USE_1_5GHZ_RAM_TIMINGS
+   depends on !PHYCORE_IMX8MP_RAM_SIZE_8GB
+   bool "Use 1.5GHz RAM timings"
+   help
+ Use fix 1.5GHz RAM timings for phyCORE-i.MX8MP instead of
+ 2GHz timings.
+endchoice
+
 source "board/phytec/common/Kconfig"
 endif
diff --git a/board/phytec/phycore_imx8mp/spl.c 
b/board/phytec/phycore_imx8mp/spl.c
index 8a0c456859e2..30964bc5e547 100644
--- a/board/phytec/phycore_imx8mp/spl.c
+++ b/board/phytec/phycore_imx8mp/spl.c
@@ -66,11 +66,21 @@ void spl_dram_init(void)
size = PHYTEC_IMX8MP_DDR_4GB;
else if (IS_ENABLED(CONFIG_PHYCORE_IMX8MP_RAM_SIZE_8GB))
size = PHYTEC_IMX8MP_DDR_8GB;
+   } else {
+   size = phytec_get_imx8m_ddr_size(NULL);
+   }
+
+   if (IS_ENABLED(CONFIG_PHYCORE_IMX8MP_RAM_FREQ_FIX)) {
if (IS_ENABLED(CONFIG_PHYCORE_IMX8MP_USE_2GHZ_RAM_TIMINGS)) {
if (size == PHYTEC_IMX8MP_DDR_4GB)
size = PHYTEC_IMX8MP_DDR_4GB_2GHZ;
else
use_2ghz_timings = true;
+   } else if 
(IS_ENABLED(CONFIG_PHYCORE_IMX8MP_USE_1_5GHZ_RAM_TIMINGS)) {
+   if (size == PHYTEC_IMX8MP_DDR_4GB_2GHZ)
+   size = PHYTEC_IMX8MP_DDR_4GB;
+   else
+   use_2ghz_timings = false;
}
} else {
u8 rev = phytec_get_rev(NULL);
@@ -79,8 +89,6 @@ void spl_dram_init(void)
if (rev != PHYTEC_EEPROM_INVAL &&
(rev >= 3 || (somtype == SOM_TYPE_PCL && rev >= 1)))
use_2ghz_timings = true;
-
-   size = phytec_get_imx8m_ddr_size(NULL);
}
 
switch (size) {
-- 
2.34.1



[PATCH 1/4] board: phytec: phycore-imx8mp: spl: Fix syle issue

2024-05-28 Thread Teresa Remmet
Use tabs instead of spaces.

Signed-off-by: Teresa Remmet 
---
 board/phytec/phycore_imx8mp/spl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/board/phytec/phycore_imx8mp/spl.c 
b/board/phytec/phycore_imx8mp/spl.c
index df158024654e..492ce3b7ca6e 100644
--- a/board/phytec/phycore_imx8mp/spl.c
+++ b/board/phytec/phycore_imx8mp/spl.c
@@ -25,8 +25,8 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-#define EEPROM_ADDR 0x51
-#define EEPROM_ADDR_FALLBACK0x59
+#define EEPROM_ADDR0x51
+#define EEPROM_ADDR_FALLBACK   0x59
 
 int spl_board_boot_device(enum boot_device boot_dev_spl)
 {
-- 
2.34.1



[PATCH 0/4] Support all phyCORE-i.MX8MP RAM variants

2024-05-28 Thread Teresa Remmet
Add support for several RAM sizes and speed grades for phyCORE-i.MX8MP.

We have support with this series for:
- 1GB 1.5GHz
- 1GB 2GHz
- 2GB 1.5GHz (was supported before)
- 2GB 2GHz (was supported before)
- 4GB 1.5GHz
- 4GB 2GHz
- 8GB 2GHz

RAM size and speed grade is detected and set using EEPROM data on default.
Alternative it is possible to set fix RAM size and speed over Kconfig.

Teresa

Benjamin Hahn (1):
  board: phycore_imx8mp: enable setting 2GHz timings without RAM size

Teresa Remmet (3):
  board: phytec: phycore-imx8mp: spl: Fix syle issue
  board: phytec: phycore_imx8mp: Add support for different RAM sizes
  board: phytec: phycore_imx8mp: Make RAM size configuration fix

 board/phytec/phycore_imx8mp/Kconfig  |  67 
 board/phytec/phycore_imx8mp/lpddr4_timing.c  | 153 +++
 board/phytec/phycore_imx8mp/lpddr4_timing.h  |  16 ++
 board/phytec/phycore_imx8mp/phycore-imx8mp.c |  11 ++
 board/phytec/phycore_imx8mp/spl.c| 136 +
 include/configs/phycore_imx8mp.h |   4 +-
 6 files changed, 322 insertions(+), 65 deletions(-)
 create mode 100644 board/phytec/phycore_imx8mp/lpddr4_timing.h

-- 
2.34.1



Re: [PATCH v4 2/5] lib/lz4: update LZ4 decompressor module

2024-05-28 Thread Jonathan Liu
Hi Jianan,

Here are the LZ4 decompression times I got running "time unlz4 ..." on
Rock 4 SE with RK3399 CPU.

v2024.04: 1.329 seconds
v2024.04 with 26c7fdadcb7 ("lib/lz4: update LZ4 decompressor module")
reverted: 0.665 seconds
v2024.04 with your patch: 1.216 seconds

I managed to get better performance by optimizing it myself.
v2024.04 with my patch: 0.785 seconds

With my patch, it makes no difference if I use __builtin_memcpy or
memcpy in lz4.c and lz4_wrapper.c so I just left it using memcpy.
It is still slower than reverting the LZ4 update though.

My patch:
--- a/lib/lz4.c
+++ b/lib/lz4.c
@@ -41,6 +41,16 @@ static FORCE_INLINE u16 LZ4_readLE16(const void *src)
return get_unaligned_le16(src);
 }

+static FORCE_INLINE void LZ4_copy2(void *dst, const void *src)
+{
+ put_unaligned(get_unaligned((const u16 *)src), (u16 *)dst);
+}
+
+static FORCE_INLINE void LZ4_copy4(void *dst, const void *src)
+{
+ put_unaligned(get_unaligned((const u32 *)src), (u32 *)dst);
+}
+
 static FORCE_INLINE void LZ4_copy8(void *dst, const void *src)
 {
put_unaligned(get_unaligned((const u64 *)src), (u64 *)dst);
@@ -215,7 +225,10 @@ static FORCE_INLINE int LZ4_decompress_generic(
   && likely((endOnInput ? ip < shortiend : 1) &
 (op <= shortoend))) {
/* Copy the literals */
-   memcpy(op, ip, endOnInput ? 16 : 8);
+ LZ4_copy8(op, ip);
+ if (endOnInput)
+ LZ4_copy8(op + 8, ip + 8);
+
op += length; ip += length;

/*
@@ -234,9 +247,9 @@ static FORCE_INLINE int LZ4_decompress_generic(
(offset >= 8) &&
(dict == withPrefix64k || match >= lowPrefix)) {
/* Copy the match. */
-   memcpy(op + 0, match + 0, 8);
-   memcpy(op + 8, match + 8, 8);
-   memcpy(op + 16, match + 16, 2);
+ LZ4_copy8(op, match);
+ LZ4_copy8(op + 8, match + 8);
+ LZ4_copy2(op + 16, match + 16);
op += length + MINMATCH;
/* Both stages worked, load the next token. */
continue;
@@ -466,7 +479,7 @@ _copy_match:
op[2] = match[2];
op[3] = match[3];
match += inc32table[offset];
-   memcpy(op + 4, match, 4);
+ LZ4_copy4(op + 4, match);
match -= dec64table[offset];
} else {
LZ4_copy8(op, match);

Let me know if you have any further suggestions.

Thanks.

Regards,
Jonathan

On Sun, 26 May 2024 at 22:18, Jianan Huang  wrote:
>
> Hi Jonathan,
>
> Could you please try the following patch ? It replaces all memcpy() calls in 
> lz4 with __builtin_memcpy().
>
> diff --git a/lib/lz4.c b/lib/lz4.c
> index d365dc727c..2afe31c1c3 100644
> --- a/lib/lz4.c
> +++ b/lib/lz4.c
> @@ -34,6 +34,8 @@
>  #include 
>  #include 
>
> +#define LZ4_memcpy(dst, src, size) __builtin_memcpy(dst, src, size)
> +
>  #define FORCE_INLINE inline __attribute__((always_inline))
>
>  static FORCE_INLINE u16 LZ4_readLE16(const void *src)
> @@ -215,7 +217,7 @@ static FORCE_INLINE int LZ4_decompress_generic(
> && likely((endOnInput ? ip < shortiend : 1) &
>   (op <= shortoend))) {
>   /* Copy the literals */
> - memcpy(op, ip, endOnInput ? 16 : 8);
> + LZ4_memcpy(op, ip, endOnInput ? 16 : 8);
>   op += length; ip += length;
>
>   /*
> @@ -234,9 +236,9 @@ static FORCE_INLINE int LZ4_decompress_generic(
>  (offset >= 8) &&
>  (dict == withPrefix64k || match >= lowPrefix)) {
>   /* Copy the match. */
> - memcpy(op + 0, match + 0, 8);
> - memcpy(op + 8, match + 8, 8);
> - memcpy(op + 16, match + 16, 2);
> + LZ4_memcpy(op + 0, match + 0, 8);
> + LZ4_memcpy(op + 8, match + 8, 8);
> + LZ4_memcpy(op + 16, match + 16, 2);
>   op += length + MINMATCH;
>   /* Both stages worked, load the next token. */
>   continue;
> @@ -416,7 +418,7 @@ _copy_match:
>   size_t const copySize = (size_t)(lowPrefix - match);
>   size_t const restSize = length - copySize;
>
> - memcpy(op, dictEnd - copySize, copySize);
> + LZ4_memcpy(op, dictEnd - copySize, copySize);
>   op += copySize;
>   if (restSize > (size_t)(op - lowPrefix)) {
>   /* overlap copy */
> @@ -426,7 +428,7 @@ _copy_match:
>   while (op < endOfMatch)
>   *op++ = *copyFrom++;
>   } else {
> - memcpy(op, lowPrefix, restSize);
> + LZ4_memcpy(op, lowPrefix, restSize);
>   op += restSize;
>   }
>   }
> @@ -452,7 +454,7 @@ _copy_match:
>   while (op < copyEnd)
>   *op++ = *match++;
>   } else {
> - memcpy(op, match, mlen);
> + LZ4_memcpy(op, match, mlen);
>   }
>   op = copyEnd;
>   if (op == oend)
> @@ -466,7 +468,7 @@ _copy_match:
>   op[2] = match[2];
>   op[3] = match[3];
>   

[PATCH v2 4/4] configs: j721s2_evm_*_defconfig: Enable OSPI configs

2024-05-28 Thread Manorit Chawdhry
Enable OSPI related configs to boot using OSPI

Reviewed-by: Apurva Nandan 
Signed-off-by: Manorit Chawdhry 
---
 configs/j721s2_evm_a72_defconfig | 3 +++
 configs/j721s2_evm_r5_defconfig  | 3 +++
 2 files changed, 6 insertions(+)

diff --git a/configs/j721s2_evm_a72_defconfig b/configs/j721s2_evm_a72_defconfig
index 5ed8d00662e3..326beedea411 100644
--- a/configs/j721s2_evm_a72_defconfig
+++ b/configs/j721s2_evm_a72_defconfig
@@ -51,6 +51,7 @@ CONFIG_SPL_FS_LOAD_PAYLOAD_NAME="u-boot.img"
 CONFIG_SPL_I2C=y
 CONFIG_SPL_DM_MAILBOX=y
 CONFIG_SPL_MTD=y
+CONFIG_SPL_MTD_SUPPORT=y
 CONFIG_SPL_DM_SPI_FLASH=y
 CONFIG_SPL_NOR_SUPPORT=y
 CONFIG_SPL_DM_RESET=y
@@ -59,6 +60,8 @@ CONFIG_SPL_RAM_SUPPORT=y
 CONFIG_SPL_RAM_DEVICE=y
 # CONFIG_SPL_SPI_FLASH_TINY is not set
 CONFIG_SPL_SPI_FLASH_SFDP_SUPPORT=y
+CONFIG_SF_DEFAULT_MODE=0
+CONFIG_SF_DEFAULT_SPEED=2500
 CONFIG_SPL_SPI_LOAD=y
 CONFIG_SYS_SPI_U_BOOT_OFFS=0x28
 CONFIG_SPL_THERMAL=y
diff --git a/configs/j721s2_evm_r5_defconfig b/configs/j721s2_evm_r5_defconfig
index 3c958cafbe8f..4039841488f9 100644
--- a/configs/j721s2_evm_r5_defconfig
+++ b/configs/j721s2_evm_r5_defconfig
@@ -57,6 +57,7 @@ CONFIG_SPL_FS_EXT4=y
 CONFIG_SPL_I2C=y
 CONFIG_SPL_DM_MAILBOX=y
 CONFIG_SPL_MTD=y
+CONFIG_SPL_MTD_SUPPORT=y
 CONFIG_SPL_DM_SPI_FLASH=y
 CONFIG_SPL_NOR_SUPPORT=y
 CONFIG_SPL_DM_RESET=y
@@ -66,6 +67,8 @@ CONFIG_SPL_RAM_DEVICE=y
 CONFIG_SPL_REMOTEPROC=y
 # CONFIG_SPL_SPI_FLASH_TINY is not set
 CONFIG_SPL_SPI_FLASH_SFDP_SUPPORT=y
+CONFIG_SF_DEFAULT_MODE=0
+CONFIG_SF_DEFAULT_SPEED=2500
 CONFIG_SPL_SPI_LOAD=y
 CONFIG_SYS_SPI_U_BOOT_OFFS=0x8
 CONFIG_SPL_THERMAL=y

-- 
2.43.2



[PATCH v2 0/4] Enable OSPI boot for j721s2

2024-05-28 Thread Manorit Chawdhry
The series enables ospi boot for j721s2.

Test logs: https://gist.github.com/manorit2001/6bb91885c608e3a8cb0267ab2c614781

Signed-off-by: Manorit Chawdhry 
---
Changes in v2:
- Rebase on top of -next
- Link to v1: 
https://lore.kernel.org/r/20240401-b4-upstream-j721s2-ospi-support-v1-0-20e4a0024...@ti.com

---
Manorit Chawdhry (3):
  arch: arm: dts: k3-j721s2-r5: Override ospi and fss for 32-bit mode
  arch: arm: dts: k3-j721s2-*-u-boot.dtsi: Enable the ospi0 node
  configs: j721s2_evm_*_defconfig: Enable OSPI configs

Pratyush Yadav (1):
  mtd: spi-nor-core: Do not start or end writes at odd address in DTR mode

 .../dts/k3-j721s2-common-proc-board-u-boot.dtsi|  4 +-
 arch/arm/dts/k3-j721s2-r5.dtsi | 13 +
 configs/j721s2_evm_a72_defconfig   |  3 ++
 configs/j721s2_evm_r5_defconfig|  3 ++
 drivers/mtd/spi/spi-nor-core.c | 59 --
 5 files changed, 77 insertions(+), 5 deletions(-)
---
base-commit: 377e91c162ab09ec20f96f966f380cb55c590edd
change-id: 20240322-b4-upstream-j721s2-ospi-support-d45dfaa926dc

Best regards,
-- 
Manorit Chawdhry 



[PATCH v2 3/4] arch: arm: dts: k3-j721s2-*-u-boot.dtsi: Enable the ospi0 node

2024-05-28 Thread Manorit Chawdhry
Enable ospi0 node for all boot stages

Reviewed-by: Apurva Nandan 
Signed-off-by: Manorit Chawdhry 
---
 arch/arm/dts/k3-j721s2-common-proc-board-u-boot.dtsi | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/arch/arm/dts/k3-j721s2-common-proc-board-u-boot.dtsi 
b/arch/arm/dts/k3-j721s2-common-proc-board-u-boot.dtsi
index 91a82b3b7ca6..54eb9b4072c7 100644
--- a/arch/arm/dts/k3-j721s2-common-proc-board-u-boot.dtsi
+++ b/arch/arm/dts/k3-j721s2-common-proc-board-u-boot.dtsi
@@ -102,7 +102,9 @@
 };
 
  {
-   status = "disabled";
+   flash@0 {
+   bootph-all;
+   };
 };
 
  {

-- 
2.43.2



[PATCH v2 2/4] arch: arm: dts: k3-j721s2-r5: Override ospi and fss for 32-bit mode

2024-05-28 Thread Manorit Chawdhry
R5 being a 32-bit processor can't understand the 64-bit mapping being
done in ospi node. Override the ospi node for 32-bit register ranges and
the fss node ( the parent node of ospi ) to map the ranges for the
updated child node correctly.

Reviewed-by: Apurva Nandan 
Signed-off-by: Manorit Chawdhry 
---
 arch/arm/dts/k3-j721s2-r5.dtsi | 13 +
 1 file changed, 13 insertions(+)

diff --git a/arch/arm/dts/k3-j721s2-r5.dtsi b/arch/arm/dts/k3-j721s2-r5.dtsi
index eb0df42583a3..dbea6b9d0113 100644
--- a/arch/arm/dts/k3-j721s2-r5.dtsi
+++ b/arch/arm/dts/k3-j721s2-r5.dtsi
@@ -79,3 +79,16 @@
 _udmap {
ti,sci = <_tifs>;
 };
+
+ {
+   reg = <0x0 0x4704 0x0 0x100>,
+ <0x0 0x5000 0x0 0x800>;
+};
+
+ {
+   /* fss node has 64 bit address regions mapped to it and since the ospi
+* nodes is being override, override the fss node ranges as well
+*/
+   ranges = <0x0 0x4700 0x0 0x4700 0x0 0x00068400>,
+<0x0 0x5000 0x0 0x5000 0x0 0x0800>;
+};

-- 
2.43.2



[PATCH v2 1/4] mtd: spi-nor-core: Do not start or end writes at odd address in DTR mode

2024-05-28 Thread Manorit Chawdhry
From: Pratyush Yadav 

On DTR capable flashes like Micron Xcella the writes cannot start or end
at an odd address in DTR mode. Extra 0xff bytes need to be prepended or
appended respectively to make sure both the start and end addresses are
even.

Signed-off-by: Pratyush Yadav 
Signed-off-by: Apurva Nandan 
Signed-off-by: Vignesh Raghavendra 
Tested-by: Jonathan Humphreys 
Signed-off-by: Manorit Chawdhry 
---
 drivers/mtd/spi/spi-nor-core.c | 59 +++---
 1 file changed, 55 insertions(+), 4 deletions(-)

diff --git a/drivers/mtd/spi/spi-nor-core.c b/drivers/mtd/spi/spi-nor-core.c
index 982dd251150d..aea611fef523 100644
--- a/drivers/mtd/spi/spi-nor-core.c
+++ b/drivers/mtd/spi/spi-nor-core.c
@@ -1804,11 +1804,62 @@ static int spi_nor_write(struct mtd_info *mtd, loff_t 
to, size_t len,
if (ret < 0)
return ret;
 #endif
+
write_enable(nor);
-   ret = nor->write(nor, addr, page_remain, buf + i);
-   if (ret < 0)
-   goto write_err;
-   written = ret;
+
+   /*
+* On DTR capable flashes like Micron Xcella the writes cannot
+* start or end at an odd address in DTR mode. So we need to
+* append or prepend extra 0xff bytes to make sure the start
+* address and end address are even.
+*/
+   if (spi_nor_protocol_is_dtr(nor->write_proto) &&
+   ((addr | page_remain) & 1)) {
+   u_char *tmp;
+   size_t extra_bytes = 0;
+
+   tmp = kmalloc(nor->page_size, 0);
+   if (!tmp) {
+   ret = -ENOMEM;
+   goto write_err;
+   }
+
+   /* Prepend a 0xff byte if the start address is odd. */
+   if (addr & 1) {
+   tmp[0] = 0xff;
+   memcpy(tmp + 1, buf + i, page_remain);
+   addr--;
+   page_remain++;
+   extra_bytes++;
+   } else {
+   memcpy(tmp, buf + i, page_remain);
+   }
+
+   /* Append a 0xff byte if the end address is odd. */
+   if ((addr + page_remain) & 1) {
+   tmp[page_remain + extra_bytes] = 0xff;
+   extra_bytes++;
+   page_remain++;
+   }
+
+   ret = nor->write(nor, addr, page_remain, tmp);
+
+   kfree(tmp);
+
+   if (ret < 0)
+   goto write_err;
+
+   /*
+* We write extra bytes but they are not part of the
+* original write.
+*/
+   written = ret - extra_bytes;
+   } else {
+   ret = nor->write(nor, addr, page_remain, buf + i);
+   if (ret < 0)
+   goto write_err;
+   written = ret;
+   }
 
ret = spi_nor_wait_till_ready(nor);
if (ret)

-- 
2.43.2



Re: [PATCH 2/2] arm: imx8mp-phycore: move to OF_UPSTREAM

2024-05-28 Thread Teresa Remmet
Am Dienstag, dem 28.05.2024 um 13:25 +0200 schrieb Yannic Moog:
> The PHYCORE_IMX8MP is used by the phyBOARD-Pollux. Migrate board to
> OF_UPSTREAM. Linux kernel device tree for the board can be used as
> is,
> corresponding U-Boot device tree files are removed. U-Boot tweaks are
> kept unchanged.
> 
> Signed-off-by: Yannic Moog 

Acked-by: Teresa Remmet 

> ---
>  arch/arm/dts/Makefile   |   1 -
>  arch/arm/dts/imx8mp-phyboard-pollux-rdk.dts | 361 --
> --
>  arch/arm/dts/imx8mp-phycore-som.dtsi    | 323 --
> ---
>  arch/arm/mach-imx/imx8m/Kconfig |   1 +
>  board/phytec/phycore_imx8mp/MAINTAINERS |   1 -
>  configs/phycore-imx8mp_defconfig    |   2 +-
>  6 files changed, 2 insertions(+), 687 deletions(-)
> 
> diff --git a/arch/arm/dts/Makefile b/arch/arm/dts/Makefile
> index 480f8ff569d..d09d0d84bec 100644
> --- a/arch/arm/dts/Makefile
> +++ b/arch/arm/dts/Makefile
> @@ -1033,7 +1033,6 @@ dtb-$(CONFIG_ARCH_IMX8M) += \
> imx8mp-dhcom-pdk3-overlay-rev100.dtbo \
> imx8mp-icore-mx8mp-edimm2.2.dtb \
> imx8mp-msc-sm2s.dtb \
> -   imx8mp-phyboard-pollux-rdk.dtb \
> imx8mq-pico-pi.dtb \
> imx8mq-kontron-pitx-imx8m.dtb \
> imx8mq-librem5-r4.dtb
> diff --git a/arch/arm/dts/imx8mp-phyboard-pollux-rdk.dts
> b/arch/arm/dts/imx8mp-phyboard-pollux-rdk.dts
> deleted file mode 100644
> index c8640cac3ed..000
> --- a/arch/arm/dts/imx8mp-phyboard-pollux-rdk.dts
> +++ /dev/null
> @@ -1,361 +0,0 @@
> -// SPDX-License-Identifier: GPL-2.0
> -/*
> - * Copyright (C) 2020 PHYTEC Messtechnik GmbH
> - * Author: Teresa Remmet 
> - */
> -
> -/dts-v1/;
> -
> -#include 
> -#include 
> -#include "imx8mp-phycore-som.dtsi"
> -
> -/ {
> -   model = "PHYTEC phyBOARD-Pollux i.MX8MP";
> -   compatible = "phytec,imx8mp-phyboard-pollux-rdk",
> -    "phytec,imx8mp-phycore-som", "fsl,imx8mp";
> -
> -   chosen {
> -   stdout-path = 
> -   };
> -
> -   reg_can1_stby: regulator-can1-stby {
> -   compatible = "regulator-fixed";
> -   pinctrl-names = "default";
> -   pinctrl-0 = <_flexcan1_reg>;
> -   gpio = < 20 GPIO_ACTIVE_LOW>;
> -   regulator-max-microvolt = <330>;
> -   regulator-min-microvolt = <330>;
> -   regulator-name = "can1-stby";
> -   };
> -
> -   reg_can2_stby: regulator-can2-stby {
> -   compatible = "regulator-fixed";
> -   pinctrl-names = "default";
> -   pinctrl-0 = <_flexcan2_reg>;
> -   gpio = < 21 GPIO_ACTIVE_LOW>;
> -   regulator-max-microvolt = <330>;
> -   regulator-min-microvolt = <330>;
> -   regulator-name = "can2-stby";
> -   };
> -
> -   reg_usb1_vbus: regulator-usb1-vbus {
> -   compatible = "regulator-fixed";
> -   pinctrl-names = "default";
> -   pinctrl-0 = <_usb1_vbus>;
> -   gpio = < 12 GPIO_ACTIVE_LOW>;
> -   regulator-max-microvolt = <500>;
> -   regulator-min-microvolt = <500>;
> -   regulator-name = "usb1_host_vbus";
> -   };
> -
> -   reg_usdhc2_vmmc: regulator-usdhc2 {
> -   compatible = "regulator-fixed";
> -   pinctrl-names = "default";
> -   pinctrl-0 = <_reg_usdhc2_vmmc>;
> -   regulator-name = "VSD_3V3";
> -   regulator-min-microvolt = <330>;
> -   regulator-max-microvolt = <330>;
> -   gpio = < 19 GPIO_ACTIVE_HIGH>;
> -   enable-active-high;
> -   startup-delay-us = <100>;
> -   off-on-delay-us = <12000>;
> -   };
> -};
> -
> - {
> -   pinctrl-names = "default";
> -   pinctrl-0 = <_eqos>;
> -   phy-mode = "rgmii-id";
> -   phy-handle = <>;
> -   status = "okay";
> -
> -   mdio {
> -   compatible = "snps,dwmac-mdio";
> -   #address-cells = <1>;
> -   #size-cells = <0>;
> -
> -   ethphy0: ethernet-phy@1 {
> -   compatible = "ethernet-phy-ieee802.3-c22";
> -   reg = <0x1>;
> -   ti,rx-internal-delay =
> ;
> -   ti,tx-internal-delay =
> ;
> -   ti,fifo-depth =
> ;
> -   ti,clk-output-sel = ;
> -   enet-phy-lane-no-swap;
> -   };
> -   };
> -};
> -
> -/* CAN FD */
> - {
> -   pinctrl-names = "default";
> -   pinctrl-0 = <_flexcan1>;
> -   xceiver-supply = <_can1_stby>;
> -   status = "okay";
> -};
> -
> - {
> -   pinctrl-names = "default";
> -   pinctrl-0 = <_flexcan2>;
> -   xceiver-supply = <_can2_stby>;
> -   status = "okay";
> -};
> -
> - {
> -   clock-frequency = <40>;
> -   pinctrl-names = "default", "gpio";
> -  

Re: [PATCH 2/2] arm: imx8mp-phycore: move to OF_UPSTREAM

2024-05-28 Thread Fabio Estevam
On Tue, May 28, 2024 at 8:25 AM Yannic Moog  wrote:
>
> The PHYCORE_IMX8MP is used by the phyBOARD-Pollux. Migrate board to
> OF_UPSTREAM. Linux kernel device tree for the board can be used as is,
> corresponding U-Boot device tree files are removed. U-Boot tweaks are
> kept unchanged.
>
> Signed-off-by: Yannic Moog 

Reviewed-by: Fabio Estevam 


Re: [PATCH 1/2] arm: imx8mm-phycore: move to OF_UPSTREAM

2024-05-28 Thread Fabio Estevam
Hi Yannic,

On Tue, May 28, 2024 at 8:25 AM Yannic Moog  wrote:
>
> The PHYCORE_IMX8MM is used by the phyBOARD-Polis and the
> phyGATE-Tauri-L. Migrate both boards to OF_UPSTREAM. Linux kernel device
> trees for both boards can be used as is, corresponding U-Boot device
> tree files are removed. U-Boot tweaks are kept unchanged.
>
> Signed-off-by: Yannic Moog 

Reviewed-by: Fabio Estevam 


[PATCH v2 1/1] andes: Use UCCTLCOMMAND instead of MCCTLCOMMAND

2024-05-28 Thread Leo Yu-Chi Liang
Use CSR_UCCTLCOMMAND instead of CSR_MCCTLCOMMAND
to do cache flush operation in M-mode and S-mode.

Signed-off-by: Leo Yu-Chi Liang 
---
 arch/riscv/cpu/andes/cache.c| 4 +---
 arch/riscv/include/asm/arch-andes/csr.h | 2 +-
 2 files changed, 2 insertions(+), 4 deletions(-)

diff --git a/arch/riscv/cpu/andes/cache.c b/arch/riscv/cpu/andes/cache.c
index 7d3df8722d..bb57498d75 100644
--- a/arch/riscv/cpu/andes/cache.c
+++ b/arch/riscv/cpu/andes/cache.c
@@ -43,9 +43,7 @@ static void cache_ops(int (*ops)(struct udevice *dev))
 
 void flush_dcache_all(void)
 {
-#if CONFIG_IS_ENABLED(RISCV_MMODE)
-   csr_write(CSR_MCCTLCOMMAND, CCTL_L1D_WBINVAL_ALL);
-#endif
+   csr_write(CSR_UCCTLCOMMAND, CCTL_L1D_WBINVAL_ALL);
 }
 
 void flush_dcache_range(unsigned long start, unsigned long end)
diff --git a/arch/riscv/include/asm/arch-andes/csr.h 
b/arch/riscv/include/asm/arch-andes/csr.h
index 028fd01c2f..7d6104a24e 100644
--- a/arch/riscv/include/asm/arch-andes/csr.h
+++ b/arch/riscv/include/asm/arch-andes/csr.h
@@ -12,7 +12,7 @@
 
 #define CSR_MCACHE_CTL 0x7ca
 #define CSR_MMISC_CTL 0x7d0
-#define CSR_MCCTLCOMMAND 0x7cc
+#define CSR_UCCTLCOMMAND 0x80c
 
 /* mcache_ctl register */
 
-- 
2.34.1



Re: [PATCH v1 0/5] board: toradex: Add new Verdin and Aquila SKUs

2024-05-28 Thread Fabio Estevam
Hi Francesco,

On Tue, May 28, 2024 at 9:39 AM Francesco Dolcini  wrote:

> I disagree on doing this on this specific series. The reason is that the
> changes on the tdx-cfg-block are depending one on each other and they
> have nothing nxp nor ti specific, they are toradex specific.
>
> If I misunderstood your request, how would you do it?
> Patches 1,2,4 and 5 are for Toradex NXP board variant, patch 3 is for a
> Toradex TI board, patches 4 and 5 depend on patch 3.

Thanks for the clarification. I will assign this series to me in patchwork then.


  1   2   >