[U-Boot] [PATCH] Avoid using gawk-specific strtonum()

We need to subtract two hex numbers. Avoid using strtonum() by doing the subtraction in bc with a suitable input base.
Signed-off-by: Simon Glass sjg@chromium.org Reported-by: Vasili Galka vvv444@gmail.com ---
Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile index 24d9687..bb2f615 100644 --- a/Makefile +++ b/Makefile @@ -788,7 +788,8 @@ OBJCOPYFLAGS_u-boot.bin := -O binary binary_size_check: u-boot.bin System.map FORCE @file_size=`stat -c %s u-boot.bin` ; \ map_size=$(shell cat System.map | \ - awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print strtonum("0x" end) - strtonum("0x" start)}'); \ + awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \ + | bc); \ if [ "" != "$$map_size" ]; then \ if test $$map_size -ne $$file_size; then \ echo "System.map shows a binary size of $$map_size" >&2 ; \

Dear Simon,
In message 1403071815-32055-1-git-send-email-sjg@chromium.org you wrote:
We need to subtract two hex numbers. Avoid using strtonum() by doing the subtraction in bc with a suitable input base.
Signed-off-by: Simon Glass sjg@chromium.org Reported-by: Vasili Galka vvv444@gmail.com
Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile index 24d9687..bb2f615 100644 --- a/Makefile +++ b/Makefile @@ -788,7 +788,8 @@ OBJCOPYFLAGS_u-boot.bin := -O binary binary_size_check: u-boot.bin System.map FORCE @file_size=`stat -c %s u-boot.bin` ; \ map_size=$(shell cat System.map | \
awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print strtonum("0x" end) - strtonum("0x" start)}'); \
awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \
| bc); \
I think instead of introducing yet another tool dependency this could be rewritten to use just bash:
awk '/_image_copy_start/ {start = $1} /_image_binary_end/ {end = $1} END { if (start != "" && end != "") print "printf %d $$((0x" end " - 0x" start "))" }' | bash
?
Best regards,
Wolfgang Denk

On Wed, Jun 18, 2014 at 12:01:12PM +0200, Wolfgang Denk wrote:
Dear Simon,
In message 1403071815-32055-1-git-send-email-sjg@chromium.org you wrote:
We need to subtract two hex numbers. Avoid using strtonum() by doing the subtraction in bc with a suitable input base.
Signed-off-by: Simon Glass sjg@chromium.org Reported-by: Vasili Galka vvv444@gmail.com
Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile index 24d9687..bb2f615 100644 --- a/Makefile +++ b/Makefile @@ -788,7 +788,8 @@ OBJCOPYFLAGS_u-boot.bin := -O binary binary_size_check: u-boot.bin System.map FORCE @file_size=`stat -c %s u-boot.bin` ; \ map_size=$(shell cat System.map | \
awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print strtonum("0x" end) - strtonum("0x" start)}'); \
awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \
| bc); \
I think instead of introducing yet another tool dependency this could be rewritten to use just bash:
awk '/_image_copy_start/ {start = $1} /_image_binary_end/ {end = $1} END { if (start != "" && end != "") print "printf %d $$((0x" end " - 0x" start "))" }' | bash
But now we're forcing bash. Jeroen, since you most often run into the "but now you've introduced a Linux'ism" problems, bash or bc or something else for a portable hex to decimal conversion? Or since so much of U-Boot is hex when talking numbers, maybe we should just make the echos say "... size of 0x$foo expected 0x$bar" ?

On wo, 2014-06-18 at 14:26 -0400, Tom Rini wrote:
On Wed, Jun 18, 2014 at 12:01:12PM +0200, Wolfgang Denk wrote:
Dear Simon,
In message 1403071815-32055-1-git-send-email-sjg@chromium.org you wrote:
We need to subtract two hex numbers. Avoid using strtonum() by doing the subtraction in bc with a suitable input base.
Signed-off-by: Simon Glass sjg@chromium.org Reported-by: Vasili Galka vvv444@gmail.com
Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile index 24d9687..bb2f615 100644 --- a/Makefile +++ b/Makefile @@ -788,7 +788,8 @@ OBJCOPYFLAGS_u-boot.bin := -O binary binary_size_check: u-boot.bin System.map FORCE @file_size=`stat -c %s u-boot.bin` ; \ map_size=$(shell cat System.map | \
awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print strtonum("0x" end) - strtonum("0x" start)}'); \
awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \
| bc); \
I think instead of introducing yet another tool dependency this could be rewritten to use just bash:
awk '/_image_copy_start/ {start = $1} /_image_binary_end/ {end = $1} END { if (start != "" && end != "") print "printf %d $$((0x" end " - 0x" start "))" }' | bash
But now we're forcing bash. Jeroen, since you most often run into the "but now you've introduced a Linux'ism" problems, bash or bc or something else for a portable hex to decimal conversion? Or since so much of U-Boot is hex when talking numbers, maybe we should just make the echos say "... size of 0x$foo expected 0x$bar" ?
I haven't checked but bc is part of POSIX standard, so it should be fine, social pressure demands I watch soccer ;). But anyway see: http://pubs.opengroup.org/onlinepubs/000095399/utilities/bc.html
Regards, Jeroen

