File: main.c

package info (click to toggle)
emile 0.10-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,716 kB
  • ctags: 2,737
  • sloc: ansic: 18,908; makefile: 726; asm: 622; sh: 2
file content (286 lines) | stat: -rw-r--r-- 6,162 bytes parent folder | download
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
/*
 *
 * (c) 2004-2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
 *
 */

#define __NO_INLINE__

#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <unistd.h>

#include <macos/types.h>
#include <macos/gestalt.h>

#include "bank.h"
#include "memory.h"
#if defined(ARCH_M68K)
#if defined(__LINUX__)
#include "bootinfo.h"
#elif defined(__NETBSD__)
#include "bootenv.h"
#endif
#endif
#ifdef ARCH_PPC
#include "bootx.h"
#endif
#include "arch.h"
#include "misc.h"
#include "load.h"
#include "console.h"
#include "vga.h"
#include "driver.h"
#include "config.h"

#include "enter_kernel.h"

#ifdef ARCH_M68K
#define KERNEL_ALIGN	(256L * 1024L)	// Kernel alignment, on 256K boundary
#if defined(__LINUX__)
#define BI_ALLOC_SIZE	(4096L)		// Allocate 4K for bootinfo
#elif defined(__NETBSD__)
#define BI_ALLOC_SIZE	(4096L)
#endif
#endif


