All of lore.kernel.org
 help / color / mirror / Atom feed
* [U-Boot] [PATCH v3 0/2] rockchip: make_fit_atf.py Eliminate pyelftools dependency
@ 2019-07-16 19:51 Chris Webb
  2019-07-16 19:52 ` [U-Boot] [PATCH v3 1/2] rockchip: make_fit_atf.py: " Chris Webb
  2019-07-16 19:53 ` [U-Boot] [PATCH v3 2/2] rockchip: Remove obsolete references to pyelftools Chris Webb
  0 siblings, 2 replies; 8+ messages in thread
From: Chris Webb @ 2019-07-16 19:51 UTC (permalink / raw)
  To: u-boot

Building for rockchip, make_fit_atf.py depends on pyelftools, a non-bundled
python module that is not used elsewhere in building u-boot or the kernel.

We only use pyelftools to pull out PT_LOAD segments. ELF is very simple, so
doing this manually is easy and spares users the extra dependency. In fact,
a straightforward implementation shrinks make_fit_aft.py rather than adding
complexity.

Andy Yan's patch

  https://gitlab.denx.de/u-boot/custodians/u-boot-rockchip/commit/619f002db864ef8caa30863bde62df5c651a7312

is a prerequisite for this. The new code does not erroneously count
GNU_STACK sections and therefore without the above patch, the loadables in
the conf section will always be one short, even where GNU_STACK is present.

I have tested this script with python 2 and 3 against all bl31 elf files at

  https://github.com/rockchip-linux/rkbin/tree/master/bin/rk33

as well as an rk3399 bl31.elf built locally from the master branch of

  https://github.com/ARM-software/arm-trusted-firmware

to which my toolchain added the extra GNU_STACK segment.

In each case, identical *.bin files were produced compared to the pyelftools
implementation, and a correct .its configuration (without off-by-one error)
was emitted, with and without dtbs supplied to the script.

Changes in v2:
  - no exception on a valid but completely empty bl31 ELF file
  - restrict documentation updates to the Rockchip docs (!)

Changes in v3:
  - code style: add correct whitespace around multiply operator

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [U-Boot] [PATCH v3 1/2] rockchip: make_fit_atf.py: Eliminate pyelftools dependency
  2019-07-16 19:51 [U-Boot] [PATCH v3 0/2] rockchip: make_fit_atf.py Eliminate pyelftools dependency Chris Webb
@ 2019-07-16 19:52 ` Chris Webb
  2019-07-17  1:08   ` Andy Yan
  2019-07-17 10:47   ` Kever Yang
  2019-07-16 19:53 ` [U-Boot] [PATCH v3 2/2] rockchip: Remove obsolete references to pyelftools Chris Webb
  1 sibling, 2 replies; 8+ messages in thread
From: Chris Webb @ 2019-07-16 19:52 UTC (permalink / raw)
  To: u-boot

make_fit_aft.py depends on the non-standard library pyelftools to pull
out PT_LOAD segments from ELF files. However, this is as easy to do
manually, without imposing the extra dependency on users.

Structures in the ELF file are unpacked into variables named to exactly
match the ELF spec to ensure the destructuring code is reasonably
self-documenting.

Signed-off-by: Chris Webb <chris@arachsys.com>
---
 arch/arm/mach-rockchip/make_fit_atf.py | 75 +++++++++++---------------
 1 file changed, 32 insertions(+), 43 deletions(-)

diff --git a/arch/arm/mach-rockchip/make_fit_atf.py b/arch/arm/mach-rockchip/make_fit_atf.py
index db0ae96ca8..b9a1988298 100755
--- a/arch/arm/mach-rockchip/make_fit_atf.py
+++ b/arch/arm/mach-rockchip/make_fit_atf.py
@@ -13,16 +13,7 @@ import os
 import sys
 import getopt
 import logging
