Merge "otafault: Add the missing #include <string>."
diff --git a/Android.bp b/Android.bp
index 99ca3a4..4c66c42 100644
--- a/Android.bp
+++ b/Android.bp
@@ -1,5 +1,6 @@
 subdirs = [
     "bootloader_message",
+    "edify",
     "otafault",
     "otautil",
 ]
diff --git a/Android.mk b/Android.mk
index ac72bac..5fe37fd 100644
--- a/Android.mk
+++ b/Android.mk
@@ -229,6 +229,7 @@
     asn1_decoder.cpp \
     verifier.cpp
 LOCAL_STATIC_LIBRARIES := \
+    libotautil \
     libcrypto_utils \
     libcrypto \
     libbase
@@ -261,7 +262,6 @@
 include \
     $(LOCAL_PATH)/applypatch/Android.mk \
     $(LOCAL_PATH)/boot_control/Android.mk \
-    $(LOCAL_PATH)/edify/Android.mk \
     $(LOCAL_PATH)/minadbd/Android.mk \
     $(LOCAL_PATH)/minui/Android.mk \
     $(LOCAL_PATH)/tests/Android.mk \
diff --git a/applypatch/Android.mk b/applypatch/Android.mk
index f5dda2b..8fac2ff 100644
--- a/applypatch/Android.mk
+++ b/applypatch/Android.mk
@@ -25,11 +25,13 @@
 LOCAL_MODULE := libapplypatch
 LOCAL_MODULE_TAGS := eng
 LOCAL_C_INCLUDES := \
-    $(LOCAL_PATH)/include \
-    bootable/recovery
-LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+    $(LOCAL_PATH)/include
+LOCAL_EXPORT_C_INCLUDE_DIRS := \
+    $(LOCAL_PATH)/include
 LOCAL_STATIC_LIBRARIES := \
+    libedify \
     libotafault \
+    libotautil \
     libbase \
     libcrypto \
     libbspatch \
@@ -49,10 +51,12 @@
     imgpatch.cpp
 LOCAL_MODULE := libimgpatch
 LOCAL_C_INCLUDES := \
-    $(LOCAL_PATH)/include \
-    bootable/recovery
-LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+    $(LOCAL_PATH)/include
+LOCAL_EXPORT_C_INCLUDE_DIRS := \
+    $(LOCAL_PATH)/include
 LOCAL_STATIC_LIBRARIES := \
+    libedify \
+    libotautil \
     libcrypto \
     libbspatch \
     libbase \
@@ -73,10 +77,12 @@
 LOCAL_MODULE := libimgpatch
 LOCAL_MODULE_HOST_OS := linux
 LOCAL_C_INCLUDES := \
-    $(LOCAL_PATH)/include \
-    bootable/recovery
-LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
+    $(LOCAL_PATH)/include
+LOCAL_EXPORT_C_INCLUDE_DIRS := \
+    $(LOCAL_PATH)/include
 LOCAL_STATIC_LIBRARIES := \
+    libedify \
+    libotautil \
     libcrypto \
     libbspatch \
     libbase \
@@ -94,9 +100,9 @@
 LOCAL_SRC_FILES := \
     applypatch_modes.cpp
 LOCAL_MODULE := libapplypatch_modes
-LOCAL_C_INCLUDES := bootable/recovery
 LOCAL_STATIC_LIBRARIES := \
     libapplypatch \
+    libotautil \
     libbase \
     libedify \
     libcrypto
@@ -108,22 +114,20 @@
 include $(CLEAR_VARS)
 LOCAL_SRC_FILES := applypatch_main.cpp
 LOCAL_MODULE := applypatch
-LOCAL_C_INCLUDES := bootable/recovery
 LOCAL_STATIC_LIBRARIES := \
     libapplypatch_modes \
     libapplypatch \
     libedify \
     libotafault \
-    libbspatch \
-    libbase \
-    libziparchive \
-    liblog \
-    libcrypto \
-    libbz
+    libotautil \
+    libbspatch
 LOCAL_SHARED_LIBRARIES := \
     libbase \
+    libbz \
+    libcrypto \
+    liblog \
     libz \
-    libcutils
+    libziparchive
 LOCAL_CFLAGS := -Wall -Werror
 include $(BUILD_EXECUTABLE)
 
@@ -137,6 +141,7 @@
     -DZLIB_CONST
 
 libimgdiff_static_libraries := \
+    libotautil \
     libbsdiff \
     libdivsufsort \
     libdivsufsort64 \
@@ -157,8 +162,7 @@
 LOCAL_STATIC_LIBRARIES := \
     $(libimgdiff_static_libraries)
 LOCAL_C_INCLUDES := \
-    $(LOCAL_PATH)/include \
-    bootable/recovery
+    $(LOCAL_PATH)/include
 LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
 include $(BUILD_STATIC_LIBRARY)
 
@@ -173,8 +177,7 @@
 LOCAL_STATIC_LIBRARIES := \
     $(libimgdiff_static_libraries)
 LOCAL_C_INCLUDES := \
-    $(LOCAL_PATH)/include \
-    bootable/recovery
+    $(LOCAL_PATH)/include
 LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
 include $(BUILD_HOST_STATIC_LIBRARY)
 
@@ -189,6 +192,5 @@
     $(libimgdiff_static_libraries) \
     libbz
 LOCAL_C_INCLUDES := \
-    $(LOCAL_PATH)/include \
-    bootable/recovery
+    $(LOCAL_PATH)/include
 include $(BUILD_HOST_EXECUTABLE)
diff --git a/applypatch/applypatch.cpp b/applypatch/applypatch.cpp
index 729d2a9..c8b75df 100644
--- a/applypatch/applypatch.cpp
+++ b/applypatch/applypatch.cpp
@@ -40,7 +40,7 @@
 
 #include "edify/expr.h"
 #include "otafault/ota_io.h"
-#include "print_sha1.h"
+#include "otautil/print_sha1.h"
 
 static int LoadPartitionContents(const std::string& filename, FileContents* file);
 static size_t FileSink(const unsigned char* data, size_t len, int fd);
diff --git a/applypatch/bspatch.cpp b/applypatch/bspatch.cpp
index 65ee614..c291464 100644
--- a/applypatch/bspatch.cpp
+++ b/applypatch/bspatch.cpp
@@ -30,7 +30,8 @@
 #include <openssl/sha.h>
 
 #include "applypatch/applypatch.h"
-#include "print_sha1.h"
+#include "edify/expr.h"
+#include "otautil/print_sha1.h"
 
 void ShowBSDiffLicense() {
     puts("The bsdiff library used herein is:\n"
@@ -91,4 +92,4 @@
     }
   }
   return result;
-}
\ No newline at end of file
+}
diff --git a/applypatch/imgdiff.cpp b/applypatch/imgdiff.cpp
index c887a85..3a61a7d 100644
--- a/applypatch/imgdiff.cpp
+++ b/applypatch/imgdiff.cpp
@@ -166,7 +166,7 @@
 #include <zlib.h>
 
 #include "applypatch/imgdiff_image.h"
-#include "rangeset.h"
+#include "otautil/rangeset.h"
 
 using android::base::get_unaligned;
 
diff --git a/applypatch/imgpatch.cpp b/applypatch/imgpatch.cpp
index df75f98..7a43ddb 100644
--- a/applypatch/imgpatch.cpp
+++ b/applypatch/imgpatch.cpp
@@ -37,6 +37,8 @@
 #include <openssl/sha.h>
 #include <zlib.h>
 
+#include "edify/expr.h"
+
 static inline int64_t Read8(const void *address) {
   return android::base::get_unaligned<int64_t>(address);
 }
