misc.c 5.42 KB
Newer Older
1 2 3
/* misc.c - miscellaneous functions */
/*
 *  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 10
 *  (at your option) any later version.
 *
11
 *  GRUB is distributed in the hope that it will be useful,
12 13 14 15 16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  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 20 21 22 23 24
 */

#include <grub/normal.h>
#include <grub/disk.h>
#include <grub/fs.h>
#include <grub/err.h>
#include <grub/misc.h>
25
#include <grub/mm.h>
26
#include <grub/datetime.h>
27 28
#include <grub/term.h>
#include <grub/i18n.h>
29
#include <grub/partition.h>
30

31 32 33 34 35 36 37 38 39 40 41 42 43
static const char *grub_human_sizes[3][6] =
  {
    /* This algorithm in reality would work only up to (2^64) / 100 B = 81 PiB.
       Put here all possible suffixes it can produce so no array bounds check
       is needed.
     */
    /* TRANSLATORS: that's the list of binary unit prefixes.  */
    { N_("B"),   N_("KiB"),   N_("MiB"),   N_("GiB"),   N_("TiB"),   N_("PiB")},
    /* TRANSLATORS: that's the list of binary unit prefixes.  */
    {    "",     N_("K"),     N_("M"),     N_("G"),     N_("T"),     N_("P") },
    /* TRANSLATORS: that's the list of binary unit prefixes.  */
    { N_("B/s"), N_("KiB/s"), N_("MiB/s"), N_("GiB/s"), N_("TiB/s"), N_("PiB/s"),  },    
  };
44 45

const char *
46
grub_get_human_size (grub_uint64_t size, enum grub_human_size_type type)
47
{
48 49 50 51
  grub_uint64_t fsize;
  unsigned units = 0;
  static char buf[30];
  const char *umsg;
52

53 54 55 56 57 58 59 60 61
  if (type != GRUB_HUMAN_SIZE_SPEED)
    fsize = size * 100ULL;
  else
    fsize = size;

  /* Since 2^64 / 1024^5  < 102400, this can give at most 5 iterations.
     So units <=5, so impossible to go past the end of array.
   */
  while (fsize >= 102400)
62 63 64 65 66
    {
      fsize = (fsize + 512) / 1024;
      units++;
    }

67 68 69
  umsg = _(grub_human_sizes[type][units]);

  if (units || type == GRUB_HUMAN_SIZE_SPEED)
70 71 72 73 74 75 76
    {
      grub_uint64_t whole, fraction;

      whole = grub_divmod64 (fsize, 100, &fraction);
      grub_snprintf (buf, sizeof (buf),
		     "%" PRIuGRUB_UINT64_T
		     ".%02" PRIuGRUB_UINT64_T "%s", whole, fraction,
77
		     umsg);
78 79 80
    }
  else
    grub_snprintf (buf, sizeof (buf), "%llu%s", (unsigned long long) size,
81
		   umsg);
82 83 84
  return buf;
}

85 86 87 88 89 90 91 92 93
/* Print the information on the device NAME.  */
grub_err_t
grub_normal_print_device_info (const char *name)
{
  grub_device_t dev;
  char *p;

  p = grub_strchr (name, ',');
  if (p)
94
    {
95
      grub_xputs ("\t");
96
      grub_printf_ (N_("Partition %s:"), name);
97
      grub_xputs (" ");
98
    }
99
  else
100 101
    {
      grub_printf_ (N_("Device %s:"), name);
102
      grub_xputs (" ");
103
    }
104

105 106
  dev = grub_device_open (name);
  if (! dev)
107
    grub_printf ("%s", _("Filesystem cannot be accessed"));
108
  else if (dev->disk)
109 110 111 112 113 114 115
    {
      grub_fs_t fs;

      fs = grub_fs_probe (dev);
      /* Ignore all errors.  */
      grub_errno = 0;

116
      if (fs)
117
	{
118 119 120 121
	  const char *fsname = fs->name;
	  if (grub_strcmp (fsname, "ext2") == 0)
	    fsname = "ext*";
	  grub_printf_ (N_("Filesystem type %s"), fsname);
122 123 124 125 126 127 128
	  if (fs->label)
	    {
	      char *label;
	      (fs->label) (dev, &label);
	      if (grub_errno == GRUB_ERR_NONE)
		{
		  if (label && grub_strlen (label))
129
		    {
130
		      grub_xputs (" ");
131
		      grub_printf_ (N_("- Label `%s'"), label);
132
		    }
133 134 135 136
		  grub_free (label);
		}
	      grub_errno = GRUB_ERR_NONE;
	    }
137 138 139 140 141 142 143 144
	  if (fs->mtime)
	    {
	      grub_int32_t tm;
	      struct grub_datetime datetime;
	      (fs->mtime) (dev, &tm);
	      if (grub_errno == GRUB_ERR_NONE)
		{
		  grub_unixtime2datetime (tm, &datetime);
145
		  grub_xputs (" ");
146 147
		  /* TRANSLATORS: Arguments are year, month, day, hour, minute,
		     second, day of the week (translated).  */
148 149
		  grub_printf_ (N_("- Last modification time %d-%02d-%02d "
			       "%02d:%02d:%02d %s"),
150 151 152 153 154 155 156
			       datetime.year, datetime.month, datetime.day,
			       datetime.hour, datetime.minute, datetime.second,
			       grub_get_weekday_name (&datetime));

		}
	      grub_errno = GRUB_ERR_NONE;
	    }
157 158 159 160 161 162 163 164 165 166 167 168 169
	  if (fs->uuid)
	    {
	      char *uuid;
	      (fs->uuid) (dev, &uuid);
	      if (grub_errno == GRUB_ERR_NONE)
		{
		  if (uuid && grub_strlen (uuid))
		    grub_printf (", UUID %s", uuid);
		  grub_free (uuid);
		}
	      grub_errno = GRUB_ERR_NONE;
	    }
	}
170
      else
171
	grub_printf ("%s", _("No known filesystem detected"));
172

173
      if (dev->disk->partition)
174 175 176 177 178
	grub_printf (_(" - Partition start at %llu%sKiB"),
		     (unsigned long long) (grub_partition_get_start (dev->disk->partition) >> 1),
		     (grub_partition_get_start (dev->disk->partition) & 1) ? ".5" : "" );
      else
	grub_printf_ (N_(" - Sector size %uB"), 1 << dev->disk->log_sector_size);
179
      if (grub_disk_get_size (dev->disk) == GRUB_DISK_SIZE_UNKNOWN)
180
	grub_puts_ (N_(" - Total size unknown"));
181
      else
182 183
	grub_printf (_(" - Total size %llu%sKiB"),
		     (unsigned long long) (grub_disk_get_size (dev->disk) >> 1),
184 185 186
		     /* TRANSLATORS: Replace dot with appropriate decimal separator for
			your language.  */
		     (grub_disk_get_size (dev->disk) & 1) ? _(".5") : "");
187 188
    }

189 190 191
  if (dev)
    grub_device_close (dev);

192
  grub_xputs ("\n");
193 194
  return grub_errno;
}