diff options
37 files changed, 1101 insertions, 727 deletions
diff --git a/Android.mk b/Android.mk index 3b899b5fc..577679c06 100644 --- a/Android.mk +++ b/Android.mk @@ -46,7 +46,6 @@ LOCAL_STATIC_LIBRARIES := \ libmincrypt \ libminadbd \ libminui \ - libpixelflinger_static \ libpng \ libfs_mgr \ libcutils \ diff --git a/applypatch/applypatch.c b/applypatch/applypatch.c index c9c40c98f..60e9e4a5c 100644 --- a/applypatch/applypatch.c +++ b/applypatch/applypatch.c @@ -231,7 +231,7 @@ static int LoadPartitionContents(const char* filename, FileContents* file) { break; } if (next != read) { - printf("short read (%d bytes of %d) for partition \"%s\"\n", + printf("short read (%zu bytes of %zu) for partition \"%s\"\n", read, next, partition); free(file->data); file->data = NULL; @@ -258,7 +258,7 @@ static int LoadPartitionContents(const char* filename, FileContents* file) { if (memcmp(sha_so_far, parsed_sha, SHA_DIGEST_SIZE) == 0) { // we have a match. stop reading the partition; we'll return // the data we've read so far. - printf("partition read matched size %d sha %s\n", + printf("partition read matched size %zu sha %s\n", size[index[i]], sha1sum[index[i]]); break; } @@ -386,7 +386,7 @@ int WriteToPartition(unsigned char* data, size_t len, size_t written = mtd_write_data(ctx, (char*)data, len); if (written != len) { - printf("only wrote %d of %d bytes to MTD %s\n", + printf("only wrote %zu of %zu bytes to MTD %s\n", written, len, partition); mtd_write_close(ctx); return -1; @@ -460,20 +460,20 @@ int WriteToPartition(unsigned char* data, size_t len, if (errno == EINTR) { read_count = 0; } else { - printf("verify read error %s at %d: %s\n", + printf("verify read error %s at %zu: %s\n", partition, p, strerror(errno)); return -1; } } if ((size_t)read_count < to_read) { - printf("short verify read %s at %d: %d %d %s\n", + printf("short verify read %s at %zu: %zd %zu %s\n", partition, p, read_count, to_read, strerror(errno)); } so_far += read_count; } if (memcmp(buffer, data+p, to_read)) { - printf("verification failed starting at %d\n", p); + printf("verification failed starting at %zu\n", p); start = p; break; } diff --git a/applypatch/imgpatch.c b/applypatch/imgpatch.c index 3a1df3872..af4d07281 100644 --- a/applypatch/imgpatch.c +++ b/applypatch/imgpatch.c @@ -18,6 +18,7 @@ // format. #include <stdio.h> +#include <sys/cdefs.h> #include <sys/stat.h> #include <errno.h> #include <unistd.h> @@ -35,7 +36,7 @@ * file, and update the SHA context with the output data as well. * Return 0 on success. */ -int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size, +int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size __unused, const Value* patch, SinkFn sink, void* token, SHA_CTX* ctx, const Value* bonus_data) { @@ -132,7 +133,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size, unsigned char* expanded_source = malloc(expanded_len); if (expanded_source == NULL) { - printf("failed to allocate %d bytes for expanded_source\n", + printf("failed to allocate %zu bytes for expanded_source\n", expanded_len); return -1; } @@ -163,7 +164,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size, // We should have filled the output buffer exactly, except // for the bonus_size. if (strm.avail_out != bonus_size) { - printf("source inflation short by %d bytes\n", strm.avail_out-bonus_size); + printf("source inflation short by %zu bytes\n", strm.avail_out-bonus_size); return -1; } inflateEnd(&strm); diff --git a/interlace-frames.py b/interlace-frames.py new file mode 100644 index 000000000..243e565e7 --- /dev/null +++ b/interlace-frames.py @@ -0,0 +1,53 @@ +# Copyright (C) 2014 The Android Open Source Project +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Script to take a set of frames (PNG files) for a recovery animation +and turn it into a single output image which contains the input frames +interlaced by row. Run with the names of all the input frames on the +command line, in order, followed by the name of the output file.""" + +import sys +try: + import Image + import PngImagePlugin +except ImportError: + print "This script requires the Python Imaging Library to be installed." + sys.exit(1) + +frames = [Image.open(fn).convert("RGB") for fn in sys.argv[1:-1]] +assert len(frames) > 0, "Must have at least one input frame." +sizes = set() +for fr in frames: + sizes.add(fr.size) + +assert len(sizes) == 1, "All input images must have the same size." +w, h = sizes.pop() +N = len(frames) + +out = Image.new("RGB", (w, h*N)) +for j in range(h): + for i in range(w): + for fn, f in enumerate(frames): + out.putpixel((i, j*N+fn), f.getpixel((i, j))) + +# When loading this image, the graphics library expects to find a text +# chunk that specifies how many frames this animation represents. If +# you post-process the output of this script with some kind of +# optimizer tool (eg pngcrush or zopflipng) make sure that your +# optimizer preserves this text chunk. + +meta = PngImagePlugin.PngInfo() +meta.add_text("Frames", str(N)) + +out.save(sys.argv[-1], pnginfo=meta) diff --git a/make-overlay.py b/make-overlay.py deleted file mode 100644 index 7f931b373..000000000 --- a/make-overlay.py +++ /dev/null @@ -1,102 +0,0 @@ -# Copyright (C) 2011 The Android Open Source Project -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -"""Script to take a set of frames (PNG files) for a recovery -"installing" icon animation and turn it into a base image plus a set -of overlays, as needed by the recovery UI code. Run with the names of -all the input frames on the command line, in order.""" - -import sys -try: - import Image -except ImportError: - print "This script requires the Python Imaging Library to be installed." - sys.exit(1) - -# Find the smallest box that contains all the pixels which change -# between images. - -print "reading", sys.argv[1] -base = Image.open(sys.argv[1]) - -minmini = base.size[0]-1 -maxmaxi = 0 -minminj = base.size[1]-1 -maxmaxj = 0 - -for top_name in sys.argv[2:]: - print "reading", top_name - top = Image.open(top_name) - - assert base.size == top.size - - mini = base.size[0]-1 - maxi = 0 - minj = base.size[1]-1 - maxj = 0 - - h, w = base.size - for j in range(w): - for i in range(h): - b = base.getpixel((i,j)) - t = top.getpixel((i,j)) - if b != t: - if i < mini: mini = i - if i > maxi: maxi = i - if j < minj: minj = j - if j > maxj: maxj = j - - minmini = min(minmini, mini) - maxmaxi = max(maxmaxi, maxi) - minminj = min(minminj, minj) - maxmaxj = max(maxmaxj, maxj) - -w = maxmaxi - minmini + 1 -h = maxmaxj - minminj + 1 - -# Now write out an image containing just that box, for each frame. - -for num, top_name in enumerate(sys.argv[1:]): - top = Image.open(top_name) - - out = Image.new("RGB", (w, h)) - for i in range(w): - for j in range(h): - t = top.getpixel((i+minmini, j+minminj)) - out.putpixel((i, j), t) - - fn = "icon_installing_overlay%02d.png" % (num+1,) - out.save(fn) - print "saved", fn - -# Write out the base icon, which is the first frame with that box -# blacked out (just to make the file smaller, since it's always -# displayed with one of the overlays on top of it). - -for i in range(w): - for j in range(h): - base.putpixel((i+minmini, j+minminj), (0, 0, 0)) -fn = "icon_installing.png" -base.save(fn) -print "saved", fn - -# The device_ui_init() function needs to tell the recovery UI the -# position of the overlay box. - -print -print "add this to your device_ui_init() function:" -print "-" * 40 -print " ui_parameters->install_overlay_offset_x = %d;" % (minmini,) -print " ui_parameters->install_overlay_offset_y = %d;" % (minminj,) -print "-" * 40 diff --git a/minadbd/sockets.c b/minadbd/sockets.c index 2dd646159..817410d13 100644 --- a/minadbd/sockets.c +++ b/minadbd/sockets.c @@ -319,7 +319,8 @@ static void local_socket_event_func(int fd, unsigned ev, void *_s) while(avail > 0) { r = adb_read(fd, x, avail); - D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%d\n", s->id, s->fd, r, r<0?errno:0, avail); + D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu\n", + s->id, s->fd, r, r<0?errno:0, avail); if(r > 0) { avail -= r; x += r; diff --git a/minadbd/transport.c b/minadbd/transport.c index 4c0c97f75..92679f518 100644 --- a/minadbd/transport.c +++ b/minadbd/transport.c @@ -713,7 +713,7 @@ int readx(int fd, void *ptr, size_t len) char *p = ptr; int r; #if ADB_TRACE - int len0 = len; + size_t len0 = len; #endif D("readx: fd=%d wanted=%d\n", fd, (int)len); while(len > 0) { @@ -734,7 +734,7 @@ int readx(int fd, void *ptr, size_t len) } #if ADB_TRACE - D("readx: fd=%d wanted=%d got=%d\n", fd, len0, len0 - len); + D("readx: fd=%d wanted=%zu got=%zu\n", fd, len0, len0 - len); dump_hex( ptr, len0 ); #endif return 0; diff --git a/minadbd/usb_linux_client.c b/minadbd/usb_linux_client.c index c135d6396..29bab1558 100644 --- a/minadbd/usb_linux_client.c +++ b/minadbd/usb_linux_client.c @@ -388,7 +388,7 @@ static int bulk_read(int bulk_out, char *buf, size_t length) ret = adb_read(bulk_out, buf + count, length - count); if (ret < 0) { if (errno != EINTR) { - D("[ bulk_read failed fd=%d length=%d count=%d ]\n", + D("[ bulk_read failed fd=%d length=%zu count=%zu ]\n", bulk_out, length, count); return ret; } diff --git a/minui/Android.mk b/minui/Android.mk index 43e0ad33b..5cc84ac7a 100644 --- a/minui/Android.mk +++ b/minui/Android.mk @@ -1,7 +1,7 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) -LOCAL_SRC_FILES := graphics.c events.c resources.c +LOCAL_SRC_FILES := graphics.c graphics_fbdev.c events.c resources.c LOCAL_C_INCLUDES +=\ external/libpng\ diff --git a/minui/graphics.c b/minui/graphics.c index a2014dca3..32b3361e3 100644 --- a/minui/graphics.c +++ b/minui/graphics.c @@ -28,188 +28,37 @@ #include <linux/fb.h> #include <linux/kd.h> -#include <pixelflinger/pixelflinger.h> +#include <time.h> #include "font_10x18.h" #include "minui.h" - -#if defined(RECOVERY_BGRA) -#define PIXEL_FORMAT GGL_PIXEL_FORMAT_BGRA_8888 -#define PIXEL_SIZE 4 -#elif defined(RECOVERY_RGBX) -#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGBX_8888 -#define PIXEL_SIZE 4 -#else -#define PIXEL_FORMAT GGL_PIXEL_FORMAT_RGB_565 -#define PIXEL_SIZE 2 -#endif - -#define NUM_BUFFERS 2 +#include "graphics.h" typedef struct { - GGLSurface* texture; - unsigned cwidth; - unsigned cheight; + GRSurface* texture; + int cwidth; + int cheight; } GRFont; -static GRFont *gr_font = 0; -static GGLContext *gr_context = 0; -static GGLSurface gr_font_texture; -static GGLSurface gr_framebuffer[NUM_BUFFERS]; -static GGLSurface gr_mem_surface; -static unsigned gr_active_fb = 0; -static unsigned double_buffering = 0; +static GRFont* gr_font = NULL; +static minui_backend* gr_backend = NULL; + static int overscan_percent = OVERSCAN_PERCENT; static int overscan_offset_x = 0; static int overscan_offset_y = 0; -static int gr_fb_fd = -1; static int gr_vt_fd = -1; -static struct fb_var_screeninfo vi; -static struct fb_fix_screeninfo fi; - -static int get_framebuffer(GGLSurface *fb) -{ - int fd; - void *bits; - - fd = open("/dev/graphics/fb0", O_RDWR); - if (fd < 0) { - perror("cannot open fb0"); - return -1; - } - - if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) { - perror("failed to get fb0 info"); - close(fd); - return -1; - } - - vi.bits_per_pixel = PIXEL_SIZE * 8; - if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_BGRA_8888) { - vi.red.offset = 8; - vi.red.length = 8; - vi.green.offset = 16; - vi.green.length = 8; - vi.blue.offset = 24; - vi.blue.length = 8; - vi.transp.offset = 0; - vi.transp.length = 8; - } else if (PIXEL_FORMAT == GGL_PIXEL_FORMAT_RGBX_8888) { - vi.red.offset = 24; - vi.red.length = 8; - vi.green.offset = 16; - vi.green.length = 8; - vi.blue.offset = 8; - vi.blue.length = 8; - vi.transp.offset = 0; - vi.transp.length = 8; - } else { /* RGB565*/ - vi.red.offset = 11; - vi.red.length = 5; - vi.green.offset = 5; - vi.green.length = 6; - vi.blue.offset = 0; - vi.blue.length = 5; - vi.transp.offset = 0; - vi.transp.length = 0; - } - if (ioctl(fd, FBIOPUT_VSCREENINFO, &vi) < 0) { - perror("failed to put fb0 info"); - close(fd); - return -1; - } - - if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) { - perror("failed to get fb0 info"); - close(fd); - return -1; - } - - bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (bits == MAP_FAILED) { - perror("failed to mmap framebuffer"); - close(fd); - return -1; - } - - overscan_offset_x = vi.xres * overscan_percent / 100; - overscan_offset_y = vi.yres * overscan_percent / 100; - - fb->version = sizeof(*fb); - fb->width = vi.xres; - fb->height = vi.yres; - fb->stride = fi.line_length/PIXEL_SIZE; - fb->data = bits; - fb->format = PIXEL_FORMAT; - memset(fb->data, 0, vi.yres * fi.line_length); - - fb++; +static unsigned char gr_current_r = 255; +static unsigned char gr_current_g = 255; +static unsigned char gr_current_b = 255; +static unsigned char gr_current_a = 255; - /* check if we can use double buffering */ - if (vi.yres * fi.line_length * 2 > fi.smem_len) - return fd; +static GRSurface* gr_draw = NULL; - double_buffering = 1; - - fb->version = sizeof(*fb); - fb->width = vi.xres; - fb->height = vi.yres; - fb->stride = fi.line_length/PIXEL_SIZE; - fb->data = (void*) (((char*) bits) + vi.yres * fi.line_length); - fb->format = PIXEL_FORMAT; - memset(fb->data, 0, vi.yres * fi.line_length); - - return fd; -} - -static void get_memory_surface(GGLSurface* ms) { - ms->version = sizeof(*ms); - ms->width = vi.xres; - ms->height = vi.yres; - ms->stride = fi.line_length/PIXEL_SIZE; - ms->data = malloc(fi.line_length * vi.yres); - ms->format = PIXEL_FORMAT; -} - -static void set_active_framebuffer(unsigned n) -{ - if (n > 1 || !double_buffering) return; - vi.yres_virtual = vi.yres * NUM_BUFFERS; - vi.yoffset = n * vi.yres; - vi.bits_per_pixel = PIXEL_SIZE * 8; - if (ioctl(gr_fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) { - perror("active fb swap failed"); - } -} - -void gr_flip(void) -{ - GGLContext *gl = gr_context; - - /* swap front and back buffers */ - if (double_buffering) - gr_active_fb = (gr_active_fb + 1) & 1; - - /* copy data from the in-memory surface to the buffer we're about - * to make active. */ - memcpy(gr_framebuffer[gr_active_fb].data, gr_mem_surface.data, - fi.line_length * vi.yres); - - /* inform the display driver */ - set_active_framebuffer(gr_active_fb); -} - -void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) +static bool outside(int x, int y) { - GGLContext *gl = gr_context; - GGLint color[4]; - color[0] = ((r << 8) | r) + 1; - color[1] = ((g << 8) | g) + 1; - color[2] = ((b << 8) | b) + 1; - color[3] = ((a << 8) | a) + 1; - gl->color4xv(gl, color); + return x < 0 || x >= gr_draw->width || y < 0 || y >= gr_draw->height; } int gr_measure(const char *s) @@ -223,58 +72,118 @@ void gr_font_size(int *x, int *y) *y = gr_font->cheight; } -int gr_text(int x, int y, const char *s, int bold) +static void text_blend(unsigned char* src_p, int src_row_bytes, + unsigned char* dst_p, int dst_row_bytes, + int width, int height) +{ + int i, j; + for (j = 0; j < height; ++j) { + unsigned char* sx = src_p; + unsigned char* px = dst_p; + for (i = 0; i < width; ++i) { + unsigned char a = *sx++; + if (gr_current_a < 255) a = ((int)a * gr_current_a) / 255; + if (a == 255) { + *px++ = gr_current_r; + *px++ = gr_current_g; + *px++ = gr_current_b; + px++; + } else if (a > 0) { + *px = (*px * (255-a) + gr_current_r * a) / 255; + ++px; + *px = (*px * (255-a) + gr_current_g * a) / 255; + ++px; + *px = (*px * (255-a) + gr_current_b * a) / 255; + ++px; + ++px; + } else { + px += 4; + } + } + src_p += src_row_bytes; + dst_p += dst_row_bytes; + } +} + + +void gr_text(int x, int y, const char *s, int bold) { - GGLContext *gl = gr_context; GRFont *font = gr_font; unsigned off; - if (!font->texture) return x; + if (!font->texture) return; + if (gr_current_a == 0) return; bold = bold && (font->texture->height != font->cheight); x += overscan_offset_x; y += overscan_offset_y; - gl->bindTexture(gl, font->texture); - gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); - gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); - gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); - gl->enable(gl, GGL_TEXTURE_2D); - while((off = *s++)) { off -= 32; + if (outside(x, y) || outside(x+font->cwidth-1, y+font->cheight-1)) break; if (off < 96) { - gl->texCoord2i(gl, (off * font->cwidth) - x, - (bold ? font->cheight : 0) - y); - gl->recti(gl, x, y, x + font->cwidth, y + font->cheight); + + unsigned char* src_p = font->texture->data + (off * font->cwidth) + + (bold ? font->cheight * font->texture->row_bytes : 0); + unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes; + + text_blend(src_p, font->texture->row_bytes, + dst_p, gr_draw->row_bytes, + font->cwidth, font->cheight); + } x += font->cwidth; } - - return x; } -void gr_texticon(int x, int y, gr_surface icon) { - if (gr_context == NULL || icon == NULL) { +void gr_texticon(int x, int y, GRSurface* icon) { + if (icon == NULL) return; + + if (icon->pixel_bytes != 1) { + printf("gr_texticon: source has wrong format\n"); return; } - GGLContext* gl = gr_context; x += overscan_offset_x; y += overscan_offset_y; - gl->bindTexture(gl, (GGLSurface*) icon); - gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); - gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); - gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); - gl->enable(gl, GGL_TEXTURE_2D); + if (outside(x, y) || outside(x+icon->width-1, y+icon->height-1)) return; - int w = gr_get_width(icon); - int h = gr_get_height(icon); + unsigned char* src_p = icon->data; + unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes; - gl->texCoord2i(gl, -x, -y); - gl->recti(gl, x, y, x+gr_get_width(icon), y+gr_get_height(icon)); + text_blend(src_p, icon->row_bytes, + dst_p, gr_draw->row_bytes, + icon->width, icon->height); +} + +void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) +{ + gr_current_r = r; + gr_current_g = g; + gr_current_b = b; + gr_current_a = a; +} + +void gr_clear() +{ + if (gr_current_r == gr_current_g && + gr_current_r == gr_current_b) { + memset(gr_draw->data, gr_current_r, gr_draw->height * gr_draw->row_bytes); + } else { + int x, y; + unsigned char* px = gr_draw->data; + for (y = 0; y < gr_draw->height; ++y) { + for (x = 0; x < gr_draw->width; ++x) { + *px++ = gr_current_r; + *px++ = gr_current_g; + *px++ = gr_current_b; + px++; + } + px += gr_draw->row_bytes - (gr_draw->width * gr_draw->pixel_bytes); + } + } } void gr_fill(int x1, int y1, int x2, int y2) @@ -285,48 +194,82 @@ void gr_fill(int x1, int y1, int x2, int y2) x2 += overscan_offset_x; y2 += overscan_offset_y; - GGLContext *gl = gr_context; - gl->disable(gl, GGL_TEXTURE_2D); - gl->recti(gl, x1, y1, x2, y2); + if (outside(x1, y1) || outside(x2-1, y2-1)) return; + + unsigned char* p = gr_draw->data + y1 * gr_draw->row_bytes + x1 * gr_draw->pixel_bytes; + if (gr_current_a == 255) { + int x, y; + for (y = y1; y < y2; ++y) { + unsigned char* px = p; + for (x = x1; x < x2; ++x) { + *px++ = gr_current_r; + *px++ = gr_current_g; + *px++ = gr_current_b; + px++; + } + p += gr_draw->row_bytes; + } + } else if (gr_current_a > 0) { + int x, y; + for (y = y1; y < y2; ++y) { + unsigned char* px = p; + for (x = x1; x < x2; ++x) { + *px = (*px * (255-gr_current_a) + gr_current_r * gr_current_a) / 255; + ++px; + *px = (*px * (255-gr_current_a) + gr_current_g * gr_current_a) / 255; + ++px; + *px = (*px * (255-gr_current_a) + gr_current_b * gr_current_a) / 255; + ++px; + ++px; + } + p += gr_draw->row_bytes; + } + } } -void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy) { - if (gr_context == NULL || source == NULL) { +void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) { + if (source == NULL) return; + + if (gr_draw->pixel_bytes != source->pixel_bytes) { + printf("gr_blit: source has wrong format\n"); return; } - GGLContext *gl = gr_context; dx += overscan_offset_x; dy += overscan_offset_y; - gl->bindTexture(gl, (GGLSurface*) source); - gl->texEnvi(gl, GGL_TEXTURE_ENV, GGL_TEXTURE_ENV_MODE, GGL_REPLACE); - gl->texGeni(gl, GGL_S, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); - gl->texGeni(gl, GGL_T, GGL_TEXTURE_GEN_MODE, GGL_ONE_TO_ONE); - gl->enable(gl, GGL_TEXTURE_2D); - gl->texCoord2i(gl, sx - dx, sy - dy); - gl->recti(gl, dx, dy, dx + w, dy + h); + if (outside(dx, dy) || outside(dx+w-1, dy+h-1)) return; + + unsigned char* src_p = source->data + sy*source->row_bytes + sx*source->pixel_bytes; + unsigned char* dst_p = gr_draw->data + dy*gr_draw->row_bytes + dx*gr_draw->pixel_bytes; + + int i; + for (i = 0; i < h; ++i) { + memcpy(dst_p, src_p, w * source->pixel_bytes); + src_p += source->row_bytes; + dst_p += gr_draw->row_bytes; + } } -unsigned int gr_get_width(gr_surface surface) { +unsigned int gr_get_width(GRSurface* surface) { if (surface == NULL) { return 0; } - return ((GGLSurface*) surface)->width; + return surface->width; } -unsigned int gr_get_height(gr_surface surface) { +unsigned int gr_get_height(GRSurface* surface) { if (surface == NULL) { return 0; } - return ((GGLSurface*) surface)->height; + return surface->height; } static void gr_init_font(void) { gr_font = calloc(sizeof(*gr_font), 1); - int res = res_create_surface("font", (void**)&(gr_font->texture)); + int res = res_create_alpha_surface("font", &(gr_font->texture)); if (res == 0) { // The font image should be a 96x2 array of character images. The // columns are the printable ASCII characters 0x20 - 0x7f. The @@ -340,7 +283,8 @@ static void gr_init_font(void) gr_font->texture = malloc(sizeof(*gr_font->texture)); gr_font->texture->width = font.width; gr_font->texture->height = font.height; - gr_font->texture->stride = font.width; + gr_font->texture->row_bytes = font.width; + gr_font->texture->pixel_bytes = 1; unsigned char* bits = malloc(font.width * font.height); gr_font->texture->data = (void*) bits; @@ -355,17 +299,65 @@ static void gr_init_font(void) gr_font->cwidth = font.cwidth; gr_font->cheight = font.cheight; } +} - // interpret the grayscale as alpha - gr_font->texture->format = GGL_PIXEL_FORMAT_A_8; +#if 0 +// Exercises many of the gr_*() functions; useful for testing. +static void gr_test() { + GRSurface** images; + int frames; + int result = res_create_multi_surface("icon_installing", &frames, &images); + if (result < 0) { + printf("create surface %d\n", result); + gr_exit(); + return; + } + + time_t start = time(NULL); + int x; + for (x = 0; x <= 1200; ++x) { + if (x < 400) { + gr_color(0, 0, 0, 255); + } else { + gr_color(0, (x-400)%128, 0, 255); + } + gr_clear(); + + gr_color(255, 0, 0, 255); + gr_surface frame = images[x%frames]; + gr_blit(frame, 0, 0, frame->width, frame->height, x, 0); + + gr_color(255, 0, 0, 128); + gr_fill(400, 150, 600, 350); + + gr_color(255, 255, 255, 255); + gr_text(500, 225, "hello, world!", 0); + gr_color(255, 255, 0, 128); + gr_text(300+x, 275, "pack my box with five dozen liquor jugs", 1); + + gr_color(0, 0, 255, 128); + gr_fill(gr_draw->width - 200 - x, 300, gr_draw->width - x, 500); + + gr_draw = gr_backend->flip(gr_backend); + } + printf("getting end time\n"); + time_t end = time(NULL); + printf("got end time\n"); + printf("start %ld end %ld\n", (long)start, (long)end); + if (end > start) { + printf("%.2f fps\n", ((double)x) / (end-start)); + } +} +#endif + +void gr_flip() { + gr_draw = gr_backend->flip(gr_backend); } int gr_init(void) { - gglInit(&gr_context); - GGLContext *gl = gr_context; - gr_init_font(); + gr_vt_fd = open("/dev/tty0", O_RDWR | O_SYNC); if (gr_vt_fd < 0) { // This is non-fatal; post-Cupcake kernels don't have tty0. @@ -377,38 +369,24 @@ int gr_init(void) return -1; } - gr_fb_fd = get_framebuffer(gr_framebuffer); - if (gr_fb_fd < 0) { - gr_exit(); + gr_backend = open_fbdev(); + gr_draw = gr_backend->init(gr_backend); + if (gr_draw == NULL) { return -1; } - get_memory_surface(&gr_mem_surface); - - printf("framebuffer: fd %d (%d x %d)\n", - gr_fb_fd, gr_framebuffer[0].width, gr_framebuffer[0].height); - - /* start with 0 as front (displayed) and 1 as back (drawing) */ - gr_active_fb = 0; - set_active_framebuffer(0); - gl->colorBuffer(gl, &gr_mem_surface); + overscan_offset_x = gr_draw->width * overscan_percent / 100; + overscan_offset_y = gr_draw->height * overscan_percent / 100; - gl->activeTexture(gl, 0); - gl->enable(gl, GGL_BLEND); - gl->blendFunc(gl, GGL_SRC_ALPHA, GGL_ONE_MINUS_SRC_ALPHA); - - gr_fb_blank(true); - gr_fb_blank(false); + gr_flip(); + gr_flip(); return 0; } void gr_exit(void) { - close(gr_fb_fd); - gr_fb_fd = -1; - - free(gr_mem_surface.data); + gr_backend->exit(gr_backend); ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT); close(gr_vt_fd); @@ -417,24 +395,15 @@ void gr_exit(void) int gr_fb_width(void) { - return gr_framebuffer[0].width - 2*overscan_offset_x; + return gr_draw->width - 2*overscan_offset_x; } int gr_fb_height(void) { - return gr_framebuffer[0].height - 2*overscan_offset_y; -} - -gr_pixel *gr_fb_data(void) -{ - return (unsigned short *) gr_mem_surface.data; + return gr_draw->height - 2*overscan_offset_y; } void gr_fb_blank(bool blank) { - int ret; - - ret = ioctl(gr_fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK); - if (ret < 0) - perror("ioctl(): blank"); + gr_backend->blank(gr_backend, blank); } diff --git a/minui/graphics.h b/minui/graphics.h new file mode 100644 index 000000000..94b89a554 --- /dev/null +++ b/minui/graphics.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef _GRAPHICS_H_ +#define _GRAPHICS_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include <stdbool.h> +#include "minui.h" + +typedef struct minui_backend { + // Initializes the backend and returns a gr_surface to draw into. + gr_surface (*init)(struct minui_backend*); + + // Causes the current drawing surface (returned by the most recent + // call to flip() or init()) to be displayed, and returns a new + // drawing surface. + gr_surface (*flip)(struct minui_backend*); + + // Blank (or unblank) the screen. + void (*blank)(struct minui_backend*, bool); + + // Device cleanup when drawing is done. + void (*exit)(struct minui_backend*); +} minui_backend; + +minui_backend* open_fbdev(); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/minui/graphics_fbdev.c b/minui/graphics_fbdev.c new file mode 100644 index 000000000..bb91975a9 --- /dev/null +++ b/minui/graphics_fbdev.c @@ -0,0 +1,202 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <stdbool.h> +#include <stdlib.h> +#include <unistd.h> + +#include <fcntl.h> +#include <stdio.h> + +#include <sys/cdefs.h> +#include <sys/ioctl.h> +#include <sys/mman.h> +#include <sys/types.h> + +#include <linux/fb.h> +#include <linux/kd.h> + +#include "minui.h" +#include "graphics.h" + +static gr_surface fbdev_init(minui_backend*); +static gr_surface fbdev_flip(minui_backend*); +static void fbdev_blank(minui_backend*, bool); +static void fbdev_exit(minui_backend*); + +static GRSurface gr_framebuffer[2]; +static bool double_buffered; +static GRSurface* gr_draw = NULL; +static int displayed_buffer; + +static struct fb_var_screeninfo vi; +static int fb_fd = -1; + +static minui_backend my_backend = { + .init = fbdev_init, + .flip = fbdev_flip, + .blank = fbdev_blank, + .exit = fbdev_exit, +}; + +minui_backend* open_fbdev() { + return &my_backend; +} + +static void fbdev_blank(minui_backend* backend __unused, bool blank) +{ + int ret; + + ret = ioctl(fb_fd, FBIOBLANK, blank ? FB_BLANK_POWERDOWN : FB_BLANK_UNBLANK); + if (ret < 0) + perror("ioctl(): blank"); +} + +static void set_displayed_framebuffer(unsigned n) +{ + if (n > 1 || !double_buffered) return; + + vi.yres_virtual = gr_framebuffer[0].height * 2; + vi.yoffset = n * gr_framebuffer[0].height; + vi.bits_per_pixel = gr_framebuffer[0].pixel_bytes * 8; + if (ioctl(fb_fd, FBIOPUT_VSCREENINFO, &vi) < 0) { + perror("active fb swap failed"); + } + displayed_buffer = n; +} + +static gr_surface fbdev_init(minui_backend* backend) { + int fd; + void *bits; + + struct fb_fix_screeninfo fi; + + fd = open("/dev/graphics/fb0", O_RDWR); + if (fd < 0) { + perror("cannot open fb0"); + return NULL; + } + + if (ioctl(fd, FBIOGET_FSCREENINFO, &fi) < 0) { + perror("failed to get fb0 info"); + close(fd); + return NULL; + } + + if (ioctl(fd, FBIOGET_VSCREENINFO, &vi) < 0) { + perror("failed to get fb0 info"); + close(fd); + return NULL; + } + + // We print this out for informational purposes only, but + // throughout we assume that the framebuffer device uses an RGBX + // pixel format. This is the case for every development device I + // have access to. For some of those devices (eg, hammerhead aka + // Nexus 5), FBIOGET_VSCREENINFO *reports* that it wants a + // different format (XBGR) but actually produces the correct + // results on the display when you write RGBX. + // + // If you have a device that actually *needs* another pixel format + // (ie, BGRX, or 565), patches welcome... + + printf("fb0 reports (possibly inaccurate):\n" + " vi.bits_per_pixel = %d\n" + " vi.red.offset = %3d .length = %3d\n" + " vi.green.offset = %3d .length = %3d\n" + " vi.blue.offset = %3d .length = %3d\n", + vi.bits_per_pixel, + vi.red.offset, vi.red.length, + vi.green.offset, vi.green.length, + vi.blue.offset, vi.blue.length); + + bits = mmap(0, fi.smem_len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (bits == MAP_FAILED) { + perror("failed to mmap framebuffer"); + close(fd); + return NULL; + } + + gr_framebuffer[0].width = vi.xres; + gr_framebuffer[0].height = vi.yres; + gr_framebuffer[0].row_bytes = fi.line_length; + gr_framebuffer[0].pixel_bytes = vi.bits_per_pixel / 8; + gr_framebuffer[0].data = bits; + memset(gr_framebuffer[0].data, 0, gr_framebuffer[0].height * gr_framebuffer[0].row_bytes); + + /* check if we can use double buffering */ + if (vi.yres * fi.line_length * 2 <= fi.smem_len) { + double_buffered = true; + + memcpy(gr_framebuffer+1, gr_framebuffer, sizeof(GRSurface)); + gr_framebuffer[1].data = gr_framebuffer[0].data + + gr_framebuffer[0].height * gr_framebuffer[0].row_bytes; + + gr_draw = gr_framebuffer+1; + + } else { + double_buffered = false; + + // Without double-buffering, we allocate RAM for a buffer to + // draw in, and then "flipping" the buffer consists of a + // memcpy from the buffer we allocated to the framebuffer. + + gr_draw = (GRSurface*) malloc(sizeof(GRSurface)); + memcpy(gr_draw, gr_framebuffer, sizeof(GRSurface)); + gr_draw->data = (unsigned char*) malloc(gr_draw->height * gr_draw->row_bytes); + if (!gr_draw->data) { + perror("failed to allocate in-memory surface"); + return NULL; + } + } + + memset(gr_draw->data, 0, gr_draw->height * gr_draw->row_bytes); + fb_fd = fd; + set_displayed_framebuffer(0); + + printf("framebuffer: %d (%d x %d)\n", fb_fd, gr_draw->width, gr_draw->height); + + fbdev_blank(backend, true); + fbdev_blank(backend, false); + + return gr_draw; +} + +static gr_surface fbdev_flip(minui_backend* backend __unused) { + if (double_buffered) { + // Change gr_draw to point to the buffer currently displayed, + // then flip the driver so we're displaying the other buffer + // instead. + gr_draw = gr_framebuffer + displayed_buffer; + set_displayed_framebuffer(1-displayed_buffer); + } else { + // Copy from the in-memory surface to the framebuffer. + memcpy(gr_framebuffer[0].data, gr_draw->data, + gr_draw->height * gr_draw->row_bytes); + } + return gr_draw; +} + +static void fbdev_exit(minui_backend* backend __unused) { + close(fb_fd); + fb_fd = -1; + + if (!double_buffered && gr_draw) { + free(gr_draw->data); + free(gr_draw); + } + gr_draw = NULL; +} diff --git a/minui/minui.h b/minui/minui.h index 573dd71f4..733b675f3 100644 --- a/minui/minui.h +++ b/minui/minui.h @@ -17,28 +17,38 @@ #ifndef _MINUI_H_ #define _MINUI_H_ +#include <sys/types.h> + #include <stdbool.h> #ifdef __cplusplus extern "C" { #endif -typedef void* gr_surface; -typedef unsigned short gr_pixel; +typedef struct { + int width; + int height; + int row_bytes; + int pixel_bytes; + unsigned char* data; +} GRSurface; + +typedef GRSurface* gr_surface; int gr_init(void); void gr_exit(void); int gr_fb_width(void); int gr_fb_height(void); -gr_pixel *gr_fb_data(void); + void gr_flip(void); void gr_fb_blank(bool blank); +void gr_clear(); // clear entire surface to current color void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a); void gr_fill(int x1, int y1, int x2, int y2); -int gr_text(int x, int y, const char *s, int bold); - void gr_texticon(int x, int y, gr_surface icon); +void gr_text(int x, int y, const char *s, int bold); +void gr_texticon(int x, int y, gr_surface icon); int gr_measure(const char *s); void gr_font_size(int *x, int *y); @@ -71,9 +81,40 @@ int ev_get_epollfd(void); // Resources -// Returns 0 if no error, else negative. -int res_create_surface(const char* name, gr_surface* pSurface); -int res_create_localized_surface(const char* name, gr_surface* pSurface); +// res_create_*_surface() functions return 0 if no error, else +// negative. +// +// A "display" surface is one that is intended to be drawn to the +// screen with gr_blit(). An "alpha" surface is a grayscale image +// interpreted as an alpha mask used to render text in the current +// color (with gr_text() or gr_texticon()). +// +// All these functions load PNG images from "/res/images/${name}.png". + +// Load a single display surface from a PNG image. +int res_create_display_surface(const char* name, gr_surface* pSurface); + +// Load an array of display surfaces from a single PNG image. The PNG +// should have a 'Frames' text chunk whose value is the number of +// frames this image represents. The pixel data itself is interlaced +// by row. +int res_create_multi_display_surface(const char* name, + int* frames, gr_surface** pSurface); + +// Load a single alpha surface from a grayscale PNG image. +int res_create_alpha_surface(const char* name, gr_surface* pSurface); + +// Load part of a grayscale PNG image that is the first match for the +// given locale. The image is expected to be a composite of multiple +// translations of the same text, with special added rows that encode +// the subimages' size and intended locale in the pixel data. See +// development/tools/recovery_l10n for an app that will generate these +// specialized images from Android resources. +int res_create_localized_alpha_surface(const char* name, const char* locale, + gr_surface* pSurface); + +// Free a surface allocated by any of the res_create_*_surface() +// functions. void res_free_surface(gr_surface surface); #ifdef __cplusplus diff --git a/minui/resources.c b/minui/resources.c index b20c00abd..2bae4ded0 100644 --- a/minui/resources.c +++ b/minui/resources.c @@ -27,31 +27,28 @@ #include <linux/fb.h> #include <linux/kd.h> -#include <pixelflinger/pixelflinger.h> - #include <png.h> #include "minui.h" extern char* locale; -// libpng gives "undefined reference to 'pow'" errors, and I have no -// idea how to convince the build system to link with -lm. We don't -// need this functionality (it's used for gamma adjustment) so provide -// a dummy implementation to satisfy the linker. -double pow(double x, double y) { - return x * y; +#define SURFACE_DATA_ALIGNMENT 8 + +static gr_surface malloc_surface(size_t data_size) { + unsigned char* temp = malloc(sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT); + if (temp == NULL) return NULL; + gr_surface surface = (gr_surface) temp; + surface->data = temp + sizeof(GRSurface) + + (SURFACE_DATA_ALIGNMENT - (sizeof(GRSurface) % SURFACE_DATA_ALIGNMENT)); + return surface; } -int res_create_surface(const char* name, gr_surface* pSurface) { +static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr, + png_uint_32* width, png_uint_32* height, png_byte* channels) { char resPath[256]; - GGLSurface* surface = NULL; - int result = 0; unsigned char header[8]; - png_structp png_ptr = NULL; - png_infop info_ptr = NULL; - - *pSurface = NULL; + int result = 0; snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name); resPath[sizeof(resPath)-1] = '\0'; @@ -72,124 +69,286 @@ int res_create_surface(const char* name, gr_surface* pSurface) { goto exit; } - png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if (!png_ptr) { + *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); + if (!*png_ptr) { result = -4; goto exit; } - info_ptr = png_create_info_struct(png_ptr); - if (!info_ptr) { + *info_ptr = png_create_info_struct(*png_ptr); + if (!*info_ptr) { result = -5; goto exit; } - if (setjmp(png_jmpbuf(png_ptr))) { + if (setjmp(png_jmpbuf(*png_ptr))) { result = -6; goto exit; } - png_init_io(png_ptr, fp); - png_set_sig_bytes(png_ptr, sizeof(header)); - png_read_info(png_ptr, info_ptr); + png_init_io(*png_ptr, fp); + png_set_sig_bytes(*png_ptr, sizeof(header)); + png_read_info(*png_ptr, *info_ptr); int color_type, bit_depth; - size_t width, height; - png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, + png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth, &color_type, NULL, NULL, NULL); - int channels = png_get_channels(png_ptr, info_ptr); - - if (!(bit_depth == 8 && - ((channels == 3 && color_type == PNG_COLOR_TYPE_RGB) || - (channels == 4 && color_type == PNG_COLOR_TYPE_RGBA) || - (channels == 1 && (color_type == PNG_COLOR_TYPE_PALETTE || - color_type == PNG_COLOR_TYPE_GRAY))))) { - return -7; + *channels = png_get_channels(*png_ptr, *info_ptr); + + if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) { + // 8-bit RGB images: great, nothing to do. + } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) { + // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray. + png_set_expand_gray_1_2_4_to_8(*png_ptr); + } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) { + // paletted images: expand to 8-bit RGB. Note that we DON'T + // currently expand the tRNS chunk (if any) to an alpha + // channel, because minui doesn't support alpha channels in + // general. + png_set_palette_to_rgb(*png_ptr); + *channels = 3; + } else { + fprintf(stderr, "minui doesn't support PNG depth %d channels %d color_type %d\n", + bit_depth, *channels, color_type); + result = -7; goto exit; } - size_t stride = (color_type == PNG_COLOR_TYPE_GRAY ? 1 : 4) * width; - size_t pixelSize = stride * height; + return result; - surface = malloc(sizeof(GGLSurface) + pixelSize); - if (surface == NULL) { - result = -8; - goto exit; + exit: + if (result < 0) { + png_destroy_read_struct(png_ptr, info_ptr, NULL); } - unsigned char* pData = (unsigned char*) (surface + 1); - surface->version = sizeof(GGLSurface); + if (fp != NULL) { + fclose(fp); + } + + return result; +} + +// "display" surfaces are transformed into the framebuffer's required +// pixel format (currently only RGBX is supported) at load time, so +// gr_blit() can be nothing more than a memcpy() for each row. The +// next two functions are the only ones that know anything about the +// framebuffer pixel format; they need to be modified if the +// framebuffer format changes (but nothing else should). + +// Allocate and return a gr_surface sufficient for storing an image of +// the indicated size in the framebuffer pixel format. +static gr_surface init_display_surface(png_uint_32 width, png_uint_32 height) { + gr_surface surface; + + surface = malloc_surface(width * height * 4); + if (surface == NULL) return NULL; + surface->width = width; surface->height = height; - surface->stride = width; /* Yes, pixels, not bytes */ - surface->data = pData; - - if (channels == 3) { - surface->format = GGL_PIXEL_FORMAT_RGBX_8888; - } else if (color_type == PNG_COLOR_TYPE_PALETTE) { - surface->format = GGL_PIXEL_FORMAT_RGBA_8888; - } else if (channels == 1) { - surface->format = GGL_PIXEL_FORMAT_L_8; - } else { - surface->format = GGL_PIXEL_FORMAT_RGBA_8888; - } + surface->row_bytes = width * 4; + surface->pixel_bytes = 4; - int alpha = (channels == 4); - if (color_type == PNG_COLOR_TYPE_PALETTE) { - png_set_palette_to_rgb(png_ptr); - } - if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { - png_set_tRNS_to_alpha(png_ptr); - alpha = 1; + return surface; +} + +// Copy 'input_row' to 'output_row', transforming it to the +// framebuffer pixel format. The input format depends on the value of +// 'channels': +// +// 1 - input is 8-bit grayscale +// 3 - input is 24-bit RGB +// 4 - input is 32-bit RGBA/RGBX +// +// 'width' is the number of pixels in the row. +static void transform_rgb_to_draw(unsigned char* input_row, + unsigned char* output_row, + int channels, int width) { + int x; + unsigned char* ip = input_row; + unsigned char* op = output_row; + + switch (channels) { + case 1: + // expand gray level to RGBX + for (x = 0; x < width; ++x) { + *op++ = *ip; + *op++ = *ip; + *op++ = *ip; + *op++ = 0xff; + ip++; + } + break; + + case 3: + // expand RGBA to RGBX + for (x = 0; x < width; ++x) { + *op++ = *ip++; + *op++ = *ip++; + *op++ = *ip++; + *op++ = 0xff; + } + break; + + case 4: + // copy RGBA to RGBX + memcpy(output_row, input_row, width*4); + break; } - if (color_type == PNG_COLOR_TYPE_GRAY) { - alpha = 1; +} + +int res_create_display_surface(const char* name, gr_surface* pSurface) { + gr_surface surface = NULL; + int result = 0; + png_structp png_ptr = NULL; + png_infop info_ptr = NULL; + png_uint_32 width, height; + png_byte channels; + + *pSurface = NULL; + + result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); + if (result < 0) return result; + + surface = init_display_surface(width, height); + if (surface == NULL) { + result = -8; + goto exit; } + unsigned char* p_row = malloc(width * 4); unsigned int y; - if (channels == 3 || (channels == 1 && !alpha)) { - for (y = 0; y < height; ++y) { - unsigned char* pRow = pData + y * stride; - png_read_row(png_ptr, pRow, NULL); - - int x; - for(x = width - 1; x >= 0; x--) { - int sx = x * 3; - int dx = x * 4; - unsigned char r = pRow[sx]; - unsigned char g = pRow[sx + 1]; - unsigned char b = pRow[sx + 2]; - unsigned char a = 0xff; - pRow[dx ] = r; // r - pRow[dx + 1] = g; // g - pRow[dx + 2] = b; // b - pRow[dx + 3] = a; + for (y = 0; y < height; ++y) { + png_read_row(png_ptr, p_row, NULL); + transform_rgb_to_draw(p_row, surface->data + y * surface->row_bytes, channels, width); + } + free(p_row); + + *pSurface = surface; + + exit: + png_destroy_read_struct(&png_ptr, &info_ptr, NULL); + if (result < 0 && surface != NULL) free(surface); + return result; +} + +int res_create_multi_display_surface(const char* name, int* frames, gr_surface** pSurface) { + gr_surface* surface = NULL; + int result = 0; + png_structp png_ptr = NULL; + png_infop info_ptr = NULL; + png_uint_32 width, height; + png_byte channels; + int i; + + *pSurface = NULL; + *frames = -1; + + result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); + if (result < 0) return result; + + *frames = 1; + png_textp text; + int num_text; + if (png_get_text(png_ptr, info_ptr, &text, &num_text)) { + for (i = 0; i < num_text; ++i) { + if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) { + *frames = atoi(text[i].text); + break; } } - } else { - for (y = 0; y < height; ++y) { - unsigned char* pRow = pData + y * stride; - png_read_row(png_ptr, pRow, NULL); + printf(" found frames = %d\n", *frames); + } + + if (height % *frames != 0) { + printf("bad height (%d) for frame count (%d)\n", height, *frames); + result = -9; + goto exit; + } + + surface = malloc(*frames * sizeof(gr_surface)); + if (surface == NULL) { + result = -8; + goto exit; + } + for (i = 0; i < *frames; ++i) { + surface[i] = init_display_surface(width, height / *frames); + if (surface[i] == NULL) { + result = -8; + goto exit; } } - *pSurface = (gr_surface) surface; + unsigned char* p_row = malloc(width * 4); + unsigned int y; + for (y = 0; y < height; ++y) { + png_read_row(png_ptr, p_row, NULL); + int frame = y % *frames; + unsigned char* out_row = surface[frame]->data + + (y / *frames) * surface[frame]->row_bytes; + transform_rgb_to_draw(p_row, out_row, channels, width); + } + free(p_row); + + *pSurface = (gr_surface*) surface; exit: png_destroy_read_struct(&png_ptr, &info_ptr, NULL); - if (fp != NULL) { - fclose(fp); - } if (result < 0) { if (surface) { + for (i = 0; i < *frames; ++i) { + if (surface[i]) free(surface[i]); + } free(surface); } } return result; } -static int matches_locale(const char* loc) { +int res_create_alpha_surface(const char* name, gr_surface* pSurface) { + gr_surface surface = NULL; + int result = 0; + png_structp png_ptr = NULL; + png_infop info_ptr = NULL; + png_uint_32 width, height; + png_byte channels; + + *pSurface = NULL; + + result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); + if (result < 0) return result; + + if (channels != 1) { + result = -7; + goto exit; + } + + surface = malloc_surface(width * height); + if (surface == NULL) { + result = -8; + goto exit; + } + surface->width = width; + surface->height = height; + surface->row_bytes = width; + surface->pixel_bytes = 1; + + unsigned char* p_row; + unsigned int y; + for (y = 0; y < height; ++y) { + p_row = surface->data + y * surface->row_bytes; + png_read_row(png_ptr, p_row, NULL); + } + + *pSurface = surface; + + exit: + png_destroy_read_struct(&png_ptr, &info_ptr, NULL); + if (result < 0 && surface != NULL) free(surface); + return result; +} + +static int matches_locale(const char* loc, const char* locale) { if (locale == NULL) return 0; if (strcmp(loc, locale) == 0) return 1; @@ -206,98 +365,61 @@ static int matches_locale(const char* loc) { return (strncmp(locale, loc, i) == 0 && locale[i] == '_'); } -int res_create_localized_surface(const char* name, gr_surface* pSurface) { - char resPath[256]; - GGLSurface* surface = NULL; +int res_create_localized_alpha_surface(const char* name, + const char* locale, + gr_surface* pSurface) { + gr_surface surface = NULL; int result = 0; - unsigned char header[8]; png_structp png_ptr = NULL; png_infop info_ptr = NULL; + png_uint_32 width, height; + png_byte channels; *pSurface = NULL; - snprintf(resPath, sizeof(resPath)-1, "/res/images/%s.png", name); - resPath[sizeof(resPath)-1] = '\0'; - FILE* fp = fopen(resPath, "rb"); - if (fp == NULL) { - result = -1; + if (locale == NULL) { + surface = malloc_surface(0); + surface->width = 0; + surface->height = 0; + surface->row_bytes = 0; + surface->pixel_bytes = 1; goto exit; } - size_t bytesRead = fread(header, 1, sizeof(header), fp); - if (bytesRead != sizeof(header)) { - result = -2; - goto exit; - } - - if (png_sig_cmp(header, 0, sizeof(header))) { - result = -3; - goto exit; - } + result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); + if (result < 0) return result; - png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); - if (!png_ptr) { - result = -4; - goto exit; - } - - info_ptr = png_create_info_struct(png_ptr); - if (!info_ptr) { - result = -5; - goto exit; - } - - if (setjmp(png_jmpbuf(png_ptr))) { - result = -6; - goto exit; - } - - png_init_io(png_ptr, fp); - png_set_sig_bytes(png_ptr, sizeof(header)); - png_read_info(png_ptr, info_ptr); - - int color_type, bit_depth; - size_t width, height; - png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, - &color_type, NULL, NULL, NULL); - int channels = png_get_channels(png_ptr, info_ptr); - - if (!(bit_depth == 8 && - (channels == 1 && color_type == PNG_COLOR_TYPE_GRAY))) { - return -7; + if (channels != 1) { + result = -7; goto exit; } unsigned char* row = malloc(width); - int y; + png_uint_32 y; for (y = 0; y < height; ++y) { png_read_row(png_ptr, row, NULL); int w = (row[1] << 8) | row[0]; int h = (row[3] << 8) | row[2]; int len = row[4]; - char* loc = row+5; + char* loc = (char*)row+5; - if (y+1+h >= height || matches_locale(loc)) { + if (y+1+h >= height || matches_locale(loc, locale)) { printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y); - surface = malloc(sizeof(GGLSurface)); + surface = malloc_surface(w*h); if (surface == NULL) { result = -8; goto exit; } - unsigned char* pData = malloc(w*h); - - surface->version = sizeof(GGLSurface); surface->width = w; surface->height = h; - surface->stride = w; /* Yes, pixels, not bytes */ - surface->data = pData; - surface->format = GGL_PIXEL_FORMAT_A_8; + surface->row_bytes = w; + surface->pixel_bytes = 1; int i; for (i = 0; i < h; ++i, ++y) { png_read_row(png_ptr, row, NULL); - memcpy(pData + i*w, row, w); + memcpy(surface->data + i*w, row, w); } *pSurface = (gr_surface) surface; @@ -312,21 +434,10 @@ int res_create_localized_surface(const char* name, gr_surface* pSurface) { exit: png_destroy_read_struct(&png_ptr, &info_ptr, NULL); - - if (fp != NULL) { - fclose(fp); - } - if (result < 0) { - if (surface) { - free(surface); - } - } + if (result < 0 && surface != NULL) free(surface); return result; } void res_free_surface(gr_surface surface) { - GGLSurface* pSurface = (GGLSurface*) surface; - if (pSurface) { - free(pSurface); - } + free(surface); } diff --git a/minzip/SysUtil.c b/minzip/SysUtil.c index c046a8cf2..ac6f5c33f 100644 --- a/minzip/SysUtil.c +++ b/minzip/SysUtil.c @@ -103,7 +103,7 @@ static int sysMapBlockFile(FILE* mapf, MemMapping* pMap) } } - if (fscanf(mapf, "%d %d\n%d\n", &size, &blksize, &range_count) != 3) { + if (fscanf(mapf, "%zu %u\n%u\n", &size, &blksize, &range_count) != 3) { LOGW("failed to parse block map header\n"); return -1; } diff --git a/mtdutils/flash_image.c b/mtdutils/flash_image.c index a39d60001..5657dfc82 100644 --- a/mtdutils/flash_image.c +++ b/mtdutils/flash_image.c @@ -24,6 +24,9 @@ #include "cutils/log.h" #include "mtdutils.h" +#ifdef LOG_TAG +#undef LOG_TAG +#endif #define LOG_TAG "flash_image" #define HEADER_SIZE 2048 // size of header to compare for equality diff --git a/recovery.cpp b/recovery.cpp index db35f1e97..8d3731540 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -57,6 +57,7 @@ static const struct option OPTIONS[] = { { "just_exit", no_argument, NULL, 'x' }, { "locale", required_argument, NULL, 'l' }, { "stages", required_argument, NULL, 'g' }, + { "shutdown_after", no_argument, NULL, 'p' }, { NULL, 0, NULL, 0 }, }; @@ -177,11 +178,11 @@ get_args(int *argc, char ***argv) { stage = strndup(boot.stage, sizeof(boot.stage)); if (boot.command[0] != 0 && boot.command[0] != 255) { - LOGI("Boot command: %.*s\n", sizeof(boot.command), boot.command); + LOGI("Boot command: %.*s\n", (int)sizeof(boot.command), boot.command); } if (boot.status[0] != 0 && boot.status[0] != 255) { - LOGI("Boot status: %.*s\n", sizeof(boot.status), boot.status); + LOGI("Boot status: %.*s\n", (int)sizeof(boot.status), boot.status); } // --- if arguments weren't supplied, look in the bootloader control block @@ -945,16 +946,15 @@ main(int argc, char **argv) { rotate_last_logs(10); get_args(&argc, &argv); - int previous_runs = 0; const char *send_intent = NULL; const char *update_package = NULL; int wipe_data = 0, wipe_cache = 0, show_text = 0; bool just_exit = false; + bool shutdown_after = false; int arg; while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) { switch (arg) { - case 'p': previous_runs = atoi(optarg); break; case 's': send_intent = optarg; break; case 'u': update_package = optarg; break; case 'w': wipe_data = wipe_cache = 1; break; @@ -970,6 +970,7 @@ main(int argc, char **argv) { } break; } + case 'p': shutdown_after = true; break; case '?': LOGE("Invalid command argument\n"); continue; @@ -986,6 +987,7 @@ main(int argc, char **argv) { ui = device->GetUI(); gCurrentUI = ui; + ui->SetLocale(locale); ui->Init(); int st_cur, st_max; @@ -993,7 +995,6 @@ main(int argc, char **argv) { ui->SetStage(st_cur, st_max); } - ui->SetLocale(locale); ui->SetBackground(RecoveryUI::NONE); if (show_text) ui->ShowText(true); @@ -1079,7 +1080,12 @@ main(int argc, char **argv) { // Otherwise, get ready to boot the main system... finish_recovery(send_intent); - ui->Print("Rebooting...\n"); - property_set(ANDROID_RB_PROPERTY, "reboot,"); + if (shutdown_after) { + ui->Print("Shutting down...\n"); + property_set(ANDROID_RB_PROPERTY, "shutdown,"); + } else { + ui->Print("Rebooting...\n"); + property_set(ANDROID_RB_PROPERTY, "reboot,"); + } return EXIT_SUCCESS; } diff --git a/res/images/icon_installing.png b/res/images/icon_installing.png Binary files differindex 571eb8b0f..c2c020162 100644 --- a/res/images/icon_installing.png +++ b/res/images/icon_installing.png diff --git a/res/images/icon_installing_overlay01.png b/res/images/icon_installing_overlay01.png Binary files differdeleted file mode 100644 index e762d6cbe..000000000 --- a/res/images/icon_installing_overlay01.png +++ /dev/null diff --git a/res/images/icon_installing_overlay02.png b/res/images/icon_installing_overlay02.png Binary files differdeleted file mode 100644 index f7a853017..000000000 --- a/res/images/icon_installing_overlay02.png +++ /dev/null diff --git a/res/images/icon_installing_overlay03.png b/res/images/icon_installing_overlay03.png Binary files differdeleted file mode 100644 index 1a1d738e4..000000000 --- a/res/images/icon_installing_overlay03.png +++ /dev/null diff --git a/res/images/icon_installing_overlay04.png b/res/images/icon_installing_overlay04.png Binary files differdeleted file mode 100644 index a74903d33..000000000 --- a/res/images/icon_installing_overlay04.png +++ /dev/null diff --git a/res/images/icon_installing_overlay05.png b/res/images/icon_installing_overlay05.png Binary files differdeleted file mode 100644 index d17bdc006..000000000 --- a/res/images/icon_installing_overlay05.png +++ /dev/null diff --git a/res/images/icon_installing_overlay06.png b/res/images/icon_installing_overlay06.png Binary files differdeleted file mode 100644 index 1200b75cb..000000000 --- a/res/images/icon_installing_overlay06.png +++ /dev/null diff --git a/res/images/icon_installing_overlay07.png b/res/images/icon_installing_overlay07.png Binary files differdeleted file mode 100644 index 3838a85ad..000000000 --- a/res/images/icon_installing_overlay07.png +++ /dev/null diff --git a/res/images/indeterminate01.png b/res/images/indeterminate01.png Binary files differdeleted file mode 100644 index 933528d6d..000000000 --- a/res/images/indeterminate01.png +++ /dev/null diff --git a/res/images/indeterminate02.png b/res/images/indeterminate02.png Binary files differdeleted file mode 100644 index d760e2bdd..000000000 --- a/res/images/indeterminate02.png +++ /dev/null diff --git a/res/images/indeterminate03.png b/res/images/indeterminate03.png Binary files differdeleted file mode 100644 index 0e97399d1..000000000 --- a/res/images/indeterminate03.png +++ /dev/null diff --git a/res/images/indeterminate04.png b/res/images/indeterminate04.png Binary files differdeleted file mode 100644 index c7d5b4e04..000000000 --- a/res/images/indeterminate04.png +++ /dev/null diff --git a/res/images/indeterminate05.png b/res/images/indeterminate05.png Binary files differdeleted file mode 100644 index d6fb2a032..000000000 --- a/res/images/indeterminate05.png +++ /dev/null diff --git a/res/images/indeterminate06.png b/res/images/indeterminate06.png Binary files differdeleted file mode 100644 index 44867619f..000000000 --- a/res/images/indeterminate06.png +++ /dev/null diff --git a/screen_ui.cpp b/screen_ui.cpp index fd1a6c7fa..af58643dc 100644 --- a/screen_ui.cpp +++ b/screen_ui.cpp @@ -52,6 +52,7 @@ static double now() { ScreenRecoveryUI::ScreenRecoveryUI() : currentIcon(NONE), installingFrame(0), + locale(NULL), rtl_locale(false), progressBarType(EMPTY), progressScopeStart(0), @@ -69,19 +70,8 @@ ScreenRecoveryUI::ScreenRecoveryUI() : menu_top(0), menu_items(0), menu_sel(0), - - // These values are correct for the default image resources - // provided with the android platform. Devices which use - // different resources should have a subclass of ScreenRecoveryUI - // that overrides Init() to set these values appropriately and - // then call the superclass Init(). animation_fps(20), - indeterminate_frames(6), - installing_frames(7), - install_overlay_offset_x(13), - install_overlay_offset_y(190), - overlay_offset_x(-1), - overlay_offset_y(-1), + installing_frames(-1), stage(-1), max_stage(-1) { @@ -92,30 +82,19 @@ ScreenRecoveryUI::ScreenRecoveryUI() : self = this; } -// Draw the given frame over the installation overlay animation. The -// background is not cleared or draw with the base icon first; we -// assume that the frame already contains some other frame of the -// animation. Does nothing if no overlay animation is defined. -// Should only be called with updateMutex locked. -void ScreenRecoveryUI::draw_install_overlay_locked(int frame) { - if (installationOverlay == NULL || overlay_offset_x < 0) return; - gr_surface surface = installationOverlay[frame]; - int iconWidth = gr_get_width(surface); - int iconHeight = gr_get_height(surface); - gr_blit(surface, 0, 0, iconWidth, iconHeight, - overlay_offset_x, overlay_offset_y); -} - // Clear the screen and draw the currently selected background icon (if any). // Should only be called with updateMutex locked. void ScreenRecoveryUI::draw_background_locked(Icon icon) { pagesIdentical = false; gr_color(0, 0, 0, 255); - gr_fill(0, 0, gr_fb_width(), gr_fb_height()); + gr_clear(); if (icon) { gr_surface surface = backgroundIcon[icon]; + if (icon == INSTALLING_UPDATE || icon == ERASING) { + surface = installation[installingFrame]; + } gr_surface text_surface = backgroundText[icon]; int iconWidth = gr_get_width(surface); @@ -126,8 +105,8 @@ void ScreenRecoveryUI::draw_background_locked(Icon icon) int sh = (max_stage >= 0) ? stageHeight : 0; - int iconX = (gr_fb_width() - iconWidth) / 2; - int iconY = (gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2; + iconX = (gr_fb_width() - iconWidth) / 2; + iconY = (gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2; int textX = (gr_fb_width() - textWidth) / 2; int textY = ((gr_fb_height() - (iconHeight+textHeight+40+sh)) / 2) + iconHeight + 40; @@ -144,10 +123,6 @@ void ScreenRecoveryUI::draw_background_locked(Icon icon) } } - if (icon == INSTALLING_UPDATE || icon == ERASING) { - draw_install_overlay_locked(installingFrame); - } - gr_color(255, 255, 255, 255); gr_texticon(textX, textY, text_surface); } @@ -160,7 +135,8 @@ void ScreenRecoveryUI::draw_progress_locked() if (currentIcon == ERROR) return; if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) { - draw_install_overlay_locked(installingFrame); + gr_surface icon = installation[installingFrame]; + gr_blit(icon, 0, 0, gr_get_width(icon), gr_get_height(icon), iconX, iconY); } if (progressBarType != EMPTY) { @@ -197,18 +173,6 @@ void ScreenRecoveryUI::draw_progress_locked() } } } - - if (progressBarType == INDETERMINATE) { - static int frame = 0; - gr_blit(progressBarIndeterminate[frame], 0, 0, width, height, dx, dy); - // in RTL locales, we run the animation backwards, which - // makes the spinner spin the other way. - if (rtl_locale) { - frame = (frame + indeterminate_frames - 1) % indeterminate_frames; - } else { - frame = (frame + 1) % indeterminate_frames; - } - } } } @@ -240,12 +204,12 @@ void ScreenRecoveryUI::SetColor(UIElement e) { // Should only be called with updateMutex locked. void ScreenRecoveryUI::draw_screen_locked() { - draw_background_locked(currentIcon); - draw_progress_locked(); - - if (show_text) { - SetColor(TEXT_FILL); - gr_fill(0, 0, gr_fb_width(), gr_fb_height()); + if (!show_text) { + draw_background_locked(currentIcon); + draw_progress_locked(); + } else { + gr_color(0, 0, 0, 255); + gr_clear(); int y = 0; int i = 0; @@ -335,12 +299,6 @@ void ScreenRecoveryUI::progress_loop() { redraw = 1; } - // update the progress bar animation, if active - // skip this if we have a text overlay (too expensive to update) - if (progressBarType == INDETERMINATE && !show_text) { - redraw = 1; - } - // move the progress bar forward on timed intervals, if configured int duration = progressScopeDuration; if (progressBarType == DETERMINATE && duration > 0) { @@ -365,14 +323,21 @@ void ScreenRecoveryUI::progress_loop() { } void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) { - int result = res_create_surface(filename, surface); + int result = res_create_display_surface(filename, surface); + if (result < 0) { + LOGE("missing bitmap %s\n(Code %d)\n", filename, result); + } +} + +void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) { + int result = res_create_multi_display_surface(filename, frames, surface); if (result < 0) { LOGE("missing bitmap %s\n(Code %d)\n", filename, result); } } void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* surface) { - int result = res_create_localized_surface(filename, surface); + int result = res_create_localized_alpha_surface(filename, locale, surface); if (result < 0) { LOGE("missing bitmap %s\n(Code %d)\n", filename, result); } @@ -393,7 +358,8 @@ void ScreenRecoveryUI::Init() if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1; backgroundIcon[NONE] = NULL; - LoadBitmap("icon_installing", &backgroundIcon[INSTALLING_UPDATE]); + LoadBitmapArray("icon_installing", &installing_frames, &installation); + backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : NULL; backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE]; LoadBitmap("icon_error", &backgroundIcon[ERROR]); backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR]; @@ -408,38 +374,14 @@ void ScreenRecoveryUI::Init() LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]); LoadLocalizedBitmap("error_text", &backgroundText[ERROR]); - int i; - - progressBarIndeterminate = (gr_surface*)malloc(indeterminate_frames * - sizeof(gr_surface)); - for (i = 0; i < indeterminate_frames; ++i) { - char filename[40]; - // "indeterminate01.png", "indeterminate02.png", ... - sprintf(filename, "indeterminate%02d", i+1); - LoadBitmap(filename, progressBarIndeterminate+i); - } - - if (installing_frames > 0) { - installationOverlay = (gr_surface*)malloc(installing_frames * - sizeof(gr_surface)); - for (i = 0; i < installing_frames; ++i) { - char filename[40]; - // "icon_installing_overlay01.png", - // "icon_installing_overlay02.png", ... - sprintf(filename, "icon_installing_overlay%02d", i+1); - LoadBitmap(filename, installationOverlay+i); - } - } else { - installationOverlay = NULL; - } - pthread_create(&progress_t, NULL, progress_thread, NULL); RecoveryUI::Init(); } -void ScreenRecoveryUI::SetLocale(const char* locale) { - if (locale) { +void ScreenRecoveryUI::SetLocale(const char* new_locale) { + if (new_locale) { + this->locale = new_locale; char* lang = strdup(locale); for (char* p = lang; *p; ++p) { if (*p == '_') { @@ -458,6 +400,8 @@ void ScreenRecoveryUI::SetLocale(const char* locale) { rtl_locale = true; } free(lang); + } else { + new_locale = NULL; } } @@ -465,19 +409,6 @@ void ScreenRecoveryUI::SetBackground(Icon icon) { pthread_mutex_lock(&updateMutex); - // Adjust the offset to account for the positioning of the - // base image on the screen. - if (backgroundIcon[icon] != NULL) { - gr_surface bg = backgroundIcon[icon]; - gr_surface text = backgroundText[icon]; - overlay_offset_x = install_overlay_offset_x + (gr_fb_width() - gr_get_width(bg)) / 2; - overlay_offset_y = install_overlay_offset_y + - (gr_fb_height() - (gr_get_height(bg) + - gr_get_height(text) + - 40 + - ((max_stage >= 0) ? gr_get_height(stageMarkerEmpty) : 0))) / 2; - } - currentIcon = icon; update_screen_locked(); @@ -517,7 +448,7 @@ void ScreenRecoveryUI::SetProgress(float fraction) if (fraction > 1.0) fraction = 1.0; if (progressBarType == DETERMINATE && fraction > progress) { // Skip updates that aren't visibly different. - int width = gr_get_width(progressBarIndeterminate[0]); + int width = gr_get_width(progressBarEmpty); float scale = width * progressScopeSize; if ((int) (progress * scale) != (int) (fraction * scale)) { progress = fraction; diff --git a/screen_ui.h b/screen_ui.h index 5c4366d28..92e4795b4 100644 --- a/screen_ui.h +++ b/screen_ui.h @@ -63,13 +63,13 @@ class ScreenRecoveryUI : public RecoveryUI { private: Icon currentIcon; int installingFrame; + const char* locale; bool rtl_locale; pthread_mutex_t updateMutex; gr_surface backgroundIcon[5]; gr_surface backgroundText[5]; - gr_surface *installationOverlay; - gr_surface *progressBarIndeterminate; + gr_surface *installation; gr_surface progressBarEmpty; gr_surface progressBarFill; gr_surface stageMarkerEmpty; @@ -101,12 +101,11 @@ class ScreenRecoveryUI : public RecoveryUI { pthread_t progress_t; int animation_fps; - int indeterminate_frames; int installing_frames; protected: - int install_overlay_offset_x, install_overlay_offset_y; private: - int overlay_offset_x, overlay_offset_y; + + int iconX, iconY; int stage, max_stage; @@ -120,6 +119,7 @@ class ScreenRecoveryUI : public RecoveryUI { void progress_loop(); void LoadBitmap(const char* filename, gr_surface* surface); + void LoadBitmapArray(const char* filename, int* frames, gr_surface** surface); void LoadLocalizedBitmap(const char* filename, gr_surface* surface); }; diff --git a/tools/ota/add-property-tag.c b/tools/ota/add-property-tag.c index 5277edd9c..aab30b2d0 100644 --- a/tools/ota/add-property-tag.c +++ b/tools/ota/add-property-tag.c @@ -57,9 +57,9 @@ void write_tagged(FILE *out, const char *line, const char *tag, int number) { const char *end = line + strlen(line); while (end > line && isspace(end[-1])) --end; if (number > 0) { - fprintf(out, "%.*s%s%d%s", end - line, line, tag, number, end); + fprintf(out, "%.*s%s%d%s", (int)(end - line), line, tag, number, end); } else { - fprintf(out, "%.*s%s%s", end - line, line, tag, end); + fprintf(out, "%.*s%s%s", (int)(end - line), line, tag, end); } } diff --git a/uncrypt/uncrypt.c b/uncrypt/uncrypt.c index 7c2d99477..24d1ffc2a 100644 --- a/uncrypt/uncrypt.c +++ b/uncrypt/uncrypt.c @@ -189,10 +189,10 @@ int produce_block_map(const char* path, const char* map_file, const char* blk_de return -1; } - printf(" block size: %ld bytes\n", sb.st_blksize); + printf(" block size: %ld bytes\n", (long)sb.st_blksize); int blocks = ((sb.st_size-1) / sb.st_blksize) + 1; - printf(" file size: %lld bytes, %d blocks\n", sb.st_size, blocks); + printf(" file size: %lld bytes, %d blocks\n", (long long)sb.st_size, blocks); int* ranges; int range_alloc = 1; @@ -201,7 +201,7 @@ int produce_block_map(const char* path, const char* map_file, const char* blk_de ranges[0] = -1; ranges[1] = -1; - fprintf(mapf, "%s\n%lld %lu\n", blk_dev, sb.st_size, sb.st_blksize); + fprintf(mapf, "%s\n%lld %lu\n", blk_dev, (long long)sb.st_size, (unsigned long)sb.st_blksize); unsigned char* buffers[WINDOW_SIZE]; int i; diff --git a/updater/install.c b/updater/install.c index ccafad9c2..53f5e48cb 100644 --- a/updater/install.c +++ b/updater/install.c @@ -46,9 +46,11 @@ #include "mtdutils/mtdutils.h" #include "updater.h" #include "syspatch.h" +#include "install.h" #ifdef USE_EXT4 #include "make_ext4fs.h" +#include "wipe.h" #endif // Take a sha-1 digest and return it as a newly-allocated hex string. @@ -428,6 +430,54 @@ Value* PackageExtractDirFn(const char* name, State* state, } +DontCareMap* ReadDontCareMapFromZip(ZipArchive* za, const char* path) { + const char* name = "ReadDontCareMapFromZip"; + + const ZipEntry* entry = mzFindZipEntry(za, path); + if (entry == NULL) { + printf("%s: no %s in package\n", name, path); + return NULL; + } + + size_t map_size = mzGetZipEntryUncompLen(entry); + char* map_data = malloc(map_size); + if (map_data == NULL) { + printf("%s: failed to allocate %zu bytes for %s\n", + name, map_size, path); + return NULL; + } + + if (!mzExtractZipEntryToBuffer(za, entry, (unsigned char*) map_data)) { + printf("%s: failed to read %s\n", name, path); + return NULL; + } + + char* p = map_data; + DontCareMap* map = (DontCareMap*) malloc(sizeof(DontCareMap)); + + map->block_size = strtoul(p, &p, 0); + if (map->block_size != 4096) { + printf("%s: unexpected block size %zu\n", name, map->block_size); + return NULL; + } + + map->region_count = strtoul(p, &p, 0); + map->regions = (int*) malloc(map->region_count * sizeof(int)); + + int i; + for (i = 0; i < map->region_count; ++i) { + map->regions[i] = strtoul(p, &p, 0); + } + + return map; +} + +bool MapWriter(const unsigned char* data, int dataLen, void* cookie) { + return write_with_map(data, dataLen, (MapState*) cookie) == dataLen; +} + +// package_extract_file(package_path, destination_path, map_path) +// or // package_extract_file(package_path, destination_path) // or // package_extract_file(package_path) @@ -435,19 +485,30 @@ Value* PackageExtractDirFn(const char* name, State* state, // function (the char* returned is actually a FileContents*). Value* PackageExtractFileFn(const char* name, State* state, int argc, Expr* argv[]) { - if (argc != 1 && argc != 2) { - return ErrorAbort(state, "%s() expects 1 or 2 args, got %d", + if (argc < 1 || argc > 3) { + return ErrorAbort(state, "%s() expects 1 or 2 or 3 args, got %d", name, argc); } bool success = false; - if (argc == 2) { - // The two-argument version extracts to a file. + if (argc >= 2) { + // The two-argument version extracts to a file; the three-arg + // version extracts to a file, skipping over regions in a + // don't care map. + + ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; char* zip_path; char* dest_path; - if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL; + char* map_path = NULL; + DontCareMap* map = NULL; + if (argc == 2) { + if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL; + } else { + if (ReadArgs(state, argv, 3, &zip_path, &dest_path, &map_path) < 0) return NULL; + map = ReadDontCareMapFromZip(za, map_path); + if (map == NULL) goto done2; + } - ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; const ZipEntry* entry = mzFindZipEntry(za, zip_path); if (entry == NULL) { printf("%s: no %s in package\n", name, zip_path); @@ -460,12 +521,26 @@ Value* PackageExtractFileFn(const char* name, State* state, name, dest_path, strerror(errno)); goto done2; } - success = mzExtractZipEntryToFile(za, entry, fileno(f)); + if (map) { + MapState state; + state.map = map; + state.cr = 0; + state.so_far = 0; + state.f = f; + success = mzProcessZipEntryContents(za, entry, MapWriter, &state); + } else { + success = mzExtractZipEntryToFile(za, entry, fileno(f)); + } fclose(f); done2: free(zip_path); free(dest_path); + free(map_path); + if (map) { + free(map->regions); + free(map); + } return StringValue(strdup(success ? "t" : "")); } else { // The one-argument version returns the contents of the file @@ -880,7 +955,7 @@ Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) { buffer = malloc(st.st_size+1); if (buffer == NULL) { - ErrorAbort(state, "%s: failed to alloc %lld bytes", name, st.st_size+1); + ErrorAbort(state, "%s: failed to alloc %lld bytes", name, (long long)st.st_size+1); goto done; } @@ -893,7 +968,7 @@ Value* FileGetPropFn(const char* name, State* state, int argc, Expr* argv[]) { if (fread(buffer, 1, st.st_size, f) != st.st_size) { ErrorAbort(state, "%s: failed to read %lld bytes from %s", - name, st.st_size+1, filename); + name, (long long)st.st_size+1, filename); fclose(f); goto done; } @@ -1067,23 +1142,48 @@ Value* ApplyPatchSpaceFn(const char* name, State* state, return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t")); } -// syspatch(file, size, tgt_sha1, init_sha1, patch) +bool CheckMappedFileSha1(FILE* f, DontCareMap* map, uint8_t* intended_digest) { + MapState state; + + state.f = f; + state.so_far = 0; + state.cr = 0; + state.map = map; + + SHA_CTX ctx; + SHA_init(&ctx); + + unsigned char buffer[32173]; + size_t bytes_read; + + while ((bytes_read = read_with_map(buffer, sizeof(buffer), &state)) > 0) { + SHA_update(&ctx, buffer, bytes_read); + } + const uint8_t* digest = SHA_final(&ctx); + + return memcmp(digest, intended_digest, SHA_DIGEST_SIZE) == 0; +} + + +// syspatch(file, tgt_mapfile, tgt_sha1, init_mapfile, init_sha1, patch) Value* SysPatchFn(const char* name, State* state, int argc, Expr* argv[]) { - if (argc != 5) { - return ErrorAbort(state, "%s(): expected 5 args, got %d", name, argc); + if (argc != 6) { + return ErrorAbort(state, "%s(): expected 6 args, got %d", name, argc); } char* filename; - char* filename_size_str; + char* target_mapfilename; char* target_sha1; + char* init_mapfilename; char* init_sha1; char* patch_filename; uint8_t target_digest[SHA_DIGEST_SIZE]; uint8_t init_digest[SHA_DIGEST_SIZE]; - if (ReadArgs(state, argv, 5, &filename, &filename_size_str, - &target_sha1, &init_sha1, &patch_filename) < 0) { + if (ReadArgs(state, argv, 6, &filename, + &target_mapfilename, &target_sha1, + &init_mapfilename, &init_sha1, &patch_filename) < 0) { return NULL; } @@ -1096,68 +1196,56 @@ Value* SysPatchFn(const char* name, State* state, int argc, Expr* argv[]) { memset(init_digest, 0, SHA_DIGEST_SIZE); } - size_t len = strtoull(filename_size_str, NULL, 0); - - SHA_CTX ctx; - SHA_init(&ctx); + ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; FILE* src = fopen(filename, "r"); - size_t pos = 0; - unsigned char buffer[4096]; - while (pos < len) { - size_t to_read = len - pos; - if (to_read > sizeof(buffer)) to_read = sizeof(buffer); - size_t read = fread(buffer, 1, to_read, src); - if (read <= 0) { - printf("%s(): short read after %zu bytes\n", name, pos); - break; - } - SHA_update(&ctx, buffer, read); - pos += read; - } - rewind(src); - const uint8_t* digest = SHA_final(&ctx); - - const char* hexdigest = PrintSha1(digest); - printf(" system partition sha1 = %s\n", hexdigest); - if (memcmp(digest, target_digest, SHA_DIGEST_SIZE) == 0) { - printf("%s(): %s is already target\n", name, filename); - fclose(src); - goto done; - } + DontCareMap* init_map = ReadDontCareMapFromZip(za, init_mapfilename); + if (init_map == NULL) return ErrorAbort(state, "%s(): failed to read init map\n", name); + DontCareMap* target_map = ReadDontCareMapFromZip(za, target_mapfilename); + if (target_map == NULL) return ErrorAbort(state, "%s(): failed to read target map\n", name); - if (memcmp(digest, init_digest, SHA_DIGEST_SIZE) != 0) { - return ErrorAbort(state, "%s(): %s in unknown state\n", name, filename); - } + if (CheckMappedFileSha1(src, init_map, init_digest)) { + // If the partition contents match the init_digest, then we need to apply the patch. - ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; + rewind(src); - const ZipEntry* entry = mzFindZipEntry(za, patch_filename); - if (entry == NULL) { - return ErrorAbort(state, "%s(): no %s in package\n", name, patch_filename); - } + const ZipEntry* entry = mzFindZipEntry(za, patch_filename); + if (entry == NULL) { + return ErrorAbort(state, "%s(): no %s in package\n", name, patch_filename); + } - unsigned char* patch_data; - size_t patch_len; - if (!mzGetStoredEntry(za, entry, &patch_data, &patch_len)) { - return ErrorAbort(state, "%s(): failed to get %s entry\n", name, patch_filename); - } + unsigned char* patch_data; + size_t patch_len; + if (!mzGetStoredEntry(za, entry, &patch_data, &patch_len)) { + return ErrorAbort(state, "%s(): failed to get %s entry\n", name, patch_filename); + } - FILE* tgt = fopen(filename, "r+"); + FILE* tgt = fopen(filename, "r+"); - int ret = syspatch(src, patch_data, patch_len, tgt); + int ret = syspatch(src, init_map, patch_data, patch_len, tgt, target_map); - fclose(src); - fclose(tgt); + fclose(src); + fclose(tgt); - if (ret != 0) { - return ErrorAbort(state, "%s(): patching failed\n", name); + if (ret != 0) { + return ErrorAbort(state, "%s(): patching failed\n", name); + } + } else { + rewind(src); + if (CheckMappedFileSha1(src, target_map, target_digest)) { + // If the partition contents match the target already, we + // don't need to do anything. + printf("%s: output is already target\n", name); + } else { + return ErrorAbort(state, "%s(): %s in unknown state\n", name, filename); + } } - done: - free(filename_size_str); + done: free(target_sha1); + free(target_mapfilename); free(init_sha1); + free(init_mapfilename); free(patch_filename); return StringValue(filename); @@ -1516,7 +1604,7 @@ Value* SetStageFn(const char* name, State* state, int argc, Expr* argv[]) { // Return the value most recently saved with SetStageFn. The argument // is the block device for the misc partition. Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) { - if (argc != 2) { + if (argc != 1) { return ErrorAbort(state, "%s() expects 1 arg, got %d", name, argc); } @@ -1533,6 +1621,27 @@ Value* GetStageFn(const char* name, State* state, int argc, Expr* argv[]) { return StringValue(strdup(buffer)); } +Value* WipeBlockDeviceFn(const char* name, State* state, int argc, Expr* argv[]) { + if (argc != 2) { + return ErrorAbort(state, "%s() expects 2 args, got %d", name, argc); + } + + char* filename; + char* len_str; + if (ReadArgs(state, argv, 2, &filename, &len_str) < 0) return NULL; + + size_t len = strtoull(len_str, NULL, 0); + int fd = open(filename, O_WRONLY, 0644); + int success = wipe_block_device(fd, len); + + free(filename); + free(len_str); + + close(fd); + + return StringValue(strdup(success ? "t" : "")); +} + void RegisterInstallFunctions() { RegisterFunction("mount", MountFn); RegisterFunction("is_mounted", IsMountedFn); @@ -1566,6 +1675,7 @@ void RegisterInstallFunctions() { RegisterFunction("apply_patch_check", ApplyPatchCheckFn); RegisterFunction("apply_patch_space", ApplyPatchSpaceFn); + RegisterFunction("wipe_block_device", WipeBlockDeviceFn); RegisterFunction("syspatch", SysPatchFn); RegisterFunction("read_file", ReadFileFn); diff --git a/verifier.cpp b/verifier.cpp index 55d58ee22..eeff95a59 100644 --- a/verifier.cpp +++ b/verifier.cpp @@ -140,7 +140,7 @@ int verify_file(unsigned char* addr, size_t length, size_t comment_size = footer[4] + (footer[5] << 8); size_t signature_start = footer[0] + (footer[1] << 8); - LOGI("comment is %d bytes; signature %d bytes from end\n", + LOGI("comment is %zu bytes; signature %zu bytes from end\n", comment_size, signature_start); if (signature_start <= FOOTER_SIZE) { @@ -252,24 +252,24 @@ int verify_file(unsigned char* addr, size_t length, if (pKeys[i].key_type == Certificate::RSA) { if (sig_der_length < RSANUMBYTES) { // "signature" block isn't big enough to contain an RSA block. - LOGI("signature is too short for RSA key %d\n", i); + LOGI("signature is too short for RSA key %zu\n", i); continue; } if (!RSA_verify(pKeys[i].rsa, sig_der, RSANUMBYTES, hash, pKeys[i].hash_len)) { - LOGI("failed to verify against RSA key %d\n", i); + LOGI("failed to verify against RSA key %zu\n", i); continue; } - LOGI("whole-file signature verified against RSA key %d\n", i); + LOGI("whole-file signature verified against RSA key %zu\n", i); free(sig_der); return VERIFY_SUCCESS; } else if (pKeys[i].key_type == Certificate::EC && pKeys[i].hash_len == SHA256_DIGEST_SIZE) { p256_int r, s; if (!dsa_sig_unpack(sig_der, sig_der_length, &r, &s)) { - LOGI("Not a DSA signature block for EC key %d\n", i); + LOGI("Not a DSA signature block for EC key %zu\n", i); continue; } @@ -277,11 +277,11 @@ int verify_file(unsigned char* addr, size_t length, p256_from_bin(hash, &p256_hash); if (!p256_ecdsa_verify(&(pKeys[i].ec->x), &(pKeys[i].ec->y), &p256_hash, &r, &s)) { - LOGI("failed to verify against EC key %d\n", i); + LOGI("failed to verify against EC key %zu\n", i); continue; } - LOGI("whole-file signature verified against EC key %d\n", i); + LOGI("whole-file signature verified against EC key %zu\n", i); free(sig_der); return VERIFY_SUCCESS; } else { |