1 |
dpavlin |
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 |
dpavlin |
7 |
#include "cpu/cpu.h" |
27 |
dpavlin |
1 |
|
28 |
|
|
#define NVRAM_IMAGE_SIZE 0x2000 |
29 |
|
|
#define NVRAM_FREE_PARTITION_NAME "wwwwwwwwwwww" |
30 |
|
|
#define NVRAM_FREE_PARTITION_MAGIC 0x7f |
31 |
|
|
#define NVRAM_PARTITION_HDR_SIZE 16 |
32 |
|
|
|
33 |
|
|
struct NVRAM { |
34 |
|
|
FILE *f; |
35 |
|
|
}; |
36 |
|
|
|
37 |
|
|
// For reference: |
38 |
|
|
struct NVRAMPartitionHeader { |
39 |
|
|
uint8 magic; |
40 |
|
|
uint8 chksum; |
41 |
|
|
uint16 length_div_16; |
42 |
|
|
char name[12]; |
43 |
|
|
}; |
44 |
|
|
|
45 |
|
|
NVRAM gNVRAM; |
46 |
|
|
|
47 |
|
|
void nvram_write(uint32 addr, uint32 data, int size) |
48 |
|
|
{ |
49 |
|
|
uint8 d = (uint8) data; |
50 |
|
|
addr -= IO_NVRAM_PA_START; |
51 |
dpavlin |
7 |
IO_NVRAM_TRACE("write(%d): %08x at %08x (from @%08x, lr: %08x)\n", size, data, addr, ppc_cpu_get_pc(0), ppc_cpu_get_lr(0)); |
52 |
dpavlin |
1 |
if (addr & 0xf) IO_NVRAM_ERR("address not aligned\n"); |
53 |
|
|
addr >>= 4; |
54 |
|
|
if (addr >= NVRAM_IMAGE_SIZE) IO_NVRAM_ERR("out of bounds\n"); |
55 |
|
|
if (size != 1) IO_NVRAM_ERR("only supports byte writes\n"); |
56 |
|
|
fseek(gNVRAM.f, addr, SEEK_SET); |
57 |
|
|
fwrite(&d, 1, 1, gNVRAM.f); |
58 |
|
|
fflush(gNVRAM.f); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
void nvram_read(uint32 addr, uint32 &data, int size) |
62 |
|
|
{ |
63 |
|
|
uint8 d = 0; |
64 |
|
|
addr -= IO_NVRAM_PA_START; |
65 |
dpavlin |
7 |
IO_NVRAM_TRACE("read(%d): at %08x (from @%08x, lr: %08x)\n", size, addr, ppc_cpu_get_pc(0), ppc_cpu_get_lr(0)); |
66 |
dpavlin |
1 |
if (addr & 0xf) IO_NVRAM_ERR("address not aligned\n"); |
67 |
|
|
addr >>= 4; |
68 |
|
|
if (addr >= NVRAM_IMAGE_SIZE) IO_NVRAM_ERR("out of bounds\n"); |
69 |
|
|
if (size != 1) IO_NVRAM_ERR("only supports byte reads\n"); |
70 |
|
|
fseek(gNVRAM.f, addr, SEEK_SET); |
71 |
|
|
fread(&d, 1, 1, gNVRAM.f); |
72 |
|
|
data = d; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
static uint8 calcChksum(byte *buf) |
76 |
|
|
{ |
77 |
|
|
uint8 x, y; |
78 |
|
|
y = 0; |
79 |
|
|
for (int i=0; i < NVRAM_PARTITION_HDR_SIZE; i++) { |
80 |
|
|
x = y + buf[i]; |
81 |
|
|
if (x < y) x++; |
82 |
|
|
y = x; |
83 |
|
|
} |
84 |
|
|
return y; |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
#define NRAM_KEY_FILE "nvram_file" |
88 |
|
|
#include "configparser.h" |
89 |
|
|
|
90 |
|
|
void nvram_init() |
91 |
|
|
{ |
92 |
|
|
String filename; |
93 |
|
|
gConfig->getConfigString(NRAM_KEY_FILE, filename); |
94 |
|
|
|
95 |
|
|
gNVRAM.f = fopen(filename.contentChar(), "rb+"); |
96 |
|
|
if (!gNVRAM.f) { |
97 |
|
|
gNVRAM.f = fopen(filename.contentChar(), "wb+"); |
98 |
|
|
if (!gNVRAM.f) IO_NVRAM_ERR("couldn't create file '%y'\n", &filename); |
99 |
|
|
byte buf[NVRAM_IMAGE_SIZE]; |
100 |
|
|
memset(buf, 0, sizeof buf); |
101 |
|
|
|
102 |
|
|
// Mark nvram as free: |
103 |
|
|
buf[0] = NVRAM_FREE_PARTITION_MAGIC; |
104 |
|
|
buf[1] = 0; // Checksum |
105 |
|
|
buf[2] = 0x02; // Length/0x10 (MSB) |
106 |
|
|
buf[3] = 0x00; // Length/0x10 (LSB) |
107 |
|
|
memcpy(&buf[4], NVRAM_FREE_PARTITION_NAME, 12); |
108 |
|
|
buf[1] = calcChksum(buf); |
109 |
|
|
|
110 |
|
|
if (fwrite(buf, sizeof buf, 1, gNVRAM.f) != 1) IO_NVRAM_ERR("can't write file '%y'\n", &filename); |
111 |
|
|
fflush(gNVRAM.f); |
112 |
|
|
} |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
void nvram_init_config() |
116 |
|
|
{ |
117 |
|
|
gConfig->acceptConfigEntryStringDef(NRAM_KEY_FILE, "nvram"); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
void nvram_done() |
121 |
|
|
{ |
122 |
|
|
fclose(gNVRAM.f); |
123 |
|
|
} |
124 |
|
|
|