sparc64: Limit nvme of_path_of_nvme to just SPARC
[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 __sparc__
42 typedef enum
43   {
44     GRUB_OFPATH_SPARC_WWN_ADDR = 1,
45     GRUB_OFPATH_SPARC_TGT_LUN,
46   } ofpath_sparc_addressing;
47
48 struct ofpath_sparc_hba
49 {
50   grub_uint32_t device_id;
51   ofpath_sparc_addressing addressing;
52 };
53
54 static struct ofpath_sparc_hba sparc_lsi_hba[] = {
55   /* Rhea, Jasper 320, LSI53C1020/1030. */
56   {0x30, GRUB_OFPATH_SPARC_TGT_LUN},
57   /* SAS-1068E. */
58   {0x50, GRUB_OFPATH_SPARC_TGT_LUN},
59   /* SAS-1064E. */
60   {0x56, GRUB_OFPATH_SPARC_TGT_LUN},
61   /* Pandora SAS-1068E. */
62   {0x58, GRUB_OFPATH_SPARC_TGT_LUN},
63   /* Aspen, Invader, LSI SAS-3108. */
64   {0x5d, GRUB_OFPATH_SPARC_TGT_LUN},
65   /* Niwot, SAS 2108. */
66   {0x79, GRUB_OFPATH_SPARC_TGT_LUN},
67   /* Erie, Falcon, LSI SAS 2008. */
68   {0x72, GRUB_OFPATH_SPARC_WWN_ADDR},
69   /* LSI WarpDrive 6203. */
70   {0x7e, GRUB_OFPATH_SPARC_WWN_ADDR},
71   /* LSI SAS 2308. */
72   {0x87, GRUB_OFPATH_SPARC_WWN_ADDR},
73   /* LSI SAS 3008. */
74   {0x97, GRUB_OFPATH_SPARC_WWN_ADDR},
75   {0, 0}
76 };
77
78 static const int LSI_VENDOR_ID = 0x1000;
79 #endif
80
81 #ifdef OFPATH_STANDALONE
82 #define xmalloc malloc
83 void
84 grub_util_error (const char *fmt, ...)
85 {
86   va_list ap;
87
88   fprintf (stderr, "ofpath: error: ");
89   va_start (ap, fmt);
90   vfprintf (stderr, fmt, ap);
91   va_end (ap);
92   fputc ('\n', stderr);
93   exit (1);
94 }
95
96 void
97 grub_util_info (const char *fmt, ...)
98 {
99   va_list ap;
100
101   fprintf (stderr, "ofpath: info: ");
102   va_start (ap, fmt);
103   vfprintf (stderr, fmt, ap);
104   va_end (ap);
105   fputc ('\n', stderr);
106 }
107
108 #define grub_util_warn grub_util_info
109 #define _(x) x
110 #define xstrdup strdup
111 #endif
112
113 static void
114 kill_trailing_dir(char *path)
115 {
116   char *end = path + strlen(path) - 1;
117
118   while (end >= path)
119     {
120       if (*end != '/')
121         {
122           end--;
123           continue;
124         }
125       *end = '\0';
126       break;
127     }
128 }
129
130 static void
131 trim_newline (char *path)
132 {
133   char *end = path + strlen(path) - 1;
134
135   while (*end == '\n')
136     *end-- = '\0';
137 }
138
139 #define MAX_DISK_CAT    64
140
141 static char *
142 find_obppath (const char *sysfs_path_orig)
143 {
144   char *sysfs_path, *path;
145   size_t path_size = strlen (sysfs_path_orig) + sizeof ("/obppath");
146
147   sysfs_path = xstrdup (sysfs_path_orig);
148   path = xmalloc (path_size);
149
150   while (1)
151     {
152       int fd;
153       char *of_path;
154       struct stat st;
155       size_t size;
156
157       snprintf(path, path_size, "%s/obppath", sysfs_path);
158 #if 0
159       printf("Trying %s\n", path);
160 #endif
161
162       fd = open(path, O_RDONLY);
163
164 #ifndef __sparc__
165       if (fd < 0 || fstat (fd, &st) < 0)
166         {
167           if (fd >= 0)
168             close (fd);
169           snprintf(path, path_size, "%s/devspec", sysfs_path);
170           fd = open(path, O_RDONLY);
171         }
172 #endif
173
174       if (fd < 0 || fstat (fd, &st) < 0)
175         {
176           if (fd >= 0)
177             close (fd);
178           kill_trailing_dir(sysfs_path);
179           if (!strcmp(sysfs_path, "/sys"))
180             {
181               grub_util_info (_("`obppath' not found in parent dirs of `%s',"
182                                 " no IEEE1275 name discovery"),
183                               sysfs_path_orig);
184               free (path);
185               free (sysfs_path);
186               return NULL;
187             }
188           continue;
189         }
190       size = st.st_size;
191       of_path = xmalloc (size + MAX_DISK_CAT + 1);
192       memset(of_path, 0, size + MAX_DISK_CAT + 1);
193       if (read(fd, of_path, size) < 0)
194         {
195           grub_util_info (_("cannot read `%s': %s"), path, strerror (errno));
196           close(fd);
197           free (path);
198           free (of_path);
199           free (sysfs_path);
200           return NULL;
201         }
202       close(fd);
203
204       trim_newline(of_path);
205       free (path);
206       free (sysfs_path);
207       return of_path;
208     }
209 }
210
211 static char *
212 xrealpath (const char *in)
213 {
214   char *out;
215 #ifdef PATH_MAX
216   out = xmalloc (PATH_MAX);
217   out = realpath (in, out);
218 #else
219   out = realpath (in, NULL);
220 #endif
221   if (!out)
222     grub_util_error (_("failed to get canonical path of `%s'"), in);
223   return out;
224 }
225
226 static char *
227 block_device_get_sysfs_path_and_link(const char *devicenode)
228 {
229   char *rpath;
230   char *rpath2;
231   char *ret;
232   size_t tmp_size = strlen (devicenode) + sizeof ("/sys/block/");
233   char *tmp = xmalloc (tmp_size);
234
235   memcpy (tmp, "/sys/block/", sizeof ("/sys/block/"));
236   strcat (tmp, devicenode);
237
238   rpath = xrealpath (tmp);
239   rpath2 = xmalloc (strlen (rpath) + sizeof ("/device"));
240   strcpy (rpath2, rpath);
241   strcat (rpath2, "/device");
242
243   ret = xrealpath (rpath2);
244
245   free (tmp);
246   free (rpath);
247   free (rpath2);
248   return ret;
249 }
250
251 static inline int
252 my_isdigit (int c)
253 {
254   return (c >= '0' && c <= '9');
255 }
256
257 static const char *
258 trailing_digits (const char *p)
259 {
260   const char *end;
261
262   end = p + strlen(p) - 1;
263   while (end >= p)
264     {
265       if (! my_isdigit(*end))
266         break;
267       end--;
268     }
269
270   return end + 1;
271 }
272
273 static char *
274 __of_path_common(char *sysfs_path,
275                  const char *device, int devno)
276 {
277   const char *digit_string;
278   char disk[MAX_DISK_CAT];
279   char *of_path = find_obppath(sysfs_path);
280
281   if (!of_path)
282     return NULL;
283
284   digit_string = trailing_digits (device);
285   if (*digit_string == '\0')
286     {
287       snprintf(disk, sizeof (disk), "/disk@%d", devno);
288     }
289   else
290     {
291       int part;
292
293       sscanf(digit_string, "%d", &part);
294       snprintf(disk, sizeof (disk), "/disk@%d:%c", devno, 'a' + (part - 1));
295     }
296   strcat(of_path, disk);
297   return of_path;
298 }
299
300 static char *
301 get_basename(char *p)
302 {
303   char *ret = p;
304
305   while (*p)
306     {
307       if (*p == '/')
308         ret = p + 1;
309       p++;
310     }
311
312   return ret;
313 }
314
315 static char *
316 of_path_of_vdisk(const char *sys_devname __attribute__((unused)),
317                  const char *device,
318                  const char *devnode __attribute__((unused)),
319                  const char *devicenode)
320 {
321   char *sysfs_path, *p;
322   int devno, junk;
323   char *ret;
324
325   sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
326   p = get_basename (sysfs_path);
327   sscanf(p, "vdc-port-%d-%d", &devno, &junk);
328   ret = __of_path_common (sysfs_path, device, devno);
329
330   free (sysfs_path);
331   return ret;
332 }
333
334 static char *
335 of_path_of_ide(const char *sys_devname __attribute__((unused)), const char *device,
336                const char *devnode __attribute__((unused)),
337                const char *devicenode)
338 {
339   char *sysfs_path, *p;
340   int chan, devno;
341   char *ret;
342
343   sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
344   p = get_basename (sysfs_path);
345   sscanf(p, "%d.%d", &chan, &devno);
346
347   ret = __of_path_common(sysfs_path, device, 2 * chan + devno);
348
349   free (sysfs_path);
350   return ret;
351 }
352
353 #ifdef __sparc__
354 static char *
355 of_path_of_nvme(const char *sys_devname __attribute__((unused)),
356                 const char *device,
357                 const char *devnode __attribute__((unused)),
358                 const char *devicenode)
359 {
360   char *sysfs_path, *of_path, disk[MAX_DISK_CAT];
361   const char *digit_string, *part_end;
362
363   digit_string = trailing_digits (device);
364   part_end = devicenode + strlen (devicenode) - 1;
365
366   if ((*digit_string != '\0') && (*part_end == 'p'))
367     {
368       /* We have a partition number, strip it off. */
369       int part;
370       char *nvmedev, *end;
371
372       nvmedev = strdup (devicenode);
373
374       if (!nvmedev)
375         return NULL;
376
377       end = nvmedev + strlen (nvmedev) - 1;
378       /* Remove the p. */
379       *end = '\0';
380       sscanf (digit_string, "%d", &part);
381       snprintf (disk, sizeof (disk), "/disk@1:%c", 'a' + (part - 1));
382       sysfs_path = block_device_get_sysfs_path_and_link (nvmedev);
383       free (nvmedev);
384     }
385   else
386     {
387       /* We do not have the parition. */
388       snprintf (disk, sizeof (disk), "/disk@1");
389       sysfs_path = block_device_get_sysfs_path_and_link (device);
390     }
391
392   of_path = find_obppath (sysfs_path);
393
394   if (of_path)
395     strcat (of_path, disk);
396
397   free (sysfs_path);
398   return of_path;
399 }
400 #endif
401
402 static int
403 vendor_is_ATA(const char *path)
404 {
405   int fd, err;
406   char *bufname;
407   char bufcont[3];
408   size_t path_size;
409
410   path_size = strlen (path) + sizeof ("/vendor");
411
412   bufname = xmalloc (path_size);
413
414   snprintf (bufname, path_size, "%s/vendor", path);
415   fd = open (bufname, O_RDONLY);
416   if (fd < 0)
417     grub_util_error (_("cannot open `%s': %s"), bufname, strerror (errno));
418
419   memset(bufcont, 0, sizeof (bufcont));
420   err = read(fd, bufcont, sizeof (bufcont));
421   if (err < 0)
422     grub_util_error (_("cannot open `%s': %s"), bufname, strerror (errno));
423
424   close(fd);
425   free (bufname);
426
427   return (memcmp(bufcont, "ATA", 3) == 0);
428 }
429
430 #ifdef __sparc__
431 static void
432 check_hba_identifiers (const char *sysfs_path, int *vendor, int *device_id)
433 {
434   char *ed = strstr (sysfs_path, "host");
435   size_t path_size;
436   char *p, *path;
437   char buf[8];
438   int fd;
439
440   if (!ed)
441     return;
442
443   p = xstrdup (sysfs_path);
444   ed = strstr (p, "host");
445
446   *ed = '\0';
447
448   path_size = (strlen (p) + sizeof ("vendor"));
449   path = xmalloc (path_size);
450
451   if (!path)
452     goto out;
453
454   snprintf (path, path_size, "%svendor", p);
455   fd = open (path, O_RDONLY);
456
457   if (fd < 0)
458     goto out;
459
460   memset (buf, 0, sizeof (buf));
461
462   if (read (fd, buf, sizeof (buf) - 1) < 0)
463     goto out;
464
465   close (fd);
466   sscanf (buf, "%x", vendor);
467
468   snprintf (path, path_size, "%sdevice", p);
469   fd = open (path, O_RDONLY);
470
471   if (fd < 0)
472     goto out;
473
474   memset (buf, 0, sizeof (buf));
475
476   if (read (fd, buf, sizeof (buf) - 1) < 0)
477     goto out;
478
479   close (fd);
480   sscanf (buf, "%x", device_id);
481
482  out:
483   free (path);
484   free (p);
485 }
486 #endif
487
488 static void
489 check_sas (const char *sysfs_path, int *tgt, unsigned long int *sas_address)
490 {
491   char *ed = strstr (sysfs_path, "end_device");
492   char *p, *q, *path;
493   char phy[21];
494   int fd;
495   size_t path_size;
496
497   if (!ed)
498     return;
499
500   /* SAS devices are identified using disk@$PHY_ID */
501   p = xstrdup (sysfs_path);
502   ed = strstr(p, "end_device");
503   if (!ed)
504     return;
505
506   q = ed;
507   while (*q && *q != '/')
508     q++;
509   *q = '\0';
510
511   path_size = (strlen (p) + strlen (ed)
512                + sizeof ("%s/sas_device/%s/phy_identifier"));
513   path = xmalloc (path_size);
514   snprintf (path, path_size, "%s/sas_device/%s/phy_identifier", p, ed);
515   fd = open (path, O_RDONLY);
516   if (fd < 0)
517     grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
518
519   memset (phy, 0, sizeof (phy));
520   if (read (fd, phy, sizeof (phy) - 1) < 0)
521     grub_util_error (_("cannot read `%s': %s"), path, strerror (errno));
522
523   close (fd);
524
525   sscanf (phy, "%d", tgt);
526
527   snprintf (path, path_size, "%s/sas_device/%s/sas_address", p, ed);
528   fd = open (path, O_RDONLY);
529   if (fd < 0)
530     grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
531
532   memset (phy, 0, sizeof (phy));
533   if (read (fd, phy, sizeof (phy) - 1) < 0)
534     grub_util_error (_("cannot read `%s': %s"), path, strerror (errno));
535   sscanf (phy, "%lx", sas_address);
536
537   free (path);
538   free (p);
539   close (fd);
540 }
541
542 static char *
543 of_path_of_scsi(const char *sys_devname __attribute__((unused)), const char *device,
544                 const char *devnode __attribute__((unused)),
545                 const char *devicenode)
546 {
547   const char *p, *digit_string, *disk_name;
548   int host, bus, tgt, lun;
549   unsigned long int sas_address = 0;
550   char *sysfs_path, disk[MAX_DISK_CAT - sizeof ("/fp@0,0")];
551   char *of_path;
552
553   sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
554   p = get_basename (sysfs_path);
555   sscanf(p, "%d:%d:%d:%d", &host, &bus, &tgt, &lun);
556   check_sas (sysfs_path, &tgt, &sas_address);
557
558   if (vendor_is_ATA(sysfs_path))
559     {
560       of_path = __of_path_common(sysfs_path, device, tgt);
561       free (sysfs_path);
562       return of_path;
563     }
564
565   of_path = find_obppath(sysfs_path);
566   if (!of_path)
567     goto out;
568
569   if (strstr (of_path, "qlc"))
570     strcat (of_path, "/fp@0,0");
571
572   if (strstr (of_path, "sbus"))
573     disk_name = "sd";
574   else
575     disk_name = "disk";
576
577   digit_string = trailing_digits (device);
578   if (strncmp (of_path, "/vdevice/", sizeof ("/vdevice/") - 1) == 0)
579     {
580       unsigned long id = 0x8000 | (tgt << 8) | (bus << 5) | lun;
581       if (*digit_string == '\0')
582         {
583           snprintf(disk, sizeof (disk), "/%s@%04lx000000000000", disk_name, id);
584         }
585       else
586         {
587           int part;
588
589           sscanf(digit_string, "%d", &part);
590           snprintf(disk, sizeof (disk),
591                    "/%s@%04lx000000000000:%c", disk_name, id, 'a' + (part - 1));
592         }
593     }
594   else
595     {
596 #ifdef __sparc__
597       ofpath_sparc_addressing addressing = GRUB_OFPATH_SPARC_TGT_LUN;
598       int vendor = 0, device_id = 0;
599       char *optr = disk;
600
601       check_hba_identifiers (sysfs_path, &vendor, &device_id);
602
603       if (vendor == LSI_VENDOR_ID)
604         {
605           struct ofpath_sparc_hba *lsi_hba;
606
607           /*
608            * Over time different OF addressing schemes have been supported.
609            * There is no generic addressing scheme that works across
610            * every HBA.
611            */
612           for (lsi_hba = sparc_lsi_hba; lsi_hba->device_id; lsi_hba++)
613             if (lsi_hba->device_id == device_id)
614               {
615                 addressing = lsi_hba->addressing;
616                 break;
617               }
618         }
619
620       if (addressing == GRUB_OFPATH_SPARC_WWN_ADDR)
621         optr += snprintf (disk, sizeof (disk), "/%s@w%lx,%x", disk_name,
622                           sas_address, lun);
623       else
624         optr += snprintf (disk, sizeof (disk), "/%s@%x,%x", disk_name, tgt,
625                           lun);
626
627       if (*digit_string != '\0')
628         {
629           int part;
630
631           sscanf (digit_string, "%d", &part);
632           snprintf (optr, sizeof (disk) - (optr - disk - 1), ":%c", 'a'
633                     + (part - 1));
634         }
635 #else
636       if (lun == 0)
637         {
638           int sas_id = 0;
639           sas_id = bus << 16 | tgt << 8 | lun;
640
641           if (*digit_string == '\0')
642             {
643               snprintf(disk, sizeof (disk), "/sas/%s@%x", disk_name, sas_id);
644             }
645           else
646             {
647               int part;
648
649               sscanf(digit_string, "%d", &part);
650               snprintf(disk, sizeof (disk),
651                        "/sas/%s@%x:%c", disk_name, sas_id, 'a' + (part - 1));
652             }
653         }
654       else
655         {
656           char *lunstr;
657           int lunpart[4];
658
659           lunstr = xmalloc (20);
660
661           lunpart[0] = (lun >> 8) & 0xff;
662           lunpart[1] = lun & 0xff;
663           lunpart[2] = (lun >> 24) & 0xff;
664           lunpart[3] = (lun >> 16) & 0xff;
665
666           sprintf(lunstr, "%02x%02x%02x%02x00000000", lunpart[0], lunpart[1], lunpart[2], lunpart[3]);
667           long int longlun = atol(lunstr);
668
669           if (*digit_string == '\0')
670             {
671               snprintf(disk, sizeof (disk), "/sas/%s@%lx,%lu", disk_name, sas_address, longlun);
672             }
673           else
674             {
675               int part;
676
677               sscanf(digit_string, "%d", &part);
678               snprintf(disk, sizeof (disk),
679                        "/sas/%s@%lx,%lu:%c", disk_name, sas_address, longlun, 'a' + (part - 1));
680             }
681           free (lunstr);
682         }
683 #endif
684     }
685   strcat(of_path, disk);
686
687  out:
688   free (sysfs_path);
689   return of_path;
690 }
691
692 static char *
693 strip_trailing_digits (const char *p)
694 {
695   char *new, *end;
696
697   new = strdup (p);
698   end = new + strlen(new) - 1;
699   while (end >= new)
700     {
701       if (! my_isdigit(*end))
702         break;
703       *end-- = '\0';
704     }
705
706   return new;
707 }
708
709 char *
710 grub_util_devname_to_ofpath (const char *sys_devname)
711 {
712   char *name_buf, *device, *devnode, *devicenode, *ofpath;
713
714   name_buf = xrealpath (sys_devname);
715
716   device = get_basename (name_buf);
717   devnode = strip_trailing_digits (name_buf);
718   devicenode = strip_trailing_digits (device);
719
720   if (device[0] == 'h' && device[1] == 'd')
721     ofpath = of_path_of_ide(name_buf, device, devnode, devicenode);
722   else if (device[0] == 's'
723            && (device[1] == 'd' || device[1] == 'r'))
724     ofpath = of_path_of_scsi(name_buf, device, devnode, devicenode);
725   else if (device[0] == 'v' && device[1] == 'd' && device[2] == 'i'
726            && device[3] == 's' && device[4] == 'k')
727     ofpath = of_path_of_vdisk(name_buf, device, devnode, devicenode);
728   else if (device[0] == 'f' && device[1] == 'd'
729            && device[2] == '0' && device[3] == '\0')
730     /* All the models I've seen have a devalias "floppy".
731        New models have no floppy at all. */
732     ofpath = xstrdup ("floppy");
733 #ifdef __sparc__
734   else if (device[0] == 'n' && device[1] == 'v' && device[2] == 'm'
735            && device[3] == 'e')
736     ofpath = of_path_of_nvme (name_buf, device, devnode, devicenode);
737 #endif
738   else
739     {
740       grub_util_warn (_("unknown device type %s"), device);
741       ofpath = NULL;
742     }
743
744   free (devnode);
745   free (devicenode);
746   free (name_buf);
747
748   return ofpath;
749 }
750
751 #ifdef OFPATH_STANDALONE
752 int main(int argc, char **argv)
753 {
754   char *of_path;
755
756   if (argc != 2)
757     {
758       printf(_("Usage: %s DEVICE\n"), argv[0]);
759       return 1;
760     }
761
762   of_path = grub_util_devname_to_ofpath (argv[1]);
763   if (of_path)
764     printf("%s\n", of_path);
765   free (of_path);
766
767   return 0;
768 }
769 #endif