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