aboutsummaryrefslogtreecommitdiffstats
path: root/util/ieee1275/ofpath.c
blob: 1a433345d7bdf0921ed243737b5404bda520a2f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/* ofpath.c - calculate OpenFirmware path names given an OS device */
/*
 *  GRUB  --  GRand Unified Bootloader
 *  Copyright (C) 2009  Free Software Foundation, Inc.
 *
 *  GRUB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  GRUB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
 */

#undef OFPATH_STANDALONE

#ifndef OFPATH_STANDALONE
#include <grub/types.h>
#include <grub/util/misc.h>
#include <grub/util/ofpath.h>
#endif

#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <malloc.h>
#include <fcntl.h>
#include <errno.h>
#include <ctype.h>

#ifdef OFPATH_STANDALONE
#define xmalloc malloc
void
grub_util_error (const char *fmt, ...)
{
  va_list ap;

  fprintf (stderr, "ofpath: error: ");
  va_start (ap, fmt);
  vfprintf (stderr, fmt, ap);
  va_end (ap);
  fputc ('\n', stderr);
  exit (1);
}

#endif

static void
kill_trailing_dir(char *path)
{
  char *end = path + strlen(path) - 1;

  while (end >= path)
    {
      if (*end != '/')
	{
	  end--;
	  continue;
	}
      *end = '\0';
      break;
    }
}

static void
trim_newline (char *path)
{
  char *end = path + strlen(path) - 1;

  while (*end == '\n')
    *end-- = '\0';
}

#define OF_PATH_MAX	256

static void
find_obppath(char *of_path, const char *sysfs_path_orig)
{
  char *sysfs_path, *path;

  sysfs_path = xmalloc (PATH_MAX);
  path = xmalloc (PATH_MAX);

  strcpy(sysfs_path, sysfs_path_orig);
  while (1)
    {
      int fd;

      snprintf(path, PATH_MAX, "%s/obppath", sysfs_path);
#if 0
      printf("Trying %s\n", path);
#endif

      fd = open(path, O_RDONLY);
      if (fd < 0)
	{
	  kill_trailing_dir(sysfs_path);
	  if (!strcmp(sysfs_path, "/sys"))
	    grub_util_error("'obppath' not found in parent dirs of %s",
			    sysfs_path_orig);
	  continue;
	}
      memset(of_path, 0, OF_PATH_MAX);
      read(fd, of_path, OF_PATH_MAX);
      close(fd);

      trim_newline(of_path);
      break;
    }

  free (path);
  free (sysfs_path);
}

static void
block_device_get_sysfs_path_and_link(const char *devicenode,
				     char *sysfs_path, int sysfs_path_len)
{
  char *rpath = xmalloc (PATH_MAX);

  snprintf(sysfs_path, sysfs_path_len, "/sys/block/%s", devicenode);

  if (!realpath (sysfs_path, rpath))
    grub_util_error ("cannot get the real path of `%s'", sysfs_path);

  strcat(rpath, "/device");

  if (!realpath (rpath, sysfs_path))
    grub_util_error ("cannot get the real path of `%s'", rpath);

  free (rpath);
}

static const char *
trailing_digits (const char *p)
{
  const char *end;

  end = p + strlen(p) - 1;
  while (end >= p)
    {
      if (! isdigit(*end))
	break;
      end--;
    }

  return end + 1;
}

static void
__of_path_common(char *of_path, char *sysfs_path,
		 const char *device, int devno)
{
  const char *digit_string;
  char disk[64];

  find_obppath(of_path, sysfs_path);

  digit_string = trailing_digits (device);
  if (*digit_string == '\0')
    {
      sprintf(disk, "/disk@%d", devno);
    }
  else
    {
      int part;

      sscanf(digit_string, "%d", &part);
      sprintf(disk, "/disk@%d:%c", devno, 'a' + (part - 1));
    }
  strcat(of_path, disk);
}

static char *
get_basename(char *p)
{
  char *ret = p;

  while (*p)
    {
      if (*p == '/')
	ret = p + 1;
      p++;
    }

  return ret;
}

static void
of_path_of_vdisk(char *of_path,
		 const char *devname __attribute__((unused)),
		 const char *device,
		 const char *devnode __attribute__((unused)),
		 const char *devicenode)
{
  char *sysfs_path, *p;
  int devno, junk;

  sysfs_path = xmalloc (PATH_MAX);
  block_device_get_sysfs_path_and_link(devicenode,
				       sysfs_path, PATH_MAX);
  p = get_basename (sysfs_path);
  sscanf(p, "vdc-port-%d-%d", &devno, &junk);
  __of_path_common(of_path, sysfs_path, device, devno);

  free (sysfs_path);
}

static void
of_path_of_ide(char *of_path,
	       const char *devname __attribute__((unused)), const char *device,
	       const char *devnode __attribute__((unused)),
	       const char *devicenode)
{
  char *sysfs_path, *p;
  int chan, devno;

  sysfs_path = xmalloc (PATH_MAX);
  block_device_get_sysfs_path_and_link(devicenode,
				       sysfs_path, PATH_MAX);
  p = get_basename (sysfs_path);
  sscanf(p, "%d.%d", &chan, &devno);

  __of_path_common(of_path, sysfs_path, device, devno);

  free (sysfs_path);
}

