engine.c revision 50b3995d027b53f24bbba099b3b6884d5845b614
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 <stdio.h>
30#include <stdlib.h>
31#include <stdarg.h>
32#include <string.h>
33#include <sys/time.h>
34
35#include "fastboot.h"
36
37double now()
38{
39    struct timeval tv;
40    gettimeofday(&tv, NULL);
41    return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
42}
43
44char *mkmsg(const char *fmt, ...)
45{
46    char buf[256];
47    char *s;
48    va_list ap;
49
50    va_start(ap, fmt);
51    vsprintf(buf, fmt, ap);
52    va_end(ap);
53
54    s = strdup(buf);
55    if (s == 0) die("out of memory");
56    return s;
57}
58
59#define OP_DOWNLOAD   1
60#define OP_COMMAND    2
61#define OP_QUERY      3
62#define OP_NOTICE     4
63
64typedef struct Action Action;
65
66struct Action
67{
68    unsigned op;
69    Action *next;
70
71    char cmd[64];
72    void *data;
73    unsigned size;
74
75    const char *msg;
76    int (*func)(Action *a, int status, char *resp);
77
78    double start;
79};
80
81static Action *action_list = 0;
82static Action *action_last = 0;
83
84static int cb_default(Action *a, int status, char *resp)
85{
86    if (status) {
87        fprintf(stderr,"FAILED (%s)\n", resp);
88    } else {
89        double split = now();
90        fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
91        a->start = split;
92    }
93    return status;
94}
95
96static Action *queue_action(unsigned op, const char *fmt, ...)
97{
98    Action *a;
99    va_list ap;
100    size_t cmdsize;
101
102    a = calloc(1, sizeof(Action));
103    if (a == 0) die("out of memory");
104
105    va_start(ap, fmt);
106    cmdsize = vsnprintf(a->cmd, sizeof(a->cmd), fmt, ap);
107    va_end(ap);
108
109    if (cmdsize >= sizeof(a->cmd)) {
110        free(a);
111        die("Command length (%d) exceeds maximum size (%d)", cmdsize, sizeof(a->cmd));
112    }
113
114    if (action_last) {
115        action_last->next = a;
116    } else {
117        action_list = a;
118    }
119    action_last = a;
120    a->op = op;
121    a->func = cb_default;
122
123    a->start = -1;
124
125    return a;
126}
127
128void fb_queue_erase(const char *ptn)
129{
130    Action *a;
131    a = queue_action(OP_COMMAND, "erase:%s", ptn);
132    a->msg = mkmsg("erasing '%s'", ptn);
133}
134
135void fb_queue_flash(const char *ptn, void *data, unsigned sz)
136{
137    Action *a;
138
139    a = queue_action(OP_DOWNLOAD, "");
140    a->data = data;
141    a->size = sz;
142    a->msg = mkmsg("sending '%s' (%d KB)", ptn, sz / 1024);
143
144    a = queue_action(OP_COMMAND, "flash:%s", ptn);
145    a->msg = mkmsg("writing '%s'", ptn);
146}
147
148static int match(char *str, const char **value, unsigned count)
149{
150    const char *val;
151    unsigned n;
152    int len;
153
154    for (n = 0; n < count; n++) {
155        const char *val = value[n];
156        int len = strlen(val);
157        int match;
158
159        if ((len > 1) && (val[len-1] == '*')) {
160            len--;
161            match = !strncmp(val, str, len);
162        } else {
163            match = !strcmp(val, str);
164        }
165
166        if (match) return 1;
167    }
168
169    return 0;
170}
171
172
173
174static int cb_check(Action *a, int status, char *resp, int invert)
175{
176    const char **value = a->data;
177    unsigned count = a->size;
178    unsigned n;
179    int yes;
180
181    if (status) {
182        fprintf(stderr,"FAILED (%s)\n", resp);
183        return status;
184    }
185
186    yes = match(resp, value, count);
187    if (invert) yes = !yes;
188
189    if (yes) {
190        double split = now();
191        fprintf(stderr,"OKAY [%7.3fs]\n", (split - a->start));
192        a->start = split;
193        return 0;
194    }
195
196    fprintf(stderr,"FAILED\n\n");
197    fprintf(stderr,"Device %s is '%s'.\n", a->cmd + 7, resp);
198    fprintf(stderr,"Update %s '%s'",
199            invert ? "rejects" : "requires", value[0]);
200    for (n = 1; n < count; n++) {
201        fprintf(stderr," or '%s'", value[n]);
202    }
203    fprintf(stderr,".\n\n");
204    return -1;
205}
206
207static int cb_require(Action *a, int status, char *resp)
208{
209    return cb_check(a, status, resp, 0);
210}
211
212static int cb_reject(Action *a, int status, char *resp)
213{
214    return cb_check(a, status, resp, 1);
215}
216
217void fb_queue_require(const char *var, int invert, unsigned nvalues, const char **value)
218{
219    Action *a;
220    a = queue_action(OP_QUERY, "getvar:%s", var);
221    a->data = value;
222    a->size = nvalues;
223    a->msg = mkmsg("checking %s", var);
224    a->func = invert ? cb_reject : cb_require;
225    if (a->data == 0) die("out of memory");
226}
227
228static int cb_display(Action *a, int status, char *resp)
229{
230    if (status) {
231        fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp);
232        return status;
233    }
234    fprintf(stderr, "%s: %s\n", (char*) a->data, resp);
235    return 0;
236}
237
238void fb_queue_display(const char *var, const char *prettyname)
239{
240    Action *a;
241    a = queue_action(OP_QUERY, "getvar:%s", var);
242    a->data = strdup(prettyname);
243    if (a->data == 0) die("out of memory");
244    a->func = cb_display;
245}
246
247static int cb_do_nothing(Action *a, int status, char *resp)
248{
249    fprintf(stderr,"\n");
250    return 0;
251}
252
253void fb_queue_reboot(void)
254{
255    Action *a = queue_action(OP_COMMAND, "reboot");
256    a->func = cb_do_nothing;
257    a->msg = "rebooting";
258}
259
260void fb_queue_command(const char *cmd, const char *msg)
261{
262    Action *a = queue_action(OP_COMMAND, cmd);
263    a->msg = msg;
264}
265
266void fb_queue_download(const char *name, void *data, unsigned size)
267{
268    Action *a = queue_action(OP_DOWNLOAD, "");
269    a->data = data;
270    a->size = size;
271    a->msg = mkmsg("downloading '%s'", name);
272}
273
274void fb_queue_notice(const char *notice)
275{
276    Action *a = queue_action(OP_NOTICE, "");
277    a->data = (void*) notice;
278}
279
280void fb_execute_queue(usb_handle *usb)
281{
282    Action *a;
283    char resp[FB_RESPONSE_SZ+1];
284    int status;
285
286    a = action_list;
287    resp[FB_RESPONSE_SZ] = 0;
288
289    double start = -1;
290    for (a = action_list; a; a = a->next) {
291        a->start = now();
292        if (start < 0) start = a->start;
293        if (a->msg) {
294            fprintf(stderr,"%30s... ",a->msg);
295        }
296        if (a->op == OP_DOWNLOAD) {
297            status = fb_download_data(usb, a->data, a->size);
298            status = a->func(a, status, status ? fb_get_error() : "");
299            if (status) break;
300        } else if (a->op == OP_COMMAND) {
301            status = fb_command(usb, a->cmd);
302            status = a->func(a, status, status ? fb_get_error() : "");
303            if (status) break;
304        } else if (a->op == OP_QUERY) {
305            status = fb_command_response(usb, a->cmd, resp);
306            status = a->func(a, status, status ? fb_get_error() : resp);
307            if (status) break;
308        } else if (a->op == OP_NOTICE) {
309            fprintf(stderr,"%s\n",(char*)a->data);
310        } else {
311            die("bogus action");
312        }
313    }
314
315    fprintf(stderr,"finished. total time: %.3fs\n", (now() - start));
316}
317
318