1 /*
2 * Copyright (c) 2022 Hunan OpenValley Digital Industry Development 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 <stdint.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include "hal_hota_board.h"
20 #include "hota_partition.h"
21
22 #define NOT_SUPPORT 4096
23 #define PATH_SEPARATE_LEN 2
24
25 typedef struct {
26 uint8_t index;
27 uint8_t init;
28 UpdateMetaData metaData;
29 ComponentTableInfo componentTableInfo;
30 uint8_t pubKey[16];
31 } UpdateInfo_t;
32
33 static UpdateInfo_t UpdateInfo = {0};
34 /**
35 * @brief OTA module initialization.
36 *
37 * @return OHOS_SUCCESS: Success,
38 * Others: Failure.
39 */
HotaHalInit(void)40 int HotaHalInit(void)
41 {
42 if (UpdateInfo.init != 0) {
43 return OHOS_FAILURE;
44 }
45 UpdateInfo.init = 1;
46 memset_s(&UpdateInfo, sizeof(UpdateInfo), 0, sizeof(UpdateInfo));
47 UpdateInfo.componentTableInfo.componentName = "OpenValley";
48 UpdateInfo.componentTableInfo.imgPath = "/";
49 return OHOS_SUCCESS;
50 }
51
52 /**
53 * @brief Release OTA module resource.
54 *
55 * @return OHOS_SUCCESS: Success,
56 * Others: Failure.
57 */
HotaHalDeInit(void)58 int HotaHalDeInit(void)
59 {
60 if (UpdateInfo.init == 0) {
61 return OHOS_FAILURE;
62 }
63 UpdateInfo.init = 0;
64 return OHOS_SUCCESS;
65 }
66
67 /**
68 * @brief Release OTA module resource.
69 *
70 * @return OHOS_SUCCESS: Success,
71 * Others: Failure.
72 */
HotaHalGetUpdateIndex(unsigned int * index)73 int HotaHalGetUpdateIndex(unsigned int *index)
74 {
75 if (index == NULL) {
76 return OHOS_FAILURE;
77 }
78 *index = UpdateInfo.index;
79 return OHOS_SUCCESS;
80 }
81
82 /**
83 * @brief Write image to partition.
84 *
85 * @param partition [in] scan result, result array size must larger than WIFI_SCAN_AP_LIMIT.
86 * @param buffer [in] image buffer.
87 * @param offset [in] The buffer offset of file.
88 * @param bufLen [in] The Length of buffer.
89 *
90 * @return OHOS_SUCCESS: Success,
91 * Others: Failure.
92 */
HotaHalWrite(int partition,unsigned char * buffer,unsigned int offset,unsigned int bufLen)93 int HotaHalWrite(int partition, unsigned char *buffer, unsigned int offset, unsigned int bufLen)
94 {
95 if ((buffer == NULL) || (offset == 0) || (bufLen == 0)) {
96 return OHOS_FAILURE;
97 }
98 return OHOS_SUCCESS;
99 }
100
101 /**
102 * @brief read image of partition.
103 *
104 * @param partition [in] scan result, result array size must larger than WIFI_SCAN_AP_LIMIT.
105 * @param offset [in] The buffer offset of file.
106 * @param bufLen [in] The Length of buffer.
107 * @param buffer [out] image buffer.
108 *
109 * @return OHOS_SUCCESS: Success,
110 * Others: Failure.
111 */
HotaHalRead(int partition,unsigned int offset,unsigned int bufLen,unsigned char * buffer)112 int HotaHalRead(int partition, unsigned int offset, unsigned int bufLen, unsigned char *buffer)
113 {
114 if ((buffer == NULL) || (offset == 0) || (bufLen == 0)) {
115 return OHOS_FAILURE;
116 }
117 return OHOS_SUCCESS;
118 }
119
120 /**
121 * @brief Write Boot Settings in order to notify device upgrade success or enter Recovery Part.
122 *
123 * @return OHOS_SUCCESS: Success,
124 * Others: Failure.
125 */
HotaHalSetBootSettings(void)126 int HotaHalSetBootSettings(void)
127 {
128 UpdateInfo.index ^= 1;
129 return OHOS_SUCCESS;
130 }
131
132 /**
133 * @brief Restart after upgrade finish or go bootloader to upgrade.
134 *
135 * @return OHOS_SUCCESS: Success,
136 * Others: Failure.
137 */
HotaHalRestart(void)138 int HotaHalRestart(void)
139 {
140 void panic_restart(void);
141 panic_restart();
142 return OHOS_SUCCESS;
143 }
144
145 /**
146 * @brief Get partition info.
147 *
148 * You need to call this funtion in Init function, you need partition info when upgrade. \n
149 *
150 * @return Returns <b>0</b> if the operation is successful; returns <b>-1</b> otherwise.
151 *
152 * @since 1.0
153 * @version 1.0
154 */
155 const ComponentTableInfo *HotaHalGetPartitionInfo();
156
157 /**
158 * @brief Get public key.
159 *
160 * You need to call this funtion when verfiy sign data \n
161 *
162 * @param length Indicates pubkey len.
163 *
164 * @return Returns <b>0</b> if the operation is successful; public key.
165 *
166 * @since 1.0
167 * @version 1.0
168 */
HotaHalGetPubKey(unsigned int * length)169 unsigned char *HotaHalGetPubKey(unsigned int *length)
170 {
171 if (length == NULL) {
172 return NULL;
173 }
174 *length = sizeof(UpdateInfo.pubKey);
175 return UpdateInfo.pubKey;
176 }
177
178 /**
179 * @brief get update ability.
180 *
181 * You need to call this function when update process init. \n
182 *
183 * @return Returns update abilty.
184 *
185 * @since 1.0
186 * @version 1.0
187 */
HotaHalGetUpdateAbility(void)188 int HotaHalGetUpdateAbility(void)
189 {
190 return NOT_SUPPORT;
191 }
192
193 /**
194 * @brief get ota package update path.
195 *
196 * You need to call this function before update process. \n
197 *
198 * @param path Indicates where ota package you place.
199 * @param len Indicates path len.
200 *
201 * @return Returns <b>0</b> if the operation is successful; returns <b>-1</b> otherwise.
202 *
203 * @since 1.0
204 * @version 1.0
205 */
HotaHalGetOtaPkgPath(char * path,int len)206 int HotaHalGetOtaPkgPath(char *path, int len)
207 {
208 int ret;
209 if ((path == NULL) || (len == 0)) {
210 return OHOS_FAILURE;
211 }
212 ret = memcpy_s(path, len, "/", PATH_SEPARATE_LEN);
213 if (ret != 0) {
214 return OHOS_FAILURE;
215 }
216 return OHOS_SUCCESS;
217 }
218
219 /**
220 * @brief get update metadata.
221 *
222 * You need to call this function when update process .\n
223 *
224 * @return Returns OtaStatus if the operation is successful; returns <b>-1</b> otherwise.
225 *
226 * @since 1.0
227 * @version 1.0
228 */
HotaHalGetMetaData(UpdateMetaData * metaData)229 int HotaHalGetMetaData(UpdateMetaData *metaData)
230 {
231 int ret;
232 if (metaData == NULL) {
233 return OHOS_FAILURE;
234 }
235 ret = memcpy_s(metaData, sizeof(UpdateMetaData), &UpdateInfo.metaData, sizeof(UpdateMetaData));
236 if (ret != 0) {
237 return OHOS_FAILURE;
238 }
239 return OHOS_SUCCESS;
240 }
241
242 /**
243 * @brief set update metadata.
244 *
245 * You need to call this function when update process.\n
246 *
247 * @return Returns <b>0</b> if the operation is successful; returns <b>-1</b> otherwise.
248 *
249 * @since 1.0
250 * @version 1.0
251 */
HotaHalSetMetaData(UpdateMetaData * metaData)252 int HotaHalSetMetaData(UpdateMetaData *metaData)
253 {
254 int ret;
255 if (metaData == NULL) {
256 return OHOS_FAILURE;
257 }
258 ret = memcpy_s(&UpdateInfo.metaData, sizeof(UpdateMetaData), metaData, sizeof(UpdateMetaData));
259 if (ret != 0) {
260 return OHOS_FAILURE;
261 }
262 return OHOS_SUCCESS;
263 }
264
265 /**
266 * @brief check whether pkgVersion is valid.
267 *
268 * You need to call this function before update process.\n
269 *
270 * @return Returns <b>1</b> if pkgVersion is valid compared to currentVersion; returns <b>0</b> otherwise.
271 *
272 * @since 1.0
273 * @version 1.0
274 */
HotaHalCheckVersionValid(const char * currentVersion,const char * pkgVersion,unsigned int pkgVersionLength)275 int HotaHalCheckVersionValid(const char *currentVersion, const char *pkgVersion, unsigned int pkgVersionLength)
276 {
277 if ((currentVersion == NULL) || (pkgVersion == NULL) || (pkgVersionLength == 0)) {
278 return OHOS_FAILURE;
279 }
280 return 0;
281 }
282
283 /**
284 * @brief Get partition info.
285 *
286 * You need to call this funtion in Init function, you need partition info when upgrade. \n
287 *
288 * @return Returns <b>0</b> if the operation is successful; returns <b>-1</b> otherwise.
289 *
290 * @since 1.0
291 * @version 1.0
292 */
HotaHalGetPartitionInfo()293 const ComponentTableInfo *HotaHalGetPartitionInfo()
294 {
295 return &UpdateInfo.componentTableInfo;
296 }