diff options
Diffstat (limited to 'firmware/eeprom_settings.c')
-rw-r--r-- | firmware/eeprom_settings.c | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/firmware/eeprom_settings.c b/firmware/eeprom_settings.c new file mode 100644 index 0000000000..43f519d3fa --- /dev/null +++ b/firmware/eeprom_settings.c | |||
@@ -0,0 +1,116 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2006 by Miika Pekkarinen | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | |||
20 | #include "eeprom_settings.h" | ||
21 | #include "eeprom_24cxx.h" | ||
22 | #include "crc32.h" | ||
23 | |||
24 | #include "string.h" | ||
25 | #include "logf.h" | ||
26 | |||
27 | struct eeprom_settings firmware_settings; | ||
28 | |||
29 | static void reset_config(void) | ||
30 | { | ||
31 | memset(&firmware_settings, 0, sizeof(struct eeprom_settings)); | ||
32 | firmware_settings.version = EEPROM_SETTINGS_VERSION; | ||
33 | firmware_settings.initialized = true; | ||
34 | firmware_settings.boot_disk = false; | ||
35 | firmware_settings.bl_version = 0; | ||
36 | } | ||
37 | |||
38 | bool eeprom_settings_init(void) | ||
39 | { | ||
40 | bool ret; | ||
41 | uint32_t sum; | ||
42 | |||
43 | eeprom_24cxx_init(); | ||
44 | |||
45 | /* Check if player has been flashed. */ | ||
46 | if (!detect_flashed_rockbox()) | ||
47 | { | ||
48 | memset(&firmware_settings, 0, sizeof(struct eeprom_settings)); | ||
49 | firmware_settings.initialized = false; | ||
50 | logf("Rockbox in flash is required"); | ||
51 | return false; | ||
52 | } | ||
53 | |||
54 | ret = eeprom_24cxx_read(0, &firmware_settings, | ||
55 | sizeof(struct eeprom_settings)); | ||
56 | |||
57 | if (!ret) | ||
58 | { | ||
59 | memset(&firmware_settings, 0, sizeof(struct eeprom_settings)); | ||
60 | firmware_settings.initialized = false; | ||
61 | return false; | ||
62 | } | ||
63 | |||
64 | sum = crc_32(&firmware_settings, sizeof(struct eeprom_settings)-4, | ||
65 | 0xffffffff); | ||
66 | |||
67 | if (firmware_settings.checksum != sum) | ||
68 | { | ||
69 | logf("Checksum mismatch"); | ||
70 | reset_config(); | ||
71 | return true; | ||
72 | } | ||
73 | |||
74 | if (firmware_settings.version != EEPROM_SETTINGS_VERSION) | ||
75 | { | ||
76 | logf("Version mismatch"); | ||
77 | reset_config(); | ||
78 | return true; | ||
79 | } | ||
80 | |||
81 | #ifndef BOOTLOADER | ||
82 | if (firmware_settings.bl_version < EEPROM_SETTINGS_BL_MINVER) | ||
83 | { | ||
84 | logf("Too old bootloader: %d", firmware_settings.bl_version); | ||
85 | reset_config(); | ||
86 | return true; | ||
87 | } | ||
88 | #endif | ||
89 | |||
90 | return true; | ||
91 | } | ||
92 | |||
93 | bool eeprom_settings_store(void) | ||
94 | { | ||
95 | bool ret; | ||
96 | uint32_t sum; | ||
97 | |||
98 | if (!firmware_settings.initialized || !detect_flashed_rockbox()) | ||
99 | { | ||
100 | logf("Rockbox in flash is required"); | ||
101 | return false; | ||
102 | } | ||
103 | |||
104 | /* Update the checksum. */ | ||
105 | sum = crc_32(&firmware_settings, sizeof(struct eeprom_settings)-4, | ||
106 | 0xffffffff); | ||
107 | firmware_settings.checksum = sum; | ||
108 | ret = eeprom_24cxx_write(0, &firmware_settings, | ||
109 | sizeof(struct eeprom_settings)); | ||
110 | |||
111 | if (!ret) | ||
112 | firmware_settings.initialized = false; | ||
113 | |||
114 | return ret; | ||
115 | } | ||
116 | |||