blob: bf887c9e9c6555a4bbcf81852689ef52c022cd10 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080012 * the documentation and/or other materials provided with the
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080013 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080022 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080023 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080029#include "fastboot.h"
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070030#include "fs.h"
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080031
Colin Cross81c632e2013-01-23 19:13:43 -080032#include <errno.h>
Elliott Hughes3ab8b852015-08-25 19:34:13 -070033#include <stdarg.h>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070034#include <stdio.h>
35#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080036#include <string.h>
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080037#include <sys/stat.h>
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080038#include <sys/types.h>
39#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080040
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041#define OP_DOWNLOAD 1
42#define OP_COMMAND 2
43#define OP_QUERY 3
44#define OP_NOTICE 4
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070045#define OP_DOWNLOAD_SPARSE 5
46#define OP_WAIT_FOR_DISCONNECT 6
Chris Fries0ea946c2017-04-12 10:25:57 -050047#define OP_DOWNLOAD_FD 7
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080048
49typedef struct Action Action;
50
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080051#define CMD_SIZE 64
52
Elliott Hughesfc797672015-04-07 20:12:50 -070053struct Action {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080054 unsigned op;
Elliott Hughesfc797672015-04-07 20:12:50 -070055 Action* next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080057 char cmd[CMD_SIZE];
Elliott Hughesfc797672015-04-07 20:12:50 -070058 const char* prod;
59 void* data;
Chris Fries0ea946c2017-04-12 10:25:57 -050060 int fd;
Elliott Hughesfc797672015-04-07 20:12:50 -070061
62 // The protocol only supports 32-bit sizes, so you'll have to break
63 // anything larger into chunks.
64 uint32_t size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080065
66 const char *msg;
Elliott Hughesb3748de2015-06-23 20:27:58 -070067 int (*func)(Action* a, int status, const char* resp);
Daniel Sandlercb6e22b2010-02-25 14:05:33 -050068
69 double start;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070};
71
72static Action *action_list = 0;
73static Action *action_last = 0;
74
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080075
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080076
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080077
David Pursell0b156632015-10-30 11:22:01 -070078bool fb_getvar(Transport* transport, const std::string& key, std::string* value) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -070079 std::string cmd = "getvar:";
80 cmd += key;
Colin Cross80f2d032012-05-24 18:24:53 -070081
Elliott Hughes2fd45a92015-10-30 11:49:47 -070082 char buf[FB_RESPONSE_SZ + 1];
83 memset(buf, 0, sizeof(buf));
David Pursell0b156632015-10-30 11:22:01 -070084 if (fb_command_response(transport, cmd.c_str(), buf)) {
Elliott Hughes2fd45a92015-10-30 11:49:47 -070085 return false;
86 }
87 *value = buf;
88 return true;
Colin Cross80f2d032012-05-24 18:24:53 -070089}
90
Elliott Hughesb3748de2015-06-23 20:27:58 -070091static int cb_default(Action* a, int status, const char* resp) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080092 if (status) {
93 fprintf(stderr,"FAILED (%s)\n", resp);
94 } else {
Daniel Sandlercb6e22b2010-02-25 14:05:33 -050095 double split = now();
96 fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
97 a->start = split;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080098 }
99 return status;
100}
101
102static Action *queue_action(unsigned op, const char *fmt, ...)
103{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800104 va_list ap;
Bruce Beare50b39952010-07-15 08:52:01 -0700105 size_t cmdsize;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800106
Elliott Hughesb3748de2015-06-23 20:27:58 -0700107 Action* a = reinterpret_cast<Action*>(calloc(1, sizeof(Action)));
108 if (a == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800109
110 va_start(ap, fmt);
Bruce Beare50b39952010-07-15 08:52:01 -0700111 cmdsize = vsnprintf(a->cmd, sizeof(a->cmd), fmt, ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800112 va_end(ap);
113
Bruce Beare50b39952010-07-15 08:52:01 -0700114 if (cmdsize >= sizeof(a->cmd)) {
115 free(a);
116 die("Command length (%d) exceeds maximum size (%d)", cmdsize, sizeof(a->cmd));
117 }
118
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119 if (action_last) {
120 action_last->next = a;
121 } else {
122 action_list = a;
123 }
124 action_last = a;
125 a->op = op;
126 a->func = cb_default;
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500127
128 a->start = -1;
129
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800130 return a;
131}
132
Daniel Rosenbergb7bd4ae2015-09-14 21:05:41 -0700133void fb_set_active(const char *slot)
134{
135 Action *a;
136 a = queue_action(OP_COMMAND, "set_active:%s", slot);
137 a->msg = mkmsg("Setting current slot to '%s'", slot);
138}
139
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800140void fb_queue_erase(const char *ptn)
141{
142 Action *a;
143 a = queue_action(OP_COMMAND, "erase:%s", ptn);
144 a->msg = mkmsg("erasing '%s'", ptn);
145}
146
Chris Fries0ea946c2017-04-12 10:25:57 -0500147void fb_queue_flash_fd(const char *ptn, int fd, uint32_t sz)
148{
149 Action *a;
150
151 a = queue_action(OP_DOWNLOAD_FD, "");
152 a->fd = fd;
153 a->size = sz;
154 a->msg = mkmsg("sending '%s' (%d KB)", ptn, sz / 1024);
155
156 a = queue_action(OP_COMMAND, "flash:%s", ptn);
157 a->msg = mkmsg("writing '%s'", ptn);
158}
159
160void fb_queue_flash(const char *ptn, void *data, uint32_t sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800161{
162 Action *a;
163
164 a = queue_action(OP_DOWNLOAD, "");
165 a->data = data;
166 a->size = sz;
167 a->msg = mkmsg("sending '%s' (%d KB)", ptn, sz / 1024);
168
169 a = queue_action(OP_COMMAND, "flash:%s", ptn);
170 a->msg = mkmsg("writing '%s'", ptn);
171}
172
Chris Fries0ea946c2017-04-12 10:25:57 -0500173void fb_queue_flash_sparse(const char* ptn, struct sparse_file* s, uint32_t sz, size_t current,
Josh Gao9da9ac52016-01-19 14:50:18 -0800174 size_t total) {
Colin Crossf8387882012-05-24 17:18:41 -0700175 Action *a;
176
177 a = queue_action(OP_DOWNLOAD_SPARSE, "");
178 a->data = s;
179 a->size = 0;
Josh Gao9da9ac52016-01-19 14:50:18 -0800180 a->msg = mkmsg("sending sparse '%s' %zu/%zu (%d KB)", ptn, current, total, sz / 1024);
Colin Crossf8387882012-05-24 17:18:41 -0700181
182 a = queue_action(OP_COMMAND, "flash:%s", ptn);
Josh Gao9da9ac52016-01-19 14:50:18 -0800183 a->msg = mkmsg("writing '%s' %zu/%zu", ptn, current, total);
Colin Crossf8387882012-05-24 17:18:41 -0700184}
185
Elliott Hughesb3748de2015-06-23 20:27:58 -0700186static int match(const char* str, const char** value, unsigned count) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800187 unsigned n;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800188
189 for (n = 0; n < count; n++) {
190 const char *val = value[n];
191 int len = strlen(val);
192 int match;
193
194 if ((len > 1) && (val[len-1] == '*')) {
195 len--;
196 match = !strncmp(val, str, len);
197 } else {
198 match = !strcmp(val, str);
199 }
200
201 if (match) return 1;
202 }
203
204 return 0;
205}
206
207
208
Elliott Hughesb3748de2015-06-23 20:27:58 -0700209static int cb_check(Action* a, int status, const char* resp, int invert)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800210{
Elliott Hughesb3748de2015-06-23 20:27:58 -0700211 const char** value = reinterpret_cast<const char**>(a->data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800212 unsigned count = a->size;
213 unsigned n;
214 int yes;
215
216 if (status) {
217 fprintf(stderr,"FAILED (%s)\n", resp);
218 return status;
219 }
220
Wink Savilleb98762f2011-04-04 17:54:59 -0700221 if (a->prod) {
222 if (strcmp(a->prod, cur_product) != 0) {
223 double split = now();
224 fprintf(stderr,"IGNORE, product is %s required only for %s [%7.3fs]\n",
225 cur_product, a->prod, (split - a->start));
226 a->start = split;
227 return 0;
228 }
229 }
230
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800231 yes = match(resp, value, count);
232 if (invert) yes = !yes;
233
234 if (yes) {
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500235 double split = now();
236 fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
237 a->start = split;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800238 return 0;
239 }
240
241 fprintf(stderr,"FAILED\n\n");
242 fprintf(stderr,"Device %s is '%s'.\n", a->cmd + 7, resp);
243 fprintf(stderr,"Update %s '%s'",
244 invert ? "rejects" : "requires", value[0]);
245 for (n = 1; n < count; n++) {
246 fprintf(stderr," or '%s'", value[n]);
247 }
248 fprintf(stderr,".\n\n");
249 return -1;
250}
251
Elliott Hughesb3748de2015-06-23 20:27:58 -0700252static int cb_require(Action*a, int status, const char* resp) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 return cb_check(a, status, resp, 0);
254}
255
Elliott Hughesb3748de2015-06-23 20:27:58 -0700256static int cb_reject(Action* a, int status, const char* resp) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800257 return cb_check(a, status, resp, 1);
258}
259
Wink Savilleb98762f2011-04-04 17:54:59 -0700260void fb_queue_require(const char *prod, const char *var,
Elliott Hughesfc797672015-04-07 20:12:50 -0700261 bool invert, size_t nvalues, const char **value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800262{
263 Action *a;
264 a = queue_action(OP_QUERY, "getvar:%s", var);
Wink Savilleb98762f2011-04-04 17:54:59 -0700265 a->prod = prod;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800266 a->data = value;
267 a->size = nvalues;
268 a->msg = mkmsg("checking %s", var);
269 a->func = invert ? cb_reject : cb_require;
Elliott Hughesb3748de2015-06-23 20:27:58 -0700270 if (a->data == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800271}
272
Elliott Hughesb3748de2015-06-23 20:27:58 -0700273static int cb_display(Action* a, int status, const char* resp) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274 if (status) {
275 fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
276 return status;
277 }
278 fprintf(stderr, "%s: %s\n", (char*) a->data, resp);
279 return 0;
280}
281
282void fb_queue_display(const char *var, const char *prettyname)
283{
284 Action *a;
285 a = queue_action(OP_QUERY, "getvar:%s", var);
286 a->data = strdup(prettyname);
Elliott Hughesb3748de2015-06-23 20:27:58 -0700287 if (a->data == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800288 a->func = cb_display;
289}
290
Elliott Hughesb3748de2015-06-23 20:27:58 -0700291static int cb_save(Action* a, int status, const char* resp) {
Wink Savilleb98762f2011-04-04 17:54:59 -0700292 if (status) {
293 fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
294 return status;
295 }
Elliott Hughesb3748de2015-06-23 20:27:58 -0700296 strncpy(reinterpret_cast<char*>(a->data), resp, a->size);
Wink Savilleb98762f2011-04-04 17:54:59 -0700297 return 0;
298}
299
Chris Fries0ea946c2017-04-12 10:25:57 -0500300void fb_queue_query_save(const char *var, char *dest, uint32_t dest_size)
Wink Savilleb98762f2011-04-04 17:54:59 -0700301{
302 Action *a;
303 a = queue_action(OP_QUERY, "getvar:%s", var);
304 a->data = (void *)dest;
305 a->size = dest_size;
306 a->func = cb_save;
307}
308
Elliott Hughesb3748de2015-06-23 20:27:58 -0700309static int cb_do_nothing(Action*, int , const char*) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800310 fprintf(stderr,"\n");
311 return 0;
312}
313
314void fb_queue_reboot(void)
315{
316 Action *a = queue_action(OP_COMMAND, "reboot");
317 a->func = cb_do_nothing;
318 a->msg = "rebooting";
319}
320
321void fb_queue_command(const char *cmd, const char *msg)
322{
323 Action *a = queue_action(OP_COMMAND, cmd);
324 a->msg = msg;
325}
326
Chris Fries0ea946c2017-04-12 10:25:57 -0500327void fb_queue_download(const char *name, void *data, uint32_t size)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328{
329 Action *a = queue_action(OP_DOWNLOAD, "");
330 a->data = data;
331 a->size = size;
332 a->msg = mkmsg("downloading '%s'", name);
333}
334
335void fb_queue_notice(const char *notice)
336{
337 Action *a = queue_action(OP_NOTICE, "");
338 a->data = (void*) notice;
339}
340
Mark Wachsler157b0012013-10-02 09:35:38 -0400341void fb_queue_wait_for_disconnect(void)
342{
343 queue_action(OP_WAIT_FOR_DISCONNECT, "");
344}
345
Chris Fries6a999712017-04-04 09:52:47 -0500346int64_t fb_execute_queue(Transport* transport)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800347{
348 Action *a;
349 char resp[FB_RESPONSE_SZ+1];
Chris Fries6a999712017-04-04 09:52:47 -0500350 int64_t status = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800351
352 a = action_list;
Scott Anderson13081c62012-04-06 12:39:30 -0700353 if (!a)
354 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800355 resp[FB_RESPONSE_SZ] = 0;
356
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500357 double start = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 for (a = action_list; a; a = a->next) {
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500359 a->start = now();
360 if (start < 0) start = a->start;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 if (a->msg) {
Brian Swetland63e52052010-06-28 11:14:26 -0700362 // fprintf(stderr,"%30s... ",a->msg);
363 fprintf(stderr,"%s...\n",a->msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364 }
365 if (a->op == OP_DOWNLOAD) {
David Pursell0b156632015-10-30 11:22:01 -0700366 status = fb_download_data(transport, a->data, a->size);
Elliott Hughes2810d002016-04-25 14:31:18 -0700367 status = a->func(a, status, status ? fb_get_error().c_str() : "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368 if (status) break;
Chris Fries0ea946c2017-04-12 10:25:57 -0500369 } else if (a->op == OP_DOWNLOAD_FD) {
370 status = fb_download_data_fd(transport, a->fd, a->size);
371 status = a->func(a, status, status ? fb_get_error().c_str() : "");
372 if (status) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800373 } else if (a->op == OP_COMMAND) {
David Pursell0b156632015-10-30 11:22:01 -0700374 status = fb_command(transport, a->cmd);
Elliott Hughes2810d002016-04-25 14:31:18 -0700375 status = a->func(a, status, status ? fb_get_error().c_str() : "");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376 if (status) break;
377 } else if (a->op == OP_QUERY) {
David Pursell0b156632015-10-30 11:22:01 -0700378 status = fb_command_response(transport, a->cmd, resp);
Elliott Hughes2810d002016-04-25 14:31:18 -0700379 status = a->func(a, status, status ? fb_get_error().c_str() : resp);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800380 if (status) break;
381 } else if (a->op == OP_NOTICE) {
382 fprintf(stderr,"%s\n",(char*)a->data);
Colin Crossf8387882012-05-24 17:18:41 -0700383 } else if (a->op == OP_DOWNLOAD_SPARSE) {
David Pursell0b156632015-10-30 11:22:01 -0700384 status = fb_download_data_sparse(transport, reinterpret_cast<sparse_file*>(a->data));
Elliott Hughes2810d002016-04-25 14:31:18 -0700385 status = a->func(a, status, status ? fb_get_error().c_str() : "");
Colin Crossf8387882012-05-24 17:18:41 -0700386 if (status) break;
Mark Wachsler157b0012013-10-02 09:35:38 -0400387 } else if (a->op == OP_WAIT_FOR_DISCONNECT) {
David Pursell0b156632015-10-30 11:22:01 -0700388 transport->WaitForDisconnect();
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800389 } else {
390 die("bogus action");
391 }
392 }
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500393
394 fprintf(stderr,"finished. total time: %.3fs\n", (now() - start));
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700395 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800396}