-
-# pip install pyelftools
-from elftools.elf.elffile import ELFFile
-
-ELF_SEG_P_TYPE = 'p_type'
-ELF_SEG_P_PADDR = 'p_paddr'
-ELF_SEG_P_VADDR = 'p_vaddr'
-ELF_SEG_P_OFFSET = 'p_offset'
-ELF_SEG_P_FILESZ = 'p_filesz'
-ELF_SEG_P_MEMSZ = 'p_memsz'
+import struct
 
 DT_HEADER = """
 /*
@@ -118,33 +109,19 @@ def append_conf_node(file, dtbs, segments):
     file.write('\n')
 
 def generate_atf_fit_dts_uboot(fit_file, uboot_file_name):
-    num_load_seg = 0
-    p_paddr = 0xFFFFFFFF
-    with open(uboot_file_name, 'rb') as uboot_file:
-        uboot = ELFFile(uboot_file)
-        for i in range(uboot.num_segments()):
-            seg = uboot.get_segment(i)
-            if seg.__getitem__(ELF_SEG_P_TYPE) == 'PT_LOAD':
-                p_paddr = seg.__getitem__(ELF_SEG_P_PADDR)
-                num_load_seg = num_load_seg + 1
-
-    assert (p_paddr != 0xFFFFFFFF and num_load_seg == 1)
-
+    segments = unpack_elf(uboot_file_name)
+    if len(segments) != 1:
+        raise ValueError("Invalid u-boot ELF image '%s'" % uboot_file_name)
+    index, entry, p_paddr, data = segments[0]
     fit_file.write(DT_UBOOT % p_paddr)
 
 def generate_atf_fit_dts_bl31(fit_file, bl31_file_name, dtbs_file_name):
-    with open(bl31_file_name, 'rb') as bl31_file:
-        bl31 = ELFFile(bl31_file)
-        elf_entry = bl31.header['e_entry']
-        segments = bl31.num_segments()
-        for i in range(segments):
-            seg = bl31.get_segment(i)
-            if seg.__getitem__(ELF_SEG_P_TYPE) == 'PT_LOAD':
-                paddr = seg.__getitem__(ELF_SEG_P_PADDR)
-                append_bl31_node(fit_file, i + 1, paddr, elf_entry)
+    segments = unpack_elf(bl31_file_name)
+    for index, entry, paddr, data in segments:
+        append_bl31_node(fit_file, index + 1, paddr, entry)
     append_fdt_node(fit_file, dtbs_file_name)
     fit_file.write(DT_IMAGES_NODE_END)
-    append_conf_node(fit_file, dtbs_file_name, segments)
+    append_conf_node(fit_file, dtbs_file_name, len(segments))
 
 def generate_atf_fit_dts(fit_file_name, bl31_file_name, uboot_file_name, dtbs_file_name):
     # Generate FIT script for ATF image.
@@ -162,17 +139,29 @@ def generate_atf_fit_dts(fit_file_name, bl31_file_name, uboot_file_name, dtbs_fi
         fit_file.close()
 
 def generate_atf_binary(bl31_file_name):
-    with open(bl31_file_name, 'rb') as bl31_file:
-        bl31 = ELFFile(bl31_file)
-
-        num = bl31.num_segments()
-        for i in range(num):
-            seg = bl31.get_segment(i)
-            if seg.__getitem__(ELF_SEG_P_TYPE) == 'PT_LOAD':
-                paddr = seg.__getitem__(ELF_SEG_P_PADDR)
-                file_name = 'bl31_0x%08x.bin' % paddr
-                with open(file_name, "wb") as atf:
-                    atf.write(seg.data())
+    for index, entry, paddr, data in unpack_elf(bl31_file_name):
+        file_name = 'bl31_0x%08x.bin' % paddr
+        with open(file_name, "wb") as atf:
+            atf.write(data)
+
+def unpack_elf(filename):
+    with open(filename, 'rb') as file:
+        elf = file.read()
+    if elf[0:7] != b'\x7fELF\x02\x01\x01' or elf[18:20] != b'\xb7\x00':
+        raise ValueError("Invalid arm64 ELF file '%s'" % filename)
+
+    e_entry, e_phoff = struct.unpack_from('<2Q', elf, 0x18)
+    e_phentsize, e_phnum = struct.unpack_from('<2H', elf, 0x36)
+    segments = []
+
+    for index in range(e_phnum):
+        offset = e_phoff + e_phentsize * index
+        p_type, p_flags, p_offset = struct.unpack_from('<LLQ', elf, offset)
+        if p_type == 1: # PT_LOAD
+            p_paddr, p_filesz = struct.unpack_from('<2Q', elf, offset + 0x18)
+            p_data = elf[p_offset:p_offset + p_filesz]
+            segments.append((index, e_entry, p_paddr, p_data))
+    return segments
 
 def main():
     uboot_elf = "./u-boot"

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [U-Boot] [PATCH v3 2/2] rockchip: Remove obsolete references to pyelftools
  2019-07-16 19:51 [U-Boot] [PATCH v3 0/2] rockchip: make_fit_atf.py Eliminate pyelftools dependency Chris Webb
  2019-07-16 19:52 ` [U-Boot] [PATCH v3 1/2] rockchip: make_fit_atf.py: " Chris Webb
@ 2019-07-16 19:53 ` Chris Webb
  2019-07-17 10:39   ` Kever Yang
  1 sibling, 1 reply; 8+ messages in thread
From: Chris Webb @ 2019-07-16 19:53 UTC (permalink / raw)
  To: u-boot

make_fit_atf.py no longer requires pyelftools, and nothing else in the
rockchip build requires it either, so remove references to installing it
from the documentation.

Signed-off-by: Chris Webb <chris@arachsys.com>
---
 board/rockchip/evb_rk3399/README | 6 ------
 doc/README.rockchip              | 4 ----
 2 files changed, 10 deletions(-)

diff --git a/board/rockchip/evb_rk3399/README b/board/rockchip/evb_rk3399/README
index 6469821987..ea3258cf37 100644
--- a/board/rockchip/evb_rk3399/README
+++ b/board/rockchip/evb_rk3399/README
@@ -35,12 +35,6 @@ Get the Source and prebuild binary
   > git clone https://github.com/rockchip-linux/rkbin.git
   > git clone https://github.com/rockchip-linux/rkdeveloptool.git
 
-Get some prerequisites
-======================
-
-You need the Python elftools.elf.elffile library for make_fit_atf.py to work:
-
-  > sudo apt-get install python-pyelftools
 
 Compile ATF
 ===========
diff --git a/doc/README.rockchip b/doc/README.rockchip
index 02e2497b15..8ccbb87264 100644
--- a/doc/README.rockchip
+++ b/doc/README.rockchip
@@ -123,10 +123,6 @@ For example:
 
    Option 2: Package the image with SPL:
 
-   - We need the Python elftools.elf.elffile library for make_fit_atf.py to work
-
-     => sudo apt-get install python-pyelftools
-
    - Export cross compiler path for aarch64
 
    - Compile ATF

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [U-Boot] [PATCH v3 1/2] rockchip: make_fit_atf.py: Eliminate pyelftools dependency
  2019-07-16 19:52 ` [U-Boot] [PATCH v3 1/2] rockchip: make_fit_atf.py: " Chris Webb
@ 2019-07-17  1:08   ` Andy Yan
  2019-07-17 10:47   ` Kever Yang
  1 sibling, 0 replies; 8+ messages in thread
From: Andy Yan @ 2019-07-17  1:08 UTC (permalink / raw)
  To: u-boot

Hi Chris:

On 7/17/19 3:52 AM, Chris Webb wrote:
> make_fit_aft.py depends on the non-standard library pyelftools to pull
> out PT_LOAD segments from ELF files. However, this is as easy to do
> manually, without imposing the extra dependency on users.
>
> Structures in the ELF file are unpacked into variables named to exactly
> match the ELF spec to ensure the destructuring code is reasonably
> self-documenting.
>
> Signed-off-by: Chris Webb <chris@arachsys.com>


Reviewed-by: Andy Yan <andy.yan@rock-chips.com>

> ---
>   arch/arm/mach-rockchip/make_fit_atf.py | 75 +++++++++++---------------
>   1 file changed, 32 insertions(+), 43 deletions(-)
>
> diff --git a/arch/arm/mach-rockchip/make_fit_atf.py b/arch/arm/mach-rockchip/make_fit_atf.py
> index db0ae96ca8..b9a1988298 100755
> --- a/arch/arm/mach-rockchip/make_fit_atf.py
> +++ b/arch/arm/mach-rockchip/make_fit_atf.py
> @@ -13,16 +13,7 @@ import os
>   import sys
>   import getopt
>   import logging
> -
> -# pip install pyelftools
> -from elftools.elf.elffile import ELFFile
> -
> -ELF_SEG_P_TYPE = 'p_type'
> -ELF_SEG_P_PADDR = 'p_paddr'
> -ELF_SEG_P_VADDR = 'p_vaddr'
> -ELF_SEG_P_OFFSET = 'p_offset'
> -ELF_SEG_P_FILESZ = 'p_filesz'
> -ELF_SEG_P_MEMSZ = 'p_memsz'
> +import struct
>   
>   DT_HEADER = """
>   /*
> @@ -118,33 +109,19 @@ def append_conf_node(file, dtbs, segments):
>       file.write('\n')
>   
>   def generate_atf_fit_dts_uboot(fit_file, uboot_file_name):
> -    num_load_seg = 0
> -    p_paddr = 0xFFFFFFFF
> -    with open(uboot_file_name, 'rb') as uboot_file:
> -        uboot = ELFFile(uboot_file)
> -        for i in range(uboot.num_segments()):
> -            seg = uboot.get_segment(i)
> -            if seg.__getitem__(ELF_SEG_P_TYPE) == 'PT_LOAD':
> -                p_paddr = seg.__getitem__(ELF_SEG_P_PADDR)
> -                num_load_seg = num_load_seg + 1
> -
> -    assert (p_paddr != 0xFFFFFFFF and num_load_seg == 1)
> -
> +    segments = unpack_elf(uboot_file_name)
> +    if len(segments) != 1:
> +        raise ValueError("Invalid u-boot ELF image '%s'" % uboot_file_name)
> +    index, entry, p_paddr, data = segments[0]
>       fit_file.write(DT_UBOOT % p_paddr)
>   
>   def generate_atf_fit_dts_bl31(fit_file, bl31_file_name, dtbs_file_name):
> -    with open(bl31_file_name, 'rb') as bl31_file:
> -        bl31 = ELFFile(bl31_file)
> -        elf_entry = bl31.header['e_entry']
> -        segments = bl31.num_segments()
> -        for i in range(segments):
> -            seg = bl31.get_segment(i)
> -            if seg.__getitem__(ELF_SEG_P_TYPE) == 'PT_LOAD':
> -                paddr = seg.__getitem__(ELF_SEG_P_PADDR)
> -                append_bl31_node(fit_file, i + 1, paddr, elf_entry)
> +    segments = unpack_elf(bl31_file_name)
> +    for index, entry, paddr, data in segments:
> +        append_bl31_node(fit_file, index + 1, paddr, entry)
>       append_fdt_node(fit_file, dtbs_file_name)
>       fit_file.write(DT_IMAGES_NODE_END)
> -    append_conf_node(fit_file, dtbs_file_name, segments)
> +    append_conf_node(fit_file, dtbs_file_name, len(segments))
>   
>   def generate_atf_fit_dts(fit_file_name, bl31_file_name, uboot_file_name, dtbs_file_name):
>       # Generate FIT script for ATF image.
> @@ -162,17 +139,29 @@ def generate_atf_fit_dts(fit_file_name, bl31_file_name, uboot_file_name, dtbs_fi
>           fit_file.close()
>   
>   def generate_atf_binary(bl31_file_name):
> -    with open(bl31_file_name, 'rb') as bl31_file:
> -        bl31 = ELFFile(bl31_file)
> -
> -        num = bl31.num_segments()
> -        for i in range(num):
> -            seg = bl31.get_segment(i)
> -            if seg.__getitem__(ELF_SEG_P_TYPE) == 'PT_LOAD':
> -                paddr = seg.__getitem__(ELF_SEG_P_PADDR)
> -                file_name = 'bl31_0x%08x.bin' % paddr
> -                with open(file_name, "wb") as atf:
> -                    atf.write(seg.data())
> +    for index, entry, paddr, data in unpack_elf(bl31_file_name):
> +        file_name = 'bl31_0x%08x.bin' % paddr
> +        with open(file_name, "wb") as atf:
> +            atf.write(data)
> +
> +def unpack_elf(filename):
> +    with open(filename, 'rb') as file:
> +        elf = file.read()
> +    if elf[0:7] != b'\x7fELF\x02\x01\x01' or elf[18:20] != b'\xb7\x00':
> +        raise ValueError("Invalid arm64 ELF file '%s'" % filename)
> +
> +    e_entry, e_phoff = struct.unpack_from('<2Q', elf, 0x18)
> +    e_phentsize, e_phnum = struct.unpack_from('<2H', elf, 0x36)
> +    segments = []
> +
> +    for index in range(e_phnum):
> +        offset = e_phoff + e_phentsize * index
> +        p_type, p_flags, p_offset = struct.unpack_from('<LLQ', elf, offset)
> +        if p_type == 1: # PT_LOAD
> +            p_paddr, p_filesz = struct.unpack_from('<2Q', elf, offset + 0x18)
> +            p_data = elf[p_offset:p_offset + p_filesz]
> +            segments.append((index, e_entry, p_paddr, p_data))
> +    return segments
>   
>   def main():
>       uboot_elf = "./u-boot"
>
>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [U-Boot] [PATCH v3 2/2] rockchip: Remove obsolete references to pyelftools
  2019-07-16 19:53 ` [U-Boot] [PATCH v3 2/2] rockchip: Remove obsolete references to pyelftools Chris Webb
@ 2019-07-17 10:39   ` Kever Yang
  2019-07-23  8:04     ` [U-Boot] [PATCH v3 2/2] rockchip: Remove obsolete references to pyelftools【请注意,邮件由u-boot-bounces@lists.denx.de代发】 pyelftools Kever Yang
  0 siblings, 1 reply; 8+ messages in thread
From: Kever Yang @ 2019-07-17 10:39 UTC (permalink / raw)
  To: u-boot


On 2019/7/17 上午3:53, Chris Webb wrote:
> make_fit_atf.py no longer requires pyelftools, and nothing else in the
> rockchip build requires it either, so remove references to installing it
> from the documentation.
>
> Signed-off-by: Chris Webb <chris@arachsys.com>


Reviewed-by: Kever Yang <Kever.yang@rock-chips.com>

Thanks,
  - Kever
> ---
>   board/rockchip/evb_rk3399/README | 6 ------
>   doc/README.rockchip              | 4 ----
>   2 files changed, 10 deletions(-)
>
> diff --git a/board/rockchip/evb_rk3399/README b/board/rockchip/evb_rk3399/README
> index 6469821987..ea3258cf37 100644
> --- a/board/rockchip/evb_rk3399/README
> +++ b/board/rockchip/evb_rk3399/README
> @@ -35,12 +35,6 @@ Get the Source and prebuild binary
>     > git clone https://github.com/rockchip-linux/rkbin.git
>     > git clone https://github.com/rockchip-linux/rkdeveloptool.git
>   
> -Get some prerequisites
> -======================
> -
> -You need the Python elftools.elf.elffile library for make_fit_atf.py to work:
> -
> -  > sudo apt-get install python-pyelftools
>   
>   Compile ATF
>   ===========
> diff --git a/doc/README.rockchip b/doc/README.rockchip
> index 02e2497b15..8ccbb87264 100644
> --- a/doc/README.rockchip
> +++ b/doc/README.rockchip
> @@ -123,10 +123,6 @@ For example:
>   
>      Option 2: Package the image with SPL:
>   
> -   - We need the Python elftools.elf.elffile library for make_fit_atf.py to work
> -
> -     => sudo apt-get install python-pyelftools
> -
>      - Export cross compiler path for aarch64
>   
>      - Compile ATF

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [U-Boot] [PATCH v3 1/2] rockchip: make_fit_atf.py: Eliminate pyelftools dependency
  2019-07-16 19:52 ` [U-Boot] [PATCH v3 1/2] rockchip: make_fit_atf.py: " Chris Webb
  2019-07-17  1:08   ` Andy Yan
@ 2019-07-17 10:47   ` Kever Yang
  2019-07-23  8:04     ` [U-Boot] [PATCH v3 1/2] rockchip: make_fit_atf.py: Eliminate pyelftools dependency【请注意,邮件由u-boot-bounces@lists.denx.de代发】 Kever Yang
  1 sibling, 1 reply; 8+ messages in thread
From: Kever Yang @ 2019-07-17 10:47 UTC (permalink / raw)
  To: u-boot


On 2019/7/17 上午3:52, Chris Webb wrote:
> make_fit_aft.py depends on the non-standard library pyelftools to pull
> out PT_LOAD segments from ELF files. However, this is as easy to do
> manually, without imposing the extra dependency on users.
>
> Structures in the ELF file are unpacked into variables named to exactly
> match the ELF spec to ensure the destructuring code is reasonably
> self-documenting.
>
> Signed-off-by: Chris Webb <chris@arachsys.com>


Reviewed-by: Kever Yang <Kever.yang@rock-chips.com>

Thanks,
  - Kever
> ---
>   arch/arm/mach-rockchip/make_fit_atf.py | 75 +++++++++++---------------
>   1 file changed, 32 insertions(+), 43 deletions(-)
>
> diff --git a/arch/arm/mach-rockchip/make_fit_atf.py b/arch/arm/mach-rockchip/make_fit_atf.py
> index db0ae96ca8..b9a1988298 100755
> --- a/arch/arm/mach-rockchip/make_fit_atf.py
> +++ b/arch/arm/mach-rockchip/make_fit_atf.py
> @@ -13,16 +13,7 @@ import os
>   import sys
>   import getopt
>   import logging
> -
> -# pip install pyelftools
> -from elftools.elf.elffile import ELFFile
> -
> -ELF_SEG_P_TYPE = 'p_type'
> -ELF_SEG_P_PADDR = 'p_paddr'
> -ELF_SEG_P_VADDR = 'p_vaddr'
> -ELF_SEG_P_OFFSET = 'p_offset'
> -ELF_SEG_P_FILESZ = 'p_filesz'
> -ELF_SEG_P_MEMSZ = 'p_memsz'
> +import struct
>   
>   DT_HEADER = """
>   /*
> @@ -118,33 +109,19 @@ def append_conf_node(file, dtbs, segments):
>       file.write('\n')
>   
>   def generate_atf_fit_dts_uboot(fit_file, uboot_file_name):
> -    num_load_seg = 0
> -    p_paddr = 0xFFFFFFFF
> -    with open(uboot_file_name, 'rb') as uboot_file:
> -        uboot = ELFFile(uboot_file)
> -        for i in range(uboot.num_segments()):
> -            seg = uboot.get_segment(i)
> -            if seg.__getitem__(ELF_SEG_P_TYPE) == 'PT_LOAD':
> -                p_paddr = seg.__getitem__(ELF_SEG_P_PADDR)
> -                num_load_seg = num_load_seg + 1
> -
> -    assert (p_paddr != 0xFFFFFFFF and num_load_seg == 1)
> -
> +    segments = unpack_elf(uboot_file_name)
> +    if len(segments) != 1:
> +        raise ValueError("Invalid u-boot ELF image '%s'" % uboot_file_name)
> +    index, entry, p_paddr, data = segments[0]
>       fit_file.write(DT_UBOOT % p_paddr)
>   
>   def generate_atf_fit_dts_bl31(fit_file, bl31_file_name, dtbs_file_name):
> -    with open(bl31_file_name, 'rb') as bl31_file:
> -        bl31 = ELFFile(bl31_file)
> -        elf_entry = bl31.header['e_entry']
> -        segments = bl31.num_segments()
> -        for i in range(segments):
> -            seg = bl31.get_segment(i)
> -            if seg.__getitem__(ELF_SEG_P_TYPE) == 'PT_LOAD':
> -                paddr = seg.__getitem__(ELF_SEG_P_PADDR)
> -                append_bl31_node(fit_file, i + 1, paddr, elf_entry)
> +    segments = unpack_elf(bl31_file_name)
> +    for index, entry, paddr, data in segments:
> +        append_bl31_node(fit_file, index + 1, paddr, entry)
>       append_fdt_node(fit_file, dtbs_file_name)
>       fit_file.write(DT_IMAGES_NODE_END)
> -    append_conf_node(fit_file, dtbs_file_name, segments)
> +    append_conf_node(fit_file, dtbs_file_name, len(segments))
>   
>   def generate_atf_fit_dts(fit_file_name, bl31_file_name, uboot_file_name, dtbs_file_name):
>       # Generate FIT script for ATF image.
> @@ -162,17 +139,29 @@ def generate_atf_fit_dts(fit_file_name, bl31_file_name, uboot_file_name, dtbs_fi
>           fit_file.close()
>   
>   def generate_atf_binary(bl31_file_name):
> -    with open(bl31_file_name, 'rb') as bl31_file:
> -        bl31 = ELFFile(bl31_file)
> -
> -        num = bl31.num_segments()
> -        for i in range(num):
> -            seg = bl31.get_segment(i)
> -            if seg.__getitem__(ELF_SEG_P_TYPE) == 'PT_LOAD':
> -                paddr = seg.__getitem__(ELF_SEG_P_PADDR)
> -                file_name = 'bl31_0x%08x.bin' % paddr
> -                with open(file_name, "wb") as atf:
> -                    atf.write(seg.data())
> +    for index, entry, paddr, data in unpack_elf(bl31_file_name):
> +        file_name = 'bl31_0x%08x.bin' % paddr
> +        with open(file_name, "wb") as atf:
> +            atf.write(data)
> +
> +def unpack_elf(filename):
> +    with open(filename, 'rb') as file:
> +        elf = file.read()
> +    if elf[0:7] != b'\x7fELF\x02\x01\x01' or elf[18:20] != b'\xb7\x00':
> +        raise ValueError("Invalid arm64 ELF file '%s'" % filename)
> +
> +    e_entry, e_phoff = struct.unpack_from('<2Q', elf, 0x18)
> +    e_phentsize, e_phnum = struct.unpack_from('<2H', elf, 0x36)
> +    segments = []
> +
> +    for index in range(e_phnum):
> +        offset = e_phoff + e_phentsize * index
> +        p_type, p_flags, p_offset = struct.unpack_from('<LLQ', elf, offset)
> +        if p_type == 1: # PT_LOAD
> +            p_paddr, p_filesz = struct.unpack_from('<2Q', elf, offset + 0x18)
> +            p_data = elf[p_offset:p_offset + p_filesz]
> +            segments.append((index, e_entry, p_paddr, p_data))
> +    return segments
>   
>   def main():
>       uboot_elf = "./u-boot"

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [U-Boot] [PATCH v3 1/2] rockchip: make_fit_atf.py: Eliminate pyelftools dependency【请注意,邮件由u-boot-bounces@lists.denx.de代发】
  2019-07-17 10:47   ` Kever Yang
@ 2019-07-23  8:04     ` Kever Yang
  0 siblings, 0 replies; 8+ messages in thread
From: Kever Yang @ 2019-07-23  8:04 UTC (permalink / raw)
  To: u-boot


On 2019/7/17 下午6:47, Kever Yang wrote:
>
> On 2019/7/17 上午3:52, Chris Webb wrote:
>> make_fit_aft.py depends on the non-standard library pyelftools to pull
>> out PT_LOAD segments from ELF files. However, this is as easy to do
>> manually, without imposing the extra dependency on users.
>>
>> Structures in the ELF file are unpacked into variables named to exactly
>> match the ELF spec to ensure the destructuring code is reasonably
>> self-documenting.
>>
>> Signed-off-by: Chris Webb <chris@arachsys.com>
>
>
> Reviewed-by: Kever Yang <Kever.yang@rock-chips.com>

Applied to u-boot-rockchip, thanks!
>
> Thanks,
>  - Kever
>> ---
>>   arch/arm/mach-rockchip/make_fit_atf.py | 75 +++++++++++---------------
>>   1 file changed, 32 insertions(+), 43 deletions(-)
>>
>> diff --git a/arch/arm/mach-rockchip/make_fit_atf.py 
>> b/arch/arm/mach-rockchip/make_fit_atf.py
>> index db0ae96ca8..b9a1988298 100755
>> --- a/arch/arm/mach-rockchip/make_fit_atf.py
>> +++ b/arch/arm/mach-rockchip/make_fit_atf.py
>> @@ -13,16 +13,7 @@ import os
>>   import sys
>>   import getopt
>>   import logging
>> -
>> -# pip install pyelftools
>> -from elftools.elf.elffile import ELFFile
>> -
>> -ELF_SEG_P_TYPE = 'p_type'
>> -ELF_SEG_P_PADDR = 'p_paddr'
>> -ELF_SEG_P_VADDR = 'p_vaddr'
>> -ELF_SEG_P_OFFSET = 'p_offset'
>> -ELF_SEG_P_FILESZ = 'p_filesz'
>> -ELF_SEG_P_MEMSZ = 'p_memsz'
>> +import struct
>>     DT_HEADER = """
>>   /*
>> @@ -118,33 +109,19 @@ def append_conf_node(file, dtbs, segments):
>>       file.write('\n')
>>     def generate_atf_fit_dts_uboot(fit_file, uboot_file_name):
>> -    num_load_seg = 0
>> -    p_paddr = 0xFFFFFFFF
>> -    with open(uboot_file_name, 'rb') as uboot_file:
>> -        uboot = ELFFile(uboot_file)
>> -        for i in range(uboot.num_segments()):
>> -            seg = uboot.get_segment(i)
>> -            if seg.__getitem__(ELF_SEG_P_TYPE) == 'PT_LOAD':
>> -                p_paddr = seg.__getitem__(ELF_SEG_P_PADDR)
>> -                num_load_seg = num_load_seg + 1
>> -
>> -    assert (p_paddr != 0xFFFFFFFF and num_load_seg == 1)
>> -
>> +    segments = unpack_elf(uboot_file_name)
>> +    if len(segments) != 1:
>> +        raise ValueError("Invalid u-boot ELF image '%s'" % 
>> uboot_file_name)
>> +    index, entry, p_paddr, data = segments[0]
>>       fit_file.write(DT_UBOOT % p_paddr)
>>     def generate_atf_fit_dts_bl31(fit_file, bl31_file_name, 
>> dtbs_file_name):
>> -    with open(bl31_file_name, 'rb') as bl31_file:
>> -        bl31 = ELFFile(bl31_file)
>> -        elf_entry = bl31.header['e_entry']
>> -        segments = bl31.num_segments()
>> -        for i in range(segments):
>> -            seg = bl31.get_segment(i)
>> -            if seg.__getitem__(ELF_SEG_P_TYPE) == 'PT_LOAD':
>> -                paddr = seg.__getitem__(ELF_SEG_P_PADDR)
>> -                append_bl31_node(fit_file, i + 1, paddr, elf_entry)
>> +    segments = unpack_elf(bl31_file_name)
>> +    for index, entry, paddr, data in segments:
>> +        append_bl31_node(fit_file, index + 1, paddr, entry)
>>       append_fdt_node(fit_file, dtbs_file_name)
>>       fit_file.write(DT_IMAGES_NODE_END)
>> -    append_conf_node(fit_file, dtbs_file_name, segments)
>> +    append_conf_node(fit_file, dtbs_file_name, len(segments))
>>     def generate_atf_fit_dts(fit_file_name, bl31_file_name, 
>> uboot_file_name, dtbs_file_name):
>>       # Generate FIT script for ATF image.
>> @@ -162,17 +139,29 @@ def generate_atf_fit_dts(fit_file_name, 
>> bl31_file_name, uboot_file_name, dtbs_fi
>>           fit_file.close()
>>     def generate_atf_binary(bl31_file_name):
>> -    with open(bl31_file_name, 'rb') as bl31_file:
>> -        bl31 = ELFFile(bl31_file)
>> -
>> -        num = bl31.num_segments()
>> -        for i in range(num):
>> -            seg = bl31.get_segment(i)
>> -            if seg.__getitem__(ELF_SEG_P_TYPE) == 'PT_LOAD':
>> -                paddr = seg.__getitem__(ELF_SEG_P_PADDR)
>> -                file_name = 'bl31_0x%08x.bin' % paddr
>> -                with open(file_name, "wb") as atf:
>> -                    atf.write(seg.data())
>> +    for index, entry, paddr, data in unpack_elf(bl31_file_name):
>> +        file_name = 'bl31_0x%08x.bin' % paddr
>> +        with open(file_name, "wb") as atf:
>> +            atf.write(data)
>> +
>> +def unpack_elf(filename):
>> +    with open(filename, 'rb') as file:
>> +        elf = file.read()
>> +    if elf[0:7] != b'\x7fELF\x02\x01\x01' or elf[18:20] != b'\xb7\x00':
>> +        raise ValueError("Invalid arm64 ELF file '%s'" % filename)
>> +
>> +    e_entry, e_phoff = struct.unpack_from('<2Q', elf, 0x18)
>> +    e_phentsize, e_phnum = struct.unpack_from('<2H', elf, 0x36)
>> +    segments = []
>> +
>> +    for index in range(e_phnum):
>> +        offset = e_phoff + e_phentsize * index
>> +        p_type, p_flags, p_offset = struct.unpack_from('<LLQ', elf, 
>> offset)
>> +        if p_type == 1: # PT_LOAD
>> +            p_paddr, p_filesz = struct.unpack_from('<2Q', elf, 
>> offset + 0x18)
>> +            p_data = elf[p_offset:p_offset + p_filesz]
>> +            segments.append((index, e_entry, p_paddr, p_data))
>> +    return segments
>>     def main():
>>       uboot_elf = "./u-boot"
>
>
> _______________________________________________
> U-Boot mailing list
> U-Boot at lists.denx.de
> https://lists.denx.de/listinfo/u-boot

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [U-Boot] [PATCH v3 2/2] rockchip: Remove obsolete references to pyelftools【请注意,邮件由u-boot-bounces@lists.denx.de代发】 pyelftools
  2019-07-17 10:39   ` Kever Yang
@ 2019-07-23  8:04     ` Kever Yang
  0 siblings, 0 replies; 8+ messages in thread
From: Kever Yang @ 2019-07-23  8:04 UTC (permalink / raw)
  To: u-boot


On 2019/7/17 下午6:39, Kever Yang wrote:
>
> On 2019/7/17 上午3:53, Chris Webb wrote:
>> make_fit_atf.py no longer requires pyelftools, and nothing else in the
>> rockchip build requires it either, so remove references to installing it
>> from the documentation.
>>
>> Signed-off-by: Chris Webb <chris@arachsys.com>
>
>
> Reviewed-by: Kever Yang <Kever.yang@rock-chips.com>

Applied to u-boot-rockchip, thanks!
>
> Thanks,
>  - Kever
>> ---
>>   board/rockchip/evb_rk3399/README | 6 ------
>>   doc/README.rockchip              | 4 ----
>>   2 files changed, 10 deletions(-)
>>
>> diff --git a/board/rockchip/evb_rk3399/README 
>> b/board/rockchip/evb_rk3399/README
>> index 6469821987..ea3258cf37 100644
>> --- a/board/rockchip/evb_rk3399/README
>> +++ b/board/rockchip/evb_rk3399/README
>> @@ -35,12 +35,6 @@ Get the Source and prebuild binary
>>     > git clone https://github.com/rockchip-linux/rkbin.git
>>     > git clone https://github.com/rockchip-linux/rkdeveloptool.git
>>   -Get some prerequisites
>> -======================
>> -
>> -You need the Python elftools.elf.elffile library for make_fit_atf.py 
>> to work:
>> -
>> -  > sudo apt-get install python-pyelftools
>>     Compile ATF
>>   ===========
>> diff --git a/doc/README.rockchip b/doc/README.rockchip
>> index 02e2497b15..8ccbb87264 100644
>> --- a/doc/README.rockchip
>> +++ b/doc/README.rockchip
>> @@ -123,10 +123,6 @@ For example:
>>        Option 2: Package the image with SPL:
>>   -   - We need the Python elftools.elf.elffile library for 
>> make_fit_atf.py to work
>> -
>> -     => sudo apt-get install python-pyelftools
>> -
>>      - Export cross compiler path for aarch64
>>        - Compile ATF
>
>
> _______________________________________________
> U-Boot mailing list
> U-Boot at lists.denx.de
> https://lists.denx.de/listinfo/u-boot

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2019-07-23  8:04 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-16 19:51 [U-Boot] [PATCH v3 0/2] rockchip: make_fit_atf.py Eliminate pyelftools dependency Chris Webb
2019-07-16 19:52 ` [U-Boot] [PATCH v3 1/2] rockchip: make_fit_atf.py: " Chris Webb
2019-07-17  1:08   ` Andy Yan
2019-07-17 10:47   ` Kever Yang
2019-07-23  8:04     ` [U-Boot] [PATCH v3 1/2] rockchip: make_fit_atf.py: Eliminate pyelftools dependency【请注意,邮件由u-boot-bounces@lists.denx.de代发】 Kever Yang
2019-07-16 19:53 ` [U-Boot] [PATCH v3 2/2] rockchip: Remove obsolete references to pyelftools Chris Webb
2019-07-17 10:39   ` Kever Yang
2019-07-23  8:04     ` [U-Boot] [PATCH v3 2/2] rockchip: Remove obsolete references to pyelftools【请注意,邮件由u-boot-bounces@lists.denx.de代发】 pyelftools Kever Yang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.