1
/* init.c -- Initialize GRUB on the newworld mac (PPC). */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2003,2004,2005,2007,2008 Free Software Foundation, Inc.
6
* GRUB is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* GRUB is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20
#include <grub/kernel.h>
22
#include <grub/disk.h>
24
#include <grub/partition.h>
25
#include <grub/normal.h>
27
#include <grub/setjmp.h>
29
#include <grub/misc.h>
30
#include <grub/time.h>
31
#include <grub/machine/console.h>
32
#include <grub/machine/kernel.h>
33
#include <grub/cpu/kernel.h>
34
#include <grub/ieee1275/ofdisk.h>
35
#include <grub/ieee1275/ieee1275.h>
37
/* The minimal heap size we can live with. */
38
#define HEAP_MIN_SIZE (unsigned long) (2 * 1024 * 1024)
40
/* The maximum heap size we're going to claim */
41
#define HEAP_MAX_SIZE (unsigned long) (4 * 1024 * 1024)
43
/* If possible, we will avoid claiming heap above this address, because it
44
seems to cause relocation problems with OSes that link at 4 MiB */
45
#define HEAP_MAX_ADDR (unsigned long) (4 * 1024 * 1024)
53
grub_ieee1275_exit ();
56
/* Translate an OF filesystem path (separated by backslashes), into a GRUB
57
path (separated by forward slashes). */
59
grub_translate_ieee1275_path (char *filepath)
63
backslash = grub_strchr (filepath, '\\');
64
while (backslash != 0)
67
backslash = grub_strchr (filepath, '\\');
72
grub_machine_set_prefix (void)
74
char bootpath[64]; /* XXX check length */
78
if (grub_env_get ("prefix"))
79
/* We already set prefix in grub_machine_init(). */
84
grub_env_set ("prefix", grub_prefix);
85
/* Prefix is hardcoded in the core image. */
89
if (grub_ieee1275_get_property (grub_ieee1275_chosen, "bootpath", &bootpath,
90
sizeof (bootpath), 0))
92
/* Should never happen. */
93
grub_printf ("/chosen/bootpath property missing!\n");
94
grub_env_set ("prefix", "");
98
/* Transform an OF device path to a GRUB path. */
100
prefix = grub_ieee1275_encode_devname (bootpath);
102
filename = grub_ieee1275_get_filename (bootpath);
106
char *lastslash = grub_strrchr (filename, '\\');
108
/* Truncate at last directory. */
112
grub_translate_ieee1275_path (filename);
114
newprefix = grub_malloc (grub_strlen (prefix)
115
+ grub_strlen (filename));
116
grub_sprintf (newprefix, "%s%s", prefix, filename);
122
grub_env_set ("prefix", prefix);
124
grub_free (filename);
128
/* Claim some available memory in the first /memory node. */
129
static void grub_claim_heap (void)
131
unsigned long total = 0;
133
auto int NESTED_FUNC_ATTR heap_init (grub_uint64_t addr, grub_uint64_t len, grub_uint32_t type);
134
int NESTED_FUNC_ATTR heap_init (grub_uint64_t addr, grub_uint64_t len, grub_uint32_t type)
139
len -= 1; /* Required for some firmware. */
141
/* Never exceed HEAP_MAX_SIZE */
142
if (total + len > HEAP_MAX_SIZE)
143
len = HEAP_MAX_SIZE - total;
145
/* Avoid claiming anything above HEAP_MAX_ADDR, if possible. */
146
if ((addr < HEAP_MAX_ADDR) && /* if it's too late, don't bother */
147
(addr + len > HEAP_MAX_ADDR) && /* if it wasn't available anyway, don't bother */
148
(total + (HEAP_MAX_ADDR - addr) > HEAP_MIN_SIZE)) /* only limit ourselves when we can afford to */
149
len = HEAP_MAX_ADDR - addr;
151
/* In theory, firmware should already prevent this from happening by not
152
listing our own image in /memory/available. The check below is intended
153
as a safeguard in case that doesn't happen. However, it doesn't protect
154
us from corrupting our module area, which extends up to a
155
yet-undetermined region above _end. */
156
if ((addr < (grub_addr_t) _end) && ((addr + len) > (grub_addr_t) _start))
158
grub_printf ("Warning: attempt to claim over our own code!\n");
164
/* Claim and use it. */
165
if (grub_claimmap (addr, len) < 0)
166
return grub_error (GRUB_ERR_OUT_OF_MEMORY,
167
"Failed to claim heap at 0x%llx, len 0x%llx\n",
169
grub_mm_init_region ((void *) (grub_addr_t) addr, len);
173
if (total >= HEAP_MAX_SIZE)
179
if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET))
180
heap_init (HEAP_MAX_ADDR - HEAP_MIN_SIZE, HEAP_MIN_SIZE, 1);
182
grub_machine_mmap_iterate (heap_init);
187
grub_uint32_t grub_upper_mem;
189
/* We need to call this before grub_claim_memory. */
191
grub_get_extended_memory (void)
193
auto int NESTED_FUNC_ATTR find_ext_mem (grub_uint64_t addr, grub_uint64_t len, grub_uint32_t type);
194
int NESTED_FUNC_ATTR find_ext_mem (grub_uint64_t addr, grub_uint64_t len, grub_uint32_t type)
196
if (type == 1 && addr == 0x100000)
198
grub_upper_mem = len;
205
grub_machine_mmap_iterate (find_ext_mem);
210
static grub_uint64_t ieee1275_get_time_ms (void);
213
grub_machine_init (void)
218
grub_ieee1275_init ();
220
grub_console_init ();
222
grub_get_extended_memory ();
227
/* Process commandline. */
228
if (grub_ieee1275_get_property (grub_ieee1275_chosen, "bootargs", &args,
229
sizeof args, &actual) == 0
236
char *command = &args[i];
240
end = grub_strchr (command, ';');
242
i = actual; /* No more commands after this one. */
246
i += end - command + 1;
247
while (grub_isspace(args[i]))
251
/* Process command. */
252
val = grub_strchr (command, '=');
256
grub_env_set (command, val + 1);
261
grub_install_get_time_ms (ieee1275_get_time_ms);
265
grub_machine_fini (void)
268
grub_console_fini ();
272
ieee1275_get_time_ms (void)
274
grub_uint32_t msecs = 0;
276
grub_ieee1275_milliseconds (&msecs);
284
return ieee1275_get_time_ms ();
288
grub_arch_modules_addr (void)
290
return ALIGN_UP((grub_addr_t) _end + GRUB_MOD_GAP, GRUB_MOD_ALIGN);