1 /* misc.c - miscellaneous functions */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2005,2007,2008,2009 Free Software Foundation, Inc.
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.
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.
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/>.
20 #include <grub/normal.h>
21 #include <grub/disk.h>
24 #include <grub/misc.h>
26 #include <grub/datetime.h>
27 #include <grub/term.h>
28 #include <grub/i18n.h>
29 #include <grub/partition.h>
31 static const char *grub_human_sizes
[3][6] =
33 /* This algorithm in reality would work only up to (2^64) / 100 B = 81 PiB.
34 Put here all possible suffixes it can produce so no array bounds check
37 /* TRANSLATORS: that's the list of binary unit prefixes. */
38 { N_("B"), N_("KiB"), N_("MiB"), N_("GiB"), N_("TiB"), N_("PiB")},
39 /* TRANSLATORS: that's the list of binary unit prefixes. */
40 { "", N_("K"), N_("M"), N_("G"), N_("T"), N_("P") },
41 /* TRANSLATORS: that's the list of binary unit prefixes. */
42 { N_("B/s"), N_("KiB/s"), N_("MiB/s"), N_("GiB/s"), N_("TiB/s"), N_("PiB/s"), },
46 grub_get_human_size (grub_uint64_t size
, enum grub_human_size_type type
)
53 if (type
!= GRUB_HUMAN_SIZE_SPEED
)
54 fsize
= size
* 100ULL;
58 /* Since 2^64 / 1024^5 < 102400, this can give at most 5 iterations.
59 So units <=5, so impossible to go past the end of array.
61 while (fsize
>= 102400)
63 fsize
= (fsize
+ 512) / 1024;
67 umsg
= _(grub_human_sizes
[type
][units
]);
69 if (units
|| type
== GRUB_HUMAN_SIZE_SPEED
)
71 grub_uint64_t whole
, fraction
;
73 whole
= grub_divmod64 (fsize
, 100, &fraction
);
74 grub_snprintf (buf
, sizeof (buf
),
76 ".%02" PRIuGRUB_UINT64_T
"%s", whole
, fraction
,
80 grub_snprintf (buf
, sizeof (buf
), "%llu%s", (unsigned long long) size
,
85 /* Print the information on the device NAME. */
87 grub_normal_print_device_info (const char *name
)
92 p
= grub_strchr (name
, ',');
96 grub_printf_ (N_("Partition %s:"), name
);
101 grub_printf_ (N_("Device %s:"), name
);
105 dev
= grub_device_open (name
);
107 grub_printf ("%s", _("Filesystem cannot be accessed"));
112 fs
= grub_fs_probe (dev
);
113 /* Ignore all errors. */
118 const char *fsname
= fs
->name
;
119 if (grub_strcmp (fsname
, "ext2") == 0)
121 grub_printf_ (N_("Filesystem type %s"), fsname
);
125 (fs
->label
) (dev
, &label
);
126 if (grub_errno
== GRUB_ERR_NONE
)
128 if (label
&& grub_strlen (label
))
131 grub_printf_ (N_("- Label `%s'"), label
);
135 grub_errno
= GRUB_ERR_NONE
;
140 struct grub_datetime datetime
;
141 (fs
->mtime
) (dev
, &tm
);
142 if (grub_errno
== GRUB_ERR_NONE
)
144 grub_unixtime2datetime (tm
, &datetime
);
146 /* TRANSLATORS: Arguments are year, month, day, hour, minute,
147 second, day of the week (translated). */
148 grub_printf_ (N_("- Last modification time %d-%02d-%02d "
149 "%02d:%02d:%02d %s"),
150 datetime
.year
, datetime
.month
, datetime
.day
,
151 datetime
.hour
, datetime
.minute
, datetime
.second
,
152 grub_get_weekday_name (&datetime
));
155 grub_errno
= GRUB_ERR_NONE
;
160 (fs
->uuid
) (dev
, &uuid
);
161 if (grub_errno
== GRUB_ERR_NONE
)
163 if (uuid
&& grub_strlen (uuid
))
164 grub_printf (", UUID %s", uuid
);
167 grub_errno
= GRUB_ERR_NONE
;
171 grub_printf ("%s", _("No known filesystem detected"));
173 if (dev
->disk
->partition
)
174 grub_printf (_(" - Partition start at %llu%sKiB"),
175 (unsigned long long) (grub_partition_get_start (dev
->disk
->partition
) >> 1),
176 (grub_partition_get_start (dev
->disk
->partition
) & 1) ? ".5" : "" );
178 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"));
182 grub_printf (_(" - Total size %llu%sKiB"),
183 (unsigned long long) (grub_disk_get_size (dev
->disk
) >> 1),
184 /* TRANSLATORS: Replace dot with appropriate decimal separator for
186 (grub_disk_get_size (dev
->disk
) & 1) ? _(".5") : "");
190 grub_device_close (dev
);