partition.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 = 0;
  94. grub_partition_t curpart = 0;
  95. grub_partition_t tail;
  96. const char *ptr;
  97. part = tail = disk->partition;
  98. for (ptr = str; *ptr;)
  99. {
  100. grub_partition_map_t partmap;
  101. int num;
  102. const char *partname, *partname_end;
  103. partname = ptr;
  104. while (*ptr && grub_isalpha (*ptr))
  105. ptr++;
  106. partname_end = ptr;
  107. num = grub_strtoul (ptr, (char **) &ptr, 0) - 1;
  108. curpart = 0;
  109. /* Use the first partition map type found. */
  110. FOR_PARTITION_MAPS(partmap)
  111. {
  112. if (partname_end != partname &&
  113. (grub_strncmp (partmap->name, partname, partname_end - partname)
  114. != 0 || partmap->name[partname_end - partname] != 0))
  115. continue;
  116. disk->partition = part;
  117. curpart = grub_partition_map_probe (partmap, disk, num);
  118. disk->partition = tail;
  119. if (curpart)
  120. break;
  121. if (grub_errno == GRUB_ERR_BAD_PART_TABLE)
  122. {
  123. /* Continue to next partition map type. */
  124. grub_errno = GRUB_ERR_NONE;
  125. continue;
  126. }
  127. break;
  128. }
  129. if (! curpart)
  130. {
  131. while (part)
  132. {
  133. curpart = part->parent;
  134. grub_free (part);
  135. part = curpart;
  136. }
  137. return 0;
  138. }
  139. curpart->parent = part;
  140. part = curpart;
  141. if (! ptr || *ptr != ',')
  142. break;
  143. ptr++;
  144. }
  145. return part;
  146. }
  147. /* Context for grub_partition_iterate. */
  148. struct grub_partition_iterate_ctx
  149. {
  150. int ret;
  151. grub_partition_iterate_hook_t hook;
  152. void *hook_data;
  153. };
  154. /* Helper for grub_partition_iterate. */
  155. static int
  156. part_iterate (grub_disk_t dsk, const grub_partition_t partition, void *data)
  157. {
  158. struct grub_partition_iterate_ctx *ctx = data;
  159. struct grub_partition p = *partition;
  160. if (!(grub_partition_check_containment (dsk, partition)))
  161. return 0;
  162. p.parent = dsk->partition;
  163. dsk->partition = 0;
  164. if (ctx->hook (dsk, &p, ctx->hook_data))
  165. {
  166. ctx->ret = 1;
  167. return 1;
  168. }
  169. if (p.start != 0)
  170. {
  171. const struct grub_partition_map *partmap;
  172. dsk->partition = &p;
  173. FOR_PARTITION_MAPS(partmap)
  174. {
  175. grub_err_t err;
  176. err = partmap->iterate (dsk, part_iterate, ctx);
  177. if (err)
  178. grub_errno = GRUB_ERR_NONE;
  179. if (ctx->ret)
  180. break;
  181. }
  182. }
  183. dsk->partition = p.parent;
  184. return ctx->ret;
  185. }
  186. int
  187. grub_partition_iterate (struct grub_disk *disk,
  188. grub_partition_iterate_hook_t hook, void *hook_data)
  189. {
  190. struct grub_partition_iterate_ctx ctx = {
  191. .ret = 0,
  192. .hook = hook,
  193. .hook_data = hook_data
  194. };
  195. const struct grub_partition_map *partmap;
  196. FOR_PARTITION_MAPS(partmap)
  197. {
  198. grub_err_t err;
  199. err = partmap->iterate (disk, part_iterate, &ctx);
  200. if (err)
  201. grub_errno = GRUB_ERR_NONE;
  202. if (ctx.ret)
  203. break;
  204. }
  205. return ctx.ret;
  206. }
  207. char *
  208. grub_partition_get_name (const grub_partition_t partition)
  209. {
  210. char *out = 0, *ptr;
  211. grub_size_t needlen = 0;
  212. grub_partition_t part;
  213. if (!partition)
  214. return grub_strdup ("");
  215. for (part = partition; part; part = part->parent)
  216. /* Even on 64-bit machines this buffer is enough to hold
  217. longest number. */
  218. needlen += grub_strlen (part->partmap->name) + 1 + 27;
  219. out = grub_malloc (needlen + 1);
  220. if (!out)
  221. return NULL;
  222. ptr = out + needlen;
  223. *ptr = 0;
  224. for (part = partition; part; part = part->parent)
  225. {
  226. char buf[27];
  227. grub_size_t len;
  228. grub_snprintf (buf, sizeof (buf), "%d", part->number + 1);
  229. len = grub_strlen (buf);
  230. ptr -= len;
  231. grub_memcpy (ptr, buf, len);
  232. len = grub_strlen (part->partmap->name);
  233. ptr -= len;
  234. grub_memcpy (ptr, part->partmap->name, len);
  235. *--ptr = ',';
  236. }
  237. grub_memmove (out, ptr + 1, out + needlen - ptr);
  238. return out;
  239. }