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 } // Updater
108