1 |
/* |
2 |
* PearPC |
3 |
* nvram.cc |
4 |
* |
5 |
* Copyright (C) 2003 Sebastian Biallas (sb@biallas.net) |
6 |
* |
7 |
* This program is free software; you can redistribute it and/or modify |
8 |
* it under the terms of the GNU General Public License version 2 as |
9 |
* published by the Free Software Foundation. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
*/ |
20 |
|
21 |
#include <cstdio> |
22 |
#include <cstring> |
23 |
|
24 |
#include "debug/tracers.h" |
25 |
#include "nvram.h" |
26 |
|
27 |
#define NVRAM_IMAGE_SIZE 0x2000 |
28 |
#define NVRAM_FREE_PARTITION_NAME "wwwwwwwwwwww" |
29 |
#define NVRAM_FREE_PARTITION_MAGIC 0x7f |
30 |
#define NVRAM_PARTITION_HDR_SIZE 16 |
31 |
|
32 |
struct NVRAM { |
33 |
FILE *f; |
34 |
}; |
35 |
|
36 |
// For reference: |
37 |
struct NVRAMPartitionHeader { |
38 |
uint8 magic; |
39 |
uint8 chksum; |
40 |
uint16 length_div_16; |
41 |
char name[12]; |
42 |
}; |
43 |
|
44 |
NVRAM gNVRAM; |
45 |
|
46 |
void nvram_write(uint32 addr, uint32 data, int size) |
47 |
{ |
48 |
uint8 d = (uint8) data; |
49 |
addr -= IO_NVRAM_PA_START; |
50 |
IO_NVRAM_TRACE("write(%d): %08x at %08x (from @%08x, lr: %08x)\n", size, data, addr, gCPU.pc, gCPU.lr); |
51 |
if (addr & 0xf) IO_NVRAM_ERR("address not aligned\n"); |
52 |
addr >>= 4; |
53 |
if (addr >= NVRAM_IMAGE_SIZE) IO_NVRAM_ERR("out of bounds\n"); |
54 |
if (size != 1) IO_NVRAM_ERR("only supports byte writes\n"); |
55 |
fseek(gNVRAM.f, addr, SEEK_SET); |
56 |
fwrite(&d, 1, 1, gNVRAM.f); |
57 |
fflush(gNVRAM.f); |
58 |
} |
59 |
|
60 |
void nvram_read(uint32 addr, uint32 &data, int size) |
61 |
{ |
62 |
uint8 d = 0; |
63 |
addr -= IO_NVRAM_PA_START; |
64 |
IO_NVRAM_TRACE("read(%d): at %08x (from @%08x, lr: %08x)\n", size, addr, gCPU.pc, gCPU.lr); |
65 |
if (addr & 0xf) IO_NVRAM_ERR("address not aligned\n"); |
66 |
addr >>= 4; |
67 |
if (addr >= NVRAM_IMAGE_SIZE) IO_NVRAM_ERR("out of bounds\n"); |
68 |
if (size != 1) IO_NVRAM_ERR("only supports byte reads\n"); |
69 |
fseek(gNVRAM.f, addr, SEEK_SET); |
70 |
fread(&d, 1, 1, gNVRAM.f); |
71 |
data = d; |
72 |
} |
73 |
|
74 |
static uint8 calcChksum(byte *buf) |
75 |
{ |
76 |
uint8 x, y; |
77 |
y = 0; |
78 |
for (int i=0; i < NVRAM_PARTITION_HDR_SIZE; i++) { |
79 |
x = y + buf[i]; |
80 |
if (x < y) x++; |
81 |
y = x; |
82 |
} |
83 |
return y; |
84 |
} |
85 |
|
86 |
#define NRAM_KEY_FILE "nvram_file" |
87 |
#include "configparser.h" |
88 |
|
89 |
void nvram_init() |
90 |
{ |
91 |
String filename; |
92 |
gConfig->getConfigString(NRAM_KEY_FILE, filename); |
93 |
|
94 |
gNVRAM.f = fopen(filename.contentChar(), "rb+"); |
95 |
if (!gNVRAM.f) { |
96 |
gNVRAM.f = fopen(filename.contentChar(), "wb+"); |
97 |
if (!gNVRAM.f) IO_NVRAM_ERR("couldn't create file '%y'\n", &filename); |
98 |
byte buf[NVRAM_IMAGE_SIZE]; |
99 |
memset(buf, 0, sizeof buf); |
100 |
|
101 |
// Mark nvram as free: |
102 |
buf[0] = NVRAM_FREE_PARTITION_MAGIC; |
103 |
buf[1] = 0; // Checksum |
104 |
buf[2] = 0x02; // Length/0x10 (MSB) |
105 |
buf[3] = 0x00; // Length/0x10 (LSB) |
106 |
memcpy(&buf[4], NVRAM_FREE_PARTITION_NAME, 12); |
107 |
buf[1] = calcChksum(buf); |
108 |
|
109 |
if (fwrite(buf, sizeof buf, 1, gNVRAM.f) != 1) IO_NVRAM_ERR("can't write file '%y'\n", &filename); |
110 |
fflush(gNVRAM.f); |
111 |
} |
112 |
} |
113 |
|
114 |
void nvram_init_config() |
115 |
{ |
116 |
gConfig->acceptConfigEntryStringDef(NRAM_KEY_FILE, "nvram"); |
117 |
} |
118 |
|
119 |
void nvram_done() |
120 |
{ |
121 |
fclose(gNVRAM.f); |
122 |
} |
123 |
|