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
);
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");
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",
166 grub_mm_init_region ((void *) (grub_addr_t
) addr
, len
);
170 if (total
>= HEAP_MAX_SIZE
)
176 if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_INTERPRET
))
177 heap_init (HEAP_MAX_ADDR
- HEAP_MIN_SIZE
, HEAP_MIN_SIZE
);
179 grub_available_iterate (heap_init
);
184 grub_uint32_t grub_upper_mem
;
186 /* We need to call this before grub_claim_memory. */
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
;
202 grub_available_iterate (find_ext_mem
);
207 static grub_uint64_t
ieee1275_get_time_ms (void);
210 grub_machine_init (void)
215 grub_ieee1275_init ();
217 grub_console_init ();
219 grub_get_extended_memory ();
224 /* Process commandline. */
225 if (grub_ieee1275_get_property (grub_ieee1275_chosen
, "bootargs", &args
,
226 sizeof args
, &actual
) == 0
233 char *command
= &args
[i
];
237 end
= grub_strchr (command
, ';');
239 i
= actual
; /* No more commands after this one. */
243 i
+= end
- command
+ 1;
244 while (grub_isspace(args
[i
]))
248 /* Process command. */
249 val
= grub_strchr (command
, '=');
253 grub_env_set (command
, val
+ 1);
258 grub_install_get_time_ms (ieee1275_get_time_ms
);
262 grub_machine_fini (void)
265 grub_console_fini ();
269 ieee1275_get_time_ms (void)
271 grub_uint32_t msecs
= 0;
273 grub_ieee1275_milliseconds (&msecs
);
281 return ieee1275_get_time_ms ();
285 grub_arch_modules_addr (void)
287 return ALIGN_UP((grub_addr_t
) _end
+ GRUB_MOD_GAP
, GRUB_MOD_ALIGN
);