1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "misc_info/misc_info.h"
17 #include <cstring>
18 #include "fs_manager/mount.h"
19 #include "log/log.h"
20 #include "securec.h"
21 #include "updater/updater_const.h"
22
23 namespace Updater {
WriteUpdaterMessage(const std::string & path,const UpdateMessage & boot)24 bool WriteUpdaterMessage(const std::string &path, const UpdateMessage &boot)
25 {
26 char *realPath = realpath(path.c_str(), NULL);
27 if (realPath == nullptr) {
28 LOG(ERROR) << "realPath is NULL" << " : " << strerror(errno);
29 return false;
30 }
31 FILE* fp = fopen(realPath, "rb+");
32 free(realPath);
33 if (fp == nullptr) {
34 LOG(ERROR) << "WriteUpdaterMessage fopen failed" << " : " << strerror(errno);
35 return false;
36 }
37
38 size_t ret = fwrite(&boot, sizeof(UpdateMessage), 1, fp);
39 if (ret != 1) {
40 LOG(ERROR) << "WriteUpdaterMessage fwrite failed" << " : " << strerror(errno);
41 fclose(fp);
42 return false;
43 }
44
45 int res = fclose(fp);
46 if (res != 0) {
47 LOG(ERROR) << "WriteUpdaterMessage fclose failed" << " : " << strerror(errno);
48 return false;
49 }
50 return true;
51 }
52
ReadUpdaterMessage(const std::string & path,UpdateMessage & boot)53 bool ReadUpdaterMessage(const std::string &path, UpdateMessage &boot)
54 {
55 char *realPath = realpath(path.c_str(), NULL);
56 if (realPath == nullptr) {
57 LOG(ERROR) << "realPath is NULL" << " : " << strerror(errno);
58 return false;
59 }
60 FILE* fp = fopen(realPath, "rb");
61 free(realPath);
62 if (fp == nullptr) {
63 LOG(ERROR) << "ReadUpdaterMessage fopen failed" << " : " << strerror(errno);
64 return false;
65 }
66
67 struct UpdateMessage tempBoot {};
68 size_t ret = fread(&tempBoot, sizeof(UpdateMessage), 1, fp);
69 if (ret != 1) {
70 LOG(ERROR) << "ReadUpdaterMessage fwrite failed" << " : " << strerror(errno);
71 fclose(fp);
72 return false;
73 }
74
75 int res = fclose(fp);
76 if (res != 0) {
77 LOG(ERROR) << "ReadUpdaterMessage fclose failed" << " : " << strerror(errno);
78 return false;
79 }
80 if (memcpy_s(&boot, sizeof(UpdateMessage), &tempBoot, sizeof(UpdateMessage)) != EOK) {
81 LOG(ERROR) << "ReadUpdaterMessage memcpy failed" << " : " << strerror(errno);
82 return false;
83 }
84 return true;
85 }
86
WriteUpdaterMiscMsg(const UpdateMessage & boot)87 bool WriteUpdaterMiscMsg(const UpdateMessage &boot)
88 {
89 auto path = GetBlockDeviceByMountPoint(MISC_PATH);
90 if (path.empty()) {
91 LOG(INFO) << "cannot get block device of partition";
92 path = MISC_FILE;
93 }
94 LOG(INFO) << "WriteUpdaterMiscMsg::misc path : " << path;
95 return WriteUpdaterMessage(path, boot);
96 }
97
ReadUpdaterMiscMsg(UpdateMessage & boot)98 bool ReadUpdaterMiscMsg(UpdateMessage &boot)
99 {
100 auto path = GetBlockDeviceByMountPoint(MISC_PATH);
101 if (path.empty()) {
102 LOG(INFO) << "cannot get block device of partition";
103 path = MISC_FILE;
104 }
105 return ReadUpdaterMessage(path, boot);
106 }
107
WriteUpdaterParaMisc(const UpdaterPara & para)108 bool WriteUpdaterParaMisc(const UpdaterPara ¶)
109 {
110 auto path = GetBlockDeviceByMountPoint(MISC_PATH);
111 if (path.empty()) {
112 LOG(INFO) << "WriteUpdaterParaMisc cannot get block device of partition";
113 path = MISC_FILE;
114 }
115
116 char *realPath = realpath(path.c_str(), NULL);
117 if (realPath == nullptr) {
118 LOG(ERROR) << "WriteUpdaterParaMisc realPath is NULL" << ":" << strerror(errno);
119 return false;
120 }
121 FILE *fp = fopen(realPath, "rb+");
122 free(realPath);
123 if (fp == nullptr) {
124 LOG(ERROR) << "WriteUpdaterParaMisc fopen failed" << ":" << strerror(errno);
125 return false;
126 }
127
128 if (lseek(fileno(fp), MISC_UPDATER_PARA_OFFSET, SEEK_SET) == -1) {
129 LOG(ERROR) << "lseek fp failed";
130 fclose(fp);
131 return false;
132 }
133 size_t ret = fwrite(¶, sizeof(UpdaterPara), 1, fp);
134 if (ret != 1) {
135 LOG(ERROR) << "WriteUpdaterParaMisc fwrite failed" << " : " << strerror(errno);
136 fclose(fp);
137 return false;
138 }
139
140 fclose(fp);
141 return true;
142 }
143
ReadUpdaterParaMisc(UpdaterPara & para)144 bool ReadUpdaterParaMisc(UpdaterPara ¶)
145 {
146 auto path = GetBlockDeviceByMountPoint(MISC_PATH);
147 if (path.empty()) {
148 LOG(INFO) << "ReadUpdaterParaMisc cannot get block device of partition";
149 path = MISC_FILE;
150 }
151
152 char *realPath = realpath(path.c_str(), NULL);
153 if (realPath == nullptr) {
154 LOG(ERROR) << "ReadUpdaterParaMisc realPath is NULL" << ":" << strerror(errno);
155 return false;
156 }
157 FILE *fp = fopen(realPath, "rb");
158 free(realPath);
159 if (fp == nullptr) {
160 LOG(ERROR) << "ReadUpdaterParaMisc fopen failed" << ":" << strerror(errno);
161 return false;
162 }
163
164 if (lseek(fileno(fp), MISC_UPDATER_PARA_OFFSET, SEEK_SET) == -1) {
165 LOG(ERROR) << "lseek fp failed";
166 fclose(fp);
167 return false;
168 }
169 size_t ret = fread(¶, sizeof(UpdaterPara), 1, fp);
170 if (ret != 1) {
171 LOG(ERROR) << "ReadUpdaterParaMisc fwrite failed" << " : " << strerror(errno);
172 fclose(fp);
173 return false;
174 }
175
176 fclose(fp);
177 return true;
178 }
179
ClearUpdaterParaMisc(void)180 void ClearUpdaterParaMisc(void)
181 {
182 struct UpdaterPara cleanPara {};
183 if (!WriteUpdaterParaMisc(cleanPara)) {
184 LOG(ERROR) << "Clear para including language of misc failed";
185 }
186 }
187 } // Updater
188