Move functions from sdp.h to sdp.cpp
[mfgtools.git] / libuuu / cmd.cpp
blob83384d09c4d86ce67ebf269cfedeabdc669d05b6
1 /*
2 * Copyright 2018 NXP.
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice, this
11 * list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
14 * Neither the name of the NXP Semiconductor nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 #include <memory>
33 #include <string.h>
34 #include "cmd.h"
35 #include "libcomm.h"
36 #include "libuuu.h"
37 #include "config.h"
38 #include "trans.h"
39 #include "sdps.h"
40 #include <atomic>
41 #include "buffer.h"
42 #include "sdp.h"
43 #include "fastboot.h"
44 #include <sys/stat.h>
45 #include <thread>
47 #include <stdio.h>
48 #include <stdlib.h>
50 static CmdMap g_cmd_map;
51 static CmdObjCreateMap g_cmd_create_map;
52 static string g_cmd_list_file;
54 int parser_cmd_list_file(shared_ptr<FileBuffer> pbuff, CmdMap *pCmdMap = NULL);
56 template <class T>
57 void * create_object() { return new T; }
59 typedef void * (*FN)();
61 FN g_fn = create_object<int>;
63 CmdCtx::~CmdCtx()
67 CmdBase::~CmdBase()
71 int CmdBase::parser(char *p)
73 if (p != NULL)
74 m_cmd = p;
76 size_t pos = 0;
77 string param = get_next_param(m_cmd, pos);
79 if (param.find(':') != string::npos)
80 param = get_next_param(m_cmd, pos);
82 int index = 0;
84 while (pos < m_cmd.size())
86 param = get_next_param(m_cmd, pos);
88 struct Param *pp = NULL;
90 if (m_NoKeyParam)
92 if (index > m_param.size())
94 set_last_err_string("More parameter then expected");
95 return -1;
97 pp = &(m_param[index]);
98 index++;
100 else
102 for (size_t i = 0; i < m_param.size(); i++)
104 string key = string(m_param[i].key);
105 if (compare_str(param, key, m_param[i].ignore_case))
107 pp = &(m_param[i]);
108 break;
113 if (pp == NULL)
115 string err;
116 err = "unknown Option";
117 err += param;
118 set_last_err_string(err);
119 return -1;
122 if (pp->type == Param::Type::e_uint32)
124 if (!m_NoKeyParam)
125 param = get_next_param(m_cmd, pos);
126 *(uint32_t*)pp->pData = str_to_uint32(param);
129 if (pp->type == Param::Type::e_string_filename)
131 if (!m_NoKeyParam)
132 param = get_next_param(m_cmd, pos);
133 *(string*)pp->pData = param;
135 if (!check_file_exist(param))
136 return -1;
139 if (pp->type == Param::Type::e_string)
141 if (!m_NoKeyParam)
142 param = get_next_param(m_cmd, pos);
143 *(string*)pp->pData = param;
146 if (pp->type == Param::Type::e_bool)
148 *(bool*)pp->pData = true;
151 if (pp->type == Param::Type::e_null)
156 if (m_bCheckTotalParam)
158 if (index < m_param.size())
160 string str;
161 str += "Missed: ";
162 str += m_param[index].Error;
163 set_last_err_string(str);
164 return -1;
167 return 0;
170 int CmdBase::parser_protocal(char *p, size_t &pos)
172 if (p)
173 m_cmd = *p;
175 string prot = get_next_param(m_cmd, pos, ':');
176 string param;
177 if (get_string_in_square_brackets(prot, param))
178 return -1;
180 if (!param.empty())
182 size_t param_pos = 0;
183 string s = get_next_param(param, param_pos);
185 if (s == "-t")
187 string timeout;
188 timeout = get_next_param(param, param_pos);
189 m_timeout = str_to_uint32(timeout);
191 else
193 string err;
194 err = "Unknown option: ";
195 err += s;
196 err += " for protocol: ";
197 err += remove_square_brackets(prot);
198 set_last_err_string(err);
199 return -1;
202 return 0;
205 int CmdBase::dump()
207 uuu_notify nt;
208 nt.type = uuu_notify::NOTIFY_CMD_INFO;
210 string str = m_cmd;
211 str += "\n";
212 nt.str = (char*)str.c_str();
213 call_notify(nt);
215 return 0;
218 int CmdList::run_all(CmdCtx *p, bool dry)
220 CmdList::iterator it;
221 int ret;
223 uuu_notify nt;
224 nt.type = uuu_notify::NOTIFY_CMD_TOTAL;
225 nt.total = size();
226 call_notify(nt);
228 int i = 0;
230 for (it = begin(); it != end(); it++, i++)
232 uuu_notify nt;
234 nt.type = uuu_notify::NOTIFY_CMD_INDEX;
235 nt.index = i;
236 call_notify(nt);
238 nt.type = uuu_notify::NOTIFY_CMD_START;
239 nt.str = (char *)(*it)->m_cmd.c_str();
240 call_notify(nt);
242 if (dry)
243 ret = (*it)->dump();
244 else
245 ret = (*it)->run(p);
247 nt.type = uuu_notify::NOTIFY_CMD_END;
248 nt.status = ret;
249 call_notify(nt);
250 if (ret)
251 return ret;
253 if ((*it)->m_lastcmd)
254 break;
256 return ret;
259 int CmdMap::run_all(const std::string &protocol, CmdCtx *p, bool dry_run)
261 if (find(protocol) == end())
263 set_last_err_id(-1);
264 std::string err;
265 err.append("Unknown Protocal:");
266 err.append(protocol);
267 set_last_err_string(err);
268 return -1;
270 return at(protocol)->run_all(p, dry_run);
273 string get_next_param(const string &cmd, size_t &pos, char sperate)
275 string str;
276 if (pos == string::npos)
277 return str;
278 if (pos >= cmd.size())
279 return str;
281 //trim left space
282 while (cmd[pos] == sperate && pos < cmd.size())
283 pos++;
285 bool quate = false;
286 size_t end = string::npos;
288 for (size_t s = pos; s < cmd.size(); s++)
290 if (cmd[s] == '"')
291 quate = !quate;
293 if (!quate && cmd[s] == sperate)
295 end = s;
296 break;
300 if (end == cmd.npos)
301 end = cmd.size();
303 str = cmd.substr(pos, end - pos);
304 pos = end + 1;
306 return str;
309 string remove_square_brackets(const string &cmd)
311 size_t sz=cmd.find('[');
312 return cmd.substr(0, sz);
315 int get_string_in_square_brackets(const string &cmd, string &context)
317 size_t start = cmd.find('[');
318 if (start == string::npos)
320 context.clear();
321 return 0;
324 size_t end = cmd.find(']', start);
325 if (end == string::npos)
327 set_last_err_string("missed ]");
328 return -1;
331 context = cmd.substr(start + 1, end - start - 1);
332 return 0;
335 template<typename T, uint64_t MAX_VAL>
336 T str_to_uint(const std::string &str, bool * conversion_succeeded)
338 if (conversion_succeeded) *conversion_succeeded = false;
340 int base = 10;
341 if (str.size() > 2)
343 if (str.substr(0, 2).compare("0x") == 0)
345 base = 16;
349 try {
350 const auto tmp_val = std::stoull(str, nullptr, base);
351 if (tmp_val <= MAX_VAL)
353 if (conversion_succeeded) *conversion_succeeded = true;
354 return static_cast<T>(tmp_val);
356 } catch (const std::invalid_argument &) {
357 } catch (const std::out_of_range &) {
360 set_last_err_string("Conversion of string to unsigned failed");
362 return MAX_VAL;
365 uint16_t str_to_uint16(const string &str, bool * conversion_suceeded)
367 return str_to_uint<uint16_t, UINT16_MAX>(str, conversion_suceeded);
370 uint32_t str_to_uint32(const string &str, bool * conversion_suceeded)
372 return str_to_uint<uint32_t, UINT32_MAX>(str, conversion_suceeded);
375 uint64_t str_to_uint64(const string &str, bool * conversion_suceeded)
377 return str_to_uint<uint64_t, UINT64_MAX>(str, conversion_suceeded);
380 template <class T> shared_ptr<CmdBase> new_cmd_obj(char *p)
382 return shared_ptr<CmdBase>(new T(p));
385 CmdObjCreateMap::CmdObjCreateMap()
387 (*this)["CFG:"] = new_cmd_obj<CfgCmd>;
389 (*this)["SDPS:BOOT"] = new_cmd_obj<SDPSCmd>;
391 (*this)["SDP:DCD"] = new_cmd_obj<SDPDcdCmd>;
392 (*this)["SDP:JUMP"] = new_cmd_obj<SDPJumpCmd>;
393 (*this)["SDP:RDMEM"] = new_cmd_obj<SDPReadMemCmd>;
394 (*this)["SDP:WRMEM"] = new_cmd_obj<SDPWriteMemCmd>;
395 (*this)["SDP:WRITE"] = new_cmd_obj<SDPWriteCmd>;
396 (*this)["SDP:STATUS"] = new_cmd_obj<SDPStatusCmd>;
397 (*this)["SDP:BOOT"] = new_cmd_obj<SDPBootCmd>;
398 (*this)["SDP:BLOG"] = new_cmd_obj<SDPBootlogCmd>;
400 (*this)["SDPU:JUMP"] = new_cmd_obj<SDPJumpCmd>;
401 (*this)["SDPU:WRITE"] = new_cmd_obj<SDPWriteCmd>;
402 (*this)["SDPU:BLOG"] = new_cmd_obj<SDPBootlogCmd>;
404 (*this)["SDPV:JUMP"] = new_cmd_obj<SDPJumpCmd>;
405 (*this)["SDPV:WRITE"] = new_cmd_obj<SDPWriteCmd>;
406 (*this)["SDPV:BLOG"] = new_cmd_obj<SDPBootlogCmd>;
408 (*this)["FB:GETVAR"] = new_cmd_obj<FBGetVar>;
409 (*this)["FASTBOOT:GETVAR"] = new_cmd_obj<FBGetVar>;
410 (*this)["FB:UCMD"] = new_cmd_obj<FBUCmd>;
411 (*this)["FASTBOOT:UCMD"] = new_cmd_obj<FBUCmd>;
412 (*this)["FB:ACMD"] = new_cmd_obj<FBACmd>;
413 (*this)["FASTBOOT:ACMD"] = new_cmd_obj<FBACmd>;
414 (*this)["FB:DOWNLOAD"] = new_cmd_obj<FBDownload>;
415 (*this)["FASTBOOT:DOWNLOAD"] = new_cmd_obj<FBDownload>;
416 (*this)["FB:FLASH"] = new_cmd_obj<FBFlashCmd>;
417 (*this)["FASTBOOT:FLASH"] = new_cmd_obj<FBFlashCmd>;
418 (*this)["FB:ERASE"] = new_cmd_obj<FBEraseCmd>;
419 (*this)["FASTBOOT:ERASE"] = new_cmd_obj<FBEraseCmd>;
420 (*this)["FB:OEM"] = new_cmd_obj<FBOemCmd>;
421 (*this)["FASTBOOT:OEM"] = new_cmd_obj<FBOemCmd>;
422 (*this)["FB:FLASHING"] = new_cmd_obj<FBFlashingCmd>;
423 (*this)["FASTBOOT:FLASHING"] = new_cmd_obj<FBFlashingCmd>;
424 (*this)["FB:SET_ACTIVE"] = new_cmd_obj<FBSetActiveCmd>;
425 (*this)["FASTBOOT:SET_ACTIVE"] = new_cmd_obj<FBSetActiveCmd>;
426 (*this)["FB:CONTINUE"] = new_cmd_obj<FBContinueCmd>;
427 (*this)["FASTBOOT:CONTINUE"] = new_cmd_obj<FBContinueCmd>;
429 (*this)["FB:UPDATE-SUPER"] = new_cmd_obj<FBUpdateSuper>;
430 (*this)["FASTBOOT:UPDATE-SUPER"] = new_cmd_obj<FBUpdateSuper>;
431 (*this)["FB:CREATE-LOGICAL-PARTITION"] = new_cmd_obj<FBCreatePartition>;
432 (*this)["FASTBOOT:CREATE-LOGICAL-PARTITION"] = new_cmd_obj<FBCreatePartition>;
433 (*this)["FB:DELETE-LOGICAL-PARTITION"] = new_cmd_obj<FBDelPartition>;
434 (*this)["FASTBOOT:DELETE-LOGICAL-PARTITION"] = new_cmd_obj<FBDelPartition>;
435 (*this)["FB:RESIZE-LOGICAL-PARTITION"] = new_cmd_obj<FBResizePartition>;
436 (*this)["FASTBOOT:RESIZE-LOGICAL-PARTITION"] = new_cmd_obj<FBResizePartition>;
438 (*this)["FBK:UCMD"] = new_cmd_obj<FBUCmd>;
439 (*this)["FBK:ACMD"] = new_cmd_obj<FBACmd>;
440 (*this)["FBK:SYNC"] = new_cmd_obj<FBSyncCmd>;
441 (*this)["FBK:UCP"] = new_cmd_obj<FBCopy>;
443 (*this)["_ALL:DONE"] = new_cmd_obj<CmdDone>;
444 (*this)["_ALL:DELAY"] = new_cmd_obj<CmdDelay>;
445 (*this)["_ALL:SH"] = new_cmd_obj<CmdShell>;
446 (*this)["_ALL:SHELL"] = new_cmd_obj<CmdShell>;
447 (*this)["_ALL:<"] = new_cmd_obj<CmdShell>;
451 shared_ptr<CmdBase> create_cmd_obj(string cmd)
453 string param;
454 size_t pos = 0;
455 param = get_next_param(cmd, pos, ':');
456 param = remove_square_brackets(param);
457 param += ":";
458 param = str_to_upper(param);
460 if (g_cmd_create_map.find(param) == g_cmd_create_map.end())
462 string s = param;
463 param = get_next_param(cmd, pos);
464 s += str_to_upper(param);
465 if (g_cmd_create_map.find(s) != g_cmd_create_map.end())
466 return g_cmd_create_map[s]((char*)cmd.c_str());
468 string commoncmd = "_ALL:";
469 commoncmd += str_to_upper(param);
470 if (g_cmd_create_map.find(commoncmd) != g_cmd_create_map.end())
471 return g_cmd_create_map[commoncmd]((char*)cmd.c_str());
473 else
475 return g_cmd_create_map[param]((char*)cmd.c_str());
478 string err;
479 err = "Unknown Command:";
480 err += cmd;
481 set_last_err_string(err);
482 return NULL;
485 int uuu_run_cmd(const char * cmd, int dry)
487 shared_ptr<CmdBase> p;
488 p = create_cmd_obj(cmd);
489 int ret;
491 if (p == NULL)
492 return -1;
494 uuu_notify nt;
495 nt.type = uuu_notify::NOTIFY_CMD_TOTAL;
496 nt.total = 1;
497 call_notify(nt);
499 nt.type = uuu_notify::NOTIFY_CMD_START;
500 nt.str = (char *)p->m_cmd.c_str();
501 call_notify(nt);
503 if (typeid(*p) != typeid(CfgCmd))
505 size_t pos = 0;
506 string c = cmd;
508 string pro = get_next_param(c, pos, ':');
509 pro = remove_square_brackets(pro);
510 pro += ":";
512 if (p->parser())
513 ret = -1;
514 else
516 if (dry)
518 ret = p->dump();
519 }else
521 CmdUsbCtx ctx;
522 ret = ctx.look_for_match_device(pro.c_str());
523 if (ret)
524 return ret;
526 ret = p->run(&ctx);
530 else
532 return ret = dry? p->dump() : p->run(NULL);
535 nt.type = uuu_notify::NOTIFY_CMD_END;
536 nt.status = ret;
537 call_notify(nt);
539 return ret;
542 int CmdDone::run(CmdCtx *)
544 uuu_notify nt;
545 nt.type = uuu_notify::NOTIFY_DONE;
546 call_notify(nt);
547 return 0;
550 int CmdDelay::parser(char * /*p*/)
552 size_t pos = 0;
553 string param = get_next_param(m_cmd, pos);
555 if (param.find(':') != string::npos)
556 param = get_next_param(m_cmd, pos);
558 if (str_to_upper(param) != "DELAY")
560 string err = "Unknown Commnd:";
561 err += param;
562 set_last_err_string(err);
563 return -1;
566 string ms = get_next_param(m_cmd, pos);
567 m_ms = str_to_uint32(ms);
568 return 0;
571 int CmdDelay::run(CmdCtx *)
573 std::this_thread::sleep_for(std::chrono::milliseconds(m_ms));
574 return 0;
577 int CmdShell::parser(char * p)
579 if (p)
580 m_cmd = p;
582 size_t pos = 0;
583 string s;
585 if (parser_protocal(p, pos))
586 return -1;
588 m_protocal = m_cmd.substr(0, pos);
590 s = get_next_param(m_cmd, pos);
592 m_dyn = (s == "<");
594 if (pos != string::npos && pos < m_cmd.size())
595 m_shellcmd = m_cmd.substr(pos);
597 return 0;
600 int CmdShell::run(CmdCtx*)
602 #ifndef WIN32
603 #define _popen popen
604 #define _pclose pclose
605 #endif
606 FILE *pipe = _popen(m_shellcmd.c_str(), "r");
608 if (pipe == NULL)
610 string err = "failure popen: ";
611 err += m_shellcmd.c_str();
612 set_last_err_string(err);
613 return -1;
616 string str;
617 str.resize(256);
618 while (fgets((char*)str.c_str(), str.size(), pipe))
620 if (m_dyn)
622 string cmd;
623 cmd = m_protocal;
624 str.resize(strlen(str.c_str()));
625 cmd += ' ';
626 cmd += str;
628 size_t pos = cmd.find_first_of("\r\n");
629 if (pos != string::npos)
630 cmd = cmd.substr(0, pos);
632 return uuu_run_cmd(cmd.c_str(), 0);
634 uuu_notify nt;
635 nt.type = uuu_notify::NOTIFY_CMD_INFO;
636 nt.str = (char*)str.c_str();
637 call_notify(nt);
640 /* Close pipe and print return value of pPipe. */
641 if (feof(pipe))
643 int ret = _pclose(pipe);
644 string_ex str;
645 str.format("\nProcess returned %d\n", ret);;
646 if (ret)
648 set_last_err_string(str.c_str());
649 return ret;
652 else
654 set_last_err_string("Error: Failed to read the pipe to the end.\n");
655 return -1;
658 return 0;
661 int run_cmds(const char *procotal, CmdCtx *p)
663 CmdMap cmdmap, *pCmdMap;
665 if (!g_cmd_list_file.empty())
667 shared_ptr<FileBuffer> pbuff = get_file_buffer(g_cmd_list_file);
668 if (pbuff == NULL)
669 return -1;
670 if(parser_cmd_list_file(pbuff, &cmdmap))
671 return -1;
672 pCmdMap = &cmdmap;
674 else
676 pCmdMap = &g_cmd_map;
679 if (pCmdMap->find(procotal) == pCmdMap->end())
681 return 0;
684 return (*pCmdMap)[procotal]->run_all(p);
687 static int insert_one_cmd(const char * cmd, CmdMap *pCmdMap)
689 string s = cmd;
690 size_t pos = 0;
692 string pro = get_next_param(s, pos, ':');
693 pro = remove_square_brackets(pro);
694 pro += ":";
696 pro = str_to_upper(pro);
698 shared_ptr<CmdBase> p = create_cmd_obj(s);
699 if (p == NULL)
700 return -1;
702 if (p->parser())
703 return -1;
705 if (pCmdMap->find(pro) == pCmdMap->end())
707 shared_ptr<CmdList> list(new CmdList);
708 (*pCmdMap)[pro] = list;
711 (*pCmdMap)[pro]->push_back(p);
713 return 0;
717 static int added_default_boot_cmd(const char *filename)
719 string str;
721 str = "SDPS: boot -f ";
722 str += "\"";
723 str += filename;
724 str += "\"";
726 int ret = insert_one_cmd(str.c_str(), &g_cmd_map);
727 if (ret) return ret;
729 insert_one_cmd("SDPS: done", &g_cmd_map);
731 str = "SDP: boot -f ";
732 str += "\"";
733 str += filename;
734 str += "\"";
736 ret = insert_one_cmd(str.c_str(), &g_cmd_map);
737 if (ret) return ret;
739 insert_one_cmd("SDP: done", &g_cmd_map);
741 str = "SDPU: write -f ";
742 str += "\"";
743 str += filename;
744 str += "\"";
745 str += " -offset 0x57c00";
746 insert_one_cmd(str.c_str(), &g_cmd_map);
747 insert_one_cmd("SDPU: jump", &g_cmd_map);
748 insert_one_cmd("SDPU: done", &g_cmd_map);
750 str = "SDPV: write -f ";
751 str += "\"";
752 str += filename;
753 str += "\"";
754 str += " -skipspl";
755 insert_one_cmd(str.c_str(), &g_cmd_map);
756 insert_one_cmd("SDPV: jump", &g_cmd_map);
757 insert_one_cmd("SDPV: done", &g_cmd_map);
759 return 0;
762 int check_version(string str)
764 int x = 0;
765 int ver = 0;
766 for (size_t i = 0; i < str.size(); i++)
768 char c = str[i];
769 if (c >= '0' && c <= '9')
771 x *= 10;
772 x += c - '0';
774 if (c == '.' || i == str.size()-1 || c == '\n')
776 ver <<= 8;
777 ver += x;
778 x = 0;
782 int cur = uuu_get_version();
784 if (ver > cur)
786 string str;
787 str = "Current uuu version is too low, please download latest one";
788 set_last_err_string(str);
789 return -1;
791 return 0;
794 int uuu_run_cmd_script(const char * buff, int dry)
796 shared_ptr<FileBuffer> p(new FileBuffer((void*)buff, strlen(buff)));
798 return parser_cmd_list_file(p);
801 int parser_cmd_list_file(shared_ptr<FileBuffer> pbuff, CmdMap *pCmdMap)
803 char uuu_version[] = "uuu_version";
804 string str;
806 if (pCmdMap == NULL)
807 pCmdMap = &g_cmd_map;
809 pCmdMap->clear();
811 for (size_t i = 0; i < pbuff->size(); i++)
813 uint8_t c = pbuff->at(i);
814 if (c == '\r')
815 continue;
817 if(c != '\n')
818 str.push_back(c);
820 if (c == '\n' || c == 0 || i == pbuff->size() - 1)
822 if (str.substr(0, strlen(uuu_version)) == uuu_version)
824 if (check_version(str.substr(strlen(uuu_version), 10)))
826 return -1;
828 }else if (str.size() > 1)
830 if (str[0] != '#')
831 if (insert_one_cmd(str.c_str(), pCmdMap))
832 return -1;
834 str.clear();
837 return 0;
840 int uuu_auto_detect_file(const char *filename)
842 string_ex fn;
843 fn += remove_quota(filename);
844 fn.replace('\\', '/');
846 if (fn.empty())
847 fn += "./";
849 string oldfn =fn;
851 fn += "/uuu.auto";
852 shared_ptr<FileBuffer> buffer = get_file_buffer(fn);
853 if (buffer == NULL)
855 fn.clear();
856 fn += oldfn;
857 size_t pos = str_to_upper(fn).find("ZIP");
858 if(pos == string::npos || pos != fn.size() - 3)
860 pos = str_to_upper(fn).find("SDCARD");
861 if (pos == string::npos || pos != fn.size() - 6)
862 buffer = get_file_buffer(fn); //we don't try open a zip file here
865 if(buffer == NULL)
866 return -1;
869 string str= "uuu_version";
870 void *p1 = buffer->data();
871 void *p2 = (void*)str.data();
872 if (memcmp(p1, p2, str.size()) == 0)
874 size_t pos = fn.rfind('/');
875 if (pos != string::npos)
876 set_current_dir(fn.substr(0, pos + 1));
878 g_cmd_list_file = fn.substr(pos+1);
880 return parser_cmd_list_file(buffer);
883 //flash.bin or uboot.bin
884 return added_default_boot_cmd(fn.c_str());
887 int notify_done(uuu_notify nt, void *p)
889 if(nt.type == uuu_notify::NOTIFY_DONE)
890 *(std::atomic<int> *) p = 1;
891 if (nt.type == uuu_notify::NOTIFY_CMD_END && nt.status)
892 *(std::atomic<int> *) p = 1;
894 return 0;
896 int uuu_wait_uuu_finish(int deamon, int dry)
898 std::atomic<int> exit;
899 exit = 0;
901 if(dry) {
902 for(auto it=g_cmd_map.begin(); it != g_cmd_map.end(); it++)
904 for(auto cmd = it->second->begin(); cmd != it->second->end(); cmd++)
906 (*cmd)->dump();
909 return 0;
912 if (!deamon)
913 uuu_register_notify_callback(notify_done, &exit);
915 if(polling_usb(exit))
916 return -1;
918 return 0;