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 <stdio.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
// 外部函数声明
@@ -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;
// Acquire global flash lock — held until END or ABORT releases it
swlock_acquire(&flash_lock);
if (dfu_flash_cmd_init() != 0) {
debug_printf("Firmware upgrade error: flash init failed\n");
swlock_release(&flash_lock);
response[0] = STATUS_FAIL;
send_firmware_upgrade_response(FIRMWARE_UPGRADE_START, response, 1);
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");
dfu_flash_cmd_deinit();
g_upgrade_status.state = UPGRADE_IDLE;
swlock_release(&flash_lock);
response[0] = STATUS_FAIL;
send_firmware_upgrade_response(FIRMWARE_UPGRADE_START, response, 1);
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");
g_upgrade_status.state = UPGRADE_ERROR;
g_upgrade_status.error_code = STATUS_FAIL;
swlock_release(&flash_lock);
response[0] = STATUS_FAIL;
send_firmware_upgrade_response(FIRMWARE_UPGRADE_END, response, 1);
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");
g_firmware_upgrade_mcu_notify = 2;
g_in_fw_upgrade = 0;
swlock_release(&flash_lock);
g_upgrade_status.state = UPGRADE_IDLE;
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);
g_firmware_upgrade_mcu_notify = 3;
g_in_fw_upgrade = 0;
swlock_release(&flash_lock);
g_upgrade_status.state = UPGRADE_IDLE;
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");
swlock_acquire(&flash_lock);
if (dfu_flash_cmd_init() != 0) {
debug_printf("Firmware upgrade error: flash init failed\n");
swlock_release(&flash_lock);
response[0] = STATUS_FAIL;
send_firmware_upgrade_response(FIRMWARE_UPGRADE_ERASE, response, 1);
return STATUS_FAIL;
@@ -397,6 +412,7 @@ unsigned char handle_firmware_upgrade_erase(uint8_t data[], uint16_t len)
}
dfu_flash_cmd_deinit();
swlock_release(&flash_lock);
response[0] = STATUS_SUCCESS;
send_firmware_upgrade_response(FIRMWARE_UPGRADE_ERASE, response, 1);

View File

@@ -16,6 +16,10 @@
#include "aizip_dnr.h"
#include "debug_print.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
//static agc_stage_ctx_t DWORD_ALIGNED agc_stage_state = {};
@@ -81,7 +85,9 @@ void Aizip_DNR_init(void)
delay_microseconds(10000);
// setFlashPortPins(XS1_PORT_1B,XS1_PORT_1C,XS1_PORT_4B, XS1_CLKBLK_3);
swlock_acquire(&flash_lock);
int sta = AI_DNR_init(&pram);
swlock_release(&flash_lock);
g_dnr_init_flag = 1;
debug_printf("AI_DNR_init status %d\n", sta);
}

View File

@@ -23,6 +23,10 @@
#include "dnr_dsp_buf.h"
#include "roleswitchflag.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
extern void save_value(unsigned char *path, unsigned char value);
extern unsigned char load_value(unsigned char *path);
@@ -105,7 +109,9 @@ void key_receiver(chanend_t c)
// tile0
// load the license key
// This function must be called before audio_ex3d_activate_key.
swlock_acquire(&flash_lock);
audio_ex3d_load_key(c);
swlock_release(&flash_lock);
SET_SHARED_GLOBAL(g_ex3d_key_verified, 1);
}

View File

@@ -14,7 +14,10 @@
// variables used by the filesystem
lfs_t lfs;
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;
#define FLASH_CLKBLK XS1_CLKBLK_3
@@ -91,10 +94,11 @@ int lfs_init(void) {
// 固件升级期间禁止LFS操作避免与DFU flash访问冲突
if (g_in_fw_upgrade) {
debug_printf("lfs_init: blocked during firmware upgrade\n");
lfs_session_active = 0;
return -2;
}
#endif
swlock_acquire(&lfs_lock);
swlock_acquire(&flash_lock);
lfs_session_active = 1;
rtos_qspi_flash_init(
qspi_flash_ctx,
@@ -117,7 +121,7 @@ int lfs_init(void) {
if (err) {
debug_printf("lfs_init: format+remount failed, err=%d\n", err);
lfs_session_active = 0;
swlock_release(&lfs_lock);
swlock_release(&flash_lock);
return -1;
}
// Format succeeded and FS is now mounted — return 0 so callers
@@ -136,7 +140,7 @@ void lfs_deinit(void) {
}
lfs_unmount(&lfs);
lfs_session_active = 0;
swlock_release(&lfs_lock);
swlock_release(&flash_lock);
}
int lfs_format_all(void) {
@@ -153,7 +157,7 @@ int lfs_format_all(void) {
// Even if format fails, we must cleanup the session
lfs_session_active = 0;
swlock_release(&lfs_lock);
swlock_release(&flash_lock);
return err;
}

View File

@@ -6,6 +6,10 @@
#include <xcore/channel.h>
#include "hmac.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_DISABLE_COMMAND (0x04)
@@ -173,9 +177,12 @@ void flash_opt_unlock(void)
uint8_t sr1, sr2, sr3;
uint8_t did[3];
swlock_acquire(&flash_lock);
/* Reason: 使用 tile0 端口连接 Flash保持原有逻辑 */
if (flash_opt_enable_ports(&p_opt_tile0) == 0) {
debug_printf("flash_opt_unlock failed\n");
swlock_release(&flash_lock);
return;
}
@@ -233,6 +240,7 @@ void flash_opt_unlock(void)
}
flash_opt_disable_ports();
swlock_release(&flash_lock);
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 ret = 0;
swlock_acquire(&flash_lock);
if (flash_opt_enable_ports(&p_opt_tile0) == 0)
{
swlock_release(&flash_lock);
return 0;
}
@@ -327,6 +337,7 @@ uint8_t opt_key_read(uint8_t key[], unsigned offset)
key[i] = read_bin[i + offset * 20 + 1];
}
flash_opt_disable_ports();
swlock_release(&flash_lock);
return 1;
}
@@ -419,8 +430,10 @@ uint8_t key_validate(uint8_t offset)
uint8_t did[4];
uint8_t hmac_bin[20];
swlock_acquire(&flash_lock);
if (flash_opt_enable_ports(&p_opt_tile0) == 0)
{
swlock_release(&flash_lock);
return 0;
}
@@ -437,19 +450,23 @@ uint8_t key_validate(uint8_t offset)
}
flash_opt_disable_ports();
swlock_release(&flash_lock);
return ret;
}
void read_uid_did(uint8_t uid[])
{
swlock_acquire(&flash_lock);
if (flash_opt_enable_ports(&p_opt_tile0) == 0)
{
swlock_release(&flash_lock);
return;
}
flash_opt_read_uid(uid, 20);
flash_opt_read_did(uid, 3);
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)
{
uint8_t ret = 0;
swlock_acquire(&flash_lock);
switch (buffer[2])
{
case 1:
@@ -665,5 +683,6 @@ void program_key(uint8_t *buffer, int datalength)
case 11:
break;
}
swlock_release(&flash_lock);
}
}