Manish Pandey | 3f90ad7 | 2020-01-14 11:52:05 +0000 | [diff] [blame] | 1 | #!/usr/bin/python3 |
Anders Dellien | 836c64f | 2022-01-07 13:26:29 +0000 | [diff] [blame] | 2 | # Copyright (c) 2020-2022, Arm Limited. All rights reserved. |
Manish Pandey | 3f90ad7 | 2020-01-14 11:52:05 +0000 | [diff] [blame] | 3 | # |
| 4 | # SPDX-License-Identifier: BSD-3-Clause |
| 5 | |
| 6 | """ |
| 7 | This script is invoked by Make system and generates secure partition makefile. |
| 8 | It expects platform provided secure partition layout file which contains list |
| 9 | of Secure Partition Images and Partition manifests(PM). |
| 10 | Layout file can exist outside of TF-A tree and the paths of Image and PM files |
| 11 | must be relative to it. |
| 12 | |
| 13 | This script parses the layout file and generates a make file which updates |
Manish Pandey | aaaeb31 | 2020-05-26 23:59:36 +0100 | [diff] [blame] | 14 | FDT_SOURCES, FIP_ARGS, CRT_ARGS and SPTOOL_ARGS which are used in later build |
| 15 | steps. |
Manish Pandey | 3f90ad7 | 2020-01-14 11:52:05 +0000 | [diff] [blame] | 16 | This script also gets SP "uuid" from parsing its PM and converting it to a |
| 17 | standard format. |
| 18 | |
| 19 | param1: Generated mk file "sp_gen.mk" |
| 20 | param2: "SP_LAYOUT_FILE", json file containing platform provided information |
| 21 | param3: plat out directory |
Ruari Phipps | 571ed6b | 2020-07-24 16:20:57 +0100 | [diff] [blame] | 22 | param4: CoT parameter |
Manish Pandey | 3f90ad7 | 2020-01-14 11:52:05 +0000 | [diff] [blame] | 23 | |
| 24 | Generated "sp_gen.mk" file contains triplet of following information for each |
| 25 | Secure Partition entry |
| 26 | FDT_SOURCES += sp1.dts |
| 27 | SPTOOL_ARGS += -i sp1.bin:sp1.dtb -o sp1.pkg |
| 28 | FIP_ARGS += --blob uuid=XXXXX-XXX...,file=sp1.pkg |
Manish Pandey | aaaeb31 | 2020-05-26 23:59:36 +0100 | [diff] [blame] | 29 | CRT_ARGS += --sp-pkg1 sp1.pkg |
Manish Pandey | 3f90ad7 | 2020-01-14 11:52:05 +0000 | [diff] [blame] | 30 | |
| 31 | A typical SP_LAYOUT_FILE file will look like |
| 32 | { |
| 33 | "SP1" : { |
| 34 | "image": "sp1.bin", |
| 35 | "pm": "test/sp1.dts" |
| 36 | }, |
| 37 | |
| 38 | "SP2" : { |
| 39 | "image": "sp2.bin", |
| 40 | "pm": "test/sp2.dts" |
| 41 | } |
| 42 | |
| 43 | ... |
| 44 | } |
| 45 | |
| 46 | """ |
| 47 | |
| 48 | import getopt |
| 49 | import json |
| 50 | import os |
| 51 | import re |
| 52 | import sys |
| 53 | import uuid |
| 54 | |
| 55 | with open(sys.argv[2],'r') as in_file: |
| 56 | data = json.load(in_file) |
| 57 | json_file = os.path.abspath(sys.argv[2]) |
| 58 | json_dir = os.path.dirname(json_file) |
Grant Likely | 388248a | 2020-07-30 08:50:10 +0100 | [diff] [blame] | 59 | gen_file = os.path.abspath(sys.argv[1]) |
| 60 | out_dir = os.path.abspath(sys.argv[3]) |
Manish Pandey | 3f90ad7 | 2020-01-14 11:52:05 +0000 | [diff] [blame] | 61 | dtb_dir = out_dir + "/fdts/" |
Ruari Phipps | 571ed6b | 2020-07-24 16:20:57 +0100 | [diff] [blame] | 62 | MAX_SP = 8 |
| 63 | dualroot = sys.argv[4].lower() == "dualroot" |
| 64 | split = int(MAX_SP / 2) |
Manish Pandey | 3f90ad7 | 2020-01-14 11:52:05 +0000 | [diff] [blame] | 65 | print(dtb_dir) |
Ruari Phipps | 571ed6b | 2020-07-24 16:20:57 +0100 | [diff] [blame] | 66 | platform_count = 1 |
| 67 | sip_count = 1 |
Manish Pandey | 3f90ad7 | 2020-01-14 11:52:05 +0000 | [diff] [blame] | 68 | |
| 69 | with open(gen_file, 'w') as out_file: |
Manish Pandey | aaaeb31 | 2020-05-26 23:59:36 +0100 | [diff] [blame] | 70 | for idx, key in enumerate(data.keys()): |
Manish Pandey | 3f90ad7 | 2020-01-14 11:52:05 +0000 | [diff] [blame] | 71 | |
Ruari Phipps | 571ed6b | 2020-07-24 16:20:57 +0100 | [diff] [blame] | 72 | pkg_num = idx + 1 |
| 73 | |
| 74 | if (pkg_num > MAX_SP): |
| 75 | print("WARNING: Too many secure partitions\n") |
| 76 | exit(-1) |
| 77 | |
| 78 | if dualroot: |
| 79 | owner = data[key].get('owner') |
| 80 | if owner == "Plat": |
| 81 | if (platform_count > split): |
| 82 | print("WARNING: Maximum Secure partitions by Plat " + |
| 83 | "have been exceeded (" + str(split) + ")\n") |
| 84 | exit(-1) |
| 85 | pkg_num = split + platform_count |
| 86 | platform_count += 1 |
| 87 | elif (sip_count > split): |
| 88 | print("WARNING: Maximum Secure partitions by SiP " + |
| 89 | "have been exceeded (" + str(split) + ")\n") |
| 90 | exit(-1) |
| 91 | else: |
| 92 | pkg_num = sip_count |
| 93 | sip_count += 1 |
| 94 | |
Manish Pandey | 3f90ad7 | 2020-01-14 11:52:05 +0000 | [diff] [blame] | 95 | """ |
| 96 | Append FDT_SOURCES |
| 97 | """ |
| 98 | dts = os.path.join(json_dir, data[key]['pm']) |
| 99 | dtb = dtb_dir + os.path.basename(data[key]['pm'][:-1] + "b") |
| 100 | out_file.write("FDT_SOURCES += " + dts + "\n") |
| 101 | |
| 102 | """ |
| 103 | Update SPTOOL_ARGS |
| 104 | """ |
| 105 | dst = out_dir + "/" + key + ".pkg" |
| 106 | src = [ json_dir + "/" + data[key]['image'] , dtb ] |
| 107 | out_file.write("SPTOOL_ARGS += -i " + ":".join(src) + " -o " + dst + "\n") |
| 108 | |
| 109 | """ |
| 110 | Extract uuid from partition manifest |
| 111 | """ |
| 112 | pm_file = open(dts) |
Manish Pandey | 3f90ad7 | 2020-01-14 11:52:05 +0000 | [diff] [blame] | 113 | for line in pm_file: |
Olivier Deprez | 1890984 | 2021-05-11 09:43:37 +0200 | [diff] [blame] | 114 | if "uuid" in line: |
| 115 | # re.findall returns a list of string tuples. |
| 116 | # uuid_hex is the first item in this list representing the four |
| 117 | # uuid hex integers from the manifest uuid field. The heading |
| 118 | # '0x' of the hexadecimal representation is stripped out. |
| 119 | # e.g. uuid = <0x1e67b5b4 0xe14f904a 0x13fb1fb8 0xcbdae1da>; |
| 120 | # uuid_hex = ('1e67b5b4', 'e14f904a', '13fb1fb8', 'cbdae1da') |
| 121 | uuid_hex = re.findall(r'0x([0-9a-f]+) 0x([0-9a-f]+) 0x([0-9a-f]+) 0x([0-9a-f]+)', line)[0]; |
| 122 | |
| 123 | # uuid_hex is a list of four hex string values |
| 124 | if len(uuid_hex) != 4: |
| 125 | print("ERROR: malformed UUID") |
| 126 | exit(-1) |
Manish Pandey | 3f90ad7 | 2020-01-14 11:52:05 +0000 | [diff] [blame] | 127 | |
Olivier Deprez | 1890984 | 2021-05-11 09:43:37 +0200 | [diff] [blame] | 128 | # The uuid field in SP manifest is the little endian representation |
| 129 | # mapped to arguments as described in SMCCC section 5.3. |
| 130 | # Convert each unsigned integer value to a big endian representation |
| 131 | # required by fiptool. |
| 132 | y=list(map(bytearray.fromhex, uuid_hex)) |
| 133 | z=(int.from_bytes(y[0], byteorder='little', signed=False), |
| 134 | int.from_bytes(y[1], byteorder='little', signed=False), |
| 135 | int.from_bytes(y[2], byteorder='little', signed=False), |
| 136 | int.from_bytes(y[3], byteorder='little', signed=False)) |
Anders Dellien | 836c64f | 2022-01-07 13:26:29 +0000 | [diff] [blame] | 137 | uuid_std = uuid.UUID(f'{z[0]:08x}{z[1]:08x}{z[2]:08x}{z[3]:08x}') |
Manish Pandey | 3f90ad7 | 2020-01-14 11:52:05 +0000 | [diff] [blame] | 138 | |
| 139 | """ |
| 140 | Append FIP_ARGS |
| 141 | """ |
Olivier Deprez | 1890984 | 2021-05-11 09:43:37 +0200 | [diff] [blame] | 142 | out_file.write("FIP_ARGS += --blob uuid=" + str(uuid_std) + ",file=" + dst + "\n") |
Manish Pandey | aaaeb31 | 2020-05-26 23:59:36 +0100 | [diff] [blame] | 143 | |
| 144 | """ |
| 145 | Append CRT_ARGS |
| 146 | """ |
Ruari Phipps | 571ed6b | 2020-07-24 16:20:57 +0100 | [diff] [blame] | 147 | |
| 148 | out_file.write("CRT_ARGS += --sp-pkg" + str(pkg_num) + " " + dst + "\n") |
Manish Pandey | 3f90ad7 | 2020-01-14 11:52:05 +0000 | [diff] [blame] | 149 | out_file.write("\n") |