ofpath.c 13 KB

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