Revert "Revert "Some cleanups to recovery.""

This reverts commit 8584fcf677.

This CL re-lands commit c0319b60f5.
The "stage" and "reason" variables are now declared as global by
dropping the static qualifier, because they may be used by vendor
recovery libraries.

Test: lunch aosp_angler-userdebug; mmma bootable/recovery
Test: lunch aosp_dragon-userdebug; mmma bootable/recovery

Change-Id: I252c346f450079478cff22bbff01590b8ab2e2b3
This commit is contained in:
Tao Bao 2016-11-03 11:37:15 -07:00
parent 4011bb161a
commit ac9d94d19c
7 changed files with 70 additions and 71 deletions

View file

@ -17,15 +17,22 @@
#ifndef RECOVERY_COMMON_H #ifndef RECOVERY_COMMON_H
#define RECOVERY_COMMON_H #define RECOVERY_COMMON_H
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#define STRINGIFY(x) #x #define STRINGIFY(x) #x
#define EXPAND(x) STRINGIFY(x) #define EXPAND(x) STRINGIFY(x)
class RecoveryUI;
extern RecoveryUI* ui;
extern bool modified_flash; extern bool modified_flash;
typedef struct fstab_rec Volume;
// The current stage, e.g. "1/2".
extern const char* stage;
// The reason argument provided in "--reason=".
extern const char* reason;
// fopen a file, mounting volumes and making parent dirs as necessary. // fopen a file, mounting volumes and making parent dirs as necessary.
FILE* fopen_path(const char *path, const char *mode); FILE* fopen_path(const char *path, const char *mode);

View file

@ -43,8 +43,6 @@
#include "ui.h" #include "ui.h"
#include "verifier.h" #include "verifier.h"
extern RecoveryUI* ui;
#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary" #define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
#define PUBLIC_KEYS_FILE "/res/keys" #define PUBLIC_KEYS_FILE "/res/keys"
static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata"; static constexpr const char* METADATA_PATH = "META-INF/com/android/metadata";

View file

@ -20,10 +20,9 @@
#include <string> #include <string>
#include <ziparchive/zip_archive.h> #include <ziparchive/zip_archive.h>
#include "common.h"
enum { INSTALL_SUCCESS, INSTALL_ERROR, INSTALL_CORRUPT, INSTALL_NONE, INSTALL_SKIPPED, enum { INSTALL_SUCCESS, INSTALL_ERROR, INSTALL_CORRUPT, INSTALL_NONE, INSTALL_SKIPPED,
INSTALL_RETRY }; INSTALL_RETRY };
// Install the package specified by root_path. If INSTALL_SUCCESS is // Install the package specified by root_path. If INSTALL_SUCCESS is
// returned and *wipe_cache is true on exit, caller should wipe the // returned and *wipe_cache is true on exit, caller should wipe the
// cache partition. // cache partition.

View file

