1 /** @defgroup ota_internal_api
2  * @{
3  *
4  * This is an include file of OTA internal interface.
5  *
6  * Copyright (C) 2015-2021 Alibaba Group Holding Limited
7  */
8 
9 #ifndef OTA_API_H
10 #define OTA_API_H
11 #include "ota_agent.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 #define OTA_INFO        (UAGENT_FUNC_USER_BASE)   /* ota process */
18 #define OTA_UPGRADE_CMD (UAGENT_FUNC_USER_BASE+1) /* upgrade command */
19 #define VERSION_INFORM  (UAGENT_FUNC_USER_BASE+2) /* version inform */
20 
21 #define OTA_COAP_OPTION_BLOCK      23    /*OTA coap option block*/
22 
23 #ifndef OTA_DOWNLOAD_RETRY_CNT
24 #define OTA_DOWNLOAD_RETRY_CNT     5     /*OTA download retry count default:5*/
25 #endif
26 
27 #ifndef OTA_DOWNLOAD_TIMEOUT
28 #define OTA_DOWNLOAD_TIMEOUT       20000 /*OTA download timeout Unit:ms default:20s*/
29 #endif
30 
31 #ifndef OTA_DOWNLOAD_BLOCK_SIZE
32 #define OTA_DOWNLOAD_BLOCK_SIZE    2048   /*OTA download block size:2048*/
33 #endif
34 
35 #ifndef OTA_FLASH_BLOCK_SIZE
36 #define OTA_FLASH_BLOCK_SIZE     0x10000   /*OTA erase/write block size:64K*/
37 #endif
38 
39 #ifndef OTA_FLASH_WRITE_CACHE_SIZE
40 #define OTA_FLASH_WRITE_CACHE_SIZE 2048 /*OTA write flash cache size*/
41 #endif
42 
43 #define OTA_MSG_LEN  256 /*OTA topic message max len*/
44 
45 #define L_EXTRACT_U16(d) ((*((unsigned char *)(d)) << 8) | *((unsigned char *)(d) + 1))
46 #define L_EXTRACT_U32(d)                                              \
47     ((*((unsigned char *)(d)) << 24) | (*((unsigned char *)(d) + 1) << 16) | \
48     (*((unsigned char *)(d) + 2) << 8) | *((unsigned char *)(d) + 3))
49 
50 #define EXTRACT_U16(d) (*((unsigned char *)(d)) | (*((unsigned char *)(d) + 1) << 8))
51 #define EXTRACT_U32(d)                                              \
52     (*((unsigned char *)(d)) | (*((unsigned char *)(d) + 1) << 8) | \
53     (*((unsigned char *)(d) + 2) << 16) | (*((unsigned char *)(d) + 3) << 24))
54 
55 #define ENCODE_U16(d, val)                             \
56 {                                                      \
57     *((unsigned char *)(d))     = (val)&0xFF;          \
58     *((unsigned char *)(d) + 1) = ((val) >> 8) & 0xFF; \
59 }
60 
61 #define ENCODE_U32(d, val)                              \
62 {                                                       \
63     *((unsigned char *)(d))     = (val)&0xFF;           \
64     *((unsigned char *)(d) + 1) = ((val) >> 8) & 0xFF;  \
65     *((unsigned char *)(d) + 2) = ((val) >> 16) & 0xFF; \
66     *((unsigned char *)(d) + 3) = ((val) >> 24) & 0xFF; \
67 }
68 
69 enum {
70     OTA_PROCESS_NORMAL = 0,
71     OTA_PROCESS_UAGENT_OTA
72 };
73 
74 typedef enum {
75     OTA_REQ_VERSION = 0,
76     OTA_UPGRADE_SOC = 1,
77     OTA_UPGRADE_MCU = 2
78 } OTA_UPGRADE_CMD_T;
79 
80 /**
81 *
82 * ota transport
83 *
84 **/
85 typedef enum {
86         OTA_PAR_SUCCESS      = 0,
87         OTA_ARGS_PAR_FAIL    = -1,
88         OTA_MESSAGE_PAR_FAIL = -2,
89         OTA_SUCCESS_PAR_FAIL = -3,
90         OTA_DATA_PAR_FAIL    = -4,
91         OTA_URL_PAR_FAIL     = -5,
92         OTA_VER_PAR_FAIL     = -6,
93         OTA_HASH_PAR_FAIL    = -7,
94         OTA_MD5_PAR_FAIL     = -8,
95         OTA_SHA256_PAR_FAIL  = -9,
96         OTA_SIZE_PAR_FAIL    = -10,
97         OTA_SIGN_PAR_FAIL    = -11,
98 } OTA_TRANSPORT_ERR_E;
99 
100 /*
101 *
102 * ota verify
103 */
104 #define OTA_HASH_NONE                (0)
105 #define OTA_SHA256                   (1)
106 #define OTA_MD5                      (2)
107 #define OTA_SHA256_HASH_SIZE         (32)
108 #define OTA_MD5_HASH_SIZE            (16)
109 #define OTA_SIGN_BITNUMB             (2048)
110 
111 #if defined(__ICCARM__)
112 #define OTA_WEAK                __weak
113 #else
114 #define OTA_WEAK                __attribute__((weak))
115 #endif
116 
117 int ota_read_parameter(ota_boot_param_t *ota_param);
118 int ota_update_parameter(ota_boot_param_t *ota_param);
119 
120 int ota_clear();
121 int hal_reboot_bank(void);
122 int ota_clear_paramters();
123 int ota_is_download_mode(void);
124 int ota_int(ota_boot_param_t *param);
125 int ota_verify(ota_boot_param_t *param);
126 int ota_verify_fsfile(ota_boot_param_t *param, char *file_path);
127 int ota_write(unsigned int *off, char *in_buf, unsigned int in_buf_len);
128 int ota_read(unsigned int *off, char *out_buf, unsigned int out_buf_len);
129 
130 int ota_fclose(int fd);
131 int ota_fopen(const char *filename, int mode);
132 int ota_fread(int fd, void *buf, unsigned int size);
133 int ota_fwrite(int fd, const void *buf, unsigned int size);
134 int ota_check_hash(unsigned char type, char *src, char *dst);  /* ota compare hash value. */
135 unsigned short ota_get_upgrade_flag(void);                     /* ota get upgrade flag. */
136 int ota_update_upg_flag(unsigned short flag);                  /* ota update upgrade flag. */
137 int ota_update_process(const char *err, const int step);
138 unsigned int ota_parse_ota_type(unsigned char *buf, unsigned int len); /* parse ota type:diff xz or normal*/
139 int ota_transport_inform_otaver(ota_service_t *ctx);
140 void ota_set_upgrade_status(char is_upgrade);
141 /**
142  * @}
143  */
144 #ifdef __cplusplus
145 }
146 #endif
147 #endif /*__OTA_API_H__*/
148