int start(emile_l2_header_t* info)
{
	char * kernel;
#ifdef ARCH_M68K
	entry_t entry;
	unsigned long physImage;
#ifdef USE_MMU
	unsigned long physical;
#ifdef __LINUX__
	unsigned long aligned_size;
	unsigned long aligned_addr;
#endif /* __LINUX__ */
#endif /* USE_MMU */
	unsigned long start_mem;
	unsigned long entry_point;
#endif /* ARCH_M68K */
#ifdef ARCH_PPC
	PPCRegisterList regs;
#endif
	int ret;
	char *ramdisk_start;
	int bootstrap_size;
	unsigned long kernel_size;
	unsigned long ramdisk_size;
	char *kernel_path;
	char *ramdisk_path;
	char *command_line;

	printf("EMILE v"VERSION" (c) 2004,2005 Laurent Vivier\n");

	arch_init();

	init_memory_map();

#ifdef BANK_DUMP
	bank_dump();
#endif

	printf("Available Memory: %ld kB\n", bank_mem_avail() / 1024);

	enter_kernel_init();

#ifdef ARCH_M68K
	if (arch_type == gestalt68k)
	{
#if defined(__LINUX__)
		/* and BI_ALLOC_SIZE for bootinfo */

		bootstrap_size = BI_ALLOC_SIZE + 
				 end_enter_kernel - enter_kernel;
#elif defined(__NETBSD__)
		bootstrap_size = BI_ALLOC_SIZE + 
				 end_enter_kernel - enter_kernel;
#endif
	}
#ifndef ARCH_PPC
	else
		error("EMILE doesn't support your architecture");
#endif
#endif

	if (read_config(info, &kernel_path, &command_line, &ramdisk_path) != 0)
		error("cannot read configuration\n");

	/* load kernel */

	kernel = load_kernel(kernel_path,
			     bootstrap_size, 
			     &start_mem, &entry_point, &kernel_size);
	if (kernel == NULL)
		error("Cannot load and uncompress kernel image\n");

#ifdef ARCH_M68K
	if (arch_type == gestalt68k)
	{
		unsigned long enter_size = end_enter_kernel - enter_kernel;

		/* copy enter_kernel at end of kernel */

		memcpy((char*)kernel +
			kernel_size + bootstrap_size - enter_size,
	       		(char*)enter_kernel, enter_size);

		end_enter_kernel = (unsigned long)kernel + 
				   kernel_size + bootstrap_size;
		enter_kernel = (unsigned long)kernel + 
			       bootstrap_size - enter_size + kernel_size;
	}
#endif

	/* load ramdisk if needed */

	if (ramdisk_path)
	{
		ramdisk_start = load_ramdisk(ramdisk_path, &ramdisk_size);
		if (ramdisk_start == NULL)
			error("Cannot open ramdisk\n");
	}
	else
	{
		ramdisk_start = 0;
		printf("no RAMDISK\n");
	}

#ifdef ARCH_M68K
	if (arch_type == gestalt68k)
	{
		/* compute final address of kernel */

		if  (mmu_type == gestaltNoMMU)
		{
			unsigned long size = end_enter_kernel - enter_kernel;

#if defined(__LINUX__)
			/* initialize bootinfo structure */

			bootinfo_init(command_line, 
		      		ramdisk_start, ramdisk_size);

			/* set bootinfo at end of kernel image */

			set_kernel_bootinfo(kernel + kernel_size);

			physImage = (unsigned long)kernel;
#endif
			entry = (entry_t)(start_mem - size);

			printf("\n");
			printf("Ok, booting the kernel.\n");

			memcpy(entry, (char*)enter_kernel, size);
		} else
#ifndef USE_MMU
			error("Unsupported MMU");
#else
		{
			ret = logical2physical((unsigned long)kernel, &physImage);

			/* disable and flush cache */

			disable_cache();

#if defined(__LINUX__)
			/* initialize bootinfo structure */

			bootinfo_init(command_line, 
		      		ramdisk_start, ramdisk_size);

			/* add KERNEL_ALIGN if we have to align */
		 
			aligned_size = boot_info.memory[0].addr & (KERNEL_ALIGN - 1);
			if ( aligned_size > 0 )
			{
				aligned_size = KERNEL_ALIGN - aligned_size;
				aligned_addr = boot_info.memory[0].addr + aligned_size;
				aligned_size = boot_info.memory[0].size - aligned_size;
				boot_info.memory[0].addr = aligned_addr;
				boot_info.memory[0].size = aligned_size;
			}

			/* set bootinfo at end of kernel image */

			set_kernel_bootinfo(kernel + kernel_size);

#elif defined(__NETBSD__)
			bootenv_init(kernel + kernel_size);
#endif

			printf("\n");
			printf("Ok, booting the kernel.\n");

			ret = logical2physical(enter_kernel, &physical);
			entry = (entry_t)physical;

			if ( (ret == 0) && (enter_kernel != (unsigned long)entry) )
			{
				unsigned long logi;
				unsigned long size = end_enter_kernel - enter_kernel;

				logi = vga_get_video();
				ret = logical2physical(logi, &physical);
				entry = (entry_t)physical;
	
				memcpy((char*)logi, (char*)enter_kernel, size);
				memcpy((char*)entry, (char*)enter_kernel, size);
			}
		}
#endif /* USE_MMU */
	}
	else
#ifndef ARCH_PPC
		error("EMILE doesn't support your architecture");
#endif
#endif /* ARCH_M68K */
#ifdef ARCH_PPC
	if (arch_type == gestaltPowerPC)
	{
		bootx_init(command_line, ramdisk_start, ramdisk_size);

		regs.PC      = (u_int32_t)kernel;
#define BOOT_KERNEL_STACK_SIZE 65536
		regs.GPR[1]  = (u_int32_t)malloc_contiguous(BOOT_KERNEL_STACK_SIZE)
				+ BOOT_KERNEL_STACK_SIZE - 512;
		regs.GPR[2]  = 0;
		regs.GPR[3]  = 'BooX';
		regs.GPR[4]  = (u_int32_t)&bootx_infos;

		/* Set up the info for BAT mapping on Nubus */

		regs.GPR[5]  = vga_get_videobase() & 0xFF800000UL;
		regs.GPR[11]  = 1;

		printf("\n");
		printf("Ok, booting the kernel.\n");
	}
	else
		error("EMILE doesn't support your architecture");
#endif

	turn_off_interrupts();

	 asm("ori.w #0x0700,%sr");

	/* kick off */

#ifdef ARCH_M68K
	if (arch_type == gestalt68k)
		entry(physImage, kernel_size + BI_ALLOC_SIZE, start_mem, entry_point);
#endif
#ifdef ARCH_PPC
	if (arch_type == gestaltPowerPC)
		enter_kernelPPC((unsigned long)kernel, &regs);
#endif

	error("Kernel startup failed");

	return 0;
}