Skip to content

Instantly share code, notes, and snippets.

@pamaury
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pamaury/a3379864a23e9630c901 to your computer and use it in GitHub Desktop.
Save pamaury/a3379864a23e9630c901 to your computer and use it in GitHub Desktop.
diff --git a/utils/hwstub/hwstub_protocol.h b/utils/hwstub/hwstub_protocol.h
index 33081d3..a19f448 100644
--- a/utils/hwstub/hwstub_protocol.h
+++ b/utils/hwstub/hwstub_protocol.h
@@ -140,6 +140,7 @@ struct hwstub_device_desc_t
#define HWSTUB_READ2 0x42
#define HWSTUB_WRITE 0x43
#define HWSTUB_EXEC 0x44
+#define HWSTUB_READ_ROM 0x45
/**
* HWSTUB_GET_LOG:
@@ -186,4 +187,9 @@ struct hwstub_exec_req_t
uint16_t bmFlags;
} __attribute__((packed));
+/**
+ * HWSTUB_READ_ROM
+ * Read a part of the ROM, and increase the read address
+ */
+
#endif /* __HWSTUB_PROTOCOL__ */
diff --git a/utils/hwstub/lib/hwstub.c b/utils/hwstub/lib/hwstub.c
index 6ae0400..33f465f 100644
--- a/utils/hwstub/lib/hwstub.c
+++ b/utils/hwstub/lib/hwstub.c
@@ -229,3 +229,10 @@ int hwstub_jump(struct hwstub_device_t *dev, uint32_t addr)
{
return hwstub_exec(dev, addr, HWSTUB_EXEC_JUMP);
}
+
+int hwstub_read_rom(struct hwstub_device_t *dev, void *buf, size_t sz)
+{
+ return libusb_control_transfer(dev->handle,
+ LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE | LIBUSB_ENDPOINT_IN,
+ HWSTUB_READ_ROM, dev->id++, dev->intf, buf, sz, 1000);
+}
diff --git a/utils/hwstub/lib/hwstub.h b/utils/hwstub/lib/hwstub.h
index d7d6ceb..4ba0ba8 100644
--- a/utils/hwstub/lib/hwstub.h
+++ b/utils/hwstub/lib/hwstub.h
@@ -59,6 +59,7 @@ int hwstub_rw_mem(struct hwstub_device_t *dev, int read, uint32_t addr, void *bu
int hwstub_exec(struct hwstub_device_t *dev, uint32_t addr, uint16_t flags);
int hwstub_call(struct hwstub_device_t *dev, uint32_t addr);
int hwstub_jump(struct hwstub_device_t *dev, uint32_t addr);
+int hwstub_read_rom(struct hwstub_device_t *dev, void *buf, size_t sz);
#ifdef __cplusplus
} // extern "C"
diff --git a/utils/hwstub/stub/atj213x/target.c b/utils/hwstub/stub/atj213x/target.c
index 04762bc..f8eeebd 100644
--- a/utils/hwstub/stub/atj213x/target.c
+++ b/utils/hwstub/stub/atj213x/target.c
@@ -111,3 +111,12 @@ void target_get_config_desc(void *buffer, int *size)
(void) buffer;
(void) size;
}
+
+static uint32_t rom_addr = 0xbfc00000;
+
+unsigned target_read_rom(void *buffer, unsigned size)
+{
+ memcpy(buffer, (void *)rom_addr, size);
+ rom_addr += size;
+ return size;
+}
diff --git a/utils/hwstub/stub/main.c b/utils/hwstub/stub/main.c
index 4e54b8f..c9e483a 100644
--- a/utils/hwstub/stub/main.c
+++ b/utils/hwstub/stub/main.c
@@ -439,6 +439,15 @@ static void handle_exec(struct usb_ctrlrequest *req)
}
}
+extern unsigned target_read_rom(void *buffer, unsigned size);
+
+static void handle_read_rom(struct usb_ctrlrequest *req)
+{
+ int length = target_read_rom(usb_buffer, req->wLength);
+ usb_drv_send(EP_CONTROL, usb_buffer, length);
+ usb_drv_recv(EP_CONTROL, NULL, 0);
+}
+
static void handle_class_intf_req(struct usb_ctrlrequest *req)
{
unsigned intf = req->wIndex & 0xff;
@@ -456,6 +465,8 @@ static void handle_class_intf_req(struct usb_ctrlrequest *req)
return handle_write(req);
case HWSTUB_EXEC:
return handle_exec(req);
+ case HWSTUB_READ_ROM:
+ return handle_read_rom(req);
default:
usb_drv_stall(EP_CONTROL, true, true);
}
diff --git a/utils/hwstub/tools/Makefile b/utils/hwstub/tools/Makefile
index 6db0c70..f953d56 100644
--- a/utils/hwstub/tools/Makefile
+++ b/utils/hwstub/tools/Makefile
@@ -6,7 +6,7 @@ REGTOOLS_LIB_DIR=../../regtools/lib
CFLAGS=-Wall -O2 `pkg-config --cflags libusb-1.0` -std=c99 -g -I$(HWSTUB_LIB_DIR) -I$(REGTOOLS_LIB_DIR) `pkg-config --cflags lua5.2`
CXXFLAGS=-Wall -O2 `pkg-config --cflags libusb-1.0` -g -I$(HWSTUB_LIB_DIR) -I$(REGTOOLS_LIB_DIR) `pkg-config --cflags lua5.2`
LDFLAGS=`pkg-config --libs libusb-1.0` `pkg-config --libs lua5.2` -lreadline -L$(HWSTUB_LIB_DIR) -L$(REGTOOLS_LIB_DIR) -lsocdesc -lhwstub `xml2-config --libs`
-EXEC=hwstub_shell hwstub_load
+EXEC=hwstub_shell hwstub_load hwstub_read_rom
SRC=$(wildcard *.c)
SRCXX=$(wildcard *.cpp)
OBJ=$(SRC:.c=.o) $(SRCXX:.cpp=.o)
@@ -32,6 +32,9 @@ hwstub_shell: hwstub_shell.o $(LIBS)
hwstub_load: hwstub_load.o $(LIBS)
$(LD) -o $@ $^ $(LDFLAGS)
+hwstub_read_rom: hwstub_read_rom.o $(LIBS)
+ $(LD) -o $@ $^ $(LDFLAGS)
+
clean:
rm -rf $(OBJ) $(LIB) $(EXEC)
diff --git a/utils/hwstub/tools/hwstub_read_rom.cpp b/utils/hwstub/tools/hwstub_read_rom.cpp
new file mode 100644
index 0000000..400cf24
--- /dev/null
+++ b/utils/hwstub/tools/hwstub_read_rom.cpp
@@ -0,0 +1,90 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2014 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 "hwstub.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <getopt.h>
+
+struct hwstub_device_t *g_hwdev;
+struct hwstub_version_desc_t g_hwdev_ver;
+
+int main(int argc, char **argv)
+{
+ // create usb context
+ libusb_context *ctx;
+ libusb_init(&ctx);
+ libusb_set_debug(ctx, 3);
+
+ // look for device
+ printf("Looking for hwstub device ...\n");
+ // open first device
+ libusb_device **list;
+ ssize_t cnt = hwstub_get_device_list(ctx, &list);
+ if(cnt <= 0)
+ {
+ printf("No device found\n");
+ return 1;
+ }
+ libusb_device_handle *handle;
+ if(libusb_open(list[0], &handle) != 0)
+ {
+ printf("Cannot open device\n");
+ return 1;
+ }
+ libusb_free_device_list(list, 1);
+
+ // admin stuff
+ libusb_device *mydev = libusb_get_device(handle);
+ printf("device found at %d:%d\n",
+ libusb_get_bus_number(mydev),
+ libusb_get_device_address(mydev));
+ g_hwdev = hwstub_open(handle);
+ if(g_hwdev == NULL)
+ {
+ printf("Cannot open device!\n");
+ return 1;
+ }
+
+ // get hwstub information
+ int ret = hwstub_get_desc(g_hwdev, HWSTUB_DT_VERSION, &g_hwdev_ver, sizeof(g_hwdev_ver));
+ if(ret != sizeof(g_hwdev_ver))
+ {
+ printf("Cannot get version!\n");
+ hwstub_release(g_hwdev);
+ return 1;
+ }
+ if(g_hwdev_ver.bMajor != HWSTUB_VERSION_MAJOR || g_hwdev_ver.bMinor < HWSTUB_VERSION_MINOR)
+ printf("Warning: this tool is possibly incompatible with your device:\n");
+ printf("Device version: %d.%d.%d\n", g_hwdev_ver.bMajor, g_hwdev_ver.bMinor, g_hwdev_ver.bRevision);
+ printf("Host version: %d.%d\n", HWSTUB_VERSION_MAJOR, HWSTUB_VERSION_MINOR);
+
+ int len = 32;
+ uint8_t *buffer = (uint8_t *)malloc(len);
+ len = hwstub_read_rom(g_hwdev, buffer, len);
+ printf("ROM dump:\n");
+ for(int i = 0; i < len; i++)
+ printf("%02x ", buffer[i]);
+ printf("\n");
+
+ hwstub_release(g_hwdev);
+ return 0;
+}
\ No newline at end of file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment