void eeprom_store_data()
{
    uint8_t buf[200] = {0};  // initialize with zeros

    int len = ts.pub_msg_cbor(buf + EEPROM_HEADER_SIZE, sizeof(buf) - EEPROM_HEADER_SIZE, eeprom_data_objects, sizeof(eeprom_data_objects)/sizeof(uint16_t));
    uint32_t crc = _calc_crc(buf + EEPROM_HEADER_SIZE, len);

    // store EEPROM_VERSION, number of bytes and CRC
    *((uint16_t*)&buf[0]) = (uint16_t)EEPROM_VERSION;
    *((uint16_t*)&buf[2]) = (uint16_t)(len);   // length of data
    *((uint32_t*)&buf[4]) = crc;

    //printf("Data (len=%d): ", len);
    //for (int i = 0; i < len; i++) printf("%.2x ", buf[i + EEPROM_HEADER_SIZE]);

    //printf("Header: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n",
    //    buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);

    if (len == 0) {
        printf("EEPROM: Data could not be stored, ThingSet error: %d\n", len);
    }
    else if (eeprom_write(0, buf, len + EEPROM_HEADER_SIZE) < 0) {
        printf("EEPROM: Write error.\n");
    }
    else {
        printf("EEPROM: Data successfully stored.\n");
    }
}
Ejemplo n.º 2
0
/**
 * Store LAN user configuration
 *
 * \return status code
 */
int save_global_user_config(void)
{
    int len, retval;
    unsigned char i;

    portENTER_CRITICAL();

    /* encrypt the passwords with the XTEA algorithm */
    for (i = 0; i < configPRIV_MAX_USERS; i++)
    {
        xtea_encrypt_block((unsigned char *)(global_user.user[i].password), IPMI_PRIV_PW_MAX_LEN, pw_crypto_key);
    }

    /* calculate CRC for global user configuration */
    len = ((unsigned char *)(&(global_user.crc)) - (unsigned char *)(&global_user));
    global_user.crc = _calc_crc(&global_user, len);

    /* save global user config to eeprom */
    retval = spi_eeprom_safe_area_write(USER_CONFIGURATION_OFFSET, sizeof(global_user_conf_t), &global_user);

    /* decrypt the passwords with the XTEA algorithm to use them in ram in plaintext */
    for (i = 0; i < configPRIV_MAX_USERS; i++)
    {
        xtea_decrypt_block((unsigned char *)(global_user.user[i].password), IPMI_PRIV_PW_MAX_LEN, pw_crypto_key);
    }

    portEXIT_CRITICAL();

    return retval;
}
void eeprom_restore_data()
{
    uint8_t buf_req[200] = {0};  // ThingSet request buffer, initialize with zeros

    // EEPROM header
    uint8_t buf_header[EEPROM_HEADER_SIZE];
    if (eeprom_read(0, buf_header, EEPROM_HEADER_SIZE) < 0) {
        printf("EEPROM: read error!\n");
        return;
    }
    uint16_t version = *((uint16_t*)&buf_header[0]);
    uint16_t len     = *((uint16_t*)&buf_header[2]);
    uint32_t crc     = *((uint32_t*)&buf_header[4]);

    //printf("EEPROM header restore: ver %d, len %d, CRC %.8x\n", version, len, (unsigned int)crc);
    //printf("Header: %.2x %.2x %.2x %.2x %.2x %.2x %.2x %.2x\n",
    //    buf_header[0], buf_header[1], buf_header[2], buf_header[3],
    //    buf_header[4], buf_header[5], buf_header[6], buf_header[7]);

    if (version == EEPROM_VERSION && len <= sizeof(buf_req)) {
        eeprom_read(EEPROM_HEADER_SIZE, buf_req, len);

        //printf("Data (len=%d): ", len);
        //for (int i = 0; i < len; i++) printf("%.2x ", buf_req[i]);

        if (_calc_crc(buf_req, len) == crc) {
            int status = ts.init_cbor(buf_req, sizeof(buf_req));     // first byte is ignored
            printf("EEPROM: Data objects read and updated, ThingSet result: %d\n", status);
        }
        else {
            printf("EEPROM: CRC of data not correct, expected 0x%x (data_len = %d)\n", (unsigned int)crc, len);
        }
    }
}
Ejemplo n.º 4
0
/**
 * checks the checksum over the buffer with length
 *
 * \return checksum is correct (TRUE = 1) or not (FALSE = 0)
 */
static unsigned char _check_crc(void *buffer, int len, unsigned char crc)
{
    if (_calc_crc(buffer, len) == crc)
    {
        return E_CRC;
    }
    else
    {
        return E_OK;
    }
}
Ejemplo n.º 5
0
/**
 * Store LAN configuration
 *
 * \return status code
 */
int save_global_lan_config(void)
{
    int len;

#ifdef SOL_BITRATE_DEFAULT
    unsigned char setting;

    /* write default oem settings to eeprom */
    setting = SOL_BITRATE_DEFAULT;
    spi_eeprom_safe_area_write(NV_SOL_BITRATE_OFFSET, 1, &setting);
#endif

    /* calculate CRC for global lan configuration */
    len = ((unsigned char *)(&(lan_conf.crc)) - (unsigned char *)(&lan_conf));
    lan_conf.crc = _calc_crc(&lan_conf, len);

    /* save global lan config to eeprom */
    return spi_eeprom_safe_area_write(LAN_CONFIGURATION_OFFSET, sizeof(global_lan_conf_t), &lan_conf);
}