|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +/******************************************************************************* |
| 5 | +* Ledger SDK |
| 6 | +* (c) 2022 Ledger |
| 7 | +* |
| 8 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +* you may not use this file except in compliance with the License. |
| 10 | +* You may obtain a copy of the License at |
| 11 | +* |
| 12 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +* |
| 14 | +* Unless required by applicable law or agreed to in writing, software |
| 15 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +* See the License for the specific language governing permissions and |
| 18 | +* limitations under the License. |
| 19 | +/******************************************************************************* |
| 20 | +""" |
| 21 | + |
| 22 | +import sys |
| 23 | +import argparse |
| 24 | +import tomli_w |
| 25 | + |
| 26 | +def auto_int(x): |
| 27 | + return int(x, 0) |
| 28 | + |
| 29 | +def get_argparser(): |
| 30 | + parser = argparse.ArgumentParser(description="Load an app onto the device from a hex file.") |
| 31 | + parser.add_argument("-output", help="File where the manifest is saved", required=True) |
| 32 | + parser.add_argument("--targetId", help="The device's target ID (default is Ledger Blue)", type=auto_int, default=0x31000002) |
| 33 | + parser.add_argument("--apiLevel", help="Set the API level of the SDK used to build the app", type=auto_int, default=0) |
| 34 | + parser.add_argument("--fileName", help="The application hex file to be loaded onto the device", required=True) |
| 35 | + parser.add_argument("--curve", help="""A curve on which BIP 32 derivation is locked ("secp256k1", "secp256r1", "ed25519" or "bls12381g1"), can be repeated""", action='append') |
| 36 | + parser.add_argument("--path", help="""A BIP 32 path to which derivation is locked (format decimal a'/b'/c), can be repeated""", action='append') |
| 37 | + parser.add_argument("--appName", help="The name to give the application after loading it", required=True) |
| 38 | + parser.add_argument("--appFlags", help="The application flags", type=auto_int, default=0) |
| 39 | + parser.add_argument("--dataSize", help="The code section's size in the provided hex file (to separate data from code, if not provided the whole allocated NVRAM section for the application will remain readonly.", type=auto_int) |
| 40 | + parser.add_argument("--appVersion", help="The application version (as a string)") |
| 41 | + parser.add_argument("--gif", help="The gif icon") |
| 42 | + |
| 43 | + # Not used - to be compatible with python3 ledgerblue |
| 44 | + parser.add_argument("--icon", help="The icon content to use (hex encoded)") |
| 45 | + parser.add_argument("--targetVersion", help="Set the chip target version") |
| 46 | + parser.add_argument("--path_slip21", help="""A SLIP 21 path to which derivation is locked""", action='append') |
| 47 | + parser.add_argument("--signature", help="A signature of the application (hex encoded)") |
| 48 | + parser.add_argument("--signApp", help="Sign application with provided signPrivateKey", action='store_true') |
| 49 | + parser.add_argument("--bootAddr", help="The application's boot address", type=auto_int) |
| 50 | + parser.add_argument("--rootPrivateKey", help="""The Signer private key used to establish a Secure Channel (otherwise |
| 51 | +a random one will be generated)""") |
| 52 | + parser.add_argument("--signPrivateKey", help="Set the private key used to sign the loaded app") |
| 53 | + parser.add_argument("--apdu", help="Display APDU log", action='store_true') |
| 54 | + parser.add_argument("--deployLegacy", help="Use legacy deployment API", action='store_true') |
| 55 | + parser.add_argument("--apilevel", help="Use given API level when interacting with the device", type=auto_int, default=10) |
| 56 | + parser.add_argument("--delete", help="Delete the app with the same name before loading the provided one", action='store_true') |
| 57 | + parser.add_argument("--params", help="Store icon and install parameters in a parameter section before the code", action='store_true') |
| 58 | + parser.add_argument("--tlv", help="Use install parameters for all variable length parameters", action='store_true') |
| 59 | + parser.add_argument("--offline", help="Request to only output application load APDUs into given filename") |
| 60 | + parser.add_argument("--offlineText", help="Request to only output application load APDUs into given filename in text mode", action='store_true') |
| 61 | + parser.add_argument("--installparamsSize", help="The loaded install parameters section size (when parameters are already included within the .hex file.", type=auto_int) |
| 62 | + parser.add_argument("--tlvraw", help="Add a custom install param with the hextag:hexvalue encoding", action='append') |
| 63 | + parser.add_argument("--dep", help="Add a dependency over an appname[:appversion]", action='append') |
| 64 | + parser.add_argument("--nocrc", help="Skip CRC generation when loading", action='store_true') |
| 65 | + |
| 66 | + return parser |
| 67 | + |
| 68 | +def main(): |
| 69 | + args = get_argparser().parse_args() |
| 70 | + manifest = {} |
| 71 | + manifest["name"] = args.appName |
| 72 | + if args.appVersion: |
| 73 | + manifest["version"] = args.appVersion |
| 74 | + |
| 75 | + if args.targetId: |
| 76 | + target = hex(args.targetId) |
| 77 | + manifest[target] = {} |
| 78 | + manifest[target]["binary"] = args.fileName |
| 79 | + if args.gif: |
| 80 | + manifest[target]["icon"] = args.gif |
| 81 | + if args.appFlags: |
| 82 | + manifest[target]["flags"] = args.appFlags |
| 83 | + manifest[target]["derivationPath"] = {} |
| 84 | + if args.curve: |
| 85 | + manifest[target]["derivationPath"]["curves"] = args.curve |
| 86 | + if args.path: |
| 87 | + manifest[target]["derivationPath"]["paths"] = args.path |
| 88 | + if args.apiLevel: |
| 89 | + manifest[target]["apiLevel"] = str(args.apiLevel) |
| 90 | + if args.dataSize: |
| 91 | + manifest[target]["dataSize"] = args.dataSize |
| 92 | + |
| 93 | + with open(args.output, 'wb') as f: |
| 94 | + tomli_w.dump(manifest, f) |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + main() |
0 commit comments