add more flash lock

This commit is contained in:
Steven Dan
2026-04-12 13:30:21 +08:00
parent ed23a1d16f
commit c2da1caca9
5 changed files with 56 additions and 5 deletions

View File

@@ -8,6 +8,10 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include "debug_print.h" #include "debug_print.h"
#include "swlock.h"
// Global flash lock defined in lfs_services.c — protects all flash hardware access
extern swlock_t flash_lock;
#if HID_DFU_EN #if HID_DFU_EN
// 外部函数声明 // 外部函数声明
@@ -100,8 +104,12 @@ unsigned char handle_firmware_upgrade_start(uint8_t data[], uint16_t len)
uint32_t aligned_size = ((firmware_size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE) * FLASH_PAGE_SIZE; uint32_t aligned_size = ((firmware_size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE) * FLASH_PAGE_SIZE;
// Acquire global flash lock — held until END or ABORT releases it
swlock_acquire(&flash_lock);
if (dfu_flash_cmd_init() != 0) { if (dfu_flash_cmd_init() != 0) {
debug_printf("Firmware upgrade error: flash init failed\n"); debug_printf("Firmware upgrade error: flash init failed\n");
swlock_release(&flash_lock);
response[0] = STATUS_FAIL; response[0] = STATUS_FAIL;
send_firmware_upgrade_response(FIRMWARE_UPGRADE_START, response, 1); send_firmware_upgrade_response(FIRMWARE_UPGRADE_START, response, 1);
return STATUS_FAIL; return STATUS_FAIL;
@@ -136,6 +144,7 @@ unsigned char handle_firmware_upgrade_start(uint8_t data[], uint16_t len)
debug_printf("Firmware upgrade error: first page write command failed (begin_write error)\n"); debug_printf("Firmware upgrade error: first page write command failed (begin_write error)\n");
dfu_flash_cmd_deinit(); dfu_flash_cmd_deinit();
g_upgrade_status.state = UPGRADE_IDLE; g_upgrade_status.state = UPGRADE_IDLE;
swlock_release(&flash_lock);
response[0] = STATUS_FAIL; response[0] = STATUS_FAIL;
send_firmware_upgrade_response(FIRMWARE_UPGRADE_START, response, 1); send_firmware_upgrade_response(FIRMWARE_UPGRADE_START, response, 1);
return STATUS_FAIL; return STATUS_FAIL;
@@ -301,6 +310,7 @@ unsigned char handle_firmware_upgrade_end(uint8_t data[], uint16_t len)
debug_printf("Firmware upgrade error: flash write termination failed\n"); debug_printf("Firmware upgrade error: flash write termination failed\n");
g_upgrade_status.state = UPGRADE_ERROR; g_upgrade_status.state = UPGRADE_ERROR;
g_upgrade_status.error_code = STATUS_FAIL; g_upgrade_status.error_code = STATUS_FAIL;
swlock_release(&flash_lock);
response[0] = STATUS_FAIL; response[0] = STATUS_FAIL;
send_firmware_upgrade_response(FIRMWARE_UPGRADE_END, response, 1); send_firmware_upgrade_response(FIRMWARE_UPGRADE_END, response, 1);
return STATUS_FAIL; return STATUS_FAIL;
@@ -314,6 +324,7 @@ unsigned char handle_firmware_upgrade_end(uint8_t data[], uint16_t len)
debug_printf("Firmware upgrade completed successfully\n"); debug_printf("Firmware upgrade completed successfully\n");
g_firmware_upgrade_mcu_notify = 2; g_firmware_upgrade_mcu_notify = 2;
g_in_fw_upgrade = 0; g_in_fw_upgrade = 0;
swlock_release(&flash_lock);
g_upgrade_status.state = UPGRADE_IDLE; g_upgrade_status.state = UPGRADE_IDLE;
return STATUS_SUCCESS; return STATUS_SUCCESS;
} }
@@ -371,6 +382,7 @@ unsigned char handle_firmware_upgrade_abort(uint8_t data[], uint16_t len)
send_firmware_upgrade_response(FIRMWARE_UPGRADE_ABORT, response, 1); send_firmware_upgrade_response(FIRMWARE_UPGRADE_ABORT, response, 1);
g_firmware_upgrade_mcu_notify = 3; g_firmware_upgrade_mcu_notify = 3;
g_in_fw_upgrade = 0; g_in_fw_upgrade = 0;
swlock_release(&flash_lock);
g_upgrade_status.state = UPGRADE_IDLE; g_upgrade_status.state = UPGRADE_IDLE;
return STATUS_SUCCESS; return STATUS_SUCCESS;
} }
@@ -382,8 +394,11 @@ unsigned char handle_firmware_upgrade_erase(uint8_t data[], uint16_t len)
debug_printf("Firmware upgrade ERASE: erasing all upgrade images\n"); debug_printf("Firmware upgrade ERASE: erasing all upgrade images\n");
swlock_acquire(&flash_lock);
if (dfu_flash_cmd_init() != 0) { if (dfu_flash_cmd_init() != 0) {
debug_printf("Firmware upgrade error: flash init failed\n"); debug_printf("Firmware upgrade error: flash init failed\n");
swlock_release(&flash_lock);
response[0] = STATUS_FAIL; response[0] = STATUS_FAIL;
send_firmware_upgrade_response(FIRMWARE_UPGRADE_ERASE, response, 1); send_firmware_upgrade_response(FIRMWARE_UPGRADE_ERASE, response, 1);
return STATUS_FAIL; return STATUS_FAIL;
@@ -397,6 +412,7 @@ unsigned char handle_firmware_upgrade_erase(uint8_t data[], uint16_t len)
} }
dfu_flash_cmd_deinit(); dfu_flash_cmd_deinit();
swlock_release(&flash_lock);
response[0] = STATUS_SUCCESS; response[0] = STATUS_SUCCESS;
send_firmware_upgrade_response(FIRMWARE_UPGRADE_ERASE, response, 1); send_firmware_upgrade_response(FIRMWARE_UPGRADE_ERASE, response, 1);

