Skip to contentSkip to conversation

mach-rockchip: make_fit_atf.py: support OP-TEE tee.bin v1 format

PATCH In review Jerome Forissier 5 participants 10 messages May 11, 2022 – Jun 20, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mach-rockchip: make_fit_atf.py: support OP-TEE tee.bin v1 format

This commit adds support for the OP-TEE 'tee.bin' v1 format for Rockchip platforms.

Since OP-TEE 3.8.0, tee.bin contains meta-data in a proprietary format in addition to the ELF data. They are essential information for proper initialization of the TEE core, such as the size of the memory region covered by the TEE or a compact representation of runtime relocation data when ASLR is enabled.

With OP-TEE 3.8.0 onwards, 'tee.elf' MUST NOT be used and 'tee.bin' MUST be used instead. Ignoring this recommendation can lead to crashes as described in [3].

---
 arch/arm/mach-rockchip/make_fit_atf.py | 43 +++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 8 deletions(-)
arch/arm/mach-rockchip/make_fit_atf.py +35 -8
index f3224d2555..fcea652388 100755
@@ -137,7 +137,7 @@ def generate_atf_fit_dts_bl31(fit_file, bl31_file_name, tee_file_name, dtbs_file
     num_segments = len(segments)
 
     if tee_file_name:
-        tee_segments = unpack_elf(tee_file_name)
+        tee_segments = unpack_tee_file(tee_file_name)
         for index, entry, paddr, data in tee_segments:
             append_tee_node(fit_file, num_segments + index + 1, paddr, entry)
         num_segments = num_segments + len(tee_segments)
@@ -169,7 +169,7 @@ def generate_atf_binary(bl31_file_name):
 
 def generate_tee_binary(tee_file_name):
     if tee_file_name:
-        for index, entry, paddr, data in unpack_elf(tee_file_name):
+        for index, entry, paddr, data in unpack_tee_file(tee_file_name):
             file_name = 'tee_0x%08x.bin' % paddr
             with open(file_name, "wb") as atf:
                 atf.write(data)
@@ -194,6 +194,31 @@ def unpack_elf(filename):
                 segments.append((index, e_entry, p_paddr, p_data))
     return segments
 
+def unpack_tee_file(filename):
+    if filename.endswith('.elf'):
+        return unpack_elf(filename)
+    with open(filename, 'rb') as file:
+        bin = file.read()
+    segments = []
+    if bin[0:5] == b'OPTE\x01':
+        # OP-TEE v1 format (tee.bin)
+        init_sz, start_hi, start_lo, _, paged_sz = struct.unpack_from('<5I',
+                                                                      bin,
+                                                                      0x8)
+        if paged_sz != 0:
+            raise ValueError("OP-TEE paged mode not supported")
+        e_entry = (start_hi << 32) + start_lo
+        p_addr = e_entry
+        p_data = bin[0x1c:]
+        if len(p_data) != init_sz:
+            raise ValueError("Invalid file '%s': size mismatch "
+                             "(expected %d, have %d)" % (filename, init_sz,
+                                                         len(p_data)))
+        segments.append((0, e_entry, p_addr, p_data))
+    else:
+        raise ValueError("Unknown format for TEE file '%s'" % filename)
+    return segments
+
 def main():
     uboot_elf = "./u-boot"
     fit_its = sys.stdout
@@ -210,11 +235,13 @@ def main():
         logging.warning(' Please read Building section in doc/README.rockchip')
 
     if "TEE" in os.environ:
-        tee_elf = os.getenv("TEE")
+        tee_file = os.getenv("TEE")
+    elif os.path.isfile("./tee.bin"):
+        tee_file = "./tee.bin"
     elif os.path.isfile("./tee.elf"):
-        tee_elf = "./tee.elf"
+        tee_file = "./tee.elf"
     else:
-        tee_elf = ""
+        tee_file = ""
 
     opts, args = getopt.getopt(sys.argv[1:], "o:u:b:t:h")
     for opt, val in opts:
@@ -225,16 +252,16 @@ def main():
         elif opt == "-b":
             bl31_elf = val
         elif opt == "-t":
-            tee_elf = val
+            tee_file = val
         elif opt == "-h":
             print(__doc__)
             sys.exit(2)
 
     dtbs = args
 
-    generate_atf_fit_dts(fit_its, bl31_elf, tee_elf, uboot_elf, dtbs)
+    generate_atf_fit_dts(fit_its, bl31_elf, tee_file, uboot_elf, dtbs)
     generate_atf_binary(bl31_elf)
-    generate_tee_binary(tee_elf)
+    generate_tee_binary(tee_file)
 
 if __name__ == "__main__":
     main()
Signature · 1 line
2.17.1
Tested-by on v1 1/1
#1 lore raw

Hi Jerome,

Quoted 13 lines · Jerome Forissier

This commit adds support for the OP-TEE 'tee.bin' v1 format for Rockchip platforms.

Since OP-TEE 3.8.0, tee.bin contains meta-data in a proprietary format in addition to the ELF data. They are essential information for proper initialization of the TEE core, such as the size of the memory region covered by the TEE or a compact representation of runtime relocation data when ASLR is enabled.

With OP-TEE 3.8.0 onwards, 'tee.elf' MUST NOT be used and 'tee.bin' MUST be used instead. Ignoring this recommendation can lead to crashes as described in [3].

Quoted 7 lines

Link: [1]
https://github.com/OP-TEE/optee_os/commit/5dd1570ac5b0f6563b1a9c074533a19107b8222d
Link: [2]
https://github.com/OP-TEE/optee_os/blob/3.17.0/scripts/gen_tee_bin.py#L275-L302

Tested on my RockPi4b board, so

Quoted 105 lines
---
 arch/arm/mach-rockchip/make_fit_atf.py | 43 +++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 8 deletions(-)
arch/arm/mach-rockchip/make_fit_atf.py +0 -0
index f3224d2555..fcea652388 100755
@@ -137,7 +137,7 @@ def generate_atf_fit_dts_bl31(fit_file,

bl31_file_name, tee_file_name, dtbs_file
num_segments = len(segments)

if tee_file_name:

arch/arm/mach-rockchip/make_fit_atf.py +1 -1
-        tee_segments = unpack_elf(tee_file_name)
+        tee_segments = unpack_tee_file(tee_file_name)
         for index, entry, paddr, data in tee_segments:
             append_tee_node(fit_file, num_segments + index + 1, paddr,

entry)
num_segments = num_segments + len(tee_segments)

arch/arm/mach-rockchip/make_fit_atf.py +26 -1
@@ -169,7 +169,7 @@ def generate_atf_binary(bl31_file_name):
 
 def generate_tee_binary(tee_file_name):
     if tee_file_name:
-        for index, entry, paddr, data in unpack_elf(tee_file_name):
+        for index, entry, paddr, data in unpack_tee_file(tee_file_name):
             file_name = 'tee_0x%08x.bin' % paddr
             with open(file_name, "wb") as atf:
                 atf.write(data)
@@ -194,6 +194,31 @@ def unpack_elf(filename):
                 segments.append((index, e_entry, p_paddr, p_data))
     return segments
 
+def unpack_tee_file(filename):
+    if filename.endswith('.elf'):
+        return unpack_elf(filename)
+    with open(filename, 'rb') as file:
+        bin = file.read()
+    segments = []
+    if bin[0:5] == b'OPTE\x01':
+        # OP-TEE v1 format (tee.bin)
+        init_sz, start_hi, start_lo, _, paged_sz =
 struct.unpack_from('<5I',
+                                                                      bin,
+                                                                      0x8)
+        if paged_sz != 0:
+            raise ValueError("OP-TEE paged mode not supported")
+        e_entry = (start_hi << 32) + start_lo
+        p_addr = e_entry
+        p_data = bin[0x1c:]
+        if len(p_data) != init_sz:
+            raise ValueError("Invalid file '%s': size mismatch "
+                             "(expected %d, have %d)" % (filename,
 init_sz,
+                                                         len(p_data)))
+        segments.append((0, e_entry, p_addr, p_data))
+    else:
+        raise ValueError("Unknown format for TEE file '%s'" % filename)
+    return segments
+
 def main():

uboot_elf = "./u-boot"
fit_its = sys.stdout

arch/arm/mach-rockchip/make_fit_atf.py +5 -3
@@ -210,11 +235,13 @@ def main():
         logging.warning(' Please read Building section in
 doc/README.rockchip')
 
     if "TEE" in os.environ:
-        tee_elf = os.getenv("TEE")
+        tee_file = os.getenv("TEE")
+    elif os.path.isfile("./tee.bin"):
+        tee_file = "./tee.bin"
     elif os.path.isfile("./tee.elf"):
-        tee_elf = "./tee.elf"
+        tee_file = "./tee.elf"
     else:
-        tee_elf = ""
+        tee_file = ""
 
     opts, args = getopt.getopt(sys.argv[1:], "o:u:b:t:h")

for opt, val in opts:

arch/arm/mach-rockchip/make_fit_atf.py +3 -4
@@ -225,16 +252,16 @@ def main():
         elif opt == "-b":
             bl31_elf = val
         elif opt == "-t":
-            tee_elf = val
+            tee_file = val
         elif opt == "-h":
             print(__doc__)
             sys.exit(2)
 
     dtbs = args
 
-    generate_atf_fit_dts(fit_its, bl31_elf, tee_elf, uboot_elf, dtbs)
+    generate_atf_fit_dts(fit_its, bl31_elf, tee_file, uboot_elf, dtbs)
     generate_atf_binary(bl31_elf)
-    generate_tee_binary(tee_elf)
+    generate_tee_binary(tee_file)
 
 if __name__ == "__main__":
     main()
--

2.17.1

Reviewed-by on v1 1/1
#2 lore raw
In reply to Jerome Forissier
Quoted 9 lines · Jerome Forissier

This commit adds support for the OP-TEE 'tee.bin' v1 format for Rockchip platforms.

Since OP-TEE 3.8.0, tee.bin contains meta-data in a proprietary format in addition to the ELF data. They are essential information for proper initialization of the TEE core, such as the size of the memory region covered by the TEE or a compact representation of runtime relocation data when ASLR is enabled.

Quoted 8 lines

With OP-TEE 3.8.0 onwards, 'tee.elf' MUST NOT be used and 'tee.bin' MUST be used instead. Ignoring this recommendation can lead to crashes as described in [3].

Thanks,
- Kever

Quoted 95 lines
---
  arch/arm/mach-rockchip/make_fit_atf.py | 43 +++++++++++++++++++++-----
  1 file changed, 35 insertions(+), 8 deletions(-)
arch/arm/mach-rockchip/make_fit_atf.py +35 -8
index f3224d2555..fcea652388 100755
@@ -137,7 +137,7 @@ def generate_atf_fit_dts_bl31(fit_file, bl31_file_name, tee_file_name, dtbs_file
      num_segments = len(segments)
 
      if tee_file_name:
-        tee_segments = unpack_elf(tee_file_name)
+        tee_segments = unpack_tee_file(tee_file_name)
          for index, entry, paddr, data in tee_segments:
              append_tee_node(fit_file, num_segments + index + 1, paddr, entry)
          num_segments = num_segments + len(tee_segments)
@@ -169,7 +169,7 @@ def generate_atf_binary(bl31_file_name):
 
  def generate_tee_binary(tee_file_name):
      if tee_file_name:
-        for index, entry, paddr, data in unpack_elf(tee_file_name):
+        for index, entry, paddr, data in unpack_tee_file(tee_file_name):
              file_name = 'tee_0x%08x.bin' % paddr
              with open(file_name, "wb") as atf:
                  atf.write(data)
@@ -194,6 +194,31 @@ def unpack_elf(filename):
                  segments.append((index, e_entry, p_paddr, p_data))
      return segments
 
+def unpack_tee_file(filename):
+    if filename.endswith('.elf'):
+        return unpack_elf(filename)
+    with open(filename, 'rb') as file:
+        bin = file.read()
+    segments = []
+    if bin[0:5] == b'OPTE\x01':
+        # OP-TEE v1 format (tee.bin)
+        init_sz, start_hi, start_lo, _, paged_sz = struct.unpack_from('<5I',
+                                                                      bin,
+                                                                      0x8)
+        if paged_sz != 0:
+            raise ValueError("OP-TEE paged mode not supported")
+        e_entry = (start_hi << 32) + start_lo
+        p_addr = e_entry
+        p_data = bin[0x1c:]
+        if len(p_data) != init_sz:
+            raise ValueError("Invalid file '%s': size mismatch "
+                             "(expected %d, have %d)" % (filename, init_sz,
+                                                         len(p_data)))
+        segments.append((0, e_entry, p_addr, p_data))
+    else:
+        raise ValueError("Unknown format for TEE file '%s'" % filename)
+    return segments
+
  def main():
      uboot_elf = "./u-boot"
      fit_its = sys.stdout
@@ -210,11 +235,13 @@ def main():
          logging.warning(' Please read Building section in doc/README.rockchip')
 
      if "TEE" in os.environ:
-        tee_elf = os.getenv("TEE")
+        tee_file = os.getenv("TEE")
+    elif os.path.isfile("./tee.bin"):
+        tee_file = "./tee.bin"
      elif os.path.isfile("./tee.elf"):
-        tee_elf = "./tee.elf"
+        tee_file = "./tee.elf"
      else:
-        tee_elf = ""
+        tee_file = ""
 
      opts, args = getopt.getopt(sys.argv[1:], "o:u:b:t:h")
      for opt, val in opts:
@@ -225,16 +252,16 @@ def main():
          elif opt == "-b":
              bl31_elf = val
          elif opt == "-t":
-            tee_elf = val
+            tee_file = val
          elif opt == "-h":
              print(__doc__)
              sys.exit(2)
 
      dtbs = args
 
-    generate_atf_fit_dts(fit_its, bl31_elf, tee_elf, uboot_elf, dtbs)
+    generate_atf_fit_dts(fit_its, bl31_elf, tee_file, uboot_elf, dtbs)
      generate_atf_binary(bl31_elf)
-    generate_tee_binary(tee_elf)
+    generate_tee_binary(tee_file)
 
  if __name__ == "__main__":
      main()
Acked-by on v1 1/1
#3 lore raw
In reply to Jerome Forissier
Quoted 13 lines · Jerome Forissier

This commit adds support for the OP-TEE 'tee.bin' v1 format for Rockchip platforms.

Since OP-TEE 3.8.0, tee.bin contains meta-data in a proprietary format in addition to the ELF data. They are essential information for proper initialization of the TEE core, such as the size of the memory region covered by the TEE or a compact representation of runtime relocation data when ASLR is enabled.

With OP-TEE 3.8.0 onwards, 'tee.elf' MUST NOT be used and 'tee.bin' MUST be used instead. Ignoring this recommendation can lead to crashes as described in [3].

Quoted 7 lines
---
 arch/arm/mach-rockchip/make_fit_atf.py | 43 +++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 8 deletions(-)

This might make it slightly harder to convert this file to binman, since it's not as good at deconstructing things. I can't say I understand OP-TEE OS or its files/formats, so:

Quoted 29 lines
arch/arm/mach-rockchip/make_fit_atf.py +5 -2
index f3224d2555..fcea652388 100755
@@ -137,7 +137,7 @@ def generate_atf_fit_dts_bl31(fit_file, bl31_file_name, tee_file_name, dtbs_file
     num_segments = len(segments)
 
     if tee_file_name:
-        tee_segments = unpack_elf(tee_file_name)
+        tee_segments = unpack_tee_file(tee_file_name)
         for index, entry, paddr, data in tee_segments:
             append_tee_node(fit_file, num_segments + index + 1, paddr, entry)
         num_segments = num_segments + len(tee_segments)
@@ -169,7 +169,7 @@ def generate_atf_binary(bl31_file_name):
 
 def generate_tee_binary(tee_file_name):
     if tee_file_name:
-        for index, entry, paddr, data in unpack_elf(tee_file_name):
+        for index, entry, paddr, data in unpack_tee_file(tee_file_name):
             file_name = 'tee_0x%08x.bin' % paddr
             with open(file_name, "wb") as atf:
                 atf.write(data)
@@ -194,6 +194,31 @@ def unpack_elf(filename):
                 segments.append((index, e_entry, p_paddr, p_data))
     return segments
 
+def unpack_tee_file(filename):
+    if filename.endswith('.elf'):
+        return unpack_elf(filename)

arch/arm/mach-rockchip/make_fit_atf.py def unpack_elf(filename): · line 209

Quoted 10 lines
arch/arm/mach-rockchip/make_fit_atf.py +10 -0
+    with open(filename, 'rb') as file:
+        bin = file.read()
+    segments = []
+    if bin[0:5] == b'OPTE\x01':
+        # OP-TEE v1 format (tee.bin)
+        init_sz, start_hi, start_lo, _, paged_sz = struct.unpack_from('<5I',
+                                                                      bin,
+                                                                      0x8)
+        if paged_sz != 0:
+            raise ValueError("OP-TEE paged mode not supported")

Is this not feasible/necessary to do, or just not implemented yet?

arch/arm/mach-rockchip/make_fit_atf.py def unpack_elf(filename): · line 219

Quoted 10 lines
arch/arm/mach-rockchip/make_fit_atf.py +10 -0
+        e_entry = (start_hi << 32) + start_lo
+        p_addr = e_entry
+        p_data = bin[0x1c:]
+        if len(p_data) != init_sz:
+            raise ValueError("Invalid file '%s': size mismatch "
+                             "(expected %d, have %d)" % (filename, init_sz,
+                                                         len(p_data)))
+        segments.append((0, e_entry, p_addr, p_data))
+    else:
+        raise ValueError("Unknown format for TEE file '%s'" % filename)

I see an 'output_header_v2' in your link [2], what about that?

Quoted 42 lines
arch/arm/mach-rockchip/make_fit_atf.py +10 -6
+    return segments
+
 def main():
     uboot_elf = "./u-boot"
     fit_its = sys.stdout
@@ -210,11 +235,13 @@ def main():
         logging.warning(' Please read Building section in doc/README.rockchip')
 
     if "TEE" in os.environ:
-        tee_elf = os.getenv("TEE")
+        tee_file = os.getenv("TEE")
+    elif os.path.isfile("./tee.bin"):
+        tee_file = "./tee.bin"
     elif os.path.isfile("./tee.elf"):
-        tee_elf = "./tee.elf"
+        tee_file = "./tee.elf"
     else:
-        tee_elf = ""
+        tee_file = ""
 
     opts, args = getopt.getopt(sys.argv[1:], "o:u:b:t:h")
     for opt, val in opts:
@@ -225,16 +252,16 @@ def main():
         elif opt == "-b":
             bl31_elf = val
         elif opt == "-t":
-            tee_elf = val
+            tee_file = val
         elif opt == "-h":
             print(__doc__)
             sys.exit(2)
 
     dtbs = args
 
-    generate_atf_fit_dts(fit_its, bl31_elf, tee_elf, uboot_elf, dtbs)
+    generate_atf_fit_dts(fit_its, bl31_elf, tee_file, uboot_elf, dtbs)
     generate_atf_binary(bl31_elf)
-    generate_tee_binary(tee_elf)
+    generate_tee_binary(tee_file)
 
 if __name__ == "__main__":
     main()
#4 lore raw
Quoted 22 lines · Alper Nebi Yasak
Quoted 13 lines · Jerome Forissier

This commit adds support for the OP-TEE 'tee.bin' v1 format for Rockchip platforms.

Since OP-TEE 3.8.0, tee.bin contains meta-data in a proprietary format in addition to the ELF data. They are essential information for proper initialization of the TEE core, such as the size of the memory region covered by the TEE or a compact representation of runtime relocation data when ASLR is enabled.

With OP-TEE 3.8.0 onwards, 'tee.elf' MUST NOT be used and 'tee.bin' MUST be used instead. Ignoring this recommendation can lead to crashes as described in [3].

Quoted 7 lines
---
 arch/arm/mach-rockchip/make_fit_atf.py | 43 +++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 8 deletions(-)
Quoted 2 lines

This might make it slightly harder to convert this file to binman, since it's not as good at deconstructing things.

Yes it is a bit unfortunate, but nothing we can't fix I'm sure. Well, I say this knowing absolutely nothing about binman... ;-)

Quoted 4 lines

I can't say I understand
OP-TEE OS or its files/formats, so:

Thanks, answers to your questions below.

Quoted 26 lines
Quoted 26 lines
arch/arm/mach-rockchip/make_fit_atf.py +2 -2
index f3224d2555..fcea652388 100755
@@ -137,7 +137,7 @@ def generate_atf_fit_dts_bl31(fit_file, bl31_file_name, tee_file_name, dtbs_file
     num_segments = len(segments)
 
     if tee_file_name:
-        tee_segments = unpack_elf(tee_file_name)
+        tee_segments = unpack_tee_file(tee_file_name)
         for index, entry, paddr, data in tee_segments:
             append_tee_node(fit_file, num_segments + index + 1, paddr, entry)
         num_segments = num_segments + len(tee_segments)
@@ -169,7 +169,7 @@ def generate_atf_binary(bl31_file_name):
 
 def generate_tee_binary(tee_file_name):
     if tee_file_name:
-        for index, entry, paddr, data in unpack_elf(tee_file_name):
+        for index, entry, paddr, data in unpack_tee_file(tee_file_name):
             file_name = 'tee_0x%08x.bin' % paddr
             with open(file_name, "wb") as atf:
                 atf.write(data)
@@ -194,6 +194,31 @@ def unpack_elf(filename):
                 segments.append((index, e_entry, p_paddr, p_data))
     return segments
 
Quoted 15 lines

arch/arm/mach-rockchip/make_fit_atf.py def unpack_elf(filename): · line 209

Quoted 13 lines
arch/arm/mach-rockchip/make_fit_atf.py +13 -0
+def unpack_tee_file(filename):
+    if filename.endswith('.elf'):
+        return unpack_elf(filename)
+    with open(filename, 'rb') as file:
+        bin = file.read()
+    segments = []
+    if bin[0:5] == b'OPTE\x01':
+        # OP-TEE v1 format (tee.bin)
+        init_sz, start_hi, start_lo, _, paged_sz = struct.unpack_from('<5I',
+                                                                      bin,
+                                                                      0x8)
+        if paged_sz != 0:
+            raise ValueError("OP-TEE paged mode not supported")

Is this not feasible/necessary to do, or just not implemented yet?

Just not implemented yet. Pager support is not strictly needed, its use or not depends on the platform and on the threat model. In other words, whether or not it is OK to have the TEE and TAs run in DRAM, usually isolated only from Normal World software by TrustZone or some kind of memory firewall. Pager allows to protect from physical access to the DRAM too. It provides authentication and/or encryption to anything stored outside the internal SRAM of the SoC. Testing this mode on RockPi4 would require some non trivial work. Here I simply focused on implementing the current use case properly.

Quoted 11 lines

arch/arm/mach-rockchip/make_fit_atf.py def unpack_elf(filename): · line 219

Quoted 10 lines
arch/arm/mach-rockchip/make_fit_atf.py +10 -0
+        e_entry = (start_hi << 32) + start_lo
+        p_addr = e_entry
+        p_data = bin[0x1c:]
+        if len(p_data) != init_sz:
+            raise ValueError("Invalid file '%s': size mismatch "
+                             "(expected %d, have %d)" % (filename, init_sz,
+                                                         len(p_data)))
+        segments.append((0, e_entry, p_addr, p_data))
+    else:
+        raise ValueError("Unknown format for TEE file '%s'" % filename)
Quoted 1 line

I see an 'output_header_v2' in your link [2], what about that?

v2 is useful only when the OP-TEE pager is used, in which case it is a matter of preference whether to use a single binary and have the loader split it as expected (tee.bin) or use separate binaries instead.

Historically (Jens please correct me if I'm wrong!), there was a single raw binary for OP-TEE: tee.bin, and no pager support. Then pager was added. The build would then create two separate files: tee-pager.bin and tee-pageable.bin (the latter would be zero sized with pager disabled). At the same time a header was introduced in tee.bin to indicate whether or not the file would contain a pageable section (tee.bin consisted in the header + tee-pager.bin + tee-pageable.bin). Later, and for reasons I don't remember exactly (related to the integration with TF-A IIRC), the header was written to its own file and the format of that header changed a bit. So we had tee-header_v2.bin, tee-pager_v2.bin, and tee-pageable_v2.bin. The previous tee-pager.bin and tee-pageable.bin were subsequently deprecated; but tee.bin wasn't. It is still generated today, unchanged with its "v1" header. Note, I'm not sure if tee-pager_v2.bin is different from tee-pager.bin (and same goes for the pageable part), but it doesn't really matter for this discussion.

Quoted 43 lines
Quoted 43 lines
arch/arm/mach-rockchip/make_fit_atf.py +10 -6
+    return segments
+
 def main():
     uboot_elf = "./u-boot"
     fit_its = sys.stdout
@@ -210,11 +235,13 @@ def main():
         logging.warning(' Please read Building section in doc/README.rockchip')
 
     if "TEE" in os.environ:
-        tee_elf = os.getenv("TEE")
+        tee_file = os.getenv("TEE")
+    elif os.path.isfile("./tee.bin"):
+        tee_file = "./tee.bin"
     elif os.path.isfile("./tee.elf"):
-        tee_elf = "./tee.elf"
+        tee_file = "./tee.elf"
     else:
-        tee_elf = ""
+        tee_file = ""
 
     opts, args = getopt.getopt(sys.argv[1:], "o:u:b:t:h")
     for opt, val in opts:
@@ -225,16 +252,16 @@ def main():
         elif opt == "-b":
             bl31_elf = val
         elif opt == "-t":
-            tee_elf = val
+            tee_file = val
         elif opt == "-h":
             print(__doc__)
             sys.exit(2)
 
     dtbs = args
 
-    generate_atf_fit_dts(fit_its, bl31_elf, tee_elf, uboot_elf, dtbs)
+    generate_atf_fit_dts(fit_its, bl31_elf, tee_file, uboot_elf, dtbs)
     generate_atf_binary(bl31_elf)
-    generate_tee_binary(tee_elf)
+    generate_tee_binary(tee_file)
 
 if __name__ == "__main__":
     main()
#5 lore raw
Quoted 7 lines · Jerome Forissier
Quoted 5 lines · Alper Nebi Yasak

arch/arm/mach-rockchip/make_fit_atf.py def unpack_elf(filename): · line 209

Quoted 2 lines · Jerome Forissier
arch/arm/mach-rockchip/make_fit_atf.py +2 -0
+        if paged_sz != 0:
+            raise ValueError("OP-TEE paged mode not supported")

Is this not feasible/necessary to do, or just not implemented yet?

Quoted 8 lines

Just not implemented yet. Pager support is not strictly needed, its use or not depends on the platform and on the threat model. In other words, whether or not it is OK to have the TEE and TAs run in DRAM, usually isolated only from Normal World software by TrustZone or some kind of memory firewall. Pager allows to protect from physical access to the DRAM too. It provides authentication and/or encryption to anything stored outside the internal SRAM of the SoC. Testing this mode on RockPi4 would require some non trivial work. Here I simply focused on implementing the current use case properly.

I assume you mean it would need platform-specific work on the OP-TEE OS side. But let me ask in a general sense, would we need to pass 'paged_size', 'init_mem_usage', etc. via the FIT to U-Boot? Or, how well does the info in the files and headers (v1 and v2) map to FIT images and their properties?

Why I am asking these is that I'm hypothesizing we can put the unmodified 'tee.bin' (including header) and 'bl31.elf' (normally split into raw binary pieces) into the FIT, and then interpret them at runtime on the C side. Trying to figure out if it's a good idea.

Quoted 17 lines
Quoted 11 lines

arch/arm/mach-rockchip/make_fit_atf.py def unpack_elf(filename): · line 219

Quoted 10 lines
arch/arm/mach-rockchip/make_fit_atf.py +10 -0
+        e_entry = (start_hi << 32) + start_lo
+        p_addr = e_entry
+        p_data = bin[0x1c:]
+        if len(p_data) != init_sz:
+            raise ValueError("Invalid file '%s': size mismatch "
+                             "(expected %d, have %d)" % (filename, init_sz,
+                                                         len(p_data)))
+        segments.append((0, e_entry, p_addr, p_data))
+    else:
+        raise ValueError("Unknown format for TEE file '%s'" % filename)
Quoted 1 line

I see an 'output_header_v2' in your link [2], what about that?

v2 is useful only when the OP-TEE pager is used, in which case it is a matter of preference whether to use a single binary and have the loader split it as expected (tee.bin) or use separate binaries instead.

Quoted 14 lines

Historically (Jens please correct me if I'm wrong!), there was a single raw binary for OP-TEE: tee.bin, and no pager support. Then pager was added. The build would then create two separate files: tee-pager.bin and tee-pageable.bin (the latter would be zero sized with pager disabled). At the same time a header was introduced in tee.bin to indicate whether or not the file would contain a pageable section (tee.bin consisted in the header + tee-pager.bin + tee-pageable.bin). Later, and for reasons I don't remember exactly (related to the integration with TF-A IIRC), the header was written to its own file and the format of that header changed a bit. So we had tee-header_v2.bin, tee-pager_v2.bin, and tee-pageable_v2.bin. The previous tee-pager.bin and tee-pageable.bin were subsequently deprecated; but tee.bin wasn't. It is still generated today, unchanged with its "v1" header. Note, I'm not sure if tee-pager_v2.bin is different from tee-pager.bin (and same goes for the pageable part), but it doesn't really matter for this discussion.

Thanks for both explanations. I also kept looking at gen_tee_bin.py and
outputs from a few OP-TEE OS builds. If I got things right, I can do:

cat tee-header_v2.bin tee-pager_v2.bin tee-pageable_v2.bin >tee_v2.bin

and that would effectively be the same kind of file/image as tee.bin, just with a v2 header instead of v1, right?

#6 lore raw
Quoted 17 lines · Alper Nebi Yasak
Quoted 7 lines · Jerome Forissier
Quoted 5 lines · Alper Nebi Yasak

arch/arm/mach-rockchip/make_fit_atf.py def unpack_elf(filename): · line 209

Quoted 2 lines · Jerome Forissier
arch/arm/mach-rockchip/make_fit_atf.py +2 -0
+        if paged_sz != 0:
+            raise ValueError("OP-TEE paged mode not supported")

Is this not feasible/necessary to do, or just not implemented yet?

Quoted 8 lines

Just not implemented yet. Pager support is not strictly needed, its use or not depends on the platform and on the threat model. In other words, whether or not it is OK to have the TEE and TAs run in DRAM, usually isolated only from Normal World software by TrustZone or some kind of memory firewall. Pager allows to protect from physical access to the DRAM too. It provides authentication and/or encryption to anything stored outside the internal SRAM of the SoC. Testing this mode on RockPi4 would require some non trivial work. Here I simply focused on implementing the current use case properly.

Quoted 2 lines

I assume you mean it would need platform-specific work on the OP-TEE OS side.

Yes.

Quoted 4 lines

But let me ask in a general sense, would we need to pass 'paged_size', 'init_mem_usage', etc. via the FIT to U-Boot? Or, how well does the info in the files and headers (v1 and v2) map to FIT images and their properties?

Yes, the parameters in the tee.bin header would tell U-Boot if the binary needs to be split in two parts (paged_size != 0) or not. The load address of the "non paged" part is also in the header. And, when a "paged" part is present its load address has to be passed to OP-TEE via register x1.

Quoted 4 lines

Why I am asking these is that I'm hypothesizing we can put the unmodified 'tee.bin' (including header) and 'bl31.elf' (normally split into raw binary pieces) into the FIT, and then interpret them at runtime on the C side. Trying to figure out if it's a good idea.

Definitely sounds doable. It could help having more common code perhaps, and also simplify the content of the FIT and therefore the verification of hash and signatures.

Quoted 32 lines
Quoted 17 lines
Quoted 11 lines

arch/arm/mach-rockchip/make_fit_atf.py def unpack_elf(filename): · line 219

Quoted 10 lines
arch/arm/mach-rockchip/make_fit_atf.py +10 -0
+        e_entry = (start_hi << 32) + start_lo
+        p_addr = e_entry
+        p_data = bin[0x1c:]
+        if len(p_data) != init_sz:
+            raise ValueError("Invalid file '%s': size mismatch "
+                             "(expected %d, have %d)" % (filename, init_sz,
+                                                         len(p_data)))
+        segments.append((0, e_entry, p_addr, p_data))
+    else:
+        raise ValueError("Unknown format for TEE file '%s'" % filename)
Quoted 1 line

I see an 'output_header_v2' in your link [2], what about that?

v2 is useful only when the OP-TEE pager is used, in which case it is a matter of preference whether to use a single binary and have the loader split it as expected (tee.bin) or use separate binaries instead.

Quoted 14 lines

Historically (Jens please correct me if I'm wrong!), there was a single raw binary for OP-TEE: tee.bin, and no pager support. Then pager was added. The build would then create two separate files: tee-pager.bin and tee-pageable.bin (the latter would be zero sized with pager disabled). At the same time a header was introduced in tee.bin to indicate whether or not the file would contain a pageable section (tee.bin consisted in the header + tee-pager.bin + tee-pageable.bin). Later, and for reasons I don't remember exactly (related to the integration with TF-A IIRC), the header was written to its own file and the format of that header changed a bit. So we had tee-header_v2.bin, tee-pager_v2.bin, and tee-pageable_v2.bin. The previous tee-pager.bin and tee-pageable.bin were subsequently deprecated; but tee.bin wasn't. It is still generated today, unchanged with its "v1" header. Note, I'm not sure if tee-pager_v2.bin is different from tee-pager.bin (and same goes for the pageable part), but it doesn't really matter for this discussion.

Quoted 7 lines

Thanks for both explanations. I also kept looking at gen_tee_bin.py and
outputs from a few OP-TEE OS builds. If I got things right, I can do:

cat tee-header_v2.bin tee-pager_v2.bin tee-pageable_v2.bin >tee_v2.bin

and that would effectively be the same kind of file/image as tee.bin, just with a v2 header instead of v1, right?

Correct.

Signature · 1 line
Jerome
#7 lore raw

Hi,

Quoted 79 lines · Jerome Forissier
Quoted 17 lines · Alper Nebi Yasak
Quoted 7 lines · Jerome Forissier
Quoted 5 lines · Alper Nebi Yasak

arch/arm/mach-rockchip/make_fit_atf.py def unpack_elf(filename): · line 209

Quoted 2 lines · Jerome Forissier
arch/arm/mach-rockchip/make_fit_atf.py +2 -0
+        if paged_sz != 0:
+            raise ValueError("OP-TEE paged mode not supported")

Is this not feasible/necessary to do, or just not implemented yet?

Quoted 8 lines

Just not implemented yet. Pager support is not strictly needed, its use or not depends on the platform and on the threat model. In other words, whether or not it is OK to have the TEE and TAs run in DRAM, usually isolated only from Normal World software by TrustZone or some kind of memory firewall. Pager allows to protect from physical access to the DRAM too. It provides authentication and/or encryption to anything stored outside the internal SRAM of the SoC. Testing this mode on RockPi4 would require some non trivial work. Here I simply focused on implementing the current use case properly.

Quoted 2 lines

I assume you mean it would need platform-specific work on the OP-TEE OS side.

Yes.

Quoted 4 lines

But let me ask in a general sense, would we need to pass 'paged_size', 'init_mem_usage', etc. via the FIT to U-Boot? Or, how well does the info in the files and headers (v1 and v2) map to FIT images and their properties?

Yes, the parameters in the tee.bin header would tell U-Boot if the binary needs to be split in two parts (paged_size != 0) or not. The load address of the "non paged" part is also in the header. And, when a "paged" part is present its load address has to be passed to OP-TEE via register x1.

Quoted 4 lines

Why I am asking these is that I'm hypothesizing we can put the unmodified 'tee.bin' (including header) and 'bl31.elf' (normally split into raw binary pieces) into the FIT, and then interpret them at runtime on the C side. Trying to figure out if it's a good idea.

Definitely sounds doable. It could help having more common code perhaps, and also simplify the content of the FIT and therefore the verification of hash and signatures.

Quoted 35 lines
Quoted 17 lines
Quoted 11 lines

arch/arm/mach-rockchip/make_fit_atf.py def unpack_elf(filename): · line 219

Quoted 10 lines
arch/arm/mach-rockchip/make_fit_atf.py +10 -0
+        e_entry = (start_hi << 32) + start_lo
+        p_addr = e_entry
+        p_data = bin[0x1c:]
+        if len(p_data) != init_sz:
+            raise ValueError("Invalid file '%s': size mismatch "
+                             "(expected %d, have %d)" % (filename, init_sz,
+                                                         len(p_data)))
+        segments.append((0, e_entry, p_addr, p_data))
+    else:
+        raise ValueError("Unknown format for TEE file '%s'" % filename)
Quoted 1 line

I see an 'output_header_v2' in your link [2], what about that?

v2 is useful only when the OP-TEE pager is used, in which case it is a matter of preference whether to use a single binary and have the loader split it as expected (tee.bin) or use separate binaries instead.

Quoted 14 lines

Historically (Jens please correct me if I'm wrong!), there was a single raw binary for OP-TEE: tee.bin, and no pager support. Then pager was added. The build would then create two separate files: tee-pager.bin and tee-pageable.bin (the latter would be zero sized with pager disabled). At the same time a header was introduced in tee.bin to indicate whether or not the file would contain a pageable section (tee.bin consisted in the header + tee-pager.bin + tee-pageable.bin). Later, and for reasons I don't remember exactly (related to the integration with TF-A IIRC), the header was written to its own file and the format of that header changed a bit. So we had tee-header_v2.bin, tee-pager_v2.bin, and tee-pageable_v2.bin. The previous tee-pager.bin and tee-pageable.bin were subsequently deprecated; but tee.bin wasn't. It is still generated today, unchanged with its "v1" header. Note, I'm not sure if tee-pager_v2.bin is different from tee-pager.bin (and same goes for the pageable part), but it doesn't really matter for this discussion.

Thanks for both explanations. I also kept looking at gen_tee_bin.py and
outputs from a few OP-TEE OS builds. If I got things right, I can do:

Quoted 6 lines
Quoted 4 lines

cat tee-header_v2.bin tee-pager_v2.bin tee-pageable_v2.bin >tee_v2.bin

and that would effectively be the same kind of file/image as tee.bin, just with a v2 header instead of v1, right?

Correct.

It could be worth checking how TF-A loads OP-TEE. The relevant code is
in https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/tree/lib/optee/optee_utils.c

Cheers,
Jens

#8 lore raw
Quoted 8 lines · Jens Wiklander
Quoted 6 lines · Jerome Forissier
Quoted 4 lines · Alper Nebi Yasak

Why I am asking these is that I'm hypothesizing we can put the unmodified 'tee.bin' (including header) and 'bl31.elf' (normally split into raw binary pieces) into the FIT, and then interpret them at runtime on the C side. Trying to figure out if it's a good idea.

Quoted 8 lines
Quoted 5 lines

Definitely sounds doable. It could help having more common code perhaps, and also simplify the content of the FIT and therefore the verification of hash and signatures.

[...]

It could be worth checking how TF-A loads OP-TEE. The relevant code is
in https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/tree/lib/optee/optee_utils.c

I think I'll eventually try to implement what I mentioned above, and the linked TF-A code looks like it would help a lot, thank you both!

Quoted 2 lines

Cheers,
Jens

#9 lore raw
In reply to Jerome Forissier

Hi,

Quoted 13 lines · Jerome Forissier

This commit adds support for the OP-TEE 'tee.bin' v1 format for Rockchip platforms.

Since OP-TEE 3.8.0, tee.bin contains meta-data in a proprietary format in addition to the ELF data. They are essential information for proper initialization of the TEE core, such as the size of the memory region covered by the TEE or a compact representation of runtime relocation data when ASLR is enabled.

With OP-TEE 3.8.0 onwards, 'tee.elf' MUST NOT be used and 'tee.bin' MUST be used instead. Ignoring this recommendation can lead to crashes as described in [3].

Quoted 7 lines
---
 arch/arm/mach-rockchip/make_fit_atf.py | 43 +++++++++++++++++++++-----
 1 file changed, 35 insertions(+), 8 deletions(-)

I have just seen the v2022.07-rc5 announcement. I'm wondering if this fix would be suitable for inclusion? Or is it too late in the RC cycle?

Thanks,

Signature · 1 line
Jerome