1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <stdarg.h>
4 #include <time.h>
5 #include <errno.h>
6 #include "nbpatch.h"
7 #include "updater.h"
8 #include "aos/hal/flash.h"
9
10 #define FLASH_SIZE 0x2000000
11
12 unsigned char* memory_base = NULL;
13
ota_patch_init(char * old_file,char * patch_file)14 int ota_patch_init(char* old_file, char* patch_file){
15 int ret = 0;
16 int app_size = 0;
17 int patch_size = 0;
18 unsigned char* pos = NULL;
19 int read_size = 0;
20 int size = 0;
21 FILE *old_fd = NULL;
22
23 hal_logic_partition_t *ota_info = flash_get_info(HAL_PARTITION_OTA_TEMP);
24 hal_logic_partition_t *app_info = flash_get_info(HAL_PARTITION_APPLICATION);
25 if(ota_info == NULL || app_info == NULL) {
26 OTA_LOG_I("flash info err.\r\n");
27 return -1;
28 }
29 ota_boot_param_t ota_param = {0};
30 memory_base = malloc (FLASH_SIZE);
31 if(memory_base == NULL) {
32 return -1;
33 }
34 memset(memory_base, 0xFF, FLASH_SIZE);
35 old_fd = fopen (old_file, "rb" );
36 if (old_fd==NULL) {
37 OTA_LOG_I("open old err:%d\n",errno);
38 return -1;
39 }
40 fseek (old_fd , 0 , SEEK_END);
41 app_size = ftell(old_fd);
42 rewind (old_fd);
43
44 pos = (unsigned char*)(memory_base + app_info->partition_start_addr);
45 read_size = 0;
46 size = app_size;
47 while(size > 0) {
48 read_size = size > SECTOR_SIZE ? SECTOR_SIZE : size;
49 ret = fread(pos, read_size, 1, old_fd);
50 if(ret < 0 ) {
51 OTA_LOG_I("read old err:%d ret:%d rsize:%d \n",errno, ret, read_size);
52 return -1;
53 }
54 pos += read_size;
55 size -= read_size;
56 }
57 fclose(old_fd);
58 old_fd = fopen (patch_file, "rb" );
59 if (old_fd==NULL) {
60 OTA_LOG_I("open old err:%d\n",errno);
61 return -1;
62 }
63 fseek (old_fd , 0 , SEEK_END);
64 patch_size = ftell(old_fd);
65 rewind (old_fd);
66 memset(&ota_param, 0x00, sizeof(ota_boot_param_t));
67 ota_param.dst_adr = app_info->partition_start_addr;
68 ota_param.src_adr = ota_info->partition_start_addr;
69 ota_param.len = patch_size;
70 ota_param.old_size = app_info->partition_length;
71 ota_param.upg_flag = OTA_UPGRADE_DIFF;
72 ret = ota_patch_write_param(&ota_param);
73 if(ret < 0) {
74 OTA_LOG_I("write parameter err.\n");
75 return -1;
76 }
77 pos = (unsigned char*)(memory_base + ota_info->partition_start_addr);
78 read_size = 0;
79 size = app_size;
80 while(size > 0) {
81 read_size = size > SECTOR_SIZE ? SECTOR_SIZE:size;
82 ret = fread(pos, read_size, 1, old_fd);
83 if(ret < 0) {
84 OTA_LOG_I("read patch err:%d ret:%d rsize:%d \n",errno, ret, read_size);
85 return -1;
86 }
87 pos += read_size;
88 size -= read_size;
89 }
90 fclose(old_fd);
91 old_fd = NULL;
92 OTA_LOG_I("ota patch load app size:0x%x patch size:0x%x success.\n", app_size, patch_size);
93 return ret;
94 }
95
ota_patch_dump_flash(char * name,int size)96 int ota_patch_dump_flash(char* name, int size){
97 int ret = 0;
98 FILE* all_fd = NULL;
99 all_fd = fopen (name, "wb" );
100 if (all_fd==NULL) {
101 OTA_LOG_I("open name err:%d\n",errno);
102 return -1;
103 }
104 hal_logic_partition_t *app_info = flash_get_info(HAL_PARTITION_APPLICATION);
105 if(app_info == NULL) {
106 OTA_LOG_I("app info err.\r\n");
107 return -1;
108 }
109 OTA_LOG_I("ota patch new file off:%d size:%d.\n", app_info->partition_start_addr, size);
110 ret = fwrite(memory_base + app_info->partition_start_addr, size, 1, all_fd);
111 if(ret < 0){
112 OTA_LOG_I("write name err:%d\n",errno);
113 return -1;
114 }
115 fflush(all_fd);
116 return ret;
117 }
118
main(int argc,char * argv[])119 int main(int argc, char *argv[])
120 {
121 char *old_file = NULL;
122 char *new_file = NULL;
123 char *diff_file = NULL;
124 int ret = 0;
125
126 OTA_LOG_I("nbpatch ver:%s start ...\n", NBPATCH_VERSION);
127 if(argc < 4) {
128 OTA_LOG_I("Usage: %s old_file new_file diff file [log_file]\r\n", argv[0]);
129 OTA_LOG_I("Notice: old file and new file less than 8M. \r\n");
130 return -1;
131 }
132 old_file = argv[1];
133 new_file = argv[2];
134 diff_file = argv[3];
135
136 OTA_LOG_I("ota patch old:%s new:%s diff:%s.\n", old_file, new_file, diff_file);
137 ret = ota_patch_init(old_file, diff_file);
138 if(ret < 0){
139 return -1;
140 }
141 ret = ota_nbpatch_main();
142 if(ret < 0) {
143 return -1;
144 }
145 ret = ota_patch_dump_flash(new_file, ret);
146 if(ret < 0) {
147 return -1;
148 }
149 OTA_LOG_I("ota patch success. \n");
150 return 0;
151 }
152