source: grub-pc/trunk/fuentes/.pc/olpc_prefix_hack.patch/grub-core/kern/ieee1275/init.c @ 22

Last change on this file since 22 was 22, checked in by mabarracus, 4 years ago

updated version and apply net.ifnames=0 into debian/rules

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