blob: 44796d7d970276a94370c1ede8f8f414b4cd1037 [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
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080041#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043#define OP_DOWNLOAD 1
44#define OP_COMMAND 2
45#define OP_QUERY 3
46#define OP_NOTICE 4
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070047#define OP_DOWNLOAD_SPARSE 5
48#define OP_WAIT_FOR_DISCONNECT 6
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
50typedef struct Action Action;
51
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080052#define CMD_SIZE 64
53
Elliott Hughesfc797672015-04-07 20:12:50 -070054struct Action {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055 unsigned op;
Elliott Hughesfc797672015-04-07 20:12:50 -070056 Action* next;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080057
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080058 char cmd[CMD_SIZE];
Elliott Hughesfc797672015-04-07 20:12:50 -070059 const char* prod;
60 void* data;
61
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
Elliott Hughes2fd45a92015-10-30 11:49:47 -070078bool fb_getvar(usb_handle* usb, const std::string& key, std::string* value) {
79 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));
84 if (fb_command_response(usb, cmd.c_str(), buf)) {
85 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
133void fb_queue_erase(const char *ptn)
134{
135 Action *a;
136 a = queue_action(OP_COMMAND, "erase:%s", ptn);
137 a->msg = mkmsg("erasing '%s'", ptn);
138}
139
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000140void fb_queue_flash(const char *ptn, void *data, unsigned sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800141{
142 Action *a;
143
144 a = queue_action(OP_DOWNLOAD, "");
145 a->data = data;
146 a->size = sz;
147 a->msg = mkmsg("sending '%s' (%d KB)", ptn, sz / 1024);
148
149 a = queue_action(OP_COMMAND, "flash:%s", ptn);
150 a->msg = mkmsg("writing '%s'", ptn);
151}
152
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000153void fb_queue_flash_sparse(const char *ptn, struct sparse_file *s, unsigned sz)
Colin Crossf8387882012-05-24 17:18:41 -0700154{
155 Action *a;
156
157 a = queue_action(OP_DOWNLOAD_SPARSE, "");
158 a->data = s;
159 a->size = 0;
160 a->msg = mkmsg("sending sparse '%s' (%d KB)", ptn, sz / 1024);
161
162 a = queue_action(OP_COMMAND, "flash:%s", ptn);
163 a->msg = mkmsg("writing '%s'", ptn);
164}
165
Elliott Hughesb3748de2015-06-23 20:27:58 -0700166static int match(const char* str, const char** value, unsigned count) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800167 unsigned n;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800168
169 for (n = 0; n < count; n++) {
170 const char *val = value[n];
171 int len = strlen(val);
172 int match;
173
174 if ((len > 1) && (val[len-1] == '*')) {
175 len--;
176 match = !strncmp(val, str, len);
177 } else {
178 match = !strcmp(val, str);
179 }
180
181 if (match) return 1;
182 }
183
184 return 0;
185}
186
187
188
Elliott Hughesb3748de2015-06-23 20:27:58 -0700189static int cb_check(Action* a, int status, const char* resp, int invert)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190{
Elliott Hughesb3748de2015-06-23 20:27:58 -0700191 const char** value = reinterpret_cast<const char**>(a->data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800192 unsigned count = a->size;
193 unsigned n;
194 int yes;
195
196 if (status) {
197 fprintf(stderr,"FAILED (%s)\n", resp);
198 return status;
199 }
200
Wink Savilleb98762f2011-04-04 17:54:59 -0700201 if (a->prod) {
202 if (strcmp(a->prod, cur_product) != 0) {
203 double split = now();
204 fprintf(stderr,"IGNORE, product is %s required only for %s [%7.3fs]\n",
205 cur_product, a->prod, (split - a->start));
206 a->start = split;
207 return 0;
208 }
209 }
210
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800211 yes = match(resp, value, count);
212 if (invert) yes = !yes;
213
214 if (yes) {
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500215 double split = now();
216 fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
217 a->start = split;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800218 return 0;
219 }
220
221 fprintf(stderr,"FAILED\n\n");
222 fprintf(stderr,"Device %s is '%s'.\n", a->cmd + 7, resp);
223 fprintf(stderr,"Update %s '%s'",
224 invert ? "rejects" : "requires", value[0]);
225 for (n = 1; n < count; n++) {
226 fprintf(stderr," or '%s'", value[n]);
227 }
228 fprintf(stderr,".\n\n");
229 return -1;
230}
231
Elliott Hughesb3748de2015-06-23 20:27:58 -0700232static int cb_require(Action*a, int status, const char* resp) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800233 return cb_check(a, status, resp, 0);
234}
235
Elliott Hughesb3748de2015-06-23 20:27:58 -0700236static int cb_reject(Action* a, int status, const char* resp) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800237 return cb_check(a, status, resp, 1);
238}
239
Wink Savilleb98762f2011-04-04 17:54:59 -0700240void fb_queue_require(const char *prod, const char *var,
Elliott Hughesfc797672015-04-07 20:12:50 -0700241 bool invert, size_t nvalues, const char **value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800242{
243 Action *a;
244 a = queue_action(OP_QUERY, "getvar:%s", var);
Wink Savilleb98762f2011-04-04 17:54:59 -0700245 a->prod = prod;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 a->data = value;
247 a->size = nvalues;
248 a->msg = mkmsg("checking %s", var);
249 a->func = invert ? cb_reject : cb_require;
Elliott Hughesb3748de2015-06-23 20:27:58 -0700250 if (a->data == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251}
252
Elliott Hughesb3748de2015-06-23 20:27:58 -0700253static int cb_display(Action* a, int status, const char* resp) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800254 if (status) {
255 fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
256 return status;
257 }
258 fprintf(stderr, "%s: %s\n", (char*) a->data, resp);
259 return 0;
260}
261
262void fb_queue_display(const char *var, const char *prettyname)
263{
264 Action *a;
265 a = queue_action(OP_QUERY, "getvar:%s", var);
266 a->data = strdup(prettyname);
Elliott Hughesb3748de2015-06-23 20:27:58 -0700267 if (a->data == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800268 a->func = cb_display;
269}
270
Elliott Hughesb3748de2015-06-23 20:27:58 -0700271static int cb_save(Action* a, int status, const char* resp) {
Wink Savilleb98762f2011-04-04 17:54:59 -0700272 if (status) {
273 fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
274 return status;
275 }
Elliott Hughesb3748de2015-06-23 20:27:58 -0700276 strncpy(reinterpret_cast<char*>(a->data), resp, a->size);
Wink Savilleb98762f2011-04-04 17:54:59 -0700277 return 0;
278}
279
280void fb_queue_query_save(const char *var, char *dest, unsigned dest_size)
281{
282 Action *a;
283 a = queue_action(OP_QUERY, "getvar:%s", var);
284 a->data = (void *)dest;
285 a->size = dest_size;
286 a->func = cb_save;
287}
288
Elliott Hughesb3748de2015-06-23 20:27:58 -0700289static int cb_do_nothing(Action*, int , const char*) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800290 fprintf(stderr,"\n");
291 return 0;
292}
293
294void fb_queue_reboot(void)
295{
296 Action *a = queue_action(OP_COMMAND, "reboot");
297 a->func = cb_do_nothing;
298 a->msg = "rebooting";
299}
300
301void fb_queue_command(const char *cmd, const char *msg)
302{
303 Action *a = queue_action(OP_COMMAND, cmd);
304 a->msg = msg;
305}
306
307void fb_queue_download(const char *name, void *data, unsigned size)
308{
309 Action *a = queue_action(OP_DOWNLOAD, "");
310 a->data = data;
311 a->size = size;
312 a->msg = mkmsg("downloading '%s'", name);
313}
314
315void fb_queue_notice(const char *notice)
316{
317 Action *a = queue_action(OP_NOTICE, "");
318 a->data = (void*) notice;
319}
320
Mark Wachsler157b0012013-10-02 09:35:38 -0400321void fb_queue_wait_for_disconnect(void)
322{
323 queue_action(OP_WAIT_FOR_DISCONNECT, "");
324}
325
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700326int fb_execute_queue(usb_handle *usb)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800327{
328 Action *a;
329 char resp[FB_RESPONSE_SZ+1];
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700330 int status = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800331
332 a = action_list;
Scott Anderson13081c62012-04-06 12:39:30 -0700333 if (!a)
334 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800335 resp[FB_RESPONSE_SZ] = 0;
336
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500337 double start = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800338 for (a = action_list; a; a = a->next) {
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500339 a->start = now();
340 if (start < 0) start = a->start;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800341 if (a->msg) {
Brian Swetland63e52052010-06-28 11:14:26 -0700342 // fprintf(stderr,"%30s... ",a->msg);
343 fprintf(stderr,"%s...\n",a->msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800344 }
345 if (a->op == OP_DOWNLOAD) {
346 status = fb_download_data(usb, a->data, a->size);
347 status = a->func(a, status, status ? fb_get_error() : "");
348 if (status) break;
349 } else if (a->op == OP_COMMAND) {
350 status = fb_command(usb, a->cmd);
351 status = a->func(a, status, status ? fb_get_error() : "");
352 if (status) break;
353 } else if (a->op == OP_QUERY) {
354 status = fb_command_response(usb, a->cmd, resp);
355 status = a->func(a, status, status ? fb_get_error() : resp);
356 if (status) break;
357 } else if (a->op == OP_NOTICE) {
358 fprintf(stderr,"%s\n",(char*)a->data);
Colin Crossf8387882012-05-24 17:18:41 -0700359 } else if (a->op == OP_DOWNLOAD_SPARSE) {
Elliott Hughesb3748de2015-06-23 20:27:58 -0700360 status = fb_download_data_sparse(usb, reinterpret_cast<sparse_file*>(a->data));
Colin Crossf8387882012-05-24 17:18:41 -0700361 status = a->func(a, status, status ? fb_get_error() : "");
362 if (status) break;
Mark Wachsler157b0012013-10-02 09:35:38 -0400363 } else if (a->op == OP_WAIT_FOR_DISCONNECT) {
364 usb_wait_for_disconnect(usb);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365 } else {
366 die("bogus action");
367 }
368 }
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500369
370 fprintf(stderr,"finished. total time: %.3fs\n", (now() - start));
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700371 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800372}