blob: a7c86f380c4fc66247fd4c0d030ea95ac6540fa8 [file] [log] [blame]
mj.qu49441c22025-02-10 18:11:48 -08001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <unistd.h>
5#include <fcntl.h>
6#include <errno.h>
w.deng82d2b052025-05-21 00:21:18 -07007#include "cfg_api.h"
mj.qu49441c22025-02-10 18:11:48 -08008
9#define BUFFER_SIZE (1024 * 20) // Define the buffer size for each read
10#define FIND_STR_LEN 128 // Define the length of the string to find
11#define FOTA_UPDATE_STATUS_FILE "/cache/zte_fota/update_status"
12
13// Custom memmem function
14static void* custom_memmem(const void* haystack, size_t haystack_len, const void* needle, size_t needle_len)
15{
16 if (needle_len == 0 || haystack_len < needle_len)
17 {
18 return NULL;
19 }
20
21 size_t i = 0;
22 const unsigned char* h = (const unsigned char*)haystack;
23 const unsigned char* n = (const unsigned char*)needle;
24
25 for (i = 0; i <= haystack_len - needle_len; ++i)
26 {
27 if (memcmp(h + i, n, needle_len) == 0)
28 {
29 return (void*)(h + i);
30 }
31 }
32
33 return NULL;
34}
35
36// Remove the last boundary marker from the file
37static int remove_last_boundary(const char *filename, const char *boundary_marker, int boundary_marker_len)
38{
39 int fd = open(filename, O_RDWR);
40 if (fd == -1)
41 {
42 perror("Unable to open file for reading and writing");
43 return 1;
44 }
45
46 // Get the file size
47 off_t file_size = lseek(fd, 0, SEEK_END);
48 if (file_size == -1)
49 {
50 perror("Unable to get file size");
51 close(fd);
52 return 1;
53 }
54
55 // Search for the boundary_marker from the end of the file
56 off_t search_start = file_size - FIND_STR_LEN;
57 if (search_start < 0)
58 {
59 search_start = 0;
60 }
61
62 lseek(fd, search_start, SEEK_SET);
63
64 char buffer[FIND_STR_LEN + 1]; // +1 for storing '\0'
65 int bytes_read = read(fd, buffer, FIND_STR_LEN);
66 if (bytes_read <= 0)
67 {
68 perror("Unable to read file");
69 close(fd);
70 return 1;
71 }
72 buffer[bytes_read] = '\0'; // Ensure the string is null-terminated
73
74 // Find the boundary_marker
75 char *boundary = custom_memmem(buffer, bytes_read, boundary_marker, boundary_marker_len);
76 if (boundary)
77 {
78 // Found boundary_marker, calculate the truncate position
79 off_t truncate_pos = search_start + (boundary - buffer);
80 if (ftruncate(fd, truncate_pos) == -1)
81 {
82 perror("Unable to truncate file");
83 close(fd);
84 return 1;
85 }
86 }
87
88 close(fd);
89 return 0;
90}
91
92// Function to handle file upload
93static int handle_file_upload(const char *source_filename, const char *target_filename, int content_length)
94{
95 char buffer[BUFFER_SIZE];
96 int bytes_read;
97 int target_fd = -1;
98 int source_fd = -1; // Source file descriptor
99 char *content_start_ptr = NULL;
100 int content_start = 0;
mj.quacba5cb2025-04-24 20:35:25 -0700101 const char *boundary_marker = "\r\n------"; // Boundary marker
mj.qu49441c22025-02-10 18:11:48 -0800102 int boundary_marker_len = strlen(boundary_marker);
103
104 // Open the target file
105 target_fd = open(target_filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
106 if (target_fd == -1)
107 {
108 perror("Unable to open target file");
109 return 1;
110 }
111
112 // Open the source file
113 source_fd = open(source_filename, O_RDONLY);
114 if (source_fd == -1)
115 {
116 perror("Unable to open source file");
117 close(target_fd);
118 return 1;
119 }
120
121 // Read the request header to find the start position of file content
122 bytes_read = read(source_fd, buffer, BUFFER_SIZE);
123 if (bytes_read <= 0)
124 {
125 printf("Content-type: text/html\r\n\r\n");
126 printf("<h1>Error: Failed to read data</h1>");
127 printf("<p>Error number: %d - %s</p>", errno, strerror(errno));
128 close(target_fd);
129 close(source_fd);
130 return 1;
131 }
132
133 // Find the start position of the file content (usually after \r\n\r\n)
134 content_start_ptr = strstr(buffer, "\r\n\r\n");
135 if (!content_start_ptr)
136 {
137 printf("Content-type: text/html\r\n\r\n");
138 printf("<h1>Error: Unable to find the start position of file content</h1>");
139 close(target_fd);
140 close(source_fd);
141 return 1;
142 }
143
144 // Calculate the actual start position of the file content
145 content_start = content_start_ptr - buffer + 4;
146
147 // Write the first part of the file content
148 if (content_start < bytes_read)
149 {
150 if (write(target_fd, buffer + content_start, bytes_read - content_start) == -1)
151 {
152 perror("Unable to write to target file");
153 close(target_fd);
154 close(source_fd);
155 return 1;
156 }
157 }
158
159 // Update the remaining content length
160 content_length -= (bytes_read - content_start);
161
162 // Continue reading and writing the file in segments
163 while (content_length > 0)
164 {
165 bytes_read = read(source_fd, buffer, BUFFER_SIZE);
166 if (bytes_read <= 0)
167 {
168 // If reading is complete, normally break out of the loop
169 break;
170 }
171
172 // Write the file content
173 if (write(target_fd, buffer, bytes_read) == -1)
174 {
175 perror("Unable to write to target file");
176 close(target_fd);
177 close(source_fd);
178 return 1;
179 }
180
181 // Update the remaining content length
182 content_length -= bytes_read;
183 }
184
185 // Close file descriptors
186 close(target_fd);
187 close(source_fd);
188
189 // Remove the last boundary marker from the file
190 return remove_last_boundary(target_filename, boundary_marker, boundary_marker_len);
191}
192
193static int fota_is_file_exist(const char* path)
194{
195 if ( (path == NULL) || (*path == '\0') )
196 return 0;
197 if (access(path, R_OK) != 0)
198 return 0;
199
200 return 1;
201}
202
203static int fota_read_file(const char*path, char*buf, size_t sz)
204{
205 int fd = -1;
206 size_t cnt;
207
208 fd = open(path, O_RDONLY, 0);
209 if(fd < 0)
210 {
211 printf("fota_read_file failed to open %s: %s\n", path, strerror(errno));
212 cnt = -1;
213 return cnt;
214 }
215 cnt = read(fd, buf, sz - 1);
216 if(cnt <= 0)
217 {
218 printf("failed to read %s: %s\n", path, strerror(errno));
219 close(fd);
220 cnt = -1;
221 return cnt;
222 }
223 buf[cnt] = '\0';
224 if(buf[cnt - 1] == '\n')
225 {
226 cnt--;
227 buf[cnt] = '\0';
228 }
229 close(fd);
230
231 return cnt;
232}
233
234static int fota_read_file_int(const char* path, int *val)
235{
236 char buf[32];
237 char *end;
238 int ret;
239 int tmp;
240
241 ret = fota_read_file(path, buf, sizeof(buf));
242 if(ret < 0)
243 return -1;
244
245 errno = 0;
246 tmp = strtol(buf, &end, 0);
247 if (errno == ERANGE)
248 {
249 printf("strtol errno %d: %s\n", errno, strerror(errno));
250 }
251
252 if ((end == buf) || ((end < buf + sizeof(buf)) && (*end != '\0')))
253 {
254 return -1;
255 }
256
257 *val = tmp;
258
259 return 0;
260}
261
262static int fota_get_update_status(int *fota_status)
263{
264 int status = 0;
265 int ret = 0;
266 if(!fota_is_file_exist(FOTA_UPDATE_STATUS_FILE))
267 {
268 *fota_status = -1;
269 return -1;
270 }
271 ret = fota_read_file_int(FOTA_UPDATE_STATUS_FILE, &status);
272 if(ret < 0)
273 {
274 *fota_status = -1;
275 return -1;
276 }
277 *fota_status = status;
278 return 0;
279}
280
mj.qu290d6552025-02-11 22:34:46 -0800281static void print_json_response(int success, const char* message)
282{
283 printf("Content-type: application/json\r\n\r\n");
284 printf("{\"success\": %d, \"message\": \"%s\"}\n", success, message);
285}
286
mj.qu49441c22025-02-10 18:11:48 -0800287static void cgi_fota_update_progress()
288{
289 int upgradeStatus, result;
290
291 system("fota_upi -u verify > /dev/null 2>&1");
mj.qu290d6552025-02-11 22:34:46 -0800292 result = fota_get_update_status(&upgradeStatus);
293 if(result < 0)
294 {
w.deng82d2b052025-05-21 00:21:18 -0700295 cfg_set("fota_update_flag", "0");
mj.qu290d6552025-02-11 22:34:46 -0800296 print_json_response(0, "Fail to read update file");
297 }
298 else if(upgradeStatus != 0)
299 {
w.deng82d2b052025-05-21 00:21:18 -0700300 cfg_set("fota_update_flag", "0");
mj.qu290d6552025-02-11 22:34:46 -0800301 print_json_response(0, "Verify update file failed");
302 }
303 else
304 {
305 print_json_response(1, "File verification successful, start updating...");
306 sleep(1);
307 system("fota_upi -u recovery > /dev/null 2>&1 &");
308 }
mj.qu49441c22025-02-10 18:11:48 -0800309}
310
311int main()
312{
mj.quc4b3b122025-02-12 23:53:45 -0800313 const char *source_filename = "/tmp/firmware_tmp_file"; // Source file path
mj.qu49441c22025-02-10 18:11:48 -0800314 const char *target_filename = "/cache/zte_fota/delta.package"; // Target file path
315 const char *content_length_str = NULL;
316 int content_length = 0;
317
318 system("rm -rf /cache/zte_fota");
319 system("mkdir -p /cache/zte_fota");
320
321 // Get environment variables
322 content_length_str = getenv("CONTENT_LENGTH");
323 if (!content_length_str)
324 {
mj.quc4b3b122025-02-12 23:53:45 -0800325 system("rm -rf /tmp/firmware_tmp_file");
mj.qu290d6552025-02-11 22:34:46 -0800326 print_json_response(0, "Missing CONTENT_LENGTH environment variable");
mj.qu49441c22025-02-10 18:11:48 -0800327 return 1;
328 }
329
330 content_length = atoi(content_length_str);
331 if (content_length <= 0)
332 {
mj.quc4b3b122025-02-12 23:53:45 -0800333 system("rm -rf /tmp/firmware_tmp_file");
mj.qu290d6552025-02-11 22:34:46 -0800334 print_json_response(0, "Invalid CONTENT_LENGTH");
mj.qu49441c22025-02-10 18:11:48 -0800335 return 1;
336 }
w.deng82d2b052025-05-21 00:21:18 -0700337 cfg_set("fota_update_flag", "1");
mj.qu49441c22025-02-10 18:11:48 -0800338 // Call the file upload handling function
339 if (handle_file_upload(source_filename, target_filename, content_length) != 0)
340 {
mj.quc4b3b122025-02-12 23:53:45 -0800341 system("rm -rf /tmp/firmware_tmp_file");
mj.qu290d6552025-02-11 22:34:46 -0800342 print_json_response(0, "File upload failed");
w.deng82d2b052025-05-21 00:21:18 -0700343 cfg_set("fota_update_flag", "0");
mj.qu49441c22025-02-10 18:11:48 -0800344 return 1;
345 }
346
347 // Output success information
mj.qu290d6552025-02-11 22:34:46 -0800348 //print_json_response(1, "File upload successful");
mj.quc4b3b122025-02-12 23:53:45 -0800349 system("rm -rf /tmp/firmware_tmp_file");
mj.qu49441c22025-02-10 18:11:48 -0800350
351 cgi_fota_update_progress();
352
mj.qu49441c22025-02-10 18:11:48 -0800353 return 0;
354}