1 /* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #include "translate.h" 16 #include "host_updater.h" 17 18 namespace Hdc { 19 namespace TranslateCommand { Usage()20 string Usage() 21 { 22 string ret; 23 24 ret = "\n OpenHarmony flash ...\n\n" 25 "---------------------------------global commands:----------------------------------\n" 26 " -h/help - Print flashd help\n" 27 " -v/version - Print flashd version\n" 28 " -l 0-5 - Set runtime loglevel\n" 29 " -t connectkey - Use device with given connect key\n" 30 "\n" 31 "---------------------------------component commands:-------------------------------\n" 32 "session commands(on server):\n" 33 " discover - Discover devices listening on TCP via LAN broadcast\n" 34 " list targets [-v] - List all devices status, -v for detail\n" 35 " tconn key - Connect device via key, TCP use ip:port\n" 36 " example:192.168.0.100:10178/192.168.0.100\n" 37 " USB connect automatic, TCP need to connect manually\n" 38 " start [-r] - Start server. If with '-r', will be restart server\n" 39 " kill [-r] - Kill server. If with '-r', will be restart server\n" 40 " -s [ip:]port - Set flash server listen config\n" 41 "\n" 42 "service commands(on daemon):\n" 43 " target boot [-bootloader|-recovery] - Reboot the device or boot into bootloader\\recovery.\n" 44 " tmode usb - Reboot the device, listening on USB\n" 45 " tmode port [port] - Reboot the device, listening on TCP port\n" 46 "\n" 47 "---------------------------------flash commands:-------------------------------------\n" 48 "flash commands:\n" 49 " update packagename - Update system by package\n" 50 " flash [-f] partition imagename - Flash partition by image\n" 51 " erase [-f] partition - Erase partition\n" 52 " format [-f] partition -t fs_type - Format partition -t [ext4 | f2fs]\n"; 53 return ret; 54 } 55 TargetConnect(FormatCommand * outCmd)56 string TargetConnect(FormatCommand *outCmd) 57 { 58 string stringError; 59 if (Base::StringEndsWith(outCmd->parameters, " -remove")) { 60 outCmd->parameters = outCmd->parameters.substr(0, outCmd->parameters.size() - 8); // 8 leng 61 outCmd->cmdFlag = CMD_KERNEL_TARGET_DISCONNECT; 62 } else { 63 outCmd->cmdFlag = CMD_KERNEL_TARGET_CONNECT; 64 if (outCmd->parameters.size() > 22) { // 22: tcp max=21,USB max=8bytes 65 stringError = "Error connect key's size"; 66 outCmd->bJumpDo = true; 67 } 68 } 69 if (outCmd->parameters.find(":") != std::string::npos) { 70 // tcp mode 71 string ip = outCmd->parameters.substr(0, outCmd->parameters.find(":")); 72 string sport = outCmd->parameters.substr(outCmd->parameters.find(":") + 1); 73 int port = std::stoi(sport); 74 sockaddr_in addr; 75 if ((port <= 0 || port > MAX_IP_PORT) || uv_ip4_addr(ip.c_str(), port, &addr) < 0) { 76 stringError = "IP:Port incorrect"; 77 outCmd->bJumpDo = true; 78 } 79 } 80 return stringError; 81 } 82 ForwardPort(const char * input,FormatCommand * outCmd)83 string ForwardPort(const char *input, FormatCommand *outCmd) 84 { 85 string stringError; 86 const char *pExtra = input + 6; // CMDSTR_FORWARD_FPORT CMDSTR_FORWARD_RPORT + " " size 87 if (!strcmp(pExtra, "ls")) { 88 outCmd->cmdFlag = CMD_FORWARD_LIST; 89 } else if (!strncmp(pExtra, "rm", 2)) { // 2 rm size 90 outCmd->cmdFlag = CMD_FORWARD_REMOVE; 91 if (strcmp(pExtra, "rm")) { 92 outCmd->parameters = input + 9; // 9 rm extra size 93 } 94 } else { 95 const char *p = input + 6; // 6 length 96 // clang-format off 97 if (strncmp(p, "tcp:", 4) && // 4 legnth of tcp 98 strncmp(p, "localabstract:", 14) && // 14 legnth of localabstract 99 strncmp(p, "localreserved:", 14) && // 14 legnth of localreserved 100 strncmp(p, "localfilesystem:", 16) && // 14 legnth of localfilesystem 101 strncmp(p, "dev:", 4) && // 4 legnth of dev 102 strncmp(p, "jdwp:", 5)) { // 14 legnth of jdwp 103 stringError = "Incorrect forward command"; 104 outCmd->bJumpDo = true; 105 } 106 // clang-format on 107 outCmd->cmdFlag = CMD_FORWARD_INIT; 108 outCmd->parameters = input; 109 } 110 return stringError; 111 } 112 RunMode(const char * input,FormatCommand * outCmd)113 string RunMode(const char *input, FormatCommand *outCmd) 114 { 115 string stringError; 116 outCmd->cmdFlag = CMD_UNITY_RUNMODE; 117 outCmd->parameters = input + CMDSTR_TARGET_MODE.size() + 1; // with ' ' 118 if (!strncmp(outCmd->parameters.c_str(), "port", 4) // 4 port len 119 && !strcmp(outCmd->parameters.c_str(), CMDSTR_TMODE_USB.c_str())) { 120 stringError = "Error tmode command"; 121 outCmd->bJumpDo = true; 122 } else if (!strncmp(outCmd->parameters.c_str(), "port ", 5)) { // 5 port len 123 int port = atoi(input + 4); // 4 port len 124 if (port > MAX_IP_PORT || port <= 0) { 125 stringError = "Incorrect port range"; 126 outCmd->bJumpDo = true; 127 } 128 } 129 return stringError; 130 } 131 TargetReboot(const char * input,FormatCommand * outCmd)132 string TargetReboot(const char *input, FormatCommand *outCmd) 133 { 134 string stringError; 135 outCmd->cmdFlag = CMD_UNITY_REBOOT; 136 if (strcmp(input, CMDSTR_TARGET_REBOOT.c_str())) { 137 outCmd->parameters = input + 12; // 12 length of target boot 138 if (outCmd->parameters != "-bootloader" 139 && outCmd->parameters != "-recovery" && outCmd->parameters != "-flashd") { 140 stringError = "Error reboot paramenter"; 141 outCmd->bJumpDo = true; 142 } else { 143 outCmd->parameters.erase(outCmd->parameters.begin()); 144 } 145 } 146 return stringError; 147 } 148 149 // command input 150 // client side:Enter string data formatting conversion to module see internal processing command String2FormatCommand(const char * inputRaw,int sizeInputRaw,FormatCommand * outCmd)151 string String2FormatCommand(const char *inputRaw, int sizeInputRaw, FormatCommand *outCmd) 152 { 153 string stringError; 154 string input = string(inputRaw, sizeInputRaw); 155 if (!strcmp(input.c_str(), CMDSTR_SOFTWARE_HELP.c_str())) { 156 outCmd->cmdFlag = CMD_KERNEL_HELP; 157 stringError = Usage(); 158 outCmd->bJumpDo = true; 159 } else if (!strcmp(input.c_str(), CMDSTR_SOFTWARE_VERSION.c_str())) { 160 outCmd->cmdFlag = CMD_KERNEL_HELP; 161 stringError = Base::GetVersion(); 162 outCmd->bJumpDo = true; 163 } else if (!strcmp(input.c_str(), CMDSTR_TARGET_DISCOVER.c_str())) { 164 outCmd->cmdFlag = CMD_KERNEL_TARGET_DISCOVER; 165 } else if (!strncmp(input.c_str(), CMDSTR_LIST_TARGETS.c_str(), CMDSTR_LIST_TARGETS.size())) { 166 outCmd->cmdFlag = CMD_KERNEL_TARGET_LIST; 167 if (strstr(input.c_str(), " -v")) { 168 outCmd->parameters = "v"; 169 } 170 } else if (!strcmp(input.c_str(), CMDSTR_CONNECT_ANY.c_str())) { 171 outCmd->cmdFlag = CMD_KERNEL_TARGET_ANY; 172 } else if (!strncmp(input.c_str(), CMDSTR_CONNECT_TARGET.c_str(), CMDSTR_CONNECT_TARGET.size())) { 173 outCmd->parameters = input.c_str() + CMDSTR_CONNECT_TARGET.size() + 1; // with ' ' 174 stringError = TargetConnect(outCmd); 175 } else if (!strncmp(input.c_str(), (CMDSTR_SHELL + " ").c_str(), CMDSTR_SHELL.size() + 1)) { 176 outCmd->cmdFlag = CMD_UNITY_EXECUTE; 177 outCmd->parameters = input.c_str() + CMDSTR_SHELL.size() + 1; 178 } else if (!strcmp(input.c_str(), CMDSTR_SHELL.c_str())) { 179 outCmd->cmdFlag = CMD_SHELL_INIT; 180 } else if (!strncmp(input.c_str(), CMDSTR_FILE_SEND.c_str(), CMDSTR_FILE_SEND.size()) 181 || !strncmp(input.c_str(), CMDSTR_FILE_RECV.c_str(), CMDSTR_FILE_RECV.size())) { 182 outCmd->cmdFlag = CMD_FILE_INIT; 183 outCmd->parameters = input.c_str() + 5; // 5: CMDSTR_FORWARD_FPORT CMDSTR_FORWARD_RPORT size 184 } else if (!strncmp(input.c_str(), string(CMDSTR_FORWARD_FPORT + " ").c_str(), CMDSTR_FORWARD_FPORT.size() + 1) 185 || !strncmp(input.c_str(), string(CMDSTR_FORWARD_RPORT + " ").c_str(), 186 CMDSTR_FORWARD_RPORT.size() + 1)) { 187 stringError = ForwardPort(input.c_str(), outCmd); 188 } else if (!strcmp(input.c_str(), CMDSTR_KILL_SERVER.c_str())) { 189 outCmd->cmdFlag = CMD_KERNEL_SERVER_KILL; 190 } else if (!strcmp(input.c_str(), CMDSTR_KILL_DAEMON.c_str())) { 191 outCmd->cmdFlag = CMD_UNITY_TERMINATE; 192 outCmd->parameters = "0"; 193 } else if (!strncmp(input.c_str(), CMDSTR_APP_INSTALL.c_str(), CMDSTR_APP_INSTALL.size())) { 194 outCmd->cmdFlag = CMD_APP_INIT; 195 outCmd->parameters = input; 196 } else if (!strncmp(input.c_str(), CMDSTR_APP_UNINSTALL.c_str(), CMDSTR_APP_UNINSTALL.size())) { 197 outCmd->cmdFlag = CMD_APP_UNINSTALL; 198 outCmd->parameters = input; 199 if (outCmd->parameters.size() > 512 || outCmd->parameters.size() < 4) { // 512 4 max and min name length 200 stringError = "Package's path incorrect"; 201 outCmd->bJumpDo = true; 202 } 203 } else if (!strcmp(input.c_str(), CMDSTR_TARGET_MOUNT.c_str())) { 204 outCmd->cmdFlag = CMD_UNITY_REMOUNT; 205 } else if (!strcmp(input.c_str(), CMDSTR_LIST_JDWP.c_str())) { 206 outCmd->cmdFlag = CMD_UNITY_JPID; 207 } else if (!strncmp(input.c_str(), CMDSTR_TARGET_REBOOT.c_str(), CMDSTR_TARGET_REBOOT.size())) { 208 TargetReboot(input.c_str(), outCmd); 209 } else if (!strncmp(input.c_str(), CMDSTR_TARGET_MODE.c_str(), CMDSTR_TARGET_MODE.size())) { 210 RunMode(input.c_str(), outCmd); 211 } else if (!strcmp(input.c_str(), CMDSTR_CONNECT_ANY.c_str())) { 212 outCmd->cmdFlag = CMD_KERNEL_TARGET_ANY; 213 } else if (!strncmp(input.c_str(), CMDSTR_HILOG.c_str(), CMDSTR_HILOG.size())) { 214 outCmd->cmdFlag = CMD_UNITY_HILOG; 215 if (strstr(input.c_str(), " -v")) { 216 outCmd->parameters = "v"; 217 } 218 } else if (!strncmp(input.c_str(), CMDSTR_STARTUP_MODE.c_str(), CMDSTR_STARTUP_MODE.size())) { 219 outCmd->cmdFlag = CMD_UNITY_ROOTRUN; 220 if (strstr(input.c_str(), " -r")) { 221 outCmd->parameters = "r"; 222 } 223 } else if (!strncmp(input.c_str(), CMDSTR_APP_SIDELOAD.c_str(), CMDSTR_APP_SIDELOAD.size())) { 224 if (strlen(input.c_str()) == CMDSTR_APP_SIDELOAD.size()) { 225 stringError = "Incorrect command, please with local path"; 226 outCmd->bJumpDo = true; 227 } 228 outCmd->cmdFlag = CMD_APP_SIDELOAD; 229 outCmd->parameters = input; 230 } else if (!strncmp(input.c_str(), CMDSTR_BUGREPORT.c_str(), CMDSTR_BUGREPORT.size())) { 231 outCmd->cmdFlag = CMD_UNITY_BUGREPORT_INIT; 232 outCmd->parameters = input; 233 if (outCmd->parameters.size() == CMDSTR_BUGREPORT.size()) { 234 outCmd->parameters += " "; 235 } 236 } else if (input == CMDSTR_INNER_ENABLE_KEEPALIVE) { // Inner command, protocol uses only 237 outCmd->cmdFlag = CMD_KERNEL_ENABLE_KEEPALIVE; 238 } else if (HostUpdater::CheckMatchUpdate(input, stringError, outCmd->cmdFlag, outCmd->bJumpDo)) { 239 outCmd->parameters = input; 240 } else { 241 stringError = "Unknown command..."; 242 outCmd->bJumpDo = true; 243 } 244 // nl 245 if (stringError.size()) { 246 stringError += "\n"; 247 } 248 return stringError; 249 }; 250 } 251 } // namespace Hdc 252