View File

@@ -16,6 +16,10 @@
#include "aizip_dnr.h" #include "aizip_dnr.h"
#include "debug_print.h" #include "debug_print.h"
#include "flash.h" #include "flash.h"
#include "swlock.h"
// Global flash lock defined in lfs_services.c — protects all flash hardware access
extern swlock_t flash_lock;
//for AGC //for AGC
//static agc_stage_ctx_t DWORD_ALIGNED agc_stage_state = {}; //static agc_stage_ctx_t DWORD_ALIGNED agc_stage_state = {};
@@ -81,7 +85,9 @@ void Aizip_DNR_init(void)
delay_microseconds(10000); delay_microseconds(10000);
// setFlashPortPins(XS1_PORT_1B,XS1_PORT_1C,XS1_PORT_4B, XS1_CLKBLK_3); // setFlashPortPins(XS1_PORT_1B,XS1_PORT_1C,XS1_PORT_4B, XS1_CLKBLK_3);
swlock_acquire(&flash_lock);
int sta = AI_DNR_init(&pram); int sta = AI_DNR_init(&pram);
swlock_release(&flash_lock);
g_dnr_init_flag = 1; g_dnr_init_flag = 1;
debug_printf("AI_DNR_init status %d\n", sta); debug_printf("AI_DNR_init status %d\n", sta);
} }

View File

