blocklist.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. struct grub_cmd_blocklist_closure
  28. {
  29. unsigned long start_sector;
  30. unsigned num_sectors;
  31. int num_entries;
  32. grub_disk_addr_t part_start;
  33. };
  34. static void
  35. print_blocklist (grub_disk_addr_t sector, unsigned num,
  36. unsigned offset, unsigned length,
  37. struct grub_cmd_blocklist_closure *c)
  38. {
  39. if (c->num_entries++)
  40. grub_printf (",");
  41. grub_printf ("%llu", (unsigned long long) (sector - c->part_start));
  42. if (num > 0)
  43. grub_printf ("+%u", num);
  44. if (offset != 0 || length != 0)
  45. grub_printf ("[%u-%u]", offset, offset + length);
  46. }
  47. static void
  48. read_blocklist (grub_disk_addr_t sector, unsigned offset,
  49. unsigned length, void *closure)
  50. {
  51. struct grub_cmd_blocklist_closure *c = closure;
  52. if (c->num_sectors > 0)
  53. {
  54. if (c->start_sector + c->num_sectors == sector
  55. && offset == 0 && length == GRUB_DISK_SECTOR_SIZE)
  56. {
  57. c->num_sectors++;
  58. return;
  59. }
  60. print_blocklist (c->start_sector, c->num_sectors, 0, 0, c);
  61. c->num_sectors = 0;
  62. }
  63. if (offset == 0 && length == GRUB_DISK_SECTOR_SIZE)
  64. {
  65. c->start_sector = sector;
  66. c->num_sectors++;
  67. }
  68. else
  69. print_blocklist (sector, 0, offset, length, c);
  70. }
  71. static grub_err_t
  72. grub_cmd_blocklist (grub_command_t cmd __attribute__ ((unused)),
  73. int argc, char **args)
  74. {
  75. grub_file_t file;
  76. struct grub_cmd_blocklist_closure c;
  77. c.start_sector = 0;
  78. c.num_sectors = 0;
  79. c.num_entries = 0;
  80. c.part_start = 0;
  81. if (argc < 1)
  82. return grub_error (GRUB_ERR_BAD_ARGUMENT, "no file specified");
  83. file = grub_file_open (args[0]);
  84. if (! file)
  85. return grub_errno;
  86. file->flags = 1;
  87. if (! file->device->disk)
  88. return grub_error (GRUB_ERR_BAD_DEVICE,
  89. "this command is available only for disk devices");
  90. c.part_start = grub_partition_get_start (file->device->disk->partition);
  91. file->read_hook = read_blocklist;
  92. file->closure = &c;
  93. grub_file_read (file, 0, file->size);
  94. if (c.num_sectors > 0)
  95. print_blocklist (c.start_sector, c.num_sectors, 0, 0, &c);
  96. grub_file_close (file);
  97. return grub_errno;
  98. }
  99. static grub_command_t cmd;
  100. GRUB_MOD_INIT(blocklist)
  101. {
  102. cmd = grub_register_command ("blocklist", grub_cmd_blocklist,
  103. N_("FILE"), N_("Print a block list."));
  104. }
  105. GRUB_MOD_FINI(blocklist)
  106. {
  107. grub_unregister_command (cmd);
  108. }