1 /**
2  * Copyright (C) 2019 Alibaba.inc, All rights reserved.
3  *
4  * @file:    ota_port.h
5  * @brief:   vendor ota interface
6  * @author:
7  * @date:    2019/12/16
8  * @version: 1.0
9  */
10 #ifndef _TG_VENDOR_OTA_H_
11 #define _TG_VENDOR_OTA_H_
12 
13 #include "aos/hal/flash.h"
14 
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 #define OTA_BOOT_INFO_SIZE          0x1000
20 #define OTA_BOOT_INFO_HEAD_LEN      4
21 #define OTA_BOOT_INFO_BODY_LEN      44
22 
23 #define OTA_LINK_NOTUSED            0
24 #define OTA_LINK_USED               1
25 
26 #define OTA_OK                      0
27 #define OTA_FAILE                   1
28 
29 typedef struct
30 {
31 	char *bin_name;
32     hal_partition_t partition;
33 } ota_bin_partition;
34 
35 enum bootinfo_zone {
36     OTA_BOOTINFO_ZONEA,
37     OTA_BOOTINFO_ZONEB,
38     OTA_BOOTINFO_ZONEMAX
39 };
40 
41 typedef enum {
42     ALI_C2H,
43     ALI_C4H,
44     ALI_C5A01,
45     ALI_C1H,
46     ALI_ODM_MAX
47 }ODM_TYPE;
48 
49 enum ota_link {
50     OTA_LINK_ERR = -1,
51     OTA_LINK_A,
52     OTA_LINK_B,
53     OTA_LINK_MAX
54 };
55 
56 struct ota_boot_info {
57     uint32_t  crc32;
58     uint16_t  info_len;
59     uint8_t   odm_type;
60     uint8_t   reserved;
61     uint16_t  linkA_used_flag;
62     uint16_t  linkB_used_flag;
63     uint16_t  update_link;
64     uint8_t   crash_reboot_count;
65 	uint8_t   secureERR_reboot_count;
66     uint16_t  reboot_count_max;
67     uint16_t  reboot_reason;
68     uint16_t  fallback_disable;
69 
70     uint16_t  reserved1; //Tool request struct four byte align
71     uint32_t  boot2a_addr;
72     uint32_t  boot2b_addr;
73     uint32_t  rtosa_addr;
74     uint32_t  rtosb_addr;
75     uint32_t  boot2_len;
76     uint32_t  rtos_len;
77 };
78 
79 int tg_flash_read(const uint32_t addr, uint8_t *dst, const uint32_t size);
80 
81 int ota_adapt_flash_write(const hal_partition_t partition, const uint32_t addr, const uint8_t *src, const uint32_t size);
82 
83 int ota_set_bootinfo_to_zoneAB(struct ota_boot_info *info);
84 
85 enum bootinfo_zone ota_get_valid_bootinfo_zone(void);
86 
87 int ota_get_bootinfo(struct ota_boot_info *info, enum bootinfo_zone zone);
88 
89 ODM_TYPE ota_adapt_get_odm_type(void);
90 
91 int ota_adapt_set_odm_type(ODM_TYPE type);
92 
93 int ota_upgrade_link(void);
94 int ota_set_user_bootinfo(void *param);
95 
96 int ota_clear_reboot_count(void);
97 
98 /**
99  * @brief Get current active A/B slot
100  *
101  * @returns "a" if current slot is A, or "b" if current slot is B.
102  */
103 const char* tg_ota_get_current_ab(void);
104 
105 /**
106  * @brief To mark that the non-active slot is upgraded successfully.
107  * It's time to set A/B related flag to let system switch to new system until next reboot.
108  * It MAY do two actions:
109  * 1. To set flag to indicate system boot from non-active slot next time.
110  * 2. To enable A/B fallback to old system if new system boot failed
111  * @return: - 0: success; - -1: fail
112  */
113 int tg_ota_upgrade_end(void);
114 
115 /**
116  * @brief To mark the ota process is finished and present slot is workable,
117  * It called by genie sdk after main boot flow done
118  * @param[in] ab_fallback: 1: enable ab fallback, if it is not enabled; 0, disable ab_fallback.
119  */
120 void tg_ota_finish(int ab_fallback);
121 int ota_get_running_index(void);
122 #ifdef __cplusplus
123 }
124 #endif
125 
126 #endif /* _TG_VENDOR_OTA_H_ */
127