1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #include <trusty/avb.h>
26 #include <trusty/keymaster.h>
27 #include <trusty/rpmb.h>
28 #include <trusty/trusty_dev.h>
29 #include <trusty/trusty_ipc.h>
30 #include <trusty/util.h>
31 
32 #define LOCAL_LOG 0
33 
34 typedef uintptr_t vaddr_t;
35 
36 static struct trusty_ipc_dev* _ipc_dev;
37 static struct trusty_dev _tdev; /* There should only be one trusty device */
38 static void* rpmb_ctx;
39 
trusty_ipc_shutdown(void)40 void trusty_ipc_shutdown(void) {
41     (void)rpmb_storage_proxy_shutdown(_ipc_dev);
42     (void)rpmb_storage_put_ctx(rpmb_ctx);
43 
44     (void)avb_tipc_shutdown(_ipc_dev);
45     (void)km_tipc_shutdown(_ipc_dev);
46 
47     /* shutdown Trusty IPC device */
48     (void)trusty_ipc_dev_shutdown(_ipc_dev);
49 
50     /* shutdown Trusty device */
51     (void)trusty_dev_shutdown(&_tdev);
52 }
53 
trusty_ipc_init(void)54 int trusty_ipc_init(void) {
55     int rc;
56     /* init Trusty device */
57     trusty_info("Initializing Trusty device\n");
58     rc = trusty_dev_init(&_tdev, NULL);
59     if (rc != 0) {
60         trusty_error("Initializing Trusty device failed (%d)\n", rc);
61         return rc;
62     }
63 
64     /* create Trusty IPC device */
65     trusty_info("Initializing Trusty IPC device\n");
66     rc = trusty_ipc_dev_create(&_ipc_dev, &_tdev, PAGE_SIZE);
67     if (rc != 0) {
68         trusty_error("Initializing Trusty IPC device failed (%d)\n", rc);
69         return rc;
70     }
71 
72     /* get storage rpmb */
73     rpmb_ctx = rpmb_storage_get_ctx();
74 
75     /* start secure storage proxy service */
76     trusty_info("Initializing RPMB storage proxy service\n");
77     rc = rpmb_storage_proxy_init(_ipc_dev, rpmb_ctx);
78     if (rc != 0) {
79         trusty_error("Initlializing RPMB storage proxy service failed (%d)\n",
80                      rc);
81         return rc;
82     }
83 
84     trusty_info("Initializing Trusty AVB client\n");
85     rc = avb_tipc_init(_ipc_dev);
86     if (rc != 0) {
87         trusty_error("Initlializing Trusty AVB client failed (%d)\n", rc);
88         return rc;
89     }
90 
91     trusty_info("Initializing Trusty Keymaster client\n");
92     rc = km_tipc_init(_ipc_dev);
93     if (rc != 0) {
94         trusty_error("Initlializing Trusty Keymaster client failed (%d)\n", rc);
95         return rc;
96     }
97 
98     return TRUSTY_ERR_NONE;
99 }
100