~ilya-yanok/ubuntu/precise/grub2/fix-for-948716

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2006-01-05 15:20:40 UTC
  • mto: (17.3.1 squeeze) (1.9.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 4.
  • Revision ID: james.westby@ubuntu.com-20060105152040-b72i5pq1a82z22yi
Tags: upstream-1.92
ImportĀ upstreamĀ versionĀ 1.92

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>
 
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 (dev == (grub_ieee1275_phandle_t) -1)
 
43
    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Unknown device");
 
44
 
 
45
  grub_ieee1275_child (dev, &child);
 
46
  if (child == (grub_ieee1275_phandle_t) -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
      int 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
  int actual;
 
94
  struct grub_ieee1275_devalias alias;
 
95
 
 
96
  if (grub_ieee1275_finddevice ("/aliases", &devalias))
 
97
    return -1;
 
98
 
 
99
  /* XXX: Is this the right way to find the first property?  */
 
100
  aliasname[0] = '\0';
 
101
 
 
102
  /* XXX: Are the while conditions correct?  */
 
103
  while (grub_ieee1275_next_property (devalias, aliasname, aliasname, &actual)
 
104
         || actual)
 
105
    {
 
106
      grub_ieee1275_phandle_t dev;
 
107
      grub_size_t pathlen;
 
108
      char *devpath;
 
109
      /* XXX: This should be large enough for any possible case.  */
 
110
      char devtype[64];
 
111
  
 
112
      grub_ieee1275_get_property_length (devalias, aliasname, &pathlen);
 
113
 
 
114
      /* The property `name' is a special case we should skip.  */
 
115
      if (!grub_strcmp (aliasname, "name"))
 
116
          continue;
 
117
      
 
118
      devpath = grub_malloc (pathlen);
 
119
      if (! devpath)
 
120
        return grub_errno;
 
121
 
 
122
      if (grub_ieee1275_get_property (devalias, aliasname, devpath, pathlen,
 
123
                                      &actual))
 
124
        {
 
125
          grub_free (devpath);
 
126
          continue;
 
127
        }
 
128
      
 
129
      if (grub_ieee1275_finddevice (devpath, &dev)
 
130
          || dev == (grub_ieee1275_phandle_t) -1)
 
131
        {
 
132
          grub_free (devpath);
 
133
          continue;
 
134
        }
 
135
 
 
136
      if (grub_ieee1275_get_property (dev, "device_type", devtype, sizeof devtype,
 
137
                                      &actual))
 
138
        {
 
139
          grub_free (devpath);
 
140
          continue;
 
141
        }
 
142
 
 
143
      alias.name = aliasname;
 
144
      alias.path= devpath;
 
145
      alias.type = devtype;
 
146
      hook (&alias);
 
147
      
 
148
      grub_free (devpath);
 
149
    }
 
150
 
 
151
  return 0;
 
152
}
 
153
 
 
154
/* Call the "map" method of /chosen/mmu.  */
 
155
static int
 
156
grub_map (grub_addr_t phys, grub_addr_t virt, grub_uint32_t size,
 
157
                   grub_uint8_t mode)
 
158
{
 
159
  struct map_args {
 
160
    struct grub_ieee1275_common_hdr common;
 
161
    char *method;
 
162
    grub_ieee1275_ihandle_t ihandle;
 
163
    grub_uint32_t mode;
 
164
    grub_uint32_t size;
 
165
    grub_uint32_t virt;
 
166
    grub_uint32_t phys;
 
167
    int catch_result;
 
168
  } args;
 
169
  grub_ieee1275_ihandle_t mmu;
 
170
  int len;
 
171
 
 
172
  grub_ieee1275_get_property (grub_ieee1275_chosen, "mmu", &mmu, sizeof mmu,
 
173
                              &len);
 
174
  if (len != sizeof mmu)
 
175
    return -1;
 
176
 
 
177
  INIT_IEEE1275_COMMON (&args.common, "call-method", 6, 1);
 
178
  args.method = "map";
 
179
  args.ihandle = mmu;
 
180
  args.phys = phys;
 
181
  args.virt = virt;
 
182
  args.size = size;
 
183
  args.mode = mode; /* Format is WIMG0PP.  */
 
184
 
 
185
  if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
 
186
    return -1;
 
187
 
 
188
  return args.catch_result;
 
189
}
 
190
 
 
191
int
 
192
grub_claimmap (grub_addr_t addr, grub_size_t size)
 
193
{
 
194
  if (grub_ieee1275_claim (addr, size, 0, 0))
 
195
    return -1;
 
196
 
 
197
  if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_REAL_MODE)
 
198
      && grub_map (addr, addr, size, 0x00))
 
199
    {
 
200
      grub_printf ("map failed: address 0x%x, size 0x%x\n", addr, size);
 
201
      grub_ieee1275_release (addr, size);
 
202
      return -1;
 
203
    }
 
204
 
 
205
  return 0;
 
206
}
 
207
 
 
208
/* Get the device arguments of the Open Firmware node name `path'.  */
 
209
static char *
 
210
grub_ieee1275_get_devargs (const char *path)
 
211
{
 
212
  char *colon = grub_strchr (path, ':');
 
213
 
 
214
  if (! colon)
 
215
    return 0;
 
216
 
 
217
  return grub_strdup (colon + 1);
 
218
}
 
219
 
 
220
/* Get the device path of the Open Firmware node name `path'.  */
 
221
static char *
 
222
grub_ieee1275_get_devname (const char *path)
 
