summaryrefslogtreecommitdiff
path: root/firmware/eeprom_settings.c
blob: f3be2ec2b397bcddb6db9b99b503e4e94e49b1ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 by Miika Pekkarinen
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include "eeprom_settings.h"
#include "eeprom_24cxx.h"
#include "crc32.h"

#include "system.h"
#include "string.h"
#include "logf.h"

struct eeprom_settings firmware_settings;

static bool reset_config(void)
{
    memset(&firmware_settings, 0, sizeof(struct eeprom_settings));
#ifdef BOOTLOADER
    /* Don't reset settings if we are inside bootloader. */
    firmware_settings.initialized = false;
#else
    firmware_settings.version = EEPROM_SETTINGS_VERSION;
    firmware_settings.initialized = true;
    firmware_settings.bootmethod = BOOT_RECOVERY;
    firmware_settings.bl_version = 0;
#endif
    
    return firmware_settings.initialized;
}

bool eeprom_settings_init(void)
{
    int ret;
    uint32_t sum;
    
    eeprom_24cxx_init();
    
    /* Check if player has been flashed. */
    if (detect_original_firmware())
    {
        memset(&firmware_settings, 0, sizeof(struct eeprom_settings));
        firmware_settings.initialized = false;
        logf("Rockbox in flash is required");
        return false;
    }
    
    ret = eeprom_24cxx_read(0, &firmware_settings, 
                            sizeof(struct eeprom_settings));

    if (ret < 0)
    {
        memset(&firmware_settings, 0, sizeof(struct eeprom_settings));
        firmware_settings.initialized = false;
        return false;
    }
    
    sum = crc_32(&firmware_settings, sizeof(struct eeprom_settings)-4,
                 0xffffffff);
    
    logf("BL version: %d", firmware_settings.bl_version);
    if (firmware_settings.version != EEPROM_SETTINGS_VERSION)
    {
        logf("Version mismatch");
        return reset_config();
    }
    
    if (firmware_settings.checksum != sum)
    {
        logf("Checksum mismatch");
        return reset_config();
    }
    
#ifndef BOOTLOADER
    if (firmware_settings.bl_version < EEPROM_SETTINGS_BL_MINVER)
    {
        logf("Too old bootloader: %d", firmware_settings.bl_version);
        return reset_config();
    }
#endif
    
    return true;
}

bool eeprom_settings_store(void)
{
    int ret;
    uint32_t sum;
    
    if (!firmware_settings.initialized || detect_original_firmware())
    {
        logf("Rockbox in flash is required");
        return false;
    }
    
    /* Update the checksum. */
    sum = crc_32(&firmware_settings, sizeof(struct eeprom_settings)-4,
                 0xffffffff);
    firmware_settings.checksum = sum;
    ret = eeprom_24cxx_write(0, &firmware_settings,
                             sizeof(struct eeprom_settings));
    
    if (ret < 0)
        firmware_settings.initialized = false;
    
    return ret;
}