1 /**
2   ******************************************************************************
3   * @file    rtl8721d_ota.h
4   * @author
5   * @version V1.0.0
6   * @date    2016-05-17
7   * @brief   This file provides firmware functions to manage the OTA functions.
8   ******************************************************************************
9   * @attention
10   *
11   * This module is a confidential and proprietary property of RealTek and
12   * possession or use of this module requires written permission of RealTek.
13   *
14   * Copyright(c) 2015, Realtek Semiconductor Corporation. All rights reserved.
15   ******************************************************************************
16   */
17 
18 #ifndef _RTL8721D_OTA_H_
19 #define _RTL8721D_OTA_H_
20 
21 
22 /** @addtogroup AmebaD_Platform
23   * @{
24   */
25 #define SERVER_LOCAL	1
26 #define SERVER_CLOUD	2
27 #define SERVER_TYPE		SERVER_LOCAL						/*configure OTA demo type*/
28 #define MAX_IMG_NUM		2
29 
30 #define HTTP_OTA_UPDATE
31 #define HTTPS_OTA_UPDATE
32 
33 #if (defined HTTP_OTA_UPDATE) || (defined HTTPS_OTA_UPDATE)
34 
35 #define HEADER_BAK_LEN			32
36 
37 typedef struct {
38 	u32	status_code;
39 	u32	header_len;
40 	u8		*body;
41 	u32	body_len;
42 	u8		*header_bak;
43 	u32	parse_status;
44 } http_response_result_t;
45 #endif
46 
47 
48 /** @defgroup OTA
49   * @brief OTA driver modules
50   * @{
51   */
52 
53 /* Exported Types --------------------------------------------------------*/
54 /** @defgroup OTA_Exported_Types OTA Exported Types
55   * @{
56   */
57 
58 /**
59   * @brief  OTA firmware file header structure definition
60   */
61 typedef struct
62 {
63 	u32	FwVer;	/*!< Specifies the OTA firmware verision.
64 	                         This parameter is in first Dword in the firmware file. */
65 	u32	HdrNum;/*!< Specifies the OTA firmware header number.
66 	                         This parameter indicates how many headers in firmware file. */
67 }update_file_hdr;
68 
69 /**
70   * @brief  OTA firmware file image header structure definition
71   */
72 typedef struct
73 {
74 	u8	ImgId[4];	/*!< Specifies the OTA image ID.
75 	                         	This parameter is used to identify the OTA header needed. */
76 	u32	ImgHdrLen;	/*!< Specifies the OTA image header length.
77 	                         	This parameter indicates the Image Header Length. */
78 	u32	Checksum;	/*!< Specifies the OTA image checksum.
79 	                         	This parameter is used to judge whether the image received is correct. */
80 	u32  ImgLen;		/*!< Specifies the OTA image length. */
81 	u32  Offset;		/*!< Specifies the the location in the total firmware file. */
82 	u32  FlashAddr;    /*!< Specifies the flash offset address of the corresponding image. */
83 }update_file_img_hdr;
84 
85 /**
86   * @brief  OTA firmware file download information structure definition
87   */
88 typedef struct
89 {
90 	u32 ImgId;		/*!< Specifies the Image ID.*/
91 
92 	u32	FlashAddr;	/*!< Specifies the Flash Address.
93 	                         	This parameter is used to write the Image to the flash. */
94 	u32	ImgOffset;	/*!< Specifies the Image location in Firmware header.
95 	                         	This parameter indicates the Image location in firmware file. */
96 	u32	ImageLen;	/*!< Specifies the OTA image length. */
97 }update_dw_info;
98 
99 /**
100   * @brief  OTA target image header structure definition
101   */
102 typedef struct
103 {
104 	update_file_hdr	FileHdr;			/*!< Specifies the firmware file header. */
105 	update_file_img_hdr	FileImgHdr[MAX_IMG_NUM];	/*!< Specifies the target OTA image firmware file header. */
106 	u8 Sign[MAX_IMG_NUM][9];			/*!< Specifies the signature of target image. */
107 	u8 ValidImgCnt;						/*!< Specifies valid image number in file. */
108 }update_ota_target_hdr;
109 
110 
111 /* Exported constants --------------------------------------------------------*/
112 
113 /** @defgroup OTA_Exported_Constants OTA Exported Constants
114   * @{
115   */
116 /** @defgroup OTA_system_parameter_definitions
117   * @{
118   */
119 #define BACKUP_SECTOR	(FLASH_RESERVED_DATA_BASE)	/*back up system data offset address*/
120 #define LS_IMG2_OTA1_ADDR	0x08014000				/* KM0 OTA1 start address*/
121 #define LS_IMG2_OTA2_ADDR	0x08194000				/* KM0 OTA2 start address*/
122 
123 
124 #define BUF_SIZE			512								/*the size of the buffer used for receiving firmware data from server*/
125 
126 #define OTA_IMAG			0								/*identify the OTA image*/
127 
128 /**
129   * @}
130   */
131 
132 
133 /**
134   * @}
135   */
136 
137 /* Exported functions --------------------------------------------------------*/
138 /** @defgroup OTA_Exported_Functions OTA Exported Functions
139   * @{
140   */
141 u32 OTA_Change(u32 OTAIdx);
142 
143 void* ota_update_malloc(unsigned int size);
144 void ota_update_free(void *buf);
145 
146 #if (SERVER_TYPE == SERVER_LOCAL)
147 void ota_platform_reset(void);
148 int ota_write_ota2_addr(uint32_t ota_addr);
149 u32 ota_get_cur_index(void);
150 int  ota_readstream_user(u32 address, u32 len, u8 * data);
151 
152 u32 recv_file_info_from_server(u8 * Recvbuf, u32 len, int socket);
153 u32 recv_ota_file_hdr(u8 * Recvbuf, u32 * len, update_ota_target_hdr * pOtaTgtHdr, int socket);
154 u32 get_ota_tartget_header(u8* buf, u32 len, update_ota_target_hdr * pOtaTgtHdr, u8 target_idx);
155 void erase_ota_target_flash(u32 addr, u32 len);
156 u32 download_new_fw_from_server(int socket, update_ota_target_hdr * pOtaTgtHdr, u8 targetIdx);
157 u32 verify_ota_checksum(update_ota_target_hdr * pOtaTgtHdr);
158 u32 change_ota_signature(update_ota_target_hdr * pOtaTgtHdr, u32 ota_target_index);
159 #endif
160 
161 #if (defined HTTP_OTA_UPDATE) || (defined HTTPS_OTA_UPDATE)
162 int  parser_url( char *url, char *host, u16 *port, char *resource);
163 int parse_http_response(unsigned char *response, unsigned int response_len, http_response_result_t *result);
164 #ifdef HTTP_OTA_UPDATE
165 int update_ota_http_connect_server(int server_socket, char *host, int port);
166 u32 recv_ota_file_hdr_http(u8 * Recvbuf, u32 writelen, u32 * len, update_ota_target_hdr * pOtaTgtHdr, int socket);
167 int http_read_socket( int socket, u8 *recevie_buf, int buf_len );
168 u32 download_new_fw_from_server_http(u8* first_buf, unsigned int firstbuf_len, int socket, update_ota_target_hdr * pOtaTgtHdr, u8 targetIdx);
169 int http_update_ota(char *host, int port, char *resource);
170 #endif
171 #ifdef HTTPS_OTA_UPDATE
172 #if !defined(MBEDTLS_CONFIG_FILE)
173 #include <mbedtls/config.h>
174 #else
175 #include MBEDTLS_CONFIG_FILE
176 #endif
177 #include <mbedtls/platform.h>
178 #include <mbedtls/net_sockets.h>
179 #include <mbedtls/ssl.h>
180 u32 recv_ota_file_hdr_https(u8 * Recvbuf, u32 writelen, u32 * len, update_ota_target_hdr * pOtaTgtHdr, mbedtls_ssl_context * ssl);
181 int https_read_socket(mbedtls_ssl_context * ssl, u8 * recevie_buf, int buf_len);
182 u32 download_new_fw_from_server_https(u8 * first_buf, unsigned int firstbuf_len, mbedtls_ssl_context * ssl, update_ota_target_hdr * pOtaTgtHdr, u8 targetIdx);
183 int https_update_ota(char *host, int port, char *resource);
184 #endif
185 #endif
186 
187 /**
188   * @}
189   */
190 
191 /**
192   * @}
193   */
194 
195 /**
196   * @}
197   */
198 #endif //_RTL8721D_OTA_H_
199 /******************* (C) COPYRIGHT 2016 Realtek Semiconductor *****END OF FILE****/
200