From nobody Thu Dec 18 04:46:33 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=quarantine dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1745871237; cv=none; d=zohomail.com; s=zohoarc; b=knT7mXLzB4ua8xSzZsbT+pojUYN9vN8dvtBejJ7vZUQnUaeneXQyXEyC3MSEpGW5IHMGU3QnA+ttdEUtJOFofFVqWChiYKILdxyBwV/b3sSshgYTPwJORUUmb81YFaatr4Ce5MOgox9cRODJdpg+NZht/KtjVhVhaxhMTPC/YSk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1745871237; h=Content-Type:Cc:Cc:Date:Date:From:From:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=naSyCodnpZoPu0taqY3UIrfMHzOb099mbmKYmacUMaE=; b=kjAvdECBZThi9nc6qLihEYRuFU4/Jhgj8OU21X/A09e7j5ayvF+ws1k+4i0F3F7hNYRfieyN9xlZLmNufLKXnrKCVvRvMYKre40VZPJussnSQBQ1wSDZj/a0gJd4+HNV0byezaaZIjeEdGNoltumrhJRKRTwhKmQwEWJBMMK/F4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1745871237452944.6576626502726; Mon, 28 Apr 2025 13:13:57 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.971149.1359633 (Exim 4.92) (envelope-from ) id 1u9UrJ-0006ST-TG; Mon, 28 Apr 2025 20:13:45 +0000 Received: by outflank-mailman (output) from mailman id 971149.1359633; Mon, 28 Apr 2025 20:13:45 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1u9UrJ-0006SM-Oe; Mon, 28 Apr 2025 20:13:45 +0000 Received: by outflank-mailman (input) for mailman id 971149; Mon, 28 Apr 2025 20:13:44 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1u9UrI-0006SE-QT for xen-devel@lists.xenproject.org; Mon, 28 Apr 2025 20:13:44 +0000 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 4716fd2a-246d-11f0-9ffb-bf95429c2676; Mon, 28 Apr 2025 22:13:42 +0200 (CEST) Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by dfw.source.kernel.org (Postfix) with ESMTP id A763B5C04FC; Mon, 28 Apr 2025 20:11:23 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7CF61C4CEE4; Mon, 28 Apr 2025 20:13:39 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 4716fd2a-246d-11f0-9ffb-bf95429c2676 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1745871220; bh=0vOekQTbls2azYaGMx2HD/SDFkWASciwYaUBpMUArow=; h=Date:From:To:cc:Subject:From; b=tlhIYiDdym941cBD1oA5eVNvufHCt8x7cKmtQ4lSAF4EHvFgzP3Y0lIzdonBv4Z7P JiqDBzRuOeEm+Wpze6UoSBqlzjdkcqPI//oGQse4qEShlE3yqLjvQOWrwwuH/r0etR p8kCoPPtCEVWYeXhwhMDvUKp6wAdB4UK1MVi5iunwaVh/kDb947zxRaMYtM876LFoZ Ptfk5nf/h76sZuQft4GAbEEEpmX9R2gVi9cqXZYzWrh7khmc+Hpi5e5Q4YlvrhsbBG K3BzoY9II7dS+zroWt0LYkCEVroTJX5fq5icaMy4rTxCXknBD1N9ZEG6BFQ/Zg2peK JGSUmEJX4swaw== Date: Mon, 28 Apr 2025 13:13:38 -0700 (PDT) From: Stefano Stabellini X-X-Sender: sstabellini@ubuntu-linux-20-04-desktop To: xen-devel@lists.xenproject.org cc: sstabellini@kernel.org, andrei.cherechesu@nxp.com, dmukhin@ford.com, michal.orzel@amd.com, jason.andryuk@amd.com, S32@nxp.com Subject: [PATCH v8] uboot-script-gen: Dynamically compute addr and size when loading binaries Message-ID: User-Agent: Alpine 2.22 (DEB 394 2020-01-19) MIME-Version: 1.0 X-ZohoMail-DKIM: pass (identity @kernel.org) X-ZM-MESSAGEID: 1745871238686019000 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Andrei Cherechesu Normally, the Imagebuilder would precompute the sizes of the loaded binaries and addresses where they are loaded before generating the script, and the sizes and addresses that needed to be provided to Xen via /chosen would be hardcoded in the boot script. Added an option via "-s" parameter to avoid hardcoding any address in the boot script, and dynamically compute the loading addresses for binaries. The first loading address is based on the MEMORY_START parameter and after loading each binary, the loading address and the size of the binary are stored in variables with corresponding names. Then, the loading address for the next binary is computed and aligned to 0x200000. If the "-s" parameter is not used, the normal flow is executed, where the loading addresses and sizes for each binaries are precomputed and hardcoded inside the script, but the loading addresses and sizes for each binary are now also stored for eventual later use. Reserved memory regions are left TBD in the -s case. Link: https://lists.xenproject.org/archives/html/xen-devel/2022-09/msg01862= .html Signed-off-by: Andrei Cherechesu Signed-off-by: Stefano Stabellini Reviewed-by: Andrei Cherechesu Reviewed-by: Denis Mukhin --- Changes in v8: - remove quotes in heredoc - change memaddr to hex for readability Changes in v7: - use $() - better description and alphabetical order - use heredoc Changes in v6: - make initial ${memaddr} setting depending on CALC This patch adds quite a bit of complexity which is the reason why I didn't originally commit it. Now that we are enabling ImageBuilder in Yocto, it turns out this patch is required because Yocto invokes ImageBuilder before all the binaries are ready and available. Andrei, sorry for taking so long to realize why it is essential, but we are getting there now. The changes I made to the original patch are purely to make it simpler to maintain. --- README.md | 2 + scripts/uboot-script-gen | 181 ++++++++++++++++++++++++++++++++------- 2 files changed, 152 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index f8039ec..28c9e6b 100644 --- a/README.md +++ b/README.md @@ -356,6 +356,8 @@ Where:\ can only be used in combination with the -k option. This adds the public key into the dtb. Then one can add this dtb back into the u-boot bin or elf.\ +-s addresses and sizes are calculated dynamically from U-Boot, hence + binaries don't need to be available at the time of invocation.\ =20 ### Signed FIT images =20 diff --git a/scripts/uboot-script-gen b/scripts/uboot-script-gen index 638154a..dfa154e 100755 --- a/scripts/uboot-script-gen +++ b/scripts/uboot-script-gen @@ -1,8 +1,11 @@ #!/bin/bash =20 offset=3D$((2*1024*1024)) +PADDING_MASK=3D$(printf "0x%X\n" $(($offset - 1))) +PADDING_MASK_INV=3D$(printf "0x%X\n" $((~$PADDING_MASK))) filesize=3D0 prog_req=3D(mkimage file fdtput mktemp awk od) +CALC=3D"" =20 function cleanup_and_return_err() { @@ -100,17 +103,40 @@ function dt_set() fi } =20 +function dt_set_calc() +{ + local path=3D$1 + local var=3D$2 + local name_var=3D$3 +=20 + local addr_var=3D"$name_var"_addr + local size_var=3D"$name_var"_size + + cat >> $UBOOT_SOURCE <<- EOF + setexpr addr_hi \${$addr_var} / 0x100000000 + setexpr addr_lo \${$addr_var} \& 0xFFFFFFFF + setexpr size_hi \${$size_var} / 0x100000000 + setexpr size_lo \${$size_var} \& 0xFFFFFFFF + fdt set $path $var <0x\${addr_hi} 0x\${addr_lo} 0x\${size_hi} 0x\${size_l= o}> + EOF +} + function add_device_tree_kernel() { local path=3D$1 - local addr=3D$2 - local size=3D$3 - local bootargs=3D$4 + local name=3D$2 + local addr=3D$3 + local size=3D$4 + local bootargs=3D$5 local node_name=3D"module@${addr#0x}" =20 dt_mknode "$path" "$node_name" dt_set "$path/$node_name" "compatible" "str_a" "multiboot,kernel multi= boot,module" - dt_set "$path/$node_name" "reg" "hex" "$(split_addr_size $addr $size)" + if test "$CALC"; then + dt_set_calc "$path/$node_name" "reg" $name + else + dt_set "$path/$node_name" "reg" "hex" "$(split_addr_size $addr $s= ize)" + fi dt_set "$path/$node_name" "bootargs" "str" "$bootargs" } =20 @@ -118,26 +144,36 @@ function add_device_tree_kernel() function add_device_tree_ramdisk() { local path=3D$1 - local addr=3D$2 - local size=3D$3 + local name=3D$2 + local addr=3D$3 + local size=3D$4 local node_name=3D"module@${addr#0x}" =20 dt_mknode "$path" "$node_name" dt_set "$path/$node_name" "compatible" "str_a" "multiboot,ramdisk mult= iboot,module" - dt_set "$path/$node_name" "reg" "hex" "$(split_addr_size $addr $size)" + if test "$CALC"; then + dt_set_calc "$path/$node_name" "reg" $name + else + dt_set "$path/$node_name" "reg" "hex" "$(split_addr_size $addr $s= ize)" + fi } =20 =20 function add_device_tree_passthrough() { local path=3D$1 - local addr=3D$2 - local size=3D$3 + local name=3D$2 + local addr=3D$3 + local size=3D$4 local node_name=3D"module@${addr#0x}" =20 dt_mknode "$path" "$node_name" dt_set "$path/$node_name" "compatible" "str_a" "multiboot,device-tree = multiboot,module" - dt_set "$path/$node_name" "reg" "hex" "$(split_addr_size $addr $size)" + if test "$CALC"; then + dt_set_calc "$path/$node_name" "reg" $name + else + dt_set "$path/$node_name" "reg" "hex" "$(split_addr_size $addr $s= ize)" + fi } =20 function add_device_tree_mem() @@ -358,7 +394,11 @@ function xen_device_tree_editing() =20 dt_mknode "/chosen" "$node_name" dt_set "/chosen/$node_name" "compatible" "str_a" "xen,xsm-policy x= en,multiboot-module multiboot,module" - dt_set "/chosen/$node_name" "reg" "hex" "$(split_addr_size $xen_po= licy_addr $xen_policy_size)" + if test "$CALC"; then + dt_set_calc "/chosen/$node_name" "reg" "xen_policy" + else + dt_set "/chosen/$node_name" "reg" "hex" "$(split_addr_size $xe= n_policy_addr $xen_policy_size)" + fi fi =20 if test "$DOM0_KERNEL" @@ -367,7 +407,11 @@ function xen_device_tree_editing() =20 dt_mknode "/chosen" "$node_name" dt_set "/chosen/$node_name" "compatible" "str_a" "xen,linux-zimage= xen,multiboot-module multiboot,module" - dt_set "/chosen/$node_name" "reg" "hex" "$(split_addr_size $dom0_k= ernel_addr $dom0_kernel_size)" + if test "$CALC"; then + dt_set_calc "/chosen/$node_name" "reg" "dom0_linux" + else + dt_set "/chosen/$node_name" "reg" "hex" "$(split_addr_size $do= m0_kernel_addr $dom0_kernel_size)" + fi dt_set "/chosen" "xen,dom0-bootargs" "str" "$DOM0_CMD" fi =20 @@ -377,7 +421,11 @@ function xen_device_tree_editing() =20 dt_mknode "/chosen" "$node_name" dt_set "/chosen/$node_name" "compatible" "str_a" "xen,linux-initrd= xen,multiboot-module multiboot,module" - dt_set "/chosen/$node_name" "reg" "hex" "$(split_addr_size $ramdis= k_addr $ramdisk_size)" + if test "$CALC"; then + dt_set_calc "/chosen/$node_name" "reg" "dom0_ramdisk" + else + dt_set "/chosen/$node_name" "reg" "hex" "$(split_addr_size $ra= mdisk_addr $ramdisk_size)" + fi fi =20 i=3D0 @@ -464,14 +512,14 @@ function xen_device_tree_editing() =20 xen_dt_domu_add_vcpu_nodes "/chosen/domU$i" $i ${DOMU_VCPUS[$i]} =20 - add_device_tree_kernel "/chosen/domU$i" ${domU_kernel_addr[$i]} ${= domU_kernel_size[$i]} "${DOMU_CMD[$i]}" + add_device_tree_kernel "/chosen/domU$i" "domU${i}_kernel" ${domU_k= ernel_addr[$i]} ${domU_kernel_size[$i]} "${DOMU_CMD[$i]}" if test "${domU_ramdisk_addr[$i]}" then - add_device_tree_ramdisk "/chosen/domU$i" ${domU_ramdisk_addr[$= i]} ${domU_ramdisk_size[$i]} + add_device_tree_ramdisk "/chosen/domU$i" "domU${i}_ramdisk" ${= domU_ramdisk_addr[$i]} ${domU_ramdisk_size[$i]} fi if test "${domU_passthrough_dtb_addr[$i]}" then - add_device_tree_passthrough "/chosen/domU$i" ${domU_passthroug= h_dtb_addr[$i]} ${domU_passthrough_dtb_size[$i]} + add_device_tree_passthrough "/chosen/domU$i" "domU${i}_fdt" ${= domU_passthrough_dtb_addr[$i]} ${domU_passthrough_dtb_size[$i]} fi i=3D$(( $i + 1 )) done @@ -504,7 +552,11 @@ function device_tree_editing() =20 if test $UBOOT_SOURCE then - echo "fdt addr $device_tree_addr" >> $UBOOT_SOURCE + if test "$CALC"; then + echo "fdt addr \${host_fdt_addr}" >> $UBOOT_SOURCE + else + echo "fdt addr $device_tree_addr" >> $UBOOT_SOURCE + fi echo "fdt resize 1024" >> $UBOOT_SOURCE =20 if test $NUM_DT_OVERLAY && test $NUM_DT_OVERLAY -gt 0 @@ -512,7 +564,11 @@ function device_tree_editing() i=3D0 while test $i -lt $NUM_DT_OVERLAY do - echo "fdt apply ${dt_overlay_addr[$i]}" >> $UBOOT_SOURCE + if test "$CALC"; then + echo "fdt apply \${host_fdto${i}_addr}" >> $UBOOT_SOUR= CE + else + echo "fdt apply ${dt_overlay_addr[$i]}" >> $UBOOT_SOUR= CE + fi i=3D$(( $i + 1 )) done fi @@ -530,8 +586,12 @@ function fill_reserved_spaces_from_dtb() { if [ ! -f $DEVICE_TREE ] then - echo "File $DEVICE_TREE doesn't exist, exiting"; - cleanup_and_return_err + if test "$CALC"; then + return + else + echo "File $DEVICE_TREE doesn't exist, exiting"; + cleanup_and_return_err + fi fi =20 # Check if reserved-memory node exists @@ -613,7 +673,7 @@ function get_image_size() printf "%u" $effective_size } =20 -function add_size() +function add_size_from_file() { local filename=3D$1 local size=3D`stat -L --printf=3D"%s" $filename` @@ -645,6 +705,27 @@ function add_size() filesize=3D$size } =20 +function add_size_calculate() +{ + local fit_scr_name=3D$1 + + cat >> $UBOOT_SOURCE <<- EOF + setenv ${fit_scr_name}_addr \${memaddr} + setenv ${fit_scr_name}_size \${filesize} + setexpr memaddr \${memaddr} \+ \${filesize} + setexpr memaddr \${memaddr} \+ $PADDING_MASK + setexpr memaddr \${memaddr} \& $PADDING_MASK_INV + EOF + + # TODO: missing ${RESERVED_MEM_SPACES[@]} check + + # The following are updated to avoid collisions in node names, but + # they are not actively used. + memaddr=3D$((memaddr + offset)) + memaddr=3D$(printf "0x%X\n" $memaddr) + filesize=3D$offset +} + function load_file() { local filename=3D$1 @@ -657,10 +738,16 @@ function load_file() if test "$FIT" then echo "imxtract \$fit_addr $fit_scr_name $memaddr" >> $UBOOT_SOURCE + add_size_from_file $filename else - echo "$LOAD_CMD $memaddr ${prepend_path:+$prepend_path/}$relative_= path" >> $UBOOT_SOURCE + if test "$CALC"; then + echo "$LOAD_CMD \${memaddr} ${prepend_path:+$prepend_path/}$re= lative_path" >> $UBOOT_SOURCE + add_size_calculate $fit_scr_name + else + echo "$LOAD_CMD $memaddr ${prepend_path:+$prepend_path/}$relat= ive_path" >> $UBOOT_SOURCE + add_size_from_file $filename + fi fi - add_size $filename } =20 function check_file_type() @@ -670,8 +757,13 @@ function check_file_type() =20 if [ ! -f $filename ] then - echo "File $filename doesn't exist, exiting"; - cleanup_and_return_err + if test "$CALC" + then + return + else + echo "File $filename doesn't exist, exiting"; + cleanup_and_return_err + fi fi =20 # if file doesn't know what it is, it outputs data, so include that @@ -705,8 +797,13 @@ function check_compressed_file_type() =20 if [ ! -f $filename ] then - echo "File $filename doesn't exist, exiting"; - cleanup_and_return_err + if test "$CALC" + then + return + else + echo "File $filename doesn't exist, exiting"; + cleanup_and_return_err + fi fi =20 file_type=3D$( file -L $filename ) @@ -872,6 +969,12 @@ function linux_config() generate_uboot_images() { local arch=3D$(file -L $XEN | grep -E 'ARM64|Aarch64') + =20 + if test "$CALC" + then + echo "bootm is not compatible with -s" + cleanup_and_return_err + fi =20 if test "$arch" then @@ -997,7 +1100,11 @@ bitstream_load_and_config() if test "$UBOOT_SOURCE" then # we assume the FPGA device is 0 here - echo "fpga load 0 $bitstream_addr $bitstream_size" >> "$UBOOT_= SOURCE" + if test "$CALC"; then + echo "fpga load 0 \${fpga_bitstream_addr} \${fpga_bitstrea= m_size}" >> "$UBOOT_SOURCE" + else + echo "fpga load 0 $bitstream_addr $bitstream_size" >> "$UB= OOT_SOURCE" + fi fi fi } @@ -1271,7 +1378,7 @@ function print_help { script=3D`basename "$0"` echo "usage:" - echo " $script -c CONFIG_FILE -d DIRECTORY [-t LOAD_CMD] [-o FILE] [-k= KEY_DIR/HINT [-u U-BOOT_DTB]] [-e] [-f] [-p PREPEND_PATH]" + echo " $script -c CONFIG_FILE -d DIRECTORY [-t LOAD_CMD] [-o FILE] [-k= KEY_DIR/HINT [-u U-BOOT_DTB]] [-e] [-f] [-p PREPEND_PATH] [-s]" echo " $script -h" echo "where:" echo " CONFIG_FILE - configuration file" @@ -1289,13 +1396,14 @@ function print_help echo " -f - enable generating a FIT image" echo " PREPEND_PATH - path to be appended before file names to match d= eploy location within rootfs" echo " -h - prints out the help message and exits " + echo " -s - let U-Boot calculate binary images load addresses/sizes dy= namically" echo "Defaults:" echo " CONFIG_FILE=3D$cfg_file, UBOOT_TYPE=3D\"LOAD_CMD\" env var, DIR= ECTORY=3D$uboot_dir" echo "Example:" echo " $script -c ../config -d ./build42 -t \"scsi load 1:1\"" } =20 -while getopts ":c:t:d:ho:k:u:fp:" opt; do +while getopts ":c:t:d:ho:k:u:fp:s" opt; do case ${opt} in t ) case $OPTARG in @@ -1340,6 +1448,9 @@ while getopts ":c:t:d:ho:k:u:fp:" opt; do p ) prepend_path=3D"$OPTARG" ;; + s ) + CALC=3Dy + ;; h ) print_help exit 0 @@ -1533,6 +1644,10 @@ uboot_addr=3D$memaddr # 2MB are enough for a uboot script memaddr=3D$(( $memaddr + $offset )) memaddr=3D`printf "0x%X\n" $memaddr` +if test "$CALC" +then + echo "setenv memaddr $memaddr" >> $UBOOT_SOURCE +fi =20 fill_reserved_spaces_from_dtb =20 @@ -1583,7 +1698,11 @@ fi =20 if [ "$BOOT_CMD" !=3D "none" ] then - echo "$BOOT_CMD $kernel_addr $([ "$BOOT_CMD" =3D "bootefi" ] || echo '= -') $device_tree_addr" >> $UBOOT_SOURCE + if test "$CALC"; then + echo "$BOOT_CMD \${host_kernel_addr} $([ "$BOOT_CMD" =3D "bootefi"= ] || echo '-') \${host_fdt_addr}" >> $UBOOT_SOURCE + else + echo "$BOOT_CMD $kernel_addr $([ "$BOOT_CMD" =3D "bootefi" ] || ec= ho '-') $device_tree_addr" >> $UBOOT_SOURCE + fi else # skip boot command but store load addresses to be used later echo "setenv host_kernel_addr $kernel_addr" >> $UBOOT_SOURCE --=20 2.25.1