From a2f4c5201d78b9f351834b0512623eeac622280f Mon Sep 17 00:00:00 2001 From: Amaury Pouly Date: Sun, 7 Feb 2016 21:45:29 +0000 Subject: hwstub: port hwstub_load to the new library Change-Id: I7e8ae50907401a9480a0da809a4470f1728d3a57 --- utils/hwstub/tools/hwstub_load.cpp | 94 +++++++++++++++++--------------------- 1 file changed, 43 insertions(+), 51 deletions(-) diff --git a/utils/hwstub/tools/hwstub_load.cpp b/utils/hwstub/tools/hwstub_load.cpp index 119f6d7c58..87737939c3 100644 --- a/utils/hwstub/tools/hwstub_load.cpp +++ b/utils/hwstub/tools/hwstub_load.cpp @@ -18,13 +18,16 @@ * KIND, either express or implied. * ****************************************************************************/ -#include "hwstub.h" #include #include #include #include #include #include +#include "hwstub.hpp" +#include "hwstub_uri.hpp" + +using namespace hwstub; struct player_info_t { @@ -102,11 +105,12 @@ bool could_be_rockbox(unsigned char *buffer, size_t size) void usage(void) { - printf("usage: hwstub_load [options] \n"); + printf("usage: hwstub::load [options] \n"); printf("options:\n"); - printf(" --help/-? Display this help\n"); - printf(" --quiet/-q Quiet output\n"); - printf(" --type/-t Override file type\n"); + printf(" --help/-? Display this help\n"); + printf(" --quiet/-q Quiet output\n"); + printf(" --type/-t Override file type\n"); + printf(" --dev/-d Device URI (see below)\n"); printf("file types:\n"); printf(" raw Load a raw binary blob\n"); printf(" rockbox Load a rockbox image produced by scramble\n"); @@ -115,14 +119,15 @@ void usage(void) for(int i = 0; players[i].name; i++) printf(" %s", players[i].name); printf("\n"); + //hwstub::usage_uri(stdout); exit(1); } int main(int argc, char **argv) { bool quiet = false; - struct hwstub_device_t *hwdev; enum image_type_t type = IT_DETECT; + const char *uri = "usb:"; // parse command line while(1) @@ -132,10 +137,11 @@ int main(int argc, char **argv) {"help", no_argument, 0, '?'}, {"quiet", no_argument, 0, 'q'}, {"type", required_argument, 0, 't'}, + {"dev", required_argument, 0, 'd'}, {0, 0, 0, 0} }; - int c = getopt_long(argc, argv, "?qt:", long_options, NULL); + int c = getopt_long(argc, argv, "?qt:d:", long_options, NULL); if(c == -1) break; switch(c) @@ -161,6 +167,9 @@ int main(int argc, char **argv) return 1; } break; + case 'd': + uri = optarg; + break; default: abort(); } @@ -220,61 +229,44 @@ int main(int argc, char **argv) } // create usb context - libusb_context *ctx; - libusb_init(&ctx); - libusb_set_debug(ctx, 3); - - // look for device - if(!quiet) - printf("Looking for device %#04x:%#04x...\n", HWSTUB_USB_VID, HWSTUB_USB_PID); - - libusb_device_handle *handle = libusb_open_device_with_vid_pid(ctx, - HWSTUB_USB_VID, HWSTUB_USB_PID); - if(handle == NULL) + std::string errstr; + std::shared_ptr hwctx = uri::create_context(uri::uri(uri), &errstr); + if(!hwctx) { - fprintf(stderr, "No device found\n"); + printf("Cannot create context: %s\n", errstr.c_str()); return 1; } - - // admin stuff - libusb_device *mydev = libusb_get_device(handle); - if(!quiet) + std::vector> list; + hwstub::error ret = hwctx->get_device_list(list); + if(ret != hwstub::error::SUCCESS) { - printf("device found at %d:%d\n", - libusb_get_bus_number(mydev), - libusb_get_device_address(mydev)); - } - hwdev = hwstub_open(handle); - if(hwdev == NULL) - { - fprintf(stderr, "Cannot probe device!\n"); + printf("Cannot get device list: %d\n", (int)ret); return 1; } - - // get hwstub information - struct hwstub_version_desc_t hwdev_ver; - int ret = hwstub_get_desc(hwdev, HWSTUB_DT_VERSION, &hwdev_ver, sizeof(hwdev_ver)); - if(ret != sizeof(hwdev_ver)) + if(list.size() == 0) { - fprintf(stderr, "Cannot get version!\n"); - goto Lerr; + printf("No hwstub device detected!\n"); + return 1; } - if(hwdev_ver.bMajor != HWSTUB_VERSION_MAJOR || hwdev_ver.bMinor < HWSTUB_VERSION_MINOR) + /* open first device */ + std::shared_ptr hwdev; + ret = list[0]->open(hwdev); + if(ret != hwstub::error::SUCCESS) { - printf("Warning: this tool is possibly incompatible with your device:\n"); - printf("Device version: %d.%d.%d\n", hwdev_ver.bMajor, hwdev_ver.bMinor, hwdev_ver.bRevision); - printf("Host version: %d.%d\n", HWSTUB_VERSION_MAJOR, HWSTUB_VERSION_MINOR); + printf("Cannot open device: %d\n", (int)ret); + return 1; } - ret = hwstub_rw_mem(hwdev, 0, addr, buffer, size); - if(ret != (int)size) + size_t out_size = size; + ret = hwdev->write(addr, buffer, out_size, false); + if(ret != hwstub::error::SUCCESS || out_size != size) { - fprintf(stderr, "Image write failed: %d\n", ret); + fprintf(stderr, "Image write failed: %s, %zu/%zu\n", error_string(ret).c_str(), + out_size, size); goto Lerr; } - hwstub_jump(hwdev, addr); + hwdev->exec(addr, HWSTUB_EXEC_JUMP); - hwstub_release(hwdev); return 0; Lerr: @@ -283,13 +275,13 @@ int main(int argc, char **argv) do { char buffer[128]; - int length = hwstub_get_log(hwdev, buffer, sizeof(buffer) - 1); - if(length <= 0) + size_t size = sizeof(buffer) - 1; + hwstub::error err = hwdev->get_log(buffer, size); + if(err != hwstub::error::SUCCESS) break; - buffer[length] = 0; + buffer[size] = 0; fprintf(stderr, "%s", buffer); }while(1); - hwstub_release(hwdev); return 1; } -- cgit v1.2.3