device.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. GRUB_EXPORT(grub_device_open);
  28. GRUB_EXPORT(grub_device_close);
  29. GRUB_EXPORT(grub_device_iterate);
  30. grub_device_t
  31. grub_device_open (const char *name)
  32. {
  33. grub_disk_t disk = 0;
  34. grub_device_t dev = 0;
  35. if (! name)
  36. {
  37. name = grub_env_get ("root");
  38. if (*name == '\0')
  39. {
  40. grub_error (GRUB_ERR_BAD_DEVICE, "no device is set");
  41. goto fail;
  42. }
  43. }
  44. dev = grub_malloc (sizeof (*dev));
  45. if (! dev)
  46. goto fail;
  47. /* Try to open a disk. */
  48. disk = grub_disk_open (name);
  49. if (! disk)
  50. goto fail;
  51. dev->disk = disk;
  52. dev->net = 0; /* FIXME */
  53. return dev;
  54. fail:
  55. if (disk)
  56. grub_disk_close (disk);
  57. grub_free (dev);
  58. return 0;
  59. }
  60. grub_err_t
  61. grub_device_close (grub_device_t device)
  62. {
  63. if (device->disk)
  64. grub_disk_close (device->disk);
  65. grub_free (device);
  66. return grub_errno;
  67. }
  68. struct grub_device_iterate_closure
  69. {
  70. int (*hook) (const char *name, void *closure);
  71. void *closure;
  72. struct part_ent
  73. {
  74. struct part_ent *next;
  75. char *name;
  76. } *ents;
  77. };
  78. static int
  79. iterate_partition (grub_disk_t disk, const grub_partition_t partition,
  80. void *closure)
  81. {
  82. struct grub_device_iterate_closure *c = closure;
  83. char *partition_name;
  84. struct part_ent *p;
  85. partition_name = grub_partition_get_name (partition);
  86. if (! partition_name)
  87. return 1;
  88. p = grub_malloc (sizeof (*p));
  89. if (!p)
  90. {
  91. grub_free (partition_name);
  92. return 1;
  93. }
  94. p->name = grub_xasprintf ("%s,%s", disk->name, partition_name);
  95. if (!p->name)
  96. {
  97. grub_free (partition_name);
  98. grub_free (p);
  99. return 1;
  100. }
  101. grub_free (partition_name);
  102. p->next = c->ents;
  103. c->ents = p;
  104. return 0;
  105. }
  106. static int
  107. iterate_disk (const char *disk_name, void *closure)
  108. {
  109. struct grub_device_iterate_closure *c = closure;
  110. grub_device_t dev;
  111. if (c->hook (disk_name, c->closure))
  112. return 1;
  113. dev = grub_device_open (disk_name);
  114. if (! dev)
  115. {
  116. grub_errno = GRUB_ERR_NONE;
  117. return 0;
  118. }
  119. if (dev->disk && dev->disk->has_partitions)
  120. {
  121. struct part_ent *p;
  122. int ret = 0;
  123. c->ents = NULL;
  124. (void) grub_partition_iterate (dev->disk, iterate_partition, c);
  125. grub_device_close (dev);
  126. grub_errno = GRUB_ERR_NONE;
  127. p = c->ents;
  128. while (p != NULL)
  129. {
  130. struct part_ent *next = p->next;
  131. if (!ret)
  132. ret = c->hook (p->name, c->closure);
  133. grub_free (p->name);
  134. grub_free (p);
  135. p = next;
  136. }
  137. return ret;
  138. }
  139. grub_device_close (dev);
  140. return 0;
  141. }
  142. int
  143. grub_device_iterate (int (*hook) (const char *name, void *closure),
  144. void *closure)
  145. {
  146. struct grub_device_iterate_closure c;
  147. c.hook = hook;
  148. c.closure = closure;
  149. /* Only disk devices are supported at the moment. */
  150. return grub_disk_dev_iterate (iterate_disk, &c);
  151. }