partition.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2004,2007 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/misc.h>
  19. #include <grub/mm.h>
  20. #include <grub/partition.h>
  21. #include <grub/disk.h>
  22. GRUB_EXPORT(grub_partition_map_list);
  23. GRUB_EXPORT(grub_partition_get_name);
  24. GRUB_EXPORT(grub_partition_iterate);
  25. GRUB_EXPORT(grub_partition_probe);
  26. grub_partition_map_t grub_partition_map_list;
  27. struct find_func_closure
  28. {
  29. int partnum;
  30. grub_partition_t p;
  31. };
  32. static int
  33. find_func (grub_disk_t d __attribute__ ((unused)),
  34. const grub_partition_t partition, void *closure)
  35. {
  36. struct find_func_closure *c = closure;
  37. if (c->partnum == partition->number)
  38. {
  39. c->p = (grub_partition_t) grub_malloc (sizeof (*c->p));
  40. if (! c->p)
  41. return 1;
  42. grub_memcpy (c->p, partition, sizeof (*c->p));
  43. return 1;
  44. }
  45. return 0;
  46. }
  47. static grub_partition_t
  48. grub_partition_map_probe (const grub_partition_map_t partmap,
  49. grub_disk_t disk, int partnum)
  50. {
  51. struct find_func_closure c;
  52. c.partnum = partnum;
  53. c.p = 0;
  54. partmap->iterate (disk, find_func, &c);
  55. if (grub_errno)
  56. goto fail;
  57. return c.p;
  58. fail:
  59. grub_free (c.p);
  60. return 0;
  61. }
  62. grub_partition_t
  63. grub_partition_probe (struct grub_disk *disk, const char *str)
  64. {
  65. grub_partition_t part = 0;
  66. grub_partition_t curpart = 0;
  67. grub_partition_t tail;
  68. const char *ptr;
  69. part = tail = disk->partition;
  70. for (ptr = str; *ptr;)
  71. {
  72. grub_partition_map_t partmap;
  73. int num;
  74. const char *partname, *partname_end;
  75. partname = ptr;
  76. while (*ptr && grub_isalpha (*ptr))
  77. ptr++;
  78. partname_end = ptr;
  79. num = grub_strtoul (ptr, (char **) &ptr, 0) - 1;
  80. curpart = 0;
  81. /* Use the first partition map type found. */
  82. FOR_PARTITION_MAPS(partmap)
  83. {
  84. if (partname_end != partname &&
  85. (grub_strncmp (partmap->name, partname, partname_end - partname)
  86. != 0 || partmap->name[partname_end - partname] != 0))
  87. continue;
  88. disk->partition = part;
  89. curpart = grub_partition_map_probe (partmap, disk, num);
  90. disk->partition = tail;
  91. if (curpart)
  92. break;
  93. if (grub_errno == GRUB_ERR_BAD_PART_TABLE)
  94. {
  95. /* Continue to next partition map type. */
  96. grub_errno = GRUB_ERR_NONE;
  97. continue;
  98. }
  99. break;
  100. }
  101. if (! curpart)
  102. {
  103. while (part)
  104. {
  105. curpart = part->parent;
  106. grub_free (part);
  107. part = curpart;
  108. }
  109. return 0;
  110. }
  111. curpart->parent = part;
  112. part = curpart;
  113. if (! ptr || *ptr != ',')
  114. break;
  115. ptr++;
  116. }
  117. return part;
  118. }
  119. struct part_iterate_closure
  120. {
  121. int (*hook) (grub_disk_t disk, const grub_partition_t partition,
  122. void *closure);
  123. void *closure;
  124. int ret;
  125. };
  126. static int
  127. part_iterate (grub_disk_t dsk,
  128. const grub_partition_t partition, void *closure)
  129. {
  130. struct part_iterate_closure *c = closure;
  131. struct grub_partition p = *partition;
  132. p.parent = dsk->partition;
  133. dsk->partition = 0;
  134. if (c->hook (dsk, &p, c->closure))
  135. {
  136. c->ret = 1;
  137. return 1;
  138. }
  139. if (p.start != 0)
  140. {
  141. const struct grub_partition_map *partmap;
  142. dsk->partition = &p;
  143. FOR_PARTITION_MAPS(partmap)
  144. {
  145. grub_err_t err;
  146. err = partmap->iterate (dsk, part_iterate, closure);
  147. if (err)
  148. grub_errno = GRUB_ERR_NONE;
  149. if (c->ret)
  150. break;
  151. }
  152. }
  153. dsk->partition = p.parent;
  154. return c->ret;
  155. }
  156. int
  157. grub_partition_iterate (struct grub_disk *disk,
  158. int (*hook) (grub_disk_t disk,
  159. const grub_partition_t partition,
  160. void *closure),
  161. void *closure)
  162. {
  163. struct part_iterate_closure c;
  164. const struct grub_partition_map *partmap;
  165. c.hook = hook;
  166. c.closure = closure;
  167. c.ret = 0;
  168. FOR_PARTITION_MAPS(partmap)
  169. {
  170. grub_err_t err;
  171. err = partmap->iterate (disk, part_iterate, &c);
  172. if (err)
  173. grub_errno = GRUB_ERR_NONE;
  174. if (c.ret)
  175. break;
  176. }
  177. return c.ret;
  178. }
  179. char *
  180. grub_partition_get_name (const grub_partition_t partition)
  181. {
  182. char *out = 0;
  183. int curlen = 0;
  184. grub_partition_t part;
  185. for (part = partition; part; part = part->parent)
  186. {
  187. /* Even on 64-bit machines this buffer is enough to hold
  188. longest number. */
  189. char buf[grub_strlen (part->partmap->name) + 25];
  190. int strl;
  191. grub_snprintf (buf, sizeof (buf), "%s%d", part->partmap->name,
  192. part->number + 1);
  193. strl = grub_strlen (buf);
  194. if (curlen)
  195. {
  196. out = grub_realloc (out, curlen + strl + 2);
  197. grub_memcpy (out + strl + 1, out, curlen);
  198. out[curlen + 1 + strl] = 0;
  199. grub_memcpy (out, buf, strl);
  200. out[strl] = ',';
  201. curlen = curlen + 1 + strl;
  202. }
  203. else
  204. {
  205. curlen = strl;
  206. out = grub_strdup (buf);
  207. }
  208. }
  209. return out;
  210. }