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.

77 lines
3.0 KiB

5 years ago
  1. from pathlib import Path
  2. import filecmp
  3. from romtools import *
  4. def cli_join_rom(clean_path):
  5. print("Joining dumps in to full firmware ROM... ", end="")
  6. with open(clean_path / "bottom.bin", "rb") as bottom, open(clean_path / "top.bin", "rb") as top, open(clean_path / "clean.rom", "wb+") as rom:
  7. rom.write(bottom.read() + top.read())
  8. print("Done.")
  9. def cli_split_rom(patched_path):
  10. print("Splitting full rom in to images for each chip... ", end="")
  11. with open(patched_path / "bottom.bin", "wb+") as bottom, open(patched_path / "top.bin", "wb+") as top, open(patched_path / "patched.rom", "rb") as rom:
  12. rom_array = rom.read()
  13. bottom.write(rom_array[:0x800000])
  14. top.write(rom_array[0x800000:])
  15. print("Done.")
  16. def cli_connect_rom(chip_loc):
  17. input(f"Please connect programming clip to {chip_loc} flash chip. Press Enter when done...")
  18. cli_reconnect_rom(chip_loc)
  19. def cli_reconnect_rom(chip_loc):
  20. print("Checking if ROM is connected... ", end="")
  21. try:
  22. check_rom(chip_loc)
  23. print("ROM is reachable.")
  24. except:
  25. response = input("ROM is not reachable. Try again? (Y/q): ")
  26. if response in ['q', 'Q']:
  27. raise ROMError("Could not connect to ROM. User elected to quit.")
  28. else:
  29. cli_reconnect_rom(chip_loc)
  30. def cli_read_rom(chip_loc, clean_path, verify=True):
  31. files = [chip_loc + ".bin", chip_loc + "_2.bin"] if verify else [chip_loc + ".bin"]
  32. for count, file_name in enumerate(files):
  33. print(f"Reading {chip_loc} ROM, pass {count+1} of {len(files)}... ", end="", flush=True)
  34. read_rom(chip_loc, clean_path, file_name)
  35. print("Success.")
  36. if verify:
  37. print("Comparing ROM files... ", end="")
  38. if filecmp.cmp(clean_path / files[0], clean_path / files[1]):
  39. print("Files match.")
  40. else:
  41. raise ROMError("ROM files do not match. Read the log and try again.")
  42. else:
  43. print("ROM file will not be verified, proceed at your own risk!")
  44. print(f"{chip_loc.capitalize()} ROM read successfully.")
  45. def cli_write_rom(chip_loc, patched_path, verify=True):
  46. file_name = chip_loc + '_patched.bin'
  47. print(f"Writing {chip_loc} ROM (takes a while)... ", end="", flush=True)
  48. write_rom(chip_loc, patched_path, file_name)
  49. print("Success.")
  50. print(f"{chip_loc.capitalize()} ROM written successfully.")
  51. def cli_choose_patches():
  52. which_patches = input("Patch (W)LAN whitelist, (A)dvanced menu or (B)oth? ")
  53. if which_patches.strip()[0] in ['w', 'W']:
  54. patches = ["wifi"]
  55. elif which_patches.strip()[0] in ['a', 'A']:
  56. patches = ["advanced"]
  57. elif which_patches.strip()[0] in ['b', 'B']:
  58. patches = ["wifi", "advanced"]
  59. else:
  60. raise ValueError("Selection invalid.")
  61. return patches
  62. def cli_patch_rom(patches, clean_path, patched_path):
  63. patch_rom(patches, clean_path / "top.bin", patched_path / "top_patched.bin")
  64. print("ROM patched successfully.")