boot RAM object by signature hunt

master
kaqu 2 years ago
parent 3f4e2107ee
commit 3ec69d3495
  1. 1
      .gitignore
  2. 3
      .vscode/launch.json
  3. 416
      firmware/cmd_mem.c
  4. 81
      firmware/main.c
  5. 3
      neopixelar.py

1
.gitignore vendored

@ -1,3 +1,4 @@
build/*
__pycache__/*
backup/*
software/*

@ -10,7 +10,8 @@
"request": "launch",
"program": "${file}",
"args": ["--build",
//"--flash",
//"--load", // May be used separately ...
"--flash", // May be used separately ...
"--revision=7.0",
"--uart-name=crossover",
//"--with-ethernet", // Not to be used together w/ etherbone! Won't TFTP ...

@ -0,0 +1,416 @@
// SPDX-License-Identifier: BSD-Source-Code
#include <stdio.h>
#include <stdlib.h>
#include <memtest.h>
#include <generated/csr.h>
#include "../command.h"
#include "../helpers.h"
/**
* Command "mr"
*
* Memory read
*
*/
static void mr(int nb_params, char **params)
{
char *c;
unsigned int *addr;
unsigned int length;
if (nb_params < 1) {
printf("mr <address> [length]");
return;
}
addr = (unsigned int *)strtoul(params[0], &c, 0);
if (*c != 0) {
printf("Incorrect address");
return;
}
if (nb_params == 1) {
length = 4;
} else {
length = strtoul(params[1], &c, 0);
if(*c != 0) {
printf("\nIncorrect length");
return;
}
}
dump_bytes(addr, length, (unsigned long)addr);
}
define_command(mr, mr, "Read address space", MEM_CMDS);
/**
* Command "mw"
*
* Memory write
*
*/
static void mw(int nb_params, char **params)
{
char *c;
unsigned int *addr;
unsigned int value;
unsigned int count;
unsigned int i;
if (nb_params < 2) {
printf("mw <address> <value> [count]");
return;
}
addr = (unsigned int *)strtoul(params[0], &c, 0);
if (*c != 0) {
printf("Incorrect address");
return;
}
value = strtoul(params[1], &c, 0);
if(*c != 0) {
printf("Incorrect value");
return;
}
if (nb_params == 2) {
count = 1;
} else {
count = strtoul(params[2], &c, 0);
if(*c != 0) {
printf("Incorrect count");
return;
}
}
for (i = 0; i < count; i++)
*addr++ = value;
}
define_command(mw, mw, "Write address space", MEM_CMDS);
/**
* Command "mc"
*
* Memory copy
*
*/
static void mc(int nb_params, char **params)
{
char *c;
unsigned int *dstaddr;
unsigned int *srcaddr;
unsigned int count;
unsigned int i;
if (nb_params < 2) {
printf("mc <dst> <src> [count]");
return;
}
dstaddr = (unsigned int *)strtoul(params[0], &c, 0);
if (*c != 0) {
printf("Incorrect destination address");
return;
}
srcaddr = (unsigned int *)strtoul(params[1], &c, 0);
if (*c != 0) {
printf("Incorrect source address");
return;
}
if (nb_params == 2) {
count = 1;
} else {
count = strtoul(params[2], &c, 0);
if (*c != 0) {
printf("Incorrect count");
return;
}
}
for (i = 0; i < count; i++)
*dstaddr++ = *srcaddr++;
}
define_command(mc, mc, "Copy address space", MEM_CMDS);
/**
* Command "memtest"
*
* Run a memory test
*
*/
static void memtest_handler(int nb_params, char **params)
{
char *c;
unsigned int *addr;
unsigned long maxsize = ~0uL;
if (nb_params < 1) {
printf("memtest <addr> [<maxsize>]");
return;
}
addr = (unsigned int *)strtoul(params[0], &c, 0);
if (*c != 0) {
printf("Incorrect address");
return;
}
if (nb_params >= 2) {
maxsize = strtoul(params[1], &c, 0);
if (*c != 0) {
printf("Incorrect max size");
return;
}
}
memtest(addr, maxsize);
}
define_command(memtest, memtest_handler, "Run a memory test", MEM_CMDS);
/**
* Command "memspeed"
*
* Run a memory speed test
*
*/
static void memspeed_handler(int nb_params, char **params)
{
char *c;
unsigned int *addr;
unsigned long size;
bool read_only = false;
if (nb_params < 1) {
printf("memspeed <addr> <size> [<readonly>]");
return;
}
addr = (unsigned int *)strtoul(params[0], &c, 0);
if (*c != 0) {
printf("Incorrect address");
return;
}
size = strtoul(params[1], &c, 0);
if (*c != 0) {
printf("Incorrect size");
return;
}
if (nb_params >= 3) {
read_only = (bool) strtoul(params[2], &c, 0);
if (*c != 0) {
printf("Incorrect readonly value");
return;
}
}
memspeed(addr, size, read_only);
}
define_command(memspeed, memspeed_handler, "Run a memory speed test", MEM_CMDS);
#ifdef CSR_DEBUG_PRINTER
/**
* Command "csrprint"
*
* Print CSR values
*
*/
static void csrprint(int nb_params, char **params)
{
print_csrs();
}
define_command(csrprint, csrprint, "Print CSR values", MEM_CMDS);
#endif
#ifdef CSR_WB_SOFTCONTROL_BASE
static void wbr(int nb_params, char **params)
{
char *c;
unsigned int *addr;
unsigned int length;
unsigned int i;
if (nb_params < 1) {
printf("mr <address> [length]");
return;
}
addr = (unsigned int *)strtoul(params[0], &c, 0);
if (*c != 0) {
printf("Incorrect address");
return;
}
if (nb_params == 1) {
length = 4;
} else {
length = strtoul(params[1], &c, 0);
if(*c != 0) {
printf("\nIncorrect length");
return;
}
}
for (i = 0; i < length; ++i) {
wb_softcontrol_adr_write((unsigned long)(addr + i));
wb_softcontrol_read_write(1);
printf("0x%08x: 0x%08x\n", (unsigned long)(addr + i), wb_softcontrol_data_read());
}
}
define_command(wbr, wbr, "Read using softcontrol wishbone controller", MEM_CMDS);
static void wbw(int nb_params, char **params)
{
char *c;
unsigned int *addr;
unsigned int value;
unsigned int count;
unsigned int i;
if (nb_params < 2) {
printf("mw <address> <value> [count]");
return;
}
addr = (unsigned int *)strtoul(params[0], &c, 0);
if (*c != 0) {
printf("Incorrect address");
return;
}
value = strtoul(params[1], &c, 0);
if(*c != 0) {
printf("Incorrect value");
return;
}
if (nb_params == 2) {
count = 1;
} else {
count = strtoul(params[2], &c, 0);
if(*c != 0) {
printf("Incorrect count");
return;
}
}
wb_softcontrol_data_write(value);
for (i = 0; i < count; i++) {
wb_softcontrol_adr_write((unsigned long)(addr + i));
wb_softcontrol_write_write(1);
}
}
define_command(wbw, wbw, "Write using softcontrol wishbone controller", MEM_CMDS);
#endif
/*
* Command "mx"
*
* Memory execute
*/
static void mx(int nb_params, char **params)
{
char *c;
unsigned int *addr,i;
unsigned int length;
if (nb_params < 1) {
// Hunt for 12h 34h 56h 78h 87h 65h 43h 21h ...
// Subtract 10h & jump ...
c = (char *)0x40000000; //MAIN_RAM_BASE;
for(i=0;i < (0x400000-8);i++) //MAIN_RAM_LENGTH;i++)
{
if( (*(c + i) == 0x12)
&& (*(c+i+1) == 0x34)
&& (*(c+i+2) == 0x56)
&& (*(c+i+3) == 0x78)
&& (*(c+i+4) == 0x87)
&& (*(c+i+5) == 0x65)
&& (*(c+i+6) == 0x43)
&& (*(c+i+7) == 0x21)
)
{
c = c + i - 0x10;
printf("Signature match @%08Xh\n", c);
addr = (unsigned int *)c;
break;
}
}
if(i >= 0x400000-32)
{
printf("Signature not found?!\n");
return;
}
}
else {
addr = (unsigned int *)strtoul(params[0], &c, 0);
if (*c != 0) {
printf("Incorrect address");
return;
}
}
//printf("Executing from ...\n");
//length = 32;
//dump_bytes(addr, length, (unsigned long)addr);
//serial output requires delay ?!
extern void busy_wait(unsigned int ms); // Worx!
busy_wait(2000);
void (*fptr)(void);
fptr = (void (*))addr;
(fptr)();
//printf("Returned from execution.\n");
}
define_command(mx, mx, "Execute in RAM, optional: @address / signature hunt otherwise", MEM_CMDS);
/*
* Command "mhsig"
*
* Memory hunt signature
*/
static void mhsig(int nb_params, char **params)
{
char *c;
unsigned int *addr,i;
unsigned int length;
if (nb_params < 1) {
// Hunt for 12h 34h 56h 78h 87h 65h 43h 21h ...
// Subtract 10h & jump ...
c = (char *)0x40000000; //MAIN_RAM_BASE;
for(i=0;i < (0x400000-8);i++) //MAIN_RAM_LENGTH;i++)
{
if( (*(c + i) == 0x12)
&& (*(c+i+1) == 0x34)
&& (*(c+i+2) == 0x56)
&& (*(c+i+3) == 0x78)
&& (*(c+i+4) == 0x87)
&& (*(c+i+5) == 0x65)
&& (*(c+i+6) == 0x43)
&& (*(c+i+7) == 0x21)
)
{
c = c + i - 0x10;
printf("Signature match @%08Xh\n", c);
addr = (unsigned int *)c;
break;
}
}
if(i >= 0x400000-32)
{
printf("Signature not found?!\n");
return;
}
}
}
define_command(mhsig, mhsig, "RAM signature hunt", MEM_CMDS);

