progress.c 4.5 KB

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