From 8e82839fe29c8f5d5180a0cd9e7561c0d74dfabe Mon Sep 17 00:00:00 2001 From: Amaury Pouly Date: Mon, 12 Dec 2016 11:23:20 +0100 Subject: hwstub: various cleanups - hwstub load now properly stops reading the log when the device returns a 0 size buffer instead of STALLing - add debug output option to hwstub_load - correctly report transfered size on write error - add some debug error message in usb code so that some errors can be diagnosed more easily - add a batch mode to hwstub_shell to disable the interactive shell - increase usb control timeout to 1sec, 100ms was really tight - cap usb buffer size to ~4000 bytes because libusb has a hardwired limit of 4096 bytes for control transfers Change-Id: Id3200ab99ce70a7a3b09ce7faeaafa4a0fac64c7 --- utils/hwstub/lib/hwstub.cpp | 3 +++ utils/hwstub/lib/hwstub_usb.cpp | 22 ++++++++++++++++++++-- utils/hwstub/tools/hwstub_load.cpp | 13 +++++++++++-- utils/hwstub/tools/hwstub_shell.cpp | 12 ++++++++++-- 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/utils/hwstub/lib/hwstub.cpp b/utils/hwstub/lib/hwstub.cpp index 7c81146c77..9dd2915903 100644 --- a/utils/hwstub/lib/hwstub.cpp +++ b/utils/hwstub/lib/hwstub.cpp @@ -444,7 +444,10 @@ error handle::write(uint32_t addr, const void *buf, size_t& sz, bool atomic) size_t xfer = std::min(sz, get_buffer_size()); err = write_dev(addr, buf, xfer, atomic); if(err != error::SUCCESS) + { + sz = cnt; return err; + } sz -= xfer; bufp += xfer; addr += xfer; diff --git a/utils/hwstub/lib/hwstub_usb.cpp b/utils/hwstub/lib/hwstub_usb.cpp index 28c64d9df3..6bb1cfa049 100644 --- a/utils/hwstub/lib/hwstub_usb.cpp +++ b/utils/hwstub/lib/hwstub_usb.cpp @@ -153,7 +153,10 @@ error device::open_dev(std::shared_ptr& handle) libusb_device_handle *h; int err = libusb_open(m_dev, &h); if(err != LIBUSB_SUCCESS) + { + get_context()->debug() << "Cannot open device: " << err << "\n"; return error::ERROR; + } /* fetch some descriptors */ struct libusb_device_descriptor dev_desc; struct libusb_config_descriptor *config = nullptr; @@ -234,7 +237,7 @@ uint16_t device::get_pid() handle::handle(std::shared_ptr dev, libusb_device_handle *handle) :hwstub::handle(dev), m_handle(handle) { - set_timeout(std::chrono::milliseconds(100)); + set_timeout(std::chrono::milliseconds(1000)); } handle::~handle() @@ -284,8 +287,13 @@ rb_handle::rb_handle(std::shared_ptr dev, { m_probe_status = error::SUCCESS; /* claim interface */ - if(libusb_claim_interface(m_handle, m_intf) != 0) + int err = libusb_claim_interface(m_handle, m_intf); + if(err != 0) + { + get_device()->get_context()->debug() << + "Cannot claim interface: " << err <<"\n"; m_probe_status = error::PROBE_FAILURE; + } /* check version */ if(m_probe_status == error::SUCCESS) { @@ -295,7 +303,13 @@ rb_handle::rb_handle(std::shared_ptr dev, { if(ver_desc.bMajor != HWSTUB_VERSION_MAJOR || ver_desc.bMinor < HWSTUB_VERSION_MINOR) + { + get_device()->get_context()->debug() << + "Version mismatch: host is " << HWSTUB_VERSION_MAJOR << + "." << HWSTUB_VERSION_MINOR << ", device is " << + ver_desc.bMajor << "." << ver_desc.bMinor << "\n"; m_probe_status = error::PROBE_FAILURE; + } } } /* get buffer size */ @@ -305,6 +319,10 @@ rb_handle::rb_handle(std::shared_ptr dev, m_probe_status = get_layout_desc(layout_desc); if(m_probe_status == error::SUCCESS) m_buf_size = layout_desc.dBufferSize; + /* libusb limits control transfers to 4096 bytes, to which we need to subtract + * the size of the possible header. To play safe, limit to 4000 bytes */ + if(m_buf_size > 4000) + m_buf_size = 4000; } } diff --git a/utils/hwstub/tools/hwstub_load.cpp b/utils/hwstub/tools/hwstub_load.cpp index 87737939c3..6ca6079294 100644 --- a/utils/hwstub/tools/hwstub_load.cpp +++ b/utils/hwstub/tools/hwstub_load.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include "hwstub.hpp" #include "hwstub_uri.hpp" @@ -111,6 +112,7 @@ void usage(void) printf(" --quiet/-q Quiet output\n"); printf(" --type/-t Override file type\n"); printf(" --dev/-d Device URI (see below)\n"); + printf(" --verbose/-v Display debug output\n"); printf("file types:\n"); printf(" raw Load a raw binary blob\n"); printf(" rockbox Load a rockbox image produced by scramble\n"); @@ -128,6 +130,7 @@ int main(int argc, char **argv) bool quiet = false; enum image_type_t type = IT_DETECT; const char *uri = "usb:"; + bool verbose = false; // parse command line while(1) @@ -138,10 +141,11 @@ int main(int argc, char **argv) {"quiet", no_argument, 0, 'q'}, {"type", required_argument, 0, 't'}, {"dev", required_argument, 0, 'd'}, + {"verbose", no_argument, 0, 'v'}, {0, 0, 0, 0} }; - int c = getopt_long(argc, argv, "?qt:d:", long_options, NULL); + int c = getopt_long(argc, argv, "?qt:d:v", long_options, NULL); if(c == -1) break; switch(c) @@ -170,6 +174,9 @@ int main(int argc, char **argv) case 'd': uri = optarg; break; + case 'v': + verbose = true; + break; default: abort(); } @@ -236,6 +243,8 @@ int main(int argc, char **argv) printf("Cannot create context: %s\n", errstr.c_str()); return 1; } + if(verbose) + hwctx->set_debug(std::cout); std::vector> list; hwstub::error ret = hwctx->get_device_list(list); if(ret != hwstub::error::SUCCESS) @@ -277,7 +286,7 @@ int main(int argc, char **argv) char buffer[128]; size_t size = sizeof(buffer) - 1; hwstub::error err = hwdev->get_log(buffer, size); - if(err != hwstub::error::SUCCESS) + if(err != error::SUCCESS || size == 0) break; buffer[size] = 0; fprintf(stderr, "%s", buffer); diff --git a/utils/hwstub/tools/hwstub_shell.cpp b/utils/hwstub/tools/hwstub_shell.cpp index 5fb3e13840..b2cf2cec60 100644 --- a/utils/hwstub/tools/hwstub_shell.cpp +++ b/utils/hwstub/tools/hwstub_shell.cpp @@ -1239,6 +1239,7 @@ void usage(void) printf("options:\n"); printf(" --help/-? Display this help\n"); printf(" --quiet/-q Quiet non-command messages\n"); + printf(" -b/--batch Disable interactive mode after running commands and files\n"); printf(" --verbose/-v Verbose output\n"); printf(" -i Set lua init file (default is init.lua)\n"); printf(" -e Execute at startup\n"); @@ -1293,6 +1294,7 @@ int main(int argc, char **argv) { std::string dev_uri = hwstub::uri::default_uri().full_uri(); bool verbose = false; + bool batch = false; const char *lua_init = "init.lua"; std::vector< std::pair< exec_type, std::string > > startup_cmds; @@ -1309,11 +1311,12 @@ int main(int argc, char **argv) {"startfile", required_argument, 0, 'f'}, {"dev", required_argument, 0, 'd'}, {"verbose", no_argument, 0, 'v'}, + {"batch", no_argument, 0, 'b'}, {"debug-rw", no_argument, 0, OPT_DBG_RW}, {0, 0, 0, 0} }; - int c = getopt_long(argc, argv, "?qi:e:f:d:v", long_options, NULL); + int c = getopt_long(argc, argv, "?qi:e:f:d:vb", long_options, NULL); if(c == -1) break; switch(c) @@ -1344,6 +1347,9 @@ int main(int argc, char **argv) case OPT_DBG_RW: g_print_mem_rw = true; break; + case 'b': + batch = true; + break; default: abort(); } @@ -1441,7 +1447,9 @@ int main(int argc, char **argv) /* intercept CTRL+C */ signal(SIGINT, do_signal); // start interactive shell - luap_enter(g_lua, &g_exit); + if(!batch) + luap_enter(g_lua, &g_exit); + // cleanup lua_close(g_lua); // display log if handled -- cgit v1.2.3