嵌入式反向:通过SPI闪存进行代码跟踪



做什么的?!



, , . , : , , UART "Hello, Habr!", JTAG, .



SPI-.





USB-SATA- ( ) SPI- EM100-Pro. — SPI-flash , SPI . , . GUI .



SPI? . - , , , , -. , ?





.





SPI- , SPI- . - , . .





SM32F4DISCOVERY SPI1 . , SPI-Flash .





, SPI- . .





Write uFIFO. uFIFO — 512 , . , . , uFIFO , GUI . .





uFIFO :



techCmd CMD write uFIFO preByte1 preByte2 preByte3 preByte4 typeData lenData Data.....
11h don't care C0h 40h 44h 36h 47h 1-7


Dediprog "@D6G", "debug".



ASCII



UART. . , ASCII , uFIFO Data Type 05h. , ASCII-.



//     uFIFO
void SpiFlash_WriteUFifo(unsigned int count, unsigned char* buffer)
{   
    // 0x11 - custom- 
    // 0x00 - don't care byte
    // 0xC0 -    uFIFO
    char writeFifoCmd[3] = {0x11, 0x00, 0xC0};
    CSEnable();
    HAL_SPI_Transmit(&hspi1, writeFifoCmd, sizeof(writeFifoCmd), HAL_MAX_DEALY);
    HAL_SPI_Transmit(&hspi1, buffer, count, HAL_MAX_DEALY);     //  
    CSDisable();    
}

//   ASCII 
void SpiFlash_TxString(char* str)
{
    // preamble data packet, 0x05 - ASCII type
    char premsg[6] = {0x40, 0x44, 0x36, 0x47, 0x05};  
    premsg[5] = strlen(str);
    Flash_WaitBusy();                                               
    SpiFlash_WriteUFifo(sizeof(premsg), premsg);    //  ""
    SpiFlash_WriteUFifo(strlen(str), str);          //  
}

//  
SpiFlash_TxString("Hello, Habr!\n\0");
SpiFlash_TxString("Program start!\n\0");
SpiFlash_TxString("While loop:\n\0");


, , GUI , SPI Hyper Terminal .





HEX



, ASCII-. , , Flash- . Data Type 04h. HEX.



//   HEX 
void SpiFlash_TxHexArray(unsigned char* hexArray, unsigned int count){
    char premsg[6] = {0x40, 0x44, 0x36, 0x47, 0x04};
    premsg[5] = count;
    Flash_WaitBusy();
    SpiFlash_WriteUFifo(sizeof(premsg), premsg);
    SpiFlash_WriteUFifo(count, hexArray);
}

//  -
unsigned char array[0x10];
for(int i = 0; i < 0x10; i++)
{
    array[i] = i;
}
SpiFlash_TxString("Array dump:\0");
SpiFlash_TxHexArray(array, 0x10);
//    
Spi_Flash_TxString("Memory dump:\0");
Spi_Flash_TxHexArray((char*)0x08000000, 0x10);


, ST-LINK Utility. . - , STM32 Cortex-M, little-endian.





Checkpoint



, . "Checkpoint.txt" ini-, . , .



//  checkpoint-a 
void SpiFlash_SetCheckPoints(unsigned char* points, unsigned char countPoints)
{
    char premsg[6] = {0x40, 0x44, 0x36, 0x47, 0x01};    
    premsg[5] = countPoints;    
    Flash_WaitBusy();
    SpiFlash_WriteUFifo(sizeof(premsg), premsg);
    SpiFlash_WriteUFifo(countPoints, points);   
}

//  
unsigned char checkPoint[1];
checkPoint[0] = 0x1;
SpiFlash_SetCheckPoints(checkPoint, 1);
//... 
checkPoint[0] = 0x2;
SpiFlash_SetCheckPoints(checkPoint, 1);
//... 
checkPoint[0] = 0x3;
SpiFlash_SetCheckPoints(checkPoint, 1);


SpiFlash_SetCheckPoints(checkPoint, 1), .





Lookup table



Lookup table. ASCII Checkpoint. , GUI, . , , , .



//       
void SpiFlash_LookUpTable(uint16_t index, uint16_t AsciiChar)
{
    char premsg[10] = {0x40, 0x44, 0x36, 0x47, 0x07, 0x04};  
    premsg[6] = (index >> 8) & 0xFF;
    premsg[7] = index & 0xFF;
    premsg[8] = (AsciiChar >> 8) & 0xFF;
    premsg[9] = AsciiChar & 0xFF;
    Flash_WaitBusy();
    SpiFlash_WriteUFifo(10, premsg);
}

//  
uint16_t varShow = 0x3031;              // ASCII: 01
SpiFlash_LookUpTable(0x0, varShow);     //  0-   
varShow++;                              // Increment-> ASCII: 02
SpiFlash_TxString("IncrementVar\n\0");
SpiFlash_LookUpTable(0x0, varShow);
SpiFlash_TxString("Reg2:\n\0");
SpiFlash_LookUpTable(0x1, 0x3535);      //  1-    (ASCII: 55)
SpiFlash_TxString("LookUpDone\n\0");


varShow, . , HEX- .





Timestamp



, , , . Timestamp, . , . , , 1 10 , GUI . , uFIFO time stamp.



//     
void SpiFlash_TxTimeStamp(int timeValue)
{   
    char premsg[] = {0x40, 0x44, 0x36, 0x47, 0x06, 0x04};   
    Flash_WaitBusy();
    SpiFlash_WriteUFifo(sizeof(premsg), premsg);

    unsigned char timeBuffer[4];
    timeBuffer[0] = (timeValue >> 24) & 0xFF;
    timeBuffer[1] = (timeValue >> 16) & 0xFF;
    timeBuffer[2] = (timeValue >> 8) & 0xFF;
    timeBuffer[3] = timeValue & 0xFF;
    SpiFlash_WriteUFifo(sizeof(timeBuffer), timeBuffer);
}

//  
SpiFlash_TxTimeStamp(100000000);    // 100000000 * 10  = 1 


SpiFlash_TxTimeStamp 100000000 , 10 1 . , 1 .





10 100000000 = 100, . .





, UART JTAG. , , SPI- . , (~600$) "" , :)



? .






All Articles