progress.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* progress.c - show loading progress */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2013 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/types.h>
  20. #include <grub/time.h>
  21. #include <grub/term.h>
  22. #include <grub/dl.h>
  23. #include <grub/misc.h>
  24. #include <grub/normal.h>
  25. #include <grub/net.h>
  26. GRUB_MOD_LICENSE ("GPLv3+");
  27. #define UPDATE_INTERVAL 800
  28. static void
  29. grub_file_progress_hook_real (grub_disk_addr_t sector __attribute__ ((unused)),
  30. unsigned offset __attribute__ ((unused)),
  31. unsigned length, void *data)
  32. {
  33. static int call_depth = 0;
  34. grub_uint64_t now;
  35. static grub_uint64_t last_progress_update_time;
  36. grub_file_t file = data;
  37. const char *e;
  38. file->progress_offset += length;
  39. if (call_depth)
  40. return;
  41. e = grub_env_get ("enable_progress_indicator");
  42. if (e && e[0] == '0') {
  43. return;
  44. }
  45. call_depth = 1;
  46. now = grub_get_time_ms ();
  47. if (((now - last_progress_update_time > UPDATE_INTERVAL) &&
  48. (file->progress_offset - file->offset > 0)) ||
  49. (file->progress_offset == file->size))
  50. {
  51. static char buffer[80];
  52. struct grub_term_output *term;
  53. const char *partial_file_name;
  54. unsigned long long percent;
  55. grub_uint64_t current_speed;
  56. if (now - file->last_progress_time < 10)
  57. current_speed = 0;
  58. else
  59. current_speed = grub_divmod64 ((file->progress_offset
  60. - file->last_progress_offset)
  61. * 100ULL * 1000ULL,
  62. now - file->last_progress_time, 0);
  63. if (file->size == 0)
  64. percent = 100;
  65. else
  66. percent = grub_divmod64 (100 * file->progress_offset,
  67. file->size, 0);
  68. /* grub_net_fs_open() saves off partial file structure before name is initialized.
  69. It already saves passed file name in net structure so just use it in this case.
  70. */
  71. if (file->device->net)
  72. partial_file_name = grub_strrchr (file->device->net->name, '/');
  73. else if (file->name) /* grub_file_open() may leave it as NULL */
  74. partial_file_name = grub_strrchr (file->name, '/');
  75. else
  76. partial_file_name = NULL;
  77. if (partial_file_name)
  78. partial_file_name++;
  79. else
  80. partial_file_name = "";
  81. file->estimated_speed = (file->estimated_speed + current_speed) >> 1;
  82. grub_snprintf (buffer, sizeof (buffer), " [ %.20s %s %llu%% ",
  83. partial_file_name,
  84. grub_get_human_size (file->progress_offset,
  85. GRUB_HUMAN_SIZE_NORMAL),
  86. (unsigned long long) percent);
  87. char *ptr = buffer + grub_strlen (buffer);
  88. grub_snprintf (ptr, sizeof (buffer) - (ptr - buffer), "%s ]",
  89. grub_get_human_size (file->estimated_speed,
  90. GRUB_HUMAN_SIZE_SPEED));
  91. grub_size_t len = grub_strlen (buffer);
  92. FOR_ACTIVE_TERM_OUTPUTS (term)
  93. {
  94. if (term->progress_update_counter++ > term->progress_update_divisor
  95. || (file->progress_offset == file->size
  96. && term->progress_update_divisor
  97. != (unsigned) GRUB_PROGRESS_NO_UPDATE))
  98. {
  99. struct grub_term_coordinate old_pos = grub_term_getxy (term);
  100. struct grub_term_coordinate new_pos = old_pos;
  101. new_pos.x = grub_term_width (term) - len - 1;
  102. grub_term_gotoxy (term, new_pos);
  103. grub_puts_terminal (buffer, term);
  104. grub_term_gotoxy (term, old_pos);
  105. term->progress_update_counter = 0;
  106. if (term->refresh)
  107. term->refresh (term);
  108. }
  109. }
  110. file->last_progress_offset = file->progress_offset;
  111. file->last_progress_time = now;
  112. last_progress_update_time = now;
  113. }
  114. call_depth = 0;
  115. }
  116. GRUB_MOD_INIT(progress)
  117. {
  118. grub_file_progress_hook = grub_file_progress_hook_real;
  119. }
  120. GRUB_MOD_FINI(progress)
  121. {
  122. grub_file_progress_hook = 0;
  123. }