Updated PCI IDs to latest snapshot.
[tangerine.git] / arch / common / boot / grub2 / kern / ieee1275 / init.c
blob71b7ba9c665fd4225e645f59835a5e9442c3b441
1 /* init.c -- Initialize GRUB on the newworld mac (PPC). */
2 /*
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>
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/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)
47 extern char _start[];
48 extern char _end[];
50 void
51 grub_exit (void)
53 grub_ieee1275_exit ();
56 /* Translate an OF filesystem path (separated by backslashes), into a GRUB
57 path (separated by forward slashes). */
58 static void
59 grub_translate_ieee1275_path (char *filepath)
61 char *backslash;
63 backslash = grub_strchr (filepath, '\\');
64 while (backslash != 0)
66 *backslash = '/';
67 backslash = grub_strchr (filepath, '\\');
71 void
72 grub_machine_set_prefix (void)
74 char bootpath[64]; /* XXX check length */
75 char *filename;
76 char *prefix;
78 if (grub_env_get ("prefix"))
79 /* We already set prefix in grub_machine_init(). */
80 return;
82 if (grub_prefix[0])
84 grub_env_set ("prefix", grub_prefix);
85 /* Prefix is hardcoded in the core image. */
86 return;
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", "");
95 return;
98 /* Transform an OF device path to a GRUB path. */
100 prefix = grub_ieee1275_encode_devname (bootpath);
102 filename = grub_ieee1275_get_filename (bootpath);
103 if (filename)
105 char *newprefix;
106 char *lastslash = grub_strrchr (filename, '\\');
108 /* Truncate at last directory. */
109 if (lastslash)
111 *lastslash = '\0';
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);
117 grub_free (prefix);
118 prefix = newprefix;
122 grub_env_set ("prefix", prefix);
124 grub_free (filename);
125 grub_free (prefix);
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);
134 int NESTED_FUNC_ATTR heap_init (grub_uint64_t addr, grub_uint64_t len)
136 len -= 1; /* Required for some firmware. */
138 /* Never exceed HEAP_MAX_SIZE */
139 if (total + len > HEAP_MAX_SIZE)
140 len = HEAP_MAX_SIZE - total;
142 /* Avoid claiming anything above HEAP_MAX_ADDR, if possible. */
143 if ((addr < HEAP_MAX_ADDR) && /* if it's too late, don't bother */
144 (addr + len > HEAP_MAX_ADDR) && /* if it wasn't available anyway, don't bother */
145 (total + (HEAP_MAX_ADDR - addr) > HEAP_MIN_SIZE)) /* only limit ourselves when we can afford to */
146 len = HEAP_MAX_ADDR - addr;
148 /* In theory, firmware should already prevent this from happening by not
149 listing our own image in /memory/available. The check below is intended
150 as a safegard in case that doesn't happen. It does, however, not protect
151 us from corrupting our module area, which extends up to a
152 yet-undetermined region above _end. */
153 if ((addr < (grub_addr_t) _end) && ((addr + len) > (grub_addr_t) _start))
155 grub_printf ("Warning: attempt to claim over our own code!\n");
156 len = 0;
159 if (len)
161 /* Claim and use it. */
162 if (grub_claimmap (addr, len) < 0)
163 return grub_error (GRUB_ERR_OUT_OF_MEMORY,
164 "Failed to claim heap at 0x%llx, len 0x%llx\n",
165 addr, len);
166 grub_mm_init_region ((void *) (grub_addr_t) addr, len);
169 total += len;
170 if (total >= HEAP_MAX_SIZE)
171 return 1;
173 return 0;
176 if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET))
177 heap_init (HEAP_MAX_ADDR - HEAP_MIN_SIZE, HEAP_MIN_SIZE);
178 else
179 grub_available_iterate (heap_init);
182 #ifdef __i386__
184 grub_uint32_t grub_upper_mem;
186 /* We need to call this before grub_claim_memory. */
187 static void
188 grub_get_extended_memory (void)
190 auto int NESTED_FUNC_ATTR find_ext_mem (grub_uint64_t addr, grub_uint64_t len);
191 int NESTED_FUNC_ATTR find_ext_mem (grub_uint64_t addr, grub_uint64_t len)
193 if (addr == 0x100000)
195 grub_upper_mem = len;
196 return 1;
199 return 0;
202 grub_available_iterate (find_ext_mem);
205 #endif
207 static grub_uint64_t ieee1275_get_time_ms (void);
209 void
210 grub_machine_init (void)
212 char args[256];
213 int actual;
215 grub_ieee1275_init ();
217 grub_console_init ();
218 #ifdef __i386__
219 grub_get_extended_memory ();
220 #endif
221 grub_claim_heap ();
222 grub_ofdisk_init ();
224 /* Process commandline. */
225 if (grub_ieee1275_get_property (grub_ieee1275_chosen, "bootargs", &args,
226 sizeof args, &actual) == 0
227 && actual > 1)
229 int i = 0;
231 while (i < actual)
233 char *command = &args[i];
234 char *end;
235 char *val;
237 end = grub_strchr (command, ';');
238 if (end == 0)
239 i = actual; /* No more commands after this one. */
240 else
242 *end = '\0';
243 i += end - command + 1;
244 while (grub_isspace(args[i]))
245 i++;
248 /* Process command. */
249 val = grub_strchr (command, '=');
250 if (val)
252 *val = '\0';
253 grub_env_set (command, val + 1);
258 grub_install_get_time_ms (ieee1275_get_time_ms);
261 void
262 grub_machine_fini (void)
264 grub_ofdisk_fini ();
265 grub_console_fini ();
268 static grub_uint64_t
269 ieee1275_get_time_ms (void)
271 grub_uint32_t msecs = 0;
273 grub_ieee1275_milliseconds (&msecs);
275 return msecs;
278 grub_uint32_t
279 grub_get_rtc (void)
281 return ieee1275_get_time_ms ();
284 grub_addr_t
285 grub_arch_modules_addr (void)
287 return ALIGN_UP((grub_addr_t) _end + GRUB_MOD_GAP, GRUB_MOD_ALIGN);