1 #include <string.h>
2 #include "ota_port.h"
3 #include "cmsis.h"
4 #include "sys_api.h"
5 #include "rtl8721d_ota.h"
6 
7 
ota_get_bootinfo(struct ota_boot_info * info,enum bootinfo_zone zone)8 int ota_get_bootinfo(struct ota_boot_info *info, enum bootinfo_zone zone)
9 {
10     return 0;
11 }
12 
ota_set_bootinfo_crc32value(struct ota_boot_info * info)13 int ota_set_bootinfo_crc32value(struct ota_boot_info *info)
14 {
15 
16     return 0;
17 }
18 
ota_set_bootinfo(struct ota_boot_info * info,enum bootinfo_zone zone)19 int ota_set_bootinfo(struct ota_boot_info *info, enum bootinfo_zone zone)
20 {
21 
22     return 0;
23 }
24 
ota_clear_reboot_count(void)25 int ota_clear_reboot_count(void)
26 {
27     return 0;
28 }
29 
ota_get_boot_type()30 int ota_get_boot_type()
31 {
32     int boot_type = 1;
33     return boot_type;
34 }
35 
ota_get_running_index(void)36 int ota_get_running_index(void)
37 {
38     return ota_get_cur_index();
39 }
40 
ota_set_user_bootinfo(void * param)41 int ota_set_user_bootinfo(void *param)
42 {
43     int ret = -1;
44     char dev_str[16] = {0};
45     int partition_id =5;
46     int fd;
47     unsigned char buf[8] = {0};
48     unsigned char signature[8] = {0x38, 0x31, 0x39, 0x35, 0x38, 0x37, 0x31, 0x31};
49     (void *)param;
50     /*
51         step1: enable new partition to valid.
52     */
53     if(ota_get_running_index()==0)
54         partition_id = MTD_PART_ID_KERNEL2;
55     else
56         partition_id = MTD_PART_ID_KERNEL;
57     printf("enable partition[%d]'s signature to valid\r\n", partition_id);
58     memset(dev_str, 0, sizeof(dev_str));
59     snprintf(dev_str, 15, "/dev/mtdblock%d", partition_id);
60     fd = open(dev_str, 0);
61     if (fd < 0) {
62         printf("open %s failed\r\n", dev_str);
63         return -1;
64     }
65     lseek(fd, 0 ,SEEK_SET);
66     ret = write(fd, signature, sizeof(signature));
67 step1err:
68     /*
69         step1 err: now old partition is valid, so return directly.
70      */
71     close(fd);
72     if(ret != sizeof(signature)) {
73         printf("%s-%d ret %d\r\n" , __FUNCTION__, __LINE__, ret);
74         return -1;
75     }
76     /*
77         step2: enable old partition to invalid.
78     */
79     if(ota_get_running_index()==0)
80         partition_id = MTD_PART_ID_KERNEL;
81     else
82         partition_id = MTD_PART_ID_KERNEL2;
83     printf("enable  partition[%d]'s signature to invalid\r\n", partition_id);
84     memset(dev_str, 0 , sizeof(dev_str));
85     snprintf(dev_str, 15, "/dev/mtdblock%d", partition_id);
86     fd = open(dev_str, 0);
87     if (fd < 0) {
88         printf("open %s failed\r\n", dev_str);
89         goto rollback;
90     }
91     lseek(fd, 0 ,SEEK_SET);
92     memset(buf, 0 ,sizeof(buf));
93     ret = write(fd, buf, sizeof(buf));
94     if(ret != sizeof(buf)) {
95        unsigned char tmp[8];
96        lseek(fd, 0, SEEK_SET);
97        read(fd, tmp, sizeof(tmp));
98        if(!memcmp(tmp, signature, sizeof(tmp))) {
99             close(fd);
100             goto rollback;
101        }
102     }
103     close(fd);
104     return 0;
105     /*
106         step2 open failed or step2 write is not valid:  new partition needs to be rollbacked.
107      */
108 rollback:
109     if(ota_get_running_index()==0)
110         partition_id = MTD_PART_ID_KERNEL2;
111     else
112         partition_id = MTD_PART_ID_KERNEL;
113     printf("rollback partition[%d]\r\n", partition_id);
114     snprintf(dev_str, 15, "/dev/mtdblock%d", partition_id);
115     /*
116         rollback: currently not consider exception.
117      */
118     fd = open(dev_str, 0);
119     lseek(fd, 0 ,SEEK_SET);
120     write(fd, buf, sizeof(buf));
121     close(fd);
122     return -1;
123 
124 }
125 
ota_hal_final(void)126 int ota_hal_final(void)
127 {
128     return ota_set_user_bootinfo(NULL);
129 }
130 
ota_hal_platform_boot_type(void)131 int ota_hal_platform_boot_type(void)
132 {
133     return ota_get_boot_type();
134 }
135 
136