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