Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / kern / ieee1275 / init.c
blob7d03a8aac1e5bf7aa843fcfa75be299137990e49
1 /* init.c -- Initialize GRUB on the newworld mac (PPC). */
2 /*
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>
21 #include <grub/dl.h>
22 #include <grub/disk.h>
23 #include <grub/mm.h>
24 #include <grub/partition.h>
25 #include <grub/normal.h>
26 #include <grub/fs.h>
27 #include <grub/setjmp.h>
28 #include <grub/env.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>
34 #include <grub/net.h>
35 #include <grub/offsets.h>
36 #include <grub/memory.h>
37 #ifdef __sparc__
38 #include <grub/machine/kernel.h>
39 #endif
41 /* The minimal heap size we can live with. */
42 #define HEAP_MIN_SIZE (unsigned long) (2 * 1024 * 1024)
44 /* The maximum heap size we're going to claim */
45 #define HEAP_MAX_SIZE (unsigned long) (32 * 1024 * 1024)
47 /* If possible, we will avoid claiming heap above this address, because it
48 seems to cause relocation problems with OSes that link at 4 MiB */
49 #define HEAP_MAX_ADDR (unsigned long) (32 * 1024 * 1024)
51 extern char _start[];
52 extern char _end[];
54 #ifdef __sparc__
55 grub_addr_t grub_ieee1275_original_stack;
56 #endif
58 void
59 grub_exit (void)
61 grub_ieee1275_exit ();
64 /* Translate an OF filesystem path (separated by backslashes), into a GRUB
65 path (separated by forward slashes). */
66 static void
67 grub_translate_ieee1275_path (char *filepath)
69 char *backslash;
71 backslash = grub_strchr (filepath, '\\');
72 while (backslash != 0)
74 *backslash = '/';
75 backslash = grub_strchr (filepath, '\\');
79 void (*grub_ieee1275_net_config) (const char *dev,
80 char **device,
81 char **path);
82 void
83 grub_machine_get_bootlocation (char **device, char **path)
85 char bootpath[64]; /* XXX check length */
86 char *filename;
87 char *type;
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 return;
97 /* Transform an OF device path to a GRUB path. */
99 type = grub_ieee1275_get_device_type (bootpath);
100 if (type && grub_strcmp (type, "network") == 0)
102 char *dev, *canon;
103 char *ptr;
104 dev = grub_ieee1275_get_aliasdevname (bootpath);
105 canon = grub_ieee1275_canonicalise_devname (dev);
106 ptr = canon + grub_strlen (canon) - 1;
107 while (ptr > canon && (*ptr == ',' || *ptr == ':'))
108 ptr--;
109 ptr++;
110 *ptr = 0;
112 if (grub_ieee1275_net_config)
113 grub_ieee1275_net_config (canon, device, path);
114 grub_free (dev);
115 grub_free (canon);
117 else
118 *device = grub_ieee1275_encode_devname (bootpath);
119 grub_free (type);
121 filename = grub_ieee1275_get_filename (bootpath);
122 if (filename)
124 char *lastslash = grub_strrchr (filename, '\\');
126 /* Truncate at last directory. */
127 if (lastslash)
129 *lastslash = '\0';
130 grub_translate_ieee1275_path (filename);
132 *path = filename;
137 /* Claim some available memory in the first /memory node. */
138 #ifdef __sparc__
139 static void
140 grub_claim_heap (void)
142 grub_mm_init_region ((void *) (grub_modules_get_end ()
143 + GRUB_KERNEL_MACHINE_STACK_SIZE), 0x200000);
145 #else
146 static void
147 grub_claim_heap (void)
149 unsigned long total = 0;
151 auto int NESTED_FUNC_ATTR heap_init (grub_uint64_t addr, grub_uint64_t len,
152 grub_memory_type_t type);
153 int NESTED_FUNC_ATTR heap_init (grub_uint64_t addr, grub_uint64_t len,
154 grub_memory_type_t type)
156 if (type != 1)
157 return 0;
159 if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_PRE1_5M_CLAIM))
161 if (addr + len <= 0x180000)
162 return 0;
164 if (addr < 0x180000)
166 len = addr + len - 0x180000;
167 addr = 0x180000;
170 len -= 1; /* Required for some firmware. */
172 /* Never exceed HEAP_MAX_SIZE */
173 if (total + len > HEAP_MAX_SIZE)
174 len = HEAP_MAX_SIZE - total;
176 /* Avoid claiming anything above HEAP_MAX_ADDR, if possible. */
177 if ((addr < HEAP_MAX_ADDR) && /* if it's too late, don't bother */
178 (addr + len > HEAP_MAX_ADDR) && /* if it wasn't available anyway, don't bother */
179 (total + (HEAP_MAX_ADDR - addr) > HEAP_MIN_SIZE)) /* only limit ourselves when we can afford to */
180 len = HEAP_MAX_ADDR - addr;
182 /* In theory, firmware should already prevent this from happening by not
183 listing our own image in /memory/available. The check below is intended
184 as a safeguard in case that doesn't happen. However, it doesn't protect
185 us from corrupting our module area, which extends up to a
186 yet-undetermined region above _end. */
187 if ((addr < (grub_addr_t) _end) && ((addr + len) > (grub_addr_t) _start))
189 grub_printf ("Warning: attempt to claim over our own code!\n");
190 len = 0;
193 if (len)
195 grub_err_t err;
196 /* Claim and use it. */
197 err = grub_claimmap (addr, len);
198 if (err)
199 return err;
200 grub_mm_init_region ((void *) (grub_addr_t) addr, len);
203 total += len;
204 if (total >= HEAP_MAX_SIZE)
205 return 1;
207 return 0;
210 if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET))
211 heap_init (HEAP_MAX_ADDR - HEAP_MIN_SIZE, HEAP_MIN_SIZE, 1);
212 else
213 grub_machine_mmap_iterate (heap_init);
215 #endif
217 static void
218 grub_parse_cmdline (void)
220 grub_ssize_t actual;
221 char args[256];
223 if (grub_ieee1275_get_property (grub_ieee1275_chosen, "bootargs", &args,
224 sizeof args, &actual) == 0
225 && actual > 1)
227 int i = 0;
229 while (i < actual)
231 char *command = &args[i];
232 char *end;
233 char *val;
235 end = grub_strchr (command, ';');
236 if (end == 0)
237 i = actual; /* No more commands after this one. */
238 else
240 *end = '\0';
241 i += end - command + 1;
242 while (grub_isspace(args[i]))
243 i++;
246 /* Process command. */
247 val = grub_strchr (command, '=');
248 if (val)
250 *val = '\0';
251 grub_env_set (command, val + 1);
257 static grub_uint64_t ieee1275_get_time_ms (void);
259 grub_addr_t grub_modbase;
261 void
262 grub_machine_init (void)
264 grub_modbase = ALIGN_UP((grub_addr_t) _end
265 + GRUB_KERNEL_MACHINE_MOD_GAP,
266 GRUB_KERNEL_MACHINE_MOD_ALIGN);
267 grub_ieee1275_init ();
269 grub_console_init_early ();
270 grub_claim_heap ();
271 grub_console_init_lately ();
272 grub_ofdisk_init ();
274 grub_parse_cmdline ();
276 grub_install_get_time_ms (ieee1275_get_time_ms);
279 void
280 grub_machine_fini (void)
282 grub_ofdisk_fini ();
283 grub_console_fini ();
286 static grub_uint64_t
287 ieee1275_get_time_ms (void)
289 grub_uint32_t msecs = 0;
291 grub_ieee1275_milliseconds (&msecs);
293 return msecs;