misc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* misc.c - miscellaneous functions */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2005,2007,2008,2009 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/normal.h>
  20. #include <grub/disk.h>
  21. #include <grub/fs.h>
  22. #include <grub/err.h>
  23. #include <grub/misc.h>
  24. #include <grub/mm.h>
  25. #include <grub/datetime.h>
  26. #include <grub/term.h>
  27. #include <grub/i18n.h>
  28. #include <grub/partition.h>
  29. static const char *grub_human_sizes[3][6] =
  30. {
  31. /* This algorithm in reality would work only up to (2^64) / 100 B = 81 PiB.
  32. Put here all possible suffixes it can produce so no array bounds check
  33. is needed.
  34. */
  35. /* TRANSLATORS: that's the list of binary unit prefixes. */
  36. { N_("B"), N_("KiB"), N_("MiB"), N_("GiB"), N_("TiB"), N_("PiB")},
  37. /* TRANSLATORS: that's the list of binary unit prefixes. */
  38. { "", N_("K"), N_("M"), N_("G"), N_("T"), N_("P") },
  39. /* TRANSLATORS: that's the list of binary unit prefixes. */
  40. { N_("B/s"), N_("KiB/s"), N_("MiB/s"), N_("GiB/s"), N_("TiB/s"), N_("PiB/s"), },
  41. };
  42. const char *
  43. grub_get_human_size (grub_uint64_t size, enum grub_human_size_type type)
  44. {
  45. grub_uint64_t fsize;
  46. unsigned units = 0;
  47. static char buf[30];
  48. const char *umsg;
  49. if (type != GRUB_HUMAN_SIZE_SPEED)
  50. fsize = size * 100ULL;
  51. else
  52. fsize = size;
  53. /* Since 2^64 / 1024^5 < 102400, this can give at most 5 iterations.
  54. So units <=5, so impossible to go past the end of array.
  55. */
  56. while (fsize >= 102400)
  57. {
  58. fsize = (fsize + 512) / 1024;
  59. units++;
  60. }
  61. umsg = _(grub_human_sizes[type][units]);
  62. if (units || type == GRUB_HUMAN_SIZE_SPEED)
  63. {
  64. grub_uint64_t whole, fraction;
  65. whole = grub_divmod64 (fsize, 100, &fraction);
  66. grub_snprintf (buf, sizeof (buf),
  67. "%" PRIuGRUB_UINT64_T
  68. ".%02" PRIuGRUB_UINT64_T "%s", whole, fraction,
  69. umsg);
  70. }
  71. else
  72. grub_snprintf (buf, sizeof (buf), "%llu%s", (unsigned long long) size,
  73. umsg);
  74. return buf;
  75. }
  76. /* Print the information on the device NAME. */
  77. grub_err_t
  78. grub_normal_print_device_info (const char *name)
  79. {
  80. grub_device_t dev;
  81. char *p;
  82. p = grub_strchr (name, ',');
  83. if (p)
  84. {
  85. grub_xputs ("\t");
  86. grub_printf_ (N_("Partition %s:"), name);
  87. grub_xputs (" ");
  88. }
  89. else
  90. {
  91. grub_printf_ (N_("Device %s:"), name);
  92. grub_xputs (" ");
  93. }
  94. dev = grub_device_open (name);
  95. if (! dev)
  96. grub_printf ("%s", _("Filesystem cannot be accessed"));
  97. else if (dev->disk)
  98. {
  99. grub_fs_t fs;
  100. fs = grub_fs_probe (dev);
  101. /* Ignore all errors. */
  102. grub_errno = 0;
  103. if (fs)
  104. {
  105. const char *fsname = fs->name;
  106. if (grub_strcmp (fsname, "ext2") == 0)
  107. fsname = "ext*";
  108. grub_printf_ (N_("Filesystem type %s"), fsname);
  109. if (fs->fs_label)
  110. {
  111. char *label;
  112. (fs->fs_label) (dev, &label);
  113. if (grub_errno == GRUB_ERR_NONE)
  114. {
  115. if (label && grub_strlen (label))
  116. {
  117. grub_xputs (" ");
  118. grub_printf_ (N_("- Label `%s'"), label);
  119. }
  120. grub_free (label);
  121. }
  122. grub_errno = GRUB_ERR_NONE;
  123. }
  124. if (fs->fs_mtime)
  125. {
  126. grub_int64_t tm;
  127. struct grub_datetime datetime;
  128. (fs->fs_mtime) (dev, &tm);
  129. if (grub_errno == GRUB_ERR_NONE)
  130. {
  131. grub_unixtime2datetime (tm, &datetime);
  132. grub_xputs (" ");
  133. /* TRANSLATORS: Arguments are year, month, day, hour, minute,
  134. second, day of the week (translated). */
  135. grub_printf_ (N_("- Last modification time %d-%02d-%02d "
  136. "%02d:%02d:%02d %s"),
  137. datetime.year, datetime.month, datetime.day,
  138. datetime.hour, datetime.minute, datetime.second,
  139. grub_get_weekday_name (&datetime));
  140. }
  141. grub_errno = GRUB_ERR_NONE;
  142. }
  143. if (fs->fs_uuid)
  144. {
  145. char *uuid;
  146. (fs->fs_uuid) (dev, &uuid);
  147. if (grub_errno == GRUB_ERR_NONE)
  148. {
  149. if (uuid && grub_strlen (uuid))
  150. grub_printf (", UUID %s", uuid);
  151. grub_free (uuid);
  152. }
  153. grub_errno = GRUB_ERR_NONE;
  154. }
  155. }
  156. else
  157. grub_printf ("%s", _("No known filesystem detected"));
  158. if (dev->disk->partition)
  159. grub_printf (_(" - Partition start at %llu%sKiB"),
  160. (unsigned long long) (grub_partition_get_start (dev->disk->partition) >> 1),
  161. (grub_partition_get_start (dev->disk->partition) & 1) ? ".5" : "" );
  162. else
  163. grub_printf_ (N_(" - Sector size %uB"), 1 << dev->disk->log_sector_size);
  164. if (grub_disk_native_sectors (dev->disk) == GRUB_DISK_SIZE_UNKNOWN)
  165. grub_puts_ (N_(" - Total size unknown"));
  166. else
  167. grub_printf (_(" - Total size %llu%sKiB"),
  168. (unsigned long long) (grub_disk_native_sectors (dev->disk) >> 1),
  169. /* TRANSLATORS: Replace dot with appropriate decimal separator for
  170. your language. */
  171. (grub_disk_native_sectors (dev->disk) & 1) ? _(".5") : "");
  172. }
  173. if (dev)
  174. grub_device_close (dev);
  175. grub_xputs ("\n");
  176. return grub_errno;
  177. }