obdisk.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  1. /* obdisk.c - Open Boot disk access. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2019 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. #include <grub/disk.h>
  20. #include <grub/env.h>
  21. #include <grub/i18n.h>
  22. #include <grub/kernel.h>
  23. #include <grub/list.h>
  24. #include <grub/misc.h>
  25. #include <grub/mm.h>
  26. #include <grub/scsicmd.h>
  27. #include <grub/time.h>
  28. #include <grub/safemath.h>
  29. #include <grub/ieee1275/ieee1275.h>
  30. #include <grub/ieee1275/obdisk.h>
  31. #define IEEE1275_DEV "ieee1275/"
  32. #define IEEE1275_DISK_ALIAS "/disk@"
  33. struct disk_dev
  34. {
  35. struct disk_dev *next;
  36. struct disk_dev **prev;
  37. char *name;
  38. char *raw_name;
  39. char *grub_devpath;
  40. char *grub_alias_devpath;
  41. grub_ieee1275_ihandle_t ihandle;
  42. grub_uint32_t block_size;
  43. grub_uint64_t num_blocks;
  44. unsigned int log_sector_size;
  45. grub_uint32_t opened;
  46. grub_uint32_t valid;
  47. grub_uint32_t boot_dev;
  48. };
  49. struct parent_dev
  50. {
  51. struct parent_dev *next;
  52. struct parent_dev **prev;
  53. char *name;
  54. char *type;
  55. grub_ieee1275_ihandle_t ihandle;
  56. grub_uint32_t address_cells;
  57. };
  58. static struct grub_scsi_test_unit_ready tur =
  59. {
  60. .opcode = grub_scsi_cmd_test_unit_ready,
  61. .lun = 0,
  62. .reserved1 = 0,
  63. .reserved2 = 0,
  64. .reserved3 = 0,
  65. .control = 0,
  66. };
  67. static int disks_enumerated;
  68. static struct disk_dev *disk_devs;
  69. static struct parent_dev *parent_devs;
  70. static const char *block_blacklist[] = {
  71. /* Requires additional work in grub before being able to be used. */
  72. "/iscsi-hba",
  73. /* This block device should never be used by grub. */
  74. "/reboot-memory@0",
  75. 0
  76. };
  77. #define STRCMP(a, b) ((a) && (b) && (grub_strcmp (a, b) == 0))
  78. static char *
  79. strip_ob_partition (char *path)
  80. {
  81. char *sptr;
  82. sptr = grub_strstr (path, ":");
  83. if (sptr != NULL)
  84. *sptr = '\0';
  85. return path;
  86. }
  87. static void
  88. escape_commas (const char *src, char *dest)
  89. {
  90. const char *iptr;
  91. for (iptr = src; *iptr; )
  92. {
  93. if (*iptr == ',')
  94. *dest++ ='\\';
  95. *dest++ = *iptr++;
  96. }
  97. *dest = '\0';
  98. }
  99. static int
  100. count_commas (const char *src)
  101. {
  102. int count = 0;
  103. for ( ; *src; src++)
  104. if (*src == ',')
  105. count++;
  106. return count;
  107. }
  108. static char *
  109. decode_grub_devname (const char *name)
  110. {
  111. char *devpath;
  112. char *p, c;
  113. grub_size_t sz;
  114. if (grub_add (grub_strlen (name), 1, &sz))
  115. {
  116. grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining size of device name"));
  117. return NULL;
  118. }
  119. devpath = grub_malloc (sz);
  120. if (devpath == NULL)
  121. return NULL;
  122. /* Un-escape commas. */
  123. p = devpath;
  124. while ((c = *name++) != '\0')
  125. {
  126. if (c == '\\' && *name == ',')
  127. {
  128. *p++ = ',';
  129. name++;
  130. }
  131. else
  132. *p++ = c;
  133. }
  134. *p++ = '\0';
  135. return devpath;
  136. }
  137. static char *
  138. encode_grub_devname (const char *path)
  139. {
  140. char *encoding, *optr;
  141. grub_size_t sz;
  142. if (path == NULL)
  143. return NULL;
  144. if (grub_add (sizeof (IEEE1275_DEV) + 1, count_commas (path), &sz) ||
  145. grub_add (sz, grub_strlen (path), &sz))
  146. {
  147. grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining encoding size"));
  148. grub_print_error ();
  149. return NULL;
  150. }
  151. encoding = grub_malloc (sz);
  152. if (encoding == NULL)
  153. {
  154. grub_print_error ();
  155. return NULL;
  156. }
  157. optr = grub_stpcpy (encoding, IEEE1275_DEV);
  158. escape_commas (path, optr);
  159. return encoding;
  160. }
  161. static char *
  162. get_parent_devname (const char *devname)
  163. {
  164. char *parent, *pptr;
  165. parent = grub_strdup (devname);
  166. if (parent == NULL)
  167. {
  168. grub_print_error ();
  169. return NULL;
  170. }
  171. pptr = grub_strstr (parent, IEEE1275_DISK_ALIAS);
  172. if (pptr != NULL)
  173. *pptr = '\0';
  174. return parent;
  175. }
  176. static void
  177. free_parent_dev (struct parent_dev *parent)
  178. {
  179. if (parent != NULL)
  180. {
  181. grub_free (parent->name);
  182. grub_free (parent->type);
  183. grub_free (parent);
  184. }
  185. }
  186. static struct parent_dev *
  187. init_parent (const char *parent)
  188. {
  189. struct parent_dev *op;
  190. op = grub_zalloc (sizeof (struct parent_dev));
  191. if (op == NULL)
  192. {
  193. grub_print_error ();
  194. return NULL;
  195. }
  196. op->name = grub_strdup (parent);
  197. op->type = grub_malloc (IEEE1275_MAX_PROP_LEN);
  198. if ((op->name == NULL) || (op->type == NULL))
  199. {
  200. grub_print_error ();
  201. free_parent_dev (op);
  202. return NULL;
  203. }
  204. return op;
  205. }
  206. static struct parent_dev *
  207. open_new_parent (const char *parent)
  208. {
  209. struct parent_dev *op = init_parent(parent);
  210. grub_ieee1275_ihandle_t ihandle;
  211. grub_ieee1275_phandle_t phandle;
  212. grub_uint32_t address_cells = 2;
  213. if (op == NULL)
  214. return NULL;
  215. grub_ieee1275_open (parent, &ihandle);
  216. if (ihandle == 0)
  217. {
  218. grub_error (GRUB_ERR_BAD_DEVICE, "unable to open %s", parent);
  219. grub_print_error ();
  220. free_parent_dev (op);
  221. return NULL;
  222. }
  223. if (grub_ieee1275_instance_to_package (ihandle, &phandle))
  224. {
  225. grub_error (GRUB_ERR_BAD_DEVICE, "unable to get parent %s", parent);
  226. grub_print_error ();
  227. free_parent_dev (op);
  228. return NULL;
  229. }
  230. /*
  231. * IEEE Std 1275-1994 page 110: A missing "address-cells" property
  232. * signifies that the number of address cells is two. So ignore on error.
  233. */
  234. if (grub_ieee1275_get_integer_property (phandle, "#address-cells",
  235. &address_cells,
  236. sizeof (address_cells), 0) != 0)
  237. address_cells = 2;
  238. grub_ieee1275_get_property (phandle, "device_type", op->type,
  239. IEEE1275_MAX_PROP_LEN, NULL);
  240. op->ihandle = ihandle;
  241. op->address_cells = address_cells;
  242. return op;
  243. }
  244. static struct parent_dev *
  245. open_parent (const char *parent)
  246. {
  247. struct parent_dev *op;
  248. op = grub_named_list_find (GRUB_AS_NAMED_LIST (parent_devs), parent);
  249. if (op == NULL)
  250. {
  251. op = open_new_parent (parent);
  252. if (op != NULL)
  253. grub_list_push (GRUB_AS_LIST_P (&parent_devs), GRUB_AS_LIST (op));
  254. }
  255. return op;
  256. }
  257. static void
  258. display_parents (void)
  259. {
  260. struct parent_dev *parent;
  261. grub_printf ("-------------------- PARENTS --------------------\n");
  262. FOR_LIST_ELEMENTS (parent, parent_devs)
  263. {
  264. grub_printf ("name: %s\n", parent->name);
  265. grub_printf ("type: %s\n", parent->type);
  266. grub_printf ("address_cells %x\n", parent->address_cells);
  267. }
  268. grub_printf ("-------------------------------------------------\n");
  269. }
  270. static char *
  271. canonicalise_4cell_ua (grub_ieee1275_ihandle_t ihandle, char *unit_address)
  272. {
  273. grub_uint32_t phy_lo, phy_hi, lun_lo, lun_hi;
  274. int valid_phy = 0;
  275. grub_size_t size;
  276. char *canon = NULL;
  277. valid_phy = grub_ieee1275_decode_unit4 (ihandle, unit_address,
  278. grub_strlen (unit_address), &phy_lo,
  279. &phy_hi, &lun_lo, &lun_hi);
  280. if ((valid_phy == 0) && (phy_hi != 0xffffffff))
  281. canon = grub_ieee1275_encode_uint4 (ihandle, phy_lo, phy_hi,
  282. lun_lo, lun_hi, &size);
  283. return canon;
  284. }
  285. static char *
  286. canonicalise_disk (const char *devname)
  287. {
  288. char *canon, *parent;
  289. struct parent_dev *op;
  290. canon = grub_ieee1275_canonicalise_devname (devname);
  291. if (canon == NULL)
  292. {
  293. /* This should not happen. */
  294. grub_error (GRUB_ERR_BAD_DEVICE, "canonicalise devname failed");
  295. grub_print_error ();
  296. return NULL;
  297. }
  298. /* Don't try to open the parent of a virtual device. */
  299. if (grub_strstr (canon, "virtual-devices"))
  300. return canon;
  301. parent = get_parent_devname (canon);
  302. if (parent == NULL)
  303. return NULL;
  304. op = open_parent (parent);
  305. /*
  306. * Devices with 4 address cells can have many different types of addressing
  307. * (phy, wwn, and target lun). Use the parents encode-unit / decode-unit
  308. * to find the true canonical name.
  309. */
  310. if ((op) && (op->address_cells == 4))
  311. {
  312. char *unit_address, *real_unit_address, *real_canon;
  313. grub_size_t real_unit_str_len;
  314. unit_address = grub_strstr (canon, IEEE1275_DISK_ALIAS);
  315. unit_address += grub_strlen (IEEE1275_DISK_ALIAS);
  316. if (unit_address == NULL)
  317. {
  318. /*
  319. * This should not be possible, but return the canonical name for
  320. * the non-disk block device.
  321. */
  322. grub_free (parent);
  323. return (canon);
  324. }
  325. real_unit_address = canonicalise_4cell_ua (op->ihandle, unit_address);
  326. if (real_unit_address == NULL)
  327. {
  328. /*
  329. * This is not an error, since this function could be called with a devalias
  330. * containing a drive that isn't installed in the system.
  331. */
  332. grub_free (parent);
  333. return NULL;
  334. }
  335. real_unit_str_len = grub_strlen (op->name) + sizeof (IEEE1275_DISK_ALIAS)
  336. + grub_strlen (real_unit_address);
  337. if (grub_add (grub_strlen (op->name), sizeof (IEEE1275_DISK_ALIAS), &real_unit_str_len) ||
  338. grub_add (real_unit_str_len, grub_strlen (real_unit_address), &real_unit_str_len))
  339. {
  340. grub_free (parent);
  341. grub_error (GRUB_ERR_OUT_OF_RANGE, N_("overflow detected while obtaining size of canonical name"));
  342. grub_print_error ();
  343. return NULL;
  344. }
  345. real_canon = grub_malloc (real_unit_str_len);
  346. if (real_canon == NULL)
  347. {
  348. grub_free (parent);
  349. grub_print_error ();
  350. return NULL;
  351. }
  352. grub_snprintf (real_canon, real_unit_str_len, "%s/disk@%s",
  353. op->name, real_unit_address);
  354. grub_free (canon);
  355. canon = real_canon;
  356. }
  357. grub_free (parent);
  358. return (canon);
  359. }
  360. static struct disk_dev *
  361. add_canon_disk (const char *cname)
  362. {
  363. grub_size_t sz;
  364. struct disk_dev *dev;
  365. dev = grub_zalloc (sizeof (struct disk_dev));
  366. if (dev == NULL)
  367. goto failed;
  368. if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_RAW_DEVNAMES))
  369. {
  370. /*
  371. * Append :nolabel to the end of all SPARC disks.
  372. * nolabel is mutually exclusive with all other
  373. * arguments and allows a client program to open
  374. * the entire (raw) disk. Any disk label is ignored.
  375. */
  376. if (grub_add (grub_strlen (cname), sizeof (":nolabel"), &sz))
  377. {
  378. grub_error (GRUB_ERR_OUT_OF_RANGE, "overflow detected while appending :nolabel to end of canonical name");
  379. goto failed;
  380. }
  381. dev->raw_name = grub_malloc (sz);
  382. if (dev->raw_name == NULL)
  383. goto failed;
  384. grub_snprintf (dev->raw_name, sz, "%s:nolabel", cname);
  385. }
  386. /*
  387. * Don't use grub_ieee1275_encode_devname here, the devpath in grub.cfg doesn't
  388. * understand device aliases, which the layer above sometimes sends us.
  389. */
  390. dev->grub_devpath = encode_grub_devname(cname);
  391. if (dev->grub_devpath == NULL)
  392. goto failed;
  393. dev->name = grub_strdup (cname);
  394. if (dev->name == NULL)
  395. goto failed;
  396. dev->valid = 1;
  397. grub_list_push (GRUB_AS_LIST_P (&disk_devs), GRUB_AS_LIST (dev));
  398. return dev;
  399. failed:
  400. grub_print_error ();
  401. if (dev != NULL)
  402. {
  403. grub_free (dev->name);
  404. grub_free (dev->grub_devpath);
  405. grub_free (dev->raw_name);
  406. }
  407. grub_free (dev);
  408. return NULL;
  409. }
  410. static grub_err_t
  411. add_disk (const char *path)
  412. {
  413. grub_err_t ret = GRUB_ERR_NONE;
  414. struct disk_dev *dev;
  415. char *canon;
  416. canon = canonicalise_disk (path);
  417. dev = grub_named_list_find (GRUB_AS_NAMED_LIST (disk_devs), canon);
  418. if ((canon != NULL) && (dev == NULL))
  419. {
  420. struct disk_dev *ob_device;
  421. ob_device = add_canon_disk (canon);
  422. if (ob_device == NULL)
  423. ret = grub_error (GRUB_ERR_OUT_OF_MEMORY, "failure to add disk");
  424. }
  425. else if (dev != NULL)
  426. dev->valid = 1;
  427. grub_free (canon);
  428. return (ret);
  429. }
  430. static grub_err_t
  431. grub_obdisk_read (grub_disk_t disk, grub_disk_addr_t sector,
  432. grub_size_t size, char *dest)
  433. {
  434. grub_err_t ret = GRUB_ERR_NONE;
  435. struct disk_dev *dev;
  436. unsigned long long pos;
  437. grub_ssize_t result = 0;
  438. if (disk->data == NULL)
  439. return grub_error (GRUB_ERR_BAD_DEVICE, "invalid disk data");
  440. dev = (struct disk_dev *)disk->data;
  441. pos = sector << disk->log_sector_size;
  442. grub_ieee1275_seek (dev->ihandle, pos, &result);
  443. if (result < 0)
  444. {
  445. dev->opened = 0;
  446. return grub_error (GRUB_ERR_READ_ERROR, "seek error, can't seek block %llu",
  447. (long long) sector);
  448. }
  449. grub_ieee1275_read (dev->ihandle, dest, size << disk->log_sector_size,
  450. &result);
  451. if (result != (grub_ssize_t) (size << disk->log_sector_size))
  452. {
  453. dev->opened = 0;
  454. return grub_error (GRUB_ERR_READ_ERROR, N_("failure reading sector 0x%llx "
  455. "from `%s'"),
  456. (unsigned long long) sector,
  457. disk->name);
  458. }
  459. return ret;
  460. }
  461. static void
  462. grub_obdisk_close (grub_disk_t disk)
  463. {
  464. grub_memset (disk, 0, sizeof (*disk));
  465. }
  466. static void
  467. scan_usb_disk (const char *parent)
  468. {
  469. struct parent_dev *op;
  470. grub_ssize_t result;
  471. op = open_parent (parent);
  472. if (op == NULL)
  473. {
  474. grub_error (GRUB_ERR_BAD_DEVICE, "unable to open %s", parent);
  475. grub_print_error ();
  476. return;
  477. }
  478. if ((grub_ieee1275_set_address (op->ihandle, 0, 0) == 0) &&
  479. (grub_ieee1275_no_data_command (op->ihandle, &tur, &result) == 0) &&
  480. (result == 0))
  481. {
  482. char *buf;
  483. buf = grub_malloc (IEEE1275_MAX_PATH_LEN);
  484. if (buf == NULL)
  485. {
  486. grub_error (GRUB_ERR_OUT_OF_MEMORY, "disk scan failure");
  487. grub_print_error ();
  488. return;
  489. }
  490. grub_snprintf (buf, IEEE1275_MAX_PATH_LEN, "%s/disk@0", parent);
  491. add_disk (buf);
  492. grub_free (buf);
  493. }
  494. }
  495. static void
  496. scan_nvme_disk (const char *path)
  497. {
  498. char *buf;
  499. buf = grub_malloc (IEEE1275_MAX_PATH_LEN);
  500. if (buf == NULL)
  501. {
  502. grub_error (GRUB_ERR_OUT_OF_MEMORY, "disk scan failure");
  503. grub_print_error ();
  504. return;
  505. }
  506. grub_snprintf (buf, IEEE1275_MAX_PATH_LEN, "%s/disk@1", path);
  507. add_disk (buf);
  508. grub_free (buf);
  509. }
  510. static void
  511. scan_sparc_sas_2cell (struct parent_dev *op)
  512. {
  513. grub_ssize_t result;
  514. grub_uint8_t tgt;
  515. char *buf;
  516. buf = grub_malloc (IEEE1275_MAX_PATH_LEN);
  517. if (buf == NULL)
  518. {
  519. grub_error (GRUB_ERR_OUT_OF_MEMORY, "disk scan failure");
  520. grub_print_error ();
  521. return;
  522. }
  523. for (tgt = 0; tgt < 0xf; tgt++)
  524. {
  525. if ((grub_ieee1275_set_address(op->ihandle, tgt, 0) == 0) &&
  526. (grub_ieee1275_no_data_command (op->ihandle, &tur, &result) == 0) &&
  527. (result == 0))
  528. {
  529. grub_snprintf (buf, IEEE1275_MAX_PATH_LEN, "%s/disk@%"
  530. PRIxGRUB_UINT32_T, op->name, tgt);
  531. add_disk (buf);
  532. }
  533. }
  534. }
  535. static void
  536. scan_sparc_sas_4cell (struct parent_dev *op)
  537. {
  538. grub_uint16_t exp;
  539. grub_uint8_t phy;
  540. char *buf;
  541. buf = grub_malloc (IEEE1275_MAX_PATH_LEN);
  542. if (buf == NULL)
  543. {
  544. grub_error (GRUB_ERR_OUT_OF_MEMORY, "disk scan failure");
  545. grub_print_error ();
  546. return;
  547. }
  548. /*
  549. * Cycle thru the potential for dual ported SAS disks
  550. * behind a SAS expander.
  551. */
  552. for (exp = 0; exp <= 0x100; exp+=0x100)
  553. /* The current limit is 32 disks on a phy. */
  554. for (phy = 0; phy < 0x20; phy++)
  555. {
  556. char *canon = NULL;
  557. grub_snprintf (buf, IEEE1275_MAX_PATH_LEN, "p%" PRIxGRUB_UINT32_T ",0",
  558. exp | phy);
  559. canon = canonicalise_4cell_ua (op->ihandle, buf);
  560. if (canon != NULL)
  561. {
  562. grub_snprintf (buf, IEEE1275_MAX_PATH_LEN, "%s/disk@%s",
  563. op->name, canon);
  564. add_disk (buf);
  565. grub_free (canon);
  566. }
  567. }
  568. grub_free (buf);
  569. }
  570. static void
  571. scan_sparc_sas_disk (const char *parent)
  572. {
  573. struct parent_dev *op;
  574. op = open_parent (parent);
  575. if ((op != NULL) && (op->address_cells == 4))
  576. scan_sparc_sas_4cell (op);
  577. else if ((op != NULL) && (op->address_cells == 2))
  578. scan_sparc_sas_2cell (op);
  579. }
  580. static void
  581. iterate_devtree (const struct grub_ieee1275_devalias *alias)
  582. {
  583. struct grub_ieee1275_devalias child;
  584. if ((grub_strcmp (alias->type, "scsi-2") == 0) ||
  585. (grub_strcmp (alias->type, "scsi-sas") == 0))
  586. return scan_sparc_sas_disk (alias->path);
  587. else if (grub_strcmp (alias->type, "nvme") == 0)
  588. return scan_nvme_disk (alias->path);
  589. else if (grub_strcmp (alias->type, "scsi-usb") == 0)
  590. return scan_usb_disk (alias->path);
  591. else if (grub_strcmp (alias->type, "block") == 0)
  592. {
  593. const char **bl = block_blacklist;
  594. while (*bl != NULL)
  595. {
  596. if (grub_strstr (alias->path, *bl))
  597. return;
  598. bl++;
  599. }
  600. add_disk (alias->path);
  601. return;
  602. }
  603. FOR_IEEE1275_DEVCHILDREN (alias->path, child)
  604. iterate_devtree (&child);
  605. }
  606. static void
  607. enumerate_disks (void)
  608. {
  609. struct grub_ieee1275_devalias alias;
  610. FOR_IEEE1275_DEVCHILDREN("/", alias)
  611. iterate_devtree (&alias);
  612. }
  613. static grub_err_t
  614. add_bootpath (void)
  615. {
  616. struct disk_dev *ob_device;
  617. grub_err_t ret = GRUB_ERR_NONE;
  618. char *dev, *alias;
  619. char *type;
  620. dev = grub_ieee1275_get_boot_dev ();
  621. if (dev == NULL)
  622. return grub_error (GRUB_ERR_OUT_OF_MEMORY, "failure adding boot device");
  623. type = grub_ieee1275_get_device_type (dev);
  624. if (type == NULL)
  625. {
  626. grub_free (dev);
  627. return grub_error (GRUB_ERR_OUT_OF_MEMORY, "failure adding boot device");
  628. }
  629. alias = NULL;
  630. if (grub_strcmp (type, "network") != 0)
  631. {
  632. dev = strip_ob_partition (dev);
  633. ob_device = add_canon_disk (dev);
  634. if (ob_device == NULL)
  635. ret = grub_error (GRUB_ERR_OUT_OF_MEMORY, "failure adding boot device");
  636. ob_device->valid = 1;
  637. alias = grub_ieee1275_get_devname (dev);
  638. if (alias && grub_strcmp (alias, dev) != 0)
  639. ob_device->grub_alias_devpath = grub_ieee1275_encode_devname (dev);
  640. ob_device->boot_dev = 1;
  641. }
  642. grub_free (type);
  643. grub_free (dev);
  644. grub_free (alias);
  645. return ret;
  646. }
  647. static void
  648. enumerate_aliases (void)
  649. {
  650. struct grub_ieee1275_devalias alias;
  651. /*
  652. * Some block device aliases are not in canonical form
  653. *
  654. * For example:
  655. *
  656. * disk3 /pci@301/pci@1/scsi@0/disk@p3
  657. * disk2 /pci@301/pci@1/scsi@0/disk@p2
  658. * disk1 /pci@301/pci@1/scsi@0/disk@p1
  659. * disk /pci@301/pci@1/scsi@0/disk@p0
  660. * disk0 /pci@301/pci@1/scsi@0/disk@p0
  661. *
  662. * None of these devices are in canonical form.
  663. *
  664. * Also, just because there is a devalias, doesn't mean there is a disk
  665. * at that location. And a valid boot block device doesn't have to have
  666. * a devalias at all.
  667. *
  668. * At this point, all valid disks have been found in the system
  669. * and devaliases that point to canonical names are stored in the
  670. * disk_devs list already.
  671. */
  672. FOR_IEEE1275_DEVALIASES (alias)
  673. {
  674. struct disk_dev *dev;
  675. char *canon;
  676. if (grub_strcmp (alias.type, "block") != 0)
  677. continue;
  678. canon = canonicalise_disk (alias.name);
  679. if (canon == NULL)
  680. /* This is not an error, a devalias could point to a nonexistent disk. */
  681. continue;
  682. dev = grub_named_list_find (GRUB_AS_NAMED_LIST (disk_devs), canon);
  683. if (dev != NULL)
  684. {
  685. /*
  686. * If more than one alias points to the same device,
  687. * remove the previous one unless it is the boot dev,
  688. * since the upper level will use the first one. The reason
  689. * all the others are redone is in the case of hot-plugging
  690. * a disk. If the boot disk gets hot-plugged, it will come
  691. * thru here with a different name without the boot_dev flag
  692. * set.
  693. */
  694. if ((dev->boot_dev) && (dev->grub_alias_devpath))
  695. continue;
  696. grub_free (dev->grub_alias_devpath);
  697. dev->grub_alias_devpath = grub_ieee1275_encode_devname (alias.path);
  698. }
  699. grub_free (canon);
  700. }
  701. }
  702. static void
  703. display_disks (void)
  704. {
  705. struct disk_dev *dev;
  706. grub_printf ("--------------------- DISKS ---------------------\n");
  707. FOR_LIST_ELEMENTS (dev, disk_devs)
  708. {
  709. grub_printf ("name: %s\n", dev->name);
  710. grub_printf ("grub_devpath: %s\n", dev->grub_devpath);
  711. grub_printf ("grub_alias_devpath: %s\n", dev->grub_alias_devpath);
  712. grub_printf ("valid: %s\n", (dev->valid) ? "yes" : "no");
  713. grub_printf ("boot_dev: %s\n", (dev->boot_dev) ? "yes" : "no");
  714. grub_printf ("opened: %s\n", (dev->ihandle) ? "yes" : "no");
  715. grub_printf ("block size: %" PRIuGRUB_UINT32_T "\n",
  716. dev->block_size);
  717. grub_printf ("num blocks: %" PRIuGRUB_UINT64_T "\n",
  718. dev->num_blocks);
  719. grub_printf ("log sector size: %" PRIuGRUB_UINT32_T "\n",
  720. dev->log_sector_size);
  721. grub_printf ("\n");
  722. }
  723. grub_printf ("-------------------------------------------------\n");
  724. }
  725. static void
  726. display_stats (void)
  727. {
  728. const char *debug = grub_env_get ("debug");
  729. if (debug == NULL)
  730. return;
  731. if (grub_strword (debug, "all") || grub_strword (debug, "obdisk"))
  732. {
  733. display_parents ();
  734. display_disks ();
  735. }
  736. }
  737. static void
  738. invalidate_all_disks (void)
  739. {
  740. struct disk_dev *dev = NULL;
  741. if (disks_enumerated != 0)
  742. FOR_LIST_ELEMENTS (dev, disk_devs)
  743. dev->valid = 0;
  744. }
  745. static struct disk_dev *
  746. find_legacy_grub_devpath (const char *name)
  747. {
  748. struct disk_dev *dev = NULL;
  749. char *canon, *devpath = NULL;
  750. devpath = decode_grub_devname (name + sizeof ("ieee1275"));
  751. canon = canonicalise_disk (devpath);
  752. if (canon != NULL)
  753. dev = grub_named_list_find (GRUB_AS_NAMED_LIST (disk_devs), canon);
  754. grub_free (devpath);
  755. grub_free (canon);
  756. return dev;
  757. }
  758. static void
  759. enumerate_devices (void)
  760. {
  761. invalidate_all_disks ();
  762. enumerate_disks ();
  763. enumerate_aliases ();
  764. disks_enumerated = 1;
  765. display_stats ();
  766. }
  767. static struct disk_dev *
  768. find_grub_devpath_real (const char *name)
  769. {
  770. struct disk_dev *dev = NULL;
  771. FOR_LIST_ELEMENTS (dev, disk_devs)
  772. {
  773. if ((STRCMP (dev->grub_devpath, name))
  774. || (STRCMP (dev->grub_alias_devpath, name)))
  775. break;
  776. }
  777. return dev;
  778. }
  779. static struct disk_dev *
  780. find_grub_devpath (const char *name)
  781. {
  782. struct disk_dev *dev = NULL;
  783. int enumerated;
  784. do {
  785. enumerated = disks_enumerated;
  786. dev = find_grub_devpath_real (name);
  787. if (dev != NULL)
  788. break;
  789. dev = find_legacy_grub_devpath (name);
  790. if (dev != NULL)
  791. break;
  792. enumerate_devices ();
  793. } while (enumerated == 0);
  794. return dev;
  795. }
  796. static int
  797. grub_obdisk_iterate (grub_disk_dev_iterate_hook_t hook, void *hook_data,
  798. grub_disk_pull_t pull)
  799. {
  800. struct disk_dev *dev;
  801. const char *name;
  802. if (pull != GRUB_DISK_PULL_NONE)
  803. return 0;
  804. enumerate_devices ();
  805. FOR_LIST_ELEMENTS (dev, disk_devs)
  806. {
  807. if (dev->valid == 1)
  808. {
  809. if (dev->grub_alias_devpath)
  810. name = dev->grub_alias_devpath;
  811. else
  812. name = dev->grub_devpath;
  813. if (hook (name, hook_data))
  814. return 1;
  815. }
  816. }
  817. return 0;
  818. }
  819. static grub_err_t
  820. grub_obdisk_open (const char *name, grub_disk_t disk)
  821. {
  822. grub_ieee1275_ihandle_t ihandle = 0;
  823. struct disk_dev *dev = NULL;
  824. if (grub_strncmp (name, IEEE1275_DEV, sizeof (IEEE1275_DEV) - 1) != 0)
  825. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "not IEEE1275 device");
  826. dev = find_grub_devpath (name);
  827. if (dev == NULL)
  828. {
  829. grub_printf ("UNKNOWN DEVICE: %s\n", name);
  830. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "%s", name);
  831. }
  832. if (dev->opened == 0)
  833. {
  834. if (dev->raw_name != NULL)
  835. grub_ieee1275_open (dev->raw_name, &ihandle);
  836. else
  837. grub_ieee1275_open (dev->name, &ihandle);
  838. if (ihandle == 0)
  839. {
  840. grub_printf ("Can't open device %s\n", name);
  841. return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "can't open device %s", name);
  842. }
  843. dev->block_size = grub_ieee1275_get_block_size (ihandle);
  844. dev->num_blocks = grub_ieee1275_num_blocks (ihandle);
  845. if (dev->num_blocks == 0)
  846. dev->num_blocks = grub_ieee1275_num_blocks64 (ihandle);
  847. if (dev->num_blocks == 0)
  848. dev->num_blocks = GRUB_DISK_SIZE_UNKNOWN;
  849. if (dev->block_size != 0)
  850. dev->log_sector_size = grub_log2ull (dev->block_size);
  851. else
  852. dev->log_sector_size = 9;
  853. dev->ihandle = ihandle;
  854. dev->opened = 1;
  855. }
  856. disk->total_sectors = dev->num_blocks;
  857. disk->id = dev->ihandle;
  858. disk->data = dev;
  859. disk->log_sector_size = dev->log_sector_size;
  860. return GRUB_ERR_NONE;
  861. }
  862. static struct grub_disk_dev grub_obdisk_dev =
  863. {
  864. .name = "obdisk",
  865. .id = GRUB_DISK_DEVICE_OBDISK_ID,
  866. .disk_iterate = grub_obdisk_iterate,
  867. .disk_open = grub_obdisk_open,
  868. .disk_close = grub_obdisk_close,
  869. .disk_read = grub_obdisk_read,
  870. };
  871. void
  872. grub_obdisk_init (void)
  873. {
  874. grub_disk_firmware_fini = grub_obdisk_fini;
  875. add_bootpath ();
  876. grub_disk_dev_register (&grub_obdisk_dev);
  877. }
  878. void
  879. grub_obdisk_fini (void)
  880. {
  881. struct disk_dev *dev;
  882. FOR_LIST_ELEMENTS (dev, disk_devs)
  883. {
  884. if (dev->opened != 0)
  885. grub_ieee1275_close (dev->ihandle);
  886. }
  887. grub_disk_dev_unregister (&grub_obdisk_dev);
  888. }