1 /*
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 //
18 // Strictly to deal with reboot into system after OTA, then
19 // reboot while in system before boot complete landing us back
20 // into recovery to continue with any mitigations with retained
21 // log history. This simply refreshes the pmsg files from
22 // the last pmsg file contents.
23 //
24 // Usage:
25 //    recovery-refresh [--force-rotate|--rotate]
26 //
27 //    All file content representing in the recovery/ directory stored in
28 //    /sys/fs/pstore/pmsg-ramoops-0 in logger format that reside in the
29 //    LOG_ID_SYSTEM buffer at ANDROID_LOG_INFO priority or higher is
30 //    refreshed into /dev/pmsg0. This ensures that an unexpected reboot
31 //    before recovery-persist is run will still contain the associated
32 //    pmsg Android Logger content.
33 //
34 //    --force-rotate  recovery/last_kmsg and recovery.last_log files are
35 //                    rotated with .<number> suffixes upwards.
36 //    --rotate        rotated only if rocovery/last_msg or recovery/last_log
37 //                    exist, otherwise perform 1:1 refresh.
38 //
39 
40 #include <string.h>
41 
42 #include <string>
43 
44 #include <private/android_logger.h> /* private pmsg functions */
45 
46 #include "recovery_utils/logging.h"
47 
main(int argc,char ** argv)48 int main(int argc, char **argv) {
49     static const char filter[] = "recovery/";
50     static const char force_rotate_flag[] = "--force-rotate";
51     static const char rotate_flag[] = "--rotate";
52     ssize_t ret;
53     bool doRotate = false;
54     // Take last pmsg contents and rewrite it to the current pmsg session.
55     if ((argc <= 1) || !argv[1] ||
56             (((doRotate = strcmp(argv[1], rotate_flag))) &&
57                 strcmp(argv[1], force_rotate_flag))) {
58         doRotate = false;
59     } else if (!doRotate) {
60         // Do we need to rotate?
61         __android_log_pmsg_file_read(
62             LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter,
63             logbasename, &doRotate);
64     }
65 
66     // Take action to refresh pmsg contents
67     ret = __android_log_pmsg_file_read(
68         LOG_ID_SYSTEM, ANDROID_LOG_INFO, filter,
69         logrotate, &doRotate);
70 
71     return (ret < 0) ? ret : 0;
72 }
73