device.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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->disk)
  65. grub_disk_close (device->disk);
  66. if (device->net)
  67. {
  68. grub_free (device->net->server);
  69. grub_free (device->net);
  70. }
  71. grub_free (device);
  72. return grub_errno;
  73. }
  74. struct part_ent
  75. {
  76. struct part_ent *next;
  77. char *name;
  78. };
  79. /* Context for grub_device_iterate. */
  80. struct grub_device_iterate_ctx
  81. {
  82. grub_device_iterate_hook_t hook;
  83. void *hook_data;
  84. struct part_ent *ents;
  85. };
  86. /* Helper for grub_device_iterate. */
  87. static int
  88. iterate_partition (grub_disk_t disk, const grub_partition_t partition,
  89. void *data)
  90. {
  91. struct grub_device_iterate_ctx *ctx = data;
  92. struct part_ent *p;
  93. char *part_name;
  94. p = grub_malloc (sizeof (*p));
  95. if (!p)
  96. {
  97. return 1;
  98. }
  99. part_name = grub_partition_get_name (partition);
  100. if (!part_name)
  101. {
  102. grub_free (p);
  103. return 1;
  104. }
  105. p->name = grub_xasprintf ("%s,%s", disk->name, part_name);
  106. grub_free (part_name);
  107. if (!p->name)
  108. {
  109. grub_free (p);
  110. return 1;
  111. }
  112. p->next = ctx->ents;
  113. ctx->ents = p;
  114. return 0;
  115. }
  116. /* Helper for grub_device_iterate. */
  117. static int
  118. iterate_disk (const char *disk_name, void *data)
  119. {
  120. struct grub_device_iterate_ctx *ctx = data;
  121. grub_device_t dev;
  122. if (ctx->hook (disk_name, ctx->hook_data))
  123. return 1;
  124. dev = grub_device_open (disk_name);
  125. if (! dev)
  126. {
  127. grub_errno = GRUB_ERR_NONE;
  128. return 0;
  129. }
  130. if (dev->disk)
  131. {
  132. struct part_ent *p;
  133. int ret = 0;
  134. ctx->ents = NULL;
  135. (void) grub_partition_iterate (dev->disk, iterate_partition, ctx);
  136. grub_device_close (dev);
  137. grub_errno = GRUB_ERR_NONE;
  138. p = ctx->ents;
  139. while (p != NULL)
  140. {
  141. struct part_ent *next = p->next;
  142. if (!ret)
  143. ret = ctx->hook (p->name, ctx->hook_data);
  144. grub_free (p->name);
  145. grub_free (p);
  146. p = next;
  147. }
  148. return ret;
  149. }
  150. grub_device_close (dev);
  151. return 0;
  152. }
  153. int
  154. grub_device_iterate (grub_device_iterate_hook_t hook, void *hook_data)
  155. {
  156. struct grub_device_iterate_ctx ctx = { hook, hook_data, NULL };
  157. /* Only disk devices are supported at the moment. */
  158. return grub_disk_dev_iterate (iterate_disk, &ctx);
  159. }