EMILE/second/main.c

319 lines
6.8 KiB
C
Raw Normal View History

2004-02-15 20:46:45 +00:00
/*
*
2005-10-10 21:51:02 +00:00
* (c) 2004-2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
2004-02-15 20:46:45 +00:00
*
*/
#define __NO_INLINE__
2004-02-15 20:46:45 +00:00
#include <stdio.h>
#include <malloc.h>
2004-02-21 01:43:51 +00:00
#include <string.h>
#include <unistd.h>
2004-02-15 20:46:45 +00:00
#include <macos/types.h>
#include <macos/gestalt.h>
#include "bank.h"
2004-02-15 20:46:45 +00:00
#include "memory.h"
2005-09-19 22:10:41 +00:00
#if defined(ARCH_M68K)
#if defined(__LINUX__)
2004-02-15 20:46:45 +00:00
#include "bootinfo.h"
2005-09-19 22:10:41 +00:00
#elif defined(__NETBSD__)
#include "bootenv.h"
#endif
#endif
#ifdef ARCH_PPC
#include "bootx.h"
#endif
2004-02-26 22:51:37 +00:00
#include "arch.h"
#include "misc.h"
#include "load.h"
#include "console.h"
#include "vga.h"
2005-10-10 21:51:02 +00:00
#include "driver.h"
#include "config.h"
2004-02-15 20:46:45 +00:00
2005-10-20 22:00:44 +00:00
#include "enter_kernel.h"
2004-02-24 21:27:48 +00:00
2005-10-20 22:00:44 +00:00
#ifdef ARCH_M68K
2004-02-24 21:27:48 +00:00
#define KERNEL_ALIGN (256L * 1024L) // Kernel alignment, on 256K boundary
2005-09-19 22:10:41 +00:00
#if defined(__LINUX__)
2005-05-13 17:25:13 +00:00
#define BI_ALLOC_SIZE (4096L) // Allocate 4K for bootinfo
2005-09-19 22:10:41 +00:00
#elif defined(__NETBSD__)
#define BI_ALLOC_SIZE (4096L)
#endif
2005-10-20 22:00:44 +00:00
#endif
2005-05-13 17:25:13 +00:00
2005-10-20 22:00:44 +00:00
static void banner(void)
{
printf("Early Macintosh Image LoadEr"
#if defined(__LINUX__) && defined(__NETBSD__)
#error Cannot support LINUX AND NETBSD
#elif defined(__LINUX__)
" for Linux"
#elif defined(__NETBSD__)
" for NetBSD"
2005-05-13 17:25:13 +00:00
#endif
2005-10-20 22:00:44 +00:00
#if defined(ARCH_M68K) && defined(ARCH_PPC)
" (mixed mode)\n");
#elif defined(ARCH_M68K)
#ifdef USE_MMU
" on Motorola 680x0\n");
#else
" on Motorola 68000\n");
#endif /* USE_MMU */
#elif defined(ARCH_PPC)
" on PowerPC\n");
#else
" (unknown processor)\n");
2005-05-13 17:25:13 +00:00
#endif
2005-10-20 22:00:44 +00:00
printf("EMILE v"VERSION" (c) 2004,2005 Laurent Vivier\n");
printf("This is free software, redistribute it under GPL\n");
}
2004-02-15 20:46:45 +00:00
int start(emile_l2_header_t* info)
2004-02-15 20:46:45 +00:00
{
char * kernel;
#ifdef ARCH_M68K
2005-05-13 17:25:13 +00:00
entry_t entry;
unsigned long physImage;
2005-10-20 21:12:13 +00:00
#ifdef USE_MMU
unsigned long physical;
2005-09-19 22:10:41 +00:00
#ifdef __LINUX__
2005-05-12 21:04:29 +00:00
unsigned long aligned_size;
unsigned long aligned_addr;
2005-09-19 22:10:41 +00:00
#endif /* __LINUX__ */
2005-10-20 21:12:13 +00:00
#endif /* USE_MMU */
2005-05-13 17:25:13 +00:00
unsigned long start_mem;
unsigned long entry_point;
2005-09-19 22:10:41 +00:00
#endif /* ARCH_M68K */
2005-05-13 17:25:13 +00:00
#ifdef ARCH_PPC
PPCRegisterList regs;
#endif
int ret;
char *ramdisk_start;
2005-05-16 21:38:33 +00:00
int bootstrap_size;
unsigned long kernel_size;
unsigned long ramdisk_size;
char *kernel_path;
char *ramdisk_path;
char *command_line;
2004-02-15 20:46:45 +00:00
2005-10-20 22:00:44 +00:00
banner();
2004-02-15 20:46:45 +00:00
2004-05-26 18:32:49 +00:00
arch_init();
init_memory_map();
#ifdef BANK_DUMP
bank_dump();
#endif
printf("Available Memory: %ld kB\n", bank_mem_avail() / 1024);
2005-10-20 22:00:44 +00:00
enter_kernel_init();
2005-05-10 21:49:28 +00:00
#ifdef ARCH_M68K
if (arch_type == gestalt68k)
{
2005-09-19 22:10:41 +00:00
#if defined(__LINUX__)
2005-05-16 21:38:33 +00:00
/* and BI_ALLOC_SIZE for bootinfo */
bootstrap_size = BI_ALLOC_SIZE +
end_enter_kernel - enter_kernel;
2005-09-19 22:10:41 +00:00
#elif defined(__NETBSD__)
bootstrap_size = BI_ALLOC_SIZE +
end_enter_kernel - enter_kernel;
#endif
}
#ifndef ARCH_PPC
2005-05-16 21:38:33 +00:00
else
error("EMILE doesn't support your architecture");
2005-10-20 22:00:44 +00:00
#endif
2005-05-10 21:49:28 +00:00
#endif
if (read_config(info, &kernel_path, &command_line, &ramdisk_path) != 0)
error("cannot read configuration\n");
2004-02-26 22:51:37 +00:00
/* 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");
2005-05-13 17:25:13 +00:00
#ifdef ARCH_M68K
2005-05-16 21:38:33 +00:00
if (arch_type == gestalt68k)
{
2005-09-19 22:10:41 +00:00
unsigned long enter_size = end_enter_kernel - enter_kernel;
2005-05-16 21:38:33 +00:00
/* copy enter_kernel at end of kernel */
2004-02-15 20:46:45 +00:00
2005-09-19 22:10:41 +00:00
memcpy((char*)kernel +
kernel_size + bootstrap_size - enter_size,
2005-09-19 22:10:41 +00:00
(char*)enter_kernel, enter_size);
2005-09-19 22:10:41 +00:00
end_enter_kernel = (unsigned long)kernel +
kernel_size + bootstrap_size;
2005-09-19 22:10:41 +00:00
enter_kernel = (unsigned long)kernel +
bootstrap_size - enter_size + kernel_size;
2005-05-16 21:38:33 +00:00
}
2005-05-13 17:25:13 +00:00
#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");
}
2004-06-16 17:22:22 +00:00
#ifdef ARCH_M68K
if (arch_type == gestalt68k)
{
/* compute final address of kernel */
2004-02-23 00:20:11 +00:00
if (mmu_type == gestaltNoMMU)
{
unsigned long size = end_enter_kernel - enter_kernel;
2005-09-19 22:10:41 +00:00
#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;
2005-09-19 22:10:41 +00:00
#endif
entry = (entry_t)(start_mem - size);
2005-05-14 01:12:46 +00:00
printf("\n");
printf("Physical address of kernel will be 0x%08lx\n",
start_mem);
printf("Ok, booting the kernel.\n");
2004-02-23 00:20:11 +00:00
memcpy(entry, (char*)enter_kernel, size);
2005-10-20 21:12:13 +00:00
} else
#ifndef USE_MMU
error("Unsupported MMU");
#else
{
ret = logical2physical((unsigned long)kernel, &physImage);
/* disable and flush cache */
disable_cache();
2004-02-21 01:43:51 +00:00
2005-09-19 22:10:41 +00:00
#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);
2005-09-19 22:10:41 +00:00
#elif defined(__NETBSD__)
bootenv_init(kernel + kernel_size);
2005-09-19 22:10:41 +00:00
#endif
printf("\n");
printf("Physical address of kernel will be 0x%08lx\n",
start_mem);
printf("Ok, booting the kernel.\n");
ret = logical2physical(enter_kernel, &physical);
2005-05-22 21:16:57 +00:00
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;
2004-02-21 01:43:51 +00:00
memcpy((char*)logi, (char*)enter_kernel, size);
memcpy((char*)entry, (char*)enter_kernel, size);
}
}
2005-10-20 21:12:13 +00:00
#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);
2005-05-13 17:25:13 +00:00
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;
2005-05-13 17:25:13 +00:00
printf("\n");
printf("Physical address of kernel will be 0x%08lx\n",
(unsigned long)kernel);
2005-05-14 01:12:46 +00:00
printf("Ok, booting the kernel.\n");
2004-02-21 01:43:51 +00:00
}
2005-05-14 01:12:46 +00:00
else
error("EMILE doesn't support your architecture");
#endif
2005-10-10 21:51:02 +00:00
turn_off_interrupts();
2005-05-25 22:05:35 +00:00
asm("ori.w #0x0700,%sr");
/* kick off */
2005-05-13 17:25:13 +00:00
#ifdef ARCH_M68K
if (arch_type == gestalt68k)
entry(physImage, kernel_size + BI_ALLOC_SIZE, start_mem, entry_point);
2005-05-13 17:25:13 +00:00
#endif
#ifdef ARCH_PPC
if (arch_type == gestaltPowerPC)
enter_kernelPPC((unsigned long)kernel, &regs);
#endif
2004-02-15 20:46:45 +00:00
error("Kernel startup failed");
2004-02-15 20:46:45 +00:00
return 0;
}