blob: 7d440e0731734c53effd8df7a87fa95ac84082d9 [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;
Sasha Levitskiy782111b2014-05-05 19:43:15 -070069 size_t size;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080070
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 */
JP Abgrall7e859742014-05-06 15:14:15 -0700105int fb_format_supported(usb_handle *usb, const char *partition, const char *type_override)
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700106{
JP Abgrall7e859742014-05-06 15:14:15 -0700107 char fs_type[FB_RESPONSE_SZ + 1] = {0,};
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700108 int status;
109 unsigned int i;
110
JP Abgrall7e859742014-05-06 15:14:15 -0700111 if (type_override) {
112 return !!fs_get_generator(type_override);
113 }
114 status = fb_getvar(usb, fs_type, "partition-type:%s", partition);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700115 if (status) {
116 return 0;
117 }
JP Abgrall7e859742014-05-06 15:14:15 -0700118 return !!fs_get_generator(fs_type);
Ken Sumrall5ee5d382012-09-29 14:46:25 -0700119}
120
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800121static int cb_default(Action *a, int status, char *resp)
122{
123 if (status) {
124 fprintf(stderr,"FAILED (%s)\n", resp);
125 } else {
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500126 double split = now();
127 fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
128 a->start = split;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800129 }
130 return status;
131}
132
133static Action *queue_action(unsigned op, const char *fmt, ...)
134{
135 Action *a;
136 va_list ap;
Bruce Beare50b39952010-07-15 08:52:01 -0700137 size_t cmdsize;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800138
139 a = calloc(1, sizeof(Action));
140 if (a == 0) die("out of memory");
141
142 va_start(ap, fmt);
Bruce Beare50b39952010-07-15 08:52:01 -0700143 cmdsize = vsnprintf(a->cmd, sizeof(a->cmd), fmt, ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 va_end(ap);
145
Bruce Beare50b39952010-07-15 08:52:01 -0700146 if (cmdsize >= sizeof(a->cmd)) {
147 free(a);
148 die("Command length (%d) exceeds maximum size (%d)", cmdsize, sizeof(a->cmd));
149 }
150
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151 if (action_last) {
152 action_last->next = a;
153 } else {
154 action_list = a;
155 }
156 action_last = a;
157 a->op = op;
158 a->func = cb_default;
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500159
160 a->start = -1;
161
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800162 return a;
163}
164
165void fb_queue_erase(const char *ptn)
166{
167 Action *a;
168 a = queue_action(OP_COMMAND, "erase:%s", ptn);
169 a->msg = mkmsg("erasing '%s'", ptn);
170}
171
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700172void fb_queue_flash(const char *ptn, void *data, size_t sz)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800173{
174 Action *a;
175
176 a = queue_action(OP_DOWNLOAD, "");
177 a->data = data;
178 a->size = sz;
179 a->msg = mkmsg("sending '%s' (%d KB)", ptn, sz / 1024);
180
181 a = queue_action(OP_COMMAND, "flash:%s", ptn);
182 a->msg = mkmsg("writing '%s'", ptn);
183}
184
Sasha Levitskiy782111b2014-05-05 19:43:15 -0700185void fb_queue_flash_sparse(const char *ptn, struct sparse_file *s, size_t sz)
Colin Crossf8387882012-05-24 17:18:41 -0700186{
187 Action *a;
188
189 a = queue_action(OP_DOWNLOAD_SPARSE, "");
190 a->data = s;
191 a->size = 0;
192 a->msg = mkmsg("sending sparse '%s' (%d KB)", ptn, sz / 1024);
193
194 a = queue_action(OP_COMMAND, "flash:%s", ptn);
195 a->msg = mkmsg("writing '%s'", ptn);
196}
197
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800198static int match(char *str, const char **value, unsigned count)
199{
200 const char *val;
201 unsigned n;
202 int len;
203
204 for (n = 0; n < count; n++) {
205 const char *val = value[n];
206 int len = strlen(val);
207 int match;
208
209 if ((len > 1) && (val[len-1] == '*')) {
210 len--;
211 match = !strncmp(val, str, len);
212 } else {
213 match = !strcmp(val, str);
214 }
215
216 if (match) return 1;
217 }
218
219 return 0;
220}
221
222
223
224static int cb_check(Action *a, int status, char *resp, int invert)
225{
226 const char **value = a->data;
227 unsigned count = a->size;
228 unsigned n;
229 int yes;
230
231 if (status) {
232 fprintf(stderr,"FAILED (%s)\n", resp);
233 return status;
234 }
235
Wink Savilleb98762f2011-04-04 17:54:59 -0700236 if (a->prod) {
237 if (strcmp(a->prod, cur_product) != 0) {
238 double split = now();
239 fprintf(stderr,"IGNORE, product is %s required only for %s [%7.3fs]\n",
240 cur_product, a->prod, (split - a->start));
241 a->start = split;
242 return 0;
243 }
244 }
245
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800246 yes = match(resp, value, count);
247 if (invert) yes = !yes;
248
249 if (yes) {
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500250 double split = now();
251 fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
252 a->start = split;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800253 return 0;
254 }
255
256 fprintf(stderr,"FAILED\n\n");
257 fprintf(stderr,"Device %s is '%s'.\n", a->cmd + 7, resp);
258 fprintf(stderr,"Update %s '%s'",
259 invert ? "rejects" : "requires", value[0]);
260 for (n = 1; n < count; n++) {
261 fprintf(stderr," or '%s'", value[n]);
262 }
263 fprintf(stderr,".\n\n");
264 return -1;
265}
266
267static int cb_require(Action *a, int status, char *resp)
268{
269 return cb_check(a, status, resp, 0);
270}
271
272static int cb_reject(Action *a, int status, char *resp)
273{
274 return cb_check(a, status, resp, 1);
275}
276
Wink Savilleb98762f2011-04-04 17:54:59 -0700277void fb_queue_require(const char *prod, const char *var,
278 int invert, unsigned nvalues, const char **value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800279{
280 Action *a;
281 a = queue_action(OP_QUERY, "getvar:%s", var);
Wink Savilleb98762f2011-04-04 17:54:59 -0700282 a->prod = prod;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800283 a->data = value;
284 a->size = nvalues;
285 a->msg = mkmsg("checking %s", var);
286 a->func = invert ? cb_reject : cb_require;
287 if (a->data == 0) die("out of memory");
288}
289
290static int cb_display(Action *a, int status, char *resp)
291{
292 if (status) {
293 fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
294 return status;
295 }
296 fprintf(stderr, "%s: %s\n", (char*) a->data, resp);
297 return 0;
298}
299
300void fb_queue_display(const char *var, const char *prettyname)
301{
302 Action *a;
303 a = queue_action(OP_QUERY, "getvar:%s", var);
304 a->data = strdup(prettyname);
305 if (a->data == 0) die("out of memory");
306 a->func = cb_display;
307}
308
Wink Savilleb98762f2011-04-04 17:54:59 -0700309static int cb_save(Action *a, int status, char *resp)
310{
311 if (status) {
312 fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
313 return status;
314 }
315 strncpy(a->data, resp, a->size);
316 return 0;
317}
318
319void fb_queue_query_save(const char *var, char *dest, unsigned dest_size)
320{
321 Action *a;
322 a = queue_action(OP_QUERY, "getvar:%s", var);
323 a->data = (void *)dest;
324 a->size = dest_size;
325 a->func = cb_save;
326}
327
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800328static int cb_do_nothing(Action *a, int status, char *resp)
329{
330 fprintf(stderr,"\n");
331 return 0;
332}
333
334void fb_queue_reboot(void)
335{
336 Action *a = queue_action(OP_COMMAND, "reboot");
337 a->func = cb_do_nothing;
338 a->msg = "rebooting";
339}
340
341void fb_queue_command(const char *cmd, const char *msg)
342{
343 Action *a = queue_action(OP_COMMAND, cmd);
344 a->msg = msg;
345}
346
347void fb_queue_download(const char *name, void *data, unsigned size)
348{
349 Action *a = queue_action(OP_DOWNLOAD, "");
350 a->data = data;
351 a->size = size;
352 a->msg = mkmsg("downloading '%s'", name);
353}
354
355void fb_queue_notice(const char *notice)
356{
357 Action *a = queue_action(OP_NOTICE, "");
358 a->data = (void*) notice;
359}
360
Mark Wachsler157b0012013-10-02 09:35:38 -0400361void fb_queue_wait_for_disconnect(void)
362{
363 queue_action(OP_WAIT_FOR_DISCONNECT, "");
364}
365
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700366int fb_execute_queue(usb_handle *usb)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800367{
368 Action *a;
369 char resp[FB_RESPONSE_SZ+1];
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700370 int status = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800371
372 a = action_list;
Scott Anderson13081c62012-04-06 12:39:30 -0700373 if (!a)
374 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800375 resp[FB_RESPONSE_SZ] = 0;
376
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500377 double start = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800378 for (a = action_list; a; a = a->next) {
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500379 a->start = now();
380 if (start < 0) start = a->start;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800381 if (a->msg) {
Brian Swetland63e52052010-06-28 11:14:26 -0700382 // fprintf(stderr,"%30s... ",a->msg);
383 fprintf(stderr,"%s...\n",a->msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800384 }
385 if (a->op == OP_DOWNLOAD) {
386 status = fb_download_data(usb, a->data, a->size);
387 status = a->func(a, status, status ? fb_get_error() : "");
388 if (status) break;
389 } else if (a->op == OP_COMMAND) {
390 status = fb_command(usb, a->cmd);
391 status = a->func(a, status, status ? fb_get_error() : "");
392 if (status) break;
393 } else if (a->op == OP_QUERY) {
394 status = fb_command_response(usb, a->cmd, resp);
395 status = a->func(a, status, status ? fb_get_error() : resp);
396 if (status) break;
397 } else if (a->op == OP_NOTICE) {
398 fprintf(stderr,"%s\n",(char*)a->data);
Colin Crossf8387882012-05-24 17:18:41 -0700399 } else if (a->op == OP_DOWNLOAD_SPARSE) {
400 status = fb_download_data_sparse(usb, a->data);
401 status = a->func(a, status, status ? fb_get_error() : "");
402 if (status) break;
Mark Wachsler157b0012013-10-02 09:35:38 -0400403 } else if (a->op == OP_WAIT_FOR_DISCONNECT) {
404 usb_wait_for_disconnect(usb);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800405 } else {
406 die("bogus action");
407 }
408 }
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500409
410 fprintf(stderr,"finished. total time: %.3fs\n", (now() - start));
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700411 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800412}
Scott Anderson13081c62012-04-06 12:39:30 -0700413
414int fb_queue_is_empty(void)
415{
416 return (action_list == NULL);
417}