blob: fc28661375ec7444db1b2b85fa7099813ba98186 [file] [log] [blame]
xf.liaa4d92f2023-09-13 00:18:58 -07001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include "libcpnv.h"
5#include "flags_api.h"
6
xf.lie31de8b2023-12-26 23:38:58 -08007static int nvro_restoring(int nvro_flag)
8{
9 int ret = -1;
10 if (nvro_flag != NVRO_RESTORING)
11 {
12 ret = flags_set_nvroflag(NVRO_RESTORING);
13 if (ret != 0)
14 {
15 printf("[error]nvro_tool set NVRO_RESTORING\n");
16 return -1;
17 }
18 else
19 {
20 printf("nvro_tool set NVRO_RESTORING success\n");
21 }
22 }
23 if (cpnv_NvroRestore() == CPNV_OK)
24 {
25 printf("cpnv_NvroRestore success\n");
26 return 0;
27 }
28 else
29 {
30 printf("cpnv_NvroRestore fail\n");
31 return -1;
32 }
33}
34
35static int nvro_check(int flag)
36{
37 int ret = -1;
38
39 if (cpnv_nvro_check(flag) == CPNV_OK)
40 { // nvro hash check ok
41 printf("nvro_tool nvro hash check ok\n");
42 ret = 0;
43 }
44 else
45 { // nvro hash check fail and try restore
46 ret = nvro_restoring(NVRO_BACKED_UP);
47 }
48 return ret;
49}
50
xf.liaa4d92f2023-09-13 00:18:58 -070051int main(int argc, char *argv[])
52{
53 unsigned int nvro_flag;
54 int ret;
55
56 if (argc == 1)
57 {
xf.lie31de8b2023-12-26 23:38:58 -080058 printf("%s backup backup now\n", argv[0]);
59 printf("%s restore restore next boot\n", argv[0]);
60 printf("%s backing_up backup next boot\n", argv[0]);
61 printf("%s check check now\n", argv[0]);
62 printf("%s getflag show nvro_flag\n", argv[0]);
xf.liaa4d92f2023-09-13 00:18:58 -070063 return -1;
64 }
xf.lie31de8b2023-12-26 23:38:58 -080065 if (argc > 1 && (strcmp("backup", argv[1]) == 0))
xf.liaa4d92f2023-09-13 00:18:58 -070066 {
67 if (cpnv_NvroBackup() == CPNV_OK)
68 {
69 printf("cpnv_NvroBackup success\n");
70 return 0;
71 }
72 else
73 {
74 printf("cpnv_NvroBackup fail\n");
75 return -1;
76 }
77 }
xf.lie31de8b2023-12-26 23:38:58 -080078 if (argc > 1 && (strcmp("restore", argv[1]) == 0))
xf.liaa4d92f2023-09-13 00:18:58 -070079 {
80 ret = flags_set_nvroflag(NVRO_RESTORING);
81 if (ret != 0)
82 {
83 printf("[error]nvro_tool set NVRO_RESTORING\n");
84 return -1;
85 }
86 else
87 {
88 printf("nvro_tool set NVRO_RESTORING success\n");
89 return 0;
90 }
xf.liaa4d92f2023-09-13 00:18:58 -070091 }
xf.lie31de8b2023-12-26 23:38:58 -080092 if (argc > 1 && (strcmp("backing_up", argv[1]) == 0))
xf.liaa4d92f2023-09-13 00:18:58 -070093 {
xf.lie31de8b2023-12-26 23:38:58 -080094 ret = flags_set_nvroflag(NVRO_BACKING_UP);
95 if (ret != 0)
xf.liaa4d92f2023-09-13 00:18:58 -070096 {
xf.lie31de8b2023-12-26 23:38:58 -080097 printf("[error]nvro_tool set NVRO_BACKING_UP\n");
98 return -1;
xf.liaa4d92f2023-09-13 00:18:58 -070099 }
100 else
101 {
xf.lie31de8b2023-12-26 23:38:58 -0800102 printf("nvro_tool set NVRO_BACKING_UP success\n");
103 return 0;
xf.liaa4d92f2023-09-13 00:18:58 -0700104 }
105 }
xf.lie31de8b2023-12-26 23:38:58 -0800106 if (argc > 1 && (strcmp("check", argv[1]) == 0))
107 {
108 nvro_flag = flags_get_nvroflag();
109
110 switch (nvro_flag)
111 {
112 case NVRO_RESTORING:
113 ret = nvro_restoring(nvro_flag);
114 break;
115 case NVRO_BACKING_UP:
116 if (cpnv_nvro_check(1) != CPNV_OK)
117 { /* nvro check fail and restoring */
118 ret = nvro_restoring(nvro_flag);
119 }
120 else
121 { /* nvro check ok */
122 if (cpnv_NvroBackup() == CPNV_OK)
123 {
124 printf("cpnv_NvroBackup success\n");
125 ret = 0;
126 }
127 else
128 {
129 printf("cpnv_NvroBackup fail\n");
130 ret = -1;
131 }
132 }
133 break;
134 case NVRO_BACKED_UP:
135 ret = nvro_check(2);
136 break;
137 default:
138 ret = -1;
139 break;
140 }
141 return ret;
142 }
143 if (argc > 1 && (strcmp("getflag", argv[1]) == 0))
xf.li9d1a0e12023-09-20 01:43:20 -0700144 {
145 nvro_flag = flags_get_nvroflag();
146 switch (nvro_flag)
147 {
148 case NVRO_INIT:
149 printf("nvro_flag init:%08x\n", nvro_flag);
150 break;
151 case NVRO_BACKED_UP:
152 printf("nvro_flag backed up:%08x\n", nvro_flag);
153 break;
xf.lie31de8b2023-12-26 23:38:58 -0800154 case NVRO_BACKING_UP:
155 printf("nvro_flag back up next reboot:%08x\n", nvro_flag);
156 break;
xf.li9d1a0e12023-09-20 01:43:20 -0700157 case NVRO_RESTORING:
158 printf("nvro_flag restoring:%08x\n", nvro_flag);
159 break;
160 default:
161 printf("nvro_flag invalid:%08x\n", nvro_flag);
162 break;
163 }
164 return 0;
165 }
xf.liaa4d92f2023-09-13 00:18:58 -0700166
167 return -1;
168}
169