blob: 345577d3821114b6064f15bc58aed77811b91320 [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
12 * the documentation and/or other materials provided with the
13 * 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
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * 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 Pomazau65cf84f2011-12-15 17:50:18 -080029#include "fastboot.h"
30#include "make_ext4fs.h"
31#include "ext4_utils.h"
32
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080033#include <stdio.h>
34#include <stdlib.h>
35#include <stdarg.h>
Anatol Pomazau65cf84f2011-12-15 17:50:18 -080036#include <stdbool.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080037#include <string.h>
Anatol Pomazau65cf84f2011-12-15 17:50:18 -080038#include <sys/mman.h>
39#include <sys/stat.h>
Daniel Sandlercb6e22b2010-02-25 14:05:33 -050040#include <sys/time.h>
Anatol Pomazau65cf84f2011-12-15 17:50:18 -080041#include <sys/types.h>
42#include <unistd.h>
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080043
Anatol Pomazau65cf84f2011-12-15 17:50:18 -080044extern struct fs_info info;
45
46#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080047
Daniel Sandlercb6e22b2010-02-25 14:05:33 -050048double now()
49{
50 struct timeval tv;
51 gettimeofday(&tv, NULL);
52 return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
53}
54
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080055char *mkmsg(const char *fmt, ...)
56{
57 char buf[256];
58 char *s;
59 va_list ap;
60
61 va_start(ap, fmt);
62 vsprintf(buf, fmt, ap);
63 va_end(ap);
64
65 s = strdup(buf);
66 if (s == 0) die("out of memory");
67 return s;
68}
69
70#define OP_DOWNLOAD 1
71#define OP_COMMAND 2
72#define OP_QUERY 3
73#define OP_NOTICE 4
Anatol Pomazau65cf84f2011-12-15 17:50:18 -080074#define OP_FORMAT 5
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080075
76typedef struct Action Action;
77
Anatol Pomazau65cf84f2011-12-15 17:50:18 -080078#define CMD_SIZE 64
79
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080080struct Action
81{
82 unsigned op;
83 Action *next;
84
Anatol Pomazau65cf84f2011-12-15 17:50:18 -080085 char cmd[CMD_SIZE];
Wink Savilleb98762f2011-04-04 17:54:59 -070086 const char *prod;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080087 void *data;
88 unsigned size;
89
90 const char *msg;
91 int (*func)(Action *a, int status, char *resp);
Daniel Sandlercb6e22b2010-02-25 14:05:33 -050092
93 double start;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080094};
95
96static Action *action_list = 0;
97static Action *action_last = 0;
98
Anatol Pomazau65cf84f2011-12-15 17:50:18 -080099
100struct image_data {
101 long long partition_size;
102 long long image_size; // real size of image file
103 void *buffer;
104};
105
106void generate_ext4_image(struct image_data *image);
107void munmap_image(struct image_data *image);
108
109struct generator {
110 char *fs_type;
111
112 /* generate image and return it as image->buffer.
113 * size of the buffer returned as image->image_size.
114 *
115 * image->partition_size specifies what is the size of the
116 * file partition we generate image for.
117 */
118 void (*generate)(struct image_data *image);
119
120 /* it cleans the buffer allocated during image creation.
121 * this function probably does free() or munmap().
122 */
123 void (*cleanup)(struct image_data *image);
124} generators[] = {
125 { "ext4", generate_ext4_image, munmap_image }
126};
127
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800128static int cb_default(Action *a, int status, char *resp)
129{
130 if (status) {
131 fprintf(stderr,"FAILED (%s)\n", resp);
132 } else {
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500133 double split = now();
134 fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
135 a->start = split;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800136 }
137 return status;
138}
139
140static Action *queue_action(unsigned op, const char *fmt, ...)
141{
142 Action *a;
143 va_list ap;
Bruce Beare50b39952010-07-15 08:52:01 -0700144 size_t cmdsize;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800145
146 a = calloc(1, sizeof(Action));
147 if (a == 0) die("out of memory");
148
149 va_start(ap, fmt);
Bruce Beare50b39952010-07-15 08:52:01 -0700150 cmdsize = vsnprintf(a->cmd, sizeof(a->cmd), fmt, ap);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800151 va_end(ap);
152
Bruce Beare50b39952010-07-15 08:52:01 -0700153 if (cmdsize >= sizeof(a->cmd)) {
154 free(a);
155 die("Command length (%d) exceeds maximum size (%d)", cmdsize, sizeof(a->cmd));
156 }
157
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800158 if (action_last) {
159 action_last->next = a;
160 } else {
161 action_list = a;
162 }
163 action_last = a;
164 a->op = op;
165 a->func = cb_default;
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500166
167 a->start = -1;
168
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800169 return a;
170}
171
172void fb_queue_erase(const char *ptn)
173{
174 Action *a;
175 a = queue_action(OP_COMMAND, "erase:%s", ptn);
176 a->msg = mkmsg("erasing '%s'", ptn);
177}
178
Anatol Pomazau65cf84f2011-12-15 17:50:18 -0800179void munmap_image(struct image_data *image)
180{
181 munmap(image->buffer, image->image_size);
182}
183
184void generate_ext4_image(struct image_data *image)
185{
186 int fd;
187 struct stat st;
188
189 fd = fileno(tmpfile());
Mike J. Chen714052b2012-02-04 16:22:22 -0800190 /* reset ext4fs info so we can be called multiple times */
191 reset_ext4fs_info();
Anatol Pomazau65cf84f2011-12-15 17:50:18 -0800192 info.len = image->partition_size;
193 make_ext4fs_internal(fd, NULL, NULL, 0, 0, 1, 0, 0, 0);
194
195 fstat(fd, &st);
196 image->image_size = st.st_size;
197
198 image->buffer = mmap(NULL, image->image_size, PROT_READ, MAP_PRIVATE, fd, 0);
199 if (image->buffer == MAP_FAILED) {
200 perror("mmap");
201 image->buffer = NULL;
202 }
203
204 close(fd);
205}
206
Mike J. Chen714052b2012-02-04 16:22:22 -0800207int fb_format(Action *a, usb_handle *usb, int skip_if_not_supported)
Anatol Pomazau65cf84f2011-12-15 17:50:18 -0800208{
209 const char *partition = a->cmd;
210 char query[256];
211 char response[FB_RESPONSE_SZ+1];
212 int status = 0;
213 struct image_data image;
214 struct generator *generator = NULL;
215 int fd;
216 unsigned i;
217 char cmd[CMD_SIZE];
218
219 response[FB_RESPONSE_SZ] = '\0';
220 snprintf(query, sizeof(query), "getvar:partition-type:%s", partition);
221 status = fb_command_response(usb, query, response);
222 if (status) {
Mike J. Chen714052b2012-02-04 16:22:22 -0800223 if (skip_if_not_supported) {
224 fprintf(stderr,
225 "Erase successful, but not automatically formatting.\n");
226 fprintf(stderr,
227 "Can't determine partition type.\n");
228 return 0;
229 }
Anatol Pomazau65cf84f2011-12-15 17:50:18 -0800230 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
231 return status;
232 }
233
234 for (i = 0; i < ARRAY_SIZE(generators); i++) {
235 if (!strncmp(generators[i].fs_type, response, FB_RESPONSE_SZ)) {
236 generator = &generators[i];
237 break;
238 }
239 }
240 if (!generator) {
Mike J. Chen714052b2012-02-04 16:22:22 -0800241 if (skip_if_not_supported) {
242 fprintf(stderr,
243 "Erase successful, but not automatically formatting.\n");
244 fprintf(stderr,
245 "File system type %s not supported.\n", response);
246 return 0;
247 }
Anatol Pomazau65cf84f2011-12-15 17:50:18 -0800248 fprintf(stderr,"Formatting is not supported for filesystem with type '%s'.\n",
249 response);
250 return -1;
251 }
252
253 response[FB_RESPONSE_SZ] = '\0';
254 snprintf(query, sizeof(query), "getvar:partition-size:%s", partition);
255 status = fb_command_response(usb, query, response);
256 if (status) {
Mike J. Chen714052b2012-02-04 16:22:22 -0800257 if (skip_if_not_supported) {
258 fprintf(stderr,
259 "Erase successful, but not automatically formatting.\n");
260 fprintf(stderr, "Unable to get partition size\n.");
261 return 0;
262 }
Anatol Pomazau65cf84f2011-12-15 17:50:18 -0800263 fprintf(stderr,"FAILED (%s)\n", fb_get_error());
264 return status;
265 }
266 image.partition_size = strtoll(response, (char **)NULL, 16);
267
268 generator->generate(&image);
269 if (!image.buffer) {
270 fprintf(stderr,"Cannot generate image.\n");
271 return -1;
272 }
273
274 // Following piece of code is similar to fb_queue_flash() but executes
275 // actions directly without queuing
276 fprintf(stderr, "sending '%s' (%lli KB)...\n", partition, image.image_size/1024);
277 status = fb_download_data(usb, image.buffer, image.image_size);
278 if (status) goto cleanup;
279
280 fprintf(stderr, "writing '%s'...\n", partition);
281 snprintf(cmd, CMD_SIZE, "flash:%s", partition);
282 status = fb_command(usb, cmd);
283 if (status) goto cleanup;
284
285cleanup:
286 generator->cleanup(&image);
287
288 return status;
289}
290
Mike J. Chen714052b2012-02-04 16:22:22 -0800291void fb_queue_format(const char *partition, int skip_if_not_supported)
Anatol Pomazau65cf84f2011-12-15 17:50:18 -0800292{
293 Action *a;
294
295 a = queue_action(OP_FORMAT, partition);
Mike J. Chen714052b2012-02-04 16:22:22 -0800296 a->data = (void*)skip_if_not_supported;
Anatol Pomazau65cf84f2011-12-15 17:50:18 -0800297 a->msg = mkmsg("formatting '%s' partition", partition);
298}
299
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800300void fb_queue_flash(const char *ptn, void *data, unsigned sz)
301{
302 Action *a;
303
304 a = queue_action(OP_DOWNLOAD, "");
305 a->data = data;
306 a->size = sz;
307 a->msg = mkmsg("sending '%s' (%d KB)", ptn, sz / 1024);
308
309 a = queue_action(OP_COMMAND, "flash:%s", ptn);
310 a->msg = mkmsg("writing '%s'", ptn);
311}
312
313static int match(char *str, const char **value, unsigned count)
314{
315 const char *val;
316 unsigned n;
317 int len;
318
319 for (n = 0; n < count; n++) {
320 const char *val = value[n];
321 int len = strlen(val);
322 int match;
323
324 if ((len > 1) && (val[len-1] == '*')) {
325 len--;
326 match = !strncmp(val, str, len);
327 } else {
328 match = !strcmp(val, str);
329 }
330
331 if (match) return 1;
332 }
333
334 return 0;
335}
336
337
338
339static int cb_check(Action *a, int status, char *resp, int invert)
340{
341 const char **value = a->data;
342 unsigned count = a->size;
343 unsigned n;
344 int yes;
345
346 if (status) {
347 fprintf(stderr,"FAILED (%s)\n", resp);
348 return status;
349 }
350
Wink Savilleb98762f2011-04-04 17:54:59 -0700351 if (a->prod) {
352 if (strcmp(a->prod, cur_product) != 0) {
353 double split = now();
354 fprintf(stderr,"IGNORE, product is %s required only for %s [%7.3fs]\n",
355 cur_product, a->prod, (split - a->start));
356 a->start = split;
357 return 0;
358 }
359 }
360
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800361 yes = match(resp, value, count);
362 if (invert) yes = !yes;
363
364 if (yes) {
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500365 double split = now();
366 fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
367 a->start = split;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800368 return 0;
369 }
370
371 fprintf(stderr,"FAILED\n\n");
372 fprintf(stderr,"Device %s is '%s'.\n", a->cmd + 7, resp);
373 fprintf(stderr,"Update %s '%s'",
374 invert ? "rejects" : "requires", value[0]);
375 for (n = 1; n < count; n++) {
376 fprintf(stderr," or '%s'", value[n]);
377 }
378 fprintf(stderr,".\n\n");
379 return -1;
380}
381
382static int cb_require(Action *a, int status, char *resp)
383{
384 return cb_check(a, status, resp, 0);
385}
386
387static int cb_reject(Action *a, int status, char *resp)
388{
389 return cb_check(a, status, resp, 1);
390}
391
Wink Savilleb98762f2011-04-04 17:54:59 -0700392void fb_queue_require(const char *prod, const char *var,
393 int invert, unsigned nvalues, const char **value)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800394{
395 Action *a;
396 a = queue_action(OP_QUERY, "getvar:%s", var);
Wink Savilleb98762f2011-04-04 17:54:59 -0700397 a->prod = prod;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800398 a->data = value;
399 a->size = nvalues;
400 a->msg = mkmsg("checking %s", var);
401 a->func = invert ? cb_reject : cb_require;
402 if (a->data == 0) die("out of memory");
403}
404
405static int cb_display(Action *a, int status, char *resp)
406{
407 if (status) {
408 fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
409 return status;
410 }
411 fprintf(stderr, "%s: %s\n", (char*) a->data, resp);
412 return 0;
413}
414
415void fb_queue_display(const char *var, const char *prettyname)
416{
417 Action *a;
418 a = queue_action(OP_QUERY, "getvar:%s", var);
419 a->data = strdup(prettyname);
420 if (a->data == 0) die("out of memory");
421 a->func = cb_display;
422}
423
Wink Savilleb98762f2011-04-04 17:54:59 -0700424static int cb_save(Action *a, int status, char *resp)
425{
426 if (status) {
427 fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
428 return status;
429 }
430 strncpy(a->data, resp, a->size);
431 return 0;
432}
433
434void fb_queue_query_save(const char *var, char *dest, unsigned dest_size)
435{
436 Action *a;
437 a = queue_action(OP_QUERY, "getvar:%s", var);
438 a->data = (void *)dest;
439 a->size = dest_size;
440 a->func = cb_save;
441}
442
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800443static int cb_do_nothing(Action *a, int status, char *resp)
444{
445 fprintf(stderr,"\n");
446 return 0;
447}
448
449void fb_queue_reboot(void)
450{
451 Action *a = queue_action(OP_COMMAND, "reboot");
452 a->func = cb_do_nothing;
453 a->msg = "rebooting";
454}
455
456void fb_queue_command(const char *cmd, const char *msg)
457{
458 Action *a = queue_action(OP_COMMAND, cmd);
459 a->msg = msg;
460}
461
462void fb_queue_download(const char *name, void *data, unsigned size)
463{
464 Action *a = queue_action(OP_DOWNLOAD, "");
465 a->data = data;
466 a->size = size;
467 a->msg = mkmsg("downloading '%s'", name);
468}
469
470void fb_queue_notice(const char *notice)
471{
472 Action *a = queue_action(OP_NOTICE, "");
473 a->data = (void*) notice;
474}
475
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700476int fb_execute_queue(usb_handle *usb)
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800477{
478 Action *a;
479 char resp[FB_RESPONSE_SZ+1];
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700480 int status = 0;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800481
482 a = action_list;
483 resp[FB_RESPONSE_SZ] = 0;
484
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500485 double start = -1;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800486 for (a = action_list; a; a = a->next) {
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500487 a->start = now();
488 if (start < 0) start = a->start;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800489 if (a->msg) {
Brian Swetland63e52052010-06-28 11:14:26 -0700490 // fprintf(stderr,"%30s... ",a->msg);
491 fprintf(stderr,"%s...\n",a->msg);
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800492 }
493 if (a->op == OP_DOWNLOAD) {
494 status = fb_download_data(usb, a->data, a->size);
495 status = a->func(a, status, status ? fb_get_error() : "");
496 if (status) break;
497 } else if (a->op == OP_COMMAND) {
498 status = fb_command(usb, a->cmd);
499 status = a->func(a, status, status ? fb_get_error() : "");
500 if (status) break;
501 } else if (a->op == OP_QUERY) {
502 status = fb_command_response(usb, a->cmd, resp);
503 status = a->func(a, status, status ? fb_get_error() : resp);
504 if (status) break;
505 } else if (a->op == OP_NOTICE) {
506 fprintf(stderr,"%s\n",(char*)a->data);
Anatol Pomazau65cf84f2011-12-15 17:50:18 -0800507 } else if (a->op == OP_FORMAT) {
Mike J. Chen714052b2012-02-04 16:22:22 -0800508 status = fb_format(a, usb, (int)a->data);
Anatol Pomazau65cf84f2011-12-15 17:50:18 -0800509 status = a->func(a, status, status ? fb_get_error() : "");
510 if (status) break;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800511 } else {
512 die("bogus action");
513 }
514 }
Daniel Sandlercb6e22b2010-02-25 14:05:33 -0500515
516 fprintf(stderr,"finished. total time: %.3fs\n", (now() - start));
Brian Carlstromeb31c0b2010-04-23 12:38:51 -0700517 return status;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800518}