~ubuntu-branches/debian/sid/grub2/sid-200907171837

« back to all changes in this revision

Viewing changes to kern/sparc64/ieee1275/openfw.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2006-01-05 15:20:40 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060105152040-1ab076d4n3y2o5yf
Tags: 1.92-1
* New upstream release.
  - Add support for GPT partition table format.
  - Add a new command "play" to play an audio file on PC.
  - Add support for Linux/ADFS partition table format.
  - Add support for BASH-like scripting.
  - Add support for Apple HFS+ filesystems.
* 01_fix_grub-install.patch: Added. Fix grub-install to use
  /bin/grub-mkimage instead of /sbin/grub-mkimage. Closes: #338824
* Do not use CDBS tarball mode anymore. Closes: #344272  

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  openfw.c -- Open firmware support funtions.  */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
 
5
 *
 
6
 *  This program 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 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program 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 this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 */
 
20
 
 
21
#include <grub/err.h>
 
22
#include <grub/misc.h>
 
23
#include <grub/mm.h>
 
24
#include <grub/machine/kernel.h> /* Needed ?  */
 
25
#include <grub/ieee1275/ieee1275.h>
 
26
 
 
27
enum grub_ieee1275_parse_type
 
28
{
 
29
  GRUB_PARSE_FILENAME,
 
30
  GRUB_PARSE_PARTITION,
 
31
};
 
32
 
 
33
/* Walk children of 'devpath', calling hook for each.  */
 
34
grub_err_t
 
35
grub_children_iterate (char *devpath,
 
36
                  int (*hook) (struct grub_ieee1275_devalias *alias))
 
37
{
 
38
  grub_ieee1275_phandle_t dev;
 
39
  grub_ieee1275_phandle_t child;
 
40
 
 
41
  grub_ieee1275_finddevice (devpath, &dev);
 
42
  if (((signed) dev) == -1)
 
43
    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Unknown device");
 
44
 
 
45
  grub_ieee1275_child (dev, &child);
 
46
  if (((signed) child) == -1)
 
47
    return grub_error (GRUB_ERR_BAD_DEVICE, "Device has no children");
 
48
 
 
49
  do
 
50
    {
 
51
      /* XXX: Don't use hardcoded path lengths.  */
 
52
      char childtype[64];
 
53
      char childpath[64];
 
54
      char childname[64];
 
55
      char fullname[64];
 
56
      struct grub_ieee1275_devalias alias;
 
57
      grub_ssize_t actual;
 
58
 
 
59
      grub_ieee1275_get_property (child, "device_type", childtype,
 
60
                                  sizeof childtype, &actual);
 
61
      if (actual == -1)
 
62
        continue;
 
63
 
 
64
      grub_ieee1275_package_to_path (child, childpath, sizeof childpath,
 
65
                                     &actual);
 
66
      if (actual == -1)
 
67
        continue;
 
68
 
 
69
      grub_ieee1275_get_property (child, "name", childname,
 
70
                                  sizeof childname, &actual);
 
71
      if (actual == -1)
 
72
        continue;
 
73
 
 
74
      grub_sprintf (fullname, "%s/%s", devpath, childname);
 
75
 
 
76
      alias.type = childtype;
 
77
      alias.path = childpath;
 
78
      alias.name = fullname;
 
79
      hook (&alias);
 
80
    }
 
81
  while (grub_ieee1275_peer (child, &child));
 
82
 
 
83
  return 0;
 
84
}
 
85
 
 
86
/* Iterate through all device aliases.  This function can be used to
 
87
   find a device of a specific type.  */
 
88
grub_err_t
 
89
grub_devalias_iterate (int (*hook) (struct grub_ieee1275_devalias *alias))
 
