From d29f641b1c4809c0f074a179286e163d376a289e Mon Sep 17 00:00:00 2001 From: Michael Runge Date: Mon, 21 Jul 2014 17:40:02 -0700 Subject: Auto create parent directories for rename support Sometimes renames will move a file into a directory that does not yet exist. This will create the parent directories, using the same symlink logic, to ensure that there is a valid destination. Bug: 16458395 Change-Id: Iaa005a12ce800c39f4db20f7c25a2a68cb40a52d --- updater/install.c | 8 +++++--- updater/install.h | 2 ++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/updater/install.c b/updater/install.c index edc386dc6..5025881d2 100644 --- a/updater/install.c +++ b/updater/install.c @@ -357,8 +357,10 @@ Value* RenameFn(const char* name, State* state, int argc, Expr* argv[]) { name); goto done; } - - if (rename(src_name, dst_name) != 0) { + if (make_parents(dst_name) != 0) { + ErrorAbort(state, "Creating parent of %s() failed, error %s()", + dst_name, strerror(errno)); + } else if (rename(src_name, dst_name) != 0) { ErrorAbort(state, "Rename of %s() to %s() failed, error %s()", src_name, dst_name, strerror(errno)); } else { @@ -642,7 +644,7 @@ static int make_parents(char* name) { *p = '\0'; if (make_parents(name) < 0) return -1; int result = mkdir(name, 0700); - if (result == 0) printf("symlink(): created [%s]\n", name); + if (result == 0) printf("created [%s]\n", name); *p = '/'; if (result == 0 || errno == EEXIST) { // successfully created or already existed; we're done diff --git a/updater/install.h b/updater/install.h index 94f344f8e..659c8b41c 100644 --- a/updater/install.h +++ b/updater/install.h @@ -19,4 +19,6 @@ void RegisterInstallFunctions(); +static int make_parents(char* name); + #endif -- cgit v1.2.3 From 66f5ce387af87584b40eeec774429d7ead28b7cc Mon Sep 17 00:00:00 2001 From: Doug Zongker Date: Fri, 15 Aug 2014 14:31:52 -0700 Subject: installer for new block OTA system Bug: 16984795 Change-Id: I90f958446baed83dec658de2430c8fc5e9c3047e --- applypatch/applypatch.c | 6 +- applypatch/applypatch.h | 2 +- applypatch/bspatch.c | 4 +- applypatch/imgpatch.c | 4 +- updater/Android.mk | 1 + updater/blockimg.c | 631 ++++++++++++++++++++++++++++++++++++++++++++++++ updater/blockimg.h | 22 ++ updater/install.c | 2 +- updater/updater.c | 4 + updater/updater.h | 3 + 10 files changed, 669 insertions(+), 10 deletions(-) create mode 100644 updater/blockimg.c create mode 100644 updater/blockimg.h diff --git a/applypatch/applypatch.c b/applypatch/applypatch.c index 60e9e4a5c..bfb9440e4 100644 --- a/applypatch/applypatch.c +++ b/applypatch/applypatch.c @@ -32,7 +32,7 @@ #include "edify/expr.h" static int LoadPartitionContents(const char* filename, FileContents* file); -static ssize_t FileSink(unsigned char* data, ssize_t len, void* token); +static ssize_t FileSink(const unsigned char* data, ssize_t len, void* token); static int GenerateTarget(FileContents* source_file, const Value* source_patch_value, FileContents* copy_file, @@ -599,7 +599,7 @@ int ShowLicenses() { return 0; } -ssize_t FileSink(unsigned char* data, ssize_t len, void* token) { +ssize_t FileSink(const unsigned char* data, ssize_t len, void* token) { int fd = *(int *)token; ssize_t done = 0; ssize_t wrote; @@ -620,7 +620,7 @@ typedef struct { ssize_t pos; } MemorySinkInfo; -ssize_t MemorySink(unsigned char* data, ssize_t len, void* token) { +ssize_t MemorySink(const unsigned char* data, ssize_t len, void* token) { MemorySinkInfo* msi = (MemorySinkInfo*)token; if (msi->size - msi->pos < len) { return -1; diff --git a/applypatch/applypatch.h b/applypatch/applypatch.h index ee54c24ea..edec84812 100644 --- a/applypatch/applypatch.h +++ b/applypatch/applypatch.h @@ -40,7 +40,7 @@ typedef struct _FileContents { // and use it as the source instead. #define CACHE_TEMP_SOURCE "/cache/saved.file" -typedef ssize_t (*SinkFn)(unsigned char*, ssize_t, void*); +typedef ssize_t (*SinkFn)(const unsigned char*, ssize_t, void*); // applypatch.c int ShowLicenses(); diff --git a/applypatch/bspatch.c b/applypatch/bspatch.c index 1dc7ab10b..b34ec2a88 100644 --- a/applypatch/bspatch.c +++ b/applypatch/bspatch.c @@ -112,9 +112,7 @@ int ApplyBSDiffPatch(const unsigned char* old_data, ssize_t old_size, printf("short write of output: %d (%s)\n", errno, strerror(errno)); return 1; } - if (ctx) { - SHA_update(ctx, new_data, new_size); - } + if (ctx) SHA_update(ctx, new_data, new_size); free(new_data); return 0; diff --git a/applypatch/imgpatch.c b/applypatch/imgpatch.c index af4d07281..33c448762 100644 --- a/applypatch/imgpatch.c +++ b/applypatch/imgpatch.c @@ -95,7 +95,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size __unused, printf("failed to read chunk %d raw data\n", i); return -1; } - SHA_update(ctx, patch->data + pos, data_len); + if (ctx) SHA_update(ctx, patch->data + pos, data_len); if (sink((unsigned char*)patch->data + pos, data_len, token) != data_len) { printf("failed to write chunk %d raw data\n", i); @@ -217,7 +217,7 @@ int ApplyImagePatch(const unsigned char* old_data, ssize_t old_size __unused, (long)have); return -1; } - SHA_update(ctx, temp_data, have); + if (ctx) SHA_update(ctx, temp_data, have); } while (ret != Z_STREAM_END); deflateEnd(&strm); diff --git a/updater/Android.mk b/updater/Android.mk index 99b489029..b183c9221 100644 --- a/updater/Android.mk +++ b/updater/Android.mk @@ -4,6 +4,7 @@ LOCAL_PATH := $(call my-dir) updater_src_files := \ install.c \ + blockimg.c \ updater.c # diff --git a/updater/blockimg.c b/updater/blockimg.c new file mode 100644 index 000000000..c442ab22a --- /dev/null +++ b/updater/blockimg.c @@ -0,0 +1,631 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "applypatch/applypatch.h" +#include "edify/expr.h" +#include "mincrypt/sha.h" +#include "minzip/DirUtil.h" +#include "updater.h" + +#define BLOCKSIZE 4096 + +// Set this to 1 to interpret 'erase' transfers to mean do a +// BLKDISCARD ioctl. Set to 0 to interpret erase to mean fill the +// region with zeroes. +#define DEBUG_ERASE 0 + +#ifndef BLKDISCARD +#define BLKDISCARD _IO(0x12,119) +#endif + +char* PrintSha1(const uint8_t* digest); + +typedef struct { + int count; + int size; + int pos[0]; +} RangeSet; + +static RangeSet* parse_range(char* text) { + char* save; + int num; + num = strtol(strtok_r(text, ",", &save), NULL, 0); + + RangeSet* out = malloc(sizeof(RangeSet) + num * sizeof(int)); + if (out == NULL) { + fprintf(stderr, "failed to allocate range of %d bytes\n", + sizeof(RangeSet) + num * sizeof(int)); + exit(1); + } + out->count = num / 2; + out->size = 0; + int i; + for (i = 0; i < num; ++i) { + out->pos[i] = strtol(strtok_r(NULL, ",", &save), NULL, 0); + if (i%2) { + out->size += out->pos[i]; + } else { + out->size -= out->pos[i]; + } + } + + return out; +} + +static void readblock(int fd, uint8_t* data, size_t size) { + size_t so_far = 0; + while (so_far < size) { + ssize_t r = read(fd, data+so_far, size-so_far); + if (r < 0 && errno != EINTR) { + fprintf(stderr, "read failed: %s\n", strerror(errno)); + return; + } else { + so_far += r; + } + } +} + +static void writeblock(int fd, const uint8_t* data, size_t size) { + size_t written = 0; + while (written < size) { + ssize_t w = write(fd, data+written, size-written); + if (w < 0 && errno != EINTR) { + fprintf(stderr, "write failed: %s\n", strerror(errno)); + return; + } else { + written += w; + } + } +} + +static void check_lseek(int fd, off_t offset, int whence) { + while (true) { + int ret = lseek(fd, offset, whence); + if (ret < 0) { + if (errno != EINTR) { + fprintf(stderr, "lseek failed: %s\n", strerror(errno)); + exit(1); + } + } else { + break; + } + } +} + +static void allocate(size_t size, uint8_t** buffer, size_t* buffer_alloc) { + // if the buffer's big enough, reuse it. + if (size <= *buffer_alloc) return; + + free(*buffer); + + *buffer = (uint8_t*) malloc(size); + if (*buffer == NULL) { + fprintf(stderr, "failed to allocate %d bytes\n", size); + exit(1); + } + *buffer_alloc = size; +} + +typedef struct { + int fd; + RangeSet* tgt; + int p_block; + size_t p_remain; +} RangeSinkState; + +static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) { + RangeSinkState* rss = (RangeSinkState*) token; + + if (rss->p_remain <= 0) { + fprintf(stderr, "range sink write overrun"); + exit(1); + } + + ssize_t written = 0; + while (size > 0) { + size_t write_now = size; + if (rss->p_remain < write_now) write_now = rss->p_remain; + writeblock(rss->fd, data, write_now); + data += write_now; + size -= write_now; + + rss->p_remain -= write_now; + written += write_now; + + if (rss->p_remain == 0) { + // move to the next block + ++rss->p_block; + if (rss->p_block < rss->tgt->count) { + rss->p_remain = (rss->tgt->pos[rss->p_block*2+1] - rss->tgt->pos[rss->p_block*2]) * BLOCKSIZE; + check_lseek(rss->fd, rss->tgt->pos[rss->p_block*2] * BLOCKSIZE, SEEK_SET); + } else { + // we can't write any more; return how many bytes have + // been written so far. + return written; + } + } + } + + return written; +} + +// All of the data for all the 'new' transfers is contained in one +// file in the update package, concatenated together in the order in +// which transfers.list will need it. We want to stream it out of the +// archive (it's compressed) without writing it to a temp file, but we +// can't write each section until it's that transfer's turn to go. +// +// To achieve this, we expand the new data from the archive in a +// background thread, and block that threads 'receive uncompressed +// data' function until the main thread has reached a point where we +// want some new data to be written. We signal the background thread +// with the destination for the data and block the main thread, +// waiting for the background thread to complete writing that section. +// Then it signals the main thread to wake up and goes back to +// blocking waiting for a transfer. +// +// NewThreadInfo is the struct used to pass information back and forth +// between the two threads. When the main thread wants some data +// written, it sets rss to the destination location and signals the +// condition. When the background thread is done writing, it clears +// rss and signals the condition again. + +typedef struct { + ZipArchive* za; + const ZipEntry* entry; + + RangeSinkState* rss; + + pthread_mutex_t mu; + pthread_cond_t cv; +} NewThreadInfo; + +static bool receive_new_data(const unsigned char* data, int size, void* cookie) { + NewThreadInfo* nti = (NewThreadInfo*) cookie; + + while (size > 0) { + // Wait for nti->rss to be non-NULL, indicating some of this + // data is wanted. + pthread_mutex_lock(&nti->mu); + while (nti->rss == NULL) { + pthread_cond_wait(&nti->cv, &nti->mu); + } + pthread_mutex_unlock(&nti->mu); + + // At this point nti->rss is set, and we own it. The main + // thread is waiting for it to disappear from nti. + ssize_t written = RangeSinkWrite(data, size, nti->rss); + data += written; + size -= written; + + if (nti->rss->p_block == nti->rss->tgt->count) { + // we have written all the bytes desired by this rss. + + pthread_mutex_lock(&nti->mu); + nti->rss = NULL; + pthread_cond_broadcast(&nti->cv); + pthread_mutex_unlock(&nti->mu); + } + } + + return true; +} + +static void* unzip_new_data(void* cookie) { + NewThreadInfo* nti = (NewThreadInfo*) cookie; + mzProcessZipEntryContents(nti->za, nti->entry, receive_new_data, nti); + return NULL; +} + +// args: +// - block device (or file) to modify in-place +// - transfer list (blob) +// - new data stream (filename within package.zip) +// - patch stream (filename within package.zip, must be uncompressed) + +Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[]) { + Value* blockdev_filename; + Value* transfer_list; + Value* new_data_fn; + Value* patch_data_fn; + bool success = false; + + if (ReadValueArgs(state, argv, 4, &blockdev_filename, &transfer_list, + &new_data_fn, &patch_data_fn) < 0) { + return NULL; + } + + if (blockdev_filename->type != VAL_STRING) { + ErrorAbort(state, "blockdev_filename argument to %s must be string", name); + goto done; + } + if (transfer_list->type != VAL_BLOB) { + ErrorAbort(state, "transfer_list argument to %s must be blob", name); + goto done; + } + if (new_data_fn->type != VAL_STRING) { + ErrorAbort(state, "new_data_fn argument to %s must be string", name); + goto done; + } + if (patch_data_fn->type != VAL_STRING) { + ErrorAbort(state, "patch_data_fn argument to %s must be string", name); + goto done; + } + + UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); + FILE* cmd_pipe = ui->cmd_pipe; + + ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; + + const ZipEntry* patch_entry = mzFindZipEntry(za, patch_data_fn->data); + if (patch_entry == NULL) { + ErrorAbort(state, "%s(): no file \"%s\" in package", name, patch_data_fn->data); + goto done; + } + + uint8_t* patch_start = ((UpdaterInfo*)(state->cookie))->package_zip_addr + + mzGetZipEntryOffset(patch_entry); + + const ZipEntry* new_entry = mzFindZipEntry(za, new_data_fn->data); + if (new_entry == NULL) { + ErrorAbort(state, "%s(): no file \"%s\" in package", name, new_data_fn->data); + goto done; + } + + // The transfer list is a text file containing commands to + // transfer data from one place to another on the target + // partition. We parse it and execute the commands in order: + // + // zero [rangeset] + // - fill the indicated blocks with zeros + // + // new [rangeset] + // - fill the blocks with data read from the new_data file + // + // bsdiff patchstart patchlen [src rangeset] [tgt rangeset] + // imgdiff patchstart patchlen [src rangeset] [tgt rangeset] + // - read the source blocks, apply a patch, write result to + // target blocks. bsdiff or imgdiff specifies the type of + // patch. + // + // move [src rangeset] [tgt rangeset] + // - copy data from source blocks to target blocks (no patch + // needed; rangesets are the same size) + // + // erase [rangeset] + // - mark the given blocks as empty + // + // The creator of the transfer list will guarantee that no block + // is read (ie, used as the source for a patch or move) after it + // has been written. + // + // Within one command the source and target ranges may overlap so + // in general we need to read the entire source into memory before + // writing anything to the target blocks. + // + // All the patch data is concatenated into one patch_data file in + // the update package. It must be stored uncompressed because we + // memory-map it in directly from the archive. (Since patches are + // already compressed, we lose very little by not compressing + // their concatenation.) + + pthread_t new_data_thread; + NewThreadInfo nti; + nti.za = za; + nti.entry = new_entry; + nti.rss = NULL; + pthread_mutex_init(&nti.mu, NULL); + pthread_cond_init(&nti.cv, NULL); + + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + pthread_create(&new_data_thread, &attr, unzip_new_data, &nti); + + int i, j; + + char* linesave; + char* wordsave; + + int fd = open(blockdev_filename->data, O_RDWR); + if (fd < 0) { + ErrorAbort(state, "failed to open %s: %s", blockdev_filename->data, strerror(errno)); + goto done; + } + + char* line; + char* word; + + line = strtok_r(transfer_list->data, "\n", &linesave); + + // first line in transfer list is the version number; currently + // there's only version 1. + if (strcmp(line, "1") != 0) { + ErrorAbort(state, "unexpected transfer list version [%s]\n", line); + goto done; + } + + // second line in transfer list is the total number of blocks we + // expect to write. + line = strtok_r(NULL, "\n", &linesave); + int total_blocks = strtol(line, NULL, 0); + // shouldn't happen, but avoid divide by zero. + if (total_blocks == 0) ++total_blocks; + int blocks_so_far = 0; + + uint8_t* buffer = NULL; + size_t buffer_alloc = 0; + + // third and subsequent lines are all individual transfer commands. + for (line = strtok_r(NULL, "\n", &linesave); line; + line = strtok_r(NULL, "\n", &linesave)) { + char* style; + style = strtok_r(line, " ", &wordsave); + + if (strcmp("move", style) == 0) { + word = strtok_r(NULL, " ", &wordsave); + RangeSet* src = parse_range(word); + word = strtok_r(NULL, " ", &wordsave); + RangeSet* tgt = parse_range(word); + + printf(" moving %d blocks\n", src->size); + + allocate(src->size * BLOCKSIZE, &buffer, &buffer_alloc); + size_t p = 0; + for (i = 0; i < src->count; ++i) { + check_lseek(fd, src->pos[i*2] * BLOCKSIZE, SEEK_SET); + size_t sz = (src->pos[i*2+1] - src->pos[i*2]) * BLOCKSIZE; + readblock(fd, buffer+p, sz); + p += sz; + } + + p = 0; + for (i = 0; i < tgt->count; ++i) { + check_lseek(fd, tgt->pos[i*2] * BLOCKSIZE, SEEK_SET); + size_t sz = (tgt->pos[i*2+1] - tgt->pos[i*2]) * BLOCKSIZE; + writeblock(fd, buffer+p, sz); + p += sz; + } + + blocks_so_far += tgt->size; + fprintf(cmd_pipe, "set_progress %.4f\n", (double)blocks_so_far / total_blocks); + fflush(cmd_pipe); + + free(src); + free(tgt); + + } else if (strcmp("zero", style) == 0 || + (DEBUG_ERASE && strcmp("erase", style) == 0)) { + word = strtok_r(NULL, " ", &wordsave); + RangeSet* tgt = parse_range(word); + + printf(" zeroing %d blocks\n", tgt->size); + + allocate(BLOCKSIZE, &buffer, &buffer_alloc); + memset(buffer, 0, BLOCKSIZE); + for (i = 0; i < tgt->count; ++i) { + check_lseek(fd, tgt->pos[i*2] * BLOCKSIZE, SEEK_SET); + for (j = tgt->pos[i*2]; j < tgt->pos[i*2+1]; ++j) { + writeblock(fd, buffer, BLOCKSIZE); + } + } + + if (style[0] == 'z') { // "zero" but not "erase" + blocks_so_far += tgt->size; + fprintf(cmd_pipe, "set_progress %.4f\n", (double)blocks_so_far / total_blocks); + fflush(cmd_pipe); + } + + free(tgt); + } else if (strcmp("new", style) == 0) { + + word = strtok_r(NULL, " ", &wordsave); + RangeSet* tgt = parse_range(word); + + printf(" writing %d blocks of new data\n", tgt->size); + + RangeSinkState rss; + rss.fd = fd; + rss.tgt = tgt; + rss.p_block = 0; + rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE; + check_lseek(fd, tgt->pos[0] * BLOCKSIZE, SEEK_SET); + + pthread_mutex_lock(&nti.mu); + nti.rss = &rss; + pthread_cond_broadcast(&nti.cv); + while (nti.rss) { + pthread_cond_wait(&nti.cv, &nti.mu); + } + pthread_mutex_unlock(&nti.mu); + + blocks_so_far += tgt->size; + fprintf(cmd_pipe, "set_progress %.4f\n", (double)blocks_so_far / total_blocks); + fflush(cmd_pipe); + + free(tgt); + + } else if (strcmp("bsdiff", style) == 0 || + strcmp("imgdiff", style) == 0) { + word = strtok_r(NULL, " ", &wordsave); + size_t patch_offset = strtoul(word, NULL, 0); + word = strtok_r(NULL, " ", &wordsave); + size_t patch_len = strtoul(word, NULL, 0); + + word = strtok_r(NULL, " ", &wordsave); + RangeSet* src = parse_range(word); + word = strtok_r(NULL, " ", &wordsave); + RangeSet* tgt = parse_range(word); + + printf(" patching %d blocks to %d\n", src->size, tgt->size); + + // Read the source into memory. + allocate(src->size * BLOCKSIZE, &buffer, &buffer_alloc); + size_t p = 0; + for (i = 0; i < src->count; ++i) { + check_lseek(fd, src->pos[i*2] * BLOCKSIZE, SEEK_SET); + size_t sz = (src->pos[i*2+1] - src->pos[i*2]) * BLOCKSIZE; + readblock(fd, buffer+p, sz); + p += sz; + } + + Value patch_value; + patch_value.type = VAL_BLOB; + patch_value.size = patch_len; + patch_value.data = (char*)(patch_start + patch_offset); + + RangeSinkState rss; + rss.fd = fd; + rss.tgt = tgt; + rss.p_block = 0; + rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE; + check_lseek(fd, tgt->pos[0] * BLOCKSIZE, SEEK_SET); + + if (style[0] == 'i') { // imgdiff + ApplyImagePatch(buffer, src->size * BLOCKSIZE, + &patch_value, + &RangeSinkWrite, &rss, NULL, NULL); + } else { + ApplyBSDiffPatch(buffer, src->size * BLOCKSIZE, + &patch_value, 0, + &RangeSinkWrite, &rss, NULL); + } + + // We expect the output of the patcher to fill the tgt ranges exactly. + if (rss.p_block != tgt->count || rss.p_remain != 0) { + fprintf(stderr, "range sink underrun?\n"); + } + + blocks_so_far += tgt->size; + fprintf(cmd_pipe, "set_progress %.4f\n", (double)blocks_so_far / total_blocks); + fflush(cmd_pipe); + + free(src); + free(tgt); + } else if (!DEBUG_ERASE && strcmp("erase", style) == 0) { + struct stat st; + if (fstat(fd, &st) == 0 && S_ISBLK(st.st_mode)) { + word = strtok_r(NULL, " ", &wordsave); + RangeSet* tgt = parse_range(word); + + printf(" erasing %d blocks\n", tgt->size); + + for (i = 0; i < tgt->count; ++i) { + uint64_t range[2]; + // offset in bytes + range[0] = tgt->pos[i*2] * BLOCKSIZE; + // len in bytes + range[1] = (tgt->pos[i*2+1] - tgt->pos[i*2]) * BLOCKSIZE; + + if (ioctl(fd, BLKDISCARD, &range) < 0) { + printf(" blkdiscard failed: %s\n", strerror(errno)); + } + } + + free(tgt); + } else { + printf(" ignoring erase (not block device)\n"); + } + } else { + fprintf(stderr, "unknown transfer style \"%s\"\n", style); + exit(1); + } + } + + pthread_join(new_data_thread, NULL); + success = true; + + free(buffer); + printf("wrote %d blocks; expected %d\n", blocks_so_far, total_blocks); + printf("max alloc needed was %zu\n", buffer_alloc); + + done: + FreeValue(blockdev_filename); + FreeValue(transfer_list); + FreeValue(new_data_fn); + FreeValue(patch_data_fn); + return StringValue(success ? strdup("t") : strdup("")); +} + +Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) { + Value* blockdev_filename; + Value* ranges; + const uint8_t* digest = NULL; + if (ReadValueArgs(state, argv, 2, &blockdev_filename, &ranges) < 0) { + return NULL; + } + + if (blockdev_filename->type != VAL_STRING) { + ErrorAbort(state, "blockdev_filename argument to %s must be string", name); + goto done; + } + if (ranges->type != VAL_STRING) { + ErrorAbort(state, "ranges argument to %s must be string", name); + goto done; + } + + int fd = open(blockdev_filename->data, O_RDWR); + if (fd < 0) { + ErrorAbort(state, "failed to open %s: %s", blockdev_filename->data, strerror(errno)); + goto done; + } + + RangeSet* rs = parse_range(ranges->data); + uint8_t buffer[BLOCKSIZE]; + + SHA_CTX ctx; + SHA_init(&ctx); + + int i, j; + for (i = 0; i < rs->count; ++i) { + check_lseek(fd, rs->pos[i*2] * BLOCKSIZE, SEEK_SET); + for (j = rs->pos[i*2]; j < rs->pos[i*2+1]; ++j) { + readblock(fd, buffer, BLOCKSIZE); + SHA_update(&ctx, buffer, BLOCKSIZE); + } + } + digest = SHA_final(&ctx); + close(fd); + + done: + FreeValue(blockdev_filename); + FreeValue(ranges); + if (digest == NULL) { + return StringValue(strdup("")); + } else { + return StringValue(PrintSha1(digest)); + } +} + +void RegisterBlockImageFunctions() { + RegisterFunction("block_image_update", BlockImageUpdateFn); + RegisterFunction("range_sha1", RangeSha1Fn); +} diff --git a/updater/blockimg.h b/updater/blockimg.h new file mode 100644 index 000000000..2f4ad3c04 --- /dev/null +++ b/updater/blockimg.h @@ -0,0 +1,22 @@ +/* + * 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 _UPDATER_BLOCKIMG_H_ +#define _UPDATER_BLOCKIMG_H_ + +void RegisterBlockImageFunctions(); + +#endif diff --git a/updater/install.c b/updater/install.c index 198618001..cdcdb8fdb 100644 --- a/updater/install.c +++ b/updater/install.c @@ -54,7 +54,7 @@ #endif // Take a sha-1 digest and return it as a newly-allocated hex string. -static char* PrintSha1(const uint8_t* digest) { +char* PrintSha1(const uint8_t* digest) { char* buffer = malloc(SHA_DIGEST_SIZE*2 + 1); int i; const char* alphabet = "0123456789abcdef"; diff --git a/updater/updater.c b/updater/updater.c index b7af3e500..465e1238e 100644 --- a/updater/updater.c +++ b/updater/updater.c @@ -21,6 +21,7 @@ #include "edify/expr.h" #include "updater.h" #include "install.h" +#include "blockimg.h" #include "minzip/Zip.h" #include "minzip/SysUtil.h" @@ -98,6 +99,7 @@ int main(int argc, char** argv) { RegisterBuiltins(); RegisterInstallFunctions(); + RegisterBlockImageFunctions(); RegisterDeviceExtensions(); FinishRegistration(); @@ -127,6 +129,8 @@ int main(int argc, char** argv) { updater_info.cmd_pipe = cmd_pipe; updater_info.package_zip = &za; updater_info.version = atoi(version); + updater_info.package_zip_addr = map.addr; + updater_info.package_zip_len = map.length; State state; state.cookie = &updater_info; diff --git a/updater/updater.h b/updater/updater.h index d2e901141..d1dfdd05e 100644 --- a/updater/updater.h +++ b/updater/updater.h @@ -27,6 +27,9 @@ typedef struct { FILE* cmd_pipe; ZipArchive* package_zip; int version; + + uint8_t* package_zip_addr; + size_t package_zip_len; } UpdaterInfo; extern struct selabel_handle *sehandle; -- cgit v1.2.3 From adad8ec045e118215b1745c2ba86031c7f2c73d2 Mon Sep 17 00:00:00 2001 From: Doug Zongker Date: Fri, 22 Aug 2014 14:53:43 -0700 Subject: remove code for original block OTA mechanism Superseded by newer code. Bug: 16984795 Change-Id: I70c1d29dc03287b06ea909d17f729ec51ccb0344 --- updater/Android.mk | 2 - updater/install.c | 213 ++--------------------------------------------------- 2 files changed, 6 insertions(+), 209 deletions(-) diff --git a/updater/Android.mk b/updater/Android.mk index b183c9221..a3a900a80 100644 --- a/updater/Android.mk +++ b/updater/Android.mk @@ -36,8 +36,6 @@ LOCAL_STATIC_LIBRARIES += libcutils liblog libstdc++ libc LOCAL_STATIC_LIBRARIES += libselinux LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. -LOCAL_STATIC_LIBRARIES += libsyspatch libxz libxdelta3 - # Each library in TARGET_RECOVERY_UPDATER_LIBS should have a function # named "Register_()". Here we emit a little C function that # gets #included by updater.c. It calls all those registration diff --git a/updater/install.c b/updater/install.c index cdcdb8fdb..dad0d08c9 100644 --- a/updater/install.c +++ b/updater/install.c @@ -45,7 +45,6 @@ #include "mtdutils/mounts.h" #include "mtdutils/mtdutils.h" #include "updater.h" -#include "syspatch.h" #include "install.h" #ifdef USE_EXT4 @@ -464,68 +463,6 @@ 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)); - map->total_blocks = 0; - - int i; - for (i = 0; i < map->region_count; ++i) { - map->regions[i] = strtoul(p, &p, 0); - map->total_blocks += map->regions[i]; - } - - return map; -} - -static FILE* mapwrite_cmd_pipe; - -static void progress_cb(long done, long total) { - if (total > 0) { - double frac = (double)done / total; - fprintf(mapwrite_cmd_pipe, "set_progress %f\n", frac); - fflush(mapwrite_cmd_pipe); - } -} - - - -bool MapWriter(const unsigned char* data, int dataLen, void* cookie) { - return write_with_map(data, dataLen, (MapState*) cookie, progress_cb) == dataLen; -} - -// package_extract_file(package_path, destination_path, map_path) -// or // package_extract_file(package_path, destination_path) // or // package_extract_file(package_path) @@ -533,33 +470,22 @@ bool MapWriter(const unsigned char* data, int dataLen, void* cookie) { // function (the char* returned is actually a FileContents*). Value* PackageExtractFileFn(const char* name, State* state, int argc, Expr* argv[]) { - if (argc < 1 || argc > 3) { - return ErrorAbort(state, "%s() expects 1 or 2 or 3 args, got %d", + if (argc < 1 || argc > 2) { + return ErrorAbort(state, "%s() expects 1 or 2 args, got %d", name, argc); } bool success = false; UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); - mapwrite_cmd_pipe = ui->cmd_pipe; - 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. + if (argc == 2) { + // The two-argument version extracts to a file. ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; char* zip_path; char* dest_path; - 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; - } + if (ReadArgs(state, argv, 2, &zip_path, &dest_path) < 0) return NULL; const ZipEntry* entry = mzFindZipEntry(za, zip_path); if (entry == NULL) { @@ -573,26 +499,12 @@ Value* PackageExtractFileFn(const char* name, State* state, name, dest_path, strerror(errno)); goto done2; } - 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)); - } + 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 @@ -1192,118 +1104,6 @@ Value* ApplyPatchSpaceFn(const char* name, State* state, return StringValue(strdup(CacheSizeCheck(bytes) ? "" : "t")); } -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 != 6) { - return ErrorAbort(state, "%s(): expected 6 args, got %d", name, argc); - } - - char* filename; - 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, 6, &filename, - &target_mapfilename, &target_sha1, - &init_mapfilename, &init_sha1, &patch_filename) < 0) { - return NULL; - } - - UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); - mapwrite_cmd_pipe = ui->cmd_pipe; - - if (ParseSha1(target_sha1, target_digest) != 0) { - printf("%s(): failed to parse '%s' as target SHA-1", name, target_sha1); - memset(target_digest, 0, SHA_DIGEST_SIZE); - } - if (ParseSha1(init_sha1, init_digest) != 0) { - printf("%s(): failed to parse '%s' as init SHA-1", name, init_sha1); - memset(init_digest, 0, SHA_DIGEST_SIZE); - } - - ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; - FILE* src = fopen(filename, "r"); - - 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 (CheckMappedFileSha1(src, init_map, init_digest)) { - // If the partition contents match the init_digest, then we need to apply the patch. - - rewind(src); - - 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); - } - - FILE* tgt = fopen(filename, "r+"); - - int ret = syspatch(src, init_map, patch_data, patch_len, tgt, target_map, progress_cb); - - fclose(src); - fclose(tgt); - - 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(target_sha1); - free(target_mapfilename); - free(init_sha1); - free(init_mapfilename); - free(patch_filename); - return StringValue(filename); - -} - // apply_patch(file, size, init_sha1, tgt_sha1, patch) Value* ApplyPatchFn(const char* name, State* state, int argc, Expr* argv[]) { @@ -1738,7 +1538,6 @@ void RegisterInstallFunctions() { RegisterFunction("apply_patch_space", ApplyPatchSpaceFn); RegisterFunction("wipe_block_device", WipeBlockDeviceFn); - RegisterFunction("syspatch", SysPatchFn); RegisterFunction("read_file", ReadFileFn); RegisterFunction("sha1_check", Sha1CheckFn); -- cgit v1.2.3 From 52ae67d659e92bdd32c7c2a6b4d09dfe94e987fe Mon Sep 17 00:00:00 2001 From: Doug Zongker Date: Mon, 8 Sep 2014 12:22:09 -0700 Subject: support for version 2 of block image diffs In version 2 of block image diffs, we support a new command to load data from the image and store it in the "stash table" and then subsequently use entries in the stash table to fill in missing bits of source data we're not allowed to read when doing move/bsdiff/imgdiff commands. This leads to smaller update packages because we can break cycles in the ordering of how pieces are updated by storing data away and using it later, rather than not using the data as input to the patch system at all. This comes at the cost of the RAM or scratch disk needed to store the data. The implementation is backwards compatible; it can still handle the existing version 1 of the transfer file format. Change-Id: I7fafe741d86b92d82d46feb2939ecf5a3890dc64 --- updater/blockimg.c | 254 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 209 insertions(+), 45 deletions(-) diff --git a/updater/blockimg.c b/updater/blockimg.c index c3319c973..302689313 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -61,7 +61,7 @@ static RangeSet* parse_range(char* text) { RangeSet* out = malloc(sizeof(RangeSet) + num * sizeof(int)); if (out == NULL) { - fprintf(stderr, "failed to allocate range of %lu bytes\n", + fprintf(stderr, "failed to allocate range of %zu bytes\n", sizeof(RangeSet) + num * sizeof(int)); exit(1); } @@ -245,6 +245,133 @@ static void* unzip_new_data(void* cookie) { return NULL; } +// Do a source/target load for move/bsdiff/imgdiff in version 1. +// 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect +// to parse the remainder of the string as: +// +// +// +// The source range is loaded into the provided buffer, reallocating +// it to make it larger if necessary. The target ranges are returned +// in *tgt, if tgt is non-NULL. + +static void LoadSrcTgtVersion1(char* wordsave, RangeSet** tgt, int* src_blocks, + uint8_t** buffer, size_t* buffer_alloc, int fd) { + char* word; + + word = strtok_r(NULL, " ", &wordsave); + RangeSet* src = parse_range(word); + + if (tgt != NULL) { + word = strtok_r(NULL, " ", &wordsave); + *tgt = parse_range(word); + } + + allocate(src->size * BLOCKSIZE, buffer, buffer_alloc); + size_t p = 0; + int i; + for (i = 0; i < src->count; ++i) { + check_lseek(fd, (off64_t)src->pos[i*2] * BLOCKSIZE, SEEK_SET); + size_t sz = (src->pos[i*2+1] - src->pos[i*2]) * BLOCKSIZE; + readblock(fd, *buffer+p, sz); + p += sz; + } + + *src_blocks = src->size; + free(src); +} + +static void MoveRange(uint8_t* dest, RangeSet* locs, const uint8_t* source) { + // source contains packed data, which we want to move to the + // locations given in *locs in the dest buffer. source and dest + // may be the same buffer. + + int start = locs->size; + int i; + for (i = locs->count-1; i >= 0; --i) { + int blocks = locs->pos[i*2+1] - locs->pos[i*2]; + start -= blocks; + memmove(dest + (locs->pos[i*2] * BLOCKSIZE), source + (start * BLOCKSIZE), + blocks * BLOCKSIZE); + } +} + +// Do a source/target load for move/bsdiff/imgdiff in version 2. +// 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect +// to parse the remainder of the string as one of: +// +// +// (loads data from source image only) +// +// - <[stash_id:stash_range] ...> +// (loads data from stashes only) +// +// <[stash_id:stash_range] ...> +// (loads data from both source image and stashes) +// +// On return, buffer is filled with the loaded source data (rearranged +// and combined with stashed data as necessary). buffer may be +// reallocated if needed to accommodate the source data. *tgt is the +// target RangeSet. Any stashes required are taken from stash_table +// and free()'d after being used. + +static void LoadSrcTgtVersion2(char* wordsave, RangeSet** tgt, int* src_blocks, + uint8_t** buffer, size_t* buffer_alloc, int fd, + uint8_t** stash_table) { + char* word; + + if (tgt != NULL) { + word = strtok_r(NULL, " ", &wordsave); + *tgt = parse_range(word); + } + + word = strtok_r(NULL, " ", &wordsave); + *src_blocks = strtol(word, NULL, 0); + + allocate(*src_blocks * BLOCKSIZE, buffer, buffer_alloc); + + word = strtok_r(NULL, " ", &wordsave); + if (word[0] == '-' && word[1] == '\0') { + // no source ranges, only stashes + } else { + RangeSet* src = parse_range(word); + + size_t p = 0; + int i; + for (i = 0; i < src->count; ++i) { + check_lseek(fd, (off64_t)src->pos[i*2] * BLOCKSIZE, SEEK_SET); + size_t sz = (src->pos[i*2+1] - src->pos[i*2]) * BLOCKSIZE; + readblock(fd, *buffer+p, sz); + p += sz; + } + free(src); + + word = strtok_r(NULL, " ", &wordsave); + if (word == NULL) { + // no stashes, only source range + return; + } + + RangeSet* locs = parse_range(word); + MoveRange(*buffer, locs, *buffer); + } + + while ((word = strtok_r(NULL, " ", &wordsave)) != NULL) { + // Each word is a an index into the stash table, a colon, and + // then a rangeset describing where in the source block that + // stashed data should go. + char* colonsave = NULL; + char* colon = strtok_r(word, ":", &colonsave); + int stash_id = strtol(colon, NULL, 0); + colon = strtok_r(NULL, ":", &colonsave); + RangeSet* locs = parse_range(colon); + MoveRange(*buffer, locs, stash_table[stash_id]); + free(stash_table[stash_id]); + stash_table[stash_id] = NULL; + free(locs); + } +} + // args: // - block device (or file) to modify in-place // - transfer list (blob) @@ -311,23 +438,33 @@ Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[] // new [rangeset] // - fill the blocks with data read from the new_data file // - // bsdiff patchstart patchlen [src rangeset] [tgt rangeset] - // imgdiff patchstart patchlen [src rangeset] [tgt rangeset] - // - read the source blocks, apply a patch, write result to - // target blocks. bsdiff or imgdiff specifies the type of - // patch. - // - // move [src rangeset] [tgt rangeset] - // - copy data from source blocks to target blocks (no patch - // needed; rangesets are the same size) - // // erase [rangeset] // - mark the given blocks as empty // + // move <...> + // bsdiff <...> + // imgdiff <...> + // - read the source blocks, apply a patch (or not in the + // case of move), write result to target blocks. bsdiff or + // imgdiff specifies the type of patch; move means no patch + // at all. + // + // The format of <...> differs between versions 1 and 2; + // see the LoadSrcTgtVersion{1,2}() functions for a + // description of what's expected. + // + // stash + // - (version 2 only) load the given source range and stash + // the data in the given slot of the stash table. + // // The creator of the transfer list will guarantee that no block // is read (ie, used as the source for a patch or move) after it // has been written. // + // In version 2, the creator will guarantee that a given stash is + // loaded (with a stash command) before it's used in a + // move/bsdiff/imgdiff command. + // // Within one command the source and target ranges may overlap so // in general we need to read the entire source into memory before // writing anything to the target blocks. @@ -379,12 +516,18 @@ Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[] line = strtok_r(transfer_list, "\n", &linesave); + int version; // first line in transfer list is the version number; currently // there's only version 1. - if (strcmp(line, "1") != 0) { + if (strcmp(line, "1") == 0) { + version = 1; + } else if (strcmp(line, "2") == 0) { + version = 2; + } else { ErrorAbort(state, "unexpected transfer list version [%s]\n", line); goto done; } + printf("blockimg version is %d\n", version); // second line in transfer list is the total number of blocks we // expect to write. @@ -394,33 +537,49 @@ Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[] if (total_blocks == 0) ++total_blocks; int blocks_so_far = 0; + uint8_t** stash_table = NULL; + if (version >= 2) { + // Next line is how many stash entries are needed simultaneously. + line = strtok_r(NULL, "\n", &linesave); + int stash_entries = strtol(line, NULL, 0); + + stash_table = (uint8_t**) calloc(stash_entries, sizeof(uint8_t*)); + if (stash_table == NULL) { + fprintf(stderr, "failed to allocate %d-entry stash table\n", stash_entries); + exit(1); + } + + // Next line is the maximum number of blocks that will be + // stashed simultaneously. This could be used to verify that + // enough memory or scratch disk space is available. + line = strtok_r(NULL, "\n", &linesave); + int stash_max_blocks = strtol(line, NULL, 0); + } + uint8_t* buffer = NULL; size_t buffer_alloc = 0; // third and subsequent lines are all individual transfer commands. for (line = strtok_r(NULL, "\n", &linesave); line; line = strtok_r(NULL, "\n", &linesave)) { + char* style; style = strtok_r(line, " ", &wordsave); if (strcmp("move", style) == 0) { - word = strtok_r(NULL, " ", &wordsave); - RangeSet* src = parse_range(word); - word = strtok_r(NULL, " ", &wordsave); - RangeSet* tgt = parse_range(word); + RangeSet* tgt; + int src_blocks; + if (version == 1) { + LoadSrcTgtVersion1(wordsave, &tgt, &src_blocks, + &buffer, &buffer_alloc, fd); + } else if (version == 2) { + LoadSrcTgtVersion2(wordsave, &tgt, &src_blocks, + &buffer, &buffer_alloc, fd, stash_table); + } - printf(" moving %d blocks\n", src->size); + printf(" moving %d blocks\n", src_blocks); - allocate(src->size * BLOCKSIZE, &buffer, &buffer_alloc); size_t p = 0; - for (i = 0; i < src->count; ++i) { - check_lseek(fd, (off64_t)src->pos[i*2] * BLOCKSIZE, SEEK_SET); - size_t sz = (src->pos[i*2+1] - src->pos[i*2]) * BLOCKSIZE; - readblock(fd, buffer+p, sz); - p += sz; - } - - p = 0; for (i = 0; i < tgt->count; ++i) { check_lseek(fd, (off64_t)tgt->pos[i*2] * BLOCKSIZE, SEEK_SET); size_t sz = (tgt->pos[i*2+1] - tgt->pos[i*2]) * BLOCKSIZE; @@ -432,9 +591,20 @@ Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[] fprintf(cmd_pipe, "set_progress %.4f\n", (double)blocks_so_far / total_blocks); fflush(cmd_pipe); - free(src); free(tgt); + } else if (strcmp("stash", style) == 0) { + word = strtok_r(NULL, " ", &wordsave); + int stash_id = strtol(word, NULL, 0); + int src_blocks; + size_t stash_alloc = 0; + + // Even though the "stash" style only appears in version + // 2, the version 1 source loader happens to do exactly + // what we want to read data into the stash_table. + LoadSrcTgtVersion1(wordsave, NULL, &src_blocks, + stash_table + stash_id, &stash_alloc, fd); + } else if (strcmp("zero", style) == 0 || (DEBUG_ERASE && strcmp("erase", style) == 0)) { word = strtok_r(NULL, " ", &wordsave); @@ -493,23 +663,18 @@ Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[] word = strtok_r(NULL, " ", &wordsave); size_t patch_len = strtoul(word, NULL, 0); - word = strtok_r(NULL, " ", &wordsave); - RangeSet* src = parse_range(word); - word = strtok_r(NULL, " ", &wordsave); - RangeSet* tgt = parse_range(word); - - printf(" patching %d blocks to %d\n", src->size, tgt->size); - - // Read the source into memory. - allocate(src->size * BLOCKSIZE, &buffer, &buffer_alloc); - size_t p = 0; - for (i = 0; i < src->count; ++i) { - check_lseek(fd, (off64_t)src->pos[i*2] * BLOCKSIZE, SEEK_SET); - size_t sz = (src->pos[i*2+1] - src->pos[i*2]) * BLOCKSIZE; - readblock(fd, buffer+p, sz); - p += sz; + RangeSet* tgt; + int src_blocks; + if (version == 1) { + LoadSrcTgtVersion1(wordsave, &tgt, &src_blocks, + &buffer, &buffer_alloc, fd); + } else if (version == 2) { + LoadSrcTgtVersion2(wordsave, &tgt, &src_blocks, + &buffer, &buffer_alloc, fd, stash_table); } + printf(" patching %d blocks to %d\n", src_blocks, tgt->size); + Value patch_value; patch_value.type = VAL_BLOB; patch_value.size = patch_len; @@ -523,11 +688,11 @@ Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[] check_lseek(fd, (off64_t)tgt->pos[0] * BLOCKSIZE, SEEK_SET); if (style[0] == 'i') { // imgdiff - ApplyImagePatch(buffer, src->size * BLOCKSIZE, + ApplyImagePatch(buffer, src_blocks * BLOCKSIZE, &patch_value, &RangeSinkWrite, &rss, NULL, NULL); } else { - ApplyBSDiffPatch(buffer, src->size * BLOCKSIZE, + ApplyBSDiffPatch(buffer, src_blocks * BLOCKSIZE, &patch_value, 0, &RangeSinkWrite, &rss, NULL); } @@ -541,7 +706,6 @@ Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[] fprintf(cmd_pipe, "set_progress %.4f\n", (double)blocks_so_far / total_blocks); fflush(cmd_pipe); - free(src); free(tgt); } else if (!DEBUG_ERASE && strcmp("erase", style) == 0) { struct stat st; -- cgit v1.2.3 From 1d30c2fe19fdbdfd6e5f52102247cf01b87e586e Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Tue, 16 Sep 2014 14:53:39 -0700 Subject: Use the correct fuse_init_out structure size. Kernel 2.6.16 is the first stable kernel with struct fuse_init_out defined (fuse version 7.6). The structure is the same from 7.6 through 7.22. Beginning with 7.23, the structure increased in size and added new parameters. If the kernel only works on minor revs older than or equal to 22, then use the older structure size since this code only uses the 7.22 version of the structure. Change-Id: I00d7530e01e6b4718dcd04ad2484959d12ef4a65 --- fuse_sideload.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/fuse_sideload.c b/fuse_sideload.c index ab91defbf..4e11e01e4 100644 --- a/fuse_sideload.c +++ b/fuse_sideload.c @@ -53,6 +53,7 @@ #include #include #include +#include #include #include #include @@ -117,15 +118,40 @@ static void fuse_reply(struct fuse_data* fd, __u64 unique, const void *data, siz static int handle_init(void* data, struct fuse_data* fd, const struct fuse_in_header* hdr) { const struct fuse_init_in* req = data; struct fuse_init_out out; + size_t fuse_struct_size; + + + /* Kernel 2.6.16 is the first stable kernel with struct fuse_init_out + * defined (fuse version 7.6). The structure is the same from 7.6 through + * 7.22. Beginning with 7.23, the structure increased in size and added + * new parameters. + */ + if (req->major != FUSE_KERNEL_VERSION || req->minor < 6) { + printf("Fuse kernel version mismatch: Kernel version %d.%d, Expected at least %d.6", + req->major, req->minor, FUSE_KERNEL_VERSION); + return -1; + } + + out.minor = MIN(req->minor, FUSE_KERNEL_MINOR_VERSION); + fuse_struct_size = sizeof(out); +#if defined(FUSE_COMPAT_22_INIT_OUT_SIZE) + /* FUSE_KERNEL_VERSION >= 23. */ + + /* If the kernel only works on minor revs older than or equal to 22, + * then use the older structure size since this code only uses the 7.22 + * version of the structure. */ + if (req->minor <= 22) { + fuse_struct_size = FUSE_COMPAT_22_INIT_OUT_SIZE; + } +#endif out.major = FUSE_KERNEL_VERSION; - out.minor = FUSE_KERNEL_MINOR_VERSION; out.max_readahead = req->max_readahead; out.flags = 0; out.max_background = 32; out.congestion_threshold = 32; out.max_write = 4096; - fuse_reply(fd, hdr->unique, &out, sizeof(out)); + fuse_reply(fd, hdr->unique, &out, fuse_struct_size); return NO_STATUS; } -- cgit v1.2.3 From db8c959049d21fe63e48c270f1fc12b32a0cf0ec Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 23 Sep 2014 10:50:55 -0700 Subject: Copy epoll(2) changes to minadb. Bug: 17588403 Change-Id: Ib3525824ff09330bd9d6f9e96d662e5a55a20ec2 --- minadbd/fdevent.c | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/minadbd/fdevent.c b/minadbd/fdevent.c index 5c374a71b..b62781753 100644 --- a/minadbd/fdevent.c +++ b/minadbd/fdevent.c @@ -102,8 +102,7 @@ static fdevent list_pending = { static fdevent **fd_table = 0; static int fd_table_max = 0; -#ifdef CRAPTASTIC -//HAVE_EPOLL +#ifdef __linux__ #include @@ -111,32 +110,16 @@ static int epoll_fd = -1; static void fdevent_init() { - /* XXX: what's a good size for the passed in hint? */ - epoll_fd = epoll_create(256); - - if(epoll_fd < 0) { + epoll_fd = epoll_create1(EPOLL_CLOEXEC); + if(epoll_fd == -1) { perror("epoll_create() failed"); exit(1); } - - /* mark for close-on-exec */ - fcntl(epoll_fd, F_SETFD, FD_CLOEXEC); } static void fdevent_connect(fdevent *fde) { - struct epoll_event ev; - - memset(&ev, 0, sizeof(ev)); - ev.events = 0; - ev.data.ptr = fde; - -#if 0 - if(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fde->fd, &ev)) { - perror("epoll_ctl() failed\n"); - exit(1); - } -#endif + // Nothing to do here. fdevent_update will handle the EPOLL_CTL_ADD. } static void fdevent_disconnect(fdevent *fde) -- cgit v1.2.3 From f420f8eb6327a69954b8565d000060133439c39e Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Thu, 25 Sep 2014 15:12:19 -0700 Subject: Revert "Copy epoll(2) changes to minadb." This reverts commit db8c959049d21fe63e48c270f1fc12b32a0cf0ec. --- minadbd/fdevent.c | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/minadbd/fdevent.c b/minadbd/fdevent.c index b62781753..5c374a71b 100644 --- a/minadbd/fdevent.c +++ b/minadbd/fdevent.c @@ -102,7 +102,8 @@ static fdevent list_pending = { static fdevent **fd_table = 0; static int fd_table_max = 0; -#ifdef __linux__ +#ifdef CRAPTASTIC +//HAVE_EPOLL #include @@ -110,16 +111,32 @@ static int epoll_fd = -1; static void fdevent_init() { - epoll_fd = epoll_create1(EPOLL_CLOEXEC); - if(epoll_fd == -1) { + /* XXX: what's a good size for the passed in hint? */ + epoll_fd = epoll_create(256); + + if(epoll_fd < 0) { perror("epoll_create() failed"); exit(1); } + + /* mark for close-on-exec */ + fcntl(epoll_fd, F_SETFD, FD_CLOEXEC); } static void fdevent_connect(fdevent *fde) { - // Nothing to do here. fdevent_update will handle the EPOLL_CTL_ADD. + struct epoll_event ev; + + memset(&ev, 0, sizeof(ev)); + ev.events = 0; + ev.data.ptr = fde; + +#if 0 + if(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fde->fd, &ev)) { + perror("epoll_ctl() failed\n"); + exit(1); + } +#endif } static void fdevent_disconnect(fdevent *fde) -- cgit v1.2.3 From 13f21c2bc7bc5ca640795e4c2796c324c786c7ae Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Thu, 16 Oct 2014 17:39:31 -0700 Subject: More test makefile cleanup. Global variables kill. No need to manually link gtest, and that causes problems with libc++. Change-Id: If804cdd436cf1addfa9a777708efbc37c27770b6 --- tests/Android.mk | 47 +++++++++++++++++++++++------------------------ 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/tests/Android.mk b/tests/Android.mk index 4d99d5249..02a272a24 100644 --- a/tests/Android.mk +++ b/tests/Android.mk @@ -1,26 +1,25 @@ -# Build the unit tests. -LOCAL_PATH := $(call my-dir) -include $(CLEAR_VARS) - -# Build the unit tests. -test_src_files := \ - asn1_decoder_test.cpp +# +# 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. +# -shared_libraries := \ - liblog \ - libcutils - -static_libraries := \ - libgtest \ - libgtest_main \ - libverifier +LOCAL_PATH := $(call my-dir) -$(foreach file,$(test_src_files), \ - $(eval include $(CLEAR_VARS)) \ - $(eval LOCAL_SHARED_LIBRARIES := $(shared_libraries)) \ - $(eval LOCAL_STATIC_LIBRARIES := $(static_libraries)) \ - $(eval LOCAL_SRC_FILES := $(file)) \ - $(eval LOCAL_MODULE := $(notdir $(file:%.cpp=%))) \ - $(eval LOCAL_C_INCLUDES := $(LOCAL_PATH)/..) \ - $(eval include $(BUILD_NATIVE_TEST)) \ -) \ No newline at end of file +include $(CLEAR_VARS) +LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk +LOCAL_STATIC_LIBRARIES := libverifier +LOCAL_SRC_FILES := asn1_decoder_test.cpp +LOCAL_MODULE := asn1_decoder_test +LOCAL_C_INCLUDES := $(LOCAL_PATH)/.. +include $(BUILD_NATIVE_TEST) -- cgit v1.2.3 From b5b43043fa71f9cb620ddd02ec2bc98eced5a6ce Mon Sep 17 00:00:00 2001 From: Badhri Jagan Sridharan Date: Mon, 27 Oct 2014 18:32:35 -0700 Subject: adbd: Support for new f_fs descriptor format The patch "[RFC] usb: gadget: f_fs: Add flags to descriptors block" marks the current usb_functionfs_descs_head format deprecated and introduces support for sending SuperSpeed descriptors. This CL makes adbd to send Descriptors in the new format. Adbd would fall back to the old format, if kernel is not able to recognize the new format. This is done to prevent adbd from breaking in the older versions of the kernel. Bug: 17394972 Change-Id: I1acf684ef8a4dcc612ac20b5abe1e27b43901031 Signed-off-by: Badhri Jagan Sridharan --- minadbd/usb_linux_client.c | 161 ++++++++++++++++++++++++++++++--------------- 1 file changed, 107 insertions(+), 54 deletions(-) diff --git a/minadbd/usb_linux_client.c b/minadbd/usb_linux_client.c index 29bab1558..e7d3c4854 100644 --- a/minadbd/usb_linux_client.c +++ b/minadbd/usb_linux_client.c @@ -52,7 +52,35 @@ struct usb_handle int bulk_in; /* "in" from the host's perspective => sink for adbd */ }; -static const struct { +struct func_desc { + struct usb_interface_descriptor intf; + struct usb_endpoint_descriptor_no_audio source; + struct usb_endpoint_descriptor_no_audio sink; +} __attribute__((packed)); + +struct desc_v1 { + struct usb_functionfs_descs_head_v1 { + __le32 magic; + __le32 length; + __le32 fs_count; + __le32 hs_count; + } __attribute__((packed)) header; + struct func_desc fs_descs, hs_descs; +} __attribute__((packed)); + +struct desc_v2 { + struct usb_functionfs_descs_head_v2 { + __le32 magic; + __le32 length; + __le32 flags; + __le32 fs_count; + __le32 hs_count; + __le32 ss_count; + } __attribute__((packed)) header; + struct func_desc fs_descs, hs_descs; +} __attribute__((packed)); + +/*static const struct { struct usb_functionfs_descs_head header; struct { struct usb_interface_descriptor intf; @@ -66,57 +94,60 @@ static const struct { .fs_count = 3, .hs_count = 3, }, - .fs_descs = { - .intf = { - .bLength = sizeof(descriptors.fs_descs.intf), - .bDescriptorType = USB_DT_INTERFACE, - .bInterfaceNumber = 0, - .bNumEndpoints = 2, - .bInterfaceClass = ADB_CLASS, - .bInterfaceSubClass = ADB_SUBCLASS, - .bInterfaceProtocol = ADB_PROTOCOL, - .iInterface = 1, /* first string from the provided table */ - }, - .source = { - .bLength = sizeof(descriptors.fs_descs.source), - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 1 | USB_DIR_OUT, - .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = MAX_PACKET_SIZE_FS, - }, - .sink = { - .bLength = sizeof(descriptors.fs_descs.sink), - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 2 | USB_DIR_IN, - .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = MAX_PACKET_SIZE_FS, - }, + +*/ + +struct func_desc fs_descriptors = { + .intf = { + .bLength = sizeof(fs_descriptors.intf), + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bNumEndpoints = 2, + .bInterfaceClass = ADB_CLASS, + .bInterfaceSubClass = ADB_SUBCLASS, + .bInterfaceProtocol = ADB_PROTOCOL, + .iInterface = 1, /* first string from the provided table */ + }, + .source = { + .bLength = sizeof(fs_descriptors.source), + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 1 | USB_DIR_OUT, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = MAX_PACKET_SIZE_FS, + }, + .sink = { + .bLength = sizeof(fs_descriptors.sink), + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 2 | USB_DIR_IN, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = MAX_PACKET_SIZE_FS, + }, +}; + +struct func_desc hs_descriptors = { + .intf = { + .bLength = sizeof(hs_descriptors.intf), + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bNumEndpoints = 2, + .bInterfaceClass = ADB_CLASS, + .bInterfaceSubClass = ADB_SUBCLASS, + .bInterfaceProtocol = ADB_PROTOCOL, + .iInterface = 1, /* first string from the provided table */ + }, + .source = { + .bLength = sizeof(hs_descriptors.source), + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 1 | USB_DIR_OUT, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = MAX_PACKET_SIZE_HS, }, - .hs_descs = { - .intf = { - .bLength = sizeof(descriptors.hs_descs.intf), - .bDescriptorType = USB_DT_INTERFACE, - .bInterfaceNumber = 0, - .bNumEndpoints = 2, - .bInterfaceClass = ADB_CLASS, - .bInterfaceSubClass = ADB_SUBCLASS, - .bInterfaceProtocol = ADB_PROTOCOL, - .iInterface = 1, /* first string from the provided table */ - }, - .source = { - .bLength = sizeof(descriptors.hs_descs.source), - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 1 | USB_DIR_OUT, - .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = MAX_PACKET_SIZE_HS, - }, - .sink = { - .bLength = sizeof(descriptors.hs_descs.sink), - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 2 | USB_DIR_IN, - .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = MAX_PACKET_SIZE_HS, - }, + .sink = { + .bLength = sizeof(hs_descriptors.sink), + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 2 | USB_DIR_IN, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = MAX_PACKET_SIZE_HS, }, }; @@ -267,6 +298,18 @@ static void usb_adb_init() static void init_functionfs(struct usb_handle *h) { ssize_t ret; + struct desc_v1 v1_descriptor; + struct desc_v2 v2_descriptor; + + v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2); + v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor)); + v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC; + v2_descriptor.header.fs_count = 3; + v2_descriptor.header.hs_count = 3; + v2_descriptor.header.ss_count = 0; + v2_descriptor.fs_descs = fs_descriptors; + v2_descriptor.hs_descs = hs_descriptors; + D("OPENING %s\n", USB_FFS_ADB_EP0); h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR); @@ -275,10 +318,20 @@ static void init_functionfs(struct usb_handle *h) goto err; } - ret = adb_write(h->control, &descriptors, sizeof(descriptors)); + ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor)); if (ret < 0) { - D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno); - goto err; + v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC); + v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor)); + v1_descriptor.header.fs_count = 3; + v1_descriptor.header.hs_count = 3; + v1_descriptor.fs_descs = fs_descriptors; + v1_descriptor.hs_descs = hs_descriptors; + D("[ %s: Switching to V1_descriptor format errno=%d ]\n", USB_FFS_ADB_EP0, errno); + ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor)); + if (ret < 0) { + D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno); + goto err; + } } ret = adb_write(h->control, &strings, sizeof(strings)); -- cgit v1.2.3 From 3cd669fd5d5317c21d75159d9caabffeb2d1f963 Mon Sep 17 00:00:00 2001 From: Adrien Grassein Date: Thu, 6 Nov 2014 14:53:50 +0100 Subject: Fix build when TARGET_USERIMAGES_USE_EXT4 is not defined The cryptfs.h files is always included, but its path is only included when TARGET_USERIMAGES_USE_EXT4 is defined. Change-Id: Iec6aa4601a56a1feac456a21a53a08557dc1d00d --- Android.mk | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Android.mk b/Android.mk index 1a91f0029..4eb18aae8 100644 --- a/Android.mk +++ b/Android.mk @@ -54,6 +54,8 @@ RECOVERY_FSTAB_VERSION := 2 LOCAL_CFLAGS += -DRECOVERY_API_VERSION=$(RECOVERY_API_VERSION) LOCAL_CFLAGS += -Wno-unused-parameter +LOCAL_C_INCLUDES += system/vold + LOCAL_STATIC_LIBRARIES := \ libext4_utils_static \ libsparse_static \ @@ -75,7 +77,7 @@ LOCAL_STATIC_LIBRARIES := \ ifeq ($(TARGET_USERIMAGES_USE_EXT4), true) LOCAL_CFLAGS += -DUSE_EXT4 - LOCAL_C_INCLUDES += system/extras/ext4_utils system/vold + LOCAL_C_INCLUDES += system/extras/ext4_utils LOCAL_STATIC_LIBRARIES += libext4_utils_static libz endif -- cgit v1.2.3 From 678f7d4a36eeae9a2c1bad6c861cecc2e5dab70c Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Tue, 11 Nov 2014 09:22:15 -0800 Subject: kill HAVE_FORKEXEC Bug: 18317407 Change-Id: Idd4e0effa96752e2c0ca959728f80df4d2d34187 --- minadbd/adb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minadbd/adb.c b/minadbd/adb.c index 127d072be..0ac16e4d9 100644 --- a/minadbd/adb.c +++ b/minadbd/adb.c @@ -379,7 +379,7 @@ static void adb_cleanup(void) int adb_main() { atexit(adb_cleanup); -#if defined(HAVE_FORKEXEC) +#if !defined(_WIN32) // No SIGCHLD. Let the service subproc handle its children. signal(SIGPIPE, SIG_IGN); #endif -- cgit v1.2.3 From 32f9fe7fab642378767318d971d7bcc7df47dab9 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Thu, 20 Nov 2014 09:00:46 -0800 Subject: Global C++11 compatibility. Our build system compiles flex/bison as C++ rather than C, but a few projects add `-x c` to their flags, forcing the compiler to compile them as C. This causes the compiler to reject the global C++ standard flag, so we need to explicitly provide a C standard flag to override it. Bug: 18466763 Change-Id: Id68ad9317261ed4d857a949b07288bd137ff6303 --- edify/Android.mk | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/edify/Android.mk b/edify/Android.mk index 61ed6fa17..03c04e432 100644 --- a/edify/Android.mk +++ b/edify/Android.mk @@ -7,9 +7,10 @@ edify_src_files := \ parser.y \ expr.c -# "-x c" forces the lex/yacc files to be compiled as c; -# the build system otherwise forces them to be c++. -edify_cflags := -x c +# "-x c" forces the lex/yacc files to be compiled as c the build system +# otherwise forces them to be c++. Need to also add an explicit -std because the +# build system will soon default C++ to -std=c++11. +edify_cflags := -x c -std=gnu89 # # Build the host-side command line tool -- cgit v1.2.3 From acf47db238b2f4fff01969210945eec2528de9b7 Mon Sep 17 00:00:00 2001 From: Michael Runge Date: Fri, 21 Nov 2014 00:12:28 -0800 Subject: Add support for tune2fs file operations This allows tune2fs to be executed from within OTA scripts, allowing for file system modifications without formatting the partition Bug: 18430740 Change-Id: I0c2e05b5ef4a81ecea043e9b7b99b545d18fe5e6 --- updater/Android.mk | 10 ++++++++++ updater/install.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/updater/Android.mk b/updater/Android.mk index a3a900a80..11e7bb807 100644 --- a/updater/Android.mk +++ b/updater/Android.mk @@ -34,6 +34,16 @@ LOCAL_STATIC_LIBRARIES += libapplypatch libedify libmtdutils libminzip libz LOCAL_STATIC_LIBRARIES += libmincrypt libbz LOCAL_STATIC_LIBRARIES += libcutils liblog libstdc++ libc LOCAL_STATIC_LIBRARIES += libselinux +tune2fs_static_libraries := \ + libext2_com_err \ + libext2_blkid \ + libext2_quota \ + libext2_uuid_static \ + libext2_e2p \ + libext2fs +LOCAL_STATIC_LIBRARIES += libtune2fs $(tune2fs_static_libraries) + +LOCAL_C_INCLUDES += external/e2fsprogs/misc LOCAL_C_INCLUDES += $(LOCAL_PATH)/.. # Each library in TARGET_RECOVERY_UPDATER_LIBS should have a function diff --git a/updater/install.c b/updater/install.c index ff7de4793..2b2ffb0c5 100644 --- a/updater/install.c +++ b/updater/install.c @@ -46,6 +46,7 @@ #include "mtdutils/mtdutils.h" #include "updater.h" #include "install.h" +#include "tune2fs.h" #ifdef USE_EXT4 #include "make_ext4fs.h" @@ -1539,6 +1540,37 @@ Value* EnableRebootFn(const char* name, State* state, int argc, Expr* argv[]) { return StringValue(strdup("t")); } +Value* Tune2FsFn(const char* name, State* state, int argc, Expr* argv[]) { + if (argc == 0) { + return ErrorAbort(state, "%s() expects args, got %d", name, argc); + } + + char** args = ReadVarArgs(state, argc, argv); + if (args == NULL) { + return ErrorAbort(state, "%s() could not read args", name); + } + + int i; + char** args2 = malloc(sizeof(char*) * (argc+1)); + // Tune2fs expects the program name as its args[0] + args2[0] = strdup(name); + for (i = 0; i < argc; ++i) { + args2[i + 1] = args[i]; + } + int result = tune2fs_main(argc + 1, args2); + for (i = 0; i < argc; ++i) { + free(args[i]); + } + free(args); + + free(args2[0]); + free(args2); + if (result != 0) { + return ErrorAbort(state, "%s() returned error code %d", name, result); + } + return StringValue(strdup("t")); +} + void RegisterInstallFunctions() { RegisterFunction("mount", MountFn); RegisterFunction("is_mounted", IsMountedFn); @@ -1589,4 +1621,5 @@ void RegisterInstallFunctions() { RegisterFunction("set_stage", SetStageFn); RegisterFunction("enable_reboot", EnableRebootFn); + RegisterFunction("tune2fs", Tune2FsFn); } -- cgit v1.2.3 From a01ce3a229aa5d696a282be13d693c7597cfba2f Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Mon, 24 Nov 2014 23:32:33 -0800 Subject: Start losing code to libadb. Bug: 17626262 Change-Id: I8ce7cff2b7789f39f35a4211d7120d072c05a863 --- Android.mk | 1 + minadbd/Android.mk | 3 - minadbd/fdevent.c | 695 ----------------------------------------------------- 3 files changed, 1 insertion(+), 698 deletions(-) delete mode 100644 minadbd/fdevent.c diff --git a/Android.mk b/Android.mk index 1a91f0029..b55bb4e60 100644 --- a/Android.mk +++ b/Android.mk @@ -62,6 +62,7 @@ LOCAL_STATIC_LIBRARIES := \ libmtdutils \ libmincrypt \ libminadbd \ + libadb \ libfusesideload \ libminui \ libpng \ diff --git a/minadbd/Android.mk b/minadbd/Android.mk index 04956d870..c07715706 100644 --- a/minadbd/Android.mk +++ b/minadbd/Android.mk @@ -12,7 +12,6 @@ include $(CLEAR_VARS) LOCAL_SRC_FILES := \ adb.c \ - fdevent.c \ fuse_adb_provider.c \ transport.c \ transport_usb.c \ @@ -27,6 +26,4 @@ LOCAL_C_INCLUDES += bootable/recovery LOCAL_MODULE := libminadbd -LOCAL_STATIC_LIBRARIES := libfusesideload libcutils libc - include $(BUILD_STATIC_LIBRARY) diff --git a/minadbd/fdevent.c b/minadbd/fdevent.c deleted file mode 100644 index 5c374a71b..000000000 --- a/minadbd/fdevent.c +++ /dev/null @@ -1,695 +0,0 @@ -/* http://frotznet.googlecode.com/svn/trunk/utils/fdevent.c -** -** Copyright 2006, Brian Swetland -** -** 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 - -#include -#include -#include -#include -#include - -#include - -#include -#include - -#include "fdevent.h" -#include "transport.h" -#include "sysdeps.h" - - -/* !!! Do not enable DEBUG for the adb that will run as the server: -** both stdout and stderr are used to communicate between the client -** and server. Any extra output will cause failures. -*/ -#define DEBUG 0 /* non-0 will break adb server */ - -// This socket is used when a subproc shell service exists. -// It wakes up the fdevent_loop() and cause the correct handling -// of the shell's pseudo-tty master. I.e. force close it. -int SHELL_EXIT_NOTIFY_FD = -1; - -static void fatal(const char *fn, const char *fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - fprintf(stderr, "%s:", fn); - vfprintf(stderr, fmt, ap); - va_end(ap); - abort(); -} - -#define FATAL(x...) fatal(__FUNCTION__, x) - -#if DEBUG -#define D(...) \ - do { \ - adb_mutex_lock(&D_lock); \ - int save_errno = errno; \ - fprintf(stderr, "%s::%s():", __FILE__, __FUNCTION__); \ - errno = save_errno; \ - fprintf(stderr, __VA_ARGS__); \ - adb_mutex_unlock(&D_lock); \ - errno = save_errno; \ - } while(0) -static void dump_fde(fdevent *fde, const char *info) -{ - adb_mutex_lock(&D_lock); - fprintf(stderr,"FDE #%03d %c%c%c %s\n", fde->fd, - fde->state & FDE_READ ? 'R' : ' ', - fde->state & FDE_WRITE ? 'W' : ' ', - fde->state & FDE_ERROR ? 'E' : ' ', - info); - adb_mutex_unlock(&D_lock); -} -#else -#define D(...) ((void)0) -#define dump_fde(fde, info) do { } while(0) -#endif - -#define FDE_EVENTMASK 0x00ff -#define FDE_STATEMASK 0xff00 - -#define FDE_ACTIVE 0x0100 -#define FDE_PENDING 0x0200 -#define FDE_CREATED 0x0400 - -static void fdevent_plist_enqueue(fdevent *node); -static void fdevent_plist_remove(fdevent *node); -static fdevent *fdevent_plist_dequeue(void); -static void fdevent_subproc_event_func(int fd, unsigned events, void *userdata); - -static fdevent list_pending = { - .next = &list_pending, - .prev = &list_pending, -}; - -static fdevent **fd_table = 0; -static int fd_table_max = 0; - -#ifdef CRAPTASTIC -//HAVE_EPOLL - -#include - -static int epoll_fd = -1; - -static void fdevent_init() -{ - /* XXX: what's a good size for the passed in hint? */ - epoll_fd = epoll_create(256); - - if(epoll_fd < 0) { - perror("epoll_create() failed"); - exit(1); - } - - /* mark for close-on-exec */ - fcntl(epoll_fd, F_SETFD, FD_CLOEXEC); -} - -static void fdevent_connect(fdevent *fde) -{ - struct epoll_event ev; - - memset(&ev, 0, sizeof(ev)); - ev.events = 0; - ev.data.ptr = fde; - -#if 0 - if(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fde->fd, &ev)) { - perror("epoll_ctl() failed\n"); - exit(1); - } -#endif -} - -static void fdevent_disconnect(fdevent *fde) -{ - struct epoll_event ev; - - memset(&ev, 0, sizeof(ev)); - ev.events = 0; - ev.data.ptr = fde; - - /* technically we only need to delete if we - ** were actively monitoring events, but let's - ** be aggressive and do it anyway, just in case - ** something's out of sync - */ - epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fde->fd, &ev); -} - -static void fdevent_update(fdevent *fde, unsigned events) -{ - struct epoll_event ev; - int active; - - active = (fde->state & FDE_EVENTMASK) != 0; - - memset(&ev, 0, sizeof(ev)); - ev.events = 0; - ev.data.ptr = fde; - - if(events & FDE_READ) ev.events |= EPOLLIN; - if(events & FDE_WRITE) ev.events |= EPOLLOUT; - if(events & FDE_ERROR) ev.events |= (EPOLLERR | EPOLLHUP); - - fde->state = (fde->state & FDE_STATEMASK) | events; - - if(active) { - /* we're already active. if we're changing to *no* - ** events being monitored, we need to delete, otherwise - ** we need to just modify - */ - if(ev.events) { - if(epoll_ctl(epoll_fd, EPOLL_CTL_MOD, fde->fd, &ev)) { - perror("epoll_ctl() failed\n"); - exit(1); - } - } else { - if(epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fde->fd, &ev)) { - perror("epoll_ctl() failed\n"); - exit(1); - } - } - } else { - /* we're not active. if we're watching events, we need - ** to add, otherwise we can just do nothing - */ - if(ev.events) { - if(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fde->fd, &ev)) { - perror("epoll_ctl() failed\n"); - exit(1); - } - } - } -} - -static void fdevent_process() -{ - struct epoll_event events[256]; - fdevent *fde; - int i, n; - - n = epoll_wait(epoll_fd, events, 256, -1); - - if(n < 0) { - if(errno == EINTR) return; - perror("epoll_wait"); - exit(1); - } - - for(i = 0; i < n; i++) { - struct epoll_event *ev = events + i; - fde = ev->data.ptr; - - if(ev->events & EPOLLIN) { - fde->events |= FDE_READ; - } - if(ev->events & EPOLLOUT) { - fde->events |= FDE_WRITE; - } - if(ev->events & (EPOLLERR | EPOLLHUP)) { - fde->events |= FDE_ERROR; - } - if(fde->events) { - if(fde->state & FDE_PENDING) continue; - fde->state |= FDE_PENDING; - fdevent_plist_enqueue(fde); - } - } -} - -#else /* USE_SELECT */ - -#ifdef HAVE_WINSOCK -#include -#else -#include -#endif - -static fd_set read_fds; -static fd_set write_fds; -static fd_set error_fds; - -static int select_n = 0; - -static void fdevent_init(void) -{ - FD_ZERO(&read_fds); - FD_ZERO(&write_fds); - FD_ZERO(&error_fds); -} - -static void fdevent_connect(fdevent *fde) -{ - if(fde->fd >= select_n) { - select_n = fde->fd + 1; - } -} - -static void fdevent_disconnect(fdevent *fde) -{ - int i, n; - - FD_CLR(fde->fd, &read_fds); - FD_CLR(fde->fd, &write_fds); - FD_CLR(fde->fd, &error_fds); - - for(n = 0, i = 0; i < select_n; i++) { - if(fd_table[i] != 0) n = i; - } - select_n = n + 1; -} - -static void fdevent_update(fdevent *fde, unsigned events) -{ - if(events & FDE_READ) { - FD_SET(fde->fd, &read_fds); - } else { - FD_CLR(fde->fd, &read_fds); - } - if(events & FDE_WRITE) { - FD_SET(fde->fd, &write_fds); - } else { - FD_CLR(fde->fd, &write_fds); - } - if(events & FDE_ERROR) { - FD_SET(fde->fd, &error_fds); - } else { - FD_CLR(fde->fd, &error_fds); - } - - fde->state = (fde->state & FDE_STATEMASK) | events; -} - -/* Looks at fd_table[] for bad FDs and sets bit in fds. -** Returns the number of bad FDs. -*/ -static int fdevent_fd_check(fd_set *fds) -{ - int i, n = 0; - fdevent *fde; - - for(i = 0; i < select_n; i++) { - fde = fd_table[i]; - if(fde == 0) continue; - if(fcntl(i, F_GETFL, NULL) < 0) { - FD_SET(i, fds); - n++; - // fde->state |= FDE_DONT_CLOSE; - - } - } - return n; -} - -#if !DEBUG -static inline void dump_all_fds(const char *extra_msg) {} -#else -static void dump_all_fds(const char *extra_msg) -{ -int i; - fdevent *fde; - // per fd: 4 digits (but really: log10(FD_SETSIZE)), 1 staus, 1 blank - char msg_buff[FD_SETSIZE*6 + 1], *pb=msg_buff; - size_t max_chars = FD_SETSIZE * 6 + 1; - int printed_out; -#define SAFE_SPRINTF(...) \ - do { \ - printed_out = snprintf(pb, max_chars, __VA_ARGS__); \ - if (printed_out <= 0) { \ - D("... snprintf failed.\n"); \ - return; \ - } \ - if (max_chars < (unsigned int)printed_out) { \ - D("... snprintf out of space.\n"); \ - return; \ - } \ - pb += printed_out; \ - max_chars -= printed_out; \ - } while(0) - - for(i = 0; i < select_n; i++) { - fde = fd_table[i]; - SAFE_SPRINTF("%d", i); - if(fde == 0) { - SAFE_SPRINTF("? "); - continue; - } - if(fcntl(i, F_GETFL, NULL) < 0) { - SAFE_SPRINTF("b"); - } - SAFE_SPRINTF(" "); - } - D("%s fd_table[]->fd = {%s}\n", extra_msg, msg_buff); -} -#endif - -static void fdevent_process() -{ - int i, n; - fdevent *fde; - unsigned events; - fd_set rfd, wfd, efd; - - memcpy(&rfd, &read_fds, sizeof(fd_set)); - memcpy(&wfd, &write_fds, sizeof(fd_set)); - memcpy(&efd, &error_fds, sizeof(fd_set)); - - dump_all_fds("pre select()"); - - n = select(select_n, &rfd, &wfd, &efd, NULL); - int saved_errno = errno; - D("select() returned n=%d, errno=%d\n", n, n<0?saved_errno:0); - - dump_all_fds("post select()"); - - if(n < 0) { - switch(saved_errno) { - case EINTR: return; - case EBADF: - // Can't trust the FD sets after an error. - FD_ZERO(&wfd); - FD_ZERO(&efd); - FD_ZERO(&rfd); - break; - default: - D("Unexpected select() error=%d\n", saved_errno); - return; - } - } - if(n <= 0) { - // We fake a read, as the rest of the code assumes - // that errors will be detected at that point. - n = fdevent_fd_check(&rfd); - } - - for(i = 0; (i < select_n) && (n > 0); i++) { - events = 0; - if(FD_ISSET(i, &rfd)) { events |= FDE_READ; n--; } - if(FD_ISSET(i, &wfd)) { events |= FDE_WRITE; n--; } - if(FD_ISSET(i, &efd)) { events |= FDE_ERROR; n--; } - - if(events) { - fde = fd_table[i]; - if(fde == 0) - FATAL("missing fde for fd %d\n", i); - - fde->events |= events; - - D("got events fde->fd=%d events=%04x, state=%04x\n", - fde->fd, fde->events, fde->state); - if(fde->state & FDE_PENDING) continue; - fde->state |= FDE_PENDING; - fdevent_plist_enqueue(fde); - } - } -} - -#endif - -static void fdevent_register(fdevent *fde) -{ - if(fde->fd < 0) { - FATAL("bogus negative fd (%d)\n", fde->fd); - } - - if(fde->fd >= fd_table_max) { - int oldmax = fd_table_max; - if(fde->fd > 32000) { - FATAL("bogus huuuuge fd (%d)\n", fde->fd); - } - if(fd_table_max == 0) { - fdevent_init(); - fd_table_max = 256; - } - while(fd_table_max <= fde->fd) { - fd_table_max *= 2; - } - fd_table = realloc(fd_table, sizeof(fdevent*) * fd_table_max); - if(fd_table == 0) { - FATAL("could not expand fd_table to %d entries\n", fd_table_max); - } - memset(fd_table + oldmax, 0, sizeof(int) * (fd_table_max - oldmax)); - } - - fd_table[fde->fd] = fde; -} - -static void fdevent_unregister(fdevent *fde) -{ - if((fde->fd < 0) || (fde->fd >= fd_table_max)) { - FATAL("fd out of range (%d)\n", fde->fd); - } - - if(fd_table[fde->fd] != fde) { - FATAL("fd_table out of sync [%d]\n", fde->fd); - } - - fd_table[fde->fd] = 0; - - if(!(fde->state & FDE_DONT_CLOSE)) { - dump_fde(fde, "close"); - adb_close(fde->fd); - } -} - -static void fdevent_plist_enqueue(fdevent *node) -{ - fdevent *list = &list_pending; - - node->next = list; - node->prev = list->prev; - node->prev->next = node; - list->prev = node; -} - -static void fdevent_plist_remove(fdevent *node) -{ - node->prev->next = node->next; - node->next->prev = node->prev; - node->next = 0; - node->prev = 0; -} - -static fdevent *fdevent_plist_dequeue(void) -{ - fdevent *list = &list_pending; - fdevent *node = list->next; - - if(node == list) return 0; - - list->next = node->next; - list->next->prev = list; - node->next = 0; - node->prev = 0; - - return node; -} - -static void fdevent_call_fdfunc(fdevent* fde) -{ - unsigned events = fde->events; - fde->events = 0; - if(!(fde->state & FDE_PENDING)) return; - fde->state &= (~FDE_PENDING); - dump_fde(fde, "callback"); - fde->func(fde->fd, events, fde->arg); -} - -static void fdevent_subproc_event_func(int fd, unsigned ev, void *userdata) -{ - - D("subproc handling on fd=%d ev=%04x\n", fd, ev); - - // Hook oneself back into the fde's suitable for select() on read. - if((fd < 0) || (fd >= fd_table_max)) { - FATAL("fd %d out of range for fd_table \n", fd); - } - fdevent *fde = fd_table[fd]; - fdevent_add(fde, FDE_READ); - - if(ev & FDE_READ){ - int subproc_fd; - - if(readx(fd, &subproc_fd, sizeof(subproc_fd))) { - FATAL("Failed to read the subproc's fd from fd=%d\n", fd); - } - if((subproc_fd < 0) || (subproc_fd >= fd_table_max)) { - D("subproc_fd %d out of range 0, fd_table_max=%d\n", - subproc_fd, fd_table_max); - return; - } - fdevent *subproc_fde = fd_table[subproc_fd]; - if(!subproc_fde) { - D("subproc_fd %d cleared from fd_table\n", subproc_fd); - return; - } - if(subproc_fde->fd != subproc_fd) { - // Already reallocated? - D("subproc_fd %d != fd_table[].fd %d\n", subproc_fd, subproc_fde->fd); - return; - } - - subproc_fde->force_eof = 1; - - int rcount = 0; - ioctl(subproc_fd, FIONREAD, &rcount); - D("subproc with fd=%d has rcount=%d err=%d\n", - subproc_fd, rcount, errno); - - if(rcount) { - // If there is data left, it will show up in the select(). - // This works because there is no other thread reading that - // data when in this fd_func(). - return; - } - - D("subproc_fde.state=%04x\n", subproc_fde->state); - subproc_fde->events |= FDE_READ; - if(subproc_fde->state & FDE_PENDING) { - return; - } - subproc_fde->state |= FDE_PENDING; - fdevent_call_fdfunc(subproc_fde); - } -} - -fdevent *fdevent_create(int fd, fd_func func, void *arg) -{ - fdevent *fde = (fdevent*) malloc(sizeof(fdevent)); - if(fde == 0) return 0; - fdevent_install(fde, fd, func, arg); - fde->state |= FDE_CREATED; - return fde; -} - -void fdevent_destroy(fdevent *fde) -{ - if(fde == 0) return; - if(!(fde->state & FDE_CREATED)) { - FATAL("fde %p not created by fdevent_create()\n", fde); - } - fdevent_remove(fde); -} - -void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg) -{ - memset(fde, 0, sizeof(fdevent)); - fde->state = FDE_ACTIVE; - fde->fd = fd; - fde->force_eof = 0; - fde->func = func; - fde->arg = arg; - -#ifndef HAVE_WINSOCK - fcntl(fd, F_SETFL, O_NONBLOCK); -#endif - fdevent_register(fde); - dump_fde(fde, "connect"); - fdevent_connect(fde); - fde->state |= FDE_ACTIVE; -} - -void fdevent_remove(fdevent *fde) -{ - if(fde->state & FDE_PENDING) { - fdevent_plist_remove(fde); - } - - if(fde->state & FDE_ACTIVE) { - fdevent_disconnect(fde); - dump_fde(fde, "disconnect"); - fdevent_unregister(fde); - } - - fde->state = 0; - fde->events = 0; -} - - -void fdevent_set(fdevent *fde, unsigned events) -{ - events &= FDE_EVENTMASK; - - if((fde->state & FDE_EVENTMASK) == events) return; - - if(fde->state & FDE_ACTIVE) { - fdevent_update(fde, events); - dump_fde(fde, "update"); - } - - fde->state = (fde->state & FDE_STATEMASK) | events; - - if(fde->state & FDE_PENDING) { - /* if we're pending, make sure - ** we don't signal an event that - ** is no longer wanted. - */ - fde->events &= (~events); - if(fde->events == 0) { - fdevent_plist_remove(fde); - fde->state &= (~FDE_PENDING); - } - } -} - -void fdevent_add(fdevent *fde, unsigned events) -{ - fdevent_set( - fde, (fde->state & FDE_EVENTMASK) | (events & FDE_EVENTMASK)); -} - -void fdevent_del(fdevent *fde, unsigned events) -{ - fdevent_set( - fde, (fde->state & FDE_EVENTMASK) & (~(events & FDE_EVENTMASK))); -} - -void fdevent_subproc_setup() -{ - int s[2]; - - if(adb_socketpair(s)) { - FATAL("cannot create shell-exit socket-pair\n"); - } - SHELL_EXIT_NOTIFY_FD = s[0]; - fdevent *fde; - fde = fdevent_create(s[1], fdevent_subproc_event_func, NULL); - if(!fde) - FATAL("cannot create fdevent for shell-exit handler\n"); - fdevent_add(fde, FDE_READ); -} - -void fdevent_loop() -{ - fdevent *fde; - fdevent_subproc_setup(); - - for(;;) { - D("--- ---- waiting for events\n"); - - fdevent_process(); - - while((fde = fdevent_plist_dequeue())) { - fdevent_call_fdfunc(fde); - } - } -} -- cgit v1.2.3 From 4c3c7a962f5b5ec7fde06065cd60a49bf272329d Mon Sep 17 00:00:00 2001 From: Bruce Beare Date: Fri, 12 Sep 2014 09:09:52 -0700 Subject: Fix recovery image build for 32p When building for 32p, we need to be explicit that we wish to build the 32bit version of the binaries that will be placed in the recovery image. The recovery image doesn't actually care... but if we are not explicit in this, the makefiles will ask for the 64bit binaries but the Android.mk for the binaries will supply the 32bit images (causing the build to fail). Change-Id: Iea2d5f412740c082795da4358765751138a4b167 --- updater/Android.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/updater/Android.mk b/updater/Android.mk index a3a900a80..c73cdc083 100644 --- a/updater/Android.mk +++ b/updater/Android.mk @@ -69,7 +69,7 @@ $(inc) : $(inc_dep_file) $(hide) $(foreach lib,$(libs),echo " Register_$(lib)();" >> $@;) $(hide) echo "}" >> $@ -$(call intermediates-dir-for,EXECUTABLES,updater)/updater.o : $(inc) +$(call intermediates-dir-for,EXECUTABLES,updater,,,$(TARGET_PREFER_32_BIT))/updater.o : $(inc) LOCAL_C_INCLUDES += $(dir $(inc)) inc := -- cgit v1.2.3 From 7279f97ab492094810a4bc88f81d53b5e8c4bb74 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Thu, 18 Dec 2014 11:50:23 -0800 Subject: Remove an uninitialized value. The assignment of this value was removed in 0d32f25, but the declaration was still there and still tested. Clang issues a warning for this. Change-Id: I748bfb8b4f78ceed1c1b5b1bb80cb4e873e4facc --- edify/main.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/edify/main.c b/edify/main.c index b3fad53b8..b1baa0b13 100644 --- a/edify/main.c +++ b/edify/main.c @@ -25,13 +25,12 @@ extern int yyparse(Expr** root, int* error_count); int expect(const char* expr_str, const char* expected, int* errors) { Expr* e; - int error; char* result; printf("."); int error_count = parse_string(expr_str, &e, &error_count); - if (error > 0 || error_count > 0) { + if (error_count > 0) { printf("error parsing \"%s\" (%d errors)\n", expr_str, error_count); ++*errors; -- cgit v1.2.3 From d4d4c2456ac6649f65fd561998b2cb8eb2c97edd Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 29 Dec 2014 12:46:43 -0800 Subject: Fix missing #includes in bootable/recovery. Change-Id: I58dfbac6ca1aa80d3659f53a8fad1bbbbdc9b941 --- uncrypt/uncrypt.c | 1 + verifier_test.cpp | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/uncrypt/uncrypt.c b/uncrypt/uncrypt.c index 189fa57e1..e619237be 100644 --- a/uncrypt/uncrypt.c +++ b/uncrypt/uncrypt.c @@ -39,6 +39,7 @@ // Recovery can take this block map file and retrieve the underlying // file data to use as an update package. +#include #include #include #include diff --git a/verifier_test.cpp b/verifier_test.cpp index 10a5ddaad..e2f3d105c 100644 --- a/verifier_test.cpp +++ b/verifier_test.cpp @@ -14,12 +14,13 @@ * limitations under the License. */ +#include +#include +#include #include #include -#include #include #include -#include #include "common.h" #include "verifier.h" -- cgit v1.2.3 From a382e2bdb22b5b68d83f32c6c339465b2e8f6e46 Mon Sep 17 00:00:00 2001 From: Yabin Cui Date: Fri, 2 Jan 2015 14:00:13 -0800 Subject: Use getmntent when accessing /proc/mounts. Bug: 18887435 Change-Id: Ice44c14fc8ee79eab259caf486e123b6af21ceb0 --- mtdutils/mounts.c | 89 +++++++++---------------------------------------------- 1 file changed, 14 insertions(+), 75 deletions(-) diff --git a/mtdutils/mounts.c b/mtdutils/mounts.c index c90fc8acf..6a9b03d30 100644 --- a/mtdutils/mounts.c +++ b/mtdutils/mounts.c @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include #include #include @@ -59,10 +60,8 @@ free_volume_internals(const MountedVolume *volume, int zero) int scan_mounted_volumes() { - char buf[2048]; - const char *bufp; - int fd; - ssize_t nbytes; + FILE* fp; + struct mntent* mentry; if (g_mounts_state.volumes == NULL) { const int numv = 32; @@ -84,80 +83,20 @@ scan_mounted_volumes() } g_mounts_state.volume_count = 0; - /* Open and read the file contents. - */ - fd = open(PROC_MOUNTS_FILENAME, O_RDONLY); - if (fd < 0) { - goto bail; - } - nbytes = read(fd, buf, sizeof(buf) - 1); - close(fd); - if (nbytes < 0) { - goto bail; + /* Open and read mount table entries. */ + fp = setmntent(PROC_MOUNTS_FILENAME, "r"); + if (fp == NULL) { + return -1; } - buf[nbytes] = '\0'; - - /* Parse the contents of the file, which looks like: - * - * # cat /proc/mounts - * rootfs / rootfs rw 0 0 - * /dev/pts /dev/pts devpts rw 0 0 - * /proc /proc proc rw 0 0 - * /sys /sys sysfs rw 0 0 - * /dev/block/mtdblock4 /system yaffs2 rw,nodev,noatime,nodiratime 0 0 - * /dev/block/mtdblock5 /data yaffs2 rw,nodev,noatime,nodiratime 0 0 - * /dev/block/mmcblk0p1 /sdcard vfat rw,sync,dirsync,fmask=0000,dmask=0000,codepage=cp437,iocharset=iso8859-1,utf8 0 0 - * - * The zeroes at the end are dummy placeholder fields to make the - * output match Linux's /etc/mtab, but don't represent anything here. - */ - bufp = buf; - while (nbytes > 0) { - char device[64]; - char mount_point[64]; - char filesystem[64]; - char flags[128]; - int matches; - - /* %as is a gnu extension that malloc()s a string for each field. - */ - matches = sscanf(bufp, "%63s %63s %63s %127s", - device, mount_point, filesystem, flags); - - if (matches == 4) { - device[sizeof(device)-1] = '\0'; - mount_point[sizeof(mount_point)-1] = '\0'; - filesystem[sizeof(filesystem)-1] = '\0'; - flags[sizeof(flags)-1] = '\0'; - - MountedVolume *v = - &g_mounts_state.volumes[g_mounts_state.volume_count++]; - v->device = strdup(device); - v->mount_point = strdup(mount_point); - v->filesystem = strdup(filesystem); - v->flags = strdup(flags); - } else { -printf("matches was %d on <<%.40s>>\n", matches, bufp); - } - - /* Eat the line. - */ - while (nbytes > 0 && *bufp != '\n') { - bufp++; - nbytes--; - } - if (nbytes > 0) { - bufp++; - nbytes--; - } + while ((mentry = getmntent(fp)) != NULL) { + MountedVolume* v = &g_mounts_state.volumes[g_mounts_state.volume_count++]; + v->device = strdup(mentry->mnt_fsname); + v->mount_point = strdup(mentry->mnt_dir); + v->filesystem = strdup(mentry->mnt_type); + v->flags = strdup(mentry->mnt_opts); } - + endmntent(fp); return 0; - -bail: -//TODO: free the strings we've allocated. - g_mounts_state.volume_count = 0; - return -1; } const MountedVolume * -- cgit v1.2.3 From 6ed899a9b6798f841595253549d740b446fe24fc Mon Sep 17 00:00:00 2001 From: Adam Langley Date: Thu, 22 Jan 2015 16:38:12 -0800 Subject: Remove superfluous OpenSSL include paths. This include path was needed because system/vold/cryptfs.h included an OpenSSL header just to get the length of a SHA-256 hash. This has been fixed in https://android-review.googlesource.com/#/c/124477/1. Change-Id: I06a8ba0ee5b9efcc3260598f07d9819f065711de --- Android.mk | 1 - 1 file changed, 1 deletion(-) diff --git a/Android.mk b/Android.mk index 1a91f0029..45aa22ddd 100644 --- a/Android.mk +++ b/Android.mk @@ -92,7 +92,6 @@ else endif LOCAL_C_INCLUDES += system/extras/ext4_utils -LOCAL_C_INCLUDES += external/openssl/include include $(BUILD_EXECUTABLE) -- cgit v1.2.3 From e01d9de9816e71e333d4c94c6edcb06752236656 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Sat, 24 Jan 2015 22:21:24 -0800 Subject: Add missing include. Change-Id: I79a9a58904b2992c306d8de0c7b3a4aacd4b67e0 --- verifier_test.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/verifier_test.cpp b/verifier_test.cpp index e2f3d105c..93a071e37 100644 --- a/verifier_test.cpp +++ b/verifier_test.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include -- cgit v1.2.3 From 9e3cce50a3a571fcdc6a0a28da81ef00071ccb4f Mon Sep 17 00:00:00 2001 From: Badhri Jagan Sridharan Date: Mon, 27 Oct 2014 18:32:35 -0700 Subject: adbd: Support for new f_fs descriptor format The patch "[RFC] usb: gadget: f_fs: Add flags to descriptors block" marks the current usb_functionfs_descs_head format deprecated and introduces support for sending SuperSpeed descriptors. This CL makes adbd to send Descriptors in the new format. Adbd would fall back to the old format, if kernel is not able to recognize the new format. This is done to prevent adbd from breaking in the older versions of the kernel. Bug: 17394972 (cherry picked from commit b5b43043fa71f9cb620ddd02ec2bc98eced5a6ce) Change-Id: I5af9dc9d4f41ad47d678279054a648f69497b24e --- minadbd/usb_linux_client.c | 161 ++++++++++++++++++++++++++++++--------------- 1 file changed, 107 insertions(+), 54 deletions(-) diff --git a/minadbd/usb_linux_client.c b/minadbd/usb_linux_client.c index 29bab1558..e7d3c4854 100644 --- a/minadbd/usb_linux_client.c +++ b/minadbd/usb_linux_client.c @@ -52,7 +52,35 @@ struct usb_handle int bulk_in; /* "in" from the host's perspective => sink for adbd */ }; -static const struct { +struct func_desc { + struct usb_interface_descriptor intf; + struct usb_endpoint_descriptor_no_audio source; + struct usb_endpoint_descriptor_no_audio sink; +} __attribute__((packed)); + +struct desc_v1 { + struct usb_functionfs_descs_head_v1 { + __le32 magic; + __le32 length; + __le32 fs_count; + __le32 hs_count; + } __attribute__((packed)) header; + struct func_desc fs_descs, hs_descs; +} __attribute__((packed)); + +struct desc_v2 { + struct usb_functionfs_descs_head_v2 { + __le32 magic; + __le32 length; + __le32 flags; + __le32 fs_count; + __le32 hs_count; + __le32 ss_count; + } __attribute__((packed)) header; + struct func_desc fs_descs, hs_descs; +} __attribute__((packed)); + +/*static const struct { struct usb_functionfs_descs_head header; struct { struct usb_interface_descriptor intf; @@ -66,57 +94,60 @@ static const struct { .fs_count = 3, .hs_count = 3, }, - .fs_descs = { - .intf = { - .bLength = sizeof(descriptors.fs_descs.intf), - .bDescriptorType = USB_DT_INTERFACE, - .bInterfaceNumber = 0, - .bNumEndpoints = 2, - .bInterfaceClass = ADB_CLASS, - .bInterfaceSubClass = ADB_SUBCLASS, - .bInterfaceProtocol = ADB_PROTOCOL, - .iInterface = 1, /* first string from the provided table */ - }, - .source = { - .bLength = sizeof(descriptors.fs_descs.source), - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 1 | USB_DIR_OUT, - .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = MAX_PACKET_SIZE_FS, - }, - .sink = { - .bLength = sizeof(descriptors.fs_descs.sink), - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 2 | USB_DIR_IN, - .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = MAX_PACKET_SIZE_FS, - }, + +*/ + +struct func_desc fs_descriptors = { + .intf = { + .bLength = sizeof(fs_descriptors.intf), + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bNumEndpoints = 2, + .bInterfaceClass = ADB_CLASS, + .bInterfaceSubClass = ADB_SUBCLASS, + .bInterfaceProtocol = ADB_PROTOCOL, + .iInterface = 1, /* first string from the provided table */ + }, + .source = { + .bLength = sizeof(fs_descriptors.source), + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 1 | USB_DIR_OUT, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = MAX_PACKET_SIZE_FS, + }, + .sink = { + .bLength = sizeof(fs_descriptors.sink), + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 2 | USB_DIR_IN, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = MAX_PACKET_SIZE_FS, + }, +}; + +struct func_desc hs_descriptors = { + .intf = { + .bLength = sizeof(hs_descriptors.intf), + .bDescriptorType = USB_DT_INTERFACE, + .bInterfaceNumber = 0, + .bNumEndpoints = 2, + .bInterfaceClass = ADB_CLASS, + .bInterfaceSubClass = ADB_SUBCLASS, + .bInterfaceProtocol = ADB_PROTOCOL, + .iInterface = 1, /* first string from the provided table */ + }, + .source = { + .bLength = sizeof(hs_descriptors.source), + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 1 | USB_DIR_OUT, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = MAX_PACKET_SIZE_HS, }, - .hs_descs = { - .intf = { - .bLength = sizeof(descriptors.hs_descs.intf), - .bDescriptorType = USB_DT_INTERFACE, - .bInterfaceNumber = 0, - .bNumEndpoints = 2, - .bInterfaceClass = ADB_CLASS, - .bInterfaceSubClass = ADB_SUBCLASS, - .bInterfaceProtocol = ADB_PROTOCOL, - .iInterface = 1, /* first string from the provided table */ - }, - .source = { - .bLength = sizeof(descriptors.hs_descs.source), - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 1 | USB_DIR_OUT, - .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = MAX_PACKET_SIZE_HS, - }, - .sink = { - .bLength = sizeof(descriptors.hs_descs.sink), - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 2 | USB_DIR_IN, - .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = MAX_PACKET_SIZE_HS, - }, + .sink = { + .bLength = sizeof(hs_descriptors.sink), + .bDescriptorType = USB_DT_ENDPOINT, + .bEndpointAddress = 2 | USB_DIR_IN, + .bmAttributes = USB_ENDPOINT_XFER_BULK, + .wMaxPacketSize = MAX_PACKET_SIZE_HS, }, }; @@ -267,6 +298,18 @@ static void usb_adb_init() static void init_functionfs(struct usb_handle *h) { ssize_t ret; + struct desc_v1 v1_descriptor; + struct desc_v2 v2_descriptor; + + v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2); + v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor)); + v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC; + v2_descriptor.header.fs_count = 3; + v2_descriptor.header.hs_count = 3; + v2_descriptor.header.ss_count = 0; + v2_descriptor.fs_descs = fs_descriptors; + v2_descriptor.hs_descs = hs_descriptors; + D("OPENING %s\n", USB_FFS_ADB_EP0); h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR); @@ -275,10 +318,20 @@ static void init_functionfs(struct usb_handle *h) goto err; } - ret = adb_write(h->control, &descriptors, sizeof(descriptors)); + ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor)); if (ret < 0) { - D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno); - goto err; + v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC); + v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor)); + v1_descriptor.header.fs_count = 3; + v1_descriptor.header.hs_count = 3; + v1_descriptor.fs_descs = fs_descriptors; + v1_descriptor.hs_descs = hs_descriptors; + D("[ %s: Switching to V1_descriptor format errno=%d ]\n", USB_FFS_ADB_EP0, errno); + ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor)); + if (ret < 0) { + D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno); + goto err; + } } ret = adb_write(h->control, &strings, sizeof(strings)); -- cgit v1.2.3 From 3ed8ef02ede8539dc80adc3e6cf5650a80650763 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Mon, 26 Jan 2015 12:53:20 -0800 Subject: Fix the v2 descriptor handling. There was a misinterpretation of how the v2 header works. The flags in the header indicate what is in the rest of the structure. Bug: 19127803 Change-Id: I83fd93df1df196300a80ddeb3b49ca7851ffcfb8 --- minadbd/usb_linux_client.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/minadbd/usb_linux_client.c b/minadbd/usb_linux_client.c index e7d3c4854..b3a38dce5 100644 --- a/minadbd/usb_linux_client.c +++ b/minadbd/usb_linux_client.c @@ -69,14 +69,10 @@ struct desc_v1 { } __attribute__((packed)); struct desc_v2 { - struct usb_functionfs_descs_head_v2 { - __le32 magic; - __le32 length; - __le32 flags; - __le32 fs_count; - __le32 hs_count; - __le32 ss_count; - } __attribute__((packed)) header; + struct usb_functionfs_descs_head_v2 header; + // The rest of the structure depends on the flags in the header. + __le32 fs_count; + __le32 hs_count; struct func_desc fs_descs, hs_descs; } __attribute__((packed)); @@ -304,9 +300,8 @@ static void init_functionfs(struct usb_handle *h) v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2); v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor)); v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC; - v2_descriptor.header.fs_count = 3; - v2_descriptor.header.hs_count = 3; - v2_descriptor.header.ss_count = 0; + v2_descriptor.fs_count = 3; + v2_descriptor.hs_count = 3; v2_descriptor.fs_descs = fs_descriptors; v2_descriptor.hs_descs = hs_descriptors; -- cgit v1.2.3 From 26dbad2b984e69f6c938ac3e82267d0ded0df8fd Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 28 Jan 2015 12:09:05 -0800 Subject: Add missing includes. Change-Id: I0737456e0221ebe9cc854d65c95a7d37d0869d56 --- applypatch/bspatch.c | 1 + asn1_decoder.cpp | 1 + install.cpp | 1 + verifier.cpp | 5 +++-- 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/applypatch/bspatch.c b/applypatch/bspatch.c index b34ec2a88..b57760eda 100644 --- a/applypatch/bspatch.c +++ b/applypatch/bspatch.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include diff --git a/asn1_decoder.cpp b/asn1_decoder.cpp index 7280f7480..e7aef781c 100644 --- a/asn1_decoder.cpp +++ b/asn1_decoder.cpp @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include #include diff --git a/install.cpp b/install.cpp index 9db5640a0..31606bb1a 100644 --- a/install.cpp +++ b/install.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include diff --git a/verifier.cpp b/verifier.cpp index eeff95a59..61e5adf0b 100644 --- a/verifier.cpp +++ b/verifier.cpp @@ -26,9 +26,10 @@ #include "mincrypt/sha.h" #include "mincrypt/sha256.h" -#include -#include #include +#include +#include +#include extern RecoveryUI* ui; -- cgit v1.2.3 From cd3c55ab40efd12f5a2d396dbb57509e4d071641 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Thu, 29 Jan 2015 20:50:08 -0800 Subject: Add missing includes. Change-Id: I06ea08400efa511e627be37a4fd70fbdfadea2e6 --- applypatch/imgpatch.c | 1 + fuse_sdcard_provider.c | 1 + minadbd/fuse_adb_provider.c | 1 + minui/events.c | 2 ++ minui/graphics.c | 1 + minui/graphics_adf.c | 1 + minui/graphics_fbdev.c | 1 + minui/resources.c | 1 + tools/ota/check-lost+found.c | 1 + uncrypt/uncrypt.c | 1 + updater/updater.c | 1 + 11 files changed, 12 insertions(+) diff --git a/applypatch/imgpatch.c b/applypatch/imgpatch.c index 33c448762..09b0a7397 100644 --- a/applypatch/imgpatch.c +++ b/applypatch/imgpatch.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include diff --git a/fuse_sdcard_provider.c b/fuse_sdcard_provider.c index 19fb52df0..ca8c914f9 100644 --- a/fuse_sdcard_provider.c +++ b/fuse_sdcard_provider.c @@ -16,6 +16,7 @@ #include #include +#include #include #include #include diff --git a/minadbd/fuse_adb_provider.c b/minadbd/fuse_adb_provider.c index f80533a8c..e9262858c 100644 --- a/minadbd/fuse_adb_provider.c +++ b/minadbd/fuse_adb_provider.c @@ -16,6 +16,7 @@ #include #include +#include #include #include "adb.h" diff --git a/minui/events.c b/minui/events.c index df7dad448..d98a774ef 100644 --- a/minui/events.c +++ b/minui/events.c @@ -16,6 +16,8 @@ #include #include +#include +#include #include #include #include diff --git a/minui/graphics.c b/minui/graphics.c index 6049d85ca..ec39433b8 100644 --- a/minui/graphics.c +++ b/minui/graphics.c @@ -16,6 +16,7 @@ #include #include +#include #include #include diff --git a/minui/graphics_adf.c b/minui/graphics_adf.c index ac6d64e9e..289c3be63 100644 --- a/minui/graphics_adf.c +++ b/minui/graphics_adf.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include diff --git a/minui/graphics_fbdev.c b/minui/graphics_fbdev.c index c0c1bcb1a..a087899bd 100644 --- a/minui/graphics_fbdev.c +++ b/minui/graphics_fbdev.c @@ -16,6 +16,7 @@ #include #include +#include #include #include diff --git a/minui/resources.c b/minui/resources.c index 2bae4ded0..f645c4b67 100644 --- a/minui/resources.c +++ b/minui/resources.c @@ -15,6 +15,7 @@ */ #include +#include #include #include diff --git a/tools/ota/check-lost+found.c b/tools/ota/check-lost+found.c index da02f4602..cbf792629 100644 --- a/tools/ota/check-lost+found.c +++ b/tools/ota/check-lost+found.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "private/android_filesystem_config.h" diff --git a/uncrypt/uncrypt.c b/uncrypt/uncrypt.c index e619237be..b90bd6b87 100644 --- a/uncrypt/uncrypt.c +++ b/uncrypt/uncrypt.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include diff --git a/updater/updater.c b/updater/updater.c index 465e1238e..661f69587 100644 --- a/updater/updater.c +++ b/updater/updater.c @@ -17,6 +17,7 @@ #include #include #include +#include #include "edify/expr.h" #include "updater.h" -- cgit v1.2.3 From 90221205a3e58f2a198faa838088dc7bc7c9c752 Mon Sep 17 00:00:00 2001 From: Sami Tolvanen Date: Tue, 9 Dec 2014 16:39:47 +0000 Subject: Support resuming block based OTAs Add support for transfer list version 3, which allows us to verify the status of each command and resume an interrupted block based OTA update. Notes on the changes: - Move the previous BlockImageUpdateFn to a shorter and reusable PerformBlockImageUpdate, which can be used also in BlockImageVerifyFn for verification. - Split individual transfer list commands into separate functions with unified parameters for clarity, and use a hash table to locate them during execution. - Move common block reading and writing to ReadBlocks and WriteBlocks to reduce code duplication, and rename the readblock and writeblock to less confusing read_all and write_all. The coding style of the new functions follows the existing style in the updater/edify code. Needs matching changes from Ia5c56379f570047f10f0aa7373a1025439495c98 Bug: 18262110 Change-Id: I1e752464134aeb2d396946348e6041acabe13942 --- updater/blockimg.c | 1988 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 1551 insertions(+), 437 deletions(-) diff --git a/updater/blockimg.c b/updater/blockimg.c index 302689313..a0f81e935 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -16,6 +16,7 @@ #include #include +#include #include #include #include @@ -23,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -32,7 +34,7 @@ #include "applypatch/applypatch.h" #include "edify/expr.h" #include "mincrypt/sha.h" -#include "minzip/DirUtil.h" +#include "minzip/Hash.h" #include "updater.h" #define BLOCKSIZE 4096 @@ -46,6 +48,10 @@ #define BLKDISCARD _IO(0x12,119) #endif +#define STASH_DIRECTORY_BASE "/cache/recovery" +#define STASH_DIRECTORY_MODE 0700 +#define STASH_FILE_MODE 0600 + char* PrintSha1(const uint8_t* digest); typedef struct { @@ -80,44 +86,77 @@ static RangeSet* parse_range(char* text) { return out; } -static void readblock(int fd, uint8_t* data, size_t size) { +static int range_overlaps(RangeSet* r1, RangeSet* r2) { + int i, j, r1_0, r1_1, r2_0, r2_1; + + if (!r1 || !r2) { + return 0; + } + + for (i = 0; i < r1->count; ++i) { + r1_0 = r1->pos[i * 2]; + r1_1 = r1->pos[i * 2 + 1]; + + for (j = 0; j < r2->count; ++j) { + r2_0 = r2->pos[j * 2]; + r2_1 = r2->pos[j * 2 + 1]; + + if (!(r2_0 > r1_1 || r1_0 > r2_1)) { + return 1; + } + } + } + + return 0; +} + +static int read_all(int fd, uint8_t* data, size_t size) { size_t so_far = 0; while (so_far < size) { ssize_t r = read(fd, data+so_far, size-so_far); if (r < 0 && errno != EINTR) { fprintf(stderr, "read failed: %s\n", strerror(errno)); - return; + return -1; } else { so_far += r; } } + return 0; } -static void writeblock(int fd, const uint8_t* data, size_t size) { +static int write_all(int fd, const uint8_t* data, size_t size) { size_t written = 0; while (written < size) { ssize_t w = write(fd, data+written, size-written); if (w < 0 && errno != EINTR) { fprintf(stderr, "write failed: %s\n", strerror(errno)); - return; + return -1; } else { written += w; } } + + if (fsync(fd) == -1) { + fprintf(stderr, "fsync failed: %s\n", strerror(errno)); + return -1; + } + + return 0; } -static void check_lseek(int fd, off64_t offset, int whence) { +static int check_lseek(int fd, off64_t offset, int whence) { while (true) { off64_t ret = lseek64(fd, offset, whence); if (ret < 0) { if (errno != EINTR) { fprintf(stderr, "lseek64 failed: %s\n", strerror(errno)); - exit(1); + return -1; } } else { break; } } + return 0; } static void allocate(size_t size, uint8_t** buffer, size_t* buffer_alloc) { @@ -146,14 +185,21 @@ static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) { if (rss->p_remain <= 0) { fprintf(stderr, "range sink write overrun"); - exit(1); + return 0; } ssize_t written = 0; while (size > 0) { size_t write_now = size; - if (rss->p_remain < write_now) write_now = rss->p_remain; - writeblock(rss->fd, data, write_now); + + if (rss->p_remain < write_now) { + write_now = rss->p_remain; + } + + if (write_all(rss->fd, data, write_now) == -1) { + break; + } + data += write_now; size -= write_now; @@ -164,12 +210,17 @@ static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) { // move to the next block ++rss->p_block; if (rss->p_block < rss->tgt->count) { - rss->p_remain = (rss->tgt->pos[rss->p_block*2+1] - rss->tgt->pos[rss->p_block*2]) * BLOCKSIZE; - check_lseek(rss->fd, (off64_t)rss->tgt->pos[rss->p_block*2] * BLOCKSIZE, SEEK_SET); + rss->p_remain = (rss->tgt->pos[rss->p_block * 2 + 1] - + rss->tgt->pos[rss->p_block * 2]) * BLOCKSIZE; + + if (check_lseek(rss->fd, (off64_t)rss->tgt->pos[rss->p_block*2] * BLOCKSIZE, + SEEK_SET) == -1) { + break; + } } else { // we can't write any more; return how many bytes have // been written so far. - return written; + break; } } } @@ -245,6 +296,58 @@ static void* unzip_new_data(void* cookie) { return NULL; } +static int ReadBlocks(RangeSet* src, uint8_t* buffer, int fd) { + int i; + size_t p = 0; + size_t size; + + if (!src || !buffer) { + return -1; + } + + for (i = 0; i < src->count; ++i) { + if (check_lseek(fd, (off64_t) src->pos[i * 2] * BLOCKSIZE, SEEK_SET) == -1) { + return -1; + } + + size = (src->pos[i * 2 + 1] - src->pos[i * 2]) * BLOCKSIZE; + + if (read_all(fd, buffer + p, size) == -1) { + return -1; + } + + p += size; + } + + return 0; +} + +static int WriteBlocks(RangeSet* tgt, uint8_t* buffer, int fd) { + int i; + size_t p = 0; + size_t size; + + if (!tgt || !buffer) { + return -1; + } + + for (i = 0; i < tgt->count; ++i) { + if (check_lseek(fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET) == -1) { + return -1; + } + + size = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * BLOCKSIZE; + + if (write_all(fd, buffer + p, size) == -1) { + return -1; + } + + p += size; + } + + return 0; +} + // Do a source/target load for move/bsdiff/imgdiff in version 1. // 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect // to parse the remainder of the string as: @@ -255,522 +358,1522 @@ static void* unzip_new_data(void* cookie) { // it to make it larger if necessary. The target ranges are returned // in *tgt, if tgt is non-NULL. -static void LoadSrcTgtVersion1(char* wordsave, RangeSet** tgt, int* src_blocks, +static int LoadSrcTgtVersion1(char** wordsave, RangeSet** tgt, int* src_blocks, uint8_t** buffer, size_t* buffer_alloc, int fd) { char* word; + int rc; - word = strtok_r(NULL, " ", &wordsave); + word = strtok_r(NULL, " ", wordsave); RangeSet* src = parse_range(word); if (tgt != NULL) { - word = strtok_r(NULL, " ", &wordsave); + word = strtok_r(NULL, " ", wordsave); *tgt = parse_range(word); } allocate(src->size * BLOCKSIZE, buffer, buffer_alloc); - size_t p = 0; - int i; - for (i = 0; i < src->count; ++i) { - check_lseek(fd, (off64_t)src->pos[i*2] * BLOCKSIZE, SEEK_SET); - size_t sz = (src->pos[i*2+1] - src->pos[i*2]) * BLOCKSIZE; - readblock(fd, *buffer+p, sz); - p += sz; - } - + rc = ReadBlocks(src, *buffer, fd); *src_blocks = src->size; + free(src); + return rc; } -static void MoveRange(uint8_t* dest, RangeSet* locs, const uint8_t* source) { - // source contains packed data, which we want to move to the - // locations given in *locs in the dest buffer. source and dest - // may be the same buffer. +static int VerifyBlocks(const char *expected, const uint8_t *buffer, + size_t blocks, int printerror) { + char* hexdigest = NULL; + int rc = -1; + uint8_t digest[SHA_DIGEST_SIZE]; - int start = locs->size; - int i; - for (i = locs->count-1; i >= 0; --i) { - int blocks = locs->pos[i*2+1] - locs->pos[i*2]; - start -= blocks; - memmove(dest + (locs->pos[i*2] * BLOCKSIZE), source + (start * BLOCKSIZE), - blocks * BLOCKSIZE); + if (!expected || !buffer) { + return rc; } -} -// Do a source/target load for move/bsdiff/imgdiff in version 2. -// 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect -// to parse the remainder of the string as one of: -// -// -// (loads data from source image only) -// -// - <[stash_id:stash_range] ...> -// (loads data from stashes only) -// -// <[stash_id:stash_range] ...> -// (loads data from both source image and stashes) -// -// On return, buffer is filled with the loaded source data (rearranged -// and combined with stashed data as necessary). buffer may be -// reallocated if needed to accommodate the source data. *tgt is the -// target RangeSet. Any stashes required are taken from stash_table -// and free()'d after being used. + SHA_hash(buffer, blocks * BLOCKSIZE, digest); + hexdigest = PrintSha1(digest); -static void LoadSrcTgtVersion2(char* wordsave, RangeSet** tgt, int* src_blocks, - uint8_t** buffer, size_t* buffer_alloc, int fd, - uint8_t** stash_table) { - char* word; + if (hexdigest != NULL) { + rc = strcmp(expected, hexdigest); - if (tgt != NULL) { - word = strtok_r(NULL, " ", &wordsave); - *tgt = parse_range(word); + if (rc != 0 && printerror) { + fprintf(stderr, "failed to verify blocks (expected %s, read %s)\n", + expected, hexdigest); + } + + free(hexdigest); } - word = strtok_r(NULL, " ", &wordsave); - *src_blocks = strtol(word, NULL, 0); + return rc; +} - allocate(*src_blocks * BLOCKSIZE, buffer, buffer_alloc); +static char* GetStashFileName(const char* base, const char* id, const char* postfix) { + char* fn; + int len; + int res; - word = strtok_r(NULL, " ", &wordsave); - if (word[0] == '-' && word[1] == '\0') { - // no source ranges, only stashes - } else { - RangeSet* src = parse_range(word); + if (base == NULL) { + return NULL; + } - size_t p = 0; - int i; - for (i = 0; i < src->count; ++i) { - check_lseek(fd, (off64_t)src->pos[i*2] * BLOCKSIZE, SEEK_SET); - size_t sz = (src->pos[i*2+1] - src->pos[i*2]) * BLOCKSIZE; - readblock(fd, *buffer+p, sz); - p += sz; - } - free(src); + if (id == NULL) { + id = ""; + } - word = strtok_r(NULL, " ", &wordsave); - if (word == NULL) { - // no stashes, only source range - return; - } + if (postfix == NULL) { + postfix = ""; + } - RangeSet* locs = parse_range(word); - MoveRange(*buffer, locs, *buffer); + len = strlen(STASH_DIRECTORY_BASE) + 1 + strlen(base) + 1 + strlen(id) + strlen(postfix) + 1; + fn = malloc(len); + + if (fn == NULL) { + fprintf(stderr, "failed to malloc %d bytes for fn\n", len); + return NULL; } - while ((word = strtok_r(NULL, " ", &wordsave)) != NULL) { - // Each word is a an index into the stash table, a colon, and - // then a rangeset describing where in the source block that - // stashed data should go. - char* colonsave = NULL; - char* colon = strtok_r(word, ":", &colonsave); - int stash_id = strtol(colon, NULL, 0); - colon = strtok_r(NULL, ":", &colonsave); - RangeSet* locs = parse_range(colon); - MoveRange(*buffer, locs, stash_table[stash_id]); - free(stash_table[stash_id]); - stash_table[stash_id] = NULL; - free(locs); + res = snprintf(fn, len, STASH_DIRECTORY_BASE "/%s/%s%s", base, id, postfix); + + if (res < 0 || res >= len) { + fprintf(stderr, "failed to format file name (return value %d)\n", res); + free(fn); + return NULL; } + + return fn; } -// args: -// - block device (or file) to modify in-place -// - transfer list (blob) -// - new data stream (filename within package.zip) -// - patch stream (filename within package.zip, must be uncompressed) +typedef void (*StashCallback)(const char*, void*); -Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[]) { - Value* blockdev_filename; - Value* transfer_list_value; - char* transfer_list = NULL; - Value* new_data_fn; - Value* patch_data_fn; - bool success = false; +// Does a best effort enumeration of stash files. Ignores possible non-file +// items in the stash directory and continues despite of errors. Calls the +// 'callback' function for each file and passes 'data' to the function as a +// parameter. - if (ReadValueArgs(state, argv, 4, &blockdev_filename, &transfer_list_value, - &new_data_fn, &patch_data_fn) < 0) { - return NULL; - } +static void EnumerateStash(const char* dirname, StashCallback callback, void* data) { + char* fn; + DIR* directory; + int len; + int res; + struct dirent* item; - if (blockdev_filename->type != VAL_STRING) { - ErrorAbort(state, "blockdev_filename argument to %s must be string", name); - goto done; + if (dirname == NULL || callback == NULL) { + return; } - if (transfer_list_value->type != VAL_BLOB) { - ErrorAbort(state, "transfer_list argument to %s must be blob", name); - goto done; + + directory = opendir(dirname); + + if (directory == NULL) { + if (errno != ENOENT) { + fprintf(stderr, "failed to opendir %s (errno %d)\n", dirname, errno); + } + return; } - if (new_data_fn->type != VAL_STRING) { - ErrorAbort(state, "new_data_fn argument to %s must be string", name); - goto done; + + while ((item = readdir(directory)) != NULL) { + if (item->d_type != DT_REG) { + continue; + } + + len = strlen(dirname) + 1 + strlen(item->d_name) + 1; + fn = malloc(len); + + if (fn == NULL) { + fprintf(stderr, "failed to malloc %d bytes for fn\n", len); + continue; + } + + res = snprintf(fn, len, "%s/%s", dirname, item->d_name); + + if (res < 0 || res >= len) { + fprintf(stderr, "failed to format file name (return value %d)\n", res); + free(fn); + continue; + } + + callback(fn, data); + free(fn); } - if (patch_data_fn->type != VAL_STRING) { - ErrorAbort(state, "patch_data_fn argument to %s must be string", name); - goto done; + + if (closedir(directory) == -1) { + fprintf(stderr, "failed to closedir %s (errno %d)\n", dirname, errno); } +} - UpdaterInfo* ui = (UpdaterInfo*)(state->cookie); - FILE* cmd_pipe = ui->cmd_pipe; +static void UpdateFileSize(const char* fn, void* data) { + int* size = (int*) data; + struct stat st; - ZipArchive* za = ((UpdaterInfo*)(state->cookie))->package_zip; + if (!fn || !data) { + return; + } - const ZipEntry* patch_entry = mzFindZipEntry(za, patch_data_fn->data); - if (patch_entry == NULL) { - ErrorAbort(state, "%s(): no file \"%s\" in package", name, patch_data_fn->data); - goto done; + if (stat(fn, &st) == -1) { + fprintf(stderr, "failed to stat %s (errno %d)\n", fn, errno); + return; } - uint8_t* patch_start = ((UpdaterInfo*)(state->cookie))->package_zip_addr + - mzGetZipEntryOffset(patch_entry); + *size += st.st_size; +} - const ZipEntry* new_entry = mzFindZipEntry(za, new_data_fn->data); - if (new_entry == NULL) { - ErrorAbort(state, "%s(): no file \"%s\" in package", name, new_data_fn->data); - goto done; +// Deletes the stash directory and all files in it. Assumes that it only +// contains files. There is nothing we can do about unlikely, but possible +// errors, so they are merely logged. + +static void DeleteFile(const char* fn, void* data) { + if (fn) { + fprintf(stderr, "deleting %s\n", fn); + + if (unlink(fn) == -1 && errno != ENOENT) { + fprintf(stderr, "failed to unlink %s (errno %d)\n", fn, errno); + } } +} - // The transfer list is a text file containing commands to - // transfer data from one place to another on the target - // partition. We parse it and execute the commands in order: - // - // zero [rangeset] - // - fill the indicated blocks with zeros - // - // new [rangeset] - // - fill the blocks with data read from the new_data file - // - // erase [rangeset] - // - mark the given blocks as empty - // - // move <...> - // bsdiff <...> - // imgdiff <...> - // - read the source blocks, apply a patch (or not in the - // case of move), write result to target blocks. bsdiff or - // imgdiff specifies the type of patch; move means no patch - // at all. - // - // The format of <...> differs between versions 1 and 2; - // see the LoadSrcTgtVersion{1,2}() functions for a - // description of what's expected. - // - // stash - // - (version 2 only) load the given source range and stash - // the data in the given slot of the stash table. - // - // The creator of the transfer list will guarantee that no block - // is read (ie, used as the source for a patch or move) after it - // has been written. - // - // In version 2, the creator will guarantee that a given stash is - // loaded (with a stash command) before it's used in a - // move/bsdiff/imgdiff command. - // - // Within one command the source and target ranges may overlap so - // in general we need to read the entire source into memory before - // writing anything to the target blocks. - // - // All the patch data is concatenated into one patch_data file in - // the update package. It must be stored uncompressed because we - // memory-map it in directly from the archive. (Since patches are - // already compressed, we lose very little by not compressing - // their concatenation.) - - pthread_t new_data_thread; - NewThreadInfo nti; - nti.za = za; - nti.entry = new_entry; - nti.rss = NULL; - pthread_mutex_init(&nti.mu, NULL); - pthread_cond_init(&nti.cv, NULL); +static void DeletePartial(const char* fn, void* data) { + if (fn && strstr(fn, ".partial") != NULL) { + DeleteFile(fn, data); + } +} - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); - pthread_create(&new_data_thread, &attr, unzip_new_data, &nti); +static void DeleteStash(const char* base) { + char* dirname; - int i, j; + if (base == NULL) { + return; + } - char* linesave; - char* wordsave; + dirname = GetStashFileName(base, NULL, NULL); - int fd = open(blockdev_filename->data, O_RDWR); - if (fd < 0) { - ErrorAbort(state, "failed to open %s: %s", blockdev_filename->data, strerror(errno)); - goto done; + if (dirname == NULL) { + return; } - char* line; - char* word; + fprintf(stderr, "deleting stash %s\n", base); + EnumerateStash(dirname, DeleteFile, NULL); - // The data in transfer_list_value is not necessarily - // null-terminated, so we need to copy it to a new buffer and add - // the null that strtok_r will need. - transfer_list = malloc(transfer_list_value->size+1); - if (transfer_list == NULL) { - fprintf(stderr, "failed to allocate %zd bytes for transfer list\n", - transfer_list_value->size+1); - exit(1); + if (rmdir(dirname) == -1) { + if (errno != ENOENT && errno != ENOTDIR) { + fprintf(stderr, "failed to rmdir %s (errno %d)\n", dirname, errno); + } } - memcpy(transfer_list, transfer_list_value->data, transfer_list_value->size); - transfer_list[transfer_list_value->size] = '\0'; - line = strtok_r(transfer_list, "\n", &linesave); + free(dirname); +} - int version; - // first line in transfer list is the version number; currently - // there's only version 1. - if (strcmp(line, "1") == 0) { - version = 1; - } else if (strcmp(line, "2") == 0) { - version = 2; - } else { - ErrorAbort(state, "unexpected transfer list version [%s]\n", line); - goto done; +static int LoadStash(const char* base, const char* id, int verify, int* blocks, uint8_t** buffer, + size_t* buffer_alloc, int printnoent) { + char *fn = NULL; + int blockcount = 0; + int fd = -1; + int rc = -1; + int res; + struct stat st; + + if (!base || !id || !buffer || !buffer_alloc) { + goto lsout; } - printf("blockimg version is %d\n", version); - // second line in transfer list is the total number of blocks we - // expect to write. - line = strtok_r(NULL, "\n", &linesave); - int total_blocks = strtol(line, NULL, 0); - // shouldn't happen, but avoid divide by zero. - if (total_blocks == 0) ++total_blocks; - int blocks_so_far = 0; - - uint8_t** stash_table = NULL; - if (version >= 2) { - // Next line is how many stash entries are needed simultaneously. - line = strtok_r(NULL, "\n", &linesave); - int stash_entries = strtol(line, NULL, 0); + if (!blocks) { + blocks = &blockcount; + } - stash_table = (uint8_t**) calloc(stash_entries, sizeof(uint8_t*)); - if (stash_table == NULL) { - fprintf(stderr, "failed to allocate %d-entry stash table\n", stash_entries); - exit(1); - } + fn = GetStashFileName(base, id, NULL); - // Next line is the maximum number of blocks that will be - // stashed simultaneously. This could be used to verify that - // enough memory or scratch disk space is available. - line = strtok_r(NULL, "\n", &linesave); - int stash_max_blocks = strtol(line, NULL, 0); + if (fn == NULL) { + goto lsout; } - uint8_t* buffer = NULL; - size_t buffer_alloc = 0; + res = stat(fn, &st); - // third and subsequent lines are all individual transfer commands. - for (line = strtok_r(NULL, "\n", &linesave); line; - line = strtok_r(NULL, "\n", &linesave)) { + if (res == -1) { + if (errno != ENOENT || printnoent) { + fprintf(stderr, "failed to stat %s (errno %d)\n", fn, errno); + } + goto lsout; + } - char* style; - style = strtok_r(line, " ", &wordsave); - - if (strcmp("move", style) == 0) { - RangeSet* tgt; - int src_blocks; - if (version == 1) { - LoadSrcTgtVersion1(wordsave, &tgt, &src_blocks, - &buffer, &buffer_alloc, fd); - } else if (version == 2) { - LoadSrcTgtVersion2(wordsave, &tgt, &src_blocks, - &buffer, &buffer_alloc, fd, stash_table); - } + fprintf(stderr, " loading %s\n", fn); - printf(" moving %d blocks\n", src_blocks); + if ((st.st_size % BLOCKSIZE) != 0) { + fprintf(stderr, "%s size %zd not multiple of block size %d", fn, st.st_size, BLOCKSIZE); + goto lsout; + } - size_t p = 0; - for (i = 0; i < tgt->count; ++i) { - check_lseek(fd, (off64_t)tgt->pos[i*2] * BLOCKSIZE, SEEK_SET); - size_t sz = (tgt->pos[i*2+1] - tgt->pos[i*2]) * BLOCKSIZE; - writeblock(fd, buffer+p, sz); - p += sz; - } + fd = TEMP_FAILURE_RETRY(open(fn, O_RDONLY)); - blocks_so_far += tgt->size; - fprintf(cmd_pipe, "set_progress %.4f\n", (double)blocks_so_far / total_blocks); - fflush(cmd_pipe); + if (fd == -1) { + fprintf(stderr, "failed to open %s (errno %d)\n", fn, errno); + goto lsout; + } - free(tgt); - - } else if (strcmp("stash", style) == 0) { - word = strtok_r(NULL, " ", &wordsave); - int stash_id = strtol(word, NULL, 0); - int src_blocks; - size_t stash_alloc = 0; - - // Even though the "stash" style only appears in version - // 2, the version 1 source loader happens to do exactly - // what we want to read data into the stash_table. - LoadSrcTgtVersion1(wordsave, NULL, &src_blocks, - stash_table + stash_id, &stash_alloc, fd); - - } else if (strcmp("zero", style) == 0 || - (DEBUG_ERASE && strcmp("erase", style) == 0)) { - word = strtok_r(NULL, " ", &wordsave); - RangeSet* tgt = parse_range(word); - - printf(" zeroing %d blocks\n", tgt->size); - - allocate(BLOCKSIZE, &buffer, &buffer_alloc); - memset(buffer, 0, BLOCKSIZE); - for (i = 0; i < tgt->count; ++i) { - check_lseek(fd, (off64_t)tgt->pos[i*2] * BLOCKSIZE, SEEK_SET); - for (j = tgt->pos[i*2]; j < tgt->pos[i*2+1]; ++j) { - writeblock(fd, buffer, BLOCKSIZE); - } - } + allocate(st.st_size, buffer, buffer_alloc); - if (style[0] == 'z') { // "zero" but not "erase" - blocks_so_far += tgt->size; - fprintf(cmd_pipe, "set_progress %.4f\n", (double)blocks_so_far / total_blocks); - fflush(cmd_pipe); - } + if (read_all(fd, *buffer, st.st_size) == -1) { + goto lsout; + } - free(tgt); - } else if (strcmp("new", style) == 0) { + *blocks = st.st_size / BLOCKSIZE; - word = strtok_r(NULL, " ", &wordsave); - RangeSet* tgt = parse_range(word); + if (verify && VerifyBlocks(id, *buffer, *blocks, 1) != 0) { + fprintf(stderr, "unexpected contents in %s\n", fn); + DeleteFile(fn, NULL); + goto lsout; + } - printf(" writing %d blocks of new data\n", tgt->size); + rc = 0; - RangeSinkState rss; - rss.fd = fd; - rss.tgt = tgt; - rss.p_block = 0; - rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE; - check_lseek(fd, (off64_t)tgt->pos[0] * BLOCKSIZE, SEEK_SET); +lsout: + if (fd != -1) { + TEMP_FAILURE_RETRY(close(fd)); + } - pthread_mutex_lock(&nti.mu); - nti.rss = &rss; - pthread_cond_broadcast(&nti.cv); - while (nti.rss) { - pthread_cond_wait(&nti.cv, &nti.mu); - } - pthread_mutex_unlock(&nti.mu); + if (fn) { + free(fn); + } - blocks_so_far += tgt->size; - fprintf(cmd_pipe, "set_progress %.4f\n", (double)blocks_so_far / total_blocks); - fflush(cmd_pipe); + return rc; +} - free(tgt); - - } else if (strcmp("bsdiff", style) == 0 || - strcmp("imgdiff", style) == 0) { - word = strtok_r(NULL, " ", &wordsave); - size_t patch_offset = strtoul(word, NULL, 0); - word = strtok_r(NULL, " ", &wordsave); - size_t patch_len = strtoul(word, NULL, 0); - - RangeSet* tgt; - int src_blocks; - if (version == 1) { - LoadSrcTgtVersion1(wordsave, &tgt, &src_blocks, - &buffer, &buffer_alloc, fd); - } else if (version == 2) { - LoadSrcTgtVersion2(wordsave, &tgt, &src_blocks, - &buffer, &buffer_alloc, fd, stash_table); - } +static int WriteStash(const char* base, const char* id, int blocks, uint8_t* buffer, + int checkspace) { + char *fn = NULL; + char *cn = NULL; + int fd = -1; + int rc = -1; + int res; - printf(" patching %d blocks to %d\n", src_blocks, tgt->size); + if (base == NULL || buffer == NULL) { + goto wsout; + } - Value patch_value; - patch_value.type = VAL_BLOB; - patch_value.size = patch_len; - patch_value.data = (char*)(patch_start + patch_offset); + if (checkspace && CacheSizeCheck(blocks * BLOCKSIZE) != 0) { + fprintf(stderr, "not enough space to write stash\n"); + goto wsout; + } - RangeSinkState rss; - rss.fd = fd; - rss.tgt = tgt; - rss.p_block = 0; - rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE; - check_lseek(fd, (off64_t)tgt->pos[0] * BLOCKSIZE, SEEK_SET); + fn = GetStashFileName(base, id, ".partial"); + cn = GetStashFileName(base, id, NULL); - if (style[0] == 'i') { // imgdiff - ApplyImagePatch(buffer, src_blocks * BLOCKSIZE, - &patch_value, - &RangeSinkWrite, &rss, NULL, NULL); - } else { - ApplyBSDiffPatch(buffer, src_blocks * BLOCKSIZE, - &patch_value, 0, - &RangeSinkWrite, &rss, NULL); - } + if (fn == NULL || cn == NULL) { + goto wsout; + } - // We expect the output of the patcher to fill the tgt ranges exactly. - if (rss.p_block != tgt->count || rss.p_remain != 0) { - fprintf(stderr, "range sink underrun?\n"); - } + fprintf(stderr, " writing %d blocks to %s\n", blocks, cn); - blocks_so_far += tgt->size; - fprintf(cmd_pipe, "set_progress %.4f\n", (double)blocks_so_far / total_blocks); - fflush(cmd_pipe); + fd = TEMP_FAILURE_RETRY(open(fn, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, STASH_FILE_MODE)); - free(tgt); - } else if (!DEBUG_ERASE && strcmp("erase", style) == 0) { - struct stat st; - if (fstat(fd, &st) == 0 && S_ISBLK(st.st_mode)) { - word = strtok_r(NULL, " ", &wordsave); - RangeSet* tgt = parse_range(word); - - printf(" erasing %d blocks\n", tgt->size); - - for (i = 0; i < tgt->count; ++i) { - uint64_t range[2]; - // offset in bytes - range[0] = tgt->pos[i*2] * (uint64_t)BLOCKSIZE; - // len in bytes - range[1] = (tgt->pos[i*2+1] - tgt->pos[i*2]) * (uint64_t)BLOCKSIZE; - - if (ioctl(fd, BLKDISCARD, &range) < 0) { - printf(" blkdiscard failed: %s\n", strerror(errno)); - } - } + if (fd == -1) { + fprintf(stderr, "failed to create %s (errno %d)\n", fn, errno); + goto wsout; + } - free(tgt); - } else { - printf(" ignoring erase (not block device)\n"); - } - } else { - fprintf(stderr, "unknown transfer style \"%s\"\n", style); - exit(1); - } + if (write_all(fd, buffer, blocks * BLOCKSIZE) == -1) { + goto wsout; } - pthread_join(new_data_thread, NULL); - success = true; + if (fsync(fd) == -1) { + fprintf(stderr, "failed to fsync %s (errno %d)\n", fn, errno); + goto wsout; + } - free(buffer); - printf("wrote %d blocks; expected %d\n", blocks_so_far, total_blocks); - printf("max alloc needed was %zu\n", buffer_alloc); + if (rename(fn, cn) == -1) { + fprintf(stderr, "failed to rename %s to %s (errno %d)\n", fn, cn, errno); + goto wsout; + } - done: - free(transfer_list); - FreeValue(blockdev_filename); - FreeValue(transfer_list_value); - FreeValue(new_data_fn); - FreeValue(patch_data_fn); - return StringValue(success ? strdup("t") : strdup("")); -} + rc = 0; -Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) { - Value* blockdev_filename; - Value* ranges; - const uint8_t* digest = NULL; - if (ReadValueArgs(state, argv, 2, &blockdev_filename, &ranges) < 0) { - return NULL; +wsout: + if (fd != -1) { + TEMP_FAILURE_RETRY(close(fd)); } - if (blockdev_filename->type != VAL_STRING) { - ErrorAbort(state, "blockdev_filename argument to %s must be string", name); - goto done; + if (fn) { + free(fn); } - if (ranges->type != VAL_STRING) { - ErrorAbort(state, "ranges argument to %s must be string", name); - goto done; + + if (cn) { + free(cn); } - int fd = open(blockdev_filename->data, O_RDWR); + return rc; +} + +// Creates a directory for storing stash files and checks if the /cache partition +// hash enough space for the expected amount of blocks we need to store. Returns +// >0 if we created the directory, zero if it existed already, and <0 of failure. + +static int CreateStash(State* state, int maxblocks, const char* blockdev, char** base) { + char* dirname = NULL; + const uint8_t* digest; + int rc = -1; + int res; + int size = 0; + SHA_CTX ctx; + struct stat st; + + if (blockdev == NULL || base == NULL) { + goto csout; + } + + // Stash directory should be different for each partition to avoid conflicts + // when updating multiple partitions at the same time, so we use the hash of + // the block device name as the base directory + SHA_init(&ctx); + SHA_update(&ctx, blockdev, strlen(blockdev)); + digest = SHA_final(&ctx); + *base = PrintSha1(digest); + + if (*base == NULL) { + goto csout; + } + + dirname = GetStashFileName(*base, NULL, NULL); + + if (dirname == NULL) { + goto csout; + } + + res = stat(dirname, &st); + + if (res == -1 && errno != ENOENT) { + ErrorAbort(state, "failed to stat %s (errno %d)\n", dirname, errno); + goto csout; + } else if (res != 0) { + fprintf(stderr, "creating stash %s\n", dirname); + res = mkdir(dirname, STASH_DIRECTORY_MODE); + + if (res != 0) { + ErrorAbort(state, "failed to create %s (errno %d)\n", dirname, errno); + goto csout; + } + + if (CacheSizeCheck(maxblocks * BLOCKSIZE) != 0) { + ErrorAbort(state, "not enough space for stash\n"); + goto csout; + } + + rc = 1; // Created directory + goto csout; + } + + fprintf(stderr, "using existing stash %s\n", dirname); + + // If the directory already exists, calculate the space already allocated to + // stash files and check if there's enough for all required blocks. Delete any + // partially completed stash files first. + + EnumerateStash(dirname, DeletePartial, NULL); + EnumerateStash(dirname, UpdateFileSize, &size); + + size = (maxblocks * BLOCKSIZE) - size; + + if (size > 0 && CacheSizeCheck(size) != 0) { + ErrorAbort(state, "not enough space for stash (%d more needed)\n", size); + goto csout; + } + + rc = 0; // Using existing directory + +csout: + if (dirname) { + free(dirname); + } + + return rc; +} + +static int SaveStash(const char* base, char** wordsave, uint8_t** buffer, size_t* buffer_alloc, + int fd, int usehash, int* isunresumable) { + char *id = NULL; + int res = -1; + int blocks = 0; + + if (!wordsave || !buffer || !buffer_alloc || !isunresumable) { + return -1; + } + + id = strtok_r(NULL, " ", wordsave); + + if (id == NULL) { + fprintf(stderr, "missing id field in stash command\n"); + return -1; + } + + if (usehash && LoadStash(base, id, 1, &blocks, buffer, buffer_alloc, 0) == 0) { + // Stash file already exists and has expected contents. Do not + // read from source again, as the source may have been already + // overwritten during a previous attempt. + return 0; + } + + if (LoadSrcTgtVersion1(wordsave, NULL, &blocks, buffer, buffer_alloc, fd) == -1) { + return -1; + } + + if (usehash && VerifyBlocks(id, *buffer, blocks, 1) != 0) { + // Source blocks have unexpected contents. If we actually need this + // data later, this is an unrecoverable error. However, the command + // that uses the data may have already completed previously, so the + // possible failure will occur during source block verification. + fprintf(stderr, "failed to load source blocks for stash %s\n", id); + return 0; + } + + fprintf(stderr, "stashing %d blocks to %s\n", blocks, id); + return WriteStash(base, id, blocks, *buffer, 0); +} + +static int FreeStash(const char* base, const char* id) { + char *fn = NULL; + + if (base == NULL || id == NULL) { + return -1; + } + + fn = GetStashFileName(base, id, NULL); + + if (fn == NULL) { + return -1; + } + + DeleteFile(fn, NULL); + free(fn); + + return 0; +} + +static void MoveRange(uint8_t* dest, RangeSet* locs, const uint8_t* source) { + // source contains packed data, which we want to move to the + // locations given in *locs in the dest buffer. source and dest + // may be the same buffer. + + int start = locs->size; + int i; + for (i = locs->count-1; i >= 0; --i) { + int blocks = locs->pos[i*2+1] - locs->pos[i*2]; + start -= blocks; + memmove(dest + (locs->pos[i*2] * BLOCKSIZE), source + (start * BLOCKSIZE), + blocks * BLOCKSIZE); + } +} + +// Do a source/target load for move/bsdiff/imgdiff in version 2. +// 'wordsave' is the save_ptr of a strtok_r()-in-progress. We expect +// to parse the remainder of the string as one of: +// +// +// (loads data from source image only) +// +// - <[stash_id:stash_range] ...> +// (loads data from stashes only) +// +// <[stash_id:stash_range] ...> +// (loads data from both source image and stashes) +// +// On return, buffer is filled with the loaded source data (rearranged +// and combined with stashed data as necessary). buffer may be +// reallocated if needed to accommodate the source data. *tgt is the +// target RangeSet. Any stashes required are loaded using LoadStash. + +static int LoadSrcTgtVersion2(char** wordsave, RangeSet** tgt, int* src_blocks, + uint8_t** buffer, size_t* buffer_alloc, int fd, + const char* stashbase, int* overlap) { + char* word; + char* colonsave; + char* colon; + int id; + int res; + RangeSet* locs; + size_t stashalloc = 0; + uint8_t* stash = NULL; + + if (tgt != NULL) { + word = strtok_r(NULL, " ", wordsave); + *tgt = parse_range(word); + } + + word = strtok_r(NULL, " ", wordsave); + *src_blocks = strtol(word, NULL, 0); + + allocate(*src_blocks * BLOCKSIZE, buffer, buffer_alloc); + + word = strtok_r(NULL, " ", wordsave); + if (word[0] == '-' && word[1] == '\0') { + // no source ranges, only stashes + } else { + RangeSet* src = parse_range(word); + res = ReadBlocks(src, *buffer, fd); + + if (overlap && tgt) { + *overlap = range_overlaps(src, *tgt); + } + + free(src); + + if (res == -1) { + return -1; + } + + word = strtok_r(NULL, " ", wordsave); + if (word == NULL) { + // no stashes, only source range + return 0; + } + + locs = parse_range(word); + MoveRange(*buffer, locs, *buffer); + free(locs); + } + + while ((word = strtok_r(NULL, " ", wordsave)) != NULL) { + // Each word is a an index into the stash table, a colon, and + // then a rangeset describing where in the source block that + // stashed data should go. + colonsave = NULL; + colon = strtok_r(word, ":", &colonsave); + + res = LoadStash(stashbase, colon, 0, NULL, &stash, &stashalloc, 1); + + if (res == -1) { + // These source blocks will fail verification if used later, but we + // will let the caller decide if this is a fatal failure + fprintf(stderr, "failed to load stash %s\n", colon); + continue; + } + + colon = strtok_r(NULL, ":", &colonsave); + locs = parse_range(colon); + + MoveRange(*buffer, locs, stash); + free(locs); + } + + if (stash) { + free(stash); + } + + return 0; +} + +// Parameters for transfer list command functions +typedef struct { + char* cmdname; + char* cpos; + char* freestash; + char* stashbase; + int canwrite; + int createdstash; + int fd; + int foundwrites; + int isunresumable; + int version; + int written; + NewThreadInfo nti; + pthread_t thread; + size_t bufsize; + uint8_t* buffer; + uint8_t* patch_start; +} CommandParameters; + +// Do a source/target load for move/bsdiff/imgdiff in version 3. +// +// Parameters are the same as for LoadSrcTgtVersion2, except for 'onehash', which +// tells the function whether to expect separate source and targe block hashes, or +// if they are both the same and only one hash should be expected, and +// 'isunresumable', which receives a non-zero value if block verification fails in +// a way that the update cannot be resumed anymore. +// +// If the function is unable to load the necessary blocks or their contents don't +// match the hashes, the return value is -1 and the command should be aborted. +// +// If the return value is 1, the command has already been completed according to +// the contents of the target blocks, and should not be performed again. +// +// If the return value is 0, source blocks have expected content and the command +// can be performed. + +static int LoadSrcTgtVersion3(CommandParameters* params, RangeSet** tgt, int* src_blocks, + int onehash, int* overlap) { + char* srchash = NULL; + char* tgthash = NULL; + int overlap_blocks = 0; + int rc = -1; + uint8_t* tgtbuffer = NULL; + + if (!params|| !tgt || !src_blocks || !overlap) { + goto v3out; + } + + srchash = strtok_r(NULL, " ", ¶ms->cpos); + + if (srchash == NULL) { + fprintf(stderr, "missing source hash\n"); + goto v3out; + } + + if (onehash) { + tgthash = srchash; + } else { + tgthash = strtok_r(NULL, " ", ¶ms->cpos); + + if (tgthash == NULL) { + fprintf(stderr, "missing target hash\n"); + goto v3out; + } + } + + if (LoadSrcTgtVersion2(¶ms->cpos, tgt, src_blocks, ¶ms->buffer, ¶ms->bufsize, + params->fd, params->stashbase, overlap) == -1) { + goto v3out; + } + + tgtbuffer = (uint8_t*) malloc((*tgt)->size * BLOCKSIZE); + + if (tgtbuffer == NULL) { + fprintf(stderr, "failed to allocate %d bytes\n", (*tgt)->size * BLOCKSIZE); + goto v3out; + } + + if (ReadBlocks(*tgt, tgtbuffer, params->fd) == -1) { + goto v3out; + } + + if (VerifyBlocks(tgthash, tgtbuffer, (*tgt)->size, 0) == 0) { + // Target blocks already have expected content, command should be skipped + rc = 1; + goto v3out; + } + + if (VerifyBlocks(srchash, params->buffer, *src_blocks, 1) == 0) { + // If source and target blocks overlap, stash the source blocks so we can + // resume from possible write errors + if (*overlap) { + fprintf(stderr, "stashing %d overlapping blocks to %s\n", *src_blocks, + srchash); + + if (WriteStash(params->stashbase, srchash, *src_blocks, params->buffer, 1) != 0) { + fprintf(stderr, "failed to stash overlapping source blocks\n"); + goto v3out; + } + + // Can be deleted when the write has completed + params->freestash = srchash; + } + + // Source blocks have expected content, command can proceed + rc = 0; + goto v3out; + } + + if (*overlap && LoadStash(params->stashbase, srchash, 1, NULL, ¶ms->buffer, + ¶ms->bufsize, 1) == 0) { + // Overlapping source blocks were previously stashed, command can proceed + if (params->canwrite) { + // We didn't create the stash, so delete after write only if we will + // actually perform the write + params->freestash = srchash; + } + rc = 0; + goto v3out; + } + + // Valid source data not available, update cannot be resumed + fprintf(stderr, "partition has unexpected contents\n"); + params->isunresumable = 1; + +v3out: + if (tgtbuffer) { + free(tgtbuffer); + } + + return rc; +} + +static int PerformCommandMove(CommandParameters* params) { + int blocks = 0; + int overlap = 0; + int rc = -1; + int status = 0; + RangeSet* tgt = NULL; + + if (!params) { + goto pcmout; + } + + if (params->version == 1) { + status = LoadSrcTgtVersion1(¶ms->cpos, &tgt, &blocks, ¶ms->buffer, + ¶ms->bufsize, params->fd); + } else if (params->version == 2) { + status = LoadSrcTgtVersion2(¶ms->cpos, &tgt, &blocks, ¶ms->buffer, + ¶ms->bufsize, params->fd, params->stashbase, NULL); + } else if (params->version >= 3) { + status = LoadSrcTgtVersion3(params, &tgt, &blocks, 1, &overlap); + } + + if (status == -1) { + fprintf(stderr, "failed to read blocks for move\n"); + goto pcmout; + } + + if (status == 0) { + params->foundwrites = 1; + } else if (params->foundwrites) { + fprintf(stderr, "warning: commands executed out of order [%s]\n", params->cmdname); + } + + if (params->canwrite) { + if (status == 0) { + fprintf(stderr, " moving %d blocks\n", blocks); + + if (WriteBlocks(tgt, params->buffer, params->fd) == -1) { + goto pcmout; + } + } else { + fprintf(stderr, "skipping %d already moved blocks\n", blocks); + } + + } + + if (params->freestash) { + FreeStash(params->stashbase, params->freestash); + params->freestash = NULL; + } + + params->written += tgt->size; + rc = 0; + +pcmout: + if (tgt) { + free(tgt); + } + + return rc; +} + +static int PerformCommandStash(CommandParameters* params) { + if (!params) { + return -1; + } + + return SaveStash(params->stashbase, ¶ms->cpos, ¶ms->buffer, ¶ms->bufsize, + params->fd, (params->version >= 3), ¶ms->isunresumable); +} + +static int PerformCommandFree(CommandParameters* params) { + if (!params) { + return -1; + } + + if (params->createdstash || params->canwrite) { + return FreeStash(params->stashbase, params->cpos); + } + + return 0; +} + +static int PerformCommandZero(CommandParameters* params) { + char* range = NULL; + int i; + int j; + int rc = -1; + RangeSet* tgt = NULL; + + if (!params) { + goto pczout; + } + + range = strtok_r(NULL, " ", ¶ms->cpos); + + if (range == NULL) { + fprintf(stderr, "missing target blocks for zero\n"); + goto pczout; + } + + tgt = parse_range(range); + + fprintf(stderr, " zeroing %d blocks\n", tgt->size); + + allocate(BLOCKSIZE, ¶ms->buffer, ¶ms->bufsize); + memset(params->buffer, 0, BLOCKSIZE); + + if (params->canwrite) { + for (i = 0; i < tgt->count; ++i) { + if (check_lseek(params->fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET) == -1) { + goto pczout; + } + + for (j = tgt->pos[i * 2]; j < tgt->pos[i * 2 + 1]; ++j) { + if (write_all(params->fd, params->buffer, BLOCKSIZE) == -1) { + goto pczout; + } + } + } + } + + if (params->cmdname[0] == 'z') { + // Update only for the zero command, as the erase command will call + // this if DEBUG_ERASE is defined. + params->written += tgt->size; + } + + rc = 0; + +pczout: + if (tgt) { + free(tgt); + } + + return rc; +} + +static int PerformCommandNew(CommandParameters* params) { + char* range = NULL; + int rc = -1; + RangeSet* tgt = NULL; + RangeSinkState rss; + + if (!params) { + goto pcnout; + } + + range = strtok_r(NULL, " ", ¶ms->cpos); + + if (range == NULL) { + goto pcnout; + } + + tgt = parse_range(range); + + if (params->canwrite) { + fprintf(stderr, " writing %d blocks of new data\n", tgt->size); + + rss.fd = params->fd; + rss.tgt = tgt; + rss.p_block = 0; + rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE; + + if (check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET) == -1) { + goto pcnout; + } + + pthread_mutex_lock(¶ms->nti.mu); + params->nti.rss = &rss; + pthread_cond_broadcast(¶ms->nti.cv); + + while (params->nti.rss) { + pthread_cond_wait(¶ms->nti.cv, ¶ms->nti.mu); + } + + pthread_mutex_unlock(¶ms->nti.mu); + } + + params->written += tgt->size; + rc = 0; + +pcnout: + if (tgt) { + free(tgt); + } + + return rc; +} + +static int PerformCommandDiff(CommandParameters* params) { + char* logparams = NULL; + char* value = NULL; + int blocks = 0; + int overlap = 0; + int rc = -1; + int status = 0; + RangeSet* tgt = NULL; + RangeSinkState rss; + size_t len = 0; + size_t offset = 0; + Value patch_value; + + if (!params) { + goto pcdout; + } + + logparams = strdup(params->cpos); + value = strtok_r(NULL, " ", ¶ms->cpos); + + if (value == NULL) { + fprintf(stderr, "missing patch offset for %s\n", params->cmdname); + goto pcdout; + } + + offset = strtoul(value, NULL, 0); + + value = strtok_r(NULL, " ", ¶ms->cpos); + + if (value == NULL) { + fprintf(stderr, "missing patch length for %s\n", params->cmdname); + goto pcdout; + } + + len = strtoul(value, NULL, 0); + + if (params->version == 1) { + status = LoadSrcTgtVersion1(¶ms->cpos, &tgt, &blocks, ¶ms->buffer, + ¶ms->bufsize, params->fd); + } else if (params->version == 2) { + status = LoadSrcTgtVersion2(¶ms->cpos, &tgt, &blocks, ¶ms->buffer, + ¶ms->bufsize, params->fd, params->stashbase, NULL); + } else if (params->version >= 3) { + status = LoadSrcTgtVersion3(params, &tgt, &blocks, 0, &overlap); + } + + if (status == -1) { + fprintf(stderr, "failed to read blocks for diff\n"); + goto pcdout; + } + + if (status == 0) { + params->foundwrites = 1; + } else if (params->foundwrites) { + fprintf(stderr, "warning: commands executed out of order [%s]\n", params->cmdname); + } + + if (params->canwrite) { + if (status == 0) { + fprintf(stderr, "patching %d blocks to %d\n", blocks, tgt->size); + + patch_value.type = VAL_BLOB; + patch_value.size = len; + patch_value.data = (char*) (params->patch_start + offset); + + rss.fd = params->fd; + rss.tgt = tgt; + rss.p_block = 0; + rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE; + + if (check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET) == -1) { + goto pcdout; + } + + if (params->cmdname[0] == 'i') { // imgdiff + ApplyImagePatch(params->buffer, blocks * BLOCKSIZE, &patch_value, + &RangeSinkWrite, &rss, NULL, NULL); + } else { + ApplyBSDiffPatch(params->buffer, blocks * BLOCKSIZE, &patch_value, + 0, &RangeSinkWrite, &rss, NULL); + } + + // We expect the output of the patcher to fill the tgt ranges exactly. + if (rss.p_block != tgt->count || rss.p_remain != 0) { + fprintf(stderr, "range sink underrun?\n"); + } + } else { + fprintf(stderr, "skipping %d blocks already patched to %d [%s]\n", + blocks, tgt->size, logparams); + } + } + + if (params->freestash) { + FreeStash(params->stashbase, params->freestash); + params->freestash = NULL; + } + + params->written += tgt->size; + rc = 0; + +pcdout: + if (logparams) { + free(logparams); + } + + if (tgt) { + free(tgt); + } + + return rc; +} + +static int PerformCommandErase(CommandParameters* params) { + char* range = NULL; + int i; + int rc = -1; + RangeSet* tgt = NULL; + struct stat st; + uint64_t blocks[2]; + + if (DEBUG_ERASE) { + return PerformCommandZero(params); + } + + if (!params) { + goto pceout; + } + + if (fstat(params->fd, &st) == -1) { + fprintf(stderr, "failed to fstat device to erase (errno %d)\n", errno); + goto pceout; + } + + if (!S_ISBLK(st.st_mode)) { + fprintf(stderr, "not a block device; skipping erase\n"); + rc = 0; + goto pceout; + } + + range = strtok_r(NULL, " ", ¶ms->cpos); + + if (range == NULL) { + fprintf(stderr, "missing target blocks for zero\n"); + goto pceout; + } + + tgt = parse_range(range); + + if (params->canwrite) { + fprintf(stderr, " erasing %d blocks\n", tgt->size); + + for (i = 0; i < tgt->count; ++i) { + // offset in bytes + blocks[0] = tgt->pos[i * 2] * (uint64_t) BLOCKSIZE; + // length in bytes + blocks[1] = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * (uint64_t) BLOCKSIZE; + + if (ioctl(params->fd, BLKDISCARD, &blocks) == -1) { + fprintf(stderr, "failed to blkdiscard (errno %d)\n", errno); + // Continue anyway, nothing we can do + } + } + } + + rc = 0; + +pceout: + if (tgt) { + free(tgt); + } + + return rc; +} + +// Definitions for transfer list command functions +typedef int (*CommandFunction)(CommandParameters*); + +typedef struct { + const char* name; + CommandFunction f; +} Command; + +// CompareCommands and CompareCommandNames are for the hash table + +static int CompareCommands(const void* c1, const void* c2) { + return strcmp(((const Command*) c1)->name, ((const Command*) c2)->name); +} + +static int CompareCommandNames(const void* c1, const void* c2) { + return strcmp(((const Command*) c1)->name, (const char*) c2); +} + +// HashString is used to hash command names for the hash table + +static unsigned int HashString(const char *s) { + unsigned int hash = 0; + if (s) { + while (*s) { + hash = hash * 33 + *s++; + } + } + return hash; +} + +// args: +// - block device (or file) to modify in-place +// - transfer list (blob) +// - new data stream (filename within package.zip) +// - patch stream (filename within package.zip, must be uncompressed) + +static Value* PerformBlockImageUpdate(const char* name, State* state, int argc, Expr* argv[], + const Command* commands, int cmdcount, int dryrun) { + + char* line = NULL; + char* linesave = NULL; + char* logcmd = NULL; + char* transfer_list = NULL; + CommandParameters params; + const Command* cmd = NULL; + const ZipEntry* new_entry = NULL; + const ZipEntry* patch_entry = NULL; + FILE* cmd_pipe = NULL; + HashTable* cmdht = NULL; + int i; + int res; + int rc = -1; + int stash_max_blocks = 0; + int total_blocks = 0; + pthread_attr_t attr; + unsigned int cmdhash; + UpdaterInfo* ui = NULL; + Value* blockdev_filename = NULL; + Value* new_data_fn = NULL; + Value* patch_data_fn = NULL; + Value* transfer_list_value = NULL; + ZipArchive* za = NULL; + + memset(¶ms, 0, sizeof(params)); + params.canwrite = !dryrun; + + fprintf(stderr, "performing %s\n", dryrun ? "verification" : "update"); + + if (ReadValueArgs(state, argv, 4, &blockdev_filename, &transfer_list_value, + &new_data_fn, &patch_data_fn) < 0) { + goto pbiudone; + } + + if (blockdev_filename->type != VAL_STRING) { + ErrorAbort(state, "blockdev_filename argument to %s must be string", name); + goto pbiudone; + } + if (transfer_list_value->type != VAL_BLOB) { + ErrorAbort(state, "transfer_list argument to %s must be blob", name); + goto pbiudone; + } + if (new_data_fn->type != VAL_STRING) { + ErrorAbort(state, "new_data_fn argument to %s must be string", name); + goto pbiudone; + } + if (patch_data_fn->type != VAL_STRING) { + ErrorAbort(state, "patch_data_fn argument to %s must be string", name); + goto pbiudone; + } + + ui = (UpdaterInfo*) state->cookie; + + if (ui == NULL) { + goto pbiudone; + } + + cmd_pipe = ui->cmd_pipe; + za = ui->package_zip; + + if (cmd_pipe == NULL || za == NULL) { + goto pbiudone; + } + + patch_entry = mzFindZipEntry(za, patch_data_fn->data); + + if (patch_entry == NULL) { + fprintf(stderr, "%s(): no file \"%s\" in package", name, patch_data_fn->data); + goto pbiudone; + } + + params.patch_start = ui->package_zip_addr + mzGetZipEntryOffset(patch_entry); + new_entry = mzFindZipEntry(za, new_data_fn->data); + + if (new_entry == NULL) { + fprintf(stderr, "%s(): no file \"%s\" in package", name, new_data_fn->data); + goto pbiudone; + } + + params.fd = TEMP_FAILURE_RETRY(open(blockdev_filename->data, O_RDWR)); + + if (params.fd == -1) { + fprintf(stderr, "failed to open %s: %s", blockdev_filename->data, strerror(errno)); + goto pbiudone; + } + + if (params.canwrite) { + params.nti.za = za; + params.nti.entry = new_entry; + + pthread_mutex_init(¶ms.nti.mu, NULL); + pthread_cond_init(¶ms.nti.cv, NULL); + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); + + if (pthread_create(¶ms.thread, &attr, unzip_new_data, ¶ms.nti) != 0) { + fprintf(stderr, "failed to create a thread (errno %d)\n", errno); + goto pbiudone; + } + } + + // The data in transfer_list_value is not necessarily null-terminated, so we need + // to copy it to a new buffer and add the null that strtok_r will need. + transfer_list = malloc(transfer_list_value->size + 1); + + if (transfer_list == NULL) { + fprintf(stderr, "failed to allocate %zd bytes for transfer list\n", + transfer_list_value->size + 1); + goto pbiudone; + } + + memcpy(transfer_list, transfer_list_value->data, transfer_list_value->size); + transfer_list[transfer_list_value->size] = '\0'; + + // First line in transfer list is the version number + line = strtok_r(transfer_list, "\n", &linesave); + params.version = strtol(line, NULL, 0); + + if (params.version < 1 || params.version > 3) { + fprintf(stderr, "unexpected transfer list version [%s]\n", line); + goto pbiudone; + } + + fprintf(stderr, "blockimg version is %d\n", params.version); + + // Second line in transfer list is the total number of blocks we expect to write + line = strtok_r(NULL, "\n", &linesave); + total_blocks = strtol(line, NULL, 0); + + if (total_blocks < 0) { + ErrorAbort(state, "unexpected block count [%s]\n", line); + goto pbiudone; + } else if (total_blocks == 0) { + rc = 0; + goto pbiudone; + } + + if (params.version >= 2) { + // Third line is how many stash entries are needed simultaneously + line = strtok_r(NULL, "\n", &linesave); + fprintf(stderr, "maximum stash entries %s\n", line); + + // Fourth line is the maximum number of blocks that will be stashed simultaneously + line = strtok_r(NULL, "\n", &linesave); + stash_max_blocks = strtol(line, NULL, 0); + + if (stash_max_blocks < 0) { + ErrorAbort(state, "unexpected maximum stash blocks [%s]\n", line); + goto pbiudone; + } + + if (stash_max_blocks > 0) { + res = CreateStash(state, stash_max_blocks, blockdev_filename->data, + ¶ms.stashbase); + + if (res == -1) { + goto pbiudone; + } + + params.createdstash = res; + } + } + + // Build a hash table of the available commands + cmdht = mzHashTableCreate(cmdcount, NULL); + + for (i = 0; i < cmdcount; ++i) { + cmdhash = HashString(commands[i].name); + mzHashTableLookup(cmdht, cmdhash, (void*) &commands[i], CompareCommands, true); + } + + // Subsequent lines are all individual transfer commands + for (line = strtok_r(NULL, "\n", &linesave); line; + line = strtok_r(NULL, "\n", &linesave)) { + + logcmd = strdup(line); + params.cmdname = strtok_r(line, " ", ¶ms.cpos); + + if (params.cmdname == NULL) { + fprintf(stderr, "missing command [%s]\n", line); + goto pbiudone; + } + + cmdhash = HashString(params.cmdname); + cmd = (const Command*) mzHashTableLookup(cmdht, cmdhash, params.cmdname, + CompareCommandNames, false); + + if (cmd == NULL) { + fprintf(stderr, "unexpected command [%s]\n", params.cmdname); + goto pbiudone; + } + + if (cmd->f != NULL && cmd->f(¶ms) == -1) { + fprintf(stderr, "failed to execute command [%s]\n", + logcmd ? logcmd : params.cmdname); + goto pbiudone; + } + + if (logcmd) { + free(logcmd); + logcmd = NULL; + } + + if (params.canwrite) { + fprintf(cmd_pipe, "set_progress %.4f\n", (double) params.written / total_blocks); + fflush(cmd_pipe); + } + } + + if (params.canwrite) { + pthread_join(params.thread, NULL); + + fprintf(stderr, "wrote %d blocks; expected %d\n", params.written, total_blocks); + fprintf(stderr, "max alloc needed was %zu\n", params.bufsize); + + // Delete stash only after successfully completing the update, as it + // may contain blocks needed to complete the update later. + DeleteStash(params.stashbase); + } else { + fprintf(stderr, "verified partition contents; update may be resumed\n"); + } + + rc = 0; + +pbiudone: + if (params.fd != -1) { + if (fsync(params.fd) == -1) { + fprintf(stderr, "failed to fsync device (errno %d)\n", errno); + } + TEMP_FAILURE_RETRY(close(params.fd)); + } + + if (logcmd) { + free(logcmd); + } + + if (cmdht) { + mzHashTableFree(cmdht); + } + + if (params.buffer) { + free(params.buffer); + } + + if (transfer_list) { + free(transfer_list); + } + + if (blockdev_filename) { + FreeValue(blockdev_filename); + } + + if (transfer_list_value) { + FreeValue(transfer_list_value); + } + + if (new_data_fn) { + FreeValue(new_data_fn); + } + + if (patch_data_fn) { + FreeValue(patch_data_fn); + } + + // Only delete the stash if the update cannot be resumed, or it's + // a verification run and we created the stash. + if (params.isunresumable || (!params.canwrite && params.createdstash)) { + DeleteStash(params.stashbase); + } + + if (params.stashbase) { + free(params.stashbase); + } + + return StringValue(rc == 0 ? strdup("t") : strdup("")); +} + +// The transfer list is a text file containing commands to +// transfer data from one place to another on the target +// partition. We parse it and execute the commands in order: +// +// zero [rangeset] +// - fill the indicated blocks with zeros +// +// new [rangeset] +// - fill the blocks with data read from the new_data file +// +// erase [rangeset] +// - mark the given blocks as empty +// +// move <...> +// bsdiff <...> +// imgdiff <...> +// - read the source blocks, apply a patch (or not in the +// case of move), write result to target blocks. bsdiff or +// imgdiff specifies the type of patch; move means no patch +// at all. +// +// The format of <...> differs between versions 1 and 2; +// see the LoadSrcTgtVersion{1,2}() functions for a +// description of what's expected. +// +// stash +// - (version 2+ only) load the given source range and stash +// the data in the given slot of the stash table. +// +// The creator of the transfer list will guarantee that no block +// is read (ie, used as the source for a patch or move) after it +// has been written. +// +// In version 2, the creator will guarantee that a given stash is +// loaded (with a stash command) before it's used in a +// move/bsdiff/imgdiff command. +// +// Within one command the source and target ranges may overlap so +// in general we need to read the entire source into memory before +// writing anything to the target blocks. +// +// All the patch data is concatenated into one patch_data file in +// the update package. It must be stored uncompressed because we +// memory-map it in directly from the archive. (Since patches are +// already compressed, we lose very little by not compressing +// their concatenation.) +// +// In version 3, commands that read data from the partition (i.e. +// move/bsdiff/imgdiff/stash) have one or more additional hashes +// before the range parameters, which are used to check if the +// command has already been completed and verify the integrity of +// the source data. + +Value* BlockImageVerifyFn(const char* name, State* state, int argc, Expr* argv[]) { + // Commands which are not tested are set to NULL to skip them completely + const Command commands[] = { + { "bsdiff", PerformCommandDiff }, + { "erase", NULL }, + { "free", PerformCommandFree }, + { "imgdiff", PerformCommandDiff }, + { "move", PerformCommandMove }, + { "new", NULL }, + { "stash", PerformCommandStash }, + { "zero", NULL } + }; + + // Perform a dry run without writing to test if an update can proceed + return PerformBlockImageUpdate(name, state, argc, argv, commands, + sizeof(commands) / sizeof(commands[0]), 1); +} + +Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[]) { + const Command commands[] = { + { "bsdiff", PerformCommandDiff }, + { "erase", PerformCommandErase }, + { "free", PerformCommandFree }, + { "imgdiff", PerformCommandDiff }, + { "move", PerformCommandMove }, + { "new", PerformCommandNew }, + { "stash", PerformCommandStash }, + { "zero", PerformCommandZero } + }; + + return PerformBlockImageUpdate(name, state, argc, argv, commands, + sizeof(commands) / sizeof(commands[0]), 0); +} + +Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) { + Value* blockdev_filename; + Value* ranges; + const uint8_t* digest = NULL; + if (ReadValueArgs(state, argv, 2, &blockdev_filename, &ranges) < 0) { + return NULL; + } + + if (blockdev_filename->type != VAL_STRING) { + ErrorAbort(state, "blockdev_filename argument to %s must be string", name); + goto done; + } + if (ranges->type != VAL_STRING) { + ErrorAbort(state, "ranges argument to %s must be string", name); + goto done; + } + + int fd = open(blockdev_filename->data, O_RDWR); if (fd < 0) { ErrorAbort(state, "failed to open %s: %s", blockdev_filename->data, strerror(errno)); goto done; @@ -784,9 +1887,19 @@ Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) { int i, j; for (i = 0; i < rs->count; ++i) { - check_lseek(fd, (off64_t)rs->pos[i*2] * BLOCKSIZE, SEEK_SET); + if (check_lseek(fd, (off64_t)rs->pos[i*2] * BLOCKSIZE, SEEK_SET) == -1) { + ErrorAbort(state, "failed to seek %s: %s", blockdev_filename->data, + strerror(errno)); + goto done; + } + for (j = rs->pos[i*2]; j < rs->pos[i*2+1]; ++j) { - readblock(fd, buffer, BLOCKSIZE); + if (read_all(fd, buffer, BLOCKSIZE) == -1) { + ErrorAbort(state, "failed to read %s: %s", blockdev_filename->data, + strerror(errno)); + goto done; + } + SHA_update(&ctx, buffer, BLOCKSIZE); } } @@ -804,6 +1917,7 @@ Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) { } void RegisterBlockImageFunctions() { + RegisterFunction("block_image_verify", BlockImageVerifyFn); RegisterFunction("block_image_update", BlockImageUpdateFn); RegisterFunction("range_sha1", RangeSha1Fn); } -- cgit v1.2.3 From 8a9014d57271beb8a23124b7464852f2cc193259 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Thu, 5 Feb 2015 14:52:09 -0800 Subject: There's no GPL code in 'updater'. This notice was added for libsyspatch and libxdelta3, but that code has been removed since. Change-Id: I4008878ded56ca1d5094a8208728f8c02fe1fe03 --- updater/MODULE_LICENSE_GPL | 0 updater/NOTICE | 339 --------------------------------------------- 2 files changed, 339 deletions(-) delete mode 100644 updater/MODULE_LICENSE_GPL delete mode 100644 updater/NOTICE diff --git a/updater/MODULE_LICENSE_GPL b/updater/MODULE_LICENSE_GPL deleted file mode 100644 index e69de29bb..000000000 diff --git a/updater/NOTICE b/updater/NOTICE deleted file mode 100644 index e77696ae8..000000000 --- a/updater/NOTICE +++ /dev/null @@ -1,339 +0,0 @@ - GNU GENERAL PUBLIC LICENSE - Version 2, June 1991 - - Copyright (C) 1989, 1991 Free Software Foundation, Inc. - 675 Mass Ave, Cambridge, MA 02139, USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - - Preamble - - The licenses for most software are designed to take away your -freedom to share and change it. By contrast, the GNU General Public -License is intended to guarantee your freedom to share and change free -software--to make sure the software is free for all its users. This -General Public License applies to most of the Free Software -Foundation's software and to any other program whose authors commit to -using it. (Some other Free Software Foundation software is covered by -the GNU Library General Public License instead.) You can apply it to -your programs, too. - - When we speak of free software, we are referring to freedom, not -price. Our General Public Licenses are designed to make sure that you -have the freedom to distribute copies of free software (and charge for -this service if you wish), that you receive source code or can get it -if you want it, that you can change the software or use pieces of it -in new free programs; and that you know you can do these things. - - To protect your rights, we need to make restrictions that forbid -anyone to deny you these rights or to ask you to surrender the rights. -These restrictions translate to certain responsibilities for you if you -distribute copies of the software, or if you modify it. - - For example, if you distribute copies of such a program, whether -gratis or for a fee, you must give the recipients all the rights that -you have. You must make sure that they, too, receive or can get the -source code. And you must show them these terms so they know their -rights. - - We protect your rights with two steps: (1) copyright the software, and -(2) offer you this license which gives you legal permission to copy, -distribute and/or modify the software. - - Also, for each author's protection and ours, we want to make certain -that everyone understands that there is no warranty for this free -software. If the software is modified by someone else and passed on, we -want its recipients to know that what they have is not the original, so -that any problems introduced by others will not reflect on the original -authors' reputations. - - Finally, any free program is threatened constantly by software -patents. We wish to avoid the danger that redistributors of a free -program will individually obtain patent licenses, in effect making the -program proprietary. To prevent this, we have made it clear that any -patent must be licensed for everyone's free use or not licensed at all. - - The precise terms and conditions for copying, distribution and -modification follow. - - GNU GENERAL PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. This License applies to any program or other work which contains -a notice placed by the copyright holder saying it may be distributed -under the terms of this General Public License. The "Program", below, -refers to any such program or work, and a "work based on the Program" -means either the Program or any derivative work under copyright law: -that is to say, a work containing the Program or a portion of it, -either verbatim or with modifications and/or translated into another -language. (Hereinafter, translation is included without limitation in -the term "modification".) Each licensee is addressed as "you". - -Activities other than copying, distribution and modification are not -covered by this License; they are outside its scope. The act of -running the Program is not restricted, and the output from the Program -is covered only if its contents constitute a work based on the -Program (independent of having been made by running the Program). -Whether that is true depends on what the Program does. - - 1. You may copy and distribute verbatim copies of the Program's -source code as you receive it, in any medium, provided that you -conspicuously and appropriately publish on each copy an appropriate -copyright notice and disclaimer of warranty; keep intact all the -notices that refer to this License and to the absence of any warranty; -and give any other recipients of the Program a copy of this License -along with the Program. - -You may charge a fee for the physical act of transferring a copy, and -you may at your option offer warranty protection in exchange for a fee. - - 2. You may modify your copy or copies of the Program or any portion -of it, thus forming a work based on the Program, and copy and -distribute such modifications or work under the terms of Section 1 -above, provided that you also meet all of these conditions: - - a) You must cause the modified files to carry prominent notices - stating that you changed the files and the date of any change. - - b) You must cause any work that you distribute or publish, that in - whole or in part contains or is derived from the Program or any - part thereof, to be licensed as a whole at no charge to all third - parties under the terms of this License. - - c) If the modified program normally reads commands interactively - when run, you must cause it, when started running for such - interactive use in the most ordinary way, to print or display an - announcement including an appropriate copyright notice and a - notice that there is no warranty (or else, saying that you provide - a warranty) and that users may redistribute the program under - these conditions, and telling the user how to view a copy of this - License. (Exception: if the Program itself is interactive but - does not normally print such an announcement, your work based on - the Program is not required to print an announcement.) - -These requirements apply to the modified work as a whole. If -identifiable sections of that work are not derived from the Program, -and can be reasonably considered independent and separate works in -themselves, then this License, and its terms, do not apply to those -sections when you distribute them as separate works. But when you -distribute the same sections as part of a whole which is a work based -on the Program, the distribution of the whole must be on the terms of -this License, whose permissions for other licensees extend to the -entire whole, and thus to each and every part regardless of who wrote it. - -Thus, it is not the intent of this section to claim rights or contest -your rights to work written entirely by you; rather, the intent is to -exercise the right to control the distribution of derivative or -collective works based on the Program. - -In addition, mere aggregation of another work not based on the Program -with the Program (or with a work based on the Program) on a volume of -a storage or distribution medium does not bring the other work under -the scope of this License. - - 3. You may copy and distribute the Program (or a work based on it, -under Section 2) in object code or executable form under the terms of -Sections 1 and 2 above provided that you also do one of the following: - - a) Accompany it with the complete corresponding machine-readable - source code, which must be distributed under the terms of Sections - 1 and 2 above on a medium customarily used for software interchange; or, - - b) Accompany it with a written offer, valid for at least three - years, to give any third party, for a charge no more than your - cost of physically performing source distribution, a complete - machine-readable copy of the corresponding source code, to be - distributed under the terms of Sections 1 and 2 above on a medium - customarily used for software interchange; or, - - c) Accompany it with the information you received as to the offer - to distribute corresponding source code. (This alternative is - allowed only for noncommercial distribution and only if you - received the program in object code or executable form with such - an offer, in accord with Subsection b above.) - -The source code for a work means the preferred form of the work for -making modifications to it. For an executable work, complete source -code means all the source code for all modules it contains, plus any -associated interface definition files, plus the scripts used to -control compilation and installation of the executable. However, as a -special exception, the source code distributed need not include -anything that is normally distributed (in either source or binary -form) with the major components (compiler, kernel, and so on) of the -operating system on which the executable runs, unless that component -itself accompanies the executable. - -If distribution of executable or object code is made by offering -access to copy from a designated place, then offering equivalent -access to copy the source code from the same place counts as -distribution of the source code, even though third parties are not -compelled to copy the source along with the object code. - - 4. You may not copy, modify, sublicense, or distribute the Program -except as expressly provided under this License. Any attempt -otherwise to copy, modify, sublicense or distribute the Program is -void, and will automatically terminate your rights under this License. -However, parties who have received copies, or rights, from you under -this License will not have their licenses terminated so long as such -parties remain in full compliance. - - 5. You are not required to accept this License, since you have not -signed it. However, nothing else grants you permission to modify or -distribute the Program or its derivative works. These actions are -prohibited by law if you do not accept this License. Therefore, by -modifying or distributing the Program (or any work based on the -Program), you indicate your acceptance of this License to do so, and -all its terms and conditions for copying, distributing or modifying -the Program or works based on it. - - 6. Each time you redistribute the Program (or any work based on the -Program), the recipient automatically receives a license from the -original licensor to copy, distribute or modify the Program subject to -these terms and conditions. You may not impose any further -restrictions on the recipients' exercise of the rights granted herein. -You are not responsible for enforcing compliance by third parties to -this License. - - 7. If, as a consequence of a court judgment or allegation of patent -infringement or for any other reason (not limited to patent issues), -conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot -distribute so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you -may not distribute the Program at all. For example, if a patent -license would not permit royalty-free redistribution of the Program by -all those who receive copies directly or indirectly through you, then -the only way you could satisfy both it and this License would be to -refrain entirely from distribution of the Program. - -If any portion of this section is held invalid or unenforceable under -any particular circumstance, the balance of the section is intended to -apply and the section as a whole is intended to apply in other -circumstances. - -It is not the purpose of this section to induce you to infringe any -patents or other property right claims or to contest validity of any -such claims; this section has the sole purpose of protecting the -integrity of the free software distribution system, which is -implemented by public license practices. Many people have made -generous contributions to the wide range of software distributed -through that system in reliance on consistent application of that -system; it is up to the author/donor to decide if he or she is willing -to distribute software through any other system and a licensee cannot -impose that choice. - -This section is intended to make thoroughly clear what is believed to -be a consequence of the rest of this License. - - 8. If the distribution and/or use of the Program is restricted in -certain countries either by patents or by copyrighted interfaces, the -original copyright holder who places the Program under this License -may add an explicit geographical distribution limitation excluding -those countries, so that distribution is permitted only in or among -countries not thus excluded. In such case, this License incorporates -the limitation as if written in the body of this License. - - 9. The Free Software Foundation may publish revised and/or new versions -of the General Public License from time to time. Such new versions will -be similar in spirit to the present version, but may differ in detail to -address new problems or concerns. - -Each version is given a distinguishing version number. If the Program -specifies a version number of this License which applies to it and "any -later version", you have the option of following the terms and conditions -either of that version or of any later version published by the Free -Software Foundation. If the Program does not specify a version number of -this License, you may choose any version ever published by the Free Software -Foundation. - - 10. If you wish to incorporate parts of the Program into other free -programs whose distribution conditions are different, write to the author -to ask for permission. For software which is copyrighted by the Free -Software Foundation, write to the Free Software Foundation; we sometimes -make exceptions for this. Our decision will be guided by the two goals -of preserving the free status of all derivatives of our free software and -of promoting the sharing and reuse of software generally. - - NO WARRANTY - - 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY -FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN -OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES -PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED -OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF -MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS -TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE -PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, -REPAIR OR CORRECTION. - - 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING -WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR -REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, -INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING -OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED -TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY -YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER -PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE -POSSIBILITY OF SUCH DAMAGES. - - END OF TERMS AND CONDITIONS - - How to Apply These Terms to Your New Programs - - If you develop a new program, and you want it to be of the greatest -possible use to the public, the best way to achieve this is to make it -free software which everyone can redistribute and change under these terms. - - To do so, attach the following notices to the program. It is safest -to attach them to the start of each source file to most effectively -convey the exclusion of warranty; and each file should have at least -the "copyright" line and a pointer to where the full notice is found. - - - Copyright (C) 19yy - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - -Also add information on how to contact you by electronic and paper mail. - -If the program is interactive, make it output a short notice like this -when it starts in an interactive mode: - - Gnomovision version 69, Copyright (C) 19yy name of author - Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. - This is free software, and you are welcome to redistribute it - under certain conditions; type `show c' for details. - -The hypothetical commands `show w' and `show c' should show the appropriate -parts of the General Public License. Of course, the commands you use may -be called something other than `show w' and `show c'; they could even be -mouse-clicks or menu items--whatever suits your program. - -You should also get your employer (if you work as a programmer) or your -school, if any, to sign a "copyright disclaimer" for the program, if -necessary. Here is a sample; alter the names: - - Yoyodyne, Inc., hereby disclaims all copyright interest in the program - `Gnomovision' (which makes passes at compilers) written by James Hacker. - - , 1 April 1989 - Ty Coon, President of Vice - -This General Public License does not permit incorporating your program into -proprietary programs. If your program is a subroutine library, you may -consider it more useful to permit linking proprietary applications with the -library. If this is what you want to do, use the GNU Library General -Public License instead of this License. -- cgit v1.2.3 From 4e8e93b6661f4de86c295b76fff82b8815266780 Mon Sep 17 00:00:00 2001 From: Nanik Tolaram Date: Sun, 8 Feb 2015 22:31:14 +1100 Subject: Remove dead/unused code and realign some of the comments to make it more cleaner and easier to read Change-Id: If536d482c0ed645368084e76d8ec060f05d89137 Signed-off-by: Nanik Tolaram --- minzip/DirUtil.c | 4 ++-- minzip/Hash.c | 11 ----------- minzip/Zip.c | 16 ++++++---------- 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/minzip/DirUtil.c b/minzip/DirUtil.c index fe2c880ac..97cb2e0ee 100644 --- a/minzip/DirUtil.c +++ b/minzip/DirUtil.c @@ -85,7 +85,7 @@ dirCreateHierarchy(const char *path, int mode, c--; } if (c == cpath) { -//xxx test this path + //xxx test this path /* No directory component. Act like the path was empty. */ errno = ENOENT; @@ -206,7 +206,7 @@ dirUnlinkHierarchy(const char *path) /* recurse over components */ errno = 0; while ((de = readdir(dir)) != NULL) { -//TODO: don't blow the stack + //TODO: don't blow the stack char dn[PATH_MAX]; if (!strcmp(de->d_name, "..") || !strcmp(de->d_name, ".")) { continue; diff --git a/minzip/Hash.c b/minzip/Hash.c index 8c6ca9bc2..8f8ed68e5 100644 --- a/minzip/Hash.c +++ b/minzip/Hash.c @@ -140,7 +140,6 @@ static bool resizeHash(HashTable* pHashTable, int newSize) int i; assert(countTombStones(pHashTable) == pHashTable->numDeadEntries); - //LOGI("before: dead=%d\n", pHashTable->numDeadEntries); pNewEntries = (HashEntry*) calloc(newSize, sizeof(HashTable)); if (pNewEntries == NULL) @@ -196,7 +195,6 @@ void* mzHashTableLookup(HashTable* pHashTable, unsigned int itemHash, void* item (*cmpFunc)(pEntry->data, item) == 0) { /* match */ - //LOGD("+++ match on entry %d\n", pEntry - pHashTable->pEntries); break; } @@ -206,8 +204,6 @@ void* mzHashTableLookup(HashTable* pHashTable, unsigned int itemHash, void* item break; /* edge case - single-entry table */ pEntry = pHashTable->pEntries; } - - //LOGI("+++ look probing %d...\n", pEntry - pHashTable->pEntries); } if (pEntry->data == NULL) { @@ -228,10 +224,6 @@ void* mzHashTableLookup(HashTable* pHashTable, unsigned int itemHash, void* item abort(); } /* note "pEntry" is now invalid */ - } else { - //LOGW("okay %d/%d/%d\n", - // pHashTable->numEntries, pHashTable->tableSize, - // (pHashTable->tableSize * LOAD_NUMER) / LOAD_DENOM); } /* full table is bad -- search for nonexistent never halts */ @@ -264,7 +256,6 @@ bool mzHashTableRemove(HashTable* pHashTable, unsigned int itemHash, void* item) pEnd = &pHashTable->pEntries[pHashTable->tableSize]; while (pEntry->data != NULL) { if (pEntry->data == item) { - //LOGI("+++ stepping on entry %d\n", pEntry - pHashTable->pEntries); pEntry->data = HASH_TOMBSTONE; pHashTable->numEntries--; pHashTable->numDeadEntries++; @@ -277,8 +268,6 @@ bool mzHashTableRemove(HashTable* pHashTable, unsigned int itemHash, void* item) break; /* edge case - single-entry table */ pEntry = pHashTable->pEntries; } - - //LOGI("+++ del probing %d...\n", pEntry - pHashTable->pEntries); } return false; diff --git a/minzip/Zip.c b/minzip/Zip.c index 5070104d3..aec35e375 100644 --- a/minzip/Zip.c +++ b/minzip/Zip.c @@ -327,10 +327,6 @@ static bool parseZipArchive(ZipArchive* pArchive) #else pEntry = &pArchive->pEntries[i]; #endif - - //LOGI("%d: localHdr=%d fnl=%d el=%d cl=%d\n", - // i, localHdrOffset, fileNameLen, extraLen, commentLen); - pEntry->fileNameLen = fileNameLen; pEntry->fileName = fileName; @@ -923,8 +919,8 @@ bool mzExtractRecursive(const ZipArchive *pArchive, /* Walk through the entries and extract anything whose path begins * with zpath. -//TODO: since the entries are sorted, binary search for the first match -// and stop after the first non-match. + //TODO: since the entries are sorted, binary search for the first match + // and stop after the first non-match. */ unsigned int i; bool seenMatch = false; @@ -933,10 +929,10 @@ bool mzExtractRecursive(const ZipArchive *pArchive, for (i = 0; i < pArchive->numEntries; i++) { ZipEntry *pEntry = pArchive->pEntries + i; if (pEntry->fileNameLen < zipDirLen) { -//TODO: look out for a single empty directory entry that matches zpath, but -// missing the trailing slash. Most zip files seem to include -// the trailing slash, but I think it's legal to leave it off. -// e.g., zpath "a/b/", entry "a/b", with no children of the entry. + //TODO: look out for a single empty directory entry that matches zpath, but + // missing the trailing slash. Most zip files seem to include + // the trailing slash, but I think it's legal to leave it off. + // e.g., zpath "a/b/", entry "a/b", with no children of the entry. /* No chance of matching. */ #if SORT_ENTRIES -- cgit v1.2.3 From f14af80a1418acdc0ae6fea3da0285a357d57182 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 10 Feb 2015 14:46:14 -0800 Subject: recovery: Properly detect userdebug or eng builds The recovery system behaves a little bit differently on userdebug or eng builds by presenting error reports to the user in the ui. This is controlled by checking the build fingerprint for the string :userdebug/ or :eng/. But with AOSP version numbers most AOSP builds blows the 92 char limit of ro.build.fingerprint and therefore the property is not set, so this condition will always be evaluated to false, for most builds. Instead of depending on the flaky ro.build.fingerprint this change uses ro.debuggable. Change-Id: I74bc00c655ac596aaf4b488ecea58f0a8de9c26b --- adb_install.cpp | 4 +--- common.h | 3 +++ recovery.cpp | 9 ++++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/adb_install.cpp b/adb_install.cpp index be3b9a063..e3289608f 100644 --- a/adb_install.cpp +++ b/adb_install.cpp @@ -61,9 +61,7 @@ stop_adbd() { static void maybe_restart_adbd() { - char value[PROPERTY_VALUE_MAX+1]; - int len = property_get("ro.debuggable", value, NULL); - if (len == 1 && value[0] == '1') { + if (is_ro_debuggable()) { ui->Print("Restarting adbd...\n"); set_usb_driver(true); property_set("ctl.start", "adbd"); diff --git a/common.h b/common.h index 768f499f9..4f1c099df 100644 --- a/common.h +++ b/common.h @@ -17,6 +17,7 @@ #ifndef RECOVERY_COMMON_H #define RECOVERY_COMMON_H +#include #include #include @@ -46,6 +47,8 @@ FILE* fopen_path(const char *path, const char *mode); void ui_print(const char* format, ...); +bool is_ro_debuggable(); + #ifdef __cplusplus } #endif diff --git a/recovery.cpp b/recovery.cpp index 7f17b16ef..d8756d7ce 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -161,6 +161,11 @@ fopen_path(const char *path, const char *mode) { return fp; } +bool is_ro_debuggable() { + char value[PROPERTY_VALUE_MAX+1]; + return (property_get("ro.debuggable", value, NULL) == 1 && value[0] == '1'); +} + // close a file, log an error if the error indicator is set static void check_and_fclose(FILE *fp, const char *name) { @@ -954,9 +959,7 @@ main(int argc, char **argv) { // If this is an eng or userdebug build, then automatically // turn the text display on if the script fails so the error // message is visible. - char buffer[PROPERTY_VALUE_MAX+1]; - property_get("ro.build.fingerprint", buffer, ""); - if (strstr(buffer, ":userdebug/") || strstr(buffer, ":eng/")) { + if (is_ro_debuggable()) { ui->ShowText(true); } } -- cgit v1.2.3 From 9905f3a4ee90e4f569bb187e6fa15616cc54bbb7 Mon Sep 17 00:00:00 2001 From: Ajay Dudani Date: Wed, 4 Feb 2015 16:49:44 -0800 Subject: recovery: Generate libminui dynamic library Allow factory test images to use minui functionaltiy by making use of libminui dynamic library. Change-Id: I63e77420d5addbcc6eebeedc213f629085766b4c --- minui/Android.mk | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/minui/Android.mk b/minui/Android.mk index df4aac169..aee2a34ab 100644 --- a/minui/Android.mk +++ b/minui/Android.mk @@ -30,3 +30,10 @@ else endif include $(BUILD_STATIC_LIBRARY) + +# Used by OEMs for factory test images. +include $(CLEAR_VARS) +LOCAL_MODULE := libminui +LOCAL_WHOLE_STATIC_LIBRARIES += libminui +LOCAL_SHARED_LIBRARIES := libpng +include $(BUILD_SHARED_LIBRARY) -- cgit v1.2.3 From 07d9627d7cf31e03b778bc4b1c918d552680cd28 Mon Sep 17 00:00:00 2001 From: Ajay Dudani Date: Wed, 4 Feb 2015 16:51:34 -0800 Subject: recovery: Handle EV_SW events Change-Id: I29c13a941067cd1bbdced3bd67fd93293347d7b6 --- minui/events.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/minui/events.c b/minui/events.c index d98a774ef..9e4255dd7 100644 --- a/minui/events.c +++ b/minui/events.c @@ -80,8 +80,8 @@ int ev_init(ev_callback input_cb, void *data) } /* TODO: add ability to specify event masks. For now, just assume - * that only EV_KEY and EV_REL event types are ever needed. */ - if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits)) { + * that only EV_KEY, EV_REL & EV_SW event types are ever needed. */ + if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) { close(fd); continue; } -- cgit v1.2.3 From fd778e3e406a7e83536ea66776996f032f24af64 Mon Sep 17 00:00:00 2001 From: Tony Kuo Date: Thu, 5 Feb 2015 21:25:56 +0800 Subject: Fix Droid and animation color in recovery mode [Problem] Droid and animation color in recovery mode are incorrect [Modify] - Add support for flipping (zero copy) with RECOVERY_ABGR. - Decodes PNG files to BGRA directly, and other fills, text and alpha blending are also done directly in BGRA (i.e. blits can still bypass conversion) - Remove the BGRA workaround added previous for single buffer mode (f766396) Bug:19216535 Change-Id: Ie864419fc6da776ff58b2d02e130f203c194500f Signed-off-by: Tony Kuo --- minui/Android.mk | 3 +++ minui/graphics.c | 7 +++++++ minui/graphics_adf.c | 4 +++- minui/graphics_fbdev.c | 13 ------------- minui/resources.c | 12 ++++++++++++ 5 files changed, 25 insertions(+), 14 deletions(-) diff --git a/minui/Android.mk b/minui/Android.mk index aee2a34ab..ddee165f9 100644 --- a/minui/Android.mk +++ b/minui/Android.mk @@ -16,6 +16,9 @@ LOCAL_MODULE := libminui # ordinary characters in this context). Strip double-quotes from the # value so that either will work. +ifeq ($(subst ",,$(TARGET_RECOVERY_PIXEL_FORMAT)),ABGR_8888) + LOCAL_CFLAGS += -DRECOVERY_ABGR +endif ifeq ($(subst ",,$(TARGET_RECOVERY_PIXEL_FORMAT)),RGBX_8888) LOCAL_CFLAGS += -DRECOVERY_RGBX endif diff --git a/minui/graphics.c b/minui/graphics.c index ec39433b8..870ffa089 100644 --- a/minui/graphics.c +++ b/minui/graphics.c @@ -161,10 +161,17 @@ void gr_texticon(int x, int y, GRSurface* icon) { void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) { +#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) + gr_current_r = b; + gr_current_g = g; + gr_current_b = r; + gr_current_a = a; +#else gr_current_r = r; gr_current_g = g; gr_current_b = b; gr_current_a = a; +#endif } void gr_clear() diff --git a/minui/graphics_adf.c b/minui/graphics_adf.c index 289c3be63..c023d4db9 100644 --- a/minui/graphics_adf.c +++ b/minui/graphics_adf.c @@ -142,7 +142,9 @@ static gr_surface adf_init(minui_backend *backend) ssize_t n_dev_ids, i; gr_surface ret; -#if defined(RECOVERY_BGRA) +#if defined(RECOVERY_ABGR) + pdata->format = DRM_FORMAT_ABGR8888; +#elif defined(RECOVERY_BGRA) pdata->format = DRM_FORMAT_BGRA8888; #elif defined(RECOVERY_RGBX) pdata->format = DRM_FORMAT_RGBX8888; diff --git a/minui/graphics_fbdev.c b/minui/graphics_fbdev.c index a087899bd..ecd40c3eb 100644 --- a/minui/graphics_fbdev.c +++ b/minui/graphics_fbdev.c @@ -187,21 +187,8 @@ static gr_surface fbdev_flip(minui_backend* backend __unused) { set_displayed_framebuffer(1-displayed_buffer); } else { // Copy from the in-memory surface to the framebuffer. - -#if defined(RECOVERY_BGRA) - unsigned int idx; - unsigned char* ucfb_vaddr = (unsigned char*)gr_framebuffer[0].data; - unsigned char* ucbuffer_vaddr = (unsigned char*)gr_draw->data; - for (idx = 0 ; idx < (gr_draw->height * gr_draw->row_bytes); idx += 4) { - ucfb_vaddr[idx ] = ucbuffer_vaddr[idx + 2]; - ucfb_vaddr[idx + 1] = ucbuffer_vaddr[idx + 1]; - ucfb_vaddr[idx + 2] = ucbuffer_vaddr[idx ]; - ucfb_vaddr[idx + 3] = ucbuffer_vaddr[idx + 3]; - } -#else memcpy(gr_framebuffer[0].data, gr_draw->data, gr_draw->height * gr_draw->row_bytes); -#endif } return gr_draw; } diff --git a/minui/resources.c b/minui/resources.c index f645c4b67..886c3255d 100644 --- a/minui/resources.c +++ b/minui/resources.c @@ -216,6 +216,10 @@ int res_create_display_surface(const char* name, gr_surface* pSurface) { goto exit; } +#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) + png_set_bgr(png_ptr); +#endif + unsigned char* p_row = malloc(width * 4); unsigned int y; for (y = 0; y < height; ++y) { @@ -279,6 +283,10 @@ int res_create_multi_display_surface(const char* name, int* frames, gr_surface** } } +#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) + png_set_bgr(png_ptr); +#endif + unsigned char* p_row = malloc(width * 4); unsigned int y; for (y = 0; y < height; ++y) { @@ -334,6 +342,10 @@ int res_create_alpha_surface(const char* name, gr_surface* pSurface) { surface->row_bytes = width; surface->pixel_bytes = 1; +#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) + png_set_bgr(png_ptr); +#endif + unsigned char* p_row; unsigned int y; for (y = 0; y < height; ++y) { -- cgit v1.2.3 From 1df64d3278378d0d234be11cfeafec5e6a96bd2d Mon Sep 17 00:00:00 2001 From: Jesse Zhao Date: Tue, 17 Feb 2015 17:09:23 -0800 Subject: Initialize stashbase even stash_max_blocks = 0 Change-Id: I480c02ffedd811f4dda9940ef979a05ff54f1435 Bug: 19410117 --- updater/blockimg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/updater/blockimg.c b/updater/blockimg.c index a0f81e935..9c39634fc 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -1643,7 +1643,7 @@ static Value* PerformBlockImageUpdate(const char* name, State* state, int argc, goto pbiudone; } - if (stash_max_blocks > 0) { + if (stash_max_blocks >= 0) { res = CreateStash(state, stash_max_blocks, blockdev_filename->data, ¶ms.stashbase); -- cgit v1.2.3 From d808d2194f7a71309545bfcf111e874fb97bfd6b Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Wed, 18 Feb 2015 10:21:54 -0800 Subject: Add extern "C" to all the headers. Change-Id: Idc249ff1b199b7c455f90092ff2c8a48b539faf4 --- minadbd/adb.h | 8 ++++++++ minadbd/fdevent.h | 7 +++++++ minadbd/fuse_adb_provider.h | 8 ++++++++ minadbd/sysdeps.h | 12 ++++++++++++ minadbd/transport.h | 9 +++++++++ minadbd/utils.h | 8 ++++++++ 6 files changed, 52 insertions(+) diff --git a/minadbd/adb.h b/minadbd/adb.h index 714868f5c..010a36485 100644 --- a/minadbd/adb.h +++ b/minadbd/adb.h @@ -22,6 +22,10 @@ #include "transport.h" /* readx(), writex() */ #include "fdevent.h" +#ifdef __cplusplus +extern "C" { +#endif + #define MAX_PAYLOAD 4096 #define A_SYNC 0x434e5953 @@ -421,4 +425,8 @@ extern int SHELL_EXIT_NOTIFY_FD; int sendfailmsg(int fd, const char *reason); int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s); +#ifdef __cplusplus +} +#endif + #endif diff --git a/minadbd/fdevent.h b/minadbd/fdevent.h index a0ebe2a7e..38b08cce1 100644 --- a/minadbd/fdevent.h +++ b/minadbd/fdevent.h @@ -19,6 +19,10 @@ #include /* for int64_t */ +#ifdef __cplusplus +extern "C" { +#endif + /* events that may be observed */ #define FDE_READ 0x0001 #define FDE_WRITE 0x0002 @@ -79,5 +83,8 @@ struct fdevent void *arg; }; +#ifdef __cplusplus +} +#endif #endif diff --git a/minadbd/fuse_adb_provider.h b/minadbd/fuse_adb_provider.h index 0eb1f79d1..23de44ab2 100644 --- a/minadbd/fuse_adb_provider.h +++ b/minadbd/fuse_adb_provider.h @@ -17,6 +17,14 @@ #ifndef __FUSE_ADB_PROVIDER_H #define __FUSE_ADB_PROVIDER_H +#ifdef __cplusplus +extern "C" { +#endif + int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size); +#ifdef __cplusplus +} +#endif + #endif diff --git a/minadbd/sysdeps.h b/minadbd/sysdeps.h index 800ddb753..3edaef472 100644 --- a/minadbd/sysdeps.h +++ b/minadbd/sysdeps.h @@ -36,6 +36,10 @@ #include #include +#ifdef __cplusplus +extern "C" { +#endif + #define OS_PATH_SEPARATOR '\\' #define OS_PATH_SEPARATOR_STR "\\" @@ -254,6 +258,10 @@ static __inline__ int adb_is_absolute_host_path( const char* path ) return isalpha(path[0]) && path[1] == ':' && path[2] == '\\'; } +#ifdef __cplusplus +} +#endif + #else /* !_WIN32 a.k.a. Unix */ #include "fdevent.h" @@ -491,4 +499,8 @@ static __inline__ int adb_is_absolute_host_path( const char* path ) #endif /* !_WIN32 */ +#ifdef __cplusplus +} +#endif + #endif /* _ADB_SYSDEPS_H */ diff --git a/minadbd/transport.h b/minadbd/transport.h index 992e05285..c1b8ff34f 100644 --- a/minadbd/transport.h +++ b/minadbd/transport.h @@ -17,10 +17,19 @@ #ifndef __TRANSPORT_H #define __TRANSPORT_H +#ifdef __cplusplus +extern "C" { +#endif + /* convenience wrappers around read/write that will retry on ** EINTR and/or short read/write. Returns 0 on success, -1 ** on error or EOF. */ int readx(int fd, void *ptr, size_t len); int writex(int fd, const void *ptr, size_t len); + +#ifdef __cplusplus +} +#endif + #endif /* __TRANSPORT_H */ diff --git a/minadbd/utils.h b/minadbd/utils.h index f70ecd24d..e833820ab 100644 --- a/minadbd/utils.h +++ b/minadbd/utils.h @@ -16,6 +16,10 @@ #ifndef _ADB_UTILS_H #define _ADB_UTILS_H +#ifdef __cplusplus +extern "C" { +#endif + /* bounded buffer functions */ /* all these functions are used to append data to a bounded buffer. @@ -65,4 +69,8 @@ char* buff_add (char* buff, char* buffEnd, const char* format, ... ); #define BUFF_DECL(_buff,_cursor,_end,_size) \ char _buff[_size], *_cursor=_buff, *_end = _cursor + (_size) +#ifdef __cplusplus +} +#endif + #endif /* _ADB_UTILS_H */ -- cgit v1.2.3 From 8f1bfead3f77f16ac3e85ef1d5e0008e52848e79 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Tue, 25 Nov 2014 10:55:50 -0800 Subject: Lose USB transport code to libadb. Bug: 17626262 Change-Id: If41031ba20a3a75fa510f155c654a482b47e409d --- Android.mk | 2 +- minadbd/Android.mk | 3 - minadbd/adb.h | 3 +- minadbd/transport.c | 803 --------------------------------------------- minadbd/transport_usb.c | 121 ------- minadbd/usb_linux_client.c | 541 ------------------------------ 6 files changed, 3 insertions(+), 1470 deletions(-) delete mode 100644 minadbd/transport.c delete mode 100644 minadbd/transport_usb.c delete mode 100644 minadbd/usb_linux_client.c diff --git a/Android.mk b/Android.mk index e360c6b22..0cb0836cd 100644 --- a/Android.mk +++ b/Android.mk @@ -64,7 +64,7 @@ LOCAL_STATIC_LIBRARIES := \ libmtdutils \ libmincrypt \ libminadbd \ - libadb \ + libadbd \ libfusesideload \ libminui \ libpng \ diff --git a/minadbd/Android.mk b/minadbd/Android.mk index c07715706..8de1b7453 100644 --- a/minadbd/Android.mk +++ b/minadbd/Android.mk @@ -13,11 +13,8 @@ include $(CLEAR_VARS) LOCAL_SRC_FILES := \ adb.c \ fuse_adb_provider.c \ - transport.c \ - transport_usb.c \ sockets.c \ services.c \ - usb_linux_client.c \ utils.c LOCAL_CFLAGS := -O2 -g -DADB_HOST=0 -Wall -Wno-unused-parameter diff --git a/minadbd/adb.h b/minadbd/adb.h index 010a36485..58818a996 100644 --- a/minadbd/adb.h +++ b/minadbd/adb.h @@ -257,7 +257,8 @@ void close_usb_devices(); void unregister_transport(atransport *t); void unregister_all_tcp_transports(); -void register_usb_transport(usb_handle *h, const char *serial, unsigned writeable); +void register_usb_transport(usb_handle *h, const char *serial, + const char* dev_path, unsigned writeable); /* this should only be used for transports with connection_state == CS_NOPERM */ void unregister_usb_transport(usb_handle *usb); diff --git a/minadbd/transport.c b/minadbd/transport.c deleted file mode 100644 index 92679f518..000000000 --- a/minadbd/transport.c +++ /dev/null @@ -1,803 +0,0 @@ -/* - * Copyright (C) 2007 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 -#include -#include -#include -#include - -#include "sysdeps.h" - -#define TRACE_TAG TRACE_TRANSPORT -#include "adb.h" - -static void transport_unref(atransport *t); - -static atransport transport_list = { - .next = &transport_list, - .prev = &transport_list, -}; - -ADB_MUTEX_DEFINE( transport_lock ); - -#if ADB_TRACE -#define MAX_DUMP_HEX_LEN 16 -static void dump_hex( const unsigned char* ptr, size_t len ) -{ - int nn, len2 = len; - // Build a string instead of logging each character. - // MAX chars in 2 digit hex, one space, MAX chars, one '\0'. - char buffer[MAX_DUMP_HEX_LEN *2 + 1 + MAX_DUMP_HEX_LEN + 1 ], *pb = buffer; - - if (len2 > MAX_DUMP_HEX_LEN) len2 = MAX_DUMP_HEX_LEN; - - for (nn = 0; nn < len2; nn++) { - sprintf(pb, "%02x", ptr[nn]); - pb += 2; - } - sprintf(pb++, " "); - - for (nn = 0; nn < len2; nn++) { - int c = ptr[nn]; - if (c < 32 || c > 127) - c = '.'; - *pb++ = c; - } - *pb++ = '\0'; - DR("%s\n", buffer); -} -#endif - -void -kick_transport(atransport* t) -{ - if (t && !t->kicked) - { - int kicked; - - adb_mutex_lock(&transport_lock); - kicked = t->kicked; - if (!kicked) - t->kicked = 1; - adb_mutex_unlock(&transport_lock); - - if (!kicked) - t->kick(t); - } -} - -void -run_transport_disconnects(atransport* t) -{ - adisconnect* dis = t->disconnects.next; - - D("%s: run_transport_disconnects\n", t->serial); - while (dis != &t->disconnects) { - adisconnect* next = dis->next; - dis->func( dis->opaque, t ); - dis = next; - } -} - -#if ADB_TRACE -static void -dump_packet(const char* name, const char* func, apacket* p) -{ - unsigned command = p->msg.command; - int len = p->msg.data_length; - char cmd[9]; - char arg0[12], arg1[12]; - int n; - - for (n = 0; n < 4; n++) { - int b = (command >> (n*8)) & 255; - if (b < 32 || b >= 127) - break; - cmd[n] = (char)b; - } - if (n == 4) { - cmd[4] = 0; - } else { - /* There is some non-ASCII name in the command, so dump - * the hexadecimal value instead */ - snprintf(cmd, sizeof cmd, "%08x", command); - } - - if (p->msg.arg0 < 256U) - snprintf(arg0, sizeof arg0, "%d", p->msg.arg0); - else - snprintf(arg0, sizeof arg0, "0x%x", p->msg.arg0); - - if (p->msg.arg1 < 256U) - snprintf(arg1, sizeof arg1, "%d", p->msg.arg1); - else - snprintf(arg1, sizeof arg1, "0x%x", p->msg.arg1); - - D("%s: %s: [%s] arg0=%s arg1=%s (len=%d) ", - name, func, cmd, arg0, arg1, len); - dump_hex(p->data, len); -} -#endif /* ADB_TRACE */ - -static int -read_packet(int fd, const char* name, apacket** ppacket) -{ - char *p = (char*)ppacket; /* really read a packet address */ - int r; - int len = sizeof(*ppacket); - char buff[8]; - if (!name) { - snprintf(buff, sizeof buff, "fd=%d", fd); - name = buff; - } - while(len > 0) { - r = adb_read(fd, p, len); - if(r > 0) { - len -= r; - p += r; - } else { - D("%s: read_packet (fd=%d), error ret=%d errno=%d: %s\n", name, fd, r, errno, strerror(errno)); - if((r < 0) && (errno == EINTR)) continue; - return -1; - } - } - -#if ADB_TRACE - if (ADB_TRACING) { - dump_packet(name, "from remote", *ppacket); - } -#endif - return 0; -} - -static int -write_packet(int fd, const char* name, apacket** ppacket) -{ - char *p = (char*) ppacket; /* we really write the packet address */ - int r, len = sizeof(ppacket); - char buff[8]; - if (!name) { - snprintf(buff, sizeof buff, "fd=%d", fd); - name = buff; - } - -#if ADB_TRACE - if (ADB_TRACING) { - dump_packet(name, "to remote", *ppacket); - } -#endif - len = sizeof(ppacket); - while(len > 0) { - r = adb_write(fd, p, len); - if(r > 0) { - len -= r; - p += r; - } else { - D("%s: write_packet (fd=%d) error ret=%d errno=%d: %s\n", name, fd, r, errno, strerror(errno)); - if((r < 0) && (errno == EINTR)) continue; - return -1; - } - } - return 0; -} - -static void transport_socket_events(int fd, unsigned events, void *_t) -{ - atransport *t = _t; - D("transport_socket_events(fd=%d, events=%04x,...)\n", fd, events); - if(events & FDE_READ){ - apacket *p = 0; - if(read_packet(fd, t->serial, &p)){ - D("%s: failed to read packet from transport socket on fd %d\n", t->serial, fd); - } else { - handle_packet(p, (atransport *) _t); - } - } -} - -void send_packet(apacket *p, atransport *t) -{ - unsigned char *x; - unsigned sum; - unsigned count; - - p->msg.magic = p->msg.command ^ 0xffffffff; - - count = p->msg.data_length; - x = (unsigned char *) p->data; - sum = 0; - while(count-- > 0){ - sum += *x++; - } - p->msg.data_check = sum; - - print_packet("send", p); - - if (t == NULL) { - D("Transport is null \n"); - // Zap errno because print_packet() and other stuff have errno effect. - errno = 0; - fatal_errno("Transport is null"); - } - - if(write_packet(t->transport_socket, t->serial, &p)){ - fatal_errno("cannot enqueue packet on transport socket"); - } -} - -/* The transport is opened by transport_register_func before -** the input and output threads are started. -** -** The output thread issues a SYNC(1, token) message to let -** the input thread know to start things up. In the event -** of transport IO failure, the output thread will post a -** SYNC(0,0) message to ensure shutdown. -** -** The transport will not actually be closed until both -** threads exit, but the input thread will kick the transport -** on its way out to disconnect the underlying device. -*/ - -static void *output_thread(void *_t) -{ - atransport *t = _t; - apacket *p; - - D("%s: starting transport output thread on fd %d, SYNC online (%d)\n", - t->serial, t->fd, t->sync_token + 1); - p = get_apacket(); - p->msg.command = A_SYNC; - p->msg.arg0 = 1; - p->msg.arg1 = ++(t->sync_token); - p->msg.magic = A_SYNC ^ 0xffffffff; - if(write_packet(t->fd, t->serial, &p)) { - put_apacket(p); - D("%s: failed to write SYNC packet\n", t->serial); - goto oops; - } - - D("%s: data pump started\n", t->serial); - for(;;) { - p = get_apacket(); - - if(t->read_from_remote(p, t) == 0){ - D("%s: received remote packet, sending to transport\n", - t->serial); - if(write_packet(t->fd, t->serial, &p)){ - put_apacket(p); - D("%s: failed to write apacket to transport\n", t->serial); - goto oops; - } - } else { - D("%s: remote read failed for transport\n", t->serial); - put_apacket(p); - break; - } - } - - D("%s: SYNC offline for transport\n", t->serial); - p = get_apacket(); - p->msg.command = A_SYNC; - p->msg.arg0 = 0; - p->msg.arg1 = 0; - p->msg.magic = A_SYNC ^ 0xffffffff; - if(write_packet(t->fd, t->serial, &p)) { - put_apacket(p); - D("%s: failed to write SYNC apacket to transport", t->serial); - } - -oops: - D("%s: transport output thread is exiting\n", t->serial); - kick_transport(t); - transport_unref(t); - return 0; -} - -static void *input_thread(void *_t) -{ - atransport *t = _t; - apacket *p; - int active = 0; - - D("%s: starting transport input thread, reading from fd %d\n", - t->serial, t->fd); - - for(;;){ - if(read_packet(t->fd, t->serial, &p)) { - D("%s: failed to read apacket from transport on fd %d\n", - t->serial, t->fd ); - break; - } - if(p->msg.command == A_SYNC){ - if(p->msg.arg0 == 0) { - D("%s: transport SYNC offline\n", t->serial); - put_apacket(p); - break; - } else { - if(p->msg.arg1 == t->sync_token) { - D("%s: transport SYNC online\n", t->serial); - active = 1; - } else { - D("%s: transport ignoring SYNC %d != %d\n", - t->serial, p->msg.arg1, t->sync_token); - } - } - } else { - if(active) { - D("%s: transport got packet, sending to remote\n", t->serial); - t->write_to_remote(p, t); - } else { - D("%s: transport ignoring packet while offline\n", t->serial); - } - } - - put_apacket(p); - } - - // this is necessary to avoid a race condition that occured when a transport closes - // while a client socket is still active. - close_all_sockets(t); - - D("%s: transport input thread is exiting, fd %d\n", t->serial, t->fd); - kick_transport(t); - transport_unref(t); - return 0; -} - - -static int transport_registration_send = -1; -static int transport_registration_recv = -1; -static fdevent transport_registration_fde; - -void update_transports(void) -{ - // nothing to do on the device side -} - -typedef struct tmsg tmsg; -struct tmsg -{ - atransport *transport; - int action; -}; - -static int -transport_read_action(int fd, struct tmsg* m) -{ - char *p = (char*)m; - int len = sizeof(*m); - int r; - - while(len > 0) { - r = adb_read(fd, p, len); - if(r > 0) { - len -= r; - p += r; - } else { - if((r < 0) && (errno == EINTR)) continue; - D("transport_read_action: on fd %d, error %d: %s\n", - fd, errno, strerror(errno)); - return -1; - } - } - return 0; -} - -static int -transport_write_action(int fd, struct tmsg* m) -{ - char *p = (char*)m; - int len = sizeof(*m); - int r; - - while(len > 0) { - r = adb_write(fd, p, len); - if(r > 0) { - len -= r; - p += r; - } else { - if((r < 0) && (errno == EINTR)) continue; - D("transport_write_action: on fd %d, error %d: %s\n", - fd, errno, strerror(errno)); - return -1; - } - } - return 0; -} - -static void transport_registration_func(int _fd, unsigned ev, void *data) -{ - tmsg m; - adb_thread_t output_thread_ptr; - adb_thread_t input_thread_ptr; - int s[2]; - atransport *t; - - if(!(ev & FDE_READ)) { - return; - } - - if(transport_read_action(_fd, &m)) { - fatal_errno("cannot read transport registration socket"); - } - - t = m.transport; - - if(m.action == 0){ - D("transport: %s removing and free'ing %d\n", t->serial, t->transport_socket); - - /* IMPORTANT: the remove closes one half of the - ** socket pair. The close closes the other half. - */ - fdevent_remove(&(t->transport_fde)); - adb_close(t->fd); - - adb_mutex_lock(&transport_lock); - t->next->prev = t->prev; - t->prev->next = t->next; - adb_mutex_unlock(&transport_lock); - - run_transport_disconnects(t); - - if (t->product) - free(t->product); - if (t->serial) - free(t->serial); - - memset(t,0xee,sizeof(atransport)); - free(t); - - update_transports(); - return; - } - - /* don't create transport threads for inaccessible devices */ - if (t->connection_state != CS_NOPERM) { - /* initial references are the two threads */ - t->ref_count = 2; - - if(adb_socketpair(s)) { - fatal_errno("cannot open transport socketpair"); - } - - D("transport: %s (%d,%d) starting\n", t->serial, s[0], s[1]); - - t->transport_socket = s[0]; - t->fd = s[1]; - - fdevent_install(&(t->transport_fde), - t->transport_socket, - transport_socket_events, - t); - - fdevent_set(&(t->transport_fde), FDE_READ); - - if(adb_thread_create(&input_thread_ptr, input_thread, t)){ - fatal_errno("cannot create input thread"); - } - - if(adb_thread_create(&output_thread_ptr, output_thread, t)){ - fatal_errno("cannot create output thread"); - } - } - - /* put us on the master device list */ - adb_mutex_lock(&transport_lock); - t->next = &transport_list; - t->prev = transport_list.prev; - t->next->prev = t; - t->prev->next = t; - adb_mutex_unlock(&transport_lock); - - t->disconnects.next = t->disconnects.prev = &t->disconnects; - - update_transports(); -} - -void init_transport_registration(void) -{ - int s[2]; - - if(adb_socketpair(s)){ - fatal_errno("cannot open transport registration socketpair"); - } - - transport_registration_send = s[0]; - transport_registration_recv = s[1]; - - fdevent_install(&transport_registration_fde, - transport_registration_recv, - transport_registration_func, - 0); - - fdevent_set(&transport_registration_fde, FDE_READ); -} - -/* the fdevent select pump is single threaded */ -static void register_transport(atransport *transport) -{ - tmsg m; - m.transport = transport; - m.action = 1; - D("transport: %s registered\n", transport->serial); - if(transport_write_action(transport_registration_send, &m)) { - fatal_errno("cannot write transport registration socket\n"); - } -} - -static void remove_transport(atransport *transport) -{ - tmsg m; - m.transport = transport; - m.action = 0; - D("transport: %s removed\n", transport->serial); - if(transport_write_action(transport_registration_send, &m)) { - fatal_errno("cannot write transport registration socket\n"); - } -} - - -static void transport_unref_locked(atransport *t) -{ - t->ref_count--; - if (t->ref_count == 0) { - D("transport: %s unref (kicking and closing)\n", t->serial); - if (!t->kicked) { - t->kicked = 1; - t->kick(t); - } - t->close(t); - remove_transport(t); - } else { - D("transport: %s unref (count=%d)\n", t->serial, t->ref_count); - } -} - -static void transport_unref(atransport *t) -{ - if (t) { - adb_mutex_lock(&transport_lock); - transport_unref_locked(t); - adb_mutex_unlock(&transport_lock); - } -} - -void add_transport_disconnect(atransport* t, adisconnect* dis) -{ - adb_mutex_lock(&transport_lock); - dis->next = &t->disconnects; - dis->prev = dis->next->prev; - dis->prev->next = dis; - dis->next->prev = dis; - adb_mutex_unlock(&transport_lock); -} - -void remove_transport_disconnect(atransport* t, adisconnect* dis) -{ - dis->prev->next = dis->next; - dis->next->prev = dis->prev; - dis->next = dis->prev = dis; -} - - -atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char** error_out) -{ - atransport *t; - atransport *result = NULL; - int ambiguous = 0; - -retry: - if (error_out) - *error_out = "device not found"; - - adb_mutex_lock(&transport_lock); - for (t = transport_list.next; t != &transport_list; t = t->next) { - if (t->connection_state == CS_NOPERM) { - if (error_out) - *error_out = "insufficient permissions for device"; - continue; - } - - /* check for matching serial number */ - if (serial) { - if (t->serial && !strcmp(serial, t->serial)) { - result = t; - break; - } - } else { - if (ttype == kTransportUsb && t->type == kTransportUsb) { - if (result) { - if (error_out) - *error_out = "more than one device"; - ambiguous = 1; - result = NULL; - break; - } - result = t; - } else if (ttype == kTransportLocal && t->type == kTransportLocal) { - if (result) { - if (error_out) - *error_out = "more than one emulator"; - ambiguous = 1; - result = NULL; - break; - } - result = t; - } else if (ttype == kTransportAny) { - if (result) { - if (error_out) - *error_out = "more than one device and emulator"; - ambiguous = 1; - result = NULL; - break; - } - result = t; - } - } - } - adb_mutex_unlock(&transport_lock); - - if (result) { - /* offline devices are ignored -- they are either being born or dying */ - if (result && result->connection_state == CS_OFFLINE) { - if (error_out) - *error_out = "device offline"; - result = NULL; - } - /* check for required connection state */ - if (result && state != CS_ANY && result->connection_state != state) { - if (error_out) - *error_out = "invalid device state"; - result = NULL; - } - } - - if (result) { - /* found one that we can take */ - if (error_out) - *error_out = NULL; - } else if (state != CS_ANY && (serial || !ambiguous)) { - adb_sleep_ms(1000); - goto retry; - } - - return result; -} - -void register_usb_transport(usb_handle *usb, const char *serial, unsigned writeable) -{ - atransport *t = calloc(1, sizeof(atransport)); - D("transport: %p init'ing for usb_handle %p (sn='%s')\n", t, usb, - serial ? serial : ""); - init_usb_transport(t, usb, (writeable ? CS_OFFLINE : CS_NOPERM)); - if(serial) { - t->serial = strdup(serial); - } - register_transport(t); -} - -/* this should only be used for transports with connection_state == CS_NOPERM */ -void unregister_usb_transport(usb_handle *usb) -{ - atransport *t; - adb_mutex_lock(&transport_lock); - for(t = transport_list.next; t != &transport_list; t = t->next) { - if (t->usb == usb && t->connection_state == CS_NOPERM) { - t->next->prev = t->prev; - t->prev->next = t->next; - break; - } - } - adb_mutex_unlock(&transport_lock); -} - -#undef TRACE_TAG -#define TRACE_TAG TRACE_RWX - -int readx(int fd, void *ptr, size_t len) -{ - char *p = ptr; - int r; -#if ADB_TRACE - size_t len0 = len; -#endif - D("readx: fd=%d wanted=%d\n", fd, (int)len); - while(len > 0) { - r = adb_read(fd, p, len); - if(r > 0) { - len -= r; - p += r; - } else { - if (r < 0) { - D("readx: fd=%d error %d: %s\n", fd, errno, strerror(errno)); - if (errno == EINTR) - continue; - } else { - D("readx: fd=%d disconnected\n", fd); - } - return -1; - } - } - -#if ADB_TRACE - D("readx: fd=%d wanted=%zu got=%zu\n", fd, len0, len0 - len); - dump_hex( ptr, len0 ); -#endif - return 0; -} - -int writex(int fd, const void *ptr, size_t len) -{ - char *p = (char*) ptr; - int r; - -#if ADB_TRACE - D("writex: fd=%d len=%d: ", fd, (int)len); - dump_hex( ptr, len ); -#endif - while(len > 0) { - r = adb_write(fd, p, len); - if(r > 0) { - len -= r; - p += r; - } else { - if (r < 0) { - D("writex: fd=%d error %d: %s\n", fd, errno, strerror(errno)); - if (errno == EINTR) - continue; - } else { - D("writex: fd=%d disconnected\n", fd); - } - return -1; - } - } - return 0; -} - -int check_header(apacket *p) -{ - if(p->msg.magic != (p->msg.command ^ 0xffffffff)) { - D("check_header(): invalid magic\n"); - return -1; - } - - if(p->msg.data_length > MAX_PAYLOAD) { - D("check_header(): %d > MAX_PAYLOAD\n", p->msg.data_length); - return -1; - } - - return 0; -} - -int check_data(apacket *p) -{ - unsigned count, sum; - unsigned char *x; - - count = p->msg.data_length; - x = p->data; - sum = 0; - while(count-- > 0) { - sum += *x++; - } - - if(sum != p->msg.data_check) { - return -1; - } else { - return 0; - } -} diff --git a/minadbd/transport_usb.c b/minadbd/transport_usb.c deleted file mode 100644 index 91cbf6151..000000000 --- a/minadbd/transport_usb.c +++ /dev/null @@ -1,121 +0,0 @@ -/* - * Copyright (C) 2007 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 -#include -#include - -#include - -#define TRACE_TAG TRACE_TRANSPORT -#include "adb.h" - -#ifdef HAVE_BIG_ENDIAN -#define H4(x) (((x) & 0xFF000000) >> 24) | (((x) & 0x00FF0000) >> 8) | (((x) & 0x0000FF00) << 8) | (((x) & 0x000000FF) << 24) -static inline void fix_endians(apacket *p) -{ - p->msg.command = H4(p->msg.command); - p->msg.arg0 = H4(p->msg.arg0); - p->msg.arg1 = H4(p->msg.arg1); - p->msg.data_length = H4(p->msg.data_length); - p->msg.data_check = H4(p->msg.data_check); - p->msg.magic = H4(p->msg.magic); -} -unsigned host_to_le32(unsigned n) -{ - return H4(n); -} -#else -#define fix_endians(p) do {} while (0) -unsigned host_to_le32(unsigned n) -{ - return n; -} -#endif - -static int remote_read(apacket *p, atransport *t) -{ - if(usb_read(t->usb, &p->msg, sizeof(amessage))){ - D("remote usb: read terminated (message)\n"); - return -1; - } - - fix_endians(p); - - if(check_header(p)) { - D("remote usb: check_header failed\n"); - return -1; - } - - if(p->msg.data_length) { - if(usb_read(t->usb, p->data, p->msg.data_length)){ - D("remote usb: terminated (data)\n"); - return -1; - } - } - - if(check_data(p)) { - D("remote usb: check_data failed\n"); - return -1; - } - - return 0; -} - -static int remote_write(apacket *p, atransport *t) -{ - unsigned size = p->msg.data_length; - - fix_endians(p); - - if(usb_write(t->usb, &p->msg, sizeof(amessage))) { - D("remote usb: 1 - write terminated\n"); - return -1; - } - if(p->msg.data_length == 0) return 0; - if(usb_write(t->usb, &p->data, size)) { - D("remote usb: 2 - write terminated\n"); - return -1; - } - - return 0; -} - -static void remote_close(atransport *t) -{ - usb_close(t->usb); - t->usb = 0; -} - -static void remote_kick(atransport *t) -{ - usb_kick(t->usb); -} - -void init_usb_transport(atransport *t, usb_handle *h, int state) -{ - D("transport: usb\n"); - t->close = remote_close; - t->kick = remote_kick; - t->read_from_remote = remote_read; - t->write_to_remote = remote_write; - t->sync_token = 1; - t->connection_state = state; - t->type = kTransportUsb; - t->usb = h; - - HOST = 0; -} diff --git a/minadbd/usb_linux_client.c b/minadbd/usb_linux_client.c deleted file mode 100644 index b3a38dce5..000000000 --- a/minadbd/usb_linux_client.c +++ /dev/null @@ -1,541 +0,0 @@ -/* - * Copyright (C) 2007 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 -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include "sysdeps.h" - -#define TRACE_TAG TRACE_USB -#include "adb.h" - -#define MAX_PACKET_SIZE_FS 64 -#define MAX_PACKET_SIZE_HS 512 - -#define cpu_to_le16(x) htole16(x) -#define cpu_to_le32(x) htole32(x) - -struct usb_handle -{ - int fd; - adb_cond_t notify; - adb_mutex_t lock; - - int (*write)(usb_handle *h, const void *data, int len); - int (*read)(usb_handle *h, void *data, int len); - void (*kick)(usb_handle *h); - - int control; - int bulk_out; /* "out" from the host's perspective => source for adbd */ - int bulk_in; /* "in" from the host's perspective => sink for adbd */ -}; - -struct func_desc { - struct usb_interface_descriptor intf; - struct usb_endpoint_descriptor_no_audio source; - struct usb_endpoint_descriptor_no_audio sink; -} __attribute__((packed)); - -struct desc_v1 { - struct usb_functionfs_descs_head_v1 { - __le32 magic; - __le32 length; - __le32 fs_count; - __le32 hs_count; - } __attribute__((packed)) header; - struct func_desc fs_descs, hs_descs; -} __attribute__((packed)); - -struct desc_v2 { - struct usb_functionfs_descs_head_v2 header; - // The rest of the structure depends on the flags in the header. - __le32 fs_count; - __le32 hs_count; - struct func_desc fs_descs, hs_descs; -} __attribute__((packed)); - -/*static const struct { - struct usb_functionfs_descs_head header; - struct { - struct usb_interface_descriptor intf; - struct usb_endpoint_descriptor_no_audio source; - struct usb_endpoint_descriptor_no_audio sink; - } __attribute__((packed)) fs_descs, hs_descs; -} __attribute__((packed)) descriptors = { - .header = { - .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC), - .length = cpu_to_le32(sizeof(descriptors)), - .fs_count = 3, - .hs_count = 3, - }, - -*/ - -struct func_desc fs_descriptors = { - .intf = { - .bLength = sizeof(fs_descriptors.intf), - .bDescriptorType = USB_DT_INTERFACE, - .bInterfaceNumber = 0, - .bNumEndpoints = 2, - .bInterfaceClass = ADB_CLASS, - .bInterfaceSubClass = ADB_SUBCLASS, - .bInterfaceProtocol = ADB_PROTOCOL, - .iInterface = 1, /* first string from the provided table */ - }, - .source = { - .bLength = sizeof(fs_descriptors.source), - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 1 | USB_DIR_OUT, - .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = MAX_PACKET_SIZE_FS, - }, - .sink = { - .bLength = sizeof(fs_descriptors.sink), - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 2 | USB_DIR_IN, - .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = MAX_PACKET_SIZE_FS, - }, -}; - -struct func_desc hs_descriptors = { - .intf = { - .bLength = sizeof(hs_descriptors.intf), - .bDescriptorType = USB_DT_INTERFACE, - .bInterfaceNumber = 0, - .bNumEndpoints = 2, - .bInterfaceClass = ADB_CLASS, - .bInterfaceSubClass = ADB_SUBCLASS, - .bInterfaceProtocol = ADB_PROTOCOL, - .iInterface = 1, /* first string from the provided table */ - }, - .source = { - .bLength = sizeof(hs_descriptors.source), - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 1 | USB_DIR_OUT, - .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = MAX_PACKET_SIZE_HS, - }, - .sink = { - .bLength = sizeof(hs_descriptors.sink), - .bDescriptorType = USB_DT_ENDPOINT, - .bEndpointAddress = 2 | USB_DIR_IN, - .bmAttributes = USB_ENDPOINT_XFER_BULK, - .wMaxPacketSize = MAX_PACKET_SIZE_HS, - }, -}; - -#define STR_INTERFACE_ "ADB Interface" - -static const struct { - struct usb_functionfs_strings_head header; - struct { - __le16 code; - const char str1[sizeof(STR_INTERFACE_)]; - } __attribute__((packed)) lang0; -} __attribute__((packed)) strings = { - .header = { - .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC), - .length = cpu_to_le32(sizeof(strings)), - .str_count = cpu_to_le32(1), - .lang_count = cpu_to_le32(1), - }, - .lang0 = { - cpu_to_le16(0x0409), /* en-us */ - STR_INTERFACE_, - }, -}; - -void usb_cleanup() -{ - // nothing to do here -} - -static void *usb_adb_open_thread(void *x) -{ - struct usb_handle *usb = (struct usb_handle *)x; - int fd; - - while (1) { - // wait until the USB device needs opening - adb_mutex_lock(&usb->lock); - while (usb->fd != -1) - adb_cond_wait(&usb->notify, &usb->lock); - adb_mutex_unlock(&usb->lock); - - D("[ usb_thread - opening device ]\n"); - do { - /* XXX use inotify? */ - fd = unix_open("/dev/android_adb", O_RDWR); - if (fd < 0) { - // to support older kernels - fd = unix_open("/dev/android", O_RDWR); - fprintf(stderr, "usb_adb_open_thread: %d\n", fd ); - } - if (fd < 0) { - adb_sleep_ms(1000); - } - } while (fd < 0); - D("[ opening device succeeded ]\n"); - - close_on_exec(fd); - usb->fd = fd; - - D("[ usb_thread - registering device ]\n"); - register_usb_transport(usb, 0, 1); - } - - // never gets here - return 0; -} - -static int usb_adb_write(usb_handle *h, const void *data, int len) -{ - int n; - - D("about to write (fd=%d, len=%d)\n", h->fd, len); - n = adb_write(h->fd, data, len); - if(n != len) { - D("ERROR: fd = %d, n = %d, errno = %d (%s)\n", - h->fd, n, errno, strerror(errno)); - return -1; - } - D("[ done fd=%d ]\n", h->fd); - return 0; -} - -static int usb_adb_read(usb_handle *h, void *data, int len) -{ - int n; - - D("about to read (fd=%d, len=%d)\n", h->fd, len); - n = adb_read(h->fd, data, len); - if(n != len) { - D("ERROR: fd = %d, n = %d, errno = %d (%s)\n", - h->fd, n, errno, strerror(errno)); - return -1; - } - D("[ done fd=%d ]\n", h->fd); - return 0; -} - -static void usb_adb_kick(usb_handle *h) -{ - D("usb_kick\n"); - adb_mutex_lock(&h->lock); - adb_close(h->fd); - h->fd = -1; - - // notify usb_adb_open_thread that we are disconnected - adb_cond_signal(&h->notify); - adb_mutex_unlock(&h->lock); -} - -static void usb_adb_init() -{ - usb_handle *h; - adb_thread_t tid; - int fd; - - h = calloc(1, sizeof(usb_handle)); - - h->write = usb_adb_write; - h->read = usb_adb_read; - h->kick = usb_adb_kick; - h->fd = -1; - - adb_cond_init(&h->notify, 0); - adb_mutex_init(&h->lock, 0); - - fprintf(stderr, "Starting to open usb_init()\n"); - // Open the file /dev/android_adb_enable to trigger - // the enabling of the adb USB function in the kernel. - // We never touch this file again - just leave it open - // indefinitely so the kernel will know when we are running - // and when we are not. - fd = unix_open("/dev/android_adb_enable", O_RDWR); - fprintf(stderr, "unix_open to open usb_init(): %d\n", fd); - if (fd < 0) { - D("failed to open /dev/android_adb_enable\n"); - } else { - close_on_exec(fd); - } - - D("[ usb_init - starting thread ]\n"); - if(adb_thread_create(&tid, usb_adb_open_thread, h)){ - fatal_errno("cannot create usb thread"); - fprintf(stderr, "cannot create the usb thread()\n"); - } -} - - -static void init_functionfs(struct usb_handle *h) -{ - ssize_t ret; - struct desc_v1 v1_descriptor; - struct desc_v2 v2_descriptor; - - v2_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC_V2); - v2_descriptor.header.length = cpu_to_le32(sizeof(v2_descriptor)); - v2_descriptor.header.flags = FUNCTIONFS_HAS_FS_DESC | FUNCTIONFS_HAS_HS_DESC; - v2_descriptor.fs_count = 3; - v2_descriptor.hs_count = 3; - v2_descriptor.fs_descs = fs_descriptors; - v2_descriptor.hs_descs = hs_descriptors; - - - D("OPENING %s\n", USB_FFS_ADB_EP0); - h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR); - if (h->control < 0) { - D("[ %s: cannot open control endpoint: errno=%d]\n", USB_FFS_ADB_EP0, errno); - goto err; - } - - ret = adb_write(h->control, &v2_descriptor, sizeof(v2_descriptor)); - if (ret < 0) { - v1_descriptor.header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC); - v1_descriptor.header.length = cpu_to_le32(sizeof(v1_descriptor)); - v1_descriptor.header.fs_count = 3; - v1_descriptor.header.hs_count = 3; - v1_descriptor.fs_descs = fs_descriptors; - v1_descriptor.hs_descs = hs_descriptors; - D("[ %s: Switching to V1_descriptor format errno=%d ]\n", USB_FFS_ADB_EP0, errno); - ret = adb_write(h->control, &v1_descriptor, sizeof(v1_descriptor)); - if (ret < 0) { - D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno); - goto err; - } - } - - ret = adb_write(h->control, &strings, sizeof(strings)); - if (ret < 0) { - D("[ %s: writing strings failed: errno=%d]\n", USB_FFS_ADB_EP0, errno); - goto err; - } - - h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR); - if (h->bulk_out < 0) { - D("[ %s: cannot open bulk-out ep: errno=%d ]\n", USB_FFS_ADB_OUT, errno); - goto err; - } - - h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR); - if (h->bulk_in < 0) { - D("[ %s: cannot open bulk-in ep: errno=%d ]\n", USB_FFS_ADB_IN, errno); - goto err; - } - - return; - -err: - if (h->bulk_in > 0) { - adb_close(h->bulk_in); - h->bulk_in = -1; - } - if (h->bulk_out > 0) { - adb_close(h->bulk_out); - h->bulk_out = -1; - } - if (h->control > 0) { - adb_close(h->control); - h->control = -1; - } - return; -} - -static void *usb_ffs_open_thread(void *x) -{ - struct usb_handle *usb = (struct usb_handle *)x; - - while (1) { - // wait until the USB device needs opening - adb_mutex_lock(&usb->lock); - while (usb->control != -1) - adb_cond_wait(&usb->notify, &usb->lock); - adb_mutex_unlock(&usb->lock); - - while (1) { - init_functionfs(usb); - - if (usb->control >= 0) - break; - - adb_sleep_ms(1000); - } - - D("[ usb_thread - registering device ]\n"); - register_usb_transport(usb, 0, 1); - } - - // never gets here - return 0; -} - -static int bulk_write(int bulk_in, const char *buf, size_t length) -{ - size_t count = 0; - int ret; - - do { - ret = adb_write(bulk_in, buf + count, length - count); - if (ret < 0) { - if (errno != EINTR) - return ret; - } else { - count += ret; - } - } while (count < length); - - D("[ bulk_write done fd=%d ]\n", bulk_in); - return count; -} - -static int usb_ffs_write(usb_handle *h, const void *data, int len) -{ - int n; - - D("about to write (fd=%d, len=%d)\n", h->bulk_in, len); - n = bulk_write(h->bulk_in, data, len); - if (n != len) { - D("ERROR: fd = %d, n = %d, errno = %d (%s)\n", - h->bulk_in, n, errno, strerror(errno)); - return -1; - } - D("[ done fd=%d ]\n", h->bulk_in); - return 0; -} - -static int bulk_read(int bulk_out, char *buf, size_t length) -{ - size_t count = 0; - int ret; - - do { - ret = adb_read(bulk_out, buf + count, length - count); - if (ret < 0) { - if (errno != EINTR) { - D("[ bulk_read failed fd=%d length=%zu count=%zu ]\n", - bulk_out, length, count); - return ret; - } - } else { - count += ret; - } - } while (count < length); - - return count; -} - -static int usb_ffs_read(usb_handle *h, void *data, int len) -{ - int n; - - D("about to read (fd=%d, len=%d)\n", h->bulk_out, len); - n = bulk_read(h->bulk_out, data, len); - if (n != len) { - D("ERROR: fd = %d, n = %d, errno = %d (%s)\n", - h->bulk_out, n, errno, strerror(errno)); - return -1; - } - D("[ done fd=%d ]\n", h->bulk_out); - return 0; -} - -static void usb_ffs_kick(usb_handle *h) -{ - int err; - - err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT); - if (err < 0) - D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno); - - err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT); - if (err < 0) - D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno); - - adb_mutex_lock(&h->lock); - adb_close(h->control); - adb_close(h->bulk_out); - adb_close(h->bulk_in); - h->control = h->bulk_out = h->bulk_in = -1; - - // notify usb_ffs_open_thread that we are disconnected - adb_cond_signal(&h->notify); - adb_mutex_unlock(&h->lock); -} - -static void usb_ffs_init() -{ - usb_handle *h; - adb_thread_t tid; - - D("[ usb_init - using FunctionFS ]\n"); - - h = calloc(1, sizeof(usb_handle)); - - h->write = usb_ffs_write; - h->read = usb_ffs_read; - h->kick = usb_ffs_kick; - - h->control = -1; - h->bulk_out = -1; - h->bulk_out = -1; - - adb_cond_init(&h->notify, 0); - adb_mutex_init(&h->lock, 0); - - D("[ usb_init - starting thread ]\n"); - if (adb_thread_create(&tid, usb_ffs_open_thread, h)){ - fatal_errno("[ cannot create usb thread ]\n"); - } -} - -void usb_init() -{ - if (access(USB_FFS_ADB_EP0, F_OK) == 0) - usb_ffs_init(); - else - usb_adb_init(); -} - -int usb_write(usb_handle *h, const void *data, int len) -{ - return h->write(h, data, len); -} - -int usb_read(usb_handle *h, void *data, int len) -{ - return h->read(h, data, len); -} -int usb_close(usb_handle *h) -{ - // nothing to do here - return 0; -} - -void usb_kick(usb_handle *h) -{ - h->kick(h); -} -- cgit v1.2.3 From 2b2a14ff3528b1b65e78b4d2bbb2dd2fb0c326c5 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Wed, 18 Feb 2015 14:24:48 -0800 Subject: Remove unused code. Change-Id: Ie37734e75bc4d1e284dcb5dee4c0512021663dbd --- minadbd/Android.mk | 1 - minadbd/utils.c | 106 ----------------------------------------------------- minadbd/utils.h | 76 -------------------------------------- 3 files changed, 183 deletions(-) delete mode 100644 minadbd/utils.c delete mode 100644 minadbd/utils.h diff --git a/minadbd/Android.mk b/minadbd/Android.mk index 8de1b7453..0f1634bb2 100644 --- a/minadbd/Android.mk +++ b/minadbd/Android.mk @@ -15,7 +15,6 @@ LOCAL_SRC_FILES := \ fuse_adb_provider.c \ sockets.c \ services.c \ - utils.c LOCAL_CFLAGS := -O2 -g -DADB_HOST=0 -Wall -Wno-unused-parameter LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE diff --git a/minadbd/utils.c b/minadbd/utils.c deleted file mode 100644 index 91518bab6..000000000 --- a/minadbd/utils.c +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright (C) 2008 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 "utils.h" -#include -#include -#include - -char* -buff_addc (char* buff, char* buffEnd, int c) -{ - int avail = buffEnd - buff; - - if (avail <= 0) /* already in overflow mode */ - return buff; - - if (avail == 1) { /* overflowing, the last byte is reserved for zero */ - buff[0] = 0; - return buff + 1; - } - - buff[0] = (char) c; /* add char and terminating zero */ - buff[1] = 0; - return buff + 1; -} - -char* -buff_adds (char* buff, char* buffEnd, const char* s) -{ - int slen = strlen(s); - - return buff_addb(buff, buffEnd, s, slen); -} - -char* -buff_addb (char* buff, char* buffEnd, const void* data, int len) -{ - int avail = (buffEnd - buff); - - if (avail <= 0 || len <= 0) /* already overflowing */ - return buff; - - if (len > avail) - len = avail; - - memcpy(buff, data, len); - - buff += len; - - /* ensure there is a terminating zero */ - if (buff >= buffEnd) { /* overflow */ - buff[-1] = 0; - } else - buff[0] = 0; - - return buff; -} - -char* -buff_add (char* buff, char* buffEnd, const char* format, ... ) -{ - int avail; - - avail = (buffEnd - buff); - - if (avail > 0) { - va_list args; - int nn; - - va_start(args, format); - nn = vsnprintf( buff, avail, format, args); - va_end(args); - - if (nn < 0) { - /* some C libraries return -1 in case of overflow, - * but they will also do that if the format spec is - * invalid. We assume ADB is not buggy enough to - * trigger that last case. */ - nn = avail; - } - else if (nn > avail) { - nn = avail; - } - - buff += nn; - - /* ensure that there is a terminating zero */ - if (buff >= buffEnd) - buff[-1] = 0; - else - buff[0] = 0; - } - return buff; -} diff --git a/minadbd/utils.h b/minadbd/utils.h deleted file mode 100644 index e833820ab..000000000 --- a/minadbd/utils.h +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (C) 2008 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 _ADB_UTILS_H -#define _ADB_UTILS_H - -#ifdef __cplusplus -extern "C" { -#endif - -/* bounded buffer functions */ - -/* all these functions are used to append data to a bounded buffer. - * - * after each operation, the buffer is guaranteed to be zero-terminated, - * even in the case of an overflow. they all return the new buffer position - * which allows one to use them in succession, only checking for overflows - * at the end. For example: - * - * BUFF_DECL(temp,p,end,1024); - * char* p; - * - * p = buff_addc(temp, end, '"'); - * p = buff_adds(temp, end, string); - * p = buff_addc(temp, end, '"'); - * - * if (p >= end) { - * overflow detected. note that 'temp' is - * zero-terminated for safety. - * } - * return strdup(temp); - */ - -/* tries to add a character to the buffer, in case of overflow - * this will only write a terminating zero and return buffEnd. - */ -char* buff_addc (char* buff, char* buffEnd, int c); - -/* tries to add a string to the buffer */ -char* buff_adds (char* buff, char* buffEnd, const char* s); - -/* tries to add a bytes to the buffer. the input can contain zero bytes, - * but a terminating zero will always be appended at the end anyway - */ -char* buff_addb (char* buff, char* buffEnd, const void* data, int len); - -/* tries to add a formatted string to a bounded buffer */ -char* buff_add (char* buff, char* buffEnd, const char* format, ... ); - -/* convenience macro used to define a bounded buffer, as well as - * a 'cursor' and 'end' variables all in one go. - * - * note: this doesn't place an initial terminating zero in the buffer, - * you need to use one of the buff_ functions for this. or simply - * do _cursor[0] = 0 manually. - */ -#define BUFF_DECL(_buff,_cursor,_end,_size) \ - char _buff[_size], *_cursor=_buff, *_end = _cursor + (_size) - -#ifdef __cplusplus -} -#endif - -#endif /* _ADB_UTILS_H */ -- cgit v1.2.3 From 1ddd35050417b79307c5e0a989ee39d6ce2bfc1b Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Wed, 18 Feb 2015 15:58:15 -0800 Subject: Use headers from adb. adb.h has diverged a bit, so that one will be more involved, but these three are all trivial, unimportant changes. Change-Id: Ief8474c1c2927d7e955adf04f887c76ab37077a6 --- Android.mk | 7 +- minadbd/Android.mk | 2 +- minadbd/fdevent.h | 90 --------- minadbd/mutex_list.h | 26 --- minadbd/services.c | 4 +- minadbd/sysdeps.h | 506 --------------------------------------------------- minadbd/transport.h | 35 ---- 7 files changed, 7 insertions(+), 663 deletions(-) delete mode 100644 minadbd/fdevent.h delete mode 100644 minadbd/mutex_list.h delete mode 100644 minadbd/sysdeps.h delete mode 100644 minadbd/transport.h diff --git a/Android.mk b/Android.mk index 0cb0836cd..b8ef63e99 100644 --- a/Android.mk +++ b/Android.mk @@ -54,7 +54,10 @@ RECOVERY_FSTAB_VERSION := 2 LOCAL_CFLAGS += -DRECOVERY_API_VERSION=$(RECOVERY_API_VERSION) LOCAL_CFLAGS += -Wno-unused-parameter -LOCAL_C_INCLUDES += system/vold +LOCAL_C_INCLUDES += \ + system/vold \ + system/extras/ext4_utils \ + system/core/adb \ LOCAL_STATIC_LIBRARIES := \ libext4_utils_static \ @@ -94,8 +97,6 @@ else LOCAL_STATIC_LIBRARIES += $(TARGET_RECOVERY_UI_LIB) endif -LOCAL_C_INCLUDES += system/extras/ext4_utils - include $(BUILD_EXECUTABLE) # All the APIs for testing diff --git a/minadbd/Android.mk b/minadbd/Android.mk index 0f1634bb2..fecce8777 100644 --- a/minadbd/Android.mk +++ b/minadbd/Android.mk @@ -18,7 +18,7 @@ LOCAL_SRC_FILES := \ LOCAL_CFLAGS := -O2 -g -DADB_HOST=0 -Wall -Wno-unused-parameter LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE -LOCAL_C_INCLUDES += bootable/recovery +LOCAL_C_INCLUDES := bootable/recovery system/core/adb LOCAL_MODULE := libminadbd diff --git a/minadbd/fdevent.h b/minadbd/fdevent.h deleted file mode 100644 index 38b08cce1..000000000 --- a/minadbd/fdevent.h +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2006 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 __FDEVENT_H -#define __FDEVENT_H - -#include /* for int64_t */ - -#ifdef __cplusplus -extern "C" { -#endif - -/* events that may be observed */ -#define FDE_READ 0x0001 -#define FDE_WRITE 0x0002 -#define FDE_ERROR 0x0004 -#define FDE_TIMEOUT 0x0008 - -/* features that may be set (via the events set/add/del interface) */ -#define FDE_DONT_CLOSE 0x0080 - -typedef struct fdevent fdevent; - -typedef void (*fd_func)(int fd, unsigned events, void *userdata); - -/* Allocate and initialize a new fdevent object - * Note: use FD_TIMER as 'fd' to create a fd-less object - * (used to implement timers). -*/ -fdevent *fdevent_create(int fd, fd_func func, void *arg); - -/* Uninitialize and deallocate an fdevent object that was -** created by fdevent_create() -*/ -void fdevent_destroy(fdevent *fde); - -/* Initialize an fdevent object that was externally allocated -*/ -void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg); - -/* Uninitialize an fdevent object that was initialized by -** fdevent_install() -*/ -void fdevent_remove(fdevent *item); - -/* Change which events should cause notifications -*/ -void fdevent_set(fdevent *fde, unsigned events); -void fdevent_add(fdevent *fde, unsigned events); -void fdevent_del(fdevent *fde, unsigned events); - -void fdevent_set_timeout(fdevent *fde, int64_t timeout_ms); - -/* loop forever, handling events. -*/ -void fdevent_loop(); - -struct fdevent -{ - fdevent *next; - fdevent *prev; - - int fd; - int force_eof; - - unsigned short state; - unsigned short events; - - fd_func func; - void *arg; -}; - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/minadbd/mutex_list.h b/minadbd/mutex_list.h deleted file mode 100644 index 652dd7341..000000000 --- a/minadbd/mutex_list.h +++ /dev/null @@ -1,26 +0,0 @@ -/* the list of mutexes used by adb */ -/* #ifndef __MUTEX_LIST_H - * Do not use an include-guard. This file is included once to declare the locks - * and once in win32 to actually do the runtime initialization. - */ -#ifndef ADB_MUTEX -#error ADB_MUTEX not defined when including this file -#endif -ADB_MUTEX(dns_lock) -ADB_MUTEX(socket_list_lock) -ADB_MUTEX(transport_lock) -#if ADB_HOST -ADB_MUTEX(local_transports_lock) -#endif -ADB_MUTEX(usb_lock) - -// Sadly logging to /data/adb/adb-... is not thread safe. -// After modifying adb.h::D() to count invocations: -// DEBUG(jpa):0:Handling main() -// DEBUG(jpa):1:[ usb_init - starting thread ] -// (Oopsies, no :2:, and matching message is also gone.) -// DEBUG(jpa):3:[ usb_thread - opening device ] -// DEBUG(jpa):4:jdwp control socket started (10) -ADB_MUTEX(D_lock) - -#undef ADB_MUTEX diff --git a/minadbd/services.c b/minadbd/services.c index 218b84a38..2a52eebae 100644 --- a/minadbd/services.c +++ b/minadbd/services.c @@ -47,9 +47,9 @@ void *service_bootstrap_func(void *x) static void sideload_host_service(int sfd, void* cookie) { char* saveptr; - const char* s = strtok_r(cookie, ":", &saveptr); + const char* s = adb_strtok_r(cookie, ":", &saveptr); uint64_t file_size = strtoull(s, NULL, 10); - s = strtok_r(NULL, ":", &saveptr); + s = adb_strtok_r(NULL, ":", &saveptr); uint32_t block_size = strtoul(s, NULL, 10); printf("sideload-host file size %llu block size %lu\n", file_size, block_size); diff --git a/minadbd/sysdeps.h b/minadbd/sysdeps.h deleted file mode 100644 index 3edaef472..000000000 --- a/minadbd/sysdeps.h +++ /dev/null @@ -1,506 +0,0 @@ -/* - * Copyright (C) 2007 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 file contains system-dependent definitions used by ADB - * they're related to threads, sockets and file descriptors - */ -#ifndef _ADB_SYSDEPS_H -#define _ADB_SYSDEPS_H - -#ifdef __CYGWIN__ -# undef _WIN32 -#endif - -#ifdef _WIN32 - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -#define OS_PATH_SEPARATOR '\\' -#define OS_PATH_SEPARATOR_STR "\\" - -typedef CRITICAL_SECTION adb_mutex_t; - -#define ADB_MUTEX_DEFINE(x) adb_mutex_t x - -/* declare all mutexes */ -/* For win32, adb_sysdeps_init() will do the mutex runtime initialization. */ -#define ADB_MUTEX(x) extern adb_mutex_t x; -#include "mutex_list.h" - -extern void adb_sysdeps_init(void); - -static __inline__ void adb_mutex_lock( adb_mutex_t* lock ) -{ - EnterCriticalSection( lock ); -} - -static __inline__ void adb_mutex_unlock( adb_mutex_t* lock ) -{ - LeaveCriticalSection( lock ); -} - -typedef struct { unsigned tid; } adb_thread_t; - -typedef void* (*adb_thread_func_t)(void* arg); - -typedef void (*win_thread_func_t)(void* arg); - -static __inline__ int adb_thread_create( adb_thread_t *thread, adb_thread_func_t func, void* arg) -{ - thread->tid = _beginthread( (win_thread_func_t)func, 0, arg ); - if (thread->tid == (unsigned)-1L) { - return -1; - } - return 0; -} - -static __inline__ void close_on_exec(int fd) -{ - /* nothing really */ -} - -extern void disable_tcp_nagle(int fd); - -#define lstat stat /* no symlinks on Win32 */ - -#define S_ISLNK(m) 0 /* no symlinks on Win32 */ - -static __inline__ int adb_unlink(const char* path) -{ - int rc = unlink(path); - - if (rc == -1 && errno == EACCES) { - /* unlink returns EACCES when the file is read-only, so we first */ - /* try to make it writable, then unlink again... */ - rc = chmod(path, _S_IREAD|_S_IWRITE ); - if (rc == 0) - rc = unlink(path); - } - return rc; -} -#undef unlink -#define unlink ___xxx_unlink - -static __inline__ int adb_mkdir(const char* path, int mode) -{ - return _mkdir(path); -} -#undef mkdir -#define mkdir ___xxx_mkdir - -extern int adb_open(const char* path, int options); -extern int adb_creat(const char* path, int mode); -extern int adb_read(int fd, void* buf, int len); -extern int adb_write(int fd, const void* buf, int len); -extern int adb_lseek(int fd, int pos, int where); -extern int adb_shutdown(int fd); -extern int adb_close(int fd); - -static __inline__ int unix_close(int fd) -{ - return close(fd); -} -#undef close -#define close ____xxx_close - -static __inline__ int unix_read(int fd, void* buf, size_t len) -{ - return read(fd, buf, len); -} -#undef read -#define read ___xxx_read - -static __inline__ int unix_write(int fd, const void* buf, size_t len) -{ - return write(fd, buf, len); -} -#undef write -#define write ___xxx_write - -static __inline__ int adb_open_mode(const char* path, int options, int mode) -{ - return adb_open(path, options); -} - -static __inline__ int unix_open(const char* path, int options,...) -{ - if ((options & O_CREAT) == 0) - { - return open(path, options); - } - else - { - int mode; - va_list args; - va_start( args, options ); - mode = va_arg( args, int ); - va_end( args ); - return open(path, options, mode); - } -} -#define open ___xxx_unix_open - - -/* normally provided by */ -extern void* load_file(const char* pathname, unsigned* psize); - -/* normally provided by */ -extern int socket_loopback_client(int port, int type); -extern int socket_network_client(const char *host, int port, int type); -extern int socket_loopback_server(int port, int type); -extern int socket_inaddr_any_server(int port, int type); - -/* normally provided by "fdevent.h" */ - -#define FDE_READ 0x0001 -#define FDE_WRITE 0x0002 -#define FDE_ERROR 0x0004 -#define FDE_DONT_CLOSE 0x0080 - -typedef struct fdevent fdevent; - -typedef void (*fd_func)(int fd, unsigned events, void *userdata); - -fdevent *fdevent_create(int fd, fd_func func, void *arg); -void fdevent_destroy(fdevent *fde); -void fdevent_install(fdevent *fde, int fd, fd_func func, void *arg); -void fdevent_remove(fdevent *item); -void fdevent_set(fdevent *fde, unsigned events); -void fdevent_add(fdevent *fde, unsigned events); -void fdevent_del(fdevent *fde, unsigned events); -void fdevent_loop(); - -struct fdevent { - fdevent *next; - fdevent *prev; - - int fd; - int force_eof; - - unsigned short state; - unsigned short events; - - fd_func func; - void *arg; -}; - -static __inline__ void adb_sleep_ms( int mseconds ) -{ - Sleep( mseconds ); -} - -extern int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen); - -#undef accept -#define accept ___xxx_accept - -static __inline__ int adb_socket_setbufsize( int fd, int bufsize ) -{ - int opt = bufsize; - return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (const char*)&opt, sizeof(opt)); -} - -extern int adb_socketpair( int sv[2] ); - -static __inline__ char* adb_dirstart( const char* path ) -{ - char* p = strchr(path, '/'); - char* p2 = strchr(path, '\\'); - - if ( !p ) - p = p2; - else if ( p2 && p2 > p ) - p = p2; - - return p; -} - -static __inline__ char* adb_dirstop( const char* path ) -{ - char* p = strrchr(path, '/'); - char* p2 = strrchr(path, '\\'); - - if ( !p ) - p = p2; - else if ( p2 && p2 > p ) - p = p2; - - return p; -} - -static __inline__ int adb_is_absolute_host_path( const char* path ) -{ - return isalpha(path[0]) && path[1] == ':' && path[2] == '\\'; -} - -#ifdef __cplusplus -} -#endif - -#else /* !_WIN32 a.k.a. Unix */ - -#include "fdevent.h" -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#define OS_PATH_SEPARATOR '/' -#define OS_PATH_SEPARATOR_STR "/" - -typedef pthread_mutex_t adb_mutex_t; - -#define ADB_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER -#define adb_mutex_init pthread_mutex_init -#define adb_mutex_lock pthread_mutex_lock -#define adb_mutex_unlock pthread_mutex_unlock -#define adb_mutex_destroy pthread_mutex_destroy - -#define ADB_MUTEX_DEFINE(m) adb_mutex_t m = PTHREAD_MUTEX_INITIALIZER - -#define adb_cond_t pthread_cond_t -#define adb_cond_init pthread_cond_init -#define adb_cond_wait pthread_cond_wait -#define adb_cond_broadcast pthread_cond_broadcast -#define adb_cond_signal pthread_cond_signal -#define adb_cond_destroy pthread_cond_destroy - -/* declare all mutexes */ -#define ADB_MUTEX(x) extern adb_mutex_t x; -#include "mutex_list.h" - -static __inline__ void close_on_exec(int fd) -{ - fcntl( fd, F_SETFD, FD_CLOEXEC ); -} - -static __inline__ int unix_open(const char* path, int options,...) -{ - if ((options & O_CREAT) == 0) - { - return open(path, options); - } - else - { - int mode; - va_list args; - va_start( args, options ); - mode = va_arg( args, int ); - va_end( args ); - return open(path, options, mode); - } -} - -static __inline__ int adb_open_mode( const char* pathname, int options, int mode ) -{ - return open( pathname, options, mode ); -} - -static __inline__ int adb_creat(const char* path, int mode) -{ - int fd = open(path, O_CREAT|O_WRONLY|O_TRUNC|O_NOFOLLOW, mode); - - if ( fd < 0 ) - return -1; - - close_on_exec(fd); - return fd; -} -#undef creat -#define creat ___xxx_creat - -static __inline__ int adb_open( const char* pathname, int options ) -{ - int fd = open( pathname, options ); - if (fd < 0) - return -1; - close_on_exec( fd ); - return fd; -} -#undef open -#define open ___xxx_open - -static __inline__ int adb_shutdown(int fd) -{ - return shutdown(fd, SHUT_RDWR); -} -#undef shutdown -#define shutdown ____xxx_shutdown - -static __inline__ int adb_close(int fd) -{ - return close(fd); -} -#undef close -#define close ____xxx_close - - -static __inline__ int adb_read(int fd, void* buf, size_t len) -{ - return read(fd, buf, len); -} - -#undef read -#define read ___xxx_read - -static __inline__ int adb_write(int fd, const void* buf, size_t len) -{ - return write(fd, buf, len); -} -#undef write -#define write ___xxx_write - -static __inline__ int adb_lseek(int fd, int pos, int where) -{ - return lseek(fd, pos, where); -} -#undef lseek -#define lseek ___xxx_lseek - -static __inline__ int adb_unlink(const char* path) -{ - return unlink(path); -} -#undef unlink -#define unlink ___xxx_unlink - -static __inline__ int adb_socket_accept(int serverfd, struct sockaddr* addr, socklen_t *addrlen) -{ - int fd; - - fd = accept(serverfd, addr, addrlen); - if (fd >= 0) - close_on_exec(fd); - - return fd; -} - -#undef accept -#define accept ___xxx_accept - -#define unix_read adb_read -#define unix_write adb_write -#define unix_close adb_close - -typedef pthread_t adb_thread_t; - -typedef void* (*adb_thread_func_t)( void* arg ); - -static __inline__ int adb_thread_create( adb_thread_t *pthread, adb_thread_func_t start, void* arg ) -{ - pthread_attr_t attr; - - pthread_attr_init (&attr); - pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); - - return pthread_create( pthread, &attr, start, arg ); -} - -static __inline__ int adb_socket_setbufsize( int fd, int bufsize ) -{ - int opt = bufsize; - return setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)); -} - -static __inline__ void disable_tcp_nagle(int fd) -{ - int on = 1; - setsockopt( fd, IPPROTO_TCP, TCP_NODELAY, (void*)&on, sizeof(on) ); -} - - -static __inline__ int unix_socketpair( int d, int type, int protocol, int sv[2] ) -{ - return socketpair( d, type, protocol, sv ); -} - -static __inline__ int adb_socketpair( int sv[2] ) -{ - int rc; - - rc = unix_socketpair( AF_UNIX, SOCK_STREAM, 0, sv ); - if (rc < 0) - return -1; - - close_on_exec( sv[0] ); - close_on_exec( sv[1] ); - return 0; -} - -#undef socketpair -#define socketpair ___xxx_socketpair - -static __inline__ void adb_sleep_ms( int mseconds ) -{ - usleep( mseconds*1000 ); -} - -static __inline__ int adb_mkdir(const char* path, int mode) -{ - return mkdir(path, mode); -} -#undef mkdir -#define mkdir ___xxx_mkdir - -static __inline__ void adb_sysdeps_init(void) -{ -} - -static __inline__ char* adb_dirstart(const char* path) -{ - return strchr(path, '/'); -} - -static __inline__ char* adb_dirstop(const char* path) -{ - return strrchr(path, '/'); -} - -static __inline__ int adb_is_absolute_host_path( const char* path ) -{ - return path[0] == '/'; -} - -#endif /* !_WIN32 */ - -#ifdef __cplusplus -} -#endif - -#endif /* _ADB_SYSDEPS_H */ diff --git a/minadbd/transport.h b/minadbd/transport.h deleted file mode 100644 index c1b8ff34f..000000000 --- a/minadbd/transport.h +++ /dev/null @@ -1,35 +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. - */ - -#ifndef __TRANSPORT_H -#define __TRANSPORT_H - -#ifdef __cplusplus -extern "C" { -#endif - -/* convenience wrappers around read/write that will retry on -** EINTR and/or short read/write. Returns 0 on success, -1 -** on error or EOF. -*/ -int readx(int fd, void *ptr, size_t len); -int writex(int fd, const void *ptr, size_t len); - -#ifdef __cplusplus -} -#endif - -#endif /* __TRANSPORT_H */ -- cgit v1.2.3 From 3efa676fa69c55f4d71b44d4f09e03cdf7a6fff2 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Wed, 18 Feb 2015 16:32:41 -0800 Subject: Remove dead code. This code doesn't exist in the normal adb, so it just makes it harder to diff the two. Change-Id: Ibb21b49bb9944c4245199536cbe88e8a107cf00d --- minadbd/services.c | 38 -------------------------------------- 1 file changed, 38 deletions(-) diff --git a/minadbd/services.c b/minadbd/services.c index 2a52eebae..357c222b4 100644 --- a/minadbd/services.c +++ b/minadbd/services.c @@ -61,40 +61,6 @@ static void sideload_host_service(int sfd, void* cookie) exit(result == 0 ? 0 : 1); } -#if 0 -static void echo_service(int fd, void *cookie) -{ - char buf[4096]; - int r; - char *p; - int c; - - for(;;) { - r = read(fd, buf, 4096); - if(r == 0) goto done; - if(r < 0) { - if(errno == EINTR) continue; - else goto done; - } - - c = r; - p = buf; - while(c > 0) { - r = write(fd, p, c); - if(r > 0) { - c -= r; - p += r; - continue; - } - if((r < 0) && (errno == EINTR)) continue; - goto done; - } - } -done: - close(fd); -} -#endif - static int create_service_thread(void (*func)(int, void *), void *cookie) { stinfo *sti; @@ -135,10 +101,6 @@ int service_to_fd(const char *name) exit(3); } else if (!strncmp(name, "sideload-host:", 14)) { ret = create_service_thread(sideload_host_service, (void*)(name + 14)); -#if 0 - } else if(!strncmp(name, "echo:", 5)){ - ret = create_service_thread(echo_service, 0); -#endif } if (ret >= 0) { close_on_exec(ret); -- cgit v1.2.3 From 9a894b7ece585e4254eb7879508bccd53802e6d7 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Wed, 18 Feb 2015 18:36:08 -0800 Subject: Move adb_main to its own file. Change-Id: Ia5150e1ef8be5a8f2d2da0fdca9383e22218f4ac --- minadbd/Android.mk | 1 + minadbd/adb.c | 30 ------------------------------ minadbd/adb_main.c | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 30 deletions(-) create mode 100644 minadbd/adb_main.c diff --git a/minadbd/Android.mk b/minadbd/Android.mk index fecce8777..0cba0c5f1 100644 --- a/minadbd/Android.mk +++ b/minadbd/Android.mk @@ -12,6 +12,7 @@ include $(CLEAR_VARS) LOCAL_SRC_FILES := \ adb.c \ + adb_main.c \ fuse_adb_provider.c \ sockets.c \ services.c \ diff --git a/minadbd/adb.c b/minadbd/adb.c index 0ac16e4d9..5f9cfdfa1 100644 --- a/minadbd/adb.c +++ b/minadbd/adb.c @@ -370,33 +370,3 @@ void handle_packet(apacket *p, atransport *t) put_apacket(p); } - -static void adb_cleanup(void) -{ - usb_cleanup(); -} - -int adb_main() -{ - atexit(adb_cleanup); -#if !defined(_WIN32) - // No SIGCHLD. Let the service subproc handle its children. - signal(SIGPIPE, SIG_IGN); -#endif - - init_transport_registration(); - - // The minimal version of adbd only uses USB. - if (access(USB_ADB_PATH, F_OK) == 0 || access(USB_FFS_ADB_EP0, F_OK) == 0) { - // listen on USB - usb_init(); - } - - D("Event loop starting\n"); - - fdevent_loop(); - - usb_cleanup(); - - return 0; -} diff --git a/minadbd/adb_main.c b/minadbd/adb_main.c new file mode 100644 index 000000000..66d270261 --- /dev/null +++ b/minadbd/adb_main.c @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2015 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 +#include +#include +#include + +#define TRACE_TAG TRACE_ADB + +#include "adb.h" +#include "sysdeps.h" + +int adb_main() +{ + atexit(usb_cleanup); + + // No SIGCHLD. Let the service subproc handle its children. + signal(SIGPIPE, SIG_IGN); + + init_transport_registration(); + usb_init(); + + D("Event loop starting\n"); + fdevent_loop(); + + return 0; +} -- cgit v1.2.3 From f3a5726919ce35d3f5e628e9561496470195ac1f Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Thu, 19 Feb 2015 13:21:14 -0800 Subject: Kill of most of the remainder of minadbd. I think everything left now is here to stay (services.c might get massaged in to libadbd if it gets refactored). Bug: 17626262 Change-Id: I01faf8b277a601a40e3a0f4c3b8206c97f1d2ce6 --- Android.mk | 1 - minadbd/Android.mk | 3 +- minadbd/README.txt | 37 +-- minadbd/adb.c | 372 --------------------------- minadbd/adb.h | 433 ------------------------------- minadbd/adb_main.c | 4 +- minadbd/sockets.c | 731 ----------------------------------------------------- recovery.cpp | 4 +- 8 files changed, 9 insertions(+), 1576 deletions(-) delete mode 100644 minadbd/adb.c delete mode 100644 minadbd/adb.h delete mode 100644 minadbd/sockets.c diff --git a/Android.mk b/Android.mk index b8ef63e99..9fd3f866a 100644 --- a/Android.mk +++ b/Android.mk @@ -67,7 +67,6 @@ LOCAL_STATIC_LIBRARIES := \ libmtdutils \ libmincrypt \ libminadbd \ - libadbd \ libfusesideload \ libminui \ libpng \ diff --git a/minadbd/Android.mk b/minadbd/Android.mk index 0cba0c5f1..f4b060b47 100644 --- a/minadbd/Android.mk +++ b/minadbd/Android.mk @@ -11,15 +11,14 @@ LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_SRC_FILES := \ - adb.c \ adb_main.c \ fuse_adb_provider.c \ - sockets.c \ services.c \ LOCAL_CFLAGS := -O2 -g -DADB_HOST=0 -Wall -Wno-unused-parameter LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE LOCAL_C_INCLUDES := bootable/recovery system/core/adb +LOCAL_WHOLE_STATIC_LIBRARIES := libadbd LOCAL_MODULE := libminadbd diff --git a/minadbd/README.txt b/minadbd/README.txt index c9df484c3..e69dc87c6 100644 --- a/minadbd/README.txt +++ b/minadbd/README.txt @@ -1,39 +1,8 @@ -The contents of this directory are copied from system/core/adb, with -the following changes: +minadbd is now mostly built from libadbd. The fuse features are unique to +minadbd, and services.c has been modified as follows: -adb.c - - much support for host mode and non-linux OS's stripped out; this - version only runs as adbd on the device. - - always setuid/setgid's itself to the shell user - - only uses USB transport - - references to JDWP removed - - main() removed - - all ADB_HOST and win32 code removed - - removed listeners, logging code, background server (for host) - -adb.h - - minor changes to match adb.c changes - -sockets.c - - references to JDWP removed - - ADB_HOST code removed - -services.c - - all services except echo_service (which is commented out) removed + - all services removed - all host mode support removed - sideload_service() added; this is the only service supported. It receives a single blob of data, writes it to a fixed filename, and makes the process exit. - -Android.mk - - only builds in adbd mode; builds as static library instead of a - standalone executable. - -sysdeps.h - - changes adb_creat() to use O_NOFOLLOW - -transport.c - - removed ADB_HOST code - -transport_usb.c - - removed ADB_HOST code diff --git a/minadbd/adb.c b/minadbd/adb.c deleted file mode 100644 index 5f9cfdfa1..000000000 --- a/minadbd/adb.c +++ /dev/null @@ -1,372 +0,0 @@ -/* - * Copyright (C) 2007 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. - */ - -#define TRACE_TAG TRACE_ADB - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "sysdeps.h" -#include "adb.h" - -#include - -#if ADB_TRACE -ADB_MUTEX_DEFINE( D_lock ); -#endif - -int HOST = 0; - -static const char *adb_device_banner = "sideload"; - -void fatal(const char *fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - fprintf(stderr, "error: "); - vfprintf(stderr, fmt, ap); - fprintf(stderr, "\n"); - va_end(ap); - exit(-1); -} - -void fatal_errno(const char *fmt, ...) -{ - va_list ap; - va_start(ap, fmt); - fprintf(stderr, "error: %s: ", strerror(errno)); - vfprintf(stderr, fmt, ap); - fprintf(stderr, "\n"); - va_end(ap); - exit(-1); -} - -int adb_trace_mask; - -/* read a comma/space/colum/semi-column separated list of tags - * from the ADB_TRACE environment variable and build the trace - * mask from it. note that '1' and 'all' are special cases to - * enable all tracing - */ -void adb_trace_init(void) -{ - const char* p = getenv("ADB_TRACE"); - const char* q; - - static const struct { - const char* tag; - int flag; - } tags[] = { - { "1", 0 }, - { "all", 0 }, - { "adb", TRACE_ADB }, - { "sockets", TRACE_SOCKETS }, - { "packets", TRACE_PACKETS }, - { "rwx", TRACE_RWX }, - { "usb", TRACE_USB }, - { "sync", TRACE_SYNC }, - { "sysdeps", TRACE_SYSDEPS }, - { "transport", TRACE_TRANSPORT }, - { "jdwp", TRACE_JDWP }, - { "services", TRACE_SERVICES }, - { NULL, 0 } - }; - - if (p == NULL) - return; - - /* use a comma/column/semi-colum/space separated list */ - while (*p) { - int len, tagn; - - q = strpbrk(p, " ,:;"); - if (q == NULL) { - q = p + strlen(p); - } - len = q - p; - - for (tagn = 0; tags[tagn].tag != NULL; tagn++) - { - int taglen = strlen(tags[tagn].tag); - - if (len == taglen && !memcmp(tags[tagn].tag, p, len) ) - { - int flag = tags[tagn].flag; - if (flag == 0) { - adb_trace_mask = ~0; - return; - } - adb_trace_mask |= (1 << flag); - break; - } - } - p = q; - if (*p) - p++; - } -} - - -apacket *get_apacket(void) -{ - apacket *p = malloc(sizeof(apacket)); - if(p == 0) fatal("failed to allocate an apacket"); - memset(p, 0, sizeof(apacket) - MAX_PAYLOAD); - return p; -} - -void put_apacket(apacket *p) -{ - free(p); -} - -void handle_online(void) -{ - D("adb: online\n"); -} - -void handle_offline(atransport *t) -{ - D("adb: offline\n"); - //Close the associated usb - run_transport_disconnects(t); -} - -#if TRACE_PACKETS -#define DUMPMAX 32 -void print_packet(const char *label, apacket *p) -{ - char *tag; - char *x; - unsigned count; - - switch(p->msg.command){ - case A_SYNC: tag = "SYNC"; break; - case A_CNXN: tag = "CNXN" ; break; - case A_OPEN: tag = "OPEN"; break; - case A_OKAY: tag = "OKAY"; break; - case A_CLSE: tag = "CLSE"; break; - case A_WRTE: tag = "WRTE"; break; - default: tag = "????"; break; - } - - fprintf(stderr, "%s: %s %08x %08x %04x \"", - label, tag, p->msg.arg0, p->msg.arg1, p->msg.data_length); - count = p->msg.data_length; - x = (char*) p->data; - if(count > DUMPMAX) { - count = DUMPMAX; - tag = "\n"; - } else { - tag = "\"\n"; - } - while(count-- > 0){ - if((*x >= ' ') && (*x < 127)) { - fputc(*x, stderr); - } else { - fputc('.', stderr); - } - x++; - } - fprintf(stderr, tag); -} -#endif - -static void send_ready(unsigned local, unsigned remote, atransport *t) -{ - D("Calling send_ready \n"); - apacket *p = get_apacket(); - p->msg.command = A_OKAY; - p->msg.arg0 = local; - p->msg.arg1 = remote; - send_packet(p, t); -} - -static void send_close(unsigned local, unsigned remote, atransport *t) -{ - D("Calling send_close \n"); - apacket *p = get_apacket(); - p->msg.command = A_CLSE; - p->msg.arg0 = local; - p->msg.arg1 = remote; - send_packet(p, t); -} - -static void send_connect(atransport *t) -{ - D("Calling send_connect \n"); - apacket *cp = get_apacket(); - cp->msg.command = A_CNXN; - cp->msg.arg0 = A_VERSION; - cp->msg.arg1 = MAX_PAYLOAD; - snprintf((char*) cp->data, sizeof cp->data, "%s::", - HOST ? "host" : adb_device_banner); - cp->msg.data_length = strlen((char*) cp->data) + 1; - send_packet(cp, t); -} - -void parse_banner(char *banner, atransport *t) -{ - char *type, *product, *end; - - D("parse_banner: %s\n", banner); - type = banner; - product = strchr(type, ':'); - if(product) { - *product++ = 0; - } else { - product = ""; - } - - /* remove trailing ':' */ - end = strchr(product, ':'); - if(end) *end = 0; - - /* save product name in device structure */ - if (t->product == NULL) { - t->product = strdup(product); - } else if (strcmp(product, t->product) != 0) { - free(t->product); - t->product = strdup(product); - } - - if(!strcmp(type, "bootloader")){ - D("setting connection_state to CS_BOOTLOADER\n"); - t->connection_state = CS_BOOTLOADER; - update_transports(); - return; - } - - if(!strcmp(type, "device")) { - D("setting connection_state to CS_DEVICE\n"); - t->connection_state = CS_DEVICE; - update_transports(); - return; - } - - if(!strcmp(type, "recovery")) { - D("setting connection_state to CS_RECOVERY\n"); - t->connection_state = CS_RECOVERY; - update_transports(); - return; - } - - if(!strcmp(type, "sideload")) { - D("setting connection_state to CS_SIDELOAD\n"); - t->connection_state = CS_SIDELOAD; - update_transports(); - return; - } - - t->connection_state = CS_HOST; -} - -void handle_packet(apacket *p, atransport *t) -{ - asocket *s; - - D("handle_packet() %c%c%c%c\n", ((char*) (&(p->msg.command)))[0], - ((char*) (&(p->msg.command)))[1], - ((char*) (&(p->msg.command)))[2], - ((char*) (&(p->msg.command)))[3]); - print_packet("recv", p); - - switch(p->msg.command){ - case A_SYNC: - if(p->msg.arg0){ - send_packet(p, t); - if(HOST) send_connect(t); - } else { - t->connection_state = CS_OFFLINE; - handle_offline(t); - send_packet(p, t); - } - return; - - case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */ - /* XXX verify version, etc */ - if(t->connection_state != CS_OFFLINE) { - t->connection_state = CS_OFFLINE; - handle_offline(t); - } - parse_banner((char*) p->data, t); - handle_online(); - if(!HOST) send_connect(t); - break; - - case A_OPEN: /* OPEN(local-id, 0, "destination") */ - if(t->connection_state != CS_OFFLINE) { - char *name = (char*) p->data; - name[p->msg.data_length > 0 ? p->msg.data_length - 1 : 0] = 0; - s = create_local_service_socket(name); - if(s == 0) { - send_close(0, p->msg.arg0, t); - } else { - s->peer = create_remote_socket(p->msg.arg0, t); - s->peer->peer = s; - send_ready(s->id, s->peer->id, t); - s->ready(s); - } - } - break; - - case A_OKAY: /* READY(local-id, remote-id, "") */ - if(t->connection_state != CS_OFFLINE) { - if((s = find_local_socket(p->msg.arg1))) { - if(s->peer == 0) { - s->peer = create_remote_socket(p->msg.arg0, t); - s->peer->peer = s; - } - s->ready(s); - } - } - break; - - case A_CLSE: /* CLOSE(local-id, remote-id, "") */ - if(t->connection_state != CS_OFFLINE) { - if((s = find_local_socket(p->msg.arg1))) { - s->close(s); - } - } - break; - - case A_WRTE: - if(t->connection_state != CS_OFFLINE) { - if((s = find_local_socket(p->msg.arg1))) { - unsigned rid = p->msg.arg0; - p->len = p->msg.data_length; - - if(s->enqueue(s, p) == 0) { - D("Enqueue the socket\n"); - send_ready(s->id, rid, t); - } - return; - } - } - break; - - default: - printf("handle_packet: what is %08x?!\n", p->msg.command); - } - - put_apacket(p); -} diff --git a/minadbd/adb.h b/minadbd/adb.h deleted file mode 100644 index 58818a996..000000000 --- a/minadbd/adb.h +++ /dev/null @@ -1,433 +0,0 @@ -/* - * Copyright (C) 2007 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 __ADB_H -#define __ADB_H - -#include - -#include "transport.h" /* readx(), writex() */ -#include "fdevent.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define MAX_PAYLOAD 4096 - -#define A_SYNC 0x434e5953 -#define A_CNXN 0x4e584e43 -#define A_OPEN 0x4e45504f -#define A_OKAY 0x59414b4f -#define A_CLSE 0x45534c43 -#define A_WRTE 0x45545257 - -#define A_VERSION 0x01000000 // ADB protocol version - -#define ADB_VERSION_MAJOR 1 // Used for help/version information -#define ADB_VERSION_MINOR 0 // Used for help/version information - -#define ADB_SERVER_VERSION 29 // Increment this when we want to force users to start a new adb server - -typedef struct amessage amessage; -typedef struct apacket apacket; -typedef struct asocket asocket; -typedef struct aservice aservice; -typedef struct atransport atransport; -typedef struct adisconnect adisconnect; -typedef struct usb_handle usb_handle; - -struct amessage { - unsigned command; /* command identifier constant */ - unsigned arg0; /* first argument */ - unsigned arg1; /* second argument */ - unsigned data_length; /* length of payload (0 is allowed) */ - unsigned data_check; /* checksum of data payload */ - unsigned magic; /* command ^ 0xffffffff */ -}; - -struct apacket -{ - apacket *next; - - unsigned len; - unsigned char *ptr; - - amessage msg; - unsigned char data[MAX_PAYLOAD]; -}; - -/* An asocket represents one half of a connection between a local and -** remote entity. A local asocket is bound to a file descriptor. A -** remote asocket is bound to the protocol engine. -*/ -struct asocket { - /* chain pointers for the local/remote list of - ** asockets that this asocket lives in - */ - asocket *next; - asocket *prev; - - /* the unique identifier for this asocket - */ - unsigned id; - - /* flag: set when the socket's peer has closed - ** but packets are still queued for delivery - */ - int closing; - - /* the asocket we are connected to - */ - - asocket *peer; - - /* For local asockets, the fde is used to bind - ** us to our fd event system. For remote asockets - ** these fields are not used. - */ - fdevent fde; - int fd; - - /* queue of apackets waiting to be written - */ - apacket *pkt_first; - apacket *pkt_last; - - /* enqueue is called by our peer when it has data - ** for us. It should return 0 if we can accept more - ** data or 1 if not. If we return 1, we must call - ** peer->ready() when we once again are ready to - ** receive data. - */ - int (*enqueue)(asocket *s, apacket *pkt); - - /* ready is called by the peer when it is ready for - ** us to send data via enqueue again - */ - void (*ready)(asocket *s); - - /* close is called by the peer when it has gone away. - ** we are not allowed to make any further calls on the - ** peer once our close method is called. - */ - void (*close)(asocket *s); - - /* socket-type-specific extradata */ - void *extra; - - /* A socket is bound to atransport */ - atransport *transport; -}; - - -/* the adisconnect structure is used to record a callback that -** will be called whenever a transport is disconnected (e.g. by the user) -** this should be used to cleanup objects that depend on the -** transport (e.g. remote sockets, etc...) -*/ -struct adisconnect -{ - void (*func)(void* opaque, atransport* t); - void* opaque; - adisconnect* next; - adisconnect* prev; -}; - - -/* a transport object models the connection to a remote device or emulator -** there is one transport per connected device/emulator. a "local transport" -** connects through TCP (for the emulator), while a "usb transport" through -** USB (for real devices) -** -** note that kTransportHost doesn't really correspond to a real transport -** object, it's a special value used to indicate that a client wants to -** connect to a service implemented within the ADB server itself. -*/ -typedef enum transport_type { - kTransportUsb, - kTransportLocal, - kTransportAny, - kTransportHost, -} transport_type; - -struct atransport -{ - atransport *next; - atransport *prev; - - int (*read_from_remote)(apacket *p, atransport *t); - int (*write_to_remote)(apacket *p, atransport *t); - void (*close)(atransport *t); - void (*kick)(atransport *t); - - int fd; - int transport_socket; - fdevent transport_fde; - int ref_count; - unsigned sync_token; - int connection_state; - transport_type type; - - /* usb handle or socket fd as needed */ - usb_handle *usb; - int sfd; - - /* used to identify transports for clients */ - char *serial; - char *product; - int adb_port; // Use for emulators (local transport) - - /* a list of adisconnect callbacks called when the transport is kicked */ - int kicked; - adisconnect disconnects; -}; - - -void print_packet(const char *label, apacket *p); - -asocket *find_local_socket(unsigned id); -void install_local_socket(asocket *s); -void remove_socket(asocket *s); -void close_all_sockets(atransport *t); - -#define LOCAL_CLIENT_PREFIX "emulator-" - -asocket *create_local_socket(int fd); -asocket *create_local_service_socket(const char *destination); - -asocket *create_remote_socket(unsigned id, atransport *t); -void connect_to_remote(asocket *s, const char *destination); -void connect_to_smartsocket(asocket *s); - -void fatal(const char *fmt, ...); -void fatal_errno(const char *fmt, ...); - -void handle_packet(apacket *p, atransport *t); -void send_packet(apacket *p, atransport *t); - -void get_my_path(char *s, size_t maxLen); -int launch_server(int server_port); -int adb_main(); - - -/* transports are ref-counted -** get_device_transport does an acquire on your behalf before returning -*/ -void init_transport_registration(void); -int list_transports(char *buf, size_t bufsize); -void update_transports(void); - -asocket* create_device_tracker(void); - -/* Obtain a transport from the available transports. -** If state is != CS_ANY, only transports in that state are considered. -** If serial is non-NULL then only the device with that serial will be chosen. -** If no suitable transport is found, error is set. -*/ -atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char **error_out); -void add_transport_disconnect( atransport* t, adisconnect* dis ); -void remove_transport_disconnect( atransport* t, adisconnect* dis ); -void run_transport_disconnects( atransport* t ); -void kick_transport( atransport* t ); - -/* initialize a transport object's func pointers and state */ -#if ADB_HOST -int get_available_local_transport_index(); -#endif -void init_usb_transport(atransport *t, usb_handle *usb, int state); - -/* for MacOS X cleanup */ -void close_usb_devices(); - -/* these should only be used for the "adb disconnect" command */ -void unregister_transport(atransport *t); -void unregister_all_tcp_transports(); - -void register_usb_transport(usb_handle *h, const char *serial, - const char* dev_path, unsigned writeable); - -/* this should only be used for transports with connection_state == CS_NOPERM */ -void unregister_usb_transport(usb_handle *usb); - -atransport *find_transport(const char *serial); -#if ADB_HOST -atransport* find_emulator_transport_by_adb_port(int adb_port); -#endif - -int service_to_fd(const char *name); -#if ADB_HOST -asocket *host_service_to_socket(const char* name, const char *serial); -#endif - -#if !ADB_HOST -typedef enum { - BACKUP, - RESTORE -} BackupOperation; -int backup_service(BackupOperation operation, char* args); -void framebuffer_service(int fd, void *cookie); -void log_service(int fd, void *cookie); -void remount_service(int fd, void *cookie); -char * get_log_file_path(const char * log_name); -#endif - -/* packet allocator */ -apacket *get_apacket(void); -void put_apacket(apacket *p); - -int check_header(apacket *p); -int check_data(apacket *p); - -/* define ADB_TRACE to 1 to enable tracing support, or 0 to disable it */ - -#define ADB_TRACE 1 - -/* IMPORTANT: if you change the following list, don't - * forget to update the corresponding 'tags' table in - * the adb_trace_init() function implemented in adb.c - */ -typedef enum { - TRACE_ADB = 0, /* 0x001 */ - TRACE_SOCKETS, - TRACE_PACKETS, - TRACE_TRANSPORT, - TRACE_RWX, /* 0x010 */ - TRACE_USB, - TRACE_SYNC, - TRACE_SYSDEPS, - TRACE_JDWP, /* 0x100 */ - TRACE_SERVICES, -} AdbTrace; - -#if ADB_TRACE - - extern int adb_trace_mask; - extern unsigned char adb_trace_output_count; - void adb_trace_init(void); - -# define ADB_TRACING ((adb_trace_mask & (1 << TRACE_TAG)) != 0) - - /* you must define TRACE_TAG before using this macro */ -# define D(...) \ - do { \ - if (ADB_TRACING) { \ - int save_errno = errno; \ - adb_mutex_lock(&D_lock); \ - fprintf(stderr, "%s::%s():", \ - __FILE__, __FUNCTION__); \ - errno = save_errno; \ - fprintf(stderr, __VA_ARGS__ ); \ - fflush(stderr); \ - adb_mutex_unlock(&D_lock); \ - errno = save_errno; \ - } \ - } while (0) -# define DR(...) \ - do { \ - if (ADB_TRACING) { \ - int save_errno = errno; \ - adb_mutex_lock(&D_lock); \ - errno = save_errno; \ - fprintf(stderr, __VA_ARGS__ ); \ - fflush(stderr); \ - adb_mutex_unlock(&D_lock); \ - errno = save_errno; \ - } \ - } while (0) -#else -# define D(...) ((void)0) -# define DR(...) ((void)0) -# define ADB_TRACING 0 -#endif - - -#if !TRACE_PACKETS -#define print_packet(tag,p) do {} while (0) -#endif - -#if ADB_HOST_ON_TARGET -/* adb and adbd are coexisting on the target, so use 5038 for adb - * to avoid conflicting with adbd's usage of 5037 - */ -# define DEFAULT_ADB_PORT 5038 -#else -# define DEFAULT_ADB_PORT 5037 -#endif - -#define DEFAULT_ADB_LOCAL_TRANSPORT_PORT 5555 - -#define ADB_CLASS 0xff -#define ADB_SUBCLASS 0x42 -#define ADB_PROTOCOL 0x1 - - -void local_init(int port); -int local_connect(int port); -int local_connect_arbitrary_ports(int console_port, int adb_port); - -/* usb host/client interface */ -void usb_init(); -void usb_cleanup(); -int usb_write(usb_handle *h, const void *data, int len); -int usb_read(usb_handle *h, void *data, int len); -int usb_close(usb_handle *h); -void usb_kick(usb_handle *h); - -/* used for USB device detection */ -#if ADB_HOST -int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_protocol); -#endif - -unsigned host_to_le32(unsigned n); -int adb_commandline(int argc, char **argv); - -int connection_state(atransport *t); - -#define CS_ANY -1 -#define CS_OFFLINE 0 -#define CS_BOOTLOADER 1 -#define CS_DEVICE 2 -#define CS_HOST 3 -#define CS_RECOVERY 4 -#define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */ -#define CS_SIDELOAD 6 -#define CS_UNAUTHORIZED 7 - -extern int HOST; -extern int SHELL_EXIT_NOTIFY_FD; - -#define CHUNK_SIZE (64*1024) - -#if !ADB_HOST -#define USB_ADB_PATH "/dev/android_adb" - -#define USB_FFS_ADB_PATH "/dev/usb-ffs/adb/" -#define USB_FFS_ADB_EP(x) USB_FFS_ADB_PATH#x - -#define USB_FFS_ADB_EP0 USB_FFS_ADB_EP(ep0) -#define USB_FFS_ADB_OUT USB_FFS_ADB_EP(ep1) -#define USB_FFS_ADB_IN USB_FFS_ADB_EP(ep2) -#endif - -int sendfailmsg(int fd, const char *reason); -int handle_host_request(char *service, transport_type ttype, char* serial, int reply_fd, asocket *s); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/minadbd/adb_main.c b/minadbd/adb_main.c index 66d270261..a62881d88 100644 --- a/minadbd/adb_main.c +++ b/minadbd/adb_main.c @@ -24,10 +24,12 @@ #include "adb.h" #include "sysdeps.h" -int adb_main() +int adb_main(int is_daemon, int server_port) { atexit(usb_cleanup); + adb_device_banner = "sideload"; + // No SIGCHLD. Let the service subproc handle its children. signal(SIGPIPE, SIG_IGN); diff --git a/minadbd/sockets.c b/minadbd/sockets.c deleted file mode 100644 index 817410d13..000000000 --- a/minadbd/sockets.c +++ /dev/null @@ -1,731 +0,0 @@ -/* - * Copyright (C) 2007 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 -#include -#include -#include -#include -#include - -#include "sysdeps.h" - -#define TRACE_TAG TRACE_SOCKETS -#include "adb.h" - -ADB_MUTEX_DEFINE( socket_list_lock ); - -static void local_socket_close_locked(asocket *s); - -int sendfailmsg(int fd, const char *reason) -{ - char buf[9]; - int len; - len = strlen(reason); - if(len > 0xffff) len = 0xffff; - snprintf(buf, sizeof buf, "FAIL%04x", len); - if(writex(fd, buf, 8)) return -1; - return writex(fd, reason, len); -} - -//extern int online; - -static unsigned local_socket_next_id = 1; - -static asocket local_socket_list = { - .next = &local_socket_list, - .prev = &local_socket_list, -}; - -/* the the list of currently closing local sockets. -** these have no peer anymore, but still packets to -** write to their fd. -*/ -static asocket local_socket_closing_list = { - .next = &local_socket_closing_list, - .prev = &local_socket_closing_list, -}; - -asocket *find_local_socket(unsigned id) -{ - asocket *s; - asocket *result = NULL; - - adb_mutex_lock(&socket_list_lock); - for (s = local_socket_list.next; s != &local_socket_list; s = s->next) { - if (s->id == id) { - result = s; - break; - } - } - adb_mutex_unlock(&socket_list_lock); - - return result; -} - -static void -insert_local_socket(asocket* s, asocket* list) -{ - s->next = list; - s->prev = s->next->prev; - s->prev->next = s; - s->next->prev = s; -} - - -void install_local_socket(asocket *s) -{ - adb_mutex_lock(&socket_list_lock); - - s->id = local_socket_next_id++; - insert_local_socket(s, &local_socket_list); - - adb_mutex_unlock(&socket_list_lock); -} - -void remove_socket(asocket *s) -{ - // socket_list_lock should already be held - if (s->prev && s->next) - { - s->prev->next = s->next; - s->next->prev = s->prev; - s->next = 0; - s->prev = 0; - s->id = 0; - } -} - -void close_all_sockets(atransport *t) -{ - asocket *s; - - /* this is a little gross, but since s->close() *will* modify - ** the list out from under you, your options are limited. - */ - adb_mutex_lock(&socket_list_lock); -restart: - for(s = local_socket_list.next; s != &local_socket_list; s = s->next){ - if(s->transport == t || (s->peer && s->peer->transport == t)) { - local_socket_close_locked(s); - goto restart; - } - } - adb_mutex_unlock(&socket_list_lock); -} - -static int local_socket_enqueue(asocket *s, apacket *p) -{ - D("LS(%d): enqueue %d\n", s->id, p->len); - - p->ptr = p->data; - - /* if there is already data queue'd, we will receive - ** events when it's time to write. just add this to - ** the tail - */ - if(s->pkt_first) { - goto enqueue; - } - - /* write as much as we can, until we - ** would block or there is an error/eof - */ - while(p->len > 0) { - int r = adb_write(s->fd, p->ptr, p->len); - if(r > 0) { - p->len -= r; - p->ptr += r; - continue; - } - if((r == 0) || (errno != EAGAIN)) { - D( "LS(%d): not ready, errno=%d: %s\n", s->id, errno, strerror(errno) ); - s->close(s); - return 1; /* not ready (error) */ - } else { - break; - } - } - - if(p->len == 0) { - put_apacket(p); - return 0; /* ready for more data */ - } - -enqueue: - p->next = 0; - if(s->pkt_first) { - s->pkt_last->next = p; - } else { - s->pkt_first = p; - } - s->pkt_last = p; - - /* make sure we are notified when we can drain the queue */ - fdevent_add(&s->fde, FDE_WRITE); - - return 1; /* not ready (backlog) */ -} - -static void local_socket_ready(asocket *s) -{ - /* far side is ready for data, pay attention to - readable events */ - fdevent_add(&s->fde, FDE_READ); -// D("LS(%d): ready()\n", s->id); -} - -static void local_socket_close(asocket *s) -{ - adb_mutex_lock(&socket_list_lock); - local_socket_close_locked(s); - adb_mutex_unlock(&socket_list_lock); -} - -// be sure to hold the socket list lock when calling this -static void local_socket_destroy(asocket *s) -{ - apacket *p, *n; - D("LS(%d): destroying fde.fd=%d\n", s->id, s->fde.fd); - - /* IMPORTANT: the remove closes the fd - ** that belongs to this socket - */ - fdevent_remove(&s->fde); - - /* dispose of any unwritten data */ - for(p = s->pkt_first; p; p = n) { - D("LS(%d): discarding %d bytes\n", s->id, p->len); - n = p->next; - put_apacket(p); - } - remove_socket(s); - free(s); -} - - -static void local_socket_close_locked(asocket *s) -{ - D("entered. LS(%d) fd=%d\n", s->id, s->fd); - if(s->peer) { - D("LS(%d): closing peer. peer->id=%d peer->fd=%d\n", - s->id, s->peer->id, s->peer->fd); - s->peer->peer = 0; - // tweak to avoid deadlock - if (s->peer->close == local_socket_close) { - local_socket_close_locked(s->peer); - } else { - s->peer->close(s->peer); - } - s->peer = 0; - } - - /* If we are already closing, or if there are no - ** pending packets, destroy immediately - */ - if (s->closing || s->pkt_first == NULL) { - int id = s->id; - local_socket_destroy(s); - D("LS(%d): closed\n", id); - return; - } - - /* otherwise, put on the closing list - */ - D("LS(%d): closing\n", s->id); - s->closing = 1; - fdevent_del(&s->fde, FDE_READ); - remove_socket(s); - D("LS(%d): put on socket_closing_list fd=%d\n", s->id, s->fd); - insert_local_socket(s, &local_socket_closing_list); -} - -static void local_socket_event_func(int fd, unsigned ev, void *_s) -{ - asocket *s = _s; - - D("LS(%d): event_func(fd=%d(==%d), ev=%04x)\n", s->id, s->fd, fd, ev); - - /* put the FDE_WRITE processing before the FDE_READ - ** in order to simplify the code. - */ - if(ev & FDE_WRITE){ - apacket *p; - - while((p = s->pkt_first) != 0) { - while(p->len > 0) { - int r = adb_write(fd, p->ptr, p->len); - if(r > 0) { - p->ptr += r; - p->len -= r; - continue; - } - if(r < 0) { - /* returning here is ok because FDE_READ will - ** be processed in the next iteration loop - */ - if(errno == EAGAIN) return; - if(errno == EINTR) continue; - } - D(" closing after write because r=%d and errno is %d\n", r, errno); - s->close(s); - return; - } - - if(p->len == 0) { - s->pkt_first = p->next; - if(s->pkt_first == 0) s->pkt_last = 0; - put_apacket(p); - } - } - - /* if we sent the last packet of a closing socket, - ** we can now destroy it. - */ - if (s->closing) { - D(" closing because 'closing' is set after write\n"); - s->close(s); - return; - } - - /* no more packets queued, so we can ignore - ** writable events again and tell our peer - ** to resume writing - */ - fdevent_del(&s->fde, FDE_WRITE); - s->peer->ready(s->peer); - } - - - if(ev & FDE_READ){ - apacket *p = get_apacket(); - unsigned char *x = p->data; - size_t avail = MAX_PAYLOAD; - int r; - int is_eof = 0; - - while(avail > 0) { - r = adb_read(fd, x, 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; - continue; - } - if(r < 0) { - if(errno == EAGAIN) break; - if(errno == EINTR) continue; - } - - /* r = 0 or unhandled error */ - is_eof = 1; - break; - } - D("LS(%d): fd=%d post avail loop. r=%d is_eof=%d forced_eof=%d\n", - s->id, s->fd, r, is_eof, s->fde.force_eof); - if((avail == MAX_PAYLOAD) || (s->peer == 0)) { - put_apacket(p); - } else { - p->len = MAX_PAYLOAD - avail; - - r = s->peer->enqueue(s->peer, p); - D("LS(%d): fd=%d post peer->enqueue(). r=%d\n", s->id, s->fd, r); - - if(r < 0) { - /* error return means they closed us as a side-effect - ** and we must return immediately. - ** - ** note that if we still have buffered packets, the - ** socket will be placed on the closing socket list. - ** this handler function will be called again - ** to process FDE_WRITE events. - */ - return; - } - - if(r > 0) { - /* if the remote cannot accept further events, - ** we disable notification of READs. They'll - ** be enabled again when we get a call to ready() - */ - fdevent_del(&s->fde, FDE_READ); - } - } - /* Don't allow a forced eof if data is still there */ - if((s->fde.force_eof && !r) || is_eof) { - D(" closing because is_eof=%d r=%d s->fde.force_eof=%d\n", is_eof, r, s->fde.force_eof); - s->close(s); - } - } - - if(ev & FDE_ERROR){ - /* this should be caught be the next read or write - ** catching it here means we may skip the last few - ** bytes of readable data. - */ -// s->close(s); - D("LS(%d): FDE_ERROR (fd=%d)\n", s->id, s->fd); - - return; - } -} - -asocket *create_local_socket(int fd) -{ - asocket *s = calloc(1, sizeof(asocket)); - if (s == NULL) fatal("cannot allocate socket"); - s->fd = fd; - s->enqueue = local_socket_enqueue; - s->ready = local_socket_ready; - s->close = local_socket_close; - install_local_socket(s); - - fdevent_install(&s->fde, fd, local_socket_event_func, s); -/* fdevent_add(&s->fde, FDE_ERROR); */ - //fprintf(stderr, "Created local socket in create_local_socket \n"); - D("LS(%d): created (fd=%d)\n", s->id, s->fd); - return s; -} - -asocket *create_local_service_socket(const char *name) -{ - asocket *s; - int fd; - - fd = service_to_fd(name); - if(fd < 0) return 0; - - s = create_local_socket(fd); - D("LS(%d): bound to '%s' via %d\n", s->id, name, fd); - return s; -} - -/* a Remote socket is used to send/receive data to/from a given transport object -** it needs to be closed when the transport is forcibly destroyed by the user -*/ -typedef struct aremotesocket { - asocket socket; - adisconnect disconnect; -} aremotesocket; - -static int remote_socket_enqueue(asocket *s, apacket *p) -{ - D("entered remote_socket_enqueue RS(%d) WRITE fd=%d peer.fd=%d\n", - s->id, s->fd, s->peer->fd); - p->msg.command = A_WRTE; - p->msg.arg0 = s->peer->id; - p->msg.arg1 = s->id; - p->msg.data_length = p->len; - send_packet(p, s->transport); - return 1; -} - -static void remote_socket_ready(asocket *s) -{ - D("entered remote_socket_ready RS(%d) OKAY fd=%d peer.fd=%d\n", - s->id, s->fd, s->peer->fd); - apacket *p = get_apacket(); - p->msg.command = A_OKAY; - p->msg.arg0 = s->peer->id; - p->msg.arg1 = s->id; - send_packet(p, s->transport); -} - -static void remote_socket_close(asocket *s) -{ - D("entered remote_socket_close RS(%d) CLOSE fd=%d peer->fd=%d\n", - s->id, s->fd, s->peer?s->peer->fd:-1); - apacket *p = get_apacket(); - p->msg.command = A_CLSE; - if(s->peer) { - p->msg.arg0 = s->peer->id; - s->peer->peer = 0; - D("RS(%d) peer->close()ing peer->id=%d peer->fd=%d\n", - s->id, s->peer->id, s->peer->fd); - s->peer->close(s->peer); - } - p->msg.arg1 = s->id; - send_packet(p, s->transport); - D("RS(%d): closed\n", s->id); - remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect ); - free(s); -} - -static void remote_socket_disconnect(void* _s, atransport* t) -{ - asocket* s = _s; - asocket* peer = s->peer; - - D("remote_socket_disconnect RS(%d)\n", s->id); - if (peer) { - peer->peer = NULL; - peer->close(peer); - } - remove_transport_disconnect( s->transport, &((aremotesocket*)s)->disconnect ); - free(s); -} - -asocket *create_remote_socket(unsigned id, atransport *t) -{ - asocket *s = calloc(1, sizeof(aremotesocket)); - adisconnect* dis = &((aremotesocket*)s)->disconnect; - - if (s == NULL) fatal("cannot allocate socket"); - s->id = id; - s->enqueue = remote_socket_enqueue; - s->ready = remote_socket_ready; - s->close = remote_socket_close; - s->transport = t; - - dis->func = remote_socket_disconnect; - dis->opaque = s; - add_transport_disconnect( t, dis ); - D("RS(%d): created\n", s->id); - return s; -} - -void connect_to_remote(asocket *s, const char *destination) -{ - D("Connect_to_remote call RS(%d) fd=%d\n", s->id, s->fd); - apacket *p = get_apacket(); - int len = strlen(destination) + 1; - - if(len > (MAX_PAYLOAD-1)) { - fatal("destination oversized"); - } - - D("LS(%d): connect('%s')\n", s->id, destination); - p->msg.command = A_OPEN; - p->msg.arg0 = s->id; - p->msg.data_length = len; - strcpy((char*) p->data, destination); - send_packet(p, s->transport); -} - - -/* this is used by magic sockets to rig local sockets to - send the go-ahead message when they connect */ -static void local_socket_ready_notify(asocket *s) -{ - s->ready = local_socket_ready; - s->close = local_socket_close; - adb_write(s->fd, "OKAY", 4); - s->ready(s); -} - -/* this is used by magic sockets to rig local sockets to - send the failure message if they are closed before - connected (to avoid closing them without a status message) */ -static void local_socket_close_notify(asocket *s) -{ - s->ready = local_socket_ready; - s->close = local_socket_close; - sendfailmsg(s->fd, "closed"); - s->close(s); -} - -unsigned unhex(unsigned char *s, int len) -{ - unsigned n = 0, c; - - while(len-- > 0) { - switch((c = *s++)) { - case '0': case '1': case '2': - case '3': case '4': case '5': - case '6': case '7': case '8': - case '9': - c -= '0'; - break; - case 'a': case 'b': case 'c': - case 'd': case 'e': case 'f': - c = c - 'a' + 10; - break; - case 'A': case 'B': case 'C': - case 'D': case 'E': case 'F': - c = c - 'A' + 10; - break; - default: - return 0xffffffff; - } - - n = (n << 4) | c; - } - - return n; -} - -/* skip_host_serial return the position in a string - skipping over the 'serial' parameter in the ADB protocol, - where parameter string may be a host:port string containing - the protocol delimiter (colon). */ -char *skip_host_serial(char *service) { - char *first_colon, *serial_end; - - first_colon = strchr(service, ':'); - if (!first_colon) { - /* No colon in service string. */ - return NULL; - } - serial_end = first_colon; - if (isdigit(serial_end[1])) { - serial_end++; - while ((*serial_end) && isdigit(*serial_end)) { - serial_end++; - } - if ((*serial_end) != ':') { - // Something other than numbers was found, reset the end. - serial_end = first_colon; - } - } - return serial_end; -} - -static int smart_socket_enqueue(asocket *s, apacket *p) -{ - unsigned len; - - D("SS(%d): enqueue %d\n", s->id, p->len); - - if(s->pkt_first == 0) { - s->pkt_first = p; - s->pkt_last = p; - } else { - if((s->pkt_first->len + p->len) > MAX_PAYLOAD) { - D("SS(%d): overflow\n", s->id); - put_apacket(p); - goto fail; - } - - memcpy(s->pkt_first->data + s->pkt_first->len, - p->data, p->len); - s->pkt_first->len += p->len; - put_apacket(p); - - p = s->pkt_first; - } - - /* don't bother if we can't decode the length */ - if(p->len < 4) return 0; - - len = unhex(p->data, 4); - if((len < 1) || (len > 1024)) { - D("SS(%d): bad size (%d)\n", s->id, len); - goto fail; - } - - D("SS(%d): len is %d\n", s->id, len ); - /* can't do anything until we have the full header */ - if((len + 4) > p->len) { - D("SS(%d): waiting for %d more bytes\n", s->id, len+4 - p->len); - return 0; - } - - p->data[len + 4] = 0; - - D("SS(%d): '%s'\n", s->id, (char*) (p->data + 4)); - - if (s->transport == NULL) { - char* error_string = "unknown failure"; - s->transport = acquire_one_transport (CS_ANY, - kTransportAny, NULL, &error_string); - - if (s->transport == NULL) { - sendfailmsg(s->peer->fd, error_string); - goto fail; - } - } - - if(!(s->transport) || (s->transport->connection_state == CS_OFFLINE)) { - /* if there's no remote we fail the connection - ** right here and terminate it - */ - sendfailmsg(s->peer->fd, "device offline (x)"); - goto fail; - } - - - /* instrument our peer to pass the success or fail - ** message back once it connects or closes, then - ** detach from it, request the connection, and - ** tear down - */ - s->peer->ready = local_socket_ready_notify; - s->peer->close = local_socket_close_notify; - s->peer->peer = 0; - /* give him our transport and upref it */ - s->peer->transport = s->transport; - - connect_to_remote(s->peer, (char*) (p->data + 4)); - s->peer = 0; - s->close(s); - return 1; - -fail: - /* we're going to close our peer as a side-effect, so - ** return -1 to signal that state to the local socket - ** who is enqueueing against us - */ - s->close(s); - return -1; -} - -static void smart_socket_ready(asocket *s) -{ - D("SS(%d): ready\n", s->id); -} - -static void smart_socket_close(asocket *s) -{ - D("SS(%d): closed\n", s->id); - if(s->pkt_first){ - put_apacket(s->pkt_first); - } - if(s->peer) { - s->peer->peer = 0; - s->peer->close(s->peer); - s->peer = 0; - } - free(s); -} - -asocket *create_smart_socket(void (*action_cb)(asocket *s, const char *act)) -{ - D("Creating smart socket \n"); - asocket *s = calloc(1, sizeof(asocket)); - if (s == NULL) fatal("cannot allocate socket"); - s->enqueue = smart_socket_enqueue; - s->ready = smart_socket_ready; - s->close = smart_socket_close; - s->extra = action_cb; - - D("SS(%d): created %p\n", s->id, action_cb); - return s; -} - -void smart_socket_action(asocket *s, const char *act) -{ - -} - -void connect_to_smartsocket(asocket *s) -{ - D("Connecting to smart socket \n"); - asocket *ss = create_smart_socket(smart_socket_action); - s->peer = ss; - ss->peer = s; - s->ready(s); -} diff --git a/recovery.cpp b/recovery.cpp index d8756d7ce..2b5332e00 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -43,7 +43,7 @@ #include "device.h" #include "adb_install.h" extern "C" { -#include "minadbd/adb.h" +#include "adb.h" #include "fuse_sideload.h" #include "fuse_sdcard_provider.h" } @@ -841,7 +841,7 @@ main(int argc, char **argv) { // only way recovery should be run with this argument is when it // starts a copy of itself from the apply_from_adb() function. if (argc == 2 && strcmp(argv[1], "--adbd") == 0) { - adb_main(); + adb_main(0, DEFAULT_ADB_PORT); return 0; } -- cgit v1.2.3 From 3e700cff53c0fdd4de2a0f89ef7916f05a131351 Mon Sep 17 00:00:00 2001 From: Narayan Kamath Date: Mon, 23 Feb 2015 13:29:16 +0000 Subject: Delete unused functions from minzip. This is in preparation of replacing it with libziparchive and providing shim wrappers. bug: 19472796 Change-Id: I1f2fb59ee7a41434e794e4ed15b754aa2b74a11d --- minzip/Zip.c | 43 +------------------------------------------ minzip/Zip.h | 51 --------------------------------------------------- 2 files changed, 1 insertion(+), 93 deletions(-) diff --git a/minzip/Zip.c b/minzip/Zip.c index aec35e375..fb462f45c 100644 --- a/minzip/Zip.c +++ b/minzip/Zip.c @@ -484,7 +484,7 @@ const ZipEntry* mzFindZipEntry(const ZipArchive* pArchive, /* * Return true if the entry is a symbolic link. */ -bool mzIsZipEntrySymlink(const ZipEntry* pEntry) +static bool mzIsZipEntrySymlink(const ZipEntry* pEntry) { if ((pEntry->versionMadeBy & 0xff00) == CENVEM_UNIX) { return S_ISLNK(pEntry->externalFileAttributes >> 16); @@ -628,30 +628,6 @@ static bool crcProcessFunction(const unsigned char *data, int dataLen, return true; } -/* - * Check the CRC on this entry; return true if it is correct. - * May do other internal checks as well. - */ -bool mzIsZipEntryIntact(const ZipArchive *pArchive, const ZipEntry *pEntry) -{ - unsigned long crc; - bool ret; - - crc = crc32(0L, Z_NULL, 0); - ret = mzProcessZipEntryContents(pArchive, pEntry, crcProcessFunction, - (void *)&crc); - if (!ret) { - LOGE("Can't calculate CRC for entry\n"); - return false; - } - if (crc != (unsigned long)pEntry->crc32) { - LOGW("CRC for entry %.*s (0x%08lx) != expected (0x%08lx)\n", - pEntry->fileNameLen, pEntry->fileName, crc, pEntry->crc32); - return false; - } - return true; -} - typedef struct { char *buf; int bufLen; @@ -733,23 +709,6 @@ bool mzExtractZipEntryToFile(const ZipArchive *pArchive, return true; } -/* - * Obtain a pointer to the in-memory representation of a stored entry. - */ -bool mzGetStoredEntry(const ZipArchive *pArchive, - const ZipEntry *pEntry, unsigned char **addr, size_t *length) -{ - if (pEntry->compression != STORED) { - LOGE("Can't getStoredEntry for '%s'; not stored\n", - pEntry->fileName); - return false; - } - - *addr = pArchive->addr + pEntry->offset; - *length = pEntry->uncompLen; - return true; -} - typedef struct { unsigned char* buffer; long len; diff --git a/minzip/Zip.h b/minzip/Zip.h index 2054b38a4..54eab3222 100644 --- a/minzip/Zip.h +++ b/minzip/Zip.h @@ -92,49 +92,15 @@ INLINE unsigned int mzZipEntryCount(const ZipArchive* pArchive) { return pArchive->numEntries; } -/* - * Get an entry by index. Returns NULL if the index is out-of-bounds. - */ -INLINE const ZipEntry* -mzGetZipEntryAt(const ZipArchive* pArchive, unsigned int index) -{ - if (index < pArchive->numEntries) { - return pArchive->pEntries + index; - } - return NULL; -} - -/* - * Get the index number of an entry in the archive. - */ -INLINE unsigned int -mzGetZipEntryIndex(const ZipArchive *pArchive, const ZipEntry *pEntry) { - return pEntry - pArchive->pEntries; -} - -/* - * Simple accessors. - */ -INLINE UnterminatedString mzGetZipEntryFileName(const ZipEntry* pEntry) { - UnterminatedString ret; - ret.str = pEntry->fileName; - ret.len = pEntry->fileNameLen; - return ret; -} INLINE long mzGetZipEntryOffset(const ZipEntry* pEntry) { return pEntry->offset; } INLINE long mzGetZipEntryUncompLen(const ZipEntry* pEntry) { return pEntry->uncompLen; } -INLINE long mzGetZipEntryModTime(const ZipEntry* pEntry) { - return pEntry->modTime; -} INLINE long mzGetZipEntryCrc32(const ZipEntry* pEntry) { return pEntry->crc32; } -bool mzIsZipEntrySymlink(const ZipEntry* pEntry); - /* * Type definition for the callback function used by @@ -163,12 +129,6 @@ bool mzProcessZipEntryContents(const ZipArchive *pArchive, bool mzReadZipEntry(const ZipArchive* pArchive, const ZipEntry* pEntry, char* buf, int bufLen); -/* - * Check the CRC on this entry; return true if it is correct. - * May do other internal checks as well. - */ -bool mzIsZipEntryIntact(const ZipArchive *pArchive, const ZipEntry *pEntry); - /* * Inflate and write an entry to a file. */ @@ -182,17 +142,6 @@ bool mzExtractZipEntryToFile(const ZipArchive *pArchive, bool mzExtractZipEntryToBuffer(const ZipArchive *pArchive, const ZipEntry *pEntry, unsigned char* buffer); -/* - * Return a pointer and length for a given entry. The returned region - * should be valid until pArchive is closed, and should be treated as - * read-only. - * - * Only makes sense for entries which are stored (ie, not compressed). - * No guarantees are made regarding alignment of the returned pointer. - */ -bool mzGetStoredEntry(const ZipArchive *pArchive, - const ZipEntry* pEntry, unsigned char **addr, size_t *length); - /* * Inflate all entries under zipDir to the directory specified by * targetDir, which must exist and be a writable directory. -- cgit v1.2.3 From d84b055972ba910efdedad0c31a408bb115582f9 Mon Sep 17 00:00:00 2001 From: Narayan Kamath Date: Mon, 23 Feb 2015 13:38:08 +0000 Subject: Fix a printf format warning. warning: format '%lu' expects argument of type 'long unsigned int', but argument 3 has type 'unsigned int' [-Wformat] sizeof(RangeSet) + num * sizeof(int)); Change-Id: I4a3c6fc8d40c08ea84f8f5ee13f39350e4264027 --- updater/blockimg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/updater/blockimg.c b/updater/blockimg.c index c3319c973..42c12fd98 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -61,7 +61,7 @@ static RangeSet* parse_range(char* text) { RangeSet* out = malloc(sizeof(RangeSet) + num * sizeof(int)); if (out == NULL) { - fprintf(stderr, "failed to allocate range of %lu bytes\n", + fprintf(stderr, "failed to allocate range of %zu bytes\n", sizeof(RangeSet) + num * sizeof(int)); exit(1); } -- cgit v1.2.3 From 8f6eb5c045896d13b411ab8906f9af8e2f258b5a Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Tue, 24 Feb 2015 22:07:18 -0800 Subject: Fix build from implicit declaration. Turn the warning on by default and turn on -Werror so this doesn't happen next time. Change-Id: Id65bf0cb63bbf0ff224655b425463ae2f55435df --- minadbd/Android.mk | 22 +++++++++++----------- minadbd/adb_main.c | 4 +++- minadbd/fuse_adb_provider.c | 1 + minadbd/services.c | 14 ++++++++------ 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/minadbd/Android.mk b/minadbd/Android.mk index f4b060b47..dce004feb 100644 --- a/minadbd/Android.mk +++ b/minadbd/Android.mk @@ -1,22 +1,22 @@ # Copyright 2005 The Android Open Source Project -# -# Android.mk for adb -# LOCAL_PATH:= $(call my-dir) -# minadbd library -# ========================================================= - include $(CLEAR_VARS) LOCAL_SRC_FILES := \ - adb_main.c \ - fuse_adb_provider.c \ - services.c \ + adb_main.c \ + fuse_adb_provider.c \ + services.c \ + +LOCAL_CFLAGS := \ + -Wall -Werror \ + -Wno-unused-parameter \ + -Wimplicit-function-declaration \ + -D_GNU_SOURCE \ + -D_XOPEN_SOURCE \ + -DADB_HOST=0 \ -LOCAL_CFLAGS := -O2 -g -DADB_HOST=0 -Wall -Wno-unused-parameter -LOCAL_CFLAGS += -D_XOPEN_SOURCE -D_GNU_SOURCE LOCAL_C_INCLUDES := bootable/recovery system/core/adb LOCAL_WHOLE_STATIC_LIBRARIES := libadbd diff --git a/minadbd/adb_main.c b/minadbd/adb_main.c index a62881d88..f6e240108 100644 --- a/minadbd/adb_main.c +++ b/minadbd/adb_main.c @@ -21,9 +21,11 @@ #define TRACE_TAG TRACE_ADB -#include "adb.h" #include "sysdeps.h" +#include "adb.h" +#include "transport.h" + int adb_main(int is_daemon, int server_port) { atexit(usb_cleanup); diff --git a/minadbd/fuse_adb_provider.c b/minadbd/fuse_adb_provider.c index e9262858c..2386e82bb 100644 --- a/minadbd/fuse_adb_provider.c +++ b/minadbd/fuse_adb_provider.c @@ -21,6 +21,7 @@ #include "adb.h" #include "fuse_sideload.h" +#include "transport.h" struct adb_data { int sfd; // file descriptor for the adb channel diff --git a/minadbd/services.c b/minadbd/services.c index 357c222b4..7e419cc85 100644 --- a/minadbd/services.c +++ b/minadbd/services.c @@ -14,18 +14,19 @@ * limitations under the License. */ -#include +#include +#include #include -#include +#include #include -#include +#include #include "sysdeps.h" -#include "fdevent.h" -#include "fuse_adb_provider.h" #define TRACE_TAG TRACE_SERVICES #include "adb.h" +#include "fdevent.h" +#include "fuse_adb_provider.h" typedef struct stinfo stinfo; @@ -52,7 +53,8 @@ static void sideload_host_service(int sfd, void* cookie) s = adb_strtok_r(NULL, ":", &saveptr); uint32_t block_size = strtoul(s, NULL, 10); - printf("sideload-host file size %llu block size %lu\n", file_size, block_size); + printf("sideload-host file size %llu block size %" PRIu32 "\n", file_size, + block_size); int result = run_adb_fuse(sfd, file_size, block_size); -- cgit v1.2.3 From 24043705895d4ec170490d7d2761443d264f14a4 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Tue, 24 Feb 2015 22:41:07 -0800 Subject: Remove _(GNU|XOPEN)_SORUCE makefile cruft. None of the functions needing these are used. Change-Id: Ibe3ca24d993788bf2f1108bac8417a7094ef386b --- minadbd/Android.mk | 2 -- 1 file changed, 2 deletions(-) diff --git a/minadbd/Android.mk b/minadbd/Android.mk index dce004feb..24b043ee3 100644 --- a/minadbd/Android.mk +++ b/minadbd/Android.mk @@ -13,8 +13,6 @@ LOCAL_CFLAGS := \ -Wall -Werror \ -Wno-unused-parameter \ -Wimplicit-function-declaration \ - -D_GNU_SOURCE \ - -D_XOPEN_SOURCE \ -DADB_HOST=0 \ LOCAL_C_INCLUDES := bootable/recovery system/core/adb -- cgit v1.2.3 From 017db6d89e00183cea617dee7b2bc05c2098d643 Mon Sep 17 00:00:00 2001 From: Narayan Kamath Date: Wed, 25 Feb 2015 15:18:24 +0000 Subject: Fix LP64 build for minadbd. services.c:57:12: error: format '%llu' expects argument of type 'long long unsigned int', but argument 2 has type 'uint64_t' [-Werror=format=] Change-Id: Ieba691bf9e7a30c8bb38f4e1f36e86b6ea3f8c80 --- minadbd/services.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/minadbd/services.c b/minadbd/services.c index 7e419cc85..581d847fc 100644 --- a/minadbd/services.c +++ b/minadbd/services.c @@ -53,8 +53,8 @@ static void sideload_host_service(int sfd, void* cookie) s = adb_strtok_r(NULL, ":", &saveptr); uint32_t block_size = strtoul(s, NULL, 10); - printf("sideload-host file size %llu block size %" PRIu32 "\n", file_size, - block_size); + printf("sideload-host file size %" PRIu64 " block size %" PRIu32 "\n", + file_size, block_size); int result = run_adb_fuse(sfd, file_size, block_size); -- cgit v1.2.3 From 3e91f691a6bf6b154ea98982b073e84e86328baf Mon Sep 17 00:00:00 2001 From: Jesse Zhao Date: Thu, 8 Jan 2015 15:59:23 -0800 Subject: Bump up max_map_count value. Change-Id: Id3e2c0795b817db9a85bc84cba2aa05d20179d39 Bug: 18503789 (cherry picked from commit 5bf74b238b402eaaf8c5bd1663fe4d592e59421f) --- etc/init.rc | 1 + 1 file changed, 1 insertion(+) diff --git a/etc/init.rc b/etc/init.rc index 1b402e20d..8ff728097 100644 --- a/etc/init.rc +++ b/etc/init.rc @@ -30,6 +30,7 @@ on init chmod 0775 /tmp write /proc/sys/kernel/panic_on_oops 1 + write /proc/sys/vm/max_map_count 1000000 on fs mkdir /dev/usb-ffs 0770 shell shell -- cgit v1.2.3 From 432584fccc1455532d824c21ff578b3bfcef92ce Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Tue, 24 Feb 2015 22:48:25 -0800 Subject: Fix readx/writex names. Change-Id: I9556960b8293ea0e81def8b73f88edadb68841e3 --- minadbd/fuse_adb_provider.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/minadbd/fuse_adb_provider.c b/minadbd/fuse_adb_provider.c index 2386e82bb..300e3c748 100644 --- a/minadbd/fuse_adb_provider.c +++ b/minadbd/fuse_adb_provider.c @@ -19,9 +19,11 @@ #include #include +#include "sysdeps.h" + #include "adb.h" +#include "adb_io.h" #include "fuse_sideload.h" -#include "transport.h" struct adb_data { int sfd; // file descriptor for the adb channel @@ -35,12 +37,12 @@ static int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, uint32_ char buf[10]; snprintf(buf, sizeof(buf), "%08u", block); - if (writex(ad->sfd, buf, 8) < 0) { + if (!WriteStringFully(ad->sfd, buf)) { fprintf(stderr, "failed to write to adb host: %s\n", strerror(errno)); return -EIO; } - if (readx(ad->sfd, buffer, fetch_size) < 0) { + if (!WriteFdExactly(ad->sfd, buffer, fetch_size)) { fprintf(stderr, "failed to read from adb host: %s\n", strerror(errno)); return -EIO; } @@ -51,7 +53,7 @@ static int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, uint32_ static void close_adb(void* cookie) { struct adb_data* ad = (struct adb_data*)cookie; - writex(ad->sfd, "DONEDONE", 8); + WriteStringFully(ad->sfd, "DONEDONE"); } int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size) { -- cgit v1.2.3 From fee324f1b6a345c4306c71abad9f9640cddd1018 Mon Sep 17 00:00:00 2001 From: Trevor Drake Date: Thu, 26 Feb 2015 15:57:58 +0000 Subject: Drop hardcoded LOCAL_C_INCLUDES from minui/Android.mk The zlib include was not required. libpng is now handled by referencing the libpng static library Change-Id: Ie4e0abad3fff5b763eba363d3d0fa96128ff49bc --- minui/Android.mk | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/minui/Android.mk b/minui/Android.mk index ddee165f9..9b2e09b70 100644 --- a/minui/Android.mk +++ b/minui/Android.mk @@ -4,11 +4,8 @@ include $(CLEAR_VARS) LOCAL_SRC_FILES := graphics.c graphics_adf.c graphics_fbdev.c events.c \ resources.c -LOCAL_C_INCLUDES +=\ - external/libpng\ - external/zlib - LOCAL_WHOLE_STATIC_LIBRARIES += libadf +LOCAL_STATIC_LIBRARIES += libpng LOCAL_MODULE := libminui -- cgit v1.2.3 From 190977157bbb7118a9416614d5e9492114bc2fc5 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Thu, 26 Feb 2015 13:50:01 -0800 Subject: This read accidentally got turned in to a write. Too many mechanical changes in a row... Bug: 19522788 Change-Id: Ic451792aab2700cdbdbb64529b99ff5f567918ad --- minadbd/fuse_adb_provider.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minadbd/fuse_adb_provider.c b/minadbd/fuse_adb_provider.c index 300e3c748..0d710727d 100644 --- a/minadbd/fuse_adb_provider.c +++ b/minadbd/fuse_adb_provider.c @@ -42,7 +42,7 @@ static int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, uint32_ return -EIO; } - if (!WriteFdExactly(ad->sfd, buffer, fetch_size)) { + if (!ReadFdExactly(ad->sfd, buffer, fetch_size)) { fprintf(stderr, "failed to read from adb host: %s\n", strerror(errno)); return -EIO; } -- cgit v1.2.3 From ffd6c31a770618eff6354b65ba7352f4da116055 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Thu, 26 Feb 2015 15:33:00 -0800 Subject: Add tests for read_block_adb. These tests aren't completely representative in that they don't run in the recovery image. We might want to look in to adding a self-test option to the recovery UI. Until then, these can be run on a normal device (which is easier to do anyway). Bug: 19522788 Change-Id: Idb20feb55d10c62905c2480ab1b61a2e4b5f60d8 --- minadbd/Android.mk | 29 +++++++++---- minadbd/fuse_adb_provider.c | 11 ++--- minadbd/fuse_adb_provider.h | 11 +++++ minadbd/fuse_adb_provider_test.cpp | 89 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 16 deletions(-) create mode 100644 minadbd/fuse_adb_provider_test.cpp diff --git a/minadbd/Android.mk b/minadbd/Android.mk index 24b043ee3..79fe96b93 100644 --- a/minadbd/Android.mk +++ b/minadbd/Android.mk @@ -2,6 +2,12 @@ LOCAL_PATH:= $(call my-dir) +minadbd_cflags := \ + -Wall -Werror \ + -Wno-unused-parameter \ + -Wno-missing-field-initializers \ + -DADB_HOST=0 \ + include $(CLEAR_VARS) LOCAL_SRC_FILES := \ @@ -9,15 +15,22 @@ LOCAL_SRC_FILES := \ fuse_adb_provider.c \ services.c \ -LOCAL_CFLAGS := \ - -Wall -Werror \ - -Wno-unused-parameter \ - -Wimplicit-function-declaration \ - -DADB_HOST=0 \ - +LOCAL_MODULE := libminadbd +LOCAL_CFLAGS := $(minadbd_cflags) +LOCAL_CONLY_FLAGS := -Wimplicit-function-declaration LOCAL_C_INCLUDES := bootable/recovery system/core/adb LOCAL_WHOLE_STATIC_LIBRARIES := libadbd -LOCAL_MODULE := libminadbd - include $(BUILD_STATIC_LIBRARY) + +include $(CLEAR_VARS) + +LOCAL_CLANG := true +LOCAL_MODULE := minadbd_test +LOCAL_SRC_FILES := fuse_adb_provider_test.cpp +LOCAL_CFLAGS := $(minadbd_cflags) +LOCAL_C_INCLUDES := $(LOCAL_PATH) system/core/adb +LOCAL_STATIC_LIBRARIES := libminadbd +LOCAL_SHARED_LIBRARIES := liblog libutils + +include $(BUILD_NATIVE_TEST) diff --git a/minadbd/fuse_adb_provider.c b/minadbd/fuse_adb_provider.c index 0d710727d..5da7fd76c 100644 --- a/minadbd/fuse_adb_provider.c +++ b/minadbd/fuse_adb_provider.c @@ -23,16 +23,11 @@ #include "adb.h" #include "adb_io.h" +#include "fuse_adb_provider.h" #include "fuse_sideload.h" -struct adb_data { - int sfd; // file descriptor for the adb channel - - uint64_t file_size; - uint32_t block_size; -}; - -static int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size) { +int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, + uint32_t fetch_size) { struct adb_data* ad = (struct adb_data*)cookie; char buf[10]; diff --git a/minadbd/fuse_adb_provider.h b/minadbd/fuse_adb_provider.h index 23de44ab2..b88ce497b 100644 --- a/minadbd/fuse_adb_provider.h +++ b/minadbd/fuse_adb_provider.h @@ -17,10 +17,21 @@ #ifndef __FUSE_ADB_PROVIDER_H #define __FUSE_ADB_PROVIDER_H +#include + #ifdef __cplusplus extern "C" { #endif +struct adb_data { + int sfd; // file descriptor for the adb channel + + uint64_t file_size; + uint32_t block_size; +}; + +int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, + uint32_t fetch_size); int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size); #ifdef __cplusplus diff --git a/minadbd/fuse_adb_provider_test.cpp b/minadbd/fuse_adb_provider_test.cpp new file mode 100644 index 000000000..ecd9f384b --- /dev/null +++ b/minadbd/fuse_adb_provider_test.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2015 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 "fuse_adb_provider.h" + +#include + +#include +#include +#include + +#include + +#include "adb_io.h" +#include "utils/file.h" + +TEST(fuse_adb_provider, read_block_adb) { + adb_data data = {}; + int sockets[2]; + + ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sockets)); + data.sfd = sockets[0]; + + int host_socket = sockets[1]; + fcntl(host_socket, F_SETFL, O_NONBLOCK); + + const char expected_data[] = "foobar"; + char block_data[sizeof(expected_data)] = {}; + + // If we write the result of read_block_adb's request before the request is + // actually made we can avoid needing an extra thread for this test. + ASSERT_TRUE(WriteFdExactly(host_socket, expected_data, + strlen(expected_data))); + + uint32_t block = 1234U; + const char expected_block[] = "00001234"; + ASSERT_EQ(0, read_block_adb(reinterpret_cast(&data), block, + reinterpret_cast(block_data), + sizeof(expected_data) - 1)); + + // Check that read_block_adb requested the right block. + char block_req[sizeof(expected_block)] = {}; + ASSERT_TRUE(ReadFdExactly(host_socket, block_req, 8)); + ASSERT_EQ(0, block_req[8]); + ASSERT_EQ(8U, strlen(block_req)); + ASSERT_STREQ(expected_block, block_req); + + // Check that read_block_adb returned the right data. + ASSERT_EQ(0, block_req[8]); + ASSERT_STREQ(expected_data, block_data); + + // Check that nothing else was written to the socket. + char tmp; + errno = 0; + ASSERT_EQ(-1, read(host_socket, &tmp, 1)); + ASSERT_EQ(EWOULDBLOCK, errno); + + close(sockets[0]); + close(sockets[1]); +} + +TEST(fuse_adb_provider, read_block_adb_fail_write) { + adb_data data = {}; + int sockets[2]; + + ASSERT_EQ(0, socketpair(AF_UNIX, SOCK_STREAM, 0, sockets)); + data.sfd = sockets[0]; + + ASSERT_EQ(0, close(sockets[1])); + + char buf[1]; + ASSERT_EQ(-EIO, read_block_adb(reinterpret_cast(&data), 0, + reinterpret_cast(buf), 1)); + + close(sockets[0]); +} -- cgit v1.2.3 From 9c0f5d6b348e37533bdcccf1166d6cbf1ca5c50b Mon Sep 17 00:00:00 2001 From: Narayan Kamath Date: Mon, 23 Feb 2015 14:09:31 +0000 Subject: Remove more dead code from minzip. I've added explanatory comments to mzExtractRecursive because that function will live on as a utility even after we move the zip format related logic to libziparchive. bug: 19472796 (cherry-picked from commit c9ccdfd7a42de08c47ab771b94dc5b9d1f957b95) Change-Id: I8b7fb6fa3eafb2e7ac080ef7a7eceb691b252d8a --- minzip/Zip.c | 164 ++++++++++++++++++++---------------------------------- minzip/Zip.h | 13 ++--- updater/install.c | 2 +- 3 files changed, 65 insertions(+), 114 deletions(-) diff --git a/minzip/Zip.c b/minzip/Zip.c index 12e06f6d8..d3ff79be6 100644 --- a/minzip/Zip.c +++ b/minzip/Zip.c @@ -828,7 +828,7 @@ static const char *targetEntryPath(MzPathHelper *helper, ZipEntry *pEntry) */ bool mzExtractRecursive(const ZipArchive *pArchive, const char *zipDir, const char *targetDir, - int flags, const struct utimbuf *timestamp, + const struct utimbuf *timestamp, void (*callback)(const char *fn, void *), void *cookie, struct selabel_handle *sehnd) { @@ -932,30 +932,19 @@ bool mzExtractRecursive(const ZipArchive *pArchive, break; } - /* With DRY_RUN set, invoke the callback but don't do anything else. - */ - if (flags & MZ_EXTRACT_DRY_RUN) { - if (callback != NULL) callback(targetFile, cookie); - continue; - } - - /* Create the file or directory. - */ #define UNZIP_DIRMODE 0755 #define UNZIP_FILEMODE 0644 - if (pEntry->fileName[pEntry->fileNameLen-1] == '/') { - if (!(flags & MZ_EXTRACT_FILES_ONLY)) { - int ret = dirCreateHierarchy( - targetFile, UNZIP_DIRMODE, timestamp, false, sehnd); - if (ret != 0) { - LOGE("Can't create containing directory for \"%s\": %s\n", - targetFile, strerror(errno)); - ok = false; - break; - } - LOGD("Extracted dir \"%s\"\n", targetFile); - } - } else { + /* + * Create the file or directory. We ignore directory entries + * because we recursively create paths to each file entry we encounter + * in the zip archive anyway. + * + * NOTE: A "directory entry" in a zip archive is just a zero length + * entry that ends in a "/". They're not mandatory and many tools get + * rid of them. We need to process them only if we want to preserve + * empty directories from the archive. + */ + if (pEntry->fileName[pEntry->fileNameLen-1] != '/') { /* This is not a directory. First, make sure that * the containing directory exists. */ @@ -968,97 +957,62 @@ bool mzExtractRecursive(const ZipArchive *pArchive, break; } - /* With FILES_ONLY set, we need to ignore metadata entirely, - * so treat symlinks as regular files. + /* + * The entry is a regular file or a symlink. Open the target for writing. + * + * TODO: This behavior for symlinks seems rather bizarre. For a + * symlink foo/bar/baz -> foo/tar/taz, we will create a file called + * "foo/bar/baz" whose contents are the literal "foo/tar/taz". We + * warn about this for now and preserve older behavior. */ - if (!(flags & MZ_EXTRACT_FILES_ONLY) && mzIsZipEntrySymlink(pEntry)) { - /* The entry is a symbolic link. - * The relative target of the symlink is in the - * data section of this entry. - */ - if (pEntry->uncompLen == 0) { - LOGE("Symlink entry \"%s\" has no target\n", - targetFile); - ok = false; - break; - } - char *linkTarget = malloc(pEntry->uncompLen + 1); - if (linkTarget == NULL) { - ok = false; - break; - } - ok = mzReadZipEntry(pArchive, pEntry, linkTarget, - pEntry->uncompLen); - if (!ok) { - LOGE("Can't read symlink target for \"%s\"\n", - targetFile); - free(linkTarget); - break; - } - linkTarget[pEntry->uncompLen] = '\0'; - - /* Make the link. - */ - ret = symlink(linkTarget, targetFile); - if (ret != 0) { - LOGE("Can't symlink \"%s\" to \"%s\": %s\n", - targetFile, linkTarget, strerror(errno)); - free(linkTarget); - ok = false; - break; - } - LOGD("Extracted symlink \"%s\" -> \"%s\"\n", - targetFile, linkTarget); - free(linkTarget); - } else { - /* The entry is a regular file. - * Open the target for writing. - */ - - char *secontext = NULL; + if (mzIsZipEntrySymlink(pEntry)) { + LOGE("Symlink entry \"%.*s\" will be output as a regular file.", + pEntry->fileNameLen, pEntry->fileName); + } - if (sehnd) { - selabel_lookup(sehnd, &secontext, targetFile, UNZIP_FILEMODE); - setfscreatecon(secontext); - } + char *secontext = NULL; - int fd = open(targetFile, O_CREAT|O_WRONLY|O_TRUNC|O_SYNC - , UNZIP_FILEMODE); + if (sehnd) { + selabel_lookup(sehnd, &secontext, targetFile, UNZIP_FILEMODE); + setfscreatecon(secontext); + } - if (secontext) { - freecon(secontext); - setfscreatecon(NULL); - } + int fd = open(targetFile, O_CREAT|O_WRONLY|O_TRUNC|O_SYNC, + UNZIP_FILEMODE); - if (fd < 0) { - LOGE("Can't create target file \"%s\": %s\n", - targetFile, strerror(errno)); - ok = false; - break; - } + if (secontext) { + freecon(secontext); + setfscreatecon(NULL); + } - bool ok = mzExtractZipEntryToFile(pArchive, pEntry, fd); - if (ok) { - ok = (fsync(fd) == 0); - } - if (close(fd) != 0) { - ok = false; - } - if (!ok) { - LOGE("Error extracting \"%s\"\n", targetFile); - ok = false; - break; - } + if (fd < 0) { + LOGE("Can't create target file \"%s\": %s\n", + targetFile, strerror(errno)); + ok = false; + break; + } - if (timestamp != NULL && utime(targetFile, timestamp)) { - LOGE("Error touching \"%s\"\n", targetFile); - ok = false; - break; - } + bool ok = mzExtractZipEntryToFile(pArchive, pEntry, fd); + if (ok) { + ok = (fsync(fd) == 0); + } + if (close(fd) != 0) { + ok = false; + } + if (!ok) { + LOGE("Error extracting \"%s\"\n", targetFile); + ok = false; + break; + } - LOGV("Extracted file \"%s\"\n", targetFile); - ++extractCount; + if (timestamp != NULL && utime(targetFile, timestamp)) { + LOGE("Error touching \"%s\"\n", targetFile); + ok = false; + break; } + + LOGV("Extracted file \"%s\"\n", targetFile); + ++extractCount; } if (callback != NULL) callback(targetFile, cookie); diff --git a/minzip/Zip.h b/minzip/Zip.h index 54eab3222..a2b2c26fc 100644 --- a/minzip/Zip.h +++ b/minzip/Zip.h @@ -143,9 +143,12 @@ bool mzExtractZipEntryToBuffer(const ZipArchive *pArchive, const ZipEntry *pEntry, unsigned char* buffer); /* - * Inflate all entries under zipDir to the directory specified by + * Inflate all files under zipDir to the directory specified by * targetDir, which must exist and be a writable directory. * + * Directory entries and symlinks are not extracted. + * + * * The immediate children of zipDir will become the immediate * children of targetDir; e.g., if the archive contains the entries * @@ -160,21 +163,15 @@ bool mzExtractZipEntryToBuffer(const ZipArchive *pArchive, * /tmp/two * /tmp/d/three * - * flags is zero or more of the following: - * - * MZ_EXTRACT_FILES_ONLY - only unpack files, not directories or symlinks - * MZ_EXTRACT_DRY_RUN - don't do anything, but do invoke the callback - * * If timestamp is non-NULL, file timestamps will be set accordingly. * * If callback is non-NULL, it will be invoked with each unpacked file. * * Returns true on success, false on failure. */ -enum { MZ_EXTRACT_FILES_ONLY = 1, MZ_EXTRACT_DRY_RUN = 2 }; bool mzExtractRecursive(const ZipArchive *pArchive, const char *zipDir, const char *targetDir, - int flags, const struct utimbuf *timestamp, + const struct utimbuf *timestamp, void (*callback)(const char *fn, void*), void *cookie, struct selabel_handle *sehnd); diff --git a/updater/install.c b/updater/install.c index 2b2ffb0c5..01a5dd24b 100644 --- a/updater/install.c +++ b/updater/install.c @@ -496,7 +496,7 @@ Value* PackageExtractDirFn(const char* name, State* state, struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default bool success = mzExtractRecursive(za, zip_path, dest_path, - MZ_EXTRACT_FILES_ONLY, ×tamp, + ×tamp, NULL, NULL, sehandle); free(zip_path); free(dest_path); -- cgit v1.2.3 From e57c62a007002577ee2339f53f9767af8a6b2877 Mon Sep 17 00:00:00 2001 From: Narayan Kamath Date: Mon, 23 Feb 2015 14:09:31 +0000 Subject: Remove more dead code from minzip. I've added explanatory comments to mzExtractRecursive because that function will live on as a utility even after we move the zip format related logic to libziparchive. bug: 19472796 Change-Id: Id69db859b9b90c13429134d40ba72c1d7c17aa8e --- minzip/Zip.c | 152 +++++++++++++++++++----------------------------------- minzip/Zip.h | 13 ++--- updater/install.c | 2 +- 3 files changed, 59 insertions(+), 108 deletions(-) diff --git a/minzip/Zip.c b/minzip/Zip.c index fb462f45c..b5e446716 100644 --- a/minzip/Zip.c +++ b/minzip/Zip.c @@ -828,7 +828,7 @@ static const char *targetEntryPath(MzPathHelper *helper, ZipEntry *pEntry) */ bool mzExtractRecursive(const ZipArchive *pArchive, const char *zipDir, const char *targetDir, - int flags, const struct utimbuf *timestamp, + const struct utimbuf *timestamp, void (*callback)(const char *fn, void *), void *cookie, struct selabel_handle *sehnd) { @@ -932,30 +932,19 @@ bool mzExtractRecursive(const ZipArchive *pArchive, break; } - /* With DRY_RUN set, invoke the callback but don't do anything else. - */ - if (flags & MZ_EXTRACT_DRY_RUN) { - if (callback != NULL) callback(targetFile, cookie); - continue; - } - - /* Create the file or directory. - */ #define UNZIP_DIRMODE 0755 #define UNZIP_FILEMODE 0644 - if (pEntry->fileName[pEntry->fileNameLen-1] == '/') { - if (!(flags & MZ_EXTRACT_FILES_ONLY)) { - int ret = dirCreateHierarchy( - targetFile, UNZIP_DIRMODE, timestamp, false, sehnd); - if (ret != 0) { - LOGE("Can't create containing directory for \"%s\": %s\n", - targetFile, strerror(errno)); - ok = false; - break; - } - LOGD("Extracted dir \"%s\"\n", targetFile); - } - } else { + /* + * Create the file or directory. We ignore directory entries + * because we recursively create paths to each file entry we encounter + * in the zip archive anyway. + * + * NOTE: A "directory entry" in a zip archive is just a zero length + * entry that ends in a "/". They're not mandatory and many tools get + * rid of them. We need to process them only if we want to preserve + * empty directories from the archive. + */ + if (pEntry->fileName[pEntry->fileNameLen-1] != '/') { /* This is not a directory. First, make sure that * the containing directory exists. */ @@ -968,91 +957,56 @@ bool mzExtractRecursive(const ZipArchive *pArchive, break; } - /* With FILES_ONLY set, we need to ignore metadata entirely, - * so treat symlinks as regular files. + /* + * The entry is a regular file or a symlink. Open the target for writing. + * + * TODO: This behavior for symlinks seems rather bizarre. For a + * symlink foo/bar/baz -> foo/tar/taz, we will create a file called + * "foo/bar/baz" whose contents are the literal "foo/tar/taz". We + * warn about this for now and preserve older behavior. */ - if (!(flags & MZ_EXTRACT_FILES_ONLY) && mzIsZipEntrySymlink(pEntry)) { - /* The entry is a symbolic link. - * The relative target of the symlink is in the - * data section of this entry. - */ - if (pEntry->uncompLen == 0) { - LOGE("Symlink entry \"%s\" has no target\n", - targetFile); - ok = false; - break; - } - char *linkTarget = malloc(pEntry->uncompLen + 1); - if (linkTarget == NULL) { - ok = false; - break; - } - ok = mzReadZipEntry(pArchive, pEntry, linkTarget, - pEntry->uncompLen); - if (!ok) { - LOGE("Can't read symlink target for \"%s\"\n", - targetFile); - free(linkTarget); - break; - } - linkTarget[pEntry->uncompLen] = '\0'; - - /* Make the link. - */ - ret = symlink(linkTarget, targetFile); - if (ret != 0) { - LOGE("Can't symlink \"%s\" to \"%s\": %s\n", - targetFile, linkTarget, strerror(errno)); - free(linkTarget); - ok = false; - break; - } - LOGD("Extracted symlink \"%s\" -> \"%s\"\n", - targetFile, linkTarget); - free(linkTarget); - } else { - /* The entry is a regular file. - * Open the target for writing. - */ - - char *secontext = NULL; + if (mzIsZipEntrySymlink(pEntry)) { + LOGE("Symlink entry \"%.*s\" will be output as a regular file.", + pEntry->fileNameLen, pEntry->fileName); + } - if (sehnd) { - selabel_lookup(sehnd, &secontext, targetFile, UNZIP_FILEMODE); - setfscreatecon(secontext); - } + char *secontext = NULL; - int fd = creat(targetFile, UNZIP_FILEMODE); + if (sehnd) { + selabel_lookup(sehnd, &secontext, targetFile, UNZIP_FILEMODE); + setfscreatecon(secontext); + } - if (secontext) { - freecon(secontext); - setfscreatecon(NULL); - } + int fd = creat(targetFile, UNZIP_FILEMODE); - if (fd < 0) { - LOGE("Can't create target file \"%s\": %s\n", - targetFile, strerror(errno)); - ok = false; - break; - } + if (secontext) { + freecon(secontext); + setfscreatecon(NULL); + } - bool ok = mzExtractZipEntryToFile(pArchive, pEntry, fd); - close(fd); - if (!ok) { - LOGE("Error extracting \"%s\"\n", targetFile); - ok = false; - break; - } + if (fd < 0) { + LOGE("Can't create target file \"%s\": %s\n", + targetFile, strerror(errno)); + ok = false; + break; + } - if (timestamp != NULL && utime(targetFile, timestamp)) { - LOGE("Error touching \"%s\"\n", targetFile); - ok = false; - break; - } + bool ok = mzExtractZipEntryToFile(pArchive, pEntry, fd); + close(fd); + if (!ok) { + LOGE("Error extracting \"%s\"\n", targetFile); + ok = false; + break; + } - LOGV("Extracted file \"%s\"\n", targetFile); - ++extractCount; + if (timestamp != NULL && utime(targetFile, timestamp)) { + LOGE("Error touching \"%s\"\n", targetFile); + ok = false; + break; } + + LOGV("Extracted file \"%s\"\n", targetFile); + ++extractCount; } if (callback != NULL) callback(targetFile, cookie); diff --git a/minzip/Zip.h b/minzip/Zip.h index 54eab3222..a2b2c26fc 100644 --- a/minzip/Zip.h +++ b/minzip/Zip.h @@ -143,9 +143,12 @@ bool mzExtractZipEntryToBuffer(const ZipArchive *pArchive, const ZipEntry *pEntry, unsigned char* buffer); /* - * Inflate all entries under zipDir to the directory specified by + * Inflate all files under zipDir to the directory specified by * targetDir, which must exist and be a writable directory. * + * Directory entries and symlinks are not extracted. + * + * * The immediate children of zipDir will become the immediate * children of targetDir; e.g., if the archive contains the entries * @@ -160,21 +163,15 @@ bool mzExtractZipEntryToBuffer(const ZipArchive *pArchive, * /tmp/two * /tmp/d/three * - * flags is zero or more of the following: - * - * MZ_EXTRACT_FILES_ONLY - only unpack files, not directories or symlinks - * MZ_EXTRACT_DRY_RUN - don't do anything, but do invoke the callback - * * If timestamp is non-NULL, file timestamps will be set accordingly. * * If callback is non-NULL, it will be invoked with each unpacked file. * * Returns true on success, false on failure. */ -enum { MZ_EXTRACT_FILES_ONLY = 1, MZ_EXTRACT_DRY_RUN = 2 }; bool mzExtractRecursive(const ZipArchive *pArchive, const char *zipDir, const char *targetDir, - int flags, const struct utimbuf *timestamp, + const struct utimbuf *timestamp, void (*callback)(const char *fn, void*), void *cookie, struct selabel_handle *sehnd); diff --git a/updater/install.c b/updater/install.c index dad0d08c9..05d68db4c 100644 --- a/updater/install.c +++ b/updater/install.c @@ -455,7 +455,7 @@ Value* PackageExtractDirFn(const char* name, State* state, struct utimbuf timestamp = { 1217592000, 1217592000 }; // 8/1/2008 default bool success = mzExtractRecursive(za, zip_path, dest_path, - MZ_EXTRACT_FILES_ONLY, ×tamp, + ×tamp, NULL, NULL, sehandle); free(zip_path); free(dest_path); -- cgit v1.2.3 From 5a50b1bce574f9a6b786f7139ca576fcdedb467b Mon Sep 17 00:00:00 2001 From: Ying Wang Date: Tue, 10 Mar 2015 11:56:56 -0700 Subject: Install the recovery binary directly to the recovery image. This eliminated the previous hack, that doesn't work reliably with the "LOCAL_REQUIRED_MODULES := mkfs.f2fs". Bug: 19666886 Change-Id: I1f0a2d41129f402c0165f3b86b6fda077291f282 --- Android.mk | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Android.mk b/Android.mk index 9fd3f866a..dd1e96e7a 100644 --- a/Android.mk +++ b/Android.mk @@ -84,11 +84,7 @@ ifeq ($(TARGET_USERIMAGES_USE_EXT4), true) LOCAL_STATIC_LIBRARIES += libext4_utils_static libz endif -# This binary is in the recovery ramdisk, which is otherwise a copy of root. -# It gets copied there in config/Makefile. LOCAL_MODULE_TAGS suppresses -# a (redundant) copy of the binary in /system/bin for user builds. -# TODO: Build the ramdisk image in a more principled way. -LOCAL_MODULE_TAGS := eng +LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin ifeq ($(TARGET_RECOVERY_UI_LIB),) LOCAL_SRC_FILES += default_device.cpp -- cgit v1.2.3 From 18f371d814b26132aadf11de6cc305f02484535a Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Tue, 10 Mar 2015 19:25:11 -0700 Subject: updater: Check the return value from ApplyImagePatch / ApplyBSDiffPatch Return NULL to abort the update process. Note that returning "" won't stop the script. Change-Id: Ifd108c1356f7c92a905c8776247a8842c6445319 --- recovery.cpp | 1 + updater/blockimg.c | 42 ++++++++++++++++++++++++++++-------------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/recovery.cpp b/recovery.cpp index 1d22b248a..6deeaaaed 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -1107,6 +1107,7 @@ main(int argc, char **argv) { } if (status != INSTALL_SUCCESS) { ui->Print("Installation aborted.\n"); + ui->Print("OTA failed! Please power off the device to keep it in this state and file a bug report!\n"); // If this is an eng or userdebug build, then automatically // turn the text display on if the script fails so the error diff --git a/updater/blockimg.c b/updater/blockimg.c index 302689313..6060ac28b 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -687,19 +687,26 @@ Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[] rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE; check_lseek(fd, (off64_t)tgt->pos[0] * BLOCKSIZE, SEEK_SET); + int ret; if (style[0] == 'i') { // imgdiff - ApplyImagePatch(buffer, src_blocks * BLOCKSIZE, - &patch_value, - &RangeSinkWrite, &rss, NULL, NULL); + ret = ApplyImagePatch(buffer, src_blocks * BLOCKSIZE, + &patch_value, + &RangeSinkWrite, &rss, NULL, NULL); } else { - ApplyBSDiffPatch(buffer, src_blocks * BLOCKSIZE, - &patch_value, 0, - &RangeSinkWrite, &rss, NULL); + ret = ApplyBSDiffPatch(buffer, src_blocks * BLOCKSIZE, + &patch_value, 0, + &RangeSinkWrite, &rss, NULL); + } + + if (ret != 0) { + ErrorAbort(state, "patch failed\n"); + goto done; } // We expect the output of the patcher to fill the tgt ranges exactly. if (rss.p_block != tgt->count || rss.p_remain != 0) { - fprintf(stderr, "range sink underrun?\n"); + ErrorAbort(state, "range sink underrun?\n"); + goto done; } blocks_so_far += tgt->size; @@ -723,7 +730,8 @@ Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[] range[1] = (tgt->pos[i*2+1] - tgt->pos[i*2]) * (uint64_t)BLOCKSIZE; if (ioctl(fd, BLKDISCARD, &range) < 0) { - printf(" blkdiscard failed: %s\n", strerror(errno)); + ErrorAbort(state, " blkdiscard failed: %s\n", strerror(errno)); + goto done; } } @@ -732,8 +740,8 @@ Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[] printf(" ignoring erase (not block device)\n"); } } else { - fprintf(stderr, "unknown transfer style \"%s\"\n", style); - exit(1); + ErrorAbort(state, "unknown transfer style \"%s\"\n", style); + goto done; } } @@ -744,13 +752,19 @@ Value* BlockImageUpdateFn(const char* name, State* state, int argc, Expr* argv[] printf("wrote %d blocks; expected %d\n", blocks_so_far, total_blocks); printf("max alloc needed was %zu\n", buffer_alloc); - done: +done: free(transfer_list); FreeValue(blockdev_filename); FreeValue(transfer_list_value); FreeValue(new_data_fn); FreeValue(patch_data_fn); - return StringValue(success ? strdup("t") : strdup("")); + if (success) { + return StringValue(strdup("t")); + } else { + // NULL will be passed to its caller at Evaluate() and abort the OTA + // process. + return NULL; + } } Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) { @@ -793,11 +807,11 @@ Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) { digest = SHA_final(&ctx); close(fd); - done: +done: FreeValue(blockdev_filename); FreeValue(ranges); if (digest == NULL) { - return StringValue(strdup("")); + return NULL; } else { return StringValue(PrintSha1(digest)); } -- cgit v1.2.3 From 2196a3eda038d7b08e4f834a8086db69f32ee299 Mon Sep 17 00:00:00 2001 From: Ying Wang Date: Tue, 10 Mar 2015 11:56:56 -0700 Subject: Install the recovery binary directly to the recovery image. This eliminated the previous hack, that doesn't work reliably with the "LOCAL_REQUIRED_MODULES := mkfs.f2fs". Bug: 19666886 Change-Id: I1f0a2d41129f402c0165f3b86b6fda077291f282 --- Android.mk | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Android.mk b/Android.mk index 9fd3f866a..dd1e96e7a 100644 --- a/Android.mk +++ b/Android.mk @@ -84,11 +84,7 @@ ifeq ($(TARGET_USERIMAGES_USE_EXT4), true) LOCAL_STATIC_LIBRARIES += libext4_utils_static libz endif -# This binary is in the recovery ramdisk, which is otherwise a copy of root. -# It gets copied there in config/Makefile. LOCAL_MODULE_TAGS suppresses -# a (redundant) copy of the binary in /system/bin for user builds. -# TODO: Build the ramdisk image in a more principled way. -LOCAL_MODULE_TAGS := eng +LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin ifeq ($(TARGET_RECOVERY_UI_LIB),) LOCAL_SRC_FILES += default_device.cpp -- cgit v1.2.3 From b2f945932c3cd89138b8dcfacd880c627dfed370 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Fri, 13 Mar 2015 22:33:38 -0700 Subject: Remove unused libutils references. Change-Id: I22797643103ce1d4371198433ad9849cc83d3d7c --- minadbd/Android.mk | 2 +- minadbd/fuse_adb_provider_test.cpp | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/minadbd/Android.mk b/minadbd/Android.mk index 79fe96b93..52d3fa4e4 100644 --- a/minadbd/Android.mk +++ b/minadbd/Android.mk @@ -31,6 +31,6 @@ LOCAL_SRC_FILES := fuse_adb_provider_test.cpp LOCAL_CFLAGS := $(minadbd_cflags) LOCAL_C_INCLUDES := $(LOCAL_PATH) system/core/adb LOCAL_STATIC_LIBRARIES := libminadbd -LOCAL_SHARED_LIBRARIES := liblog libutils +LOCAL_SHARED_LIBRARIES := liblog include $(BUILD_NATIVE_TEST) diff --git a/minadbd/fuse_adb_provider_test.cpp b/minadbd/fuse_adb_provider_test.cpp index ecd9f384b..0f2e881c7 100644 --- a/minadbd/fuse_adb_provider_test.cpp +++ b/minadbd/fuse_adb_provider_test.cpp @@ -25,7 +25,6 @@ #include #include "adb_io.h" -#include "utils/file.h" TEST(fuse_adb_provider, read_block_adb) { adb_data data = {}; -- cgit v1.2.3 From c51ac89f0ee5d4a0dd8edebc134a924f83738140 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 20 Mar 2015 18:22:15 -0700 Subject: Add "Apply update from sdcard" to default recovery image. At the moment, this is the only difference in the sprout recovery image. That's silly. Let's just improve the error handling slightly and always have this option present. Also make the obscure "<3e>" less unclear. Also use "power button" as the default text rather than "enter button", because it's been years since anyone had one of those. (Longer term we should let subclassers tell us the keycode and we translate it to the correct string.) Also move the two "Reboot" options together, put "Power off" at the bottom (and use that terminology, like the real UI, rather than "Power down"), and use capitals throughout. Finally, add a README.md with some useful instructions. Change-Id: I94fb19f73d79c54fed2dda30cefb884426641b5c --- README.md | 14 ++++++++++++++ default_device.cpp | 40 +++++++++++++++++++++++----------------- recovery.cpp | 10 +++++++--- 3 files changed, 44 insertions(+), 20 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 000000000..c0833f2d0 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +The Recovery Image +================== + +Quick turn-around testing +------------------------- + + mm -j + m ramdisk-nodeps + m recoveryimage-nodeps + adb reboot bootloader + + # To boot into the new recovery image + # without flashing the recovery partition: + fastboot boot $ANDROID_PRODUCT_OUT/recovery.img diff --git a/default_device.cpp b/default_device.cpp index 97806ac58..ed601f6c6 100644 --- a/default_device.cpp +++ b/default_device.cpp @@ -20,19 +20,24 @@ #include "device.h" #include "screen_ui.h" -static const char* HEADERS[] = { "Volume up/down to move highlight;", - "enter button to select.", - "", - NULL }; +static const char* HEADERS[] = { + "Volume up/down to move highlight.", + "Power button to select.", + "", + NULL +}; -static const char* ITEMS[] = {"reboot system now", - "apply update from ADB", - "wipe data/factory reset", - "wipe cache partition", - "reboot to bootloader", - "power down", - "view recovery logs", - NULL }; +static const char* ITEMS[] = { + "Reboot system now", + "Reboot to bootloader", + "Apply update from ADB", + "Apply update from SD card", + "Wipe data/factory reset", + "Wipe cache partition", + "View recovery logs", + "Power off", + NULL +}; class DefaultDevice : public Device { public: @@ -65,12 +70,13 @@ class DefaultDevice : public Device { BuiltinAction InvokeMenuItem(int menu_position) { switch (menu_position) { case 0: return REBOOT; - case 1: return APPLY_ADB_SIDELOAD; - case 2: return WIPE_DATA; - case 3: return WIPE_CACHE; - case 4: return REBOOT_BOOTLOADER; - case 5: return SHUTDOWN; + case 1: return REBOOT_BOOTLOADER; + case 2: return APPLY_ADB_SIDELOAD; + case 3: return APPLY_EXT; + case 4: return WIPE_DATA; + case 5: return WIPE_CACHE; case 6: return READ_RECOVERY_LASTLOG; + case 7: return SHUTDOWN; default: return NO_ACTION; } } diff --git a/recovery.cpp b/recovery.cpp index 07606137b..641f36f3e 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -512,7 +512,7 @@ prepend_title(const char* const* headers) { const char** new_headers = (const char**)malloc((count+1) * sizeof(char*)); const char** h = new_headers; - *(h++) = "Android system recovery <" EXPAND(RECOVERY_API_VERSION) "e>"; + *(h++) = "Android system recovery (API " EXPAND(RECOVERY_API_VERSION) ")"; *(h++) = recovery_version; *(h++) = ""; for (p = headers; *p; ++p, ++h) *h = *p; @@ -877,7 +877,11 @@ prompt_and_wait(Device* device, int status) { break; case Device::APPLY_EXT: { - ensure_path_mounted(SDCARD_ROOT); + if (ensure_path_mounted(SDCARD_ROOT) != 0) { + ui->Print("\n-- Couldn't mount %s.\n", SDCARD_ROOT); + break; + } + char* path = browse_directory(SDCARD_ROOT, device); if (path == NULL) { ui->Print("\n-- No package file selected.\n", path); @@ -910,7 +914,7 @@ prompt_and_wait(Device* device, int status) { } else if (!ui->IsTextVisible()) { return Device::NO_ACTION; // reboot if logs aren't visible } else { - ui->Print("\nInstall from sdcard complete.\n"); + ui->Print("\nInstall from SD card complete.\n"); } } break; -- cgit v1.2.3 From 1fdd452f47299be60cac9acbd8e7c864d9c174bd Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 23 Mar 2015 13:33:57 -0700 Subject: Always use strerror to report errno in recovery. Change-Id: I7009959043150fabf5853a43ee2448c7fbea176e --- fuse_sideload.c | 6 +++--- mtdutils/mtdutils.c | 8 ++++---- updater/blockimg.c | 39 ++++++++++++++++++++------------------- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/fuse_sideload.c b/fuse_sideload.c index 4e11e01e4..1dd84e97a 100644 --- a/fuse_sideload.c +++ b/fuse_sideload.c @@ -106,12 +106,12 @@ static void fuse_reply(struct fuse_data* fd, __u64 unique, const void *data, siz vec[0].iov_base = &hdr; vec[0].iov_len = sizeof(hdr); - vec[1].iov_base = data; + vec[1].iov_base = /* const_cast */(void*)(data); vec[1].iov_len = len; res = writev(fd->ffd, vec, 2); if (res < 0) { - printf("*** REPLY FAILED *** %d\n", errno); + printf("*** REPLY FAILED *** %s\n", strerror(errno)); } } @@ -430,7 +430,7 @@ int run_fuse_sideload(struct provider_vtab* vtab, void* cookie, char opts[256]; snprintf(opts, sizeof(opts), - ("fd=%d,user_id=%d,group_id=%d,max_read=%zu," + ("fd=%d,user_id=%d,group_id=%d,max_read=%u," "allow_other,rootmode=040000"), fd.ffd, fd.uid, fd.gid, block_size); diff --git a/mtdutils/mtdutils.c b/mtdutils/mtdutils.c index d04b26efa..9a17e38d8 100644 --- a/mtdutils/mtdutils.c +++ b/mtdutils/mtdutils.c @@ -313,8 +313,8 @@ static int read_block(const MtdPartition *partition, int fd, char *data) memcpy(&before, &after, sizeof(struct mtd_ecc_stats)); } else if ((mgbb = ioctl(fd, MEMGETBADBLOCK, &pos))) { fprintf(stderr, - "mtd: MEMGETBADBLOCK returned %d at 0x%08llx (errno=%d)\n", - mgbb, pos, errno); + "mtd: MEMGETBADBLOCK returned %d at 0x%08llx: %s\n", + mgbb, pos, strerror(errno)); } else { return 0; // Success! } @@ -419,8 +419,8 @@ static int write_block(MtdWriteContext *ctx, const char *data) if (ret != 0 && !(ret == -1 && errno == EOPNOTSUPP)) { add_bad_block_offset(ctx, pos); fprintf(stderr, - "mtd: not writing bad block at 0x%08lx (ret %d errno %d)\n", - pos, ret, errno); + "mtd: not writing bad block at 0x%08lx (ret %d): %s\n", + pos, ret, strerror(errno)); pos += partition->erase_size; continue; // Don't try to erase known factory-bad blocks. } diff --git a/updater/blockimg.c b/updater/blockimg.c index 9c39634fc..90b55b2d2 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -464,7 +464,7 @@ static void EnumerateStash(const char* dirname, StashCallback callback, void* da if (directory == NULL) { if (errno != ENOENT) { - fprintf(stderr, "failed to opendir %s (errno %d)\n", dirname, errno); + fprintf(stderr, "opendir \"%s\" failed: %s\n", dirname, strerror(errno)); } return; } @@ -495,7 +495,7 @@ static void EnumerateStash(const char* dirname, StashCallback callback, void* da } if (closedir(directory) == -1) { - fprintf(stderr, "failed to closedir %s (errno %d)\n", dirname, errno); + fprintf(stderr, "closedir \"%s\" failed: %s\n", dirname, strerror(errno)); } } @@ -508,7 +508,7 @@ static void UpdateFileSize(const char* fn, void* data) { } if (stat(fn, &st) == -1) { - fprintf(stderr, "failed to stat %s (errno %d)\n", fn, errno); + fprintf(stderr, "stat \"%s\" failed: %s\n", fn, strerror(errno)); return; } @@ -524,7 +524,7 @@ static void DeleteFile(const char* fn, void* data) { fprintf(stderr, "deleting %s\n", fn); if (unlink(fn) == -1 && errno != ENOENT) { - fprintf(stderr, "failed to unlink %s (errno %d)\n", fn, errno); + fprintf(stderr, "unlink \"%s\" failed: %s\n", fn, strerror(errno)); } } } @@ -553,7 +553,7 @@ static void DeleteStash(const char* base) { if (rmdir(dirname) == -1) { if (errno != ENOENT && errno != ENOTDIR) { - fprintf(stderr, "failed to rmdir %s (errno %d)\n", dirname, errno); + fprintf(stderr, "rmdir \"%s\" failed: %s\n", dirname, strerror(errno)); } } @@ -587,7 +587,7 @@ static int LoadStash(const char* base, const char* id, int verify, int* blocks, if (res == -1) { if (errno != ENOENT || printnoent) { - fprintf(stderr, "failed to stat %s (errno %d)\n", fn, errno); + fprintf(stderr, "stat \"%s\" failed: %s\n", fn, strerror(errno)); } goto lsout; } @@ -602,7 +602,7 @@ static int LoadStash(const char* base, const char* id, int verify, int* blocks, fd = TEMP_FAILURE_RETRY(open(fn, O_RDONLY)); if (fd == -1) { - fprintf(stderr, "failed to open %s (errno %d)\n", fn, errno); + fprintf(stderr, "open \"%s\" failed: %s\n", fn, strerror(errno)); goto lsout; } @@ -663,7 +663,7 @@ static int WriteStash(const char* base, const char* id, int blocks, uint8_t* buf fd = TEMP_FAILURE_RETRY(open(fn, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, STASH_FILE_MODE)); if (fd == -1) { - fprintf(stderr, "failed to create %s (errno %d)\n", fn, errno); + fprintf(stderr, "failed to create \"%s\": %s\n", fn, strerror(errno)); goto wsout; } @@ -672,12 +672,12 @@ static int WriteStash(const char* base, const char* id, int blocks, uint8_t* buf } if (fsync(fd) == -1) { - fprintf(stderr, "failed to fsync %s (errno %d)\n", fn, errno); + fprintf(stderr, "fsync \"%s\" failed: %s\n", fn, strerror(errno)); goto wsout; } if (rename(fn, cn) == -1) { - fprintf(stderr, "failed to rename %s to %s (errno %d)\n", fn, cn, errno); + fprintf(stderr, "rename(\"%s\", \"%s\") failed: %s\n", fn, cn, strerror(errno)); goto wsout; } @@ -737,14 +737,14 @@ static int CreateStash(State* state, int maxblocks, const char* blockdev, char** res = stat(dirname, &st); if (res == -1 && errno != ENOENT) { - ErrorAbort(state, "failed to stat %s (errno %d)\n", dirname, errno); + ErrorAbort(state, "stat \"%s\" failed: %s\n", dirname, strerror(errno)); goto csout; } else if (res != 0) { fprintf(stderr, "creating stash %s\n", dirname); res = mkdir(dirname, STASH_DIRECTORY_MODE); if (res != 0) { - ErrorAbort(state, "failed to create %s (errno %d)\n", dirname, errno); + ErrorAbort(state, "mkdir \"%s\" failed: %s\n", dirname, strerror(errno)); goto csout; } @@ -1408,7 +1408,7 @@ static int PerformCommandErase(CommandParameters* params) { } if (fstat(params->fd, &st) == -1) { - fprintf(stderr, "failed to fstat device to erase (errno %d)\n", errno); + fprintf(stderr, "failed to fstat device to erase: %s\n", strerror(errno)); goto pceout; } @@ -1437,7 +1437,7 @@ static int PerformCommandErase(CommandParameters* params) { blocks[1] = (tgt->pos[i * 2 + 1] - tgt->pos[i * 2]) * (uint64_t) BLOCKSIZE; if (ioctl(params->fd, BLKDISCARD, &blocks) == -1) { - fprintf(stderr, "failed to blkdiscard (errno %d)\n", errno); + fprintf(stderr, "BLKDISCARD ioctl failed: %s\n", strerror(errno)); // Continue anyway, nothing we can do } } @@ -1574,7 +1574,7 @@ static Value* PerformBlockImageUpdate(const char* name, State* state, int argc, params.fd = TEMP_FAILURE_RETRY(open(blockdev_filename->data, O_RDWR)); if (params.fd == -1) { - fprintf(stderr, "failed to open %s: %s", blockdev_filename->data, strerror(errno)); + fprintf(stderr, "open \"%s\" failed: %s\n", blockdev_filename->data, strerror(errno)); goto pbiudone; } @@ -1587,8 +1587,9 @@ static Value* PerformBlockImageUpdate(const char* name, State* state, int argc, pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); - if (pthread_create(¶ms.thread, &attr, unzip_new_data, ¶ms.nti) != 0) { - fprintf(stderr, "failed to create a thread (errno %d)\n", errno); + int error = pthread_create(¶ms.thread, &attr, unzip_new_data, ¶ms.nti); + if (error != 0) { + fprintf(stderr, "pthread_create failed: %s\n", strerror(error)); goto pbiudone; } } @@ -1719,7 +1720,7 @@ static Value* PerformBlockImageUpdate(const char* name, State* state, int argc, pbiudone: if (params.fd != -1) { if (fsync(params.fd) == -1) { - fprintf(stderr, "failed to fsync device (errno %d)\n", errno); + fprintf(stderr, "fsync failed: %s\n", strerror(errno)); } TEMP_FAILURE_RETRY(close(params.fd)); } @@ -1875,7 +1876,7 @@ Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) { int fd = open(blockdev_filename->data, O_RDWR); if (fd < 0) { - ErrorAbort(state, "failed to open %s: %s", blockdev_filename->data, strerror(errno)); + ErrorAbort(state, "open \"%s\" failed: %s", blockdev_filename->data, strerror(errno)); goto done; } -- cgit v1.2.3 From fc06f87ecc71cb79887b1365dca8ac5555fe1205 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 23 Mar 2015 13:45:31 -0700 Subject: Make the recovery menus wrap. The real problem is that the recovery UI is sluggish. But being able to wrap off the top to the bottom halves the maximum distance you'll have to go. Change-Id: Ifebe5b818f9c9a1c4187d4ac609422da1f38537f --- screen_ui.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/screen_ui.cpp b/screen_ui.cpp index 03ef049ae..5e3a24fd2 100644 --- a/screen_ui.cpp +++ b/screen_ui.cpp @@ -526,8 +526,11 @@ int ScreenRecoveryUI::SelectMenu(int sel) { if (show_menu > 0) { old_sel = menu_sel; menu_sel = sel; - if (menu_sel < 0) menu_sel = 0; - if (menu_sel >= menu_items) menu_sel = menu_items-1; + + // Wrap at top and bottom. + if (menu_sel < 0) menu_sel = menu_items - 1; + if (menu_sel >= menu_items) menu_sel = 0; + sel = menu_sel; if (menu_sel != old_sel) update_screen_locked(); } -- cgit v1.2.3 From e853e96b40a77a1c89779d9bddd612622f04a62d Mon Sep 17 00:00:00 2001 From: Gaelle Nassiet Date: Mon, 23 Mar 2015 17:51:53 +0100 Subject: always use volume mount option when mounting a partition From ROS, if enable adb using the vol.up and vol.down buttons, the /system partition is mounted by the function ensure_path_mounted() but with hardcoded mount options. As a consequence, the blocks are modified and the reboot in MOS is blocked by the dm_verity feature that detects a corruption. This patch forces the function ensure_path_mounted() to use the mount options from the volume structure, that were previously read from the fstab. Change-Id: I748d32c14cb821f4aae5bcc430089dab45375515 Signed-off-by: Gaelle Nassiet Signed-off-by: Jeremy Compostella --- roots.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roots.cpp b/roots.cpp index 0d47577b2..c067bccb0 100644 --- a/roots.cpp +++ b/roots.cpp @@ -115,7 +115,7 @@ int ensure_path_mounted(const char* path) { } else if (strcmp(v->fs_type, "ext4") == 0 || strcmp(v->fs_type, "vfat") == 0) { result = mount(v->blk_device, v->mount_point, v->fs_type, - MS_NOATIME | MS_NODEV | MS_NODIRATIME, ""); + v->flags, v->fs_options); if (result == 0) return 0; LOGE("failed to mount %s (%s)\n", v->mount_point, strerror(errno)); -- cgit v1.2.3 From 4ec58a4ee5d025deeb83ce8322aa8a65b16d8a9c Mon Sep 17 00:00:00 2001 From: Christian Poetzsch Date: Thu, 19 Feb 2015 10:42:39 +0000 Subject: Fix wipe command when using sideload in recovery Add support for the wipe command when using sideload within the recovery. All the support for this command is in place, only the execution of the actual wipe command itself was missing. Change-Id: Ia9cdfc912bfb9f558fa89b9f0ed54e843ede41f2 Signed-off-by: Christian Poetzsch --- recovery.cpp | 99 +++++++++++++++++++++++++++++------------------------------- 1 file changed, 47 insertions(+), 52 deletions(-) diff --git a/recovery.cpp b/recovery.cpp index 641f36f3e..51c131d9b 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -825,6 +825,30 @@ static void choose_recovery_file(Device* device) { } } +static int apply_from_sdcard(Device* device, int* wipe_cache) { + if (ensure_path_mounted(SDCARD_ROOT) != 0) { + ui->Print("\n-- Couldn't mount %s.\n", SDCARD_ROOT); + return INSTALL_ERROR; + } + + char* path = browse_directory(SDCARD_ROOT, device); + if (path == NULL) { + ui->Print("\n-- No package file selected.\n", path); + return INSTALL_ERROR; + } + + ui->Print("\n-- Install %s ...\n", path); + set_sdcard_update_bootloader_message(); + void* token = start_sdcard_fuse(path); + + int status = install_package(FUSE_SIDELOAD_HOST_PATHNAME, wipe_cache, + TEMPORARY_INSTALL_FILE, false); + + finish_sdcard_fuse(token); + ensure_path_unmounted(SDCARD_ROOT); + return status; +} + // Return REBOOT, SHUTDOWN, or REBOOT_BOOTLOADER. Returning NO_ACTION // means to take the default, which is to reboot or shutdown depending // on if the --shutdown_after flag was passed to recovery. @@ -876,49 +900,35 @@ prompt_and_wait(Device* device, int status) { if (!ui->IsTextVisible()) return Device::NO_ACTION; break; - case Device::APPLY_EXT: { - if (ensure_path_mounted(SDCARD_ROOT) != 0) { - ui->Print("\n-- Couldn't mount %s.\n", SDCARD_ROOT); - break; - } - - char* path = browse_directory(SDCARD_ROOT, device); - if (path == NULL) { - ui->Print("\n-- No package file selected.\n", path); - break; - } - - ui->Print("\n-- Install %s ...\n", path); - set_sdcard_update_bootloader_message(); - void* token = start_sdcard_fuse(path); - - int status = install_package(FUSE_SIDELOAD_HOST_PATHNAME, &wipe_cache, - TEMPORARY_INSTALL_FILE, false); - - finish_sdcard_fuse(token); - ensure_path_unmounted(SDCARD_ROOT); - - if (status == INSTALL_SUCCESS && wipe_cache) { - ui->Print("\n-- Wiping cache (at package request)...\n"); - if (erase_volume("/cache")) { - ui->Print("Cache wipe failed.\n"); + case Device::APPLY_ADB_SIDELOAD: + case Device::APPLY_EXT: + { + bool adb = (chosen_action == Device::APPLY_ADB_SIDELOAD); + if (adb) { + status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE); } else { - ui->Print("Cache wipe complete.\n"); + status = apply_from_sdcard(device, &wipe_cache); } - } - if (status >= 0) { - if (status != INSTALL_SUCCESS) { - ui->SetBackground(RecoveryUI::ERROR); - ui->Print("Installation aborted.\n"); - } else if (!ui->IsTextVisible()) { - return Device::NO_ACTION; // reboot if logs aren't visible - } else { - ui->Print("\nInstall from SD card complete.\n"); + if (status == INSTALL_SUCCESS && wipe_cache) { + ui->Print("\n-- Wiping cache (at package request)...\n"); + bool okay = erase_volume("/cache"); + ui->Print("Cache wipe %s.\n", okay ? "succeeded" : "failed"); + } + + if (status >= 0) { + if (status != INSTALL_SUCCESS) { + ui->SetBackground(RecoveryUI::ERROR); + ui->Print("Installation aborted.\n"); + copy_logs(); + } else if (!ui->IsTextVisible()) { + return Device::NO_ACTION; // reboot if logs aren't visible + } else { + ui->Print("\nInstall from %s complete.\n", adb ? "ADB" : "SD card"); + } } } break; - } case Device::APPLY_CACHE: ui->Print("\nAPPLY_CACHE is deprecated.\n"); @@ -927,21 +937,6 @@ prompt_and_wait(Device* device, int status) { case Device::READ_RECOVERY_LASTLOG: choose_recovery_file(device); break; - - case Device::APPLY_ADB_SIDELOAD: - status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE); - if (status >= 0) { - if (status != INSTALL_SUCCESS) { - ui->SetBackground(RecoveryUI::ERROR); - ui->Print("Installation aborted.\n"); - copy_logs(); - } else if (!ui->IsTextVisible()) { - return Device::NO_ACTION; // reboot if logs aren't visible - } else { - ui->Print("\nInstall from ADB complete.\n"); - } - } - break; } } } -- cgit v1.2.3 From 01a4d08010d1c26cc2cb37ca62b624829a7e1506 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 24 Mar 2015 15:21:48 -0700 Subject: Fix recovery image text rendering. Previously most devices would lose the character before a line wrap. The log's text rendering was starting at offset 4 but none of the arithmetic was taking this into account. It just happened to work on the Nexus 9's 1536-pixel wide display (1536/18=85.3) but not on a device such as the Nexus 5 (1080/18=60). The only active part of this change is the change from 4 to 0 in the gr_text call. The rest is just a few bits of trivial cleanup while I was working out what was going on. Change-Id: I9279ae323c77bc8b6ea87dc0fe009aaaec6bfa0e --- minui/Android.mk | 5 +++-- minui/graphics.c | 42 +++++++++++++++++++----------------------- screen_ui.cpp | 10 +++------- 3 files changed, 25 insertions(+), 32 deletions(-) diff --git a/minui/Android.mk b/minui/Android.mk index 9b2e09b70..53d072fac 100644 --- a/minui/Android.mk +++ b/minui/Android.mk @@ -1,14 +1,15 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) -LOCAL_SRC_FILES := graphics.c graphics_adf.c graphics_fbdev.c events.c \ - resources.c +LOCAL_SRC_FILES := graphics.c graphics_adf.c graphics_fbdev.c events.c resources.c LOCAL_WHOLE_STATIC_LIBRARIES += libadf LOCAL_STATIC_LIBRARIES += libpng LOCAL_MODULE := libminui +LOCAL_CFLAGS := -std=gnu11 + # This used to compare against values in double-quotes (which are just # ordinary characters in this context). Strip double-quotes from the # value so that either will work. diff --git a/minui/graphics.c b/minui/graphics.c index 870ffa089..1a9be6cb2 100644 --- a/minui/graphics.c +++ b/minui/graphics.c @@ -77,11 +77,10 @@ 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) { + for (int j = 0; j < height; ++j) { unsigned char* sx = src_p; unsigned char* px = dst_p; - for (i = 0; i < width; ++i) { + 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) { @@ -106,34 +105,33 @@ static void text_blend(unsigned char* src_p, int src_row_bytes, } } - void gr_text(int x, int y, const char *s, int bold) { - GRFont *font = gr_font; - unsigned off; + GRFont* font = gr_font; - if (!font->texture) return; - if (gr_current_a == 0) return; + if (!font->texture || gr_current_a == 0) return; bold = bold && (font->texture->height != font->cheight); x += overscan_offset_x; y += overscan_offset_y; - while((off = *s++)) { - off -= 32; + unsigned char ch; + while ((ch = *s++)) { if (outside(x, y) || outside(x+font->cwidth-1, y+font->cheight-1)) break; - if (off < 96) { - 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; + if (ch < ' ' || ch > '~') { + ch = '?'; + } - text_blend(src_p, font->texture->row_bytes, - dst_p, gr_draw->row_bytes, - font->cwidth, font->cheight); + unsigned char* src_p = font->texture->data + ((ch - ' ') * 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; } } @@ -176,14 +174,12 @@ void gr_color(unsigned char r, unsigned char g, unsigned char b, unsigned char a void gr_clear() { - if (gr_current_r == gr_current_g && - gr_current_r == gr_current_b) { + 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) { + 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; diff --git a/screen_ui.cpp b/screen_ui.cpp index 5e3a24fd2..edc41abc0 100644 --- a/screen_ui.cpp +++ b/screen_ui.cpp @@ -246,12 +246,11 @@ void ScreenRecoveryUI::draw_screen_locked() // display from the bottom up, until we hit the top of the // screen, the bottom of the menu, or we've displayed the // entire text buffer. - int ty; int row = (text_top+text_rows-1) % text_rows; for (int ty = gr_fb_height() - char_height, count = 0; ty > y+2 && count < text_rows; ty -= char_height, ++count) { - gr_text(4, ty, text[row], 0); + gr_text(0, ty, text[row], 0); --row; if (row < 0) row = text_rows-1; } @@ -480,8 +479,7 @@ void ScreenRecoveryUI::Print(const char *fmt, ...) // This can get called before ui_init(), so be careful. pthread_mutex_lock(&updateMutex); if (text_rows > 0 && text_cols > 0) { - char *ptr; - for (ptr = buf; *ptr != '\0'; ++ptr) { + for (char* ptr = buf; *ptr != '\0'; ++ptr) { if (*ptr == '\n' || text_col >= text_cols) { text[text_row][text_col] = '\0'; text_col = 0; @@ -521,10 +519,9 @@ void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const } int ScreenRecoveryUI::SelectMenu(int sel) { - int old_sel; pthread_mutex_lock(&updateMutex); if (show_menu > 0) { - old_sel = menu_sel; + int old_sel = menu_sel; menu_sel = sel; // Wrap at top and bottom. @@ -539,7 +536,6 @@ int ScreenRecoveryUI::SelectMenu(int sel) { } void ScreenRecoveryUI::EndMenu() { - int i; pthread_mutex_lock(&updateMutex); if (show_menu > 0 && text_rows > 0 && text_cols > 0) { show_menu = 0; -- cgit v1.2.3 From 59156bdedad7e4f0d5ae042d79a5aee24c685884 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 24 Mar 2015 15:58:15 -0700 Subject: Remove support for Cupcake kernels. Change-Id: I7376b9d3c1e11d19e164072d6e9d09c1183114a0 --- minui/graphics.c | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/minui/graphics.c b/minui/graphics.c index 1a9be6cb2..9d1e1b480 100644 --- a/minui/graphics.c +++ b/minui/graphics.c @@ -48,8 +48,6 @@ static int overscan_percent = OVERSCAN_PERCENT; static int overscan_offset_x = 0; static int overscan_offset_y = 0; -static int gr_vt_fd = -1; - static unsigned char gr_current_r = 255; static unsigned char gr_current_g = 255; static unsigned char gr_current_b = 255; @@ -362,17 +360,6 @@ int gr_init(void) { 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. - perror("can't open /dev/tty0"); - } else if (ioctl(gr_vt_fd, KDSETMODE, (void*) KD_GRAPHICS)) { - // However, if we do open tty0, we expect the ioctl to work. - perror("failed KDSETMODE to KD_GRAPHICS on tty0"); - gr_exit(); - return -1; - } - gr_backend = open_adf(); if (gr_backend) { gr_draw = gr_backend->init(gr_backend); @@ -401,10 +388,6 @@ int gr_init(void) void gr_exit(void) { gr_backend->exit(gr_backend); - - ioctl(gr_vt_fd, KDSETMODE, (void*) KD_TEXT); - close(gr_vt_fd); - gr_vt_fd = -1; } int gr_fb_width(void) -- cgit v1.2.3 From 145d86146084b199e64c3b2db21a22c05aa30098 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Wed, 25 Mar 2015 15:51:15 -0700 Subject: Factor out option variables from int to bool types Change-Id: Ia897aa43e44d115bde6de91789b35723826ace22 --- adb_install.cpp | 2 +- adb_install.h | 2 +- install.cpp | 10 +++++----- install.h | 2 +- recovery.cpp | 14 ++++++++------ 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/adb_install.cpp b/adb_install.cpp index e3289608f..6d6dbb165 100644 --- a/adb_install.cpp +++ b/adb_install.cpp @@ -73,7 +73,7 @@ maybe_restart_adbd() { #define ADB_INSTALL_TIMEOUT 300 int -apply_from_adb(RecoveryUI* ui_, int* wipe_cache, const char* install_file) { +apply_from_adb(RecoveryUI* ui_, bool* wipe_cache, const char* install_file) { ui = ui_; stop_adbd(); diff --git a/adb_install.h b/adb_install.h index a18b712a2..efad436fa 100644 --- a/adb_install.h +++ b/adb_install.h @@ -19,6 +19,6 @@ class RecoveryUI; -int apply_from_adb(RecoveryUI* h, int* wipe_cache, const char* install_file); +int apply_from_adb(RecoveryUI* h, bool* wipe_cache, const char* install_file); #endif diff --git a/install.cpp b/install.cpp index 31606bb1a..63b1d38c3 100644 --- a/install.cpp +++ b/install.cpp @@ -48,7 +48,7 @@ static const float DEFAULT_IMAGE_PROGRESS_FRACTION = 0.1; // If the package contains an update binary, extract it and run it. static int -try_update_binary(const char *path, ZipArchive *zip, int* wipe_cache) { +try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache) { const ZipEntry* binary_entry = mzFindZipEntry(zip, ASSUMED_UPDATE_BINARY_NAME); if (binary_entry == NULL) { @@ -129,7 +129,7 @@ try_update_binary(const char *path, ZipArchive *zip, int* wipe_cache) { } close(pipefd[1]); - *wipe_cache = 0; + *wipe_cache = false; char buffer[1024]; FILE* from_child = fdopen(pipefd[0], "r"); @@ -158,7 +158,7 @@ try_update_binary(const char *path, ZipArchive *zip, int* wipe_cache) { } fflush(stdout); } else if (strcmp(command, "wipe_cache") == 0) { - *wipe_cache = 1; + *wipe_cache = true; } else if (strcmp(command, "clear_display") == 0) { ui->SetBackground(RecoveryUI::NONE); } else if (strcmp(command, "enable_reboot") == 0) { @@ -183,7 +183,7 @@ try_update_binary(const char *path, ZipArchive *zip, int* wipe_cache) { } static int -really_install_package(const char *path, int* wipe_cache, bool needs_mount) +really_install_package(const char *path, bool* wipe_cache, bool needs_mount) { ui->SetBackground(RecoveryUI::INSTALLING_UPDATE); ui->Print("Finding update package...\n"); @@ -253,7 +253,7 @@ really_install_package(const char *path, int* wipe_cache, bool needs_mount) } int -install_package(const char* path, int* wipe_cache, const char* install_file, +install_package(const char* path, bool* wipe_cache, const char* install_file, bool needs_mount) { FILE* install_log = fopen_path(install_file, "w"); diff --git a/install.h b/install.h index 53c0d312b..680499db3 100644 --- a/install.h +++ b/install.h @@ -27,7 +27,7 @@ enum { INSTALL_SUCCESS, INSTALL_ERROR, INSTALL_CORRUPT, INSTALL_NONE }; // Install the package specified by root_path. If INSTALL_SUCCESS is // returned and *wipe_cache is true on exit, caller should wipe the // cache partition. -int install_package(const char *root_path, int* wipe_cache, +int install_package(const char* root_path, bool* wipe_cache, const char* install_file, bool needs_mount); #ifdef __cplusplus diff --git a/recovery.cpp b/recovery.cpp index 51c131d9b..d27f5271e 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -825,7 +825,7 @@ static void choose_recovery_file(Device* device) { } } -static int apply_from_sdcard(Device* device, int* wipe_cache) { +static int apply_from_sdcard(Device* device, bool* wipe_cache) { if (ensure_path_mounted(SDCARD_ROOT) != 0) { ui->Print("\n-- Couldn't mount %s.\n", SDCARD_ROOT); return INSTALL_ERROR; @@ -878,7 +878,7 @@ prompt_and_wait(Device* device, int status) { // statement below. Device::BuiltinAction chosen_action = device->InvokeMenuItem(chosen_item); - int wipe_cache = 0; + bool wipe_cache = false; switch (chosen_action) { case Device::NO_ACTION: break; @@ -1010,7 +1010,9 @@ main(int argc, char **argv) { const char *send_intent = NULL; const char *update_package = NULL; - int wipe_data = 0, wipe_cache = 0, show_text = 0; + bool wipe_data = false; + bool wipe_cache = false; + bool show_text = false; bool just_exit = false; bool shutdown_after = false; @@ -1019,9 +1021,9 @@ main(int argc, char **argv) { switch (arg) { case 's': send_intent = optarg; break; case 'u': update_package = optarg; break; - case 'w': wipe_data = wipe_cache = 1; break; - case 'c': wipe_cache = 1; break; - case 't': show_text = 1; break; + case 'w': wipe_data = wipe_cache = true; break; + case 'c': wipe_cache = true; break; + case 't': show_text = true; break; case 'x': just_exit = true; break; case 'l': locale = optarg; break; case 'g': { -- cgit v1.2.3 From 30694c9e2f2610deba0ef684d0072fe9abd14ac0 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 25 Mar 2015 15:16:51 -0700 Subject: Factor out the "yes/no" menu and use it for "Wipe cache" too. It's surprising that only one of the wipe options asks for confirmation. This change makes it easier to add confirmation to any action. I've also removed the version information from all but the main menu, because I find I'm not really reading the red text because there's so much of it all the time. (Given that fingerprints are long and menu items aren't wrapped, we might want to go with an actual "About" menu item instead.) Change-Id: I7d809fdd53f9af32efc78bee618f98a69883fffe --- recovery.cpp | 95 ++++++++++++++++++++++-------------------------------------- 1 file changed, 35 insertions(+), 60 deletions(-) diff --git a/recovery.cpp b/recovery.cpp index d27f5271e..708d5f150 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -502,8 +502,7 @@ erase_volume(const char *volume) { return result; } -static const char** -prepend_title(const char* const* headers) { +static const char** prepend_title(const char* const* headers) { // count the number of lines in our title, plus the // caller-provided headers. int count = 3; // our title has 3 lines @@ -578,24 +577,15 @@ static int compare_string(const void* a, const void* b) { } // Returns a malloc'd path, or NULL. -static char* -browse_directory(const char* path, Device* device) { +static char* browse_directory(const char* path, Device* device) { ensure_path_mounted(path); - const char* MENU_HEADERS[] = { "Choose a package to install:", - path, - "", - NULL }; - DIR* d; - struct dirent* de; - d = opendir(path); + DIR* d = opendir(path); if (d == NULL) { LOGE("error opening %s: %s\n", path, strerror(errno)); return NULL; } - const char** headers = prepend_title(MENU_HEADERS); - int d_size = 0; int d_alloc = 10; char** dirs = (char**)malloc(d_alloc * sizeof(char*)); @@ -604,6 +594,7 @@ browse_directory(const char* path, Device* device) { char** zips = (char**)malloc(z_alloc * sizeof(char*)); zips[0] = strdup("../"); + struct dirent* de; while ((de = readdir(d)) != NULL) { int name_len = strlen(de->d_name); @@ -647,6 +638,8 @@ browse_directory(const char* path, Device* device) { z_size += d_size; zips[z_size] = NULL; + const char* headers[] = { "Choose a package to install:", path, "", NULL }; + char* result; int chosen_item = 0; while (true) { @@ -677,44 +670,23 @@ browse_directory(const char* path, Device* device) { } } - int i; - for (i = 0; i < z_size; ++i) free(zips[i]); + for (int i = 0; i < z_size; ++i) free(zips[i]); free(zips); - free(headers); return result; } -static void -wipe_data(int confirm, Device* device) { - if (confirm) { - static const char** title_headers = NULL; - - if (title_headers == NULL) { - const char* headers[] = { "Confirm wipe of all user data?", - " THIS CAN NOT BE UNDONE.", - "", - NULL }; - title_headers = prepend_title((const char**)headers); - } +static bool yes_no(Device* device, const char* question1, const char* question2) { + const char* headers[] = { question1, question2, "", NULL }; + const char* items[] = { " No", " Yes", NULL }; - const char* items[] = { " No", - " No", - " No", - " No", - " No", - " No", - " No", - " Yes -- delete all user data", // [7] - " No", - " No", - " No", - NULL }; - - int chosen_item = get_menu_selection(title_headers, items, 1, 0, device); - if (chosen_item != 7) { - return; - } + int chosen_item = get_menu_selection(headers, items, 1, 0, device); + return (chosen_item == 1); +} + +static void wipe_data(int confirm, Device* device) { + if (confirm && !yes_no(device, "Wipe all user data?", " THIS CAN NOT BE UNDONE!")) { + return; } ui->Print("\n-- Wiping data...\n"); @@ -725,6 +697,16 @@ wipe_data(int confirm, Device* device) { ui->Print("Data wipe complete.\n"); } +static void wipe_cache(bool should_confirm, Device* device) { + if (should_confirm && !yes_no(device, "Wipe cache?", " THIS CAN NOT BE UNDONE!")) { + return; + } + + ui->Print("\n-- Wiping cache...\n"); + erase_volume("/cache"); + ui->Print("Cache wipe complete.\n"); +} + static void file_to_ui(const char* fn) { FILE *fp = fopen_path(fn, "re"); if (fp == NULL) { @@ -782,9 +764,6 @@ static void choose_recovery_file(Device* device) { unsigned int n; static const char** title_headers = NULL; char *filename; - const char* headers[] = { "Select file to view", - "", - NULL }; // "Go back" + LAST_KMSG_FILE + KEEP_LOG_COUNT + terminating NULL entry char* entries[KEEP_LOG_COUNT + 3]; memset(entries, 0, sizeof(entries)); @@ -812,10 +791,10 @@ static void choose_recovery_file(Device* device) { entries[n++] = filename; } - title_headers = prepend_title((const char**)headers); + const char* headers[] = { "Select file to view", "", NULL }; while(1) { - int chosen_item = get_menu_selection(title_headers, entries, 1, 0, device); + int chosen_item = get_menu_selection(headers, entries, 1, 0, device); if (chosen_item == 0) break; file_to_ui(entries[chosen_item]); } @@ -878,7 +857,7 @@ prompt_and_wait(Device* device, int status) { // statement below. Device::BuiltinAction chosen_action = device->InvokeMenuItem(chosen_item); - bool wipe_cache = false; + bool should_wipe_cache = false; switch (chosen_action) { case Device::NO_ACTION: break; @@ -894,9 +873,7 @@ prompt_and_wait(Device* device, int status) { break; case Device::WIPE_CACHE: - ui->Print("\n-- Wiping cache...\n"); - erase_volume("/cache"); - ui->Print("Cache wipe complete.\n"); + wipe_cache(ui->IsTextVisible(), device); if (!ui->IsTextVisible()) return Device::NO_ACTION; break; @@ -905,15 +882,13 @@ prompt_and_wait(Device* device, int status) { { bool adb = (chosen_action == Device::APPLY_ADB_SIDELOAD); if (adb) { - status = apply_from_adb(ui, &wipe_cache, TEMPORARY_INSTALL_FILE); + status = apply_from_adb(ui, &should_wipe_cache, TEMPORARY_INSTALL_FILE); } else { - status = apply_from_sdcard(device, &wipe_cache); + status = apply_from_sdcard(device, &should_wipe_cache); } - if (status == INSTALL_SUCCESS && wipe_cache) { - ui->Print("\n-- Wiping cache (at package request)...\n"); - bool okay = erase_volume("/cache"); - ui->Print("Cache wipe %s.\n", okay ? "succeeded" : "failed"); + if (status == INSTALL_SUCCESS && should_wipe_cache) { + wipe_cache(false, device); } if (status >= 0) { -- cgit v1.2.3 From 037444642bc32d8fed3bb996823b6a62faa57195 Mon Sep 17 00:00:00 2001 From: Andres Morales Date: Mon, 30 Mar 2015 20:56:57 +0000 Subject: Revert "Erase PST partition if its marked to be erased." This now gets done at the framework level. Doing it here breaks the signature on the partition. This reverts commit ee19387905650cab5da7dd97ada5502cd17ac93d. Bug: 19967123 Change-Id: I447b926b733ca145f11a916d9569ce39889db627 --- recovery.cpp | 2 -- roots.cpp | 37 ------------------------------------- roots.h | 5 ----- 3 files changed, 44 deletions(-) diff --git a/recovery.cpp b/recovery.cpp index 708d5f150..9b3f5bba4 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -693,7 +693,6 @@ static void wipe_data(int confirm, Device* device) { device->WipeData(); erase_volume("/data"); erase_volume("/cache"); - erase_persistent_partition(); ui->Print("Data wipe complete.\n"); } @@ -1100,7 +1099,6 @@ main(int argc, char **argv) { if (device->WipeData()) status = INSTALL_ERROR; if (erase_volume("/data")) status = INSTALL_ERROR; if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR; - if (erase_persistent_partition() == -1 ) status = INSTALL_ERROR; if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n"); } else if (wipe_cache) { if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR; diff --git a/roots.cpp b/roots.cpp index 0d47577b2..ee140160c 100644 --- a/roots.cpp +++ b/roots.cpp @@ -39,8 +39,6 @@ static struct fstab *fstab = NULL; extern struct selabel_handle *sehandle; -static const char* PERSISTENT_PATH = "/persistent"; - void load_volume_table() { int i; @@ -266,41 +264,6 @@ int format_volume(const char* volume) { return -1; } -int erase_persistent_partition() { - Volume *v = volume_for_path(PERSISTENT_PATH); - if (v == NULL) { - // most devices won't have /persistent, so this is not an error. - return 0; - } - - int fd = open(v->blk_device, O_RDWR); - uint64_t size = get_file_size(fd); - if (size == 0) { - LOGE("failed to stat size of /persistent\n"); - close(fd); - return -1; - } - - char oem_unlock_enabled; - lseek(fd, size - 1, SEEK_SET); - read(fd, &oem_unlock_enabled, 1); - - if (oem_unlock_enabled) { - if (wipe_block_device(fd, size)) { - LOGE("error wiping /persistent: %s\n", strerror(errno)); - close(fd); - return -1; - } - - lseek(fd, size - 1, SEEK_SET); - write(fd, &oem_unlock_enabled, 1); - } - - close(fd); - - return (int) oem_unlock_enabled; -} - int setup_install_mounts() { if (fstab == NULL) { LOGE("can't set up install mounts: no fstab loaded\n"); diff --git a/roots.h b/roots.h index b62a5b13a..230d9ded3 100644 --- a/roots.h +++ b/roots.h @@ -46,11 +46,6 @@ int format_volume(const char* volume); // mounted (/tmp and /cache) are mounted. Returns 0 on success. int setup_install_mounts(); -// Conditionally wipes the /persistent partition if it's marked -// to wipe. Returns -1 on failure, 1 if the partition was wiped -// and 0 if the partition was not wiped. -int erase_persistent_partition(); - #ifdef __cplusplus } #endif -- cgit v1.2.3 From c679f93da3659514444fb9193d75b447cee95e2c Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Mon, 30 Mar 2015 09:43:49 -0700 Subject: Add support to enter sideload mode directly When the command file contains "--sideload" (as a result of 'adb reboot sideload'), it goes into sideload mode directly. Text display will be turned on by default. It waits for user interaction upon finishing. When the command file contains "--sideload_auto_reboot", it enters sideload mode silently. And it will reboot after the installation regardless of its result, which is designed for automated testing purpose. Change-Id: Ifdf173351221c7bbf635cfd32463b48e1fff5740 --- recovery.cpp | 75 +++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 26 deletions(-) diff --git a/recovery.cpp b/recovery.cpp index 708d5f150..a390c8afb 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -52,11 +52,13 @@ extern "C" { struct selabel_handle *sehandle; static const struct option OPTIONS[] = { - { "send_intent", required_argument, NULL, 's' }, + { "send_intent", required_argument, NULL, 'i' }, { "update_package", required_argument, NULL, 'u' }, { "wipe_data", no_argument, NULL, 'w' }, { "wipe_cache", no_argument, NULL, 'c' }, { "show_text", no_argument, NULL, 't' }, + { "sideload", no_argument, NULL, 's' }, + { "sideload_auto_reboot", no_argument, NULL, 'a' }, { "just_exit", no_argument, NULL, 'x' }, { "locale", required_argument, NULL, 'l' }, { "stages", required_argument, NULL, 'g' }, @@ -891,16 +893,14 @@ prompt_and_wait(Device* device, int status) { wipe_cache(false, device); } - if (status >= 0) { - if (status != INSTALL_SUCCESS) { - ui->SetBackground(RecoveryUI::ERROR); - ui->Print("Installation aborted.\n"); - copy_logs(); - } else if (!ui->IsTextVisible()) { - return Device::NO_ACTION; // reboot if logs aren't visible - } else { - ui->Print("\nInstall from %s complete.\n", adb ? "ADB" : "SD card"); - } + if (status != INSTALL_SUCCESS) { + ui->SetBackground(RecoveryUI::ERROR); + ui->Print("Installation aborted.\n"); + copy_logs(); + } else if (!ui->IsTextVisible()) { + return Device::NO_ACTION; // reboot if logs aren't visible + } else { + ui->Print("\nInstall from %s complete.\n", adb ? "ADB" : "SD card"); } } break; @@ -986,19 +986,23 @@ main(int argc, char **argv) { const char *send_intent = NULL; const char *update_package = NULL; bool wipe_data = false; - bool wipe_cache = false; + bool should_wipe_cache = false; bool show_text = false; + bool sideload = false; + bool sideload_auto_reboot = false; bool just_exit = false; bool shutdown_after = false; int arg; while ((arg = getopt_long(argc, argv, "", OPTIONS, NULL)) != -1) { switch (arg) { - case 's': send_intent = optarg; break; + case 'i': send_intent = optarg; break; case 'u': update_package = optarg; break; - case 'w': wipe_data = wipe_cache = true; break; - case 'c': wipe_cache = true; break; + case 'w': wipe_data = true; should_wipe_cache = true; break; + case 'c': should_wipe_cache = true; break; case 't': show_text = true; break; + case 's': sideload = true; break; + case 'a': sideload = true; sideload_auto_reboot = true; break; case 'x': just_exit = true; break; case 'l': locale = optarg; break; case 'g': { @@ -1080,11 +1084,9 @@ main(int argc, char **argv) { int status = INSTALL_SUCCESS; if (update_package != NULL) { - status = install_package(update_package, &wipe_cache, TEMPORARY_INSTALL_FILE, true); - if (status == INSTALL_SUCCESS && wipe_cache) { - if (erase_volume("/cache")) { - LOGE("Cache wipe (requested by package) failed."); - } + status = install_package(update_package, &should_wipe_cache, TEMPORARY_INSTALL_FILE, true); + if (status == INSTALL_SUCCESS && should_wipe_cache) { + wipe_cache(false, device); } if (status != INSTALL_SUCCESS) { ui->Print("Installation aborted.\n"); @@ -1099,25 +1101,46 @@ main(int argc, char **argv) { } else if (wipe_data) { if (device->WipeData()) status = INSTALL_ERROR; if (erase_volume("/data")) status = INSTALL_ERROR; - if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR; + if (should_wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR; if (erase_persistent_partition() == -1 ) status = INSTALL_ERROR; if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n"); - } else if (wipe_cache) { - if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR; + } else if (should_wipe_cache) { + if (should_wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR; if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n"); + } else if (sideload) { + // 'adb reboot sideload' acts the same as user presses key combinations + // to enter the sideload mode. When 'sideload-auto-reboot' is used, text + // display will NOT be turned on by default. And it will reboot after + // sideload finishes even if there are errors. Unless one turns on the + // text display during the installation. This is to enable automated + // testing. + if (!sideload_auto_reboot) { + ui->ShowText(true); + } + status = apply_from_adb(ui, &should_wipe_cache, TEMPORARY_INSTALL_FILE); + if (status == INSTALL_SUCCESS && should_wipe_cache) { + wipe_cache(false, device); + } + ui->Print("\nInstall from ADB complete (status: %d).\n", status); + if (sideload_auto_reboot) { + ui->Print("Rebooting automatically.\n"); + } } else if (!just_exit) { status = INSTALL_NONE; // No command specified ui->SetBackground(RecoveryUI::NO_COMMAND); } - if (status == INSTALL_ERROR || status == INSTALL_CORRUPT) { + if (!sideload_auto_reboot && (status == INSTALL_ERROR || status == INSTALL_CORRUPT)) { copy_logs(); ui->SetBackground(RecoveryUI::ERROR); } + Device::BuiltinAction after = shutdown_after ? Device::SHUTDOWN : Device::REBOOT; - if (status != INSTALL_SUCCESS || ui->IsTextVisible()) { + if ((status != INSTALL_SUCCESS && !sideload_auto_reboot) || ui->IsTextVisible()) { Device::BuiltinAction temp = prompt_and_wait(device, status); - if (temp != Device::NO_ACTION) after = temp; + if (temp != Device::NO_ACTION) { + after = temp; + } } // Save logs and clean up before rebooting or shutting down. -- cgit v1.2.3 From c277762de13653f73451e1a21fe73eafc51f918e Mon Sep 17 00:00:00 2001 From: Andres Morales Date: Mon, 30 Mar 2015 20:56:57 +0000 Subject: Revert "Erase PST partition if its marked to be erased." This now gets done at the framework level. Doing it here breaks the signature on the partition. This reverts commit ee19387905650cab5da7dd97ada5502cd17ac93d. Bug: 19967123 Change-Id: I2a977cb0f0ba94defa1bf9091219398ddc1d3528 (cherry picked from commit 037444642bc32d8fed3bb996823b6a62faa57195) --- recovery.cpp | 2 -- roots.cpp | 37 ------------------------------------- roots.h | 5 ----- 3 files changed, 44 deletions(-) diff --git a/recovery.cpp b/recovery.cpp index a390c8afb..4e43e8362 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -695,7 +695,6 @@ static void wipe_data(int confirm, Device* device) { device->WipeData(); erase_volume("/data"); erase_volume("/cache"); - erase_persistent_partition(); ui->Print("Data wipe complete.\n"); } @@ -1102,7 +1101,6 @@ main(int argc, char **argv) { if (device->WipeData()) status = INSTALL_ERROR; if (erase_volume("/data")) status = INSTALL_ERROR; if (should_wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR; - if (erase_persistent_partition() == -1 ) status = INSTALL_ERROR; if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n"); } else if (should_wipe_cache) { if (should_wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR; diff --git a/roots.cpp b/roots.cpp index 0d47577b2..ee140160c 100644 --- a/roots.cpp +++ b/roots.cpp @@ -39,8 +39,6 @@ static struct fstab *fstab = NULL; extern struct selabel_handle *sehandle; -static const char* PERSISTENT_PATH = "/persistent"; - void load_volume_table() { int i; @@ -266,41 +264,6 @@ int format_volume(const char* volume) { return -1; } -int erase_persistent_partition() { - Volume *v = volume_for_path(PERSISTENT_PATH); - if (v == NULL) { - // most devices won't have /persistent, so this is not an error. - return 0; - } - - int fd = open(v->blk_device, O_RDWR); - uint64_t size = get_file_size(fd); - if (size == 0) { - LOGE("failed to stat size of /persistent\n"); - close(fd); - return -1; - } - - char oem_unlock_enabled; - lseek(fd, size - 1, SEEK_SET); - read(fd, &oem_unlock_enabled, 1); - - if (oem_unlock_enabled) { - if (wipe_block_device(fd, size)) { - LOGE("error wiping /persistent: %s\n", strerror(errno)); - close(fd); - return -1; - } - - lseek(fd, size - 1, SEEK_SET); - write(fd, &oem_unlock_enabled, 1); - } - - close(fd); - - return (int) oem_unlock_enabled; -} - int setup_install_mounts() { if (fstab == NULL) { LOGE("can't set up install mounts: no fstab loaded\n"); diff --git a/roots.h b/roots.h index b62a5b13a..230d9ded3 100644 --- a/roots.h +++ b/roots.h @@ -46,11 +46,6 @@ int format_volume(const char* volume); // mounted (/tmp and /cache) are mounted. Returns 0 on success. int setup_install_mounts(); -// Conditionally wipes the /persistent partition if it's marked -// to wipe. Returns -1 on failure, 1 if the partition was wiped -// and 0 if the partition was not wiped. -int erase_persistent_partition(); - #ifdef __cplusplus } #endif -- cgit v1.2.3 From e39a9bc722fd41bba82390b8ac6892e49e060314 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Tue, 31 Mar 2015 12:19:05 -0700 Subject: Refactor the codes to call wipe_data/wipe_cache functions It also changes the return type of wipe_data/wipe_cache to bool, so the caller can get the status accordingly. Change-Id: I3022dcdadd6504dac757a52c2932d1176ffd1918 --- recovery.cpp | 58 ++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/recovery.cpp b/recovery.cpp index 4e43e8362..0ba4d1e27 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -686,26 +686,36 @@ static bool yes_no(Device* device, const char* question1, const char* question2) return (chosen_item == 1); } -static void wipe_data(int confirm, Device* device) { - if (confirm && !yes_no(device, "Wipe all user data?", " THIS CAN NOT BE UNDONE!")) { - return; +// Return true on success. +static bool wipe_data(int should_confirm, Device* device) { + if (should_confirm && !yes_no(device, "Wipe all user data?", " THIS CAN NOT BE UNDONE!")) { + return false; } ui->Print("\n-- Wiping data...\n"); - device->WipeData(); - erase_volume("/data"); - erase_volume("/cache"); - ui->Print("Data wipe complete.\n"); + if (device->WipeData() == 0 && erase_volume("/data") == 0 && erase_volume("/cache") == 0) { + ui->Print("Data wipe complete.\n"); + return true; + } else { + ui->Print("Data wipe failed.\n"); + return false; + } } -static void wipe_cache(bool should_confirm, Device* device) { +// Return true on success. +static bool wipe_cache(bool should_confirm, Device* device) { if (should_confirm && !yes_no(device, "Wipe cache?", " THIS CAN NOT BE UNDONE!")) { - return; + return false; } ui->Print("\n-- Wiping cache...\n"); - erase_volume("/cache"); - ui->Print("Cache wipe complete.\n"); + if (erase_volume("/cache") == 0) { + ui->Print("Cache wipe complete.\n"); + return true; + } else { + ui->Print("Cache wipe failed.\n"); + return false; + } } static void file_to_ui(const char* fn) { @@ -889,7 +899,9 @@ prompt_and_wait(Device* device, int status) { } if (status == INSTALL_SUCCESS && should_wipe_cache) { - wipe_cache(false, device); + if (!wipe_cache(false, device)) { + status = INSTALL_ERROR; + } } if (status != INSTALL_SUCCESS) { @@ -984,7 +996,7 @@ main(int argc, char **argv) { const char *send_intent = NULL; const char *update_package = NULL; - bool wipe_data = false; + bool should_wipe_data = false; bool should_wipe_cache = false; bool show_text = false; bool sideload = false; @@ -997,7 +1009,7 @@ main(int argc, char **argv) { switch (arg) { case 'i': send_intent = optarg; break; case 'u': update_package = optarg; break; - case 'w': wipe_data = true; should_wipe_cache = true; break; + case 'w': should_wipe_data = true; break; case 'c': should_wipe_cache = true; break; case 't': show_text = true; break; case 's': sideload = true; break; @@ -1097,14 +1109,14 @@ main(int argc, char **argv) { ui->ShowText(true); } } - } else if (wipe_data) { - if (device->WipeData()) status = INSTALL_ERROR; - if (erase_volume("/data")) status = INSTALL_ERROR; - if (should_wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR; - if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n"); + } else if (should_wipe_data) { + if (!wipe_data(false, device)) { + status = INSTALL_ERROR; + } } else if (should_wipe_cache) { - if (should_wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR; - if (status != INSTALL_SUCCESS) ui->Print("Cache wipe failed.\n"); + if (!wipe_cache(false, device)) { + status = INSTALL_ERROR; + } } else if (sideload) { // 'adb reboot sideload' acts the same as user presses key combinations // to enter the sideload mode. When 'sideload-auto-reboot' is used, text @@ -1117,7 +1129,9 @@ main(int argc, char **argv) { } status = apply_from_adb(ui, &should_wipe_cache, TEMPORARY_INSTALL_FILE); if (status == INSTALL_SUCCESS && should_wipe_cache) { - wipe_cache(false, device); + if (!wipe_cache(false, device)) { + status = INSTALL_ERROR; + } } ui->Print("\nInstall from ADB complete (status: %d).\n", status); if (sideload_auto_reboot) { -- cgit v1.2.3 From c94fa0b01b43709531dfbcdb94abb2a1cc23be3d Mon Sep 17 00:00:00 2001 From: Andres Morales Date: Mon, 30 Mar 2015 20:56:57 +0000 Subject: DO NOT MERGE Revert "Erase PST partition if its marked to be erased." This now gets done at the framework level. Doing it here breaks the signature on the partition. This reverts commit ee19387905650cab5da7dd97ada5502cd17ac93d. Bug: 19967123 Change-Id: I447b926b733ca145f11a916d9569ce39889db627 --- recovery.cpp | 2 -- roots.cpp | 37 ------------------------------------- roots.h | 5 ----- 3 files changed, 44 deletions(-) diff --git a/recovery.cpp b/recovery.cpp index 6deeaaaed..575e287aa 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -716,7 +716,6 @@ wipe_data(int confirm, Device* device) { device->WipeData(); erase_volume("/data"); erase_volume("/cache"); - erase_persistent_partition(); ui->Print("Data wipe complete.\n"); } @@ -1122,7 +1121,6 @@ main(int argc, char **argv) { if (device->WipeData()) status = INSTALL_ERROR; if (erase_volume("/data")) status = INSTALL_ERROR; if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR; - if (erase_persistent_partition() == -1 ) status = INSTALL_ERROR; if (status != INSTALL_SUCCESS) ui->Print("Data wipe failed.\n"); } else if (wipe_cache) { if (wipe_cache && erase_volume("/cache")) status = INSTALL_ERROR; diff --git a/roots.cpp b/roots.cpp index 0d47577b2..ee140160c 100644 --- a/roots.cpp +++ b/roots.cpp @@ -39,8 +39,6 @@ static struct fstab *fstab = NULL; extern struct selabel_handle *sehandle; -static const char* PERSISTENT_PATH = "/persistent"; - void load_volume_table() { int i; @@ -266,41 +264,6 @@ int format_volume(const char* volume) { return -1; } -int erase_persistent_partition() { - Volume *v = volume_for_path(PERSISTENT_PATH); - if (v == NULL) { - // most devices won't have /persistent, so this is not an error. - return 0; - } - - int fd = open(v->blk_device, O_RDWR); - uint64_t size = get_file_size(fd); - if (size == 0) { - LOGE("failed to stat size of /persistent\n"); - close(fd); - return -1; - } - - char oem_unlock_enabled; - lseek(fd, size - 1, SEEK_SET); - read(fd, &oem_unlock_enabled, 1); - - if (oem_unlock_enabled) { - if (wipe_block_device(fd, size)) { - LOGE("error wiping /persistent: %s\n", strerror(errno)); - close(fd); - return -1; - } - - lseek(fd, size - 1, SEEK_SET); - write(fd, &oem_unlock_enabled, 1); - } - - close(fd); - - return (int) oem_unlock_enabled; -} - int setup_install_mounts() { if (fstab == NULL) { LOGE("can't set up install mounts: no fstab loaded\n"); diff --git a/roots.h b/roots.h index b62a5b13a..230d9ded3 100644 --- a/roots.h +++ b/roots.h @@ -46,11 +46,6 @@ int format_volume(const char* volume); // mounted (/tmp and /cache) are mounted. Returns 0 on success. int setup_install_mounts(); -// Conditionally wipes the /persistent partition if it's marked -// to wipe. Returns -1 on failure, 1 if the partition was wiped -// and 0 if the partition was not wiped. -int erase_persistent_partition(); - #ifdef __cplusplus } #endif -- cgit v1.2.3 From 682c34bbc32f3a9f007dd949282651ed35d4f6e3 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Tue, 7 Apr 2015 17:16:35 -0700 Subject: Rotate logs only when there are actual operations Currently it rotates the log files every time it boots into the recovery mode. We lose useful logs after ten times. This CL changes the rotation condition so that it will rotate only if it performs some actual operations that modify the flash (installs, wipes, sideloads and etc). Bug: 19695622 Change-Id: Ie708ad955ef31aa500b6590c65faa72391705940 --- adb_install.cpp | 2 ++ common.h | 1 + install.cpp | 2 ++ recovery.cpp | 37 ++++++++++++++++++++++++++++--------- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/adb_install.cpp b/adb_install.cpp index 6d6dbb165..ed15938e2 100644 --- a/adb_install.cpp +++ b/adb_install.cpp @@ -74,6 +74,8 @@ maybe_restart_adbd() { int apply_from_adb(RecoveryUI* ui_, bool* wipe_cache, const char* install_file) { + modified_flash = true; + ui = ui_; stop_adbd(); diff --git a/common.h b/common.h index 4f1c099df..b818ceb84 100644 --- a/common.h +++ b/common.h @@ -40,6 +40,7 @@ extern "C" { #define STRINGIFY(x) #x #define EXPAND(x) STRINGIFY(x) +extern bool modified_flash; typedef struct fstab_rec Volume; // fopen a file, mounting volumes and making parent dirs as necessary. diff --git a/install.cpp b/install.cpp index 63b1d38c3..662f81c76 100644 --- a/install.cpp +++ b/install.cpp @@ -256,6 +256,8 @@ int install_package(const char* path, bool* wipe_cache, const char* install_file, bool needs_mount) { + modified_flash = true; + FILE* install_log = fopen_path(install_file, "w"); if (install_log) { fputs(path, install_log); diff --git a/recovery.cpp b/recovery.cpp index 0ba4d1e27..2f8654a84 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -92,6 +92,7 @@ char* locale = NULL; char recovery_version[PROPERTY_VALUE_MAX+1]; char* stage = NULL; char* reason = NULL; +bool modified_flash = false; /* * The recovery tool communicates with the main system through /cache files. @@ -337,13 +338,18 @@ copy_log_file(const char* source, const char* destination, int append) { // Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max // Overwrites any existing last_log.$max. -static void -rotate_last_logs(int max) { +static void rotate_last_logs(int max) { + // Logs should only be rotated once. + static bool rotated = false; + if (rotated) { + return; + } + rotated = true; + ensure_path_mounted(LAST_LOG_FILE); + char oldfn[256]; char newfn[256]; - - int i; - for (i = max-1; i >= 0; --i) { + for (int i = max-1; i >= 0; --i) { snprintf(oldfn, sizeof(oldfn), (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i); snprintf(newfn, sizeof(newfn), LAST_LOG_FILE ".%d", i+1); // ignore errors @@ -351,8 +357,17 @@ rotate_last_logs(int max) { } } -static void -copy_logs() { +static void copy_logs() { + // We only rotate and record the log of the current session if there are + // actual attempts to modify the flash, such as wipes, installs from BCB + // or menu selections. This is to avoid unnecessary rotation (and + // possible deletion) of log files, if it does not do anything loggable. + if (!modified_flash) { + return; + } + + rotate_last_logs(KEEP_LOG_COUNT); + // Copy logs to cache so the system can find out what happened. copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true); copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false); @@ -692,6 +707,8 @@ static bool wipe_data(int should_confirm, Device* device) { return false; } + modified_flash = true; + ui->Print("\n-- Wiping data...\n"); if (device->WipeData() == 0 && erase_volume("/data") == 0 && erase_volume("/cache") == 0) { ui->Print("Data wipe complete.\n"); @@ -708,6 +725,8 @@ static bool wipe_cache(bool should_confirm, Device* device) { return false; } + modified_flash = true; + ui->Print("\n-- Wiping cache...\n"); if (erase_volume("/cache") == 0) { ui->Print("Cache wipe complete.\n"); @@ -816,6 +835,8 @@ static void choose_recovery_file(Device* device) { } static int apply_from_sdcard(Device* device, bool* wipe_cache) { + modified_flash = true; + if (ensure_path_mounted(SDCARD_ROOT) != 0) { ui->Print("\n-- Couldn't mount %s.\n", SDCARD_ROOT); return INSTALL_ERROR; @@ -990,8 +1011,6 @@ main(int argc, char **argv) { printf("Starting recovery (pid %d) on %s", getpid(), ctime(&start)); load_volume_table(); - ensure_path_mounted(LAST_LOG_FILE); - rotate_last_logs(KEEP_LOG_COUNT); get_args(&argc, &argv); const char *send_intent = NULL; -- cgit v1.2.3 From 9ad9d66f818fc9a86eacaac988b1ad72c6b27696 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 8 Apr 2015 12:08:32 -0700 Subject: Remove a couple of unused inlines from minzip/Zip.h. Change-Id: I805883e3863673416898bdef39c5703ca33f18e0 --- minzip/Zip.h | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/minzip/Zip.h b/minzip/Zip.h index a2b2c26fc..86d8db597 100644 --- a/minzip/Zip.h +++ b/minzip/Zip.h @@ -85,22 +85,12 @@ void mzCloseZipArchive(ZipArchive* pArchive); const ZipEntry* mzFindZipEntry(const ZipArchive* pArchive, const char* entryName); -/* - * Get the number of entries in the Zip archive. - */ -INLINE unsigned int mzZipEntryCount(const ZipArchive* pArchive) { - return pArchive->numEntries; -} - INLINE long mzGetZipEntryOffset(const ZipEntry* pEntry) { return pEntry->offset; } INLINE long mzGetZipEntryUncompLen(const ZipEntry* pEntry) { return pEntry->uncompLen; } -INLINE long mzGetZipEntryCrc32(const ZipEntry* pEntry) { - return pEntry->crc32; -} /* * Type definition for the callback function used by -- cgit v1.2.3 From aa0d6afb61f4cf928e87c7a21bcb59fc973f15a0 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 8 Apr 2015 12:42:50 -0700 Subject: Remove the fixed screen size assumptions. Dynamically allocate the text and menu arrays instead. Change-Id: Idbfc3fe4e4b50db4fee62ac2b6a7323cad369749 --- screen_ui.cpp | 56 +++++++++++++++++++++++++++++++++----------------------- screen_ui.h | 13 ++++--------- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/screen_ui.cpp b/screen_ui.cpp index edc41abc0..a0df455cd 100644 --- a/screen_ui.cpp +++ b/screen_ui.cpp @@ -40,25 +40,26 @@ static int char_height; // There's only (at most) one of these objects, and global callbacks // (for pthread_create, and the input event system) need to find it, // so use a global variable. -static ScreenRecoveryUI* self = NULL; +static ScreenRecoveryUI* self = nullptr; // Return the current time as a double (including fractions of a second). static double now() { struct timeval tv; - gettimeofday(&tv, NULL); + gettimeofday(&tv, nullptr); return tv.tv_sec + tv.tv_usec / 1000000.0; } ScreenRecoveryUI::ScreenRecoveryUI() : currentIcon(NONE), installingFrame(0), - locale(NULL), + locale(nullptr), rtl_locale(false), progressBarType(EMPTY), progressScopeStart(0), progressScopeSize(0), progress(0), pagesIdentical(false), + text(nullptr), text_cols(0), text_rows(0), text_col(0), @@ -66,6 +67,7 @@ ScreenRecoveryUI::ScreenRecoveryUI() : text_top(0), show_text(false), show_text_ever(false), + menu(nullptr), show_menu(false), menu_top(0), menu_items(0), @@ -75,12 +77,10 @@ ScreenRecoveryUI::ScreenRecoveryUI() : stage(-1), max_stage(-1) { - for (int i = 0; i < 5; i++) - backgroundIcon[i] = NULL; - - memset(text, 0, sizeof(text)); - - pthread_mutex_init(&updateMutex, NULL); + for (int i = 0; i < 5; i++) { + backgroundIcon[i] = nullptr; + } + pthread_mutex_init(&updateMutex, nullptr); self = this; } @@ -247,7 +247,8 @@ void ScreenRecoveryUI::draw_screen_locked() // screen, the bottom of the menu, or we've displayed the // entire text buffer. int row = (text_top+text_rows-1) % text_rows; - for (int ty = gr_fb_height() - char_height, count = 0; + size_t count = 0; + for (int ty = gr_fb_height() - char_height; ty > y+2 && count < text_rows; ty -= char_height, ++count) { gr_text(0, ty, text[row], 0); @@ -281,7 +282,7 @@ void ScreenRecoveryUI::update_progress_locked() // Keeps the progress bar updated, even when the process is otherwise busy. void* ScreenRecoveryUI::progress_thread(void *cookie) { self->progress_loop(); - return NULL; + return nullptr; } void ScreenRecoveryUI::progress_loop() { @@ -344,23 +345,32 @@ void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, gr_surface* sur } } +static char** Alloc2d(size_t rows, size_t cols) { + char** result = new char*[rows]; + for (size_t i = 0; i < rows; ++i) { + result[i] = new char[cols]; + memset(result[i], 0, cols); + } + return result; +} + void ScreenRecoveryUI::Init() { gr_init(); gr_font_size(&char_width, &char_height); + text_rows = gr_fb_height() / char_height; + text_cols = gr_fb_width() / char_width; + + text = Alloc2d(text_rows, text_cols + 1); + menu = Alloc2d(text_rows, text_cols + 1); text_col = text_row = 0; - text_rows = gr_fb_height() / char_height; - if (text_rows > kMaxRows) text_rows = kMaxRows; text_top = 1; - text_cols = gr_fb_width() / char_width; - if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1; - - backgroundIcon[NONE] = NULL; + backgroundIcon[NONE] = nullptr; LoadBitmapArray("icon_installing", &installing_frames, &installation); - backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : NULL; + backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : nullptr; backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE]; LoadBitmap("icon_error", &backgroundIcon[ERROR]); backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR]; @@ -375,7 +385,7 @@ void ScreenRecoveryUI::Init() LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]); LoadLocalizedBitmap("error_text", &backgroundText[ERROR]); - pthread_create(&progress_t, NULL, progress_thread, NULL); + pthread_create(&progress_t, nullptr, progress_thread, nullptr); RecoveryUI::Init(); } @@ -402,7 +412,7 @@ void ScreenRecoveryUI::SetLocale(const char* new_locale) { } free(lang); } else { - new_locale = NULL; + new_locale = nullptr; } } @@ -496,17 +506,17 @@ void ScreenRecoveryUI::Print(const char *fmt, ...) void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items, int initial_selection) { - int i; pthread_mutex_lock(&updateMutex); if (text_rows > 0 && text_cols > 0) { + size_t i; for (i = 0; i < text_rows; ++i) { - if (headers[i] == NULL) break; + if (headers[i] == nullptr) break; strncpy(menu[i], headers[i], text_cols-1); menu[i][text_cols-1] = '\0'; } menu_top = i; for (; i < text_rows; ++i) { - if (items[i-menu_top] == NULL) break; + if (items[i-menu_top] == nullptr) break; strncpy(menu[i], items[i-menu_top], text_cols-1); menu[i][text_cols-1] = '\0'; } diff --git a/screen_ui.h b/screen_ui.h index 01a33bfe2..210fd3e17 100644 --- a/screen_ui.h +++ b/screen_ui.h @@ -84,17 +84,14 @@ class ScreenRecoveryUI : public RecoveryUI { // progress bar) bool pagesIdentical; - static const int kMaxCols = 96; - static const int kMaxRows = 96; - // Log text overlay, displayed when a magic key is pressed - char text[kMaxRows][kMaxCols]; - int text_cols, text_rows; - int text_col, text_row, text_top; + char** text; + size_t text_cols, text_rows; + size_t text_col, text_row, text_top; bool show_text; bool show_text_ever; // has show_text ever been true? - char menu[kMaxRows][kMaxCols]; + char** menu; bool show_menu; int menu_top, menu_items, menu_sel; @@ -102,8 +99,6 @@ class ScreenRecoveryUI : public RecoveryUI { int animation_fps; int installing_frames; - protected: - private: int iconX, iconY; -- cgit v1.2.3 From 018ed31c515de2c840dedaebb078ab455bf37ae8 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 8 Apr 2015 16:51:36 -0700 Subject: Enable printf format argument checking. The original attempt missed the fact that Print is a member function, so the first argument is the implicit 'this'. Change-Id: I963b668c5432804c767f0a2e3ef7dea5978a1218 --- adb_install.cpp | 2 +- recovery.cpp | 2 +- screen_ui.h | 4 ++-- ui.h | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/adb_install.cpp b/adb_install.cpp index ed15938e2..9e605e2d5 100644 --- a/adb_install.cpp +++ b/adb_install.cpp @@ -109,7 +109,7 @@ apply_from_adb(RecoveryUI* ui_, bool* wipe_cache, const char* install_file) { sleep(1); continue; } else { - ui->Print("\nTimed out waiting for package.\n\n", strerror(errno)); + ui->Print("\nTimed out waiting for package.\n\n"); result = INSTALL_ERROR; kill(child, SIGKILL); break; diff --git a/recovery.cpp b/recovery.cpp index 2f8654a84..1726a227e 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -844,7 +844,7 @@ static int apply_from_sdcard(Device* device, bool* wipe_cache) { char* path = browse_directory(SDCARD_ROOT, device); if (path == NULL) { - ui->Print("\n-- No package file selected.\n", path); + ui->Print("\n-- No package file selected.\n"); return INSTALL_ERROR; } diff --git a/screen_ui.h b/screen_ui.h index 210fd3e17..82647ac75 100644 --- a/screen_ui.h +++ b/screen_ui.h @@ -47,11 +47,11 @@ class ScreenRecoveryUI : public RecoveryUI { bool WasTextEverVisible(); // printing messages - void Print(const char* fmt, ...); // __attribute__((format(printf, 1, 2))); + void Print(const char* fmt, ...) __printflike(2, 3); // menu display void StartMenu(const char* const * headers, const char* const * items, - int initial_selection); + int initial_selection); int SelectMenu(int sel); void EndMenu(); diff --git a/ui.h b/ui.h index 31a8a7fb1..a0580b703 100644 --- a/ui.h +++ b/ui.h @@ -63,7 +63,7 @@ class RecoveryUI { // Write a message to the on-screen log (shown if the user has // toggled on the text display). - virtual void Print(const char* fmt, ...) = 0; // __attribute__((format(printf, 1, 2))) = 0; + virtual void Print(const char* fmt, ...) __printflike(2, 3) = 0; // --- key handling --- -- cgit v1.2.3 From 8de52078a42882873322b19becb42612f7708b54 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 8 Apr 2015 20:06:50 -0700 Subject: Move file paging into ScreenRecoveryUI. This fixes the N9 performance problem. Change-Id: I00c10d4162ff266a6243285e5a5e768217f6f799 --- recovery.cpp | 63 +++-------------------------- screen_ui.cpp | 119 +++++++++++++++++++++++++++++++++++++++--------------- screen_ui.h | 8 ++-- ui.h | 6 ++- verifier_test.cpp | 3 ++ 5 files changed, 104 insertions(+), 95 deletions(-) diff --git a/recovery.cpp b/recovery.cpp index 1726a227e..7776f1f31 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -84,9 +84,6 @@ static const char *LAST_KMSG_FILE = "/cache/recovery/last_kmsg"; #define KEEP_LOG_COUNT 10 -// Number of lines per page when displaying a file on screen -#define LINES_PER_PAGE 30 - RecoveryUI* ui = NULL; char* locale = NULL; char recovery_version[PROPERTY_VALUE_MAX+1]; @@ -737,58 +734,6 @@ static bool wipe_cache(bool should_confirm, Device* device) { } } -static void file_to_ui(const char* fn) { - FILE *fp = fopen_path(fn, "re"); - if (fp == NULL) { - ui->Print(" Unable to open %s: %s\n", fn, strerror(errno)); - return; - } - char line[1024]; - int ct = 0; - int key = 0; - redirect_stdio("/dev/null"); - while(fgets(line, sizeof(line), fp) != NULL) { - ui->Print("%s", line); - ct++; - if (ct % LINES_PER_PAGE == 0) { - // give the user time to glance at the entries - key = ui->WaitKey(); - - if (key == KEY_POWER) { - break; - } - - if (key == KEY_VOLUMEUP) { - // Go back by seeking to the beginning and dumping ct - n - // lines. It's ugly, but this way we don't need to store - // the previous offsets. The files we're dumping here aren't - // expected to be very large. - int i; - - ct -= 2 * LINES_PER_PAGE; - if (ct < 0) { - ct = 0; - } - fseek(fp, 0, SEEK_SET); - for (i = 0; i < ct; i++) { - fgets(line, sizeof(line), fp); - } - ui->Print("^^^^^^^^^^\n"); - } - } - } - - // If the user didn't abort, then give the user time to glance at - // the end of the log, sorry, no rewind here - if (key != KEY_POWER) { - ui->Print("\n--END-- (press any key)\n"); - ui->WaitKey(); - } - - redirect_stdio(TEMPORARY_LOG_FILE); - fclose(fp); -} - static void choose_recovery_file(Device* device) { unsigned int i; unsigned int n; @@ -823,10 +768,14 @@ static void choose_recovery_file(Device* device) { const char* headers[] = { "Select file to view", "", NULL }; - while(1) { + while (true) { int chosen_item = get_menu_selection(headers, entries, 1, 0, device); if (chosen_item == 0) break; - file_to_ui(entries[chosen_item]); + + // TODO: do we need to redirect? ShowFile could just avoid writing to stdio. + redirect_stdio("/dev/null"); + ui->ShowFile(entries[chosen_item]); + redirect_stdio(TEMPORARY_LOG_FILE); } for (i = 0; i < (sizeof(entries) / sizeof(*entries)); i++) { diff --git a/screen_ui.cpp b/screen_ui.cpp index a0df455cd..6d8df68b3 100644 --- a/screen_ui.cpp +++ b/screen_ui.cpp @@ -86,8 +86,7 @@ ScreenRecoveryUI::ScreenRecoveryUI() : // 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) -{ +void ScreenRecoveryUI::draw_background_locked(Icon icon) { pagesIdentical = false; gr_color(0, 0, 0, 255); gr_clear(); @@ -132,8 +131,7 @@ void ScreenRecoveryUI::draw_background_locked(Icon icon) // Draw the progress bar (if any) on the screen. Does not flip pages. // Should only be called with updateMutex locked. -void ScreenRecoveryUI::draw_progress_locked() -{ +void ScreenRecoveryUI::draw_progress_locked() { if (currentIcon == ERROR) return; if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) { @@ -204,8 +202,7 @@ void ScreenRecoveryUI::SetColor(UIElement e) { // Redraw everything on the screen. Does not flip pages. // Should only be called with updateMutex locked. -void ScreenRecoveryUI::draw_screen_locked() -{ +void ScreenRecoveryUI::draw_screen_locked() { if (!show_text) { draw_background_locked(currentIcon); draw_progress_locked(); @@ -260,16 +257,14 @@ void ScreenRecoveryUI::draw_screen_locked() // Redraw everything on the screen and flip the screen (make it visible). // Should only be called with updateMutex locked. -void ScreenRecoveryUI::update_screen_locked() -{ +void ScreenRecoveryUI::update_screen_locked() { draw_screen_locked(); gr_flip(); } // Updates only the progress bar, if possible, otherwise redraws the screen. // Should only be called with updateMutex locked. -void ScreenRecoveryUI::update_progress_locked() -{ +void ScreenRecoveryUI::update_progress_locked() { if (show_text || !pagesIdentical) { draw_screen_locked(); // Must redraw the whole screen pagesIdentical = true; @@ -354,8 +349,7 @@ static char** Alloc2d(size_t rows, size_t cols) { return result; } -void ScreenRecoveryUI::Init() -{ +void ScreenRecoveryUI::Init() { gr_init(); gr_font_size(&char_width, &char_height); @@ -416,8 +410,7 @@ void ScreenRecoveryUI::SetLocale(const char* new_locale) { } } -void ScreenRecoveryUI::SetBackground(Icon icon) -{ +void ScreenRecoveryUI::SetBackground(Icon icon) { pthread_mutex_lock(&updateMutex); currentIcon = icon; @@ -426,8 +419,7 @@ void ScreenRecoveryUI::SetBackground(Icon icon) pthread_mutex_unlock(&updateMutex); } -void ScreenRecoveryUI::SetProgressType(ProgressType type) -{ +void ScreenRecoveryUI::SetProgressType(ProgressType type) { pthread_mutex_lock(&updateMutex); if (progressBarType != type) { progressBarType = type; @@ -439,8 +431,7 @@ void ScreenRecoveryUI::SetProgressType(ProgressType type) pthread_mutex_unlock(&updateMutex); } -void ScreenRecoveryUI::ShowProgress(float portion, float seconds) -{ +void ScreenRecoveryUI::ShowProgress(float portion, float seconds) { pthread_mutex_lock(&updateMutex); progressBarType = DETERMINATE; progressScopeStart += progressScopeSize; @@ -452,8 +443,7 @@ void ScreenRecoveryUI::ShowProgress(float portion, float seconds) pthread_mutex_unlock(&updateMutex); } -void ScreenRecoveryUI::SetProgress(float fraction) -{ +void ScreenRecoveryUI::SetProgress(float fraction) { pthread_mutex_lock(&updateMutex); if (fraction < 0.0) fraction = 0.0; if (fraction > 1.0) fraction = 1.0; @@ -476,8 +466,7 @@ void ScreenRecoveryUI::SetStage(int current, int max) { pthread_mutex_unlock(&updateMutex); } -void ScreenRecoveryUI::Print(const char *fmt, ...) -{ +void ScreenRecoveryUI::Print(const char *fmt, ...) { char buf[256]; va_list ap; va_start(ap, fmt); @@ -486,10 +475,9 @@ void ScreenRecoveryUI::Print(const char *fmt, ...) fputs(buf, stdout); - // This can get called before ui_init(), so be careful. pthread_mutex_lock(&updateMutex); if (text_rows > 0 && text_cols > 0) { - for (char* ptr = buf; *ptr != '\0'; ++ptr) { + for (const char* ptr = buf; *ptr != '\0'; ++ptr) { if (*ptr == '\n' || text_col >= text_cols) { text[text_row][text_col] = '\0'; text_col = 0; @@ -504,6 +492,75 @@ void ScreenRecoveryUI::Print(const char *fmt, ...) pthread_mutex_unlock(&updateMutex); } +// TODO: replace this with something not line-based so we can wrap correctly without getting +// confused about what line we're on. +void ScreenRecoveryUI::print_no_update(const char* s) { + pthread_mutex_lock(&updateMutex); + if (text_rows > 0 && text_cols > 0) { + for (const char* ptr = s; *ptr != '\0'; ++ptr) { + if (*ptr == '\n' || text_col >= text_cols) { + text[text_row][text_col] = '\0'; + text_col = 0; + text_row = (text_row + 1) % text_rows; + if (text_row == text_top) text_top = (text_top + 1) % text_rows; + } + if (*ptr != '\n') text[text_row][text_col++] = *ptr; + } + text[text_row][text_col] = '\0'; + } + pthread_mutex_unlock(&updateMutex); +} + +void ScreenRecoveryUI::ShowFile(const char* filename) { + FILE* fp = fopen_path(filename, "re"); + if (fp == nullptr) { + Print(" Unable to open %s: %s\n", filename, strerror(errno)); + return; + } + + char line[1024]; + int ct = 0; + int key = 0; + while (fgets(line, sizeof(line), fp) != nullptr) { + print_no_update(line); + ct++; + if (ct % text_rows == 0) { + Redraw(); + + // give the user time to glance at the entries + key = WaitKey(); + + if (key == KEY_POWER) { + break; + } else if (key == KEY_VOLUMEUP) { + // Go back by seeking to the beginning and dumping ct - n + // lines. It's ugly, but this way we don't need to store + // the previous offsets. The files we're dumping here aren't + // expected to be very large. + ct -= 2 * text_rows; + if (ct < 0) { + ct = 0; + } + fseek(fp, 0, SEEK_SET); + for (int i = 0; i < ct; i++) { + fgets(line, sizeof(line), fp); + } + Print("^^^^^^^^^^\n"); + } else { + // Next page. + } + } + } + + // If the user didn't abort, then give the user time to glance at + // the end of the log, sorry, no rewind here + if (key != KEY_POWER) { + Print("\n--END-- (press any key)\n"); + WaitKey(); + } + fclose(fp); +} + void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items, int initial_selection) { pthread_mutex_lock(&updateMutex); @@ -554,33 +611,29 @@ void ScreenRecoveryUI::EndMenu() { pthread_mutex_unlock(&updateMutex); } -bool ScreenRecoveryUI::IsTextVisible() -{ +bool ScreenRecoveryUI::IsTextVisible() { pthread_mutex_lock(&updateMutex); int visible = show_text; pthread_mutex_unlock(&updateMutex); return visible; } -bool ScreenRecoveryUI::WasTextEverVisible() -{ +bool ScreenRecoveryUI::WasTextEverVisible() { pthread_mutex_lock(&updateMutex); int ever_visible = show_text_ever; pthread_mutex_unlock(&updateMutex); return ever_visible; } -void ScreenRecoveryUI::ShowText(bool visible) -{ +void ScreenRecoveryUI::ShowText(bool visible) { pthread_mutex_lock(&updateMutex); show_text = visible; - if (show_text) show_text_ever = 1; + if (show_text) show_text_ever = true; update_screen_locked(); pthread_mutex_unlock(&updateMutex); } -void ScreenRecoveryUI::Redraw() -{ +void ScreenRecoveryUI::Redraw() { pthread_mutex_lock(&updateMutex); update_screen_locked(); pthread_mutex_unlock(&updateMutex); diff --git a/screen_ui.h b/screen_ui.h index 82647ac75..41ff4af11 100644 --- a/screen_ui.h +++ b/screen_ui.h @@ -48,6 +48,7 @@ class ScreenRecoveryUI : public RecoveryUI { // printing messages void Print(const char* fmt, ...) __printflike(2, 3); + void ShowFile(const char* filename); // menu display void StartMenu(const char* const * headers, const char* const * items, @@ -80,11 +81,10 @@ class ScreenRecoveryUI : public RecoveryUI { float progressScopeStart, progressScopeSize, progress; double progressScopeTime, progressScopeDuration; - // true when both graphics pages are the same (except for the - // progress bar) + // true when both graphics pages are the same (except for the progress bar). bool pagesIdentical; - // Log text overlay, displayed when a magic key is pressed + // Log text overlay, displayed when a magic key is pressed. char** text; size_t text_cols, text_rows; size_t text_col, text_row, text_top; @@ -112,6 +112,8 @@ class ScreenRecoveryUI : public RecoveryUI { static void* progress_thread(void* cookie); void progress_loop(); + void print_no_update(const char*); + 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/ui.h b/ui.h index a0580b703..3b217745f 100644 --- a/ui.h +++ b/ui.h @@ -31,10 +31,10 @@ class RecoveryUI { // Initialize the object; called before anything else. virtual void Init(); // Show a stage indicator. Call immediately after Init(). - virtual void SetStage(int current, int max) { } + virtual void SetStage(int current, int max) = 0; // After calling Init(), you can tell the UI what locale it is operating in. - virtual void SetLocale(const char* locale) { } + virtual void SetLocale(const char* locale) = 0; // Set the overall recovery state ("background image"). enum Icon { NONE, INSTALLING_UPDATE, ERASING, NO_COMMAND, ERROR }; @@ -65,6 +65,8 @@ class RecoveryUI { // toggled on the text display). virtual void Print(const char* fmt, ...) __printflike(2, 3) = 0; + virtual void ShowFile(const char* filename) = 0; + // --- key handling --- // Wait for keypress and return it. May return -1 after timeout. diff --git a/verifier_test.cpp b/verifier_test.cpp index 93a071e37..82546edce 100644 --- a/verifier_test.cpp +++ b/verifier_test.cpp @@ -124,6 +124,8 @@ RecoveryUI* ui = NULL; // nothing but print. class FakeUI : public RecoveryUI { void Init() { } + void SetStage(int, int) { } + void SetLocale(const char*) { } void SetBackground(Icon icon) { } void SetProgressType(ProgressType determinate) { } @@ -139,6 +141,7 @@ class FakeUI : public RecoveryUI { vfprintf(stderr, fmt, ap); va_end(ap); } + void ShowFile(const char*) { } void StartMenu(const char* const * headers, const char* const * items, int initial_selection) { } -- cgit v1.2.3 From 42c12306c90e9ac467262f838dba30a5ff81957e Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Thu, 9 Apr 2015 11:50:56 -0700 Subject: Remove some commented-out code. Change-Id: Ifb466ee2a89da88832c04086fa43da2b8409c232 --- device.h | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/device.h b/device.h index 8ff4ec031..02f956489 100644 --- a/device.h +++ b/device.h @@ -34,16 +34,6 @@ class Device { // before anything else). virtual void StartRecovery() { }; - // enum KeyAction { NONE, TOGGLE, REBOOT }; - - // // Called in the input thread when a new key (key_code) is - // // pressed. *key_pressed is an array of KEY_MAX+1 bytes - // // indicating which other keys are already pressed. Return a - // // KeyAction to indicate action should be taken immediately. - // // These actions happen when recovery is not waiting for input - // // (eg, in the midst of installing a package). - // virtual KeyAction CheckImmediateKeyAction(volatile char* key_pressed, int key_code) = 0; - // Called from the main thread when recovery is at the main menu // and waiting for input, and a key is pressed. (Note that "at" // the main menu does not necessarily mean the menu is visible; -- cgit v1.2.3 From 9e7ae8a62652258f3ecbf147b578b73286f6d4d8 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Thu, 9 Apr 2015 13:40:31 -0700 Subject: Move default implementations into Device. The current abstract class was a nice idea but has led to a lot of copy & paste in practice. Right now, no one we know of has any extra menu items, so let's make the default menu available to everyone. (If we assume that someone somewhere really does need custom device-specific menu options, a better API would let them add to our menu rather than replacing it.) Change-Id: I59f6a92f3ecd830c2ce78ce9da19eaaf472c5dfa --- Android.mk | 11 ++++--- default_device.cpp | 90 ++++++++++++++---------------------------------------- device.cpp | 54 ++++++++++++++++++++++++++++++++ device.h | 28 +++++++++-------- 4 files changed, 99 insertions(+), 84 deletions(-) create mode 100644 device.cpp diff --git a/Android.mk b/Android.mk index dd1e96e7a..a567aa5a2 100644 --- a/Android.mk +++ b/Android.mk @@ -30,16 +30,17 @@ include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_SRC_FILES := \ - recovery.cpp \ + adb_install.cpp \ + asn1_decoder.cpp \ bootloader.cpp \ + device.cpp \ + fuse_sdcard_provider.c \ install.cpp \ + recovery.cpp \ roots.cpp \ - ui.cpp \ screen_ui.cpp \ - asn1_decoder.cpp \ + ui.cpp \ verifier.cpp \ - adb_install.cpp \ - fuse_sdcard_provider.c LOCAL_MODULE := recovery diff --git a/default_device.cpp b/default_device.cpp index ed601f6c6..d7dd45283 100644 --- a/default_device.cpp +++ b/default_device.cpp @@ -14,80 +14,36 @@ * limitations under the License. */ -#include - -#include "common.h" #include "device.h" #include "screen_ui.h" -static const char* HEADERS[] = { - "Volume up/down to move highlight.", - "Power button to select.", - "", - NULL -}; - -static const char* ITEMS[] = { - "Reboot system now", - "Reboot to bootloader", - "Apply update from ADB", - "Apply update from SD card", - "Wipe data/factory reset", - "Wipe cache partition", - "View recovery logs", - "Power off", - NULL -}; - class DefaultDevice : public Device { - public: - DefaultDevice() : - ui(new ScreenRecoveryUI) { - } - - RecoveryUI* GetUI() { return ui; } - - int HandleMenuKey(int key, int visible) { - if (visible) { - switch (key) { - case KEY_DOWN: - case KEY_VOLUMEDOWN: - return kHighlightDown; - - case KEY_UP: - case KEY_VOLUMEUP: - return kHighlightUp; - - case KEY_ENTER: - case KEY_POWER: - return kInvokeItem; - } - } - - return kNoAction; + public: + DefaultDevice() : Device(new ScreenRecoveryUI) { + } + + // TODO: make this handle more cases, and move the default implementation into Device too. + int HandleMenuKey(int key, int visible) { + if (visible) { + switch (key) { + case KEY_DOWN: + case KEY_VOLUMEDOWN: + return kHighlightDown; + + case KEY_UP: + case KEY_VOLUMEUP: + return kHighlightUp; + + case KEY_ENTER: + case KEY_POWER: + return kInvokeItem; + } } - BuiltinAction InvokeMenuItem(int menu_position) { - switch (menu_position) { - case 0: return REBOOT; - case 1: return REBOOT_BOOTLOADER; - case 2: return APPLY_ADB_SIDELOAD; - case 3: return APPLY_EXT; - case 4: return WIPE_DATA; - case 5: return WIPE_CACHE; - case 6: return READ_RECOVERY_LASTLOG; - case 7: return SHUTDOWN; - default: return NO_ACTION; - } - } - - const char* const* GetMenuHeaders() { return HEADERS; } - const char* const* GetMenuItems() { return ITEMS; } - - private: - RecoveryUI* ui; + return kNoAction; + } }; Device* make_device() { - return new DefaultDevice(); + return new DefaultDevice; } diff --git a/device.cpp b/device.cpp new file mode 100644 index 000000000..20a763f2b --- /dev/null +++ b/device.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2015 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 "device.h" + +// TODO: this is a lie for, say, fugu. +static const char* HEADERS[] = { + "Volume up/down to move highlight.", + "Power button to select.", + "", + NULL +}; + +static const char* ITEMS[] = { + "Reboot system now", + "Reboot to bootloader", + "Apply update from ADB", + "Apply update from SD card", + "Wipe data/factory reset", + "Wipe cache partition", + "View recovery logs", + "Power off", + NULL +}; + +const char* const* Device::GetMenuHeaders() { return HEADERS; } +const char* const* Device::GetMenuItems() { return ITEMS; } + +Device::BuiltinAction Device::InvokeMenuItem(int menu_position) { + switch (menu_position) { + case 0: return REBOOT; + case 1: return REBOOT_BOOTLOADER; + case 2: return APPLY_ADB_SIDELOAD; + case 3: return APPLY_EXT; + case 4: return WIPE_DATA; + case 5: return WIPE_CACHE; + case 6: return READ_RECOVERY_LASTLOG; + case 7: return SHUTDOWN; + default: return NO_ACTION; + } +} diff --git a/device.h b/device.h index 8ff4ec031..a24540066 100644 --- a/device.h +++ b/device.h @@ -21,13 +21,14 @@ class Device { public: + Device(RecoveryUI* ui) : ui_(ui) { } virtual ~Device() { } // Called to obtain the UI object that should be used to display // the recovery user interface for this device. You should not // have called Init() on the UI object already, the caller will do // that after this method returns. - virtual RecoveryUI* GetUI() = 0; + virtual RecoveryUI* GetUI() { return ui_; } // Called when recovery starts up (after the UI has been obtained // and initialized and after the arguments have been parsed, but @@ -70,6 +71,17 @@ class Device { APPLY_ADB_SIDELOAD, WIPE_DATA, WIPE_CACHE, REBOOT_BOOTLOADER, SHUTDOWN, READ_RECOVERY_LASTLOG }; + // Return the headers (an array of strings, one per line, + // NULL-terminated) for the main menu. Typically these tell users + // what to push to move the selection and invoke the selected + // item. + virtual const char* const* GetMenuHeaders(); + + // Return the list of menu items (an array of strings, + // NULL-terminated). The menu_position passed to InvokeMenuItem + // will correspond to the indexes into this array. + virtual const char* const* GetMenuItems(); + // Perform a recovery action selected from the menu. // 'menu_position' will be the item number of the selected menu // item, or a non-negative number returned from @@ -79,7 +91,7 @@ class Device { // builtin actions, you can just return the corresponding enum // value. If it is an action specific to your device, you // actually perform it here and return NO_ACTION. - virtual BuiltinAction InvokeMenuItem(int menu_position) = 0; + virtual BuiltinAction InvokeMenuItem(int menu_position); static const int kNoAction = -1; static const int kHighlightUp = -2; @@ -94,16 +106,8 @@ class Device { // are erased AFTER this returns (whether it returns success or not). virtual int WipeData() { return 0; } - // Return the headers (an array of strings, one per line, - // NULL-terminated) for the main menu. Typically these tell users - // what to push to move the selection and invoke the selected - // item. - virtual const char* const* GetMenuHeaders() = 0; - - // Return the list of menu items (an array of strings, - // NULL-terminated). The menu_position passed to InvokeMenuItem - // will correspond to the indexes into this array. - virtual const char* const* GetMenuItems() = 0; + private: + RecoveryUI* ui_; }; // The device-specific library must define this function (or the -- cgit v1.2.3 From bb78d6286dedea56deca4e97bdf319cc1dc8c613 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Thu, 9 Apr 2015 20:51:08 -0700 Subject: Move the recovery image version out of the menu header. Rather than add code to wrap menu items, let's just put output the recovery version to the log. It'll be visible at the bottom of the screen and automatically wrap. Change-Id: I158fe2d85bc56b195e00619fba455321743923bd --- recovery.cpp | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) diff --git a/recovery.cpp b/recovery.cpp index 7776f1f31..bbfeda4a6 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -86,7 +86,6 @@ static const char *LAST_KMSG_FILE = "/cache/recovery/last_kmsg"; RecoveryUI* ui = NULL; char* locale = NULL; -char recovery_version[PROPERTY_VALUE_MAX+1]; char* stage = NULL; char* reason = NULL; bool modified_flash = false; @@ -516,24 +515,6 @@ erase_volume(const char *volume) { return result; } -static const char** prepend_title(const char* const* headers) { - // count the number of lines in our title, plus the - // caller-provided headers. - int count = 3; // our title has 3 lines - const char* const* p; - for (p = headers; *p; ++p, ++count); - - const char** new_headers = (const char**)malloc((count+1) * sizeof(char*)); - const char** h = new_headers; - *(h++) = "Android system recovery (API " EXPAND(RECOVERY_API_VERSION) ")"; - *(h++) = recovery_version; - *(h++) = ""; - for (p = headers; *p; ++p, ++h) *h = *p; - *h = NULL; - - return new_headers; -} - static int get_menu_selection(const char* const * headers, const char* const * items, int menu_only, int initial_selection, Device* device) { @@ -814,7 +795,7 @@ static int apply_from_sdcard(Device* device, bool* wipe_cache) { // on if the --shutdown_after flag was passed to recovery. static Device::BuiltinAction prompt_and_wait(Device* device, int status) { - const char* const* headers = prepend_title(device->GetMenuHeaders()); + const char* const* headers = device->GetMenuHeaders(); for (;;) { finish_recovery(NULL); @@ -1057,9 +1038,14 @@ main(int argc, char **argv) { printf("\n"); property_list(print_property, NULL); - property_get("ro.build.display.id", recovery_version, ""); printf("\n"); + char recovery_build[PROPERTY_VALUE_MAX]; + property_get("ro.build.display.id", recovery_build, ""); + + ui->Print("%s\n", recovery_build); + ui->Print("Supported API: %d\n", RECOVERY_API_VERSION); + int status = INSTALL_SUCCESS; if (update_package != NULL) { -- cgit v1.2.3 From 0713819fd256bfdfcf241c71195b7e8c4e18c742 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 10 Apr 2015 09:40:53 -0700 Subject: Add ev_iterate_available_keys to minui. This lets us recognize whether we have up/down/power, say, and tailor the UI accordingly. Change-Id: If94e454f14243b59d2f473ac9a436bd60591da01 --- minui/Android.mk | 9 ++- minui/events.c | 213 -------------------------------------------------- minui/events.cpp | 231 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ minui/minui.h | 35 +++++---- 4 files changed, 260 insertions(+), 228 deletions(-) delete mode 100644 minui/events.c create mode 100644 minui/events.cpp diff --git a/minui/Android.mk b/minui/Android.mk index 53d072fac..66dea741a 100644 --- a/minui/Android.mk +++ b/minui/Android.mk @@ -1,14 +1,19 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) -LOCAL_SRC_FILES := graphics.c graphics_adf.c graphics_fbdev.c events.c resources.c +LOCAL_SRC_FILES := \ + events.cpp \ + graphics.c \ + graphics_adf.c \ + graphics_fbdev.c \ + resources.c \ LOCAL_WHOLE_STATIC_LIBRARIES += libadf LOCAL_STATIC_LIBRARIES += libpng LOCAL_MODULE := libminui -LOCAL_CFLAGS := -std=gnu11 +LOCAL_CLANG := true # This used to compare against values in double-quotes (which are just # ordinary characters in this context). Strip double-quotes from the diff --git a/minui/events.c b/minui/events.c deleted file mode 100644 index 9e4255dd7..000000000 --- a/minui/events.c +++ /dev/null @@ -1,213 +0,0 @@ -/* - * Copyright (C) 2007 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 -#include -#include -#include -#include -#include -#include - -#include - -#include "minui.h" - -#define MAX_DEVICES 16 -#define MAX_MISC_FDS 16 - -#define BITS_PER_LONG (sizeof(unsigned long) * 8) -#define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG) - -#define test_bit(bit, array) \ - ((array)[(bit)/BITS_PER_LONG] & (1 << ((bit) % BITS_PER_LONG))) - -struct fd_info { - int fd; - ev_callback cb; - void *data; -}; - -static int epollfd; -static struct epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS]; -static int npolledevents; - -static struct fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS]; - -static unsigned ev_count = 0; -static unsigned ev_dev_count = 0; -static unsigned ev_misc_count = 0; - -int ev_init(ev_callback input_cb, void *data) -{ - DIR *dir; - struct dirent *de; - int fd; - struct epoll_event ev; - bool epollctlfail = false; - - epollfd = epoll_create(MAX_DEVICES + MAX_MISC_FDS); - if (epollfd == -1) - return -1; - - dir = opendir("/dev/input"); - if(dir != 0) { - while((de = readdir(dir))) { - unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; - -// fprintf(stderr,"/dev/input/%s\n", de->d_name); - if(strncmp(de->d_name,"event",5)) continue; - fd = openat(dirfd(dir), de->d_name, O_RDONLY); - if(fd < 0) continue; - - /* read the evbits of the input device */ - if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) < 0) { - close(fd); - continue; - } - - /* TODO: add ability to specify event masks. For now, just assume - * that only EV_KEY, EV_REL & EV_SW event types are ever needed. */ - if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) { - close(fd); - continue; - } - - ev.events = EPOLLIN | EPOLLWAKEUP; - ev.data.ptr = (void *)&ev_fdinfo[ev_count]; - if (epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev)) { - close(fd); - epollctlfail = true; - continue; - } - - ev_fdinfo[ev_count].fd = fd; - ev_fdinfo[ev_count].cb = input_cb; - ev_fdinfo[ev_count].data = data; - ev_count++; - ev_dev_count++; - if(ev_dev_count == MAX_DEVICES) break; - } - } - - if (epollctlfail && !ev_count) { - close(epollfd); - epollfd = -1; - return -1; - } - - return 0; -} - -int ev_add_fd(int fd, ev_callback cb, void *data) -{ - struct epoll_event ev; - int ret; - - if (ev_misc_count == MAX_MISC_FDS || cb == NULL) - return -1; - - ev.events = EPOLLIN | EPOLLWAKEUP; - ev.data.ptr = (void *)&ev_fdinfo[ev_count]; - ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, fd, &ev); - if (!ret) { - ev_fdinfo[ev_count].fd = fd; - ev_fdinfo[ev_count].cb = cb; - ev_fdinfo[ev_count].data = data; - ev_count++; - ev_misc_count++; - } - - return ret; -} - -int ev_get_epollfd(void) -{ - return epollfd; -} - -void ev_exit(void) -{ - while (ev_count > 0) { - close(ev_fdinfo[--ev_count].fd); - } - ev_misc_count = 0; - ev_dev_count = 0; - close(epollfd); -} - -int ev_wait(int timeout) -{ - npolledevents = epoll_wait(epollfd, polledevents, ev_count, timeout); - if (npolledevents <= 0) - return -1; - return 0; -} - -void ev_dispatch(void) -{ - int n; - int ret; - - for (n = 0; n < npolledevents; n++) { - struct fd_info *fdi = polledevents[n].data.ptr; - ev_callback cb = fdi->cb; - if (cb) - cb(fdi->fd, polledevents[n].events, fdi->data); - } -} - -int ev_get_input(int fd, uint32_t epevents, struct input_event *ev) -{ - int r; - - if (epevents & EPOLLIN) { - r = read(fd, ev, sizeof(*ev)); - if (r == sizeof(*ev)) - return 0; - } - return -1; -} - -int ev_sync_key_state(ev_set_key_callback set_key_cb, void *data) -{ - unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; - unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; - unsigned i; - int ret; - - for (i = 0; i < ev_dev_count; i++) { - int code; - - memset(key_bits, 0, sizeof(key_bits)); - memset(ev_bits, 0, sizeof(ev_bits)); - - ret = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits); - if (ret < 0 || !test_bit(EV_KEY, ev_bits)) - continue; - - ret = ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits); - if (ret < 0) - continue; - - for (code = 0; code <= KEY_MAX; code++) { - if (test_bit(code, key_bits)) - set_key_cb(code, 1, data); - } - } - - return 0; -} diff --git a/minui/events.cpp b/minui/events.cpp new file mode 100644 index 000000000..2c41eb8a7 --- /dev/null +++ b/minui/events.cpp @@ -0,0 +1,231 @@ +/* + * Copyright (C) 2007 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 +#include +#include +#include +#include +#include +#include + +#include + +#include "minui.h" + +#define MAX_DEVICES 16 +#define MAX_MISC_FDS 16 + +#define BITS_PER_LONG (sizeof(unsigned long) * 8) +#define BITS_TO_LONGS(x) (((x) + BITS_PER_LONG - 1) / BITS_PER_LONG) + +struct fd_info { + int fd; + ev_callback cb; + void* data; +}; + +static int g_epoll_fd; +static struct epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS]; +static int npolledevents; + +static struct fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS]; + +static unsigned ev_count = 0; +static unsigned ev_dev_count = 0; +static unsigned ev_misc_count = 0; + +static bool test_bit(size_t bit, unsigned long* array) { + return (array[bit/BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG))) != 0; +} + +int ev_init(ev_callback input_cb, void* data) { + bool epollctlfail = false; + + g_epoll_fd = epoll_create(MAX_DEVICES + MAX_MISC_FDS); + if (g_epoll_fd == -1) { + return -1; + } + + DIR* dir = opendir("/dev/input"); + if (dir != NULL) { + struct dirent* de; + while ((de = readdir(dir))) { + unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; + +// fprintf(stderr,"/dev/input/%s\n", de->d_name); + if (strncmp(de->d_name, "event", 5)) continue; + int fd = openat(dirfd(dir), de->d_name, O_RDONLY); + if (fd == -1) continue; + + // Read the evbits of the input device. + if (ioctl(fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { + close(fd); + continue; + } + + // We assume that only EV_KEY, EV_REL, and EV_SW event types are ever needed. + if (!test_bit(EV_KEY, ev_bits) && !test_bit(EV_REL, ev_bits) && !test_bit(EV_SW, ev_bits)) { + close(fd); + continue; + } + + struct epoll_event ev; + ev.events = EPOLLIN | EPOLLWAKEUP; + ev.data.ptr = &ev_fdinfo[ev_count]; + if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) { + close(fd); + epollctlfail = true; + continue; + } + + ev_fdinfo[ev_count].fd = fd; + ev_fdinfo[ev_count].cb = input_cb; + ev_fdinfo[ev_count].data = data; + ev_count++; + ev_dev_count++; + if (ev_dev_count == MAX_DEVICES) break; + } + + closedir(dir); + } + + if (epollctlfail && !ev_count) { + close(g_epoll_fd); + g_epoll_fd = -1; + return -1; + } + + return 0; +} + +int ev_get_epollfd(void) { + return g_epoll_fd; +} + +int ev_add_fd(int fd, ev_callback cb, void* data) { + if (ev_misc_count == MAX_MISC_FDS || cb == NULL) { + return -1; + } + + struct epoll_event ev; + ev.events = EPOLLIN | EPOLLWAKEUP; + ev.data.ptr = (void *)&ev_fdinfo[ev_count]; + int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev); + if (!ret) { + ev_fdinfo[ev_count].fd = fd; + ev_fdinfo[ev_count].cb = cb; + ev_fdinfo[ev_count].data = data; + ev_count++; + ev_misc_count++; + } + + return ret; +} + +void ev_exit(void) { + while (ev_count > 0) { + close(ev_fdinfo[--ev_count].fd); + } + ev_misc_count = 0; + ev_dev_count = 0; + close(g_epoll_fd); +} + +int ev_wait(int timeout) { + npolledevents = epoll_wait(g_epoll_fd, polledevents, ev_count, timeout); + if (npolledevents <= 0) { + return -1; + } + return 0; +} + +void ev_dispatch(void) { + for (int n = 0; n < npolledevents; n++) { + fd_info* fdi = reinterpret_cast(polledevents[n].data.ptr); + ev_callback cb = fdi->cb; + if (cb) { + cb(fdi->fd, polledevents[n].events, fdi->data); + } + } +} + +int ev_get_input(int fd, uint32_t epevents, struct input_event* ev) { + if (epevents & EPOLLIN) { + ssize_t r = read(fd, ev, sizeof(*ev)); + if (r == sizeof(*ev)) { + return 0; + } + } + return -1; +} + +int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data) { + unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; + unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; + + for (size_t i = 0; i < ev_dev_count; ++i) { + memset(ev_bits, 0, sizeof(ev_bits)); + memset(key_bits, 0, sizeof(key_bits)); + + if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { + continue; + } + if (!test_bit(EV_KEY, ev_bits)) { + continue; + } + if (ioctl(ev_fdinfo[i].fd, EVIOCGKEY(sizeof(key_bits)), key_bits) == -1) { + continue; + } + + for (int code = 0; code <= KEY_MAX; code++) { + if (test_bit(code, key_bits)) { + set_key_cb(code, 1, data); + } + } + } + + return 0; +} + +void ev_iterate_available_keys(ev_key_callback cb, void* data) { + unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; + unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; + + for (size_t i = 0; i < ev_dev_count; ++i) { + memset(ev_bits, 0, sizeof(ev_bits)); + memset(key_bits, 0, sizeof(key_bits)); + + // Does this device even have keys? + if (ioctl(ev_fdinfo[i].fd, EVIOCGBIT(0, sizeof(ev_bits)), ev_bits) == -1) { + continue; + } + if (!test_bit(EV_KEY, ev_bits)) { + continue; + } + + int rc = ioctl(ev_fdinfo[i].fd, EVIOCGBIT(EV_KEY, KEY_MAX), key_bits); + if (rc == -1) { + continue; + } + + for (int key_code = 0; key_code <= KEY_MAX; ++key_code) { + if (test_bit(key_code, key_bits)) { + cb(key_code, data); + } + } + } +} diff --git a/minui/minui.h b/minui/minui.h index 733b675f3..8f2ff1139 100644 --- a/minui/minui.h +++ b/minui/minui.h @@ -25,6 +25,10 @@ extern "C" { #endif +// +// Graphics. +// + typedef struct { int width; int height; @@ -56,30 +60,35 @@ void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy); unsigned int gr_get_width(gr_surface surface); unsigned int gr_get_height(gr_surface surface); -// input event structure, include for the definition. -// see http://www.mjmwired.net/kernel/Documentation/input/ for info. +// +// Input events. +// + struct input_event; -typedef int (*ev_callback)(int fd, uint32_t epevents, void *data); -typedef int (*ev_set_key_callback)(int code, int value, void *data); +typedef int (*ev_callback)(int fd, uint32_t epevents, void* data); +typedef int (*ev_set_key_callback)(int code, int value, void* data); +typedef void (*ev_key_callback)(int code, void* data); -int ev_init(ev_callback input_cb, void *data); +int ev_init(ev_callback input_cb, void* data); void ev_exit(void); -int ev_add_fd(int fd, ev_callback cb, void *data); -int ev_sync_key_state(ev_set_key_callback set_key_cb, void *data); - -/* timeout has the same semantics as for poll - * 0 : don't block - * < 0 : block forever - * > 0 : block for 'timeout' milliseconds - */ +int ev_add_fd(int fd, ev_callback cb, void* data); +int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data); +void ev_iterate_available_keys(ev_key_callback cb, void* data); + +// 'timeout' has the same semantics as poll(2). +// 0 : don't block +// < 0 : block forever +// > 0 : block for 'timeout' milliseconds int ev_wait(int timeout); int ev_get_input(int fd, uint32_t epevents, struct input_event *ev); void ev_dispatch(void); int ev_get_epollfd(void); +// // Resources +// // res_create_*_surface() functions return 0 if no error, else // negative. -- cgit v1.2.3 From ec28340cf3af1029a00db1c83d78d14e8798e245 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 10 Apr 2015 10:01:53 -0700 Subject: Move "Mount /system" to the main menu. Everyone's adding secret key combinations for this anyway, and it's very useful when debugging. Change-Id: Iad549452b872a7af963dd649f283ebcd3ea24234 --- Android.mk | 1 - device.cpp | 8 +++++--- device.h | 17 +++++++++++++---- recovery.cpp | 12 +++++++----- ui.cpp | 56 ++++++++++++++++---------------------------------------- ui.h | 3 +-- 6 files changed, 42 insertions(+), 55 deletions(-) diff --git a/Android.mk b/Android.mk index a567aa5a2..a34c2659d 100644 --- a/Android.mk +++ b/Android.mk @@ -107,7 +107,6 @@ include $(CLEAR_VARS) LOCAL_MODULE := verifier_test LOCAL_FORCE_STATIC_EXECUTABLE := true LOCAL_MODULE_TAGS := tests -LOCAL_CFLAGS += -DNO_RECOVERY_MOUNT LOCAL_CFLAGS += -Wno-unused-parameter LOCAL_SRC_FILES := \ verifier_test.cpp \ diff --git a/device.cpp b/device.cpp index 20a763f2b..af92b15bd 100644 --- a/device.cpp +++ b/device.cpp @@ -31,6 +31,7 @@ static const char* ITEMS[] = { "Apply update from SD card", "Wipe data/factory reset", "Wipe cache partition", + "Mount /system", "View recovery logs", "Power off", NULL @@ -44,11 +45,12 @@ Device::BuiltinAction Device::InvokeMenuItem(int menu_position) { case 0: return REBOOT; case 1: return REBOOT_BOOTLOADER; case 2: return APPLY_ADB_SIDELOAD; - case 3: return APPLY_EXT; + case 3: return APPLY_SDCARD; case 4: return WIPE_DATA; case 5: return WIPE_CACHE; - case 6: return READ_RECOVERY_LASTLOG; - case 7: return SHUTDOWN; + case 6: return MOUNT_SYSTEM; + case 7: return VIEW_RECOVERY_LOGS; + case 8: return SHUTDOWN; default: return NO_ACTION; } } diff --git a/device.h b/device.h index 97ec2fb41..3d9101bf4 100644 --- a/device.h +++ b/device.h @@ -56,10 +56,19 @@ class Device { // - invoke a specific action (a menu position: any non-negative number) virtual int HandleMenuKey(int key, int visible) = 0; - enum BuiltinAction { NO_ACTION, REBOOT, APPLY_EXT, - APPLY_CACHE, // APPLY_CACHE is deprecated; has no effect - APPLY_ADB_SIDELOAD, WIPE_DATA, WIPE_CACHE, - REBOOT_BOOTLOADER, SHUTDOWN, READ_RECOVERY_LASTLOG }; + enum BuiltinAction { + NO_ACTION = 0, + REBOOT = 1, + APPLY_SDCARD = 2, + // APPLY_CACHE was 3. + APPLY_ADB_SIDELOAD = 4, + WIPE_DATA = 5, + WIPE_CACHE = 6, + REBOOT_BOOTLOADER = 7, + SHUTDOWN = 8, + VIEW_RECOVERY_LOGS = 9, + MOUNT_SYSTEM = 10, + }; // Return the headers (an array of strings, one per line, // NULL-terminated) for the main menu. Typically these tell users diff --git a/recovery.cpp b/recovery.cpp index bbfeda4a6..cdbb598e3 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -840,7 +840,7 @@ prompt_and_wait(Device* device, int status) { break; case Device::APPLY_ADB_SIDELOAD: - case Device::APPLY_EXT: + case Device::APPLY_SDCARD: { bool adb = (chosen_action == Device::APPLY_ADB_SIDELOAD); if (adb) { @@ -867,12 +867,14 @@ prompt_and_wait(Device* device, int status) { } break; - case Device::APPLY_CACHE: - ui->Print("\nAPPLY_CACHE is deprecated.\n"); + case Device::VIEW_RECOVERY_LOGS: + choose_recovery_file(device); break; - case Device::READ_RECOVERY_LASTLOG: - choose_recovery_file(device); + case Device::MOUNT_SYSTEM: + if (ensure_path_mounted("/system") != -1) { + ui->Print("Mounted /system."); + } break; } } diff --git a/ui.cpp b/ui.cpp index c8f08cdd1..871624520 100644 --- a/ui.cpp +++ b/ui.cpp @@ -44,15 +44,14 @@ // so use a global variable. static RecoveryUI* self = NULL; -RecoveryUI::RecoveryUI() : - key_queue_len(0), - key_last_down(-1), - key_long_press(false), - key_down_count(0), - enable_reboot(true), - consecutive_power_keys(0), - consecutive_alternate_keys(0), - last_key(-1) { +RecoveryUI::RecoveryUI() + : key_queue_len(0), + key_last_down(-1), + key_long_press(false), + key_down_count(0), + enable_reboot(true), + consecutive_power_keys(0), + last_key(-1) { pthread_mutex_init(&key_queue_mutex, NULL); pthread_cond_init(&key_queue_cond, NULL); self = this; @@ -65,8 +64,7 @@ void RecoveryUI::Init() { } -int RecoveryUI::input_callback(int fd, uint32_t epevents, void* data) -{ +int RecoveryUI::input_callback(int fd, uint32_t epevents, void* data) { struct input_event ev; int ret; @@ -162,13 +160,6 @@ void RecoveryUI::process_key(int key_code, int updown) { case RecoveryUI::ENQUEUE: EnqueueKey(key_code); break; - - case RecoveryUI::MOUNT_SYSTEM: -#ifndef NO_RECOVERY_MOUNT - ensure_path_mounted("/system"); - Print("Mounted /system."); -#endif - break; } } } @@ -203,17 +194,16 @@ void RecoveryUI::EnqueueKey(int key_code) { // Reads input events, handles special hot keys, and adds to the key queue. -void* RecoveryUI::input_thread(void *cookie) -{ - for (;;) { - if (!ev_wait(-1)) +void* RecoveryUI::input_thread(void* cookie) { + while (true) { + if (!ev_wait(-1)) { ev_dispatch(); + } } return NULL; } -int RecoveryUI::WaitKey() -{ +int RecoveryUI::WaitKey() { pthread_mutex_lock(&key_queue_mutex); // Time out after UI_WAIT_KEY_TIMEOUT_SEC, unless a USB cable is @@ -228,8 +218,7 @@ int RecoveryUI::WaitKey() int rc = 0; while (key_queue_len == 0 && rc != ETIMEDOUT) { - rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, - &timeout); + rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, &timeout); } } while (usb_connected() && key_queue_len == 0); @@ -261,8 +250,7 @@ bool RecoveryUI::usb_connected() { return connected; } -bool RecoveryUI::IsKeyPressed(int key) -{ +bool RecoveryUI::IsKeyPressed(int key) { pthread_mutex_lock(&key_queue_mutex); int pressed = key_pressed[key]; pthread_mutex_unlock(&key_queue_mutex); @@ -301,18 +289,6 @@ RecoveryUI::KeyAction RecoveryUI::CheckKey(int key) { consecutive_power_keys = 0; } - if ((key == KEY_VOLUMEUP && - (last_key == KEY_VOLUMEDOWN || last_key == -1)) || - (key == KEY_VOLUMEDOWN && - (last_key == KEY_VOLUMEUP || last_key == -1))) { - ++consecutive_alternate_keys; - if (consecutive_alternate_keys >= 7) { - consecutive_alternate_keys = 0; - return MOUNT_SYSTEM; - } - } else { - consecutive_alternate_keys = 0; - } last_key = key; return ENQUEUE; diff --git a/ui.h b/ui.h index 3b217745f..bc728b05a 100644 --- a/ui.h +++ b/ui.h @@ -81,7 +81,7 @@ class RecoveryUI { // Return value indicates whether an immediate operation should be // triggered (toggling the display, rebooting the device), or if // the key should be enqueued for use by the main thread. - enum KeyAction { ENQUEUE, TOGGLE, REBOOT, IGNORE, MOUNT_SYSTEM }; + enum KeyAction { ENQUEUE, TOGGLE, REBOOT, IGNORE }; virtual KeyAction CheckKey(int key); // Called immediately before each call to CheckKey(), tell you if @@ -134,7 +134,6 @@ private: int rel_sum; int consecutive_power_keys; - int consecutive_alternate_keys; int last_key; typedef struct { -- cgit v1.2.3 From 642aaa7a3e11b2de719fc9decc45174bcc235c0c Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 10 Apr 2015 12:47:46 -0700 Subject: Fix ScreenRecoveryUI to handle devices without power/up/down. Currently fugu has a custom subclass to handle this. The default code supports devices with trackballs but not all shipping Nexus devices? That's just silly. Change-Id: Id2779c91284899a26b4bb1af41e7033aa889df10 --- minui/events.cpp | 4 +-- minui/minui.h | 9 +++++-- recovery.cpp | 6 ++--- screen_ui.cpp | 11 +++++++- screen_ui.h | 6 +++-- ui.cpp | 79 +++++++++++++++++++++++++++++++++++++++----------------- ui.h | 29 +++++++++++---------- 7 files changed, 96 insertions(+), 48 deletions(-) diff --git a/minui/events.cpp b/minui/events.cpp index 2c41eb8a7..daa10c6cf 100644 --- a/minui/events.cpp +++ b/minui/events.cpp @@ -201,7 +201,7 @@ int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data) { return 0; } -void ev_iterate_available_keys(ev_key_callback cb, void* data) { +void ev_iterate_available_keys(std::function f) { unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; unsigned long key_bits[BITS_TO_LONGS(KEY_MAX)]; @@ -224,7 +224,7 @@ void ev_iterate_available_keys(ev_key_callback cb, void* data) { for (int key_code = 0; key_code <= KEY_MAX; ++key_code) { if (test_bit(key_code, key_bits)) { - cb(key_code, data); + f(key_code); } } } diff --git a/minui/minui.h b/minui/minui.h index 8f2ff1139..82abb8a63 100644 --- a/minui/minui.h +++ b/minui/minui.h @@ -68,13 +68,11 @@ struct input_event; typedef int (*ev_callback)(int fd, uint32_t epevents, void* data); typedef int (*ev_set_key_callback)(int code, int value, void* data); -typedef void (*ev_key_callback)(int code, void* data); int ev_init(ev_callback input_cb, void* data); void ev_exit(void); int ev_add_fd(int fd, ev_callback cb, void* data); int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data); -void ev_iterate_available_keys(ev_key_callback cb, void* data); // 'timeout' has the same semantics as poll(2). // 0 : don't block @@ -130,4 +128,11 @@ void res_free_surface(gr_surface surface); } #endif +#ifdef __cplusplus + +#include +void ev_iterate_available_keys(std::function f); + +#endif + #endif diff --git a/recovery.cpp b/recovery.cpp index cdbb598e3..43a42eab8 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -545,12 +545,10 @@ get_menu_selection(const char* const * headers, const char* const * items, if (action < 0) { switch (action) { case Device::kHighlightUp: - --selected; - selected = ui->SelectMenu(selected); + selected = ui->SelectMenu(--selected); break; case Device::kHighlightDown: - ++selected; - selected = ui->SelectMenu(selected); + selected = ui->SelectMenu(++selected); break; case Device::kInvokeItem: chosen_item = selected; diff --git a/screen_ui.cpp b/screen_ui.cpp index 6d8df68b3..b62417f5f 100644 --- a/screen_ui.cpp +++ b/screen_ui.cpp @@ -185,6 +185,9 @@ void ScreenRecoveryUI::SetColor(UIElement e) { case MENU_SEL_BG: gr_color(0, 106, 157, 255); break; + case MENU_SEL_BG_ACTIVE: + gr_color(0, 156, 100, 255); + break; case MENU_SEL_FG: gr_color(255, 255, 255, 255); break; @@ -220,7 +223,7 @@ void ScreenRecoveryUI::draw_screen_locked() { if (i == menu_top + menu_sel) { // draw the highlight bar - SetColor(MENU_SEL_BG); + SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG); gr_fill(0, y-2, gr_fb_width(), y+char_height+2); // white text of selected item SetColor(MENU_SEL_FG); @@ -638,3 +641,9 @@ void ScreenRecoveryUI::Redraw() { update_screen_locked(); pthread_mutex_unlock(&updateMutex); } + +void ScreenRecoveryUI::KeyLongPress(int) { + // Redraw so that if we're in the menu, the highlight + // will change color to indicate a successful long press. + Redraw(); +} diff --git a/screen_ui.h b/screen_ui.h index 41ff4af11..ea1a95be1 100644 --- a/screen_ui.h +++ b/screen_ui.h @@ -56,10 +56,12 @@ class ScreenRecoveryUI : public RecoveryUI { int SelectMenu(int sel); void EndMenu(); + void KeyLongPress(int); + void Redraw(); - enum UIElement { HEADER, MENU, MENU_SEL_BG, MENU_SEL_FG, LOG, TEXT_FILL }; - virtual void SetColor(UIElement e); + enum UIElement { HEADER, MENU, MENU_SEL_BG, MENU_SEL_BG_ACTIVE, MENU_SEL_FG, LOG, TEXT_FILL }; + void SetColor(UIElement e); private: Icon currentIcon; diff --git a/ui.cpp b/ui.cpp index 871624520..064890ea4 100644 --- a/ui.cpp +++ b/ui.cpp @@ -51,26 +51,40 @@ RecoveryUI::RecoveryUI() key_down_count(0), enable_reboot(true), consecutive_power_keys(0), - last_key(-1) { + last_key(-1), + has_power_key(false), + has_up_key(false), + has_down_key(false) { pthread_mutex_init(&key_queue_mutex, NULL); pthread_cond_init(&key_queue_cond, NULL); self = this; memset(key_pressed, 0, sizeof(key_pressed)); } +void RecoveryUI::OnKeyDetected(int key_code) { + if (key_code == KEY_POWER) { + has_power_key = true; + } else if (key_code == KEY_DOWN || key_code == KEY_VOLUMEDOWN) { + has_down_key = true; + } else if (key_code == KEY_UP || key_code == KEY_VOLUMEUP) { + has_up_key = true; + } +} + void RecoveryUI::Init() { ev_init(input_callback, NULL); + + using namespace std::placeholders; + ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, _1)); + pthread_create(&input_t, NULL, input_thread, NULL); } - int RecoveryUI::input_callback(int fd, uint32_t epevents, void* data) { struct input_event ev; - int ret; - - ret = ev_get_input(fd, epevents, &ev); - if (ret) + if (ev_get_input(fd, epevents, &ev) == -1) { return -1; + } if (ev.type == EV_SYN) { return 0; @@ -95,8 +109,9 @@ int RecoveryUI::input_callback(int fd, uint32_t epevents, void* data) { self->rel_sum = 0; } - if (ev.type == EV_KEY && ev.code <= KEY_MAX) + if (ev.type == EV_KEY && ev.code <= KEY_MAX) { self->process_key(ev.code, ev.value); + } return 0; } @@ -142,8 +157,7 @@ void RecoveryUI::process_key(int key_code, int updown) { pthread_mutex_unlock(&key_queue_mutex); if (register_key) { - NextCheckKeyIsLong(long_press); - switch (CheckKey(key_code)) { + switch (CheckKey(key_code, long_press)) { case RecoveryUI::IGNORE: break; @@ -257,23 +271,44 @@ bool RecoveryUI::IsKeyPressed(int key) { return pressed; } +bool RecoveryUI::IsLongPress() { + pthread_mutex_lock(&key_queue_mutex); + bool result = key_long_press; + pthread_mutex_unlock(&key_queue_mutex); + return result; +} + void RecoveryUI::FlushKeys() { pthread_mutex_lock(&key_queue_mutex); key_queue_len = 0; pthread_mutex_unlock(&key_queue_mutex); } -// The default CheckKey implementation assumes the device has power, -// volume up, and volume down keys. -// -// - Hold power and press vol-up to toggle display. -// - Press power seven times in a row to reboot. -// - Alternate vol-up and vol-down seven times to mount /system. -RecoveryUI::KeyAction RecoveryUI::CheckKey(int key) { - if ((IsKeyPressed(KEY_POWER) && key == KEY_VOLUMEUP) || key == KEY_HOME) { - return TOGGLE; +RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) { + pthread_mutex_lock(&key_queue_mutex); + key_long_press = false; + pthread_mutex_unlock(&key_queue_mutex); + + // If we have power and volume up keys, that chord is the signal to toggle the text display. + if (has_power_key && has_up_key) { + if (key == KEY_VOLUMEUP && IsKeyPressed(KEY_POWER)) { + return TOGGLE; + } + } else { + // Otherwise long press of any button toggles to the text display, + // and there's no way to toggle back (but that's pretty useless anyway). + if (is_long_press && !IsTextVisible()) { + return TOGGLE; + } + + // Also, for button-limited devices, a long press is translated to KEY_ENTER. + if (is_long_press && IsTextVisible()) { + EnqueueKey(KEY_ENTER); + return IGNORE; + } } + // Press power seven times in a row to reboot. if (key == KEY_POWER) { pthread_mutex_lock(&key_queue_mutex); bool reboot_enabled = enable_reboot; @@ -290,14 +325,10 @@ RecoveryUI::KeyAction RecoveryUI::CheckKey(int key) { } last_key = key; - - return ENQUEUE; -} - -void RecoveryUI::NextCheckKeyIsLong(bool is_long_press) { + return IsTextVisible() ? ENQUEUE : IGNORE; } -void RecoveryUI::KeyLongPress(int key) { +void RecoveryUI::KeyLongPress(int) { } void RecoveryUI::SetEnableReboot(bool enabled) { diff --git a/ui.h b/ui.h index bc728b05a..0d5ab557d 100644 --- a/ui.h +++ b/ui.h @@ -69,30 +69,27 @@ class RecoveryUI { // --- key handling --- - // Wait for keypress and return it. May return -1 after timeout. + // Wait for a key and return it. May return -1 after timeout. virtual int WaitKey(); virtual bool IsKeyPressed(int key); + virtual bool IsLongPress(); // Erase any queued-up keys. virtual void FlushKeys(); - // Called on each keypress, even while operations are in progress. + // Called on each key press, even while operations are in progress. // Return value indicates whether an immediate operation should be // triggered (toggling the display, rebooting the device), or if // the key should be enqueued for use by the main thread. enum KeyAction { ENQUEUE, TOGGLE, REBOOT, IGNORE }; - virtual KeyAction CheckKey(int key); - - // Called immediately before each call to CheckKey(), tell you if - // the key was long-pressed. - virtual void NextCheckKeyIsLong(bool is_long_press); + virtual KeyAction CheckKey(int key, bool is_long_press); // Called when a key is held down long enough to have been a // long-press (but before the key is released). This means that // if the key is eventually registered (released without any other - // keys being pressed in the meantime), NextCheckKeyIsLong() will - // be called with "true". + // keys being pressed in the meantime), CheckKey will be called with + // 'is_long_press' true. virtual void KeyLongPress(int key); // Normally in recovery there's a key sequence that triggers @@ -110,8 +107,8 @@ class RecoveryUI { virtual void StartMenu(const char* const * headers, const char* const * items, int initial_selection) = 0; - // Set the menu highlight to the given index, and return it (capped to - // the range [0..numitems). + // Set the menu highlight to the given index, wrapping if necessary. + // Returns the actual item selected. virtual int SelectMenu(int sel) = 0; // End menu mode, resetting the text overlay so that ui_print() @@ -136,14 +133,20 @@ private: int consecutive_power_keys; int last_key; - typedef struct { + bool has_power_key; + bool has_up_key; + bool has_down_key; + + struct key_timer_t { RecoveryUI* ui; int key_code; int count; - } key_timer_t; + }; pthread_t input_t; + void OnKeyDetected(int key_code); + static void* input_thread(void* cookie); static int input_callback(int fd, uint32_t epevents, void* data); void process_key(int key_code, int updown); -- cgit v1.2.3 From 07cfb8fe799901948afd6af05ef4674173713bcb Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 10 Apr 2015 13:12:05 -0700 Subject: Switch minui over to C++. Change-Id: I59e08a304ae514a3fdb6fab58721f11670bc1b01 --- minui/Android.mk | 8 +- minui/events.cpp | 12 +- minui/graphics.c | 406 ----------------------------------------- minui/graphics.cpp | 406 +++++++++++++++++++++++++++++++++++++++++ minui/graphics.h | 22 +-- minui/graphics_adf.c | 250 ------------------------- minui/graphics_adf.cpp | 249 +++++++++++++++++++++++++ minui/graphics_fbdev.c | 217 ---------------------- minui/graphics_fbdev.cpp | 213 ++++++++++++++++++++++ minui/minui.h | 42 ++--- minui/resources.c | 456 ---------------------------------------------- minui/resources.cpp | 461 +++++++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 1361 insertions(+), 1381 deletions(-) delete mode 100644 minui/graphics.c create mode 100644 minui/graphics.cpp delete mode 100644 minui/graphics_adf.c create mode 100644 minui/graphics_adf.cpp delete mode 100644 minui/graphics_fbdev.c create mode 100644 minui/graphics_fbdev.cpp delete mode 100644 minui/resources.c create mode 100644 minui/resources.cpp diff --git a/minui/Android.mk b/minui/Android.mk index 66dea741a..52f066256 100644 --- a/minui/Android.mk +++ b/minui/Android.mk @@ -3,10 +3,10 @@ include $(CLEAR_VARS) LOCAL_SRC_FILES := \ events.cpp \ - graphics.c \ - graphics_adf.c \ - graphics_fbdev.c \ - resources.c \ + graphics.cpp \ + graphics_adf.cpp \ + graphics_fbdev.cpp \ + resources.cpp \ LOCAL_WHOLE_STATIC_LIBRARIES += libadf LOCAL_STATIC_LIBRARIES += libpng diff --git a/minui/events.cpp b/minui/events.cpp index daa10c6cf..2d47a587f 100644 --- a/minui/events.cpp +++ b/minui/events.cpp @@ -39,10 +39,10 @@ struct fd_info { }; static int g_epoll_fd; -static struct epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS]; +static epoll_event polledevents[MAX_DEVICES + MAX_MISC_FDS]; static int npolledevents; -static struct fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS]; +static fd_info ev_fdinfo[MAX_DEVICES + MAX_MISC_FDS]; static unsigned ev_count = 0; static unsigned ev_dev_count = 0; @@ -62,7 +62,7 @@ int ev_init(ev_callback input_cb, void* data) { DIR* dir = opendir("/dev/input"); if (dir != NULL) { - struct dirent* de; + dirent* de; while ((de = readdir(dir))) { unsigned long ev_bits[BITS_TO_LONGS(EV_MAX)]; @@ -83,7 +83,7 @@ int ev_init(ev_callback input_cb, void* data) { continue; } - struct epoll_event ev; + epoll_event ev; ev.events = EPOLLIN | EPOLLWAKEUP; ev.data.ptr = &ev_fdinfo[ev_count]; if (epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev) == -1) { @@ -121,7 +121,7 @@ int ev_add_fd(int fd, ev_callback cb, void* data) { return -1; } - struct epoll_event ev; + epoll_event ev; ev.events = EPOLLIN | EPOLLWAKEUP; ev.data.ptr = (void *)&ev_fdinfo[ev_count]; int ret = epoll_ctl(g_epoll_fd, EPOLL_CTL_ADD, fd, &ev); @@ -163,7 +163,7 @@ void ev_dispatch(void) { } } -int ev_get_input(int fd, uint32_t epevents, struct input_event* ev) { +int ev_get_input(int fd, uint32_t epevents, input_event* ev) { if (epevents & EPOLLIN) { ssize_t r = read(fd, ev, sizeof(*ev)); if (r == sizeof(*ev)) { diff --git a/minui/graphics.c b/minui/graphics.c deleted file mode 100644 index 9d1e1b480..000000000 --- a/minui/graphics.c +++ /dev/null @@ -1,406 +0,0 @@ -/* - * Copyright (C) 2007 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 -#include -#include -#include - -#include -#include - -#include -#include -#include - -#include -#include - -#include - -#include "font_10x18.h" -#include "minui.h" -#include "graphics.h" - -typedef struct { - GRSurface* texture; - int cwidth; - int cheight; -} GRFont; - -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 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 GRSurface* gr_draw = NULL; - -static bool outside(int x, int y) -{ - return x < 0 || x >= gr_draw->width || y < 0 || y >= gr_draw->height; -} - -int gr_measure(const char *s) -{ - return gr_font->cwidth * strlen(s); -} - -void gr_font_size(int *x, int *y) -{ - *x = gr_font->cwidth; - *y = gr_font->cheight; -} - -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; - } -} - -void gr_text(int x, int y, const char *s, int bold) -{ - GRFont* font = gr_font; - - if (!font->texture || gr_current_a == 0) return; - - bold = bold && (font->texture->height != font->cheight); - - x += overscan_offset_x; - y += overscan_offset_y; - - unsigned char ch; - while ((ch = *s++)) { - if (outside(x, y) || outside(x+font->cwidth-1, y+font->cheight-1)) break; - - if (ch < ' ' || ch > '~') { - ch = '?'; - } - - unsigned char* src_p = font->texture->data + ((ch - ' ') * 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; - } -} - -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; - } - - x += overscan_offset_x; - y += overscan_offset_y; - - 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; - - 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) -{ -#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) - gr_current_r = b; - gr_current_g = g; - gr_current_b = r; - gr_current_a = a; -#else - gr_current_r = r; - gr_current_g = g; - gr_current_b = b; - gr_current_a = a; -#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_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; - - 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(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; - } - - 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; - - 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(GRSurface* surface) { - if (surface == NULL) { - return 0; - } - return surface->width; -} - -unsigned int gr_get_height(GRSurface* surface) { - if (surface == NULL) { - return 0; - } - return surface->height; -} - -static void gr_init_font(void) -{ - gr_font = calloc(sizeof(*gr_font), 1); - - 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 - // top row is regular text; the bottom row is bold. - gr_font->cwidth = gr_font->texture->width / 96; - gr_font->cheight = gr_font->texture->height / 2; - } else { - printf("failed to read font: res=%d\n", res); - - // fall back to the compiled-in font. - gr_font->texture = 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 = malloc(font.width * font.height); - gr_font->texture->data = (void*) bits; - - unsigned char data; - unsigned char* in = font.rundata; - while((data = *in++)) { - memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f); - bits += (data & 0x7f); - } - - gr_font->cwidth = font.cwidth; - gr_font->cheight = font.cheight; - } -} - -#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) -{ - gr_init_font(); - - gr_backend = open_adf(); - if (gr_backend) { - gr_draw = gr_backend->init(gr_backend); - if (!gr_draw) { - gr_backend->exit(gr_backend); - } - } - - if (!gr_draw) { - gr_backend = open_fbdev(); - gr_draw = gr_backend->init(gr_backend); - if (gr_draw == NULL) { - return -1; - } - } - - overscan_offset_x = gr_draw->width * overscan_percent / 100; - overscan_offset_y = gr_draw->height * overscan_percent / 100; - - gr_flip(); - gr_flip(); - - return 0; -} - -void gr_exit(void) -{ - gr_backend->exit(gr_backend); -} - -int gr_fb_width(void) -{ - return gr_draw->width - 2*overscan_offset_x; -} - -int gr_fb_height(void) -{ - return gr_draw->height - 2*overscan_offset_y; -} - -void gr_fb_blank(bool blank) -{ - gr_backend->blank(gr_backend, blank); -} diff --git a/minui/graphics.cpp b/minui/graphics.cpp new file mode 100644 index 000000000..d7d6e8d5a --- /dev/null +++ b/minui/graphics.cpp @@ -0,0 +1,406 @@ +/* + * Copyright (C) 2007 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 +#include +#include +#include + +#include +#include + +#include +#include +#include + +#include +#include + +#include + +#include "font_10x18.h" +#include "minui.h" +#include "graphics.h" + +struct GRFont { + GRSurface* texture; + int cwidth; + int cheight; +}; + +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 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 GRSurface* gr_draw = NULL; + +static bool outside(int x, int y) +{ + return x < 0 || x >= gr_draw->width || y < 0 || y >= gr_draw->height; +} + +int gr_measure(const char *s) +{ + return gr_font->cwidth * strlen(s); +} + +void gr_font_size(int *x, int *y) +{ + *x = gr_font->cwidth; + *y = gr_font->cheight; +} + +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; + } +} + +void gr_text(int x, int y, const char *s, int bold) +{ + GRFont* font = gr_font; + + if (!font->texture || gr_current_a == 0) return; + + bold = bold && (font->texture->height != font->cheight); + + x += overscan_offset_x; + y += overscan_offset_y; + + unsigned char ch; + while ((ch = *s++)) { + if (outside(x, y) || outside(x+font->cwidth-1, y+font->cheight-1)) break; + + if (ch < ' ' || ch > '~') { + ch = '?'; + } + + unsigned char* src_p = font->texture->data + ((ch - ' ') * 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; + } +} + +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; + } + + x += overscan_offset_x; + y += overscan_offset_y; + + 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; + + 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) +{ +#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) + gr_current_r = b; + gr_current_g = g; + gr_current_b = r; + gr_current_a = a; +#else + gr_current_r = r; + gr_current_g = g; + gr_current_b = b; + gr_current_a = a; +#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_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; + + 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(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; + } + + 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; + + 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(GRSurface* surface) { + if (surface == NULL) { + return 0; + } + return surface->width; +} + +unsigned int gr_get_height(GRSurface* surface) { + if (surface == NULL) { + return 0; + } + return surface->height; +} + +static void gr_init_font(void) +{ + gr_font = reinterpret_cast(calloc(sizeof(*gr_font), 1)); + + 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 + // top row is regular text; the bottom row is bold. + gr_font->cwidth = gr_font->texture->width / 96; + gr_font->cheight = gr_font->texture->height / 2; + } else { + printf("failed to read font: res=%d\n", res); + + // fall back to the compiled-in font. + gr_font->texture = reinterpret_cast(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 = reinterpret_cast(malloc(font.width * font.height)); + gr_font->texture->data = reinterpret_cast(bits); + + unsigned char data; + unsigned char* in = font.rundata; + while((data = *in++)) { + memset(bits, (data & 0x80) ? 255 : 0, data & 0x7f); + bits += (data & 0x7f); + } + + gr_font->cwidth = font.cwidth; + gr_font->cheight = font.cheight; + } +} + +#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) +{ + gr_init_font(); + + gr_backend = open_adf(); + if (gr_backend) { + gr_draw = gr_backend->init(gr_backend); + if (!gr_draw) { + gr_backend->exit(gr_backend); + } + } + + if (!gr_draw) { + gr_backend = open_fbdev(); + gr_draw = gr_backend->init(gr_backend); + if (gr_draw == NULL) { + return -1; + } + } + + overscan_offset_x = gr_draw->width * overscan_percent / 100; + overscan_offset_y = gr_draw->height * overscan_percent / 100; + + gr_flip(); + gr_flip(); + + return 0; +} + +void gr_exit(void) +{ + gr_backend->exit(gr_backend); +} + +int gr_fb_width(void) +{ + return gr_draw->width - 2*overscan_offset_x; +} + +int gr_fb_height(void) +{ + return gr_draw->height - 2*overscan_offset_y; +} + +void gr_fb_blank(bool blank) +{ + gr_backend->blank(gr_backend, blank); +} diff --git a/minui/graphics.h b/minui/graphics.h index 993e986ee..ed229a0c8 100644 --- a/minui/graphics.h +++ b/minui/graphics.h @@ -17,34 +17,26 @@ #ifndef _GRAPHICS_H_ #define _GRAPHICS_H_ -#ifdef __cplusplus -extern "C" { -#endif - -#include #include "minui.h" -typedef struct minui_backend { +// TODO: lose the function pointers. +struct minui_backend { // Initializes the backend and returns a gr_surface to draw into. - gr_surface (*init)(struct minui_backend*); + gr_surface (*init)(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*); + gr_surface (*flip)(minui_backend*); // Blank (or unblank) the screen. - void (*blank)(struct minui_backend*, bool); + void (*blank)(minui_backend*, bool); // Device cleanup when drawing is done. - void (*exit)(struct minui_backend*); -} minui_backend; + void (*exit)(minui_backend*); +}; minui_backend* open_fbdev(); minui_backend* open_adf(); -#ifdef __cplusplus -} -#endif - #endif diff --git a/minui/graphics_adf.c b/minui/graphics_adf.c deleted file mode 100644 index c023d4db9..000000000 --- a/minui/graphics_adf.c +++ /dev/null @@ -1,250 +0,0 @@ -/* - * 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 -#include -#include -#include -#include -#include -#include - -#include -#include - -#include - -#include "graphics.h" - -struct adf_surface_pdata { - GRSurface base; - int fd; - __u32 offset; - __u32 pitch; -}; - -struct adf_pdata { - minui_backend base; - int intf_fd; - adf_id_t eng_id; - __u32 format; - - unsigned int current_surface; - unsigned int n_surfaces; - struct adf_surface_pdata surfaces[2]; -}; - -static gr_surface adf_flip(struct minui_backend *backend); -static void adf_blank(struct minui_backend *backend, bool blank); - -static int adf_surface_init(struct adf_pdata *pdata, - struct drm_mode_modeinfo *mode, struct adf_surface_pdata *surf) -{ - memset(surf, 0, sizeof(*surf)); - - surf->fd = adf_interface_simple_buffer_alloc(pdata->intf_fd, mode->hdisplay, - mode->vdisplay, pdata->format, &surf->offset, &surf->pitch); - if (surf->fd < 0) - return surf->fd; - - surf->base.width = mode->hdisplay; - surf->base.height = mode->vdisplay; - surf->base.row_bytes = surf->pitch; - surf->base.pixel_bytes = (pdata->format == DRM_FORMAT_RGB565) ? 2 : 4; - - surf->base.data = mmap(NULL, surf->pitch * surf->base.height, PROT_WRITE, - MAP_SHARED, surf->fd, surf->offset); - if (surf->base.data == MAP_FAILED) { - close(surf->fd); - return -errno; - } - - return 0; -} - -static int adf_interface_init(struct adf_pdata *pdata) -{ - struct adf_interface_data intf_data; - int ret = 0; - int err; - - err = adf_get_interface_data(pdata->intf_fd, &intf_data); - if (err < 0) - return err; - - err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[0]); - if (err < 0) { - fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err)); - ret = err; - goto done; - } - - err = adf_surface_init(pdata, &intf_data.current_mode, - &pdata->surfaces[1]); - if (err < 0) { - fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err)); - memset(&pdata->surfaces[1], 0, sizeof(pdata->surfaces[1])); - pdata->n_surfaces = 1; - } else { - pdata->n_surfaces = 2; - } - -done: - adf_free_interface_data(&intf_data); - return ret; -} - -static int adf_device_init(struct adf_pdata *pdata, struct adf_device *dev) -{ - adf_id_t intf_id; - int intf_fd; - int err; - - err = adf_find_simple_post_configuration(dev, &pdata->format, 1, &intf_id, - &pdata->eng_id); - if (err < 0) - return err; - - err = adf_device_attach(dev, pdata->eng_id, intf_id); - if (err < 0 && err != -EALREADY) - return err; - - pdata->intf_fd = adf_interface_open(dev, intf_id, O_RDWR); - if (pdata->intf_fd < 0) - return pdata->intf_fd; - - err = adf_interface_init(pdata); - if (err < 0) { - close(pdata->intf_fd); - pdata->intf_fd = -1; - } - - return err; -} - -static gr_surface adf_init(minui_backend *backend) -{ - struct adf_pdata *pdata = (struct adf_pdata *)backend; - adf_id_t *dev_ids = NULL; - ssize_t n_dev_ids, i; - gr_surface ret; - -#if defined(RECOVERY_ABGR) - pdata->format = DRM_FORMAT_ABGR8888; -#elif defined(RECOVERY_BGRA) - pdata->format = DRM_FORMAT_BGRA8888; -#elif defined(RECOVERY_RGBX) - pdata->format = DRM_FORMAT_RGBX8888; -#else - pdata->format = DRM_FORMAT_RGB565; -#endif - - n_dev_ids = adf_devices(&dev_ids); - if (n_dev_ids == 0) { - return NULL; - } else if (n_dev_ids < 0) { - fprintf(stderr, "enumerating adf devices failed: %s\n", - strerror(-n_dev_ids)); - return NULL; - } - - pdata->intf_fd = -1; - - for (i = 0; i < n_dev_ids && pdata->intf_fd < 0; i++) { - struct adf_device dev; - - int err = adf_device_open(dev_ids[i], O_RDWR, &dev); - if (err < 0) { - fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], - strerror(-err)); - continue; - } - - err = adf_device_init(pdata, &dev); - if (err < 0) - fprintf(stderr, "initializing adf device %u failed: %s\n", - dev_ids[i], strerror(-err)); - - adf_device_close(&dev); - } - - free(dev_ids); - - if (pdata->intf_fd < 0) - return NULL; - - ret = adf_flip(backend); - - adf_blank(backend, true); - adf_blank(backend, false); - - return ret; -} - -static gr_surface adf_flip(struct minui_backend *backend) -{ - struct adf_pdata *pdata = (struct adf_pdata *)backend; - struct adf_surface_pdata *surf = &pdata->surfaces[pdata->current_surface]; - - int fence_fd = adf_interface_simple_post(pdata->intf_fd, pdata->eng_id, - surf->base.width, surf->base.height, pdata->format, surf->fd, - surf->offset, surf->pitch, -1); - if (fence_fd >= 0) - close(fence_fd); - - pdata->current_surface = (pdata->current_surface + 1) % pdata->n_surfaces; - return &pdata->surfaces[pdata->current_surface].base; -} - -static void adf_blank(struct minui_backend *backend, bool blank) -{ - struct adf_pdata *pdata = (struct adf_pdata *)backend; - adf_interface_blank(pdata->intf_fd, - blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON); -} - -static void adf_surface_destroy(struct adf_surface_pdata *surf) -{ - munmap(surf->base.data, surf->pitch * surf->base.height); - close(surf->fd); -} - -static void adf_exit(struct minui_backend *backend) -{ - struct adf_pdata *pdata = (struct adf_pdata *)backend; - unsigned int i; - - for (i = 0; i < pdata->n_surfaces; i++) - adf_surface_destroy(&pdata->surfaces[i]); - if (pdata->intf_fd >= 0) - close(pdata->intf_fd); - free(pdata); -} - -minui_backend *open_adf() -{ - struct adf_pdata *pdata = calloc(1, sizeof(*pdata)); - if (!pdata) { - perror("allocating adf backend failed"); - return NULL; - } - - pdata->base.init = adf_init; - pdata->base.flip = adf_flip; - pdata->base.blank = adf_blank; - pdata->base.exit = adf_exit; - return &pdata->base; -} diff --git a/minui/graphics_adf.cpp b/minui/graphics_adf.cpp new file mode 100644 index 000000000..ea7c0abe1 --- /dev/null +++ b/minui/graphics_adf.cpp @@ -0,0 +1,249 @@ +/* + * 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 +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include "graphics.h" + +struct adf_surface_pdata { + GRSurface base; + int fd; + __u32 offset; + __u32 pitch; +}; + +struct adf_pdata { + minui_backend base; + int intf_fd; + adf_id_t eng_id; + __u32 format; + + unsigned int current_surface; + unsigned int n_surfaces; + adf_surface_pdata surfaces[2]; +}; + +static gr_surface adf_flip(minui_backend *backend); +static void adf_blank(minui_backend *backend, bool blank); + +static int adf_surface_init(adf_pdata *pdata, drm_mode_modeinfo *mode, adf_surface_pdata *surf) { + memset(surf, 0, sizeof(*surf)); + + surf->fd = adf_interface_simple_buffer_alloc(pdata->intf_fd, mode->hdisplay, + mode->vdisplay, pdata->format, &surf->offset, &surf->pitch); + if (surf->fd < 0) + return surf->fd; + + surf->base.width = mode->hdisplay; + surf->base.height = mode->vdisplay; + surf->base.row_bytes = surf->pitch; + surf->base.pixel_bytes = (pdata->format == DRM_FORMAT_RGB565) ? 2 : 4; + + surf->base.data = reinterpret_cast(mmap(NULL, + surf->pitch * surf->base.height, PROT_WRITE, + MAP_SHARED, surf->fd, surf->offset)); + if (surf->base.data == MAP_FAILED) { + close(surf->fd); + return -errno; + } + + return 0; +} + +static int adf_interface_init(adf_pdata *pdata) +{ + adf_interface_data intf_data; + int ret = 0; + int err; + + err = adf_get_interface_data(pdata->intf_fd, &intf_data); + if (err < 0) + return err; + + err = adf_surface_init(pdata, &intf_data.current_mode, &pdata->surfaces[0]); + if (err < 0) { + fprintf(stderr, "allocating surface 0 failed: %s\n", strerror(-err)); + ret = err; + goto done; + } + + err = adf_surface_init(pdata, &intf_data.current_mode, + &pdata->surfaces[1]); + if (err < 0) { + fprintf(stderr, "allocating surface 1 failed: %s\n", strerror(-err)); + memset(&pdata->surfaces[1], 0, sizeof(pdata->surfaces[1])); + pdata->n_surfaces = 1; + } else { + pdata->n_surfaces = 2; + } + +done: + adf_free_interface_data(&intf_data); + return ret; +} + +static int adf_device_init(adf_pdata *pdata, adf_device *dev) +{ + adf_id_t intf_id; + int intf_fd; + int err; + + err = adf_find_simple_post_configuration(dev, &pdata->format, 1, &intf_id, + &pdata->eng_id); + if (err < 0) + return err; + + err = adf_device_attach(dev, pdata->eng_id, intf_id); + if (err < 0 && err != -EALREADY) + return err; + + pdata->intf_fd = adf_interface_open(dev, intf_id, O_RDWR); + if (pdata->intf_fd < 0) + return pdata->intf_fd; + + err = adf_interface_init(pdata); + if (err < 0) { + close(pdata->intf_fd); + pdata->intf_fd = -1; + } + + return err; +} + +static gr_surface adf_init(minui_backend *backend) +{ + adf_pdata *pdata = (adf_pdata *)backend; + adf_id_t *dev_ids = NULL; + ssize_t n_dev_ids, i; + gr_surface ret; + +#if defined(RECOVERY_ABGR) + pdata->format = DRM_FORMAT_ABGR8888; +#elif defined(RECOVERY_BGRA) + pdata->format = DRM_FORMAT_BGRA8888; +#elif defined(RECOVERY_RGBX) + pdata->format = DRM_FORMAT_RGBX8888; +#else + pdata->format = DRM_FORMAT_RGB565; +#endif + + n_dev_ids = adf_devices(&dev_ids); + if (n_dev_ids == 0) { + return NULL; + } else if (n_dev_ids < 0) { + fprintf(stderr, "enumerating adf devices failed: %s\n", + strerror(-n_dev_ids)); + return NULL; + } + + pdata->intf_fd = -1; + + for (i = 0; i < n_dev_ids && pdata->intf_fd < 0; i++) { + adf_device dev; + + int err = adf_device_open(dev_ids[i], O_RDWR, &dev); + if (err < 0) { + fprintf(stderr, "opening adf device %u failed: %s\n", dev_ids[i], + strerror(-err)); + continue; + } + + err = adf_device_init(pdata, &dev); + if (err < 0) + fprintf(stderr, "initializing adf device %u failed: %s\n", + dev_ids[i], strerror(-err)); + + adf_device_close(&dev); + } + + free(dev_ids); + + if (pdata->intf_fd < 0) + return NULL; + + ret = adf_flip(backend); + + adf_blank(backend, true); + adf_blank(backend, false); + + return ret; +} + +static gr_surface adf_flip(minui_backend *backend) +{ + adf_pdata *pdata = (adf_pdata *)backend; + adf_surface_pdata *surf = &pdata->surfaces[pdata->current_surface]; + + int fence_fd = adf_interface_simple_post(pdata->intf_fd, pdata->eng_id, + surf->base.width, surf->base.height, pdata->format, surf->fd, + surf->offset, surf->pitch, -1); + if (fence_fd >= 0) + close(fence_fd); + + pdata->current_surface = (pdata->current_surface + 1) % pdata->n_surfaces; + return &pdata->surfaces[pdata->current_surface].base; +} + +static void adf_blank(minui_backend *backend, bool blank) +{ + adf_pdata *pdata = (adf_pdata *)backend; + adf_interface_blank(pdata->intf_fd, + blank ? DRM_MODE_DPMS_OFF : DRM_MODE_DPMS_ON); +} + +static void adf_surface_destroy(adf_surface_pdata *surf) +{ + munmap(surf->base.data, surf->pitch * surf->base.height); + close(surf->fd); +} + +static void adf_exit(minui_backend *backend) +{ + adf_pdata *pdata = (adf_pdata *)backend; + unsigned int i; + + for (i = 0; i < pdata->n_surfaces; i++) + adf_surface_destroy(&pdata->surfaces[i]); + if (pdata->intf_fd >= 0) + close(pdata->intf_fd); + free(pdata); +} + +minui_backend *open_adf() +{ + adf_pdata* pdata = reinterpret_cast(calloc(1, sizeof(*pdata))); + if (!pdata) { + perror("allocating adf backend failed"); + return NULL; + } + + pdata->base.init = adf_init; + pdata->base.flip = adf_flip; + pdata->base.blank = adf_blank; + pdata->base.exit = adf_exit; + return &pdata->base; +} diff --git a/minui/graphics_fbdev.c b/minui/graphics_fbdev.c deleted file mode 100644 index 4a5b5b513..000000000 --- a/minui/graphics_fbdev.c +++ /dev/null @@ -1,217 +0,0 @@ -/* - * 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 -#include -#include -#include - -#include -#include - -#include -#include -#include -#include - -#include -#include - -#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; - } - - memset(bits, 0, fi.smem_len); - - 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) { -#if defined(RECOVERY_BGRA) - // In case of BGRA, do some byte swapping - unsigned int idx; - unsigned char tmp; - unsigned char* ucfb_vaddr = (unsigned char*)gr_draw->data; - for (idx = 0 ; idx < (gr_draw->height * gr_draw->row_bytes); - idx += 4) { - tmp = ucfb_vaddr[idx]; - ucfb_vaddr[idx ] = ucfb_vaddr[idx + 2]; - ucfb_vaddr[idx + 2] = tmp; - } -#endif - // 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/graphics_fbdev.cpp b/minui/graphics_fbdev.cpp new file mode 100644 index 000000000..9dbdde810 --- /dev/null +++ b/minui/graphics_fbdev.cpp @@ -0,0 +1,213 @@ +/* + * 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 +#include +#include +#include + +#include +#include + +#include +#include +#include +#include + +#include +#include + +#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 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 = open("/dev/graphics/fb0", O_RDWR); + if (fd == -1) { + perror("cannot open fb0"); + return NULL; + } + + fb_fix_screeninfo fi; + 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); + + void* 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; + } + + memset(bits, 0, fi.smem_len); + + 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 = reinterpret_cast(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) { +#if defined(RECOVERY_BGRA) + // In case of BGRA, do some byte swapping + unsigned int idx; + unsigned char tmp; + unsigned char* ucfb_vaddr = (unsigned char*)gr_draw->data; + for (idx = 0 ; idx < (gr_draw->height * gr_draw->row_bytes); + idx += 4) { + tmp = ucfb_vaddr[idx]; + ucfb_vaddr[idx ] = ucfb_vaddr[idx + 2]; + ucfb_vaddr[idx + 2] = tmp; + } +#endif + // 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 82abb8a63..eca3a5030 100644 --- a/minui/minui.h +++ b/minui/minui.h @@ -19,33 +19,30 @@ #include -#include - -#ifdef __cplusplus -extern "C" { -#endif +#include // // Graphics. // -typedef struct { +struct GRSurface { int width; int height; int row_bytes; int pixel_bytes; unsigned char* data; -} GRSurface; +}; +// TODO: remove this. typedef GRSurface* gr_surface; -int gr_init(void); -void gr_exit(void); +int gr_init(); +void gr_exit(); -int gr_fb_width(void); -int gr_fb_height(void); +int gr_fb_width(); +int gr_fb_height(); -void gr_flip(void); +void gr_flip(); void gr_fb_blank(bool blank); void gr_clear(); // clear entire surface to current color @@ -66,12 +63,14 @@ unsigned int gr_get_height(gr_surface surface); struct input_event; +// TODO: move these over to std::function. typedef int (*ev_callback)(int fd, uint32_t epevents, void* data); typedef int (*ev_set_key_callback)(int code, int value, void* data); int ev_init(ev_callback input_cb, void* data); -void ev_exit(void); +void ev_exit(); int ev_add_fd(int fd, ev_callback cb, void* data); +void ev_iterate_available_keys(std::function f); int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data); // 'timeout' has the same semantics as poll(2). @@ -80,9 +79,9 @@ int ev_sync_key_state(ev_set_key_callback set_key_cb, void* data); // > 0 : block for 'timeout' milliseconds int ev_wait(int timeout); -int ev_get_input(int fd, uint32_t epevents, struct input_event *ev); -void ev_dispatch(void); -int ev_get_epollfd(void); +int ev_get_input(int fd, uint32_t epevents, input_event* ev); +void ev_dispatch(); +int ev_get_epollfd(); // // Resources @@ -124,15 +123,4 @@ int res_create_localized_alpha_surface(const char* name, const char* locale, // functions. void res_free_surface(gr_surface surface); -#ifdef __cplusplus -} -#endif - -#ifdef __cplusplus - -#include -void ev_iterate_available_keys(std::function f); - -#endif - #endif diff --git a/minui/resources.c b/minui/resources.c deleted file mode 100644 index 886c3255d..000000000 --- a/minui/resources.c +++ /dev/null @@ -1,456 +0,0 @@ -/* - * Copyright (C) 2007 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 -#include -#include - -#include -#include - -#include -#include -#include - -#include -#include - -#include - -#include "minui.h" - -extern char* locale; - -#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; -} - -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]; - unsigned char header[8]; - int result = 0; - - 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; - 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; - } - - *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; - png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth, - &color_type, NULL, NULL, NULL); - - *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; - } - - return result; - - exit: - if (result < 0) { - png_destroy_read_struct(png_ptr, info_ptr, NULL); - } - 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->row_bytes = width * 4; - surface->pixel_bytes = 4; - - 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; - } -} - -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; - } - -#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) - png_set_bgr(png_ptr); -#endif - - unsigned char* p_row = malloc(width * 4); - unsigned int y; - 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; - } - } - 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; - } - } - -#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) - png_set_bgr(png_ptr); -#endif - - 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 (result < 0) { - if (surface) { - for (i = 0; i < *frames; ++i) { - if (surface[i]) free(surface[i]); - } - free(surface); - } - } - return result; -} - -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; - -#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) - png_set_bgr(png_ptr); -#endif - - 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; - - // if loc does *not* have an underscore, and it matches the start - // of locale, and the next character in locale *is* an underscore, - // that's a match. For instance, loc == "en" matches locale == - // "en_US". - - int i; - for (i = 0; loc[i] != 0 && loc[i] != '_'; ++i); - if (loc[i] == '_') return 0; - - return (strncmp(locale, loc, i) == 0 && locale[i] == '_'); -} - -int res_create_localized_alpha_surface(const char* name, - const char* locale, - 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; - - if (locale == NULL) { - surface = malloc_surface(0); - surface->width = 0; - surface->height = 0; - surface->row_bytes = 0; - surface->pixel_bytes = 1; - goto exit; - } - - result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); - if (result < 0) return result; - - if (channels != 1) { - result = -7; - goto exit; - } - - unsigned char* row = malloc(width); - 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 = (char*)row+5; - - if (y+1+h >= height || matches_locale(loc, locale)) { - printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y); - - surface = malloc_surface(w*h); - if (surface == NULL) { - result = -8; - goto exit; - } - surface->width = w; - surface->height = h; - 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(surface->data + i*w, row, w); - } - - *pSurface = (gr_surface) surface; - break; - } else { - int i; - for (i = 0; i < h; ++i, ++y) { - png_read_row(png_ptr, row, NULL); - } - } - } - -exit: - png_destroy_read_struct(&png_ptr, &info_ptr, NULL); - if (result < 0 && surface != NULL) free(surface); - return result; -} - -void res_free_surface(gr_surface surface) { - free(surface); -} diff --git a/minui/resources.cpp b/minui/resources.cpp new file mode 100644 index 000000000..fa413b608 --- /dev/null +++ b/minui/resources.cpp @@ -0,0 +1,461 @@ +/* + * Copyright (C) 2007 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 +#include +#include + +#include +#include + +#include +#include +#include + +#include +#include + +#include + +#include "minui.h" + +extern char* locale; + +#define SURFACE_DATA_ALIGNMENT 8 + +static gr_surface malloc_surface(size_t data_size) { + size_t size = sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT; + unsigned char* temp = reinterpret_cast(malloc(size)); + 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; +} + +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]; + unsigned char header[8]; + int result = 0; + int color_type, bit_depth; + size_t bytesRead; + + 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; + goto exit; + } + + 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; + } + + *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); + + png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth, + &color_type, NULL, NULL, NULL); + + *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; + } + + return result; + + exit: + if (result < 0) { + png_destroy_read_struct(png_ptr, info_ptr, NULL); + } + 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->row_bytes = width * 4; + surface->pixel_bytes = 4; + + 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; + } +} + +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; + unsigned char* p_row; + unsigned int y; + + *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; + } + +#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) + png_set_bgr(png_ptr); +#endif + + p_row = reinterpret_cast(malloc(width * 4)); + 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; + png_textp text; + int num_text; + unsigned char* p_row; + unsigned int y; + + *pSurface = NULL; + *frames = -1; + + result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); + if (result < 0) return result; + + *frames = 1; + 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; + } + } + 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 = reinterpret_cast(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; + } + } + +#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) + png_set_bgr(png_ptr); +#endif + + p_row = reinterpret_cast(malloc(width * 4)); + 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 (result < 0) { + if (surface) { + for (i = 0; i < *frames; ++i) { + if (surface[i]) free(surface[i]); + } + free(surface); + } + } + return result; +} + +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; + +#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA) + png_set_bgr(png_ptr); +#endif + + 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; + + // if loc does *not* have an underscore, and it matches the start + // of locale, and the next character in locale *is* an underscore, + // that's a match. For instance, loc == "en" matches locale == + // "en_US". + + int i; + for (i = 0; loc[i] != 0 && loc[i] != '_'; ++i); + if (loc[i] == '_') return 0; + + return (strncmp(locale, loc, i) == 0 && locale[i] == '_'); +} + +int res_create_localized_alpha_surface(const char* name, + const char* locale, + 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; + unsigned char* row; + png_uint_32 y; + + *pSurface = NULL; + + if (locale == NULL) { + surface = malloc_surface(0); + surface->width = 0; + surface->height = 0; + surface->row_bytes = 0; + surface->pixel_bytes = 1; + goto exit; + } + + result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels); + if (result < 0) return result; + + if (channels != 1) { + result = -7; + goto exit; + } + + row = reinterpret_cast(malloc(width)); + 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 = (char*)row+5; + + if (y+1+h >= height || matches_locale(loc, locale)) { + printf(" %20s: %s (%d x %d @ %d)\n", name, loc, w, h, y); + + surface = malloc_surface(w*h); + if (surface == NULL) { + result = -8; + goto exit; + } + surface->width = w; + surface->height = h; + 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(surface->data + i*w, row, w); + } + + *pSurface = (gr_surface) surface; + break; + } else { + int i; + for (i = 0; i < h; ++i, ++y) { + png_read_row(png_ptr, row, NULL); + } + } + } + +exit: + png_destroy_read_struct(&png_ptr, &info_ptr, NULL); + if (result < 0 && surface != NULL) free(surface); + return result; +} + +void res_free_surface(gr_surface surface) { + free(surface); +} -- cgit v1.2.3 From 20531ef60524e6205b1d9a6c12a8335c5d97e311 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 10 Apr 2015 13:59:19 -0700 Subject: Switch minadb over to C++. Change-Id: I5afaf70caa590525627c676c88b445d3162de33e --- adb_install.cpp | 2 - fuse_sdcard_provider.h | 6 +++ fuse_sideload.h | 6 +++ minadbd/Android.mk | 6 +-- minadbd/adb_main.c | 45 ----------------- minadbd/adb_main.cpp | 45 +++++++++++++++++ minadbd/fuse_adb_provider.c | 66 ------------------------- minadbd/fuse_adb_provider.cpp | 66 +++++++++++++++++++++++++ minadbd/fuse_adb_provider.h | 11 +---- minadbd/services.c | 111 ------------------------------------------ minadbd/services.cpp | 105 +++++++++++++++++++++++++++++++++++++++ recovery.cpp | 2 - 12 files changed, 232 insertions(+), 239 deletions(-) delete mode 100644 minadbd/adb_main.c create mode 100644 minadbd/adb_main.cpp delete mode 100644 minadbd/fuse_adb_provider.c create mode 100644 minadbd/fuse_adb_provider.cpp delete mode 100644 minadbd/services.c create mode 100644 minadbd/services.cpp diff --git a/adb_install.cpp b/adb_install.cpp index 9e605e2d5..ebd4cac00 100644 --- a/adb_install.cpp +++ b/adb_install.cpp @@ -30,10 +30,8 @@ #include "install.h" #include "common.h" #include "adb_install.h" -extern "C" { #include "minadbd/fuse_adb_provider.h" #include "fuse_sideload.h" -} static RecoveryUI* ui = NULL; diff --git a/fuse_sdcard_provider.h b/fuse_sdcard_provider.h index dc2982ca0..dbfbcd521 100644 --- a/fuse_sdcard_provider.h +++ b/fuse_sdcard_provider.h @@ -17,7 +17,13 @@ #ifndef __FUSE_SDCARD_PROVIDER_H #define __FUSE_SDCARD_PROVIDER_H +#include + +__BEGIN_DECLS + void* start_sdcard_fuse(const char* path); void finish_sdcard_fuse(void* token); +__END_DECLS + #endif diff --git a/fuse_sideload.h b/fuse_sideload.h index c0b16efbe..f9e3bf0d3 100644 --- a/fuse_sideload.h +++ b/fuse_sideload.h @@ -17,6 +17,10 @@ #ifndef __FUSE_SIDELOAD_H #define __FUSE_SIDELOAD_H +#include + +__BEGIN_DECLS + // define the filenames created by the sideload FUSE filesystem #define FUSE_SIDELOAD_HOST_MOUNTPOINT "/sideload" #define FUSE_SIDELOAD_HOST_FILENAME "package.zip" @@ -35,4 +39,6 @@ struct provider_vtab { int run_fuse_sideload(struct provider_vtab* vtab, void* cookie, uint64_t file_size, uint32_t block_size); +__END_DECLS + #endif diff --git a/minadbd/Android.mk b/minadbd/Android.mk index 52d3fa4e4..cbfd76e4e 100644 --- a/minadbd/Android.mk +++ b/minadbd/Android.mk @@ -11,9 +11,9 @@ minadbd_cflags := \ include $(CLEAR_VARS) LOCAL_SRC_FILES := \ - adb_main.c \ - fuse_adb_provider.c \ - services.c \ + adb_main.cpp \ + fuse_adb_provider.cpp \ + services.cpp \ LOCAL_MODULE := libminadbd LOCAL_CFLAGS := $(minadbd_cflags) diff --git a/minadbd/adb_main.c b/minadbd/adb_main.c deleted file mode 100644 index f6e240108..000000000 --- a/minadbd/adb_main.c +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2015 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 -#include -#include -#include - -#define TRACE_TAG TRACE_ADB - -#include "sysdeps.h" - -#include "adb.h" -#include "transport.h" - -int adb_main(int is_daemon, int server_port) -{ - atexit(usb_cleanup); - - adb_device_banner = "sideload"; - - // No SIGCHLD. Let the service subproc handle its children. - signal(SIGPIPE, SIG_IGN); - - init_transport_registration(); - usb_init(); - - D("Event loop starting\n"); - fdevent_loop(); - - return 0; -} diff --git a/minadbd/adb_main.cpp b/minadbd/adb_main.cpp new file mode 100644 index 000000000..f6e240108 --- /dev/null +++ b/minadbd/adb_main.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2015 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 +#include +#include +#include + +#define TRACE_TAG TRACE_ADB + +#include "sysdeps.h" + +#include "adb.h" +#include "transport.h" + +int adb_main(int is_daemon, int server_port) +{ + atexit(usb_cleanup); + + adb_device_banner = "sideload"; + + // No SIGCHLD. Let the service subproc handle its children. + signal(SIGPIPE, SIG_IGN); + + init_transport_registration(); + usb_init(); + + D("Event loop starting\n"); + fdevent_loop(); + + return 0; +} diff --git a/minadbd/fuse_adb_provider.c b/minadbd/fuse_adb_provider.c deleted file mode 100644 index 5da7fd76c..000000000 --- a/minadbd/fuse_adb_provider.c +++ /dev/null @@ -1,66 +0,0 @@ -/* - * 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 -#include -#include -#include - -#include "sysdeps.h" - -#include "adb.h" -#include "adb_io.h" -#include "fuse_adb_provider.h" -#include "fuse_sideload.h" - -int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, - uint32_t fetch_size) { - struct adb_data* ad = (struct adb_data*)cookie; - - char buf[10]; - snprintf(buf, sizeof(buf), "%08u", block); - if (!WriteStringFully(ad->sfd, buf)) { - fprintf(stderr, "failed to write to adb host: %s\n", strerror(errno)); - return -EIO; - } - - if (!ReadFdExactly(ad->sfd, buffer, fetch_size)) { - fprintf(stderr, "failed to read from adb host: %s\n", strerror(errno)); - return -EIO; - } - - return 0; -} - -static void close_adb(void* cookie) { - struct adb_data* ad = (struct adb_data*)cookie; - - WriteStringFully(ad->sfd, "DONEDONE"); -} - -int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size) { - struct adb_data ad; - struct provider_vtab vtab; - - ad.sfd = sfd; - ad.file_size = file_size; - ad.block_size = block_size; - - vtab.read_block = read_block_adb; - vtab.close = close_adb; - - return run_fuse_sideload(&vtab, &ad, file_size, block_size); -} diff --git a/minadbd/fuse_adb_provider.cpp b/minadbd/fuse_adb_provider.cpp new file mode 100644 index 000000000..5da7fd76c --- /dev/null +++ b/minadbd/fuse_adb_provider.cpp @@ -0,0 +1,66 @@ +/* + * 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 +#include +#include +#include + +#include "sysdeps.h" + +#include "adb.h" +#include "adb_io.h" +#include "fuse_adb_provider.h" +#include "fuse_sideload.h" + +int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, + uint32_t fetch_size) { + struct adb_data* ad = (struct adb_data*)cookie; + + char buf[10]; + snprintf(buf, sizeof(buf), "%08u", block); + if (!WriteStringFully(ad->sfd, buf)) { + fprintf(stderr, "failed to write to adb host: %s\n", strerror(errno)); + return -EIO; + } + + if (!ReadFdExactly(ad->sfd, buffer, fetch_size)) { + fprintf(stderr, "failed to read from adb host: %s\n", strerror(errno)); + return -EIO; + } + + return 0; +} + +static void close_adb(void* cookie) { + struct adb_data* ad = (struct adb_data*)cookie; + + WriteStringFully(ad->sfd, "DONEDONE"); +} + +int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size) { + struct adb_data ad; + struct provider_vtab vtab; + + ad.sfd = sfd; + ad.file_size = file_size; + ad.block_size = block_size; + + vtab.read_block = read_block_adb; + vtab.close = close_adb; + + return run_fuse_sideload(&vtab, &ad, file_size, block_size); +} diff --git a/minadbd/fuse_adb_provider.h b/minadbd/fuse_adb_provider.h index b88ce497b..9941709b9 100644 --- a/minadbd/fuse_adb_provider.h +++ b/minadbd/fuse_adb_provider.h @@ -19,10 +19,6 @@ #include -#ifdef __cplusplus -extern "C" { -#endif - struct adb_data { int sfd; // file descriptor for the adb channel @@ -30,12 +26,7 @@ struct adb_data { uint32_t block_size; }; -int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, - uint32_t fetch_size); +int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size); int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size); -#ifdef __cplusplus -} -#endif - #endif diff --git a/minadbd/services.c b/minadbd/services.c deleted file mode 100644 index 581d847fc..000000000 --- a/minadbd/services.c +++ /dev/null @@ -1,111 +0,0 @@ -/* - * Copyright (C) 2007 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 -#include -#include -#include -#include -#include - -#include "sysdeps.h" - -#define TRACE_TAG TRACE_SERVICES -#include "adb.h" -#include "fdevent.h" -#include "fuse_adb_provider.h" - -typedef struct stinfo stinfo; - -struct stinfo { - void (*func)(int fd, void *cookie); - int fd; - void *cookie; -}; - - -void *service_bootstrap_func(void *x) -{ - stinfo *sti = x; - sti->func(sti->fd, sti->cookie); - free(sti); - return 0; -} - -static void sideload_host_service(int sfd, void* cookie) -{ - char* saveptr; - const char* s = adb_strtok_r(cookie, ":", &saveptr); - uint64_t file_size = strtoull(s, NULL, 10); - s = adb_strtok_r(NULL, ":", &saveptr); - uint32_t block_size = strtoul(s, NULL, 10); - - printf("sideload-host file size %" PRIu64 " block size %" PRIu32 "\n", - file_size, block_size); - - int result = run_adb_fuse(sfd, file_size, block_size); - - printf("sideload_host finished\n"); - sleep(1); - exit(result == 0 ? 0 : 1); -} - -static int create_service_thread(void (*func)(int, void *), void *cookie) -{ - stinfo *sti; - adb_thread_t t; - int s[2]; - - if(adb_socketpair(s)) { - printf("cannot create service socket pair\n"); - return -1; - } - - sti = malloc(sizeof(stinfo)); - if(sti == 0) fatal("cannot allocate stinfo"); - sti->func = func; - sti->cookie = cookie; - sti->fd = s[1]; - - if(adb_thread_create( &t, service_bootstrap_func, sti)){ - free(sti); - adb_close(s[0]); - adb_close(s[1]); - printf("cannot create service thread\n"); - return -1; - } - - D("service thread started, %d:%d\n",s[0], s[1]); - return s[0]; -} - -int service_to_fd(const char *name) -{ - int ret = -1; - - if (!strncmp(name, "sideload:", 9)) { - // this exit status causes recovery to print a special error - // message saying to use a newer adb (that supports - // sideload-host). - exit(3); - } else if (!strncmp(name, "sideload-host:", 14)) { - ret = create_service_thread(sideload_host_service, (void*)(name + 14)); - } - if (ret >= 0) { - close_on_exec(ret); - } - return ret; -} diff --git a/minadbd/services.cpp b/minadbd/services.cpp new file mode 100644 index 000000000..a83256796 --- /dev/null +++ b/minadbd/services.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2007 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 +#include +#include +#include +#include +#include + +#include "sysdeps.h" + +#define TRACE_TAG TRACE_SERVICES +#include "adb.h" +#include "fdevent.h" +#include "fuse_adb_provider.h" + +typedef struct stinfo stinfo; + +struct stinfo { + void (*func)(int fd, void *cookie); + int fd; + void *cookie; +}; + +void* service_bootstrap_func(void* x) { + stinfo* sti = reinterpret_cast(x); + sti->func(sti->fd, sti->cookie); + free(sti); + return 0; +} + +static void sideload_host_service(int sfd, void* cookie) { + char* saveptr; + const char* s = adb_strtok_r(reinterpret_cast(cookie), ":", &saveptr); + uint64_t file_size = strtoull(s, NULL, 10); + s = adb_strtok_r(NULL, ":", &saveptr); + uint32_t block_size = strtoul(s, NULL, 10); + + printf("sideload-host file size %" PRIu64 " block size %" PRIu32 "\n", + file_size, block_size); + + int result = run_adb_fuse(sfd, file_size, block_size); + + printf("sideload_host finished\n"); + sleep(1); + exit(result == 0 ? 0 : 1); +} + +static int create_service_thread(void (*func)(int, void *), void *cookie) +{ + int s[2]; + if(adb_socketpair(s)) { + printf("cannot create service socket pair\n"); + return -1; + } + + stinfo* sti = reinterpret_cast(malloc(sizeof(stinfo))); + if(sti == 0) fatal("cannot allocate stinfo"); + sti->func = func; + sti->cookie = cookie; + sti->fd = s[1]; + + adb_thread_t t; + if (adb_thread_create( &t, service_bootstrap_func, sti)){ + free(sti); + adb_close(s[0]); + adb_close(s[1]); + printf("cannot create service thread\n"); + return -1; + } + + D("service thread started, %d:%d\n",s[0], s[1]); + return s[0]; +} + +int service_to_fd(const char* name) { + int ret = -1; + + if (!strncmp(name, "sideload:", 9)) { + // this exit status causes recovery to print a special error + // message saying to use a newer adb (that supports + // sideload-host). + exit(3); + } else if (!strncmp(name, "sideload-host:", 14)) { + ret = create_service_thread(sideload_host_service, (void*)(name + 14)); + } + if (ret >= 0) { + close_on_exec(ret); + } + return ret; +} diff --git a/recovery.cpp b/recovery.cpp index 43a42eab8..75534e74f 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -43,11 +43,9 @@ #include "screen_ui.h" #include "device.h" #include "adb_install.h" -extern "C" { #include "adb.h" #include "fuse_sideload.h" #include "fuse_sdcard_provider.h" -} struct selabel_handle *sehandle; -- cgit v1.2.3 From 4af215b2c35b41e983753256ad6dbebbf879c982 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 10 Apr 2015 15:00:34 -0700 Subject: Auto-detect whether to use the long-press UI. Change-Id: Ie77a5584e301467c6a5e164d2c62d6f036b2c0c0 --- README.md | 6 ++---- default_device.cpp | 29 +---------------------------- device.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++------- device.h | 2 +- ui.cpp | 6 +++++- ui.h | 4 ++++ 6 files changed, 56 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index c0833f2d0..bab7e87cd 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,9 @@ The Recovery Image Quick turn-around testing ------------------------- - mm -j - m ramdisk-nodeps - m recoveryimage-nodeps - adb reboot bootloader + mm -j && m ramdisk-nodeps && m recoveryimage-nodeps # To boot into the new recovery image # without flashing the recovery partition: + adb reboot bootloader fastboot boot $ANDROID_PRODUCT_OUT/recovery.img diff --git a/default_device.cpp b/default_device.cpp index d7dd45283..a9718668d 100644 --- a/default_device.cpp +++ b/default_device.cpp @@ -17,33 +17,6 @@ #include "device.h" #include "screen_ui.h" -class DefaultDevice : public Device { - public: - DefaultDevice() : Device(new ScreenRecoveryUI) { - } - - // TODO: make this handle more cases, and move the default implementation into Device too. - int HandleMenuKey(int key, int visible) { - if (visible) { - switch (key) { - case KEY_DOWN: - case KEY_VOLUMEDOWN: - return kHighlightDown; - - case KEY_UP: - case KEY_VOLUMEUP: - return kHighlightUp; - - case KEY_ENTER: - case KEY_POWER: - return kInvokeItem; - } - } - - return kNoAction; - } -}; - Device* make_device() { - return new DefaultDevice; + return new Device(new ScreenRecoveryUI); } diff --git a/device.cpp b/device.cpp index af92b15bd..024fc3465 100644 --- a/device.cpp +++ b/device.cpp @@ -16,15 +16,21 @@ #include "device.h" -// TODO: this is a lie for, say, fugu. -static const char* HEADERS[] = { - "Volume up/down to move highlight.", - "Power button to select.", +static const char* REGULAR_HEADERS[] = { + "Volume up/down move highlight.", + "Power button activates.", "", NULL }; -static const char* ITEMS[] = { +static const char* LONG_PRESS_HEADERS[] = { + "Any button cycles highlight.", + "Long-press activates.", + "", + NULL +}; + +static const char* MENU_ITEMS[] = { "Reboot system now", "Reboot to bootloader", "Apply update from ADB", @@ -37,8 +43,13 @@ static const char* ITEMS[] = { NULL }; -const char* const* Device::GetMenuHeaders() { return HEADERS; } -const char* const* Device::GetMenuItems() { return ITEMS; } +const char* const* Device::GetMenuHeaders() { + return ui_->HasThreeButtons() ? REGULAR_HEADERS : LONG_PRESS_HEADERS; +} + +const char* const* Device::GetMenuItems() { + return MENU_ITEMS; +} Device::BuiltinAction Device::InvokeMenuItem(int menu_position) { switch (menu_position) { @@ -54,3 +65,28 @@ Device::BuiltinAction Device::InvokeMenuItem(int menu_position) { default: return NO_ACTION; } } + +int Device::HandleMenuKey(int key, int visible) { + if (!visible) { + return kNoAction; + } + + switch (key) { + case KEY_DOWN: + case KEY_VOLUMEDOWN: + return kHighlightDown; + + case KEY_UP: + case KEY_VOLUMEUP: + return kHighlightUp; + + case KEY_ENTER: + case KEY_POWER: + return kInvokeItem; + + default: + // If you have all of the above buttons, any other buttons + // are ignored. Otherwise, any button cycles the highlight. + return ui_->HasThreeButtons() ? kNoAction : kHighlightDown; + } +} diff --git a/device.h b/device.h index 3d9101bf4..150718359 100644 --- a/device.h +++ b/device.h @@ -54,7 +54,7 @@ class Device { // - invoke the highlighted item (kInvokeItem) // - do nothing (kNoAction) // - invoke a specific action (a menu position: any non-negative number) - virtual int HandleMenuKey(int key, int visible) = 0; + virtual int HandleMenuKey(int key, int visible); enum BuiltinAction { NO_ACTION = 0, diff --git a/ui.cpp b/ui.cpp index 064890ea4..dbe5c5164 100644 --- a/ui.cpp +++ b/ui.cpp @@ -278,6 +278,10 @@ bool RecoveryUI::IsLongPress() { return result; } +bool RecoveryUI::HasThreeButtons() { + return has_power_key && has_up_key && has_down_key; +} + void RecoveryUI::FlushKeys() { pthread_mutex_lock(&key_queue_mutex); key_queue_len = 0; @@ -290,7 +294,7 @@ RecoveryUI::KeyAction RecoveryUI::CheckKey(int key, bool is_long_press) { pthread_mutex_unlock(&key_queue_mutex); // If we have power and volume up keys, that chord is the signal to toggle the text display. - if (has_power_key && has_up_key) { + if (HasThreeButtons()) { if (key == KEY_VOLUMEUP && IsKeyPressed(KEY_POWER)) { return TOGGLE; } diff --git a/ui.h b/ui.h index 0d5ab557d..c5c65c247 100644 --- a/ui.h +++ b/ui.h @@ -75,6 +75,10 @@ class RecoveryUI { virtual bool IsKeyPressed(int key); virtual bool IsLongPress(); + // Returns true if you have the volume up/down and power trio typical + // of phones and tablets, false otherwise. + virtual bool HasThreeButtons(); + // Erase any queued-up keys. virtual void FlushKeys(); -- cgit v1.2.3 From b07e1f3a3a5dbe3b2e733681080b6c692164a632 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 10 Apr 2015 16:14:52 -0700 Subject: Update the comments for package installer commands These commands are for the communication between the installer and the update binary (edify interpreter). Update the comments in sync with the codes. Change-Id: I7390f022b1447049a974b0b45697ef1d2e71d4e0 --- install.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/install.cpp b/install.cpp index 662f81c76..c7d382f3e 100644 --- a/install.cpp +++ b/install.cpp @@ -88,7 +88,7 @@ try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache) { // fill up the next part of of the progress bar // over seconds. If is zero, use // set_progress commands to manually control the - // progress of this segment of the bar + // progress of this segment of the bar. // // set_progress // should be between 0.0 and 1.0; sets the @@ -107,6 +107,18 @@ try_update_binary(const char* path, ZipArchive* zip, bool* wipe_cache) { // ui_print // display on the screen. // + // wipe_cache + // a wipe of cache will be performed following a successful + // installation. + // + // clear_display + // turn off the text display. + // + // enable_reboot + // packages can explicitly request that they want the user + // to be able to reboot during installation (useful for + // debugging packages that don't exit). + // // - the name of the package zip file. // -- cgit v1.2.3 From 2ec803f4350f7b72f5dd65c5f27656c6807e2966 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 10 Apr 2015 14:29:34 -0700 Subject: Append kernel logs to last_log file Currently we are keeping one copy of the kernel log (LAST_KMSG_FILE). This CL changes to append it to the recovery log. Bug: 18092237 Change-Id: I06ad5629016846927153064f1663753a90296f79 --- recovery.cpp | 88 +++++++++++++++++++++++++++++++----------------------------- 1 file changed, 46 insertions(+), 42 deletions(-) diff --git a/recovery.cpp b/recovery.cpp index 75534e74f..ce298cb27 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -65,19 +65,19 @@ static const struct option OPTIONS[] = { { NULL, 0, NULL, 0 }, }; -#define LAST_LOG_FILE "/cache/recovery/last_log" static const char *CACHE_LOG_DIR = "/cache/recovery"; static const char *COMMAND_FILE = "/cache/recovery/command"; static const char *INTENT_FILE = "/cache/recovery/intent"; static const char *LOG_FILE = "/cache/recovery/log"; static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install"; +static const char *LAST_LOG_FILE = "/cache/recovery/last_log"; static const char *LOCALE_FILE = "/cache/recovery/last_locale"; static const char *CACHE_ROOT = "/cache"; static const char *SDCARD_ROOT = "/sdcard"; static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log"; static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install"; -static const char *LAST_KMSG_FILE = "/cache/recovery/last_kmsg"; +static const char *TEMPORARY_KMSG_FILE = "/tmp/kmsg.log"; #define KLOG_DEFAULT_LEN (64 * 1024) #define KEEP_LOG_COUNT 10 @@ -300,33 +300,36 @@ save_kernel_log(const char *destination) { free(buffer); return; } + fputs("\n\n=== KERNEL LOG ===\n", log); fwrite(buffer, n, 1, log); check_and_fclose(log, destination); free(buffer); } -// How much of the temp log we have copied to the copy in cache. -static long tmplog_offset = 0; - -static void -copy_log_file(const char* source, const char* destination, int append) { - FILE *log = fopen_path(destination, append ? "a" : "w"); - if (log == NULL) { +// Copy the log file contents from source to destination. When offset is +// nonnull, start copying from the offset of the source and append to the +// end of the dest file. Otherwise overwrite the dest file. +static void copy_log_file(const char* source, const char* destination, long* offset) { + FILE *dest_fp = fopen_path(destination, offset != nullptr ? "a" : "w"); + if (dest_fp == nullptr) { LOGE("Can't open %s\n", destination); } else { - FILE *tmplog = fopen(source, "r"); - if (tmplog != NULL) { - if (append) { - fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write + FILE *source_fp = fopen(source, "r"); + if (source_fp != nullptr) { + if (offset) { + fseek(source_fp, *offset, SEEK_SET); // Since last write } char buf[4096]; - while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log); - if (append) { - tmplog_offset = ftell(tmplog); + size_t bytes; + while ((bytes = fread(buf, 1, sizeof(buf), source_fp)) != 0) { + fwrite(buf, 1, bytes, dest_fp); } - check_and_fclose(tmplog, source); + if (offset) { + *offset = ftell(source_fp); + } + check_and_fclose(source_fp, source); } - check_and_fclose(log, destination); + check_and_fclose(dest_fp, destination); } } @@ -344,8 +347,8 @@ static void rotate_last_logs(int max) { char oldfn[256]; char newfn[256]; for (int i = max-1; i >= 0; --i) { - snprintf(oldfn, sizeof(oldfn), (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i); - snprintf(newfn, sizeof(newfn), LAST_LOG_FILE ".%d", i+1); + snprintf(oldfn, sizeof(oldfn), (i==0) ? "%s" : "%s.%d", LAST_LOG_FILE, i); + snprintf(newfn, sizeof(newfn), "%s.%d", LAST_LOG_FILE, i+1); // ignore errors rename(oldfn, newfn); } @@ -363,14 +366,23 @@ static void copy_logs() { rotate_last_logs(KEEP_LOG_COUNT); // Copy logs to cache so the system can find out what happened. - copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true); - copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false); - copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false); - save_kernel_log(LAST_KMSG_FILE); + + // How much of the temp log we have copied to the LOG_FILE in cache. + // The offset is saved for next sync. + static long tmp_log_offset = 0; + copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, &tmp_log_offset); + + // We have to rewrite LAST_LOG_FILE since it has kmsg at the end. + copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, nullptr); + + save_kernel_log(TEMPORARY_KMSG_FILE); + // We will always append the whole file to LAST_LOG_FILE. + long tmp_kmsg_offset = 0; + copy_log_file(TEMPORARY_KMSG_FILE, LAST_LOG_FILE, &tmp_kmsg_offset); + copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, nullptr); + chmod(LOG_FILE, 0600); chown(LOG_FILE, 1000, 1000); // system user - chmod(LAST_KMSG_FILE, 0600); - chown(LAST_KMSG_FILE, 1000, 1000); // system user chmod(LAST_LOG_FILE, 0640); chmod(LAST_INSTALL_FILE, 0644); sync(); @@ -439,8 +451,9 @@ erase_volume(const char *volume) { saved_log_file* head = NULL; if (is_cache) { - // If we're reformatting /cache, we load any - // "/cache/recovery/last*" files into memory, so we can restore + // If we're reformatting /cache, we load any past logs + // (i.e. "/cache/recovery/last*") and the current log + // ("/cache/recovery/log") into memory, so we can restore // them after the reformat. ensure_path_mounted(volume); @@ -454,7 +467,7 @@ erase_volume(const char *volume) { strcat(path, "/"); int path_len = strlen(path); while ((de = readdir(d)) != NULL) { - if (strncmp(de->d_name, "last", 4) == 0) { + if (strncmp(de->d_name, "last_", 5) == 0 || strcmp(de->d_name, "log") == 0) { saved_log_file* p = (saved_log_file*) malloc(sizeof(saved_log_file)); strcpy(path+path_len, de->d_name); p->name = strdup(path); @@ -503,10 +516,6 @@ erase_volume(const char *volume) { head = temp; } - // Any part of the log we'd copied to cache is now gone. - // Reset the pointer so we copy from the beginning of the temp - // log. - tmplog_offset = 0; copy_logs(); } @@ -716,22 +725,17 @@ static void choose_recovery_file(Device* device) { unsigned int n; static const char** title_headers = NULL; char *filename; - // "Go back" + LAST_KMSG_FILE + KEEP_LOG_COUNT + terminating NULL entry - char* entries[KEEP_LOG_COUNT + 3]; + // "Go back" + KEEP_LOG_COUNT + terminating NULL entry + char* entries[KEEP_LOG_COUNT + 2]; memset(entries, 0, sizeof(entries)); n = 0; entries[n++] = strdup("Go back"); - // Add kernel kmsg file if available - if ((ensure_path_mounted(LAST_KMSG_FILE) == 0) && (access(LAST_KMSG_FILE, R_OK) == 0)) { - entries[n++] = strdup(LAST_KMSG_FILE); - } - // Add LAST_LOG_FILE + LAST_LOG_FILE.x for (i = 0; i < KEEP_LOG_COUNT; i++) { char *filename; - if (asprintf(&filename, (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i) == -1) { + if (asprintf(&filename, (i==0) ? "%s" : "%s.%d", LAST_LOG_FILE, i) == -1) { // memory allocation failure - return early. Should never happen. return; } @@ -869,7 +873,7 @@ prompt_and_wait(Device* device, int status) { case Device::MOUNT_SYSTEM: if (ensure_path_mounted("/system") != -1) { - ui->Print("Mounted /system."); + ui->Print("Mounted /system.\n"); } break; } -- cgit v1.2.3 From f012432f96c86a3010552b2d3b4d76ca416b6cd6 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Sat, 11 Apr 2015 02:04:11 +0000 Subject: Revert "Append kernel logs to last_log file" This reverts commit 2ec803f4350f7b72f5dd65c5f27656c6807e2966. Change-Id: I419025a772ef99db4c0a78bfa7ef66767f3fa062 --- recovery.cpp | 88 +++++++++++++++++++++++++++++------------------------------- 1 file changed, 42 insertions(+), 46 deletions(-) diff --git a/recovery.cpp b/recovery.cpp index ce298cb27..75534e74f 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -65,19 +65,19 @@ static const struct option OPTIONS[] = { { NULL, 0, NULL, 0 }, }; +#define LAST_LOG_FILE "/cache/recovery/last_log" static const char *CACHE_LOG_DIR = "/cache/recovery"; static const char *COMMAND_FILE = "/cache/recovery/command"; static const char *INTENT_FILE = "/cache/recovery/intent"; static const char *LOG_FILE = "/cache/recovery/log"; static const char *LAST_INSTALL_FILE = "/cache/recovery/last_install"; -static const char *LAST_LOG_FILE = "/cache/recovery/last_log"; static const char *LOCALE_FILE = "/cache/recovery/last_locale"; static const char *CACHE_ROOT = "/cache"; static const char *SDCARD_ROOT = "/sdcard"; static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log"; static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install"; -static const char *TEMPORARY_KMSG_FILE = "/tmp/kmsg.log"; +static const char *LAST_KMSG_FILE = "/cache/recovery/last_kmsg"; #define KLOG_DEFAULT_LEN (64 * 1024) #define KEEP_LOG_COUNT 10 @@ -300,36 +300,33 @@ save_kernel_log(const char *destination) { free(buffer); return; } - fputs("\n\n=== KERNEL LOG ===\n", log); fwrite(buffer, n, 1, log); check_and_fclose(log, destination); free(buffer); } -// Copy the log file contents from source to destination. When offset is -// nonnull, start copying from the offset of the source and append to the -// end of the dest file. Otherwise overwrite the dest file. -static void copy_log_file(const char* source, const char* destination, long* offset) { - FILE *dest_fp = fopen_path(destination, offset != nullptr ? "a" : "w"); - if (dest_fp == nullptr) { +// How much of the temp log we have copied to the copy in cache. +static long tmplog_offset = 0; + +static void +copy_log_file(const char* source, const char* destination, int append) { + FILE *log = fopen_path(destination, append ? "a" : "w"); + if (log == NULL) { LOGE("Can't open %s\n", destination); } else { - FILE *source_fp = fopen(source, "r"); - if (source_fp != nullptr) { - if (offset) { - fseek(source_fp, *offset, SEEK_SET); // Since last write + FILE *tmplog = fopen(source, "r"); + if (tmplog != NULL) { + if (append) { + fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write } char buf[4096]; - size_t bytes; - while ((bytes = fread(buf, 1, sizeof(buf), source_fp)) != 0) { - fwrite(buf, 1, bytes, dest_fp); + while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log); + if (append) { + tmplog_offset = ftell(tmplog); } - if (offset) { - *offset = ftell(source_fp); - } - check_and_fclose(source_fp, source); + check_and_fclose(tmplog, source); } - check_and_fclose(dest_fp, destination); + check_and_fclose(log, destination); } } @@ -347,8 +344,8 @@ static void rotate_last_logs(int max) { char oldfn[256]; char newfn[256]; for (int i = max-1; i >= 0; --i) { - snprintf(oldfn, sizeof(oldfn), (i==0) ? "%s" : "%s.%d", LAST_LOG_FILE, i); - snprintf(newfn, sizeof(newfn), "%s.%d", LAST_LOG_FILE, i+1); + snprintf(oldfn, sizeof(oldfn), (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i); + snprintf(newfn, sizeof(newfn), LAST_LOG_FILE ".%d", i+1); // ignore errors rename(oldfn, newfn); } @@ -366,23 +363,14 @@ static void copy_logs() { rotate_last_logs(KEEP_LOG_COUNT); // Copy logs to cache so the system can find out what happened. - - // How much of the temp log we have copied to the LOG_FILE in cache. - // The offset is saved for next sync. - static long tmp_log_offset = 0; - copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, &tmp_log_offset); - - // We have to rewrite LAST_LOG_FILE since it has kmsg at the end. - copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, nullptr); - - save_kernel_log(TEMPORARY_KMSG_FILE); - // We will always append the whole file to LAST_LOG_FILE. - long tmp_kmsg_offset = 0; - copy_log_file(TEMPORARY_KMSG_FILE, LAST_LOG_FILE, &tmp_kmsg_offset); - copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, nullptr); - + copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true); + copy_log_file(TEMPORARY_LOG_FILE, LAST_LOG_FILE, false); + copy_log_file(TEMPORARY_INSTALL_FILE, LAST_INSTALL_FILE, false); + save_kernel_log(LAST_KMSG_FILE); chmod(LOG_FILE, 0600); chown(LOG_FILE, 1000, 1000); // system user + chmod(LAST_KMSG_FILE, 0600); + chown(LAST_KMSG_FILE, 1000, 1000); // system user chmod(LAST_LOG_FILE, 0640); chmod(LAST_INSTALL_FILE, 0644); sync(); @@ -451,9 +439,8 @@ erase_volume(const char *volume) { saved_log_file* head = NULL; if (is_cache) { - // If we're reformatting /cache, we load any past logs - // (i.e. "/cache/recovery/last*") and the current log - // ("/cache/recovery/log") into memory, so we can restore + // If we're reformatting /cache, we load any + // "/cache/recovery/last*" files into memory, so we can restore // them after the reformat. ensure_path_mounted(volume); @@ -467,7 +454,7 @@ erase_volume(const char *volume) { strcat(path, "/"); int path_len = strlen(path); while ((de = readdir(d)) != NULL) { - if (strncmp(de->d_name, "last_", 5) == 0 || strcmp(de->d_name, "log") == 0) { + if (strncmp(de->d_name, "last", 4) == 0) { saved_log_file* p = (saved_log_file*) malloc(sizeof(saved_log_file)); strcpy(path+path_len, de->d_name); p->name = strdup(path); @@ -516,6 +503,10 @@ erase_volume(const char *volume) { head = temp; } + // Any part of the log we'd copied to cache is now gone. + // Reset the pointer so we copy from the beginning of the temp + // log. + tmplog_offset = 0; copy_logs(); } @@ -725,17 +716,22 @@ static void choose_recovery_file(Device* device) { unsigned int n; static const char** title_headers = NULL; char *filename; - // "Go back" + KEEP_LOG_COUNT + terminating NULL entry - char* entries[KEEP_LOG_COUNT + 2]; + // "Go back" + LAST_KMSG_FILE + KEEP_LOG_COUNT + terminating NULL entry + char* entries[KEEP_LOG_COUNT + 3]; memset(entries, 0, sizeof(entries)); n = 0; entries[n++] = strdup("Go back"); + // Add kernel kmsg file if available + if ((ensure_path_mounted(LAST_KMSG_FILE) == 0) && (access(LAST_KMSG_FILE, R_OK) == 0)) { + entries[n++] = strdup(LAST_KMSG_FILE); + } + // Add LAST_LOG_FILE + LAST_LOG_FILE.x for (i = 0; i < KEEP_LOG_COUNT; i++) { char *filename; - if (asprintf(&filename, (i==0) ? "%s" : "%s.%d", LAST_LOG_FILE, i) == -1) { + if (asprintf(&filename, (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i) == -1) { // memory allocation failure - return early. Should never happen. return; } @@ -873,7 +869,7 @@ prompt_and_wait(Device* device, int status) { case Device::MOUNT_SYSTEM: if (ensure_path_mounted("/system") != -1) { - ui->Print("Mounted /system.\n"); + ui->Print("Mounted /system."); } break; } -- cgit v1.2.3 From 95fc63e87b7cd04cce65f78954b56b5cbc5d6c23 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 10 Apr 2015 19:12:01 -0700 Subject: Rewritten file pager. Most importantly, this one no longer skips lines because of wrapping. Change-Id: Ic1c1944682ab8cbf3d542418ee86d29819173fc9 --- screen_ui.cpp | 146 +++++++++++++++++++++++++++++++++------------------------- screen_ui.h | 7 ++- 2 files changed, 88 insertions(+), 65 deletions(-) diff --git a/screen_ui.cpp b/screen_ui.cpp index b62417f5f..1e42ee7b4 100644 --- a/screen_ui.cpp +++ b/screen_ui.cpp @@ -28,6 +28,8 @@ #include #include +#include + #include "common.h" #include "device.h" #include "minui/minui.h" @@ -203,6 +205,13 @@ void ScreenRecoveryUI::SetColor(UIElement e) { } } +void ScreenRecoveryUI::DrawHorizontalRule(int* y) { + SetColor(MENU); + *y += 4; + gr_fill(0, *y, gr_fb_width(), *y + 2); + *y += 8; +} + // Redraw everything on the screen. Does not flip pages. // Should only be called with updateMutex locked. void ScreenRecoveryUI::draw_screen_locked() { @@ -214,12 +223,11 @@ void ScreenRecoveryUI::draw_screen_locked() { gr_clear(); int y = 0; - int i = 0; if (show_menu) { SetColor(HEADER); - for (; i < menu_top + menu_items; ++i) { - if (i == menu_top) SetColor(MENU); + for (int i = 0; i < menu_top + menu_items; ++i) { + if (i == menu_top) DrawHorizontalRule(&y); if (i == menu_top + menu_sel) { // draw the highlight bar @@ -234,11 +242,8 @@ void ScreenRecoveryUI::draw_screen_locked() { } y += char_height+4; } - SetColor(MENU); - y += 4; - gr_fill(0, y, gr_fb_width(), y+2); - y += 4; - ++i; + + DrawHorizontalRule(&y); } SetColor(LOG); @@ -249,7 +254,7 @@ void ScreenRecoveryUI::draw_screen_locked() { int row = (text_top+text_rows-1) % text_rows; size_t count = 0; for (int ty = gr_fb_height() - char_height; - ty > y+2 && count < text_rows; + ty >= y && count < text_rows; ty -= char_height, ++count) { gr_text(0, ty, text[row], 0); --row; @@ -495,72 +500,85 @@ void ScreenRecoveryUI::Print(const char *fmt, ...) { pthread_mutex_unlock(&updateMutex); } -// TODO: replace this with something not line-based so we can wrap correctly without getting -// confused about what line we're on. -void ScreenRecoveryUI::print_no_update(const char* s) { +void ScreenRecoveryUI::PutChar(char ch) { pthread_mutex_lock(&updateMutex); - if (text_rows > 0 && text_cols > 0) { - for (const char* ptr = s; *ptr != '\0'; ++ptr) { - if (*ptr == '\n' || text_col >= text_cols) { - text[text_row][text_col] = '\0'; - text_col = 0; - text_row = (text_row + 1) % text_rows; - if (text_row == text_top) text_top = (text_top + 1) % text_rows; - } - if (*ptr != '\n') text[text_row][text_col++] = *ptr; - } - text[text_row][text_col] = '\0'; + if (ch != '\n') text[text_row][text_col++] = ch; + if (ch == '\n' || text_col >= text_cols) { + text_col = 0; + ++text_row; } pthread_mutex_unlock(&updateMutex); } -void ScreenRecoveryUI::ShowFile(const char* filename) { - FILE* fp = fopen_path(filename, "re"); - if (fp == nullptr) { - Print(" Unable to open %s: %s\n", filename, strerror(errno)); - return; +void ScreenRecoveryUI::ClearText() { + pthread_mutex_lock(&updateMutex); + text_col = 0; + text_row = 0; + text_top = 1; + for (size_t i = 0; i < text_rows; ++i) { + memset(text[i], 0, text_cols + 1); } + pthread_mutex_unlock(&updateMutex); +} - char line[1024]; - int ct = 0; - int key = 0; - while (fgets(line, sizeof(line), fp) != nullptr) { - print_no_update(line); - ct++; - if (ct % text_rows == 0) { - Redraw(); +void ScreenRecoveryUI::ShowFile(FILE* fp) { + std::vector offsets; + offsets.push_back(ftell(fp)); + ClearText(); - // give the user time to glance at the entries - key = WaitKey(); + struct stat sb; + fstat(fileno(fp), &sb); - if (key == KEY_POWER) { - break; - } else if (key == KEY_VOLUMEUP) { - // Go back by seeking to the beginning and dumping ct - n - // lines. It's ugly, but this way we don't need to store - // the previous offsets. The files we're dumping here aren't - // expected to be very large. - ct -= 2 * text_rows; - if (ct < 0) { - ct = 0; - } - fseek(fp, 0, SEEK_SET); - for (int i = 0; i < ct; i++) { - fgets(line, sizeof(line), fp); + bool show_prompt = false; + while (true) { + if (show_prompt) { + Print("--(%d%% of %d bytes)--", + static_cast(100 * (double(ftell(fp)) / double(sb.st_size))), + static_cast(sb.st_size)); + Redraw(); + while (show_prompt) { + show_prompt = false; + int key = WaitKey(); + if (key == KEY_POWER) { + return; + } else if (key == KEY_UP || key == KEY_VOLUMEUP) { + if (offsets.size() <= 1) { + show_prompt = true; + } else { + offsets.pop_back(); + fseek(fp, offsets.back(), SEEK_SET); + } + } else { + if (feof(fp)) { + return; + } + offsets.push_back(ftell(fp)); } - Print("^^^^^^^^^^\n"); - } else { - // Next page. + } + ClearText(); + } + + int ch = getc(fp); + if (ch == EOF) { + text_row = text_top = text_rows - 2; + show_prompt = true; + } else { + PutChar(ch); + if (text_col == 0 && text_row >= text_rows - 2) { + text_top = text_row; + show_prompt = true; } } } +} - // If the user didn't abort, then give the user time to glance at - // the end of the log, sorry, no rewind here - if (key != KEY_POWER) { - Print("\n--END-- (press any key)\n"); - WaitKey(); +void ScreenRecoveryUI::ShowFile(const char* filename) { + FILE* fp = fopen_path(filename, "re"); + if (fp == nullptr) { + Print(" Unable to open %s: %s\n", filename, strerror(errno)); + return; } + ShowFile(fp); fclose(fp); } @@ -581,7 +599,7 @@ void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const menu[i][text_cols-1] = '\0'; } menu_items = i - menu_top; - show_menu = 1; + show_menu = true; menu_sel = initial_selection; update_screen_locked(); } @@ -590,7 +608,7 @@ void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const int ScreenRecoveryUI::SelectMenu(int sel) { pthread_mutex_lock(&updateMutex); - if (show_menu > 0) { + if (show_menu) { int old_sel = menu_sel; menu_sel = sel; @@ -607,8 +625,8 @@ int ScreenRecoveryUI::SelectMenu(int sel) { void ScreenRecoveryUI::EndMenu() { pthread_mutex_lock(&updateMutex); - if (show_menu > 0 && text_rows > 0 && text_cols > 0) { - show_menu = 0; + if (show_menu && text_rows > 0 && text_cols > 0) { + show_menu = false; update_screen_locked(); } pthread_mutex_unlock(&updateMutex); diff --git a/screen_ui.h b/screen_ui.h index ea1a95be1..590e5c8e6 100644 --- a/screen_ui.h +++ b/screen_ui.h @@ -18,6 +18,7 @@ #define RECOVERY_SCREEN_UI_H #include +#include #include "ui.h" #include "minui/minui.h" @@ -114,7 +115,11 @@ class ScreenRecoveryUI : public RecoveryUI { static void* progress_thread(void* cookie); void progress_loop(); - void print_no_update(const char*); + void ShowFile(FILE*); + void PutChar(char); + void ClearText(); + + void DrawHorizontalRule(int* y); void LoadBitmap(const char* filename, gr_surface* surface); void LoadBitmapArray(const char* filename, int* frames, gr_surface** surface); -- cgit v1.2.3 From 300ed089b3c5717ddf46eb3a25d94c8d017e7be2 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 13 Apr 2015 10:47:59 -0700 Subject: Treat KEY_ENTER the same as KEY_POWER in the pager. Our long-press UI sends KEY_ENTER for long presses, which the long-press UI treats as equivalent to KEY_POWER in the regular UI. So anywhere we accept KEY_POWER we should accept KEY_ENTER too. Change-Id: I99d376c961887043cf02037c26d000c8ba4d66f9 --- screen_ui.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/screen_ui.cpp b/screen_ui.cpp index 1e42ee7b4..cca261fc4 100644 --- a/screen_ui.cpp +++ b/screen_ui.cpp @@ -539,7 +539,7 @@ void ScreenRecoveryUI::ShowFile(FILE* fp) { while (show_prompt) { show_prompt = false; int key = WaitKey(); - if (key == KEY_POWER) { + if (key == KEY_POWER || key == KEY_ENTER) { return; } else if (key == KEY_UP || key == KEY_VOLUMEUP) { if (offsets.size() <= 1) { -- cgit v1.2.3 From 985022a6231814de2bfaf621fd0725c48bb98411 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 13 Apr 2015 13:04:32 -0700 Subject: Remove unnecessary globals. Change-Id: I76a042432aede08ceaf250319cf5eeb25d601150 --- screen_ui.cpp | 16 ++++------- screen_ui.h | 7 ++--- ui.cpp | 85 ++++++++++++++++++++++++++++------------------------------- ui.h | 11 ++++---- 4 files changed, 55 insertions(+), 64 deletions(-) diff --git a/screen_ui.cpp b/screen_ui.cpp index 1e42ee7b4..2aa3dab19 100644 --- a/screen_ui.cpp +++ b/screen_ui.cpp @@ -39,11 +39,6 @@ static int char_width; static int char_height; -// There's only (at most) one of these objects, and global callbacks -// (for pthread_create, and the input event system) need to find it, -// so use a global variable. -static ScreenRecoveryUI* self = nullptr; - // Return the current time as a double (including fractions of a second). static double now() { struct timeval tv; @@ -83,7 +78,6 @@ ScreenRecoveryUI::ScreenRecoveryUI() : backgroundIcon[i] = nullptr; } pthread_mutex_init(&updateMutex, nullptr); - self = this; } // Clear the screen and draw the currently selected background icon (if any). @@ -283,14 +277,14 @@ void ScreenRecoveryUI::update_progress_locked() { } // Keeps the progress bar updated, even when the process is otherwise busy. -void* ScreenRecoveryUI::progress_thread(void *cookie) { - self->progress_loop(); +void* ScreenRecoveryUI::ProgressThreadStartRoutine(void* data) { + reinterpret_cast(data)->ProgressThreadLoop(); return nullptr; } -void ScreenRecoveryUI::progress_loop() { +void ScreenRecoveryUI::ProgressThreadLoop() { double interval = 1.0 / animation_fps; - for (;;) { + while (true) { double start = now(); pthread_mutex_lock(&updateMutex); @@ -387,7 +381,7 @@ void ScreenRecoveryUI::Init() { LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]); LoadLocalizedBitmap("error_text", &backgroundText[ERROR]); - pthread_create(&progress_t, nullptr, progress_thread, nullptr); + pthread_create(&progress_thread_, nullptr, ProgressThreadStartRoutine, this); RecoveryUI::Init(); } diff --git a/screen_ui.h b/screen_ui.h index 590e5c8e6..50a456425 100644 --- a/screen_ui.h +++ b/screen_ui.h @@ -98,7 +98,7 @@ class ScreenRecoveryUI : public RecoveryUI { bool show_menu; int menu_top, menu_items, menu_sel; - pthread_t progress_t; + pthread_t progress_thread_; int animation_fps; int installing_frames; @@ -112,8 +112,9 @@ class ScreenRecoveryUI : public RecoveryUI { void draw_screen_locked(); void update_screen_locked(); void update_progress_locked(); - static void* progress_thread(void* cookie); - void progress_loop(); + + static void* ProgressThreadStartRoutine(void* data); + void ProgressThreadLoop(); void ShowFile(FILE*); void PutChar(char); diff --git a/ui.cpp b/ui.cpp index dbe5c5164..dca325fea 100644 --- a/ui.cpp +++ b/ui.cpp @@ -39,11 +39,6 @@ #define UI_WAIT_KEY_TIMEOUT_SEC 120 -// There's only (at most) one of these objects, and global callbacks -// (for pthread_create, and the input event system) need to find it, -// so use a global variable. -static RecoveryUI* self = NULL; - RecoveryUI::RecoveryUI() : key_queue_len(0), key_last_down(-1), @@ -55,9 +50,8 @@ RecoveryUI::RecoveryUI() has_power_key(false), has_up_key(false), has_down_key(false) { - pthread_mutex_init(&key_queue_mutex, NULL); - pthread_cond_init(&key_queue_cond, NULL); - self = this; + pthread_mutex_init(&key_queue_mutex, nullptr); + pthread_cond_init(&key_queue_cond, nullptr); memset(key_pressed, 0, sizeof(key_pressed)); } @@ -71,16 +65,29 @@ void RecoveryUI::OnKeyDetected(int key_code) { } } +int RecoveryUI::InputCallback(int fd, uint32_t epevents, void* data) { + return reinterpret_cast(data)->OnInputEvent(fd, epevents); +} + +// Reads input events, handles special hot keys, and adds to the key queue. +static void* InputThreadLoop(void*) { + while (true) { + if (!ev_wait(-1)) { + ev_dispatch(); + } + } + return nullptr; +} + void RecoveryUI::Init() { - ev_init(input_callback, NULL); + ev_init(InputCallback, this); - using namespace std::placeholders; - ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, _1)); + ev_iterate_available_keys(std::bind(&RecoveryUI::OnKeyDetected, this, std::placeholders::_1)); - pthread_create(&input_t, NULL, input_thread, NULL); + pthread_create(&input_thread_, nullptr, InputThreadLoop, nullptr); } -int RecoveryUI::input_callback(int fd, uint32_t epevents, void* data) { +int RecoveryUI::OnInputEvent(int fd, uint32_t epevents) { struct input_event ev; if (ev_get_input(fd, epevents, &ev) == -1) { return -1; @@ -94,23 +101,23 @@ int RecoveryUI::input_callback(int fd, uint32_t epevents, void* data) { // the trackball. When it exceeds a threshold // (positive or negative), fake an up/down // key event. - self->rel_sum += ev.value; - if (self->rel_sum > 3) { - self->process_key(KEY_DOWN, 1); // press down key - self->process_key(KEY_DOWN, 0); // and release it - self->rel_sum = 0; - } else if (self->rel_sum < -3) { - self->process_key(KEY_UP, 1); // press up key - self->process_key(KEY_UP, 0); // and release it - self->rel_sum = 0; + rel_sum += ev.value; + if (rel_sum > 3) { + ProcessKey(KEY_DOWN, 1); // press down key + ProcessKey(KEY_DOWN, 0); // and release it + rel_sum = 0; + } else if (rel_sum < -3) { + ProcessKey(KEY_UP, 1); // press up key + ProcessKey(KEY_UP, 0); // and release it + rel_sum = 0; } } } else { - self->rel_sum = 0; + rel_sum = 0; } if (ev.type == EV_KEY && ev.code <= KEY_MAX) { - self->process_key(ev.code, ev.value); + ProcessKey(ev.code, ev.value); } return 0; @@ -128,7 +135,7 @@ int RecoveryUI::input_callback(int fd, uint32_t epevents, void* data) { // a key is registered. // // updown == 1 for key down events; 0 for key up events -void RecoveryUI::process_key(int key_code, int updown) { +void RecoveryUI::ProcessKey(int key_code, int updown) { bool register_key = false; bool long_press = false; bool reboot_enabled; @@ -139,13 +146,13 @@ void RecoveryUI::process_key(int key_code, int updown) { ++key_down_count; key_last_down = key_code; key_long_press = false; - pthread_t th; key_timer_t* info = new key_timer_t; info->ui = this; info->key_code = key_code; info->count = key_down_count; - pthread_create(&th, NULL, &RecoveryUI::time_key_helper, info); - pthread_detach(th); + pthread_t thread; + pthread_create(&thread, nullptr, &RecoveryUI::time_key_helper, info); + pthread_detach(thread); } else { if (key_last_down == key_code) { long_press = key_long_press; @@ -182,7 +189,7 @@ void* RecoveryUI::time_key_helper(void* cookie) { key_timer_t* info = (key_timer_t*) cookie; info->ui->time_key(info->key_code, info->count); delete info; - return NULL; + return nullptr; } void RecoveryUI::time_key(int key_code, int count) { @@ -206,17 +213,6 @@ void RecoveryUI::EnqueueKey(int key_code) { pthread_mutex_unlock(&key_queue_mutex); } - -// Reads input events, handles special hot keys, and adds to the key queue. -void* RecoveryUI::input_thread(void* cookie) { - while (true) { - if (!ev_wait(-1)) { - ev_dispatch(); - } - } - return NULL; -} - int RecoveryUI::WaitKey() { pthread_mutex_lock(&key_queue_mutex); @@ -225,7 +221,7 @@ int RecoveryUI::WaitKey() { do { struct timeval now; struct timespec timeout; - gettimeofday(&now, NULL); + gettimeofday(&now, nullptr); timeout.tv_sec = now.tv_sec; timeout.tv_nsec = now.tv_usec * 1000; timeout.tv_sec += UI_WAIT_KEY_TIMEOUT_SEC; @@ -234,7 +230,7 @@ int RecoveryUI::WaitKey() { while (key_queue_len == 0 && rc != ETIMEDOUT) { rc = pthread_cond_timedwait(&key_queue_cond, &key_queue_mutex, &timeout); } - } while (usb_connected() && key_queue_len == 0); + } while (IsUsbConnected() && key_queue_len == 0); int key = -1; if (key_queue_len > 0) { @@ -245,8 +241,7 @@ int RecoveryUI::WaitKey() { return key; } -// Return true if USB is connected. -bool RecoveryUI::usb_connected() { +bool RecoveryUI::IsUsbConnected() { int fd = open("/sys/class/android_usb/android0/state", O_RDONLY); if (fd < 0) { printf("failed to open /sys/class/android_usb/android0/state: %s\n", @@ -255,7 +250,7 @@ bool RecoveryUI::usb_connected() { } char buf; - /* USB is connected if android_usb state is CONNECTED or CONFIGURED */ + // USB is connected if android_usb state is CONNECTED or CONFIGURED. int connected = (read(fd, &buf, 1) == 1) && (buf == 'C'); if (close(fd) < 0) { printf("failed to close /sys/class/android_usb/android0/state: %s\n", diff --git a/ui.h b/ui.h index c5c65c247..4dcaa0f8d 100644 --- a/ui.h +++ b/ui.h @@ -147,14 +147,15 @@ private: int count; }; - pthread_t input_t; + pthread_t input_thread_; void OnKeyDetected(int key_code); - static void* input_thread(void* cookie); - static int input_callback(int fd, uint32_t epevents, void* data); - void process_key(int key_code, int updown); - bool usb_connected(); + static int InputCallback(int fd, uint32_t epevents, void* data); + int OnInputEvent(int fd, uint32_t epevents); + void ProcessKey(int key_code, int updown); + + bool IsUsbConnected(); static void* time_key_helper(void* cookie); void time_key(int key_code, int count); -- cgit v1.2.3 From e46066f2694ff98b439ea370826dc14698998aa2 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 13 Apr 2015 13:29:38 -0700 Subject: Add missing \n after "Mounting /system." message. Change-Id: I280a478526f033f5c0041d7e8a818fce6177d732 --- recovery.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/recovery.cpp b/recovery.cpp index 75534e74f..f7ae5e71b 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -869,7 +869,7 @@ prompt_and_wait(Device* device, int status) { case Device::MOUNT_SYSTEM: if (ensure_path_mounted("/system") != -1) { - ui->Print("Mounted /system."); + ui->Print("Mounted /system.\n"); } break; } -- cgit v1.2.3 From 8fd86d77f1a2f15c6fa95bc390bcbe646374873a Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 13 Apr 2015 14:36:02 -0700 Subject: Move the menu header out of the menu. This makes it easier for us to deal with arbitrary information at the top, and means that headers added by specific commands don't overwrite the default ones. Add the fingerprint back, but broken up so it fits even on sprout's display. Change-Id: Id71da79ab1aa455a611d72756a3100a97ceb4c1c --- Android.mk | 1 + device.cpp | 18 ------------ device.h | 6 ---- minui/graphics.cpp | 2 +- minui/minui.h | 2 +- recovery.cpp | 14 +++------ screen_ui.cpp | 86 ++++++++++++++++++++++++++++++++++++------------------ screen_ui.h | 9 ++++-- 8 files changed, 72 insertions(+), 66 deletions(-) diff --git a/Android.mk b/Android.mk index a34c2659d..0484065a1 100644 --- a/Android.mk +++ b/Android.mk @@ -72,6 +72,7 @@ LOCAL_STATIC_LIBRARIES := \ libminui \ libpng \ libfs_mgr \ + libbase \ libcutils \ liblog \ libselinux \ diff --git a/device.cpp b/device.cpp index 024fc3465..fd1a9875b 100644 --- a/device.cpp +++ b/device.cpp @@ -16,20 +16,6 @@ #include "device.h" -static const char* REGULAR_HEADERS[] = { - "Volume up/down move highlight.", - "Power button activates.", - "", - NULL -}; - -static const char* LONG_PRESS_HEADERS[] = { - "Any button cycles highlight.", - "Long-press activates.", - "", - NULL -}; - static const char* MENU_ITEMS[] = { "Reboot system now", "Reboot to bootloader", @@ -43,10 +29,6 @@ static const char* MENU_ITEMS[] = { NULL }; -const char* const* Device::GetMenuHeaders() { - return ui_->HasThreeButtons() ? REGULAR_HEADERS : LONG_PRESS_HEADERS; -} - const char* const* Device::GetMenuItems() { return MENU_ITEMS; } diff --git a/device.h b/device.h index 150718359..dad8ccd56 100644 --- a/device.h +++ b/device.h @@ -70,12 +70,6 @@ class Device { MOUNT_SYSTEM = 10, }; - // Return the headers (an array of strings, one per line, - // NULL-terminated) for the main menu. Typically these tell users - // what to push to move the selection and invoke the selected - // item. - virtual const char* const* GetMenuHeaders(); - // Return the list of menu items (an array of strings, // NULL-terminated). The menu_position passed to InvokeMenuItem // will correspond to the indexes into this array. diff --git a/minui/graphics.cpp b/minui/graphics.cpp index d7d6e8d5a..f240f4bbd 100644 --- a/minui/graphics.cpp +++ b/minui/graphics.cpp @@ -103,7 +103,7 @@ static void text_blend(unsigned char* src_p, int src_row_bytes, } } -void gr_text(int x, int y, const char *s, int bold) +void gr_text(int x, int y, const char *s, bool bold) { GRFont* font = gr_font; diff --git a/minui/minui.h b/minui/minui.h index eca3a5030..936f7eec8 100644 --- a/minui/minui.h +++ b/minui/minui.h @@ -48,7 +48,7 @@ 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); -void gr_text(int x, int y, const char *s, int bold); +void gr_text(int x, int y, const char *s, bool 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); diff --git a/recovery.cpp b/recovery.cpp index f7ae5e71b..4dd827919 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -629,7 +629,7 @@ static char* browse_directory(const char* path, Device* device) { z_size += d_size; zips[z_size] = NULL; - const char* headers[] = { "Choose a package to install:", path, "", NULL }; + const char* headers[] = { "Choose a package to install:", path, NULL }; char* result; int chosen_item = 0; @@ -668,7 +668,7 @@ static char* browse_directory(const char* path, Device* device) { } static bool yes_no(Device* device, const char* question1, const char* question2) { - const char* headers[] = { question1, question2, "", NULL }; + const char* headers[] = { question1, question2, NULL }; const char* items[] = { " No", " Yes", NULL }; int chosen_item = get_menu_selection(headers, items, 1, 0, device); @@ -743,7 +743,7 @@ static void choose_recovery_file(Device* device) { entries[n++] = filename; } - const char* headers[] = { "Select file to view", "", NULL }; + const char* headers[] = { "Select file to view", NULL }; while (true) { int chosen_item = get_menu_selection(headers, entries, 1, 0, device); @@ -791,8 +791,6 @@ static int apply_from_sdcard(Device* device, bool* wipe_cache) { // on if the --shutdown_after flag was passed to recovery. static Device::BuiltinAction prompt_and_wait(Device* device, int status) { - const char* const* headers = device->GetMenuHeaders(); - for (;;) { finish_recovery(NULL); switch (status) { @@ -808,7 +806,7 @@ prompt_and_wait(Device* device, int status) { } ui->SetProgressType(RecoveryUI::EMPTY); - int chosen_item = get_menu_selection(headers, device->GetMenuItems(), 0, 0, device); + int chosen_item = get_menu_selection(nullptr, device->GetMenuItems(), 0, 0, device); // device-specific code may take some action here. It may // return one of the core actions handled in the switch @@ -1038,10 +1036,6 @@ main(int argc, char **argv) { property_list(print_property, NULL); printf("\n"); - char recovery_build[PROPERTY_VALUE_MAX]; - property_get("ro.build.display.id", recovery_build, ""); - - ui->Print("%s\n", recovery_build); ui->Print("Supported API: %d\n", RECOVERY_API_VERSION); int status = INSTALL_SUCCESS; diff --git a/screen_ui.cpp b/screen_ui.cpp index 76793350a..52f22c246 100644 --- a/screen_ui.cpp +++ b/screen_ui.cpp @@ -30,6 +30,8 @@ #include +#include "base/strings.h" +#include "cutils/properties.h" #include "common.h" #include "device.h" #include "minui/minui.h" @@ -66,7 +68,6 @@ ScreenRecoveryUI::ScreenRecoveryUI() : show_text_ever(false), menu(nullptr), show_menu(false), - menu_top(0), menu_items(0), menu_sel(0), animation_fps(20), @@ -174,6 +175,9 @@ void ScreenRecoveryUI::draw_progress_locked() { void ScreenRecoveryUI::SetColor(UIElement e) { switch (e) { + case INFO: + gr_color(249, 194, 0, 255); + break; case HEADER: gr_color(247, 0, 6, 255); break; @@ -188,7 +192,7 @@ void ScreenRecoveryUI::SetColor(UIElement e) { gr_color(255, 255, 255, 255); break; case LOG: - gr_color(249, 194, 0, 255); + gr_color(196, 196, 196, 255); break; case TEXT_FILL: gr_color(0, 0, 0, 160); @@ -203,9 +207,31 @@ void ScreenRecoveryUI::DrawHorizontalRule(int* y) { SetColor(MENU); *y += 4; gr_fill(0, *y, gr_fb_width(), *y + 2); - *y += 8; + *y += 4; +} + +void ScreenRecoveryUI::DrawTextLine(int* y, const char* line, bool bold) { + gr_text(4, *y, line, bold); + *y += char_height + 4; } +void ScreenRecoveryUI::DrawTextLines(int* y, const char* const* lines) { + for (size_t i = 0; lines != nullptr && lines[i] != nullptr; ++i) { + DrawTextLine(y, lines[i], false); + } +} + +static const char* REGULAR_HELP[] = { + "Use volume up/down and power.", + NULL +}; + +static const char* LONG_PRESS_HELP[] = { + "Any button cycles highlight.", + "Long-press activates.", + NULL +}; + // Redraw everything on the screen. Does not flip pages. // Should only be called with updateMutex locked. void ScreenRecoveryUI::draw_screen_locked() { @@ -218,39 +244,49 @@ void ScreenRecoveryUI::draw_screen_locked() { int y = 0; if (show_menu) { - SetColor(HEADER); + char recovery_fingerprint[PROPERTY_VALUE_MAX]; + property_get("ro.bootimage.build.fingerprint", recovery_fingerprint, ""); + + SetColor(INFO); + DrawTextLine(&y, "Android Recovery", true); + for (auto& chunk : android::base::Split(recovery_fingerprint, ":")) { + DrawTextLine(&y, chunk.c_str(), false); + } + DrawTextLines(&y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP); - for (int i = 0; i < menu_top + menu_items; ++i) { - if (i == menu_top) DrawHorizontalRule(&y); + SetColor(HEADER); + DrawTextLines(&y, menu_headers); - if (i == menu_top + menu_sel) { - // draw the highlight bar + SetColor(MENU); + DrawHorizontalRule(&y); + y += 4; + for (int i = 0; i < menu_items; ++i) { + if (i == menu_sel) { + // Draw the highlight bar. SetColor(IsLongPress() ? MENU_SEL_BG_ACTIVE : MENU_SEL_BG); - gr_fill(0, y-2, gr_fb_width(), y+char_height+2); - // white text of selected item + gr_fill(0, y - 2, gr_fb_width(), y + char_height + 2); + // Bold white text for the selected item. SetColor(MENU_SEL_FG); - if (menu[i][0]) gr_text(4, y, menu[i], 1); + gr_text(4, y, menu[i], true); SetColor(MENU); } else { - if (menu[i][0]) gr_text(4, y, menu[i], i < menu_top); + gr_text(4, y, menu[i], false); } - y += char_height+4; + y += char_height + 4; } - DrawHorizontalRule(&y); } - SetColor(LOG); - // display from the bottom up, until we hit the top of the // screen, the bottom of the menu, or we've displayed the // entire text buffer. + SetColor(LOG); int row = (text_top+text_rows-1) % text_rows; size_t count = 0; for (int ty = gr_fb_height() - char_height; ty >= y && count < text_rows; ty -= char_height, ++count) { - gr_text(0, ty, text[row], 0); + gr_text(0, ty, text[row], false); --row; if (row < 0) row = text_rows-1; } @@ -580,19 +616,13 @@ void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const int initial_selection) { pthread_mutex_lock(&updateMutex); if (text_rows > 0 && text_cols > 0) { - size_t i; - for (i = 0; i < text_rows; ++i) { - if (headers[i] == nullptr) break; - strncpy(menu[i], headers[i], text_cols-1); - menu[i][text_cols-1] = '\0'; - } - menu_top = i; - for (; i < text_rows; ++i) { - if (items[i-menu_top] == nullptr) break; - strncpy(menu[i], items[i-menu_top], text_cols-1); + menu_headers = headers; + size_t i = 0; + for (; i < text_rows && items[i] != nullptr; ++i) { + strncpy(menu[i], items[i], text_cols-1); menu[i][text_cols-1] = '\0'; } - menu_items = i - menu_top; + menu_items = i; show_menu = true; menu_sel = initial_selection; update_screen_locked(); diff --git a/screen_ui.h b/screen_ui.h index 50a456425..d473b8e94 100644 --- a/screen_ui.h +++ b/screen_ui.h @@ -61,7 +61,9 @@ class ScreenRecoveryUI : public RecoveryUI { void Redraw(); - enum UIElement { HEADER, MENU, MENU_SEL_BG, MENU_SEL_BG_ACTIVE, MENU_SEL_FG, LOG, TEXT_FILL }; + enum UIElement { + HEADER, MENU, MENU_SEL_BG, MENU_SEL_BG_ACTIVE, MENU_SEL_FG, LOG, TEXT_FILL, INFO + }; void SetColor(UIElement e); private: @@ -95,8 +97,9 @@ class ScreenRecoveryUI : public RecoveryUI { bool show_text_ever; // has show_text ever been true? char** menu; + const char* const* menu_headers; bool show_menu; - int menu_top, menu_items, menu_sel; + int menu_items, menu_sel; pthread_t progress_thread_; @@ -121,6 +124,8 @@ class ScreenRecoveryUI : public RecoveryUI { void ClearText(); void DrawHorizontalRule(int* y); + void DrawTextLine(int* y, const char* line, bool bold); + void DrawTextLines(int* y, const char* const* lines); void LoadBitmap(const char* filename, gr_surface* surface); void LoadBitmapArray(const char* filename, int* frames, gr_surface** surface); -- cgit v1.2.3 From c68bd34dc8d43f685c1f304a6cd9917c18c690aa Mon Sep 17 00:00:00 2001 From: Johan Redestig Date: Tue, 14 Apr 2015 21:20:06 +0200 Subject: imgdiff: Avoid infinite loop if inflate fails Break out of the loop if inflate returns an error and print some details. Change-Id: Ie157cf943291b1a26f4523b17691dfcefbc881dc --- applypatch/imgdiff.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/applypatch/imgdiff.c b/applypatch/imgdiff.c index 05c4f250f..3bac8be91 100644 --- a/applypatch/imgdiff.c +++ b/applypatch/imgdiff.c @@ -408,6 +408,7 @@ unsigned char* ReadImage(const char* filename, p[2] == 0x08 && // deflate compression p[3] == 0x00) { // no header flags // 'pos' is the offset of the start of a gzip chunk. + size_t chunk_offset = pos; *num_chunks += 3; *chunks = realloc(*chunks, *num_chunks * sizeof(ImageChunk)); @@ -453,6 +454,14 @@ unsigned char* ReadImage(const char* filename, strm.avail_out = allocated - curr->len; strm.next_out = curr->data + curr->len; ret = inflate(&strm, Z_NO_FLUSH); + if (ret < 0) { + printf("Error: inflate failed [%s] at file offset [%zu]\n" + "imgdiff only supports gzip kernel compression," + " did you try CONFIG_KERNEL_LZO?\n", + strm.msg, chunk_offset); + free(img); + return NULL; + } curr->len = allocated - strm.avail_out; if (strm.avail_out == 0) { allocated *= 2; -- cgit v1.2.3 From 0a5cb0c7cd995ae0330a7d54a8d0db5d892a48a9 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 15 Apr 2015 10:58:56 -0700 Subject: Don't use typedefs that hide *s. gr_surface was causing confusion for no good reason. Change-Id: If7120187f9a00dd16297877fc49352185a4d4ea6 --- minui/graphics.cpp | 2 +- minui/graphics.h | 6 +++--- minui/graphics_adf.cpp | 8 ++++---- minui/graphics_fbdev.cpp | 8 ++++---- minui/minui.h | 21 +++++++++------------ minui/resources.cpp | 36 +++++++++++++++++------------------- screen_ui.cpp | 12 ++++++------ screen_ui.h | 20 ++++++++++---------- 8 files changed, 54 insertions(+), 59 deletions(-) diff --git a/minui/graphics.cpp b/minui/graphics.cpp index f240f4bbd..f09f1c6b0 100644 --- a/minui/graphics.cpp +++ b/minui/graphics.cpp @@ -326,7 +326,7 @@ static void gr_test() { gr_clear(); gr_color(255, 0, 0, 255); - gr_surface frame = images[x%frames]; + GRSurface* frame = images[x%frames]; gr_blit(frame, 0, 0, frame->width, frame->height, x, 0); gr_color(255, 0, 0, 128); diff --git a/minui/graphics.h b/minui/graphics.h index ed229a0c8..81a923383 100644 --- a/minui/graphics.h +++ b/minui/graphics.h @@ -21,13 +21,13 @@ // TODO: lose the function pointers. struct minui_backend { - // Initializes the backend and returns a gr_surface to draw into. - gr_surface (*init)(minui_backend*); + // Initializes the backend and returns a GRSurface* to draw into. + GRSurface* (*init)(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)(minui_backend*); + GRSurface* (*flip)(minui_backend*); // Blank (or unblank) the screen. void (*blank)(minui_backend*, bool); diff --git a/minui/graphics_adf.cpp b/minui/graphics_adf.cpp index ea7c0abe1..5d0867f58 100644 --- a/minui/graphics_adf.cpp +++ b/minui/graphics_adf.cpp @@ -47,7 +47,7 @@ struct adf_pdata { adf_surface_pdata surfaces[2]; }; -static gr_surface adf_flip(minui_backend *backend); +static GRSurface* adf_flip(minui_backend *backend); static void adf_blank(minui_backend *backend, bool blank); static int adf_surface_init(adf_pdata *pdata, drm_mode_modeinfo *mode, adf_surface_pdata *surf) { @@ -134,12 +134,12 @@ static int adf_device_init(adf_pdata *pdata, adf_device *dev) return err; } -static gr_surface adf_init(minui_backend *backend) +static GRSurface* adf_init(minui_backend *backend) { adf_pdata *pdata = (adf_pdata *)backend; adf_id_t *dev_ids = NULL; ssize_t n_dev_ids, i; - gr_surface ret; + GRSurface* ret; #if defined(RECOVERY_ABGR) pdata->format = DRM_FORMAT_ABGR8888; @@ -193,7 +193,7 @@ static gr_surface adf_init(minui_backend *backend) return ret; } -static gr_surface adf_flip(minui_backend *backend) +static GRSurface* adf_flip(minui_backend *backend) { adf_pdata *pdata = (adf_pdata *)backend; adf_surface_pdata *surf = &pdata->surfaces[pdata->current_surface]; diff --git a/minui/graphics_fbdev.cpp b/minui/graphics_fbdev.cpp index 9dbdde810..997e9cac2 100644 --- a/minui/graphics_fbdev.cpp +++ b/minui/graphics_fbdev.cpp @@ -33,8 +33,8 @@ #include "minui.h" #include "graphics.h" -static gr_surface fbdev_init(minui_backend*); -static gr_surface fbdev_flip(minui_backend*); +static GRSurface* fbdev_init(minui_backend*); +static GRSurface* fbdev_flip(minui_backend*); static void fbdev_blank(minui_backend*, bool); static void fbdev_exit(minui_backend*); @@ -79,7 +79,7 @@ static void set_displayed_framebuffer(unsigned n) displayed_buffer = n; } -static gr_surface fbdev_init(minui_backend* backend) { +static GRSurface* fbdev_init(minui_backend* backend) { int fd = open("/dev/graphics/fb0", O_RDWR); if (fd == -1) { perror("cannot open fb0"); @@ -174,7 +174,7 @@ static gr_surface fbdev_init(minui_backend* backend) { return gr_draw; } -static gr_surface fbdev_flip(minui_backend* backend __unused) { +static GRSurface* fbdev_flip(minui_backend* backend __unused) { if (double_buffered) { #if defined(RECOVERY_BGRA) // In case of BGRA, do some byte swapping diff --git a/minui/minui.h b/minui/minui.h index 936f7eec8..bdde083f3 100644 --- a/minui/minui.h +++ b/minui/minui.h @@ -33,9 +33,6 @@ struct GRSurface { unsigned char* data; }; -// TODO: remove this. -typedef GRSurface* gr_surface; - int gr_init(); void gr_exit(); @@ -49,13 +46,13 @@ 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); void gr_text(int x, int y, const char *s, bool bold); -void gr_texticon(int x, int y, gr_surface icon); +void gr_texticon(int x, int y, GRSurface* icon); int gr_measure(const char *s); void gr_font_size(int *x, int *y); -void gr_blit(gr_surface source, int sx, int sy, int w, int h, int dx, int dy); -unsigned int gr_get_width(gr_surface surface); -unsigned int gr_get_height(gr_surface surface); +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); // // Input events. @@ -98,17 +95,17 @@ int ev_get_epollfd(); // 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); +int res_create_display_surface(const char* name, GRSurface** 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); + int* frames, GRSurface*** pSurface); // Load a single alpha surface from a grayscale PNG image. -int res_create_alpha_surface(const char* name, gr_surface* pSurface); +int res_create_alpha_surface(const char* name, GRSurface** 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 @@ -117,10 +114,10 @@ int res_create_alpha_surface(const char* name, gr_surface* pSurface); // 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); + GRSurface** pSurface); // Free a surface allocated by any of the res_create_*_surface() // functions. -void res_free_surface(gr_surface surface); +void res_free_surface(GRSurface* surface); #endif diff --git a/minui/resources.cpp b/minui/resources.cpp index fa413b608..5e4789277 100644 --- a/minui/resources.cpp +++ b/minui/resources.cpp @@ -36,11 +36,11 @@ extern char* locale; #define SURFACE_DATA_ALIGNMENT 8 -static gr_surface malloc_surface(size_t data_size) { +static GRSurface* malloc_surface(size_t data_size) { size_t size = sizeof(GRSurface) + data_size + SURFACE_DATA_ALIGNMENT; unsigned char* temp = reinterpret_cast(malloc(size)); if (temp == NULL) return NULL; - gr_surface surface = (gr_surface) temp; + GRSurface* surface = reinterpret_cast(temp); surface->data = temp + sizeof(GRSurface) + (SURFACE_DATA_ALIGNMENT - (sizeof(GRSurface) % SURFACE_DATA_ALIGNMENT)); return surface; @@ -138,12 +138,10 @@ static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr, // 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 +// Allocate and return a GRSurface* 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); +static GRSurface* init_display_surface(png_uint_32 width, png_uint_32 height) { + GRSurface* surface = malloc_surface(width * height * 4); if (surface == NULL) return NULL; surface->width = width; @@ -199,8 +197,8 @@ static void transform_rgb_to_draw(unsigned char* input_row, } } -int res_create_display_surface(const char* name, gr_surface* pSurface) { - gr_surface surface = NULL; +int res_create_display_surface(const char* name, GRSurface** pSurface) { + GRSurface* surface = NULL; int result = 0; png_structp png_ptr = NULL; png_infop info_ptr = NULL; @@ -239,8 +237,8 @@ int res_create_display_surface(const char* name, gr_surface* pSurface) { return result; } -int res_create_multi_display_surface(const char* name, int* frames, gr_surface** pSurface) { - gr_surface* surface = NULL; +int res_create_multi_display_surface(const char* name, int* frames, GRSurface*** pSurface) { + GRSurface** surface = NULL; int result = 0; png_structp png_ptr = NULL; png_infop info_ptr = NULL; @@ -275,7 +273,7 @@ int res_create_multi_display_surface(const char* name, int* frames, gr_surface** goto exit; } - surface = reinterpret_cast(malloc(*frames * sizeof(gr_surface))); + surface = reinterpret_cast(malloc(*frames * sizeof(GRSurface*))); if (surface == NULL) { result = -8; goto exit; @@ -302,7 +300,7 @@ int res_create_multi_display_surface(const char* name, int* frames, gr_surface** } free(p_row); - *pSurface = (gr_surface*) surface; + *pSurface = reinterpret_cast(surface); exit: png_destroy_read_struct(&png_ptr, &info_ptr, NULL); @@ -318,8 +316,8 @@ exit: return result; } -int res_create_alpha_surface(const char* name, gr_surface* pSurface) { - gr_surface surface = NULL; +int res_create_alpha_surface(const char* name, GRSurface** pSurface) { + GRSurface* surface = NULL; int result = 0; png_structp png_ptr = NULL; png_infop info_ptr = NULL; @@ -384,8 +382,8 @@ static int matches_locale(const char* loc, const char* locale) { int res_create_localized_alpha_surface(const char* name, const char* locale, - gr_surface* pSurface) { - gr_surface surface = NULL; + GRSurface** pSurface) { + GRSurface* surface = NULL; int result = 0; png_structp png_ptr = NULL; png_infop info_ptr = NULL; @@ -440,7 +438,7 @@ int res_create_localized_alpha_surface(const char* name, memcpy(surface->data + i*w, row, w); } - *pSurface = (gr_surface) surface; + *pSurface = reinterpret_cast(surface); break; } else { int i; @@ -456,6 +454,6 @@ exit: return result; } -void res_free_surface(gr_surface surface) { +void res_free_surface(GRSurface* surface) { free(surface); } diff --git a/screen_ui.cpp b/screen_ui.cpp index 52f22c246..5e73d37c4 100644 --- a/screen_ui.cpp +++ b/screen_ui.cpp @@ -89,11 +89,11 @@ void ScreenRecoveryUI::draw_background_locked(Icon icon) { gr_clear(); if (icon) { - gr_surface surface = backgroundIcon[icon]; + GRSurface* surface = backgroundIcon[icon]; if (icon == INSTALLING_UPDATE || icon == ERASING) { surface = installation[installingFrame]; } - gr_surface text_surface = backgroundText[icon]; + GRSurface* text_surface = backgroundText[icon]; int iconWidth = gr_get_width(surface); int iconHeight = gr_get_height(surface); @@ -132,7 +132,7 @@ void ScreenRecoveryUI::draw_progress_locked() { if (currentIcon == ERROR) return; if (currentIcon == INSTALLING_UPDATE || currentIcon == ERASING) { - gr_surface icon = installation[installingFrame]; + GRSurface* icon = installation[installingFrame]; gr_blit(icon, 0, 0, gr_get_width(icon), gr_get_height(icon), iconX, iconY); } @@ -357,21 +357,21 @@ void ScreenRecoveryUI::ProgressThreadLoop() { } } -void ScreenRecoveryUI::LoadBitmap(const char* filename, gr_surface* surface) { +void ScreenRecoveryUI::LoadBitmap(const char* filename, GRSurface** 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) { +void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, GRSurface*** 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) { +void ScreenRecoveryUI::LoadLocalizedBitmap(const char* filename, GRSurface** surface) { int result = res_create_localized_alpha_surface(filename, locale, surface); if (result < 0) { LOGE("missing bitmap %s\n(Code %d)\n", filename, result); diff --git a/screen_ui.h b/screen_ui.h index d473b8e94..46165d90c 100644 --- a/screen_ui.h +++ b/screen_ui.h @@ -73,13 +73,13 @@ class ScreenRecoveryUI : public RecoveryUI { bool rtl_locale; pthread_mutex_t updateMutex; - gr_surface backgroundIcon[5]; - gr_surface backgroundText[5]; - gr_surface *installation; - gr_surface progressBarEmpty; - gr_surface progressBarFill; - gr_surface stageMarkerEmpty; - gr_surface stageMarkerFill; + GRSurface* backgroundIcon[5]; + GRSurface* backgroundText[5]; + GRSurface** installation; + GRSurface* progressBarEmpty; + GRSurface* progressBarFill; + GRSurface* stageMarkerEmpty; + GRSurface* stageMarkerFill; ProgressType progressBarType; @@ -127,9 +127,9 @@ class ScreenRecoveryUI : public RecoveryUI { void DrawTextLine(int* y, const char* line, bool bold); void DrawTextLines(int* y, const char* const* lines); - 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); + void LoadBitmap(const char* filename, GRSurface** surface); + void LoadBitmapArray(const char* filename, int* frames, GRSurface*** surface); + void LoadLocalizedBitmap(const char* filename, GRSurface** surface); }; #endif // RECOVERY_UI_H -- cgit v1.2.3 From 43b748f2541ea77b2aa7b2f7eb4e0e8387beecca Mon Sep 17 00:00:00 2001 From: Sami Tolvanen Date: Fri, 17 Apr 2015 12:50:31 +0100 Subject: Don't remove existing explicitly stashed blocks When automatically stashing overlapping blocks, should the stash file already exist due to an explicit stash command, it's not safe to remove the stash file after the command has completed. Note that it is safe to assume that the stash file will remain in place during the execution of the next command, so we don't have take other measures to preserve overlapping blocks. The stash file itself will be removed by a free command when it's no longer needed. Bug: 20297065 Change-Id: I8ff1a798b94086adff183c5aac03260eb947ae2c --- updater/blockimg.c | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/updater/blockimg.c b/updater/blockimg.c index 90b55b2d2..d5344f991 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -635,12 +635,13 @@ lsout: } static int WriteStash(const char* base, const char* id, int blocks, uint8_t* buffer, - int checkspace) { + int checkspace, int *exists) { char *fn = NULL; char *cn = NULL; int fd = -1; int rc = -1; int res; + struct stat st; if (base == NULL || buffer == NULL) { goto wsout; @@ -658,6 +659,22 @@ static int WriteStash(const char* base, const char* id, int blocks, uint8_t* buf goto wsout; } + if (exists) { + res = stat(cn, &st); + + if (res == 0) { + // The file already exists and since the name is the hash of the contents, + // it's safe to assume the contents are identical (accidental hash collisions + // are unlikely) + fprintf(stderr, " skipping %d existing blocks in %s\n", blocks, cn); + *exists = 1; + rc = 0; + goto wsout; + } + + *exists = 0; + } + fprintf(stderr, " writing %d blocks to %s\n", blocks, cn); fd = TEMP_FAILURE_RETRY(open(fn, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, STASH_FILE_MODE)); @@ -821,7 +838,7 @@ static int SaveStash(const char* base, char** wordsave, uint8_t** buffer, size_t } fprintf(stderr, "stashing %d blocks to %s\n", blocks, id); - return WriteStash(base, id, blocks, *buffer, 0); + return WriteStash(base, id, blocks, *buffer, 0, NULL); } static int FreeStash(const char* base, const char* id) { @@ -997,6 +1014,7 @@ static int LoadSrcTgtVersion3(CommandParameters* params, RangeSet** tgt, int* sr int onehash, int* overlap) { char* srchash = NULL; char* tgthash = NULL; + int stash_exists = 0; int overlap_blocks = 0; int rc = -1; uint8_t* tgtbuffer = NULL; @@ -1052,13 +1070,16 @@ static int LoadSrcTgtVersion3(CommandParameters* params, RangeSet** tgt, int* sr fprintf(stderr, "stashing %d overlapping blocks to %s\n", *src_blocks, srchash); - if (WriteStash(params->stashbase, srchash, *src_blocks, params->buffer, 1) != 0) { + if (WriteStash(params->stashbase, srchash, *src_blocks, params->buffer, 1, + &stash_exists) != 0) { fprintf(stderr, "failed to stash overlapping source blocks\n"); goto v3out; } // Can be deleted when the write has completed - params->freestash = srchash; + if (!stash_exists) { + params->freestash = srchash; + } } // Source blocks have expected content, command can proceed @@ -1068,12 +1089,9 @@ static int LoadSrcTgtVersion3(CommandParameters* params, RangeSet** tgt, int* sr if (*overlap && LoadStash(params->stashbase, srchash, 1, NULL, ¶ms->buffer, ¶ms->bufsize, 1) == 0) { - // Overlapping source blocks were previously stashed, command can proceed - if (params->canwrite) { - // We didn't create the stash, so delete after write only if we will - // actually perform the write - params->freestash = srchash; - } + // Overlapping source blocks were previously stashed, command can proceed. + // We are recovering from an interrupted command, so we don't know if the + // stash can safely be deleted after this command. rc = 0; goto v3out; } -- cgit v1.2.3 From c57453d5377a13445c4b1d3f73c0e0ab19aa0c1e Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Thu, 23 Apr 2015 20:26:56 -0700 Subject: init re-execs to set its security context now. Change-Id: I0a014f8dddfe775159903b5d6fa632733fef692c --- etc/init.rc | 7 ------- 1 file changed, 7 deletions(-) diff --git a/etc/init.rc b/etc/init.rc index c78a44a2a..6c07c6027 100644 --- a/etc/init.rc +++ b/etc/init.rc @@ -1,13 +1,6 @@ import /init.recovery.${ro.hardware}.rc on early-init - # Apply strict SELinux checking of PROT_EXEC on mmap/mprotect calls. - write /sys/fs/selinux/checkreqprot 0 - - # Set the security context for the init process. - # This should occur before anything else (e.g. ueventd) is started. - setcon u:r:init:s0 - start ueventd start healthd -- cgit v1.2.3 From c819dbe95bf80645178b0180f519ab2983da01a0 Mon Sep 17 00:00:00 2001 From: Nick Kralevich Date: Fri, 24 Apr 2015 16:58:33 +0000 Subject: Revert "init re-execs to set its security context now." shamu isn't booting now This reverts commit c57453d5377a13445c4b1d3f73c0e0ab19aa0c1e. Change-Id: I8efbf6260f5fcf983e5056fac6d03916415b944e --- etc/init.rc | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/etc/init.rc b/etc/init.rc index 6c07c6027..c78a44a2a 100644 --- a/etc/init.rc +++ b/etc/init.rc @@ -1,6 +1,13 @@ import /init.recovery.${ro.hardware}.rc on early-init + # Apply strict SELinux checking of PROT_EXEC on mmap/mprotect calls. + write /sys/fs/selinux/checkreqprot 0 + + # Set the security context for the init process. + # This should occur before anything else (e.g. ueventd) is started. + setcon u:r:init:s0 + start ueventd start healthd -- cgit v1.2.3 From 6f76dd58f496f06c7e332fb5269d20aa2a301d4a Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 24 Apr 2015 19:41:26 +0000 Subject: Revert "Revert "init re-execs to set its security context now."" This reverts commit c819dbe95bf80645178b0180f519ab2983da01a0. Bug: http://b/19702273 Change-Id: I5c75b148a12e644dd247a4df4f67dc9b4b9ff8cf --- etc/init.rc | 7 ------- 1 file changed, 7 deletions(-) diff --git a/etc/init.rc b/etc/init.rc index c78a44a2a..6c07c6027 100644 --- a/etc/init.rc +++ b/etc/init.rc @@ -1,13 +1,6 @@ import /init.recovery.${ro.hardware}.rc on early-init - # Apply strict SELinux checking of PROT_EXEC on mmap/mprotect calls. - write /sys/fs/selinux/checkreqprot 0 - - # Set the security context for the init process. - # This should occur before anything else (e.g. ueventd) is started. - setcon u:r:init:s0 - start ueventd start healthd -- cgit v1.2.3 From f7466f9f2334b0e9025e1c7ecf65b4d04a246b20 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 27 Apr 2015 18:39:27 -0700 Subject: Stop using adb_strtok, and check argument validity. (cherry picked from commit ba45ddf37cf4543143af6b2e27fc1214f3dbe892) Change-Id: Iba4f77f7db54ca0184437bd8ea96abfadabc72a3 --- minadbd/services.cpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/minadbd/services.cpp b/minadbd/services.cpp index a83256796..dd1fd7c4b 100644 --- a/minadbd/services.cpp +++ b/minadbd/services.cpp @@ -43,15 +43,16 @@ void* service_bootstrap_func(void* x) { return 0; } -static void sideload_host_service(int sfd, void* cookie) { - char* saveptr; - const char* s = adb_strtok_r(reinterpret_cast(cookie), ":", &saveptr); - uint64_t file_size = strtoull(s, NULL, 10); - s = adb_strtok_r(NULL, ":", &saveptr); - uint32_t block_size = strtoul(s, NULL, 10); - - printf("sideload-host file size %" PRIu64 " block size %" PRIu32 "\n", - file_size, block_size); +static void sideload_host_service(int sfd, void* data) { + const char* args = reinterpret_cast(data); + int file_size; + int block_size; + if (sscanf(args, "%d:%d", &file_size, &block_size) != 2) { + printf("bad sideload-host arguments: %s\n", args); + exit(1); + } + + printf("sideload-host file size %d block size %d\n", file_size, block_size); int result = run_adb_fuse(sfd, file_size, block_size); -- cgit v1.2.3 From 2f5feedf1d705b53e5bf90c8b5207dd91f4522f1 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 28 Apr 2015 17:24:24 -0700 Subject: Check all lseek calls succeed. Also add missing TEMP_FAILURE_RETRYs on read, write, and lseek. Bug: http://b/20625546 Change-Id: I03b198e11c1921b35518ee2dd005a7cfcf4fd94b (cherry picked from commit 7bad7c4646ee8fd8d6e6ed0ffd3ddbb0c1b41a2f) --- adb_install.cpp | 2 +- applypatch/applypatch.c | 51 +++++++++++++++++++++-------------------- fuse_sdcard_provider.c | 16 ++++++------- fuse_sideload.c | 16 ++++++------- minui/events.cpp | 3 ++- minzip/SysUtil.c | 10 ++++---- minzip/Zip.c | 6 ++--- mtdutils/flash_image.c | 8 +++---- mtdutils/mtdutils.c | 54 ++++++++++++++++++-------------------------- mtdutils/mtdutils.h | 2 -- tools/ota/check-lost+found.c | 2 +- ui.cpp | 2 +- uncrypt/uncrypt.c | 20 +++++++++------- updater/blockimg.c | 48 ++++++++++++++++----------------------- 14 files changed, 112 insertions(+), 128 deletions(-) diff --git a/adb_install.cpp b/adb_install.cpp index ebd4cac00..e3b94ea59 100644 --- a/adb_install.cpp +++ b/adb_install.cpp @@ -42,7 +42,7 @@ set_usb_driver(bool enabled) { ui->Print("failed to open driver control: %s\n", strerror(errno)); return; } - if (write(fd, enabled ? "1" : "0", 1) < 0) { + if (TEMP_FAILURE_RETRY(write(fd, enabled ? "1" : "0", 1)) == -1) { ui->Print("failed to set driver control: %s\n", strerror(errno)); } if (close(fd) < 0) { diff --git a/applypatch/applypatch.c b/applypatch/applypatch.c index 2c86e0984..6f02a38ee 100644 --- a/applypatch/applypatch.c +++ b/applypatch/applypatch.c @@ -422,20 +422,19 @@ int WriteToPartition(unsigned char* data, size_t len, int attempt; for (attempt = 0; attempt < 2; ++attempt) { - lseek(fd, start, SEEK_SET); + if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1) { + printf("failed seek on %s: %s\n", + partition, strerror(errno)); + return -1; + } while (start < len) { size_t to_write = len - start; if (to_write > 1<<20) to_write = 1<<20; - ssize_t written = write(fd, data+start, to_write); - if (written < 0) { - if (errno == EINTR) { - written = 0; - } else { - printf("failed write writing to %s (%s)\n", - partition, strerror(errno)); - return -1; - } + ssize_t written = TEMP_FAILURE_RETRY(write(fd, data+start, to_write)); + if (written == -1) { + printf("failed write writing to %s: %s\n", partition, strerror(errno)); + return -1; } start += written; } @@ -460,13 +459,20 @@ int WriteToPartition(unsigned char* data, size_t len, // won't just be reading the cache. sync(); int dc = open("/proc/sys/vm/drop_caches", O_WRONLY); - write(dc, "3\n", 2); + if (TEMP_FAILURE_RETRY(write(dc, "3\n", 2)) == -1) { + printf("write to /proc/sys/vm/drop_caches failed: %s\n", strerror(errno)); + } else { + printf(" caches dropped\n"); + } close(dc); sleep(1); - printf(" caches dropped\n"); // verify - lseek(fd, 0, SEEK_SET); + if (TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_SET)) == -1) { + printf("failed to seek back to beginning of %s: %s\n", + partition, strerror(errno)); + return -1; + } unsigned char buffer[4096]; start = len; size_t p; @@ -476,15 +482,12 @@ int WriteToPartition(unsigned char* data, size_t len, size_t so_far = 0; while (so_far < to_read) { - ssize_t read_count = read(fd, buffer+so_far, to_read-so_far); - if (read_count < 0) { - if (errno == EINTR) { - read_count = 0; - } else { - printf("verify read error %s at %zu: %s\n", - partition, p, strerror(errno)); - return -1; - } + ssize_t read_count = + TEMP_FAILURE_RETRY(read(fd, buffer+so_far, to_read-so_far)); + if (read_count == -1) { + 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 %zu: %zd %zu %s\n", @@ -625,8 +628,8 @@ ssize_t FileSink(const unsigned char* data, ssize_t len, void* token) { ssize_t done = 0; ssize_t wrote; while (done < (ssize_t) len) { - wrote = write(fd, data+done, len-done); - if (wrote <= 0) { + wrote = TEMP_FAILURE_RETRY(write(fd, data+done, len-done)); + if (wrote == -1) { printf("error writing %d bytes: %s\n", (int)(len-done), strerror(errno)); return done; } diff --git a/fuse_sdcard_provider.c b/fuse_sdcard_provider.c index ca8c914f9..4565c7b5b 100644 --- a/fuse_sdcard_provider.c +++ b/fuse_sdcard_provider.c @@ -36,19 +36,17 @@ struct file_data { static int read_block_file(void* cookie, uint32_t block, uint8_t* buffer, uint32_t fetch_size) { struct file_data* fd = (struct file_data*)cookie; - if (lseek(fd->fd, block * fd->block_size, SEEK_SET) < 0) { - printf("seek on sdcard failed: %s\n", strerror(errno)); + off64_t offset = ((off64_t) block) * fd->block_size; + if (TEMP_FAILURE_RETRY(lseek64(fd->fd, offset, SEEK_SET)) == -1) { + fprintf(stderr, "seek on sdcard failed: %s\n", strerror(errno)); return -EIO; } while (fetch_size > 0) { - ssize_t r = read(fd->fd, buffer, fetch_size); - if (r < 0) { - if (r != -EINTR) { - printf("read on sdcard failed: %s\n", strerror(errno)); - return -EIO; - } - r = 0; + ssize_t r = TEMP_FAILURE_RETRY(read(fd->fd, buffer, fetch_size)); + if (r == -1) { + fprintf(stderr, "read on sdcard failed: %s\n", strerror(errno)); + return -EIO; } fetch_size -= r; buffer += r; diff --git a/fuse_sideload.c b/fuse_sideload.c index 1dd84e97a..48e6cc53a 100644 --- a/fuse_sideload.c +++ b/fuse_sideload.c @@ -442,14 +442,12 @@ int run_fuse_sideload(struct provider_vtab* vtab, void* cookie, } uint8_t request_buffer[sizeof(struct fuse_in_header) + PATH_MAX*8]; for (;;) { - ssize_t len = read(fd.ffd, request_buffer, sizeof(request_buffer)); - if (len < 0) { - if (errno != EINTR) { - perror("read request"); - if (errno == ENODEV) { - result = -1; - break; - } + ssize_t len = TEMP_FAILURE_RETRY(read(fd.ffd, request_buffer, sizeof(request_buffer))); + if (len == -1) { + perror("read request"); + if (errno == ENODEV) { + result = -1; + break; } continue; } @@ -508,7 +506,7 @@ int run_fuse_sideload(struct provider_vtab* vtab, void* cookie, outhdr.len = sizeof(outhdr); outhdr.error = result; outhdr.unique = hdr->unique; - write(fd.ffd, &outhdr, sizeof(outhdr)); + TEMP_FAILURE_RETRY(write(fd.ffd, &outhdr, sizeof(outhdr))); } } diff --git a/minui/events.cpp b/minui/events.cpp index 2d47a587f..3b2262a4b 100644 --- a/minui/events.cpp +++ b/minui/events.cpp @@ -15,6 +15,7 @@ */ #include +#include #include #include #include @@ -165,7 +166,7 @@ void ev_dispatch(void) { int ev_get_input(int fd, uint32_t epevents, input_event* ev) { if (epevents & EPOLLIN) { - ssize_t r = read(fd, ev, sizeof(*ev)); + ssize_t r = TEMP_FAILURE_RETRY(read(fd, ev, sizeof(*ev))); if (r == sizeof(*ev)) { return 0; } diff --git a/minzip/SysUtil.c b/minzip/SysUtil.c index ac6f5c33f..b160c9e3d 100644 --- a/minzip/SysUtil.c +++ b/minzip/SysUtil.c @@ -27,11 +27,13 @@ static int getFileStartAndLength(int fd, off_t *start_, size_t *length_) assert(start_ != NULL); assert(length_ != NULL); - start = lseek(fd, 0L, SEEK_CUR); - end = lseek(fd, 0L, SEEK_END); - (void) lseek(fd, start, SEEK_SET); + // TODO: isn't start always 0 for the single call site? just use fstat instead? - if (start == (off_t) -1 || end == (off_t) -1) { + start = TEMP_FAILURE_RETRY(lseek(fd, 0L, SEEK_CUR)); + end = TEMP_FAILURE_RETRY(lseek(fd, 0L, SEEK_END)); + + if (TEMP_FAILURE_RETRY(lseek(fd, start, SEEK_SET)) == -1 || + start == (off_t) -1 || end == (off_t) -1) { LOGE("could not determine length of file\n"); return -1; } diff --git a/minzip/Zip.c b/minzip/Zip.c index d3ff79be6..40712e03a 100644 --- a/minzip/Zip.c +++ b/minzip/Zip.c @@ -675,13 +675,11 @@ static bool writeProcessFunction(const unsigned char *data, int dataLen, } ssize_t soFar = 0; while (true) { - ssize_t n = write(fd, data+soFar, dataLen-soFar); + ssize_t n = TEMP_FAILURE_RETRY(write(fd, data+soFar, dataLen-soFar)); if (n <= 0) { LOGE("Error writing %zd bytes from zip file from %p: %s\n", dataLen-soFar, data+soFar, strerror(errno)); - if (errno != EINTR) { - return false; - } + return false; } else if (n > 0) { soFar += n; if (soFar == dataLen) return true; diff --git a/mtdutils/flash_image.c b/mtdutils/flash_image.c index 5657dfc82..36ffa1314 100644 --- a/mtdutils/flash_image.c +++ b/mtdutils/flash_image.c @@ -72,7 +72,7 @@ int main(int argc, char **argv) { if (fd < 0) die("error opening %s", argv[2]); char header[HEADER_SIZE]; - int headerlen = read(fd, header, sizeof(header)); + int headerlen = TEMP_FAILURE_RETRY(read(fd, header, sizeof(header))); if (headerlen <= 0) die("error reading %s header", argv[2]); MtdReadContext *in = mtd_read_partition(partition); @@ -104,7 +104,7 @@ int main(int argc, char **argv) { if (wrote != headerlen) die("error writing %s", argv[1]); int len; - while ((len = read(fd, buf, sizeof(buf))) > 0) { + while ((len = TEMP_FAILURE_RETRY(read(fd, buf, sizeof(buf)))) > 0) { wrote = mtd_write_data(out, buf, len); if (wrote != len) die("error writing %s", argv[1]); } @@ -125,13 +125,13 @@ int main(int argc, char **argv) { if (mtd_partition_info(partition, NULL, &block_size, NULL)) die("error getting %s block size", argv[1]); - if (lseek(fd, headerlen, SEEK_SET) != headerlen) + if (TEMP_FAILURE_RETRY(lseek(fd, headerlen, SEEK_SET)) != headerlen) die("error rewinding %s", argv[2]); int left = block_size - headerlen; while (left < 0) left += block_size; while (left > 0) { - len = read(fd, buf, left > (int)sizeof(buf) ? (int)sizeof(buf) : left); + len = TEMP_FAILURE_RETRY(read(fd, buf, left > (int)sizeof(buf) ? (int)sizeof(buf) : left)); if (len <= 0) die("error reading %s", argv[2]); if (mtd_write_data(out, buf, len) != len) die("error writing %s", argv[1]); diff --git a/mtdutils/mtdutils.c b/mtdutils/mtdutils.c index 9a17e38d8..cc3033444 100644 --- a/mtdutils/mtdutils.c +++ b/mtdutils/mtdutils.c @@ -108,7 +108,7 @@ mtd_scan_partitions() if (fd < 0) { goto bail; } - nbytes = read(fd, buf, sizeof(buf) - 1); + nbytes = TEMP_FAILURE_RETRY(read(fd, buf, sizeof(buf) - 1)); close(fd); if (nbytes < 0) { goto bail; @@ -279,12 +279,6 @@ MtdReadContext *mtd_read_partition(const MtdPartition *partition) return ctx; } -// Seeks to a location in the partition. Don't mix with reads of -// anything other than whole blocks; unpredictable things will result. -void mtd_read_skip_to(const MtdReadContext* ctx, size_t offset) { - lseek64(ctx->fd, offset, SEEK_SET); -} - static int read_block(const MtdPartition *partition, int fd, char *data) { struct mtd_ecc_stats before, after; @@ -293,13 +287,18 @@ static int read_block(const MtdPartition *partition, int fd, char *data) return -1; } - loff_t pos = lseek64(fd, 0, SEEK_CUR); + loff_t pos = TEMP_FAILURE_RETRY(lseek64(fd, 0, SEEK_CUR)); + if (pos == -1) { + printf("mtd: read_block: couldn't SEEK_CUR: %s\n", strerror(errno)); + return -1; + } ssize_t size = partition->erase_size; int mgbb; while (pos + size <= (int) partition->size) { - if (lseek64(fd, pos, SEEK_SET) != pos || read(fd, data, size) != size) { + if (TEMP_FAILURE_RETRY(lseek64(fd, pos, SEEK_SET)) != pos || + TEMP_FAILURE_RETRY(read(fd, data, size)) != size) { printf("mtd: read error at 0x%08llx (%s)\n", pos, strerror(errno)); } else if (ioctl(fd, ECCGETSTATS, &after)) { @@ -409,8 +408,11 @@ static int write_block(MtdWriteContext *ctx, const char *data) const MtdPartition *partition = ctx->partition; int fd = ctx->fd; - off_t pos = lseek(fd, 0, SEEK_CUR); - if (pos == (off_t) -1) return 1; + off_t pos = TEMP_FAILURE_RETRY(lseek(fd, 0, SEEK_CUR)); + if (pos == (off_t) -1) { + printf("mtd: write_block: couldn't SEEK_CUR: %s\n", strerror(errno)); + return -1; + } ssize_t size = partition->erase_size; while (pos + size <= (int) partition->size) { @@ -435,15 +437,15 @@ static int write_block(MtdWriteContext *ctx, const char *data) pos, strerror(errno)); continue; } - if (lseek(fd, pos, SEEK_SET) != pos || - write(fd, data, size) != size) { + if (TEMP_FAILURE_RETRY(lseek(fd, pos, SEEK_SET)) != pos || + TEMP_FAILURE_RETRY(write(fd, data, size)) != size) { printf("mtd: write error at 0x%08lx (%s)\n", pos, strerror(errno)); } char verify[size]; - if (lseek(fd, pos, SEEK_SET) != pos || - read(fd, verify, size) != size) { + if (TEMP_FAILURE_RETRY(lseek(fd, pos, SEEK_SET)) != pos || + TEMP_FAILURE_RETRY(read(fd, verify, size)) != size) { printf("mtd: re-read error at 0x%08lx (%s)\n", pos, strerror(errno)); continue; @@ -512,8 +514,11 @@ off_t mtd_erase_blocks(MtdWriteContext *ctx, int blocks) ctx->stored = 0; } - off_t pos = lseek(ctx->fd, 0, SEEK_CUR); - if ((off_t) pos == (off_t) -1) return pos; + off_t pos = TEMP_FAILURE_RETRY(lseek(ctx->fd, 0, SEEK_CUR)); + if ((off_t) pos == (off_t) -1) { + printf("mtd_erase_blocks: couldn't SEEK_CUR: %s\n", strerror(errno)); + return -1; + } const int total = (ctx->partition->size - pos) / ctx->partition->erase_size; if (blocks < 0) blocks = total; @@ -554,18 +559,3 @@ int mtd_write_close(MtdWriteContext *ctx) free(ctx); return r; } - -/* Return the offset of the first good block at or after pos (which - * might be pos itself). - */ -off_t mtd_find_write_start(MtdWriteContext *ctx, off_t pos) { - int i; - for (i = 0; i < ctx->bad_block_count; ++i) { - if (ctx->bad_block_offsets[i] == pos) { - pos += ctx->partition->erase_size; - } else if (ctx->bad_block_offsets[i] > pos) { - return pos; - } - } - return pos; -} diff --git a/mtdutils/mtdutils.h b/mtdutils/mtdutils.h index 2708c4318..8059d6a4d 100644 --- a/mtdutils/mtdutils.h +++ b/mtdutils/mtdutils.h @@ -49,12 +49,10 @@ typedef struct MtdWriteContext MtdWriteContext; MtdReadContext *mtd_read_partition(const MtdPartition *); ssize_t mtd_read_data(MtdReadContext *, char *data, size_t data_len); void mtd_read_close(MtdReadContext *); -void mtd_read_skip_to(const MtdReadContext *, size_t offset); MtdWriteContext *mtd_write_partition(const MtdPartition *); ssize_t mtd_write_data(MtdWriteContext *, const char *data, size_t data_len); off_t mtd_erase_blocks(MtdWriteContext *, int blocks); /* 0 ok, -1 for all */ -off_t mtd_find_write_start(MtdWriteContext *ctx, off_t pos); int mtd_write_close(MtdWriteContext *); #ifdef __cplusplus diff --git a/tools/ota/check-lost+found.c b/tools/ota/check-lost+found.c index cbf792629..8ce12d39f 100644 --- a/tools/ota/check-lost+found.c +++ b/tools/ota/check-lost+found.c @@ -78,7 +78,7 @@ int main(int argc __attribute__((unused)), char **argv __attribute__((unused))) snprintf(fn, sizeof(fn), "%s/%s", kPartitions[i], "dirty"); fd = open(fn, O_WRONLY|O_CREAT, 0444); if (fd >= 0) { // Don't sweat it if we can't write the file. - write(fd, fn, sizeof(fn)); // write, you know, some data + TEMP_FAILURE_RETRY(write(fd, fn, sizeof(fn))); // write, you know, some data close(fd); unlink(fn); } diff --git a/ui.cpp b/ui.cpp index dca325fea..1a0b079cc 100644 --- a/ui.cpp +++ b/ui.cpp @@ -251,7 +251,7 @@ bool RecoveryUI::IsUsbConnected() { char buf; // USB is connected if android_usb state is CONNECTED or CONFIGURED. - int connected = (read(fd, &buf, 1) == 1) && (buf == 'C'); + int connected = (TEMP_FAILURE_RETRY(read(fd, &buf, 1)) == 1) && (buf == 'C'); if (close(fd) < 0) { printf("failed to close /sys/class/android_usb/android0/state: %s\n", strerror(errno)); diff --git a/uncrypt/uncrypt.c b/uncrypt/uncrypt.c index aa75210b0..da035dfba 100644 --- a/uncrypt/uncrypt.c +++ b/uncrypt/uncrypt.c @@ -65,12 +65,15 @@ static struct fstab* fstab = NULL; static int write_at_offset(unsigned char* buffer, size_t size, int wfd, off64_t offset) { - lseek64(wfd, offset, SEEK_SET); + if (TEMP_FAILURE_RETRY(lseek64(wfd, offset, SEEK_SET)) == -1) { + ALOGE("error seeking to offset %lld: %s\n", offset, strerror(errno)); + return -1; + } size_t written = 0; while (written < size) { - ssize_t wrote = write(wfd, buffer + written, size - written); - if (wrote < 0) { - ALOGE("error writing offset %lld: %s\n", offset, strerror(errno)); + ssize_t wrote = TEMP_FAILURE_RETRY(write(wfd, buffer + written, size - written)); + if (wrote == -1) { + ALOGE("error writing offset %lld: %s\n", (offset + written), strerror(errno)); return -1; } written += wrote; @@ -275,8 +278,9 @@ int produce_block_map(const char* path, const char* map_file, const char* blk_de if (encrypted) { size_t so_far = 0; while (so_far < sb.st_blksize && pos < sb.st_size) { - ssize_t this_read = read(fd, buffers[tail] + so_far, sb.st_blksize - so_far); - if (this_read < 0) { + ssize_t this_read = + TEMP_FAILURE_RETRY(read(fd, buffers[tail] + so_far, sb.st_blksize - so_far)); + if (this_read == -1) { ALOGE("failed to read: %s\n", strerror(errno)); return -1; } @@ -340,8 +344,8 @@ void wipe_misc() { size_t written = 0; size_t size = sizeof(zeroes); while (written < size) { - ssize_t w = write(fd, zeroes, size-written); - if (w < 0 && errno != EINTR) { + ssize_t w = TEMP_FAILURE_RETRY(write(fd, zeroes, size-written)); + if (w == -1) { ALOGE("zero write failed: %s\n", strerror(errno)); return; } else { diff --git a/updater/blockimg.c b/updater/blockimg.c index d5344f991..532e7b845 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -113,13 +113,12 @@ static int range_overlaps(RangeSet* r1, RangeSet* r2) { static int read_all(int fd, uint8_t* data, size_t size) { size_t so_far = 0; while (so_far < size) { - ssize_t r = read(fd, data+so_far, size-so_far); - if (r < 0 && errno != EINTR) { + ssize_t r = TEMP_FAILURE_RETRY(read(fd, data+so_far, size-so_far)); + if (r == -1) { fprintf(stderr, "read failed: %s\n", strerror(errno)); return -1; - } else { - so_far += r; } + so_far += r; } return 0; } @@ -127,13 +126,12 @@ static int read_all(int fd, uint8_t* data, size_t size) { static int write_all(int fd, const uint8_t* data, size_t size) { size_t written = 0; while (written < size) { - ssize_t w = write(fd, data+written, size-written); - if (w < 0 && errno != EINTR) { + ssize_t w = TEMP_FAILURE_RETRY(write(fd, data+written, size-written)); + if (w == -1) { fprintf(stderr, "write failed: %s\n", strerror(errno)); return -1; - } else { - written += w; } + written += w; } if (fsync(fd) == -1) { @@ -144,19 +142,13 @@ static int write_all(int fd, const uint8_t* data, size_t size) { return 0; } -static int check_lseek(int fd, off64_t offset, int whence) { - while (true) { - off64_t ret = lseek64(fd, offset, whence); - if (ret < 0) { - if (errno != EINTR) { - fprintf(stderr, "lseek64 failed: %s\n", strerror(errno)); - return -1; - } - } else { - break; - } +static bool check_lseek(int fd, off64_t offset, int whence) { + off64_t rc = TEMP_FAILURE_RETRY(lseek64(fd, offset, whence)); + if (rc == -1) { + fprintf(stderr, "lseek64 failed: %s\n", strerror(errno)); + return false; } - return 0; + return true; } static void allocate(size_t size, uint8_t** buffer, size_t* buffer_alloc) { @@ -213,8 +205,8 @@ static ssize_t RangeSinkWrite(const uint8_t* data, ssize_t size, void* token) { rss->p_remain = (rss->tgt->pos[rss->p_block * 2 + 1] - rss->tgt->pos[rss->p_block * 2]) * BLOCKSIZE; - if (check_lseek(rss->fd, (off64_t)rss->tgt->pos[rss->p_block*2] * BLOCKSIZE, - SEEK_SET) == -1) { + if (!check_lseek(rss->fd, (off64_t)rss->tgt->pos[rss->p_block*2] * BLOCKSIZE, + SEEK_SET)) { break; } } else { @@ -306,7 +298,7 @@ static int ReadBlocks(RangeSet* src, uint8_t* buffer, int fd) { } for (i = 0; i < src->count; ++i) { - if (check_lseek(fd, (off64_t) src->pos[i * 2] * BLOCKSIZE, SEEK_SET) == -1) { + if (!check_lseek(fd, (off64_t) src->pos[i * 2] * BLOCKSIZE, SEEK_SET)) { return -1; } @@ -332,7 +324,7 @@ static int WriteBlocks(RangeSet* tgt, uint8_t* buffer, int fd) { } for (i = 0; i < tgt->count; ++i) { - if (check_lseek(fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET) == -1) { + if (!check_lseek(fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) { return -1; } @@ -1217,7 +1209,7 @@ static int PerformCommandZero(CommandParameters* params) { if (params->canwrite) { for (i = 0; i < tgt->count; ++i) { - if (check_lseek(params->fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET) == -1) { + if (!check_lseek(params->fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) { goto pczout; } @@ -1271,7 +1263,7 @@ static int PerformCommandNew(CommandParameters* params) { rss.p_block = 0; rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE; - if (check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET) == -1) { + if (!check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET)) { goto pcnout; } @@ -1367,7 +1359,7 @@ static int PerformCommandDiff(CommandParameters* params) { rss.p_block = 0; rss.p_remain = (tgt->pos[1] - tgt->pos[0]) * BLOCKSIZE; - if (check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET) == -1) { + if (!check_lseek(params->fd, (off64_t) tgt->pos[0] * BLOCKSIZE, SEEK_SET)) { goto pcdout; } @@ -1906,7 +1898,7 @@ Value* RangeSha1Fn(const char* name, State* state, int argc, Expr* argv[]) { int i, j; for (i = 0; i < rs->count; ++i) { - if (check_lseek(fd, (off64_t)rs->pos[i*2] * BLOCKSIZE, SEEK_SET) == -1) { + if (!check_lseek(fd, (off64_t)rs->pos[i*2] * BLOCKSIZE, SEEK_SET)) { ErrorAbort(state, "failed to seek %s: %s", blockdev_filename->data, strerror(errno)); goto done; -- cgit v1.2.3 From 87ec73a264530f5fa19cde2598d5e65c4c67a686 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 1 May 2015 17:25:24 -0700 Subject: Fix minadb_test build breakage. Change-Id: I98bb900debb7d7dd57d3f8f84d605163ec192b03 (cherry picked from commit 3e7d82c621240bb80f9882c64377c4f5f3d97c7b) --- minadbd/Android.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minadbd/Android.mk b/minadbd/Android.mk index cbfd76e4e..083063be1 100644 --- a/minadbd/Android.mk +++ b/minadbd/Android.mk @@ -31,6 +31,6 @@ LOCAL_SRC_FILES := fuse_adb_provider_test.cpp LOCAL_CFLAGS := $(minadbd_cflags) LOCAL_C_INCLUDES := $(LOCAL_PATH) system/core/adb LOCAL_STATIC_LIBRARIES := libminadbd -LOCAL_SHARED_LIBRARIES := liblog +LOCAL_SHARED_LIBRARIES := liblog libbase include $(BUILD_NATIVE_TEST) -- cgit v1.2.3 From 15931924e1820d4c2bc9f9c0c7734c43c92b866b Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 1 May 2015 17:07:44 -0700 Subject: Move minadb over to new API. Change-Id: I889bcf2222245c7665287513669cae8831e37081 (cherry picked from commit 4039933c62f52dda06e6f355cf42ac9b392d0888) --- minadbd/Android.mk | 1 + minadbd/fuse_adb_provider.cpp | 21 ++++++++------------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/minadbd/Android.mk b/minadbd/Android.mk index 083063be1..8398cefe4 100644 --- a/minadbd/Android.mk +++ b/minadbd/Android.mk @@ -20,6 +20,7 @@ LOCAL_CFLAGS := $(minadbd_cflags) LOCAL_CONLY_FLAGS := -Wimplicit-function-declaration LOCAL_C_INCLUDES := bootable/recovery system/core/adb LOCAL_WHOLE_STATIC_LIBRARIES := libadbd +LOCAL_STATIC_LIBRARIES := libbase include $(BUILD_STATIC_LIBRARY) diff --git a/minadbd/fuse_adb_provider.cpp b/minadbd/fuse_adb_provider.cpp index 5da7fd76c..d71807dfb 100644 --- a/minadbd/fuse_adb_provider.cpp +++ b/minadbd/fuse_adb_provider.cpp @@ -26,13 +26,10 @@ #include "fuse_adb_provider.h" #include "fuse_sideload.h" -int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, - uint32_t fetch_size) { - struct adb_data* ad = (struct adb_data*)cookie; +int read_block_adb(void* data, uint32_t block, uint8_t* buffer, uint32_t fetch_size) { + adb_data* ad = reinterpret_cast(data); - char buf[10]; - snprintf(buf, sizeof(buf), "%08u", block); - if (!WriteStringFully(ad->sfd, buf)) { + if (!WriteFdFmt(ad->sfd, "%08u", block)) { fprintf(stderr, "failed to write to adb host: %s\n", strerror(errno)); return -EIO; } @@ -45,20 +42,18 @@ int read_block_adb(void* cookie, uint32_t block, uint8_t* buffer, return 0; } -static void close_adb(void* cookie) { - struct adb_data* ad = (struct adb_data*)cookie; - - WriteStringFully(ad->sfd, "DONEDONE"); +static void close_adb(void* data) { + adb_data* ad = reinterpret_cast(data); + WriteFdExactly(ad->sfd, "DONEDONE"); } int run_adb_fuse(int sfd, uint64_t file_size, uint32_t block_size) { - struct adb_data ad; - struct provider_vtab vtab; - + adb_data ad; ad.sfd = sfd; ad.file_size = file_size; ad.block_size = block_size; + provider_vtab vtab; vtab.read_block = read_block_adb; vtab.close = close_adb; -- cgit v1.2.3 From 4cf34d5d297db4f7130a7f1472c394e38ea0e642 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 1 May 2015 22:29:01 -0700 Subject: Fix mips64 minadbd_test build. Looks like the mips64 linker isn't as good as the others at GCing unused stuff, which means it needs libcutils. Change-Id: I5f768e44514350fb81e5360351db3e9cc4201702 (cherry picked from commit dbb20c48633e63c7c244e84f3fea76e083e225d7) --- minadbd/Android.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minadbd/Android.mk b/minadbd/Android.mk index 8398cefe4..a7a3e087d 100644 --- a/minadbd/Android.mk +++ b/minadbd/Android.mk @@ -32,6 +32,6 @@ LOCAL_SRC_FILES := fuse_adb_provider_test.cpp LOCAL_CFLAGS := $(minadbd_cflags) LOCAL_C_INCLUDES := $(LOCAL_PATH) system/core/adb LOCAL_STATIC_LIBRARIES := libminadbd -LOCAL_SHARED_LIBRARIES := liblog libbase +LOCAL_SHARED_LIBRARIES := liblog libbase libcutils include $(BUILD_NATIVE_TEST) -- cgit v1.2.3 From 6d820f2c91f6cafcec5b378ae896b7118ca8921d Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Mon, 4 May 2015 09:34:29 -0700 Subject: Turn on text display for debuggable builds For userdebug and eng builds, turn on the text display automatically if no command is specified. (cherry-pick of 785d22c88cda46972331c04ebc9df97371a696da.) Bug: http://b/17489952 Change-Id: I38377c45f2a8e45ca788e5506695aa88c769cbcf --- recovery.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/recovery.cpp b/recovery.cpp index 4dd827919..65172dfcc 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -1086,6 +1086,13 @@ main(int argc, char **argv) { } else if (!just_exit) { status = INSTALL_NONE; // No command specified ui->SetBackground(RecoveryUI::NO_COMMAND); + + // http://b/17489952 + // If this is an eng or userdebug build, automatically turn on the + // text display if no command is specified. + if (is_ro_debuggable()) { + ui->ShowText(true); + } } if (!sideload_auto_reboot && (status == INSTALL_ERROR || status == INSTALL_CORRUPT)) { -- cgit v1.2.3 From 93cec9a0c402d6ca5d41e10db738bb5f61b19be2 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Mon, 4 May 2015 18:50:27 -0700 Subject: Keep multiple kernel logs Currently we are keeping one copy of the kernel log (LAST_KMSG_FILE). This CL changes to keep up to KEEP_LOG_COUNT copies for kernel logs. Bug: http://b/18092237 Change-Id: Ied862b5b70cbbae7775f59c78c32ec62aeeca655 (cherry picked from commit bef39710ff50cedf6a4de8eb6c7802f66930aab4) --- recovery.cpp | 155 ++++++++++++++++++++++++++++------------------------------- 1 file changed, 73 insertions(+), 82 deletions(-) diff --git a/recovery.cpp b/recovery.cpp index 65172dfcc..2c78bafac 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -31,6 +31,9 @@ #include #include +#include +#include + #include "bootloader.h" #include "common.h" #include "cutils/properties.h" @@ -65,8 +68,6 @@ static const struct option OPTIONS[] = { { NULL, 0, NULL, 0 }, }; -#define LAST_LOG_FILE "/cache/recovery/last_log" - static const char *CACHE_LOG_DIR = "/cache/recovery"; static const char *COMMAND_FILE = "/cache/recovery/command"; static const char *INTENT_FILE = "/cache/recovery/intent"; @@ -78,9 +79,8 @@ static const char *SDCARD_ROOT = "/sdcard"; static const char *TEMPORARY_LOG_FILE = "/tmp/recovery.log"; static const char *TEMPORARY_INSTALL_FILE = "/tmp/last_install"; static const char *LAST_KMSG_FILE = "/cache/recovery/last_kmsg"; -#define KLOG_DEFAULT_LEN (64 * 1024) - -#define KEEP_LOG_COUNT 10 +static const char *LAST_LOG_FILE = "/cache/recovery/last_log"; +static const int KEEP_LOG_COUNT = 10; RecoveryUI* ui = NULL; char* locale = NULL; @@ -267,72 +267,55 @@ set_sdcard_update_bootloader_message() { set_bootloader_message(&boot); } -// read from kernel log into buffer and write out to file -static void -save_kernel_log(const char *destination) { - int n; - char *buffer; - int klog_buf_len; - FILE *log; - - klog_buf_len = klogctl(KLOG_SIZE_BUFFER, 0, 0); +// Read from kernel log into buffer and write out to file. +static void save_kernel_log(const char* destination) { + int klog_buf_len = klogctl(KLOG_SIZE_BUFFER, 0, 0); if (klog_buf_len <= 0) { - LOGE("Error getting klog size (%s), using default\n", strerror(errno)); - klog_buf_len = KLOG_DEFAULT_LEN; - } - - buffer = (char *)malloc(klog_buf_len); - if (!buffer) { - LOGE("Can't alloc %d bytes for klog buffer\n", klog_buf_len); - return; - } - - n = klogctl(KLOG_READ_ALL, buffer, klog_buf_len); - if (n < 0) { - LOGE("Error in reading klog (%s)\n", strerror(errno)); - free(buffer); + LOGE("Error getting klog size: %s\n", strerror(errno)); return; } - log = fopen_path(destination, "w"); - if (log == NULL) { - LOGE("Can't open %s\n", destination); - free(buffer); + std::string buffer(klog_buf_len, 0); + int n = klogctl(KLOG_READ_ALL, &buffer[0], klog_buf_len); + if (n == -1) { + LOGE("Error in reading klog: %s\n", strerror(errno)); return; } - fwrite(buffer, n, 1, log); - check_and_fclose(log, destination); - free(buffer); + buffer.resize(n); + android::base::WriteStringToFile(buffer, destination); } // How much of the temp log we have copied to the copy in cache. static long tmplog_offset = 0; -static void -copy_log_file(const char* source, const char* destination, int append) { - FILE *log = fopen_path(destination, append ? "a" : "w"); - if (log == NULL) { +static void copy_log_file(const char* source, const char* destination, bool append) { + FILE* dest_fp = fopen_path(destination, append ? "a" : "w"); + if (dest_fp == nullptr) { LOGE("Can't open %s\n", destination); } else { - FILE *tmplog = fopen(source, "r"); - if (tmplog != NULL) { + FILE* source_fp = fopen(source, "r"); + if (source_fp != nullptr) { if (append) { - fseek(tmplog, tmplog_offset, SEEK_SET); // Since last write + fseek(source_fp, tmplog_offset, SEEK_SET); // Since last write } char buf[4096]; - while (fgets(buf, sizeof(buf), tmplog)) fputs(buf, log); + size_t bytes; + while ((bytes = fread(buf, 1, sizeof(buf), source_fp)) != 0) { + fwrite(buf, 1, bytes, dest_fp); + } if (append) { - tmplog_offset = ftell(tmplog); + tmplog_offset = ftell(source_fp); } - check_and_fclose(tmplog, source); + check_and_fclose(source_fp, source); } - check_and_fclose(log, destination); + check_and_fclose(dest_fp, destination); } } -// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max -// Overwrites any existing last_log.$max. -static void rotate_last_logs(int max) { +// Rename last_log -> last_log.1 -> last_log.2 -> ... -> last_log.$max. +// Similarly rename last_kmsg -> last_kmsg.1 -> ... -> last_kmsg.$max. +// Overwrite any existing last_log.$max and last_kmsg.$max. +static void rotate_logs(int max) { // Logs should only be rotated once. static bool rotated = false; if (rotated) { @@ -340,14 +323,19 @@ static void rotate_last_logs(int max) { } rotated = true; ensure_path_mounted(LAST_LOG_FILE); + ensure_path_mounted(LAST_KMSG_FILE); - char oldfn[256]; - char newfn[256]; for (int i = max-1; i >= 0; --i) { - snprintf(oldfn, sizeof(oldfn), (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i); - snprintf(newfn, sizeof(newfn), LAST_LOG_FILE ".%d", i+1); - // ignore errors - rename(oldfn, newfn); + std::string old_log = android::base::StringPrintf((i == 0) ? "%s" : "%s.%d", + LAST_LOG_FILE, i); + std::string new_log = android::base::StringPrintf("%s.%d", LAST_LOG_FILE, i+1); + // Ignore errors if old_log doesn't exist. + rename(old_log.c_str(), new_log.c_str()); + + std::string old_kmsg = android::base::StringPrintf((i == 0) ? "%s" : "%s.%d", + LAST_KMSG_FILE, i); + std::string new_kmsg = android::base::StringPrintf("%s.%d", LAST_KMSG_FILE, i+1); + rename(old_kmsg.c_str(), new_kmsg.c_str()); } } @@ -360,7 +348,7 @@ static void copy_logs() { return; } - rotate_last_logs(KEEP_LOG_COUNT); + rotate_logs(KEEP_LOG_COUNT); // Copy logs to cache so the system can find out what happened. copy_log_file(TEMPORARY_LOG_FILE, LOG_FILE, true); @@ -439,9 +427,10 @@ erase_volume(const char *volume) { saved_log_file* head = NULL; if (is_cache) { - // If we're reformatting /cache, we load any - // "/cache/recovery/last*" files into memory, so we can restore - // them after the reformat. + // If we're reformatting /cache, we load any past logs + // (i.e. "/cache/recovery/last_*") and the current log + // ("/cache/recovery/log") into memory, so we can restore them after + // the reformat. ensure_path_mounted(volume); @@ -454,7 +443,7 @@ erase_volume(const char *volume) { strcat(path, "/"); int path_len = strlen(path); while ((de = readdir(d)) != NULL) { - if (strncmp(de->d_name, "last", 4) == 0) { + if (strncmp(de->d_name, "last_", 5) == 0 || strcmp(de->d_name, "log") == 0) { saved_log_file* p = (saved_log_file*) malloc(sizeof(saved_log_file)); strcpy(path+path_len, de->d_name); p->name = strdup(path); @@ -712,38 +701,40 @@ static bool wipe_cache(bool should_confirm, Device* device) { } static void choose_recovery_file(Device* device) { - unsigned int i; - unsigned int n; - static const char** title_headers = NULL; - char *filename; - // "Go back" + LAST_KMSG_FILE + KEEP_LOG_COUNT + terminating NULL entry - char* entries[KEEP_LOG_COUNT + 3]; + // "Go back" + KEEP_LOG_COUNT * 2 + terminating nullptr entry + char* entries[KEEP_LOG_COUNT * 2 + 2]; memset(entries, 0, sizeof(entries)); - n = 0; + unsigned int n = 0; entries[n++] = strdup("Go back"); - // Add kernel kmsg file if available - if ((ensure_path_mounted(LAST_KMSG_FILE) == 0) && (access(LAST_KMSG_FILE, R_OK) == 0)) { - entries[n++] = strdup(LAST_KMSG_FILE); - } - // Add LAST_LOG_FILE + LAST_LOG_FILE.x - for (i = 0; i < KEEP_LOG_COUNT; i++) { - char *filename; - if (asprintf(&filename, (i==0) ? LAST_LOG_FILE : (LAST_LOG_FILE ".%d"), i) == -1) { + // Add LAST_KMSG_FILE + LAST_KMSG_FILE.x + for (int i = 0; i < KEEP_LOG_COUNT; i++) { + char* log_file; + if (asprintf(&log_file, (i == 0) ? "%s" : "%s.%d", LAST_LOG_FILE, i) == -1) { // memory allocation failure - return early. Should never happen. return; } - if ((ensure_path_mounted(filename) != 0) || (access(filename, R_OK) == -1)) { - free(filename); - entries[n++] = NULL; - break; + if ((ensure_path_mounted(log_file) != 0) || (access(log_file, R_OK) == -1)) { + free(log_file); + } else { + entries[n++] = log_file; + } + + char* kmsg_file; + if (asprintf(&kmsg_file, (i == 0) ? "%s" : "%s.%d", LAST_KMSG_FILE, i) == -1) { + // memory allocation failure - return early. Should never happen. + return; + } + if ((ensure_path_mounted(kmsg_file) != 0) || (access(kmsg_file, R_OK) == -1)) { + free(kmsg_file); + } else { + entries[n++] = kmsg_file; } - entries[n++] = filename; } - const char* headers[] = { "Select file to view", NULL }; + const char* headers[] = { "Select file to view", nullptr }; while (true) { int chosen_item = get_menu_selection(headers, entries, 1, 0, device); @@ -755,7 +746,7 @@ static void choose_recovery_file(Device* device) { redirect_stdio(TEMPORARY_LOG_FILE); } - for (i = 0; i < (sizeof(entries) / sizeof(*entries)); i++) { + for (size_t i = 0; i < (sizeof(entries) / sizeof(*entries)); i++) { free(entries[i]); } } -- cgit v1.2.3 From 8853cb2f296adfdc57871e9482a8e97b0011b323 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Mon, 4 May 2015 10:10:13 -0700 Subject: uncrypt: package on non-data partition should follow the right path Fix the accidental change of behavior in [1]. OTA packages not on /data partition should still go through the path that has validity checks and wipe_misc() steps. [1]: commit eaf33654c1817bd665831a13c5bd0c04daabee02. Change-Id: I3e86e19f06603bfe6ecc691c9aa66a8a8a79c5fb (cherry picked from commit fb4ccef1df4f0bd8fa830c750f2970dd2df9e51b) --- uncrypt/uncrypt.c | 61 +++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/uncrypt/uncrypt.c b/uncrypt/uncrypt.c index da035dfba..42ae649cc 100644 --- a/uncrypt/uncrypt.c +++ b/uncrypt/uncrypt.c @@ -159,12 +159,13 @@ const char* find_block_device(const char* path, int* encryptable, int* encrypted return NULL; } -char* parse_recovery_command_file() +// Parse the command file RECOVERY_COMMAND_FILE to find the update package +// name. If it's on the /data partition, replace the package name with the +// block map file name and store it temporarily in RECOVERY_COMMAND_FILE_TMP. +// It will be renamed to RECOVERY_COMMAND_FILE if uncrypt finishes +// successfully. +static char* find_update_package() { - char* fn = NULL; - int count = 0; - char temp[1024]; - FILE* f = fopen(RECOVERY_COMMAND_FILE, "r"); if (f == NULL) { return NULL; @@ -175,17 +176,27 @@ char* parse_recovery_command_file() return NULL; } FILE* fo = fdopen(fd, "w"); - - while (fgets(temp, sizeof(temp), f)) { - printf("read: %s", temp); - if (strncmp(temp, "--update_package=/data/", strlen("--update_package=/data/")) == 0) { - fn = strdup(temp + strlen("--update_package=")); - strcpy(temp, "--update_package=@" CACHE_BLOCK_MAP "\n"); + char* fn = NULL; + char* line = NULL; + size_t len = 0; + while (getline(&line, &len, f) != -1) { + if (strncmp(line, "--update_package=", strlen("--update_package=")) == 0) { + fn = strdup(line + strlen("--update_package=")); + // Replace the package name with block map file if it's on /data partition. + if (strncmp(fn, "/data/", strlen("/data/")) == 0) { + fputs("--update_package=@" CACHE_BLOCK_MAP "\n", fo); + continue; + } } - fputs(temp, fo); + fputs(line, fo); } + free(line); fclose(f); - fsync(fd); + if (fsync(fd) == -1) { + ALOGE("failed to fsync \"%s\": %s\n", RECOVERY_COMMAND_FILE_TMP, strerror(errno)); + fclose(fo); + return NULL; + } fclose(fo); if (fn) { @@ -244,7 +255,6 @@ int produce_block_map(const char* path, const char* map_file, const char* blk_de ALOGE("failed to open fd for reading: %s\n", strerror(errno)); return -1; } - fsync(fd); int wfd = -1; if (encrypted) { @@ -319,11 +329,17 @@ int produce_block_map(const char* path, const char* map_file, const char* blk_de fprintf(mapf, "%d %d\n", ranges[i*2], ranges[i*2+1]); } - fsync(mapfd); + if (fsync(mapfd) == -1) { + ALOGE("failed to fsync \"%s\": %s\n", map_file, strerror(errno)); + return -1; + } fclose(mapf); close(fd); if (encrypted) { - fsync(wfd); + if (fsync(wfd) == -1) { + ALOGE("failed to fsync \"%s\": %s\n", blk_dev, strerror(errno)); + return -1; + } close(wfd); } @@ -352,7 +368,11 @@ void wipe_misc() { written += w; } } - fsync(fd); + if (fsync(fd) == -1) { + ALOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno)); + close(fd); + return; + } close(fd); } } @@ -383,7 +403,7 @@ int main(int argc, char** argv) map_file = argv[2]; do_reboot = 0; } else { - input_path = parse_recovery_command_file(); + input_path = find_update_package(); if (input_path == NULL) { // if we're rebooting to recovery without a package (say, // to wipe data), then we don't need to do anything before @@ -432,15 +452,16 @@ int main(int argc, char** argv) if (strncmp(path, "/data/", 6) != 0) { // path does not start with "/data/"; leave it alone. unlink(RECOVERY_COMMAND_FILE_TMP); + wipe_misc(); } else { ALOGI("writing block map %s", map_file); if (produce_block_map(path, map_file, blk_dev, encrypted) != 0) { return 1; } + wipe_misc(); + rename(RECOVERY_COMMAND_FILE_TMP, RECOVERY_COMMAND_FILE); } - wipe_misc(); - rename(RECOVERY_COMMAND_FILE_TMP, RECOVERY_COMMAND_FILE); if (do_reboot) reboot_to_recovery(); return 0; } -- cgit v1.2.3 From 3e8d28b547cd75af3f77f38a7e2895d0dbd2e232 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Tue, 5 May 2015 18:36:45 -0700 Subject: uncrypt: Switch to C++ Also apply some trivial changes like int -> bool and clean-ups. Change-Id: I5c6c42d34965305c394f4f2de78487bd1174992a (cherry picked from commit 381f455cac0905b023dde79625b06c27b6165dd0) --- uncrypt/Android.mk | 2 +- uncrypt/uncrypt.c | 467 ---------------------------------------------------- uncrypt/uncrypt.cpp | 465 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 466 insertions(+), 468 deletions(-) delete mode 100644 uncrypt/uncrypt.c create mode 100644 uncrypt/uncrypt.cpp diff --git a/uncrypt/Android.mk b/uncrypt/Android.mk index 878d2757e..d832d9724 100644 --- a/uncrypt/Android.mk +++ b/uncrypt/Android.mk @@ -16,7 +16,7 @@ LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) -LOCAL_SRC_FILES := uncrypt.c +LOCAL_SRC_FILES := uncrypt.cpp LOCAL_MODULE := uncrypt diff --git a/uncrypt/uncrypt.c b/uncrypt/uncrypt.c deleted file mode 100644 index 42ae649cc..000000000 --- a/uncrypt/uncrypt.c +++ /dev/null @@ -1,467 +0,0 @@ -/* - * 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. - */ - -// This program takes a file on an ext4 filesystem and produces a list -// of the blocks that file occupies, which enables the file contents -// to be read directly from the block device without mounting the -// filesystem. -// -// If the filesystem is using an encrypted block device, it will also -// read the file and rewrite it to the same blocks of the underlying -// (unencrypted) block device, so the file contents can be read -// without the need for the decryption key. -// -// The output of this program is a "block map" which looks like this: -// -// /dev/block/platform/msm_sdcc.1/by-name/userdata # block device -// 49652 4096 # file size in bytes, block size -// 3 # count of block ranges -// 1000 1008 # block range 0 -// 2100 2102 # ... block range 1 -// 30 33 # ... block range 2 -// -// Each block range represents a half-open interval; the line "30 33" -// reprents the blocks [30, 31, 32]. -// -// Recovery can take this block map file and retrieve the underlying -// file data to use as an update package. - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define LOG_TAG "uncrypt" -#include -#include -#include - -#define WINDOW_SIZE 5 -#define RECOVERY_COMMAND_FILE "/cache/recovery/command" -#define RECOVERY_COMMAND_FILE_TMP "/cache/recovery/command.tmp" -#define CACHE_BLOCK_MAP "/cache/recovery/block.map" - -static struct fstab* fstab = NULL; - -static int write_at_offset(unsigned char* buffer, size_t size, - int wfd, off64_t offset) -{ - if (TEMP_FAILURE_RETRY(lseek64(wfd, offset, SEEK_SET)) == -1) { - ALOGE("error seeking to offset %lld: %s\n", offset, strerror(errno)); - return -1; - } - size_t written = 0; - while (written < size) { - ssize_t wrote = TEMP_FAILURE_RETRY(write(wfd, buffer + written, size - written)); - if (wrote == -1) { - ALOGE("error writing offset %lld: %s\n", (offset + written), strerror(errno)); - return -1; - } - written += wrote; - } - return 0; -} - -void add_block_to_ranges(int** ranges, int* range_alloc, int* range_used, int new_block) -{ - // If the current block start is < 0, set the start to the new - // block. (This only happens for the very first block of the very - // first range.) - if ((*ranges)[*range_used*2-2] < 0) { - (*ranges)[*range_used*2-2] = new_block; - (*ranges)[*range_used*2-1] = new_block; - } - - if (new_block == (*ranges)[*range_used*2-1]) { - // If the new block comes immediately after the current range, - // all we have to do is extend the current range. - ++(*ranges)[*range_used*2-1]; - } else { - // We need to start a new range. - - // If there isn't enough room in the array, we need to expand it. - if (*range_used >= *range_alloc) { - *range_alloc *= 2; - *ranges = realloc(*ranges, *range_alloc * 2 * sizeof(int)); - } - - ++*range_used; - (*ranges)[*range_used*2-2] = new_block; - (*ranges)[*range_used*2-1] = new_block+1; - } -} - -static struct fstab* read_fstab() -{ - fstab = NULL; - - // The fstab path is always "/fstab.${ro.hardware}". - char fstab_path[PATH_MAX+1] = "/fstab."; - if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) { - ALOGE("failed to get ro.hardware\n"); - return NULL; - } - - fstab = fs_mgr_read_fstab(fstab_path); - if (!fstab) { - ALOGE("failed to read %s\n", fstab_path); - return NULL; - } - - return fstab; -} - -const char* find_block_device(const char* path, int* encryptable, int* encrypted) -{ - // Look for a volume whose mount point is the prefix of path and - // return its block device. Set encrypted if it's currently - // encrypted. - int i; - for (i = 0; i < fstab->num_entries; ++i) { - struct fstab_rec* v = &fstab->recs[i]; - if (!v->mount_point) continue; - int len = strlen(v->mount_point); - if (strncmp(path, v->mount_point, len) == 0 && - (path[len] == '/' || path[len] == 0)) { - *encrypted = 0; - *encryptable = 0; - if (fs_mgr_is_encryptable(v)) { - *encryptable = 1; - char buffer[PROPERTY_VALUE_MAX+1]; - if (property_get("ro.crypto.state", buffer, "") && - strcmp(buffer, "encrypted") == 0) { - *encrypted = 1; - } - } - return v->blk_device; - } - } - - return NULL; -} - -// Parse the command file RECOVERY_COMMAND_FILE to find the update package -// name. If it's on the /data partition, replace the package name with the -// block map file name and store it temporarily in RECOVERY_COMMAND_FILE_TMP. -// It will be renamed to RECOVERY_COMMAND_FILE if uncrypt finishes -// successfully. -static char* find_update_package() -{ - FILE* f = fopen(RECOVERY_COMMAND_FILE, "r"); - if (f == NULL) { - return NULL; - } - int fd = open(RECOVERY_COMMAND_FILE_TMP, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR); - if (fd < 0) { - ALOGE("failed to open %s\n", RECOVERY_COMMAND_FILE_TMP); - return NULL; - } - FILE* fo = fdopen(fd, "w"); - char* fn = NULL; - char* line = NULL; - size_t len = 0; - while (getline(&line, &len, f) != -1) { - if (strncmp(line, "--update_package=", strlen("--update_package=")) == 0) { - fn = strdup(line + strlen("--update_package=")); - // Replace the package name with block map file if it's on /data partition. - if (strncmp(fn, "/data/", strlen("/data/")) == 0) { - fputs("--update_package=@" CACHE_BLOCK_MAP "\n", fo); - continue; - } - } - fputs(line, fo); - } - free(line); - fclose(f); - if (fsync(fd) == -1) { - ALOGE("failed to fsync \"%s\": %s\n", RECOVERY_COMMAND_FILE_TMP, strerror(errno)); - fclose(fo); - return NULL; - } - fclose(fo); - - if (fn) { - char* newline = strchr(fn, '\n'); - if (newline) *newline = 0; - } - return fn; -} - -int produce_block_map(const char* path, const char* map_file, const char* blk_dev, - int encrypted) -{ - struct stat sb; - int ret; - - int mapfd = open(map_file, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR); - if (mapfd < 0) { - ALOGE("failed to open %s\n", map_file); - return -1; - } - FILE* mapf = fdopen(mapfd, "w"); - - ret = stat(path, &sb); - if (ret != 0) { - ALOGE("failed to stat %s\n", path); - return -1; - } - - ALOGI(" block size: %ld bytes\n", (long)sb.st_blksize); - - int blocks = ((sb.st_size-1) / sb.st_blksize) + 1; - ALOGI(" file size: %lld bytes, %d blocks\n", (long long)sb.st_size, blocks); - - int* ranges; - int range_alloc = 1; - int range_used = 1; - ranges = malloc(range_alloc * 2 * sizeof(int)); - ranges[0] = -1; - ranges[1] = -1; - - 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; - if (encrypted) { - for (i = 0; i < WINDOW_SIZE; ++i) { - buffers[i] = malloc(sb.st_blksize); - } - } - int head_block = 0; - int head = 0, tail = 0; - size_t pos = 0; - - int fd = open(path, O_RDONLY); - if (fd < 0) { - ALOGE("failed to open fd for reading: %s\n", strerror(errno)); - return -1; - } - - int wfd = -1; - if (encrypted) { - wfd = open(blk_dev, O_WRONLY | O_SYNC); - if (wfd < 0) { - ALOGE("failed to open fd for writing: %s\n", strerror(errno)); - return -1; - } - } - - while (pos < sb.st_size) { - if ((tail+1) % WINDOW_SIZE == head) { - // write out head buffer - int block = head_block; - ret = ioctl(fd, FIBMAP, &block); - if (ret != 0) { - ALOGE("failed to find block %d\n", head_block); - return -1; - } - add_block_to_ranges(&ranges, &range_alloc, &range_used, block); - if (encrypted) { - if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) { - return -1; - } - } - head = (head + 1) % WINDOW_SIZE; - ++head_block; - } - - // read next block to tail - if (encrypted) { - size_t so_far = 0; - while (so_far < sb.st_blksize && pos < sb.st_size) { - ssize_t this_read = - TEMP_FAILURE_RETRY(read(fd, buffers[tail] + so_far, sb.st_blksize - so_far)); - if (this_read == -1) { - ALOGE("failed to read: %s\n", strerror(errno)); - return -1; - } - so_far += this_read; - pos += this_read; - } - } else { - // If we're not encrypting; we don't need to actually read - // anything, just skip pos forward as if we'd read a - // block. - pos += sb.st_blksize; - } - tail = (tail+1) % WINDOW_SIZE; - } - - while (head != tail) { - // write out head buffer - int block = head_block; - ret = ioctl(fd, FIBMAP, &block); - if (ret != 0) { - ALOGE("failed to find block %d\n", head_block); - return -1; - } - add_block_to_ranges(&ranges, &range_alloc, &range_used, block); - if (encrypted) { - if (write_at_offset(buffers[head], sb.st_blksize, wfd, (off64_t)sb.st_blksize * block) != 0) { - return -1; - } - } - head = (head + 1) % WINDOW_SIZE; - ++head_block; - } - - fprintf(mapf, "%d\n", range_used); - for (i = 0; i < range_used; ++i) { - fprintf(mapf, "%d %d\n", ranges[i*2], ranges[i*2+1]); - } - - if (fsync(mapfd) == -1) { - ALOGE("failed to fsync \"%s\": %s\n", map_file, strerror(errno)); - return -1; - } - fclose(mapf); - close(fd); - if (encrypted) { - if (fsync(wfd) == -1) { - ALOGE("failed to fsync \"%s\": %s\n", blk_dev, strerror(errno)); - return -1; - } - close(wfd); - } - - return 0; -} - -void wipe_misc() { - ALOGI("removing old commands from misc"); - int i; - for (i = 0; i < fstab->num_entries; ++i) { - struct fstab_rec* v = &fstab->recs[i]; - if (!v->mount_point) continue; - if (strcmp(v->mount_point, "/misc") == 0) { - int fd = open(v->blk_device, O_WRONLY | O_SYNC); - uint8_t zeroes[1088]; // sizeof(bootloader_message) from recovery - memset(zeroes, 0, sizeof(zeroes)); - - size_t written = 0; - size_t size = sizeof(zeroes); - while (written < size) { - ssize_t w = TEMP_FAILURE_RETRY(write(fd, zeroes, size-written)); - if (w == -1) { - ALOGE("zero write failed: %s\n", strerror(errno)); - return; - } else { - written += w; - } - } - if (fsync(fd) == -1) { - ALOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno)); - close(fd); - return; - } - close(fd); - } - } -} - -void reboot_to_recovery() { - ALOGI("rebooting to recovery"); - property_set("sys.powerctl", "reboot,recovery"); - sleep(10); - ALOGE("reboot didn't succeed?"); -} - -int main(int argc, char** argv) -{ - const char* input_path; - const char* map_file; - int do_reboot = 1; - - if (argc != 1 && argc != 3) { - fprintf(stderr, "usage: %s [ ]\n", argv[0]); - return 2; - } - - if (argc == 3) { - // when command-line args are given this binary is being used - // for debugging; don't reboot to recovery at the end. - input_path = argv[1]; - map_file = argv[2]; - do_reboot = 0; - } else { - input_path = find_update_package(); - if (input_path == NULL) { - // if we're rebooting to recovery without a package (say, - // to wipe data), then we don't need to do anything before - // going to recovery. - ALOGI("no recovery command file or no update package arg"); - reboot_to_recovery(); - return 1; - } - map_file = CACHE_BLOCK_MAP; - } - - ALOGI("update package is %s", input_path); - - // Turn the name of the file we're supposed to convert into an - // absolute path, so we can find what filesystem it's on. - char path[PATH_MAX+1]; - if (realpath(input_path, path) == NULL) { - ALOGE("failed to convert %s to absolute path: %s", input_path, strerror(errno)); - return 1; - } - - int encryptable; - int encrypted; - if (read_fstab() == NULL) { - return 1; - } - const char* blk_dev = find_block_device(path, &encryptable, &encrypted); - if (blk_dev == NULL) { - ALOGE("failed to find block device for %s", path); - return 1; - } - - // If the filesystem it's on isn't encrypted, we only produce the - // block map, we don't rewrite the file contents (it would be - // pointless to do so). - ALOGI("encryptable: %s\n", encryptable ? "yes" : "no"); - ALOGI(" encrypted: %s\n", encrypted ? "yes" : "no"); - - // Recovery supports installing packages from 3 paths: /cache, - // /data, and /sdcard. (On a particular device, other locations - // may work, but those are three we actually expect.) - // - // On /data we want to convert the file to a block map so that we - // can read the package without mounting the partition. On /cache - // and /sdcard we leave the file alone. - if (strncmp(path, "/data/", 6) != 0) { - // path does not start with "/data/"; leave it alone. - unlink(RECOVERY_COMMAND_FILE_TMP); - wipe_misc(); - } else { - ALOGI("writing block map %s", map_file); - if (produce_block_map(path, map_file, blk_dev, encrypted) != 0) { - return 1; - } - wipe_misc(); - rename(RECOVERY_COMMAND_FILE_TMP, RECOVERY_COMMAND_FILE); - } - - if (do_reboot) reboot_to_recovery(); - return 0; -} diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp new file mode 100644 index 000000000..11766f14c --- /dev/null +++ b/uncrypt/uncrypt.cpp @@ -0,0 +1,465 @@ +/* + * 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. + */ + +// This program takes a file on an ext4 filesystem and produces a list +// of the blocks that file occupies, which enables the file contents +// to be read directly from the block device without mounting the +// filesystem. +// +// If the filesystem is using an encrypted block device, it will also +// read the file and rewrite it to the same blocks of the underlying +// (unencrypted) block device, so the file contents can be read +// without the need for the decryption key. +// +// The output of this program is a "block map" which looks like this: +// +// /dev/block/platform/msm_sdcc.1/by-name/userdata # block device +// 49652 4096 # file size in bytes, block size +// 3 # count of block ranges +// 1000 1008 # block range 0 +// 2100 2102 # ... block range 1 +// 30 33 # ... block range 2 +// +// Each block range represents a half-open interval; the line "30 33" +// reprents the blocks [30, 31, 32]. +// +// Recovery can take this block map file and retrieve the underlying +// file data to use as an update package. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define LOG_TAG "uncrypt" +#include +#include +#include + +#define WINDOW_SIZE 5 +#define RECOVERY_COMMAND_FILE "/cache/recovery/command" +#define RECOVERY_COMMAND_FILE_TMP "/cache/recovery/command.tmp" +#define CACHE_BLOCK_MAP "/cache/recovery/block.map" + +static struct fstab* fstab = NULL; + +static int write_at_offset(unsigned char* buffer, size_t size, int wfd, off64_t offset) { + if (TEMP_FAILURE_RETRY(lseek64(wfd, offset, SEEK_SET)) == -1) { + ALOGE("error seeking to offset %lld: %s\n", offset, strerror(errno)); + return -1; + } + size_t written = 0; + while (written < size) { + ssize_t wrote = TEMP_FAILURE_RETRY(write(wfd, buffer + written, size - written)); + if (wrote == -1) { + ALOGE("error writing offset %lld: %s\n", (offset + written), strerror(errno)); + return -1; + } + written += wrote; + } + return 0; +} + +static void add_block_to_ranges(int** ranges, int* range_alloc, int* range_used, int new_block) { + // If the current block start is < 0, set the start to the new + // block. (This only happens for the very first block of the very + // first range.) + if ((*ranges)[*range_used*2-2] < 0) { + (*ranges)[*range_used*2-2] = new_block; + (*ranges)[*range_used*2-1] = new_block; + } + + if (new_block == (*ranges)[*range_used*2-1]) { + // If the new block comes immediately after the current range, + // all we have to do is extend the current range. + ++(*ranges)[*range_used*2-1]; + } else { + // We need to start a new range. + + // If there isn't enough room in the array, we need to expand it. + if (*range_used >= *range_alloc) { + *range_alloc *= 2; + *ranges = reinterpret_cast(realloc(*ranges, *range_alloc * 2 * sizeof(int))); + } + + ++*range_used; + (*ranges)[*range_used*2-2] = new_block; + (*ranges)[*range_used*2-1] = new_block+1; + } +} + +static struct fstab* read_fstab() { + fstab = NULL; + + // The fstab path is always "/fstab.${ro.hardware}". + char fstab_path[PATH_MAX+1] = "/fstab."; + if (!property_get("ro.hardware", fstab_path+strlen(fstab_path), "")) { + ALOGE("failed to get ro.hardware\n"); + return NULL; + } + + fstab = fs_mgr_read_fstab(fstab_path); + if (!fstab) { + ALOGE("failed to read %s\n", fstab_path); + return NULL; + } + + return fstab; +} + +static const char* find_block_device(const char* path, bool* encryptable, bool* encrypted) { + // Look for a volume whose mount point is the prefix of path and + // return its block device. Set encrypted if it's currently + // encrypted. + for (int i = 0; i < fstab->num_entries; ++i) { + struct fstab_rec* v = &fstab->recs[i]; + if (!v->mount_point) { + continue; + } + int len = strlen(v->mount_point); + if (strncmp(path, v->mount_point, len) == 0 && + (path[len] == '/' || path[len] == 0)) { + *encrypted = false; + *encryptable = false; + if (fs_mgr_is_encryptable(v)) { + *encryptable = true; + char buffer[PROPERTY_VALUE_MAX+1]; + if (property_get("ro.crypto.state", buffer, "") && + strcmp(buffer, "encrypted") == 0) { + *encrypted = true; + } + } + return v->blk_device; + } + } + + return NULL; +} + +// Parse the command file RECOVERY_COMMAND_FILE to find the update package +// name. If it's on the /data partition, replace the package name with the +// block map file name and store it temporarily in RECOVERY_COMMAND_FILE_TMP. +// It will be renamed to RECOVERY_COMMAND_FILE if uncrypt finishes +// successfully. +static char* find_update_package() +{ + FILE* f = fopen(RECOVERY_COMMAND_FILE, "r"); + if (f == NULL) { + return NULL; + } + int fd = open(RECOVERY_COMMAND_FILE_TMP, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR); + if (fd < 0) { + ALOGE("failed to open %s\n", RECOVERY_COMMAND_FILE_TMP); + return NULL; + } + FILE* fo = fdopen(fd, "w"); + char* fn = NULL; + char* line = NULL; + size_t len = 0; + while (getline(&line, &len, f) != -1) { + if (strncmp(line, "--update_package=", strlen("--update_package=")) == 0) { + fn = strdup(line + strlen("--update_package=")); + // Replace the package name with block map file if it's on /data partition. + if (strncmp(fn, "/data/", strlen("/data/")) == 0) { + fputs("--update_package=@" CACHE_BLOCK_MAP "\n", fo); + continue; + } + } + fputs(line, fo); + } + free(line); + fclose(f); + if (fsync(fd) == -1) { + ALOGE("failed to fsync \"%s\": %s\n", RECOVERY_COMMAND_FILE_TMP, strerror(errno)); + fclose(fo); + return NULL; + } + fclose(fo); + + if (fn) { + char* newline = strchr(fn, '\n'); + if (newline) { + *newline = 0; + } + } + return fn; +} + +static int produce_block_map(const char* path, const char* map_file, const char* blk_dev, + bool encrypted) { + + int mapfd = open(map_file, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR); + if (mapfd < 0) { + ALOGE("failed to open %s\n", map_file); + return -1; + } + FILE* mapf = fdopen(mapfd, "w"); + + struct stat sb; + int ret = stat(path, &sb); + if (ret != 0) { + ALOGE("failed to stat %s\n", path); + return -1; + } + + ALOGI(" block size: %ld bytes\n", (long)sb.st_blksize); + + int blocks = ((sb.st_size-1) / sb.st_blksize) + 1; + ALOGI(" file size: %lld bytes, %d blocks\n", (long long)sb.st_size, blocks); + + int range_alloc = 1; + int range_used = 1; + int* ranges = reinterpret_cast(malloc(range_alloc * 2 * sizeof(int))); + ranges[0] = -1; + ranges[1] = -1; + + fprintf(mapf, "%s\n%lld %lu\n", blk_dev, (long long)sb.st_size, (unsigned long)sb.st_blksize); + + unsigned char* buffers[WINDOW_SIZE]; + if (encrypted) { + for (size_t i = 0; i < WINDOW_SIZE; ++i) { + buffers[i] = reinterpret_cast(malloc(sb.st_blksize)); + } + } + int head_block = 0; + int head = 0, tail = 0; + size_t pos = 0; + + int fd = open(path, O_RDONLY); + if (fd < 0) { + ALOGE("failed to open fd for reading: %s\n", strerror(errno)); + return -1; + } + + int wfd = -1; + if (encrypted) { + wfd = open(blk_dev, O_WRONLY | O_SYNC); + if (wfd < 0) { + ALOGE("failed to open fd for writing: %s\n", strerror(errno)); + return -1; + } + } + + while (pos < sb.st_size) { + if ((tail+1) % WINDOW_SIZE == head) { + // write out head buffer + int block = head_block; + ret = ioctl(fd, FIBMAP, &block); + if (ret != 0) { + ALOGE("failed to find block %d\n", head_block); + return -1; + } + add_block_to_ranges(&ranges, &range_alloc, &range_used, block); + if (encrypted) { + if (write_at_offset(buffers[head], sb.st_blksize, wfd, + (off64_t)sb.st_blksize * block) != 0) { + return -1; + } + } + head = (head + 1) % WINDOW_SIZE; + ++head_block; + } + + // read next block to tail + if (encrypted) { + size_t so_far = 0; + while (so_far < sb.st_blksize && pos < sb.st_size) { + ssize_t this_read = + TEMP_FAILURE_RETRY(read(fd, buffers[tail] + so_far, sb.st_blksize - so_far)); + if (this_read == -1) { + ALOGE("failed to read: %s\n", strerror(errno)); + return -1; + } + so_far += this_read; + pos += this_read; + } + } else { + // If we're not encrypting; we don't need to actually read + // anything, just skip pos forward as if we'd read a + // block. + pos += sb.st_blksize; + } + tail = (tail+1) % WINDOW_SIZE; + } + + while (head != tail) { + // write out head buffer + int block = head_block; + ret = ioctl(fd, FIBMAP, &block); + if (ret != 0) { + ALOGE("failed to find block %d\n", head_block); + return -1; + } + add_block_to_ranges(&ranges, &range_alloc, &range_used, block); + if (encrypted) { + if (write_at_offset(buffers[head], sb.st_blksize, wfd, + (off64_t)sb.st_blksize * block) != 0) { + return -1; + } + } + head = (head + 1) % WINDOW_SIZE; + ++head_block; + } + + fprintf(mapf, "%d\n", range_used); + for (int i = 0; i < range_used; ++i) { + fprintf(mapf, "%d %d\n", ranges[i*2], ranges[i*2+1]); + } + + if (fsync(mapfd) == -1) { + ALOGE("failed to fsync \"%s\": %s\n", map_file, strerror(errno)); + return -1; + } + fclose(mapf); + close(fd); + if (encrypted) { + if (fsync(wfd) == -1) { + ALOGE("failed to fsync \"%s\": %s\n", blk_dev, strerror(errno)); + return -1; + } + close(wfd); + } + + return 0; +} + +static void wipe_misc() { + ALOGI("removing old commands from misc"); + for (int i = 0; i < fstab->num_entries; ++i) { + struct fstab_rec* v = &fstab->recs[i]; + if (!v->mount_point) continue; + if (strcmp(v->mount_point, "/misc") == 0) { + int fd = open(v->blk_device, O_WRONLY | O_SYNC); + uint8_t zeroes[1088]; // sizeof(bootloader_message) from recovery + memset(zeroes, 0, sizeof(zeroes)); + + size_t written = 0; + size_t size = sizeof(zeroes); + while (written < size) { + ssize_t w = TEMP_FAILURE_RETRY(write(fd, zeroes, size-written)); + if (w == -1) { + ALOGE("zero write failed: %s\n", strerror(errno)); + return; + } else { + written += w; + } + } + if (fsync(fd) == -1) { + ALOGE("failed to fsync \"%s\": %s\n", v->blk_device, strerror(errno)); + close(fd); + return; + } + close(fd); + } + } +} + +static void reboot_to_recovery() { + ALOGI("rebooting to recovery"); + property_set("sys.powerctl", "reboot,recovery"); + sleep(10); + ALOGE("reboot didn't succeed?"); +} + +int main(int argc, char** argv) +{ + const char* input_path; + const char* map_file; + bool do_reboot = true; + + if (argc != 1 && argc != 3) { + fprintf(stderr, "usage: %s [ ]\n", argv[0]); + return 2; + } + + if (argc == 3) { + // when command-line args are given this binary is being used + // for debugging; don't reboot to recovery at the end. + input_path = argv[1]; + map_file = argv[2]; + do_reboot = false; + } else { + input_path = find_update_package(); + if (input_path == NULL) { + // if we're rebooting to recovery without a package (say, + // to wipe data), then we don't need to do anything before + // going to recovery. + ALOGI("no recovery command file or no update package arg"); + reboot_to_recovery(); + return 1; + } + map_file = CACHE_BLOCK_MAP; + } + + ALOGI("update package is %s", input_path); + + // Turn the name of the file we're supposed to convert into an + // absolute path, so we can find what filesystem it's on. + char path[PATH_MAX+1]; + if (realpath(input_path, path) == NULL) { + ALOGE("failed to convert %s to absolute path: %s", input_path, strerror(errno)); + return 1; + } + + if (read_fstab() == NULL) { + return 1; + } + + bool encryptable; + bool encrypted; + const char* blk_dev = find_block_device(path, &encryptable, &encrypted); + if (blk_dev == NULL) { + ALOGE("failed to find block device for %s", path); + return 1; + } + + // If the filesystem it's on isn't encrypted, we only produce the + // block map, we don't rewrite the file contents (it would be + // pointless to do so). + ALOGI("encryptable: %s\n", encryptable ? "yes" : "no"); + ALOGI(" encrypted: %s\n", encrypted ? "yes" : "no"); + + // Recovery supports installing packages from 3 paths: /cache, + // /data, and /sdcard. (On a particular device, other locations + // may work, but those are three we actually expect.) + // + // On /data we want to convert the file to a block map so that we + // can read the package without mounting the partition. On /cache + // and /sdcard we leave the file alone. + if (strncmp(path, "/data/", 6) != 0) { + // path does not start with "/data/"; leave it alone. + unlink(RECOVERY_COMMAND_FILE_TMP); + wipe_misc(); + } else { + ALOGI("writing block map %s", map_file); + if (produce_block_map(path, map_file, blk_dev, encrypted) != 0) { + return 1; + } + wipe_misc(); + rename(RECOVERY_COMMAND_FILE_TMP, RECOVERY_COMMAND_FILE); + } + + if (do_reboot) { + reboot_to_recovery(); + } + return 0; +} -- cgit v1.2.3 From df52e1e119804cc5e7d0b7f77a7d1bf42b2da9dc Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Wed, 6 May 2015 12:40:05 -0700 Subject: Add an alternate screen for viewing recovery logs. This makes it easier to go back and forth without losing current output. Also make the display more like regular more(1). Bug: http://b/20834540 Change-Id: Icc5703e9c8a378cc7072d8ebb79e34451267ee1b (cherry picked from commit c049163234003ef463bca018920622bc8269c69b) --- recovery.cpp | 9 ++--- screen_ui.cpp | 110 ++++++++++++++++++++++++++++++++++------------------------ screen_ui.h | 15 +++++--- 3 files changed, 79 insertions(+), 55 deletions(-) diff --git a/recovery.cpp b/recovery.cpp index 2c78bafac..80a8c7ba5 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -701,12 +701,11 @@ static bool wipe_cache(bool should_confirm, Device* device) { } static void choose_recovery_file(Device* device) { - // "Go back" + KEEP_LOG_COUNT * 2 + terminating nullptr entry - char* entries[KEEP_LOG_COUNT * 2 + 2]; + // "Back" + KEEP_LOG_COUNT * 2 + terminating nullptr entry + char* entries[1 + KEEP_LOG_COUNT * 2 + 1]; memset(entries, 0, sizeof(entries)); unsigned int n = 0; - entries[n++] = strdup("Go back"); // Add LAST_LOG_FILE + LAST_LOG_FILE.x // Add LAST_KMSG_FILE + LAST_KMSG_FILE.x @@ -734,11 +733,13 @@ static void choose_recovery_file(Device* device) { } } + entries[n++] = strdup("Back"); + const char* headers[] = { "Select file to view", nullptr }; while (true) { int chosen_item = get_menu_selection(headers, entries, 1, 0, device); - if (chosen_item == 0) break; + if (strcmp(entries[chosen_item], "Back") == 0) break; // TODO: do we need to redirect? ShowFile could just avoid writing to stdio. redirect_stdio("/dev/null"); diff --git a/screen_ui.cpp b/screen_ui.cpp index 5e73d37c4..ff9591514 100644 --- a/screen_ui.cpp +++ b/screen_ui.cpp @@ -58,18 +58,19 @@ ScreenRecoveryUI::ScreenRecoveryUI() : progressScopeSize(0), progress(0), pagesIdentical(false), - text(nullptr), - text_cols(0), - text_rows(0), - text_col(0), - text_row(0), - text_top(0), + text_cols_(0), + text_rows_(0), + text_(nullptr), + text_col_(0), + text_row_(0), + text_top_(0), show_text(false), show_text_ever(false), - menu(nullptr), + menu_(nullptr), show_menu(false), menu_items(0), menu_sel(0), + file_viewer_text_(nullptr), animation_fps(20), installing_frames(-1), stage(-1), @@ -255,7 +256,7 @@ void ScreenRecoveryUI::draw_screen_locked() { DrawTextLines(&y, HasThreeButtons() ? REGULAR_HELP : LONG_PRESS_HELP); SetColor(HEADER); - DrawTextLines(&y, menu_headers); + DrawTextLines(&y, menu_headers_); SetColor(MENU); DrawHorizontalRule(&y); @@ -267,10 +268,10 @@ void ScreenRecoveryUI::draw_screen_locked() { gr_fill(0, y - 2, gr_fb_width(), y + char_height + 2); // Bold white text for the selected item. SetColor(MENU_SEL_FG); - gr_text(4, y, menu[i], true); + gr_text(4, y, menu_[i], true); SetColor(MENU); } else { - gr_text(4, y, menu[i], false); + gr_text(4, y, menu_[i], false); } y += char_height + 4; } @@ -281,14 +282,14 @@ void ScreenRecoveryUI::draw_screen_locked() { // screen, the bottom of the menu, or we've displayed the // entire text buffer. SetColor(LOG); - int row = (text_top+text_rows-1) % text_rows; + int row = (text_top_ + text_rows_ - 1) % text_rows_; size_t count = 0; for (int ty = gr_fb_height() - char_height; - ty >= y && count < text_rows; + ty >= y && count < text_rows_; ty -= char_height, ++count) { - gr_text(0, ty, text[row], false); + gr_text(0, ty, text_[row], false); --row; - if (row < 0) row = text_rows-1; + if (row < 0) row = text_rows_ - 1; } } } @@ -391,14 +392,15 @@ void ScreenRecoveryUI::Init() { gr_init(); gr_font_size(&char_width, &char_height); - text_rows = gr_fb_height() / char_height; - text_cols = gr_fb_width() / char_width; + text_rows_ = gr_fb_height() / char_height; + text_cols_ = gr_fb_width() / char_width; - text = Alloc2d(text_rows, text_cols + 1); - menu = Alloc2d(text_rows, text_cols + 1); + text_ = Alloc2d(text_rows_, text_cols_ + 1); + file_viewer_text_ = Alloc2d(text_rows_, text_cols_ + 1); + menu_ = Alloc2d(text_rows_, text_cols_ + 1); - text_col = text_row = 0; - text_top = 1; + text_col_ = text_row_ = 0; + text_top_ = 1; backgroundIcon[NONE] = nullptr; LoadBitmapArray("icon_installing", &installing_frames, &installation); @@ -514,17 +516,17 @@ void ScreenRecoveryUI::Print(const char *fmt, ...) { fputs(buf, stdout); pthread_mutex_lock(&updateMutex); - if (text_rows > 0 && text_cols > 0) { + if (text_rows_ > 0 && text_cols_ > 0) { for (const char* ptr = buf; *ptr != '\0'; ++ptr) { - if (*ptr == '\n' || text_col >= text_cols) { - text[text_row][text_col] = '\0'; - text_col = 0; - text_row = (text_row + 1) % text_rows; - if (text_row == text_top) text_top = (text_top + 1) % text_rows; + if (*ptr == '\n' || text_col_ >= text_cols_) { + text_[text_row_][text_col_] = '\0'; + text_col_ = 0; + text_row_ = (text_row_ + 1) % text_rows_; + if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_; } - if (*ptr != '\n') text[text_row][text_col++] = *ptr; + if (*ptr != '\n') text_[text_row_][text_col_++] = *ptr; } - text[text_row][text_col] = '\0'; + text_[text_row_][text_col_] = '\0'; update_screen_locked(); } pthread_mutex_unlock(&updateMutex); @@ -532,21 +534,23 @@ void ScreenRecoveryUI::Print(const char *fmt, ...) { void ScreenRecoveryUI::PutChar(char ch) { pthread_mutex_lock(&updateMutex); - if (ch != '\n') text[text_row][text_col++] = ch; - if (ch == '\n' || text_col >= text_cols) { - text_col = 0; - ++text_row; + if (ch != '\n') text_[text_row_][text_col_++] = ch; + if (ch == '\n' || text_col_ >= text_cols_) { + text_col_ = 0; + ++text_row_; + + if (text_row_ == text_top_) text_top_ = (text_top_ + 1) % text_rows_; } pthread_mutex_unlock(&updateMutex); } void ScreenRecoveryUI::ClearText() { pthread_mutex_lock(&updateMutex); - text_col = 0; - text_row = 0; - text_top = 1; - for (size_t i = 0; i < text_rows; ++i) { - memset(text[i], 0, text_cols + 1); + text_col_ = 0; + text_row_ = 0; + text_top_ = 1; + for (size_t i = 0; i < text_rows_; ++i) { + memset(text_[i], 0, text_cols_ + 1); } pthread_mutex_unlock(&updateMutex); } @@ -590,12 +594,11 @@ void ScreenRecoveryUI::ShowFile(FILE* fp) { int ch = getc(fp); if (ch == EOF) { - text_row = text_top = text_rows - 2; + while (text_row_ < text_rows_ - 1) PutChar('\n'); show_prompt = true; } else { PutChar(ch); - if (text_col == 0 && text_row >= text_rows - 2) { - text_top = text_row; + if (text_col_ == 0 && text_row_ >= text_rows_ - 1) { show_prompt = true; } } @@ -608,19 +611,34 @@ void ScreenRecoveryUI::ShowFile(const char* filename) { Print(" Unable to open %s: %s\n", filename, strerror(errno)); return; } + + char** old_text = text_; + size_t old_text_col = text_col_; + size_t old_text_row = text_row_; + size_t old_text_top = text_top_; + + // Swap in the alternate screen and clear it. + text_ = file_viewer_text_; + ClearText(); + ShowFile(fp); fclose(fp); + + text_ = old_text; + text_col_ = old_text_col; + text_row_ = old_text_row; + text_top_ = old_text_top; } void ScreenRecoveryUI::StartMenu(const char* const * headers, const char* const * items, int initial_selection) { pthread_mutex_lock(&updateMutex); - if (text_rows > 0 && text_cols > 0) { - menu_headers = headers; + if (text_rows_ > 0 && text_cols_ > 0) { + menu_headers_ = headers; size_t i = 0; - for (; i < text_rows && items[i] != nullptr; ++i) { - strncpy(menu[i], items[i], text_cols-1); - menu[i][text_cols-1] = '\0'; + for (; i < text_rows_ && items[i] != nullptr; ++i) { + strncpy(menu_[i], items[i], text_cols_ - 1); + menu_[i][text_cols_ - 1] = '\0'; } menu_items = i; show_menu = true; @@ -649,7 +667,7 @@ int ScreenRecoveryUI::SelectMenu(int sel) { void ScreenRecoveryUI::EndMenu() { pthread_mutex_lock(&updateMutex); - if (show_menu && text_rows > 0 && text_cols > 0) { + if (show_menu && text_rows_ > 0 && text_cols_ > 0) { show_menu = false; update_screen_locked(); } diff --git a/screen_ui.h b/screen_ui.h index 46165d90c..ea05bf15f 100644 --- a/screen_ui.h +++ b/screen_ui.h @@ -89,18 +89,23 @@ class ScreenRecoveryUI : public RecoveryUI { // true when both graphics pages are the same (except for the progress bar). bool pagesIdentical; + size_t text_cols_, text_rows_; + // Log text overlay, displayed when a magic key is pressed. - char** text; - size_t text_cols, text_rows; - size_t text_col, text_row, text_top; + char** text_; + size_t text_col_, text_row_, text_top_; + bool show_text; bool show_text_ever; // has show_text ever been true? - char** menu; - const char* const* menu_headers; + char** menu_; + const char* const* menu_headers_; bool show_menu; int menu_items, menu_sel; + // An alternate text screen, swapped with 'text_' when we're viewing a log file. + char** file_viewer_text_; + pthread_t progress_thread_; int animation_fps; -- cgit v1.2.3 From 1857a7f57929a3bb4c831199b702059b259b0e3e Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 15 May 2015 16:19:20 -0700 Subject: Don't use TEMP_FAILURE_RETRY on close in recovery. Bug: http://b/20501816 Change-Id: I35efcd8dcec7a6492ba70602d380d9980cdda31f (cherry picked from commit b47afedb42866e85b76822736d915afd371ef5f0) --- updater/blockimg.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/updater/blockimg.c b/updater/blockimg.c index 532e7b845..a0e9aec6a 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -694,7 +694,7 @@ static int WriteStash(const char* base, const char* id, int blocks, uint8_t* buf wsout: if (fd != -1) { - TEMP_FAILURE_RETRY(close(fd)); + close(fd); } if (fn) { @@ -1732,7 +1732,7 @@ pbiudone: if (fsync(params.fd) == -1) { fprintf(stderr, "fsync failed: %s\n", strerror(errno)); } - TEMP_FAILURE_RETRY(close(params.fd)); + close(params.fd); } if (logcmd) { -- cgit v1.2.3 From 158e11d6738a751b754d09df7275add589c31191 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Thu, 21 May 2015 16:44:44 -0700 Subject: Separate uncrypt into two modes uncrypt needs to be triggered to prepare the OTA package before rebooting into the recovery. Separate uncrypt into two modes. In mode 1, it uncrypts the OTA package, but will not reboot the device. In mode 2, it wipes the /misc partition and reboots. Needs matching changes in frameworks/base, system/core and external/sepolicy to work properly. Bug: 20012567 Bug: 20949086 Change-Id: I14d25cb62770dd405cb56824d05d649c3a94f315 --- uncrypt/Android.mk | 2 +- uncrypt/uncrypt.cpp | 191 ++++++++++++++++++++++++++-------------------------- 2 files changed, 95 insertions(+), 98 deletions(-) diff --git a/uncrypt/Android.mk b/uncrypt/Android.mk index d832d9724..c7d4d3746 100644 --- a/uncrypt/Android.mk +++ b/uncrypt/Android.mk @@ -20,6 +20,6 @@ LOCAL_SRC_FILES := uncrypt.cpp LOCAL_MODULE := uncrypt -LOCAL_STATIC_LIBRARIES := libfs_mgr liblog libcutils +LOCAL_STATIC_LIBRARIES := libbase liblog libfs_mgr libcutils include $(BUILD_EXECUTABLE) diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index 11766f14c..6e670a449 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -40,25 +40,29 @@ // file data to use as an update package. #include +#include +#include +#include #include #include #include -#include -#include -#include -#include -#include #include +#include +#include +#include -#define LOG_TAG "uncrypt" -#include +#include +#include #include #include +#define LOG_TAG "uncrypt" +#include #define WINDOW_SIZE 5 -#define RECOVERY_COMMAND_FILE "/cache/recovery/command" -#define RECOVERY_COMMAND_FILE_TMP "/cache/recovery/command.tmp" -#define CACHE_BLOCK_MAP "/cache/recovery/block.map" + +static const std::string cache_block_map = "/cache/recovery/block.map"; +static const std::string status_file = "/cache/recovery/uncrypt_status"; +static const std::string uncrypt_file = "/cache/recovery/uncrypt_file"; static struct fstab* fstab = NULL; @@ -155,65 +159,35 @@ static const char* find_block_device(const char* path, bool* encryptable, bool* return NULL; } -// Parse the command file RECOVERY_COMMAND_FILE to find the update package -// name. If it's on the /data partition, replace the package name with the -// block map file name and store it temporarily in RECOVERY_COMMAND_FILE_TMP. -// It will be renamed to RECOVERY_COMMAND_FILE if uncrypt finishes -// successfully. -static char* find_update_package() +// Parse uncrypt_file to find the update package name. +static bool find_uncrypt_package(std::string& package_name) { - FILE* f = fopen(RECOVERY_COMMAND_FILE, "r"); - if (f == NULL) { - return NULL; + if (!android::base::ReadFileToString(uncrypt_file, &package_name)) { + ALOGE("failed to open \"%s\": %s\n", uncrypt_file.c_str(), strerror(errno)); + return false; } - int fd = open(RECOVERY_COMMAND_FILE_TMP, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR); - if (fd < 0) { - ALOGE("failed to open %s\n", RECOVERY_COMMAND_FILE_TMP); - return NULL; - } - FILE* fo = fdopen(fd, "w"); - char* fn = NULL; - char* line = NULL; - size_t len = 0; - while (getline(&line, &len, f) != -1) { - if (strncmp(line, "--update_package=", strlen("--update_package=")) == 0) { - fn = strdup(line + strlen("--update_package=")); - // Replace the package name with block map file if it's on /data partition. - if (strncmp(fn, "/data/", strlen("/data/")) == 0) { - fputs("--update_package=@" CACHE_BLOCK_MAP "\n", fo); - continue; - } - } - fputs(line, fo); - } - free(line); - fclose(f); - if (fsync(fd) == -1) { - ALOGE("failed to fsync \"%s\": %s\n", RECOVERY_COMMAND_FILE_TMP, strerror(errno)); - fclose(fo); - return NULL; - } - fclose(fo); - if (fn) { - char* newline = strchr(fn, '\n'); - if (newline) { - *newline = 0; - } - } - return fn; + // Remove the trailing '\n' if present. + package_name = android::base::Trim(package_name); + + return true; } static int produce_block_map(const char* path, const char* map_file, const char* blk_dev, - bool encrypted) { - + bool encrypted, int status_fd) { int mapfd = open(map_file, O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR); - if (mapfd < 0) { + if (mapfd == -1) { ALOGE("failed to open %s\n", map_file); return -1; } FILE* mapf = fdopen(mapfd, "w"); + // Make sure we can write to the status_file. + if (!android::base::WriteStringToFd("0\n", status_fd)) { + ALOGE("failed to update \"%s\"\n", status_file.c_str()); + return -1; + } + struct stat sb; int ret = stat(path, &sb); if (ret != 0) { @@ -259,7 +233,15 @@ static int produce_block_map(const char* path, const char* map_file, const char* } } + int last_progress = 0; while (pos < sb.st_size) { + // Update the status file, progress must be between [0, 99]. + int progress = static_cast(100 * (double(pos) / double(sb.st_size))); + if (progress > last_progress) { + last_progress = progress; + android::base::WriteStringToFd(std::to_string(progress) + "\n", status_fd); + } + if ((tail+1) % WINDOW_SIZE == head) { // write out head buffer int block = head_block; @@ -380,43 +362,15 @@ static void reboot_to_recovery() { ALOGE("reboot didn't succeed?"); } -int main(int argc, char** argv) -{ - const char* input_path; - const char* map_file; - bool do_reboot = true; - - if (argc != 1 && argc != 3) { - fprintf(stderr, "usage: %s [ ]\n", argv[0]); - return 2; - } - - if (argc == 3) { - // when command-line args are given this binary is being used - // for debugging; don't reboot to recovery at the end. - input_path = argv[1]; - map_file = argv[2]; - do_reboot = false; - } else { - input_path = find_update_package(); - if (input_path == NULL) { - // if we're rebooting to recovery without a package (say, - // to wipe data), then we don't need to do anything before - // going to recovery. - ALOGI("no recovery command file or no update package arg"); - reboot_to_recovery(); - return 1; - } - map_file = CACHE_BLOCK_MAP; - } +int uncrypt(const char* input_path, const char* map_file, int status_fd) { - ALOGI("update package is %s", input_path); + ALOGI("update package is \"%s\"", input_path); // Turn the name of the file we're supposed to convert into an // absolute path, so we can find what filesystem it's on. char path[PATH_MAX+1]; if (realpath(input_path, path) == NULL) { - ALOGE("failed to convert %s to absolute path: %s", input_path, strerror(errno)); + ALOGE("failed to convert \"%s\" to absolute path: %s", input_path, strerror(errno)); return 1; } @@ -445,21 +399,64 @@ int main(int argc, char** argv) // On /data we want to convert the file to a block map so that we // can read the package without mounting the partition. On /cache // and /sdcard we leave the file alone. - if (strncmp(path, "/data/", 6) != 0) { - // path does not start with "/data/"; leave it alone. - unlink(RECOVERY_COMMAND_FILE_TMP); - wipe_misc(); - } else { + if (strncmp(path, "/data/", 6) == 0) { ALOGI("writing block map %s", map_file); - if (produce_block_map(path, map_file, blk_dev, encrypted) != 0) { + if (produce_block_map(path, map_file, blk_dev, encrypted, status_fd) != 0) { return 1; } - wipe_misc(); - rename(RECOVERY_COMMAND_FILE_TMP, RECOVERY_COMMAND_FILE); } - if (do_reboot) { + return 0; +} + +int main(int argc, char** argv) { + const char* input_path; + const char* map_file; + + if (argc != 3 && argc != 1 && (argc == 2 && strcmp(argv[1], "--reboot") != 0)) { + fprintf(stderr, "usage: %s [--reboot] [ ]\n", argv[0]); + return 2; + } + + // When uncrypt is started with "--reboot", it wipes misc and reboots. + // Otherwise it uncrypts the package and writes the block map. + if (argc == 2) { + if (read_fstab() == NULL) { + return 1; + } + wipe_misc(); reboot_to_recovery(); + } else { + std::string package; + if (argc == 3) { + // when command-line args are given this binary is being used + // for debugging. + input_path = argv[1]; + map_file = argv[2]; + } else { + if (!find_uncrypt_package(package)) { + return 1; + } + input_path = package.c_str(); + map_file = cache_block_map.c_str(); + } + + // The pipe has been created by the system server. + int status_fd = open(status_file.c_str(), O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR); + if (status_fd == -1) { + ALOGE("failed to open pipe \"%s\": %s\n", status_file.c_str(), strerror(errno)); + return 1; + } + int status = uncrypt(input_path, map_file, status_fd); + if (status != 0) { + android::base::WriteStringToFd("-1\n", status_fd); + close(status_fd); + return 1; + } + + android::base::WriteStringToFd("100\n", status_fd); + close(status_fd); } + return 0; } -- cgit v1.2.3 From 92eea1bc414a7b6faedaf812c33ede17403fcaf7 Mon Sep 17 00:00:00 2001 From: Sami Tolvanen Date: Mon, 27 Apr 2015 11:24:29 +0100 Subject: Handle BLKDISCARD failures In the block updater, if BLKDISCARD fails, the error is silently ignored and some of the blocks may not be erased. This means the target partition will have inconsistent contents. If the ioctl fails, return an error and abort the update. Bug: 20614277 Change-Id: I33867ba9337c514de8ffae59f28584b285324067 (cherry picked from commit cc2428c8181d18c9a88db908fa4eabd2db5601ad) --- updater/blockimg.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/updater/blockimg.c b/updater/blockimg.c index a0e9aec6a..0e2ba4441 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -1424,7 +1424,6 @@ static int PerformCommandErase(CommandParameters* params) { if (!S_ISBLK(st.st_mode)) { fprintf(stderr, "not a block device; skipping erase\n"); - rc = 0; goto pceout; } @@ -1448,7 +1447,7 @@ static int PerformCommandErase(CommandParameters* params) { if (ioctl(params->fd, BLKDISCARD, &blocks) == -1) { fprintf(stderr, "BLKDISCARD ioctl failed: %s\n", strerror(errno)); - // Continue anyway, nothing we can do + goto pceout; } } } -- cgit v1.2.3 From b5dabd25e1d04ce134837b49fbef889e511d4d35 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Thu, 28 May 2015 23:06:17 -0700 Subject: Really don't use TEMP_FAILURE_RETRY with close in recovery. I missed one last time. Bug: http://b/20501816 Change-Id: I9896ee2704237d61ee169f898680761e946e0a56 (cherry picked from commit b3ac676192a093c561b7f15064cbd67733407b12) --- updater/blockimg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/updater/blockimg.c b/updater/blockimg.c index 0e2ba4441..7b8721dbf 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -616,7 +616,7 @@ static int LoadStash(const char* base, const char* id, int verify, int* blocks, lsout: if (fd != -1) { - TEMP_FAILURE_RETRY(close(fd)); + close(fd); } if (fn) { -- cgit v1.2.3 From 2c2cae8a4a18b85043bb6260a59ac7d1589016bf Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 29 May 2015 14:24:02 -0700 Subject: uncrypt: Write status when it reboots to factory reset When it reboots into recovery for a factory reset, it still needs to write the uncrypt status (-1) to the pipe. Bug: 21511893 Change-Id: I1a725820f1e1875146e49b5a6f28af2fbf284fc7 --- uncrypt/uncrypt.cpp | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/uncrypt/uncrypt.cpp b/uncrypt/uncrypt.cpp index 6e670a449..1db3013c6 100644 --- a/uncrypt/uncrypt.cpp +++ b/uncrypt/uncrypt.cpp @@ -427,26 +427,29 @@ int main(int argc, char** argv) { wipe_misc(); reboot_to_recovery(); } else { - std::string package; + // The pipe has been created by the system server. + int status_fd = open(status_file.c_str(), O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR); + if (status_fd == -1) { + ALOGE("failed to open pipe \"%s\": %s\n", status_file.c_str(), strerror(errno)); + return 1; + } + if (argc == 3) { // when command-line args are given this binary is being used // for debugging. input_path = argv[1]; map_file = argv[2]; } else { + std::string package; if (!find_uncrypt_package(package)) { + android::base::WriteStringToFd("-1\n", status_fd); + close(status_fd); return 1; } input_path = package.c_str(); map_file = cache_block_map.c_str(); } - // The pipe has been created by the system server. - int status_fd = open(status_file.c_str(), O_WRONLY | O_CREAT | O_SYNC, S_IRUSR | S_IWUSR); - if (status_fd == -1) { - ALOGE("failed to open pipe \"%s\": %s\n", status_file.c_str(), strerror(errno)); - return 1; - } int status = uncrypt(input_path, map_file, status_fd); if (status != 0) { android::base::WriteStringToFd("-1\n", status_fd); -- cgit v1.2.3 From 604c583c9dd3d47906b1a57c14a7e9650df7471e Mon Sep 17 00:00:00 2001 From: Sami Tolvanen Date: Tue, 9 Jun 2015 21:42:30 +0100 Subject: Zero blocks before BLKDISCARD Due to observed BLKDISCARD flakiness, overwrite blocks that we want to discard with zeros first to avoid later issues with dm-verity if BLKDISCARD is not successful. Bug: 20614277 Bug: 20881595 Change-Id: I0280fe115b020dcab35f49041fb55b7f8e793da3 (cherry picked from commit 96392b97f6bf1670d478494fb6df89a3410e53fa) --- updater/blockimg.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/updater/blockimg.c b/updater/blockimg.c index 7b8721dbf..a2786c832 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -39,11 +39,6 @@ #define BLOCKSIZE 4096 -// Set this to 0 to interpret 'erase' transfers to mean do a -// BLKDISCARD ioctl (the normal behavior). Set to 1 to interpret -// erase to mean fill the region with zeroes. -#define DEBUG_ERASE 0 - #ifndef BLKDISCARD #define BLKDISCARD _IO(0x12,119) #endif @@ -1222,8 +1217,7 @@ static int PerformCommandZero(CommandParameters* params) { } if (params->cmdname[0] == 'z') { - // Update only for the zero command, as the erase command will call - // this if DEBUG_ERASE is defined. + // Update only for the zero command, as the erase command will call this params->written += tgt->size; } @@ -1409,8 +1403,10 @@ static int PerformCommandErase(CommandParameters* params) { struct stat st; uint64_t blocks[2]; - if (DEBUG_ERASE) { - return PerformCommandZero(params); + // Always zero the blocks first to work around possibly flaky BLKDISCARD + // Bug: 20881595 + if (PerformCommandZero(params) != 0) { + goto pceout; } if (!params) { -- cgit v1.2.3 From 6abd52f62be24981398a99ebefc8717eb2294f97 Mon Sep 17 00:00:00 2001 From: Sami Tolvanen Date: Wed, 10 Jun 2015 15:52:04 +0000 Subject: Revert "Zero blocks before BLKDISCARD" This reverts commit 604c583c9dd3d47906b1a57c14a7e9650df7471e. Change-Id: I2b0b283dc3f44bae55c5e9f7231d7c712630c2b5 --- updater/blockimg.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/updater/blockimg.c b/updater/blockimg.c index a2786c832..7b8721dbf 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -39,6 +39,11 @@ #define BLOCKSIZE 4096 +// Set this to 0 to interpret 'erase' transfers to mean do a +// BLKDISCARD ioctl (the normal behavior). Set to 1 to interpret +// erase to mean fill the region with zeroes. +#define DEBUG_ERASE 0 + #ifndef BLKDISCARD #define BLKDISCARD _IO(0x12,119) #endif @@ -1217,7 +1222,8 @@ static int PerformCommandZero(CommandParameters* params) { } if (params->cmdname[0] == 'z') { - // Update only for the zero command, as the erase command will call this + // Update only for the zero command, as the erase command will call + // this if DEBUG_ERASE is defined. params->written += tgt->size; } @@ -1403,10 +1409,8 @@ static int PerformCommandErase(CommandParameters* params) { struct stat st; uint64_t blocks[2]; - // Always zero the blocks first to work around possibly flaky BLKDISCARD - // Bug: 20881595 - if (PerformCommandZero(params) != 0) { - goto pceout; + if (DEBUG_ERASE) { + return PerformCommandZero(params); } if (!params) { -- cgit v1.2.3 From 0460f697a18fa2ea2b98380e663b7d33d9cbdb00 Mon Sep 17 00:00:00 2001 From: Sami Tolvanen Date: Wed, 10 Jun 2015 15:52:04 +0000 Subject: Revert "Zero blocks before BLKDISCARD" This reverts commit 604c583c9dd3d47906b1a57c14a7e9650df7471e. Change-Id: I2b0b283dc3f44bae55c5e9f7231d7c712630c2b5 --- updater/blockimg.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/updater/blockimg.c b/updater/blockimg.c index a2786c832..7b8721dbf 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -39,6 +39,11 @@ #define BLOCKSIZE 4096 +// Set this to 0 to interpret 'erase' transfers to mean do a +// BLKDISCARD ioctl (the normal behavior). Set to 1 to interpret +// erase to mean fill the region with zeroes. +#define DEBUG_ERASE 0 + #ifndef BLKDISCARD #define BLKDISCARD _IO(0x12,119) #endif @@ -1217,7 +1222,8 @@ static int PerformCommandZero(CommandParameters* params) { } if (params->cmdname[0] == 'z') { - // Update only for the zero command, as the erase command will call this + // Update only for the zero command, as the erase command will call + // this if DEBUG_ERASE is defined. params->written += tgt->size; } @@ -1403,10 +1409,8 @@ static int PerformCommandErase(CommandParameters* params) { struct stat st; uint64_t blocks[2]; - // Always zero the blocks first to work around possibly flaky BLKDISCARD - // Bug: 20881595 - if (PerformCommandZero(params) != 0) { - goto pceout; + if (DEBUG_ERASE) { + return PerformCommandZero(params); } if (!params) { -- cgit v1.2.3 From b65f0272c860771f2105668accd175be1ed95ae9 Mon Sep 17 00:00:00 2001 From: Sami Tolvanen Date: Wed, 10 Jun 2015 17:07:05 +0100 Subject: Zero blocks before BLKDISCARD Due to observed BLKDISCARD flakiness, overwrite blocks that we want to discard with zeros first to avoid later issues with dm-verity if BLKDISCARD is not successful. Bug: 20614277 Bug: 20881595 Change-Id: I4f6f2db39db990879ff10468c9db41606497bd6f (cherry picked from commit a3c75e3ea60d61df93461f5c356befe825c429d2) --- updater/blockimg.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/updater/blockimg.c b/updater/blockimg.c index 7b8721dbf..e18480062 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -1404,6 +1404,7 @@ pcdout: static int PerformCommandErase(CommandParameters* params) { char* range = NULL; int i; + int j; int rc = -1; RangeSet* tgt = NULL; struct stat st; @@ -1430,7 +1431,7 @@ static int PerformCommandErase(CommandParameters* params) { range = strtok_r(NULL, " ", ¶ms->cpos); if (range == NULL) { - fprintf(stderr, "missing target blocks for zero\n"); + fprintf(stderr, "missing target blocks for erase\n"); goto pceout; } @@ -1439,7 +1440,22 @@ static int PerformCommandErase(CommandParameters* params) { if (params->canwrite) { fprintf(stderr, " erasing %d blocks\n", tgt->size); + allocate(BLOCKSIZE, ¶ms->buffer, ¶ms->bufsize); + memset(params->buffer, 0, BLOCKSIZE); + for (i = 0; i < tgt->count; ++i) { + // Always zero the blocks first to work around possibly flaky BLKDISCARD + // Bug: 20881595 + if (!check_lseek(params->fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) { + goto pceout; + } + + for (j = tgt->pos[i * 2]; j < tgt->pos[i * 2 + 1]; ++j) { + if (write_all(params->fd, params->buffer, BLOCKSIZE) == -1) { + goto pceout; + } + } + // offset in bytes blocks[0] = tgt->pos[i * 2] * (uint64_t) BLOCKSIZE; // length in bytes -- cgit v1.2.3 From 0005f89c31dfc2ca9053512900571620a0eba842 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 5 Jun 2015 17:59:56 -0700 Subject: Split WipeData into PreWipeData and PostWipeData. Bug: http://b/21760064 Change-Id: Idde268fe4d7e27586ca4469de16783f1ffdc5069 (cherry picked from commit 945548ef7b3eee5dbfb46f6291465d4b0b6d02e1) --- device.h | 17 ++++++++++------- recovery.cpp | 29 ++++++++++++----------------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/device.h b/device.h index dad8ccd56..f74b6b047 100644 --- a/device.h +++ b/device.h @@ -91,13 +91,16 @@ class Device { static const int kHighlightDown = -3; static const int kInvokeItem = -4; - // Called when we do a wipe data/factory reset operation (either via a - // reboot from the main system with the --wipe_data flag, or when the - // user boots into recovery manually and selects the option from the - // menu.) Can perform whatever device-specific wiping actions are - // needed. Return 0 on success. The userdata and cache partitions - // are erased AFTER this returns (whether it returns success or not). - virtual int WipeData() { return 0; } + // Called before and after we do a wipe data/factory reset operation, + // either via a reboot from the main system with the --wipe_data flag, + // or when the user boots into recovery image manually and selects the + // option from the menu, to perform whatever device-specific wiping + // actions are needed. + // Return true on success; returning false from PreWipeData will prevent + // the regular wipe, and returning false from PostWipeData will cause + // the wipe to be considered a failure. + virtual bool PreWipeData() { return true; } + virtual bool PostWipeData() { return true; } private: RecoveryUI* ui_; diff --git a/recovery.cpp b/recovery.cpp index 80a8c7ba5..b7a545898 100644 --- a/recovery.cpp +++ b/recovery.cpp @@ -417,8 +417,7 @@ typedef struct _saved_log_file { struct _saved_log_file* next; } saved_log_file; -static int -erase_volume(const char *volume) { +static bool erase_volume(const char* volume) { bool is_cache = (strcmp(volume, CACHE_ROOT) == 0); ui->SetBackground(RecoveryUI::ERASING); @@ -499,7 +498,7 @@ erase_volume(const char *volume) { copy_logs(); } - return result; + return (result == 0); } static int @@ -673,13 +672,13 @@ static bool wipe_data(int should_confirm, Device* device) { modified_flash = true; ui->Print("\n-- Wiping data...\n"); - if (device->WipeData() == 0 && erase_volume("/data") == 0 && erase_volume("/cache") == 0) { - ui->Print("Data wipe complete.\n"); - return true; - } else { - ui->Print("Data wipe failed.\n"); - return false; - } + bool success = + device->PreWipeData() && + erase_volume("/data") && + erase_volume("/cache") && + device->PostWipeData(); + ui->Print("Data wipe %s.\n", success ? "complete" : "failed"); + return success; } // Return true on success. @@ -691,13 +690,9 @@ static bool wipe_cache(bool should_confirm, Device* device) { modified_flash = true; ui->Print("\n-- Wiping cache...\n"); - if (erase_volume("/cache") == 0) { - ui->Print("Cache wipe complete.\n"); - return true; - } else { - ui->Print("Cache wipe failed.\n"); - return false; - } + bool success = erase_volume("/cache"); + ui->Print("Cache wipe %s.\n", success ? "complete" : "failed"); + return success; } static void choose_recovery_file(Device* device) { -- cgit v1.2.3 From c35f3ce30e4bb0de33fe6fcf0b928d0e4b7269b0 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 23 Jun 2015 11:12:58 -0700 Subject: Allow sideloading without authentication. Bug: http://b/22025550 Change-Id: I20f09ae442536f924f19ede0abf6a2bcc0a5cedf (cherry picked from commit 9813f5ba57fe7d90d45cb1c2b6f65920ce580e72) --- minadbd/adb_main.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/minadbd/adb_main.cpp b/minadbd/adb_main.cpp index f6e240108..7fae99a9a 100644 --- a/minadbd/adb_main.cpp +++ b/minadbd/adb_main.cpp @@ -19,11 +19,12 @@ #include #include -#define TRACE_TAG TRACE_ADB +#define TRACE_TAG TRACE_ADB #include "sysdeps.h" #include "adb.h" +#include "adb_auth.h" #include "transport.h" int adb_main(int is_daemon, int server_port) @@ -35,6 +36,9 @@ int adb_main(int is_daemon, int server_port) // No SIGCHLD. Let the service subproc handle its children. signal(SIGPIPE, SIG_IGN); + // We can't require authentication for sideloading. http://b/22025550. + auth_required = false; + init_transport_registration(); usb_init(); -- cgit v1.2.3 From afc7a78349160743b206ed89aed401c3d2d45761 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Tue, 23 Jun 2015 11:12:58 -0700 Subject: Allow sideloading without authentication. Bug: http://b/22025550 Change-Id: I20f09ae442536f924f19ede0abf6a2bcc0a5cedf (cherry picked from commit 9813f5ba57fe7d90d45cb1c2b6f65920ce580e72) --- minadbd/adb_main.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/minadbd/adb_main.cpp b/minadbd/adb_main.cpp index f6e240108..7fae99a9a 100644 --- a/minadbd/adb_main.cpp +++ b/minadbd/adb_main.cpp @@ -19,11 +19,12 @@ #include #include -#define TRACE_TAG TRACE_ADB +#define TRACE_TAG TRACE_ADB #include "sysdeps.h" #include "adb.h" +#include "adb_auth.h" #include "transport.h" int adb_main(int is_daemon, int server_port) @@ -35,6 +36,9 @@ int adb_main(int is_daemon, int server_port) // No SIGCHLD. Let the service subproc handle its children. signal(SIGPIPE, SIG_IGN); + // We can't require authentication for sideloading. http://b/22025550. + auth_required = false; + init_transport_registration(); usb_init(); -- cgit v1.2.3 From cb9450e11337700907a7a5ec290902196c74fa9b Mon Sep 17 00:00:00 2001 From: caozhiyuan Date: Tue, 19 May 2015 17:21:00 +0800 Subject: Use f_bavail to calculate free space Failures are seen on devices with Linux 3.10. And they are mainly due to this change: https://lwn.net/Articles/546473/ The blocks reserved in this change is not the same thing as what we think are reserved for common usage of root user. And this part is included in free blocks but not in available blocks. Bug: 22118089 Change-Id: I81c9531703298019a4fc11839f28d2cc8b9df34e (cherry picked from commit 3b4977638f48e59d23d7ea2bb6dde78552c257fb) --- applypatch/applypatch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applypatch/applypatch.c b/applypatch/applypatch.c index 6f02a38ee..2358d4292 100644 --- a/applypatch/applypatch.c +++ b/applypatch/applypatch.c @@ -662,7 +662,7 @@ size_t FreeSpaceForFile(const char* filename) { printf("failed to statfs %s: %s\n", filename, strerror(errno)); return -1; } - return sf.f_bsize * sf.f_bfree; + return sf.f_bsize * sf.f_bavail; } int CacheSizeCheck(size_t bytes) { -- cgit v1.2.3 From c3dddce205ef44933813394013ee5ef89b63653b Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Thu, 25 Jun 2015 14:00:31 -0700 Subject: More accurate checking for overlapped ranges. A RangeSet has half-closed half-open bounds. For example, "3,5" contains blocks 3 and 4. So "3,5" and "5,7" are actually not overlapped. Bug: 22098085 Change-Id: I362d259f8b5d62478858ad0422b635bc5068698d (cherry picked from commit c0f56ad76680df555689d4a2397487ef8c16b1a6) --- updater/blockimg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/updater/blockimg.c b/updater/blockimg.c index e18480062..1e95f4e7c 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -101,7 +101,7 @@ static int range_overlaps(RangeSet* r1, RangeSet* r2) { r2_0 = r2->pos[j * 2]; r2_1 = r2->pos[j * 2 + 1]; - if (!(r2_0 > r1_1 || r1_0 > r2_1)) { + if (!(r2_0 >= r1_1 || r1_0 >= r2_1)) { return 1; } } -- cgit v1.2.3 From 9154748cc5398d5606765d67409c3cfcdb8b7ad6 Mon Sep 17 00:00:00 2001 From: caozhiyuan Date: Tue, 19 May 2015 17:21:00 +0800 Subject: Use f_bavail to calculate free space Failures are seen on devices with Linux 3.10. And they are mainly due to this change: https://lwn.net/Articles/546473/ The blocks reserved in this change is not the same thing as what we think are reserved for common usage of root user. And this part is included in free blocks but not in available blocks. Bug: 22118089 Change-Id: I81c9531703298019a4fc11839f28d2cc8b9df34e (cherry picked from commit 3b4977638f48e59d23d7ea2bb6dde78552c257fb) --- applypatch/applypatch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/applypatch/applypatch.c b/applypatch/applypatch.c index 6f02a38ee..2358d4292 100644 --- a/applypatch/applypatch.c +++ b/applypatch/applypatch.c @@ -662,7 +662,7 @@ size_t FreeSpaceForFile(const char* filename) { printf("failed to statfs %s: %s\n", filename, strerror(errno)); return -1; } - return sf.f_bsize * sf.f_bfree; + return sf.f_bsize * sf.f_bavail; } int CacheSizeCheck(size_t bytes) { -- cgit v1.2.3 From 7125f9594db027ce4313d940ce2cafac67ae8c31 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Tue, 30 Jun 2015 23:09:12 -0700 Subject: Revert "Zero blocks before BLKDISCARD" This reverts commit b65f0272c860771f2105668accd175be1ed95ae9. It slows down the update too much on some devices (e.g. increased from 8 mins to 40 mins to take a full OTA update). Bug: 22129621 Change-Id: I4e8d4f6734967caf4f0d19c734027f7b6c107370 --- updater/blockimg.c | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/updater/blockimg.c b/updater/blockimg.c index 1e95f4e7c..5b8a6a3c2 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -1404,7 +1404,6 @@ pcdout: static int PerformCommandErase(CommandParameters* params) { char* range = NULL; int i; - int j; int rc = -1; RangeSet* tgt = NULL; struct stat st; @@ -1431,7 +1430,7 @@ static int PerformCommandErase(CommandParameters* params) { range = strtok_r(NULL, " ", ¶ms->cpos); if (range == NULL) { - fprintf(stderr, "missing target blocks for erase\n"); + fprintf(stderr, "missing target blocks for zero\n"); goto pceout; } @@ -1440,22 +1439,7 @@ static int PerformCommandErase(CommandParameters* params) { if (params->canwrite) { fprintf(stderr, " erasing %d blocks\n", tgt->size); - allocate(BLOCKSIZE, ¶ms->buffer, ¶ms->bufsize); - memset(params->buffer, 0, BLOCKSIZE); - for (i = 0; i < tgt->count; ++i) { - // Always zero the blocks first to work around possibly flaky BLKDISCARD - // Bug: 20881595 - if (!check_lseek(params->fd, (off64_t) tgt->pos[i * 2] * BLOCKSIZE, SEEK_SET)) { - goto pceout; - } - - for (j = tgt->pos[i * 2]; j < tgt->pos[i * 2 + 1]; ++j) { - if (write_all(params->fd, params->buffer, BLOCKSIZE) == -1) { - goto pceout; - } - } - // offset in bytes blocks[0] = tgt->pos[i * 2] * (uint64_t) BLOCKSIZE; // length in bytes -- cgit v1.2.3 From 0ddfa329acb1e6464fe5d66b58257013abf21116 Mon Sep 17 00:00:00 2001 From: Mohamad Ayyash Date: Mon, 29 Jun 2015 18:57:14 -0700 Subject: Allow mounting squashfs partitions Change-Id: Ic023eb7d8a11e2a65172a23ff39fa902ef566183 Signed-off-by: Mohamad Ayyash --- roots.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/roots.cpp b/roots.cpp index ee140160c..bca4b57a2 100644 --- a/roots.cpp +++ b/roots.cpp @@ -111,6 +111,7 @@ int ensure_path_mounted(const char* path) { } return mtd_mount_partition(partition, v->mount_point, v->fs_type, 0); } else if (strcmp(v->fs_type, "ext4") == 0 || + strcmp(v->fs_type, "squashfs") == 0 || strcmp(v->fs_type, "vfat") == 0) { result = mount(v->blk_device, v->mount_point, v->fs_type, MS_NOATIME | MS_NODEV | MS_NODIRATIME, ""); -- cgit v1.2.3 From 1a92c4458dcc983f624a60fb75f9679c213e6814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Marchesin?= Date: Mon, 29 Jun 2015 20:05:48 -0700 Subject: Add drm support to minui MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: 22231636 Change-Id: I103c8e906b7dd9862b7bb89d8642268e9a3006b4 Signed-off-by: Stéphane Marchesin --- minui/Android.mk | 2 + minui/graphics.cpp | 5 + minui/graphics.h | 1 + minui/graphics_drm.cpp | 476 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 484 insertions(+) create mode 100644 minui/graphics_drm.cpp diff --git a/minui/Android.mk b/minui/Android.mk index 52f066256..97724fbf0 100644 --- a/minui/Android.mk +++ b/minui/Android.mk @@ -5,10 +5,12 @@ LOCAL_SRC_FILES := \ events.cpp \ graphics.cpp \ graphics_adf.cpp \ + graphics_drm.cpp \ graphics_fbdev.cpp \ resources.cpp \ LOCAL_WHOLE_STATIC_LIBRARIES += libadf +LOCAL_WHOLE_STATIC_LIBRARIES += libdrm LOCAL_STATIC_LIBRARIES += libpng LOCAL_MODULE := libminui diff --git a/minui/graphics.cpp b/minui/graphics.cpp index f09f1c6b0..c0eea9e38 100644 --- a/minui/graphics.cpp +++ b/minui/graphics.cpp @@ -368,6 +368,11 @@ int gr_init(void) } } + if (!gr_draw) { + gr_backend = open_drm(); + gr_draw = gr_backend->init(gr_backend); + } + if (!gr_draw) { gr_backend = open_fbdev(); gr_draw = gr_backend->init(gr_backend); diff --git a/minui/graphics.h b/minui/graphics.h index 81a923383..52968eb10 100644 --- a/minui/graphics.h +++ b/minui/graphics.h @@ -38,5 +38,6 @@ struct minui_backend { minui_backend* open_fbdev(); minui_backend* open_adf(); +minui_backend* open_drm(); #endif diff --git a/minui/graphics_drm.cpp b/minui/graphics_drm.cpp new file mode 100644 index 000000000..03e33b775 --- /dev/null +++ b/minui/graphics_drm.cpp @@ -0,0 +1,476 @@ +/* + * Copyright (C) 2015 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "minui.h" +#include "graphics.h" + +#define ARRAY_SIZE(A) (sizeof(A)/sizeof(*(A))) + +struct drm_surface { + GRSurface base; + uint32_t fb_id; + uint32_t handle; +}; + +static drm_surface *drm_surfaces[2]; +static int current_buffer; + +static drmModeCrtc *main_monitor_crtc; +static drmModeConnector *main_monitor_connector; + +static int drm_fd = -1; + +static void drm_disable_crtc(int drm_fd, drmModeCrtc *crtc) { + if (crtc) { + drmModeSetCrtc(drm_fd, crtc->crtc_id, + 0, // fb_id + 0, 0, // x,y + NULL, // connectors + 0, // connector_count + NULL); // mode + } +} + +static void drm_enable_crtc(int drm_fd, drmModeCrtc *crtc, + struct drm_surface *surface) { + int32_t ret; + + ret = drmModeSetCrtc(drm_fd, crtc->crtc_id, + surface->fb_id, + 0, 0, // x,y + &main_monitor_connector->connector_id, + 1, // connector_count + &main_monitor_crtc->mode); + + if (ret) + printf("drmModeSetCrtc failed ret=%d\n", ret); +} + +static void drm_blank(minui_backend* backend __unused, bool blank) { + if (blank) + drm_disable_crtc(drm_fd, main_monitor_crtc); + else + drm_enable_crtc(drm_fd, main_monitor_crtc, + drm_surfaces[current_buffer]); +} + +static void drm_destroy_surface(struct drm_surface *surface) { + struct drm_gem_close gem_close; + int ret; + + if(!surface) + return; + + if (surface->base.data) + munmap(surface->base.data, + surface->base.row_bytes * surface->base.height); + + if (surface->fb_id) { + ret = drmModeRmFB(drm_fd, surface->fb_id); + if (ret) + printf("drmModeRmFB failed ret=%d\n", ret); + } + + if (surface->handle) { + memset(&gem_close, 0, sizeof(gem_close)); + gem_close.handle = surface->handle; + + ret = drmIoctl(drm_fd, DRM_IOCTL_GEM_CLOSE, &gem_close); + if (ret) + printf("DRM_IOCTL_GEM_CLOSE failed ret=%d\n", ret); + } + + free(surface); +} + +static int drm_format_to_bpp(uint32_t format) { + switch(format) { + case DRM_FORMAT_ABGR8888: + case DRM_FORMAT_BGRA8888: + case DRM_FORMAT_RGBX8888: + case DRM_FORMAT_BGRX8888: + case DRM_FORMAT_XBGR8888: + case DRM_FORMAT_XRGB8888: + return 32; + case DRM_FORMAT_RGB565: + return 16; + default: + printf("Unknown format %d\n", format); + return 32; + } +} + +static drm_surface *drm_create_surface(int width, int height) { + struct drm_surface *surface; + struct drm_mode_create_dumb create_dumb; + uint32_t format; + int ret; + + surface = (struct drm_surface*)calloc(1, sizeof(*surface)); + if (!surface) { + printf("Can't allocate memory\n"); + return NULL; + } + +#if defined(RECOVERY_ABGR) + format = DRM_FORMAT_RGBA8888; +#elif defined(RECOVERY_BGRA) + format = DRM_FORMAT_ARGB8888; +#elif defined(RECOVERY_RGBX) + format = DRM_FORMAT_XBGR8888; +#else + format = DRM_FORMAT_RGB565; +#endif + + memset(&create_dumb, 0, sizeof(create_dumb)); + create_dumb.height = height; + create_dumb.width = width; + create_dumb.bpp = drm_format_to_bpp(format); + create_dumb.flags = 0; + + ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_CREATE_DUMB, &create_dumb); + if (ret) { + printf("DRM_IOCTL_MODE_CREATE_DUMB failed ret=%d\n",ret); + drm_destroy_surface(surface); + return NULL; + } + surface->handle = create_dumb.handle; + + uint32_t handles[4], pitches[4], offsets[4]; + + handles[0] = surface->handle; + pitches[0] = create_dumb.pitch; + offsets[0] = 0; + + ret = drmModeAddFB2(drm_fd, width, height, + format, handles, pitches, offsets, + &(surface->fb_id), 0); + if (ret) { + printf("drmModeAddFB2 failed ret=%d\n", ret); + drm_destroy_surface(surface); + return NULL; + } + + struct drm_mode_map_dumb map_dumb; + memset(&map_dumb, 0, sizeof(map_dumb)); + map_dumb.handle = create_dumb.handle; + ret = drmIoctl(drm_fd, DRM_IOCTL_MODE_MAP_DUMB, &map_dumb); + if (ret) { + printf("DRM_IOCTL_MODE_MAP_DUMB failed ret=%d\n",ret); + drm_destroy_surface(surface); + return NULL;; + } + + surface->base.height = height; + surface->base.width = width; + surface->base.row_bytes = create_dumb.pitch; + surface->base.pixel_bytes = create_dumb.bpp / 8; + surface->base.data = (unsigned char*) + mmap(NULL, + surface->base.height * surface->base.row_bytes, + PROT_READ | PROT_WRITE, MAP_SHARED, + drm_fd, map_dumb.offset); + if (surface->base.data == MAP_FAILED) { + perror("mmap() failed"); + drm_destroy_surface(surface); + return NULL; + } + + return surface; +} + +static drmModeCrtc *find_crtc_for_connector(int fd, + drmModeRes *resources, + drmModeConnector *connector) { + int i, j; + drmModeEncoder *encoder; + int32_t crtc; + + /* + * Find the encoder. If we already have one, just use it. + */ + if (connector->encoder_id) + encoder = drmModeGetEncoder(fd, connector->encoder_id); + else + encoder = NULL; + + if (encoder && encoder->crtc_id) { + crtc = encoder->crtc_id; + drmModeFreeEncoder(encoder); + return drmModeGetCrtc(fd, crtc); + } + + /* + * Didn't find anything, try to find a crtc and encoder combo. + */ + crtc = -1; + for (i = 0; i < connector->count_encoders; i++) { + encoder = drmModeGetEncoder(fd, connector->encoders[i]); + + if (encoder) { + for (j = 0; j < resources->count_crtcs; j++) { + if (!(encoder->possible_crtcs & (1 << j))) + continue; + crtc = resources->crtcs[j]; + break; + } + if (crtc >= 0) { + drmModeFreeEncoder(encoder); + return drmModeGetCrtc(fd, crtc); + } + } + } + + return NULL; +} + +static drmModeConnector *find_used_connector_by_type(int fd, + drmModeRes *resources, + unsigned type) { + int i; + for (i = 0; i < resources->count_connectors; i++) { + drmModeConnector *connector; + + connector = drmModeGetConnector(fd, resources->connectors[i]); + if (connector) { + if ((connector->connector_type == type) && + (connector->connection == DRM_MODE_CONNECTED) && + (connector->count_modes > 0)) + return connector; + + drmModeFreeConnector(connector); + } + } + return NULL; +} + +static drmModeConnector *find_first_connected_connector(int fd, + drmModeRes *resources) { + int i; + for (i = 0; i < resources->count_connectors; i++) { + drmModeConnector *connector; + + connector = drmModeGetConnector(fd, resources->connectors[i]); + if (connector) { + if ((connector->count_modes > 0) && + (connector->connection == DRM_MODE_CONNECTED)) + return connector; + + drmModeFreeConnector(connector); + } + } + return NULL; +} + +static drmModeConnector *find_main_monitor(int fd, drmModeRes *resources, + uint32_t *mode_index) { + unsigned i = 0; + int modes; + /* Look for LVDS/eDP/DSI connectors. Those are the main screens. */ + unsigned kConnectorPriority[] = { + DRM_MODE_CONNECTOR_LVDS, + DRM_MODE_CONNECTOR_eDP, + DRM_MODE_CONNECTOR_DSI, + }; + + drmModeConnector *main_monitor_connector = NULL; + do { + main_monitor_connector = find_used_connector_by_type(fd, + resources, + kConnectorPriority[i]); + i++; + } while (!main_monitor_connector && i < ARRAY_SIZE(kConnectorPriority)); + + /* If we didn't find a connector, grab the first one that is connected. */ + if (!main_monitor_connector) + main_monitor_connector = + find_first_connected_connector(fd, resources); + + /* If we still didn't find a connector, give up and return. */ + if (!main_monitor_connector) + return NULL; + + *mode_index = 0; + for (modes = 0; modes < main_monitor_connector->count_modes; modes++) { + if (main_monitor_connector->modes[modes].type & + DRM_MODE_TYPE_PREFERRED) { + *mode_index = modes; + break; + } + } + + return main_monitor_connector; +} + +static void disable_non_main_crtcs(int fd, + drmModeRes *resources, + drmModeCrtc* main_crtc) { + int i; + drmModeCrtc* crtc; + + for (i = 0; i < resources->count_connectors; i++) { + drmModeConnector *connector; + + connector = drmModeGetConnector(fd, resources->connectors[i]); + crtc = find_crtc_for_connector(fd, resources, connector); + if (crtc->crtc_id != main_crtc->crtc_id) + drm_disable_crtc(fd, crtc); + drmModeFreeCrtc(crtc); + } +} + +static GRSurface* drm_init(minui_backend* backend __unused) { + drmModeRes *res = NULL; + uint32_t selected_mode; + char *dev_name; + int width, height; + int ret, i; + + /* Consider DRM devices in order. */ + for (i = 0; i < DRM_MAX_MINOR; i++) { + uint64_t cap = 0; + + ret = asprintf(&dev_name, DRM_DEV_NAME, DRM_DIR_NAME, i); + if (ret < 0) + continue; + + drm_fd = open(dev_name, O_RDWR, 0); + free(dev_name); + if (drm_fd < 0) + continue; + + /* We need dumb buffers. */ + ret = drmGetCap(drm_fd, DRM_CAP_DUMB_BUFFER, &cap); + if (ret || cap == 0) { + close(drm_fd); + continue; + } + + res = drmModeGetResources(drm_fd); + if (!res) { + close(drm_fd); + continue; + } + + /* Use this device if it has at least one connected monitor. */ + if (res->count_crtcs > 0 && res->count_connectors > 0) + if (find_first_connected_connector(drm_fd, res)) + break; + + drmModeFreeResources(res); + close(drm_fd); + res = NULL; + } + + if (drm_fd < 0 || res == NULL) { + perror("cannot find/open a drm device"); + return NULL; + } + + main_monitor_connector = find_main_monitor(drm_fd, + res, &selected_mode); + + if (!main_monitor_connector) { + printf("main_monitor_connector not found\n"); + drmModeFreeResources(res); + close(drm_fd); + return NULL; + } + + main_monitor_crtc = find_crtc_for_connector(drm_fd, res, + main_monitor_connector); + + if (!main_monitor_crtc) { + printf("main_monitor_crtc not found\n"); + drmModeFreeResources(res); + close(drm_fd); + return NULL; + } + + disable_non_main_crtcs(drm_fd, + res, main_monitor_crtc); + + main_monitor_crtc->mode = main_monitor_connector->modes[selected_mode]; + + width = main_monitor_crtc->mode.hdisplay; + height = main_monitor_crtc->mode.vdisplay; + + drmModeFreeResources(res); + + drm_surfaces[0] = drm_create_surface(width, height); + drm_surfaces[1] = drm_create_surface(width, height); + if (!drm_surfaces[0] || !drm_surfaces[1]) { + drm_destroy_surface(drm_surfaces[0]); + drm_destroy_surface(drm_surfaces[1]); + drmModeFreeResources(res); + close(drm_fd); + return NULL; + } + + current_buffer = 0; + + drm_enable_crtc(drm_fd, main_monitor_crtc, drm_surfaces[1]); + + return &(drm_surfaces[0]->base); +} + +static GRSurface* drm_flip(minui_backend* backend __unused) { + int ret; + + ret = drmModePageFlip(drm_fd, main_monitor_crtc->crtc_id, + drm_surfaces[current_buffer]->fb_id, 0, NULL); + if (ret < 0) { + printf("drmModePageFlip failed ret=%d\n", ret); + return NULL; + } + current_buffer = 1 - current_buffer; + return &(drm_surfaces[current_buffer]->base); +} + +static void drm_exit(minui_backend* backend __unused) { + drm_disable_crtc(drm_fd, main_monitor_crtc); + drm_destroy_surface(drm_surfaces[0]); + drm_destroy_surface(drm_surfaces[1]); + drmModeFreeCrtc(main_monitor_crtc); + drmModeFreeConnector(main_monitor_connector); + close(drm_fd); + drm_fd = -1; +} + +static minui_backend drm_backend = { + .init = drm_init, + .flip = drm_flip, + .blank = drm_blank, + .exit = drm_exit, +}; + +minui_backend* open_drm() { + return &drm_backend; +} -- cgit v1.2.3 From 98c1a3de23ae8b589c36e74939193c44d25cac65 Mon Sep 17 00:00:00 2001 From: Paul Lawrence Date: Mon, 6 Jul 2015 10:44:33 -0700 Subject: Change init sequence to support file level encryption File level encryption must get the key between mounting userdata and calling post_fs_data when the directories are created. This requires access to keymaster, which in turn is found from a system property. Split property loaded into system and data, and load in right order. Bug: 22233063 Change-Id: I409c12e3f4a8cef474eb48818e96760fe292cc49 --- etc/init.rc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/etc/init.rc b/etc/init.rc index 6c07c6027..427727768 100644 --- a/etc/init.rc +++ b/etc/init.rc @@ -47,8 +47,8 @@ on boot class_start default # Load properties from /system/ + /factory after fs mount. -on load_all_props_action - load_all_props +on load_system_props_action + load_system_props on firmware_mounts_complete rm /dev/.booting @@ -63,7 +63,7 @@ on late-init # Load properties from /system/ + /factory after fs mount. Place # this in another action so that the load will be scheduled after the prior # issued fs triggers have completed. - trigger load_all_props_action + trigger load_system_props_action # Remove a file to wake up anything waiting for firmware trigger firmware_mounts_complete -- cgit v1.2.3 From 392879eec0ef42155a3f641f1979891d055f923c Mon Sep 17 00:00:00 2001 From: Paul Lawrence Date: Tue, 7 Jul 2015 17:05:39 +0000 Subject: Revert "Change init sequence to support file level encryption" This reverts commit 98c1a3de23ae8b589c36e74939193c44d25cac65. Change-Id: I524060418de18f97c3865ebc4435f501015e92ee --- etc/init.rc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/etc/init.rc b/etc/init.rc index 427727768..6c07c6027 100644 --- a/etc/init.rc +++ b/etc/init.rc @@ -47,8 +47,8 @@ on boot class_start default # Load properties from /system/ + /factory after fs mount. -on load_system_props_action - load_system_props +on load_all_props_action + load_all_props on firmware_mounts_complete rm /dev/.booting @@ -63,7 +63,7 @@ on late-init # Load properties from /system/ + /factory after fs mount. Place # this in another action so that the load will be scheduled after the prior # issued fs triggers have completed. - trigger load_system_props_action + trigger load_all_props_action # Remove a file to wake up anything waiting for firmware trigger firmware_mounts_complete -- cgit v1.2.3 From 69772764159f4cdf4f81400774a428e55f687f07 Mon Sep 17 00:00:00 2001 From: Paul Lawrence Date: Tue, 7 Jul 2015 17:05:39 +0000 Subject: Revert "Change init sequence to support file level encryption" This reverts commit 98c1a3de23ae8b589c36e74939193c44d25cac65. Change-Id: I524060418de18f97c3865ebc4435f501015e92ee --- etc/init.rc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/etc/init.rc b/etc/init.rc index 427727768..6c07c6027 100644 --- a/etc/init.rc +++ b/etc/init.rc @@ -47,8 +47,8 @@ on boot class_start default # Load properties from /system/ + /factory after fs mount. -on load_system_props_action - load_system_props +on load_all_props_action + load_all_props on firmware_mounts_complete rm /dev/.booting @@ -63,7 +63,7 @@ on late-init # Load properties from /system/ + /factory after fs mount. Place # this in another action so that the load will be scheduled after the prior # issued fs triggers have completed. - trigger load_system_props_action + trigger load_all_props_action # Remove a file to wake up anything waiting for firmware trigger firmware_mounts_complete -- cgit v1.2.3 From be19dce86ce7d4a83f1cfcd11db393f8be8f4397 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Fri, 31 Jul 2015 15:56:44 -0700 Subject: udpater: Call fsync() after rename(). We need to ensure the renamed filename reaches the underlying storage. Bug: 22840552 Change-Id: I824b6e9d8a9c5966035be7b42a73678d07376342 (cherry picked from commit dc3922622a94af4f6412fd68e8f075f839ab2348) --- updater/blockimg.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/updater/blockimg.c b/updater/blockimg.c index 5b8a6a3c2..b006d10c5 100644 --- a/updater/blockimg.c +++ b/updater/blockimg.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -632,6 +633,7 @@ static int WriteStash(const char* base, const char* id, int blocks, uint8_t* buf char *cn = NULL; int fd = -1; int rc = -1; + int dfd = -1; int res; struct stat st; @@ -690,6 +692,20 @@ static int WriteStash(const char* base, const char* id, int blocks, uint8_t* buf goto wsout; } + const char* dname; + dname = dirname(cn); + dfd = TEMP_FAILURE_RETRY(open(dname, O_RDONLY | O_DIRECTORY)); + + if (dfd == -1) { + fprintf(stderr, "failed to open \"%s\" failed: %s\n", dname, strerror(errno)); + goto wsout; + } + + if (fsync(dfd) == -1) { + fprintf(stderr, "fsync \"%s\" failed: %s\n", dname, strerror(errno)); + goto wsout; + } + rc = 0; wsout: @@ -697,6 +713,10 @@ wsout: close(fd); } + if (dfd != -1) { + close(dfd); + } + if (fn) { free(fn); } -- cgit v1.2.3