1 /*
2  * Copyright (C) 2022 The Android Open Source Project
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 #define TLOG_TAG "device_tree_user_service"
18 #define LOCAL_TRACE 0
19 
20 #include <lib/shared/device_tree/service/device_tree_service.h>
21 #include <lib/unittest/unittest.h>
22 #include <lib/vmm_obj/vmm_obj.h>
23 #include <lk/err_ptr.h>
24 #include <string.h>
25 #include <trusty_unittest.h>
26 #include <uapi/err.h>
27 
28 #include <binder/RpcServerTrusty.h>
29 #include <com/android/trusty/device_tree/IDeviceTree.h>
30 
31 using com::android::trusty::device_tree::DeviceTree;
32 using com::android::trusty::device_tree::IDeviceTree;
33 
main(void)34 int main(void) {
35     TLOGI("Mapping in device tree blob\n");
36     const void* dtb = NULL;
37     size_t dtb_size = 0;
38     int rc = vmm_obj_map_ro("com.android.trusty.kernel.device_tree.blob", &dtb,
39                             &dtb_size);
40     if (rc != NO_ERROR) {
41         TLOGE("vmm_obj_map failed to map in device tree blob (%d)\n", rc);
42         return EXIT_FAILURE;
43     }
44     if (!dtb || !dtb_size) {
45         TLOGE("Failed to map device tree blob into dt server process\n");
46         return EXIT_FAILURE;
47     }
48     TLOGI("Mapped device tree blob (%zu bytes) into dt server process at %p\n",
49           dtb_size, dtb);
50 
51     TLOGI("Starting service\n");
52 
53     tipc_hset* hset = tipc_hset_create();
54     if (IS_ERR(hset)) {
55         TLOGE("Failed to create handle set (%d)\n", PTR_ERR(hset));
56         return EXIT_FAILURE;
57     }
58 
59     const auto port_acl = android::RpcServerTrusty::PortAcl{
60             .flags = IPC_PORT_ALLOW_TA_CONNECT,
61     };
62 
63     /* message size should be large enough to cover any message sent by tests */
64     constexpr size_t max_msg_size = 256;
65     auto srv = android::RpcServerTrusty::make(
66             hset, IDeviceTree::PORT().c_str(),
67             std::make_shared<const android::RpcServerTrusty::PortAcl>(port_acl),
68             max_msg_size);
69     if (srv == nullptr) {
70         return EXIT_FAILURE;
71     }
72 
73     android::sp<DeviceTree> test_srv = android::sp<DeviceTree>::make(
74             static_cast<unsigned char*>(const_cast<void*>(dtb)), dtb_size);
75     if (!test_srv) {
76         TLOGE("Failed to create DeviceTree server\n");
77         return EXIT_FAILURE;
78     }
79     srv->setRootObject(test_srv);
80 
81     return tipc_run_event_loop(hset);
82 }
83