90
{
 
91
  grub_ieee1275_phandle_t devalias;
 
92
  char aliasname[32];
 
93
  grub_ssize_t actual;
 
94
  grub_ieee1275_cell_t flags;
 
95
  struct grub_ieee1275_devalias alias;
 
96
 
 
97
  if (grub_ieee1275_finddevice ("/aliases", &devalias))
 
98
    return -1;
 
99
 
 
100
  aliasname[0] = '\0';
 
101
 
 
102
  while (grub_ieee1275_next_property (devalias, aliasname, aliasname, &flags) != -1
 
103
         && ((signed) flags) != -1 )
 
104
    {
 
105
      grub_ieee1275_phandle_t dev;
 
106
      grub_ssize_t pathlen, typelen;
 
107
      char *devpath, *devtype;
 
108
 
 
109
      grub_dprintf ("devalias", "devalias name = %s\n", aliasname);
 
110
      
 
111
      /* The property `name' is a special case we should skip.  */
 
112
      if (!grub_strcmp (aliasname, "name"))
 
113
          continue;
 
114
      
 
115
      grub_ieee1275_get_property_length (devalias, aliasname, &pathlen);
 
116
      devpath = grub_malloc (pathlen);
 
117
      if (! devpath)
 
118
        return grub_errno;
 
119
 
 
120
      if (grub_ieee1275_get_property (devalias, aliasname, devpath, pathlen,
 
121
                                      &actual))
 
122
        {
 
123
          grub_dprintf ("devalias", "get_property (%s) failed\n", aliasname);
 
124
          grub_free (devpath);
 
125
          continue;
 
126
        }
 
127
      
 
128
      if (grub_ieee1275_finddevice (devpath, &dev) || ((signed) dev) == -1)
 
129
        {
 
130
          grub_dprintf ("devalias", "finddevice (%s) failed\n", devpath);
 
131
          grub_free (devpath);
 
132
          continue;
 
133
        }
 
134
 
 
135
      grub_ieee1275_get_property_length (dev, "device_type", &typelen);
 
136
      devtype = grub_malloc (typelen);
 
137
      if (! devtype)
 
138
      {
 
139
        grub_free (devpath);
 
140
        return grub_errno;
 
141
      }
 
142
      if (grub_ieee1275_get_property (dev, "device_type", devtype, typelen, &actual))
 
143
        {
 
144
          grub_dprintf ("devalias", "get device type failed\n");
 
145
          grub_free (devtype);
 
146
          grub_free (devpath);
 
147
          continue;
 
148
        }
 
149
 
 
150
      alias.name = aliasname;
 
151
      alias.path= devpath;
 
152
      alias.type = devtype;
 
153
      if((*hook) (&alias))
 
154
        {
 
155
          grub_free (devtype);
 
156
          grub_free (devpath);
 
157
          break;
 
158
        }
 
159
 
 
160
      grub_free (devtype);
 
161
      grub_free (devpath);
 
162
    }
 
163
 
 
164
  return 0;
 
165
}
 
166
 
 
167
/* FIXME (sparc64) */
 
168
#if 0
 
169
/* Call the "map" method of /chosen/mmu.  */
 
170
static int
 
171
grub_map (grub_addr_t phys, grub_addr_t virt, grub_uint32_t size,
 
172
                   grub_uint8_t mode)
 
173
{
 
174
  struct map_args {
 
175
    struct grub_ieee1275_common_hdr common;
 
176
    char *method;
 
177
    grub_ieee1275_ihandle_t ihandle;
 
178
    grub_uint32_t mode;
 
179
    grub_uint32_t size;
 
180
    grub_uint32_t virt;
 
181
    grub_uint32_t phys;
 
182
    int catch_result;
 
183
  } args;
 
184
  grub_ieee1275_ihandle_t mmu;
 
185
  grub_ssize_t len;
 
186
 
 
187
  grub_ieee1275_get_property (grub_ieee1275_chosen, "mmu", &mmu, sizeof mmu,
 
188
                              &len);
 
189
  if (len != sizeof mmu)
 
190
    return -1;
 
191
 
 
192
  INIT_IEEE1275_COMMON (&args.common, "call-method", 6, 1);
 
193
  args.method = "map";
 
194
  args.ihandle = mmu;
 
195
  args.phys = phys;
 
196
  args.virt = virt;
 
197
  args.size = size;
 
198
  args.mode = mode; /* Format is WIMG0PP.  */
 
199
 
 
200
  if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
 
201
    return -1;
 
202
 
 
203
  return args.catch_result;
 
204
}
 
205
#endif
 
206
 
 
207
int
 
208
grub_claimmap (grub_addr_t addr, grub_size_t size)
 
209
{
 
210
  if (grub_ieee1275_claim (addr, size, 0, 0))
 
211
    return -1;
 
212
  return 0;
 
213
}
 
214
 
 
215
/* Get the device arguments of the Open Firmware node name `path'.  */
 
216
static char *
 
217
grub_ieee1275_get_devargs (const char *path)
 
218
{
 
219
  char *colon = grub_strchr (path, ':');
 
220
 
 
221
  if (! colon)
 
222
    return 0;
 
223
 
 
224
  return grub_strdup (colon + 1);
 
225
}
 
226
 
 
227
/* Get the device path of the Open Firmware node name `path'.  */
 
228
static char *
 
229
grub_ieee1275_get_devname (const char *path)
 
230
{
 
231
  char *colon = grub_strchr (path, ':');
 
232
  char *newpath = 0;
 
233
  int pathlen = grub_strlen (path);
 
234
  auto int match_alias (struct grub_ieee1275_devalias *alias);
 
235
 
 
236
  int match_alias (struct grub_ieee1275_devalias *curalias)
 
237
    {
 
238
      /* briQ firmware can change capitalization in /chosen/bootpath.  */
 
239
      if (! grub_strncasecmp (curalias->path, path, pathlen))
 
240
        {
 
241
          newpath = grub_strndup (curalias->name, grub_strlen (curalias->name));
 
242
          return 1;
 
243
        }
 
244
 
 
245
      return 0;
 
246
    }
 
247
 
 
248
  if (colon)
 
249
    pathlen = (int)(colon - path);
 
250
 
 
251
  /* Try to find an alias for this device.  */
 
252
  grub_devalias_iterate (match_alias);
 
253
 
 
254
  if (! newpath)
 
255
    newpath = grub_strdup (path);
 
256
 
 
257
  return newpath;
 
258
}
 
