blob: b2ab52f7ebec9b27aedb14ea25230a038d3274b6 [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
17#define LOG_TAG "recovery-refresh"
18
19//
20// Strictly to deal with reboot into system after OTA, then
21// reboot while in system before boot complete landing us back
22// into recovery to continue with any mitigations with retained
23// log history. This simply refreshes the pmsg files from
24// the last pmsg file contents.
25//
26// Usage:
27// recovery-refresh [--force-rotate|--rotate]
28//
29// All file content representing in the recovery/ directory stored in
30// /sys/fs/pstore/pmsg-ramoops-0 in logger format that reside in the
31// LOG_ID_SYSTEM buffer at ANDROID_LOG_INFO priority or higher is
32// refreshed into /dev/pmsg0. This ensures that an unexpected reboot
33// before recovery-persist is run will still contain the associated
34// pmsg Android Logger content.
35//
36// --force-rotate recovery/last_kmsg and recovery.last_log files are
37// rotated with .<number> suffixes upwards.
38// --rotate rotated only if rocovery/last_msg or recovery/last_log
39// exist, otherwise perform 1:1 refresh.
40//
41
42#include <string.h>
43
44#include <string>
45
46#include <android/log.h> /* Android Log Priority Tags */
Mark Salyzyna4f701a2016-03-09 14:58:16 -080047#include <private/android_logger.h> /* private pmsg functions */
48
49static const char LAST_KMSG_FILE[] = "recovery/last_kmsg";
50static const char LAST_LOG_FILE[] = "recovery/last_log";
51
52static ssize_t logbasename(
53 log_id_t /* logId */,
54 char /* prio */,
55 const char *filename,
56 const char * /* buf */, size_t len,
57 void *arg) {
58 if (strstr(LAST_KMSG_FILE, filename) ||
59 strstr(LAST_LOG_FILE, filename)) {
60 bool *doRotate = reinterpret_cast<bool *>(arg);
61 *doRotate = true;
62 }
63 return len;
64}
65
66static ssize_t logrotate(
67 log_id_t logId,
68 char prio,
69 const char *filename,
70 const char *buf, size_t len,
71 void *arg) {
72 bool *doRotate = reinterpret_cast<bool *>(arg);
73 if (!*doRotate) {
74 return __android_log_pmsg_file_write(logId, prio, filename, buf, len);
75 }
76
77 std::string name(filename);
Chih-Hung Hsieh23abfd32016-07-27 10:19:47 -070078 size_t dot = name.find_last_of('.');
Mark Salyzyna4f701a2016-03-09 14:58:16 -080079 std::string sub = name.substr(0, dot);
80
81 if (!strstr(LAST_KMSG_FILE, sub.c_str()) &&
82 !strstr(LAST_LOG_FILE, sub.c_str())) {
83 return __android_log_pmsg_file_write(logId, prio, filename, buf, len);
84 }
85
86 // filename rotation
87 if (dot == std::string::npos) {
88 name += ".1";
89 } else {
90 std::string number = name.substr(dot + 1);
91 if (!isdigit(number.data()[0])) {
92 name += ".1";
93 } else {
Chih-Hung Hsieh54a27472016-04-18 11:30:55 -070094 auto i = std::stoull(number);
Mark Salyzyna4f701a2016-03-09 14:58:16 -080095 name = sub + "." + std::to_string(i + 1);
96 }
97 }
98
99 return __android_log_pmsg_file_write(logId, prio, name.c_str(), buf, len);
100}
101
102int main(int argc, char **argv) {
103 static const char filter[] = "recovery/";
104 static const char force_rotate_flag[] = "--force-rotate";
105 static const char rotate_flag[] = "--rotate";
106 ssize_t ret;
107 bool doRotate = false;
108
109 // Take last pmsg contents and rewrite it to the current pmsg session.
110 if ((argc <= 1) || !argv[1] ||
111 (((doRotate = strcmp(argv[1], rotate_flag))) &&
112 strcmp(argv[1], force_rotate_flag))) {
113 doRotate = false;
114 } else if (!doRotate) {
115 // Do we need to rotate?
116 __android_log_pmsg_file_read(
117 LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter,
118 logbasename, &doRotate);
119 }
120
121 // Take action to refresh pmsg contents
122 ret = __android_log_pmsg_file_read(
123 LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter,
124 logrotate, &doRotate);
125
126 return (ret < 0) ? ret : 0;
127}