blocklist.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* grub-setup.c - make GRUB usable */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011 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 <config.h>
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <stdlib.h>
  24. #include <sys/types.h>
  25. #include <sys/stat.h>
  26. #include <dirent.h>
  27. #include <assert.h>
  28. #include <sys/ioctl.h>
  29. #include <linux/types.h>
  30. #include <linux/fs.h>
  31. #include <linux/fiemap.h>
  32. #include <grub/disk.h>
  33. #include <grub/partition.h>
  34. #include <grub/util/misc.h>
  35. #include <grub/util/install.h>
  36. #include <errno.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include <fcntl.h>
  40. void
  41. grub_install_get_blocklist (grub_device_t root_dev,
  42. const char *core_path,
  43. const char *core_img __attribute__ ((unused)),
  44. size_t core_size,
  45. void (*callback) (grub_disk_addr_t sector,
  46. unsigned offset,
  47. unsigned length,
  48. void *data),
  49. void *hook_data)
  50. {
  51. grub_partition_t container = root_dev->disk->partition;
  52. grub_uint64_t container_start = grub_partition_get_start (container);
  53. struct fiemap fie1;
  54. int fd;
  55. /* Write the first two sectors of the core image onto the disk. */
  56. grub_util_info ("opening the core image `%s'", core_path);
  57. fd = open (core_path, O_RDONLY);
  58. if (fd < 0)
  59. grub_util_error (_("cannot open `%s': %s"), core_path,
  60. strerror (errno));
  61. grub_memset (&fie1, 0, sizeof (fie1));
  62. fie1.fm_length = core_size;
  63. fie1.fm_flags = FIEMAP_FLAG_SYNC;
  64. if (ioctl (fd, FS_IOC_FIEMAP, &fie1) < 0)
  65. {
  66. int nblocks, i;
  67. int bsize;
  68. int mul;
  69. grub_util_info ("FIEMAP failed. Reverting to FIBMAP");
  70. if (ioctl (fd, FIGETBSZ, &bsize) < 0)
  71. grub_util_error (_("can't retrieve blocklists: %s"),
  72. strerror (errno));
  73. if (bsize & (GRUB_DISK_SECTOR_SIZE - 1))
  74. grub_util_error ("%s", _("blocksize is not divisible by 512"));
  75. if (!bsize)
  76. grub_util_error ("%s", _("invalid zero blocksize"));
  77. mul = bsize >> GRUB_DISK_SECTOR_BITS;
  78. nblocks = (core_size + bsize - 1) / bsize;
  79. if (mul == 0 || nblocks == 0)
  80. grub_util_error ("%s", _("can't retrieve blocklists"));
  81. for (i = 0; i < nblocks; i++)
  82. {
  83. unsigned blk = i;
  84. int rest;
  85. if (ioctl (fd, FIBMAP, &blk) < 0)
  86. grub_util_error (_("can't retrieve blocklists: %s"),
  87. strerror (errno));
  88. rest = core_size - ((i * mul) << GRUB_DISK_SECTOR_BITS);
  89. if (rest <= 0)
  90. break;
  91. if (rest > GRUB_DISK_SECTOR_SIZE * mul)
  92. rest = GRUB_DISK_SECTOR_SIZE * mul;
  93. callback (((grub_uint64_t) blk) * mul
  94. + container_start,
  95. 0, rest, hook_data);
  96. }
  97. }
  98. else
  99. {
  100. struct fiemap *fie2;
  101. int i;
  102. fie2 = xmalloc (sizeof (*fie2)
  103. + fie1.fm_mapped_extents
  104. * sizeof (fie1.fm_extents[1]));
  105. memset (fie2, 0, sizeof (*fie2)
  106. + fie1.fm_mapped_extents * sizeof (fie2->fm_extents[1]));
  107. fie2->fm_length = core_size;
  108. fie2->fm_flags = FIEMAP_FLAG_SYNC;
  109. fie2->fm_extent_count = fie1.fm_mapped_extents;
  110. if (ioctl (fd, FS_IOC_FIEMAP, fie2) < 0)
  111. grub_util_error (_("can't retrieve blocklists: %s"),
  112. strerror (errno));
  113. for (i = 0; i < fie2->fm_mapped_extents; i++)
  114. {
  115. callback ((fie2->fm_extents[i].fe_physical
  116. >> GRUB_DISK_SECTOR_BITS)
  117. + container_start,
  118. fie2->fm_extents[i].fe_physical
  119. & (GRUB_DISK_SECTOR_SIZE - 1),
  120. fie2->fm_extents[i].fe_length, hook_data);
  121. }
  122. free (fie2);
  123. }
  124. close (fd);
  125. }