From adb5060ecb10285ebaa4299dc2e8df014bae26fb Mon Sep 17 00:00:00 2001 From: "AL Musaffar, Yazen" Date: Tue, 15 Apr 2025 07:47:43 +0800 Subject: [PATCH] Fix binary dump Change-Id: I3d91a7d33fc6860eea27fb396937139fe229daeb Signed-off-by: AL Musaffar, Yazen [ROCm/amdsmi commit: 9b66bc56902e71a50cead36bf2c3b36fe2b1e68a] --- projects/amdsmi/amdsmi_cli/amdsmi_helpers.py | 35 +++++++++++--------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/projects/amdsmi/amdsmi_cli/amdsmi_helpers.py b/projects/amdsmi/amdsmi_cli/amdsmi_helpers.py index d1f52d84bb..a78b48b24a 100644 --- a/projects/amdsmi/amdsmi_cli/amdsmi_helpers.py +++ b/projects/amdsmi/amdsmi_cli/amdsmi_helpers.py @@ -1063,21 +1063,26 @@ class AMDSMIHelpers(): logging.warning(msg) - def hexdump(self, data, size, filepath): + def write_binary(self, data, size, filepath): """ - Converts binary data to a hex dump string, similar to the hexdump utility. - """ - def to_printable_ascii(byte): - return chr(byte) if 32 <= byte <= 126 else "." + Writes binary data directly to a file. - with open(filepath, 'w') as f: - offset = 0 - while offset < size: - chunk = data[offset:offset + 16] - hex_values = " ".join(f"{byte:02x}" for byte in chunk) - ascii_values = "".join(to_printable_ascii(byte) for byte in chunk) - print(f"{offset:08x} {hex_values:<48} |{ascii_values}|", file=f) - offset += 16 + Parameters: + data: Either a bytes object or a list of integers representing binary data. + size (int): The number of bytes to write. + filepath: The path to the output file. + """ + with open(filepath, 'wb') as f: + if isinstance(data, list): + try: + # Attempt to convert the list to a bytes object. + data_bytes = bytes(data[:size]) + except ValueError: + # If any value is out of range, force them into 0-255. + data_bytes = bytes(x % 256 for x in data[:size]) + else: + data_bytes = data[:size] + f.write(data_bytes) def dump_entries(self, folder, entries, cper_data): @@ -1106,7 +1111,7 @@ class AMDSMIHelpers(): cper_data_file = f"{prefix}_{self.get_cper_count()}.cper" cper_data_file_path = folder / cper_data_file - self.hexdump(cper_data[entry_index]["bytes"], cper_data[entry_index]["size"], cper_data_file_path) + self.write_binary(cper_data[entry_index]["bytes"], cper_data[entry_index]["size"], cper_data_file_path) try: with output_path.open("w") as f: @@ -1119,4 +1124,4 @@ class AMDSMIHelpers(): else: print(json.dumps(entries, indent=2, default=lambda o: o.decode('utf-8') if isinstance(o, bytes) else o)) - self.increment_cper_count() \ No newline at end of file + self.increment_cper_count()