2N/A/* init.c -- Initialize GRUB on the newworld mac (PPC). */
2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/kernel.h>
2N/A#include <grub/dl.h>
2N/A#include <grub/disk.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/partition.h>
2N/A#include <grub/normal.h>
2N/A#include <grub/fs.h>
2N/A#include <grub/setjmp.h>
2N/A#include <grub/env.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/time.h>
2N/A#include <grub/ieee1275/console.h>
2N/A#include <grub/ieee1275/ofdisk.h>
2N/A#include <grub/ieee1275/ieee1275.h>
2N/A#include <grub/net.h>
2N/A#include <grub/offsets.h>
2N/A#include <grub/memory.h>
2N/A#ifdef __sparc__
2N/A#include <grub/machine/kernel.h>
2N/A#endif
2N/A
2N/A/* The minimal heap size we can live with. */
2N/A#define HEAP_MIN_SIZE (unsigned long) (2 * 1024 * 1024)
2N/A
2N/A/* The maximum heap size we're going to claim */
2N/A#define HEAP_MAX_SIZE (unsigned long) (32 * 1024 * 1024)
2N/A
2N/A/* If possible, we will avoid claiming heap above this address, because it
2N/A seems to cause relocation problems with OSes that link at 4 MiB */
2N/A#define HEAP_MAX_ADDR (unsigned long) (32 * 1024 * 1024)
2N/A
2N/Aextern char _start[];
2N/Aextern char _end[];
2N/A
2N/A#ifdef __sparc__
2N/Agrub_addr_t grub_ieee1275_original_stack;
2N/A#endif
2N/A
2N/Avoid
2N/Agrub_exit (void)
2N/A{
2N/A grub_ieee1275_exit ();
2N/A}
2N/A
2N/A/* Translate an OF filesystem path (separated by backslashes), into a GRUB
2N/A path (separated by forward slashes). */
2N/Astatic void
2N/Agrub_translate_ieee1275_path (char *filepath)
2N/A{
2N/A char *backslash;
2N/A
2N/A backslash = grub_strchr (filepath, '\\');
2N/A while (backslash != 0)
2N/A {
2N/A *backslash = '/';
2N/A backslash = grub_strchr (filepath, '\\');
2N/A }
2N/A}
2N/A
2N/Avoid (*grub_ieee1275_net_config) (const char *dev,
2N/A char **device,
2N/A char **path);
2N/Avoid
2N/Agrub_machine_get_bootlocation (char **device, char **path)
2N/A{
2N/A char bootpath[64]; /* XXX check length */
2N/A char *filename;
2N/A char *type;
2N/A
2N/A if (grub_ieee1275_get_property (grub_ieee1275_chosen, "bootpath", &bootpath,
2N/A sizeof (bootpath), 0))
2N/A {
2N/A /* Should never happen. */
2N/A grub_printf ("/chosen/bootpath property missing!\n");
2N/A return;
2N/A }
2N/A
2N/A /* Transform an OF device path to a GRUB path. */
2N/A
2N/A type = grub_ieee1275_get_device_type (bootpath);
2N/A if (type && grub_strcmp (type, "network") == 0)
2N/A {
2N/A char *dev, *canon;
2N/A char *ptr;
2N/A dev = grub_ieee1275_get_aliasdevname (bootpath);
2N/A canon = grub_ieee1275_canonicalise_devname (dev);
2N/A ptr = canon + grub_strlen (canon) - 1;
2N/A while (ptr > canon && (*ptr == ',' || *ptr == ':'))
2N/A ptr--;
2N/A ptr++;
2N/A *ptr = 0;
2N/A
2N/A if (grub_ieee1275_net_config)
2N/A grub_ieee1275_net_config (canon, device, path);
2N/A grub_free (dev);
2N/A grub_free (canon);
2N/A }
2N/A else
2N/A *device = grub_ieee1275_encode_devname (bootpath);
2N/A grub_free (type);
2N/A
2N/A filename = grub_ieee1275_get_filename (bootpath);
2N/A if (filename)
2N/A {
2N/A char *lastslash = grub_strrchr (filename, '\\');
2N/A
2N/A /* Truncate at last directory. */
2N/A if (lastslash)
2N/A {
2N/A *lastslash = '\0';
2N/A grub_translate_ieee1275_path (filename);
2N/A
2N/A *path = filename;
2N/A }
2N/A }
2N/A}
2N/A
2N/A/* Claim some available memory in the first /memory node. */
2N/A#ifdef __sparc__
2N/Astatic void
2N/Agrub_claim_heap (void)
2N/A{
2N/A grub_mm_init_region ((void *) (grub_modules_get_end ()
2N/A + GRUB_KERNEL_MACHINE_STACK_SIZE), 0x200000);
2N/A}
2N/A#else
2N/Astatic void
2N/Agrub_claim_heap (void)
2N/A{
2N/A unsigned long total = 0;
2N/A
2N/A auto int NESTED_FUNC_ATTR heap_init (grub_uint64_t addr, grub_uint64_t len,
2N/A grub_memory_type_t type);
2N/A int NESTED_FUNC_ATTR heap_init (grub_uint64_t addr, grub_uint64_t len,
2N/A grub_memory_type_t type)
2N/A {
2N/A if (type != 1)
2N/A return 0;
2N/A
2N/A if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_PRE1_5M_CLAIM))
2N/A {
2N/A if (addr + len <= 0x180000)
2N/A return 0;
2N/A
2N/A if (addr < 0x180000)
2N/A {
2N/A len = addr + len - 0x180000;
2N/A addr = 0x180000;
2N/A }
2N/A }
2N/A len -= 1; /* Required for some firmware. */
2N/A
2N/A /* Never exceed HEAP_MAX_SIZE */
2N/A if (total + len > HEAP_MAX_SIZE)
2N/A len = HEAP_MAX_SIZE - total;
2N/A
2N/A /* Avoid claiming anything above HEAP_MAX_ADDR, if possible. */
2N/A if ((addr < HEAP_MAX_ADDR) && /* if it's too late, don't bother */
2N/A (addr + len > HEAP_MAX_ADDR) && /* if it wasn't available anyway, don't bother */
2N/A (total + (HEAP_MAX_ADDR - addr) > HEAP_MIN_SIZE)) /* only limit ourselves when we can afford to */
2N/A len = HEAP_MAX_ADDR - addr;
2N/A
2N/A /* In theory, firmware should already prevent this from happening by not
2N/A listing our own image in /memory/available. The check below is intended
2N/A as a safeguard in case that doesn't happen. However, it doesn't protect
2N/A us from corrupting our module area, which extends up to a
2N/A yet-undetermined region above _end. */
2N/A if ((addr < (grub_addr_t) _end) && ((addr + len) > (grub_addr_t) _start))
2N/A {
2N/A grub_printf ("Warning: attempt to claim over our own code!\n");
2N/A len = 0;
2N/A }
2N/A
2N/A if (len)
2N/A {
2N/A /* Claim and use it. */
2N/A if (grub_claimmap (addr, len) < 0)
2N/A return grub_error (GRUB_ERR_OUT_OF_MEMORY,
2N/A "failed to claim heap at 0x%llx, len 0x%llx",
2N/A addr, len);
2N/A grub_mm_init_region ((void *) (grub_addr_t) addr, len);
2N/A }
2N/A
2N/A total += len;
2N/A if (total >= HEAP_MAX_SIZE)
2N/A return 1;
2N/A
2N/A return 0;
2N/A }
2N/A
2N/A if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET))
2N/A heap_init (HEAP_MAX_ADDR - HEAP_MIN_SIZE, HEAP_MIN_SIZE, 1);
2N/A else
2N/A grub_machine_mmap_iterate (heap_init);
2N/A}
2N/A#endif
2N/A
2N/Astatic void
2N/Agrub_parse_cmdline (void)
2N/A{
2N/A grub_ssize_t actual;
2N/A char args[256];
2N/A
2N/A if (grub_ieee1275_get_property (grub_ieee1275_chosen, "bootargs", &args,
2N/A sizeof args, &actual) == 0
2N/A && actual > 1)
2N/A {
2N/A int i = 0;
2N/A
2N/A while (i < actual)
2N/A {
2N/A char *command = &args[i];
2N/A char *end;
2N/A char *val;
2N/A
2N/A end = grub_strchr (command, ';');
2N/A if (end == 0)
2N/A i = actual; /* No more commands after this one. */
2N/A else
2N/A {
2N/A *end = '\0';
2N/A i += end - command + 1;
2N/A while (grub_isspace(args[i]))
2N/A i++;
2N/A }
2N/A
2N/A /* Process command. */
2N/A val = grub_strchr (command, '=');
2N/A if (val)
2N/A {
2N/A *val = '\0';
2N/A grub_env_set (command, val + 1);
2N/A }
2N/A }
2N/A }
2N/A}
2N/A
2N/Astatic grub_uint64_t ieee1275_get_time_ms (void);
2N/A
2N/Agrub_addr_t grub_modbase;
2N/A
2N/Avoid
2N/Agrub_machine_init (void)
2N/A{
2N/A grub_modbase = ALIGN_UP((grub_addr_t) _end
2N/A + GRUB_KERNEL_MACHINE_MOD_GAP,
2N/A GRUB_KERNEL_MACHINE_MOD_ALIGN);
2N/A grub_ieee1275_init ();
2N/A
2N/A grub_console_init_early ();
2N/A grub_claim_heap ();
2N/A grub_console_init_lately ();
2N/A grub_ofdisk_init ();
2N/A
2N/A grub_parse_cmdline ();
2N/A
2N/A grub_install_get_time_ms (ieee1275_get_time_ms);
2N/A}
2N/A
2N/Avoid
2N/Agrub_machine_fini (void)
2N/A{
2N/A grub_ofdisk_fini ();
2N/A grub_console_fini ();
2N/A}
2N/A
2N/Astatic grub_uint64_t
2N/Aieee1275_get_time_ms (void)
2N/A{
2N/A grub_uint32_t msecs = 0;
2N/A
2N/A grub_ieee1275_milliseconds (&msecs);
2N/A
2N/A return msecs;
2N/A}
2N/A
2N/Agrub_uint32_t
2N/Agrub_get_rtc (void)
2N/A{
2N/A return ieee1275_get_time_ms ();
2N/A}