Browse Source

board_hw_eeprom_rev_ver:Versioning hierarchy

main
David Sidrane 3 years ago committed by Beat Küng
parent
commit
00e82c9060
  1. 22
      platforms/nuttx/src/px4/common/include/px4_platform/board_hw_eeprom_rev_ver.h
  2. 80
      platforms/nuttx/src/px4/stm/stm32_common/board_hw_info/board_hw_rev_ver.c

22
platforms/nuttx/src/px4/common/include/px4_platform/board_hw_eeprom_rev_ver.h

@ -38,22 +38,22 @@
#pragma pack(push, 1) #pragma pack(push, 1)
typedef struct { typedef struct {
uint16_t format_version; uint16_t id;
} mtd_mft_t;
typedef struct {
mtd_mft_t version;
uint16_t hw_extended_ver; uint16_t hw_extended_ver;
uint16_t crc; uint16_t crc;
} mtd_mft_v0_t; } mtd_mft_v0_t;
typedef struct { typedef struct {
uint16_t format_version; mtd_mft_t version;
uint16_t hw_extended_ver; uint16_t hw_extended_ver;
//{device tree overlay} //{device tree overlay}
uint16_t crc; uint16_t crc;
} mtd_mft_v1_t; } mtd_mft_v1_t;
typedef struct {
uint16_t hw_extended_ver;
//{device tree overlay}
} mtd_mft_t;
#pragma pack(pop) #pragma pack(pop)
@ -62,6 +62,8 @@ typedef struct {
#define MTD_MFT_OFFSET 0 //<! Offset in EEPROM where mtd_mft data starts #define MTD_MFT_OFFSET 0 //<! Offset in EEPROM where mtd_mft data starts
__BEGIN_DECLS
/************************************************************************************ /************************************************************************************
* Name: board_set_eeprom_hw_info * Name: board_set_eeprom_hw_info
* *
@ -69,7 +71,7 @@ typedef struct {
* Function for writing hardware info to EEPROM * Function for writing hardware info to EEPROM
* *
* Input Parameters: * Input Parameters:
* *mtd_mft - pointer to mtd_mft to write hw_info * *mtd_mft_unk - pointer to mtd_mft to write hw_info
* *
* Returned Value: * Returned Value:
* 0 - Successful storing to EEPROM * 0 - Successful storing to EEPROM
@ -78,9 +80,9 @@ typedef struct {
************************************************************************************/ ************************************************************************************/
#if !defined(BOARD_HAS_SIMPLE_HW_VERSIONING) && defined(BOARD_HAS_VERSIONING) #if !defined(BOARD_HAS_SIMPLE_HW_VERSIONING) && defined(BOARD_HAS_VERSIONING)
__EXPORT int board_set_eeprom_hw_info(mtd_mft_t *mtd_mft); __EXPORT int board_set_eeprom_hw_info(mtd_mft_t *mtd_mft_unk);
#else #else
static inline int board_set_eeprom_hw_info(mtd_mft_t *mtd_mft) { return -ENOSYS; } static inline int board_set_eeprom_hw_info(mtd_mft_t *mtd_mft_unk) { return -ENOSYS; }
#endif #endif
/************************************************************************************ /************************************************************************************
@ -103,3 +105,5 @@ __EXPORT int board_get_eeprom_hw_info(mtd_mft_t *mtd_mft);
#else #else
static inline int board_get_eeprom_hw_info(mtd_mft_t *mtd_mft) { return -ENOSYS; } static inline int board_get_eeprom_hw_info(mtd_mft_t *mtd_mft) { return -ENOSYS; }
#endif #endif
__END_DECLS

80
platforms/nuttx/src/px4/stm/stm32_common/board_hw_info/board_hw_rev_ver.c

@ -66,7 +66,6 @@ static int hw_version = 0;
static int hw_revision = 0; static int hw_revision = 0;
static char hw_info[HW_INFO_SIZE] = {0}; static char hw_info[HW_INFO_SIZE] = {0};
static const uint16_t mtd_mft_version = MTD_MFT_v0; //< Current version of structure in EEPROM
static const char *mtd_mft_path = "/fs/mtd_mft"; static const char *mtd_mft_path = "/fs/mtd_mft";
/**************************************************************************** /****************************************************************************
@ -454,8 +453,8 @@ int board_determine_hw_info()
/* EEPROM hw version range: {0x10 - 0xFFFF} */ /* EEPROM hw version range: {0x10 - 0xFFFF} */
if (hw_version == HW_VERSION_EEPROM) { if (hw_version == HW_VERSION_EEPROM) {
mtd_mft_t mtd_mft; mtd_mft_v0_t mtd_mft = {MTD_MFT_v0};
rv = board_get_eeprom_hw_info(&mtd_mft); rv = board_get_eeprom_hw_info((mtd_mft_t *)&mtd_mft);
if (rv == OK) { if (rv == OK) {
hw_version = mtd_mft.hw_extended_ver; hw_version = mtd_mft.hw_extended_ver;
@ -483,7 +482,7 @@ int board_determine_hw_info()
* Function for writing hardware info to EEPROM * Function for writing hardware info to EEPROM
* *
* Input Parameters: * Input Parameters:
* *mtd_mft - pointer to mtd_mft to write hw_info * *mtd_mft_unk - pointer to mtd_mft to write hw_info
* *
* Returned Value: * Returned Value:
* 0 - Successful storing to EEPROM * 0 - Successful storing to EEPROM
@ -491,12 +490,20 @@ int board_determine_hw_info()
* *
************************************************************************************/ ************************************************************************************/
__EXPORT int board_set_eeprom_hw_info(mtd_mft_t *mtd_mft) int board_set_eeprom_hw_info(mtd_mft_t *mtd_mft_unk)
{ {
if (mtd_mft == NULL) { if (mtd_mft_unk == NULL) {
return -1; return -1;
} }
// Later this will be a demux on type
if (mtd_mft_unk->id != MTD_MFT_v0) {
printf("Verson is: %d, Only mft version %d is supported\n", mtd_mft_unk->id, MTD_MFT_v0);
return -EINVAL;
}
mtd_mft_v0_t *mtd_mft = (mtd_mft_v0_t *)mtd_mft_unk;
if (mtd_mft->hw_extended_ver < HW_EEPROM_VERSION_MIN) { if (mtd_mft->hw_extended_ver < HW_EEPROM_VERSION_MIN) {
printf("hardware version for EEPROM must be greater than %x\n", HW_EEPROM_VERSION_MIN); printf("hardware version for EEPROM must be greater than %x\n", HW_EEPROM_VERSION_MIN);
return -EINVAL; return -EINVAL;
@ -510,24 +517,16 @@ __EXPORT int board_set_eeprom_hw_info(mtd_mft_t *mtd_mft)
int ret_val = OK; int ret_val = OK;
mtd_mft_v0_t mtd_mft_v0 = { mtd_mft->crc = crc16_signature(CRC16_INITIAL, sizeof(*mtd_mft) - sizeof(mtd_mft->crc), (uint8_t *) mtd_mft);
.format_version = mtd_mft_version,
.hw_extended_ver = mtd_mft->hw_extended_ver
};
mtd_mft_v0.crc = crc16_signature(CRC16_INITIAL, sizeof(mtd_mft_v0) - sizeof(mtd_mft_v0.crc), (uint8_t *)&mtd_mft_v0);
if ( if (
(MTD_MFT_OFFSET != lseek(fd, MTD_MFT_OFFSET, SEEK_SET)) || (MTD_MFT_OFFSET != lseek(fd, MTD_MFT_OFFSET, SEEK_SET)) ||
(sizeof(mtd_mft_v0) != write(fd, &mtd_mft_v0, sizeof(mtd_mft_v0))) (sizeof(*mtd_mft) != write(fd, mtd_mft, sizeof(*mtd_mft)))
) { ) {
ret_val = -errno; ret_val = -errno;
} }
if (fd >= 0) {
close(fd); close(fd);
fd = -1;
}
return ret_val; return ret_val;
} }
@ -548,14 +547,18 @@ __EXPORT int board_set_eeprom_hw_info(mtd_mft_t *mtd_mft)
************************************************************************************/ ************************************************************************************/
__EXPORT int board_get_eeprom_hw_info(mtd_mft_t *mtd_mft) __EXPORT int board_get_eeprom_hw_info(mtd_mft_t *mtd_mft)
{ {
if (mtd_mft == NULL) {
return -EINVAL;
}
int fd = open(mtd_mft_path, O_RDONLY); int fd = open(mtd_mft_path, O_RDONLY);
if ((fd < 0) || (mtd_mft == NULL)) { if (fd < 0) {
return -errno; return -errno;
} }
int ret_val = OK; int ret_val = OK;
uint16_t format_version = 0; mtd_mft_t format_version = {-1};
if ( if (
(MTD_MFT_OFFSET != lseek(fd, MTD_MFT_OFFSET, SEEK_SET)) || (MTD_MFT_OFFSET != lseek(fd, MTD_MFT_OFFSET, SEEK_SET)) ||
@ -563,54 +566,51 @@ __EXPORT int board_get_eeprom_hw_info(mtd_mft_t *mtd_mft)
) { ) {
ret_val = -errno; ret_val = -errno;
} else if (format_version.id != mtd_mft->id) {
ret_val = -EPROTO;
} else { } else {
uint16_t buffer_size = 0; uint16_t mft_size = 0;
if (MTD_MFT_v0 == format_version) { switch (format_version.id) {
buffer_size = sizeof(mtd_mft_v0_t); case MTD_MFT_v0: mft_size = sizeof(mtd_mft_v0_t); break;
} else if (MTD_MFT_v1 == format_version) { case MTD_MFT_v1: mft_size = sizeof(mtd_mft_v1_t); break;
buffer_size = sizeof(mtd_mft_v1_t);
} else { default:
printf("[boot] Error, unknown version %d of mtd_mft in EEPROM\n", format_version); printf("[boot] Error, unknown version %d of mtd_mft in EEPROM\n", format_version.id);
ret_val = -1; ret_val = -1;
break;
} }
if (ret_val == OK) { if (ret_val == OK) {
uint8_t buffer[buffer_size];
memset(buffer, 0u, buffer_size);
if ( if (
(MTD_MFT_OFFSET != lseek(fd, MTD_MFT_OFFSET, SEEK_SET)) || (MTD_MFT_OFFSET != lseek(fd, MTD_MFT_OFFSET, SEEK_SET)) ||
(buffer_size != read(fd, buffer, buffer_size)) (mft_size != read(fd, mtd_mft, mft_size))
) { ) {
ret_val = -errno; ret_val = -errno;
} else { } else {
uint16_t crc = 0, eeprom_crc = 0; union {
crc = crc16_signature(CRC16_INITIAL, buffer_size - sizeof(crc), buffer); uint16_t w;
uint8_t b[2];
eeprom_crc = (uint16_t)((buffer[buffer_size - 1] << 8) | buffer[buffer_size - 2]); } crc;
if (crc == eeprom_crc) { uint8_t *bytes = (uint8_t *) mtd_mft;
memcpy(mtd_mft, &buffer[sizeof(format_version)], buffer_size - sizeof(format_version) - sizeof(crc)); crc.w = crc16_signature(CRC16_INITIAL, mft_size - sizeof(crc), bytes);
uint8_t *eeprom_crc = &bytes[mft_size - sizeof(crc)];
} else { if (!(crc.b[0] == eeprom_crc[0] && crc.b[1] == eeprom_crc[1])) {
ret_val = -1; ret_val = -1;
} }
} }
} }
} }
if (fd >= 0) {
close(fd); close(fd);
fd = -1;
}
return ret_val; return ret_val;
} }

Loading…
Cancel
Save