1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 #include "nbpatch.h"
5 #include "updater.h"
6 
ota_patch_read(unsigned long src,unsigned char * buffer,unsigned long pos,unsigned long size)7 int ota_patch_read(unsigned long src, unsigned char *buffer, unsigned long pos, unsigned long size)
8 {
9     unsigned long base = pos;
10     unsigned long read_size = 0;
11     int ret = 0;
12 
13     while(size > 0) {
14         read_size = size > SECTOR_SIZE ? SECTOR_SIZE : size;
15         ret = flash_read_data(src + pos, buffer+pos-base, read_size);
16         if(ret < 0) {
17             return -1;
18         }
19         pos += read_size;
20         size -= read_size;
21     }
22     return pos - base;
23 }
24 
ota_patch_write(unsigned long dst,unsigned char * buffer,unsigned long pos,unsigned long size)25 int ota_patch_write(unsigned long dst, unsigned char *buffer, unsigned long pos, unsigned long size)
26 {
27     unsigned long base = pos;
28     unsigned long write_size = 0;
29     int ret = 0;
30 
31     while(size > 0) {
32         write_size = size > SECTOR_SIZE? SECTOR_SIZE:size;
33         ret = flash_erase(dst + pos, write_size);
34         if(ret < 0) {
35             return ret;
36         }
37         ret = flash_write_data(dst + pos, buffer+pos-base, write_size);
38         if(ret < 0) {
39             return ret;
40         }
41         pos += write_size;
42         size -= write_size;
43     }
44     return pos - base;
45 }
46 
ota_patch_new_data(unsigned char * buf,long size)47 int ota_patch_new_data(unsigned char *buf, long size)
48 {
49     unsigned long pos = 0;
50     unsigned long bsize = 0;
51     int ret = 0;
52     hal_logic_partition_t *ota_info = flash_get_info(HAL_PARTITION_OTA_TEMP);
53     if(ota_info == NULL) {
54         printf("ota flash info err.\r\n");
55         return -1;
56     }
57     unsigned int diff_data = ota_info->partition_start_addr + ota_info->partition_length - DIFF_BLOCK_SIZE;
58     while (size > 0) {
59         bsize = size > SECTOR_SIZE ? SECTOR_SIZE : size;
60         ret = flash_erase(diff_data + pos, bsize);
61         if(ret < 0) {
62             return ret;
63         }
64         ret = flash_write_data(diff_data + pos, buf + pos, bsize);
65         if(ret < 0) {
66             return ret;
67         }
68         pos += bsize;
69         size -= bsize;
70     }
71     return 0;
72 }
ota_load_new_data(unsigned long dst,long size)73 int ota_load_new_data(unsigned long dst, long size) {
74     unsigned char tmp[SECTOR_SIZE];
75     unsigned long pos = 0;
76     unsigned long bsize = 0;
77     int ret = 0;
78     hal_logic_partition_t *app_info = flash_get_info(HAL_PARTITION_APPLICATION);
79     hal_logic_partition_t *ota_info = flash_get_info(HAL_PARTITION_OTA_TEMP);
80     if(app_info == NULL || ota_info == NULL) {
81         printf("ota data & app info err.\r\n");
82         return -1;
83     }
84     unsigned int diff_data = ota_info->partition_start_addr + ota_info->partition_length - DIFF_BLOCK_SIZE;
85     unsigned int diff_app = app_info->partition_start_addr;
86     while (size > 0) {
87         bsize = size > SECTOR_SIZE ? SECTOR_SIZE : size;
88         memset(tmp, 0, SECTOR_SIZE);
89         ret = flash_read_data(diff_data + pos, tmp, bsize);
90         if(ret < 0) {
91             return ret;
92         }
93         ret = flash_erase(diff_app + dst + pos, bsize);
94         if(ret < 0) {
95             return ret;
96         }
97         ret = flash_write_data(diff_app + dst + pos, tmp, bsize);
98         if(ret < 0) {
99             return ret;
100         }
101         pos += bsize;
102         size -= bsize;
103     }
104     return ret;
105 }
106 
ota_patch_read_param(ota_boot_param_t * ota_param)107 int ota_patch_read_param(ota_boot_param_t *ota_param) {
108     int ret = -1;
109     unsigned short patch_crc = 0;
110     if(!ota_param){
111         return -1;
112     }
113     hal_logic_partition_t *par_info = flash_get_info(HAL_PARTITION_PARAMETER_1);
114     if(par_info == NULL) {
115         printf("par info err.\r\n");
116         return -1;
117     }
118     ret = flash_read_data(par_info->partition_start_addr,(unsigned char *)ota_param, sizeof(ota_boot_param_t));
119     if(ret < 0) {
120         return -1;
121     }
122     patch_crc = crc16_computer(ota_param, sizeof(ota_boot_param_t) - sizeof(unsigned short));
123     if(patch_crc == ota_param->param_crc) {
124         return 0;
125     }
126     OTA_LOG_I("ota crc cal:0x%04x  param:0x%04x \n", patch_crc, ota_param->param_crc);
127     return ret;
128 }
129 
ota_patch_write_param(ota_boot_param_t * ota_param)130 int ota_patch_write_param(ota_boot_param_t *ota_param) {
131     int ret = 0;
132     if(!ota_param){
133         return -1;
134     }
135     hal_logic_partition_t *par_info = flash_get_info(HAL_PARTITION_PARAMETER_1);
136     if(par_info == NULL) {
137         printf("par info err.\r\n");
138         return -1;
139     }
140 
141     ota_param->param_crc = crc16_computer(ota_param, sizeof(ota_boot_param_t) - sizeof(unsigned short));
142     ret = flash_erase(par_info->partition_start_addr, sizeof(ota_boot_param_t));
143     if(ret < 0) {
144         return -1;
145     }
146     ret = flash_write_data(par_info->partition_start_addr, (unsigned char *) ota_param, sizeof(ota_boot_param_t));
147     if (ret < 0) {
148         return -1;
149     }
150     return ret;
151 }
152 
153