From 25fd3c4fb1c034e894150fe4ef8f8d3c1de89910 Mon Sep 17 00:00:00 2001 From: "jeanluc.malet" Date: Thu, 17 Jan 2019 10:55:57 +0100 Subject: [PATCH 1/4] first implementation of low level tunables --- libuuu/CMakeLists.txt | 1 + libuuu/cmd.h | 2 +- libuuu/conf.cpp | 51 ++++++++++++++++++++ libuuu/conf.h | 52 ++++++++++++++++++++ libuuu/fastboot.cpp | 15 ++++-- libuuu/fastboot.h | 5 +- libuuu/sdp.cpp | 3 +- libuuu/trans.cpp | 109 ++++++++++++++++++++++++++++++------------ libuuu/trans.h | 7 +-- 9 files changed, 198 insertions(+), 47 deletions(-) create mode 100644 libuuu/conf.cpp create mode 100644 libuuu/conf.h diff --git a/libuuu/CMakeLists.txt b/libuuu/CMakeLists.txt index 46d48a24..720fd32e 100644 --- a/libuuu/CMakeLists.txt +++ b/libuuu/CMakeLists.txt @@ -23,6 +23,7 @@ set(SOURCES zip.cpp fat.cpp rominfo.cpp + conf.cpp ) set(generated_files_dir "${CMAKE_BINARY_DIR}/libuuu/gen") diff --git a/libuuu/cmd.h b/libuuu/cmd.h index 6f05adba..46d2e46a 100644 --- a/libuuu/cmd.h +++ b/libuuu/cmd.h @@ -35,6 +35,7 @@ #include #include #include +#include #include "liberror.h" #include "libcomm.h" @@ -107,7 +108,6 @@ class CmdBase string param; if (get_string_in_square_brackets(prot, param)) return -1; - if (!param.empty()) { size_t param_pos = 0; diff --git a/libuuu/conf.cpp b/libuuu/conf.cpp new file mode 100644 index 00000000..91dbfacc --- /dev/null +++ b/libuuu/conf.cpp @@ -0,0 +1,51 @@ +/* +* Copyright 2018 NXP. +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* +* Redistributions of source code must retain the above copyright notice, this +* list of conditions and the following disclaimer. +* +* Redistributions in binary form must reproduce the above copyright notice, this +* list of conditions and the following disclaimer in the documentation and/or +* other materials provided with the distribution. +* +* Neither the name of the NXP Semiconductor nor the names of its +* contributors may be used to endorse or promote products derived from this +* software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +* POSSIBILITY OF SUCH DAMAGE. +* +*/ + + +#include "conf.h" + + +Conf::Conf() +{ + m_USB.m_BulkMaxTransfer = 0x10000; + m_USB.m_BulkTimeout = 9000; + m_USB.m_HIDMaxTransfer= 0x10000; + m_USB.m_HIDTimeout = 9000; +} + +Conf& Conf::GetConf() +{ + static Conf instance; + return instance; +} + + + diff --git a/libuuu/conf.h b/libuuu/conf.h new file mode 100644 index 00000000..49fb8502 --- /dev/null +++ b/libuuu/conf.h @@ -0,0 +1,52 @@ +/* +* Copyright 2018 NXP. +* +* Redistribution and use in source and binary forms, with or without modification, +* are permitted provided that the following conditions are met: +* +* Redistributions of source code must retain the above copyright notice, this +* list of conditions and the following disclaimer. +* +* Redistributions in binary form must reproduce the above copyright notice, this +* list of conditions and the following disclaimer in the documentation and/or +* other materials provided with the distribution. +* +* Neither the name of the NXP Semiconductor nor the names of its +* contributors may be used to endorse or promote products derived from this +* software without specific prior written permission. +* +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +* POSSIBILITY OF SUCH DAMAGE. +* +*/ + +#include +#include + + +class Conf +{ + public: + struct UsbConf + { + size_t m_BulkMaxTransfer; + uint64_t m_BulkTimeout; + size_t m_HIDMaxTransfer; + uint64_t m_HIDTimeout; + }; + public: + static Conf& GetConf(); + UsbConf m_USB; + + private: + Conf(); +}; diff --git a/libuuu/fastboot.cpp b/libuuu/fastboot.cpp index ba108036..4a379e14 100644 --- a/libuuu/fastboot.cpp +++ b/libuuu/fastboot.cpp @@ -43,6 +43,7 @@ #include #include #include "sparse.h" +#include "conf.h" int FastBoot::Transport(string cmd, void *p, size_t size, vector *input) { @@ -170,7 +171,6 @@ int FBCmd::run(CmdCtx *ctx) if (dev.open(ctx->m_dev)) return -1; - dev.m_timeout = m_timeout; FastBoot fb(&dev); string cmd; @@ -212,6 +212,10 @@ int FBCopy::parser(char *p) size_t pos = 0; string s; + + if (parser_protocal(p, pos)) + return -1; + s = get_next_param(m_cmd, pos); if (s.find(":") != s.npos) s = get_next_param(m_cmd, pos); @@ -310,11 +314,13 @@ int FBCopy::run(CmdCtx *ctx) nt.total = buff->size(); call_notify(nt); - for (i = 0; i < buff->size(); i += this->m_Maxsize_pre_cmd) + for (i = 0; + i < buff->size(); + i += Conf::GetConf().m_USB.m_BulkMaxTransfer) { size_t sz = buff->size() - i; - if (sz > m_Maxsize_pre_cmd) - sz = m_Maxsize_pre_cmd; + if (sz > Conf::GetConf().m_USB.m_BulkMaxTransfer) + sz = Conf::GetConf().m_USB.m_BulkMaxTransfer; cmd.format("donwload:%08X", sz); if (fb.Transport(cmd, buff->data() + i, sz)) @@ -515,7 +521,6 @@ int FBFlashCmd::run(CmdCtx *ctx) return -1; FastBoot fb(&dev); - dev.m_timeout = m_timeout; shared_ptr pdata = get_file_buffer(m_filename); if (pdata == NULL) diff --git a/libuuu/fastboot.h b/libuuu/fastboot.h index 6fddb4b3..80b1029a 100644 --- a/libuuu/fastboot.h +++ b/libuuu/fastboot.h @@ -136,14 +136,13 @@ class FBDownload : public CmdBase int run(CmdCtx *ctx); }; -class FBCopy : public CmdBase +class FBCopy : public FBCmd { public: string m_local_file; string m_target_file; bool m_bDownload; - size_t m_Maxsize_pre_cmd; int parser(char *p=NULL); - FBCopy(char *p) :CmdBase(p) { m_Maxsize_pre_cmd = 0x10000; }; + FBCopy(char *p) :FBCmd(p) { }; int run(CmdCtx *ctx); }; diff --git a/libuuu/sdp.cpp b/libuuu/sdp.cpp index a5041b81..38319675 100644 --- a/libuuu/sdp.cpp +++ b/libuuu/sdp.cpp @@ -355,7 +355,6 @@ int SDPJumpCmd::run(CmdCtx *ctx) int SDPBootlogCmd::run(CmdCtx *ctx) { HIDTrans dev; - dev.m_read_timeout = 2000; if (dev.open(ctx->m_dev)) return -1; @@ -383,4 +382,4 @@ int SDPBootlogCmd::run(CmdCtx *ctx) } } return 0; -} \ No newline at end of file +} diff --git a/libuuu/trans.cpp b/libuuu/trans.cpp index 94e3b4c1..af547441 100644 --- a/libuuu/trans.cpp +++ b/libuuu/trans.cpp @@ -28,11 +28,12 @@ * POSSIBILITY OF SUCH DAMAGE. * */ - +#include #include "trans.h" #include "libuuu.h" #include "liberror.h" #include "libusb.h" +#include "conf.h" extern "C" { @@ -112,26 +113,38 @@ int HIDTrans::read(void *buff, size_t size, size_t *rsize) { int ret; int actual; - ret = libusb_interrupt_transfer( - (libusb_device_handle *)m_devhandle, - 0x81, - (uint8_t*)buff, - size, - &actual, - m_read_timeout - ); - - *rsize = actual; - if (ret < 0) + for (size_t i = 0; i < size; ) { - string error; - string err; - err = "HID(R):"; - err += libusb_error_name(ret); - set_last_err_string(err); - return ret; - } + uint8_t *p = (uint8_t *)buff; + p += i; + size_t sz; + sz = size - i; + if (sz > Conf::GetConf().m_USB.m_HIDMaxTransfer) + sz = Conf::GetConf().m_USB.m_HIDMaxTransfer; + + ret = libusb_interrupt_transfer( + (libusb_device_handle *)m_devhandle, + 0x81, + p, + sz, + &actual, + Conf::GetConf().m_USB.m_HIDTimeout + ); + + *rsize = actual; + + if (ret < 0) + { + string error; + string err; + err = "HID(R):"; + err += libusb_error_name(ret); + set_last_err_string(err); + return ret; + } + i += sz; + } return 0; } @@ -140,14 +153,15 @@ int BulkTrans::write(void *buff, size_t size) { int ret; int actual_lenght; - for (size_t i = 0; i < size; i += m_MaxTransPreRequest) + int retries_on_timeout=3; + for (size_t i = 0; i < size; ) { uint8_t *p = (uint8_t *)buff; p += i; size_t sz; sz = size - i; - if (sz > m_MaxTransPreRequest) - sz = m_MaxTransPreRequest; + if (sz > Conf::GetConf().m_USB.m_BulkMaxTransfer) + sz = Conf::GetConf().m_USB.m_BulkMaxTransfer; ret = libusb_bulk_transfer( (libusb_device_handle *)m_devhandle, @@ -155,18 +169,53 @@ int BulkTrans::write(void *buff, size_t size) p, sz, &actual_lenght, - m_timeout + Conf::GetConf().m_USB.m_BulkTimeout ); if (ret < 0) { - string error; - string err; - err = "Bulk(W):"; - err += libusb_error_name(ret); - set_last_err_string(err); - return ret; + if (ret == LIBUSB_ERROR_TIMEOUT && actual_lenght != 0) + { + cerr << "warning : timeout, transferred :" + << actual_lenght + << " on " + << sz + << " after " + << Conf::GetConf().m_USB.m_BulkTimeout + << " ms" + << endl; + retries_on_timeout = 3; + } + else if (ret == LIBUSB_ERROR_TIMEOUT && actual_lenght == 0 && retries_on_timeout != 0) + { + cerr << "warning : timeout, transferred :" + << actual_lenght + << " on " + << sz + << " after " + << Conf::GetConf().m_USB.m_BulkTimeout + << " ms, retry " + << retries_on_timeout + << endl; + retries_on_timeout --; + sz = actual_lenght; + } + else + { + string error; + string err; + err = "Bulk(W):"; + err += libusb_error_name(ret); + cerr << " transferred :" << actual_lenght << " on " << sz << endl; + set_last_err_string(err); + return ret; + } + } + else + { + retries_on_timeout = 3; } + i += sz; } //Send zero package @@ -223,7 +272,7 @@ int BulkTrans::read(void *buff, size_t size, size_t *rsize) p, size, &actual_lenght, - m_timeout + Conf::GetConf().m_USB.m_BulkTimeout ); *rsize = actual_lenght; diff --git a/libuuu/trans.h b/libuuu/trans.h index ad2cb04b..e5807500 100644 --- a/libuuu/trans.h +++ b/libuuu/trans.h @@ -78,8 +78,7 @@ class HIDTrans : public USBTrans { int m_set_report; public: - int m_read_timeout; - HIDTrans() { m_set_report = 9; m_read_timeout = 1000; } + HIDTrans() { m_set_report = 9; } ~HIDTrans() { if (m_devhandle) close(); m_devhandle = NULL; } int write(void *buff, size_t size); int read(void *buff, size_t size, size_t *return_size); @@ -89,17 +88,13 @@ class BulkTrans : public USBTrans { void Init() { - m_MaxTransPreRequest = 0x100000; m_b_send_zero = 0; - m_timeout = 2000; } public: EPInfo m_ep_in; EPInfo m_ep_out; - size_t m_MaxTransPreRequest; int m_b_send_zero; - uint64_t m_timeout; BulkTrans() { Init(); From 1b83dc746815d3984712913c08efd5d4e63020e3 Mon Sep 17 00:00:00 2001 From: "jeanluc.malet" Date: Thu, 24 Jan 2019 13:47:58 +0100 Subject: [PATCH 2/4] now support tunning in script, restore default values --- libuuu/cmd.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++++--- libuuu/cmd.h | 10 ++++++ libuuu/conf.cpp | 4 +-- 3 files changed, 96 insertions(+), 6 deletions(-) diff --git a/libuuu/cmd.cpp b/libuuu/cmd.cpp index 57f989e2..c9b0d5fa 100644 --- a/libuuu/cmd.cpp +++ b/libuuu/cmd.cpp @@ -41,6 +41,7 @@ #include "buffer.h" #include "sdp.h" #include "fastboot.h" +#include "conf.h" #include #include @@ -235,12 +236,23 @@ int get_string_in_square_brackets(string &cmd, string &context) uint32_t str_to_uint(string &str) { - if (str.size() > 2) + uint32_t val = 0; + const char * start = str.substr(0).c_str(); + char * endPtr = const_cast(start); + if (str.size() > 2 && + str.substr(0, 2).compare("0x") == 0) { - if (str.substr(0, 2).compare("0x") == 0) - return strtoul(str.substr(2).c_str(), NULL, 16); + val = strtoul(start+2, &endPtr, 16); } - return strtoul(str.c_str(), NULL, 10); + else + { + val = strtoul(start, &endPtr, 10); + } + if (start == endPtr) + { + throw ("Trying to convert non numeric value to int"); + } + return val; } template shared_ptr new_cmd_obj(char *p) @@ -289,6 +301,7 @@ CmdObjCreateMap::CmdObjCreateMap() (*this)["_ALL:DONE"] = new_cmd_obj; (*this)["_ALL:DELAY"] = new_cmd_obj; + (*this)["_ALL:CONFIG"] = new_cmd_obj; (*this)["_ALL:SH"] = new_cmd_obj; (*this)["_ALL:SHELL"] = new_cmd_obj; (*this)["_ALL:<"] = new_cmd_obj; @@ -415,6 +428,73 @@ int CmdDelay::run(CmdCtx *) return 0; } +int CmdConfig::parser(char * /*p*/) +{ + try + { + size_t pos = 0; + string param = get_next_param(m_cmd, pos); + + if (param.find(':') != string::npos) + param = get_next_param(m_cmd, pos); + + if (str_to_upper(param) != "CONFIG") + { + string err = "CmdConfig::parser Uknown Commnd:"; + err += param; + set_last_err_string(err); + return -1; + } + + string args = get_next_param(m_cmd, pos); + if (str_to_upper(args) == "USB::HID::MAX_PACKET_SIZE") + { + string size = get_next_param(m_cmd, pos); + Conf::GetConf().m_USB.m_HIDMaxTransfer = str_to_uint(size); + cout << "changed USB::HID::MAX_PACKET_SIZE to " << Conf::GetConf().m_USB.m_HIDMaxTransfer << endl; + } + else if (str_to_upper(args) == "USB::HID::TIMEOUT" ) + { + string ms = get_next_param(m_cmd, pos); + Conf::GetConf().m_USB.m_HIDTimeout = str_to_uint(ms); + cout << "changed USB::HID::TIMEOUT to " << Conf::GetConf().m_USB.m_HIDTimeout << endl; + } + else if (str_to_upper(args) == "USB::BULK::MAX_PACKET_SIZE" ) + { + string size = get_next_param(m_cmd, pos); + Conf::GetConf().m_USB.m_BulkMaxTransfer = str_to_uint(size); + cout << "changed USB::BULK::MAX_PACKET_SIZE to " << Conf::GetConf().m_USB.m_BulkMaxTransfer << endl; + } + else if (str_to_upper(args) == "USB::BULK::TIMEOUT") + { + string ms = get_next_param(m_cmd, pos); + Conf::GetConf().m_USB.m_BulkTimeout = str_to_uint(ms); + cout << "changed CONF::USB::BULK::TIMEOUT to " << Conf::GetConf().m_USB.m_BulkTimeout << endl; + } + else + { + string err = "CmdConfig::parser invalid parameter:"; + err += m_cmd; + set_last_err_string(err); + return -1; + + } + } + catch (string const& except) + { + string err(" error at line : "); + err += except; + throw err; + } + return 0; +} + +int CmdConfig::run(CmdCtx *) +{ + return 0; +} + + int CmdShell::parser(char * p) { if (p) diff --git a/libuuu/cmd.h b/libuuu/cmd.h index 46d2e46a..b187cd6f 100644 --- a/libuuu/cmd.h +++ b/libuuu/cmd.h @@ -161,6 +161,16 @@ class CmdDelay :public CmdBase int run(CmdCtx *p); }; +class CmdConfig :public CmdBase +{ +public: + virtual int parser(char *p = NULL); + CmdConfig(char *p) :CmdBase(p) { }; + int run(CmdCtx *p); +}; + + + class CmdShell : public CmdBase { public: diff --git a/libuuu/conf.cpp b/libuuu/conf.cpp index 91dbfacc..cbbe07e0 100644 --- a/libuuu/conf.cpp +++ b/libuuu/conf.cpp @@ -36,9 +36,9 @@ Conf::Conf() { m_USB.m_BulkMaxTransfer = 0x10000; - m_USB.m_BulkTimeout = 9000; + m_USB.m_BulkTimeout = 2000; m_USB.m_HIDMaxTransfer= 0x10000; - m_USB.m_HIDTimeout = 9000; + m_USB.m_HIDTimeout = 2000; } Conf& Conf::GetConf() From e86abaf96f232246eb01d1771c3390a864df1401 Mon Sep 17 00:00:00 2001 From: "jeanluc.malet" Date: Mon, 4 Feb 2019 17:42:54 +0100 Subject: [PATCH 3/4] bugfix in parsing int.... --- libuuu/cmd.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/libuuu/cmd.cpp b/libuuu/cmd.cpp index c9b0d5fa..fdcfd70f 100644 --- a/libuuu/cmd.cpp +++ b/libuuu/cmd.cpp @@ -45,8 +45,9 @@ #include #include -#include -#include +#include +#include +#include static CmdMap g_cmd_map; static CmdObjCreateMap g_cmd_create_map; @@ -237,21 +238,17 @@ int get_string_in_square_brackets(string &cmd, string &context) uint32_t str_to_uint(string &str) { uint32_t val = 0; - const char * start = str.substr(0).c_str(); - char * endPtr = const_cast(start); + std::stringstream ss; if (str.size() > 2 && str.substr(0, 2).compare("0x") == 0) { - val = strtoul(start+2, &endPtr, 16); + ss << std::hex << str.substr(2); } else { - val = strtoul(start, &endPtr, 10); - } - if (start == endPtr) - { - throw ("Trying to convert non numeric value to int"); + ss << std::dec << str; } + ss >> val; return val; } From 1be0f4b9e03569224c321d63e6149addaeab0130 Mon Sep 17 00:00:00 2001 From: "jeanluc.malet" Date: Fri, 29 Mar 2019 16:26:19 +0100 Subject: [PATCH 4/4] added conf.cpp to msvc projet --- msvc/libuuu.vcxproj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/msvc/libuuu.vcxproj b/msvc/libuuu.vcxproj index 292346e9..6c6b5ab1 100644 --- a/msvc/libuuu.vcxproj +++ b/msvc/libuuu.vcxproj @@ -33,6 +33,7 @@ + @@ -208,4 +209,4 @@ - \ No newline at end of file +