aboutsummaryrefslogtreecommitdiffstats
path: root/roms/openbios/packages/nvram.c
blob: 3182edf588185571f0fb06831c12903d1e7ce3e6 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 *   Creation Date: <2003/12/01 00:26:13 samuel>
 *   Time-stamp: <2004/01/07 19:59:53 samuel>
 *
 *	<nvram.c>
 *
 *	medium-level NVRAM handling
 *
 *   Copyright (C) 2003, 2004 Samuel Rydh (samuel@ibrium.se)
 *
 *   This program is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License
 *   version 2
 *
 */

#include "config.h"
#include "libopenbios/bindings.h"
#include "arch/common/nvram.h"
#include "packages/nvram.h"

//#define CONFIG_DEBUG_NVRAM 1

#ifdef CONFIG_DEBUG_NVRAM
#define DPRINTF(fmt, args...) \
do { printk("NVRAM: " fmt , ##args); } while (0)
#else
#define DPRINTF(fmt, args...) do {} while(0)
#endif

#define DEF_SYSTEM_SIZE	0xc10

#define NV_SIG_SYSTEM	0x70
#define NV_SIG_FREE	0x7f


typedef struct {
	unsigned char	signature;
	unsigned char	checksum;
	unsigned char	len_hi;
	unsigned char	len_lo;
	char		name[12];
	char		data[0];
} nvpart_t;

static struct {
	char		*data;
	int		size;

	nvpart_t	*config;
	int		config_size;
} nvram;


/************************************************************************/
/*	generic								*/
/************************************************************************/

static unsigned int
nvpart_checksum( nvpart_t* hdr )
{
	unsigned char *p = (unsigned char*)hdr;
	int i, val = p[0];

	for( i=2; i<16; i++ ) {
		val += p[i];
		if( val > 255 )
			val = (val - 256 + 1) & 0xff;
	}
	return val;
}

static inline int
nvpart_size( nvpart_t *p )
{
	return (p->len_lo | ((int)p->len_hi<<8)) * 16;
}

static int
next_nvpart( nvpart_t **p )
{
	nvpart_t *end = (nvpart_t*)(nvram.data + nvram.size);
	int len;

	if( !*p ) {
		*p = (nvpart_t*)nvram.data;
		return 1;
	}

	if( !(len=nvpart_size(*p)) ) {
		printk("invalid nvram partition length\n");
		return -1;
	}
	*p = (nvpart_t*)((char*)*p + len);
	if( *p < end )
		return 1;
	if( *p == end )
		return 0;
	return -1;
}

static void
create_free_part( char *ptr, int size )
{
	nvpart_t *nvp = (nvpart_t*)ptr;
	memset( nvp, 0, size );

	strncpy( nvp->name, "777777777777", sizeof(nvp->name) );
	nvp->signature = NV_SIG_FREE;
	nvp->len_hi = (size /16) >> 8;
	nvp->len_lo = size /16;
	nvp->checksum = nvpart_checksum(nvp);
}

static int
create_nv_part( int signature, const char *name, int size )
{
	nvpart_t *p = NULL;
	int fs;

	while( next_nvpart(&p) > 0 ) {
		if( p->signature != NV_SIG_FREE )
			continue;

		fs = nvpart_size( p );
		if( fs < size )
			size = fs;
		p->signature = signature;
		memset( p->name, 0, sizeof(p->name) );
		strncpy( p->name, name, sizeof(p->name) );
		p->len_hi = (size>>8)/16;
		p->len_lo = size/16;
		p->checksum = nvpart_checksum(p);
		if( fs > size ) {
			char *fp = (char*)p + size;
			create_free_part( fp, fs-size );
		}
		return size;
	}
	printk("create-failed\n");
	return -1;
}

static void
zap_nvram( void )
{
	create_free_part( nvram.data, nvram.size );
	create_nv_part( NV_SIG_SYSTEM, "common", DEF_SYSTEM_SIZE );
}

#if 0
static void
show_partitions( void )
{
	nvpart_t *p = NULL;
	char buf[13];

	while( next_nvpart(&p) > 0 ) {
		memcpy( buf, p->name, sizeof(p->name) );
		buf[12] = 0;
		printk("[%02x] %-13s:  %03x\n",
		       p->signature, buf, nvpart_size(p));
	}
}
#endif

void
update_nvram( void )
{
	PUSH( pointer2cell(nvram.config->data) );
	PUSH( nvram.config_size );
	fword("nvram-store-configs");
	arch_nvram_put( nvram.data );
}

void
nvconf_init( void )
{
	int once=0;

	/* initialize nvram structure completely */
	nvram.config = NULL;
	nvram.config_size = 0;

	nvram.size = arch_nvram_size();
	nvram.data = malloc( nvram.size );
	arch_nvram_get( nvram.data );

	bind_func( "update-nvram", update_nvram );

	for( ;; ) {
		nvpart_t *p = NULL;
		int err;

		while( (err=next_nvpart(&p)) > 0 ) {
			if( nvpart_checksum(p) != p->checksum ) {
				err = -1;
				break;
			}
			if( p->signature == NV_SIG_SYSTEM ) {
				nvram.config = p;
				nvram.config_size = nvpart_size(p) - 0x10;

				if( !once++ ) {
					PUSH( pointer2cell(p->data) );
					PUSH( nvram.config_size );
					fword("nvram-load-configs");
				}
			}
		}
		if( err || !nvram.config ) {
			printk("nvram error detected, zapping pram\n");
			zap_nvram();
			if( !once++ )
				fword("set-defaults");
			continue;
		}
		break;
	}
}


/************************************************************************/
/*	nvram								*/
/************************************************************************/

typedef struct {
	unsigned int   mark_hi;
	unsigned int   mark_lo;
} nvram_ibuf_t;

DECLARE_UNNAMED_NODE( nvram, INSTALL_OPEN, sizeof(nvram_ibuf_t ));

/* ( pos_lo pos_hi -- status ) */
static void
nvram_seek( nvram_ibuf_t *nd )
{
	int pos_hi = POP();
	int pos_lo = POP();

	DPRINTF("seek %08x %08x\n", pos_hi, pos_lo );
	nd->mark_lo = pos_lo;
	nd->mark_hi = pos_hi;

	if( nd->mark_lo >= nvram.size ) {
		PUSH(-1);
		return;
	}

	/* 0=success, -1=failure (1=legacy success) */
	PUSH(0);
}

/* ( addr len -- actual ) */
static void
nvram_read( nvram_ibuf_t *nd )
{
	int len = POP();
	char *p = (char*)cell2pointer(POP());
	int n=0;

	while( nd->mark_lo < nvram.size && n < len ) {
		*p++ = nvram.data[nd->mark_lo++];
		n++;
	}
	PUSH(n);
	DPRINTF("read %p %x -- %x\n", p, len, n);
}

/* ( addr len -- actual ) */
static void
nvram_write( nvram_ibuf_t *nd )
{
	int len = POP();
	char *p = (char*)cell2pointer(POP());
	int n=0;

	while( nd->mark_lo < nvram.size && n < len ) {
		nvram.data[nd->mark_lo++] = *p++;
		n++;
	}
	PUSH(n);
	DPRINTF("write %p %x -- %x\n", p, len, n );
}

/* ( -- size ) */
static void
nvram_size( __attribute__((unused)) nvram_ibuf_t *nd )
{
	DPRINTF("nvram_size %d\n", nvram.size);
	PUSH( nvram.size );
}

NODE_METHODS( nvram ) = {
	{ "size",	(void*)nvram_size	},
	{ "read",	(void*)nvram_read	},
	{ "write",	(void*)nvram_write	},
	{ "seek",	(void*)nvram_seek	},
};


void
nvram_init( const char *path )
{
	nvconf_init();

	REGISTER_NAMED_NODE( nvram, path );
}