@@ -23,6 +23,10 @@
#include "dnr_dsp_buf.h" #include "dnr_dsp_buf.h"
#include "roleswitchflag.h" #include "roleswitchflag.h"
#include "debug_print.h" #include "debug_print.h"
#include "swlock.h"
// Global flash lock defined in lfs_services.c — protects all flash hardware access
extern swlock_t flash_lock;
// 改动原因添加user_func.h头文件用于调用save_value和load_value函数保存/加载模式到/从flash // 改动原因添加user_func.h头文件用于调用save_value和load_value函数保存/加载模式到/从flash
extern void save_value(unsigned char *path, unsigned char value); extern void save_value(unsigned char *path, unsigned char value);
extern unsigned char load_value(unsigned char *path); extern unsigned char load_value(unsigned char *path);
@@ -105,7 +109,9 @@ void key_receiver(chanend_t c)
// tile0 // tile0
// load the license key // load the license key
// This function must be called before audio_ex3d_activate_key. // This function must be called before audio_ex3d_activate_key.
swlock_acquire(&flash_lock);
audio_ex3d_load_key(c); audio_ex3d_load_key(c);
swlock_release(&flash_lock);
SET_SHARED_GLOBAL(g_ex3d_key_verified, 1); SET_SHARED_GLOBAL(g_ex3d_key_verified, 1);
} }

View File