@ -20,7 +20,7 @@
#include <stdint.h> #include <stdint.h>
#include <string> #include <string>
#include "openssl/sha.h" #include <openssl/sha.h>
static std::string print_sha1(const uint8_t* sha1, size_t len) { static std::string print_sha1(const uint8_t* sha1, size_t len) {
const char* hex = "0123456789abcdef"; const char* hex = "0123456789abcdef";
@ -41,7 +41,7 @@ static std::string short_sha1(const uint8_t sha1[SHA_DIGEST_LENGTH]) {
} }
static std::string print_hex(const uint8_t* bytes, size_t len) { static std::string print_hex(const uint8_t* bytes, size_t len) {
return print_sha1(bytes, len); return print_sha1(bytes, len);
} }
#endif // RECOVERY_PRINT_SHA1_H #endif // RECOVERY_PRINT_SHA1_H

View file

@ -69,8 +69,6 @@
#include "ui.h" #include "ui.h"
#include "screen_ui.h" #include "screen_ui.h"
struct selabel_handle *sehandle;
static const struct option OPTIONS[] = { static const struct option OPTIONS[] = {
{ "update_package", required_argument, NULL, 'u' }, { "update_package", required_argument, NULL, 'u' },
{ "retry_count", required_argument, NULL, 'n' }, { "retry_count", required_argument, NULL, 'n' },
@ -119,15 +117,18 @@ static const int BATTERY_READ_TIMEOUT_IN_SEC = 10;
// So we should check battery with a slightly lower limitation. // So we should check battery with a slightly lower limitation.
static const int BATTERY_OK_PERCENTAGE = 20; static const int BATTERY_OK_PERCENTAGE = 20;
static const int BATTERY_WITH_CHARGER_OK_PERCENTAGE = 15; static const int BATTERY_WITH_CHARGER_OK_PERCENTAGE = 15;
constexpr const char* RECOVERY_WIPE = "/etc/recovery.wipe"; static constexpr const char* RECOVERY_WIPE = "/etc/recovery.wipe";
static constexpr const char* DEFAULT_LOCALE = "en_US";
RecoveryUI* ui = NULL; static std::string locale;
static const char* locale = "en_US";
char* stage = NULL;
char* reason = NULL;
bool modified_flash = false;
static bool has_cache = false; static bool has_cache = false;
RecoveryUI* ui = nullptr;
bool modified_flash = false;
const char* stage = nullptr;
const char* reason = nullptr;
struct selabel_handle* sehandle;
/* /*
* The recovery tool communicates with the main system through /cache files. * The recovery tool communicates with the main system through /cache files.
* /cache/recovery/command - INPUT - command line for tool, one arg per line * /cache/recovery/command - INPUT - command line for tool, one arg per line
@ -206,6 +207,9 @@ FILE* fopen_path(const char *path, const char *mode) {
// close a file, log an error if the error indicator is set // close a file, log an error if the error indicator is set
static void check_and_fclose(FILE *fp, const char *name) { static void check_and_fclose(FILE *fp, const char *name) {
fflush(fp); fflush(fp);
if (fsync(fileno(fp)) == -1) {
PLOG(ERROR) << "Failed to fsync " << name;
}
if (ferror(fp)) { if (ferror(fp)) {
PLOG(ERROR) << "Error in " << name; PLOG(ERROR) << "Error in " << name;
} }
@ -515,24 +519,18 @@ static void copy_logs() {
// clear the recovery command and prepare to boot a (hopefully working) system, // clear the recovery command and prepare to boot a (hopefully working) system,
// copy our log file to cache as well (for the system to read). This function is // copy our log file to cache as well (for the system to read). This function is
// idempotent: call it as many times as you like. // idempotent: call it as many times as you like.
static void static void finish_recovery() {
finish_recovery() {
// Save the locale to cache, so if recovery is next started up // Save the locale to cache, so if recovery is next started up
// without a --locale argument (eg, directly from the bootloader) // without a --locale argument (eg, directly from the bootloader)
// it will use the last-known locale. // it will use the last-known locale.
if (locale != NULL) { if (!locale.empty() && has_cache) {
size_t len = strlen(locale); LOG(INFO) << "Saving locale \"" << locale << "\"";
__pmsg_write(LOCALE_FILE, locale, len);
if (has_cache) { FILE* fp = fopen_path(LOCALE_FILE, "w");
LOG(INFO) << "Saving locale \"" << locale << "\""; if (!android::base::WriteStringToFd(locale, fileno(fp))) {
FILE* fp = fopen_path(LOCALE_FILE, "w"); PLOG(ERROR) << "Failed to save locale to " << LOCALE_FILE;
if (fp != NULL) {
fwrite(locale, 1, len, fp);
fflush(fp);
fsync(fileno(fp));
check_and_fclose(fp, LOCALE_FILE);
}
} }
check_and_fclose(fp, LOCALE_FILE);
} }
copy_logs(); copy_logs();
@ -1282,40 +1280,32 @@ print_property(const char *key, const char *name, void *cookie) {
printf("%s=%s\n", key, name); printf("%s=%s\n", key, name);
} }
static void static std::string load_locale_from_cache() {
load_locale_from_cache() { if (ensure_path_mounted(LOCALE_FILE) != 0) {
FILE* fp = fopen_path(LOCALE_FILE, "r"); LOG(ERROR) << "Can't mount " << LOCALE_FILE;
char buffer[80]; return "";
if (fp != NULL) {
fgets(buffer, sizeof(buffer), fp);
int j = 0;
unsigned int i;
for (i = 0; i < sizeof(buffer) && buffer[i]; ++i) {
if (!isspace(buffer[i])) {
buffer[j++] = buffer[i];
}
}
buffer[j] = 0;
locale = strdup(buffer);
check_and_fclose(fp, LOCALE_FILE);
} }
std::string content;
if (!android::base::ReadFileToString(LOCALE_FILE, &content)) {
PLOG(ERROR) << "Can't read " << LOCALE_FILE;
return "";
}
return android::base::Trim(content);
} }
static RecoveryUI* gCurrentUI = NULL; void ui_print(const char* format, ...) {
std::string buffer;
void
ui_print(const char* format, ...) {
char buffer[256];
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
vsnprintf(buffer, sizeof(buffer), format, ap); android::base::StringAppendV(&buffer, format, ap);
va_end(ap); va_end(ap);
if (gCurrentUI != NULL) { if (ui != nullptr) {
gCurrentUI->Print("%s", buffer); ui->Print("%s", buffer.c_str());
} else { } else {
fputs(buffer, stdout); fputs(buffer.c_str(), stdout);
} }
} }
@ -1324,8 +1314,8 @@ static constexpr char log_characters[] = "VDIWEF";
void UiLogger(android::base::LogId id, android::base::LogSeverity severity, void UiLogger(android::base::LogId id, android::base::LogSeverity severity,
const char* tag, const char* file, unsigned int line, const char* tag, const char* file, unsigned int line,
const char* message) { const char* message) {
if (severity >= android::base::ERROR && gCurrentUI != NULL) { if (severity >= android::base::ERROR && ui != nullptr) {
gCurrentUI->Print("E:%s\n", message); ui->Print("E:%s\n", message);
} else { } else {
fprintf(stdout, "%c:%s\n", log_characters[severity], message); fprintf(stdout, "%c:%s\n", log_characters[severity], message);
} }
@ -1421,7 +1411,7 @@ static void log_failure_code(ErrorCode code, const char *update_package) {
}; };
std::string log_content = android::base::Join(log_buffer, "\n"); std::string log_content = android::base::Join(log_buffer, "\n");
if (!android::base::WriteStringToFile(log_content, TEMPORARY_INSTALL_FILE)) { if (!android::base::WriteStringToFile(log_content, TEMPORARY_INSTALL_FILE)) {
PLOG(ERROR) << "failed to write " << TEMPORARY_INSTALL_FILE; PLOG(ERROR) << "failed to write " << TEMPORARY_INSTALL_FILE;
} }
// Also write the info into last_log. // Also write the info into last_log.
@ -1573,18 +1563,24 @@ int main(int argc, char **argv) {
} }
} }
if (locale == nullptr && has_cache) { if (locale.empty()) {
load_locale_from_cache(); if (has_cache) {
locale = load_locale_from_cache();
}
if (locale.empty()) {
locale = DEFAULT_LOCALE;
}
} }
printf("locale is [%s]\n", locale);
printf("locale is [%s]\n", locale.c_str());
printf("stage is [%s]\n", stage); printf("stage is [%s]\n", stage);
printf("reason is [%s]\n", reason); printf("reason is [%s]\n", reason);
Device* device = make_device(); Device* device = make_device();
ui = device->GetUI(); ui = device->GetUI();
gCurrentUI = ui;
ui->SetLocale(locale); ui->SetLocale(locale.c_str());
ui->Init(); ui->Init();
// Set background string to "installing security update" for security update, // Set background string to "installing security update" for security update,
// otherwise set it to "installing system update". // otherwise set it to "installing system update".
@ -1599,7 +1595,7 @@ int main(int argc, char **argv) {
if (show_text) ui->ShowText(true); if (show_text) ui->ShowText(true);
struct selinux_opt seopts[] = { struct selinux_opt seopts[] = {
{ SELABEL_OPT_PATH, "/file_contexts" } { SELABEL_OPT_PATH, "/file_contexts" }
}; };
sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1); sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
@ -1771,7 +1767,7 @@ int main(int argc, char **argv) {
break; break;
} }
while (true) { while (true) {
pause(); pause();
} }
// Should be unreachable. // Should be unreachable.
return EXIT_SUCCESS; return EXIT_SUCCESS;

View file

@ -17,7 +17,7 @@
#ifndef RECOVERY_ROOTS_H_ #ifndef RECOVERY_ROOTS_H_
#define RECOVERY_ROOTS_H_ #define RECOVERY_ROOTS_H_
#include "common.h" typedef struct fstab_rec Volume;
// Load and parse volume data from /etc/recovery.fstab. // Load and parse volume data from /etc/recovery.fstab.
void load_volume_table(); void load_volume_table();

View file

@ -14,9 +14,11 @@
* limitations under the License. * limitations under the License.
*/ */
#include "verifier.h"
#include <errno.h> #include <errno.h>
#include <malloc.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <algorithm> #include <algorithm>
@ -31,9 +33,6 @@
#include "common.h" #include "common.h"
#include "print_sha1.h" #include "print_sha1.h"
#include "ui.h" #include "ui.h"
#include "verifier.h"
extern RecoveryUI* ui;
static constexpr size_t MiB = 1024 * 1024; static constexpr size_t MiB = 1024 * 1024;