#include "include.h"

#define TRACE_EN                0

#if TRACE_EN
#define TRACE(...)              printf(__VA_ARGS__)
#else
#define TRACE(...)
#endif

///CM Init, judge whether the Page parameter is valid, and load it if it is valid
bool cm_loadparam(void *buff, uint page)
{
    u8 *buf = buff;
    uint key;
    TRACE("cm_loadparam: %d\n", page);
    if (page == SYS_CM_PAGE_NUM) {
        key = GET_LE32(buf + PARAM_RANDOM_KEY);
        if (key == 0 || key == UINT_MAX) {
            TRACE("key: %d error\n", key);
            return false;
        }
    }
    return true;
}

///When CM Init, if no valid parameter is found, set the factory value
void cm_factory(void *buff, uint page)
{
    u8 *buf = buff;
    memset(buf, 0, 250);

    TRACE("cm_factory: %d\n", page);
    if (page == SYS_CM_PAGE_NUM) {
        PUT_LE32(buf + PARAM_RANDOM_KEY, sys_get_rand_key());
    }
}
