blob: bda6ad8e1c5fdf5c3dd5ccd75dd80886847e4d26 [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>
Mark Salyzyn5957c1f2014-04-30 14:05:28 -070033#include <stdio.h>
34#include <stdlib.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080035#include <string.h>
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080036#include <sys/stat.h>
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080037#include <sys/types.h>
38#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080039
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080040#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080042#define OP_DOWNLOAD 1
43#define OP_COMMAND 2
44#define OP_QUERY 3
45#define OP_NOTICE 4
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070046#define OP_DOWNLOAD_SPARSE 5
47#define OP_WAIT_FOR_DISCONNECT 6
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;
60
61 // The protocol only supports 32-bit sizes, so you'll have to break
62 // anything larger into chunks.
63 uint32_t size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080064
65 const char *msg;
Elliott Hughesb3748de2015-06-23 20:27:58 -070066 int (*func)(Action* a, int status, const char* resp);
Daniel Sandlercb6e22b2010-02-25 14:05:33 -050067
68 double start;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080069};
70
71static Action *action_list = 0;
72static Action *action_last = 0;
73
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080074
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080075
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080076
Colin Cross80f2d032012-05-24 18:24:53 -070077int fb_getvar(struct usb_handle *usb, char *response, const char *fmt, ...)
78{
79 char cmd[CMD_SIZE] = "getvar:";
80 int getvar_len = strlen(cmd);
81 va_list args;
82
83 response[FB_RESPONSE_SZ] = '\0';
84 va_start(args, fmt);
85 vsnprintf(cmd + getvar_len, sizeof(cmd) - getvar_len, fmt, args);
86 va_end(args);
87 cmd[CMD_SIZE - 1] = '\0';
88 return fb_command_response(usb, cmd, response);
89}
90
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080091
Ken Sumrall5ee5d382012-09-29 14:46:25 -070092/* Return true if this partition is supported by the fastboot format command.
93 * It is also used to determine if we should first erase a partition before
94 * flashing it with an ext4 filesystem. See needs_erase()
95 *
96 * Not all devices report the filesystem type, so don't report any errors,
97 * just return false.
98 */
JP Abgrall7e859742014-05-06 15:14:15 -070099int fb_format_supported(usb_handle *usb, const char *partition, const char *type_override)
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700100{
JP Abgrall7e859742014-05-06 15:14:15 -0700101 char fs_type[FB_RESPONSE_SZ + 1] = {0,};
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700102 int status;
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700103
JP Abgrall7e859742014-05-06 15:14:15 -0700104 if (type_override) {
105 return !!fs_get_generator(type_override);
106 }
107 status = fb_getvar(usb, fs_type, "partition-type:%s", partition);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700108 if (status) {
109 return 0;
110 }
JP Abgrall7e859742014-05-06 15:14:15 -0700111 return !!fs_get_generator(fs_type);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700112}
113
Elliott Hughesb3748de2015-06-23 20:27:58 -0700114static int cb_default(Action* a, int status, const char* resp) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800115 if (status) {
116 fprintf(stderr,"FAILED (%s)\n", resp);
117 } else {
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500118 double split = now();
119 fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
120 a->start = split;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121 }
122 return status;
123}
124
125static Action *queue_action(unsigned op, const char *fmt, ...)
126{
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 va_list ap;
Bruce Beare50b39952010-07-15 08:52:01 -0700128 size_t cmdsize;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129
Elliott Hughesb3748de2015-06-23 20:27:58 -0700130 Action* a = reinterpret_cast<Action*>(calloc(1, sizeof(Action)));
131 if (a == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800132
133 va_start(ap, fmt);
Bruce Beare50b39952010-07-15 08:52:01 -0700134 cmdsize = vsnprintf(a->cmd, sizeof(a->cmd), fmt, ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800135 va_end(ap);
136
Bruce Beare50b39952010-07-15 08:52:01 -0700137 if (cmdsize >= sizeof(a->cmd)) {
138 free(a);
139 die("Command length (%d) exceeds maximum size (%d)", cmdsize, sizeof(a->cmd));
140 }
141
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 if (action_last) {
143 action_last->next = a;
144 } else {
145 action_list = a;
146 }
147 action_last = a;
148 a->op = op;
149 a->func = cb_default;
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500150
151 a->start = -1;
152
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800153 return a;
154}
155
156void fb_queue_erase(const char *ptn)
157{
158 Action *a;
159 a = queue_action(OP_COMMAND, "erase:%s", ptn);
160 a->msg = mkmsg("erasing '%s'", ptn);
161}
162
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000163void fb_queue_flash(const char *ptn, void *data, unsigned sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800164{
165 Action *a;
166
167 a = queue_action(OP_DOWNLOAD, "");
168 a->data = data;
169 a->size = sz;
170 a->msg = mkmsg("sending '%s' (%d KB)", ptn, sz / 1024);
171
172 a = queue_action(OP_COMMAND, "flash:%s", ptn);
173 a->msg = mkmsg("writing '%s'", ptn);
174}
175
Alexander Levitskiy8d7ddb32014-05-07 23:31:59 +0000176void fb_queue_flash_sparse(const char *ptn, struct sparse_file *s, unsigned sz)
Colin Crossf8387882012-05-24 17:18:41 -0700177{
178 Action *a;
179
180 a = queue_action(OP_DOWNLOAD_SPARSE, "");
181 a->data = s;
182 a->size = 0;
183 a->msg = mkmsg("sending sparse '%s' (%d KB)", ptn, sz / 1024);
184
185 a = queue_action(OP_COMMAND, "flash:%s", ptn);
186 a->msg = mkmsg("writing '%s'", ptn);
187}
188
Elliott Hughesb3748de2015-06-23 20:27:58 -0700189static int match(const char* str, const char** value, unsigned count) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800190 unsigned n;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800191
192 for (n = 0; n < count; n++) {
193 const char *val = value[n];
194 int len = strlen(val);
195 int match;
196
197 if ((len > 1) && (val[len-1] == '*')) {
198 len--;
199 match = !strncmp(val, str, len);
200 } else {
201 match = !strcmp(val, str);
202 }
203
204 if (match) return 1;
205 }
206
207 return 0;
208}
209
210
211
Elliott Hughesb3748de2015-06-23 20:27:58 -0700212static int cb_check(Action* a, int status, const char* resp, int invert)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800213{
Elliott Hughesb3748de2015-06-23 20:27:58 -0700214 const char** value = reinterpret_cast<const char**>(a->data);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800215 unsigned count = a->size;
216 unsigned n;
217 int yes;
218
219 if (status) {
220 fprintf(stderr,"FAILED (%s)\n", resp);
221 return status;
222 }
223
Wink Savilleb98762f2011-04-04 17:54:59 -0700224 if (a->prod) {
225 if (strcmp(a->prod, cur_product) != 0) {
226 double split = now();
227 fprintf(stderr,"IGNORE, product is %s required only for %s [%7.3fs]\n",
228 cur_product, a->prod, (split - a->start));
229 a->start = split;
230 return 0;
231 }
232 }
233
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800234 yes = match(resp, value, count);
235 if (invert) yes = !yes;
236
237 if (yes) {
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500238 double split = now();
239 fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
240 a->start = split;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800241 return 0;
242 }
243
244 fprintf(stderr,"FAILED\n\n");
245 fprintf(stderr,"Device %s is '%s'.\n", a->cmd + 7, resp);
246 fprintf(stderr,"Update %s '%s'",
247 invert ? "rejects" : "requires", value[0]);
248 for (n = 1; n < count; n++) {
249 fprintf(stderr," or '%s'", value[n]);
250 }
251 fprintf(stderr,".\n\n");
252 return -1;
253}
254
Elliott Hughesb3748de2015-06-23 20:27:58 -0700255static int cb_require(Action*a, int status, const char* resp) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800256 return cb_check(a, status, resp, 0);
257}
258
Elliott Hughesb3748de2015-06-23 20:27:58 -0700259static int cb_reject(Action* a, int status, const char* resp) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800260 return cb_check(a, status, resp, 1);
261}
262
Wink Savilleb98762f2011-04-04 17:54:59 -0700263void fb_queue_require(const char *prod, const char *var,
Elliott Hughesfc797672015-04-07 20:12:50 -0700264 bool invert, size_t nvalues, const char **value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800265{
266 Action *a;
267 a = queue_action(OP_QUERY, "getvar:%s", var);
Wink Savilleb98762f2011-04-04 17:54:59 -0700268 a->prod = prod;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800269 a->data = value;
270 a->size = nvalues;
271 a->msg = mkmsg("checking %s", var);
272 a->func = invert ? cb_reject : cb_require;
Elliott Hughesb3748de2015-06-23 20:27:58 -0700273 if (a->data == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800274}
275
Elliott Hughesb3748de2015-06-23 20:27:58 -0700276static int cb_display(Action* a, int status, const char* resp) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277 if (status) {
278 fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
279 return status;
280 }
281 fprintf(stderr, "%s: %s\n", (char*) a->data, resp);
282 return 0;
283}
284
285void fb_queue_display(const char *var, const char *prettyname)
286{
287 Action *a;
288 a = queue_action(OP_QUERY, "getvar:%s", var);
289 a->data = strdup(prettyname);
Elliott Hughesb3748de2015-06-23 20:27:58 -0700290 if (a->data == nullptr) die("out of memory");
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800291 a->func = cb_display;
292}
293
Elliott Hughesb3748de2015-06-23 20:27:58 -0700294static int cb_save(Action* a, int status, const char* resp) {
Wink Savilleb98762f2011-04-04 17:54:59 -0700295 if (status) {
296 fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
297 return status;
298 }
Elliott Hughesb3748de2015-06-23 20:27:58 -0700299 strncpy(reinterpret_cast<char*>(a->data), resp, a->size);
Wink Savilleb98762f2011-04-04 17:54:59 -0700300 return 0;
301}
302
303void fb_queue_query_save(const char *var, char *dest, unsigned dest_size)
304{
305 Action *a;
306 a = queue_action(OP_QUERY, "getvar:%s", var);
307 a->data = (void *)dest;
308 a->size = dest_size;
309 a->func = cb_save;
310}
311
Elliott Hughesb3748de2015-06-23 20:27:58 -0700312static int cb_do_nothing(Action*, int , const char*) {
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800313 fprintf(stderr,"\n");
314 return 0;
315}
316
317void fb_queue_reboot(void)
318{
319 Action *a = queue_action(OP_COMMAND, "reboot");
320 a->func = cb_do_nothing;
321 a->msg = "rebooting";
322}
323
324void fb_queue_command(const char *cmd, const char *msg)
325{
326 Action *a = queue_action(OP_COMMAND, cmd);
327 a->msg = msg;
328}
329
330void fb_queue_download(const char *name, void *data, unsigned size)
331{
332 Action *a = queue_action(OP_DOWNLOAD, "");
333 a->data = data;
334 a->size = size;
335 a->msg = mkmsg("downloading '%s'", name);
336}
337
338void fb_queue_notice(const char *notice)
339{
340 Action *a = queue_action(OP_NOTICE, "");
341 a->data = (void*) notice;
342}
343
Mark Wachsler157b0012013-10-02 09:35:38 -0400344void fb_queue_wait_for_disconnect(void)
345{
346 queue_action(OP_WAIT_FOR_DISCONNECT, "");
347}
348
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700349int fb_execute_queue(usb_handle *usb)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800350{
351 Action *a;
352 char resp[FB_RESPONSE_SZ+1];
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700353 int status = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800354
355 a = action_list;
Scott Anderson13081c62012-04-06 12:39:30 -0700356 if (!a)
357 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800358 resp[FB_RESPONSE_SZ] = 0;
359
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500360 double start = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 for (a = action_list; a; a = a->next) {
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500362 a->start = now();
363 if (start < 0) start = a->start;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800364 if (a->msg) {
Brian Swetland63e52052010-06-28 11:14:26 -0700365 // fprintf(stderr,"%30s... ",a->msg);
366 fprintf(stderr,"%s...\n",a->msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367 }
368 if (a->op == OP_DOWNLOAD) {
369 status = fb_download_data(usb, a->data, a->size);
370 status = a->func(a, status, status ? fb_get_error() : "");
371 if (status) break;
372 } else if (a->op == OP_COMMAND) {
373 status = fb_command(usb, a->cmd);
374 status = a->func(a, status, status ? fb_get_error() : "");
375 if (status) break;
376 } else if (a->op == OP_QUERY) {
377 status = fb_command_response(usb, a->cmd, resp);
378 status = a->func(a, status, status ? fb_get_error() : resp);
379 if (status) break;
380 } else if (a->op == OP_NOTICE) {
381 fprintf(stderr,"%s\n",(char*)a->data);
Colin Crossf8387882012-05-24 17:18:41 -0700382 } else if (a->op == OP_DOWNLOAD_SPARSE) {
Elliott Hughesb3748de2015-06-23 20:27:58 -0700383 status = fb_download_data_sparse(usb, reinterpret_cast<sparse_file*>(a->data));
Colin Crossf8387882012-05-24 17:18:41 -0700384 status = a->func(a, status, status ? fb_get_error() : "");
385 if (status) break;
Mark Wachsler157b0012013-10-02 09:35:38 -0400386 } else if (a->op == OP_WAIT_FOR_DISCONNECT) {
387 usb_wait_for_disconnect(usb);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800388 } else {
389 die("bogus action");
390 }
391 }
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500392
393 fprintf(stderr,"finished. total time: %.3fs\n", (now() - start));
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700394 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800395}
Scott Anderson13081c62012-04-06 12:39:30 -0700396
397int fb_queue_is_empty(void)
398{
Elliott Hughesb3748de2015-06-23 20:27:58 -0700399 return (action_list == nullptr);
Scott Anderson13081c62012-04-06 12:39:30 -0700400}