1 /* init.c -- Initialize GRUB on the newworld mac (PPC). */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2004,2005,2007,2008,2009 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/ieee1275/console.h>
32 #include <grub/ieee1275/ofdisk.h>
33 #include <grub/ieee1275/ieee1275.h>
35 #include <grub/offsets.h>
36 #include <grub/memory.h>
37 #include <grub/loader.h>
39 #include <grub/cpu/tsc.h>
42 #include <grub/machine/kernel.h>
45 /* The minimal heap size we can live with. */
46 #define HEAP_MIN_SIZE (unsigned long) (2 * 1024 * 1024)
48 /* The maximum heap size we're going to claim */
50 #define HEAP_MAX_SIZE (unsigned long) (64 * 1024 * 1024)
52 #define HEAP_MAX_SIZE (unsigned long) (32 * 1024 * 1024)
55 /* If possible, we will avoid claiming heap above this address, because it
56 seems to cause relocation problems with OSes that link at 4 MiB */
58 #define HEAP_MAX_ADDR (unsigned long) (64 * 1024 * 1024)
60 #define HEAP_MAX_ADDR (unsigned long) (32 * 1024 * 1024)
67 grub_addr_t grub_ieee1275_original_stack;
73 grub_ieee1275_exit ();
76 /* Translate an OF filesystem path (separated by backslashes), into a GRUB
77 path (separated by forward slashes). */
79 grub_translate_ieee1275_path (char *filepath)
83 backslash = grub_strchr (filepath, '\\');
84 while (backslash != 0)
87 backslash = grub_strchr (filepath, '\\');
91 void (*grub_ieee1275_net_config) (const char *dev, char **device, char **path,
94 grub_machine_get_bootlocation (char **device, char **path)
100 bootpath = grub_ieee1275_get_boot_dev ();
104 /* Transform an OF device path to a GRUB path. */
106 type = grub_ieee1275_get_device_type (bootpath);
107 if (type && grub_strcmp (type, "network") == 0)
111 dev = grub_ieee1275_get_aliasdevname (bootpath);
112 canon = grub_ieee1275_canonicalise_devname (dev);
113 ptr = canon + grub_strlen (canon) - 1;
114 while (ptr > canon && (*ptr == ',' || *ptr == ':'))
119 if (grub_ieee1275_net_config)
120 grub_ieee1275_net_config (canon, device, path, bootpath);
125 *device = grub_ieee1275_encode_devname (bootpath);
128 filename = grub_ieee1275_get_filename (bootpath);
131 char *lastslash = grub_strrchr (filename, '\\');
133 /* Truncate at last directory. */
137 grub_translate_ieee1275_path (filename);
142 grub_free (bootpath);
145 /* Claim some available memory in the first /memory node. */
148 grub_claim_heap (void)
150 grub_mm_init_region ((void *) (grub_modules_get_end ()
151 + GRUB_KERNEL_MACHINE_STACK_SIZE), 0x200000);
154 /* Helper for grub_claim_heap. */
156 heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
159 unsigned long *total = data;
161 if (type != GRUB_MEMORY_AVAILABLE)
164 if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_PRE1_5M_CLAIM))
166 if (addr + len <= 0x180000)
171 len = addr + len - 0x180000;
175 len -= 1; /* Required for some firmware. */
177 /* Never exceed HEAP_MAX_SIZE */
178 if (*total + len > HEAP_MAX_SIZE)
179 len = HEAP_MAX_SIZE - *total;
181 /* Avoid claiming anything above HEAP_MAX_ADDR, if possible. */
182 if ((addr < HEAP_MAX_ADDR) && /* if it's too late, don't bother */
183 (addr + len > HEAP_MAX_ADDR) && /* if it wasn't available anyway, don't bother */
184 (*total + (HEAP_MAX_ADDR - addr) > HEAP_MIN_SIZE)) /* only limit ourselves when we can afford to */
185 len = HEAP_MAX_ADDR - addr;
187 /* In theory, firmware should already prevent this from happening by not
188 listing our own image in /memory/available. The check below is intended
189 as a safeguard in case that doesn't happen. However, it doesn't protect
190 us from corrupting our module area, which extends up to a
191 yet-undetermined region above _end. */
192 if ((addr < (grub_addr_t) _end) && ((addr + len) > (grub_addr_t) _start))
194 grub_printf ("Warning: attempt to claim over our own code!\n");
201 /* Claim and use it. */
202 err = grub_claimmap (addr, len);
205 grub_mm_init_region ((void *) (grub_addr_t) addr, len);
209 if (*total >= HEAP_MAX_SIZE)
216 grub_claim_heap (void)
218 unsigned long total = 0;
220 if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_FORCE_CLAIM))
221 heap_init (GRUB_IEEE1275_STATIC_HEAP_START, GRUB_IEEE1275_STATIC_HEAP_LEN,
224 grub_machine_mmap_iterate (heap_init, &total);
229 grub_parse_cmdline (void)
234 if (grub_ieee1275_get_property (grub_ieee1275_chosen, "bootargs", &args,
235 sizeof args, &actual) == 0
242 char *command = &args[i];
246 end = grub_strchr (command, ';');
248 i = actual; /* No more commands after this one. */
252 i += end - command + 1;
253 while (grub_isspace(args[i]))
257 /* Process command. */
258 val = grub_strchr (command, '=');
262 grub_env_set (command, val + 1);
268 grub_addr_t grub_modbase;
271 grub_machine_init (void)
273 grub_modbase = ALIGN_UP((grub_addr_t) _end
274 + GRUB_KERNEL_MACHINE_MOD_GAP,
275 GRUB_KERNEL_MACHINE_MOD_ALIGN);
276 grub_ieee1275_init ();
278 grub_console_init_early ();
280 grub_console_init_lately ();
283 grub_parse_cmdline ();
288 grub_install_get_time_ms (grub_rtc_get_time_ms);
293 grub_machine_fini (int flags)
295 if (flags & GRUB_LOADER_FLAG_NORETURN)
298 grub_console_fini ();
303 grub_rtc_get_time_ms (void)
305 grub_uint32_t msecs = 0;
307 grub_ieee1275_milliseconds (&msecs);