diff mbox series

[ImageBuilder,v2,1/2] scripts/common: Introduce phandle_next and get_next_phandle()

Message ID 20220907110852.5673-1-michal.orzel@amd.com (mailing list archive)
State Accepted
Headers show
Series [ImageBuilder,v2,1/2] scripts/common: Introduce phandle_next and get_next_phandle() | expand

Commit Message

Michal Orzel Sept. 7, 2022, 11:08 a.m. UTC
When dealing with device trees, we need to have a solution to add
custom phandles to the nodes we create/modify. Add a global variable
phandle_next to act as a countdown counter starting with the highest
valid phandle value 0xfffffffe. Add a new function get_next_phandle
to get a value of the next available phandle and set it to a variable
whose name is passed as a first argument. The global counter will be
decremented with each call to this function.

Signed-off-by: Michal Orzel <michal.orzel@amd.com>
---
To make the interface to phandle_next as simple as possible, we just
need a single function that will get us the next phandle and update the
global counter. That is why we cannot use the following:
- "phandle=$(get_next_phandle)" as a subshell cannot modify the environment
  of its parent shell,
- making use of return statement as it only works with values up to 255

The current solution with passing a variable name to a function that
will modify its value using eval is the simplest one and serves our purpose.

Changes in v2:
- new patch
---
 scripts/common | 11 +++++++++++
 1 file changed, 11 insertions(+)

Comments

Stefano Stabellini Sept. 7, 2022, 11:39 p.m. UTC | #1
On Wed, 7 Sep 2022, Michal Orzel wrote:
> When dealing with device trees, we need to have a solution to add
> custom phandles to the nodes we create/modify. Add a global variable
> phandle_next to act as a countdown counter starting with the highest
> valid phandle value 0xfffffffe. Add a new function get_next_phandle
> to get a value of the next available phandle and set it to a variable
> whose name is passed as a first argument. The global counter will be
> decremented with each call to this function.
> 
> Signed-off-by: Michal Orzel <michal.orzel@amd.com>

Reviewed-by: Stefano Stabellini <sstabellini@kernel.org>


> ---
> To make the interface to phandle_next as simple as possible, we just
> need a single function that will get us the next phandle and update the
> global counter. That is why we cannot use the following:
> - "phandle=$(get_next_phandle)" as a subshell cannot modify the environment
>   of its parent shell,
> - making use of return statement as it only works with values up to 255
> 
> The current solution with passing a variable name to a function that
> will modify its value using eval is the simplest one and serves our purpose.

I love the trick



> Changes in v2:
> - new patch
> ---
>  scripts/common | 11 +++++++++++
>  1 file changed, 11 insertions(+)
> 
> diff --git a/scripts/common b/scripts/common
> index 68938beb8557..25c041270c29 100644
> --- a/scripts/common
> +++ b/scripts/common
> @@ -13,6 +13,9 @@
>  tmp_files=()
>  tmp_dirs=()
>  
> +# Highest valid phandle. Will be decremented with each call to get_next_phandle
> +phandle_next="0xfffffffe"
> +
>  function remove_tmp_files()
>  {
>      for i in "${tmp_files[@]}"
> @@ -26,6 +29,14 @@ function remove_tmp_files()
>      done
>  }
>  
> +# Get next phandle and set it as a value (in hex) of a variable whose name is
> +# passed as a first argument. Decrement global counter phandle_next.
> +function get_next_phandle()
> +{
> +    eval "$1=$(printf "0x%x" $phandle_next)"
> +    phandle_next=$(( $phandle_next - 1 ))
> +}
> +
>  function sanity_check_partial_dts()
>  {
>      local domU_passthrough_path="$1"
> -- 
> 2.25.1
>
diff mbox series

Patch

diff --git a/scripts/common b/scripts/common
index 68938beb8557..25c041270c29 100644
--- a/scripts/common
+++ b/scripts/common
@@ -13,6 +13,9 @@ 
 tmp_files=()
 tmp_dirs=()
 
+# Highest valid phandle. Will be decremented with each call to get_next_phandle
+phandle_next="0xfffffffe"
+
 function remove_tmp_files()
 {
     for i in "${tmp_files[@]}"
@@ -26,6 +29,14 @@  function remove_tmp_files()
     done
 }
 
+# Get next phandle and set it as a value (in hex) of a variable whose name is
+# passed as a first argument. Decrement global counter phandle_next.
+function get_next_phandle()
+{
+    eval "$1=$(printf "0x%x" $phandle_next)"
+    phandle_next=$(( $phandle_next - 1 ))
+}
+
 function sanity_check_partial_dts()
 {
     local domU_passthrough_path="$1"