Clean up for 12.04.01
Esse commit está contido em:
1326
nfc/recovery_fw.c
1326
nfc/recovery_fw.c
Diferenças do arquivo suprimidas por serem muito extensas
Carregar Diff
@@ -1,249 +0,0 @@
|
||||
/******************************************************************************
|
||||
* Copyright (C) 2021 NXP
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
******************************************************************************/
|
||||
#include "recovery_seq.h"
|
||||
|
||||
struct recovery_info g_recovery_info;
|
||||
struct recovery_frame g_recovery_frame;
|
||||
|
||||
/** @brief Function to calculate crc value.
|
||||
*
|
||||
* @param pbuffer: input buffer for crc calculation.
|
||||
* dwLength: length of input buffer
|
||||
* @return calculated uint16_t crc valueof input buffer.
|
||||
*/
|
||||
static uint16_t calcCrc16(uint8_t *pbuffer, uint32_t dwLength)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
uint16_t crc_new = 0;
|
||||
uint16_t crc = DL_INVALID_CRC_VALUE;
|
||||
|
||||
if (pbuffer == NULL) {
|
||||
pr_err("%s, invalid params", __func__);
|
||||
return crc;
|
||||
}
|
||||
for (i = 0; i < dwLength; i++) {
|
||||
crc_new = (uint8_t)(crc >> MSB_POS) | (crc << MSB_POS);
|
||||
crc_new ^= pbuffer[i];
|
||||
crc_new ^= (uint8_t)(crc_new & DL_CRC_MASK) >> 4;
|
||||
crc_new ^= crc_new << 12;
|
||||
crc_new ^= (crc_new & DL_CRC_MASK) << 5;
|
||||
crc = crc_new;
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
/** @brief Function to build command frame for recover.
|
||||
*
|
||||
* @return status code of recovery_status type.
|
||||
*/
|
||||
static enum recovery_status build_cmd_frame(void)
|
||||
{
|
||||
uint16_t len = 0;
|
||||
uint16_t wCrc = 0;
|
||||
uint16_t writeOffset = 0;
|
||||
|
||||
pr_debug(" %s Entry", __func__);
|
||||
if (gphDnldNfc_DlSeqSz == 0) {
|
||||
pr_err(" %s invalid params", __func__);
|
||||
return STATUS_FAILED;
|
||||
}
|
||||
memset(g_recovery_frame.p_buffer, 0x00, MAX_FRAME_SIZE);
|
||||
g_recovery_frame.len = 0;
|
||||
if (g_recovery_info.bFrameSegmented == false) {
|
||||
len = gphDnldNfc_DlSequence[g_recovery_info.currentReadOffset];
|
||||
len <<= MSB_POS;
|
||||
len |= gphDnldNfc_DlSequence[g_recovery_info.currentReadOffset + 1];
|
||||
} else {
|
||||
/* last frame was segmented frame
|
||||
* read length reamaining length
|
||||
*/
|
||||
len = g_recovery_info.wRemChunkBytes;
|
||||
}
|
||||
if (len > MAX_DATA_SIZE) {
|
||||
/* set remaining chunk */
|
||||
g_recovery_info.wRemChunkBytes = (len - MAX_DATA_SIZE);
|
||||
len = MAX_DATA_SIZE;
|
||||
/* set chunk bit to write in header */
|
||||
len = DL_SET_HDR_FRAGBIT(len);
|
||||
g_recovery_frame.p_buffer[writeOffset++] = (len >> MSB_POS) & SHIFT_MASK;
|
||||
g_recovery_frame.p_buffer[writeOffset++] = len & SHIFT_MASK;
|
||||
/* clear chunk bit for length variable */
|
||||
len = DL_CLR_HDR_FRAGBIT(len);
|
||||
/* first chunk of segmented frame*/
|
||||
if (!g_recovery_info.bFrameSegmented) {
|
||||
/* ignore header from user buffer */
|
||||
g_recovery_info.currentReadOffset += FW_HDR_LEN;
|
||||
g_recovery_info.remBytes -= FW_HDR_LEN;
|
||||
}
|
||||
g_recovery_frame.len += FW_HDR_LEN;
|
||||
g_recovery_info.bFrameSegmented = true;
|
||||
} else {
|
||||
/* last chunk of segmented frame */
|
||||
if (g_recovery_info.bFrameSegmented) {
|
||||
/* write header with user chunk length */
|
||||
g_recovery_frame.p_buffer[writeOffset++] = (len >> MSB_POS) & SHIFT_MASK;
|
||||
g_recovery_frame.p_buffer[writeOffset++] = len & SHIFT_MASK;
|
||||
g_recovery_frame.len += FW_HDR_LEN;
|
||||
} else {
|
||||
/* normal Frame with in supported size increase
|
||||
* len to read header from user data
|
||||
*/
|
||||
len += FW_HDR_LEN;
|
||||
}
|
||||
g_recovery_info.wRemChunkBytes = 0;
|
||||
g_recovery_info.bFrameSegmented = false;
|
||||
}
|
||||
if (((writeOffset + len) > MAX_FRAME_SIZE) ||
|
||||
((g_recovery_info.currentReadOffset + len) > gphDnldNfc_DlSeqSz)) {
|
||||
pr_err("%s frame offsets out of bound", __func__);
|
||||
return STATUS_FAILED;
|
||||
}
|
||||
memcpy(&g_recovery_frame.p_buffer[writeOffset],
|
||||
&gphDnldNfc_DlSequence[g_recovery_info.currentReadOffset], len);
|
||||
g_recovery_info.currentReadOffset += len;
|
||||
g_recovery_frame.len += len;
|
||||
writeOffset += len;
|
||||
g_recovery_info.remBytes -= len;
|
||||
wCrc = calcCrc16(g_recovery_frame.p_buffer,
|
||||
g_recovery_frame.len);
|
||||
g_recovery_frame.p_buffer[writeOffset++] = (wCrc >> MSB_POS) & SHIFT_MASK;
|
||||
g_recovery_frame.p_buffer[writeOffset++] = wCrc & SHIFT_MASK;
|
||||
g_recovery_frame.len += FW_CRC_LEN;
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
/** @brief Function to transmit recovery frame.
|
||||
* @param nfc_dev nfc driver object.
|
||||
* @return status code of recovery_status type.
|
||||
*/
|
||||
static enum recovery_status transmit(struct nfc_dev *nfc_dev)
|
||||
{
|
||||
int ret = 0;
|
||||
int frame_resp_len = 0;
|
||||
uint16_t respCRC = 0;
|
||||
uint16_t respCRCOffset = 0;
|
||||
uint8_t *rsp_buf = nfc_dev->read_kbuf;
|
||||
|
||||
pr_debug("%s Entry", __func__);
|
||||
if (nfc_dev == NULL || g_recovery_frame.len <= 0) {
|
||||
pr_err("%s invalid Params", __func__);
|
||||
return STATUS_FAILED;
|
||||
}
|
||||
ret = nfc_dev->nfc_write(nfc_dev, g_recovery_frame.p_buffer,
|
||||
g_recovery_frame.len, MAX_RETRY_COUNT);
|
||||
if (ret <= 0) {
|
||||
pr_err(" %s: Write recovery frame error %d\n", __func__, ret);
|
||||
return STATUS_FAILED;
|
||||
}
|
||||
pr_debug(" %s Reading response\n", __func__);
|
||||
memset(rsp_buf, 0x00, MAX_BUFFER_SIZE);
|
||||
ret = nfc_dev->nfc_read(nfc_dev, rsp_buf, FW_HDR_LEN, NCI_CMD_RSP_TIMEOUT);
|
||||
if (ret < FW_HDR_LEN) {
|
||||
pr_err(" %s - Read recovery frame response error ret %d\n", __func__, ret);
|
||||
return STATUS_FAILED;
|
||||
}
|
||||
if (rsp_buf[0] != FW_MSG_CMD_RSP ||
|
||||
rsp_buf[DL_FRAME_RESP_LEN_OFFSET] != DL_FRAME_RESP_LEN) {
|
||||
pr_err("%s, invalid response", __func__);
|
||||
return STATUS_FAILED;
|
||||
}
|
||||
frame_resp_len = rsp_buf[DL_FRAME_RESP_LEN_OFFSET] + FW_CRC_LEN;
|
||||
ret = nfc_dev->nfc_read(nfc_dev, rsp_buf + FW_HDR_LEN, frame_resp_len, NCI_CMD_RSP_TIMEOUT);
|
||||
if (ret < frame_resp_len) {
|
||||
pr_err(" %s - Read recovery frame response error ret %d\n", __func__, ret);
|
||||
return STATUS_FAILED;
|
||||
}
|
||||
pr_debug(" %s: recovery frame Response 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",
|
||||
__func__, rsp_buf[0], rsp_buf[1], rsp_buf[2], rsp_buf[3], rsp_buf[4], rsp_buf[5]);
|
||||
respCRCOffset = FW_HDR_LEN + rsp_buf[DL_FRAME_RESP_LEN_OFFSET];
|
||||
respCRC = rsp_buf[respCRCOffset++];
|
||||
respCRC <<= MSB_POS;
|
||||
respCRC |= rsp_buf[respCRCOffset];
|
||||
if (respCRC != calcCrc16(rsp_buf, DL_FRAME_RESP_LEN + FW_HDR_LEN)) {
|
||||
pr_err("%s, invalid response crc", __func__);
|
||||
return STATUS_FAILED;
|
||||
}
|
||||
if (g_recovery_info.bFrameSegmented &&
|
||||
(rsp_buf[DL_FRAME_RESP_STAT_OFFSET] != DL_SEGMENTED_FRAME_RESP_STAT1
|
||||
&& rsp_buf[DL_FRAME_RESP_STAT_OFFSET] != DL_SEGMENTED_FRAME_RESP_STAT2)) {
|
||||
pr_err("%s, invalid stat flag in chunk response", __func__);
|
||||
return STATUS_FAILED;
|
||||
}
|
||||
if (!g_recovery_info.bFrameSegmented &&
|
||||
rsp_buf[DL_FRAME_RESP_STAT_OFFSET] != DL_NON_SEGMENTED_FRAME_RESP_STAT) {
|
||||
pr_err("%s, invalid stat flag in response", __func__);
|
||||
return STATUS_FAILED;
|
||||
}
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
/** @brief Function to check input version with recovery fw version.
|
||||
* @param fw_major_version: input major_version to check.
|
||||
* @return true if input major_version matches with recovery fw major version
|
||||
* otherwise returns false.
|
||||
*/
|
||||
static bool check_major_version(uint8_t fw_major_version)
|
||||
{
|
||||
if (gphDnldNfc_DlSeqSz < RECOVERY_FW_MJ_VER_OFFSET) {
|
||||
/* Recovery data corrupted */
|
||||
pr_err("%s Not able to extract major version from recovery fw\n", __func__);
|
||||
return false;
|
||||
}
|
||||
return (fw_major_version == gphDnldNfc_DlSequence[RECOVERY_FW_MJ_VER_OFFSET]);
|
||||
}
|
||||
|
||||
/** @brief Function to recover the nfcc.
|
||||
* @param nfc_dev nfc driver object.
|
||||
* @return status code of type recovery_status.
|
||||
*/
|
||||
enum recovery_status do_recovery(struct nfc_dev *nfc_dev)
|
||||
{
|
||||
enum recovery_status status = STATUS_SUCCESS;
|
||||
|
||||
g_recovery_info.remBytes = gphDnldNfc_DlSeqSz;
|
||||
g_recovery_info.currentReadOffset = 0;
|
||||
g_recovery_info.bFrameSegmented = false;
|
||||
g_recovery_info.wRemChunkBytes = 0;
|
||||
g_recovery_frame.p_buffer = nfc_dev->write_kbuf;
|
||||
pr_debug("%s Entry", __func__);
|
||||
if (nfc_dev == NULL) {
|
||||
pr_err("%s invalid params ", __func__);
|
||||
return STATUS_FAILED;
|
||||
}
|
||||
if (!(check_major_version(nfc_dev->fw_major_version))) {
|
||||
pr_err("%s unsupported version", __func__);
|
||||
status = STATUS_FAILED;
|
||||
goto EXIT_RECOVERY;
|
||||
}
|
||||
while (g_recovery_info.remBytes > 0) {
|
||||
status = build_cmd_frame();
|
||||
if (status != STATUS_SUCCESS) {
|
||||
pr_err(" %s Unable to create recovery frame");
|
||||
break;
|
||||
}
|
||||
status = transmit(nfc_dev);
|
||||
if (status != STATUS_SUCCESS) {
|
||||
pr_err(" %s Unable to send recovery frame");
|
||||
break;
|
||||
}
|
||||
}
|
||||
EXIT_RECOVERY:
|
||||
pr_info("%s Recovery done status %d", __func__, status);
|
||||
return status;
|
||||
}
|
@@ -1,79 +0,0 @@
|
||||
/******************************************************************************
|
||||
* Copyright (C) 2021 NXP
|
||||
* *
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*
|
||||
******************************************************************************/
|
||||
#if IS_ENABLED(CONFIG_NXP_NFC_RECOVERY)
|
||||
|
||||
#ifndef __RECOVERY_SEQ_H_
|
||||
#define __RECOVERY_SEQ_H_
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#define MAX_FRAME_SIZE 0x22A /* support for 256(0x100) & 554(0x22A) frame size*/
|
||||
|
||||
#define MAX_DATA_SIZE \
|
||||
(MAX_FRAME_SIZE - FW_CRC_LEN - FW_HDR_LEN)
|
||||
|
||||
#define RECOVERY_FW_MJ_VER_OFFSET 5
|
||||
|
||||
#define DL_SET_HDR_FRAGBIT(n) \
|
||||
((n) | (1 << 10)) /* Header chunk bit set macro */
|
||||
#define DL_CLR_HDR_FRAGBIT(n) \
|
||||
((n) & ~(1U << 10)) /* Header chunk bit clear macro */
|
||||
|
||||
#define DL_FRAME_RESP_LEN 0x04
|
||||
#define DL_FRAME_RESP_LEN_OFFSET 1
|
||||
#define DL_FRAME_RESP_STAT_OFFSET 2
|
||||
#define DL_SEGMENTED_FRAME_RESP_STAT1 0x2D
|
||||
#define DL_SEGMENTED_FRAME_RESP_STAT2 0x2E
|
||||
#define DL_NON_SEGMENTED_FRAME_RESP_STAT 0x00
|
||||
#define DL_INVALID_CRC_VALUE 0xffffU
|
||||
#define DL_CRC_MASK 0xff
|
||||
|
||||
#define SHIFT_MASK 0x00FF
|
||||
#define MSB_POS 8
|
||||
|
||||
/* Data buffer for frame to write */
|
||||
struct recovery_frame {
|
||||
uint32_t len;
|
||||
uint8_t *p_buffer;
|
||||
};
|
||||
|
||||
/* Contains Info about user buffer and last data frame */
|
||||
struct recovery_info {
|
||||
uint32_t currentReadOffset; /* current offset within the user buffer to read/write */
|
||||
uint32_t remBytes; /* Remaining bytes to write */
|
||||
uint16_t wRemChunkBytes; /* Remaining bytes within the chunked frame */
|
||||
bool bFrameSegmented; /* Indicates the current frame is segmented */
|
||||
};
|
||||
|
||||
/* indicates the error codes for nfc recovery module */
|
||||
enum recovery_status {
|
||||
STATUS_SUCCESS = 0x00,
|
||||
STATUS_FAILED = 0x01,
|
||||
};
|
||||
|
||||
extern const uint32_t gphDnldNfc_DlSeqSz; /* Recovery user buffer size */
|
||||
extern const uint8_t gphDnldNfc_DlSequence[]; /* Recovery user buffer */
|
||||
|
||||
/** @brief Function to recover the nfcc.
|
||||
* @param nfc_dev nfc driver object.
|
||||
* @return status code of type recovery_status_t.
|
||||
*/
|
||||
enum recovery_status do_recovery(struct nfc_dev *nfc_dev);
|
||||
#endif// end __RECOVERY_SEQ_H_
|
||||
#endif
|
Referência em uma nova issue
Block a user