sparc64: Don't use devspec to determine the OBP path
[grub.git] / grub-core / osdep / linux / ofpath.c
1 /* ofpath.c - calculate OpenFirmware path names given an OS device */
2 /*
3  *  GRUB  --  GRand Unified Bootloader
4  *  Copyright (C) 2009, 2011,2012, 2013  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 #undef OFPATH_STANDALONE
21
22 #ifndef OFPATH_STANDALONE
23 #include <grub/types.h>
24 #include <grub/util/misc.h>
25 #include <grub/util/ofpath.h>
26 #include <grub/i18n.h>
27 #endif
28
29 #include <limits.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <errno.h>
39 #include <ctype.h>
40
41 #ifdef OFPATH_STANDALONE
42 #define xmalloc malloc
43 void
44 grub_util_error (const char *fmt, ...)
45 {
46   va_list ap;
47
48   fprintf (stderr, "ofpath: error: ");
49   va_start (ap, fmt);
50   vfprintf (stderr, fmt, ap);
51   va_end (ap);
52   fputc ('\n', stderr);
53   exit (1);
54 }
55
56 void
57 grub_util_info (const char *fmt, ...)
58 {
59   va_list ap;
60
61   fprintf (stderr, "ofpath: info: ");
62   va_start (ap, fmt);
63   vfprintf (stderr, fmt, ap);
64   va_end (ap);
65   fputc ('\n', stderr);
66 }
67
68 #define grub_util_warn grub_util_info
69 #define _(x) x
70 #define xstrdup strdup
71 #endif
72
73 static void
74 kill_trailing_dir(char *path)
75 {
76   char *end = path + strlen(path) - 1;
77
78   while (end >= path)
79     {
80       if (*end != '/')
81         {
82           end--;
83           continue;
84         }
85       *end = '\0';
86       break;
87     }
88 }
89
90 static void
91 trim_newline (char *path)
92 {
93   char *end = path + strlen(path) - 1;
94
95   while (*end == '\n')
96     *end-- = '\0';
97 }
98
99 #define MAX_DISK_CAT    64
100
101 static char *
102 find_obppath (const char *sysfs_path_orig)
103 {
104   char *sysfs_path, *path;
105   size_t path_size = strlen (sysfs_path_orig) + sizeof ("/obppath");
106
107   sysfs_path = xstrdup (sysfs_path_orig);
108   path = xmalloc (path_size);
109
110   while (1)
111     {
112       int fd;
113       char *of_path;
114       struct stat st;
115       size_t size;
116
117       snprintf(path, path_size, "%s/obppath", sysfs_path);
118 #if 0
119       printf("Trying %s\n", path);
120 #endif
121
122       fd = open(path, O_RDONLY);
123
124 #ifndef __sparc__
125       if (fd < 0 || fstat (fd, &st) < 0)
126         {
127           if (fd >= 0)
128             close (fd);
129           snprintf(path, path_size, "%s/devspec", sysfs_path);
130           fd = open(path, O_RDONLY);
131         }
132 #endif
133
134       if (fd < 0 || fstat (fd, &st) < 0)
135         {
136           if (fd >= 0)
137             close (fd);
138           kill_trailing_dir(sysfs_path);
139           if (!strcmp(sysfs_path, "/sys"))
140             {
141               grub_util_info (_("`obppath' not found in parent dirs of `%s',"
142                                 " no IEEE1275 name discovery"),
143                               sysfs_path_orig);
144               free (path);
145               free (sysfs_path);
146               return NULL;
147             }
148           continue;
149         }
150       size = st.st_size;
151       of_path = xmalloc (size + MAX_DISK_CAT + 1);
152       memset(of_path, 0, size + MAX_DISK_CAT + 1);
153       if (read(fd, of_path, size) < 0)
154         {
155           grub_util_info (_("cannot read `%s': %s"), path, strerror (errno));
156           close(fd);
157           free (path);
158           free (of_path);
159           free (sysfs_path);
160           return NULL;
161         }
162       close(fd);
163
164       trim_newline(of_path);
165       free (path);
166       free (sysfs_path);
167       return of_path;
168     }
169 }
170
171 static char *
172 xrealpath (const char *in)
173 {
174   char *out;
175 #ifdef PATH_MAX
176   out = xmalloc (PATH_MAX);
177   out = realpath (in, out);
178 #else
179   out = realpath (in, NULL);
180 #endif
181   if (!out)
182     grub_util_error (_("failed to get canonical path of `%s'"), in);
183   return out;
184 }
185
186 static char *
187 block_device_get_sysfs_path_and_link(const char *devicenode)
188 {
189   char *rpath;
190   char *rpath2;
191   char *ret;
192   size_t tmp_size = strlen (devicenode) + sizeof ("/sys/block/");
193   char *tmp = xmalloc (tmp_size);
194
195   memcpy (tmp, "/sys/block/", sizeof ("/sys/block/"));
196   strcat (tmp, devicenode);
197
198   rpath = xrealpath (tmp);
199   rpath2 = xmalloc (strlen (rpath) + sizeof ("/device"));
200   strcpy (rpath2, rpath);
201   strcat (rpath2, "/device");
202
203   ret = xrealpath (rpath2);
204
205   free (tmp);
206   free (rpath);
207   free (rpath2);
208   return ret;
209 }
210
211 static inline int
212 my_isdigit (int c)
213 {
214   return (c >= '0' && c <= '9');
215 }
216
217 static const char *
218 trailing_digits (const char *p)
219 {
220   const char *end;
221
222   end = p + strlen(p) - 1;
223   while (end >= p)
224     {
225       if (! my_isdigit(*end))
226         break;
227       end--;
228     }
229
230   return end + 1;
231 }
232
233 static char *
234 __of_path_common(char *sysfs_path,
235                  const char *device, int devno)
236 {
237   const char *digit_string;
238   char disk[MAX_DISK_CAT];
239   char *of_path = find_obppath(sysfs_path);
240
241   if (!of_path)
242     return NULL;
243
244   digit_string = trailing_digits (device);
245   if (*digit_string == '\0')
246     {
247       snprintf(disk, sizeof (disk), "/disk@%d", devno);
248     }
249   else
250     {
251       int part;
252
253       sscanf(digit_string, "%d", &part);
254       snprintf(disk, sizeof (disk), "/disk@%d:%c", devno, 'a' + (part - 1));
255     }
256   strcat(of_path, disk);
257   return of_path;
258 }
259
260 static char *
261 get_basename(char *p)
262 {
263   char *ret = p;
264
265   while (*p)
266     {
267       if (*p == '/')
268         ret = p + 1;
269       p++;
270     }
271
272   return ret;
273 }
274
275 static char *
276 of_path_of_vdisk(const char *sys_devname __attribute__((unused)),
277                  const char *device,
278                  const char *devnode __attribute__((unused)),
279                  const char *devicenode)
280 {
281   char *sysfs_path, *p;
282   int devno, junk;
283   char *ret;
284
285   sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
286   p = get_basename (sysfs_path);
287   sscanf(p, "vdc-port-%d-%d", &devno, &junk);
288   ret = __of_path_common (sysfs_path, device, devno);
289
290   free (sysfs_path);
291   return ret;
292 }
293
294 static char *
295 of_path_of_ide(const char *sys_devname __attribute__((unused)), const char *device,
296                const char *devnode __attribute__((unused)),
297                const char *devicenode)
298 {
299   char *sysfs_path, *p;
300   int chan, devno;
301   char *ret;
302
303   sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
304   p = get_basename (sysfs_path);
305   sscanf(p, "%d.%d", &chan, &devno);
306
307   ret = __of_path_common(sysfs_path, device, 2 * chan + devno);
308
309   free (sysfs_path);
310   return ret;
311 }
312
313 static int
314 vendor_is_ATA(const char *path)
315 {
316   int fd, err;
317   char *bufname;
318   char bufcont[3];
319   size_t path_size;
320
321   path_size = strlen (path) + sizeof ("/vendor");
322
323   bufname = xmalloc (path_size);
324
325   snprintf (bufname, path_size, "%s/vendor", path);
326   fd = open (bufname, O_RDONLY);
327   if (fd < 0)
328     grub_util_error (_("cannot open `%s': %s"), bufname, strerror (errno));
329
330   memset(bufcont, 0, sizeof (bufcont));
331   err = read(fd, bufcont, sizeof (bufcont));
332   if (err < 0)
333     grub_util_error (_("cannot open `%s': %s"), bufname, strerror (errno));
334
335   close(fd);
336   free (bufname);
337
338   return (memcmp(bufcont, "ATA", 3) == 0);
339 }
340
341 static void
342 check_sas (const char *sysfs_path, int *tgt, unsigned long int *sas_address)
343 {
344   char *ed = strstr (sysfs_path, "end_device");
345   char *p, *q, *path;
346   char phy[21];
347   int fd;
348   size_t path_size;
349
350   if (!ed)
351     return;
352
353   /* SAS devices are identified using disk@$PHY_ID */
354   p = xstrdup (sysfs_path);
355   ed = strstr(p, "end_device");
356   if (!ed)
357     return;
358
359   q = ed;
360   while (*q && *q != '/')
361     q++;
362   *q = '\0';
363
364   path_size = (strlen (p) + strlen (ed)
365                + sizeof ("%s/sas_device/%s/phy_identifier"));
366   path = xmalloc (path_size);
367   snprintf (path, path_size, "%s/sas_device/%s/phy_identifier", p, ed);
368   fd = open (path, O_RDONLY);
369   if (fd < 0)
370     grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
371
372   memset (phy, 0, sizeof (phy));
373   if (read (fd, phy, sizeof (phy) - 1) < 0)
374     grub_util_error (_("cannot read `%s': %s"), path, strerror (errno));
375
376   close (fd);
377
378   sscanf (phy, "%d", tgt);
379
380   snprintf (path, path_size, "%s/sas_device/%s/sas_address", p, ed);
381   fd = open (path, O_RDONLY);
382   if (fd < 0)
383     grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
384
385   memset (phy, 0, sizeof (phy));
386   if (read (fd, phy, sizeof (phy) - 1) < 0)
387     grub_util_error (_("cannot read `%s': %s"), path, strerror (errno));
388   sscanf (phy, "%lx", sas_address);
389
390   free (path);
391   free (p);
392   close (fd);
393 }
394
395 static char *
396 of_path_of_scsi(const char *sys_devname __attribute__((unused)), const char *device,
397                 const char *devnode __attribute__((unused)),
398                 const char *devicenode)
399 {
400   const char *p, *digit_string, *disk_name;
401   int host, bus, tgt, lun;
402   unsigned long int sas_address;
403   char *sysfs_path, disk[MAX_DISK_CAT - sizeof ("/fp@0,0")];
404   char *of_path;
405
406   sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
407   p = get_basename (sysfs_path);
408   sscanf(p, "%d:%d:%d:%d", &host, &bus, &tgt, &lun);
409   check_sas (sysfs_path, &tgt, &sas_address);
410
411   if (vendor_is_ATA(sysfs_path))
412     {
413       of_path = __of_path_common(sysfs_path, device, tgt);
414       free (sysfs_path);
415       return of_path;
416     }
417
418   of_path = find_obppath(sysfs_path);
419   free (sysfs_path);
420   if (!of_path)
421     return NULL;
422
423   if (strstr (of_path, "qlc"))
424     strcat (of_path, "/fp@0,0");
425
426   if (strstr (of_path, "sbus"))
427     disk_name = "sd";
428   else
429     disk_name = "disk";
430
431   digit_string = trailing_digits (device);
432   if (strncmp (of_path, "/vdevice/", sizeof ("/vdevice/") - 1) == 0)
433     {
434       unsigned long id = 0x8000 | (tgt << 8) | (bus << 5) | lun;
435       if (*digit_string == '\0')
436         {
437           snprintf(disk, sizeof (disk), "/%s@%04lx000000000000", disk_name, id);
438         }
439       else
440         {
441           int part;
442
443           sscanf(digit_string, "%d", &part);
444           snprintf(disk, sizeof (disk),
445                    "/%s@%04lx000000000000:%c", disk_name, id, 'a' + (part - 1));
446         }
447     }
448   else
449     {
450       if (lun == 0)
451         {
452           int sas_id = 0;
453           sas_id = bus << 16 | tgt << 8 | lun;
454
455           if (*digit_string == '\0')
456             {
457               snprintf(disk, sizeof (disk), "/sas/%s@%x", disk_name, sas_id);
458             }
459           else
460             {
461               int part;
462
463               sscanf(digit_string, "%d", &part);
464               snprintf(disk, sizeof (disk),
465                        "/sas/%s@%x:%c", disk_name, sas_id, 'a' + (part - 1));
466             }
467         }
468       else
469         {
470           char *lunstr;
471           int lunpart[4];
472
473           lunstr = xmalloc (20);
474
475           lunpart[0] = (lun >> 8) & 0xff;
476           lunpart[1] = lun & 0xff;
477           lunpart[2] = (lun >> 24) & 0xff;
478           lunpart[3] = (lun >> 16) & 0xff;
479
480           sprintf(lunstr, "%02x%02x%02x%02x00000000", lunpart[0], lunpart[1], lunpart[2], lunpart[3]);
481           long int longlun = atol(lunstr);
482
483           if (*digit_string == '\0')
484             {
485               snprintf(disk, sizeof (disk), "/sas/%s@%lx,%lu", disk_name, sas_address, longlun);
486             }
487           else
488             {
489               int part;
490
491               sscanf(digit_string, "%d", &part);
492               snprintf(disk, sizeof (disk),
493                        "/sas/%s@%lx,%lu:%c", disk_name, sas_address, longlun, 'a' + (part - 1));
494             }
495           free (lunstr);
496         }
497     }
498   strcat(of_path, disk);
499   return of_path;
500 }
501
502 static char *
503 strip_trailing_digits (const char *p)
504 {
505   char *new, *end;
506
507   new = strdup (p);
508   end = new + strlen(new) - 1;
509   while (end >= new)
510     {
511       if (! my_isdigit(*end))
512         break;
513       *end-- = '\0';
514     }
515
516   return new;
517 }
518
519 char *
520 grub_util_devname_to_ofpath (const char *sys_devname)
521 {
522   char *name_buf, *device, *devnode, *devicenode, *ofpath;
523
524   name_buf = xrealpath (sys_devname);
525
526   device = get_basename (name_buf);
527   devnode = strip_trailing_digits (name_buf);
528   devicenode = strip_trailing_digits (device);
529
530   if (device[0] == 'h' && device[1] == 'd')
531     ofpath = of_path_of_ide(name_buf, device, devnode, devicenode);
532   else if (device[0] == 's'
533            && (device[1] == 'd' || device[1] == 'r'))
534     ofpath = of_path_of_scsi(name_buf, device, devnode, devicenode);
535   else if (device[0] == 'v' && device[1] == 'd' && device[2] == 'i'
536            && device[3] == 's' && device[4] == 'k')
537     ofpath = of_path_of_vdisk(name_buf, device, devnode, devicenode);
538   else if (device[0] == 'f' && device[1] == 'd'
539            && device[2] == '0' && device[3] == '\0')
540     /* All the models I've seen have a devalias "floppy".
541        New models have no floppy at all. */
542     ofpath = xstrdup ("floppy");
543   else
544     {
545       grub_util_warn (_("unknown device type %s"), device);
546       ofpath = NULL;
547     }
548
549   free (devnode);
550   free (devicenode);
551   free (name_buf);
552
553   return ofpath;
554 }
555
556 #ifdef OFPATH_STANDALONE
557 int main(int argc, char **argv)
558 {
559   char *of_path;
560
561   if (argc != 2)
562     {
563       printf(_("Usage: %s DEVICE\n"), argv[0]);
564       return 1;
565     }
566
567   of_path = grub_util_devname_to_ofpath (argv[1]);
568   if (of_path)
569     printf("%s\n", of_path);
570   free (of_path);
571
572   return 0;
573 }
574 #endif