Modify the SMACK label for SDB shell.
[sdk/target/sdbd.git] / src / sdb.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __SDB_H
18 #define __SDB_H
19
20 #include <limits.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23
24 #include "transport.h"  /* readx(), writex() */
25 #include "fdevent.h"
26 #if !SDB_HOST
27 #include "commandline_sdbd.h"
28 #endif
29 #include <tzplatform_config.h>
30
31 #define MAX_PAYLOAD 4096
32
33 #define A_SYNC 0x434e5953
34 #define A_CNXN 0x4e584e43
35 #define A_OPEN 0x4e45504f
36 #define A_OKAY 0x59414b4f
37 #define A_CLSE 0x45534c43
38 #define A_WRTE 0x45545257
39 #define A_STAT 0x54415453
40 #define A_ENCR 0x40682018 // encryption 메시지
41
42 #ifdef SUPPORT_ENCRYPT
43  #define ENCR_SET_ON_REQ 0 // encryption hello 메시지
44  #define ENCR_SET_ON_OK 1 // encryption ack 메시지
45  #define ENCR_SET_OFF 2 // encryption mode off 메시지
46  #define ENCR_GET 3 // encryption status get 메시지
47  #define ENCR_ON_FAIL 4 // encryption on 실패 메시지
48  #define ENCR_OFF_FAIL 5 // encryption off 실패 메시지
49  #define ENCR_ON 1 // encryption on 상태
50  #define ENCR_OFF 0 // encryption off 상태
51 #endif
52
53 #define A_VERSION 0x02000000        // SDB protocol version
54
55 #define SDB_VERSION_MAJOR 2         // Used for help/version information
56 #define SDB_VERSION_MINOR 2         // Used for help/version information
57 #define SDB_VERSION_PATCH 31        // Used for help/version information
58
59 #define SDB_SERVER_VERSION 0        // Increment this when we want to force users to start a new sdb server
60
61 typedef struct amessage amessage;
62 typedef struct apacket apacket;
63 typedef struct asocket asocket;
64 typedef struct alistener alistener;
65 typedef struct aservice aservice;
66 typedef struct atransport atransport;
67 typedef struct adisconnect  adisconnect;
68 typedef struct usb_handle usb_handle;
69
70 struct amessage {
71     unsigned command;       /* command identifier constant      */
72     unsigned arg0;          /* first argument                   */
73     unsigned arg1;          /* second argument                  */
74     unsigned data_length;   /* length of payload (0 is allowed) */
75     unsigned data_check;    /* checksum of data payload         */
76     unsigned magic;         /* command ^ 0xffffffff             */
77 };
78
79 struct apacket
80 {
81     apacket *next;
82
83     unsigned len;
84     unsigned char *ptr;
85
86     amessage msg;
87     unsigned char data[MAX_PAYLOAD];
88 };
89
90 /* An asocket represents one half of a connection between a local and
91 ** remote entity.  A local asocket is bound to a file descriptor.  A
92 ** remote asocket is bound to the protocol engine.
93 */
94 struct asocket {
95         /* chain pointers for the local/remote list of
96         ** asockets that this asocket lives in
97         */
98     asocket *next;
99     asocket *prev;
100
101         /* the unique identifier for this asocket
102         */
103     unsigned id;
104
105         /* flag: set when the socket's peer has closed
106         ** but packets are still queued for delivery
107         */
108     int    closing;
109
110         /* the asocket we are connected to
111         */
112
113     asocket *peer;
114
115         /* For local asockets, the fde is used to bind
116         ** us to our fd event system.  For remote asockets
117         ** these fields are not used.
118         */
119     fdevent fde;
120     int fd;
121
122         /* queue of apackets waiting to be written
123         */
124     apacket *pkt_first;
125     apacket *pkt_last;
126
127         /* enqueue is called by our peer when it has data
128         ** for us.  It should return 0 if we can accept more
129         ** data or 1 if not.  If we return 1, we must call
130         ** peer->ready() when we once again are ready to
131         ** receive data.
132         */
133     int (*enqueue)(asocket *s, apacket *pkt);
134
135         /* ready is called by the peer when it is ready for
136         ** us to send data via enqueue again
137         */
138     void (*ready)(asocket *s);
139
140         /* close is called by the peer when it has gone away.
141         ** we are not allowed to make any further calls on the
142         ** peer once our close method is called.
143         */
144     void (*close)(asocket *s);
145
146         /* socket-type-specific extradata */
147     void *extra;
148
149         /* A socket is bound to atransport */
150     atransport *transport;
151 };
152
153
154 /* the adisconnect structure is used to record a callback that
155 ** will be called whenever a transport is disconnected (e.g. by the user)
156 ** this should be used to cleanup objects that depend on the
157 ** transport (e.g. remote sockets, listeners, etc...)
158 */
159 struct  adisconnect
160 {
161     void        (*func)(void*  opaque, atransport*  t);
162     void*         opaque;
163     adisconnect*  next;
164     adisconnect*  prev;
165 };
166
167
168 /* a transport object models the connection to a remote device or emulator
169 ** there is one transport per connected device/emulator. a "local transport"
170 ** connects through TCP (for the emulator), while a "usb transport" through
171 ** USB (for real devices)
172 **
173 ** note that kTransportHost doesn't really correspond to a real transport
174 ** object, it's a special value used to indicate that a client wants to
175 ** connect to a service implemented within the SDB server itself.
176 */
177 typedef enum transport_type {
178         kTransportUsb,
179         kTransportLocal,
180         kTransportAny,
181         kTransportHost,
182 } transport_type;
183
184 struct atransport
185 {
186     atransport *next;
187     atransport *prev;
188
189     int (*read_from_remote)(apacket *p, atransport *t);
190     int (*write_to_remote)(apacket *p, atransport *t);
191     void (*close)(atransport *t);
192     void (*kick)(atransport *t);
193
194     int fd;
195     int transport_socket;
196     fdevent transport_fde;
197     int ref_count;
198     unsigned sync_token;
199     int connection_state;
200     transport_type type;
201
202         /* usb handle or socket fd as needed */
203     usb_handle *usb;
204     int sfd;
205
206         /* used to identify transports for clients */
207     char *serial;
208     char *product;
209     int sdb_port; // Use for emulators (local transport)
210     char *device_name; // for connection explorer
211
212         /* a list of adisconnect callbacks called when the transport is kicked */
213     int          kicked;
214     adisconnect  disconnects;
215
216 #ifdef SUPPORT_ENCRYPT
217         unsigned encryption; // 해당 연결이 암호화 모드인지 확인하는 flag , 0 = no-encryption / 1 = encryption
218         int sessionID; // 암호화 세션 ID, 암호화 map에 대한 key
219 #endif
220 };
221
222
223 /* A listener is an entity which binds to a local port
224 ** and, upon receiving a connection on that port, creates
225 ** an asocket to connect the new local connection to a
226 ** specific remote service.
227 **
228 ** TODO: some listeners read from the new connection to
229 ** determine what exact service to connect to on the far
230 ** side.
231 */
232 struct alistener
233 {
234     alistener *next;
235     alistener *prev;
236
237     fdevent fde;
238     int fd;
239
240     const char *local_name;
241     const char *connect_to;
242     atransport *transport;
243     adisconnect  disconnect;
244 };
245
246 #define UNKNOWN "unknown"
247 #define INFOBUF_MAXLEN 64
248 #define INFO_VERSION "2.2.0"
249 typedef struct platform_info {
250     char platform_info_version[INFOBUF_MAXLEN];
251     char model_name[INFOBUF_MAXLEN]; // Emulator
252     char platform_name[INFOBUF_MAXLEN]; // Tizen
253     char platform_version[INFOBUF_MAXLEN]; // 2.2.1
254     char profile_name[INFOBUF_MAXLEN]; // 2.2.1
255 } pinfo;
256
257 #define ENABLED "enabled"
258 #define DISABLED "disabled"
259 #define CAPBUF_SIZE 4096
260 #define CAPBUF_ITEMSIZE 32
261 #define CAPBUF_L_ITEMSIZE 256
262 #define CAPBUF_LL_ITEMSIZE PATH_MAX
263 #define SDBD_CAP_VERSION_MAJOR 1
264 #define SDBD_CAP_VERSION_MINOR 0
265 typedef struct platform_capabilities
266 {
267     char secure_protocol[CAPBUF_ITEMSIZE];      // enabled or disabled
268     char intershell_support[CAPBUF_ITEMSIZE];   // enabled or disabled
269     char filesync_support[CAPBUF_ITEMSIZE];     // push or pull or pushpull or disabled
270     char rootonoff_support[CAPBUF_ITEMSIZE];    // enabled or disabled
271     char zone_support[CAPBUF_ITEMSIZE];         // enabled or disabled
272     char multiuser_support[CAPBUF_ITEMSIZE];    // enabled or disabled
273     char syncwinsz_support[CAPBUF_ITEMSIZE];    // enabled or disabled
274     char usbproto_support[CAPBUF_ITEMSIZE];     // enabled or disabled
275     char sockproto_support[CAPBUF_ITEMSIZE];    // enabled or disabled
276     char appcmd_support[CAPBUF_ITEMSIZE];       // enabled or disabled
277     char encryption_support[CAPBUF_ITEMSIZE];   // enabled or disabled
278     char appid2pid_support[CAPBUF_ITEMSIZE];    // enabled or disabled
279     char pkgcmd_debugmode[CAPBUF_ITEMSIZE];     // enabled or disabled
280
281     char log_enable[CAPBUF_ITEMSIZE];           // enabled or disabled
282     char log_path[CAPBUF_LL_ITEMSIZE];          // path of sdbd log
283
284     char cpu_arch[CAPBUF_ITEMSIZE];             // cpu architecture (ex. x86)
285     char profile_name[CAPBUF_ITEMSIZE];         // profile name (ex. mobile)
286     char vendor_name[CAPBUF_ITEMSIZE];          // vendor name (ex. Tizen)
287     char sdk_toolpath[CAPBUF_L_ITEMSIZE];       // sdk tool path
288     char can_launch[CAPBUF_L_ITEMSIZE];         // target name
289
290     char platform_version[CAPBUF_ITEMSIZE];     // platform version (ex. 2.3.0)
291     char product_version[CAPBUF_ITEMSIZE];      // product version (ex. 1.0)
292     char sdbd_version[CAPBUF_ITEMSIZE];         // sdbd version
293     char sdbd_plugin_version[CAPBUF_ITEMSIZE];  // sdbd plugin version
294     char sdbd_cap_version[CAPBUF_ITEMSIZE];     // capability version
295 } pcap;
296 extern pcap g_capabilities;
297
298 void print_packet(const char *label, apacket *p);
299
300 asocket *find_local_socket(unsigned id);
301 void install_local_socket(asocket *s);
302 void remove_socket(asocket *s);
303 void close_all_sockets(atransport *t);
304
305 #define  LOCAL_CLIENT_PREFIX  "emulator-"
306
307 asocket *create_local_socket(int fd);
308 asocket *create_local_service_socket(const char *destination);
309
310 asocket *create_remote_socket(unsigned id, atransport *t);
311 void connect_to_remote(asocket *s, const char *destination);
312 void connect_to_smartsocket(asocket *s);
313
314 void fatal(const char *fmt, ...);
315 void fatal_errno(const char *fmt, ...);
316
317 void handle_packet(apacket *p, atransport *t);
318 void send_packet(apacket *p, atransport *t);
319
320 void get_my_path(char *s, size_t maxLen);
321 int launch_server(int server_port);
322 int sdb_main(int is_daemon, int server_port);
323
324
325 /* transports are ref-counted
326 ** get_device_transport does an acquire on your behalf before returning
327 */
328 void init_transport_registration(void);
329 int  list_transports(char *buf, size_t  bufsize);
330 void update_transports(void);
331 void broadcast_transport(apacket *p);
332 int get_connected_count(transport_type type);
333
334 asocket*  create_device_tracker(void);
335
336 /* Obtain a transport from the available transports.
337 ** If state is != CS_ANY, only transports in that state are considered.
338 ** If serial is non-NULL then only the device with that serial will be chosen.
339 ** If no suitable transport is found, error is set.
340 */
341 atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out);
342 void   add_transport_disconnect( atransport*  t, adisconnect*  dis );
343 void   remove_transport_disconnect( atransport*  t, adisconnect*  dis );
344 void   run_transport_disconnects( atransport*  t );
345 void   kick_transport( atransport*  t );
346
347 /* initialize a transport object's func pointers and state */
348 #if SDB_HOST
349 int get_available_local_transport_index();
350 #endif
351 int  init_socket_transport(atransport *t, int s, int port, int local);
352 void init_usb_transport(atransport *t, usb_handle *usb, int state);
353
354 /* for MacOS X cleanup */
355 void close_usb_devices();
356
357 /* cause new transports to be init'd and added to the list */
358 void register_socket_transport(int s, const char *serial, int port, int local, const char *device_name);
359
360 /* these should only be used for the "sdb disconnect" command */
361 void unregister_transport(atransport *t);
362 void unregister_all_tcp_transports();
363
364 void register_usb_transport(usb_handle *h, const char *serial, unsigned writeable);
365
366 /* this should only be used for transports with connection_state == CS_NOPERM */
367 void unregister_usb_transport(usb_handle *usb);
368
369 atransport *find_transport(const char *serial);
370 #if SDB_HOST
371 atransport* find_emulator_transport_by_sdb_port(int sdb_port);
372 #endif
373
374 int service_to_fd(const char *name);
375 #if SDB_HOST
376 asocket *host_service_to_socket(const char*  name, const char *serial);
377 #endif
378
379 #if !SDB_HOST
380 int       init_jdwp(void);
381 asocket*  create_jdwp_service_socket();
382 asocket*  create_jdwp_tracker_service_socket();
383 int       create_jdwp_connection_fd(int  jdwp_pid);
384 #endif
385
386 #if !SDB_HOST
387 typedef enum {
388     BACKUP,
389     RESTORE
390 } BackupOperation;
391 int backup_service(BackupOperation operation, char* args);
392 void framebuffer_service(int fd, void *cookie);
393 void log_service(int fd, void *cookie);
394 void remount_service(int fd, void *cookie);
395 char * get_log_file_path(const char * log_name);
396
397 extern int rootshell_mode; // 0: sdk user, 1: root
398 extern int booting_done; // 0: platform booting is in progess 1: platform booting is done
399
400 // 1 if locked, 0 if unlocked
401 extern int is_pwlocked;
402
403 // This is the users and groups config for the platform
404
405 #define SID_ROOT        0    /* traditional unix root user */
406
407 #define SDK_USER_NAME   tzplatform_getenv(TZ_SDK_USER_NAME)
408 #define SDK_TOOL_PATH   tzplatform_getenv(TZ_SDK_TOOLS)
409 #define STATIC_SDK_USER_ID      5001
410 #define STATIC_SDK_GROUP_ID     100
411 #define STATIC_SDK_HOME_DIR     "/home/owner"
412 extern uid_t g_sdk_user_id;
413 extern gid_t g_sdk_group_id;
414 extern char* g_sdk_home_dir;
415 extern char* g_sdk_home_dir_env;
416 #endif
417
418 int should_drop_privileges(void);
419 int set_sdk_user_privileges();
420 void set_root_privileges();
421 void send_device_status();
422
423 int get_emulator_forward_port(void);
424 int get_emulator_name(char str[], int str_size);
425 int get_device_name(char str[], int str_size);
426 int get_emulator_hostip(char str[], int str_size);
427 int get_emulator_guestip(char str[], int str_size);
428
429 /* packet allocator */
430 apacket *get_apacket(void);
431 void put_apacket(apacket *p);
432
433 int check_header(apacket *p);
434 int check_data(apacket *p);
435
436 #if !TRACE_PACKETS
437 #define print_packet(tag,p) do {} while (0)
438 #endif
439
440 #if SDB_HOST_ON_TARGET
441 /* sdb and sdbd are coexisting on the target, so use 26099 for sdb
442  * to avoid conflicting with sdbd's usage of 26098
443  */
444 #  define DEFAULT_SDB_PORT 26099 /* tizen specific */
445 #else
446 #  define DEFAULT_SDB_PORT 26099 /* tizen specific */
447 #endif
448
449 #  define QEMU_FORWARD_IP "10.0.2.2"
450
451 #define DEFAULT_SDB_LOCAL_TRANSPORT_PORT 26101 /* tizen specific */
452 #define DEFAULT_SENSORS_LOCAL_TRANSPORT_PORT 26103 /* tizen specific */
453
454 #define SDB_CLASS              0xff
455 #define SDB_SUBCLASS           0x20 //0x42 /* tizen specific */
456 #define SDB_PROTOCOL           0x02 //0x01 /* tizen specific */
457
458
459 void local_init(int port);
460 int  local_connect(int  port, const char *device_name);
461 int  local_connect_arbitrary_ports(int console_port, int sdb_port, const char *device_name);
462
463 /* usb host/client interface */
464 #if SDB_HOST
465 void usb_init();
466 void usb_cleanup();
467 int usb_write(usb_handle *h, const void *data, int len);
468 int usb_read(usb_handle *h, void *data, size_t len);
469 int usb_close(usb_handle *h);
470 void usb_kick(usb_handle *h);
471 #else
472
473 extern void (*usb_init)();
474 extern void (*usb_cleanup)();
475 extern int (*usb_write)(usb_handle *h, const void *data, int len);
476 extern int (*usb_read)(usb_handle *h, void *data, size_t len);
477 extern int (*usb_close)(usb_handle *h);
478 extern void (*usb_kick)(usb_handle *h);
479
480 /* functionfs backend */
481 void ffs_usb_init();
482 void ffs_usb_cleanup();
483 int ffs_usb_write(usb_handle *h, const void *data, int len);
484 int ffs_usb_read(usb_handle *h, void *data, size_t len);
485 int ffs_usb_close(usb_handle *h);
486 void ffs_usb_kick(usb_handle *h);
487
488 /* kernel sdb gadget backend */
489 void linux_usb_init();
490 void linux_usb_cleanup();
491 int linux_usb_write(usb_handle *h, const void *data, int len);
492 int linux_usb_read(usb_handle *h, void *data, size_t len);
493 int linux_usb_close(usb_handle *h);
494 void linux_usb_kick(usb_handle *h);
495
496 #endif
497
498 /* used for USB device detection */
499 #if SDB_HOST
500 int is_sdb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol);
501 #endif
502
503 unsigned host_to_le32(unsigned n);
504 int sdb_commandline(int argc, char **argv);
505
506 int connection_state(atransport *t);
507
508 #define CS_ANY       -1
509 #define CS_OFFLINE    0
510 #define CS_BOOTLOADER 1
511 #define CS_DEVICE     2
512 #define CS_HOST       3
513 #define CS_RECOVERY   4
514 #define CS_NOPERM     5 /* Insufficient permissions to communicate with the device */
515 #define CS_SIDELOAD   6
516 #define CS_PWLOCK     10
517
518 extern int HOST;
519 extern int SHELL_EXIT_NOTIFY_FD;
520 #if !SDB_HOST
521 extern SdbdCommandlineArgs sdbd_commandline_args;
522 #endif
523
524 #define CHUNK_SIZE (64*1024)
525 #define SDBD_SHELL_CMD_MAX 4096
526
527 int sendfailmsg(int fd, const char *reason);
528 int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s);
529 int copy_packet(apacket* dest, apacket* src);
530
531 int is_emulator(void);
532 #define DEFAULT_DEVICENAME "unknown"
533
534 #if SDB_HOST /* tizen-specific */
535 #define DEVICEMAP_SEPARATOR ":"
536 #define DEVICENAME_MAX 256
537 #define VMS_PATH OS_PATH_SEPARATOR_STR "vms" OS_PATH_SEPARATOR_STR // should include sysdeps.h above
538
539 void register_device_name(const char *device_type, const char *device_name, int port);
540 int get_devicename_from_shdmem(int port, char *device_name);
541 int read_line(const int fd, char* ptr, const size_t maxlen);
542 #endif
543 #endif
544
545 #define USB_FUNCFS_SDB_PATH "/dev/usbgadget/sdb"
546 #define USB_NODE_FILE "/dev/samsung_sdb"
547 int create_subprocess(const char *cmd, pid_t *pid, char * const argv[], char * const envp[]);
548 void get_env(char *key, char **env);
549