Skip to contentSkip to conversation

uboot-script-gen: use size from arm64 Image header

PATCH Superseded Stewart Hildebrand 2 participants 4 messages Sep 12, 2023 – Sep 13, 2023

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 uboot-script-gen: use size from arm64 Image header

There is a corner case where the filesizes of the xen and Linux kernel images are not sufficient. These binaries likely contain NOLOAD sections (e.g. bss), which are not accounted in the filesize.

Check for the presence of an arm64 kernel image header, and get the effective image size from the header. Use the effective image size for calculating the next load address and for populating the size in the /chosen/dom*/reg property.

---
v1->v2:
* add in-code comments
* use variables more

---
 scripts/uboot-script-gen | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
scripts/uboot-script-gen +16 -1
index 9656a458ac00..f0972d983017 100755
@@ -2,7 +2,7 @@
 
 offset=$((2*1024*1024))
 filesize=0
-prog_req=(mkimage file fdtput mktemp awk)
+prog_req=(mkimage file fdtput mktemp awk od)
 
 function cleanup_and_return_err()
 {
@@ -435,6 +435,21 @@ function add_size()
 {
     local filename=$1
     local size=`stat -L --printf="%s" $filename`
+    # Read arm64 header magic (https://www.kernel.org/doc/Documentation/arm64/booting.txt)
+    local arm64_header_magic=$(od -j 56 -N 4 -t x4 ${filename} | head -n 1 | awk -F' ' '{ print $2 }')
+
+    # Check for valid arm64 header magic value 0x644d5241
+    if [ "${arm64_header_magic}" = "644d5241" ]
+    then
+        # Read effective size, which may be larger than the filesize due to noload sections, e.g. bss
+        local arm64_header_size=$(od -j 16 -N 8 -t u8 ${filename} | head -n 1 | awk -F' ' '{ print $2 }')
+
+        if [ "${arm64_header_size}" -gt "${size}" ]
+        then
+            size=${arm64_header_size}
+        fi
+    fi
+
     memaddr=$(( $memaddr + $size + $offset - 1))
     memaddr=$(( $memaddr & ~($offset - 1) ))
     memaddr=`printf "0x%X\n" $memaddr`
Signature · 1 line
2.42.0
Reviewed-by on v2 1/1
#1 lore raw

Hi Stewart,

Quoted 9 lines · Stewart Hildebrand

There is a corner case where the filesizes of the xen and Linux kernel images are not sufficient. These binaries likely contain NOLOAD sections (e.g. bss), which are not accounted in the filesize.

Check for the presence of an arm64 kernel image header, and get the effective image size from the header. Use the effective image size for calculating the next load address and for populating the size in the /chosen/dom*/reg property.

The patch works, thanks!

with one little suggestion...

Quoted 22 lines

---
v1->v2:
* add in-code comments
* use variables more

---
 scripts/uboot-script-gen | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
scripts/uboot-script-gen +1 -1
index 9656a458ac00..f0972d983017 100755
@@ -2,7 +2,7 @@
 
 offset=$((2*1024*1024))
 filesize=0
-prog_req=(mkimage file fdtput mktemp awk)
+prog_req=(mkimage file fdtput mktemp awk od)
 
 function cleanup_and_return_err()
 {

scripts/uboot-script-gen function add_size() · line 439

Quoted 5 lines
scripts/uboot-script-gen +2 -0
@@ -435,6 +435,21 @@ function add_size()
 {
     local filename=$1
     local size=`stat -L --printf="%s" $filename`
+    # Read arm64 header magic (https://www.kernel.org/doc/Documentation/arm64/booting.txt)
+    local arm64_header_magic=$(od -j 56 -N 4 -t x4 ${filename} | head -n 1 | awk -F' ' '{ print $2 }')

can we simplify:
head -n 1 | awk -F' ' '{ print $2 }'
by just doing:
awk 'NR==1 {print $2}'

~Michal

#2 lore raw
Quoted 36 lines · Michal Orzel

Hi Stewart,

Quoted 9 lines · Stewart Hildebrand

There is a corner case where the filesizes of the xen and Linux kernel images are not sufficient. These binaries likely contain NOLOAD sections (e.g. bss), which are not accounted in the filesize.

Check for the presence of an arm64 kernel image header, and get the effective image size from the header. Use the effective image size for calculating the next load address and for populating the size in the /chosen/dom*/reg property.

The patch works, thanks!

with one little suggestion...

Quoted 19 lines

---
v1->v2:
* add in-code comments
* use variables more

---
 scripts/uboot-script-gen | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
scripts/uboot-script-gen +1 -1
index 9656a458ac00..f0972d983017 100755
@@ -2,7 +2,7 @@
 
 offset=$((2*1024*1024))
 filesize=0
-prog_req=(mkimage file fdtput mktemp awk)
+prog_req=(mkimage file fdtput mktemp awk od)
 
Quoted 12 lines

scripts/uboot-script-gen function add_size() · line 439

Quoted 8 lines
scripts/uboot-script-gen +2 -0
 function cleanup_and_return_err()
 {
@@ -435,6 +435,21 @@ function add_size()
 {
     local filename=$1
     local size=`stat -L --printf="%s" $filename`
+    # Read arm64 header magic (https://www.kernel.org/doc/Documentation/arm64/booting.txt)
+    local arm64_header_magic=$(od -j 56 -N 4 -t x4 ${filename} | head -n 1 | awk -F' ' '{ print $2 }')

can we simplify:
head -n 1 | awk -F' ' '{ print $2 }'
by just doing:
awk 'NR==1 {print $2}'

Yes. I'll send v3.

Stew

#3 lore raw

Hi Stewart,

Quoted 50 lines · Stewart Hildebrand
Quoted 36 lines · Michal Orzel

Hi Stewart,

Quoted 9 lines · Stewart Hildebrand

There is a corner case where the filesizes of the xen and Linux kernel images are not sufficient. These binaries likely contain NOLOAD sections (e.g. bss), which are not accounted in the filesize.

Check for the presence of an arm64 kernel image header, and get the effective image size from the header. Use the effective image size for calculating the next load address and for populating the size in the /chosen/dom*/reg property.

The patch works, thanks!

with one little suggestion...

Quoted 19 lines

---
v1->v2:
* add in-code comments
* use variables more

---
 scripts/uboot-script-gen | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
scripts/uboot-script-gen +1 -1
index 9656a458ac00..f0972d983017 100755
@@ -2,7 +2,7 @@
 
 offset=$((2*1024*1024))
 filesize=0
-prog_req=(mkimage file fdtput mktemp awk)
+prog_req=(mkimage file fdtput mktemp awk od)
 
Quoted 12 lines

scripts/uboot-script-gen function add_size() · line 439

Quoted 8 lines
scripts/uboot-script-gen +2 -0
 function cleanup_and_return_err()
 {
@@ -435,6 +435,21 @@ function add_size()
 {
     local filename=$1
     local size=`stat -L --printf="%s" $filename`
+    # Read arm64 header magic (https://www.kernel.org/doc/Documentation/arm64/booting.txt)
+    local arm64_header_magic=$(od -j 56 -N 4 -t x4 ${filename} | head -n 1 | awk -F' ' '{ print $2 }')

can we simplify:
head -n 1 | awk -F' ' '{ print $2 }'
by just doing:
awk 'NR==1 {print $2}'

Quoted 1 line

Yes. I'll send v3.

I think it can be done on commit, so please check with Stefano.

~Michal