vaseboot/VasEBoot-core/kern/ieee1275/init.c

327 lines
8.3 KiB
C

/* init.c -- Initialize VasEBoot on the newworld mac (PPC). */
/*
* VasEBoot -- GRand Unified Bootloader
* Copyright (C) 2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
*
* VasEBoot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VasEBoot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VasEBoot. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/kernel.h>
#include <VasEBoot/dl.h>
#include <VasEBoot/disk.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/partition.h>
#include <VasEBoot/normal.h>
#include <VasEBoot/fs.h>
#include <VasEBoot/setjmp.h>
#include <VasEBoot/env.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/time.h>
#include <VasEBoot/ieee1275/console.h>
#include <VasEBoot/ieee1275/ofdisk.h>
#include <VasEBoot/ieee1275/ieee1275.h>
#include <VasEBoot/net.h>
#include <VasEBoot/offsets.h>
#include <VasEBoot/memory.h>
#include <VasEBoot/loader.h>
#ifdef __i386__
#include <VasEBoot/cpu/tsc.h>
#endif
#ifdef __sparc__
#include <VasEBoot/machine/kernel.h>
#endif
/* The minimal heap size we can live with. */
#define HEAP_MIN_SIZE (unsigned long) (2 * 1024 * 1024)
/* The maximum heap size we're going to claim */
#ifdef __i386__
#define HEAP_MAX_SIZE (unsigned long) (64 * 1024 * 1024)
#else
#define HEAP_MAX_SIZE (unsigned long) (32 * 1024 * 1024)
#endif
/* If possible, we will avoid claiming heap above this address, because it
seems to cause relocation problems with OSes that link at 4 MiB */
#ifdef __i386__
#define HEAP_MAX_ADDR (unsigned long) (64 * 1024 * 1024)
#else
#define HEAP_MAX_ADDR (unsigned long) (32 * 1024 * 1024)
#endif
extern char _start[];
extern char _end[];
#ifdef __sparc__
VasEBoot_addr_t VasEBoot_ieee1275_original_stack;
#endif
void
VasEBoot_exit (void)
{
VasEBoot_ieee1275_exit ();
}
/* Translate an OF filesystem path (separated by backslashes), into a VasEBoot
path (separated by forward slashes). */
static void
VasEBoot_translate_ieee1275_path (char *filepath)
{
char *backslash;
backslash = VasEBoot_strchr (filepath, '\\');
while (backslash != 0)
{
*backslash = '/';
backslash = VasEBoot_strchr (filepath, '\\');
}
}
void (*VasEBoot_ieee1275_net_config) (const char *dev, char **device, char **path,
char *bootpath);
void
VasEBoot_machine_get_bootlocation (char **device, char **path)
{
char *bootpath;
VasEBoot_ssize_t bootpath_size;
char *filename;
char *type;
if (VasEBoot_ieee1275_get_property_length (VasEBoot_ieee1275_chosen, "bootpath",
&bootpath_size)
|| bootpath_size <= 0)
{
/* Should never happen. */
VasEBoot_printf ("/chosen/bootpath property missing!\n");
return;
}
bootpath = (char *) VasEBoot_malloc ((VasEBoot_size_t) bootpath_size + 64);
if (! bootpath)
{
VasEBoot_print_error ();
return;
}
VasEBoot_ieee1275_get_property (VasEBoot_ieee1275_chosen, "bootpath", bootpath,
(VasEBoot_size_t) bootpath_size + 1, 0);
bootpath[bootpath_size] = '\0';
/* Transform an OF device path to a VasEBoot path. */
type = VasEBoot_ieee1275_get_device_type (bootpath);
if (type && VasEBoot_strcmp (type, "network") == 0)
{
char *dev, *canon;
char *ptr;
dev = VasEBoot_ieee1275_get_aliasdevname (bootpath);
canon = VasEBoot_ieee1275_canonicalise_devname (dev);
ptr = canon + VasEBoot_strlen (canon) - 1;
while (ptr > canon && (*ptr == ',' || *ptr == ':'))
ptr--;
ptr++;
*ptr = 0;
if (VasEBoot_ieee1275_net_config)
VasEBoot_ieee1275_net_config (canon, device, path, bootpath);
VasEBoot_free (dev);
VasEBoot_free (canon);
}
else
*device = VasEBoot_ieee1275_encode_devname (bootpath);
VasEBoot_free (type);
filename = VasEBoot_ieee1275_get_filename (bootpath);
if (filename)
{
char *lastslash = VasEBoot_strrchr (filename, '\\');
/* Truncate at last directory. */
if (lastslash)
{
*lastslash = '\0';
VasEBoot_translate_ieee1275_path (filename);
*path = filename;
}
}
VasEBoot_free (bootpath);
}
/* Claim some available memory in the first /memory node. */
#ifdef __sparc__
static void
VasEBoot_claim_heap (void)
{
VasEBoot_mm_init_region ((void *) (VasEBoot_modules_get_end ()
+ VasEBoot_KERNEL_MACHINE_STACK_SIZE), 0x200000);
}
#else
/* Helper for VasEBoot_claim_heap. */
static int
heap_init (VasEBoot_uint64_t addr, VasEBoot_uint64_t len, VasEBoot_memory_type_t type,
void *data)
{
unsigned long *total = data;
if (type != VasEBoot_MEMORY_AVAILABLE)
return 0;
if (VasEBoot_ieee1275_test_flag (VasEBoot_IEEE1275_FLAG_NO_PRE1_5M_CLAIM))
{
if (addr + len <= 0x180000)
return 0;
if (addr < 0x180000)
{
len = addr + len - 0x180000;
addr = 0x180000;
}
}
len -= 1; /* Required for some firmware. */
/* Never exceed HEAP_MAX_SIZE */
if (*total + len > HEAP_MAX_SIZE)
len = HEAP_MAX_SIZE - *total;
/* Avoid claiming anything above HEAP_MAX_ADDR, if possible. */
if ((addr < HEAP_MAX_ADDR) && /* if it's too late, don't bother */
(addr + len > HEAP_MAX_ADDR) && /* if it wasn't available anyway, don't bother */
(*total + (HEAP_MAX_ADDR - addr) > HEAP_MIN_SIZE)) /* only limit ourselves when we can afford to */
len = HEAP_MAX_ADDR - addr;
/* In theory, firmware should already prevent this from happening by not
listing our own image in /memory/available. The check below is intended
as a safeguard in case that doesn't happen. However, it doesn't protect
us from corrupting our module area, which extends up to a
yet-undetermined region above _end. */
if ((addr < (VasEBoot_addr_t) _end) && ((addr + len) > (VasEBoot_addr_t) _start))
{
VasEBoot_printf ("Warning: attempt to claim over our own code!\n");
len = 0;
}
if (len)
{
VasEBoot_err_t err;
/* Claim and use it. */
err = VasEBoot_claimmap (addr, len);
if (err)
return err;
VasEBoot_mm_init_region ((void *) (VasEBoot_addr_t) addr, len);
}
*total += len;
if (*total >= HEAP_MAX_SIZE)
return 1;
return 0;
}
static void
VasEBoot_claim_heap (void)
{
unsigned long total = 0;
if (VasEBoot_ieee1275_test_flag (VasEBoot_IEEE1275_FLAG_FORCE_CLAIM))
heap_init (VasEBoot_IEEE1275_STATIC_HEAP_START, VasEBoot_IEEE1275_STATIC_HEAP_LEN,
1, &total);
else
VasEBoot_machine_mmap_iterate (heap_init, &total);
}
#endif
static void
VasEBoot_parse_cmdline (void)
{
VasEBoot_ssize_t actual;
char args[256];
if (VasEBoot_ieee1275_get_property (VasEBoot_ieee1275_chosen, "bootargs", &args,
sizeof args, &actual) == 0
&& actual > 1)
{
int i = 0;
while (i < actual)
{
char *command = &args[i];
char *end;
char *val;
end = VasEBoot_strchr (command, ';');
if (end == 0)
i = actual; /* No more commands after this one. */
else
{
*end = '\0';
i += end - command + 1;
while (VasEBoot_isspace(args[i]))
i++;
}
/* Process command. */
val = VasEBoot_strchr (command, '=');
if (val)
{
*val = '\0';
VasEBoot_env_set (command, val + 1);
}
}
}
}
VasEBoot_addr_t VasEBoot_modbase;
void
VasEBoot_machine_init (void)
{
VasEBoot_modbase = ALIGN_UP((VasEBoot_addr_t) _end
+ VasEBoot_KERNEL_MACHINE_MOD_GAP,
VasEBoot_KERNEL_MACHINE_MOD_ALIGN);
VasEBoot_ieee1275_init ();
VasEBoot_console_init_early ();
VasEBoot_claim_heap ();
VasEBoot_console_init_lately ();
VasEBoot_ofdisk_init ();
VasEBoot_parse_cmdline ();
#ifdef __i386__
VasEBoot_tsc_init ();
#else
VasEBoot_install_get_time_ms (VasEBoot_rtc_get_time_ms);
#endif
}
void
VasEBoot_machine_fini (int flags)
{
if (flags & VasEBoot_LOADER_FLAG_NORETURN)
{
VasEBoot_ofdisk_fini ();
VasEBoot_console_fini ();
}
}
VasEBoot_uint64_t
VasEBoot_rtc_get_time_ms (void)
{
VasEBoot_uint32_t msecs = 0;
VasEBoot_ieee1275_milliseconds (&msecs);
return msecs;
}