1#! /usr/bin/env python3 2# 3# Copyright 2017 Linaro Limited 4# Copyright (c) 2018-2019, Arm Limited. 5# 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17 18from __future__ import print_function 19import os 20import re 21import argparse 22 23import sys 24import macro_parser 25import fileinput 26 27 28def do_flash(args): 29 image_value_re = re.compile(r"^\s*"+args.macro+r"\s*=\s*(.*)") 30 value = macro_parser.evaluate_macro(args.layout, image_value_re, 0, 1, 31 True) 32 if args.setting == 1: 33 begin_line="set "+args.begin 34 else: 35 begin_line=args.begin 36 37 for line in fileinput.input(args.infile, inplace=True): 38 if line.startswith(begin_line): 39 if args.division: 40 value = int(value/int(args.division)) 41 if args.phexa == 0: 42 line = begin_line+"="+str(value)+"\n" 43 else: 44 line = begin_line+"="+hex(value)+"\n" 45 sys.stdout.write(line) 46 47subcmds = { 48 'flash': do_flash, } 49 50 51def intparse(text): 52 """Parse a command line argument as an integer. 53 54 Accepts 0x and other prefixes to allow other bases to be used.""" 55 return int(text, 0) 56 57def args(): 58 parser = argparse.ArgumentParser() 59 subs = parser.add_subparsers(help='subcommand help', dest='subcmd') 60 61 62 flash = subs.add_parser('flash', help='modify flash script') 63 flash.add_argument("infile") 64 flash.add_argument('-l', '--layout', required=True, 65 help='Location of the file that contains preprocessed macros') 66 flash.add_argument('-m', '--macro', required =True, 67 help='macro symbol string to grep in preprocessed file') 68 flash.add_argument('-b', '--begin', required=True, 69 help='begin of line to replace ') 70 flash.add_argument('-s', '--setting',type=intparse,required=False,default=0, 71 help='search for window batch set variable') 72 flash.add_argument('-d', '--division', 73 required=False,type=intparse,default=0, 74 help='search for window batch set variable') 75 flash.add_argument('-p', '--phexa', 76 required=False,type=intparse,default=1, 77 help='print value in hexa') 78 79 args = parser.parse_args() 80 if args.subcmd is None: 81 print('Must specify a subcommand', file=sys.stderr) 82 sys.exit(1) 83 84 subcmds[args.subcmd](args) 85 86if __name__ == '__main__': 87 args() 88