correct_sboot_setting.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """
  2. This module checks preloader project setting in order to
  3. avoid releasing incorrect configuration to customer.
  4. """
  5. import os
  6. import re
  7. import shutil
  8. import argparse
  9. import sys
  10. class CheckSetting(object):
  11. """
  12. CheckSetting class for checking project setting
  13. """
  14. def __init__(self):
  15. self.sboot_forbidden = "ATTR_SBOOT_DISABLE"
  16. self.usbdl_forbidden = "ATTR_SUSBDL_DISABLE"
  17. self.sboot_allow = "ATTR_SBOOT_ONLY_ENABLE_ON_SCHIP"
  18. self.usbdl_allow = "ATTR_SUSBDL_ONLY_ENABLE_ON_SCHIP"
  19. self.bypass_pattern = ["evb", "fpga", "tb"]
  20. def analyze_and_correct(self, in_path):
  21. """
  22. analyze and correct the project setting.
  23. """
  24. file_path = os.path.abspath(in_path)
  25. if not os.path.isfile(file_path):
  26. print "ERROR: " + in_path + " does not exist."
  27. sys.exit()
  28. file_name = os.path.basename(file_path)
  29. for elem in self.bypass_pattern:
  30. if re.search(elem, file_name):
  31. print "No need to check this project (Not phone project)."
  32. return
  33. file_dir = os.path.dirname(file_path)
  34. tmp_file = os.path.join(file_dir, file_name.split(".")[0] + "_temp." + file_name.split(".")[1])
  35. f_in = open(file_path,'r')
  36. f_out = open(tmp_file,'w')
  37. for line in f_in:
  38. if re.search(self.sboot_forbidden, line):
  39. f_out.write(line.replace(self.sboot_forbidden, self.sboot_allow))
  40. print "Replace " + line.strip() + " with " + line.replace(self.sboot_forbidden, self.sboot_allow).strip()
  41. elif re.search(self.usbdl_forbidden, line):
  42. f_out.write(line.replace(self.usbdl_forbidden, self.usbdl_allow))
  43. print "Replace " + line.strip() + " with " + line.replace(self.usbdl_forbidden, self.usbdl_allow).strip()
  44. else:
  45. f_out.write(line)
  46. f_in.close()
  47. f_out.close()
  48. os.remove(file_path)
  49. shutil.move(tmp_file, file_path)
  50. print "Check and correct successfully!!!"
  51. def main():
  52. """
  53. entry point for secure boot config check
  54. """
  55. get_setting_str = lambda arg: 'Not Set' if arg is None else arg
  56. parser = argparse.ArgumentParser(description='Secure boot configuration check script.')
  57. parser.add_argument('-i',
  58. dest='in_path',
  59. help='configuration file path',
  60. required = True)
  61. input_args = parser.parse_args()
  62. print "=========================================="
  63. print "in_path:" + get_setting_str(input_args.in_path)
  64. print "=========================================="
  65. check = CheckSetting()
  66. check.analyze_and_correct(input_args.in_path)
  67. if __name__ == '__main__':
  68. main()