Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prepare file option to checksum & patch file #130

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VW_Flash.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def input_blocks_from_frf(frf_path: str) -> dict[str, BlockData]:
input_blocks = input_blocks_from_frf(args.frf)

if args.input_bin:
input_blocks = binfile.blocks_from_bin(args.input_bin, flash_info)
input_blocks = binfile.blocks_from_bin(args.input_bin, flash_info, args.haldex)
logger.info(binfile.input_block_info(input_blocks, flash_info))

# build the dict that's used to proces the blocks
Expand Down
74 changes: 73 additions & 1 deletion VW_Flash_GUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,54 @@ def update_callback(self, **kwargs):
else:
wx.CallAfter(self.threaded_callback, kwargs["logger_status"], "0", 0)

def prepare_file(self, selected_file, output_dir):
should_patch_cboot = False

if module_selection_is_dq250(self.module_choice.GetSelection()):
flash_utils = dsg_flash_utils
elif module_selection_is_dq381(self.module_choice.GetSelection()):
flash_utils = dq381_flash_utils
elif module_selection_is_haldex(self.module_choice.GetSelection()):
flash_utils = haldex_flash_utils
else:
flash_utils = simos_flash_utils
should_patch_cboot = True

input_bytes = Path(selected_file).read_bytes()
if len(input_bytes) != self.flash_info.binfile_size:
self.feedback_text.AppendText(
"File did not appear to be a valid BIN for "
+ self.module_choice.GetString(self.module_choice.GetSelection())
+ "\n"
)
return

self.progress_bar.Pulse()

self.feedback_text.AppendText(
"Starting to prepare the following file : "
+ Path(selected_file).name
+ "\n"
)

input_blocks = binfile.blocks_from_bin(selected_file, self.flash_info)
output_blocks = flash_utils.checksum_and_patch_blocks(
self.flash_info, input_blocks, should_patch_cboot=should_patch_cboot
)
output_file = Path(output_dir, "PATCHED_" + Path(selected_file).name)
outfile_data = binfile.bin_from_blocks(output_blocks, self.flash_info)
output_file.write_bytes(
outfile_data
)

self.feedback_text.AppendText(
"File prepared and saved as : "
+ output_file.name
+ "\n"
)

self.progress_bar.SetValue(0)

def flash_bin(self, get_info=True, should_patch_cboot=False):
(interface, interface_path) = split_interface_name(self.options["interface"])
if module_selection_is_dq250(self.module_choice.GetSelection()):
Expand Down Expand Up @@ -721,6 +769,17 @@ def create_menu(self):
source=extract_frf_menu_item,
)

prepare_file_menu_item = file_menu.Append(
wx.ID_ANY,
"Prepare File...",
"Checksums and patches a file for flashing to the selected ECU with a different tool",
)
self.Bind(
event=wx.EVT_MENU,
handler=self.on_select_prepare_file,
source=prepare_file_menu_item,
)

unlock_ecu_menu_item = file_menu.Append(
wx.ID_ANY,
"Unlock ECU...",
Expand Down Expand Up @@ -834,6 +893,20 @@ def on_select_unlock(self, event):
self.panel.flash_unlock(self.selected_unlock)
dlg.Destroy()

def on_select_prepare_file(self, event):
title = "Choose a file to prepare:"
dlg = wx.FileDialog(self, title, style=wx.FD_DEFAULT_STYLE, wildcard="*.bin")
if dlg.ShowModal() == wx.ID_OK:
file_path = dlg.GetPath()
dlg.Destroy()
title = "Choose an output directory:"
dlg = wx.DirDialog(self, title)
if dlg.ShowModal() == wx.ID_OK:
output_dir = dlg.GetPath()
self.panel.prepare_file(file_path, output_dir)

dlg.Destroy()

def on_select_stmin(self, event):
title = "Change STMIN_TX:"
stmin_override = self.panel.options.get("stmin_override", DEFAULT_STMIN)
Expand Down Expand Up @@ -887,7 +960,6 @@ def on_stop_logger(self, event):
self.hsl_logger.stop()
self.hsl_logger = None


def on_select_interface(self, event):
progress_dialog = wx.ProgressDialog(
"Scanning for devices...",
Expand Down
29 changes: 23 additions & 6 deletions lib/binfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,26 @@ def blocks_from_data(data: bytes, flash_info: FlashInfo, haldex_hack: bool = Fal
filename = flash_info.block_names_frf[i]
block_length = flash_info.block_lengths[i]

if haldex_hack and filename == 'FD_1DATA':
logger.info('Dynamically getting CAL length for Haldex...')
length = data[ (flash_info.binfile_layout[i] + 0x14) : (flash_info.binfile_layout[i] + 0x18)]
logger.info('Set Haldex length to: '+length.hex())
block_length = struct.unpack('<I', length)[0]
# TODO: Make this less awful

if haldex_hack:
if filename == 'FD_1DATA':
logger.info('Dynamically getting CAL length for Haldex...')
length = data[ (flash_info.binfile_layout[i] + 0x14) : (flash_info.binfile_layout[i] + 0x18)]
logger.info('Set Haldex CAL length to: '+length.hex())
block_length = struct.unpack('<I', length)[0]

if filename == 'FD_2DATA':
logger.info('Dynamically getting ASW length for Haldex...')
length = data[ (flash_info.binfile_layout[i] + 0x204) : (flash_info.binfile_layout[i] + 0x208)]
logger.info('Set Haldex ASW length to: '+length.hex())
block_length = struct.unpack('<I', length)[0]

if filename == 'FD_3DATA':
logger.info('Dynamically getting VERSION length for Haldex...')
length = data[ (flash_info.binfile_layout[i] + 0x4) : (flash_info.binfile_layout[i] + 0x8)]
logger.info('Set Haldex VERSION length to: '+length.hex())
block_length = struct.unpack('<I', length)[0]

input_blocks[filename] = BlockData(
i,
Expand All @@ -147,6 +162,8 @@ def blocks_from_data(data: bytes, flash_info: FlashInfo, haldex_hack: bool = Fal
],
)

input_blocks = filter_blocks(input_blocks, flash_info)
# TODO: Potentially remove this and figure it out properly
if not haldex_hack:
input_blocks = filter_blocks(input_blocks, flash_info)

return input_blocks
4 changes: 3 additions & 1 deletion lib/haldex_flash_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ def flash_bin(
prepared_blocks = prepare_blocks(flash_info, input_blocks, callback)

# Manually override flash_info
flash_info.block_lengths[2] = len(prepared_blocks['FD_1DATA'].block_encrypted_bytes)
flash_info.block_lengths[2] = len(prepared_blocks["FD_1DATA"].block_encrypted_bytes)
flash_info.block_lengths[3] = len(prepared_blocks["FD_2DATA"].block_encrypted_bytes)
flash_info.block_lengths[4] = len(prepared_blocks["FD_3DATA"].block_encrypted_bytes)

flash_uds.flash_blocks(
flash_info=flash_info,
Expand Down
Loading