partition.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. #include <grub/i18n.h>
  23. #ifdef GRUB_UTIL
  24. #include <grub/util/misc.h>
  25. #endif
  26. grub_partition_map_t grub_partition_map_list;
  27. /*
  28. * Checks that disk->partition contains part. This function assumes that the
  29. * start of part is relative to the start of disk->partition. Returns 1 if
  30. * disk->partition is null.
  31. */
  32. static int
  33. grub_partition_check_containment (const grub_disk_t disk,
  34. const grub_partition_t part)
  35. {
  36. if (disk->partition == NULL)
  37. return 1;
  38. if (part->start + part->len > disk->partition->len)
  39. {
  40. char *partname;
  41. partname = grub_partition_get_name (disk->partition);
  42. grub_dprintf ("partition", "sub-partition %s%d of (%s,%s) ends after parent.\n",
  43. part->partmap->name, part->number + 1, disk->name, partname);
  44. #ifdef GRUB_UTIL
  45. grub_util_warn (_("Discarding improperly nested partition (%s,%s,%s%d)"),
  46. disk->name, partname, part->partmap->name, part->number + 1);
  47. #endif
  48. grub_free (partname);
  49. return 0;
  50. }
  51. return 1;
  52. }
  53. /* Context for grub_partition_map_probe. */
  54. struct grub_partition_map_probe_ctx
  55. {
  56. int partnum;
  57. grub_partition_t p;
  58. };
  59. /* Helper for grub_partition_map_probe. */
  60. static int
  61. probe_iter (grub_disk_t dsk, const grub_partition_t partition, void *data)
  62. {
  63. struct grub_partition_map_probe_ctx *ctx = data;
  64. if (ctx->partnum != partition->number)
  65. return 0;
  66. if (!(grub_partition_check_containment (dsk, partition)))
  67. return 0;
  68. ctx->p = (grub_partition_t) grub_malloc (sizeof (*ctx->p));
  69. if (! ctx->p)
  70. return 1;
  71. grub_memcpy (ctx->p, partition, sizeof (*ctx->p));
  72. return 1;
  73. }
  74. static grub_partition_t
  75. grub_partition_map_probe (const grub_partition_map_t partmap,
  76. grub_disk_t disk, int partnum)
  77. {
  78. struct grub_partition_map_probe_ctx ctx = {
  79. .partnum = partnum,
  80. .p = 0
  81. };
  82. partmap->iterate (disk, probe_iter, &ctx);
  83. if (grub_errno)
  84. goto fail;
  85. return ctx.p;
  86. fail:
  87. grub_free (ctx.p);
  88. return 0;
  89. }
  90. grub_partition_t
  91. grub_partition_probe (struct grub_disk *disk, const char *str)
  92. {
  93. grub_partition_t part;
  94. grub_partition_t curpart = 0;
  95. grub_partition_t tail;
  96. const char *ptr;
  97. if (str == NULL)
  98. return 0;
  99. part = tail = disk->partition;
  100. for (ptr = str; *ptr;)
  101. {
  102. grub_partition_map_t partmap;
  103. int num;
  104. const char *partname, *partname_end;
  105. partname = ptr;
  106. while (*ptr && grub_isalpha (*ptr))
  107. ptr++;
  108. partname_end = ptr;
  109. num = grub_strtoul (ptr, &ptr, 0) - 1;
  110. curpart = 0;
  111. /* Use the first partition map type found. */
  112. FOR_PARTITION_MAPS(partmap)
  113. {
  114. if (partname_end != partname &&
  115. (grub_strncmp (partmap->name, partname, partname_end - partname)
  116. != 0 || partmap->name[partname_end - partname] != 0))
  117. continue;
  118. disk->partition = part;
  119. curpart = grub_partition_map_probe (partmap, disk, num);
  120. disk->partition = tail;
  121. if (curpart)
  122. break;
  123. if (grub_errno == GRUB_ERR_BAD_PART_TABLE)
  124. {
  125. /* Continue to next partition map type. */
  126. grub_errno = GRUB_ERR_NONE;
  127. continue;
  128. }
  129. break;
  130. }
  131. if (! curpart)
  132. {
  133. while (part)
  134. {
  135. curpart = part->parent;
  136. grub_free (part);
  137. part = curpart;
  138. }
  139. return 0;
  140. }
  141. curpart->parent = part;
  142. part = curpart;
  143. if (! ptr || *ptr != ',')
  144. break;
  145. ptr++;
  146. }
  147. return part;
  148. }
  149. /* Context for grub_partition_iterate. */
  150. struct grub_partition_iterate_ctx
  151. {
  152. int ret;
  153. grub_partition_iterate_hook_t hook;
  154. void *hook_data;
  155. };
  156. /* Helper for grub_partition_iterate. */
  157. static int
  158. part_iterate (grub_disk_t dsk, const grub_partition_t partition, void *data)
  159. {
  160. struct grub_partition_iterate_ctx *ctx = data;
  161. struct grub_partition p = *partition;
  162. if (!(grub_partition_check_containment (dsk, partition)))
  163. return 0;
  164. p.parent = dsk->partition;
  165. dsk->partition = 0;
  166. if (ctx->hook (dsk, &p, ctx->hook_data))
  167. {
  168. ctx->ret = 1;
  169. return 1;
  170. }
  171. if (p.start != 0)
  172. {
  173. const struct grub_partition_map *partmap;
  174. dsk->partition = &p;
  175. FOR_PARTITION_MAPS(partmap)
  176. {
  177. grub_err_t err;
  178. err = partmap->iterate (dsk, part_iterate, ctx);
  179. if (err)
  180. grub_errno = GRUB_ERR_NONE;
  181. if (ctx->ret)
  182. break;
  183. }
  184. }
  185. dsk->partition = p.parent;
  186. return ctx->ret;
  187. }
  188. int
  189. grub_partition_iterate (struct grub_disk *disk,
  190. grub_partition_iterate_hook_t hook, void *hook_data)
  191. {
  192. struct grub_partition_iterate_ctx ctx = {
  193. .ret = 0,
  194. .hook = hook,
  195. .hook_data = hook_data
  196. };
  197. const struct grub_partition_map *partmap;
  198. FOR_PARTITION_MAPS(partmap)
  199. {
  200. grub_err_t err;
  201. err = partmap->iterate (disk, part_iterate, &ctx);
  202. if (err)
  203. grub_errno = GRUB_ERR_NONE;
  204. if (ctx.ret)
  205. break;
  206. }
  207. return ctx.ret;
  208. }
  209. char *
  210. grub_partition_get_name (const grub_partition_t partition)
  211. {
  212. char *out = 0, *ptr;
  213. grub_size_t needlen = 0;
  214. grub_partition_t part;
  215. if (!partition)
  216. return grub_strdup ("");
  217. for (part = partition; part; part = part->parent)
  218. /* Even on 64-bit machines this buffer is enough to hold
  219. longest number. */
  220. needlen += grub_strlen (part->partmap->name) + 1 + 27;
  221. out = grub_malloc (needlen + 1);
  222. if (!out)
  223. return NULL;
  224. ptr = out + needlen;
  225. *ptr = 0;
  226. for (part = partition; part; part = part->parent)
  227. {
  228. char buf[27];
  229. grub_size_t len;
  230. grub_snprintf (buf, sizeof (buf), "%d", part->number + 1);
  231. len = grub_strlen (buf);
  232. ptr -= len;
  233. grub_memcpy (ptr, buf, len);
  234. len = grub_strlen (part->partmap->name);
  235. ptr -= len;
  236. grub_memcpy (ptr, part->partmap->name, len);
  237. *--ptr = ',';
  238. }
  239. grub_memmove (out, ptr + 1, out + needlen - ptr);
  240. return out;
  241. }