summaryrefslogtreecommitdiff
path: root/package/aboot/src/utils.c
blob: e2f559c300087980e69ecd9c2fd2e8d27ac927df (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
#include <linux/kernel.h>

#include "hwrpb.h"
#include <linux/version.h>
#include "system.h"

#include "string.h"

#include <stdarg.h>
#include <errno.h>

#include "aboot.h"
#include "cons.h"

unsigned long free_mem_ptr = 0;


int printf(const char *fmt, ...)
{
	static char buf[1024];
	va_list args;
	long len, num_lf;
	char *src, *dst;

	va_start(args, fmt);
	len = vsprintf(buf, fmt, args);
	va_end(args);

	/* count number of linefeeds in string: */

	num_lf = 0;
	for (src = buf; *src; ++src) {
		if (*src == '\n') {
			++num_lf;
		}
	}

	if (num_lf) {
		/* expand each linefeed into carriage-return/linefeed: */
		for (dst = src + num_lf; src >= buf; ) {
			if (*src == '\n') {
				*dst-- = '\r';
			}
			*dst-- = *src--;
		}
	}
	return cons_puts(buf, len + num_lf);
}


/*
 * Find a physical address of a virtual object..
 *
 * This is easy using the virtual page table address.
 */
struct pcb_struct *find_pa(unsigned long *vptb, struct pcb_struct *pcb)
{
	unsigned long address = (unsigned long) pcb;
	unsigned long result;

	result = vptb[address >> 13];
	result >>= 32;
	result <<= 13;
	result |= address & 0x1fff;
	return (struct pcb_struct *) result;
}	

/*
 * This function moves into OSF/1 pal-code, and has a temporary
 * PCB for that. The kernel proper should replace this PCB with
 * the real one as soon as possible.
 *
 * The page table muckery in here depends on the fact that the boot
 * code has the L1 page table identity-map itself in the second PTE
 * in the L1 page table. Thus the L1-page is virtually addressable
 * itself (through three levels) at virtual address 0x200802000.
 *
 * As we don't want it there anyway, we also move the L1 self-map
 * up as high as we can, so that the last entry in the L1 page table
 * maps the page tables.
 *
 * As a result, the OSF/1 pal-code will instead use a virtual page table
 * map located at 0xffffffe00000000.
 */
#define pcb_va ((struct pcb_struct *) 0x20000000)
#define old_vptb (0x0000000200000000UL)
#define new_vptb (0xfffffffe00000000UL)
void pal_init(void)
{
	unsigned long i, rev, sum;
	unsigned long *L1, *l;
	struct percpu_struct * percpu;
	struct pcb_struct * pcb_pa;

	/* Find the level 1 page table and duplicate it in high memory */
	L1 = (unsigned long *) 0x200802000UL; /* (1<<33 | 1<<23 | 1<<13) */
	L1[1023] = L1[1];

	percpu = (struct percpu_struct *) (INIT_HWRPB->processor_offset
					   + (unsigned long) INIT_HWRPB),
	pcb_va->ksp = 0;
	pcb_va->usp = 0;
	pcb_va->ptbr = L1[1] >> 32;
	pcb_va->asn = 0;
	pcb_va->pcc = 0;
	pcb_va->unique = 0;
	pcb_va->flags = 1;
	pcb_pa = find_pa((unsigned long *) old_vptb, pcb_va);
	printf("aboot: switching to OSF/1 PALcode");
	/*
	 * a0 = 2 (OSF)
	 * a1 = return address, but we give the asm the virtual addr of the PCB
	 * a2 = physical addr of PCB
	 * a3 = new virtual page table pointer
	 * a4 = KSP (but we give it 0, asm sets it)
	 */
	i = switch_to_osf_pal(
		2,
		pcb_va,
		pcb_pa,
		new_vptb,
		0);
	if (i) {
		printf("---failed, code %ld\n", i);
		halt();
	}
	rev = percpu->pal_revision = percpu->palcode_avail[2];

	INIT_HWRPB->vptb = new_vptb;

	/* update checksum: */
	sum = 0;
	for (l = (unsigned long *) INIT_HWRPB; l < (unsigned long *) &INIT_HWRPB->chksum; ++l)
		sum += *l;
	INIT_HWRPB->chksum = sum;

	printf(" version %ld.%ld\n", (rev >> 8) & 0xff, rev & 0xff);
	/* remove the old virtual page-table mapping */
	L1[1] = 0;
	tbia();
}

int check_memory(unsigned long start, unsigned long size)
{
	unsigned long phys_start, start_pfn, end_pfn;
	struct memclust_struct *cluster;
	struct memdesc_struct *memdesc;
	int i;

	/*
	 * Get the physical address start. 
	 * If 43-bit superpage is being used (VA<63:41> = 0x7ffffe)
	 * then the "correct" translation across all implementations is to
	 * sign extend the VA from bit 40. Othewise, assume it's already a
	 * physical address.
	 */
	phys_start = start;
	if (((long)phys_start >> 41) == -2) 
		phys_start = (long)((start) << (64-41)) >> (64-41);
	start_pfn = phys_start >> PAGE_SHIFT;
	end_pfn = (phys_start + size - 1) >> PAGE_SHIFT;

	memdesc = (struct memdesc_struct *)
	    (INIT_HWRPB->mddt_offset + (unsigned long) INIT_HWRPB);
	for (cluster = memdesc->cluster, i = 0;
	     i < memdesc->numclusters; 
	     i++, cluster++) {
		if ((cluster->start_pfn > end_pfn) ||
		    ((cluster->start_pfn + cluster->numpages) <= start_pfn))
			continue;	/* no overlap */

		/* 
		 * This cluster overlaps the memory we're checking, check
		 * the usage:
		 * 	bit 0 is console/PAL reserved
		 * 	bit 1 is non-volatile
		 * If either is set, it's a problem, return -EBUSY
		 */
		if (cluster->usage & 3)
			return -EBUSY;	/* reserved */

		/*
		 * It's not reserved, take it out of what we're checking
		 */
		if (cluster->start_pfn <= end_pfn) 
			end_pfn = cluster->start_pfn - 1;
		if ((cluster->start_pfn + cluster->numpages) > start_pfn)
			start_pfn = cluster->start_pfn + cluster->numpages;

		if (end_pfn < start_pfn)
			return 0;	/* all found, ok */
	}

	/* no conflict, but not all memory found */
	return -ENOMEM;
}

unsigned long memory_end(void)
{
	int i;
	unsigned long high = 0;
	struct memclust_struct *cluster;
	struct memdesc_struct *memdesc;

	memdesc = (struct memdesc_struct *)
	  (INIT_HWRPB->mddt_offset + (unsigned long) INIT_HWRPB);
	cluster = memdesc->cluster;
	for (i = memdesc->numclusters; i > 0; i--, cluster++) {
		unsigned long tmp;

		if (cluster->usage != 0) {
			/* this is a PAL or NVRAM cluster (not for the OS) */
			continue;
		}

		tmp = (cluster->start_pfn + cluster->numpages) << page_shift;
		if (tmp > high) {
			high = tmp;
		}
	}
	return page_offset + high;
}


static void error(char *x)
{
	printf("%s\n", x);
	_longjmp(jump_buffer, 1);
}


void unzip_error(char *x)
{
	printf("\nunzip: ");
	error(x);
}


void *malloc(size_t size)
{
	if (!free_mem_ptr) {
		free_mem_ptr = memory_end();
	}

	free_mem_ptr = (free_mem_ptr - size) & ~(sizeof(long) - 1);
	if ((char*) free_mem_ptr <= dest_addr + INIT_HWRPB->pagesize) {
		error("\nout of memory");
	}
	return (void*) free_mem_ptr;
}


void free(void *where)
{
	/* don't care */
}


void
getline (char *buf, int maxlen)
{
	int len=0;
	char c;

	do {
		c = cons_getchar();
		switch (c) {
		      case 0:
		      case 10:
		      case 13:
			break;
		      case 8:
		      case 127:
			if (len > 0) {
				--len;
				cons_putchar(8);
				cons_putchar(' ');
				cons_putchar(8);
			}
			break;

		      default:
			if (len < maxlen-1 && c >= ' ') {
				buf[len] = c;
				len++;
				cons_putchar(c);
			}
			break;
		}
	} while (c != 13 && c != 10);
	buf[len] = 0;
}