Skip to contentSkip to conversation

cmd: test: Add bitwise AND, document the feature

RFC PATCH In review Brad Mouring 2 participants 3 messages Apr 12, 2017 – Apr 17, 2017

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 cmd: test: Add bitwise AND, document the feature

Currently, there is not a good way to control script flow based on bitwise values. This initially led to NI adding the ability to read a specific bit from a port-mapped register to control bootflow.

Here, we add a more generic ability to test the bitwise AND of a value available to the grub scripting environment. This obviates the need for the inbit command (which is currently OOT).

---
 docs/grub.texi            |  2 ++
 grub-core/commands/test.c | 10 ++++++++++
 2 files changed, 12 insertions(+)
docs/grub.texi +2 -0
index 82f6fa4..f28063e 100644
@@ -5019,6 +5019,8 @@ the strings are not equal
 @var{integer1} is less than @var{integer2}
 @item @var{integer1} @code{-ne} @var{integer2}
 @var{integer1} is not equal to @var{integer2}
+@item @var{integer1} @code{-bwa} @var{integer2}
+Performs a bitwise AND between @var{integer1} and @var{integer2}
 @item @var{prefix}@var{integer1} @code{-pgt} @var{prefix}@var{integer2}
 @var{integer1} is greater than @var{integer2} after stripping off common non-numeric @var{prefix}.
 @item @var{prefix}@var{integer1} @code{-plt} @var{prefix}@var{integer2}
grub-core/commands/test.c +10 -0
index 5f06642..af4fad7 100644
@@ -290,6 +290,16 @@ test_parse (char **args, int *argn, int argc)
 	      continue;
 	    }
 
+	  /* GRUB extension: bitwise AND */
+	  if (grub_strcmp (args[*argn + 1], "-bwa") == 0)
+	    {
+	      update_val (grub_strtoul (args[*argn], 0, 0)
+			  & grub_strtoul (args[*argn + 2], 0, 0), &ctx);
+	      (*argn) += 3;
+	      continue;
+	    }
+
+
 	  /* -nt and -ot tests. GRUB extension: when doing -?t<bias> bias
 	     will be added to the first mtime. */
 	  if (grub_memcmp (args[*argn + 1], "-nt", 3) == 0
Signature · 1 line
2.9.0
#1 lore raw

12.04.2017 21:10, Brad Mouring пишет:

Quoted 8 lines

Currently, there is not a good way to control script flow based on bitwise values. This initially led to NI adding the ability to read a specific bit from a port-mapped register to control bootflow.

Here, we add a more generic ability to test the bitwise AND of a value available to the grub scripting environment. This obviates the need for the inbit command (which is currently OOT).

See recent discussion about arithmetic operations. This could also be

if $((a&b)); then
...
fi

I think it is more generic than single specific test.

Quoted 41 lines
---
 docs/grub.texi            |  2 ++
 grub-core/commands/test.c | 10 ++++++++++
 2 files changed, 12 insertions(+)
docs/grub.texi +2 -0
index 82f6fa4..f28063e 100644
@@ -5019,6 +5019,8 @@ the strings are not equal
 @var{integer1} is less than @var{integer2}
 @item @var{integer1} @code{-ne} @var{integer2}
 @var{integer1} is not equal to @var{integer2}
+@item @var{integer1} @code{-bwa} @var{integer2}
+Performs a bitwise AND between @var{integer1} and @var{integer2}
 @item @var{prefix}@var{integer1} @code{-pgt} @var{prefix}@var{integer2}
 @var{integer1} is greater than @var{integer2} after stripping off common non-numeric @var{prefix}.
 @item @var{prefix}@var{integer1} @code{-plt} @var{prefix}@var{integer2}
grub-core/commands/test.c +10 -0
index 5f06642..af4fad7 100644
@@ -290,6 +290,16 @@ test_parse (char **args, int *argn, int argc)
 	      continue;
 	    }
 
+	  /* GRUB extension: bitwise AND */
+	  if (grub_strcmp (args[*argn + 1], "-bwa") == 0)
+	    {
+	      update_val (grub_strtoul (args[*argn], 0, 0)
+			  & grub_strtoul (args[*argn + 2], 0, 0), &ctx);
+	      (*argn) += 3;
+	      continue;
+	    }
+
+
 	  /* -nt and -ot tests. GRUB extension: when doing -?t<bias> bias
 	     will be added to the first mtime. */
 	  if (grub_memcmp (args[*argn + 1], "-nt", 3) == 0
#2 lore raw
Quoted 10 lines · Andrei Borzenkov

12.04.2017 21:10, Brad Mouring пишет:

Quoted 8 lines

Currently, there is not a good way to control script flow based on bitwise values. This initially led to NI adding the ability to read a specific bit from a port-mapped register to control bootflow.

Here, we add a more generic ability to test the bitwise AND of a value available to the grub scripting environment. This obviates the need for the inbit command (which is currently OOT).

Quoted 7 lines

See recent discussion about arithmetic operations. This could also be

if $((a&b)); then
...
fi

I think it is more generic than single specific test.

I can appreciate the sentiment, but are you asking me to implement some/all of the functionality of the double-paren arith expansion, partially duplicating the functionality of the test/[ command, simply because (( a & b )) is more bash-ful than [ a -bwa b ]?

I'm not trying to bake-in any sort of assumption here, in fact, I'm just trying to get clarification.

Thanks,
Brad