• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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  * Description: UPG encry image functions source file
15  */
16 #ifdef CONFIG_MIDDLEWARE_SUPPORT_UPG_COMPRESS_ENCRY
17 #include <stddef.h>
18 #include <stdint.h>
19 #include "chip_io.h"
20 #include "securec.h"
21 #include "common_def.h"
22 #include "upg_common.h"
23 #include "upg_debug.h"
24 #include "upg_encry.h"
25 
26 /* 加密镜像后,写flash */
upg_encry_and_write_pkt(upg_lzma_decode2_data_t * data,const upg_image_header_t * image,td_bool * first_pkt)27 uint32_t upg_encry_and_write_pkt(upg_lzma_decode2_data_t *data, const upg_image_header_t *image, td_bool *first_pkt)
28 {
29     uint32_t ret;
30     uint32_t head_offset = 0;
31     uint32_t read_len = 0;
32     uint32_t res_len = 0;
33     upg_lzma_buf_t *buf = &data->buf;
34     /* 升级包未配置加密直接返回 */
35     if (image->re_enc_flag != OTA_ENCRY_FLAG) {
36         return ERRCODE_SUCC;
37     }
38 
39     /* 镜像头处理 */
40     ret = upg_process_cryto_info(&head_offset, first_pkt);
41     if (ret != ERRCODE_SUCC) {
42         return ret;
43     }
44 
45     /* 加密要求16byte对齐 */
46     read_len = buf->write_pos;
47     res_len = read_len % BYTES_IN_ONE_LINE;
48     read_len = (read_len / BYTES_IN_ONE_LINE) * BYTES_IN_ONE_LINE;
49     upg_log_dbg("[UPG] upg_encry_fota_pkt: midbuf[write_pos:%u, read_len:%u, res_len:%u] \r\n",
50         buf->write_pos, read_len, res_len);
51     /* 将16byte对齐部分加密写flash */
52     if (read_len != 0) {
53         /* 加密 */
54         ret = upg_encry_fota_pkt(buf->outbuf + head_offset, read_len - head_offset, image);
55         if (ret != ERRCODE_SUCC) {
56             upg_log_err("[UPG] upg_encry_fota_pkt:drv_rom_cipher_symc_decrypt failed, ret = 0x%x \r\n", ret);
57             return ret;
58         }
59 
60         /* 写flash */
61 #if defined(UPG_CFG_SUPPORT_ERASE_WHOLE_IMAGE) && defined(YES) && (UPG_CFG_SUPPORT_ERASE_WHOLE_IMAGE == YES)
62         ret = upg_write_new_image_data(data->out_offset, buf->outbuf, &read_len, data->image_id, false);
63 #else
64         ret = upg_write_new_image_data(data->out_offset, buf->outbuf, &read_len, data->image_id, true);
65 #endif
66         if (ret != ERRCODE_SUCC) {
67             return SZ_ERROR_DATA;
68         }
69         data->out_offset += read_len;
70     }
71     /* 将16byte对齐后剩余部分拷贝到outbuf头部 */
72     if (res_len != 0) {
73         if (memmove_s(buf->outbuf, OUT_BUF_SIZE, buf->outbuf + read_len, res_len) != EOK) {
74             upg_log_err("[UPG] upg_encry_fota_pkt:memmove_s failed.\r\n");
75             return ERRCODE_FAIL;
76         }
77         buf->write_pos = res_len;
78         return ERRCODE_SUCC;
79     }
80 
81     buf->write_pos = 0;
82     return ERRCODE_SUCC;
83 }
84 #endif
85