diff --git a/applypatch/include/applypatch/applypatch.h b/applypatch/include/applypatch/applypatch.h
index 581360e..2a3b3ef 100644
--- a/applypatch/include/applypatch/applypatch.h
+++ b/applypatch/include/applypatch/applypatch.h
@@ -27,7 +27,8 @@
 
 #include <openssl/sha.h>
 
-#include "edify/expr.h"
+// Forward declaration to avoid including "edify/expr.h" in the header.
+struct Value;
 
 struct FileContents {
   uint8_t sha1[SHA_DIGEST_LENGTH];
diff --git a/applypatch/include/applypatch/imgdiff_image.h b/applypatch/include/applypatch/imgdiff_image.h
index 491043d..3d29547 100644
--- a/applypatch/include/applypatch/imgdiff_image.h
+++ b/applypatch/include/applypatch/imgdiff_image.h
@@ -29,7 +29,7 @@
 #include <zlib.h>
 
 #include "imgdiff.h"
-#include "rangeset.h"
+#include "otautil/rangeset.h"
 
 class ImageChunk {
  public:
diff --git a/edify/Android.bp b/edify/Android.bp
new file mode 100644
index 0000000..42947eb
--- /dev/null
+++ b/edify/Android.bp
@@ -0,0 +1,45 @@
+// Copyright (C) 2017 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.
+
+cc_library_static {
+    name: "libedify",
+
+    host_supported: true,
+
+    srcs: [
+        "expr.cpp",
+        "lexer.ll",
+        "parser.yy",
+    ],
+
+    cflags: [
+        "-Wall",
+        "-Werror",
+        "-Wno-deprecated-register",
+        "-Wno-unused-parameter",
+    ],
+
+    export_include_dirs: [
+        "include",
+    ],
+
+    local_include_dirs: [
+        "include",
+    ],
+
+    static_libs: [
+        "libbase",
+        "libotautil",
+    ],
+}
diff --git a/edify/Android.mk b/edify/Android.mk
deleted file mode 100644
index baf4dd2..0000000
--- a/edify/Android.mk
+++ /dev/null
@@ -1,56 +0,0 @@
-# Copyright 2009 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.
-
-LOCAL_PATH := $(call my-dir)
-
-edify_src_files := \
-    lexer.ll \
-    parser.yy \
-    expr.cpp
-
-#
-# Build the host-side command line tool (host executable)
-#
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := \
-    $(edify_src_files) \
-    edify_parser.cpp
-
-LOCAL_CFLAGS := -Wall -Werror
-LOCAL_CPPFLAGS := -g -O0
-LOCAL_MODULE := edify_parser
-LOCAL_YACCFLAGS := -v
-LOCAL_CPPFLAGS += -Wno-unused-parameter
-LOCAL_CPPFLAGS += -Wno-deprecated-register
-LOCAL_C_INCLUDES += $(LOCAL_PATH)/..
-LOCAL_STATIC_LIBRARIES += libbase
-
-include $(BUILD_HOST_EXECUTABLE)
-
-#
-# Build the device-side library (static library)
-#
-include $(CLEAR_VARS)
-
-LOCAL_SRC_FILES := $(edify_src_files)
-
-LOCAL_CFLAGS := -Wall -Werror
-LOCAL_CPPFLAGS := -Wno-unused-parameter
-LOCAL_CPPFLAGS += -Wno-deprecated-register
-LOCAL_MODULE := libedify
-LOCAL_C_INCLUDES += $(LOCAL_PATH)/..
-LOCAL_STATIC_LIBRARIES += libbase
-
-include $(BUILD_STATIC_LIBRARY)
diff --git a/edify/edify_parser.cpp b/edify/edify_parser.cpp
deleted file mode 100644
index f1b5628..0000000
--- a/edify/edify_parser.cpp
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
- * Copyright (C) 2009 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.
- */
-
-/**
- * This is a host-side tool for validating a given edify script file.
- *
- * We used to have edify test cases here, which have been moved to
- * tests/component/edify_test.cpp.
- *
- * Caveat: It doesn't recognize functions defined through updater, which
- * makes the tool less useful. We should either extend the tool or remove it.
- */
-
-#include <errno.h>
-#include <stdio.h>
-
-#include <memory>
-#include <string>
-
-#include <android-base/file.h>
-
-#include "expr.h"
-
-static void ExprDump(int depth, const std::unique_ptr<Expr>& n, const std::string& script) {
-    printf("%*s", depth*2, "");
-    printf("%s %p (%d-%d) \"%s\"\n",
-           n->name.c_str(), n->fn, n->start, n->end,
-           script.substr(n->start, n->end - n->start).c_str());
-    for (size_t i = 0; i < n->argv.size(); ++i) {
-        ExprDump(depth+1, n->argv[i], script);
-    }
-}
-
-int main(int argc, char** argv) {
-    RegisterBuiltins();
-
-    if (argc != 2) {
-        printf("Usage: %s <edify script>\n", argv[0]);
-        return 1;
-    }
-
-    std::string buffer;
-    if (!android::base::ReadFileToString(argv[1], &buffer)) {
-        printf("%s: failed to read %s: %s\n", argv[0], argv[1], strerror(errno));
-        return 1;
-    }
-
-    std::unique_ptr<Expr> root;
-    int error_count = 0;
-    int error = parse_string(buffer.data(), &root, &error_count);
-    printf("parse returned %d; %d errors encountered\n", error, error_count);
-    if (error == 0 || error_count > 0) {
-
-        ExprDump(0, root, buffer);
-
-        State state(buffer, nullptr);
-        std::string result;
-        if (!Evaluate(&state, root, &result)) {
-            printf("result was NULL, message is: %s\n",
-                   (state.errmsg.empty() ? "(NULL)" : state.errmsg.c_str()));
-        } else {
-            printf("result is [%s]\n", result.c_str());
-        }
-    }
-    return 0;
-}
diff --git a/edify/expr.cpp b/edify/expr.cpp
index 54ab332..1b8623f 100644
--- a/edify/expr.cpp
+++ b/edify/expr.cpp
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-#include "expr.h"
+#include "edify/expr.h"
 
 #include <stdarg.h>
 #include <stdio.h>
@@ -31,6 +31,8 @@
 #include <android-base/stringprintf.h>
 #include <android-base/strings.h>
 
+#include "otautil/error_code.h"
+
 // Functions should:
 //
 //    - return a malloc()'d string
@@ -416,8 +418,5 @@
     return nullptr;
 }
 
-State::State(const std::string& script, void* cookie) :
-    script(script),
-    cookie(cookie) {
-}
-
+State::State(const std::string& script, void* cookie)
+    : script(script), cookie(cookie), error_code(kNoError), cause_code(kNoCause) {}
diff --git a/edify/expr.h b/edify/include/edify/expr.h
similarity index 86%
rename from edify/expr.h
rename to edify/include/edify/expr.h
index 4838d20..770d1cf 100644
--- a/edify/expr.h
+++ b/edify/include/edify/expr.h
@@ -23,32 +23,34 @@
 #include <string>
 #include <vector>
 
-#include "error_code.h"
+// Forward declaration to avoid including "otautil/error_code.h".
+enum ErrorCode : int;
+enum CauseCode : int;
 
 struct State {
-    State(const std::string& script, void* cookie);
+  State(const std::string& script, void* cookie);
 
-    // The source of the original script.
-    const std::string& script;
+  // The source of the original script.
+  const std::string& script;
 
-    // Optional pointer to app-specific data; the core of edify never
-    // uses this value.
-    void* cookie;
+  // Optional pointer to app-specific data; the core of edify never
+  // uses this value.
+  void* cookie;
 
-    // The error message (if any) returned if the evaluation aborts.
-    // Should be empty initially, will be either empty or a string that
-    // Evaluate() returns.
-    std::string errmsg;
+  // The error message (if any) returned if the evaluation aborts.
+  // Should be empty initially, will be either empty or a string that
+  // Evaluate() returns.
+  std::string errmsg;
 
-    // error code indicates the type of failure (e.g. failure to update system image)
-    // during the OTA process.
-    ErrorCode error_code = kNoError;
+  // error code indicates the type of failure (e.g. failure to update system image)
+  // during the OTA process.
+  ErrorCode error_code;
 
-    // cause code provides more detailed reason of an OTA failure (e.g. fsync error)
-    // in addition to the error code.
-    CauseCode cause_code = kNoCause;
+  // cause code provides more detailed reason of an OTA failure (e.g. fsync error)
+  // in addition to the error code.
+  CauseCode cause_code;
 
-    bool is_retry = false;
+  bool is_retry = false;
 };
 
 enum ValueType {
diff --git a/edify/lexer.ll b/edify/lexer.ll
index cb45943..4e04003 100644
--- a/edify/lexer.ll
+++ b/edify/lexer.ll
@@ -18,7 +18,7 @@
 #include <string.h>
 #include <string>
 
-#include "expr.h"
+#include "edify/expr.h"
 #include "yydefs.h"
 #include "parser.h"
 
diff --git a/edify/parser.yy b/edify/parser.yy
index b1685eb..bd2e010 100644
--- a/edify/parser.yy
+++ b/edify/parser.yy
@@ -25,7 +25,7 @@
 
 #include <android-base/macros.h>
 
-#include "expr.h"
+#include "edify/expr.h"
 #include "yydefs.h"
 #include "parser.h"
 
diff --git a/install.cpp b/install.cpp
index 74d1a68..d058931 100644
--- a/install.cpp
+++ b/install.cpp
@@ -49,9 +49,9 @@
 #include <ziparchive/zip_archive.h>
 
 #include "common.h"
-#include "error_code.h"
 #include "otautil/SysUtil.h"
 #include "otautil/ThermalUtil.h"
+#include "otautil/error_code.h"
 #include "private/install.h"
 #include "roots.h"
 #include "ui.h"
diff --git a/minui/Android.mk b/minui/Android.mk
index 9246984..9a217a4 100644
--- a/minui/Android.mk
+++ b/minui/Android.mk
@@ -58,6 +58,12 @@
   LOCAL_CFLAGS += -DOVERSCAN_PERCENT=0
 endif
 
+ifneq ($(TARGET_RECOVERY_DEFAULT_ROTATION),)
+  LOCAL_CFLAGS += -DDEFAULT_ROTATION=$(TARGET_RECOVERY_DEFAULT_ROTATION)
+else
+  LOCAL_CFLAGS += -DDEFAULT_ROTATION=ROTATION_NONE
+endif
+
 include $(BUILD_STATIC_LIBRARY)
 
 # Used by OEMs for factory test images.
diff --git a/minui/graphics.cpp b/minui/graphics.cpp
index 3bfce11..56f471b 100644
--- a/minui/graphics.cpp
+++ b/minui/graphics.cpp
@@ -16,6 +16,7 @@
 
 #include "graphics.h"
 
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -35,281 +36,311 @@
 static int overscan_offset_x = 0;
 static int overscan_offset_y = 0;
 
-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;
+static uint32_t gr_current = ~0;
+static constexpr uint32_t alpha_mask = 0xff000000;
 
 static GRSurface* gr_draw = NULL;
+static GRRotation rotation = ROTATION_NONE;
 
-static bool outside(int x, int y)
-{
-    return x < 0 || x >= gr_draw->width || y < 0 || y >= gr_draw->height;
+static bool outside(int x, int y) {
+  return x < 0 || x >= (rotation % 2 ? gr_draw->height : gr_draw->width) || y < 0 ||
+         y >= (rotation % 2 ? gr_draw->width : gr_draw->height);
 }
 
-const GRFont* gr_sys_font()
-{
-    return gr_font;
+const GRFont* gr_sys_font() {
+  return gr_font;
 }
 
-int gr_measure(const GRFont* font, const char *s)
-{
-    return font->char_width * strlen(s);
+int gr_measure(const GRFont* font, const char* s) {
+  return font->char_width * strlen(s);
 }
 
-void gr_font_size(const GRFont* font, int *x, int *y)
-{
-    *x = font->char_width;
-    *y = font->char_height;
+void gr_font_size(const GRFont* font, int* x, int* y) {
+  *x = font->char_width;
+  *y = font->char_height;
 }
 
-static void text_blend(unsigned char* src_p, int src_row_bytes,
-                       unsigned char* dst_p, int dst_row_bytes,
-                       int width, int height)
-{
-    for (int j = 0; j < height; ++j) {
-        unsigned char* sx = src_p;
-        unsigned char* px = dst_p;
-        for (int 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;
+// Blends gr_current onto pix value, assumes alpha as most significant byte.
+static inline uint32_t pixel_blend(uint8_t alpha, uint32_t pix) {
+  if (alpha == 255) return gr_current;
+  if (alpha == 0) return pix;
+  uint32_t pix_r = pix & 0xff;
+  uint32_t pix_g = pix & 0xff00;
+  uint32_t pix_b = pix & 0xff0000;
+  uint32_t cur_r = gr_current & 0xff;
+  uint32_t cur_g = gr_current & 0xff00;
+  uint32_t cur_b = gr_current & 0xff0000;
+
+  uint32_t out_r = (pix_r * (255 - alpha) + cur_r * alpha) / 255;
+  uint32_t out_g = (pix_g * (255 - alpha) + cur_g * alpha) / 255;
+  uint32_t out_b = (pix_b * (255 - alpha) + cur_b * alpha) / 255;
+
+  return (out_r & 0xff) | (out_g & 0xff00) | (out_b & 0xff0000) | (gr_current & 0xff000000);
+}
+
+// increments pixel pointer right, with current rotation.
+static void incr_x(uint32_t** p, int row_pixels) {
+  if (rotation % 2) {
+    *p = *p + (rotation == 1 ? 1 : -1) * row_pixels;
+  } else {
+    *p = *p + (rotation ? -1 : 1);
+  }
+}
+
+// increments pixel pointer down, with current rotation.
+static void incr_y(uint32_t** p, int row_pixels) {
+  if (rotation % 2) {
+    *p = *p + (rotation == 1 ? -1 : 1);
+  } else {
+    *p = *p + (rotation ? -1 : 1) * row_pixels;
+  }
+}
+
+// returns pixel pointer at given coordinates with rotation adjustment.
+static uint32_t* pixel_at(GRSurface* surf, int x, int y, int row_pixels) {
+  switch (rotation) {
+    case ROTATION_NONE:
+      return reinterpret_cast<uint32_t*>(surf->data) + y * row_pixels + x;
+    case ROTATION_RIGHT:
+      return reinterpret_cast<uint32_t*>(surf->data) + x * row_pixels + (surf->width - y);
+    case ROTATION_DOWN:
+      return reinterpret_cast<uint32_t*>(surf->data) + (surf->height - 1 - y) * row_pixels +
+             (surf->width - 1 - x);
+    case ROTATION_LEFT:
+      return reinterpret_cast<uint32_t*>(surf->data) + (surf->height - 1 - x) * row_pixels + y;
+    default:
+      printf("invalid rotation %d", rotation);
+  }
+  return nullptr;
+}
+
+static void text_blend(uint8_t* src_p, int src_row_bytes, uint32_t* dst_p, int dst_row_pixels,
+                       int width, int height) {
+  uint8_t alpha_current = static_cast<uint8_t>((alpha_mask & gr_current) >> 24);
+  for (int j = 0; j < height; ++j) {
+    uint8_t* sx = src_p;
+    uint32_t* px = dst_p;
+    for (int i = 0; i < width; ++i, incr_x(&px, dst_row_pixels)) {
+      uint8_t a = *sx++;
+      if (alpha_current < 255) a = (static_cast<uint32_t>(a) * alpha_current) / 255;
+      *px = pixel_blend(a, *px);
     }
+    src_p += src_row_bytes;
+    incr_y(&dst_p, dst_row_pixels);
+  }
 }
 
-void gr_text(const GRFont* font, int x, int y, const char *s, bool bold)
-{
-    if (!font->texture || gr_current_a == 0) return;
+void gr_text(const GRFont* font, int x, int y, const char* s, bool bold) {
+  if (!font || !font->texture || (gr_current & alpha_mask) == 0) return;
 
-    bold = bold && (font->texture->height != font->char_height);
+  if (font->texture->pixel_bytes != 1) {
+    printf("gr_text: font has wrong format\n");
+    return;
+  }
 
-    x += overscan_offset_x;
-    y += overscan_offset_y;
+  bold = bold && (font->texture->height != font->char_height);
 
-    unsigned char ch;
-    while ((ch = *s++)) {
-        if (outside(x, y) || outside(x+font->char_width-1, y+font->char_height-1)) break;
+  x += overscan_offset_x;
+  y += overscan_offset_y;
 
-        if (ch < ' ' || ch > '~') {
-            ch = '?';
-        }
+  unsigned char ch;
+  while ((ch = *s++)) {
+    if (outside(x, y) || outside(x + font->char_width - 1, y + font->char_height - 1)) break;
 
-        unsigned char* src_p = font->texture->data + ((ch - ' ') * font->char_width) +
-                               (bold ? font->char_height * 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->char_width, font->char_height);
-
-        x += font->char_width;
+    if (ch < ' ' || ch > '~') {
+      ch = '?';
     }
+
+    int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
+    uint8_t* src_p = font->texture->data + ((ch - ' ') * font->char_width) +
+                     (bold ? font->char_height * font->texture->row_bytes : 0);
+    uint32_t* dst_p = pixel_at(gr_draw, x, y, row_pixels);
+
+    text_blend(src_p, font->texture->row_bytes, dst_p, row_pixels, font->char_width,
+               font->char_height);
+
+    x += font->char_width;
+  }
 }
 
 void gr_texticon(int x, int y, GRSurface* icon) {
-    if (icon == NULL) return;
+  if (icon == NULL) return;
 
-    if (icon->pixel_bytes != 1) {
-        printf("gr_texticon: source has wrong format\n");
-        return;
-    }
+  if (icon->pixel_bytes != 1) {
+    printf("gr_texticon: source has wrong format\n");
+    return;
+  }
 
-    x += overscan_offset_x;
-    y += overscan_offset_y;
+  x += overscan_offset_x;
+  y += overscan_offset_y;
 
-    if (outside(x, y) || outside(x+icon->width-1, y+icon->height-1)) return;
+  if (outside(x, y) || outside(x + icon->width - 1, y + icon->height - 1)) return;
 
-    unsigned char* src_p = icon->data;
-    unsigned char* dst_p = gr_draw->data + y*gr_draw->row_bytes + x*gr_draw->pixel_bytes;
+  int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
+  uint8_t* src_p = icon->data;
+  uint32_t* dst_p = pixel_at(gr_draw, x, y, row_pixels);
 
-    text_blend(src_p, icon->row_bytes,
-               dst_p, gr_draw->row_bytes,
-               icon->width, icon->height);
+  text_blend(src_p, icon->row_bytes, dst_p, row_pixels, icon->width, icon->height);
 }
 
-void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a)
-{
+void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
+  uint32_t r32 = r, g32 = g, b32 = b, a32 = a;
 #if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
-    gr_current_r = b;
-    gr_current_g = g;
-    gr_current_b = r;
-    gr_current_a = a;
+  gr_current = (a32 << 24) | (r32 << 16) | (g32 << 8) | b32;
 #else
-    gr_current_r = r;
-    gr_current_g = g;
-    gr_current_b = b;
-    gr_current_a = a;
+  gr_current = (a32 << 24) | (b32 << 16) | (g32 << 8) | r32;
 #endif
 }
 
-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 {
-        unsigned char* px = gr_draw->data;
-        for (int y = 0; y < gr_draw->height; ++y) {
-            for (int 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_clear() {
+  if ((gr_current & 0xff) == ((gr_current >> 8) & 0xff) &&
+      (gr_current & 0xff) == ((gr_current >> 16) & 0xff) &&
+      (gr_current & 0xff) == ((gr_current >> 24) & 0xff) &&
+      gr_draw->row_bytes == gr_draw->width * gr_draw->pixel_bytes) {
+    memset(gr_draw->data, gr_current & 0xff, gr_draw->height * gr_draw->row_bytes);
+  } else {
+    uint32_t* px = reinterpret_cast<uint32_t*>(gr_draw->data);
+    int row_diff = gr_draw->row_bytes / gr_draw->pixel_bytes - gr_draw->width;
+    for (int y = 0; y < gr_draw->height; ++y) {
+      for (int x = 0; x < gr_draw->width; ++x) {
+        *px++ = gr_current;
+      }
+      px += row_diff;
     }
+  }
 }
 
-void gr_fill(int x1, int y1, int x2, int y2)
-{
-    x1 += overscan_offset_x;
-    y1 += overscan_offset_y;
+void gr_fill(int x1, int y1, int x2, int y2) {
+  x1 += overscan_offset_x;
+  y1 += overscan_offset_y;
 
-    x2 += overscan_offset_x;
-    y2 += overscan_offset_y;
+  x2 += overscan_offset_x;
+  y2 += overscan_offset_y;
 
-    if (outside(x1, y1) || outside(x2-1, y2-1)) return;
+  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;
-        }
+  int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
+  uint32_t* p = pixel_at(gr_draw, x1, y1, row_pixels);
+  uint8_t alpha = static_cast<uint8_t>(((gr_current & alpha_mask) >> 24));
+  if (alpha > 0) {
+    for (int y = y1; y < y2; ++y) {
+      uint32_t* px = p;
+      for (int x = x1; x < x2; ++x) {
+        *px = pixel_blend(alpha, *px);
+        incr_x(&px, row_pixels);
+      }
+      incr_y(&p, row_pixels);
     }
+  }
 }
 
 void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy) {
-    if (source == NULL) return;
+  if (source == NULL) return;
 
-    if (gr_draw->pixel_bytes != source->pixel_bytes) {
-        printf("gr_blit: source has wrong format\n");
-        return;
+  if (gr_draw->pixel_bytes != source->pixel_bytes) {
+    printf("gr_blit: source has wrong format\n");
+    return;
+  }
+
+  dx += overscan_offset_x;
+  dy += overscan_offset_y;
+
+  if (outside(dx, dy) || outside(dx + w - 1, dy + h - 1)) return;
+
+  if (rotation) {
+    int src_row_pixels = source->row_bytes / source->pixel_bytes;
+    int row_pixels = gr_draw->row_bytes / gr_draw->pixel_bytes;
+    uint32_t* src_py = reinterpret_cast<uint32_t*>(source->data) + sy * source->row_bytes / 4 + sx;
+    uint32_t* dst_py = pixel_at(gr_draw, dx, dy, row_pixels);
+
+    for (int y = 0; y < h; y += 1) {
+      uint32_t* src_px = src_py;
+      uint32_t* dst_px = dst_py;
+      for (int x = 0; x < w; x += 1) {
+        *dst_px = *src_px++;
+        incr_x(&dst_px, row_pixels);
+      }
+      src_py += src_row_pixels;
+      incr_y(&dst_py, row_pixels);
     }
-
-    dx += overscan_offset_x;
-    dy += overscan_offset_y;
-
-    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;
+  } else {
+    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;
+      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(GRSurface* surface) {
-    if (surface == NULL) {
-        return 0;
-    }
-    return surface->width;
+  if (surface == NULL) {
+    return 0;
+  }
+  return surface->width;
 }
 
 unsigned int gr_get_height(GRSurface* surface) {
-    if (surface == NULL) {
-        return 0;
-    }
-    return surface->height;
+  if (surface == NULL) {
+    return 0;
+  }
+  return surface->height;
 }
 
 int gr_init_font(const char* name, GRFont** dest) {
-    GRFont* font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font)));
-    if (font == nullptr) {
-        return -1;
-    }
+  GRFont* font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font)));
+  if (font == nullptr) {
+    return -1;
+  }
 
-    int res = res_create_alpha_surface(name, &(font->texture));
-    if (res < 0) {
-        free(font);
-        return res;
-    }
+  int res = res_create_alpha_surface(name, &(font->texture));
+  if (res < 0) {
+    free(font);
+    return res;
+  }
 
-    // The font image should be a 96x2 array of character images.  The
-    // columns are the printable ASCII characters 0x20 - 0x7f.  The
-    // top row is regular text; the bottom row is bold.
-    font->char_width = font->texture->width / 96;
-    font->char_height = font->texture->height / 2;
+  // The font image should be a 96x2 array of character images.  The
+  // columns are the printable ASCII characters 0x20 - 0x7f.  The
+  // top row is regular text; the bottom row is bold.
+  font->char_width = font->texture->width / 96;
+  font->char_height = font->texture->height / 2;
 
-    *dest = font;
+  *dest = font;
 
-    return 0;
+  return 0;
 }
 
-static void gr_init_font(void)
-{
-    int res = gr_init_font("font", &gr_font);
-    if (res == 0) {
-        return;
-    }
+static void gr_init_font(void) {
+  int res = gr_init_font("font", &gr_font);
+  if (res == 0) {
+    return;
+  }
 
-    printf("failed to read font: res=%d\n", res);
+  printf("failed to read font: res=%d\n", res);
 
+  // fall back to the compiled-in font.
+  gr_font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font)));
+  gr_font->texture = static_cast<GRSurface*>(malloc(sizeof(*gr_font->texture)));
+  gr_font->texture->width = font.width;
+  gr_font->texture->height = font.height;
+  gr_font->texture->row_bytes = font.width;
+  gr_font->texture->pixel_bytes = 1;
 
-    // fall back to the compiled-in font.
-    gr_font = static_cast<GRFont*>(calloc(1, sizeof(*gr_font)));
-    gr_font->texture = static_cast<GRSurface*>(malloc(sizeof(*gr_font->texture)));
-    gr_font->texture->width = font.width;
-    gr_font->texture->height = font.height;
-    gr_font->texture->row_bytes = font.width;
-    gr_font->texture->pixel_bytes = 1;
+  unsigned char* bits = static_cast<unsigned char*>(malloc(font.width * font.height));
+  gr_font->texture->data = bits;
 
-    unsigned char* bits = static_cast<unsigned char*>(malloc(font.width * font.height));
-    gr_font->texture->data = bits;
+  unsigned char data;
+  unsigned char* in = font.rundata;
+  while ((data = *in++)) {
+    memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
+    bits += (data & 0x7f);
+  }
 
-    unsigned char data;
-    unsigned char* in = font.rundata;
-    while((data = *in++)) {
-        memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f);
-        bits += (data & 0x7f);
-    }
-
-    gr_font->char_width = font.char_width;
-    gr_font->char_height = font.char_height;
+  gr_font->char_width = font.char_width;
+  gr_font->char_height = font.char_height;
 }
 
 void gr_flip() {
@@ -344,6 +375,12 @@
   gr_flip();
   gr_flip();
 
+  gr_rotate(DEFAULT_ROTATION);
+
+  if (gr_draw->pixel_bytes != 4) {
+    printf("gr_init: Only 4-byte pixel formats supported\n");
+  }
+
   return 0;
 }
 
@@ -352,13 +389,19 @@
 }
 
 int gr_fb_width() {
-  return gr_draw->width - 2 * overscan_offset_x;
+  return rotation % 2 ? gr_draw->height - 2 * overscan_offset_y
+                      : gr_draw->width - 2 * overscan_offset_x;
 }
 
 int gr_fb_height() {
-  return gr_draw->height - 2 * overscan_offset_y;
+  return rotation % 2 ? gr_draw->width - 2 * overscan_offset_x
+                      : gr_draw->height - 2 * overscan_offset_y;
 }
 
 void gr_fb_blank(bool blank) {
   gr_backend->Blank(blank);
 }
+
+void gr_rotate(GRRotation rot) {
+  rotation = rot;
+}
diff --git a/minui/include/minui/minui.h b/minui/include/minui/minui.h
index 27e6031..f9da199 100644
--- a/minui/include/minui/minui.h
+++ b/minui/include/minui/minui.h
@@ -28,17 +28,24 @@
 //
 
 struct GRSurface {
-    int width;
-    int height;
-    int row_bytes;
-    int pixel_bytes;
-    unsigned char* data;
+  int width;
+  int height;
+  int row_bytes;
+  int pixel_bytes;
+  unsigned char* data;
 };
 
 struct GRFont {
-    GRSurface* texture;
-    int char_width;
-    int char_height;
+  GRSurface* texture;
+  int char_width;
+  int char_height;
+};
+
+enum GRRotation {
+  ROTATION_NONE = 0,
+  ROTATION_RIGHT = 1,
+  ROTATION_DOWN = 2,
+  ROTATION_LEFT = 3,
 };
 
 int gr_init();
@@ -58,14 +65,17 @@
 
 const GRFont* gr_sys_font();
 int gr_init_font(const char* name, GRFont** dest);
-void gr_text(const GRFont* font, int x, int y, const char *s, bool bold);
-int gr_measure(const GRFont* font, const char *s);
-void gr_font_size(const GRFont* font, int *x, int *y);
+void gr_text(const GRFont* font, int x, int y, const char* s, bool bold);
+int gr_measure(const GRFont* font, const char* s);
+void gr_font_size(const GRFont* font, int* x, int* y);
 
 void gr_blit(GRSurface* source, int sx, int sy, int w, int h, int dx, int dy);
 unsigned int gr_get_width(GRSurface* surface);
 unsigned int gr_get_height(GRSurface* surface);
 
+// Set rotation, flips gr_fb_width/height if 90 degree rotation difference
+void gr_rotate(GRRotation rotation);
+
 //
 // Input events.
 //
@@ -115,8 +125,8 @@
 // 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,
-                                     int* fps, GRSurface*** pSurface);
+int res_create_multi_display_surface(const char* name, int* frames, int* fps,
+                                     GRSurface*** pSurface);
 
 // Load a single alpha surface from a grayscale PNG image.
 int res_create_alpha_surface(const char* name, GRSurface** pSurface);
diff --git a/otautil/Android.bp b/otautil/Android.bp
index 9cde7ba..659fefa 100644
--- a/otautil/Android.bp
+++ b/otautil/Android.bp
@@ -15,6 +15,8 @@
 cc_library_static {
     name: "libotautil",
 
+    host_supported: true,
+
     srcs: [
         "SysUtil.cpp",
         "DirUtil.cpp",
@@ -27,6 +29,7 @@
     ],
 
     cflags: [
+        "-D_FILE_OFFSET_BITS=64",
         "-Werror",
         "-Wall",
     ],
diff --git a/otautil/SysUtil.cpp b/otautil/SysUtil.cpp
index d54a824..48336ad 100644
--- a/otautil/SysUtil.cpp
+++ b/otautil/SysUtil.cpp
@@ -16,6 +16,7 @@
 
 #include "otautil/SysUtil.h"
 
+#include <errno.h>  // TEMP_FAILURE_RETRY
 #include <fcntl.h>
 #include <stdint.h>  // SIZE_MAX
 #include <sys/mman.h>
@@ -100,7 +101,7 @@
   }
 
   // Reserve enough contiguous address space for the whole file.
-  void* reserve = mmap64(nullptr, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
+  void* reserve = mmap(nullptr, blocks * blksize, PROT_NONE, MAP_PRIVATE | MAP_ANON, -1, 0);
   if (reserve == MAP_FAILED) {
     PLOG(ERROR) << "failed to reserve address space";
     return false;
@@ -135,8 +136,8 @@
       break;
     }
 
-    void* range_start = mmap64(next, range_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd,
-                               static_cast<off64_t>(start) * blksize);
+    void* range_start = mmap(next, range_size, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd,
+                             static_cast<off_t>(start) * blksize);
     if (range_start == MAP_FAILED) {
       PLOG(ERROR) << "failed to map range " << i << ": " << line;
       success = false;
diff --git a/error_code.h b/otautil/include/otautil/error_code.h
similarity index 94%
rename from error_code.h
rename to otautil/include/otautil/error_code.h
index 4e3032b..b0ff42d 100644
--- a/error_code.h
+++ b/otautil/include/otautil/error_code.h
@@ -17,7 +17,7 @@
 #ifndef _ERROR_CODE_H_
 #define _ERROR_CODE_H_
 
-enum ErrorCode {
+enum ErrorCode : int {
   kNoError = -1,
   kLowBattery = 20,
   kZipVerificationFailure,
@@ -30,7 +30,7 @@
   kUpdateBinaryCommandFailure,
 };
 
-enum CauseCode {
+enum CauseCode : int {
   kNoCause = -1,
   kArgsParsingFailure = 100,
   kStashCreationFailure,
@@ -51,7 +51,7 @@
   kVendorFailure = 200
 };
 
-enum UncryptErrorCode {
+enum UncryptErrorCode : int {
   kUncryptNoError = -1,
   kUncryptErrorPlaceholder = 50,
   kUncryptTimeoutError = 100,
@@ -75,4 +75,4 @@
   kUncryptBlockDeviceFindError,
 };
 
-#endif // _ERROR_CODE_H_
+#endif  // _ERROR_CODE_H_
diff --git a/print_sha1.h b/otautil/include/otautil/print_sha1.h
similarity index 75%
rename from print_sha1.h
rename to otautil/include/otautil/print_sha1.h
index d0c18b3..03a8d29 100644
--- a/print_sha1.h
+++ b/otautil/include/otautil/print_sha1.h
@@ -23,25 +23,25 @@
 #include <openssl/sha.h>
 
 static std::string print_sha1(const uint8_t* sha1, size_t len) {
-    const char* hex = "0123456789abcdef";
-    std::string result = "";
-    for (size_t i = 0; i < len; ++i) {
-        result.push_back(hex[(sha1[i]>>4) & 0xf]);
-        result.push_back(hex[sha1[i] & 0xf]);
-    }
-    return result;
+  const char* hex = "0123456789abcdef";
+  std::string result = "";
+  for (size_t i = 0; i < len; ++i) {
+    result.push_back(hex[(sha1[i] >> 4) & 0xf]);
+    result.push_back(hex[sha1[i] & 0xf]);
+  }
+  return result;
 }
 
 [[maybe_unused]] static std::string print_sha1(const uint8_t sha1[SHA_DIGEST_LENGTH]) {
-    return print_sha1(sha1, SHA_DIGEST_LENGTH);
+  return print_sha1(sha1, SHA_DIGEST_LENGTH);
 }
 
 [[maybe_unused]] static std::string short_sha1(const uint8_t sha1[SHA_DIGEST_LENGTH]) {
-    return print_sha1(sha1, 4);
+  return print_sha1(sha1, 4);
 }
 
 [[maybe_unused]] static std::string print_hex(const uint8_t* bytes, size_t len) {
-    return print_sha1(bytes, len);
+  return print_sha1(bytes, len);
 }
 
 #endif  // RECOVERY_PRINT_SHA1_H
diff --git a/rangeset.h b/otautil/include/otautil/rangeset.h
similarity index 100%
rename from rangeset.h
rename to otautil/include/otautil/rangeset.h
diff --git a/recovery.cpp b/recovery.cpp
index 4dc5b54..243ee4a 100644
--- a/recovery.cpp
+++ b/recovery.cpp
@@ -61,13 +61,13 @@
 #include "adb_install.h"
 #include "common.h"
 #include "device.h"
-#include "error_code.h"
 #include "fuse_sdcard_provider.h"
 #include "fuse_sideload.h"
 #include "install.h"
 #include "minadbd/minadbd.h"
 #include "minui/minui.h"
 #include "otautil/DirUtil.h"
+#include "otautil/error_code.h"
 #include "roots.h"
 #include "rotate_logs.h"
 #include "screen_ui.h"
diff --git a/screen_ui.cpp b/screen_ui.cpp
index 166d7b4..c8fb5aa 100644
--- a/screen_ui.cpp
+++ b/screen_ui.cpp
@@ -149,8 +149,8 @@
   int elements_sum = gr_get_height(loopFrames[0]) + PixelsFromDp(kLayouts[layout_][ICON]) +
                      gr_get_height(installing_text) + PixelsFromDp(kLayouts[layout_][TEXT]) +
                      gr_get_height(progressBarFill);
-  int bottom_gap = (gr_fb_height() - elements_sum) / 2;
-  return gr_fb_height() - bottom_gap - gr_get_height(progressBarFill);
+  int bottom_gap = (ScreenHeight() - elements_sum) / 2;
+  return ScreenHeight() - bottom_gap - gr_get_height(progressBarFill);
 }
 
 // Clear the screen and draw the currently selected background icon (if any).
@@ -159,25 +159,24 @@
   pagesIdentical = false;
   gr_color(0, 0, 0, 255);
   gr_clear();
-
   if (currentIcon != NONE) {
     if (max_stage != -1) {
       int stage_height = gr_get_height(stageMarkerEmpty);
       int stage_width = gr_get_width(stageMarkerEmpty);
-      int x = (gr_fb_width() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
-      int y = gr_fb_height() - stage_height - kMarginHeight;
+      int x = (ScreenWidth() - max_stage * gr_get_width(stageMarkerEmpty)) / 2;
+      int y = ScreenHeight() - stage_height - kMarginHeight;
       for (int i = 0; i < max_stage; ++i) {
         GRSurface* stage_surface = (i < stage) ? stageMarkerFill : stageMarkerEmpty;
-        gr_blit(stage_surface, 0, 0, stage_width, stage_height, x, y);
+        DrawSurface(stage_surface, 0, 0, stage_width, stage_height, x, y);
         x += stage_width;
       }
     }
 
     GRSurface* text_surface = GetCurrentText();
-    int text_x = (gr_fb_width() - gr_get_width(text_surface)) / 2;
+    int text_x = (ScreenWidth() - gr_get_width(text_surface)) / 2;
     int text_y = GetTextBaseline();
     gr_color(255, 255, 255, 255);
-    gr_texticon(text_x, text_y, text_surface);
+    DrawTextIcon(text_x, text_y, text_surface);
   }
 }
 
@@ -188,21 +187,21 @@
     GRSurface* frame = GetCurrentFrame();
     int frame_width = gr_get_width(frame);
     int frame_height = gr_get_height(frame);
-    int frame_x = (gr_fb_width() - frame_width) / 2;
+    int frame_x = (ScreenWidth() - frame_width) / 2;
     int frame_y = GetAnimationBaseline();
-    gr_blit(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
+    DrawSurface(frame, 0, 0, frame_width, frame_height, frame_x, frame_y);
   }
 
   if (progressBarType != EMPTY) {
     int width = gr_get_width(progressBarEmpty);
     int height = gr_get_height(progressBarEmpty);
 
-    int progress_x = (gr_fb_width() - width) / 2;
+    int progress_x = (ScreenWidth() - width) / 2;
     int progress_y = GetProgressBaseline();
 
     // Erase behind the progress bar (in case this was a progress-only update)
     gr_color(0, 0, 0, 255);
-    gr_fill(progress_x, progress_y, width, height);
+    DrawFill(progress_x, progress_y, width, height);
 
     if (progressBarType == DETERMINATE) {
       float p = progressScopeStart + progress * progressScopeSize;
@@ -211,19 +210,19 @@
       if (rtl_locale_) {
         // Fill the progress bar from right to left.
         if (pos > 0) {
-          gr_blit(progressBarFill, width - pos, 0, pos, height, progress_x + width - pos,
-                  progress_y);
+          DrawSurface(progressBarFill, width - pos, 0, pos, height, progress_x + width - pos,
+                      progress_y);
         }
         if (pos < width - 1) {
-          gr_blit(progressBarEmpty, 0, 0, width - pos, height, progress_x, progress_y);
+          DrawSurface(progressBarEmpty, 0, 0, width - pos, height, progress_x, progress_y);
         }
       } else {
         // Fill the progress bar from left to right.
         if (pos > 0) {
-          gr_blit(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
+          DrawSurface(progressBarFill, 0, 0, pos, height, progress_x, progress_y);
         }
         if (pos < width - 1) {
-          gr_blit(progressBarEmpty, pos, 0, width - pos, height, progress_x + pos, progress_y);
+          DrawSurface(progressBarEmpty, pos, 0, width - pos, height, progress_x + pos, progress_y);
         }
       }
     }
@@ -335,8 +334,21 @@
   SetLocale(saved_locale);
 }
 
+int ScreenRecoveryUI::ScreenWidth() const {
+  return gr_fb_width();
+}
+
+int ScreenRecoveryUI::ScreenHeight() const {
+  return gr_fb_height();
+}
+
+void ScreenRecoveryUI::DrawSurface(GRSurface* surface, int sx, int sy, int w, int h, int dx,
+                                   int dy) const {
+  gr_blit(surface, sx, sy, w, h, dx, dy);
+}
+
 int ScreenRecoveryUI::DrawHorizontalRule(int y) const {
-  gr_fill(0, y + 4, gr_fb_width(), y + 6);
+  gr_fill(0, y + 4, ScreenWidth(), y + 6);
   return 8;
 }
 
@@ -344,6 +356,14 @@
   gr_fill(x, y, x + width, y + height);
 }
 
+void ScreenRecoveryUI::DrawFill(int x, int y, int w, int h) const {
+  gr_fill(x, y, w, h);
+}
+
+void ScreenRecoveryUI::DrawTextIcon(int x, int y, GRSurface* surface) const {
+  gr_texticon(x, y, surface);
+}
+
 int ScreenRecoveryUI::DrawTextLine(int x, int y, const char* line, bool bold) const {
   gr_text(gr_sys_font(), x, y, line, bold);
   return char_height_ + 4;
@@ -432,7 +452,7 @@
       if (i == menu_sel) {
         // Draw the highlight bar.
         SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG);
-        DrawHighlightBar(0, y - 2, gr_fb_width(), char_height_ + 4);
+        DrawHighlightBar(0, y - 2, ScreenWidth(), char_height_ + 4);
         // Bold white text for the selected item.
         SetColor(MENU_SEL_FG);
         y += DrawTextLine(x, y, menu_[i].c_str(), true);
@@ -449,7 +469,7 @@
   SetColor(LOG);
   int row = text_row_;
   size_t count = 0;
-  for (int ty = gr_fb_height() - kMarginHeight - char_height_; ty >= y && count < text_rows_;
+  for (int ty = ScreenHeight() - kMarginHeight - char_height_; ty >= y && count < text_rows_;
        ty -= char_height_, ++count) {
     DrawTextLine(kMarginWidth, ty, text_[row], false);
     --row;
@@ -569,8 +589,8 @@
   }
 
   gr_font_size(gr_sys_font(), &char_width_, &char_height_);
-  text_rows_ = (gr_fb_height() - kMarginHeight * 2) / char_height_;
-  text_cols_ = (gr_fb_width() - kMarginWidth * 2) / char_width_;
+  text_rows_ = (ScreenHeight() - kMarginHeight * 2) / char_height_;
+  text_cols_ = (ScreenWidth() - kMarginWidth * 2) / char_width_;
   return true;
 }
 
diff --git a/screen_ui.h b/screen_ui.h
index 3a28a09..f05761c 100644
--- a/screen_ui.h
+++ b/screen_ui.h
@@ -124,12 +124,23 @@
   virtual int GetProgressBaseline() const;
   virtual int GetTextBaseline() const;
 
+  // Returns pixel width of draw buffer.
+  virtual int ScreenWidth() const;
+  // Returns pixel height of draw buffer.
+  virtual int ScreenHeight() const;
+
   // Draws a highlight bar at (x, y) - (x + width, y + height).
   virtual void DrawHighlightBar(int x, int y, int width, int height) const;
   // Draws a horizontal rule at Y. Returns the offset it should be moving along Y-axis.
   virtual int DrawHorizontalRule(int y) const;
   // Draws a line of text. Returns the offset it should be moving along Y-axis.
   virtual int DrawTextLine(int x, int y, const char* line, bool bold) const;
+  // Draws surface portion (sx, sy, w, h) at screen location (dx, dy).
+  virtual void DrawSurface(GRSurface* surface, int sx, int sy, int w, int h, int dx, int dy) const;
+  // Draws rectangle at (x, y) - (x + w, y + h).
+  virtual void DrawFill(int x, int y, int w, int h) const;
+  // Draws given surface (surface->pixel_bytes = 1) as text at (x, y).
+  virtual void DrawTextIcon(int x, int y, GRSurface* surface) const;
   // Draws multiple text lines. Returns the offset it should be moving along Y-axis.
   int DrawTextLines(int x, int y, const char* const* lines) const;
   // Similar to DrawTextLines() to draw multiple text lines, but additionally wraps long lines.
diff --git a/tests/Android.mk b/tests/Android.mk
index 31c7de1..b0f71a8 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -201,6 +201,7 @@
 LOCAL_STATIC_LIBRARIES := \
     libimgdiff \
     libimgpatch \
+    libotautil \
     libbsdiff \
     libbspatch \
     libziparchive \
diff --git a/tests/component/applypatch_test.cpp b/tests/component/applypatch_test.cpp
index 4254289..15ec08f 100644
--- a/tests/component/applypatch_test.cpp
+++ b/tests/component/applypatch_test.cpp
@@ -35,7 +35,7 @@
 #include "applypatch/applypatch.h"
 #include "applypatch/applypatch_modes.h"
 #include "common/test_constants.h"
-#include "print_sha1.h"
+#include "otautil/print_sha1.h"
 
 static void sha1sum(const std::string& fname, std::string* sha1, size_t* fsize = nullptr) {
   ASSERT_NE(nullptr, sha1);
diff --git a/tests/component/updater_test.cpp b/tests/component/updater_test.cpp
index 2a0575a..e6aec4a 100644
--- a/tests/component/updater_test.cpp
+++ b/tests/component/updater_test.cpp
@@ -39,9 +39,9 @@
 
 #include "common/test_constants.h"
 #include "edify/expr.h"
-#include "error_code.h"
 #include "otautil/SysUtil.h"
-#include "print_sha1.h"
+#include "otautil/error_code.h"
+#include "otautil/print_sha1.h"
 #include "updater/blockimg.h"
 #include "updater/install.h"
 #include "updater/updater.h"
diff --git a/tests/unit/rangeset_test.cpp b/tests/unit/rangeset_test.cpp
index 15bcec8..b3ed992 100644
--- a/tests/unit/rangeset_test.cpp
+++ b/tests/unit/rangeset_test.cpp
@@ -21,7 +21,7 @@
 
 #include <gtest/gtest.h>
 
-#include "rangeset.h"
+#include "otautil/rangeset.h"
 
 TEST(RangeSetTest, Parse_smoke) {
   RangeSet rs = RangeSet::Parse("2,1,10");
@@ -156,4 +156,4 @@
   ASSERT_EQ(static_cast<size_t>(40970), rs.GetOffsetInRangeSet(4096 * 16 + 10));
   // block#10 not in range.
   ASSERT_EXIT(rs.GetOffsetInRangeSet(40970), ::testing::KilledBySignal(SIGABRT), "");
-}
\ No newline at end of file
+}
diff --git a/uncrypt/Android.mk b/uncrypt/Android.mk
index a3b0ca9..601f927 100644
--- a/uncrypt/Android.mk
+++ b/uncrypt/Android.mk
@@ -17,10 +17,10 @@
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES := uncrypt.cpp
-LOCAL_C_INCLUDES := $(LOCAL_PATH)/..
 LOCAL_MODULE := uncrypt
 LOCAL_STATIC_LIBRARIES := \
     libbootloader_message \
+    libotautil \
     libbase \
     liblog \
     libfs_mgr \
diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp
index 7a2ccbc..645faad 100644
--- a/uncrypt/uncrypt.cpp
+++ b/uncrypt/uncrypt.cpp
@@ -116,7 +116,7 @@
 #include <cutils/sockets.h>
 #include <fs_mgr.h>
 
-#include "error_code.h"
+#include "otautil/error_code.h"
 
 static constexpr int WINDOW_SIZE = 5;
 static constexpr int FIBMAP_RETRY_LIMIT = 3;
diff --git a/updater/blockimg.cpp b/updater/blockimg.cpp
index 696cddf..ce3cea4 100644
--- a/updater/blockimg.cpp
+++ b/updater/blockimg.cpp
@@ -50,10 +50,10 @@
 #include <ziparchive/zip_archive.h>
 
 #include "edify/expr.h"
-#include "error_code.h"
 #include "otafault/ota_io.h"
-#include "print_sha1.h"
-#include "rangeset.h"
+#include "otautil/error_code.h"
+#include "otautil/print_sha1.h"
+#include "otautil/rangeset.h"
 #include "updater/install.h"
 #include "updater/updater.h"
 
diff --git a/updater/install.cpp b/updater/install.cpp
index fc085d5..9425d18 100644
--- a/updater/install.cpp
+++ b/updater/install.cpp
@@ -54,15 +54,15 @@
 #include <openssl/sha.h>
 #include <selinux/label.h>
 #include <selinux/selinux.h>
+#include <tune2fs.h>
 #include <ziparchive/zip_archive.h>
 
 #include "edify/expr.h"
-#include "error_code.h"
 #include "mounts.h"
 #include "otafault/ota_io.h"
 #include "otautil/DirUtil.h"
-#include "print_sha1.h"
-#include "tune2fs.h"
+#include "otautil/error_code.h"
+#include "otautil/print_sha1.h"
 #include "updater/updater.h"
 
 // Send over the buffer to recovery though the command pipe.
diff --git a/updater/updater.cpp b/updater/updater.cpp
index e10174f..f55a0d3 100644
--- a/updater/updater.cpp
+++ b/updater/updater.cpp
@@ -34,6 +34,7 @@
 #include "otafault/config.h"
 #include "otautil/DirUtil.h"
 #include "otautil/SysUtil.h"
+#include "otautil/error_code.h"
 #include "updater/blockimg.h"
 #include "updater/install.h"
 
diff --git a/verifier.cpp b/verifier.cpp
index 18437fb..283e043 100644
--- a/verifier.cpp
+++ b/verifier.cpp
@@ -32,7 +32,7 @@
 #include <openssl/obj_mac.h>
 
 #include "asn1_decoder.h"
-#include "print_sha1.h"
+#include "otautil/print_sha1.h"
 
 static constexpr size_t MiB = 1024 * 1024;
 
diff --git a/vr_ui.cpp b/vr_ui.cpp
index 1251672..07cc9da 100644
--- a/vr_ui.cpp
+++ b/vr_ui.cpp
@@ -20,16 +20,46 @@
 
 VrRecoveryUI::VrRecoveryUI() : kStereoOffset(RECOVERY_UI_VR_STEREO_OFFSET) {}
 
-bool VrRecoveryUI::InitTextParams() {
-  if (!ScreenRecoveryUI::InitTextParams()) return false;
-  int mid_divide = gr_fb_width() / 2;
-  text_cols_ = (mid_divide - kMarginWidth - kStereoOffset) / char_width_;
-  return true;
+int VrRecoveryUI::ScreenWidth() const {
+  return gr_fb_width() / 2;
+}
+
+int VrRecoveryUI::ScreenHeight() const {
+  return gr_fb_height();
+}
+
+void VrRecoveryUI::DrawSurface(GRSurface* surface, int sx, int sy, int w, int h, int dx,
+                               int dy) const {
+  gr_blit(surface, sx, sy, w, h, dx + kStereoOffset, dy);
+  gr_blit(surface, sx, sy, w, h, dx - kStereoOffset + ScreenWidth(), dy);
+}
+
+void VrRecoveryUI::DrawTextIcon(int x, int y, GRSurface* surface) const {
+  gr_texticon(x + kStereoOffset, y, surface);
+  gr_texticon(x - kStereoOffset + ScreenWidth(), y, surface);
 }
 
 int VrRecoveryUI::DrawTextLine(int x, int y, const char* line, bool bold) const {
-  int mid_divide = gr_fb_width() / 2;
   gr_text(gr_sys_font(), x + kStereoOffset, y, line, bold);
-  gr_text(gr_sys_font(), x - kStereoOffset + mid_divide, y, line, bold);
+  gr_text(gr_sys_font(), x - kStereoOffset + ScreenWidth(), y, line, bold);
   return char_height_ + 4;
 }
+
+int VrRecoveryUI::DrawHorizontalRule(int y) const {
+  y += 4;
+  gr_fill(kMarginWidth + kStereoOffset, y, ScreenWidth() - kMarginWidth + kStereoOffset, y + 2);
+  gr_fill(ScreenWidth() + kMarginWidth - kStereoOffset, y,
+          gr_fb_width() - kMarginWidth - kStereoOffset, y + 2);
+  return y + 4;
+}
+
+void VrRecoveryUI::DrawHighlightBar(int x, int y, int width, int height) const {
+  gr_fill(kMarginWidth + kStereoOffset, y, ScreenWidth() - kMarginWidth + kStereoOffset, y + height);
+  gr_fill(ScreenWidth() + kMarginWidth - kStereoOffset, y,
+          gr_fb_width() - kMarginWidth - kStereoOffset, y + height);
+}
+
+void VrRecoveryUI::DrawFill(int x, int y, int w, int h) const {
+  gr_fill(x + kStereoOffset, y, w, h);
+  gr_fill(x - kStereoOffset + ScreenWidth(), y, w, h);
+}
diff --git a/vr_ui.h b/vr_ui.h
index d996c14..eeb4589 100644
--- a/vr_ui.h
+++ b/vr_ui.h
@@ -28,8 +28,14 @@
   // Can vary per device depending on screen size and lens distortion.
   const int kStereoOffset;
 
-  bool InitTextParams() override;
+  int ScreenWidth() const override;
+  int ScreenHeight() const override;
 
+  void DrawSurface(GRSurface* surface, int sx, int sy, int w, int h, int dx, int dy) const override;
+  int DrawHorizontalRule(int y) const override;
+  void DrawHighlightBar(int x, int y, int width, int height) const override;
+  void DrawFill(int x, int y, int w, int h) const override;
+  void DrawTextIcon(int x, int y, GRSurface* surface) const override;
   int DrawTextLine(int x, int y, const char* line, bool bold) const override;
 };