|
| 1 | +#include <stdio.h> |
| 2 | +#include <string.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <unistd.h> |
| 5 | + |
| 6 | +#include <errno.h> |
| 7 | +#include <hal/ota.h> |
| 8 | +#include <aos/log.h> |
| 9 | +#include <hal/soc/soc.h> |
| 10 | + |
| 11 | +#include "rda_def.h" |
| 12 | +#include "rda_flash.h" |
| 13 | + |
| 14 | +r_u32 g_crc_result = ~0UL; |
| 15 | +r_u32 g_ota_off_set = 0; |
| 16 | + |
| 17 | +#define CRC32_TABLE_ADDR 0xbb5c |
| 18 | +#define CRC32_ADDR 0x8dff |
| 19 | +#define CRC32_TABLE_ADDR_4 0xbbd8 |
| 20 | +#define CRC32_ADDR_4 0x8e33 |
| 21 | +#define IMAGE_MAGIC 0xAEAE |
| 22 | + |
| 23 | +static r_u32 bootrom_crc32(r_u8 *p, r_u32 len) |
| 24 | +{ |
| 25 | + r_u32 func = CRC32_ADDR; |
| 26 | + if (rda_ccfg_hwver() >= 4) { |
| 27 | + func = CRC32_ADDR_4; |
| 28 | + } |
| 29 | + return ((r_u32(*)(r_u8 *, r_u32))func)(p, len); |
| 30 | +} |
| 31 | + |
| 32 | +static r_u32 crc32(const r_u8 *p, r_u32 len, r_u32 crc) |
| 33 | +{ |
| 34 | + const r_u32 *crc32_tab = (const r_u32 *)CRC32_TABLE_ADDR; |
| 35 | + |
| 36 | + if (rda_ccfg_hwver() >= 4) { |
| 37 | + crc32_tab = (const r_u32 *)CRC32_TABLE_ADDR_4; |
| 38 | + } |
| 39 | + /* Calculate CRC */ |
| 40 | + while (len--) |
| 41 | + { |
| 42 | + crc = crc32_tab[((crc & 0xFF) ^ *p++)] ^ (crc >> 8); |
| 43 | + } |
| 44 | + |
| 45 | + return crc; |
| 46 | +} |
| 47 | + |
| 48 | +static r_s32 rda59xx_ota_init(hal_ota_module_t *m, void *something) |
| 49 | +{ |
| 50 | + printf("========= OTA init ========= \r\n"); |
| 51 | + g_ota_off_set = *(r_u32*)something; |
| 52 | + hal_partition_t pno = HAL_PARTITION_OTA_TEMP; |
| 53 | + hal_logic_partition_t *partition_info; |
| 54 | + partition_info = hal_flash_get_info(pno); |
| 55 | + //g_ota_partition_len = partition_info->partition_length; |
| 56 | + if (g_ota_off_set == 0) { |
| 57 | + hal_flash_erase(pno, 0, partition_info->partition_length); |
| 58 | + } else { |
| 59 | + printf("OTA init invalid, offset = %d \r\n", g_ota_off_set); |
| 60 | + } |
| 61 | + |
| 62 | + return 0; |
| 63 | +} |
| 64 | + |
| 65 | +static r_s32 rda59xx_ota_write(hal_ota_module_t *m, volatile r_u32* off_set, r_u8* in_buf ,r_u32 in_buf_len) |
| 66 | +{ |
| 67 | + r_s32 ret; |
| 68 | + |
| 69 | + hal_partition_t pno = HAL_PARTITION_OTA_TEMP; |
| 70 | + printf("========= OTA write start: len: %d ==========\r\n", in_buf_len); |
| 71 | + g_crc_result = crc32(in_buf, in_buf_len, g_crc_result); |
| 72 | + ret = hal_flash_write(pno, &g_ota_off_set, in_buf, in_buf_len); |
| 73 | + g_ota_off_set += in_buf_len; |
| 74 | + return ret; |
| 75 | +} |
| 76 | + |
| 77 | +static r_s32 rda59xx_ota_read(hal_ota_module_t *m, volatile r_u32* off_set, r_u8* out_buf, r_u32 out_buf_len) |
| 78 | +{ |
| 79 | + hal_partition_t pno = HAL_PARTITION_OTA_TEMP; |
| 80 | + hal_flash_read(pno, off_set, out_buf, out_buf_len); |
| 81 | + |
| 82 | + return 0; |
| 83 | +} |
| 84 | + |
| 85 | +static r_s32 rda59xx_ota_set_boot(hal_ota_module_t *m, void *something) |
| 86 | +{ |
| 87 | + printf("========= OTA boot set ========= \r\n"); |
| 88 | + hal_logic_partition_t *partition_info; |
| 89 | + hal_partition_t pno = HAL_PARTITION_OTA_TEMP; |
| 90 | + ota_finish_param_t *param = (ota_finish_param_t *)something; |
| 91 | + if (param==NULL) { |
| 92 | + return -1; |
| 93 | + } |
| 94 | + if (param->result_type == OTA_FINISH) { |
| 95 | + partition_info = hal_flash_get_info(pno); |
| 96 | + core_util_critical_section_enter(); |
| 97 | + spi_flash_flush_cache(); |
| 98 | + r_u32 crc32_check = bootrom_crc32((r_u8 *)partition_info->partition_start_addr, g_ota_off_set); |
| 99 | + core_util_critical_section_exit(); |
| 100 | + printf("rda5981_write_partition_end:0x%08x:0x%08x\r\n", g_crc_result, crc32_check); |
| 101 | + if (crc32_check == g_crc_result) { |
| 102 | + hal_reboot(); |
| 103 | + } else { |
| 104 | + printf("ERROR!!! check crc32 error\r\n"); |
| 105 | + } |
| 106 | + } else if (param->result_type==OTA_BREAKPOINT) { |
| 107 | + printf("OTA package is incomplete!\r\n"); |
| 108 | + } |
| 109 | + return 0; |
| 110 | +} |
| 111 | + |
| 112 | +struct hal_ota_module_s rda59xx_ota_module = { |
| 113 | + .init = rda59xx_ota_init, |
| 114 | + .ota_write = rda59xx_ota_write, |
| 115 | + .ota_read = rda59xx_ota_read, |
| 116 | + .ota_set_boot = rda59xx_ota_set_boot, |
| 117 | +}; |
0 commit comments