summaryrefslogtreecommitdiffstats
path: root/fastboot/engine.c
diff options
context:
space:
mode:
Diffstat (limited to 'fastboot/engine.c')
-rw-r--r--fastboot/engine.c305
1 files changed, 299 insertions, 6 deletions
diff --git a/fastboot/engine.c b/fastboot/engine.c
index 6d94035..8d46991 100644
--- a/fastboot/engine.c
+++ b/fastboot/engine.c
@@ -9,7 +9,7 @@
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
+ * the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
@@ -19,20 +19,34 @@
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
- * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
+#include "fastboot.h"
+#include "make_ext4fs.h"
+
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
+#include <stdbool.h>
#include <string.h>
+#include <sys/stat.h>
#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
-#include "fastboot.h"
+#ifdef USE_MINGW
+#include <fcntl.h>
+#else
+#include <sys/mman.h>
+#endif
+
+#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
double now()
{
@@ -50,7 +64,7 @@ char *mkmsg(const char *fmt, ...)
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
va_end(ap);
-
+
s = strdup(buf);
if (s == 0) die("out of memory");
return s;
@@ -60,15 +74,19 @@ char *mkmsg(const char *fmt, ...)
#define OP_COMMAND 2
#define OP_QUERY 3
#define OP_NOTICE 4
+#define OP_FORMAT 5
+#define OP_DOWNLOAD_SPARSE 6
typedef struct Action Action;
-struct Action
+#define CMD_SIZE 64
+
+struct Action
{
unsigned op;
Action *next;
- char cmd[64];
+ char cmd[CMD_SIZE];
const char *prod;
void *data;
unsigned size;
@@ -82,6 +100,82 @@ struct Action
static Action *action_list = 0;
static Action *action_last = 0;
+
+struct image_data {
+ long long partition_size;
+ long long image_size; // real size of image file
+ void *buffer;
+};
+
+void generate_ext4_image(struct image_data *image);
+void cleanup_image(struct image_data *image);
+
+int fb_getvar(struct usb_handle *usb, char *response, const char *fmt, ...)
+{
+ char cmd[CMD_SIZE] = "getvar:";
+ int getvar_len = strlen(cmd);
+ va_list args;
+
+ response[FB_RESPONSE_SZ] = '\0';
+ va_start(args, fmt);
+ vsnprintf(cmd + getvar_len, sizeof(cmd) - getvar_len, fmt, args);
+ va_end(args);
+ cmd[CMD_SIZE - 1] = '\0';
+ return fb_command_response(usb, cmd, response);
+}
+
+struct generator {
+ char *fs_type;
+
+ /* generate image and return it as image->buffer.
+ * size of the buffer returned as image->image_size.
+ *
+ * image->partition_size specifies what is the size of the
+ * file partition we generate image for.
+ */
+ void (*generate)(struct image_data *image);
+
+ /* it cleans the buffer allocated during image creation.
+ * this function probably does free() or munmap().
+ */
+ void (*cleanup)(struct image_data *image);
+} generators[] = {
+ { "ext4", generate_ext4_image, cleanup_image }
+};
+
+/* Return true if this partition is supported by the fastboot format command.
+ * It is also used to determine if we should first erase a partition before
+ * flashing it with an ext4 filesystem. See needs_erase()
+ *
+ * Not all devices report the filesystem type, so don't report any errors,
+ * just return false.
+ */
+int fb_format_supported(usb_handle *usb, const char *partition)
+{
+ char response[FB_RESPONSE_SZ+1];
+ struct generator *generator = NULL;
+ int status;
+ unsigned int i;
+
+ status = fb_getvar(usb, response, "partition-type:%s", partition);
+ if (status) {
+ return 0;
+ }
+
+ for (i = 0; i < ARRAY_SIZE(generators); i++) {
+ if (!strncmp(generators[i].fs_type, response, FB_RESPONSE_SZ)) {
+ generator = &generators[i];
+ break;
+ }
+ }
+
+ if (generator) {
+ return 1;
+ }
+
+ return 0;
+}
+
static int cb_default(Action *a, int status, char *resp)
{
if (status) {
@@ -133,6 +227,177 @@ void fb_queue_erase(const char *ptn)
a->msg = mkmsg("erasing '%s'", ptn);
}
+/* Loads file content into buffer. Returns NULL on error. */
+static void *load_buffer(int fd, off_t size)
+{
+ void *buffer;
+
+#ifdef USE_MINGW
+ ssize_t count = 0;
+
+ // mmap is more efficient but mingw does not support it.
+ // In this case we read whole image into memory buffer.
+ buffer = malloc(size);
+ if (!buffer) {
+ perror("malloc");
+ return NULL;
+ }
+
+ lseek(fd, 0, SEEK_SET);
+ while(count < size) {
+ ssize_t actually_read = read(fd, (char*)buffer+count, size-count);
+
+ if (actually_read == 0) {
+ break;
+ }
+ if (actually_read < 0) {
+ if (errno == EINTR) {
+ continue;
+ }
+ perror("read");
+ free(buffer);
+ return NULL;
+ }
+
+ count += actually_read;
+ }
+#else
+ buffer = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, 0);
+ if (buffer == MAP_FAILED) {
+ perror("mmap");
+ return NULL;
+ }
+#endif
+
+ return buffer;
+}
+
+void cleanup_image(struct image_data *image)
+{
+#ifdef USE_MINGW
+ free(image->buffer);
+#else
+ munmap(image->buffer, image->image_size);
+#endif
+}
+
+void generate_ext4_image(struct image_data *image)
+{
+ int fd;
+ struct stat st;
+
+#ifdef USE_MINGW
+ /* Ideally we should use tmpfile() here, the same as with unix version.
+ * But unfortunately it is not portable as it is not clear whether this
+ * function opens file in TEXT or BINARY mode.
+ *
+ * There are also some reports it is buggy:
+ * http://pdplab.it.uom.gr/teaching/gcc_manuals/gnulib.html#tmpfile
+ * http://www.mega-nerd.com/erikd/Blog/Windiots/tmpfile.html
+ */
+ char *filename = tempnam(getenv("TEMP"), "fastboot-format.img");
+ fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0644);
+ unlink(filename);
+#else
+ fd = fileno(tmpfile());
+#endif
+ make_ext4fs_sparse_fd(fd, image->partition_size, NULL, NULL);
+
+ fstat(fd, &st);
+ image->image_size = st.st_size;
+ image->buffer = load_buffer(fd, st.st_size);
+
+ close(fd);
+}
+
+int fb_format(Action *a, usb_handle *usb, int skip_if_not_supported)
+{
+ const char *partition = a->cmd;
+ char response[FB_RESPONSE_SZ+1];
+ int status = 0;
+ struct image_data image;
+ struct generator *generator = NULL;
+ int fd;
+ unsigned i;
+ char cmd[CMD_SIZE];
+
+ status = fb_getvar(usb, response, "partition-type:%s", partition);
+ if (status) {
+ if (skip_if_not_supported) {
+ fprintf(stderr,
+ "Erase successful, but not automatically formatting.\n");
+ fprintf(stderr,
+ "Can't determine partition type.\n");
+ return 0;
+ }
+ fprintf(stderr,"FAILED (%s)\n", fb_get_error());
+ return status;
+ }
+
+ for (i = 0; i < ARRAY_SIZE(generators); i++) {
+ if (!strncmp(generators[i].fs_type, response, FB_RESPONSE_SZ)) {
+ generator = &generators[i];
+ break;
+ }
+ }
+ if (!generator) {
+ if (skip_if_not_supported) {
+ fprintf(stderr,
+ "Erase successful, but not automatically formatting.\n");
+ fprintf(stderr,
+ "File system type %s not supported.\n", response);
+ return 0;
+ }
+ fprintf(stderr,"Formatting is not supported for filesystem with type '%s'.\n",
+ response);
+ return -1;
+ }
+
+ status = fb_getvar(usb, response, "partition-size:%s", partition);
+ if (status) {
+ if (skip_if_not_supported) {
+ fprintf(stderr,
+ "Erase successful, but not automatically formatting.\n");
+ fprintf(stderr, "Unable to get partition size\n.");
+ return 0;
+ }
+ fprintf(stderr,"FAILED (%s)\n", fb_get_error());
+ return status;
+ }
+ image.partition_size = strtoll(response, (char **)NULL, 16);
+
+ generator->generate(&image);
+ if (!image.buffer) {
+ fprintf(stderr,"Cannot generate image.\n");
+ return -1;
+ }
+
+ // Following piece of code is similar to fb_queue_flash() but executes
+ // actions directly without queuing
+ fprintf(stderr, "sending '%s' (%lli KB)...\n", partition, image.image_size/1024);
+ status = fb_download_data(usb, image.buffer, image.image_size);
+ if (status) goto cleanup;
+
+ fprintf(stderr, "writing '%s'...\n", partition);
+ snprintf(cmd, sizeof(cmd), "flash:%s", partition);
+ status = fb_command(usb, cmd);
+ if (status) goto cleanup;
+
+cleanup:
+ generator->cleanup(&image);
+
+ return status;
+}
+
+void fb_queue_format(const char *partition, int skip_if_not_supported)
+{
+ Action *a;
+
+ a = queue_action(OP_FORMAT, partition);
+ a->data = (void*)skip_if_not_supported;
+ a->msg = mkmsg("formatting '%s' partition", partition);
+}
+
void fb_queue_flash(const char *ptn, void *data, unsigned sz)
{
Action *a;
@@ -146,6 +411,19 @@ void fb_queue_flash(const char *ptn, void *data, unsigned sz)
a->msg = mkmsg("writing '%s'", ptn);
}
+void fb_queue_flash_sparse(const char *ptn, struct sparse_file *s, unsigned sz)
+{
+ Action *a;
+
+ a = queue_action(OP_DOWNLOAD_SPARSE, "");
+ a->data = s;
+ a->size = 0;
+ a->msg = mkmsg("sending sparse '%s' (%d KB)", ptn, sz / 1024);
+
+ a = queue_action(OP_COMMAND, "flash:%s", ptn);
+ a->msg = mkmsg("writing '%s'", ptn);
+}
+
static int match(char *str, const char **value, unsigned count)
{
const char *val;
@@ -316,6 +594,8 @@ int fb_execute_queue(usb_handle *usb)
int status = 0;
a = action_list;
+ if (!a)
+ return status;
resp[FB_RESPONSE_SZ] = 0;
double start = -1;
@@ -340,6 +620,14 @@ int fb_execute_queue(usb_handle *usb)
if (status) break;
} else if (a->op == OP_NOTICE) {
fprintf(stderr,"%s\n",(char*)a->data);
+ } else if (a->op == OP_FORMAT) {
+ status = fb_format(a, usb, (int)a->data);
+ status = a->func(a, status, status ? fb_get_error() : "");
+ if (status) break;
+ } else if (a->op == OP_DOWNLOAD_SPARSE) {
+ status = fb_download_data_sparse(usb, a->data);
+ status = a->func(a, status, status ? fb_get_error() : "");
+ if (status) break;
} else {
die("bogus action");
}
@@ -348,3 +636,8 @@ int fb_execute_queue(usb_handle *usb)
fprintf(stderr,"finished. total time: %.3fs\n", (now() - start));
return status;
}
+
+int fb_queue_is_empty(void)
+{
+ return (action_list == NULL);
+}