@ -265,6 +265,58 @@ int main(int i, char **c)
printf("\n");
}
// RAM empty check
printf("RAM empty check ...\n");
#define RAMBASE 0x40000000
#define RAMLENGTH 0x400000
char *ram_base = (char *)RAMBASE;
int ii,j, i0count = 0, iffcount = 0;
for(ii = 0; ii < (int)RAMLENGTH; ii++) // Hunt for non-ffh bytes
{
if(ram_base[ii] != 0xff) // RAM not empty?
{
//printf("%08lxh:",(unsigned long)ram_base+ii);
for(j = 0;(j < 16) && ((ii + j) < RAMLENGTH);j++)
; //printf(" %02x",ram_base[ii + j]);
//printf("\n");
printf("Data detected:\n");
dump_bytes((unsigned int *)(&ram_base[ii]),j,(unsigned long)ram_base+ii);
break;
}
}
if(ii >= RAMLENGTH)
printf("RAM empty, ok.\n");
else
printf("RAM NOT EMPTY!!!\n");
#define MINRANGELEN 32
#define DUMPLENGTH 48
for(i0count=0, iffcount=0;ii < RAMLENGTH; ii++) // Now track for 0 or 0xff bytes ...
{
if(ram_base[ii] == 0x00)
i0count++;
else
i0count = 0;
if(i0count > MINRANGELEN-1)
{
printf("Zero range detected ...");
if(ii < (RAMLENGTH-DUMPLENGTH+MINRANGELEN))
dump_bytes((unsigned int *)(&ram_base[ii-MINRANGELEN]),DUMPLENGTH,(unsigned long)ram_base+ii-MINRANGELEN);
break;
}
if(ram_base[ii] == 0xff)
iffcount++;
else
iffcount = 0;
if(iffcount > MINRANGELEN-1)
{
printf("Empty range detected ...");
if(ii < (RAMLENGTH-DUMPLENGTH+MINRANGELEN))
dump_bytes((unsigned int *)(&ram_base[ii-MINRANGELEN]),DUMPLENGTH,(unsigned long)ram_base+ii-MINRANGELEN);
break;
}
}
printf("--============= \e[1mConsole\e[0m ================--\n");
#if !defined(TERM_MINI) && !defined(TERM_NO_HIST)
hist_init();
@ -278,11 +330,30 @@ int main(int i, char **c)
nb_params = get_param(buffer, &command, params);
cmd = command_dispatcher(command, nb_params, params);
if (!cmd) {
printf("Command not found, trying jump to 0x40100000 ...\n");
void (*fptr)(void);
fptr = 0x40100000;
(fptr)();
printf("Returned from jump to 0x40100000.\n");
printf("Command not found?!\n");
/*
if(*(unsigned char *)MAIN_RAM_BASE != (unsigned char)0xff)
{
// 00008067 ret <- Test data, fails?!
// 67800000 <- Byte order rearranged: Works!
printf("Command not found, trying jump to %08Xh ...\n", MAIN_RAM_BASE);
void (*fptr)(void);
fptr = MAIN_RAM_BASE;
(fptr)();
printf("Returned from jump to %08Xh.\n", MAIN_RAM_BASE);
}
//if(*(unsigned char *)MAIN_RAM_BASE != (unsigned char)0xff)
//{
//extern void boot_helper(unsigned long r1, unsigned long r2, unsigned long r3, unsigned long addr);
//printf("Memory @%08Xh not empty, trying to boot *.bin from RAM ...\n", MAIN_RAM_BASE);
//boot_helper(0, 0, 0, MAIN_RAM_BASE);
//while(1);
// extern void netboot(void);
// netboot();
//}
*/
}
}
printf("\n%s", PROMPT);

@ -28,6 +28,9 @@
# wishbone-tool --ethernet-host 192.168.1.20 --server load-file --csr-csv build/csr.csv
# --load-address 0x40100000
# --load-name build/colorlight_5a_75b/software/<filename>
# To disassemble raw file:
# ../fpga/litex/riscv64-unknown-elf-gcc-8.3.0-2019.08.0-x86_64-linux-ubuntu14/bin/riscv64-unknown-elf-objdump
# -D -b binary ./build/colorlight_5a_75b/software/bios/bios.bin -m riscv
#
import os
import argparse

Loading…
Cancel
Save