static int
vendor_is_ATA(const char *path)
{
  int fd, err;
  char *buf;

  buf = xmalloc (PATH_MAX);

  snprintf(buf, PATH_MAX, "%s/vendor", path);
  fd = open(buf, O_RDONLY);
  if (fd < 0)
    grub_util_error ("cannot open 'vendor' node of `%s'", path);

  memset(buf, 0, PATH_MAX);
  err = read(fd, buf, PATH_MAX);
  if (err < 0)
    grub_util_error ("cannot read 'vendor' node of `%s'", path);

  close(fd);

  free (buf);

  if (!strncmp(buf, "ATA", 3))
    return 1;
  return 0;
}

static void
check_sas (char *sysfs_path, int *tgt)
{
  char *ed = strstr (sysfs_path, "end_device");
  char *p, *q, *path;
  char phy[16];
  int fd;

  if (!ed)
    return;

  /* SAS devices are identified using disk@$PHY_ID */
  p = strdup (sysfs_path);
  ed = strstr(p, "end_device");

  q = ed;
  while (*q && *q != '/')
    q++;
  *q = '\0';

  path = xmalloc (PATH_MAX);
  sprintf (path, "%s/sas_device:%s/phy_identifier", p, ed);

  fd = open(path, O_RDONLY);
  if (fd < 0)
    grub_util_error("cannot open SAS PHY ID `%s'\n", path);

  memset (phy, 0, sizeof (phy));
  read (fd, phy, sizeof (phy));

  sscanf (phy, "%d", tgt);

  free (path);
  free (p);
}

static void
of_path_of_scsi(char *of_path,
		const char *devname __attribute__((unused)), const char *device,
		const char *devnode __attribute__((unused)),
		const char *devicenode)
{
  const char *p, *digit_string, *disk_name;
  int host, bus, tgt, lun;
  char *sysfs_path, disk[64];

  sysfs_path = xmalloc (PATH_MAX);

  block_device_get_sysfs_path_and_link(devicenode,
				       sysfs_path, PATH_MAX);
  p = get_basename (sysfs_path);
  sscanf(p, "%d:%d:%d:%d", &host, &bus, &tgt, &lun);
  check_sas (sysfs_path, &tgt);

  if (vendor_is_ATA(sysfs_path))
    {
      __of_path_common(of_path, sysfs_path, device, tgt);
      free (sysfs_path);
      return;
    }

  find_obppath(of_path, sysfs_path);
  free (sysfs_path);

  if (strstr (of_path, "qlc"))
    strcat (of_path, "/fp@0,0");

  if (strstr (of_path, "sbus"))
    disk_name = "sd";
  else
    disk_name = "disk";

  digit_string = trailing_digits (device);
  if (*digit_string == '\0')
    {
      sprintf(disk, "/%s@%x,%d", disk_name, tgt, lun);
    }
  else
    {
      int part;

      sscanf(digit_string, "%d", &part);
      sprintf(disk, "/%s@%x,%d:%c", disk_name, tgt, lun, 'a' + (part - 1));
    }
  strcat(of_path, disk);
}

static char *
strip_trailing_digits (const char *p)
{
  char *new, *end;

  new = strdup (p);
  end = new + strlen(new) - 1;
  while (end >= new)
    {
      if (! isdigit(*end))
	break;
      *end-- = '\0';
    }

  return new;
}

char *
grub_util_devname_to_ofpath (const char *devname)
{
  char *name_buf, *device, *devnode, *devicenode, *ofpath;

  name_buf = xmalloc (PATH_MAX);
  name_buf = realpath (devname, name_buf);
  if (! name_buf)
    grub_util_error ("cannot get the real path of `%s'", devname);

  device = get_basename (name_buf);
  devnode = strip_trailing_digits (name_buf);
  devicenode = strip_trailing_digits (device);

  ofpath = xmalloc (OF_PATH_MAX);

  if (device[0] == 'h' && device[1] == 'd')
    of_path_of_ide(ofpath, name_buf, device, devnode, devicenode);
  else if (device[0] == 's'
	   && (device[1] == 'd' || device[1] == 'r'))
    of_path_of_scsi(ofpath, name_buf, device, devnode, devicenode);
  else if (device[0] == 'v' && device[1] == 'd' && device[2] == 'i'
	   && device[3] == 's' && device[4] == 'k')
    of_path_of_vdisk(ofpath, name_buf, device, devnode, devicenode);
  else if (device[0] == 'f' && device[1] == 'd'
	   && device[2] == '0' && device[3] == '\0')
    /* All the models I've seen have a devalias "floppy".
       New models have no floppy at all. */
    strcpy (ofpath, "floppy");
  else
    grub_util_error ("unknown device type %s\n", device);

  free (devnode);
  free (devicenode);
  free (name_buf);

  return ofpath;
}

#ifdef OFPATH_STANDALONE
int main(int argc, char **argv)
{
  char *of_path;

  if (argc != 2)
    {
      printf("Usage: grub-ofpathname DEVICE\n");
      return 1;
    }

  of_path = grub_util_devname_to_ofpath (argv[1]);
  printf("%s\n", of_path);

  return 0;
}
#endif