plan.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010 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/partition.h>
  19. #include <grub/disk.h>
  20. #include <grub/mm.h>
  21. #include <grub/misc.h>
  22. #include <grub/dl.h>
  23. #include <grub/symbol.h>
  24. #include <grub/types.h>
  25. #include <grub/err.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. static struct grub_partition_map grub_plan_partition_map;
  28. static grub_err_t
  29. plan_partition_map_iterate (grub_disk_t disk,
  30. grub_partition_iterate_hook_t hook,
  31. void *hook_data)
  32. {
  33. struct grub_partition p;
  34. int ptr = 0;
  35. grub_err_t err;
  36. p.partmap = &grub_plan_partition_map;
  37. p.msdostype = 0;
  38. for (p.number = 0; ; p.number++)
  39. {
  40. char sig[sizeof ("part ") - 1];
  41. char c;
  42. p.offset = (ptr >> GRUB_DISK_SECTOR_BITS) + 1;
  43. p.index = ptr & (GRUB_DISK_SECTOR_SIZE - 1);
  44. err = grub_disk_read (disk, 1, ptr, sizeof (sig), sig);
  45. if (err)
  46. return err;
  47. if (grub_memcmp (sig, "part ", sizeof ("part ") - 1) != 0)
  48. break;
  49. ptr += sizeof (sig);
  50. do
  51. {
  52. err = grub_disk_read (disk, 1, ptr, 1, &c);
  53. if (err)
  54. return err;
  55. ptr++;
  56. }
  57. while (grub_isdigit (c) || grub_isalpha (c));
  58. if (c != ' ')
  59. break;
  60. p.start = 0;
  61. while (1)
  62. {
  63. err = grub_disk_read (disk, 1, ptr, 1, &c);
  64. if (err)
  65. return err;
  66. ptr++;
  67. if (!grub_isdigit (c))
  68. break;
  69. p.start = p.start * 10 + (c - '0');
  70. }
  71. if (c != ' ')
  72. break;
  73. p.len = 0;
  74. while (1)
  75. {
  76. err = grub_disk_read (disk, 1, ptr, 1, &c);
  77. if (err)
  78. return err;
  79. ptr++;
  80. if (!grub_isdigit (c))
  81. break;
  82. p.len = p.len * 10 + (c - '0');
  83. }
  84. if (c != '\n')
  85. break;
  86. p.len -= p.start;
  87. if (hook (disk, &p, hook_data))
  88. return grub_errno;
  89. }
  90. if (p.number == 0)
  91. return grub_error (GRUB_ERR_BAD_PART_TABLE, "not a plan partition table");
  92. return GRUB_ERR_NONE;
  93. }
  94. /* Partition map type. */
  95. static struct grub_partition_map grub_plan_partition_map =
  96. {
  97. .name = "plan",
  98. .iterate = plan_partition_map_iterate,
  99. };
  100. GRUB_MOD_INIT(part_plan)
  101. {
  102. grub_partition_map_register (&grub_plan_partition_map);
  103. }
  104. GRUB_MOD_FINI(part_plan)
  105. {
  106. grub_partition_map_unregister (&grub_plan_partition_map);
  107. }