|
| 1 | +/*********************************************************************************************************************** |
| 2 | +* * |
| 3 | +* ANTIKERNEL v0.1 * |
| 4 | +* * |
| 5 | +* Copyright (c) 2012-2018 Andrew D. Zonenberg * |
| 6 | +* All rights reserved. * |
| 7 | +* * |
| 8 | +* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the * |
| 9 | +* following conditions are met: * |
| 10 | +* * |
| 11 | +* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the * |
| 12 | +* following disclaimer. * |
| 13 | +* * |
| 14 | +* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the * |
| 15 | +* following disclaimer in the documentation and/or other materials provided with the distribution. * |
| 16 | +* * |
| 17 | +* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products * |
| 18 | +* derived from this software without specific prior written permission. * |
| 19 | +* * |
| 20 | +* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * |
| 21 | +* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL * |
| 22 | +* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * |
| 23 | +* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR * |
| 24 | +* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * |
| 25 | +* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * |
| 26 | +* POSSIBILITY OF SUCH DAMAGE. * |
| 27 | +* * |
| 28 | +***********************************************************************************************************************/ |
| 29 | + |
| 30 | +/** |
| 31 | + @file |
| 32 | + @author Andrew D. Zonenberg |
| 33 | + @brief ARM Cortex-M Flash Patch/Breakpoint |
| 34 | + */ |
| 35 | +#include "jtaghal.h" |
| 36 | +#include "ARMAPBDevice.h" |
| 37 | +#include "ARMFlashPatchBreakpoint.h" |
| 38 | + |
| 39 | +using namespace std; |
| 40 | + |
| 41 | +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 42 | +// Construction / destruction |
| 43 | + |
| 44 | +ARMFlashPatchBreakpoint::ARMFlashPatchBreakpoint( |
| 45 | + ARMv7MProcessor* cpu, |
| 46 | + ARMDebugMemAccessPort* ap, |
| 47 | + uint32_t address, |
| 48 | + ARMDebugPeripheralIDRegisterBits idreg) |
| 49 | + : ARMCoreSightDevice(ap, address, idreg) |
| 50 | + , m_cpu(cpu) |
| 51 | +{ |
| 52 | + //Assume RAM is at 0x20000000 for now. |
| 53 | + //TODO: is this always true for Cortex-M's? |
| 54 | + m_sramBase = 0x20000000; |
| 55 | + |
| 56 | + //Read the control register to get read-only config |
| 57 | + uint32_t ctrl = ReadRegisterByIndex(FP_CTRL); |
| 58 | + m_literalComparators = (ctrl >> 8) & 0xf; |
| 59 | + m_codeComparators = ( (ctrl >> 4) & 0xf ) | ( (ctrl >> 8) & 0xf0 ); |
| 60 | + |
| 61 | + //Read the remap register to see if we can remap or just to breakpoints |
| 62 | + uint32_t remap = ReadRegisterByIndex(FP_REMAP); |
| 63 | + m_canRemap = false; |
| 64 | + if(remap & 0x20000000) |
| 65 | + m_canRemap = true; |
| 66 | + |
| 67 | + //Pull volatile config |
| 68 | + ProbeStatusRegisters(); |
| 69 | +} |
| 70 | + |
| 71 | +ARMFlashPatchBreakpoint::~ARMFlashPatchBreakpoint() |
| 72 | +{ |
| 73 | +} |
| 74 | + |
| 75 | +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 76 | +// Pretty printing |
| 77 | + |
| 78 | +void ARMFlashPatchBreakpoint::ProbeStatusRegisters() |
| 79 | +{ |
| 80 | + uint32_t ctrl = ReadRegisterByIndex(FP_CTRL); |
| 81 | + m_enabled = (ctrl & 1) ? true : false; |
| 82 | + |
| 83 | + uint32_t remap = ReadRegisterByIndex(FP_REMAP); |
| 84 | + m_tableBase = (remap & 0x1FFFFFE0) | m_sramBase; |
| 85 | +} |
| 86 | + |
| 87 | +void ARMFlashPatchBreakpoint::PrintInfo() |
| 88 | +{ |
| 89 | + ProbeStatusRegisters(); |
| 90 | + |
| 91 | + //Heading |
| 92 | + LogNotice("%s rev %d.%d.%d\n", |
| 93 | + GetDescription().c_str(), |
| 94 | + m_idreg.revnum, m_idreg.cust_mod, m_idreg.revand); |
| 95 | + LogIndenter li; |
| 96 | + |
| 97 | + //LogNotice("Attached to CPU: %s\n", m_cpu->GetDescription().c_str()); |
| 98 | + |
| 99 | + //Summary |
| 100 | + if(m_enabled) |
| 101 | + LogNotice("FPB enabled\n"); |
| 102 | + else |
| 103 | + LogNotice("FPB disabled\n"); |
| 104 | + |
| 105 | + if(m_canRemap) |
| 106 | + { |
| 107 | + LogNotice("Remap supported\n"); |
| 108 | + LogNotice("Remap table is at 0x%08x\n", m_tableBase); |
| 109 | + } |
| 110 | + else |
| 111 | + LogNotice("Remap not supported, breakpoints only\n"); |
| 112 | + |
| 113 | + //Code |
| 114 | + LogNotice("%d code comparators\n", m_codeComparators); |
| 115 | + for(uint32_t i=0; i<m_codeComparators; i++) |
| 116 | + { |
| 117 | + LogIndenter li2; |
| 118 | + } |
| 119 | + |
| 120 | + //Literals |
| 121 | + LogNotice("%d literal comparators\n", m_literalComparators); |
| 122 | +} |
| 123 | + |
| 124 | +string ARMFlashPatchBreakpoint::GetDescription() |
| 125 | +{ |
| 126 | + switch(m_idreg.partnum) |
| 127 | + { |
| 128 | + case 0x003: |
| 129 | + return "Cortex-M4 Flash Patch/Breakpoint"; |
| 130 | + |
| 131 | + default: |
| 132 | + LogWarning("Unknown ARM FPB device (part number 0x%x)\n", m_idreg.partnum); |
| 133 | + return "unknown FPB device"; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 138 | +// Fun commands that actually do stuff |
| 139 | + |
| 140 | +void ARMFlashPatchBreakpoint::Enable() |
| 141 | +{ |
| 142 | + uint32_t ctrl = ReadRegisterByIndex(FP_CTRL); |
| 143 | + ctrl |= 1; //set ENABLE |
| 144 | + ctrl |= 2; //must also set KEY for writes to take effect |
| 145 | + WriteRegisterByIndex(FP_CTRL, ctrl); |
| 146 | + |
| 147 | + m_enabled = true; |
| 148 | +} |
| 149 | + |
| 150 | +void ARMFlashPatchBreakpoint::Disable() |
| 151 | +{ |
| 152 | + uint32_t ctrl = ReadRegisterByIndex(FP_CTRL); |
| 153 | + ctrl &= ~1; //clear ENABLE |
| 154 | + ctrl |= 2; //must also set KEY for writes to take effect |
| 155 | + WriteRegisterByIndex(FP_CTRL, ctrl); |
| 156 | + |
| 157 | + m_enabled = false; |
| 158 | +} |
| 159 | + |
| 160 | +void ARMFlashPatchBreakpoint::SetRemapTableBase(uint32_t base) |
| 161 | +{ |
| 162 | + //TODO: Sanity check that address is within SRAM region |
| 163 | + |
| 164 | + //Align to 8 word boundary |
| 165 | + base &= 0x1FFFFFE0; |
| 166 | + WriteRegisterByIndex(FP_REMAP, base); |
| 167 | + |
| 168 | + //Save the actual address including SRAM offset |
| 169 | + m_tableBase = base | m_sramBase; |
| 170 | +} |
| 171 | + |
| 172 | +void ARMFlashPatchBreakpoint::RemapFlashWord(uint32_t slot, uint32_t flashAddress, uint32_t newValue) |
| 173 | +{ |
| 174 | + if(flashAddress & 3) |
| 175 | + { |
| 176 | + LogWarning("ARM FPB requres word-aligned address\n"); |
| 177 | + return; |
| 178 | + } |
| 179 | + |
| 180 | + //Write the patched data to the remap table |
| 181 | + m_ap->WriteWord(m_tableBase + slot*4, newValue); |
| 182 | + |
| 183 | + //Write to FP_COMPx to enable the comparator. Mask off unused address bits. |
| 184 | + flashAddress &= 0x1ffffffc; |
| 185 | + WriteRegisterByIndex(GetCodeComparatorIndex(slot), flashAddress | 1); |
| 186 | +} |
0 commit comments