aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Albert2016-10-26 23:53:48 -0500
committerandroid-build-merger2016-10-26 23:53:48 -0500
commit0c571e5923526477a3994458da1c4320b87ea863 (patch)
tree5dd97229a584eb17c2a9f5e65e0646f0ab2b7ce8
parent00f893fe23a7cee4200a1192c7dded26f114e3d6 (diff)
parent9514eb1934f84a926732936a9041e2c8b7587ef7 (diff)
downloadplatform-bootable-recovery-0c571e5923526477a3994458da1c4320b87ea863.tar.gz
platform-bootable-recovery-0c571e5923526477a3994458da1c4320b87ea863.tar.xz
platform-bootable-recovery-0c571e5923526477a3994458da1c4320b87ea863.zip
Merge "Revert "Some cleanups to recovery.""
am: 9514eb1934 Change-Id: Id96135ffde5adaebd45811b9c3d319a99fe28ecd
-rw-r--r--common.h5
-rw-r--r--install.cpp2
-rw-r--r--install.h3
-rw-r--r--print_sha1.h4
-rw-r--r--recovery.cpp110
-rw-r--r--roots.h2
-rw-r--r--verifier.cpp7
7 files changed, 70 insertions, 63 deletions
diff --git a/common.h b/common.h
index f10fe673..a948fb1a 100644
--- a/common.h
+++ b/common.h
@@ -17,16 +17,15 @@
17#ifndef RECOVERY_COMMON_H 17#ifndef RECOVERY_COMMON_H
18#define RECOVERY_COMMON_H 18#define RECOVERY_COMMON_H
19 19
20#include <stdbool.h>
20#include <stdio.h> 21#include <stdio.h>
21#include <stdarg.h> 22#include <stdarg.h>
22 23
23#define STRINGIFY(x) #x 24#define STRINGIFY(x) #x
24#define EXPAND(x) STRINGIFY(x) 25#define EXPAND(x) STRINGIFY(x)
25 26
26class RecoveryUI;
27
28extern RecoveryUI* ui;
29extern bool modified_flash; 27extern bool modified_flash;
28typedef struct fstab_rec Volume;
30 29
31// fopen a file, mounting volumes and making parent dirs as necessary. 30// fopen a file, mounting volumes and making parent dirs as necessary.
32FILE* fopen_path(const char *path, const char *mode); 31FILE* fopen_path(const char *path, const char *mode);
diff --git a/install.cpp b/install.cpp
index dd6ed81c..fada2de6 100644
--- a/install.cpp
+++ b/install.cpp
@@ -47,6 +47,8 @@
47#include "ui.h" 47#include "ui.h"
48#include "verifier.h" 48#include "verifier.h"
49 49
50extern RecoveryUI* ui;
51
50#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary" 52#define ASSUMED_UPDATE_BINARY_NAME "META-INF/com/google/android/update-binary"
51static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt"; 53static constexpr const char* AB_OTA_PAYLOAD_PROPERTIES = "payload_properties.txt";
52static constexpr const char* AB_OTA_PAYLOAD = "payload.bin"; 54static constexpr const char* AB_OTA_PAYLOAD = "payload.bin";
diff --git a/install.h b/install.h
index 1ec01e81..7f66a51c 100644
--- a/install.h
+++ b/install.h
@@ -20,9 +20,10 @@
20#include <string> 20#include <string>
21#include <ziparchive/zip_archive.h> 21#include <ziparchive/zip_archive.h>
22 22
23#include "common.h"
24
23enum { INSTALL_SUCCESS, INSTALL_ERROR, INSTALL_CORRUPT, INSTALL_NONE, INSTALL_SKIPPED, 25enum { INSTALL_SUCCESS, INSTALL_ERROR, INSTALL_CORRUPT, INSTALL_NONE, INSTALL_SKIPPED,
24 INSTALL_RETRY }; 26 INSTALL_RETRY };
25
26// Install the package specified by root_path. If INSTALL_SUCCESS is 27// Install the package specified by root_path. If INSTALL_SUCCESS is
27// returned and *wipe_cache is true on exit, caller should wipe the 28// returned and *wipe_cache is true on exit, caller should wipe the
28// cache partition. 29// cache partition.
diff --git a/print_sha1.h b/print_sha1.h
index 1f858951..c7c1f365 100644
--- a/print_sha1.h
+++ b/print_sha1.h
@@ -20,7 +20,7 @@
20#include <stdint.h> 20#include <stdint.h>
21#include <string> 21#include <string>
22 22
23#include <openssl/sha.h> 23#include "openssl/sha.h"
24 24
25static std::string print_sha1(const uint8_t* sha1, size_t len) { 25static std::string print_sha1(const uint8_t* sha1, size_t len) {
26 const char* hex = "0123456789abcdef"; 26 const char* hex = "0123456789abcdef";
@@ -41,7 +41,7 @@ static std::string short_sha1(const uint8_t sha1[SHA_DIGEST_LENGTH]) {
41} 41}
42 42
43static std::string print_hex(const uint8_t* bytes, size_t len) { 43static std::string print_hex(const uint8_t* bytes, size_t len) {
44 return print_sha1(bytes, len); 44 return print_sha1(bytes, len);
45} 45}
46 46
47#endif // RECOVERY_PRINT_SHA1_H 47#endif // RECOVERY_PRINT_SHA1_H
diff --git a/recovery.cpp b/recovery.cpp
index 4079927e..4d1ad1df 100644
--- a/recovery.cpp
+++ b/recovery.cpp
@@ -69,6 +69,8 @@
69#include "ui.h" 69#include "ui.h"
70#include "screen_ui.h" 70#include "screen_ui.h"
71 71
72struct selabel_handle *sehandle;
73
72static const struct option OPTIONS[] = { 74static const struct option OPTIONS[] = {
73 { "update_package", required_argument, NULL, 'u' }, 75 { "update_package", required_argument, NULL, 'u' },
74 { "retry_count", required_argument, NULL, 'n' }, 76 { "retry_count", required_argument, NULL, 'n' },
@@ -117,17 +119,14 @@ static const int BATTERY_READ_TIMEOUT_IN_SEC = 10;
117// So we should check battery with a slightly lower limitation. 119// So we should check battery with a slightly lower limitation.
118static const int BATTERY_OK_PERCENTAGE = 20; 120static const int BATTERY_OK_PERCENTAGE = 20;
119static const int BATTERY_WITH_CHARGER_OK_PERCENTAGE = 15; 121static const int BATTERY_WITH_CHARGER_OK_PERCENTAGE = 15;
120static constexpr const char* RECOVERY_WIPE = "/etc/recovery.wipe"; 122constexpr const char* RECOVERY_WIPE = "/etc/recovery.wipe";
121static constexpr const char* DEFAULT_LOCALE = "en_US";
122
123static std::string locale;
124static const char* stage = nullptr;
125static const char* reason = nullptr;
126static bool has_cache = false;
127 123
128RecoveryUI* ui = nullptr; 124RecoveryUI* ui = NULL;
125static const char* locale = "en_US";
126char* stage = NULL;
127char* reason = NULL;
129bool modified_flash = false; 128bool modified_flash = false;
130struct selabel_handle* sehandle; 129static bool has_cache = false;
131 130
132/* 131/*
133 * The recovery tool communicates with the main system through /cache files. 132 * The recovery tool communicates with the main system through /cache files.
@@ -207,9 +206,6 @@ FILE* fopen_path(const char *path, const char *mode) {
207// close a file, log an error if the error indicator is set 206// close a file, log an error if the error indicator is set
208static void check_and_fclose(FILE *fp, const char *name) { 207static void check_and_fclose(FILE *fp, const char *name) {
209 fflush(fp); 208 fflush(fp);
210 if (fsync(fileno(fp)) == -1) {
211 PLOG(ERROR) << "Failed to fsync " << name;
212 }
213 if (ferror(fp)) { 209 if (ferror(fp)) {
214 PLOG(ERROR) << "Error in " << name; 210 PLOG(ERROR) << "Error in " << name;
215 } 211 }
@@ -519,18 +515,24 @@ static void copy_logs() {
519// clear the recovery command and prepare to boot a (hopefully working) system, 515// clear the recovery command and prepare to boot a (hopefully working) system,
520// copy our log file to cache as well (for the system to read). This function is 516// copy our log file to cache as well (for the system to read). This function is
521// idempotent: call it as many times as you like. 517// idempotent: call it as many times as you like.
522static void finish_recovery() { 518static void
519finish_recovery() {
523 // Save the locale to cache, so if recovery is next started up 520 // Save the locale to cache, so if recovery is next started up
524 // without a --locale argument (eg, directly from the bootloader) 521 // without a --locale argument (eg, directly from the bootloader)
525 // it will use the last-known locale. 522 // it will use the last-known locale.
526 if (!locale.empty() && has_cache) { 523 if (locale != NULL) {
527 LOG(INFO) << "Saving locale \"" << locale << "\""; 524 size_t len = strlen(locale);
528 525 __pmsg_write(LOCALE_FILE, locale, len);
529 FILE* fp = fopen_path(LOCALE_FILE, "w"); 526 if (has_cache) {
530 if (!android::base::WriteStringToFd(locale, fileno(fp))) { 527 LOG(INFO) << "Saving locale \"" << locale << "\"";
531 PLOG(ERROR) << "Failed to save locale to " << LOCALE_FILE; 528 FILE* fp = fopen_path(LOCALE_FILE, "w");
529 if (fp != NULL) {
530 fwrite(locale, 1, len, fp);
531 fflush(fp);
532 fsync(fileno(fp));
533 check_and_fclose(fp, LOCALE_FILE);
534 }
532 } 535 }
533 check_and_fclose(fp, LOCALE_FILE);
534 } 536 }
535 537
536 copy_logs(); 538 copy_logs();
@@ -1280,32 +1282,40 @@ print_property(const char *key, const char *name, void *cookie) {
1280 printf("%s=%s\n", key, name); 1282 printf("%s=%s\n", key, name);
1281} 1283}
1282 1284
1283static std::string load_locale_from_cache() { 1285static void
1284 if (ensure_path_mounted(LOCALE_FILE) != 0) { 1286load_locale_from_cache() {
1285 LOG(ERROR) << "Can't mount " << LOCALE_FILE; 1287 FILE* fp = fopen_path(LOCALE_FILE, "r");
1286 return ""; 1288 char buffer[80];
1289 if (fp != NULL) {
1290 fgets(buffer, sizeof(buffer), fp);
1291 int j = 0;
1292 unsigned int i;
1293 for (i = 0; i < sizeof(buffer) && buffer[i]; ++i) {
1294 if (!isspace(buffer[i])) {
1295 buffer[j++] = buffer[i];
1296 }
1297 }
1298 buffer[j] = 0;
1299 locale = strdup(buffer);
1300 check_and_fclose(fp, LOCALE_FILE);
1287 } 1301 }
1302}
1288 1303
1289 std::string content; 1304static RecoveryUI* gCurrentUI = NULL;
1290 if (!android::base::ReadFileToString(LOCALE_FILE, &content)) {
1291 PLOG(ERROR) << "Can't read " << LOCALE_FILE;
1292 return "";
1293 }
1294 1305
1295 return android::base::Trim(content); 1306void
1296} 1307ui_print(const char* format, ...) {
1308 char buffer[256];
1297 1309
1298void ui_print(const char* format, ...) {
1299 std::string buffer;
1300 va_list ap; 1310 va_list ap;
1301 va_start(ap, format); 1311 va_start(ap, format);
1302 android::base::StringAppendV(&buffer, format, ap); 1312 vsnprintf(buffer, sizeof(buffer), format, ap);
1303 va_end(ap); 1313 va_end(ap);
1304 1314
1305 if (ui != nullptr) { 1315 if (gCurrentUI != NULL) {
1306 ui->Print("%s", buffer.c_str()); 1316 gCurrentUI->Print("%s", buffer);
1307 } else { 1317 } else {
1308 fputs(buffer.c_str(), stdout); 1318 fputs(buffer, stdout);
1309 } 1319 }
1310} 1320}
1311 1321
@@ -1314,8 +1324,8 @@ static constexpr char log_characters[] = "VDIWEF";
1314void UiLogger(android::base::LogId id, android::base::LogSeverity severity, 1324void UiLogger(android::base::LogId id, android::base::LogSeverity severity,
1315 const char* tag, const char* file, unsigned int line, 1325 const char* tag, const char* file, unsigned int line,
1316 const char* message) { 1326 const char* message) {
1317 if (severity >= android::base::ERROR && ui != nullptr) { 1327 if (severity >= android::base::ERROR && gCurrentUI != NULL) {
1318 ui->Print("E:%s\n", message); 1328 gCurrentUI->Print("E:%s\n", message);
1319 } else { 1329 } else {
1320 fprintf(stdout, "%c:%s\n", log_characters[severity], message); 1330 fprintf(stdout, "%c:%s\n", log_characters[severity], message);
1321 } 1331 }
@@ -1411,7 +1421,7 @@ static void log_failure_code(ErrorCode code, const char *update_package) {
1411 }; 1421 };
1412 std::string log_content = android::base::Join(log_buffer, "\n"); 1422 std::string log_content = android::base::Join(log_buffer, "\n");
1413 if (!android::base::WriteStringToFile(log_content, TEMPORARY_INSTALL_FILE)) { 1423 if (!android::base::WriteStringToFile(log_content, TEMPORARY_INSTALL_FILE)) {
1414 PLOG(ERROR) << "failed to write " << TEMPORARY_INSTALL_FILE; 1424 PLOG(ERROR) << "failed to write " << TEMPORARY_INSTALL_FILE;
1415 } 1425 }
1416 1426
1417 // Also write the info into last_log. 1427 // Also write the info into last_log.
@@ -1563,24 +1573,18 @@ int main(int argc, char **argv) {
1563 } 1573 }
1564 } 1574 }
1565 1575
1566 if (locale.empty()) { 1576 if (locale == nullptr && has_cache) {
1567 if (has_cache) { 1577 load_locale_from_cache();
1568 locale = load_locale_from_cache();
1569 }
1570
1571 if (locale.empty()) {
1572 locale = DEFAULT_LOCALE;
1573 }
1574 } 1578 }
1575 1579 printf("locale is [%s]\n", locale);
1576 printf("locale is [%s]\n", locale.c_str());
1577 printf("stage is [%s]\n", stage); 1580 printf("stage is [%s]\n", stage);
1578 printf("reason is [%s]\n", reason); 1581 printf("reason is [%s]\n", reason);
1579 1582
1580 Device* device = make_device(); 1583 Device* device = make_device();
1581 ui = device->GetUI(); 1584 ui = device->GetUI();
1585 gCurrentUI = ui;
1582 1586
1583 ui->SetLocale(locale.c_str()); 1587 ui->SetLocale(locale);
1584 ui->Init(); 1588 ui->Init();
1585 // Set background string to "installing security update" for security update, 1589 // Set background string to "installing security update" for security update,
1586 // otherwise set it to "installing system update". 1590 // otherwise set it to "installing system update".
@@ -1595,7 +1599,7 @@ int main(int argc, char **argv) {
1595 if (show_text) ui->ShowText(true); 1599 if (show_text) ui->ShowText(true);
1596 1600
1597 struct selinux_opt seopts[] = { 1601 struct selinux_opt seopts[] = {
1598 { SELABEL_OPT_PATH, "/file_contexts" } 1602 { SELABEL_OPT_PATH, "/file_contexts" }
1599 }; 1603 };
1600 1604
1601 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1); 1605 sehandle = selabel_open(SELABEL_CTX_FILE, seopts, 1);
@@ -1767,7 +1771,7 @@ int main(int argc, char **argv) {
1767 break; 1771 break;
1768 } 1772 }
1769 while (true) { 1773 while (true) {
1770 pause(); 1774 pause();
1771 } 1775 }
1772 // Should be unreachable. 1776 // Should be unreachable.
1773 return EXIT_SUCCESS; 1777 return EXIT_SUCCESS;
diff --git a/roots.h b/roots.h
index 542f03b9..a14b7d97 100644
--- a/roots.h
+++ b/roots.h
@@ -17,7 +17,7 @@
17#ifndef RECOVERY_ROOTS_H_ 17#ifndef RECOVERY_ROOTS_H_
18#define RECOVERY_ROOTS_H_ 18#define RECOVERY_ROOTS_H_
19 19
20typedef struct fstab_rec Volume; 20#include "common.h"
21 21
22// Load and parse volume data from /etc/recovery.fstab. 22// Load and parse volume data from /etc/recovery.fstab.
23void load_volume_table(); 23void load_volume_table();
diff --git a/verifier.cpp b/verifier.cpp
index 44098f70..82cdd3bc 100644
--- a/verifier.cpp
+++ b/verifier.cpp
@@ -14,11 +14,9 @@
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 16
17#include "verifier.h"
18
19#include <errno.h> 17#include <errno.h>
18#include <malloc.h>
20#include <stdio.h> 19#include <stdio.h>
21#include <stdlib.h>
22#include <string.h> 20#include <string.h>
23 21
24#include <algorithm> 22#include <algorithm>
@@ -33,6 +31,9 @@
33#include "common.h" 31#include "common.h"
34#include "print_sha1.h" 32#include "print_sha1.h"
35#include "ui.h" 33#include "ui.h"
34#include "verifier.h"
35
36extern RecoveryUI* ui;
36 37
37static constexpr size_t MiB = 1024 * 1024; 38static constexpr size_t MiB = 1024 * 1024;
38 39