]> git.proxmox.com Git - grub2.git/blob - grub-core/kern/ieee1275/init.c
Hack prefix for OLPC
[grub2.git] / grub-core / kern / ieee1275 / init.c
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.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
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 #include <grub/loader.h>
38 #ifdef __i386__
39 #include <grub/cpu/tsc.h>
40 #endif
41 #ifdef __sparc__
42 #include <grub/machine/kernel.h>
43 #endif
44
45 /* The minimal heap size we can live with. */
46 #define HEAP_MIN_SIZE (unsigned long) (2 * 1024 * 1024)
47
48 /* The maximum heap size we're going to claim */
49 #ifdef __i386__
50 #define HEAP_MAX_SIZE (unsigned long) (64 * 1024 * 1024)
51 #else
52 #define HEAP_MAX_SIZE (unsigned long) (32 * 1024 * 1024)
53 #endif
54
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 */
57 #ifdef __i386__
58 #define HEAP_MAX_ADDR (unsigned long) (64 * 1024 * 1024)
59 #else
60 #define HEAP_MAX_ADDR (unsigned long) (32 * 1024 * 1024)
61 #endif
62
63 extern char _start[];
64 extern char _end[];
65
66 #ifdef __sparc__
67 grub_addr_t grub_ieee1275_original_stack;
68 #endif
69
70 void
71 grub_exit (void)
72 {
73 grub_ieee1275_exit ();
74 }
75
76 #ifndef __i386__
77 /* Translate an OF filesystem path (separated by backslashes), into a GRUB
78 path (separated by forward slashes). */
79 static void
80 grub_translate_ieee1275_path (char *filepath)
81 {
82 char *backslash;
83
84 backslash = grub_strchr (filepath, '\\');
85 while (backslash != 0)
86 {
87 *backslash = '/';
88 backslash = grub_strchr (filepath, '\\');
89 }
90 }
91 #endif
92
93 void (*grub_ieee1275_net_config) (const char *dev, char **device, char **path,
94 char *bootpath);
95 #ifdef __i386__
96 void
97 grub_machine_get_bootlocation (char **device __attribute__ ((unused)),
98 char **path __attribute__ ((unused)))
99 {
100 grub_env_set ("prefix", "(sd,1)/");
101 }
102 #else
103 void
104 grub_machine_get_bootlocation (char **device, char **path)
105 {
106 char *bootpath;
107 grub_ssize_t bootpath_size;
108 char *filename;
109 char *type;
110
111 if (grub_ieee1275_get_property_length (grub_ieee1275_chosen, "bootpath",
112 &bootpath_size)
113 || bootpath_size <= 0)
114 {
115 /* Should never happen. */
116 grub_printf ("/chosen/bootpath property missing!\n");
117 return;
118 }
119
120 bootpath = (char *) grub_malloc ((grub_size_t) bootpath_size + 64);
121 if (! bootpath)
122 {
123 grub_print_error ();
124 return;
125 }
126 grub_ieee1275_get_property (grub_ieee1275_chosen, "bootpath", bootpath,
127 (grub_size_t) bootpath_size + 1, 0);
128 bootpath[bootpath_size] = '\0';
129
130 /* Transform an OF device path to a GRUB path. */
131
132 type = grub_ieee1275_get_device_type (bootpath);
133 if (type && grub_strcmp (type, "network") == 0)
134 {
135 char *dev, *canon;
136 char *ptr;
137 dev = grub_ieee1275_get_aliasdevname (bootpath);
138 canon = grub_ieee1275_canonicalise_devname (dev);
139 ptr = canon + grub_strlen (canon) - 1;
140 while (ptr > canon && (*ptr == ',' || *ptr == ':'))
141 ptr--;
142 ptr++;
143 *ptr = 0;
144
145 if (grub_ieee1275_net_config)
146 grub_ieee1275_net_config (canon, device, path, bootpath);
147 grub_free (dev);
148 grub_free (canon);
149 }
150 else
151 *device = grub_ieee1275_encode_devname (bootpath);
152 grub_free (type);
153
154 filename = grub_ieee1275_get_filename (bootpath);
155 if (filename)
156 {
157 char *lastslash = grub_strrchr (filename, '\\');
158
159 /* Truncate at last directory. */
160 if (lastslash)
161 {
162 *lastslash = '\0';
163 grub_translate_ieee1275_path (filename);
164
165 *path = filename;
166 }
167 }
168 grub_free (bootpath);
169 }
170 #endif
171
172 /* Claim some available memory in the first /memory node. */
173 #ifdef __sparc__
174 static void
175 grub_claim_heap (void)
176 {
177 grub_mm_init_region ((void *) (grub_modules_get_end ()
178 + GRUB_KERNEL_MACHINE_STACK_SIZE), 0x200000);
179 }
180 #else
181 /* Helper for grub_claim_heap. */
182 static int
183 heap_init (grub_uint64_t addr, grub_uint64_t len, grub_memory_type_t type,
184 void *data)
185 {
186 unsigned long *total = data;
187
188 if (type != GRUB_MEMORY_AVAILABLE)
189 return 0;
190
191 if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_PRE1_5M_CLAIM))
192 {
193 if (addr + len <= 0x180000)
194 return 0;
195
196 if (addr < 0x180000)
197 {
198 len = addr + len - 0x180000;
199 addr = 0x180000;
200 }
201 }
202 len -= 1; /* Required for some firmware. */
203
204 /* Never exceed HEAP_MAX_SIZE */
205 if (*total + len > HEAP_MAX_SIZE)
206 len = HEAP_MAX_SIZE - *total;
207
208 /* Avoid claiming anything above HEAP_MAX_ADDR, if possible. */
209 if ((addr < HEAP_MAX_ADDR) && /* if it's too late, don't bother */
210 (addr + len > HEAP_MAX_ADDR) && /* if it wasn't available anyway, don't bother */
211 (*total + (HEAP_MAX_ADDR - addr) > HEAP_MIN_SIZE)) /* only limit ourselves when we can afford to */
212 len = HEAP_MAX_ADDR - addr;
213
214 /* In theory, firmware should already prevent this from happening by not
215 listing our own image in /memory/available. The check below is intended
216 as a safeguard in case that doesn't happen. However, it doesn't protect
217 us from corrupting our module area, which extends up to a
218 yet-undetermined region above _end. */
219 if ((addr < (grub_addr_t) _end) && ((addr + len) > (grub_addr_t) _start))
220 {
221 grub_printf ("Warning: attempt to claim over our own code!\n");
222 len = 0;
223 }
224
225 if (len)
226 {
227 grub_err_t err;
228 /* Claim and use it. */
229 err = grub_claimmap (addr, len);
230 if (err)
231 return err;
232 grub_mm_init_region ((void *) (grub_addr_t) addr, len);
233 }
234
235 *total += len;
236 if (*total >= HEAP_MAX_SIZE)
237 return 1;
238
239 return 0;
240 }
241
242 static void
243 grub_claim_heap (void)
244 {
245 unsigned long total = 0;
246
247 if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_FORCE_CLAIM))
248 heap_init (GRUB_IEEE1275_STATIC_HEAP_START, GRUB_IEEE1275_STATIC_HEAP_LEN,
249 1, &total);
250 else
251 grub_machine_mmap_iterate (heap_init, &total);
252 }
253 #endif
254
255 static void
256 grub_parse_cmdline (void)
257 {
258 grub_ssize_t actual;
259 char args[256];
260
261 if (grub_ieee1275_get_property (grub_ieee1275_chosen, "bootargs", &args,
262 sizeof args, &actual) == 0
263 && actual > 1)
264 {
265 int i = 0;
266
267 while (i < actual)
268 {
269 char *command = &args[i];
270 char *end;
271 char *val;
272
273 end = grub_strchr (command, ';');
274 if (end == 0)
275 i = actual; /* No more commands after this one. */
276 else
277 {
278 *end = '\0';
279 i += end - command + 1;
280 while (grub_isspace(args[i]))
281 i++;
282 }
283
284 /* Process command. */
285 val = grub_strchr (command, '=');
286 if (val)
287 {
288 *val = '\0';
289 grub_env_set (command, val + 1);
290 }
291 }
292 }
293 }
294
295 grub_addr_t grub_modbase;
296
297 void
298 grub_machine_init (void)
299 {
300 grub_modbase = ALIGN_UP((grub_addr_t) _end
301 + GRUB_KERNEL_MACHINE_MOD_GAP,
302 GRUB_KERNEL_MACHINE_MOD_ALIGN);
303 grub_ieee1275_init ();
304
305 grub_console_init_early ();
306 grub_claim_heap ();
307 grub_console_init_lately ();
308 grub_ofdisk_init ();
309
310 grub_parse_cmdline ();
311
312 #ifdef __i386__
313 grub_tsc_init ();
314 #else
315 grub_install_get_time_ms (grub_rtc_get_time_ms);
316 #endif
317 }
318
319 void
320 grub_machine_fini (int flags)
321 {
322 if (flags & GRUB_LOADER_FLAG_NORETURN)
323 {
324 grub_ofdisk_fini ();
325 grub_console_fini ();
326 }
327 }
328
329 grub_uint64_t
330 grub_rtc_get_time_ms (void)
331 {
332 grub_uint32_t msecs = 0;
333
334 grub_ieee1275_milliseconds (&msecs);
335
336 return msecs;
337 }