summaryrefslogtreecommitdiff
path: root/os/boot/mpc/flash.c
blob: d05f638f565f5c7db6ac8851c6de7fdf75201f5c (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
#include "boot.h"

typedef struct Flashdev Flashdev;
struct Flashdev {
	uchar*	base;
	int	size;
	uchar*	exec;
	char*	config;
	int	conflen;
};

enum {
	FLASHSEG = 256*1024,
	CONFIGLIM = FLASHSEG,
	BOOTOFF = FLASHSEG,
	BOOTLEN = 3*FLASHSEG,	/* third segment might be filsys */
	/* rest of flash is free */
};

static Flashdev flash;

/*
 * configuration data is written between the bootstrap and
 * the end of region 0. the region ends with allocation descriptors
 * of the following form:
 *
 * byte order is big endian
 *
 * the last valid region found that starts with the string "#plan9.ini\n" is plan9.ini
 */
typedef struct Flalloc Flalloc;
struct Flalloc {
	ulong	check;	/* checksum of data, or ~0 */
	ulong	base;	/* base of region; ~0 if unallocated, 0 if deleted */
	uchar	len[3];
	uchar	tag;		/* see below */
	uchar	sig[4];
};

enum {
	/* tags */
	Tdead=	0,
	Tboot=	0x01,	/* space reserved for boot */
	Tconf=	0x02,	/* configuration data */
	Tnone=	0xFF,

	Noval=	~0,
};

static char flashsig[] = {0xF1, 0xA5, 0x5A, 0x1F};
static char conftag[] = "#plan9.ini\n";

static ulong
checksum(uchar* p, int n)
{
	ulong s;

	for(s=0; --n >= 0;)
		s += *p++;
	return s;
}

static int
validptr(Flalloc *ap, uchar *p)
{
	return p > (uchar*)&end && p < (uchar*)ap;
}

static int
flashcheck(Flalloc *ap, char **val, int *len)
{
	uchar *base;
	int n;

	if(ap->base == Noval || ap->base >= FLASHSEG || ap->tag == Tnone)
		return 0;
	base = flash.base+ap->base;
	if(!validptr(ap, base))
		return 0;
	n = (((ap->len[0]<<8)|ap->len[1])<<8)|ap->len[2];
	if(n == 0xFFFFFF)
		n = 0;
	if(n < 0)
		return 0;
	if(n > 0 && !validptr(ap, base+n-1))
		return 0;
	if(ap->check != Noval && checksum(base, n) != ap->check){
		print("flash: bad checksum\n");
		return 0;
	}
	*val = (char*)base;
	*len = n;
	return 1;
}

int
flashinit(void)
{
	int len;
	char *val;
	Flalloc *ap;
	void *addr;
	long mbytes;
	char type[20];

	flash.base = 0;
	flash.exec = 0;
	flash.size = 0;
	if(archflashreset(type, &addr, &mbytes) < 0){
		print("flash: flash not present or not enabled\n");	/* shouldn't happen */
		return 0;
	}
	flash.size = mbytes;
	flash.base = addr;
	flash.exec = flash.base + BOOTOFF;
	flash.config = nil;
	flash.conflen = 0;

	for(ap = (Flalloc*)(flash.base+CONFIGLIM)-1; memcmp(ap->sig, flashsig, 4) == 0; ap--){
		if(0)
			print("conf #%8.8lux: #%x #%6.6lux\n", ap, ap->tag, ap->base);
		if(ap->tag == Tconf &&
		   flashcheck(ap, &val, &len) &&
		   len >= sizeof(conftag)-1 &&
		   memcmp(val, conftag, sizeof(conftag)-1) == 0){
			flash.config = val;
			flash.conflen = len;
			if(0)
				print("flash: found config %8.8lux(%d):\n%s\n", val, len, val);
		}
	}
	if(flash.config == nil)
		print("flash: no config\n");
	else
		print("flash config %8.8lux(%d):\n%s\n", flash.config, flash.conflen, flash.config);
	if(issqueezed(flash.exec) == Q_MAGIC){
		print("flash: squeezed powerpc kernel installed\n");
		return 1<<0;
	}
	if(GLLONG(flash.exec) == Q_MAGIC){
		print("flash: unsqueezed powerpc kernel installed\n");
		return 1<<0;
	}
	flash.exec = 0;
	print("flash: no powerpc kernel in Flash\n");
	return 0;
}

char*
flashconfig(int)
{
	return flash.config;
}

int
flashbootable(int)
{
	return flash.exec != nil && (issqueezed(flash.exec) || GLLONG(flash.exec) == Q_MAGIC);
}

int
flashboot(int)
{
	ulong entry, addr;
	void (*b)(void);
	Exec *ep;
	Block in;
	long n;
	uchar *p;

	if(flash.exec == 0)
		return -1;
	p = flash.exec;
	if(GLLONG(p) == Q_MAGIC){
		/* unsqueezed: copy data and perhaps text, then jump to it */
		ep = (Exec*)p;
		entry = PADDR(GLLONG(ep->entry));
		p += sizeof(Exec);
		addr = entry;
		n = GLLONG(ep->text);
		if(addr != (ulong)p){
			memmove((void*)addr, p, n);
			print("text: %8.8lux <- %8.8lux [%ld]\n", addr, p, n);
		}
		p += n;
		if(entry >= FLASHMEM)
			addr = 3*BY2PG;	/* kernel text is in Flash, data in RAM */
		else
			addr = PGROUND(addr+n);
		n = GLLONG(ep->data);
		memmove((void*)addr, p, n);
		print("data: %8.8lux <- %8.8lux [%ld]\n", addr, p, n);
	}else{
		in.data = p;
		in.rp = in.data;
		in.lim = p+BOOTLEN;
		in.wp = in.lim;
		n = unsqueezef(&in, &entry);
		if(n < 0)
			return -1;
	}
	print("entry=0x%lux\n", entry);
	uartwait();
	scc2stop();
	/*
	 *  Go to new code. It's up to the program to get its PC relocated to
	 *  the right place.
	 */
	b = (void (*)(void))KADDR(PADDR(entry));
	(*b)();
	return -1;
}