blob: 0fab7035d20366d444646816be80e9f594b73f43 [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>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033#include <stdio.h>
34#include <stdlib.h>
35#include <stdarg.h>
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080036#include <stdbool.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037#include <string.h>
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080038#include <sys/stat.h>
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080039#include <sys/types.h>
40#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080041
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080042#ifdef USE_MINGW
43#include <fcntl.h>
44#else
45#include <sys/mman.h>
46#endif
47
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080048#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080049
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080050#define OP_DOWNLOAD 1
51#define OP_COMMAND 2
52#define OP_QUERY 3
53#define OP_NOTICE 4
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -070054#define OP_DOWNLOAD_SPARSE 5
55#define OP_WAIT_FOR_DISCONNECT 6
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080056
57typedef struct Action Action;
58
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080059#define CMD_SIZE 64
60
Anatol Pomazau5ae3f932012-02-28 07:21:08 -080061struct Action
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080062{
63 unsigned op;
64 Action *next;
65
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080066 char cmd[CMD_SIZE];
Wink Savilleb98762f2011-04-04 17:54:59 -070067 const char *prod;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080068 void *data;
69 unsigned size;
70
71 const char *msg;
72 int (*func)(Action *a, int status, char *resp);
Daniel Sandlercb6e22b2010-02-25 14:05:33 -050073
74 double start;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075};
76
77static Action *action_list = 0;
78static Action *action_last = 0;
79
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080080
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080081
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080082
Colin Cross80f2d032012-05-24 18:24:53 -070083int fb_getvar(struct usb_handle *usb, char *response, const char *fmt, ...)
84{
85 char cmd[CMD_SIZE] = "getvar:";
86 int getvar_len = strlen(cmd);
87 va_list args;
88
89 response[FB_RESPONSE_SZ] = '\0';
90 va_start(args, fmt);
91 vsnprintf(cmd + getvar_len, sizeof(cmd) - getvar_len, fmt, args);
92 va_end(args);
93 cmd[CMD_SIZE - 1] = '\0';
94 return fb_command_response(usb, cmd, response);
95}
96
Anatol Pomazauc8ba5362011-12-15 17:50:18 -080097
Ken Sumrall5ee5d382012-09-29 14:46:25 -070098/* Return true if this partition is supported by the fastboot format command.
99 * It is also used to determine if we should first erase a partition before
100 * flashing it with an ext4 filesystem. See needs_erase()
101 *
102 * Not all devices report the filesystem type, so don't report any errors,
103 * just return false.
104 */
105int fb_format_supported(usb_handle *usb, const char *partition)
106{
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700107 char response[FB_RESPONSE_SZ + 1] = {0,};
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700108 int status;
109 unsigned int i;
110
111 status = fb_getvar(usb, response, "partition-type:%s", partition);
112 if (status) {
113 return 0;
114 }
115
Dmitry Grinberge6f3e9b2014-03-11 18:28:15 -0700116 return !!fs_get_generator(response);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700117}
118
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800119static int cb_default(Action *a, int status, char *resp)
120{
121 if (status) {
122 fprintf(stderr,"FAILED (%s)\n", resp);
123 } else {
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500124 double split = now();
125 fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
126 a->start = split;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800127 }
128 return status;
129}
130
131static Action *queue_action(unsigned op, const char *fmt, ...)
132{
133 Action *a;
134 va_list ap;
Bruce Beare50b39952010-07-15 08:52:01 -0700135 size_t cmdsize;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136
137 a = calloc(1, sizeof(Action));
138 if (a == 0) die("out of memory");
139
140 va_start(ap, fmt);
Bruce Beare50b39952010-07-15 08:52:01 -0700141 cmdsize = vsnprintf(a->cmd, sizeof(a->cmd), fmt, ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 va_end(ap);
143
Bruce Beare50b39952010-07-15 08:52:01 -0700144 if (cmdsize >= sizeof(a->cmd)) {
145 free(a);
146 die("Command length (%d) exceeds maximum size (%d)", cmdsize, sizeof(a->cmd));
147 }
148
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800149 if (action_last) {
150 action_last->next = a;
151 } else {
152 action_list = a;
153 }
154 action_last = a;
155 a->op = op;
156 a->func = cb_default;
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500157
158 a->start = -1;
159
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800160 return a;
161}
162
163void fb_queue_erase(const char *ptn)
164{
165 Action *a;
166 a = queue_action(OP_COMMAND, "erase:%s", ptn);
167 a->msg = mkmsg("erasing '%s'", ptn);
168}
169
170void fb_queue_flash(const char *ptn, void *data, unsigned sz)
171{
172 Action *a;
173
174 a = queue_action(OP_DOWNLOAD, "");
175 a->data = data;
176 a->size = sz;
177 a->msg = mkmsg("sending '%s' (%d KB)", ptn, sz / 1024);
178
179 a = queue_action(OP_COMMAND, "flash:%s", ptn);
180 a->msg = mkmsg("writing '%s'", ptn);
181}
182
Colin Crossf8387882012-05-24 17:18:41 -0700183void fb_queue_flash_sparse(const char *ptn, struct sparse_file *s, unsigned sz)
184{
185 Action *a;
186
187 a = queue_action(OP_DOWNLOAD_SPARSE, "");
188 a->data = s;
189 a->size = 0;
190 a->msg = mkmsg("sending sparse '%s' (%d KB)", ptn, sz / 1024);
191
192 a = queue_action(OP_COMMAND, "flash:%s", ptn);
193 a->msg = mkmsg("writing '%s'", ptn);
194}
195
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800196static int match(char *str, const char **value, unsigned count)
197{
198 const char *val;
199 unsigned n;
200 int len;
201
202 for (n = 0; n < count; n++) {
203 const char *val = value[n];
204 int len = strlen(val);
205 int match;
206
207 if ((len > 1) && (val[len-1] == '*')) {
208 len--;
209 match = !strncmp(val, str, len);
210 } else {
211 match = !strcmp(val, str);
212 }
213
214 if (match) return 1;
215 }
216
217 return 0;
218}
219
220
221
222static int cb_check(Action *a, int status, char *resp, int invert)
223{
224 const char **value = a->data;
225 unsigned count = a->size;
226 unsigned n;
227 int yes;
228
229 if (status) {
230 fprintf(stderr,"FAILED (%s)\n", resp);
231 return status;
232 }
233
Wink Savilleb98762f2011-04-04 17:54:59 -0700234 if (a->prod) {
235 if (strcmp(a->prod, cur_product) != 0) {
236 double split = now();
237 fprintf(stderr,"IGNORE, product is %s required only for %s [%7.3fs]\n",
238 cur_product, a->prod, (split - a->start));
239 a->start = split;
240 return 0;
241 }
242 }
243
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800244 yes = match(resp, value, count);
245 if (invert) yes = !yes;
246
247 if (yes) {
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500248 double split = now();
249 fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
250 a->start = split;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800251 return 0;
252 }
253
254 fprintf(stderr,"FAILED\n\n");
255 fprintf(stderr,"Device %s is '%s'.\n", a->cmd + 7, resp);
256 fprintf(stderr,"Update %s '%s'",
257 invert ? "rejects" : "requires", value[0]);
258 for (n = 1; n < count; n++) {
259 fprintf(stderr," or '%s'", value[n]);
260 }
261 fprintf(stderr,".\n\n");
262 return -1;
263}
264
265static int cb_require(Action *a, int status, char *resp)
266{
267 return cb_check(a, status, resp, 0);
268}
269
270static int cb_reject(Action *a, int status, char *resp)
271{
272 return cb_check(a, status, resp, 1);
273}
274
Wink Savilleb98762f2011-04-04 17:54:59 -0700275void fb_queue_require(const char *prod, const char *var,
276 int invert, unsigned nvalues, const char **value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800277{
278 Action *a;
279 a = queue_action(OP_QUERY, "getvar:%s", var);
Wink Savilleb98762f2011-04-04 17:54:59 -0700280 a->prod = prod;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800281 a->data = value;
282 a->size = nvalues;
283 a->msg = mkmsg("checking %s", var);
284 a->func = invert ? cb_reject : cb_require;
285 if (a->data == 0) die("out of memory");
286}
287
288static int cb_display(Action *a, int status, char *resp)
289{
290 if (status) {
291 fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
292 return status;
293 }
294 fprintf(stderr, "%s: %s\n", (char*) a->data, resp);
295 return 0;
296}
297
298void fb_queue_display(const char *var, const char *prettyname)
299{
300 Action *a;
301 a = queue_action(OP_QUERY, "getvar:%s", var);
302 a->data = strdup(prettyname);
303 if (a->data == 0) die("out of memory");
304 a->func = cb_display;
305}
306
Wink Savilleb98762f2011-04-04 17:54:59 -0700307static int cb_save(Action *a, int status, char *resp)
308{
309 if (status) {
310 fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
311 return status;
312 }
313 strncpy(a->data, resp, a->size);
314 return 0;
315}
316
317void fb_queue_query_save(const char *var, char *dest, unsigned dest_size)
318{
319 Action *a;
320 a = queue_action(OP_QUERY, "getvar:%s", var);
321 a->data = (void *)dest;
322 a->size = dest_size;
323 a->func = cb_save;
324}
325
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800326static int cb_do_nothing(Action *a, int status, char *resp)
327{
328 fprintf(stderr,"\n");
329 return 0;
330}
331
332void fb_queue_reboot(void)
333{
334 Action *a = queue_action(OP_COMMAND, "reboot");
335 a->func = cb_do_nothing;
336 a->msg = "rebooting";
337}
338
339void fb_queue_command(const char *cmd, const char *msg)
340{
341 Action *a = queue_action(OP_COMMAND, cmd);
342 a->msg = msg;
343}
344
345void fb_queue_download(const char *name, void *data, unsigned size)
346{
347 Action *a = queue_action(OP_DOWNLOAD, "");
348 a->data = data;
349 a->size = size;
350 a->msg = mkmsg("downloading '%s'", name);
351}
352
353void fb_queue_notice(const char *notice)
354{
355 Action *a = queue_action(OP_NOTICE, "");
356 a->data = (void*) notice;
357}
358
Mark Wachsler157b0012013-10-02 09:35:38 -0400359void fb_queue_wait_for_disconnect(void)
360{
361 queue_action(OP_WAIT_FOR_DISCONNECT, "");
362}
363
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700364int fb_execute_queue(usb_handle *usb)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800365{
366 Action *a;
367 char resp[FB_RESPONSE_SZ+1];
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700368 int status = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800369
370 a = action_list;
Scott Anderson13081c62012-04-06 12:39:30 -0700371 if (!a)
372 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800373 resp[FB_RESPONSE_SZ] = 0;
374
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500375 double start = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800376 for (a = action_list; a; a = a->next) {
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500377 a->start = now();
378 if (start < 0) start = a->start;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800379 if (a->msg) {
Brian Swetland63e52052010-06-28 11:14:26 -0700380 // fprintf(stderr,"%30s... ",a->msg);
381 fprintf(stderr,"%s...\n",a->msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800382 }
383 if (a->op == OP_DOWNLOAD) {
384 status = fb_download_data(usb, a->data, a->size);
385 status = a->func(a, status, status ? fb_get_error() : "");
386 if (status) break;
387 } else if (a->op == OP_COMMAND) {
388 status = fb_command(usb, a->cmd);
389 status = a->func(a, status, status ? fb_get_error() : "");
390 if (status) break;
391 } else if (a->op == OP_QUERY) {
392 status = fb_command_response(usb, a->cmd, resp);
393 status = a->func(a, status, status ? fb_get_error() : resp);
394 if (status) break;
395 } else if (a->op == OP_NOTICE) {
396 fprintf(stderr,"%s\n",(char*)a->data);
Colin Crossf8387882012-05-24 17:18:41 -0700397 } else if (a->op == OP_DOWNLOAD_SPARSE) {
398 status = fb_download_data_sparse(usb, a->data);
399 status = a->func(a, status, status ? fb_get_error() : "");
400 if (status) break;
Mark Wachsler157b0012013-10-02 09:35:38 -0400401 } else if (a->op == OP_WAIT_FOR_DISCONNECT) {
402 usb_wait_for_disconnect(usb);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800403 } else {
404 die("bogus action");
405 }
406 }
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500407
408 fprintf(stderr,"finished. total time: %.3fs\n", (now() - start));
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700409 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800410}
Scott Anderson13081c62012-04-06 12:39:30 -0700411
412int fb_queue_is_empty(void)
413{
414 return (action_list == NULL);
415}