hwstub: add tool to dump memory regions (such as ROM, RAM, or peripherals)

Although this case be done with hwstub_shell, this is common enough to deserve
its own tool.

Change-Id: I9253e40850f37257464548a3acefb14ea083841d
This commit is contained in:
Amaury Pouly 2017-01-17 12:01:46 +01:00
parent f4091be1d3
commit 07bc348c91
2 changed files with 163 additions and 1 deletions

View file

@ -9,7 +9,7 @@ INCLUDES=-I$(HWSTUB_INCLUDE_DIR) -I$(REGTOOLS_INCLUDE_DIR) `pkg-config --cflags
CFLAGS=-Wall -O2 -std=c99 -g $(INCLUDES) -D_XOPEN_SOURCE=600
CXXFLAGS=-Wall -O2 -std=c++11 -g $(INCLUDES)
LDFLAGS=-lhwstub `pkg-config --libs libusb-1.0` `pkg-config --libs lua5.2` -lreadline -L$(HWSTUB_LIB_DIR) -L$(REGTOOLS_LIB_DIR) -lsocdesc `xml2-config --libs` -pthread
EXEC=hwstub_shell hwstub_load hwstub_server hwstub_test
EXEC=hwstub_shell hwstub_load hwstub_server hwstub_test hwstub_dump
SRC=$(wildcard *.c)
SRCXX=$(wildcard *.cpp)
OBJ=$(SRC:.c=.o) $(SRCXX:.cpp=.o)
@ -38,6 +38,9 @@ hwstub_shell: hwstub_shell.o prompt.o $(LIBS)
hwstub_load: hwstub_load.o $(LIBS)
$(LD) -o $@ $^ $(LDFLAGS)
hwstub_dump: hwstub_dump.o $(LIBS)
$(LD) -o $@ $^ $(LDFLAGS)
hwstub_server: hwstub_server.o $(LIBS)
$(LD) -o $@ $^ $(LDFLAGS)

View file

@ -0,0 +1,159 @@
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Copyright (C) 2016 by Amaury Pouly
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <stdbool.h>
#include <ctype.h>
#include <iostream>
#include "hwstub.hpp"
#include "hwstub_uri.hpp"
using namespace hwstub;
void usage(void)
{
fprintf(stderr, "usage: hwstub_ump [options] <addr> <size>\n");
fprintf(stderr, "options:\n");
fprintf(stderr, " --help/-h Display this help\n");
fprintf(stderr, " --verbose/-v Verbose output\n");
fprintf(stderr, " --dev/-d <uri> Device URI (see below)\n");
//hwstub::usage_uri(stdout);
exit(1);
}
int main(int argc, char **argv)
{
const char *uri = hwstub::uri::default_uri().full_uri().c_str();
bool verbose = false;
// parse command line
while(1)
{
static struct option long_options[] =
{
{"help", no_argument, 0, 'h'},
{"dev", required_argument, 0, 'd'},
{"verbose", no_argument, 0, 'v'},
{0, 0, 0, 0}
};
int c = getopt_long(argc, argv, "hd:v", long_options, NULL);
if(c == -1)
break;
switch(c)
{
case -1:
break;
case 'v':
verbose = true;
break;
case 'h':
usage();
break;
case 'd':
uri = optarg;
break;
default:
abort();
}
}
if(optind + 2 != argc)
usage();
char *end;
unsigned long addr = strtoul(argv[optind], &end, 0);
if(*end)
{
fprintf(stderr, "Invalid dump address\n");
return 2;
}
unsigned long size = strtoul(argv[optind + 1], &end, 0);
if(*end)
{
fprintf(stderr, "Invalid dump size\n");
return 2;
}
// create usb context
std::string errstr;
std::shared_ptr<context> hwctx = uri::create_context(uri::uri(uri), &errstr);
if(!hwctx)
{
fprintf(stderr, "Cannot create context: %s\n", errstr.c_str());
return 1;
}
if(verbose)
hwctx->set_debug(std::cerr);
std::vector<std::shared_ptr<hwstub::device>> list;
hwstub::error ret = hwctx->get_device_list(list);
if(ret != hwstub::error::SUCCESS)
{
fprintf(stderr, "Cannot get device list: %d\n", (int)ret);
return 1;
}
if(list.size() == 0)
{
fprintf(stderr, "No hwstub device detected!\n");
return 1;
}
/* open first device */
std::shared_ptr<hwstub::handle> hwdev;
ret = list[0]->open(hwdev);
if(ret != hwstub::error::SUCCESS)
{
fprintf(stderr, "Cannot open device: %d\n", (int)ret);
return 1;
}
/* load */
uint8_t *buffer = new uint8_t[size];
size_t out_size = size;
ret = hwdev->read(addr, buffer, out_size, false);
if(ret != hwstub::error::SUCCESS || out_size != size)
{
fprintf(stderr, "Dump failed: %s, %zu/%zu\n", error_string(ret).c_str(),
out_size, size);
goto Lerr;
}
fwrite(buffer, 1, size, stdout);
return 0;
Lerr:
// display log if handled
fprintf(stderr, "Device log:\n");
do
{
char buffer[128];
size_t size = sizeof(buffer) - 1;
hwstub::error err = hwdev->get_log(buffer, size);
if(err != hwstub::error::SUCCESS)
break;
buffer[size] = 0;
fprintf(stderr, "%s", buffer);
}while(1);
return 1;
}