blob: b5bb8011ab7f73edf10cfe4ce463d249f554acac [file] [log] [blame]
Mark Salyzyna4f701a2016-03-09 14:58:16 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mark Salyzyna4f701a2016-03-09 14:58:16 -080017//
18// Strictly to deal with reboot into system after OTA after /data
19// mounts to pull the last pmsg file data and place it
20// into /data/misc/recovery/ directory, rotating it in.
21//
22// Usage: recovery-persist [--force-persist]
23//
24// On systems without /cache mount, all file content representing in the
25// recovery/ directory stored in /sys/fs/pstore/pmsg-ramoops-0 in logger
26// format that reside in the LOG_ID_SYSTEM buffer at ANDROID_LOG_INFO
27// priority or higher is transfered to the /data/misc/recovery/ directory.
28// The content is matched and rotated in as need be.
29//
30// --force-persist ignore /cache mount, always rotate in the contents.
31//
32
Mark Salyzyna4f701a2016-03-09 14:58:16 -080033#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37
Kelvin Zhang55a0bb82023-05-25 17:45:21 -070038#include <array>
Mark Salyzyna4f701a2016-03-09 14:58:16 -080039#include <string>
40
Mark Salyzyna4f701a2016-03-09 14:58:16 -080041#include <android-base/file.h>
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070042#include <android-base/logging.h>
Kelvin Zhang55a0bb82023-05-25 17:45:21 -070043#include <android-base/unique_fd.h>
Mark Salyzyna4f701a2016-03-09 14:58:16 -080044#include <private/android_logger.h> /* private pmsg functions */
45
Tao Baoe3f09a72019-10-01 11:55:36 -070046#include "recovery_utils/logging.h"
47#include "recovery_utils/parse_install_logs.h"
Tianjie Xue113e4d2016-10-21 17:46:13 -070048
Tianjie Xu2b1a4642018-09-06 11:58:55 -070049constexpr const char* LAST_LOG_FILE = "/data/misc/recovery/last_log";
50constexpr const char* LAST_PMSG_FILE = "/sys/fs/pstore/pmsg-ramoops-0";
51constexpr const char* LAST_KMSG_FILE = "/data/misc/recovery/last_kmsg";
52constexpr const char* LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops-0";
53constexpr const char* ALT_LAST_CONSOLE_FILE = "/sys/fs/pstore/console-ramoops";
Mark Salyzyna4f701a2016-03-09 14:58:16 -080054
Mark Salyzyna4f701a2016-03-09 14:58:16 -080055// close a file, log an error if the error indicator is set
56static void check_and_fclose(FILE *fp, const char *name) {
57 fflush(fp);
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -070058 if (ferror(fp)) {
59 PLOG(ERROR) << "Error in " << name;
60 }
Mark Salyzyna4f701a2016-03-09 14:58:16 -080061 fclose(fp);
62}
63
64static void copy_file(const char* source, const char* destination) {
Tianjie Xude6735e2017-07-10 15:13:33 -070065 FILE* dest_fp = fopen(destination, "we");
66 if (dest_fp == nullptr) {
67 PLOG(ERROR) << "Can't open " << destination;
68 } else {
69 FILE* source_fp = fopen(source, "re");
70 if (source_fp != nullptr) {
71 char buf[4096];
72 size_t bytes;
73 while ((bytes = fread(buf, 1, sizeof(buf), source_fp)) != 0) {
74 fwrite(buf, 1, bytes, dest_fp);
75 }
76 check_and_fclose(source_fp, source);
Mark Salyzyna4f701a2016-03-09 14:58:16 -080077 }
Tianjie Xude6735e2017-07-10 15:13:33 -070078 check_and_fclose(dest_fp, destination);
79 }
Mark Salyzyna4f701a2016-03-09 14:58:16 -080080}
81
Artem Yudin0cd72a92022-06-07 16:32:44 -070082static bool file_exists(const char* filename) {
83 return access(filename, R_OK) == 0;
84}
85
Mark Salyzyna4f701a2016-03-09 14:58:16 -080086static bool rotated = false;
87
Mark Salyzyna4f701a2016-03-09 14:58:16 -080088ssize_t logsave(
89 log_id_t /* logId */,
90 char /* prio */,
91 const char *filename,
92 const char *buf, size_t len,
93 void * /* arg */) {
94
95 std::string destination("/data/misc/");
96 destination += filename;
97
98 std::string buffer(buf, len);
99
100 {
101 std::string content;
102 android::base::ReadFileToString(destination, &content);
103
104 if (buffer.compare(content) == 0) {
105 return len;
106 }
107 }
108
109 // ToDo: Any others that match? Are we pulling in multiple
110 // already-rotated files? Algorithm thus far is KISS: one file,
111 // one rotation allowed.
112
Tianjie Xue113e4d2016-10-21 17:46:13 -0700113 rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE);
114 rotated = true;
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800115
116 return android::base::WriteStringToFile(buffer, destination.c_str());
117}
118
Kelvin Zhang55a0bb82023-05-25 17:45:21 -0700119size_t file_size(const char* path) {
120 struct stat st {};
121 if (stat(path, &st) < 0) {
122 return 0;
123 }
124
125 return st.st_size;
126}
127
128bool compare_file(const char* file1, const char* file2) {
129 if (!file_exists(file1) || !file_exists(file2)) {
130 return false;
131 }
132 if (file_size(file1) != file_size(file2)) {
133 return false;
134 }
135 std::array<uint8_t, 1024 * 16> buf1{};
136 std::array<uint8_t, 1024 * 16> buf2{};
137 android::base::unique_fd fd1(open(file1, O_RDONLY));
138 android::base::unique_fd fd2(open(file2, O_RDONLY));
139 auto bytes_remain = file_size(file1);
140 while (bytes_remain > 0) {
141 const auto bytes_to_read = std::min<size_t>(bytes_remain, buf1.size());
142
143 if (!android::base::ReadFully(fd1, buf1.data(), bytes_to_read)) {
144 LOG(ERROR) << "Failed to read from " << file1;
145 return false;
146 }
147 if (!android::base::ReadFully(fd2, buf2.data(), bytes_to_read)) {
148 LOG(ERROR) << "Failed to read from " << file2;
149 return false;
150 }
151 if (memcmp(buf1.data(), buf2.data(), bytes_to_read) != 0) {
152 return false;
153 }
154 }
155 return true;
156}
157
158void rotate_last_kmsg() {
159 if (rotated) {
160 return;
161 }
162 if (!file_exists(LAST_CONSOLE_FILE) && !file_exists(ALT_LAST_CONSOLE_FILE)) {
163 return;
164 }
165 if (!compare_file(LAST_KMSG_FILE, LAST_CONSOLE_FILE) &&
166 !compare_file(LAST_KMSG_FILE, ALT_LAST_CONSOLE_FILE)) {
167 rotate_logs(LAST_LOG_FILE, LAST_KMSG_FILE);
168 rotated = true;
169 }
170}
171
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800172int main(int argc, char **argv) {
173
174 /* Is /cache a mount?, we have been delivered where we are not wanted */
175 /*
176 * Following code halves the size of the executable as compared to:
177 *
178 * load_volume_table();
179 * has_cache = volume_for_path(CACHE_ROOT) != nullptr;
180 */
181 bool has_cache = false;
182 static const char mounts_file[] = "/proc/mounts";
Tianjie Xude6735e2017-07-10 15:13:33 -0700183 FILE* fp = fopen(mounts_file, "re");
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800184 if (!fp) {
Tianjie Xu7b0ad9c2016-08-05 18:00:04 -0700185 PLOG(ERROR) << "failed to open " << mounts_file;
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800186 } else {
187 char *line = NULL;
188 size_t len = 0;
Kelvin Zhang55a0bb82023-05-25 17:45:21 -0700189 ssize_t read{};
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800190 while ((read = getline(&line, &len, fp)) != -1) {
191 if (strstr(line, " /cache ")) {
192 has_cache = true;
193 break;
194 }
195 }
196 free(line);
197 fclose(fp);
198 }
199
200 if (has_cache) {
Tianjie Xu2b1a4642018-09-06 11:58:55 -0700201 // Collects and reports the non-a/b update metrics from last_install; and removes the file
202 // to avoid duplicate report.
Artem Yudin0cd72a92022-06-07 16:32:44 -0700203 if (file_exists(LAST_INSTALL_FILE_IN_CACHE) && unlink(LAST_INSTALL_FILE_IN_CACHE) == -1) {
Tianjie Xu2b1a4642018-09-06 11:58:55 -0700204 PLOG(ERROR) << "Failed to unlink " << LAST_INSTALL_FILE_IN_CACHE;
205 }
206
207 // TBD: Future location to move content from /cache/recovery to /data/misc/recovery/
208 // if --force-persist flag, then transfer pmsg data anyways
209 if ((argc <= 1) || !argv[1] || strcmp(argv[1], "--force-persist")) {
210 return 0;
211 }
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800212 }
213
Artem Yudin0cd72a92022-06-07 16:32:44 -0700214 /* Is there something in pmsg? If not, no need to proceed. */
215 if (!file_exists(LAST_PMSG_FILE)) {
216 return 0;
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800217 }
218
219 // Take last pmsg file contents and send it off to the logsave
220 __android_log_pmsg_file_read(
221 LOG_ID_SYSTEM, ANDROID_LOG_INFO, "recovery/", logsave, NULL);
222
Tianjie Xu2b1a4642018-09-06 11:58:55 -0700223 // For those device without /cache, the last_install file has been copied to
224 // /data/misc/recovery from pmsg. Looks for the sideload history only.
225 if (!has_cache) {
Artem Yudin0cd72a92022-06-07 16:32:44 -0700226 if (file_exists(LAST_INSTALL_FILE) && unlink(LAST_INSTALL_FILE) == -1) {
Tianjie Xu2b1a4642018-09-06 11:58:55 -0700227 PLOG(ERROR) << "Failed to unlink " << LAST_INSTALL_FILE;
228 }
229 }
Kelvin Zhang55a0bb82023-05-25 17:45:21 -0700230 rotate_last_kmsg();
Tianjie Xu2b1a4642018-09-06 11:58:55 -0700231
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800232 /* Is there a last console log too? */
Mark Salyzyn5f7111f2016-04-04 16:19:26 -0700233 if (rotated) {
Artem Yudin0cd72a92022-06-07 16:32:44 -0700234 if (file_exists(LAST_CONSOLE_FILE)) {
235 copy_file(LAST_CONSOLE_FILE, LAST_KMSG_FILE);
236 } else if (file_exists(ALT_LAST_CONSOLE_FILE)) {
237 copy_file(ALT_LAST_CONSOLE_FILE, LAST_KMSG_FILE);
238 }
Mark Salyzyna4f701a2016-03-09 14:58:16 -0800239 }
240
241 return 0;
242}