84
#define OF_PATH_MAX 256
99
#define MAX_DISK_CAT 64
87
find_obppath(char *of_path, const char *sysfs_path_orig)
102
find_obppath (const char *sysfs_path_orig)
89
104
char *sysfs_path, *path;
91
sysfs_path = xmalloc (PATH_MAX);
92
path = xmalloc (PATH_MAX);
94
strcpy(sysfs_path, sysfs_path_orig);
105
size_t path_size = strlen (sysfs_path_orig) + sizeof ("/obppath");
107
sysfs_path = xstrdup (sysfs_path_orig);
108
path = xmalloc (path_size);
99
snprintf(path, PATH_MAX, "%s/obppath", sysfs_path);
117
snprintf(path, path_size, "%s/obppath", sysfs_path);
101
119
printf("Trying %s\n", path);
104
122
fd = open(path, O_RDONLY);
123
if (fd < 0 || fstat (fd, &st) < 0)
125
snprintf(path, path_size, "%s/devspec", sysfs_path);
126
fd = open(path, O_RDONLY);
129
if (fd < 0 || fstat (fd, &st) < 0)
107
131
kill_trailing_dir(sysfs_path);
108
132
if (!strcmp(sysfs_path, "/sys"))
109
grub_util_error("'obppath' not found in parent dirs of %s",
134
grub_util_info (_("'obppath' not found in parent dirs of %s,"
135
" no IEEE1275 name discovery"),
113
memset(of_path, 0, OF_PATH_MAX);
114
read(fd, of_path, OF_PATH_MAX);
144
of_path = xmalloc (size + MAX_DISK_CAT + 1);
145
memset(of_path, 0, size + MAX_DISK_CAT + 1);
146
read(fd, of_path, size);
117
149
trim_newline(of_path);
126
block_device_get_sysfs_path_and_link(const char *devicenode,
127
char *sysfs_path, int sysfs_path_len)
129
char *rpath = xmalloc (PATH_MAX);
131
snprintf(sysfs_path, sysfs_path_len, "/sys/block/%s", devicenode);
133
if (!realpath (sysfs_path, rpath))
134
grub_util_error ("cannot get the real path of `%s'", sysfs_path);
136
strcat(rpath, "/device");
138
if (!realpath (rpath, sysfs_path))
139
grub_util_error ("cannot get the real path of `%s'", rpath);
157
xrealpath (const char *in)
161
out = xmalloc (PATH_MAX);
162
out = realpath (in, out);
164
out = realpath (in, NULL);
167
grub_util_error (_("failed to get canonical path of %s"), in);
172
block_device_get_sysfs_path_and_link(const char *devicenode)
177
size_t tmp_size = strlen (devicenode) + sizeof ("/sys/block/");
178
char *tmp = xmalloc (tmp_size);
180
memcpy (tmp, "/sys/block/", sizeof ("/sys/block/"));
181
strcat (tmp, devicenode);
183
rpath = xrealpath (tmp);
184
rpath2 = xmalloc (strlen (rpath) + sizeof ("/device"));
185
strcpy (rpath2, rpath);
186
strcat (rpath2, "/device");
188
ret = xrealpath (rpath2);
199
return (c >= '0' && c <= '9');
144
202
static const char *
161
__of_path_common(char *of_path, char *sysfs_path,
219
__of_path_common(char *sysfs_path,
162
220
const char *device, int devno)
164
222
const char *digit_string;
223
char disk[MAX_DISK_CAT];
224
char *of_path = find_obppath(sysfs_path);
167
find_obppath(of_path, sysfs_path);
169
229
digit_string = trailing_digits (device);
170
230
if (*digit_string == '\0')
172
sprintf(disk, "/disk@%d", devno);
232
snprintf(disk, sizeof (disk), "/disk@%d", devno);
178
238
sscanf(digit_string, "%d", &part);
179
sprintf(disk, "/disk@%d:%c", devno, 'a' + (part - 1));
239
snprintf(disk, sizeof (disk), "/disk@%d:%c", devno, 'a' + (part - 1));
181
241
strcat(of_path, disk);
200
of_path_of_vdisk(char *of_path,
201
const char *devname __attribute__((unused)),
261
of_path_of_vdisk(const char *sys_devname __attribute__((unused)),
202
262
const char *device,
203
263
const char *devnode __attribute__((unused)),
204
264
const char *devicenode)
206
266
char *sysfs_path, *p;
209
sysfs_path = xmalloc (PATH_MAX);
210
block_device_get_sysfs_path_and_link(devicenode,
211
sysfs_path, PATH_MAX);
270
sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
212
271
p = get_basename (sysfs_path);
213
272
sscanf(p, "vdc-port-%d-%d", &devno, &junk);
214
__of_path_common(of_path, sysfs_path, device, devno);
273
ret = __of_path_common (sysfs_path, device, devno);
216
275
free (sysfs_path);
220
of_path_of_ide(char *of_path,
221
const char *devname __attribute__((unused)), const char *device,
280
of_path_of_ide(const char *sys_devname __attribute__((unused)), const char *device,
222
281
const char *devnode __attribute__((unused)),
223
282
const char *devicenode)
225
284
char *sysfs_path, *p;
228
sysfs_path = xmalloc (PATH_MAX);
229
block_device_get_sysfs_path_and_link(devicenode,
230
sysfs_path, PATH_MAX);
288
sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
231
289
p = get_basename (sysfs_path);
232
290
sscanf(p, "%d.%d", &chan, &devno);
234
__of_path_common(of_path, sysfs_path, device, devno);
292
ret = __of_path_common(sysfs_path, device, 2 * chan + devno);
236
294
free (sysfs_path);
240
299
vendor_is_ATA(const char *path)
245
buf = xmalloc (PATH_MAX);
247
snprintf(buf, PATH_MAX, "%s/vendor", path);
248
fd = open(buf, O_RDONLY);
306
path_size = strlen (path) + sizeof ("/vendor");
308
bufname = xmalloc (path_size);
310
snprintf (bufname, path_size, "%s/vendor", path);
311
fd = open (bufname, O_RDONLY);
250
grub_util_error ("cannot open 'vendor' node of `%s'", path);
313
grub_util_error (_("cannot open `%s': %s"), bufname, strerror (errno));
252
memset(buf, 0, PATH_MAX);
253
err = read(fd, buf, PATH_MAX);
315
memset(bufcont, 0, sizeof (bufcont));
316
err = read(fd, bufcont, sizeof (bufcont));
255
grub_util_error ("cannot read 'vendor' node of `%s'", path);
318
grub_util_error (_("cannot open `%s': %s"), bufname, strerror (errno));
261
if (!strncmp(buf, "ATA", 3))
323
return (memcmp(bufcont, "ATA", 3) == 0);
286
path = xmalloc (PATH_MAX);
287
sprintf (path, "%s/sas_device:%s/phy_identifier", p, ed);
347
path_size = (strlen (p) + strlen (ed)
348
+ sizeof ("%s/sas_device/%s/phy_identifier"));
349
path = xmalloc (path_size);
350
snprintf (path, path_size, "%s/sas_device/%s/phy_identifier", p, ed);
289
fd = open(path, O_RDONLY);
352
fd = open (path, O_RDONLY);
291
grub_util_error("cannot open SAS PHY ID `%s'\n", path);
354
grub_util_error (_("cannot open `%s': %s"), path, strerror (errno));
293
356
memset (phy, 0, sizeof (phy));
294
357
read (fd, phy, sizeof (phy));
303
of_path_of_scsi(char *of_path,
304
const char *devname __attribute__((unused)), const char *device,
367
of_path_of_scsi(const char *sys_devname __attribute__((unused)), const char *device,
305
368
const char *devnode __attribute__((unused)),
306
369
const char *devicenode)
308
371
const char *p, *digit_string, *disk_name;
309
372
int host, bus, tgt, lun;
310
char *sysfs_path, disk[64];
312
sysfs_path = xmalloc (PATH_MAX);
314
block_device_get_sysfs_path_and_link(devicenode,
315
sysfs_path, PATH_MAX);
373
char *sysfs_path, disk[MAX_DISK_CAT - sizeof ("/fp@0,0")];
376
sysfs_path = block_device_get_sysfs_path_and_link(devicenode);
316
377
p = get_basename (sysfs_path);
317
378
sscanf(p, "%d:%d:%d:%d", &host, &bus, &tgt, &lun);
318
379
check_sas (sysfs_path, &tgt);
320
381
if (vendor_is_ATA(sysfs_path))
322
__of_path_common(of_path, sysfs_path, device, tgt);
383
of_path = __of_path_common(sysfs_path, device, tgt);
323
384
free (sysfs_path);
327
find_obppath(of_path, sysfs_path);
388
of_path = find_obppath(sysfs_path);
328
389
free (sysfs_path);
330
393
if (strstr (of_path, "qlc"))
331
394
strcat (of_path, "/fp@0,0");
336
399
disk_name = "disk";
338
401
digit_string = trailing_digits (device);
339
if (*digit_string == '\0')
402
if (strncmp (of_path, "/vdevice/", sizeof ("/vdevice/") - 1) == 0)
341
sprintf(disk, "/%s@%x,%d", disk_name, tgt, lun);
404
unsigned long id = 0x8000 | (tgt << 8) | (bus << 5) | lun;
405
if (*digit_string == '\0')
407
snprintf(disk, sizeof (disk), "/%s@%04lx000000000000", disk_name, id);
413
sscanf(digit_string, "%d", &part);
414
snprintf(disk, sizeof (disk),
415
"/%s@%04lx000000000000:%c", disk_name, id, 'a' + (part - 1));
420
if (*digit_string == '\0')
422
snprintf(disk, sizeof (disk), "/%s@%x,%d", disk_name, tgt, lun);
347
sscanf(digit_string, "%d", &part);
348
sprintf(disk, "/%s@%x,%d:%c", disk_name, tgt, lun, 'a' + (part - 1));
428
sscanf(digit_string, "%d", &part);
429
snprintf(disk, sizeof (disk),
430
"/%s@%x,%d:%c", disk_name, tgt, lun, 'a' + (part - 1));
350
433
strcat(of_path, disk);
371
grub_util_devname_to_ofpath (const char *devname)
455
grub_util_devname_to_ofpath (const char *sys_devname)
373
457
char *name_buf, *device, *devnode, *devicenode, *ofpath;
375
name_buf = xmalloc (PATH_MAX);
376
name_buf = realpath (devname, name_buf);
378
grub_util_error ("cannot get the real path of `%s'", devname);
459
name_buf = xrealpath (sys_devname);
380
461
device = get_basename (name_buf);
381
462
devnode = strip_trailing_digits (name_buf);
382
463
devicenode = strip_trailing_digits (device);
384
ofpath = xmalloc (OF_PATH_MAX);
386
465
if (device[0] == 'h' && device[1] == 'd')
387
of_path_of_ide(ofpath, name_buf, device, devnode, devicenode);
466
ofpath = of_path_of_ide(name_buf, device, devnode, devicenode);
388
467
else if (device[0] == 's'
389
468
&& (device[1] == 'd' || device[1] == 'r'))
390
of_path_of_scsi(ofpath, name_buf, device, devnode, devicenode);
469
ofpath = of_path_of_scsi(name_buf, device, devnode, devicenode);
391
470
else if (device[0] == 'v' && device[1] == 'd' && device[2] == 'i'
392
471
&& device[3] == 's' && device[4] == 'k')
393
of_path_of_vdisk(ofpath, name_buf, device, devnode, devicenode);
472
ofpath = of_path_of_vdisk(name_buf, device, devnode, devicenode);
394
473
else if (device[0] == 'f' && device[1] == 'd'
395
474
&& device[2] == '0' && device[3] == '\0')
396
475
/* All the models I've seen have a devalias "floppy".
397
476
New models have no floppy at all. */
398
strcpy (ofpath, "floppy");
477
ofpath = xstrdup ("floppy");
400
grub_util_error ("unknown device type %s\n", device);
480
grub_util_warn (_("unknown device type %s\n"), device);
403
485
free (devicenode);