]> git.proxmox.com Git - grub2.git/blob - grub-core/normal/misc.c
* grub-core/commands/testspeed.c: New command testspeed.
[grub2.git] / grub-core / normal / misc.c
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
20 #include <grub/normal.h>
21 #include <grub/disk.h>
22 #include <grub/fs.h>
23 #include <grub/err.h>
24 #include <grub/misc.h>
25 #include <grub/mm.h>
26 #include <grub/datetime.h>
27 #include <grub/term.h>
28 #include <grub/i18n.h>
29 #include <grub/partition.h>
30
31 static const char *grub_human_sizes[3][6] =
32 {
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
35 is needed.
36 */
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"), },
43 };
44
45 const char *
46 grub_get_human_size (grub_uint64_t size, enum grub_human_size_type type)
47 {
48 grub_uint64_t fsize;
49 unsigned units = 0;
50 static char buf[30];
51 const char *umsg;
52
53 if (type != GRUB_HUMAN_SIZE_SPEED)
54 fsize = size * 100ULL;
55 else
56 fsize = size;
57
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.
60 */
61 while (fsize >= 102400)
62 {
63 fsize = (fsize + 512) / 1024;
64 units++;
65 }
66
67 umsg = _(grub_human_sizes[type][units]);
68
69 if (units || type == GRUB_HUMAN_SIZE_SPEED)
70 {
71 grub_uint64_t whole, fraction;
72
73 whole = grub_divmod64 (fsize, 100, &fraction);
74 grub_snprintf (buf, sizeof (buf),
75 "%" PRIuGRUB_UINT64_T
76 ".%02" PRIuGRUB_UINT64_T "%s", whole, fraction,
77 umsg);
78 }
79 else
80 grub_snprintf (buf, sizeof (buf), "%llu%s", (unsigned long long) size,
81 umsg);
82 return buf;
83 }
84
85 /* Print the information on the device NAME. */
86 grub_err_t
87 grub_normal_print_device_info (const char *name)
88 {
89 grub_device_t dev;
90 char *p;
91
92 p = grub_strchr (name, ',');
93 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 dev = grub_device_open (name);
106 if (! dev)
107 grub_printf ("%s", _("Filesystem cannot be accessed"));
108 else if (dev->disk)
109 {
110 grub_fs_t fs;
111
112 fs = grub_fs_probe (dev);
113 /* Ignore all errors. */
114 grub_errno = 0;
115
116 if (fs)
117 {
118 const char *fsname = fs->name;
119 if (grub_strcmp (fsname, "ext2") == 0)
120 fsname = "ext*";
121 grub_printf_ (N_("Filesystem type %s"), fsname);
122 if (fs->label)
123 {
124 char *label;
125 (fs->label) (dev, &label);
126 if (grub_errno == GRUB_ERR_NONE)
127 {
128 if (label && grub_strlen (label))
129 {
130 grub_xputs (" ");
131 grub_printf_ (N_("- Label `%s'"), label);
132 }
133 grub_free (label);
134 }
135 grub_errno = GRUB_ERR_NONE;
136 }
137 if (fs->mtime)
138 {
139 grub_int32_t tm;
140 struct grub_datetime datetime;
141 (fs->mtime) (dev, &tm);
142 if (grub_errno == GRUB_ERR_NONE)
143 {
144 grub_unixtime2datetime (tm, &datetime);
145 grub_xputs (" ");
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));
153
154 }
155 grub_errno = GRUB_ERR_NONE;
156 }
157 if (fs->uuid)
158 {
159 char *uuid;
160 (fs->uuid) (dev, &uuid);
161 if (grub_errno == GRUB_ERR_NONE)
162 {
163 if (uuid && grub_strlen (uuid))
164 grub_printf (", UUID %s", uuid);
165 grub_free (uuid);
166 }
167 grub_errno = GRUB_ERR_NONE;
168 }
169 }
170 else
171 grub_printf ("%s", _("No known filesystem detected"));
172
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" : "" );
177 else
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"));
181 else
182 grub_printf (_(" - Total size %llu%sKiB"),
183 (unsigned long long) (grub_disk_get_size (dev->disk) >> 1),
184 (grub_disk_get_size (dev->disk) & 1) ? ".5" : "");
185
186 grub_device_close (dev);
187 }
188
189 grub_xputs ("\n");
190 return grub_errno;
191 }