engine.c revision 8d7ddb35d510824e50833a9b31ae6486393b1436
1/*
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
29#include "fastboot.h"
30#include "fs.h"
31
32#include <errno.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <stdarg.h>
36#include <stdbool.h>
37#include <string.h>
38#include <sys/stat.h>
39#include <sys/types.h>
40#include <unistd.h>
41
42#ifdef USE_MINGW
43#include <fcntl.h>
44#else
45#include <sys/mman.h>
46#endif
47
48#define ARRAY_SIZE(x)           (sizeof(x)/sizeof(x[0]))
49
50#define OP_DOWNLOAD   1
51#define OP_COMMAND    2
52#define OP_QUERY      3
53#define OP_NOTICE     4
54#define OP_DOWNLOAD_SPARSE 5
55#define OP_WAIT_FOR_DISCONNECT 6
56
57typedef struct Action Action;
58
59#define CMD_SIZE 64
60
61struct Action
62{
63    unsigned op;
64    Action *next;
65
66    char cmd[CMD_SIZE];
67    const char *prod;
68    void *data;
69    unsigned size;
70
71    const char *msg;
72    int (*func)(Action *a, int status, char *resp);
73
74    double start;
75};
76
77static Action *action_list = 0;
78static Action *action_last = 0;
79
80
81
82
83int 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
97
98/* 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, const char *type_override)
106{
107    char fs_type[FB_RESPONSE_SZ + 1] = {0,};
108    int status;
109    unsigned int i;
110
111    if (type_override) {
112        return !!fs_get_generator(type_override);
113    }
114    status = fb_getvar(usb, fs_type, "partition-type:%s", partition);
115    if (status) {
116        return 0;
117    }
118    return !!fs_get_generator(fs_type);
119}
120
121static int cb_default(Action *a, int status, char *resp)
122{
123    if (status) {
124        fprintf(stderr,"FAILED (%s)\n", resp);
125    } else {
126        double split = now();
127        fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
128        a->start = split;
129    }
130    return status;
131}
132
133static Action *queue_action(unsigned op, const char *fmt, ...)
134{
135    Action *a;
136    va_list ap;
137    size_t cmdsize;
138
139    a = calloc(1, sizeof(Action));
140    if (a == 0) die("out of memory");
141
142    va_start(ap, fmt);
143    cmdsize = vsnprintf(a->cmd, sizeof(a->cmd), fmt, ap);
144    va_end(ap);
145
146    if (cmdsize >= sizeof(a->cmd)) {
147        free(a);
148        die("Command length (%d) exceeds maximum size (%d)", cmdsize, sizeof(a->cmd));
149    }
150
151    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;
159
160    a->start = -1;
161
162    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
172void fb_queue_flash(const char *ptn, void *data, unsigned sz)
173{
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
185void fb_queue_flash_sparse(const char *ptn, struct sparse_file *s, unsigned sz)
186{
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
198static 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
236    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
246    yes = match(resp, value, count);
247    if (invert) yes = !yes;
248
249    if (yes) {
250        double split = now();
251        fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
252        a->start = split;
253        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
277void fb_queue_require(const char *prod, const char *var,
278		int invert, unsigned nvalues, const char **value)
279{
280    Action *a;
281    a = queue_action(OP_QUERY, "getvar:%s", var);
282    a->prod = prod;
283    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
309static 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
328static 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
361void fb_queue_wait_for_disconnect(void)
362{
363    queue_action(OP_WAIT_FOR_DISCONNECT, "");
364}
365
366int fb_execute_queue(usb_handle *usb)
367{
368    Action *a;
369    char resp[FB_RESPONSE_SZ+1];
370    int status = 0;
371
372    a = action_list;
373    if (!a)
374        return status;
375    resp[FB_RESPONSE_SZ] = 0;
376
377    double start = -1;
378    for (a = action_list; a; a = a->next) {
379        a->start = now();
380        if (start < 0) start = a->start;
381        if (a->msg) {
382            // fprintf(stderr,"%30s... ",a->msg);
383            fprintf(stderr,"%s...\n",a->msg);
384        }
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);
399        } 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;
403        } else if (a->op == OP_WAIT_FOR_DISCONNECT) {
404            usb_wait_for_disconnect(usb);
405        } else {
406            die("bogus action");
407        }
408    }
409
410    fprintf(stderr,"finished. total time: %.3fs\n", (now() - start));
411    return status;
412}
413
414int fb_queue_is_empty(void)
415{
416    return (action_list == NULL);
417}
418