xref: /aosp_15_r20/bootable/recovery/recovery-refresh.cpp (revision e7c364b630b241adcb6c7726a21055250b91fdac)
1*e7c364b6SAndroid Build Coastguard Worker /*
2*e7c364b6SAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*e7c364b6SAndroid Build Coastguard Worker  *
4*e7c364b6SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*e7c364b6SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*e7c364b6SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*e7c364b6SAndroid Build Coastguard Worker  *
8*e7c364b6SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*e7c364b6SAndroid Build Coastguard Worker  *
10*e7c364b6SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*e7c364b6SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*e7c364b6SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e7c364b6SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*e7c364b6SAndroid Build Coastguard Worker  * limitations under the License.
15*e7c364b6SAndroid Build Coastguard Worker  */
16*e7c364b6SAndroid Build Coastguard Worker 
17*e7c364b6SAndroid Build Coastguard Worker //
18*e7c364b6SAndroid Build Coastguard Worker // Strictly to deal with reboot into system after OTA, then
19*e7c364b6SAndroid Build Coastguard Worker // reboot while in system before boot complete landing us back
20*e7c364b6SAndroid Build Coastguard Worker // into recovery to continue with any mitigations with retained
21*e7c364b6SAndroid Build Coastguard Worker // log history. This simply refreshes the pmsg files from
22*e7c364b6SAndroid Build Coastguard Worker // the last pmsg file contents.
23*e7c364b6SAndroid Build Coastguard Worker //
24*e7c364b6SAndroid Build Coastguard Worker // Usage:
25*e7c364b6SAndroid Build Coastguard Worker //    recovery-refresh [--force-rotate|--rotate]
26*e7c364b6SAndroid Build Coastguard Worker //
27*e7c364b6SAndroid Build Coastguard Worker //    All file content representing in the recovery/ directory stored in
28*e7c364b6SAndroid Build Coastguard Worker //    /sys/fs/pstore/pmsg-ramoops-0 in logger format that reside in the
29*e7c364b6SAndroid Build Coastguard Worker //    LOG_ID_SYSTEM buffer at ANDROID_LOG_INFO priority or higher is
30*e7c364b6SAndroid Build Coastguard Worker //    refreshed into /dev/pmsg0. This ensures that an unexpected reboot
31*e7c364b6SAndroid Build Coastguard Worker //    before recovery-persist is run will still contain the associated
32*e7c364b6SAndroid Build Coastguard Worker //    pmsg Android Logger content.
33*e7c364b6SAndroid Build Coastguard Worker //
34*e7c364b6SAndroid Build Coastguard Worker //    --force-rotate  recovery/last_kmsg and recovery.last_log files are
35*e7c364b6SAndroid Build Coastguard Worker //                    rotated with .<number> suffixes upwards.
36*e7c364b6SAndroid Build Coastguard Worker //    --rotate        rotated only if rocovery/last_msg or recovery/last_log
37*e7c364b6SAndroid Build Coastguard Worker //                    exist, otherwise perform 1:1 refresh.
38*e7c364b6SAndroid Build Coastguard Worker //
39*e7c364b6SAndroid Build Coastguard Worker 
40*e7c364b6SAndroid Build Coastguard Worker #include <string.h>
41*e7c364b6SAndroid Build Coastguard Worker 
42*e7c364b6SAndroid Build Coastguard Worker #include <string>
43*e7c364b6SAndroid Build Coastguard Worker 
44*e7c364b6SAndroid Build Coastguard Worker #include <private/android_logger.h> /* private pmsg functions */
45*e7c364b6SAndroid Build Coastguard Worker 
46*e7c364b6SAndroid Build Coastguard Worker #include "recovery_utils/logging.h"
47*e7c364b6SAndroid Build Coastguard Worker 
main(int argc,char ** argv)48*e7c364b6SAndroid Build Coastguard Worker int main(int argc, char **argv) {
49*e7c364b6SAndroid Build Coastguard Worker     static const char filter[] = "recovery/";
50*e7c364b6SAndroid Build Coastguard Worker     static const char force_rotate_flag[] = "--force-rotate";
51*e7c364b6SAndroid Build Coastguard Worker     static const char rotate_flag[] = "--rotate";
52*e7c364b6SAndroid Build Coastguard Worker     ssize_t ret;
53*e7c364b6SAndroid Build Coastguard Worker     bool doRotate = false;
54*e7c364b6SAndroid Build Coastguard Worker     // Take last pmsg contents and rewrite it to the current pmsg session.
55*e7c364b6SAndroid Build Coastguard Worker     if ((argc <= 1) || !argv[1] ||
56*e7c364b6SAndroid Build Coastguard Worker             (((doRotate = strcmp(argv[1], rotate_flag))) &&
57*e7c364b6SAndroid Build Coastguard Worker                 strcmp(argv[1], force_rotate_flag))) {
58*e7c364b6SAndroid Build Coastguard Worker         doRotate = false;
59*e7c364b6SAndroid Build Coastguard Worker     } else if (!doRotate) {
60*e7c364b6SAndroid Build Coastguard Worker         // Do we need to rotate?
61*e7c364b6SAndroid Build Coastguard Worker         __android_log_pmsg_file_read(
62*e7c364b6SAndroid Build Coastguard Worker             LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter,
63*e7c364b6SAndroid Build Coastguard Worker             logbasename, &doRotate);
64*e7c364b6SAndroid Build Coastguard Worker     }
65*e7c364b6SAndroid Build Coastguard Worker 
66*e7c364b6SAndroid Build Coastguard Worker     // Take action to refresh pmsg contents
67*e7c364b6SAndroid Build Coastguard Worker     ret = __android_log_pmsg_file_read(
68*e7c364b6SAndroid Build Coastguard Worker         LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter,
69*e7c364b6SAndroid Build Coastguard Worker         logrotate, &doRotate);
70*e7c364b6SAndroid Build Coastguard Worker 
71*e7c364b6SAndroid Build Coastguard Worker     return (ret < 0) ? ret : 0;
72*e7c364b6SAndroid Build Coastguard Worker }
73