259
 
 
260
static char *
 
261
grub_ieee1275_parse_args (const char *path, enum grub_ieee1275_parse_type ptype)
 
262
{
 
263
  char type[64]; /* XXX check size.  */
 
264
  char *device = grub_ieee1275_get_devname (path);
 
265
  char *args = grub_ieee1275_get_devargs (path);
 
266
  char *ret = 0;
 
267
  grub_ieee1275_phandle_t dev;
 
268
 
 
269
  if (!args)
 
270
    /* Shouldn't happen.  */
 
271
    return 0;
 
272
 
 
273
  /* We need to know what type of device it is in order to parse the full
 
274
     file path properly.  */
 
275
  if (grub_ieee1275_finddevice (device, &dev))
 
276
    {
 
277
      grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Device %s not found\n", device);
 
278
      goto fail;
 
279
    }
 
280
  if (grub_ieee1275_get_property (dev, "device_type", type, sizeof type, 0))
 
281
    {
 
282
      grub_error (GRUB_ERR_UNKNOWN_DEVICE,
 
283
                  "Device %s lacks a device_type property\n", device);
 
284
      goto fail;
 
285
    }
 
286
 
 
287
  if (!grub_strcmp ("block", type))
 
288
    {
 
289
      /* The syntax of the device arguments is defined in the CHRP and PReP
 
290
         IEEE1275 bindings: "[partition][,[filename]]".  */
 
291
      char *comma = grub_strchr (args, ',');
 
292
 
 
293
      if (ptype == GRUB_PARSE_FILENAME)
 
294
        {
 
295
          if (comma)
 
296
            {
 
297
              char *filepath = comma + 1;
 
298
 
 
299
              ret = grub_malloc (grub_strlen (filepath) + 1);
 
300
              /* Make sure filepath has leading backslash.  */
 
301
              if (filepath[0] != '\\')
 
302
                grub_sprintf (ret, "\\%s", filepath);
 
303
              else
 
304
                grub_strcpy (ret, filepath);
 
305
            }
 
306
        }
 
307
      else if (ptype == GRUB_PARSE_PARTITION)
 
308
        {
 
309
          if (!comma)
 
310
            ret = grub_strdup (args);
 
311
          else
 
312
            ret = grub_strndup (args, (grub_size_t)(comma - args));
 
313
        }
 
314
    }
 
315
  else
 
316
    {
 
317
      /* XXX Handle net devices by configuring & registering a grub_net_dev
 
318
         here, then return its name?
 
319
         Example path: "net:<server ip>,<file name>,<client ip>,<gateway
 
320
         ip>,<bootp retries>,<tftp retries>".  */
 
321
      grub_printf ("Unsupported type %s for device %s\n", type, device);
 
322
    }
 
323
 
 
324
fail:
 
325
  grub_free (device);
 
326
  grub_free (args);
 
327
  return ret;
 
328
}
 
329
 
 
330
char *
 
331
grub_ieee1275_get_filename (const char *path)
 
332
{
 
333
  return grub_ieee1275_parse_args (path, GRUB_PARSE_FILENAME);
 
334
}
 
335
 
 
336
/* Convert a device name from IEEE1275 syntax to GRUB syntax.  */
 
337
char *
 
338
grub_ieee1275_encode_devname (const char *path)
 
339
{
 
340
  char *device = grub_ieee1275_get_devname (path);
 
341
  char *partition = grub_ieee1275_parse_args (path, GRUB_PARSE_PARTITION);
 
342
  char *encoding;
 
343
 
 
344
  if (partition)
 
345
    {
 
346
      unsigned int partno = grub_strtoul (partition, 0, 0);
 
347
 
 
348
      /* Assume partno will require less than five bytes to encode.  */
 
349
      encoding = grub_malloc (grub_strlen (device) + 3 + 5);
 
350
      grub_sprintf (encoding, "(%s,%d)", device, partno);
 
351
    }
 
352
  else
 
353
    {
 
354
      encoding = grub_malloc (grub_strlen (device) + 2);
 
355
      grub_sprintf (encoding, "(%s)", device);
 
356
    }
 
357
 
 
358
  grub_free (partition);
 
359
  grub_free (device);
 
360
 
 
361
  return encoding;
 
362
}
 
363
 
 
364
void
 
365
grub_reboot (void)
 
366
{
 
367
  grub_ieee1275_interpret ("reset-all", 0);
 
368
}
 
369
 
 
370
void
 
371
grub_halt (void)
 
372
{
 
373
  grub_ieee1275_interpret ("power-off", 0);
 
374
}