@@ -14,7 +14,10 @@
// variables used by the filesystem // variables used by the filesystem
lfs_t lfs; lfs_t lfs;
lfs_file_t file; lfs_file_t file;
swlock_t lfs_lock = SWLOCK_INITIAL_VALUE;
// Global flash lock: protects ALL flash hardware access (LFS, DFU, key programming).
// Only one core may perform flash I/O at a time.
swlock_t flash_lock = SWLOCK_INITIAL_VALUE;
static rtos_qspi_flash_t qspi_flash_ctx_s; static rtos_qspi_flash_t qspi_flash_ctx_s;
#define FLASH_CLKBLK XS1_CLKBLK_3 #define FLASH_CLKBLK XS1_CLKBLK_3
@@ -91,10 +94,11 @@ int lfs_init(void) {
// 固件升级期间禁止LFS操作避免与DFU flash访问冲突 // 固件升级期间禁止LFS操作避免与DFU flash访问冲突
if (g_in_fw_upgrade) { if (g_in_fw_upgrade) {
debug_printf("lfs_init: blocked during firmware upgrade\n"); debug_printf("lfs_init: blocked during firmware upgrade\n");
lfs_session_active = 0;
return -2; return -2;
} }
#endif #endif
swlock_acquire(&lfs_lock); swlock_acquire(&flash_lock);
lfs_session_active = 1; lfs_session_active = 1;
rtos_qspi_flash_init( rtos_qspi_flash_init(
qspi_flash_ctx, qspi_flash_ctx,
@@ -117,7 +121,7 @@ int lfs_init(void) {
if (err) { if (err) {
debug_printf("lfs_init: format+remount failed, err=%d\n", err); debug_printf("lfs_init: format+remount failed, err=%d\n", err);
lfs_session_active = 0; lfs_session_active = 0;
swlock_release(&lfs_lock); swlock_release(&flash_lock);
return -1; return -1;
} }
// Format succeeded and FS is now mounted — return 0 so callers // Format succeeded and FS is now mounted — return 0 so callers
@@ -136,7 +140,7 @@ void lfs_deinit(void) {
} }
lfs_unmount(&lfs); lfs_unmount(&lfs);
lfs_session_active = 0; lfs_session_active = 0;
swlock_release(&lfs_lock); swlock_release(&flash_lock);
} }
int lfs_format_all(void) { int lfs_format_all(void) {
@@ -153,7 +157,7 @@ int lfs_format_all(void) {
// Even if format fails, we must cleanup the session // Even if format fails, we must cleanup the session
lfs_session_active = 0; lfs_session_active = 0;
swlock_release(&lfs_lock); swlock_release(&flash_lock);
return err; return err;
} }

View File

@@ -6,6 +6,10 @@
#include <xcore/channel.h> #include <xcore/channel.h>
#include "hmac.h" #include "hmac.h"
#include "xua_conf.h" #include "xua_conf.h"
#include "swlock.h"
// Global flash lock defined in lfs_services.c — protects all flash hardware access
extern swlock_t flash_lock;
#define WRITE_ENABLE_COMMAND (0x06) #define WRITE_ENABLE_COMMAND (0x06)
#define WRITE_DISABLE_COMMAND (0x04) #define WRITE_DISABLE_COMMAND (0x04)
@@ -173,9 +177,12 @@ void flash_opt_unlock(void)
uint8_t sr1, sr2, sr3; uint8_t sr1, sr2, sr3;
uint8_t did[3]; uint8_t did[3];
swlock_acquire(&flash_lock);
/* Reason: 使用 tile0 端口连接 Flash保持原有逻辑 */ /* Reason: 使用 tile0 端口连接 Flash保持原有逻辑 */
if (flash_opt_enable_ports(&p_opt_tile0) == 0) { if (flash_opt_enable_ports(&p_opt_tile0) == 0) {
debug_printf("flash_opt_unlock failed\n"); debug_printf("flash_opt_unlock failed\n");
swlock_release(&flash_lock);
return; return;
} }
@@ -233,6 +240,7 @@ void flash_opt_unlock(void)
} }
flash_opt_disable_ports(); flash_opt_disable_ports();
swlock_release(&flash_lock);
debug_printf("Unlock Winbond Flash done\n"); debug_printf("Unlock Winbond Flash done\n");
} }
@@ -316,8 +324,10 @@ uint8_t opt_key_read(uint8_t key[], unsigned offset)
uint8_t read_bin[KEY_BLOCK_LEN + 1]; uint8_t read_bin[KEY_BLOCK_LEN + 1];
uint8_t ret = 0; uint8_t ret = 0;
swlock_acquire(&flash_lock);
if (flash_opt_enable_ports(&p_opt_tile0) == 0) if (flash_opt_enable_ports(&p_opt_tile0) == 0)
{ {
swlock_release(&flash_lock);
return 0; return 0;
} }
@@ -327,6 +337,7 @@ uint8_t opt_key_read(uint8_t key[], unsigned offset)
key[i] = read_bin[i + offset * 20 + 1]; key[i] = read_bin[i + offset * 20 + 1];
} }
flash_opt_disable_ports(); flash_opt_disable_ports();
swlock_release(&flash_lock);
return 1; return 1;
} }
@@ -419,8 +430,10 @@ uint8_t key_validate(uint8_t offset)
uint8_t did[4]; uint8_t did[4];
uint8_t hmac_bin[20]; uint8_t hmac_bin[20];
swlock_acquire(&flash_lock);
if (flash_opt_enable_ports(&p_opt_tile0) == 0) if (flash_opt_enable_ports(&p_opt_tile0) == 0)
{ {
swlock_release(&flash_lock);
return 0; return 0;
} }
@@ -437,19 +450,23 @@ uint8_t key_validate(uint8_t offset)
} }
flash_opt_disable_ports(); flash_opt_disable_ports();
swlock_release(&flash_lock);
return ret; return ret;
} }
void read_uid_did(uint8_t uid[]) void read_uid_did(uint8_t uid[])
{ {
swlock_acquire(&flash_lock);
if (flash_opt_enable_ports(&p_opt_tile0) == 0) if (flash_opt_enable_ports(&p_opt_tile0) == 0)
{ {
swlock_release(&flash_lock);
return; return;
} }
flash_opt_read_uid(uid, 20); flash_opt_read_uid(uid, 20);
flash_opt_read_did(uid, 3); flash_opt_read_did(uid, 3);
flash_opt_disable_ports(); flash_opt_disable_ports();
swlock_release(&flash_lock);
} }
@@ -458,6 +475,7 @@ void program_key(uint8_t *buffer, int datalength)
if (buffer[0] == 0x77 && buffer[1] == 0x5B) if (buffer[0] == 0x77 && buffer[1] == 0x5B)
{ {
uint8_t ret = 0; uint8_t ret = 0;
swlock_acquire(&flash_lock);
switch (buffer[2]) switch (buffer[2])
{ {
case 1: case 1:
@@ -665,5 +683,6 @@ void program_key(uint8_t *buffer, int datalength)
case 11: case 11:
break; break;
} }
swlock_release(&flash_lock);
} }
} }