testload.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* testload.c - load the same file in multiple ways */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2003,2005,2006,2007,2009,2010 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/mm.h>
  21. #include <grub/err.h>
  22. #include <grub/env.h>
  23. #include <grub/misc.h>
  24. #include <grub/file.h>
  25. #include <grub/disk.h>
  26. #include <grub/term.h>
  27. #include <grub/loader.h>
  28. #include <grub/command.h>
  29. #include <grub/i18n.h>
  30. GRUB_MOD_LICENSE ("GPLv3+");
  31. /* Helper for grub_cmd_testload. */
  32. static grub_err_t
  33. read_progress (grub_disk_addr_t sector __attribute__ ((unused)),
  34. unsigned offset __attribute__ ((unused)),
  35. unsigned len,
  36. char *buf __attribute__ ((unused)),
  37. void *data __attribute__ ((unused)))
  38. {
  39. for (; len >= GRUB_DISK_SECTOR_SIZE; len -= GRUB_DISK_SECTOR_SIZE)
  40. grub_xputs (".");
  41. if (len)
  42. grub_xputs (".");
  43. grub_refresh ();
  44. return GRUB_ERR_NONE;
  45. }
  46. static grub_err_t
  47. grub_cmd_testload (struct grub_command *cmd __attribute__ ((unused)),
  48. int argc, char *argv[])
  49. {
  50. grub_file_t file;
  51. char *buf;
  52. grub_size_t size;
  53. grub_off_t pos;
  54. if (argc < 1)
  55. return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
  56. file = grub_file_open (argv[0], GRUB_FILE_TYPE_TESTLOAD);
  57. if (! file)
  58. return grub_errno;
  59. size = grub_file_size (file) & ~(GRUB_DISK_SECTOR_SIZE - 1);
  60. if (size == 0)
  61. {
  62. grub_file_close (file);
  63. return GRUB_ERR_NONE;
  64. }
  65. buf = grub_malloc (size);
  66. if (! buf)
  67. goto fail;
  68. grub_printf ("Reading %s sequentially", argv[0]);
  69. file->read_hook = read_progress;
  70. if (grub_file_read (file, buf, size) != (grub_ssize_t) size)
  71. goto fail;
  72. grub_printf (" Done.\n");
  73. /* Read sequentially again. */
  74. grub_printf ("Reading %s sequentially again", argv[0]);
  75. grub_file_seek (file, 0);
  76. for (pos = 0; pos < size;)
  77. {
  78. char sector[GRUB_DISK_SECTOR_SIZE];
  79. grub_size_t curlen = GRUB_DISK_SECTOR_SIZE;
  80. if (curlen > size - pos)
  81. curlen = size - pos;
  82. if (grub_file_read (file, sector, curlen)
  83. != (grub_ssize_t) curlen)
  84. goto fail;
  85. if (grub_memcmp (sector, buf + pos, curlen) != 0)
  86. {
  87. grub_printf ("\nDiffers in %lld\n", (unsigned long long) pos);
  88. goto fail;
  89. }
  90. pos += curlen;
  91. }
  92. grub_printf (" Done.\n");
  93. /* Read backwards and compare. */
  94. grub_printf ("Reading %s backwards", argv[0]);
  95. pos = size;
  96. while (pos > 0)
  97. {
  98. char sector[GRUB_DISK_SECTOR_SIZE];
  99. if (pos >= GRUB_DISK_SECTOR_SIZE)
  100. pos -= GRUB_DISK_SECTOR_SIZE;
  101. else
  102. pos = 0;
  103. grub_file_seek (file, pos);
  104. if (grub_file_read (file, sector, GRUB_DISK_SECTOR_SIZE)
  105. != GRUB_DISK_SECTOR_SIZE)
  106. goto fail;
  107. if (grub_memcmp (sector, buf + pos, GRUB_DISK_SECTOR_SIZE) != 0)
  108. {
  109. int i;
  110. grub_printf ("\nDiffers in %lld\n", (unsigned long long) pos);
  111. for (i = 0; i < GRUB_DISK_SECTOR_SIZE; i++)
  112. {
  113. grub_printf ("%02x ", buf[pos + i]);
  114. if ((i & 15) == 15)
  115. grub_printf ("\n");
  116. }
  117. if (i)
  118. grub_refresh ();
  119. goto fail;
  120. }
  121. }
  122. grub_printf (" Done.\n");
  123. return GRUB_ERR_NONE;
  124. fail:
  125. grub_file_close (file);
  126. grub_free (buf);
  127. if (!grub_errno)
  128. grub_error (GRUB_ERR_IO, "bad read");
  129. return grub_errno;
  130. }
  131. static grub_command_t cmd;
  132. GRUB_MOD_INIT(testload)
  133. {
  134. cmd =
  135. grub_register_command ("testload", grub_cmd_testload,
  136. N_("FILE"),
  137. N_("Load the same file in multiple ways."));
  138. }
  139. GRUB_MOD_FINI(testload)
  140. {
  141. grub_unregister_command (cmd);
  142. }