• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 //! config
16 #![allow(missing_docs)]
17 
18 use std::convert::TryFrom;
19 
20 use log::LevelFilter;
21 
22 pub enum CompressType {
23     None = 0,
24     Lz4,
25     Lz77,
26     Lzma,
27     Brotli,
28 }
29 
30 impl TryFrom<u8> for CompressType {
31     type Error = ();
try_from(cmd: u8) -> Result<Self, ()>32     fn try_from(cmd: u8) -> Result<Self, ()> {
33         match cmd {
34             0 => Ok(Self::None),
35             1 => Ok(Self::Lz4),
36             2 => Ok(Self::Lz77),
37             3 => Ok(Self::Lzma),
38             4 => Ok(Self::Brotli),
39             _ => Err(()),
40         }
41     }
42 }
43 
44 #[allow(unused)]
45 #[derive(Clone, Default)]
46 pub enum ConnectType {
47     Usb(String),
48     #[default]
49     Tcp,
50     Uart,
51     Bt,
52 }
53 
54 pub enum ErrCode {
55     Success = 0,
56     ModuleJdwpFailed = -18000,
57 }
58 
59 pub enum NodeType {
60     Server,
61     Daemon,
62 }
63 
64 #[derive(Debug, Clone)]
65 pub struct TaskMessage {
66     pub channel_id: u32,
67     pub command: HdcCommand,
68     pub payload: Vec<u8>,
69 }
70 
71 #[derive(PartialEq, Debug, Clone, Copy)]
72 #[repr(u32)]
73 pub enum HdcCommand {
74     KernelHelp = 0,
75     KernelHandshake,
76     KernelChannelClose,
77     KernelServerKill,
78     KernelTargetDiscover,
79     KernelTargetList,
80     KernelTargetAny,
81     KernelTargetConnect,
82     KernelTargetDisconnect,
83     KernelEcho,
84     KernelEchoRaw,
85     KernelEnableKeepalive,
86     KernelWakeupSlavetask,
87     KernelCheckServer,
88     KernelCheckDevice,
89     KernelWaitFor,
90 
91     // New in refactor
92     KernelServerStart,
93     ClientVersion,
94     ClientKeyGenerate,
95 
96     // One-pass simple commands
97     UnityCommandHead = 1000, // not use
98     UnityExecute,
99     UnityRemount,
100     UnityReboot,
101     UnityRunmode,
102     UnityHilog,
103     UnityTerminate,
104     UnityRootrun,
105     JdwpList,
106     JdwpTrack,
107     UnityCommandTail, // not use
108     // It will be separated from unity in the near future
109     UnityBugreportInit,
110     UnityBugreportData,
111     // Shell commands types
112     ShellInit = 2000,
113     ShellData,
114     // Forward commands types
115     ForwardInit = 2500,
116     ForwardCheck,
117     ForwardCheckResult,
118     ForwardActiveSlave,
119     ForwardActiveMaster,
120     ForwardData,
121     ForwardFreeContext,
122     ForwardList,
123     ForwardRemove,
124     ForwardSuccess,
125     // File commands
126     FileInit = 3000,
127     FileCheck,
128     FileBegin,
129     FileData,
130     FileFinish,
131     AppSideload,
132     FileMode,
133     DirMode,
134     // App commands
135     AppInit = 3500,
136     AppCheck,
137     AppBegin,
138     AppData,
139     AppFinish,
140     AppUninstall,
141     // Flashd commands
142     FlashdUpdateInit = 4000,
143     FlashdFlashInit,
144     FlashdCheck,
145     FlashdBegin,
146     FlashdData,
147     FlashdFinish,
148     FlashdErase,
149     FlashdFormat,
150     FlashdProgress,
151 
152     UartFinish,
153 }
154 
155 impl TryFrom<u32> for HdcCommand {
156     type Error = ();
try_from(cmd: u32) -> Result<Self, ()>157     fn try_from(cmd: u32) -> Result<Self, ()> {
158         match cmd {
159             0 => Ok(Self::KernelHelp),
160             1 => Ok(Self::KernelHandshake),
161             2 => Ok(Self::KernelChannelClose),
162             3 => Ok(Self::KernelServerKill),
163             4 => Ok(Self::KernelTargetDiscover),
164             5 => Ok(Self::KernelTargetList),
165             6 => Ok(Self::KernelTargetAny),
166             7 => Ok(Self::KernelTargetConnect),
167             8 => Ok(Self::KernelTargetDisconnect),
168             9 => Ok(Self::KernelEcho),
169             10 => Ok(Self::KernelEchoRaw),
170             11 => Ok(Self::KernelEnableKeepalive),
171             12 => Ok(Self::KernelWakeupSlavetask),
172             13 => Ok(Self::KernelCheckServer),
173             14 => Ok(Self::KernelCheckDevice),
174             15 => Ok(Self::KernelWaitFor),
175 
176             1000 => Ok(Self::UnityCommandHead),
177             1001 => Ok(Self::UnityExecute),
178             1002 => Ok(Self::UnityRemount),
179             1003 => Ok(Self::UnityReboot),
180             1004 => Ok(Self::UnityRunmode),
181             1005 => Ok(Self::UnityHilog),
182             1006 => Ok(Self::UnityTerminate),
183             1007 => Ok(Self::UnityRootrun),
184             1008 => Ok(Self::JdwpList),
185             1009 => Ok(Self::JdwpTrack),
186             1010 => Ok(Self::UnityCommandTail),
187             1011 => Ok(Self::UnityBugreportInit),
188             1012 => Ok(Self::UnityBugreportData),
189 
190             2000 => Ok(Self::ShellInit),
191             2001 => Ok(Self::ShellData),
192 
193             2500 => Ok(Self::ForwardInit),
194             2501 => Ok(Self::ForwardCheck),
195             2502 => Ok(Self::ForwardCheckResult),
196             2503 => Ok(Self::ForwardActiveSlave),
197             2504 => Ok(Self::ForwardActiveMaster),
198             2505 => Ok(Self::ForwardData),
199             2506 => Ok(Self::ForwardFreeContext),
200             2507 => Ok(Self::ForwardList),
201             2508 => Ok(Self::ForwardRemove),
202             2509 => Ok(Self::ForwardSuccess),
203 
204             3000 => Ok(Self::FileInit),
205             3001 => Ok(Self::FileCheck),
206             3002 => Ok(Self::FileBegin),
207             3003 => Ok(Self::FileData),
208             3004 => Ok(Self::FileFinish),
209             3005 => Ok(Self::AppSideload),
210             3006 => Ok(Self::FileMode),
211             3007 => Ok(Self::DirMode),
212 
213             3500 => Ok(Self::AppInit),
214             3501 => Ok(Self::AppCheck),
215             3502 => Ok(Self::AppBegin),
216             3503 => Ok(Self::AppData),
217             3504 => Ok(Self::AppFinish),
218             3505 => Ok(Self::AppUninstall),
219 
220             4000 => Ok(Self::FlashdUpdateInit),
221             4001 => Ok(Self::FlashdFlashInit),
222             4002 => Ok(Self::FlashdCheck),
223             4003 => Ok(Self::FlashdBegin),
224             4004 => Ok(Self::FlashdData),
225             4005 => Ok(Self::FlashdFinish),
226             4006 => Ok(Self::FlashdErase),
227             4007 => Ok(Self::FlashdFormat),
228             4008 => Ok(Self::FlashdProgress),
229 
230             _ => Err(()),
231         }
232     }
233 }
234 
235 #[allow(unused)]
236 pub enum AuthType {
237     None,
238     Token,
239     Signature,
240     Publickey,
241     OK,
242     Fail,
243 }
244 
245 pub enum AppModeType {
246     Install = 1,
247     UnInstall,
248 }
249 
250 impl TryFrom<u8> for AppModeType {
251     type Error = ();
try_from(cmd: u8) -> Result<Self, ()>252     fn try_from(cmd: u8) -> Result<Self, ()> {
253         match cmd {
254             1 => Ok(Self::Install),
255             2 => Ok(Self::UnInstall),
256             _ => Err(()),
257         }
258     }
259 }
260 
261 pub const PACKET_FLAG: &[u8] = "HW".as_bytes();
262 pub const VER_PROTOCOL: u16 = 1;
263 pub const ENABLE_IO_CHECK: bool = false;
264 pub const PAYLOAD_VCODE: u8 = 0x09;
265 pub const HDC_BUF_MAX_SIZE: usize = 0x7fffffff;
266 pub const HANDSHAKE_MESSAGE: &str = "OHOS HDC";
267 pub const BANNER_SIZE: usize = 12;
268 pub const KEY_MAX_SIZE: usize = 32;
269 pub const FILE_PACKAGE_HEAD: usize = 64;
270 pub const FILE_PACKAGE_PAYLOAD_SIZE: usize = 49152;
271 pub const MAX_SIZE_IOBUF: usize = 61440;
272 
273 pub const SHELL_PROG: &str = "sh";
274 pub const SHELL_TEMP: &str = "/data/local/tmp/hdc-pty";
275 
276 pub const LOG_FILE_NAME: &str = "hdc_rust.log"; // TODO: change to hdc.log
277 pub const LOG_FILE_SIZE: usize = 1024;
278 
279 pub const DAEMON_PORT: u16 = 60000;
280 pub const SERVER_DEFAULT_PORT: u16 = 9710;
281 pub const MAX_PORT_NUM: u32 = 65535;
282 pub const MAX_PORT_LEN: usize = 5;
283 pub const IPV4_MAPPING_PREFIX: &str = "::ffff:";
284 pub const LOCAL_HOST: &str = "127.0.0.1";
285 
286 pub const UART_NODE: &str = "/dev/ttyS4";
287 pub const UART_DEFAULT_BAUD_RATE: i32 = 1500000;
288 pub const UART_DEFAULT_BITS: i32 = 8;
289 pub const UART_EVENT: u8 = 78;
290 pub const MAX_UART_SIZE_IOBUF: u32 = 4096;
291 
292 pub const USB_FFS_BASE: &str = "/dev/usb-ffs/";
293 pub const USB_PACKET_FLAG: &[u8] = "UB".as_bytes();
294 pub const USB_QUEUE_LEN: usize = 64;
295 
296 pub const TRANSFER_FUNC_NAME: &str = "install";
297 pub const INSTALL_TMP_DIR: &str = "/data/local/tmp/";
298 
299 pub const ENV_HDC_MODE: &str = "persist.hdc.mode";
300 pub const ENV_HOST_PORT: &str = "persist.hdc.port";
301 pub const MODE_USB: &str = "usb";
302 pub const MODE_TCP: &str = "tcp";
303 pub const PREFIX_PORT: &str = "port ";
304 pub const SHELL_PARAM_SET: &str = "param set";
305 pub const SHELL_PARAM_GET: &str = "param get";
306 pub const ENV_ROOT_RUN_MODE: &str = "persist.hdc.root";
307 pub const ENV_STARTUP: &str = "ohos.startup.powerctrl";
308 pub const ENV_DEBUGGABLE: &str = "const.debuggable";
309 pub const ENV_SHELL_CONTROL: &str = "persist.hdc.control.shell";
310 pub const ENV_FILE_CONTROL: &str = "persist.hdc.control.file";
311 pub const ENV_FPORT_CONTROL: &str = "persist.hdc.control.fport";
312 
313 pub const RSA_BIT_NUM: usize = 3072;
314 pub const RSA_PUBKEY_PATH: &str = "/data/misc/hdc";
315 pub const RSA_PUBKEY_NAME: &str = "hdc_keys";
316 pub const RSA_PRIKEY_PATH: &str = ".harmony";
317 pub const RSA_PRIKEY_NAME: &str = "hdckey";
318 // "\f" asicc is 0x0C
319 pub const HDC_HOST_DAEMON_BUF_SEPARATOR: char = '\x0C';
320 // the API WaitParameter can only accept max 96 bytes value
321 pub const HDC_PARAMETER_VALUE_MAX_LEN: usize = 96;
322 pub const HDC_HOSTNAME_MAX_LEN: usize = HDC_PARAMETER_VALUE_MAX_LEN;
323 pub const HDC_WAIT_PARAMETER_FOREVER: i32 = 0;
324 pub const HDC_HANDSHAKE_TOKEN_LEN: usize = 32;
325 
326 pub const LOG_LEVEL_ORDER: [LevelFilter; 7] = [
327     LevelFilter::Off,
328     LevelFilter::Error,
329     LevelFilter::Warn,
330     LevelFilter::Info,
331     LevelFilter::Debug,
332     LevelFilter::Trace,
333     LevelFilter::Trace,
334 ];
335 
336 const HDC_VERSION_NUMBER: u32 = 0x10400000;
337 
get_version() -> String338 pub fn get_version() -> String {
339     let major = (HDC_VERSION_NUMBER >> 28) & 0xff;
340     let minor = (HDC_VERSION_NUMBER >> 20) & 0xff;
341     let version = (HDC_VERSION_NUMBER >> 12) & 0xff;
342     let fix = std::char::from_u32(((HDC_VERSION_NUMBER >> 8) & 0xff) + 0x61).unwrap();
343     format!("Ver: {major}.{minor}.{version}{}", fix)
344 }
345