1 /*
2 * Copyright (c) 2022, Google Inc. All rights reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files
6 * (the "Software"), to deal in the Software without restriction,
7 * including without limitation the rights to use, copy, modify, merge,
8 * publish, distribute, sublicense, and/or sell copies of the Software,
9 * and to permit persons to whom the Software is furnished to do so,
10 * 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 NONINFRINGEMENT.
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23
24 #include <err.h>
25 #include <kernel/vm.h>
26 #include <lib/dtb_service/dtb_service.h>
27 #include <lib/ktipc/ktipc.h>
28 #include <lib/shared/binder_discover/binder_discover.h>
29 #include <lib/shared/device_tree/service/device_tree_service.h>
30 #include <lib/trusty/ipc.h>
31 #include <lib/vmm_obj_service/vmm_obj_service.h>
32 #include <libfdt.h>
33 #include <lk/trace.h>
34 #include <string.h>
35
36 #define LOCAL_TRACE (0)
37
38 /* UUID: 185b4dbc-8935-4a1e-89ee-df027b89bc7a */
39 const static struct uuid device_tree_service_uuid = {
40 0x185b4dbc,
41 0x8935,
42 0x4a1e,
43 {0x89, 0xee, 0xdf, 0x02, 0x7b, 0x89, 0xbc, 0x7a},
44 };
45
46 const static struct uuid* dtb_service_uuids[] = {
47 &device_tree_service_uuid,
48 };
49
50 const static struct ktipc_port_acl dtb_service_port_acl = {
51 .flags = IPC_PORT_ALLOW_TA_CONNECT,
52 .uuid_num = countof(dtb_service_uuids),
53 .uuids = dtb_service_uuids,
54 .extra_data = NULL,
55 };
56
dtb_service_add_user(const void * dtb,size_t dtb_size,const char * dtb_port,struct ktipc_server * server)57 static int dtb_service_add_user(const void* dtb,
58 size_t dtb_size,
59 const char* dtb_port,
60 struct ktipc_server* server) {
61 int rc;
62 vmm_aspace_t* kas = vmm_get_kernel_aspace();
63 struct vmm_obj_slice slice = VMM_OBJ_SLICE_INITIAL_VALUE(slice);
64
65 uint64_t aligned_size = round_up(dtb_size, PAGE_SIZE);
66 void* dtb_copy;
67 rc = vmm_alloc(kas, "dtb copy", aligned_size, &dtb_copy, PAGE_SIZE_SHIFT, 0,
68 ARCH_MMU_FLAG_PERM_NO_EXECUTE);
69 if (rc != NO_ERROR) {
70 TRACEF("error allocating memory (%d)\n", rc);
71 goto err_alloc;
72 }
73
74 /*
75 * We need to make a copy because calling memref_create_from_aspace
76 * directly on dtb returns ERR_OUT_OF_RANGE because there is no backing
77 * vmm_obj for the kernel image
78 */
79 rc = fdt_move(dtb, dtb_copy, aligned_size);
80 if (rc) {
81 TRACEF("failed (%d) to move fdt\n", rc);
82 goto err_fdt_move;
83 }
84
85 rc = vmm_get_obj(kas, (vaddr_t)dtb_copy, aligned_size, &slice);
86 if (rc < 0) {
87 TRACEF("failed (%d) to get vmm_obj\n", rc);
88 goto err_get_obj;
89 }
90
91 struct vmm_obj_service* srv;
92 rc = vmm_obj_service_create_ro(dtb_port, &dtb_service_port_acl, slice.obj,
93 slice.offset, slice.size, &srv);
94 if (rc < 0) {
95 TRACEF("failed (%d) to create vmm_obj_service\n", rc);
96 goto err_create_service;
97 }
98
99 rc = vmm_obj_service_add(srv, server);
100 if (rc < 0) {
101 TRACEF("error (%d) adding new service\n", rc);
102 goto err_add_service;
103 }
104
105 /* vmm_obj_service_create_ro incremented the reference count of slice.obj */
106 vmm_obj_slice_release(&slice);
107 /* We can free the allocation now that we have the slice */
108 vmm_free_region(kas, (vaddr_t)dtb_copy);
109 return NO_ERROR;
110
111 err_add_service:
112 vmm_obj_service_destroy(&srv);
113 err_create_service:
114 vmm_obj_slice_release(&slice);
115 err_get_obj:
116 err_fdt_move:
117 vmm_free_region(kas, (vaddr_t)dtb_copy);
118 err_alloc:
119 return rc;
120 }
121
dtb_service_add(const void * dtb,size_t dtb_size,const char * dt_port,const char * dtb_port,struct ktipc_server * server)122 int dtb_service_add(const void* dtb,
123 size_t dtb_size,
124 const char* dt_port,
125 const char* dtb_port,
126 struct ktipc_server* server) {
127 if (!dtb) {
128 TRACEF("invalid dtb pointer\n");
129 return ERR_INVALID_ARGS;
130 }
131 if (!dtb_size) {
132 TRACEF("invalid dtb size\n");
133 return ERR_INVALID_ARGS;
134 }
135 if (fdt_check_full(dtb, dtb_size)) {
136 TRACEF("invalid dtb contents");
137 return ERR_INVALID_ARGS;
138 }
139 if (!dt_port) {
140 TRACEF("invalid kernel port name\n");
141 return ERR_INVALID_ARGS;
142 }
143 if (!dtb_port) {
144 TRACEF("invalid user port name\n");
145 return ERR_INVALID_ARGS;
146 }
147 if (!server) {
148 TRACEF("invalid server pointer\n");
149 return ERR_INVALID_ARGS;
150 }
151
152 auto dt = android::sp<com::android::trusty::device_tree::DeviceTree>::make(
153 static_cast<const unsigned char*>(dtb), dtb_size);
154 int err = binder_discover_add_service(dt_port, dt);
155 if (err != android::OK) {
156 TRACEF("error adding service (%d)\n", err);
157 return ERR_GENERIC;
158 }
159
160 int rc = dtb_service_add_user(dtb, dtb_size, dtb_port, server);
161 if (rc < 0) {
162 binder_discover_remove_service(dt_port);
163 return rc;
164 }
165
166 return NO_ERROR;
167 }
168