{ "cells": [ { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "# Memory dump from GDB\n", "# (gdb) x/512xb 0x00804000\n", "memory_dump = \"\"\"\n", "0x804000: 0x39 0x92 0x9a 0xfe 0x80 0xff 0xc0 0xee\n", "0x804008: 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff\n", "0x804010: 0x10 0x40 0x80 0x00 0xff 0xff 0xff 0xff\n", "0x804018: 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff\n", "0x804020: 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff\n", "0x804028: 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff\n", "0x804030: 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff\n", "0x804038: 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import struct\n", "\n", "DEFAULTS = {\n", "\t'BOD33 Disable': 0x1,\n", "\t'BOD33 Level': 0x1C,\n", "\t'BOD33 Action': 0x1,\n", "\t'BOD33 Hysteresis': 0x2,\n", "\t'NVM BOOTPROT': 0xF,\n", "\t'SBLK': 0x0,\n", "\t'PSZ': 0x0,\n", "\t'RAM ECCDIS': 0x1,\n", "\t'WDT Enable': 0x0,\n", "\t'WDT Always-On': 0x0,\n", "\t'WDT Period': 0xB,\n", "\t'WDT Window': 0xB,\n", "\t'WDT EWOFFSET': 0xB,\n", "\t'WDT WEN': 0x0,\n", "\t'NVM LOCKS': 0xFFFFFFFF\n", "}\n", "\n", "# def extract_bits(value, offset, mask):\n", "# \treturn (value >> offset) & mask\n", "\n", "def extract_bits(value, start, end):\n", "\tmask = (1 << (end - start + 1)) - 1\n", "\treturn (value >> start) & mask\n", "\n", "\n", "def decode_user_row(memory_dump):\n", "\t# Extract only the hexadecimal values from the memory dump\n", "\thex_values = ''.join(line.split(':')[1].strip().replace(' ', '') for line in memory_dump.strip().split('\\n'))\n", "\thex_values = hex_values.replace('0x', '').replace('\\n', '')\n", "\t\n", "\t# Convert the cleaned hex string to bytes\n", "\tmemory_bytes = bytes.fromhex(hex_values)\n", "\t\n", "\t# Convert bytes to 32-bit words\n", "\t# words = struct.unpack('<8I', memory_bytes[:32])\n", "\twords = struct.unpack('<8I', memory_bytes[:32])\n", "\t\n", "\tsettings = {\n", "\t\t'BOD33 Disable': extract_bits(words[0], 0, 0),\n", "\t\t'BOD33 Level': extract_bits(words[0], 1, 8),\n", "\t\t'BOD33 Action': extract_bits(words[0], 9, 10),\n", "\t\t'BOD33 Hysteresis': extract_bits(words[0], 11, 14),\n", "\t\t'BOD12 Calibration': extract_bits(words[0], 15, 25),\n", "\t\t'NVM BOOTPROT': extract_bits(words[0], 26, 29),\n", "\t\t'SBLK': extract_bits(words[1], 0, 3),\n", "\t\t'PSZ': extract_bits(words[1], 4, 6),\n", "\t\t'RAM ECCDIS': extract_bits(words[1], 7, 7),\n", "\t\t'WDT Enable': extract_bits(words[1], 16, 16),\n", "\t\t'WDT Always-On': extract_bits(words[1], 17, 17),\n", "\t\t'WDT Period': extract_bits(words[1], 18, 21),\n", "\t\t'WDT Window': extract_bits(words[1], 22, 25),\n", "\t\t'WDT EWOFFSET': extract_bits(words[1], 26, 29),\n", "\t\t'WDT WEN': extract_bits(words[1], 30, 30),\n", "\t\t'NVM LOCKS': words[2],\n", "\t\t'User Page': words[3]\n", "\t}\n", "\treturn settings\n", "\n", "\n", "\n", "def display_user_row_settings(settings):\n", "\tprint(\"SAME54 User Row Settings (Current vs Default):\")\n", "\tprint(\"-\" * 50)\n", "\tfor key, value in settings.items():\n", "\t\tif key in DEFAULTS:\n", "\t\t\tif value != DEFAULTS[key]:\n", "\t\t\t\tprint(f\"{key:15}: 0x{value:X} (Default: 0x{DEFAULTS[key]:X}) *DIFFERENT*\")\n", "\t\t\telse:\n", "\t\t\t\tprint(f\"{key:15}: 0x{value:X} (Default: 0x{DEFAULTS[key]:X})\")\n", "\t\telse:\n", "\t\t\tprint(f\"{key:15}: 0x{value:X}\")\n", "\n", "\n", "\n", "# Decode the memory dump\n", "decoded_settings = decode_user_row(memory_dump)\n", "\n", "# Display the formatted settings\n", "display_user_row_settings(decoded_settings)\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.3" } }, "nbformat": 4, "nbformat_minor": 2 }