Fix binary dump

Change-Id: I3d91a7d33fc6860eea27fb396937139fe229daeb
Signed-off-by: AL Musaffar, Yazen <Yazen.ALMusaffar@amd.com>


[ROCm/amdsmi commit: 9b66bc5690]
This commit is contained in:
AL Musaffar, Yazen
2025-04-15 07:47:43 +08:00
committed by Arif, Maisam
parent 3e419ee84b
commit adb5060ecb
+19 -14
View File
@@ -1063,21 +1063,26 @@ class AMDSMIHelpers():
logging.warning(msg) 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. Writes binary data directly to a file.
"""
def to_printable_ascii(byte):
return chr(byte) if 32 <= byte <= 126 else "."
with open(filepath, 'w') as f: Parameters:
offset = 0 data: Either a bytes object or a list of integers representing binary data.
while offset < size: size (int): The number of bytes to write.
chunk = data[offset:offset + 16] filepath: The path to the output file.
hex_values = " ".join(f"{byte:02x}" for byte in chunk) """
ascii_values = "".join(to_printable_ascii(byte) for byte in chunk) with open(filepath, 'wb') as f:
print(f"{offset:08x} {hex_values:<48} |{ascii_values}|", file=f) if isinstance(data, list):
offset += 16 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): 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 = f"{prefix}_{self.get_cper_count()}.cper"
cper_data_file_path = folder / cper_data_file 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: try:
with output_path.open("w") as f: with output_path.open("w") as f: