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