device.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* device.c - device manager */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2005,2007,2008,2009 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/device.h>
  20. #include <grub/disk.h>
  21. #include <grub/net.h>
  22. #include <grub/fs.h>
  23. #include <grub/mm.h>
  24. #include <grub/misc.h>
  25. #include <grub/env.h>
  26. #include <grub/partition.h>
  27. #include <grub/i18n.h>
  28. grub_net_t (*grub_net_open) (const char *name) = NULL;
  29. grub_device_t
  30. grub_device_open (const char *name)
  31. {
  32. grub_device_t dev = 0;
  33. if (! name)
  34. {
  35. name = grub_env_get ("root");
  36. if (name == NULL || *name == '\0')
  37. {
  38. grub_error (GRUB_ERR_BAD_DEVICE, N_("variable `%s' isn't set"), "root");
  39. goto fail;
  40. }
  41. }
  42. dev = grub_malloc (sizeof (*dev));
  43. if (! dev)
  44. goto fail;
  45. dev->net = NULL;
  46. /* Try to open a disk. */
  47. dev->disk = grub_disk_open (name);
  48. if (dev->disk)
  49. return dev;
  50. if (grub_net_open && grub_errno == GRUB_ERR_UNKNOWN_DEVICE)
  51. {
  52. grub_errno = GRUB_ERR_NONE;
  53. dev->net = grub_net_open (name);
  54. }
  55. if (dev->net)
  56. return dev;
  57. fail:
  58. grub_free (dev);
  59. return 0;
  60. }
  61. grub_err_t
  62. grub_device_close (grub_device_t device)
  63. {
  64. if (device == NULL)
  65. return GRUB_ERR_NONE;
  66. if (device->disk)
  67. grub_disk_close (device->disk);
  68. if (device->net)
  69. {
  70. grub_free (device->net->server);
  71. grub_free (device->net);
  72. }
  73. grub_free (device);
  74. return grub_errno;
  75. }
  76. struct part_ent
  77. {
  78. struct part_ent *next;
  79. char *name;
  80. };
  81. /* Context for grub_device_iterate. */
  82. struct grub_device_iterate_ctx
  83. {
  84. grub_device_iterate_hook_t hook;
  85. void *hook_data;
  86. struct part_ent *ents;
  87. };
  88. /* Helper for grub_device_iterate. */
  89. static int
  90. iterate_partition (grub_disk_t disk, const grub_partition_t partition,
  91. void *data)
  92. {
  93. struct grub_device_iterate_ctx *ctx = data;
  94. struct part_ent *p;
  95. char *part_name;
  96. p = grub_malloc (sizeof (*p));
  97. if (!p)
  98. {
  99. return 1;
  100. }
  101. part_name = grub_partition_get_name (partition);
  102. if (!part_name)
  103. {
  104. grub_free (p);
  105. return 1;
  106. }
  107. p->name = grub_xasprintf ("%s,%s", disk->name, part_name);
  108. grub_free (part_name);
  109. if (!p->name)
  110. {
  111. grub_free (p);
  112. return 1;
  113. }
  114. p->next = ctx->ents;
  115. ctx->ents = p;
  116. return 0;
  117. }
  118. /* Helper for grub_device_iterate. */
  119. static int
  120. iterate_disk (const char *disk_name, void *data)
  121. {
  122. struct grub_device_iterate_ctx *ctx = data;
  123. grub_device_t dev;
  124. if (ctx->hook (disk_name, ctx->hook_data))
  125. return 1;
  126. dev = grub_device_open (disk_name);
  127. if (! dev)
  128. {
  129. grub_errno = GRUB_ERR_NONE;
  130. return 0;
  131. }
  132. if (dev->disk)
  133. {
  134. struct part_ent *p;
  135. int ret = 0;
  136. ctx->ents = NULL;
  137. (void) grub_partition_iterate (dev->disk, iterate_partition, ctx);
  138. grub_device_close (dev);
  139. grub_errno = GRUB_ERR_NONE;
  140. p = ctx->ents;
  141. while (p != NULL)
  142. {
  143. struct part_ent *next = p->next;
  144. if (!ret)
  145. ret = ctx->hook (p->name, ctx->hook_data);
  146. grub_free (p->name);
  147. grub_free (p);
  148. p = next;
  149. }
  150. return ret;
  151. }
  152. grub_device_close (dev);
  153. return 0;
  154. }
  155. int
  156. grub_device_iterate (grub_device_iterate_hook_t hook, void *hook_data)
  157. {
  158. struct grub_device_iterate_ctx ctx = { hook, hook_data, NULL };
  159. /* Only disk devices are supported at the moment. */
  160. return grub_disk_dev_iterate (iterate_disk, &ctx);
  161. }