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