Reuse the logic for checking version information.
This commit is contained in:
1 parent
516ef68ba6
commit
9cdda9aa62
2 files changed
+44
-2
No files matched your search
+42
-2
@@ -16,9 +16,47 @@
|
||||
// Utility functions used in many places, not specific to any piece of code.
|
||||
//-----------------------------------------------------------------------------
|
||||
#include "commonutil.h"
|
||||
#include "pm3_cmd.h"
|
||||
#include <string.h>
|
||||
#include "stdbool.h"
|
||||
|
||||
// Specific firmware versions correspond to specific main chip types.
|
||||
struct version_info_map_chip_type {
|
||||
int magic;
|
||||
uint32_t chiptype;
|
||||
} map[] = {
|
||||
{VERSION_INFORMATION_MAGIC_PM5V, MAIN_CHIP_TYPE_AT32},
|
||||
{VERSION_INFORMATION_MAGIC_PM3V, MAIN_CHIP_TYPE_AT91},
|
||||
};
|
||||
|
||||
/**
|
||||
* Check version information is valid
|
||||
* @param version_info pointer to the struct version_information_t
|
||||
* @return true is valid, false is invalid.
|
||||
*/
|
||||
bool CheckValidInformationMagic(const void *version_info) {
|
||||
const struct version_information_t *v = (const struct version_information_t *)version_info;
|
||||
for (int i = 0; i < ARRAYLEN(map); ++i) {
|
||||
if (v->magic == map[i].magic) {
|
||||
return true; // Valid magic found.
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CheckInformationMagicAndChipType(const void *version_info, uint32_t chiptype) {
|
||||
const struct version_information_t *v = (const struct version_information_t *) version_info;
|
||||
for (int i = 0; i < ARRAYLEN(map); ++i) {
|
||||
if (v->magic == map[i].magic) {
|
||||
if (chiptype == map[i].chiptype) {
|
||||
return true;
|
||||
}
|
||||
return false; // Magic matches but chip type does not match.
|
||||
}
|
||||
}
|
||||
return false; // No valid magic found.
|
||||
}
|
||||
|
||||
/* Similar to FpgaGatherVersion this formats stored version information
|
||||
* into a string representation. It takes a pointer to the struct version_information_t,
|
||||
* verifies the magic properties, then stores a formatted string, prefixed by
|
||||
@@ -28,7 +66,8 @@ void FormatVersionInformation(char *dst, int len, const char *prefix, const void
|
||||
const struct version_information_t *v = (const struct version_information_t *)version_info;
|
||||
dst[0] = 0;
|
||||
strncat(dst, prefix, len - 1);
|
||||
if (v->magic != VERSION_INFORMATION_MAGIC) {
|
||||
|
||||
if (!CheckValidInformationMagic(version_info)) {
|
||||
strncat(dst, "Missing/Invalid version information", len - strlen(dst) - 1);
|
||||
return;
|
||||
}
|
||||
@@ -57,7 +96,8 @@ void FormatVersionInformation(char *dst, int len, const char *prefix, const void
|
||||
void format_version_information_short(char *dst, int len, const void *version_info) {
|
||||
const struct version_information_t *v = (const struct version_information_t *)version_info;
|
||||
dst[0] = 0;
|
||||
if (v->magic != VERSION_INFORMATION_MAGIC) {
|
||||
|
||||
if (!CheckValidInformationMagic(version_info)) {
|
||||
strncat(dst, "Missing/Invalid version information", len - strlen(dst) - 1);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -80,6 +80,8 @@ void add_hours(Date_t *d, int hours_to_add);
|
||||
void add_days(Date_t *d, int days_to_add);
|
||||
uint8_t days_in_month(int year, int month);
|
||||
|
||||
bool CheckValidInformationMagic(const void *version_info);
|
||||
bool CheckInformationMagicAndChipType(const void *version_info, uint32_t chiptype);
|
||||
|
||||
extern struct version_information_t g_version_information;
|
||||
void FormatVersionInformation(char *dst, int len, const char *prefix, const void *version_info);
|
||||
|
||||
Reference in new issue
Block a user