1 /*
2 # Copyright (C) 2024 HiHope Open Source Organization .
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 "malloc.h"
17 #include "hal_hota_board.h"
18 #include "upg_debug.h"
19 #include "upg.h"
20
21 #define PUBKEY_LENGTH 270
22
23 static const unsigned char g_pub_key_buf[PUBKEY_LENGTH] = {
24 0x30, 0x82, 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xBF, 0xAA, 0xA5, 0xB3, 0xC2, 0x78, 0x5E,
25 0x63, 0x07, 0x84, 0xCF, 0x37, 0xF0, 0x45, 0xE8, 0xB9, 0x6E, 0xEF, 0x04, 0x88, 0xD3, 0x43, 0x06,
26 0x2C, 0xFC, 0x09, 0x8C, 0x84, 0x3B, 0x43, 0x07, 0x89, 0x6B, 0x23, 0x8F, 0xA2, 0xF0, 0x9D, 0x91,
27 0xCB, 0xA8, 0x02, 0x60, 0xD8, 0x5C, 0x18, 0xD9, 0x34, 0xF0, 0x3C, 0x49, 0x10, 0x0E, 0xE3, 0xC7,
28 0x19, 0xA5, 0x51, 0x93, 0x38, 0xFD, 0xE0, 0x62, 0x64, 0xBA, 0x6D, 0x11, 0x2E, 0xE1, 0x6E, 0x60,
29 0x12, 0x16, 0x1B, 0x35, 0xA6, 0x54, 0x3F, 0x0B, 0x5D, 0x54, 0x08, 0xC9, 0x23, 0x51, 0x15, 0xA9,
30 0xE2, 0x07, 0xCC, 0xF9, 0xFD, 0x19, 0x8A, 0xB3, 0x7E, 0xCE, 0x69, 0xED, 0x26, 0x34, 0xD2, 0x02,
31 0xF3, 0xEB, 0x07, 0x13, 0x69, 0xE3, 0x03, 0x87, 0xB3, 0x6A, 0x3E, 0x91, 0x94, 0xAC, 0x2C, 0xBA,
32 0xF6, 0xEE, 0x4C, 0x41, 0x0C, 0x2E, 0xD0, 0xE7, 0x58, 0xA7, 0xE6, 0x7F, 0x1A, 0xC0, 0xB8, 0xE3,
33 0x12, 0x18, 0x97, 0x8D, 0x99, 0xAB, 0x35, 0x7B, 0xAD, 0x41, 0xA0, 0xFB, 0xCB, 0x23, 0xF6, 0x51,
34 0xE6, 0x94, 0x1F, 0xF7, 0xD8, 0x16, 0xCD, 0x15, 0x67, 0x59, 0x10, 0xA2, 0x40, 0x55, 0xD1, 0x67,
35 0xDA, 0x18, 0xCD, 0x63, 0x5B, 0x10, 0xAF, 0x22, 0x99, 0xD8, 0x9D, 0x56, 0x66, 0xCD, 0x80, 0x64,
36 0x32, 0xB5, 0xD7, 0xF5, 0xBA, 0x91, 0x4A, 0x8D, 0x97, 0x14, 0x8A, 0xB2, 0xB0, 0x42, 0x4A, 0xE6,
37 0x43, 0x22, 0x3B, 0x6E, 0xD6, 0x1E, 0x1F, 0xDA, 0xEC, 0x83, 0xCF, 0x20, 0xFA, 0x02, 0xF3, 0xFB,
38 0x6E, 0x09, 0x2A, 0x0D, 0xB7, 0x81, 0x1E, 0xD5, 0x71, 0xDF, 0x80, 0xC5, 0x33, 0x78, 0xE5, 0x41,
39 0x33, 0xDF, 0x9A, 0xBD, 0x36, 0x51, 0xAC, 0x96, 0xF4, 0xC6, 0x11, 0xC3, 0x93, 0x78, 0x26, 0x96,
40 0x9F, 0x67, 0x05, 0x1D, 0xDF, 0xB3, 0xAA, 0x26, 0x25, 0x02, 0x03, 0x01, 0x00, 0x01
41 };
42 typedef enum {
43 PARTITION_OTA = 0,
44 PARTITION_KERNEL_A = 1,
45 PARTITION_MAX
46 } hota_partition_t;
47
48 static const ComponentTableInfo g_component_table[] = {
49 { PARTITION_OTA, "ota_file", "", 0},
50 { PARTITION_KERNEL_A, "kernel_A", "/sdcard/update/kernel.bin", 0},
51 { PARTITION_MAX, NULL, NULL, 0}
52 };
53
54 typedef enum {
55 UPG_STATUS_UNINIT = 0,
56 UPG_STATUS_INIT = 1,
57 UPG_STATUS_START = 2,
58 UPG_STATUS_FINISH = 3,
59 UPG_STATUS_MAX
60 } upg_proc_status_t;
61 static uint8_t g_upg_status = UPG_STATUS_UNINIT;
62
upg_init(void)63 errcode_t upg_init(void)
64 {
65 errcode_t ret;
66 upg_func_t upg_func = {0};
67 upg_func.malloc = malloc;
68 upg_func.free = free;
69 upg_func.serial_putc = NULL;
70 ret = uapi_upg_init(&upg_func);
71 if (ret != ERRCODE_SUCC && ret != ERRCODE_UPG_ALREADY_INIT) {
72 upg_log_err("[UPG] upgrade init failed!\r\n");
73 return ret;
74 }
75 upg_log_info("[UPG] upgrade init OK!\r\n");
76 return ERRCODE_SUCC;
77 }
78
HotaHalInit(void)79 int HotaHalInit(void)
80 {
81 errcode_t res = upg_init();
82 if (res != OHOS_SUCCESS) {
83 return OHOS_FAILURE;
84 }
85 g_upg_status = UPG_STATUS_INIT;
86 upg_prepare_info_t info = {0};
87 uapi_upg_prepare(&info);
88 return OHOS_SUCCESS;
89 }
90
HotaHalGetUpdateIndex(unsigned int * index)91 int HotaHalGetUpdateIndex(unsigned int *index)
92 {
93 #ifndef CONFIG_MIDDLEWARE_SUPPORT_UPG_AB
94 *index = PARTITION_OTA;
95 return OHOS_SUCCESS;
96 #endif
97 }
98
HotaHalDeInit(void)99 int HotaHalDeInit(void)
100 {
101 if (g_upg_status != UPG_STATUS_START) {
102 return OHOS_FAILURE;
103 }
104 return OHOS_SUCCESS;
105 }
106
HotaHalRead(int partition,unsigned int offset,unsigned int bufLen,unsigned char * buffer)107 int HotaHalRead(int partition, unsigned int offset, unsigned int bufLen, unsigned char *buffer)
108 {
109 if (g_upg_status != UPG_STATUS_START) {
110 return OHOS_FAILURE;
111 }
112 if (uapi_upg_read_package(offset, buffer, bufLen) != OHOS_SUCCESS) {
113 return OHOS_FAILURE;
114 }
115
116 return OHOS_SUCCESS;
117 }
118
HotaHalWrite(int partition,unsigned char * buffer,unsigned int offset,unsigned int buffLen)119 int HotaHalWrite(int partition, unsigned char *buffer, unsigned int offset, unsigned int buffLen)
120 {
121 if (partition != PARTITION_OTA || g_upg_status != UPG_STATUS_START) {
122 return OHOS_FAILURE;
123 }
124 unsigned int result = uapi_upg_write_package_sync(offset, buffer, buffLen);
125 if (result != OHOS_SUCCESS) {
126 return OHOS_FAILURE;
127 }
128 return OHOS_SUCCESS;
129 }
130
HotaHalRestart(void)131 int HotaHalRestart(void)
132 {
133 upg_reboot();
134 return OHOS_SUCCESS;
135 }
136
HotaHalSetBootSettings(void)137 int HotaHalSetBootSettings(void)
138 {
139 return OHOS_SUCCESS;
140 }
141 /* no support */
HotaHalRollback(void)142 int HotaHalRollback(void)
143 {
144 return OHOS_SUCCESS;
145 }
146
HotaHalGetPartitionInfo(void)147 const ComponentTableInfo *HotaHalGetPartitionInfo(void)
148 {
149 return g_component_table;
150 }
151
HotaHalGetPubKey(unsigned int * length)152 unsigned char *HotaHalGetPubKey(unsigned int *length)
153 {
154 if (length == NULL) {
155 return NULL;
156 }
157
158 *length = sizeof(g_pub_key_buf);
159 return (unsigned char *)g_pub_key_buf;
160 }
161
162
HotaHalGetUpdateAbility(void)163 int HotaHalGetUpdateAbility(void)
164 {
165 return ABILITY_PKG_SEARCH | ABILITY_PKG_DLOAD;
166 }
167
168 /* no support */
HotaHalGetOtaPkgPath(char * path,int len)169 int HotaHalGetOtaPkgPath(char *path, int len)
170 {
171 (void)path;
172 (void)len;
173 return OHOS_SUCCESS;
174 }
175
176 /* support reboot when update locally */
HotaHalIsDeviceCanReboot(void)177 int HotaHalIsDeviceCanReboot(void)
178 {
179 return 1;
180 }
181
182 /* no support */
HotaHalGetMetaData(UpdateMetaData * metaData)183 int HotaHalGetMetaData(UpdateMetaData *metaData)
184 {
185 (void)metaData;
186 return OHOS_SUCCESS;
187 }
188
189 /* no support */
HotaHalSetMetaData(UpdateMetaData * metaData)190 int HotaHalSetMetaData(UpdateMetaData *metaData)
191 {
192 (void)metaData;
193 return OHOS_SUCCESS;
194 }
195
196 /* no support */
HotaHalRebootAndCleanUserData(void)197 int HotaHalRebootAndCleanUserData(void)
198 {
199 return OHOS_SUCCESS;
200 }
201
202 /* no support */
HotaHalRebootAndCleanCache(void)203 int HotaHalRebootAndCleanCache(void)
204 {
205 return OHOS_SUCCESS;
206 }
207
HotaHalCheckVersionValid(const char * currentVersion,const char * pkgVersion,unsigned int pkgVersionLength)208 int HotaHalCheckVersionValid(const char *currentVersion, const char *pkgVersion, unsigned int pkgVersionLength)
209 {
210 return (strncmp(currentVersion, pkgVersion, pkgVersionLength) == 0) ? 1 : 0;
211 }
212