blob: de5b22c5eb577b6816a002967842d0f0291ffc3a [file] [log] [blame]
Andrei Homescu9afa97b2020-11-30 20:48:22 -08001/*
2 * Copyright (C) 2020 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 LOG_TAG "TrustyAppLoader"
18
19#include <BufferAllocator/BufferAllocator.h>
20#include <android-base/logging.h>
21#include <android-base/unique_fd.h>
22#include <fcntl.h>
23#include <getopt.h>
24#include <stdbool.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <sys/mman.h>
29#include <sys/sendfile.h>
30#include <sys/stat.h>
31#include <sys/types.h>
32#include <trusty/tipc.h>
33#include <unistd.h>
34#include <algorithm>
35#include <string>
36
37#include "apploader_ipc.h"
38
39using android::base::unique_fd;
40using std::string;
41
42constexpr const char kTrustyDefaultDeviceName[] = "/dev/trusty-ipc-dev0";
43
44static const char* dev_name = kTrustyDefaultDeviceName;
45
46static const char* _sopts = "hs";
47static const struct option _lopts[] = {
48 {"help", no_argument, 0, 'h'},
49 {"dev", required_argument, 0, 'D'},
50 {0, 0, 0, 0},
51};
52
53static const char* usage =
54 "Usage: %s [options] package-file\n"
55 "\n"
56 "options:\n"
57 " -h, --help prints this message and exit\n"
58 " -D, --dev name Trusty device name\n"
59 "\n";
60
61static void print_usage_and_exit(const char* prog, int code) {
62 fprintf(stderr, usage, prog);
63 exit(code);
64}
65
66static void parse_options(int argc, char** argv) {
67 int c;
68 int oidx = 0;
69
70 while (1) {
71 c = getopt_long(argc, argv, _sopts, _lopts, &oidx);
72 if (c == -1) {
73 break; /* done */
74 }
75
76 switch (c) {
77 case 'h':
78 print_usage_and_exit(argv[0], EXIT_SUCCESS);
79 break;
80
81 case 'D':
82 dev_name = strdup(optarg);
83 break;
84
85 default:
86 print_usage_and_exit(argv[0], EXIT_FAILURE);
87 }
88 }
89}
90
91static unique_fd read_file(const char* file_name, off64_t* out_file_size) {
92 int rc;
93 long page_size = sysconf(_SC_PAGESIZE);
94 off64_t file_size, file_page_offset, file_page_size;
95 struct stat64 st;
96
97 unique_fd file_fd(TEMP_FAILURE_RETRY(open(file_name, O_RDONLY)));
98 if (!file_fd.ok()) {
99 fprintf(stderr, "Error opening file '%s': %s\n", file_name, strerror(errno));
100 return {};
101 }
102
103 rc = fstat64(file_fd, &st);
104 if (rc < 0) {
105 fprintf(stderr, "Error calling stat on file '%s': %s\n", file_name, strerror(errno));
106 return {};
107 }
108
109 if (st.st_size < 0) {
110 fprintf(stderr, "Invalid size for file '%s': %ld\n", file_name, st.st_size);
111 return {};
112 }
113
114 file_size = st.st_size;
115
116 /* The dmabuf size needs to be a multiple of the page size */
117 file_page_offset = file_size & (page_size - 1);
118 if (file_page_offset) {
119 file_page_offset = page_size - file_page_offset;
120 }
121 if (__builtin_add_overflow(file_size, file_page_offset, &file_page_size)) {
122 fprintf(stderr, "Failed to page-align file size\n");
123 return {};
124 }
125
126 BufferAllocator alloc;
127 unique_fd dmabuf_fd(alloc.Alloc(kDmabufSystemHeapName, file_page_size));
128 if (!dmabuf_fd.ok()) {
129 fprintf(stderr, "Error creating dmabuf: %d\n", dmabuf_fd.get());
130 return dmabuf_fd;
131 }
132
133 void* shm = mmap(0, file_page_size, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd, 0);
134 if (shm == MAP_FAILED) {
135 return {};
136 }
137
138 off64_t file_offset = 0;
139 while (file_offset < file_size) {
140 ssize_t num_read = TEMP_FAILURE_RETRY(
141 pread(file_fd, (char*)shm + file_offset, file_size - file_offset, file_offset));
142
143 if (num_read < 0) {
144 fprintf(stderr, "Error reading package file '%s': %s\n", file_name, strerror(errno));
145 break;
146 }
147
148 if (num_read == 0) {
149 fprintf(stderr, "Unexpected end of file '%s'\n", file_name);
150 break;
151 }
152
153 file_offset += (off64_t)num_read;
154 }
155
156 munmap(shm, file_page_size);
157
158 if (file_offset < file_size) {
159 return {};
160 }
161
162 assert(file_offset == file_size);
163 if (out_file_size) {
164 *out_file_size = file_size;
165 }
166
167 return dmabuf_fd;
168}
169
170static ssize_t send_load_message(int tipc_fd, int package_fd, off64_t package_size) {
171 struct apploader_header hdr = {
172 .cmd = APPLOADER_CMD_LOAD_APPLICATION,
173 };
174 struct apploader_load_app_req req = {
175 .package_size = static_cast<uint64_t>(package_size),
176 };
177 struct iovec tx[2] = {{&hdr, sizeof(hdr)}, {&req, sizeof(req)}};
178 struct trusty_shm shm = {
179 .fd = package_fd,
180 .transfer = TRUSTY_SHARE,
181 };
182 return tipc_send(tipc_fd, tx, 2, &shm, 1);
183}
184
185static ssize_t read_response(int tipc_fd) {
186 struct apploader_resp resp;
187 ssize_t rc = read(tipc_fd, &resp, sizeof(resp));
188 if (rc < 0) {
189 fprintf(stderr, "Failed to read response: %zd\n", rc);
190 return rc;
191 }
192
193 if (rc < sizeof(resp)) {
194 fprintf(stderr, "Not enough data in response: %zd\n", rc);
195 return -EIO;
196 }
197
198 if (resp.hdr.cmd != (APPLOADER_CMD_LOAD_APPLICATION | APPLOADER_RESP_BIT)) {
199 fprintf(stderr, "Invalid command in response: %u\n", resp.hdr.cmd);
200 return -EINVAL;
201 }
202
203 switch (resp.error) {
204 case APPLOADER_NO_ERROR:
205 break;
206 case APPLOADER_ERR_UNKNOWN_CMD:
207 fprintf(stderr, "Error: unknown command\n");
208 break;
209 case APPLOADER_ERR_INVALID_CMD:
210 fprintf(stderr, "Error: invalid command arguments\n");
211 break;
212 case APPLOADER_ERR_NO_MEMORY:
213 fprintf(stderr, "Error: out of Trusty memory\n");
214 break;
215 case APPLOADER_ERR_VERIFICATION_FAILED:
216 fprintf(stderr, "Error: failed to verify the package\n");
217 break;
218 case APPLOADER_ERR_LOADING_FAILED:
219 fprintf(stderr, "Error: failed to load the package\n");
220 break;
221 case APPLOADER_ERR_ALREADY_EXISTS:
222 fprintf(stderr, "Error: application already exists\n");
223 break;
224 case APPLOADER_ERR_INTERNAL:
225 fprintf(stderr, "Error: internal apploader error\n");
226 break;
227 default:
228 fprintf(stderr, "Unrecognized error: %u\n", resp.error);
229 break;
230 }
231
232 return static_cast<ssize_t>(resp.error);
233}
234
235static ssize_t send_app_package(const char* package_file_name) {
236 ssize_t rc = 0;
237 int tipc_fd = -1;
238 off64_t package_size;
239
240 unique_fd package_fd = read_file(package_file_name, &package_size);
241 if (!package_fd.ok()) {
242 rc = -1;
243 goto err_read_file;
244 }
245
246 tipc_fd = tipc_connect(dev_name, APPLOADER_PORT);
247 if (tipc_fd < 0) {
248 fprintf(stderr, "Failed to connect to Trusty app loader: %s\n", strerror(-tipc_fd));
249 rc = tipc_fd;
250 goto err_tipc_connect;
251 }
252
253 rc = send_load_message(tipc_fd, package_fd, package_size);
254 if (rc < 0) {
255 fprintf(stderr, "Failed to send package: %zd\n", rc);
256 goto err_send;
257 }
258
259 rc = read_response(tipc_fd);
260
261err_send:
262 tipc_close(tipc_fd);
263err_tipc_connect:
264err_read_file:
265 return rc;
266}
267
268int main(int argc, char** argv) {
269 parse_options(argc, argv);
270 if (optind + 1 != argc) {
271 print_usage_and_exit(argv[0], EXIT_FAILURE);
272 }
273
274 int rc = send_app_package(argv[optind]);
275 return rc == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
276}