• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 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 "hal_hota_board.h"
17 #include "hi_upg_api.h"
18 #include "stdio.h"
19 #include "string.h"
20 
21 #define PUBKEY_LENGTH 270
22 
23 
24 static const unsigned char g_pubKeyBuf[PUBKEY_LENGTH] = {
25     0x30, 0x82, 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xBF, 0xAA, 0xA5, 0xB3, 0xC2, 0x78, 0x5E,
26     0x63, 0x07, 0x84, 0xCF, 0x37, 0xF0, 0x45, 0xE8, 0xB9, 0x6E, 0xEF, 0x04, 0x88, 0xD3, 0x43, 0x06,
27     0x2C, 0xFC, 0x09, 0x8C, 0x84, 0x3B, 0x43, 0x07, 0x89, 0x6B, 0x23, 0x8F, 0xA2, 0xF0, 0x9D, 0x91,
28     0xCB, 0xA8, 0x02, 0x60, 0xD8, 0x5C, 0x18, 0xD9, 0x34, 0xF0, 0x3C, 0x49, 0x10, 0x0E, 0xE3, 0xC7,
29     0x19, 0xA5, 0x51, 0x93, 0x38, 0xFD, 0xE0, 0x62, 0x64, 0xBA, 0x6D, 0x11, 0x2E, 0xE1, 0x6E, 0x60,
30     0x12, 0x16, 0x1B, 0x35, 0xA6, 0x54, 0x3F, 0x0B, 0x5D, 0x54, 0x08, 0xC9, 0x23, 0x51, 0x15, 0xA9,
31     0xE2, 0x07, 0xCC, 0xF9, 0xFD, 0x19, 0x8A, 0xB3, 0x7E, 0xCE, 0x69, 0xED, 0x26, 0x34, 0xD2, 0x02,
32     0xF3, 0xEB, 0x07, 0x13, 0x69, 0xE3, 0x03, 0x87, 0xB3, 0x6A, 0x3E, 0x91, 0x94, 0xAC, 0x2C, 0xBA,
33     0xF6, 0xEE, 0x4C, 0x41, 0x0C, 0x2E, 0xD0, 0xE7, 0x58, 0xA7, 0xE6, 0x7F, 0x1A, 0xC0, 0xB8, 0xE3,
34     0x12, 0x18, 0x97, 0x8D, 0x99, 0xAB, 0x35, 0x7B, 0xAD, 0x41, 0xA0, 0xFB, 0xCB, 0x23, 0xF6, 0x51,
35     0xE6, 0x94, 0x1F, 0xF7, 0xD8, 0x16, 0xCD, 0x15, 0x67, 0x59, 0x10, 0xA2, 0x40, 0x55, 0xD1, 0x67,
36     0xDA, 0x18, 0xCD, 0x63, 0x5B, 0x10, 0xAF, 0x22, 0x99, 0xD8, 0x9D, 0x56, 0x66, 0xCD, 0x80, 0x64,
37     0x32, 0xB5, 0xD7, 0xF5, 0xBA, 0x91, 0x4A, 0x8D, 0x97, 0x14, 0x8A, 0xB2, 0xB0, 0x42, 0x4A, 0xE6,
38     0x43, 0x22, 0x3B, 0x6E, 0xD6, 0x1E, 0x1F, 0xDA, 0xEC, 0x83, 0xCF, 0x20, 0xFA, 0x02, 0xF3, 0xFB,
39     0x6E, 0x09, 0x2A, 0x0D, 0xB7, 0x81, 0x1E, 0xD5, 0x71, 0xDF, 0x80, 0xC5, 0x33, 0x78, 0xE5, 0x41,
40     0x33, 0xDF, 0x9A, 0xBD, 0x36, 0x51, 0xAC, 0x96, 0xF4, 0xC6, 0x11, 0xC3, 0x93, 0x78, 0x26, 0x96,
41     0x9F, 0x67, 0x05, 0x1D, 0xDF, 0xB3, 0xAA, 0x26, 0x25, 0x02, 0x03, 0x01, 0x00, 0x01
42 };
43 typedef enum {
44     PARTITION_PASS_THROUGH = 0,
45     PARTITION_BOOTLOADER = 2,
46     PARTITION_KERNEL_A = 3,
47     PARTITION_KERNEL_B = 4,
48     PARTITION_ROOTFS = 5,
49     PARTITION_APP = 6,
50     PARTITION_DATA = 7,
51     PARTITION_OTA_TAG = 8,
52     PARTITION_OTA_CONFIG = 9,
53     PARTITION_ROOTFS_EXT4 = 10,
54     PARTITION_MAX
55 } HotaPartition;
56 
57 static const ComponentTableInfo g_componentTable[] = {
58     { PARTITION_PASS_THROUGH, "", "/sdcard/update/ota_pkg_pass_through.bin", 0},
59     { PARTITION_INFO_COMP, "", "/sdcard/update/infocomp.bin", 0},
60     { PARTITION_BOOTLOADER, "bootloader", "/sdcard/update/u-boot.bin", 0},
61     { PARTITION_KERNEL_A, "kernel_A", "/sdcard/update/kernel.bin", 0},
62     { PARTITION_KERNEL_B, "kernel_B", "", 0},
63     { PARTITION_ROOTFS, "rootfs", "/sdcard/update/rootfs.img", 0},
64     { PARTITION_APP, "app", "", 0},
65     { PARTITION_DATA, "data", "", 0},
66     { PARTITION_OTA_TAG, "ota_tag", "/sdcard/update/OTA.tag", 0},
67     { PARTITION_OTA_CONFIG, "config", "/sdcard/update/config", 0},
68     { PARTITION_ROOTFS_EXT4, "rootfs_ext4", "/sdcard/update/rootfs_ext4.img", 0},
69     { PARTITION_MAX, NULL, NULL, 0}
70 };
HotaHalInit(void)71 int HotaHalInit(void)
72 {
73     hi_u32 result = hi_upg_init();
74     if (result != HI_ERR_SUCCESS && result != HI_ERR_UPG_INITILIZATION_ALREADY) {
75         printf("hi_upg_init Failed.Result = %x.\r\n", result);
76         return OHOS_FAILURE;
77     }
78     return OHOS_SUCCESS;
79 }
80 
HotaHalGetUpdateIndex(unsigned int * index)81 int HotaHalGetUpdateIndex(unsigned int *index)
82 {
83     if (hi_upg_get_file_index((hi_u8 *)index) != HI_ERR_SUCCESS) {
84         printf("get upgrade index error\r\n");
85         return OHOS_FAILURE;
86     }
87     return OHOS_SUCCESS;
88 }
89 
HotaHalDeInit(void)90 int HotaHalDeInit(void)
91 {
92     hi_u32 result = hi_upg_stop();
93     if (result != HI_ERR_SUCCESS) {
94         printf("hi_upg_stop Failed.Result = %x.\r\n", result);
95         return OHOS_FAILURE;
96     }
97     return OHOS_SUCCESS;
98 }
99 
HotaHalRead(int partition,unsigned int offset,unsigned int bufLen,unsigned char * buffer)100 int HotaHalRead(int partition, unsigned int offset, unsigned int bufLen, unsigned char *buffer)
101 {
102     if ((buffer == NULL) || (bufLen == 0)) {
103         return OHOS_FAILURE;
104     }
105 
106     if (hi_upg_get_content(offset, buffer, bufLen) != HI_ERR_SUCCESS) {
107         printf("get ota content error! Partition: %d\r\n", partition);
108         return OHOS_FAILURE;
109     }
110 
111     return OHOS_SUCCESS;
112 }
113 
HotaHalWrite(int partition,unsigned char * buffer,unsigned int offset,unsigned int buffLen)114 int HotaHalWrite(int partition, unsigned char *buffer, unsigned int offset, unsigned int buffLen)
115 {
116     if (partition == PARTITION_INFO_COMP) {
117         printf("partition == PARTITION_INFO_COMP, skip it.");
118         return OHOS_SUCCESS;
119     }
120     hi_u32 result = hi_upg_transmit(offset, buffer, buffLen);
121     if (result != HI_ERR_SUCCESS) {
122         printf("hi_upg_transmit failed. retCode = %x.\r\n", result);
123         return OHOS_FAILURE;
124     }
125     return OHOS_SUCCESS;
126 }
127 
HotaHalRestart(void)128 int HotaHalRestart(void)
129 {
130     hi_upg_finish_with_cache();
131     return OHOS_SUCCESS;
132 }
133 
HotaHalSetBootSettings(void)134 int HotaHalSetBootSettings(void)
135 {
136     hi_u32 result = hi_upg_transmit_finish_save_cache();
137     if (result != HI_ERR_SUCCESS) {
138         printf("hi_upg_transmit_finish failed. retCode = %x.\r\n", result);
139         return OHOS_FAILURE;
140     }
141     return OHOS_SUCCESS;
142 }
143 
HotaHalRollback(void)144 int HotaHalRollback(void)
145 {
146     return OHOS_SUCCESS;
147 }
148 
HotaHalGetPartitionInfo(void)149 const ComponentTableInfo *HotaHalGetPartitionInfo(void)
150 {
151     return g_componentTable;
152 }
153 
HotaHalGetPubKey(unsigned int * length)154 unsigned char *HotaHalGetPubKey(unsigned int *length)
155 {
156     if (length == NULL) {
157         printf("OTA pub key is NULL.\r\n");
158         return NULL;
159     }
160 
161     *length = sizeof(g_pubKeyBuf);
162     return (unsigned char *)g_pubKeyBuf;
163 }
164 
165 
HotaHalGetUpdateAbility(void)166 int HotaHalGetUpdateAbility(void)
167 {
168     return ABILITY_PKG_SEARCH | ABILITY_PKG_DLOAD;
169 }
170 
HotaHalGetOtaPkgPath(char * path,int len)171 int HotaHalGetOtaPkgPath(char *path, int len)
172 {
173     int ret = strcpy_s(path, len ,"/sdcard");
174     if (ret != 0) {
175         return OHOS_FAILURE;
176     }
177     return OHOS_SUCCESS;
178 }
179 
HotaHalIsDeviceCanReboot(void)180 int HotaHalIsDeviceCanReboot(void)
181 {
182     return 1;
183 }
184 
HotaHalGetMetaData(UpdateMetaData * metaData)185 int HotaHalGetMetaData(UpdateMetaData *metaData)
186 {
187     (void)metaData;
188     return OHOS_SUCCESS;
189 }
190 
HotaHalSetMetaData(UpdateMetaData * metaData)191 int HotaHalSetMetaData(UpdateMetaData *metaData)
192 {
193     (void)metaData;
194     return OHOS_SUCCESS;
195 }
196 
HotaHalRebootAndCleanUserData(void)197 int HotaHalRebootAndCleanUserData(void)
198 {
199     return OHOS_SUCCESS;
200 }
201 
HotaHalRebootAndCleanCache(void)202 int HotaHalRebootAndCleanCache(void)
203 {
204     return OHOS_SUCCESS;
205 }
206 
HotaHalCheckVersionValid(const char * currentVersion,const char * pkgVersion,unsigned int pkgVersionLength)207 int HotaHalCheckVersionValid(const char *currentVersion, const char *pkgVersion, unsigned int pkgVersionLength)
208 {
209     return (strncmp(currentVersion, pkgVersion, pkgVersionLength) == 0) ? 1 : 0;
210 }
211