Hi Tom,
On 18 June 2014 11:26, Tom Rini trini@ti.com wrote:
On Wed, Jun 18, 2014 at 12:01:12PM +0200, Wolfgang Denk wrote:
Dear Simon,
In message 1403071815-32055-1-git-send-email-sjg@chromium.org you wrote:
We need to subtract two hex numbers. Avoid using strtonum() by doing the subtraction in bc with a suitable input base.
Signed-off-by: Simon Glass sjg@chromium.org Reported-by: Vasili Galka vvv444@gmail.com
Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile index 24d9687..bb2f615 100644 --- a/Makefile +++ b/Makefile @@ -788,7 +788,8 @@ OBJCOPYFLAGS_u-boot.bin := -O binary binary_size_check: u-boot.bin System.map FORCE @file_size=`stat -c %s u-boot.bin` ; \ map_size=$(shell cat System.map | \
awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print strtonum("0x" end) - strtonum("0x" start)}'); \
awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \
| bc); \
I think instead of introducing yet another tool dependency this could be rewritten to use just bash:
awk '/_image_copy_start/ {start = $1} /_image_binary_end/ {end = $1} END { if (start != "" && end != "") print "printf %d $$((0x" end " - 0x" start "))" }' | bash
But now we're forcing bash. Jeroen, since you most often run into the "but now you've introduced a Linux'ism" problems, bash or bc or something else for a portable hex to decimal conversion? Or since so much of U-Boot is hex when talking numbers, maybe we should just make the echos say "... size of 0x$foo expected 0x$bar" ?
That's why I didn't use bash - I recall hitting that problem before but it might have been dtc rather than U-Boot. I thought bc might be safer. But let me know and I will respin.
Tom, the problem is that I need to subtract the two values to get an answer for the size.
Regards Simon

On Wed, Jun 18, 2014 at 12:14:14PM -0700, Simon Glass wrote:
Hi Tom,
On 18 June 2014 11:26, Tom Rini trini@ti.com wrote:
On Wed, Jun 18, 2014 at 12:01:12PM +0200, Wolfgang Denk wrote:
Dear Simon,
In message 1403071815-32055-1-git-send-email-sjg@chromium.org you wrote:
We need to subtract two hex numbers. Avoid using strtonum() by doing the subtraction in bc with a suitable input base.
Signed-off-by: Simon Glass sjg@chromium.org Reported-by: Vasili Galka vvv444@gmail.com
Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile index 24d9687..bb2f615 100644 --- a/Makefile +++ b/Makefile @@ -788,7 +788,8 @@ OBJCOPYFLAGS_u-boot.bin := -O binary binary_size_check: u-boot.bin System.map FORCE @file_size=`stat -c %s u-boot.bin` ; \ map_size=$(shell cat System.map | \
awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print strtonum("0x" end) - strtonum("0x" start)}'); \
awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \
| bc); \
I think instead of introducing yet another tool dependency this could be rewritten to use just bash:
awk '/_image_copy_start/ {start = $1} /_image_binary_end/ {end = $1} END { if (start != "" && end != "") print "printf %d $$((0x" end " - 0x" start "))" }' | bash
But now we're forcing bash. Jeroen, since you most often run into the "but now you've introduced a Linux'ism" problems, bash or bc or something else for a portable hex to decimal conversion? Or since so much of U-Boot is hex when talking numbers, maybe we should just make the echos say "... size of 0x$foo expected 0x$bar" ?
That's why I didn't use bash - I recall hitting that problem before but it might have been dtc rather than U-Boot. I thought bc might be safer. But let me know and I will respin.
Given Jeroen's answer, we'll keep to bc.

Tom Rini trini@ti.com writes:
On Wed, Jun 18, 2014 at 12:01:12PM +0200, Wolfgang Denk wrote:
Dear Simon,
In message 1403071815-32055-1-git-send-email-sjg@chromium.org you wrote:
We need to subtract two hex numbers. Avoid using strtonum() by doing the subtraction in bc with a suitable input base.
Signed-off-by: Simon Glass sjg@chromium.org Reported-by: Vasili Galka vvv444@gmail.com
Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile index 24d9687..bb2f615 100644 --- a/Makefile +++ b/Makefile @@ -788,7 +788,8 @@ OBJCOPYFLAGS_u-boot.bin := -O binary binary_size_check: u-boot.bin System.map FORCE @file_size=`stat -c %s u-boot.bin` ; \ map_size=$(shell cat System.map | \
awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print strtonum("0x" end) - strtonum("0x" start)}'); \
awk '/_image_copy_start/ {start = $$1} /_image_binary_end/ {end = $$1} END {if (start != "" && end != "") print "ibase=16; " toupper(end) " - " toupper(start)}' \
| bc); \
I think instead of introducing yet another tool dependency this could be rewritten to use just bash:
awk '/_image_copy_start/ {start = $1} /_image_binary_end/ {end = $1} END { if (start != "" && end != "") print "printf %d $$((0x" end " - 0x" start "))" }' | bash
But now we're forcing bash.
Arithmetic expansion including hex constants is POSIX shell: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag...

On Wed, Jun 18, 2014 at 12:10:15AM -0600, Simon Glass wrote:
We need to subtract two hex numbers. Avoid using strtonum() by doing the subtraction in bc with a suitable input base.
Signed-off-by: Simon Glass sjg@chromium.org Reported-by: Vasili Galka vvv444@gmail.com
Applied to u-boot/master, thanks!
participants (5)
-
Jeroen Hofstee
-
Måns Rullgård
-
Simon Glass
-
Tom Rini
-
Wolfgang Denk