You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.9 KiB

5 years ago
  1. import subprocess
  2. from pathlib import Path
  3. from patches import PATCH_DATA
  4. TOP_ROM_TYPE = "MX25L3206E/MX25L3208E"
  5. BOTTOM_ROM_TYPE = "MX25L6406E/MX25L6408E"
  6. PROGRAMMER = "ch341a_spi"
  7. UEFIPATCH_LOCATION = "./UEFIPatch_0.28.0_mac/UEFIPatch"
  8. LOG_FILE_NAME = "x230patch.log"
  9. class ROMError(Exception):
  10. pass
  11. def log(text):
  12. with open(LOG_FILE_NAME, 'a') as f:
  13. f.write(text)
  14. def check_rom(chip_loc):
  15. if chip_loc == "top":
  16. chip = TOP_ROM_TYPE
  17. elif chip_loc == "bottom":
  18. chip = BOTTOM_ROM_TYPE
  19. fr = subprocess.run(["flashrom", "-p", PROGRAMMER, "-c", chip], capture_output=True)
  20. log(fr.stdout.decode('ascii'))
  21. if fr.returncode != 0:
  22. raise ROMError("ROM not found.")
  23. def read_rom(chip_loc, save_dir, file_name):
  24. if chip_loc == "top":
  25. chip = TOP_ROM_TYPE
  26. elif chip_loc == "bottom":
  27. chip = BOTTOM_ROM_TYPE
  28. fr = subprocess.run(["flashrom", "-p", PROGRAMMER, "-c", chip, "-r", file_name], cwd=save_dir, capture_output=True)
  29. log(fr.stdout.decode('ascii'))
  30. if fr.returncode != 0:
  31. raise ROMError
  32. def write_rom(chip_loc, open_dir, file_name):
  33. if chip_loc == "top":
  34. chip = TOP_ROM_TYPE
  35. elif chip_loc == "bottom":
  36. chip = BOTTOM_ROM_TYPE
  37. fr = subprocess.run(["flashrom", "-p", PROGRAMMER, "-c", chip, "-w", file_name], cwd=open_dir, capture_output=True)
  38. log(fr.stdout.decode('ascii'))
  39. if fr.returncode != 0:
  40. raise ROMError
  41. def patch_rom(patches, open_path, save_path):
  42. patches_path = save_path.parent / "patches.txt"
  43. with open(patches_path, 'w') as patches_file:
  44. for patch in patches:
  45. patches_file.write(PATCH_DATA[patch])
  46. up = subprocess.run([UEFIPATCH_LOCATION, open_path, patches_path, "-o", save_path], capture_output=True)
  47. log(up.stdout.decode('ascii'))
  48. if up.returncode != 0:
  49. raise ROMError("Patching failed.")