--- /dev/null
+#!/usr/bin/env python3
+import os
+import sys
+import struct
+import zlib
+import argparse
+
+# 文件头结构 (512字节)
+# 偏移量 长度 类型 描述
+# 0 4 uint32 魔数 (0x4F544121 "OTA!")
+# 4 32 char[] 版本字符串 (以空字符结尾)
+# 36 8 uint64 文件大小 (实际程序大小)
+# 44 4 uint32 CRC32 校验值
+# 48 464 byte[] 保留区域 (填充0)
+
+HEADER_SIZE = 512
+MAGIC_NUMBER = 0x4F544121 # "OTA!" in hex
+
+def add_header(input_file, version, output_file):
+ # 读取输入文件内容
+ with open(input_file, 'rb') as f:
+ file_data = f.read()
+
+ file_size = len(file_data)
+ crc32_value = zlib.crc32(file_data) & 0xFFFFFFFF
+
+ # 创建文件头
+ header = bytearray(HEADER_SIZE)
+
+ # 设置魔数
+ struct.pack_into('<I', header, 0, MAGIC_NUMBER)
+
+ # 设置版本字符串 (最大32字节)
+ version_bytes = version.encode('utf-8')[:31] # 保留一个字节给空字符
+ struct.pack_into('32s', header, 4, version_bytes)
+
+ # 设置文件大小
+ struct.pack_into('<Q', header, 36, file_size)
+
+ # 设置CRC32
+ struct.pack_into('<I', header, 44, crc32_value)
+
+ # 保留区域保持为0 (默认已经是0)
+
+ # 写入输出文件
+ with open(output_file, 'wb') as f:
+ f.write(header)
+ f.write(file_data)
+
+ # 设置可执行权限
+ os.chmod(output_file, 0o755)
+
+ print(f"Header added to {output_file}")
+ print(f"Version: {version}")
+ print(f"Size: {file_size} bytes")
+ print(f"CRC32: {crc32_value:08X}")
+
+def main():
+ parser = argparse.ArgumentParser(description='Add binary header to application')
+ parser.add_argument('input_file', help='Input application file')
+ parser.add_argument('version', help='Version string')
+ parser.add_argument('output_file', help='Output file with header')
+
+ args = parser.parse_args()
+
+ add_header(args.input_file, args.version, args.output_file)
+
+if __name__ == '__main__':
+ main()
if (ferror(fp)) {
perror("Error reading header");
} else {
- fprintf(stderr, "Incomplete header read: %zu/%zu bytes\n",
+ fprintf(stderr, "Incomplete header read: %zu/%zu bytes\n",
read, sizeof(header));
}
fclose(fp);
return -1;
}
-
+
// 验证魔数
if (header.magic != MAGIC_NUMBER) {
- fprintf(stderr, "Invalid magic number: 0x%08X (expected 0x%08X)\n",
+ fprintf(stderr, "Invalid magic number: 0x%08X (expected 0x%08X)\n",
header.magic, MAGIC_NUMBER);
fclose(fp);
return -1;
}
-
+
// 获取文件总大小
fseek(fp, 0, SEEK_END);
long total_size = ftell(fp);
fseek(fp, sizeof(header), SEEK_SET); // 回到数据开始位置
-
+
// 验证文件大小
if (total_size < HEADER_SIZE) {
fprintf(stderr, "File too small: %ld bytes (min %d)\n", total_size, HEADER_SIZE);
fclose(fp);
return -1;
}
-
+
if (total_size != (long)(HEADER_SIZE + header.file_size)) {
fprintf(stderr, "Size mismatch: header claims %lu, file has %ld\n",
HEADER_SIZE + header.file_size, total_size);
fclose(fp);
return -1;
}
-
+
// 读取应用程序数据
uint8_t *file_data = malloc(header.file_size);
if (!file_data) {
fclose(fp);
return -1;
}
-
+
read = fread(file_data, 1, header.file_size, fp);
if (read != header.file_size) {
fprintf(stderr, "Incomplete data read: %zu/%lu bytes\n",
return -1;
}
fclose(fp);
-
+
// 计算CRC32
uint32_t calculated_crc = crc32(0L, Z_NULL, 0);
calculated_crc = crc32(calculated_crc, file_data, header.file_size);
-
+
// 验证CRC32
if (calculated_crc != header.crc32) {
fprintf(stderr, "CRC32 mismatch: expected 0x%08X, calculated 0x%08X\n",
free(file_data);
return -1;
}
-
+
printf("Firmware validation successful\n");
printf("Version: %s\n", header.version);
printf("Size: %lu bytes\n", (unsigned long)header.file_size);
printf("CRC32: 0x%08X\n", header.crc32);
-
+
free(file_data);
return 0;
}
perror("fopen input");
return -1;
}
-
+
// 跳过文件头
if (fseek(in_fp, HEADER_SIZE, SEEK_SET)) {
perror("fseek");
fclose(in_fp);
return -1;
}
-
+
FILE *out_fp = fopen(output_path, "wb");
if (!out_fp) {
fclose(in_fp);
perror("fopen output");
return -1;
}
-
+
// 复制内容
uint8_t buffer[4096];
size_t bytes_read;
return -1;
}
}
-
+
if (ferror(in_fp)) {
perror("fread");
fclose(in_fp);
fclose(out_fp);
return -1;
}
-
+
fclose(in_fp);
-
+
if (fclose(out_fp)) {
perror("fclose output");
return -1;
}
-
+
// 设置可执行权限
if (chmod(output_path, 0755)) {
perror("chmod");
return -1;
}
-
+
printf("Application extracted to %s\n", output_path);
return 0;
}
#include <uv.h>
// 全局变量保存程序参数
-char** saved_argv;
-int saved_argc;
+static uv_timer_t app_ota_timer;
+
+void app_ota_reboot( uv_timer_t *handle );
+void app_ota_download_fail( uv_timer_t *handle );
+void app_ota_download_start( uv_timer_t *handle );
+
// 下载进度回调
void download_progress_cb(double percentage, void* userdata) {
}
// 下载完成回调
-void download_complete_cb(int status, void* userdata) {
+void download_complete_cb(int status, void* userdata)
+{
ota_context* ctx = (ota_context*)userdata;
-
+
if (!ctx) {
- fprintf(stderr, "错误: 完成回调中缺少上下文\n");
- return;
+ fprintf(stderr, "Err: downlad_cmoplete_cb , ctx ins NULL!\n");
+ goto err;
}
-
- printf("下载完成回调: ctx:%p, 状态:%d, 文件:%s\n",
+
+ printf("download_complete_cb: ctx:%p, st:%d, path:%s\n",
(void*)ctx, status, ctx->temp_path);
-
+
if (status == 0) {
- printf("下载成功,开始验证和安装...\n");
-
+ printf("Download finished, start verify install...\n");
+
if( ctx->req->file_ptr )
- fflush( ctx->req->file_ptr);
+ fflush( ctx->req->file_ptr);
// 验证固件
if (validate_firmware(ctx->temp_path)) {
- fprintf(stderr, "固件验证失败\n");
+ fprintf(stderr, "firmware verfy fail!\n");
unlink(ctx->temp_path);
- free(ctx);
- return;
+ printf("-------free ctx:%p\n", ctx );
+ goto err;
}
-
- printf("固件验证成功,开始安装...\n");
-
- sleep(3);
+
+ printf("Firmware verfy OK, start install....\n");
+
lcd_write_ota_info( 2 );
const char* target_path = "/app/alarmmonitor";
const char* backup_path = "/app/alarmmonitor.bak";
-
+
// 备份当前版本
if (rename(target_path, backup_path)) {
- perror("备份失败");
+ perror("back fw fail!\n");
unlink(ctx->temp_path);
- free(ctx);
- return;
+ printf("-------free ctx:%p\n", ctx );
+ goto err;
}
-
+
// 提取应用程序
if (extract_application(ctx->temp_path, target_path)) {
- fprintf(stderr, "提取失败,恢复备份\n");
+ fprintf(stderr, "Get Fw Fail,restore bk fw\n");
rename(backup_path, target_path);
unlink(ctx->temp_path);
- free(ctx);
- return;
+ printf("-------free ctx:%p\n", ctx );
+ goto err;
}
-
+
// 清理
unlink(ctx->temp_path);
unlink(backup_path);
-
- printf("固件升级成功,重启应用程序\n");
- sleep(3);
- lcd_write_ota_info( 0 );
-
+
+ printf("update OK,restart app...\n");
+ uv_timer_start( &app_ota_timer, app_ota_reboot, 5000, 0);
// 重启应用程序
- execv(target_path, ctx->saved_argv);
- perror("execv 失败");
- exit(EXIT_FAILURE);
+ //execv(target_path, ctx->saved_argv);
+ //perror("execv Fail!");
+ //exit(EXIT_FAILURE);
} else {
- fprintf(stderr, "下载失败,清理临时文件\n");
-
+ fprintf(stderr, "Download Fail, clean tmp files\n");
+
// 删除无效文件
if (access(ctx->temp_path, F_OK) == 0) {
if (unlink(ctx->temp_path)) {
- perror("删除临时文件失败");
+ perror("Clean tmp files ... Fail!");
} else {
- printf("已删除无效文件: %s\n", ctx->temp_path);
+ printf("deleted tmp file: %s\n", ctx->temp_path);
}
}
+ uv_timer_start( &app_ota_timer, app_ota_download_fail, 2000, 0);
}
-
+
// 清理上下文
+ printf( "free ctx:%p\n", ctx );
free(ctx);
-
- sleep(3);
+ return;
+err:
+ printf( "ota download err, clean lcd disp\n" );
lcd_write_ota_info( 0 );
+ if( ctx )
+ free(ctx);
+ return;
}
-int app_update( char *url )
+void app_ota_init( void )
+{
+ static int init_flag = 0;
+
+ if( init_flag==0 ){
+
+ uv_timer_init( uv_default_loop(), &app_ota_timer);
+ init_flag=1;
+ }
+}
+
+int app_ota_start( char *url )
{
+ app_ota_init();
// 初始化下载器
download_init(uv_default_loop());
- // 创建临时文件路径 (使用PID确保唯一性)
- char temp_path[256];
- snprintf(temp_path, sizeof(temp_path), "/tmp/firmware_%d.bin", getpid());
-
// 创建OTA上下文
ota_context* ctx = calloc(1, sizeof(ota_context));
if (!ctx) {
- fprintf(stderr, "分配OTA上下文失败\n");
+ fprintf(stderr, "alloc OTA ctx fail!\n");
return EXIT_FAILURE;
+ }else{
+ printf( "-------------------------ctx calloc: %p\n", ctx );
}
- snprintf(ctx->temp_path, sizeof(ctx->temp_path)-1, "/tmp/firmware_%d.bin", getpid());
+ // 创建临时文件路径 (使用PID确保唯一性)
+ snprintf(ctx->temp_path, sizeof(ctx->temp_path)-1, "/userdata/rfjb_app_firmware_%d.bin", getpid());
+ strncpy(ctx->url, url, sizeof(ctx->url)-1 );
- printf("开始下载: %s -> %s\n", url, temp_path);
+ printf("download start: %s -> %s\n", ctx->url, ctx->temp_path);
// 开始下载
lcd_write_ota_info( 1 );
+ app_ota_timer.data = ctx;
+ uv_timer_start( &app_ota_timer, app_ota_download_start, 2000, 0);
+ return 0;
+}
+
+void app_ota_download_start( uv_timer_t *handle )
+{
+ ota_context* ctx = ( ota_context*)handle->data;
+
download_request* req = download_file(
uv_default_loop(),
- url,
- temp_path,
+ ctx->url,
+ ctx->temp_path,
download_progress_cb,
download_complete_cb,
ctx
ctx->req = req;
if (!req) {
- fprintf(stderr, "启动下载失败\n");
+ fprintf(stderr, "download start ... Fail, free ctx:%p!\n", ctx);
free(ctx);
- return EXIT_FAILURE;
}
+}
+void app_ota_reboot( uv_timer_t *handle )
+{
+ ota_context* ctx = ( ota_context*)handle->data;
+ const char* target_path = "/app/alarmmonitor";
+ char * target_argv[] = { NULL };
+ lcd_write_ota_info( 0 );
+ execv(target_path, target_argv );
+ perror("execv Fail!");
+ exit(EXIT_FAILURE);
+}
- return EXIT_SUCCESS;
+void app_ota_download_fail( uv_timer_t *handle )
+{
+ ota_context* ctx = ( ota_context*)handle->data;
+
+ printf( "ota: app download fail!\n" );
+ lcd_write_ota_info( 0 );
}
int ota_start( char * http_url )
{
+ char cmd[256];
+
if( strstr( http_url, "rfjb_app" )){
- app_update( http_url);
+ app_ota_start( http_url);
return 0;
+ }else{
+ sprintf( cmd, "echo %s > /userdata/ota_req.txt", http_url );
+ system( cmd );
+ ota_prompt_start();
}
- char cmd[256];
- sprintf( cmd, "echo %s > /userdata/ota_req.txt", http_url );
- system( cmd );
- ota_prompt_start();
return 0;
}
+