xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include "libcpnv.h" |
| 5 | #include "flags_api.h" |
| 6 | |
xf.li | e31de8b | 2023-12-26 23:38:58 -0800 | [diff] [blame] | 7 | static 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 | |
| 35 | static 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.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 51 | int main(int argc, char *argv[]) |
| 52 | { |
| 53 | unsigned int nvro_flag; |
| 54 | int ret; |
| 55 | |
| 56 | if (argc == 1) |
| 57 | { |
xf.li | e31de8b | 2023-12-26 23:38:58 -0800 | [diff] [blame] | 58 | 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.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 63 | return -1; |
| 64 | } |
xf.li | e31de8b | 2023-12-26 23:38:58 -0800 | [diff] [blame] | 65 | if (argc > 1 && (strcmp("backup", argv[1]) == 0)) |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 66 | { |
| 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.li | e31de8b | 2023-12-26 23:38:58 -0800 | [diff] [blame] | 78 | if (argc > 1 && (strcmp("restore", argv[1]) == 0)) |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 79 | { |
| 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.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 91 | } |
xf.li | e31de8b | 2023-12-26 23:38:58 -0800 | [diff] [blame] | 92 | if (argc > 1 && (strcmp("backing_up", argv[1]) == 0)) |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 93 | { |
xf.li | e31de8b | 2023-12-26 23:38:58 -0800 | [diff] [blame] | 94 | ret = flags_set_nvroflag(NVRO_BACKING_UP); |
| 95 | if (ret != 0) |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 96 | { |
xf.li | e31de8b | 2023-12-26 23:38:58 -0800 | [diff] [blame] | 97 | printf("[error]nvro_tool set NVRO_BACKING_UP\n"); |
| 98 | return -1; |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 99 | } |
| 100 | else |
| 101 | { |
xf.li | e31de8b | 2023-12-26 23:38:58 -0800 | [diff] [blame] | 102 | printf("nvro_tool set NVRO_BACKING_UP success\n"); |
| 103 | return 0; |
xf.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
xf.li | e31de8b | 2023-12-26 23:38:58 -0800 | [diff] [blame] | 106 | 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.li | 9d1a0e1 | 2023-09-20 01:43:20 -0700 | [diff] [blame] | 144 | { |
| 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.li | e31de8b | 2023-12-26 23:38:58 -0800 | [diff] [blame] | 154 | case NVRO_BACKING_UP: |
| 155 | printf("nvro_flag back up next reboot:%08x\n", nvro_flag); |
| 156 | break; |
xf.li | 9d1a0e1 | 2023-09-20 01:43:20 -0700 | [diff] [blame] | 157 | 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.li | aa4d92f | 2023-09-13 00:18:58 -0700 | [diff] [blame] | 166 | |
| 167 | return -1; |
| 168 | } |
| 169 | |