223
{
 
224
  char *colon = grub_strchr (path, ':');
 
225
  char *newpath = 0;
 
226
  int pathlen = grub_strlen (path);
 
227
  auto int match_alias (struct grub_ieee1275_devalias *alias);
 
228
 
 
229
  int match_alias (struct grub_ieee1275_devalias *curalias)
 
230
    {
 
231
      /* briQ firmware can change capitalization in /chosen/bootpath.  */
 
232
      if (! grub_strncasecmp (curalias->path, path, pathlen))
 
233
        {
 
234
          newpath = grub_strdup (curalias->name);
 
235
          return 1;
 
236
        }
 
237
 
 
238
      return 0;
 
239
    }
 
240
 
 
241
  if (colon)
 
242
    pathlen = (int)(colon - path);
 
243
 
 
244
  /* Try to find an alias for this device.  */
 
245
  grub_devalias_iterate (match_alias);
 
246
 
 
247
  if (! newpath)
 
248
    newpath = grub_strndup (path, pathlen);
 
249
 
 
250
  return newpath;
 
251
}
 
252
 
 
253
static char *
 
254
grub_ieee1275_parse_args (const char *path, enum grub_ieee1275_parse_type ptype)
 
255
{
 
256
  char type[64]; /* XXX check size.  */
 
257
  char *device = grub_ieee1275_get_devname (path);
 
258
  char *args = grub_ieee1275_get_devargs (path);
 
259
  char *ret = 0;
 
260
  grub_ieee1275_phandle_t dev;
 
261
 
 
262
  if (!args)
 
263
    /* Shouldn't happen.  */
 
264
    return 0;
 
265
 
 
266
  /* We need to know what type of device it is in order to parse the full
 
267
     file path properly.  */
 
268
  if (grub_ieee1275_finddevice (device, &dev))
 
269
    {
 
270
      grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Device %s not found\n", device);
 
271
      goto fail;
 
272
    }
 
273
  if (grub_ieee1275_get_property (dev, "device_type", &type, sizeof type, 0))
 
274
    {
 
275
      grub_error (GRUB_ERR_UNKNOWN_DEVICE,
 
276
                  "Device %s lacks a device_type property\n", device);
 
277
      goto fail;
 
278
    }
 
279
 
 
280
  if (!grub_strcmp ("block", type))
 
281
    {
 
282
      /* The syntax of the device arguments is defined in the CHRP and PReP
 
283
         IEEE1275 bindings: "[partition][,[filename]]".  */
 
284
      char *comma = grub_strchr (args, ',');
 
285
 
 
286
      if (ptype == GRUB_PARSE_FILENAME)
 
287
        {
 
288
          if (comma)
 
289
            {
 
290
              char *filepath = comma + 1;
 
291
 
 
292
              ret = grub_malloc (grub_strlen (filepath) + 1);
 
293
              /* Make sure filepath has leading backslash.  */
 
294
              if (filepath[0] != '\\')
 
295
                grub_sprintf (ret, "\\%s", filepath);
 
296
              else
 
297
                grub_strcpy (ret, filepath);
 
298
            }
 
299
        }
 
300
      else if (ptype == GRUB_PARSE_PARTITION)
 
301
        {
 
302
          if (!comma)
 
303
            ret = grub_strdup (args);
 
304
          else
 
305
            ret = grub_strndup (args, (grub_size_t)(comma - args));
 
306
        }
 
307
    }
 
308
  else
 
309
    {
 
310
      /* XXX Handle net devices by configuring & registering a grub_net_dev
 
311
         here, then return its name?
 
312
         Example path: "net:<server ip>,<file name>,<client ip>,<gateway
 
313
         ip>,<bootp retries>,<tftp retries>".  */
 
314
      grub_printf ("Unsupported type %s for device %s\n", type, device);
 
315
    }
 
316
 
 
317
fail:
 
318
  grub_free (device);
 
319
  grub_free (args);
 
320
  return ret;
 
321
}
 
322
 
 
323
char *
 
324
grub_ieee1275_get_filename (const char *path)
 
325
{
 
326
  return grub_ieee1275_parse_args (path, GRUB_PARSE_FILENAME);
 
327
}
 
328
 
 
329
/* Convert a device name from IEEE1275 syntax to GRUB syntax.  */
 
330
char *
 
331
grub_ieee1275_encode_devname (const char *path)
 
332
{
 
333
  char *device = grub_ieee1275_get_devname (path);
 
334
  char *partition = grub_ieee1275_parse_args (path, GRUB_PARSE_PARTITION);
 
335
  char *encoding;
 
336
 
 
337
  if (partition)
 
338
    {
 
339
      unsigned int partno = grub_strtoul (partition, 0, 0);
 
340
 
 
341
      /* GRUB partition numbering is 0-based.  */
 
342
      if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_0_BASED_PARTITIONS))
 
343
        partno--;
 
344
 
 
345
      /* Assume partno will require less than five bytes to encode.  */
 
346
      encoding = grub_malloc (grub_strlen (device) + 3 + 5);
 
347
      grub_sprintf (encoding, "(%s,%d)", device, partno);
 
348
    }
 
349
  else
 
350
    {
 
351
      encoding = grub_malloc (grub_strlen (device) + 2);
 
352
      grub_sprintf (encoding, "(%s)", device);
 
353
    }
 
354
 
 
355
  grub_free (partition);
 
356
  grub_free (device);
 
357
 
 
358
  return encoding;
 
359
}
 
360
 
 
361
void
 
362
grub_reboot (void)
 
363
{
 
364
  grub_ieee1275_interpret ("reset-all", 0);
 
365
}
 
366
 
 
367
void
 
368
grub_halt (void)
 
369
{
 
370
  grub_ieee1275_interpret ("shut-down", 0);
 
371
}