ieee1275: split up grub_machine_get_bootlocation
[grub.git] / grub-core / kern / ieee1275 / openfw.c
1 /*  openfw.c -- Open firmware support functions.  */
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/types.h>
21 #include <grub/err.h>
22 #include <grub/misc.h>
23 #include <grub/mm.h>
24 #include <grub/ieee1275/ieee1275.h>
25 #include <grub/net.h>
26
27 enum grub_ieee1275_parse_type
28 {
29   GRUB_PARSE_FILENAME,
30   GRUB_PARSE_PARTITION,
31   GRUB_PARSE_DEVICE,
32   GRUB_PARSE_DEVICE_TYPE
33 };
34
35 static int
36 fill_alias (struct grub_ieee1275_devalias *alias)
37 {
38   grub_ssize_t actual;
39
40   if (grub_ieee1275_get_property (alias->phandle, "device_type", alias->type,
41                                   IEEE1275_MAX_PROP_LEN, &actual))
42     alias->type[0] = 0;
43
44   if (alias->parent_dev == alias->phandle)
45     return 0;
46
47   if (grub_ieee1275_package_to_path (alias->phandle, alias->path,
48                                      IEEE1275_MAX_PATH_LEN, &actual))
49     return 0;
50
51   if (grub_strcmp (alias->parent_path, alias->path) == 0)
52     return 0;
53
54   if (grub_ieee1275_get_property (alias->phandle, "name", alias->name,
55                                   IEEE1275_MAX_PROP_LEN, &actual))
56     return 0;
57   grub_dprintf ("devalias", "device path=%s\n", alias->path);
58   return 1;
59 }
60
61 void
62 grub_ieee1275_devalias_free (struct grub_ieee1275_devalias *alias)
63 {
64   grub_free (alias->name);
65   grub_free (alias->type);
66   grub_free (alias->path);
67   grub_free (alias->parent_path);
68   alias->name = 0;
69   alias->type = 0;
70   alias->path = 0;
71   alias->parent_path = 0;
72   alias->phandle = GRUB_IEEE1275_PHANDLE_INVALID;
73 }
74
75 void
76 grub_ieee1275_children_peer (struct grub_ieee1275_devalias *alias)
77 {
78   while (grub_ieee1275_peer (alias->phandle, &alias->phandle) != -1)
79     if (fill_alias (alias))
80       return;
81   grub_ieee1275_devalias_free (alias);
82 }
83
84 void
85 grub_ieee1275_children_first (const char *devpath,
86                               struct grub_ieee1275_devalias *alias)
87 {
88   grub_ieee1275_phandle_t dev;
89
90   grub_dprintf ("devalias", "iterating children of %s\n",
91                 devpath);
92
93   alias->name = 0;
94   alias->path = 0;
95   alias->parent_path = 0;
96   alias->type = 0;
97
98   if (grub_ieee1275_finddevice (devpath, &dev))
99     return;
100
101   if (grub_ieee1275_child (dev, &alias->phandle))
102     return;
103
104   alias->type = grub_malloc (IEEE1275_MAX_PROP_LEN);
105   if (!alias->type)
106     return;
107   alias->path = grub_malloc (IEEE1275_MAX_PATH_LEN);
108   if (!alias->path)
109     {
110       grub_free (alias->type);
111       return;
112     }
113   alias->parent_path = grub_strdup (devpath);
114   if (!alias->parent_path)
115     {
116       grub_free (alias->path);
117       grub_free (alias->type);
118       return;
119     }
120
121   alias->name = grub_malloc (IEEE1275_MAX_PROP_LEN);
122   if (!alias->name)
123     {
124       grub_free (alias->path);
125       grub_free (alias->type);
126       grub_free (alias->parent_path);
127       return;
128     }
129   if (!fill_alias (alias))
130     grub_ieee1275_children_peer (alias);
131 }
132
133 static int
134 iterate_recursively (const char *path,
135                      int (*hook) (struct grub_ieee1275_devalias *alias))
136 {
137   struct grub_ieee1275_devalias alias;
138   int ret = 0;
139
140   FOR_IEEE1275_DEVCHILDREN(path, alias)
141     {
142       ret = hook (&alias);
143       if (ret)
144         break;
145       ret = iterate_recursively (alias.path, hook);
146       if (ret)
147         break;
148     }
149   grub_ieee1275_devalias_free (&alias);
150   return ret;
151 }
152
153 int
154 grub_ieee1275_devices_iterate (int (*hook) (struct grub_ieee1275_devalias *alias))
155 {
156   return iterate_recursively ("/", hook);
157 }
158
159 void
160 grub_ieee1275_devalias_init_iterator (struct grub_ieee1275_devalias *alias)
161 {
162   alias->name = 0;
163   alias->path = 0;
164   alias->parent_path = 0;
165   alias->type = 0;
166
167   grub_dprintf ("devalias", "iterating aliases\n");
168
169   if (grub_ieee1275_finddevice ("/aliases", &alias->parent_dev))
170     return;
171
172   alias->name = grub_malloc (IEEE1275_MAX_PROP_LEN);
173   if (!alias->name)
174     return;
175
176   alias->type = grub_malloc (IEEE1275_MAX_PROP_LEN);
177   if (!alias->type)
178     {
179       grub_free (alias->name);
180       alias->name = 0;
181       return;
182     }
183
184   alias->name[0] = '\0';
185 }
186
187 int
188 grub_ieee1275_devalias_next (struct grub_ieee1275_devalias *alias)
189 {
190   if (!alias->name)
191     return 0;
192   while (1)
193     {
194       grub_ssize_t pathlen;
195       grub_ssize_t actual;
196       char *tmp;
197
198       if (alias->path)
199         {
200           grub_free (alias->path);
201           alias->path = 0;
202         }
203       tmp = grub_strdup (alias->name);
204       if (grub_ieee1275_next_property (alias->parent_dev, tmp,
205                                        alias->name) <= 0)
206         {
207           grub_free (tmp);
208           grub_ieee1275_devalias_free (alias);
209           return 0;
210         }
211       grub_free (tmp);
212
213       grub_dprintf ("devalias", "devalias name = %s\n", alias->name);
214
215       grub_ieee1275_get_property_length (alias->parent_dev, alias->name, &pathlen);
216
217       /* The property `name' is a special case we should skip.  */
218       if (grub_strcmp (alias->name, "name") == 0)
219         continue;
220
221       /* Sun's OpenBoot often doesn't zero terminate the device alias
222          strings, so we will add a NULL byte at the end explicitly.  */
223       pathlen += 1;
224
225       alias->path = grub_malloc (pathlen + 1);
226       if (! alias->path)
227         {
228           grub_ieee1275_devalias_free (alias);
229           return 0;
230         }
231
232       if (grub_ieee1275_get_property (alias->parent_dev, alias->name, alias->path,
233                                       pathlen, &actual) || actual < 0)
234         {
235           grub_dprintf ("devalias", "get_property (%s) failed\n", alias->name);
236           grub_free (alias->path);
237           continue;
238         }
239       if (actual > pathlen)
240         actual = pathlen;
241       alias->path[actual] = '\0';
242       alias->path[pathlen] = '\0';
243
244       if (grub_ieee1275_finddevice (alias->path, &alias->phandle))
245         {
246           grub_dprintf ("devalias", "finddevice (%s) failed\n", alias->path);
247           grub_free (alias->path);
248           alias->path = 0;
249           continue;
250         }
251
252       if (grub_ieee1275_get_property (alias->phandle, "device_type", alias->type,
253                                       IEEE1275_MAX_PROP_LEN, &actual))
254         {
255           /* NAND device don't have device_type property.  */
256           alias->type[0] = 0;
257         }
258       return 1;
259     }
260 }
261
262 /* Call the "map" method of /chosen/mmu.  */
263 int
264 grub_ieee1275_map (grub_addr_t phys, grub_addr_t virt, grub_size_t size,
265                    grub_uint32_t mode)
266 {
267   struct map_args {
268     struct grub_ieee1275_common_hdr common;
269     grub_ieee1275_cell_t method;
270     grub_ieee1275_cell_t ihandle;
271     grub_ieee1275_cell_t mode;
272     grub_ieee1275_cell_t size;
273     grub_ieee1275_cell_t virt;
274 #ifdef __sparc__
275     grub_ieee1275_cell_t phys_high;
276 #endif
277     grub_ieee1275_cell_t phys_low;
278     grub_ieee1275_cell_t catch_result;
279   } args;
280
281   INIT_IEEE1275_COMMON (&args.common, "call-method",
282 #ifdef __sparc__
283                         7,
284 #else
285                         6,
286 #endif
287                         1);
288   args.method = (grub_ieee1275_cell_t) "map";
289   args.ihandle = grub_ieee1275_mmu;
290 #ifdef __sparc__
291   args.phys_high = 0;
292 #endif
293   args.phys_low = phys;
294   args.virt = virt;
295   args.size = size;
296   args.mode = mode; /* Format is WIMG0PP.  */
297   args.catch_result = (grub_ieee1275_cell_t) -1;
298
299   if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
300     return -1;
301
302   return args.catch_result;
303 }
304
305 grub_err_t
306 grub_claimmap (grub_addr_t addr, grub_size_t size)
307 {
308   if (grub_ieee1275_claim (addr, size, 0, 0))
309     return -1;
310
311   if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_REAL_MODE)
312       && grub_ieee1275_map (addr, addr, size, 0x00))
313     {
314       grub_error (GRUB_ERR_OUT_OF_MEMORY, "map failed: address 0x%llx, size 0x%llx\n",
315                   (long long) addr, (long long) size);
316       grub_ieee1275_release (addr, size);
317       return grub_errno;
318     }
319
320   return GRUB_ERR_NONE;
321 }
322
323 /* Get the device arguments of the Open Firmware node name `path'.  */
324 static char *
325 grub_ieee1275_get_devargs (const char *path)
326 {
327   char *colon = grub_strchr (path, ':');
328
329   if (! colon)
330     return 0;
331
332   return grub_strdup (colon + 1);
333 }
334
335 /* Get the device path of the Open Firmware node name `path'.  */
336 char *
337 grub_ieee1275_get_devname (const char *path)
338 {
339   char *colon = grub_strchr (path, ':');
340   int pathlen = grub_strlen (path);
341   struct grub_ieee1275_devalias curalias;
342   if (colon)
343     pathlen = (int)(colon - path);
344
345   /* Try to find an alias for this device.  */
346   FOR_IEEE1275_DEVALIASES (curalias)
347     /* briQ firmware can change capitalization in /chosen/bootpath.  */
348     if (grub_strncasecmp (curalias.path, path, pathlen) == 0
349         && curalias.path[pathlen] == 0)
350       {
351         char *newpath;
352         newpath = grub_strdup (curalias.name);
353         grub_ieee1275_devalias_free (&curalias);
354         return newpath;
355       }
356
357   return grub_strndup (path, pathlen);
358 }
359
360 static char *
361 grub_ieee1275_parse_args (const char *path, enum grub_ieee1275_parse_type ptype)
362 {
363   char type[64]; /* XXX check size.  */
364   char *device = grub_ieee1275_get_devname (path);
365   char *ret = 0;
366   grub_ieee1275_phandle_t dev;
367
368   /* We need to know what type of device it is in order to parse the full
369      file path properly.  */
370   if (grub_ieee1275_finddevice (device, &dev))
371     {
372       grub_error (GRUB_ERR_UNKNOWN_DEVICE, "device %s not found", device);
373       goto fail;
374     }
375   if (grub_ieee1275_get_property (dev, "device_type", &type, sizeof type, 0))
376     {
377       grub_error (GRUB_ERR_UNKNOWN_DEVICE,
378                   "device %s lacks a device_type property", device);
379       goto fail;
380     }
381
382   switch (ptype)
383     {
384     case GRUB_PARSE_DEVICE:
385       ret = grub_strdup (device);
386       break;
387     case GRUB_PARSE_DEVICE_TYPE:
388       ret = grub_strdup (type);
389       break;
390     case GRUB_PARSE_FILENAME:
391       {
392         char *comma;
393         char *args;
394
395         args = grub_ieee1275_get_devargs (path);
396         if (!args)
397           /* Shouldn't happen.  */
398           return 0;
399
400         /* The syntax of the device arguments is defined in the CHRP and PReP
401            IEEE1275 bindings: "[partition][,[filename]]".  */
402         comma = grub_strchr (args, ',');
403
404         if (comma)
405           {
406             char *filepath = comma + 1;
407             
408             /* Make sure filepath has leading backslash.  */
409             if (filepath[0] != '\\')
410               ret = grub_xasprintf ("\\%s", filepath);
411             else
412               ret = grub_strdup (filepath);
413             }
414         grub_free (args);
415         }
416       break;
417     case GRUB_PARSE_PARTITION:
418       {
419         char *comma;
420         char *args;
421
422         if (grub_strcmp ("block", type) != 0)
423           goto unknown;
424
425         args = grub_ieee1275_get_devargs (path);
426         if (!args)
427           /* Shouldn't happen.  */
428           return 0;
429
430         comma = grub_strchr (args, ',');
431         if (!comma)
432           ret = grub_strdup (args);
433         else
434           ret = grub_strndup (args, (grub_size_t)(comma - args));
435         /* Consistently provide numbered partitions to GRUB.
436            OpenBOOT traditionally uses alphabetical partition
437            specifiers.  */
438         if (ret[0] >= 'a' && ret[0] <= 'z')
439             ret[0] = '1' + (ret[0] - 'a');
440         grub_free (args);
441       }
442       break;
443     default:
444     unknown:
445       grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
446                   "unsupported type %s for device %s", type, device);
447     }
448
449 fail:
450   grub_free (device);
451   return ret;
452 }
453
454 char *
455 grub_ieee1275_get_device_type (const char *path)
456 {
457   return grub_ieee1275_parse_args (path, GRUB_PARSE_DEVICE_TYPE);
458 }
459
460 char *
461 grub_ieee1275_get_aliasdevname (const char *path)
462 {
463   return grub_ieee1275_parse_args (path, GRUB_PARSE_DEVICE);
464 }
465
466 char *
467 grub_ieee1275_get_filename (const char *path)
468 {
469   return grub_ieee1275_parse_args (path, GRUB_PARSE_FILENAME);
470 }
471
472 /* Convert a device name from IEEE1275 syntax to GRUB syntax.  */
473 char *
474 grub_ieee1275_encode_devname (const char *path)
475 {
476   char *device = grub_ieee1275_get_devname (path);
477   char *partition;
478   char *encoding;
479   char *optr;
480   const char *iptr;
481
482   encoding = grub_malloc (sizeof ("ieee1275/") + 2 * grub_strlen (device)
483                           + sizeof (",XXXXXXXXXXXX"));
484   if (!encoding)
485     {
486       grub_free (device);
487       return 0;
488     }
489
490   partition = grub_ieee1275_parse_args (path, GRUB_PARSE_PARTITION);
491
492   optr = grub_stpcpy (encoding, "ieee1275/");
493   for (iptr = device; *iptr; )
494     {
495       if (*iptr == ',')
496         *optr++ ='\\';
497       *optr++ = *iptr++;
498     }
499   if (partition && partition[0])
500     {
501       unsigned int partno = grub_strtoul (partition, 0, 0);
502
503       *optr++ = ',';
504
505       if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_0_BASED_PARTITIONS))
506         /* GRUB partition 1 is OF partition 0.  */
507         partno++;
508
509       grub_snprintf (optr, sizeof ("XXXXXXXXXXXX"), "%d", partno);
510     }
511   else
512     *optr = '\0';
513
514   grub_free (partition);
515   grub_free (device);
516
517   return encoding;
518 }
519
520 /* Resolve aliases.  */
521 char *
522 grub_ieee1275_canonicalise_devname (const char *path)
523 {
524   struct canon_args
525   {
526     struct grub_ieee1275_common_hdr common;
527     grub_ieee1275_cell_t path;
528     grub_ieee1275_cell_t buf;
529     grub_ieee1275_cell_t inlen;
530     grub_ieee1275_cell_t outlen;
531   }
532   args;
533   char *buf = NULL;
534   grub_size_t bufsize = 64;
535   int i;
536
537   for (i = 0; i < 2; i++)
538     {
539       grub_free (buf);
540
541       buf = grub_malloc (bufsize);
542       if (!buf)
543         return NULL;
544
545       INIT_IEEE1275_COMMON (&args.common, "canon", 3, 1);
546       args.path = (grub_ieee1275_cell_t) path;
547       args.buf = (grub_ieee1275_cell_t) buf;
548       args.inlen = (grub_ieee1275_cell_t) (bufsize - 1);
549
550       if (IEEE1275_CALL_ENTRY_FN (&args) == -1)
551         return 0;
552       if (args.outlen > bufsize - 1)
553         {
554           bufsize = args.outlen + 2;
555           continue;
556         }
557       return buf;
558     }
559   /* Shouldn't reach here.  */
560   grub_free (buf);
561   return NULL;
562 }
563
564 char *
565 grub_ieee1275_get_boot_dev (void)
566 {
567   char *bootpath;
568   grub_ssize_t bootpath_size;
569
570   if (grub_ieee1275_get_property_length (grub_ieee1275_chosen, "bootpath",
571                                          &bootpath_size)
572       || bootpath_size <= 0)
573     {
574       /* Should never happen. */
575       grub_printf ("/chosen/bootpath property missing!\n");
576       return NULL;
577     }
578
579   bootpath = (char *) grub_malloc ((grub_size_t) bootpath_size + 64);
580   if (! bootpath)
581     {
582       grub_print_error ();
583       return NULL;
584     }
585   grub_ieee1275_get_property (grub_ieee1275_chosen, "bootpath", bootpath,
586                               (grub_size_t) bootpath_size + 1, 0);
587   bootpath[bootpath_size] = '\0';
588
589   return bootpath;
590 }