blocklist.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /* blocklist.c - print the block list of a file */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2006,2007 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 <grub/dl.h>
  20. #include <grub/misc.h>
  21. #include <grub/file.h>
  22. #include <grub/mm.h>
  23. #include <grub/disk.h>
  24. #include <grub/partition.h>
  25. #include <grub/command.h>
  26. #include <grub/i18n.h>
  27. GRUB_MOD_LICENSE ("GPLv3+");
  28. /* Context for grub_cmd_blocklist. */
  29. struct blocklist_ctx
  30. {
  31. unsigned long start_sector;
  32. unsigned num_sectors;
  33. int num_entries;
  34. grub_disk_addr_t part_start;
  35. };
  36. /* Helper for grub_cmd_blocklist. */
  37. static void
  38. print_blocklist (grub_disk_addr_t sector, unsigned num,
  39. unsigned offset, unsigned length, struct blocklist_ctx *ctx)
  40. {
  41. if (ctx->num_entries++)
  42. grub_printf (",");
  43. grub_printf ("%llu", (unsigned long long) (sector - ctx->part_start));
  44. if (num > 0)
  45. grub_printf ("+%u", num);
  46. if (offset != 0 || length != 0)
  47. grub_printf ("[%u-%u]", offset, offset + length);
  48. }
  49. /* Helper for grub_cmd_blocklist. */
  50. static grub_err_t
  51. read_blocklist (grub_disk_addr_t sector, unsigned offset, unsigned length,
  52. char *buf __attribute__ ((unused)), void *data)
  53. {
  54. struct blocklist_ctx *ctx = data;
  55. if (ctx->num_sectors > 0)
  56. {
  57. if (ctx->start_sector + ctx->num_sectors == sector
  58. && offset == 0 && length >= GRUB_DISK_SECTOR_SIZE)
  59. {
  60. ctx->num_sectors += length >> GRUB_DISK_SECTOR_BITS;
  61. sector += length >> GRUB_DISK_SECTOR_BITS;
  62. length &= (GRUB_DISK_SECTOR_SIZE - 1);
  63. }
  64. if (!length)
  65. return GRUB_ERR_NONE;
  66. print_blocklist (ctx->start_sector, ctx->num_sectors, 0, 0, ctx);
  67. ctx->num_sectors = 0;
  68. }
  69. if (offset)
  70. {
  71. unsigned l = length + offset;
  72. l &= (GRUB_DISK_SECTOR_SIZE - 1);
  73. l -= offset;
  74. print_blocklist (sector, 0, offset, l, ctx);
  75. length -= l;
  76. sector++;
  77. offset = 0;
  78. }
  79. if (!length)
  80. return GRUB_ERR_NONE;
  81. if (length & (GRUB_DISK_SECTOR_SIZE - 1))
  82. {
  83. if (length >> GRUB_DISK_SECTOR_BITS)
  84. {
  85. print_blocklist (sector, length >> GRUB_DISK_SECTOR_BITS, 0, 0, ctx);
  86. sector += length >> GRUB_DISK_SECTOR_BITS;
  87. }
  88. print_blocklist (sector, 0, 0, length & (GRUB_DISK_SECTOR_SIZE - 1), ctx);
  89. }
  90. else
  91. {
  92. ctx->start_sector = sector;
  93. ctx->num_sectors = length >> GRUB_DISK_SECTOR_BITS;
  94. }
  95. return GRUB_ERR_NONE;
  96. }
  97. static grub_err_t
  98. grub_cmd_blocklist (grub_command_t cmd __attribute__ ((unused)),
  99. int argc, char **args)
  100. {
  101. grub_file_t file;
  102. char buf[GRUB_DISK_SECTOR_SIZE];
  103. struct blocklist_ctx ctx = {
  104. .start_sector = 0,
  105. .num_sectors = 0,
  106. .num_entries = 0,
  107. .part_start = 0
  108. };
  109. if (argc < 1)
  110. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  111. file = grub_file_open (args[0], GRUB_FILE_TYPE_PRINT_BLOCKLIST
  112. | GRUB_FILE_TYPE_NO_DECOMPRESS);
  113. if (! file)
  114. return grub_errno;
  115. if (! file->device->disk)
  116. return grub_error (GRUB_ERR_BAD_DEVICE,
  117. "this command is available only for disk devices");
  118. ctx.part_start = grub_partition_get_start (file->device->disk->partition);
  119. file->read_hook = read_blocklist;
  120. file->read_hook_data = &ctx;
  121. while (grub_file_read (file, buf, sizeof (buf)) > 0)
  122. ;
  123. if (ctx.num_sectors > 0)
  124. print_blocklist (ctx.start_sector, ctx.num_sectors, 0, 0, &ctx);
  125. grub_file_close (file);
  126. return grub_errno;
  127. }
  128. static grub_command_t cmd;
  129. GRUB_MOD_INIT(blocklist)
  130. {
  131. cmd = grub_register_command ("blocklist", grub_cmd_blocklist,
  132. N_("FILE"), N_("Print a block list."));
  133. }
  134. GRUB_MOD_FINI(blocklist)
  135. {
  136. grub_unregister_command (cmd);
  137. }