|
| 1 | +# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +import argparse |
| 4 | +import re |
| 5 | +from typing import List |
| 6 | +from typing import Tuple |
| 7 | + |
| 8 | + |
| 9 | +def parse_services(secure_service_tbl: str) -> List[Tuple[int, str, int]]: |
| 10 | + services, service_ids = [], set() |
| 11 | + pattern = re.compile(r'^([0-9A-Fa-fXx]+)\s+\S+\s+(\S+)\s+(\d+)') |
| 12 | + |
| 13 | + with open(secure_service_tbl, 'r') as f: |
| 14 | + for line in f: |
| 15 | + if match := pattern.match(line): |
| 16 | + service_id = int(match.group(1), 0) |
| 17 | + if service_id in service_ids: |
| 18 | + raise ValueError(f'Duplicate service call ID found: 0x{service_id:X}') |
| 19 | + service_ids.add(service_id) |
| 20 | + services.append((service_id, match.group(2), int(match.group(3)))) |
| 21 | + |
| 22 | + return sorted(services, key=lambda x: x[0]) |
| 23 | + |
| 24 | + |
| 25 | +def generate_num_header(services: List[Tuple[int, str, int]], output_file: str) -> None: |
| 26 | + header = '''/** |
| 27 | + * THIS FILE WAS AUTOMATICALLY GENERATED. DO NOT EDIT! |
| 28 | + */ |
| 29 | +
|
| 30 | +#pragma once |
| 31 | +
|
| 32 | +#ifdef __cplusplus |
| 33 | +extern "C" { |
| 34 | +#endif |
| 35 | +
|
| 36 | +''' |
| 37 | + body = '\n'.join(f'#define SS_{name.upper()}\t{nr}' for nr, name, _ in services) |
| 38 | + footer = f'\n#define MAX_SECURE_SERVICES_ID\t{services[-1][0] + 1 if services else 0}\n' |
| 39 | + footer += f'#define SECURE_SERVICES_NUM\t{len(services)}\n\n' |
| 40 | + footer += '''typedef void (*secure_service_t)(void); |
| 41 | +typedef struct { int id; secure_service_t func; int nargs; } secure_service_entry_t; |
| 42 | +''' |
| 43 | + footer += '\n#ifdef __cplusplus\n}\n#endif\n' |
| 44 | + with open(output_file, 'w') as f: |
| 45 | + f.write(header + body + footer) |
| 46 | + |
| 47 | + |
| 48 | +def generate_dec_header(services: List[Tuple[int, str, int]], output_file: str) -> None: |
| 49 | + header = '''/** |
| 50 | + * THIS FILE WAS AUTOMATICALLY GENERATED. DO NOT EDIT! |
| 51 | + */ |
| 52 | +
|
| 53 | +#pragma once |
| 54 | +
|
| 55 | +#ifdef __cplusplus |
| 56 | +extern "C" { |
| 57 | +#endif |
| 58 | +''' |
| 59 | + body = '\n'.join(f'void _ss_{name}(void);' for _, name, _ in services) |
| 60 | + footer = '\n#ifdef __cplusplus\n}\n#endif\n' |
| 61 | + with open(output_file, 'w') as f: |
| 62 | + f.write(header + body + footer) |
| 63 | + |
| 64 | + |
| 65 | +def generate_table(services: List[Tuple[int, str, int]], output_file: str) -> None: |
| 66 | + header = '''/** |
| 67 | + * THIS FILE WAS AUTOMATICALLY GENERATED. DO NOT EDIT! |
| 68 | + */ |
| 69 | +
|
| 70 | +#pragma once |
| 71 | +''' |
| 72 | + body = '\n'.join(f'__SECURE_SERVICE({nr}, {name}, {nargs})' for nr, name, nargs in services) |
| 73 | + with open(output_file, 'w') as f: |
| 74 | + f.write(header + body) |
| 75 | + |
| 76 | + |
| 77 | +def generate_wrap_list(secure_service_tbl: str) -> None: |
| 78 | + pattern = re.compile(r'^[0-9A-Fa-fXx]+\s+IDF\s+(\S+)\s+\d+') |
| 79 | + with open(secure_service_tbl, 'r') as f: |
| 80 | + wrap_list = [f'-Wl,--wrap={match.group(1)}' for line in f if (match := pattern.match(line))] |
| 81 | + print(' '.join(wrap_list), end='') |
| 82 | + |
| 83 | + |
| 84 | +def main() -> None: |
| 85 | + parser = argparse.ArgumentParser(description='Generate secure service outputs') |
| 86 | + parser.add_argument('--wrap', action='store_true', help='Generate linker wrap options') |
| 87 | + parser.add_argument('secure_service_tbl', type=str, help='Path to secure service table file') |
| 88 | + parser.add_argument('output_files', nargs='*', help='Output files: [secure_service_num.h, secure_service_dec.h, secure_service.h]') |
| 89 | + |
| 90 | + args = parser.parse_args() |
| 91 | + |
| 92 | + if args.wrap: |
| 93 | + generate_wrap_list(args.secure_service_tbl) |
| 94 | + else: |
| 95 | + if len(args.output_files) != 3: |
| 96 | + parser.error('Missing output header files!') |
| 97 | + services = parse_services(args.secure_service_tbl) |
| 98 | + generate_num_header(services, args.output_files[0]) |
| 99 | + generate_dec_header(services, args.output_files[1]) |
| 100 | + generate_table(services, args.output_files[2]) |
| 101 | + |
| 102 | + |
| 103 | +if __name__ == '__main__': |
| 104 | + main() |
0 commit comments